]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
re PR c++/22137 (Internal error: Segmentation fault (program cc1plus))
[thirdparty/gcc.git] / gcc / cp / parser.c
CommitLineData
a723baf1 1/* C++ Parser.
4514aa8c
NS
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
a723baf1
MM
4 Written by Mark Mitchell <mark@codesourcery.com>.
5
f5adbb8d 6 This file is part of GCC.
a723baf1 7
f5adbb8d 8 GCC is free software; you can redistribute it and/or modify it
a723baf1
MM
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
f5adbb8d 13 GCC is distributed in the hope that it will be useful, but
a723baf1
MM
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.
17
18 You should have received a copy of the GNU General Public License
f5adbb8d 19 along with GCC; see the file COPYING. If not, write to the Free
1788952f
KC
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
a723baf1
MM
22
23#include "config.h"
24#include "system.h"
25#include "coretypes.h"
26#include "tm.h"
27#include "dyn-string.h"
28#include "varray.h"
29#include "cpplib.h"
30#include "tree.h"
31#include "cp-tree.h"
32#include "c-pragma.h"
33#include "decl.h"
34#include "flags.h"
35#include "diagnostic.h"
a723baf1
MM
36#include "toplev.h"
37#include "output.h"
62d1db17 38#include "target.h"
e58a9aa1 39#include "c-common.h"
a723baf1
MM
40
41\f
42/* The lexer. */
43
c162c75e
MA
44/* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
a723baf1
MM
46
47/* A C++ token. */
48
49typedef struct cp_token GTY (())
50{
51 /* The kind of token. */
df2b750f 52 ENUM_BITFIELD (cpp_ttype) type : 8;
a723baf1
MM
53 /* If this token is a keyword, this value indicates which keyword.
54 Otherwise, this value is RID_MAX. */
df2b750f 55 ENUM_BITFIELD (rid) keyword : 8;
f4abade9
GB
56 /* Token flags. */
57 unsigned char flags;
03fd3f84 58 /* True if this token is from a system header. */
c162c75e 59 BOOL_BITFIELD in_system_header : 1;
7d381002
MA
60 /* True if this token is from a context where it is implicitly extern "C" */
61 BOOL_BITFIELD implicit_extern_c : 1;
522df488
DN
62 /* The value associated with this token, if any. */
63 tree value;
82a98427
NS
64 /* The location at which this token was found. */
65 location_t location;
a723baf1
MM
66} cp_token;
67
0c5e4866
NS
68/* We use a stack of token pointer for saving token sets. */
69typedef struct cp_token *cp_token_position;
d4e6fecb
NS
70DEF_VEC_P (cp_token_position);
71DEF_VEC_ALLOC_P (cp_token_position,heap);
0c5e4866 72
76aebc9f
NS
73static const cp_token eof_token =
74{
75 CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
76#if USE_MAPPED_LOCATION
77 0
78#else
79 {0, 0}
80#endif
81};
0c5e4866 82
a723baf1
MM
83/* The cp_lexer structure represents the C++ lexer. It is responsible
84 for managing the token stream from the preprocessor and supplying
c162c75e 85 it to the parser. Tokens are never added to the cp_lexer after
03fd3f84 86 it is created. */
a723baf1
MM
87
88typedef struct cp_lexer GTY (())
89{
0c5e4866
NS
90 /* The memory allocated for the buffer. NULL if this lexer does not
91 own the token buffer. */
76aebc9f
NS
92 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
93 /* If the lexer owns the buffer, this is the number of tokens in the
94 buffer. */
95 size_t buffer_length;
c8094d83 96
c162c75e 97 /* A pointer just past the last available token. The tokens
03fd3f84 98 in this lexer are [buffer, last_token). */
0c5e4866 99 cp_token_position GTY ((skip)) last_token;
c162c75e 100
0c5e4866 101 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
a723baf1 102 no more available tokens. */
0c5e4866 103 cp_token_position GTY ((skip)) next_token;
a723baf1
MM
104
105 /* A stack indicating positions at which cp_lexer_save_tokens was
106 called. The top entry is the most recent position at which we
0c5e4866
NS
107 began saving tokens. If the stack is non-empty, we are saving
108 tokens. */
d4e6fecb 109 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
a723baf1 110
a723baf1
MM
111 /* True if we should output debugging information. */
112 bool debugging_p;
113
114 /* The next lexer in a linked list of lexers. */
115 struct cp_lexer *next;
116} cp_lexer;
117
c162c75e
MA
118/* cp_token_cache is a range of tokens. There is no need to represent
119 allocate heap memory for it, since tokens are never removed from the
120 lexer's array. There is also no need for the GC to walk through
121 a cp_token_cache, since everything in here is referenced through
03fd3f84 122 a lexer. */
c162c75e
MA
123
124typedef struct cp_token_cache GTY(())
125{
03fd3f84 126 /* The beginning of the token range. */
c162c75e
MA
127 cp_token * GTY((skip)) first;
128
03fd3f84 129 /* Points immediately after the last token in the range. */
c162c75e
MA
130 cp_token * GTY ((skip)) last;
131} cp_token_cache;
132
a723baf1
MM
133/* Prototypes. */
134
17211ab5 135static cp_lexer *cp_lexer_new_main
94edc4ab 136 (void);
a723baf1 137static cp_lexer *cp_lexer_new_from_tokens
c162c75e
MA
138 (cp_token_cache *tokens);
139static void cp_lexer_destroy
140 (cp_lexer *);
a723baf1 141static int cp_lexer_saving_tokens
94edc4ab 142 (const cp_lexer *);
0c5e4866
NS
143static cp_token_position cp_lexer_token_position
144 (cp_lexer *, bool);
145static cp_token *cp_lexer_token_at
146 (cp_lexer *, cp_token_position);
a723baf1 147static void cp_lexer_get_preprocessor_token
94edc4ab 148 (cp_lexer *, cp_token *);
c162c75e
MA
149static inline cp_token *cp_lexer_peek_token
150 (cp_lexer *);
a723baf1 151static cp_token *cp_lexer_peek_nth_token
94edc4ab 152 (cp_lexer *, size_t);
f7b5ecd9 153static inline bool cp_lexer_next_token_is
94edc4ab 154 (cp_lexer *, enum cpp_ttype);
a723baf1 155static bool cp_lexer_next_token_is_not
94edc4ab 156 (cp_lexer *, enum cpp_ttype);
a723baf1 157static bool cp_lexer_next_token_is_keyword
94edc4ab 158 (cp_lexer *, enum rid);
21526606 159static cp_token *cp_lexer_consume_token
94edc4ab 160 (cp_lexer *);
a723baf1
MM
161static void cp_lexer_purge_token
162 (cp_lexer *);
163static void cp_lexer_purge_tokens_after
0c5e4866 164 (cp_lexer *, cp_token_position);
c162c75e
MA
165static void cp_lexer_handle_pragma
166 (cp_lexer *);
a723baf1 167static void cp_lexer_save_tokens
94edc4ab 168 (cp_lexer *);
a723baf1 169static void cp_lexer_commit_tokens
94edc4ab 170 (cp_lexer *);
a723baf1 171static void cp_lexer_rollback_tokens
94edc4ab 172 (cp_lexer *);
6983ea08 173#ifdef ENABLE_CHECKING
a723baf1 174static void cp_lexer_print_token
94edc4ab 175 (FILE *, cp_token *);
21526606 176static inline bool cp_lexer_debugging_p
94edc4ab 177 (cp_lexer *);
a723baf1 178static void cp_lexer_start_debugging
94edc4ab 179 (cp_lexer *) ATTRIBUTE_UNUSED;
a723baf1 180static void cp_lexer_stop_debugging
94edc4ab 181 (cp_lexer *) ATTRIBUTE_UNUSED;
6983ea08 182#else
2cfe82fe
ZW
183/* If we define cp_lexer_debug_stream to NULL it will provoke warnings
184 about passing NULL to functions that require non-NULL arguments
185 (fputs, fprintf). It will never be used, so all we need is a value
186 of the right type that's guaranteed not to be NULL. */
187#define cp_lexer_debug_stream stdout
188#define cp_lexer_print_token(str, tok) (void) 0
6983ea08
MA
189#define cp_lexer_debugging_p(lexer) 0
190#endif /* ENABLE_CHECKING */
a723baf1 191
c162c75e
MA
192static cp_token_cache *cp_token_cache_new
193 (cp_token *, cp_token *);
194
a723baf1 195/* Manifest constants. */
c162c75e 196#define CP_LEXER_BUFFER_SIZE 10000
0c5e4866 197#define CP_SAVED_TOKEN_STACK 5
a723baf1
MM
198
199/* A token type for keywords, as opposed to ordinary identifiers. */
200#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
201
202/* A token type for template-ids. If a template-id is processed while
203 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
204 the value of the CPP_TEMPLATE_ID is whatever was returned by
205 cp_parser_template_id. */
206#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
207
208/* A token type for nested-name-specifiers. If a
209 nested-name-specifier is processed while parsing tentatively, it is
210 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
211 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
212 cp_parser_nested_name_specifier_opt. */
213#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
214
215/* A token type for tokens that are not tokens at all; these are used
c162c75e 216 to represent slots in the array where there used to be a token
03fd3f84 217 that has now been deleted. */
b8b94c5b
PB
218#define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
219
220/* The number of token types, including C++-specific ones. */
221#define N_CP_TTYPES ((int) (CPP_PURGED + 1))
a723baf1
MM
222
223/* Variables. */
224
6983ea08 225#ifdef ENABLE_CHECKING
a723baf1
MM
226/* The stream to which debugging output should be written. */
227static FILE *cp_lexer_debug_stream;
6983ea08 228#endif /* ENABLE_CHECKING */
a723baf1 229
17211ab5
GK
230/* Create a new main C++ lexer, the lexer that gets tokens from the
231 preprocessor. */
a723baf1
MM
232
233static cp_lexer *
17211ab5 234cp_lexer_new_main (void)
a723baf1 235{
17211ab5 236 cp_token first_token;
76aebc9f
NS
237 cp_lexer *lexer;
238 cp_token *pos;
239 size_t alloc;
240 size_t space;
241 cp_token *buffer;
17211ab5 242
3d8a8aad
MS
243 /* It's possible that lexing the first token will load a PCH file,
244 which is a GC collection point. So we have to grab the first
245 token before allocating any memory. Pragmas must not be deferred
246 as -fpch-preprocess can generate a pragma to load the PCH file in
247 the preprocessed output used by -save-temps. */
248 cp_lexer_get_preprocessor_token (NULL, &first_token);
249
03fd3f84 250 /* Tell cpplib we want CPP_PRAGMA tokens. */
c162c75e
MA
251 cpp_get_options (parse_in)->defer_pragmas = true;
252
253 /* Tell c_lex not to merge string constants. */
254 c_lex_return_raw_strings = true;
255
18c81520 256 c_common_no_more_pch ();
a723baf1
MM
257
258 /* Allocate the memory. */
99dd239f 259 lexer = GGC_CNEW (cp_lexer);
a723baf1 260
c8094d83 261#ifdef ENABLE_CHECKING
c162c75e 262 /* Initially we are not debugging. */
a723baf1 263 lexer->debugging_p = false;
6983ea08 264#endif /* ENABLE_CHECKING */
d4e6fecb
NS
265 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
266 CP_SAVED_TOKEN_STACK);
c8094d83 267
76aebc9f
NS
268 /* Create the buffer. */
269 alloc = CP_LEXER_BUFFER_SIZE;
270 buffer = ggc_alloc (alloc * sizeof (cp_token));
a723baf1 271
76aebc9f
NS
272 /* Put the first token in the buffer. */
273 space = alloc;
274 pos = buffer;
275 *pos = first_token;
c8094d83 276
03fd3f84 277 /* Get the remaining tokens from the preprocessor. */
76aebc9f 278 while (pos->type != CPP_EOF)
c162c75e 279 {
76aebc9f
NS
280 pos++;
281 if (!--space)
282 {
283 space = alloc;
284 alloc *= 2;
285 buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
286 pos = buffer + space;
287 }
288 cp_lexer_get_preprocessor_token (lexer, pos);
c162c75e 289 }
76aebc9f
NS
290 lexer->buffer = buffer;
291 lexer->buffer_length = alloc - space;
292 lexer->last_token = pos;
293 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
c162c75e
MA
294
295 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
296 direct calls to c_lex. Those callers all expect c_lex to do
297 string constant concatenation. */
298 c_lex_return_raw_strings = false;
299
2cfe82fe 300 gcc_assert (lexer->next_token->type != CPP_PURGED);
a723baf1
MM
301 return lexer;
302}
303
c162c75e 304/* Create a new lexer whose token stream is primed with the tokens in
2cfe82fe 305 CACHE. When these tokens are exhausted, no new tokens will be read. */
a723baf1
MM
306
307static cp_lexer *
2cfe82fe 308cp_lexer_new_from_tokens (cp_token_cache *cache)
a723baf1 309{
2cfe82fe
ZW
310 cp_token *first = cache->first;
311 cp_token *last = cache->last;
c162c75e 312 cp_lexer *lexer = GGC_CNEW (cp_lexer);
17211ab5 313
0c5e4866 314 /* We do not own the buffer. */
76aebc9f
NS
315 lexer->buffer = NULL;
316 lexer->buffer_length = 0;
317 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
0c5e4866 318 lexer->last_token = last;
c8094d83 319
d4e6fecb
NS
320 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
321 CP_SAVED_TOKEN_STACK);
21526606 322
6983ea08 323#ifdef ENABLE_CHECKING
c162c75e 324 /* Initially we are not debugging. */
17211ab5 325 lexer->debugging_p = false;
c162c75e 326#endif
a723baf1 327
2cfe82fe
ZW
328 gcc_assert (lexer->next_token->type != CPP_PURGED);
329 return lexer;
c162c75e
MA
330}
331
03fd3f84 332/* Frees all resources associated with LEXER. */
c162c75e
MA
333
334static void
335cp_lexer_destroy (cp_lexer *lexer)
336{
0c5e4866
NS
337 if (lexer->buffer)
338 ggc_free (lexer->buffer);
d4e6fecb 339 VEC_free (cp_token_position, heap, lexer->saved_tokens);
c162c75e
MA
340 ggc_free (lexer);
341}
342
4de8668e 343/* Returns nonzero if debugging information should be output. */
a723baf1 344
6983ea08
MA
345#ifdef ENABLE_CHECKING
346
f7b5ecd9
MM
347static inline bool
348cp_lexer_debugging_p (cp_lexer *lexer)
a723baf1 349{
f7b5ecd9
MM
350 return lexer->debugging_p;
351}
352
6983ea08
MA
353#endif /* ENABLE_CHECKING */
354
0c5e4866
NS
355static inline cp_token_position
356cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
a723baf1 357{
0c5e4866 358 gcc_assert (!previous_p || lexer->next_token != &eof_token);
c8094d83 359
0c5e4866 360 return lexer->next_token - previous_p;
a723baf1
MM
361}
362
a668c6ad 363static inline cp_token *
0c5e4866 364cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
a668c6ad 365{
0c5e4866 366 return pos;
a668c6ad
MM
367}
368
4de8668e 369/* nonzero if we are presently saving tokens. */
f7b5ecd9 370
0c5e4866 371static inline int
94edc4ab 372cp_lexer_saving_tokens (const cp_lexer* lexer)
f7b5ecd9 373{
0c5e4866 374 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
a723baf1
MM
375}
376
76aebc9f
NS
377/* Store the next token from the preprocessor in *TOKEN. Return true
378 if we reach EOF. */
a723baf1 379
21526606 380static void
94edc4ab 381cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
0cbd7506 382 cp_token *token)
a723baf1 383{
7d381002 384 static int is_extern_c = 0;
a723baf1 385
51e63e60 386 /* Get a new token from the preprocessor. */
b68b6828
PB
387 token->type
388 = c_lex_with_flags (&token->value, &token->location, &token->flags);
c162c75e 389 token->in_system_header = in_system_header;
a723baf1 390
c8094d83 391 /* On some systems, some header files are surrounded by an
7d381002 392 implicit extern "C" block. Set a flag in the token if it
03fd3f84 393 comes from such a header. */
7d381002
MA
394 is_extern_c += pending_lang_change;
395 pending_lang_change = 0;
396 token->implicit_extern_c = is_extern_c > 0;
397
a723baf1 398 /* Check to see if this token is a keyword. */
21526606 399 if (token->type == CPP_NAME
a723baf1
MM
400 && C_IS_RESERVED_WORD (token->value))
401 {
402 /* Mark this token as a keyword. */
403 token->type = CPP_KEYWORD;
404 /* Record which keyword. */
405 token->keyword = C_RID_CODE (token->value);
406 /* Update the value. Some keywords are mapped to particular
407 entities, rather than simply having the value of the
408 corresponding IDENTIFIER_NODE. For example, `__const' is
409 mapped to `const'. */
410 token->value = ridpointers[token->keyword];
411 }
e58a9aa1
ZL
412 /* Handle Objective-C++ keywords. */
413 else if (token->type == CPP_AT_NAME)
414 {
415 token->type = CPP_KEYWORD;
416 switch (C_RID_CODE (token->value))
417 {
418 /* Map 'class' to '@class', 'private' to '@private', etc. */
419 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
420 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
421 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
422 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
423 case RID_THROW: token->keyword = RID_AT_THROW; break;
424 case RID_TRY: token->keyword = RID_AT_TRY; break;
425 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
426 default: token->keyword = C_RID_CODE (token->value);
427 }
428 }
a723baf1
MM
429 else
430 token->keyword = RID_MAX;
431}
432
03fd3f84 433/* Update the globals input_location and in_system_header from TOKEN. */
2cfe82fe
ZW
434static inline void
435cp_lexer_set_source_position_from_token (cp_token *token)
436{
437 if (token->type != CPP_EOF)
438 {
439 input_location = token->location;
440 in_system_header = token->in_system_header;
441 }
442}
443
a723baf1
MM
444/* Return a pointer to the next token in the token stream, but do not
445 consume it. */
446
c162c75e
MA
447static inline cp_token *
448cp_lexer_peek_token (cp_lexer *lexer)
a723baf1 449{
a723baf1 450 if (cp_lexer_debugging_p (lexer))
0c5e4866
NS
451 {
452 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
453 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
454 putc ('\n', cp_lexer_debug_stream);
455 }
2cfe82fe 456 return lexer->next_token;
a723baf1
MM
457}
458
459/* Return true if the next token has the indicated TYPE. */
460
2cfe82fe 461static inline bool
94edc4ab 462cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
a723baf1 463{
2cfe82fe 464 return cp_lexer_peek_token (lexer)->type == type;
a723baf1
MM
465}
466
467/* Return true if the next token does not have the indicated TYPE. */
468
2cfe82fe 469static inline bool
94edc4ab 470cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
471{
472 return !cp_lexer_next_token_is (lexer, type);
473}
474
475/* Return true if the next token is the indicated KEYWORD. */
476
2cfe82fe 477static inline bool
94edc4ab 478cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
a723baf1
MM
479{
480 cp_token *token;
481
482 /* Peek at the next token. */
483 token = cp_lexer_peek_token (lexer);
484 /* Check to see if it is the indicated keyword. */
485 return token->keyword == keyword;
486}
487
488/* Return a pointer to the Nth token in the token stream. If N is 1,
2cfe82fe
ZW
489 then this is precisely equivalent to cp_lexer_peek_token (except
490 that it is not inline). One would like to disallow that case, but
491 there is one case (cp_parser_nth_token_starts_template_id) where
492 the caller passes a variable for N and it might be 1. */
a723baf1
MM
493
494static cp_token *
94edc4ab 495cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
a723baf1
MM
496{
497 cp_token *token;
498
499 /* N is 1-based, not zero-based. */
863a3314
NS
500 gcc_assert (n > 0);
501
2cfe82fe
ZW
502 if (cp_lexer_debugging_p (lexer))
503 fprintf (cp_lexer_debug_stream,
504 "cp_lexer: peeking ahead %ld at token: ", (long)n);
505
c162c75e 506 --n;
a723baf1 507 token = lexer->next_token;
863a3314 508 gcc_assert (!n || token != &eof_token);
c162c75e 509 while (n != 0)
a723baf1 510 {
c162c75e 511 ++token;
0c5e4866
NS
512 if (token == lexer->last_token)
513 {
76aebc9f 514 token = (cp_token *)&eof_token;
0c5e4866
NS
515 break;
516 }
c8094d83 517
c162c75e
MA
518 if (token->type != CPP_PURGED)
519 --n;
a723baf1
MM
520 }
521
2cfe82fe
ZW
522 if (cp_lexer_debugging_p (lexer))
523 {
524 cp_lexer_print_token (cp_lexer_debug_stream, token);
525 putc ('\n', cp_lexer_debug_stream);
526 }
527
a723baf1
MM
528 return token;
529}
530
2cfe82fe
ZW
531/* Return the next token, and advance the lexer's next_token pointer
532 to point to the next non-purged token. */
a723baf1
MM
533
534static cp_token *
94edc4ab 535cp_lexer_consume_token (cp_lexer* lexer)
a723baf1 536{
2cfe82fe 537 cp_token *token = lexer->next_token;
a723baf1 538
0c5e4866 539 gcc_assert (token != &eof_token);
c8094d83 540
2cfe82fe 541 do
0c5e4866
NS
542 {
543 lexer->next_token++;
544 if (lexer->next_token == lexer->last_token)
545 {
76aebc9f 546 lexer->next_token = (cp_token *)&eof_token;
0c5e4866
NS
547 break;
548 }
c8094d83 549
0c5e4866 550 }
2cfe82fe 551 while (lexer->next_token->type == CPP_PURGED);
c8094d83 552
2cfe82fe 553 cp_lexer_set_source_position_from_token (token);
c8094d83 554
a723baf1
MM
555 /* Provide debugging output. */
556 if (cp_lexer_debugging_p (lexer))
557 {
2cfe82fe 558 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
a723baf1 559 cp_lexer_print_token (cp_lexer_debug_stream, token);
2cfe82fe 560 putc ('\n', cp_lexer_debug_stream);
a723baf1 561 }
c8094d83 562
a723baf1
MM
563 return token;
564}
565
2cfe82fe
ZW
566/* Permanently remove the next token from the token stream, and
567 advance the next_token pointer to refer to the next non-purged
568 token. */
a723baf1
MM
569
570static void
571cp_lexer_purge_token (cp_lexer *lexer)
572{
c162c75e 573 cp_token *tok = lexer->next_token;
c8094d83 574
0c5e4866 575 gcc_assert (tok != &eof_token);
c162c75e
MA
576 tok->type = CPP_PURGED;
577 tok->location = UNKNOWN_LOCATION;
578 tok->value = NULL_TREE;
579 tok->keyword = RID_MAX;
2cfe82fe
ZW
580
581 do
0c5e4866
NS
582 {
583 tok++;
584 if (tok == lexer->last_token)
585 {
76aebc9f 586 tok = (cp_token *)&eof_token;
0c5e4866
NS
587 break;
588 }
589 }
590 while (tok->type == CPP_PURGED);
591 lexer->next_token = tok;
a723baf1
MM
592}
593
c162c75e 594/* Permanently remove all tokens after TOK, up to, but not
a723baf1
MM
595 including, the token that will be returned next by
596 cp_lexer_peek_token. */
597
598static void
c162c75e 599cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
a723baf1 600{
0c5e4866 601 cp_token *peek = lexer->next_token;
a723baf1 602
0c5e4866
NS
603 if (peek == &eof_token)
604 peek = lexer->last_token;
c8094d83 605
c162c75e
MA
606 gcc_assert (tok < peek);
607
608 for ( tok += 1; tok != peek; tok += 1)
a723baf1 609 {
c162c75e
MA
610 tok->type = CPP_PURGED;
611 tok->location = UNKNOWN_LOCATION;
612 tok->value = NULL_TREE;
613 tok->keyword = RID_MAX;
a723baf1 614 }
c162c75e
MA
615}
616
03fd3f84 617/* Consume and handle a pragma token. */
c162c75e
MA
618static void
619cp_lexer_handle_pragma (cp_lexer *lexer)
620{
36952dea
ZW
621 cpp_string s;
622 cp_token *token = cp_lexer_consume_token (lexer);
623 gcc_assert (token->type == CPP_PRAGMA);
624 gcc_assert (token->value);
c162c75e 625
36952dea
ZW
626 s.len = TREE_STRING_LENGTH (token->value);
627 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
c162c75e 628
36952dea 629 cpp_handle_deferred_pragma (parse_in, &s);
c162c75e 630
36952dea
ZW
631 /* Clearing token->value here means that we will get an ICE if we
632 try to process this #pragma again (which should be impossible). */
633 token->value = NULL;
a723baf1
MM
634}
635
636/* Begin saving tokens. All tokens consumed after this point will be
637 preserved. */
638
639static void
94edc4ab 640cp_lexer_save_tokens (cp_lexer* lexer)
a723baf1
MM
641{
642 /* Provide debugging output. */
643 if (cp_lexer_debugging_p (lexer))
644 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
645
d4e6fecb
NS
646 VEC_safe_push (cp_token_position, heap,
647 lexer->saved_tokens, lexer->next_token);
a723baf1
MM
648}
649
650/* Commit to the portion of the token stream most recently saved. */
651
652static void
94edc4ab 653cp_lexer_commit_tokens (cp_lexer* lexer)
a723baf1
MM
654{
655 /* Provide debugging output. */
656 if (cp_lexer_debugging_p (lexer))
657 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
658
0c5e4866 659 VEC_pop (cp_token_position, lexer->saved_tokens);
a723baf1
MM
660}
661
662/* Return all tokens saved since the last call to cp_lexer_save_tokens
663 to the token stream. Stop saving tokens. */
664
665static void
94edc4ab 666cp_lexer_rollback_tokens (cp_lexer* lexer)
a723baf1 667{
a723baf1
MM
668 /* Provide debugging output. */
669 if (cp_lexer_debugging_p (lexer))
670 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
671
0c5e4866 672 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
a723baf1
MM
673}
674
a723baf1
MM
675/* Print a representation of the TOKEN on the STREAM. */
676
6983ea08
MA
677#ifdef ENABLE_CHECKING
678
a723baf1 679static void
c162c75e
MA
680cp_lexer_print_token (FILE * stream, cp_token *token)
681{
682 /* We don't use cpp_type2name here because the parser defines
683 a few tokens of its own. */
684 static const char *const token_names[] = {
685 /* cpplib-defined token types */
686#define OP(e, s) #e,
687#define TK(e, s) #e,
688 TTYPE_TABLE
689#undef OP
690#undef TK
691 /* C++ parser token types - see "Manifest constants", above. */
692 "KEYWORD",
693 "TEMPLATE_ID",
694 "NESTED_NAME_SPECIFIER",
695 "PURGED"
696 };
c8094d83 697
c162c75e
MA
698 /* If we have a name for the token, print it out. Otherwise, we
699 simply give the numeric code. */
700 gcc_assert (token->type < ARRAY_SIZE(token_names));
701 fputs (token_names[token->type], stream);
a723baf1 702
c162c75e 703 /* For some tokens, print the associated data. */
a723baf1
MM
704 switch (token->type)
705 {
c162c75e
MA
706 case CPP_KEYWORD:
707 /* Some keywords have a value that is not an IDENTIFIER_NODE.
708 For example, `struct' is mapped to an INTEGER_CST. */
709 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
710 break;
711 /* else fall through */
a723baf1 712 case CPP_NAME:
c162c75e 713 fputs (IDENTIFIER_POINTER (token->value), stream);
a723baf1
MM
714 break;
715
c162c75e
MA
716 case CPP_STRING:
717 case CPP_WSTRING:
718 case CPP_PRAGMA:
719 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
a723baf1
MM
720 break;
721
a723baf1
MM
722 default:
723 break;
724 }
a723baf1
MM
725}
726
a723baf1
MM
727/* Start emitting debugging information. */
728
729static void
94edc4ab 730cp_lexer_start_debugging (cp_lexer* lexer)
a723baf1 731{
81122c44 732 lexer->debugging_p = true;
a723baf1 733}
21526606 734
a723baf1
MM
735/* Stop emitting debugging information. */
736
737static void
94edc4ab 738cp_lexer_stop_debugging (cp_lexer* lexer)
a723baf1 739{
81122c44 740 lexer->debugging_p = false;
a723baf1
MM
741}
742
6983ea08
MA
743#endif /* ENABLE_CHECKING */
744
03fd3f84 745/* Create a new cp_token_cache, representing a range of tokens. */
c162c75e
MA
746
747static cp_token_cache *
748cp_token_cache_new (cp_token *first, cp_token *last)
749{
750 cp_token_cache *cache = GGC_NEW (cp_token_cache);
751 cache->first = first;
752 cache->last = last;
753 return cache;
754}
755
a723baf1 756\f
62d1db17
MM
757/* Decl-specifiers. */
758
759static void clear_decl_specs
760 (cp_decl_specifier_seq *);
761
762/* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
763
764static void
765clear_decl_specs (cp_decl_specifier_seq *decl_specs)
766{
767 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
768}
769
058b15c1
MM
770/* Declarators. */
771
772/* Nothing other than the parser should be creating declarators;
773 declarators are a semi-syntactic representation of C++ entities.
774 Other parts of the front end that need to create entities (like
775 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
776
98ca843c 777static cp_declarator *make_call_declarator
3c01e5df 778 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
98ca843c 779static cp_declarator *make_array_declarator
058b15c1 780 (cp_declarator *, tree);
98ca843c 781static cp_declarator *make_pointer_declarator
3c01e5df 782 (cp_cv_quals, cp_declarator *);
98ca843c 783static cp_declarator *make_reference_declarator
3c01e5df 784 (cp_cv_quals, cp_declarator *);
98ca843c 785static cp_parameter_declarator *make_parameter_declarator
62d1db17 786 (cp_decl_specifier_seq *, cp_declarator *, tree);
98ca843c 787static cp_declarator *make_ptrmem_declarator
3c01e5df 788 (cp_cv_quals, tree, cp_declarator *);
058b15c1
MM
789
790cp_declarator *cp_error_declarator;
791
792/* The obstack on which declarators and related data structures are
793 allocated. */
794static struct obstack declarator_obstack;
795
796/* Alloc BYTES from the declarator memory pool. */
797
798static inline void *
799alloc_declarator (size_t bytes)
800{
801 return obstack_alloc (&declarator_obstack, bytes);
802}
803
804/* Allocate a declarator of the indicated KIND. Clear fields that are
805 common to all declarators. */
806
807static cp_declarator *
808make_declarator (cp_declarator_kind kind)
809{
810 cp_declarator *declarator;
811
812 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
813 declarator->kind = kind;
814 declarator->attributes = NULL_TREE;
815 declarator->declarator = NULL;
816
817 return declarator;
818}
819
1d786913
MM
820/* Make a declarator for a generalized identifier. If non-NULL, the
821 identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
822 just UNQUALIFIED_NAME. */
058b15c1 823
1d786913
MM
824static cp_declarator *
825make_id_declarator (tree qualifying_scope, tree unqualified_name)
058b15c1
MM
826{
827 cp_declarator *declarator;
98ca843c 828
1d786913
MM
829 /* It is valid to write:
830
831 class C { void f(); };
832 typedef C D;
833 void D::f();
834
835 The standard is not clear about whether `typedef const C D' is
836 legal; as of 2002-09-15 the committee is considering that
837 question. EDG 3.0 allows that syntax. Therefore, we do as
838 well. */
839 if (qualifying_scope && TYPE_P (qualifying_scope))
840 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
841
058b15c1 842 declarator = make_declarator (cdk_id);
1d786913
MM
843 declarator->u.id.qualifying_scope = qualifying_scope;
844 declarator->u.id.unqualified_name = unqualified_name;
058b15c1
MM
845 declarator->u.id.sfk = sfk_none;
846
847 return declarator;
848}
849
850/* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
851 of modifiers such as const or volatile to apply to the pointer
852 type, represented as identifiers. */
853
854cp_declarator *
3c01e5df 855make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
058b15c1
MM
856{
857 cp_declarator *declarator;
858
859 declarator = make_declarator (cdk_pointer);
860 declarator->declarator = target;
861 declarator->u.pointer.qualifiers = cv_qualifiers;
862 declarator->u.pointer.class_type = NULL_TREE;
863
864 return declarator;
865}
866
867/* Like make_pointer_declarator -- but for references. */
868
869cp_declarator *
3c01e5df 870make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
058b15c1
MM
871{
872 cp_declarator *declarator;
873
874 declarator = make_declarator (cdk_reference);
875 declarator->declarator = target;
876 declarator->u.pointer.qualifiers = cv_qualifiers;
877 declarator->u.pointer.class_type = NULL_TREE;
878
879 return declarator;
880}
881
882/* Like make_pointer_declarator -- but for a pointer to a non-static
883 member of CLASS_TYPE. */
884
885cp_declarator *
3c01e5df 886make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
058b15c1
MM
887 cp_declarator *pointee)
888{
889 cp_declarator *declarator;
890
891 declarator = make_declarator (cdk_ptrmem);
892 declarator->declarator = pointee;
893 declarator->u.pointer.qualifiers = cv_qualifiers;
894 declarator->u.pointer.class_type = class_type;
895
896 return declarator;
897}
898
899/* Make a declarator for the function given by TARGET, with the
900 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
901 "const"-qualified member function. The EXCEPTION_SPECIFICATION
902 indicates what exceptions can be thrown. */
903
904cp_declarator *
98ca843c 905make_call_declarator (cp_declarator *target,
058b15c1 906 cp_parameter_declarator *parms,
3c01e5df 907 cp_cv_quals cv_qualifiers,
0cbd7506 908 tree exception_specification)
058b15c1
MM
909{
910 cp_declarator *declarator;
911
912 declarator = make_declarator (cdk_function);
913 declarator->declarator = target;
914 declarator->u.function.parameters = parms;
915 declarator->u.function.qualifiers = cv_qualifiers;
916 declarator->u.function.exception_specification = exception_specification;
917
918 return declarator;
919}
920
921/* Make a declarator for an array of BOUNDS elements, each of which is
922 defined by ELEMENT. */
923
924cp_declarator *
925make_array_declarator (cp_declarator *element, tree bounds)
926{
927 cp_declarator *declarator;
928
929 declarator = make_declarator (cdk_array);
930 declarator->declarator = element;
931 declarator->u.array.bounds = bounds;
932
933 return declarator;
934}
935
936cp_parameter_declarator *no_parameters;
937
938/* Create a parameter declarator with the indicated DECL_SPECIFIERS,
939 DECLARATOR and DEFAULT_ARGUMENT. */
940
941cp_parameter_declarator *
98ca843c 942make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
058b15c1
MM
943 cp_declarator *declarator,
944 tree default_argument)
945{
946 cp_parameter_declarator *parameter;
947
98ca843c 948 parameter = ((cp_parameter_declarator *)
058b15c1
MM
949 alloc_declarator (sizeof (cp_parameter_declarator)));
950 parameter->next = NULL;
62d1db17
MM
951 if (decl_specifiers)
952 parameter->decl_specifiers = *decl_specifiers;
953 else
954 clear_decl_specs (&parameter->decl_specifiers);
058b15c1
MM
955 parameter->declarator = declarator;
956 parameter->default_argument = default_argument;
957 parameter->ellipsis_p = false;
958
959 return parameter;
960}
961
a723baf1
MM
962/* The parser. */
963
964/* Overview
965 --------
966
967 A cp_parser parses the token stream as specified by the C++
968 grammar. Its job is purely parsing, not semantic analysis. For
969 example, the parser breaks the token stream into declarators,
970 expressions, statements, and other similar syntactic constructs.
971 It does not check that the types of the expressions on either side
972 of an assignment-statement are compatible, or that a function is
973 not declared with a parameter of type `void'.
974
975 The parser invokes routines elsewhere in the compiler to perform
976 semantic analysis and to build up the abstract syntax tree for the
21526606 977 code processed.
a723baf1
MM
978
979 The parser (and the template instantiation code, which is, in a
980 way, a close relative of parsing) are the only parts of the
981 compiler that should be calling push_scope and pop_scope, or
982 related functions. The parser (and template instantiation code)
983 keeps track of what scope is presently active; everything else
984 should simply honor that. (The code that generates static
985 initializers may also need to set the scope, in order to check
986 access control correctly when emitting the initializers.)
987
988 Methodology
989 -----------
21526606 990
a723baf1
MM
991 The parser is of the standard recursive-descent variety. Upcoming
992 tokens in the token stream are examined in order to determine which
993 production to use when parsing a non-terminal. Some C++ constructs
994 require arbitrary look ahead to disambiguate. For example, it is
995 impossible, in the general case, to tell whether a statement is an
996 expression or declaration without scanning the entire statement.
997 Therefore, the parser is capable of "parsing tentatively." When the
998 parser is not sure what construct comes next, it enters this mode.
999 Then, while we attempt to parse the construct, the parser queues up
1000 error messages, rather than issuing them immediately, and saves the
1001 tokens it consumes. If the construct is parsed successfully, the
1002 parser "commits", i.e., it issues any queued error messages and
1003 the tokens that were being preserved are permanently discarded.
1004 If, however, the construct is not parsed successfully, the parser
1005 rolls back its state completely so that it can resume parsing using
1006 a different alternative.
1007
1008 Future Improvements
1009 -------------------
21526606 1010
b8b94c5b
PB
1011 The performance of the parser could probably be improved substantially.
1012 We could often eliminate the need to parse tentatively by looking ahead
1013 a little bit. In some places, this approach might not entirely eliminate
1014 the need to parse tentatively, but it might still speed up the average
1015 case. */
a723baf1
MM
1016
1017/* Flags that are passed to some parsing functions. These values can
1018 be bitwise-ored together. */
1019
1020typedef enum cp_parser_flags
1021{
1022 /* No flags. */
1023 CP_PARSER_FLAGS_NONE = 0x0,
1024 /* The construct is optional. If it is not present, then no error
1025 should be issued. */
1026 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1027 /* When parsing a type-specifier, do not allow user-defined types. */
1028 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1029} cp_parser_flags;
1030
62b8a44e
NS
1031/* The different kinds of declarators we want to parse. */
1032
1033typedef enum cp_parser_declarator_kind
1034{
852dcbdd 1035 /* We want an abstract declarator. */
62b8a44e
NS
1036 CP_PARSER_DECLARATOR_ABSTRACT,
1037 /* We want a named declarator. */
1038 CP_PARSER_DECLARATOR_NAMED,
04c06002 1039 /* We don't mind, but the name must be an unqualified-id. */
62b8a44e
NS
1040 CP_PARSER_DECLARATOR_EITHER
1041} cp_parser_declarator_kind;
1042
b8b94c5b
PB
1043/* The precedence values used to parse binary expressions. The minimum value
1044 of PREC must be 1, because zero is reserved to quickly discriminate
1045 binary operators from other tokens. */
a723baf1 1046
b8b94c5b 1047enum cp_parser_prec
a723baf1 1048{
b8b94c5b
PB
1049 PREC_NOT_OPERATOR,
1050 PREC_LOGICAL_OR_EXPRESSION,
1051 PREC_LOGICAL_AND_EXPRESSION,
1052 PREC_INCLUSIVE_OR_EXPRESSION,
1053 PREC_EXCLUSIVE_OR_EXPRESSION,
1054 PREC_AND_EXPRESSION,
b8b94c5b 1055 PREC_EQUALITY_EXPRESSION,
69475123 1056 PREC_RELATIONAL_EXPRESSION,
b8b94c5b
PB
1057 PREC_SHIFT_EXPRESSION,
1058 PREC_ADDITIVE_EXPRESSION,
1059 PREC_MULTIPLICATIVE_EXPRESSION,
1060 PREC_PM_EXPRESSION,
1061 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1062};
a723baf1 1063
b8b94c5b
PB
1064/* A mapping from a token type to a corresponding tree node type, with a
1065 precedence value. */
a723baf1 1066
b8b94c5b
PB
1067typedef struct cp_parser_binary_operations_map_node
1068{
1069 /* The token type. */
1070 enum cpp_ttype token_type;
1071 /* The corresponding tree code. */
1072 enum tree_code tree_type;
1073 /* The precedence of this operator. */
1074 enum cp_parser_prec prec;
1075} cp_parser_binary_operations_map_node;
a723baf1
MM
1076
1077/* The status of a tentative parse. */
1078
1079typedef enum cp_parser_status_kind
1080{
1081 /* No errors have occurred. */
1082 CP_PARSER_STATUS_KIND_NO_ERROR,
1083 /* An error has occurred. */
1084 CP_PARSER_STATUS_KIND_ERROR,
1085 /* We are committed to this tentative parse, whether or not an error
1086 has occurred. */
1087 CP_PARSER_STATUS_KIND_COMMITTED
1088} cp_parser_status_kind;
1089
b8b94c5b
PB
1090typedef struct cp_parser_expression_stack_entry
1091{
1092 tree lhs;
1093 enum tree_code tree_type;
1094 int prec;
1095} cp_parser_expression_stack_entry;
1096
43c2a69a
PB
1097/* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1098 entries because precedence levels on the stack are monotonically
1099 increasing. */
b8b94c5b
PB
1100typedef struct cp_parser_expression_stack_entry
1101 cp_parser_expression_stack[NUM_PREC_VALUES];
a723baf1 1102
b8b94c5b 1103/* Context that is saved and restored when parsing tentatively. */
a723baf1
MM
1104typedef struct cp_parser_context GTY (())
1105{
1106 /* If this is a tentative parsing context, the status of the
1107 tentative parse. */
1108 enum cp_parser_status_kind status;
1109 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1110 that are looked up in this context must be looked up both in the
1111 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1112 the context of the containing expression. */
1113 tree object_type;
b8b94c5b 1114
a723baf1
MM
1115 /* The next parsing context in the stack. */
1116 struct cp_parser_context *next;
1117} cp_parser_context;
1118
1119/* Prototypes. */
1120
1121/* Constructors and destructors. */
1122
1123static cp_parser_context *cp_parser_context_new
94edc4ab 1124 (cp_parser_context *);
a723baf1 1125
e5976695
MM
1126/* Class variables. */
1127
1431042e 1128static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
e5976695 1129
b8b94c5b
PB
1130/* The operator-precedence table used by cp_parser_binary_expression.
1131 Transformed into an associative array (binops_by_token) by
1132 cp_parser_new. */
1133
1134static const cp_parser_binary_operations_map_node binops[] = {
1135 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1136 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1137
1138 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1139 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1140 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1141
1142 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1143 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1144
1145 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1146 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1147
1148 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1149 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1150 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1151 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1152 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1153 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1154
1155 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1156 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1157
1158 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1159
1160 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1161
1162 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1163
1164 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1165
1166 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1167};
1168
1169/* The same as binops, but initialized by cp_parser_new so that
1170 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1171 for speed. */
1172static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1173
a723baf1
MM
1174/* Constructors and destructors. */
1175
1176/* Construct a new context. The context below this one on the stack
1177 is given by NEXT. */
1178
1179static cp_parser_context *
94edc4ab 1180cp_parser_context_new (cp_parser_context* next)
a723baf1
MM
1181{
1182 cp_parser_context *context;
1183
1184 /* Allocate the storage. */
e5976695
MM
1185 if (cp_parser_context_free_list != NULL)
1186 {
1187 /* Pull the first entry from the free list. */
1188 context = cp_parser_context_free_list;
1189 cp_parser_context_free_list = context->next;
c68b0a84 1190 memset (context, 0, sizeof (*context));
e5976695
MM
1191 }
1192 else
99dd239f 1193 context = GGC_CNEW (cp_parser_context);
b8b94c5b 1194
a723baf1
MM
1195 /* No errors have occurred yet in this context. */
1196 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1197 /* If this is not the bottomost context, copy information that we
1198 need from the previous context. */
1199 if (next)
1200 {
1201 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1202 expression, then we are parsing one in this context, too. */
1203 context->object_type = next->object_type;
a723baf1
MM
1204 /* Thread the stack. */
1205 context->next = next;
1206 }
1207
1208 return context;
1209}
1210
1211/* The cp_parser structure represents the C++ parser. */
1212
1213typedef struct cp_parser GTY(())
1214{
1215 /* The lexer from which we are obtaining tokens. */
1216 cp_lexer *lexer;
1217
1218 /* The scope in which names should be looked up. If NULL_TREE, then
1219 we look up names in the scope that is currently open in the
1220 source program. If non-NULL, this is either a TYPE or
8fe4d24b
NS
1221 NAMESPACE_DECL for the scope in which we should look. It can
1222 also be ERROR_MARK, when we've parsed a bogus scope.
a723baf1
MM
1223
1224 This value is not cleared automatically after a name is looked
1225 up, so we must be careful to clear it before starting a new look
1226 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1227 will look up `Z' in the scope of `X', rather than the current
1228 scope.) Unfortunately, it is difficult to tell when name lookup
1229 is complete, because we sometimes peek at a token, look it up,
8fe4d24b 1230 and then decide not to consume it. */
a723baf1
MM
1231 tree scope;
1232
1233 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1234 last lookup took place. OBJECT_SCOPE is used if an expression
1235 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
21526606 1236 respectively. QUALIFYING_SCOPE is used for an expression of the
a723baf1
MM
1237 form "X::Y"; it refers to X. */
1238 tree object_scope;
1239 tree qualifying_scope;
1240
1241 /* A stack of parsing contexts. All but the bottom entry on the
1242 stack will be tentative contexts.
1243
1244 We parse tentatively in order to determine which construct is in
1245 use in some situations. For example, in order to determine
1246 whether a statement is an expression-statement or a
1247 declaration-statement we parse it tentatively as a
1248 declaration-statement. If that fails, we then reparse the same
1249 token stream as an expression-statement. */
1250 cp_parser_context *context;
1251
1252 /* True if we are parsing GNU C++. If this flag is not set, then
1253 GNU extensions are not recognized. */
1254 bool allow_gnu_extensions_p;
1255
1256 /* TRUE if the `>' token should be interpreted as the greater-than
1257 operator. FALSE if it is the end of a template-id or
1258 template-parameter-list. */
1259 bool greater_than_is_operator_p;
1260
1261 /* TRUE if default arguments are allowed within a parameter list
1262 that starts at this point. FALSE if only a gnu extension makes
cd0be382 1263 them permissible. */
a723baf1 1264 bool default_arg_ok_p;
21526606 1265
a723baf1
MM
1266 /* TRUE if we are parsing an integral constant-expression. See
1267 [expr.const] for a precise definition. */
67c03833 1268 bool integral_constant_expression_p;
a723baf1 1269
14d22dd6
MM
1270 /* TRUE if we are parsing an integral constant-expression -- but a
1271 non-constant expression should be permitted as well. This flag
1272 is used when parsing an array bound so that GNU variable-length
1273 arrays are tolerated. */
67c03833 1274 bool allow_non_integral_constant_expression_p;
14d22dd6
MM
1275
1276 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1277 been seen that makes the expression non-constant. */
67c03833 1278 bool non_integral_constant_expression_p;
14d22dd6 1279
a723baf1
MM
1280 /* TRUE if local variable names and `this' are forbidden in the
1281 current context. */
1282 bool local_variables_forbidden_p;
1283
1284 /* TRUE if the declaration we are parsing is part of a
1285 linkage-specification of the form `extern string-literal
1286 declaration'. */
1287 bool in_unbraced_linkage_specification_p;
1288
1289 /* TRUE if we are presently parsing a declarator, after the
1290 direct-declarator. */
1291 bool in_declarator_p;
1292
4bb8ca28
MM
1293 /* TRUE if we are presently parsing a template-argument-list. */
1294 bool in_template_argument_list_p;
1295
0e59b3fb
MM
1296 /* TRUE if we are presently parsing the body of an
1297 iteration-statement. */
1298 bool in_iteration_statement_p;
1299
1300 /* TRUE if we are presently parsing the body of a switch
1301 statement. */
1302 bool in_switch_statement_p;
1303
4f8163b1
MM
1304 /* TRUE if we are parsing a type-id in an expression context. In
1305 such a situation, both "type (expr)" and "type (type)" are valid
1306 alternatives. */
1307 bool in_type_id_in_expr_p;
1308
7d381002 1309 /* TRUE if we are currently in a header file where declarations are
03fd3f84 1310 implicitly extern "C". */
7d381002
MA
1311 bool implicit_extern_c;
1312
c162c75e
MA
1313 /* TRUE if strings in expressions should be translated to the execution
1314 character set. */
1315 bool translate_strings_p;
1316
a723baf1
MM
1317 /* If non-NULL, then we are parsing a construct where new type
1318 definitions are not permitted. The string stored here will be
1319 issued as an error message if a type is defined. */
1320 const char *type_definition_forbidden_message;
1321
8db1028e
NS
1322 /* A list of lists. The outer list is a stack, used for member
1323 functions of local classes. At each level there are two sub-list,
1324 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1325 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1326 TREE_VALUE's. The functions are chained in reverse declaration
1327 order.
1328
1329 The TREE_PURPOSE sublist contains those functions with default
1330 arguments that need post processing, and the TREE_VALUE sublist
1331 contains those functions with definitions that need post
1332 processing.
1333
1334 These lists can only be processed once the outermost class being
9bcb9aae 1335 defined is complete. */
a723baf1
MM
1336 tree unparsed_functions_queues;
1337
1338 /* The number of classes whose definitions are currently in
1339 progress. */
1340 unsigned num_classes_being_defined;
1341
1342 /* The number of template parameter lists that apply directly to the
1343 current declaration. */
1344 unsigned num_template_parameter_lists;
1345} cp_parser;
1346
04c06002 1347/* The type of a function that parses some kind of expression. */
94edc4ab 1348typedef tree (*cp_parser_expression_fn) (cp_parser *);
a723baf1
MM
1349
1350/* Prototypes. */
1351
1352/* Constructors and destructors. */
1353
1354static cp_parser *cp_parser_new
94edc4ab 1355 (void);
a723baf1 1356
21526606 1357/* Routines to parse various constructs.
a723baf1
MM
1358
1359 Those that return `tree' will return the error_mark_node (rather
1360 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1361 Sometimes, they will return an ordinary node if error-recovery was
34cd5ae7 1362 attempted, even though a parse error occurred. So, to check
a723baf1
MM
1363 whether or not a parse error occurred, you should always use
1364 cp_parser_error_occurred. If the construct is optional (indicated
1365 either by an `_opt' in the name of the function that does the
1366 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1367 the construct is not present. */
1368
1369/* Lexical conventions [gram.lex] */
1370
1371static tree cp_parser_identifier
94edc4ab 1372 (cp_parser *);
c162c75e
MA
1373static tree cp_parser_string_literal
1374 (cp_parser *, bool, bool);
a723baf1
MM
1375
1376/* Basic concepts [gram.basic] */
1377
1378static bool cp_parser_translation_unit
94edc4ab 1379 (cp_parser *);
a723baf1
MM
1380
1381/* Expressions [gram.expr] */
1382
1383static tree cp_parser_primary_expression
02ed62dd 1384 (cp_parser *, bool, bool, bool, cp_id_kind *);
a723baf1 1385static tree cp_parser_id_expression
f3c2dfc6 1386 (cp_parser *, bool, bool, bool *, bool);
a723baf1 1387static tree cp_parser_unqualified_id
f3c2dfc6 1388 (cp_parser *, bool, bool, bool);
a723baf1 1389static tree cp_parser_nested_name_specifier_opt
a668c6ad 1390 (cp_parser *, bool, bool, bool, bool);
a723baf1 1391static tree cp_parser_nested_name_specifier
a723baf1 1392 (cp_parser *, bool, bool, bool, bool);
a668c6ad
MM
1393static tree cp_parser_class_or_namespace_name
1394 (cp_parser *, bool, bool, bool, bool, bool);
a723baf1 1395static tree cp_parser_postfix_expression
93678513 1396 (cp_parser *, bool, bool);
7a3ea201
RH
1397static tree cp_parser_postfix_open_square_expression
1398 (cp_parser *, tree, bool);
1399static tree cp_parser_postfix_dot_deref_expression
1400 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
7efa3e22 1401static tree cp_parser_parenthesized_expression_list
93678513 1402 (cp_parser *, bool, bool, bool *);
a723baf1 1403static void cp_parser_pseudo_destructor_name
94edc4ab 1404 (cp_parser *, tree *, tree *);
a723baf1 1405static tree cp_parser_unary_expression
93678513 1406 (cp_parser *, bool, bool);
a723baf1 1407static enum tree_code cp_parser_unary_operator
94edc4ab 1408 (cp_token *);
a723baf1 1409static tree cp_parser_new_expression
94edc4ab 1410 (cp_parser *);
a723baf1 1411static tree cp_parser_new_placement
94edc4ab 1412 (cp_parser *);
a723baf1 1413static tree cp_parser_new_type_id
058b15c1
MM
1414 (cp_parser *, tree *);
1415static cp_declarator *cp_parser_new_declarator_opt
94edc4ab 1416 (cp_parser *);
058b15c1 1417static cp_declarator *cp_parser_direct_new_declarator
94edc4ab 1418 (cp_parser *);
a723baf1 1419static tree cp_parser_new_initializer
94edc4ab 1420 (cp_parser *);
a723baf1 1421static tree cp_parser_delete_expression
94edc4ab 1422 (cp_parser *);
21526606 1423static tree cp_parser_cast_expression
93678513 1424 (cp_parser *, bool, bool);
b8b94c5b 1425static tree cp_parser_binary_expression
93678513 1426 (cp_parser *, bool);
a723baf1 1427static tree cp_parser_question_colon_clause
94edc4ab 1428 (cp_parser *, tree);
a723baf1 1429static tree cp_parser_assignment_expression
93678513 1430 (cp_parser *, bool);
a723baf1 1431static enum tree_code cp_parser_assignment_operator_opt
94edc4ab 1432 (cp_parser *);
a723baf1 1433static tree cp_parser_expression
93678513 1434 (cp_parser *, bool);
a723baf1 1435static tree cp_parser_constant_expression
14d22dd6 1436 (cp_parser *, bool, bool *);
7a3ea201
RH
1437static tree cp_parser_builtin_offsetof
1438 (cp_parser *);
a723baf1
MM
1439
1440/* Statements [gram.stmt.stmt] */
1441
1442static void cp_parser_statement
325c3691 1443 (cp_parser *, tree);
a723baf1 1444static tree cp_parser_labeled_statement
325c3691 1445 (cp_parser *, tree);
a723baf1 1446static tree cp_parser_expression_statement
325c3691 1447 (cp_parser *, tree);
a723baf1 1448static tree cp_parser_compound_statement
325c3691 1449 (cp_parser *, tree, bool);
a723baf1 1450static void cp_parser_statement_seq_opt
325c3691 1451 (cp_parser *, tree);
a723baf1 1452static tree cp_parser_selection_statement
94edc4ab 1453 (cp_parser *);
a723baf1 1454static tree cp_parser_condition
94edc4ab 1455 (cp_parser *);
a723baf1 1456static tree cp_parser_iteration_statement
94edc4ab 1457 (cp_parser *);
a723baf1 1458static void cp_parser_for_init_statement
94edc4ab 1459 (cp_parser *);
a723baf1 1460static tree cp_parser_jump_statement
94edc4ab 1461 (cp_parser *);
a723baf1 1462static void cp_parser_declaration_statement
94edc4ab 1463 (cp_parser *);
a723baf1
MM
1464
1465static tree cp_parser_implicitly_scoped_statement
94edc4ab 1466 (cp_parser *);
a723baf1 1467static void cp_parser_already_scoped_statement
94edc4ab 1468 (cp_parser *);
a723baf1
MM
1469
1470/* Declarations [gram.dcl.dcl] */
1471
1472static void cp_parser_declaration_seq_opt
94edc4ab 1473 (cp_parser *);
a723baf1 1474static void cp_parser_declaration
94edc4ab 1475 (cp_parser *);
a723baf1 1476static void cp_parser_block_declaration
94edc4ab 1477 (cp_parser *, bool);
a723baf1 1478static void cp_parser_simple_declaration
94edc4ab 1479 (cp_parser *, bool);
62d1db17
MM
1480static void cp_parser_decl_specifier_seq
1481 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
a723baf1 1482static tree cp_parser_storage_class_specifier_opt
94edc4ab 1483 (cp_parser *);
a723baf1 1484static tree cp_parser_function_specifier_opt
62d1db17 1485 (cp_parser *, cp_decl_specifier_seq *);
a723baf1 1486static tree cp_parser_type_specifier
98ca843c 1487 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
62d1db17 1488 int *, bool *);
a723baf1 1489static tree cp_parser_simple_type_specifier
62d1db17 1490 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
a723baf1 1491static tree cp_parser_type_name
94edc4ab 1492 (cp_parser *);
a723baf1 1493static tree cp_parser_elaborated_type_specifier
94edc4ab 1494 (cp_parser *, bool, bool);
a723baf1 1495static tree cp_parser_enum_specifier
94edc4ab 1496 (cp_parser *);
a723baf1 1497static void cp_parser_enumerator_list
94edc4ab 1498 (cp_parser *, tree);
21526606 1499static void cp_parser_enumerator_definition
94edc4ab 1500 (cp_parser *, tree);
a723baf1 1501static tree cp_parser_namespace_name
94edc4ab 1502 (cp_parser *);
a723baf1 1503static void cp_parser_namespace_definition
94edc4ab 1504 (cp_parser *);
a723baf1 1505static void cp_parser_namespace_body
94edc4ab 1506 (cp_parser *);
a723baf1 1507static tree cp_parser_qualified_namespace_specifier
94edc4ab 1508 (cp_parser *);
a723baf1 1509static void cp_parser_namespace_alias_definition
94edc4ab 1510 (cp_parser *);
a723baf1 1511static void cp_parser_using_declaration
94edc4ab 1512 (cp_parser *);
a723baf1 1513static void cp_parser_using_directive
94edc4ab 1514 (cp_parser *);
a723baf1 1515static void cp_parser_asm_definition
94edc4ab 1516 (cp_parser *);
a723baf1 1517static void cp_parser_linkage_specification
94edc4ab 1518 (cp_parser *);
a723baf1
MM
1519
1520/* Declarators [gram.dcl.decl] */
1521
1522static tree cp_parser_init_declarator
62d1db17 1523 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
058b15c1 1524static cp_declarator *cp_parser_declarator
db86dd14 1525 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
058b15c1 1526static cp_declarator *cp_parser_direct_declarator
db86dd14 1527 (cp_parser *, cp_parser_declarator_kind, int *, bool);
a723baf1 1528static enum tree_code cp_parser_ptr_operator
3c01e5df
MM
1529 (cp_parser *, tree *, cp_cv_quals *);
1530static cp_cv_quals cp_parser_cv_qualifier_seq_opt
94edc4ab 1531 (cp_parser *);
a723baf1 1532static tree cp_parser_declarator_id
94edc4ab 1533 (cp_parser *);
a723baf1 1534static tree cp_parser_type_id
94edc4ab 1535 (cp_parser *);
62d1db17 1536static void cp_parser_type_specifier_seq
d4113656 1537 (cp_parser *, bool, cp_decl_specifier_seq *);
058b15c1 1538static cp_parameter_declarator *cp_parser_parameter_declaration_clause
94edc4ab 1539 (cp_parser *);
058b15c1
MM
1540static cp_parameter_declarator *cp_parser_parameter_declaration_list
1541 (cp_parser *, bool *);
1542static cp_parameter_declarator *cp_parser_parameter_declaration
4bb8ca28 1543 (cp_parser *, bool, bool *);
a723baf1
MM
1544static void cp_parser_function_body
1545 (cp_parser *);
1546static tree cp_parser_initializer
39703eb9 1547 (cp_parser *, bool *, bool *);
a723baf1 1548static tree cp_parser_initializer_clause
39703eb9 1549 (cp_parser *, bool *);
4038c495 1550static VEC(constructor_elt,gc) *cp_parser_initializer_list
39703eb9 1551 (cp_parser *, bool *);
a723baf1
MM
1552
1553static bool cp_parser_ctor_initializer_opt_and_function_body
1554 (cp_parser *);
1555
1556/* Classes [gram.class] */
1557
1558static tree cp_parser_class_name
fc6a28d7 1559 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
a723baf1 1560static tree cp_parser_class_specifier
94edc4ab 1561 (cp_parser *);
a723baf1 1562static tree cp_parser_class_head
38b305d0 1563 (cp_parser *, bool *, tree *);
a723baf1 1564static enum tag_types cp_parser_class_key
94edc4ab 1565 (cp_parser *);
a723baf1 1566static void cp_parser_member_specification_opt
94edc4ab 1567 (cp_parser *);
a723baf1 1568static void cp_parser_member_declaration
94edc4ab 1569 (cp_parser *);
a723baf1 1570static tree cp_parser_pure_specifier
94edc4ab 1571 (cp_parser *);
a723baf1 1572static tree cp_parser_constant_initializer
94edc4ab 1573 (cp_parser *);
a723baf1
MM
1574
1575/* Derived classes [gram.class.derived] */
1576
1577static tree cp_parser_base_clause
94edc4ab 1578 (cp_parser *);
a723baf1 1579static tree cp_parser_base_specifier
94edc4ab 1580 (cp_parser *);
a723baf1
MM
1581
1582/* Special member functions [gram.special] */
1583
1584static tree cp_parser_conversion_function_id
94edc4ab 1585 (cp_parser *);
a723baf1 1586static tree cp_parser_conversion_type_id
94edc4ab 1587 (cp_parser *);
058b15c1 1588static cp_declarator *cp_parser_conversion_declarator_opt
94edc4ab 1589 (cp_parser *);
a723baf1 1590static bool cp_parser_ctor_initializer_opt
94edc4ab 1591 (cp_parser *);
a723baf1 1592static void cp_parser_mem_initializer_list
94edc4ab 1593 (cp_parser *);
a723baf1 1594static tree cp_parser_mem_initializer
94edc4ab 1595 (cp_parser *);
a723baf1 1596static tree cp_parser_mem_initializer_id
94edc4ab 1597 (cp_parser *);
a723baf1
MM
1598
1599/* Overloading [gram.over] */
1600
1601static tree cp_parser_operator_function_id
94edc4ab 1602 (cp_parser *);
a723baf1 1603static tree cp_parser_operator
94edc4ab 1604 (cp_parser *);
a723baf1
MM
1605
1606/* Templates [gram.temp] */
1607
1608static void cp_parser_template_declaration
94edc4ab 1609 (cp_parser *, bool);
a723baf1 1610static tree cp_parser_template_parameter_list
94edc4ab 1611 (cp_parser *);
a723baf1 1612static tree cp_parser_template_parameter
058b15c1 1613 (cp_parser *, bool *);
a723baf1 1614static tree cp_parser_type_parameter
94edc4ab 1615 (cp_parser *);
a723baf1 1616static tree cp_parser_template_id
a668c6ad 1617 (cp_parser *, bool, bool, bool);
a723baf1 1618static tree cp_parser_template_name
a668c6ad 1619 (cp_parser *, bool, bool, bool, bool *);
a723baf1 1620static tree cp_parser_template_argument_list
94edc4ab 1621 (cp_parser *);
a723baf1 1622static tree cp_parser_template_argument
94edc4ab 1623 (cp_parser *);
a723baf1 1624static void cp_parser_explicit_instantiation
94edc4ab 1625 (cp_parser *);
a723baf1 1626static void cp_parser_explicit_specialization
94edc4ab 1627 (cp_parser *);
a723baf1
MM
1628
1629/* Exception handling [gram.exception] */
1630
21526606 1631static tree cp_parser_try_block
94edc4ab 1632 (cp_parser *);
a723baf1 1633static bool cp_parser_function_try_block
94edc4ab 1634 (cp_parser *);
a723baf1 1635static void cp_parser_handler_seq
94edc4ab 1636 (cp_parser *);
a723baf1 1637static void cp_parser_handler
94edc4ab 1638 (cp_parser *);
a723baf1 1639static tree cp_parser_exception_declaration
94edc4ab 1640 (cp_parser *);
a723baf1 1641static tree cp_parser_throw_expression
94edc4ab 1642 (cp_parser *);
a723baf1 1643static tree cp_parser_exception_specification_opt
94edc4ab 1644 (cp_parser *);
a723baf1 1645static tree cp_parser_type_id_list
94edc4ab 1646 (cp_parser *);
a723baf1
MM
1647
1648/* GNU Extensions */
1649
1650static tree cp_parser_asm_specification_opt
94edc4ab 1651 (cp_parser *);
a723baf1 1652static tree cp_parser_asm_operand_list
94edc4ab 1653 (cp_parser *);
a723baf1 1654static tree cp_parser_asm_clobber_list
94edc4ab 1655 (cp_parser *);
a723baf1 1656static tree cp_parser_attributes_opt
94edc4ab 1657 (cp_parser *);
a723baf1 1658static tree cp_parser_attribute_list
94edc4ab 1659 (cp_parser *);
a723baf1 1660static bool cp_parser_extension_opt
94edc4ab 1661 (cp_parser *, int *);
a723baf1 1662static void cp_parser_label_declaration
94edc4ab 1663 (cp_parser *);
a723baf1 1664
e58a9aa1
ZL
1665/* Objective-C++ Productions */
1666
1667static tree cp_parser_objc_message_receiver
1668 (cp_parser *);
1669static tree cp_parser_objc_message_args
1670 (cp_parser *);
1671static tree cp_parser_objc_message_expression
1672 (cp_parser *);
1673static tree cp_parser_objc_encode_expression
1674 (cp_parser *);
c8094d83 1675static tree cp_parser_objc_defs_expression
e58a9aa1
ZL
1676 (cp_parser *);
1677static tree cp_parser_objc_protocol_expression
1678 (cp_parser *);
1679static tree cp_parser_objc_selector_expression
1680 (cp_parser *);
1681static tree cp_parser_objc_expression
1682 (cp_parser *);
1683static bool cp_parser_objc_selector_p
1684 (enum cpp_ttype);
1685static tree cp_parser_objc_selector
1686 (cp_parser *);
1687static tree cp_parser_objc_protocol_refs_opt
1688 (cp_parser *);
1689static void cp_parser_objc_declaration
1690 (cp_parser *);
1691static tree cp_parser_objc_statement
1692 (cp_parser *);
1693
a723baf1
MM
1694/* Utility Routines */
1695
1696static tree cp_parser_lookup_name
fc6a28d7 1697 (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
a723baf1 1698static tree cp_parser_lookup_name_simple
94edc4ab 1699 (cp_parser *, tree);
a723baf1
MM
1700static tree cp_parser_maybe_treat_template_as_class
1701 (tree, bool);
1702static bool cp_parser_check_declarator_template_parameters
058b15c1 1703 (cp_parser *, cp_declarator *);
a723baf1 1704static bool cp_parser_check_template_parameters
94edc4ab 1705 (cp_parser *, unsigned);
d6b4ea85
MM
1706static tree cp_parser_simple_cast_expression
1707 (cp_parser *);
a723baf1 1708static tree cp_parser_global_scope_opt
94edc4ab 1709 (cp_parser *, bool);
a723baf1
MM
1710static bool cp_parser_constructor_declarator_p
1711 (cp_parser *, bool);
1712static tree cp_parser_function_definition_from_specifiers_and_declarator
62d1db17 1713 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
a723baf1 1714static tree cp_parser_function_definition_after_declarator
94edc4ab 1715 (cp_parser *, bool);
a723baf1 1716static void cp_parser_template_declaration_after_export
94edc4ab 1717 (cp_parser *, bool);
a723baf1 1718static tree cp_parser_single_declaration
94edc4ab 1719 (cp_parser *, bool, bool *);
a723baf1 1720static tree cp_parser_functional_cast
94edc4ab 1721 (cp_parser *, tree);
4bb8ca28 1722static tree cp_parser_save_member_function_body
62d1db17 1723 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
ec75414f
MM
1724static tree cp_parser_enclosed_template_argument_list
1725 (cp_parser *);
8db1028e
NS
1726static void cp_parser_save_default_args
1727 (cp_parser *, tree);
a723baf1 1728static void cp_parser_late_parsing_for_member
94edc4ab 1729 (cp_parser *, tree);
a723baf1 1730static void cp_parser_late_parsing_default_args
8218bd34 1731 (cp_parser *, tree);
a723baf1 1732static tree cp_parser_sizeof_operand
94edc4ab 1733 (cp_parser *, enum rid);
a723baf1 1734static bool cp_parser_declares_only_class_p
94edc4ab 1735 (cp_parser *);
62d1db17
MM
1736static void cp_parser_set_storage_class
1737 (cp_decl_specifier_seq *, cp_storage_class);
98ca843c 1738static void cp_parser_set_decl_spec_type
62d1db17 1739 (cp_decl_specifier_seq *, tree, bool);
a723baf1 1740static bool cp_parser_friend_p
62d1db17 1741 (const cp_decl_specifier_seq *);
a723baf1 1742static cp_token *cp_parser_require
94edc4ab 1743 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1744static cp_token *cp_parser_require_keyword
94edc4ab 1745 (cp_parser *, enum rid, const char *);
21526606 1746static bool cp_parser_token_starts_function_definition_p
94edc4ab 1747 (cp_token *);
a723baf1
MM
1748static bool cp_parser_next_token_starts_class_definition_p
1749 (cp_parser *);
d17811fd
MM
1750static bool cp_parser_next_token_ends_template_argument_p
1751 (cp_parser *);
f4abade9
GB
1752static bool cp_parser_nth_token_starts_template_argument_list_p
1753 (cp_parser *, size_t);
a723baf1 1754static enum tag_types cp_parser_token_is_class_key
94edc4ab 1755 (cp_token *);
a723baf1
MM
1756static void cp_parser_check_class_key
1757 (enum tag_types, tree type);
37d407a1
KL
1758static void cp_parser_check_access_in_redeclaration
1759 (tree type);
a723baf1
MM
1760static bool cp_parser_optional_template_keyword
1761 (cp_parser *);
21526606 1762static void cp_parser_pre_parsed_nested_name_specifier
2050a1bb 1763 (cp_parser *);
a723baf1 1764static void cp_parser_cache_group
c162c75e 1765 (cp_parser *, enum cpp_ttype, unsigned);
21526606 1766static void cp_parser_parse_tentatively
94edc4ab 1767 (cp_parser *);
a723baf1 1768static void cp_parser_commit_to_tentative_parse
94edc4ab 1769 (cp_parser *);
a723baf1 1770static void cp_parser_abort_tentative_parse
94edc4ab 1771 (cp_parser *);
a723baf1 1772static bool cp_parser_parse_definitely
94edc4ab 1773 (cp_parser *);
f7b5ecd9 1774static inline bool cp_parser_parsing_tentatively
94edc4ab 1775 (cp_parser *);
0b16f8f4 1776static bool cp_parser_uncommitted_to_tentative_parse_p
94edc4ab 1777 (cp_parser *);
a723baf1 1778static void cp_parser_error
94edc4ab 1779 (cp_parser *, const char *);
4bb8ca28
MM
1780static void cp_parser_name_lookup_error
1781 (cp_parser *, tree, tree, const char *);
e5976695 1782static bool cp_parser_simulate_error
94edc4ab 1783 (cp_parser *);
a723baf1 1784static void cp_parser_check_type_definition
94edc4ab 1785 (cp_parser *);
560ad596 1786static void cp_parser_check_for_definition_in_return_type
fc6a28d7 1787 (cp_declarator *, tree);
ee43dab5
MM
1788static void cp_parser_check_for_invalid_template_id
1789 (cp_parser *, tree);
625cbf93
MM
1790static bool cp_parser_non_integral_constant_expression
1791 (cp_parser *, const char *);
2097b5f2
GB
1792static void cp_parser_diagnose_invalid_type_name
1793 (cp_parser *, tree, tree);
1794static bool cp_parser_parse_and_diagnose_invalid_type_name
8fbc5ae7 1795 (cp_parser *);
7efa3e22 1796static int cp_parser_skip_to_closing_parenthesis
a668c6ad 1797 (cp_parser *, bool, bool, bool);
a723baf1 1798static void cp_parser_skip_to_end_of_statement
94edc4ab 1799 (cp_parser *);
e0860732
MM
1800static void cp_parser_consume_semicolon_at_end_of_statement
1801 (cp_parser *);
a723baf1 1802static void cp_parser_skip_to_end_of_block_or_statement
94edc4ab 1803 (cp_parser *);
a723baf1
MM
1804static void cp_parser_skip_to_closing_brace
1805 (cp_parser *);
1806static void cp_parser_skip_until_found
94edc4ab 1807 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1808static bool cp_parser_error_occurred
94edc4ab 1809 (cp_parser *);
a723baf1 1810static bool cp_parser_allow_gnu_extensions_p
94edc4ab 1811 (cp_parser *);
a723baf1 1812static bool cp_parser_is_string_literal
94edc4ab 1813 (cp_token *);
21526606 1814static bool cp_parser_is_keyword
94edc4ab 1815 (cp_token *, enum rid);
2097b5f2
GB
1816static tree cp_parser_make_typename_type
1817 (cp_parser *, tree, tree);
a723baf1 1818
4de8668e 1819/* Returns nonzero if we are parsing tentatively. */
f7b5ecd9
MM
1820
1821static inline bool
94edc4ab 1822cp_parser_parsing_tentatively (cp_parser* parser)
f7b5ecd9
MM
1823{
1824 return parser->context->next != NULL;
1825}
1826
4de8668e 1827/* Returns nonzero if TOKEN is a string literal. */
a723baf1
MM
1828
1829static bool
94edc4ab 1830cp_parser_is_string_literal (cp_token* token)
a723baf1
MM
1831{
1832 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1833}
1834
4de8668e 1835/* Returns nonzero if TOKEN is the indicated KEYWORD. */
a723baf1
MM
1836
1837static bool
94edc4ab 1838cp_parser_is_keyword (cp_token* token, enum rid keyword)
a723baf1
MM
1839{
1840 return token->keyword == keyword;
1841}
1842
8ff24a79
MM
1843/* A minimum or maximum operator has been seen. As these are
1844 deprecated, issue a warning. */
1845
1846static inline void
1847cp_parser_warn_min_max (void)
1848{
1849 if (warn_deprecated && !in_system_header)
d4ee4d25 1850 warning (0, "minimum/maximum operators are deprecated");
8ff24a79
MM
1851}
1852
2cfe82fe
ZW
1853/* If not parsing tentatively, issue a diagnostic of the form
1854 FILE:LINE: MESSAGE before TOKEN
1855 where TOKEN is the next token in the input stream. MESSAGE
1856 (specified by the caller) is usually of the form "expected
1857 OTHER-TOKEN". */
a723baf1
MM
1858
1859static void
94edc4ab 1860cp_parser_error (cp_parser* parser, const char* message)
a723baf1 1861{
e5976695 1862 if (!cp_parser_simulate_error (parser))
4bb8ca28 1863 {
2cfe82fe
ZW
1864 cp_token *token = cp_lexer_peek_token (parser->lexer);
1865 /* This diagnostic makes more sense if it is tagged to the line
1866 of the token we just peeked at. */
1867 cp_lexer_set_source_position_from_token (token);
0d63048c
MM
1868 if (token->type == CPP_PRAGMA)
1869 {
c8094d83 1870 error ("%<#pragma%> is not allowed here");
0d63048c
MM
1871 cp_lexer_purge_token (parser->lexer);
1872 return;
1873 }
21526606 1874 c_parse_error (message,
5c832178
MM
1875 /* Because c_parser_error does not understand
1876 CPP_KEYWORD, keywords are treated like
1877 identifiers. */
21526606 1878 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
5c832178 1879 token->value);
4bb8ca28
MM
1880 }
1881}
1882
1883/* Issue an error about name-lookup failing. NAME is the
1884 IDENTIFIER_NODE DECL is the result of
1885 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1886 the thing that we hoped to find. */
1887
1888static void
1889cp_parser_name_lookup_error (cp_parser* parser,
1890 tree name,
1891 tree decl,
1892 const char* desired)
1893{
1894 /* If name lookup completely failed, tell the user that NAME was not
1895 declared. */
1896 if (decl == error_mark_node)
1897 {
1898 if (parser->scope && parser->scope != global_namespace)
2a13a625 1899 error ("%<%D::%D%> has not been declared",
4bb8ca28
MM
1900 parser->scope, name);
1901 else if (parser->scope == global_namespace)
2a13a625 1902 error ("%<::%D%> has not been declared", name);
c8094d83 1903 else if (parser->object_scope
b14454ba 1904 && !CLASS_TYPE_P (parser->object_scope))
2a13a625 1905 error ("request for member %qD in non-class type %qT",
b14454ba
MM
1906 name, parser->object_scope);
1907 else if (parser->object_scope)
c8094d83 1908 error ("%<%T::%D%> has not been declared",
b14454ba 1909 parser->object_scope, name);
4bb8ca28 1910 else
9e637a26 1911 error ("%qD has not been declared", name);
4bb8ca28
MM
1912 }
1913 else if (parser->scope && parser->scope != global_namespace)
2a13a625 1914 error ("%<%D::%D%> %s", parser->scope, name, desired);
4bb8ca28 1915 else if (parser->scope == global_namespace)
2a13a625 1916 error ("%<::%D%> %s", name, desired);
4bb8ca28 1917 else
2a13a625 1918 error ("%qD %s", name, desired);
a723baf1
MM
1919}
1920
1921/* If we are parsing tentatively, remember that an error has occurred
e5976695 1922 during this tentative parse. Returns true if the error was
77077b39 1923 simulated; false if a message should be issued by the caller. */
a723baf1 1924
e5976695 1925static bool
94edc4ab 1926cp_parser_simulate_error (cp_parser* parser)
a723baf1 1927{
0b16f8f4 1928 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
e5976695
MM
1929 {
1930 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1931 return true;
1932 }
1933 return false;
a723baf1
MM
1934}
1935
1936/* This function is called when a type is defined. If type
1937 definitions are forbidden at this point, an error message is
1938 issued. */
1939
1940static void
94edc4ab 1941cp_parser_check_type_definition (cp_parser* parser)
a723baf1
MM
1942{
1943 /* If types are forbidden here, issue a message. */
1944 if (parser->type_definition_forbidden_message)
1945 /* Use `%s' to print the string in case there are any escape
1946 characters in the message. */
1947 error ("%s", parser->type_definition_forbidden_message);
1948}
1949
fc6a28d7 1950/* This function is called when the DECLARATOR is processed. The TYPE
472c29c3 1951 was a type defined in the decl-specifiers. If it is invalid to
fc6a28d7
MM
1952 define a type in the decl-specifiers for DECLARATOR, an error is
1953 issued. */
560ad596
MM
1954
1955static void
058b15c1 1956cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
fc6a28d7 1957 tree type)
560ad596
MM
1958{
1959 /* [dcl.fct] forbids type definitions in return types.
1960 Unfortunately, it's not easy to know whether or not we are
1961 processing a return type until after the fact. */
1962 while (declarator
058b15c1
MM
1963 && (declarator->kind == cdk_pointer
1964 || declarator->kind == cdk_reference
1965 || declarator->kind == cdk_ptrmem))
1966 declarator = declarator->declarator;
560ad596 1967 if (declarator
fc6a28d7
MM
1968 && declarator->kind == cdk_function)
1969 {
1970 error ("new types may not be defined in a return type");
1971 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1972 type);
1973 }
560ad596
MM
1974}
1975
ee43dab5
MM
1976/* A type-specifier (TYPE) has been parsed which cannot be followed by
1977 "<" in any valid C++ program. If the next token is indeed "<",
1978 issue a message warning the user about what appears to be an
1979 invalid attempt to form a template-id. */
1980
1981static void
21526606 1982cp_parser_check_for_invalid_template_id (cp_parser* parser,
ee43dab5
MM
1983 tree type)
1984{
0c5e4866 1985 cp_token_position start = 0;
ee43dab5
MM
1986
1987 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1988 {
1989 if (TYPE_P (type))
2a13a625 1990 error ("%qT is not a template", type);
ee43dab5 1991 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2a13a625 1992 error ("%qE is not a template", type);
ee43dab5
MM
1993 else
1994 error ("invalid template-id");
1995 /* Remember the location of the invalid "<". */
0b16f8f4 1996 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
0c5e4866 1997 start = cp_lexer_token_position (parser->lexer, true);
ee43dab5
MM
1998 /* Consume the "<". */
1999 cp_lexer_consume_token (parser->lexer);
2000 /* Parse the template arguments. */
2001 cp_parser_enclosed_template_argument_list (parser);
da1d7781 2002 /* Permanently remove the invalid template arguments so that
ee43dab5 2003 this error message is not issued again. */
0c5e4866
NS
2004 if (start)
2005 cp_lexer_purge_tokens_after (parser->lexer, start);
ee43dab5
MM
2006 }
2007}
2008
625cbf93
MM
2009/* If parsing an integral constant-expression, issue an error message
2010 about the fact that THING appeared and return true. Otherwise,
93678513 2011 return false. In either case, set
c8094d83 2012 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
14d22dd6 2013
625cbf93
MM
2014static bool
2015cp_parser_non_integral_constant_expression (cp_parser *parser,
2016 const char *thing)
14d22dd6 2017{
93678513 2018 parser->non_integral_constant_expression_p = true;
625cbf93
MM
2019 if (parser->integral_constant_expression_p)
2020 {
2021 if (!parser->allow_non_integral_constant_expression_p)
2022 {
2023 error ("%s cannot appear in a constant-expression", thing);
2024 return true;
2025 }
625cbf93
MM
2026 }
2027 return false;
14d22dd6
MM
2028}
2029
0c88d886 2030/* Emit a diagnostic for an invalid type name. SCOPE is the
6ca2d67f
MM
2031 qualifying scope (or NULL, if none) for ID. This function commits
2032 to the current active tentative parse, if any. (Otherwise, the
2033 problematic construct might be encountered again later, resulting
2034 in duplicate error messages.) */
8fbc5ae7 2035
2097b5f2
GB
2036static void
2037cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
6c0cc713
GB
2038{
2039 tree decl, old_scope;
2097b5f2
GB
2040 /* Try to lookup the identifier. */
2041 old_scope = parser->scope;
2042 parser->scope = scope;
2043 decl = cp_parser_lookup_name_simple (parser, id);
2044 parser->scope = old_scope;
2045 /* If the lookup found a template-name, it means that the user forgot
c72a1a86 2046 to specify an argument list. Emit a useful error message. */
2097b5f2 2047 if (TREE_CODE (decl) == TEMPLATE_DECL)
2a13a625 2048 error ("invalid use of template-name %qE without an argument list",
6c0cc713 2049 decl);
8fe4d24b 2050 else if (!parser->scope || parser->scope == error_mark_node)
8fbc5ae7 2051 {
8fbc5ae7 2052 /* Issue an error message. */
2a13a625 2053 error ("%qE does not name a type", id);
8fbc5ae7
MM
2054 /* If we're in a template class, it's possible that the user was
2055 referring to a type from a base class. For example:
2056
2057 template <typename T> struct A { typedef T X; };
2058 template <typename T> struct B : public A<T> { X x; };
2059
2060 The user should have said "typename A<T>::X". */
32db39c0
PC
2061 if (processing_template_decl && current_class_type
2062 && TYPE_BINFO (current_class_type))
8fbc5ae7
MM
2063 {
2064 tree b;
2065
2066 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2067 b;
2068 b = TREE_CHAIN (b))
2069 {
2070 tree base_type = BINFO_TYPE (b);
21526606 2071 if (CLASS_TYPE_P (base_type)
1fb3244a 2072 && dependent_type_p (base_type))
8fbc5ae7
MM
2073 {
2074 tree field;
2075 /* Go from a particular instantiation of the
2076 template (which will have an empty TYPE_FIELDs),
2077 to the main version. */
353b4fc0 2078 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
8fbc5ae7
MM
2079 for (field = TYPE_FIELDS (base_type);
2080 field;
2081 field = TREE_CHAIN (field))
2082 if (TREE_CODE (field) == TYPE_DECL
2097b5f2 2083 && DECL_NAME (field) == id)
8fbc5ae7 2084 {
c4f73174 2085 inform ("(perhaps %<typename %T::%E%> was intended)",
0cbd7506 2086 BINFO_TYPE (b), id);
8fbc5ae7
MM
2087 break;
2088 }
2089 if (field)
2090 break;
2091 }
2092 }
2093 }
8fbc5ae7 2094 }
2097b5f2
GB
2095 /* Here we diagnose qualified-ids where the scope is actually correct,
2096 but the identifier does not resolve to a valid type name. */
21526606 2097 else
2097b5f2
GB
2098 {
2099 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2a13a625 2100 error ("%qE in namespace %qE does not name a type",
2097b5f2
GB
2101 id, parser->scope);
2102 else if (TYPE_P (parser->scope))
2fbe4889 2103 error ("%qE in class %qT does not name a type", id, parser->scope);
2097b5f2 2104 else
315fb5db 2105 gcc_unreachable ();
2097b5f2 2106 }
6ca2d67f 2107 cp_parser_commit_to_tentative_parse (parser);
2097b5f2 2108}
8fbc5ae7 2109
2097b5f2
GB
2110/* Check for a common situation where a type-name should be present,
2111 but is not, and issue a sensible error message. Returns true if an
2112 invalid type-name was detected.
21526606 2113
2097b5f2 2114 The situation handled by this function are variable declarations of the
21526606
EC
2115 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2116 Usually, `ID' should name a type, but if we got here it means that it
2097b5f2
GB
2117 does not. We try to emit the best possible error message depending on
2118 how exactly the id-expression looks like.
2119*/
2120
2121static bool
2122cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2123{
2124 tree id;
2125
2126 cp_parser_parse_tentatively (parser);
21526606 2127 id = cp_parser_id_expression (parser,
2097b5f2
GB
2128 /*template_keyword_p=*/false,
2129 /*check_dependency_p=*/true,
2130 /*template_p=*/NULL,
2131 /*declarator_p=*/true);
2132 /* After the id-expression, there should be a plain identifier,
2133 otherwise this is not a simple variable declaration. Also, if
2134 the scope is dependent, we cannot do much. */
2135 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 2136 || (parser->scope && TYPE_P (parser->scope)
2097b5f2
GB
2137 && dependent_type_p (parser->scope)))
2138 {
2139 cp_parser_abort_tentative_parse (parser);
2140 return false;
2141 }
3590f0a6
MM
2142 if (!cp_parser_parse_definitely (parser)
2143 || TREE_CODE (id) != IDENTIFIER_NODE)
2097b5f2
GB
2144 return false;
2145
2097b5f2
GB
2146 /* Emit a diagnostic for the invalid type. */
2147 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2148 /* Skip to the end of the declaration; there's no point in
2149 trying to process it. */
2150 cp_parser_skip_to_end_of_block_or_statement (parser);
2151 return true;
8fbc5ae7
MM
2152}
2153
21526606 2154/* Consume tokens up to, and including, the next non-nested closing `)'.
7efa3e22
NS
2155 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2156 are doing error recovery. Returns -1 if OR_COMMA is true and we
2157 found an unnested comma. */
a723baf1 2158
7efa3e22
NS
2159static int
2160cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
21526606 2161 bool recovering,
a668c6ad
MM
2162 bool or_comma,
2163 bool consume_paren)
a723baf1 2164{
7efa3e22
NS
2165 unsigned paren_depth = 0;
2166 unsigned brace_depth = 0;
0173bb6f 2167 int result;
a723baf1 2168
0b16f8f4
VR
2169 if (recovering && !or_comma
2170 && cp_parser_uncommitted_to_tentative_parse_p (parser))
7efa3e22 2171 return 0;
21526606 2172
a723baf1
MM
2173 while (true)
2174 {
2175 cp_token *token;
21526606 2176
a723baf1
MM
2177 /* If we've run out of tokens, then there is no closing `)'. */
2178 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
0173bb6f
AO
2179 {
2180 result = 0;
2181 break;
2182 }
a723baf1 2183
a668c6ad 2184 token = cp_lexer_peek_token (parser->lexer);
21526606 2185
f4f206f4 2186 /* This matches the processing in skip_to_end_of_statement. */
a668c6ad 2187 if (token->type == CPP_SEMICOLON && !brace_depth)
0173bb6f
AO
2188 {
2189 result = 0;
2190 break;
2191 }
a668c6ad
MM
2192 if (token->type == CPP_OPEN_BRACE)
2193 ++brace_depth;
2194 if (token->type == CPP_CLOSE_BRACE)
7efa3e22 2195 {
a668c6ad 2196 if (!brace_depth--)
0173bb6f
AO
2197 {
2198 result = 0;
2199 break;
2200 }
7efa3e22 2201 }
a668c6ad
MM
2202 if (recovering && or_comma && token->type == CPP_COMMA
2203 && !brace_depth && !paren_depth)
0173bb6f
AO
2204 {
2205 result = -1;
2206 break;
2207 }
21526606 2208
7efa3e22
NS
2209 if (!brace_depth)
2210 {
2211 /* If it is an `(', we have entered another level of nesting. */
2212 if (token->type == CPP_OPEN_PAREN)
2213 ++paren_depth;
2214 /* If it is a `)', then we might be done. */
2215 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
a668c6ad
MM
2216 {
2217 if (consume_paren)
2218 cp_lexer_consume_token (parser->lexer);
0173bb6f
AO
2219 {
2220 result = 1;
2221 break;
2222 }
a668c6ad 2223 }
7efa3e22 2224 }
21526606 2225
a668c6ad
MM
2226 /* Consume the token. */
2227 cp_lexer_consume_token (parser->lexer);
a723baf1 2228 }
0173bb6f 2229
0173bb6f 2230 return result;
a723baf1
MM
2231}
2232
2233/* Consume tokens until we reach the end of the current statement.
2234 Normally, that will be just before consuming a `;'. However, if a
2235 non-nested `}' comes first, then we stop before consuming that. */
2236
2237static void
94edc4ab 2238cp_parser_skip_to_end_of_statement (cp_parser* parser)
a723baf1
MM
2239{
2240 unsigned nesting_depth = 0;
2241
2242 while (true)
2243 {
2244 cp_token *token;
2245
2246 /* Peek at the next token. */
2247 token = cp_lexer_peek_token (parser->lexer);
2248 /* If we've run out of tokens, stop. */
2249 if (token->type == CPP_EOF)
2250 break;
2251 /* If the next token is a `;', we have reached the end of the
2252 statement. */
2253 if (token->type == CPP_SEMICOLON && !nesting_depth)
2254 break;
2255 /* If the next token is a non-nested `}', then we have reached
2256 the end of the current block. */
2257 if (token->type == CPP_CLOSE_BRACE)
2258 {
2259 /* If this is a non-nested `}', stop before consuming it.
2260 That way, when confronted with something like:
2261
21526606 2262 { 3 + }
a723baf1
MM
2263
2264 we stop before consuming the closing `}', even though we
2265 have not yet reached a `;'. */
2266 if (nesting_depth == 0)
2267 break;
2268 /* If it is the closing `}' for a block that we have
2269 scanned, stop -- but only after consuming the token.
2270 That way given:
2271
0cbd7506 2272 void f g () { ... }
a723baf1
MM
2273 typedef int I;
2274
2275 we will stop after the body of the erroneously declared
2276 function, but before consuming the following `typedef'
2277 declaration. */
2278 if (--nesting_depth == 0)
2279 {
2280 cp_lexer_consume_token (parser->lexer);
2281 break;
2282 }
2283 }
2284 /* If it the next token is a `{', then we are entering a new
2285 block. Consume the entire block. */
2286 else if (token->type == CPP_OPEN_BRACE)
2287 ++nesting_depth;
2288 /* Consume the token. */
2289 cp_lexer_consume_token (parser->lexer);
2290 }
2291}
2292
e0860732
MM
2293/* This function is called at the end of a statement or declaration.
2294 If the next token is a semicolon, it is consumed; otherwise, error
2295 recovery is attempted. */
2296
2297static void
2298cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2299{
2300 /* Look for the trailing `;'. */
2301 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2302 {
2303 /* If there is additional (erroneous) input, skip to the end of
2304 the statement. */
2305 cp_parser_skip_to_end_of_statement (parser);
2306 /* If the next token is now a `;', consume it. */
2307 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2308 cp_lexer_consume_token (parser->lexer);
2309 }
2310}
2311
a723baf1
MM
2312/* Skip tokens until we have consumed an entire block, or until we
2313 have consumed a non-nested `;'. */
2314
2315static void
94edc4ab 2316cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
a723baf1 2317{
8fe4d24b 2318 int nesting_depth = 0;
a723baf1 2319
8fe4d24b 2320 while (nesting_depth >= 0)
a723baf1 2321 {
8fe4d24b 2322 cp_token *token = cp_lexer_peek_token (parser->lexer);
c8094d83 2323
a723baf1
MM
2324 if (token->type == CPP_EOF)
2325 break;
8fe4d24b
NS
2326
2327 switch (token->type)
a723baf1 2328 {
8fe4d24b
NS
2329 case CPP_EOF:
2330 /* If we've run out of tokens, stop. */
2331 nesting_depth = -1;
2332 continue;
2333
2334 case CPP_SEMICOLON:
2335 /* Stop if this is an unnested ';'. */
2336 if (!nesting_depth)
2337 nesting_depth = -1;
2338 break;
2339
2340 case CPP_CLOSE_BRACE:
2341 /* Stop if this is an unnested '}', or closes the outermost
2342 nesting level. */
2343 nesting_depth--;
2344 if (!nesting_depth)
2345 nesting_depth = -1;
2346 break;
c8094d83 2347
8fe4d24b
NS
2348 case CPP_OPEN_BRACE:
2349 /* Nest. */
2350 nesting_depth++;
2351 break;
2352
2353 default:
a723baf1
MM
2354 break;
2355 }
c8094d83 2356
a723baf1 2357 /* Consume the token. */
8fe4d24b 2358 cp_lexer_consume_token (parser->lexer);
c8094d83 2359
a723baf1
MM
2360 }
2361}
2362
2363/* Skip tokens until a non-nested closing curly brace is the next
2364 token. */
2365
2366static void
2367cp_parser_skip_to_closing_brace (cp_parser *parser)
2368{
2369 unsigned nesting_depth = 0;
2370
2371 while (true)
2372 {
2373 cp_token *token;
2374
2375 /* Peek at the next token. */
2376 token = cp_lexer_peek_token (parser->lexer);
2377 /* If we've run out of tokens, stop. */
2378 if (token->type == CPP_EOF)
2379 break;
2380 /* If the next token is a non-nested `}', then we have reached
2381 the end of the current block. */
2382 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2383 break;
2384 /* If it the next token is a `{', then we are entering a new
2385 block. Consume the entire block. */
2386 else if (token->type == CPP_OPEN_BRACE)
2387 ++nesting_depth;
2388 /* Consume the token. */
2389 cp_lexer_consume_token (parser->lexer);
2390 }
2391}
2392
2097b5f2
GB
2393/* This is a simple wrapper around make_typename_type. When the id is
2394 an unresolved identifier node, we can provide a superior diagnostic
2395 using cp_parser_diagnose_invalid_type_name. */
2396
2397static tree
2398cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
6c0cc713
GB
2399{
2400 tree result;
2401 if (TREE_CODE (id) == IDENTIFIER_NODE)
2402 {
fc6a28d7
MM
2403 result = make_typename_type (scope, id, typename_type,
2404 /*complain=*/0);
6c0cc713
GB
2405 if (result == error_mark_node)
2406 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2407 return result;
2408 }
fc6a28d7 2409 return make_typename_type (scope, id, typename_type, tf_error);
2097b5f2
GB
2410}
2411
2412
a723baf1
MM
2413/* Create a new C++ parser. */
2414
2415static cp_parser *
94edc4ab 2416cp_parser_new (void)
a723baf1
MM
2417{
2418 cp_parser *parser;
17211ab5 2419 cp_lexer *lexer;
b8b94c5b 2420 unsigned i;
17211ab5
GK
2421
2422 /* cp_lexer_new_main is called before calling ggc_alloc because
2423 cp_lexer_new_main might load a PCH file. */
2424 lexer = cp_lexer_new_main ();
a723baf1 2425
b8b94c5b
PB
2426 /* Initialize the binops_by_token so that we can get the tree
2427 directly from the token. */
2428 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2429 binops_by_token[binops[i].token_type] = binops[i];
2430
99dd239f 2431 parser = GGC_CNEW (cp_parser);
17211ab5 2432 parser->lexer = lexer;
a723baf1
MM
2433 parser->context = cp_parser_context_new (NULL);
2434
2435 /* For now, we always accept GNU extensions. */
2436 parser->allow_gnu_extensions_p = 1;
2437
2438 /* The `>' token is a greater-than operator, not the end of a
2439 template-id. */
2440 parser->greater_than_is_operator_p = true;
2441
2442 parser->default_arg_ok_p = true;
21526606 2443
a723baf1 2444 /* We are not parsing a constant-expression. */
67c03833
JM
2445 parser->integral_constant_expression_p = false;
2446 parser->allow_non_integral_constant_expression_p = false;
2447 parser->non_integral_constant_expression_p = false;
a723baf1
MM
2448
2449 /* Local variable names are not forbidden. */
2450 parser->local_variables_forbidden_p = false;
2451
34cd5ae7 2452 /* We are not processing an `extern "C"' declaration. */
a723baf1
MM
2453 parser->in_unbraced_linkage_specification_p = false;
2454
2455 /* We are not processing a declarator. */
2456 parser->in_declarator_p = false;
2457
4bb8ca28
MM
2458 /* We are not processing a template-argument-list. */
2459 parser->in_template_argument_list_p = false;
2460
0e59b3fb
MM
2461 /* We are not in an iteration statement. */
2462 parser->in_iteration_statement_p = false;
2463
2464 /* We are not in a switch statement. */
2465 parser->in_switch_statement_p = false;
2466
4f8163b1
MM
2467 /* We are not parsing a type-id inside an expression. */
2468 parser->in_type_id_in_expr_p = false;
2469
03fd3f84 2470 /* Declarations aren't implicitly extern "C". */
7d381002
MA
2471 parser->implicit_extern_c = false;
2472
c162c75e
MA
2473 /* String literals should be translated to the execution character set. */
2474 parser->translate_strings_p = true;
2475
a723baf1
MM
2476 /* The unparsed function queue is empty. */
2477 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2478
2479 /* There are no classes being defined. */
2480 parser->num_classes_being_defined = 0;
2481
2482 /* No template parameters apply. */
2483 parser->num_template_parameter_lists = 0;
2484
2485 return parser;
2486}
2487
2cfe82fe
ZW
2488/* Create a cp_lexer structure which will emit the tokens in CACHE
2489 and push it onto the parser's lexer stack. This is used for delayed
2490 parsing of in-class method bodies and default arguments, and should
2491 not be confused with tentative parsing. */
2492static void
2493cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2494{
2495 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2496 lexer->next = parser->lexer;
2497 parser->lexer = lexer;
2498
2499 /* Move the current source position to that of the first token in the
2500 new lexer. */
2501 cp_lexer_set_source_position_from_token (lexer->next_token);
2502}
2503
2504/* Pop the top lexer off the parser stack. This is never used for the
2505 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2506static void
2507cp_parser_pop_lexer (cp_parser *parser)
2508{
2509 cp_lexer *lexer = parser->lexer;
2510 parser->lexer = lexer->next;
2511 cp_lexer_destroy (lexer);
2512
2513 /* Put the current source position back where it was before this
2514 lexer was pushed. */
2515 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2516}
2517
a723baf1
MM
2518/* Lexical conventions [gram.lex] */
2519
2520/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2521 identifier. */
2522
21526606 2523static tree
94edc4ab 2524cp_parser_identifier (cp_parser* parser)
a723baf1
MM
2525{
2526 cp_token *token;
2527
2528 /* Look for the identifier. */
2529 token = cp_parser_require (parser, CPP_NAME, "identifier");
2530 /* Return the value. */
2531 return token ? token->value : error_mark_node;
2532}
2533
c162c75e
MA
2534/* Parse a sequence of adjacent string constants. Returns a
2535 TREE_STRING representing the combined, nul-terminated string
2536 constant. If TRANSLATE is true, translate the string to the
2537 execution character set. If WIDE_OK is true, a wide string is
2538 invalid here.
2539
2540 C++98 [lex.string] says that if a narrow string literal token is
2541 adjacent to a wide string literal token, the behavior is undefined.
2542 However, C99 6.4.5p4 says that this results in a wide string literal.
2543 We follow C99 here, for consistency with the C front end.
2544
2545 This code is largely lifted from lex_string() in c-lex.c.
2546
2547 FUTURE: ObjC++ will need to handle @-strings here. */
2548static tree
2549cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2550{
2551 tree value;
2552 bool wide = false;
2553 size_t count;
2554 struct obstack str_ob;
2555 cpp_string str, istr, *strs;
2556 cp_token *tok;
2557
2558 tok = cp_lexer_peek_token (parser->lexer);
2559 if (!cp_parser_is_string_literal (tok))
2560 {
2561 cp_parser_error (parser, "expected string-literal");
2562 return error_mark_node;
2563 }
2564
9688c3b8 2565 /* Try to avoid the overhead of creating and destroying an obstack
c162c75e 2566 for the common case of just one string. */
2cfe82fe
ZW
2567 if (!cp_parser_is_string_literal
2568 (cp_lexer_peek_nth_token (parser->lexer, 2)))
c162c75e 2569 {
2cfe82fe
ZW
2570 cp_lexer_consume_token (parser->lexer);
2571
c162c75e
MA
2572 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2573 str.len = TREE_STRING_LENGTH (tok->value);
2574 count = 1;
2575 if (tok->type == CPP_WSTRING)
2576 wide = true;
c162c75e
MA
2577
2578 strs = &str;
2579 }
2580 else
2581 {
2582 gcc_obstack_init (&str_ob);
2583 count = 0;
2584
2585 do
2586 {
2cfe82fe 2587 cp_lexer_consume_token (parser->lexer);
c162c75e
MA
2588 count++;
2589 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2590 str.len = TREE_STRING_LENGTH (tok->value);
2591 if (tok->type == CPP_WSTRING)
2592 wide = true;
2593
2594 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2595
2cfe82fe 2596 tok = cp_lexer_peek_token (parser->lexer);
c162c75e
MA
2597 }
2598 while (cp_parser_is_string_literal (tok));
2599
2600 strs = (cpp_string *) obstack_finish (&str_ob);
2601 }
2602
2603 if (wide && !wide_ok)
2604 {
2605 cp_parser_error (parser, "a wide string is invalid in this context");
2606 wide = false;
2607 }
2608
2609 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2610 (parse_in, strs, count, &istr, wide))
2611 {
2612 value = build_string (istr.len, (char *)istr.text);
2613 free ((void *)istr.text);
2614
2615 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2616 value = fix_string_type (value);
2617 }
2618 else
2619 /* cpp_interpret_string has issued an error. */
2620 value = error_mark_node;
2621
2622 if (count > 1)
2623 obstack_free (&str_ob, 0);
2624
2625 return value;
2626}
2627
2628
a723baf1
MM
2629/* Basic concepts [gram.basic] */
2630
2631/* Parse a translation-unit.
2632
2633 translation-unit:
21526606 2634 declaration-seq [opt]
a723baf1
MM
2635
2636 Returns TRUE if all went well. */
2637
2638static bool
94edc4ab 2639cp_parser_translation_unit (cp_parser* parser)
a723baf1 2640{
058b15c1
MM
2641 /* The address of the first non-permanent object on the declarator
2642 obstack. */
2643 static void *declarator_obstack_base;
2644
2645 bool success;
98ca843c 2646
058b15c1
MM
2647 /* Create the declarator obstack, if necessary. */
2648 if (!cp_error_declarator)
2649 {
2650 gcc_obstack_init (&declarator_obstack);
2651 /* Create the error declarator. */
2652 cp_error_declarator = make_declarator (cdk_error);
2653 /* Create the empty parameter list. */
62d1db17 2654 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
058b15c1
MM
2655 /* Remember where the base of the declarator obstack lies. */
2656 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2657 }
2658
a30efae8
GDR
2659 cp_parser_declaration_seq_opt (parser);
2660
2661 /* If there are no tokens left then all went well. */
2662 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
a723baf1 2663 {
a30efae8
GDR
2664 /* Get rid of the token array; we don't need it any more. */
2665 cp_lexer_destroy (parser->lexer);
2666 parser->lexer = NULL;
2667
2668 /* This file might have been a context that's implicitly extern
2669 "C". If so, pop the lang context. (Only relevant for PCH.) */
2670 if (parser->implicit_extern_c)
2671 {
2672 pop_lang_context ();
2673 parser->implicit_extern_c = false;
2674 }
2675
2676 /* Finish up. */
2677 finish_translation_unit ();
2678
2679 success = true;
a723baf1 2680 }
a30efae8
GDR
2681 else
2682 {
2683 cp_parser_error (parser, "expected declaration");
2684 success = false;
2685 }
2686
058b15c1 2687 /* Make sure the declarator obstack was fully cleaned up. */
50bc768d
NS
2688 gcc_assert (obstack_next_free (&declarator_obstack)
2689 == declarator_obstack_base);
a723baf1
MM
2690
2691 /* All went well. */
058b15c1 2692 return success;
a723baf1
MM
2693}
2694
2695/* Expressions [gram.expr] */
2696
2697/* Parse a primary-expression.
2698
2699 primary-expression:
2700 literal
2701 this
2702 ( expression )
2703 id-expression
2704
2705 GNU Extensions:
2706
2707 primary-expression:
2708 ( compound-statement )
2709 __builtin_va_arg ( assignment-expression , type-id )
2710
e58a9aa1
ZL
2711 Objective-C++ Extension:
2712
2713 primary-expression:
2714 objc-expression
2715
a723baf1
MM
2716 literal:
2717 __null
2718
02ed62dd
MM
2719 ADDRESS_P is true iff this expression was immediately preceded by
2720 "&" and therefore might denote a pointer-to-member. CAST_P is true
2721 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2722 true iff this expression is a tempalte argument.
93678513 2723
02ed62dd
MM
2724 Returns a representation of the expression. Upon return, *IDK
2725 indicates what kind of id-expression (if any) was present. */
a723baf1
MM
2726
2727static tree
21526606 2728cp_parser_primary_expression (cp_parser *parser,
02ed62dd 2729 bool address_p,
93678513 2730 bool cast_p,
02ed62dd
MM
2731 bool template_arg_p,
2732 cp_id_kind *idk)
a723baf1
MM
2733{
2734 cp_token *token;
2735
2736 /* Assume the primary expression is not an id-expression. */
b3445994 2737 *idk = CP_ID_KIND_NONE;
a723baf1
MM
2738
2739 /* Peek at the next token. */
2740 token = cp_lexer_peek_token (parser->lexer);
2741 switch (token->type)
2742 {
2743 /* literal:
2744 integer-literal
2745 character-literal
2746 floating-literal
2747 string-literal
2748 boolean-literal */
2749 case CPP_CHAR:
2750 case CPP_WCHAR:
a723baf1
MM
2751 case CPP_NUMBER:
2752 token = cp_lexer_consume_token (parser->lexer);
93678513
MM
2753 /* Floating-point literals are only allowed in an integral
2754 constant expression if they are cast to an integral or
2755 enumeration type. */
2756 if (TREE_CODE (token->value) == REAL_CST
8c94c75a
MM
2757 && parser->integral_constant_expression_p
2758 && pedantic)
93678513
MM
2759 {
2760 /* CAST_P will be set even in invalid code like "int(2.7 +
2761 ...)". Therefore, we have to check that the next token
2762 is sure to end the cast. */
2763 if (cast_p)
2764 {
2765 cp_token *next_token;
2766
2767 next_token = cp_lexer_peek_token (parser->lexer);
2768 if (/* The comma at the end of an
2769 enumerator-definition. */
2770 next_token->type != CPP_COMMA
2771 /* The curly brace at the end of an enum-specifier. */
2772 && next_token->type != CPP_CLOSE_BRACE
2773 /* The end of a statement. */
2774 && next_token->type != CPP_SEMICOLON
2775 /* The end of the cast-expression. */
2776 && next_token->type != CPP_CLOSE_PAREN
2777 /* The end of an array bound. */
060e7327
MM
2778 && next_token->type != CPP_CLOSE_SQUARE
2779 /* The closing ">" in a template-argument-list. */
2780 && (next_token->type != CPP_GREATER
2781 || parser->greater_than_is_operator_p))
93678513
MM
2782 cast_p = false;
2783 }
2784
2785 /* If we are within a cast, then the constraint that the
2786 cast is to an integral or enumeration type will be
2787 checked at that point. If we are not within a cast, then
2788 this code is invalid. */
2789 if (!cast_p)
c8094d83 2790 cp_parser_non_integral_constant_expression
93678513
MM
2791 (parser, "floating-point literal");
2792 }
a723baf1
MM
2793 return token->value;
2794
0173bb6f
AO
2795 case CPP_STRING:
2796 case CPP_WSTRING:
c162c75e 2797 /* ??? Should wide strings be allowed when parser->translate_strings_p
0cbd7506 2798 is false (i.e. in attributes)? If not, we can kill the third
c162c75e
MA
2799 argument to cp_parser_string_literal. */
2800 return cp_parser_string_literal (parser,
2801 parser->translate_strings_p,
2802 true);
0173bb6f 2803
a723baf1
MM
2804 case CPP_OPEN_PAREN:
2805 {
2806 tree expr;
2807 bool saved_greater_than_is_operator_p;
2808
2809 /* Consume the `('. */
2810 cp_lexer_consume_token (parser->lexer);
2811 /* Within a parenthesized expression, a `>' token is always
2812 the greater-than operator. */
21526606 2813 saved_greater_than_is_operator_p
a723baf1
MM
2814 = parser->greater_than_is_operator_p;
2815 parser->greater_than_is_operator_p = true;
2816 /* If we see `( { ' then we are looking at the beginning of
2817 a GNU statement-expression. */
2818 if (cp_parser_allow_gnu_extensions_p (parser)
2819 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2820 {
2821 /* Statement-expressions are not allowed by the standard. */
2822 if (pedantic)
21526606
EC
2823 pedwarn ("ISO C++ forbids braced-groups within expressions");
2824
a723baf1
MM
2825 /* And they're not allowed outside of a function-body; you
2826 cannot, for example, write:
21526606 2827
0cbd7506 2828 int i = ({ int j = 3; j + 1; });
21526606 2829
a723baf1
MM
2830 at class or namespace scope. */
2831 if (!at_function_scope_p ())
2832 error ("statement-expressions are allowed only inside functions");
2833 /* Start the statement-expression. */
2834 expr = begin_stmt_expr ();
2835 /* Parse the compound-statement. */
325c3691 2836 cp_parser_compound_statement (parser, expr, false);
a723baf1 2837 /* Finish up. */
303b7406 2838 expr = finish_stmt_expr (expr, false);
a723baf1
MM
2839 }
2840 else
2841 {
2842 /* Parse the parenthesized expression. */
93678513 2843 expr = cp_parser_expression (parser, cast_p);
a723baf1
MM
2844 /* Let the front end know that this expression was
2845 enclosed in parentheses. This matters in case, for
2846 example, the expression is of the form `A::B', since
2847 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2848 not. */
2849 finish_parenthesized_expr (expr);
2850 }
2851 /* The `>' token might be the end of a template-id or
2852 template-parameter-list now. */
21526606 2853 parser->greater_than_is_operator_p
a723baf1
MM
2854 = saved_greater_than_is_operator_p;
2855 /* Consume the `)'. */
2856 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2857 cp_parser_skip_to_end_of_statement (parser);
2858
2859 return expr;
2860 }
2861
2862 case CPP_KEYWORD:
2863 switch (token->keyword)
2864 {
2865 /* These two are the boolean literals. */
2866 case RID_TRUE:
2867 cp_lexer_consume_token (parser->lexer);
2868 return boolean_true_node;
2869 case RID_FALSE:
2870 cp_lexer_consume_token (parser->lexer);
2871 return boolean_false_node;
21526606 2872
a723baf1
MM
2873 /* The `__null' literal. */
2874 case RID_NULL:
2875 cp_lexer_consume_token (parser->lexer);
2876 return null_node;
2877
2878 /* Recognize the `this' keyword. */
2879 case RID_THIS:
2880 cp_lexer_consume_token (parser->lexer);
2881 if (parser->local_variables_forbidden_p)
2882 {
2a13a625 2883 error ("%<this%> may not be used in this context");
a723baf1
MM
2884 return error_mark_node;
2885 }
14d22dd6 2886 /* Pointers cannot appear in constant-expressions. */
625cbf93
MM
2887 if (cp_parser_non_integral_constant_expression (parser,
2888 "`this'"))
2889 return error_mark_node;
a723baf1
MM
2890 return finish_this_expr ();
2891
2892 /* The `operator' keyword can be the beginning of an
2893 id-expression. */
2894 case RID_OPERATOR:
2895 goto id_expression;
2896
2897 case RID_FUNCTION_NAME:
2898 case RID_PRETTY_FUNCTION_NAME:
2899 case RID_C99_FUNCTION_NAME:
2900 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2901 __func__ are the names of variables -- but they are
2902 treated specially. Therefore, they are handled here,
2903 rather than relying on the generic id-expression logic
21526606 2904 below. Grammatically, these names are id-expressions.
a723baf1
MM
2905
2906 Consume the token. */
2907 token = cp_lexer_consume_token (parser->lexer);
2908 /* Look up the name. */
2909 return finish_fname (token->value);
2910
2911 case RID_VA_ARG:
2912 {
2913 tree expression;
2914 tree type;
2915
2916 /* The `__builtin_va_arg' construct is used to handle
2917 `va_arg'. Consume the `__builtin_va_arg' token. */
2918 cp_lexer_consume_token (parser->lexer);
2919 /* Look for the opening `('. */
2920 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2921 /* Now, parse the assignment-expression. */
93678513
MM
2922 expression = cp_parser_assignment_expression (parser,
2923 /*cast_p=*/false);
a723baf1
MM
2924 /* Look for the `,'. */
2925 cp_parser_require (parser, CPP_COMMA, "`,'");
2926 /* Parse the type-id. */
2927 type = cp_parser_type_id (parser);
2928 /* Look for the closing `)'. */
2929 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14d22dd6
MM
2930 /* Using `va_arg' in a constant-expression is not
2931 allowed. */
625cbf93
MM
2932 if (cp_parser_non_integral_constant_expression (parser,
2933 "`va_arg'"))
2934 return error_mark_node;
a723baf1
MM
2935 return build_x_va_arg (expression, type);
2936 }
2937
263ee052 2938 case RID_OFFSETOF:
7a3ea201 2939 return cp_parser_builtin_offsetof (parser);
263ee052 2940
e58a9aa1
ZL
2941 /* Objective-C++ expressions. */
2942 case RID_AT_ENCODE:
2943 case RID_AT_PROTOCOL:
2944 case RID_AT_SELECTOR:
2945 return cp_parser_objc_expression (parser);
2946
a723baf1
MM
2947 default:
2948 cp_parser_error (parser, "expected primary-expression");
2949 return error_mark_node;
2950 }
a723baf1
MM
2951
2952 /* An id-expression can start with either an identifier, a
2953 `::' as the beginning of a qualified-id, or the "operator"
2954 keyword. */
2955 case CPP_NAME:
2956 case CPP_SCOPE:
2957 case CPP_TEMPLATE_ID:
2958 case CPP_NESTED_NAME_SPECIFIER:
2959 {
2960 tree id_expression;
2961 tree decl;
b3445994 2962 const char *error_msg;
02ed62dd
MM
2963 bool template_p;
2964 bool done;
a723baf1
MM
2965
2966 id_expression:
2967 /* Parse the id-expression. */
21526606
EC
2968 id_expression
2969 = cp_parser_id_expression (parser,
a723baf1
MM
2970 /*template_keyword_p=*/false,
2971 /*check_dependency_p=*/true,
02ed62dd 2972 &template_p,
f3c2dfc6 2973 /*declarator_p=*/false);
a723baf1
MM
2974 if (id_expression == error_mark_node)
2975 return error_mark_node;
02ed62dd
MM
2976 token = cp_lexer_peek_token (parser->lexer);
2977 done = (token->type != CPP_OPEN_SQUARE
2978 && token->type != CPP_OPEN_PAREN
2979 && token->type != CPP_DOT
2980 && token->type != CPP_DEREF
2981 && token->type != CPP_PLUS_PLUS
2982 && token->type != CPP_MINUS_MINUS);
a723baf1
MM
2983 /* If we have a template-id, then no further lookup is
2984 required. If the template-id was for a template-class, we
2985 will sometimes have a TYPE_DECL at this point. */
02ed62dd
MM
2986 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2987 || TREE_CODE (id_expression) == TYPE_DECL)
a723baf1
MM
2988 decl = id_expression;
2989 /* Look up the name. */
21526606 2990 else
a723baf1 2991 {
8f78f01f
MM
2992 bool ambiguous_p;
2993
2994 decl = cp_parser_lookup_name (parser, id_expression,
fc6a28d7 2995 none_type,
02ed62dd 2996 template_p,
8f78f01f
MM
2997 /*is_namespace=*/false,
2998 /*check_dependency=*/true,
2999 &ambiguous_p);
3000 /* If the lookup was ambiguous, an error will already have
3001 been issued. */
3002 if (ambiguous_p)
3003 return error_mark_node;
e58a9aa1
ZL
3004
3005 /* In Objective-C++, an instance variable (ivar) may be preferred
3006 to whatever cp_parser_lookup_name() found. */
3007 decl = objc_lookup_ivar (decl, id_expression);
3008
a723baf1 3009 /* If name lookup gives us a SCOPE_REF, then the
02ed62dd 3010 qualifying scope was dependent. */
a723baf1 3011 if (TREE_CODE (decl) == SCOPE_REF)
02ed62dd 3012 return decl;
a723baf1
MM
3013 /* Check to see if DECL is a local variable in a context
3014 where that is forbidden. */
3015 if (parser->local_variables_forbidden_p
3016 && local_variable_p (decl))
3017 {
3018 /* It might be that we only found DECL because we are
3019 trying to be generous with pre-ISO scoping rules.
3020 For example, consider:
3021
3022 int i;
3023 void g() {
3024 for (int i = 0; i < 10; ++i) {}
3025 extern void f(int j = i);
3026 }
3027
21526606 3028 Here, name look up will originally find the out
a723baf1
MM
3029 of scope `i'. We need to issue a warning message,
3030 but then use the global `i'. */
3031 decl = check_for_out_of_scope_variable (decl);
3032 if (local_variable_p (decl))
3033 {
2a13a625 3034 error ("local variable %qD may not appear in this context",
a723baf1
MM
3035 decl);
3036 return error_mark_node;
3037 }
3038 }
c006d942 3039 }
21526606 3040
02ed62dd
MM
3041 decl = (finish_id_expression
3042 (id_expression, decl, parser->scope,
3043 idk,
3044 parser->integral_constant_expression_p,
3045 parser->allow_non_integral_constant_expression_p,
3046 &parser->non_integral_constant_expression_p,
3047 template_p, done, address_p,
3048 template_arg_p,
3049 &error_msg));
b3445994
MM
3050 if (error_msg)
3051 cp_parser_error (parser, error_msg);
a723baf1
MM
3052 return decl;
3053 }
3054
3055 /* Anything else is an error. */
3056 default:
e58a9aa1 3057 /* ...unless we have an Objective-C++ message or string literal, that is. */
c8094d83 3058 if (c_dialect_objc ()
e58a9aa1
ZL
3059 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3060 return cp_parser_objc_expression (parser);
3061
a723baf1
MM
3062 cp_parser_error (parser, "expected primary-expression");
3063 return error_mark_node;
3064 }
3065}
3066
3067/* Parse an id-expression.
3068
3069 id-expression:
3070 unqualified-id
3071 qualified-id
3072
3073 qualified-id:
3074 :: [opt] nested-name-specifier template [opt] unqualified-id
3075 :: identifier
3076 :: operator-function-id
3077 :: template-id
3078
3079 Return a representation of the unqualified portion of the
3080 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3081 a `::' or nested-name-specifier.
3082
3083 Often, if the id-expression was a qualified-id, the caller will
3084 want to make a SCOPE_REF to represent the qualified-id. This
3085 function does not do this in order to avoid wastefully creating
3086 SCOPE_REFs when they are not required.
3087
a723baf1
MM
3088 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3089 `template' keyword.
3090
3091 If CHECK_DEPENDENCY_P is false, then names are looked up inside
21526606 3092 uninstantiated templates.
a723baf1 3093
15d2cb19 3094 If *TEMPLATE_P is non-NULL, it is set to true iff the
a723baf1 3095 `template' keyword is used to explicitly indicate that the entity
21526606 3096 named is a template.
f3c2dfc6
MM
3097
3098 If DECLARATOR_P is true, the id-expression is appearing as part of
cd0be382 3099 a declarator, rather than as part of an expression. */
a723baf1
MM
3100
3101static tree
3102cp_parser_id_expression (cp_parser *parser,
3103 bool template_keyword_p,
3104 bool check_dependency_p,
f3c2dfc6
MM
3105 bool *template_p,
3106 bool declarator_p)
a723baf1
MM
3107{
3108 bool global_scope_p;
3109 bool nested_name_specifier_p;
3110
3111 /* Assume the `template' keyword was not used. */
3112 if (template_p)
02ed62dd 3113 *template_p = template_keyword_p;
a723baf1
MM
3114
3115 /* Look for the optional `::' operator. */
21526606
EC
3116 global_scope_p
3117 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
a723baf1
MM
3118 != NULL_TREE);
3119 /* Look for the optional nested-name-specifier. */
21526606 3120 nested_name_specifier_p
a723baf1
MM
3121 = (cp_parser_nested_name_specifier_opt (parser,
3122 /*typename_keyword_p=*/false,
3123 check_dependency_p,
a668c6ad 3124 /*type_p=*/false,
a52eb3bc 3125 declarator_p)
a723baf1
MM
3126 != NULL_TREE);
3127 /* If there is a nested-name-specifier, then we are looking at
3128 the first qualified-id production. */
3129 if (nested_name_specifier_p)
3130 {
3131 tree saved_scope;
3132 tree saved_object_scope;
3133 tree saved_qualifying_scope;
3134 tree unqualified_id;
3135 bool is_template;
3136
3137 /* See if the next token is the `template' keyword. */
3138 if (!template_p)
3139 template_p = &is_template;
3140 *template_p = cp_parser_optional_template_keyword (parser);
3141 /* Name lookup we do during the processing of the
3142 unqualified-id might obliterate SCOPE. */
3143 saved_scope = parser->scope;
3144 saved_object_scope = parser->object_scope;
3145 saved_qualifying_scope = parser->qualifying_scope;
3146 /* Process the final unqualified-id. */
3147 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
f3c2dfc6
MM
3148 check_dependency_p,
3149 declarator_p);
a723baf1
MM
3150 /* Restore the SAVED_SCOPE for our caller. */
3151 parser->scope = saved_scope;
3152 parser->object_scope = saved_object_scope;
3153 parser->qualifying_scope = saved_qualifying_scope;
3154
3155 return unqualified_id;
3156 }
3157 /* Otherwise, if we are in global scope, then we are looking at one
3158 of the other qualified-id productions. */
3159 else if (global_scope_p)
3160 {
3161 cp_token *token;
3162 tree id;
3163
e5976695
MM
3164 /* Peek at the next token. */
3165 token = cp_lexer_peek_token (parser->lexer);
3166
3167 /* If it's an identifier, and the next token is not a "<", then
3168 we can avoid the template-id case. This is an optimization
3169 for this common case. */
21526606
EC
3170 if (token->type == CPP_NAME
3171 && !cp_parser_nth_token_starts_template_argument_list_p
f4abade9 3172 (parser, 2))
e5976695
MM
3173 return cp_parser_identifier (parser);
3174
a723baf1
MM
3175 cp_parser_parse_tentatively (parser);
3176 /* Try a template-id. */
21526606 3177 id = cp_parser_template_id (parser,
a723baf1 3178 /*template_keyword_p=*/false,
a668c6ad
MM
3179 /*check_dependency_p=*/true,
3180 declarator_p);
a723baf1
MM
3181 /* If that worked, we're done. */
3182 if (cp_parser_parse_definitely (parser))
3183 return id;
3184
e5976695
MM
3185 /* Peek at the next token. (Changes in the token buffer may
3186 have invalidated the pointer obtained above.) */
a723baf1
MM
3187 token = cp_lexer_peek_token (parser->lexer);
3188
3189 switch (token->type)
3190 {
3191 case CPP_NAME:
3192 return cp_parser_identifier (parser);
3193
3194 case CPP_KEYWORD:
3195 if (token->keyword == RID_OPERATOR)
3196 return cp_parser_operator_function_id (parser);
3197 /* Fall through. */
21526606 3198
a723baf1
MM
3199 default:
3200 cp_parser_error (parser, "expected id-expression");
3201 return error_mark_node;
3202 }
3203 }
3204 else
3205 return cp_parser_unqualified_id (parser, template_keyword_p,
f3c2dfc6
MM
3206 /*check_dependency_p=*/true,
3207 declarator_p);
a723baf1
MM
3208}
3209
3210/* Parse an unqualified-id.
3211
3212 unqualified-id:
3213 identifier
3214 operator-function-id
3215 conversion-function-id
3216 ~ class-name
3217 template-id
3218
3219 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3220 keyword, in a construct like `A::template ...'.
3221
3222 Returns a representation of unqualified-id. For the `identifier'
3223 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3224 production a BIT_NOT_EXPR is returned; the operand of the
3225 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3226 other productions, see the documentation accompanying the
3227 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
f3c2dfc6
MM
3228 names are looked up in uninstantiated templates. If DECLARATOR_P
3229 is true, the unqualified-id is appearing as part of a declarator,
3230 rather than as part of an expression. */
a723baf1
MM
3231
3232static tree
21526606 3233cp_parser_unqualified_id (cp_parser* parser,
0cbd7506 3234 bool template_keyword_p,
f3c2dfc6
MM
3235 bool check_dependency_p,
3236 bool declarator_p)
a723baf1
MM
3237{
3238 cp_token *token;
3239
3240 /* Peek at the next token. */
3241 token = cp_lexer_peek_token (parser->lexer);
21526606 3242
a723baf1
MM
3243 switch (token->type)
3244 {
3245 case CPP_NAME:
3246 {
3247 tree id;
3248
3249 /* We don't know yet whether or not this will be a
3250 template-id. */
3251 cp_parser_parse_tentatively (parser);
3252 /* Try a template-id. */
3253 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3254 check_dependency_p,
3255 declarator_p);
a723baf1
MM
3256 /* If it worked, we're done. */
3257 if (cp_parser_parse_definitely (parser))
3258 return id;
3259 /* Otherwise, it's an ordinary identifier. */
3260 return cp_parser_identifier (parser);
3261 }
3262
3263 case CPP_TEMPLATE_ID:
3264 return cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3265 check_dependency_p,
3266 declarator_p);
a723baf1
MM
3267
3268 case CPP_COMPL:
3269 {
3270 tree type_decl;
3271 tree qualifying_scope;
3272 tree object_scope;
3273 tree scope;
88e95ee3 3274 bool done;
a723baf1
MM
3275
3276 /* Consume the `~' token. */
3277 cp_lexer_consume_token (parser->lexer);
3278 /* Parse the class-name. The standard, as written, seems to
3279 say that:
3280
3281 template <typename T> struct S { ~S (); };
3282 template <typename T> S<T>::~S() {}
3283
0cbd7506 3284 is invalid, since `~' must be followed by a class-name, but
a723baf1
MM
3285 `S<T>' is dependent, and so not known to be a class.
3286 That's not right; we need to look in uninstantiated
3287 templates. A further complication arises from:
3288
3289 template <typename T> void f(T t) {
3290 t.T::~T();
21526606 3291 }
a723baf1
MM
3292
3293 Here, it is not possible to look up `T' in the scope of `T'
3294 itself. We must look in both the current scope, and the
21526606 3295 scope of the containing complete expression.
a723baf1
MM
3296
3297 Yet another issue is:
3298
0cbd7506
MS
3299 struct S {
3300 int S;
3301 ~S();
3302 };
a723baf1 3303
0cbd7506 3304 S::~S() {}
a723baf1 3305
0cbd7506 3306 The standard does not seem to say that the `S' in `~S'
a723baf1
MM
3307 should refer to the type `S' and not the data member
3308 `S::S'. */
3309
3310 /* DR 244 says that we look up the name after the "~" in the
3311 same scope as we looked up the qualifying name. That idea
3312 isn't fully worked out; it's more complicated than that. */
3313 scope = parser->scope;
3314 object_scope = parser->object_scope;
3315 qualifying_scope = parser->qualifying_scope;
3316
3317 /* If the name is of the form "X::~X" it's OK. */
3318 if (scope && TYPE_P (scope)
3319 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 3320 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1 3321 == CPP_OPEN_PAREN)
21526606 3322 && (cp_lexer_peek_token (parser->lexer)->value
a723baf1
MM
3323 == TYPE_IDENTIFIER (scope)))
3324 {
3325 cp_lexer_consume_token (parser->lexer);
3326 return build_nt (BIT_NOT_EXPR, scope);
3327 }
3328
3329 /* If there was an explicit qualification (S::~T), first look
3330 in the scope given by the qualification (i.e., S). */
88e95ee3 3331 done = false;
e3754f9c 3332 type_decl = NULL_TREE;
a723baf1
MM
3333 if (scope)
3334 {
3335 cp_parser_parse_tentatively (parser);
21526606 3336 type_decl = cp_parser_class_name (parser,
a723baf1
MM
3337 /*typename_keyword_p=*/false,
3338 /*template_keyword_p=*/false,
fc6a28d7 3339 none_type,
a723baf1 3340 /*check_dependency=*/false,
a668c6ad
MM
3341 /*class_head_p=*/false,
3342 declarator_p);
a723baf1 3343 if (cp_parser_parse_definitely (parser))
88e95ee3 3344 done = true;
a723baf1
MM
3345 }
3346 /* In "N::S::~S", look in "N" as well. */
88e95ee3 3347 if (!done && scope && qualifying_scope)
a723baf1
MM
3348 {
3349 cp_parser_parse_tentatively (parser);
3350 parser->scope = qualifying_scope;
3351 parser->object_scope = NULL_TREE;
3352 parser->qualifying_scope = NULL_TREE;
21526606
EC
3353 type_decl
3354 = cp_parser_class_name (parser,
a723baf1
MM
3355 /*typename_keyword_p=*/false,
3356 /*template_keyword_p=*/false,
fc6a28d7 3357 none_type,
a723baf1 3358 /*check_dependency=*/false,
a668c6ad
MM
3359 /*class_head_p=*/false,
3360 declarator_p);
a723baf1 3361 if (cp_parser_parse_definitely (parser))
88e95ee3 3362 done = true;
a723baf1
MM
3363 }
3364 /* In "p->S::~T", look in the scope given by "*p" as well. */
88e95ee3 3365 else if (!done && object_scope)
a723baf1
MM
3366 {
3367 cp_parser_parse_tentatively (parser);
3368 parser->scope = object_scope;
3369 parser->object_scope = NULL_TREE;
3370 parser->qualifying_scope = NULL_TREE;
21526606
EC
3371 type_decl
3372 = cp_parser_class_name (parser,
a723baf1
MM
3373 /*typename_keyword_p=*/false,
3374 /*template_keyword_p=*/false,
fc6a28d7 3375 none_type,
a723baf1 3376 /*check_dependency=*/false,
a668c6ad
MM
3377 /*class_head_p=*/false,
3378 declarator_p);
a723baf1 3379 if (cp_parser_parse_definitely (parser))
88e95ee3 3380 done = true;
a723baf1
MM
3381 }
3382 /* Look in the surrounding context. */
88e95ee3
MM
3383 if (!done)
3384 {
3385 parser->scope = NULL_TREE;
3386 parser->object_scope = NULL_TREE;
3387 parser->qualifying_scope = NULL_TREE;
3388 type_decl
3389 = cp_parser_class_name (parser,
3390 /*typename_keyword_p=*/false,
3391 /*template_keyword_p=*/false,
3392 none_type,
3393 /*check_dependency=*/false,
3394 /*class_head_p=*/false,
3395 declarator_p);
3396 }
a723baf1
MM
3397 /* If an error occurred, assume that the name of the
3398 destructor is the same as the name of the qualifying
3399 class. That allows us to keep parsing after running
3400 into ill-formed destructor names. */
3401 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3402 return build_nt (BIT_NOT_EXPR, scope);
3403 else if (type_decl == error_mark_node)
3404 return error_mark_node;
3405
f3c2dfc6
MM
3406 /* [class.dtor]
3407
3408 A typedef-name that names a class shall not be used as the
3409 identifier in the declarator for a destructor declaration. */
21526606 3410 if (declarator_p
f3c2dfc6 3411 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
33a69702
VR
3412 && !DECL_SELF_REFERENCE_P (type_decl)
3413 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
2a13a625 3414 error ("typedef-name %qD used as destructor declarator",
f3c2dfc6
MM
3415 type_decl);
3416
a723baf1
MM
3417 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3418 }
3419
3420 case CPP_KEYWORD:
3421 if (token->keyword == RID_OPERATOR)
3422 {
3423 tree id;
3424
3425 /* This could be a template-id, so we try that first. */
3426 cp_parser_parse_tentatively (parser);
3427 /* Try a template-id. */
3428 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3429 /*check_dependency_p=*/true,
3430 declarator_p);
a723baf1
MM
3431 /* If that worked, we're done. */
3432 if (cp_parser_parse_definitely (parser))
3433 return id;
3434 /* We still don't know whether we're looking at an
3435 operator-function-id or a conversion-function-id. */
3436 cp_parser_parse_tentatively (parser);
3437 /* Try an operator-function-id. */
3438 id = cp_parser_operator_function_id (parser);
3439 /* If that didn't work, try a conversion-function-id. */
3440 if (!cp_parser_parse_definitely (parser))
3441 id = cp_parser_conversion_function_id (parser);
3442
3443 return id;
3444 }
3445 /* Fall through. */
3446
3447 default:
3448 cp_parser_error (parser, "expected unqualified-id");
3449 return error_mark_node;
3450 }
3451}
3452
3453/* Parse an (optional) nested-name-specifier.
3454
3455 nested-name-specifier:
3456 class-or-namespace-name :: nested-name-specifier [opt]
3457 class-or-namespace-name :: template nested-name-specifier [opt]
3458
3459 PARSER->SCOPE should be set appropriately before this function is
3460 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3461 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3462 in name lookups.
3463
3464 Sets PARSER->SCOPE to the class (TYPE) or namespace
3465 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3466 it unchanged if there is no nested-name-specifier. Returns the new
21526606 3467 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
a668c6ad
MM
3468
3469 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3470 part of a declaration and/or decl-specifier. */
a723baf1
MM
3471
3472static tree
21526606
EC
3473cp_parser_nested_name_specifier_opt (cp_parser *parser,
3474 bool typename_keyword_p,
a723baf1 3475 bool check_dependency_p,
a668c6ad
MM
3476 bool type_p,
3477 bool is_declaration)
a723baf1
MM
3478{
3479 bool success = false;
3480 tree access_check = NULL_TREE;
0c5e4866
NS
3481 cp_token_position start = 0;
3482 cp_token *token;
a723baf1
MM
3483
3484 /* If the next token corresponds to a nested name specifier, there
2050a1bb 3485 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
21526606 3486 false, it may have been true before, in which case something
2050a1bb
MM
3487 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3488 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3489 CHECK_DEPENDENCY_P is false, we have to fall through into the
3490 main loop. */
3491 if (check_dependency_p
3492 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3493 {
3494 cp_parser_pre_parsed_nested_name_specifier (parser);
a723baf1
MM
3495 return parser->scope;
3496 }
3497
3498 /* Remember where the nested-name-specifier starts. */
0b16f8f4 3499 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
0c5e4866 3500 start = cp_lexer_token_position (parser->lexer, false);
a723baf1 3501
8d241e0b 3502 push_deferring_access_checks (dk_deferred);
cf22909c 3503
a723baf1
MM
3504 while (true)
3505 {
3506 tree new_scope;
3507 tree old_scope;
3508 tree saved_qualifying_scope;
a723baf1
MM
3509 bool template_keyword_p;
3510
2050a1bb
MM
3511 /* Spot cases that cannot be the beginning of a
3512 nested-name-specifier. */
3513 token = cp_lexer_peek_token (parser->lexer);
3514
3515 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3516 the already parsed nested-name-specifier. */
3517 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3518 {
3519 /* Grab the nested-name-specifier and continue the loop. */
3520 cp_parser_pre_parsed_nested_name_specifier (parser);
3521 success = true;
3522 continue;
3523 }
3524
a723baf1
MM
3525 /* Spot cases that cannot be the beginning of a
3526 nested-name-specifier. On the second and subsequent times
3527 through the loop, we look for the `template' keyword. */
f7b5ecd9 3528 if (success && token->keyword == RID_TEMPLATE)
a723baf1
MM
3529 ;
3530 /* A template-id can start a nested-name-specifier. */
f7b5ecd9 3531 else if (token->type == CPP_TEMPLATE_ID)
a723baf1
MM
3532 ;
3533 else
3534 {
3535 /* If the next token is not an identifier, then it is
3536 definitely not a class-or-namespace-name. */
f7b5ecd9 3537 if (token->type != CPP_NAME)
a723baf1
MM
3538 break;
3539 /* If the following token is neither a `<' (to begin a
3540 template-id), nor a `::', then we are not looking at a
3541 nested-name-specifier. */
3542 token = cp_lexer_peek_nth_token (parser->lexer, 2);
f4abade9
GB
3543 if (token->type != CPP_SCOPE
3544 && !cp_parser_nth_token_starts_template_argument_list_p
3545 (parser, 2))
a723baf1
MM
3546 break;
3547 }
3548
3549 /* The nested-name-specifier is optional, so we parse
3550 tentatively. */
3551 cp_parser_parse_tentatively (parser);
3552
3553 /* Look for the optional `template' keyword, if this isn't the
3554 first time through the loop. */
3555 if (success)
3556 template_keyword_p = cp_parser_optional_template_keyword (parser);
3557 else
3558 template_keyword_p = false;
3559
3560 /* Save the old scope since the name lookup we are about to do
3561 might destroy it. */
3562 old_scope = parser->scope;
3563 saved_qualifying_scope = parser->qualifying_scope;
a52eb3bc
MM
3564 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3565 look up names in "X<T>::I" in order to determine that "Y" is
3566 a template. So, if we have a typename at this point, we make
3567 an effort to look through it. */
c8094d83 3568 if (is_declaration
67bcc252 3569 && !typename_keyword_p
c8094d83 3570 && parser->scope
a52eb3bc 3571 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
c8094d83 3572 parser->scope = resolve_typename_type (parser->scope,
a52eb3bc 3573 /*only_current_p=*/false);
a723baf1 3574 /* Parse the qualifying entity. */
21526606 3575 new_scope
a723baf1
MM
3576 = cp_parser_class_or_namespace_name (parser,
3577 typename_keyword_p,
3578 template_keyword_p,
3579 check_dependency_p,
a668c6ad
MM
3580 type_p,
3581 is_declaration);
a723baf1
MM
3582 /* Look for the `::' token. */
3583 cp_parser_require (parser, CPP_SCOPE, "`::'");
3584
3585 /* If we found what we wanted, we keep going; otherwise, we're
3586 done. */
3587 if (!cp_parser_parse_definitely (parser))
3588 {
3589 bool error_p = false;
3590
3591 /* Restore the OLD_SCOPE since it was valid before the
3592 failed attempt at finding the last
3593 class-or-namespace-name. */
3594 parser->scope = old_scope;
3595 parser->qualifying_scope = saved_qualifying_scope;
3596 /* If the next token is an identifier, and the one after
3597 that is a `::', then any valid interpretation would have
3598 found a class-or-namespace-name. */
3599 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 3600 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1 3601 == CPP_SCOPE)
21526606 3602 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
a723baf1
MM
3603 != CPP_COMPL))
3604 {
3605 token = cp_lexer_consume_token (parser->lexer);
21526606 3606 if (!error_p)
a723baf1
MM
3607 {
3608 tree decl;
3609
3610 decl = cp_parser_lookup_name_simple (parser, token->value);
3611 if (TREE_CODE (decl) == TEMPLATE_DECL)
2a13a625 3612 error ("%qD used without template parameters", decl);
a723baf1 3613 else
21526606
EC
3614 cp_parser_name_lookup_error
3615 (parser, token->value, decl,
4bb8ca28 3616 "is not a class or namespace");
a723baf1
MM
3617 parser->scope = NULL_TREE;
3618 error_p = true;
eea9800f
MM
3619 /* Treat this as a successful nested-name-specifier
3620 due to:
3621
3622 [basic.lookup.qual]
3623
3624 If the name found is not a class-name (clause
3625 _class_) or namespace-name (_namespace.def_), the
3626 program is ill-formed. */
3627 success = true;
a723baf1
MM
3628 }
3629 cp_lexer_consume_token (parser->lexer);
3630 }
3631 break;
3632 }
a723baf1
MM
3633 /* We've found one valid nested-name-specifier. */
3634 success = true;
02ed62dd
MM
3635 /* Name lookup always gives us a DECL. */
3636 if (TREE_CODE (new_scope) == TYPE_DECL)
3637 new_scope = TREE_TYPE (new_scope);
3638 /* Uses of "template" must be followed by actual templates. */
3639 if (template_keyword_p
3640 && !(CLASS_TYPE_P (new_scope)
3641 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3642 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3643 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3644 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3645 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3646 == TEMPLATE_ID_EXPR)))
3647 pedwarn (TYPE_P (new_scope)
3648 ? "%qT is not a template"
3649 : "%qD is not a template",
3650 new_scope);
a723baf1
MM
3651 /* If it is a class scope, try to complete it; we are about to
3652 be looking up names inside the class. */
02ed62dd 3653 if (TYPE_P (new_scope)
8fbc5ae7
MM
3654 /* Since checking types for dependency can be expensive,
3655 avoid doing it if the type is already complete. */
02ed62dd 3656 && !COMPLETE_TYPE_P (new_scope)
8fbc5ae7 3657 /* Do not try to complete dependent types. */
02ed62dd
MM
3658 && !dependent_type_p (new_scope))
3659 new_scope = complete_type (new_scope);
3660 /* Make sure we look in the right scope the next time through
3661 the loop. */
3662 parser->scope = new_scope;
a723baf1
MM
3663 }
3664
cf22909c
KL
3665 /* Retrieve any deferred checks. Do not pop this access checks yet
3666 so the memory will not be reclaimed during token replacing below. */
3667 access_check = get_deferred_access_checks ();
3668
a723baf1
MM
3669 /* If parsing tentatively, replace the sequence of tokens that makes
3670 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3671 token. That way, should we re-parse the token stream, we will
3672 not have to repeat the effort required to do the parse, nor will
3673 we issue duplicate error messages. */
0c5e4866 3674 if (success && start)
a723baf1 3675 {
0c5e4866 3676 cp_token *token = cp_lexer_token_at (parser->lexer, start);
c8094d83 3677
a723baf1
MM
3678 /* Reset the contents of the START token. */
3679 token->type = CPP_NESTED_NAME_SPECIFIER;
3680 token->value = build_tree_list (access_check, parser->scope);
3681 TREE_TYPE (token->value) = parser->qualifying_scope;
3682 token->keyword = RID_MAX;
c8094d83 3683
a723baf1 3684 /* Purge all subsequent tokens. */
0c5e4866 3685 cp_lexer_purge_tokens_after (parser->lexer, start);
a723baf1
MM
3686 }
3687
cf22909c 3688 pop_deferring_access_checks ();
a723baf1
MM
3689 return success ? parser->scope : NULL_TREE;
3690}
3691
3692/* Parse a nested-name-specifier. See
3693 cp_parser_nested_name_specifier_opt for details. This function
3694 behaves identically, except that it will an issue an error if no
8fe4d24b 3695 nested-name-specifier is present. */
a723baf1
MM
3696
3697static tree
21526606
EC
3698cp_parser_nested_name_specifier (cp_parser *parser,
3699 bool typename_keyword_p,
a723baf1 3700 bool check_dependency_p,
a668c6ad
MM
3701 bool type_p,
3702 bool is_declaration)
a723baf1
MM
3703{
3704 tree scope;
3705
3706 /* Look for the nested-name-specifier. */
3707 scope = cp_parser_nested_name_specifier_opt (parser,
3708 typename_keyword_p,
3709 check_dependency_p,
a668c6ad
MM
3710 type_p,
3711 is_declaration);
a723baf1
MM
3712 /* If it was not present, issue an error message. */
3713 if (!scope)
3714 {
3715 cp_parser_error (parser, "expected nested-name-specifier");
eb5abb39 3716 parser->scope = NULL_TREE;
a723baf1
MM
3717 }
3718
3719 return scope;
3720}
3721
3722/* Parse a class-or-namespace-name.
3723
3724 class-or-namespace-name:
3725 class-name
3726 namespace-name
3727
3728 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3729 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3730 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3731 TYPE_P is TRUE iff the next name should be taken as a class-name,
3732 even the same name is declared to be another entity in the same
3733 scope.
3734
3735 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
eea9800f
MM
3736 specified by the class-or-namespace-name. If neither is found the
3737 ERROR_MARK_NODE is returned. */
a723baf1
MM
3738
3739static tree
21526606 3740cp_parser_class_or_namespace_name (cp_parser *parser,
a723baf1
MM
3741 bool typename_keyword_p,
3742 bool template_keyword_p,
3743 bool check_dependency_p,
a668c6ad
MM
3744 bool type_p,
3745 bool is_declaration)
a723baf1
MM
3746{
3747 tree saved_scope;
3748 tree saved_qualifying_scope;
3749 tree saved_object_scope;
3750 tree scope;
eea9800f 3751 bool only_class_p;
a723baf1 3752
a723baf1
MM
3753 /* Before we try to parse the class-name, we must save away the
3754 current PARSER->SCOPE since cp_parser_class_name will destroy
3755 it. */
3756 saved_scope = parser->scope;
3757 saved_qualifying_scope = parser->qualifying_scope;
3758 saved_object_scope = parser->object_scope;
eea9800f
MM
3759 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3760 there is no need to look for a namespace-name. */
bbaab916 3761 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
eea9800f
MM
3762 if (!only_class_p)
3763 cp_parser_parse_tentatively (parser);
21526606 3764 scope = cp_parser_class_name (parser,
a723baf1
MM
3765 typename_keyword_p,
3766 template_keyword_p,
fc6a28d7 3767 type_p ? class_type : none_type,
a723baf1 3768 check_dependency_p,
a668c6ad
MM
3769 /*class_head_p=*/false,
3770 is_declaration);
a723baf1 3771 /* If that didn't work, try for a namespace-name. */
eea9800f 3772 if (!only_class_p && !cp_parser_parse_definitely (parser))
a723baf1
MM
3773 {
3774 /* Restore the saved scope. */
3775 parser->scope = saved_scope;
3776 parser->qualifying_scope = saved_qualifying_scope;
3777 parser->object_scope = saved_object_scope;
eea9800f
MM
3778 /* If we are not looking at an identifier followed by the scope
3779 resolution operator, then this is not part of a
3780 nested-name-specifier. (Note that this function is only used
3781 to parse the components of a nested-name-specifier.) */
3782 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3783 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3784 return error_mark_node;
a723baf1
MM
3785 scope = cp_parser_namespace_name (parser);
3786 }
3787
3788 return scope;
3789}
3790
3791/* Parse a postfix-expression.
3792
3793 postfix-expression:
3794 primary-expression
3795 postfix-expression [ expression ]
3796 postfix-expression ( expression-list [opt] )
3797 simple-type-specifier ( expression-list [opt] )
21526606 3798 typename :: [opt] nested-name-specifier identifier
a723baf1
MM
3799 ( expression-list [opt] )
3800 typename :: [opt] nested-name-specifier template [opt] template-id
3801 ( expression-list [opt] )
3802 postfix-expression . template [opt] id-expression
3803 postfix-expression -> template [opt] id-expression
3804 postfix-expression . pseudo-destructor-name
3805 postfix-expression -> pseudo-destructor-name
3806 postfix-expression ++
3807 postfix-expression --
3808 dynamic_cast < type-id > ( expression )
3809 static_cast < type-id > ( expression )
3810 reinterpret_cast < type-id > ( expression )
3811 const_cast < type-id > ( expression )
3812 typeid ( expression )
3813 typeid ( type-id )
3814
3815 GNU Extension:
21526606 3816
a723baf1
MM
3817 postfix-expression:
3818 ( type-id ) { initializer-list , [opt] }
3819
3820 This extension is a GNU version of the C99 compound-literal
3821 construct. (The C99 grammar uses `type-name' instead of `type-id',
3822 but they are essentially the same concept.)
3823
3824 If ADDRESS_P is true, the postfix expression is the operand of the
93678513 3825 `&' operator. CAST_P is true if this expression is the target of a
c8094d83 3826 cast.
a723baf1
MM
3827
3828 Returns a representation of the expression. */
3829
3830static tree
93678513 3831cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
a723baf1
MM
3832{
3833 cp_token *token;
3834 enum rid keyword;
b3445994 3835 cp_id_kind idk = CP_ID_KIND_NONE;
a723baf1 3836 tree postfix_expression = NULL_TREE;
a723baf1
MM
3837
3838 /* Peek at the next token. */
3839 token = cp_lexer_peek_token (parser->lexer);
3840 /* Some of the productions are determined by keywords. */
3841 keyword = token->keyword;
3842 switch (keyword)
3843 {
3844 case RID_DYNCAST:
3845 case RID_STATCAST:
3846 case RID_REINTCAST:
3847 case RID_CONSTCAST:
3848 {
3849 tree type;
3850 tree expression;
3851 const char *saved_message;
3852
3853 /* All of these can be handled in the same way from the point
3854 of view of parsing. Begin by consuming the token
3855 identifying the cast. */
3856 cp_lexer_consume_token (parser->lexer);
21526606 3857
a723baf1
MM
3858 /* New types cannot be defined in the cast. */
3859 saved_message = parser->type_definition_forbidden_message;
3860 parser->type_definition_forbidden_message
3861 = "types may not be defined in casts";
3862
3863 /* Look for the opening `<'. */
3864 cp_parser_require (parser, CPP_LESS, "`<'");
3865 /* Parse the type to which we are casting. */
3866 type = cp_parser_type_id (parser);
3867 /* Look for the closing `>'. */
3868 cp_parser_require (parser, CPP_GREATER, "`>'");
3869 /* Restore the old message. */
3870 parser->type_definition_forbidden_message = saved_message;
3871
3872 /* And the expression which is being cast. */
3873 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
93678513 3874 expression = cp_parser_expression (parser, /*cast_p=*/true);
a723baf1
MM
3875 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3876
14d22dd6
MM
3877 /* Only type conversions to integral or enumeration types
3878 can be used in constant-expressions. */
67c03833 3879 if (parser->integral_constant_expression_p
14d22dd6 3880 && !dependent_type_p (type)
263ee052 3881 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
98ca843c 3882 && (cp_parser_non_integral_constant_expression
625cbf93
MM
3883 (parser,
3884 "a cast to a type other than an integral or "
3885 "enumeration type")))
3886 return error_mark_node;
14d22dd6 3887
a723baf1
MM
3888 switch (keyword)
3889 {
3890 case RID_DYNCAST:
3891 postfix_expression
3892 = build_dynamic_cast (type, expression);
3893 break;
3894 case RID_STATCAST:
3895 postfix_expression
3896 = build_static_cast (type, expression);
3897 break;
3898 case RID_REINTCAST:
3899 postfix_expression
3900 = build_reinterpret_cast (type, expression);
3901 break;
3902 case RID_CONSTCAST:
3903 postfix_expression
3904 = build_const_cast (type, expression);
3905 break;
3906 default:
315fb5db 3907 gcc_unreachable ();
a723baf1
MM
3908 }
3909 }
3910 break;
3911
3912 case RID_TYPEID:
3913 {
3914 tree type;
3915 const char *saved_message;
4f8163b1 3916 bool saved_in_type_id_in_expr_p;
a723baf1
MM
3917
3918 /* Consume the `typeid' token. */
3919 cp_lexer_consume_token (parser->lexer);
3920 /* Look for the `(' token. */
3921 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3922 /* Types cannot be defined in a `typeid' expression. */
3923 saved_message = parser->type_definition_forbidden_message;
3924 parser->type_definition_forbidden_message
3925 = "types may not be defined in a `typeid\' expression";
3926 /* We can't be sure yet whether we're looking at a type-id or an
3927 expression. */
3928 cp_parser_parse_tentatively (parser);
3929 /* Try a type-id first. */
4f8163b1
MM
3930 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3931 parser->in_type_id_in_expr_p = true;
a723baf1 3932 type = cp_parser_type_id (parser);
4f8163b1 3933 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
3934 /* Look for the `)' token. Otherwise, we can't be sure that
3935 we're not looking at an expression: consider `typeid (int
3936 (3))', for example. */
3937 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3938 /* If all went well, simply lookup the type-id. */
3939 if (cp_parser_parse_definitely (parser))
3940 postfix_expression = get_typeid (type);
3941 /* Otherwise, fall back to the expression variant. */
3942 else
3943 {
3944 tree expression;
3945
3946 /* Look for an expression. */
93678513 3947 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
3948 /* Compute its typeid. */
3949 postfix_expression = build_typeid (expression);
3950 /* Look for the `)' token. */
3951 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3952 }
4424e0da 3953 /* `typeid' may not appear in an integral constant expression. */
98ca843c 3954 if (cp_parser_non_integral_constant_expression(parser,
4424e0da
GB
3955 "`typeid' operator"))
3956 return error_mark_node;
a723baf1
MM
3957 /* Restore the saved message. */
3958 parser->type_definition_forbidden_message = saved_message;
3959 }
3960 break;
21526606 3961
a723baf1
MM
3962 case RID_TYPENAME:
3963 {
3964 bool template_p = false;
3965 tree id;
3966 tree type;
26c895e7 3967 tree scope;
a723baf1
MM
3968
3969 /* Consume the `typename' token. */
3970 cp_lexer_consume_token (parser->lexer);
3ce5fa4f 3971
a723baf1 3972 /* Look for the optional `::' operator. */
21526606 3973 cp_parser_global_scope_opt (parser,
a723baf1 3974 /*current_scope_valid_p=*/false);
26c895e7
KL
3975 /* Look for the nested-name-specifier. In case of error here,
3976 consume the trailing id to avoid subsequent error messages
3977 for usual cases. */
3978 scope = cp_parser_nested_name_specifier (parser,
3979 /*typename_keyword_p=*/true,
3980 /*check_dependency_p=*/true,
3981 /*type_p=*/true,
3982 /*is_declaration=*/true);
3983
a723baf1
MM
3984 /* Look for the optional `template' keyword. */
3985 template_p = cp_parser_optional_template_keyword (parser);
3986 /* We don't know whether we're looking at a template-id or an
3987 identifier. */
3988 cp_parser_parse_tentatively (parser);
3989 /* Try a template-id. */
3990 id = cp_parser_template_id (parser, template_p,
a668c6ad
MM
3991 /*check_dependency_p=*/true,
3992 /*is_declaration=*/true);
a723baf1
MM
3993 /* If that didn't work, try an identifier. */
3994 if (!cp_parser_parse_definitely (parser))
3995 id = cp_parser_identifier (parser);
26c895e7
KL
3996
3997 /* Don't process id if nested name specifier is invalid. */
8fe4d24b 3998 if (!scope || scope == error_mark_node)
26c895e7 3999 return error_mark_node;
26bcf8fc
MM
4000 /* If we look up a template-id in a non-dependent qualifying
4001 scope, there's no need to create a dependent type. */
31e832d1
JM
4002 if (TREE_CODE (id) == TYPE_DECL
4003 && (!TYPE_P (scope)
4004 || !dependent_type_p (parser->scope)))
26bcf8fc 4005 type = TREE_TYPE (id);
a723baf1
MM
4006 /* Create a TYPENAME_TYPE to represent the type to which the
4007 functional cast is being performed. */
26bcf8fc 4008 else
98ca843c 4009 type = make_typename_type (parser->scope, id,
fc6a28d7 4010 typename_type,
26bcf8fc 4011 /*complain=*/1);
a723baf1
MM
4012
4013 postfix_expression = cp_parser_functional_cast (parser, type);
4014 }
4015 break;
4016
4017 default:
4018 {
4019 tree type;
4020
4021 /* If the next thing is a simple-type-specifier, we may be
4022 looking at a functional cast. We could also be looking at
4023 an id-expression. So, we try the functional cast, and if
4024 that doesn't work we fall back to the primary-expression. */
4025 cp_parser_parse_tentatively (parser);
4026 /* Look for the simple-type-specifier. */
21526606 4027 type = cp_parser_simple_type_specifier (parser,
62d1db17
MM
4028 /*decl_specs=*/NULL,
4029 CP_PARSER_FLAGS_NONE);
a723baf1
MM
4030 /* Parse the cast itself. */
4031 if (!cp_parser_error_occurred (parser))
21526606 4032 postfix_expression
a723baf1
MM
4033 = cp_parser_functional_cast (parser, type);
4034 /* If that worked, we're done. */
4035 if (cp_parser_parse_definitely (parser))
4036 break;
4037
4038 /* If the functional-cast didn't work out, try a
4039 compound-literal. */
14d22dd6
MM
4040 if (cp_parser_allow_gnu_extensions_p (parser)
4041 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
a723baf1 4042 {
4038c495 4043 VEC(constructor_elt,gc) *initializer_list = NULL;
4f8163b1 4044 bool saved_in_type_id_in_expr_p;
a723baf1
MM
4045
4046 cp_parser_parse_tentatively (parser);
14d22dd6
MM
4047 /* Consume the `('. */
4048 cp_lexer_consume_token (parser->lexer);
4049 /* Parse the type. */
4f8163b1
MM
4050 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4051 parser->in_type_id_in_expr_p = true;
14d22dd6 4052 type = cp_parser_type_id (parser);
4f8163b1 4053 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14d22dd6
MM
4054 /* Look for the `)'. */
4055 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4056 /* Look for the `{'. */
4057 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4058 /* If things aren't going well, there's no need to
4059 keep going. */
4060 if (!cp_parser_error_occurred (parser))
a723baf1 4061 {
39703eb9 4062 bool non_constant_p;
14d22dd6 4063 /* Parse the initializer-list. */
21526606 4064 initializer_list
39703eb9 4065 = cp_parser_initializer_list (parser, &non_constant_p);
14d22dd6
MM
4066 /* Allow a trailing `,'. */
4067 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4068 cp_lexer_consume_token (parser->lexer);
4069 /* Look for the final `}'. */
4070 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1
MM
4071 }
4072 /* If that worked, we're definitely looking at a
4073 compound-literal expression. */
4074 if (cp_parser_parse_definitely (parser))
4075 {
4076 /* Warn the user that a compound literal is not
4077 allowed in standard C++. */
4078 if (pedantic)
4079 pedwarn ("ISO C++ forbids compound-literals");
4080 /* Form the representation of the compound-literal. */
21526606 4081 postfix_expression
a723baf1
MM
4082 = finish_compound_literal (type, initializer_list);
4083 break;
4084 }
4085 }
4086
4087 /* It must be a primary-expression. */
02ed62dd
MM
4088 postfix_expression
4089 = cp_parser_primary_expression (parser, address_p, cast_p,
4090 /*template_arg_p=*/false,
4091 &idk);
a723baf1
MM
4092 }
4093 break;
4094 }
4095
a723baf1
MM
4096 /* Keep looping until the postfix-expression is complete. */
4097 while (true)
4098 {
10b1d5e7
MM
4099 if (idk == CP_ID_KIND_UNQUALIFIED
4100 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
a723baf1 4101 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
b3445994 4102 /* It is not a Koenig lookup function call. */
21526606 4103 postfix_expression
b3445994 4104 = unqualified_name_lookup_error (postfix_expression);
21526606 4105
a723baf1
MM
4106 /* Peek at the next token. */
4107 token = cp_lexer_peek_token (parser->lexer);
4108
4109 switch (token->type)
4110 {
4111 case CPP_OPEN_SQUARE:
7a3ea201
RH
4112 postfix_expression
4113 = cp_parser_postfix_open_square_expression (parser,
4114 postfix_expression,
4115 false);
4116 idk = CP_ID_KIND_NONE;
a723baf1
MM
4117 break;
4118
4119 case CPP_OPEN_PAREN:
4120 /* postfix-expression ( expression-list [opt] ) */
4121 {
6d80c4b9 4122 bool koenig_p;
88a7beb7
MM
4123 bool is_builtin_constant_p;
4124 bool saved_integral_constant_expression_p = false;
4125 bool saved_non_integral_constant_expression_p = false;
4126 tree args;
4127
c8094d83 4128 is_builtin_constant_p
88a7beb7
MM
4129 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4130 if (is_builtin_constant_p)
4131 {
4132 /* The whole point of __builtin_constant_p is to allow
4133 non-constant expressions to appear as arguments. */
4134 saved_integral_constant_expression_p
4135 = parser->integral_constant_expression_p;
4136 saved_non_integral_constant_expression_p
4137 = parser->non_integral_constant_expression_p;
4138 parser->integral_constant_expression_p = false;
4139 }
4140 args = (cp_parser_parenthesized_expression_list
c8094d83 4141 (parser, /*is_attribute_list=*/false,
88a7beb7
MM
4142 /*cast_p=*/false,
4143 /*non_constant_p=*/NULL));
4144 if (is_builtin_constant_p)
4145 {
4146 parser->integral_constant_expression_p
4147 = saved_integral_constant_expression_p;
4148 parser->non_integral_constant_expression_p
4149 = saved_non_integral_constant_expression_p;
4150 }
a723baf1 4151
7efa3e22
NS
4152 if (args == error_mark_node)
4153 {
4154 postfix_expression = error_mark_node;
4155 break;
4156 }
21526606 4157
14d22dd6
MM
4158 /* Function calls are not permitted in
4159 constant-expressions. */
100d337a
MA
4160 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4161 && cp_parser_non_integral_constant_expression (parser,
4162 "a function call"))
14d22dd6 4163 {
625cbf93
MM
4164 postfix_expression = error_mark_node;
4165 break;
14d22dd6 4166 }
a723baf1 4167
6d80c4b9 4168 koenig_p = false;
399dedb9
NS
4169 if (idk == CP_ID_KIND_UNQUALIFIED)
4170 {
89d594a2
NS
4171 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4172 {
4173 if (args)
4174 {
4175 koenig_p = true;
4176 postfix_expression
4177 = perform_koenig_lookup (postfix_expression, args);
4178 }
4179 else
4180 postfix_expression
4181 = unqualified_fn_lookup_error (postfix_expression);
4182 }
676e33ca
MM
4183 /* We do not perform argument-dependent lookup if
4184 normal lookup finds a non-function, in accordance
4185 with the expected resolution of DR 218. */
89d594a2 4186 else if (args && is_overloaded_fn (postfix_expression))
6d80c4b9 4187 {
89d594a2
NS
4188 tree fn = get_first_fn (postfix_expression);
4189
4190 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4191 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4192
4193 /* Only do argument dependent lookup if regular
4194 lookup does not find a set of member functions.
4195 [basic.lookup.koenig]/2a */
4196 if (!DECL_FUNCTION_MEMBER_P (fn))
4197 {
4198 koenig_p = true;
4199 postfix_expression
4200 = perform_koenig_lookup (postfix_expression, args);
4201 }
6d80c4b9 4202 }
399dedb9 4203 }
21526606 4204
d17811fd 4205 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
a723baf1 4206 {
d17811fd
MM
4207 tree instance = TREE_OPERAND (postfix_expression, 0);
4208 tree fn = TREE_OPERAND (postfix_expression, 1);
4209
4210 if (processing_template_decl
4211 && (type_dependent_expression_p (instance)
4212 || (!BASELINK_P (fn)
4213 && TREE_CODE (fn) != FIELD_DECL)
584672ee 4214 || type_dependent_expression_p (fn)
d17811fd
MM
4215 || any_type_dependent_arguments_p (args)))
4216 {
4217 postfix_expression
6de9cd9a
DN
4218 = build_min_nt (CALL_EXPR, postfix_expression,
4219 args, NULL_TREE);
d17811fd
MM
4220 break;
4221 }
9f880ef9
MM
4222
4223 if (BASELINK_P (fn))
4224 postfix_expression
21526606
EC
4225 = (build_new_method_call
4226 (instance, fn, args, NULL_TREE,
4227 (idk == CP_ID_KIND_QUALIFIED
9f880ef9
MM
4228 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4229 else
4230 postfix_expression
4231 = finish_call_expr (postfix_expression, args,
4232 /*disallow_virtual=*/false,
4233 /*koenig_p=*/false);
a723baf1 4234 }
d17811fd
MM
4235 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4236 || TREE_CODE (postfix_expression) == MEMBER_REF
4237 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
a723baf1
MM
4238 postfix_expression = (build_offset_ref_call_from_tree
4239 (postfix_expression, args));
b3445994 4240 else if (idk == CP_ID_KIND_QUALIFIED)
2050a1bb
MM
4241 /* A call to a static class member, or a namespace-scope
4242 function. */
4243 postfix_expression
4244 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
4245 /*disallow_virtual=*/true,
4246 koenig_p);
a723baf1 4247 else
2050a1bb 4248 /* All other function calls. */
21526606
EC
4249 postfix_expression
4250 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
4251 /*disallow_virtual=*/false,
4252 koenig_p);
a723baf1
MM
4253
4254 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
b3445994 4255 idk = CP_ID_KIND_NONE;
a723baf1
MM
4256 }
4257 break;
21526606 4258
a723baf1
MM
4259 case CPP_DOT:
4260 case CPP_DEREF:
21526606
EC
4261 /* postfix-expression . template [opt] id-expression
4262 postfix-expression . pseudo-destructor-name
a723baf1
MM
4263 postfix-expression -> template [opt] id-expression
4264 postfix-expression -> pseudo-destructor-name */
98ca843c 4265
7a3ea201
RH
4266 /* Consume the `.' or `->' operator. */
4267 cp_lexer_consume_token (parser->lexer);
a723baf1 4268
7a3ea201
RH
4269 postfix_expression
4270 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4271 postfix_expression,
4272 false, &idk);
a723baf1
MM
4273 break;
4274
4275 case CPP_PLUS_PLUS:
4276 /* postfix-expression ++ */
4277 /* Consume the `++' token. */
4278 cp_lexer_consume_token (parser->lexer);
a5ac3982 4279 /* Generate a representation for the complete expression. */
21526606
EC
4280 postfix_expression
4281 = finish_increment_expr (postfix_expression,
a5ac3982 4282 POSTINCREMENT_EXPR);
14d22dd6 4283 /* Increments may not appear in constant-expressions. */
625cbf93
MM
4284 if (cp_parser_non_integral_constant_expression (parser,
4285 "an increment"))
4286 postfix_expression = error_mark_node;
b3445994 4287 idk = CP_ID_KIND_NONE;
a723baf1
MM
4288 break;
4289
4290 case CPP_MINUS_MINUS:
4291 /* postfix-expression -- */
4292 /* Consume the `--' token. */
4293 cp_lexer_consume_token (parser->lexer);
a5ac3982 4294 /* Generate a representation for the complete expression. */
21526606
EC
4295 postfix_expression
4296 = finish_increment_expr (postfix_expression,
a5ac3982 4297 POSTDECREMENT_EXPR);
14d22dd6 4298 /* Decrements may not appear in constant-expressions. */
625cbf93
MM
4299 if (cp_parser_non_integral_constant_expression (parser,
4300 "a decrement"))
4301 postfix_expression = error_mark_node;
b3445994 4302 idk = CP_ID_KIND_NONE;
a723baf1
MM
4303 break;
4304
4305 default:
4306 return postfix_expression;
4307 }
4308 }
4309
4310 /* We should never get here. */
315fb5db 4311 gcc_unreachable ();
a723baf1
MM
4312 return error_mark_node;
4313}
4314
7a3ea201
RH
4315/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4316 by cp_parser_builtin_offsetof. We're looking for
4317
4318 postfix-expression [ expression ]
4319
4320 FOR_OFFSETOF is set if we're being called in that context, which
4321 changes how we deal with integer constant expressions. */
4322
4323static tree
4324cp_parser_postfix_open_square_expression (cp_parser *parser,
4325 tree postfix_expression,
4326 bool for_offsetof)
4327{
4328 tree index;
4329
4330 /* Consume the `[' token. */
4331 cp_lexer_consume_token (parser->lexer);
4332
4333 /* Parse the index expression. */
4334 /* ??? For offsetof, there is a question of what to allow here. If
4335 offsetof is not being used in an integral constant expression context,
4336 then we *could* get the right answer by computing the value at runtime.
4337 If we are in an integral constant expression context, then we might
4338 could accept any constant expression; hard to say without analysis.
4339 Rather than open the barn door too wide right away, allow only integer
77880ae4 4340 constant expressions here. */
7a3ea201
RH
4341 if (for_offsetof)
4342 index = cp_parser_constant_expression (parser, false, NULL);
4343 else
93678513 4344 index = cp_parser_expression (parser, /*cast_p=*/false);
7a3ea201
RH
4345
4346 /* Look for the closing `]'. */
4347 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4348
4349 /* Build the ARRAY_REF. */
4350 postfix_expression = grok_array_decl (postfix_expression, index);
4351
4352 /* When not doing offsetof, array references are not permitted in
4353 constant-expressions. */
4354 if (!for_offsetof
4355 && (cp_parser_non_integral_constant_expression
4356 (parser, "an array reference")))
4357 postfix_expression = error_mark_node;
4358
4359 return postfix_expression;
4360}
4361
4362/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4363 by cp_parser_builtin_offsetof. We're looking for
4364
4365 postfix-expression . template [opt] id-expression
4366 postfix-expression . pseudo-destructor-name
4367 postfix-expression -> template [opt] id-expression
4368 postfix-expression -> pseudo-destructor-name
4369
4370 FOR_OFFSETOF is set if we're being called in that context. That sorta
4371 limits what of the above we'll actually accept, but nevermind.
4372 TOKEN_TYPE is the "." or "->" token, which will already have been
4373 removed from the stream. */
4374
4375static tree
4376cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4377 enum cpp_ttype token_type,
4378 tree postfix_expression,
4379 bool for_offsetof, cp_id_kind *idk)
4380{
4381 tree name;
4382 bool dependent_p;
17a27b4f 4383 bool pseudo_destructor_p;
7a3ea201
RH
4384 tree scope = NULL_TREE;
4385
4386 /* If this is a `->' operator, dereference the pointer. */
4387 if (token_type == CPP_DEREF)
4388 postfix_expression = build_x_arrow (postfix_expression);
4389 /* Check to see whether or not the expression is type-dependent. */
4390 dependent_p = type_dependent_expression_p (postfix_expression);
4391 /* The identifier following the `->' or `.' is not qualified. */
4392 parser->scope = NULL_TREE;
4393 parser->qualifying_scope = NULL_TREE;
4394 parser->object_scope = NULL_TREE;
4395 *idk = CP_ID_KIND_NONE;
4396 /* Enter the scope corresponding to the type of the object
4397 given by the POSTFIX_EXPRESSION. */
4398 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4399 {
4400 scope = TREE_TYPE (postfix_expression);
4401 /* According to the standard, no expression should ever have
4402 reference type. Unfortunately, we do not currently match
4403 the standard in this respect in that our internal representation
4404 of an expression may have reference type even when the standard
4405 says it does not. Therefore, we have to manually obtain the
4406 underlying type here. */
4407 scope = non_reference (scope);
4408 /* The type of the POSTFIX_EXPRESSION must be complete. */
4409 scope = complete_type_or_else (scope, NULL_TREE);
4410 /* Let the name lookup machinery know that we are processing a
4411 class member access expression. */
4412 parser->context->object_type = scope;
4413 /* If something went wrong, we want to be able to discern that case,
4414 as opposed to the case where there was no SCOPE due to the type
4415 of expression being dependent. */
4416 if (!scope)
4417 scope = error_mark_node;
4418 /* If the SCOPE was erroneous, make the various semantic analysis
4419 functions exit quickly -- and without issuing additional error
4420 messages. */
4421 if (scope == error_mark_node)
4422 postfix_expression = error_mark_node;
4423 }
4424
17a27b4f
MM
4425 /* Assume this expression is not a pseudo-destructor access. */
4426 pseudo_destructor_p = false;
4427
4428 /* If the SCOPE is a scalar type, then, if this is a valid program,
4429 we must be looking at a pseudo-destructor-name. */
4430 if (scope && SCALAR_TYPE_P (scope))
7a3ea201 4431 {
17a27b4f
MM
4432 tree s;
4433 tree type;
4434
4435 cp_parser_parse_tentatively (parser);
4436 /* Parse the pseudo-destructor-name. */
4437 s = NULL_TREE;
4438 cp_parser_pseudo_destructor_name (parser, &s, &type);
4439 if (cp_parser_parse_definitely (parser))
4440 {
4441 pseudo_destructor_p = true;
4442 postfix_expression
4443 = finish_pseudo_destructor_expr (postfix_expression,
4444 s, TREE_TYPE (type));
4445 }
4446 }
4447
4448 if (!pseudo_destructor_p)
4449 {
4450 /* If the SCOPE is not a scalar type, we are looking at an
4451 ordinary class member access expression, rather than a
4452 pseudo-destructor-name. */
02ed62dd 4453 bool template_p;
7a3ea201 4454 /* Parse the id-expression. */
02ed62dd
MM
4455 name = (cp_parser_id_expression
4456 (parser,
4457 cp_parser_optional_template_keyword (parser),
4458 /*check_dependency_p=*/true,
4459 &template_p,
4460 /*declarator_p=*/false));
7a3ea201
RH
4461 /* In general, build a SCOPE_REF if the member name is qualified.
4462 However, if the name was not dependent and has already been
4463 resolved; there is no need to build the SCOPE_REF. For example;
4464
0cbd7506
MS
4465 struct X { void f(); };
4466 template <typename T> void f(T* t) { t->X::f(); }
7a3ea201
RH
4467
4468 Even though "t" is dependent, "X::f" is not and has been resolved
4469 to a BASELINK; there is no need to include scope information. */
4470
4471 /* But we do need to remember that there was an explicit scope for
4472 virtual function calls. */
4473 if (parser->scope)
4474 *idk = CP_ID_KIND_QUALIFIED;
4475
fc6a28d7
MM
4476 /* If the name is a template-id that names a type, we will get a
4477 TYPE_DECL here. That is invalid code. */
4478 if (TREE_CODE (name) == TYPE_DECL)
7a3ea201 4479 {
fc6a28d7
MM
4480 error ("invalid use of %qD", name);
4481 postfix_expression = error_mark_node;
4482 }
4483 else
4484 {
4485 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4486 {
02ed62dd
MM
4487 name = build_qualified_name (/*type=*/NULL_TREE,
4488 parser->scope,
4489 name,
4490 template_p);
fc6a28d7
MM
4491 parser->scope = NULL_TREE;
4492 parser->qualifying_scope = NULL_TREE;
4493 parser->object_scope = NULL_TREE;
4494 }
4495 if (scope && name && BASELINK_P (name))
4496 adjust_result_of_qualified_name_lookup
4497 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4498 postfix_expression
02ed62dd
MM
4499 = finish_class_member_access_expr (postfix_expression, name,
4500 template_p);
7a3ea201 4501 }
7a3ea201 4502 }
7a3ea201
RH
4503
4504 /* We no longer need to look up names in the scope of the object on
4505 the left-hand side of the `.' or `->' operator. */
4506 parser->context->object_type = NULL_TREE;
4507
4508 /* Outside of offsetof, these operators may not appear in
4509 constant-expressions. */
4510 if (!for_offsetof
98ca843c 4511 && (cp_parser_non_integral_constant_expression
7a3ea201
RH
4512 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4513 postfix_expression = error_mark_node;
4514
4515 return postfix_expression;
4516}
4517
7efa3e22 4518/* Parse a parenthesized expression-list.
a723baf1
MM
4519
4520 expression-list:
4521 assignment-expression
4522 expression-list, assignment-expression
4523
7efa3e22
NS
4524 attribute-list:
4525 expression-list
4526 identifier
4527 identifier, expression-list
4528
93678513
MM
4529 CAST_P is true if this expression is the target of a cast.
4530
a723baf1
MM
4531 Returns a TREE_LIST. The TREE_VALUE of each node is a
4532 representation of an assignment-expression. Note that a TREE_LIST
7efa3e22
NS
4533 is returned even if there is only a single expression in the list.
4534 error_mark_node is returned if the ( and or ) are
4535 missing. NULL_TREE is returned on no expressions. The parentheses
4536 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
39703eb9
MM
4537 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4538 indicates whether or not all of the expressions in the list were
4539 constant. */
a723baf1
MM
4540
4541static tree
21526606 4542cp_parser_parenthesized_expression_list (cp_parser* parser,
39703eb9 4543 bool is_attribute_list,
93678513 4544 bool cast_p,
39703eb9 4545 bool *non_constant_p)
a723baf1
MM
4546{
4547 tree expression_list = NULL_TREE;
1ed3dfd5 4548 bool fold_expr_p = is_attribute_list;
7efa3e22 4549 tree identifier = NULL_TREE;
39703eb9
MM
4550
4551 /* Assume all the expressions will be constant. */
4552 if (non_constant_p)
4553 *non_constant_p = false;
4554
7efa3e22
NS
4555 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4556 return error_mark_node;
21526606 4557
a723baf1 4558 /* Consume expressions until there are no more. */
7efa3e22
NS
4559 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4560 while (true)
4561 {
4562 tree expr;
21526606 4563
7efa3e22
NS
4564 /* At the beginning of attribute lists, check to see if the
4565 next token is an identifier. */
4566 if (is_attribute_list
4567 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4568 {
4569 cp_token *token;
21526606 4570
7efa3e22
NS
4571 /* Consume the identifier. */
4572 token = cp_lexer_consume_token (parser->lexer);
4573 /* Save the identifier. */
4574 identifier = token->value;
4575 }
4576 else
4577 {
4578 /* Parse the next assignment-expression. */
39703eb9
MM
4579 if (non_constant_p)
4580 {
4581 bool expr_non_constant_p;
21526606 4582 expr = (cp_parser_constant_expression
39703eb9
MM
4583 (parser, /*allow_non_constant_p=*/true,
4584 &expr_non_constant_p));
4585 if (expr_non_constant_p)
4586 *non_constant_p = true;
4587 }
4588 else
93678513 4589 expr = cp_parser_assignment_expression (parser, cast_p);
a723baf1 4590
1ed3dfd5
GB
4591 if (fold_expr_p)
4592 expr = fold_non_dependent_expr (expr);
4593
7efa3e22
NS
4594 /* Add it to the list. We add error_mark_node
4595 expressions to the list, so that we can still tell if
4596 the correct form for a parenthesized expression-list
4597 is found. That gives better errors. */
4598 expression_list = tree_cons (NULL_TREE, expr, expression_list);
a723baf1 4599
7efa3e22
NS
4600 if (expr == error_mark_node)
4601 goto skip_comma;
4602 }
a723baf1 4603
7efa3e22
NS
4604 /* After the first item, attribute lists look the same as
4605 expression lists. */
4606 is_attribute_list = false;
21526606 4607
7efa3e22
NS
4608 get_comma:;
4609 /* If the next token isn't a `,', then we are done. */
4610 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4611 break;
4612
4613 /* Otherwise, consume the `,' and keep going. */
4614 cp_lexer_consume_token (parser->lexer);
4615 }
21526606 4616
7efa3e22
NS
4617 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4618 {
4619 int ending;
21526606 4620
7efa3e22
NS
4621 skip_comma:;
4622 /* We try and resync to an unnested comma, as that will give the
4623 user better diagnostics. */
21526606
EC
4624 ending = cp_parser_skip_to_closing_parenthesis (parser,
4625 /*recovering=*/true,
4bb8ca28 4626 /*or_comma=*/true,
a668c6ad 4627 /*consume_paren=*/true);
7efa3e22
NS
4628 if (ending < 0)
4629 goto get_comma;
4630 if (!ending)
4631 return error_mark_node;
a723baf1
MM
4632 }
4633
4634 /* We built up the list in reverse order so we must reverse it now. */
7efa3e22
NS
4635 expression_list = nreverse (expression_list);
4636 if (identifier)
4637 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
21526606 4638
7efa3e22 4639 return expression_list;
a723baf1
MM
4640}
4641
4642/* Parse a pseudo-destructor-name.
4643
4644 pseudo-destructor-name:
4645 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4646 :: [opt] nested-name-specifier template template-id :: ~ type-name
4647 :: [opt] nested-name-specifier [opt] ~ type-name
4648
4649 If either of the first two productions is used, sets *SCOPE to the
4650 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4651 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
d6e57462 4652 or ERROR_MARK_NODE if the parse fails. */
a723baf1
MM
4653
4654static void
21526606 4655cp_parser_pseudo_destructor_name (cp_parser* parser,
0cbd7506
MS
4656 tree* scope,
4657 tree* type)
a723baf1
MM
4658{
4659 bool nested_name_specifier_p;
4660
b14454ba
MM
4661 /* Assume that things will not work out. */
4662 *type = error_mark_node;
4663
a723baf1
MM
4664 /* Look for the optional `::' operator. */
4665 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4666 /* Look for the optional nested-name-specifier. */
21526606 4667 nested_name_specifier_p
a723baf1
MM
4668 = (cp_parser_nested_name_specifier_opt (parser,
4669 /*typename_keyword_p=*/false,
4670 /*check_dependency_p=*/true,
a668c6ad 4671 /*type_p=*/false,
21526606 4672 /*is_declaration=*/true)
a723baf1
MM
4673 != NULL_TREE);
4674 /* Now, if we saw a nested-name-specifier, we might be doing the
4675 second production. */
21526606 4676 if (nested_name_specifier_p
a723baf1
MM
4677 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4678 {
4679 /* Consume the `template' keyword. */
4680 cp_lexer_consume_token (parser->lexer);
4681 /* Parse the template-id. */
21526606 4682 cp_parser_template_id (parser,
a723baf1 4683 /*template_keyword_p=*/true,
a668c6ad
MM
4684 /*check_dependency_p=*/false,
4685 /*is_declaration=*/true);
a723baf1
MM
4686 /* Look for the `::' token. */
4687 cp_parser_require (parser, CPP_SCOPE, "`::'");
4688 }
4689 /* If the next token is not a `~', then there might be some
9bcb9aae 4690 additional qualification. */
a723baf1
MM
4691 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4692 {
4693 /* Look for the type-name. */
4694 *scope = TREE_TYPE (cp_parser_type_name (parser));
d6e57462 4695
b14454ba
MM
4696 if (*scope == error_mark_node)
4697 return;
4698
4699 /* If we don't have ::~, then something has gone wrong. Since
4700 the only caller of this function is looking for something
4701 after `.' or `->' after a scalar type, most likely the
4702 program is trying to get a member of a non-aggregate
4703 type. */
4704 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
d6e57462
ILT
4705 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4706 {
4707 cp_parser_error (parser, "request for member of non-aggregate type");
d6e57462
ILT
4708 return;
4709 }
4710
a723baf1
MM
4711 /* Look for the `::' token. */
4712 cp_parser_require (parser, CPP_SCOPE, "`::'");
4713 }
4714 else
4715 *scope = NULL_TREE;
4716
4717 /* Look for the `~'. */
4718 cp_parser_require (parser, CPP_COMPL, "`~'");
4719 /* Look for the type-name again. We are not responsible for
4720 checking that it matches the first type-name. */
4721 *type = cp_parser_type_name (parser);
4722}
4723
4724/* Parse a unary-expression.
4725
4726 unary-expression:
4727 postfix-expression
4728 ++ cast-expression
4729 -- cast-expression
4730 unary-operator cast-expression
4731 sizeof unary-expression
4732 sizeof ( type-id )
4733 new-expression
4734 delete-expression
4735
4736 GNU Extensions:
4737
4738 unary-expression:
4739 __extension__ cast-expression
4740 __alignof__ unary-expression
4741 __alignof__ ( type-id )
4742 __real__ cast-expression
4743 __imag__ cast-expression
4744 && identifier
4745
4746 ADDRESS_P is true iff the unary-expression is appearing as the
93678513
MM
4747 operand of the `&' operator. CAST_P is true if this expression is
4748 the target of a cast.
a723baf1 4749
34cd5ae7 4750 Returns a representation of the expression. */
a723baf1
MM
4751
4752static tree
93678513 4753cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
a723baf1
MM
4754{
4755 cp_token *token;
4756 enum tree_code unary_operator;
4757
4758 /* Peek at the next token. */
4759 token = cp_lexer_peek_token (parser->lexer);
4760 /* Some keywords give away the kind of expression. */
4761 if (token->type == CPP_KEYWORD)
4762 {
4763 enum rid keyword = token->keyword;
4764
4765 switch (keyword)
4766 {
4767 case RID_ALIGNOF:
a723baf1
MM
4768 case RID_SIZEOF:
4769 {
4770 tree operand;
7a18b933 4771 enum tree_code op;
21526606 4772
7a18b933
NS
4773 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4774 /* Consume the token. */
a723baf1
MM
4775 cp_lexer_consume_token (parser->lexer);
4776 /* Parse the operand. */
4777 operand = cp_parser_sizeof_operand (parser, keyword);
4778
7a18b933
NS
4779 if (TYPE_P (operand))
4780 return cxx_sizeof_or_alignof_type (operand, op, true);
a723baf1 4781 else
7a18b933 4782 return cxx_sizeof_or_alignof_expr (operand, op);
a723baf1
MM
4783 }
4784
4785 case RID_NEW:
4786 return cp_parser_new_expression (parser);
4787
4788 case RID_DELETE:
4789 return cp_parser_delete_expression (parser);
21526606 4790
a723baf1
MM
4791 case RID_EXTENSION:
4792 {
4793 /* The saved value of the PEDANTIC flag. */
4794 int saved_pedantic;
4795 tree expr;
4796
4797 /* Save away the PEDANTIC flag. */
4798 cp_parser_extension_opt (parser, &saved_pedantic);
4799 /* Parse the cast-expression. */
d6b4ea85 4800 expr = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4801 /* Restore the PEDANTIC flag. */
4802 pedantic = saved_pedantic;
4803
4804 return expr;
4805 }
4806
4807 case RID_REALPART:
4808 case RID_IMAGPART:
4809 {
4810 tree expression;
4811
4812 /* Consume the `__real__' or `__imag__' token. */
4813 cp_lexer_consume_token (parser->lexer);
4814 /* Parse the cast-expression. */
d6b4ea85 4815 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4816 /* Create the complete representation. */
4817 return build_x_unary_op ((keyword == RID_REALPART
4818 ? REALPART_EXPR : IMAGPART_EXPR),
4819 expression);
4820 }
4821 break;
4822
4823 default:
4824 break;
4825 }
4826 }
4827
4828 /* Look for the `:: new' and `:: delete', which also signal the
4829 beginning of a new-expression, or delete-expression,
4830 respectively. If the next token is `::', then it might be one of
4831 these. */
4832 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4833 {
4834 enum rid keyword;
4835
4836 /* See if the token after the `::' is one of the keywords in
4837 which we're interested. */
4838 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4839 /* If it's `new', we have a new-expression. */
4840 if (keyword == RID_NEW)
4841 return cp_parser_new_expression (parser);
4842 /* Similarly, for `delete'. */
4843 else if (keyword == RID_DELETE)
4844 return cp_parser_delete_expression (parser);
4845 }
4846
4847 /* Look for a unary operator. */
4848 unary_operator = cp_parser_unary_operator (token);
4849 /* The `++' and `--' operators can be handled similarly, even though
4850 they are not technically unary-operators in the grammar. */
4851 if (unary_operator == ERROR_MARK)
4852 {
4853 if (token->type == CPP_PLUS_PLUS)
4854 unary_operator = PREINCREMENT_EXPR;
4855 else if (token->type == CPP_MINUS_MINUS)
4856 unary_operator = PREDECREMENT_EXPR;
4857 /* Handle the GNU address-of-label extension. */
4858 else if (cp_parser_allow_gnu_extensions_p (parser)
4859 && token->type == CPP_AND_AND)
4860 {
4861 tree identifier;
4862
4863 /* Consume the '&&' token. */
4864 cp_lexer_consume_token (parser->lexer);
4865 /* Look for the identifier. */
4866 identifier = cp_parser_identifier (parser);
4867 /* Create an expression representing the address. */
4868 return finish_label_address_expr (identifier);
4869 }
4870 }
4871 if (unary_operator != ERROR_MARK)
4872 {
4873 tree cast_expression;
a5ac3982
MM
4874 tree expression = error_mark_node;
4875 const char *non_constant_p = NULL;
a723baf1
MM
4876
4877 /* Consume the operator token. */
4878 token = cp_lexer_consume_token (parser->lexer);
4879 /* Parse the cast-expression. */
21526606 4880 cast_expression
c8094d83 4881 = cp_parser_cast_expression (parser,
93678513
MM
4882 unary_operator == ADDR_EXPR,
4883 /*cast_p=*/false);
a723baf1
MM
4884 /* Now, build an appropriate representation. */
4885 switch (unary_operator)
4886 {
4887 case INDIRECT_REF:
a5ac3982
MM
4888 non_constant_p = "`*'";
4889 expression = build_x_indirect_ref (cast_expression, "unary *");
4890 break;
4891
a723baf1 4892 case ADDR_EXPR:
7a3ea201 4893 non_constant_p = "`&'";
a5ac3982 4894 /* Fall through. */
d17811fd 4895 case BIT_NOT_EXPR:
a5ac3982
MM
4896 expression = build_x_unary_op (unary_operator, cast_expression);
4897 break;
4898
14d22dd6
MM
4899 case PREINCREMENT_EXPR:
4900 case PREDECREMENT_EXPR:
a5ac3982
MM
4901 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4902 ? "`++'" : "`--'");
14d22dd6 4903 /* Fall through. */
392e3d51 4904 case UNARY_PLUS_EXPR:
a723baf1
MM
4905 case NEGATE_EXPR:
4906 case TRUTH_NOT_EXPR:
a5ac3982
MM
4907 expression = finish_unary_op_expr (unary_operator, cast_expression);
4908 break;
a723baf1 4909
a723baf1 4910 default:
315fb5db 4911 gcc_unreachable ();
a723baf1 4912 }
a5ac3982 4913
98ca843c 4914 if (non_constant_p
625cbf93
MM
4915 && cp_parser_non_integral_constant_expression (parser,
4916 non_constant_p))
4917 expression = error_mark_node;
a5ac3982
MM
4918
4919 return expression;
a723baf1
MM
4920 }
4921
93678513 4922 return cp_parser_postfix_expression (parser, address_p, cast_p);
a723baf1
MM
4923}
4924
4925/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4926 unary-operator, the corresponding tree code is returned. */
4927
4928static enum tree_code
94edc4ab 4929cp_parser_unary_operator (cp_token* token)
a723baf1
MM
4930{
4931 switch (token->type)
4932 {
4933 case CPP_MULT:
4934 return INDIRECT_REF;
4935
4936 case CPP_AND:
4937 return ADDR_EXPR;
4938
4939 case CPP_PLUS:
392e3d51 4940 return UNARY_PLUS_EXPR;
a723baf1
MM
4941
4942 case CPP_MINUS:
4943 return NEGATE_EXPR;
4944
4945 case CPP_NOT:
4946 return TRUTH_NOT_EXPR;
21526606 4947
a723baf1
MM
4948 case CPP_COMPL:
4949 return BIT_NOT_EXPR;
4950
4951 default:
4952 return ERROR_MARK;
4953 }
4954}
4955
4956/* Parse a new-expression.
4957
ca099ac8 4958 new-expression:
a723baf1
MM
4959 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4960 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4961
4962 Returns a representation of the expression. */
4963
4964static tree
94edc4ab 4965cp_parser_new_expression (cp_parser* parser)
a723baf1
MM
4966{
4967 bool global_scope_p;
4968 tree placement;
4969 tree type;
4970 tree initializer;
058b15c1 4971 tree nelts;
a723baf1
MM
4972
4973 /* Look for the optional `::' operator. */
21526606 4974 global_scope_p
a723baf1
MM
4975 = (cp_parser_global_scope_opt (parser,
4976 /*current_scope_valid_p=*/false)
4977 != NULL_TREE);
4978 /* Look for the `new' operator. */
4979 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4980 /* There's no easy way to tell a new-placement from the
4981 `( type-id )' construct. */
4982 cp_parser_parse_tentatively (parser);
4983 /* Look for a new-placement. */
4984 placement = cp_parser_new_placement (parser);
4985 /* If that didn't work out, there's no new-placement. */
4986 if (!cp_parser_parse_definitely (parser))
4987 placement = NULL_TREE;
4988
4989 /* If the next token is a `(', then we have a parenthesized
4990 type-id. */
4991 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4992 {
4993 /* Consume the `('. */
4994 cp_lexer_consume_token (parser->lexer);
4995 /* Parse the type-id. */
4996 type = cp_parser_type_id (parser);
4997 /* Look for the closing `)'. */
4998 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
98ca843c 4999 /* There should not be a direct-new-declarator in this production,
0cbd7506 5000 but GCC used to allowed this, so we check and emit a sensible error
063e900f
GB
5001 message for this case. */
5002 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
0da99d4e
GB
5003 {
5004 error ("array bound forbidden after parenthesized type-id");
5005 inform ("try removing the parentheses around the type-id");
063e900f
GB
5006 cp_parser_direct_new_declarator (parser);
5007 }
17a27b4f 5008 nelts = NULL_TREE;
a723baf1
MM
5009 }
5010 /* Otherwise, there must be a new-type-id. */
5011 else
058b15c1 5012 type = cp_parser_new_type_id (parser, &nelts);
a723baf1
MM
5013
5014 /* If the next token is a `(', then we have a new-initializer. */
5015 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5016 initializer = cp_parser_new_initializer (parser);
5017 else
5018 initializer = NULL_TREE;
5019
625cbf93
MM
5020 /* A new-expression may not appear in an integral constant
5021 expression. */
5022 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5023 return error_mark_node;
5024
a723baf1 5025 /* Create a representation of the new-expression. */
058b15c1 5026 return build_new (placement, type, nelts, initializer, global_scope_p);
a723baf1
MM
5027}
5028
5029/* Parse a new-placement.
5030
5031 new-placement:
5032 ( expression-list )
5033
5034 Returns the same representation as for an expression-list. */
5035
5036static tree
94edc4ab 5037cp_parser_new_placement (cp_parser* parser)
a723baf1
MM
5038{
5039 tree expression_list;
5040
a723baf1 5041 /* Parse the expression-list. */
21526606 5042 expression_list = (cp_parser_parenthesized_expression_list
93678513
MM
5043 (parser, false, /*cast_p=*/false,
5044 /*non_constant_p=*/NULL));
a723baf1
MM
5045
5046 return expression_list;
5047}
5048
5049/* Parse a new-type-id.
5050
5051 new-type-id:
5052 type-specifier-seq new-declarator [opt]
5053
058b15c1
MM
5054 Returns the TYPE allocated. If the new-type-id indicates an array
5055 type, *NELTS is set to the number of elements in the last array
5056 bound; the TYPE will not include the last array bound. */
a723baf1
MM
5057
5058static tree
058b15c1 5059cp_parser_new_type_id (cp_parser* parser, tree *nelts)
a723baf1 5060{
62d1db17 5061 cp_decl_specifier_seq type_specifier_seq;
058b15c1
MM
5062 cp_declarator *new_declarator;
5063 cp_declarator *declarator;
5064 cp_declarator *outer_declarator;
a723baf1 5065 const char *saved_message;
058b15c1 5066 tree type;
a723baf1
MM
5067
5068 /* The type-specifier sequence must not contain type definitions.
5069 (It cannot contain declarations of new types either, but if they
5070 are not definitions we will catch that because they are not
5071 complete.) */
5072 saved_message = parser->type_definition_forbidden_message;
5073 parser->type_definition_forbidden_message
5074 = "types may not be defined in a new-type-id";
5075 /* Parse the type-specifier-seq. */
d4113656
MM
5076 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5077 &type_specifier_seq);
a723baf1
MM
5078 /* Restore the old message. */
5079 parser->type_definition_forbidden_message = saved_message;
5080 /* Parse the new-declarator. */
058b15c1
MM
5081 new_declarator = cp_parser_new_declarator_opt (parser);
5082
5083 /* Determine the number of elements in the last array dimension, if
5084 any. */
5085 *nelts = NULL_TREE;
5086 /* Skip down to the last array dimension. */
5087 declarator = new_declarator;
5088 outer_declarator = NULL;
5089 while (declarator && (declarator->kind == cdk_pointer
5090 || declarator->kind == cdk_ptrmem))
5091 {
5092 outer_declarator = declarator;
5093 declarator = declarator->declarator;
5094 }
98ca843c 5095 while (declarator
058b15c1
MM
5096 && declarator->kind == cdk_array
5097 && declarator->declarator
5098 && declarator->declarator->kind == cdk_array)
5099 {
5100 outer_declarator = declarator;
5101 declarator = declarator->declarator;
5102 }
98ca843c 5103
058b15c1
MM
5104 if (declarator && declarator->kind == cdk_array)
5105 {
5106 *nelts = declarator->u.array.bounds;
5107 if (*nelts == error_mark_node)
5108 *nelts = integer_one_node;
c8094d83 5109
058b15c1
MM
5110 if (outer_declarator)
5111 outer_declarator->declarator = declarator->declarator;
5112 else
5113 new_declarator = NULL;
5114 }
a723baf1 5115
62d1db17 5116 type = groktypename (&type_specifier_seq, new_declarator);
058b15c1
MM
5117 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5118 {
5119 *nelts = array_type_nelts_top (type);
5120 type = TREE_TYPE (type);
5121 }
5122 return type;
a723baf1
MM
5123}
5124
5125/* Parse an (optional) new-declarator.
5126
5127 new-declarator:
5128 ptr-operator new-declarator [opt]
5129 direct-new-declarator
5130
058b15c1 5131 Returns the declarator. */
a723baf1 5132
058b15c1 5133static cp_declarator *
94edc4ab 5134cp_parser_new_declarator_opt (cp_parser* parser)
a723baf1
MM
5135{
5136 enum tree_code code;
5137 tree type;
3c01e5df 5138 cp_cv_quals cv_quals;
a723baf1
MM
5139
5140 /* We don't know if there's a ptr-operator next, or not. */
5141 cp_parser_parse_tentatively (parser);
5142 /* Look for a ptr-operator. */
3c01e5df 5143 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
a723baf1
MM
5144 /* If that worked, look for more new-declarators. */
5145 if (cp_parser_parse_definitely (parser))
5146 {
058b15c1 5147 cp_declarator *declarator;
a723baf1
MM
5148
5149 /* Parse another optional declarator. */
5150 declarator = cp_parser_new_declarator_opt (parser);
5151
5152 /* Create the representation of the declarator. */
058b15c1 5153 if (type)
3c01e5df 5154 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
058b15c1 5155 else if (code == INDIRECT_REF)
3c01e5df 5156 declarator = make_pointer_declarator (cv_quals, declarator);
a723baf1 5157 else
3c01e5df 5158 declarator = make_reference_declarator (cv_quals, declarator);
a723baf1 5159
a723baf1
MM
5160 return declarator;
5161 }
5162
5163 /* If the next token is a `[', there is a direct-new-declarator. */
5164 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5165 return cp_parser_direct_new_declarator (parser);
5166
058b15c1 5167 return NULL;
a723baf1
MM
5168}
5169
5170/* Parse a direct-new-declarator.
5171
5172 direct-new-declarator:
5173 [ expression ]
21526606 5174 direct-new-declarator [constant-expression]
a723baf1 5175
058b15c1 5176 */
a723baf1 5177
058b15c1 5178static cp_declarator *
94edc4ab 5179cp_parser_direct_new_declarator (cp_parser* parser)
a723baf1 5180{
058b15c1 5181 cp_declarator *declarator = NULL;
a723baf1
MM
5182
5183 while (true)
5184 {
5185 tree expression;
5186
5187 /* Look for the opening `['. */
5188 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5189 /* The first expression is not required to be constant. */
5190 if (!declarator)
5191 {
93678513 5192 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
5193 /* The standard requires that the expression have integral
5194 type. DR 74 adds enumeration types. We believe that the
5195 real intent is that these expressions be handled like the
5196 expression in a `switch' condition, which also allows
5197 classes with a single conversion to integral or
5198 enumeration type. */
5199 if (!processing_template_decl)
5200 {
21526606 5201 expression
a723baf1
MM
5202 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5203 expression,
b746c5dc 5204 /*complain=*/true);
a723baf1
MM
5205 if (!expression)
5206 {
2a13a625 5207 error ("expression in new-declarator must have integral "
0cbd7506 5208 "or enumeration type");
a723baf1
MM
5209 expression = error_mark_node;
5210 }
5211 }
5212 }
5213 /* But all the other expressions must be. */
5214 else
21526606
EC
5215 expression
5216 = cp_parser_constant_expression (parser,
14d22dd6
MM
5217 /*allow_non_constant=*/false,
5218 NULL);
a723baf1
MM
5219 /* Look for the closing `]'. */
5220 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5221
5222 /* Add this bound to the declarator. */
058b15c1 5223 declarator = make_array_declarator (declarator, expression);
a723baf1
MM
5224
5225 /* If the next token is not a `[', then there are no more
5226 bounds. */
5227 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5228 break;
5229 }
5230
5231 return declarator;
5232}
5233
5234/* Parse a new-initializer.
5235
5236 new-initializer:
5237 ( expression-list [opt] )
5238
34cd5ae7 5239 Returns a representation of the expression-list. If there is no
a723baf1
MM
5240 expression-list, VOID_ZERO_NODE is returned. */
5241
5242static tree
94edc4ab 5243cp_parser_new_initializer (cp_parser* parser)
a723baf1
MM
5244{
5245 tree expression_list;
5246
21526606 5247 expression_list = (cp_parser_parenthesized_expression_list
93678513
MM
5248 (parser, false, /*cast_p=*/false,
5249 /*non_constant_p=*/NULL));
7efa3e22 5250 if (!expression_list)
a723baf1 5251 expression_list = void_zero_node;
a723baf1
MM
5252
5253 return expression_list;
5254}
5255
5256/* Parse a delete-expression.
5257
5258 delete-expression:
5259 :: [opt] delete cast-expression
5260 :: [opt] delete [ ] cast-expression
5261
5262 Returns a representation of the expression. */
5263
5264static tree
94edc4ab 5265cp_parser_delete_expression (cp_parser* parser)
a723baf1
MM
5266{
5267 bool global_scope_p;
5268 bool array_p;
5269 tree expression;
5270
5271 /* Look for the optional `::' operator. */
5272 global_scope_p
5273 = (cp_parser_global_scope_opt (parser,
5274 /*current_scope_valid_p=*/false)
5275 != NULL_TREE);
5276 /* Look for the `delete' keyword. */
5277 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5278 /* See if the array syntax is in use. */
5279 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5280 {
5281 /* Consume the `[' token. */
5282 cp_lexer_consume_token (parser->lexer);
5283 /* Look for the `]' token. */
5284 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5285 /* Remember that this is the `[]' construct. */
5286 array_p = true;
5287 }
5288 else
5289 array_p = false;
5290
5291 /* Parse the cast-expression. */
d6b4ea85 5292 expression = cp_parser_simple_cast_expression (parser);
a723baf1 5293
625cbf93
MM
5294 /* A delete-expression may not appear in an integral constant
5295 expression. */
5296 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5297 return error_mark_node;
5298
a723baf1
MM
5299 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5300}
5301
5302/* Parse a cast-expression.
5303
5304 cast-expression:
5305 unary-expression
5306 ( type-id ) cast-expression
5307
93678513
MM
5308 ADDRESS_P is true iff the unary-expression is appearing as the
5309 operand of the `&' operator. CAST_P is true if this expression is
5310 the target of a cast.
5311
a723baf1
MM
5312 Returns a representation of the expression. */
5313
5314static tree
93678513 5315cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
a723baf1
MM
5316{
5317 /* If it's a `(', then we might be looking at a cast. */
5318 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5319 {
5320 tree type = NULL_TREE;
5321 tree expr = NULL_TREE;
5322 bool compound_literal_p;
5323 const char *saved_message;
5324
5325 /* There's no way to know yet whether or not this is a cast.
5326 For example, `(int (3))' is a unary-expression, while `(int)
5327 3' is a cast. So, we resort to parsing tentatively. */
5328 cp_parser_parse_tentatively (parser);
5329 /* Types may not be defined in a cast. */
5330 saved_message = parser->type_definition_forbidden_message;
5331 parser->type_definition_forbidden_message
5332 = "types may not be defined in casts";
5333 /* Consume the `('. */
5334 cp_lexer_consume_token (parser->lexer);
5335 /* A very tricky bit is that `(struct S) { 3 }' is a
5336 compound-literal (which we permit in C++ as an extension).
5337 But, that construct is not a cast-expression -- it is a
5338 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5339 is legal; if the compound-literal were a cast-expression,
5340 you'd need an extra set of parentheses.) But, if we parse
5341 the type-id, and it happens to be a class-specifier, then we
5342 will commit to the parse at that point, because we cannot
5343 undo the action that is done when creating a new class. So,
21526606 5344 then we cannot back up and do a postfix-expression.
a723baf1
MM
5345
5346 Therefore, we scan ahead to the closing `)', and check to see
5347 if the token after the `)' is a `{'. If so, we are not
21526606 5348 looking at a cast-expression.
a723baf1
MM
5349
5350 Save tokens so that we can put them back. */
5351 cp_lexer_save_tokens (parser->lexer);
5352 /* Skip tokens until the next token is a closing parenthesis.
5353 If we find the closing `)', and the next token is a `{', then
5354 we are looking at a compound-literal. */
21526606 5355 compound_literal_p
a668c6ad
MM
5356 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5357 /*consume_paren=*/true)
a723baf1
MM
5358 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5359 /* Roll back the tokens we skipped. */
5360 cp_lexer_rollback_tokens (parser->lexer);
5361 /* If we were looking at a compound-literal, simulate an error
5362 so that the call to cp_parser_parse_definitely below will
5363 fail. */
5364 if (compound_literal_p)
5365 cp_parser_simulate_error (parser);
5366 else
5367 {
4f8163b1
MM
5368 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5369 parser->in_type_id_in_expr_p = true;
a723baf1
MM
5370 /* Look for the type-id. */
5371 type = cp_parser_type_id (parser);
5372 /* Look for the closing `)'. */
5373 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4f8163b1 5374 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
5375 }
5376
5377 /* Restore the saved message. */
5378 parser->type_definition_forbidden_message = saved_message;
5379
bbaab916 5380 /* If ok so far, parse the dependent expression. We cannot be
0cbd7506
MS
5381 sure it is a cast. Consider `(T ())'. It is a parenthesized
5382 ctor of T, but looks like a cast to function returning T
5383 without a dependent expression. */
bbaab916 5384 if (!cp_parser_error_occurred (parser))
c8094d83 5385 expr = cp_parser_cast_expression (parser,
93678513
MM
5386 /*address_p=*/false,
5387 /*cast_p=*/true);
bbaab916 5388
a723baf1
MM
5389 if (cp_parser_parse_definitely (parser))
5390 {
a723baf1 5391 /* Warn about old-style casts, if so requested. */
21526606
EC
5392 if (warn_old_style_cast
5393 && !in_system_header
5394 && !VOID_TYPE_P (type)
a723baf1 5395 && current_lang_name != lang_name_c)
d4ee4d25 5396 warning (0, "use of old-style cast");
14d22dd6
MM
5397
5398 /* Only type conversions to integral or enumeration types
5399 can be used in constant-expressions. */
67c03833 5400 if (parser->integral_constant_expression_p
14d22dd6 5401 && !dependent_type_p (type)
625cbf93 5402 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
98ca843c 5403 && (cp_parser_non_integral_constant_expression
625cbf93
MM
5404 (parser,
5405 "a cast to a type other than an integral or "
5406 "enumeration type")))
5407 return error_mark_node;
5408
a723baf1
MM
5409 /* Perform the cast. */
5410 expr = build_c_cast (type, expr);
bbaab916 5411 return expr;
a723baf1 5412 }
a723baf1
MM
5413 }
5414
5415 /* If we get here, then it's not a cast, so it must be a
5416 unary-expression. */
93678513 5417 return cp_parser_unary_expression (parser, address_p, cast_p);
a723baf1
MM
5418}
5419
b8b94c5b 5420/* Parse a binary expression of the general form:
a723baf1
MM
5421
5422 pm-expression:
5423 cast-expression
5424 pm-expression .* cast-expression
5425 pm-expression ->* cast-expression
5426
77077b39 5427 multiplicative-expression:
a723baf1
MM
5428 pm-expression
5429 multiplicative-expression * pm-expression
5430 multiplicative-expression / pm-expression
5431 multiplicative-expression % pm-expression
5432
a723baf1
MM
5433 additive-expression:
5434 multiplicative-expression
5435 additive-expression + multiplicative-expression
5436 additive-expression - multiplicative-expression
5437
a723baf1
MM
5438 shift-expression:
5439 additive-expression
5440 shift-expression << additive-expression
5441 shift-expression >> additive-expression
5442
a723baf1
MM
5443 relational-expression:
5444 shift-expression
5445 relational-expression < shift-expression
5446 relational-expression > shift-expression
5447 relational-expression <= shift-expression
5448 relational-expression >= shift-expression
5449
b8b94c5b 5450 GNU Extension:
c8094d83 5451
a723baf1
MM
5452 relational-expression:
5453 relational-expression <? shift-expression
5454 relational-expression >? shift-expression
5455
a723baf1
MM
5456 equality-expression:
5457 relational-expression
5458 equality-expression == relational-expression
5459 equality-expression != relational-expression
5460
a723baf1
MM
5461 and-expression:
5462 equality-expression
5463 and-expression & equality-expression
5464
a723baf1
MM
5465 exclusive-or-expression:
5466 and-expression
5467 exclusive-or-expression ^ and-expression
5468
b8b94c5b
PB
5469 inclusive-or-expression:
5470 exclusive-or-expression
5471 inclusive-or-expression | exclusive-or-expression
a723baf1 5472
b8b94c5b
PB
5473 logical-and-expression:
5474 inclusive-or-expression
5475 logical-and-expression && inclusive-or-expression
a723baf1 5476
b8b94c5b
PB
5477 logical-or-expression:
5478 logical-and-expression
5479 logical-or-expression || logical-and-expression
a723baf1 5480
b8b94c5b 5481 All these are implemented with a single function like:
a723baf1 5482
b8b94c5b
PB
5483 binary-expression:
5484 simple-cast-expression
5485 binary-expression <token> binary-expression
a723baf1 5486
93678513
MM
5487 CAST_P is true if this expression is the target of a cast.
5488
b8b94c5b
PB
5489 The binops_by_token map is used to get the tree codes for each <token> type.
5490 binary-expressions are associated according to a precedence table. */
a723baf1 5491
b8b94c5b
PB
5492#define TOKEN_PRECEDENCE(token) \
5493 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5494 ? PREC_NOT_OPERATOR \
5495 : binops_by_token[token->type].prec)
a723baf1
MM
5496
5497static tree
93678513 5498cp_parser_binary_expression (cp_parser* parser, bool cast_p)
a723baf1 5499{
b8b94c5b
PB
5500 cp_parser_expression_stack stack;
5501 cp_parser_expression_stack_entry *sp = &stack[0];
5502 tree lhs, rhs;
5503 cp_token *token;
5504 enum tree_code tree_type;
5505 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5506 bool overloaded_p;
a723baf1 5507
b8b94c5b 5508 /* Parse the first expression. */
93678513 5509 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
a723baf1 5510
b8b94c5b
PB
5511 for (;;)
5512 {
5513 /* Get an operator token. */
5514 token = cp_lexer_peek_token (parser->lexer);
8ff24a79
MM
5515 if (token->type == CPP_MIN || token->type == CPP_MAX)
5516 cp_parser_warn_min_max ();
5517
b8b94c5b
PB
5518 new_prec = TOKEN_PRECEDENCE (token);
5519
5520 /* Popping an entry off the stack means we completed a subexpression:
0cbd7506
MS
5521 - either we found a token which is not an operator (`>' where it is not
5522 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5523 will happen repeatedly;
5524 - or, we found an operator which has lower priority. This is the case
5525 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5526 parsing `3 * 4'. */
b8b94c5b 5527 if (new_prec <= prec)
0cbd7506
MS
5528 {
5529 if (sp == stack)
b8b94c5b 5530 break;
0cbd7506 5531 else
b8b94c5b 5532 goto pop;
0cbd7506 5533 }
a723baf1 5534
b8b94c5b
PB
5535 get_rhs:
5536 tree_type = binops_by_token[token->type].tree_type;
a723baf1 5537
03fd3f84 5538 /* We used the operator token. */
b8b94c5b 5539 cp_lexer_consume_token (parser->lexer);
a723baf1 5540
b8b94c5b 5541 /* Extract another operand. It may be the RHS of this expression
0cbd7506 5542 or the LHS of a new, higher priority expression. */
b8b94c5b 5543 rhs = cp_parser_simple_cast_expression (parser);
a723baf1 5544
b8b94c5b 5545 /* Get another operator token. Look up its precedence to avoid
0cbd7506
MS
5546 building a useless (immediately popped) stack entry for common
5547 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
b8b94c5b
PB
5548 token = cp_lexer_peek_token (parser->lexer);
5549 lookahead_prec = TOKEN_PRECEDENCE (token);
5550 if (lookahead_prec > new_prec)
0cbd7506
MS
5551 {
5552 /* ... and prepare to parse the RHS of the new, higher priority
5553 expression. Since precedence levels on the stack are
43c2a69a
PB
5554 monotonically increasing, we do not have to care about
5555 stack overflows. */
0cbd7506
MS
5556 sp->prec = prec;
5557 sp->tree_type = tree_type;
5558 sp->lhs = lhs;
5559 sp++;
5560 lhs = rhs;
5561 prec = new_prec;
5562 new_prec = lookahead_prec;
5563 goto get_rhs;
5564
5565 pop:
5566 /* If the stack is not empty, we have parsed into LHS the right side
b8b94c5b 5567 (`4' in the example above) of an expression we had suspended.
c8094d83 5568 We can use the information on the stack to recover the LHS (`3')
b8b94c5b
PB
5569 from the stack together with the tree code (`MULT_EXPR'), and
5570 the precedence of the higher level subexpression
5571 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5572 which will be used to actually build the additive expression. */
0cbd7506 5573 --sp;
b8b94c5b 5574 prec = sp->prec;
0cbd7506
MS
5575 tree_type = sp->tree_type;
5576 rhs = lhs;
5577 lhs = sp->lhs;
5578 }
a723baf1 5579
b8b94c5b
PB
5580 overloaded_p = false;
5581 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
a723baf1 5582
b8b94c5b 5583 /* If the binary operator required the use of an overloaded operator,
0cbd7506
MS
5584 then this expression cannot be an integral constant-expression.
5585 An overloaded operator can be used even if both operands are
5586 otherwise permissible in an integral constant-expression if at
5587 least one of the operands is of enumeration type. */
a723baf1 5588
b8b94c5b 5589 if (overloaded_p
0cbd7506
MS
5590 && (cp_parser_non_integral_constant_expression
5591 (parser, "calls to overloaded operators")))
5592 return error_mark_node;
b8b94c5b 5593 }
a723baf1 5594
b8b94c5b 5595 return lhs;
a723baf1
MM
5596}
5597
b8b94c5b 5598
a723baf1
MM
5599/* Parse the `? expression : assignment-expression' part of a
5600 conditional-expression. The LOGICAL_OR_EXPR is the
5601 logical-or-expression that started the conditional-expression.
5602 Returns a representation of the entire conditional-expression.
5603
39703eb9 5604 This routine is used by cp_parser_assignment_expression.
a723baf1
MM
5605
5606 ? expression : assignment-expression
21526606 5607
a723baf1 5608 GNU Extensions:
21526606 5609
a723baf1
MM
5610 ? : assignment-expression */
5611
5612static tree
94edc4ab 5613cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
a723baf1
MM
5614{
5615 tree expr;
5616 tree assignment_expr;
5617
5618 /* Consume the `?' token. */
5619 cp_lexer_consume_token (parser->lexer);
5620 if (cp_parser_allow_gnu_extensions_p (parser)
5621 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5622 /* Implicit true clause. */
5623 expr = NULL_TREE;
5624 else
5625 /* Parse the expression. */
93678513 5626 expr = cp_parser_expression (parser, /*cast_p=*/false);
21526606 5627
a723baf1
MM
5628 /* The next token should be a `:'. */
5629 cp_parser_require (parser, CPP_COLON, "`:'");
5630 /* Parse the assignment-expression. */
93678513 5631 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
a723baf1
MM
5632
5633 /* Build the conditional-expression. */
5634 return build_x_conditional_expr (logical_or_expr,
5635 expr,
5636 assignment_expr);
5637}
5638
5639/* Parse an assignment-expression.
5640
5641 assignment-expression:
5642 conditional-expression
5643 logical-or-expression assignment-operator assignment_expression
5644 throw-expression
5645
93678513
MM
5646 CAST_P is true if this expression is the target of a cast.
5647
a723baf1
MM
5648 Returns a representation for the expression. */
5649
5650static tree
93678513 5651cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
a723baf1
MM
5652{
5653 tree expr;
5654
5655 /* If the next token is the `throw' keyword, then we're looking at
5656 a throw-expression. */
5657 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5658 expr = cp_parser_throw_expression (parser);
5659 /* Otherwise, it must be that we are looking at a
5660 logical-or-expression. */
5661 else
5662 {
b8b94c5b 5663 /* Parse the binary expressions (logical-or-expression). */
93678513 5664 expr = cp_parser_binary_expression (parser, cast_p);
a723baf1
MM
5665 /* If the next token is a `?' then we're actually looking at a
5666 conditional-expression. */
5667 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5668 return cp_parser_question_colon_clause (parser, expr);
21526606 5669 else
a723baf1
MM
5670 {
5671 enum tree_code assignment_operator;
5672
5673 /* If it's an assignment-operator, we're using the second
5674 production. */
21526606 5675 assignment_operator
a723baf1
MM
5676 = cp_parser_assignment_operator_opt (parser);
5677 if (assignment_operator != ERROR_MARK)
5678 {
5679 tree rhs;
5680
5681 /* Parse the right-hand side of the assignment. */
93678513 5682 rhs = cp_parser_assignment_expression (parser, cast_p);
14d22dd6
MM
5683 /* An assignment may not appear in a
5684 constant-expression. */
625cbf93
MM
5685 if (cp_parser_non_integral_constant_expression (parser,
5686 "an assignment"))
5687 return error_mark_node;
34cd5ae7 5688 /* Build the assignment expression. */
21526606
EC
5689 expr = build_x_modify_expr (expr,
5690 assignment_operator,
a723baf1
MM
5691 rhs);
5692 }
5693 }
5694 }
5695
5696 return expr;
5697}
5698
5699/* Parse an (optional) assignment-operator.
5700
21526606
EC
5701 assignment-operator: one of
5702 = *= /= %= += -= >>= <<= &= ^= |=
a723baf1
MM
5703
5704 GNU Extension:
21526606 5705
a723baf1
MM
5706 assignment-operator: one of
5707 <?= >?=
5708
5709 If the next token is an assignment operator, the corresponding tree
5710 code is returned, and the token is consumed. For example, for
5711 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5712 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5713 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5714 operator, ERROR_MARK is returned. */
5715
5716static enum tree_code
94edc4ab 5717cp_parser_assignment_operator_opt (cp_parser* parser)
a723baf1
MM
5718{
5719 enum tree_code op;
5720 cp_token *token;
5721
5722 /* Peek at the next toen. */
5723 token = cp_lexer_peek_token (parser->lexer);
5724
5725 switch (token->type)
5726 {
5727 case CPP_EQ:
5728 op = NOP_EXPR;
5729 break;
5730
5731 case CPP_MULT_EQ:
5732 op = MULT_EXPR;
5733 break;
5734
5735 case CPP_DIV_EQ:
5736 op = TRUNC_DIV_EXPR;
5737 break;
5738
5739 case CPP_MOD_EQ:
5740 op = TRUNC_MOD_EXPR;
5741 break;
5742
5743 case CPP_PLUS_EQ:
5744 op = PLUS_EXPR;
5745 break;
5746
5747 case CPP_MINUS_EQ:
5748 op = MINUS_EXPR;
5749 break;
5750
5751 case CPP_RSHIFT_EQ:
5752 op = RSHIFT_EXPR;
5753 break;
5754
5755 case CPP_LSHIFT_EQ:
5756 op = LSHIFT_EXPR;
5757 break;
5758
5759 case CPP_AND_EQ:
5760 op = BIT_AND_EXPR;
5761 break;
5762
5763 case CPP_XOR_EQ:
5764 op = BIT_XOR_EXPR;
5765 break;
5766
5767 case CPP_OR_EQ:
5768 op = BIT_IOR_EXPR;
5769 break;
5770
5771 case CPP_MIN_EQ:
5772 op = MIN_EXPR;
8ff24a79 5773 cp_parser_warn_min_max ();
a723baf1
MM
5774 break;
5775
5776 case CPP_MAX_EQ:
5777 op = MAX_EXPR;
8ff24a79 5778 cp_parser_warn_min_max ();
a723baf1
MM
5779 break;
5780
21526606 5781 default:
a723baf1
MM
5782 /* Nothing else is an assignment operator. */
5783 op = ERROR_MARK;
5784 }
5785
5786 /* If it was an assignment operator, consume it. */
5787 if (op != ERROR_MARK)
5788 cp_lexer_consume_token (parser->lexer);
5789
5790 return op;
5791}
5792
5793/* Parse an expression.
5794
5795 expression:
5796 assignment-expression
5797 expression , assignment-expression
5798
93678513
MM
5799 CAST_P is true if this expression is the target of a cast.
5800
a723baf1
MM
5801 Returns a representation of the expression. */
5802
5803static tree
93678513 5804cp_parser_expression (cp_parser* parser, bool cast_p)
a723baf1
MM
5805{
5806 tree expression = NULL_TREE;
a723baf1
MM
5807
5808 while (true)
5809 {
5810 tree assignment_expression;
5811
5812 /* Parse the next assignment-expression. */
21526606 5813 assignment_expression
93678513 5814 = cp_parser_assignment_expression (parser, cast_p);
a723baf1
MM
5815 /* If this is the first assignment-expression, we can just
5816 save it away. */
5817 if (!expression)
5818 expression = assignment_expression;
a723baf1 5819 else
d17811fd
MM
5820 expression = build_x_compound_expr (expression,
5821 assignment_expression);
a723baf1
MM
5822 /* If the next token is not a comma, then we are done with the
5823 expression. */
5824 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5825 break;
5826 /* Consume the `,'. */
5827 cp_lexer_consume_token (parser->lexer);
14d22dd6 5828 /* A comma operator cannot appear in a constant-expression. */
625cbf93
MM
5829 if (cp_parser_non_integral_constant_expression (parser,
5830 "a comma operator"))
5831 expression = error_mark_node;
14d22dd6 5832 }
a723baf1
MM
5833
5834 return expression;
5835}
5836
21526606 5837/* Parse a constant-expression.
a723baf1
MM
5838
5839 constant-expression:
21526606 5840 conditional-expression
14d22dd6
MM
5841
5842 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
d17811fd
MM
5843 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5844 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5845 is false, NON_CONSTANT_P should be NULL. */
a723baf1
MM
5846
5847static tree
21526606 5848cp_parser_constant_expression (cp_parser* parser,
14d22dd6
MM
5849 bool allow_non_constant_p,
5850 bool *non_constant_p)
a723baf1 5851{
67c03833
JM
5852 bool saved_integral_constant_expression_p;
5853 bool saved_allow_non_integral_constant_expression_p;
5854 bool saved_non_integral_constant_expression_p;
a723baf1
MM
5855 tree expression;
5856
5857 /* It might seem that we could simply parse the
5858 conditional-expression, and then check to see if it were
5859 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5860 one that the compiler can figure out is constant, possibly after
5861 doing some simplifications or optimizations. The standard has a
5862 precise definition of constant-expression, and we must honor
5863 that, even though it is somewhat more restrictive.
5864
5865 For example:
5866
5867 int i[(2, 3)];
5868
5869 is not a legal declaration, because `(2, 3)' is not a
5870 constant-expression. The `,' operator is forbidden in a
5871 constant-expression. However, GCC's constant-folding machinery
5872 will fold this operation to an INTEGER_CST for `3'. */
5873
14d22dd6 5874 /* Save the old settings. */
67c03833 5875 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
21526606 5876 saved_allow_non_integral_constant_expression_p
67c03833
JM
5877 = parser->allow_non_integral_constant_expression_p;
5878 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
a723baf1 5879 /* We are now parsing a constant-expression. */
67c03833
JM
5880 parser->integral_constant_expression_p = true;
5881 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5882 parser->non_integral_constant_expression_p = false;
39703eb9
MM
5883 /* Although the grammar says "conditional-expression", we parse an
5884 "assignment-expression", which also permits "throw-expression"
5885 and the use of assignment operators. In the case that
5886 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5887 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5888 actually essential that we look for an assignment-expression.
5889 For example, cp_parser_initializer_clauses uses this function to
5890 determine whether a particular assignment-expression is in fact
5891 constant. */
93678513 5892 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14d22dd6 5893 /* Restore the old settings. */
c8094d83 5894 parser->integral_constant_expression_p
93678513 5895 = saved_integral_constant_expression_p;
21526606 5896 parser->allow_non_integral_constant_expression_p
67c03833 5897 = saved_allow_non_integral_constant_expression_p;
14d22dd6 5898 if (allow_non_constant_p)
67c03833 5899 *non_constant_p = parser->non_integral_constant_expression_p;
93678513
MM
5900 else if (parser->non_integral_constant_expression_p)
5901 expression = error_mark_node;
c8094d83 5902 parser->non_integral_constant_expression_p
93678513 5903 = saved_non_integral_constant_expression_p;
a723baf1
MM
5904
5905 return expression;
5906}
5907
7a3ea201
RH
5908/* Parse __builtin_offsetof.
5909
5910 offsetof-expression:
5911 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5912
5913 offsetof-member-designator:
5914 id-expression
5915 | offsetof-member-designator "." id-expression
5916 | offsetof-member-designator "[" expression "]"
5917*/
5918
5919static tree
5920cp_parser_builtin_offsetof (cp_parser *parser)
5921{
5922 int save_ice_p, save_non_ice_p;
5923 tree type, expr;
5924 cp_id_kind dummy;
5925
5926 /* We're about to accept non-integral-constant things, but will
5927 definitely yield an integral constant expression. Save and
5928 restore these values around our local parsing. */
5929 save_ice_p = parser->integral_constant_expression_p;
5930 save_non_ice_p = parser->non_integral_constant_expression_p;
5931
5932 /* Consume the "__builtin_offsetof" token. */
5933 cp_lexer_consume_token (parser->lexer);
5934 /* Consume the opening `('. */
5935 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5936 /* Parse the type-id. */
5937 type = cp_parser_type_id (parser);
5938 /* Look for the `,'. */
5939 cp_parser_require (parser, CPP_COMMA, "`,'");
5940
5941 /* Build the (type *)null that begins the traditional offsetof macro. */
5942 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5943
5944 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5945 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5946 true, &dummy);
5947 while (true)
5948 {
5949 cp_token *token = cp_lexer_peek_token (parser->lexer);
5950 switch (token->type)
5951 {
5952 case CPP_OPEN_SQUARE:
5953 /* offsetof-member-designator "[" expression "]" */
5954 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5955 break;
5956
5957 case CPP_DOT:
5958 /* offsetof-member-designator "." identifier */
5959 cp_lexer_consume_token (parser->lexer);
5960 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5961 true, &dummy);
5962 break;
5963
5964 case CPP_CLOSE_PAREN:
5965 /* Consume the ")" token. */
5966 cp_lexer_consume_token (parser->lexer);
5967 goto success;
5968
5969 default:
5970 /* Error. We know the following require will fail, but
5971 that gives the proper error message. */
5972 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5973 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5974 expr = error_mark_node;
5975 goto failure;
5976 }
5977 }
5978
5979 success:
42c244d8
RH
5980 /* If we're processing a template, we can't finish the semantics yet.
5981 Otherwise we can fold the entire expression now. */
5982 if (processing_template_decl)
5983 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5984 else
5985 expr = fold_offsetof (expr);
7a3ea201
RH
5986
5987 failure:
5988 parser->integral_constant_expression_p = save_ice_p;
5989 parser->non_integral_constant_expression_p = save_non_ice_p;
5990
5991 return expr;
5992}
5993
a723baf1
MM
5994/* Statements [gram.stmt.stmt] */
5995
21526606 5996/* Parse a statement.
a723baf1
MM
5997
5998 statement:
5999 labeled-statement
6000 expression-statement
6001 compound-statement
6002 selection-statement
6003 iteration-statement
6004 jump-statement
6005 declaration-statement
6006 try-block */
6007
6008static void
325c3691 6009cp_parser_statement (cp_parser* parser, tree in_statement_expr)
a723baf1
MM
6010{
6011 tree statement;
6012 cp_token *token;
93409b8c 6013 location_t statement_location;
a723baf1
MM
6014
6015 /* There is no statement yet. */
6016 statement = NULL_TREE;
6017 /* Peek at the next token. */
6018 token = cp_lexer_peek_token (parser->lexer);
6de9cd9a 6019 /* Remember the location of the first token in the statement. */
93409b8c 6020 statement_location = token->location;
a723baf1
MM
6021 /* If this is a keyword, then that will often determine what kind of
6022 statement we have. */
6023 if (token->type == CPP_KEYWORD)
6024 {
6025 enum rid keyword = token->keyword;
6026
6027 switch (keyword)
6028 {
6029 case RID_CASE:
6030 case RID_DEFAULT:
a5bcc582 6031 statement = cp_parser_labeled_statement (parser,
325c3691 6032 in_statement_expr);
a723baf1
MM
6033 break;
6034
6035 case RID_IF:
6036 case RID_SWITCH:
6037 statement = cp_parser_selection_statement (parser);
6038 break;
6039
6040 case RID_WHILE:
6041 case RID_DO:
6042 case RID_FOR:
6043 statement = cp_parser_iteration_statement (parser);
6044 break;
6045
6046 case RID_BREAK:
6047 case RID_CONTINUE:
6048 case RID_RETURN:
6049 case RID_GOTO:
6050 statement = cp_parser_jump_statement (parser);
6051 break;
6052
e58a9aa1
ZL
6053 /* Objective-C++ exception-handling constructs. */
6054 case RID_AT_TRY:
6055 case RID_AT_CATCH:
6056 case RID_AT_FINALLY:
6057 case RID_AT_SYNCHRONIZED:
6058 case RID_AT_THROW:
6059 statement = cp_parser_objc_statement (parser);
6060 break;
6061
a723baf1
MM
6062 case RID_TRY:
6063 statement = cp_parser_try_block (parser);
6064 break;
6065
6066 default:
6067 /* It might be a keyword like `int' that can start a
6068 declaration-statement. */
6069 break;
6070 }
6071 }
6072 else if (token->type == CPP_NAME)
6073 {
6074 /* If the next token is a `:', then we are looking at a
6075 labeled-statement. */
6076 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6077 if (token->type == CPP_COLON)
325c3691 6078 statement = cp_parser_labeled_statement (parser, in_statement_expr);
a723baf1
MM
6079 }
6080 /* Anything that starts with a `{' must be a compound-statement. */
6081 else if (token->type == CPP_OPEN_BRACE)
325c3691 6082 statement = cp_parser_compound_statement (parser, NULL, false);
36952dea
ZW
6083 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6084 a statement all its own. */
6085 else if (token->type == CPP_PRAGMA)
6086 {
6087 cp_lexer_handle_pragma (parser->lexer);
6088 return;
6089 }
a723baf1
MM
6090
6091 /* Everything else must be a declaration-statement or an
21526606 6092 expression-statement. Try for the declaration-statement
a723baf1
MM
6093 first, unless we are looking at a `;', in which case we know that
6094 we have an expression-statement. */
6095 if (!statement)
6096 {
6097 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6098 {
6099 cp_parser_parse_tentatively (parser);
6100 /* Try to parse the declaration-statement. */
6101 cp_parser_declaration_statement (parser);
6102 /* If that worked, we're done. */
6103 if (cp_parser_parse_definitely (parser))
6104 return;
6105 }
6106 /* Look for an expression-statement instead. */
325c3691 6107 statement = cp_parser_expression_statement (parser, in_statement_expr);
a723baf1
MM
6108 }
6109
6110 /* Set the line number for the statement. */
009ed910 6111 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
93409b8c 6112 SET_EXPR_LOCATION (statement, statement_location);
a723baf1
MM
6113}
6114
6115/* Parse a labeled-statement.
6116
6117 labeled-statement:
6118 identifier : statement
6119 case constant-expression : statement
98ce043b
MM
6120 default : statement
6121
6122 GNU Extension:
21526606 6123
98ce043b
MM
6124 labeled-statement:
6125 case constant-expression ... constant-expression : statement
a723baf1 6126
8c161995
RH
6127 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6128 For an ordinary label, returns a LABEL_EXPR. */
a723baf1
MM
6129
6130static tree
325c3691 6131cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
a723baf1
MM
6132{
6133 cp_token *token;
0e59b3fb 6134 tree statement = error_mark_node;
a723baf1
MM
6135
6136 /* The next token should be an identifier. */
6137 token = cp_lexer_peek_token (parser->lexer);
6138 if (token->type != CPP_NAME
6139 && token->type != CPP_KEYWORD)
6140 {
6141 cp_parser_error (parser, "expected labeled-statement");
6142 return error_mark_node;
6143 }
6144
6145 switch (token->keyword)
6146 {
6147 case RID_CASE:
6148 {
98ce043b
MM
6149 tree expr, expr_hi;
6150 cp_token *ellipsis;
a723baf1
MM
6151
6152 /* Consume the `case' token. */
6153 cp_lexer_consume_token (parser->lexer);
6154 /* Parse the constant-expression. */
21526606 6155 expr = cp_parser_constant_expression (parser,
d17811fd 6156 /*allow_non_constant_p=*/false,
14d22dd6 6157 NULL);
98ce043b
MM
6158
6159 ellipsis = cp_lexer_peek_token (parser->lexer);
6160 if (ellipsis->type == CPP_ELLIPSIS)
6161 {
0cbd7506 6162 /* Consume the `...' token. */
98ce043b
MM
6163 cp_lexer_consume_token (parser->lexer);
6164 expr_hi =
6165 cp_parser_constant_expression (parser,
0cbd7506 6166 /*allow_non_constant_p=*/false,
98ce043b
MM
6167 NULL);
6168 /* We don't need to emit warnings here, as the common code
6169 will do this for us. */
6170 }
6171 else
6172 expr_hi = NULL_TREE;
6173
0e59b3fb 6174 if (!parser->in_switch_statement_p)
2a13a625 6175 error ("case label %qE not within a switch statement", expr);
0e59b3fb 6176 else
98ce043b 6177 statement = finish_case_label (expr, expr_hi);
a723baf1
MM
6178 }
6179 break;
6180
6181 case RID_DEFAULT:
6182 /* Consume the `default' token. */
6183 cp_lexer_consume_token (parser->lexer);
0e59b3fb
MM
6184 if (!parser->in_switch_statement_p)
6185 error ("case label not within a switch statement");
6186 else
6187 statement = finish_case_label (NULL_TREE, NULL_TREE);
a723baf1
MM
6188 break;
6189
6190 default:
6191 /* Anything else must be an ordinary label. */
6192 statement = finish_label_stmt (cp_parser_identifier (parser));
6193 break;
6194 }
6195
6196 /* Require the `:' token. */
6197 cp_parser_require (parser, CPP_COLON, "`:'");
6198 /* Parse the labeled statement. */
325c3691 6199 cp_parser_statement (parser, in_statement_expr);
a723baf1
MM
6200
6201 /* Return the label, in the case of a `case' or `default' label. */
6202 return statement;
6203}
6204
6205/* Parse an expression-statement.
6206
6207 expression-statement:
6208 expression [opt] ;
6209
6210 Returns the new EXPR_STMT -- or NULL_TREE if the expression
a5bcc582
NS
6211 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6212 indicates whether this expression-statement is part of an
6213 expression statement. */
a723baf1
MM
6214
6215static tree
325c3691 6216cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
a723baf1 6217{
a5bcc582 6218 tree statement = NULL_TREE;
a723baf1 6219
a5bcc582 6220 /* If the next token is a ';', then there is no expression
04c06002 6221 statement. */
a723baf1 6222 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
93678513 6223 statement = cp_parser_expression (parser, /*cast_p=*/false);
21526606 6224
a723baf1 6225 /* Consume the final `;'. */
e0860732 6226 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1 6227
325c3691 6228 if (in_statement_expr
a5bcc582 6229 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
93678513
MM
6230 /* This is the final expression statement of a statement
6231 expression. */
6232 statement = finish_stmt_expr_expr (statement, in_statement_expr);
a5bcc582
NS
6233 else if (statement)
6234 statement = finish_expr_stmt (statement);
6235 else
6236 finish_stmt ();
21526606 6237
a723baf1
MM
6238 return statement;
6239}
6240
6241/* Parse a compound-statement.
6242
6243 compound-statement:
6244 { statement-seq [opt] }
21526606 6245
5882f0f3 6246 Returns a tree representing the statement. */
a723baf1
MM
6247
6248static tree
325c3691
RH
6249cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6250 bool in_try)
a723baf1
MM
6251{
6252 tree compound_stmt;
6253
6254 /* Consume the `{'. */
6255 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6256 return error_mark_node;
6257 /* Begin the compound-statement. */
325c3691 6258 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
a723baf1 6259 /* Parse an (optional) statement-seq. */
325c3691 6260 cp_parser_statement_seq_opt (parser, in_statement_expr);
a723baf1 6261 /* Finish the compound-statement. */
7a3397c7 6262 finish_compound_stmt (compound_stmt);
a723baf1
MM
6263 /* Consume the `}'. */
6264 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6265
6266 return compound_stmt;
6267}
6268
6269/* Parse an (optional) statement-seq.
6270
6271 statement-seq:
6272 statement
6273 statement-seq [opt] statement */
6274
6275static void
325c3691 6276cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
a723baf1
MM
6277{
6278 /* Scan statements until there aren't any more. */
6279 while (true)
6280 {
6281 /* If we're looking at a `}', then we've run out of statements. */
6282 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6283 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6284 break;
6285
6286 /* Parse the statement. */
325c3691 6287 cp_parser_statement (parser, in_statement_expr);
a723baf1
MM
6288 }
6289}
6290
6291/* Parse a selection-statement.
6292
6293 selection-statement:
6294 if ( condition ) statement
6295 if ( condition ) statement else statement
21526606 6296 switch ( condition ) statement
a723baf1
MM
6297
6298 Returns the new IF_STMT or SWITCH_STMT. */
6299
6300static tree
94edc4ab 6301cp_parser_selection_statement (cp_parser* parser)
a723baf1
MM
6302{
6303 cp_token *token;
6304 enum rid keyword;
6305
6306 /* Peek at the next token. */
6307 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6308
6309 /* See what kind of keyword it is. */
6310 keyword = token->keyword;
6311 switch (keyword)
6312 {
6313 case RID_IF:
6314 case RID_SWITCH:
6315 {
6316 tree statement;
6317 tree condition;
6318
6319 /* Look for the `('. */
6320 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6321 {
6322 cp_parser_skip_to_end_of_statement (parser);
6323 return error_mark_node;
6324 }
6325
6326 /* Begin the selection-statement. */
6327 if (keyword == RID_IF)
6328 statement = begin_if_stmt ();
6329 else
6330 statement = begin_switch_stmt ();
6331
6332 /* Parse the condition. */
6333 condition = cp_parser_condition (parser);
6334 /* Look for the `)'. */
6335 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
6336 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6337 /*consume_paren=*/true);
a723baf1
MM
6338
6339 if (keyword == RID_IF)
6340 {
a723baf1
MM
6341 /* Add the condition. */
6342 finish_if_stmt_cond (condition, statement);
6343
6344 /* Parse the then-clause. */
325c3691 6345 cp_parser_implicitly_scoped_statement (parser);
a723baf1
MM
6346 finish_then_clause (statement);
6347
6348 /* If the next token is `else', parse the else-clause. */
6349 if (cp_lexer_next_token_is_keyword (parser->lexer,
6350 RID_ELSE))
6351 {
a723baf1
MM
6352 /* Consume the `else' keyword. */
6353 cp_lexer_consume_token (parser->lexer);
325c3691 6354 begin_else_clause (statement);
a723baf1 6355 /* Parse the else-clause. */
325c3691 6356 cp_parser_implicitly_scoped_statement (parser);
a723baf1
MM
6357 finish_else_clause (statement);
6358 }
6359
6360 /* Now we're all done with the if-statement. */
325c3691 6361 finish_if_stmt (statement);
a723baf1
MM
6362 }
6363 else
6364 {
0e59b3fb 6365 bool in_switch_statement_p;
a723baf1
MM
6366
6367 /* Add the condition. */
6368 finish_switch_cond (condition, statement);
6369
6370 /* Parse the body of the switch-statement. */
0e59b3fb
MM
6371 in_switch_statement_p = parser->in_switch_statement_p;
6372 parser->in_switch_statement_p = true;
325c3691 6373 cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 6374 parser->in_switch_statement_p = in_switch_statement_p;
a723baf1
MM
6375
6376 /* Now we're all done with the switch-statement. */
6377 finish_switch_stmt (statement);
6378 }
6379
6380 return statement;
6381 }
6382 break;
6383
6384 default:
6385 cp_parser_error (parser, "expected selection-statement");
6386 return error_mark_node;
6387 }
6388}
6389
21526606 6390/* Parse a condition.
a723baf1
MM
6391
6392 condition:
6393 expression
21526606 6394 type-specifier-seq declarator = assignment-expression
a723baf1
MM
6395
6396 GNU Extension:
21526606 6397
a723baf1 6398 condition:
21526606 6399 type-specifier-seq declarator asm-specification [opt]
a723baf1 6400 attributes [opt] = assignment-expression
21526606 6401
a723baf1
MM
6402 Returns the expression that should be tested. */
6403
6404static tree
94edc4ab 6405cp_parser_condition (cp_parser* parser)
a723baf1 6406{
62d1db17 6407 cp_decl_specifier_seq type_specifiers;
a723baf1
MM
6408 const char *saved_message;
6409
6410 /* Try the declaration first. */
6411 cp_parser_parse_tentatively (parser);
6412 /* New types are not allowed in the type-specifier-seq for a
6413 condition. */
6414 saved_message = parser->type_definition_forbidden_message;
6415 parser->type_definition_forbidden_message
6416 = "types may not be defined in conditions";
6417 /* Parse the type-specifier-seq. */
d4113656
MM
6418 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6419 &type_specifiers);
a723baf1
MM
6420 /* Restore the saved message. */
6421 parser->type_definition_forbidden_message = saved_message;
6422 /* If all is well, we might be looking at a declaration. */
6423 if (!cp_parser_error_occurred (parser))
6424 {
6425 tree decl;
6426 tree asm_specification;
6427 tree attributes;
058b15c1 6428 cp_declarator *declarator;
a723baf1 6429 tree initializer = NULL_TREE;
21526606 6430
a723baf1 6431 /* Parse the declarator. */
62b8a44e 6432 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 6433 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
6434 /*parenthesized_p=*/NULL,
6435 /*member_p=*/false);
a723baf1
MM
6436 /* Parse the attributes. */
6437 attributes = cp_parser_attributes_opt (parser);
6438 /* Parse the asm-specification. */
6439 asm_specification = cp_parser_asm_specification_opt (parser);
6440 /* If the next token is not an `=', then we might still be
6441 looking at an expression. For example:
21526606 6442
a723baf1 6443 if (A(a).x)
21526606 6444
a723baf1
MM
6445 looks like a decl-specifier-seq and a declarator -- but then
6446 there is no `=', so this is an expression. */
6447 cp_parser_require (parser, CPP_EQ, "`='");
6448 /* If we did see an `=', then we are looking at a declaration
6449 for sure. */
6450 if (cp_parser_parse_definitely (parser))
6451 {
c8094d83 6452 tree pushed_scope;
73a8adb6 6453
a723baf1 6454 /* Create the declaration. */
62d1db17 6455 decl = start_decl (declarator, &type_specifiers,
a723baf1 6456 /*initialized_p=*/true,
73a8adb6 6457 attributes, /*prefix_attributes=*/NULL_TREE,
4514aa8c 6458 &pushed_scope);
a723baf1 6459 /* Parse the assignment-expression. */
93678513
MM
6460 initializer = cp_parser_assignment_expression (parser,
6461 /*cast_p=*/false);
21526606 6462
a723baf1 6463 /* Process the initializer. */
21526606
EC
6464 cp_finish_decl (decl,
6465 initializer,
6466 asm_specification,
a723baf1 6467 LOOKUP_ONLYCONVERTING);
c162c75e 6468
4514aa8c
NS
6469 if (pushed_scope)
6470 pop_scope (pushed_scope);
21526606 6471
a723baf1
MM
6472 return convert_from_reference (decl);
6473 }
6474 }
6475 /* If we didn't even get past the declarator successfully, we are
6476 definitely not looking at a declaration. */
6477 else
6478 cp_parser_abort_tentative_parse (parser);
6479
6480 /* Otherwise, we are looking at an expression. */
93678513 6481 return cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
6482}
6483
6484/* Parse an iteration-statement.
6485
6486 iteration-statement:
6487 while ( condition ) statement
6488 do statement while ( expression ) ;
6489 for ( for-init-statement condition [opt] ; expression [opt] )
6490 statement
6491
6492 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6493
6494static tree
94edc4ab 6495cp_parser_iteration_statement (cp_parser* parser)
a723baf1
MM
6496{
6497 cp_token *token;
6498 enum rid keyword;
6499 tree statement;
0e59b3fb
MM
6500 bool in_iteration_statement_p;
6501
a723baf1
MM
6502
6503 /* Peek at the next token. */
6504 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6505 if (!token)
6506 return error_mark_node;
6507
0e59b3fb 6508 /* Remember whether or not we are already within an iteration
21526606 6509 statement. */
0e59b3fb
MM
6510 in_iteration_statement_p = parser->in_iteration_statement_p;
6511
a723baf1
MM
6512 /* See what kind of keyword it is. */
6513 keyword = token->keyword;
6514 switch (keyword)
6515 {
6516 case RID_WHILE:
6517 {
6518 tree condition;
6519
6520 /* Begin the while-statement. */
6521 statement = begin_while_stmt ();
6522 /* Look for the `('. */
6523 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6524 /* Parse the condition. */
6525 condition = cp_parser_condition (parser);
6526 finish_while_stmt_cond (condition, statement);
6527 /* Look for the `)'. */
6528 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6529 /* Parse the dependent statement. */
0e59b3fb 6530 parser->in_iteration_statement_p = true;
a723baf1 6531 cp_parser_already_scoped_statement (parser);
0e59b3fb 6532 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
6533 /* We're done with the while-statement. */
6534 finish_while_stmt (statement);
6535 }
6536 break;
6537
6538 case RID_DO:
6539 {
6540 tree expression;
6541
6542 /* Begin the do-statement. */
6543 statement = begin_do_stmt ();
6544 /* Parse the body of the do-statement. */
0e59b3fb 6545 parser->in_iteration_statement_p = true;
a723baf1 6546 cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 6547 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
6548 finish_do_body (statement);
6549 /* Look for the `while' keyword. */
6550 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6551 /* Look for the `('. */
6552 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6553 /* Parse the expression. */
93678513 6554 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
6555 /* We're done with the do-statement. */
6556 finish_do_stmt (expression, statement);
6557 /* Look for the `)'. */
6558 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6559 /* Look for the `;'. */
6560 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6561 }
6562 break;
6563
6564 case RID_FOR:
6565 {
6566 tree condition = NULL_TREE;
6567 tree expression = NULL_TREE;
6568
6569 /* Begin the for-statement. */
6570 statement = begin_for_stmt ();
6571 /* Look for the `('. */
6572 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6573 /* Parse the initialization. */
6574 cp_parser_for_init_statement (parser);
6575 finish_for_init_stmt (statement);
6576
6577 /* If there's a condition, process it. */
6578 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6579 condition = cp_parser_condition (parser);
6580 finish_for_cond (condition, statement);
6581 /* Look for the `;'. */
6582 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6583
6584 /* If there's an expression, process it. */
6585 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
93678513 6586 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
6587 finish_for_expr (expression, statement);
6588 /* Look for the `)'. */
d5a10cf0 6589 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
98ca843c 6590
a723baf1 6591 /* Parse the body of the for-statement. */
0e59b3fb 6592 parser->in_iteration_statement_p = true;
a723baf1 6593 cp_parser_already_scoped_statement (parser);
0e59b3fb 6594 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
6595
6596 /* We're done with the for-statement. */
6597 finish_for_stmt (statement);
6598 }
6599 break;
6600
6601 default:
6602 cp_parser_error (parser, "expected iteration-statement");
6603 statement = error_mark_node;
6604 break;
6605 }
6606
6607 return statement;
6608}
6609
6610/* Parse a for-init-statement.
6611
6612 for-init-statement:
6613 expression-statement
6614 simple-declaration */
6615
6616static void
94edc4ab 6617cp_parser_for_init_statement (cp_parser* parser)
a723baf1
MM
6618{
6619 /* If the next token is a `;', then we have an empty
34cd5ae7 6620 expression-statement. Grammatically, this is also a
a723baf1
MM
6621 simple-declaration, but an invalid one, because it does not
6622 declare anything. Therefore, if we did not handle this case
6623 specially, we would issue an error message about an invalid
6624 declaration. */
6625 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6626 {
6627 /* We're going to speculatively look for a declaration, falling back
6628 to an expression, if necessary. */
6629 cp_parser_parse_tentatively (parser);
6630 /* Parse the declaration. */
6631 cp_parser_simple_declaration (parser,
6632 /*function_definition_allowed_p=*/false);
6633 /* If the tentative parse failed, then we shall need to look for an
6634 expression-statement. */
6635 if (cp_parser_parse_definitely (parser))
6636 return;
6637 }
6638
a5bcc582 6639 cp_parser_expression_statement (parser, false);
a723baf1
MM
6640}
6641
6642/* Parse a jump-statement.
6643
6644 jump-statement:
6645 break ;
6646 continue ;
6647 return expression [opt] ;
21526606 6648 goto identifier ;
a723baf1
MM
6649
6650 GNU extension:
6651
6652 jump-statement:
6653 goto * expression ;
6654
5088b058 6655 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
a723baf1
MM
6656
6657static tree
94edc4ab 6658cp_parser_jump_statement (cp_parser* parser)
a723baf1
MM
6659{
6660 tree statement = error_mark_node;
6661 cp_token *token;
6662 enum rid keyword;
6663
6664 /* Peek at the next token. */
6665 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6666 if (!token)
6667 return error_mark_node;
6668
6669 /* See what kind of keyword it is. */
6670 keyword = token->keyword;
6671 switch (keyword)
6672 {
6673 case RID_BREAK:
0e59b3fb
MM
6674 if (!parser->in_switch_statement_p
6675 && !parser->in_iteration_statement_p)
6676 {
6677 error ("break statement not within loop or switch");
6678 statement = error_mark_node;
6679 }
6680 else
6681 statement = finish_break_stmt ();
2a13a625 6682 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6683 break;
6684
6685 case RID_CONTINUE:
0e59b3fb
MM
6686 if (!parser->in_iteration_statement_p)
6687 {
6688 error ("continue statement not within a loop");
6689 statement = error_mark_node;
6690 }
6691 else
6692 statement = finish_continue_stmt ();
2a13a625 6693 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6694 break;
6695
6696 case RID_RETURN:
6697 {
6698 tree expr;
6699
21526606 6700 /* If the next token is a `;', then there is no
a723baf1
MM
6701 expression. */
6702 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
93678513 6703 expr = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
6704 else
6705 expr = NULL_TREE;
6706 /* Build the return-statement. */
6707 statement = finish_return_stmt (expr);
6708 /* Look for the final `;'. */
2a13a625 6709 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6710 }
6711 break;
6712
6713 case RID_GOTO:
6714 /* Create the goto-statement. */
6715 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6716 {
6717 /* Issue a warning about this use of a GNU extension. */
6718 if (pedantic)
6719 pedwarn ("ISO C++ forbids computed gotos");
6720 /* Consume the '*' token. */
6721 cp_lexer_consume_token (parser->lexer);
6722 /* Parse the dependent expression. */
93678513 6723 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
a723baf1
MM
6724 }
6725 else
6726 finish_goto_stmt (cp_parser_identifier (parser));
6727 /* Look for the final `;'. */
2a13a625 6728 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6729 break;
6730
6731 default:
6732 cp_parser_error (parser, "expected jump-statement");
6733 break;
6734 }
6735
6736 return statement;
6737}
6738
6739/* Parse a declaration-statement.
6740
6741 declaration-statement:
6742 block-declaration */
6743
6744static void
94edc4ab 6745cp_parser_declaration_statement (cp_parser* parser)
a723baf1 6746{
058b15c1
MM
6747 void *p;
6748
6749 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6750 p = obstack_alloc (&declarator_obstack, 0);
6751
6752 /* Parse the block-declaration. */
a723baf1
MM
6753 cp_parser_block_declaration (parser, /*statement_p=*/true);
6754
058b15c1
MM
6755 /* Free any declarators allocated. */
6756 obstack_free (&declarator_obstack, p);
6757
a723baf1
MM
6758 /* Finish off the statement. */
6759 finish_stmt ();
6760}
6761
6762/* Some dependent statements (like `if (cond) statement'), are
6763 implicitly in their own scope. In other words, if the statement is
6764 a single statement (as opposed to a compound-statement), it is
6765 none-the-less treated as if it were enclosed in braces. Any
6766 declarations appearing in the dependent statement are out of scope
6767 after control passes that point. This function parses a statement,
6768 but ensures that is in its own scope, even if it is not a
21526606 6769 compound-statement.
a723baf1
MM
6770
6771 Returns the new statement. */
6772
6773static tree
94edc4ab 6774cp_parser_implicitly_scoped_statement (cp_parser* parser)
a723baf1
MM
6775{
6776 tree statement;
6777
6778 /* If the token is not a `{', then we must take special action. */
6779 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6780 {
6781 /* Create a compound-statement. */
325c3691 6782 statement = begin_compound_stmt (0);
a723baf1 6783 /* Parse the dependent-statement. */
a5bcc582 6784 cp_parser_statement (parser, false);
a723baf1 6785 /* Finish the dummy compound-statement. */
7a3397c7 6786 finish_compound_stmt (statement);
a723baf1
MM
6787 }
6788 /* Otherwise, we simply parse the statement directly. */
6789 else
325c3691 6790 statement = cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
6791
6792 /* Return the statement. */
6793 return statement;
6794}
6795
6796/* For some dependent statements (like `while (cond) statement'), we
6797 have already created a scope. Therefore, even if the dependent
6798 statement is a compound-statement, we do not want to create another
6799 scope. */
6800
6801static void
94edc4ab 6802cp_parser_already_scoped_statement (cp_parser* parser)
a723baf1 6803{
325c3691
RH
6804 /* If the token is a `{', then we must take special action. */
6805 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6806 cp_parser_statement (parser, false);
6807 else
a723baf1 6808 {
325c3691
RH
6809 /* Avoid calling cp_parser_compound_statement, so that we
6810 don't create a new scope. Do everything else by hand. */
6811 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6812 cp_parser_statement_seq_opt (parser, false);
6813 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1 6814 }
a723baf1
MM
6815}
6816
6817/* Declarations [gram.dcl.dcl] */
6818
6819/* Parse an optional declaration-sequence.
6820
6821 declaration-seq:
6822 declaration
6823 declaration-seq declaration */
6824
6825static void
94edc4ab 6826cp_parser_declaration_seq_opt (cp_parser* parser)
a723baf1
MM
6827{
6828 while (true)
6829 {
6830 cp_token *token;
6831
6832 token = cp_lexer_peek_token (parser->lexer);
6833
6834 if (token->type == CPP_CLOSE_BRACE
6835 || token->type == CPP_EOF)
6836 break;
6837
21526606 6838 if (token->type == CPP_SEMICOLON)
a723baf1
MM
6839 {
6840 /* A declaration consisting of a single semicolon is
6841 invalid. Allow it unless we're being pedantic. */
a723baf1 6842 cp_lexer_consume_token (parser->lexer);
2cfe82fe
ZW
6843 if (pedantic && !in_system_header)
6844 pedwarn ("extra %<;%>");
a723baf1
MM
6845 continue;
6846 }
6847
7d381002 6848 /* If we're entering or exiting a region that's implicitly
03fd3f84 6849 extern "C", modify the lang context appropriately. */
7d381002
MA
6850 if (!parser->implicit_extern_c && token->implicit_extern_c)
6851 {
6852 push_lang_context (lang_name_c);
6853 parser->implicit_extern_c = true;
6854 }
6855 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6856 {
6857 pop_lang_context ();
6858 parser->implicit_extern_c = false;
6859 }
6860
36952dea
ZW
6861 if (token->type == CPP_PRAGMA)
6862 {
6863 /* A top-level declaration can consist solely of a #pragma.
6864 A nested declaration cannot, so this is done here and not
6865 in cp_parser_declaration. (A #pragma at block scope is
6866 handled in cp_parser_statement.) */
6867 cp_lexer_handle_pragma (parser->lexer);
6868 continue;
6869 }
6870
c838d82f 6871 /* Parse the declaration itself. */
a723baf1
MM
6872 cp_parser_declaration (parser);
6873 }
6874}
6875
6876/* Parse a declaration.
6877
6878 declaration:
6879 block-declaration
6880 function-definition
6881 template-declaration
6882 explicit-instantiation
6883 explicit-specialization
6884 linkage-specification
21526606 6885 namespace-definition
1092805d
MM
6886
6887 GNU extension:
6888
6889 declaration:
6890 __extension__ declaration */
a723baf1
MM
6891
6892static void
94edc4ab 6893cp_parser_declaration (cp_parser* parser)
a723baf1
MM
6894{
6895 cp_token token1;
6896 cp_token token2;
1092805d 6897 int saved_pedantic;
058b15c1 6898 void *p;
1092805d
MM
6899
6900 /* Check for the `__extension__' keyword. */
6901 if (cp_parser_extension_opt (parser, &saved_pedantic))
6902 {
6903 /* Parse the qualified declaration. */
6904 cp_parser_declaration (parser);
6905 /* Restore the PEDANTIC flag. */
6906 pedantic = saved_pedantic;
6907
6908 return;
6909 }
a723baf1
MM
6910
6911 /* Try to figure out what kind of declaration is present. */
6912 token1 = *cp_lexer_peek_token (parser->lexer);
21526606 6913
a723baf1
MM
6914 if (token1.type != CPP_EOF)
6915 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
32cafd73
MH
6916 else
6917 token2.type = token2.keyword = RID_MAX;
a723baf1 6918
058b15c1
MM
6919 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6920 p = obstack_alloc (&declarator_obstack, 0);
6921
a723baf1
MM
6922 /* If the next token is `extern' and the following token is a string
6923 literal, then we have a linkage specification. */
6924 if (token1.keyword == RID_EXTERN
6925 && cp_parser_is_string_literal (&token2))
6926 cp_parser_linkage_specification (parser);
6927 /* If the next token is `template', then we have either a template
6928 declaration, an explicit instantiation, or an explicit
6929 specialization. */
6930 else if (token1.keyword == RID_TEMPLATE)
6931 {
6932 /* `template <>' indicates a template specialization. */
6933 if (token2.type == CPP_LESS
6934 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6935 cp_parser_explicit_specialization (parser);
6936 /* `template <' indicates a template declaration. */
6937 else if (token2.type == CPP_LESS)
6938 cp_parser_template_declaration (parser, /*member_p=*/false);
6939 /* Anything else must be an explicit instantiation. */
6940 else
6941 cp_parser_explicit_instantiation (parser);
6942 }
6943 /* If the next token is `export', then we have a template
6944 declaration. */
6945 else if (token1.keyword == RID_EXPORT)
6946 cp_parser_template_declaration (parser, /*member_p=*/false);
6947 /* If the next token is `extern', 'static' or 'inline' and the one
6948 after that is `template', we have a GNU extended explicit
6949 instantiation directive. */
6950 else if (cp_parser_allow_gnu_extensions_p (parser)
6951 && (token1.keyword == RID_EXTERN
6952 || token1.keyword == RID_STATIC
6953 || token1.keyword == RID_INLINE)
6954 && token2.keyword == RID_TEMPLATE)
6955 cp_parser_explicit_instantiation (parser);
6956 /* If the next token is `namespace', check for a named or unnamed
6957 namespace definition. */
6958 else if (token1.keyword == RID_NAMESPACE
6959 && (/* A named namespace definition. */
6960 (token2.type == CPP_NAME
21526606 6961 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
a723baf1
MM
6962 == CPP_OPEN_BRACE))
6963 /* An unnamed namespace definition. */
6964 || token2.type == CPP_OPEN_BRACE))
6965 cp_parser_namespace_definition (parser);
e58a9aa1
ZL
6966 /* Objective-C++ declaration/definition. */
6967 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
6968 cp_parser_objc_declaration (parser);
a723baf1
MM
6969 /* We must have either a block declaration or a function
6970 definition. */
6971 else
6972 /* Try to parse a block-declaration, or a function-definition. */
6973 cp_parser_block_declaration (parser, /*statement_p=*/false);
058b15c1
MM
6974
6975 /* Free any declarators allocated. */
6976 obstack_free (&declarator_obstack, p);
a723baf1
MM
6977}
6978
21526606 6979/* Parse a block-declaration.
a723baf1
MM
6980
6981 block-declaration:
6982 simple-declaration
6983 asm-definition
6984 namespace-alias-definition
6985 using-declaration
21526606 6986 using-directive
a723baf1
MM
6987
6988 GNU Extension:
6989
6990 block-declaration:
21526606 6991 __extension__ block-declaration
a723baf1
MM
6992 label-declaration
6993
34cd5ae7 6994 If STATEMENT_P is TRUE, then this block-declaration is occurring as
a723baf1
MM
6995 part of a declaration-statement. */
6996
6997static void
21526606 6998cp_parser_block_declaration (cp_parser *parser,
a723baf1
MM
6999 bool statement_p)
7000{
7001 cp_token *token1;
7002 int saved_pedantic;
7003
7004 /* Check for the `__extension__' keyword. */
7005 if (cp_parser_extension_opt (parser, &saved_pedantic))
7006 {
7007 /* Parse the qualified declaration. */
7008 cp_parser_block_declaration (parser, statement_p);
7009 /* Restore the PEDANTIC flag. */
7010 pedantic = saved_pedantic;
7011
7012 return;
7013 }
7014
7015 /* Peek at the next token to figure out which kind of declaration is
7016 present. */
7017 token1 = cp_lexer_peek_token (parser->lexer);
7018
7019 /* If the next keyword is `asm', we have an asm-definition. */
7020 if (token1->keyword == RID_ASM)
7021 {
7022 if (statement_p)
7023 cp_parser_commit_to_tentative_parse (parser);
7024 cp_parser_asm_definition (parser);
7025 }
7026 /* If the next keyword is `namespace', we have a
7027 namespace-alias-definition. */
7028 else if (token1->keyword == RID_NAMESPACE)
7029 cp_parser_namespace_alias_definition (parser);
7030 /* If the next keyword is `using', we have either a
7031 using-declaration or a using-directive. */
7032 else if (token1->keyword == RID_USING)
7033 {
7034 cp_token *token2;
7035
7036 if (statement_p)
7037 cp_parser_commit_to_tentative_parse (parser);
7038 /* If the token after `using' is `namespace', then we have a
7039 using-directive. */
7040 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7041 if (token2->keyword == RID_NAMESPACE)
7042 cp_parser_using_directive (parser);
7043 /* Otherwise, it's a using-declaration. */
7044 else
7045 cp_parser_using_declaration (parser);
7046 }
7047 /* If the next keyword is `__label__' we have a label declaration. */
7048 else if (token1->keyword == RID_LABEL)
7049 {
7050 if (statement_p)
7051 cp_parser_commit_to_tentative_parse (parser);
7052 cp_parser_label_declaration (parser);
7053 }
7054 /* Anything else must be a simple-declaration. */
7055 else
7056 cp_parser_simple_declaration (parser, !statement_p);
7057}
7058
7059/* Parse a simple-declaration.
7060
7061 simple-declaration:
21526606 7062 decl-specifier-seq [opt] init-declarator-list [opt] ;
a723baf1
MM
7063
7064 init-declarator-list:
7065 init-declarator
21526606 7066 init-declarator-list , init-declarator
a723baf1 7067
34cd5ae7 7068 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9bcb9aae 7069 function-definition as a simple-declaration. */
a723baf1
MM
7070
7071static void
21526606 7072cp_parser_simple_declaration (cp_parser* parser,
0cbd7506 7073 bool function_definition_allowed_p)
a723baf1 7074{
62d1db17 7075 cp_decl_specifier_seq decl_specifiers;
560ad596 7076 int declares_class_or_enum;
a723baf1
MM
7077 bool saw_declarator;
7078
7079 /* Defer access checks until we know what is being declared; the
7080 checks for names appearing in the decl-specifier-seq should be
7081 done as if we were in the scope of the thing being declared. */
8d241e0b 7082 push_deferring_access_checks (dk_deferred);
cf22909c 7083
a723baf1
MM
7084 /* Parse the decl-specifier-seq. We have to keep track of whether
7085 or not the decl-specifier-seq declares a named class or
7086 enumeration type, since that is the only case in which the
21526606 7087 init-declarator-list is allowed to be empty.
a723baf1
MM
7088
7089 [dcl.dcl]
7090
7091 In a simple-declaration, the optional init-declarator-list can be
7092 omitted only when declaring a class or enumeration, that is when
7093 the decl-specifier-seq contains either a class-specifier, an
7094 elaborated-type-specifier, or an enum-specifier. */
62d1db17
MM
7095 cp_parser_decl_specifier_seq (parser,
7096 CP_PARSER_FLAGS_OPTIONAL,
7097 &decl_specifiers,
7098 &declares_class_or_enum);
a723baf1 7099 /* We no longer need to defer access checks. */
cf22909c 7100 stop_deferring_access_checks ();
24c0ef37 7101
39703eb9
MM
7102 /* In a block scope, a valid declaration must always have a
7103 decl-specifier-seq. By not trying to parse declarators, we can
7104 resolve the declaration/expression ambiguity more quickly. */
98ca843c 7105 if (!function_definition_allowed_p
62d1db17 7106 && !decl_specifiers.any_specifiers_p)
39703eb9
MM
7107 {
7108 cp_parser_error (parser, "expected declaration");
7109 goto done;
7110 }
7111
8fbc5ae7
MM
7112 /* If the next two tokens are both identifiers, the code is
7113 erroneous. The usual cause of this situation is code like:
7114
7115 T t;
7116
7117 where "T" should name a type -- but does not. */
de3fe73c
MM
7118 if (!decl_specifiers.type
7119 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8fbc5ae7 7120 {
8d241e0b 7121 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
7122 looking at a declaration. */
7123 cp_parser_commit_to_tentative_parse (parser);
7124 /* Give up. */
39703eb9 7125 goto done;
8fbc5ae7 7126 }
c8094d83 7127
996c2b52
MM
7128 /* If we have seen at least one decl-specifier, and the next token
7129 is not a parenthesis, then we must be looking at a declaration.
7130 (After "int (" we might be looking at a functional cast.) */
c8094d83 7131 if (decl_specifiers.any_specifiers_p
996c2b52
MM
7132 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7133 cp_parser_commit_to_tentative_parse (parser);
8fbc5ae7 7134
a723baf1
MM
7135 /* Keep going until we hit the `;' at the end of the simple
7136 declaration. */
7137 saw_declarator = false;
21526606 7138 while (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
7139 CPP_SEMICOLON))
7140 {
7141 cp_token *token;
7142 bool function_definition_p;
560ad596 7143 tree decl;
a723baf1
MM
7144
7145 saw_declarator = true;
7146 /* Parse the init-declarator. */
62d1db17 7147 decl = cp_parser_init_declarator (parser, &decl_specifiers,
560ad596
MM
7148 function_definition_allowed_p,
7149 /*member_p=*/false,
7150 declares_class_or_enum,
7151 &function_definition_p);
1fb3244a
MM
7152 /* If an error occurred while parsing tentatively, exit quickly.
7153 (That usually happens when in the body of a function; each
7154 statement is treated as a declaration-statement until proven
7155 otherwise.) */
7156 if (cp_parser_error_occurred (parser))
39703eb9 7157 goto done;
a723baf1
MM
7158 /* Handle function definitions specially. */
7159 if (function_definition_p)
7160 {
7161 /* If the next token is a `,', then we are probably
7162 processing something like:
7163
7164 void f() {}, *p;
7165
7166 which is erroneous. */
7167 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7168 error ("mixing declarations and function-definitions is forbidden");
7169 /* Otherwise, we're done with the list of declarators. */
7170 else
24c0ef37 7171 {
cf22909c 7172 pop_deferring_access_checks ();
24c0ef37
GS
7173 return;
7174 }
a723baf1
MM
7175 }
7176 /* The next token should be either a `,' or a `;'. */
7177 token = cp_lexer_peek_token (parser->lexer);
7178 /* If it's a `,', there are more declarators to come. */
7179 if (token->type == CPP_COMMA)
7180 cp_lexer_consume_token (parser->lexer);
7181 /* If it's a `;', we are done. */
7182 else if (token->type == CPP_SEMICOLON)
7183 break;
7184 /* Anything else is an error. */
7185 else
7186 {
996c2b52
MM
7187 /* If we have already issued an error message we don't need
7188 to issue another one. */
7189 if (decl != error_mark_node
0b16f8f4 7190 || cp_parser_uncommitted_to_tentative_parse_p (parser))
2a13a625 7191 cp_parser_error (parser, "expected %<,%> or %<;%>");
a723baf1
MM
7192 /* Skip tokens until we reach the end of the statement. */
7193 cp_parser_skip_to_end_of_statement (parser);
5a98fa7b
MM
7194 /* If the next token is now a `;', consume it. */
7195 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7196 cp_lexer_consume_token (parser->lexer);
39703eb9 7197 goto done;
a723baf1
MM
7198 }
7199 /* After the first time around, a function-definition is not
7200 allowed -- even if it was OK at first. For example:
7201
0cbd7506 7202 int i, f() {}
a723baf1 7203
0cbd7506 7204 is not valid. */
a723baf1
MM
7205 function_definition_allowed_p = false;
7206 }
7207
7208 /* Issue an error message if no declarators are present, and the
7209 decl-specifier-seq does not itself declare a class or
7210 enumeration. */
7211 if (!saw_declarator)
7212 {
7213 if (cp_parser_declares_only_class_p (parser))
62d1db17 7214 shadow_tag (&decl_specifiers);
a723baf1 7215 /* Perform any deferred access checks. */
cf22909c 7216 perform_deferred_access_checks ();
a723baf1
MM
7217 }
7218
7219 /* Consume the `;'. */
7220 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7221
39703eb9
MM
7222 done:
7223 pop_deferring_access_checks ();
a723baf1
MM
7224}
7225
7226/* Parse a decl-specifier-seq.
7227
7228 decl-specifier-seq:
7229 decl-specifier-seq [opt] decl-specifier
7230
7231 decl-specifier:
7232 storage-class-specifier
7233 type-specifier
7234 function-specifier
7235 friend
21526606 7236 typedef
a723baf1
MM
7237
7238 GNU Extension:
7239
15077df5
MM
7240 decl-specifier:
7241 attributes
a723baf1 7242
62d1db17 7243 Set *DECL_SPECS to a representation of the decl-specifier-seq.
a723baf1 7244
eb1aef53 7245 The parser flags FLAGS is used to control type-specifier parsing.
560ad596
MM
7246
7247 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
543ca912 7248 flags:
560ad596
MM
7249
7250 1: one of the decl-specifiers is an elaborated-type-specifier
0cbd7506 7251 (i.e., a type declaration)
560ad596 7252 2: one of the decl-specifiers is an enum-specifier or a
0cbd7506 7253 class-specifier (i.e., a type definition)
98ca843c 7254
560ad596 7255 */
a723baf1 7256
62d1db17 7257static void
21526606 7258cp_parser_decl_specifier_seq (cp_parser* parser,
62d1db17
MM
7259 cp_parser_flags flags,
7260 cp_decl_specifier_seq *decl_specs,
560ad596 7261 int* declares_class_or_enum)
a723baf1 7262{
f2ce60b8 7263 bool constructor_possible_p = !parser->in_declarator_p;
21526606 7264
62d1db17
MM
7265 /* Clear DECL_SPECS. */
7266 clear_decl_specs (decl_specs);
7267
a723baf1 7268 /* Assume no class or enumeration type is declared. */
560ad596 7269 *declares_class_or_enum = 0;
a723baf1 7270
a723baf1
MM
7271 /* Keep reading specifiers until there are no more to read. */
7272 while (true)
7273 {
a723baf1 7274 bool constructor_p;
62d1db17 7275 bool found_decl_spec;
a723baf1
MM
7276 cp_token *token;
7277
7278 /* Peek at the next token. */
7279 token = cp_lexer_peek_token (parser->lexer);
7280 /* Handle attributes. */
7281 if (token->keyword == RID_ATTRIBUTE)
7282 {
7283 /* Parse the attributes. */
98ca843c 7284 decl_specs->attributes
62d1db17
MM
7285 = chainon (decl_specs->attributes,
7286 cp_parser_attributes_opt (parser));
a723baf1
MM
7287 continue;
7288 }
62d1db17
MM
7289 /* Assume we will find a decl-specifier keyword. */
7290 found_decl_spec = true;
a723baf1
MM
7291 /* If the next token is an appropriate keyword, we can simply
7292 add it to the list. */
7293 switch (token->keyword)
7294 {
a723baf1
MM
7295 /* decl-specifier:
7296 friend */
62d1db17
MM
7297 case RID_FRIEND:
7298 if (decl_specs->specs[(int) ds_friend]++)
2a13a625 7299 error ("duplicate %<friend%>");
a723baf1
MM
7300 /* Consume the token. */
7301 cp_lexer_consume_token (parser->lexer);
7302 break;
7303
7304 /* function-specifier:
7305 inline
7306 virtual
7307 explicit */
7308 case RID_INLINE:
7309 case RID_VIRTUAL:
7310 case RID_EXPLICIT:
62d1db17 7311 cp_parser_function_specifier_opt (parser, decl_specs);
a723baf1 7312 break;
21526606 7313
a723baf1
MM
7314 /* decl-specifier:
7315 typedef */
7316 case RID_TYPEDEF:
62d1db17 7317 ++decl_specs->specs[(int) ds_typedef];
a723baf1
MM
7318 /* Consume the token. */
7319 cp_lexer_consume_token (parser->lexer);
2050a1bb
MM
7320 /* A constructor declarator cannot appear in a typedef. */
7321 constructor_possible_p = false;
c006d942
MM
7322 /* The "typedef" keyword can only occur in a declaration; we
7323 may as well commit at this point. */
7324 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
7325 break;
7326
7327 /* storage-class-specifier:
7328 auto
7329 register
7330 static
7331 extern
21526606 7332 mutable
a723baf1 7333
0cbd7506 7334 GNU Extension:
a723baf1
MM
7335 thread */
7336 case RID_AUTO:
62d1db17
MM
7337 /* Consume the token. */
7338 cp_lexer_consume_token (parser->lexer);
7339 cp_parser_set_storage_class (decl_specs, sc_auto);
7340 break;
a723baf1 7341 case RID_REGISTER:
62d1db17
MM
7342 /* Consume the token. */
7343 cp_lexer_consume_token (parser->lexer);
7344 cp_parser_set_storage_class (decl_specs, sc_register);
7345 break;
a723baf1 7346 case RID_STATIC:
62d1db17
MM
7347 /* Consume the token. */
7348 cp_lexer_consume_token (parser->lexer);
7349 if (decl_specs->specs[(int) ds_thread])
f1b90a04 7350 {
2d01edd7 7351 error ("%<__thread%> before %<static%>");
f1b90a04
MM
7352 decl_specs->specs[(int) ds_thread] = 0;
7353 }
7354 cp_parser_set_storage_class (decl_specs, sc_static);
62d1db17 7355 break;
a723baf1 7356 case RID_EXTERN:
62d1db17
MM
7357 /* Consume the token. */
7358 cp_lexer_consume_token (parser->lexer);
7359 if (decl_specs->specs[(int) ds_thread])
f1b90a04 7360 {
2d01edd7 7361 error ("%<__thread%> before %<extern%>");
f1b90a04
MM
7362 decl_specs->specs[(int) ds_thread] = 0;
7363 }
7364 cp_parser_set_storage_class (decl_specs, sc_extern);
62d1db17 7365 break;
a723baf1 7366 case RID_MUTABLE:
62d1db17
MM
7367 /* Consume the token. */
7368 cp_lexer_consume_token (parser->lexer);
7369 cp_parser_set_storage_class (decl_specs, sc_mutable);
7370 break;
a723baf1 7371 case RID_THREAD:
62d1db17
MM
7372 /* Consume the token. */
7373 cp_lexer_consume_token (parser->lexer);
7374 ++decl_specs->specs[(int) ds_thread];
a723baf1 7375 break;
21526606 7376
a723baf1 7377 default:
62d1db17
MM
7378 /* We did not yet find a decl-specifier yet. */
7379 found_decl_spec = false;
a723baf1
MM
7380 break;
7381 }
7382
7383 /* Constructors are a special case. The `S' in `S()' is not a
7384 decl-specifier; it is the beginning of the declarator. */
98ca843c 7385 constructor_p
62d1db17
MM
7386 = (!found_decl_spec
7387 && constructor_possible_p
98ca843c 7388 && (cp_parser_constructor_declarator_p
62d1db17 7389 (parser, decl_specs->specs[(int) ds_friend] != 0)));
a723baf1
MM
7390
7391 /* If we don't have a DECL_SPEC yet, then we must be looking at
7392 a type-specifier. */
62d1db17 7393 if (!found_decl_spec && !constructor_p)
a723baf1 7394 {
560ad596 7395 int decl_spec_declares_class_or_enum;
a723baf1 7396 bool is_cv_qualifier;
62d1db17 7397 tree type_spec;
a723baf1 7398
62d1db17 7399 type_spec
a723baf1 7400 = cp_parser_type_specifier (parser, flags,
62d1db17 7401 decl_specs,
a723baf1
MM
7402 /*is_declaration=*/true,
7403 &decl_spec_declares_class_or_enum,
7404 &is_cv_qualifier);
7405
7406 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7407
7408 /* If this type-specifier referenced a user-defined type
7409 (a typedef, class-name, etc.), then we can't allow any
7410 more such type-specifiers henceforth.
7411
7412 [dcl.spec]
7413
7414 The longest sequence of decl-specifiers that could
7415 possibly be a type name is taken as the
7416 decl-specifier-seq of a declaration. The sequence shall
7417 be self-consistent as described below.
7418
7419 [dcl.type]
7420
7421 As a general rule, at most one type-specifier is allowed
7422 in the complete decl-specifier-seq of a declaration. The
7423 only exceptions are the following:
7424
7425 -- const or volatile can be combined with any other
21526606 7426 type-specifier.
a723baf1
MM
7427
7428 -- signed or unsigned can be combined with char, long,
7429 short, or int.
7430
7431 -- ..
7432
7433 Example:
7434
7435 typedef char* Pc;
7436 void g (const int Pc);
7437
7438 Here, Pc is *not* part of the decl-specifier seq; it's
7439 the declarator. Therefore, once we see a type-specifier
7440 (other than a cv-qualifier), we forbid any additional
7441 user-defined types. We *do* still allow things like `int
7442 int' to be considered a decl-specifier-seq, and issue the
7443 error message later. */
62d1db17 7444 if (type_spec && !is_cv_qualifier)
a723baf1 7445 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
2050a1bb 7446 /* A constructor declarator cannot follow a type-specifier. */
62d1db17 7447 if (type_spec)
a723baf1 7448 {
62d1db17
MM
7449 constructor_possible_p = false;
7450 found_decl_spec = true;
a723baf1 7451 }
a723baf1
MM
7452 }
7453
62d1db17
MM
7454 /* If we still do not have a DECL_SPEC, then there are no more
7455 decl-specifiers. */
7456 if (!found_decl_spec)
7457 break;
a723baf1 7458
62d1db17 7459 decl_specs->any_specifiers_p = true;
a723baf1
MM
7460 /* After we see one decl-specifier, further decl-specifiers are
7461 always optional. */
7462 flags |= CP_PARSER_FLAGS_OPTIONAL;
7463 }
7464
0426c4ca 7465 /* Don't allow a friend specifier with a class definition. */
62d1db17
MM
7466 if (decl_specs->specs[(int) ds_friend] != 0
7467 && (*declares_class_or_enum & 2))
0426c4ca 7468 error ("class definition may not be declared a friend");
a723baf1
MM
7469}
7470
21526606 7471/* Parse an (optional) storage-class-specifier.
a723baf1
MM
7472
7473 storage-class-specifier:
7474 auto
7475 register
7476 static
7477 extern
21526606 7478 mutable
a723baf1
MM
7479
7480 GNU Extension:
7481
7482 storage-class-specifier:
7483 thread
7484
7485 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
21526606 7486
a723baf1 7487static tree
94edc4ab 7488cp_parser_storage_class_specifier_opt (cp_parser* parser)
a723baf1
MM
7489{
7490 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7491 {
7492 case RID_AUTO:
7493 case RID_REGISTER:
7494 case RID_STATIC:
7495 case RID_EXTERN:
7496 case RID_MUTABLE:
7497 case RID_THREAD:
7498 /* Consume the token. */
7499 return cp_lexer_consume_token (parser->lexer)->value;
7500
7501 default:
7502 return NULL_TREE;
7503 }
7504}
7505
21526606 7506/* Parse an (optional) function-specifier.
a723baf1
MM
7507
7508 function-specifier:
7509 inline
7510 virtual
7511 explicit
7512
62d1db17
MM
7513 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7514 Updates DECL_SPECS, if it is non-NULL. */
21526606 7515
a723baf1 7516static tree
62d1db17
MM
7517cp_parser_function_specifier_opt (cp_parser* parser,
7518 cp_decl_specifier_seq *decl_specs)
a723baf1
MM
7519{
7520 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7521 {
7522 case RID_INLINE:
62d1db17
MM
7523 if (decl_specs)
7524 ++decl_specs->specs[(int) ds_inline];
7525 break;
7526
a723baf1 7527 case RID_VIRTUAL:
62d1db17
MM
7528 if (decl_specs)
7529 ++decl_specs->specs[(int) ds_virtual];
7530 break;
7531
a723baf1 7532 case RID_EXPLICIT:
62d1db17
MM
7533 if (decl_specs)
7534 ++decl_specs->specs[(int) ds_explicit];
7535 break;
a723baf1
MM
7536
7537 default:
7538 return NULL_TREE;
7539 }
62d1db17
MM
7540
7541 /* Consume the token. */
7542 return cp_lexer_consume_token (parser->lexer)->value;
a723baf1
MM
7543}
7544
7545/* Parse a linkage-specification.
7546
7547 linkage-specification:
7548 extern string-literal { declaration-seq [opt] }
7549 extern string-literal declaration */
7550
7551static void
94edc4ab 7552cp_parser_linkage_specification (cp_parser* parser)
a723baf1 7553{
a723baf1
MM
7554 tree linkage;
7555
7556 /* Look for the `extern' keyword. */
7557 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7558
c162c75e
MA
7559 /* Look for the string-literal. */
7560 linkage = cp_parser_string_literal (parser, false, false);
a723baf1
MM
7561
7562 /* Transform the literal into an identifier. If the literal is a
7563 wide-character string, or contains embedded NULs, then we can't
7564 handle it as the user wants. */
c162c75e
MA
7565 if (strlen (TREE_STRING_POINTER (linkage))
7566 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
a723baf1
MM
7567 {
7568 cp_parser_error (parser, "invalid linkage-specification");
7569 /* Assume C++ linkage. */
c162c75e 7570 linkage = lang_name_cplusplus;
a723baf1 7571 }
a723baf1 7572 else
c162c75e 7573 linkage = get_identifier (TREE_STRING_POINTER (linkage));
a723baf1
MM
7574
7575 /* We're now using the new linkage. */
7576 push_lang_context (linkage);
7577
7578 /* If the next token is a `{', then we're using the first
7579 production. */
7580 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7581 {
7582 /* Consume the `{' token. */
7583 cp_lexer_consume_token (parser->lexer);
7584 /* Parse the declarations. */
7585 cp_parser_declaration_seq_opt (parser);
7586 /* Look for the closing `}'. */
7587 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7588 }
7589 /* Otherwise, there's just one declaration. */
7590 else
7591 {
7592 bool saved_in_unbraced_linkage_specification_p;
7593
21526606 7594 saved_in_unbraced_linkage_specification_p
a723baf1
MM
7595 = parser->in_unbraced_linkage_specification_p;
7596 parser->in_unbraced_linkage_specification_p = true;
7597 have_extern_spec = true;
7598 cp_parser_declaration (parser);
7599 have_extern_spec = false;
21526606 7600 parser->in_unbraced_linkage_specification_p
a723baf1
MM
7601 = saved_in_unbraced_linkage_specification_p;
7602 }
7603
7604 /* We're done with the linkage-specification. */
7605 pop_lang_context ();
7606}
7607
7608/* Special member functions [gram.special] */
7609
7610/* Parse a conversion-function-id.
7611
7612 conversion-function-id:
21526606 7613 operator conversion-type-id
a723baf1
MM
7614
7615 Returns an IDENTIFIER_NODE representing the operator. */
7616
21526606 7617static tree
94edc4ab 7618cp_parser_conversion_function_id (cp_parser* parser)
a723baf1
MM
7619{
7620 tree type;
7621 tree saved_scope;
7622 tree saved_qualifying_scope;
7623 tree saved_object_scope;
4514aa8c 7624 tree pushed_scope = NULL_TREE;
a723baf1
MM
7625
7626 /* Look for the `operator' token. */
7627 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7628 return error_mark_node;
7629 /* When we parse the conversion-type-id, the current scope will be
7630 reset. However, we need that information in able to look up the
7631 conversion function later, so we save it here. */
7632 saved_scope = parser->scope;
7633 saved_qualifying_scope = parser->qualifying_scope;
7634 saved_object_scope = parser->object_scope;
7635 /* We must enter the scope of the class so that the names of
7636 entities declared within the class are available in the
7637 conversion-type-id. For example, consider:
7638
21526606 7639 struct S {
0cbd7506 7640 typedef int I;
a723baf1
MM
7641 operator I();
7642 };
7643
7644 S::operator I() { ... }
7645
7646 In order to see that `I' is a type-name in the definition, we
7647 must be in the scope of `S'. */
7648 if (saved_scope)
4514aa8c 7649 pushed_scope = push_scope (saved_scope);
a723baf1
MM
7650 /* Parse the conversion-type-id. */
7651 type = cp_parser_conversion_type_id (parser);
7652 /* Leave the scope of the class, if any. */
4514aa8c
NS
7653 if (pushed_scope)
7654 pop_scope (pushed_scope);
a723baf1
MM
7655 /* Restore the saved scope. */
7656 parser->scope = saved_scope;
7657 parser->qualifying_scope = saved_qualifying_scope;
7658 parser->object_scope = saved_object_scope;
7659 /* If the TYPE is invalid, indicate failure. */
7660 if (type == error_mark_node)
7661 return error_mark_node;
7662 return mangle_conv_op_name_for_type (type);
7663}
7664
7665/* Parse a conversion-type-id:
7666
7667 conversion-type-id:
7668 type-specifier-seq conversion-declarator [opt]
7669
7670 Returns the TYPE specified. */
7671
7672static tree
94edc4ab 7673cp_parser_conversion_type_id (cp_parser* parser)
a723baf1
MM
7674{
7675 tree attributes;
62d1db17 7676 cp_decl_specifier_seq type_specifiers;
058b15c1 7677 cp_declarator *declarator;
037cc9c5 7678 tree type_specified;
a723baf1
MM
7679
7680 /* Parse the attributes. */
7681 attributes = cp_parser_attributes_opt (parser);
7682 /* Parse the type-specifiers. */
d4113656
MM
7683 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7684 &type_specifiers);
a723baf1 7685 /* If that didn't work, stop. */
62d1db17 7686 if (type_specifiers.type == error_mark_node)
a723baf1
MM
7687 return error_mark_node;
7688 /* Parse the conversion-declarator. */
7689 declarator = cp_parser_conversion_declarator_opt (parser);
7690
037cc9c5 7691 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
0cbd7506 7692 /*initialized=*/0, &attributes);
037cc9c5
FJ
7693 if (attributes)
7694 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7695 return type_specified;
a723baf1
MM
7696}
7697
7698/* Parse an (optional) conversion-declarator.
7699
7700 conversion-declarator:
21526606 7701 ptr-operator conversion-declarator [opt]
a723baf1 7702
058b15c1 7703 */
a723baf1 7704
058b15c1 7705static cp_declarator *
94edc4ab 7706cp_parser_conversion_declarator_opt (cp_parser* parser)
a723baf1
MM
7707{
7708 enum tree_code code;
7709 tree class_type;
3c01e5df 7710 cp_cv_quals cv_quals;
a723baf1
MM
7711
7712 /* We don't know if there's a ptr-operator next, or not. */
7713 cp_parser_parse_tentatively (parser);
7714 /* Try the ptr-operator. */
3c01e5df 7715 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
a723baf1
MM
7716 /* If it worked, look for more conversion-declarators. */
7717 if (cp_parser_parse_definitely (parser))
7718 {
058b15c1 7719 cp_declarator *declarator;
98ca843c 7720
058b15c1
MM
7721 /* Parse another optional declarator. */
7722 declarator = cp_parser_conversion_declarator_opt (parser);
98ca843c 7723
058b15c1
MM
7724 /* Create the representation of the declarator. */
7725 if (class_type)
3c01e5df 7726 declarator = make_ptrmem_declarator (cv_quals, class_type,
a723baf1 7727 declarator);
058b15c1 7728 else if (code == INDIRECT_REF)
3c01e5df 7729 declarator = make_pointer_declarator (cv_quals, declarator);
058b15c1 7730 else
3c01e5df 7731 declarator = make_reference_declarator (cv_quals, declarator);
98ca843c 7732
058b15c1 7733 return declarator;
a723baf1
MM
7734 }
7735
058b15c1 7736 return NULL;
a723baf1
MM
7737}
7738
7739/* Parse an (optional) ctor-initializer.
7740
7741 ctor-initializer:
21526606 7742 : mem-initializer-list
a723baf1
MM
7743
7744 Returns TRUE iff the ctor-initializer was actually present. */
7745
7746static bool
94edc4ab 7747cp_parser_ctor_initializer_opt (cp_parser* parser)
a723baf1
MM
7748{
7749 /* If the next token is not a `:', then there is no
7750 ctor-initializer. */
7751 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7752 {
7753 /* Do default initialization of any bases and members. */
7754 if (DECL_CONSTRUCTOR_P (current_function_decl))
7755 finish_mem_initializers (NULL_TREE);
7756
7757 return false;
7758 }
7759
7760 /* Consume the `:' token. */
7761 cp_lexer_consume_token (parser->lexer);
7762 /* And the mem-initializer-list. */
7763 cp_parser_mem_initializer_list (parser);
7764
7765 return true;
7766}
7767
7768/* Parse a mem-initializer-list.
7769
7770 mem-initializer-list:
7771 mem-initializer
7772 mem-initializer , mem-initializer-list */
7773
7774static void
94edc4ab 7775cp_parser_mem_initializer_list (cp_parser* parser)
a723baf1
MM
7776{
7777 tree mem_initializer_list = NULL_TREE;
7778
7779 /* Let the semantic analysis code know that we are starting the
7780 mem-initializer-list. */
0e136342
MM
7781 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7782 error ("only constructors take base initializers");
a723baf1
MM
7783
7784 /* Loop through the list. */
7785 while (true)
7786 {
7787 tree mem_initializer;
7788
7789 /* Parse the mem-initializer. */
7790 mem_initializer = cp_parser_mem_initializer (parser);
7791 /* Add it to the list, unless it was erroneous. */
7792 if (mem_initializer)
7793 {
7794 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7795 mem_initializer_list = mem_initializer;
7796 }
7797 /* If the next token is not a `,', we're done. */
7798 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7799 break;
7800 /* Consume the `,' token. */
7801 cp_lexer_consume_token (parser->lexer);
7802 }
7803
7804 /* Perform semantic analysis. */
0e136342
MM
7805 if (DECL_CONSTRUCTOR_P (current_function_decl))
7806 finish_mem_initializers (mem_initializer_list);
a723baf1
MM
7807}
7808
7809/* Parse a mem-initializer.
7810
7811 mem-initializer:
21526606 7812 mem-initializer-id ( expression-list [opt] )
a723baf1
MM
7813
7814 GNU extension:
21526606 7815
a723baf1 7816 mem-initializer:
34cd5ae7 7817 ( expression-list [opt] )
a723baf1
MM
7818
7819 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7820 class) or FIELD_DECL (for a non-static data member) to initialize;
7821 the TREE_VALUE is the expression-list. */
7822
7823static tree
94edc4ab 7824cp_parser_mem_initializer (cp_parser* parser)
a723baf1
MM
7825{
7826 tree mem_initializer_id;
7827 tree expression_list;
1f5a253a 7828 tree member;
21526606 7829
a723baf1
MM
7830 /* Find out what is being initialized. */
7831 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7832 {
7833 pedwarn ("anachronistic old-style base class initializer");
7834 mem_initializer_id = NULL_TREE;
7835 }
7836 else
7837 mem_initializer_id = cp_parser_mem_initializer_id (parser);
1f5a253a
NS
7838 member = expand_member_init (mem_initializer_id);
7839 if (member && !DECL_P (member))
7840 in_base_initializer = 1;
7efa3e22 7841
21526606 7842 expression_list
39703eb9 7843 = cp_parser_parenthesized_expression_list (parser, false,
93678513 7844 /*cast_p=*/false,
39703eb9 7845 /*non_constant_p=*/NULL);
7efa3e22 7846 if (!expression_list)
a723baf1 7847 expression_list = void_type_node;
a723baf1 7848
1f5a253a 7849 in_base_initializer = 0;
21526606 7850
1f5a253a 7851 return member ? build_tree_list (member, expression_list) : NULL_TREE;
a723baf1
MM
7852}
7853
7854/* Parse a mem-initializer-id.
7855
7856 mem-initializer-id:
7857 :: [opt] nested-name-specifier [opt] class-name
21526606 7858 identifier
a723baf1
MM
7859
7860 Returns a TYPE indicating the class to be initializer for the first
7861 production. Returns an IDENTIFIER_NODE indicating the data member
7862 to be initialized for the second production. */
7863
7864static tree
94edc4ab 7865cp_parser_mem_initializer_id (cp_parser* parser)
a723baf1
MM
7866{
7867 bool global_scope_p;
7868 bool nested_name_specifier_p;
8a83a693 7869 bool template_p = false;
a723baf1
MM
7870 tree id;
7871
8a83a693
GB
7872 /* `typename' is not allowed in this context ([temp.res]). */
7873 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7874 {
2a13a625 7875 error ("keyword %<typename%> not allowed in this context (a qualified "
8a83a693
GB
7876 "member initializer is implicitly a type)");
7877 cp_lexer_consume_token (parser->lexer);
7878 }
a723baf1 7879 /* Look for the optional `::' operator. */
21526606
EC
7880 global_scope_p
7881 = (cp_parser_global_scope_opt (parser,
7882 /*current_scope_valid_p=*/false)
a723baf1
MM
7883 != NULL_TREE);
7884 /* Look for the optional nested-name-specifier. The simplest way to
7885 implement:
7886
7887 [temp.res]
7888
7889 The keyword `typename' is not permitted in a base-specifier or
7890 mem-initializer; in these contexts a qualified name that
7891 depends on a template-parameter is implicitly assumed to be a
7892 type name.
7893
7894 is to assume that we have seen the `typename' keyword at this
7895 point. */
21526606 7896 nested_name_specifier_p
a723baf1
MM
7897 = (cp_parser_nested_name_specifier_opt (parser,
7898 /*typename_keyword_p=*/true,
7899 /*check_dependency_p=*/true,
a668c6ad
MM
7900 /*type_p=*/true,
7901 /*is_declaration=*/true)
a723baf1 7902 != NULL_TREE);
8a83a693
GB
7903 if (nested_name_specifier_p)
7904 template_p = cp_parser_optional_template_keyword (parser);
a723baf1
MM
7905 /* If there is a `::' operator or a nested-name-specifier, then we
7906 are definitely looking for a class-name. */
7907 if (global_scope_p || nested_name_specifier_p)
7908 return cp_parser_class_name (parser,
7909 /*typename_keyword_p=*/true,
8a83a693 7910 /*template_keyword_p=*/template_p,
fc6a28d7 7911 none_type,
a723baf1 7912 /*check_dependency_p=*/true,
a668c6ad
MM
7913 /*class_head_p=*/false,
7914 /*is_declaration=*/true);
a723baf1
MM
7915 /* Otherwise, we could also be looking for an ordinary identifier. */
7916 cp_parser_parse_tentatively (parser);
7917 /* Try a class-name. */
21526606 7918 id = cp_parser_class_name (parser,
a723baf1
MM
7919 /*typename_keyword_p=*/true,
7920 /*template_keyword_p=*/false,
fc6a28d7 7921 none_type,
a723baf1 7922 /*check_dependency_p=*/true,
a668c6ad
MM
7923 /*class_head_p=*/false,
7924 /*is_declaration=*/true);
a723baf1
MM
7925 /* If we found one, we're done. */
7926 if (cp_parser_parse_definitely (parser))
7927 return id;
7928 /* Otherwise, look for an ordinary identifier. */
7929 return cp_parser_identifier (parser);
7930}
7931
7932/* Overloading [gram.over] */
7933
7934/* Parse an operator-function-id.
7935
7936 operator-function-id:
21526606 7937 operator operator
a723baf1
MM
7938
7939 Returns an IDENTIFIER_NODE for the operator which is a
7940 human-readable spelling of the identifier, e.g., `operator +'. */
7941
21526606 7942static tree
94edc4ab 7943cp_parser_operator_function_id (cp_parser* parser)
a723baf1
MM
7944{
7945 /* Look for the `operator' keyword. */
7946 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7947 return error_mark_node;
7948 /* And then the name of the operator itself. */
7949 return cp_parser_operator (parser);
7950}
7951
7952/* Parse an operator.
7953
7954 operator:
7955 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7956 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7957 || ++ -- , ->* -> () []
7958
7959 GNU Extensions:
21526606 7960
a723baf1
MM
7961 operator:
7962 <? >? <?= >?=
7963
7964 Returns an IDENTIFIER_NODE for the operator which is a
7965 human-readable spelling of the identifier, e.g., `operator +'. */
21526606 7966
a723baf1 7967static tree
94edc4ab 7968cp_parser_operator (cp_parser* parser)
a723baf1
MM
7969{
7970 tree id = NULL_TREE;
7971 cp_token *token;
7972
7973 /* Peek at the next token. */
7974 token = cp_lexer_peek_token (parser->lexer);
7975 /* Figure out which operator we have. */
7976 switch (token->type)
7977 {
7978 case CPP_KEYWORD:
7979 {
7980 enum tree_code op;
7981
7982 /* The keyword should be either `new' or `delete'. */
7983 if (token->keyword == RID_NEW)
7984 op = NEW_EXPR;
7985 else if (token->keyword == RID_DELETE)
7986 op = DELETE_EXPR;
7987 else
7988 break;
7989
7990 /* Consume the `new' or `delete' token. */
7991 cp_lexer_consume_token (parser->lexer);
7992
7993 /* Peek at the next token. */
7994 token = cp_lexer_peek_token (parser->lexer);
7995 /* If it's a `[' token then this is the array variant of the
7996 operator. */
7997 if (token->type == CPP_OPEN_SQUARE)
7998 {
7999 /* Consume the `[' token. */
8000 cp_lexer_consume_token (parser->lexer);
8001 /* Look for the `]' token. */
8002 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
21526606 8003 id = ansi_opname (op == NEW_EXPR
a723baf1
MM
8004 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8005 }
8006 /* Otherwise, we have the non-array variant. */
8007 else
8008 id = ansi_opname (op);
8009
8010 return id;
8011 }
8012
8013 case CPP_PLUS:
8014 id = ansi_opname (PLUS_EXPR);
8015 break;
8016
8017 case CPP_MINUS:
8018 id = ansi_opname (MINUS_EXPR);
8019 break;
8020
8021 case CPP_MULT:
8022 id = ansi_opname (MULT_EXPR);
8023 break;
8024
8025 case CPP_DIV:
8026 id = ansi_opname (TRUNC_DIV_EXPR);
8027 break;
8028
8029 case CPP_MOD:
8030 id = ansi_opname (TRUNC_MOD_EXPR);
8031 break;
8032
8033 case CPP_XOR:
8034 id = ansi_opname (BIT_XOR_EXPR);
8035 break;
8036
8037 case CPP_AND:
8038 id = ansi_opname (BIT_AND_EXPR);
8039 break;
8040
8041 case CPP_OR:
8042 id = ansi_opname (BIT_IOR_EXPR);
8043 break;
8044
8045 case CPP_COMPL:
8046 id = ansi_opname (BIT_NOT_EXPR);
8047 break;
21526606 8048
a723baf1
MM
8049 case CPP_NOT:
8050 id = ansi_opname (TRUTH_NOT_EXPR);
8051 break;
8052
8053 case CPP_EQ:
8054 id = ansi_assopname (NOP_EXPR);
8055 break;
8056
8057 case CPP_LESS:
8058 id = ansi_opname (LT_EXPR);
8059 break;
8060
8061 case CPP_GREATER:
8062 id = ansi_opname (GT_EXPR);
8063 break;
8064
8065 case CPP_PLUS_EQ:
8066 id = ansi_assopname (PLUS_EXPR);
8067 break;
8068
8069 case CPP_MINUS_EQ:
8070 id = ansi_assopname (MINUS_EXPR);
8071 break;
8072
8073 case CPP_MULT_EQ:
8074 id = ansi_assopname (MULT_EXPR);
8075 break;
8076
8077 case CPP_DIV_EQ:
8078 id = ansi_assopname (TRUNC_DIV_EXPR);
8079 break;
8080
8081 case CPP_MOD_EQ:
8082 id = ansi_assopname (TRUNC_MOD_EXPR);
8083 break;
8084
8085 case CPP_XOR_EQ:
8086 id = ansi_assopname (BIT_XOR_EXPR);
8087 break;
8088
8089 case CPP_AND_EQ:
8090 id = ansi_assopname (BIT_AND_EXPR);
8091 break;
8092
8093 case CPP_OR_EQ:
8094 id = ansi_assopname (BIT_IOR_EXPR);
8095 break;
8096
8097 case CPP_LSHIFT:
8098 id = ansi_opname (LSHIFT_EXPR);
8099 break;
8100
8101 case CPP_RSHIFT:
8102 id = ansi_opname (RSHIFT_EXPR);
8103 break;
8104
8105 case CPP_LSHIFT_EQ:
8106 id = ansi_assopname (LSHIFT_EXPR);
8107 break;
8108
8109 case CPP_RSHIFT_EQ:
8110 id = ansi_assopname (RSHIFT_EXPR);
8111 break;
8112
8113 case CPP_EQ_EQ:
8114 id = ansi_opname (EQ_EXPR);
8115 break;
8116
8117 case CPP_NOT_EQ:
8118 id = ansi_opname (NE_EXPR);
8119 break;
8120
8121 case CPP_LESS_EQ:
8122 id = ansi_opname (LE_EXPR);
8123 break;
8124
8125 case CPP_GREATER_EQ:
8126 id = ansi_opname (GE_EXPR);
8127 break;
8128
8129 case CPP_AND_AND:
8130 id = ansi_opname (TRUTH_ANDIF_EXPR);
8131 break;
8132
8133 case CPP_OR_OR:
8134 id = ansi_opname (TRUTH_ORIF_EXPR);
8135 break;
21526606 8136
a723baf1
MM
8137 case CPP_PLUS_PLUS:
8138 id = ansi_opname (POSTINCREMENT_EXPR);
8139 break;
8140
8141 case CPP_MINUS_MINUS:
8142 id = ansi_opname (PREDECREMENT_EXPR);
8143 break;
8144
8145 case CPP_COMMA:
8146 id = ansi_opname (COMPOUND_EXPR);
8147 break;
8148
8149 case CPP_DEREF_STAR:
8150 id = ansi_opname (MEMBER_REF);
8151 break;
8152
8153 case CPP_DEREF:
8154 id = ansi_opname (COMPONENT_REF);
8155 break;
8156
8157 case CPP_OPEN_PAREN:
8158 /* Consume the `('. */
8159 cp_lexer_consume_token (parser->lexer);
8160 /* Look for the matching `)'. */
8161 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8162 return ansi_opname (CALL_EXPR);
8163
8164 case CPP_OPEN_SQUARE:
8165 /* Consume the `['. */
8166 cp_lexer_consume_token (parser->lexer);
8167 /* Look for the matching `]'. */
8168 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8169 return ansi_opname (ARRAY_REF);
8170
8171 /* Extensions. */
8172 case CPP_MIN:
8173 id = ansi_opname (MIN_EXPR);
8ff24a79 8174 cp_parser_warn_min_max ();
a723baf1
MM
8175 break;
8176
8177 case CPP_MAX:
8178 id = ansi_opname (MAX_EXPR);
8ff24a79 8179 cp_parser_warn_min_max ();
a723baf1
MM
8180 break;
8181
8182 case CPP_MIN_EQ:
8183 id = ansi_assopname (MIN_EXPR);
8ff24a79 8184 cp_parser_warn_min_max ();
a723baf1
MM
8185 break;
8186
8187 case CPP_MAX_EQ:
8188 id = ansi_assopname (MAX_EXPR);
8ff24a79 8189 cp_parser_warn_min_max ();
a723baf1
MM
8190 break;
8191
8192 default:
8193 /* Anything else is an error. */
8194 break;
8195 }
8196
8197 /* If we have selected an identifier, we need to consume the
8198 operator token. */
8199 if (id)
8200 cp_lexer_consume_token (parser->lexer);
8201 /* Otherwise, no valid operator name was present. */
8202 else
8203 {
8204 cp_parser_error (parser, "expected operator");
8205 id = error_mark_node;
8206 }
8207
8208 return id;
8209}
8210
8211/* Parse a template-declaration.
8212
8213 template-declaration:
21526606 8214 export [opt] template < template-parameter-list > declaration
a723baf1
MM
8215
8216 If MEMBER_P is TRUE, this template-declaration occurs within a
21526606 8217 class-specifier.
a723baf1
MM
8218
8219 The grammar rule given by the standard isn't correct. What
8220 is really meant is:
8221
8222 template-declaration:
21526606 8223 export [opt] template-parameter-list-seq
a723baf1 8224 decl-specifier-seq [opt] init-declarator [opt] ;
21526606 8225 export [opt] template-parameter-list-seq
a723baf1
MM
8226 function-definition
8227
8228 template-parameter-list-seq:
8229 template-parameter-list-seq [opt]
8230 template < template-parameter-list > */
8231
8232static void
94edc4ab 8233cp_parser_template_declaration (cp_parser* parser, bool member_p)
a723baf1
MM
8234{
8235 /* Check for `export'. */
8236 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8237 {
8238 /* Consume the `export' token. */
8239 cp_lexer_consume_token (parser->lexer);
8240 /* Warn that we do not support `export'. */
d4ee4d25 8241 warning (0, "keyword %<export%> not implemented, and will be ignored");
a723baf1
MM
8242 }
8243
8244 cp_parser_template_declaration_after_export (parser, member_p);
8245}
8246
8247/* Parse a template-parameter-list.
8248
8249 template-parameter-list:
8250 template-parameter
8251 template-parameter-list , template-parameter
8252
8253 Returns a TREE_LIST. Each node represents a template parameter.
8254 The nodes are connected via their TREE_CHAINs. */
8255
8256static tree
94edc4ab 8257cp_parser_template_parameter_list (cp_parser* parser)
a723baf1
MM
8258{
8259 tree parameter_list = NULL_TREE;
8260
8261 while (true)
8262 {
8263 tree parameter;
8264 cp_token *token;
058b15c1 8265 bool is_non_type;
a723baf1
MM
8266
8267 /* Parse the template-parameter. */
058b15c1 8268 parameter = cp_parser_template_parameter (parser, &is_non_type);
a723baf1 8269 /* Add it to the list. */
943e3ede
MM
8270 if (parameter != error_mark_node)
8271 parameter_list = process_template_parm (parameter_list,
8272 parameter,
8273 is_non_type);
a723baf1
MM
8274 /* Peek at the next token. */
8275 token = cp_lexer_peek_token (parser->lexer);
8276 /* If it's not a `,', we're done. */
8277 if (token->type != CPP_COMMA)
8278 break;
8279 /* Otherwise, consume the `,' token. */
8280 cp_lexer_consume_token (parser->lexer);
8281 }
8282
8283 return parameter_list;
8284}
8285
8286/* Parse a template-parameter.
8287
8288 template-parameter:
8289 type-parameter
8290 parameter-declaration
8291
943e3ede
MM
8292 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8293 the parameter. The TREE_PURPOSE is the default value, if any.
8294 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8295 iff this parameter is a non-type parameter. */
a723baf1
MM
8296
8297static tree
058b15c1 8298cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
a723baf1
MM
8299{
8300 cp_token *token;
62d1db17 8301 cp_parameter_declarator *parameter_declarator;
943e3ede 8302 tree parm;
a723baf1 8303
058b15c1
MM
8304 /* Assume it is a type parameter or a template parameter. */
8305 *is_non_type = false;
a723baf1
MM
8306 /* Peek at the next token. */
8307 token = cp_lexer_peek_token (parser->lexer);
8308 /* If it is `class' or `template', we have a type-parameter. */
8309 if (token->keyword == RID_TEMPLATE)
8310 return cp_parser_type_parameter (parser);
8311 /* If it is `class' or `typename' we do not know yet whether it is a
8312 type parameter or a non-type parameter. Consider:
8313
8314 template <typename T, typename T::X X> ...
8315
8316 or:
21526606 8317
a723baf1
MM
8318 template <class C, class D*> ...
8319
8320 Here, the first parameter is a type parameter, and the second is
8321 a non-type parameter. We can tell by looking at the token after
8322 the identifier -- if it is a `,', `=', or `>' then we have a type
8323 parameter. */
8324 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8325 {
8326 /* Peek at the token after `class' or `typename'. */
8327 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8328 /* If it's an identifier, skip it. */
8329 if (token->type == CPP_NAME)
8330 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8331 /* Now, see if the token looks like the end of a template
8332 parameter. */
21526606 8333 if (token->type == CPP_COMMA
a723baf1
MM
8334 || token->type == CPP_EQ
8335 || token->type == CPP_GREATER)
8336 return cp_parser_type_parameter (parser);
8337 }
8338
21526606 8339 /* Otherwise, it is a non-type parameter.
a723baf1
MM
8340
8341 [temp.param]
8342
8343 When parsing a default template-argument for a non-type
8344 template-parameter, the first non-nested `>' is taken as the end
8345 of the template parameter-list rather than a greater-than
8346 operator. */
058b15c1
MM
8347 *is_non_type = true;
8348 parameter_declarator
8349 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8350 /*parenthesized_p=*/NULL);
943e3ede
MM
8351 parm = grokdeclarator (parameter_declarator->declarator,
8352 &parameter_declarator->decl_specifiers,
8353 PARM, /*initialized=*/0,
8354 /*attrlist=*/NULL);
8355 if (parm == error_mark_node)
8356 return error_mark_node;
8357 return build_tree_list (parameter_declarator->default_argument, parm);
a723baf1
MM
8358}
8359
8360/* Parse a type-parameter.
8361
8362 type-parameter:
8363 class identifier [opt]
8364 class identifier [opt] = type-id
8365 typename identifier [opt]
8366 typename identifier [opt] = type-id
8367 template < template-parameter-list > class identifier [opt]
21526606
EC
8368 template < template-parameter-list > class identifier [opt]
8369 = id-expression
a723baf1
MM
8370
8371 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8372 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8373 the declaration of the parameter. */
8374
8375static tree
94edc4ab 8376cp_parser_type_parameter (cp_parser* parser)
a723baf1
MM
8377{
8378 cp_token *token;
8379 tree parameter;
8380
8381 /* Look for a keyword to tell us what kind of parameter this is. */
21526606 8382 token = cp_parser_require (parser, CPP_KEYWORD,
8a6393df 8383 "`class', `typename', or `template'");
a723baf1
MM
8384 if (!token)
8385 return error_mark_node;
8386
8387 switch (token->keyword)
8388 {
8389 case RID_CLASS:
8390 case RID_TYPENAME:
8391 {
8392 tree identifier;
8393 tree default_argument;
8394
8395 /* If the next token is an identifier, then it names the
0cbd7506 8396 parameter. */
a723baf1
MM
8397 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8398 identifier = cp_parser_identifier (parser);
8399 else
8400 identifier = NULL_TREE;
8401
8402 /* Create the parameter. */
8403 parameter = finish_template_type_parm (class_type_node, identifier);
8404
8405 /* If the next token is an `=', we have a default argument. */
8406 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8407 {
8408 /* Consume the `=' token. */
8409 cp_lexer_consume_token (parser->lexer);
34cd5ae7 8410 /* Parse the default-argument. */
a723baf1
MM
8411 default_argument = cp_parser_type_id (parser);
8412 }
8413 else
8414 default_argument = NULL_TREE;
8415
8416 /* Create the combined representation of the parameter and the
8417 default argument. */
c67d36d0 8418 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
8419 }
8420 break;
8421
8422 case RID_TEMPLATE:
8423 {
8424 tree parameter_list;
8425 tree identifier;
8426 tree default_argument;
8427
8428 /* Look for the `<'. */
8429 cp_parser_require (parser, CPP_LESS, "`<'");
8430 /* Parse the template-parameter-list. */
8431 begin_template_parm_list ();
21526606 8432 parameter_list
a723baf1
MM
8433 = cp_parser_template_parameter_list (parser);
8434 parameter_list = end_template_parm_list (parameter_list);
8435 /* Look for the `>'. */
8436 cp_parser_require (parser, CPP_GREATER, "`>'");
8437 /* Look for the `class' keyword. */
8438 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8439 /* If the next token is an `=', then there is a
8440 default-argument. If the next token is a `>', we are at
8441 the end of the parameter-list. If the next token is a `,',
8442 then we are at the end of this parameter. */
8443 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8444 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8445 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
71bd7186
MM
8446 {
8447 identifier = cp_parser_identifier (parser);
03fd3f84 8448 /* Treat invalid names as if the parameter were nameless. */
71bd7186
MM
8449 if (identifier == error_mark_node)
8450 identifier = NULL_TREE;
8451 }
a723baf1
MM
8452 else
8453 identifier = NULL_TREE;
71bd7186 8454
a723baf1
MM
8455 /* Create the template parameter. */
8456 parameter = finish_template_template_parm (class_type_node,
8457 identifier);
21526606 8458
a723baf1
MM
8459 /* If the next token is an `=', then there is a
8460 default-argument. */
8461 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8462 {
b0bc6e8e
KL
8463 bool is_template;
8464
a723baf1
MM
8465 /* Consume the `='. */
8466 cp_lexer_consume_token (parser->lexer);
8467 /* Parse the id-expression. */
21526606 8468 default_argument
a723baf1
MM
8469 = cp_parser_id_expression (parser,
8470 /*template_keyword_p=*/false,
8471 /*check_dependency_p=*/true,
b0bc6e8e 8472 /*template_p=*/&is_template,
f3c2dfc6 8473 /*declarator_p=*/false);
a3a503a5
GB
8474 if (TREE_CODE (default_argument) == TYPE_DECL)
8475 /* If the id-expression was a template-id that refers to
8476 a template-class, we already have the declaration here,
8477 so no further lookup is needed. */
8478 ;
8479 else
8480 /* Look up the name. */
21526606 8481 default_argument
a3a503a5 8482 = cp_parser_lookup_name (parser, default_argument,
fc6a28d7
MM
8483 none_type,
8484 /*is_template=*/is_template,
8485 /*is_namespace=*/false,
8486 /*check_dependency=*/true,
8487 /*ambiguous_p=*/NULL);
a723baf1
MM
8488 /* See if the default argument is valid. */
8489 default_argument
8490 = check_template_template_default_arg (default_argument);
8491 }
8492 else
8493 default_argument = NULL_TREE;
8494
8495 /* Create the combined representation of the parameter and the
8496 default argument. */
71bd7186 8497 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
8498 }
8499 break;
8500
8501 default:
71bd7186
MM
8502 gcc_unreachable ();
8503 break;
a723baf1 8504 }
21526606 8505
a723baf1
MM
8506 return parameter;
8507}
8508
8509/* Parse a template-id.
8510
8511 template-id:
8512 template-name < template-argument-list [opt] >
8513
8514 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8515 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8516 returned. Otherwise, if the template-name names a function, or set
8517 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
21526606 8518 names a class, returns a TYPE_DECL for the specialization.
a723baf1
MM
8519
8520 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8521 uninstantiated templates. */
8522
8523static tree
21526606
EC
8524cp_parser_template_id (cp_parser *parser,
8525 bool template_keyword_p,
a668c6ad
MM
8526 bool check_dependency_p,
8527 bool is_declaration)
a723baf1
MM
8528{
8529 tree template;
8530 tree arguments;
a723baf1 8531 tree template_id;
0c5e4866 8532 cp_token_position start_of_id = 0;
a723baf1 8533 tree access_check = NULL_TREE;
f4abade9 8534 cp_token *next_token, *next_token_2;
a668c6ad 8535 bool is_identifier;
a723baf1
MM
8536
8537 /* If the next token corresponds to a template-id, there is no need
8538 to reparse it. */
2050a1bb
MM
8539 next_token = cp_lexer_peek_token (parser->lexer);
8540 if (next_token->type == CPP_TEMPLATE_ID)
a723baf1
MM
8541 {
8542 tree value;
8543 tree check;
8544
8545 /* Get the stored value. */
8546 value = cp_lexer_consume_token (parser->lexer)->value;
8547 /* Perform any access checks that were deferred. */
8548 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c
KL
8549 perform_or_defer_access_check (TREE_PURPOSE (check),
8550 TREE_VALUE (check));
a723baf1
MM
8551 /* Return the stored value. */
8552 return TREE_VALUE (value);
8553 }
8554
2050a1bb
MM
8555 /* Avoid performing name lookup if there is no possibility of
8556 finding a template-id. */
8557 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8558 || (next_token->type == CPP_NAME
21526606 8559 && !cp_parser_nth_token_starts_template_argument_list_p
f4abade9 8560 (parser, 2)))
2050a1bb
MM
8561 {
8562 cp_parser_error (parser, "expected template-id");
8563 return error_mark_node;
8564 }
8565
a723baf1 8566 /* Remember where the template-id starts. */
0b16f8f4 8567 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
0c5e4866 8568 start_of_id = cp_lexer_token_position (parser->lexer, false);
a723baf1 8569
8d241e0b 8570 push_deferring_access_checks (dk_deferred);
cf22909c 8571
a723baf1 8572 /* Parse the template-name. */
a668c6ad 8573 is_identifier = false;
a723baf1 8574 template = cp_parser_template_name (parser, template_keyword_p,
a668c6ad
MM
8575 check_dependency_p,
8576 is_declaration,
8577 &is_identifier);
8578 if (template == error_mark_node || is_identifier)
cf22909c
KL
8579 {
8580 pop_deferring_access_checks ();
a668c6ad 8581 return template;
cf22909c 8582 }
a723baf1 8583
21526606 8584 /* If we find the sequence `[:' after a template-name, it's probably
f4abade9
GB
8585 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8586 parse correctly the argument list. */
2cfe82fe 8587 next_token = cp_lexer_peek_token (parser->lexer);
f4abade9 8588 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
21526606 8589 if (next_token->type == CPP_OPEN_SQUARE
f4abade9 8590 && next_token->flags & DIGRAPH
21526606 8591 && next_token_2->type == CPP_COLON
f4abade9 8592 && !(next_token_2->flags & PREV_WHITE))
cf22909c 8593 {
f4abade9
GB
8594 cp_parser_parse_tentatively (parser);
8595 /* Change `:' into `::'. */
8596 next_token_2->type = CPP_SCOPE;
8597 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
0cbd7506 8598 CPP_LESS. */
f4abade9
GB
8599 cp_lexer_consume_token (parser->lexer);
8600 /* Parse the arguments. */
8601 arguments = cp_parser_enclosed_template_argument_list (parser);
8602 if (!cp_parser_parse_definitely (parser))
8603 {
8604 /* If we couldn't parse an argument list, then we revert our changes
8605 and return simply an error. Maybe this is not a template-id
8606 after all. */
8607 next_token_2->type = CPP_COLON;
2a13a625 8608 cp_parser_error (parser, "expected %<<%>");
f4abade9
GB
8609 pop_deferring_access_checks ();
8610 return error_mark_node;
8611 }
8612 /* Otherwise, emit an error about the invalid digraph, but continue
0cbd7506 8613 parsing because we got our argument list. */
2a13a625
GDR
8614 pedwarn ("%<<::%> cannot begin a template-argument list");
8615 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8616 "between %<<%> and %<::%>");
f4abade9
GB
8617 if (!flag_permissive)
8618 {
8619 static bool hint;
8620 if (!hint)
8621 {
2a13a625 8622 inform ("(if you use -fpermissive G++ will accept your code)");
f4abade9
GB
8623 hint = true;
8624 }
8625 }
8626 }
8627 else
8628 {
8629 /* Look for the `<' that starts the template-argument-list. */
8630 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8631 {
8632 pop_deferring_access_checks ();
8633 return error_mark_node;
8634 }
8635 /* Parse the arguments. */
8636 arguments = cp_parser_enclosed_template_argument_list (parser);
cf22909c 8637 }
a723baf1
MM
8638
8639 /* Build a representation of the specialization. */
8640 if (TREE_CODE (template) == IDENTIFIER_NODE)
8641 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8642 else if (DECL_CLASS_TEMPLATE_P (template)
8643 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
21526606
EC
8644 template_id
8645 = finish_template_type (template, arguments,
8646 cp_lexer_next_token_is (parser->lexer,
a723baf1
MM
8647 CPP_SCOPE));
8648 else
8649 {
8650 /* If it's not a class-template or a template-template, it should be
8651 a function-template. */
50bc768d
NS
8652 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8653 || TREE_CODE (template) == OVERLOAD
8654 || BASELINK_P (template)));
21526606 8655
a723baf1
MM
8656 template_id = lookup_template_function (template, arguments);
8657 }
21526606 8658
cf22909c
KL
8659 /* Retrieve any deferred checks. Do not pop this access checks yet
8660 so the memory will not be reclaimed during token replacing below. */
8661 access_check = get_deferred_access_checks ();
8662
a723baf1
MM
8663 /* If parsing tentatively, replace the sequence of tokens that makes
8664 up the template-id with a CPP_TEMPLATE_ID token. That way,
8665 should we re-parse the token stream, we will not have to repeat
8666 the effort required to do the parse, nor will we issue duplicate
8667 error messages about problems during instantiation of the
e894ab29 8668 template. */
c8a7ed43 8669 if (start_of_id)
a723baf1 8670 {
0c5e4866 8671 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
c8094d83 8672
a723baf1
MM
8673 /* Reset the contents of the START_OF_ID token. */
8674 token->type = CPP_TEMPLATE_ID;
8675 token->value = build_tree_list (access_check, template_id);
8676 token->keyword = RID_MAX;
c8094d83 8677
a723baf1 8678 /* Purge all subsequent tokens. */
0c5e4866 8679 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
c8a7ed43
AO
8680
8681 /* ??? Can we actually assume that, if template_id ==
8682 error_mark_node, we will have issued a diagnostic to the
8683 user, as opposed to simply marking the tentative parse as
8684 failed? */
8685 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8686 error ("parse error in template argument list");
a723baf1
MM
8687 }
8688
cf22909c 8689 pop_deferring_access_checks ();
a723baf1
MM
8690 return template_id;
8691}
8692
8693/* Parse a template-name.
8694
8695 template-name:
8696 identifier
21526606 8697
a723baf1
MM
8698 The standard should actually say:
8699
8700 template-name:
8701 identifier
8702 operator-function-id
a723baf1
MM
8703
8704 A defect report has been filed about this issue.
8705
0d956474
GB
8706 A conversion-function-id cannot be a template name because they cannot
8707 be part of a template-id. In fact, looking at this code:
8708
8709 a.operator K<int>()
8710
8711 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
21526606 8712 It is impossible to call a templated conversion-function-id with an
0d956474
GB
8713 explicit argument list, since the only allowed template parameter is
8714 the type to which it is converting.
8715
a723baf1
MM
8716 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8717 `template' keyword, in a construction like:
8718
8719 T::template f<3>()
8720
8721 In that case `f' is taken to be a template-name, even though there
8722 is no way of knowing for sure.
8723
8724 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8725 name refers to a set of overloaded functions, at least one of which
8726 is a template, or an IDENTIFIER_NODE with the name of the template,
8727 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8728 names are looked up inside uninstantiated templates. */
8729
8730static tree
21526606 8731cp_parser_template_name (cp_parser* parser,
0cbd7506
MS
8732 bool template_keyword_p,
8733 bool check_dependency_p,
a668c6ad
MM
8734 bool is_declaration,
8735 bool *is_identifier)
a723baf1
MM
8736{
8737 tree identifier;
8738 tree decl;
8739 tree fns;
8740
8741 /* If the next token is `operator', then we have either an
8742 operator-function-id or a conversion-function-id. */
8743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8744 {
8745 /* We don't know whether we're looking at an
8746 operator-function-id or a conversion-function-id. */
8747 cp_parser_parse_tentatively (parser);
8748 /* Try an operator-function-id. */
8749 identifier = cp_parser_operator_function_id (parser);
8750 /* If that didn't work, try a conversion-function-id. */
8751 if (!cp_parser_parse_definitely (parser))
0cbd7506 8752 {
0d956474
GB
8753 cp_parser_error (parser, "expected template-name");
8754 return error_mark_node;
0cbd7506 8755 }
a723baf1
MM
8756 }
8757 /* Look for the identifier. */
8758 else
8759 identifier = cp_parser_identifier (parser);
21526606 8760
a723baf1
MM
8761 /* If we didn't find an identifier, we don't have a template-id. */
8762 if (identifier == error_mark_node)
8763 return error_mark_node;
8764
8765 /* If the name immediately followed the `template' keyword, then it
8766 is a template-name. However, if the next token is not `<', then
8767 we do not treat it as a template-name, since it is not being used
8768 as part of a template-id. This enables us to handle constructs
8769 like:
8770
8771 template <typename T> struct S { S(); };
8772 template <typename T> S<T>::S();
8773
8774 correctly. We would treat `S' as a template -- if it were `S<T>'
8775 -- but we do not if there is no `<'. */
a668c6ad
MM
8776
8777 if (processing_template_decl
f4abade9 8778 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
a668c6ad
MM
8779 {
8780 /* In a declaration, in a dependent context, we pretend that the
8781 "template" keyword was present in order to improve error
8782 recovery. For example, given:
21526606 8783
a668c6ad 8784 template <typename T> void f(T::X<int>);
21526606 8785
a668c6ad 8786 we want to treat "X<int>" as a template-id. */
21526606
EC
8787 if (is_declaration
8788 && !template_keyword_p
a668c6ad 8789 && parser->scope && TYPE_P (parser->scope)
a52eb3bc 8790 && check_dependency_p
4e0f4df5
GB
8791 && dependent_type_p (parser->scope)
8792 /* Do not do this for dtors (or ctors), since they never
8793 need the template keyword before their name. */
8794 && !constructor_name_p (identifier, parser->scope))
a668c6ad 8795 {
0c5e4866 8796 cp_token_position start = 0;
c8094d83 8797
a668c6ad 8798 /* Explain what went wrong. */
2a13a625
GDR
8799 error ("non-template %qD used as template", identifier);
8800 inform ("use %<%T::template %D%> to indicate that it is a template",
4e0f4df5 8801 parser->scope, identifier);
0b16f8f4
VR
8802 /* If parsing tentatively, find the location of the "<" token. */
8803 if (cp_parser_simulate_error (parser))
8804 start = cp_lexer_token_position (parser->lexer, true);
a668c6ad
MM
8805 /* Parse the template arguments so that we can issue error
8806 messages about them. */
8807 cp_lexer_consume_token (parser->lexer);
8808 cp_parser_enclosed_template_argument_list (parser);
8809 /* Skip tokens until we find a good place from which to
8810 continue parsing. */
8811 cp_parser_skip_to_closing_parenthesis (parser,
8812 /*recovering=*/true,
8813 /*or_comma=*/true,
8814 /*consume_paren=*/false);
8815 /* If parsing tentatively, permanently remove the
8816 template argument list. That will prevent duplicate
8817 error messages from being issued about the missing
8818 "template" keyword. */
0c5e4866
NS
8819 if (start)
8820 cp_lexer_purge_tokens_after (parser->lexer, start);
a668c6ad
MM
8821 if (is_identifier)
8822 *is_identifier = true;
8823 return identifier;
8824 }
9d363a56
MM
8825
8826 /* If the "template" keyword is present, then there is generally
8827 no point in doing name-lookup, so we just return IDENTIFIER.
8828 But, if the qualifying scope is non-dependent then we can
8829 (and must) do name-lookup normally. */
8830 if (template_keyword_p
8831 && (!parser->scope
98ca843c 8832 || (TYPE_P (parser->scope)
9d363a56 8833 && dependent_type_p (parser->scope))))
a668c6ad
MM
8834 return identifier;
8835 }
a723baf1
MM
8836
8837 /* Look up the name. */
8838 decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 8839 none_type,
b0bc6e8e 8840 /*is_template=*/false,
eea9800f 8841 /*is_namespace=*/false,
8f78f01f
MM
8842 check_dependency_p,
8843 /*ambiguous_p=*/NULL);
a723baf1
MM
8844 decl = maybe_get_template_decl_from_type_decl (decl);
8845
8846 /* If DECL is a template, then the name was a template-name. */
8847 if (TREE_CODE (decl) == TEMPLATE_DECL)
8848 ;
21526606 8849 else
a723baf1 8850 {
d58a2b83
MM
8851 tree fn = NULL_TREE;
8852
a723baf1
MM
8853 /* The standard does not explicitly indicate whether a name that
8854 names a set of overloaded declarations, some of which are
8855 templates, is a template-name. However, such a name should
8856 be a template-name; otherwise, there is no way to form a
8857 template-id for the overloaded templates. */
8858 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8859 if (TREE_CODE (fns) == OVERLOAD)
d58a2b83
MM
8860 for (fn = fns; fn; fn = OVL_NEXT (fn))
8861 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8862 break;
21526606 8863
d58a2b83 8864 if (!fn)
a723baf1 8865 {
d58a2b83 8866 /* The name does not name a template. */
a723baf1
MM
8867 cp_parser_error (parser, "expected template-name");
8868 return error_mark_node;
8869 }
8870 }
8871
8872 /* If DECL is dependent, and refers to a function, then just return
8873 its name; we will look it up again during template instantiation. */
8874 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8875 {
8876 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
1fb3244a 8877 if (TYPE_P (scope) && dependent_type_p (scope))
a723baf1
MM
8878 return identifier;
8879 }
8880
8881 return decl;
8882}
8883
8884/* Parse a template-argument-list.
8885
8886 template-argument-list:
8887 template-argument
8888 template-argument-list , template-argument
8889
04c06002 8890 Returns a TREE_VEC containing the arguments. */
a723baf1
MM
8891
8892static tree
94edc4ab 8893cp_parser_template_argument_list (cp_parser* parser)
a723baf1 8894{
bf12d54d
NS
8895 tree fixed_args[10];
8896 unsigned n_args = 0;
8897 unsigned alloced = 10;
8898 tree *arg_ary = fixed_args;
8899 tree vec;
4bb8ca28 8900 bool saved_in_template_argument_list_p;
5e9edb0f
MM
8901 bool saved_ice_p;
8902 bool saved_non_ice_p;
a723baf1 8903
4bb8ca28
MM
8904 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8905 parser->in_template_argument_list_p = true;
5e9edb0f
MM
8906 /* Even if the template-id appears in an integral
8907 constant-expression, the contents of the argument list do
8908 not. */
8909 saved_ice_p = parser->integral_constant_expression_p;
8910 parser->integral_constant_expression_p = false;
8911 saved_non_ice_p = parser->non_integral_constant_expression_p;
8912 parser->non_integral_constant_expression_p = false;
02ed62dd 8913 /* Parse the arguments. */
bf12d54d 8914 do
a723baf1
MM
8915 {
8916 tree argument;
8917
bf12d54d 8918 if (n_args)
04c06002 8919 /* Consume the comma. */
bf12d54d 8920 cp_lexer_consume_token (parser->lexer);
21526606 8921
a723baf1
MM
8922 /* Parse the template-argument. */
8923 argument = cp_parser_template_argument (parser);
bf12d54d
NS
8924 if (n_args == alloced)
8925 {
8926 alloced *= 2;
21526606 8927
bf12d54d
NS
8928 if (arg_ary == fixed_args)
8929 {
8930 arg_ary = xmalloc (sizeof (tree) * alloced);
8931 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8932 }
8933 else
8934 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8935 }
8936 arg_ary[n_args++] = argument;
a723baf1 8937 }
bf12d54d
NS
8938 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8939
8940 vec = make_tree_vec (n_args);
a723baf1 8941
bf12d54d
NS
8942 while (n_args--)
8943 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
21526606 8944
bf12d54d
NS
8945 if (arg_ary != fixed_args)
8946 free (arg_ary);
5e9edb0f
MM
8947 parser->non_integral_constant_expression_p = saved_non_ice_p;
8948 parser->integral_constant_expression_p = saved_ice_p;
4bb8ca28 8949 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
bf12d54d 8950 return vec;
a723baf1
MM
8951}
8952
8953/* Parse a template-argument.
8954
8955 template-argument:
8956 assignment-expression
8957 type-id
8958 id-expression
8959
8960 The representation is that of an assignment-expression, type-id, or
8961 id-expression -- except that the qualified id-expression is
8962 evaluated, so that the value returned is either a DECL or an
21526606 8963 OVERLOAD.
d17811fd
MM
8964
8965 Although the standard says "assignment-expression", it forbids
8966 throw-expressions or assignments in the template argument.
8967 Therefore, we use "conditional-expression" instead. */
a723baf1
MM
8968
8969static tree
94edc4ab 8970cp_parser_template_argument (cp_parser* parser)
a723baf1
MM
8971{
8972 tree argument;
8973 bool template_p;
d17811fd 8974 bool address_p;
4d5297fa 8975 bool maybe_type_id = false;
d17811fd 8976 cp_token *token;
b3445994 8977 cp_id_kind idk;
a723baf1
MM
8978
8979 /* There's really no way to know what we're looking at, so we just
21526606 8980 try each alternative in order.
a723baf1
MM
8981
8982 [temp.arg]
8983
8984 In a template-argument, an ambiguity between a type-id and an
8985 expression is resolved to a type-id, regardless of the form of
21526606 8986 the corresponding template-parameter.
a723baf1
MM
8987
8988 Therefore, we try a type-id first. */
8989 cp_parser_parse_tentatively (parser);
a723baf1 8990 argument = cp_parser_type_id (parser);
4d5297fa 8991 /* If there was no error parsing the type-id but the next token is a '>>',
21526606 8992 we probably found a typo for '> >'. But there are type-id which are
4d5297fa
GB
8993 also valid expressions. For instance:
8994
8995 struct X { int operator >> (int); };
8996 template <int V> struct Foo {};
8997 Foo<X () >> 5> r;
8998
8999 Here 'X()' is a valid type-id of a function type, but the user just
9000 wanted to write the expression "X() >> 5". Thus, we remember that we
9001 found a valid type-id, but we still try to parse the argument as an
9002 expression to see what happens. */
9003 if (!cp_parser_error_occurred (parser)
9004 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9005 {
9006 maybe_type_id = true;
9007 cp_parser_abort_tentative_parse (parser);
9008 }
9009 else
9010 {
9011 /* If the next token isn't a `,' or a `>', then this argument wasn't
9012 really finished. This means that the argument is not a valid
9013 type-id. */
9014 if (!cp_parser_next_token_ends_template_argument_p (parser))
9015 cp_parser_error (parser, "expected template-argument");
9016 /* If that worked, we're done. */
9017 if (cp_parser_parse_definitely (parser))
9018 return argument;
9019 }
a723baf1
MM
9020 /* We're still not sure what the argument will be. */
9021 cp_parser_parse_tentatively (parser);
9022 /* Try a template. */
21526606 9023 argument = cp_parser_id_expression (parser,
a723baf1
MM
9024 /*template_keyword_p=*/false,
9025 /*check_dependency_p=*/true,
f3c2dfc6
MM
9026 &template_p,
9027 /*declarator_p=*/false);
a723baf1
MM
9028 /* If the next token isn't a `,' or a `>', then this argument wasn't
9029 really finished. */
d17811fd 9030 if (!cp_parser_next_token_ends_template_argument_p (parser))
a723baf1
MM
9031 cp_parser_error (parser, "expected template-argument");
9032 if (!cp_parser_error_occurred (parser))
9033 {
f746161e
MM
9034 /* Figure out what is being referred to. If the id-expression
9035 was for a class template specialization, then we will have a
9036 TYPE_DECL at this point. There is no need to do name lookup
9037 at this point in that case. */
9038 if (TREE_CODE (argument) != TYPE_DECL)
9039 argument = cp_parser_lookup_name (parser, argument,
fc6a28d7 9040 none_type,
f746161e
MM
9041 /*is_template=*/template_p,
9042 /*is_namespace=*/false,
8f78f01f
MM
9043 /*check_dependency=*/true,
9044 /*ambiguous_p=*/NULL);
5b4acce1
KL
9045 if (TREE_CODE (argument) != TEMPLATE_DECL
9046 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
a723baf1
MM
9047 cp_parser_error (parser, "expected template-name");
9048 }
9049 if (cp_parser_parse_definitely (parser))
9050 return argument;
d17811fd
MM
9051 /* It must be a non-type argument. There permitted cases are given
9052 in [temp.arg.nontype]:
9053
9054 -- an integral constant-expression of integral or enumeration
0cbd7506 9055 type; or
d17811fd
MM
9056
9057 -- the name of a non-type template-parameter; or
9058
9059 -- the name of an object or function with external linkage...
9060
9061 -- the address of an object or function with external linkage...
9062
04c06002 9063 -- a pointer to member... */
d17811fd
MM
9064 /* Look for a non-type template parameter. */
9065 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9066 {
9067 cp_parser_parse_tentatively (parser);
9068 argument = cp_parser_primary_expression (parser,
02ed62dd 9069 /*adress_p=*/false,
93678513 9070 /*cast_p=*/false,
02ed62dd
MM
9071 /*template_arg_p=*/true,
9072 &idk);
d17811fd
MM
9073 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9074 || !cp_parser_next_token_ends_template_argument_p (parser))
9075 cp_parser_simulate_error (parser);
9076 if (cp_parser_parse_definitely (parser))
9077 return argument;
9078 }
db24eb1f 9079
d17811fd
MM
9080 /* If the next token is "&", the argument must be the address of an
9081 object or function with external linkage. */
9082 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9083 if (address_p)
9084 cp_lexer_consume_token (parser->lexer);
9085 /* See if we might have an id-expression. */
9086 token = cp_lexer_peek_token (parser->lexer);
9087 if (token->type == CPP_NAME
9088 || token->keyword == RID_OPERATOR
9089 || token->type == CPP_SCOPE
9090 || token->type == CPP_TEMPLATE_ID
9091 || token->type == CPP_NESTED_NAME_SPECIFIER)
9092 {
9093 cp_parser_parse_tentatively (parser);
9094 argument = cp_parser_primary_expression (parser,
02ed62dd 9095 address_p,
93678513 9096 /*cast_p=*/false,
02ed62dd
MM
9097 /*template_arg_p=*/true,
9098 &idk);
d17811fd
MM
9099 if (cp_parser_error_occurred (parser)
9100 || !cp_parser_next_token_ends_template_argument_p (parser))
9101 cp_parser_abort_tentative_parse (parser);
9102 else
9103 {
db24eb1f
NS
9104 if (TREE_CODE (argument) == INDIRECT_REF)
9105 {
9106 gcc_assert (REFERENCE_REF_P (argument));
9107 argument = TREE_OPERAND (argument, 0);
9108 }
c8094d83 9109
02ed62dd 9110 if (TREE_CODE (argument) == BASELINK)
2c164de6
MM
9111 /* We don't need the information about what class was used
9112 to name the overloaded functions. */
9113 argument = BASELINK_FUNCTIONS (argument);
9114
d17811fd
MM
9115 if (TREE_CODE (argument) == VAR_DECL)
9116 {
9117 /* A variable without external linkage might still be a
9118 valid constant-expression, so no error is issued here
9119 if the external-linkage check fails. */
9120 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9121 cp_parser_simulate_error (parser);
9122 }
9123 else if (is_overloaded_fn (argument))
9124 /* All overloaded functions are allowed; if the external
9125 linkage test does not pass, an error will be issued
9126 later. */
9127 ;
9128 else if (address_p
21526606 9129 && (TREE_CODE (argument) == OFFSET_REF
d17811fd
MM
9130 || TREE_CODE (argument) == SCOPE_REF))
9131 /* A pointer-to-member. */
9132 ;
db24eb1f
NS
9133 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9134 ;
d17811fd
MM
9135 else
9136 cp_parser_simulate_error (parser);
9137
9138 if (cp_parser_parse_definitely (parser))
9139 {
9140 if (address_p)
9141 argument = build_x_unary_op (ADDR_EXPR, argument);
9142 return argument;
9143 }
9144 }
9145 }
9146 /* If the argument started with "&", there are no other valid
9147 alternatives at this point. */
9148 if (address_p)
9149 {
9150 cp_parser_error (parser, "invalid non-type template argument");
9151 return error_mark_node;
9152 }
db24eb1f 9153
4d5297fa 9154 /* If the argument wasn't successfully parsed as a type-id followed
21526606 9155 by '>>', the argument can only be a constant expression now.
4d5297fa
GB
9156 Otherwise, we try parsing the constant-expression tentatively,
9157 because the argument could really be a type-id. */
9158 if (maybe_type_id)
9159 cp_parser_parse_tentatively (parser);
21526606 9160 argument = cp_parser_constant_expression (parser,
d17811fd
MM
9161 /*allow_non_constant_p=*/false,
9162 /*non_constant_p=*/NULL);
9baa27a9 9163 argument = fold_non_dependent_expr (argument);
4d5297fa
GB
9164 if (!maybe_type_id)
9165 return argument;
9166 if (!cp_parser_next_token_ends_template_argument_p (parser))
9167 cp_parser_error (parser, "expected template-argument");
9168 if (cp_parser_parse_definitely (parser))
9169 return argument;
9170 /* We did our best to parse the argument as a non type-id, but that
9171 was the only alternative that matched (albeit with a '>' after
21526606 9172 it). We can assume it's just a typo from the user, and a
4d5297fa
GB
9173 diagnostic will then be issued. */
9174 return cp_parser_type_id (parser);
a723baf1
MM
9175}
9176
9177/* Parse an explicit-instantiation.
9178
9179 explicit-instantiation:
21526606 9180 template declaration
a723baf1
MM
9181
9182 Although the standard says `declaration', what it really means is:
9183
9184 explicit-instantiation:
21526606 9185 template decl-specifier-seq [opt] declarator [opt] ;
a723baf1
MM
9186
9187 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9188 supposed to be allowed. A defect report has been filed about this
21526606 9189 issue.
a723baf1
MM
9190
9191 GNU Extension:
21526606 9192
a723baf1 9193 explicit-instantiation:
21526606 9194 storage-class-specifier template
a723baf1 9195 decl-specifier-seq [opt] declarator [opt] ;
21526606 9196 function-specifier template
a723baf1
MM
9197 decl-specifier-seq [opt] declarator [opt] ; */
9198
9199static void
94edc4ab 9200cp_parser_explicit_instantiation (cp_parser* parser)
a723baf1 9201{
560ad596 9202 int declares_class_or_enum;
62d1db17 9203 cp_decl_specifier_seq decl_specifiers;
a723baf1
MM
9204 tree extension_specifier = NULL_TREE;
9205
9206 /* Look for an (optional) storage-class-specifier or
9207 function-specifier. */
9208 if (cp_parser_allow_gnu_extensions_p (parser))
9209 {
21526606 9210 extension_specifier
a723baf1
MM
9211 = cp_parser_storage_class_specifier_opt (parser);
9212 if (!extension_specifier)
98ca843c 9213 extension_specifier
62d1db17
MM
9214 = cp_parser_function_specifier_opt (parser,
9215 /*decl_specs=*/NULL);
a723baf1
MM
9216 }
9217
9218 /* Look for the `template' keyword. */
9219 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9220 /* Let the front end know that we are processing an explicit
9221 instantiation. */
9222 begin_explicit_instantiation ();
9223 /* [temp.explicit] says that we are supposed to ignore access
9224 control while processing explicit instantiation directives. */
78757caa 9225 push_deferring_access_checks (dk_no_check);
a723baf1 9226 /* Parse a decl-specifier-seq. */
62d1db17
MM
9227 cp_parser_decl_specifier_seq (parser,
9228 CP_PARSER_FLAGS_OPTIONAL,
9229 &decl_specifiers,
9230 &declares_class_or_enum);
a723baf1
MM
9231 /* If there was exactly one decl-specifier, and it declared a class,
9232 and there's no declarator, then we have an explicit type
9233 instantiation. */
9234 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9235 {
9236 tree type;
9237
62d1db17 9238 type = check_tag_decl (&decl_specifiers);
b7fc8b57
KL
9239 /* Turn access control back on for names used during
9240 template instantiation. */
9241 pop_deferring_access_checks ();
a723baf1
MM
9242 if (type)
9243 do_type_instantiation (type, extension_specifier, /*complain=*/1);
9244 }
9245 else
9246 {
058b15c1 9247 cp_declarator *declarator;
a723baf1
MM
9248 tree decl;
9249
9250 /* Parse the declarator. */
21526606 9251 declarator
62b8a44e 9252 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 9253 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
9254 /*parenthesized_p=*/NULL,
9255 /*member_p=*/false);
fc6a28d7
MM
9256 if (declares_class_or_enum & 2)
9257 cp_parser_check_for_definition_in_return_type (declarator,
9258 decl_specifiers.type);
058b15c1 9259 if (declarator != cp_error_declarator)
216bb6e1 9260 {
62d1db17 9261 decl = grokdeclarator (declarator, &decl_specifiers,
216bb6e1
MM
9262 NORMAL, 0, NULL);
9263 /* Turn access control back on for names used during
9264 template instantiation. */
9265 pop_deferring_access_checks ();
9266 /* Do the explicit instantiation. */
9267 do_decl_instantiation (decl, extension_specifier);
9268 }
9269 else
9270 {
9271 pop_deferring_access_checks ();
9272 /* Skip the body of the explicit instantiation. */
9273 cp_parser_skip_to_end_of_statement (parser);
9274 }
a723baf1
MM
9275 }
9276 /* We're done with the instantiation. */
9277 end_explicit_instantiation ();
a723baf1 9278
e0860732 9279 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1
MM
9280}
9281
9282/* Parse an explicit-specialization.
9283
9284 explicit-specialization:
21526606 9285 template < > declaration
a723baf1
MM
9286
9287 Although the standard says `declaration', what it really means is:
9288
9289 explicit-specialization:
9290 template <> decl-specifier [opt] init-declarator [opt] ;
21526606 9291 template <> function-definition
a723baf1
MM
9292 template <> explicit-specialization
9293 template <> template-declaration */
9294
9295static void
94edc4ab 9296cp_parser_explicit_specialization (cp_parser* parser)
a723baf1
MM
9297{
9298 /* Look for the `template' keyword. */
9299 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9300 /* Look for the `<'. */
9301 cp_parser_require (parser, CPP_LESS, "`<'");
9302 /* Look for the `>'. */
9303 cp_parser_require (parser, CPP_GREATER, "`>'");
9304 /* We have processed another parameter list. */
9305 ++parser->num_template_parameter_lists;
9306 /* Let the front end know that we are beginning a specialization. */
9307 begin_specialization ();
9308
9309 /* If the next keyword is `template', we need to figure out whether
9310 or not we're looking a template-declaration. */
9311 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9312 {
9313 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9314 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9315 cp_parser_template_declaration_after_export (parser,
9316 /*member_p=*/false);
9317 else
9318 cp_parser_explicit_specialization (parser);
9319 }
9320 else
9321 /* Parse the dependent declaration. */
21526606 9322 cp_parser_single_declaration (parser,
a723baf1
MM
9323 /*member_p=*/false,
9324 /*friend_p=*/NULL);
9325
9326 /* We're done with the specialization. */
9327 end_specialization ();
9328 /* We're done with this parameter list. */
9329 --parser->num_template_parameter_lists;
9330}
9331
9332/* Parse a type-specifier.
9333
9334 type-specifier:
9335 simple-type-specifier
9336 class-specifier
9337 enum-specifier
9338 elaborated-type-specifier
9339 cv-qualifier
9340
9341 GNU Extension:
9342
9343 type-specifier:
9344 __complex__
9345
62d1db17
MM
9346 Returns a representation of the type-specifier. For a
9347 class-specifier, enum-specifier, or elaborated-type-specifier, a
9348 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
a723baf1 9349
eb1aef53
KL
9350 The parser flags FLAGS is used to control type-specifier parsing.
9351
9352 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9353 in a decl-specifier-seq.
a723baf1
MM
9354
9355 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9356 class-specifier, enum-specifier, or elaborated-type-specifier, then
83a00410 9357 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
560ad596
MM
9358 if a type is declared; 2 if it is defined. Otherwise, it is set to
9359 zero.
a723baf1
MM
9360
9361 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9362 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9363 is set to FALSE. */
9364
9365static tree
21526606
EC
9366cp_parser_type_specifier (cp_parser* parser,
9367 cp_parser_flags flags,
62d1db17 9368 cp_decl_specifier_seq *decl_specs,
94edc4ab 9369 bool is_declaration,
560ad596 9370 int* declares_class_or_enum,
94edc4ab 9371 bool* is_cv_qualifier)
a723baf1
MM
9372{
9373 tree type_spec = NULL_TREE;
9374 cp_token *token;
9375 enum rid keyword;
62d1db17 9376 cp_decl_spec ds = ds_last;
a723baf1
MM
9377
9378 /* Assume this type-specifier does not declare a new type. */
9379 if (declares_class_or_enum)
543ca912 9380 *declares_class_or_enum = 0;
a723baf1
MM
9381 /* And that it does not specify a cv-qualifier. */
9382 if (is_cv_qualifier)
9383 *is_cv_qualifier = false;
9384 /* Peek at the next token. */
9385 token = cp_lexer_peek_token (parser->lexer);
9386
9387 /* If we're looking at a keyword, we can use that to guide the
9388 production we choose. */
9389 keyword = token->keyword;
9390 switch (keyword)
9391 {
ff4eb0b5
ZW
9392 case RID_ENUM:
9393 /* 'enum' [identifier] '{' introduces an enum-specifier;
9394 'enum' <anything else> introduces an elaborated-type-specifier. */
9395 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9396 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9397 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
0cbd7506 9398 == CPP_OPEN_BRACE))
ff4eb0b5 9399 {
a5e51518
KL
9400 if (parser->num_template_parameter_lists)
9401 {
9402 error ("template declaration of %qs", "enum");
9403 cp_parser_skip_to_end_of_block_or_statement (parser);
9404 type_spec = error_mark_node;
9405 }
9406 else
9407 type_spec = cp_parser_enum_specifier (parser);
9408
ff4eb0b5
ZW
9409 if (declares_class_or_enum)
9410 *declares_class_or_enum = 2;
9411 if (decl_specs)
9412 cp_parser_set_decl_spec_type (decl_specs,
9413 type_spec,
9414 /*user_defined_p=*/true);
9415 return type_spec;
9416 }
9417 else
9418 goto elaborated_type_specifier;
9419
a723baf1
MM
9420 /* Any of these indicate either a class-specifier, or an
9421 elaborated-type-specifier. */
9422 case RID_CLASS:
9423 case RID_STRUCT:
9424 case RID_UNION:
a723baf1 9425 /* Parse tentatively so that we can back up if we don't find a
ff4eb0b5 9426 class-specifier. */
a723baf1 9427 cp_parser_parse_tentatively (parser);
ff4eb0b5
ZW
9428 /* Look for the class-specifier. */
9429 type_spec = cp_parser_class_specifier (parser);
a723baf1
MM
9430 /* If that worked, we're done. */
9431 if (cp_parser_parse_definitely (parser))
9432 {
9433 if (declares_class_or_enum)
560ad596 9434 *declares_class_or_enum = 2;
62d1db17
MM
9435 if (decl_specs)
9436 cp_parser_set_decl_spec_type (decl_specs,
9437 type_spec,
9438 /*user_defined_p=*/true);
a723baf1
MM
9439 return type_spec;
9440 }
9441
9442 /* Fall through. */
ff4eb0b5
ZW
9443 elaborated_type_specifier:
9444 /* We're declaring (not defining) a class or enum. */
9445 if (declares_class_or_enum)
9446 *declares_class_or_enum = 1;
a723baf1 9447
ff4eb0b5 9448 /* Fall through. */
a723baf1
MM
9449 case RID_TYPENAME:
9450 /* Look for an elaborated-type-specifier. */
98ca843c
EC
9451 type_spec
9452 = (cp_parser_elaborated_type_specifier
62d1db17
MM
9453 (parser,
9454 decl_specs && decl_specs->specs[(int) ds_friend],
9455 is_declaration));
62d1db17
MM
9456 if (decl_specs)
9457 cp_parser_set_decl_spec_type (decl_specs,
9458 type_spec,
9459 /*user_defined_p=*/true);
a723baf1
MM
9460 return type_spec;
9461
9462 case RID_CONST:
62d1db17
MM
9463 ds = ds_const;
9464 if (is_cv_qualifier)
9465 *is_cv_qualifier = true;
9466 break;
98ca843c 9467
a723baf1 9468 case RID_VOLATILE:
62d1db17 9469 ds = ds_volatile;
a723baf1
MM
9470 if (is_cv_qualifier)
9471 *is_cv_qualifier = true;
62d1db17 9472 break;
a723baf1 9473
62d1db17
MM
9474 case RID_RESTRICT:
9475 ds = ds_restrict;
9476 if (is_cv_qualifier)
9477 *is_cv_qualifier = true;
9478 break;
a723baf1
MM
9479
9480 case RID_COMPLEX:
9481 /* The `__complex__' keyword is a GNU extension. */
62d1db17
MM
9482 ds = ds_complex;
9483 break;
a723baf1
MM
9484
9485 default:
9486 break;
9487 }
9488
62d1db17
MM
9489 /* Handle simple keywords. */
9490 if (ds != ds_last)
9491 {
9492 if (decl_specs)
9493 {
9494 ++decl_specs->specs[(int)ds];
9495 decl_specs->any_specifiers_p = true;
9496 }
9497 return cp_lexer_consume_token (parser->lexer)->value;
9498 }
9499
a723baf1
MM
9500 /* If we do not already have a type-specifier, assume we are looking
9501 at a simple-type-specifier. */
98ca843c 9502 type_spec = cp_parser_simple_type_specifier (parser,
62d1db17
MM
9503 decl_specs,
9504 flags);
a723baf1
MM
9505
9506 /* If we didn't find a type-specifier, and a type-specifier was not
9507 optional in this context, issue an error message. */
9508 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9509 {
9510 cp_parser_error (parser, "expected type specifier");
9511 return error_mark_node;
9512 }
9513
9514 return type_spec;
9515}
9516
9517/* Parse a simple-type-specifier.
9518
9519 simple-type-specifier:
9520 :: [opt] nested-name-specifier [opt] type-name
9521 :: [opt] nested-name-specifier template template-id
9522 char
9523 wchar_t
9524 bool
9525 short
9526 int
9527 long
9528 signed
9529 unsigned
9530 float
9531 double
21526606 9532 void
a723baf1
MM
9533
9534 GNU Extension:
9535
9536 simple-type-specifier:
9537 __typeof__ unary-expression
9538 __typeof__ ( type-id )
9539
62d1db17
MM
9540 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9541 appropriately updated. */
a723baf1
MM
9542
9543static tree
98ca843c 9544cp_parser_simple_type_specifier (cp_parser* parser,
62d1db17
MM
9545 cp_decl_specifier_seq *decl_specs,
9546 cp_parser_flags flags)
a723baf1
MM
9547{
9548 tree type = NULL_TREE;
9549 cp_token *token;
9550
9551 /* Peek at the next token. */
9552 token = cp_lexer_peek_token (parser->lexer);
9553
9554 /* If we're looking at a keyword, things are easy. */
9555 switch (token->keyword)
9556 {
9557 case RID_CHAR:
62d1db17
MM
9558 if (decl_specs)
9559 decl_specs->explicit_char_p = true;
4b0d3cbe
MM
9560 type = char_type_node;
9561 break;
a723baf1 9562 case RID_WCHAR:
4b0d3cbe
MM
9563 type = wchar_type_node;
9564 break;
a723baf1 9565 case RID_BOOL:
4b0d3cbe
MM
9566 type = boolean_type_node;
9567 break;
a723baf1 9568 case RID_SHORT:
62d1db17
MM
9569 if (decl_specs)
9570 ++decl_specs->specs[(int) ds_short];
4b0d3cbe
MM
9571 type = short_integer_type_node;
9572 break;
a723baf1 9573 case RID_INT:
62d1db17
MM
9574 if (decl_specs)
9575 decl_specs->explicit_int_p = true;
4b0d3cbe
MM
9576 type = integer_type_node;
9577 break;
a723baf1 9578 case RID_LONG:
62d1db17
MM
9579 if (decl_specs)
9580 ++decl_specs->specs[(int) ds_long];
4b0d3cbe
MM
9581 type = long_integer_type_node;
9582 break;
a723baf1 9583 case RID_SIGNED:
62d1db17
MM
9584 if (decl_specs)
9585 ++decl_specs->specs[(int) ds_signed];
4b0d3cbe
MM
9586 type = integer_type_node;
9587 break;
a723baf1 9588 case RID_UNSIGNED:
62d1db17
MM
9589 if (decl_specs)
9590 ++decl_specs->specs[(int) ds_unsigned];
4b0d3cbe
MM
9591 type = unsigned_type_node;
9592 break;
a723baf1 9593 case RID_FLOAT:
4b0d3cbe
MM
9594 type = float_type_node;
9595 break;
a723baf1 9596 case RID_DOUBLE:
4b0d3cbe
MM
9597 type = double_type_node;
9598 break;
a723baf1 9599 case RID_VOID:
4b0d3cbe
MM
9600 type = void_type_node;
9601 break;
a723baf1
MM
9602
9603 case RID_TYPEOF:
62d1db17
MM
9604 /* Consume the `typeof' token. */
9605 cp_lexer_consume_token (parser->lexer);
9606 /* Parse the operand to `typeof'. */
9607 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9608 /* If it is not already a TYPE, take its type. */
9609 if (!TYPE_P (type))
9610 type = finish_typeof (type);
9611
9612 if (decl_specs)
9613 cp_parser_set_decl_spec_type (decl_specs, type,
9614 /*user_defined_p=*/true);
98ca843c 9615
62d1db17 9616 return type;
a723baf1
MM
9617
9618 default:
9619 break;
9620 }
9621
4b0d3cbe
MM
9622 /* If the type-specifier was for a built-in type, we're done. */
9623 if (type)
9624 {
9625 tree id;
9626
62d1db17
MM
9627 /* Record the type. */
9628 if (decl_specs
9629 && (token->keyword != RID_SIGNED
9630 && token->keyword != RID_UNSIGNED
9631 && token->keyword != RID_SHORT
9632 && token->keyword != RID_LONG))
98ca843c 9633 cp_parser_set_decl_spec_type (decl_specs,
62d1db17
MM
9634 type,
9635 /*user_defined=*/false);
9636 if (decl_specs)
9637 decl_specs->any_specifiers_p = true;
9638
4b0d3cbe
MM
9639 /* Consume the token. */
9640 id = cp_lexer_consume_token (parser->lexer)->value;
0d956474
GB
9641
9642 /* There is no valid C++ program where a non-template type is
9643 followed by a "<". That usually indicates that the user thought
9644 that the type was a template. */
9645 cp_parser_check_for_invalid_template_id (parser, type);
9646
62d1db17 9647 return TYPE_NAME (type);
4b0d3cbe
MM
9648 }
9649
a723baf1 9650 /* The type-specifier must be a user-defined type. */
21526606 9651 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
a723baf1 9652 {
0c1a1ecd 9653 bool qualified_p;
f68e4dc8 9654 bool global_p;
0c1a1ecd 9655
a723baf1
MM
9656 /* Don't gobble tokens or issue error messages if this is an
9657 optional type-specifier. */
9658 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9659 cp_parser_parse_tentatively (parser);
9660
9661 /* Look for the optional `::' operator. */
f68e4dc8 9662 global_p
da740453
MM
9663 = (cp_parser_global_scope_opt (parser,
9664 /*current_scope_valid_p=*/false)
9665 != NULL_TREE);
a723baf1 9666 /* Look for the nested-name specifier. */
0c1a1ecd
MM
9667 qualified_p
9668 = (cp_parser_nested_name_specifier_opt (parser,
9669 /*typename_keyword_p=*/false,
9670 /*check_dependency_p=*/true,
9671 /*type_p=*/false,
6661a85f
EB
9672 /*is_declaration=*/false)
9673 != NULL_TREE);
a723baf1
MM
9674 /* If we have seen a nested-name-specifier, and the next token
9675 is `template', then we are using the template-id production. */
21526606 9676 if (parser->scope
a723baf1
MM
9677 && cp_parser_optional_template_keyword (parser))
9678 {
9679 /* Look for the template-id. */
21526606 9680 type = cp_parser_template_id (parser,
a723baf1 9681 /*template_keyword_p=*/true,
a668c6ad
MM
9682 /*check_dependency_p=*/true,
9683 /*is_declaration=*/false);
a723baf1
MM
9684 /* If the template-id did not name a type, we are out of
9685 luck. */
9686 if (TREE_CODE (type) != TYPE_DECL)
9687 {
9688 cp_parser_error (parser, "expected template-id for type");
9689 type = NULL_TREE;
9690 }
9691 }
9692 /* Otherwise, look for a type-name. */
9693 else
4bb8ca28 9694 type = cp_parser_type_name (parser);
0c1a1ecd 9695 /* Keep track of all name-lookups performed in class scopes. */
98ca843c 9696 if (type
f68e4dc8 9697 && !global_p
0c1a1ecd 9698 && !qualified_p
98ca843c 9699 && TREE_CODE (type) == TYPE_DECL
0c1a1ecd
MM
9700 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9701 maybe_note_name_used_in_class (DECL_NAME (type), type);
a723baf1 9702 /* If it didn't work out, we don't have a TYPE. */
21526606 9703 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
a723baf1
MM
9704 && !cp_parser_parse_definitely (parser))
9705 type = NULL_TREE;
62d1db17
MM
9706 if (type && decl_specs)
9707 cp_parser_set_decl_spec_type (decl_specs, type,
9708 /*user_defined=*/true);
a723baf1
MM
9709 }
9710
9711 /* If we didn't get a type-name, issue an error message. */
9712 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9713 {
9714 cp_parser_error (parser, "expected type-name");
9715 return error_mark_node;
9716 }
9717
a668c6ad
MM
9718 /* There is no valid C++ program where a non-template type is
9719 followed by a "<". That usually indicates that the user thought
9720 that the type was a template. */
4bb8ca28 9721 if (type && type != error_mark_node)
e58a9aa1
ZL
9722 {
9723 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9724 If it is, then the '<'...'>' enclose protocol names rather than
9725 template arguments, and so everything is fine. */
9726 if (c_dialect_objc ()
9727 && (objc_is_id (type) || objc_is_class_name (type)))
9728 {
9729 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9730 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9731
9732 /* Clobber the "unqualified" type previously entered into
128a79fb 9733 DECL_SPECS with the new, improved protocol-qualified version. */
e58a9aa1
ZL
9734 if (decl_specs)
9735 decl_specs->type = qual_type;
9736
9737 return qual_type;
9738 }
9739
9740 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
c8094d83 9741 }
ec75414f 9742
a723baf1
MM
9743 return type;
9744}
9745
9746/* Parse a type-name.
9747
9748 type-name:
9749 class-name
9750 enum-name
21526606 9751 typedef-name
a723baf1
MM
9752
9753 enum-name:
9754 identifier
9755
9756 typedef-name:
21526606 9757 identifier
a723baf1 9758
78dcd41a 9759 Returns a TYPE_DECL for the type. */
a723baf1
MM
9760
9761static tree
94edc4ab 9762cp_parser_type_name (cp_parser* parser)
a723baf1
MM
9763{
9764 tree type_decl;
9765 tree identifier;
9766
9767 /* We can't know yet whether it is a class-name or not. */
9768 cp_parser_parse_tentatively (parser);
9769 /* Try a class-name. */
21526606 9770 type_decl = cp_parser_class_name (parser,
a723baf1
MM
9771 /*typename_keyword_p=*/false,
9772 /*template_keyword_p=*/false,
fc6a28d7 9773 none_type,
a723baf1 9774 /*check_dependency_p=*/true,
a668c6ad
MM
9775 /*class_head_p=*/false,
9776 /*is_declaration=*/false);
a723baf1
MM
9777 /* If it's not a class-name, keep looking. */
9778 if (!cp_parser_parse_definitely (parser))
9779 {
9780 /* It must be a typedef-name or an enum-name. */
9781 identifier = cp_parser_identifier (parser);
9782 if (identifier == error_mark_node)
9783 return error_mark_node;
21526606 9784
a723baf1
MM
9785 /* Look up the type-name. */
9786 type_decl = cp_parser_lookup_name_simple (parser, identifier);
e58a9aa1
ZL
9787
9788 if (TREE_CODE (type_decl) != TYPE_DECL
9789 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9790 {
9791 /* See if this is an Objective-C type. */
9792 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9793 tree type = objc_get_protocol_qualified_type (identifier, protos);
c8094d83 9794 if (type)
e58a9aa1
ZL
9795 type_decl = TYPE_NAME (type);
9796 }
9797
a723baf1
MM
9798 /* Issue an error if we did not find a type-name. */
9799 if (TREE_CODE (type_decl) != TYPE_DECL)
9800 {
4bb8ca28 9801 if (!cp_parser_simulate_error (parser))
21526606 9802 cp_parser_name_lookup_error (parser, identifier, type_decl,
4bb8ca28 9803 "is not a type");
a723baf1
MM
9804 type_decl = error_mark_node;
9805 }
9806 /* Remember that the name was used in the definition of the
9807 current class so that we can check later to see if the
9808 meaning would have been different after the class was
9809 entirely defined. */
9810 else if (type_decl != error_mark_node
9811 && !parser->scope)
9812 maybe_note_name_used_in_class (identifier, type_decl);
9813 }
21526606 9814
a723baf1
MM
9815 return type_decl;
9816}
9817
9818
9819/* Parse an elaborated-type-specifier. Note that the grammar given
9820 here incorporates the resolution to DR68.
9821
9822 elaborated-type-specifier:
9823 class-key :: [opt] nested-name-specifier [opt] identifier
9824 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9825 enum :: [opt] nested-name-specifier [opt] identifier
9826 typename :: [opt] nested-name-specifier identifier
21526606
EC
9827 typename :: [opt] nested-name-specifier template [opt]
9828 template-id
a723baf1 9829
360d1b99
MM
9830 GNU extension:
9831
9832 elaborated-type-specifier:
9833 class-key attributes :: [opt] nested-name-specifier [opt] identifier
21526606 9834 class-key attributes :: [opt] nested-name-specifier [opt]
0cbd7506 9835 template [opt] template-id
360d1b99
MM
9836 enum attributes :: [opt] nested-name-specifier [opt] identifier
9837
a723baf1
MM
9838 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9839 declared `friend'. If IS_DECLARATION is TRUE, then this
9840 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9841 something is being declared.
9842
9843 Returns the TYPE specified. */
9844
9845static tree
21526606 9846cp_parser_elaborated_type_specifier (cp_parser* parser,
0cbd7506
MS
9847 bool is_friend,
9848 bool is_declaration)
a723baf1
MM
9849{
9850 enum tag_types tag_type;
9851 tree identifier;
9852 tree type = NULL_TREE;
360d1b99 9853 tree attributes = NULL_TREE;
a723baf1
MM
9854
9855 /* See if we're looking at the `enum' keyword. */
9856 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9857 {
9858 /* Consume the `enum' token. */
9859 cp_lexer_consume_token (parser->lexer);
9860 /* Remember that it's an enumeration type. */
9861 tag_type = enum_type;
360d1b99
MM
9862 /* Parse the attributes. */
9863 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
9864 }
9865 /* Or, it might be `typename'. */
9866 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9867 RID_TYPENAME))
9868 {
9869 /* Consume the `typename' token. */
9870 cp_lexer_consume_token (parser->lexer);
9871 /* Remember that it's a `typename' type. */
9872 tag_type = typename_type;
9873 /* The `typename' keyword is only allowed in templates. */
9874 if (!processing_template_decl)
2a13a625 9875 pedwarn ("using %<typename%> outside of template");
a723baf1
MM
9876 }
9877 /* Otherwise it must be a class-key. */
9878 else
9879 {
9880 tag_type = cp_parser_class_key (parser);
9881 if (tag_type == none_type)
9882 return error_mark_node;
360d1b99
MM
9883 /* Parse the attributes. */
9884 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
9885 }
9886
9887 /* Look for the `::' operator. */
21526606 9888 cp_parser_global_scope_opt (parser,
a723baf1
MM
9889 /*current_scope_valid_p=*/false);
9890 /* Look for the nested-name-specifier. */
9891 if (tag_type == typename_type)
8fa1ad0e 9892 {
8fe4d24b 9893 if (!cp_parser_nested_name_specifier (parser,
8fa1ad0e
MM
9894 /*typename_keyword_p=*/true,
9895 /*check_dependency_p=*/true,
a668c6ad 9896 /*type_p=*/true,
8fe4d24b 9897 is_declaration))
8fa1ad0e
MM
9898 return error_mark_node;
9899 }
a723baf1
MM
9900 else
9901 /* Even though `typename' is not present, the proposed resolution
9902 to Core Issue 180 says that in `class A<T>::B', `B' should be
9903 considered a type-name, even if `A<T>' is dependent. */
9904 cp_parser_nested_name_specifier_opt (parser,
9905 /*typename_keyword_p=*/true,
9906 /*check_dependency_p=*/true,
a668c6ad
MM
9907 /*type_p=*/true,
9908 is_declaration);
a723baf1
MM
9909 /* For everything but enumeration types, consider a template-id. */
9910 if (tag_type != enum_type)
9911 {
9912 bool template_p = false;
9913 tree decl;
9914
9915 /* Allow the `template' keyword. */
9916 template_p = cp_parser_optional_template_keyword (parser);
9917 /* If we didn't see `template', we don't know if there's a
0cbd7506 9918 template-id or not. */
a723baf1
MM
9919 if (!template_p)
9920 cp_parser_parse_tentatively (parser);
9921 /* Parse the template-id. */
9922 decl = cp_parser_template_id (parser, template_p,
a668c6ad
MM
9923 /*check_dependency_p=*/true,
9924 is_declaration);
a723baf1 9925 /* If we didn't find a template-id, look for an ordinary
0cbd7506 9926 identifier. */
a723baf1
MM
9927 if (!template_p && !cp_parser_parse_definitely (parser))
9928 ;
9929 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9930 in effect, then we must assume that, upon instantiation, the
9931 template will correspond to a class. */
9932 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9933 && tag_type == typename_type)
9934 type = make_typename_type (parser->scope, decl,
fc6a28d7 9935 typename_type,
a723baf1 9936 /*complain=*/1);
21526606 9937 else
a723baf1
MM
9938 type = TREE_TYPE (decl);
9939 }
9940
9941 /* For an enumeration type, consider only a plain identifier. */
9942 if (!type)
9943 {
9944 identifier = cp_parser_identifier (parser);
9945
9946 if (identifier == error_mark_node)
eb5abb39
NS
9947 {
9948 parser->scope = NULL_TREE;
9949 return error_mark_node;
9950 }
a723baf1
MM
9951
9952 /* For a `typename', we needn't call xref_tag. */
c8094d83 9953 if (tag_type == typename_type
0c88d886 9954 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
21526606 9955 return cp_parser_make_typename_type (parser, parser->scope,
2097b5f2 9956 identifier);
a723baf1
MM
9957 /* Look up a qualified name in the usual way. */
9958 if (parser->scope)
9959 {
9960 tree decl;
9961
21526606 9962 decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 9963 tag_type,
b0bc6e8e 9964 /*is_template=*/false,
eea9800f 9965 /*is_namespace=*/false,
8f78f01f
MM
9966 /*check_dependency=*/true,
9967 /*ambiguous_p=*/NULL);
710b73e6
KL
9968
9969 /* If we are parsing friend declaration, DECL may be a
9970 TEMPLATE_DECL tree node here. However, we need to check
9971 whether this TEMPLATE_DECL results in valid code. Consider
9972 the following example:
9973
9974 namespace N {
9975 template <class T> class C {};
9976 }
9977 class X {
9978 template <class T> friend class N::C; // #1, valid code
9979 };
9980 template <class T> class Y {
9981 friend class N::C; // #2, invalid code
9982 };
9983
9984 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9985 name lookup of `N::C'. We see that friend declaration must
9986 be template for the code to be valid. Note that
9987 processing_template_decl does not work here since it is
9988 always 1 for the above two cases. */
9989
21526606 9990 decl = (cp_parser_maybe_treat_template_as_class
710b73e6
KL
9991 (decl, /*tag_name_p=*/is_friend
9992 && parser->num_template_parameter_lists));
a723baf1
MM
9993
9994 if (TREE_CODE (decl) != TYPE_DECL)
9995 {
c8094d83 9996 cp_parser_diagnose_invalid_type_name (parser,
0c88d886
MM
9997 parser->scope,
9998 identifier);
a723baf1
MM
9999 return error_mark_node;
10000 }
560ad596
MM
10001
10002 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
21526606 10003 check_elaborated_type_specifier
4b0d3cbe 10004 (tag_type, decl,
560ad596
MM
10005 (parser->num_template_parameter_lists
10006 || DECL_SELF_REFERENCE_P (decl)));
a723baf1
MM
10007
10008 type = TREE_TYPE (decl);
10009 }
21526606 10010 else
a723baf1
MM
10011 {
10012 /* An elaborated-type-specifier sometimes introduces a new type and
10013 sometimes names an existing type. Normally, the rule is that it
10014 introduces a new type only if there is not an existing type of
10015 the same name already in scope. For example, given:
10016
10017 struct S {};
10018 void f() { struct S s; }
10019
10020 the `struct S' in the body of `f' is the same `struct S' as in
10021 the global scope; the existing definition is used. However, if
21526606 10022 there were no global declaration, this would introduce a new
a723baf1
MM
10023 local class named `S'.
10024
10025 An exception to this rule applies to the following code:
10026
10027 namespace N { struct S; }
10028
10029 Here, the elaborated-type-specifier names a new type
10030 unconditionally; even if there is already an `S' in the
10031 containing scope this declaration names a new type.
10032 This exception only applies if the elaborated-type-specifier
10033 forms the complete declaration:
10034
21526606 10035 [class.name]
a723baf1
MM
10036
10037 A declaration consisting solely of `class-key identifier ;' is
10038 either a redeclaration of the name in the current scope or a
10039 forward declaration of the identifier as a class name. It
10040 introduces the name into the current scope.
10041
10042 We are in this situation precisely when the next token is a `;'.
10043
10044 An exception to the exception is that a `friend' declaration does
10045 *not* name a new type; i.e., given:
10046
10047 struct S { friend struct T; };
10048
21526606 10049 `T' is not a new type in the scope of `S'.
a723baf1
MM
10050
10051 Also, `new struct S' or `sizeof (struct S)' never results in the
10052 definition of a new type; a new type can only be declared in a
9bcb9aae 10053 declaration context. */
a723baf1 10054
29ef83de 10055 tag_scope ts;
ca85f659
MM
10056 bool template_p;
10057
29ef83de
KL
10058 if (is_friend)
10059 /* Friends have special name lookup rules. */
10060 ts = ts_within_enclosing_non_class;
10061 else if (is_declaration
10062 && cp_lexer_next_token_is (parser->lexer,
10063 CPP_SEMICOLON))
10064 /* This is a `class-key identifier ;' */
10065 ts = ts_current;
10066 else
10067 ts = ts_global;
10068
0cbd7506
MS
10069 /* Warn about attributes. They are ignored. */
10070 if (attributes)
5c498b10
DD
10071 warning (OPT_Wattributes,
10072 "type attributes are honored only at type definition");
e0fed25b 10073
ca85f659
MM
10074 template_p =
10075 (parser->num_template_parameter_lists
10076 && (cp_parser_next_token_starts_class_definition_p (parser)
10077 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10078 type = xref_tag (tag_type, identifier, ts, template_p);
a723baf1
MM
10079 }
10080 }
10081 if (tag_type != enum_type)
10082 cp_parser_check_class_key (tag_type, type);
ee43dab5
MM
10083
10084 /* A "<" cannot follow an elaborated type specifier. If that
10085 happens, the user was probably trying to form a template-id. */
10086 cp_parser_check_for_invalid_template_id (parser, type);
10087
a723baf1
MM
10088 return type;
10089}
10090
10091/* Parse an enum-specifier.
10092
10093 enum-specifier:
10094 enum identifier [opt] { enumerator-list [opt] }
10095
f6af9a15
MA
10096 GNU Extensions:
10097 enum identifier [opt] { enumerator-list [opt] } attributes
10098
a723baf1
MM
10099 Returns an ENUM_TYPE representing the enumeration. */
10100
10101static tree
94edc4ab 10102cp_parser_enum_specifier (cp_parser* parser)
a723baf1 10103{
ff4eb0b5 10104 tree identifier;
a723baf1
MM
10105 tree type;
10106
ff4eb0b5
ZW
10107 /* Caller guarantees that the current token is 'enum', an identifier
10108 possibly follows, and the token after that is an opening brace.
10109 If we don't have an identifier, fabricate an anonymous name for
10110 the enumeration being defined. */
10111 cp_lexer_consume_token (parser->lexer);
a723baf1 10112
ff4eb0b5 10113 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
a723baf1 10114 identifier = cp_parser_identifier (parser);
ff4eb0b5
ZW
10115 else
10116 identifier = make_anon_name ();
a723baf1 10117
a723baf1
MM
10118 /* Issue an error message if type-definitions are forbidden here. */
10119 cp_parser_check_type_definition (parser);
10120
2cfe82fe
ZW
10121 /* Create the new type. We do this before consuming the opening brace
10122 so the enum will be recorded as being on the line of its tag (or the
10123 'enum' keyword, if there is no tag). */
ff4eb0b5 10124 type = start_enum (identifier);
a723baf1 10125
2cfe82fe
ZW
10126 /* Consume the opening brace. */
10127 cp_lexer_consume_token (parser->lexer);
10128
ff4eb0b5
ZW
10129 /* If the next token is not '}', then there are some enumerators. */
10130 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
a723baf1 10131 cp_parser_enumerator_list (parser, type);
ff4eb0b5
ZW
10132
10133 /* Consume the final '}'. */
a723baf1
MM
10134 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10135
f6af9a15 10136 /* Look for trailing attributes to apply to this enumeration, and
03fd3f84 10137 apply them if appropriate. */
f6af9a15
MA
10138 if (cp_parser_allow_gnu_extensions_p (parser))
10139 {
10140 tree trailing_attr = cp_parser_attributes_opt (parser);
10141 cplus_decl_attributes (&type,
10142 trailing_attr,
10143 (int) ATTR_FLAG_TYPE_IN_PLACE);
10144 }
10145
a723baf1
MM
10146 /* Finish up the enumeration. */
10147 finish_enum (type);
10148
10149 return type;
10150}
10151
10152/* Parse an enumerator-list. The enumerators all have the indicated
21526606 10153 TYPE.
a723baf1
MM
10154
10155 enumerator-list:
10156 enumerator-definition
10157 enumerator-list , enumerator-definition */
10158
10159static void
94edc4ab 10160cp_parser_enumerator_list (cp_parser* parser, tree type)
a723baf1
MM
10161{
10162 while (true)
10163 {
a723baf1
MM
10164 /* Parse an enumerator-definition. */
10165 cp_parser_enumerator_definition (parser, type);
ff4eb0b5
ZW
10166
10167 /* If the next token is not a ',', we've reached the end of
10168 the list. */
10169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
a723baf1
MM
10170 break;
10171 /* Otherwise, consume the `,' and keep going. */
10172 cp_lexer_consume_token (parser->lexer);
10173 /* If the next token is a `}', there is a trailing comma. */
10174 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10175 {
10176 if (pedantic && !in_system_header)
10177 pedwarn ("comma at end of enumerator list");
10178 break;
10179 }
10180 }
10181}
10182
10183/* Parse an enumerator-definition. The enumerator has the indicated
10184 TYPE.
10185
10186 enumerator-definition:
10187 enumerator
10188 enumerator = constant-expression
21526606 10189
a723baf1
MM
10190 enumerator:
10191 identifier */
10192
10193static void
94edc4ab 10194cp_parser_enumerator_definition (cp_parser* parser, tree type)
a723baf1 10195{
a723baf1
MM
10196 tree identifier;
10197 tree value;
10198
10199 /* Look for the identifier. */
10200 identifier = cp_parser_identifier (parser);
10201 if (identifier == error_mark_node)
10202 return;
21526606 10203
ff4eb0b5
ZW
10204 /* If the next token is an '=', then there is an explicit value. */
10205 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
a723baf1
MM
10206 {
10207 /* Consume the `=' token. */
10208 cp_lexer_consume_token (parser->lexer);
10209 /* Parse the value. */
21526606 10210 value = cp_parser_constant_expression (parser,
d17811fd 10211 /*allow_non_constant_p=*/false,
14d22dd6 10212 NULL);
a723baf1
MM
10213 }
10214 else
10215 value = NULL_TREE;
10216
10217 /* Create the enumerator. */
10218 build_enumerator (identifier, value, type);
10219}
10220
10221/* Parse a namespace-name.
10222
10223 namespace-name:
10224 original-namespace-name
10225 namespace-alias
10226
10227 Returns the NAMESPACE_DECL for the namespace. */
10228
10229static tree
94edc4ab 10230cp_parser_namespace_name (cp_parser* parser)
a723baf1
MM
10231{
10232 tree identifier;
10233 tree namespace_decl;
10234
10235 /* Get the name of the namespace. */
10236 identifier = cp_parser_identifier (parser);
10237 if (identifier == error_mark_node)
10238 return error_mark_node;
10239
eea9800f
MM
10240 /* Look up the identifier in the currently active scope. Look only
10241 for namespaces, due to:
10242
10243 [basic.lookup.udir]
10244
10245 When looking up a namespace-name in a using-directive or alias
21526606 10246 definition, only namespace names are considered.
eea9800f
MM
10247
10248 And:
10249
10250 [basic.lookup.qual]
10251
10252 During the lookup of a name preceding the :: scope resolution
21526606 10253 operator, object, function, and enumerator names are ignored.
eea9800f
MM
10254
10255 (Note that cp_parser_class_or_namespace_name only calls this
10256 function if the token after the name is the scope resolution
10257 operator.) */
10258 namespace_decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 10259 none_type,
b0bc6e8e 10260 /*is_template=*/false,
eea9800f 10261 /*is_namespace=*/true,
8f78f01f
MM
10262 /*check_dependency=*/true,
10263 /*ambiguous_p=*/NULL);
a723baf1
MM
10264 /* If it's not a namespace, issue an error. */
10265 if (namespace_decl == error_mark_node
10266 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10267 {
166206ce
VR
10268 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10269 error ("%qD is not a namespace-name", identifier);
a723baf1
MM
10270 cp_parser_error (parser, "expected namespace-name");
10271 namespace_decl = error_mark_node;
10272 }
21526606 10273
a723baf1
MM
10274 return namespace_decl;
10275}
10276
10277/* Parse a namespace-definition.
10278
10279 namespace-definition:
10280 named-namespace-definition
21526606 10281 unnamed-namespace-definition
a723baf1
MM
10282
10283 named-namespace-definition:
10284 original-namespace-definition
10285 extension-namespace-definition
10286
10287 original-namespace-definition:
10288 namespace identifier { namespace-body }
21526606 10289
a723baf1
MM
10290 extension-namespace-definition:
10291 namespace original-namespace-name { namespace-body }
21526606 10292
a723baf1
MM
10293 unnamed-namespace-definition:
10294 namespace { namespace-body } */
10295
10296static void
94edc4ab 10297cp_parser_namespace_definition (cp_parser* parser)
a723baf1
MM
10298{
10299 tree identifier;
10300
10301 /* Look for the `namespace' keyword. */
10302 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10303
10304 /* Get the name of the namespace. We do not attempt to distinguish
10305 between an original-namespace-definition and an
10306 extension-namespace-definition at this point. The semantic
10307 analysis routines are responsible for that. */
10308 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10309 identifier = cp_parser_identifier (parser);
10310 else
10311 identifier = NULL_TREE;
10312
10313 /* Look for the `{' to start the namespace. */
10314 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10315 /* Start the namespace. */
10316 push_namespace (identifier);
10317 /* Parse the body of the namespace. */
10318 cp_parser_namespace_body (parser);
10319 /* Finish the namespace. */
10320 pop_namespace ();
10321 /* Look for the final `}'. */
10322 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10323}
10324
10325/* Parse a namespace-body.
10326
10327 namespace-body:
10328 declaration-seq [opt] */
10329
10330static void
94edc4ab 10331cp_parser_namespace_body (cp_parser* parser)
a723baf1
MM
10332{
10333 cp_parser_declaration_seq_opt (parser);
10334}
10335
10336/* Parse a namespace-alias-definition.
10337
10338 namespace-alias-definition:
10339 namespace identifier = qualified-namespace-specifier ; */
10340
10341static void
94edc4ab 10342cp_parser_namespace_alias_definition (cp_parser* parser)
a723baf1
MM
10343{
10344 tree identifier;
10345 tree namespace_specifier;
10346
10347 /* Look for the `namespace' keyword. */
10348 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10349 /* Look for the identifier. */
10350 identifier = cp_parser_identifier (parser);
10351 if (identifier == error_mark_node)
10352 return;
10353 /* Look for the `=' token. */
10354 cp_parser_require (parser, CPP_EQ, "`='");
10355 /* Look for the qualified-namespace-specifier. */
21526606 10356 namespace_specifier
a723baf1
MM
10357 = cp_parser_qualified_namespace_specifier (parser);
10358 /* Look for the `;' token. */
10359 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10360
10361 /* Register the alias in the symbol table. */
10362 do_namespace_alias (identifier, namespace_specifier);
10363}
10364
10365/* Parse a qualified-namespace-specifier.
10366
10367 qualified-namespace-specifier:
10368 :: [opt] nested-name-specifier [opt] namespace-name
10369
10370 Returns a NAMESPACE_DECL corresponding to the specified
10371 namespace. */
10372
10373static tree
94edc4ab 10374cp_parser_qualified_namespace_specifier (cp_parser* parser)
a723baf1
MM
10375{
10376 /* Look for the optional `::'. */
21526606 10377 cp_parser_global_scope_opt (parser,
a723baf1
MM
10378 /*current_scope_valid_p=*/false);
10379
10380 /* Look for the optional nested-name-specifier. */
10381 cp_parser_nested_name_specifier_opt (parser,
10382 /*typename_keyword_p=*/false,
10383 /*check_dependency_p=*/true,
a668c6ad
MM
10384 /*type_p=*/false,
10385 /*is_declaration=*/true);
a723baf1
MM
10386
10387 return cp_parser_namespace_name (parser);
10388}
10389
10390/* Parse a using-declaration.
10391
10392 using-declaration:
10393 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10394 using :: unqualified-id ; */
10395
10396static void
94edc4ab 10397cp_parser_using_declaration (cp_parser* parser)
a723baf1
MM
10398{
10399 cp_token *token;
10400 bool typename_p = false;
10401 bool global_scope_p;
10402 tree decl;
10403 tree identifier;
ed5f054f 10404 tree qscope;
a723baf1
MM
10405
10406 /* Look for the `using' keyword. */
10407 cp_parser_require_keyword (parser, RID_USING, "`using'");
21526606 10408
a723baf1
MM
10409 /* Peek at the next token. */
10410 token = cp_lexer_peek_token (parser->lexer);
10411 /* See if it's `typename'. */
10412 if (token->keyword == RID_TYPENAME)
10413 {
10414 /* Remember that we've seen it. */
10415 typename_p = true;
10416 /* Consume the `typename' token. */
10417 cp_lexer_consume_token (parser->lexer);
10418 }
10419
10420 /* Look for the optional global scope qualification. */
21526606 10421 global_scope_p
a723baf1 10422 = (cp_parser_global_scope_opt (parser,
21526606 10423 /*current_scope_valid_p=*/false)
a723baf1
MM
10424 != NULL_TREE);
10425
10426 /* If we saw `typename', or didn't see `::', then there must be a
10427 nested-name-specifier present. */
10428 if (typename_p || !global_scope_p)
21526606 10429 qscope = cp_parser_nested_name_specifier (parser, typename_p,
ed5f054f
AO
10430 /*check_dependency_p=*/true,
10431 /*type_p=*/false,
10432 /*is_declaration=*/true);
a723baf1
MM
10433 /* Otherwise, we could be in either of the two productions. In that
10434 case, treat the nested-name-specifier as optional. */
10435 else
ed5f054f
AO
10436 qscope = cp_parser_nested_name_specifier_opt (parser,
10437 /*typename_keyword_p=*/false,
10438 /*check_dependency_p=*/true,
10439 /*type_p=*/false,
10440 /*is_declaration=*/true);
10441 if (!qscope)
10442 qscope = global_namespace;
a723baf1
MM
10443
10444 /* Parse the unqualified-id. */
21526606 10445 identifier = cp_parser_unqualified_id (parser,
a723baf1 10446 /*template_keyword_p=*/false,
f3c2dfc6
MM
10447 /*check_dependency_p=*/true,
10448 /*declarator_p=*/true);
a723baf1
MM
10449
10450 /* The function we call to handle a using-declaration is different
10451 depending on what scope we are in. */
f3c2dfc6
MM
10452 if (identifier == error_mark_node)
10453 ;
10454 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10455 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10456 /* [namespace.udecl]
10457
10458 A using declaration shall not name a template-id. */
10459 error ("a template-id may not appear in a using-declaration");
a723baf1
MM
10460 else
10461 {
a5201a91 10462 if (at_class_scope_p ())
4eb6d609 10463 {
f3c2dfc6 10464 /* Create the USING_DECL. */
1d786913 10465 decl = do_class_using_decl (parser->scope, identifier);
f3c2dfc6
MM
10466 /* Add it to the list of members in this class. */
10467 finish_member_declaration (decl);
4eb6d609 10468 }
a723baf1 10469 else
f3c2dfc6
MM
10470 {
10471 decl = cp_parser_lookup_name_simple (parser, identifier);
10472 if (decl == error_mark_node)
4bb8ca28 10473 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
a5201a91 10474 else if (!at_namespace_scope_p ())
ed5f054f 10475 do_local_using_decl (decl, qscope, identifier);
f3c2dfc6 10476 else
ed5f054f 10477 do_toplevel_using_decl (decl, qscope, identifier);
f3c2dfc6 10478 }
a723baf1
MM
10479 }
10480
10481 /* Look for the final `;'. */
10482 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10483}
10484
21526606
EC
10485/* Parse a using-directive.
10486
a723baf1
MM
10487 using-directive:
10488 using namespace :: [opt] nested-name-specifier [opt]
10489 namespace-name ; */
10490
10491static void
94edc4ab 10492cp_parser_using_directive (cp_parser* parser)
a723baf1
MM
10493{
10494 tree namespace_decl;
86098eb8 10495 tree attribs;
a723baf1
MM
10496
10497 /* Look for the `using' keyword. */
10498 cp_parser_require_keyword (parser, RID_USING, "`using'");
10499 /* And the `namespace' keyword. */
10500 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10501 /* Look for the optional `::' operator. */
10502 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
34cd5ae7 10503 /* And the optional nested-name-specifier. */
a723baf1
MM
10504 cp_parser_nested_name_specifier_opt (parser,
10505 /*typename_keyword_p=*/false,
10506 /*check_dependency_p=*/true,
a668c6ad
MM
10507 /*type_p=*/false,
10508 /*is_declaration=*/true);
a723baf1
MM
10509 /* Get the namespace being used. */
10510 namespace_decl = cp_parser_namespace_name (parser);
86098eb8
JM
10511 /* And any specified attributes. */
10512 attribs = cp_parser_attributes_opt (parser);
a723baf1 10513 /* Update the symbol table. */
86098eb8 10514 parse_using_directive (namespace_decl, attribs);
a723baf1
MM
10515 /* Look for the final `;'. */
10516 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10517}
10518
10519/* Parse an asm-definition.
10520
10521 asm-definition:
21526606 10522 asm ( string-literal ) ;
a723baf1
MM
10523
10524 GNU Extension:
10525
10526 asm-definition:
10527 asm volatile [opt] ( string-literal ) ;
10528 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10529 asm volatile [opt] ( string-literal : asm-operand-list [opt]
0cbd7506 10530 : asm-operand-list [opt] ) ;
21526606 10531 asm volatile [opt] ( string-literal : asm-operand-list [opt]
0cbd7506
MS
10532 : asm-operand-list [opt]
10533 : asm-operand-list [opt] ) ; */
a723baf1
MM
10534
10535static void
94edc4ab 10536cp_parser_asm_definition (cp_parser* parser)
a723baf1 10537{
a723baf1
MM
10538 tree string;
10539 tree outputs = NULL_TREE;
10540 tree inputs = NULL_TREE;
10541 tree clobbers = NULL_TREE;
10542 tree asm_stmt;
10543 bool volatile_p = false;
10544 bool extended_p = false;
10545
10546 /* Look for the `asm' keyword. */
10547 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10548 /* See if the next token is `volatile'. */
10549 if (cp_parser_allow_gnu_extensions_p (parser)
10550 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10551 {
10552 /* Remember that we saw the `volatile' keyword. */
10553 volatile_p = true;
10554 /* Consume the token. */
10555 cp_lexer_consume_token (parser->lexer);
10556 }
10557 /* Look for the opening `('. */
c162c75e
MA
10558 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10559 return;
a723baf1 10560 /* Look for the string. */
c162c75e
MA
10561 string = cp_parser_string_literal (parser, false, false);
10562 if (string == error_mark_node)
10563 {
10564 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10565 /*consume_paren=*/true);
10566 return;
10567 }
10568
a723baf1 10569 /* If we're allowing GNU extensions, check for the extended assembly
21526606 10570 syntax. Unfortunately, the `:' tokens need not be separated by
a723baf1
MM
10571 a space in C, and so, for compatibility, we tolerate that here
10572 too. Doing that means that we have to treat the `::' operator as
10573 two `:' tokens. */
10574 if (cp_parser_allow_gnu_extensions_p (parser)
10575 && at_function_scope_p ()
10576 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10577 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10578 {
10579 bool inputs_p = false;
10580 bool clobbers_p = false;
10581
10582 /* The extended syntax was used. */
10583 extended_p = true;
10584
10585 /* Look for outputs. */
10586 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10587 {
10588 /* Consume the `:'. */
10589 cp_lexer_consume_token (parser->lexer);
10590 /* Parse the output-operands. */
21526606 10591 if (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
10592 CPP_COLON)
10593 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
10594 CPP_SCOPE)
10595 && cp_lexer_next_token_is_not (parser->lexer,
10596 CPP_CLOSE_PAREN))
a723baf1
MM
10597 outputs = cp_parser_asm_operand_list (parser);
10598 }
10599 /* If the next token is `::', there are no outputs, and the
10600 next token is the beginning of the inputs. */
10601 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
bf5930d4
JB
10602 /* The inputs are coming next. */
10603 inputs_p = true;
a723baf1
MM
10604
10605 /* Look for inputs. */
10606 if (inputs_p
10607 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10608 {
bf5930d4
JB
10609 /* Consume the `:' or `::'. */
10610 cp_lexer_consume_token (parser->lexer);
a723baf1 10611 /* Parse the output-operands. */
21526606 10612 if (cp_lexer_next_token_is_not (parser->lexer,
a723baf1 10613 CPP_COLON)
8caf4c38
MM
10614 && cp_lexer_next_token_is_not (parser->lexer,
10615 CPP_CLOSE_PAREN))
a723baf1
MM
10616 inputs = cp_parser_asm_operand_list (parser);
10617 }
10618 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10619 /* The clobbers are coming next. */
10620 clobbers_p = true;
10621
10622 /* Look for clobbers. */
21526606 10623 if (clobbers_p
a723baf1
MM
10624 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10625 {
bf5930d4
JB
10626 /* Consume the `:' or `::'. */
10627 cp_lexer_consume_token (parser->lexer);
a723baf1 10628 /* Parse the clobbers. */
8caf4c38
MM
10629 if (cp_lexer_next_token_is_not (parser->lexer,
10630 CPP_CLOSE_PAREN))
10631 clobbers = cp_parser_asm_clobber_list (parser);
a723baf1
MM
10632 }
10633 }
10634 /* Look for the closing `)'. */
10635 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
10636 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10637 /*consume_paren=*/true);
a723baf1
MM
10638 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10639
e130a54b 10640 /* Create the ASM_EXPR. */
a723baf1
MM
10641 if (at_function_scope_p ())
10642 {
6de9cd9a
DN
10643 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10644 inputs, clobbers);
e130a54b 10645 /* If the extended syntax was not used, mark the ASM_EXPR. */
a723baf1 10646 if (!extended_p)
ca059043
AP
10647 {
10648 tree temp = asm_stmt;
10649 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10650 temp = TREE_OPERAND (temp, 0);
c8094d83 10651
ca059043
AP
10652 ASM_INPUT_P (temp) = 1;
10653 }
a723baf1
MM
10654 }
10655 else
10656 assemble_asm (string);
10657}
10658
10659/* Declarators [gram.dcl.decl] */
10660
10661/* Parse an init-declarator.
10662
10663 init-declarator:
10664 declarator initializer [opt]
10665
10666 GNU Extension:
10667
10668 init-declarator:
10669 declarator asm-specification [opt] attributes [opt] initializer [opt]
10670
4bb8ca28
MM
10671 function-definition:
10672 decl-specifier-seq [opt] declarator ctor-initializer [opt]
21526606
EC
10673 function-body
10674 decl-specifier-seq [opt] declarator function-try-block
4bb8ca28
MM
10675
10676 GNU Extension:
10677
10678 function-definition:
21526606 10679 __extension__ function-definition
4bb8ca28 10680
a723baf1 10681 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
c8e4f0e9 10682 Returns a representation of the entity declared. If MEMBER_P is TRUE,
cf22909c
KL
10683 then this declarator appears in a class scope. The new DECL created
10684 by this declarator is returned.
a723baf1
MM
10685
10686 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10687 for a function-definition here as well. If the declarator is a
10688 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10689 be TRUE upon return. By that point, the function-definition will
10690 have been completely parsed.
10691
10692 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10693 is FALSE. */
10694
10695static tree
21526606 10696cp_parser_init_declarator (cp_parser* parser,
62d1db17 10697 cp_decl_specifier_seq *decl_specifiers,
94edc4ab
NN
10698 bool function_definition_allowed_p,
10699 bool member_p,
560ad596 10700 int declares_class_or_enum,
94edc4ab 10701 bool* function_definition_p)
a723baf1
MM
10702{
10703 cp_token *token;
058b15c1 10704 cp_declarator *declarator;
62d1db17 10705 tree prefix_attributes;
a723baf1
MM
10706 tree attributes;
10707 tree asm_specification;
10708 tree initializer;
10709 tree decl = NULL_TREE;
10710 tree scope;
a723baf1
MM
10711 bool is_initialized;
10712 bool is_parenthesized_init;
39703eb9 10713 bool is_non_constant_init;
7efa3e22 10714 int ctor_dtor_or_conv_p;
a723baf1 10715 bool friend_p;
4514aa8c 10716 tree pushed_scope = NULL;
a723baf1 10717
62d1db17
MM
10718 /* Gather the attributes that were provided with the
10719 decl-specifiers. */
10720 prefix_attributes = decl_specifiers->attributes;
62d1db17 10721
a723baf1
MM
10722 /* Assume that this is not the declarator for a function
10723 definition. */
10724 if (function_definition_p)
10725 *function_definition_p = false;
10726
10727 /* Defer access checks while parsing the declarator; we cannot know
21526606 10728 what names are accessible until we know what is being
a723baf1 10729 declared. */
cf22909c
KL
10730 resume_deferring_access_checks ();
10731
a723baf1 10732 /* Parse the declarator. */
21526606 10733 declarator
62b8a44e 10734 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 10735 &ctor_dtor_or_conv_p,
db86dd14
MM
10736 /*parenthesized_p=*/NULL,
10737 /*member_p=*/false);
a723baf1 10738 /* Gather up the deferred checks. */
cf22909c 10739 stop_deferring_access_checks ();
24c0ef37 10740
a723baf1
MM
10741 /* If the DECLARATOR was erroneous, there's no need to go
10742 further. */
058b15c1 10743 if (declarator == cp_error_declarator)
cf22909c 10744 return error_mark_node;
a723baf1 10745
fc6a28d7
MM
10746 if (declares_class_or_enum & 2)
10747 cp_parser_check_for_definition_in_return_type (declarator,
10748 decl_specifiers->type);
560ad596 10749
a723baf1
MM
10750 /* Figure out what scope the entity declared by the DECLARATOR is
10751 located in. `grokdeclarator' sometimes changes the scope, so
10752 we compute it now. */
10753 scope = get_scope_of_declarator (declarator);
10754
10755 /* If we're allowing GNU extensions, look for an asm-specification
10756 and attributes. */
10757 if (cp_parser_allow_gnu_extensions_p (parser))
10758 {
10759 /* Look for an asm-specification. */
10760 asm_specification = cp_parser_asm_specification_opt (parser);
10761 /* And attributes. */
10762 attributes = cp_parser_attributes_opt (parser);
10763 }
10764 else
10765 {
10766 asm_specification = NULL_TREE;
10767 attributes = NULL_TREE;
10768 }
10769
10770 /* Peek at the next token. */
10771 token = cp_lexer_peek_token (parser->lexer);
10772 /* Check to see if the token indicates the start of a
10773 function-definition. */
10774 if (cp_parser_token_starts_function_definition_p (token))
10775 {
10776 if (!function_definition_allowed_p)
10777 {
10778 /* If a function-definition should not appear here, issue an
10779 error message. */
10780 cp_parser_error (parser,
10781 "a function-definition is not allowed here");
10782 return error_mark_node;
10783 }
10784 else
10785 {
a723baf1
MM
10786 /* Neither attributes nor an asm-specification are allowed
10787 on a function-definition. */
10788 if (asm_specification)
10789 error ("an asm-specification is not allowed on a function-definition");
10790 if (attributes)
10791 error ("attributes are not allowed on a function-definition");
10792 /* This is a function-definition. */
10793 *function_definition_p = true;
10794
a723baf1 10795 /* Parse the function definition. */
4bb8ca28
MM
10796 if (member_p)
10797 decl = cp_parser_save_member_function_body (parser,
10798 decl_specifiers,
10799 declarator,
10800 prefix_attributes);
10801 else
21526606 10802 decl
4bb8ca28
MM
10803 = (cp_parser_function_definition_from_specifiers_and_declarator
10804 (parser, decl_specifiers, prefix_attributes, declarator));
24c0ef37 10805
a723baf1
MM
10806 return decl;
10807 }
10808 }
10809
10810 /* [dcl.dcl]
10811
10812 Only in function declarations for constructors, destructors, and
21526606 10813 type conversions can the decl-specifier-seq be omitted.
a723baf1
MM
10814
10815 We explicitly postpone this check past the point where we handle
10816 function-definitions because we tolerate function-definitions
10817 that are missing their return types in some modes. */
62d1db17 10818 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
a723baf1 10819 {
21526606 10820 cp_parser_error (parser,
a723baf1
MM
10821 "expected constructor, destructor, or type conversion");
10822 return error_mark_node;
10823 }
10824
10825 /* An `=' or an `(' indicates an initializer. */
21526606 10826 is_initialized = (token->type == CPP_EQ
a723baf1
MM
10827 || token->type == CPP_OPEN_PAREN);
10828 /* If the init-declarator isn't initialized and isn't followed by a
10829 `,' or `;', it's not a valid init-declarator. */
21526606 10830 if (!is_initialized
a723baf1
MM
10831 && token->type != CPP_COMMA
10832 && token->type != CPP_SEMICOLON)
10833 {
996c2b52 10834 cp_parser_error (parser, "expected initializer");
a723baf1
MM
10835 return error_mark_node;
10836 }
10837
10838 /* Because start_decl has side-effects, we should only call it if we
10839 know we're going ahead. By this point, we know that we cannot
10840 possibly be looking at any other construct. */
10841 cp_parser_commit_to_tentative_parse (parser);
10842
e90c7b84
ILT
10843 /* If the decl specifiers were bad, issue an error now that we're
10844 sure this was intended to be a declarator. Then continue
10845 declaring the variable(s), as int, to try to cut down on further
10846 errors. */
62d1db17
MM
10847 if (decl_specifiers->any_specifiers_p
10848 && decl_specifiers->type == error_mark_node)
e90c7b84
ILT
10849 {
10850 cp_parser_error (parser, "invalid type in declaration");
62d1db17 10851 decl_specifiers->type = integer_type_node;
e90c7b84
ILT
10852 }
10853
a723baf1
MM
10854 /* Check to see whether or not this declaration is a friend. */
10855 friend_p = cp_parser_friend_p (decl_specifiers);
10856
10857 /* Check that the number of template-parameter-lists is OK. */
ee3071ef 10858 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
cf22909c 10859 return error_mark_node;
a723baf1
MM
10860
10861 /* Enter the newly declared entry in the symbol table. If we're
10862 processing a declaration in a class-specifier, we wait until
10863 after processing the initializer. */
10864 if (!member_p)
10865 {
10866 if (parser->in_unbraced_linkage_specification_p)
10867 {
62d1db17 10868 decl_specifiers->storage_class = sc_extern;
a723baf1
MM
10869 have_extern_spec = false;
10870 }
ee3071ef 10871 decl = start_decl (declarator, decl_specifiers,
73a8adb6 10872 is_initialized, attributes, prefix_attributes,
4514aa8c 10873 &pushed_scope);
a723baf1 10874 }
73a8adb6
MM
10875 else if (scope)
10876 /* Enter the SCOPE. That way unqualified names appearing in the
10877 initializer will be looked up in SCOPE. */
4514aa8c 10878 pushed_scope = push_scope (scope);
a723baf1
MM
10879
10880 /* Perform deferred access control checks, now that we know in which
10881 SCOPE the declared entity resides. */
21526606 10882 if (!member_p && decl)
a723baf1
MM
10883 {
10884 tree saved_current_function_decl = NULL_TREE;
10885
10886 /* If the entity being declared is a function, pretend that we
10887 are in its scope. If it is a `friend', it may have access to
9bcb9aae 10888 things that would not otherwise be accessible. */
a723baf1
MM
10889 if (TREE_CODE (decl) == FUNCTION_DECL)
10890 {
10891 saved_current_function_decl = current_function_decl;
10892 current_function_decl = decl;
10893 }
21526606 10894
cf22909c
KL
10895 /* Perform the access control checks for the declarator and the
10896 the decl-specifiers. */
10897 perform_deferred_access_checks ();
a723baf1
MM
10898
10899 /* Restore the saved value. */
10900 if (TREE_CODE (decl) == FUNCTION_DECL)
10901 current_function_decl = saved_current_function_decl;
10902 }
10903
10904 /* Parse the initializer. */
10905 if (is_initialized)
21526606 10906 initializer = cp_parser_initializer (parser,
39703eb9
MM
10907 &is_parenthesized_init,
10908 &is_non_constant_init);
a723baf1
MM
10909 else
10910 {
10911 initializer = NULL_TREE;
10912 is_parenthesized_init = false;
39703eb9 10913 is_non_constant_init = true;
a723baf1
MM
10914 }
10915
10916 /* The old parser allows attributes to appear after a parenthesized
10917 initializer. Mark Mitchell proposed removing this functionality
10918 on the GCC mailing lists on 2002-08-13. This parser accepts the
10919 attributes -- but ignores them. */
10920 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10921 if (cp_parser_attributes_opt (parser))
5c498b10
DD
10922 warning (OPT_Wattributes,
10923 "attributes after parenthesized initializer ignored");
a723baf1 10924
a723baf1
MM
10925 /* For an in-class declaration, use `grokfield' to create the
10926 declaration. */
10927 if (member_p)
8db1028e 10928 {
4514aa8c 10929 if (pushed_scope)
62a4d942 10930 {
4514aa8c
NS
10931 pop_scope (pushed_scope);
10932 pushed_scope = false;
62a4d942 10933 }
8db1028e
NS
10934 decl = grokfield (declarator, decl_specifiers,
10935 initializer, /*asmspec=*/NULL_TREE,
a723baf1 10936 /*attributes=*/NULL_TREE);
8db1028e
NS
10937 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10938 cp_parser_save_default_args (parser, decl);
10939 }
21526606 10940
a723baf1
MM
10941 /* Finish processing the declaration. But, skip friend
10942 declarations. */
550205c3 10943 if (!friend_p && decl && decl != error_mark_node)
73a8adb6
MM
10944 {
10945 cp_finish_decl (decl,
10946 initializer,
10947 asm_specification,
10948 /* If the initializer is in parentheses, then this is
10949 a direct-initialization, which means that an
10950 `explicit' constructor is OK. Otherwise, an
10951 `explicit' constructor cannot be used. */
10952 ((is_parenthesized_init || !is_initialized)
a723baf1 10953 ? 0 : LOOKUP_ONLYCONVERTING));
73a8adb6 10954 }
4514aa8c
NS
10955 if (!friend_p && pushed_scope)
10956 pop_scope (pushed_scope);
a723baf1 10957
39703eb9
MM
10958 /* Remember whether or not variables were initialized by
10959 constant-expressions. */
21526606 10960 if (decl && TREE_CODE (decl) == VAR_DECL
39703eb9
MM
10961 && is_initialized && !is_non_constant_init)
10962 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10963
a723baf1
MM
10964 return decl;
10965}
10966
10967/* Parse a declarator.
21526606 10968
a723baf1
MM
10969 declarator:
10970 direct-declarator
21526606 10971 ptr-operator declarator
a723baf1
MM
10972
10973 abstract-declarator:
10974 ptr-operator abstract-declarator [opt]
10975 direct-abstract-declarator
10976
10977 GNU Extensions:
10978
10979 declarator:
10980 attributes [opt] direct-declarator
21526606 10981 attributes [opt] ptr-operator declarator
a723baf1
MM
10982
10983 abstract-declarator:
10984 attributes [opt] ptr-operator abstract-declarator [opt]
10985 attributes [opt] direct-abstract-declarator
21526606 10986
7efa3e22
NS
10987 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10988 detect constructor, destructor or conversion operators. It is set
10989 to -1 if the declarator is a name, and +1 if it is a
10990 function. Otherwise it is set to zero. Usually you just want to
10991 test for >0, but internally the negative value is used.
21526606 10992
a723baf1
MM
10993 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10994 a decl-specifier-seq unless it declares a constructor, destructor,
10995 or conversion. It might seem that we could check this condition in
10996 semantic analysis, rather than parsing, but that makes it difficult
10997 to handle something like `f()'. We want to notice that there are
10998 no decl-specifiers, and therefore realize that this is an
21526606
EC
10999 expression, not a declaration.)
11000
4bb8ca28 11001 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
c8094d83 11002 the declarator is a direct-declarator of the form "(...)".
db86dd14
MM
11003
11004 MEMBER_P is true iff this declarator is a member-declarator. */
a723baf1 11005
058b15c1 11006static cp_declarator *
21526606 11007cp_parser_declarator (cp_parser* parser,
0cbd7506
MS
11008 cp_parser_declarator_kind dcl_kind,
11009 int* ctor_dtor_or_conv_p,
db86dd14
MM
11010 bool* parenthesized_p,
11011 bool member_p)
a723baf1
MM
11012{
11013 cp_token *token;
058b15c1 11014 cp_declarator *declarator;
a723baf1 11015 enum tree_code code;
3c01e5df 11016 cp_cv_quals cv_quals;
a723baf1
MM
11017 tree class_type;
11018 tree attributes = NULL_TREE;
11019
11020 /* Assume this is not a constructor, destructor, or type-conversion
11021 operator. */
11022 if (ctor_dtor_or_conv_p)
7efa3e22 11023 *ctor_dtor_or_conv_p = 0;
a723baf1
MM
11024
11025 if (cp_parser_allow_gnu_extensions_p (parser))
11026 attributes = cp_parser_attributes_opt (parser);
21526606 11027
a723baf1
MM
11028 /* Peek at the next token. */
11029 token = cp_lexer_peek_token (parser->lexer);
21526606 11030
a723baf1
MM
11031 /* Check for the ptr-operator production. */
11032 cp_parser_parse_tentatively (parser);
11033 /* Parse the ptr-operator. */
21526606
EC
11034 code = cp_parser_ptr_operator (parser,
11035 &class_type,
3c01e5df 11036 &cv_quals);
a723baf1
MM
11037 /* If that worked, then we have a ptr-operator. */
11038 if (cp_parser_parse_definitely (parser))
11039 {
4bb8ca28
MM
11040 /* If a ptr-operator was found, then this declarator was not
11041 parenthesized. */
11042 if (parenthesized_p)
11043 *parenthesized_p = true;
a723baf1
MM
11044 /* The dependent declarator is optional if we are parsing an
11045 abstract-declarator. */
62b8a44e 11046 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1
MM
11047 cp_parser_parse_tentatively (parser);
11048
11049 /* Parse the dependent declarator. */
62b8a44e 11050 declarator = cp_parser_declarator (parser, dcl_kind,
4bb8ca28 11051 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
11052 /*parenthesized_p=*/NULL,
11053 /*member_p=*/false);
a723baf1
MM
11054
11055 /* If we are parsing an abstract-declarator, we must handle the
11056 case where the dependent declarator is absent. */
62b8a44e
NS
11057 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11058 && !cp_parser_parse_definitely (parser))
058b15c1 11059 declarator = NULL;
21526606 11060
a723baf1 11061 /* Build the representation of the ptr-operator. */
058b15c1 11062 if (class_type)
3c01e5df 11063 declarator = make_ptrmem_declarator (cv_quals,
058b15c1
MM
11064 class_type,
11065 declarator);
11066 else if (code == INDIRECT_REF)
3c01e5df 11067 declarator = make_pointer_declarator (cv_quals, declarator);
a723baf1 11068 else
3c01e5df 11069 declarator = make_reference_declarator (cv_quals, declarator);
a723baf1
MM
11070 }
11071 /* Everything else is a direct-declarator. */
11072 else
4bb8ca28
MM
11073 {
11074 if (parenthesized_p)
11075 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11076 CPP_OPEN_PAREN);
11077 declarator = cp_parser_direct_declarator (parser, dcl_kind,
db86dd14
MM
11078 ctor_dtor_or_conv_p,
11079 member_p);
4bb8ca28 11080 }
a723baf1 11081
058b15c1
MM
11082 if (attributes && declarator != cp_error_declarator)
11083 declarator->attributes = attributes;
21526606 11084
a723baf1
MM
11085 return declarator;
11086}
11087
11088/* Parse a direct-declarator or direct-abstract-declarator.
11089
11090 direct-declarator:
11091 declarator-id
11092 direct-declarator ( parameter-declaration-clause )
21526606 11093 cv-qualifier-seq [opt]
a723baf1
MM
11094 exception-specification [opt]
11095 direct-declarator [ constant-expression [opt] ]
21526606 11096 ( declarator )
a723baf1
MM
11097
11098 direct-abstract-declarator:
11099 direct-abstract-declarator [opt]
21526606 11100 ( parameter-declaration-clause )
a723baf1
MM
11101 cv-qualifier-seq [opt]
11102 exception-specification [opt]
11103 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11104 ( abstract-declarator )
11105
62b8a44e
NS
11106 Returns a representation of the declarator. DCL_KIND is
11107 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11108 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11109 we are parsing a direct-declarator. It is
11110 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11111 of ambiguity we prefer an abstract declarator, as per
db86dd14 11112 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
058b15c1 11113 cp_parser_declarator. */
a723baf1 11114
058b15c1 11115static cp_declarator *
94edc4ab 11116cp_parser_direct_declarator (cp_parser* parser,
0cbd7506
MS
11117 cp_parser_declarator_kind dcl_kind,
11118 int* ctor_dtor_or_conv_p,
db86dd14 11119 bool member_p)
a723baf1
MM
11120{
11121 cp_token *token;
058b15c1 11122 cp_declarator *declarator = NULL;
a723baf1
MM
11123 tree scope = NULL_TREE;
11124 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11125 bool saved_in_declarator_p = parser->in_declarator_p;
62b8a44e 11126 bool first = true;
4514aa8c 11127 tree pushed_scope = NULL_TREE;
21526606 11128
62b8a44e 11129 while (true)
a723baf1 11130 {
62b8a44e
NS
11131 /* Peek at the next token. */
11132 token = cp_lexer_peek_token (parser->lexer);
11133 if (token->type == CPP_OPEN_PAREN)
a723baf1 11134 {
62b8a44e 11135 /* This is either a parameter-declaration-clause, or a
0cbd7506
MS
11136 parenthesized declarator. When we know we are parsing a
11137 named declarator, it must be a parenthesized declarator
11138 if FIRST is true. For instance, `(int)' is a
11139 parameter-declaration-clause, with an omitted
11140 direct-abstract-declarator. But `((*))', is a
11141 parenthesized abstract declarator. Finally, when T is a
11142 template parameter `(T)' is a
11143 parameter-declaration-clause, and not a parenthesized
11144 named declarator.
21526606 11145
62b8a44e
NS
11146 We first try and parse a parameter-declaration-clause,
11147 and then try a nested declarator (if FIRST is true).
a723baf1 11148
62b8a44e
NS
11149 It is not an error for it not to be a
11150 parameter-declaration-clause, even when FIRST is
11151 false. Consider,
11152
11153 int i (int);
11154 int i (3);
11155
11156 The first is the declaration of a function while the
11157 second is a the definition of a variable, including its
11158 initializer.
11159
11160 Having seen only the parenthesis, we cannot know which of
11161 these two alternatives should be selected. Even more
11162 complex are examples like:
11163
0cbd7506 11164 int i (int (a));
62b8a44e
NS
11165 int i (int (3));
11166
11167 The former is a function-declaration; the latter is a
21526606 11168 variable initialization.
62b8a44e 11169
34cd5ae7 11170 Thus again, we try a parameter-declaration-clause, and if
62b8a44e
NS
11171 that fails, we back out and return. */
11172
11173 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1 11174 {
058b15c1 11175 cp_parameter_declarator *params;
4047b164 11176 unsigned saved_num_template_parameter_lists;
21526606 11177
db86dd14
MM
11178 /* In a member-declarator, the only valid interpretation
11179 of a parenthesis is the start of a
11180 parameter-declaration-clause. (It is invalid to
11181 initialize a static data member with a parenthesized
11182 initializer; only the "=" form of initialization is
11183 permitted.) */
11184 if (!member_p)
11185 cp_parser_parse_tentatively (parser);
a723baf1 11186
62b8a44e
NS
11187 /* Consume the `('. */
11188 cp_lexer_consume_token (parser->lexer);
11189 if (first)
11190 {
11191 /* If this is going to be an abstract declarator, we're
11192 in a declarator and we can't have default args. */
11193 parser->default_arg_ok_p = false;
11194 parser->in_declarator_p = true;
11195 }
21526606 11196
4047b164
KL
11197 /* Inside the function parameter list, surrounding
11198 template-parameter-lists do not apply. */
11199 saved_num_template_parameter_lists
11200 = parser->num_template_parameter_lists;
11201 parser->num_template_parameter_lists = 0;
11202
62b8a44e
NS
11203 /* Parse the parameter-declaration-clause. */
11204 params = cp_parser_parameter_declaration_clause (parser);
11205
4047b164
KL
11206 parser->num_template_parameter_lists
11207 = saved_num_template_parameter_lists;
11208
62b8a44e 11209 /* If all went well, parse the cv-qualifier-seq and the
0cbd7506 11210 exception-specification. */
db86dd14 11211 if (member_p || cp_parser_parse_definitely (parser))
62b8a44e 11212 {
3c01e5df 11213 cp_cv_quals cv_quals;
62b8a44e 11214 tree exception_specification;
7efa3e22
NS
11215
11216 if (ctor_dtor_or_conv_p)
11217 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
62b8a44e
NS
11218 first = false;
11219 /* Consume the `)'. */
11220 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11221
11222 /* Parse the cv-qualifier-seq. */
3c01e5df 11223 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
62b8a44e 11224 /* And the exception-specification. */
21526606 11225 exception_specification
62b8a44e
NS
11226 = cp_parser_exception_specification_opt (parser);
11227
11228 /* Create the function-declarator. */
11229 declarator = make_call_declarator (declarator,
11230 params,
3c01e5df 11231 cv_quals,
62b8a44e
NS
11232 exception_specification);
11233 /* Any subsequent parameter lists are to do with
0cbd7506
MS
11234 return type, so are not those of the declared
11235 function. */
62b8a44e 11236 parser->default_arg_ok_p = false;
21526606 11237
62b8a44e
NS
11238 /* Repeat the main loop. */
11239 continue;
11240 }
11241 }
21526606 11242
62b8a44e
NS
11243 /* If this is the first, we can try a parenthesized
11244 declarator. */
11245 if (first)
a723baf1 11246 {
a7324e75
MM
11247 bool saved_in_type_id_in_expr_p;
11248
a723baf1 11249 parser->default_arg_ok_p = saved_default_arg_ok_p;
62b8a44e 11250 parser->in_declarator_p = saved_in_declarator_p;
21526606 11251
62b8a44e
NS
11252 /* Consume the `('. */
11253 cp_lexer_consume_token (parser->lexer);
11254 /* Parse the nested declarator. */
a7324e75
MM
11255 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11256 parser->in_type_id_in_expr_p = true;
21526606 11257 declarator
4bb8ca28 11258 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
db86dd14
MM
11259 /*parenthesized_p=*/NULL,
11260 member_p);
a7324e75 11261 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
62b8a44e
NS
11262 first = false;
11263 /* Expect a `)'. */
11264 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
058b15c1
MM
11265 declarator = cp_error_declarator;
11266 if (declarator == cp_error_declarator)
62b8a44e 11267 break;
21526606 11268
62b8a44e 11269 goto handle_declarator;
a723baf1 11270 }
9bcb9aae 11271 /* Otherwise, we must be done. */
62b8a44e
NS
11272 else
11273 break;
a723baf1 11274 }
62b8a44e
NS
11275 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11276 && token->type == CPP_OPEN_SQUARE)
a723baf1 11277 {
62b8a44e 11278 /* Parse an array-declarator. */
a723baf1
MM
11279 tree bounds;
11280
7efa3e22
NS
11281 if (ctor_dtor_or_conv_p)
11282 *ctor_dtor_or_conv_p = 0;
21526606 11283
62b8a44e
NS
11284 first = false;
11285 parser->default_arg_ok_p = false;
11286 parser->in_declarator_p = true;
a723baf1
MM
11287 /* Consume the `['. */
11288 cp_lexer_consume_token (parser->lexer);
11289 /* Peek at the next token. */
11290 token = cp_lexer_peek_token (parser->lexer);
11291 /* If the next token is `]', then there is no
11292 constant-expression. */
11293 if (token->type != CPP_CLOSE_SQUARE)
14d22dd6
MM
11294 {
11295 bool non_constant_p;
11296
21526606 11297 bounds
14d22dd6
MM
11298 = cp_parser_constant_expression (parser,
11299 /*allow_non_constant=*/true,
11300 &non_constant_p);
d17811fd 11301 if (!non_constant_p)
9baa27a9 11302 bounds = fold_non_dependent_expr (bounds);
88e95ee3
MM
11303 /* Normally, the array bound must be an integral constant
11304 expression. However, as an extension, we allow VLAs
c8094d83 11305 in function scopes. */
b671e5a4 11306 else if (!at_function_scope_p ())
44370687
MM
11307 {
11308 error ("array bound is not an integer constant");
11309 bounds = error_mark_node;
11310 }
14d22dd6 11311 }
a723baf1
MM
11312 else
11313 bounds = NULL_TREE;
11314 /* Look for the closing `]'. */
62b8a44e
NS
11315 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11316 {
058b15c1 11317 declarator = cp_error_declarator;
62b8a44e
NS
11318 break;
11319 }
a723baf1 11320
058b15c1 11321 declarator = make_array_declarator (declarator, bounds);
a723baf1 11322 }
62b8a44e 11323 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
a723baf1 11324 {
1d786913
MM
11325 tree qualifying_scope;
11326 tree unqualified_name;
058b15c1 11327
a668c6ad 11328 /* Parse a declarator-id */
62b8a44e
NS
11329 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11330 cp_parser_parse_tentatively (parser);
1d786913
MM
11331 unqualified_name = cp_parser_declarator_id (parser);
11332 qualifying_scope = parser->scope;
712becab
NS
11333 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11334 {
11335 if (!cp_parser_parse_definitely (parser))
1d786913
MM
11336 unqualified_name = error_mark_node;
11337 else if (qualifying_scope
c8094d83 11338 || (TREE_CODE (unqualified_name)
1d786913 11339 != IDENTIFIER_NODE))
712becab
NS
11340 {
11341 cp_parser_error (parser, "expected unqualified-id");
1d786913 11342 unqualified_name = error_mark_node;
712becab
NS
11343 }
11344 }
21526606 11345
1d786913 11346 if (unqualified_name == error_mark_node)
058b15c1
MM
11347 {
11348 declarator = cp_error_declarator;
11349 break;
11350 }
21526606 11351
1d786913
MM
11352 if (qualifying_scope && at_namespace_scope_p ()
11353 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
62b8a44e 11354 {
62b8a44e 11355 /* In the declaration of a member of a template class
0cbd7506
MS
11356 outside of the class itself, the SCOPE will sometimes
11357 be a TYPENAME_TYPE. For example, given:
21526606 11358
0cbd7506
MS
11359 template <typename T>
11360 int S<T>::R::i = 3;
21526606 11361
0cbd7506
MS
11362 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11363 this context, we must resolve S<T>::R to an ordinary
11364 type, rather than a typename type.
21526606 11365
0cbd7506
MS
11366 The reason we normally avoid resolving TYPENAME_TYPEs
11367 is that a specialization of `S' might render
11368 `S<T>::R' not a type. However, if `S' is
11369 specialized, then this `i' will not be used, so there
11370 is no harm in resolving the types here. */
1d786913 11371 tree type;
c8094d83 11372
1d786913
MM
11373 /* Resolve the TYPENAME_TYPE. */
11374 type = resolve_typename_type (qualifying_scope,
11375 /*only_current_p=*/false);
11376 /* If that failed, the declarator is invalid. */
11377 if (type == error_mark_node)
11378 error ("%<%T::%D%> is not a type",
11379 TYPE_CONTEXT (qualifying_scope),
11380 TYPE_IDENTIFIER (qualifying_scope));
11381 qualifying_scope = type;
62b8a44e 11382 }
21526606 11383
c8094d83 11384 declarator = make_id_declarator (qualifying_scope,
1d786913 11385 unqualified_name);
b68b6828 11386 declarator->id_loc = token->location;
1d786913 11387 if (unqualified_name)
a723baf1 11388 {
62b8a44e
NS
11389 tree class_type;
11390
1d786913
MM
11391 if (qualifying_scope
11392 && CLASS_TYPE_P (qualifying_scope))
11393 class_type = qualifying_scope;
62b8a44e 11394 else
1d786913 11395 class_type = current_class_type;
62b8a44e 11396
058b15c1
MM
11397 if (class_type)
11398 {
11399 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11400 declarator->u.id.sfk = sfk_destructor;
11401 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11402 declarator->u.id.sfk = sfk_conversion;
27d6592c
MM
11403 else if (/* There's no way to declare a constructor
11404 for an anonymous type, even if the type
11405 got a name for linkage purposes. */
11406 !TYPE_WAS_ANONYMOUS (class_type)
11407 && (constructor_name_p (unqualified_name,
11408 class_type)
11409 || (TREE_CODE (unqualified_name) == TYPE_DECL
c8094d83 11410 && (same_type_p
27d6592c
MM
11411 (TREE_TYPE (unqualified_name),
11412 class_type)))))
058b15c1
MM
11413 declarator->u.id.sfk = sfk_constructor;
11414
11415 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11416 *ctor_dtor_or_conv_p = -1;
1d786913 11417 if (qualifying_scope
98ca843c 11418 && TREE_CODE (unqualified_name) == TYPE_DECL
058b15c1
MM
11419 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11420 {
11421 error ("invalid use of constructor as a template");
2a13a625 11422 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
0cbd7506
MS
11423 "the constructor in a qualified name",
11424 class_type,
058b15c1
MM
11425 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11426 class_type, class_type);
11427 }
11428 }
a723baf1 11429 }
62b8a44e
NS
11430
11431 handle_declarator:;
11432 scope = get_scope_of_declarator (declarator);
11433 if (scope)
91b004e5
MM
11434 /* Any names that appear after the declarator-id for a
11435 member are looked up in the containing scope. */
4514aa8c 11436 pushed_scope = push_scope (scope);
62b8a44e
NS
11437 parser->in_declarator_p = true;
11438 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
058b15c1 11439 || (declarator && declarator->kind == cdk_id))
62b8a44e
NS
11440 /* Default args are only allowed on function
11441 declarations. */
11442 parser->default_arg_ok_p = saved_default_arg_ok_p;
a723baf1 11443 else
62b8a44e
NS
11444 parser->default_arg_ok_p = false;
11445
11446 first = false;
a723baf1 11447 }
62b8a44e 11448 /* We're done. */
a723baf1
MM
11449 else
11450 break;
a723baf1
MM
11451 }
11452
11453 /* For an abstract declarator, we might wind up with nothing at this
11454 point. That's an error; the declarator is not optional. */
11455 if (!declarator)
11456 cp_parser_error (parser, "expected declarator");
11457
11458 /* If we entered a scope, we must exit it now. */
4514aa8c
NS
11459 if (pushed_scope)
11460 pop_scope (pushed_scope);
a723baf1
MM
11461
11462 parser->default_arg_ok_p = saved_default_arg_ok_p;
11463 parser->in_declarator_p = saved_in_declarator_p;
21526606 11464
a723baf1
MM
11465 return declarator;
11466}
11467
21526606 11468/* Parse a ptr-operator.
a723baf1
MM
11469
11470 ptr-operator:
11471 * cv-qualifier-seq [opt]
11472 &
11473 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11474
11475 GNU Extension:
11476
11477 ptr-operator:
11478 & cv-qualifier-seq [opt]
11479
3c01e5df
MM
11480 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11481 Returns ADDR_EXPR if a reference was used. In the case of a
11482 pointer-to-member, *TYPE is filled in with the TYPE containing the
11483 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11484 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11485 ERROR_MARK if an error occurred. */
21526606 11486
a723baf1 11487static enum tree_code
21526606 11488cp_parser_ptr_operator (cp_parser* parser,
0cbd7506 11489 tree* type,
3c01e5df 11490 cp_cv_quals *cv_quals)
a723baf1
MM
11491{
11492 enum tree_code code = ERROR_MARK;
11493 cp_token *token;
11494
11495 /* Assume that it's not a pointer-to-member. */
11496 *type = NULL_TREE;
11497 /* And that there are no cv-qualifiers. */
3c01e5df 11498 *cv_quals = TYPE_UNQUALIFIED;
a723baf1
MM
11499
11500 /* Peek at the next token. */
11501 token = cp_lexer_peek_token (parser->lexer);
11502 /* If it's a `*' or `&' we have a pointer or reference. */
11503 if (token->type == CPP_MULT || token->type == CPP_AND)
11504 {
11505 /* Remember which ptr-operator we were processing. */
11506 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11507
11508 /* Consume the `*' or `&'. */
11509 cp_lexer_consume_token (parser->lexer);
11510
11511 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11512 `&', if we are allowing GNU extensions. (The only qualifier
11513 that can legally appear after `&' is `restrict', but that is
11514 enforced during semantic analysis. */
21526606 11515 if (code == INDIRECT_REF
a723baf1 11516 || cp_parser_allow_gnu_extensions_p (parser))
3c01e5df 11517 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
a723baf1
MM
11518 }
11519 else
11520 {
11521 /* Try the pointer-to-member case. */
11522 cp_parser_parse_tentatively (parser);
11523 /* Look for the optional `::' operator. */
11524 cp_parser_global_scope_opt (parser,
11525 /*current_scope_valid_p=*/false);
11526 /* Look for the nested-name specifier. */
11527 cp_parser_nested_name_specifier (parser,
11528 /*typename_keyword_p=*/false,
11529 /*check_dependency_p=*/true,
a668c6ad
MM
11530 /*type_p=*/false,
11531 /*is_declaration=*/false);
a723baf1
MM
11532 /* If we found it, and the next token is a `*', then we are
11533 indeed looking at a pointer-to-member operator. */
11534 if (!cp_parser_error_occurred (parser)
11535 && cp_parser_require (parser, CPP_MULT, "`*'"))
11536 {
11537 /* The type of which the member is a member is given by the
11538 current SCOPE. */
11539 *type = parser->scope;
11540 /* The next name will not be qualified. */
11541 parser->scope = NULL_TREE;
11542 parser->qualifying_scope = NULL_TREE;
11543 parser->object_scope = NULL_TREE;
11544 /* Indicate that the `*' operator was used. */
11545 code = INDIRECT_REF;
11546 /* Look for the optional cv-qualifier-seq. */
3c01e5df 11547 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
a723baf1
MM
11548 }
11549 /* If that didn't work we don't have a ptr-operator. */
11550 if (!cp_parser_parse_definitely (parser))
11551 cp_parser_error (parser, "expected ptr-operator");
11552 }
11553
11554 return code;
11555}
11556
11557/* Parse an (optional) cv-qualifier-seq.
11558
11559 cv-qualifier-seq:
21526606 11560 cv-qualifier cv-qualifier-seq [opt]
a723baf1 11561
a723baf1
MM
11562 cv-qualifier:
11563 const
21526606 11564 volatile
a723baf1
MM
11565
11566 GNU Extension:
11567
11568 cv-qualifier:
98ca843c 11569 __restrict__
a723baf1 11570
3c01e5df
MM
11571 Returns a bitmask representing the cv-qualifiers. */
11572
11573static cp_cv_quals
11574cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
a723baf1 11575{
3c01e5df 11576 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
a723baf1 11577
3c01e5df 11578 while (true)
a723baf1 11579 {
3c01e5df
MM
11580 cp_token *token;
11581 cp_cv_quals cv_qualifier;
98ca843c 11582
3c01e5df
MM
11583 /* Peek at the next token. */
11584 token = cp_lexer_peek_token (parser->lexer);
11585 /* See if it's a cv-qualifier. */
11586 switch (token->keyword)
11587 {
11588 case RID_CONST:
11589 cv_qualifier = TYPE_QUAL_CONST;
11590 break;
98ca843c 11591
3c01e5df
MM
11592 case RID_VOLATILE:
11593 cv_qualifier = TYPE_QUAL_VOLATILE;
11594 break;
98ca843c 11595
3c01e5df
MM
11596 case RID_RESTRICT:
11597 cv_qualifier = TYPE_QUAL_RESTRICT;
11598 break;
98ca843c 11599
3c01e5df
MM
11600 default:
11601 cv_qualifier = TYPE_UNQUALIFIED;
11602 break;
11603 }
98ca843c 11604
3c01e5df
MM
11605 if (!cv_qualifier)
11606 break;
a723baf1 11607
3c01e5df
MM
11608 if (cv_quals & cv_qualifier)
11609 {
11610 error ("duplicate cv-qualifier");
11611 cp_lexer_purge_token (parser->lexer);
11612 }
11613 else
11614 {
11615 cp_lexer_consume_token (parser->lexer);
11616 cv_quals |= cv_qualifier;
11617 }
a723baf1
MM
11618 }
11619
3c01e5df 11620 return cv_quals;
a723baf1
MM
11621}
11622
11623/* Parse a declarator-id.
11624
11625 declarator-id:
11626 id-expression
21526606 11627 :: [opt] nested-name-specifier [opt] type-name
a723baf1
MM
11628
11629 In the `id-expression' case, the value returned is as for
11630 cp_parser_id_expression if the id-expression was an unqualified-id.
11631 If the id-expression was a qualified-id, then a SCOPE_REF is
11632 returned. The first operand is the scope (either a NAMESPACE_DECL
11633 or TREE_TYPE), but the second is still just a representation of an
11634 unqualified-id. */
11635
11636static tree
94edc4ab 11637cp_parser_declarator_id (cp_parser* parser)
a723baf1 11638{
a723baf1
MM
11639 /* The expression must be an id-expression. Assume that qualified
11640 names are the names of types so that:
11641
11642 template <class T>
11643 int S<T>::R::i = 3;
11644
11645 will work; we must treat `S<T>::R' as the name of a type.
11646 Similarly, assume that qualified names are templates, where
11647 required, so that:
11648
11649 template <class T>
11650 int S<T>::R<T>::i = 3;
11651
11652 will work, too. */
1d786913
MM
11653 return cp_parser_id_expression (parser,
11654 /*template_keyword_p=*/false,
11655 /*check_dependency_p=*/false,
11656 /*template_p=*/NULL,
11657 /*declarator_p=*/true);
a723baf1
MM
11658}
11659
11660/* Parse a type-id.
11661
11662 type-id:
11663 type-specifier-seq abstract-declarator [opt]
11664
11665 Returns the TYPE specified. */
11666
11667static tree
94edc4ab 11668cp_parser_type_id (cp_parser* parser)
a723baf1 11669{
62d1db17 11670 cp_decl_specifier_seq type_specifier_seq;
058b15c1 11671 cp_declarator *abstract_declarator;
a723baf1
MM
11672
11673 /* Parse the type-specifier-seq. */
d4113656
MM
11674 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11675 &type_specifier_seq);
62d1db17 11676 if (type_specifier_seq.type == error_mark_node)
a723baf1
MM
11677 return error_mark_node;
11678
11679 /* There might or might not be an abstract declarator. */
11680 cp_parser_parse_tentatively (parser);
11681 /* Look for the declarator. */
21526606 11682 abstract_declarator
4bb8ca28 11683 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
db86dd14
MM
11684 /*parenthesized_p=*/NULL,
11685 /*member_p=*/false);
a723baf1
MM
11686 /* Check to see if there really was a declarator. */
11687 if (!cp_parser_parse_definitely (parser))
058b15c1 11688 abstract_declarator = NULL;
a723baf1 11689
62d1db17 11690 return groktypename (&type_specifier_seq, abstract_declarator);
a723baf1
MM
11691}
11692
11693/* Parse a type-specifier-seq.
11694
11695 type-specifier-seq:
11696 type-specifier type-specifier-seq [opt]
11697
11698 GNU extension:
11699
11700 type-specifier-seq:
11701 attributes type-specifier-seq [opt]
11702
d4113656
MM
11703 If IS_CONDITION is true, we are at the start of a "condition",
11704 e.g., we've just seen "if (".
11705
62d1db17 11706 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
a723baf1 11707
62d1db17
MM
11708static void
11709cp_parser_type_specifier_seq (cp_parser* parser,
d4113656 11710 bool is_condition,
62d1db17 11711 cp_decl_specifier_seq *type_specifier_seq)
a723baf1
MM
11712{
11713 bool seen_type_specifier = false;
d4113656 11714 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
62d1db17
MM
11715
11716 /* Clear the TYPE_SPECIFIER_SEQ. */
11717 clear_decl_specs (type_specifier_seq);
a723baf1
MM
11718
11719 /* Parse the type-specifiers and attributes. */
11720 while (true)
11721 {
11722 tree type_specifier;
d4113656 11723 bool is_cv_qualifier;
a723baf1
MM
11724
11725 /* Check for attributes first. */
11726 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11727 {
98ca843c 11728 type_specifier_seq->attributes =
62d1db17
MM
11729 chainon (type_specifier_seq->attributes,
11730 cp_parser_attributes_opt (parser));
a723baf1
MM
11731 continue;
11732 }
11733
a723baf1 11734 /* Look for the type-specifier. */
21526606 11735 type_specifier = cp_parser_type_specifier (parser,
d4113656 11736 flags,
62d1db17 11737 type_specifier_seq,
a723baf1
MM
11738 /*is_declaration=*/false,
11739 NULL,
d4113656
MM
11740 &is_cv_qualifier);
11741 if (!type_specifier)
62d1db17 11742 {
d4113656
MM
11743 /* If the first type-specifier could not be found, this is not a
11744 type-specifier-seq at all. */
11745 if (!seen_type_specifier)
11746 {
11747 cp_parser_error (parser, "expected type-specifier");
11748 type_specifier_seq->type = error_mark_node;
11749 return;
11750 }
11751 /* If subsequent type-specifiers could not be found, the
11752 type-specifier-seq is complete. */
11753 break;
62d1db17 11754 }
a723baf1 11755
a723baf1 11756 seen_type_specifier = true;
d4113656
MM
11757 /* The standard says that a condition can be:
11758
0cbd7506 11759 type-specifier-seq declarator = assignment-expression
c8094d83 11760
d4113656
MM
11761 However, given:
11762
11763 struct S {};
11764 if (int S = ...)
11765
0cbd7506
MS
11766 we should treat the "S" as a declarator, not as a
11767 type-specifier. The standard doesn't say that explicitly for
11768 type-specifier-seq, but it does say that for
11769 decl-specifier-seq in an ordinary declaration. Perhaps it
11770 would be clearer just to allow a decl-specifier-seq here, and
11771 then add a semantic restriction that if any decl-specifiers
11772 that are not type-specifiers appear, the program is invalid. */
d4113656 11773 if (is_condition && !is_cv_qualifier)
c8094d83 11774 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
a723baf1
MM
11775 }
11776
62d1db17 11777 return;
a723baf1
MM
11778}
11779
11780/* Parse a parameter-declaration-clause.
11781
11782 parameter-declaration-clause:
11783 parameter-declaration-list [opt] ... [opt]
11784 parameter-declaration-list , ...
11785
058b15c1
MM
11786 Returns a representation for the parameter declarations. A return
11787 value of NULL indicates a parameter-declaration-clause consisting
11788 only of an ellipsis. */
a723baf1 11789
058b15c1 11790static cp_parameter_declarator *
94edc4ab 11791cp_parser_parameter_declaration_clause (cp_parser* parser)
a723baf1 11792{
058b15c1 11793 cp_parameter_declarator *parameters;
a723baf1
MM
11794 cp_token *token;
11795 bool ellipsis_p;
058b15c1 11796 bool is_error;
a723baf1
MM
11797
11798 /* Peek at the next token. */
11799 token = cp_lexer_peek_token (parser->lexer);
11800 /* Check for trivial parameter-declaration-clauses. */
11801 if (token->type == CPP_ELLIPSIS)
11802 {
11803 /* Consume the `...' token. */
11804 cp_lexer_consume_token (parser->lexer);
058b15c1 11805 return NULL;
a723baf1
MM
11806 }
11807 else if (token->type == CPP_CLOSE_PAREN)
11808 /* There are no parameters. */
c73aecdf
DE
11809 {
11810#ifndef NO_IMPLICIT_EXTERN_C
11811 if (in_system_header && current_class_type == NULL
11812 && current_lang_name == lang_name_c)
058b15c1 11813 return NULL;
c73aecdf
DE
11814 else
11815#endif
058b15c1 11816 return no_parameters;
c73aecdf 11817 }
a723baf1
MM
11818 /* Check for `(void)', too, which is a special case. */
11819 else if (token->keyword == RID_VOID
21526606 11820 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1
MM
11821 == CPP_CLOSE_PAREN))
11822 {
11823 /* Consume the `void' token. */
11824 cp_lexer_consume_token (parser->lexer);
11825 /* There are no parameters. */
058b15c1 11826 return no_parameters;
a723baf1 11827 }
21526606 11828
a723baf1 11829 /* Parse the parameter-declaration-list. */
058b15c1 11830 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
a723baf1
MM
11831 /* If a parse error occurred while parsing the
11832 parameter-declaration-list, then the entire
11833 parameter-declaration-clause is erroneous. */
058b15c1
MM
11834 if (is_error)
11835 return NULL;
a723baf1
MM
11836
11837 /* Peek at the next token. */
11838 token = cp_lexer_peek_token (parser->lexer);
11839 /* If it's a `,', the clause should terminate with an ellipsis. */
11840 if (token->type == CPP_COMMA)
11841 {
11842 /* Consume the `,'. */
11843 cp_lexer_consume_token (parser->lexer);
11844 /* Expect an ellipsis. */
21526606 11845 ellipsis_p
a723baf1
MM
11846 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11847 }
21526606 11848 /* It might also be `...' if the optional trailing `,' was
a723baf1
MM
11849 omitted. */
11850 else if (token->type == CPP_ELLIPSIS)
11851 {
11852 /* Consume the `...' token. */
11853 cp_lexer_consume_token (parser->lexer);
11854 /* And remember that we saw it. */
11855 ellipsis_p = true;
11856 }
11857 else
11858 ellipsis_p = false;
11859
11860 /* Finish the parameter list. */
058b15c1
MM
11861 if (parameters && ellipsis_p)
11862 parameters->ellipsis_p = true;
98ca843c 11863
058b15c1 11864 return parameters;
a723baf1
MM
11865}
11866
11867/* Parse a parameter-declaration-list.
11868
11869 parameter-declaration-list:
11870 parameter-declaration
11871 parameter-declaration-list , parameter-declaration
11872
11873 Returns a representation of the parameter-declaration-list, as for
11874 cp_parser_parameter_declaration_clause. However, the
058b15c1
MM
11875 `void_list_node' is never appended to the list. Upon return,
11876 *IS_ERROR will be true iff an error occurred. */
a723baf1 11877
058b15c1
MM
11878static cp_parameter_declarator *
11879cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
a723baf1 11880{
058b15c1
MM
11881 cp_parameter_declarator *parameters = NULL;
11882 cp_parameter_declarator **tail = &parameters;
11883
11884 /* Assume all will go well. */
11885 *is_error = false;
a723baf1
MM
11886
11887 /* Look for more parameters. */
11888 while (true)
11889 {
058b15c1 11890 cp_parameter_declarator *parameter;
4bb8ca28 11891 bool parenthesized_p;
a723baf1 11892 /* Parse the parameter. */
21526606
EC
11893 parameter
11894 = cp_parser_parameter_declaration (parser,
4bb8ca28
MM
11895 /*template_parm_p=*/false,
11896 &parenthesized_p);
ec194454 11897
34cd5ae7 11898 /* If a parse error occurred parsing the parameter declaration,
a723baf1 11899 then the entire parameter-declaration-list is erroneous. */
058b15c1 11900 if (!parameter)
a723baf1 11901 {
058b15c1
MM
11902 *is_error = true;
11903 parameters = NULL;
a723baf1
MM
11904 break;
11905 }
11906 /* Add the new parameter to the list. */
058b15c1
MM
11907 *tail = parameter;
11908 tail = &parameter->next;
a723baf1
MM
11909
11910 /* Peek at the next token. */
11911 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
e58a9aa1
ZL
11912 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11913 /* These are for Objective-C++ */
11914 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11915 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
a723baf1
MM
11916 /* The parameter-declaration-list is complete. */
11917 break;
11918 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11919 {
11920 cp_token *token;
11921
11922 /* Peek at the next token. */
11923 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11924 /* If it's an ellipsis, then the list is complete. */
11925 if (token->type == CPP_ELLIPSIS)
11926 break;
11927 /* Otherwise, there must be more parameters. Consume the
11928 `,'. */
11929 cp_lexer_consume_token (parser->lexer);
4bb8ca28
MM
11930 /* When parsing something like:
11931
0cbd7506 11932 int i(float f, double d)
21526606 11933
0cbd7506 11934 we can tell after seeing the declaration for "f" that we
4bb8ca28 11935 are not looking at an initialization of a variable "i",
21526606 11936 but rather at the declaration of a function "i".
4bb8ca28
MM
11937
11938 Due to the fact that the parsing of template arguments
11939 (as specified to a template-id) requires backtracking we
11940 cannot use this technique when inside a template argument
11941 list. */
11942 if (!parser->in_template_argument_list_p
4d5fe289 11943 && !parser->in_type_id_in_expr_p
0b16f8f4 11944 && cp_parser_uncommitted_to_tentative_parse_p (parser)
4bb8ca28
MM
11945 /* However, a parameter-declaration of the form
11946 "foat(f)" (which is a valid declaration of a
11947 parameter "f") can also be interpreted as an
11948 expression (the conversion of "f" to "float"). */
11949 && !parenthesized_p)
11950 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
11951 }
11952 else
11953 {
2a13a625 11954 cp_parser_error (parser, "expected %<,%> or %<...%>");
0b16f8f4 11955 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21526606 11956 cp_parser_skip_to_closing_parenthesis (parser,
4bb8ca28 11957 /*recovering=*/true,
5c832178 11958 /*or_comma=*/false,
4bb8ca28 11959 /*consume_paren=*/false);
a723baf1
MM
11960 break;
11961 }
11962 }
11963
058b15c1 11964 return parameters;
a723baf1
MM
11965}
11966
11967/* Parse a parameter declaration.
11968
11969 parameter-declaration:
11970 decl-specifier-seq declarator
11971 decl-specifier-seq declarator = assignment-expression
11972 decl-specifier-seq abstract-declarator [opt]
11973 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11974
ec194454
MM
11975 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11976 declares a template parameter. (In that case, a non-nested `>'
11977 token encountered during the parsing of the assignment-expression
11978 is not interpreted as a greater-than operator.)
a723baf1 11979
058b15c1
MM
11980 Returns a representation of the parameter, or NULL if an error
11981 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11982 true iff the declarator is of the form "(p)". */
a723baf1 11983
058b15c1 11984static cp_parameter_declarator *
21526606 11985cp_parser_parameter_declaration (cp_parser *parser,
4bb8ca28
MM
11986 bool template_parm_p,
11987 bool *parenthesized_p)
a723baf1 11988{
560ad596 11989 int declares_class_or_enum;
ec194454 11990 bool greater_than_is_operator_p;
62d1db17 11991 cp_decl_specifier_seq decl_specifiers;
058b15c1 11992 cp_declarator *declarator;
a723baf1 11993 tree default_argument;
a723baf1
MM
11994 cp_token *token;
11995 const char *saved_message;
11996
ec194454
MM
11997 /* In a template parameter, `>' is not an operator.
11998
11999 [temp.param]
12000
12001 When parsing a default template-argument for a non-type
12002 template-parameter, the first non-nested `>' is taken as the end
12003 of the template parameter-list rather than a greater-than
12004 operator. */
12005 greater_than_is_operator_p = !template_parm_p;
12006
a723baf1
MM
12007 /* Type definitions may not appear in parameter types. */
12008 saved_message = parser->type_definition_forbidden_message;
21526606 12009 parser->type_definition_forbidden_message
a723baf1
MM
12010 = "types may not be defined in parameter types";
12011
12012 /* Parse the declaration-specifiers. */
62d1db17
MM
12013 cp_parser_decl_specifier_seq (parser,
12014 CP_PARSER_FLAGS_NONE,
12015 &decl_specifiers,
12016 &declares_class_or_enum);
a723baf1
MM
12017 /* If an error occurred, there's no reason to attempt to parse the
12018 rest of the declaration. */
12019 if (cp_parser_error_occurred (parser))
12020 {
12021 parser->type_definition_forbidden_message = saved_message;
058b15c1 12022 return NULL;
a723baf1
MM
12023 }
12024
12025 /* Peek at the next token. */
12026 token = cp_lexer_peek_token (parser->lexer);
12027 /* If the next token is a `)', `,', `=', `>', or `...', then there
12028 is no declarator. */
21526606 12029 if (token->type == CPP_CLOSE_PAREN
a723baf1
MM
12030 || token->type == CPP_COMMA
12031 || token->type == CPP_EQ
12032 || token->type == CPP_ELLIPSIS
12033 || token->type == CPP_GREATER)
4bb8ca28 12034 {
058b15c1 12035 declarator = NULL;
4bb8ca28
MM
12036 if (parenthesized_p)
12037 *parenthesized_p = false;
12038 }
a723baf1
MM
12039 /* Otherwise, there should be a declarator. */
12040 else
12041 {
12042 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12043 parser->default_arg_ok_p = false;
21526606 12044
5c832178
MM
12045 /* After seeing a decl-specifier-seq, if the next token is not a
12046 "(", there is no possibility that the code is a valid
4f8163b1
MM
12047 expression. Therefore, if parsing tentatively, we commit at
12048 this point. */
5c832178 12049 if (!parser->in_template_argument_list_p
643aee72 12050 /* In an expression context, having seen:
4f8163b1 12051
a7324e75 12052 (int((char ...
4f8163b1
MM
12053
12054 we cannot be sure whether we are looking at a
a7324e75
MM
12055 function-type (taking a "char" as a parameter) or a cast
12056 of some object of type "char" to "int". */
4f8163b1 12057 && !parser->in_type_id_in_expr_p
0b16f8f4 12058 && cp_parser_uncommitted_to_tentative_parse_p (parser)
5c832178
MM
12059 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12060 cp_parser_commit_to_tentative_parse (parser);
12061 /* Parse the declarator. */
a723baf1 12062 declarator = cp_parser_declarator (parser,
62b8a44e 12063 CP_PARSER_DECLARATOR_EITHER,
4bb8ca28 12064 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
12065 parenthesized_p,
12066 /*member_p=*/false);
a723baf1 12067 parser->default_arg_ok_p = saved_default_arg_ok_p;
4971227d 12068 /* After the declarator, allow more attributes. */
62d1db17 12069 decl_specifiers.attributes
98ca843c 12070 = chainon (decl_specifiers.attributes,
62d1db17 12071 cp_parser_attributes_opt (parser));
a723baf1
MM
12072 }
12073
62b8a44e 12074 /* The restriction on defining new types applies only to the type
a723baf1
MM
12075 of the parameter, not to the default argument. */
12076 parser->type_definition_forbidden_message = saved_message;
12077
12078 /* If the next token is `=', then process a default argument. */
12079 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12080 {
12081 bool saved_greater_than_is_operator_p;
12082 /* Consume the `='. */
12083 cp_lexer_consume_token (parser->lexer);
12084
12085 /* If we are defining a class, then the tokens that make up the
12086 default argument must be saved and processed later. */
21526606 12087 if (!template_parm_p && at_class_scope_p ()
ec194454 12088 && TYPE_BEING_DEFINED (current_class_type))
a723baf1
MM
12089 {
12090 unsigned depth = 0;
c162c75e
MA
12091 cp_token *first_token;
12092 cp_token *token;
a723baf1
MM
12093
12094 /* Add tokens until we have processed the entire default
03fd3f84 12095 argument. We add the range [first_token, token). */
c162c75e 12096 first_token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
12097 while (true)
12098 {
12099 bool done = false;
a723baf1
MM
12100
12101 /* Peek at the next token. */
12102 token = cp_lexer_peek_token (parser->lexer);
12103 /* What we do depends on what token we have. */
12104 switch (token->type)
12105 {
12106 /* In valid code, a default argument must be
12107 immediately followed by a `,' `)', or `...'. */
12108 case CPP_COMMA:
12109 case CPP_CLOSE_PAREN:
12110 case CPP_ELLIPSIS:
12111 /* If we run into a non-nested `;', `}', or `]',
12112 then the code is invalid -- but the default
12113 argument is certainly over. */
12114 case CPP_SEMICOLON:
12115 case CPP_CLOSE_BRACE:
12116 case CPP_CLOSE_SQUARE:
12117 if (depth == 0)
12118 done = true;
12119 /* Update DEPTH, if necessary. */
12120 else if (token->type == CPP_CLOSE_PAREN
12121 || token->type == CPP_CLOSE_BRACE
12122 || token->type == CPP_CLOSE_SQUARE)
12123 --depth;
12124 break;
12125
12126 case CPP_OPEN_PAREN:
12127 case CPP_OPEN_SQUARE:
12128 case CPP_OPEN_BRACE:
12129 ++depth;
12130 break;
12131
12132 case CPP_GREATER:
12133 /* If we see a non-nested `>', and `>' is not an
12134 operator, then it marks the end of the default
12135 argument. */
12136 if (!depth && !greater_than_is_operator_p)
12137 done = true;
12138 break;
12139
12140 /* If we run out of tokens, issue an error message. */
12141 case CPP_EOF:
12142 error ("file ends in default argument");
12143 done = true;
12144 break;
12145
12146 case CPP_NAME:
12147 case CPP_SCOPE:
12148 /* In these cases, we should look for template-ids.
21526606 12149 For example, if the default argument is
a723baf1
MM
12150 `X<int, double>()', we need to do name lookup to
12151 figure out whether or not `X' is a template; if
34cd5ae7 12152 so, the `,' does not end the default argument.
a723baf1
MM
12153
12154 That is not yet done. */
12155 break;
12156
12157 default:
12158 break;
12159 }
12160
12161 /* If we've reached the end, stop. */
12162 if (done)
12163 break;
21526606 12164
a723baf1
MM
12165 /* Add the token to the token block. */
12166 token = cp_lexer_consume_token (parser->lexer);
a723baf1 12167 }
c162c75e
MA
12168
12169 /* Create a DEFAULT_ARG to represented the unparsed default
0cbd7506 12170 argument. */
c162c75e
MA
12171 default_argument = make_node (DEFAULT_ARG);
12172 DEFARG_TOKENS (default_argument)
01ea1ea8
NS
12173 = cp_token_cache_new (first_token, token);
12174 DEFARG_INSTANTIATIONS (default_argument) = NULL;
a723baf1
MM
12175 }
12176 /* Outside of a class definition, we can just parse the
0cbd7506 12177 assignment-expression. */
a723baf1
MM
12178 else
12179 {
12180 bool saved_local_variables_forbidden_p;
12181
12182 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12183 set correctly. */
21526606 12184 saved_greater_than_is_operator_p
a723baf1
MM
12185 = parser->greater_than_is_operator_p;
12186 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12187 /* Local variable names (and the `this' keyword) may not
12188 appear in a default argument. */
21526606 12189 saved_local_variables_forbidden_p
a723baf1
MM
12190 = parser->local_variables_forbidden_p;
12191 parser->local_variables_forbidden_p = true;
12192 /* Parse the assignment-expression. */
c8094d83 12193 default_argument
93678513 12194 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
a723baf1 12195 /* Restore saved state. */
21526606 12196 parser->greater_than_is_operator_p
a723baf1 12197 = saved_greater_than_is_operator_p;
21526606
EC
12198 parser->local_variables_forbidden_p
12199 = saved_local_variables_forbidden_p;
a723baf1
MM
12200 }
12201 if (!parser->default_arg_ok_p)
12202 {
c67d36d0 12203 if (!flag_pedantic_errors)
d4ee4d25 12204 warning (0, "deprecated use of default argument for parameter of non-function");
c67d36d0
NS
12205 else
12206 {
12207 error ("default arguments are only permitted for function parameters");
12208 default_argument = NULL_TREE;
12209 }
a723baf1
MM
12210 }
12211 }
12212 else
12213 default_argument = NULL_TREE;
21526606 12214
62d1db17 12215 return make_parameter_declarator (&decl_specifiers,
058b15c1
MM
12216 declarator,
12217 default_argument);
a723baf1
MM
12218}
12219
a723baf1
MM
12220/* Parse a function-body.
12221
12222 function-body:
12223 compound_statement */
12224
12225static void
12226cp_parser_function_body (cp_parser *parser)
12227{
325c3691 12228 cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
12229}
12230
12231/* Parse a ctor-initializer-opt followed by a function-body. Return
12232 true if a ctor-initializer was present. */
12233
12234static bool
12235cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12236{
12237 tree body;
12238 bool ctor_initializer_p;
12239
12240 /* Begin the function body. */
12241 body = begin_function_body ();
12242 /* Parse the optional ctor-initializer. */
12243 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12244 /* Parse the function-body. */
12245 cp_parser_function_body (parser);
12246 /* Finish the function body. */
12247 finish_function_body (body);
12248
12249 return ctor_initializer_p;
12250}
12251
12252/* Parse an initializer.
12253
12254 initializer:
12255 = initializer-clause
21526606 12256 ( expression-list )
a723baf1 12257
c72a1a86 12258 Returns an expression representing the initializer. If no
21526606 12259 initializer is present, NULL_TREE is returned.
a723baf1
MM
12260
12261 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12262 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
39703eb9
MM
12263 set to FALSE if there is no initializer present. If there is an
12264 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12265 is set to true; otherwise it is set to false. */
a723baf1
MM
12266
12267static tree
39703eb9
MM
12268cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12269 bool* non_constant_p)
a723baf1
MM
12270{
12271 cp_token *token;
12272 tree init;
12273
12274 /* Peek at the next token. */
12275 token = cp_lexer_peek_token (parser->lexer);
12276
12277 /* Let our caller know whether or not this initializer was
12278 parenthesized. */
12279 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
39703eb9
MM
12280 /* Assume that the initializer is constant. */
12281 *non_constant_p = false;
a723baf1
MM
12282
12283 if (token->type == CPP_EQ)
12284 {
12285 /* Consume the `='. */
12286 cp_lexer_consume_token (parser->lexer);
12287 /* Parse the initializer-clause. */
39703eb9 12288 init = cp_parser_initializer_clause (parser, non_constant_p);
a723baf1
MM
12289 }
12290 else if (token->type == CPP_OPEN_PAREN)
39703eb9 12291 init = cp_parser_parenthesized_expression_list (parser, false,
93678513 12292 /*cast_p=*/false,
39703eb9 12293 non_constant_p);
a723baf1
MM
12294 else
12295 {
12296 /* Anything else is an error. */
12297 cp_parser_error (parser, "expected initializer");
12298 init = error_mark_node;
12299 }
12300
12301 return init;
12302}
12303
21526606 12304/* Parse an initializer-clause.
a723baf1
MM
12305
12306 initializer-clause:
12307 assignment-expression
12308 { initializer-list , [opt] }
12309 { }
12310
21526606 12311 Returns an expression representing the initializer.
a723baf1
MM
12312
12313 If the `assignment-expression' production is used the value
21526606 12314 returned is simply a representation for the expression.
a723baf1
MM
12315
12316 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
4038c495 12317 the elements of the initializer-list (or NULL, if the last
a723baf1
MM
12318 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12319 NULL_TREE. There is no way to detect whether or not the optional
39703eb9
MM
12320 trailing `,' was provided. NON_CONSTANT_P is as for
12321 cp_parser_initializer. */
a723baf1
MM
12322
12323static tree
39703eb9 12324cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
12325{
12326 tree initializer;
12327
b2802a4b
R
12328 /* Assume the expression is constant. */
12329 *non_constant_p = false;
12330
a723baf1
MM
12331 /* If it is not a `{', then we are looking at an
12332 assignment-expression. */
12333 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
0da99d4e 12334 {
98ca843c 12335 initializer
0da99d4e
GB
12336 = cp_parser_constant_expression (parser,
12337 /*allow_non_constant_p=*/true,
12338 non_constant_p);
12339 if (!*non_constant_p)
12340 initializer = fold_non_dependent_expr (initializer);
12341 }
a723baf1
MM
12342 else
12343 {
12344 /* Consume the `{' token. */
12345 cp_lexer_consume_token (parser->lexer);
12346 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12347 initializer = make_node (CONSTRUCTOR);
a723baf1
MM
12348 /* If it's not a `}', then there is a non-trivial initializer. */
12349 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12350 {
12351 /* Parse the initializer list. */
12352 CONSTRUCTOR_ELTS (initializer)
39703eb9 12353 = cp_parser_initializer_list (parser, non_constant_p);
a723baf1
MM
12354 /* A trailing `,' token is allowed. */
12355 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12356 cp_lexer_consume_token (parser->lexer);
12357 }
a723baf1
MM
12358 /* Now, there should be a trailing `}'. */
12359 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12360 }
12361
12362 return initializer;
12363}
12364
12365/* Parse an initializer-list.
12366
12367 initializer-list:
12368 initializer-clause
12369 initializer-list , initializer-clause
12370
12371 GNU Extension:
21526606 12372
a723baf1
MM
12373 initializer-list:
12374 identifier : initializer-clause
12375 initializer-list, identifier : initializer-clause
12376
4038c495
GB
12377 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12378 for the initializer. If the INDEX of the elt is non-NULL, it is the
39703eb9
MM
12379 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12380 as for cp_parser_initializer. */
a723baf1 12381
4038c495 12382static VEC(constructor_elt,gc) *
39703eb9 12383cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
a723baf1 12384{
4038c495 12385 VEC(constructor_elt,gc) *v = NULL;
a723baf1 12386
39703eb9
MM
12387 /* Assume all of the expressions are constant. */
12388 *non_constant_p = false;
12389
a723baf1
MM
12390 /* Parse the rest of the list. */
12391 while (true)
12392 {
12393 cp_token *token;
12394 tree identifier;
12395 tree initializer;
39703eb9
MM
12396 bool clause_non_constant_p;
12397
a723baf1
MM
12398 /* If the next token is an identifier and the following one is a
12399 colon, we are looking at the GNU designated-initializer
12400 syntax. */
12401 if (cp_parser_allow_gnu_extensions_p (parser)
12402 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12403 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12404 {
12405 /* Consume the identifier. */
12406 identifier = cp_lexer_consume_token (parser->lexer)->value;
12407 /* Consume the `:'. */
12408 cp_lexer_consume_token (parser->lexer);
12409 }
12410 else
12411 identifier = NULL_TREE;
12412
12413 /* Parse the initializer. */
21526606 12414 initializer = cp_parser_initializer_clause (parser,
39703eb9
MM
12415 &clause_non_constant_p);
12416 /* If any clause is non-constant, so is the entire initializer. */
12417 if (clause_non_constant_p)
12418 *non_constant_p = true;
4038c495
GB
12419
12420 /* Add it to the vector. */
12421 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
a723baf1
MM
12422
12423 /* If the next token is not a comma, we have reached the end of
12424 the list. */
12425 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12426 break;
12427
12428 /* Peek at the next token. */
12429 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12430 /* If the next token is a `}', then we're still done. An
12431 initializer-clause can have a trailing `,' after the
12432 initializer-list and before the closing `}'. */
12433 if (token->type == CPP_CLOSE_BRACE)
12434 break;
12435
12436 /* Consume the `,' token. */
12437 cp_lexer_consume_token (parser->lexer);
12438 }
12439
4038c495 12440 return v;
a723baf1
MM
12441}
12442
12443/* Classes [gram.class] */
12444
12445/* Parse a class-name.
12446
12447 class-name:
12448 identifier
12449 template-id
12450
12451 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12452 to indicate that names looked up in dependent types should be
12453 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12454 keyword has been used to indicate that the name that appears next
fc6a28d7
MM
12455 is a template. TAG_TYPE indicates the explicit tag given before
12456 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12457 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12458 is the class being defined in a class-head.
a723baf1
MM
12459
12460 Returns the TYPE_DECL representing the class. */
12461
12462static tree
21526606
EC
12463cp_parser_class_name (cp_parser *parser,
12464 bool typename_keyword_p,
12465 bool template_keyword_p,
fc6a28d7 12466 enum tag_types tag_type,
a723baf1 12467 bool check_dependency_p,
a668c6ad
MM
12468 bool class_head_p,
12469 bool is_declaration)
a723baf1
MM
12470{
12471 tree decl;
12472 tree scope;
12473 bool typename_p;
e5976695
MM
12474 cp_token *token;
12475
12476 /* All class-names start with an identifier. */
12477 token = cp_lexer_peek_token (parser->lexer);
12478 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12479 {
12480 cp_parser_error (parser, "expected class-name");
12481 return error_mark_node;
12482 }
21526606 12483
a723baf1
MM
12484 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12485 to a template-id, so we save it here. */
12486 scope = parser->scope;
3adee96c
KL
12487 if (scope == error_mark_node)
12488 return error_mark_node;
21526606 12489
a723baf1
MM
12490 /* Any name names a type if we're following the `typename' keyword
12491 in a qualified name where the enclosing scope is type-dependent. */
12492 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
1fb3244a 12493 && dependent_type_p (scope));
e5976695
MM
12494 /* Handle the common case (an identifier, but not a template-id)
12495 efficiently. */
21526606 12496 if (token->type == CPP_NAME
f4abade9 12497 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
a723baf1 12498 {
a723baf1
MM
12499 tree identifier;
12500
12501 /* Look for the identifier. */
12502 identifier = cp_parser_identifier (parser);
12503 /* If the next token isn't an identifier, we are certainly not
12504 looking at a class-name. */
12505 if (identifier == error_mark_node)
12506 decl = error_mark_node;
12507 /* If we know this is a type-name, there's no need to look it
12508 up. */
12509 else if (typename_p)
12510 decl = identifier;
12511 else
12512 {
12513 /* If the next token is a `::', then the name must be a type
12514 name.
12515
12516 [basic.lookup.qual]
12517
12518 During the lookup for a name preceding the :: scope
12519 resolution operator, object, function, and enumerator
12520 names are ignored. */
12521 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
fc6a28d7 12522 tag_type = typename_type;
a723baf1 12523 /* Look up the name. */
21526606 12524 decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 12525 tag_type,
b0bc6e8e 12526 /*is_template=*/false,
eea9800f 12527 /*is_namespace=*/false,
8f78f01f
MM
12528 check_dependency_p,
12529 /*ambiguous_p=*/NULL);
a723baf1
MM
12530 }
12531 }
e5976695
MM
12532 else
12533 {
12534 /* Try a template-id. */
12535 decl = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
12536 check_dependency_p,
12537 is_declaration);
e5976695
MM
12538 if (decl == error_mark_node)
12539 return error_mark_node;
12540 }
a723baf1
MM
12541
12542 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12543
12544 /* If this is a typename, create a TYPENAME_TYPE. */
12545 if (typename_p && decl != error_mark_node)
4bfb8bba 12546 {
fc6a28d7 12547 decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
4bfb8bba
MM
12548 if (decl != error_mark_node)
12549 decl = TYPE_NAME (decl);
12550 }
a723baf1
MM
12551
12552 /* Check to see that it is really the name of a class. */
21526606 12553 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
a723baf1
MM
12554 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12555 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12556 /* Situations like this:
12557
12558 template <typename T> struct A {
21526606 12559 typename T::template X<int>::I i;
a723baf1
MM
12560 };
12561
12562 are problematic. Is `T::template X<int>' a class-name? The
12563 standard does not seem to be definitive, but there is no other
12564 valid interpretation of the following `::'. Therefore, those
12565 names are considered class-names. */
fc6a28d7 12566 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
a723baf1
MM
12567 else if (decl == error_mark_node
12568 || TREE_CODE (decl) != TYPE_DECL
07c65e00 12569 || TREE_TYPE (decl) == error_mark_node
a723baf1
MM
12570 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12571 {
12572 cp_parser_error (parser, "expected class-name");
12573 return error_mark_node;
12574 }
12575
12576 return decl;
12577}
12578
12579/* Parse a class-specifier.
12580
12581 class-specifier:
12582 class-head { member-specification [opt] }
12583
12584 Returns the TREE_TYPE representing the class. */
12585
12586static tree
94edc4ab 12587cp_parser_class_specifier (cp_parser* parser)
a723baf1
MM
12588{
12589 cp_token *token;
12590 tree type;
6de9cd9a 12591 tree attributes = NULL_TREE;
a723baf1
MM
12592 int has_trailing_semicolon;
12593 bool nested_name_specifier_p;
a723baf1 12594 unsigned saved_num_template_parameter_lists;
87c465f5 12595 tree old_scope = NULL_TREE;
2436b51f 12596 tree scope = NULL_TREE;
a723baf1 12597
8d241e0b 12598 push_deferring_access_checks (dk_no_deferred);
cf22909c 12599
a723baf1
MM
12600 /* Parse the class-head. */
12601 type = cp_parser_class_head (parser,
38b305d0
JM
12602 &nested_name_specifier_p,
12603 &attributes);
a723baf1
MM
12604 /* If the class-head was a semantic disaster, skip the entire body
12605 of the class. */
12606 if (!type)
12607 {
12608 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 12609 pop_deferring_access_checks ();
a723baf1
MM
12610 return error_mark_node;
12611 }
cf22909c 12612
a723baf1
MM
12613 /* Look for the `{'. */
12614 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
cf22909c
KL
12615 {
12616 pop_deferring_access_checks ();
12617 return error_mark_node;
12618 }
12619
a723baf1
MM
12620 /* Issue an error message if type-definitions are forbidden here. */
12621 cp_parser_check_type_definition (parser);
12622 /* Remember that we are defining one more class. */
12623 ++parser->num_classes_being_defined;
12624 /* Inside the class, surrounding template-parameter-lists do not
12625 apply. */
21526606
EC
12626 saved_num_template_parameter_lists
12627 = parser->num_template_parameter_lists;
a723baf1 12628 parser->num_template_parameter_lists = 0;
78757caa 12629
a723baf1 12630 /* Start the class. */
eeb23c11 12631 if (nested_name_specifier_p)
2436b51f
MM
12632 {
12633 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
87c465f5 12634 old_scope = push_inner_scope (scope);
2436b51f 12635 }
a723baf1 12636 type = begin_class_definition (type);
98ca843c 12637
a723baf1 12638 if (type == error_mark_node)
9bcb9aae 12639 /* If the type is erroneous, skip the entire body of the class. */
a723baf1
MM
12640 cp_parser_skip_to_closing_brace (parser);
12641 else
12642 /* Parse the member-specification. */
12643 cp_parser_member_specification_opt (parser);
98ca843c 12644
a723baf1
MM
12645 /* Look for the trailing `}'. */
12646 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12647 /* We get better error messages by noticing a common problem: a
12648 missing trailing `;'. */
12649 token = cp_lexer_peek_token (parser->lexer);
12650 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
38b305d0 12651 /* Look for trailing attributes to apply to this class. */
a723baf1 12652 if (cp_parser_allow_gnu_extensions_p (parser))
560ad596 12653 {
38b305d0
JM
12654 tree sub_attr = cp_parser_attributes_opt (parser);
12655 attributes = chainon (attributes, sub_attr);
560ad596 12656 }
38b305d0
JM
12657 if (type != error_mark_node)
12658 type = finish_struct (type, attributes);
87c465f5
KL
12659 if (nested_name_specifier_p)
12660 pop_inner_scope (old_scope, scope);
a723baf1
MM
12661 /* If this class is not itself within the scope of another class,
12662 then we need to parse the bodies of all of the queued function
12663 definitions. Note that the queued functions defined in a class
12664 are not always processed immediately following the
12665 class-specifier for that class. Consider:
12666
12667 struct A {
0cbd7506 12668 struct B { void f() { sizeof (A); } };
a723baf1
MM
12669 };
12670
12671 If `f' were processed before the processing of `A' were
12672 completed, there would be no way to compute the size of `A'.
12673 Note that the nesting we are interested in here is lexical --
12674 not the semantic nesting given by TYPE_CONTEXT. In particular,
12675 for:
12676
12677 struct A { struct B; };
12678 struct A::B { void f() { } };
12679
12680 there is no need to delay the parsing of `A::B::f'. */
21526606 12681 if (--parser->num_classes_being_defined == 0)
a723baf1 12682 {
8218bd34
MM
12683 tree queue_entry;
12684 tree fn;
4514aa8c
NS
12685 tree class_type = NULL_TREE;
12686 tree pushed_scope = NULL_TREE;
da611058 12687
8218bd34
MM
12688 /* In a first pass, parse default arguments to the functions.
12689 Then, in a second pass, parse the bodies of the functions.
12690 This two-phased approach handles cases like:
21526606
EC
12691
12692 struct S {
0cbd7506
MS
12693 void f() { g(); }
12694 void g(int i = 3);
12695 };
8218bd34 12696
0cbd7506 12697 */
8db1028e
NS
12698 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12699 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12700 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12701 TREE_PURPOSE (parser->unparsed_functions_queues)
12702 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
8218bd34
MM
12703 {
12704 fn = TREE_VALUE (queue_entry);
8218bd34
MM
12705 /* If there are default arguments that have not yet been processed,
12706 take care of them now. */
f44b0c8e
MM
12707 if (class_type != TREE_PURPOSE (queue_entry))
12708 {
4514aa8c
NS
12709 if (pushed_scope)
12710 pop_scope (pushed_scope);
f44b0c8e 12711 class_type = TREE_PURPOSE (queue_entry);
4514aa8c 12712 pushed_scope = push_scope (class_type);
f44b0c8e
MM
12713 }
12714 /* Make sure that any template parameters are in scope. */
12715 maybe_begin_member_template_processing (fn);
12716 /* Parse the default argument expressions. */
8218bd34
MM
12717 cp_parser_late_parsing_default_args (parser, fn);
12718 /* Remove any template parameters from the symbol table. */
12719 maybe_end_member_template_processing ();
12720 }
4514aa8c
NS
12721 if (pushed_scope)
12722 pop_scope (pushed_scope);
8218bd34 12723 /* Now parse the body of the functions. */
8db1028e
NS
12724 for (TREE_VALUE (parser->unparsed_functions_queues)
12725 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12726 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12727 TREE_VALUE (parser->unparsed_functions_queues)
12728 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
a723baf1 12729 {
a723baf1 12730 /* Figure out which function we need to process. */
a723baf1 12731 fn = TREE_VALUE (queue_entry);
a723baf1
MM
12732 /* Parse the function. */
12733 cp_parser_late_parsing_for_member (parser, fn);
a723baf1 12734 }
a723baf1
MM
12735 }
12736
12737 /* Put back any saved access checks. */
cf22909c 12738 pop_deferring_access_checks ();
a723baf1
MM
12739
12740 /* Restore the count of active template-parameter-lists. */
12741 parser->num_template_parameter_lists
12742 = saved_num_template_parameter_lists;
12743
12744 return type;
12745}
12746
12747/* Parse a class-head.
12748
12749 class-head:
12750 class-key identifier [opt] base-clause [opt]
12751 class-key nested-name-specifier identifier base-clause [opt]
21526606
EC
12752 class-key nested-name-specifier [opt] template-id
12753 base-clause [opt]
a723baf1
MM
12754
12755 GNU Extensions:
12756 class-key attributes identifier [opt] base-clause [opt]
12757 class-key attributes nested-name-specifier identifier base-clause [opt]
21526606
EC
12758 class-key attributes nested-name-specifier [opt] template-id
12759 base-clause [opt]
a723baf1
MM
12760
12761 Returns the TYPE of the indicated class. Sets
12762 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12763 involving a nested-name-specifier was used, and FALSE otherwise.
a723baf1 12764
55dcbc12 12765 Returns error_mark_node if this is not a class-head.
c8094d83 12766
a723baf1
MM
12767 Returns NULL_TREE if the class-head is syntactically valid, but
12768 semantically invalid in a way that means we should skip the entire
12769 body of the class. */
12770
12771static tree
21526606 12772cp_parser_class_head (cp_parser* parser,
38b305d0
JM
12773 bool* nested_name_specifier_p,
12774 tree *attributes_p)
a723baf1 12775{
a723baf1
MM
12776 tree nested_name_specifier;
12777 enum tag_types class_key;
12778 tree id = NULL_TREE;
12779 tree type = NULL_TREE;
12780 tree attributes;
12781 bool template_id_p = false;
12782 bool qualified_p = false;
12783 bool invalid_nested_name_p = false;
afb0918a 12784 bool invalid_explicit_specialization_p = false;
4514aa8c 12785 tree pushed_scope = NULL_TREE;
a723baf1 12786 unsigned num_templates;
cad7e87b 12787 tree bases;
a723baf1
MM
12788
12789 /* Assume no nested-name-specifier will be present. */
12790 *nested_name_specifier_p = false;
12791 /* Assume no template parameter lists will be used in defining the
12792 type. */
12793 num_templates = 0;
12794
12795 /* Look for the class-key. */
12796 class_key = cp_parser_class_key (parser);
12797 if (class_key == none_type)
12798 return error_mark_node;
12799
12800 /* Parse the attributes. */
12801 attributes = cp_parser_attributes_opt (parser);
12802
12803 /* If the next token is `::', that is invalid -- but sometimes
12804 people do try to write:
12805
21526606 12806 struct ::S {};
a723baf1
MM
12807
12808 Handle this gracefully by accepting the extra qualifier, and then
12809 issuing an error about it later if this really is a
2050a1bb 12810 class-head. If it turns out just to be an elaborated type
a723baf1
MM
12811 specifier, remain silent. */
12812 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12813 qualified_p = true;
12814
8d241e0b
KL
12815 push_deferring_access_checks (dk_no_check);
12816
a723baf1
MM
12817 /* Determine the name of the class. Begin by looking for an
12818 optional nested-name-specifier. */
21526606 12819 nested_name_specifier
a723baf1
MM
12820 = cp_parser_nested_name_specifier_opt (parser,
12821 /*typename_keyword_p=*/false,
66d418e6 12822 /*check_dependency_p=*/false,
a668c6ad
MM
12823 /*type_p=*/false,
12824 /*is_declaration=*/false);
a723baf1
MM
12825 /* If there was a nested-name-specifier, then there *must* be an
12826 identifier. */
12827 if (nested_name_specifier)
12828 {
12829 /* Although the grammar says `identifier', it really means
12830 `class-name' or `template-name'. You are only allowed to
12831 define a class that has already been declared with this
21526606 12832 syntax.
a723baf1
MM
12833
12834 The proposed resolution for Core Issue 180 says that whever
12835 you see `class T::X' you should treat `X' as a type-name.
21526606 12836
a723baf1 12837 It is OK to define an inaccessible class; for example:
21526606 12838
0cbd7506
MS
12839 class A { class B; };
12840 class A::B {};
21526606 12841
0cbd7506 12842 We do not know if we will see a class-name, or a
a723baf1
MM
12843 template-name. We look for a class-name first, in case the
12844 class-name is a template-id; if we looked for the
12845 template-name first we would stop after the template-name. */
12846 cp_parser_parse_tentatively (parser);
12847 type = cp_parser_class_name (parser,
12848 /*typename_keyword_p=*/false,
12849 /*template_keyword_p=*/false,
fc6a28d7 12850 class_type,
a723baf1 12851 /*check_dependency_p=*/false,
a668c6ad
MM
12852 /*class_head_p=*/true,
12853 /*is_declaration=*/false);
a723baf1
MM
12854 /* If that didn't work, ignore the nested-name-specifier. */
12855 if (!cp_parser_parse_definitely (parser))
12856 {
12857 invalid_nested_name_p = true;
12858 id = cp_parser_identifier (parser);
12859 if (id == error_mark_node)
12860 id = NULL_TREE;
12861 }
12862 /* If we could not find a corresponding TYPE, treat this
12863 declaration like an unqualified declaration. */
12864 if (type == error_mark_node)
12865 nested_name_specifier = NULL_TREE;
12866 /* Otherwise, count the number of templates used in TYPE and its
12867 containing scopes. */
21526606 12868 else
a723baf1
MM
12869 {
12870 tree scope;
12871
21526606 12872 for (scope = TREE_TYPE (type);
a723baf1 12873 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21526606 12874 scope = (TYPE_P (scope)
a723baf1 12875 ? TYPE_CONTEXT (scope)
21526606
EC
12876 : DECL_CONTEXT (scope)))
12877 if (TYPE_P (scope)
a723baf1
MM
12878 && CLASS_TYPE_P (scope)
12879 && CLASSTYPE_TEMPLATE_INFO (scope)
2050a1bb
MM
12880 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12881 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
a723baf1
MM
12882 ++num_templates;
12883 }
12884 }
12885 /* Otherwise, the identifier is optional. */
12886 else
12887 {
12888 /* We don't know whether what comes next is a template-id,
12889 an identifier, or nothing at all. */
12890 cp_parser_parse_tentatively (parser);
12891 /* Check for a template-id. */
21526606 12892 id = cp_parser_template_id (parser,
a723baf1 12893 /*template_keyword_p=*/false,
a668c6ad
MM
12894 /*check_dependency_p=*/true,
12895 /*is_declaration=*/true);
a723baf1
MM
12896 /* If that didn't work, it could still be an identifier. */
12897 if (!cp_parser_parse_definitely (parser))
12898 {
12899 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12900 id = cp_parser_identifier (parser);
12901 else
12902 id = NULL_TREE;
12903 }
12904 else
12905 {
12906 template_id_p = true;
12907 ++num_templates;
12908 }
12909 }
12910
8d241e0b
KL
12911 pop_deferring_access_checks ();
12912
15077df5
MM
12913 if (id)
12914 cp_parser_check_for_invalid_template_id (parser, id);
ee43dab5 12915
a723baf1
MM
12916 /* If it's not a `:' or a `{' then we can't really be looking at a
12917 class-head, since a class-head only appears as part of a
12918 class-specifier. We have to detect this situation before calling
12919 xref_tag, since that has irreversible side-effects. */
12920 if (!cp_parser_next_token_starts_class_definition_p (parser))
12921 {
2a13a625 12922 cp_parser_error (parser, "expected %<{%> or %<:%>");
a723baf1
MM
12923 return error_mark_node;
12924 }
12925
12926 /* At this point, we're going ahead with the class-specifier, even
12927 if some other problem occurs. */
12928 cp_parser_commit_to_tentative_parse (parser);
12929 /* Issue the error about the overly-qualified name now. */
12930 if (qualified_p)
12931 cp_parser_error (parser,
12932 "global qualification of class name is invalid");
12933 else if (invalid_nested_name_p)
12934 cp_parser_error (parser,
12935 "qualified name does not name a class");
88081599
MM
12936 else if (nested_name_specifier)
12937 {
12938 tree scope;
9bf0e588
VR
12939
12940 /* Reject typedef-names in class heads. */
12941 if (!DECL_IMPLICIT_TYPEDEF_P (type))
12942 {
12943 error ("invalid class name in declaration of %qD", type);
12944 type = NULL_TREE;
12945 goto done;
12946 }
12947
88081599
MM
12948 /* Figure out in what scope the declaration is being placed. */
12949 scope = current_scope ();
88081599
MM
12950 /* If that scope does not contain the scope in which the
12951 class was originally declared, the program is invalid. */
12952 if (scope && !is_ancestor (scope, nested_name_specifier))
12953 {
2a13a625 12954 error ("declaration of %qD in %qD which does not enclose %qD",
0cbd7506 12955 type, scope, nested_name_specifier);
88081599
MM
12956 type = NULL_TREE;
12957 goto done;
12958 }
12959 /* [dcl.meaning]
12960
0cbd7506 12961 A declarator-id shall not be qualified exception of the
88081599
MM
12962 definition of a ... nested class outside of its class
12963 ... [or] a the definition or explicit instantiation of a
12964 class member of a namespace outside of its namespace. */
12965 if (scope == nested_name_specifier)
12966 {
12967 pedwarn ("extra qualification ignored");
12968 nested_name_specifier = NULL_TREE;
12969 num_templates = 0;
12970 }
12971 }
afb0918a
MM
12972 /* An explicit-specialization must be preceded by "template <>". If
12973 it is not, try to recover gracefully. */
21526606 12974 if (at_namespace_scope_p ()
afb0918a 12975 && parser->num_template_parameter_lists == 0
eeb23c11 12976 && template_id_p)
afb0918a 12977 {
2a13a625 12978 error ("an explicit specialization must be preceded by %<template <>%>");
afb0918a
MM
12979 invalid_explicit_specialization_p = true;
12980 /* Take the same action that would have been taken by
12981 cp_parser_explicit_specialization. */
12982 ++parser->num_template_parameter_lists;
12983 begin_specialization ();
12984 }
12985 /* There must be no "return" statements between this point and the
12986 end of this function; set "type "to the correct return value and
12987 use "goto done;" to return. */
a723baf1
MM
12988 /* Make sure that the right number of template parameters were
12989 present. */
12990 if (!cp_parser_check_template_parameters (parser, num_templates))
afb0918a
MM
12991 {
12992 /* If something went wrong, there is no point in even trying to
12993 process the class-definition. */
12994 type = NULL_TREE;
12995 goto done;
12996 }
a723baf1 12997
a723baf1
MM
12998 /* Look up the type. */
12999 if (template_id_p)
13000 {
13001 type = TREE_TYPE (id);
13002 maybe_process_partial_specialization (type);
4514aa8c
NS
13003 if (nested_name_specifier)
13004 pushed_scope = push_scope (nested_name_specifier);
a723baf1 13005 }
4514aa8c 13006 else if (nested_name_specifier)
a723baf1 13007 {
a723baf1
MM
13008 tree class_type;
13009
13010 /* Given:
13011
13012 template <typename T> struct S { struct T };
14d22dd6 13013 template <typename T> struct S<T>::T { };
a723baf1
MM
13014
13015 we will get a TYPENAME_TYPE when processing the definition of
13016 `S::T'. We need to resolve it to the actual type before we
13017 try to define it. */
13018 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13019 {
14d22dd6
MM
13020 class_type = resolve_typename_type (TREE_TYPE (type),
13021 /*only_current_p=*/false);
13022 if (class_type != error_mark_node)
13023 type = TYPE_NAME (class_type);
13024 else
13025 {
13026 cp_parser_error (parser, "could not resolve typename type");
13027 type = error_mark_node;
13028 }
a723baf1
MM
13029 }
13030
560ad596
MM
13031 maybe_process_partial_specialization (TREE_TYPE (type));
13032 class_type = current_class_type;
13033 /* Enter the scope indicated by the nested-name-specifier. */
4514aa8c 13034 pushed_scope = push_scope (nested_name_specifier);
560ad596
MM
13035 /* Get the canonical version of this type. */
13036 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13037 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13038 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
55dcbc12
NS
13039 {
13040 type = push_template_decl (type);
13041 if (type == error_mark_node)
13042 {
13043 type = NULL_TREE;
13044 goto done;
13045 }
13046 }
c8094d83 13047
560ad596 13048 type = TREE_TYPE (type);
4514aa8c 13049 *nested_name_specifier_p = true;
a723baf1 13050 }
4514aa8c
NS
13051 else /* The name is not a nested name. */
13052 {
13053 /* If the class was unnamed, create a dummy name. */
13054 if (!id)
13055 id = make_anon_name ();
13056 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13057 parser->num_template_parameter_lists);
13058 }
13059
a723baf1
MM
13060 /* Indicate whether this class was declared as a `class' or as a
13061 `struct'. */
13062 if (TREE_CODE (type) == RECORD_TYPE)
13063 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13064 cp_parser_check_class_key (class_key, type);
13065
744b12b6
MM
13066 /* If this type was already complete, and we see another definition,
13067 that's an error. */
13068 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13069 {
13070 error ("redefinition of %q#T", type);
dee15844 13071 error ("previous definition of %q+#T", type);
0f3744f8
VR
13072 type = NULL_TREE;
13073 goto done;
744b12b6
MM
13074 }
13075
4514aa8c 13076 /* We will have entered the scope containing the class; the names of
744b12b6 13077 base classes should be looked up in that context. For example:
a723baf1
MM
13078
13079 struct A { struct B {}; struct C; };
13080 struct A::C : B {};
13081
13082 is valid. */
cad7e87b 13083 bases = NULL_TREE;
98ca843c 13084
cad7e87b
NS
13085 /* Get the list of base-classes, if there is one. */
13086 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13087 bases = cp_parser_base_clause (parser);
98ca843c 13088
cad7e87b
NS
13089 /* Process the base classes. */
13090 xref_basetypes (type, bases);
a723baf1 13091
4514aa8c 13092 done:
a723baf1
MM
13093 /* Leave the scope given by the nested-name-specifier. We will
13094 enter the class scope itself while processing the members. */
4514aa8c
NS
13095 if (pushed_scope)
13096 pop_scope (pushed_scope);
a723baf1 13097
afb0918a
MM
13098 if (invalid_explicit_specialization_p)
13099 {
13100 end_specialization ();
13101 --parser->num_template_parameter_lists;
13102 }
38b305d0 13103 *attributes_p = attributes;
a723baf1
MM
13104 return type;
13105}
13106
13107/* Parse a class-key.
13108
13109 class-key:
13110 class
13111 struct
13112 union
13113
13114 Returns the kind of class-key specified, or none_type to indicate
13115 error. */
13116
13117static enum tag_types
94edc4ab 13118cp_parser_class_key (cp_parser* parser)
a723baf1
MM
13119{
13120 cp_token *token;
13121 enum tag_types tag_type;
13122
13123 /* Look for the class-key. */
13124 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13125 if (!token)
13126 return none_type;
13127
13128 /* Check to see if the TOKEN is a class-key. */
13129 tag_type = cp_parser_token_is_class_key (token);
13130 if (!tag_type)
13131 cp_parser_error (parser, "expected class-key");
13132 return tag_type;
13133}
13134
13135/* Parse an (optional) member-specification.
13136
13137 member-specification:
13138 member-declaration member-specification [opt]
13139 access-specifier : member-specification [opt] */
13140
13141static void
94edc4ab 13142cp_parser_member_specification_opt (cp_parser* parser)
a723baf1
MM
13143{
13144 while (true)
13145 {
13146 cp_token *token;
13147 enum rid keyword;
13148
13149 /* Peek at the next token. */
13150 token = cp_lexer_peek_token (parser->lexer);
13151 /* If it's a `}', or EOF then we've seen all the members. */
13152 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
13153 break;
13154
13155 /* See if this token is a keyword. */
13156 keyword = token->keyword;
13157 switch (keyword)
13158 {
13159 case RID_PUBLIC:
13160 case RID_PROTECTED:
13161 case RID_PRIVATE:
13162 /* Consume the access-specifier. */
13163 cp_lexer_consume_token (parser->lexer);
13164 /* Remember which access-specifier is active. */
13165 current_access_specifier = token->value;
13166 /* Look for the `:'. */
13167 cp_parser_require (parser, CPP_COLON, "`:'");
13168 break;
13169
13170 default:
de3fe73c
MM
13171 /* Accept #pragmas at class scope. */
13172 if (token->type == CPP_PRAGMA)
13173 {
13174 cp_lexer_handle_pragma (parser->lexer);
13175 break;
13176 }
13177
a723baf1
MM
13178 /* Otherwise, the next construction must be a
13179 member-declaration. */
13180 cp_parser_member_declaration (parser);
a723baf1
MM
13181 }
13182 }
13183}
13184
21526606 13185/* Parse a member-declaration.
a723baf1
MM
13186
13187 member-declaration:
13188 decl-specifier-seq [opt] member-declarator-list [opt] ;
13189 function-definition ; [opt]
13190 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13191 using-declaration
21526606 13192 template-declaration
a723baf1
MM
13193
13194 member-declarator-list:
13195 member-declarator
13196 member-declarator-list , member-declarator
13197
13198 member-declarator:
21526606 13199 declarator pure-specifier [opt]
a723baf1 13200 declarator constant-initializer [opt]
21526606 13201 identifier [opt] : constant-expression
a723baf1
MM
13202
13203 GNU Extensions:
13204
13205 member-declaration:
13206 __extension__ member-declaration
13207
13208 member-declarator:
13209 declarator attributes [opt] pure-specifier [opt]
13210 declarator attributes [opt] constant-initializer [opt]
13211 identifier [opt] attributes [opt] : constant-expression */
13212
13213static void
94edc4ab 13214cp_parser_member_declaration (cp_parser* parser)
a723baf1 13215{
62d1db17 13216 cp_decl_specifier_seq decl_specifiers;
a723baf1
MM
13217 tree prefix_attributes;
13218 tree decl;
560ad596 13219 int declares_class_or_enum;
a723baf1
MM
13220 bool friend_p;
13221 cp_token *token;
13222 int saved_pedantic;
13223
13224 /* Check for the `__extension__' keyword. */
13225 if (cp_parser_extension_opt (parser, &saved_pedantic))
13226 {
13227 /* Recurse. */
13228 cp_parser_member_declaration (parser);
13229 /* Restore the old value of the PEDANTIC flag. */
13230 pedantic = saved_pedantic;
13231
13232 return;
13233 }
13234
13235 /* Check for a template-declaration. */
13236 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13237 {
13238 /* Parse the template-declaration. */
13239 cp_parser_template_declaration (parser, /*member_p=*/true);
13240
13241 return;
13242 }
13243
13244 /* Check for a using-declaration. */
13245 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13246 {
13247 /* Parse the using-declaration. */
13248 cp_parser_using_declaration (parser);
13249
13250 return;
13251 }
21526606 13252
e58a9aa1
ZL
13253 /* Check for @defs. */
13254 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13255 {
13256 tree ivar, member;
13257 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13258 ivar = ivar_chains;
13259 while (ivar)
13260 {
13261 member = ivar;
13262 ivar = TREE_CHAIN (member);
13263 TREE_CHAIN (member) = NULL_TREE;
13264 finish_member_declaration (member);
13265 }
13266 return;
13267 }
13268
a723baf1 13269 /* Parse the decl-specifier-seq. */
62d1db17
MM
13270 cp_parser_decl_specifier_seq (parser,
13271 CP_PARSER_FLAGS_OPTIONAL,
13272 &decl_specifiers,
13273 &declares_class_or_enum);
13274 prefix_attributes = decl_specifiers.attributes;
13275 decl_specifiers.attributes = NULL_TREE;
8fbc5ae7 13276 /* Check for an invalid type-name. */
de3fe73c
MM
13277 if (!decl_specifiers.type
13278 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8fbc5ae7 13279 return;
a723baf1
MM
13280 /* If there is no declarator, then the decl-specifier-seq should
13281 specify a type. */
13282 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13283 {
13284 /* If there was no decl-specifier-seq, and the next token is a
13285 `;', then we have something like:
13286
13287 struct S { ; };
13288
13289 [class.mem]
13290
13291 Each member-declaration shall declare at least one member
13292 name of the class. */
62d1db17 13293 if (!decl_specifiers.any_specifiers_p)
a723baf1 13294 {
2cfe82fe
ZW
13295 cp_token *token = cp_lexer_peek_token (parser->lexer);
13296 if (pedantic && !token->in_system_header)
13297 pedwarn ("%Hextra %<;%>", &token->location);
a723baf1 13298 }
21526606 13299 else
a723baf1
MM
13300 {
13301 tree type;
21526606 13302
a723baf1 13303 /* See if this declaration is a friend. */
62d1db17 13304 friend_p = cp_parser_friend_p (&decl_specifiers);
a723baf1
MM
13305 /* If there were decl-specifiers, check to see if there was
13306 a class-declaration. */
62d1db17 13307 type = check_tag_decl (&decl_specifiers);
a723baf1
MM
13308 /* Nested classes have already been added to the class, but
13309 a `friend' needs to be explicitly registered. */
13310 if (friend_p)
13311 {
13312 /* If the `friend' keyword was present, the friend must
13313 be introduced with a class-key. */
13314 if (!declares_class_or_enum)
13315 error ("a class-key must be used when declaring a friend");
13316 /* In this case:
13317
21526606 13318 template <typename T> struct A {
0cbd7506
MS
13319 friend struct A<T>::B;
13320 };
21526606 13321
a723baf1
MM
13322 A<T>::B will be represented by a TYPENAME_TYPE, and
13323 therefore not recognized by check_tag_decl. */
98ca843c 13324 if (!type
62d1db17
MM
13325 && decl_specifiers.type
13326 && TYPE_P (decl_specifiers.type))
13327 type = decl_specifiers.type;
fdd09134 13328 if (!type || !TYPE_P (type))
a723baf1
MM
13329 error ("friend declaration does not name a class or "
13330 "function");
13331 else
19db77ce
KL
13332 make_friend_class (current_class_type, type,
13333 /*complain=*/true);
a723baf1
MM
13334 }
13335 /* If there is no TYPE, an error message will already have
13336 been issued. */
62d1db17 13337 else if (!type || type == error_mark_node)
a723baf1
MM
13338 ;
13339 /* An anonymous aggregate has to be handled specially; such
13340 a declaration really declares a data member (with a
13341 particular type), as opposed to a nested class. */
13342 else if (ANON_AGGR_TYPE_P (type))
13343 {
13344 /* Remove constructors and such from TYPE, now that we
34cd5ae7 13345 know it is an anonymous aggregate. */
a723baf1
MM
13346 fixup_anonymous_aggr (type);
13347 /* And make the corresponding data member. */
13348 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13349 /* Add it to the class. */
13350 finish_member_declaration (decl);
13351 }
37d407a1
KL
13352 else
13353 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
a723baf1
MM
13354 }
13355 }
13356 else
13357 {
13358 /* See if these declarations will be friends. */
62d1db17 13359 friend_p = cp_parser_friend_p (&decl_specifiers);
a723baf1 13360
21526606 13361 /* Keep going until we hit the `;' at the end of the
a723baf1
MM
13362 declaration. */
13363 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13364 {
13365 tree attributes = NULL_TREE;
13366 tree first_attribute;
13367
13368 /* Peek at the next token. */
13369 token = cp_lexer_peek_token (parser->lexer);
13370
13371 /* Check for a bitfield declaration. */
13372 if (token->type == CPP_COLON
13373 || (token->type == CPP_NAME
21526606 13374 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1
MM
13375 == CPP_COLON))
13376 {
13377 tree identifier;
13378 tree width;
13379
13380 /* Get the name of the bitfield. Note that we cannot just
13381 check TOKEN here because it may have been invalidated by
13382 the call to cp_lexer_peek_nth_token above. */
13383 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13384 identifier = cp_parser_identifier (parser);
13385 else
13386 identifier = NULL_TREE;
13387
13388 /* Consume the `:' token. */
13389 cp_lexer_consume_token (parser->lexer);
13390 /* Get the width of the bitfield. */
21526606 13391 width
14d22dd6
MM
13392 = cp_parser_constant_expression (parser,
13393 /*allow_non_constant=*/false,
13394 NULL);
a723baf1
MM
13395
13396 /* Look for attributes that apply to the bitfield. */
13397 attributes = cp_parser_attributes_opt (parser);
13398 /* Remember which attributes are prefix attributes and
13399 which are not. */
13400 first_attribute = attributes;
13401 /* Combine the attributes. */
13402 attributes = chainon (prefix_attributes, attributes);
13403
13404 /* Create the bitfield declaration. */
98ca843c 13405 decl = grokbitfield (identifier
1d786913
MM
13406 ? make_id_declarator (NULL_TREE,
13407 identifier)
058b15c1 13408 : NULL,
62d1db17 13409 &decl_specifiers,
a723baf1
MM
13410 width);
13411 /* Apply the attributes. */
13412 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13413 }
13414 else
13415 {
058b15c1 13416 cp_declarator *declarator;
a723baf1
MM
13417 tree initializer;
13418 tree asm_specification;
7efa3e22 13419 int ctor_dtor_or_conv_p;
a723baf1
MM
13420
13421 /* Parse the declarator. */
21526606 13422 declarator
62b8a44e 13423 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 13424 &ctor_dtor_or_conv_p,
db86dd14
MM
13425 /*parenthesized_p=*/NULL,
13426 /*member_p=*/true);
a723baf1
MM
13427
13428 /* If something went wrong parsing the declarator, make sure
13429 that we at least consume some tokens. */
058b15c1 13430 if (declarator == cp_error_declarator)
a723baf1
MM
13431 {
13432 /* Skip to the end of the statement. */
13433 cp_parser_skip_to_end_of_statement (parser);
4bb8ca28
MM
13434 /* If the next token is not a semicolon, that is
13435 probably because we just skipped over the body of
13436 a function. So, we consume a semicolon if
13437 present, but do not issue an error message if it
13438 is not present. */
13439 if (cp_lexer_next_token_is (parser->lexer,
13440 CPP_SEMICOLON))
13441 cp_lexer_consume_token (parser->lexer);
13442 return;
a723baf1
MM
13443 }
13444
fc6a28d7
MM
13445 if (declares_class_or_enum & 2)
13446 cp_parser_check_for_definition_in_return_type
13447 (declarator, decl_specifiers.type);
560ad596 13448
a723baf1
MM
13449 /* Look for an asm-specification. */
13450 asm_specification = cp_parser_asm_specification_opt (parser);
13451 /* Look for attributes that apply to the declaration. */
13452 attributes = cp_parser_attributes_opt (parser);
13453 /* Remember which attributes are prefix attributes and
13454 which are not. */
13455 first_attribute = attributes;
13456 /* Combine the attributes. */
13457 attributes = chainon (prefix_attributes, attributes);
13458
13459 /* If it's an `=', then we have a constant-initializer or a
13460 pure-specifier. It is not correct to parse the
13461 initializer before registering the member declaration
13462 since the member declaration should be in scope while
13463 its initializer is processed. However, the rest of the
13464 front end does not yet provide an interface that allows
13465 us to handle this correctly. */
13466 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13467 {
13468 /* In [class.mem]:
13469
13470 A pure-specifier shall be used only in the declaration of
21526606 13471 a virtual function.
a723baf1
MM
13472
13473 A member-declarator can contain a constant-initializer
13474 only if it declares a static member of integral or
21526606 13475 enumeration type.
a723baf1
MM
13476
13477 Therefore, if the DECLARATOR is for a function, we look
13478 for a pure-specifier; otherwise, we look for a
13479 constant-initializer. When we call `grokfield', it will
13480 perform more stringent semantics checks. */
058b15c1 13481 if (declarator->kind == cdk_function)
a723baf1
MM
13482 initializer = cp_parser_pure_specifier (parser);
13483 else
4bb8ca28
MM
13484 /* Parse the initializer. */
13485 initializer = cp_parser_constant_initializer (parser);
a723baf1
MM
13486 }
13487 /* Otherwise, there is no initializer. */
13488 else
13489 initializer = NULL_TREE;
13490
13491 /* See if we are probably looking at a function
5a19910e 13492 definition. We are certainly not looking at a
a723baf1
MM
13493 member-declarator. Calling `grokfield' has
13494 side-effects, so we must not do it unless we are sure
13495 that we are looking at a member-declarator. */
21526606 13496 if (cp_parser_token_starts_function_definition_p
a723baf1 13497 (cp_lexer_peek_token (parser->lexer)))
4bb8ca28
MM
13498 {
13499 /* The grammar does not allow a pure-specifier to be
13500 used when a member function is defined. (It is
13501 possible that this fact is an oversight in the
13502 standard, since a pure function may be defined
13503 outside of the class-specifier. */
13504 if (initializer)
13505 error ("pure-specifier on function-definition");
13506 decl = cp_parser_save_member_function_body (parser,
62d1db17 13507 &decl_specifiers,
4bb8ca28
MM
13508 declarator,
13509 attributes);
13510 /* If the member was not a friend, declare it here. */
13511 if (!friend_p)
13512 finish_member_declaration (decl);
13513 /* Peek at the next token. */
13514 token = cp_lexer_peek_token (parser->lexer);
13515 /* If the next token is a semicolon, consume it. */
13516 if (token->type == CPP_SEMICOLON)
13517 cp_lexer_consume_token (parser->lexer);
13518 return;
13519 }
a723baf1 13520 else
39703eb9
MM
13521 {
13522 /* Create the declaration. */
62d1db17 13523 decl = grokfield (declarator, &decl_specifiers,
ee3071ef 13524 initializer, asm_specification,
39703eb9
MM
13525 attributes);
13526 /* Any initialization must have been from a
13527 constant-expression. */
13528 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13529 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13530 }
a723baf1
MM
13531 }
13532
13533 /* Reset PREFIX_ATTRIBUTES. */
13534 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13535 attributes = TREE_CHAIN (attributes);
13536 if (attributes)
13537 TREE_CHAIN (attributes) = NULL_TREE;
13538
13539 /* If there is any qualification still in effect, clear it
13540 now; we will be starting fresh with the next declarator. */
13541 parser->scope = NULL_TREE;
13542 parser->qualifying_scope = NULL_TREE;
13543 parser->object_scope = NULL_TREE;
13544 /* If it's a `,', then there are more declarators. */
13545 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13546 cp_lexer_consume_token (parser->lexer);
13547 /* If the next token isn't a `;', then we have a parse error. */
13548 else if (cp_lexer_next_token_is_not (parser->lexer,
13549 CPP_SEMICOLON))
13550 {
2a13a625 13551 cp_parser_error (parser, "expected %<;%>");
04c06002 13552 /* Skip tokens until we find a `;'. */
a723baf1
MM
13553 cp_parser_skip_to_end_of_statement (parser);
13554
13555 break;
13556 }
13557
13558 if (decl)
13559 {
13560 /* Add DECL to the list of members. */
13561 if (!friend_p)
13562 finish_member_declaration (decl);
13563
a723baf1 13564 if (TREE_CODE (decl) == FUNCTION_DECL)
8db1028e 13565 cp_parser_save_default_args (parser, decl);
a723baf1
MM
13566 }
13567 }
13568 }
13569
4bb8ca28 13570 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
a723baf1
MM
13571}
13572
13573/* Parse a pure-specifier.
13574
13575 pure-specifier:
13576 = 0
13577
13578 Returns INTEGER_ZERO_NODE if a pure specifier is found.
cd0be382 13579 Otherwise, ERROR_MARK_NODE is returned. */
a723baf1
MM
13580
13581static tree
94edc4ab 13582cp_parser_pure_specifier (cp_parser* parser)
a723baf1
MM
13583{
13584 cp_token *token;
13585
13586 /* Look for the `=' token. */
13587 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13588 return error_mark_node;
13589 /* Look for the `0' token. */
515e6a84
GB
13590 token = cp_lexer_consume_token (parser->lexer);
13591 if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13592 {
13593 cp_parser_error (parser,
13594 "invalid pure specifier (only `= 0' is allowed)");
13595 cp_parser_skip_to_end_of_statement (parser);
13596 return error_mark_node;
13597 }
a723baf1 13598
515e6a84
GB
13599 /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13600 We need to get information from the lexer about how the number
13601 was spelled in order to fix this problem. */
a723baf1
MM
13602 return integer_zero_node;
13603}
13604
13605/* Parse a constant-initializer.
13606
13607 constant-initializer:
13608 = constant-expression
13609
13610 Returns a representation of the constant-expression. */
13611
13612static tree
94edc4ab 13613cp_parser_constant_initializer (cp_parser* parser)
a723baf1
MM
13614{
13615 /* Look for the `=' token. */
13616 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13617 return error_mark_node;
13618
13619 /* It is invalid to write:
13620
13621 struct S { static const int i = { 7 }; };
13622
13623 */
13624 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13625 {
13626 cp_parser_error (parser,
13627 "a brace-enclosed initializer is not allowed here");
13628 /* Consume the opening brace. */
13629 cp_lexer_consume_token (parser->lexer);
13630 /* Skip the initializer. */
13631 cp_parser_skip_to_closing_brace (parser);
13632 /* Look for the trailing `}'. */
13633 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
21526606 13634
a723baf1
MM
13635 return error_mark_node;
13636 }
13637
21526606 13638 return cp_parser_constant_expression (parser,
14d22dd6
MM
13639 /*allow_non_constant=*/false,
13640 NULL);
a723baf1
MM
13641}
13642
13643/* Derived classes [gram.class.derived] */
13644
13645/* Parse a base-clause.
13646
13647 base-clause:
21526606 13648 : base-specifier-list
a723baf1
MM
13649
13650 base-specifier-list:
13651 base-specifier
13652 base-specifier-list , base-specifier
13653
13654 Returns a TREE_LIST representing the base-classes, in the order in
13655 which they were declared. The representation of each node is as
21526606 13656 described by cp_parser_base_specifier.
a723baf1
MM
13657
13658 In the case that no bases are specified, this function will return
13659 NULL_TREE, not ERROR_MARK_NODE. */
13660
13661static tree
94edc4ab 13662cp_parser_base_clause (cp_parser* parser)
a723baf1
MM
13663{
13664 tree bases = NULL_TREE;
13665
13666 /* Look for the `:' that begins the list. */
13667 cp_parser_require (parser, CPP_COLON, "`:'");
13668
13669 /* Scan the base-specifier-list. */
13670 while (true)
13671 {
13672 cp_token *token;
13673 tree base;
13674
13675 /* Look for the base-specifier. */
13676 base = cp_parser_base_specifier (parser);
13677 /* Add BASE to the front of the list. */
13678 if (base != error_mark_node)
13679 {
13680 TREE_CHAIN (base) = bases;
13681 bases = base;
13682 }
13683 /* Peek at the next token. */
13684 token = cp_lexer_peek_token (parser->lexer);
13685 /* If it's not a comma, then the list is complete. */
13686 if (token->type != CPP_COMMA)
13687 break;
13688 /* Consume the `,'. */
13689 cp_lexer_consume_token (parser->lexer);
13690 }
13691
13692 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13693 base class had a qualified name. However, the next name that
13694 appears is certainly not qualified. */
13695 parser->scope = NULL_TREE;
13696 parser->qualifying_scope = NULL_TREE;
13697 parser->object_scope = NULL_TREE;
13698
13699 return nreverse (bases);
13700}
13701
13702/* Parse a base-specifier.
13703
13704 base-specifier:
13705 :: [opt] nested-name-specifier [opt] class-name
13706 virtual access-specifier [opt] :: [opt] nested-name-specifier
13707 [opt] class-name
13708 access-specifier virtual [opt] :: [opt] nested-name-specifier
13709 [opt] class-name
13710
13711 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13712 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13713 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13714 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21526606 13715
a723baf1 13716static tree
94edc4ab 13717cp_parser_base_specifier (cp_parser* parser)
a723baf1
MM
13718{
13719 cp_token *token;
13720 bool done = false;
13721 bool virtual_p = false;
13722 bool duplicate_virtual_error_issued_p = false;
13723 bool duplicate_access_error_issued_p = false;
bbaab916 13724 bool class_scope_p, template_p;
dbbf88d1 13725 tree access = access_default_node;
a723baf1
MM
13726 tree type;
13727
13728 /* Process the optional `virtual' and `access-specifier'. */
13729 while (!done)
13730 {
13731 /* Peek at the next token. */
13732 token = cp_lexer_peek_token (parser->lexer);
13733 /* Process `virtual'. */
13734 switch (token->keyword)
13735 {
13736 case RID_VIRTUAL:
13737 /* If `virtual' appears more than once, issue an error. */
13738 if (virtual_p && !duplicate_virtual_error_issued_p)
13739 {
13740 cp_parser_error (parser,
2a13a625 13741 "%<virtual%> specified more than once in base-specified");
a723baf1
MM
13742 duplicate_virtual_error_issued_p = true;
13743 }
13744
13745 virtual_p = true;
13746
13747 /* Consume the `virtual' token. */
13748 cp_lexer_consume_token (parser->lexer);
13749
13750 break;
13751
13752 case RID_PUBLIC:
13753 case RID_PROTECTED:
13754 case RID_PRIVATE:
13755 /* If more than one access specifier appears, issue an
13756 error. */
dbbf88d1
NS
13757 if (access != access_default_node
13758 && !duplicate_access_error_issued_p)
a723baf1
MM
13759 {
13760 cp_parser_error (parser,
13761 "more than one access specifier in base-specified");
13762 duplicate_access_error_issued_p = true;
13763 }
13764
dbbf88d1 13765 access = ridpointers[(int) token->keyword];
a723baf1
MM
13766
13767 /* Consume the access-specifier. */
13768 cp_lexer_consume_token (parser->lexer);
13769
13770 break;
13771
13772 default:
13773 done = true;
13774 break;
13775 }
13776 }
852dcbdd 13777 /* It is not uncommon to see programs mechanically, erroneously, use
a3a503a5 13778 the 'typename' keyword to denote (dependent) qualified types
1ed53ef3
GB
13779 as base classes. */
13780 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13781 {
13782 if (!processing_template_decl)
2a13a625 13783 error ("keyword %<typename%> not allowed outside of templates");
1ed53ef3 13784 else
2a13a625 13785 error ("keyword %<typename%> not allowed in this context "
1ed53ef3
GB
13786 "(the base class is implicitly a type)");
13787 cp_lexer_consume_token (parser->lexer);
13788 }
a723baf1 13789
a723baf1
MM
13790 /* Look for the optional `::' operator. */
13791 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13792 /* Look for the nested-name-specifier. The simplest way to
13793 implement:
13794
13795 [temp.res]
13796
13797 The keyword `typename' is not permitted in a base-specifier or
13798 mem-initializer; in these contexts a qualified name that
13799 depends on a template-parameter is implicitly assumed to be a
13800 type name.
13801
13802 is to pretend that we have seen the `typename' keyword at this
21526606 13803 point. */
a723baf1
MM
13804 cp_parser_nested_name_specifier_opt (parser,
13805 /*typename_keyword_p=*/true,
13806 /*check_dependency_p=*/true,
fc6a28d7 13807 typename_type,
a668c6ad 13808 /*is_declaration=*/true);
a723baf1
MM
13809 /* If the base class is given by a qualified name, assume that names
13810 we see are type names or templates, as appropriate. */
13811 class_scope_p = (parser->scope && TYPE_P (parser->scope));
bbaab916 13812 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21526606 13813
a723baf1 13814 /* Finally, look for the class-name. */
21526606 13815 type = cp_parser_class_name (parser,
a723baf1 13816 class_scope_p,
bbaab916 13817 template_p,
fc6a28d7 13818 typename_type,
a723baf1 13819 /*check_dependency_p=*/true,
a668c6ad
MM
13820 /*class_head_p=*/false,
13821 /*is_declaration=*/true);
a723baf1
MM
13822
13823 if (type == error_mark_node)
13824 return error_mark_node;
13825
dbbf88d1 13826 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
a723baf1
MM
13827}
13828
13829/* Exception handling [gram.exception] */
13830
13831/* Parse an (optional) exception-specification.
13832
13833 exception-specification:
13834 throw ( type-id-list [opt] )
13835
13836 Returns a TREE_LIST representing the exception-specification. The
13837 TREE_VALUE of each node is a type. */
13838
13839static tree
94edc4ab 13840cp_parser_exception_specification_opt (cp_parser* parser)
a723baf1
MM
13841{
13842 cp_token *token;
13843 tree type_id_list;
13844
13845 /* Peek at the next token. */
13846 token = cp_lexer_peek_token (parser->lexer);
13847 /* If it's not `throw', then there's no exception-specification. */
13848 if (!cp_parser_is_keyword (token, RID_THROW))
13849 return NULL_TREE;
13850
13851 /* Consume the `throw'. */
13852 cp_lexer_consume_token (parser->lexer);
13853
13854 /* Look for the `('. */
13855 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13856
13857 /* Peek at the next token. */
13858 token = cp_lexer_peek_token (parser->lexer);
13859 /* If it's not a `)', then there is a type-id-list. */
13860 if (token->type != CPP_CLOSE_PAREN)
13861 {
13862 const char *saved_message;
13863
13864 /* Types may not be defined in an exception-specification. */
13865 saved_message = parser->type_definition_forbidden_message;
13866 parser->type_definition_forbidden_message
13867 = "types may not be defined in an exception-specification";
13868 /* Parse the type-id-list. */
13869 type_id_list = cp_parser_type_id_list (parser);
13870 /* Restore the saved message. */
13871 parser->type_definition_forbidden_message = saved_message;
13872 }
13873 else
13874 type_id_list = empty_except_spec;
13875
13876 /* Look for the `)'. */
13877 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13878
13879 return type_id_list;
13880}
13881
13882/* Parse an (optional) type-id-list.
13883
13884 type-id-list:
13885 type-id
13886 type-id-list , type-id
13887
13888 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13889 in the order that the types were presented. */
13890
13891static tree
94edc4ab 13892cp_parser_type_id_list (cp_parser* parser)
a723baf1
MM
13893{
13894 tree types = NULL_TREE;
13895
13896 while (true)
13897 {
13898 cp_token *token;
13899 tree type;
13900
13901 /* Get the next type-id. */
13902 type = cp_parser_type_id (parser);
13903 /* Add it to the list. */
13904 types = add_exception_specifier (types, type, /*complain=*/1);
13905 /* Peek at the next token. */
13906 token = cp_lexer_peek_token (parser->lexer);
13907 /* If it is not a `,', we are done. */
13908 if (token->type != CPP_COMMA)
13909 break;
13910 /* Consume the `,'. */
13911 cp_lexer_consume_token (parser->lexer);
13912 }
13913
13914 return nreverse (types);
13915}
13916
13917/* Parse a try-block.
13918
13919 try-block:
13920 try compound-statement handler-seq */
13921
13922static tree
94edc4ab 13923cp_parser_try_block (cp_parser* parser)
a723baf1
MM
13924{
13925 tree try_block;
13926
13927 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13928 try_block = begin_try_block ();
325c3691 13929 cp_parser_compound_statement (parser, NULL, true);
a723baf1
MM
13930 finish_try_block (try_block);
13931 cp_parser_handler_seq (parser);
13932 finish_handler_sequence (try_block);
13933
13934 return try_block;
13935}
13936
13937/* Parse a function-try-block.
13938
13939 function-try-block:
13940 try ctor-initializer [opt] function-body handler-seq */
13941
13942static bool
94edc4ab 13943cp_parser_function_try_block (cp_parser* parser)
a723baf1
MM
13944{
13945 tree try_block;
13946 bool ctor_initializer_p;
13947
13948 /* Look for the `try' keyword. */
13949 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13950 return false;
13951 /* Let the rest of the front-end know where we are. */
13952 try_block = begin_function_try_block ();
13953 /* Parse the function-body. */
21526606 13954 ctor_initializer_p
a723baf1
MM
13955 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13956 /* We're done with the `try' part. */
13957 finish_function_try_block (try_block);
13958 /* Parse the handlers. */
13959 cp_parser_handler_seq (parser);
13960 /* We're done with the handlers. */
13961 finish_function_handler_sequence (try_block);
13962
13963 return ctor_initializer_p;
13964}
13965
13966/* Parse a handler-seq.
13967
13968 handler-seq:
13969 handler handler-seq [opt] */
13970
13971static void
94edc4ab 13972cp_parser_handler_seq (cp_parser* parser)
a723baf1
MM
13973{
13974 while (true)
13975 {
13976 cp_token *token;
13977
13978 /* Parse the handler. */
13979 cp_parser_handler (parser);
13980 /* Peek at the next token. */
13981 token = cp_lexer_peek_token (parser->lexer);
13982 /* If it's not `catch' then there are no more handlers. */
13983 if (!cp_parser_is_keyword (token, RID_CATCH))
13984 break;
13985 }
13986}
13987
13988/* Parse a handler.
13989
13990 handler:
13991 catch ( exception-declaration ) compound-statement */
13992
13993static void
94edc4ab 13994cp_parser_handler (cp_parser* parser)
a723baf1
MM
13995{
13996 tree handler;
13997 tree declaration;
13998
13999 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14000 handler = begin_handler ();
14001 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14002 declaration = cp_parser_exception_declaration (parser);
14003 finish_handler_parms (declaration, handler);
14004 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
325c3691 14005 cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
14006 finish_handler (handler);
14007}
14008
14009/* Parse an exception-declaration.
14010
14011 exception-declaration:
14012 type-specifier-seq declarator
14013 type-specifier-seq abstract-declarator
14014 type-specifier-seq
21526606 14015 ...
a723baf1
MM
14016
14017 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14018 ellipsis variant is used. */
14019
14020static tree
94edc4ab 14021cp_parser_exception_declaration (cp_parser* parser)
a723baf1 14022{
058b15c1 14023 tree decl;
62d1db17 14024 cp_decl_specifier_seq type_specifiers;
058b15c1 14025 cp_declarator *declarator;
a723baf1
MM
14026 const char *saved_message;
14027
14028 /* If it's an ellipsis, it's easy to handle. */
14029 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14030 {
14031 /* Consume the `...' token. */
14032 cp_lexer_consume_token (parser->lexer);
14033 return NULL_TREE;
14034 }
14035
14036 /* Types may not be defined in exception-declarations. */
14037 saved_message = parser->type_definition_forbidden_message;
14038 parser->type_definition_forbidden_message
14039 = "types may not be defined in exception-declarations";
14040
14041 /* Parse the type-specifier-seq. */
d4113656
MM
14042 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14043 &type_specifiers);
a723baf1
MM
14044 /* If it's a `)', then there is no declarator. */
14045 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
058b15c1 14046 declarator = NULL;
a723baf1 14047 else
62b8a44e 14048 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
4bb8ca28 14049 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
14050 /*parenthesized_p=*/NULL,
14051 /*member_p=*/false);
a723baf1
MM
14052
14053 /* Restore the saved message. */
14054 parser->type_definition_forbidden_message = saved_message;
14055
62d1db17 14056 if (type_specifiers.any_specifiers_p)
058b15c1 14057 {
62d1db17 14058 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
058b15c1
MM
14059 if (decl == NULL_TREE)
14060 error ("invalid catch parameter");
14061 }
14062 else
14063 decl = NULL_TREE;
14064
14065 return decl;
a723baf1
MM
14066}
14067
21526606 14068/* Parse a throw-expression.
a723baf1
MM
14069
14070 throw-expression:
34cd5ae7 14071 throw assignment-expression [opt]
a723baf1
MM
14072
14073 Returns a THROW_EXPR representing the throw-expression. */
14074
14075static tree
94edc4ab 14076cp_parser_throw_expression (cp_parser* parser)
a723baf1
MM
14077{
14078 tree expression;
89f1a6ec 14079 cp_token* token;
a723baf1
MM
14080
14081 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
89f1a6ec
MM
14082 token = cp_lexer_peek_token (parser->lexer);
14083 /* Figure out whether or not there is an assignment-expression
14084 following the "throw" keyword. */
14085 if (token->type == CPP_COMMA
14086 || token->type == CPP_SEMICOLON
14087 || token->type == CPP_CLOSE_PAREN
14088 || token->type == CPP_CLOSE_SQUARE
14089 || token->type == CPP_CLOSE_BRACE
14090 || token->type == CPP_COLON)
a723baf1 14091 expression = NULL_TREE;
89f1a6ec 14092 else
93678513
MM
14093 expression = cp_parser_assignment_expression (parser,
14094 /*cast_p=*/false);
a723baf1
MM
14095
14096 return build_throw (expression);
14097}
14098
14099/* GNU Extensions */
14100
14101/* Parse an (optional) asm-specification.
14102
14103 asm-specification:
14104 asm ( string-literal )
14105
14106 If the asm-specification is present, returns a STRING_CST
14107 corresponding to the string-literal. Otherwise, returns
14108 NULL_TREE. */
14109
14110static tree
94edc4ab 14111cp_parser_asm_specification_opt (cp_parser* parser)
a723baf1
MM
14112{
14113 cp_token *token;
14114 tree asm_specification;
14115
14116 /* Peek at the next token. */
14117 token = cp_lexer_peek_token (parser->lexer);
21526606 14118 /* If the next token isn't the `asm' keyword, then there's no
a723baf1
MM
14119 asm-specification. */
14120 if (!cp_parser_is_keyword (token, RID_ASM))
14121 return NULL_TREE;
14122
14123 /* Consume the `asm' token. */
14124 cp_lexer_consume_token (parser->lexer);
14125 /* Look for the `('. */
14126 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14127
14128 /* Look for the string-literal. */
c162c75e 14129 asm_specification = cp_parser_string_literal (parser, false, false);
a723baf1
MM
14130
14131 /* Look for the `)'. */
14132 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14133
14134 return asm_specification;
14135}
14136
21526606 14137/* Parse an asm-operand-list.
a723baf1
MM
14138
14139 asm-operand-list:
14140 asm-operand
14141 asm-operand-list , asm-operand
21526606 14142
a723baf1 14143 asm-operand:
21526606 14144 string-literal ( expression )
a723baf1
MM
14145 [ string-literal ] string-literal ( expression )
14146
14147 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14148 each node is the expression. The TREE_PURPOSE is itself a
14149 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14150 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14151 is a STRING_CST for the string literal before the parenthesis. */
14152
14153static tree
94edc4ab 14154cp_parser_asm_operand_list (cp_parser* parser)
a723baf1
MM
14155{
14156 tree asm_operands = NULL_TREE;
14157
14158 while (true)
14159 {
14160 tree string_literal;
14161 tree expression;
14162 tree name;
21526606 14163
21526606 14164 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
a723baf1
MM
14165 {
14166 /* Consume the `[' token. */
14167 cp_lexer_consume_token (parser->lexer);
14168 /* Read the operand name. */
14169 name = cp_parser_identifier (parser);
21526606 14170 if (name != error_mark_node)
a723baf1
MM
14171 name = build_string (IDENTIFIER_LENGTH (name),
14172 IDENTIFIER_POINTER (name));
14173 /* Look for the closing `]'. */
14174 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14175 }
14176 else
14177 name = NULL_TREE;
14178 /* Look for the string-literal. */
c162c75e
MA
14179 string_literal = cp_parser_string_literal (parser, false, false);
14180
a723baf1
MM
14181 /* Look for the `('. */
14182 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14183 /* Parse the expression. */
93678513 14184 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
14185 /* Look for the `)'. */
14186 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
c162c75e 14187
a723baf1
MM
14188 /* Add this operand to the list. */
14189 asm_operands = tree_cons (build_tree_list (name, string_literal),
21526606 14190 expression,
a723baf1 14191 asm_operands);
21526606 14192 /* If the next token is not a `,', there are no more
a723baf1
MM
14193 operands. */
14194 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14195 break;
14196 /* Consume the `,'. */
14197 cp_lexer_consume_token (parser->lexer);
14198 }
14199
14200 return nreverse (asm_operands);
14201}
14202
21526606 14203/* Parse an asm-clobber-list.
a723baf1
MM
14204
14205 asm-clobber-list:
14206 string-literal
21526606 14207 asm-clobber-list , string-literal
a723baf1
MM
14208
14209 Returns a TREE_LIST, indicating the clobbers in the order that they
14210 appeared. The TREE_VALUE of each node is a STRING_CST. */
14211
14212static tree
94edc4ab 14213cp_parser_asm_clobber_list (cp_parser* parser)
a723baf1
MM
14214{
14215 tree clobbers = NULL_TREE;
14216
14217 while (true)
14218 {
a723baf1
MM
14219 tree string_literal;
14220
14221 /* Look for the string literal. */
c162c75e 14222 string_literal = cp_parser_string_literal (parser, false, false);
a723baf1
MM
14223 /* Add it to the list. */
14224 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21526606 14225 /* If the next token is not a `,', then the list is
a723baf1
MM
14226 complete. */
14227 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14228 break;
14229 /* Consume the `,' token. */
14230 cp_lexer_consume_token (parser->lexer);
14231 }
14232
14233 return clobbers;
14234}
14235
14236/* Parse an (optional) series of attributes.
14237
14238 attributes:
14239 attributes attribute
14240
14241 attribute:
21526606 14242 __attribute__ (( attribute-list [opt] ))
a723baf1
MM
14243
14244 The return value is as for cp_parser_attribute_list. */
21526606 14245
a723baf1 14246static tree
94edc4ab 14247cp_parser_attributes_opt (cp_parser* parser)
a723baf1
MM
14248{
14249 tree attributes = NULL_TREE;
14250
14251 while (true)
14252 {
14253 cp_token *token;
14254 tree attribute_list;
14255
14256 /* Peek at the next token. */
14257 token = cp_lexer_peek_token (parser->lexer);
14258 /* If it's not `__attribute__', then we're done. */
14259 if (token->keyword != RID_ATTRIBUTE)
14260 break;
14261
14262 /* Consume the `__attribute__' keyword. */
14263 cp_lexer_consume_token (parser->lexer);
14264 /* Look for the two `(' tokens. */
14265 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14266 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14267
14268 /* Peek at the next token. */
14269 token = cp_lexer_peek_token (parser->lexer);
14270 if (token->type != CPP_CLOSE_PAREN)
14271 /* Parse the attribute-list. */
14272 attribute_list = cp_parser_attribute_list (parser);
14273 else
14274 /* If the next token is a `)', then there is no attribute
14275 list. */
14276 attribute_list = NULL;
14277
14278 /* Look for the two `)' tokens. */
14279 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14280 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14281
14282 /* Add these new attributes to the list. */
14283 attributes = chainon (attributes, attribute_list);
14284 }
14285
14286 return attributes;
14287}
14288
21526606 14289/* Parse an attribute-list.
a723baf1 14290
21526606
EC
14291 attribute-list:
14292 attribute
a723baf1
MM
14293 attribute-list , attribute
14294
14295 attribute:
21526606 14296 identifier
a723baf1
MM
14297 identifier ( identifier )
14298 identifier ( identifier , expression-list )
21526606 14299 identifier ( expression-list )
a723baf1 14300
88e95ee3
MM
14301 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14302 to an attribute. The TREE_PURPOSE of each node is the identifier
14303 indicating which attribute is in use. The TREE_VALUE represents
14304 the arguments, if any. */
a723baf1
MM
14305
14306static tree
94edc4ab 14307cp_parser_attribute_list (cp_parser* parser)
a723baf1
MM
14308{
14309 tree attribute_list = NULL_TREE;
c162c75e 14310 bool save_translate_strings_p = parser->translate_strings_p;
a723baf1 14311
c162c75e 14312 parser->translate_strings_p = false;
a723baf1
MM
14313 while (true)
14314 {
14315 cp_token *token;
14316 tree identifier;
14317 tree attribute;
14318
14319 /* Look for the identifier. We also allow keywords here; for
14320 example `__attribute__ ((const))' is legal. */
14321 token = cp_lexer_peek_token (parser->lexer);
88e95ee3
MM
14322 if (token->type == CPP_NAME
14323 || token->type == CPP_KEYWORD)
14324 {
14325 /* Consume the token. */
14326 token = cp_lexer_consume_token (parser->lexer);
21526606 14327
88e95ee3 14328 /* Save away the identifier that indicates which attribute
c8094d83 14329 this is. */
88e95ee3
MM
14330 identifier = token->value;
14331 attribute = build_tree_list (identifier, NULL_TREE);
a723baf1 14332
88e95ee3
MM
14333 /* Peek at the next token. */
14334 token = cp_lexer_peek_token (parser->lexer);
14335 /* If it's an `(', then parse the attribute arguments. */
14336 if (token->type == CPP_OPEN_PAREN)
14337 {
14338 tree arguments;
a723baf1 14339
88e95ee3 14340 arguments = (cp_parser_parenthesized_expression_list
c8094d83 14341 (parser, true, /*cast_p=*/false,
88e95ee3
MM
14342 /*non_constant_p=*/NULL));
14343 /* Save the identifier and arguments away. */
14344 TREE_VALUE (attribute) = arguments;
14345 }
a723baf1 14346
88e95ee3
MM
14347 /* Add this attribute to the list. */
14348 TREE_CHAIN (attribute) = attribute_list;
14349 attribute_list = attribute;
a723baf1 14350
88e95ee3
MM
14351 token = cp_lexer_peek_token (parser->lexer);
14352 }
14353 /* Now, look for more attributes. If the next token isn't a
14354 `,', we're done. */
a723baf1
MM
14355 if (token->type != CPP_COMMA)
14356 break;
14357
cd0be382 14358 /* Consume the comma and keep going. */
a723baf1
MM
14359 cp_lexer_consume_token (parser->lexer);
14360 }
c162c75e 14361 parser->translate_strings_p = save_translate_strings_p;
a723baf1
MM
14362
14363 /* We built up the list in reverse order. */
14364 return nreverse (attribute_list);
14365}
14366
14367/* Parse an optional `__extension__' keyword. Returns TRUE if it is
14368 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14369 current value of the PEDANTIC flag, regardless of whether or not
14370 the `__extension__' keyword is present. The caller is responsible
14371 for restoring the value of the PEDANTIC flag. */
14372
14373static bool
94edc4ab 14374cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
a723baf1
MM
14375{
14376 /* Save the old value of the PEDANTIC flag. */
14377 *saved_pedantic = pedantic;
14378
14379 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14380 {
14381 /* Consume the `__extension__' token. */
14382 cp_lexer_consume_token (parser->lexer);
14383 /* We're not being pedantic while the `__extension__' keyword is
14384 in effect. */
14385 pedantic = 0;
14386
14387 return true;
14388 }
14389
14390 return false;
14391}
14392
14393/* Parse a label declaration.
14394
14395 label-declaration:
14396 __label__ label-declarator-seq ;
14397
14398 label-declarator-seq:
14399 identifier , label-declarator-seq
14400 identifier */
14401
14402static void
94edc4ab 14403cp_parser_label_declaration (cp_parser* parser)
a723baf1
MM
14404{
14405 /* Look for the `__label__' keyword. */
14406 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14407
14408 while (true)
14409 {
14410 tree identifier;
14411
14412 /* Look for an identifier. */
14413 identifier = cp_parser_identifier (parser);
cb6d4a9f
VR
14414 /* If we failed, stop. */
14415 if (identifier == error_mark_node)
14416 break;
14417 /* Declare it as a label. */
a723baf1
MM
14418 finish_label_decl (identifier);
14419 /* If the next token is a `;', stop. */
14420 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14421 break;
14422 /* Look for the `,' separating the label declarations. */
14423 cp_parser_require (parser, CPP_COMMA, "`,'");
14424 }
14425
14426 /* Look for the final `;'. */
14427 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14428}
14429
14430/* Support Functions */
14431
14432/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14433 NAME should have one of the representations used for an
14434 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14435 is returned. If PARSER->SCOPE is a dependent type, then a
14436 SCOPE_REF is returned.
14437
14438 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14439 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14440 was formed. Abstractly, such entities should not be passed to this
14441 function, because they do not need to be looked up, but it is
14442 simpler to check for this special case here, rather than at the
14443 call-sites.
14444
14445 In cases not explicitly covered above, this function returns a
14446 DECL, OVERLOAD, or baselink representing the result of the lookup.
14447 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14448 is returned.
14449
472c29c3 14450 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
fc6a28d7
MM
14451 (e.g., "struct") that was used. In that case bindings that do not
14452 refer to types are ignored.
a723baf1 14453
b0bc6e8e
KL
14454 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14455 ignored.
14456
eea9800f
MM
14457 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14458 are ignored.
14459
a723baf1 14460 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
c8094d83 14461 types.
8f78f01f
MM
14462
14463 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14464 results in an ambiguity, and false otherwise. */
a723baf1
MM
14465
14466static tree
21526606 14467cp_parser_lookup_name (cp_parser *parser, tree name,
fc6a28d7 14468 enum tag_types tag_type,
02ed62dd
MM
14469 bool is_template,
14470 bool is_namespace,
8f78f01f
MM
14471 bool check_dependency,
14472 bool *ambiguous_p)
a723baf1 14473{
ef07d61b 14474 int flags = 0;
a723baf1
MM
14475 tree decl;
14476 tree object_type = parser->context->object_type;
14477
ef07d61b
VR
14478 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14479 flags |= LOOKUP_COMPLAIN;
14480
8f78f01f
MM
14481 /* Assume that the lookup will be unambiguous. */
14482 if (ambiguous_p)
14483 *ambiguous_p = false;
14484
a723baf1
MM
14485 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14486 no longer valid. Note that if we are parsing tentatively, and
14487 the parse fails, OBJECT_TYPE will be automatically restored. */
14488 parser->context->object_type = NULL_TREE;
14489
14490 if (name == error_mark_node)
14491 return error_mark_node;
14492
14493 /* A template-id has already been resolved; there is no lookup to
14494 do. */
14495 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14496 return name;
14497 if (BASELINK_P (name))
14498 {
50bc768d
NS
14499 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14500 == TEMPLATE_ID_EXPR);
a723baf1
MM
14501 return name;
14502 }
14503
14504 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14505 it should already have been checked to make sure that the name
14506 used matches the type being destroyed. */
14507 if (TREE_CODE (name) == BIT_NOT_EXPR)
14508 {
14509 tree type;
14510
14511 /* Figure out to which type this destructor applies. */
14512 if (parser->scope)
14513 type = parser->scope;
14514 else if (object_type)
14515 type = object_type;
14516 else
14517 type = current_class_type;
14518 /* If that's not a class type, there is no destructor. */
14519 if (!type || !CLASS_TYPE_P (type))
14520 return error_mark_node;
9f4faeae
MM
14521 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14522 lazily_declare_fn (sfk_destructor, type);
fd6e3cce
GB
14523 if (!CLASSTYPE_DESTRUCTORS (type))
14524 return error_mark_node;
a723baf1
MM
14525 /* If it was a class type, return the destructor. */
14526 return CLASSTYPE_DESTRUCTORS (type);
14527 }
14528
14529 /* By this point, the NAME should be an ordinary identifier. If
14530 the id-expression was a qualified name, the qualifying scope is
14531 stored in PARSER->SCOPE at this point. */
50bc768d 14532 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
21526606 14533
a723baf1
MM
14534 /* Perform the lookup. */
14535 if (parser->scope)
21526606 14536 {
1fb3244a 14537 bool dependent_p;
a723baf1
MM
14538
14539 if (parser->scope == error_mark_node)
14540 return error_mark_node;
14541
14542 /* If the SCOPE is dependent, the lookup must be deferred until
14543 the template is instantiated -- unless we are explicitly
14544 looking up names in uninstantiated templates. Even then, we
14545 cannot look up the name if the scope is not a class type; it
14546 might, for example, be a template type parameter. */
1fb3244a
MM
14547 dependent_p = (TYPE_P (parser->scope)
14548 && !(parser->in_declarator_p
14549 && currently_open_class (parser->scope))
14550 && dependent_type_p (parser->scope));
a723baf1 14551 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
1fb3244a 14552 && dependent_p)
a723baf1 14553 {
fc6a28d7
MM
14554 if (tag_type)
14555 {
14556 tree type;
14557
14558 /* The resolution to Core Issue 180 says that `struct
14559 A::B' should be considered a type-name, even if `A'
14560 is dependent. */
14561 type = make_typename_type (parser->scope, name, tag_type,
14562 /*complain=*/1);
fc6a28d7
MM
14563 decl = TYPE_NAME (type);
14564 }
02ed62dd
MM
14565 else if (is_template
14566 && (cp_parser_next_token_ends_template_argument_p (parser)
14567 || cp_lexer_next_token_is (parser->lexer,
14568 CPP_CLOSE_PAREN)))
5b4acce1 14569 decl = make_unbound_class_template (parser->scope,
b939a023 14570 name, NULL_TREE,
5b4acce1 14571 /*complain=*/1);
b0bc6e8e 14572 else
02ed62dd
MM
14573 decl = build_qualified_name (/*type=*/NULL_TREE,
14574 parser->scope, name,
14575 is_template);
a723baf1
MM
14576 }
14577 else
14578 {
4514aa8c 14579 tree pushed_scope = NULL_TREE;
91b004e5 14580
a723baf1
MM
14581 /* If PARSER->SCOPE is a dependent type, then it must be a
14582 class type, and we must not be checking dependencies;
14583 otherwise, we would have processed this lookup above. So
14584 that PARSER->SCOPE is not considered a dependent base by
14585 lookup_member, we must enter the scope here. */
1fb3244a 14586 if (dependent_p)
4514aa8c 14587 pushed_scope = push_scope (parser->scope);
78dcd41a 14588 /* If the PARSER->SCOPE is a template specialization, it
a723baf1
MM
14589 may be instantiated during name lookup. In that case,
14590 errors may be issued. Even if we rollback the current
14591 tentative parse, those errors are valid. */
c8094d83
MS
14592 decl = lookup_qualified_name (parser->scope, name,
14593 tag_type != none_type,
5e08432e 14594 /*complain=*/true);
4514aa8c
NS
14595 if (pushed_scope)
14596 pop_scope (pushed_scope);
a723baf1
MM
14597 }
14598 parser->qualifying_scope = parser->scope;
14599 parser->object_scope = NULL_TREE;
14600 }
14601 else if (object_type)
14602 {
14603 tree object_decl = NULL_TREE;
14604 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14605 OBJECT_TYPE is not a class. */
14606 if (CLASS_TYPE_P (object_type))
14607 /* If the OBJECT_TYPE is a template specialization, it may
14608 be instantiated during name lookup. In that case, errors
14609 may be issued. Even if we rollback the current tentative
14610 parse, those errors are valid. */
14611 object_decl = lookup_member (object_type,
14612 name,
c8094d83 14613 /*protect=*/0,
fc6a28d7 14614 tag_type != none_type);
a723baf1 14615 /* Look it up in the enclosing context, too. */
c8094d83 14616 decl = lookup_name_real (name, tag_type != none_type,
fc6a28d7 14617 /*nonclass=*/0,
ef07d61b 14618 /*block_p=*/true, is_namespace, flags);
a723baf1
MM
14619 parser->object_scope = object_type;
14620 parser->qualifying_scope = NULL_TREE;
14621 if (object_decl)
14622 decl = object_decl;
14623 }
14624 else
14625 {
c8094d83 14626 decl = lookup_name_real (name, tag_type != none_type,
fc6a28d7 14627 /*nonclass=*/0,
ef07d61b 14628 /*block_p=*/true, is_namespace, flags);
a723baf1
MM
14629 parser->qualifying_scope = NULL_TREE;
14630 parser->object_scope = NULL_TREE;
14631 }
14632
14633 /* If the lookup failed, let our caller know. */
bd3d082e 14634 if (!decl || decl == error_mark_node)
a723baf1
MM
14635 return error_mark_node;
14636
14637 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14638 if (TREE_CODE (decl) == TREE_LIST)
14639 {
8f78f01f
MM
14640 if (ambiguous_p)
14641 *ambiguous_p = true;
a723baf1
MM
14642 /* The error message we have to print is too complicated for
14643 cp_parser_error, so we incorporate its actions directly. */
e5976695 14644 if (!cp_parser_simulate_error (parser))
a723baf1 14645 {
2a13a625 14646 error ("reference to %qD is ambiguous", name);
a723baf1
MM
14647 print_candidates (decl);
14648 }
14649 return error_mark_node;
14650 }
14651
50bc768d
NS
14652 gcc_assert (DECL_P (decl)
14653 || TREE_CODE (decl) == OVERLOAD
14654 || TREE_CODE (decl) == SCOPE_REF
14655 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14656 || BASELINK_P (decl));
a723baf1
MM
14657
14658 /* If we have resolved the name of a member declaration, check to
14659 see if the declaration is accessible. When the name resolves to
34cd5ae7 14660 set of overloaded functions, accessibility is checked when
21526606 14661 overload resolution is done.
a723baf1
MM
14662
14663 During an explicit instantiation, access is not checked at all,
14664 as per [temp.explicit]. */
8d241e0b 14665 if (DECL_P (decl))
ee76b931 14666 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
a723baf1
MM
14667
14668 return decl;
14669}
14670
14671/* Like cp_parser_lookup_name, but for use in the typical case where
b0bc6e8e
KL
14672 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14673 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
a723baf1
MM
14674
14675static tree
94edc4ab 14676cp_parser_lookup_name_simple (cp_parser* parser, tree name)
a723baf1 14677{
21526606 14678 return cp_parser_lookup_name (parser, name,
fc6a28d7 14679 none_type,
b0bc6e8e 14680 /*is_template=*/false,
eea9800f 14681 /*is_namespace=*/false,
8f78f01f
MM
14682 /*check_dependency=*/true,
14683 /*ambiguous_p=*/NULL);
a723baf1
MM
14684}
14685
a723baf1
MM
14686/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14687 the current context, return the TYPE_DECL. If TAG_NAME_P is
14688 true, the DECL indicates the class being defined in a class-head,
14689 or declared in an elaborated-type-specifier.
14690
14691 Otherwise, return DECL. */
14692
14693static tree
14694cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14695{
710b73e6
KL
14696 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14697 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
a723baf1 14698
21526606 14699 struct A {
0cbd7506 14700 template <typename T> struct B;
a723baf1
MM
14701 };
14702
21526606
EC
14703 template <typename T> struct A::B {};
14704
c72a1a86 14705 Similarly, in an elaborated-type-specifier:
a723baf1
MM
14706
14707 namespace N { struct X{}; }
14708
14709 struct A {
0cbd7506 14710 template <typename T> friend struct N::X;
a723baf1
MM
14711 };
14712
710b73e6
KL
14713 However, if the DECL refers to a class type, and we are in
14714 the scope of the class, then the name lookup automatically
14715 finds the TYPE_DECL created by build_self_reference rather
14716 than a TEMPLATE_DECL. For example, in:
14717
14718 template <class T> struct S {
0cbd7506 14719 S s;
710b73e6
KL
14720 };
14721
14722 there is no need to handle such case. */
14723
14724 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
a723baf1
MM
14725 return DECL_TEMPLATE_RESULT (decl);
14726
14727 return decl;
14728}
14729
14730/* If too many, or too few, template-parameter lists apply to the
14731 declarator, issue an error message. Returns TRUE if all went well,
14732 and FALSE otherwise. */
14733
14734static bool
21526606 14735cp_parser_check_declarator_template_parameters (cp_parser* parser,
058b15c1 14736 cp_declarator *declarator)
a723baf1
MM
14737{
14738 unsigned num_templates;
14739
14740 /* We haven't seen any classes that involve template parameters yet. */
14741 num_templates = 0;
14742
058b15c1 14743 switch (declarator->kind)
a723baf1 14744 {
058b15c1 14745 case cdk_id:
1d786913 14746 if (declarator->u.id.qualifying_scope)
058b15c1
MM
14747 {
14748 tree scope;
14749 tree member;
a723baf1 14750
1d786913
MM
14751 scope = declarator->u.id.qualifying_scope;
14752 member = declarator->u.id.unqualified_name;
a723baf1 14753
058b15c1
MM
14754 while (scope && CLASS_TYPE_P (scope))
14755 {
14756 /* You're supposed to have one `template <...>'
14757 for every template class, but you don't need one
14758 for a full specialization. For example:
14759
14760 template <class T> struct S{};
14761 template <> struct S<int> { void f(); };
14762 void S<int>::f () {}
14763
14764 is correct; there shouldn't be a `template <>' for
14765 the definition of `S<int>::f'. */
14766 if (CLASSTYPE_TEMPLATE_INFO (scope)
14767 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14768 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14769 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14770 ++num_templates;
14771
14772 scope = TYPE_CONTEXT (scope);
14773 }
14774 }
c8094d83 14775 else if (TREE_CODE (declarator->u.id.unqualified_name)
1d786913
MM
14776 == TEMPLATE_ID_EXPR)
14777 /* If the DECLARATOR has the form `X<y>' then it uses one
14778 additional level of template parameters. */
a723baf1
MM
14779 ++num_templates;
14780
21526606 14781 return cp_parser_check_template_parameters (parser,
a723baf1 14782 num_templates);
058b15c1
MM
14783
14784 case cdk_function:
14785 case cdk_array:
14786 case cdk_pointer:
14787 case cdk_reference:
14788 case cdk_ptrmem:
98ca843c 14789 return (cp_parser_check_declarator_template_parameters
058b15c1
MM
14790 (parser, declarator->declarator));
14791
14792 case cdk_error:
14793 return true;
14794
14795 default:
315fb5db 14796 gcc_unreachable ();
a723baf1 14797 }
315fb5db 14798 return false;
a723baf1
MM
14799}
14800
14801/* NUM_TEMPLATES were used in the current declaration. If that is
14802 invalid, return FALSE and issue an error messages. Otherwise,
14803 return TRUE. */
14804
14805static bool
94edc4ab 14806cp_parser_check_template_parameters (cp_parser* parser,
0cbd7506 14807 unsigned num_templates)
a723baf1
MM
14808{
14809 /* If there are more template classes than parameter lists, we have
14810 something like:
21526606 14811
a723baf1
MM
14812 template <class T> void S<T>::R<T>::f (); */
14813 if (parser->num_template_parameter_lists < num_templates)
14814 {
14815 error ("too few template-parameter-lists");
14816 return false;
14817 }
14818 /* If there are the same number of template classes and parameter
14819 lists, that's OK. */
14820 if (parser->num_template_parameter_lists == num_templates)
14821 return true;
14822 /* If there are more, but only one more, then we are referring to a
14823 member template. That's OK too. */
14824 if (parser->num_template_parameter_lists == num_templates + 1)
14825 return true;
14826 /* Otherwise, there are too many template parameter lists. We have
14827 something like:
14828
14829 template <class T> template <class U> void S::f(); */
14830 error ("too many template-parameter-lists");
14831 return false;
14832}
14833
a723baf1
MM
14834/* Parse an optional `::' token indicating that the following name is
14835 from the global namespace. If so, PARSER->SCOPE is set to the
14836 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14837 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14838 Returns the new value of PARSER->SCOPE, if the `::' token is
14839 present, and NULL_TREE otherwise. */
14840
14841static tree
94edc4ab 14842cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
a723baf1
MM
14843{
14844 cp_token *token;
14845
14846 /* Peek at the next token. */
14847 token = cp_lexer_peek_token (parser->lexer);
14848 /* If we're looking at a `::' token then we're starting from the
14849 global namespace, not our current location. */
14850 if (token->type == CPP_SCOPE)
14851 {
14852 /* Consume the `::' token. */
14853 cp_lexer_consume_token (parser->lexer);
14854 /* Set the SCOPE so that we know where to start the lookup. */
14855 parser->scope = global_namespace;
14856 parser->qualifying_scope = global_namespace;
14857 parser->object_scope = NULL_TREE;
14858
14859 return parser->scope;
14860 }
14861 else if (!current_scope_valid_p)
14862 {
14863 parser->scope = NULL_TREE;
14864 parser->qualifying_scope = NULL_TREE;
14865 parser->object_scope = NULL_TREE;
14866 }
14867
14868 return NULL_TREE;
14869}
14870
14871/* Returns TRUE if the upcoming token sequence is the start of a
14872 constructor declarator. If FRIEND_P is true, the declarator is
14873 preceded by the `friend' specifier. */
14874
14875static bool
14876cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14877{
14878 bool constructor_p;
14879 tree type_decl = NULL_TREE;
14880 bool nested_name_p;
2050a1bb
MM
14881 cp_token *next_token;
14882
14883 /* The common case is that this is not a constructor declarator, so
8fbc5ae7
MM
14884 try to avoid doing lots of work if at all possible. It's not
14885 valid declare a constructor at function scope. */
14886 if (at_function_scope_p ())
14887 return false;
14888 /* And only certain tokens can begin a constructor declarator. */
2050a1bb
MM
14889 next_token = cp_lexer_peek_token (parser->lexer);
14890 if (next_token->type != CPP_NAME
14891 && next_token->type != CPP_SCOPE
14892 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14893 && next_token->type != CPP_TEMPLATE_ID)
14894 return false;
a723baf1
MM
14895
14896 /* Parse tentatively; we are going to roll back all of the tokens
14897 consumed here. */
14898 cp_parser_parse_tentatively (parser);
14899 /* Assume that we are looking at a constructor declarator. */
14900 constructor_p = true;
8d241e0b 14901
a723baf1
MM
14902 /* Look for the optional `::' operator. */
14903 cp_parser_global_scope_opt (parser,
14904 /*current_scope_valid_p=*/false);
14905 /* Look for the nested-name-specifier. */
21526606 14906 nested_name_p
a723baf1
MM
14907 = (cp_parser_nested_name_specifier_opt (parser,
14908 /*typename_keyword_p=*/false,
14909 /*check_dependency_p=*/false,
a668c6ad
MM
14910 /*type_p=*/false,
14911 /*is_declaration=*/false)
a723baf1
MM
14912 != NULL_TREE);
14913 /* Outside of a class-specifier, there must be a
14914 nested-name-specifier. */
21526606 14915 if (!nested_name_p &&
a723baf1
MM
14916 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14917 || friend_p))
14918 constructor_p = false;
14919 /* If we still think that this might be a constructor-declarator,
14920 look for a class-name. */
14921 if (constructor_p)
14922 {
14923 /* If we have:
14924
8fbc5ae7 14925 template <typename T> struct S { S(); };
a723baf1
MM
14926 template <typename T> S<T>::S ();
14927
14928 we must recognize that the nested `S' names a class.
14929 Similarly, for:
14930
14931 template <typename T> S<T>::S<T> ();
14932
14933 we must recognize that the nested `S' names a template. */
14934 type_decl = cp_parser_class_name (parser,
14935 /*typename_keyword_p=*/false,
14936 /*template_keyword_p=*/false,
fc6a28d7 14937 none_type,
a723baf1 14938 /*check_dependency_p=*/false,
a668c6ad
MM
14939 /*class_head_p=*/false,
14940 /*is_declaration=*/false);
a723baf1
MM
14941 /* If there was no class-name, then this is not a constructor. */
14942 constructor_p = !cp_parser_error_occurred (parser);
14943 }
8d241e0b 14944
a723baf1
MM
14945 /* If we're still considering a constructor, we have to see a `(',
14946 to begin the parameter-declaration-clause, followed by either a
14947 `)', an `...', or a decl-specifier. We need to check for a
14948 type-specifier to avoid being fooled into thinking that:
14949
14950 S::S (f) (int);
14951
14952 is a constructor. (It is actually a function named `f' that
14953 takes one parameter (of type `int') and returns a value of type
14954 `S::S'. */
21526606 14955 if (constructor_p
a723baf1
MM
14956 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14957 {
14958 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14959 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15077df5
MM
14960 /* A parameter declaration begins with a decl-specifier,
14961 which is either the "attribute" keyword, a storage class
14962 specifier, or (usually) a type-specifier. */
14963 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
a723baf1
MM
14964 && !cp_parser_storage_class_specifier_opt (parser))
14965 {
5dae1114 14966 tree type;
4514aa8c 14967 tree pushed_scope = NULL_TREE;
4047b164 14968 unsigned saved_num_template_parameter_lists;
5dae1114
MM
14969
14970 /* Names appearing in the type-specifier should be looked up
14971 in the scope of the class. */
14972 if (current_class_type)
14973 type = NULL_TREE;
a723baf1
MM
14974 else
14975 {
5dae1114
MM
14976 type = TREE_TYPE (type_decl);
14977 if (TREE_CODE (type) == TYPENAME_TYPE)
14d22dd6 14978 {
21526606 14979 type = resolve_typename_type (type,
14d22dd6
MM
14980 /*only_current_p=*/false);
14981 if (type == error_mark_node)
14982 {
14983 cp_parser_abort_tentative_parse (parser);
14984 return false;
14985 }
14986 }
4514aa8c 14987 pushed_scope = push_scope (type);
a723baf1 14988 }
4047b164
KL
14989
14990 /* Inside the constructor parameter list, surrounding
14991 template-parameter-lists do not apply. */
14992 saved_num_template_parameter_lists
14993 = parser->num_template_parameter_lists;
14994 parser->num_template_parameter_lists = 0;
14995
5dae1114
MM
14996 /* Look for the type-specifier. */
14997 cp_parser_type_specifier (parser,
14998 CP_PARSER_FLAGS_NONE,
62d1db17 14999 /*decl_specs=*/NULL,
5dae1114
MM
15000 /*is_declarator=*/true,
15001 /*declares_class_or_enum=*/NULL,
15002 /*is_cv_qualifier=*/NULL);
4047b164
KL
15003
15004 parser->num_template_parameter_lists
15005 = saved_num_template_parameter_lists;
15006
5dae1114 15007 /* Leave the scope of the class. */
4514aa8c
NS
15008 if (pushed_scope)
15009 pop_scope (pushed_scope);
5dae1114
MM
15010
15011 constructor_p = !cp_parser_error_occurred (parser);
a723baf1
MM
15012 }
15013 }
15014 else
15015 constructor_p = false;
15016 /* We did not really want to consume any tokens. */
15017 cp_parser_abort_tentative_parse (parser);
15018
15019 return constructor_p;
15020}
15021
15022/* Parse the definition of the function given by the DECL_SPECIFIERS,
cf22909c 15023 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
a723baf1
MM
15024 they must be performed once we are in the scope of the function.
15025
15026 Returns the function defined. */
15027
15028static tree
15029cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab 15030 (cp_parser* parser,
62d1db17 15031 cp_decl_specifier_seq *decl_specifiers,
94edc4ab 15032 tree attributes,
058b15c1 15033 const cp_declarator *declarator)
a723baf1
MM
15034{
15035 tree fn;
15036 bool success_p;
15037
15038 /* Begin the function-definition. */
058b15c1
MM
15039 success_p = start_function (decl_specifiers, declarator, attributes);
15040
15041 /* The things we're about to see are not directly qualified by any
15042 template headers we've seen thus far. */
15043 reset_specialization ();
a723baf1
MM
15044
15045 /* If there were names looked up in the decl-specifier-seq that we
15046 did not check, check them now. We must wait until we are in the
15047 scope of the function to perform the checks, since the function
15048 might be a friend. */
cf22909c 15049 perform_deferred_access_checks ();
a723baf1
MM
15050
15051 if (!success_p)
15052 {
058b15c1 15053 /* Skip the entire function. */
a723baf1
MM
15054 error ("invalid function declaration");
15055 cp_parser_skip_to_end_of_block_or_statement (parser);
15056 fn = error_mark_node;
15057 }
15058 else
15059 fn = cp_parser_function_definition_after_declarator (parser,
15060 /*inline_p=*/false);
15061
15062 return fn;
15063}
15064
15065/* Parse the part of a function-definition that follows the
15066 declarator. INLINE_P is TRUE iff this function is an inline
15067 function defined with a class-specifier.
15068
15069 Returns the function defined. */
15070
21526606
EC
15071static tree
15072cp_parser_function_definition_after_declarator (cp_parser* parser,
94edc4ab 15073 bool inline_p)
a723baf1
MM
15074{
15075 tree fn;
15076 bool ctor_initializer_p = false;
15077 bool saved_in_unbraced_linkage_specification_p;
15078 unsigned saved_num_template_parameter_lists;
15079
15080 /* If the next token is `return', then the code may be trying to
15081 make use of the "named return value" extension that G++ used to
15082 support. */
15083 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15084 {
15085 /* Consume the `return' keyword. */
15086 cp_lexer_consume_token (parser->lexer);
15087 /* Look for the identifier that indicates what value is to be
15088 returned. */
15089 cp_parser_identifier (parser);
15090 /* Issue an error message. */
15091 error ("named return values are no longer supported");
15092 /* Skip tokens until we reach the start of the function body. */
21eb631b
MM
15093 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
15094 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
a723baf1
MM
15095 cp_lexer_consume_token (parser->lexer);
15096 }
15097 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15098 anything declared inside `f'. */
21526606 15099 saved_in_unbraced_linkage_specification_p
a723baf1
MM
15100 = parser->in_unbraced_linkage_specification_p;
15101 parser->in_unbraced_linkage_specification_p = false;
15102 /* Inside the function, surrounding template-parameter-lists do not
15103 apply. */
21526606
EC
15104 saved_num_template_parameter_lists
15105 = parser->num_template_parameter_lists;
a723baf1
MM
15106 parser->num_template_parameter_lists = 0;
15107 /* If the next token is `try', then we are looking at a
15108 function-try-block. */
15109 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15110 ctor_initializer_p = cp_parser_function_try_block (parser);
15111 /* A function-try-block includes the function-body, so we only do
15112 this next part if we're not processing a function-try-block. */
15113 else
21526606 15114 ctor_initializer_p
a723baf1
MM
15115 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15116
15117 /* Finish the function. */
21526606 15118 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
a723baf1
MM
15119 (inline_p ? 2 : 0));
15120 /* Generate code for it, if necessary. */
8cd2462c 15121 expand_or_defer_fn (fn);
a723baf1 15122 /* Restore the saved values. */
21526606 15123 parser->in_unbraced_linkage_specification_p
a723baf1 15124 = saved_in_unbraced_linkage_specification_p;
21526606 15125 parser->num_template_parameter_lists
a723baf1
MM
15126 = saved_num_template_parameter_lists;
15127
15128 return fn;
15129}
15130
15131/* Parse a template-declaration, assuming that the `export' (and
15132 `extern') keywords, if present, has already been scanned. MEMBER_P
15133 is as for cp_parser_template_declaration. */
15134
15135static void
94edc4ab 15136cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
a723baf1
MM
15137{
15138 tree decl = NULL_TREE;
15139 tree parameter_list;
15140 bool friend_p = false;
15141
15142 /* Look for the `template' keyword. */
15143 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15144 return;
21526606 15145
a723baf1
MM
15146 /* And the `<'. */
15147 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15148 return;
21526606 15149
a723baf1
MM
15150 /* If the next token is `>', then we have an invalid
15151 specialization. Rather than complain about an invalid template
15152 parameter, issue an error message here. */
15153 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15154 {
15155 cp_parser_error (parser, "invalid explicit specialization");
2f9afd51 15156 begin_specialization ();
a723baf1
MM
15157 parameter_list = NULL_TREE;
15158 }
15159 else
2f9afd51
KL
15160 {
15161 /* Parse the template parameters. */
15162 begin_template_parm_list ();
15163 parameter_list = cp_parser_template_parameter_list (parser);
15164 parameter_list = end_template_parm_list (parameter_list);
15165 }
15166
a723baf1
MM
15167 /* Look for the `>'. */
15168 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15169 /* We just processed one more parameter list. */
15170 ++parser->num_template_parameter_lists;
15171 /* If the next token is `template', there are more template
15172 parameters. */
21526606 15173 if (cp_lexer_next_token_is_keyword (parser->lexer,
a723baf1
MM
15174 RID_TEMPLATE))
15175 cp_parser_template_declaration_after_export (parser, member_p);
15176 else
15177 {
fe88415f 15178 /* There are no access checks when parsing a template, as we do not
0cbd7506 15179 know if a specialization will be a friend. */
fe88415f 15180 push_deferring_access_checks (dk_no_check);
98ca843c 15181
a723baf1
MM
15182 decl = cp_parser_single_declaration (parser,
15183 member_p,
15184 &friend_p);
15185
fe88415f 15186 pop_deferring_access_checks ();
98ca843c 15187
a723baf1
MM
15188 /* If this is a member template declaration, let the front
15189 end know. */
15190 if (member_p && !friend_p && decl)
37d407a1
KL
15191 {
15192 if (TREE_CODE (decl) == TYPE_DECL)
15193 cp_parser_check_access_in_redeclaration (decl);
15194
15195 decl = finish_member_template_decl (decl);
15196 }
a723baf1 15197 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19db77ce
KL
15198 make_friend_class (current_class_type, TREE_TYPE (decl),
15199 /*complain=*/true);
a723baf1
MM
15200 }
15201 /* We are done with the current parameter list. */
15202 --parser->num_template_parameter_lists;
15203
15204 /* Finish up. */
15205 finish_template_decl (parameter_list);
15206
15207 /* Register member declarations. */
15208 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15209 finish_member_declaration (decl);
15210
15211 /* If DECL is a function template, we must return to parse it later.
15212 (Even though there is no definition, there might be default
15213 arguments that need handling.) */
21526606 15214 if (member_p && decl
a723baf1
MM
15215 && (TREE_CODE (decl) == FUNCTION_DECL
15216 || DECL_FUNCTION_TEMPLATE_P (decl)))
15217 TREE_VALUE (parser->unparsed_functions_queues)
21526606 15218 = tree_cons (NULL_TREE, decl,
a723baf1
MM
15219 TREE_VALUE (parser->unparsed_functions_queues));
15220}
15221
15222/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15223 `function-definition' sequence. MEMBER_P is true, this declaration
15224 appears in a class scope.
15225
15226 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15227 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15228
15229static tree
21526606 15230cp_parser_single_declaration (cp_parser* parser,
94edc4ab
NN
15231 bool member_p,
15232 bool* friend_p)
a723baf1 15233{
560ad596 15234 int declares_class_or_enum;
a723baf1 15235 tree decl = NULL_TREE;
62d1db17 15236 cp_decl_specifier_seq decl_specifiers;
4bb8ca28 15237 bool function_definition_p = false;
a723baf1 15238
71bd7186
MM
15239 /* This function is only used when processing a template
15240 declaration. */
15241 gcc_assert (innermost_scope_kind () == sk_template_parms
15242 || innermost_scope_kind () == sk_template_spec);
15243
a723baf1 15244 /* Defer access checks until we know what is being declared. */
8d241e0b 15245 push_deferring_access_checks (dk_deferred);
cf22909c 15246
a723baf1
MM
15247 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15248 alternative. */
62d1db17
MM
15249 cp_parser_decl_specifier_seq (parser,
15250 CP_PARSER_FLAGS_OPTIONAL,
15251 &decl_specifiers,
15252 &declares_class_or_enum);
4bb8ca28 15253 if (friend_p)
62d1db17 15254 *friend_p = cp_parser_friend_p (&decl_specifiers);
71bd7186
MM
15255
15256 /* There are no template typedefs. */
15257 if (decl_specifiers.specs[(int) ds_typedef])
15258 {
15259 error ("template declaration of %qs", "typedef");
15260 decl = error_mark_node;
15261 }
15262
a723baf1
MM
15263 /* Gather up the access checks that occurred the
15264 decl-specifier-seq. */
cf22909c
KL
15265 stop_deferring_access_checks ();
15266
a723baf1
MM
15267 /* Check for the declaration of a template class. */
15268 if (declares_class_or_enum)
15269 {
15270 if (cp_parser_declares_only_class_p (parser))
15271 {
62d1db17 15272 decl = shadow_tag (&decl_specifiers);
b939a023
KL
15273
15274 /* In this case:
15275
15276 struct C {
15277 friend template <typename T> struct A<T>::B;
15278 };
15279
15280 A<T>::B will be represented by a TYPENAME_TYPE, and
15281 therefore not recognized by shadow_tag. */
15282 if (friend_p && *friend_p
15283 && !decl
15284 && decl_specifiers.type
15285 && TYPE_P (decl_specifiers.type))
15286 decl = decl_specifiers.type;
15287
62d1db17 15288 if (decl && decl != error_mark_node)
a723baf1
MM
15289 decl = TYPE_NAME (decl);
15290 else
15291 decl = error_mark_node;
15292 }
15293 }
a723baf1
MM
15294 /* If it's not a template class, try for a template function. If
15295 the next token is a `;', then this declaration does not declare
15296 anything. But, if there were errors in the decl-specifiers, then
15297 the error might well have come from an attempted class-specifier.
15298 In that case, there's no need to warn about a missing declarator. */
15299 if (!decl
15300 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
62d1db17 15301 || decl_specifiers.type != error_mark_node))
21526606 15302 decl = cp_parser_init_declarator (parser,
62d1db17 15303 &decl_specifiers,
4bb8ca28 15304 /*function_definition_allowed_p=*/true,
a723baf1 15305 member_p,
560ad596 15306 declares_class_or_enum,
4bb8ca28 15307 &function_definition_p);
cf22909c
KL
15308
15309 pop_deferring_access_checks ();
15310
a723baf1
MM
15311 /* Clear any current qualification; whatever comes next is the start
15312 of something new. */
15313 parser->scope = NULL_TREE;
15314 parser->qualifying_scope = NULL_TREE;
15315 parser->object_scope = NULL_TREE;
15316 /* Look for a trailing `;' after the declaration. */
4bb8ca28 15317 if (!function_definition_p
71bd7186
MM
15318 && (decl == error_mark_node
15319 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
a723baf1 15320 cp_parser_skip_to_end_of_block_or_statement (parser);
a723baf1
MM
15321
15322 return decl;
15323}
15324
d6b4ea85
MM
15325/* Parse a cast-expression that is not the operand of a unary "&". */
15326
15327static tree
15328cp_parser_simple_cast_expression (cp_parser *parser)
15329{
93678513
MM
15330 return cp_parser_cast_expression (parser, /*address_p=*/false,
15331 /*cast_p=*/false);
d6b4ea85
MM
15332}
15333
a723baf1
MM
15334/* Parse a functional cast to TYPE. Returns an expression
15335 representing the cast. */
15336
15337static tree
94edc4ab 15338cp_parser_functional_cast (cp_parser* parser, tree type)
a723baf1
MM
15339{
15340 tree expression_list;
d36d5600 15341 tree cast;
a723baf1 15342
21526606 15343 expression_list
39703eb9 15344 = cp_parser_parenthesized_expression_list (parser, false,
93678513 15345 /*cast_p=*/true,
39703eb9 15346 /*non_constant_p=*/NULL);
a723baf1 15347
d36d5600
GB
15348 cast = build_functional_cast (type, expression_list);
15349 /* [expr.const]/1: In an integral constant expression "only type
15350 conversions to integral or enumeration type can be used". */
3ce5fa4f
NS
15351 if (TREE_CODE (type) == TYPE_DECL)
15352 type = TREE_TYPE (type);
15353 if (cast != error_mark_node && !dependent_type_p (type)
15354 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
d36d5600 15355 {
98ca843c 15356 if (cp_parser_non_integral_constant_expression
d36d5600
GB
15357 (parser, "a call to a constructor"))
15358 return error_mark_node;
15359 }
15360 return cast;
a723baf1
MM
15361}
15362
4bb8ca28
MM
15363/* Save the tokens that make up the body of a member function defined
15364 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15365 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15366 specifiers applied to the declaration. Returns the FUNCTION_DECL
15367 for the member function. */
15368
7ce27103 15369static tree
4bb8ca28 15370cp_parser_save_member_function_body (cp_parser* parser,
62d1db17 15371 cp_decl_specifier_seq *decl_specifiers,
058b15c1 15372 cp_declarator *declarator,
4bb8ca28
MM
15373 tree attributes)
15374{
c162c75e
MA
15375 cp_token *first;
15376 cp_token *last;
4bb8ca28
MM
15377 tree fn;
15378
15379 /* Create the function-declaration. */
15380 fn = start_method (decl_specifiers, declarator, attributes);
15381 /* If something went badly wrong, bail out now. */
15382 if (fn == error_mark_node)
15383 {
15384 /* If there's a function-body, skip it. */
21526606 15385 if (cp_parser_token_starts_function_definition_p
4bb8ca28
MM
15386 (cp_lexer_peek_token (parser->lexer)))
15387 cp_parser_skip_to_end_of_block_or_statement (parser);
15388 return error_mark_node;
15389 }
15390
15391 /* Remember it, if there default args to post process. */
15392 cp_parser_save_default_args (parser, fn);
15393
21526606 15394 /* Save away the tokens that make up the body of the
4bb8ca28 15395 function. */
c162c75e
MA
15396 first = parser->lexer->next_token;
15397 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
4bb8ca28
MM
15398 /* Handle function try blocks. */
15399 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
c162c75e
MA
15400 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15401 last = parser->lexer->next_token;
4bb8ca28
MM
15402
15403 /* Save away the inline definition; we will process it when the
15404 class is complete. */
c162c75e 15405 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
4bb8ca28
MM
15406 DECL_PENDING_INLINE_P (fn) = 1;
15407
15408 /* We need to know that this was defined in the class, so that
15409 friend templates are handled correctly. */
15410 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15411
15412 /* We're done with the inline definition. */
15413 finish_method (fn);
15414
15415 /* Add FN to the queue of functions to be parsed later. */
15416 TREE_VALUE (parser->unparsed_functions_queues)
21526606 15417 = tree_cons (NULL_TREE, fn,
4bb8ca28
MM
15418 TREE_VALUE (parser->unparsed_functions_queues));
15419
15420 return fn;
15421}
15422
ec75414f
MM
15423/* Parse a template-argument-list, as well as the trailing ">" (but
15424 not the opening ">"). See cp_parser_template_argument_list for the
15425 return value. */
15426
15427static tree
15428cp_parser_enclosed_template_argument_list (cp_parser* parser)
15429{
15430 tree arguments;
15431 tree saved_scope;
15432 tree saved_qualifying_scope;
15433 tree saved_object_scope;
15434 bool saved_greater_than_is_operator_p;
49f210a2 15435 bool saved_skip_evaluation;
ec75414f
MM
15436
15437 /* [temp.names]
15438
15439 When parsing a template-id, the first non-nested `>' is taken as
15440 the end of the template-argument-list rather than a greater-than
15441 operator. */
21526606 15442 saved_greater_than_is_operator_p
ec75414f
MM
15443 = parser->greater_than_is_operator_p;
15444 parser->greater_than_is_operator_p = false;
15445 /* Parsing the argument list may modify SCOPE, so we save it
15446 here. */
15447 saved_scope = parser->scope;
15448 saved_qualifying_scope = parser->qualifying_scope;
15449 saved_object_scope = parser->object_scope;
49f210a2
MM
15450 /* We need to evaluate the template arguments, even though this
15451 template-id may be nested within a "sizeof". */
15452 saved_skip_evaluation = skip_evaluation;
15453 skip_evaluation = false;
ec75414f
MM
15454 /* Parse the template-argument-list itself. */
15455 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15456 arguments = NULL_TREE;
15457 else
15458 arguments = cp_parser_template_argument_list (parser);
4d5297fa
GB
15459 /* Look for the `>' that ends the template-argument-list. If we find
15460 a '>>' instead, it's probably just a typo. */
15461 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15462 {
15463 if (!saved_greater_than_is_operator_p)
15464 {
2cfe82fe
ZW
15465 /* If we're in a nested template argument list, the '>>' has
15466 to be a typo for '> >'. We emit the error message, but we
15467 continue parsing and we push a '>' as next token, so that
15468 the argument list will be parsed correctly. Note that the
15469 global source location is still on the token before the
15470 '>>', so we need to say explicitly where we want it. */
15471 cp_token *token = cp_lexer_peek_token (parser->lexer);
15472 error ("%H%<>>%> should be %<> >%> "
15473 "within a nested template argument list",
15474 &token->location);
15475
15476 /* ??? Proper recovery should terminate two levels of
15477 template argument list here. */
4d5297fa
GB
15478 token->type = CPP_GREATER;
15479 }
15480 else
15481 {
2cfe82fe
ZW
15482 /* If this is not a nested template argument list, the '>>'
15483 is a typo for '>'. Emit an error message and continue.
15484 Same deal about the token location, but here we can get it
15485 right by consuming the '>>' before issuing the diagnostic. */
4d5297fa 15486 cp_lexer_consume_token (parser->lexer);
2cfe82fe
ZW
15487 error ("spurious %<>>%>, use %<>%> to terminate "
15488 "a template argument list");
4d5297fa
GB
15489 }
15490 }
2cfe82fe
ZW
15491 else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15492 error ("missing %<>%> to terminate the template argument list");
15493 else
15494 /* It's what we want, a '>'; consume it. */
15495 cp_lexer_consume_token (parser->lexer);
ec75414f 15496 /* The `>' token might be a greater-than operator again now. */
21526606 15497 parser->greater_than_is_operator_p
ec75414f
MM
15498 = saved_greater_than_is_operator_p;
15499 /* Restore the SAVED_SCOPE. */
15500 parser->scope = saved_scope;
15501 parser->qualifying_scope = saved_qualifying_scope;
15502 parser->object_scope = saved_object_scope;
49f210a2 15503 skip_evaluation = saved_skip_evaluation;
ec75414f
MM
15504
15505 return arguments;
15506}
15507
a723baf1
MM
15508/* MEMBER_FUNCTION is a member function, or a friend. If default
15509 arguments, or the body of the function have not yet been parsed,
15510 parse them now. */
15511
15512static void
94edc4ab 15513cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
a723baf1 15514{
a723baf1
MM
15515 /* If this member is a template, get the underlying
15516 FUNCTION_DECL. */
15517 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15518 member_function = DECL_TEMPLATE_RESULT (member_function);
15519
15520 /* There should not be any class definitions in progress at this
15521 point; the bodies of members are only parsed outside of all class
15522 definitions. */
50bc768d 15523 gcc_assert (parser->num_classes_being_defined == 0);
a723baf1
MM
15524 /* While we're parsing the member functions we might encounter more
15525 classes. We want to handle them right away, but we don't want
15526 them getting mixed up with functions that are currently in the
15527 queue. */
15528 parser->unparsed_functions_queues
15529 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15530
15531 /* Make sure that any template parameters are in scope. */
15532 maybe_begin_member_template_processing (member_function);
15533
a723baf1
MM
15534 /* If the body of the function has not yet been parsed, parse it
15535 now. */
15536 if (DECL_PENDING_INLINE_P (member_function))
15537 {
15538 tree function_scope;
15539 cp_token_cache *tokens;
15540
15541 /* The function is no longer pending; we are processing it. */
15542 tokens = DECL_PENDING_INLINE_INFO (member_function);
15543 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15544 DECL_PENDING_INLINE_P (member_function) = 0;
c8094d83 15545
f769035f
NS
15546 /* If this is a local class, enter the scope of the containing
15547 function. */
15548 function_scope = current_function_decl;
a723baf1
MM
15549 if (function_scope)
15550 push_function_context_to (function_scope);
21526606 15551
c8094d83 15552
2cfe82fe
ZW
15553 /* Push the body of the function onto the lexer stack. */
15554 cp_parser_push_lexer_for_tokens (parser, tokens);
21526606 15555
a723baf1
MM
15556 /* Let the front end know that we going to be defining this
15557 function. */
058b15c1
MM
15558 start_preparsed_function (member_function, NULL_TREE,
15559 SF_PRE_PARSED | SF_INCLASS_INLINE);
21526606 15560
2d637547
NS
15561 /* Don't do access checking if it is a templated function. */
15562 if (processing_template_decl)
15563 push_deferring_access_checks (dk_no_check);
c8094d83 15564
a723baf1
MM
15565 /* Now, parse the body of the function. */
15566 cp_parser_function_definition_after_declarator (parser,
15567 /*inline_p=*/true);
21526606 15568
2d637547
NS
15569 if (processing_template_decl)
15570 pop_deferring_access_checks ();
c8094d83 15571
a723baf1
MM
15572 /* Leave the scope of the containing function. */
15573 if (function_scope)
15574 pop_function_context_from (function_scope);
2cfe82fe 15575 cp_parser_pop_lexer (parser);
a723baf1
MM
15576 }
15577
15578 /* Remove any template parameters from the symbol table. */
15579 maybe_end_member_template_processing ();
15580
15581 /* Restore the queue. */
21526606 15582 parser->unparsed_functions_queues
a723baf1
MM
15583 = TREE_CHAIN (parser->unparsed_functions_queues);
15584}
15585
cd0be382 15586/* If DECL contains any default args, remember it on the unparsed
8db1028e
NS
15587 functions queue. */
15588
15589static void
15590cp_parser_save_default_args (cp_parser* parser, tree decl)
15591{
15592 tree probe;
15593
15594 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15595 probe;
15596 probe = TREE_CHAIN (probe))
15597 if (TREE_PURPOSE (probe))
15598 {
15599 TREE_PURPOSE (parser->unparsed_functions_queues)
f44b0c8e 15600 = tree_cons (current_class_type, decl,
8db1028e
NS
15601 TREE_PURPOSE (parser->unparsed_functions_queues));
15602 break;
15603 }
15604 return;
15605}
15606
8218bd34 15607/* FN is a FUNCTION_DECL which may contains a parameter with an
f44b0c8e
MM
15608 unparsed DEFAULT_ARG. Parse the default args now. This function
15609 assumes that the current scope is the scope in which the default
15610 argument should be processed. */
a723baf1
MM
15611
15612static void
8218bd34 15613cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
a723baf1 15614{
a723baf1 15615 bool saved_local_variables_forbidden_p;
2cfe82fe 15616 tree parm;
8218bd34 15617
b92bc2a0
NS
15618 /* While we're parsing the default args, we might (due to the
15619 statement expression extension) encounter more classes. We want
15620 to handle them right away, but we don't want them getting mixed
15621 up with default args that are currently in the queue. */
15622 parser->unparsed_functions_queues
15623 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15624
2cfe82fe
ZW
15625 /* Local variable names (and the `this' keyword) may not appear
15626 in a default argument. */
15627 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15628 parser->local_variables_forbidden_p = true;
15629
15630 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15631 parm;
15632 parm = TREE_CHAIN (parm))
a723baf1 15633 {
2cfe82fe 15634 cp_token_cache *tokens;
5e97d404
NS
15635 tree default_arg = TREE_PURPOSE (parm);
15636 tree parsed_arg;
01ea1ea8
NS
15637 VEC(tree,gc) *insts;
15638 tree copy;
15639 unsigned ix;
c8094d83 15640
5e97d404 15641 if (!default_arg)
2cfe82fe 15642 continue;
a723baf1 15643
efb169b0
NS
15644 if (TREE_CODE (default_arg) != DEFAULT_ARG)
15645 /* This can happen for a friend declaration for a function
15646 already declared with default arguments. */
15647 continue;
5e97d404 15648
2cfe82fe
ZW
15649 /* Push the saved tokens for the default argument onto the parser's
15650 lexer stack. */
5e97d404 15651 tokens = DEFARG_TOKENS (default_arg);
2cfe82fe 15652 cp_parser_push_lexer_for_tokens (parser, tokens);
a723baf1 15653
2cfe82fe 15654 /* Parse the assignment-expression. */
5e97d404
NS
15655 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15656
15657 TREE_PURPOSE (parm) = parsed_arg;
15658
15659 /* Update any instantiations we've already created. */
01ea1ea8
NS
15660 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15661 VEC_iterate (tree, insts, ix, copy); ix++)
15662 TREE_PURPOSE (copy) = parsed_arg;
a723baf1 15663
676e33ca
MM
15664 /* If the token stream has not been completely used up, then
15665 there was extra junk after the end of the default
15666 argument. */
15667 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2a13a625 15668 cp_parser_error (parser, "expected %<,%>");
676e33ca 15669
2cfe82fe
ZW
15670 /* Revert to the main lexer. */
15671 cp_parser_pop_lexer (parser);
a723baf1 15672 }
b92bc2a0 15673
2cfe82fe
ZW
15674 /* Restore the state of local_variables_forbidden_p. */
15675 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15676
b92bc2a0 15677 /* Restore the queue. */
21526606 15678 parser->unparsed_functions_queues
b92bc2a0 15679 = TREE_CHAIN (parser->unparsed_functions_queues);
a723baf1
MM
15680}
15681
15682/* Parse the operand of `sizeof' (or a similar operator). Returns
15683 either a TYPE or an expression, depending on the form of the
15684 input. The KEYWORD indicates which kind of expression we have
15685 encountered. */
15686
15687static tree
94edc4ab 15688cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
a723baf1
MM
15689{
15690 static const char *format;
15691 tree expr = NULL_TREE;
15692 const char *saved_message;
67c03833 15693 bool saved_integral_constant_expression_p;
93678513 15694 bool saved_non_integral_constant_expression_p;
a723baf1
MM
15695
15696 /* Initialize FORMAT the first time we get here. */
15697 if (!format)
9e637a26 15698 format = "types may not be defined in '%s' expressions";
a723baf1
MM
15699
15700 /* Types cannot be defined in a `sizeof' expression. Save away the
15701 old message. */
15702 saved_message = parser->type_definition_forbidden_message;
15703 /* And create the new one. */
21526606
EC
15704 parser->type_definition_forbidden_message
15705 = xmalloc (strlen (format)
c68b0a84
KG
15706 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15707 + 1 /* `\0' */);
a723baf1
MM
15708 sprintf ((char *) parser->type_definition_forbidden_message,
15709 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15710
15711 /* The restrictions on constant-expressions do not apply inside
15712 sizeof expressions. */
c8094d83 15713 saved_integral_constant_expression_p
93678513
MM
15714 = parser->integral_constant_expression_p;
15715 saved_non_integral_constant_expression_p
15716 = parser->non_integral_constant_expression_p;
67c03833 15717 parser->integral_constant_expression_p = false;
a723baf1 15718
3beb3abf
MM
15719 /* Do not actually evaluate the expression. */
15720 ++skip_evaluation;
a723baf1
MM
15721 /* If it's a `(', then we might be looking at the type-id
15722 construction. */
15723 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15724 {
15725 tree type;
4f8163b1 15726 bool saved_in_type_id_in_expr_p;
a723baf1
MM
15727
15728 /* We can't be sure yet whether we're looking at a type-id or an
15729 expression. */
15730 cp_parser_parse_tentatively (parser);
15731 /* Consume the `('. */
15732 cp_lexer_consume_token (parser->lexer);
15733 /* Parse the type-id. */
4f8163b1
MM
15734 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15735 parser->in_type_id_in_expr_p = true;
a723baf1 15736 type = cp_parser_type_id (parser);
4f8163b1 15737 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1 15738 /* Now, look for the trailing `)'. */
9e637a26 15739 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
a723baf1
MM
15740 /* If all went well, then we're done. */
15741 if (cp_parser_parse_definitely (parser))
15742 {
62d1db17
MM
15743 cp_decl_specifier_seq decl_specs;
15744
15745 /* Build a trivial decl-specifier-seq. */
15746 clear_decl_specs (&decl_specs);
15747 decl_specs.type = type;
a723baf1
MM
15748
15749 /* Call grokdeclarator to figure out what type this is. */
058b15c1 15750 expr = grokdeclarator (NULL,
62d1db17 15751 &decl_specs,
a723baf1
MM
15752 TYPENAME,
15753 /*initialized=*/0,
15754 /*attrlist=*/NULL);
15755 }
15756 }
15757
15758 /* If the type-id production did not work out, then we must be
15759 looking at the unary-expression production. */
15760 if (!expr)
93678513
MM
15761 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15762 /*cast_p=*/false);
3beb3abf
MM
15763 /* Go back to evaluating expressions. */
15764 --skip_evaluation;
a723baf1
MM
15765
15766 /* Free the message we created. */
15767 free ((char *) parser->type_definition_forbidden_message);
15768 /* And restore the old one. */
15769 parser->type_definition_forbidden_message = saved_message;
c8094d83 15770 parser->integral_constant_expression_p
93678513
MM
15771 = saved_integral_constant_expression_p;
15772 parser->non_integral_constant_expression_p
15773 = saved_non_integral_constant_expression_p;
a723baf1
MM
15774
15775 return expr;
15776}
15777
15778/* If the current declaration has no declarator, return true. */
15779
15780static bool
15781cp_parser_declares_only_class_p (cp_parser *parser)
15782{
21526606 15783 /* If the next token is a `;' or a `,' then there is no
a723baf1
MM
15784 declarator. */
15785 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15786 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15787}
15788
62d1db17 15789/* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
a723baf1 15790
62d1db17
MM
15791static void
15792cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15793 cp_storage_class storage_class)
a723baf1 15794{
62d1db17
MM
15795 if (decl_specs->storage_class != sc_none)
15796 decl_specs->multiple_storage_classes_p = true;
15797 else
15798 decl_specs->storage_class = storage_class;
15799}
15800
15801/* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15802 is true, the type is a user-defined type; otherwise it is a
15803 built-in type specified by a keyword. */
a723baf1 15804
62d1db17
MM
15805static void
15806cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15807 tree type_spec,
15808 bool user_defined_p)
15809{
15810 decl_specs->any_specifiers_p = true;
98ca843c 15811
9306cccb
MM
15812 /* If the user tries to redeclare bool or wchar_t (with, for
15813 example, in "typedef int wchar_t;") we remember that this is what
f84b6c96
MM
15814 happened. In system headers, we ignore these declarations so
15815 that G++ can work with system headers that are not C++-safe. */
98ca843c 15816 if (decl_specs->specs[(int) ds_typedef]
f84b6c96 15817 && !user_defined_p
9306cccb
MM
15818 && (type_spec == boolean_type_node
15819 || type_spec == wchar_type_node)
f84b6c96
MM
15820 && (decl_specs->type
15821 || decl_specs->specs[(int) ds_long]
15822 || decl_specs->specs[(int) ds_short]
15823 || decl_specs->specs[(int) ds_unsigned]
15824 || decl_specs->specs[(int) ds_signed]))
0a73e37f
MM
15825 {
15826 decl_specs->redefined_builtin_type = type_spec;
15827 if (!decl_specs->type)
15828 {
15829 decl_specs->type = type_spec;
15830 decl_specs->user_defined_type_p = false;
15831 }
15832 }
f84b6c96
MM
15833 else if (decl_specs->type)
15834 decl_specs->multiple_types_p = true;
62d1db17
MM
15835 else
15836 {
15837 decl_specs->type = type_spec;
15838 decl_specs->user_defined_type_p = user_defined_p;
0a73e37f 15839 decl_specs->redefined_builtin_type = NULL_TREE;
a723baf1 15840 }
62d1db17 15841}
a723baf1 15842
62d1db17
MM
15843/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15844 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15845
15846static bool
15847cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15848{
15849 return decl_specifiers->specs[(int) ds_friend] != 0;
a723baf1
MM
15850}
15851
15852/* If the next token is of the indicated TYPE, consume it. Otherwise,
15853 issue an error message indicating that TOKEN_DESC was expected.
21526606 15854
a723baf1
MM
15855 Returns the token consumed, if the token had the appropriate type.
15856 Otherwise, returns NULL. */
15857
15858static cp_token *
94edc4ab 15859cp_parser_require (cp_parser* parser,
0cbd7506
MS
15860 enum cpp_ttype type,
15861 const char* token_desc)
a723baf1
MM
15862{
15863 if (cp_lexer_next_token_is (parser->lexer, type))
15864 return cp_lexer_consume_token (parser->lexer);
15865 else
15866 {
e5976695
MM
15867 /* Output the MESSAGE -- unless we're parsing tentatively. */
15868 if (!cp_parser_simulate_error (parser))
216bb6e1
MM
15869 {
15870 char *message = concat ("expected ", token_desc, NULL);
15871 cp_parser_error (parser, message);
15872 free (message);
15873 }
a723baf1
MM
15874 return NULL;
15875 }
15876}
15877
15878/* Like cp_parser_require, except that tokens will be skipped until
15879 the desired token is found. An error message is still produced if
15880 the next token is not as expected. */
15881
15882static void
21526606 15883cp_parser_skip_until_found (cp_parser* parser,
0cbd7506
MS
15884 enum cpp_ttype type,
15885 const char* token_desc)
a723baf1
MM
15886{
15887 cp_token *token;
15888 unsigned nesting_depth = 0;
15889
15890 if (cp_parser_require (parser, type, token_desc))
15891 return;
15892
15893 /* Skip tokens until the desired token is found. */
15894 while (true)
15895 {
15896 /* Peek at the next token. */
15897 token = cp_lexer_peek_token (parser->lexer);
21526606 15898 /* If we've reached the token we want, consume it and
a723baf1
MM
15899 stop. */
15900 if (token->type == type && !nesting_depth)
15901 {
15902 cp_lexer_consume_token (parser->lexer);
15903 return;
15904 }
15905 /* If we've run out of tokens, stop. */
15906 if (token->type == CPP_EOF)
15907 return;
21526606 15908 if (token->type == CPP_OPEN_BRACE
a723baf1
MM
15909 || token->type == CPP_OPEN_PAREN
15910 || token->type == CPP_OPEN_SQUARE)
15911 ++nesting_depth;
21526606 15912 else if (token->type == CPP_CLOSE_BRACE
a723baf1
MM
15913 || token->type == CPP_CLOSE_PAREN
15914 || token->type == CPP_CLOSE_SQUARE)
15915 {
15916 if (nesting_depth-- == 0)
15917 return;
15918 }
15919 /* Consume this token. */
15920 cp_lexer_consume_token (parser->lexer);
15921 }
15922}
15923
15924/* If the next token is the indicated keyword, consume it. Otherwise,
15925 issue an error message indicating that TOKEN_DESC was expected.
21526606 15926
a723baf1
MM
15927 Returns the token consumed, if the token had the appropriate type.
15928 Otherwise, returns NULL. */
15929
15930static cp_token *
94edc4ab 15931cp_parser_require_keyword (cp_parser* parser,
0cbd7506
MS
15932 enum rid keyword,
15933 const char* token_desc)
a723baf1
MM
15934{
15935 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15936
15937 if (token && token->keyword != keyword)
15938 {
15939 dyn_string_t error_msg;
15940
15941 /* Format the error message. */
15942 error_msg = dyn_string_new (0);
15943 dyn_string_append_cstr (error_msg, "expected ");
15944 dyn_string_append_cstr (error_msg, token_desc);
15945 cp_parser_error (parser, error_msg->s);
15946 dyn_string_delete (error_msg);
15947 return NULL;
15948 }
15949
15950 return token;
15951}
15952
15953/* Returns TRUE iff TOKEN is a token that can begin the body of a
15954 function-definition. */
15955
21526606 15956static bool
94edc4ab 15957cp_parser_token_starts_function_definition_p (cp_token* token)
a723baf1
MM
15958{
15959 return (/* An ordinary function-body begins with an `{'. */
15960 token->type == CPP_OPEN_BRACE
15961 /* A ctor-initializer begins with a `:'. */
15962 || token->type == CPP_COLON
15963 /* A function-try-block begins with `try'. */
15964 || token->keyword == RID_TRY
15965 /* The named return value extension begins with `return'. */
15966 || token->keyword == RID_RETURN);
15967}
15968
15969/* Returns TRUE iff the next token is the ":" or "{" beginning a class
15970 definition. */
15971
15972static bool
15973cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15974{
15975 cp_token *token;
15976
15977 token = cp_lexer_peek_token (parser->lexer);
15978 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15979}
15980
d17811fd 15981/* Returns TRUE iff the next token is the "," or ">" ending a
03fd3f84 15982 template-argument. */
d17811fd
MM
15983
15984static bool
15985cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15986{
15987 cp_token *token;
15988
15989 token = cp_lexer_peek_token (parser->lexer);
391c4bc5 15990 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
d17811fd 15991}
f4abade9 15992
48b5c5c1 15993/* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
f4abade9
GB
15994 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15995
15996static bool
21526606 15997cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
f4abade9
GB
15998 size_t n)
15999{
16000 cp_token *token;
16001
16002 token = cp_lexer_peek_nth_token (parser->lexer, n);
16003 if (token->type == CPP_LESS)
16004 return true;
16005 /* Check for the sequence `<::' in the original code. It would be lexed as
16006 `[:', where `[' is a digraph, and there is no whitespace before
16007 `:'. */
16008 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16009 {
16010 cp_token *token2;
16011 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16012 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16013 return true;
16014 }
16015 return false;
16016}
21526606 16017
a723baf1
MM
16018/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16019 or none_type otherwise. */
16020
16021static enum tag_types
94edc4ab 16022cp_parser_token_is_class_key (cp_token* token)
a723baf1
MM
16023{
16024 switch (token->keyword)
16025 {
16026 case RID_CLASS:
16027 return class_type;
16028 case RID_STRUCT:
16029 return record_type;
16030 case RID_UNION:
16031 return union_type;
21526606 16032
a723baf1
MM
16033 default:
16034 return none_type;
16035 }
16036}
16037
16038/* Issue an error message if the CLASS_KEY does not match the TYPE. */
16039
16040static void
16041cp_parser_check_class_key (enum tag_types class_key, tree type)
16042{
16043 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
2a13a625 16044 pedwarn ("%qs tag used in naming %q#T",
a723baf1 16045 class_key == union_type ? "union"
21526606 16046 : class_key == record_type ? "struct" : "class",
a723baf1
MM
16047 type);
16048}
21526606 16049
cd0be382 16050/* Issue an error message if DECL is redeclared with different
37d407a1
KL
16051 access than its original declaration [class.access.spec/3].
16052 This applies to nested classes and nested class templates.
16053 [class.mem/1]. */
16054
2a13a625
GDR
16055static void
16056cp_parser_check_access_in_redeclaration (tree decl)
37d407a1
KL
16057{
16058 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16059 return;
16060
16061 if ((TREE_PRIVATE (decl)
16062 != (current_access_specifier == access_private_node))
16063 || (TREE_PROTECTED (decl)
16064 != (current_access_specifier == access_protected_node)))
2a13a625 16065 error ("%qD redeclared with different access", decl);
37d407a1
KL
16066}
16067
a723baf1 16068/* Look for the `template' keyword, as a syntactic disambiguator.
21526606 16069 Return TRUE iff it is present, in which case it will be
a723baf1
MM
16070 consumed. */
16071
16072static bool
16073cp_parser_optional_template_keyword (cp_parser *parser)
16074{
16075 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16076 {
16077 /* The `template' keyword can only be used within templates;
16078 outside templates the parser can always figure out what is a
16079 template and what is not. */
16080 if (!processing_template_decl)
16081 {
2a13a625 16082 error ("%<template%> (as a disambiguator) is only allowed "
a723baf1
MM
16083 "within templates");
16084 /* If this part of the token stream is rescanned, the same
16085 error message would be generated. So, we purge the token
16086 from the stream. */
16087 cp_lexer_purge_token (parser->lexer);
16088 return false;
16089 }
16090 else
16091 {
16092 /* Consume the `template' keyword. */
16093 cp_lexer_consume_token (parser->lexer);
16094 return true;
16095 }
16096 }
16097
16098 return false;
16099}
16100
2050a1bb
MM
16101/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16102 set PARSER->SCOPE, and perform other related actions. */
16103
16104static void
16105cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16106{
16107 tree value;
16108 tree check;
16109
16110 /* Get the stored value. */
16111 value = cp_lexer_consume_token (parser->lexer)->value;
16112 /* Perform any access checks that were deferred. */
16113 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c 16114 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
2050a1bb
MM
16115 /* Set the scope from the stored value. */
16116 parser->scope = TREE_VALUE (value);
16117 parser->qualifying_scope = TREE_TYPE (value);
16118 parser->object_scope = NULL_TREE;
16119}
16120
03fd3f84 16121/* Consume tokens up through a non-nested END token. */
a723baf1
MM
16122
16123static void
c162c75e
MA
16124cp_parser_cache_group (cp_parser *parser,
16125 enum cpp_ttype end,
16126 unsigned depth)
a723baf1
MM
16127{
16128 while (true)
16129 {
16130 cp_token *token;
16131
16132 /* Abort a parenthesized expression if we encounter a brace. */
16133 if ((end == CPP_CLOSE_PAREN || depth == 0)
16134 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16135 return;
a723baf1 16136 /* If we've reached the end of the file, stop. */
4bfb8bba 16137 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
a723baf1 16138 return;
4bfb8bba
MM
16139 /* Consume the next token. */
16140 token = cp_lexer_consume_token (parser->lexer);
a723baf1
MM
16141 /* See if it starts a new group. */
16142 if (token->type == CPP_OPEN_BRACE)
16143 {
c162c75e 16144 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
a723baf1
MM
16145 if (depth == 0)
16146 return;
16147 }
16148 else if (token->type == CPP_OPEN_PAREN)
c162c75e 16149 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
a723baf1
MM
16150 else if (token->type == end)
16151 return;
16152 }
16153}
16154
16155/* Begin parsing tentatively. We always save tokens while parsing
16156 tentatively so that if the tentative parsing fails we can restore the
16157 tokens. */
16158
16159static void
94edc4ab 16160cp_parser_parse_tentatively (cp_parser* parser)
a723baf1
MM
16161{
16162 /* Enter a new parsing context. */
16163 parser->context = cp_parser_context_new (parser->context);
16164 /* Begin saving tokens. */
16165 cp_lexer_save_tokens (parser->lexer);
16166 /* In order to avoid repetitive access control error messages,
16167 access checks are queued up until we are no longer parsing
16168 tentatively. */
8d241e0b 16169 push_deferring_access_checks (dk_deferred);
a723baf1
MM
16170}
16171
16172/* Commit to the currently active tentative parse. */
16173
16174static void
94edc4ab 16175cp_parser_commit_to_tentative_parse (cp_parser* parser)
a723baf1
MM
16176{
16177 cp_parser_context *context;
16178 cp_lexer *lexer;
16179
16180 /* Mark all of the levels as committed. */
16181 lexer = parser->lexer;
16182 for (context = parser->context; context->next; context = context->next)
16183 {
16184 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16185 break;
16186 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16187 while (!cp_lexer_saving_tokens (lexer))
16188 lexer = lexer->next;
16189 cp_lexer_commit_tokens (lexer);
16190 }
16191}
16192
16193/* Abort the currently active tentative parse. All consumed tokens
16194 will be rolled back, and no diagnostics will be issued. */
16195
16196static void
94edc4ab 16197cp_parser_abort_tentative_parse (cp_parser* parser)
a723baf1
MM
16198{
16199 cp_parser_simulate_error (parser);
16200 /* Now, pretend that we want to see if the construct was
16201 successfully parsed. */
16202 cp_parser_parse_definitely (parser);
16203}
16204
34cd5ae7 16205/* Stop parsing tentatively. If a parse error has occurred, restore the
a723baf1
MM
16206 token stream. Otherwise, commit to the tokens we have consumed.
16207 Returns true if no error occurred; false otherwise. */
16208
16209static bool
94edc4ab 16210cp_parser_parse_definitely (cp_parser* parser)
a723baf1
MM
16211{
16212 bool error_occurred;
16213 cp_parser_context *context;
16214
34cd5ae7 16215 /* Remember whether or not an error occurred, since we are about to
a723baf1
MM
16216 destroy that information. */
16217 error_occurred = cp_parser_error_occurred (parser);
16218 /* Remove the topmost context from the stack. */
16219 context = parser->context;
16220 parser->context = context->next;
16221 /* If no parse errors occurred, commit to the tentative parse. */
16222 if (!error_occurred)
16223 {
16224 /* Commit to the tokens read tentatively, unless that was
16225 already done. */
16226 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16227 cp_lexer_commit_tokens (parser->lexer);
cf22909c
KL
16228
16229 pop_to_parent_deferring_access_checks ();
a723baf1
MM
16230 }
16231 /* Otherwise, if errors occurred, roll back our state so that things
16232 are just as they were before we began the tentative parse. */
16233 else
cf22909c
KL
16234 {
16235 cp_lexer_rollback_tokens (parser->lexer);
16236 pop_deferring_access_checks ();
16237 }
e5976695
MM
16238 /* Add the context to the front of the free list. */
16239 context->next = cp_parser_context_free_list;
16240 cp_parser_context_free_list = context;
16241
16242 return !error_occurred;
a723baf1
MM
16243}
16244
0b16f8f4
VR
16245/* Returns true if we are parsing tentatively and are not committed to
16246 this tentative parse. */
a723baf1
MM
16247
16248static bool
0b16f8f4 16249cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
a723baf1
MM
16250{
16251 return (cp_parser_parsing_tentatively (parser)
0b16f8f4 16252 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
a723baf1
MM
16253}
16254
4de8668e 16255/* Returns nonzero iff an error has occurred during the most recent
a723baf1 16256 tentative parse. */
21526606 16257
a723baf1 16258static bool
94edc4ab 16259cp_parser_error_occurred (cp_parser* parser)
a723baf1
MM
16260{
16261 return (cp_parser_parsing_tentatively (parser)
16262 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16263}
16264
4de8668e 16265/* Returns nonzero if GNU extensions are allowed. */
a723baf1
MM
16266
16267static bool
94edc4ab 16268cp_parser_allow_gnu_extensions_p (cp_parser* parser)
a723baf1
MM
16269{
16270 return parser->allow_gnu_extensions_p;
16271}
e58a9aa1
ZL
16272\f
16273/* Objective-C++ Productions */
16274
16275
16276/* Parse an Objective-C expression, which feeds into a primary-expression
16277 above.
16278
16279 objc-expression:
16280 objc-message-expression
16281 objc-string-literal
16282 objc-encode-expression
16283 objc-protocol-expression
16284 objc-selector-expression
16285
16286 Returns a tree representation of the expression. */
16287
16288static tree
16289cp_parser_objc_expression (cp_parser* parser)
16290{
16291 /* Try to figure out what kind of declaration is present. */
16292 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16293
16294 switch (kwd->type)
16295 {
16296 case CPP_OPEN_SQUARE:
16297 return cp_parser_objc_message_expression (parser);
16298
16299 case CPP_OBJC_STRING:
16300 kwd = cp_lexer_consume_token (parser->lexer);
16301 return objc_build_string_object (kwd->value);
16302
16303 case CPP_KEYWORD:
16304 switch (kwd->keyword)
16305 {
16306 case RID_AT_ENCODE:
16307 return cp_parser_objc_encode_expression (parser);
16308
16309 case RID_AT_PROTOCOL:
16310 return cp_parser_objc_protocol_expression (parser);
16311
16312 case RID_AT_SELECTOR:
16313 return cp_parser_objc_selector_expression (parser);
16314
16315 default:
16316 break;
16317 }
16318 default:
c85ce869 16319 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
e58a9aa1
ZL
16320 cp_parser_skip_to_end_of_block_or_statement (parser);
16321 }
16322
16323 return error_mark_node;
16324}
16325
16326/* Parse an Objective-C message expression.
16327
16328 objc-message-expression:
16329 [ objc-message-receiver objc-message-args ]
16330
16331 Returns a representation of an Objective-C message. */
16332
16333static tree
16334cp_parser_objc_message_expression (cp_parser* parser)
16335{
16336 tree receiver, messageargs;
16337
16338 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16339 receiver = cp_parser_objc_message_receiver (parser);
16340 messageargs = cp_parser_objc_message_args (parser);
16341 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16342
16343 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16344}
16345
16346/* Parse an objc-message-receiver.
16347
16348 objc-message-receiver:
e58a9aa1 16349 expression
660845bf 16350 simple-type-specifier
e58a9aa1
ZL
16351
16352 Returns a representation of the type or expression. */
16353
16354static tree
16355cp_parser_objc_message_receiver (cp_parser* parser)
16356{
16357 tree rcv;
e58a9aa1
ZL
16358
16359 /* An Objective-C message receiver may be either (1) a type
16360 or (2) an expression. */
16361 cp_parser_parse_tentatively (parser);
16362 rcv = cp_parser_expression (parser, false);
16363
16364 if (cp_parser_parse_definitely (parser))
16365 return rcv;
16366
660845bf
ZL
16367 rcv = cp_parser_simple_type_specifier (parser,
16368 /*decl_specs=*/NULL,
16369 CP_PARSER_FLAGS_NONE);
e58a9aa1
ZL
16370
16371 return objc_get_class_reference (rcv);
16372}
16373
16374/* Parse the arguments and selectors comprising an Objective-C message.
16375
16376 objc-message-args:
16377 objc-selector
16378 objc-selector-args
16379 objc-selector-args , objc-comma-args
16380
16381 objc-selector-args:
16382 objc-selector [opt] : assignment-expression
16383 objc-selector-args objc-selector [opt] : assignment-expression
16384
16385 objc-comma-args:
16386 assignment-expression
16387 objc-comma-args , assignment-expression
16388
16389 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16390 selector arguments and TREE_VALUE containing a list of comma
16391 arguments. */
16392
16393static tree
16394cp_parser_objc_message_args (cp_parser* parser)
16395{
16396 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16397 bool maybe_unary_selector_p = true;
16398 cp_token *token = cp_lexer_peek_token (parser->lexer);
16399
16400 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16401 {
16402 tree selector = NULL_TREE, arg;
16403
16404 if (token->type != CPP_COLON)
16405 selector = cp_parser_objc_selector (parser);
16406
16407 /* Detect if we have a unary selector. */
16408 if (maybe_unary_selector_p
16409 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16410 return build_tree_list (selector, NULL_TREE);
16411
16412 maybe_unary_selector_p = false;
16413 cp_parser_require (parser, CPP_COLON, "`:'");
16414 arg = cp_parser_assignment_expression (parser, false);
16415
16416 sel_args
16417 = chainon (sel_args,
16418 build_tree_list (selector, arg));
16419
16420 token = cp_lexer_peek_token (parser->lexer);
16421 }
16422
16423 /* Handle non-selector arguments, if any. */
16424 while (token->type == CPP_COMMA)
16425 {
16426 tree arg;
16427
16428 cp_lexer_consume_token (parser->lexer);
16429 arg = cp_parser_assignment_expression (parser, false);
16430
16431 addl_args
16432 = chainon (addl_args,
16433 build_tree_list (NULL_TREE, arg));
16434
16435 token = cp_lexer_peek_token (parser->lexer);
16436 }
16437
16438 return build_tree_list (sel_args, addl_args);
16439}
16440
16441/* Parse an Objective-C encode expression.
16442
16443 objc-encode-expression:
16444 @encode objc-typename
c8094d83 16445
e58a9aa1
ZL
16446 Returns an encoded representation of the type argument. */
16447
16448static tree
16449cp_parser_objc_encode_expression (cp_parser* parser)
16450{
16451 tree type;
16452
16453 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16454 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16455 type = complete_type (cp_parser_type_id (parser));
16456 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16457
16458 if (!type)
16459 {
c85ce869 16460 error ("%<@encode%> must specify a type as an argument");
e58a9aa1
ZL
16461 return error_mark_node;
16462 }
16463
16464 return objc_build_encode_expr (type);
16465}
16466
16467/* Parse an Objective-C @defs expression. */
16468
16469static tree
16470cp_parser_objc_defs_expression (cp_parser *parser)
16471{
16472 tree name;
16473
16474 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16475 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16476 name = cp_parser_identifier (parser);
16477 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16478
16479 return objc_get_class_ivars (name);
16480}
16481
16482/* Parse an Objective-C protocol expression.
16483
16484 objc-protocol-expression:
16485 @protocol ( identifier )
16486
16487 Returns a representation of the protocol expression. */
16488
16489static tree
16490cp_parser_objc_protocol_expression (cp_parser* parser)
16491{
16492 tree proto;
16493
16494 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16495 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16496 proto = cp_parser_identifier (parser);
16497 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16498
16499 return objc_build_protocol_expr (proto);
16500}
16501
16502/* Parse an Objective-C selector expression.
16503
16504 objc-selector-expression:
16505 @selector ( objc-method-signature )
16506
16507 objc-method-signature:
16508 objc-selector
16509 objc-selector-seq
16510
16511 objc-selector-seq:
16512 objc-selector :
16513 objc-selector-seq objc-selector :
16514
16515 Returns a representation of the method selector. */
16516
16517static tree
16518cp_parser_objc_selector_expression (cp_parser* parser)
16519{
16520 tree sel_seq = NULL_TREE;
16521 bool maybe_unary_selector_p = true;
16522 cp_token *token;
16523
16524 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16525 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16526 token = cp_lexer_peek_token (parser->lexer);
16527
d95036e3
AP
16528 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16529 || token->type == CPP_SCOPE)
e58a9aa1
ZL
16530 {
16531 tree selector = NULL_TREE;
16532
d95036e3
AP
16533 if (token->type != CPP_COLON
16534 || token->type == CPP_SCOPE)
e58a9aa1
ZL
16535 selector = cp_parser_objc_selector (parser);
16536
d95036e3
AP
16537 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16538 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
e58a9aa1 16539 {
d95036e3
AP
16540 /* Detect if we have a unary selector. */
16541 if (maybe_unary_selector_p)
16542 {
16543 sel_seq = selector;
16544 goto finish_selector;
16545 }
16546 else
16547 {
16548 cp_parser_error (parser, "expected %<:%>");
16549 }
e58a9aa1 16550 }
e58a9aa1 16551 maybe_unary_selector_p = false;
d95036e3
AP
16552 token = cp_lexer_consume_token (parser->lexer);
16553
16554 if (token->type == CPP_SCOPE)
16555 {
16556 sel_seq
16557 = chainon (sel_seq,
16558 build_tree_list (selector, NULL_TREE));
16559 sel_seq
16560 = chainon (sel_seq,
16561 build_tree_list (NULL_TREE, NULL_TREE));
16562 }
16563 else
16564 sel_seq
16565 = chainon (sel_seq,
16566 build_tree_list (selector, NULL_TREE));
e58a9aa1
ZL
16567
16568 token = cp_lexer_peek_token (parser->lexer);
16569 }
16570
16571 finish_selector:
16572 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16573
16574 return objc_build_selector_expr (sel_seq);
16575}
16576
16577/* Parse a list of identifiers.
16578
16579 objc-identifier-list:
16580 identifier
16581 objc-identifier-list , identifier
16582
16583 Returns a TREE_LIST of identifier nodes. */
16584
16585static tree
16586cp_parser_objc_identifier_list (cp_parser* parser)
16587{
16588 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16589 cp_token *sep = cp_lexer_peek_token (parser->lexer);
16590
16591 while (sep->type == CPP_COMMA)
16592 {
16593 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
c8094d83 16594 list = chainon (list,
e58a9aa1
ZL
16595 build_tree_list (NULL_TREE,
16596 cp_parser_identifier (parser)));
16597 sep = cp_lexer_peek_token (parser->lexer);
16598 }
c8094d83 16599
e58a9aa1
ZL
16600 return list;
16601}
16602
16603/* Parse an Objective-C alias declaration.
16604
16605 objc-alias-declaration:
16606 @compatibility_alias identifier identifier ;
16607
16608 This function registers the alias mapping with the Objective-C front-end.
16609 It returns nothing. */
16610
16611static void
16612cp_parser_objc_alias_declaration (cp_parser* parser)
16613{
16614 tree alias, orig;
16615
16616 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
16617 alias = cp_parser_identifier (parser);
16618 orig = cp_parser_identifier (parser);
16619 objc_declare_alias (alias, orig);
16620 cp_parser_consume_semicolon_at_end_of_statement (parser);
16621}
16622
16623/* Parse an Objective-C class forward-declaration.
16624
16625 objc-class-declaration:
16626 @class objc-identifier-list ;
16627
16628 The function registers the forward declarations with the Objective-C
16629 front-end. It returns nothing. */
16630
16631static void
16632cp_parser_objc_class_declaration (cp_parser* parser)
16633{
16634 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
16635 objc_declare_class (cp_parser_objc_identifier_list (parser));
16636 cp_parser_consume_semicolon_at_end_of_statement (parser);
16637}
16638
16639/* Parse a list of Objective-C protocol references.
16640
16641 objc-protocol-refs-opt:
16642 objc-protocol-refs [opt]
16643
16644 objc-protocol-refs:
16645 < objc-identifier-list >
16646
16647 Returns a TREE_LIST of identifiers, if any. */
16648
16649static tree
16650cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16651{
16652 tree protorefs = NULL_TREE;
16653
16654 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16655 {
16656 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
16657 protorefs = cp_parser_objc_identifier_list (parser);
16658 cp_parser_require (parser, CPP_GREATER, "`>'");
16659 }
16660
16661 return protorefs;
16662}
16663
16664/* Parse a Objective-C visibility specification. */
16665
16666static void
16667cp_parser_objc_visibility_spec (cp_parser* parser)
16668{
16669 cp_token *vis = cp_lexer_peek_token (parser->lexer);
16670
16671 switch (vis->keyword)
16672 {
16673 case RID_AT_PRIVATE:
16674 objc_set_visibility (2);
16675 break;
16676 case RID_AT_PROTECTED:
16677 objc_set_visibility (0);
16678 break;
16679 case RID_AT_PUBLIC:
16680 objc_set_visibility (1);
16681 break;
16682 default:
16683 return;
16684 }
a723baf1 16685
e58a9aa1
ZL
16686 /* Eat '@private'/'@protected'/'@public'. */
16687 cp_lexer_consume_token (parser->lexer);
16688}
16689
16690/* Parse an Objective-C method type. */
16691
16692static void
16693cp_parser_objc_method_type (cp_parser* parser)
16694{
16695 objc_set_method_type
16696 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16697 ? PLUS_EXPR
16698 : MINUS_EXPR);
16699}
16700
16701/* Parse an Objective-C protocol qualifier. */
16702
16703static tree
16704cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16705{
16706 tree quals = NULL_TREE, node;
16707 cp_token *token = cp_lexer_peek_token (parser->lexer);
16708
16709 node = token->value;
16710
16711 while (node && TREE_CODE (node) == IDENTIFIER_NODE
16712 && (node == ridpointers [(int) RID_IN]
16713 || node == ridpointers [(int) RID_OUT]
16714 || node == ridpointers [(int) RID_INOUT]
16715 || node == ridpointers [(int) RID_BYCOPY]
0cbd7506 16716 || node == ridpointers [(int) RID_BYREF]
e58a9aa1
ZL
16717 || node == ridpointers [(int) RID_ONEWAY]))
16718 {
16719 quals = tree_cons (NULL_TREE, node, quals);
16720 cp_lexer_consume_token (parser->lexer);
16721 token = cp_lexer_peek_token (parser->lexer);
16722 node = token->value;
16723 }
16724
16725 return quals;
16726}
16727
16728/* Parse an Objective-C typename. */
16729
16730static tree
16731cp_parser_objc_typename (cp_parser* parser)
16732{
16733 tree typename = NULL_TREE;
16734
16735 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16736 {
16737 tree proto_quals, cp_type = NULL_TREE;
16738
16739 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
16740 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16741
16742 /* An ObjC type name may consist of just protocol qualifiers, in which
16743 case the type shall default to 'id'. */
16744 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16745 cp_type = cp_parser_type_id (parser);
16746
16747 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16748 typename = build_tree_list (proto_quals, cp_type);
16749 }
16750
16751 return typename;
16752}
16753
16754/* Check to see if TYPE refers to an Objective-C selector name. */
16755
16756static bool
16757cp_parser_objc_selector_p (enum cpp_ttype type)
16758{
16759 return (type == CPP_NAME || type == CPP_KEYWORD
16760 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
16761 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
16762 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
16763 || type == CPP_XOR || type == CPP_XOR_EQ);
16764}
16765
16766/* Parse an Objective-C selector. */
16767
16768static tree
16769cp_parser_objc_selector (cp_parser* parser)
16770{
16771 cp_token *token = cp_lexer_consume_token (parser->lexer);
c8094d83 16772
e58a9aa1
ZL
16773 if (!cp_parser_objc_selector_p (token->type))
16774 {
16775 error ("invalid Objective-C++ selector name");
16776 return error_mark_node;
16777 }
16778
16779 /* C++ operator names are allowed to appear in ObjC selectors. */
16780 switch (token->type)
16781 {
16782 case CPP_AND_AND: return get_identifier ("and");
16783 case CPP_AND_EQ: return get_identifier ("and_eq");
16784 case CPP_AND: return get_identifier ("bitand");
16785 case CPP_OR: return get_identifier ("bitor");
16786 case CPP_COMPL: return get_identifier ("compl");
16787 case CPP_NOT: return get_identifier ("not");
16788 case CPP_NOT_EQ: return get_identifier ("not_eq");
16789 case CPP_OR_OR: return get_identifier ("or");
16790 case CPP_OR_EQ: return get_identifier ("or_eq");
16791 case CPP_XOR: return get_identifier ("xor");
16792 case CPP_XOR_EQ: return get_identifier ("xor_eq");
16793 default: return token->value;
16794 }
16795}
16796
16797/* Parse an Objective-C params list. */
16798
16799static tree
16800cp_parser_objc_method_keyword_params (cp_parser* parser)
16801{
16802 tree params = NULL_TREE;
16803 bool maybe_unary_selector_p = true;
16804 cp_token *token = cp_lexer_peek_token (parser->lexer);
16805
16806 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16807 {
16808 tree selector = NULL_TREE, typename, identifier;
16809
16810 if (token->type != CPP_COLON)
16811 selector = cp_parser_objc_selector (parser);
16812
16813 /* Detect if we have a unary selector. */
16814 if (maybe_unary_selector_p
16815 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16816 return selector;
16817
16818 maybe_unary_selector_p = false;
16819 cp_parser_require (parser, CPP_COLON, "`:'");
16820 typename = cp_parser_objc_typename (parser);
16821 identifier = cp_parser_identifier (parser);
16822
16823 params
16824 = chainon (params,
c8094d83 16825 objc_build_keyword_decl (selector,
e58a9aa1
ZL
16826 typename,
16827 identifier));
16828
16829 token = cp_lexer_peek_token (parser->lexer);
16830 }
16831
16832 return params;
16833}
16834
16835/* Parse the non-keyword Objective-C params. */
16836
16837static tree
16838cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
16839{
16840 tree params = make_node (TREE_LIST);
16841 cp_token *token = cp_lexer_peek_token (parser->lexer);
16842 *ellipsisp = false; /* Initially, assume no ellipsis. */
16843
16844 while (token->type == CPP_COMMA)
16845 {
16846 cp_parameter_declarator *parmdecl;
16847 tree parm;
16848
16849 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
16850 token = cp_lexer_peek_token (parser->lexer);
16851
16852 if (token->type == CPP_ELLIPSIS)
16853 {
16854 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
16855 *ellipsisp = true;
16856 break;
16857 }
16858
16859 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
16860 parm = grokdeclarator (parmdecl->declarator,
16861 &parmdecl->decl_specifiers,
c8094d83 16862 PARM, /*initialized=*/0,
e58a9aa1
ZL
16863 /*attrlist=*/NULL);
16864
16865 chainon (params, build_tree_list (NULL_TREE, parm));
16866 token = cp_lexer_peek_token (parser->lexer);
16867 }
16868
16869 return params;
16870}
16871
16872/* Parse a linkage specification, a pragma, an extra semicolon or a block. */
16873
16874static void
16875cp_parser_objc_interstitial_code (cp_parser* parser)
16876{
16877 cp_token *token = cp_lexer_peek_token (parser->lexer);
16878
16879 /* If the next token is `extern' and the following token is a string
16880 literal, then we have a linkage specification. */
16881 if (token->keyword == RID_EXTERN
16882 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
16883 cp_parser_linkage_specification (parser);
16884 /* Handle #pragma, if any. */
16885 else if (token->type == CPP_PRAGMA)
16886 cp_lexer_handle_pragma (parser->lexer);
16887 /* Allow stray semicolons. */
16888 else if (token->type == CPP_SEMICOLON)
16889 cp_lexer_consume_token (parser->lexer);
16890 /* Finally, try to parse a block-declaration, or a function-definition. */
16891 else
16892 cp_parser_block_declaration (parser, /*statement_p=*/false);
16893}
16894
16895/* Parse a method signature. */
16896
16897static tree
16898cp_parser_objc_method_signature (cp_parser* parser)
16899{
16900 tree rettype, kwdparms, optparms;
16901 bool ellipsis = false;
16902
16903 cp_parser_objc_method_type (parser);
16904 rettype = cp_parser_objc_typename (parser);
16905 kwdparms = cp_parser_objc_method_keyword_params (parser);
16906 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
16907
16908 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
16909}
16910
16911/* Pars an Objective-C method prototype list. */
16912
16913static void
16914cp_parser_objc_method_prototype_list (cp_parser* parser)
16915{
16916 cp_token *token = cp_lexer_peek_token (parser->lexer);
16917
16918 while (token->keyword != RID_AT_END)
16919 {
16920 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
16921 {
16922 objc_add_method_declaration
16923 (cp_parser_objc_method_signature (parser));
16924 cp_parser_consume_semicolon_at_end_of_statement (parser);
16925 }
16926 else
16927 /* Allow for interspersed non-ObjC++ code. */
16928 cp_parser_objc_interstitial_code (parser);
16929
16930 token = cp_lexer_peek_token (parser->lexer);
16931 }
16932
16933 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
16934 objc_finish_interface ();
16935}
16936
16937/* Parse an Objective-C method definition list. */
16938
16939static void
16940cp_parser_objc_method_definition_list (cp_parser* parser)
16941{
16942 cp_token *token = cp_lexer_peek_token (parser->lexer);
16943
16944 while (token->keyword != RID_AT_END)
16945 {
16946 tree meth;
16947
16948 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
16949 {
16950 push_deferring_access_checks (dk_deferred);
16951 objc_start_method_definition
16952 (cp_parser_objc_method_signature (parser));
16953
16954 /* For historical reasons, we accept an optional semicolon. */
16955 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16956 cp_lexer_consume_token (parser->lexer);
16957
16958 perform_deferred_access_checks ();
16959 stop_deferring_access_checks ();
16960 meth = cp_parser_function_definition_after_declarator (parser,
16961 false);
16962 pop_deferring_access_checks ();
16963 objc_finish_method_definition (meth);
16964 }
16965 else
16966 /* Allow for interspersed non-ObjC++ code. */
16967 cp_parser_objc_interstitial_code (parser);
16968
16969 token = cp_lexer_peek_token (parser->lexer);
16970 }
16971
16972 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
16973 objc_finish_implementation ();
16974}
16975
16976/* Parse Objective-C ivars. */
16977
16978static void
16979cp_parser_objc_class_ivars (cp_parser* parser)
16980{
16981 cp_token *token = cp_lexer_peek_token (parser->lexer);
16982
16983 if (token->type != CPP_OPEN_BRACE)
16984 return; /* No ivars specified. */
16985
16986 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
16987 token = cp_lexer_peek_token (parser->lexer);
16988
16989 while (token->type != CPP_CLOSE_BRACE)
16990 {
16991 cp_decl_specifier_seq declspecs;
16992 int decl_class_or_enum_p;
16993 tree prefix_attributes;
16994
16995 cp_parser_objc_visibility_spec (parser);
16996
16997 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16998 break;
16999
17000 cp_parser_decl_specifier_seq (parser,
17001 CP_PARSER_FLAGS_OPTIONAL,
17002 &declspecs,
17003 &decl_class_or_enum_p);
17004 prefix_attributes = declspecs.attributes;
17005 declspecs.attributes = NULL_TREE;
17006
17007 /* Keep going until we hit the `;' at the end of the
17008 declaration. */
17009 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17010 {
17011 tree width = NULL_TREE, attributes, first_attribute, decl;
17012 cp_declarator *declarator = NULL;
17013 int ctor_dtor_or_conv_p;
17014
17015 /* Check for a (possibly unnamed) bitfield declaration. */
17016 token = cp_lexer_peek_token (parser->lexer);
17017 if (token->type == CPP_COLON)
17018 goto eat_colon;
17019
17020 if (token->type == CPP_NAME
17021 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17022 == CPP_COLON))
17023 {
17024 /* Get the name of the bitfield. */
17025 declarator = make_id_declarator (NULL_TREE,
17026 cp_parser_identifier (parser));
17027
17028 eat_colon:
17029 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17030 /* Get the width of the bitfield. */
17031 width
17032 = cp_parser_constant_expression (parser,
17033 /*allow_non_constant=*/false,
17034 NULL);
17035 }
17036 else
17037 {
17038 /* Parse the declarator. */
c8094d83 17039 declarator
e58a9aa1
ZL
17040 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17041 &ctor_dtor_or_conv_p,
17042 /*parenthesized_p=*/NULL,
17043 /*member_p=*/false);
17044 }
17045
17046 /* Look for attributes that apply to the ivar. */
17047 attributes = cp_parser_attributes_opt (parser);
17048 /* Remember which attributes are prefix attributes and
17049 which are not. */
17050 first_attribute = attributes;
17051 /* Combine the attributes. */
17052 attributes = chainon (prefix_attributes, attributes);
17053
17054 if (width)
17055 {
17056 /* Create the bitfield declaration. */
17057 decl = grokbitfield (declarator, &declspecs, width);
17058 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17059 }
17060 else
17061 decl = grokfield (declarator, &declspecs, NULL_TREE,
17062 NULL_TREE, attributes);
c8094d83 17063
e58a9aa1
ZL
17064 /* Add the instance variable. */
17065 objc_add_instance_variable (decl);
17066
17067 /* Reset PREFIX_ATTRIBUTES. */
17068 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17069 attributes = TREE_CHAIN (attributes);
17070 if (attributes)
17071 TREE_CHAIN (attributes) = NULL_TREE;
17072
17073 token = cp_lexer_peek_token (parser->lexer);
17074
17075 if (token->type == CPP_COMMA)
17076 {
17077 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17078 continue;
17079 }
17080 break;
17081 }
17082
17083 cp_parser_consume_semicolon_at_end_of_statement (parser);
17084 token = cp_lexer_peek_token (parser->lexer);
17085 }
17086
17087 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17088 /* For historical reasons, we accept an optional semicolon. */
17089 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17090 cp_lexer_consume_token (parser->lexer);
17091}
17092
17093/* Parse an Objective-C protocol declaration. */
17094
17095static void
17096cp_parser_objc_protocol_declaration (cp_parser* parser)
17097{
17098 tree proto, protorefs;
17099 cp_token *tok;
17100
17101 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17103 {
c85ce869 17104 error ("identifier expected after %<@protocol%>");
e58a9aa1
ZL
17105 goto finish;
17106 }
17107
128a79fb 17108 /* See if we have a forward declaration or a definition. */
e58a9aa1 17109 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
c8094d83 17110
e58a9aa1
ZL
17111 /* Try a forward declaration first. */
17112 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17113 {
17114 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
c8094d83 17115 finish:
e58a9aa1 17116 cp_parser_consume_semicolon_at_end_of_statement (parser);
c8094d83 17117 }
e58a9aa1
ZL
17118
17119 /* Ok, we got a full-fledged definition (or at least should). */
17120 else
17121 {
17122 proto = cp_parser_identifier (parser);
17123 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17124 objc_start_protocol (proto, protorefs);
17125 cp_parser_objc_method_prototype_list (parser);
17126 }
17127}
17128
17129/* Parse an Objective-C superclass or category. */
17130
17131static void
17132cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17133 tree *categ)
17134{
17135 cp_token *next = cp_lexer_peek_token (parser->lexer);
17136
17137 *super = *categ = NULL_TREE;
17138 if (next->type == CPP_COLON)
17139 {
17140 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17141 *super = cp_parser_identifier (parser);
17142 }
17143 else if (next->type == CPP_OPEN_PAREN)
17144 {
17145 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17146 *categ = cp_parser_identifier (parser);
17147 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17148 }
17149}
17150
17151/* Parse an Objective-C class interface. */
17152
17153static void
17154cp_parser_objc_class_interface (cp_parser* parser)
17155{
17156 tree name, super, categ, protos;
17157
17158 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17159 name = cp_parser_identifier (parser);
17160 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17161 protos = cp_parser_objc_protocol_refs_opt (parser);
17162
17163 /* We have either a class or a category on our hands. */
17164 if (categ)
17165 objc_start_category_interface (name, categ, protos);
17166 else
17167 {
17168 objc_start_class_interface (name, super, protos);
17169 /* Handle instance variable declarations, if any. */
17170 cp_parser_objc_class_ivars (parser);
17171 objc_continue_interface ();
17172 }
17173
17174 cp_parser_objc_method_prototype_list (parser);
17175}
17176
17177/* Parse an Objective-C class implementation. */
17178
17179static void
17180cp_parser_objc_class_implementation (cp_parser* parser)
17181{
17182 tree name, super, categ;
17183
17184 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17185 name = cp_parser_identifier (parser);
17186 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17187
17188 /* We have either a class or a category on our hands. */
17189 if (categ)
17190 objc_start_category_implementation (name, categ);
17191 else
17192 {
17193 objc_start_class_implementation (name, super);
17194 /* Handle instance variable declarations, if any. */
17195 cp_parser_objc_class_ivars (parser);
17196 objc_continue_implementation ();
17197 }
17198
17199 cp_parser_objc_method_definition_list (parser);
17200}
17201
17202/* Consume the @end token and finish off the implementation. */
17203
17204static void
17205cp_parser_objc_end_implementation (cp_parser* parser)
17206{
17207 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17208 objc_finish_implementation ();
17209}
17210
17211/* Parse an Objective-C declaration. */
17212
17213static void
17214cp_parser_objc_declaration (cp_parser* parser)
17215{
17216 /* Try to figure out what kind of declaration is present. */
17217 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17218
17219 switch (kwd->keyword)
17220 {
17221 case RID_AT_ALIAS:
17222 cp_parser_objc_alias_declaration (parser);
17223 break;
17224 case RID_AT_CLASS:
17225 cp_parser_objc_class_declaration (parser);
17226 break;
17227 case RID_AT_PROTOCOL:
17228 cp_parser_objc_protocol_declaration (parser);
17229 break;
17230 case RID_AT_INTERFACE:
17231 cp_parser_objc_class_interface (parser);
17232 break;
17233 case RID_AT_IMPLEMENTATION:
17234 cp_parser_objc_class_implementation (parser);
17235 break;
17236 case RID_AT_END:
17237 cp_parser_objc_end_implementation (parser);
17238 break;
17239 default:
c85ce869 17240 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
e58a9aa1
ZL
17241 cp_parser_skip_to_end_of_block_or_statement (parser);
17242 }
17243}
17244
17245/* Parse an Objective-C try-catch-finally statement.
17246
17247 objc-try-catch-finally-stmt:
17248 @try compound-statement objc-catch-clause-seq [opt]
17249 objc-finally-clause [opt]
17250
17251 objc-catch-clause-seq:
17252 objc-catch-clause objc-catch-clause-seq [opt]
17253
17254 objc-catch-clause:
17255 @catch ( exception-declaration ) compound-statement
17256
17257 objc-finally-clause
17258 @finally compound-statement
17259
17260 Returns NULL_TREE. */
17261
17262static tree
17263cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17264 location_t location;
17265 tree stmt;
17266
17267 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17268 location = cp_lexer_peek_token (parser->lexer)->location;
17269 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17270 node, lest it get absorbed into the surrounding block. */
17271 stmt = push_stmt_list ();
17272 cp_parser_compound_statement (parser, NULL, false);
17273 objc_begin_try_stmt (location, pop_stmt_list (stmt));
c8094d83 17274
e58a9aa1
ZL
17275 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17276 {
17277 cp_parameter_declarator *parmdecl;
17278 tree parm;
17279
17280 cp_lexer_consume_token (parser->lexer);
17281 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17282 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17283 parm = grokdeclarator (parmdecl->declarator,
17284 &parmdecl->decl_specifiers,
c8094d83 17285 PARM, /*initialized=*/0,
e58a9aa1
ZL
17286 /*attrlist=*/NULL);
17287 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17288 objc_begin_catch_clause (parm);
17289 cp_parser_compound_statement (parser, NULL, false);
17290 objc_finish_catch_clause ();
17291 }
17292
17293 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17294 {
17295 cp_lexer_consume_token (parser->lexer);
17296 location = cp_lexer_peek_token (parser->lexer)->location;
17297 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17298 node, lest it get absorbed into the surrounding block. */
17299 stmt = push_stmt_list ();
17300 cp_parser_compound_statement (parser, NULL, false);
17301 objc_build_finally_clause (location, pop_stmt_list (stmt));
17302 }
17303
17304 return objc_finish_try_stmt ();
17305}
17306
17307/* Parse an Objective-C synchronized statement.
17308
17309 objc-synchronized-stmt:
17310 @synchronized ( expression ) compound-statement
17311
17312 Returns NULL_TREE. */
17313
17314static tree
17315cp_parser_objc_synchronized_statement (cp_parser *parser) {
17316 location_t location;
17317 tree lock, stmt;
17318
17319 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17320
17321 location = cp_lexer_peek_token (parser->lexer)->location;
17322 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17323 lock = cp_parser_expression (parser, false);
17324 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17325
17326 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17327 node, lest it get absorbed into the surrounding block. */
17328 stmt = push_stmt_list ();
17329 cp_parser_compound_statement (parser, NULL, false);
17330
17331 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17332}
17333
17334/* Parse an Objective-C throw statement.
17335
17336 objc-throw-stmt:
17337 @throw assignment-expression [opt] ;
17338
17339 Returns a constructed '@throw' statement. */
17340
17341static tree
17342cp_parser_objc_throw_statement (cp_parser *parser) {
17343 tree expr = NULL_TREE;
17344
17345 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17346
17347 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17348 expr = cp_parser_assignment_expression (parser, false);
17349
17350 cp_parser_consume_semicolon_at_end_of_statement (parser);
17351
17352 return objc_build_throw_stmt (expr);
17353}
17354
17355/* Parse an Objective-C statement. */
17356
17357static tree
17358cp_parser_objc_statement (cp_parser * parser) {
17359 /* Try to figure out what kind of declaration is present. */
17360 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17361
17362 switch (kwd->keyword)
17363 {
17364 case RID_AT_TRY:
17365 return cp_parser_objc_try_catch_finally_statement (parser);
17366 case RID_AT_SYNCHRONIZED:
17367 return cp_parser_objc_synchronized_statement (parser);
17368 case RID_AT_THROW:
17369 return cp_parser_objc_throw_statement (parser);
17370 default:
c85ce869 17371 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
e58a9aa1
ZL
17372 cp_parser_skip_to_end_of_block_or_statement (parser);
17373 }
17374
17375 return error_mark_node;
17376}
a723baf1 17377\f
a723baf1
MM
17378/* The parser. */
17379
17380static GTY (()) cp_parser *the_parser;
17381
17382/* External interface. */
17383
d1bd0ded 17384/* Parse one entire translation unit. */
a723baf1 17385
d1bd0ded
GK
17386void
17387c_parse_file (void)
a723baf1
MM
17388{
17389 bool error_occurred;
f75fbaf7
ZW
17390 static bool already_called = false;
17391
17392 if (already_called)
17393 {
17394 sorry ("inter-module optimizations not implemented for C++");
17395 return;
17396 }
17397 already_called = true;
a723baf1
MM
17398
17399 the_parser = cp_parser_new ();
78757caa
KL
17400 push_deferring_access_checks (flag_access_control
17401 ? dk_no_deferred : dk_no_check);
a723baf1
MM
17402 error_occurred = cp_parser_translation_unit (the_parser);
17403 the_parser = NULL;
a723baf1
MM
17404}
17405
a723baf1
MM
17406/* This variable must be provided by every front end. */
17407
17408int yydebug;
17409
17410#include "gt-cp-parser.h"