2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "coretypes.h"
27 #include "dyn-string.h"
35 #include "diagnostic.h"
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
50 typedef struct cp_token
GTY (())
52 /* The kind of token. */
53 ENUM_BITFIELD (cpp_ttype
) type
: 8;
54 /* If this token is a keyword, this value indicates which keyword.
55 Otherwise, this value is RID_MAX. */
56 ENUM_BITFIELD (rid
) keyword
: 8;
59 /* Identifier for the pragma. */
60 ENUM_BITFIELD (pragma_kind
) pragma_kind
: 6;
61 /* True if this token is from a system header. */
62 BOOL_BITFIELD in_system_header
: 1;
63 /* True if this token is from a context where it is implicitly extern "C" */
64 BOOL_BITFIELD implicit_extern_c
: 1;
65 /* True for a CPP_NAME token that is not a keyword (i.e., for which
66 KEYWORD is RID_MAX) iff this name was looked up and found to be
67 ambiguous. An error has already been reported. */
68 BOOL_BITFIELD ambiguous_p
: 1;
69 /* The value associated with this token, if any. */
71 /* The location at which this token was found. */
75 /* We use a stack of token pointer for saving token sets. */
76 typedef struct cp_token
*cp_token_position
;
77 DEF_VEC_P (cp_token_position
);
78 DEF_VEC_ALLOC_P (cp_token_position
,heap
);
80 static const cp_token eof_token
=
82 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, 0, 0, false, NULL_TREE
,
83 #if USE_MAPPED_LOCATION
90 /* The cp_lexer structure represents the C++ lexer. It is responsible
91 for managing the token stream from the preprocessor and supplying
92 it to the parser. Tokens are never added to the cp_lexer after
95 typedef struct cp_lexer
GTY (())
97 /* The memory allocated for the buffer. NULL if this lexer does not
98 own the token buffer. */
99 cp_token
* GTY ((length ("%h.buffer_length"))) buffer
;
100 /* If the lexer owns the buffer, this is the number of tokens in the
102 size_t buffer_length
;
104 /* A pointer just past the last available token. The tokens
105 in this lexer are [buffer, last_token). */
106 cp_token_position
GTY ((skip
)) last_token
;
108 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
109 no more available tokens. */
110 cp_token_position
GTY ((skip
)) next_token
;
112 /* A stack indicating positions at which cp_lexer_save_tokens was
113 called. The top entry is the most recent position at which we
114 began saving tokens. If the stack is non-empty, we are saving
116 VEC(cp_token_position
,heap
) *GTY ((skip
)) saved_tokens
;
118 /* The next lexer in a linked list of lexers. */
119 struct cp_lexer
*next
;
121 /* True if we should output debugging information. */
124 /* True if we're in the context of parsing a pragma, and should not
125 increment past the end-of-line marker. */
129 /* cp_token_cache is a range of tokens. There is no need to represent
130 allocate heap memory for it, since tokens are never removed from the
131 lexer's array. There is also no need for the GC to walk through
132 a cp_token_cache, since everything in here is referenced through
135 typedef struct cp_token_cache
GTY(())
137 /* The beginning of the token range. */
138 cp_token
* GTY((skip
)) first
;
140 /* Points immediately after the last token in the range. */
141 cp_token
* GTY ((skip
)) last
;
146 static cp_lexer
*cp_lexer_new_main
148 static cp_lexer
*cp_lexer_new_from_tokens
149 (cp_token_cache
*tokens
);
150 static void cp_lexer_destroy
152 static int cp_lexer_saving_tokens
154 static cp_token_position cp_lexer_token_position
156 static cp_token
*cp_lexer_token_at
157 (cp_lexer
*, cp_token_position
);
158 static void cp_lexer_get_preprocessor_token
159 (cp_lexer
*, cp_token
*);
160 static inline cp_token
*cp_lexer_peek_token
162 static cp_token
*cp_lexer_peek_nth_token
163 (cp_lexer
*, size_t);
164 static inline bool cp_lexer_next_token_is
165 (cp_lexer
*, enum cpp_ttype
);
166 static bool cp_lexer_next_token_is_not
167 (cp_lexer
*, enum cpp_ttype
);
168 static bool cp_lexer_next_token_is_keyword
169 (cp_lexer
*, enum rid
);
170 static cp_token
*cp_lexer_consume_token
172 static void cp_lexer_purge_token
174 static void cp_lexer_purge_tokens_after
175 (cp_lexer
*, cp_token_position
);
176 static void cp_lexer_save_tokens
178 static void cp_lexer_commit_tokens
180 static void cp_lexer_rollback_tokens
182 #ifdef ENABLE_CHECKING
183 static void cp_lexer_print_token
184 (FILE *, cp_token
*);
185 static inline bool cp_lexer_debugging_p
187 static void cp_lexer_start_debugging
188 (cp_lexer
*) ATTRIBUTE_UNUSED
;
189 static void cp_lexer_stop_debugging
190 (cp_lexer
*) ATTRIBUTE_UNUSED
;
192 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
193 about passing NULL to functions that require non-NULL arguments
194 (fputs, fprintf). It will never be used, so all we need is a value
195 of the right type that's guaranteed not to be NULL. */
196 #define cp_lexer_debug_stream stdout
197 #define cp_lexer_print_token(str, tok) (void) 0
198 #define cp_lexer_debugging_p(lexer) 0
199 #endif /* ENABLE_CHECKING */
201 static cp_token_cache
*cp_token_cache_new
202 (cp_token
*, cp_token
*);
204 static void cp_parser_initial_pragma
207 /* Manifest constants. */
208 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
209 #define CP_SAVED_TOKEN_STACK 5
211 /* A token type for keywords, as opposed to ordinary identifiers. */
212 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
214 /* A token type for template-ids. If a template-id is processed while
215 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
216 the value of the CPP_TEMPLATE_ID is whatever was returned by
217 cp_parser_template_id. */
218 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
220 /* A token type for nested-name-specifiers. If a
221 nested-name-specifier is processed while parsing tentatively, it is
222 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
223 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
224 cp_parser_nested_name_specifier_opt. */
225 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
227 /* A token type for tokens that are not tokens at all; these are used
228 to represent slots in the array where there used to be a token
229 that has now been deleted. */
230 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
232 /* The number of token types, including C++-specific ones. */
233 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
237 #ifdef ENABLE_CHECKING
238 /* The stream to which debugging output should be written. */
239 static FILE *cp_lexer_debug_stream
;
240 #endif /* ENABLE_CHECKING */
242 /* Create a new main C++ lexer, the lexer that gets tokens from the
246 cp_lexer_new_main (void)
248 cp_token first_token
;
255 /* It's possible that parsing the first pragma will load a PCH file,
256 which is a GC collection point. So we have to do that before
257 allocating any memory. */
258 cp_parser_initial_pragma (&first_token
);
260 /* Tell c_lex_with_flags not to merge string constants. */
261 c_lex_return_raw_strings
= true;
263 c_common_no_more_pch ();
265 /* Allocate the memory. */
266 lexer
= GGC_CNEW (cp_lexer
);
268 #ifdef ENABLE_CHECKING
269 /* Initially we are not debugging. */
270 lexer
->debugging_p
= false;
271 #endif /* ENABLE_CHECKING */
272 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
273 CP_SAVED_TOKEN_STACK
);
275 /* Create the buffer. */
276 alloc
= CP_LEXER_BUFFER_SIZE
;
277 buffer
= GGC_NEWVEC (cp_token
, alloc
);
279 /* Put the first token in the buffer. */
284 /* Get the remaining tokens from the preprocessor. */
285 while (pos
->type
!= CPP_EOF
)
292 buffer
= GGC_RESIZEVEC (cp_token
, buffer
, alloc
);
293 pos
= buffer
+ space
;
295 cp_lexer_get_preprocessor_token (lexer
, pos
);
297 lexer
->buffer
= buffer
;
298 lexer
->buffer_length
= alloc
- space
;
299 lexer
->last_token
= pos
;
300 lexer
->next_token
= lexer
->buffer_length
? buffer
: (cp_token
*)&eof_token
;
302 /* Subsequent preprocessor diagnostics should use compiler
303 diagnostic functions to get the compiler source location. */
304 cpp_get_options (parse_in
)->client_diagnostic
= true;
305 cpp_get_callbacks (parse_in
)->error
= cp_cpp_error
;
307 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
311 /* Create a new lexer whose token stream is primed with the tokens in
312 CACHE. When these tokens are exhausted, no new tokens will be read. */
315 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
317 cp_token
*first
= cache
->first
;
318 cp_token
*last
= cache
->last
;
319 cp_lexer
*lexer
= GGC_CNEW (cp_lexer
);
321 /* We do not own the buffer. */
322 lexer
->buffer
= NULL
;
323 lexer
->buffer_length
= 0;
324 lexer
->next_token
= first
== last
? (cp_token
*)&eof_token
: first
;
325 lexer
->last_token
= last
;
327 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
328 CP_SAVED_TOKEN_STACK
);
330 #ifdef ENABLE_CHECKING
331 /* Initially we are not debugging. */
332 lexer
->debugging_p
= false;
335 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
339 /* Frees all resources associated with LEXER. */
342 cp_lexer_destroy (cp_lexer
*lexer
)
345 ggc_free (lexer
->buffer
);
346 VEC_free (cp_token_position
, heap
, lexer
->saved_tokens
);
350 /* Returns nonzero if debugging information should be output. */
352 #ifdef ENABLE_CHECKING
355 cp_lexer_debugging_p (cp_lexer
*lexer
)
357 return lexer
->debugging_p
;
360 #endif /* ENABLE_CHECKING */
362 static inline cp_token_position
363 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
365 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
367 return lexer
->next_token
- previous_p
;
370 static inline cp_token
*
371 cp_lexer_token_at (cp_lexer
*lexer ATTRIBUTE_UNUSED
, cp_token_position pos
)
376 /* nonzero if we are presently saving tokens. */
379 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
381 return VEC_length (cp_token_position
, lexer
->saved_tokens
) != 0;
384 /* Store the next token from the preprocessor in *TOKEN. Return true
388 cp_lexer_get_preprocessor_token (cp_lexer
*lexer ATTRIBUTE_UNUSED
,
391 static int is_extern_c
= 0;
393 /* Get a new token from the preprocessor. */
395 = c_lex_with_flags (&token
->value
, &token
->location
, &token
->flags
);
396 token
->keyword
= RID_MAX
;
397 token
->pragma_kind
= PRAGMA_NONE
;
398 token
->in_system_header
= in_system_header
;
400 /* On some systems, some header files are surrounded by an
401 implicit extern "C" block. Set a flag in the token if it
402 comes from such a header. */
403 is_extern_c
+= pending_lang_change
;
404 pending_lang_change
= 0;
405 token
->implicit_extern_c
= is_extern_c
> 0;
407 /* Check to see if this token is a keyword. */
408 if (token
->type
== CPP_NAME
)
410 if (C_IS_RESERVED_WORD (token
->value
))
412 /* Mark this token as a keyword. */
413 token
->type
= CPP_KEYWORD
;
414 /* Record which keyword. */
415 token
->keyword
= C_RID_CODE (token
->value
);
416 /* Update the value. Some keywords are mapped to particular
417 entities, rather than simply having the value of the
418 corresponding IDENTIFIER_NODE. For example, `__const' is
419 mapped to `const'. */
420 token
->value
= ridpointers
[token
->keyword
];
424 token
->ambiguous_p
= false;
425 token
->keyword
= RID_MAX
;
428 /* Handle Objective-C++ keywords. */
429 else if (token
->type
== CPP_AT_NAME
)
431 token
->type
= CPP_KEYWORD
;
432 switch (C_RID_CODE (token
->value
))
434 /* Map 'class' to '@class', 'private' to '@private', etc. */
435 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
436 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
437 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
438 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
439 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
440 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
441 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
442 default: token
->keyword
= C_RID_CODE (token
->value
);
445 else if (token
->type
== CPP_PRAGMA
)
447 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
448 token
->pragma_kind
= TREE_INT_CST_LOW (token
->value
);
453 /* Update the globals input_location and in_system_header from TOKEN. */
455 cp_lexer_set_source_position_from_token (cp_token
*token
)
457 if (token
->type
!= CPP_EOF
)
459 input_location
= token
->location
;
460 in_system_header
= token
->in_system_header
;
464 /* Return a pointer to the next token in the token stream, but do not
467 static inline cp_token
*
468 cp_lexer_peek_token (cp_lexer
*lexer
)
470 if (cp_lexer_debugging_p (lexer
))
472 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
473 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
474 putc ('\n', cp_lexer_debug_stream
);
476 return lexer
->next_token
;
479 /* Return true if the next token has the indicated TYPE. */
482 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
484 return cp_lexer_peek_token (lexer
)->type
== type
;
487 /* Return true if the next token does not have the indicated TYPE. */
490 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
492 return !cp_lexer_next_token_is (lexer
, type
);
495 /* Return true if the next token is the indicated KEYWORD. */
498 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
500 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
503 /* Return a pointer to the Nth token in the token stream. If N is 1,
504 then this is precisely equivalent to cp_lexer_peek_token (except
505 that it is not inline). One would like to disallow that case, but
506 there is one case (cp_parser_nth_token_starts_template_id) where
507 the caller passes a variable for N and it might be 1. */
510 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
514 /* N is 1-based, not zero-based. */
517 if (cp_lexer_debugging_p (lexer
))
518 fprintf (cp_lexer_debug_stream
,
519 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
522 token
= lexer
->next_token
;
523 gcc_assert (!n
|| token
!= &eof_token
);
527 if (token
== lexer
->last_token
)
529 token
= (cp_token
*)&eof_token
;
533 if (token
->type
!= CPP_PURGED
)
537 if (cp_lexer_debugging_p (lexer
))
539 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
540 putc ('\n', cp_lexer_debug_stream
);
546 /* Return the next token, and advance the lexer's next_token pointer
547 to point to the next non-purged token. */
550 cp_lexer_consume_token (cp_lexer
* lexer
)
552 cp_token
*token
= lexer
->next_token
;
554 gcc_assert (token
!= &eof_token
);
555 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
560 if (lexer
->next_token
== lexer
->last_token
)
562 lexer
->next_token
= (cp_token
*)&eof_token
;
567 while (lexer
->next_token
->type
== CPP_PURGED
);
569 cp_lexer_set_source_position_from_token (token
);
571 /* Provide debugging output. */
572 if (cp_lexer_debugging_p (lexer
))
574 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
575 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
576 putc ('\n', cp_lexer_debug_stream
);
582 /* Permanently remove the next token from the token stream, and
583 advance the next_token pointer to refer to the next non-purged
587 cp_lexer_purge_token (cp_lexer
*lexer
)
589 cp_token
*tok
= lexer
->next_token
;
591 gcc_assert (tok
!= &eof_token
);
592 tok
->type
= CPP_PURGED
;
593 tok
->location
= UNKNOWN_LOCATION
;
594 tok
->value
= NULL_TREE
;
595 tok
->keyword
= RID_MAX
;
600 if (tok
== lexer
->last_token
)
602 tok
= (cp_token
*)&eof_token
;
606 while (tok
->type
== CPP_PURGED
);
607 lexer
->next_token
= tok
;
610 /* Permanently remove all tokens after TOK, up to, but not
611 including, the token that will be returned next by
612 cp_lexer_peek_token. */
615 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
617 cp_token
*peek
= lexer
->next_token
;
619 if (peek
== &eof_token
)
620 peek
= lexer
->last_token
;
622 gcc_assert (tok
< peek
);
624 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
626 tok
->type
= CPP_PURGED
;
627 tok
->location
= UNKNOWN_LOCATION
;
628 tok
->value
= NULL_TREE
;
629 tok
->keyword
= RID_MAX
;
633 /* Begin saving tokens. All tokens consumed after this point will be
637 cp_lexer_save_tokens (cp_lexer
* lexer
)
639 /* Provide debugging output. */
640 if (cp_lexer_debugging_p (lexer
))
641 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
643 VEC_safe_push (cp_token_position
, heap
,
644 lexer
->saved_tokens
, lexer
->next_token
);
647 /* Commit to the portion of the token stream most recently saved. */
650 cp_lexer_commit_tokens (cp_lexer
* lexer
)
652 /* Provide debugging output. */
653 if (cp_lexer_debugging_p (lexer
))
654 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
656 VEC_pop (cp_token_position
, lexer
->saved_tokens
);
659 /* Return all tokens saved since the last call to cp_lexer_save_tokens
660 to the token stream. Stop saving tokens. */
663 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
665 /* Provide debugging output. */
666 if (cp_lexer_debugging_p (lexer
))
667 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
669 lexer
->next_token
= VEC_pop (cp_token_position
, lexer
->saved_tokens
);
672 /* Print a representation of the TOKEN on the STREAM. */
674 #ifdef ENABLE_CHECKING
677 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
679 /* We don't use cpp_type2name here because the parser defines
680 a few tokens of its own. */
681 static const char *const token_names
[] = {
682 /* cpplib-defined token types */
688 /* C++ parser token types - see "Manifest constants", above. */
691 "NESTED_NAME_SPECIFIER",
695 /* If we have a name for the token, print it out. Otherwise, we
696 simply give the numeric code. */
697 gcc_assert (token
->type
< ARRAY_SIZE(token_names
));
698 fputs (token_names
[token
->type
], stream
);
700 /* For some tokens, print the associated data. */
704 /* Some keywords have a value that is not an IDENTIFIER_NODE.
705 For example, `struct' is mapped to an INTEGER_CST. */
706 if (TREE_CODE (token
->value
) != IDENTIFIER_NODE
)
708 /* else fall through */
710 fputs (IDENTIFIER_POINTER (token
->value
), stream
);
715 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->value
));
723 /* Start emitting debugging information. */
726 cp_lexer_start_debugging (cp_lexer
* lexer
)
728 lexer
->debugging_p
= true;
731 /* Stop emitting debugging information. */
734 cp_lexer_stop_debugging (cp_lexer
* lexer
)
736 lexer
->debugging_p
= false;
739 #endif /* ENABLE_CHECKING */
741 /* Create a new cp_token_cache, representing a range of tokens. */
743 static cp_token_cache
*
744 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
746 cp_token_cache
*cache
= GGC_NEW (cp_token_cache
);
747 cache
->first
= first
;
753 /* Decl-specifiers. */
755 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
758 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
760 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
765 /* Nothing other than the parser should be creating declarators;
766 declarators are a semi-syntactic representation of C++ entities.
767 Other parts of the front end that need to create entities (like
768 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
770 static cp_declarator
*make_call_declarator
771 (cp_declarator
*, cp_parameter_declarator
*, cp_cv_quals
, tree
);
772 static cp_declarator
*make_array_declarator
773 (cp_declarator
*, tree
);
774 static cp_declarator
*make_pointer_declarator
775 (cp_cv_quals
, cp_declarator
*);
776 static cp_declarator
*make_reference_declarator
777 (cp_cv_quals
, cp_declarator
*);
778 static cp_parameter_declarator
*make_parameter_declarator
779 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
780 static cp_declarator
*make_ptrmem_declarator
781 (cp_cv_quals
, tree
, cp_declarator
*);
783 /* An erroneous declarator. */
784 static cp_declarator
*cp_error_declarator
;
786 /* The obstack on which declarators and related data structures are
788 static struct obstack declarator_obstack
;
790 /* Alloc BYTES from the declarator memory pool. */
793 alloc_declarator (size_t bytes
)
795 return obstack_alloc (&declarator_obstack
, bytes
);
798 /* Allocate a declarator of the indicated KIND. Clear fields that are
799 common to all declarators. */
801 static cp_declarator
*
802 make_declarator (cp_declarator_kind kind
)
804 cp_declarator
*declarator
;
806 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
807 declarator
->kind
= kind
;
808 declarator
->attributes
= NULL_TREE
;
809 declarator
->declarator
= NULL
;
814 /* Make a declarator for a generalized identifier. If
815 QUALIFYING_SCOPE is non-NULL, the identifier is
816 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
817 UNQUALIFIED_NAME. SFK indicates the kind of special function this
820 static cp_declarator
*
821 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
822 special_function_kind sfk
)
824 cp_declarator
*declarator
;
826 /* It is valid to write:
828 class C { void f(); };
832 The standard is not clear about whether `typedef const C D' is
833 legal; as of 2002-09-15 the committee is considering that
834 question. EDG 3.0 allows that syntax. Therefore, we do as
836 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
837 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
839 gcc_assert (TREE_CODE (unqualified_name
) == IDENTIFIER_NODE
840 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
841 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
843 declarator
= make_declarator (cdk_id
);
844 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
845 declarator
->u
.id
.unqualified_name
= unqualified_name
;
846 declarator
->u
.id
.sfk
= sfk
;
851 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
852 of modifiers such as const or volatile to apply to the pointer
853 type, represented as identifiers. */
856 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
858 cp_declarator
*declarator
;
860 declarator
= make_declarator (cdk_pointer
);
861 declarator
->declarator
= target
;
862 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
863 declarator
->u
.pointer
.class_type
= NULL_TREE
;
868 /* Like make_pointer_declarator -- but for references. */
871 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
873 cp_declarator
*declarator
;
875 declarator
= make_declarator (cdk_reference
);
876 declarator
->declarator
= target
;
877 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
878 declarator
->u
.pointer
.class_type
= NULL_TREE
;
883 /* Like make_pointer_declarator -- but for a pointer to a non-static
884 member of CLASS_TYPE. */
887 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
888 cp_declarator
*pointee
)
890 cp_declarator
*declarator
;
892 declarator
= make_declarator (cdk_ptrmem
);
893 declarator
->declarator
= pointee
;
894 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
895 declarator
->u
.pointer
.class_type
= class_type
;
900 /* Make a declarator for the function given by TARGET, with the
901 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
902 "const"-qualified member function. The EXCEPTION_SPECIFICATION
903 indicates what exceptions can be thrown. */
906 make_call_declarator (cp_declarator
*target
,
907 cp_parameter_declarator
*parms
,
908 cp_cv_quals cv_qualifiers
,
909 tree exception_specification
)
911 cp_declarator
*declarator
;
913 declarator
= make_declarator (cdk_function
);
914 declarator
->declarator
= target
;
915 declarator
->u
.function
.parameters
= parms
;
916 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
917 declarator
->u
.function
.exception_specification
= exception_specification
;
922 /* Make a declarator for an array of BOUNDS elements, each of which is
923 defined by ELEMENT. */
926 make_array_declarator (cp_declarator
*element
, tree bounds
)
928 cp_declarator
*declarator
;
930 declarator
= make_declarator (cdk_array
);
931 declarator
->declarator
= element
;
932 declarator
->u
.array
.bounds
= bounds
;
937 cp_parameter_declarator
*no_parameters
;
939 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
940 DECLARATOR and DEFAULT_ARGUMENT. */
942 cp_parameter_declarator
*
943 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
944 cp_declarator
*declarator
,
945 tree default_argument
)
947 cp_parameter_declarator
*parameter
;
949 parameter
= ((cp_parameter_declarator
*)
950 alloc_declarator (sizeof (cp_parameter_declarator
)));
951 parameter
->next
= NULL
;
953 parameter
->decl_specifiers
= *decl_specifiers
;
955 clear_decl_specs (¶meter
->decl_specifiers
);
956 parameter
->declarator
= declarator
;
957 parameter
->default_argument
= default_argument
;
958 parameter
->ellipsis_p
= false;
968 A cp_parser parses the token stream as specified by the C++
969 grammar. Its job is purely parsing, not semantic analysis. For
970 example, the parser breaks the token stream into declarators,
971 expressions, statements, and other similar syntactic constructs.
972 It does not check that the types of the expressions on either side
973 of an assignment-statement are compatible, or that a function is
974 not declared with a parameter of type `void'.
976 The parser invokes routines elsewhere in the compiler to perform
977 semantic analysis and to build up the abstract syntax tree for the
980 The parser (and the template instantiation code, which is, in a
981 way, a close relative of parsing) are the only parts of the
982 compiler that should be calling push_scope and pop_scope, or
983 related functions. The parser (and template instantiation code)
984 keeps track of what scope is presently active; everything else
985 should simply honor that. (The code that generates static
986 initializers may also need to set the scope, in order to check
987 access control correctly when emitting the initializers.)
992 The parser is of the standard recursive-descent variety. Upcoming
993 tokens in the token stream are examined in order to determine which
994 production to use when parsing a non-terminal. Some C++ constructs
995 require arbitrary look ahead to disambiguate. For example, it is
996 impossible, in the general case, to tell whether a statement is an
997 expression or declaration without scanning the entire statement.
998 Therefore, the parser is capable of "parsing tentatively." When the
999 parser is not sure what construct comes next, it enters this mode.
1000 Then, while we attempt to parse the construct, the parser queues up
1001 error messages, rather than issuing them immediately, and saves the
1002 tokens it consumes. If the construct is parsed successfully, the
1003 parser "commits", i.e., it issues any queued error messages and
1004 the tokens that were being preserved are permanently discarded.
1005 If, however, the construct is not parsed successfully, the parser
1006 rolls back its state completely so that it can resume parsing using
1007 a different alternative.
1012 The performance of the parser could probably be improved substantially.
1013 We could often eliminate the need to parse tentatively by looking ahead
1014 a little bit. In some places, this approach might not entirely eliminate
1015 the need to parse tentatively, but it might still speed up the average
1018 /* Flags that are passed to some parsing functions. These values can
1019 be bitwise-ored together. */
1021 typedef enum cp_parser_flags
1024 CP_PARSER_FLAGS_NONE
= 0x0,
1025 /* The construct is optional. If it is not present, then no error
1026 should be issued. */
1027 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1028 /* When parsing a type-specifier, do not allow user-defined types. */
1029 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2
1032 /* The different kinds of declarators we want to parse. */
1034 typedef enum cp_parser_declarator_kind
1036 /* We want an abstract declarator. */
1037 CP_PARSER_DECLARATOR_ABSTRACT
,
1038 /* We want a named declarator. */
1039 CP_PARSER_DECLARATOR_NAMED
,
1040 /* We don't mind, but the name must be an unqualified-id. */
1041 CP_PARSER_DECLARATOR_EITHER
1042 } cp_parser_declarator_kind
;
1044 /* The precedence values used to parse binary expressions. The minimum value
1045 of PREC must be 1, because zero is reserved to quickly discriminate
1046 binary operators from other tokens. */
1051 PREC_LOGICAL_OR_EXPRESSION
,
1052 PREC_LOGICAL_AND_EXPRESSION
,
1053 PREC_INCLUSIVE_OR_EXPRESSION
,
1054 PREC_EXCLUSIVE_OR_EXPRESSION
,
1055 PREC_AND_EXPRESSION
,
1056 PREC_EQUALITY_EXPRESSION
,
1057 PREC_RELATIONAL_EXPRESSION
,
1058 PREC_SHIFT_EXPRESSION
,
1059 PREC_ADDITIVE_EXPRESSION
,
1060 PREC_MULTIPLICATIVE_EXPRESSION
,
1062 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1065 /* A mapping from a token type to a corresponding tree node type, with a
1066 precedence value. */
1068 typedef struct cp_parser_binary_operations_map_node
1070 /* The token type. */
1071 enum cpp_ttype token_type
;
1072 /* The corresponding tree code. */
1073 enum tree_code tree_type
;
1074 /* The precedence of this operator. */
1075 enum cp_parser_prec prec
;
1076 } cp_parser_binary_operations_map_node
;
1078 /* The status of a tentative parse. */
1080 typedef enum cp_parser_status_kind
1082 /* No errors have occurred. */
1083 CP_PARSER_STATUS_KIND_NO_ERROR
,
1084 /* An error has occurred. */
1085 CP_PARSER_STATUS_KIND_ERROR
,
1086 /* We are committed to this tentative parse, whether or not an error
1088 CP_PARSER_STATUS_KIND_COMMITTED
1089 } cp_parser_status_kind
;
1091 typedef struct cp_parser_expression_stack_entry
1094 enum tree_code tree_type
;
1096 } cp_parser_expression_stack_entry
;
1098 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1099 entries because precedence levels on the stack are monotonically
1101 typedef struct cp_parser_expression_stack_entry
1102 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1104 /* Context that is saved and restored when parsing tentatively. */
1105 typedef struct cp_parser_context
GTY (())
1107 /* If this is a tentative parsing context, the status of the
1109 enum cp_parser_status_kind status
;
1110 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1111 that are looked up in this context must be looked up both in the
1112 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1113 the context of the containing expression. */
1116 /* The next parsing context in the stack. */
1117 struct cp_parser_context
*next
;
1118 } cp_parser_context
;
1122 /* Constructors and destructors. */
1124 static cp_parser_context
*cp_parser_context_new
1125 (cp_parser_context
*);
1127 /* Class variables. */
1129 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1131 /* The operator-precedence table used by cp_parser_binary_expression.
1132 Transformed into an associative array (binops_by_token) by
1135 static const cp_parser_binary_operations_map_node binops
[] = {
1136 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1137 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1139 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1140 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1141 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1143 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1144 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1146 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1147 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1149 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1150 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1151 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1152 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1153 { CPP_MIN
, MIN_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1154 { CPP_MAX
, MAX_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1156 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1157 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1159 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1161 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1163 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1165 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1167 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1170 /* The same as binops, but initialized by cp_parser_new so that
1171 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1173 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1175 /* Constructors and destructors. */
1177 /* Construct a new context. The context below this one on the stack
1178 is given by NEXT. */
1180 static cp_parser_context
*
1181 cp_parser_context_new (cp_parser_context
* next
)
1183 cp_parser_context
*context
;
1185 /* Allocate the storage. */
1186 if (cp_parser_context_free_list
!= NULL
)
1188 /* Pull the first entry from the free list. */
1189 context
= cp_parser_context_free_list
;
1190 cp_parser_context_free_list
= context
->next
;
1191 memset (context
, 0, sizeof (*context
));
1194 context
= GGC_CNEW (cp_parser_context
);
1196 /* No errors have occurred yet in this context. */
1197 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1198 /* If this is not the bottomost context, copy information that we
1199 need from the previous context. */
1202 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1203 expression, then we are parsing one in this context, too. */
1204 context
->object_type
= next
->object_type
;
1205 /* Thread the stack. */
1206 context
->next
= next
;
1212 /* The cp_parser structure represents the C++ parser. */
1214 typedef struct cp_parser
GTY(())
1216 /* The lexer from which we are obtaining tokens. */
1219 /* The scope in which names should be looked up. If NULL_TREE, then
1220 we look up names in the scope that is currently open in the
1221 source program. If non-NULL, this is either a TYPE or
1222 NAMESPACE_DECL for the scope in which we should look. It can
1223 also be ERROR_MARK, when we've parsed a bogus scope.
1225 This value is not cleared automatically after a name is looked
1226 up, so we must be careful to clear it before starting a new look
1227 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1228 will look up `Z' in the scope of `X', rather than the current
1229 scope.) Unfortunately, it is difficult to tell when name lookup
1230 is complete, because we sometimes peek at a token, look it up,
1231 and then decide not to consume it. */
1234 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1235 last lookup took place. OBJECT_SCOPE is used if an expression
1236 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1237 respectively. QUALIFYING_SCOPE is used for an expression of the
1238 form "X::Y"; it refers to X. */
1240 tree qualifying_scope
;
1242 /* A stack of parsing contexts. All but the bottom entry on the
1243 stack will be tentative contexts.
1245 We parse tentatively in order to determine which construct is in
1246 use in some situations. For example, in order to determine
1247 whether a statement is an expression-statement or a
1248 declaration-statement we parse it tentatively as a
1249 declaration-statement. If that fails, we then reparse the same
1250 token stream as an expression-statement. */
1251 cp_parser_context
*context
;
1253 /* True if we are parsing GNU C++. If this flag is not set, then
1254 GNU extensions are not recognized. */
1255 bool allow_gnu_extensions_p
;
1257 /* TRUE if the `>' token should be interpreted as the greater-than
1258 operator. FALSE if it is the end of a template-id or
1259 template-parameter-list. */
1260 bool greater_than_is_operator_p
;
1262 /* TRUE if default arguments are allowed within a parameter list
1263 that starts at this point. FALSE if only a gnu extension makes
1264 them permissible. */
1265 bool default_arg_ok_p
;
1267 /* TRUE if we are parsing an integral constant-expression. See
1268 [expr.const] for a precise definition. */
1269 bool integral_constant_expression_p
;
1271 /* TRUE if we are parsing an integral constant-expression -- but a
1272 non-constant expression should be permitted as well. This flag
1273 is used when parsing an array bound so that GNU variable-length
1274 arrays are tolerated. */
1275 bool allow_non_integral_constant_expression_p
;
1277 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1278 been seen that makes the expression non-constant. */
1279 bool non_integral_constant_expression_p
;
1281 /* TRUE if local variable names and `this' are forbidden in the
1283 bool local_variables_forbidden_p
;
1285 /* TRUE if the declaration we are parsing is part of a
1286 linkage-specification of the form `extern string-literal
1288 bool in_unbraced_linkage_specification_p
;
1290 /* TRUE if we are presently parsing a declarator, after the
1291 direct-declarator. */
1292 bool in_declarator_p
;
1294 /* TRUE if we are presently parsing a template-argument-list. */
1295 bool in_template_argument_list_p
;
1297 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1298 to IN_OMP_BLOCK if parsing OpenMP structured block and
1299 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1300 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1301 iteration-statement, OpenMP block or loop within that switch. */
1302 #define IN_SWITCH_STMT 1
1303 #define IN_ITERATION_STMT 2
1304 #define IN_OMP_BLOCK 4
1305 #define IN_OMP_FOR 8
1306 unsigned char in_statement
;
1308 /* TRUE if we are presently parsing the body of a switch statement.
1309 Note that this doesn't quite overlap with in_statement above.
1310 The difference relates to giving the right sets of error messages:
1311 "case not in switch" vs "break statement used with OpenMP...". */
1312 bool in_switch_statement_p
;
1314 /* TRUE if we are parsing a type-id in an expression context. In
1315 such a situation, both "type (expr)" and "type (type)" are valid
1317 bool in_type_id_in_expr_p
;
1319 /* TRUE if we are currently in a header file where declarations are
1320 implicitly extern "C". */
1321 bool implicit_extern_c
;
1323 /* TRUE if strings in expressions should be translated to the execution
1325 bool translate_strings_p
;
1327 /* If non-NULL, then we are parsing a construct where new type
1328 definitions are not permitted. The string stored here will be
1329 issued as an error message if a type is defined. */
1330 const char *type_definition_forbidden_message
;
1332 /* A list of lists. The outer list is a stack, used for member
1333 functions of local classes. At each level there are two sub-list,
1334 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1335 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1336 TREE_VALUE's. The functions are chained in reverse declaration
1339 The TREE_PURPOSE sublist contains those functions with default
1340 arguments that need post processing, and the TREE_VALUE sublist
1341 contains those functions with definitions that need post
1344 These lists can only be processed once the outermost class being
1345 defined is complete. */
1346 tree unparsed_functions_queues
;
1348 /* The number of classes whose definitions are currently in
1350 unsigned num_classes_being_defined
;
1352 /* The number of template parameter lists that apply directly to the
1353 current declaration. */
1354 unsigned num_template_parameter_lists
;
1359 /* Constructors and destructors. */
1361 static cp_parser
*cp_parser_new
1364 /* Routines to parse various constructs.
1366 Those that return `tree' will return the error_mark_node (rather
1367 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1368 Sometimes, they will return an ordinary node if error-recovery was
1369 attempted, even though a parse error occurred. So, to check
1370 whether or not a parse error occurred, you should always use
1371 cp_parser_error_occurred. If the construct is optional (indicated
1372 either by an `_opt' in the name of the function that does the
1373 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1374 the construct is not present. */
1376 /* Lexical conventions [gram.lex] */
1378 static tree cp_parser_identifier
1380 static tree cp_parser_string_literal
1381 (cp_parser
*, bool, bool);
1383 /* Basic concepts [gram.basic] */
1385 static bool cp_parser_translation_unit
1388 /* Expressions [gram.expr] */
1390 static tree cp_parser_primary_expression
1391 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1392 static tree cp_parser_id_expression
1393 (cp_parser
*, bool, bool, bool *, bool, bool);
1394 static tree cp_parser_unqualified_id
1395 (cp_parser
*, bool, bool, bool, bool);
1396 static tree cp_parser_nested_name_specifier_opt
1397 (cp_parser
*, bool, bool, bool, bool);
1398 static tree cp_parser_nested_name_specifier
1399 (cp_parser
*, bool, bool, bool, bool);
1400 static tree cp_parser_class_or_namespace_name
1401 (cp_parser
*, bool, bool, bool, bool, bool);
1402 static tree cp_parser_postfix_expression
1403 (cp_parser
*, bool, bool);
1404 static tree cp_parser_postfix_open_square_expression
1405 (cp_parser
*, tree
, bool);
1406 static tree cp_parser_postfix_dot_deref_expression
1407 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*);
1408 static tree cp_parser_parenthesized_expression_list
1409 (cp_parser
*, bool, bool, bool *);
1410 static void cp_parser_pseudo_destructor_name
1411 (cp_parser
*, tree
*, tree
*);
1412 static tree cp_parser_unary_expression
1413 (cp_parser
*, bool, bool);
1414 static enum tree_code cp_parser_unary_operator
1416 static tree cp_parser_new_expression
1418 static tree cp_parser_new_placement
1420 static tree cp_parser_new_type_id
1421 (cp_parser
*, tree
*);
1422 static cp_declarator
*cp_parser_new_declarator_opt
1424 static cp_declarator
*cp_parser_direct_new_declarator
1426 static tree cp_parser_new_initializer
1428 static tree cp_parser_delete_expression
1430 static tree cp_parser_cast_expression
1431 (cp_parser
*, bool, bool);
1432 static tree cp_parser_binary_expression
1433 (cp_parser
*, bool);
1434 static tree cp_parser_question_colon_clause
1435 (cp_parser
*, tree
);
1436 static tree cp_parser_assignment_expression
1437 (cp_parser
*, bool);
1438 static enum tree_code cp_parser_assignment_operator_opt
1440 static tree cp_parser_expression
1441 (cp_parser
*, bool);
1442 static tree cp_parser_constant_expression
1443 (cp_parser
*, bool, bool *);
1444 static tree cp_parser_builtin_offsetof
1447 /* Statements [gram.stmt.stmt] */
1449 static void cp_parser_statement
1450 (cp_parser
*, tree
, bool);
1451 static tree cp_parser_labeled_statement
1452 (cp_parser
*, tree
, bool);
1453 static tree cp_parser_expression_statement
1454 (cp_parser
*, tree
);
1455 static tree cp_parser_compound_statement
1456 (cp_parser
*, tree
, bool);
1457 static void cp_parser_statement_seq_opt
1458 (cp_parser
*, tree
);
1459 static tree cp_parser_selection_statement
1461 static tree cp_parser_condition
1463 static tree cp_parser_iteration_statement
1465 static void cp_parser_for_init_statement
1467 static tree cp_parser_jump_statement
1469 static void cp_parser_declaration_statement
1472 static tree cp_parser_implicitly_scoped_statement
1474 static void cp_parser_already_scoped_statement
1477 /* Declarations [gram.dcl.dcl] */
1479 static void cp_parser_declaration_seq_opt
1481 static void cp_parser_declaration
1483 static void cp_parser_block_declaration
1484 (cp_parser
*, bool);
1485 static void cp_parser_simple_declaration
1486 (cp_parser
*, bool);
1487 static void cp_parser_decl_specifier_seq
1488 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1489 static tree cp_parser_storage_class_specifier_opt
1491 static tree cp_parser_function_specifier_opt
1492 (cp_parser
*, cp_decl_specifier_seq
*);
1493 static tree cp_parser_type_specifier
1494 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1496 static tree cp_parser_simple_type_specifier
1497 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1498 static tree cp_parser_type_name
1500 static tree cp_parser_elaborated_type_specifier
1501 (cp_parser
*, bool, bool);
1502 static tree cp_parser_enum_specifier
1504 static void cp_parser_enumerator_list
1505 (cp_parser
*, tree
);
1506 static void cp_parser_enumerator_definition
1507 (cp_parser
*, tree
);
1508 static tree cp_parser_namespace_name
1510 static void cp_parser_namespace_definition
1512 static void cp_parser_namespace_body
1514 static tree cp_parser_qualified_namespace_specifier
1516 static void cp_parser_namespace_alias_definition
1518 static void cp_parser_using_declaration
1520 static void cp_parser_using_directive
1522 static void cp_parser_asm_definition
1524 static void cp_parser_linkage_specification
1527 /* Declarators [gram.dcl.decl] */
1529 static tree cp_parser_init_declarator
1530 (cp_parser
*, cp_decl_specifier_seq
*, tree
, bool, bool, int, bool *);
1531 static cp_declarator
*cp_parser_declarator
1532 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1533 static cp_declarator
*cp_parser_direct_declarator
1534 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1535 static enum tree_code cp_parser_ptr_operator
1536 (cp_parser
*, tree
*, cp_cv_quals
*);
1537 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1539 static tree cp_parser_declarator_id
1540 (cp_parser
*, bool);
1541 static tree cp_parser_type_id
1543 static void cp_parser_type_specifier_seq
1544 (cp_parser
*, bool, cp_decl_specifier_seq
*);
1545 static cp_parameter_declarator
*cp_parser_parameter_declaration_clause
1547 static cp_parameter_declarator
*cp_parser_parameter_declaration_list
1548 (cp_parser
*, bool *);
1549 static cp_parameter_declarator
*cp_parser_parameter_declaration
1550 (cp_parser
*, bool, bool *);
1551 static void cp_parser_function_body
1553 static tree cp_parser_initializer
1554 (cp_parser
*, bool *, bool *);
1555 static tree cp_parser_initializer_clause
1556 (cp_parser
*, bool *);
1557 static VEC(constructor_elt
,gc
) *cp_parser_initializer_list
1558 (cp_parser
*, bool *);
1560 static bool cp_parser_ctor_initializer_opt_and_function_body
1563 /* Classes [gram.class] */
1565 static tree cp_parser_class_name
1566 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
1567 static tree cp_parser_class_specifier
1569 static tree cp_parser_class_head
1570 (cp_parser
*, bool *, tree
*);
1571 static enum tag_types cp_parser_class_key
1573 static void cp_parser_member_specification_opt
1575 static void cp_parser_member_declaration
1577 static tree cp_parser_pure_specifier
1579 static tree cp_parser_constant_initializer
1582 /* Derived classes [gram.class.derived] */
1584 static tree cp_parser_base_clause
1586 static tree cp_parser_base_specifier
1589 /* Special member functions [gram.special] */
1591 static tree cp_parser_conversion_function_id
1593 static tree cp_parser_conversion_type_id
1595 static cp_declarator
*cp_parser_conversion_declarator_opt
1597 static bool cp_parser_ctor_initializer_opt
1599 static void cp_parser_mem_initializer_list
1601 static tree cp_parser_mem_initializer
1603 static tree cp_parser_mem_initializer_id
1606 /* Overloading [gram.over] */
1608 static tree cp_parser_operator_function_id
1610 static tree cp_parser_operator
1613 /* Templates [gram.temp] */
1615 static void cp_parser_template_declaration
1616 (cp_parser
*, bool);
1617 static tree cp_parser_template_parameter_list
1619 static tree cp_parser_template_parameter
1620 (cp_parser
*, bool *);
1621 static tree cp_parser_type_parameter
1623 static tree cp_parser_template_id
1624 (cp_parser
*, bool, bool, bool);
1625 static tree cp_parser_template_name
1626 (cp_parser
*, bool, bool, bool, bool *);
1627 static tree cp_parser_template_argument_list
1629 static tree cp_parser_template_argument
1631 static void cp_parser_explicit_instantiation
1633 static void cp_parser_explicit_specialization
1636 /* Exception handling [gram.exception] */
1638 static tree cp_parser_try_block
1640 static bool cp_parser_function_try_block
1642 static void cp_parser_handler_seq
1644 static void cp_parser_handler
1646 static tree cp_parser_exception_declaration
1648 static tree cp_parser_throw_expression
1650 static tree cp_parser_exception_specification_opt
1652 static tree cp_parser_type_id_list
1655 /* GNU Extensions */
1657 static tree cp_parser_asm_specification_opt
1659 static tree cp_parser_asm_operand_list
1661 static tree cp_parser_asm_clobber_list
1663 static tree cp_parser_attributes_opt
1665 static tree cp_parser_attribute_list
1667 static bool cp_parser_extension_opt
1668 (cp_parser
*, int *);
1669 static void cp_parser_label_declaration
1672 enum pragma_context
{ pragma_external
, pragma_stmt
, pragma_compound
};
1673 static bool cp_parser_pragma
1674 (cp_parser
*, enum pragma_context
);
1676 /* Objective-C++ Productions */
1678 static tree cp_parser_objc_message_receiver
1680 static tree cp_parser_objc_message_args
1682 static tree cp_parser_objc_message_expression
1684 static tree cp_parser_objc_encode_expression
1686 static tree cp_parser_objc_defs_expression
1688 static tree cp_parser_objc_protocol_expression
1690 static tree cp_parser_objc_selector_expression
1692 static tree cp_parser_objc_expression
1694 static bool cp_parser_objc_selector_p
1696 static tree cp_parser_objc_selector
1698 static tree cp_parser_objc_protocol_refs_opt
1700 static void cp_parser_objc_declaration
1702 static tree cp_parser_objc_statement
1705 /* Utility Routines */
1707 static tree cp_parser_lookup_name
1708 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*);
1709 static tree cp_parser_lookup_name_simple
1710 (cp_parser
*, tree
);
1711 static tree cp_parser_maybe_treat_template_as_class
1713 static bool cp_parser_check_declarator_template_parameters
1714 (cp_parser
*, cp_declarator
*);
1715 static bool cp_parser_check_template_parameters
1716 (cp_parser
*, unsigned);
1717 static tree cp_parser_simple_cast_expression
1719 static tree cp_parser_global_scope_opt
1720 (cp_parser
*, bool);
1721 static bool cp_parser_constructor_declarator_p
1722 (cp_parser
*, bool);
1723 static tree cp_parser_function_definition_from_specifiers_and_declarator
1724 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
1725 static tree cp_parser_function_definition_after_declarator
1726 (cp_parser
*, bool);
1727 static void cp_parser_template_declaration_after_export
1728 (cp_parser
*, bool);
1729 static void cp_parser_perform_template_parameter_access_checks
1731 static tree cp_parser_single_declaration
1732 (cp_parser
*, tree
, bool, bool *);
1733 static tree cp_parser_functional_cast
1734 (cp_parser
*, tree
);
1735 static tree cp_parser_save_member_function_body
1736 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1737 static tree cp_parser_enclosed_template_argument_list
1739 static void cp_parser_save_default_args
1740 (cp_parser
*, tree
);
1741 static void cp_parser_late_parsing_for_member
1742 (cp_parser
*, tree
);
1743 static void cp_parser_late_parsing_default_args
1744 (cp_parser
*, tree
);
1745 static tree cp_parser_sizeof_operand
1746 (cp_parser
*, enum rid
);
1747 static bool cp_parser_declares_only_class_p
1749 static void cp_parser_set_storage_class
1750 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
);
1751 static void cp_parser_set_decl_spec_type
1752 (cp_decl_specifier_seq
*, tree
, bool);
1753 static bool cp_parser_friend_p
1754 (const cp_decl_specifier_seq
*);
1755 static cp_token
*cp_parser_require
1756 (cp_parser
*, enum cpp_ttype
, const char *);
1757 static cp_token
*cp_parser_require_keyword
1758 (cp_parser
*, enum rid
, const char *);
1759 static bool cp_parser_token_starts_function_definition_p
1761 static bool cp_parser_next_token_starts_class_definition_p
1763 static bool cp_parser_next_token_ends_template_argument_p
1765 static bool cp_parser_nth_token_starts_template_argument_list_p
1766 (cp_parser
*, size_t);
1767 static enum tag_types cp_parser_token_is_class_key
1769 static void cp_parser_check_class_key
1770 (enum tag_types
, tree type
);
1771 static void cp_parser_check_access_in_redeclaration
1773 static bool cp_parser_optional_template_keyword
1775 static void cp_parser_pre_parsed_nested_name_specifier
1777 static void cp_parser_cache_group
1778 (cp_parser
*, enum cpp_ttype
, unsigned);
1779 static void cp_parser_parse_tentatively
1781 static void cp_parser_commit_to_tentative_parse
1783 static void cp_parser_abort_tentative_parse
1785 static bool cp_parser_parse_definitely
1787 static inline bool cp_parser_parsing_tentatively
1789 static bool cp_parser_uncommitted_to_tentative_parse_p
1791 static void cp_parser_error
1792 (cp_parser
*, const char *);
1793 static void cp_parser_name_lookup_error
1794 (cp_parser
*, tree
, tree
, const char *);
1795 static bool cp_parser_simulate_error
1797 static void cp_parser_check_type_definition
1799 static void cp_parser_check_for_definition_in_return_type
1800 (cp_declarator
*, tree
);
1801 static void cp_parser_check_for_invalid_template_id
1802 (cp_parser
*, tree
);
1803 static bool cp_parser_non_integral_constant_expression
1804 (cp_parser
*, const char *);
1805 static void cp_parser_diagnose_invalid_type_name
1806 (cp_parser
*, tree
, tree
);
1807 static bool cp_parser_parse_and_diagnose_invalid_type_name
1809 static int cp_parser_skip_to_closing_parenthesis
1810 (cp_parser
*, bool, bool, bool);
1811 static void cp_parser_skip_to_end_of_statement
1813 static void cp_parser_consume_semicolon_at_end_of_statement
1815 static void cp_parser_skip_to_end_of_block_or_statement
1817 static void cp_parser_skip_to_closing_brace
1819 static void cp_parser_skip_until_found
1820 (cp_parser
*, enum cpp_ttype
, const char *);
1821 static void cp_parser_skip_to_pragma_eol
1822 (cp_parser
*, cp_token
*);
1823 static bool cp_parser_error_occurred
1825 static bool cp_parser_allow_gnu_extensions_p
1827 static bool cp_parser_is_string_literal
1829 static bool cp_parser_is_keyword
1830 (cp_token
*, enum rid
);
1831 static tree cp_parser_make_typename_type
1832 (cp_parser
*, tree
, tree
);
1834 /* Returns nonzero if we are parsing tentatively. */
1837 cp_parser_parsing_tentatively (cp_parser
* parser
)
1839 return parser
->context
->next
!= NULL
;
1842 /* Returns nonzero if TOKEN is a string literal. */
1845 cp_parser_is_string_literal (cp_token
* token
)
1847 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
1850 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1853 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
1855 return token
->keyword
== keyword
;
1858 /* A minimum or maximum operator has been seen. As these are
1859 deprecated, issue a warning. */
1862 cp_parser_warn_min_max (void)
1864 if (warn_deprecated
&& !in_system_header
)
1865 warning (OPT_Wdeprecated
, "minimum/maximum operators are deprecated");
1868 /* If not parsing tentatively, issue a diagnostic of the form
1869 FILE:LINE: MESSAGE before TOKEN
1870 where TOKEN is the next token in the input stream. MESSAGE
1871 (specified by the caller) is usually of the form "expected
1875 cp_parser_error (cp_parser
* parser
, const char* message
)
1877 if (!cp_parser_simulate_error (parser
))
1879 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
1880 /* This diagnostic makes more sense if it is tagged to the line
1881 of the token we just peeked at. */
1882 cp_lexer_set_source_position_from_token (token
);
1884 if (token
->type
== CPP_PRAGMA
)
1886 error ("%<#pragma%> is not allowed here");
1887 cp_parser_skip_to_pragma_eol (parser
, token
);
1891 c_parse_error (message
,
1892 /* Because c_parser_error does not understand
1893 CPP_KEYWORD, keywords are treated like
1895 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
1900 /* Issue an error about name-lookup failing. NAME is the
1901 IDENTIFIER_NODE DECL is the result of
1902 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1903 the thing that we hoped to find. */
1906 cp_parser_name_lookup_error (cp_parser
* parser
,
1909 const char* desired
)
1911 /* If name lookup completely failed, tell the user that NAME was not
1913 if (decl
== error_mark_node
)
1915 if (parser
->scope
&& parser
->scope
!= global_namespace
)
1916 error ("%<%D::%D%> has not been declared",
1917 parser
->scope
, name
);
1918 else if (parser
->scope
== global_namespace
)
1919 error ("%<::%D%> has not been declared", name
);
1920 else if (parser
->object_scope
1921 && !CLASS_TYPE_P (parser
->object_scope
))
1922 error ("request for member %qD in non-class type %qT",
1923 name
, parser
->object_scope
);
1924 else if (parser
->object_scope
)
1925 error ("%<%T::%D%> has not been declared",
1926 parser
->object_scope
, name
);
1928 error ("%qD has not been declared", name
);
1930 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
1931 error ("%<%D::%D%> %s", parser
->scope
, name
, desired
);
1932 else if (parser
->scope
== global_namespace
)
1933 error ("%<::%D%> %s", name
, desired
);
1935 error ("%qD %s", name
, desired
);
1938 /* If we are parsing tentatively, remember that an error has occurred
1939 during this tentative parse. Returns true if the error was
1940 simulated; false if a message should be issued by the caller. */
1943 cp_parser_simulate_error (cp_parser
* parser
)
1945 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
1947 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
1953 /* Check for repeated decl-specifiers. */
1956 cp_parser_check_decl_spec (cp_decl_specifier_seq
*decl_specs
)
1960 for (ds
= ds_first
; ds
!= ds_last
; ++ds
)
1962 unsigned count
= decl_specs
->specs
[(int)ds
];
1965 /* The "long" specifier is a special case because of "long long". */
1969 error ("%<long long long%> is too long for GCC");
1970 else if (pedantic
&& !in_system_header
&& warn_long_long
)
1971 pedwarn ("ISO C++ does not support %<long long%>");
1975 static const char *const decl_spec_names
[] = {
1991 error ("duplicate %qs", decl_spec_names
[(int)ds
]);
1996 /* This function is called when a type is defined. If type
1997 definitions are forbidden at this point, an error message is
2001 cp_parser_check_type_definition (cp_parser
* parser
)
2003 /* If types are forbidden here, issue a message. */
2004 if (parser
->type_definition_forbidden_message
)
2005 /* Use `%s' to print the string in case there are any escape
2006 characters in the message. */
2007 error ("%s", parser
->type_definition_forbidden_message
);
2010 /* This function is called when the DECLARATOR is processed. The TYPE
2011 was a type defined in the decl-specifiers. If it is invalid to
2012 define a type in the decl-specifiers for DECLARATOR, an error is
2016 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2019 /* [dcl.fct] forbids type definitions in return types.
2020 Unfortunately, it's not easy to know whether or not we are
2021 processing a return type until after the fact. */
2023 && (declarator
->kind
== cdk_pointer
2024 || declarator
->kind
== cdk_reference
2025 || declarator
->kind
== cdk_ptrmem
))
2026 declarator
= declarator
->declarator
;
2028 && declarator
->kind
== cdk_function
)
2030 error ("new types may not be defined in a return type");
2031 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2036 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2037 "<" in any valid C++ program. If the next token is indeed "<",
2038 issue a message warning the user about what appears to be an
2039 invalid attempt to form a template-id. */
2042 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2045 cp_token_position start
= 0;
2047 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2050 error ("%qT is not a template", type
);
2051 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
2052 error ("%qE is not a template", type
);
2054 error ("invalid template-id");
2055 /* Remember the location of the invalid "<". */
2056 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2057 start
= cp_lexer_token_position (parser
->lexer
, true);
2058 /* Consume the "<". */
2059 cp_lexer_consume_token (parser
->lexer
);
2060 /* Parse the template arguments. */
2061 cp_parser_enclosed_template_argument_list (parser
);
2062 /* Permanently remove the invalid template arguments so that
2063 this error message is not issued again. */
2065 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2069 /* If parsing an integral constant-expression, issue an error message
2070 about the fact that THING appeared and return true. Otherwise,
2071 return false. In either case, set
2072 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2075 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2078 parser
->non_integral_constant_expression_p
= true;
2079 if (parser
->integral_constant_expression_p
)
2081 if (!parser
->allow_non_integral_constant_expression_p
)
2083 error ("%s cannot appear in a constant-expression", thing
);
2090 /* Emit a diagnostic for an invalid type name. SCOPE is the
2091 qualifying scope (or NULL, if none) for ID. This function commits
2092 to the current active tentative parse, if any. (Otherwise, the
2093 problematic construct might be encountered again later, resulting
2094 in duplicate error messages.) */
2097 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree scope
, tree id
)
2099 tree decl
, old_scope
;
2100 /* Try to lookup the identifier. */
2101 old_scope
= parser
->scope
;
2102 parser
->scope
= scope
;
2103 decl
= cp_parser_lookup_name_simple (parser
, id
);
2104 parser
->scope
= old_scope
;
2105 /* If the lookup found a template-name, it means that the user forgot
2106 to specify an argument list. Emit a useful error message. */
2107 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2108 error ("invalid use of template-name %qE without an argument list",
2110 else if (!parser
->scope
)
2112 /* Issue an error message. */
2113 error ("%qE does not name a type", id
);
2114 /* If we're in a template class, it's possible that the user was
2115 referring to a type from a base class. For example:
2117 template <typename T> struct A { typedef T X; };
2118 template <typename T> struct B : public A<T> { X x; };
2120 The user should have said "typename A<T>::X". */
2121 if (processing_template_decl
&& current_class_type
2122 && TYPE_BINFO (current_class_type
))
2126 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2130 tree base_type
= BINFO_TYPE (b
);
2131 if (CLASS_TYPE_P (base_type
)
2132 && dependent_type_p (base_type
))
2135 /* Go from a particular instantiation of the
2136 template (which will have an empty TYPE_FIELDs),
2137 to the main version. */
2138 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2139 for (field
= TYPE_FIELDS (base_type
);
2141 field
= TREE_CHAIN (field
))
2142 if (TREE_CODE (field
) == TYPE_DECL
2143 && DECL_NAME (field
) == id
)
2145 inform ("(perhaps %<typename %T::%E%> was intended)",
2146 BINFO_TYPE (b
), id
);
2155 /* Here we diagnose qualified-ids where the scope is actually correct,
2156 but the identifier does not resolve to a valid type name. */
2157 else if (parser
->scope
!= error_mark_node
)
2159 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2160 error ("%qE in namespace %qE does not name a type",
2162 else if (TYPE_P (parser
->scope
))
2163 error ("%qE in class %qT does not name a type", id
, parser
->scope
);
2167 cp_parser_commit_to_tentative_parse (parser
);
2170 /* Check for a common situation where a type-name should be present,
2171 but is not, and issue a sensible error message. Returns true if an
2172 invalid type-name was detected.
2174 The situation handled by this function are variable declarations of the
2175 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2176 Usually, `ID' should name a type, but if we got here it means that it
2177 does not. We try to emit the best possible error message depending on
2178 how exactly the id-expression looks like. */
2181 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2185 cp_parser_parse_tentatively (parser
);
2186 id
= cp_parser_id_expression (parser
,
2187 /*template_keyword_p=*/false,
2188 /*check_dependency_p=*/true,
2189 /*template_p=*/NULL
,
2190 /*declarator_p=*/true,
2191 /*optional_p=*/false);
2192 /* After the id-expression, there should be a plain identifier,
2193 otherwise this is not a simple variable declaration. Also, if
2194 the scope is dependent, we cannot do much. */
2195 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2196 || (parser
->scope
&& TYPE_P (parser
->scope
)
2197 && dependent_type_p (parser
->scope
)))
2199 cp_parser_abort_tentative_parse (parser
);
2202 if (!cp_parser_parse_definitely (parser
)
2203 || TREE_CODE (id
) != IDENTIFIER_NODE
)
2206 /* Emit a diagnostic for the invalid type. */
2207 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
, id
);
2208 /* Skip to the end of the declaration; there's no point in
2209 trying to process it. */
2210 cp_parser_skip_to_end_of_block_or_statement (parser
);
2214 /* Consume tokens up to, and including, the next non-nested closing `)'.
2215 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2216 are doing error recovery. Returns -1 if OR_COMMA is true and we
2217 found an unnested comma. */
2220 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2225 unsigned paren_depth
= 0;
2226 unsigned brace_depth
= 0;
2228 if (recovering
&& !or_comma
2229 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2234 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
2236 switch (token
->type
)
2239 case CPP_PRAGMA_EOL
:
2240 /* If we've run out of tokens, then there is no closing `)'. */
2244 /* This matches the processing in skip_to_end_of_statement. */
2249 case CPP_OPEN_BRACE
:
2252 case CPP_CLOSE_BRACE
:
2258 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
)
2262 case CPP_OPEN_PAREN
:
2267 case CPP_CLOSE_PAREN
:
2268 if (!brace_depth
&& !paren_depth
--)
2271 cp_lexer_consume_token (parser
->lexer
);
2280 /* Consume the token. */
2281 cp_lexer_consume_token (parser
->lexer
);
2285 /* Consume tokens until we reach the end of the current statement.
2286 Normally, that will be just before consuming a `;'. However, if a
2287 non-nested `}' comes first, then we stop before consuming that. */
2290 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2292 unsigned nesting_depth
= 0;
2296 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2298 switch (token
->type
)
2301 case CPP_PRAGMA_EOL
:
2302 /* If we've run out of tokens, stop. */
2306 /* If the next token is a `;', we have reached the end of the
2312 case CPP_CLOSE_BRACE
:
2313 /* If this is a non-nested '}', stop before consuming it.
2314 That way, when confronted with something like:
2318 we stop before consuming the closing '}', even though we
2319 have not yet reached a `;'. */
2320 if (nesting_depth
== 0)
2323 /* If it is the closing '}' for a block that we have
2324 scanned, stop -- but only after consuming the token.
2330 we will stop after the body of the erroneously declared
2331 function, but before consuming the following `typedef'
2333 if (--nesting_depth
== 0)
2335 cp_lexer_consume_token (parser
->lexer
);
2339 case CPP_OPEN_BRACE
:
2347 /* Consume the token. */
2348 cp_lexer_consume_token (parser
->lexer
);
2352 /* This function is called at the end of a statement or declaration.
2353 If the next token is a semicolon, it is consumed; otherwise, error
2354 recovery is attempted. */
2357 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2359 /* Look for the trailing `;'. */
2360 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2362 /* If there is additional (erroneous) input, skip to the end of
2364 cp_parser_skip_to_end_of_statement (parser
);
2365 /* If the next token is now a `;', consume it. */
2366 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2367 cp_lexer_consume_token (parser
->lexer
);
2371 /* Skip tokens until we have consumed an entire block, or until we
2372 have consumed a non-nested `;'. */
2375 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2377 int nesting_depth
= 0;
2379 while (nesting_depth
>= 0)
2381 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2383 switch (token
->type
)
2386 case CPP_PRAGMA_EOL
:
2387 /* If we've run out of tokens, stop. */
2391 /* Stop if this is an unnested ';'. */
2396 case CPP_CLOSE_BRACE
:
2397 /* Stop if this is an unnested '}', or closes the outermost
2404 case CPP_OPEN_BRACE
:
2413 /* Consume the token. */
2414 cp_lexer_consume_token (parser
->lexer
);
2418 /* Skip tokens until a non-nested closing curly brace is the next
2422 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2424 unsigned nesting_depth
= 0;
2428 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2430 switch (token
->type
)
2433 case CPP_PRAGMA_EOL
:
2434 /* If we've run out of tokens, stop. */
2437 case CPP_CLOSE_BRACE
:
2438 /* If the next token is a non-nested `}', then we have reached
2439 the end of the current block. */
2440 if (nesting_depth
-- == 0)
2444 case CPP_OPEN_BRACE
:
2445 /* If it the next token is a `{', then we are entering a new
2446 block. Consume the entire block. */
2454 /* Consume the token. */
2455 cp_lexer_consume_token (parser
->lexer
);
2459 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2460 parameter is the PRAGMA token, allowing us to purge the entire pragma
2464 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
2468 parser
->lexer
->in_pragma
= false;
2471 token
= cp_lexer_consume_token (parser
->lexer
);
2472 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
2474 /* Ensure that the pragma is not parsed again. */
2475 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
2478 /* Require pragma end of line, resyncing with it as necessary. The
2479 arguments are as for cp_parser_skip_to_pragma_eol. */
2482 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
2484 parser
->lexer
->in_pragma
= false;
2485 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, "end of line"))
2486 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
2489 /* This is a simple wrapper around make_typename_type. When the id is
2490 an unresolved identifier node, we can provide a superior diagnostic
2491 using cp_parser_diagnose_invalid_type_name. */
2494 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
, tree id
)
2497 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
2499 result
= make_typename_type (scope
, id
, typename_type
,
2500 /*complain=*/tf_none
);
2501 if (result
== error_mark_node
)
2502 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
);
2505 return make_typename_type (scope
, id
, typename_type
, tf_error
);
2509 /* Create a new C++ parser. */
2512 cp_parser_new (void)
2518 /* cp_lexer_new_main is called before calling ggc_alloc because
2519 cp_lexer_new_main might load a PCH file. */
2520 lexer
= cp_lexer_new_main ();
2522 /* Initialize the binops_by_token so that we can get the tree
2523 directly from the token. */
2524 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
2525 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
2527 parser
= GGC_CNEW (cp_parser
);
2528 parser
->lexer
= lexer
;
2529 parser
->context
= cp_parser_context_new (NULL
);
2531 /* For now, we always accept GNU extensions. */
2532 parser
->allow_gnu_extensions_p
= 1;
2534 /* The `>' token is a greater-than operator, not the end of a
2536 parser
->greater_than_is_operator_p
= true;
2538 parser
->default_arg_ok_p
= true;
2540 /* We are not parsing a constant-expression. */
2541 parser
->integral_constant_expression_p
= false;
2542 parser
->allow_non_integral_constant_expression_p
= false;
2543 parser
->non_integral_constant_expression_p
= false;
2545 /* Local variable names are not forbidden. */
2546 parser
->local_variables_forbidden_p
= false;
2548 /* We are not processing an `extern "C"' declaration. */
2549 parser
->in_unbraced_linkage_specification_p
= false;
2551 /* We are not processing a declarator. */
2552 parser
->in_declarator_p
= false;
2554 /* We are not processing a template-argument-list. */
2555 parser
->in_template_argument_list_p
= false;
2557 /* We are not in an iteration statement. */
2558 parser
->in_statement
= 0;
2560 /* We are not in a switch statement. */
2561 parser
->in_switch_statement_p
= false;
2563 /* We are not parsing a type-id inside an expression. */
2564 parser
->in_type_id_in_expr_p
= false;
2566 /* Declarations aren't implicitly extern "C". */
2567 parser
->implicit_extern_c
= false;
2569 /* String literals should be translated to the execution character set. */
2570 parser
->translate_strings_p
= true;
2572 /* The unparsed function queue is empty. */
2573 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2575 /* There are no classes being defined. */
2576 parser
->num_classes_being_defined
= 0;
2578 /* No template parameters apply. */
2579 parser
->num_template_parameter_lists
= 0;
2584 /* Create a cp_lexer structure which will emit the tokens in CACHE
2585 and push it onto the parser's lexer stack. This is used for delayed
2586 parsing of in-class method bodies and default arguments, and should
2587 not be confused with tentative parsing. */
2589 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
2591 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
2592 lexer
->next
= parser
->lexer
;
2593 parser
->lexer
= lexer
;
2595 /* Move the current source position to that of the first token in the
2597 cp_lexer_set_source_position_from_token (lexer
->next_token
);
2600 /* Pop the top lexer off the parser stack. This is never used for the
2601 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2603 cp_parser_pop_lexer (cp_parser
*parser
)
2605 cp_lexer
*lexer
= parser
->lexer
;
2606 parser
->lexer
= lexer
->next
;
2607 cp_lexer_destroy (lexer
);
2609 /* Put the current source position back where it was before this
2610 lexer was pushed. */
2611 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
2614 /* Lexical conventions [gram.lex] */
2616 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2620 cp_parser_identifier (cp_parser
* parser
)
2624 /* Look for the identifier. */
2625 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2626 /* Return the value. */
2627 return token
? token
->value
: error_mark_node
;
2630 /* Parse a sequence of adjacent string constants. Returns a
2631 TREE_STRING representing the combined, nul-terminated string
2632 constant. If TRANSLATE is true, translate the string to the
2633 execution character set. If WIDE_OK is true, a wide string is
2636 C++98 [lex.string] says that if a narrow string literal token is
2637 adjacent to a wide string literal token, the behavior is undefined.
2638 However, C99 6.4.5p4 says that this results in a wide string literal.
2639 We follow C99 here, for consistency with the C front end.
2641 This code is largely lifted from lex_string() in c-lex.c.
2643 FUTURE: ObjC++ will need to handle @-strings here. */
2645 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
2650 struct obstack str_ob
;
2651 cpp_string str
, istr
, *strs
;
2654 tok
= cp_lexer_peek_token (parser
->lexer
);
2655 if (!cp_parser_is_string_literal (tok
))
2657 cp_parser_error (parser
, "expected string-literal");
2658 return error_mark_node
;
2661 /* Try to avoid the overhead of creating and destroying an obstack
2662 for the common case of just one string. */
2663 if (!cp_parser_is_string_literal
2664 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
2666 cp_lexer_consume_token (parser
->lexer
);
2668 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->value
);
2669 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2671 if (tok
->type
== CPP_WSTRING
)
2678 gcc_obstack_init (&str_ob
);
2683 cp_lexer_consume_token (parser
->lexer
);
2685 str
.text
= (unsigned char *)TREE_STRING_POINTER (tok
->value
);
2686 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2687 if (tok
->type
== CPP_WSTRING
)
2690 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
2692 tok
= cp_lexer_peek_token (parser
->lexer
);
2694 while (cp_parser_is_string_literal (tok
));
2696 strs
= (cpp_string
*) obstack_finish (&str_ob
);
2699 if (wide
&& !wide_ok
)
2701 cp_parser_error (parser
, "a wide string is invalid in this context");
2705 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
2706 (parse_in
, strs
, count
, &istr
, wide
))
2708 value
= build_string (istr
.len
, (char *)istr
.text
);
2709 free ((void *)istr
.text
);
2711 TREE_TYPE (value
) = wide
? wchar_array_type_node
: char_array_type_node
;
2712 value
= fix_string_type (value
);
2715 /* cpp_interpret_string has issued an error. */
2716 value
= error_mark_node
;
2719 obstack_free (&str_ob
, 0);
2725 /* Basic concepts [gram.basic] */
2727 /* Parse a translation-unit.
2730 declaration-seq [opt]
2732 Returns TRUE if all went well. */
2735 cp_parser_translation_unit (cp_parser
* parser
)
2737 /* The address of the first non-permanent object on the declarator
2739 static void *declarator_obstack_base
;
2743 /* Create the declarator obstack, if necessary. */
2744 if (!cp_error_declarator
)
2746 gcc_obstack_init (&declarator_obstack
);
2747 /* Create the error declarator. */
2748 cp_error_declarator
= make_declarator (cdk_error
);
2749 /* Create the empty parameter list. */
2750 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
2751 /* Remember where the base of the declarator obstack lies. */
2752 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
2755 cp_parser_declaration_seq_opt (parser
);
2757 /* If there are no tokens left then all went well. */
2758 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2760 /* Get rid of the token array; we don't need it any more. */
2761 cp_lexer_destroy (parser
->lexer
);
2762 parser
->lexer
= NULL
;
2764 /* This file might have been a context that's implicitly extern
2765 "C". If so, pop the lang context. (Only relevant for PCH.) */
2766 if (parser
->implicit_extern_c
)
2768 pop_lang_context ();
2769 parser
->implicit_extern_c
= false;
2773 finish_translation_unit ();
2779 cp_parser_error (parser
, "expected declaration");
2783 /* Make sure the declarator obstack was fully cleaned up. */
2784 gcc_assert (obstack_next_free (&declarator_obstack
)
2785 == declarator_obstack_base
);
2787 /* All went well. */
2791 /* Expressions [gram.expr] */
2793 /* Parse a primary-expression.
2804 ( compound-statement )
2805 __builtin_va_arg ( assignment-expression , type-id )
2806 __builtin_offsetof ( type-id , offsetof-expression )
2808 Objective-C++ Extension:
2816 ADDRESS_P is true iff this expression was immediately preceded by
2817 "&" and therefore might denote a pointer-to-member. CAST_P is true
2818 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2819 true iff this expression is a template argument.
2821 Returns a representation of the expression. Upon return, *IDK
2822 indicates what kind of id-expression (if any) was present. */
2825 cp_parser_primary_expression (cp_parser
*parser
,
2828 bool template_arg_p
,
2833 /* Assume the primary expression is not an id-expression. */
2834 *idk
= CP_ID_KIND_NONE
;
2836 /* Peek at the next token. */
2837 token
= cp_lexer_peek_token (parser
->lexer
);
2838 switch (token
->type
)
2849 token
= cp_lexer_consume_token (parser
->lexer
);
2850 /* Floating-point literals are only allowed in an integral
2851 constant expression if they are cast to an integral or
2852 enumeration type. */
2853 if (TREE_CODE (token
->value
) == REAL_CST
2854 && parser
->integral_constant_expression_p
2857 /* CAST_P will be set even in invalid code like "int(2.7 +
2858 ...)". Therefore, we have to check that the next token
2859 is sure to end the cast. */
2862 cp_token
*next_token
;
2864 next_token
= cp_lexer_peek_token (parser
->lexer
);
2865 if (/* The comma at the end of an
2866 enumerator-definition. */
2867 next_token
->type
!= CPP_COMMA
2868 /* The curly brace at the end of an enum-specifier. */
2869 && next_token
->type
!= CPP_CLOSE_BRACE
2870 /* The end of a statement. */
2871 && next_token
->type
!= CPP_SEMICOLON
2872 /* The end of the cast-expression. */
2873 && next_token
->type
!= CPP_CLOSE_PAREN
2874 /* The end of an array bound. */
2875 && next_token
->type
!= CPP_CLOSE_SQUARE
2876 /* The closing ">" in a template-argument-list. */
2877 && (next_token
->type
!= CPP_GREATER
2878 || parser
->greater_than_is_operator_p
))
2882 /* If we are within a cast, then the constraint that the
2883 cast is to an integral or enumeration type will be
2884 checked at that point. If we are not within a cast, then
2885 this code is invalid. */
2887 cp_parser_non_integral_constant_expression
2888 (parser
, "floating-point literal");
2890 return token
->value
;
2894 /* ??? Should wide strings be allowed when parser->translate_strings_p
2895 is false (i.e. in attributes)? If not, we can kill the third
2896 argument to cp_parser_string_literal. */
2897 return cp_parser_string_literal (parser
,
2898 parser
->translate_strings_p
,
2901 case CPP_OPEN_PAREN
:
2904 bool saved_greater_than_is_operator_p
;
2906 /* Consume the `('. */
2907 cp_lexer_consume_token (parser
->lexer
);
2908 /* Within a parenthesized expression, a `>' token is always
2909 the greater-than operator. */
2910 saved_greater_than_is_operator_p
2911 = parser
->greater_than_is_operator_p
;
2912 parser
->greater_than_is_operator_p
= true;
2913 /* If we see `( { ' then we are looking at the beginning of
2914 a GNU statement-expression. */
2915 if (cp_parser_allow_gnu_extensions_p (parser
)
2916 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
2918 /* Statement-expressions are not allowed by the standard. */
2920 pedwarn ("ISO C++ forbids braced-groups within expressions");
2922 /* And they're not allowed outside of a function-body; you
2923 cannot, for example, write:
2925 int i = ({ int j = 3; j + 1; });
2927 at class or namespace scope. */
2928 if (!at_function_scope_p ())
2929 error ("statement-expressions are allowed only inside functions");
2930 /* Start the statement-expression. */
2931 expr
= begin_stmt_expr ();
2932 /* Parse the compound-statement. */
2933 cp_parser_compound_statement (parser
, expr
, false);
2935 expr
= finish_stmt_expr (expr
, false);
2939 /* Parse the parenthesized expression. */
2940 expr
= cp_parser_expression (parser
, cast_p
);
2941 /* Let the front end know that this expression was
2942 enclosed in parentheses. This matters in case, for
2943 example, the expression is of the form `A::B', since
2944 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2946 finish_parenthesized_expr (expr
);
2948 /* The `>' token might be the end of a template-id or
2949 template-parameter-list now. */
2950 parser
->greater_than_is_operator_p
2951 = saved_greater_than_is_operator_p
;
2952 /* Consume the `)'. */
2953 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
2954 cp_parser_skip_to_end_of_statement (parser
);
2960 switch (token
->keyword
)
2962 /* These two are the boolean literals. */
2964 cp_lexer_consume_token (parser
->lexer
);
2965 return boolean_true_node
;
2967 cp_lexer_consume_token (parser
->lexer
);
2968 return boolean_false_node
;
2970 /* The `__null' literal. */
2972 cp_lexer_consume_token (parser
->lexer
);
2975 /* Recognize the `this' keyword. */
2977 cp_lexer_consume_token (parser
->lexer
);
2978 if (parser
->local_variables_forbidden_p
)
2980 error ("%<this%> may not be used in this context");
2981 return error_mark_node
;
2983 /* Pointers cannot appear in constant-expressions. */
2984 if (cp_parser_non_integral_constant_expression (parser
,
2986 return error_mark_node
;
2987 return finish_this_expr ();
2989 /* The `operator' keyword can be the beginning of an
2994 case RID_FUNCTION_NAME
:
2995 case RID_PRETTY_FUNCTION_NAME
:
2996 case RID_C99_FUNCTION_NAME
:
2997 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2998 __func__ are the names of variables -- but they are
2999 treated specially. Therefore, they are handled here,
3000 rather than relying on the generic id-expression logic
3001 below. Grammatically, these names are id-expressions.
3003 Consume the token. */
3004 token
= cp_lexer_consume_token (parser
->lexer
);
3005 /* Look up the name. */
3006 return finish_fname (token
->value
);
3013 /* The `__builtin_va_arg' construct is used to handle
3014 `va_arg'. Consume the `__builtin_va_arg' token. */
3015 cp_lexer_consume_token (parser
->lexer
);
3016 /* Look for the opening `('. */
3017 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3018 /* Now, parse the assignment-expression. */
3019 expression
= cp_parser_assignment_expression (parser
,
3021 /* Look for the `,'. */
3022 cp_parser_require (parser
, CPP_COMMA
, "`,'");
3023 /* Parse the type-id. */
3024 type
= cp_parser_type_id (parser
);
3025 /* Look for the closing `)'. */
3026 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3027 /* Using `va_arg' in a constant-expression is not
3029 if (cp_parser_non_integral_constant_expression (parser
,
3031 return error_mark_node
;
3032 return build_x_va_arg (expression
, type
);
3036 return cp_parser_builtin_offsetof (parser
);
3038 /* Objective-C++ expressions. */
3040 case RID_AT_PROTOCOL
:
3041 case RID_AT_SELECTOR
:
3042 return cp_parser_objc_expression (parser
);
3045 cp_parser_error (parser
, "expected primary-expression");
3046 return error_mark_node
;
3049 /* An id-expression can start with either an identifier, a
3050 `::' as the beginning of a qualified-id, or the "operator"
3054 case CPP_TEMPLATE_ID
:
3055 case CPP_NESTED_NAME_SPECIFIER
:
3059 const char *error_msg
;
3064 /* Parse the id-expression. */
3066 = cp_parser_id_expression (parser
,
3067 /*template_keyword_p=*/false,
3068 /*check_dependency_p=*/true,
3070 /*declarator_p=*/false,
3071 /*optional_p=*/false);
3072 if (id_expression
== error_mark_node
)
3073 return error_mark_node
;
3074 token
= cp_lexer_peek_token (parser
->lexer
);
3075 done
= (token
->type
!= CPP_OPEN_SQUARE
3076 && token
->type
!= CPP_OPEN_PAREN
3077 && token
->type
!= CPP_DOT
3078 && token
->type
!= CPP_DEREF
3079 && token
->type
!= CPP_PLUS_PLUS
3080 && token
->type
!= CPP_MINUS_MINUS
);
3081 /* If we have a template-id, then no further lookup is
3082 required. If the template-id was for a template-class, we
3083 will sometimes have a TYPE_DECL at this point. */
3084 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
3085 || TREE_CODE (id_expression
) == TYPE_DECL
)
3086 decl
= id_expression
;
3087 /* Look up the name. */
3090 tree ambiguous_decls
;
3092 decl
= cp_parser_lookup_name (parser
, id_expression
,
3095 /*is_namespace=*/false,
3096 /*check_dependency=*/true,
3098 /* If the lookup was ambiguous, an error will already have
3100 if (ambiguous_decls
)
3101 return error_mark_node
;
3103 /* In Objective-C++, an instance variable (ivar) may be preferred
3104 to whatever cp_parser_lookup_name() found. */
3105 decl
= objc_lookup_ivar (decl
, id_expression
);
3107 /* If name lookup gives us a SCOPE_REF, then the
3108 qualifying scope was dependent. */
3109 if (TREE_CODE (decl
) == SCOPE_REF
)
3111 /* Check to see if DECL is a local variable in a context
3112 where that is forbidden. */
3113 if (parser
->local_variables_forbidden_p
3114 && local_variable_p (decl
))
3116 /* It might be that we only found DECL because we are
3117 trying to be generous with pre-ISO scoping rules.
3118 For example, consider:
3122 for (int i = 0; i < 10; ++i) {}
3123 extern void f(int j = i);
3126 Here, name look up will originally find the out
3127 of scope `i'. We need to issue a warning message,
3128 but then use the global `i'. */
3129 decl
= check_for_out_of_scope_variable (decl
);
3130 if (local_variable_p (decl
))
3132 error ("local variable %qD may not appear in this context",
3134 return error_mark_node
;
3139 decl
= (finish_id_expression
3140 (id_expression
, decl
, parser
->scope
,
3142 parser
->integral_constant_expression_p
,
3143 parser
->allow_non_integral_constant_expression_p
,
3144 &parser
->non_integral_constant_expression_p
,
3145 template_p
, done
, address_p
,
3149 cp_parser_error (parser
, error_msg
);
3153 /* Anything else is an error. */
3155 /* ...unless we have an Objective-C++ message or string literal, that is. */
3156 if (c_dialect_objc ()
3157 && (token
->type
== CPP_OPEN_SQUARE
|| token
->type
== CPP_OBJC_STRING
))
3158 return cp_parser_objc_expression (parser
);
3160 cp_parser_error (parser
, "expected primary-expression");
3161 return error_mark_node
;
3165 /* Parse an id-expression.
3172 :: [opt] nested-name-specifier template [opt] unqualified-id
3174 :: operator-function-id
3177 Return a representation of the unqualified portion of the
3178 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3179 a `::' or nested-name-specifier.
3181 Often, if the id-expression was a qualified-id, the caller will
3182 want to make a SCOPE_REF to represent the qualified-id. This
3183 function does not do this in order to avoid wastefully creating
3184 SCOPE_REFs when they are not required.
3186 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3189 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3190 uninstantiated templates.
3192 If *TEMPLATE_P is non-NULL, it is set to true iff the
3193 `template' keyword is used to explicitly indicate that the entity
3194 named is a template.
3196 If DECLARATOR_P is true, the id-expression is appearing as part of
3197 a declarator, rather than as part of an expression. */
3200 cp_parser_id_expression (cp_parser
*parser
,
3201 bool template_keyword_p
,
3202 bool check_dependency_p
,
3207 bool global_scope_p
;
3208 bool nested_name_specifier_p
;
3210 /* Assume the `template' keyword was not used. */
3212 *template_p
= template_keyword_p
;
3214 /* Look for the optional `::' operator. */
3216 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
3218 /* Look for the optional nested-name-specifier. */
3219 nested_name_specifier_p
3220 = (cp_parser_nested_name_specifier_opt (parser
,
3221 /*typename_keyword_p=*/false,
3226 /* If there is a nested-name-specifier, then we are looking at
3227 the first qualified-id production. */
3228 if (nested_name_specifier_p
)
3231 tree saved_object_scope
;
3232 tree saved_qualifying_scope
;
3233 tree unqualified_id
;
3236 /* See if the next token is the `template' keyword. */
3238 template_p
= &is_template
;
3239 *template_p
= cp_parser_optional_template_keyword (parser
);
3240 /* Name lookup we do during the processing of the
3241 unqualified-id might obliterate SCOPE. */
3242 saved_scope
= parser
->scope
;
3243 saved_object_scope
= parser
->object_scope
;
3244 saved_qualifying_scope
= parser
->qualifying_scope
;
3245 /* Process the final unqualified-id. */
3246 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
3249 /*optional_p=*/false);
3250 /* Restore the SAVED_SCOPE for our caller. */
3251 parser
->scope
= saved_scope
;
3252 parser
->object_scope
= saved_object_scope
;
3253 parser
->qualifying_scope
= saved_qualifying_scope
;
3255 return unqualified_id
;
3257 /* Otherwise, if we are in global scope, then we are looking at one
3258 of the other qualified-id productions. */
3259 else if (global_scope_p
)
3264 /* Peek at the next token. */
3265 token
= cp_lexer_peek_token (parser
->lexer
);
3267 /* If it's an identifier, and the next token is not a "<", then
3268 we can avoid the template-id case. This is an optimization
3269 for this common case. */
3270 if (token
->type
== CPP_NAME
3271 && !cp_parser_nth_token_starts_template_argument_list_p
3273 return cp_parser_identifier (parser
);
3275 cp_parser_parse_tentatively (parser
);
3276 /* Try a template-id. */
3277 id
= cp_parser_template_id (parser
,
3278 /*template_keyword_p=*/false,
3279 /*check_dependency_p=*/true,
3281 /* If that worked, we're done. */
3282 if (cp_parser_parse_definitely (parser
))
3285 /* Peek at the next token. (Changes in the token buffer may
3286 have invalidated the pointer obtained above.) */
3287 token
= cp_lexer_peek_token (parser
->lexer
);
3289 switch (token
->type
)
3292 return cp_parser_identifier (parser
);
3295 if (token
->keyword
== RID_OPERATOR
)
3296 return cp_parser_operator_function_id (parser
);
3300 cp_parser_error (parser
, "expected id-expression");
3301 return error_mark_node
;
3305 return cp_parser_unqualified_id (parser
, template_keyword_p
,
3306 /*check_dependency_p=*/true,
3311 /* Parse an unqualified-id.
3315 operator-function-id
3316 conversion-function-id
3320 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3321 keyword, in a construct like `A::template ...'.
3323 Returns a representation of unqualified-id. For the `identifier'
3324 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3325 production a BIT_NOT_EXPR is returned; the operand of the
3326 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3327 other productions, see the documentation accompanying the
3328 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3329 names are looked up in uninstantiated templates. If DECLARATOR_P
3330 is true, the unqualified-id is appearing as part of a declarator,
3331 rather than as part of an expression. */
3334 cp_parser_unqualified_id (cp_parser
* parser
,
3335 bool template_keyword_p
,
3336 bool check_dependency_p
,
3342 /* Peek at the next token. */
3343 token
= cp_lexer_peek_token (parser
->lexer
);
3345 switch (token
->type
)
3351 /* We don't know yet whether or not this will be a
3353 cp_parser_parse_tentatively (parser
);
3354 /* Try a template-id. */
3355 id
= cp_parser_template_id (parser
, template_keyword_p
,
3358 /* If it worked, we're done. */
3359 if (cp_parser_parse_definitely (parser
))
3361 /* Otherwise, it's an ordinary identifier. */
3362 return cp_parser_identifier (parser
);
3365 case CPP_TEMPLATE_ID
:
3366 return cp_parser_template_id (parser
, template_keyword_p
,
3373 tree qualifying_scope
;
3378 /* Consume the `~' token. */
3379 cp_lexer_consume_token (parser
->lexer
);
3380 /* Parse the class-name. The standard, as written, seems to
3383 template <typename T> struct S { ~S (); };
3384 template <typename T> S<T>::~S() {}
3386 is invalid, since `~' must be followed by a class-name, but
3387 `S<T>' is dependent, and so not known to be a class.
3388 That's not right; we need to look in uninstantiated
3389 templates. A further complication arises from:
3391 template <typename T> void f(T t) {
3395 Here, it is not possible to look up `T' in the scope of `T'
3396 itself. We must look in both the current scope, and the
3397 scope of the containing complete expression.
3399 Yet another issue is:
3408 The standard does not seem to say that the `S' in `~S'
3409 should refer to the type `S' and not the data member
3412 /* DR 244 says that we look up the name after the "~" in the
3413 same scope as we looked up the qualifying name. That idea
3414 isn't fully worked out; it's more complicated than that. */
3415 scope
= parser
->scope
;
3416 object_scope
= parser
->object_scope
;
3417 qualifying_scope
= parser
->qualifying_scope
;
3419 /* If the name is of the form "X::~X" it's OK. */
3420 token
= cp_lexer_peek_token (parser
->lexer
);
3421 if (scope
&& TYPE_P (scope
)
3422 && token
->type
== CPP_NAME
3423 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3425 && constructor_name_p (token
->value
, scope
))
3427 cp_lexer_consume_token (parser
->lexer
);
3428 return build_nt (BIT_NOT_EXPR
, scope
);
3431 /* If there was an explicit qualification (S::~T), first look
3432 in the scope given by the qualification (i.e., S). */
3434 type_decl
= NULL_TREE
;
3437 cp_parser_parse_tentatively (parser
);
3438 type_decl
= cp_parser_class_name (parser
,
3439 /*typename_keyword_p=*/false,
3440 /*template_keyword_p=*/false,
3442 /*check_dependency=*/false,
3443 /*class_head_p=*/false,
3445 if (cp_parser_parse_definitely (parser
))
3448 /* In "N::S::~S", look in "N" as well. */
3449 if (!done
&& scope
&& qualifying_scope
)
3451 cp_parser_parse_tentatively (parser
);
3452 parser
->scope
= qualifying_scope
;
3453 parser
->object_scope
= NULL_TREE
;
3454 parser
->qualifying_scope
= NULL_TREE
;
3456 = cp_parser_class_name (parser
,
3457 /*typename_keyword_p=*/false,
3458 /*template_keyword_p=*/false,
3460 /*check_dependency=*/false,
3461 /*class_head_p=*/false,
3463 if (cp_parser_parse_definitely (parser
))
3466 /* In "p->S::~T", look in the scope given by "*p" as well. */
3467 else if (!done
&& object_scope
)
3469 cp_parser_parse_tentatively (parser
);
3470 parser
->scope
= object_scope
;
3471 parser
->object_scope
= NULL_TREE
;
3472 parser
->qualifying_scope
= NULL_TREE
;
3474 = cp_parser_class_name (parser
,
3475 /*typename_keyword_p=*/false,
3476 /*template_keyword_p=*/false,
3478 /*check_dependency=*/false,
3479 /*class_head_p=*/false,
3481 if (cp_parser_parse_definitely (parser
))
3484 /* Look in the surrounding context. */
3487 parser
->scope
= NULL_TREE
;
3488 parser
->object_scope
= NULL_TREE
;
3489 parser
->qualifying_scope
= NULL_TREE
;
3491 = cp_parser_class_name (parser
,
3492 /*typename_keyword_p=*/false,
3493 /*template_keyword_p=*/false,
3495 /*check_dependency=*/false,
3496 /*class_head_p=*/false,
3499 /* If an error occurred, assume that the name of the
3500 destructor is the same as the name of the qualifying
3501 class. That allows us to keep parsing after running
3502 into ill-formed destructor names. */
3503 if (type_decl
== error_mark_node
&& scope
&& TYPE_P (scope
))
3504 return build_nt (BIT_NOT_EXPR
, scope
);
3505 else if (type_decl
== error_mark_node
)
3506 return error_mark_node
;
3508 /* Check that destructor name and scope match. */
3509 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
3511 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3512 error ("declaration of %<~%T%> as member of %qT",
3514 return error_mark_node
;
3519 A typedef-name that names a class shall not be used as the
3520 identifier in the declarator for a destructor declaration. */
3522 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
3523 && !DECL_SELF_REFERENCE_P (type_decl
)
3524 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
3525 error ("typedef-name %qD used as destructor declarator",
3528 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3532 if (token
->keyword
== RID_OPERATOR
)
3536 /* This could be a template-id, so we try that first. */
3537 cp_parser_parse_tentatively (parser
);
3538 /* Try a template-id. */
3539 id
= cp_parser_template_id (parser
, template_keyword_p
,
3540 /*check_dependency_p=*/true,
3542 /* If that worked, we're done. */
3543 if (cp_parser_parse_definitely (parser
))
3545 /* We still don't know whether we're looking at an
3546 operator-function-id or a conversion-function-id. */
3547 cp_parser_parse_tentatively (parser
);
3548 /* Try an operator-function-id. */
3549 id
= cp_parser_operator_function_id (parser
);
3550 /* If that didn't work, try a conversion-function-id. */
3551 if (!cp_parser_parse_definitely (parser
))
3552 id
= cp_parser_conversion_function_id (parser
);
3561 cp_parser_error (parser
, "expected unqualified-id");
3562 return error_mark_node
;
3566 /* Parse an (optional) nested-name-specifier.
3568 nested-name-specifier:
3569 class-or-namespace-name :: nested-name-specifier [opt]
3570 class-or-namespace-name :: template nested-name-specifier [opt]
3572 PARSER->SCOPE should be set appropriately before this function is
3573 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3574 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3577 Sets PARSER->SCOPE to the class (TYPE) or namespace
3578 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3579 it unchanged if there is no nested-name-specifier. Returns the new
3580 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3582 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3583 part of a declaration and/or decl-specifier. */
3586 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3587 bool typename_keyword_p
,
3588 bool check_dependency_p
,
3590 bool is_declaration
)
3592 bool success
= false;
3593 cp_token_position start
= 0;
3596 /* Remember where the nested-name-specifier starts. */
3597 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3599 start
= cp_lexer_token_position (parser
->lexer
, false);
3600 push_deferring_access_checks (dk_deferred
);
3607 tree saved_qualifying_scope
;
3608 bool template_keyword_p
;
3610 /* Spot cases that cannot be the beginning of a
3611 nested-name-specifier. */
3612 token
= cp_lexer_peek_token (parser
->lexer
);
3614 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3615 the already parsed nested-name-specifier. */
3616 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3618 /* Grab the nested-name-specifier and continue the loop. */
3619 cp_parser_pre_parsed_nested_name_specifier (parser
);
3624 /* Spot cases that cannot be the beginning of a
3625 nested-name-specifier. On the second and subsequent times
3626 through the loop, we look for the `template' keyword. */
3627 if (success
&& token
->keyword
== RID_TEMPLATE
)
3629 /* A template-id can start a nested-name-specifier. */
3630 else if (token
->type
== CPP_TEMPLATE_ID
)
3634 /* If the next token is not an identifier, then it is
3635 definitely not a class-or-namespace-name. */
3636 if (token
->type
!= CPP_NAME
)
3638 /* If the following token is neither a `<' (to begin a
3639 template-id), nor a `::', then we are not looking at a
3640 nested-name-specifier. */
3641 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3642 if (token
->type
!= CPP_SCOPE
3643 && !cp_parser_nth_token_starts_template_argument_list_p
3648 /* The nested-name-specifier is optional, so we parse
3650 cp_parser_parse_tentatively (parser
);
3652 /* Look for the optional `template' keyword, if this isn't the
3653 first time through the loop. */
3655 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3657 template_keyword_p
= false;
3659 /* Save the old scope since the name lookup we are about to do
3660 might destroy it. */
3661 old_scope
= parser
->scope
;
3662 saved_qualifying_scope
= parser
->qualifying_scope
;
3663 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3664 look up names in "X<T>::I" in order to determine that "Y" is
3665 a template. So, if we have a typename at this point, we make
3666 an effort to look through it. */
3668 && !typename_keyword_p
3670 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3671 parser
->scope
= resolve_typename_type (parser
->scope
,
3672 /*only_current_p=*/false);
3673 /* Parse the qualifying entity. */
3675 = cp_parser_class_or_namespace_name (parser
,
3681 /* Look for the `::' token. */
3682 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3684 /* If we found what we wanted, we keep going; otherwise, we're
3686 if (!cp_parser_parse_definitely (parser
))
3688 bool error_p
= false;
3690 /* Restore the OLD_SCOPE since it was valid before the
3691 failed attempt at finding the last
3692 class-or-namespace-name. */
3693 parser
->scope
= old_scope
;
3694 parser
->qualifying_scope
= saved_qualifying_scope
;
3695 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3697 /* If the next token is an identifier, and the one after
3698 that is a `::', then any valid interpretation would have
3699 found a class-or-namespace-name. */
3700 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3701 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3703 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
3706 token
= cp_lexer_consume_token (parser
->lexer
);
3709 if (!token
->ambiguous_p
)
3712 tree ambiguous_decls
;
3714 decl
= cp_parser_lookup_name (parser
, token
->value
,
3716 /*is_template=*/false,
3717 /*is_namespace=*/false,
3718 /*check_dependency=*/true,
3720 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3721 error ("%qD used without template parameters", decl
);
3722 else if (ambiguous_decls
)
3724 error ("reference to %qD is ambiguous",
3726 print_candidates (ambiguous_decls
);
3727 decl
= error_mark_node
;
3730 cp_parser_name_lookup_error
3731 (parser
, token
->value
, decl
,
3732 "is not a class or namespace");
3734 parser
->scope
= error_mark_node
;
3736 /* Treat this as a successful nested-name-specifier
3741 If the name found is not a class-name (clause
3742 _class_) or namespace-name (_namespace.def_), the
3743 program is ill-formed. */
3746 cp_lexer_consume_token (parser
->lexer
);
3750 /* We've found one valid nested-name-specifier. */
3752 /* Name lookup always gives us a DECL. */
3753 if (TREE_CODE (new_scope
) == TYPE_DECL
)
3754 new_scope
= TREE_TYPE (new_scope
);
3755 /* Uses of "template" must be followed by actual templates. */
3756 if (template_keyword_p
3757 && !(CLASS_TYPE_P (new_scope
)
3758 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
3759 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
3760 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
3761 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
3762 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
3763 == TEMPLATE_ID_EXPR
)))
3764 pedwarn (TYPE_P (new_scope
)
3765 ? "%qT is not a template"
3766 : "%qD is not a template",
3768 /* If it is a class scope, try to complete it; we are about to
3769 be looking up names inside the class. */
3770 if (TYPE_P (new_scope
)
3771 /* Since checking types for dependency can be expensive,
3772 avoid doing it if the type is already complete. */
3773 && !COMPLETE_TYPE_P (new_scope
)
3774 /* Do not try to complete dependent types. */
3775 && !dependent_type_p (new_scope
))
3776 new_scope
= complete_type (new_scope
);
3777 /* Make sure we look in the right scope the next time through
3779 parser
->scope
= new_scope
;
3782 /* If parsing tentatively, replace the sequence of tokens that makes
3783 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3784 token. That way, should we re-parse the token stream, we will
3785 not have to repeat the effort required to do the parse, nor will
3786 we issue duplicate error messages. */
3787 if (success
&& start
)
3792 token
= cp_lexer_token_at (parser
->lexer
, start
);
3793 /* Reset the contents of the START token. */
3794 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
3795 /* Retrieve any deferred checks. Do not pop this access checks yet
3796 so the memory will not be reclaimed during token replacing below. */
3797 access_checks
= get_deferred_access_checks ();
3798 token
->value
= build_tree_list (copy_list (access_checks
),
3800 TREE_TYPE (token
->value
) = parser
->qualifying_scope
;
3801 token
->keyword
= RID_MAX
;
3803 /* Purge all subsequent tokens. */
3804 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3808 pop_to_parent_deferring_access_checks ();
3810 return success
? parser
->scope
: NULL_TREE
;
3813 /* Parse a nested-name-specifier. See
3814 cp_parser_nested_name_specifier_opt for details. This function
3815 behaves identically, except that it will an issue an error if no
3816 nested-name-specifier is present. */
3819 cp_parser_nested_name_specifier (cp_parser
*parser
,
3820 bool typename_keyword_p
,
3821 bool check_dependency_p
,
3823 bool is_declaration
)
3827 /* Look for the nested-name-specifier. */
3828 scope
= cp_parser_nested_name_specifier_opt (parser
,
3833 /* If it was not present, issue an error message. */
3836 cp_parser_error (parser
, "expected nested-name-specifier");
3837 parser
->scope
= NULL_TREE
;
3843 /* Parse a class-or-namespace-name.
3845 class-or-namespace-name:
3849 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3850 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3851 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3852 TYPE_P is TRUE iff the next name should be taken as a class-name,
3853 even the same name is declared to be another entity in the same
3856 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3857 specified by the class-or-namespace-name. If neither is found the
3858 ERROR_MARK_NODE is returned. */
3861 cp_parser_class_or_namespace_name (cp_parser
*parser
,
3862 bool typename_keyword_p
,
3863 bool template_keyword_p
,
3864 bool check_dependency_p
,
3866 bool is_declaration
)
3869 tree saved_qualifying_scope
;
3870 tree saved_object_scope
;
3874 /* Before we try to parse the class-name, we must save away the
3875 current PARSER->SCOPE since cp_parser_class_name will destroy
3877 saved_scope
= parser
->scope
;
3878 saved_qualifying_scope
= parser
->qualifying_scope
;
3879 saved_object_scope
= parser
->object_scope
;
3880 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3881 there is no need to look for a namespace-name. */
3882 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
3884 cp_parser_parse_tentatively (parser
);
3885 scope
= cp_parser_class_name (parser
,
3888 type_p
? class_type
: none_type
,
3890 /*class_head_p=*/false,
3892 /* If that didn't work, try for a namespace-name. */
3893 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
3895 /* Restore the saved scope. */
3896 parser
->scope
= saved_scope
;
3897 parser
->qualifying_scope
= saved_qualifying_scope
;
3898 parser
->object_scope
= saved_object_scope
;
3899 /* If we are not looking at an identifier followed by the scope
3900 resolution operator, then this is not part of a
3901 nested-name-specifier. (Note that this function is only used
3902 to parse the components of a nested-name-specifier.) */
3903 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
3904 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
3905 return error_mark_node
;
3906 scope
= cp_parser_namespace_name (parser
);
3912 /* Parse a postfix-expression.
3916 postfix-expression [ expression ]
3917 postfix-expression ( expression-list [opt] )
3918 simple-type-specifier ( expression-list [opt] )
3919 typename :: [opt] nested-name-specifier identifier
3920 ( expression-list [opt] )
3921 typename :: [opt] nested-name-specifier template [opt] template-id
3922 ( expression-list [opt] )
3923 postfix-expression . template [opt] id-expression
3924 postfix-expression -> template [opt] id-expression
3925 postfix-expression . pseudo-destructor-name
3926 postfix-expression -> pseudo-destructor-name
3927 postfix-expression ++
3928 postfix-expression --
3929 dynamic_cast < type-id > ( expression )
3930 static_cast < type-id > ( expression )
3931 reinterpret_cast < type-id > ( expression )
3932 const_cast < type-id > ( expression )
3933 typeid ( expression )
3939 ( type-id ) { initializer-list , [opt] }
3941 This extension is a GNU version of the C99 compound-literal
3942 construct. (The C99 grammar uses `type-name' instead of `type-id',
3943 but they are essentially the same concept.)
3945 If ADDRESS_P is true, the postfix expression is the operand of the
3946 `&' operator. CAST_P is true if this expression is the target of a
3949 Returns a representation of the expression. */
3952 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
3956 cp_id_kind idk
= CP_ID_KIND_NONE
;
3957 tree postfix_expression
= NULL_TREE
;
3959 /* Peek at the next token. */
3960 token
= cp_lexer_peek_token (parser
->lexer
);
3961 /* Some of the productions are determined by keywords. */
3962 keyword
= token
->keyword
;
3972 const char *saved_message
;
3974 /* All of these can be handled in the same way from the point
3975 of view of parsing. Begin by consuming the token
3976 identifying the cast. */
3977 cp_lexer_consume_token (parser
->lexer
);
3979 /* New types cannot be defined in the cast. */
3980 saved_message
= parser
->type_definition_forbidden_message
;
3981 parser
->type_definition_forbidden_message
3982 = "types may not be defined in casts";
3984 /* Look for the opening `<'. */
3985 cp_parser_require (parser
, CPP_LESS
, "`<'");
3986 /* Parse the type to which we are casting. */
3987 type
= cp_parser_type_id (parser
);
3988 /* Look for the closing `>'. */
3989 cp_parser_require (parser
, CPP_GREATER
, "`>'");
3990 /* Restore the old message. */
3991 parser
->type_definition_forbidden_message
= saved_message
;
3993 /* And the expression which is being cast. */
3994 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3995 expression
= cp_parser_expression (parser
, /*cast_p=*/true);
3996 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3998 /* Only type conversions to integral or enumeration types
3999 can be used in constant-expressions. */
4000 if (parser
->integral_constant_expression_p
4001 && !dependent_type_p (type
)
4002 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
4003 && (cp_parser_non_integral_constant_expression
4005 "a cast to a type other than an integral or "
4006 "enumeration type")))
4007 return error_mark_node
;
4013 = build_dynamic_cast (type
, expression
);
4017 = build_static_cast (type
, expression
);
4021 = build_reinterpret_cast (type
, expression
);
4025 = build_const_cast (type
, expression
);
4036 const char *saved_message
;
4037 bool saved_in_type_id_in_expr_p
;
4039 /* Consume the `typeid' token. */
4040 cp_lexer_consume_token (parser
->lexer
);
4041 /* Look for the `(' token. */
4042 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4043 /* Types cannot be defined in a `typeid' expression. */
4044 saved_message
= parser
->type_definition_forbidden_message
;
4045 parser
->type_definition_forbidden_message
4046 = "types may not be defined in a `typeid\' expression";
4047 /* We can't be sure yet whether we're looking at a type-id or an
4049 cp_parser_parse_tentatively (parser
);
4050 /* Try a type-id first. */
4051 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4052 parser
->in_type_id_in_expr_p
= true;
4053 type
= cp_parser_type_id (parser
);
4054 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4055 /* Look for the `)' token. Otherwise, we can't be sure that
4056 we're not looking at an expression: consider `typeid (int
4057 (3))', for example. */
4058 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4059 /* If all went well, simply lookup the type-id. */
4060 if (cp_parser_parse_definitely (parser
))
4061 postfix_expression
= get_typeid (type
);
4062 /* Otherwise, fall back to the expression variant. */
4067 /* Look for an expression. */
4068 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
4069 /* Compute its typeid. */
4070 postfix_expression
= build_typeid (expression
);
4071 /* Look for the `)' token. */
4072 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4074 /* `typeid' may not appear in an integral constant expression. */
4075 if (cp_parser_non_integral_constant_expression(parser
,
4076 "`typeid' operator"))
4077 return error_mark_node
;
4078 /* Restore the saved message. */
4079 parser
->type_definition_forbidden_message
= saved_message
;
4086 /* The syntax permitted here is the same permitted for an
4087 elaborated-type-specifier. */
4088 type
= cp_parser_elaborated_type_specifier (parser
,
4089 /*is_friend=*/false,
4090 /*is_declaration=*/false);
4091 postfix_expression
= cp_parser_functional_cast (parser
, type
);
4099 /* If the next thing is a simple-type-specifier, we may be
4100 looking at a functional cast. We could also be looking at
4101 an id-expression. So, we try the functional cast, and if
4102 that doesn't work we fall back to the primary-expression. */
4103 cp_parser_parse_tentatively (parser
);
4104 /* Look for the simple-type-specifier. */
4105 type
= cp_parser_simple_type_specifier (parser
,
4106 /*decl_specs=*/NULL
,
4107 CP_PARSER_FLAGS_NONE
);
4108 /* Parse the cast itself. */
4109 if (!cp_parser_error_occurred (parser
))
4111 = cp_parser_functional_cast (parser
, type
);
4112 /* If that worked, we're done. */
4113 if (cp_parser_parse_definitely (parser
))
4116 /* If the functional-cast didn't work out, try a
4117 compound-literal. */
4118 if (cp_parser_allow_gnu_extensions_p (parser
)
4119 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4121 VEC(constructor_elt
,gc
) *initializer_list
= NULL
;
4122 bool saved_in_type_id_in_expr_p
;
4124 cp_parser_parse_tentatively (parser
);
4125 /* Consume the `('. */
4126 cp_lexer_consume_token (parser
->lexer
);
4127 /* Parse the type. */
4128 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4129 parser
->in_type_id_in_expr_p
= true;
4130 type
= cp_parser_type_id (parser
);
4131 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4132 /* Look for the `)'. */
4133 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4134 /* Look for the `{'. */
4135 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
4136 /* If things aren't going well, there's no need to
4138 if (!cp_parser_error_occurred (parser
))
4140 bool non_constant_p
;
4141 /* Parse the initializer-list. */
4143 = cp_parser_initializer_list (parser
, &non_constant_p
);
4144 /* Allow a trailing `,'. */
4145 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
4146 cp_lexer_consume_token (parser
->lexer
);
4147 /* Look for the final `}'. */
4148 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
4150 /* If that worked, we're definitely looking at a
4151 compound-literal expression. */
4152 if (cp_parser_parse_definitely (parser
))
4154 /* Warn the user that a compound literal is not
4155 allowed in standard C++. */
4157 pedwarn ("ISO C++ forbids compound-literals");
4158 /* Form the representation of the compound-literal. */
4160 = finish_compound_literal (type
, initializer_list
);
4165 /* It must be a primary-expression. */
4167 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
4168 /*template_arg_p=*/false,
4174 /* Keep looping until the postfix-expression is complete. */
4177 if (idk
== CP_ID_KIND_UNQUALIFIED
4178 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
4179 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
4180 /* It is not a Koenig lookup function call. */
4182 = unqualified_name_lookup_error (postfix_expression
);
4184 /* Peek at the next token. */
4185 token
= cp_lexer_peek_token (parser
->lexer
);
4187 switch (token
->type
)
4189 case CPP_OPEN_SQUARE
:
4191 = cp_parser_postfix_open_square_expression (parser
,
4194 idk
= CP_ID_KIND_NONE
;
4197 case CPP_OPEN_PAREN
:
4198 /* postfix-expression ( expression-list [opt] ) */
4201 bool is_builtin_constant_p
;
4202 bool saved_integral_constant_expression_p
= false;
4203 bool saved_non_integral_constant_expression_p
= false;
4206 is_builtin_constant_p
4207 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
4208 if (is_builtin_constant_p
)
4210 /* The whole point of __builtin_constant_p is to allow
4211 non-constant expressions to appear as arguments. */
4212 saved_integral_constant_expression_p
4213 = parser
->integral_constant_expression_p
;
4214 saved_non_integral_constant_expression_p
4215 = parser
->non_integral_constant_expression_p
;
4216 parser
->integral_constant_expression_p
= false;
4218 args
= (cp_parser_parenthesized_expression_list
4219 (parser
, /*is_attribute_list=*/false,
4221 /*non_constant_p=*/NULL
));
4222 if (is_builtin_constant_p
)
4224 parser
->integral_constant_expression_p
4225 = saved_integral_constant_expression_p
;
4226 parser
->non_integral_constant_expression_p
4227 = saved_non_integral_constant_expression_p
;
4230 if (args
== error_mark_node
)
4232 postfix_expression
= error_mark_node
;
4236 /* Function calls are not permitted in
4237 constant-expressions. */
4238 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
4239 && cp_parser_non_integral_constant_expression (parser
,
4242 postfix_expression
= error_mark_node
;
4247 if (idk
== CP_ID_KIND_UNQUALIFIED
)
4249 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
4255 = perform_koenig_lookup (postfix_expression
, args
);
4259 = unqualified_fn_lookup_error (postfix_expression
);
4261 /* We do not perform argument-dependent lookup if
4262 normal lookup finds a non-function, in accordance
4263 with the expected resolution of DR 218. */
4264 else if (args
&& is_overloaded_fn (postfix_expression
))
4266 tree fn
= get_first_fn (postfix_expression
);
4268 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4269 fn
= OVL_CURRENT (TREE_OPERAND (fn
, 0));
4271 /* Only do argument dependent lookup if regular
4272 lookup does not find a set of member functions.
4273 [basic.lookup.koenig]/2a */
4274 if (!DECL_FUNCTION_MEMBER_P (fn
))
4278 = perform_koenig_lookup (postfix_expression
, args
);
4283 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
4285 tree instance
= TREE_OPERAND (postfix_expression
, 0);
4286 tree fn
= TREE_OPERAND (postfix_expression
, 1);
4288 if (processing_template_decl
4289 && (type_dependent_expression_p (instance
)
4290 || (!BASELINK_P (fn
)
4291 && TREE_CODE (fn
) != FIELD_DECL
)
4292 || type_dependent_expression_p (fn
)
4293 || any_type_dependent_arguments_p (args
)))
4296 = build_min_nt (CALL_EXPR
, postfix_expression
,
4301 if (BASELINK_P (fn
))
4303 = (build_new_method_call
4304 (instance
, fn
, args
, NULL_TREE
,
4305 (idk
== CP_ID_KIND_QUALIFIED
4306 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
),
4310 = finish_call_expr (postfix_expression
, args
,
4311 /*disallow_virtual=*/false,
4312 /*koenig_p=*/false);
4314 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
4315 || TREE_CODE (postfix_expression
) == MEMBER_REF
4316 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
4317 postfix_expression
= (build_offset_ref_call_from_tree
4318 (postfix_expression
, args
));
4319 else if (idk
== CP_ID_KIND_QUALIFIED
)
4320 /* A call to a static class member, or a namespace-scope
4323 = finish_call_expr (postfix_expression
, args
,
4324 /*disallow_virtual=*/true,
4327 /* All other function calls. */
4329 = finish_call_expr (postfix_expression
, args
,
4330 /*disallow_virtual=*/false,
4333 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4334 idk
= CP_ID_KIND_NONE
;
4340 /* postfix-expression . template [opt] id-expression
4341 postfix-expression . pseudo-destructor-name
4342 postfix-expression -> template [opt] id-expression
4343 postfix-expression -> pseudo-destructor-name */
4345 /* Consume the `.' or `->' operator. */
4346 cp_lexer_consume_token (parser
->lexer
);
4349 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
4355 /* postfix-expression ++ */
4356 /* Consume the `++' token. */
4357 cp_lexer_consume_token (parser
->lexer
);
4358 /* Generate a representation for the complete expression. */
4360 = finish_increment_expr (postfix_expression
,
4361 POSTINCREMENT_EXPR
);
4362 /* Increments may not appear in constant-expressions. */
4363 if (cp_parser_non_integral_constant_expression (parser
,
4365 postfix_expression
= error_mark_node
;
4366 idk
= CP_ID_KIND_NONE
;
4369 case CPP_MINUS_MINUS
:
4370 /* postfix-expression -- */
4371 /* Consume the `--' token. */
4372 cp_lexer_consume_token (parser
->lexer
);
4373 /* Generate a representation for the complete expression. */
4375 = finish_increment_expr (postfix_expression
,
4376 POSTDECREMENT_EXPR
);
4377 /* Decrements may not appear in constant-expressions. */
4378 if (cp_parser_non_integral_constant_expression (parser
,
4380 postfix_expression
= error_mark_node
;
4381 idk
= CP_ID_KIND_NONE
;
4385 return postfix_expression
;
4389 /* We should never get here. */
4391 return error_mark_node
;
4394 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4395 by cp_parser_builtin_offsetof. We're looking for
4397 postfix-expression [ expression ]
4399 FOR_OFFSETOF is set if we're being called in that context, which
4400 changes how we deal with integer constant expressions. */
4403 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
4404 tree postfix_expression
,
4409 /* Consume the `[' token. */
4410 cp_lexer_consume_token (parser
->lexer
);
4412 /* Parse the index expression. */
4413 /* ??? For offsetof, there is a question of what to allow here. If
4414 offsetof is not being used in an integral constant expression context,
4415 then we *could* get the right answer by computing the value at runtime.
4416 If we are in an integral constant expression context, then we might
4417 could accept any constant expression; hard to say without analysis.
4418 Rather than open the barn door too wide right away, allow only integer
4419 constant expressions here. */
4421 index
= cp_parser_constant_expression (parser
, false, NULL
);
4423 index
= cp_parser_expression (parser
, /*cast_p=*/false);
4425 /* Look for the closing `]'. */
4426 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4428 /* Build the ARRAY_REF. */
4429 postfix_expression
= grok_array_decl (postfix_expression
, index
);
4431 /* When not doing offsetof, array references are not permitted in
4432 constant-expressions. */
4434 && (cp_parser_non_integral_constant_expression
4435 (parser
, "an array reference")))
4436 postfix_expression
= error_mark_node
;
4438 return postfix_expression
;
4441 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4442 by cp_parser_builtin_offsetof. We're looking for
4444 postfix-expression . template [opt] id-expression
4445 postfix-expression . pseudo-destructor-name
4446 postfix-expression -> template [opt] id-expression
4447 postfix-expression -> pseudo-destructor-name
4449 FOR_OFFSETOF is set if we're being called in that context. That sorta
4450 limits what of the above we'll actually accept, but nevermind.
4451 TOKEN_TYPE is the "." or "->" token, which will already have been
4452 removed from the stream. */
4455 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
4456 enum cpp_ttype token_type
,
4457 tree postfix_expression
,
4458 bool for_offsetof
, cp_id_kind
*idk
)
4462 bool pseudo_destructor_p
;
4463 tree scope
= NULL_TREE
;
4465 /* If this is a `->' operator, dereference the pointer. */
4466 if (token_type
== CPP_DEREF
)
4467 postfix_expression
= build_x_arrow (postfix_expression
);
4468 /* Check to see whether or not the expression is type-dependent. */
4469 dependent_p
= type_dependent_expression_p (postfix_expression
);
4470 /* The identifier following the `->' or `.' is not qualified. */
4471 parser
->scope
= NULL_TREE
;
4472 parser
->qualifying_scope
= NULL_TREE
;
4473 parser
->object_scope
= NULL_TREE
;
4474 *idk
= CP_ID_KIND_NONE
;
4475 /* Enter the scope corresponding to the type of the object
4476 given by the POSTFIX_EXPRESSION. */
4477 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
4479 scope
= TREE_TYPE (postfix_expression
);
4480 /* According to the standard, no expression should ever have
4481 reference type. Unfortunately, we do not currently match
4482 the standard in this respect in that our internal representation
4483 of an expression may have reference type even when the standard
4484 says it does not. Therefore, we have to manually obtain the
4485 underlying type here. */
4486 scope
= non_reference (scope
);
4487 /* The type of the POSTFIX_EXPRESSION must be complete. */
4488 if (scope
== unknown_type_node
)
4490 error ("%qE does not have class type", postfix_expression
);
4494 scope
= complete_type_or_else (scope
, NULL_TREE
);
4495 /* Let the name lookup machinery know that we are processing a
4496 class member access expression. */
4497 parser
->context
->object_type
= scope
;
4498 /* If something went wrong, we want to be able to discern that case,
4499 as opposed to the case where there was no SCOPE due to the type
4500 of expression being dependent. */
4502 scope
= error_mark_node
;
4503 /* If the SCOPE was erroneous, make the various semantic analysis
4504 functions exit quickly -- and without issuing additional error
4506 if (scope
== error_mark_node
)
4507 postfix_expression
= error_mark_node
;
4510 /* Assume this expression is not a pseudo-destructor access. */
4511 pseudo_destructor_p
= false;
4513 /* If the SCOPE is a scalar type, then, if this is a valid program,
4514 we must be looking at a pseudo-destructor-name. */
4515 if (scope
&& SCALAR_TYPE_P (scope
))
4520 cp_parser_parse_tentatively (parser
);
4521 /* Parse the pseudo-destructor-name. */
4523 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
4524 if (cp_parser_parse_definitely (parser
))
4526 pseudo_destructor_p
= true;
4528 = finish_pseudo_destructor_expr (postfix_expression
,
4529 s
, TREE_TYPE (type
));
4533 if (!pseudo_destructor_p
)
4535 /* If the SCOPE is not a scalar type, we are looking at an
4536 ordinary class member access expression, rather than a
4537 pseudo-destructor-name. */
4539 /* Parse the id-expression. */
4540 name
= (cp_parser_id_expression
4542 cp_parser_optional_template_keyword (parser
),
4543 /*check_dependency_p=*/true,
4545 /*declarator_p=*/false,
4546 /*optional_p=*/false));
4547 /* In general, build a SCOPE_REF if the member name is qualified.
4548 However, if the name was not dependent and has already been
4549 resolved; there is no need to build the SCOPE_REF. For example;
4551 struct X { void f(); };
4552 template <typename T> void f(T* t) { t->X::f(); }
4554 Even though "t" is dependent, "X::f" is not and has been resolved
4555 to a BASELINK; there is no need to include scope information. */
4557 /* But we do need to remember that there was an explicit scope for
4558 virtual function calls. */
4560 *idk
= CP_ID_KIND_QUALIFIED
;
4562 /* If the name is a template-id that names a type, we will get a
4563 TYPE_DECL here. That is invalid code. */
4564 if (TREE_CODE (name
) == TYPE_DECL
)
4566 error ("invalid use of %qD", name
);
4567 postfix_expression
= error_mark_node
;
4571 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
4573 name
= build_qualified_name (/*type=*/NULL_TREE
,
4577 parser
->scope
= NULL_TREE
;
4578 parser
->qualifying_scope
= NULL_TREE
;
4579 parser
->object_scope
= NULL_TREE
;
4581 if (scope
&& name
&& BASELINK_P (name
))
4582 adjust_result_of_qualified_name_lookup
4583 (name
, BINFO_TYPE (BASELINK_BINFO (name
)), scope
);
4585 = finish_class_member_access_expr (postfix_expression
, name
,
4590 /* We no longer need to look up names in the scope of the object on
4591 the left-hand side of the `.' or `->' operator. */
4592 parser
->context
->object_type
= NULL_TREE
;
4594 /* Outside of offsetof, these operators may not appear in
4595 constant-expressions. */
4597 && (cp_parser_non_integral_constant_expression
4598 (parser
, token_type
== CPP_DEREF
? "'->'" : "`.'")))
4599 postfix_expression
= error_mark_node
;
4601 return postfix_expression
;
4604 /* Parse a parenthesized expression-list.
4607 assignment-expression
4608 expression-list, assignment-expression
4613 identifier, expression-list
4615 CAST_P is true if this expression is the target of a cast.
4617 Returns a TREE_LIST. The TREE_VALUE of each node is a
4618 representation of an assignment-expression. Note that a TREE_LIST
4619 is returned even if there is only a single expression in the list.
4620 error_mark_node is returned if the ( and or ) are
4621 missing. NULL_TREE is returned on no expressions. The parentheses
4622 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4623 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4624 indicates whether or not all of the expressions in the list were
4628 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
4629 bool is_attribute_list
,
4631 bool *non_constant_p
)
4633 tree expression_list
= NULL_TREE
;
4634 bool fold_expr_p
= is_attribute_list
;
4635 tree identifier
= NULL_TREE
;
4637 /* Assume all the expressions will be constant. */
4639 *non_constant_p
= false;
4641 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
4642 return error_mark_node
;
4644 /* Consume expressions until there are no more. */
4645 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
4650 /* At the beginning of attribute lists, check to see if the
4651 next token is an identifier. */
4652 if (is_attribute_list
4653 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
4657 /* Consume the identifier. */
4658 token
= cp_lexer_consume_token (parser
->lexer
);
4659 /* Save the identifier. */
4660 identifier
= token
->value
;
4664 /* Parse the next assignment-expression. */
4667 bool expr_non_constant_p
;
4668 expr
= (cp_parser_constant_expression
4669 (parser
, /*allow_non_constant_p=*/true,
4670 &expr_non_constant_p
));
4671 if (expr_non_constant_p
)
4672 *non_constant_p
= true;
4675 expr
= cp_parser_assignment_expression (parser
, cast_p
);
4678 expr
= fold_non_dependent_expr (expr
);
4680 /* Add it to the list. We add error_mark_node
4681 expressions to the list, so that we can still tell if
4682 the correct form for a parenthesized expression-list
4683 is found. That gives better errors. */
4684 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
4686 if (expr
== error_mark_node
)
4690 /* After the first item, attribute lists look the same as
4691 expression lists. */
4692 is_attribute_list
= false;
4695 /* If the next token isn't a `,', then we are done. */
4696 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
4699 /* Otherwise, consume the `,' and keep going. */
4700 cp_lexer_consume_token (parser
->lexer
);
4703 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
4708 /* We try and resync to an unnested comma, as that will give the
4709 user better diagnostics. */
4710 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
4711 /*recovering=*/true,
4713 /*consume_paren=*/true);
4717 return error_mark_node
;
4720 /* We built up the list in reverse order so we must reverse it now. */
4721 expression_list
= nreverse (expression_list
);
4723 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
4725 return expression_list
;
4728 /* Parse a pseudo-destructor-name.
4730 pseudo-destructor-name:
4731 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4732 :: [opt] nested-name-specifier template template-id :: ~ type-name
4733 :: [opt] nested-name-specifier [opt] ~ type-name
4735 If either of the first two productions is used, sets *SCOPE to the
4736 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4737 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4738 or ERROR_MARK_NODE if the parse fails. */
4741 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
4745 bool nested_name_specifier_p
;
4747 /* Assume that things will not work out. */
4748 *type
= error_mark_node
;
4750 /* Look for the optional `::' operator. */
4751 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
4752 /* Look for the optional nested-name-specifier. */
4753 nested_name_specifier_p
4754 = (cp_parser_nested_name_specifier_opt (parser
,
4755 /*typename_keyword_p=*/false,
4756 /*check_dependency_p=*/true,
4758 /*is_declaration=*/true)
4760 /* Now, if we saw a nested-name-specifier, we might be doing the
4761 second production. */
4762 if (nested_name_specifier_p
4763 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
4765 /* Consume the `template' keyword. */
4766 cp_lexer_consume_token (parser
->lexer
);
4767 /* Parse the template-id. */
4768 cp_parser_template_id (parser
,
4769 /*template_keyword_p=*/true,
4770 /*check_dependency_p=*/false,
4771 /*is_declaration=*/true);
4772 /* Look for the `::' token. */
4773 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4775 /* If the next token is not a `~', then there might be some
4776 additional qualification. */
4777 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
4779 /* Look for the type-name. */
4780 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
4782 if (*scope
== error_mark_node
)
4785 /* If we don't have ::~, then something has gone wrong. Since
4786 the only caller of this function is looking for something
4787 after `.' or `->' after a scalar type, most likely the
4788 program is trying to get a member of a non-aggregate
4790 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
)
4791 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_COMPL
)
4793 cp_parser_error (parser
, "request for member of non-aggregate type");
4797 /* Look for the `::' token. */
4798 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4803 /* Look for the `~'. */
4804 cp_parser_require (parser
, CPP_COMPL
, "`~'");
4805 /* Look for the type-name again. We are not responsible for
4806 checking that it matches the first type-name. */
4807 *type
= cp_parser_type_name (parser
);
4810 /* Parse a unary-expression.
4816 unary-operator cast-expression
4817 sizeof unary-expression
4825 __extension__ cast-expression
4826 __alignof__ unary-expression
4827 __alignof__ ( type-id )
4828 __real__ cast-expression
4829 __imag__ cast-expression
4832 ADDRESS_P is true iff the unary-expression is appearing as the
4833 operand of the `&' operator. CAST_P is true if this expression is
4834 the target of a cast.
4836 Returns a representation of the expression. */
4839 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
4842 enum tree_code unary_operator
;
4844 /* Peek at the next token. */
4845 token
= cp_lexer_peek_token (parser
->lexer
);
4846 /* Some keywords give away the kind of expression. */
4847 if (token
->type
== CPP_KEYWORD
)
4849 enum rid keyword
= token
->keyword
;
4859 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
4860 /* Consume the token. */
4861 cp_lexer_consume_token (parser
->lexer
);
4862 /* Parse the operand. */
4863 operand
= cp_parser_sizeof_operand (parser
, keyword
);
4865 if (TYPE_P (operand
))
4866 return cxx_sizeof_or_alignof_type (operand
, op
, true);
4868 return cxx_sizeof_or_alignof_expr (operand
, op
);
4872 return cp_parser_new_expression (parser
);
4875 return cp_parser_delete_expression (parser
);
4879 /* The saved value of the PEDANTIC flag. */
4883 /* Save away the PEDANTIC flag. */
4884 cp_parser_extension_opt (parser
, &saved_pedantic
);
4885 /* Parse the cast-expression. */
4886 expr
= cp_parser_simple_cast_expression (parser
);
4887 /* Restore the PEDANTIC flag. */
4888 pedantic
= saved_pedantic
;
4898 /* Consume the `__real__' or `__imag__' token. */
4899 cp_lexer_consume_token (parser
->lexer
);
4900 /* Parse the cast-expression. */
4901 expression
= cp_parser_simple_cast_expression (parser
);
4902 /* Create the complete representation. */
4903 return build_x_unary_op ((keyword
== RID_REALPART
4904 ? REALPART_EXPR
: IMAGPART_EXPR
),
4914 /* Look for the `:: new' and `:: delete', which also signal the
4915 beginning of a new-expression, or delete-expression,
4916 respectively. If the next token is `::', then it might be one of
4918 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
4922 /* See if the token after the `::' is one of the keywords in
4923 which we're interested. */
4924 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
4925 /* If it's `new', we have a new-expression. */
4926 if (keyword
== RID_NEW
)
4927 return cp_parser_new_expression (parser
);
4928 /* Similarly, for `delete'. */
4929 else if (keyword
== RID_DELETE
)
4930 return cp_parser_delete_expression (parser
);
4933 /* Look for a unary operator. */
4934 unary_operator
= cp_parser_unary_operator (token
);
4935 /* The `++' and `--' operators can be handled similarly, even though
4936 they are not technically unary-operators in the grammar. */
4937 if (unary_operator
== ERROR_MARK
)
4939 if (token
->type
== CPP_PLUS_PLUS
)
4940 unary_operator
= PREINCREMENT_EXPR
;
4941 else if (token
->type
== CPP_MINUS_MINUS
)
4942 unary_operator
= PREDECREMENT_EXPR
;
4943 /* Handle the GNU address-of-label extension. */
4944 else if (cp_parser_allow_gnu_extensions_p (parser
)
4945 && token
->type
== CPP_AND_AND
)
4949 /* Consume the '&&' token. */
4950 cp_lexer_consume_token (parser
->lexer
);
4951 /* Look for the identifier. */
4952 identifier
= cp_parser_identifier (parser
);
4953 /* Create an expression representing the address. */
4954 return finish_label_address_expr (identifier
);
4957 if (unary_operator
!= ERROR_MARK
)
4959 tree cast_expression
;
4960 tree expression
= error_mark_node
;
4961 const char *non_constant_p
= NULL
;
4963 /* Consume the operator token. */
4964 token
= cp_lexer_consume_token (parser
->lexer
);
4965 /* Parse the cast-expression. */
4967 = cp_parser_cast_expression (parser
,
4968 unary_operator
== ADDR_EXPR
,
4970 /* Now, build an appropriate representation. */
4971 switch (unary_operator
)
4974 non_constant_p
= "`*'";
4975 expression
= build_x_indirect_ref (cast_expression
, "unary *");
4979 non_constant_p
= "`&'";
4982 expression
= build_x_unary_op (unary_operator
, cast_expression
);
4985 case PREINCREMENT_EXPR
:
4986 case PREDECREMENT_EXPR
:
4987 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
4990 case UNARY_PLUS_EXPR
:
4992 case TRUTH_NOT_EXPR
:
4993 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
5001 && cp_parser_non_integral_constant_expression (parser
,
5003 expression
= error_mark_node
;
5008 return cp_parser_postfix_expression (parser
, address_p
, cast_p
);
5011 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5012 unary-operator, the corresponding tree code is returned. */
5014 static enum tree_code
5015 cp_parser_unary_operator (cp_token
* token
)
5017 switch (token
->type
)
5020 return INDIRECT_REF
;
5026 return UNARY_PLUS_EXPR
;
5032 return TRUTH_NOT_EXPR
;
5035 return BIT_NOT_EXPR
;
5042 /* Parse a new-expression.
5045 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5046 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5048 Returns a representation of the expression. */
5051 cp_parser_new_expression (cp_parser
* parser
)
5053 bool global_scope_p
;
5059 /* Look for the optional `::' operator. */
5061 = (cp_parser_global_scope_opt (parser
,
5062 /*current_scope_valid_p=*/false)
5064 /* Look for the `new' operator. */
5065 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
5066 /* There's no easy way to tell a new-placement from the
5067 `( type-id )' construct. */
5068 cp_parser_parse_tentatively (parser
);
5069 /* Look for a new-placement. */
5070 placement
= cp_parser_new_placement (parser
);
5071 /* If that didn't work out, there's no new-placement. */
5072 if (!cp_parser_parse_definitely (parser
))
5073 placement
= NULL_TREE
;
5075 /* If the next token is a `(', then we have a parenthesized
5077 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5079 /* Consume the `('. */
5080 cp_lexer_consume_token (parser
->lexer
);
5081 /* Parse the type-id. */
5082 type
= cp_parser_type_id (parser
);
5083 /* Look for the closing `)'. */
5084 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5085 /* There should not be a direct-new-declarator in this production,
5086 but GCC used to allowed this, so we check and emit a sensible error
5087 message for this case. */
5088 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5090 error ("array bound forbidden after parenthesized type-id");
5091 inform ("try removing the parentheses around the type-id");
5092 cp_parser_direct_new_declarator (parser
);
5096 /* Otherwise, there must be a new-type-id. */
5098 type
= cp_parser_new_type_id (parser
, &nelts
);
5100 /* If the next token is a `(', then we have a new-initializer. */
5101 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5102 initializer
= cp_parser_new_initializer (parser
);
5104 initializer
= NULL_TREE
;
5106 /* A new-expression may not appear in an integral constant
5108 if (cp_parser_non_integral_constant_expression (parser
, "`new'"))
5109 return error_mark_node
;
5111 /* Create a representation of the new-expression. */
5112 return build_new (placement
, type
, nelts
, initializer
, global_scope_p
);
5115 /* Parse a new-placement.
5120 Returns the same representation as for an expression-list. */
5123 cp_parser_new_placement (cp_parser
* parser
)
5125 tree expression_list
;
5127 /* Parse the expression-list. */
5128 expression_list
= (cp_parser_parenthesized_expression_list
5129 (parser
, false, /*cast_p=*/false,
5130 /*non_constant_p=*/NULL
));
5132 return expression_list
;
5135 /* Parse a new-type-id.
5138 type-specifier-seq new-declarator [opt]
5140 Returns the TYPE allocated. If the new-type-id indicates an array
5141 type, *NELTS is set to the number of elements in the last array
5142 bound; the TYPE will not include the last array bound. */
5145 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
5147 cp_decl_specifier_seq type_specifier_seq
;
5148 cp_declarator
*new_declarator
;
5149 cp_declarator
*declarator
;
5150 cp_declarator
*outer_declarator
;
5151 const char *saved_message
;
5154 /* The type-specifier sequence must not contain type definitions.
5155 (It cannot contain declarations of new types either, but if they
5156 are not definitions we will catch that because they are not
5158 saved_message
= parser
->type_definition_forbidden_message
;
5159 parser
->type_definition_forbidden_message
5160 = "types may not be defined in a new-type-id";
5161 /* Parse the type-specifier-seq. */
5162 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
5163 &type_specifier_seq
);
5164 /* Restore the old message. */
5165 parser
->type_definition_forbidden_message
= saved_message
;
5166 /* Parse the new-declarator. */
5167 new_declarator
= cp_parser_new_declarator_opt (parser
);
5169 /* Determine the number of elements in the last array dimension, if
5172 /* Skip down to the last array dimension. */
5173 declarator
= new_declarator
;
5174 outer_declarator
= NULL
;
5175 while (declarator
&& (declarator
->kind
== cdk_pointer
5176 || declarator
->kind
== cdk_ptrmem
))
5178 outer_declarator
= declarator
;
5179 declarator
= declarator
->declarator
;
5182 && declarator
->kind
== cdk_array
5183 && declarator
->declarator
5184 && declarator
->declarator
->kind
== cdk_array
)
5186 outer_declarator
= declarator
;
5187 declarator
= declarator
->declarator
;
5190 if (declarator
&& declarator
->kind
== cdk_array
)
5192 *nelts
= declarator
->u
.array
.bounds
;
5193 if (*nelts
== error_mark_node
)
5194 *nelts
= integer_one_node
;
5196 if (outer_declarator
)
5197 outer_declarator
->declarator
= declarator
->declarator
;
5199 new_declarator
= NULL
;
5202 type
= groktypename (&type_specifier_seq
, new_declarator
);
5203 if (TREE_CODE (type
) == ARRAY_TYPE
&& *nelts
== NULL_TREE
)
5205 *nelts
= array_type_nelts_top (type
);
5206 type
= TREE_TYPE (type
);
5211 /* Parse an (optional) new-declarator.
5214 ptr-operator new-declarator [opt]
5215 direct-new-declarator
5217 Returns the declarator. */
5219 static cp_declarator
*
5220 cp_parser_new_declarator_opt (cp_parser
* parser
)
5222 enum tree_code code
;
5224 cp_cv_quals cv_quals
;
5226 /* We don't know if there's a ptr-operator next, or not. */
5227 cp_parser_parse_tentatively (parser
);
5228 /* Look for a ptr-operator. */
5229 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
5230 /* If that worked, look for more new-declarators. */
5231 if (cp_parser_parse_definitely (parser
))
5233 cp_declarator
*declarator
;
5235 /* Parse another optional declarator. */
5236 declarator
= cp_parser_new_declarator_opt (parser
);
5238 /* Create the representation of the declarator. */
5240 declarator
= make_ptrmem_declarator (cv_quals
, type
, declarator
);
5241 else if (code
== INDIRECT_REF
)
5242 declarator
= make_pointer_declarator (cv_quals
, declarator
);
5244 declarator
= make_reference_declarator (cv_quals
, declarator
);
5249 /* If the next token is a `[', there is a direct-new-declarator. */
5250 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5251 return cp_parser_direct_new_declarator (parser
);
5256 /* Parse a direct-new-declarator.
5258 direct-new-declarator:
5260 direct-new-declarator [constant-expression]
5264 static cp_declarator
*
5265 cp_parser_direct_new_declarator (cp_parser
* parser
)
5267 cp_declarator
*declarator
= NULL
;
5273 /* Look for the opening `['. */
5274 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
5275 /* The first expression is not required to be constant. */
5278 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
5279 /* The standard requires that the expression have integral
5280 type. DR 74 adds enumeration types. We believe that the
5281 real intent is that these expressions be handled like the
5282 expression in a `switch' condition, which also allows
5283 classes with a single conversion to integral or
5284 enumeration type. */
5285 if (!processing_template_decl
)
5288 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
5293 error ("expression in new-declarator must have integral "
5294 "or enumeration type");
5295 expression
= error_mark_node
;
5299 /* But all the other expressions must be. */
5302 = cp_parser_constant_expression (parser
,
5303 /*allow_non_constant=*/false,
5305 /* Look for the closing `]'. */
5306 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5308 /* Add this bound to the declarator. */
5309 declarator
= make_array_declarator (declarator
, expression
);
5311 /* If the next token is not a `[', then there are no more
5313 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
5320 /* Parse a new-initializer.
5323 ( expression-list [opt] )
5325 Returns a representation of the expression-list. If there is no
5326 expression-list, VOID_ZERO_NODE is returned. */
5329 cp_parser_new_initializer (cp_parser
* parser
)
5331 tree expression_list
;
5333 expression_list
= (cp_parser_parenthesized_expression_list
5334 (parser
, false, /*cast_p=*/false,
5335 /*non_constant_p=*/NULL
));
5336 if (!expression_list
)
5337 expression_list
= void_zero_node
;
5339 return expression_list
;
5342 /* Parse a delete-expression.
5345 :: [opt] delete cast-expression
5346 :: [opt] delete [ ] cast-expression
5348 Returns a representation of the expression. */
5351 cp_parser_delete_expression (cp_parser
* parser
)
5353 bool global_scope_p
;
5357 /* Look for the optional `::' operator. */
5359 = (cp_parser_global_scope_opt (parser
,
5360 /*current_scope_valid_p=*/false)
5362 /* Look for the `delete' keyword. */
5363 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
5364 /* See if the array syntax is in use. */
5365 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5367 /* Consume the `[' token. */
5368 cp_lexer_consume_token (parser
->lexer
);
5369 /* Look for the `]' token. */
5370 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5371 /* Remember that this is the `[]' construct. */
5377 /* Parse the cast-expression. */
5378 expression
= cp_parser_simple_cast_expression (parser
);
5380 /* A delete-expression may not appear in an integral constant
5382 if (cp_parser_non_integral_constant_expression (parser
, "`delete'"))
5383 return error_mark_node
;
5385 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
5388 /* Parse a cast-expression.
5392 ( type-id ) cast-expression
5394 ADDRESS_P is true iff the unary-expression is appearing as the
5395 operand of the `&' operator. CAST_P is true if this expression is
5396 the target of a cast.
5398 Returns a representation of the expression. */
5401 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
5403 /* If it's a `(', then we might be looking at a cast. */
5404 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5406 tree type
= NULL_TREE
;
5407 tree expr
= NULL_TREE
;
5408 bool compound_literal_p
;
5409 const char *saved_message
;
5411 /* There's no way to know yet whether or not this is a cast.
5412 For example, `(int (3))' is a unary-expression, while `(int)
5413 3' is a cast. So, we resort to parsing tentatively. */
5414 cp_parser_parse_tentatively (parser
);
5415 /* Types may not be defined in a cast. */
5416 saved_message
= parser
->type_definition_forbidden_message
;
5417 parser
->type_definition_forbidden_message
5418 = "types may not be defined in casts";
5419 /* Consume the `('. */
5420 cp_lexer_consume_token (parser
->lexer
);
5421 /* A very tricky bit is that `(struct S) { 3 }' is a
5422 compound-literal (which we permit in C++ as an extension).
5423 But, that construct is not a cast-expression -- it is a
5424 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5425 is legal; if the compound-literal were a cast-expression,
5426 you'd need an extra set of parentheses.) But, if we parse
5427 the type-id, and it happens to be a class-specifier, then we
5428 will commit to the parse at that point, because we cannot
5429 undo the action that is done when creating a new class. So,
5430 then we cannot back up and do a postfix-expression.
5432 Therefore, we scan ahead to the closing `)', and check to see
5433 if the token after the `)' is a `{'. If so, we are not
5434 looking at a cast-expression.
5436 Save tokens so that we can put them back. */
5437 cp_lexer_save_tokens (parser
->lexer
);
5438 /* Skip tokens until the next token is a closing parenthesis.
5439 If we find the closing `)', and the next token is a `{', then
5440 we are looking at a compound-literal. */
5442 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5443 /*consume_paren=*/true)
5444 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5445 /* Roll back the tokens we skipped. */
5446 cp_lexer_rollback_tokens (parser
->lexer
);
5447 /* If we were looking at a compound-literal, simulate an error
5448 so that the call to cp_parser_parse_definitely below will
5450 if (compound_literal_p
)
5451 cp_parser_simulate_error (parser
);
5454 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5455 parser
->in_type_id_in_expr_p
= true;
5456 /* Look for the type-id. */
5457 type
= cp_parser_type_id (parser
);
5458 /* Look for the closing `)'. */
5459 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5460 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5463 /* Restore the saved message. */
5464 parser
->type_definition_forbidden_message
= saved_message
;
5466 /* If ok so far, parse the dependent expression. We cannot be
5467 sure it is a cast. Consider `(T ())'. It is a parenthesized
5468 ctor of T, but looks like a cast to function returning T
5469 without a dependent expression. */
5470 if (!cp_parser_error_occurred (parser
))
5471 expr
= cp_parser_cast_expression (parser
,
5472 /*address_p=*/false,
5475 if (cp_parser_parse_definitely (parser
))
5477 /* Warn about old-style casts, if so requested. */
5478 if (warn_old_style_cast
5479 && !in_system_header
5480 && !VOID_TYPE_P (type
)
5481 && current_lang_name
!= lang_name_c
)
5482 warning (OPT_Wold_style_cast
, "use of old-style cast");
5484 /* Only type conversions to integral or enumeration types
5485 can be used in constant-expressions. */
5486 if (parser
->integral_constant_expression_p
5487 && !dependent_type_p (type
)
5488 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
5489 && (cp_parser_non_integral_constant_expression
5491 "a cast to a type other than an integral or "
5492 "enumeration type")))
5493 return error_mark_node
;
5495 /* Perform the cast. */
5496 expr
= build_c_cast (type
, expr
);
5501 /* If we get here, then it's not a cast, so it must be a
5502 unary-expression. */
5503 return cp_parser_unary_expression (parser
, address_p
, cast_p
);
5506 /* Parse a binary expression of the general form:
5510 pm-expression .* cast-expression
5511 pm-expression ->* cast-expression
5513 multiplicative-expression:
5515 multiplicative-expression * pm-expression
5516 multiplicative-expression / pm-expression
5517 multiplicative-expression % pm-expression
5519 additive-expression:
5520 multiplicative-expression
5521 additive-expression + multiplicative-expression
5522 additive-expression - multiplicative-expression
5526 shift-expression << additive-expression
5527 shift-expression >> additive-expression
5529 relational-expression:
5531 relational-expression < shift-expression
5532 relational-expression > shift-expression
5533 relational-expression <= shift-expression
5534 relational-expression >= shift-expression
5538 relational-expression:
5539 relational-expression <? shift-expression
5540 relational-expression >? shift-expression
5542 equality-expression:
5543 relational-expression
5544 equality-expression == relational-expression
5545 equality-expression != relational-expression
5549 and-expression & equality-expression
5551 exclusive-or-expression:
5553 exclusive-or-expression ^ and-expression
5555 inclusive-or-expression:
5556 exclusive-or-expression
5557 inclusive-or-expression | exclusive-or-expression
5559 logical-and-expression:
5560 inclusive-or-expression
5561 logical-and-expression && inclusive-or-expression
5563 logical-or-expression:
5564 logical-and-expression
5565 logical-or-expression || logical-and-expression
5567 All these are implemented with a single function like:
5570 simple-cast-expression
5571 binary-expression <token> binary-expression
5573 CAST_P is true if this expression is the target of a cast.
5575 The binops_by_token map is used to get the tree codes for each <token> type.
5576 binary-expressions are associated according to a precedence table. */
5578 #define TOKEN_PRECEDENCE(token) \
5579 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5580 ? PREC_NOT_OPERATOR \
5581 : binops_by_token[token->type].prec)
5584 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
)
5586 cp_parser_expression_stack stack
;
5587 cp_parser_expression_stack_entry
*sp
= &stack
[0];
5590 enum tree_code tree_type
;
5591 enum cp_parser_prec prec
= PREC_NOT_OPERATOR
, new_prec
, lookahead_prec
;
5594 /* Parse the first expression. */
5595 lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false, cast_p
);
5599 /* Get an operator token. */
5600 token
= cp_lexer_peek_token (parser
->lexer
);
5601 if (token
->type
== CPP_MIN
|| token
->type
== CPP_MAX
)
5602 cp_parser_warn_min_max ();
5604 new_prec
= TOKEN_PRECEDENCE (token
);
5606 /* Popping an entry off the stack means we completed a subexpression:
5607 - either we found a token which is not an operator (`>' where it is not
5608 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5609 will happen repeatedly;
5610 - or, we found an operator which has lower priority. This is the case
5611 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5613 if (new_prec
<= prec
)
5622 tree_type
= binops_by_token
[token
->type
].tree_type
;
5624 /* We used the operator token. */
5625 cp_lexer_consume_token (parser
->lexer
);
5627 /* Extract another operand. It may be the RHS of this expression
5628 or the LHS of a new, higher priority expression. */
5629 rhs
= cp_parser_simple_cast_expression (parser
);
5631 /* Get another operator token. Look up its precedence to avoid
5632 building a useless (immediately popped) stack entry for common
5633 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5634 token
= cp_lexer_peek_token (parser
->lexer
);
5635 lookahead_prec
= TOKEN_PRECEDENCE (token
);
5636 if (lookahead_prec
> new_prec
)
5638 /* ... and prepare to parse the RHS of the new, higher priority
5639 expression. Since precedence levels on the stack are
5640 monotonically increasing, we do not have to care about
5643 sp
->tree_type
= tree_type
;
5648 new_prec
= lookahead_prec
;
5652 /* If the stack is not empty, we have parsed into LHS the right side
5653 (`4' in the example above) of an expression we had suspended.
5654 We can use the information on the stack to recover the LHS (`3')
5655 from the stack together with the tree code (`MULT_EXPR'), and
5656 the precedence of the higher level subexpression
5657 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5658 which will be used to actually build the additive expression. */
5661 tree_type
= sp
->tree_type
;
5666 overloaded_p
= false;
5667 lhs
= build_x_binary_op (tree_type
, lhs
, rhs
, &overloaded_p
);
5669 /* If the binary operator required the use of an overloaded operator,
5670 then this expression cannot be an integral constant-expression.
5671 An overloaded operator can be used even if both operands are
5672 otherwise permissible in an integral constant-expression if at
5673 least one of the operands is of enumeration type. */
5676 && (cp_parser_non_integral_constant_expression
5677 (parser
, "calls to overloaded operators")))
5678 return error_mark_node
;
5685 /* Parse the `? expression : assignment-expression' part of a
5686 conditional-expression. The LOGICAL_OR_EXPR is the
5687 logical-or-expression that started the conditional-expression.
5688 Returns a representation of the entire conditional-expression.
5690 This routine is used by cp_parser_assignment_expression.
5692 ? expression : assignment-expression
5696 ? : assignment-expression */
5699 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
5702 tree assignment_expr
;
5704 /* Consume the `?' token. */
5705 cp_lexer_consume_token (parser
->lexer
);
5706 if (cp_parser_allow_gnu_extensions_p (parser
)
5707 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
5708 /* Implicit true clause. */
5711 /* Parse the expression. */
5712 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
5714 /* The next token should be a `:'. */
5715 cp_parser_require (parser
, CPP_COLON
, "`:'");
5716 /* Parse the assignment-expression. */
5717 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
5719 /* Build the conditional-expression. */
5720 return build_x_conditional_expr (logical_or_expr
,
5725 /* Parse an assignment-expression.
5727 assignment-expression:
5728 conditional-expression
5729 logical-or-expression assignment-operator assignment_expression
5732 CAST_P is true if this expression is the target of a cast.
5734 Returns a representation for the expression. */
5737 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
)
5741 /* If the next token is the `throw' keyword, then we're looking at
5742 a throw-expression. */
5743 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
5744 expr
= cp_parser_throw_expression (parser
);
5745 /* Otherwise, it must be that we are looking at a
5746 logical-or-expression. */
5749 /* Parse the binary expressions (logical-or-expression). */
5750 expr
= cp_parser_binary_expression (parser
, cast_p
);
5751 /* If the next token is a `?' then we're actually looking at a
5752 conditional-expression. */
5753 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5754 return cp_parser_question_colon_clause (parser
, expr
);
5757 enum tree_code assignment_operator
;
5759 /* If it's an assignment-operator, we're using the second
5762 = cp_parser_assignment_operator_opt (parser
);
5763 if (assignment_operator
!= ERROR_MARK
)
5767 /* Parse the right-hand side of the assignment. */
5768 rhs
= cp_parser_assignment_expression (parser
, cast_p
);
5769 /* An assignment may not appear in a
5770 constant-expression. */
5771 if (cp_parser_non_integral_constant_expression (parser
,
5773 return error_mark_node
;
5774 /* Build the assignment expression. */
5775 expr
= build_x_modify_expr (expr
,
5776 assignment_operator
,
5785 /* Parse an (optional) assignment-operator.
5787 assignment-operator: one of
5788 = *= /= %= += -= >>= <<= &= ^= |=
5792 assignment-operator: one of
5795 If the next token is an assignment operator, the corresponding tree
5796 code is returned, and the token is consumed. For example, for
5797 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5798 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5799 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5800 operator, ERROR_MARK is returned. */
5802 static enum tree_code
5803 cp_parser_assignment_operator_opt (cp_parser
* parser
)
5808 /* Peek at the next toen. */
5809 token
= cp_lexer_peek_token (parser
->lexer
);
5811 switch (token
->type
)
5822 op
= TRUNC_DIV_EXPR
;
5826 op
= TRUNC_MOD_EXPR
;
5859 cp_parser_warn_min_max ();
5864 cp_parser_warn_min_max ();
5868 /* Nothing else is an assignment operator. */
5872 /* If it was an assignment operator, consume it. */
5873 if (op
!= ERROR_MARK
)
5874 cp_lexer_consume_token (parser
->lexer
);
5879 /* Parse an expression.
5882 assignment-expression
5883 expression , assignment-expression
5885 CAST_P is true if this expression is the target of a cast.
5887 Returns a representation of the expression. */
5890 cp_parser_expression (cp_parser
* parser
, bool cast_p
)
5892 tree expression
= NULL_TREE
;
5896 tree assignment_expression
;
5898 /* Parse the next assignment-expression. */
5899 assignment_expression
5900 = cp_parser_assignment_expression (parser
, cast_p
);
5901 /* If this is the first assignment-expression, we can just
5904 expression
= assignment_expression
;
5906 expression
= build_x_compound_expr (expression
,
5907 assignment_expression
);
5908 /* If the next token is not a comma, then we are done with the
5910 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5912 /* Consume the `,'. */
5913 cp_lexer_consume_token (parser
->lexer
);
5914 /* A comma operator cannot appear in a constant-expression. */
5915 if (cp_parser_non_integral_constant_expression (parser
,
5916 "a comma operator"))
5917 expression
= error_mark_node
;
5923 /* Parse a constant-expression.
5925 constant-expression:
5926 conditional-expression
5928 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5929 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5930 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5931 is false, NON_CONSTANT_P should be NULL. */
5934 cp_parser_constant_expression (cp_parser
* parser
,
5935 bool allow_non_constant_p
,
5936 bool *non_constant_p
)
5938 bool saved_integral_constant_expression_p
;
5939 bool saved_allow_non_integral_constant_expression_p
;
5940 bool saved_non_integral_constant_expression_p
;
5943 /* It might seem that we could simply parse the
5944 conditional-expression, and then check to see if it were
5945 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5946 one that the compiler can figure out is constant, possibly after
5947 doing some simplifications or optimizations. The standard has a
5948 precise definition of constant-expression, and we must honor
5949 that, even though it is somewhat more restrictive.
5955 is not a legal declaration, because `(2, 3)' is not a
5956 constant-expression. The `,' operator is forbidden in a
5957 constant-expression. However, GCC's constant-folding machinery
5958 will fold this operation to an INTEGER_CST for `3'. */
5960 /* Save the old settings. */
5961 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
5962 saved_allow_non_integral_constant_expression_p
5963 = parser
->allow_non_integral_constant_expression_p
;
5964 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
5965 /* We are now parsing a constant-expression. */
5966 parser
->integral_constant_expression_p
= true;
5967 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
5968 parser
->non_integral_constant_expression_p
= false;
5969 /* Although the grammar says "conditional-expression", we parse an
5970 "assignment-expression", which also permits "throw-expression"
5971 and the use of assignment operators. In the case that
5972 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5973 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5974 actually essential that we look for an assignment-expression.
5975 For example, cp_parser_initializer_clauses uses this function to
5976 determine whether a particular assignment-expression is in fact
5978 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
5979 /* Restore the old settings. */
5980 parser
->integral_constant_expression_p
5981 = saved_integral_constant_expression_p
;
5982 parser
->allow_non_integral_constant_expression_p
5983 = saved_allow_non_integral_constant_expression_p
;
5984 if (allow_non_constant_p
)
5985 *non_constant_p
= parser
->non_integral_constant_expression_p
;
5986 else if (parser
->non_integral_constant_expression_p
)
5987 expression
= error_mark_node
;
5988 parser
->non_integral_constant_expression_p
5989 = saved_non_integral_constant_expression_p
;
5994 /* Parse __builtin_offsetof.
5996 offsetof-expression:
5997 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5999 offsetof-member-designator:
6001 | offsetof-member-designator "." id-expression
6002 | offsetof-member-designator "[" expression "]" */
6005 cp_parser_builtin_offsetof (cp_parser
*parser
)
6007 int save_ice_p
, save_non_ice_p
;
6011 /* We're about to accept non-integral-constant things, but will
6012 definitely yield an integral constant expression. Save and
6013 restore these values around our local parsing. */
6014 save_ice_p
= parser
->integral_constant_expression_p
;
6015 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
6017 /* Consume the "__builtin_offsetof" token. */
6018 cp_lexer_consume_token (parser
->lexer
);
6019 /* Consume the opening `('. */
6020 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6021 /* Parse the type-id. */
6022 type
= cp_parser_type_id (parser
);
6023 /* Look for the `,'. */
6024 cp_parser_require (parser
, CPP_COMMA
, "`,'");
6026 /* Build the (type *)null that begins the traditional offsetof macro. */
6027 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
);
6029 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6030 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
6034 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6035 switch (token
->type
)
6037 case CPP_OPEN_SQUARE
:
6038 /* offsetof-member-designator "[" expression "]" */
6039 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
6043 /* offsetof-member-designator "." identifier */
6044 cp_lexer_consume_token (parser
->lexer
);
6045 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
, expr
,
6049 case CPP_CLOSE_PAREN
:
6050 /* Consume the ")" token. */
6051 cp_lexer_consume_token (parser
->lexer
);
6055 /* Error. We know the following require will fail, but
6056 that gives the proper error message. */
6057 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6058 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
6059 expr
= error_mark_node
;
6065 /* If we're processing a template, we can't finish the semantics yet.
6066 Otherwise we can fold the entire expression now. */
6067 if (processing_template_decl
)
6068 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
6070 expr
= finish_offsetof (expr
);
6073 parser
->integral_constant_expression_p
= save_ice_p
;
6074 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
6079 /* Statements [gram.stmt.stmt] */
6081 /* Parse a statement.
6085 expression-statement
6090 declaration-statement
6093 IN_COMPOUND is true when the statement is nested inside a
6094 cp_parser_compound_statement; this matters for certain pragmas. */
6097 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
6102 location_t statement_location
;
6105 /* There is no statement yet. */
6106 statement
= NULL_TREE
;
6107 /* Peek at the next token. */
6108 token
= cp_lexer_peek_token (parser
->lexer
);
6109 /* Remember the location of the first token in the statement. */
6110 statement_location
= token
->location
;
6111 /* If this is a keyword, then that will often determine what kind of
6112 statement we have. */
6113 if (token
->type
== CPP_KEYWORD
)
6115 enum rid keyword
= token
->keyword
;
6121 statement
= cp_parser_labeled_statement (parser
, in_statement_expr
,
6127 statement
= cp_parser_selection_statement (parser
);
6133 statement
= cp_parser_iteration_statement (parser
);
6140 statement
= cp_parser_jump_statement (parser
);
6143 /* Objective-C++ exception-handling constructs. */
6146 case RID_AT_FINALLY
:
6147 case RID_AT_SYNCHRONIZED
:
6149 statement
= cp_parser_objc_statement (parser
);
6153 statement
= cp_parser_try_block (parser
);
6157 /* It might be a keyword like `int' that can start a
6158 declaration-statement. */
6162 else if (token
->type
== CPP_NAME
)
6164 /* If the next token is a `:', then we are looking at a
6165 labeled-statement. */
6166 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6167 if (token
->type
== CPP_COLON
)
6168 statement
= cp_parser_labeled_statement (parser
, in_statement_expr
,
6171 /* Anything that starts with a `{' must be a compound-statement. */
6172 else if (token
->type
== CPP_OPEN_BRACE
)
6173 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6174 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6175 a statement all its own. */
6176 else if (token
->type
== CPP_PRAGMA
)
6178 /* Only certain OpenMP pragmas are attached to statements, and thus
6179 are considered statements themselves. All others are not. In
6180 the context of a compound, accept the pragma as a "statement" and
6181 return so that we can check for a close brace. Otherwise we
6182 require a real statement and must go back and read one. */
6184 cp_parser_pragma (parser
, pragma_compound
);
6185 else if (!cp_parser_pragma (parser
, pragma_stmt
))
6189 else if (token
->type
== CPP_EOF
)
6191 cp_parser_error (parser
, "expected statement");
6195 /* Everything else must be a declaration-statement or an
6196 expression-statement. Try for the declaration-statement
6197 first, unless we are looking at a `;', in which case we know that
6198 we have an expression-statement. */
6201 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6203 cp_parser_parse_tentatively (parser
);
6204 /* Try to parse the declaration-statement. */
6205 cp_parser_declaration_statement (parser
);
6206 /* If that worked, we're done. */
6207 if (cp_parser_parse_definitely (parser
))
6210 /* Look for an expression-statement instead. */
6211 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
6214 /* Set the line number for the statement. */
6215 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
6216 SET_EXPR_LOCATION (statement
, statement_location
);
6219 /* Parse a labeled-statement.
6222 identifier : statement
6223 case constant-expression : statement
6229 case constant-expression ... constant-expression : statement
6231 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6232 For an ordinary label, returns a LABEL_EXPR.
6234 IN_COMPOUND is as for cp_parser_statement: true when we're nested
6235 inside a compound. */
6238 cp_parser_labeled_statement (cp_parser
* parser
, tree in_statement_expr
,
6242 tree statement
= error_mark_node
;
6244 /* The next token should be an identifier. */
6245 token
= cp_lexer_peek_token (parser
->lexer
);
6246 if (token
->type
!= CPP_NAME
6247 && token
->type
!= CPP_KEYWORD
)
6249 cp_parser_error (parser
, "expected labeled-statement");
6250 return error_mark_node
;
6253 switch (token
->keyword
)
6260 /* Consume the `case' token. */
6261 cp_lexer_consume_token (parser
->lexer
);
6262 /* Parse the constant-expression. */
6263 expr
= cp_parser_constant_expression (parser
,
6264 /*allow_non_constant_p=*/false,
6267 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
6268 if (ellipsis
->type
== CPP_ELLIPSIS
)
6270 /* Consume the `...' token. */
6271 cp_lexer_consume_token (parser
->lexer
);
6273 cp_parser_constant_expression (parser
,
6274 /*allow_non_constant_p=*/false,
6276 /* We don't need to emit warnings here, as the common code
6277 will do this for us. */
6280 expr_hi
= NULL_TREE
;
6282 if (parser
->in_switch_statement_p
)
6283 statement
= finish_case_label (expr
, expr_hi
);
6285 error ("case label %qE not within a switch statement", expr
);
6290 /* Consume the `default' token. */
6291 cp_lexer_consume_token (parser
->lexer
);
6293 if (parser
->in_switch_statement_p
)
6294 statement
= finish_case_label (NULL_TREE
, NULL_TREE
);
6296 error ("case label not within a switch statement");
6300 /* Anything else must be an ordinary label. */
6301 statement
= finish_label_stmt (cp_parser_identifier (parser
));
6305 /* Require the `:' token. */
6306 cp_parser_require (parser
, CPP_COLON
, "`:'");
6307 /* Parse the labeled statement. */
6308 cp_parser_statement (parser
, in_statement_expr
, in_compound
);
6310 /* Return the label, in the case of a `case' or `default' label. */
6314 /* Parse an expression-statement.
6316 expression-statement:
6319 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6320 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6321 indicates whether this expression-statement is part of an
6322 expression statement. */
6325 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
6327 tree statement
= NULL_TREE
;
6329 /* If the next token is a ';', then there is no expression
6331 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6332 statement
= cp_parser_expression (parser
, /*cast_p=*/false);
6334 /* Consume the final `;'. */
6335 cp_parser_consume_semicolon_at_end_of_statement (parser
);
6337 if (in_statement_expr
6338 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
6339 /* This is the final expression statement of a statement
6341 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
6343 statement
= finish_expr_stmt (statement
);
6350 /* Parse a compound-statement.
6353 { statement-seq [opt] }
6355 Returns a tree representing the statement. */
6358 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
6363 /* Consume the `{'. */
6364 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
6365 return error_mark_node
;
6366 /* Begin the compound-statement. */
6367 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
6368 /* Parse an (optional) statement-seq. */
6369 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
6370 /* Finish the compound-statement. */
6371 finish_compound_stmt (compound_stmt
);
6372 /* Consume the `}'. */
6373 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6375 return compound_stmt
;
6378 /* Parse an (optional) statement-seq.
6382 statement-seq [opt] statement */
6385 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
6387 /* Scan statements until there aren't any more. */
6390 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6392 /* If we're looking at a `}', then we've run out of statements. */
6393 if (token
->type
== CPP_CLOSE_BRACE
6394 || token
->type
== CPP_EOF
6395 || token
->type
== CPP_PRAGMA_EOL
)
6398 /* Parse the statement. */
6399 cp_parser_statement (parser
, in_statement_expr
, true);
6403 /* Parse a selection-statement.
6405 selection-statement:
6406 if ( condition ) statement
6407 if ( condition ) statement else statement
6408 switch ( condition ) statement
6410 Returns the new IF_STMT or SWITCH_STMT. */
6413 cp_parser_selection_statement (cp_parser
* parser
)
6418 /* Peek at the next token. */
6419 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
6421 /* See what kind of keyword it is. */
6422 keyword
= token
->keyword
;
6431 /* Look for the `('. */
6432 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
6434 cp_parser_skip_to_end_of_statement (parser
);
6435 return error_mark_node
;
6438 /* Begin the selection-statement. */
6439 if (keyword
== RID_IF
)
6440 statement
= begin_if_stmt ();
6442 statement
= begin_switch_stmt ();
6444 /* Parse the condition. */
6445 condition
= cp_parser_condition (parser
);
6446 /* Look for the `)'. */
6447 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
6448 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
6449 /*consume_paren=*/true);
6451 if (keyword
== RID_IF
)
6453 /* Add the condition. */
6454 finish_if_stmt_cond (condition
, statement
);
6456 /* Parse the then-clause. */
6457 cp_parser_implicitly_scoped_statement (parser
);
6458 finish_then_clause (statement
);
6460 /* If the next token is `else', parse the else-clause. */
6461 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
6464 /* Consume the `else' keyword. */
6465 cp_lexer_consume_token (parser
->lexer
);
6466 begin_else_clause (statement
);
6467 /* Parse the else-clause. */
6468 cp_parser_implicitly_scoped_statement (parser
);
6469 finish_else_clause (statement
);
6472 /* Now we're all done with the if-statement. */
6473 finish_if_stmt (statement
);
6477 bool in_switch_statement_p
;
6478 unsigned char in_statement
;
6480 /* Add the condition. */
6481 finish_switch_cond (condition
, statement
);
6483 /* Parse the body of the switch-statement. */
6484 in_switch_statement_p
= parser
->in_switch_statement_p
;
6485 in_statement
= parser
->in_statement
;
6486 parser
->in_switch_statement_p
= true;
6487 parser
->in_statement
|= IN_SWITCH_STMT
;
6488 cp_parser_implicitly_scoped_statement (parser
);
6489 parser
->in_switch_statement_p
= in_switch_statement_p
;
6490 parser
->in_statement
= in_statement
;
6492 /* Now we're all done with the switch-statement. */
6493 finish_switch_stmt (statement
);
6501 cp_parser_error (parser
, "expected selection-statement");
6502 return error_mark_node
;
6506 /* Parse a condition.
6510 type-specifier-seq declarator = assignment-expression
6515 type-specifier-seq declarator asm-specification [opt]
6516 attributes [opt] = assignment-expression
6518 Returns the expression that should be tested. */
6521 cp_parser_condition (cp_parser
* parser
)
6523 cp_decl_specifier_seq type_specifiers
;
6524 const char *saved_message
;
6526 /* Try the declaration first. */
6527 cp_parser_parse_tentatively (parser
);
6528 /* New types are not allowed in the type-specifier-seq for a
6530 saved_message
= parser
->type_definition_forbidden_message
;
6531 parser
->type_definition_forbidden_message
6532 = "types may not be defined in conditions";
6533 /* Parse the type-specifier-seq. */
6534 cp_parser_type_specifier_seq (parser
, /*is_condition==*/true,
6536 /* Restore the saved message. */
6537 parser
->type_definition_forbidden_message
= saved_message
;
6538 /* If all is well, we might be looking at a declaration. */
6539 if (!cp_parser_error_occurred (parser
))
6542 tree asm_specification
;
6544 cp_declarator
*declarator
;
6545 tree initializer
= NULL_TREE
;
6547 /* Parse the declarator. */
6548 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
6549 /*ctor_dtor_or_conv_p=*/NULL
,
6550 /*parenthesized_p=*/NULL
,
6551 /*member_p=*/false);
6552 /* Parse the attributes. */
6553 attributes
= cp_parser_attributes_opt (parser
);
6554 /* Parse the asm-specification. */
6555 asm_specification
= cp_parser_asm_specification_opt (parser
);
6556 /* If the next token is not an `=', then we might still be
6557 looking at an expression. For example:
6561 looks like a decl-specifier-seq and a declarator -- but then
6562 there is no `=', so this is an expression. */
6563 cp_parser_require (parser
, CPP_EQ
, "`='");
6564 /* If we did see an `=', then we are looking at a declaration
6566 if (cp_parser_parse_definitely (parser
))
6569 bool non_constant_p
;
6571 /* Create the declaration. */
6572 decl
= start_decl (declarator
, &type_specifiers
,
6573 /*initialized_p=*/true,
6574 attributes
, /*prefix_attributes=*/NULL_TREE
,
6576 /* Parse the assignment-expression. */
6578 = cp_parser_constant_expression (parser
,
6579 /*allow_non_constant_p=*/true,
6581 if (!non_constant_p
)
6582 initializer
= fold_non_dependent_expr (initializer
);
6584 /* Process the initializer. */
6585 cp_finish_decl (decl
,
6586 initializer
, !non_constant_p
,
6588 LOOKUP_ONLYCONVERTING
);
6591 pop_scope (pushed_scope
);
6593 return convert_from_reference (decl
);
6596 /* If we didn't even get past the declarator successfully, we are
6597 definitely not looking at a declaration. */
6599 cp_parser_abort_tentative_parse (parser
);
6601 /* Otherwise, we are looking at an expression. */
6602 return cp_parser_expression (parser
, /*cast_p=*/false);
6605 /* Parse an iteration-statement.
6607 iteration-statement:
6608 while ( condition ) statement
6609 do statement while ( expression ) ;
6610 for ( for-init-statement condition [opt] ; expression [opt] )
6613 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6616 cp_parser_iteration_statement (cp_parser
* parser
)
6621 unsigned char in_statement
;
6623 /* Peek at the next token. */
6624 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
6626 return error_mark_node
;
6628 /* Remember whether or not we are already within an iteration
6630 in_statement
= parser
->in_statement
;
6632 /* See what kind of keyword it is. */
6633 keyword
= token
->keyword
;
6640 /* Begin the while-statement. */
6641 statement
= begin_while_stmt ();
6642 /* Look for the `('. */
6643 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6644 /* Parse the condition. */
6645 condition
= cp_parser_condition (parser
);
6646 finish_while_stmt_cond (condition
, statement
);
6647 /* Look for the `)'. */
6648 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6649 /* Parse the dependent statement. */
6650 parser
->in_statement
= IN_ITERATION_STMT
;
6651 cp_parser_already_scoped_statement (parser
);
6652 parser
->in_statement
= in_statement
;
6653 /* We're done with the while-statement. */
6654 finish_while_stmt (statement
);
6662 /* Begin the do-statement. */
6663 statement
= begin_do_stmt ();
6664 /* Parse the body of the do-statement. */
6665 parser
->in_statement
= IN_ITERATION_STMT
;
6666 cp_parser_implicitly_scoped_statement (parser
);
6667 parser
->in_statement
= in_statement
;
6668 finish_do_body (statement
);
6669 /* Look for the `while' keyword. */
6670 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
6671 /* Look for the `('. */
6672 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6673 /* Parse the expression. */
6674 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6675 /* We're done with the do-statement. */
6676 finish_do_stmt (expression
, statement
);
6677 /* Look for the `)'. */
6678 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6679 /* Look for the `;'. */
6680 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6686 tree condition
= NULL_TREE
;
6687 tree expression
= NULL_TREE
;
6689 /* Begin the for-statement. */
6690 statement
= begin_for_stmt ();
6691 /* Look for the `('. */
6692 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6693 /* Parse the initialization. */
6694 cp_parser_for_init_statement (parser
);
6695 finish_for_init_stmt (statement
);
6697 /* If there's a condition, process it. */
6698 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6699 condition
= cp_parser_condition (parser
);
6700 finish_for_cond (condition
, statement
);
6701 /* Look for the `;'. */
6702 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6704 /* If there's an expression, process it. */
6705 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6706 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6707 finish_for_expr (expression
, statement
);
6708 /* Look for the `)'. */
6709 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6711 /* Parse the body of the for-statement. */
6712 parser
->in_statement
= IN_ITERATION_STMT
;
6713 cp_parser_already_scoped_statement (parser
);
6714 parser
->in_statement
= in_statement
;
6716 /* We're done with the for-statement. */
6717 finish_for_stmt (statement
);
6722 cp_parser_error (parser
, "expected iteration-statement");
6723 statement
= error_mark_node
;
6730 /* Parse a for-init-statement.
6733 expression-statement
6734 simple-declaration */
6737 cp_parser_for_init_statement (cp_parser
* parser
)
6739 /* If the next token is a `;', then we have an empty
6740 expression-statement. Grammatically, this is also a
6741 simple-declaration, but an invalid one, because it does not
6742 declare anything. Therefore, if we did not handle this case
6743 specially, we would issue an error message about an invalid
6745 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6747 /* We're going to speculatively look for a declaration, falling back
6748 to an expression, if necessary. */
6749 cp_parser_parse_tentatively (parser
);
6750 /* Parse the declaration. */
6751 cp_parser_simple_declaration (parser
,
6752 /*function_definition_allowed_p=*/false);
6753 /* If the tentative parse failed, then we shall need to look for an
6754 expression-statement. */
6755 if (cp_parser_parse_definitely (parser
))
6759 cp_parser_expression_statement (parser
, false);
6762 /* Parse a jump-statement.
6767 return expression [opt] ;
6775 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6778 cp_parser_jump_statement (cp_parser
* parser
)
6780 tree statement
= error_mark_node
;
6784 /* Peek at the next token. */
6785 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
6787 return error_mark_node
;
6789 /* See what kind of keyword it is. */
6790 keyword
= token
->keyword
;
6794 switch (parser
->in_statement
)
6797 error ("break statement not within loop or switch");
6800 gcc_assert ((parser
->in_statement
& IN_SWITCH_STMT
)
6801 || parser
->in_statement
== IN_ITERATION_STMT
);
6802 statement
= finish_break_stmt ();
6805 error ("invalid exit from OpenMP structured block");
6808 error ("break statement used with OpenMP for loop");
6811 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6815 switch (parser
->in_statement
& ~IN_SWITCH_STMT
)
6818 error ("continue statement not within a loop");
6820 case IN_ITERATION_STMT
:
6822 statement
= finish_continue_stmt ();
6825 error ("invalid exit from OpenMP structured block");
6830 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6837 /* If the next token is a `;', then there is no
6839 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6840 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
6843 /* Build the return-statement. */
6844 statement
= finish_return_stmt (expr
);
6845 /* Look for the final `;'. */
6846 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6851 /* Create the goto-statement. */
6852 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
6854 /* Issue a warning about this use of a GNU extension. */
6856 pedwarn ("ISO C++ forbids computed gotos");
6857 /* Consume the '*' token. */
6858 cp_lexer_consume_token (parser
->lexer
);
6859 /* Parse the dependent expression. */
6860 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false));
6863 finish_goto_stmt (cp_parser_identifier (parser
));
6864 /* Look for the final `;'. */
6865 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6869 cp_parser_error (parser
, "expected jump-statement");
6876 /* Parse a declaration-statement.
6878 declaration-statement:
6879 block-declaration */
6882 cp_parser_declaration_statement (cp_parser
* parser
)
6886 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6887 p
= obstack_alloc (&declarator_obstack
, 0);
6889 /* Parse the block-declaration. */
6890 cp_parser_block_declaration (parser
, /*statement_p=*/true);
6892 /* Free any declarators allocated. */
6893 obstack_free (&declarator_obstack
, p
);
6895 /* Finish off the statement. */
6899 /* Some dependent statements (like `if (cond) statement'), are
6900 implicitly in their own scope. In other words, if the statement is
6901 a single statement (as opposed to a compound-statement), it is
6902 none-the-less treated as if it were enclosed in braces. Any
6903 declarations appearing in the dependent statement are out of scope
6904 after control passes that point. This function parses a statement,
6905 but ensures that is in its own scope, even if it is not a
6908 Returns the new statement. */
6911 cp_parser_implicitly_scoped_statement (cp_parser
* parser
)
6915 /* Mark if () ; with a special NOP_EXPR. */
6916 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
6918 cp_lexer_consume_token (parser
->lexer
);
6919 statement
= add_stmt (build_empty_stmt ());
6921 /* if a compound is opened, we simply parse the statement directly. */
6922 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6923 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6924 /* If the token is not a `{', then we must take special action. */
6927 /* Create a compound-statement. */
6928 statement
= begin_compound_stmt (0);
6929 /* Parse the dependent-statement. */
6930 cp_parser_statement (parser
, NULL_TREE
, false);
6931 /* Finish the dummy compound-statement. */
6932 finish_compound_stmt (statement
);
6935 /* Return the statement. */
6939 /* For some dependent statements (like `while (cond) statement'), we
6940 have already created a scope. Therefore, even if the dependent
6941 statement is a compound-statement, we do not want to create another
6945 cp_parser_already_scoped_statement (cp_parser
* parser
)
6947 /* If the token is a `{', then we must take special action. */
6948 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
6949 cp_parser_statement (parser
, NULL_TREE
, false);
6952 /* Avoid calling cp_parser_compound_statement, so that we
6953 don't create a new scope. Do everything else by hand. */
6954 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
6955 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
6956 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6960 /* Declarations [gram.dcl.dcl] */
6962 /* Parse an optional declaration-sequence.
6966 declaration-seq declaration */
6969 cp_parser_declaration_seq_opt (cp_parser
* parser
)
6975 token
= cp_lexer_peek_token (parser
->lexer
);
6977 if (token
->type
== CPP_CLOSE_BRACE
6978 || token
->type
== CPP_EOF
6979 || token
->type
== CPP_PRAGMA_EOL
)
6982 if (token
->type
== CPP_SEMICOLON
)
6984 /* A declaration consisting of a single semicolon is
6985 invalid. Allow it unless we're being pedantic. */
6986 cp_lexer_consume_token (parser
->lexer
);
6987 if (pedantic
&& !in_system_header
)
6988 pedwarn ("extra %<;%>");
6992 /* If we're entering or exiting a region that's implicitly
6993 extern "C", modify the lang context appropriately. */
6994 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
6996 push_lang_context (lang_name_c
);
6997 parser
->implicit_extern_c
= true;
6999 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
7001 pop_lang_context ();
7002 parser
->implicit_extern_c
= false;
7005 if (token
->type
== CPP_PRAGMA
)
7007 /* A top-level declaration can consist solely of a #pragma.
7008 A nested declaration cannot, so this is done here and not
7009 in cp_parser_declaration. (A #pragma at block scope is
7010 handled in cp_parser_statement.) */
7011 cp_parser_pragma (parser
, pragma_external
);
7015 /* Parse the declaration itself. */
7016 cp_parser_declaration (parser
);
7020 /* Parse a declaration.
7025 template-declaration
7026 explicit-instantiation
7027 explicit-specialization
7028 linkage-specification
7029 namespace-definition
7034 __extension__ declaration */
7037 cp_parser_declaration (cp_parser
* parser
)
7044 /* Check for the `__extension__' keyword. */
7045 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7047 /* Parse the qualified declaration. */
7048 cp_parser_declaration (parser
);
7049 /* Restore the PEDANTIC flag. */
7050 pedantic
= saved_pedantic
;
7055 /* Try to figure out what kind of declaration is present. */
7056 token1
= *cp_lexer_peek_token (parser
->lexer
);
7058 if (token1
.type
!= CPP_EOF
)
7059 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
7062 token2
.type
= CPP_EOF
;
7063 token2
.keyword
= RID_MAX
;
7066 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7067 p
= obstack_alloc (&declarator_obstack
, 0);
7069 /* If the next token is `extern' and the following token is a string
7070 literal, then we have a linkage specification. */
7071 if (token1
.keyword
== RID_EXTERN
7072 && cp_parser_is_string_literal (&token2
))
7073 cp_parser_linkage_specification (parser
);
7074 /* If the next token is `template', then we have either a template
7075 declaration, an explicit instantiation, or an explicit
7077 else if (token1
.keyword
== RID_TEMPLATE
)
7079 /* `template <>' indicates a template specialization. */
7080 if (token2
.type
== CPP_LESS
7081 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
7082 cp_parser_explicit_specialization (parser
);
7083 /* `template <' indicates a template declaration. */
7084 else if (token2
.type
== CPP_LESS
)
7085 cp_parser_template_declaration (parser
, /*member_p=*/false);
7086 /* Anything else must be an explicit instantiation. */
7088 cp_parser_explicit_instantiation (parser
);
7090 /* If the next token is `export', then we have a template
7092 else if (token1
.keyword
== RID_EXPORT
)
7093 cp_parser_template_declaration (parser
, /*member_p=*/false);
7094 /* If the next token is `extern', 'static' or 'inline' and the one
7095 after that is `template', we have a GNU extended explicit
7096 instantiation directive. */
7097 else if (cp_parser_allow_gnu_extensions_p (parser
)
7098 && (token1
.keyword
== RID_EXTERN
7099 || token1
.keyword
== RID_STATIC
7100 || token1
.keyword
== RID_INLINE
)
7101 && token2
.keyword
== RID_TEMPLATE
)
7102 cp_parser_explicit_instantiation (parser
);
7103 /* If the next token is `namespace', check for a named or unnamed
7104 namespace definition. */
7105 else if (token1
.keyword
== RID_NAMESPACE
7106 && (/* A named namespace definition. */
7107 (token2
.type
== CPP_NAME
7108 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
7110 /* An unnamed namespace definition. */
7111 || token2
.type
== CPP_OPEN_BRACE
7112 || token2
.keyword
== RID_ATTRIBUTE
))
7113 cp_parser_namespace_definition (parser
);
7114 /* Objective-C++ declaration/definition. */
7115 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
7116 cp_parser_objc_declaration (parser
);
7117 /* We must have either a block declaration or a function
7120 /* Try to parse a block-declaration, or a function-definition. */
7121 cp_parser_block_declaration (parser
, /*statement_p=*/false);
7123 /* Free any declarators allocated. */
7124 obstack_free (&declarator_obstack
, p
);
7127 /* Parse a block-declaration.
7132 namespace-alias-definition
7139 __extension__ block-declaration
7142 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7143 part of a declaration-statement. */
7146 cp_parser_block_declaration (cp_parser
*parser
,
7152 /* Check for the `__extension__' keyword. */
7153 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7155 /* Parse the qualified declaration. */
7156 cp_parser_block_declaration (parser
, statement_p
);
7157 /* Restore the PEDANTIC flag. */
7158 pedantic
= saved_pedantic
;
7163 /* Peek at the next token to figure out which kind of declaration is
7165 token1
= cp_lexer_peek_token (parser
->lexer
);
7167 /* If the next keyword is `asm', we have an asm-definition. */
7168 if (token1
->keyword
== RID_ASM
)
7171 cp_parser_commit_to_tentative_parse (parser
);
7172 cp_parser_asm_definition (parser
);
7174 /* If the next keyword is `namespace', we have a
7175 namespace-alias-definition. */
7176 else if (token1
->keyword
== RID_NAMESPACE
)
7177 cp_parser_namespace_alias_definition (parser
);
7178 /* If the next keyword is `using', we have either a
7179 using-declaration or a using-directive. */
7180 else if (token1
->keyword
== RID_USING
)
7185 cp_parser_commit_to_tentative_parse (parser
);
7186 /* If the token after `using' is `namespace', then we have a
7188 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7189 if (token2
->keyword
== RID_NAMESPACE
)
7190 cp_parser_using_directive (parser
);
7191 /* Otherwise, it's a using-declaration. */
7193 cp_parser_using_declaration (parser
);
7195 /* If the next keyword is `__label__' we have a label declaration. */
7196 else if (token1
->keyword
== RID_LABEL
)
7199 cp_parser_commit_to_tentative_parse (parser
);
7200 cp_parser_label_declaration (parser
);
7202 /* Anything else must be a simple-declaration. */
7204 cp_parser_simple_declaration (parser
, !statement_p
);
7207 /* Parse a simple-declaration.
7210 decl-specifier-seq [opt] init-declarator-list [opt] ;
7212 init-declarator-list:
7214 init-declarator-list , init-declarator
7216 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7217 function-definition as a simple-declaration. */
7220 cp_parser_simple_declaration (cp_parser
* parser
,
7221 bool function_definition_allowed_p
)
7223 cp_decl_specifier_seq decl_specifiers
;
7224 int declares_class_or_enum
;
7225 bool saw_declarator
;
7227 /* Defer access checks until we know what is being declared; the
7228 checks for names appearing in the decl-specifier-seq should be
7229 done as if we were in the scope of the thing being declared. */
7230 push_deferring_access_checks (dk_deferred
);
7232 /* Parse the decl-specifier-seq. We have to keep track of whether
7233 or not the decl-specifier-seq declares a named class or
7234 enumeration type, since that is the only case in which the
7235 init-declarator-list is allowed to be empty.
7239 In a simple-declaration, the optional init-declarator-list can be
7240 omitted only when declaring a class or enumeration, that is when
7241 the decl-specifier-seq contains either a class-specifier, an
7242 elaborated-type-specifier, or an enum-specifier. */
7243 cp_parser_decl_specifier_seq (parser
,
7244 CP_PARSER_FLAGS_OPTIONAL
,
7246 &declares_class_or_enum
);
7247 /* We no longer need to defer access checks. */
7248 stop_deferring_access_checks ();
7250 /* In a block scope, a valid declaration must always have a
7251 decl-specifier-seq. By not trying to parse declarators, we can
7252 resolve the declaration/expression ambiguity more quickly. */
7253 if (!function_definition_allowed_p
7254 && !decl_specifiers
.any_specifiers_p
)
7256 cp_parser_error (parser
, "expected declaration");
7260 /* If the next two tokens are both identifiers, the code is
7261 erroneous. The usual cause of this situation is code like:
7265 where "T" should name a type -- but does not. */
7266 if (!decl_specifiers
.type
7267 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
7269 /* If parsing tentatively, we should commit; we really are
7270 looking at a declaration. */
7271 cp_parser_commit_to_tentative_parse (parser
);
7276 /* If we have seen at least one decl-specifier, and the next token
7277 is not a parenthesis, then we must be looking at a declaration.
7278 (After "int (" we might be looking at a functional cast.) */
7279 if (decl_specifiers
.any_specifiers_p
7280 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7281 cp_parser_commit_to_tentative_parse (parser
);
7283 /* Keep going until we hit the `;' at the end of the simple
7285 saw_declarator
= false;
7286 while (cp_lexer_next_token_is_not (parser
->lexer
,
7290 bool function_definition_p
;
7295 /* If we are processing next declarator, coma is expected */
7296 token
= cp_lexer_peek_token (parser
->lexer
);
7297 gcc_assert (token
->type
== CPP_COMMA
);
7298 cp_lexer_consume_token (parser
->lexer
);
7301 saw_declarator
= true;
7303 /* Parse the init-declarator. */
7304 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
7305 /*checks=*/NULL_TREE
,
7306 function_definition_allowed_p
,
7308 declares_class_or_enum
,
7309 &function_definition_p
);
7310 /* If an error occurred while parsing tentatively, exit quickly.
7311 (That usually happens when in the body of a function; each
7312 statement is treated as a declaration-statement until proven
7314 if (cp_parser_error_occurred (parser
))
7316 /* Handle function definitions specially. */
7317 if (function_definition_p
)
7319 /* If the next token is a `,', then we are probably
7320 processing something like:
7324 which is erroneous. */
7325 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
7326 error ("mixing declarations and function-definitions is forbidden");
7327 /* Otherwise, we're done with the list of declarators. */
7330 pop_deferring_access_checks ();
7334 /* The next token should be either a `,' or a `;'. */
7335 token
= cp_lexer_peek_token (parser
->lexer
);
7336 /* If it's a `,', there are more declarators to come. */
7337 if (token
->type
== CPP_COMMA
)
7338 /* will be consumed next time around */;
7339 /* If it's a `;', we are done. */
7340 else if (token
->type
== CPP_SEMICOLON
)
7342 /* Anything else is an error. */
7345 /* If we have already issued an error message we don't need
7346 to issue another one. */
7347 if (decl
!= error_mark_node
7348 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
7349 cp_parser_error (parser
, "expected %<,%> or %<;%>");
7350 /* Skip tokens until we reach the end of the statement. */
7351 cp_parser_skip_to_end_of_statement (parser
);
7352 /* If the next token is now a `;', consume it. */
7353 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7354 cp_lexer_consume_token (parser
->lexer
);
7357 /* After the first time around, a function-definition is not
7358 allowed -- even if it was OK at first. For example:
7363 function_definition_allowed_p
= false;
7366 /* Issue an error message if no declarators are present, and the
7367 decl-specifier-seq does not itself declare a class or
7369 if (!saw_declarator
)
7371 if (cp_parser_declares_only_class_p (parser
))
7372 shadow_tag (&decl_specifiers
);
7373 /* Perform any deferred access checks. */
7374 perform_deferred_access_checks ();
7377 /* Consume the `;'. */
7378 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
7381 pop_deferring_access_checks ();
7384 /* Parse a decl-specifier-seq.
7387 decl-specifier-seq [opt] decl-specifier
7390 storage-class-specifier
7401 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7403 The parser flags FLAGS is used to control type-specifier parsing.
7405 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7408 1: one of the decl-specifiers is an elaborated-type-specifier
7409 (i.e., a type declaration)
7410 2: one of the decl-specifiers is an enum-specifier or a
7411 class-specifier (i.e., a type definition)
7416 cp_parser_decl_specifier_seq (cp_parser
* parser
,
7417 cp_parser_flags flags
,
7418 cp_decl_specifier_seq
*decl_specs
,
7419 int* declares_class_or_enum
)
7421 bool constructor_possible_p
= !parser
->in_declarator_p
;
7423 /* Clear DECL_SPECS. */
7424 clear_decl_specs (decl_specs
);
7426 /* Assume no class or enumeration type is declared. */
7427 *declares_class_or_enum
= 0;
7429 /* Keep reading specifiers until there are no more to read. */
7433 bool found_decl_spec
;
7436 /* Peek at the next token. */
7437 token
= cp_lexer_peek_token (parser
->lexer
);
7438 /* Handle attributes. */
7439 if (token
->keyword
== RID_ATTRIBUTE
)
7441 /* Parse the attributes. */
7442 decl_specs
->attributes
7443 = chainon (decl_specs
->attributes
,
7444 cp_parser_attributes_opt (parser
));
7447 /* Assume we will find a decl-specifier keyword. */
7448 found_decl_spec
= true;
7449 /* If the next token is an appropriate keyword, we can simply
7450 add it to the list. */
7451 switch (token
->keyword
)
7456 if (!at_class_scope_p ())
7458 error ("%<friend%> used outside of class");
7459 cp_lexer_purge_token (parser
->lexer
);
7463 ++decl_specs
->specs
[(int) ds_friend
];
7464 /* Consume the token. */
7465 cp_lexer_consume_token (parser
->lexer
);
7469 /* function-specifier:
7476 cp_parser_function_specifier_opt (parser
, decl_specs
);
7482 ++decl_specs
->specs
[(int) ds_typedef
];
7483 /* Consume the token. */
7484 cp_lexer_consume_token (parser
->lexer
);
7485 /* A constructor declarator cannot appear in a typedef. */
7486 constructor_possible_p
= false;
7487 /* The "typedef" keyword can only occur in a declaration; we
7488 may as well commit at this point. */
7489 cp_parser_commit_to_tentative_parse (parser
);
7492 /* storage-class-specifier:
7506 /* Consume the token. */
7507 cp_lexer_consume_token (parser
->lexer
);
7508 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
);
7511 /* Consume the token. */
7512 cp_lexer_consume_token (parser
->lexer
);
7513 ++decl_specs
->specs
[(int) ds_thread
];
7517 /* We did not yet find a decl-specifier yet. */
7518 found_decl_spec
= false;
7522 /* Constructors are a special case. The `S' in `S()' is not a
7523 decl-specifier; it is the beginning of the declarator. */
7526 && constructor_possible_p
7527 && (cp_parser_constructor_declarator_p
7528 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
7530 /* If we don't have a DECL_SPEC yet, then we must be looking at
7531 a type-specifier. */
7532 if (!found_decl_spec
&& !constructor_p
)
7534 int decl_spec_declares_class_or_enum
;
7535 bool is_cv_qualifier
;
7539 = cp_parser_type_specifier (parser
, flags
,
7541 /*is_declaration=*/true,
7542 &decl_spec_declares_class_or_enum
,
7545 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
7547 /* If this type-specifier referenced a user-defined type
7548 (a typedef, class-name, etc.), then we can't allow any
7549 more such type-specifiers henceforth.
7553 The longest sequence of decl-specifiers that could
7554 possibly be a type name is taken as the
7555 decl-specifier-seq of a declaration. The sequence shall
7556 be self-consistent as described below.
7560 As a general rule, at most one type-specifier is allowed
7561 in the complete decl-specifier-seq of a declaration. The
7562 only exceptions are the following:
7564 -- const or volatile can be combined with any other
7567 -- signed or unsigned can be combined with char, long,
7575 void g (const int Pc);
7577 Here, Pc is *not* part of the decl-specifier seq; it's
7578 the declarator. Therefore, once we see a type-specifier
7579 (other than a cv-qualifier), we forbid any additional
7580 user-defined types. We *do* still allow things like `int
7581 int' to be considered a decl-specifier-seq, and issue the
7582 error message later. */
7583 if (type_spec
&& !is_cv_qualifier
)
7584 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
7585 /* A constructor declarator cannot follow a type-specifier. */
7588 constructor_possible_p
= false;
7589 found_decl_spec
= true;
7593 /* If we still do not have a DECL_SPEC, then there are no more
7595 if (!found_decl_spec
)
7598 decl_specs
->any_specifiers_p
= true;
7599 /* After we see one decl-specifier, further decl-specifiers are
7601 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
7604 cp_parser_check_decl_spec (decl_specs
);
7606 /* Don't allow a friend specifier with a class definition. */
7607 if (decl_specs
->specs
[(int) ds_friend
] != 0
7608 && (*declares_class_or_enum
& 2))
7609 error ("class definition may not be declared a friend");
7612 /* Parse an (optional) storage-class-specifier.
7614 storage-class-specifier:
7623 storage-class-specifier:
7626 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7629 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
7631 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7639 /* Consume the token. */
7640 return cp_lexer_consume_token (parser
->lexer
)->value
;
7647 /* Parse an (optional) function-specifier.
7654 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7655 Updates DECL_SPECS, if it is non-NULL. */
7658 cp_parser_function_specifier_opt (cp_parser
* parser
,
7659 cp_decl_specifier_seq
*decl_specs
)
7661 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7665 ++decl_specs
->specs
[(int) ds_inline
];
7669 /* 14.5.2.3 [temp.mem]
7671 A member function template shall not be virtual. */
7672 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7673 error ("templates may not be %<virtual%>");
7674 else if (decl_specs
)
7675 ++decl_specs
->specs
[(int) ds_virtual
];
7680 ++decl_specs
->specs
[(int) ds_explicit
];
7687 /* Consume the token. */
7688 return cp_lexer_consume_token (parser
->lexer
)->value
;
7691 /* Parse a linkage-specification.
7693 linkage-specification:
7694 extern string-literal { declaration-seq [opt] }
7695 extern string-literal declaration */
7698 cp_parser_linkage_specification (cp_parser
* parser
)
7702 /* Look for the `extern' keyword. */
7703 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
7705 /* Look for the string-literal. */
7706 linkage
= cp_parser_string_literal (parser
, false, false);
7708 /* Transform the literal into an identifier. If the literal is a
7709 wide-character string, or contains embedded NULs, then we can't
7710 handle it as the user wants. */
7711 if (strlen (TREE_STRING_POINTER (linkage
))
7712 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
7714 cp_parser_error (parser
, "invalid linkage-specification");
7715 /* Assume C++ linkage. */
7716 linkage
= lang_name_cplusplus
;
7719 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
7721 /* We're now using the new linkage. */
7722 push_lang_context (linkage
);
7724 /* If the next token is a `{', then we're using the first
7726 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7728 /* Consume the `{' token. */
7729 cp_lexer_consume_token (parser
->lexer
);
7730 /* Parse the declarations. */
7731 cp_parser_declaration_seq_opt (parser
);
7732 /* Look for the closing `}'. */
7733 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
7735 /* Otherwise, there's just one declaration. */
7738 bool saved_in_unbraced_linkage_specification_p
;
7740 saved_in_unbraced_linkage_specification_p
7741 = parser
->in_unbraced_linkage_specification_p
;
7742 parser
->in_unbraced_linkage_specification_p
= true;
7743 cp_parser_declaration (parser
);
7744 parser
->in_unbraced_linkage_specification_p
7745 = saved_in_unbraced_linkage_specification_p
;
7748 /* We're done with the linkage-specification. */
7749 pop_lang_context ();
7752 /* Special member functions [gram.special] */
7754 /* Parse a conversion-function-id.
7756 conversion-function-id:
7757 operator conversion-type-id
7759 Returns an IDENTIFIER_NODE representing the operator. */
7762 cp_parser_conversion_function_id (cp_parser
* parser
)
7766 tree saved_qualifying_scope
;
7767 tree saved_object_scope
;
7768 tree pushed_scope
= NULL_TREE
;
7770 /* Look for the `operator' token. */
7771 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7772 return error_mark_node
;
7773 /* When we parse the conversion-type-id, the current scope will be
7774 reset. However, we need that information in able to look up the
7775 conversion function later, so we save it here. */
7776 saved_scope
= parser
->scope
;
7777 saved_qualifying_scope
= parser
->qualifying_scope
;
7778 saved_object_scope
= parser
->object_scope
;
7779 /* We must enter the scope of the class so that the names of
7780 entities declared within the class are available in the
7781 conversion-type-id. For example, consider:
7788 S::operator I() { ... }
7790 In order to see that `I' is a type-name in the definition, we
7791 must be in the scope of `S'. */
7793 pushed_scope
= push_scope (saved_scope
);
7794 /* Parse the conversion-type-id. */
7795 type
= cp_parser_conversion_type_id (parser
);
7796 /* Leave the scope of the class, if any. */
7798 pop_scope (pushed_scope
);
7799 /* Restore the saved scope. */
7800 parser
->scope
= saved_scope
;
7801 parser
->qualifying_scope
= saved_qualifying_scope
;
7802 parser
->object_scope
= saved_object_scope
;
7803 /* If the TYPE is invalid, indicate failure. */
7804 if (type
== error_mark_node
)
7805 return error_mark_node
;
7806 return mangle_conv_op_name_for_type (type
);
7809 /* Parse a conversion-type-id:
7812 type-specifier-seq conversion-declarator [opt]
7814 Returns the TYPE specified. */
7817 cp_parser_conversion_type_id (cp_parser
* parser
)
7820 cp_decl_specifier_seq type_specifiers
;
7821 cp_declarator
*declarator
;
7822 tree type_specified
;
7824 /* Parse the attributes. */
7825 attributes
= cp_parser_attributes_opt (parser
);
7826 /* Parse the type-specifiers. */
7827 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
7829 /* If that didn't work, stop. */
7830 if (type_specifiers
.type
== error_mark_node
)
7831 return error_mark_node
;
7832 /* Parse the conversion-declarator. */
7833 declarator
= cp_parser_conversion_declarator_opt (parser
);
7835 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
7836 /*initialized=*/0, &attributes
);
7838 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
7839 return type_specified
;
7842 /* Parse an (optional) conversion-declarator.
7844 conversion-declarator:
7845 ptr-operator conversion-declarator [opt]
7849 static cp_declarator
*
7850 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
7852 enum tree_code code
;
7854 cp_cv_quals cv_quals
;
7856 /* We don't know if there's a ptr-operator next, or not. */
7857 cp_parser_parse_tentatively (parser
);
7858 /* Try the ptr-operator. */
7859 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
7860 /* If it worked, look for more conversion-declarators. */
7861 if (cp_parser_parse_definitely (parser
))
7863 cp_declarator
*declarator
;
7865 /* Parse another optional declarator. */
7866 declarator
= cp_parser_conversion_declarator_opt (parser
);
7868 /* Create the representation of the declarator. */
7870 declarator
= make_ptrmem_declarator (cv_quals
, class_type
,
7872 else if (code
== INDIRECT_REF
)
7873 declarator
= make_pointer_declarator (cv_quals
, declarator
);
7875 declarator
= make_reference_declarator (cv_quals
, declarator
);
7883 /* Parse an (optional) ctor-initializer.
7886 : mem-initializer-list
7888 Returns TRUE iff the ctor-initializer was actually present. */
7891 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
7893 /* If the next token is not a `:', then there is no
7894 ctor-initializer. */
7895 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
7897 /* Do default initialization of any bases and members. */
7898 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7899 finish_mem_initializers (NULL_TREE
);
7904 /* Consume the `:' token. */
7905 cp_lexer_consume_token (parser
->lexer
);
7906 /* And the mem-initializer-list. */
7907 cp_parser_mem_initializer_list (parser
);
7912 /* Parse a mem-initializer-list.
7914 mem-initializer-list:
7916 mem-initializer , mem-initializer-list */
7919 cp_parser_mem_initializer_list (cp_parser
* parser
)
7921 tree mem_initializer_list
= NULL_TREE
;
7923 /* Let the semantic analysis code know that we are starting the
7924 mem-initializer-list. */
7925 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
7926 error ("only constructors take base initializers");
7928 /* Loop through the list. */
7931 tree mem_initializer
;
7933 /* Parse the mem-initializer. */
7934 mem_initializer
= cp_parser_mem_initializer (parser
);
7935 /* Add it to the list, unless it was erroneous. */
7936 if (mem_initializer
!= error_mark_node
)
7938 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
7939 mem_initializer_list
= mem_initializer
;
7941 /* If the next token is not a `,', we're done. */
7942 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7944 /* Consume the `,' token. */
7945 cp_lexer_consume_token (parser
->lexer
);
7948 /* Perform semantic analysis. */
7949 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7950 finish_mem_initializers (mem_initializer_list
);
7953 /* Parse a mem-initializer.
7956 mem-initializer-id ( expression-list [opt] )
7961 ( expression-list [opt] )
7963 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7964 class) or FIELD_DECL (for a non-static data member) to initialize;
7965 the TREE_VALUE is the expression-list. An empty initialization
7966 list is represented by void_list_node. */
7969 cp_parser_mem_initializer (cp_parser
* parser
)
7971 tree mem_initializer_id
;
7972 tree expression_list
;
7975 /* Find out what is being initialized. */
7976 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7978 pedwarn ("anachronistic old-style base class initializer");
7979 mem_initializer_id
= NULL_TREE
;
7982 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
7983 member
= expand_member_init (mem_initializer_id
);
7984 if (member
&& !DECL_P (member
))
7985 in_base_initializer
= 1;
7988 = cp_parser_parenthesized_expression_list (parser
, false,
7990 /*non_constant_p=*/NULL
);
7991 if (expression_list
== error_mark_node
)
7992 return error_mark_node
;
7993 if (!expression_list
)
7994 expression_list
= void_type_node
;
7996 in_base_initializer
= 0;
7998 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
8001 /* Parse a mem-initializer-id.
8004 :: [opt] nested-name-specifier [opt] class-name
8007 Returns a TYPE indicating the class to be initializer for the first
8008 production. Returns an IDENTIFIER_NODE indicating the data member
8009 to be initialized for the second production. */
8012 cp_parser_mem_initializer_id (cp_parser
* parser
)
8014 bool global_scope_p
;
8015 bool nested_name_specifier_p
;
8016 bool template_p
= false;
8019 /* `typename' is not allowed in this context ([temp.res]). */
8020 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
8022 error ("keyword %<typename%> not allowed in this context (a qualified "
8023 "member initializer is implicitly a type)");
8024 cp_lexer_consume_token (parser
->lexer
);
8026 /* Look for the optional `::' operator. */
8028 = (cp_parser_global_scope_opt (parser
,
8029 /*current_scope_valid_p=*/false)
8031 /* Look for the optional nested-name-specifier. The simplest way to
8036 The keyword `typename' is not permitted in a base-specifier or
8037 mem-initializer; in these contexts a qualified name that
8038 depends on a template-parameter is implicitly assumed to be a
8041 is to assume that we have seen the `typename' keyword at this
8043 nested_name_specifier_p
8044 = (cp_parser_nested_name_specifier_opt (parser
,
8045 /*typename_keyword_p=*/true,
8046 /*check_dependency_p=*/true,
8048 /*is_declaration=*/true)
8050 if (nested_name_specifier_p
)
8051 template_p
= cp_parser_optional_template_keyword (parser
);
8052 /* If there is a `::' operator or a nested-name-specifier, then we
8053 are definitely looking for a class-name. */
8054 if (global_scope_p
|| nested_name_specifier_p
)
8055 return cp_parser_class_name (parser
,
8056 /*typename_keyword_p=*/true,
8057 /*template_keyword_p=*/template_p
,
8059 /*check_dependency_p=*/true,
8060 /*class_head_p=*/false,
8061 /*is_declaration=*/true);
8062 /* Otherwise, we could also be looking for an ordinary identifier. */
8063 cp_parser_parse_tentatively (parser
);
8064 /* Try a class-name. */
8065 id
= cp_parser_class_name (parser
,
8066 /*typename_keyword_p=*/true,
8067 /*template_keyword_p=*/false,
8069 /*check_dependency_p=*/true,
8070 /*class_head_p=*/false,
8071 /*is_declaration=*/true);
8072 /* If we found one, we're done. */
8073 if (cp_parser_parse_definitely (parser
))
8075 /* Otherwise, look for an ordinary identifier. */
8076 return cp_parser_identifier (parser
);
8079 /* Overloading [gram.over] */
8081 /* Parse an operator-function-id.
8083 operator-function-id:
8086 Returns an IDENTIFIER_NODE for the operator which is a
8087 human-readable spelling of the identifier, e.g., `operator +'. */
8090 cp_parser_operator_function_id (cp_parser
* parser
)
8092 /* Look for the `operator' keyword. */
8093 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
8094 return error_mark_node
;
8095 /* And then the name of the operator itself. */
8096 return cp_parser_operator (parser
);
8099 /* Parse an operator.
8102 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8103 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8104 || ++ -- , ->* -> () []
8111 Returns an IDENTIFIER_NODE for the operator which is a
8112 human-readable spelling of the identifier, e.g., `operator +'. */
8115 cp_parser_operator (cp_parser
* parser
)
8117 tree id
= NULL_TREE
;
8120 /* Peek at the next token. */
8121 token
= cp_lexer_peek_token (parser
->lexer
);
8122 /* Figure out which operator we have. */
8123 switch (token
->type
)
8129 /* The keyword should be either `new' or `delete'. */
8130 if (token
->keyword
== RID_NEW
)
8132 else if (token
->keyword
== RID_DELETE
)
8137 /* Consume the `new' or `delete' token. */
8138 cp_lexer_consume_token (parser
->lexer
);
8140 /* Peek at the next token. */
8141 token
= cp_lexer_peek_token (parser
->lexer
);
8142 /* If it's a `[' token then this is the array variant of the
8144 if (token
->type
== CPP_OPEN_SQUARE
)
8146 /* Consume the `[' token. */
8147 cp_lexer_consume_token (parser
->lexer
);
8148 /* Look for the `]' token. */
8149 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8150 id
= ansi_opname (op
== NEW_EXPR
8151 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
8153 /* Otherwise, we have the non-array variant. */
8155 id
= ansi_opname (op
);
8161 id
= ansi_opname (PLUS_EXPR
);
8165 id
= ansi_opname (MINUS_EXPR
);
8169 id
= ansi_opname (MULT_EXPR
);
8173 id
= ansi_opname (TRUNC_DIV_EXPR
);
8177 id
= ansi_opname (TRUNC_MOD_EXPR
);
8181 id
= ansi_opname (BIT_XOR_EXPR
);
8185 id
= ansi_opname (BIT_AND_EXPR
);
8189 id
= ansi_opname (BIT_IOR_EXPR
);
8193 id
= ansi_opname (BIT_NOT_EXPR
);
8197 id
= ansi_opname (TRUTH_NOT_EXPR
);
8201 id
= ansi_assopname (NOP_EXPR
);
8205 id
= ansi_opname (LT_EXPR
);
8209 id
= ansi_opname (GT_EXPR
);
8213 id
= ansi_assopname (PLUS_EXPR
);
8217 id
= ansi_assopname (MINUS_EXPR
);
8221 id
= ansi_assopname (MULT_EXPR
);
8225 id
= ansi_assopname (TRUNC_DIV_EXPR
);
8229 id
= ansi_assopname (TRUNC_MOD_EXPR
);
8233 id
= ansi_assopname (BIT_XOR_EXPR
);
8237 id
= ansi_assopname (BIT_AND_EXPR
);
8241 id
= ansi_assopname (BIT_IOR_EXPR
);
8245 id
= ansi_opname (LSHIFT_EXPR
);
8249 id
= ansi_opname (RSHIFT_EXPR
);
8253 id
= ansi_assopname (LSHIFT_EXPR
);
8257 id
= ansi_assopname (RSHIFT_EXPR
);
8261 id
= ansi_opname (EQ_EXPR
);
8265 id
= ansi_opname (NE_EXPR
);
8269 id
= ansi_opname (LE_EXPR
);
8272 case CPP_GREATER_EQ
:
8273 id
= ansi_opname (GE_EXPR
);
8277 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
8281 id
= ansi_opname (TRUTH_ORIF_EXPR
);
8285 id
= ansi_opname (POSTINCREMENT_EXPR
);
8288 case CPP_MINUS_MINUS
:
8289 id
= ansi_opname (PREDECREMENT_EXPR
);
8293 id
= ansi_opname (COMPOUND_EXPR
);
8296 case CPP_DEREF_STAR
:
8297 id
= ansi_opname (MEMBER_REF
);
8301 id
= ansi_opname (COMPONENT_REF
);
8304 case CPP_OPEN_PAREN
:
8305 /* Consume the `('. */
8306 cp_lexer_consume_token (parser
->lexer
);
8307 /* Look for the matching `)'. */
8308 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
8309 return ansi_opname (CALL_EXPR
);
8311 case CPP_OPEN_SQUARE
:
8312 /* Consume the `['. */
8313 cp_lexer_consume_token (parser
->lexer
);
8314 /* Look for the matching `]'. */
8315 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8316 return ansi_opname (ARRAY_REF
);
8320 id
= ansi_opname (MIN_EXPR
);
8321 cp_parser_warn_min_max ();
8325 id
= ansi_opname (MAX_EXPR
);
8326 cp_parser_warn_min_max ();
8330 id
= ansi_assopname (MIN_EXPR
);
8331 cp_parser_warn_min_max ();
8335 id
= ansi_assopname (MAX_EXPR
);
8336 cp_parser_warn_min_max ();
8340 /* Anything else is an error. */
8344 /* If we have selected an identifier, we need to consume the
8347 cp_lexer_consume_token (parser
->lexer
);
8348 /* Otherwise, no valid operator name was present. */
8351 cp_parser_error (parser
, "expected operator");
8352 id
= error_mark_node
;
8358 /* Parse a template-declaration.
8360 template-declaration:
8361 export [opt] template < template-parameter-list > declaration
8363 If MEMBER_P is TRUE, this template-declaration occurs within a
8366 The grammar rule given by the standard isn't correct. What
8369 template-declaration:
8370 export [opt] template-parameter-list-seq
8371 decl-specifier-seq [opt] init-declarator [opt] ;
8372 export [opt] template-parameter-list-seq
8375 template-parameter-list-seq:
8376 template-parameter-list-seq [opt]
8377 template < template-parameter-list > */
8380 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
8382 /* Check for `export'. */
8383 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
8385 /* Consume the `export' token. */
8386 cp_lexer_consume_token (parser
->lexer
);
8387 /* Warn that we do not support `export'. */
8388 warning (0, "keyword %<export%> not implemented, and will be ignored");
8391 cp_parser_template_declaration_after_export (parser
, member_p
);
8394 /* Parse a template-parameter-list.
8396 template-parameter-list:
8398 template-parameter-list , template-parameter
8400 Returns a TREE_LIST. Each node represents a template parameter.
8401 The nodes are connected via their TREE_CHAINs. */
8404 cp_parser_template_parameter_list (cp_parser
* parser
)
8406 tree parameter_list
= NULL_TREE
;
8408 begin_template_parm_list ();
8415 /* Parse the template-parameter. */
8416 parameter
= cp_parser_template_parameter (parser
, &is_non_type
);
8417 /* Add it to the list. */
8418 if (parameter
!= error_mark_node
)
8419 parameter_list
= process_template_parm (parameter_list
,
8422 /* Peek at the next token. */
8423 token
= cp_lexer_peek_token (parser
->lexer
);
8424 /* If it's not a `,', we're done. */
8425 if (token
->type
!= CPP_COMMA
)
8427 /* Otherwise, consume the `,' token. */
8428 cp_lexer_consume_token (parser
->lexer
);
8431 return end_template_parm_list (parameter_list
);
8434 /* Parse a template-parameter.
8438 parameter-declaration
8440 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8441 the parameter. The TREE_PURPOSE is the default value, if any.
8442 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8443 iff this parameter is a non-type parameter. */
8446 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
)
8449 cp_parameter_declarator
*parameter_declarator
;
8452 /* Assume it is a type parameter or a template parameter. */
8453 *is_non_type
= false;
8454 /* Peek at the next token. */
8455 token
= cp_lexer_peek_token (parser
->lexer
);
8456 /* If it is `class' or `template', we have a type-parameter. */
8457 if (token
->keyword
== RID_TEMPLATE
)
8458 return cp_parser_type_parameter (parser
);
8459 /* If it is `class' or `typename' we do not know yet whether it is a
8460 type parameter or a non-type parameter. Consider:
8462 template <typename T, typename T::X X> ...
8466 template <class C, class D*> ...
8468 Here, the first parameter is a type parameter, and the second is
8469 a non-type parameter. We can tell by looking at the token after
8470 the identifier -- if it is a `,', `=', or `>' then we have a type
8472 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
8474 /* Peek at the token after `class' or `typename'. */
8475 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8476 /* If it's an identifier, skip it. */
8477 if (token
->type
== CPP_NAME
)
8478 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
8479 /* Now, see if the token looks like the end of a template
8481 if (token
->type
== CPP_COMMA
8482 || token
->type
== CPP_EQ
8483 || token
->type
== CPP_GREATER
)
8484 return cp_parser_type_parameter (parser
);
8487 /* Otherwise, it is a non-type parameter.
8491 When parsing a default template-argument for a non-type
8492 template-parameter, the first non-nested `>' is taken as the end
8493 of the template parameter-list rather than a greater-than
8495 *is_non_type
= true;
8496 parameter_declarator
8497 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
8498 /*parenthesized_p=*/NULL
);
8499 parm
= grokdeclarator (parameter_declarator
->declarator
,
8500 ¶meter_declarator
->decl_specifiers
,
8501 PARM
, /*initialized=*/0,
8503 if (parm
== error_mark_node
)
8504 return error_mark_node
;
8505 return build_tree_list (parameter_declarator
->default_argument
, parm
);
8508 /* Parse a type-parameter.
8511 class identifier [opt]
8512 class identifier [opt] = type-id
8513 typename identifier [opt]
8514 typename identifier [opt] = type-id
8515 template < template-parameter-list > class identifier [opt]
8516 template < template-parameter-list > class identifier [opt]
8519 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8520 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8521 the declaration of the parameter. */
8524 cp_parser_type_parameter (cp_parser
* parser
)
8529 /* Look for a keyword to tell us what kind of parameter this is. */
8530 token
= cp_parser_require (parser
, CPP_KEYWORD
,
8531 "`class', `typename', or `template'");
8533 return error_mark_node
;
8535 switch (token
->keyword
)
8541 tree default_argument
;
8543 /* If the next token is an identifier, then it names the
8545 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8546 identifier
= cp_parser_identifier (parser
);
8548 identifier
= NULL_TREE
;
8550 /* Create the parameter. */
8551 parameter
= finish_template_type_parm (class_type_node
, identifier
);
8553 /* If the next token is an `=', we have a default argument. */
8554 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8556 /* Consume the `=' token. */
8557 cp_lexer_consume_token (parser
->lexer
);
8558 /* Parse the default-argument. */
8559 push_deferring_access_checks (dk_no_deferred
);
8560 default_argument
= cp_parser_type_id (parser
);
8561 pop_deferring_access_checks ();
8564 default_argument
= NULL_TREE
;
8566 /* Create the combined representation of the parameter and the
8567 default argument. */
8568 parameter
= build_tree_list (default_argument
, parameter
);
8574 tree parameter_list
;
8576 tree default_argument
;
8578 /* Look for the `<'. */
8579 cp_parser_require (parser
, CPP_LESS
, "`<'");
8580 /* Parse the template-parameter-list. */
8581 parameter_list
= cp_parser_template_parameter_list (parser
);
8582 /* Look for the `>'. */
8583 cp_parser_require (parser
, CPP_GREATER
, "`>'");
8584 /* Look for the `class' keyword. */
8585 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
8586 /* If the next token is an `=', then there is a
8587 default-argument. If the next token is a `>', we are at
8588 the end of the parameter-list. If the next token is a `,',
8589 then we are at the end of this parameter. */
8590 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
8591 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
8592 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8594 identifier
= cp_parser_identifier (parser
);
8595 /* Treat invalid names as if the parameter were nameless. */
8596 if (identifier
== error_mark_node
)
8597 identifier
= NULL_TREE
;
8600 identifier
= NULL_TREE
;
8602 /* Create the template parameter. */
8603 parameter
= finish_template_template_parm (class_type_node
,
8606 /* If the next token is an `=', then there is a
8607 default-argument. */
8608 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8612 /* Consume the `='. */
8613 cp_lexer_consume_token (parser
->lexer
);
8614 /* Parse the id-expression. */
8615 push_deferring_access_checks (dk_no_deferred
);
8617 = cp_parser_id_expression (parser
,
8618 /*template_keyword_p=*/false,
8619 /*check_dependency_p=*/true,
8620 /*template_p=*/&is_template
,
8621 /*declarator_p=*/false,
8622 /*optional_p=*/false);
8623 if (TREE_CODE (default_argument
) == TYPE_DECL
)
8624 /* If the id-expression was a template-id that refers to
8625 a template-class, we already have the declaration here,
8626 so no further lookup is needed. */
8629 /* Look up the name. */
8631 = cp_parser_lookup_name (parser
, default_argument
,
8633 /*is_template=*/is_template
,
8634 /*is_namespace=*/false,
8635 /*check_dependency=*/true,
8636 /*ambiguous_decls=*/NULL
);
8637 /* See if the default argument is valid. */
8639 = check_template_template_default_arg (default_argument
);
8640 pop_deferring_access_checks ();
8643 default_argument
= NULL_TREE
;
8645 /* Create the combined representation of the parameter and the
8646 default argument. */
8647 parameter
= build_tree_list (default_argument
, parameter
);
8659 /* Parse a template-id.
8662 template-name < template-argument-list [opt] >
8664 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8665 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8666 returned. Otherwise, if the template-name names a function, or set
8667 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8668 names a class, returns a TYPE_DECL for the specialization.
8670 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8671 uninstantiated templates. */
8674 cp_parser_template_id (cp_parser
*parser
,
8675 bool template_keyword_p
,
8676 bool check_dependency_p
,
8677 bool is_declaration
)
8682 cp_token_position start_of_id
= 0;
8683 tree access_check
= NULL_TREE
;
8684 cp_token
*next_token
, *next_token_2
;
8687 /* If the next token corresponds to a template-id, there is no need
8689 next_token
= cp_lexer_peek_token (parser
->lexer
);
8690 if (next_token
->type
== CPP_TEMPLATE_ID
)
8695 /* Get the stored value. */
8696 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
8697 /* Perform any access checks that were deferred. */
8698 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
8699 perform_or_defer_access_check (TREE_PURPOSE (check
),
8700 TREE_VALUE (check
));
8701 /* Return the stored value. */
8702 return TREE_VALUE (value
);
8705 /* Avoid performing name lookup if there is no possibility of
8706 finding a template-id. */
8707 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
8708 || (next_token
->type
== CPP_NAME
8709 && !cp_parser_nth_token_starts_template_argument_list_p
8712 cp_parser_error (parser
, "expected template-id");
8713 return error_mark_node
;
8716 /* Remember where the template-id starts. */
8717 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
8718 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
8720 push_deferring_access_checks (dk_deferred
);
8722 /* Parse the template-name. */
8723 is_identifier
= false;
8724 template = cp_parser_template_name (parser
, template_keyword_p
,
8728 if (template == error_mark_node
|| is_identifier
)
8730 pop_deferring_access_checks ();
8734 /* If we find the sequence `[:' after a template-name, it's probably
8735 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8736 parse correctly the argument list. */
8737 next_token
= cp_lexer_peek_token (parser
->lexer
);
8738 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8739 if (next_token
->type
== CPP_OPEN_SQUARE
8740 && next_token
->flags
& DIGRAPH
8741 && next_token_2
->type
== CPP_COLON
8742 && !(next_token_2
->flags
& PREV_WHITE
))
8744 cp_parser_parse_tentatively (parser
);
8745 /* Change `:' into `::'. */
8746 next_token_2
->type
= CPP_SCOPE
;
8747 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8749 cp_lexer_consume_token (parser
->lexer
);
8750 /* Parse the arguments. */
8751 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8752 if (!cp_parser_parse_definitely (parser
))
8754 /* If we couldn't parse an argument list, then we revert our changes
8755 and return simply an error. Maybe this is not a template-id
8757 next_token_2
->type
= CPP_COLON
;
8758 cp_parser_error (parser
, "expected %<<%>");
8759 pop_deferring_access_checks ();
8760 return error_mark_node
;
8762 /* Otherwise, emit an error about the invalid digraph, but continue
8763 parsing because we got our argument list. */
8764 pedwarn ("%<<::%> cannot begin a template-argument list");
8765 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8766 "between %<<%> and %<::%>");
8767 if (!flag_permissive
)
8772 inform ("(if you use -fpermissive G++ will accept your code)");
8779 /* Look for the `<' that starts the template-argument-list. */
8780 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
8782 pop_deferring_access_checks ();
8783 return error_mark_node
;
8785 /* Parse the arguments. */
8786 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8789 /* Build a representation of the specialization. */
8790 if (TREE_CODE (template) == IDENTIFIER_NODE
)
8791 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
8792 else if (DECL_CLASS_TEMPLATE_P (template)
8793 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8795 bool entering_scope
;
8796 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8797 template (rather than some instantiation thereof) only if
8798 is not nested within some other construct. For example, in
8799 "template <typename T> void f(T) { A<T>::", A<T> is just an
8800 instantiation of A. */
8801 entering_scope
= (template_parm_scope_p ()
8802 && cp_lexer_next_token_is (parser
->lexer
,
8805 = finish_template_type (template, arguments
, entering_scope
);
8809 /* If it's not a class-template or a template-template, it should be
8810 a function-template. */
8811 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8812 || TREE_CODE (template) == OVERLOAD
8813 || BASELINK_P (template)));
8815 template_id
= lookup_template_function (template, arguments
);
8818 /* Retrieve any deferred checks. Do not pop this access checks yet
8819 so the memory will not be reclaimed during token replacing below. */
8820 access_check
= get_deferred_access_checks ();
8822 /* If parsing tentatively, replace the sequence of tokens that makes
8823 up the template-id with a CPP_TEMPLATE_ID token. That way,
8824 should we re-parse the token stream, we will not have to repeat
8825 the effort required to do the parse, nor will we issue duplicate
8826 error messages about problems during instantiation of the
8830 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
8832 /* Reset the contents of the START_OF_ID token. */
8833 token
->type
= CPP_TEMPLATE_ID
;
8834 token
->value
= build_tree_list (access_check
, template_id
);
8835 token
->keyword
= RID_MAX
;
8837 /* Purge all subsequent tokens. */
8838 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
8840 /* ??? Can we actually assume that, if template_id ==
8841 error_mark_node, we will have issued a diagnostic to the
8842 user, as opposed to simply marking the tentative parse as
8844 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
8845 error ("parse error in template argument list");
8848 pop_deferring_access_checks ();
8852 /* Parse a template-name.
8857 The standard should actually say:
8861 operator-function-id
8863 A defect report has been filed about this issue.
8865 A conversion-function-id cannot be a template name because they cannot
8866 be part of a template-id. In fact, looking at this code:
8870 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8871 It is impossible to call a templated conversion-function-id with an
8872 explicit argument list, since the only allowed template parameter is
8873 the type to which it is converting.
8875 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8876 `template' keyword, in a construction like:
8880 In that case `f' is taken to be a template-name, even though there
8881 is no way of knowing for sure.
8883 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8884 name refers to a set of overloaded functions, at least one of which
8885 is a template, or an IDENTIFIER_NODE with the name of the template,
8886 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8887 names are looked up inside uninstantiated templates. */
8890 cp_parser_template_name (cp_parser
* parser
,
8891 bool template_keyword_p
,
8892 bool check_dependency_p
,
8893 bool is_declaration
,
8894 bool *is_identifier
)
8900 /* If the next token is `operator', then we have either an
8901 operator-function-id or a conversion-function-id. */
8902 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
8904 /* We don't know whether we're looking at an
8905 operator-function-id or a conversion-function-id. */
8906 cp_parser_parse_tentatively (parser
);
8907 /* Try an operator-function-id. */
8908 identifier
= cp_parser_operator_function_id (parser
);
8909 /* If that didn't work, try a conversion-function-id. */
8910 if (!cp_parser_parse_definitely (parser
))
8912 cp_parser_error (parser
, "expected template-name");
8913 return error_mark_node
;
8916 /* Look for the identifier. */
8918 identifier
= cp_parser_identifier (parser
);
8920 /* If we didn't find an identifier, we don't have a template-id. */
8921 if (identifier
== error_mark_node
)
8922 return error_mark_node
;
8924 /* If the name immediately followed the `template' keyword, then it
8925 is a template-name. However, if the next token is not `<', then
8926 we do not treat it as a template-name, since it is not being used
8927 as part of a template-id. This enables us to handle constructs
8930 template <typename T> struct S { S(); };
8931 template <typename T> S<T>::S();
8933 correctly. We would treat `S' as a template -- if it were `S<T>'
8934 -- but we do not if there is no `<'. */
8936 if (processing_template_decl
8937 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
8939 /* In a declaration, in a dependent context, we pretend that the
8940 "template" keyword was present in order to improve error
8941 recovery. For example, given:
8943 template <typename T> void f(T::X<int>);
8945 we want to treat "X<int>" as a template-id. */
8947 && !template_keyword_p
8948 && parser
->scope
&& TYPE_P (parser
->scope
)
8949 && check_dependency_p
8950 && dependent_type_p (parser
->scope
)
8951 /* Do not do this for dtors (or ctors), since they never
8952 need the template keyword before their name. */
8953 && !constructor_name_p (identifier
, parser
->scope
))
8955 cp_token_position start
= 0;
8957 /* Explain what went wrong. */
8958 error ("non-template %qD used as template", identifier
);
8959 inform ("use %<%T::template %D%> to indicate that it is a template",
8960 parser
->scope
, identifier
);
8961 /* If parsing tentatively, find the location of the "<" token. */
8962 if (cp_parser_simulate_error (parser
))
8963 start
= cp_lexer_token_position (parser
->lexer
, true);
8964 /* Parse the template arguments so that we can issue error
8965 messages about them. */
8966 cp_lexer_consume_token (parser
->lexer
);
8967 cp_parser_enclosed_template_argument_list (parser
);
8968 /* Skip tokens until we find a good place from which to
8969 continue parsing. */
8970 cp_parser_skip_to_closing_parenthesis (parser
,
8971 /*recovering=*/true,
8973 /*consume_paren=*/false);
8974 /* If parsing tentatively, permanently remove the
8975 template argument list. That will prevent duplicate
8976 error messages from being issued about the missing
8977 "template" keyword. */
8979 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
8981 *is_identifier
= true;
8985 /* If the "template" keyword is present, then there is generally
8986 no point in doing name-lookup, so we just return IDENTIFIER.
8987 But, if the qualifying scope is non-dependent then we can
8988 (and must) do name-lookup normally. */
8989 if (template_keyword_p
8991 || (TYPE_P (parser
->scope
)
8992 && dependent_type_p (parser
->scope
))))
8996 /* Look up the name. */
8997 decl
= cp_parser_lookup_name (parser
, identifier
,
8999 /*is_template=*/false,
9000 /*is_namespace=*/false,
9002 /*ambiguous_decls=*/NULL
);
9003 decl
= maybe_get_template_decl_from_type_decl (decl
);
9005 /* If DECL is a template, then the name was a template-name. */
9006 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9010 tree fn
= NULL_TREE
;
9012 /* The standard does not explicitly indicate whether a name that
9013 names a set of overloaded declarations, some of which are
9014 templates, is a template-name. However, such a name should
9015 be a template-name; otherwise, there is no way to form a
9016 template-id for the overloaded templates. */
9017 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
9018 if (TREE_CODE (fns
) == OVERLOAD
)
9019 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
9020 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
9025 /* The name does not name a template. */
9026 cp_parser_error (parser
, "expected template-name");
9027 return error_mark_node
;
9031 /* If DECL is dependent, and refers to a function, then just return
9032 its name; we will look it up again during template instantiation. */
9033 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
9035 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
9036 if (TYPE_P (scope
) && dependent_type_p (scope
))
9043 /* Parse a template-argument-list.
9045 template-argument-list:
9047 template-argument-list , template-argument
9049 Returns a TREE_VEC containing the arguments. */
9052 cp_parser_template_argument_list (cp_parser
* parser
)
9054 tree fixed_args
[10];
9055 unsigned n_args
= 0;
9056 unsigned alloced
= 10;
9057 tree
*arg_ary
= fixed_args
;
9059 bool saved_in_template_argument_list_p
;
9061 bool saved_non_ice_p
;
9063 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
9064 parser
->in_template_argument_list_p
= true;
9065 /* Even if the template-id appears in an integral
9066 constant-expression, the contents of the argument list do
9068 saved_ice_p
= parser
->integral_constant_expression_p
;
9069 parser
->integral_constant_expression_p
= false;
9070 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
9071 parser
->non_integral_constant_expression_p
= false;
9072 /* Parse the arguments. */
9078 /* Consume the comma. */
9079 cp_lexer_consume_token (parser
->lexer
);
9081 /* Parse the template-argument. */
9082 argument
= cp_parser_template_argument (parser
);
9083 if (n_args
== alloced
)
9087 if (arg_ary
== fixed_args
)
9089 arg_ary
= XNEWVEC (tree
, alloced
);
9090 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
9093 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
9095 arg_ary
[n_args
++] = argument
;
9097 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
9099 vec
= make_tree_vec (n_args
);
9102 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
9104 if (arg_ary
!= fixed_args
)
9106 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
9107 parser
->integral_constant_expression_p
= saved_ice_p
;
9108 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
9112 /* Parse a template-argument.
9115 assignment-expression
9119 The representation is that of an assignment-expression, type-id, or
9120 id-expression -- except that the qualified id-expression is
9121 evaluated, so that the value returned is either a DECL or an
9124 Although the standard says "assignment-expression", it forbids
9125 throw-expressions or assignments in the template argument.
9126 Therefore, we use "conditional-expression" instead. */
9129 cp_parser_template_argument (cp_parser
* parser
)
9134 bool maybe_type_id
= false;
9138 /* There's really no way to know what we're looking at, so we just
9139 try each alternative in order.
9143 In a template-argument, an ambiguity between a type-id and an
9144 expression is resolved to a type-id, regardless of the form of
9145 the corresponding template-parameter.
9147 Therefore, we try a type-id first. */
9148 cp_parser_parse_tentatively (parser
);
9149 argument
= cp_parser_type_id (parser
);
9150 /* If there was no error parsing the type-id but the next token is a '>>',
9151 we probably found a typo for '> >'. But there are type-id which are
9152 also valid expressions. For instance:
9154 struct X { int operator >> (int); };
9155 template <int V> struct Foo {};
9158 Here 'X()' is a valid type-id of a function type, but the user just
9159 wanted to write the expression "X() >> 5". Thus, we remember that we
9160 found a valid type-id, but we still try to parse the argument as an
9161 expression to see what happens. */
9162 if (!cp_parser_error_occurred (parser
)
9163 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
9165 maybe_type_id
= true;
9166 cp_parser_abort_tentative_parse (parser
);
9170 /* If the next token isn't a `,' or a `>', then this argument wasn't
9171 really finished. This means that the argument is not a valid
9173 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9174 cp_parser_error (parser
, "expected template-argument");
9175 /* If that worked, we're done. */
9176 if (cp_parser_parse_definitely (parser
))
9179 /* We're still not sure what the argument will be. */
9180 cp_parser_parse_tentatively (parser
);
9181 /* Try a template. */
9182 argument
= cp_parser_id_expression (parser
,
9183 /*template_keyword_p=*/false,
9184 /*check_dependency_p=*/true,
9186 /*declarator_p=*/false,
9187 /*optional_p=*/false);
9188 /* If the next token isn't a `,' or a `>', then this argument wasn't
9190 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9191 cp_parser_error (parser
, "expected template-argument");
9192 if (!cp_parser_error_occurred (parser
))
9194 /* Figure out what is being referred to. If the id-expression
9195 was for a class template specialization, then we will have a
9196 TYPE_DECL at this point. There is no need to do name lookup
9197 at this point in that case. */
9198 if (TREE_CODE (argument
) != TYPE_DECL
)
9199 argument
= cp_parser_lookup_name (parser
, argument
,
9201 /*is_template=*/template_p
,
9202 /*is_namespace=*/false,
9203 /*check_dependency=*/true,
9204 /*ambiguous_decls=*/NULL
);
9205 if (TREE_CODE (argument
) != TEMPLATE_DECL
9206 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
9207 cp_parser_error (parser
, "expected template-name");
9209 if (cp_parser_parse_definitely (parser
))
9211 /* It must be a non-type argument. There permitted cases are given
9212 in [temp.arg.nontype]:
9214 -- an integral constant-expression of integral or enumeration
9217 -- the name of a non-type template-parameter; or
9219 -- the name of an object or function with external linkage...
9221 -- the address of an object or function with external linkage...
9223 -- a pointer to member... */
9224 /* Look for a non-type template parameter. */
9225 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9227 cp_parser_parse_tentatively (parser
);
9228 argument
= cp_parser_primary_expression (parser
,
9231 /*template_arg_p=*/true,
9233 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
9234 || !cp_parser_next_token_ends_template_argument_p (parser
))
9235 cp_parser_simulate_error (parser
);
9236 if (cp_parser_parse_definitely (parser
))
9240 /* If the next token is "&", the argument must be the address of an
9241 object or function with external linkage. */
9242 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
9244 cp_lexer_consume_token (parser
->lexer
);
9245 /* See if we might have an id-expression. */
9246 token
= cp_lexer_peek_token (parser
->lexer
);
9247 if (token
->type
== CPP_NAME
9248 || token
->keyword
== RID_OPERATOR
9249 || token
->type
== CPP_SCOPE
9250 || token
->type
== CPP_TEMPLATE_ID
9251 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
9253 cp_parser_parse_tentatively (parser
);
9254 argument
= cp_parser_primary_expression (parser
,
9257 /*template_arg_p=*/true,
9259 if (cp_parser_error_occurred (parser
)
9260 || !cp_parser_next_token_ends_template_argument_p (parser
))
9261 cp_parser_abort_tentative_parse (parser
);
9264 if (TREE_CODE (argument
) == INDIRECT_REF
)
9266 gcc_assert (REFERENCE_REF_P (argument
));
9267 argument
= TREE_OPERAND (argument
, 0);
9270 if (TREE_CODE (argument
) == BASELINK
)
9271 /* We don't need the information about what class was used
9272 to name the overloaded functions. */
9273 argument
= BASELINK_FUNCTIONS (argument
);
9275 if (TREE_CODE (argument
) == VAR_DECL
)
9277 /* A variable without external linkage might still be a
9278 valid constant-expression, so no error is issued here
9279 if the external-linkage check fails. */
9280 if (!DECL_EXTERNAL_LINKAGE_P (argument
))
9281 cp_parser_simulate_error (parser
);
9283 else if (is_overloaded_fn (argument
))
9284 /* All overloaded functions are allowed; if the external
9285 linkage test does not pass, an error will be issued
9289 && (TREE_CODE (argument
) == OFFSET_REF
9290 || TREE_CODE (argument
) == SCOPE_REF
))
9291 /* A pointer-to-member. */
9293 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
9296 cp_parser_simulate_error (parser
);
9298 if (cp_parser_parse_definitely (parser
))
9301 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
9306 /* If the argument started with "&", there are no other valid
9307 alternatives at this point. */
9310 cp_parser_error (parser
, "invalid non-type template argument");
9311 return error_mark_node
;
9314 /* If the argument wasn't successfully parsed as a type-id followed
9315 by '>>', the argument can only be a constant expression now.
9316 Otherwise, we try parsing the constant-expression tentatively,
9317 because the argument could really be a type-id. */
9319 cp_parser_parse_tentatively (parser
);
9320 argument
= cp_parser_constant_expression (parser
,
9321 /*allow_non_constant_p=*/false,
9322 /*non_constant_p=*/NULL
);
9323 argument
= fold_non_dependent_expr (argument
);
9326 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9327 cp_parser_error (parser
, "expected template-argument");
9328 if (cp_parser_parse_definitely (parser
))
9330 /* We did our best to parse the argument as a non type-id, but that
9331 was the only alternative that matched (albeit with a '>' after
9332 it). We can assume it's just a typo from the user, and a
9333 diagnostic will then be issued. */
9334 return cp_parser_type_id (parser
);
9337 /* Parse an explicit-instantiation.
9339 explicit-instantiation:
9340 template declaration
9342 Although the standard says `declaration', what it really means is:
9344 explicit-instantiation:
9345 template decl-specifier-seq [opt] declarator [opt] ;
9347 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9348 supposed to be allowed. A defect report has been filed about this
9353 explicit-instantiation:
9354 storage-class-specifier template
9355 decl-specifier-seq [opt] declarator [opt] ;
9356 function-specifier template
9357 decl-specifier-seq [opt] declarator [opt] ; */
9360 cp_parser_explicit_instantiation (cp_parser
* parser
)
9362 int declares_class_or_enum
;
9363 cp_decl_specifier_seq decl_specifiers
;
9364 tree extension_specifier
= NULL_TREE
;
9366 /* Look for an (optional) storage-class-specifier or
9367 function-specifier. */
9368 if (cp_parser_allow_gnu_extensions_p (parser
))
9371 = cp_parser_storage_class_specifier_opt (parser
);
9372 if (!extension_specifier
)
9374 = cp_parser_function_specifier_opt (parser
,
9375 /*decl_specs=*/NULL
);
9378 /* Look for the `template' keyword. */
9379 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9380 /* Let the front end know that we are processing an explicit
9382 begin_explicit_instantiation ();
9383 /* [temp.explicit] says that we are supposed to ignore access
9384 control while processing explicit instantiation directives. */
9385 push_deferring_access_checks (dk_no_check
);
9386 /* Parse a decl-specifier-seq. */
9387 cp_parser_decl_specifier_seq (parser
,
9388 CP_PARSER_FLAGS_OPTIONAL
,
9390 &declares_class_or_enum
);
9391 /* If there was exactly one decl-specifier, and it declared a class,
9392 and there's no declarator, then we have an explicit type
9394 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
9398 type
= check_tag_decl (&decl_specifiers
);
9399 /* Turn access control back on for names used during
9400 template instantiation. */
9401 pop_deferring_access_checks ();
9403 do_type_instantiation (type
, extension_specifier
,
9404 /*complain=*/tf_error
);
9408 cp_declarator
*declarator
;
9411 /* Parse the declarator. */
9413 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9414 /*ctor_dtor_or_conv_p=*/NULL
,
9415 /*parenthesized_p=*/NULL
,
9416 /*member_p=*/false);
9417 if (declares_class_or_enum
& 2)
9418 cp_parser_check_for_definition_in_return_type (declarator
,
9419 decl_specifiers
.type
);
9420 if (declarator
!= cp_error_declarator
)
9422 decl
= grokdeclarator (declarator
, &decl_specifiers
,
9423 NORMAL
, 0, &decl_specifiers
.attributes
);
9424 /* Turn access control back on for names used during
9425 template instantiation. */
9426 pop_deferring_access_checks ();
9427 /* Do the explicit instantiation. */
9428 do_decl_instantiation (decl
, extension_specifier
);
9432 pop_deferring_access_checks ();
9433 /* Skip the body of the explicit instantiation. */
9434 cp_parser_skip_to_end_of_statement (parser
);
9437 /* We're done with the instantiation. */
9438 end_explicit_instantiation ();
9440 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9443 /* Parse an explicit-specialization.
9445 explicit-specialization:
9446 template < > declaration
9448 Although the standard says `declaration', what it really means is:
9450 explicit-specialization:
9451 template <> decl-specifier [opt] init-declarator [opt] ;
9452 template <> function-definition
9453 template <> explicit-specialization
9454 template <> template-declaration */
9457 cp_parser_explicit_specialization (cp_parser
* parser
)
9460 /* Look for the `template' keyword. */
9461 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9462 /* Look for the `<'. */
9463 cp_parser_require (parser
, CPP_LESS
, "`<'");
9464 /* Look for the `>'. */
9465 cp_parser_require (parser
, CPP_GREATER
, "`>'");
9466 /* We have processed another parameter list. */
9467 ++parser
->num_template_parameter_lists
;
9470 A template ... explicit specialization ... shall not have C
9472 if (current_lang_name
== lang_name_c
)
9474 error ("template specialization with C linkage");
9475 /* Give it C++ linkage to avoid confusing other parts of the
9477 push_lang_context (lang_name_cplusplus
);
9478 need_lang_pop
= true;
9481 need_lang_pop
= false;
9482 /* Let the front end know that we are beginning a specialization. */
9483 begin_specialization ();
9484 /* If the next keyword is `template', we need to figure out whether
9485 or not we're looking a template-declaration. */
9486 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
9488 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
9489 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
9490 cp_parser_template_declaration_after_export (parser
,
9491 /*member_p=*/false);
9493 cp_parser_explicit_specialization (parser
);
9496 /* Parse the dependent declaration. */
9497 cp_parser_single_declaration (parser
,
9498 /*checks=*/NULL_TREE
,
9501 /* We're done with the specialization. */
9502 end_specialization ();
9503 /* For the erroneous case of a template with C linkage, we pushed an
9504 implicit C++ linkage scope; exit that scope now. */
9506 pop_lang_context ();
9507 /* We're done with this parameter list. */
9508 --parser
->num_template_parameter_lists
;
9511 /* Parse a type-specifier.
9514 simple-type-specifier
9517 elaborated-type-specifier
9525 Returns a representation of the type-specifier. For a
9526 class-specifier, enum-specifier, or elaborated-type-specifier, a
9527 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9529 The parser flags FLAGS is used to control type-specifier parsing.
9531 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9532 in a decl-specifier-seq.
9534 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9535 class-specifier, enum-specifier, or elaborated-type-specifier, then
9536 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9537 if a type is declared; 2 if it is defined. Otherwise, it is set to
9540 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9541 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9545 cp_parser_type_specifier (cp_parser
* parser
,
9546 cp_parser_flags flags
,
9547 cp_decl_specifier_seq
*decl_specs
,
9548 bool is_declaration
,
9549 int* declares_class_or_enum
,
9550 bool* is_cv_qualifier
)
9552 tree type_spec
= NULL_TREE
;
9555 cp_decl_spec ds
= ds_last
;
9557 /* Assume this type-specifier does not declare a new type. */
9558 if (declares_class_or_enum
)
9559 *declares_class_or_enum
= 0;
9560 /* And that it does not specify a cv-qualifier. */
9561 if (is_cv_qualifier
)
9562 *is_cv_qualifier
= false;
9563 /* Peek at the next token. */
9564 token
= cp_lexer_peek_token (parser
->lexer
);
9566 /* If we're looking at a keyword, we can use that to guide the
9567 production we choose. */
9568 keyword
= token
->keyword
;
9572 /* Look for the enum-specifier. */
9573 type_spec
= cp_parser_enum_specifier (parser
);
9574 /* If that worked, we're done. */
9577 if (declares_class_or_enum
)
9578 *declares_class_or_enum
= 2;
9580 cp_parser_set_decl_spec_type (decl_specs
,
9582 /*user_defined_p=*/true);
9586 goto elaborated_type_specifier
;
9588 /* Any of these indicate either a class-specifier, or an
9589 elaborated-type-specifier. */
9593 /* Parse tentatively so that we can back up if we don't find a
9595 cp_parser_parse_tentatively (parser
);
9596 /* Look for the class-specifier. */
9597 type_spec
= cp_parser_class_specifier (parser
);
9598 /* If that worked, we're done. */
9599 if (cp_parser_parse_definitely (parser
))
9601 if (declares_class_or_enum
)
9602 *declares_class_or_enum
= 2;
9604 cp_parser_set_decl_spec_type (decl_specs
,
9606 /*user_defined_p=*/true);
9611 elaborated_type_specifier
:
9612 /* We're declaring (not defining) a class or enum. */
9613 if (declares_class_or_enum
)
9614 *declares_class_or_enum
= 1;
9618 /* Look for an elaborated-type-specifier. */
9620 = (cp_parser_elaborated_type_specifier
9622 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
9625 cp_parser_set_decl_spec_type (decl_specs
,
9627 /*user_defined_p=*/true);
9632 if (is_cv_qualifier
)
9633 *is_cv_qualifier
= true;
9638 if (is_cv_qualifier
)
9639 *is_cv_qualifier
= true;
9644 if (is_cv_qualifier
)
9645 *is_cv_qualifier
= true;
9649 /* The `__complex__' keyword is a GNU extension. */
9657 /* Handle simple keywords. */
9662 ++decl_specs
->specs
[(int)ds
];
9663 decl_specs
->any_specifiers_p
= true;
9665 return cp_lexer_consume_token (parser
->lexer
)->value
;
9668 /* If we do not already have a type-specifier, assume we are looking
9669 at a simple-type-specifier. */
9670 type_spec
= cp_parser_simple_type_specifier (parser
,
9674 /* If we didn't find a type-specifier, and a type-specifier was not
9675 optional in this context, issue an error message. */
9676 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9678 cp_parser_error (parser
, "expected type specifier");
9679 return error_mark_node
;
9685 /* Parse a simple-type-specifier.
9687 simple-type-specifier:
9688 :: [opt] nested-name-specifier [opt] type-name
9689 :: [opt] nested-name-specifier template template-id
9704 simple-type-specifier:
9705 __typeof__ unary-expression
9706 __typeof__ ( type-id )
9708 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9709 appropriately updated. */
9712 cp_parser_simple_type_specifier (cp_parser
* parser
,
9713 cp_decl_specifier_seq
*decl_specs
,
9714 cp_parser_flags flags
)
9716 tree type
= NULL_TREE
;
9719 /* Peek at the next token. */
9720 token
= cp_lexer_peek_token (parser
->lexer
);
9722 /* If we're looking at a keyword, things are easy. */
9723 switch (token
->keyword
)
9727 decl_specs
->explicit_char_p
= true;
9728 type
= char_type_node
;
9731 type
= wchar_type_node
;
9734 type
= boolean_type_node
;
9738 ++decl_specs
->specs
[(int) ds_short
];
9739 type
= short_integer_type_node
;
9743 decl_specs
->explicit_int_p
= true;
9744 type
= integer_type_node
;
9748 ++decl_specs
->specs
[(int) ds_long
];
9749 type
= long_integer_type_node
;
9753 ++decl_specs
->specs
[(int) ds_signed
];
9754 type
= integer_type_node
;
9758 ++decl_specs
->specs
[(int) ds_unsigned
];
9759 type
= unsigned_type_node
;
9762 type
= float_type_node
;
9765 type
= double_type_node
;
9768 type
= void_type_node
;
9772 /* Consume the `typeof' token. */
9773 cp_lexer_consume_token (parser
->lexer
);
9774 /* Parse the operand to `typeof'. */
9775 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
9776 /* If it is not already a TYPE, take its type. */
9778 type
= finish_typeof (type
);
9781 cp_parser_set_decl_spec_type (decl_specs
, type
,
9782 /*user_defined_p=*/true);
9790 /* If the type-specifier was for a built-in type, we're done. */
9795 /* Record the type. */
9797 && (token
->keyword
!= RID_SIGNED
9798 && token
->keyword
!= RID_UNSIGNED
9799 && token
->keyword
!= RID_SHORT
9800 && token
->keyword
!= RID_LONG
))
9801 cp_parser_set_decl_spec_type (decl_specs
,
9803 /*user_defined=*/false);
9805 decl_specs
->any_specifiers_p
= true;
9807 /* Consume the token. */
9808 id
= cp_lexer_consume_token (parser
->lexer
)->value
;
9810 /* There is no valid C++ program where a non-template type is
9811 followed by a "<". That usually indicates that the user thought
9812 that the type was a template. */
9813 cp_parser_check_for_invalid_template_id (parser
, type
);
9815 return TYPE_NAME (type
);
9818 /* The type-specifier must be a user-defined type. */
9819 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
9824 /* Don't gobble tokens or issue error messages if this is an
9825 optional type-specifier. */
9826 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
9827 cp_parser_parse_tentatively (parser
);
9829 /* Look for the optional `::' operator. */
9831 = (cp_parser_global_scope_opt (parser
,
9832 /*current_scope_valid_p=*/false)
9834 /* Look for the nested-name specifier. */
9836 = (cp_parser_nested_name_specifier_opt (parser
,
9837 /*typename_keyword_p=*/false,
9838 /*check_dependency_p=*/true,
9840 /*is_declaration=*/false)
9842 /* If we have seen a nested-name-specifier, and the next token
9843 is `template', then we are using the template-id production. */
9845 && cp_parser_optional_template_keyword (parser
))
9847 /* Look for the template-id. */
9848 type
= cp_parser_template_id (parser
,
9849 /*template_keyword_p=*/true,
9850 /*check_dependency_p=*/true,
9851 /*is_declaration=*/false);
9852 /* If the template-id did not name a type, we are out of
9854 if (TREE_CODE (type
) != TYPE_DECL
)
9856 cp_parser_error (parser
, "expected template-id for type");
9860 /* Otherwise, look for a type-name. */
9862 type
= cp_parser_type_name (parser
);
9863 /* Keep track of all name-lookups performed in class scopes. */
9867 && TREE_CODE (type
) == TYPE_DECL
9868 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
9869 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
9870 /* If it didn't work out, we don't have a TYPE. */
9871 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
9872 && !cp_parser_parse_definitely (parser
))
9874 if (type
&& decl_specs
)
9875 cp_parser_set_decl_spec_type (decl_specs
, type
,
9876 /*user_defined=*/true);
9879 /* If we didn't get a type-name, issue an error message. */
9880 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9882 cp_parser_error (parser
, "expected type-name");
9883 return error_mark_node
;
9886 /* There is no valid C++ program where a non-template type is
9887 followed by a "<". That usually indicates that the user thought
9888 that the type was a template. */
9889 if (type
&& type
!= error_mark_node
)
9891 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9892 If it is, then the '<'...'>' enclose protocol names rather than
9893 template arguments, and so everything is fine. */
9894 if (c_dialect_objc ()
9895 && (objc_is_id (type
) || objc_is_class_name (type
)))
9897 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
9898 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
9900 /* Clobber the "unqualified" type previously entered into
9901 DECL_SPECS with the new, improved protocol-qualified version. */
9903 decl_specs
->type
= qual_type
;
9908 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
));
9914 /* Parse a type-name.
9927 Returns a TYPE_DECL for the type. */
9930 cp_parser_type_name (cp_parser
* parser
)
9935 /* We can't know yet whether it is a class-name or not. */
9936 cp_parser_parse_tentatively (parser
);
9937 /* Try a class-name. */
9938 type_decl
= cp_parser_class_name (parser
,
9939 /*typename_keyword_p=*/false,
9940 /*template_keyword_p=*/false,
9942 /*check_dependency_p=*/true,
9943 /*class_head_p=*/false,
9944 /*is_declaration=*/false);
9945 /* If it's not a class-name, keep looking. */
9946 if (!cp_parser_parse_definitely (parser
))
9948 /* It must be a typedef-name or an enum-name. */
9949 identifier
= cp_parser_identifier (parser
);
9950 if (identifier
== error_mark_node
)
9951 return error_mark_node
;
9953 /* Look up the type-name. */
9954 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
9956 if (TREE_CODE (type_decl
) != TYPE_DECL
9957 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
9959 /* See if this is an Objective-C type. */
9960 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
9961 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
9963 type_decl
= TYPE_NAME (type
);
9966 /* Issue an error if we did not find a type-name. */
9967 if (TREE_CODE (type_decl
) != TYPE_DECL
)
9969 if (!cp_parser_simulate_error (parser
))
9970 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
9972 type_decl
= error_mark_node
;
9974 /* Remember that the name was used in the definition of the
9975 current class so that we can check later to see if the
9976 meaning would have been different after the class was
9977 entirely defined. */
9978 else if (type_decl
!= error_mark_node
9980 maybe_note_name_used_in_class (identifier
, type_decl
);
9987 /* Parse an elaborated-type-specifier. Note that the grammar given
9988 here incorporates the resolution to DR68.
9990 elaborated-type-specifier:
9991 class-key :: [opt] nested-name-specifier [opt] identifier
9992 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9993 enum :: [opt] nested-name-specifier [opt] identifier
9994 typename :: [opt] nested-name-specifier identifier
9995 typename :: [opt] nested-name-specifier template [opt]
10000 elaborated-type-specifier:
10001 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10002 class-key attributes :: [opt] nested-name-specifier [opt]
10003 template [opt] template-id
10004 enum attributes :: [opt] nested-name-specifier [opt] identifier
10006 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10007 declared `friend'. If IS_DECLARATION is TRUE, then this
10008 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10009 something is being declared.
10011 Returns the TYPE specified. */
10014 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
10016 bool is_declaration
)
10018 enum tag_types tag_type
;
10020 tree type
= NULL_TREE
;
10021 tree attributes
= NULL_TREE
;
10023 /* See if we're looking at the `enum' keyword. */
10024 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
10026 /* Consume the `enum' token. */
10027 cp_lexer_consume_token (parser
->lexer
);
10028 /* Remember that it's an enumeration type. */
10029 tag_type
= enum_type
;
10030 /* Parse the attributes. */
10031 attributes
= cp_parser_attributes_opt (parser
);
10033 /* Or, it might be `typename'. */
10034 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
10037 /* Consume the `typename' token. */
10038 cp_lexer_consume_token (parser
->lexer
);
10039 /* Remember that it's a `typename' type. */
10040 tag_type
= typename_type
;
10041 /* The `typename' keyword is only allowed in templates. */
10042 if (!processing_template_decl
)
10043 pedwarn ("using %<typename%> outside of template");
10045 /* Otherwise it must be a class-key. */
10048 tag_type
= cp_parser_class_key (parser
);
10049 if (tag_type
== none_type
)
10050 return error_mark_node
;
10051 /* Parse the attributes. */
10052 attributes
= cp_parser_attributes_opt (parser
);
10055 /* Look for the `::' operator. */
10056 cp_parser_global_scope_opt (parser
,
10057 /*current_scope_valid_p=*/false);
10058 /* Look for the nested-name-specifier. */
10059 if (tag_type
== typename_type
)
10061 if (!cp_parser_nested_name_specifier (parser
,
10062 /*typename_keyword_p=*/true,
10063 /*check_dependency_p=*/true,
10066 return error_mark_node
;
10069 /* Even though `typename' is not present, the proposed resolution
10070 to Core Issue 180 says that in `class A<T>::B', `B' should be
10071 considered a type-name, even if `A<T>' is dependent. */
10072 cp_parser_nested_name_specifier_opt (parser
,
10073 /*typename_keyword_p=*/true,
10074 /*check_dependency_p=*/true,
10077 /* For everything but enumeration types, consider a template-id. */
10078 /* For an enumeration type, consider only a plain identifier. */
10079 if (tag_type
!= enum_type
)
10081 bool template_p
= false;
10084 /* Allow the `template' keyword. */
10085 template_p
= cp_parser_optional_template_keyword (parser
);
10086 /* If we didn't see `template', we don't know if there's a
10087 template-id or not. */
10089 cp_parser_parse_tentatively (parser
);
10090 /* Parse the template-id. */
10091 decl
= cp_parser_template_id (parser
, template_p
,
10092 /*check_dependency_p=*/true,
10094 /* If we didn't find a template-id, look for an ordinary
10096 if (!template_p
&& !cp_parser_parse_definitely (parser
))
10098 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10099 in effect, then we must assume that, upon instantiation, the
10100 template will correspond to a class. */
10101 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
10102 && tag_type
== typename_type
)
10103 type
= make_typename_type (parser
->scope
, decl
,
10105 /*complain=*/tf_error
);
10107 type
= TREE_TYPE (decl
);
10112 identifier
= cp_parser_identifier (parser
);
10114 if (identifier
== error_mark_node
)
10116 parser
->scope
= NULL_TREE
;
10117 return error_mark_node
;
10120 /* For a `typename', we needn't call xref_tag. */
10121 if (tag_type
== typename_type
10122 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
10123 return cp_parser_make_typename_type (parser
, parser
->scope
,
10125 /* Look up a qualified name in the usual way. */
10130 decl
= cp_parser_lookup_name (parser
, identifier
,
10132 /*is_template=*/false,
10133 /*is_namespace=*/false,
10134 /*check_dependency=*/true,
10135 /*ambiguous_decls=*/NULL
);
10137 /* If we are parsing friend declaration, DECL may be a
10138 TEMPLATE_DECL tree node here. However, we need to check
10139 whether this TEMPLATE_DECL results in valid code. Consider
10140 the following example:
10143 template <class T> class C {};
10146 template <class T> friend class N::C; // #1, valid code
10148 template <class T> class Y {
10149 friend class N::C; // #2, invalid code
10152 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10153 name lookup of `N::C'. We see that friend declaration must
10154 be template for the code to be valid. Note that
10155 processing_template_decl does not work here since it is
10156 always 1 for the above two cases. */
10158 decl
= (cp_parser_maybe_treat_template_as_class
10159 (decl
, /*tag_name_p=*/is_friend
10160 && parser
->num_template_parameter_lists
));
10162 if (TREE_CODE (decl
) != TYPE_DECL
)
10164 cp_parser_diagnose_invalid_type_name (parser
,
10167 return error_mark_node
;
10170 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
10171 check_elaborated_type_specifier
10173 (parser
->num_template_parameter_lists
10174 || DECL_SELF_REFERENCE_P (decl
)));
10176 type
= TREE_TYPE (decl
);
10180 /* An elaborated-type-specifier sometimes introduces a new type and
10181 sometimes names an existing type. Normally, the rule is that it
10182 introduces a new type only if there is not an existing type of
10183 the same name already in scope. For example, given:
10186 void f() { struct S s; }
10188 the `struct S' in the body of `f' is the same `struct S' as in
10189 the global scope; the existing definition is used. However, if
10190 there were no global declaration, this would introduce a new
10191 local class named `S'.
10193 An exception to this rule applies to the following code:
10195 namespace N { struct S; }
10197 Here, the elaborated-type-specifier names a new type
10198 unconditionally; even if there is already an `S' in the
10199 containing scope this declaration names a new type.
10200 This exception only applies if the elaborated-type-specifier
10201 forms the complete declaration:
10205 A declaration consisting solely of `class-key identifier ;' is
10206 either a redeclaration of the name in the current scope or a
10207 forward declaration of the identifier as a class name. It
10208 introduces the name into the current scope.
10210 We are in this situation precisely when the next token is a `;'.
10212 An exception to the exception is that a `friend' declaration does
10213 *not* name a new type; i.e., given:
10215 struct S { friend struct T; };
10217 `T' is not a new type in the scope of `S'.
10219 Also, `new struct S' or `sizeof (struct S)' never results in the
10220 definition of a new type; a new type can only be declared in a
10221 declaration context. */
10227 /* Friends have special name lookup rules. */
10228 ts
= ts_within_enclosing_non_class
;
10229 else if (is_declaration
10230 && cp_lexer_next_token_is (parser
->lexer
,
10232 /* This is a `class-key identifier ;' */
10238 (parser
->num_template_parameter_lists
10239 && (cp_parser_next_token_starts_class_definition_p (parser
)
10240 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
10241 /* An unqualified name was used to reference this type, so
10242 there were no qualifying templates. */
10243 if (!cp_parser_check_template_parameters (parser
,
10244 /*num_templates=*/0))
10245 return error_mark_node
;
10246 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
10250 /* Allow attributes on forward declarations of classes. */
10253 if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
10254 && ! processing_explicit_instantiation
)
10255 warning (OPT_Wattributes
,
10256 "attributes ignored on template instantiation");
10257 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
10258 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
10260 warning (OPT_Wattributes
,
10261 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10264 if (tag_type
!= enum_type
)
10265 cp_parser_check_class_key (tag_type
, type
);
10267 /* A "<" cannot follow an elaborated type specifier. If that
10268 happens, the user was probably trying to form a template-id. */
10269 cp_parser_check_for_invalid_template_id (parser
, type
);
10274 /* Parse an enum-specifier.
10277 enum identifier [opt] { enumerator-list [opt] }
10280 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10283 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10284 if the token stream isn't an enum-specifier after all. */
10287 cp_parser_enum_specifier (cp_parser
* parser
)
10293 /* Parse tentatively so that we can back up if we don't find a
10295 cp_parser_parse_tentatively (parser
);
10297 /* Caller guarantees that the current token is 'enum', an identifier
10298 possibly follows, and the token after that is an opening brace.
10299 If we don't have an identifier, fabricate an anonymous name for
10300 the enumeration being defined. */
10301 cp_lexer_consume_token (parser
->lexer
);
10303 attributes
= cp_parser_attributes_opt (parser
);
10305 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10306 identifier
= cp_parser_identifier (parser
);
10308 identifier
= make_anon_name ();
10310 /* Look for the `{' but don't consume it yet. */
10311 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10312 cp_parser_simulate_error (parser
);
10314 if (!cp_parser_parse_definitely (parser
))
10317 /* Issue an error message if type-definitions are forbidden here. */
10318 cp_parser_check_type_definition (parser
);
10320 /* Create the new type. We do this before consuming the opening brace
10321 so the enum will be recorded as being on the line of its tag (or the
10322 'enum' keyword, if there is no tag). */
10323 type
= start_enum (identifier
);
10325 /* Consume the opening brace. */
10326 cp_lexer_consume_token (parser
->lexer
);
10328 if (type
== error_mark_node
)
10330 cp_parser_skip_to_end_of_block_or_statement (parser
);
10331 return error_mark_node
;
10334 /* If the next token is not '}', then there are some enumerators. */
10335 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
10336 cp_parser_enumerator_list (parser
, type
);
10338 /* Consume the final '}'. */
10339 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10341 /* Look for trailing attributes to apply to this enumeration, and
10342 apply them if appropriate. */
10343 if (cp_parser_allow_gnu_extensions_p (parser
))
10345 tree trailing_attr
= cp_parser_attributes_opt (parser
);
10346 cplus_decl_attributes (&type
,
10348 (int) ATTR_FLAG_TYPE_IN_PLACE
);
10351 /* Finish up the enumeration. */
10352 finish_enum (type
);
10357 /* Parse an enumerator-list. The enumerators all have the indicated
10361 enumerator-definition
10362 enumerator-list , enumerator-definition */
10365 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
10369 /* Parse an enumerator-definition. */
10370 cp_parser_enumerator_definition (parser
, type
);
10372 /* If the next token is not a ',', we've reached the end of
10374 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
10376 /* Otherwise, consume the `,' and keep going. */
10377 cp_lexer_consume_token (parser
->lexer
);
10378 /* If the next token is a `}', there is a trailing comma. */
10379 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
10381 if (pedantic
&& !in_system_header
)
10382 pedwarn ("comma at end of enumerator list");
10388 /* Parse an enumerator-definition. The enumerator has the indicated
10391 enumerator-definition:
10393 enumerator = constant-expression
10399 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
10404 /* Look for the identifier. */
10405 identifier
= cp_parser_identifier (parser
);
10406 if (identifier
== error_mark_node
)
10409 /* If the next token is an '=', then there is an explicit value. */
10410 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10412 /* Consume the `=' token. */
10413 cp_lexer_consume_token (parser
->lexer
);
10414 /* Parse the value. */
10415 value
= cp_parser_constant_expression (parser
,
10416 /*allow_non_constant_p=*/false,
10422 /* Create the enumerator. */
10423 build_enumerator (identifier
, value
, type
);
10426 /* Parse a namespace-name.
10429 original-namespace-name
10432 Returns the NAMESPACE_DECL for the namespace. */
10435 cp_parser_namespace_name (cp_parser
* parser
)
10438 tree namespace_decl
;
10440 /* Get the name of the namespace. */
10441 identifier
= cp_parser_identifier (parser
);
10442 if (identifier
== error_mark_node
)
10443 return error_mark_node
;
10445 /* Look up the identifier in the currently active scope. Look only
10446 for namespaces, due to:
10448 [basic.lookup.udir]
10450 When looking up a namespace-name in a using-directive or alias
10451 definition, only namespace names are considered.
10455 [basic.lookup.qual]
10457 During the lookup of a name preceding the :: scope resolution
10458 operator, object, function, and enumerator names are ignored.
10460 (Note that cp_parser_class_or_namespace_name only calls this
10461 function if the token after the name is the scope resolution
10463 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
10465 /*is_template=*/false,
10466 /*is_namespace=*/true,
10467 /*check_dependency=*/true,
10468 /*ambiguous_decls=*/NULL
);
10469 /* If it's not a namespace, issue an error. */
10470 if (namespace_decl
== error_mark_node
10471 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
10473 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
10474 error ("%qD is not a namespace-name", identifier
);
10475 cp_parser_error (parser
, "expected namespace-name");
10476 namespace_decl
= error_mark_node
;
10479 return namespace_decl
;
10482 /* Parse a namespace-definition.
10484 namespace-definition:
10485 named-namespace-definition
10486 unnamed-namespace-definition
10488 named-namespace-definition:
10489 original-namespace-definition
10490 extension-namespace-definition
10492 original-namespace-definition:
10493 namespace identifier { namespace-body }
10495 extension-namespace-definition:
10496 namespace original-namespace-name { namespace-body }
10498 unnamed-namespace-definition:
10499 namespace { namespace-body } */
10502 cp_parser_namespace_definition (cp_parser
* parser
)
10504 tree identifier
, attribs
;
10506 /* Look for the `namespace' keyword. */
10507 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10509 /* Get the name of the namespace. We do not attempt to distinguish
10510 between an original-namespace-definition and an
10511 extension-namespace-definition at this point. The semantic
10512 analysis routines are responsible for that. */
10513 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10514 identifier
= cp_parser_identifier (parser
);
10516 identifier
= NULL_TREE
;
10518 /* Parse any specified attributes. */
10519 attribs
= cp_parser_attributes_opt (parser
);
10521 /* Look for the `{' to start the namespace. */
10522 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
10523 /* Start the namespace. */
10524 push_namespace_with_attribs (identifier
, attribs
);
10525 /* Parse the body of the namespace. */
10526 cp_parser_namespace_body (parser
);
10527 /* Finish the namespace. */
10529 /* Look for the final `}'. */
10530 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10533 /* Parse a namespace-body.
10536 declaration-seq [opt] */
10539 cp_parser_namespace_body (cp_parser
* parser
)
10541 cp_parser_declaration_seq_opt (parser
);
10544 /* Parse a namespace-alias-definition.
10546 namespace-alias-definition:
10547 namespace identifier = qualified-namespace-specifier ; */
10550 cp_parser_namespace_alias_definition (cp_parser
* parser
)
10553 tree namespace_specifier
;
10555 /* Look for the `namespace' keyword. */
10556 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10557 /* Look for the identifier. */
10558 identifier
= cp_parser_identifier (parser
);
10559 if (identifier
== error_mark_node
)
10561 /* Look for the `=' token. */
10562 cp_parser_require (parser
, CPP_EQ
, "`='");
10563 /* Look for the qualified-namespace-specifier. */
10564 namespace_specifier
10565 = cp_parser_qualified_namespace_specifier (parser
);
10566 /* Look for the `;' token. */
10567 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10569 /* Register the alias in the symbol table. */
10570 do_namespace_alias (identifier
, namespace_specifier
);
10573 /* Parse a qualified-namespace-specifier.
10575 qualified-namespace-specifier:
10576 :: [opt] nested-name-specifier [opt] namespace-name
10578 Returns a NAMESPACE_DECL corresponding to the specified
10582 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
10584 /* Look for the optional `::'. */
10585 cp_parser_global_scope_opt (parser
,
10586 /*current_scope_valid_p=*/false);
10588 /* Look for the optional nested-name-specifier. */
10589 cp_parser_nested_name_specifier_opt (parser
,
10590 /*typename_keyword_p=*/false,
10591 /*check_dependency_p=*/true,
10593 /*is_declaration=*/true);
10595 return cp_parser_namespace_name (parser
);
10598 /* Parse a using-declaration.
10601 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10602 using :: unqualified-id ; */
10605 cp_parser_using_declaration (cp_parser
* parser
)
10608 bool typename_p
= false;
10609 bool global_scope_p
;
10614 /* Look for the `using' keyword. */
10615 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10617 /* Peek at the next token. */
10618 token
= cp_lexer_peek_token (parser
->lexer
);
10619 /* See if it's `typename'. */
10620 if (token
->keyword
== RID_TYPENAME
)
10622 /* Remember that we've seen it. */
10624 /* Consume the `typename' token. */
10625 cp_lexer_consume_token (parser
->lexer
);
10628 /* Look for the optional global scope qualification. */
10630 = (cp_parser_global_scope_opt (parser
,
10631 /*current_scope_valid_p=*/false)
10634 /* If we saw `typename', or didn't see `::', then there must be a
10635 nested-name-specifier present. */
10636 if (typename_p
|| !global_scope_p
)
10637 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
10638 /*check_dependency_p=*/true,
10640 /*is_declaration=*/true);
10641 /* Otherwise, we could be in either of the two productions. In that
10642 case, treat the nested-name-specifier as optional. */
10644 qscope
= cp_parser_nested_name_specifier_opt (parser
,
10645 /*typename_keyword_p=*/false,
10646 /*check_dependency_p=*/true,
10648 /*is_declaration=*/true);
10650 qscope
= global_namespace
;
10652 /* Parse the unqualified-id. */
10653 identifier
= cp_parser_unqualified_id (parser
,
10654 /*template_keyword_p=*/false,
10655 /*check_dependency_p=*/true,
10656 /*declarator_p=*/true,
10657 /*optional_p=*/false);
10659 /* The function we call to handle a using-declaration is different
10660 depending on what scope we are in. */
10661 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
10663 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
10664 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
10665 /* [namespace.udecl]
10667 A using declaration shall not name a template-id. */
10668 error ("a template-id may not appear in a using-declaration");
10671 if (at_class_scope_p ())
10673 /* Create the USING_DECL. */
10674 decl
= do_class_using_decl (parser
->scope
, identifier
);
10675 /* Add it to the list of members in this class. */
10676 finish_member_declaration (decl
);
10680 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
10681 if (decl
== error_mark_node
)
10682 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
10683 else if (!at_namespace_scope_p ())
10684 do_local_using_decl (decl
, qscope
, identifier
);
10686 do_toplevel_using_decl (decl
, qscope
, identifier
);
10690 /* Look for the final `;'. */
10691 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10694 /* Parse a using-directive.
10697 using namespace :: [opt] nested-name-specifier [opt]
10698 namespace-name ; */
10701 cp_parser_using_directive (cp_parser
* parser
)
10703 tree namespace_decl
;
10706 /* Look for the `using' keyword. */
10707 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10708 /* And the `namespace' keyword. */
10709 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10710 /* Look for the optional `::' operator. */
10711 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
10712 /* And the optional nested-name-specifier. */
10713 cp_parser_nested_name_specifier_opt (parser
,
10714 /*typename_keyword_p=*/false,
10715 /*check_dependency_p=*/true,
10717 /*is_declaration=*/true);
10718 /* Get the namespace being used. */
10719 namespace_decl
= cp_parser_namespace_name (parser
);
10720 /* And any specified attributes. */
10721 attribs
= cp_parser_attributes_opt (parser
);
10722 /* Update the symbol table. */
10723 parse_using_directive (namespace_decl
, attribs
);
10724 /* Look for the final `;'. */
10725 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10728 /* Parse an asm-definition.
10731 asm ( string-literal ) ;
10736 asm volatile [opt] ( string-literal ) ;
10737 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10738 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10739 : asm-operand-list [opt] ) ;
10740 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10741 : asm-operand-list [opt]
10742 : asm-operand-list [opt] ) ; */
10745 cp_parser_asm_definition (cp_parser
* parser
)
10748 tree outputs
= NULL_TREE
;
10749 tree inputs
= NULL_TREE
;
10750 tree clobbers
= NULL_TREE
;
10752 bool volatile_p
= false;
10753 bool extended_p
= false;
10755 /* Look for the `asm' keyword. */
10756 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
10757 /* See if the next token is `volatile'. */
10758 if (cp_parser_allow_gnu_extensions_p (parser
)
10759 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
10761 /* Remember that we saw the `volatile' keyword. */
10763 /* Consume the token. */
10764 cp_lexer_consume_token (parser
->lexer
);
10766 /* Look for the opening `('. */
10767 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
10769 /* Look for the string. */
10770 string
= cp_parser_string_literal (parser
, false, false);
10771 if (string
== error_mark_node
)
10773 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10774 /*consume_paren=*/true);
10778 /* If we're allowing GNU extensions, check for the extended assembly
10779 syntax. Unfortunately, the `:' tokens need not be separated by
10780 a space in C, and so, for compatibility, we tolerate that here
10781 too. Doing that means that we have to treat the `::' operator as
10783 if (cp_parser_allow_gnu_extensions_p (parser
)
10784 && at_function_scope_p ()
10785 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
10786 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
10788 bool inputs_p
= false;
10789 bool clobbers_p
= false;
10791 /* The extended syntax was used. */
10794 /* Look for outputs. */
10795 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10797 /* Consume the `:'. */
10798 cp_lexer_consume_token (parser
->lexer
);
10799 /* Parse the output-operands. */
10800 if (cp_lexer_next_token_is_not (parser
->lexer
,
10802 && cp_lexer_next_token_is_not (parser
->lexer
,
10804 && cp_lexer_next_token_is_not (parser
->lexer
,
10806 outputs
= cp_parser_asm_operand_list (parser
);
10808 /* If the next token is `::', there are no outputs, and the
10809 next token is the beginning of the inputs. */
10810 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10811 /* The inputs are coming next. */
10814 /* Look for inputs. */
10816 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10818 /* Consume the `:' or `::'. */
10819 cp_lexer_consume_token (parser
->lexer
);
10820 /* Parse the output-operands. */
10821 if (cp_lexer_next_token_is_not (parser
->lexer
,
10823 && cp_lexer_next_token_is_not (parser
->lexer
,
10825 inputs
= cp_parser_asm_operand_list (parser
);
10827 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10828 /* The clobbers are coming next. */
10831 /* Look for clobbers. */
10833 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10835 /* Consume the `:' or `::'. */
10836 cp_lexer_consume_token (parser
->lexer
);
10837 /* Parse the clobbers. */
10838 if (cp_lexer_next_token_is_not (parser
->lexer
,
10840 clobbers
= cp_parser_asm_clobber_list (parser
);
10843 /* Look for the closing `)'. */
10844 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
10845 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10846 /*consume_paren=*/true);
10847 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10849 /* Create the ASM_EXPR. */
10850 if (at_function_scope_p ())
10852 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
10854 /* If the extended syntax was not used, mark the ASM_EXPR. */
10857 tree temp
= asm_stmt
;
10858 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
10859 temp
= TREE_OPERAND (temp
, 0);
10861 ASM_INPUT_P (temp
) = 1;
10865 cgraph_add_asm_node (string
);
10868 /* Declarators [gram.dcl.decl] */
10870 /* Parse an init-declarator.
10873 declarator initializer [opt]
10878 declarator asm-specification [opt] attributes [opt] initializer [opt]
10880 function-definition:
10881 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10883 decl-specifier-seq [opt] declarator function-try-block
10887 function-definition:
10888 __extension__ function-definition
10890 The DECL_SPECIFIERS apply to this declarator. Returns a
10891 representation of the entity declared. If MEMBER_P is TRUE, then
10892 this declarator appears in a class scope. The new DECL created by
10893 this declarator is returned.
10895 The CHECKS are access checks that should be performed once we know
10896 what entity is being declared (and, therefore, what classes have
10899 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10900 for a function-definition here as well. If the declarator is a
10901 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10902 be TRUE upon return. By that point, the function-definition will
10903 have been completely parsed.
10905 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10909 cp_parser_init_declarator (cp_parser
* parser
,
10910 cp_decl_specifier_seq
*decl_specifiers
,
10912 bool function_definition_allowed_p
,
10914 int declares_class_or_enum
,
10915 bool* function_definition_p
)
10918 cp_declarator
*declarator
;
10919 tree prefix_attributes
;
10921 tree asm_specification
;
10923 tree decl
= NULL_TREE
;
10925 bool is_initialized
;
10926 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
10927 initialized with "= ..", CPP_OPEN_PAREN if initialized with
10929 enum cpp_ttype initialization_kind
;
10930 bool is_parenthesized_init
= false;
10931 bool is_non_constant_init
;
10932 int ctor_dtor_or_conv_p
;
10934 tree pushed_scope
= NULL
;
10936 /* Gather the attributes that were provided with the
10937 decl-specifiers. */
10938 prefix_attributes
= decl_specifiers
->attributes
;
10940 /* Assume that this is not the declarator for a function
10942 if (function_definition_p
)
10943 *function_definition_p
= false;
10945 /* Defer access checks while parsing the declarator; we cannot know
10946 what names are accessible until we know what is being
10948 resume_deferring_access_checks ();
10950 /* Parse the declarator. */
10952 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10953 &ctor_dtor_or_conv_p
,
10954 /*parenthesized_p=*/NULL
,
10955 /*member_p=*/false);
10956 /* Gather up the deferred checks. */
10957 stop_deferring_access_checks ();
10959 /* If the DECLARATOR was erroneous, there's no need to go
10961 if (declarator
== cp_error_declarator
)
10962 return error_mark_node
;
10964 if (declares_class_or_enum
& 2)
10965 cp_parser_check_for_definition_in_return_type (declarator
,
10966 decl_specifiers
->type
);
10968 /* Figure out what scope the entity declared by the DECLARATOR is
10969 located in. `grokdeclarator' sometimes changes the scope, so
10970 we compute it now. */
10971 scope
= get_scope_of_declarator (declarator
);
10973 /* If we're allowing GNU extensions, look for an asm-specification
10975 if (cp_parser_allow_gnu_extensions_p (parser
))
10977 /* Look for an asm-specification. */
10978 asm_specification
= cp_parser_asm_specification_opt (parser
);
10979 /* And attributes. */
10980 attributes
= cp_parser_attributes_opt (parser
);
10984 asm_specification
= NULL_TREE
;
10985 attributes
= NULL_TREE
;
10988 /* Peek at the next token. */
10989 token
= cp_lexer_peek_token (parser
->lexer
);
10990 /* Check to see if the token indicates the start of a
10991 function-definition. */
10992 if (cp_parser_token_starts_function_definition_p (token
))
10994 if (!function_definition_allowed_p
)
10996 /* If a function-definition should not appear here, issue an
10998 cp_parser_error (parser
,
10999 "a function-definition is not allowed here");
11000 return error_mark_node
;
11004 /* Neither attributes nor an asm-specification are allowed
11005 on a function-definition. */
11006 if (asm_specification
)
11007 error ("an asm-specification is not allowed on a function-definition");
11009 error ("attributes are not allowed on a function-definition");
11010 /* This is a function-definition. */
11011 *function_definition_p
= true;
11013 /* Parse the function definition. */
11015 decl
= cp_parser_save_member_function_body (parser
,
11018 prefix_attributes
);
11021 = (cp_parser_function_definition_from_specifiers_and_declarator
11022 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
11030 Only in function declarations for constructors, destructors, and
11031 type conversions can the decl-specifier-seq be omitted.
11033 We explicitly postpone this check past the point where we handle
11034 function-definitions because we tolerate function-definitions
11035 that are missing their return types in some modes. */
11036 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
11038 cp_parser_error (parser
,
11039 "expected constructor, destructor, or type conversion");
11040 return error_mark_node
;
11043 /* An `=' or an `(' indicates an initializer. */
11044 if (token
->type
== CPP_EQ
11045 || token
->type
== CPP_OPEN_PAREN
)
11047 is_initialized
= true;
11048 initialization_kind
= token
->type
;
11052 /* If the init-declarator isn't initialized and isn't followed by a
11053 `,' or `;', it's not a valid init-declarator. */
11054 if (token
->type
!= CPP_COMMA
11055 && token
->type
!= CPP_SEMICOLON
)
11057 cp_parser_error (parser
, "expected initializer");
11058 return error_mark_node
;
11060 is_initialized
= false;
11061 initialization_kind
= CPP_EOF
;
11064 /* Because start_decl has side-effects, we should only call it if we
11065 know we're going ahead. By this point, we know that we cannot
11066 possibly be looking at any other construct. */
11067 cp_parser_commit_to_tentative_parse (parser
);
11069 /* If the decl specifiers were bad, issue an error now that we're
11070 sure this was intended to be a declarator. Then continue
11071 declaring the variable(s), as int, to try to cut down on further
11073 if (decl_specifiers
->any_specifiers_p
11074 && decl_specifiers
->type
== error_mark_node
)
11076 cp_parser_error (parser
, "invalid type in declaration");
11077 decl_specifiers
->type
= integer_type_node
;
11080 /* Check to see whether or not this declaration is a friend. */
11081 friend_p
= cp_parser_friend_p (decl_specifiers
);
11083 /* Check that the number of template-parameter-lists is OK. */
11084 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
11085 return error_mark_node
;
11087 /* Enter the newly declared entry in the symbol table. If we're
11088 processing a declaration in a class-specifier, we wait until
11089 after processing the initializer. */
11092 if (parser
->in_unbraced_linkage_specification_p
)
11093 decl_specifiers
->storage_class
= sc_extern
;
11094 decl
= start_decl (declarator
, decl_specifiers
,
11095 is_initialized
, attributes
, prefix_attributes
,
11099 /* Enter the SCOPE. That way unqualified names appearing in the
11100 initializer will be looked up in SCOPE. */
11101 pushed_scope
= push_scope (scope
);
11103 /* Perform deferred access control checks, now that we know in which
11104 SCOPE the declared entity resides. */
11105 if (!member_p
&& decl
)
11107 tree saved_current_function_decl
= NULL_TREE
;
11109 /* If the entity being declared is a function, pretend that we
11110 are in its scope. If it is a `friend', it may have access to
11111 things that would not otherwise be accessible. */
11112 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11114 saved_current_function_decl
= current_function_decl
;
11115 current_function_decl
= decl
;
11118 /* Perform access checks for template parameters. */
11119 cp_parser_perform_template_parameter_access_checks (checks
);
11121 /* Perform the access control checks for the declarator and the
11122 the decl-specifiers. */
11123 perform_deferred_access_checks ();
11125 /* Restore the saved value. */
11126 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11127 current_function_decl
= saved_current_function_decl
;
11130 /* Parse the initializer. */
11131 initializer
= NULL_TREE
;
11132 is_parenthesized_init
= false;
11133 is_non_constant_init
= true;
11134 if (is_initialized
)
11136 if (declarator
->kind
== cdk_function
11137 && declarator
->declarator
->kind
== cdk_id
11138 && initialization_kind
== CPP_EQ
)
11139 initializer
= cp_parser_pure_specifier (parser
);
11141 initializer
= cp_parser_initializer (parser
,
11142 &is_parenthesized_init
,
11143 &is_non_constant_init
);
11146 /* The old parser allows attributes to appear after a parenthesized
11147 initializer. Mark Mitchell proposed removing this functionality
11148 on the GCC mailing lists on 2002-08-13. This parser accepts the
11149 attributes -- but ignores them. */
11150 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
11151 if (cp_parser_attributes_opt (parser
))
11152 warning (OPT_Wattributes
,
11153 "attributes after parenthesized initializer ignored");
11155 /* For an in-class declaration, use `grokfield' to create the
11161 pop_scope (pushed_scope
);
11162 pushed_scope
= false;
11164 decl
= grokfield (declarator
, decl_specifiers
,
11165 initializer
, !is_non_constant_init
,
11166 /*asmspec=*/NULL_TREE
,
11167 prefix_attributes
);
11168 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
11169 cp_parser_save_default_args (parser
, decl
);
11172 /* Finish processing the declaration. But, skip friend
11174 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
11176 cp_finish_decl (decl
,
11177 initializer
, !is_non_constant_init
,
11179 /* If the initializer is in parentheses, then this is
11180 a direct-initialization, which means that an
11181 `explicit' constructor is OK. Otherwise, an
11182 `explicit' constructor cannot be used. */
11183 ((is_parenthesized_init
|| !is_initialized
)
11184 ? 0 : LOOKUP_ONLYCONVERTING
));
11186 if (!friend_p
&& pushed_scope
)
11187 pop_scope (pushed_scope
);
11192 /* Parse a declarator.
11196 ptr-operator declarator
11198 abstract-declarator:
11199 ptr-operator abstract-declarator [opt]
11200 direct-abstract-declarator
11205 attributes [opt] direct-declarator
11206 attributes [opt] ptr-operator declarator
11208 abstract-declarator:
11209 attributes [opt] ptr-operator abstract-declarator [opt]
11210 attributes [opt] direct-abstract-declarator
11212 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11213 detect constructor, destructor or conversion operators. It is set
11214 to -1 if the declarator is a name, and +1 if it is a
11215 function. Otherwise it is set to zero. Usually you just want to
11216 test for >0, but internally the negative value is used.
11218 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11219 a decl-specifier-seq unless it declares a constructor, destructor,
11220 or conversion. It might seem that we could check this condition in
11221 semantic analysis, rather than parsing, but that makes it difficult
11222 to handle something like `f()'. We want to notice that there are
11223 no decl-specifiers, and therefore realize that this is an
11224 expression, not a declaration.)
11226 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11227 the declarator is a direct-declarator of the form "(...)".
11229 MEMBER_P is true iff this declarator is a member-declarator. */
11231 static cp_declarator
*
11232 cp_parser_declarator (cp_parser
* parser
,
11233 cp_parser_declarator_kind dcl_kind
,
11234 int* ctor_dtor_or_conv_p
,
11235 bool* parenthesized_p
,
11239 cp_declarator
*declarator
;
11240 enum tree_code code
;
11241 cp_cv_quals cv_quals
;
11243 tree attributes
= NULL_TREE
;
11245 /* Assume this is not a constructor, destructor, or type-conversion
11247 if (ctor_dtor_or_conv_p
)
11248 *ctor_dtor_or_conv_p
= 0;
11250 if (cp_parser_allow_gnu_extensions_p (parser
))
11251 attributes
= cp_parser_attributes_opt (parser
);
11253 /* Peek at the next token. */
11254 token
= cp_lexer_peek_token (parser
->lexer
);
11256 /* Check for the ptr-operator production. */
11257 cp_parser_parse_tentatively (parser
);
11258 /* Parse the ptr-operator. */
11259 code
= cp_parser_ptr_operator (parser
,
11262 /* If that worked, then we have a ptr-operator. */
11263 if (cp_parser_parse_definitely (parser
))
11265 /* If a ptr-operator was found, then this declarator was not
11267 if (parenthesized_p
)
11268 *parenthesized_p
= true;
11269 /* The dependent declarator is optional if we are parsing an
11270 abstract-declarator. */
11271 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11272 cp_parser_parse_tentatively (parser
);
11274 /* Parse the dependent declarator. */
11275 declarator
= cp_parser_declarator (parser
, dcl_kind
,
11276 /*ctor_dtor_or_conv_p=*/NULL
,
11277 /*parenthesized_p=*/NULL
,
11278 /*member_p=*/false);
11280 /* If we are parsing an abstract-declarator, we must handle the
11281 case where the dependent declarator is absent. */
11282 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
11283 && !cp_parser_parse_definitely (parser
))
11286 /* Build the representation of the ptr-operator. */
11288 declarator
= make_ptrmem_declarator (cv_quals
,
11291 else if (code
== INDIRECT_REF
)
11292 declarator
= make_pointer_declarator (cv_quals
, declarator
);
11294 declarator
= make_reference_declarator (cv_quals
, declarator
);
11296 /* Everything else is a direct-declarator. */
11299 if (parenthesized_p
)
11300 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
11302 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
11303 ctor_dtor_or_conv_p
,
11307 if (attributes
&& declarator
&& declarator
!= cp_error_declarator
)
11308 declarator
->attributes
= attributes
;
11313 /* Parse a direct-declarator or direct-abstract-declarator.
11317 direct-declarator ( parameter-declaration-clause )
11318 cv-qualifier-seq [opt]
11319 exception-specification [opt]
11320 direct-declarator [ constant-expression [opt] ]
11323 direct-abstract-declarator:
11324 direct-abstract-declarator [opt]
11325 ( parameter-declaration-clause )
11326 cv-qualifier-seq [opt]
11327 exception-specification [opt]
11328 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11329 ( abstract-declarator )
11331 Returns a representation of the declarator. DCL_KIND is
11332 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11333 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11334 we are parsing a direct-declarator. It is
11335 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11336 of ambiguity we prefer an abstract declarator, as per
11337 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11338 cp_parser_declarator. */
11340 static cp_declarator
*
11341 cp_parser_direct_declarator (cp_parser
* parser
,
11342 cp_parser_declarator_kind dcl_kind
,
11343 int* ctor_dtor_or_conv_p
,
11347 cp_declarator
*declarator
= NULL
;
11348 tree scope
= NULL_TREE
;
11349 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
11350 bool saved_in_declarator_p
= parser
->in_declarator_p
;
11352 tree pushed_scope
= NULL_TREE
;
11356 /* Peek at the next token. */
11357 token
= cp_lexer_peek_token (parser
->lexer
);
11358 if (token
->type
== CPP_OPEN_PAREN
)
11360 /* This is either a parameter-declaration-clause, or a
11361 parenthesized declarator. When we know we are parsing a
11362 named declarator, it must be a parenthesized declarator
11363 if FIRST is true. For instance, `(int)' is a
11364 parameter-declaration-clause, with an omitted
11365 direct-abstract-declarator. But `((*))', is a
11366 parenthesized abstract declarator. Finally, when T is a
11367 template parameter `(T)' is a
11368 parameter-declaration-clause, and not a parenthesized
11371 We first try and parse a parameter-declaration-clause,
11372 and then try a nested declarator (if FIRST is true).
11374 It is not an error for it not to be a
11375 parameter-declaration-clause, even when FIRST is
11381 The first is the declaration of a function while the
11382 second is a the definition of a variable, including its
11385 Having seen only the parenthesis, we cannot know which of
11386 these two alternatives should be selected. Even more
11387 complex are examples like:
11392 The former is a function-declaration; the latter is a
11393 variable initialization.
11395 Thus again, we try a parameter-declaration-clause, and if
11396 that fails, we back out and return. */
11398 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11400 cp_parameter_declarator
*params
;
11401 unsigned saved_num_template_parameter_lists
;
11403 /* In a member-declarator, the only valid interpretation
11404 of a parenthesis is the start of a
11405 parameter-declaration-clause. (It is invalid to
11406 initialize a static data member with a parenthesized
11407 initializer; only the "=" form of initialization is
11410 cp_parser_parse_tentatively (parser
);
11412 /* Consume the `('. */
11413 cp_lexer_consume_token (parser
->lexer
);
11416 /* If this is going to be an abstract declarator, we're
11417 in a declarator and we can't have default args. */
11418 parser
->default_arg_ok_p
= false;
11419 parser
->in_declarator_p
= true;
11422 /* Inside the function parameter list, surrounding
11423 template-parameter-lists do not apply. */
11424 saved_num_template_parameter_lists
11425 = parser
->num_template_parameter_lists
;
11426 parser
->num_template_parameter_lists
= 0;
11428 /* Parse the parameter-declaration-clause. */
11429 params
= cp_parser_parameter_declaration_clause (parser
);
11431 parser
->num_template_parameter_lists
11432 = saved_num_template_parameter_lists
;
11434 /* If all went well, parse the cv-qualifier-seq and the
11435 exception-specification. */
11436 if (member_p
|| cp_parser_parse_definitely (parser
))
11438 cp_cv_quals cv_quals
;
11439 tree exception_specification
;
11441 if (ctor_dtor_or_conv_p
)
11442 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
11444 /* Consume the `)'. */
11445 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
11447 /* Parse the cv-qualifier-seq. */
11448 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11449 /* And the exception-specification. */
11450 exception_specification
11451 = cp_parser_exception_specification_opt (parser
);
11453 /* Create the function-declarator. */
11454 declarator
= make_call_declarator (declarator
,
11457 exception_specification
);
11458 /* Any subsequent parameter lists are to do with
11459 return type, so are not those of the declared
11461 parser
->default_arg_ok_p
= false;
11463 /* Repeat the main loop. */
11468 /* If this is the first, we can try a parenthesized
11472 bool saved_in_type_id_in_expr_p
;
11474 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11475 parser
->in_declarator_p
= saved_in_declarator_p
;
11477 /* Consume the `('. */
11478 cp_lexer_consume_token (parser
->lexer
);
11479 /* Parse the nested declarator. */
11480 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
11481 parser
->in_type_id_in_expr_p
= true;
11483 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
11484 /*parenthesized_p=*/NULL
,
11486 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
11488 /* Expect a `)'. */
11489 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
11490 declarator
= cp_error_declarator
;
11491 if (declarator
== cp_error_declarator
)
11494 goto handle_declarator
;
11496 /* Otherwise, we must be done. */
11500 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11501 && token
->type
== CPP_OPEN_SQUARE
)
11503 /* Parse an array-declarator. */
11506 if (ctor_dtor_or_conv_p
)
11507 *ctor_dtor_or_conv_p
= 0;
11510 parser
->default_arg_ok_p
= false;
11511 parser
->in_declarator_p
= true;
11512 /* Consume the `['. */
11513 cp_lexer_consume_token (parser
->lexer
);
11514 /* Peek at the next token. */
11515 token
= cp_lexer_peek_token (parser
->lexer
);
11516 /* If the next token is `]', then there is no
11517 constant-expression. */
11518 if (token
->type
!= CPP_CLOSE_SQUARE
)
11520 bool non_constant_p
;
11523 = cp_parser_constant_expression (parser
,
11524 /*allow_non_constant=*/true,
11526 if (!non_constant_p
)
11527 bounds
= fold_non_dependent_expr (bounds
);
11528 /* Normally, the array bound must be an integral constant
11529 expression. However, as an extension, we allow VLAs
11530 in function scopes. */
11531 else if (!at_function_scope_p ())
11533 error ("array bound is not an integer constant");
11534 bounds
= error_mark_node
;
11538 bounds
= NULL_TREE
;
11539 /* Look for the closing `]'. */
11540 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
11542 declarator
= cp_error_declarator
;
11546 declarator
= make_array_declarator (declarator
, bounds
);
11548 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
11550 tree qualifying_scope
;
11551 tree unqualified_name
;
11552 special_function_kind sfk
;
11555 /* Parse a declarator-id */
11556 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
11558 cp_parser_parse_tentatively (parser
);
11560 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
11561 qualifying_scope
= parser
->scope
;
11564 if (!cp_parser_parse_definitely (parser
))
11565 unqualified_name
= error_mark_node
;
11566 else if (unqualified_name
11567 && (qualifying_scope
11568 || (TREE_CODE (unqualified_name
)
11569 != IDENTIFIER_NODE
)))
11571 cp_parser_error (parser
, "expected unqualified-id");
11572 unqualified_name
= error_mark_node
;
11576 if (!unqualified_name
)
11578 if (unqualified_name
== error_mark_node
)
11580 declarator
= cp_error_declarator
;
11584 if (qualifying_scope
&& at_namespace_scope_p ()
11585 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
11587 /* In the declaration of a member of a template class
11588 outside of the class itself, the SCOPE will sometimes
11589 be a TYPENAME_TYPE. For example, given:
11591 template <typename T>
11592 int S<T>::R::i = 3;
11594 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11595 this context, we must resolve S<T>::R to an ordinary
11596 type, rather than a typename type.
11598 The reason we normally avoid resolving TYPENAME_TYPEs
11599 is that a specialization of `S' might render
11600 `S<T>::R' not a type. However, if `S' is
11601 specialized, then this `i' will not be used, so there
11602 is no harm in resolving the types here. */
11605 /* Resolve the TYPENAME_TYPE. */
11606 type
= resolve_typename_type (qualifying_scope
,
11607 /*only_current_p=*/false);
11608 /* If that failed, the declarator is invalid. */
11609 if (type
== error_mark_node
)
11610 error ("%<%T::%D%> is not a type",
11611 TYPE_CONTEXT (qualifying_scope
),
11612 TYPE_IDENTIFIER (qualifying_scope
));
11613 qualifying_scope
= type
;
11617 if (unqualified_name
)
11621 if (qualifying_scope
11622 && CLASS_TYPE_P (qualifying_scope
))
11623 class_type
= qualifying_scope
;
11625 class_type
= current_class_type
;
11627 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
11629 tree name_type
= TREE_TYPE (unqualified_name
);
11630 if (class_type
&& same_type_p (name_type
, class_type
))
11632 if (qualifying_scope
11633 && CLASSTYPE_USE_TEMPLATE (name_type
))
11635 error ("invalid use of constructor as a template");
11636 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11637 "name the constructor in a qualified name",
11639 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
11640 class_type
, name_type
);
11641 declarator
= cp_error_declarator
;
11645 unqualified_name
= constructor_name (class_type
);
11649 /* We do not attempt to print the declarator
11650 here because we do not have enough
11651 information about its original syntactic
11653 cp_parser_error (parser
, "invalid declarator");
11654 declarator
= cp_error_declarator
;
11661 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
11662 sfk
= sfk_destructor
;
11663 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
11664 sfk
= sfk_conversion
;
11665 else if (/* There's no way to declare a constructor
11666 for an anonymous type, even if the type
11667 got a name for linkage purposes. */
11668 !TYPE_WAS_ANONYMOUS (class_type
)
11669 && constructor_name_p (unqualified_name
,
11672 unqualified_name
= constructor_name (class_type
);
11673 sfk
= sfk_constructor
;
11676 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
11677 *ctor_dtor_or_conv_p
= -1;
11680 declarator
= make_id_declarator (qualifying_scope
,
11683 declarator
->id_loc
= token
->location
;
11685 handle_declarator
:;
11686 scope
= get_scope_of_declarator (declarator
);
11688 /* Any names that appear after the declarator-id for a
11689 member are looked up in the containing scope. */
11690 pushed_scope
= push_scope (scope
);
11691 parser
->in_declarator_p
= true;
11692 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
11693 || (declarator
&& declarator
->kind
== cdk_id
))
11694 /* Default args are only allowed on function
11696 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11698 parser
->default_arg_ok_p
= false;
11707 /* For an abstract declarator, we might wind up with nothing at this
11708 point. That's an error; the declarator is not optional. */
11710 cp_parser_error (parser
, "expected declarator");
11712 /* If we entered a scope, we must exit it now. */
11714 pop_scope (pushed_scope
);
11716 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11717 parser
->in_declarator_p
= saved_in_declarator_p
;
11722 /* Parse a ptr-operator.
11725 * cv-qualifier-seq [opt]
11727 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11732 & cv-qualifier-seq [opt]
11734 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11735 Returns ADDR_EXPR if a reference was used. In the case of a
11736 pointer-to-member, *TYPE is filled in with the TYPE containing the
11737 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11738 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11739 ERROR_MARK if an error occurred. */
11741 static enum tree_code
11742 cp_parser_ptr_operator (cp_parser
* parser
,
11744 cp_cv_quals
*cv_quals
)
11746 enum tree_code code
= ERROR_MARK
;
11749 /* Assume that it's not a pointer-to-member. */
11751 /* And that there are no cv-qualifiers. */
11752 *cv_quals
= TYPE_UNQUALIFIED
;
11754 /* Peek at the next token. */
11755 token
= cp_lexer_peek_token (parser
->lexer
);
11756 /* If it's a `*' or `&' we have a pointer or reference. */
11757 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
11759 /* Remember which ptr-operator we were processing. */
11760 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
11762 /* Consume the `*' or `&'. */
11763 cp_lexer_consume_token (parser
->lexer
);
11765 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11766 `&', if we are allowing GNU extensions. (The only qualifier
11767 that can legally appear after `&' is `restrict', but that is
11768 enforced during semantic analysis. */
11769 if (code
== INDIRECT_REF
11770 || cp_parser_allow_gnu_extensions_p (parser
))
11771 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11775 /* Try the pointer-to-member case. */
11776 cp_parser_parse_tentatively (parser
);
11777 /* Look for the optional `::' operator. */
11778 cp_parser_global_scope_opt (parser
,
11779 /*current_scope_valid_p=*/false);
11780 /* Look for the nested-name specifier. */
11781 cp_parser_nested_name_specifier (parser
,
11782 /*typename_keyword_p=*/false,
11783 /*check_dependency_p=*/true,
11785 /*is_declaration=*/false);
11786 /* If we found it, and the next token is a `*', then we are
11787 indeed looking at a pointer-to-member operator. */
11788 if (!cp_parser_error_occurred (parser
)
11789 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
11791 /* Indicate that the `*' operator was used. */
11792 code
= INDIRECT_REF
;
11794 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
11795 error ("%qD is a namespace", parser
->scope
);
11798 /* The type of which the member is a member is given by the
11800 *type
= parser
->scope
;
11801 /* The next name will not be qualified. */
11802 parser
->scope
= NULL_TREE
;
11803 parser
->qualifying_scope
= NULL_TREE
;
11804 parser
->object_scope
= NULL_TREE
;
11805 /* Look for the optional cv-qualifier-seq. */
11806 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11809 /* If that didn't work we don't have a ptr-operator. */
11810 if (!cp_parser_parse_definitely (parser
))
11811 cp_parser_error (parser
, "expected ptr-operator");
11817 /* Parse an (optional) cv-qualifier-seq.
11820 cv-qualifier cv-qualifier-seq [opt]
11831 Returns a bitmask representing the cv-qualifiers. */
11834 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
11836 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
11841 cp_cv_quals cv_qualifier
;
11843 /* Peek at the next token. */
11844 token
= cp_lexer_peek_token (parser
->lexer
);
11845 /* See if it's a cv-qualifier. */
11846 switch (token
->keyword
)
11849 cv_qualifier
= TYPE_QUAL_CONST
;
11853 cv_qualifier
= TYPE_QUAL_VOLATILE
;
11857 cv_qualifier
= TYPE_QUAL_RESTRICT
;
11861 cv_qualifier
= TYPE_UNQUALIFIED
;
11868 if (cv_quals
& cv_qualifier
)
11870 error ("duplicate cv-qualifier");
11871 cp_lexer_purge_token (parser
->lexer
);
11875 cp_lexer_consume_token (parser
->lexer
);
11876 cv_quals
|= cv_qualifier
;
11883 /* Parse a declarator-id.
11887 :: [opt] nested-name-specifier [opt] type-name
11889 In the `id-expression' case, the value returned is as for
11890 cp_parser_id_expression if the id-expression was an unqualified-id.
11891 If the id-expression was a qualified-id, then a SCOPE_REF is
11892 returned. The first operand is the scope (either a NAMESPACE_DECL
11893 or TREE_TYPE), but the second is still just a representation of an
11897 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
11900 /* The expression must be an id-expression. Assume that qualified
11901 names are the names of types so that:
11904 int S<T>::R::i = 3;
11906 will work; we must treat `S<T>::R' as the name of a type.
11907 Similarly, assume that qualified names are templates, where
11911 int S<T>::R<T>::i = 3;
11914 id
= cp_parser_id_expression (parser
,
11915 /*template_keyword_p=*/false,
11916 /*check_dependency_p=*/false,
11917 /*template_p=*/NULL
,
11918 /*declarator_p=*/true,
11920 if (id
&& BASELINK_P (id
))
11921 id
= BASELINK_FUNCTIONS (id
);
11925 /* Parse a type-id.
11928 type-specifier-seq abstract-declarator [opt]
11930 Returns the TYPE specified. */
11933 cp_parser_type_id (cp_parser
* parser
)
11935 cp_decl_specifier_seq type_specifier_seq
;
11936 cp_declarator
*abstract_declarator
;
11938 /* Parse the type-specifier-seq. */
11939 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
11940 &type_specifier_seq
);
11941 if (type_specifier_seq
.type
== error_mark_node
)
11942 return error_mark_node
;
11944 /* There might or might not be an abstract declarator. */
11945 cp_parser_parse_tentatively (parser
);
11946 /* Look for the declarator. */
11947 abstract_declarator
11948 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
11949 /*parenthesized_p=*/NULL
,
11950 /*member_p=*/false);
11951 /* Check to see if there really was a declarator. */
11952 if (!cp_parser_parse_definitely (parser
))
11953 abstract_declarator
= NULL
;
11955 return groktypename (&type_specifier_seq
, abstract_declarator
);
11958 /* Parse a type-specifier-seq.
11960 type-specifier-seq:
11961 type-specifier type-specifier-seq [opt]
11965 type-specifier-seq:
11966 attributes type-specifier-seq [opt]
11968 If IS_CONDITION is true, we are at the start of a "condition",
11969 e.g., we've just seen "if (".
11971 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11974 cp_parser_type_specifier_seq (cp_parser
* parser
,
11976 cp_decl_specifier_seq
*type_specifier_seq
)
11978 bool seen_type_specifier
= false;
11979 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
11981 /* Clear the TYPE_SPECIFIER_SEQ. */
11982 clear_decl_specs (type_specifier_seq
);
11984 /* Parse the type-specifiers and attributes. */
11987 tree type_specifier
;
11988 bool is_cv_qualifier
;
11990 /* Check for attributes first. */
11991 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
11993 type_specifier_seq
->attributes
=
11994 chainon (type_specifier_seq
->attributes
,
11995 cp_parser_attributes_opt (parser
));
11999 /* Look for the type-specifier. */
12000 type_specifier
= cp_parser_type_specifier (parser
,
12002 type_specifier_seq
,
12003 /*is_declaration=*/false,
12006 if (!type_specifier
)
12008 /* If the first type-specifier could not be found, this is not a
12009 type-specifier-seq at all. */
12010 if (!seen_type_specifier
)
12012 cp_parser_error (parser
, "expected type-specifier");
12013 type_specifier_seq
->type
= error_mark_node
;
12016 /* If subsequent type-specifiers could not be found, the
12017 type-specifier-seq is complete. */
12021 seen_type_specifier
= true;
12022 /* The standard says that a condition can be:
12024 type-specifier-seq declarator = assignment-expression
12031 we should treat the "S" as a declarator, not as a
12032 type-specifier. The standard doesn't say that explicitly for
12033 type-specifier-seq, but it does say that for
12034 decl-specifier-seq in an ordinary declaration. Perhaps it
12035 would be clearer just to allow a decl-specifier-seq here, and
12036 then add a semantic restriction that if any decl-specifiers
12037 that are not type-specifiers appear, the program is invalid. */
12038 if (is_condition
&& !is_cv_qualifier
)
12039 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
12042 cp_parser_check_decl_spec (type_specifier_seq
);
12045 /* Parse a parameter-declaration-clause.
12047 parameter-declaration-clause:
12048 parameter-declaration-list [opt] ... [opt]
12049 parameter-declaration-list , ...
12051 Returns a representation for the parameter declarations. A return
12052 value of NULL indicates a parameter-declaration-clause consisting
12053 only of an ellipsis. */
12055 static cp_parameter_declarator
*
12056 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
12058 cp_parameter_declarator
*parameters
;
12063 /* Peek at the next token. */
12064 token
= cp_lexer_peek_token (parser
->lexer
);
12065 /* Check for trivial parameter-declaration-clauses. */
12066 if (token
->type
== CPP_ELLIPSIS
)
12068 /* Consume the `...' token. */
12069 cp_lexer_consume_token (parser
->lexer
);
12072 else if (token
->type
== CPP_CLOSE_PAREN
)
12073 /* There are no parameters. */
12075 #ifndef NO_IMPLICIT_EXTERN_C
12076 if (in_system_header
&& current_class_type
== NULL
12077 && current_lang_name
== lang_name_c
)
12081 return no_parameters
;
12083 /* Check for `(void)', too, which is a special case. */
12084 else if (token
->keyword
== RID_VOID
12085 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
12086 == CPP_CLOSE_PAREN
))
12088 /* Consume the `void' token. */
12089 cp_lexer_consume_token (parser
->lexer
);
12090 /* There are no parameters. */
12091 return no_parameters
;
12094 /* Parse the parameter-declaration-list. */
12095 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
12096 /* If a parse error occurred while parsing the
12097 parameter-declaration-list, then the entire
12098 parameter-declaration-clause is erroneous. */
12102 /* Peek at the next token. */
12103 token
= cp_lexer_peek_token (parser
->lexer
);
12104 /* If it's a `,', the clause should terminate with an ellipsis. */
12105 if (token
->type
== CPP_COMMA
)
12107 /* Consume the `,'. */
12108 cp_lexer_consume_token (parser
->lexer
);
12109 /* Expect an ellipsis. */
12111 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
12113 /* It might also be `...' if the optional trailing `,' was
12115 else if (token
->type
== CPP_ELLIPSIS
)
12117 /* Consume the `...' token. */
12118 cp_lexer_consume_token (parser
->lexer
);
12119 /* And remember that we saw it. */
12123 ellipsis_p
= false;
12125 /* Finish the parameter list. */
12126 if (parameters
&& ellipsis_p
)
12127 parameters
->ellipsis_p
= true;
12132 /* Parse a parameter-declaration-list.
12134 parameter-declaration-list:
12135 parameter-declaration
12136 parameter-declaration-list , parameter-declaration
12138 Returns a representation of the parameter-declaration-list, as for
12139 cp_parser_parameter_declaration_clause. However, the
12140 `void_list_node' is never appended to the list. Upon return,
12141 *IS_ERROR will be true iff an error occurred. */
12143 static cp_parameter_declarator
*
12144 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
12146 cp_parameter_declarator
*parameters
= NULL
;
12147 cp_parameter_declarator
**tail
= ¶meters
;
12148 bool saved_in_unbraced_linkage_specification_p
;
12150 /* Assume all will go well. */
12152 /* The special considerations that apply to a function within an
12153 unbraced linkage specifications do not apply to the parameters
12154 to the function. */
12155 saved_in_unbraced_linkage_specification_p
12156 = parser
->in_unbraced_linkage_specification_p
;
12157 parser
->in_unbraced_linkage_specification_p
= false;
12159 /* Look for more parameters. */
12162 cp_parameter_declarator
*parameter
;
12163 bool parenthesized_p
;
12164 /* Parse the parameter. */
12166 = cp_parser_parameter_declaration (parser
,
12167 /*template_parm_p=*/false,
12170 /* If a parse error occurred parsing the parameter declaration,
12171 then the entire parameter-declaration-list is erroneous. */
12178 /* Add the new parameter to the list. */
12180 tail
= ¶meter
->next
;
12182 /* Peek at the next token. */
12183 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
12184 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
12185 /* These are for Objective-C++ */
12186 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
12187 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12188 /* The parameter-declaration-list is complete. */
12190 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12194 /* Peek at the next token. */
12195 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12196 /* If it's an ellipsis, then the list is complete. */
12197 if (token
->type
== CPP_ELLIPSIS
)
12199 /* Otherwise, there must be more parameters. Consume the
12201 cp_lexer_consume_token (parser
->lexer
);
12202 /* When parsing something like:
12204 int i(float f, double d)
12206 we can tell after seeing the declaration for "f" that we
12207 are not looking at an initialization of a variable "i",
12208 but rather at the declaration of a function "i".
12210 Due to the fact that the parsing of template arguments
12211 (as specified to a template-id) requires backtracking we
12212 cannot use this technique when inside a template argument
12214 if (!parser
->in_template_argument_list_p
12215 && !parser
->in_type_id_in_expr_p
12216 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
12217 /* However, a parameter-declaration of the form
12218 "foat(f)" (which is a valid declaration of a
12219 parameter "f") can also be interpreted as an
12220 expression (the conversion of "f" to "float"). */
12221 && !parenthesized_p
)
12222 cp_parser_commit_to_tentative_parse (parser
);
12226 cp_parser_error (parser
, "expected %<,%> or %<...%>");
12227 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
12228 cp_parser_skip_to_closing_parenthesis (parser
,
12229 /*recovering=*/true,
12230 /*or_comma=*/false,
12231 /*consume_paren=*/false);
12236 parser
->in_unbraced_linkage_specification_p
12237 = saved_in_unbraced_linkage_specification_p
;
12242 /* Parse a parameter declaration.
12244 parameter-declaration:
12245 decl-specifier-seq declarator
12246 decl-specifier-seq declarator = assignment-expression
12247 decl-specifier-seq abstract-declarator [opt]
12248 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12250 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12251 declares a template parameter. (In that case, a non-nested `>'
12252 token encountered during the parsing of the assignment-expression
12253 is not interpreted as a greater-than operator.)
12255 Returns a representation of the parameter, or NULL if an error
12256 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12257 true iff the declarator is of the form "(p)". */
12259 static cp_parameter_declarator
*
12260 cp_parser_parameter_declaration (cp_parser
*parser
,
12261 bool template_parm_p
,
12262 bool *parenthesized_p
)
12264 int declares_class_or_enum
;
12265 bool greater_than_is_operator_p
;
12266 cp_decl_specifier_seq decl_specifiers
;
12267 cp_declarator
*declarator
;
12268 tree default_argument
;
12270 const char *saved_message
;
12272 /* In a template parameter, `>' is not an operator.
12276 When parsing a default template-argument for a non-type
12277 template-parameter, the first non-nested `>' is taken as the end
12278 of the template parameter-list rather than a greater-than
12280 greater_than_is_operator_p
= !template_parm_p
;
12282 /* Type definitions may not appear in parameter types. */
12283 saved_message
= parser
->type_definition_forbidden_message
;
12284 parser
->type_definition_forbidden_message
12285 = "types may not be defined in parameter types";
12287 /* Parse the declaration-specifiers. */
12288 cp_parser_decl_specifier_seq (parser
,
12289 CP_PARSER_FLAGS_NONE
,
12291 &declares_class_or_enum
);
12292 /* If an error occurred, there's no reason to attempt to parse the
12293 rest of the declaration. */
12294 if (cp_parser_error_occurred (parser
))
12296 parser
->type_definition_forbidden_message
= saved_message
;
12300 /* Peek at the next token. */
12301 token
= cp_lexer_peek_token (parser
->lexer
);
12302 /* If the next token is a `)', `,', `=', `>', or `...', then there
12303 is no declarator. */
12304 if (token
->type
== CPP_CLOSE_PAREN
12305 || token
->type
== CPP_COMMA
12306 || token
->type
== CPP_EQ
12307 || token
->type
== CPP_ELLIPSIS
12308 || token
->type
== CPP_GREATER
)
12311 if (parenthesized_p
)
12312 *parenthesized_p
= false;
12314 /* Otherwise, there should be a declarator. */
12317 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
12318 parser
->default_arg_ok_p
= false;
12320 /* After seeing a decl-specifier-seq, if the next token is not a
12321 "(", there is no possibility that the code is a valid
12322 expression. Therefore, if parsing tentatively, we commit at
12324 if (!parser
->in_template_argument_list_p
12325 /* In an expression context, having seen:
12329 we cannot be sure whether we are looking at a
12330 function-type (taking a "char" as a parameter) or a cast
12331 of some object of type "char" to "int". */
12332 && !parser
->in_type_id_in_expr_p
12333 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
12334 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
12335 cp_parser_commit_to_tentative_parse (parser
);
12336 /* Parse the declarator. */
12337 declarator
= cp_parser_declarator (parser
,
12338 CP_PARSER_DECLARATOR_EITHER
,
12339 /*ctor_dtor_or_conv_p=*/NULL
,
12341 /*member_p=*/false);
12342 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12343 /* After the declarator, allow more attributes. */
12344 decl_specifiers
.attributes
12345 = chainon (decl_specifiers
.attributes
,
12346 cp_parser_attributes_opt (parser
));
12349 /* The restriction on defining new types applies only to the type
12350 of the parameter, not to the default argument. */
12351 parser
->type_definition_forbidden_message
= saved_message
;
12353 /* If the next token is `=', then process a default argument. */
12354 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12356 bool saved_greater_than_is_operator_p
;
12357 /* Consume the `='. */
12358 cp_lexer_consume_token (parser
->lexer
);
12360 /* If we are defining a class, then the tokens that make up the
12361 default argument must be saved and processed later. */
12362 if (!template_parm_p
&& at_class_scope_p ()
12363 && TYPE_BEING_DEFINED (current_class_type
))
12365 unsigned depth
= 0;
12366 cp_token
*first_token
;
12369 /* Add tokens until we have processed the entire default
12370 argument. We add the range [first_token, token). */
12371 first_token
= cp_lexer_peek_token (parser
->lexer
);
12376 /* Peek at the next token. */
12377 token
= cp_lexer_peek_token (parser
->lexer
);
12378 /* What we do depends on what token we have. */
12379 switch (token
->type
)
12381 /* In valid code, a default argument must be
12382 immediately followed by a `,' `)', or `...'. */
12384 case CPP_CLOSE_PAREN
:
12386 /* If we run into a non-nested `;', `}', or `]',
12387 then the code is invalid -- but the default
12388 argument is certainly over. */
12389 case CPP_SEMICOLON
:
12390 case CPP_CLOSE_BRACE
:
12391 case CPP_CLOSE_SQUARE
:
12394 /* Update DEPTH, if necessary. */
12395 else if (token
->type
== CPP_CLOSE_PAREN
12396 || token
->type
== CPP_CLOSE_BRACE
12397 || token
->type
== CPP_CLOSE_SQUARE
)
12401 case CPP_OPEN_PAREN
:
12402 case CPP_OPEN_SQUARE
:
12403 case CPP_OPEN_BRACE
:
12408 /* If we see a non-nested `>', and `>' is not an
12409 operator, then it marks the end of the default
12411 if (!depth
&& !greater_than_is_operator_p
)
12415 /* If we run out of tokens, issue an error message. */
12417 case CPP_PRAGMA_EOL
:
12418 error ("file ends in default argument");
12424 /* In these cases, we should look for template-ids.
12425 For example, if the default argument is
12426 `X<int, double>()', we need to do name lookup to
12427 figure out whether or not `X' is a template; if
12428 so, the `,' does not end the default argument.
12430 That is not yet done. */
12437 /* If we've reached the end, stop. */
12441 /* Add the token to the token block. */
12442 token
= cp_lexer_consume_token (parser
->lexer
);
12445 /* Create a DEFAULT_ARG to represented the unparsed default
12447 default_argument
= make_node (DEFAULT_ARG
);
12448 DEFARG_TOKENS (default_argument
)
12449 = cp_token_cache_new (first_token
, token
);
12450 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
12452 /* Outside of a class definition, we can just parse the
12453 assignment-expression. */
12456 bool saved_local_variables_forbidden_p
;
12458 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12460 saved_greater_than_is_operator_p
12461 = parser
->greater_than_is_operator_p
;
12462 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
12463 /* Local variable names (and the `this' keyword) may not
12464 appear in a default argument. */
12465 saved_local_variables_forbidden_p
12466 = parser
->local_variables_forbidden_p
;
12467 parser
->local_variables_forbidden_p
= true;
12468 /* The default argument expression may cause implicitly
12469 defined member functions to be synthesized, which will
12470 result in garbage collection. We must treat this
12471 situation as if we were within the body of function so as
12472 to avoid collecting live data on the stack. */
12474 /* Parse the assignment-expression. */
12475 if (template_parm_p
)
12476 push_deferring_access_checks (dk_no_deferred
);
12478 = cp_parser_assignment_expression (parser
, /*cast_p=*/false);
12479 if (template_parm_p
)
12480 pop_deferring_access_checks ();
12481 /* Restore saved state. */
12483 parser
->greater_than_is_operator_p
12484 = saved_greater_than_is_operator_p
;
12485 parser
->local_variables_forbidden_p
12486 = saved_local_variables_forbidden_p
;
12488 if (!parser
->default_arg_ok_p
)
12490 if (!flag_pedantic_errors
)
12491 warning (0, "deprecated use of default argument for parameter of non-function");
12494 error ("default arguments are only permitted for function parameters");
12495 default_argument
= NULL_TREE
;
12500 default_argument
= NULL_TREE
;
12502 return make_parameter_declarator (&decl_specifiers
,
12507 /* Parse a function-body.
12510 compound_statement */
12513 cp_parser_function_body (cp_parser
*parser
)
12515 cp_parser_compound_statement (parser
, NULL
, false);
12518 /* Parse a ctor-initializer-opt followed by a function-body. Return
12519 true if a ctor-initializer was present. */
12522 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
12525 bool ctor_initializer_p
;
12527 /* Begin the function body. */
12528 body
= begin_function_body ();
12529 /* Parse the optional ctor-initializer. */
12530 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
12531 /* Parse the function-body. */
12532 cp_parser_function_body (parser
);
12533 /* Finish the function body. */
12534 finish_function_body (body
);
12536 return ctor_initializer_p
;
12539 /* Parse an initializer.
12542 = initializer-clause
12543 ( expression-list )
12545 Returns an expression representing the initializer. If no
12546 initializer is present, NULL_TREE is returned.
12548 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12549 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12550 set to FALSE if there is no initializer present. If there is an
12551 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12552 is set to true; otherwise it is set to false. */
12555 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
12556 bool* non_constant_p
)
12561 /* Peek at the next token. */
12562 token
= cp_lexer_peek_token (parser
->lexer
);
12564 /* Let our caller know whether or not this initializer was
12566 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
12567 /* Assume that the initializer is constant. */
12568 *non_constant_p
= false;
12570 if (token
->type
== CPP_EQ
)
12572 /* Consume the `='. */
12573 cp_lexer_consume_token (parser
->lexer
);
12574 /* Parse the initializer-clause. */
12575 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
12577 else if (token
->type
== CPP_OPEN_PAREN
)
12578 init
= cp_parser_parenthesized_expression_list (parser
, false,
12583 /* Anything else is an error. */
12584 cp_parser_error (parser
, "expected initializer");
12585 init
= error_mark_node
;
12591 /* Parse an initializer-clause.
12593 initializer-clause:
12594 assignment-expression
12595 { initializer-list , [opt] }
12598 Returns an expression representing the initializer.
12600 If the `assignment-expression' production is used the value
12601 returned is simply a representation for the expression.
12603 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12604 the elements of the initializer-list (or NULL, if the last
12605 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12606 NULL_TREE. There is no way to detect whether or not the optional
12607 trailing `,' was provided. NON_CONSTANT_P is as for
12608 cp_parser_initializer. */
12611 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
12615 /* Assume the expression is constant. */
12616 *non_constant_p
= false;
12618 /* If it is not a `{', then we are looking at an
12619 assignment-expression. */
12620 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12623 = cp_parser_constant_expression (parser
,
12624 /*allow_non_constant_p=*/true,
12626 if (!*non_constant_p
)
12627 initializer
= fold_non_dependent_expr (initializer
);
12631 /* Consume the `{' token. */
12632 cp_lexer_consume_token (parser
->lexer
);
12633 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12634 initializer
= make_node (CONSTRUCTOR
);
12635 /* If it's not a `}', then there is a non-trivial initializer. */
12636 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
12638 /* Parse the initializer list. */
12639 CONSTRUCTOR_ELTS (initializer
)
12640 = cp_parser_initializer_list (parser
, non_constant_p
);
12641 /* A trailing `,' token is allowed. */
12642 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12643 cp_lexer_consume_token (parser
->lexer
);
12645 /* Now, there should be a trailing `}'. */
12646 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12649 return initializer
;
12652 /* Parse an initializer-list.
12656 initializer-list , initializer-clause
12661 identifier : initializer-clause
12662 initializer-list, identifier : initializer-clause
12664 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12665 for the initializer. If the INDEX of the elt is non-NULL, it is the
12666 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12667 as for cp_parser_initializer. */
12669 static VEC(constructor_elt
,gc
) *
12670 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
12672 VEC(constructor_elt
,gc
) *v
= NULL
;
12674 /* Assume all of the expressions are constant. */
12675 *non_constant_p
= false;
12677 /* Parse the rest of the list. */
12683 bool clause_non_constant_p
;
12685 /* If the next token is an identifier and the following one is a
12686 colon, we are looking at the GNU designated-initializer
12688 if (cp_parser_allow_gnu_extensions_p (parser
)
12689 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
12690 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
12692 /* Consume the identifier. */
12693 identifier
= cp_lexer_consume_token (parser
->lexer
)->value
;
12694 /* Consume the `:'. */
12695 cp_lexer_consume_token (parser
->lexer
);
12698 identifier
= NULL_TREE
;
12700 /* Parse the initializer. */
12701 initializer
= cp_parser_initializer_clause (parser
,
12702 &clause_non_constant_p
);
12703 /* If any clause is non-constant, so is the entire initializer. */
12704 if (clause_non_constant_p
)
12705 *non_constant_p
= true;
12707 /* Add it to the vector. */
12708 CONSTRUCTOR_APPEND_ELT(v
, identifier
, initializer
);
12710 /* If the next token is not a comma, we have reached the end of
12712 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12715 /* Peek at the next token. */
12716 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12717 /* If the next token is a `}', then we're still done. An
12718 initializer-clause can have a trailing `,' after the
12719 initializer-list and before the closing `}'. */
12720 if (token
->type
== CPP_CLOSE_BRACE
)
12723 /* Consume the `,' token. */
12724 cp_lexer_consume_token (parser
->lexer
);
12730 /* Classes [gram.class] */
12732 /* Parse a class-name.
12738 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12739 to indicate that names looked up in dependent types should be
12740 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12741 keyword has been used to indicate that the name that appears next
12742 is a template. TAG_TYPE indicates the explicit tag given before
12743 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12744 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12745 is the class being defined in a class-head.
12747 Returns the TYPE_DECL representing the class. */
12750 cp_parser_class_name (cp_parser
*parser
,
12751 bool typename_keyword_p
,
12752 bool template_keyword_p
,
12753 enum tag_types tag_type
,
12754 bool check_dependency_p
,
12756 bool is_declaration
)
12763 /* All class-names start with an identifier. */
12764 token
= cp_lexer_peek_token (parser
->lexer
);
12765 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
12767 cp_parser_error (parser
, "expected class-name");
12768 return error_mark_node
;
12771 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12772 to a template-id, so we save it here. */
12773 scope
= parser
->scope
;
12774 if (scope
== error_mark_node
)
12775 return error_mark_node
;
12777 /* Any name names a type if we're following the `typename' keyword
12778 in a qualified name where the enclosing scope is type-dependent. */
12779 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
12780 && dependent_type_p (scope
));
12781 /* Handle the common case (an identifier, but not a template-id)
12783 if (token
->type
== CPP_NAME
12784 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
12786 cp_token
*identifier_token
;
12790 /* Look for the identifier. */
12791 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
12792 ambiguous_p
= identifier_token
->ambiguous_p
;
12793 identifier
= cp_parser_identifier (parser
);
12794 /* If the next token isn't an identifier, we are certainly not
12795 looking at a class-name. */
12796 if (identifier
== error_mark_node
)
12797 decl
= error_mark_node
;
12798 /* If we know this is a type-name, there's no need to look it
12800 else if (typename_p
)
12804 tree ambiguous_decls
;
12805 /* If we already know that this lookup is ambiguous, then
12806 we've already issued an error message; there's no reason
12810 cp_parser_simulate_error (parser
);
12811 return error_mark_node
;
12813 /* If the next token is a `::', then the name must be a type
12816 [basic.lookup.qual]
12818 During the lookup for a name preceding the :: scope
12819 resolution operator, object, function, and enumerator
12820 names are ignored. */
12821 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12822 tag_type
= typename_type
;
12823 /* Look up the name. */
12824 decl
= cp_parser_lookup_name (parser
, identifier
,
12826 /*is_template=*/false,
12827 /*is_namespace=*/false,
12828 check_dependency_p
,
12830 if (ambiguous_decls
)
12832 error ("reference to %qD is ambiguous", identifier
);
12833 print_candidates (ambiguous_decls
);
12834 if (cp_parser_parsing_tentatively (parser
))
12836 identifier_token
->ambiguous_p
= true;
12837 cp_parser_simulate_error (parser
);
12839 return error_mark_node
;
12845 /* Try a template-id. */
12846 decl
= cp_parser_template_id (parser
, template_keyword_p
,
12847 check_dependency_p
,
12849 if (decl
== error_mark_node
)
12850 return error_mark_node
;
12853 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
12855 /* If this is a typename, create a TYPENAME_TYPE. */
12856 if (typename_p
&& decl
!= error_mark_node
)
12858 decl
= make_typename_type (scope
, decl
, typename_type
,
12859 /*complain=*/tf_error
);
12860 if (decl
!= error_mark_node
)
12861 decl
= TYPE_NAME (decl
);
12864 /* Check to see that it is really the name of a class. */
12865 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
12866 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
12867 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12868 /* Situations like this:
12870 template <typename T> struct A {
12871 typename T::template X<int>::I i;
12874 are problematic. Is `T::template X<int>' a class-name? The
12875 standard does not seem to be definitive, but there is no other
12876 valid interpretation of the following `::'. Therefore, those
12877 names are considered class-names. */
12879 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
12880 if (decl
!= error_mark_node
)
12881 decl
= TYPE_NAME (decl
);
12883 else if (TREE_CODE (decl
) != TYPE_DECL
12884 || TREE_TYPE (decl
) == error_mark_node
12885 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
12886 decl
= error_mark_node
;
12888 if (decl
== error_mark_node
)
12889 cp_parser_error (parser
, "expected class-name");
12894 /* Parse a class-specifier.
12897 class-head { member-specification [opt] }
12899 Returns the TREE_TYPE representing the class. */
12902 cp_parser_class_specifier (cp_parser
* parser
)
12906 tree attributes
= NULL_TREE
;
12907 int has_trailing_semicolon
;
12908 bool nested_name_specifier_p
;
12909 unsigned saved_num_template_parameter_lists
;
12910 tree old_scope
= NULL_TREE
;
12911 tree scope
= NULL_TREE
;
12913 push_deferring_access_checks (dk_no_deferred
);
12915 /* Parse the class-head. */
12916 type
= cp_parser_class_head (parser
,
12917 &nested_name_specifier_p
,
12919 /* If the class-head was a semantic disaster, skip the entire body
12923 cp_parser_skip_to_end_of_block_or_statement (parser
);
12924 pop_deferring_access_checks ();
12925 return error_mark_node
;
12928 /* Look for the `{'. */
12929 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
12931 pop_deferring_access_checks ();
12932 return error_mark_node
;
12935 /* Issue an error message if type-definitions are forbidden here. */
12936 cp_parser_check_type_definition (parser
);
12937 /* Remember that we are defining one more class. */
12938 ++parser
->num_classes_being_defined
;
12939 /* Inside the class, surrounding template-parameter-lists do not
12941 saved_num_template_parameter_lists
12942 = parser
->num_template_parameter_lists
;
12943 parser
->num_template_parameter_lists
= 0;
12945 /* Start the class. */
12946 if (nested_name_specifier_p
)
12948 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
12949 old_scope
= push_inner_scope (scope
);
12951 type
= begin_class_definition (type
, attributes
);
12953 if (type
== error_mark_node
)
12954 /* If the type is erroneous, skip the entire body of the class. */
12955 cp_parser_skip_to_closing_brace (parser
);
12957 /* Parse the member-specification. */
12958 cp_parser_member_specification_opt (parser
);
12960 /* Look for the trailing `}'. */
12961 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12962 /* We get better error messages by noticing a common problem: a
12963 missing trailing `;'. */
12964 token
= cp_lexer_peek_token (parser
->lexer
);
12965 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
12966 /* Look for trailing attributes to apply to this class. */
12967 if (cp_parser_allow_gnu_extensions_p (parser
))
12968 attributes
= cp_parser_attributes_opt (parser
);
12969 if (type
!= error_mark_node
)
12970 type
= finish_struct (type
, attributes
);
12971 if (nested_name_specifier_p
)
12972 pop_inner_scope (old_scope
, scope
);
12973 /* If this class is not itself within the scope of another class,
12974 then we need to parse the bodies of all of the queued function
12975 definitions. Note that the queued functions defined in a class
12976 are not always processed immediately following the
12977 class-specifier for that class. Consider:
12980 struct B { void f() { sizeof (A); } };
12983 If `f' were processed before the processing of `A' were
12984 completed, there would be no way to compute the size of `A'.
12985 Note that the nesting we are interested in here is lexical --
12986 not the semantic nesting given by TYPE_CONTEXT. In particular,
12989 struct A { struct B; };
12990 struct A::B { void f() { } };
12992 there is no need to delay the parsing of `A::B::f'. */
12993 if (--parser
->num_classes_being_defined
== 0)
12997 tree class_type
= NULL_TREE
;
12998 tree pushed_scope
= NULL_TREE
;
13000 /* In a first pass, parse default arguments to the functions.
13001 Then, in a second pass, parse the bodies of the functions.
13002 This two-phased approach handles cases like:
13010 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
13011 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
13012 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
13013 TREE_PURPOSE (parser
->unparsed_functions_queues
)
13014 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
13016 fn
= TREE_VALUE (queue_entry
);
13017 /* If there are default arguments that have not yet been processed,
13018 take care of them now. */
13019 if (class_type
!= TREE_PURPOSE (queue_entry
))
13022 pop_scope (pushed_scope
);
13023 class_type
= TREE_PURPOSE (queue_entry
);
13024 pushed_scope
= push_scope (class_type
);
13026 /* Make sure that any template parameters are in scope. */
13027 maybe_begin_member_template_processing (fn
);
13028 /* Parse the default argument expressions. */
13029 cp_parser_late_parsing_default_args (parser
, fn
);
13030 /* Remove any template parameters from the symbol table. */
13031 maybe_end_member_template_processing ();
13034 pop_scope (pushed_scope
);
13035 /* Now parse the body of the functions. */
13036 for (TREE_VALUE (parser
->unparsed_functions_queues
)
13037 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
13038 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
13039 TREE_VALUE (parser
->unparsed_functions_queues
)
13040 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
13042 /* Figure out which function we need to process. */
13043 fn
= TREE_VALUE (queue_entry
);
13044 /* Parse the function. */
13045 cp_parser_late_parsing_for_member (parser
, fn
);
13049 /* Put back any saved access checks. */
13050 pop_deferring_access_checks ();
13052 /* Restore the count of active template-parameter-lists. */
13053 parser
->num_template_parameter_lists
13054 = saved_num_template_parameter_lists
;
13059 /* Parse a class-head.
13062 class-key identifier [opt] base-clause [opt]
13063 class-key nested-name-specifier identifier base-clause [opt]
13064 class-key nested-name-specifier [opt] template-id
13068 class-key attributes identifier [opt] base-clause [opt]
13069 class-key attributes nested-name-specifier identifier base-clause [opt]
13070 class-key attributes nested-name-specifier [opt] template-id
13073 Returns the TYPE of the indicated class. Sets
13074 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13075 involving a nested-name-specifier was used, and FALSE otherwise.
13077 Returns error_mark_node if this is not a class-head.
13079 Returns NULL_TREE if the class-head is syntactically valid, but
13080 semantically invalid in a way that means we should skip the entire
13081 body of the class. */
13084 cp_parser_class_head (cp_parser
* parser
,
13085 bool* nested_name_specifier_p
,
13086 tree
*attributes_p
)
13088 tree nested_name_specifier
;
13089 enum tag_types class_key
;
13090 tree id
= NULL_TREE
;
13091 tree type
= NULL_TREE
;
13093 bool template_id_p
= false;
13094 bool qualified_p
= false;
13095 bool invalid_nested_name_p
= false;
13096 bool invalid_explicit_specialization_p
= false;
13097 tree pushed_scope
= NULL_TREE
;
13098 unsigned num_templates
;
13101 /* Assume no nested-name-specifier will be present. */
13102 *nested_name_specifier_p
= false;
13103 /* Assume no template parameter lists will be used in defining the
13107 /* Look for the class-key. */
13108 class_key
= cp_parser_class_key (parser
);
13109 if (class_key
== none_type
)
13110 return error_mark_node
;
13112 /* Parse the attributes. */
13113 attributes
= cp_parser_attributes_opt (parser
);
13115 /* If the next token is `::', that is invalid -- but sometimes
13116 people do try to write:
13120 Handle this gracefully by accepting the extra qualifier, and then
13121 issuing an error about it later if this really is a
13122 class-head. If it turns out just to be an elaborated type
13123 specifier, remain silent. */
13124 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
13125 qualified_p
= true;
13127 push_deferring_access_checks (dk_no_check
);
13129 /* Determine the name of the class. Begin by looking for an
13130 optional nested-name-specifier. */
13131 nested_name_specifier
13132 = cp_parser_nested_name_specifier_opt (parser
,
13133 /*typename_keyword_p=*/false,
13134 /*check_dependency_p=*/false,
13136 /*is_declaration=*/false);
13137 /* If there was a nested-name-specifier, then there *must* be an
13139 if (nested_name_specifier
)
13141 /* Although the grammar says `identifier', it really means
13142 `class-name' or `template-name'. You are only allowed to
13143 define a class that has already been declared with this
13146 The proposed resolution for Core Issue 180 says that wherever
13147 you see `class T::X' you should treat `X' as a type-name.
13149 It is OK to define an inaccessible class; for example:
13151 class A { class B; };
13154 We do not know if we will see a class-name, or a
13155 template-name. We look for a class-name first, in case the
13156 class-name is a template-id; if we looked for the
13157 template-name first we would stop after the template-name. */
13158 cp_parser_parse_tentatively (parser
);
13159 type
= cp_parser_class_name (parser
,
13160 /*typename_keyword_p=*/false,
13161 /*template_keyword_p=*/false,
13163 /*check_dependency_p=*/false,
13164 /*class_head_p=*/true,
13165 /*is_declaration=*/false);
13166 /* If that didn't work, ignore the nested-name-specifier. */
13167 if (!cp_parser_parse_definitely (parser
))
13169 invalid_nested_name_p
= true;
13170 id
= cp_parser_identifier (parser
);
13171 if (id
== error_mark_node
)
13174 /* If we could not find a corresponding TYPE, treat this
13175 declaration like an unqualified declaration. */
13176 if (type
== error_mark_node
)
13177 nested_name_specifier
= NULL_TREE
;
13178 /* Otherwise, count the number of templates used in TYPE and its
13179 containing scopes. */
13184 for (scope
= TREE_TYPE (type
);
13185 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
13186 scope
= (TYPE_P (scope
)
13187 ? TYPE_CONTEXT (scope
)
13188 : DECL_CONTEXT (scope
)))
13190 && CLASS_TYPE_P (scope
)
13191 && CLASSTYPE_TEMPLATE_INFO (scope
)
13192 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
13193 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
13197 /* Otherwise, the identifier is optional. */
13200 /* We don't know whether what comes next is a template-id,
13201 an identifier, or nothing at all. */
13202 cp_parser_parse_tentatively (parser
);
13203 /* Check for a template-id. */
13204 id
= cp_parser_template_id (parser
,
13205 /*template_keyword_p=*/false,
13206 /*check_dependency_p=*/true,
13207 /*is_declaration=*/true);
13208 /* If that didn't work, it could still be an identifier. */
13209 if (!cp_parser_parse_definitely (parser
))
13211 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13212 id
= cp_parser_identifier (parser
);
13218 template_id_p
= true;
13223 pop_deferring_access_checks ();
13226 cp_parser_check_for_invalid_template_id (parser
, id
);
13228 /* If it's not a `:' or a `{' then we can't really be looking at a
13229 class-head, since a class-head only appears as part of a
13230 class-specifier. We have to detect this situation before calling
13231 xref_tag, since that has irreversible side-effects. */
13232 if (!cp_parser_next_token_starts_class_definition_p (parser
))
13234 cp_parser_error (parser
, "expected %<{%> or %<:%>");
13235 return error_mark_node
;
13238 /* At this point, we're going ahead with the class-specifier, even
13239 if some other problem occurs. */
13240 cp_parser_commit_to_tentative_parse (parser
);
13241 /* Issue the error about the overly-qualified name now. */
13243 cp_parser_error (parser
,
13244 "global qualification of class name is invalid");
13245 else if (invalid_nested_name_p
)
13246 cp_parser_error (parser
,
13247 "qualified name does not name a class");
13248 else if (nested_name_specifier
)
13252 /* Reject typedef-names in class heads. */
13253 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
13255 error ("invalid class name in declaration of %qD", type
);
13260 /* Figure out in what scope the declaration is being placed. */
13261 scope
= current_scope ();
13262 /* If that scope does not contain the scope in which the
13263 class was originally declared, the program is invalid. */
13264 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
13266 error ("declaration of %qD in %qD which does not enclose %qD",
13267 type
, scope
, nested_name_specifier
);
13273 A declarator-id shall not be qualified exception of the
13274 definition of a ... nested class outside of its class
13275 ... [or] a the definition or explicit instantiation of a
13276 class member of a namespace outside of its namespace. */
13277 if (scope
== nested_name_specifier
)
13279 pedwarn ("extra qualification ignored");
13280 nested_name_specifier
= NULL_TREE
;
13284 /* An explicit-specialization must be preceded by "template <>". If
13285 it is not, try to recover gracefully. */
13286 if (at_namespace_scope_p ()
13287 && parser
->num_template_parameter_lists
== 0
13290 error ("an explicit specialization must be preceded by %<template <>%>");
13291 invalid_explicit_specialization_p
= true;
13292 /* Take the same action that would have been taken by
13293 cp_parser_explicit_specialization. */
13294 ++parser
->num_template_parameter_lists
;
13295 begin_specialization ();
13297 /* There must be no "return" statements between this point and the
13298 end of this function; set "type "to the correct return value and
13299 use "goto done;" to return. */
13300 /* Make sure that the right number of template parameters were
13302 if (!cp_parser_check_template_parameters (parser
, num_templates
))
13304 /* If something went wrong, there is no point in even trying to
13305 process the class-definition. */
13310 /* Look up the type. */
13313 type
= TREE_TYPE (id
);
13314 maybe_process_partial_specialization (type
);
13315 if (nested_name_specifier
)
13316 pushed_scope
= push_scope (nested_name_specifier
);
13318 else if (nested_name_specifier
)
13324 template <typename T> struct S { struct T };
13325 template <typename T> struct S<T>::T { };
13327 we will get a TYPENAME_TYPE when processing the definition of
13328 `S::T'. We need to resolve it to the actual type before we
13329 try to define it. */
13330 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
13332 class_type
= resolve_typename_type (TREE_TYPE (type
),
13333 /*only_current_p=*/false);
13334 if (class_type
!= error_mark_node
)
13335 type
= TYPE_NAME (class_type
);
13338 cp_parser_error (parser
, "could not resolve typename type");
13339 type
= error_mark_node
;
13343 maybe_process_partial_specialization (TREE_TYPE (type
));
13344 class_type
= current_class_type
;
13345 /* Enter the scope indicated by the nested-name-specifier. */
13346 pushed_scope
= push_scope (nested_name_specifier
);
13347 /* Get the canonical version of this type. */
13348 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
13349 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13350 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
13352 type
= push_template_decl (type
);
13353 if (type
== error_mark_node
)
13360 type
= TREE_TYPE (type
);
13361 *nested_name_specifier_p
= true;
13363 else /* The name is not a nested name. */
13365 /* If the class was unnamed, create a dummy name. */
13367 id
= make_anon_name ();
13368 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
13369 parser
->num_template_parameter_lists
);
13372 /* Indicate whether this class was declared as a `class' or as a
13374 if (TREE_CODE (type
) == RECORD_TYPE
)
13375 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
13376 cp_parser_check_class_key (class_key
, type
);
13378 /* If this type was already complete, and we see another definition,
13379 that's an error. */
13380 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
13382 error ("redefinition of %q#T", type
);
13383 error ("previous definition of %q+#T", type
);
13388 /* We will have entered the scope containing the class; the names of
13389 base classes should be looked up in that context. For example:
13391 struct A { struct B {}; struct C; };
13392 struct A::C : B {};
13397 /* Get the list of base-classes, if there is one. */
13398 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13399 bases
= cp_parser_base_clause (parser
);
13401 /* Process the base classes. */
13402 xref_basetypes (type
, bases
);
13405 /* Leave the scope given by the nested-name-specifier. We will
13406 enter the class scope itself while processing the members. */
13408 pop_scope (pushed_scope
);
13410 if (invalid_explicit_specialization_p
)
13412 end_specialization ();
13413 --parser
->num_template_parameter_lists
;
13415 *attributes_p
= attributes
;
13419 /* Parse a class-key.
13426 Returns the kind of class-key specified, or none_type to indicate
13429 static enum tag_types
13430 cp_parser_class_key (cp_parser
* parser
)
13433 enum tag_types tag_type
;
13435 /* Look for the class-key. */
13436 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
13440 /* Check to see if the TOKEN is a class-key. */
13441 tag_type
= cp_parser_token_is_class_key (token
);
13443 cp_parser_error (parser
, "expected class-key");
13447 /* Parse an (optional) member-specification.
13449 member-specification:
13450 member-declaration member-specification [opt]
13451 access-specifier : member-specification [opt] */
13454 cp_parser_member_specification_opt (cp_parser
* parser
)
13461 /* Peek at the next token. */
13462 token
= cp_lexer_peek_token (parser
->lexer
);
13463 /* If it's a `}', or EOF then we've seen all the members. */
13464 if (token
->type
== CPP_CLOSE_BRACE
13465 || token
->type
== CPP_EOF
13466 || token
->type
== CPP_PRAGMA_EOL
)
13469 /* See if this token is a keyword. */
13470 keyword
= token
->keyword
;
13474 case RID_PROTECTED
:
13476 /* Consume the access-specifier. */
13477 cp_lexer_consume_token (parser
->lexer
);
13478 /* Remember which access-specifier is active. */
13479 current_access_specifier
= token
->value
;
13480 /* Look for the `:'. */
13481 cp_parser_require (parser
, CPP_COLON
, "`:'");
13485 /* Accept #pragmas at class scope. */
13486 if (token
->type
== CPP_PRAGMA
)
13488 cp_parser_pragma (parser
, pragma_external
);
13492 /* Otherwise, the next construction must be a
13493 member-declaration. */
13494 cp_parser_member_declaration (parser
);
13499 /* Parse a member-declaration.
13501 member-declaration:
13502 decl-specifier-seq [opt] member-declarator-list [opt] ;
13503 function-definition ; [opt]
13504 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13506 template-declaration
13508 member-declarator-list:
13510 member-declarator-list , member-declarator
13513 declarator pure-specifier [opt]
13514 declarator constant-initializer [opt]
13515 identifier [opt] : constant-expression
13519 member-declaration:
13520 __extension__ member-declaration
13523 declarator attributes [opt] pure-specifier [opt]
13524 declarator attributes [opt] constant-initializer [opt]
13525 identifier [opt] attributes [opt] : constant-expression */
13528 cp_parser_member_declaration (cp_parser
* parser
)
13530 cp_decl_specifier_seq decl_specifiers
;
13531 tree prefix_attributes
;
13533 int declares_class_or_enum
;
13536 int saved_pedantic
;
13538 /* Check for the `__extension__' keyword. */
13539 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13542 cp_parser_member_declaration (parser
);
13543 /* Restore the old value of the PEDANTIC flag. */
13544 pedantic
= saved_pedantic
;
13549 /* Check for a template-declaration. */
13550 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
13552 /* An explicit specialization here is an error condition, and we
13553 expect the specialization handler to detect and report this. */
13554 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
13555 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
13556 cp_parser_explicit_specialization (parser
);
13558 cp_parser_template_declaration (parser
, /*member_p=*/true);
13563 /* Check for a using-declaration. */
13564 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
13566 /* Parse the using-declaration. */
13567 cp_parser_using_declaration (parser
);
13572 /* Check for @defs. */
13573 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
13576 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
13577 ivar
= ivar_chains
;
13581 ivar
= TREE_CHAIN (member
);
13582 TREE_CHAIN (member
) = NULL_TREE
;
13583 finish_member_declaration (member
);
13588 /* Parse the decl-specifier-seq. */
13589 cp_parser_decl_specifier_seq (parser
,
13590 CP_PARSER_FLAGS_OPTIONAL
,
13592 &declares_class_or_enum
);
13593 prefix_attributes
= decl_specifiers
.attributes
;
13594 decl_specifiers
.attributes
= NULL_TREE
;
13595 /* Check for an invalid type-name. */
13596 if (!decl_specifiers
.type
13597 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13599 /* If there is no declarator, then the decl-specifier-seq should
13601 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13603 /* If there was no decl-specifier-seq, and the next token is a
13604 `;', then we have something like:
13610 Each member-declaration shall declare at least one member
13611 name of the class. */
13612 if (!decl_specifiers
.any_specifiers_p
)
13614 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13615 if (pedantic
&& !token
->in_system_header
)
13616 pedwarn ("%Hextra %<;%>", &token
->location
);
13622 /* See if this declaration is a friend. */
13623 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13624 /* If there were decl-specifiers, check to see if there was
13625 a class-declaration. */
13626 type
= check_tag_decl (&decl_specifiers
);
13627 /* Nested classes have already been added to the class, but
13628 a `friend' needs to be explicitly registered. */
13631 /* If the `friend' keyword was present, the friend must
13632 be introduced with a class-key. */
13633 if (!declares_class_or_enum
)
13634 error ("a class-key must be used when declaring a friend");
13637 template <typename T> struct A {
13638 friend struct A<T>::B;
13641 A<T>::B will be represented by a TYPENAME_TYPE, and
13642 therefore not recognized by check_tag_decl. */
13644 && decl_specifiers
.type
13645 && TYPE_P (decl_specifiers
.type
))
13646 type
= decl_specifiers
.type
;
13647 if (!type
|| !TYPE_P (type
))
13648 error ("friend declaration does not name a class or "
13651 make_friend_class (current_class_type
, type
,
13652 /*complain=*/true);
13654 /* If there is no TYPE, an error message will already have
13656 else if (!type
|| type
== error_mark_node
)
13658 /* An anonymous aggregate has to be handled specially; such
13659 a declaration really declares a data member (with a
13660 particular type), as opposed to a nested class. */
13661 else if (ANON_AGGR_TYPE_P (type
))
13663 /* Remove constructors and such from TYPE, now that we
13664 know it is an anonymous aggregate. */
13665 fixup_anonymous_aggr (type
);
13666 /* And make the corresponding data member. */
13667 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
13668 /* Add it to the class. */
13669 finish_member_declaration (decl
);
13672 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
13677 /* See if these declarations will be friends. */
13678 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13680 /* Keep going until we hit the `;' at the end of the
13682 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13684 tree attributes
= NULL_TREE
;
13685 tree first_attribute
;
13687 /* Peek at the next token. */
13688 token
= cp_lexer_peek_token (parser
->lexer
);
13690 /* Check for a bitfield declaration. */
13691 if (token
->type
== CPP_COLON
13692 || (token
->type
== CPP_NAME
13693 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
13699 /* Get the name of the bitfield. Note that we cannot just
13700 check TOKEN here because it may have been invalidated by
13701 the call to cp_lexer_peek_nth_token above. */
13702 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
13703 identifier
= cp_parser_identifier (parser
);
13705 identifier
= NULL_TREE
;
13707 /* Consume the `:' token. */
13708 cp_lexer_consume_token (parser
->lexer
);
13709 /* Get the width of the bitfield. */
13711 = cp_parser_constant_expression (parser
,
13712 /*allow_non_constant=*/false,
13715 /* Look for attributes that apply to the bitfield. */
13716 attributes
= cp_parser_attributes_opt (parser
);
13717 /* Remember which attributes are prefix attributes and
13719 first_attribute
= attributes
;
13720 /* Combine the attributes. */
13721 attributes
= chainon (prefix_attributes
, attributes
);
13723 /* Create the bitfield declaration. */
13724 decl
= grokbitfield (identifier
13725 ? make_id_declarator (NULL_TREE
,
13731 /* Apply the attributes. */
13732 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
13736 cp_declarator
*declarator
;
13738 tree asm_specification
;
13739 int ctor_dtor_or_conv_p
;
13741 /* Parse the declarator. */
13743 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
13744 &ctor_dtor_or_conv_p
,
13745 /*parenthesized_p=*/NULL
,
13746 /*member_p=*/true);
13748 /* If something went wrong parsing the declarator, make sure
13749 that we at least consume some tokens. */
13750 if (declarator
== cp_error_declarator
)
13752 /* Skip to the end of the statement. */
13753 cp_parser_skip_to_end_of_statement (parser
);
13754 /* If the next token is not a semicolon, that is
13755 probably because we just skipped over the body of
13756 a function. So, we consume a semicolon if
13757 present, but do not issue an error message if it
13759 if (cp_lexer_next_token_is (parser
->lexer
,
13761 cp_lexer_consume_token (parser
->lexer
);
13765 if (declares_class_or_enum
& 2)
13766 cp_parser_check_for_definition_in_return_type
13767 (declarator
, decl_specifiers
.type
);
13769 /* Look for an asm-specification. */
13770 asm_specification
= cp_parser_asm_specification_opt (parser
);
13771 /* Look for attributes that apply to the declaration. */
13772 attributes
= cp_parser_attributes_opt (parser
);
13773 /* Remember which attributes are prefix attributes and
13775 first_attribute
= attributes
;
13776 /* Combine the attributes. */
13777 attributes
= chainon (prefix_attributes
, attributes
);
13779 /* If it's an `=', then we have a constant-initializer or a
13780 pure-specifier. It is not correct to parse the
13781 initializer before registering the member declaration
13782 since the member declaration should be in scope while
13783 its initializer is processed. However, the rest of the
13784 front end does not yet provide an interface that allows
13785 us to handle this correctly. */
13786 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13790 A pure-specifier shall be used only in the declaration of
13791 a virtual function.
13793 A member-declarator can contain a constant-initializer
13794 only if it declares a static member of integral or
13797 Therefore, if the DECLARATOR is for a function, we look
13798 for a pure-specifier; otherwise, we look for a
13799 constant-initializer. When we call `grokfield', it will
13800 perform more stringent semantics checks. */
13801 if (declarator
->kind
== cdk_function
13802 && declarator
->declarator
->kind
== cdk_id
)
13803 initializer
= cp_parser_pure_specifier (parser
);
13805 /* Parse the initializer. */
13806 initializer
= cp_parser_constant_initializer (parser
);
13808 /* Otherwise, there is no initializer. */
13810 initializer
= NULL_TREE
;
13812 /* See if we are probably looking at a function
13813 definition. We are certainly not looking at a
13814 member-declarator. Calling `grokfield' has
13815 side-effects, so we must not do it unless we are sure
13816 that we are looking at a member-declarator. */
13817 if (cp_parser_token_starts_function_definition_p
13818 (cp_lexer_peek_token (parser
->lexer
)))
13820 /* The grammar does not allow a pure-specifier to be
13821 used when a member function is defined. (It is
13822 possible that this fact is an oversight in the
13823 standard, since a pure function may be defined
13824 outside of the class-specifier. */
13826 error ("pure-specifier on function-definition");
13827 decl
= cp_parser_save_member_function_body (parser
,
13831 /* If the member was not a friend, declare it here. */
13833 finish_member_declaration (decl
);
13834 /* Peek at the next token. */
13835 token
= cp_lexer_peek_token (parser
->lexer
);
13836 /* If the next token is a semicolon, consume it. */
13837 if (token
->type
== CPP_SEMICOLON
)
13838 cp_lexer_consume_token (parser
->lexer
);
13842 /* Create the declaration. */
13843 decl
= grokfield (declarator
, &decl_specifiers
,
13844 initializer
, /*init_const_expr_p=*/true,
13849 /* Reset PREFIX_ATTRIBUTES. */
13850 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
13851 attributes
= TREE_CHAIN (attributes
);
13853 TREE_CHAIN (attributes
) = NULL_TREE
;
13855 /* If there is any qualification still in effect, clear it
13856 now; we will be starting fresh with the next declarator. */
13857 parser
->scope
= NULL_TREE
;
13858 parser
->qualifying_scope
= NULL_TREE
;
13859 parser
->object_scope
= NULL_TREE
;
13860 /* If it's a `,', then there are more declarators. */
13861 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13862 cp_lexer_consume_token (parser
->lexer
);
13863 /* If the next token isn't a `;', then we have a parse error. */
13864 else if (cp_lexer_next_token_is_not (parser
->lexer
,
13867 cp_parser_error (parser
, "expected %<;%>");
13868 /* Skip tokens until we find a `;'. */
13869 cp_parser_skip_to_end_of_statement (parser
);
13876 /* Add DECL to the list of members. */
13878 finish_member_declaration (decl
);
13880 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13881 cp_parser_save_default_args (parser
, decl
);
13886 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
13889 /* Parse a pure-specifier.
13894 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13895 Otherwise, ERROR_MARK_NODE is returned. */
13898 cp_parser_pure_specifier (cp_parser
* parser
)
13902 /* Look for the `=' token. */
13903 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
13904 return error_mark_node
;
13905 /* Look for the `0' token. */
13906 token
= cp_lexer_consume_token (parser
->lexer
);
13907 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
13908 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
13910 cp_parser_error (parser
,
13911 "invalid pure specifier (only `= 0' is allowed)");
13912 cp_parser_skip_to_end_of_statement (parser
);
13913 return error_mark_node
;
13915 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
13917 error ("templates may not be %<virtual%>");
13918 return error_mark_node
;
13921 return integer_zero_node
;
13924 /* Parse a constant-initializer.
13926 constant-initializer:
13927 = constant-expression
13929 Returns a representation of the constant-expression. */
13932 cp_parser_constant_initializer (cp_parser
* parser
)
13934 /* Look for the `=' token. */
13935 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
13936 return error_mark_node
;
13938 /* It is invalid to write:
13940 struct S { static const int i = { 7 }; };
13943 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13945 cp_parser_error (parser
,
13946 "a brace-enclosed initializer is not allowed here");
13947 /* Consume the opening brace. */
13948 cp_lexer_consume_token (parser
->lexer
);
13949 /* Skip the initializer. */
13950 cp_parser_skip_to_closing_brace (parser
);
13951 /* Look for the trailing `}'. */
13952 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
13954 return error_mark_node
;
13957 return cp_parser_constant_expression (parser
,
13958 /*allow_non_constant=*/false,
13962 /* Derived classes [gram.class.derived] */
13964 /* Parse a base-clause.
13967 : base-specifier-list
13969 base-specifier-list:
13971 base-specifier-list , base-specifier
13973 Returns a TREE_LIST representing the base-classes, in the order in
13974 which they were declared. The representation of each node is as
13975 described by cp_parser_base_specifier.
13977 In the case that no bases are specified, this function will return
13978 NULL_TREE, not ERROR_MARK_NODE. */
13981 cp_parser_base_clause (cp_parser
* parser
)
13983 tree bases
= NULL_TREE
;
13985 /* Look for the `:' that begins the list. */
13986 cp_parser_require (parser
, CPP_COLON
, "`:'");
13988 /* Scan the base-specifier-list. */
13994 /* Look for the base-specifier. */
13995 base
= cp_parser_base_specifier (parser
);
13996 /* Add BASE to the front of the list. */
13997 if (base
!= error_mark_node
)
13999 TREE_CHAIN (base
) = bases
;
14002 /* Peek at the next token. */
14003 token
= cp_lexer_peek_token (parser
->lexer
);
14004 /* If it's not a comma, then the list is complete. */
14005 if (token
->type
!= CPP_COMMA
)
14007 /* Consume the `,'. */
14008 cp_lexer_consume_token (parser
->lexer
);
14011 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14012 base class had a qualified name. However, the next name that
14013 appears is certainly not qualified. */
14014 parser
->scope
= NULL_TREE
;
14015 parser
->qualifying_scope
= NULL_TREE
;
14016 parser
->object_scope
= NULL_TREE
;
14018 return nreverse (bases
);
14021 /* Parse a base-specifier.
14024 :: [opt] nested-name-specifier [opt] class-name
14025 virtual access-specifier [opt] :: [opt] nested-name-specifier
14027 access-specifier virtual [opt] :: [opt] nested-name-specifier
14030 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14031 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14032 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14033 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14036 cp_parser_base_specifier (cp_parser
* parser
)
14040 bool virtual_p
= false;
14041 bool duplicate_virtual_error_issued_p
= false;
14042 bool duplicate_access_error_issued_p
= false;
14043 bool class_scope_p
, template_p
;
14044 tree access
= access_default_node
;
14047 /* Process the optional `virtual' and `access-specifier'. */
14050 /* Peek at the next token. */
14051 token
= cp_lexer_peek_token (parser
->lexer
);
14052 /* Process `virtual'. */
14053 switch (token
->keyword
)
14056 /* If `virtual' appears more than once, issue an error. */
14057 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
14059 cp_parser_error (parser
,
14060 "%<virtual%> specified more than once in base-specified");
14061 duplicate_virtual_error_issued_p
= true;
14066 /* Consume the `virtual' token. */
14067 cp_lexer_consume_token (parser
->lexer
);
14072 case RID_PROTECTED
:
14074 /* If more than one access specifier appears, issue an
14076 if (access
!= access_default_node
14077 && !duplicate_access_error_issued_p
)
14079 cp_parser_error (parser
,
14080 "more than one access specifier in base-specified");
14081 duplicate_access_error_issued_p
= true;
14084 access
= ridpointers
[(int) token
->keyword
];
14086 /* Consume the access-specifier. */
14087 cp_lexer_consume_token (parser
->lexer
);
14096 /* It is not uncommon to see programs mechanically, erroneously, use
14097 the 'typename' keyword to denote (dependent) qualified types
14098 as base classes. */
14099 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14101 if (!processing_template_decl
)
14102 error ("keyword %<typename%> not allowed outside of templates");
14104 error ("keyword %<typename%> not allowed in this context "
14105 "(the base class is implicitly a type)");
14106 cp_lexer_consume_token (parser
->lexer
);
14109 /* Look for the optional `::' operator. */
14110 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
14111 /* Look for the nested-name-specifier. The simplest way to
14116 The keyword `typename' is not permitted in a base-specifier or
14117 mem-initializer; in these contexts a qualified name that
14118 depends on a template-parameter is implicitly assumed to be a
14121 is to pretend that we have seen the `typename' keyword at this
14123 cp_parser_nested_name_specifier_opt (parser
,
14124 /*typename_keyword_p=*/true,
14125 /*check_dependency_p=*/true,
14127 /*is_declaration=*/true);
14128 /* If the base class is given by a qualified name, assume that names
14129 we see are type names or templates, as appropriate. */
14130 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
14131 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
14133 /* Finally, look for the class-name. */
14134 type
= cp_parser_class_name (parser
,
14138 /*check_dependency_p=*/true,
14139 /*class_head_p=*/false,
14140 /*is_declaration=*/true);
14142 if (type
== error_mark_node
)
14143 return error_mark_node
;
14145 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
14148 /* Exception handling [gram.exception] */
14150 /* Parse an (optional) exception-specification.
14152 exception-specification:
14153 throw ( type-id-list [opt] )
14155 Returns a TREE_LIST representing the exception-specification. The
14156 TREE_VALUE of each node is a type. */
14159 cp_parser_exception_specification_opt (cp_parser
* parser
)
14164 /* Peek at the next token. */
14165 token
= cp_lexer_peek_token (parser
->lexer
);
14166 /* If it's not `throw', then there's no exception-specification. */
14167 if (!cp_parser_is_keyword (token
, RID_THROW
))
14170 /* Consume the `throw'. */
14171 cp_lexer_consume_token (parser
->lexer
);
14173 /* Look for the `('. */
14174 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14176 /* Peek at the next token. */
14177 token
= cp_lexer_peek_token (parser
->lexer
);
14178 /* If it's not a `)', then there is a type-id-list. */
14179 if (token
->type
!= CPP_CLOSE_PAREN
)
14181 const char *saved_message
;
14183 /* Types may not be defined in an exception-specification. */
14184 saved_message
= parser
->type_definition_forbidden_message
;
14185 parser
->type_definition_forbidden_message
14186 = "types may not be defined in an exception-specification";
14187 /* Parse the type-id-list. */
14188 type_id_list
= cp_parser_type_id_list (parser
);
14189 /* Restore the saved message. */
14190 parser
->type_definition_forbidden_message
= saved_message
;
14193 type_id_list
= empty_except_spec
;
14195 /* Look for the `)'. */
14196 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14198 return type_id_list
;
14201 /* Parse an (optional) type-id-list.
14205 type-id-list , type-id
14207 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14208 in the order that the types were presented. */
14211 cp_parser_type_id_list (cp_parser
* parser
)
14213 tree types
= NULL_TREE
;
14220 /* Get the next type-id. */
14221 type
= cp_parser_type_id (parser
);
14222 /* Add it to the list. */
14223 types
= add_exception_specifier (types
, type
, /*complain=*/1);
14224 /* Peek at the next token. */
14225 token
= cp_lexer_peek_token (parser
->lexer
);
14226 /* If it is not a `,', we are done. */
14227 if (token
->type
!= CPP_COMMA
)
14229 /* Consume the `,'. */
14230 cp_lexer_consume_token (parser
->lexer
);
14233 return nreverse (types
);
14236 /* Parse a try-block.
14239 try compound-statement handler-seq */
14242 cp_parser_try_block (cp_parser
* parser
)
14246 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
14247 try_block
= begin_try_block ();
14248 cp_parser_compound_statement (parser
, NULL
, true);
14249 finish_try_block (try_block
);
14250 cp_parser_handler_seq (parser
);
14251 finish_handler_sequence (try_block
);
14256 /* Parse a function-try-block.
14258 function-try-block:
14259 try ctor-initializer [opt] function-body handler-seq */
14262 cp_parser_function_try_block (cp_parser
* parser
)
14264 tree compound_stmt
;
14266 bool ctor_initializer_p
;
14268 /* Look for the `try' keyword. */
14269 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
14271 /* Let the rest of the front-end know where we are. */
14272 try_block
= begin_function_try_block (&compound_stmt
);
14273 /* Parse the function-body. */
14275 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
14276 /* We're done with the `try' part. */
14277 finish_function_try_block (try_block
);
14278 /* Parse the handlers. */
14279 cp_parser_handler_seq (parser
);
14280 /* We're done with the handlers. */
14281 finish_function_handler_sequence (try_block
, compound_stmt
);
14283 return ctor_initializer_p
;
14286 /* Parse a handler-seq.
14289 handler handler-seq [opt] */
14292 cp_parser_handler_seq (cp_parser
* parser
)
14298 /* Parse the handler. */
14299 cp_parser_handler (parser
);
14300 /* Peek at the next token. */
14301 token
= cp_lexer_peek_token (parser
->lexer
);
14302 /* If it's not `catch' then there are no more handlers. */
14303 if (!cp_parser_is_keyword (token
, RID_CATCH
))
14308 /* Parse a handler.
14311 catch ( exception-declaration ) compound-statement */
14314 cp_parser_handler (cp_parser
* parser
)
14319 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
14320 handler
= begin_handler ();
14321 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14322 declaration
= cp_parser_exception_declaration (parser
);
14323 finish_handler_parms (declaration
, handler
);
14324 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14325 cp_parser_compound_statement (parser
, NULL
, false);
14326 finish_handler (handler
);
14329 /* Parse an exception-declaration.
14331 exception-declaration:
14332 type-specifier-seq declarator
14333 type-specifier-seq abstract-declarator
14337 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14338 ellipsis variant is used. */
14341 cp_parser_exception_declaration (cp_parser
* parser
)
14344 cp_decl_specifier_seq type_specifiers
;
14345 cp_declarator
*declarator
;
14346 const char *saved_message
;
14348 /* If it's an ellipsis, it's easy to handle. */
14349 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14351 /* Consume the `...' token. */
14352 cp_lexer_consume_token (parser
->lexer
);
14356 /* Types may not be defined in exception-declarations. */
14357 saved_message
= parser
->type_definition_forbidden_message
;
14358 parser
->type_definition_forbidden_message
14359 = "types may not be defined in exception-declarations";
14361 /* Parse the type-specifier-seq. */
14362 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
14364 /* If it's a `)', then there is no declarator. */
14365 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
14368 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
14369 /*ctor_dtor_or_conv_p=*/NULL
,
14370 /*parenthesized_p=*/NULL
,
14371 /*member_p=*/false);
14373 /* Restore the saved message. */
14374 parser
->type_definition_forbidden_message
= saved_message
;
14376 if (type_specifiers
.any_specifiers_p
)
14378 decl
= grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
14379 if (decl
== NULL_TREE
)
14380 error ("invalid catch parameter");
14388 /* Parse a throw-expression.
14391 throw assignment-expression [opt]
14393 Returns a THROW_EXPR representing the throw-expression. */
14396 cp_parser_throw_expression (cp_parser
* parser
)
14401 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
14402 token
= cp_lexer_peek_token (parser
->lexer
);
14403 /* Figure out whether or not there is an assignment-expression
14404 following the "throw" keyword. */
14405 if (token
->type
== CPP_COMMA
14406 || token
->type
== CPP_SEMICOLON
14407 || token
->type
== CPP_CLOSE_PAREN
14408 || token
->type
== CPP_CLOSE_SQUARE
14409 || token
->type
== CPP_CLOSE_BRACE
14410 || token
->type
== CPP_COLON
)
14411 expression
= NULL_TREE
;
14413 expression
= cp_parser_assignment_expression (parser
,
14416 return build_throw (expression
);
14419 /* GNU Extensions */
14421 /* Parse an (optional) asm-specification.
14424 asm ( string-literal )
14426 If the asm-specification is present, returns a STRING_CST
14427 corresponding to the string-literal. Otherwise, returns
14431 cp_parser_asm_specification_opt (cp_parser
* parser
)
14434 tree asm_specification
;
14436 /* Peek at the next token. */
14437 token
= cp_lexer_peek_token (parser
->lexer
);
14438 /* If the next token isn't the `asm' keyword, then there's no
14439 asm-specification. */
14440 if (!cp_parser_is_keyword (token
, RID_ASM
))
14443 /* Consume the `asm' token. */
14444 cp_lexer_consume_token (parser
->lexer
);
14445 /* Look for the `('. */
14446 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14448 /* Look for the string-literal. */
14449 asm_specification
= cp_parser_string_literal (parser
, false, false);
14451 /* Look for the `)'. */
14452 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
14454 return asm_specification
;
14457 /* Parse an asm-operand-list.
14461 asm-operand-list , asm-operand
14464 string-literal ( expression )
14465 [ string-literal ] string-literal ( expression )
14467 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14468 each node is the expression. The TREE_PURPOSE is itself a
14469 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14470 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14471 is a STRING_CST for the string literal before the parenthesis. */
14474 cp_parser_asm_operand_list (cp_parser
* parser
)
14476 tree asm_operands
= NULL_TREE
;
14480 tree string_literal
;
14484 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
14486 /* Consume the `[' token. */
14487 cp_lexer_consume_token (parser
->lexer
);
14488 /* Read the operand name. */
14489 name
= cp_parser_identifier (parser
);
14490 if (name
!= error_mark_node
)
14491 name
= build_string (IDENTIFIER_LENGTH (name
),
14492 IDENTIFIER_POINTER (name
));
14493 /* Look for the closing `]'. */
14494 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
14498 /* Look for the string-literal. */
14499 string_literal
= cp_parser_string_literal (parser
, false, false);
14501 /* Look for the `('. */
14502 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14503 /* Parse the expression. */
14504 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
14505 /* Look for the `)'. */
14506 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14508 /* Add this operand to the list. */
14509 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
14512 /* If the next token is not a `,', there are no more
14514 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14516 /* Consume the `,'. */
14517 cp_lexer_consume_token (parser
->lexer
);
14520 return nreverse (asm_operands
);
14523 /* Parse an asm-clobber-list.
14527 asm-clobber-list , string-literal
14529 Returns a TREE_LIST, indicating the clobbers in the order that they
14530 appeared. The TREE_VALUE of each node is a STRING_CST. */
14533 cp_parser_asm_clobber_list (cp_parser
* parser
)
14535 tree clobbers
= NULL_TREE
;
14539 tree string_literal
;
14541 /* Look for the string literal. */
14542 string_literal
= cp_parser_string_literal (parser
, false, false);
14543 /* Add it to the list. */
14544 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
14545 /* If the next token is not a `,', then the list is
14547 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14549 /* Consume the `,' token. */
14550 cp_lexer_consume_token (parser
->lexer
);
14556 /* Parse an (optional) series of attributes.
14559 attributes attribute
14562 __attribute__ (( attribute-list [opt] ))
14564 The return value is as for cp_parser_attribute_list. */
14567 cp_parser_attributes_opt (cp_parser
* parser
)
14569 tree attributes
= NULL_TREE
;
14574 tree attribute_list
;
14576 /* Peek at the next token. */
14577 token
= cp_lexer_peek_token (parser
->lexer
);
14578 /* If it's not `__attribute__', then we're done. */
14579 if (token
->keyword
!= RID_ATTRIBUTE
)
14582 /* Consume the `__attribute__' keyword. */
14583 cp_lexer_consume_token (parser
->lexer
);
14584 /* Look for the two `(' tokens. */
14585 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14586 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14588 /* Peek at the next token. */
14589 token
= cp_lexer_peek_token (parser
->lexer
);
14590 if (token
->type
!= CPP_CLOSE_PAREN
)
14591 /* Parse the attribute-list. */
14592 attribute_list
= cp_parser_attribute_list (parser
);
14594 /* If the next token is a `)', then there is no attribute
14596 attribute_list
= NULL
;
14598 /* Look for the two `)' tokens. */
14599 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14600 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14602 /* Add these new attributes to the list. */
14603 attributes
= chainon (attributes
, attribute_list
);
14609 /* Parse an attribute-list.
14613 attribute-list , attribute
14617 identifier ( identifier )
14618 identifier ( identifier , expression-list )
14619 identifier ( expression-list )
14621 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14622 to an attribute. The TREE_PURPOSE of each node is the identifier
14623 indicating which attribute is in use. The TREE_VALUE represents
14624 the arguments, if any. */
14627 cp_parser_attribute_list (cp_parser
* parser
)
14629 tree attribute_list
= NULL_TREE
;
14630 bool save_translate_strings_p
= parser
->translate_strings_p
;
14632 parser
->translate_strings_p
= false;
14639 /* Look for the identifier. We also allow keywords here; for
14640 example `__attribute__ ((const))' is legal. */
14641 token
= cp_lexer_peek_token (parser
->lexer
);
14642 if (token
->type
== CPP_NAME
14643 || token
->type
== CPP_KEYWORD
)
14645 tree arguments
= NULL_TREE
;
14647 /* Consume the token. */
14648 token
= cp_lexer_consume_token (parser
->lexer
);
14650 /* Save away the identifier that indicates which attribute
14652 identifier
= token
->value
;
14653 attribute
= build_tree_list (identifier
, NULL_TREE
);
14655 /* Peek at the next token. */
14656 token
= cp_lexer_peek_token (parser
->lexer
);
14657 /* If it's an `(', then parse the attribute arguments. */
14658 if (token
->type
== CPP_OPEN_PAREN
)
14660 arguments
= cp_parser_parenthesized_expression_list
14661 (parser
, true, /*cast_p=*/false,
14662 /*non_constant_p=*/NULL
);
14663 /* Save the arguments away. */
14664 TREE_VALUE (attribute
) = arguments
;
14667 if (arguments
!= error_mark_node
)
14669 /* Add this attribute to the list. */
14670 TREE_CHAIN (attribute
) = attribute_list
;
14671 attribute_list
= attribute
;
14674 token
= cp_lexer_peek_token (parser
->lexer
);
14676 /* Now, look for more attributes. If the next token isn't a
14677 `,', we're done. */
14678 if (token
->type
!= CPP_COMMA
)
14681 /* Consume the comma and keep going. */
14682 cp_lexer_consume_token (parser
->lexer
);
14684 parser
->translate_strings_p
= save_translate_strings_p
;
14686 /* We built up the list in reverse order. */
14687 return nreverse (attribute_list
);
14690 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14691 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14692 current value of the PEDANTIC flag, regardless of whether or not
14693 the `__extension__' keyword is present. The caller is responsible
14694 for restoring the value of the PEDANTIC flag. */
14697 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
14699 /* Save the old value of the PEDANTIC flag. */
14700 *saved_pedantic
= pedantic
;
14702 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
14704 /* Consume the `__extension__' token. */
14705 cp_lexer_consume_token (parser
->lexer
);
14706 /* We're not being pedantic while the `__extension__' keyword is
14716 /* Parse a label declaration.
14719 __label__ label-declarator-seq ;
14721 label-declarator-seq:
14722 identifier , label-declarator-seq
14726 cp_parser_label_declaration (cp_parser
* parser
)
14728 /* Look for the `__label__' keyword. */
14729 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
14735 /* Look for an identifier. */
14736 identifier
= cp_parser_identifier (parser
);
14737 /* If we failed, stop. */
14738 if (identifier
== error_mark_node
)
14740 /* Declare it as a label. */
14741 finish_label_decl (identifier
);
14742 /* If the next token is a `;', stop. */
14743 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14745 /* Look for the `,' separating the label declarations. */
14746 cp_parser_require (parser
, CPP_COMMA
, "`,'");
14749 /* Look for the final `;'. */
14750 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
14753 /* Support Functions */
14755 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14756 NAME should have one of the representations used for an
14757 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14758 is returned. If PARSER->SCOPE is a dependent type, then a
14759 SCOPE_REF is returned.
14761 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14762 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14763 was formed. Abstractly, such entities should not be passed to this
14764 function, because they do not need to be looked up, but it is
14765 simpler to check for this special case here, rather than at the
14768 In cases not explicitly covered above, this function returns a
14769 DECL, OVERLOAD, or baselink representing the result of the lookup.
14770 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14773 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14774 (e.g., "struct") that was used. In that case bindings that do not
14775 refer to types are ignored.
14777 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14780 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14783 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14786 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14787 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14788 NULL_TREE otherwise. */
14791 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
14792 enum tag_types tag_type
,
14795 bool check_dependency
,
14796 tree
*ambiguous_decls
)
14800 tree object_type
= parser
->context
->object_type
;
14802 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
14803 flags
|= LOOKUP_COMPLAIN
;
14805 /* Assume that the lookup will be unambiguous. */
14806 if (ambiguous_decls
)
14807 *ambiguous_decls
= NULL_TREE
;
14809 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14810 no longer valid. Note that if we are parsing tentatively, and
14811 the parse fails, OBJECT_TYPE will be automatically restored. */
14812 parser
->context
->object_type
= NULL_TREE
;
14814 if (name
== error_mark_node
)
14815 return error_mark_node
;
14817 /* A template-id has already been resolved; there is no lookup to
14819 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14821 if (BASELINK_P (name
))
14823 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
14824 == TEMPLATE_ID_EXPR
);
14828 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14829 it should already have been checked to make sure that the name
14830 used matches the type being destroyed. */
14831 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
14835 /* Figure out to which type this destructor applies. */
14837 type
= parser
->scope
;
14838 else if (object_type
)
14839 type
= object_type
;
14841 type
= current_class_type
;
14842 /* If that's not a class type, there is no destructor. */
14843 if (!type
|| !CLASS_TYPE_P (type
))
14844 return error_mark_node
;
14845 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
14846 lazily_declare_fn (sfk_destructor
, type
);
14847 if (!CLASSTYPE_DESTRUCTORS (type
))
14848 return error_mark_node
;
14849 /* If it was a class type, return the destructor. */
14850 return CLASSTYPE_DESTRUCTORS (type
);
14853 /* By this point, the NAME should be an ordinary identifier. If
14854 the id-expression was a qualified name, the qualifying scope is
14855 stored in PARSER->SCOPE at this point. */
14856 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
14858 /* Perform the lookup. */
14863 if (parser
->scope
== error_mark_node
)
14864 return error_mark_node
;
14866 /* If the SCOPE is dependent, the lookup must be deferred until
14867 the template is instantiated -- unless we are explicitly
14868 looking up names in uninstantiated templates. Even then, we
14869 cannot look up the name if the scope is not a class type; it
14870 might, for example, be a template type parameter. */
14871 dependent_p
= (TYPE_P (parser
->scope
)
14872 && !(parser
->in_declarator_p
14873 && currently_open_class (parser
->scope
))
14874 && dependent_type_p (parser
->scope
));
14875 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
14882 /* The resolution to Core Issue 180 says that `struct
14883 A::B' should be considered a type-name, even if `A'
14885 type
= make_typename_type (parser
->scope
, name
, tag_type
,
14886 /*complain=*/tf_error
);
14887 decl
= TYPE_NAME (type
);
14889 else if (is_template
14890 && (cp_parser_next_token_ends_template_argument_p (parser
)
14891 || cp_lexer_next_token_is (parser
->lexer
,
14893 decl
= make_unbound_class_template (parser
->scope
,
14895 /*complain=*/tf_error
);
14897 decl
= build_qualified_name (/*type=*/NULL_TREE
,
14898 parser
->scope
, name
,
14903 tree pushed_scope
= NULL_TREE
;
14905 /* If PARSER->SCOPE is a dependent type, then it must be a
14906 class type, and we must not be checking dependencies;
14907 otherwise, we would have processed this lookup above. So
14908 that PARSER->SCOPE is not considered a dependent base by
14909 lookup_member, we must enter the scope here. */
14911 pushed_scope
= push_scope (parser
->scope
);
14912 /* If the PARSER->SCOPE is a template specialization, it
14913 may be instantiated during name lookup. In that case,
14914 errors may be issued. Even if we rollback the current
14915 tentative parse, those errors are valid. */
14916 decl
= lookup_qualified_name (parser
->scope
, name
,
14917 tag_type
!= none_type
,
14918 /*complain=*/true);
14920 pop_scope (pushed_scope
);
14922 parser
->qualifying_scope
= parser
->scope
;
14923 parser
->object_scope
= NULL_TREE
;
14925 else if (object_type
)
14927 tree object_decl
= NULL_TREE
;
14928 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14929 OBJECT_TYPE is not a class. */
14930 if (CLASS_TYPE_P (object_type
))
14931 /* If the OBJECT_TYPE is a template specialization, it may
14932 be instantiated during name lookup. In that case, errors
14933 may be issued. Even if we rollback the current tentative
14934 parse, those errors are valid. */
14935 object_decl
= lookup_member (object_type
,
14938 tag_type
!= none_type
);
14939 /* Look it up in the enclosing context, too. */
14940 decl
= lookup_name_real (name
, tag_type
!= none_type
,
14942 /*block_p=*/true, is_namespace
, flags
);
14943 parser
->object_scope
= object_type
;
14944 parser
->qualifying_scope
= NULL_TREE
;
14946 decl
= object_decl
;
14950 decl
= lookup_name_real (name
, tag_type
!= none_type
,
14952 /*block_p=*/true, is_namespace
, flags
);
14953 parser
->qualifying_scope
= NULL_TREE
;
14954 parser
->object_scope
= NULL_TREE
;
14957 /* If the lookup failed, let our caller know. */
14958 if (!decl
|| decl
== error_mark_node
)
14959 return error_mark_node
;
14961 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14962 if (TREE_CODE (decl
) == TREE_LIST
)
14964 if (ambiguous_decls
)
14965 *ambiguous_decls
= decl
;
14966 /* The error message we have to print is too complicated for
14967 cp_parser_error, so we incorporate its actions directly. */
14968 if (!cp_parser_simulate_error (parser
))
14970 error ("reference to %qD is ambiguous", name
);
14971 print_candidates (decl
);
14973 return error_mark_node
;
14976 gcc_assert (DECL_P (decl
)
14977 || TREE_CODE (decl
) == OVERLOAD
14978 || TREE_CODE (decl
) == SCOPE_REF
14979 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
14980 || BASELINK_P (decl
));
14982 /* If we have resolved the name of a member declaration, check to
14983 see if the declaration is accessible. When the name resolves to
14984 set of overloaded functions, accessibility is checked when
14985 overload resolution is done.
14987 During an explicit instantiation, access is not checked at all,
14988 as per [temp.explicit]. */
14990 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
14995 /* Like cp_parser_lookup_name, but for use in the typical case where
14996 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14997 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15000 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
15002 return cp_parser_lookup_name (parser
, name
,
15004 /*is_template=*/false,
15005 /*is_namespace=*/false,
15006 /*check_dependency=*/true,
15007 /*ambiguous_decls=*/NULL
);
15010 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15011 the current context, return the TYPE_DECL. If TAG_NAME_P is
15012 true, the DECL indicates the class being defined in a class-head,
15013 or declared in an elaborated-type-specifier.
15015 Otherwise, return DECL. */
15018 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
15020 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15021 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15024 template <typename T> struct B;
15027 template <typename T> struct A::B {};
15029 Similarly, in an elaborated-type-specifier:
15031 namespace N { struct X{}; }
15034 template <typename T> friend struct N::X;
15037 However, if the DECL refers to a class type, and we are in
15038 the scope of the class, then the name lookup automatically
15039 finds the TYPE_DECL created by build_self_reference rather
15040 than a TEMPLATE_DECL. For example, in:
15042 template <class T> struct S {
15046 there is no need to handle such case. */
15048 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
15049 return DECL_TEMPLATE_RESULT (decl
);
15054 /* If too many, or too few, template-parameter lists apply to the
15055 declarator, issue an error message. Returns TRUE if all went well,
15056 and FALSE otherwise. */
15059 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
15060 cp_declarator
*declarator
)
15062 unsigned num_templates
;
15064 /* We haven't seen any classes that involve template parameters yet. */
15067 switch (declarator
->kind
)
15070 if (declarator
->u
.id
.qualifying_scope
)
15075 scope
= declarator
->u
.id
.qualifying_scope
;
15076 member
= declarator
->u
.id
.unqualified_name
;
15078 while (scope
&& CLASS_TYPE_P (scope
))
15080 /* You're supposed to have one `template <...>'
15081 for every template class, but you don't need one
15082 for a full specialization. For example:
15084 template <class T> struct S{};
15085 template <> struct S<int> { void f(); };
15086 void S<int>::f () {}
15088 is correct; there shouldn't be a `template <>' for
15089 the definition of `S<int>::f'. */
15090 if (CLASSTYPE_TEMPLATE_INFO (scope
)
15091 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope
)
15092 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
)))
15093 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
15096 scope
= TYPE_CONTEXT (scope
);
15099 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
15100 == TEMPLATE_ID_EXPR
)
15101 /* If the DECLARATOR has the form `X<y>' then it uses one
15102 additional level of template parameters. */
15105 return cp_parser_check_template_parameters (parser
,
15111 case cdk_reference
:
15113 return (cp_parser_check_declarator_template_parameters
15114 (parser
, declarator
->declarator
));
15120 gcc_unreachable ();
15125 /* NUM_TEMPLATES were used in the current declaration. If that is
15126 invalid, return FALSE and issue an error messages. Otherwise,
15130 cp_parser_check_template_parameters (cp_parser
* parser
,
15131 unsigned num_templates
)
15133 /* If there are more template classes than parameter lists, we have
15136 template <class T> void S<T>::R<T>::f (); */
15137 if (parser
->num_template_parameter_lists
< num_templates
)
15139 error ("too few template-parameter-lists");
15142 /* If there are the same number of template classes and parameter
15143 lists, that's OK. */
15144 if (parser
->num_template_parameter_lists
== num_templates
)
15146 /* If there are more, but only one more, then we are referring to a
15147 member template. That's OK too. */
15148 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
15150 /* Otherwise, there are too many template parameter lists. We have
15153 template <class T> template <class U> void S::f(); */
15154 error ("too many template-parameter-lists");
15158 /* Parse an optional `::' token indicating that the following name is
15159 from the global namespace. If so, PARSER->SCOPE is set to the
15160 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15161 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15162 Returns the new value of PARSER->SCOPE, if the `::' token is
15163 present, and NULL_TREE otherwise. */
15166 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
15170 /* Peek at the next token. */
15171 token
= cp_lexer_peek_token (parser
->lexer
);
15172 /* If we're looking at a `::' token then we're starting from the
15173 global namespace, not our current location. */
15174 if (token
->type
== CPP_SCOPE
)
15176 /* Consume the `::' token. */
15177 cp_lexer_consume_token (parser
->lexer
);
15178 /* Set the SCOPE so that we know where to start the lookup. */
15179 parser
->scope
= global_namespace
;
15180 parser
->qualifying_scope
= global_namespace
;
15181 parser
->object_scope
= NULL_TREE
;
15183 return parser
->scope
;
15185 else if (!current_scope_valid_p
)
15187 parser
->scope
= NULL_TREE
;
15188 parser
->qualifying_scope
= NULL_TREE
;
15189 parser
->object_scope
= NULL_TREE
;
15195 /* Returns TRUE if the upcoming token sequence is the start of a
15196 constructor declarator. If FRIEND_P is true, the declarator is
15197 preceded by the `friend' specifier. */
15200 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
15202 bool constructor_p
;
15203 tree type_decl
= NULL_TREE
;
15204 bool nested_name_p
;
15205 cp_token
*next_token
;
15207 /* The common case is that this is not a constructor declarator, so
15208 try to avoid doing lots of work if at all possible. It's not
15209 valid declare a constructor at function scope. */
15210 if (at_function_scope_p ())
15212 /* And only certain tokens can begin a constructor declarator. */
15213 next_token
= cp_lexer_peek_token (parser
->lexer
);
15214 if (next_token
->type
!= CPP_NAME
15215 && next_token
->type
!= CPP_SCOPE
15216 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
15217 && next_token
->type
!= CPP_TEMPLATE_ID
)
15220 /* Parse tentatively; we are going to roll back all of the tokens
15222 cp_parser_parse_tentatively (parser
);
15223 /* Assume that we are looking at a constructor declarator. */
15224 constructor_p
= true;
15226 /* Look for the optional `::' operator. */
15227 cp_parser_global_scope_opt (parser
,
15228 /*current_scope_valid_p=*/false);
15229 /* Look for the nested-name-specifier. */
15231 = (cp_parser_nested_name_specifier_opt (parser
,
15232 /*typename_keyword_p=*/false,
15233 /*check_dependency_p=*/false,
15235 /*is_declaration=*/false)
15237 /* Outside of a class-specifier, there must be a
15238 nested-name-specifier. */
15239 if (!nested_name_p
&&
15240 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
15242 constructor_p
= false;
15243 /* If we still think that this might be a constructor-declarator,
15244 look for a class-name. */
15249 template <typename T> struct S { S(); };
15250 template <typename T> S<T>::S ();
15252 we must recognize that the nested `S' names a class.
15255 template <typename T> S<T>::S<T> ();
15257 we must recognize that the nested `S' names a template. */
15258 type_decl
= cp_parser_class_name (parser
,
15259 /*typename_keyword_p=*/false,
15260 /*template_keyword_p=*/false,
15262 /*check_dependency_p=*/false,
15263 /*class_head_p=*/false,
15264 /*is_declaration=*/false);
15265 /* If there was no class-name, then this is not a constructor. */
15266 constructor_p
= !cp_parser_error_occurred (parser
);
15269 /* If we're still considering a constructor, we have to see a `(',
15270 to begin the parameter-declaration-clause, followed by either a
15271 `)', an `...', or a decl-specifier. We need to check for a
15272 type-specifier to avoid being fooled into thinking that:
15276 is a constructor. (It is actually a function named `f' that
15277 takes one parameter (of type `int') and returns a value of type
15280 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
15282 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
15283 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
15284 /* A parameter declaration begins with a decl-specifier,
15285 which is either the "attribute" keyword, a storage class
15286 specifier, or (usually) a type-specifier. */
15287 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
)
15288 && !cp_parser_storage_class_specifier_opt (parser
))
15291 tree pushed_scope
= NULL_TREE
;
15292 unsigned saved_num_template_parameter_lists
;
15294 /* Names appearing in the type-specifier should be looked up
15295 in the scope of the class. */
15296 if (current_class_type
)
15300 type
= TREE_TYPE (type_decl
);
15301 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15303 type
= resolve_typename_type (type
,
15304 /*only_current_p=*/false);
15305 if (type
== error_mark_node
)
15307 cp_parser_abort_tentative_parse (parser
);
15311 pushed_scope
= push_scope (type
);
15314 /* Inside the constructor parameter list, surrounding
15315 template-parameter-lists do not apply. */
15316 saved_num_template_parameter_lists
15317 = parser
->num_template_parameter_lists
;
15318 parser
->num_template_parameter_lists
= 0;
15320 /* Look for the type-specifier. */
15321 cp_parser_type_specifier (parser
,
15322 CP_PARSER_FLAGS_NONE
,
15323 /*decl_specs=*/NULL
,
15324 /*is_declarator=*/true,
15325 /*declares_class_or_enum=*/NULL
,
15326 /*is_cv_qualifier=*/NULL
);
15328 parser
->num_template_parameter_lists
15329 = saved_num_template_parameter_lists
;
15331 /* Leave the scope of the class. */
15333 pop_scope (pushed_scope
);
15335 constructor_p
= !cp_parser_error_occurred (parser
);
15339 constructor_p
= false;
15340 /* We did not really want to consume any tokens. */
15341 cp_parser_abort_tentative_parse (parser
);
15343 return constructor_p
;
15346 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15347 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15348 they must be performed once we are in the scope of the function.
15350 Returns the function defined. */
15353 cp_parser_function_definition_from_specifiers_and_declarator
15354 (cp_parser
* parser
,
15355 cp_decl_specifier_seq
*decl_specifiers
,
15357 const cp_declarator
*declarator
)
15362 /* Begin the function-definition. */
15363 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
15365 /* The things we're about to see are not directly qualified by any
15366 template headers we've seen thus far. */
15367 reset_specialization ();
15369 /* If there were names looked up in the decl-specifier-seq that we
15370 did not check, check them now. We must wait until we are in the
15371 scope of the function to perform the checks, since the function
15372 might be a friend. */
15373 perform_deferred_access_checks ();
15377 /* Skip the entire function. */
15378 cp_parser_skip_to_end_of_block_or_statement (parser
);
15379 fn
= error_mark_node
;
15382 fn
= cp_parser_function_definition_after_declarator (parser
,
15383 /*inline_p=*/false);
15388 /* Parse the part of a function-definition that follows the
15389 declarator. INLINE_P is TRUE iff this function is an inline
15390 function defined with a class-specifier.
15392 Returns the function defined. */
15395 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
15399 bool ctor_initializer_p
= false;
15400 bool saved_in_unbraced_linkage_specification_p
;
15401 unsigned saved_num_template_parameter_lists
;
15403 /* If the next token is `return', then the code may be trying to
15404 make use of the "named return value" extension that G++ used to
15406 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
15408 /* Consume the `return' keyword. */
15409 cp_lexer_consume_token (parser
->lexer
);
15410 /* Look for the identifier that indicates what value is to be
15412 cp_parser_identifier (parser
);
15413 /* Issue an error message. */
15414 error ("named return values are no longer supported");
15415 /* Skip tokens until we reach the start of the function body. */
15418 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15419 if (token
->type
== CPP_OPEN_BRACE
15420 || token
->type
== CPP_EOF
15421 || token
->type
== CPP_PRAGMA_EOL
)
15423 cp_lexer_consume_token (parser
->lexer
);
15426 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15427 anything declared inside `f'. */
15428 saved_in_unbraced_linkage_specification_p
15429 = parser
->in_unbraced_linkage_specification_p
;
15430 parser
->in_unbraced_linkage_specification_p
= false;
15431 /* Inside the function, surrounding template-parameter-lists do not
15433 saved_num_template_parameter_lists
15434 = parser
->num_template_parameter_lists
;
15435 parser
->num_template_parameter_lists
= 0;
15436 /* If the next token is `try', then we are looking at a
15437 function-try-block. */
15438 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
15439 ctor_initializer_p
= cp_parser_function_try_block (parser
);
15440 /* A function-try-block includes the function-body, so we only do
15441 this next part if we're not processing a function-try-block. */
15444 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
15446 /* Finish the function. */
15447 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
15448 (inline_p
? 2 : 0));
15449 /* Generate code for it, if necessary. */
15450 expand_or_defer_fn (fn
);
15451 /* Restore the saved values. */
15452 parser
->in_unbraced_linkage_specification_p
15453 = saved_in_unbraced_linkage_specification_p
;
15454 parser
->num_template_parameter_lists
15455 = saved_num_template_parameter_lists
;
15460 /* Parse a template-declaration, assuming that the `export' (and
15461 `extern') keywords, if present, has already been scanned. MEMBER_P
15462 is as for cp_parser_template_declaration. */
15465 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
15467 tree decl
= NULL_TREE
;
15469 tree parameter_list
;
15470 bool friend_p
= false;
15471 bool need_lang_pop
;
15473 /* Look for the `template' keyword. */
15474 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
15478 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
15482 A template ... shall not have C linkage. */
15483 if (current_lang_name
== lang_name_c
)
15485 error ("template with C linkage");
15486 /* Give it C++ linkage to avoid confusing other parts of the
15488 push_lang_context (lang_name_cplusplus
);
15489 need_lang_pop
= true;
15492 need_lang_pop
= false;
15494 /* We cannot perform access checks on the template parameter
15495 declarations until we know what is being declared, just as we
15496 cannot check the decl-specifier list. */
15497 push_deferring_access_checks (dk_deferred
);
15499 /* If the next token is `>', then we have an invalid
15500 specialization. Rather than complain about an invalid template
15501 parameter, issue an error message here. */
15502 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15504 cp_parser_error (parser
, "invalid explicit specialization");
15505 begin_specialization ();
15506 parameter_list
= NULL_TREE
;
15509 /* Parse the template parameters. */
15510 parameter_list
= cp_parser_template_parameter_list (parser
);
15512 /* Get the deferred access checks from the parameter list. These
15513 will be checked once we know what is being declared, as for a
15514 member template the checks must be performed in the scope of the
15515 class containing the member. */
15516 checks
= get_deferred_access_checks ();
15518 /* Look for the `>'. */
15519 cp_parser_skip_until_found (parser
, CPP_GREATER
, "`>'");
15520 /* We just processed one more parameter list. */
15521 ++parser
->num_template_parameter_lists
;
15522 /* If the next token is `template', there are more template
15524 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
15526 cp_parser_template_declaration_after_export (parser
, member_p
);
15529 /* There are no access checks when parsing a template, as we do not
15530 know if a specialization will be a friend. */
15531 push_deferring_access_checks (dk_no_check
);
15532 decl
= cp_parser_single_declaration (parser
,
15536 pop_deferring_access_checks ();
15538 /* If this is a member template declaration, let the front
15540 if (member_p
&& !friend_p
&& decl
)
15542 if (TREE_CODE (decl
) == TYPE_DECL
)
15543 cp_parser_check_access_in_redeclaration (decl
);
15545 decl
= finish_member_template_decl (decl
);
15547 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
15548 make_friend_class (current_class_type
, TREE_TYPE (decl
),
15549 /*complain=*/true);
15551 /* We are done with the current parameter list. */
15552 --parser
->num_template_parameter_lists
;
15554 pop_deferring_access_checks ();
15557 finish_template_decl (parameter_list
);
15559 /* Register member declarations. */
15560 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
15561 finish_member_declaration (decl
);
15562 /* For the erroneous case of a template with C linkage, we pushed an
15563 implicit C++ linkage scope; exit that scope now. */
15565 pop_lang_context ();
15566 /* If DECL is a function template, we must return to parse it later.
15567 (Even though there is no definition, there might be default
15568 arguments that need handling.) */
15569 if (member_p
&& decl
15570 && (TREE_CODE (decl
) == FUNCTION_DECL
15571 || DECL_FUNCTION_TEMPLATE_P (decl
)))
15572 TREE_VALUE (parser
->unparsed_functions_queues
)
15573 = tree_cons (NULL_TREE
, decl
,
15574 TREE_VALUE (parser
->unparsed_functions_queues
));
15577 /* Perform the deferred access checks from a template-parameter-list.
15578 CHECKS is a TREE_LIST of access checks, as returned by
15579 get_deferred_access_checks. */
15582 cp_parser_perform_template_parameter_access_checks (tree checks
)
15584 ++processing_template_parmlist
;
15585 perform_access_checks (checks
);
15586 --processing_template_parmlist
;
15589 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15590 `function-definition' sequence. MEMBER_P is true, this declaration
15591 appears in a class scope.
15593 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15594 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15597 cp_parser_single_declaration (cp_parser
* parser
,
15602 int declares_class_or_enum
;
15603 tree decl
= NULL_TREE
;
15604 cp_decl_specifier_seq decl_specifiers
;
15605 bool function_definition_p
= false;
15607 /* This function is only used when processing a template
15609 gcc_assert (innermost_scope_kind () == sk_template_parms
15610 || innermost_scope_kind () == sk_template_spec
);
15612 /* Defer access checks until we know what is being declared. */
15613 push_deferring_access_checks (dk_deferred
);
15615 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15617 cp_parser_decl_specifier_seq (parser
,
15618 CP_PARSER_FLAGS_OPTIONAL
,
15620 &declares_class_or_enum
);
15622 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
15624 /* There are no template typedefs. */
15625 if (decl_specifiers
.specs
[(int) ds_typedef
])
15627 error ("template declaration of %qs", "typedef");
15628 decl
= error_mark_node
;
15631 /* Gather up the access checks that occurred the
15632 decl-specifier-seq. */
15633 stop_deferring_access_checks ();
15635 /* Check for the declaration of a template class. */
15636 if (declares_class_or_enum
)
15638 if (cp_parser_declares_only_class_p (parser
))
15640 decl
= shadow_tag (&decl_specifiers
);
15645 friend template <typename T> struct A<T>::B;
15648 A<T>::B will be represented by a TYPENAME_TYPE, and
15649 therefore not recognized by shadow_tag. */
15650 if (friend_p
&& *friend_p
15652 && decl_specifiers
.type
15653 && TYPE_P (decl_specifiers
.type
))
15654 decl
= decl_specifiers
.type
;
15656 if (decl
&& decl
!= error_mark_node
)
15657 decl
= TYPE_NAME (decl
);
15659 decl
= error_mark_node
;
15661 /* Perform access checks for template parameters. */
15662 cp_parser_perform_template_parameter_access_checks (checks
);
15665 /* If it's not a template class, try for a template function. If
15666 the next token is a `;', then this declaration does not declare
15667 anything. But, if there were errors in the decl-specifiers, then
15668 the error might well have come from an attempted class-specifier.
15669 In that case, there's no need to warn about a missing declarator. */
15671 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
15672 || decl_specifiers
.type
!= error_mark_node
))
15673 decl
= cp_parser_init_declarator (parser
,
15676 /*function_definition_allowed_p=*/true,
15678 declares_class_or_enum
,
15679 &function_definition_p
);
15681 pop_deferring_access_checks ();
15683 /* Clear any current qualification; whatever comes next is the start
15684 of something new. */
15685 parser
->scope
= NULL_TREE
;
15686 parser
->qualifying_scope
= NULL_TREE
;
15687 parser
->object_scope
= NULL_TREE
;
15688 /* Look for a trailing `;' after the declaration. */
15689 if (!function_definition_p
15690 && (decl
== error_mark_node
15691 || !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'")))
15692 cp_parser_skip_to_end_of_block_or_statement (parser
);
15697 /* Parse a cast-expression that is not the operand of a unary "&". */
15700 cp_parser_simple_cast_expression (cp_parser
*parser
)
15702 return cp_parser_cast_expression (parser
, /*address_p=*/false,
15706 /* Parse a functional cast to TYPE. Returns an expression
15707 representing the cast. */
15710 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
15712 tree expression_list
;
15716 = cp_parser_parenthesized_expression_list (parser
, false,
15718 /*non_constant_p=*/NULL
);
15720 cast
= build_functional_cast (type
, expression_list
);
15721 /* [expr.const]/1: In an integral constant expression "only type
15722 conversions to integral or enumeration type can be used". */
15723 if (TREE_CODE (type
) == TYPE_DECL
)
15724 type
= TREE_TYPE (type
);
15725 if (cast
!= error_mark_node
&& !dependent_type_p (type
)
15726 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
15728 if (cp_parser_non_integral_constant_expression
15729 (parser
, "a call to a constructor"))
15730 return error_mark_node
;
15735 /* Save the tokens that make up the body of a member function defined
15736 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15737 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15738 specifiers applied to the declaration. Returns the FUNCTION_DECL
15739 for the member function. */
15742 cp_parser_save_member_function_body (cp_parser
* parser
,
15743 cp_decl_specifier_seq
*decl_specifiers
,
15744 cp_declarator
*declarator
,
15751 /* Create the function-declaration. */
15752 fn
= start_method (decl_specifiers
, declarator
, attributes
);
15753 /* If something went badly wrong, bail out now. */
15754 if (fn
== error_mark_node
)
15756 /* If there's a function-body, skip it. */
15757 if (cp_parser_token_starts_function_definition_p
15758 (cp_lexer_peek_token (parser
->lexer
)))
15759 cp_parser_skip_to_end_of_block_or_statement (parser
);
15760 return error_mark_node
;
15763 /* Remember it, if there default args to post process. */
15764 cp_parser_save_default_args (parser
, fn
);
15766 /* Save away the tokens that make up the body of the
15768 first
= parser
->lexer
->next_token
;
15769 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15770 /* Handle function try blocks. */
15771 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
15772 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15773 last
= parser
->lexer
->next_token
;
15775 /* Save away the inline definition; we will process it when the
15776 class is complete. */
15777 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
15778 DECL_PENDING_INLINE_P (fn
) = 1;
15780 /* We need to know that this was defined in the class, so that
15781 friend templates are handled correctly. */
15782 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
15784 /* We're done with the inline definition. */
15785 finish_method (fn
);
15787 /* Add FN to the queue of functions to be parsed later. */
15788 TREE_VALUE (parser
->unparsed_functions_queues
)
15789 = tree_cons (NULL_TREE
, fn
,
15790 TREE_VALUE (parser
->unparsed_functions_queues
));
15795 /* Parse a template-argument-list, as well as the trailing ">" (but
15796 not the opening ">"). See cp_parser_template_argument_list for the
15800 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
15804 tree saved_qualifying_scope
;
15805 tree saved_object_scope
;
15806 bool saved_greater_than_is_operator_p
;
15807 bool saved_skip_evaluation
;
15811 When parsing a template-id, the first non-nested `>' is taken as
15812 the end of the template-argument-list rather than a greater-than
15814 saved_greater_than_is_operator_p
15815 = parser
->greater_than_is_operator_p
;
15816 parser
->greater_than_is_operator_p
= false;
15817 /* Parsing the argument list may modify SCOPE, so we save it
15819 saved_scope
= parser
->scope
;
15820 saved_qualifying_scope
= parser
->qualifying_scope
;
15821 saved_object_scope
= parser
->object_scope
;
15822 /* We need to evaluate the template arguments, even though this
15823 template-id may be nested within a "sizeof". */
15824 saved_skip_evaluation
= skip_evaluation
;
15825 skip_evaluation
= false;
15826 /* Parse the template-argument-list itself. */
15827 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15828 arguments
= NULL_TREE
;
15830 arguments
= cp_parser_template_argument_list (parser
);
15831 /* Look for the `>' that ends the template-argument-list. If we find
15832 a '>>' instead, it's probably just a typo. */
15833 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
15835 if (!saved_greater_than_is_operator_p
)
15837 /* If we're in a nested template argument list, the '>>' has
15838 to be a typo for '> >'. We emit the error message, but we
15839 continue parsing and we push a '>' as next token, so that
15840 the argument list will be parsed correctly. Note that the
15841 global source location is still on the token before the
15842 '>>', so we need to say explicitly where we want it. */
15843 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15844 error ("%H%<>>%> should be %<> >%> "
15845 "within a nested template argument list",
15848 /* ??? Proper recovery should terminate two levels of
15849 template argument list here. */
15850 token
->type
= CPP_GREATER
;
15854 /* If this is not a nested template argument list, the '>>'
15855 is a typo for '>'. Emit an error message and continue.
15856 Same deal about the token location, but here we can get it
15857 right by consuming the '>>' before issuing the diagnostic. */
15858 cp_lexer_consume_token (parser
->lexer
);
15859 error ("spurious %<>>%>, use %<>%> to terminate "
15860 "a template argument list");
15864 cp_parser_skip_until_found (parser
, CPP_GREATER
, "`>'");
15865 /* The `>' token might be a greater-than operator again now. */
15866 parser
->greater_than_is_operator_p
15867 = saved_greater_than_is_operator_p
;
15868 /* Restore the SAVED_SCOPE. */
15869 parser
->scope
= saved_scope
;
15870 parser
->qualifying_scope
= saved_qualifying_scope
;
15871 parser
->object_scope
= saved_object_scope
;
15872 skip_evaluation
= saved_skip_evaluation
;
15877 /* MEMBER_FUNCTION is a member function, or a friend. If default
15878 arguments, or the body of the function have not yet been parsed,
15882 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
15884 /* If this member is a template, get the underlying
15886 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
15887 member_function
= DECL_TEMPLATE_RESULT (member_function
);
15889 /* There should not be any class definitions in progress at this
15890 point; the bodies of members are only parsed outside of all class
15892 gcc_assert (parser
->num_classes_being_defined
== 0);
15893 /* While we're parsing the member functions we might encounter more
15894 classes. We want to handle them right away, but we don't want
15895 them getting mixed up with functions that are currently in the
15897 parser
->unparsed_functions_queues
15898 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
15900 /* Make sure that any template parameters are in scope. */
15901 maybe_begin_member_template_processing (member_function
);
15903 /* If the body of the function has not yet been parsed, parse it
15905 if (DECL_PENDING_INLINE_P (member_function
))
15907 tree function_scope
;
15908 cp_token_cache
*tokens
;
15910 /* The function is no longer pending; we are processing it. */
15911 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
15912 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
15913 DECL_PENDING_INLINE_P (member_function
) = 0;
15915 /* If this is a local class, enter the scope of the containing
15917 function_scope
= current_function_decl
;
15918 if (function_scope
)
15919 push_function_context_to (function_scope
);
15922 /* Push the body of the function onto the lexer stack. */
15923 cp_parser_push_lexer_for_tokens (parser
, tokens
);
15925 /* Let the front end know that we going to be defining this
15927 start_preparsed_function (member_function
, NULL_TREE
,
15928 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
15930 /* Don't do access checking if it is a templated function. */
15931 if (processing_template_decl
)
15932 push_deferring_access_checks (dk_no_check
);
15934 /* Now, parse the body of the function. */
15935 cp_parser_function_definition_after_declarator (parser
,
15936 /*inline_p=*/true);
15938 if (processing_template_decl
)
15939 pop_deferring_access_checks ();
15941 /* Leave the scope of the containing function. */
15942 if (function_scope
)
15943 pop_function_context_from (function_scope
);
15944 cp_parser_pop_lexer (parser
);
15947 /* Remove any template parameters from the symbol table. */
15948 maybe_end_member_template_processing ();
15950 /* Restore the queue. */
15951 parser
->unparsed_functions_queues
15952 = TREE_CHAIN (parser
->unparsed_functions_queues
);
15955 /* If DECL contains any default args, remember it on the unparsed
15956 functions queue. */
15959 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
15963 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
15965 probe
= TREE_CHAIN (probe
))
15966 if (TREE_PURPOSE (probe
))
15968 TREE_PURPOSE (parser
->unparsed_functions_queues
)
15969 = tree_cons (current_class_type
, decl
,
15970 TREE_PURPOSE (parser
->unparsed_functions_queues
));
15975 /* FN is a FUNCTION_DECL which may contains a parameter with an
15976 unparsed DEFAULT_ARG. Parse the default args now. This function
15977 assumes that the current scope is the scope in which the default
15978 argument should be processed. */
15981 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
15983 bool saved_local_variables_forbidden_p
;
15986 /* While we're parsing the default args, we might (due to the
15987 statement expression extension) encounter more classes. We want
15988 to handle them right away, but we don't want them getting mixed
15989 up with default args that are currently in the queue. */
15990 parser
->unparsed_functions_queues
15991 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
15993 /* Local variable names (and the `this' keyword) may not appear
15994 in a default argument. */
15995 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
15996 parser
->local_variables_forbidden_p
= true;
15998 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
16000 parm
= TREE_CHAIN (parm
))
16002 cp_token_cache
*tokens
;
16003 tree default_arg
= TREE_PURPOSE (parm
);
16005 VEC(tree
,gc
) *insts
;
16012 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
16013 /* This can happen for a friend declaration for a function
16014 already declared with default arguments. */
16017 /* Push the saved tokens for the default argument onto the parser's
16019 tokens
= DEFARG_TOKENS (default_arg
);
16020 cp_parser_push_lexer_for_tokens (parser
, tokens
);
16022 /* Parse the assignment-expression. */
16023 parsed_arg
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
16025 if (!processing_template_decl
)
16026 parsed_arg
= check_default_argument (TREE_VALUE (parm
), parsed_arg
);
16028 TREE_PURPOSE (parm
) = parsed_arg
;
16030 /* Update any instantiations we've already created. */
16031 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
16032 VEC_iterate (tree
, insts
, ix
, copy
); ix
++)
16033 TREE_PURPOSE (copy
) = parsed_arg
;
16035 /* If the token stream has not been completely used up, then
16036 there was extra junk after the end of the default
16038 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
16039 cp_parser_error (parser
, "expected %<,%>");
16041 /* Revert to the main lexer. */
16042 cp_parser_pop_lexer (parser
);
16045 /* Make sure no default arg is missing. */
16046 check_default_args (fn
);
16048 /* Restore the state of local_variables_forbidden_p. */
16049 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
16051 /* Restore the queue. */
16052 parser
->unparsed_functions_queues
16053 = TREE_CHAIN (parser
->unparsed_functions_queues
);
16056 /* Parse the operand of `sizeof' (or a similar operator). Returns
16057 either a TYPE or an expression, depending on the form of the
16058 input. The KEYWORD indicates which kind of expression we have
16062 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
16064 static const char *format
;
16065 tree expr
= NULL_TREE
;
16066 const char *saved_message
;
16067 bool saved_integral_constant_expression_p
;
16068 bool saved_non_integral_constant_expression_p
;
16070 /* Initialize FORMAT the first time we get here. */
16072 format
= "types may not be defined in '%s' expressions";
16074 /* Types cannot be defined in a `sizeof' expression. Save away the
16076 saved_message
= parser
->type_definition_forbidden_message
;
16077 /* And create the new one. */
16078 parser
->type_definition_forbidden_message
16079 = XNEWVEC (const char, strlen (format
)
16080 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
16082 sprintf ((char *) parser
->type_definition_forbidden_message
,
16083 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
16085 /* The restrictions on constant-expressions do not apply inside
16086 sizeof expressions. */
16087 saved_integral_constant_expression_p
16088 = parser
->integral_constant_expression_p
;
16089 saved_non_integral_constant_expression_p
16090 = parser
->non_integral_constant_expression_p
;
16091 parser
->integral_constant_expression_p
= false;
16093 /* Do not actually evaluate the expression. */
16095 /* If it's a `(', then we might be looking at the type-id
16097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16100 bool saved_in_type_id_in_expr_p
;
16102 /* We can't be sure yet whether we're looking at a type-id or an
16104 cp_parser_parse_tentatively (parser
);
16105 /* Consume the `('. */
16106 cp_lexer_consume_token (parser
->lexer
);
16107 /* Parse the type-id. */
16108 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
16109 parser
->in_type_id_in_expr_p
= true;
16110 type
= cp_parser_type_id (parser
);
16111 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
16112 /* Now, look for the trailing `)'. */
16113 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
16114 /* If all went well, then we're done. */
16115 if (cp_parser_parse_definitely (parser
))
16117 cp_decl_specifier_seq decl_specs
;
16119 /* Build a trivial decl-specifier-seq. */
16120 clear_decl_specs (&decl_specs
);
16121 decl_specs
.type
= type
;
16123 /* Call grokdeclarator to figure out what type this is. */
16124 expr
= grokdeclarator (NULL
,
16128 /*attrlist=*/NULL
);
16132 /* If the type-id production did not work out, then we must be
16133 looking at the unary-expression production. */
16135 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
16137 /* Go back to evaluating expressions. */
16140 /* Free the message we created. */
16141 free ((char *) parser
->type_definition_forbidden_message
);
16142 /* And restore the old one. */
16143 parser
->type_definition_forbidden_message
= saved_message
;
16144 parser
->integral_constant_expression_p
16145 = saved_integral_constant_expression_p
;
16146 parser
->non_integral_constant_expression_p
16147 = saved_non_integral_constant_expression_p
;
16152 /* If the current declaration has no declarator, return true. */
16155 cp_parser_declares_only_class_p (cp_parser
*parser
)
16157 /* If the next token is a `;' or a `,' then there is no
16159 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
16160 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16163 /* Update the DECL_SPECS to reflect the storage class indicated by
16167 cp_parser_set_storage_class (cp_parser
*parser
,
16168 cp_decl_specifier_seq
*decl_specs
,
16171 cp_storage_class storage_class
;
16173 if (parser
->in_unbraced_linkage_specification_p
)
16175 error ("invalid use of %qD in linkage specification",
16176 ridpointers
[keyword
]);
16179 else if (decl_specs
->storage_class
!= sc_none
)
16181 decl_specs
->multiple_storage_classes_p
= true;
16185 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
16186 && decl_specs
->specs
[(int) ds_thread
])
16188 error ("%<__thread%> before %qD", ridpointers
[keyword
]);
16189 decl_specs
->specs
[(int) ds_thread
] = 0;
16195 storage_class
= sc_auto
;
16198 storage_class
= sc_register
;
16201 storage_class
= sc_static
;
16204 storage_class
= sc_extern
;
16207 storage_class
= sc_mutable
;
16210 gcc_unreachable ();
16212 decl_specs
->storage_class
= storage_class
;
16215 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16216 is true, the type is a user-defined type; otherwise it is a
16217 built-in type specified by a keyword. */
16220 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
16222 bool user_defined_p
)
16224 decl_specs
->any_specifiers_p
= true;
16226 /* If the user tries to redeclare bool or wchar_t (with, for
16227 example, in "typedef int wchar_t;") we remember that this is what
16228 happened. In system headers, we ignore these declarations so
16229 that G++ can work with system headers that are not C++-safe. */
16230 if (decl_specs
->specs
[(int) ds_typedef
]
16232 && (type_spec
== boolean_type_node
16233 || type_spec
== wchar_type_node
)
16234 && (decl_specs
->type
16235 || decl_specs
->specs
[(int) ds_long
]
16236 || decl_specs
->specs
[(int) ds_short
]
16237 || decl_specs
->specs
[(int) ds_unsigned
]
16238 || decl_specs
->specs
[(int) ds_signed
]))
16240 decl_specs
->redefined_builtin_type
= type_spec
;
16241 if (!decl_specs
->type
)
16243 decl_specs
->type
= type_spec
;
16244 decl_specs
->user_defined_type_p
= false;
16247 else if (decl_specs
->type
)
16248 decl_specs
->multiple_types_p
= true;
16251 decl_specs
->type
= type_spec
;
16252 decl_specs
->user_defined_type_p
= user_defined_p
;
16253 decl_specs
->redefined_builtin_type
= NULL_TREE
;
16257 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16258 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16261 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
16263 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
16266 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16267 issue an error message indicating that TOKEN_DESC was expected.
16269 Returns the token consumed, if the token had the appropriate type.
16270 Otherwise, returns NULL. */
16273 cp_parser_require (cp_parser
* parser
,
16274 enum cpp_ttype type
,
16275 const char* token_desc
)
16277 if (cp_lexer_next_token_is (parser
->lexer
, type
))
16278 return cp_lexer_consume_token (parser
->lexer
);
16281 /* Output the MESSAGE -- unless we're parsing tentatively. */
16282 if (!cp_parser_simulate_error (parser
))
16284 char *message
= concat ("expected ", token_desc
, NULL
);
16285 cp_parser_error (parser
, message
);
16292 /* Like cp_parser_require, except that tokens will be skipped until
16293 the desired token is found. An error message is still produced if
16294 the next token is not as expected. */
16297 cp_parser_skip_until_found (cp_parser
* parser
,
16298 enum cpp_ttype type
,
16299 const char* token_desc
)
16302 unsigned nesting_depth
= 0;
16304 if (cp_parser_require (parser
, type
, token_desc
))
16307 /* Skip tokens until the desired token is found. */
16310 /* Peek at the next token. */
16311 token
= cp_lexer_peek_token (parser
->lexer
);
16313 /* If we've reached the token we want, consume it and stop. */
16314 if (token
->type
== type
&& !nesting_depth
)
16316 cp_lexer_consume_token (parser
->lexer
);
16320 switch (token
->type
)
16323 case CPP_PRAGMA_EOL
:
16324 /* If we've run out of tokens, stop. */
16327 case CPP_OPEN_BRACE
:
16328 case CPP_OPEN_PAREN
:
16329 case CPP_OPEN_SQUARE
:
16333 case CPP_CLOSE_BRACE
:
16334 case CPP_CLOSE_PAREN
:
16335 case CPP_CLOSE_SQUARE
:
16336 if (nesting_depth
-- == 0)
16344 /* Consume this token. */
16345 cp_lexer_consume_token (parser
->lexer
);
16349 /* If the next token is the indicated keyword, consume it. Otherwise,
16350 issue an error message indicating that TOKEN_DESC was expected.
16352 Returns the token consumed, if the token had the appropriate type.
16353 Otherwise, returns NULL. */
16356 cp_parser_require_keyword (cp_parser
* parser
,
16358 const char* token_desc
)
16360 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
16362 if (token
&& token
->keyword
!= keyword
)
16364 dyn_string_t error_msg
;
16366 /* Format the error message. */
16367 error_msg
= dyn_string_new (0);
16368 dyn_string_append_cstr (error_msg
, "expected ");
16369 dyn_string_append_cstr (error_msg
, token_desc
);
16370 cp_parser_error (parser
, error_msg
->s
);
16371 dyn_string_delete (error_msg
);
16378 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16379 function-definition. */
16382 cp_parser_token_starts_function_definition_p (cp_token
* token
)
16384 return (/* An ordinary function-body begins with an `{'. */
16385 token
->type
== CPP_OPEN_BRACE
16386 /* A ctor-initializer begins with a `:'. */
16387 || token
->type
== CPP_COLON
16388 /* A function-try-block begins with `try'. */
16389 || token
->keyword
== RID_TRY
16390 /* The named return value extension begins with `return'. */
16391 || token
->keyword
== RID_RETURN
);
16394 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16398 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
16402 token
= cp_lexer_peek_token (parser
->lexer
);
16403 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
16406 /* Returns TRUE iff the next token is the "," or ">" ending a
16407 template-argument. */
16410 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
16414 token
= cp_lexer_peek_token (parser
->lexer
);
16415 return (token
->type
== CPP_COMMA
|| token
->type
== CPP_GREATER
);
16418 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16419 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16422 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
16427 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
16428 if (token
->type
== CPP_LESS
)
16430 /* Check for the sequence `<::' in the original code. It would be lexed as
16431 `[:', where `[' is a digraph, and there is no whitespace before
16433 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
16436 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
16437 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
16443 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16444 or none_type otherwise. */
16446 static enum tag_types
16447 cp_parser_token_is_class_key (cp_token
* token
)
16449 switch (token
->keyword
)
16454 return record_type
;
16463 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16466 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
16468 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
16469 pedwarn ("%qs tag used in naming %q#T",
16470 class_key
== union_type
? "union"
16471 : class_key
== record_type
? "struct" : "class",
16475 /* Issue an error message if DECL is redeclared with different
16476 access than its original declaration [class.access.spec/3].
16477 This applies to nested classes and nested class templates.
16481 cp_parser_check_access_in_redeclaration (tree decl
)
16483 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
16486 if ((TREE_PRIVATE (decl
)
16487 != (current_access_specifier
== access_private_node
))
16488 || (TREE_PROTECTED (decl
)
16489 != (current_access_specifier
== access_protected_node
)))
16490 error ("%qD redeclared with different access", decl
);
16493 /* Look for the `template' keyword, as a syntactic disambiguator.
16494 Return TRUE iff it is present, in which case it will be
16498 cp_parser_optional_template_keyword (cp_parser
*parser
)
16500 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16502 /* The `template' keyword can only be used within templates;
16503 outside templates the parser can always figure out what is a
16504 template and what is not. */
16505 if (!processing_template_decl
)
16507 error ("%<template%> (as a disambiguator) is only allowed "
16508 "within templates");
16509 /* If this part of the token stream is rescanned, the same
16510 error message would be generated. So, we purge the token
16511 from the stream. */
16512 cp_lexer_purge_token (parser
->lexer
);
16517 /* Consume the `template' keyword. */
16518 cp_lexer_consume_token (parser
->lexer
);
16526 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16527 set PARSER->SCOPE, and perform other related actions. */
16530 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
16535 /* Get the stored value. */
16536 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
16537 /* Perform any access checks that were deferred. */
16538 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
16539 perform_or_defer_access_check (TREE_PURPOSE (check
), TREE_VALUE (check
));
16540 /* Set the scope from the stored value. */
16541 parser
->scope
= TREE_VALUE (value
);
16542 parser
->qualifying_scope
= TREE_TYPE (value
);
16543 parser
->object_scope
= NULL_TREE
;
16546 /* Consume tokens up through a non-nested END token. */
16549 cp_parser_cache_group (cp_parser
*parser
,
16550 enum cpp_ttype end
,
16557 /* Abort a parenthesized expression if we encounter a brace. */
16558 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
16559 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
16561 /* If we've reached the end of the file, stop. */
16562 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
)
16563 || (end
!= CPP_PRAGMA_EOL
16564 && cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
)))
16566 /* Consume the next token. */
16567 token
= cp_lexer_consume_token (parser
->lexer
);
16568 /* See if it starts a new group. */
16569 if (token
->type
== CPP_OPEN_BRACE
)
16571 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
16575 else if (token
->type
== CPP_OPEN_PAREN
)
16576 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
16577 else if (token
->type
== CPP_PRAGMA
)
16578 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
16579 else if (token
->type
== end
)
16584 /* Begin parsing tentatively. We always save tokens while parsing
16585 tentatively so that if the tentative parsing fails we can restore the
16589 cp_parser_parse_tentatively (cp_parser
* parser
)
16591 /* Enter a new parsing context. */
16592 parser
->context
= cp_parser_context_new (parser
->context
);
16593 /* Begin saving tokens. */
16594 cp_lexer_save_tokens (parser
->lexer
);
16595 /* In order to avoid repetitive access control error messages,
16596 access checks are queued up until we are no longer parsing
16598 push_deferring_access_checks (dk_deferred
);
16601 /* Commit to the currently active tentative parse. */
16604 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
16606 cp_parser_context
*context
;
16609 /* Mark all of the levels as committed. */
16610 lexer
= parser
->lexer
;
16611 for (context
= parser
->context
; context
->next
; context
= context
->next
)
16613 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
16615 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
16616 while (!cp_lexer_saving_tokens (lexer
))
16617 lexer
= lexer
->next
;
16618 cp_lexer_commit_tokens (lexer
);
16622 /* Abort the currently active tentative parse. All consumed tokens
16623 will be rolled back, and no diagnostics will be issued. */
16626 cp_parser_abort_tentative_parse (cp_parser
* parser
)
16628 cp_parser_simulate_error (parser
);
16629 /* Now, pretend that we want to see if the construct was
16630 successfully parsed. */
16631 cp_parser_parse_definitely (parser
);
16634 /* Stop parsing tentatively. If a parse error has occurred, restore the
16635 token stream. Otherwise, commit to the tokens we have consumed.
16636 Returns true if no error occurred; false otherwise. */
16639 cp_parser_parse_definitely (cp_parser
* parser
)
16641 bool error_occurred
;
16642 cp_parser_context
*context
;
16644 /* Remember whether or not an error occurred, since we are about to
16645 destroy that information. */
16646 error_occurred
= cp_parser_error_occurred (parser
);
16647 /* Remove the topmost context from the stack. */
16648 context
= parser
->context
;
16649 parser
->context
= context
->next
;
16650 /* If no parse errors occurred, commit to the tentative parse. */
16651 if (!error_occurred
)
16653 /* Commit to the tokens read tentatively, unless that was
16655 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
16656 cp_lexer_commit_tokens (parser
->lexer
);
16658 pop_to_parent_deferring_access_checks ();
16660 /* Otherwise, if errors occurred, roll back our state so that things
16661 are just as they were before we began the tentative parse. */
16664 cp_lexer_rollback_tokens (parser
->lexer
);
16665 pop_deferring_access_checks ();
16667 /* Add the context to the front of the free list. */
16668 context
->next
= cp_parser_context_free_list
;
16669 cp_parser_context_free_list
= context
;
16671 return !error_occurred
;
16674 /* Returns true if we are parsing tentatively and are not committed to
16675 this tentative parse. */
16678 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
16680 return (cp_parser_parsing_tentatively (parser
)
16681 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
16684 /* Returns nonzero iff an error has occurred during the most recent
16685 tentative parse. */
16688 cp_parser_error_occurred (cp_parser
* parser
)
16690 return (cp_parser_parsing_tentatively (parser
)
16691 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
16694 /* Returns nonzero if GNU extensions are allowed. */
16697 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
16699 return parser
->allow_gnu_extensions_p
;
16702 /* Objective-C++ Productions */
16705 /* Parse an Objective-C expression, which feeds into a primary-expression
16709 objc-message-expression
16710 objc-string-literal
16711 objc-encode-expression
16712 objc-protocol-expression
16713 objc-selector-expression
16715 Returns a tree representation of the expression. */
16718 cp_parser_objc_expression (cp_parser
* parser
)
16720 /* Try to figure out what kind of declaration is present. */
16721 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
16725 case CPP_OPEN_SQUARE
:
16726 return cp_parser_objc_message_expression (parser
);
16728 case CPP_OBJC_STRING
:
16729 kwd
= cp_lexer_consume_token (parser
->lexer
);
16730 return objc_build_string_object (kwd
->value
);
16733 switch (kwd
->keyword
)
16735 case RID_AT_ENCODE
:
16736 return cp_parser_objc_encode_expression (parser
);
16738 case RID_AT_PROTOCOL
:
16739 return cp_parser_objc_protocol_expression (parser
);
16741 case RID_AT_SELECTOR
:
16742 return cp_parser_objc_selector_expression (parser
);
16748 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
16749 cp_parser_skip_to_end_of_block_or_statement (parser
);
16752 return error_mark_node
;
16755 /* Parse an Objective-C message expression.
16757 objc-message-expression:
16758 [ objc-message-receiver objc-message-args ]
16760 Returns a representation of an Objective-C message. */
16763 cp_parser_objc_message_expression (cp_parser
* parser
)
16765 tree receiver
, messageargs
;
16767 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
16768 receiver
= cp_parser_objc_message_receiver (parser
);
16769 messageargs
= cp_parser_objc_message_args (parser
);
16770 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
16772 return objc_build_message_expr (build_tree_list (receiver
, messageargs
));
16775 /* Parse an objc-message-receiver.
16777 objc-message-receiver:
16779 simple-type-specifier
16781 Returns a representation of the type or expression. */
16784 cp_parser_objc_message_receiver (cp_parser
* parser
)
16788 /* An Objective-C message receiver may be either (1) a type
16789 or (2) an expression. */
16790 cp_parser_parse_tentatively (parser
);
16791 rcv
= cp_parser_expression (parser
, false);
16793 if (cp_parser_parse_definitely (parser
))
16796 rcv
= cp_parser_simple_type_specifier (parser
,
16797 /*decl_specs=*/NULL
,
16798 CP_PARSER_FLAGS_NONE
);
16800 return objc_get_class_reference (rcv
);
16803 /* Parse the arguments and selectors comprising an Objective-C message.
16808 objc-selector-args , objc-comma-args
16810 objc-selector-args:
16811 objc-selector [opt] : assignment-expression
16812 objc-selector-args objc-selector [opt] : assignment-expression
16815 assignment-expression
16816 objc-comma-args , assignment-expression
16818 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16819 selector arguments and TREE_VALUE containing a list of comma
16823 cp_parser_objc_message_args (cp_parser
* parser
)
16825 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
16826 bool maybe_unary_selector_p
= true;
16827 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16829 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
16831 tree selector
= NULL_TREE
, arg
;
16833 if (token
->type
!= CPP_COLON
)
16834 selector
= cp_parser_objc_selector (parser
);
16836 /* Detect if we have a unary selector. */
16837 if (maybe_unary_selector_p
16838 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
16839 return build_tree_list (selector
, NULL_TREE
);
16841 maybe_unary_selector_p
= false;
16842 cp_parser_require (parser
, CPP_COLON
, "`:'");
16843 arg
= cp_parser_assignment_expression (parser
, false);
16846 = chainon (sel_args
,
16847 build_tree_list (selector
, arg
));
16849 token
= cp_lexer_peek_token (parser
->lexer
);
16852 /* Handle non-selector arguments, if any. */
16853 while (token
->type
== CPP_COMMA
)
16857 cp_lexer_consume_token (parser
->lexer
);
16858 arg
= cp_parser_assignment_expression (parser
, false);
16861 = chainon (addl_args
,
16862 build_tree_list (NULL_TREE
, arg
));
16864 token
= cp_lexer_peek_token (parser
->lexer
);
16867 return build_tree_list (sel_args
, addl_args
);
16870 /* Parse an Objective-C encode expression.
16872 objc-encode-expression:
16873 @encode objc-typename
16875 Returns an encoded representation of the type argument. */
16878 cp_parser_objc_encode_expression (cp_parser
* parser
)
16882 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
16883 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16884 type
= complete_type (cp_parser_type_id (parser
));
16885 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
16889 error ("%<@encode%> must specify a type as an argument");
16890 return error_mark_node
;
16893 return objc_build_encode_expr (type
);
16896 /* Parse an Objective-C @defs expression. */
16899 cp_parser_objc_defs_expression (cp_parser
*parser
)
16903 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
16904 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16905 name
= cp_parser_identifier (parser
);
16906 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
16908 return objc_get_class_ivars (name
);
16911 /* Parse an Objective-C protocol expression.
16913 objc-protocol-expression:
16914 @protocol ( identifier )
16916 Returns a representation of the protocol expression. */
16919 cp_parser_objc_protocol_expression (cp_parser
* parser
)
16923 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
16924 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16925 proto
= cp_parser_identifier (parser
);
16926 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
16928 return objc_build_protocol_expr (proto
);
16931 /* Parse an Objective-C selector expression.
16933 objc-selector-expression:
16934 @selector ( objc-method-signature )
16936 objc-method-signature:
16942 objc-selector-seq objc-selector :
16944 Returns a representation of the method selector. */
16947 cp_parser_objc_selector_expression (cp_parser
* parser
)
16949 tree sel_seq
= NULL_TREE
;
16950 bool maybe_unary_selector_p
= true;
16953 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
16954 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16955 token
= cp_lexer_peek_token (parser
->lexer
);
16957 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
16958 || token
->type
== CPP_SCOPE
)
16960 tree selector
= NULL_TREE
;
16962 if (token
->type
!= CPP_COLON
16963 || token
->type
== CPP_SCOPE
)
16964 selector
= cp_parser_objc_selector (parser
);
16966 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
16967 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
16969 /* Detect if we have a unary selector. */
16970 if (maybe_unary_selector_p
)
16972 sel_seq
= selector
;
16973 goto finish_selector
;
16977 cp_parser_error (parser
, "expected %<:%>");
16980 maybe_unary_selector_p
= false;
16981 token
= cp_lexer_consume_token (parser
->lexer
);
16983 if (token
->type
== CPP_SCOPE
)
16986 = chainon (sel_seq
,
16987 build_tree_list (selector
, NULL_TREE
));
16989 = chainon (sel_seq
,
16990 build_tree_list (NULL_TREE
, NULL_TREE
));
16994 = chainon (sel_seq
,
16995 build_tree_list (selector
, NULL_TREE
));
16997 token
= cp_lexer_peek_token (parser
->lexer
);
17001 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17003 return objc_build_selector_expr (sel_seq
);
17006 /* Parse a list of identifiers.
17008 objc-identifier-list:
17010 objc-identifier-list , identifier
17012 Returns a TREE_LIST of identifier nodes. */
17015 cp_parser_objc_identifier_list (cp_parser
* parser
)
17017 tree list
= build_tree_list (NULL_TREE
, cp_parser_identifier (parser
));
17018 cp_token
*sep
= cp_lexer_peek_token (parser
->lexer
);
17020 while (sep
->type
== CPP_COMMA
)
17022 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17023 list
= chainon (list
,
17024 build_tree_list (NULL_TREE
,
17025 cp_parser_identifier (parser
)));
17026 sep
= cp_lexer_peek_token (parser
->lexer
);
17032 /* Parse an Objective-C alias declaration.
17034 objc-alias-declaration:
17035 @compatibility_alias identifier identifier ;
17037 This function registers the alias mapping with the Objective-C front-end.
17038 It returns nothing. */
17041 cp_parser_objc_alias_declaration (cp_parser
* parser
)
17045 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
17046 alias
= cp_parser_identifier (parser
);
17047 orig
= cp_parser_identifier (parser
);
17048 objc_declare_alias (alias
, orig
);
17049 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17052 /* Parse an Objective-C class forward-declaration.
17054 objc-class-declaration:
17055 @class objc-identifier-list ;
17057 The function registers the forward declarations with the Objective-C
17058 front-end. It returns nothing. */
17061 cp_parser_objc_class_declaration (cp_parser
* parser
)
17063 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
17064 objc_declare_class (cp_parser_objc_identifier_list (parser
));
17065 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17068 /* Parse a list of Objective-C protocol references.
17070 objc-protocol-refs-opt:
17071 objc-protocol-refs [opt]
17073 objc-protocol-refs:
17074 < objc-identifier-list >
17076 Returns a TREE_LIST of identifiers, if any. */
17079 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
17081 tree protorefs
= NULL_TREE
;
17083 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
17085 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
17086 protorefs
= cp_parser_objc_identifier_list (parser
);
17087 cp_parser_require (parser
, CPP_GREATER
, "`>'");
17093 /* Parse a Objective-C visibility specification. */
17096 cp_parser_objc_visibility_spec (cp_parser
* parser
)
17098 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
17100 switch (vis
->keyword
)
17102 case RID_AT_PRIVATE
:
17103 objc_set_visibility (2);
17105 case RID_AT_PROTECTED
:
17106 objc_set_visibility (0);
17108 case RID_AT_PUBLIC
:
17109 objc_set_visibility (1);
17115 /* Eat '@private'/'@protected'/'@public'. */
17116 cp_lexer_consume_token (parser
->lexer
);
17119 /* Parse an Objective-C method type. */
17122 cp_parser_objc_method_type (cp_parser
* parser
)
17124 objc_set_method_type
17125 (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
17130 /* Parse an Objective-C protocol qualifier. */
17133 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
17135 tree quals
= NULL_TREE
, node
;
17136 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17138 node
= token
->value
;
17140 while (node
&& TREE_CODE (node
) == IDENTIFIER_NODE
17141 && (node
== ridpointers
[(int) RID_IN
]
17142 || node
== ridpointers
[(int) RID_OUT
]
17143 || node
== ridpointers
[(int) RID_INOUT
]
17144 || node
== ridpointers
[(int) RID_BYCOPY
]
17145 || node
== ridpointers
[(int) RID_BYREF
]
17146 || node
== ridpointers
[(int) RID_ONEWAY
]))
17148 quals
= tree_cons (NULL_TREE
, node
, quals
);
17149 cp_lexer_consume_token (parser
->lexer
);
17150 token
= cp_lexer_peek_token (parser
->lexer
);
17151 node
= token
->value
;
17157 /* Parse an Objective-C typename. */
17160 cp_parser_objc_typename (cp_parser
* parser
)
17162 tree typename
= NULL_TREE
;
17164 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17166 tree proto_quals
, cp_type
= NULL_TREE
;
17168 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
17169 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
17171 /* An ObjC type name may consist of just protocol qualifiers, in which
17172 case the type shall default to 'id'. */
17173 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
17174 cp_type
= cp_parser_type_id (parser
);
17176 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17177 typename
= build_tree_list (proto_quals
, cp_type
);
17183 /* Check to see if TYPE refers to an Objective-C selector name. */
17186 cp_parser_objc_selector_p (enum cpp_ttype type
)
17188 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
17189 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
17190 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
17191 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
17192 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
17195 /* Parse an Objective-C selector. */
17198 cp_parser_objc_selector (cp_parser
* parser
)
17200 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
17202 if (!cp_parser_objc_selector_p (token
->type
))
17204 error ("invalid Objective-C++ selector name");
17205 return error_mark_node
;
17208 /* C++ operator names are allowed to appear in ObjC selectors. */
17209 switch (token
->type
)
17211 case CPP_AND_AND
: return get_identifier ("and");
17212 case CPP_AND_EQ
: return get_identifier ("and_eq");
17213 case CPP_AND
: return get_identifier ("bitand");
17214 case CPP_OR
: return get_identifier ("bitor");
17215 case CPP_COMPL
: return get_identifier ("compl");
17216 case CPP_NOT
: return get_identifier ("not");
17217 case CPP_NOT_EQ
: return get_identifier ("not_eq");
17218 case CPP_OR_OR
: return get_identifier ("or");
17219 case CPP_OR_EQ
: return get_identifier ("or_eq");
17220 case CPP_XOR
: return get_identifier ("xor");
17221 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
17222 default: return token
->value
;
17226 /* Parse an Objective-C params list. */
17229 cp_parser_objc_method_keyword_params (cp_parser
* parser
)
17231 tree params
= NULL_TREE
;
17232 bool maybe_unary_selector_p
= true;
17233 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17235 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
17237 tree selector
= NULL_TREE
, typename
, identifier
;
17239 if (token
->type
!= CPP_COLON
)
17240 selector
= cp_parser_objc_selector (parser
);
17242 /* Detect if we have a unary selector. */
17243 if (maybe_unary_selector_p
17244 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
17247 maybe_unary_selector_p
= false;
17248 cp_parser_require (parser
, CPP_COLON
, "`:'");
17249 typename
= cp_parser_objc_typename (parser
);
17250 identifier
= cp_parser_identifier (parser
);
17254 objc_build_keyword_decl (selector
,
17258 token
= cp_lexer_peek_token (parser
->lexer
);
17264 /* Parse the non-keyword Objective-C params. */
17267 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
)
17269 tree params
= make_node (TREE_LIST
);
17270 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17271 *ellipsisp
= false; /* Initially, assume no ellipsis. */
17273 while (token
->type
== CPP_COMMA
)
17275 cp_parameter_declarator
*parmdecl
;
17278 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17279 token
= cp_lexer_peek_token (parser
->lexer
);
17281 if (token
->type
== CPP_ELLIPSIS
)
17283 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
17288 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
17289 parm
= grokdeclarator (parmdecl
->declarator
,
17290 &parmdecl
->decl_specifiers
,
17291 PARM
, /*initialized=*/0,
17292 /*attrlist=*/NULL
);
17294 chainon (params
, build_tree_list (NULL_TREE
, parm
));
17295 token
= cp_lexer_peek_token (parser
->lexer
);
17301 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17304 cp_parser_objc_interstitial_code (cp_parser
* parser
)
17306 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17308 /* If the next token is `extern' and the following token is a string
17309 literal, then we have a linkage specification. */
17310 if (token
->keyword
== RID_EXTERN
17311 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
17312 cp_parser_linkage_specification (parser
);
17313 /* Handle #pragma, if any. */
17314 else if (token
->type
== CPP_PRAGMA
)
17315 cp_parser_pragma (parser
, pragma_external
);
17316 /* Allow stray semicolons. */
17317 else if (token
->type
== CPP_SEMICOLON
)
17318 cp_lexer_consume_token (parser
->lexer
);
17319 /* Finally, try to parse a block-declaration, or a function-definition. */
17321 cp_parser_block_declaration (parser
, /*statement_p=*/false);
17324 /* Parse a method signature. */
17327 cp_parser_objc_method_signature (cp_parser
* parser
)
17329 tree rettype
, kwdparms
, optparms
;
17330 bool ellipsis
= false;
17332 cp_parser_objc_method_type (parser
);
17333 rettype
= cp_parser_objc_typename (parser
);
17334 kwdparms
= cp_parser_objc_method_keyword_params (parser
);
17335 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
);
17337 return objc_build_method_signature (rettype
, kwdparms
, optparms
, ellipsis
);
17340 /* Pars an Objective-C method prototype list. */
17343 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
17345 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17347 while (token
->keyword
!= RID_AT_END
)
17349 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
17351 objc_add_method_declaration
17352 (cp_parser_objc_method_signature (parser
));
17353 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17356 /* Allow for interspersed non-ObjC++ code. */
17357 cp_parser_objc_interstitial_code (parser
);
17359 token
= cp_lexer_peek_token (parser
->lexer
);
17362 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17363 objc_finish_interface ();
17366 /* Parse an Objective-C method definition list. */
17369 cp_parser_objc_method_definition_list (cp_parser
* parser
)
17371 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17373 while (token
->keyword
!= RID_AT_END
)
17377 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
17379 push_deferring_access_checks (dk_deferred
);
17380 objc_start_method_definition
17381 (cp_parser_objc_method_signature (parser
));
17383 /* For historical reasons, we accept an optional semicolon. */
17384 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17385 cp_lexer_consume_token (parser
->lexer
);
17387 perform_deferred_access_checks ();
17388 stop_deferring_access_checks ();
17389 meth
= cp_parser_function_definition_after_declarator (parser
,
17391 pop_deferring_access_checks ();
17392 objc_finish_method_definition (meth
);
17395 /* Allow for interspersed non-ObjC++ code. */
17396 cp_parser_objc_interstitial_code (parser
);
17398 token
= cp_lexer_peek_token (parser
->lexer
);
17401 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17402 objc_finish_implementation ();
17405 /* Parse Objective-C ivars. */
17408 cp_parser_objc_class_ivars (cp_parser
* parser
)
17410 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17412 if (token
->type
!= CPP_OPEN_BRACE
)
17413 return; /* No ivars specified. */
17415 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
17416 token
= cp_lexer_peek_token (parser
->lexer
);
17418 while (token
->type
!= CPP_CLOSE_BRACE
)
17420 cp_decl_specifier_seq declspecs
;
17421 int decl_class_or_enum_p
;
17422 tree prefix_attributes
;
17424 cp_parser_objc_visibility_spec (parser
);
17426 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
17429 cp_parser_decl_specifier_seq (parser
,
17430 CP_PARSER_FLAGS_OPTIONAL
,
17432 &decl_class_or_enum_p
);
17433 prefix_attributes
= declspecs
.attributes
;
17434 declspecs
.attributes
= NULL_TREE
;
17436 /* Keep going until we hit the `;' at the end of the
17438 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17440 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
17441 cp_declarator
*declarator
= NULL
;
17442 int ctor_dtor_or_conv_p
;
17444 /* Check for a (possibly unnamed) bitfield declaration. */
17445 token
= cp_lexer_peek_token (parser
->lexer
);
17446 if (token
->type
== CPP_COLON
)
17449 if (token
->type
== CPP_NAME
17450 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
17453 /* Get the name of the bitfield. */
17454 declarator
= make_id_declarator (NULL_TREE
,
17455 cp_parser_identifier (parser
),
17459 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
17460 /* Get the width of the bitfield. */
17462 = cp_parser_constant_expression (parser
,
17463 /*allow_non_constant=*/false,
17468 /* Parse the declarator. */
17470 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17471 &ctor_dtor_or_conv_p
,
17472 /*parenthesized_p=*/NULL
,
17473 /*member_p=*/false);
17476 /* Look for attributes that apply to the ivar. */
17477 attributes
= cp_parser_attributes_opt (parser
);
17478 /* Remember which attributes are prefix attributes and
17480 first_attribute
= attributes
;
17481 /* Combine the attributes. */
17482 attributes
= chainon (prefix_attributes
, attributes
);
17486 /* Create the bitfield declaration. */
17487 decl
= grokbitfield (declarator
, &declspecs
, width
);
17488 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
17491 decl
= grokfield (declarator
, &declspecs
,
17492 NULL_TREE
, /*init_const_expr_p=*/false,
17493 NULL_TREE
, attributes
);
17495 /* Add the instance variable. */
17496 objc_add_instance_variable (decl
);
17498 /* Reset PREFIX_ATTRIBUTES. */
17499 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
17500 attributes
= TREE_CHAIN (attributes
);
17502 TREE_CHAIN (attributes
) = NULL_TREE
;
17504 token
= cp_lexer_peek_token (parser
->lexer
);
17506 if (token
->type
== CPP_COMMA
)
17508 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17514 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17515 token
= cp_lexer_peek_token (parser
->lexer
);
17518 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
17519 /* For historical reasons, we accept an optional semicolon. */
17520 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17521 cp_lexer_consume_token (parser
->lexer
);
17524 /* Parse an Objective-C protocol declaration. */
17527 cp_parser_objc_protocol_declaration (cp_parser
* parser
)
17529 tree proto
, protorefs
;
17532 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
17533 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
17535 error ("identifier expected after %<@protocol%>");
17539 /* See if we have a forward declaration or a definition. */
17540 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
17542 /* Try a forward declaration first. */
17543 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
17545 objc_declare_protocols (cp_parser_objc_identifier_list (parser
));
17547 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17550 /* Ok, we got a full-fledged definition (or at least should). */
17553 proto
= cp_parser_identifier (parser
);
17554 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
17555 objc_start_protocol (proto
, protorefs
);
17556 cp_parser_objc_method_prototype_list (parser
);
17560 /* Parse an Objective-C superclass or category. */
17563 cp_parser_objc_superclass_or_category (cp_parser
*parser
, tree
*super
,
17566 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
17568 *super
= *categ
= NULL_TREE
;
17569 if (next
->type
== CPP_COLON
)
17571 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
17572 *super
= cp_parser_identifier (parser
);
17574 else if (next
->type
== CPP_OPEN_PAREN
)
17576 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
17577 *categ
= cp_parser_identifier (parser
);
17578 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17582 /* Parse an Objective-C class interface. */
17585 cp_parser_objc_class_interface (cp_parser
* parser
)
17587 tree name
, super
, categ
, protos
;
17589 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
17590 name
= cp_parser_identifier (parser
);
17591 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
17592 protos
= cp_parser_objc_protocol_refs_opt (parser
);
17594 /* We have either a class or a category on our hands. */
17596 objc_start_category_interface (name
, categ
, protos
);
17599 objc_start_class_interface (name
, super
, protos
);
17600 /* Handle instance variable declarations, if any. */
17601 cp_parser_objc_class_ivars (parser
);
17602 objc_continue_interface ();
17605 cp_parser_objc_method_prototype_list (parser
);
17608 /* Parse an Objective-C class implementation. */
17611 cp_parser_objc_class_implementation (cp_parser
* parser
)
17613 tree name
, super
, categ
;
17615 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
17616 name
= cp_parser_identifier (parser
);
17617 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
17619 /* We have either a class or a category on our hands. */
17621 objc_start_category_implementation (name
, categ
);
17624 objc_start_class_implementation (name
, super
);
17625 /* Handle instance variable declarations, if any. */
17626 cp_parser_objc_class_ivars (parser
);
17627 objc_continue_implementation ();
17630 cp_parser_objc_method_definition_list (parser
);
17633 /* Consume the @end token and finish off the implementation. */
17636 cp_parser_objc_end_implementation (cp_parser
* parser
)
17638 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17639 objc_finish_implementation ();
17642 /* Parse an Objective-C declaration. */
17645 cp_parser_objc_declaration (cp_parser
* parser
)
17647 /* Try to figure out what kind of declaration is present. */
17648 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
17650 switch (kwd
->keyword
)
17653 cp_parser_objc_alias_declaration (parser
);
17656 cp_parser_objc_class_declaration (parser
);
17658 case RID_AT_PROTOCOL
:
17659 cp_parser_objc_protocol_declaration (parser
);
17661 case RID_AT_INTERFACE
:
17662 cp_parser_objc_class_interface (parser
);
17664 case RID_AT_IMPLEMENTATION
:
17665 cp_parser_objc_class_implementation (parser
);
17668 cp_parser_objc_end_implementation (parser
);
17671 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
17672 cp_parser_skip_to_end_of_block_or_statement (parser
);
17676 /* Parse an Objective-C try-catch-finally statement.
17678 objc-try-catch-finally-stmt:
17679 @try compound-statement objc-catch-clause-seq [opt]
17680 objc-finally-clause [opt]
17682 objc-catch-clause-seq:
17683 objc-catch-clause objc-catch-clause-seq [opt]
17686 @catch ( exception-declaration ) compound-statement
17688 objc-finally-clause
17689 @finally compound-statement
17691 Returns NULL_TREE. */
17694 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
) {
17695 location_t location
;
17698 cp_parser_require_keyword (parser
, RID_AT_TRY
, "`@try'");
17699 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17700 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17701 node, lest it get absorbed into the surrounding block. */
17702 stmt
= push_stmt_list ();
17703 cp_parser_compound_statement (parser
, NULL
, false);
17704 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
17706 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
17708 cp_parameter_declarator
*parmdecl
;
17711 cp_lexer_consume_token (parser
->lexer
);
17712 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17713 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
17714 parm
= grokdeclarator (parmdecl
->declarator
,
17715 &parmdecl
->decl_specifiers
,
17716 PARM
, /*initialized=*/0,
17717 /*attrlist=*/NULL
);
17718 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17719 objc_begin_catch_clause (parm
);
17720 cp_parser_compound_statement (parser
, NULL
, false);
17721 objc_finish_catch_clause ();
17724 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
17726 cp_lexer_consume_token (parser
->lexer
);
17727 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17728 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17729 node, lest it get absorbed into the surrounding block. */
17730 stmt
= push_stmt_list ();
17731 cp_parser_compound_statement (parser
, NULL
, false);
17732 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
17735 return objc_finish_try_stmt ();
17738 /* Parse an Objective-C synchronized statement.
17740 objc-synchronized-stmt:
17741 @synchronized ( expression ) compound-statement
17743 Returns NULL_TREE. */
17746 cp_parser_objc_synchronized_statement (cp_parser
*parser
) {
17747 location_t location
;
17750 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, "`@synchronized'");
17752 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17753 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17754 lock
= cp_parser_expression (parser
, false);
17755 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17757 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17758 node, lest it get absorbed into the surrounding block. */
17759 stmt
= push_stmt_list ();
17760 cp_parser_compound_statement (parser
, NULL
, false);
17762 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
17765 /* Parse an Objective-C throw statement.
17768 @throw assignment-expression [opt] ;
17770 Returns a constructed '@throw' statement. */
17773 cp_parser_objc_throw_statement (cp_parser
*parser
) {
17774 tree expr
= NULL_TREE
;
17776 cp_parser_require_keyword (parser
, RID_AT_THROW
, "`@throw'");
17778 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17779 expr
= cp_parser_assignment_expression (parser
, false);
17781 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17783 return objc_build_throw_stmt (expr
);
17786 /* Parse an Objective-C statement. */
17789 cp_parser_objc_statement (cp_parser
* parser
) {
17790 /* Try to figure out what kind of declaration is present. */
17791 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
17793 switch (kwd
->keyword
)
17796 return cp_parser_objc_try_catch_finally_statement (parser
);
17797 case RID_AT_SYNCHRONIZED
:
17798 return cp_parser_objc_synchronized_statement (parser
);
17800 return cp_parser_objc_throw_statement (parser
);
17802 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
17803 cp_parser_skip_to_end_of_block_or_statement (parser
);
17806 return error_mark_node
;
17809 /* OpenMP 2.5 parsing routines. */
17811 /* All OpenMP clauses. OpenMP 2.5. */
17812 typedef enum pragma_omp_clause
{
17813 PRAGMA_OMP_CLAUSE_NONE
= 0,
17815 PRAGMA_OMP_CLAUSE_COPYIN
,
17816 PRAGMA_OMP_CLAUSE_COPYPRIVATE
,
17817 PRAGMA_OMP_CLAUSE_DEFAULT
,
17818 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
,
17819 PRAGMA_OMP_CLAUSE_IF
,
17820 PRAGMA_OMP_CLAUSE_LASTPRIVATE
,
17821 PRAGMA_OMP_CLAUSE_NOWAIT
,
17822 PRAGMA_OMP_CLAUSE_NUM_THREADS
,
17823 PRAGMA_OMP_CLAUSE_ORDERED
,
17824 PRAGMA_OMP_CLAUSE_PRIVATE
,
17825 PRAGMA_OMP_CLAUSE_REDUCTION
,
17826 PRAGMA_OMP_CLAUSE_SCHEDULE
,
17827 PRAGMA_OMP_CLAUSE_SHARED
17828 } pragma_omp_clause
;
17830 /* Returns name of the next clause.
17831 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17832 the token is not consumed. Otherwise appropriate pragma_omp_clause is
17833 returned and the token is consumed. */
17835 static pragma_omp_clause
17836 cp_parser_omp_clause_name (cp_parser
*parser
)
17838 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
17840 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
17841 result
= PRAGMA_OMP_CLAUSE_IF
;
17842 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
17843 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
17844 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
17845 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
17846 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17848 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
17849 const char *p
= IDENTIFIER_POINTER (id
);
17854 if (!strcmp ("copyin", p
))
17855 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
17856 else if (!strcmp ("copyprivate", p
))
17857 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
17860 if (!strcmp ("firstprivate", p
))
17861 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
17864 if (!strcmp ("lastprivate", p
))
17865 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
17868 if (!strcmp ("nowait", p
))
17869 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
17870 else if (!strcmp ("num_threads", p
))
17871 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
17874 if (!strcmp ("ordered", p
))
17875 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
17878 if (!strcmp ("reduction", p
))
17879 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
17882 if (!strcmp ("schedule", p
))
17883 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
17884 else if (!strcmp ("shared", p
))
17885 result
= PRAGMA_OMP_CLAUSE_SHARED
;
17890 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
17891 cp_lexer_consume_token (parser
->lexer
);
17896 /* Validate that a clause of the given type does not already exist. */
17899 check_no_duplicate_clause (tree clauses
, enum tree_code code
, const char *name
)
17903 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
17904 if (OMP_CLAUSE_CODE (c
) == code
)
17906 error ("too many %qs clauses", name
);
17914 variable-list , identifier
17916 In addition, we match a closing parenthesis. An opening parenthesis
17917 will have been consumed by the caller.
17919 If KIND is nonzero, create the appropriate node and install the decl
17920 in OMP_CLAUSE_DECL and add the node to the head of the list.
17922 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17923 return the list created. */
17926 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
17933 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
17934 /*check_dependency_p=*/true,
17935 /*template_p=*/NULL
,
17936 /*declarator_p=*/false,
17937 /*optional_p=*/false);
17938 if (name
== error_mark_node
)
17941 decl
= cp_parser_lookup_name_simple (parser
, name
);
17942 if (decl
== error_mark_node
)
17943 cp_parser_name_lookup_error (parser
, name
, decl
, NULL
);
17944 else if (kind
!= 0)
17946 tree u
= build_omp_clause (kind
);
17947 OMP_CLAUSE_DECL (u
) = decl
;
17948 OMP_CLAUSE_CHAIN (u
) = list
;
17952 list
= tree_cons (decl
, NULL_TREE
, list
);
17955 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
17957 cp_lexer_consume_token (parser
->lexer
);
17960 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
17964 /* Try to resync to an unnested comma. Copied from
17965 cp_parser_parenthesized_expression_list. */
17967 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
17968 /*recovering=*/true,
17970 /*consume_paren=*/true);
17978 /* Similarly, but expect leading and trailing parenthesis. This is a very
17979 common case for omp clauses. */
17982 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
17984 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
17985 return cp_parser_omp_var_list_no_open (parser
, kind
, list
);
17990 default ( shared | none ) */
17993 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
)
17995 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
17998 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18000 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18002 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
18003 const char *p
= IDENTIFIER_POINTER (id
);
18008 if (strcmp ("none", p
) != 0)
18010 kind
= OMP_CLAUSE_DEFAULT_NONE
;
18014 if (strcmp ("shared", p
) != 0)
18016 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
18023 cp_lexer_consume_token (parser
->lexer
);
18028 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
18031 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18032 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18033 /*or_comma=*/false,
18034 /*consume_paren=*/true);
18036 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
18039 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default");
18040 c
= build_omp_clause (OMP_CLAUSE_DEFAULT
);
18041 OMP_CLAUSE_CHAIN (c
) = list
;
18042 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
18048 if ( expression ) */
18051 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
)
18055 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18058 t
= cp_parser_condition (parser
);
18060 if (t
== error_mark_node
18061 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18062 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18063 /*or_comma=*/false,
18064 /*consume_paren=*/true);
18066 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if");
18068 c
= build_omp_clause (OMP_CLAUSE_IF
);
18069 OMP_CLAUSE_IF_EXPR (c
) = t
;
18070 OMP_CLAUSE_CHAIN (c
) = list
;
18079 cp_parser_omp_clause_nowait (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
18083 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait");
18085 c
= build_omp_clause (OMP_CLAUSE_NOWAIT
);
18086 OMP_CLAUSE_CHAIN (c
) = list
;
18091 num_threads ( expression ) */
18094 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
)
18098 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18101 t
= cp_parser_expression (parser
, false);
18103 if (t
== error_mark_node
18104 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18105 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18106 /*or_comma=*/false,
18107 /*consume_paren=*/true);
18109 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
, "num_threads");
18111 c
= build_omp_clause (OMP_CLAUSE_NUM_THREADS
);
18112 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
18113 OMP_CLAUSE_CHAIN (c
) = list
;
18122 cp_parser_omp_clause_ordered (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
18126 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
, "ordered");
18128 c
= build_omp_clause (OMP_CLAUSE_ORDERED
);
18129 OMP_CLAUSE_CHAIN (c
) = list
;
18134 reduction ( reduction-operator : variable-list )
18136 reduction-operator:
18137 One of: + * - & ^ | && || */
18140 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
18142 enum tree_code code
;
18145 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18148 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
18160 code
= BIT_AND_EXPR
;
18163 code
= BIT_XOR_EXPR
;
18166 code
= BIT_IOR_EXPR
;
18169 code
= TRUTH_ANDIF_EXPR
;
18172 code
= TRUTH_ORIF_EXPR
;
18175 cp_parser_error (parser
, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18177 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18178 /*or_comma=*/false,
18179 /*consume_paren=*/true);
18182 cp_lexer_consume_token (parser
->lexer
);
18184 if (!cp_parser_require (parser
, CPP_COLON
, "`:'"))
18187 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
);
18188 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
18189 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
18195 schedule ( schedule-kind )
18196 schedule ( schedule-kind , expression )
18199 static | dynamic | guided | runtime */
18202 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
)
18206 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
18209 c
= build_omp_clause (OMP_CLAUSE_SCHEDULE
);
18211 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18213 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
18214 const char *p
= IDENTIFIER_POINTER (id
);
18219 if (strcmp ("dynamic", p
) != 0)
18221 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
18225 if (strcmp ("guided", p
) != 0)
18227 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
18231 if (strcmp ("runtime", p
) != 0)
18233 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
18240 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
18241 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
18244 cp_lexer_consume_token (parser
->lexer
);
18246 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18248 cp_lexer_consume_token (parser
->lexer
);
18250 t
= cp_parser_assignment_expression (parser
, false);
18252 if (t
== error_mark_node
)
18254 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
18255 error ("schedule %<runtime%> does not take "
18256 "a %<chunk_size%> parameter");
18258 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
18260 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18263 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`,' or `)'"))
18266 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule");
18267 OMP_CLAUSE_CHAIN (c
) = list
;
18271 cp_parser_error (parser
, "invalid schedule kind");
18273 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18274 /*or_comma=*/false,
18275 /*consume_paren=*/true);
18279 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18280 is a bitmask in MASK. Return the list of clauses found; the result
18281 of clause default goes in *pdefault. */
18284 cp_parser_omp_all_clauses (cp_parser
*parser
, unsigned int mask
,
18285 const char *where
, cp_token
*pragma_tok
)
18287 tree clauses
= NULL
;
18289 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
18291 pragma_omp_clause c_kind
= cp_parser_omp_clause_name (parser
);
18292 const char *c_name
;
18293 tree prev
= clauses
;
18297 case PRAGMA_OMP_CLAUSE_COPYIN
:
18298 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
18301 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
18302 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
18304 c_name
= "copyprivate";
18306 case PRAGMA_OMP_CLAUSE_DEFAULT
:
18307 clauses
= cp_parser_omp_clause_default (parser
, clauses
);
18308 c_name
= "default";
18310 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
18311 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
18313 c_name
= "firstprivate";
18315 case PRAGMA_OMP_CLAUSE_IF
:
18316 clauses
= cp_parser_omp_clause_if (parser
, clauses
);
18319 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
18320 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
18322 c_name
= "lastprivate";
18324 case PRAGMA_OMP_CLAUSE_NOWAIT
:
18325 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
);
18328 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
18329 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
);
18330 c_name
= "num_threads";
18332 case PRAGMA_OMP_CLAUSE_ORDERED
:
18333 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
);
18334 c_name
= "ordered";
18336 case PRAGMA_OMP_CLAUSE_PRIVATE
:
18337 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
18339 c_name
= "private";
18341 case PRAGMA_OMP_CLAUSE_REDUCTION
:
18342 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
18343 c_name
= "reduction";
18345 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
18346 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
);
18347 c_name
= "schedule";
18349 case PRAGMA_OMP_CLAUSE_SHARED
:
18350 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
18355 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
18359 if (((mask
>> c_kind
) & 1) == 0)
18361 /* Remove the invalid clause(s) from the list to avoid
18362 confusing the rest of the compiler. */
18364 error ("%qs is not valid for %qs", c_name
, where
);
18368 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
18369 return finish_omp_clauses (clauses
);
18376 In practice, we're also interested in adding the statement to an
18377 outer node. So it is convenient if we work around the fact that
18378 cp_parser_statement calls add_stmt. */
18381 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
18383 unsigned save
= parser
->in_statement
;
18385 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18386 This preserves the "not within loop or switch" style error messages
18387 for nonsense cases like
18393 if (parser
->in_statement
)
18394 parser
->in_statement
= IN_OMP_BLOCK
;
18400 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
18402 parser
->in_statement
= save
;
18406 cp_parser_omp_structured_block (cp_parser
*parser
)
18408 tree stmt
= begin_omp_structured_block ();
18409 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
18411 cp_parser_statement (parser
, NULL_TREE
, false);
18413 cp_parser_end_omp_structured_block (parser
, save
);
18414 return finish_omp_structured_block (stmt
);
18418 # pragma omp atomic new-line
18422 x binop= expr | x++ | ++x | x-- | --x
18424 +, *, -, /, &, ^, |, <<, >>
18426 where x is an lvalue expression with scalar type. */
18429 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
18432 enum tree_code code
;
18434 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18436 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
18438 switch (TREE_CODE (lhs
))
18443 case PREINCREMENT_EXPR
:
18444 case POSTINCREMENT_EXPR
:
18445 lhs
= TREE_OPERAND (lhs
, 0);
18447 rhs
= integer_one_node
;
18450 case PREDECREMENT_EXPR
:
18451 case POSTDECREMENT_EXPR
:
18452 lhs
= TREE_OPERAND (lhs
, 0);
18454 rhs
= integer_one_node
;
18458 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
18464 code
= TRUNC_DIV_EXPR
;
18472 case CPP_LSHIFT_EQ
:
18473 code
= LSHIFT_EXPR
;
18475 case CPP_RSHIFT_EQ
:
18476 code
= RSHIFT_EXPR
;
18479 code
= BIT_AND_EXPR
;
18482 code
= BIT_IOR_EXPR
;
18485 code
= BIT_XOR_EXPR
;
18488 cp_parser_error (parser
,
18489 "invalid operator for %<#pragma omp atomic%>");
18492 cp_lexer_consume_token (parser
->lexer
);
18494 rhs
= cp_parser_expression (parser
, false);
18495 if (rhs
== error_mark_node
)
18499 finish_omp_atomic (code
, lhs
, rhs
);
18500 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18504 cp_parser_skip_to_end_of_block_or_statement (parser
);
18509 # pragma omp barrier new-line */
18512 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
18514 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18515 finish_omp_barrier ();
18519 # pragma omp critical [(name)] new-line
18520 structured-block */
18523 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
18525 tree stmt
, name
= NULL
;
18527 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18529 cp_lexer_consume_token (parser
->lexer
);
18531 name
= cp_parser_identifier (parser
);
18533 if (name
== error_mark_node
18534 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18535 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18536 /*or_comma=*/false,
18537 /*consume_paren=*/true);
18538 if (name
== error_mark_node
)
18541 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18543 stmt
= cp_parser_omp_structured_block (parser
);
18544 return c_finish_omp_critical (stmt
, name
);
18548 # pragma omp flush flush-vars[opt] new-line
18551 ( variable-list ) */
18554 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
18556 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18557 (void) cp_parser_omp_var_list (parser
, 0, NULL
);
18558 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18560 finish_omp_flush ();
18563 /* Parse the restricted form of the for statment allowed by OpenMP. */
18566 cp_parser_omp_for_loop (cp_parser
*parser
)
18568 tree init
, cond
, incr
, body
, decl
, pre_body
;
18571 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
18573 cp_parser_error (parser
, "for statement expected");
18576 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
18577 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18580 init
= decl
= NULL
;
18581 pre_body
= push_stmt_list ();
18582 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18584 cp_decl_specifier_seq type_specifiers
;
18586 /* First, try to parse as an initialized declaration. See
18587 cp_parser_condition, from whence the bulk of this is copied. */
18589 cp_parser_parse_tentatively (parser
);
18590 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
18592 if (!cp_parser_error_occurred (parser
))
18594 tree asm_specification
, attributes
;
18595 cp_declarator
*declarator
;
18597 declarator
= cp_parser_declarator (parser
,
18598 CP_PARSER_DECLARATOR_NAMED
,
18599 /*ctor_dtor_or_conv_p=*/NULL
,
18600 /*parenthesized_p=*/NULL
,
18601 /*member_p=*/false);
18602 attributes
= cp_parser_attributes_opt (parser
);
18603 asm_specification
= cp_parser_asm_specification_opt (parser
);
18605 cp_parser_require (parser
, CPP_EQ
, "`='");
18606 if (cp_parser_parse_definitely (parser
))
18610 decl
= start_decl (declarator
, &type_specifiers
,
18611 /*initialized_p=*/false, attributes
,
18612 /*prefix_attributes=*/NULL_TREE
,
18615 init
= cp_parser_assignment_expression (parser
, false);
18617 cp_finish_decl (decl
, NULL_TREE
, /*init_const_expr_p=*/false,
18618 asm_specification
, LOOKUP_ONLYCONVERTING
);
18621 pop_scope (pushed_scope
);
18625 cp_parser_abort_tentative_parse (parser
);
18627 /* If parsing as an initialized declaration failed, try again as
18628 a simple expression. */
18630 init
= cp_parser_expression (parser
, false);
18632 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
18633 pre_body
= pop_stmt_list (pre_body
);
18636 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18637 cond
= cp_parser_condition (parser
);
18638 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
18641 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
18642 incr
= cp_parser_expression (parser
, false);
18644 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18645 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18646 /*or_comma=*/false,
18647 /*consume_paren=*/true);
18649 /* Note that we saved the original contents of this flag when we entered
18650 the structured block, and so we don't need to re-save it here. */
18651 parser
->in_statement
= IN_OMP_FOR
;
18653 /* Note that the grammar doesn't call for a structured block here,
18654 though the loop as a whole is a structured block. */
18655 body
= push_stmt_list ();
18656 cp_parser_statement (parser
, NULL_TREE
, false);
18657 body
= pop_stmt_list (body
);
18659 return finish_omp_for (loc
, decl
, init
, cond
, incr
, body
, pre_body
);
18663 #pragma omp for for-clause[optseq] new-line
18666 #define OMP_FOR_CLAUSE_MASK \
18667 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18668 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18669 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18670 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18671 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18672 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18673 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18676 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
)
18678 tree clauses
, sb
, ret
;
18681 clauses
= cp_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
18682 "#pragma omp for", pragma_tok
);
18684 sb
= begin_omp_structured_block ();
18685 save
= cp_parser_begin_omp_structured_block (parser
);
18687 ret
= cp_parser_omp_for_loop (parser
);
18689 OMP_FOR_CLAUSES (ret
) = clauses
;
18691 cp_parser_end_omp_structured_block (parser
, save
);
18692 add_stmt (finish_omp_structured_block (sb
));
18698 # pragma omp master new-line
18699 structured-block */
18702 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
18704 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18705 return c_finish_omp_master (cp_parser_omp_structured_block (parser
));
18709 # pragma omp ordered new-line
18710 structured-block */
18713 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
18715 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18716 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser
));
18722 { section-sequence }
18725 section-directive[opt] structured-block
18726 section-sequence section-directive structured-block */
18729 cp_parser_omp_sections_scope (cp_parser
*parser
)
18731 tree stmt
, substmt
;
18732 bool error_suppress
= false;
18735 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
18738 stmt
= push_stmt_list ();
18740 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
18744 substmt
= begin_omp_structured_block ();
18745 save
= cp_parser_begin_omp_structured_block (parser
);
18749 cp_parser_statement (parser
, NULL_TREE
, false);
18751 tok
= cp_lexer_peek_token (parser
->lexer
);
18752 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
18754 if (tok
->type
== CPP_CLOSE_BRACE
)
18756 if (tok
->type
== CPP_EOF
)
18760 cp_parser_end_omp_structured_block (parser
, save
);
18761 substmt
= finish_omp_structured_block (substmt
);
18762 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
18763 add_stmt (substmt
);
18768 tok
= cp_lexer_peek_token (parser
->lexer
);
18769 if (tok
->type
== CPP_CLOSE_BRACE
)
18771 if (tok
->type
== CPP_EOF
)
18774 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
18776 cp_lexer_consume_token (parser
->lexer
);
18777 cp_parser_require_pragma_eol (parser
, tok
);
18778 error_suppress
= false;
18780 else if (!error_suppress
)
18782 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
18783 error_suppress
= true;
18786 substmt
= cp_parser_omp_structured_block (parser
);
18787 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
18788 add_stmt (substmt
);
18790 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
18792 substmt
= pop_stmt_list (stmt
);
18794 stmt
= make_node (OMP_SECTIONS
);
18795 TREE_TYPE (stmt
) = void_type_node
;
18796 OMP_SECTIONS_BODY (stmt
) = substmt
;
18803 # pragma omp sections sections-clause[optseq] newline
18806 #define OMP_SECTIONS_CLAUSE_MASK \
18807 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18808 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18809 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18810 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18811 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18814 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
)
18818 clauses
= cp_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
18819 "#pragma omp sections", pragma_tok
);
18821 ret
= cp_parser_omp_sections_scope (parser
);
18823 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
18829 # pragma parallel parallel-clause new-line
18830 # pragma parallel for parallel-for-clause new-line
18831 # pragma parallel sections parallel-sections-clause new-line */
18833 #define OMP_PARALLEL_CLAUSE_MASK \
18834 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
18835 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18836 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18837 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
18838 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
18839 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
18840 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18841 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18844 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
18846 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
18847 const char *p_name
= "#pragma omp parallel";
18848 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
18849 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
18852 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
18854 cp_lexer_consume_token (parser
->lexer
);
18855 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
18856 p_name
= "#pragma omp parallel for";
18857 mask
|= OMP_FOR_CLAUSE_MASK
;
18858 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
18860 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18862 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
18863 const char *p
= IDENTIFIER_POINTER (id
);
18864 if (strcmp (p
, "sections") == 0)
18866 cp_lexer_consume_token (parser
->lexer
);
18867 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
18868 p_name
= "#pragma omp parallel sections";
18869 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
18870 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
18874 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
18875 block
= begin_omp_parallel ();
18876 save
= cp_parser_begin_omp_structured_block (parser
);
18880 case PRAGMA_OMP_PARALLEL
:
18881 cp_parser_already_scoped_statement (parser
);
18882 par_clause
= clauses
;
18885 case PRAGMA_OMP_PARALLEL_FOR
:
18886 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
18887 stmt
= cp_parser_omp_for_loop (parser
);
18889 OMP_FOR_CLAUSES (stmt
) = ws_clause
;
18892 case PRAGMA_OMP_PARALLEL_SECTIONS
:
18893 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
18894 stmt
= cp_parser_omp_sections_scope (parser
);
18896 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
18900 gcc_unreachable ();
18903 cp_parser_end_omp_structured_block (parser
, save
);
18904 stmt
= finish_omp_parallel (par_clause
, block
);
18905 if (p_kind
!= PRAGMA_OMP_PARALLEL
)
18906 OMP_PARALLEL_COMBINED (stmt
) = 1;
18911 # pragma omp single single-clause[optseq] new-line
18912 structured-block */
18914 #define OMP_SINGLE_CLAUSE_MASK \
18915 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18916 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18917 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
18918 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18921 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
18923 tree stmt
= make_node (OMP_SINGLE
);
18924 TREE_TYPE (stmt
) = void_type_node
;
18926 OMP_SINGLE_CLAUSES (stmt
)
18927 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
18928 "#pragma omp single", pragma_tok
);
18929 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
18931 return add_stmt (stmt
);
18935 # pragma omp threadprivate (variable-list) */
18938 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
18942 vars
= cp_parser_omp_var_list (parser
, 0, NULL
);
18943 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18945 if (!targetm
.have_tls
)
18946 sorry ("threadprivate variables not supported in this target");
18948 finish_omp_threadprivate (vars
);
18951 /* Main entry point to OpenMP statement pragmas. */
18954 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
18958 switch (pragma_tok
->pragma_kind
)
18960 case PRAGMA_OMP_ATOMIC
:
18961 cp_parser_omp_atomic (parser
, pragma_tok
);
18963 case PRAGMA_OMP_CRITICAL
:
18964 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
18966 case PRAGMA_OMP_FOR
:
18967 stmt
= cp_parser_omp_for (parser
, pragma_tok
);
18969 case PRAGMA_OMP_MASTER
:
18970 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
18972 case PRAGMA_OMP_ORDERED
:
18973 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
18975 case PRAGMA_OMP_PARALLEL
:
18976 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
);
18978 case PRAGMA_OMP_SECTIONS
:
18979 stmt
= cp_parser_omp_sections (parser
, pragma_tok
);
18981 case PRAGMA_OMP_SINGLE
:
18982 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
18985 gcc_unreachable ();
18989 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
18994 static GTY (()) cp_parser
*the_parser
;
18997 /* Special handling for the first token or line in the file. The first
18998 thing in the file might be #pragma GCC pch_preprocess, which loads a
18999 PCH file, which is a GC collection point. So we need to handle this
19000 first pragma without benefit of an existing lexer structure.
19002 Always returns one token to the caller in *FIRST_TOKEN. This is
19003 either the true first token of the file, or the first token after
19004 the initial pragma. */
19007 cp_parser_initial_pragma (cp_token
*first_token
)
19011 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19012 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
19015 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19016 if (first_token
->type
== CPP_STRING
)
19018 name
= first_token
->value
;
19020 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19021 if (first_token
->type
!= CPP_PRAGMA_EOL
)
19022 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19025 error ("expected string literal");
19027 /* Skip to the end of the pragma. */
19028 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
19029 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19031 /* Now actually load the PCH file. */
19033 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
19035 /* Read one more token to return to our caller. We have to do this
19036 after reading the PCH file in, since its pointers have to be
19038 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19041 /* Normal parsing of a pragma token. Here we can (and must) use the
19045 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
19047 cp_token
*pragma_tok
;
19050 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
19051 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
19052 parser
->lexer
->in_pragma
= true;
19054 id
= pragma_tok
->pragma_kind
;
19057 case PRAGMA_GCC_PCH_PREPROCESS
:
19058 error ("%<#pragma GCC pch_preprocess%> must be first");
19061 case PRAGMA_OMP_BARRIER
:
19064 case pragma_compound
:
19065 cp_parser_omp_barrier (parser
, pragma_tok
);
19068 error ("%<#pragma omp barrier%> may only be "
19069 "used in compound statements");
19076 case PRAGMA_OMP_FLUSH
:
19079 case pragma_compound
:
19080 cp_parser_omp_flush (parser
, pragma_tok
);
19083 error ("%<#pragma omp flush%> may only be "
19084 "used in compound statements");
19091 case PRAGMA_OMP_THREADPRIVATE
:
19092 cp_parser_omp_threadprivate (parser
, pragma_tok
);
19095 case PRAGMA_OMP_ATOMIC
:
19096 case PRAGMA_OMP_CRITICAL
:
19097 case PRAGMA_OMP_FOR
:
19098 case PRAGMA_OMP_MASTER
:
19099 case PRAGMA_OMP_ORDERED
:
19100 case PRAGMA_OMP_PARALLEL
:
19101 case PRAGMA_OMP_SECTIONS
:
19102 case PRAGMA_OMP_SINGLE
:
19103 if (context
== pragma_external
)
19105 cp_parser_omp_construct (parser
, pragma_tok
);
19108 case PRAGMA_OMP_SECTION
:
19109 error ("%<#pragma omp section%> may only be used in "
19110 "%<#pragma omp sections%> construct");
19114 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
19115 c_invoke_pragma_handler (id
);
19119 cp_parser_error (parser
, "expected declaration specifiers");
19123 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
19127 /* The interface the pragma parsers have to the lexer. */
19130 pragma_lex (tree
*value
)
19133 enum cpp_ttype ret
;
19135 tok
= cp_lexer_peek_token (the_parser
->lexer
);
19138 *value
= tok
->value
;
19140 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
19142 else if (ret
== CPP_STRING
)
19143 *value
= cp_parser_string_literal (the_parser
, false, false);
19146 cp_lexer_consume_token (the_parser
->lexer
);
19147 if (ret
== CPP_KEYWORD
)
19155 /* External interface. */
19157 /* Parse one entire translation unit. */
19160 c_parse_file (void)
19162 bool error_occurred
;
19163 static bool already_called
= false;
19165 if (already_called
)
19167 sorry ("inter-module optimizations not implemented for C++");
19170 already_called
= true;
19172 the_parser
= cp_parser_new ();
19173 push_deferring_access_checks (flag_access_control
19174 ? dk_no_deferred
: dk_no_check
);
19175 error_occurred
= cp_parser_translation_unit (the_parser
);
19179 /* This variable must be provided by every front end. */
19183 #include "gt-cp-parser.h"