]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
re-apply fold_indirect_ref patch
[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
a723baf1
MM
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
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"
a723baf1
MM
39
40\f
41/* The lexer. */
42
c162c75e
MA
43/* The cp_lexer_* routines mediate between the lexer proper (in libcpp
44 and c-lex.c) and the C++ parser. */
a723baf1
MM
45
46/* A C++ token. */
47
48typedef struct cp_token GTY (())
49{
50 /* The kind of token. */
df2b750f 51 ENUM_BITFIELD (cpp_ttype) type : 8;
a723baf1
MM
52 /* If this token is a keyword, this value indicates which keyword.
53 Otherwise, this value is RID_MAX. */
df2b750f 54 ENUM_BITFIELD (rid) keyword : 8;
f4abade9
GB
55 /* Token flags. */
56 unsigned char flags;
03fd3f84 57 /* True if this token is from a system header. */
c162c75e 58 BOOL_BITFIELD in_system_header : 1;
7d381002
MA
59 /* True if this token is from a context where it is implicitly extern "C" */
60 BOOL_BITFIELD implicit_extern_c : 1;
522df488
DN
61 /* The value associated with this token, if any. */
62 tree value;
82a98427
NS
63 /* The location at which this token was found. */
64 location_t location;
a723baf1
MM
65} cp_token;
66
0c5e4866
NS
67/* We use a stack of token pointer for saving token sets. */
68typedef struct cp_token *cp_token_position;
69DEF_VEC_MALLOC_P (cp_token_position);
70
76aebc9f
NS
71static const cp_token eof_token =
72{
73 CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
74#if USE_MAPPED_LOCATION
75 0
76#else
77 {0, 0}
78#endif
79};
0c5e4866 80
a723baf1
MM
81/* The cp_lexer structure represents the C++ lexer. It is responsible
82 for managing the token stream from the preprocessor and supplying
c162c75e 83 it to the parser. Tokens are never added to the cp_lexer after
03fd3f84 84 it is created. */
a723baf1
MM
85
86typedef struct cp_lexer GTY (())
87{
0c5e4866
NS
88 /* The memory allocated for the buffer. NULL if this lexer does not
89 own the token buffer. */
76aebc9f
NS
90 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
91 /* If the lexer owns the buffer, this is the number of tokens in the
92 buffer. */
93 size_t buffer_length;
0c5e4866 94
c162c75e 95 /* A pointer just past the last available token. The tokens
03fd3f84 96 in this lexer are [buffer, last_token). */
0c5e4866 97 cp_token_position GTY ((skip)) last_token;
c162c75e 98
0c5e4866 99 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
a723baf1 100 no more available tokens. */
0c5e4866 101 cp_token_position GTY ((skip)) next_token;
a723baf1
MM
102
103 /* A stack indicating positions at which cp_lexer_save_tokens was
104 called. The top entry is the most recent position at which we
0c5e4866
NS
105 began saving tokens. If the stack is non-empty, we are saving
106 tokens. */
107 VEC (cp_token_position) *GTY ((skip)) saved_tokens;
a723baf1 108
a723baf1
MM
109 /* True if we should output debugging information. */
110 bool debugging_p;
111
112 /* The next lexer in a linked list of lexers. */
113 struct cp_lexer *next;
114} cp_lexer;
115
c162c75e
MA
116/* cp_token_cache is a range of tokens. There is no need to represent
117 allocate heap memory for it, since tokens are never removed from the
118 lexer's array. There is also no need for the GC to walk through
119 a cp_token_cache, since everything in here is referenced through
03fd3f84 120 a lexer. */
c162c75e
MA
121
122typedef struct cp_token_cache GTY(())
123{
03fd3f84 124 /* The beginning of the token range. */
c162c75e
MA
125 cp_token * GTY((skip)) first;
126
03fd3f84 127 /* Points immediately after the last token in the range. */
c162c75e
MA
128 cp_token * GTY ((skip)) last;
129} cp_token_cache;
130
a723baf1
MM
131/* Prototypes. */
132
17211ab5 133static cp_lexer *cp_lexer_new_main
94edc4ab 134 (void);
a723baf1 135static cp_lexer *cp_lexer_new_from_tokens
c162c75e
MA
136 (cp_token_cache *tokens);
137static void cp_lexer_destroy
138 (cp_lexer *);
a723baf1 139static int cp_lexer_saving_tokens
94edc4ab 140 (const cp_lexer *);
0c5e4866
NS
141static cp_token_position cp_lexer_token_position
142 (cp_lexer *, bool);
143static cp_token *cp_lexer_token_at
144 (cp_lexer *, cp_token_position);
a723baf1 145static void cp_lexer_get_preprocessor_token
94edc4ab 146 (cp_lexer *, cp_token *);
c162c75e
MA
147static inline cp_token *cp_lexer_peek_token
148 (cp_lexer *);
a723baf1 149static cp_token *cp_lexer_peek_nth_token
94edc4ab 150 (cp_lexer *, size_t);
f7b5ecd9 151static inline bool cp_lexer_next_token_is
94edc4ab 152 (cp_lexer *, enum cpp_ttype);
a723baf1 153static bool cp_lexer_next_token_is_not
94edc4ab 154 (cp_lexer *, enum cpp_ttype);
a723baf1 155static bool cp_lexer_next_token_is_keyword
94edc4ab 156 (cp_lexer *, enum rid);
21526606 157static cp_token *cp_lexer_consume_token
94edc4ab 158 (cp_lexer *);
a723baf1
MM
159static void cp_lexer_purge_token
160 (cp_lexer *);
161static void cp_lexer_purge_tokens_after
0c5e4866 162 (cp_lexer *, cp_token_position);
c162c75e
MA
163static void cp_lexer_handle_pragma
164 (cp_lexer *);
a723baf1 165static void cp_lexer_save_tokens
94edc4ab 166 (cp_lexer *);
a723baf1 167static void cp_lexer_commit_tokens
94edc4ab 168 (cp_lexer *);
a723baf1 169static void cp_lexer_rollback_tokens
94edc4ab 170 (cp_lexer *);
6983ea08 171#ifdef ENABLE_CHECKING
a723baf1 172static void cp_lexer_print_token
94edc4ab 173 (FILE *, cp_token *);
21526606 174static inline bool cp_lexer_debugging_p
94edc4ab 175 (cp_lexer *);
a723baf1 176static void cp_lexer_start_debugging
94edc4ab 177 (cp_lexer *) ATTRIBUTE_UNUSED;
a723baf1 178static void cp_lexer_stop_debugging
94edc4ab 179 (cp_lexer *) ATTRIBUTE_UNUSED;
6983ea08 180#else
2cfe82fe
ZW
181/* If we define cp_lexer_debug_stream to NULL it will provoke warnings
182 about passing NULL to functions that require non-NULL arguments
183 (fputs, fprintf). It will never be used, so all we need is a value
184 of the right type that's guaranteed not to be NULL. */
185#define cp_lexer_debug_stream stdout
186#define cp_lexer_print_token(str, tok) (void) 0
6983ea08
MA
187#define cp_lexer_debugging_p(lexer) 0
188#endif /* ENABLE_CHECKING */
a723baf1 189
c162c75e
MA
190static cp_token_cache *cp_token_cache_new
191 (cp_token *, cp_token *);
192
a723baf1 193/* Manifest constants. */
c162c75e 194#define CP_LEXER_BUFFER_SIZE 10000
0c5e4866 195#define CP_SAVED_TOKEN_STACK 5
a723baf1
MM
196
197/* A token type for keywords, as opposed to ordinary identifiers. */
198#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
199
200/* A token type for template-ids. If a template-id is processed while
201 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
202 the value of the CPP_TEMPLATE_ID is whatever was returned by
203 cp_parser_template_id. */
204#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
205
206/* A token type for nested-name-specifiers. If a
207 nested-name-specifier is processed while parsing tentatively, it is
208 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
209 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
210 cp_parser_nested_name_specifier_opt. */
211#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
212
213/* A token type for tokens that are not tokens at all; these are used
c162c75e 214 to represent slots in the array where there used to be a token
03fd3f84 215 that has now been deleted. */
b8b94c5b
PB
216#define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
217
218/* The number of token types, including C++-specific ones. */
219#define N_CP_TTYPES ((int) (CPP_PURGED + 1))
a723baf1
MM
220
221/* Variables. */
222
6983ea08 223#ifdef ENABLE_CHECKING
a723baf1
MM
224/* The stream to which debugging output should be written. */
225static FILE *cp_lexer_debug_stream;
6983ea08 226#endif /* ENABLE_CHECKING */
a723baf1 227
17211ab5
GK
228/* Create a new main C++ lexer, the lexer that gets tokens from the
229 preprocessor. */
a723baf1
MM
230
231static cp_lexer *
17211ab5 232cp_lexer_new_main (void)
a723baf1 233{
17211ab5 234 cp_token first_token;
76aebc9f
NS
235 cp_lexer *lexer;
236 cp_token *pos;
237 size_t alloc;
238 size_t space;
239 cp_token *buffer;
17211ab5 240
3d8a8aad
MS
241 /* It's possible that lexing the first token will load a PCH file,
242 which is a GC collection point. So we have to grab the first
243 token before allocating any memory. Pragmas must not be deferred
244 as -fpch-preprocess can generate a pragma to load the PCH file in
245 the preprocessed output used by -save-temps. */
246 cp_lexer_get_preprocessor_token (NULL, &first_token);
247
03fd3f84 248 /* Tell cpplib we want CPP_PRAGMA tokens. */
c162c75e
MA
249 cpp_get_options (parse_in)->defer_pragmas = true;
250
251 /* Tell c_lex not to merge string constants. */
252 c_lex_return_raw_strings = true;
253
18c81520 254 c_common_no_more_pch ();
a723baf1
MM
255
256 /* Allocate the memory. */
99dd239f 257 lexer = GGC_CNEW (cp_lexer);
a723baf1 258
6983ea08 259#ifdef ENABLE_CHECKING
c162c75e 260 /* Initially we are not debugging. */
a723baf1 261 lexer->debugging_p = false;
6983ea08 262#endif /* ENABLE_CHECKING */
76aebc9f
NS
263 lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
264
265 /* Create the buffer. */
266 alloc = CP_LEXER_BUFFER_SIZE;
267 buffer = ggc_alloc (alloc * sizeof (cp_token));
a723baf1 268
76aebc9f
NS
269 /* Put the first token in the buffer. */
270 space = alloc;
271 pos = buffer;
272 *pos = first_token;
273
03fd3f84 274 /* Get the remaining tokens from the preprocessor. */
76aebc9f 275 while (pos->type != CPP_EOF)
c162c75e 276 {
76aebc9f
NS
277 pos++;
278 if (!--space)
279 {
280 space = alloc;
281 alloc *= 2;
282 buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
283 pos = buffer + space;
284 }
285 cp_lexer_get_preprocessor_token (lexer, pos);
c162c75e 286 }
76aebc9f
NS
287 lexer->buffer = buffer;
288 lexer->buffer_length = alloc - space;
289 lexer->last_token = pos;
290 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
c162c75e
MA
291
292 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
293 direct calls to c_lex. Those callers all expect c_lex to do
294 string constant concatenation. */
295 c_lex_return_raw_strings = false;
296
2cfe82fe 297 gcc_assert (lexer->next_token->type != CPP_PURGED);
a723baf1
MM
298 return lexer;
299}
300
c162c75e 301/* Create a new lexer whose token stream is primed with the tokens in
2cfe82fe 302 CACHE. When these tokens are exhausted, no new tokens will be read. */
a723baf1
MM
303
304static cp_lexer *
2cfe82fe 305cp_lexer_new_from_tokens (cp_token_cache *cache)
a723baf1 306{
2cfe82fe
ZW
307 cp_token *first = cache->first;
308 cp_token *last = cache->last;
c162c75e 309 cp_lexer *lexer = GGC_CNEW (cp_lexer);
17211ab5 310
0c5e4866 311 /* We do not own the buffer. */
76aebc9f
NS
312 lexer->buffer = NULL;
313 lexer->buffer_length = 0;
314 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
0c5e4866
NS
315 lexer->last_token = last;
316
317 lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
21526606 318
6983ea08 319#ifdef ENABLE_CHECKING
c162c75e 320 /* Initially we are not debugging. */
17211ab5 321 lexer->debugging_p = false;
c162c75e 322#endif
a723baf1 323
2cfe82fe
ZW
324 gcc_assert (lexer->next_token->type != CPP_PURGED);
325 return lexer;
c162c75e
MA
326}
327
03fd3f84 328/* Frees all resources associated with LEXER. */
c162c75e
MA
329
330static void
331cp_lexer_destroy (cp_lexer *lexer)
332{
0c5e4866
NS
333 if (lexer->buffer)
334 ggc_free (lexer->buffer);
335 VEC_free (cp_token_position, lexer->saved_tokens);
c162c75e
MA
336 ggc_free (lexer);
337}
338
4de8668e 339/* Returns nonzero if debugging information should be output. */
a723baf1 340
6983ea08
MA
341#ifdef ENABLE_CHECKING
342
f7b5ecd9
MM
343static inline bool
344cp_lexer_debugging_p (cp_lexer *lexer)
a723baf1 345{
f7b5ecd9
MM
346 return lexer->debugging_p;
347}
348
6983ea08
MA
349#endif /* ENABLE_CHECKING */
350
0c5e4866
NS
351static inline cp_token_position
352cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
a723baf1 353{
0c5e4866
NS
354 gcc_assert (!previous_p || lexer->next_token != &eof_token);
355
356 return lexer->next_token - previous_p;
a723baf1
MM
357}
358
a668c6ad 359static inline cp_token *
0c5e4866 360cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
a668c6ad 361{
0c5e4866 362 return pos;
a668c6ad
MM
363}
364
4de8668e 365/* nonzero if we are presently saving tokens. */
f7b5ecd9 366
0c5e4866 367static inline int
94edc4ab 368cp_lexer_saving_tokens (const cp_lexer* lexer)
f7b5ecd9 369{
0c5e4866 370 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
a723baf1
MM
371}
372
76aebc9f
NS
373/* Store the next token from the preprocessor in *TOKEN. Return true
374 if we reach EOF. */
a723baf1 375
21526606 376static void
94edc4ab
NN
377cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
378 cp_token *token)
a723baf1 379{
7d381002 380 static int is_extern_c = 0;
a723baf1 381
51e63e60
NS
382 /* Get a new token from the preprocessor. */
383 token->type = c_lex_with_flags (&token->value, &token->flags);
82a98427 384 token->location = input_location;
c162c75e 385 token->in_system_header = in_system_header;
a723baf1 386
7d381002
MA
387 /* On some systems, some header files are surrounded by an
388 implicit extern "C" block. Set a flag in the token if it
03fd3f84 389 comes from such a header. */
7d381002
MA
390 is_extern_c += pending_lang_change;
391 pending_lang_change = 0;
392 token->implicit_extern_c = is_extern_c > 0;
393
a723baf1 394 /* Check to see if this token is a keyword. */
21526606 395 if (token->type == CPP_NAME
a723baf1
MM
396 && C_IS_RESERVED_WORD (token->value))
397 {
398 /* Mark this token as a keyword. */
399 token->type = CPP_KEYWORD;
400 /* Record which keyword. */
401 token->keyword = C_RID_CODE (token->value);
402 /* Update the value. Some keywords are mapped to particular
403 entities, rather than simply having the value of the
404 corresponding IDENTIFIER_NODE. For example, `__const' is
405 mapped to `const'. */
406 token->value = ridpointers[token->keyword];
407 }
408 else
409 token->keyword = RID_MAX;
410}
411
03fd3f84 412/* Update the globals input_location and in_system_header from TOKEN. */
2cfe82fe
ZW
413static inline void
414cp_lexer_set_source_position_from_token (cp_token *token)
415{
416 if (token->type != CPP_EOF)
417 {
418 input_location = token->location;
419 in_system_header = token->in_system_header;
420 }
421}
422
a723baf1
MM
423/* Return a pointer to the next token in the token stream, but do not
424 consume it. */
425
c162c75e
MA
426static inline cp_token *
427cp_lexer_peek_token (cp_lexer *lexer)
a723baf1 428{
a723baf1 429 if (cp_lexer_debugging_p (lexer))
0c5e4866
NS
430 {
431 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
432 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
433 putc ('\n', cp_lexer_debug_stream);
434 }
2cfe82fe 435 return lexer->next_token;
a723baf1
MM
436}
437
438/* Return true if the next token has the indicated TYPE. */
439
2cfe82fe 440static inline bool
94edc4ab 441cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
a723baf1 442{
2cfe82fe 443 return cp_lexer_peek_token (lexer)->type == type;
a723baf1
MM
444}
445
446/* Return true if the next token does not have the indicated TYPE. */
447
2cfe82fe 448static inline bool
94edc4ab 449cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
450{
451 return !cp_lexer_next_token_is (lexer, type);
452}
453
454/* Return true if the next token is the indicated KEYWORD. */
455
2cfe82fe 456static inline bool
94edc4ab 457cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
a723baf1
MM
458{
459 cp_token *token;
460
461 /* Peek at the next token. */
462 token = cp_lexer_peek_token (lexer);
463 /* Check to see if it is the indicated keyword. */
464 return token->keyword == keyword;
465}
466
467/* Return a pointer to the Nth token in the token stream. If N is 1,
2cfe82fe
ZW
468 then this is precisely equivalent to cp_lexer_peek_token (except
469 that it is not inline). One would like to disallow that case, but
470 there is one case (cp_parser_nth_token_starts_template_id) where
471 the caller passes a variable for N and it might be 1. */
a723baf1
MM
472
473static cp_token *
94edc4ab 474cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
a723baf1
MM
475{
476 cp_token *token;
477
478 /* N is 1-based, not zero-based. */
0c5e4866 479 gcc_assert (n > 0 && lexer->next_token != &eof_token);
a723baf1 480
2cfe82fe
ZW
481 if (cp_lexer_debugging_p (lexer))
482 fprintf (cp_lexer_debug_stream,
483 "cp_lexer: peeking ahead %ld at token: ", (long)n);
484
c162c75e 485 --n;
a723baf1 486 token = lexer->next_token;
c162c75e 487 while (n != 0)
a723baf1 488 {
c162c75e 489 ++token;
0c5e4866
NS
490 if (token == lexer->last_token)
491 {
76aebc9f 492 token = (cp_token *)&eof_token;
0c5e4866
NS
493 break;
494 }
495
c162c75e
MA
496 if (token->type != CPP_PURGED)
497 --n;
a723baf1
MM
498 }
499
2cfe82fe
ZW
500 if (cp_lexer_debugging_p (lexer))
501 {
502 cp_lexer_print_token (cp_lexer_debug_stream, token);
503 putc ('\n', cp_lexer_debug_stream);
504 }
505
a723baf1
MM
506 return token;
507}
508
2cfe82fe
ZW
509/* Return the next token, and advance the lexer's next_token pointer
510 to point to the next non-purged token. */
a723baf1
MM
511
512static cp_token *
94edc4ab 513cp_lexer_consume_token (cp_lexer* lexer)
a723baf1 514{
2cfe82fe 515 cp_token *token = lexer->next_token;
a723baf1 516
0c5e4866
NS
517 gcc_assert (token != &eof_token);
518
2cfe82fe 519 do
0c5e4866
NS
520 {
521 lexer->next_token++;
522 if (lexer->next_token == lexer->last_token)
523 {
76aebc9f 524 lexer->next_token = (cp_token *)&eof_token;
0c5e4866
NS
525 break;
526 }
527
528 }
2cfe82fe 529 while (lexer->next_token->type == CPP_PURGED);
0c5e4866 530
2cfe82fe 531 cp_lexer_set_source_position_from_token (token);
0c5e4866 532
a723baf1
MM
533 /* Provide debugging output. */
534 if (cp_lexer_debugging_p (lexer))
535 {
2cfe82fe 536 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
a723baf1 537 cp_lexer_print_token (cp_lexer_debug_stream, token);
2cfe82fe 538 putc ('\n', cp_lexer_debug_stream);
a723baf1 539 }
0c5e4866 540
a723baf1
MM
541 return token;
542}
543
2cfe82fe
ZW
544/* Permanently remove the next token from the token stream, and
545 advance the next_token pointer to refer to the next non-purged
546 token. */
a723baf1
MM
547
548static void
549cp_lexer_purge_token (cp_lexer *lexer)
550{
c162c75e 551 cp_token *tok = lexer->next_token;
0c5e4866
NS
552
553 gcc_assert (tok != &eof_token);
c162c75e
MA
554 tok->type = CPP_PURGED;
555 tok->location = UNKNOWN_LOCATION;
556 tok->value = NULL_TREE;
557 tok->keyword = RID_MAX;
2cfe82fe
ZW
558
559 do
0c5e4866
NS
560 {
561 tok++;
562 if (tok == lexer->last_token)
563 {
76aebc9f 564 tok = (cp_token *)&eof_token;
0c5e4866
NS
565 break;
566 }
567 }
568 while (tok->type == CPP_PURGED);
569 lexer->next_token = tok;
a723baf1
MM
570}
571
c162c75e 572/* Permanently remove all tokens after TOK, up to, but not
a723baf1
MM
573 including, the token that will be returned next by
574 cp_lexer_peek_token. */
575
576static void
c162c75e 577cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
a723baf1 578{
0c5e4866 579 cp_token *peek = lexer->next_token;
a723baf1 580
0c5e4866
NS
581 if (peek == &eof_token)
582 peek = lexer->last_token;
583
c162c75e
MA
584 gcc_assert (tok < peek);
585
586 for ( tok += 1; tok != peek; tok += 1)
a723baf1 587 {
c162c75e
MA
588 tok->type = CPP_PURGED;
589 tok->location = UNKNOWN_LOCATION;
590 tok->value = NULL_TREE;
591 tok->keyword = RID_MAX;
a723baf1 592 }
c162c75e
MA
593}
594
03fd3f84 595/* Consume and handle a pragma token. */
c162c75e
MA
596static void
597cp_lexer_handle_pragma (cp_lexer *lexer)
598{
36952dea
ZW
599 cpp_string s;
600 cp_token *token = cp_lexer_consume_token (lexer);
601 gcc_assert (token->type == CPP_PRAGMA);
602 gcc_assert (token->value);
c162c75e 603
36952dea
ZW
604 s.len = TREE_STRING_LENGTH (token->value);
605 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
c162c75e 606
36952dea 607 cpp_handle_deferred_pragma (parse_in, &s);
c162c75e 608
36952dea
ZW
609 /* Clearing token->value here means that we will get an ICE if we
610 try to process this #pragma again (which should be impossible). */
611 token->value = NULL;
a723baf1
MM
612}
613
614/* Begin saving tokens. All tokens consumed after this point will be
615 preserved. */
616
617static void
94edc4ab 618cp_lexer_save_tokens (cp_lexer* lexer)
a723baf1
MM
619{
620 /* Provide debugging output. */
621 if (cp_lexer_debugging_p (lexer))
622 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
623
0c5e4866 624 VEC_safe_push (cp_token_position, lexer->saved_tokens, lexer->next_token);
a723baf1
MM
625}
626
627/* Commit to the portion of the token stream most recently saved. */
628
629static void
94edc4ab 630cp_lexer_commit_tokens (cp_lexer* lexer)
a723baf1
MM
631{
632 /* Provide debugging output. */
633 if (cp_lexer_debugging_p (lexer))
634 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
635
0c5e4866 636 VEC_pop (cp_token_position, lexer->saved_tokens);
a723baf1
MM
637}
638
639/* Return all tokens saved since the last call to cp_lexer_save_tokens
640 to the token stream. Stop saving tokens. */
641
642static void
94edc4ab 643cp_lexer_rollback_tokens (cp_lexer* lexer)
a723baf1 644{
a723baf1
MM
645 /* Provide debugging output. */
646 if (cp_lexer_debugging_p (lexer))
647 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
648
0c5e4866 649 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
a723baf1
MM
650}
651
a723baf1
MM
652/* Print a representation of the TOKEN on the STREAM. */
653
6983ea08
MA
654#ifdef ENABLE_CHECKING
655
a723baf1 656static void
c162c75e
MA
657cp_lexer_print_token (FILE * stream, cp_token *token)
658{
659 /* We don't use cpp_type2name here because the parser defines
660 a few tokens of its own. */
661 static const char *const token_names[] = {
662 /* cpplib-defined token types */
663#define OP(e, s) #e,
664#define TK(e, s) #e,
665 TTYPE_TABLE
666#undef OP
667#undef TK
668 /* C++ parser token types - see "Manifest constants", above. */
669 "KEYWORD",
670 "TEMPLATE_ID",
671 "NESTED_NAME_SPECIFIER",
672 "PURGED"
673 };
674
675 /* If we have a name for the token, print it out. Otherwise, we
676 simply give the numeric code. */
677 gcc_assert (token->type < ARRAY_SIZE(token_names));
678 fputs (token_names[token->type], stream);
a723baf1 679
c162c75e 680 /* For some tokens, print the associated data. */
a723baf1
MM
681 switch (token->type)
682 {
c162c75e
MA
683 case CPP_KEYWORD:
684 /* Some keywords have a value that is not an IDENTIFIER_NODE.
685 For example, `struct' is mapped to an INTEGER_CST. */
686 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
687 break;
688 /* else fall through */
a723baf1 689 case CPP_NAME:
c162c75e 690 fputs (IDENTIFIER_POINTER (token->value), stream);
a723baf1
MM
691 break;
692
c162c75e
MA
693 case CPP_STRING:
694 case CPP_WSTRING:
695 case CPP_PRAGMA:
696 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
a723baf1
MM
697 break;
698
a723baf1
MM
699 default:
700 break;
701 }
a723baf1
MM
702}
703
a723baf1
MM
704/* Start emitting debugging information. */
705
706static void
94edc4ab 707cp_lexer_start_debugging (cp_lexer* lexer)
a723baf1 708{
81122c44 709 lexer->debugging_p = true;
a723baf1 710}
21526606 711
a723baf1
MM
712/* Stop emitting debugging information. */
713
714static void
94edc4ab 715cp_lexer_stop_debugging (cp_lexer* lexer)
a723baf1 716{
81122c44 717 lexer->debugging_p = false;
a723baf1
MM
718}
719
6983ea08
MA
720#endif /* ENABLE_CHECKING */
721
03fd3f84 722/* Create a new cp_token_cache, representing a range of tokens. */
c162c75e
MA
723
724static cp_token_cache *
725cp_token_cache_new (cp_token *first, cp_token *last)
726{
727 cp_token_cache *cache = GGC_NEW (cp_token_cache);
728 cache->first = first;
729 cache->last = last;
730 return cache;
731}
732
a723baf1 733\f
62d1db17
MM
734/* Decl-specifiers. */
735
736static void clear_decl_specs
737 (cp_decl_specifier_seq *);
738
739/* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
740
741static void
742clear_decl_specs (cp_decl_specifier_seq *decl_specs)
743{
744 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
745}
746
058b15c1
MM
747/* Declarators. */
748
749/* Nothing other than the parser should be creating declarators;
750 declarators are a semi-syntactic representation of C++ entities.
751 Other parts of the front end that need to create entities (like
752 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
753
98ca843c 754static cp_declarator *make_call_declarator
3c01e5df 755 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
98ca843c 756static cp_declarator *make_array_declarator
058b15c1 757 (cp_declarator *, tree);
98ca843c 758static cp_declarator *make_pointer_declarator
3c01e5df 759 (cp_cv_quals, cp_declarator *);
98ca843c 760static cp_declarator *make_reference_declarator
3c01e5df 761 (cp_cv_quals, cp_declarator *);
98ca843c 762static cp_parameter_declarator *make_parameter_declarator
62d1db17 763 (cp_decl_specifier_seq *, cp_declarator *, tree);
98ca843c 764static cp_declarator *make_ptrmem_declarator
3c01e5df 765 (cp_cv_quals, tree, cp_declarator *);
058b15c1
MM
766
767cp_declarator *cp_error_declarator;
768
769/* The obstack on which declarators and related data structures are
770 allocated. */
771static struct obstack declarator_obstack;
772
773/* Alloc BYTES from the declarator memory pool. */
774
775static inline void *
776alloc_declarator (size_t bytes)
777{
778 return obstack_alloc (&declarator_obstack, bytes);
779}
780
781/* Allocate a declarator of the indicated KIND. Clear fields that are
782 common to all declarators. */
783
784static cp_declarator *
785make_declarator (cp_declarator_kind kind)
786{
787 cp_declarator *declarator;
788
789 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
790 declarator->kind = kind;
791 declarator->attributes = NULL_TREE;
792 declarator->declarator = NULL;
793
794 return declarator;
795}
796
1d786913
MM
797/* Make a declarator for a generalized identifier. If non-NULL, the
798 identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
799 just UNQUALIFIED_NAME. */
058b15c1 800
1d786913
MM
801static cp_declarator *
802make_id_declarator (tree qualifying_scope, tree unqualified_name)
058b15c1
MM
803{
804 cp_declarator *declarator;
98ca843c 805
1d786913
MM
806 /* It is valid to write:
807
808 class C { void f(); };
809 typedef C D;
810 void D::f();
811
812 The standard is not clear about whether `typedef const C D' is
813 legal; as of 2002-09-15 the committee is considering that
814 question. EDG 3.0 allows that syntax. Therefore, we do as
815 well. */
816 if (qualifying_scope && TYPE_P (qualifying_scope))
817 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
818
058b15c1 819 declarator = make_declarator (cdk_id);
1d786913
MM
820 declarator->u.id.qualifying_scope = qualifying_scope;
821 declarator->u.id.unqualified_name = unqualified_name;
058b15c1
MM
822 declarator->u.id.sfk = sfk_none;
823
824 return declarator;
825}
826
827/* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
828 of modifiers such as const or volatile to apply to the pointer
829 type, represented as identifiers. */
830
831cp_declarator *
3c01e5df 832make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
058b15c1
MM
833{
834 cp_declarator *declarator;
835
836 declarator = make_declarator (cdk_pointer);
837 declarator->declarator = target;
838 declarator->u.pointer.qualifiers = cv_qualifiers;
839 declarator->u.pointer.class_type = NULL_TREE;
840
841 return declarator;
842}
843
844/* Like make_pointer_declarator -- but for references. */
845
846cp_declarator *
3c01e5df 847make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
058b15c1
MM
848{
849 cp_declarator *declarator;
850
851 declarator = make_declarator (cdk_reference);
852 declarator->declarator = target;
853 declarator->u.pointer.qualifiers = cv_qualifiers;
854 declarator->u.pointer.class_type = NULL_TREE;
855
856 return declarator;
857}
858
859/* Like make_pointer_declarator -- but for a pointer to a non-static
860 member of CLASS_TYPE. */
861
862cp_declarator *
3c01e5df 863make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
058b15c1
MM
864 cp_declarator *pointee)
865{
866 cp_declarator *declarator;
867
868 declarator = make_declarator (cdk_ptrmem);
869 declarator->declarator = pointee;
870 declarator->u.pointer.qualifiers = cv_qualifiers;
871 declarator->u.pointer.class_type = class_type;
872
873 return declarator;
874}
875
876/* Make a declarator for the function given by TARGET, with the
877 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
878 "const"-qualified member function. The EXCEPTION_SPECIFICATION
879 indicates what exceptions can be thrown. */
880
881cp_declarator *
98ca843c 882make_call_declarator (cp_declarator *target,
058b15c1 883 cp_parameter_declarator *parms,
3c01e5df 884 cp_cv_quals cv_qualifiers,
058b15c1
MM
885 tree exception_specification)
886{
887 cp_declarator *declarator;
888
889 declarator = make_declarator (cdk_function);
890 declarator->declarator = target;
891 declarator->u.function.parameters = parms;
892 declarator->u.function.qualifiers = cv_qualifiers;
893 declarator->u.function.exception_specification = exception_specification;
894
895 return declarator;
896}
897
898/* Make a declarator for an array of BOUNDS elements, each of which is
899 defined by ELEMENT. */
900
901cp_declarator *
902make_array_declarator (cp_declarator *element, tree bounds)
903{
904 cp_declarator *declarator;
905
906 declarator = make_declarator (cdk_array);
907 declarator->declarator = element;
908 declarator->u.array.bounds = bounds;
909
910 return declarator;
911}
912
913cp_parameter_declarator *no_parameters;
914
915/* Create a parameter declarator with the indicated DECL_SPECIFIERS,
916 DECLARATOR and DEFAULT_ARGUMENT. */
917
918cp_parameter_declarator *
98ca843c 919make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
058b15c1
MM
920 cp_declarator *declarator,
921 tree default_argument)
922{
923 cp_parameter_declarator *parameter;
924
98ca843c 925 parameter = ((cp_parameter_declarator *)
058b15c1
MM
926 alloc_declarator (sizeof (cp_parameter_declarator)));
927 parameter->next = NULL;
62d1db17
MM
928 if (decl_specifiers)
929 parameter->decl_specifiers = *decl_specifiers;
930 else
931 clear_decl_specs (&parameter->decl_specifiers);
058b15c1
MM
932 parameter->declarator = declarator;
933 parameter->default_argument = default_argument;
934 parameter->ellipsis_p = false;
935
936 return parameter;
937}
938
a723baf1
MM
939/* The parser. */
940
941/* Overview
942 --------
943
944 A cp_parser parses the token stream as specified by the C++
945 grammar. Its job is purely parsing, not semantic analysis. For
946 example, the parser breaks the token stream into declarators,
947 expressions, statements, and other similar syntactic constructs.
948 It does not check that the types of the expressions on either side
949 of an assignment-statement are compatible, or that a function is
950 not declared with a parameter of type `void'.
951
952 The parser invokes routines elsewhere in the compiler to perform
953 semantic analysis and to build up the abstract syntax tree for the
21526606 954 code processed.
a723baf1
MM
955
956 The parser (and the template instantiation code, which is, in a
957 way, a close relative of parsing) are the only parts of the
958 compiler that should be calling push_scope and pop_scope, or
959 related functions. The parser (and template instantiation code)
960 keeps track of what scope is presently active; everything else
961 should simply honor that. (The code that generates static
962 initializers may also need to set the scope, in order to check
963 access control correctly when emitting the initializers.)
964
965 Methodology
966 -----------
21526606 967
a723baf1
MM
968 The parser is of the standard recursive-descent variety. Upcoming
969 tokens in the token stream are examined in order to determine which
970 production to use when parsing a non-terminal. Some C++ constructs
971 require arbitrary look ahead to disambiguate. For example, it is
972 impossible, in the general case, to tell whether a statement is an
973 expression or declaration without scanning the entire statement.
974 Therefore, the parser is capable of "parsing tentatively." When the
975 parser is not sure what construct comes next, it enters this mode.
976 Then, while we attempt to parse the construct, the parser queues up
977 error messages, rather than issuing them immediately, and saves the
978 tokens it consumes. If the construct is parsed successfully, the
979 parser "commits", i.e., it issues any queued error messages and
980 the tokens that were being preserved are permanently discarded.
981 If, however, the construct is not parsed successfully, the parser
982 rolls back its state completely so that it can resume parsing using
983 a different alternative.
984
985 Future Improvements
986 -------------------
21526606 987
b8b94c5b
PB
988 The performance of the parser could probably be improved substantially.
989 We could often eliminate the need to parse tentatively by looking ahead
990 a little bit. In some places, this approach might not entirely eliminate
991 the need to parse tentatively, but it might still speed up the average
992 case. */
a723baf1
MM
993
994/* Flags that are passed to some parsing functions. These values can
995 be bitwise-ored together. */
996
997typedef enum cp_parser_flags
998{
999 /* No flags. */
1000 CP_PARSER_FLAGS_NONE = 0x0,
1001 /* The construct is optional. If it is not present, then no error
1002 should be issued. */
1003 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1004 /* When parsing a type-specifier, do not allow user-defined types. */
1005 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1006} cp_parser_flags;
1007
62b8a44e
NS
1008/* The different kinds of declarators we want to parse. */
1009
1010typedef enum cp_parser_declarator_kind
1011{
852dcbdd 1012 /* We want an abstract declarator. */
62b8a44e
NS
1013 CP_PARSER_DECLARATOR_ABSTRACT,
1014 /* We want a named declarator. */
1015 CP_PARSER_DECLARATOR_NAMED,
04c06002 1016 /* We don't mind, but the name must be an unqualified-id. */
62b8a44e
NS
1017 CP_PARSER_DECLARATOR_EITHER
1018} cp_parser_declarator_kind;
1019
b8b94c5b
PB
1020/* The precedence values used to parse binary expressions. The minimum value
1021 of PREC must be 1, because zero is reserved to quickly discriminate
1022 binary operators from other tokens. */
a723baf1 1023
b8b94c5b 1024enum cp_parser_prec
a723baf1 1025{
b8b94c5b
PB
1026 PREC_NOT_OPERATOR,
1027 PREC_LOGICAL_OR_EXPRESSION,
1028 PREC_LOGICAL_AND_EXPRESSION,
1029 PREC_INCLUSIVE_OR_EXPRESSION,
1030 PREC_EXCLUSIVE_OR_EXPRESSION,
1031 PREC_AND_EXPRESSION,
b8b94c5b 1032 PREC_EQUALITY_EXPRESSION,
69475123 1033 PREC_RELATIONAL_EXPRESSION,
b8b94c5b
PB
1034 PREC_SHIFT_EXPRESSION,
1035 PREC_ADDITIVE_EXPRESSION,
1036 PREC_MULTIPLICATIVE_EXPRESSION,
1037 PREC_PM_EXPRESSION,
1038 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1039};
a723baf1 1040
b8b94c5b
PB
1041/* A mapping from a token type to a corresponding tree node type, with a
1042 precedence value. */
a723baf1 1043
b8b94c5b
PB
1044typedef struct cp_parser_binary_operations_map_node
1045{
1046 /* The token type. */
1047 enum cpp_ttype token_type;
1048 /* The corresponding tree code. */
1049 enum tree_code tree_type;
1050 /* The precedence of this operator. */
1051 enum cp_parser_prec prec;
1052} cp_parser_binary_operations_map_node;
a723baf1
MM
1053
1054/* The status of a tentative parse. */
1055
1056typedef enum cp_parser_status_kind
1057{
1058 /* No errors have occurred. */
1059 CP_PARSER_STATUS_KIND_NO_ERROR,
1060 /* An error has occurred. */
1061 CP_PARSER_STATUS_KIND_ERROR,
1062 /* We are committed to this tentative parse, whether or not an error
1063 has occurred. */
1064 CP_PARSER_STATUS_KIND_COMMITTED
1065} cp_parser_status_kind;
1066
b8b94c5b
PB
1067typedef struct cp_parser_expression_stack_entry
1068{
1069 tree lhs;
1070 enum tree_code tree_type;
1071 int prec;
1072} cp_parser_expression_stack_entry;
1073
43c2a69a
PB
1074/* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1075 entries because precedence levels on the stack are monotonically
1076 increasing. */
b8b94c5b
PB
1077typedef struct cp_parser_expression_stack_entry
1078 cp_parser_expression_stack[NUM_PREC_VALUES];
a723baf1 1079
b8b94c5b 1080/* Context that is saved and restored when parsing tentatively. */
a723baf1
MM
1081typedef struct cp_parser_context GTY (())
1082{
1083 /* If this is a tentative parsing context, the status of the
1084 tentative parse. */
1085 enum cp_parser_status_kind status;
1086 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1087 that are looked up in this context must be looked up both in the
1088 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1089 the context of the containing expression. */
1090 tree object_type;
b8b94c5b 1091
a723baf1
MM
1092 /* The next parsing context in the stack. */
1093 struct cp_parser_context *next;
1094} cp_parser_context;
1095
1096/* Prototypes. */
1097
1098/* Constructors and destructors. */
1099
1100static cp_parser_context *cp_parser_context_new
94edc4ab 1101 (cp_parser_context *);
a723baf1 1102
e5976695
MM
1103/* Class variables. */
1104
1431042e 1105static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
e5976695 1106
b8b94c5b
PB
1107/* The operator-precedence table used by cp_parser_binary_expression.
1108 Transformed into an associative array (binops_by_token) by
1109 cp_parser_new. */
1110
1111static const cp_parser_binary_operations_map_node binops[] = {
1112 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1113 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1114
1115 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1116 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1117 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1118
1119 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1120 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1121
1122 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1123 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1124
1125 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1126 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1127 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1128 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1129 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1130 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1131
1132 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1133 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1134
1135 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1136
1137 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1138
1139 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1140
1141 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1142
1143 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1144};
1145
1146/* The same as binops, but initialized by cp_parser_new so that
1147 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1148 for speed. */
1149static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1150
a723baf1
MM
1151/* Constructors and destructors. */
1152
1153/* Construct a new context. The context below this one on the stack
1154 is given by NEXT. */
1155
1156static cp_parser_context *
94edc4ab 1157cp_parser_context_new (cp_parser_context* next)
a723baf1
MM
1158{
1159 cp_parser_context *context;
1160
1161 /* Allocate the storage. */
e5976695
MM
1162 if (cp_parser_context_free_list != NULL)
1163 {
1164 /* Pull the first entry from the free list. */
1165 context = cp_parser_context_free_list;
1166 cp_parser_context_free_list = context->next;
c68b0a84 1167 memset (context, 0, sizeof (*context));
e5976695
MM
1168 }
1169 else
99dd239f 1170 context = GGC_CNEW (cp_parser_context);
b8b94c5b 1171
a723baf1
MM
1172 /* No errors have occurred yet in this context. */
1173 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1174 /* If this is not the bottomost context, copy information that we
1175 need from the previous context. */
1176 if (next)
1177 {
1178 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1179 expression, then we are parsing one in this context, too. */
1180 context->object_type = next->object_type;
a723baf1
MM
1181 /* Thread the stack. */
1182 context->next = next;
1183 }
1184
1185 return context;
1186}
1187
1188/* The cp_parser structure represents the C++ parser. */
1189
1190typedef struct cp_parser GTY(())
1191{
1192 /* The lexer from which we are obtaining tokens. */
1193 cp_lexer *lexer;
1194
1195 /* The scope in which names should be looked up. If NULL_TREE, then
1196 we look up names in the scope that is currently open in the
1197 source program. If non-NULL, this is either a TYPE or
21526606 1198 NAMESPACE_DECL for the scope in which we should look.
a723baf1
MM
1199
1200 This value is not cleared automatically after a name is looked
1201 up, so we must be careful to clear it before starting a new look
1202 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1203 will look up `Z' in the scope of `X', rather than the current
1204 scope.) Unfortunately, it is difficult to tell when name lookup
1205 is complete, because we sometimes peek at a token, look it up,
1206 and then decide not to consume it. */
1207 tree scope;
1208
1209 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1210 last lookup took place. OBJECT_SCOPE is used if an expression
1211 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
21526606 1212 respectively. QUALIFYING_SCOPE is used for an expression of the
a723baf1
MM
1213 form "X::Y"; it refers to X. */
1214 tree object_scope;
1215 tree qualifying_scope;
1216
1217 /* A stack of parsing contexts. All but the bottom entry on the
1218 stack will be tentative contexts.
1219
1220 We parse tentatively in order to determine which construct is in
1221 use in some situations. For example, in order to determine
1222 whether a statement is an expression-statement or a
1223 declaration-statement we parse it tentatively as a
1224 declaration-statement. If that fails, we then reparse the same
1225 token stream as an expression-statement. */
1226 cp_parser_context *context;
1227
1228 /* True if we are parsing GNU C++. If this flag is not set, then
1229 GNU extensions are not recognized. */
1230 bool allow_gnu_extensions_p;
1231
1232 /* TRUE if the `>' token should be interpreted as the greater-than
1233 operator. FALSE if it is the end of a template-id or
1234 template-parameter-list. */
1235 bool greater_than_is_operator_p;
1236
1237 /* TRUE if default arguments are allowed within a parameter list
1238 that starts at this point. FALSE if only a gnu extension makes
cd0be382 1239 them permissible. */
a723baf1 1240 bool default_arg_ok_p;
21526606 1241
a723baf1
MM
1242 /* TRUE if we are parsing an integral constant-expression. See
1243 [expr.const] for a precise definition. */
67c03833 1244 bool integral_constant_expression_p;
a723baf1 1245
14d22dd6
MM
1246 /* TRUE if we are parsing an integral constant-expression -- but a
1247 non-constant expression should be permitted as well. This flag
1248 is used when parsing an array bound so that GNU variable-length
1249 arrays are tolerated. */
67c03833 1250 bool allow_non_integral_constant_expression_p;
14d22dd6
MM
1251
1252 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1253 been seen that makes the expression non-constant. */
67c03833 1254 bool non_integral_constant_expression_p;
14d22dd6 1255
a723baf1
MM
1256 /* TRUE if local variable names and `this' are forbidden in the
1257 current context. */
1258 bool local_variables_forbidden_p;
1259
1260 /* TRUE if the declaration we are parsing is part of a
1261 linkage-specification of the form `extern string-literal
1262 declaration'. */
1263 bool in_unbraced_linkage_specification_p;
1264
1265 /* TRUE if we are presently parsing a declarator, after the
1266 direct-declarator. */
1267 bool in_declarator_p;
1268
4bb8ca28
MM
1269 /* TRUE if we are presently parsing a template-argument-list. */
1270 bool in_template_argument_list_p;
1271
0e59b3fb
MM
1272 /* TRUE if we are presently parsing the body of an
1273 iteration-statement. */
1274 bool in_iteration_statement_p;
1275
1276 /* TRUE if we are presently parsing the body of a switch
1277 statement. */
1278 bool in_switch_statement_p;
1279
4f8163b1
MM
1280 /* TRUE if we are parsing a type-id in an expression context. In
1281 such a situation, both "type (expr)" and "type (type)" are valid
1282 alternatives. */
1283 bool in_type_id_in_expr_p;
1284
7d381002 1285 /* TRUE if we are currently in a header file where declarations are
03fd3f84 1286 implicitly extern "C". */
7d381002
MA
1287 bool implicit_extern_c;
1288
c162c75e
MA
1289 /* TRUE if strings in expressions should be translated to the execution
1290 character set. */
1291 bool translate_strings_p;
1292
a723baf1
MM
1293 /* If non-NULL, then we are parsing a construct where new type
1294 definitions are not permitted. The string stored here will be
1295 issued as an error message if a type is defined. */
1296 const char *type_definition_forbidden_message;
1297
8db1028e
NS
1298 /* A list of lists. The outer list is a stack, used for member
1299 functions of local classes. At each level there are two sub-list,
1300 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1301 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1302 TREE_VALUE's. The functions are chained in reverse declaration
1303 order.
1304
1305 The TREE_PURPOSE sublist contains those functions with default
1306 arguments that need post processing, and the TREE_VALUE sublist
1307 contains those functions with definitions that need post
1308 processing.
1309
1310 These lists can only be processed once the outermost class being
9bcb9aae 1311 defined is complete. */
a723baf1
MM
1312 tree unparsed_functions_queues;
1313
1314 /* The number of classes whose definitions are currently in
1315 progress. */
1316 unsigned num_classes_being_defined;
1317
1318 /* The number of template parameter lists that apply directly to the
1319 current declaration. */
1320 unsigned num_template_parameter_lists;
1321} cp_parser;
1322
04c06002 1323/* The type of a function that parses some kind of expression. */
94edc4ab 1324typedef tree (*cp_parser_expression_fn) (cp_parser *);
a723baf1
MM
1325
1326/* Prototypes. */
1327
1328/* Constructors and destructors. */
1329
1330static cp_parser *cp_parser_new
94edc4ab 1331 (void);
a723baf1 1332
21526606 1333/* Routines to parse various constructs.
a723baf1
MM
1334
1335 Those that return `tree' will return the error_mark_node (rather
1336 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1337 Sometimes, they will return an ordinary node if error-recovery was
34cd5ae7 1338 attempted, even though a parse error occurred. So, to check
a723baf1
MM
1339 whether or not a parse error occurred, you should always use
1340 cp_parser_error_occurred. If the construct is optional (indicated
1341 either by an `_opt' in the name of the function that does the
1342 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1343 the construct is not present. */
1344
1345/* Lexical conventions [gram.lex] */
1346
1347static tree cp_parser_identifier
94edc4ab 1348 (cp_parser *);
c162c75e
MA
1349static tree cp_parser_string_literal
1350 (cp_parser *, bool, bool);
a723baf1
MM
1351
1352/* Basic concepts [gram.basic] */
1353
1354static bool cp_parser_translation_unit
94edc4ab 1355 (cp_parser *);
a723baf1
MM
1356
1357/* Expressions [gram.expr] */
1358
1359static tree cp_parser_primary_expression
93678513 1360 (cp_parser *, bool, cp_id_kind *, tree *);
a723baf1 1361static tree cp_parser_id_expression
f3c2dfc6 1362 (cp_parser *, bool, bool, bool *, bool);
a723baf1 1363static tree cp_parser_unqualified_id
f3c2dfc6 1364 (cp_parser *, bool, bool, bool);
a723baf1 1365static tree cp_parser_nested_name_specifier_opt
a668c6ad 1366 (cp_parser *, bool, bool, bool, bool);
a723baf1 1367static tree cp_parser_nested_name_specifier
a723baf1 1368 (cp_parser *, bool, bool, bool, bool);
a668c6ad
MM
1369static tree cp_parser_class_or_namespace_name
1370 (cp_parser *, bool, bool, bool, bool, bool);
a723baf1 1371static tree cp_parser_postfix_expression
93678513 1372 (cp_parser *, bool, bool);
7a3ea201
RH
1373static tree cp_parser_postfix_open_square_expression
1374 (cp_parser *, tree, bool);
1375static tree cp_parser_postfix_dot_deref_expression
1376 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
7efa3e22 1377static tree cp_parser_parenthesized_expression_list
93678513 1378 (cp_parser *, bool, bool, bool *);
a723baf1 1379static void cp_parser_pseudo_destructor_name
94edc4ab 1380 (cp_parser *, tree *, tree *);
a723baf1 1381static tree cp_parser_unary_expression
93678513 1382 (cp_parser *, bool, bool);
a723baf1 1383static enum tree_code cp_parser_unary_operator
94edc4ab 1384 (cp_token *);
a723baf1 1385static tree cp_parser_new_expression
94edc4ab 1386 (cp_parser *);
a723baf1 1387static tree cp_parser_new_placement
94edc4ab 1388 (cp_parser *);
a723baf1 1389static tree cp_parser_new_type_id
058b15c1
MM
1390 (cp_parser *, tree *);
1391static cp_declarator *cp_parser_new_declarator_opt
94edc4ab 1392 (cp_parser *);
058b15c1 1393static cp_declarator *cp_parser_direct_new_declarator
94edc4ab 1394 (cp_parser *);
a723baf1 1395static tree cp_parser_new_initializer
94edc4ab 1396 (cp_parser *);
a723baf1 1397static tree cp_parser_delete_expression
94edc4ab 1398 (cp_parser *);
21526606 1399static tree cp_parser_cast_expression
93678513 1400 (cp_parser *, bool, bool);
b8b94c5b 1401static tree cp_parser_binary_expression
93678513 1402 (cp_parser *, bool);
a723baf1 1403static tree cp_parser_question_colon_clause
94edc4ab 1404 (cp_parser *, tree);
a723baf1 1405static tree cp_parser_assignment_expression
93678513 1406 (cp_parser *, bool);
a723baf1 1407static enum tree_code cp_parser_assignment_operator_opt
94edc4ab 1408 (cp_parser *);
a723baf1 1409static tree cp_parser_expression
93678513 1410 (cp_parser *, bool);
a723baf1 1411static tree cp_parser_constant_expression
14d22dd6 1412 (cp_parser *, bool, bool *);
7a3ea201
RH
1413static tree cp_parser_builtin_offsetof
1414 (cp_parser *);
a723baf1
MM
1415
1416/* Statements [gram.stmt.stmt] */
1417
1418static void cp_parser_statement
325c3691 1419 (cp_parser *, tree);
a723baf1 1420static tree cp_parser_labeled_statement
325c3691 1421 (cp_parser *, tree);
a723baf1 1422static tree cp_parser_expression_statement
325c3691 1423 (cp_parser *, tree);
a723baf1 1424static tree cp_parser_compound_statement
325c3691 1425 (cp_parser *, tree, bool);
a723baf1 1426static void cp_parser_statement_seq_opt
325c3691 1427 (cp_parser *, tree);
a723baf1 1428static tree cp_parser_selection_statement
94edc4ab 1429 (cp_parser *);
a723baf1 1430static tree cp_parser_condition
94edc4ab 1431 (cp_parser *);
a723baf1 1432static tree cp_parser_iteration_statement
94edc4ab 1433 (cp_parser *);
a723baf1 1434static void cp_parser_for_init_statement
94edc4ab 1435 (cp_parser *);
a723baf1 1436static tree cp_parser_jump_statement
94edc4ab 1437 (cp_parser *);
a723baf1 1438static void cp_parser_declaration_statement
94edc4ab 1439 (cp_parser *);
a723baf1
MM
1440
1441static tree cp_parser_implicitly_scoped_statement
94edc4ab 1442 (cp_parser *);
a723baf1 1443static void cp_parser_already_scoped_statement
94edc4ab 1444 (cp_parser *);
a723baf1
MM
1445
1446/* Declarations [gram.dcl.dcl] */
1447
1448static void cp_parser_declaration_seq_opt
94edc4ab 1449 (cp_parser *);
a723baf1 1450static void cp_parser_declaration
94edc4ab 1451 (cp_parser *);
a723baf1 1452static void cp_parser_block_declaration
94edc4ab 1453 (cp_parser *, bool);
a723baf1 1454static void cp_parser_simple_declaration
94edc4ab 1455 (cp_parser *, bool);
62d1db17
MM
1456static void cp_parser_decl_specifier_seq
1457 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
a723baf1 1458static tree cp_parser_storage_class_specifier_opt
94edc4ab 1459 (cp_parser *);
a723baf1 1460static tree cp_parser_function_specifier_opt
62d1db17 1461 (cp_parser *, cp_decl_specifier_seq *);
a723baf1 1462static tree cp_parser_type_specifier
98ca843c 1463 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
62d1db17 1464 int *, bool *);
a723baf1 1465static tree cp_parser_simple_type_specifier
62d1db17 1466 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
a723baf1 1467static tree cp_parser_type_name
94edc4ab 1468 (cp_parser *);
a723baf1 1469static tree cp_parser_elaborated_type_specifier
94edc4ab 1470 (cp_parser *, bool, bool);
a723baf1 1471static tree cp_parser_enum_specifier
94edc4ab 1472 (cp_parser *);
a723baf1 1473static void cp_parser_enumerator_list
94edc4ab 1474 (cp_parser *, tree);
21526606 1475static void cp_parser_enumerator_definition
94edc4ab 1476 (cp_parser *, tree);
a723baf1 1477static tree cp_parser_namespace_name
94edc4ab 1478 (cp_parser *);
a723baf1 1479static void cp_parser_namespace_definition
94edc4ab 1480 (cp_parser *);
a723baf1 1481static void cp_parser_namespace_body
94edc4ab 1482 (cp_parser *);
a723baf1 1483static tree cp_parser_qualified_namespace_specifier
94edc4ab 1484 (cp_parser *);
a723baf1 1485static void cp_parser_namespace_alias_definition
94edc4ab 1486 (cp_parser *);
a723baf1 1487static void cp_parser_using_declaration
94edc4ab 1488 (cp_parser *);
a723baf1 1489static void cp_parser_using_directive
94edc4ab 1490 (cp_parser *);
a723baf1 1491static void cp_parser_asm_definition
94edc4ab 1492 (cp_parser *);
a723baf1 1493static void cp_parser_linkage_specification
94edc4ab 1494 (cp_parser *);
a723baf1
MM
1495
1496/* Declarators [gram.dcl.decl] */
1497
1498static tree cp_parser_init_declarator
62d1db17 1499 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
058b15c1 1500static cp_declarator *cp_parser_declarator
db86dd14 1501 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
058b15c1 1502static cp_declarator *cp_parser_direct_declarator
db86dd14 1503 (cp_parser *, cp_parser_declarator_kind, int *, bool);
a723baf1 1504static enum tree_code cp_parser_ptr_operator
3c01e5df
MM
1505 (cp_parser *, tree *, cp_cv_quals *);
1506static cp_cv_quals cp_parser_cv_qualifier_seq_opt
94edc4ab 1507 (cp_parser *);
a723baf1 1508static tree cp_parser_declarator_id
94edc4ab 1509 (cp_parser *);
a723baf1 1510static tree cp_parser_type_id
94edc4ab 1511 (cp_parser *);
62d1db17
MM
1512static void cp_parser_type_specifier_seq
1513 (cp_parser *, cp_decl_specifier_seq *);
058b15c1 1514static cp_parameter_declarator *cp_parser_parameter_declaration_clause
94edc4ab 1515 (cp_parser *);
058b15c1
MM
1516static cp_parameter_declarator *cp_parser_parameter_declaration_list
1517 (cp_parser *, bool *);
1518static cp_parameter_declarator *cp_parser_parameter_declaration
4bb8ca28 1519 (cp_parser *, bool, bool *);
a723baf1
MM
1520static void cp_parser_function_body
1521 (cp_parser *);
1522static tree cp_parser_initializer
39703eb9 1523 (cp_parser *, bool *, bool *);
a723baf1 1524static tree cp_parser_initializer_clause
39703eb9 1525 (cp_parser *, bool *);
a723baf1 1526static tree cp_parser_initializer_list
39703eb9 1527 (cp_parser *, bool *);
a723baf1
MM
1528
1529static bool cp_parser_ctor_initializer_opt_and_function_body
1530 (cp_parser *);
1531
1532/* Classes [gram.class] */
1533
1534static tree cp_parser_class_name
fc6a28d7 1535 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
a723baf1 1536static tree cp_parser_class_specifier
94edc4ab 1537 (cp_parser *);
a723baf1 1538static tree cp_parser_class_head
38b305d0 1539 (cp_parser *, bool *, tree *);
a723baf1 1540static enum tag_types cp_parser_class_key
94edc4ab 1541 (cp_parser *);
a723baf1 1542static void cp_parser_member_specification_opt
94edc4ab 1543 (cp_parser *);
a723baf1 1544static void cp_parser_member_declaration
94edc4ab 1545 (cp_parser *);
a723baf1 1546static tree cp_parser_pure_specifier
94edc4ab 1547 (cp_parser *);
a723baf1 1548static tree cp_parser_constant_initializer
94edc4ab 1549 (cp_parser *);
a723baf1
MM
1550
1551/* Derived classes [gram.class.derived] */
1552
1553static tree cp_parser_base_clause
94edc4ab 1554 (cp_parser *);
a723baf1 1555static tree cp_parser_base_specifier
94edc4ab 1556 (cp_parser *);
a723baf1
MM
1557
1558/* Special member functions [gram.special] */
1559
1560static tree cp_parser_conversion_function_id
94edc4ab 1561 (cp_parser *);
a723baf1 1562static tree cp_parser_conversion_type_id
94edc4ab 1563 (cp_parser *);
058b15c1 1564static cp_declarator *cp_parser_conversion_declarator_opt
94edc4ab 1565 (cp_parser *);
a723baf1 1566static bool cp_parser_ctor_initializer_opt
94edc4ab 1567 (cp_parser *);
a723baf1 1568static void cp_parser_mem_initializer_list
94edc4ab 1569 (cp_parser *);
a723baf1 1570static tree cp_parser_mem_initializer
94edc4ab 1571 (cp_parser *);
a723baf1 1572static tree cp_parser_mem_initializer_id
94edc4ab 1573 (cp_parser *);
a723baf1
MM
1574
1575/* Overloading [gram.over] */
1576
1577static tree cp_parser_operator_function_id
94edc4ab 1578 (cp_parser *);
a723baf1 1579static tree cp_parser_operator
94edc4ab 1580 (cp_parser *);
a723baf1
MM
1581
1582/* Templates [gram.temp] */
1583
1584static void cp_parser_template_declaration
94edc4ab 1585 (cp_parser *, bool);
a723baf1 1586static tree cp_parser_template_parameter_list
94edc4ab 1587 (cp_parser *);
a723baf1 1588static tree cp_parser_template_parameter
058b15c1 1589 (cp_parser *, bool *);
a723baf1 1590static tree cp_parser_type_parameter
94edc4ab 1591 (cp_parser *);
a723baf1 1592static tree cp_parser_template_id
a668c6ad 1593 (cp_parser *, bool, bool, bool);
a723baf1 1594static tree cp_parser_template_name
a668c6ad 1595 (cp_parser *, bool, bool, bool, bool *);
a723baf1 1596static tree cp_parser_template_argument_list
94edc4ab 1597 (cp_parser *);
a723baf1 1598static tree cp_parser_template_argument
94edc4ab 1599 (cp_parser *);
a723baf1 1600static void cp_parser_explicit_instantiation
94edc4ab 1601 (cp_parser *);
a723baf1 1602static void cp_parser_explicit_specialization
94edc4ab 1603 (cp_parser *);
a723baf1
MM
1604
1605/* Exception handling [gram.exception] */
1606
21526606 1607static tree cp_parser_try_block
94edc4ab 1608 (cp_parser *);
a723baf1 1609static bool cp_parser_function_try_block
94edc4ab 1610 (cp_parser *);
a723baf1 1611static void cp_parser_handler_seq
94edc4ab 1612 (cp_parser *);
a723baf1 1613static void cp_parser_handler
94edc4ab 1614 (cp_parser *);
a723baf1 1615static tree cp_parser_exception_declaration
94edc4ab 1616 (cp_parser *);
a723baf1 1617static tree cp_parser_throw_expression
94edc4ab 1618 (cp_parser *);
a723baf1 1619static tree cp_parser_exception_specification_opt
94edc4ab 1620 (cp_parser *);
a723baf1 1621static tree cp_parser_type_id_list
94edc4ab 1622 (cp_parser *);
a723baf1
MM
1623
1624/* GNU Extensions */
1625
1626static tree cp_parser_asm_specification_opt
94edc4ab 1627 (cp_parser *);
a723baf1 1628static tree cp_parser_asm_operand_list
94edc4ab 1629 (cp_parser *);
a723baf1 1630static tree cp_parser_asm_clobber_list
94edc4ab 1631 (cp_parser *);
a723baf1 1632static tree cp_parser_attributes_opt
94edc4ab 1633 (cp_parser *);
a723baf1 1634static tree cp_parser_attribute_list
94edc4ab 1635 (cp_parser *);
a723baf1 1636static bool cp_parser_extension_opt
94edc4ab 1637 (cp_parser *, int *);
a723baf1 1638static void cp_parser_label_declaration
94edc4ab 1639 (cp_parser *);
a723baf1
MM
1640
1641/* Utility Routines */
1642
1643static tree cp_parser_lookup_name
fc6a28d7 1644 (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
a723baf1 1645static tree cp_parser_lookup_name_simple
94edc4ab 1646 (cp_parser *, tree);
a723baf1
MM
1647static tree cp_parser_maybe_treat_template_as_class
1648 (tree, bool);
1649static bool cp_parser_check_declarator_template_parameters
058b15c1 1650 (cp_parser *, cp_declarator *);
a723baf1 1651static bool cp_parser_check_template_parameters
94edc4ab 1652 (cp_parser *, unsigned);
d6b4ea85
MM
1653static tree cp_parser_simple_cast_expression
1654 (cp_parser *);
a723baf1 1655static tree cp_parser_global_scope_opt
94edc4ab 1656 (cp_parser *, bool);
a723baf1
MM
1657static bool cp_parser_constructor_declarator_p
1658 (cp_parser *, bool);
1659static tree cp_parser_function_definition_from_specifiers_and_declarator
62d1db17 1660 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
a723baf1 1661static tree cp_parser_function_definition_after_declarator
94edc4ab 1662 (cp_parser *, bool);
a723baf1 1663static void cp_parser_template_declaration_after_export
94edc4ab 1664 (cp_parser *, bool);
a723baf1 1665static tree cp_parser_single_declaration
94edc4ab 1666 (cp_parser *, bool, bool *);
a723baf1 1667static tree cp_parser_functional_cast
94edc4ab 1668 (cp_parser *, tree);
4bb8ca28 1669static tree cp_parser_save_member_function_body
62d1db17 1670 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
ec75414f
MM
1671static tree cp_parser_enclosed_template_argument_list
1672 (cp_parser *);
8db1028e
NS
1673static void cp_parser_save_default_args
1674 (cp_parser *, tree);
a723baf1 1675static void cp_parser_late_parsing_for_member
94edc4ab 1676 (cp_parser *, tree);
a723baf1 1677static void cp_parser_late_parsing_default_args
8218bd34 1678 (cp_parser *, tree);
a723baf1 1679static tree cp_parser_sizeof_operand
94edc4ab 1680 (cp_parser *, enum rid);
a723baf1 1681static bool cp_parser_declares_only_class_p
94edc4ab 1682 (cp_parser *);
62d1db17
MM
1683static void cp_parser_set_storage_class
1684 (cp_decl_specifier_seq *, cp_storage_class);
98ca843c 1685static void cp_parser_set_decl_spec_type
62d1db17 1686 (cp_decl_specifier_seq *, tree, bool);
a723baf1 1687static bool cp_parser_friend_p
62d1db17 1688 (const cp_decl_specifier_seq *);
a723baf1 1689static cp_token *cp_parser_require
94edc4ab 1690 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1691static cp_token *cp_parser_require_keyword
94edc4ab 1692 (cp_parser *, enum rid, const char *);
21526606 1693static bool cp_parser_token_starts_function_definition_p
94edc4ab 1694 (cp_token *);
a723baf1
MM
1695static bool cp_parser_next_token_starts_class_definition_p
1696 (cp_parser *);
d17811fd
MM
1697static bool cp_parser_next_token_ends_template_argument_p
1698 (cp_parser *);
f4abade9
GB
1699static bool cp_parser_nth_token_starts_template_argument_list_p
1700 (cp_parser *, size_t);
a723baf1 1701static enum tag_types cp_parser_token_is_class_key
94edc4ab 1702 (cp_token *);
a723baf1
MM
1703static void cp_parser_check_class_key
1704 (enum tag_types, tree type);
37d407a1
KL
1705static void cp_parser_check_access_in_redeclaration
1706 (tree type);
a723baf1
MM
1707static bool cp_parser_optional_template_keyword
1708 (cp_parser *);
21526606 1709static void cp_parser_pre_parsed_nested_name_specifier
2050a1bb 1710 (cp_parser *);
a723baf1 1711static void cp_parser_cache_group
c162c75e 1712 (cp_parser *, enum cpp_ttype, unsigned);
21526606 1713static void cp_parser_parse_tentatively
94edc4ab 1714 (cp_parser *);
a723baf1 1715static void cp_parser_commit_to_tentative_parse
94edc4ab 1716 (cp_parser *);
a723baf1 1717static void cp_parser_abort_tentative_parse
94edc4ab 1718 (cp_parser *);
a723baf1 1719static bool cp_parser_parse_definitely
94edc4ab 1720 (cp_parser *);
f7b5ecd9 1721static inline bool cp_parser_parsing_tentatively
94edc4ab 1722 (cp_parser *);
0b16f8f4 1723static bool cp_parser_uncommitted_to_tentative_parse_p
94edc4ab 1724 (cp_parser *);
a723baf1 1725static void cp_parser_error
94edc4ab 1726 (cp_parser *, const char *);
4bb8ca28
MM
1727static void cp_parser_name_lookup_error
1728 (cp_parser *, tree, tree, const char *);
e5976695 1729static bool cp_parser_simulate_error
94edc4ab 1730 (cp_parser *);
a723baf1 1731static void cp_parser_check_type_definition
94edc4ab 1732 (cp_parser *);
560ad596 1733static void cp_parser_check_for_definition_in_return_type
fc6a28d7 1734 (cp_declarator *, tree);
ee43dab5
MM
1735static void cp_parser_check_for_invalid_template_id
1736 (cp_parser *, tree);
625cbf93
MM
1737static bool cp_parser_non_integral_constant_expression
1738 (cp_parser *, const char *);
2097b5f2
GB
1739static void cp_parser_diagnose_invalid_type_name
1740 (cp_parser *, tree, tree);
1741static bool cp_parser_parse_and_diagnose_invalid_type_name
8fbc5ae7 1742 (cp_parser *);
7efa3e22 1743static int cp_parser_skip_to_closing_parenthesis
a668c6ad 1744 (cp_parser *, bool, bool, bool);
a723baf1 1745static void cp_parser_skip_to_end_of_statement
94edc4ab 1746 (cp_parser *);
e0860732
MM
1747static void cp_parser_consume_semicolon_at_end_of_statement
1748 (cp_parser *);
a723baf1 1749static void cp_parser_skip_to_end_of_block_or_statement
94edc4ab 1750 (cp_parser *);
a723baf1
MM
1751static void cp_parser_skip_to_closing_brace
1752 (cp_parser *);
1753static void cp_parser_skip_until_found
94edc4ab 1754 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1755static bool cp_parser_error_occurred
94edc4ab 1756 (cp_parser *);
a723baf1 1757static bool cp_parser_allow_gnu_extensions_p
94edc4ab 1758 (cp_parser *);
a723baf1 1759static bool cp_parser_is_string_literal
94edc4ab 1760 (cp_token *);
21526606 1761static bool cp_parser_is_keyword
94edc4ab 1762 (cp_token *, enum rid);
2097b5f2
GB
1763static tree cp_parser_make_typename_type
1764 (cp_parser *, tree, tree);
a723baf1 1765
4de8668e 1766/* Returns nonzero if we are parsing tentatively. */
f7b5ecd9
MM
1767
1768static inline bool
94edc4ab 1769cp_parser_parsing_tentatively (cp_parser* parser)
f7b5ecd9
MM
1770{
1771 return parser->context->next != NULL;
1772}
1773
4de8668e 1774/* Returns nonzero if TOKEN is a string literal. */
a723baf1
MM
1775
1776static bool
94edc4ab 1777cp_parser_is_string_literal (cp_token* token)
a723baf1
MM
1778{
1779 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1780}
1781
4de8668e 1782/* Returns nonzero if TOKEN is the indicated KEYWORD. */
a723baf1
MM
1783
1784static bool
94edc4ab 1785cp_parser_is_keyword (cp_token* token, enum rid keyword)
a723baf1
MM
1786{
1787 return token->keyword == keyword;
1788}
1789
2cfe82fe
ZW
1790/* If not parsing tentatively, issue a diagnostic of the form
1791 FILE:LINE: MESSAGE before TOKEN
1792 where TOKEN is the next token in the input stream. MESSAGE
1793 (specified by the caller) is usually of the form "expected
1794 OTHER-TOKEN". */
a723baf1
MM
1795
1796static void
94edc4ab 1797cp_parser_error (cp_parser* parser, const char* message)
a723baf1 1798{
e5976695 1799 if (!cp_parser_simulate_error (parser))
4bb8ca28 1800 {
2cfe82fe
ZW
1801 cp_token *token = cp_lexer_peek_token (parser->lexer);
1802 /* This diagnostic makes more sense if it is tagged to the line
1803 of the token we just peeked at. */
1804 cp_lexer_set_source_position_from_token (token);
0d63048c
MM
1805 if (token->type == CPP_PRAGMA)
1806 {
1807 error ("%<#pragma%> is not allowed here");
1808 cp_lexer_purge_token (parser->lexer);
1809 return;
1810 }
21526606 1811 c_parse_error (message,
5c832178
MM
1812 /* Because c_parser_error does not understand
1813 CPP_KEYWORD, keywords are treated like
1814 identifiers. */
21526606 1815 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
5c832178 1816 token->value);
4bb8ca28
MM
1817 }
1818}
1819
1820/* Issue an error about name-lookup failing. NAME is the
1821 IDENTIFIER_NODE DECL is the result of
1822 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1823 the thing that we hoped to find. */
1824
1825static void
1826cp_parser_name_lookup_error (cp_parser* parser,
1827 tree name,
1828 tree decl,
1829 const char* desired)
1830{
1831 /* If name lookup completely failed, tell the user that NAME was not
1832 declared. */
1833 if (decl == error_mark_node)
1834 {
1835 if (parser->scope && parser->scope != global_namespace)
2a13a625 1836 error ("%<%D::%D%> has not been declared",
4bb8ca28
MM
1837 parser->scope, name);
1838 else if (parser->scope == global_namespace)
2a13a625 1839 error ("%<::%D%> has not been declared", name);
b14454ba
MM
1840 else if (parser->object_scope
1841 && !CLASS_TYPE_P (parser->object_scope))
2a13a625 1842 error ("request for member %qD in non-class type %qT",
b14454ba
MM
1843 name, parser->object_scope);
1844 else if (parser->object_scope)
2a13a625 1845 error ("%<%T::%D%> has not been declared",
b14454ba 1846 parser->object_scope, name);
4bb8ca28 1847 else
9e637a26 1848 error ("%qD has not been declared", name);
4bb8ca28
MM
1849 }
1850 else if (parser->scope && parser->scope != global_namespace)
2a13a625 1851 error ("%<%D::%D%> %s", parser->scope, name, desired);
4bb8ca28 1852 else if (parser->scope == global_namespace)
2a13a625 1853 error ("%<::%D%> %s", name, desired);
4bb8ca28 1854 else
2a13a625 1855 error ("%qD %s", name, desired);
a723baf1
MM
1856}
1857
1858/* If we are parsing tentatively, remember that an error has occurred
e5976695 1859 during this tentative parse. Returns true if the error was
77077b39 1860 simulated; false if a message should be issued by the caller. */
a723baf1 1861
e5976695 1862static bool
94edc4ab 1863cp_parser_simulate_error (cp_parser* parser)
a723baf1 1864{
0b16f8f4 1865 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
e5976695
MM
1866 {
1867 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1868 return true;
1869 }
1870 return false;
a723baf1
MM
1871}
1872
1873/* This function is called when a type is defined. If type
1874 definitions are forbidden at this point, an error message is
1875 issued. */
1876
1877static void
94edc4ab 1878cp_parser_check_type_definition (cp_parser* parser)
a723baf1
MM
1879{
1880 /* If types are forbidden here, issue a message. */
1881 if (parser->type_definition_forbidden_message)
1882 /* Use `%s' to print the string in case there are any escape
1883 characters in the message. */
1884 error ("%s", parser->type_definition_forbidden_message);
1885}
1886
fc6a28d7 1887/* This function is called when the DECLARATOR is processed. The TYPE
472c29c3 1888 was a type defined in the decl-specifiers. If it is invalid to
fc6a28d7
MM
1889 define a type in the decl-specifiers for DECLARATOR, an error is
1890 issued. */
560ad596
MM
1891
1892static void
058b15c1 1893cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
fc6a28d7 1894 tree type)
560ad596
MM
1895{
1896 /* [dcl.fct] forbids type definitions in return types.
1897 Unfortunately, it's not easy to know whether or not we are
1898 processing a return type until after the fact. */
1899 while (declarator
058b15c1
MM
1900 && (declarator->kind == cdk_pointer
1901 || declarator->kind == cdk_reference
1902 || declarator->kind == cdk_ptrmem))
1903 declarator = declarator->declarator;
560ad596 1904 if (declarator
fc6a28d7
MM
1905 && declarator->kind == cdk_function)
1906 {
1907 error ("new types may not be defined in a return type");
1908 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1909 type);
1910 }
560ad596
MM
1911}
1912
ee43dab5
MM
1913/* A type-specifier (TYPE) has been parsed which cannot be followed by
1914 "<" in any valid C++ program. If the next token is indeed "<",
1915 issue a message warning the user about what appears to be an
1916 invalid attempt to form a template-id. */
1917
1918static void
21526606 1919cp_parser_check_for_invalid_template_id (cp_parser* parser,
ee43dab5
MM
1920 tree type)
1921{
0c5e4866 1922 cp_token_position start = 0;
ee43dab5
MM
1923
1924 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1925 {
1926 if (TYPE_P (type))
2a13a625 1927 error ("%qT is not a template", type);
ee43dab5 1928 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2a13a625 1929 error ("%qE is not a template", type);
ee43dab5
MM
1930 else
1931 error ("invalid template-id");
1932 /* Remember the location of the invalid "<". */
0b16f8f4 1933 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
0c5e4866 1934 start = cp_lexer_token_position (parser->lexer, true);
ee43dab5
MM
1935 /* Consume the "<". */
1936 cp_lexer_consume_token (parser->lexer);
1937 /* Parse the template arguments. */
1938 cp_parser_enclosed_template_argument_list (parser);
da1d7781 1939 /* Permanently remove the invalid template arguments so that
ee43dab5 1940 this error message is not issued again. */
0c5e4866
NS
1941 if (start)
1942 cp_lexer_purge_tokens_after (parser->lexer, start);
ee43dab5
MM
1943 }
1944}
1945
625cbf93
MM
1946/* If parsing an integral constant-expression, issue an error message
1947 about the fact that THING appeared and return true. Otherwise,
93678513
MM
1948 return false. In either case, set
1949 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
14d22dd6 1950
625cbf93
MM
1951static bool
1952cp_parser_non_integral_constant_expression (cp_parser *parser,
1953 const char *thing)
14d22dd6 1954{
93678513 1955 parser->non_integral_constant_expression_p = true;
625cbf93
MM
1956 if (parser->integral_constant_expression_p)
1957 {
1958 if (!parser->allow_non_integral_constant_expression_p)
1959 {
1960 error ("%s cannot appear in a constant-expression", thing);
1961 return true;
1962 }
625cbf93
MM
1963 }
1964 return false;
14d22dd6
MM
1965}
1966
0c88d886 1967/* Emit a diagnostic for an invalid type name. SCOPE is the
6ca2d67f
MM
1968 qualifying scope (or NULL, if none) for ID. This function commits
1969 to the current active tentative parse, if any. (Otherwise, the
1970 problematic construct might be encountered again later, resulting
1971 in duplicate error messages.) */
8fbc5ae7 1972
2097b5f2
GB
1973static void
1974cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
6c0cc713
GB
1975{
1976 tree decl, old_scope;
2097b5f2
GB
1977 /* Try to lookup the identifier. */
1978 old_scope = parser->scope;
1979 parser->scope = scope;
1980 decl = cp_parser_lookup_name_simple (parser, id);
1981 parser->scope = old_scope;
1982 /* If the lookup found a template-name, it means that the user forgot
1983 to specify an argument list. Emit an useful error message. */
1984 if (TREE_CODE (decl) == TEMPLATE_DECL)
2a13a625 1985 error ("invalid use of template-name %qE without an argument list",
6c0cc713 1986 decl);
2097b5f2 1987 else if (!parser->scope)
8fbc5ae7 1988 {
8fbc5ae7 1989 /* Issue an error message. */
2a13a625 1990 error ("%qE does not name a type", id);
8fbc5ae7
MM
1991 /* If we're in a template class, it's possible that the user was
1992 referring to a type from a base class. For example:
1993
1994 template <typename T> struct A { typedef T X; };
1995 template <typename T> struct B : public A<T> { X x; };
1996
1997 The user should have said "typename A<T>::X". */
1998 if (processing_template_decl && current_class_type)
1999 {
2000 tree b;
2001
2002 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2003 b;
2004 b = TREE_CHAIN (b))
2005 {
2006 tree base_type = BINFO_TYPE (b);
21526606 2007 if (CLASS_TYPE_P (base_type)
1fb3244a 2008 && dependent_type_p (base_type))
8fbc5ae7
MM
2009 {
2010 tree field;
2011 /* Go from a particular instantiation of the
2012 template (which will have an empty TYPE_FIELDs),
2013 to the main version. */
353b4fc0 2014 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
8fbc5ae7
MM
2015 for (field = TYPE_FIELDS (base_type);
2016 field;
2017 field = TREE_CHAIN (field))
2018 if (TREE_CODE (field) == TYPE_DECL
2097b5f2 2019 && DECL_NAME (field) == id)
8fbc5ae7 2020 {
c4f73174 2021 inform ("(perhaps %<typename %T::%E%> was intended)",
2097b5f2 2022 BINFO_TYPE (b), id);
8fbc5ae7
MM
2023 break;
2024 }
2025 if (field)
2026 break;
2027 }
2028 }
2029 }
8fbc5ae7 2030 }
2097b5f2
GB
2031 /* Here we diagnose qualified-ids where the scope is actually correct,
2032 but the identifier does not resolve to a valid type name. */
21526606 2033 else
2097b5f2
GB
2034 {
2035 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2a13a625 2036 error ("%qE in namespace %qE does not name a type",
2097b5f2
GB
2037 id, parser->scope);
2038 else if (TYPE_P (parser->scope))
2fbe4889 2039 error ("%qE in class %qT does not name a type", id, parser->scope);
2097b5f2 2040 else
315fb5db 2041 gcc_unreachable ();
2097b5f2 2042 }
6ca2d67f 2043 cp_parser_commit_to_tentative_parse (parser);
2097b5f2 2044}
8fbc5ae7 2045
2097b5f2
GB
2046/* Check for a common situation where a type-name should be present,
2047 but is not, and issue a sensible error message. Returns true if an
2048 invalid type-name was detected.
21526606 2049
2097b5f2 2050 The situation handled by this function are variable declarations of the
21526606
EC
2051 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2052 Usually, `ID' should name a type, but if we got here it means that it
2097b5f2
GB
2053 does not. We try to emit the best possible error message depending on
2054 how exactly the id-expression looks like.
2055*/
2056
2057static bool
2058cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2059{
2060 tree id;
2061
2062 cp_parser_parse_tentatively (parser);
21526606 2063 id = cp_parser_id_expression (parser,
2097b5f2
GB
2064 /*template_keyword_p=*/false,
2065 /*check_dependency_p=*/true,
2066 /*template_p=*/NULL,
2067 /*declarator_p=*/true);
2068 /* After the id-expression, there should be a plain identifier,
2069 otherwise this is not a simple variable declaration. Also, if
2070 the scope is dependent, we cannot do much. */
2071 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 2072 || (parser->scope && TYPE_P (parser->scope)
2097b5f2
GB
2073 && dependent_type_p (parser->scope)))
2074 {
2075 cp_parser_abort_tentative_parse (parser);
2076 return false;
2077 }
3590f0a6
MM
2078 if (!cp_parser_parse_definitely (parser)
2079 || TREE_CODE (id) != IDENTIFIER_NODE)
2097b5f2
GB
2080 return false;
2081
2097b5f2
GB
2082 /* Emit a diagnostic for the invalid type. */
2083 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2084 /* Skip to the end of the declaration; there's no point in
2085 trying to process it. */
2086 cp_parser_skip_to_end_of_block_or_statement (parser);
2087 return true;
8fbc5ae7
MM
2088}
2089
21526606 2090/* Consume tokens up to, and including, the next non-nested closing `)'.
7efa3e22
NS
2091 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2092 are doing error recovery. Returns -1 if OR_COMMA is true and we
2093 found an unnested comma. */
a723baf1 2094
7efa3e22
NS
2095static int
2096cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
21526606 2097 bool recovering,
a668c6ad
MM
2098 bool or_comma,
2099 bool consume_paren)
a723baf1 2100{
7efa3e22
NS
2101 unsigned paren_depth = 0;
2102 unsigned brace_depth = 0;
0173bb6f 2103 int result;
a723baf1 2104
0b16f8f4
VR
2105 if (recovering && !or_comma
2106 && cp_parser_uncommitted_to_tentative_parse_p (parser))
7efa3e22 2107 return 0;
21526606 2108
a723baf1
MM
2109 while (true)
2110 {
2111 cp_token *token;
21526606 2112
a723baf1
MM
2113 /* If we've run out of tokens, then there is no closing `)'. */
2114 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
0173bb6f
AO
2115 {
2116 result = 0;
2117 break;
2118 }
a723baf1 2119
a668c6ad 2120 token = cp_lexer_peek_token (parser->lexer);
21526606 2121
f4f206f4 2122 /* This matches the processing in skip_to_end_of_statement. */
a668c6ad 2123 if (token->type == CPP_SEMICOLON && !brace_depth)
0173bb6f
AO
2124 {
2125 result = 0;
2126 break;
2127 }
a668c6ad
MM
2128 if (token->type == CPP_OPEN_BRACE)
2129 ++brace_depth;
2130 if (token->type == CPP_CLOSE_BRACE)
7efa3e22 2131 {
a668c6ad 2132 if (!brace_depth--)
0173bb6f
AO
2133 {
2134 result = 0;
2135 break;
2136 }
7efa3e22 2137 }
a668c6ad
MM
2138 if (recovering && or_comma && token->type == CPP_COMMA
2139 && !brace_depth && !paren_depth)
0173bb6f
AO
2140 {
2141 result = -1;
2142 break;
2143 }
21526606 2144
7efa3e22
NS
2145 if (!brace_depth)
2146 {
2147 /* If it is an `(', we have entered another level of nesting. */
2148 if (token->type == CPP_OPEN_PAREN)
2149 ++paren_depth;
2150 /* If it is a `)', then we might be done. */
2151 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
a668c6ad
MM
2152 {
2153 if (consume_paren)
2154 cp_lexer_consume_token (parser->lexer);
0173bb6f
AO
2155 {
2156 result = 1;
2157 break;
2158 }
a668c6ad 2159 }
7efa3e22 2160 }
21526606 2161
a668c6ad
MM
2162 /* Consume the token. */
2163 cp_lexer_consume_token (parser->lexer);
a723baf1 2164 }
0173bb6f 2165
0173bb6f 2166 return result;
a723baf1
MM
2167}
2168
2169/* Consume tokens until we reach the end of the current statement.
2170 Normally, that will be just before consuming a `;'. However, if a
2171 non-nested `}' comes first, then we stop before consuming that. */
2172
2173static void
94edc4ab 2174cp_parser_skip_to_end_of_statement (cp_parser* parser)
a723baf1
MM
2175{
2176 unsigned nesting_depth = 0;
2177
2178 while (true)
2179 {
2180 cp_token *token;
2181
2182 /* Peek at the next token. */
2183 token = cp_lexer_peek_token (parser->lexer);
2184 /* If we've run out of tokens, stop. */
2185 if (token->type == CPP_EOF)
2186 break;
2187 /* If the next token is a `;', we have reached the end of the
2188 statement. */
2189 if (token->type == CPP_SEMICOLON && !nesting_depth)
2190 break;
2191 /* If the next token is a non-nested `}', then we have reached
2192 the end of the current block. */
2193 if (token->type == CPP_CLOSE_BRACE)
2194 {
2195 /* If this is a non-nested `}', stop before consuming it.
2196 That way, when confronted with something like:
2197
21526606 2198 { 3 + }
a723baf1
MM
2199
2200 we stop before consuming the closing `}', even though we
2201 have not yet reached a `;'. */
2202 if (nesting_depth == 0)
2203 break;
2204 /* If it is the closing `}' for a block that we have
2205 scanned, stop -- but only after consuming the token.
2206 That way given:
2207
2208 void f g () { ... }
2209 typedef int I;
2210
2211 we will stop after the body of the erroneously declared
2212 function, but before consuming the following `typedef'
2213 declaration. */
2214 if (--nesting_depth == 0)
2215 {
2216 cp_lexer_consume_token (parser->lexer);
2217 break;
2218 }
2219 }
2220 /* If it the next token is a `{', then we are entering a new
2221 block. Consume the entire block. */
2222 else if (token->type == CPP_OPEN_BRACE)
2223 ++nesting_depth;
2224 /* Consume the token. */
2225 cp_lexer_consume_token (parser->lexer);
2226 }
2227}
2228
e0860732
MM
2229/* This function is called at the end of a statement or declaration.
2230 If the next token is a semicolon, it is consumed; otherwise, error
2231 recovery is attempted. */
2232
2233static void
2234cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2235{
2236 /* Look for the trailing `;'. */
2237 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2238 {
2239 /* If there is additional (erroneous) input, skip to the end of
2240 the statement. */
2241 cp_parser_skip_to_end_of_statement (parser);
2242 /* If the next token is now a `;', consume it. */
2243 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2244 cp_lexer_consume_token (parser->lexer);
2245 }
2246}
2247
a723baf1
MM
2248/* Skip tokens until we have consumed an entire block, or until we
2249 have consumed a non-nested `;'. */
2250
2251static void
94edc4ab 2252cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
a723baf1
MM
2253{
2254 unsigned nesting_depth = 0;
2255
2256 while (true)
2257 {
2258 cp_token *token;
2259
2260 /* Peek at the next token. */
2261 token = cp_lexer_peek_token (parser->lexer);
2262 /* If we've run out of tokens, stop. */
2263 if (token->type == CPP_EOF)
2264 break;
2265 /* If the next token is a `;', we have reached the end of the
2266 statement. */
2267 if (token->type == CPP_SEMICOLON && !nesting_depth)
2268 {
2269 /* Consume the `;'. */
2270 cp_lexer_consume_token (parser->lexer);
2271 break;
2272 }
2273 /* Consume the token. */
2274 token = cp_lexer_consume_token (parser->lexer);
2275 /* If the next token is a non-nested `}', then we have reached
2276 the end of the current block. */
21526606 2277 if (token->type == CPP_CLOSE_BRACE
a723baf1
MM
2278 && (nesting_depth == 0 || --nesting_depth == 0))
2279 break;
2280 /* If it the next token is a `{', then we are entering a new
2281 block. Consume the entire block. */
2282 if (token->type == CPP_OPEN_BRACE)
2283 ++nesting_depth;
2284 }
2285}
2286
2287/* Skip tokens until a non-nested closing curly brace is the next
2288 token. */
2289
2290static void
2291cp_parser_skip_to_closing_brace (cp_parser *parser)
2292{
2293 unsigned nesting_depth = 0;
2294
2295 while (true)
2296 {
2297 cp_token *token;
2298
2299 /* Peek at the next token. */
2300 token = cp_lexer_peek_token (parser->lexer);
2301 /* If we've run out of tokens, stop. */
2302 if (token->type == CPP_EOF)
2303 break;
2304 /* If the next token is a non-nested `}', then we have reached
2305 the end of the current block. */
2306 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2307 break;
2308 /* If it the next token is a `{', then we are entering a new
2309 block. Consume the entire block. */
2310 else if (token->type == CPP_OPEN_BRACE)
2311 ++nesting_depth;
2312 /* Consume the token. */
2313 cp_lexer_consume_token (parser->lexer);
2314 }
2315}
2316
2097b5f2
GB
2317/* This is a simple wrapper around make_typename_type. When the id is
2318 an unresolved identifier node, we can provide a superior diagnostic
2319 using cp_parser_diagnose_invalid_type_name. */
2320
2321static tree
2322cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
6c0cc713
GB
2323{
2324 tree result;
2325 if (TREE_CODE (id) == IDENTIFIER_NODE)
2326 {
fc6a28d7
MM
2327 result = make_typename_type (scope, id, typename_type,
2328 /*complain=*/0);
6c0cc713
GB
2329 if (result == error_mark_node)
2330 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2331 return result;
2332 }
fc6a28d7 2333 return make_typename_type (scope, id, typename_type, tf_error);
2097b5f2
GB
2334}
2335
2336
a723baf1
MM
2337/* Create a new C++ parser. */
2338
2339static cp_parser *
94edc4ab 2340cp_parser_new (void)
a723baf1
MM
2341{
2342 cp_parser *parser;
17211ab5 2343 cp_lexer *lexer;
b8b94c5b 2344 unsigned i;
17211ab5
GK
2345
2346 /* cp_lexer_new_main is called before calling ggc_alloc because
2347 cp_lexer_new_main might load a PCH file. */
2348 lexer = cp_lexer_new_main ();
a723baf1 2349
b8b94c5b
PB
2350 /* Initialize the binops_by_token so that we can get the tree
2351 directly from the token. */
2352 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2353 binops_by_token[binops[i].token_type] = binops[i];
2354
99dd239f 2355 parser = GGC_CNEW (cp_parser);
17211ab5 2356 parser->lexer = lexer;
a723baf1
MM
2357 parser->context = cp_parser_context_new (NULL);
2358
2359 /* For now, we always accept GNU extensions. */
2360 parser->allow_gnu_extensions_p = 1;
2361
2362 /* The `>' token is a greater-than operator, not the end of a
2363 template-id. */
2364 parser->greater_than_is_operator_p = true;
2365
2366 parser->default_arg_ok_p = true;
21526606 2367
a723baf1 2368 /* We are not parsing a constant-expression. */
67c03833
JM
2369 parser->integral_constant_expression_p = false;
2370 parser->allow_non_integral_constant_expression_p = false;
2371 parser->non_integral_constant_expression_p = false;
a723baf1
MM
2372
2373 /* Local variable names are not forbidden. */
2374 parser->local_variables_forbidden_p = false;
2375
34cd5ae7 2376 /* We are not processing an `extern "C"' declaration. */
a723baf1
MM
2377 parser->in_unbraced_linkage_specification_p = false;
2378
2379 /* We are not processing a declarator. */
2380 parser->in_declarator_p = false;
2381
4bb8ca28
MM
2382 /* We are not processing a template-argument-list. */
2383 parser->in_template_argument_list_p = false;
2384
0e59b3fb
MM
2385 /* We are not in an iteration statement. */
2386 parser->in_iteration_statement_p = false;
2387
2388 /* We are not in a switch statement. */
2389 parser->in_switch_statement_p = false;
2390
4f8163b1
MM
2391 /* We are not parsing a type-id inside an expression. */
2392 parser->in_type_id_in_expr_p = false;
2393
03fd3f84 2394 /* Declarations aren't implicitly extern "C". */
7d381002
MA
2395 parser->implicit_extern_c = false;
2396
c162c75e
MA
2397 /* String literals should be translated to the execution character set. */
2398 parser->translate_strings_p = true;
2399
a723baf1
MM
2400 /* The unparsed function queue is empty. */
2401 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2402
2403 /* There are no classes being defined. */
2404 parser->num_classes_being_defined = 0;
2405
2406 /* No template parameters apply. */
2407 parser->num_template_parameter_lists = 0;
2408
2409 return parser;
2410}
2411
2cfe82fe
ZW
2412/* Create a cp_lexer structure which will emit the tokens in CACHE
2413 and push it onto the parser's lexer stack. This is used for delayed
2414 parsing of in-class method bodies and default arguments, and should
2415 not be confused with tentative parsing. */
2416static void
2417cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2418{
2419 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2420 lexer->next = parser->lexer;
2421 parser->lexer = lexer;
2422
2423 /* Move the current source position to that of the first token in the
2424 new lexer. */
2425 cp_lexer_set_source_position_from_token (lexer->next_token);
2426}
2427
2428/* Pop the top lexer off the parser stack. This is never used for the
2429 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2430static void
2431cp_parser_pop_lexer (cp_parser *parser)
2432{
2433 cp_lexer *lexer = parser->lexer;
2434 parser->lexer = lexer->next;
2435 cp_lexer_destroy (lexer);
2436
2437 /* Put the current source position back where it was before this
2438 lexer was pushed. */
2439 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2440}
2441
a723baf1
MM
2442/* Lexical conventions [gram.lex] */
2443
2444/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2445 identifier. */
2446
21526606 2447static tree
94edc4ab 2448cp_parser_identifier (cp_parser* parser)
a723baf1
MM
2449{
2450 cp_token *token;
2451
2452 /* Look for the identifier. */
2453 token = cp_parser_require (parser, CPP_NAME, "identifier");
2454 /* Return the value. */
2455 return token ? token->value : error_mark_node;
2456}
2457
c162c75e
MA
2458/* Parse a sequence of adjacent string constants. Returns a
2459 TREE_STRING representing the combined, nul-terminated string
2460 constant. If TRANSLATE is true, translate the string to the
2461 execution character set. If WIDE_OK is true, a wide string is
2462 invalid here.
2463
2464 C++98 [lex.string] says that if a narrow string literal token is
2465 adjacent to a wide string literal token, the behavior is undefined.
2466 However, C99 6.4.5p4 says that this results in a wide string literal.
2467 We follow C99 here, for consistency with the C front end.
2468
2469 This code is largely lifted from lex_string() in c-lex.c.
2470
2471 FUTURE: ObjC++ will need to handle @-strings here. */
2472static tree
2473cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2474{
2475 tree value;
2476 bool wide = false;
2477 size_t count;
2478 struct obstack str_ob;
2479 cpp_string str, istr, *strs;
2480 cp_token *tok;
2481
2482 tok = cp_lexer_peek_token (parser->lexer);
2483 if (!cp_parser_is_string_literal (tok))
2484 {
2485 cp_parser_error (parser, "expected string-literal");
2486 return error_mark_node;
2487 }
2488
9688c3b8 2489 /* Try to avoid the overhead of creating and destroying an obstack
c162c75e 2490 for the common case of just one string. */
2cfe82fe
ZW
2491 if (!cp_parser_is_string_literal
2492 (cp_lexer_peek_nth_token (parser->lexer, 2)))
c162c75e 2493 {
2cfe82fe
ZW
2494 cp_lexer_consume_token (parser->lexer);
2495
c162c75e
MA
2496 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2497 str.len = TREE_STRING_LENGTH (tok->value);
2498 count = 1;
2499 if (tok->type == CPP_WSTRING)
2500 wide = true;
c162c75e
MA
2501
2502 strs = &str;
2503 }
2504 else
2505 {
2506 gcc_obstack_init (&str_ob);
2507 count = 0;
2508
2509 do
2510 {
2cfe82fe 2511 cp_lexer_consume_token (parser->lexer);
c162c75e
MA
2512 count++;
2513 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2514 str.len = TREE_STRING_LENGTH (tok->value);
2515 if (tok->type == CPP_WSTRING)
2516 wide = true;
2517
2518 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2519
2cfe82fe 2520 tok = cp_lexer_peek_token (parser->lexer);
c162c75e
MA
2521 }
2522 while (cp_parser_is_string_literal (tok));
2523
2524 strs = (cpp_string *) obstack_finish (&str_ob);
2525 }
2526
2527 if (wide && !wide_ok)
2528 {
2529 cp_parser_error (parser, "a wide string is invalid in this context");
2530 wide = false;
2531 }
2532
2533 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2534 (parse_in, strs, count, &istr, wide))
2535 {
2536 value = build_string (istr.len, (char *)istr.text);
2537 free ((void *)istr.text);
2538
2539 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2540 value = fix_string_type (value);
2541 }
2542 else
2543 /* cpp_interpret_string has issued an error. */
2544 value = error_mark_node;
2545
2546 if (count > 1)
2547 obstack_free (&str_ob, 0);
2548
2549 return value;
2550}
2551
2552
a723baf1
MM
2553/* Basic concepts [gram.basic] */
2554
2555/* Parse a translation-unit.
2556
2557 translation-unit:
21526606 2558 declaration-seq [opt]
a723baf1
MM
2559
2560 Returns TRUE if all went well. */
2561
2562static bool
94edc4ab 2563cp_parser_translation_unit (cp_parser* parser)
a723baf1 2564{
058b15c1
MM
2565 /* The address of the first non-permanent object on the declarator
2566 obstack. */
2567 static void *declarator_obstack_base;
2568
2569 bool success;
98ca843c 2570
058b15c1
MM
2571 /* Create the declarator obstack, if necessary. */
2572 if (!cp_error_declarator)
2573 {
2574 gcc_obstack_init (&declarator_obstack);
2575 /* Create the error declarator. */
2576 cp_error_declarator = make_declarator (cdk_error);
2577 /* Create the empty parameter list. */
62d1db17 2578 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
058b15c1
MM
2579 /* Remember where the base of the declarator obstack lies. */
2580 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2581 }
2582
a723baf1
MM
2583 while (true)
2584 {
2585 cp_parser_declaration_seq_opt (parser);
2586
2587 /* If there are no tokens left then all went well. */
2588 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
058b15c1 2589 {
03fd3f84 2590 /* Get rid of the token array; we don't need it any more. */
c162c75e
MA
2591 cp_lexer_destroy (parser->lexer);
2592 parser->lexer = NULL;
2593
7d381002
MA
2594 /* This file might have been a context that's implicitly extern
2595 "C". If so, pop the lang context. (Only relevant for PCH.) */
2596 if (parser->implicit_extern_c)
2597 {
2598 pop_lang_context ();
2599 parser->implicit_extern_c = false;
2600 }
2601
058b15c1
MM
2602 /* Finish up. */
2603 finish_translation_unit ();
21526606 2604
058b15c1
MM
2605 success = true;
2606 break;
2607 }
2608 else
2609 {
2610 cp_parser_error (parser, "expected declaration");
2611 success = false;
2612 break;
2613 }
a723baf1
MM
2614 }
2615
058b15c1 2616 /* Make sure the declarator obstack was fully cleaned up. */
50bc768d
NS
2617 gcc_assert (obstack_next_free (&declarator_obstack)
2618 == declarator_obstack_base);
a723baf1
MM
2619
2620 /* All went well. */
058b15c1 2621 return success;
a723baf1
MM
2622}
2623
2624/* Expressions [gram.expr] */
2625
2626/* Parse a primary-expression.
2627
2628 primary-expression:
2629 literal
2630 this
2631 ( expression )
2632 id-expression
2633
2634 GNU Extensions:
2635
2636 primary-expression:
2637 ( compound-statement )
2638 __builtin_va_arg ( assignment-expression , type-id )
2639
2640 literal:
2641 __null
2642
93678513
MM
2643 CAST_P is true if this primary expression is the target of a cast.
2644
21526606 2645 Returns a representation of the expression.
a723baf1 2646
21526606 2647 *IDK indicates what kind of id-expression (if any) was present.
a723baf1
MM
2648
2649 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2650 used as the operand of a pointer-to-member. In that case,
2651 *QUALIFYING_CLASS gives the class that is used as the qualifying
2652 class in the pointer-to-member. */
2653
2654static tree
21526606 2655cp_parser_primary_expression (cp_parser *parser,
93678513 2656 bool cast_p,
b3445994 2657 cp_id_kind *idk,
a723baf1
MM
2658 tree *qualifying_class)
2659{
2660 cp_token *token;
2661
2662 /* Assume the primary expression is not an id-expression. */
b3445994 2663 *idk = CP_ID_KIND_NONE;
a723baf1
MM
2664 /* And that it cannot be used as pointer-to-member. */
2665 *qualifying_class = NULL_TREE;
2666
2667 /* Peek at the next token. */
2668 token = cp_lexer_peek_token (parser->lexer);
2669 switch (token->type)
2670 {
2671 /* literal:
2672 integer-literal
2673 character-literal
2674 floating-literal
2675 string-literal
2676 boolean-literal */
2677 case CPP_CHAR:
2678 case CPP_WCHAR:
a723baf1
MM
2679 case CPP_NUMBER:
2680 token = cp_lexer_consume_token (parser->lexer);
93678513
MM
2681 /* Floating-point literals are only allowed in an integral
2682 constant expression if they are cast to an integral or
2683 enumeration type. */
2684 if (TREE_CODE (token->value) == REAL_CST
8c94c75a
MM
2685 && parser->integral_constant_expression_p
2686 && pedantic)
93678513
MM
2687 {
2688 /* CAST_P will be set even in invalid code like "int(2.7 +
2689 ...)". Therefore, we have to check that the next token
2690 is sure to end the cast. */
2691 if (cast_p)
2692 {
2693 cp_token *next_token;
2694
2695 next_token = cp_lexer_peek_token (parser->lexer);
2696 if (/* The comma at the end of an
2697 enumerator-definition. */
2698 next_token->type != CPP_COMMA
2699 /* The curly brace at the end of an enum-specifier. */
2700 && next_token->type != CPP_CLOSE_BRACE
2701 /* The end of a statement. */
2702 && next_token->type != CPP_SEMICOLON
2703 /* The end of the cast-expression. */
2704 && next_token->type != CPP_CLOSE_PAREN
2705 /* The end of an array bound. */
2706 && next_token->type != CPP_CLOSE_SQUARE)
2707 cast_p = false;
2708 }
2709
2710 /* If we are within a cast, then the constraint that the
2711 cast is to an integral or enumeration type will be
2712 checked at that point. If we are not within a cast, then
2713 this code is invalid. */
2714 if (!cast_p)
2715 cp_parser_non_integral_constant_expression
2716 (parser, "floating-point literal");
2717 }
a723baf1
MM
2718 return token->value;
2719
0173bb6f
AO
2720 case CPP_STRING:
2721 case CPP_WSTRING:
c162c75e
MA
2722 /* ??? Should wide strings be allowed when parser->translate_strings_p
2723 is false (i.e. in attributes)? If not, we can kill the third
2724 argument to cp_parser_string_literal. */
2725 return cp_parser_string_literal (parser,
2726 parser->translate_strings_p,
2727 true);
0173bb6f 2728
a723baf1
MM
2729 case CPP_OPEN_PAREN:
2730 {
2731 tree expr;
2732 bool saved_greater_than_is_operator_p;
2733
2734 /* Consume the `('. */
2735 cp_lexer_consume_token (parser->lexer);
2736 /* Within a parenthesized expression, a `>' token is always
2737 the greater-than operator. */
21526606 2738 saved_greater_than_is_operator_p
a723baf1
MM
2739 = parser->greater_than_is_operator_p;
2740 parser->greater_than_is_operator_p = true;
2741 /* If we see `( { ' then we are looking at the beginning of
2742 a GNU statement-expression. */
2743 if (cp_parser_allow_gnu_extensions_p (parser)
2744 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2745 {
2746 /* Statement-expressions are not allowed by the standard. */
2747 if (pedantic)
21526606
EC
2748 pedwarn ("ISO C++ forbids braced-groups within expressions");
2749
a723baf1
MM
2750 /* And they're not allowed outside of a function-body; you
2751 cannot, for example, write:
21526606 2752
a723baf1 2753 int i = ({ int j = 3; j + 1; });
21526606 2754
a723baf1
MM
2755 at class or namespace scope. */
2756 if (!at_function_scope_p ())
2757 error ("statement-expressions are allowed only inside functions");
2758 /* Start the statement-expression. */
2759 expr = begin_stmt_expr ();
2760 /* Parse the compound-statement. */
325c3691 2761 cp_parser_compound_statement (parser, expr, false);
a723baf1 2762 /* Finish up. */
303b7406 2763 expr = finish_stmt_expr (expr, false);
a723baf1
MM
2764 }
2765 else
2766 {
2767 /* Parse the parenthesized expression. */
93678513 2768 expr = cp_parser_expression (parser, cast_p);
a723baf1
MM
2769 /* Let the front end know that this expression was
2770 enclosed in parentheses. This matters in case, for
2771 example, the expression is of the form `A::B', since
2772 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2773 not. */
2774 finish_parenthesized_expr (expr);
2775 }
2776 /* The `>' token might be the end of a template-id or
2777 template-parameter-list now. */
21526606 2778 parser->greater_than_is_operator_p
a723baf1
MM
2779 = saved_greater_than_is_operator_p;
2780 /* Consume the `)'. */
2781 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2782 cp_parser_skip_to_end_of_statement (parser);
2783
2784 return expr;
2785 }
2786
2787 case CPP_KEYWORD:
2788 switch (token->keyword)
2789 {
2790 /* These two are the boolean literals. */
2791 case RID_TRUE:
2792 cp_lexer_consume_token (parser->lexer);
2793 return boolean_true_node;
2794 case RID_FALSE:
2795 cp_lexer_consume_token (parser->lexer);
2796 return boolean_false_node;
21526606 2797
a723baf1
MM
2798 /* The `__null' literal. */
2799 case RID_NULL:
2800 cp_lexer_consume_token (parser->lexer);
2801 return null_node;
2802
2803 /* Recognize the `this' keyword. */
2804 case RID_THIS:
2805 cp_lexer_consume_token (parser->lexer);
2806 if (parser->local_variables_forbidden_p)
2807 {
2a13a625 2808 error ("%<this%> may not be used in this context");
a723baf1
MM
2809 return error_mark_node;
2810 }
14d22dd6 2811 /* Pointers cannot appear in constant-expressions. */
625cbf93
MM
2812 if (cp_parser_non_integral_constant_expression (parser,
2813 "`this'"))
2814 return error_mark_node;
a723baf1
MM
2815 return finish_this_expr ();
2816
2817 /* The `operator' keyword can be the beginning of an
2818 id-expression. */
2819 case RID_OPERATOR:
2820 goto id_expression;
2821
2822 case RID_FUNCTION_NAME:
2823 case RID_PRETTY_FUNCTION_NAME:
2824 case RID_C99_FUNCTION_NAME:
2825 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2826 __func__ are the names of variables -- but they are
2827 treated specially. Therefore, they are handled here,
2828 rather than relying on the generic id-expression logic
21526606 2829 below. Grammatically, these names are id-expressions.
a723baf1
MM
2830
2831 Consume the token. */
2832 token = cp_lexer_consume_token (parser->lexer);
2833 /* Look up the name. */
2834 return finish_fname (token->value);
2835
2836 case RID_VA_ARG:
2837 {
2838 tree expression;
2839 tree type;
2840
2841 /* The `__builtin_va_arg' construct is used to handle
2842 `va_arg'. Consume the `__builtin_va_arg' token. */
2843 cp_lexer_consume_token (parser->lexer);
2844 /* Look for the opening `('. */
2845 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2846 /* Now, parse the assignment-expression. */
93678513
MM
2847 expression = cp_parser_assignment_expression (parser,
2848 /*cast_p=*/false);
a723baf1
MM
2849 /* Look for the `,'. */
2850 cp_parser_require (parser, CPP_COMMA, "`,'");
2851 /* Parse the type-id. */
2852 type = cp_parser_type_id (parser);
2853 /* Look for the closing `)'. */
2854 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14d22dd6
MM
2855 /* Using `va_arg' in a constant-expression is not
2856 allowed. */
625cbf93
MM
2857 if (cp_parser_non_integral_constant_expression (parser,
2858 "`va_arg'"))
2859 return error_mark_node;
a723baf1
MM
2860 return build_x_va_arg (expression, type);
2861 }
2862
263ee052 2863 case RID_OFFSETOF:
7a3ea201 2864 return cp_parser_builtin_offsetof (parser);
263ee052 2865
a723baf1
MM
2866 default:
2867 cp_parser_error (parser, "expected primary-expression");
2868 return error_mark_node;
2869 }
a723baf1
MM
2870
2871 /* An id-expression can start with either an identifier, a
2872 `::' as the beginning of a qualified-id, or the "operator"
2873 keyword. */
2874 case CPP_NAME:
2875 case CPP_SCOPE:
2876 case CPP_TEMPLATE_ID:
2877 case CPP_NESTED_NAME_SPECIFIER:
2878 {
2879 tree id_expression;
2880 tree decl;
b3445994 2881 const char *error_msg;
a723baf1
MM
2882
2883 id_expression:
2884 /* Parse the id-expression. */
21526606
EC
2885 id_expression
2886 = cp_parser_id_expression (parser,
a723baf1
MM
2887 /*template_keyword_p=*/false,
2888 /*check_dependency_p=*/true,
f3c2dfc6
MM
2889 /*template_p=*/NULL,
2890 /*declarator_p=*/false);
a723baf1
MM
2891 if (id_expression == error_mark_node)
2892 return error_mark_node;
2893 /* If we have a template-id, then no further lookup is
2894 required. If the template-id was for a template-class, we
2895 will sometimes have a TYPE_DECL at this point. */
2896 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2897 || TREE_CODE (id_expression) == TYPE_DECL)
2898 decl = id_expression;
2899 /* Look up the name. */
21526606 2900 else
a723baf1 2901 {
8f78f01f
MM
2902 bool ambiguous_p;
2903
2904 decl = cp_parser_lookup_name (parser, id_expression,
fc6a28d7 2905 none_type,
8f78f01f
MM
2906 /*is_template=*/false,
2907 /*is_namespace=*/false,
2908 /*check_dependency=*/true,
2909 &ambiguous_p);
2910 /* If the lookup was ambiguous, an error will already have
2911 been issued. */
2912 if (ambiguous_p)
2913 return error_mark_node;
a723baf1
MM
2914 /* If name lookup gives us a SCOPE_REF, then the
2915 qualifying scope was dependent. Just propagate the
2916 name. */
2917 if (TREE_CODE (decl) == SCOPE_REF)
2918 {
2919 if (TYPE_P (TREE_OPERAND (decl, 0)))
2920 *qualifying_class = TREE_OPERAND (decl, 0);
2921 return decl;
2922 }
2923 /* Check to see if DECL is a local variable in a context
2924 where that is forbidden. */
2925 if (parser->local_variables_forbidden_p
2926 && local_variable_p (decl))
2927 {
2928 /* It might be that we only found DECL because we are
2929 trying to be generous with pre-ISO scoping rules.
2930 For example, consider:
2931
2932 int i;
2933 void g() {
2934 for (int i = 0; i < 10; ++i) {}
2935 extern void f(int j = i);
2936 }
2937
21526606 2938 Here, name look up will originally find the out
a723baf1
MM
2939 of scope `i'. We need to issue a warning message,
2940 but then use the global `i'. */
2941 decl = check_for_out_of_scope_variable (decl);
2942 if (local_variable_p (decl))
2943 {
2a13a625 2944 error ("local variable %qD may not appear in this context",
a723baf1
MM
2945 decl);
2946 return error_mark_node;
2947 }
2948 }
c006d942 2949 }
21526606
EC
2950
2951 decl = finish_id_expression (id_expression, decl, parser->scope,
b3445994 2952 idk, qualifying_class,
67c03833
JM
2953 parser->integral_constant_expression_p,
2954 parser->allow_non_integral_constant_expression_p,
2955 &parser->non_integral_constant_expression_p,
b3445994
MM
2956 &error_msg);
2957 if (error_msg)
2958 cp_parser_error (parser, error_msg);
a723baf1
MM
2959 return decl;
2960 }
2961
2962 /* Anything else is an error. */
2963 default:
2964 cp_parser_error (parser, "expected primary-expression");
2965 return error_mark_node;
2966 }
2967}
2968
2969/* Parse an id-expression.
2970
2971 id-expression:
2972 unqualified-id
2973 qualified-id
2974
2975 qualified-id:
2976 :: [opt] nested-name-specifier template [opt] unqualified-id
2977 :: identifier
2978 :: operator-function-id
2979 :: template-id
2980
2981 Return a representation of the unqualified portion of the
2982 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2983 a `::' or nested-name-specifier.
2984
2985 Often, if the id-expression was a qualified-id, the caller will
2986 want to make a SCOPE_REF to represent the qualified-id. This
2987 function does not do this in order to avoid wastefully creating
2988 SCOPE_REFs when they are not required.
2989
a723baf1
MM
2990 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2991 `template' keyword.
2992
2993 If CHECK_DEPENDENCY_P is false, then names are looked up inside
21526606 2994 uninstantiated templates.
a723baf1 2995
15d2cb19 2996 If *TEMPLATE_P is non-NULL, it is set to true iff the
a723baf1 2997 `template' keyword is used to explicitly indicate that the entity
21526606 2998 named is a template.
f3c2dfc6
MM
2999
3000 If DECLARATOR_P is true, the id-expression is appearing as part of
cd0be382 3001 a declarator, rather than as part of an expression. */
a723baf1
MM
3002
3003static tree
3004cp_parser_id_expression (cp_parser *parser,
3005 bool template_keyword_p,
3006 bool check_dependency_p,
f3c2dfc6
MM
3007 bool *template_p,
3008 bool declarator_p)
a723baf1
MM
3009{
3010 bool global_scope_p;
3011 bool nested_name_specifier_p;
3012
3013 /* Assume the `template' keyword was not used. */
3014 if (template_p)
3015 *template_p = false;
3016
3017 /* Look for the optional `::' operator. */
21526606
EC
3018 global_scope_p
3019 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
a723baf1
MM
3020 != NULL_TREE);
3021 /* Look for the optional nested-name-specifier. */
21526606 3022 nested_name_specifier_p
a723baf1
MM
3023 = (cp_parser_nested_name_specifier_opt (parser,
3024 /*typename_keyword_p=*/false,
3025 check_dependency_p,
a668c6ad 3026 /*type_p=*/false,
a52eb3bc 3027 declarator_p)
a723baf1
MM
3028 != NULL_TREE);
3029 /* If there is a nested-name-specifier, then we are looking at
3030 the first qualified-id production. */
3031 if (nested_name_specifier_p)
3032 {
3033 tree saved_scope;
3034 tree saved_object_scope;
3035 tree saved_qualifying_scope;
3036 tree unqualified_id;
3037 bool is_template;
3038
3039 /* See if the next token is the `template' keyword. */
3040 if (!template_p)
3041 template_p = &is_template;
3042 *template_p = cp_parser_optional_template_keyword (parser);
3043 /* Name lookup we do during the processing of the
3044 unqualified-id might obliterate SCOPE. */
3045 saved_scope = parser->scope;
3046 saved_object_scope = parser->object_scope;
3047 saved_qualifying_scope = parser->qualifying_scope;
3048 /* Process the final unqualified-id. */
3049 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
f3c2dfc6
MM
3050 check_dependency_p,
3051 declarator_p);
a723baf1
MM
3052 /* Restore the SAVED_SCOPE for our caller. */
3053 parser->scope = saved_scope;
3054 parser->object_scope = saved_object_scope;
3055 parser->qualifying_scope = saved_qualifying_scope;
3056
3057 return unqualified_id;
3058 }
3059 /* Otherwise, if we are in global scope, then we are looking at one
3060 of the other qualified-id productions. */
3061 else if (global_scope_p)
3062 {
3063 cp_token *token;
3064 tree id;
3065
e5976695
MM
3066 /* Peek at the next token. */
3067 token = cp_lexer_peek_token (parser->lexer);
3068
3069 /* If it's an identifier, and the next token is not a "<", then
3070 we can avoid the template-id case. This is an optimization
3071 for this common case. */
21526606
EC
3072 if (token->type == CPP_NAME
3073 && !cp_parser_nth_token_starts_template_argument_list_p
f4abade9 3074 (parser, 2))
e5976695
MM
3075 return cp_parser_identifier (parser);
3076
a723baf1
MM
3077 cp_parser_parse_tentatively (parser);
3078 /* Try a template-id. */
21526606 3079 id = cp_parser_template_id (parser,
a723baf1 3080 /*template_keyword_p=*/false,
a668c6ad
MM
3081 /*check_dependency_p=*/true,
3082 declarator_p);
a723baf1
MM
3083 /* If that worked, we're done. */
3084 if (cp_parser_parse_definitely (parser))
3085 return id;
3086
e5976695
MM
3087 /* Peek at the next token. (Changes in the token buffer may
3088 have invalidated the pointer obtained above.) */
a723baf1
MM
3089 token = cp_lexer_peek_token (parser->lexer);
3090
3091 switch (token->type)
3092 {
3093 case CPP_NAME:
3094 return cp_parser_identifier (parser);
3095
3096 case CPP_KEYWORD:
3097 if (token->keyword == RID_OPERATOR)
3098 return cp_parser_operator_function_id (parser);
3099 /* Fall through. */
21526606 3100
a723baf1
MM
3101 default:
3102 cp_parser_error (parser, "expected id-expression");
3103 return error_mark_node;
3104 }
3105 }
3106 else
3107 return cp_parser_unqualified_id (parser, template_keyword_p,
f3c2dfc6
MM
3108 /*check_dependency_p=*/true,
3109 declarator_p);
a723baf1
MM
3110}
3111
3112/* Parse an unqualified-id.
3113
3114 unqualified-id:
3115 identifier
3116 operator-function-id
3117 conversion-function-id
3118 ~ class-name
3119 template-id
3120
3121 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3122 keyword, in a construct like `A::template ...'.
3123
3124 Returns a representation of unqualified-id. For the `identifier'
3125 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3126 production a BIT_NOT_EXPR is returned; the operand of the
3127 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3128 other productions, see the documentation accompanying the
3129 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
f3c2dfc6
MM
3130 names are looked up in uninstantiated templates. If DECLARATOR_P
3131 is true, the unqualified-id is appearing as part of a declarator,
3132 rather than as part of an expression. */
a723baf1
MM
3133
3134static tree
21526606 3135cp_parser_unqualified_id (cp_parser* parser,
94edc4ab 3136 bool template_keyword_p,
f3c2dfc6
MM
3137 bool check_dependency_p,
3138 bool declarator_p)
a723baf1
MM
3139{
3140 cp_token *token;
3141
3142 /* Peek at the next token. */
3143 token = cp_lexer_peek_token (parser->lexer);
21526606 3144
a723baf1
MM
3145 switch (token->type)
3146 {
3147 case CPP_NAME:
3148 {
3149 tree id;
3150
3151 /* We don't know yet whether or not this will be a
3152 template-id. */
3153 cp_parser_parse_tentatively (parser);
3154 /* Try a template-id. */
3155 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3156 check_dependency_p,
3157 declarator_p);
a723baf1
MM
3158 /* If it worked, we're done. */
3159 if (cp_parser_parse_definitely (parser))
3160 return id;
3161 /* Otherwise, it's an ordinary identifier. */
3162 return cp_parser_identifier (parser);
3163 }
3164
3165 case CPP_TEMPLATE_ID:
3166 return cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3167 check_dependency_p,
3168 declarator_p);
a723baf1
MM
3169
3170 case CPP_COMPL:
3171 {
3172 tree type_decl;
3173 tree qualifying_scope;
3174 tree object_scope;
3175 tree scope;
88e95ee3 3176 bool done;
a723baf1
MM
3177
3178 /* Consume the `~' token. */
3179 cp_lexer_consume_token (parser->lexer);
3180 /* Parse the class-name. The standard, as written, seems to
3181 say that:
3182
3183 template <typename T> struct S { ~S (); };
3184 template <typename T> S<T>::~S() {}
3185
3186 is invalid, since `~' must be followed by a class-name, but
3187 `S<T>' is dependent, and so not known to be a class.
3188 That's not right; we need to look in uninstantiated
3189 templates. A further complication arises from:
3190
3191 template <typename T> void f(T t) {
3192 t.T::~T();
21526606 3193 }
a723baf1
MM
3194
3195 Here, it is not possible to look up `T' in the scope of `T'
3196 itself. We must look in both the current scope, and the
21526606 3197 scope of the containing complete expression.
a723baf1
MM
3198
3199 Yet another issue is:
3200
3201 struct S {
3202 int S;
3203 ~S();
3204 };
3205
3206 S::~S() {}
3207
3208 The standard does not seem to say that the `S' in `~S'
3209 should refer to the type `S' and not the data member
3210 `S::S'. */
3211
3212 /* DR 244 says that we look up the name after the "~" in the
3213 same scope as we looked up the qualifying name. That idea
3214 isn't fully worked out; it's more complicated than that. */
3215 scope = parser->scope;
3216 object_scope = parser->object_scope;
3217 qualifying_scope = parser->qualifying_scope;
3218
3219 /* If the name is of the form "X::~X" it's OK. */
3220 if (scope && TYPE_P (scope)
3221 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 3222 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1 3223 == CPP_OPEN_PAREN)
21526606 3224 && (cp_lexer_peek_token (parser->lexer)->value
a723baf1
MM
3225 == TYPE_IDENTIFIER (scope)))
3226 {
3227 cp_lexer_consume_token (parser->lexer);
3228 return build_nt (BIT_NOT_EXPR, scope);
3229 }
3230
3231 /* If there was an explicit qualification (S::~T), first look
3232 in the scope given by the qualification (i.e., S). */
88e95ee3 3233 done = false;
e3754f9c 3234 type_decl = NULL_TREE;
a723baf1
MM
3235 if (scope)
3236 {
3237 cp_parser_parse_tentatively (parser);
21526606 3238 type_decl = cp_parser_class_name (parser,
a723baf1
MM
3239 /*typename_keyword_p=*/false,
3240 /*template_keyword_p=*/false,
fc6a28d7 3241 none_type,
a723baf1 3242 /*check_dependency=*/false,
a668c6ad
MM
3243 /*class_head_p=*/false,
3244 declarator_p);
a723baf1 3245 if (cp_parser_parse_definitely (parser))
88e95ee3 3246 done = true;
a723baf1
MM
3247 }
3248 /* In "N::S::~S", look in "N" as well. */
88e95ee3 3249 if (!done && scope && qualifying_scope)
a723baf1
MM
3250 {
3251 cp_parser_parse_tentatively (parser);
3252 parser->scope = qualifying_scope;
3253 parser->object_scope = NULL_TREE;
3254 parser->qualifying_scope = NULL_TREE;
21526606
EC
3255 type_decl
3256 = cp_parser_class_name (parser,
a723baf1
MM
3257 /*typename_keyword_p=*/false,
3258 /*template_keyword_p=*/false,
fc6a28d7 3259 none_type,
a723baf1 3260 /*check_dependency=*/false,
a668c6ad
MM
3261 /*class_head_p=*/false,
3262 declarator_p);
a723baf1 3263 if (cp_parser_parse_definitely (parser))
88e95ee3 3264 done = true;
a723baf1
MM
3265 }
3266 /* In "p->S::~T", look in the scope given by "*p" as well. */
88e95ee3 3267 else if (!done && object_scope)
a723baf1
MM
3268 {
3269 cp_parser_parse_tentatively (parser);
3270 parser->scope = object_scope;
3271 parser->object_scope = NULL_TREE;
3272 parser->qualifying_scope = NULL_TREE;
21526606
EC
3273 type_decl
3274 = cp_parser_class_name (parser,
a723baf1
MM
3275 /*typename_keyword_p=*/false,
3276 /*template_keyword_p=*/false,
fc6a28d7 3277 none_type,
a723baf1 3278 /*check_dependency=*/false,
a668c6ad
MM
3279 /*class_head_p=*/false,
3280 declarator_p);
a723baf1 3281 if (cp_parser_parse_definitely (parser))
88e95ee3 3282 done = true;
a723baf1
MM
3283 }
3284 /* Look in the surrounding context. */
88e95ee3
MM
3285 if (!done)
3286 {
3287 parser->scope = NULL_TREE;
3288 parser->object_scope = NULL_TREE;
3289 parser->qualifying_scope = NULL_TREE;
3290 type_decl
3291 = cp_parser_class_name (parser,
3292 /*typename_keyword_p=*/false,
3293 /*template_keyword_p=*/false,
3294 none_type,
3295 /*check_dependency=*/false,
3296 /*class_head_p=*/false,
3297 declarator_p);
3298 }
a723baf1
MM
3299 /* If an error occurred, assume that the name of the
3300 destructor is the same as the name of the qualifying
3301 class. That allows us to keep parsing after running
3302 into ill-formed destructor names. */
3303 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3304 return build_nt (BIT_NOT_EXPR, scope);
3305 else if (type_decl == error_mark_node)
3306 return error_mark_node;
3307
f3c2dfc6
MM
3308 /* [class.dtor]
3309
3310 A typedef-name that names a class shall not be used as the
3311 identifier in the declarator for a destructor declaration. */
21526606 3312 if (declarator_p
f3c2dfc6 3313 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
33a69702
VR
3314 && !DECL_SELF_REFERENCE_P (type_decl)
3315 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
2a13a625 3316 error ("typedef-name %qD used as destructor declarator",
f3c2dfc6
MM
3317 type_decl);
3318
a723baf1
MM
3319 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3320 }
3321
3322 case CPP_KEYWORD:
3323 if (token->keyword == RID_OPERATOR)
3324 {
3325 tree id;
3326
3327 /* This could be a template-id, so we try that first. */
3328 cp_parser_parse_tentatively (parser);
3329 /* Try a template-id. */
3330 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3331 /*check_dependency_p=*/true,
3332 declarator_p);
a723baf1
MM
3333 /* If that worked, we're done. */
3334 if (cp_parser_parse_definitely (parser))
3335 return id;
3336 /* We still don't know whether we're looking at an
3337 operator-function-id or a conversion-function-id. */
3338 cp_parser_parse_tentatively (parser);
3339 /* Try an operator-function-id. */
3340 id = cp_parser_operator_function_id (parser);
3341 /* If that didn't work, try a conversion-function-id. */
3342 if (!cp_parser_parse_definitely (parser))
3343 id = cp_parser_conversion_function_id (parser);
3344
3345 return id;
3346 }
3347 /* Fall through. */
3348
3349 default:
3350 cp_parser_error (parser, "expected unqualified-id");
3351 return error_mark_node;
3352 }
3353}
3354
3355/* Parse an (optional) nested-name-specifier.
3356
3357 nested-name-specifier:
3358 class-or-namespace-name :: nested-name-specifier [opt]
3359 class-or-namespace-name :: template nested-name-specifier [opt]
3360
3361 PARSER->SCOPE should be set appropriately before this function is
3362 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3363 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3364 in name lookups.
3365
3366 Sets PARSER->SCOPE to the class (TYPE) or namespace
3367 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3368 it unchanged if there is no nested-name-specifier. Returns the new
21526606 3369 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
a668c6ad
MM
3370
3371 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3372 part of a declaration and/or decl-specifier. */
a723baf1
MM
3373
3374static tree
21526606
EC
3375cp_parser_nested_name_specifier_opt (cp_parser *parser,
3376 bool typename_keyword_p,
a723baf1 3377 bool check_dependency_p,
a668c6ad
MM
3378 bool type_p,
3379 bool is_declaration)
a723baf1
MM
3380{
3381 bool success = false;
3382 tree access_check = NULL_TREE;
0c5e4866
NS
3383 cp_token_position start = 0;
3384 cp_token *token;
a723baf1
MM
3385
3386 /* If the next token corresponds to a nested name specifier, there
2050a1bb 3387 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
21526606 3388 false, it may have been true before, in which case something
2050a1bb
MM
3389 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3390 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3391 CHECK_DEPENDENCY_P is false, we have to fall through into the
3392 main loop. */
3393 if (check_dependency_p
3394 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3395 {
3396 cp_parser_pre_parsed_nested_name_specifier (parser);
a723baf1
MM
3397 return parser->scope;
3398 }
3399
3400 /* Remember where the nested-name-specifier starts. */
0b16f8f4 3401 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
0c5e4866 3402 start = cp_lexer_token_position (parser->lexer, false);
a723baf1 3403
8d241e0b 3404 push_deferring_access_checks (dk_deferred);
cf22909c 3405
a723baf1
MM
3406 while (true)
3407 {
3408 tree new_scope;
3409 tree old_scope;
3410 tree saved_qualifying_scope;
a723baf1
MM
3411 bool template_keyword_p;
3412
2050a1bb
MM
3413 /* Spot cases that cannot be the beginning of a
3414 nested-name-specifier. */
3415 token = cp_lexer_peek_token (parser->lexer);
3416
3417 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3418 the already parsed nested-name-specifier. */
3419 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3420 {
3421 /* Grab the nested-name-specifier and continue the loop. */
3422 cp_parser_pre_parsed_nested_name_specifier (parser);
3423 success = true;
3424 continue;
3425 }
3426
a723baf1
MM
3427 /* Spot cases that cannot be the beginning of a
3428 nested-name-specifier. On the second and subsequent times
3429 through the loop, we look for the `template' keyword. */
f7b5ecd9 3430 if (success && token->keyword == RID_TEMPLATE)
a723baf1
MM
3431 ;
3432 /* A template-id can start a nested-name-specifier. */
f7b5ecd9 3433 else if (token->type == CPP_TEMPLATE_ID)
a723baf1
MM
3434 ;
3435 else
3436 {
3437 /* If the next token is not an identifier, then it is
3438 definitely not a class-or-namespace-name. */
f7b5ecd9 3439 if (token->type != CPP_NAME)
a723baf1
MM
3440 break;
3441 /* If the following token is neither a `<' (to begin a
3442 template-id), nor a `::', then we are not looking at a
3443 nested-name-specifier. */
3444 token = cp_lexer_peek_nth_token (parser->lexer, 2);
f4abade9
GB
3445 if (token->type != CPP_SCOPE
3446 && !cp_parser_nth_token_starts_template_argument_list_p
3447 (parser, 2))
a723baf1
MM
3448 break;
3449 }
3450
3451 /* The nested-name-specifier is optional, so we parse
3452 tentatively. */
3453 cp_parser_parse_tentatively (parser);
3454
3455 /* Look for the optional `template' keyword, if this isn't the
3456 first time through the loop. */
3457 if (success)
3458 template_keyword_p = cp_parser_optional_template_keyword (parser);
3459 else
3460 template_keyword_p = false;
3461
3462 /* Save the old scope since the name lookup we are about to do
3463 might destroy it. */
3464 old_scope = parser->scope;
3465 saved_qualifying_scope = parser->qualifying_scope;
a52eb3bc
MM
3466 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3467 look up names in "X<T>::I" in order to determine that "Y" is
3468 a template. So, if we have a typename at this point, we make
3469 an effort to look through it. */
67bcc252
MM
3470 if (is_declaration
3471 && !typename_keyword_p
3472 && parser->scope
a52eb3bc
MM
3473 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3474 parser->scope = resolve_typename_type (parser->scope,
3475 /*only_current_p=*/false);
a723baf1 3476 /* Parse the qualifying entity. */
21526606 3477 new_scope
a723baf1
MM
3478 = cp_parser_class_or_namespace_name (parser,
3479 typename_keyword_p,
3480 template_keyword_p,
3481 check_dependency_p,
a668c6ad
MM
3482 type_p,
3483 is_declaration);
a723baf1
MM
3484 /* Look for the `::' token. */
3485 cp_parser_require (parser, CPP_SCOPE, "`::'");
3486
3487 /* If we found what we wanted, we keep going; otherwise, we're
3488 done. */
3489 if (!cp_parser_parse_definitely (parser))
3490 {
3491 bool error_p = false;
3492
3493 /* Restore the OLD_SCOPE since it was valid before the
3494 failed attempt at finding the last
3495 class-or-namespace-name. */
3496 parser->scope = old_scope;
3497 parser->qualifying_scope = saved_qualifying_scope;
3498 /* If the next token is an identifier, and the one after
3499 that is a `::', then any valid interpretation would have
3500 found a class-or-namespace-name. */
3501 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 3502 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1 3503 == CPP_SCOPE)
21526606 3504 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
a723baf1
MM
3505 != CPP_COMPL))
3506 {
3507 token = cp_lexer_consume_token (parser->lexer);
21526606 3508 if (!error_p)
a723baf1
MM
3509 {
3510 tree decl;
3511
3512 decl = cp_parser_lookup_name_simple (parser, token->value);
3513 if (TREE_CODE (decl) == TEMPLATE_DECL)
2a13a625 3514 error ("%qD used without template parameters", decl);
a723baf1 3515 else
21526606
EC
3516 cp_parser_name_lookup_error
3517 (parser, token->value, decl,
4bb8ca28 3518 "is not a class or namespace");
a723baf1
MM
3519 parser->scope = NULL_TREE;
3520 error_p = true;
eea9800f
MM
3521 /* Treat this as a successful nested-name-specifier
3522 due to:
3523
3524 [basic.lookup.qual]
3525
3526 If the name found is not a class-name (clause
3527 _class_) or namespace-name (_namespace.def_), the
3528 program is ill-formed. */
3529 success = true;
a723baf1
MM
3530 }
3531 cp_lexer_consume_token (parser->lexer);
3532 }
3533 break;
3534 }
3535
3536 /* We've found one valid nested-name-specifier. */
3537 success = true;
3538 /* Make sure we look in the right scope the next time through
3539 the loop. */
21526606 3540 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
a723baf1
MM
3541 ? TREE_TYPE (new_scope)
3542 : new_scope);
3543 /* If it is a class scope, try to complete it; we are about to
3544 be looking up names inside the class. */
8fbc5ae7
MM
3545 if (TYPE_P (parser->scope)
3546 /* Since checking types for dependency can be expensive,
3547 avoid doing it if the type is already complete. */
3548 && !COMPLETE_TYPE_P (parser->scope)
3549 /* Do not try to complete dependent types. */
1fb3244a 3550 && !dependent_type_p (parser->scope))
a723baf1
MM
3551 complete_type (parser->scope);
3552 }
3553
cf22909c
KL
3554 /* Retrieve any deferred checks. Do not pop this access checks yet
3555 so the memory will not be reclaimed during token replacing below. */
3556 access_check = get_deferred_access_checks ();
3557
a723baf1
MM
3558 /* If parsing tentatively, replace the sequence of tokens that makes
3559 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3560 token. That way, should we re-parse the token stream, we will
3561 not have to repeat the effort required to do the parse, nor will
3562 we issue duplicate error messages. */
0c5e4866 3563 if (success && start)
a723baf1 3564 {
0c5e4866
NS
3565 cp_token *token = cp_lexer_token_at (parser->lexer, start);
3566
a723baf1
MM
3567 /* Reset the contents of the START token. */
3568 token->type = CPP_NESTED_NAME_SPECIFIER;
3569 token->value = build_tree_list (access_check, parser->scope);
3570 TREE_TYPE (token->value) = parser->qualifying_scope;
3571 token->keyword = RID_MAX;
0c5e4866 3572
a723baf1 3573 /* Purge all subsequent tokens. */
0c5e4866 3574 cp_lexer_purge_tokens_after (parser->lexer, start);
a723baf1
MM
3575 }
3576
cf22909c 3577 pop_deferring_access_checks ();
a723baf1
MM
3578 return success ? parser->scope : NULL_TREE;
3579}
3580
3581/* Parse a nested-name-specifier. See
3582 cp_parser_nested_name_specifier_opt for details. This function
3583 behaves identically, except that it will an issue an error if no
3584 nested-name-specifier is present, and it will return
3585 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3586 is present. */
3587
3588static tree
21526606
EC
3589cp_parser_nested_name_specifier (cp_parser *parser,
3590 bool typename_keyword_p,
a723baf1 3591 bool check_dependency_p,
a668c6ad
MM
3592 bool type_p,
3593 bool is_declaration)
a723baf1
MM
3594{
3595 tree scope;
3596
3597 /* Look for the nested-name-specifier. */
3598 scope = cp_parser_nested_name_specifier_opt (parser,
3599 typename_keyword_p,
3600 check_dependency_p,
a668c6ad
MM
3601 type_p,
3602 is_declaration);
a723baf1
MM
3603 /* If it was not present, issue an error message. */
3604 if (!scope)
3605 {
3606 cp_parser_error (parser, "expected nested-name-specifier");
eb5abb39 3607 parser->scope = NULL_TREE;
a723baf1
MM
3608 return error_mark_node;
3609 }
3610
3611 return scope;
3612}
3613
3614/* Parse a class-or-namespace-name.
3615
3616 class-or-namespace-name:
3617 class-name
3618 namespace-name
3619
3620 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3621 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3622 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3623 TYPE_P is TRUE iff the next name should be taken as a class-name,
3624 even the same name is declared to be another entity in the same
3625 scope.
3626
3627 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
eea9800f
MM
3628 specified by the class-or-namespace-name. If neither is found the
3629 ERROR_MARK_NODE is returned. */
a723baf1
MM
3630
3631static tree
21526606 3632cp_parser_class_or_namespace_name (cp_parser *parser,
a723baf1
MM
3633 bool typename_keyword_p,
3634 bool template_keyword_p,
3635 bool check_dependency_p,
a668c6ad
MM
3636 bool type_p,
3637 bool is_declaration)
a723baf1
MM
3638{
3639 tree saved_scope;
3640 tree saved_qualifying_scope;
3641 tree saved_object_scope;
3642 tree scope;
eea9800f 3643 bool only_class_p;
a723baf1 3644
a723baf1
MM
3645 /* Before we try to parse the class-name, we must save away the
3646 current PARSER->SCOPE since cp_parser_class_name will destroy
3647 it. */
3648 saved_scope = parser->scope;
3649 saved_qualifying_scope = parser->qualifying_scope;
3650 saved_object_scope = parser->object_scope;
eea9800f
MM
3651 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3652 there is no need to look for a namespace-name. */
bbaab916 3653 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
eea9800f
MM
3654 if (!only_class_p)
3655 cp_parser_parse_tentatively (parser);
21526606 3656 scope = cp_parser_class_name (parser,
a723baf1
MM
3657 typename_keyword_p,
3658 template_keyword_p,
fc6a28d7 3659 type_p ? class_type : none_type,
a723baf1 3660 check_dependency_p,
a668c6ad
MM
3661 /*class_head_p=*/false,
3662 is_declaration);
a723baf1 3663 /* If that didn't work, try for a namespace-name. */
eea9800f 3664 if (!only_class_p && !cp_parser_parse_definitely (parser))
a723baf1
MM
3665 {
3666 /* Restore the saved scope. */
3667 parser->scope = saved_scope;
3668 parser->qualifying_scope = saved_qualifying_scope;
3669 parser->object_scope = saved_object_scope;
eea9800f
MM
3670 /* If we are not looking at an identifier followed by the scope
3671 resolution operator, then this is not part of a
3672 nested-name-specifier. (Note that this function is only used
3673 to parse the components of a nested-name-specifier.) */
3674 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3675 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3676 return error_mark_node;
a723baf1
MM
3677 scope = cp_parser_namespace_name (parser);
3678 }
3679
3680 return scope;
3681}
3682
3683/* Parse a postfix-expression.
3684
3685 postfix-expression:
3686 primary-expression
3687 postfix-expression [ expression ]
3688 postfix-expression ( expression-list [opt] )
3689 simple-type-specifier ( expression-list [opt] )
21526606 3690 typename :: [opt] nested-name-specifier identifier
a723baf1
MM
3691 ( expression-list [opt] )
3692 typename :: [opt] nested-name-specifier template [opt] template-id
3693 ( expression-list [opt] )
3694 postfix-expression . template [opt] id-expression
3695 postfix-expression -> template [opt] id-expression
3696 postfix-expression . pseudo-destructor-name
3697 postfix-expression -> pseudo-destructor-name
3698 postfix-expression ++
3699 postfix-expression --
3700 dynamic_cast < type-id > ( expression )
3701 static_cast < type-id > ( expression )
3702 reinterpret_cast < type-id > ( expression )
3703 const_cast < type-id > ( expression )
3704 typeid ( expression )
3705 typeid ( type-id )
3706
3707 GNU Extension:
21526606 3708
a723baf1
MM
3709 postfix-expression:
3710 ( type-id ) { initializer-list , [opt] }
3711
3712 This extension is a GNU version of the C99 compound-literal
3713 construct. (The C99 grammar uses `type-name' instead of `type-id',
3714 but they are essentially the same concept.)
3715
3716 If ADDRESS_P is true, the postfix expression is the operand of the
93678513
MM
3717 `&' operator. CAST_P is true if this expression is the target of a
3718 cast.
a723baf1
MM
3719
3720 Returns a representation of the expression. */
3721
3722static tree
93678513 3723cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
a723baf1
MM
3724{
3725 cp_token *token;
3726 enum rid keyword;
b3445994 3727 cp_id_kind idk = CP_ID_KIND_NONE;
a723baf1
MM
3728 tree postfix_expression = NULL_TREE;
3729 /* Non-NULL only if the current postfix-expression can be used to
3730 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3731 class used to qualify the member. */
3732 tree qualifying_class = NULL_TREE;
a723baf1
MM
3733
3734 /* Peek at the next token. */
3735 token = cp_lexer_peek_token (parser->lexer);
3736 /* Some of the productions are determined by keywords. */
3737 keyword = token->keyword;
3738 switch (keyword)
3739 {
3740 case RID_DYNCAST:
3741 case RID_STATCAST:
3742 case RID_REINTCAST:
3743 case RID_CONSTCAST:
3744 {
3745 tree type;
3746 tree expression;
3747 const char *saved_message;
3748
3749 /* All of these can be handled in the same way from the point
3750 of view of parsing. Begin by consuming the token
3751 identifying the cast. */
3752 cp_lexer_consume_token (parser->lexer);
21526606 3753
a723baf1
MM
3754 /* New types cannot be defined in the cast. */
3755 saved_message = parser->type_definition_forbidden_message;
3756 parser->type_definition_forbidden_message
3757 = "types may not be defined in casts";
3758
3759 /* Look for the opening `<'. */
3760 cp_parser_require (parser, CPP_LESS, "`<'");
3761 /* Parse the type to which we are casting. */
3762 type = cp_parser_type_id (parser);
3763 /* Look for the closing `>'. */
3764 cp_parser_require (parser, CPP_GREATER, "`>'");
3765 /* Restore the old message. */
3766 parser->type_definition_forbidden_message = saved_message;
3767
3768 /* And the expression which is being cast. */
3769 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
93678513 3770 expression = cp_parser_expression (parser, /*cast_p=*/true);
a723baf1
MM
3771 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3772
14d22dd6
MM
3773 /* Only type conversions to integral or enumeration types
3774 can be used in constant-expressions. */
67c03833 3775 if (parser->integral_constant_expression_p
14d22dd6 3776 && !dependent_type_p (type)
263ee052 3777 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
98ca843c 3778 && (cp_parser_non_integral_constant_expression
625cbf93
MM
3779 (parser,
3780 "a cast to a type other than an integral or "
3781 "enumeration type")))
3782 return error_mark_node;
14d22dd6 3783
a723baf1
MM
3784 switch (keyword)
3785 {
3786 case RID_DYNCAST:
3787 postfix_expression
3788 = build_dynamic_cast (type, expression);
3789 break;
3790 case RID_STATCAST:
3791 postfix_expression
3792 = build_static_cast (type, expression);
3793 break;
3794 case RID_REINTCAST:
3795 postfix_expression
3796 = build_reinterpret_cast (type, expression);
3797 break;
3798 case RID_CONSTCAST:
3799 postfix_expression
3800 = build_const_cast (type, expression);
3801 break;
3802 default:
315fb5db 3803 gcc_unreachable ();
a723baf1
MM
3804 }
3805 }
3806 break;
3807
3808 case RID_TYPEID:
3809 {
3810 tree type;
3811 const char *saved_message;
4f8163b1 3812 bool saved_in_type_id_in_expr_p;
a723baf1
MM
3813
3814 /* Consume the `typeid' token. */
3815 cp_lexer_consume_token (parser->lexer);
3816 /* Look for the `(' token. */
3817 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3818 /* Types cannot be defined in a `typeid' expression. */
3819 saved_message = parser->type_definition_forbidden_message;
3820 parser->type_definition_forbidden_message
3821 = "types may not be defined in a `typeid\' expression";
3822 /* We can't be sure yet whether we're looking at a type-id or an
3823 expression. */
3824 cp_parser_parse_tentatively (parser);
3825 /* Try a type-id first. */
4f8163b1
MM
3826 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3827 parser->in_type_id_in_expr_p = true;
a723baf1 3828 type = cp_parser_type_id (parser);
4f8163b1 3829 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
3830 /* Look for the `)' token. Otherwise, we can't be sure that
3831 we're not looking at an expression: consider `typeid (int
3832 (3))', for example. */
3833 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3834 /* If all went well, simply lookup the type-id. */
3835 if (cp_parser_parse_definitely (parser))
3836 postfix_expression = get_typeid (type);
3837 /* Otherwise, fall back to the expression variant. */
3838 else
3839 {
3840 tree expression;
3841
3842 /* Look for an expression. */
93678513 3843 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
3844 /* Compute its typeid. */
3845 postfix_expression = build_typeid (expression);
3846 /* Look for the `)' token. */
3847 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3848 }
4424e0da 3849 /* `typeid' may not appear in an integral constant expression. */
98ca843c 3850 if (cp_parser_non_integral_constant_expression(parser,
4424e0da
GB
3851 "`typeid' operator"))
3852 return error_mark_node;
a723baf1
MM
3853 /* Restore the saved message. */
3854 parser->type_definition_forbidden_message = saved_message;
3855 }
3856 break;
21526606 3857
a723baf1
MM
3858 case RID_TYPENAME:
3859 {
3860 bool template_p = false;
3861 tree id;
3862 tree type;
3863
3864 /* Consume the `typename' token. */
3865 cp_lexer_consume_token (parser->lexer);
3866 /* Look for the optional `::' operator. */
21526606 3867 cp_parser_global_scope_opt (parser,
a723baf1
MM
3868 /*current_scope_valid_p=*/false);
3869 /* Look for the nested-name-specifier. */
3870 cp_parser_nested_name_specifier (parser,
3871 /*typename_keyword_p=*/true,
3872 /*check_dependency_p=*/true,
a668c6ad
MM
3873 /*type_p=*/true,
3874 /*is_declaration=*/true);
a723baf1
MM
3875 /* Look for the optional `template' keyword. */
3876 template_p = cp_parser_optional_template_keyword (parser);
3877 /* We don't know whether we're looking at a template-id or an
3878 identifier. */
3879 cp_parser_parse_tentatively (parser);
3880 /* Try a template-id. */
3881 id = cp_parser_template_id (parser, template_p,
a668c6ad
MM
3882 /*check_dependency_p=*/true,
3883 /*is_declaration=*/true);
a723baf1
MM
3884 /* If that didn't work, try an identifier. */
3885 if (!cp_parser_parse_definitely (parser))
3886 id = cp_parser_identifier (parser);
26bcf8fc
MM
3887 /* If we look up a template-id in a non-dependent qualifying
3888 scope, there's no need to create a dependent type. */
3889 if (TREE_CODE (id) == TYPE_DECL
3890 && !dependent_type_p (parser->scope))
3891 type = TREE_TYPE (id);
a723baf1
MM
3892 /* Create a TYPENAME_TYPE to represent the type to which the
3893 functional cast is being performed. */
26bcf8fc 3894 else
98ca843c 3895 type = make_typename_type (parser->scope, id,
fc6a28d7 3896 typename_type,
26bcf8fc 3897 /*complain=*/1);
a723baf1
MM
3898
3899 postfix_expression = cp_parser_functional_cast (parser, type);
3900 }
3901 break;
3902
3903 default:
3904 {
3905 tree type;
3906
3907 /* If the next thing is a simple-type-specifier, we may be
3908 looking at a functional cast. We could also be looking at
3909 an id-expression. So, we try the functional cast, and if
3910 that doesn't work we fall back to the primary-expression. */
3911 cp_parser_parse_tentatively (parser);
3912 /* Look for the simple-type-specifier. */
21526606 3913 type = cp_parser_simple_type_specifier (parser,
62d1db17
MM
3914 /*decl_specs=*/NULL,
3915 CP_PARSER_FLAGS_NONE);
a723baf1
MM
3916 /* Parse the cast itself. */
3917 if (!cp_parser_error_occurred (parser))
21526606 3918 postfix_expression
a723baf1
MM
3919 = cp_parser_functional_cast (parser, type);
3920 /* If that worked, we're done. */
3921 if (cp_parser_parse_definitely (parser))
3922 break;
3923
3924 /* If the functional-cast didn't work out, try a
3925 compound-literal. */
14d22dd6
MM
3926 if (cp_parser_allow_gnu_extensions_p (parser)
3927 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
a723baf1
MM
3928 {
3929 tree initializer_list = NULL_TREE;
4f8163b1 3930 bool saved_in_type_id_in_expr_p;
a723baf1
MM
3931
3932 cp_parser_parse_tentatively (parser);
14d22dd6
MM
3933 /* Consume the `('. */
3934 cp_lexer_consume_token (parser->lexer);
3935 /* Parse the type. */
4f8163b1
MM
3936 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3937 parser->in_type_id_in_expr_p = true;
14d22dd6 3938 type = cp_parser_type_id (parser);
4f8163b1 3939 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14d22dd6
MM
3940 /* Look for the `)'. */
3941 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3942 /* Look for the `{'. */
3943 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3944 /* If things aren't going well, there's no need to
3945 keep going. */
3946 if (!cp_parser_error_occurred (parser))
a723baf1 3947 {
39703eb9 3948 bool non_constant_p;
14d22dd6 3949 /* Parse the initializer-list. */
21526606 3950 initializer_list
39703eb9 3951 = cp_parser_initializer_list (parser, &non_constant_p);
14d22dd6
MM
3952 /* Allow a trailing `,'. */
3953 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3954 cp_lexer_consume_token (parser->lexer);
3955 /* Look for the final `}'. */
3956 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1
MM
3957 }
3958 /* If that worked, we're definitely looking at a
3959 compound-literal expression. */
3960 if (cp_parser_parse_definitely (parser))
3961 {
3962 /* Warn the user that a compound literal is not
3963 allowed in standard C++. */
3964 if (pedantic)
3965 pedwarn ("ISO C++ forbids compound-literals");
3966 /* Form the representation of the compound-literal. */
21526606 3967 postfix_expression
a723baf1
MM
3968 = finish_compound_literal (type, initializer_list);
3969 break;
3970 }
3971 }
3972
3973 /* It must be a primary-expression. */
21526606 3974 postfix_expression = cp_parser_primary_expression (parser,
93678513 3975 cast_p,
a723baf1
MM
3976 &idk,
3977 &qualifying_class);
3978 }
3979 break;
3980 }
3981
ee76b931
MM
3982 /* If we were avoiding committing to the processing of a
3983 qualified-id until we knew whether or not we had a
3984 pointer-to-member, we now know. */
089d6ea7 3985 if (qualifying_class)
a723baf1 3986 {
ee76b931 3987 bool done;
a723baf1 3988
ee76b931
MM
3989 /* Peek at the next token. */
3990 token = cp_lexer_peek_token (parser->lexer);
3991 done = (token->type != CPP_OPEN_SQUARE
3992 && token->type != CPP_OPEN_PAREN
3993 && token->type != CPP_DOT
3994 && token->type != CPP_DEREF
3995 && token->type != CPP_PLUS_PLUS
3996 && token->type != CPP_MINUS_MINUS);
3997
3998 postfix_expression = finish_qualified_id_expr (qualifying_class,
3999 postfix_expression,
4000 done,
4001 address_p);
4002 if (done)
4003 return postfix_expression;
a723baf1
MM
4004 }
4005
a723baf1
MM
4006 /* Keep looping until the postfix-expression is complete. */
4007 while (true)
4008 {
10b1d5e7
MM
4009 if (idk == CP_ID_KIND_UNQUALIFIED
4010 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
a723baf1 4011 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
b3445994 4012 /* It is not a Koenig lookup function call. */
21526606 4013 postfix_expression
b3445994 4014 = unqualified_name_lookup_error (postfix_expression);
21526606 4015
a723baf1
MM
4016 /* Peek at the next token. */
4017 token = cp_lexer_peek_token (parser->lexer);
4018
4019 switch (token->type)
4020 {
4021 case CPP_OPEN_SQUARE:
7a3ea201
RH
4022 postfix_expression
4023 = cp_parser_postfix_open_square_expression (parser,
4024 postfix_expression,
4025 false);
4026 idk = CP_ID_KIND_NONE;
a723baf1
MM
4027 break;
4028
4029 case CPP_OPEN_PAREN:
4030 /* postfix-expression ( expression-list [opt] ) */
4031 {
6d80c4b9 4032 bool koenig_p;
21526606 4033 tree args = (cp_parser_parenthesized_expression_list
93678513
MM
4034 (parser, false,
4035 /*cast_p=*/false,
4036 /*non_constant_p=*/NULL));
a723baf1 4037
7efa3e22
NS
4038 if (args == error_mark_node)
4039 {
4040 postfix_expression = error_mark_node;
4041 break;
4042 }
21526606 4043
14d22dd6
MM
4044 /* Function calls are not permitted in
4045 constant-expressions. */
100d337a
MA
4046 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4047 && cp_parser_non_integral_constant_expression (parser,
4048 "a function call"))
14d22dd6 4049 {
625cbf93
MM
4050 postfix_expression = error_mark_node;
4051 break;
14d22dd6 4052 }
a723baf1 4053
6d80c4b9 4054 koenig_p = false;
399dedb9
NS
4055 if (idk == CP_ID_KIND_UNQUALIFIED)
4056 {
89d594a2
NS
4057 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4058 {
4059 if (args)
4060 {
4061 koenig_p = true;
4062 postfix_expression
4063 = perform_koenig_lookup (postfix_expression, args);
4064 }
4065 else
4066 postfix_expression
4067 = unqualified_fn_lookup_error (postfix_expression);
4068 }
676e33ca
MM
4069 /* We do not perform argument-dependent lookup if
4070 normal lookup finds a non-function, in accordance
4071 with the expected resolution of DR 218. */
89d594a2 4072 else if (args && is_overloaded_fn (postfix_expression))
6d80c4b9 4073 {
89d594a2
NS
4074 tree fn = get_first_fn (postfix_expression);
4075
4076 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4077 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4078
4079 /* Only do argument dependent lookup if regular
4080 lookup does not find a set of member functions.
4081 [basic.lookup.koenig]/2a */
4082 if (!DECL_FUNCTION_MEMBER_P (fn))
4083 {
4084 koenig_p = true;
4085 postfix_expression
4086 = perform_koenig_lookup (postfix_expression, args);
4087 }
6d80c4b9 4088 }
399dedb9 4089 }
21526606 4090
d17811fd 4091 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
a723baf1 4092 {
d17811fd
MM
4093 tree instance = TREE_OPERAND (postfix_expression, 0);
4094 tree fn = TREE_OPERAND (postfix_expression, 1);
4095
4096 if (processing_template_decl
4097 && (type_dependent_expression_p (instance)
4098 || (!BASELINK_P (fn)
4099 && TREE_CODE (fn) != FIELD_DECL)
584672ee 4100 || type_dependent_expression_p (fn)
d17811fd
MM
4101 || any_type_dependent_arguments_p (args)))
4102 {
4103 postfix_expression
6de9cd9a
DN
4104 = build_min_nt (CALL_EXPR, postfix_expression,
4105 args, NULL_TREE);
d17811fd
MM
4106 break;
4107 }
9f880ef9
MM
4108
4109 if (BASELINK_P (fn))
4110 postfix_expression
21526606
EC
4111 = (build_new_method_call
4112 (instance, fn, args, NULL_TREE,
4113 (idk == CP_ID_KIND_QUALIFIED
9f880ef9
MM
4114 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4115 else
4116 postfix_expression
4117 = finish_call_expr (postfix_expression, args,
4118 /*disallow_virtual=*/false,
4119 /*koenig_p=*/false);
a723baf1 4120 }
d17811fd
MM
4121 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4122 || TREE_CODE (postfix_expression) == MEMBER_REF
4123 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
a723baf1
MM
4124 postfix_expression = (build_offset_ref_call_from_tree
4125 (postfix_expression, args));
b3445994 4126 else if (idk == CP_ID_KIND_QUALIFIED)
2050a1bb
MM
4127 /* A call to a static class member, or a namespace-scope
4128 function. */
4129 postfix_expression
4130 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
4131 /*disallow_virtual=*/true,
4132 koenig_p);
a723baf1 4133 else
2050a1bb 4134 /* All other function calls. */
21526606
EC
4135 postfix_expression
4136 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
4137 /*disallow_virtual=*/false,
4138 koenig_p);
a723baf1
MM
4139
4140 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
b3445994 4141 idk = CP_ID_KIND_NONE;
a723baf1
MM
4142 }
4143 break;
21526606 4144
a723baf1
MM
4145 case CPP_DOT:
4146 case CPP_DEREF:
21526606
EC
4147 /* postfix-expression . template [opt] id-expression
4148 postfix-expression . pseudo-destructor-name
a723baf1
MM
4149 postfix-expression -> template [opt] id-expression
4150 postfix-expression -> pseudo-destructor-name */
98ca843c 4151
7a3ea201
RH
4152 /* Consume the `.' or `->' operator. */
4153 cp_lexer_consume_token (parser->lexer);
a723baf1 4154
7a3ea201
RH
4155 postfix_expression
4156 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4157 postfix_expression,
4158 false, &idk);
a723baf1
MM
4159 break;
4160
4161 case CPP_PLUS_PLUS:
4162 /* postfix-expression ++ */
4163 /* Consume the `++' token. */
4164 cp_lexer_consume_token (parser->lexer);
a5ac3982 4165 /* Generate a representation for the complete expression. */
21526606
EC
4166 postfix_expression
4167 = finish_increment_expr (postfix_expression,
a5ac3982 4168 POSTINCREMENT_EXPR);
14d22dd6 4169 /* Increments may not appear in constant-expressions. */
625cbf93
MM
4170 if (cp_parser_non_integral_constant_expression (parser,
4171 "an increment"))
4172 postfix_expression = error_mark_node;
b3445994 4173 idk = CP_ID_KIND_NONE;
a723baf1
MM
4174 break;
4175
4176 case CPP_MINUS_MINUS:
4177 /* postfix-expression -- */
4178 /* Consume the `--' token. */
4179 cp_lexer_consume_token (parser->lexer);
a5ac3982 4180 /* Generate a representation for the complete expression. */
21526606
EC
4181 postfix_expression
4182 = finish_increment_expr (postfix_expression,
a5ac3982 4183 POSTDECREMENT_EXPR);
14d22dd6 4184 /* Decrements may not appear in constant-expressions. */
625cbf93
MM
4185 if (cp_parser_non_integral_constant_expression (parser,
4186 "a decrement"))
4187 postfix_expression = error_mark_node;
b3445994 4188 idk = CP_ID_KIND_NONE;
a723baf1
MM
4189 break;
4190
4191 default:
4192 return postfix_expression;
4193 }
4194 }
4195
4196 /* We should never get here. */
315fb5db 4197 gcc_unreachable ();
a723baf1
MM
4198 return error_mark_node;
4199}
4200
7a3ea201
RH
4201/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4202 by cp_parser_builtin_offsetof. We're looking for
4203
4204 postfix-expression [ expression ]
4205
4206 FOR_OFFSETOF is set if we're being called in that context, which
4207 changes how we deal with integer constant expressions. */
4208
4209static tree
4210cp_parser_postfix_open_square_expression (cp_parser *parser,
4211 tree postfix_expression,
4212 bool for_offsetof)
4213{
4214 tree index;
4215
4216 /* Consume the `[' token. */
4217 cp_lexer_consume_token (parser->lexer);
4218
4219 /* Parse the index expression. */
4220 /* ??? For offsetof, there is a question of what to allow here. If
4221 offsetof is not being used in an integral constant expression context,
4222 then we *could* get the right answer by computing the value at runtime.
4223 If we are in an integral constant expression context, then we might
4224 could accept any constant expression; hard to say without analysis.
4225 Rather than open the barn door too wide right away, allow only integer
77880ae4 4226 constant expressions here. */
7a3ea201
RH
4227 if (for_offsetof)
4228 index = cp_parser_constant_expression (parser, false, NULL);
4229 else
93678513 4230 index = cp_parser_expression (parser, /*cast_p=*/false);
7a3ea201
RH
4231
4232 /* Look for the closing `]'. */
4233 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4234
4235 /* Build the ARRAY_REF. */
4236 postfix_expression = grok_array_decl (postfix_expression, index);
4237
4238 /* When not doing offsetof, array references are not permitted in
4239 constant-expressions. */
4240 if (!for_offsetof
4241 && (cp_parser_non_integral_constant_expression
4242 (parser, "an array reference")))
4243 postfix_expression = error_mark_node;
4244
4245 return postfix_expression;
4246}
4247
4248/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4249 by cp_parser_builtin_offsetof. We're looking for
4250
4251 postfix-expression . template [opt] id-expression
4252 postfix-expression . pseudo-destructor-name
4253 postfix-expression -> template [opt] id-expression
4254 postfix-expression -> pseudo-destructor-name
4255
4256 FOR_OFFSETOF is set if we're being called in that context. That sorta
4257 limits what of the above we'll actually accept, but nevermind.
4258 TOKEN_TYPE is the "." or "->" token, which will already have been
4259 removed from the stream. */
4260
4261static tree
4262cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4263 enum cpp_ttype token_type,
4264 tree postfix_expression,
4265 bool for_offsetof, cp_id_kind *idk)
4266{
4267 tree name;
4268 bool dependent_p;
4269 bool template_p;
17a27b4f 4270 bool pseudo_destructor_p;
7a3ea201
RH
4271 tree scope = NULL_TREE;
4272
4273 /* If this is a `->' operator, dereference the pointer. */
4274 if (token_type == CPP_DEREF)
4275 postfix_expression = build_x_arrow (postfix_expression);
4276 /* Check to see whether or not the expression is type-dependent. */
4277 dependent_p = type_dependent_expression_p (postfix_expression);
4278 /* The identifier following the `->' or `.' is not qualified. */
4279 parser->scope = NULL_TREE;
4280 parser->qualifying_scope = NULL_TREE;
4281 parser->object_scope = NULL_TREE;
4282 *idk = CP_ID_KIND_NONE;
4283 /* Enter the scope corresponding to the type of the object
4284 given by the POSTFIX_EXPRESSION. */
4285 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4286 {
4287 scope = TREE_TYPE (postfix_expression);
4288 /* According to the standard, no expression should ever have
4289 reference type. Unfortunately, we do not currently match
4290 the standard in this respect in that our internal representation
4291 of an expression may have reference type even when the standard
4292 says it does not. Therefore, we have to manually obtain the
4293 underlying type here. */
4294 scope = non_reference (scope);
4295 /* The type of the POSTFIX_EXPRESSION must be complete. */
4296 scope = complete_type_or_else (scope, NULL_TREE);
4297 /* Let the name lookup machinery know that we are processing a
4298 class member access expression. */
4299 parser->context->object_type = scope;
4300 /* If something went wrong, we want to be able to discern that case,
4301 as opposed to the case where there was no SCOPE due to the type
4302 of expression being dependent. */
4303 if (!scope)
4304 scope = error_mark_node;
4305 /* If the SCOPE was erroneous, make the various semantic analysis
4306 functions exit quickly -- and without issuing additional error
4307 messages. */
4308 if (scope == error_mark_node)
4309 postfix_expression = error_mark_node;
4310 }
4311
17a27b4f
MM
4312 /* Assume this expression is not a pseudo-destructor access. */
4313 pseudo_destructor_p = false;
4314
4315 /* If the SCOPE is a scalar type, then, if this is a valid program,
4316 we must be looking at a pseudo-destructor-name. */
4317 if (scope && SCALAR_TYPE_P (scope))
7a3ea201 4318 {
17a27b4f
MM
4319 tree s;
4320 tree type;
4321
4322 cp_parser_parse_tentatively (parser);
4323 /* Parse the pseudo-destructor-name. */
4324 s = NULL_TREE;
4325 cp_parser_pseudo_destructor_name (parser, &s, &type);
4326 if (cp_parser_parse_definitely (parser))
4327 {
4328 pseudo_destructor_p = true;
4329 postfix_expression
4330 = finish_pseudo_destructor_expr (postfix_expression,
4331 s, TREE_TYPE (type));
4332 }
4333 }
4334
4335 if (!pseudo_destructor_p)
4336 {
4337 /* If the SCOPE is not a scalar type, we are looking at an
4338 ordinary class member access expression, rather than a
4339 pseudo-destructor-name. */
7a3ea201
RH
4340 template_p = cp_parser_optional_template_keyword (parser);
4341 /* Parse the id-expression. */
4342 name = cp_parser_id_expression (parser, template_p,
4343 /*check_dependency_p=*/true,
4344 /*template_p=*/NULL,
4345 /*declarator_p=*/false);
4346 /* In general, build a SCOPE_REF if the member name is qualified.
4347 However, if the name was not dependent and has already been
4348 resolved; there is no need to build the SCOPE_REF. For example;
4349
4350 struct X { void f(); };
4351 template <typename T> void f(T* t) { t->X::f(); }
4352
4353 Even though "t" is dependent, "X::f" is not and has been resolved
4354 to a BASELINK; there is no need to include scope information. */
4355
4356 /* But we do need to remember that there was an explicit scope for
4357 virtual function calls. */
4358 if (parser->scope)
4359 *idk = CP_ID_KIND_QUALIFIED;
4360
fc6a28d7
MM
4361 /* If the name is a template-id that names a type, we will get a
4362 TYPE_DECL here. That is invalid code. */
4363 if (TREE_CODE (name) == TYPE_DECL)
7a3ea201 4364 {
fc6a28d7
MM
4365 error ("invalid use of %qD", name);
4366 postfix_expression = error_mark_node;
4367 }
4368 else
4369 {
4370 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4371 {
4372 name = build_nt (SCOPE_REF, parser->scope, name);
4373 parser->scope = NULL_TREE;
4374 parser->qualifying_scope = NULL_TREE;
4375 parser->object_scope = NULL_TREE;
4376 }
4377 if (scope && name && BASELINK_P (name))
4378 adjust_result_of_qualified_name_lookup
4379 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4380 postfix_expression
4381 = finish_class_member_access_expr (postfix_expression, name);
7a3ea201 4382 }
7a3ea201 4383 }
7a3ea201
RH
4384
4385 /* We no longer need to look up names in the scope of the object on
4386 the left-hand side of the `.' or `->' operator. */
4387 parser->context->object_type = NULL_TREE;
4388
4389 /* Outside of offsetof, these operators may not appear in
4390 constant-expressions. */
4391 if (!for_offsetof
98ca843c 4392 && (cp_parser_non_integral_constant_expression
7a3ea201
RH
4393 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4394 postfix_expression = error_mark_node;
4395
4396 return postfix_expression;
4397}
4398
7efa3e22 4399/* Parse a parenthesized expression-list.
a723baf1
MM
4400
4401 expression-list:
4402 assignment-expression
4403 expression-list, assignment-expression
4404
7efa3e22
NS
4405 attribute-list:
4406 expression-list
4407 identifier
4408 identifier, expression-list
4409
93678513
MM
4410 CAST_P is true if this expression is the target of a cast.
4411
a723baf1
MM
4412 Returns a TREE_LIST. The TREE_VALUE of each node is a
4413 representation of an assignment-expression. Note that a TREE_LIST
7efa3e22
NS
4414 is returned even if there is only a single expression in the list.
4415 error_mark_node is returned if the ( and or ) are
4416 missing. NULL_TREE is returned on no expressions. The parentheses
4417 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
39703eb9
MM
4418 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4419 indicates whether or not all of the expressions in the list were
4420 constant. */
a723baf1
MM
4421
4422static tree
21526606 4423cp_parser_parenthesized_expression_list (cp_parser* parser,
39703eb9 4424 bool is_attribute_list,
93678513 4425 bool cast_p,
39703eb9 4426 bool *non_constant_p)
a723baf1
MM
4427{
4428 tree expression_list = NULL_TREE;
1ed3dfd5 4429 bool fold_expr_p = is_attribute_list;
7efa3e22 4430 tree identifier = NULL_TREE;
39703eb9
MM
4431
4432 /* Assume all the expressions will be constant. */
4433 if (non_constant_p)
4434 *non_constant_p = false;
4435
7efa3e22
NS
4436 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4437 return error_mark_node;
21526606 4438
a723baf1 4439 /* Consume expressions until there are no more. */
7efa3e22
NS
4440 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4441 while (true)
4442 {
4443 tree expr;
21526606 4444
7efa3e22
NS
4445 /* At the beginning of attribute lists, check to see if the
4446 next token is an identifier. */
4447 if (is_attribute_list
4448 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4449 {
4450 cp_token *token;
21526606 4451
7efa3e22
NS
4452 /* Consume the identifier. */
4453 token = cp_lexer_consume_token (parser->lexer);
4454 /* Save the identifier. */
4455 identifier = token->value;
4456 }
4457 else
4458 {
4459 /* Parse the next assignment-expression. */
39703eb9
MM
4460 if (non_constant_p)
4461 {
4462 bool expr_non_constant_p;
21526606 4463 expr = (cp_parser_constant_expression
39703eb9
MM
4464 (parser, /*allow_non_constant_p=*/true,
4465 &expr_non_constant_p));
4466 if (expr_non_constant_p)
4467 *non_constant_p = true;
4468 }
4469 else
93678513 4470 expr = cp_parser_assignment_expression (parser, cast_p);
a723baf1 4471
1ed3dfd5
GB
4472 if (fold_expr_p)
4473 expr = fold_non_dependent_expr (expr);
4474
7efa3e22
NS
4475 /* Add it to the list. We add error_mark_node
4476 expressions to the list, so that we can still tell if
4477 the correct form for a parenthesized expression-list
4478 is found. That gives better errors. */
4479 expression_list = tree_cons (NULL_TREE, expr, expression_list);
a723baf1 4480
7efa3e22
NS
4481 if (expr == error_mark_node)
4482 goto skip_comma;
4483 }
a723baf1 4484
7efa3e22
NS
4485 /* After the first item, attribute lists look the same as
4486 expression lists. */
4487 is_attribute_list = false;
21526606 4488
7efa3e22
NS
4489 get_comma:;
4490 /* If the next token isn't a `,', then we are done. */
4491 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4492 break;
4493
4494 /* Otherwise, consume the `,' and keep going. */
4495 cp_lexer_consume_token (parser->lexer);
4496 }
21526606 4497
7efa3e22
NS
4498 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4499 {
4500 int ending;
21526606 4501
7efa3e22
NS
4502 skip_comma:;
4503 /* We try and resync to an unnested comma, as that will give the
4504 user better diagnostics. */
21526606
EC
4505 ending = cp_parser_skip_to_closing_parenthesis (parser,
4506 /*recovering=*/true,
4bb8ca28 4507 /*or_comma=*/true,
a668c6ad 4508 /*consume_paren=*/true);
7efa3e22
NS
4509 if (ending < 0)
4510 goto get_comma;
4511 if (!ending)
4512 return error_mark_node;
a723baf1
MM
4513 }
4514
4515 /* We built up the list in reverse order so we must reverse it now. */
7efa3e22
NS
4516 expression_list = nreverse (expression_list);
4517 if (identifier)
4518 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
21526606 4519
7efa3e22 4520 return expression_list;
a723baf1
MM
4521}
4522
4523/* Parse a pseudo-destructor-name.
4524
4525 pseudo-destructor-name:
4526 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4527 :: [opt] nested-name-specifier template template-id :: ~ type-name
4528 :: [opt] nested-name-specifier [opt] ~ type-name
4529
4530 If either of the first two productions is used, sets *SCOPE to the
4531 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4532 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
d6e57462 4533 or ERROR_MARK_NODE if the parse fails. */
a723baf1
MM
4534
4535static void
21526606
EC
4536cp_parser_pseudo_destructor_name (cp_parser* parser,
4537 tree* scope,
94edc4ab 4538 tree* type)
a723baf1
MM
4539{
4540 bool nested_name_specifier_p;
4541
b14454ba
MM
4542 /* Assume that things will not work out. */
4543 *type = error_mark_node;
4544
a723baf1
MM
4545 /* Look for the optional `::' operator. */
4546 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4547 /* Look for the optional nested-name-specifier. */
21526606 4548 nested_name_specifier_p
a723baf1
MM
4549 = (cp_parser_nested_name_specifier_opt (parser,
4550 /*typename_keyword_p=*/false,
4551 /*check_dependency_p=*/true,
a668c6ad 4552 /*type_p=*/false,
21526606 4553 /*is_declaration=*/true)
a723baf1
MM
4554 != NULL_TREE);
4555 /* Now, if we saw a nested-name-specifier, we might be doing the
4556 second production. */
21526606 4557 if (nested_name_specifier_p
a723baf1
MM
4558 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4559 {
4560 /* Consume the `template' keyword. */
4561 cp_lexer_consume_token (parser->lexer);
4562 /* Parse the template-id. */
21526606 4563 cp_parser_template_id (parser,
a723baf1 4564 /*template_keyword_p=*/true,
a668c6ad
MM
4565 /*check_dependency_p=*/false,
4566 /*is_declaration=*/true);
a723baf1
MM
4567 /* Look for the `::' token. */
4568 cp_parser_require (parser, CPP_SCOPE, "`::'");
4569 }
4570 /* If the next token is not a `~', then there might be some
9bcb9aae 4571 additional qualification. */
a723baf1
MM
4572 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4573 {
4574 /* Look for the type-name. */
4575 *scope = TREE_TYPE (cp_parser_type_name (parser));
d6e57462 4576
b14454ba
MM
4577 if (*scope == error_mark_node)
4578 return;
4579
4580 /* If we don't have ::~, then something has gone wrong. Since
4581 the only caller of this function is looking for something
4582 after `.' or `->' after a scalar type, most likely the
4583 program is trying to get a member of a non-aggregate
4584 type. */
4585 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
d6e57462
ILT
4586 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4587 {
4588 cp_parser_error (parser, "request for member of non-aggregate type");
d6e57462
ILT
4589 return;
4590 }
4591
a723baf1
MM
4592 /* Look for the `::' token. */
4593 cp_parser_require (parser, CPP_SCOPE, "`::'");
4594 }
4595 else
4596 *scope = NULL_TREE;
4597
4598 /* Look for the `~'. */
4599 cp_parser_require (parser, CPP_COMPL, "`~'");
4600 /* Look for the type-name again. We are not responsible for
4601 checking that it matches the first type-name. */
4602 *type = cp_parser_type_name (parser);
4603}
4604
4605/* Parse a unary-expression.
4606
4607 unary-expression:
4608 postfix-expression
4609 ++ cast-expression
4610 -- cast-expression
4611 unary-operator cast-expression
4612 sizeof unary-expression
4613 sizeof ( type-id )
4614 new-expression
4615 delete-expression
4616
4617 GNU Extensions:
4618
4619 unary-expression:
4620 __extension__ cast-expression
4621 __alignof__ unary-expression
4622 __alignof__ ( type-id )
4623 __real__ cast-expression
4624 __imag__ cast-expression
4625 && identifier
4626
4627 ADDRESS_P is true iff the unary-expression is appearing as the
93678513
MM
4628 operand of the `&' operator. CAST_P is true if this expression is
4629 the target of a cast.
a723baf1 4630
34cd5ae7 4631 Returns a representation of the expression. */
a723baf1
MM
4632
4633static tree
93678513 4634cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
a723baf1
MM
4635{
4636 cp_token *token;
4637 enum tree_code unary_operator;
4638
4639 /* Peek at the next token. */
4640 token = cp_lexer_peek_token (parser->lexer);
4641 /* Some keywords give away the kind of expression. */
4642 if (token->type == CPP_KEYWORD)
4643 {
4644 enum rid keyword = token->keyword;
4645
4646 switch (keyword)
4647 {
4648 case RID_ALIGNOF:
a723baf1
MM
4649 case RID_SIZEOF:
4650 {
4651 tree operand;
7a18b933 4652 enum tree_code op;
21526606 4653
7a18b933
NS
4654 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4655 /* Consume the token. */
a723baf1
MM
4656 cp_lexer_consume_token (parser->lexer);
4657 /* Parse the operand. */
4658 operand = cp_parser_sizeof_operand (parser, keyword);
4659
7a18b933
NS
4660 if (TYPE_P (operand))
4661 return cxx_sizeof_or_alignof_type (operand, op, true);
a723baf1 4662 else
7a18b933 4663 return cxx_sizeof_or_alignof_expr (operand, op);
a723baf1
MM
4664 }
4665
4666 case RID_NEW:
4667 return cp_parser_new_expression (parser);
4668
4669 case RID_DELETE:
4670 return cp_parser_delete_expression (parser);
21526606 4671
a723baf1
MM
4672 case RID_EXTENSION:
4673 {
4674 /* The saved value of the PEDANTIC flag. */
4675 int saved_pedantic;
4676 tree expr;
4677
4678 /* Save away the PEDANTIC flag. */
4679 cp_parser_extension_opt (parser, &saved_pedantic);
4680 /* Parse the cast-expression. */
d6b4ea85 4681 expr = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4682 /* Restore the PEDANTIC flag. */
4683 pedantic = saved_pedantic;
4684
4685 return expr;
4686 }
4687
4688 case RID_REALPART:
4689 case RID_IMAGPART:
4690 {
4691 tree expression;
4692
4693 /* Consume the `__real__' or `__imag__' token. */
4694 cp_lexer_consume_token (parser->lexer);
4695 /* Parse the cast-expression. */
d6b4ea85 4696 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4697 /* Create the complete representation. */
4698 return build_x_unary_op ((keyword == RID_REALPART
4699 ? REALPART_EXPR : IMAGPART_EXPR),
4700 expression);
4701 }
4702 break;
4703
4704 default:
4705 break;
4706 }
4707 }
4708
4709 /* Look for the `:: new' and `:: delete', which also signal the
4710 beginning of a new-expression, or delete-expression,
4711 respectively. If the next token is `::', then it might be one of
4712 these. */
4713 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4714 {
4715 enum rid keyword;
4716
4717 /* See if the token after the `::' is one of the keywords in
4718 which we're interested. */
4719 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4720 /* If it's `new', we have a new-expression. */
4721 if (keyword == RID_NEW)
4722 return cp_parser_new_expression (parser);
4723 /* Similarly, for `delete'. */
4724 else if (keyword == RID_DELETE)
4725 return cp_parser_delete_expression (parser);
4726 }
4727
4728 /* Look for a unary operator. */
4729 unary_operator = cp_parser_unary_operator (token);
4730 /* The `++' and `--' operators can be handled similarly, even though
4731 they are not technically unary-operators in the grammar. */
4732 if (unary_operator == ERROR_MARK)
4733 {
4734 if (token->type == CPP_PLUS_PLUS)
4735 unary_operator = PREINCREMENT_EXPR;
4736 else if (token->type == CPP_MINUS_MINUS)
4737 unary_operator = PREDECREMENT_EXPR;
4738 /* Handle the GNU address-of-label extension. */
4739 else if (cp_parser_allow_gnu_extensions_p (parser)
4740 && token->type == CPP_AND_AND)
4741 {
4742 tree identifier;
4743
4744 /* Consume the '&&' token. */
4745 cp_lexer_consume_token (parser->lexer);
4746 /* Look for the identifier. */
4747 identifier = cp_parser_identifier (parser);
4748 /* Create an expression representing the address. */
4749 return finish_label_address_expr (identifier);
4750 }
4751 }
4752 if (unary_operator != ERROR_MARK)
4753 {
4754 tree cast_expression;
a5ac3982
MM
4755 tree expression = error_mark_node;
4756 const char *non_constant_p = NULL;
a723baf1
MM
4757
4758 /* Consume the operator token. */
4759 token = cp_lexer_consume_token (parser->lexer);
4760 /* Parse the cast-expression. */
21526606 4761 cast_expression
93678513
MM
4762 = cp_parser_cast_expression (parser,
4763 unary_operator == ADDR_EXPR,
4764 /*cast_p=*/false);
a723baf1
MM
4765 /* Now, build an appropriate representation. */
4766 switch (unary_operator)
4767 {
4768 case INDIRECT_REF:
a5ac3982
MM
4769 non_constant_p = "`*'";
4770 expression = build_x_indirect_ref (cast_expression, "unary *");
4771 break;
4772
a723baf1 4773 case ADDR_EXPR:
7a3ea201 4774 non_constant_p = "`&'";
a5ac3982 4775 /* Fall through. */
d17811fd 4776 case BIT_NOT_EXPR:
a5ac3982
MM
4777 expression = build_x_unary_op (unary_operator, cast_expression);
4778 break;
4779
14d22dd6
MM
4780 case PREINCREMENT_EXPR:
4781 case PREDECREMENT_EXPR:
a5ac3982
MM
4782 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4783 ? "`++'" : "`--'");
14d22dd6 4784 /* Fall through. */
a723baf1
MM
4785 case CONVERT_EXPR:
4786 case NEGATE_EXPR:
4787 case TRUTH_NOT_EXPR:
a5ac3982
MM
4788 expression = finish_unary_op_expr (unary_operator, cast_expression);
4789 break;
a723baf1 4790
a723baf1 4791 default:
315fb5db 4792 gcc_unreachable ();
a723baf1 4793 }
a5ac3982 4794
98ca843c 4795 if (non_constant_p
625cbf93
MM
4796 && cp_parser_non_integral_constant_expression (parser,
4797 non_constant_p))
4798 expression = error_mark_node;
a5ac3982
MM
4799
4800 return expression;
a723baf1
MM
4801 }
4802
93678513 4803 return cp_parser_postfix_expression (parser, address_p, cast_p);
a723baf1
MM
4804}
4805
4806/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4807 unary-operator, the corresponding tree code is returned. */
4808
4809static enum tree_code
94edc4ab 4810cp_parser_unary_operator (cp_token* token)
a723baf1
MM
4811{
4812 switch (token->type)
4813 {
4814 case CPP_MULT:
4815 return INDIRECT_REF;
4816
4817 case CPP_AND:
4818 return ADDR_EXPR;
4819
4820 case CPP_PLUS:
4821 return CONVERT_EXPR;
4822
4823 case CPP_MINUS:
4824 return NEGATE_EXPR;
4825
4826 case CPP_NOT:
4827 return TRUTH_NOT_EXPR;
21526606 4828
a723baf1
MM
4829 case CPP_COMPL:
4830 return BIT_NOT_EXPR;
4831
4832 default:
4833 return ERROR_MARK;
4834 }
4835}
4836
4837/* Parse a new-expression.
4838
ca099ac8 4839 new-expression:
a723baf1
MM
4840 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4841 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4842
4843 Returns a representation of the expression. */
4844
4845static tree
94edc4ab 4846cp_parser_new_expression (cp_parser* parser)
a723baf1
MM
4847{
4848 bool global_scope_p;
4849 tree placement;
4850 tree type;
4851 tree initializer;
058b15c1 4852 tree nelts;
a723baf1
MM
4853
4854 /* Look for the optional `::' operator. */
21526606 4855 global_scope_p
a723baf1
MM
4856 = (cp_parser_global_scope_opt (parser,
4857 /*current_scope_valid_p=*/false)
4858 != NULL_TREE);
4859 /* Look for the `new' operator. */
4860 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4861 /* There's no easy way to tell a new-placement from the
4862 `( type-id )' construct. */
4863 cp_parser_parse_tentatively (parser);
4864 /* Look for a new-placement. */
4865 placement = cp_parser_new_placement (parser);
4866 /* If that didn't work out, there's no new-placement. */
4867 if (!cp_parser_parse_definitely (parser))
4868 placement = NULL_TREE;
4869
4870 /* If the next token is a `(', then we have a parenthesized
4871 type-id. */
4872 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4873 {
4874 /* Consume the `('. */
4875 cp_lexer_consume_token (parser->lexer);
4876 /* Parse the type-id. */
4877 type = cp_parser_type_id (parser);
4878 /* Look for the closing `)'. */
4879 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
98ca843c 4880 /* There should not be a direct-new-declarator in this production,
063e900f
GB
4881 but GCC used to allowed this, so we check and emit a sensible error
4882 message for this case. */
4883 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
0da99d4e
GB
4884 {
4885 error ("array bound forbidden after parenthesized type-id");
4886 inform ("try removing the parentheses around the type-id");
063e900f
GB
4887 cp_parser_direct_new_declarator (parser);
4888 }
17a27b4f 4889 nelts = NULL_TREE;
a723baf1
MM
4890 }
4891 /* Otherwise, there must be a new-type-id. */
4892 else
058b15c1 4893 type = cp_parser_new_type_id (parser, &nelts);
a723baf1
MM
4894
4895 /* If the next token is a `(', then we have a new-initializer. */
4896 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4897 initializer = cp_parser_new_initializer (parser);
4898 else
4899 initializer = NULL_TREE;
4900
625cbf93
MM
4901 /* A new-expression may not appear in an integral constant
4902 expression. */
4903 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4904 return error_mark_node;
4905
a723baf1 4906 /* Create a representation of the new-expression. */
058b15c1 4907 return build_new (placement, type, nelts, initializer, global_scope_p);
a723baf1
MM
4908}
4909
4910/* Parse a new-placement.
4911
4912 new-placement:
4913 ( expression-list )
4914
4915 Returns the same representation as for an expression-list. */
4916
4917static tree
94edc4ab 4918cp_parser_new_placement (cp_parser* parser)
a723baf1
MM
4919{
4920 tree expression_list;
4921
a723baf1 4922 /* Parse the expression-list. */
21526606 4923 expression_list = (cp_parser_parenthesized_expression_list
93678513
MM
4924 (parser, false, /*cast_p=*/false,
4925 /*non_constant_p=*/NULL));
a723baf1
MM
4926
4927 return expression_list;
4928}
4929
4930/* Parse a new-type-id.
4931
4932 new-type-id:
4933 type-specifier-seq new-declarator [opt]
4934
058b15c1
MM
4935 Returns the TYPE allocated. If the new-type-id indicates an array
4936 type, *NELTS is set to the number of elements in the last array
4937 bound; the TYPE will not include the last array bound. */
a723baf1
MM
4938
4939static tree
058b15c1 4940cp_parser_new_type_id (cp_parser* parser, tree *nelts)
a723baf1 4941{
62d1db17 4942 cp_decl_specifier_seq type_specifier_seq;
058b15c1
MM
4943 cp_declarator *new_declarator;
4944 cp_declarator *declarator;
4945 cp_declarator *outer_declarator;
a723baf1 4946 const char *saved_message;
058b15c1 4947 tree type;
a723baf1
MM
4948
4949 /* The type-specifier sequence must not contain type definitions.
4950 (It cannot contain declarations of new types either, but if they
4951 are not definitions we will catch that because they are not
4952 complete.) */
4953 saved_message = parser->type_definition_forbidden_message;
4954 parser->type_definition_forbidden_message
4955 = "types may not be defined in a new-type-id";
4956 /* Parse the type-specifier-seq. */
62d1db17 4957 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
a723baf1
MM
4958 /* Restore the old message. */
4959 parser->type_definition_forbidden_message = saved_message;
4960 /* Parse the new-declarator. */
058b15c1
MM
4961 new_declarator = cp_parser_new_declarator_opt (parser);
4962
4963 /* Determine the number of elements in the last array dimension, if
4964 any. */
4965 *nelts = NULL_TREE;
4966 /* Skip down to the last array dimension. */
4967 declarator = new_declarator;
4968 outer_declarator = NULL;
4969 while (declarator && (declarator->kind == cdk_pointer
4970 || declarator->kind == cdk_ptrmem))
4971 {
4972 outer_declarator = declarator;
4973 declarator = declarator->declarator;
4974 }
98ca843c 4975 while (declarator
058b15c1
MM
4976 && declarator->kind == cdk_array
4977 && declarator->declarator
4978 && declarator->declarator->kind == cdk_array)
4979 {
4980 outer_declarator = declarator;
4981 declarator = declarator->declarator;
4982 }
98ca843c 4983
058b15c1
MM
4984 if (declarator && declarator->kind == cdk_array)
4985 {
4986 *nelts = declarator->u.array.bounds;
4987 if (*nelts == error_mark_node)
4988 *nelts = integer_one_node;
ad1063d5 4989
058b15c1
MM
4990 if (outer_declarator)
4991 outer_declarator->declarator = declarator->declarator;
4992 else
4993 new_declarator = NULL;
4994 }
a723baf1 4995
62d1db17 4996 type = groktypename (&type_specifier_seq, new_declarator);
058b15c1
MM
4997 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4998 {
4999 *nelts = array_type_nelts_top (type);
5000 type = TREE_TYPE (type);
5001 }
5002 return type;
a723baf1
MM
5003}
5004
5005/* Parse an (optional) new-declarator.
5006
5007 new-declarator:
5008 ptr-operator new-declarator [opt]
5009 direct-new-declarator
5010
058b15c1 5011 Returns the declarator. */
a723baf1 5012
058b15c1 5013static cp_declarator *
94edc4ab 5014cp_parser_new_declarator_opt (cp_parser* parser)
a723baf1
MM
5015{
5016 enum tree_code code;
5017 tree type;
3c01e5df 5018 cp_cv_quals cv_quals;
a723baf1
MM
5019
5020 /* We don't know if there's a ptr-operator next, or not. */
5021 cp_parser_parse_tentatively (parser);
5022 /* Look for a ptr-operator. */
3c01e5df 5023 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
a723baf1
MM
5024 /* If that worked, look for more new-declarators. */
5025 if (cp_parser_parse_definitely (parser))
5026 {
058b15c1 5027 cp_declarator *declarator;
a723baf1
MM
5028
5029 /* Parse another optional declarator. */
5030 declarator = cp_parser_new_declarator_opt (parser);
5031
5032 /* Create the representation of the declarator. */
058b15c1 5033 if (type)
3c01e5df 5034 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
058b15c1 5035 else if (code == INDIRECT_REF)
3c01e5df 5036 declarator = make_pointer_declarator (cv_quals, declarator);
a723baf1 5037 else
3c01e5df 5038 declarator = make_reference_declarator (cv_quals, declarator);
a723baf1 5039
a723baf1
MM
5040 return declarator;
5041 }
5042
5043 /* If the next token is a `[', there is a direct-new-declarator. */
5044 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5045 return cp_parser_direct_new_declarator (parser);
5046
058b15c1 5047 return NULL;
a723baf1
MM
5048}
5049
5050/* Parse a direct-new-declarator.
5051
5052 direct-new-declarator:
5053 [ expression ]
21526606 5054 direct-new-declarator [constant-expression]
a723baf1 5055
058b15c1 5056 */
a723baf1 5057
058b15c1 5058static cp_declarator *
94edc4ab 5059cp_parser_direct_new_declarator (cp_parser* parser)
a723baf1 5060{
058b15c1 5061 cp_declarator *declarator = NULL;
a723baf1
MM
5062
5063 while (true)
5064 {
5065 tree expression;
5066
5067 /* Look for the opening `['. */
5068 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5069 /* The first expression is not required to be constant. */
5070 if (!declarator)
5071 {
93678513 5072 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
5073 /* The standard requires that the expression have integral
5074 type. DR 74 adds enumeration types. We believe that the
5075 real intent is that these expressions be handled like the
5076 expression in a `switch' condition, which also allows
5077 classes with a single conversion to integral or
5078 enumeration type. */
5079 if (!processing_template_decl)
5080 {
21526606 5081 expression
a723baf1
MM
5082 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5083 expression,
b746c5dc 5084 /*complain=*/true);
a723baf1
MM
5085 if (!expression)
5086 {
2a13a625
GDR
5087 error ("expression in new-declarator must have integral "
5088 "or enumeration type");
a723baf1
MM
5089 expression = error_mark_node;
5090 }
5091 }
5092 }
5093 /* But all the other expressions must be. */
5094 else
21526606
EC
5095 expression
5096 = cp_parser_constant_expression (parser,
14d22dd6
MM
5097 /*allow_non_constant=*/false,
5098 NULL);
a723baf1
MM
5099 /* Look for the closing `]'. */
5100 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5101
5102 /* Add this bound to the declarator. */
058b15c1 5103 declarator = make_array_declarator (declarator, expression);
a723baf1
MM
5104
5105 /* If the next token is not a `[', then there are no more
5106 bounds. */
5107 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5108 break;
5109 }
5110
5111 return declarator;
5112}
5113
5114/* Parse a new-initializer.
5115
5116 new-initializer:
5117 ( expression-list [opt] )
5118
34cd5ae7 5119 Returns a representation of the expression-list. If there is no
a723baf1
MM
5120 expression-list, VOID_ZERO_NODE is returned. */
5121
5122static tree
94edc4ab 5123cp_parser_new_initializer (cp_parser* parser)
a723baf1
MM
5124{
5125 tree expression_list;
5126
21526606 5127 expression_list = (cp_parser_parenthesized_expression_list
93678513
MM
5128 (parser, false, /*cast_p=*/false,
5129 /*non_constant_p=*/NULL));
7efa3e22 5130 if (!expression_list)
a723baf1 5131 expression_list = void_zero_node;
a723baf1
MM
5132
5133 return expression_list;
5134}
5135
5136/* Parse a delete-expression.
5137
5138 delete-expression:
5139 :: [opt] delete cast-expression
5140 :: [opt] delete [ ] cast-expression
5141
5142 Returns a representation of the expression. */
5143
5144static tree
94edc4ab 5145cp_parser_delete_expression (cp_parser* parser)
a723baf1
MM
5146{
5147 bool global_scope_p;
5148 bool array_p;
5149 tree expression;
5150
5151 /* Look for the optional `::' operator. */
5152 global_scope_p
5153 = (cp_parser_global_scope_opt (parser,
5154 /*current_scope_valid_p=*/false)
5155 != NULL_TREE);
5156 /* Look for the `delete' keyword. */
5157 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5158 /* See if the array syntax is in use. */
5159 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5160 {
5161 /* Consume the `[' token. */
5162 cp_lexer_consume_token (parser->lexer);
5163 /* Look for the `]' token. */
5164 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5165 /* Remember that this is the `[]' construct. */
5166 array_p = true;
5167 }
5168 else
5169 array_p = false;
5170
5171 /* Parse the cast-expression. */
d6b4ea85 5172 expression = cp_parser_simple_cast_expression (parser);
a723baf1 5173
625cbf93
MM
5174 /* A delete-expression may not appear in an integral constant
5175 expression. */
5176 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5177 return error_mark_node;
5178
a723baf1
MM
5179 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5180}
5181
5182/* Parse a cast-expression.
5183
5184 cast-expression:
5185 unary-expression
5186 ( type-id ) cast-expression
5187
93678513
MM
5188 ADDRESS_P is true iff the unary-expression is appearing as the
5189 operand of the `&' operator. CAST_P is true if this expression is
5190 the target of a cast.
5191
a723baf1
MM
5192 Returns a representation of the expression. */
5193
5194static tree
93678513 5195cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
a723baf1
MM
5196{
5197 /* If it's a `(', then we might be looking at a cast. */
5198 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5199 {
5200 tree type = NULL_TREE;
5201 tree expr = NULL_TREE;
5202 bool compound_literal_p;
5203 const char *saved_message;
5204
5205 /* There's no way to know yet whether or not this is a cast.
5206 For example, `(int (3))' is a unary-expression, while `(int)
5207 3' is a cast. So, we resort to parsing tentatively. */
5208 cp_parser_parse_tentatively (parser);
5209 /* Types may not be defined in a cast. */
5210 saved_message = parser->type_definition_forbidden_message;
5211 parser->type_definition_forbidden_message
5212 = "types may not be defined in casts";
5213 /* Consume the `('. */
5214 cp_lexer_consume_token (parser->lexer);
5215 /* A very tricky bit is that `(struct S) { 3 }' is a
5216 compound-literal (which we permit in C++ as an extension).
5217 But, that construct is not a cast-expression -- it is a
5218 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5219 is legal; if the compound-literal were a cast-expression,
5220 you'd need an extra set of parentheses.) But, if we parse
5221 the type-id, and it happens to be a class-specifier, then we
5222 will commit to the parse at that point, because we cannot
5223 undo the action that is done when creating a new class. So,
21526606 5224 then we cannot back up and do a postfix-expression.
a723baf1
MM
5225
5226 Therefore, we scan ahead to the closing `)', and check to see
5227 if the token after the `)' is a `{'. If so, we are not
21526606 5228 looking at a cast-expression.
a723baf1
MM
5229
5230 Save tokens so that we can put them back. */
5231 cp_lexer_save_tokens (parser->lexer);
5232 /* Skip tokens until the next token is a closing parenthesis.
5233 If we find the closing `)', and the next token is a `{', then
5234 we are looking at a compound-literal. */
21526606 5235 compound_literal_p
a668c6ad
MM
5236 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5237 /*consume_paren=*/true)
a723baf1
MM
5238 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5239 /* Roll back the tokens we skipped. */
5240 cp_lexer_rollback_tokens (parser->lexer);
5241 /* If we were looking at a compound-literal, simulate an error
5242 so that the call to cp_parser_parse_definitely below will
5243 fail. */
5244 if (compound_literal_p)
5245 cp_parser_simulate_error (parser);
5246 else
5247 {
4f8163b1
MM
5248 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5249 parser->in_type_id_in_expr_p = true;
a723baf1
MM
5250 /* Look for the type-id. */
5251 type = cp_parser_type_id (parser);
5252 /* Look for the closing `)'. */
5253 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4f8163b1 5254 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
5255 }
5256
5257 /* Restore the saved message. */
5258 parser->type_definition_forbidden_message = saved_message;
5259
bbaab916
NS
5260 /* If ok so far, parse the dependent expression. We cannot be
5261 sure it is a cast. Consider `(T ())'. It is a parenthesized
5262 ctor of T, but looks like a cast to function returning T
5263 without a dependent expression. */
5264 if (!cp_parser_error_occurred (parser))
93678513
MM
5265 expr = cp_parser_cast_expression (parser,
5266 /*address_p=*/false,
5267 /*cast_p=*/true);
bbaab916 5268
a723baf1
MM
5269 if (cp_parser_parse_definitely (parser))
5270 {
a723baf1 5271 /* Warn about old-style casts, if so requested. */
21526606
EC
5272 if (warn_old_style_cast
5273 && !in_system_header
5274 && !VOID_TYPE_P (type)
a723baf1
MM
5275 && current_lang_name != lang_name_c)
5276 warning ("use of old-style cast");
14d22dd6
MM
5277
5278 /* Only type conversions to integral or enumeration types
5279 can be used in constant-expressions. */
67c03833 5280 if (parser->integral_constant_expression_p
14d22dd6 5281 && !dependent_type_p (type)
625cbf93 5282 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
98ca843c 5283 && (cp_parser_non_integral_constant_expression
625cbf93
MM
5284 (parser,
5285 "a cast to a type other than an integral or "
5286 "enumeration type")))
5287 return error_mark_node;
5288
a723baf1
MM
5289 /* Perform the cast. */
5290 expr = build_c_cast (type, expr);
bbaab916 5291 return expr;
a723baf1 5292 }
a723baf1
MM
5293 }
5294
5295 /* If we get here, then it's not a cast, so it must be a
5296 unary-expression. */
93678513 5297 return cp_parser_unary_expression (parser, address_p, cast_p);
a723baf1
MM
5298}
5299
b8b94c5b 5300/* Parse a binary expression of the general form:
a723baf1
MM
5301
5302 pm-expression:
5303 cast-expression
5304 pm-expression .* cast-expression
5305 pm-expression ->* cast-expression
5306
77077b39 5307 multiplicative-expression:
a723baf1
MM
5308 pm-expression
5309 multiplicative-expression * pm-expression
5310 multiplicative-expression / pm-expression
5311 multiplicative-expression % pm-expression
5312
a723baf1
MM
5313 additive-expression:
5314 multiplicative-expression
5315 additive-expression + multiplicative-expression
5316 additive-expression - multiplicative-expression
5317
a723baf1
MM
5318 shift-expression:
5319 additive-expression
5320 shift-expression << additive-expression
5321 shift-expression >> additive-expression
5322
a723baf1
MM
5323 relational-expression:
5324 shift-expression
5325 relational-expression < shift-expression
5326 relational-expression > shift-expression
5327 relational-expression <= shift-expression
5328 relational-expression >= shift-expression
5329
b8b94c5b
PB
5330 GNU Extension:
5331
a723baf1
MM
5332 relational-expression:
5333 relational-expression <? shift-expression
5334 relational-expression >? shift-expression
5335
a723baf1
MM
5336 equality-expression:
5337 relational-expression
5338 equality-expression == relational-expression
5339 equality-expression != relational-expression
5340
a723baf1
MM
5341 and-expression:
5342 equality-expression
5343 and-expression & equality-expression
5344
a723baf1
MM
5345 exclusive-or-expression:
5346 and-expression
5347 exclusive-or-expression ^ and-expression
5348
b8b94c5b
PB
5349 inclusive-or-expression:
5350 exclusive-or-expression
5351 inclusive-or-expression | exclusive-or-expression
a723baf1 5352
b8b94c5b
PB
5353 logical-and-expression:
5354 inclusive-or-expression
5355 logical-and-expression && inclusive-or-expression
a723baf1 5356
b8b94c5b
PB
5357 logical-or-expression:
5358 logical-and-expression
5359 logical-or-expression || logical-and-expression
a723baf1 5360
b8b94c5b 5361 All these are implemented with a single function like:
a723baf1 5362
b8b94c5b
PB
5363 binary-expression:
5364 simple-cast-expression
5365 binary-expression <token> binary-expression
a723baf1 5366
93678513
MM
5367 CAST_P is true if this expression is the target of a cast.
5368
b8b94c5b
PB
5369 The binops_by_token map is used to get the tree codes for each <token> type.
5370 binary-expressions are associated according to a precedence table. */
a723baf1 5371
b8b94c5b
PB
5372#define TOKEN_PRECEDENCE(token) \
5373 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5374 ? PREC_NOT_OPERATOR \
5375 : binops_by_token[token->type].prec)
a723baf1
MM
5376
5377static tree
93678513 5378cp_parser_binary_expression (cp_parser* parser, bool cast_p)
a723baf1 5379{
b8b94c5b
PB
5380 cp_parser_expression_stack stack;
5381 cp_parser_expression_stack_entry *sp = &stack[0];
5382 tree lhs, rhs;
5383 cp_token *token;
5384 enum tree_code tree_type;
5385 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5386 bool overloaded_p;
a723baf1 5387
b8b94c5b 5388 /* Parse the first expression. */
93678513 5389 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
a723baf1 5390
b8b94c5b
PB
5391 for (;;)
5392 {
5393 /* Get an operator token. */
5394 token = cp_lexer_peek_token (parser->lexer);
5395 new_prec = TOKEN_PRECEDENCE (token);
5396
5397 /* Popping an entry off the stack means we completed a subexpression:
5398 - either we found a token which is not an operator (`>' where it is not
5399 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5400 will happen repeatedly;
5401 - or, we found an operator which has lower priority. This is the case
5402 where the recursive descent *ascends*, as in `3 * 4 + 5' after
03fd3f84 5403 parsing `3 * 4'. */
b8b94c5b
PB
5404 if (new_prec <= prec)
5405 {
5406 if (sp == stack)
5407 break;
5408 else
5409 goto pop;
5410 }
a723baf1 5411
b8b94c5b
PB
5412 get_rhs:
5413 tree_type = binops_by_token[token->type].tree_type;
a723baf1 5414
03fd3f84 5415 /* We used the operator token. */
b8b94c5b 5416 cp_lexer_consume_token (parser->lexer);
a723baf1 5417
b8b94c5b
PB
5418 /* Extract another operand. It may be the RHS of this expression
5419 or the LHS of a new, higher priority expression. */
5420 rhs = cp_parser_simple_cast_expression (parser);
a723baf1 5421
b8b94c5b
PB
5422 /* Get another operator token. Look up its precedence to avoid
5423 building a useless (immediately popped) stack entry for common
03fd3f84 5424 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
b8b94c5b
PB
5425 token = cp_lexer_peek_token (parser->lexer);
5426 lookahead_prec = TOKEN_PRECEDENCE (token);
5427 if (lookahead_prec > new_prec)
5428 {
5429 /* ... and prepare to parse the RHS of the new, higher priority
43c2a69a
PB
5430 expression. Since precedence levels on the stack are
5431 monotonically increasing, we do not have to care about
5432 stack overflows. */
b8b94c5b
PB
5433 sp->prec = prec;
5434 sp->tree_type = tree_type;
5435 sp->lhs = lhs;
5436 sp++;
5437 lhs = rhs;
5438 prec = new_prec;
5439 new_prec = lookahead_prec;
5440 goto get_rhs;
5441
5442 pop:
5443 /* If the stack is not empty, we have parsed into LHS the right side
5444 (`4' in the example above) of an expression we had suspended.
5445 We can use the information on the stack to recover the LHS (`3')
5446 from the stack together with the tree code (`MULT_EXPR'), and
5447 the precedence of the higher level subexpression
5448 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5449 which will be used to actually build the additive expression. */
5450 --sp;
5451 prec = sp->prec;
5452 tree_type = sp->tree_type;
5453 rhs = lhs;
5454 lhs = sp->lhs;
5455 }
a723baf1 5456
b8b94c5b
PB
5457 overloaded_p = false;
5458 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
a723baf1 5459
b8b94c5b
PB
5460 /* If the binary operator required the use of an overloaded operator,
5461 then this expression cannot be an integral constant-expression.
5462 An overloaded operator can be used even if both operands are
5463 otherwise permissible in an integral constant-expression if at
5464 least one of the operands is of enumeration type. */
a723baf1 5465
b8b94c5b
PB
5466 if (overloaded_p
5467 && (cp_parser_non_integral_constant_expression
5468 (parser, "calls to overloaded operators")))
5469 return error_mark_node;
5470 }
a723baf1 5471
b8b94c5b 5472 return lhs;
a723baf1
MM
5473}
5474
b8b94c5b 5475
a723baf1
MM
5476/* Parse the `? expression : assignment-expression' part of a
5477 conditional-expression. The LOGICAL_OR_EXPR is the
5478 logical-or-expression that started the conditional-expression.
5479 Returns a representation of the entire conditional-expression.
5480
39703eb9 5481 This routine is used by cp_parser_assignment_expression.
a723baf1
MM
5482
5483 ? expression : assignment-expression
21526606 5484
a723baf1 5485 GNU Extensions:
21526606 5486
a723baf1
MM
5487 ? : assignment-expression */
5488
5489static tree
94edc4ab 5490cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
a723baf1
MM
5491{
5492 tree expr;
5493 tree assignment_expr;
5494
5495 /* Consume the `?' token. */
5496 cp_lexer_consume_token (parser->lexer);
5497 if (cp_parser_allow_gnu_extensions_p (parser)
5498 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5499 /* Implicit true clause. */
5500 expr = NULL_TREE;
5501 else
5502 /* Parse the expression. */
93678513 5503 expr = cp_parser_expression (parser, /*cast_p=*/false);
21526606 5504
a723baf1
MM
5505 /* The next token should be a `:'. */
5506 cp_parser_require (parser, CPP_COLON, "`:'");
5507 /* Parse the assignment-expression. */
93678513 5508 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
a723baf1
MM
5509
5510 /* Build the conditional-expression. */
5511 return build_x_conditional_expr (logical_or_expr,
5512 expr,
5513 assignment_expr);
5514}
5515
5516/* Parse an assignment-expression.
5517
5518 assignment-expression:
5519 conditional-expression
5520 logical-or-expression assignment-operator assignment_expression
5521 throw-expression
5522
93678513
MM
5523 CAST_P is true if this expression is the target of a cast.
5524
a723baf1
MM
5525 Returns a representation for the expression. */
5526
5527static tree
93678513 5528cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
a723baf1
MM
5529{
5530 tree expr;
5531
5532 /* If the next token is the `throw' keyword, then we're looking at
5533 a throw-expression. */
5534 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5535 expr = cp_parser_throw_expression (parser);
5536 /* Otherwise, it must be that we are looking at a
5537 logical-or-expression. */
5538 else
5539 {
b8b94c5b 5540 /* Parse the binary expressions (logical-or-expression). */
93678513 5541 expr = cp_parser_binary_expression (parser, cast_p);
a723baf1
MM
5542 /* If the next token is a `?' then we're actually looking at a
5543 conditional-expression. */
5544 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5545 return cp_parser_question_colon_clause (parser, expr);
21526606 5546 else
a723baf1
MM
5547 {
5548 enum tree_code assignment_operator;
5549
5550 /* If it's an assignment-operator, we're using the second
5551 production. */
21526606 5552 assignment_operator
a723baf1
MM
5553 = cp_parser_assignment_operator_opt (parser);
5554 if (assignment_operator != ERROR_MARK)
5555 {
5556 tree rhs;
5557
5558 /* Parse the right-hand side of the assignment. */
93678513 5559 rhs = cp_parser_assignment_expression (parser, cast_p);
14d22dd6
MM
5560 /* An assignment may not appear in a
5561 constant-expression. */
625cbf93
MM
5562 if (cp_parser_non_integral_constant_expression (parser,
5563 "an assignment"))
5564 return error_mark_node;
34cd5ae7 5565 /* Build the assignment expression. */
21526606
EC
5566 expr = build_x_modify_expr (expr,
5567 assignment_operator,
a723baf1
MM
5568 rhs);
5569 }
5570 }
5571 }
5572
5573 return expr;
5574}
5575
5576/* Parse an (optional) assignment-operator.
5577
21526606
EC
5578 assignment-operator: one of
5579 = *= /= %= += -= >>= <<= &= ^= |=
a723baf1
MM
5580
5581 GNU Extension:
21526606 5582
a723baf1
MM
5583 assignment-operator: one of
5584 <?= >?=
5585
5586 If the next token is an assignment operator, the corresponding tree
5587 code is returned, and the token is consumed. For example, for
5588 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5589 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5590 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5591 operator, ERROR_MARK is returned. */
5592
5593static enum tree_code
94edc4ab 5594cp_parser_assignment_operator_opt (cp_parser* parser)
a723baf1
MM
5595{
5596 enum tree_code op;
5597 cp_token *token;
5598
5599 /* Peek at the next toen. */
5600 token = cp_lexer_peek_token (parser->lexer);
5601
5602 switch (token->type)
5603 {
5604 case CPP_EQ:
5605 op = NOP_EXPR;
5606 break;
5607
5608 case CPP_MULT_EQ:
5609 op = MULT_EXPR;
5610 break;
5611
5612 case CPP_DIV_EQ:
5613 op = TRUNC_DIV_EXPR;
5614 break;
5615
5616 case CPP_MOD_EQ:
5617 op = TRUNC_MOD_EXPR;
5618 break;
5619
5620 case CPP_PLUS_EQ:
5621 op = PLUS_EXPR;
5622 break;
5623
5624 case CPP_MINUS_EQ:
5625 op = MINUS_EXPR;
5626 break;
5627
5628 case CPP_RSHIFT_EQ:
5629 op = RSHIFT_EXPR;
5630 break;
5631
5632 case CPP_LSHIFT_EQ:
5633 op = LSHIFT_EXPR;
5634 break;
5635
5636 case CPP_AND_EQ:
5637 op = BIT_AND_EXPR;
5638 break;
5639
5640 case CPP_XOR_EQ:
5641 op = BIT_XOR_EXPR;
5642 break;
5643
5644 case CPP_OR_EQ:
5645 op = BIT_IOR_EXPR;
5646 break;
5647
5648 case CPP_MIN_EQ:
5649 op = MIN_EXPR;
5650 break;
5651
5652 case CPP_MAX_EQ:
5653 op = MAX_EXPR;
5654 break;
5655
21526606 5656 default:
a723baf1
MM
5657 /* Nothing else is an assignment operator. */
5658 op = ERROR_MARK;
5659 }
5660
5661 /* If it was an assignment operator, consume it. */
5662 if (op != ERROR_MARK)
5663 cp_lexer_consume_token (parser->lexer);
5664
5665 return op;
5666}
5667
5668/* Parse an expression.
5669
5670 expression:
5671 assignment-expression
5672 expression , assignment-expression
5673
93678513
MM
5674 CAST_P is true if this expression is the target of a cast.
5675
a723baf1
MM
5676 Returns a representation of the expression. */
5677
5678static tree
93678513 5679cp_parser_expression (cp_parser* parser, bool cast_p)
a723baf1
MM
5680{
5681 tree expression = NULL_TREE;
a723baf1
MM
5682
5683 while (true)
5684 {
5685 tree assignment_expression;
5686
5687 /* Parse the next assignment-expression. */
21526606 5688 assignment_expression
93678513 5689 = cp_parser_assignment_expression (parser, cast_p);
a723baf1
MM
5690 /* If this is the first assignment-expression, we can just
5691 save it away. */
5692 if (!expression)
5693 expression = assignment_expression;
a723baf1 5694 else
d17811fd
MM
5695 expression = build_x_compound_expr (expression,
5696 assignment_expression);
a723baf1
MM
5697 /* If the next token is not a comma, then we are done with the
5698 expression. */
5699 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5700 break;
5701 /* Consume the `,'. */
5702 cp_lexer_consume_token (parser->lexer);
14d22dd6 5703 /* A comma operator cannot appear in a constant-expression. */
625cbf93
MM
5704 if (cp_parser_non_integral_constant_expression (parser,
5705 "a comma operator"))
5706 expression = error_mark_node;
14d22dd6 5707 }
a723baf1
MM
5708
5709 return expression;
5710}
5711
21526606 5712/* Parse a constant-expression.
a723baf1
MM
5713
5714 constant-expression:
21526606 5715 conditional-expression
14d22dd6
MM
5716
5717 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
d17811fd
MM
5718 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5719 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5720 is false, NON_CONSTANT_P should be NULL. */
a723baf1
MM
5721
5722static tree
21526606 5723cp_parser_constant_expression (cp_parser* parser,
14d22dd6
MM
5724 bool allow_non_constant_p,
5725 bool *non_constant_p)
a723baf1 5726{
67c03833
JM
5727 bool saved_integral_constant_expression_p;
5728 bool saved_allow_non_integral_constant_expression_p;
5729 bool saved_non_integral_constant_expression_p;
a723baf1
MM
5730 tree expression;
5731
5732 /* It might seem that we could simply parse the
5733 conditional-expression, and then check to see if it were
5734 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5735 one that the compiler can figure out is constant, possibly after
5736 doing some simplifications or optimizations. The standard has a
5737 precise definition of constant-expression, and we must honor
5738 that, even though it is somewhat more restrictive.
5739
5740 For example:
5741
5742 int i[(2, 3)];
5743
5744 is not a legal declaration, because `(2, 3)' is not a
5745 constant-expression. The `,' operator is forbidden in a
5746 constant-expression. However, GCC's constant-folding machinery
5747 will fold this operation to an INTEGER_CST for `3'. */
5748
14d22dd6 5749 /* Save the old settings. */
67c03833 5750 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
21526606 5751 saved_allow_non_integral_constant_expression_p
67c03833
JM
5752 = parser->allow_non_integral_constant_expression_p;
5753 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
a723baf1 5754 /* We are now parsing a constant-expression. */
67c03833
JM
5755 parser->integral_constant_expression_p = true;
5756 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5757 parser->non_integral_constant_expression_p = false;
39703eb9
MM
5758 /* Although the grammar says "conditional-expression", we parse an
5759 "assignment-expression", which also permits "throw-expression"
5760 and the use of assignment operators. In the case that
5761 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5762 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5763 actually essential that we look for an assignment-expression.
5764 For example, cp_parser_initializer_clauses uses this function to
5765 determine whether a particular assignment-expression is in fact
5766 constant. */
93678513 5767 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14d22dd6 5768 /* Restore the old settings. */
93678513
MM
5769 parser->integral_constant_expression_p
5770 = saved_integral_constant_expression_p;
21526606 5771 parser->allow_non_integral_constant_expression_p
67c03833 5772 = saved_allow_non_integral_constant_expression_p;
14d22dd6 5773 if (allow_non_constant_p)
67c03833 5774 *non_constant_p = parser->non_integral_constant_expression_p;
93678513
MM
5775 else if (parser->non_integral_constant_expression_p)
5776 expression = error_mark_node;
5777 parser->non_integral_constant_expression_p
5778 = saved_non_integral_constant_expression_p;
a723baf1
MM
5779
5780 return expression;
5781}
5782
7a3ea201
RH
5783/* Parse __builtin_offsetof.
5784
5785 offsetof-expression:
5786 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5787
5788 offsetof-member-designator:
5789 id-expression
5790 | offsetof-member-designator "." id-expression
5791 | offsetof-member-designator "[" expression "]"
5792*/
5793
5794static tree
5795cp_parser_builtin_offsetof (cp_parser *parser)
5796{
5797 int save_ice_p, save_non_ice_p;
5798 tree type, expr;
5799 cp_id_kind dummy;
5800
5801 /* We're about to accept non-integral-constant things, but will
5802 definitely yield an integral constant expression. Save and
5803 restore these values around our local parsing. */
5804 save_ice_p = parser->integral_constant_expression_p;
5805 save_non_ice_p = parser->non_integral_constant_expression_p;
5806
5807 /* Consume the "__builtin_offsetof" token. */
5808 cp_lexer_consume_token (parser->lexer);
5809 /* Consume the opening `('. */
5810 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5811 /* Parse the type-id. */
5812 type = cp_parser_type_id (parser);
5813 /* Look for the `,'. */
5814 cp_parser_require (parser, CPP_COMMA, "`,'");
5815
5816 /* Build the (type *)null that begins the traditional offsetof macro. */
5817 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5818
5819 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5820 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5821 true, &dummy);
5822 while (true)
5823 {
5824 cp_token *token = cp_lexer_peek_token (parser->lexer);
5825 switch (token->type)
5826 {
5827 case CPP_OPEN_SQUARE:
5828 /* offsetof-member-designator "[" expression "]" */
5829 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5830 break;
5831
5832 case CPP_DOT:
5833 /* offsetof-member-designator "." identifier */
5834 cp_lexer_consume_token (parser->lexer);
5835 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5836 true, &dummy);
5837 break;
5838
5839 case CPP_CLOSE_PAREN:
5840 /* Consume the ")" token. */
5841 cp_lexer_consume_token (parser->lexer);
5842 goto success;
5843
5844 default:
5845 /* Error. We know the following require will fail, but
5846 that gives the proper error message. */
5847 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5848 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5849 expr = error_mark_node;
5850 goto failure;
5851 }
5852 }
5853
5854 success:
42c244d8
RH
5855 /* If we're processing a template, we can't finish the semantics yet.
5856 Otherwise we can fold the entire expression now. */
5857 if (processing_template_decl)
5858 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5859 else
5860 expr = fold_offsetof (expr);
7a3ea201
RH
5861
5862 failure:
5863 parser->integral_constant_expression_p = save_ice_p;
5864 parser->non_integral_constant_expression_p = save_non_ice_p;
5865
5866 return expr;
5867}
5868
a723baf1
MM
5869/* Statements [gram.stmt.stmt] */
5870
21526606 5871/* Parse a statement.
a723baf1
MM
5872
5873 statement:
5874 labeled-statement
5875 expression-statement
5876 compound-statement
5877 selection-statement
5878 iteration-statement
5879 jump-statement
5880 declaration-statement
5881 try-block */
5882
5883static void
325c3691 5884cp_parser_statement (cp_parser* parser, tree in_statement_expr)
a723baf1
MM
5885{
5886 tree statement;
5887 cp_token *token;
93409b8c 5888 location_t statement_location;
a723baf1
MM
5889
5890 /* There is no statement yet. */
5891 statement = NULL_TREE;
5892 /* Peek at the next token. */
5893 token = cp_lexer_peek_token (parser->lexer);
6de9cd9a 5894 /* Remember the location of the first token in the statement. */
93409b8c 5895 statement_location = token->location;
a723baf1
MM
5896 /* If this is a keyword, then that will often determine what kind of
5897 statement we have. */
5898 if (token->type == CPP_KEYWORD)
5899 {
5900 enum rid keyword = token->keyword;
5901
5902 switch (keyword)
5903 {
5904 case RID_CASE:
5905 case RID_DEFAULT:
a5bcc582 5906 statement = cp_parser_labeled_statement (parser,
325c3691 5907 in_statement_expr);
a723baf1
MM
5908 break;
5909
5910 case RID_IF:
5911 case RID_SWITCH:
5912 statement = cp_parser_selection_statement (parser);
5913 break;
5914
5915 case RID_WHILE:
5916 case RID_DO:
5917 case RID_FOR:
5918 statement = cp_parser_iteration_statement (parser);
5919 break;
5920
5921 case RID_BREAK:
5922 case RID_CONTINUE:
5923 case RID_RETURN:
5924 case RID_GOTO:
5925 statement = cp_parser_jump_statement (parser);
5926 break;
5927
5928 case RID_TRY:
5929 statement = cp_parser_try_block (parser);
5930 break;
5931
5932 default:
5933 /* It might be a keyword like `int' that can start a
5934 declaration-statement. */
5935 break;
5936 }
5937 }
5938 else if (token->type == CPP_NAME)
5939 {
5940 /* If the next token is a `:', then we are looking at a
5941 labeled-statement. */
5942 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5943 if (token->type == CPP_COLON)
325c3691 5944 statement = cp_parser_labeled_statement (parser, in_statement_expr);
a723baf1
MM
5945 }
5946 /* Anything that starts with a `{' must be a compound-statement. */
5947 else if (token->type == CPP_OPEN_BRACE)
325c3691 5948 statement = cp_parser_compound_statement (parser, NULL, false);
36952dea
ZW
5949 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5950 a statement all its own. */
5951 else if (token->type == CPP_PRAGMA)
5952 {
5953 cp_lexer_handle_pragma (parser->lexer);
5954 return;
5955 }
a723baf1
MM
5956
5957 /* Everything else must be a declaration-statement or an
21526606 5958 expression-statement. Try for the declaration-statement
a723baf1
MM
5959 first, unless we are looking at a `;', in which case we know that
5960 we have an expression-statement. */
5961 if (!statement)
5962 {
5963 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5964 {
5965 cp_parser_parse_tentatively (parser);
5966 /* Try to parse the declaration-statement. */
5967 cp_parser_declaration_statement (parser);
5968 /* If that worked, we're done. */
5969 if (cp_parser_parse_definitely (parser))
5970 return;
5971 }
5972 /* Look for an expression-statement instead. */
325c3691 5973 statement = cp_parser_expression_statement (parser, in_statement_expr);
a723baf1
MM
5974 }
5975
5976 /* Set the line number for the statement. */
009ed910 5977 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
93409b8c 5978 SET_EXPR_LOCATION (statement, statement_location);
a723baf1
MM
5979}
5980
5981/* Parse a labeled-statement.
5982
5983 labeled-statement:
5984 identifier : statement
5985 case constant-expression : statement
98ce043b
MM
5986 default : statement
5987
5988 GNU Extension:
21526606 5989
98ce043b
MM
5990 labeled-statement:
5991 case constant-expression ... constant-expression : statement
a723baf1 5992
8c161995
RH
5993 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5994 For an ordinary label, returns a LABEL_EXPR. */
a723baf1
MM
5995
5996static tree
325c3691 5997cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
a723baf1
MM
5998{
5999 cp_token *token;
0e59b3fb 6000 tree statement = error_mark_node;
a723baf1
MM
6001
6002 /* The next token should be an identifier. */
6003 token = cp_lexer_peek_token (parser->lexer);
6004 if (token->type != CPP_NAME
6005 && token->type != CPP_KEYWORD)
6006 {
6007 cp_parser_error (parser, "expected labeled-statement");
6008 return error_mark_node;
6009 }
6010
6011 switch (token->keyword)
6012 {
6013 case RID_CASE:
6014 {
98ce043b
MM
6015 tree expr, expr_hi;
6016 cp_token *ellipsis;
a723baf1
MM
6017
6018 /* Consume the `case' token. */
6019 cp_lexer_consume_token (parser->lexer);
6020 /* Parse the constant-expression. */
21526606 6021 expr = cp_parser_constant_expression (parser,
d17811fd 6022 /*allow_non_constant_p=*/false,
14d22dd6 6023 NULL);
98ce043b
MM
6024
6025 ellipsis = cp_lexer_peek_token (parser->lexer);
6026 if (ellipsis->type == CPP_ELLIPSIS)
6027 {
6028 /* Consume the `...' token. */
6029 cp_lexer_consume_token (parser->lexer);
6030 expr_hi =
6031 cp_parser_constant_expression (parser,
6032 /*allow_non_constant_p=*/false,
6033 NULL);
6034 /* We don't need to emit warnings here, as the common code
6035 will do this for us. */
6036 }
6037 else
6038 expr_hi = NULL_TREE;
6039
0e59b3fb 6040 if (!parser->in_switch_statement_p)
2a13a625 6041 error ("case label %qE not within a switch statement", expr);
0e59b3fb 6042 else
98ce043b 6043 statement = finish_case_label (expr, expr_hi);
a723baf1
MM
6044 }
6045 break;
6046
6047 case RID_DEFAULT:
6048 /* Consume the `default' token. */
6049 cp_lexer_consume_token (parser->lexer);
0e59b3fb
MM
6050 if (!parser->in_switch_statement_p)
6051 error ("case label not within a switch statement");
6052 else
6053 statement = finish_case_label (NULL_TREE, NULL_TREE);
a723baf1
MM
6054 break;
6055
6056 default:
6057 /* Anything else must be an ordinary label. */
6058 statement = finish_label_stmt (cp_parser_identifier (parser));
6059 break;
6060 }
6061
6062 /* Require the `:' token. */
6063 cp_parser_require (parser, CPP_COLON, "`:'");
6064 /* Parse the labeled statement. */
325c3691 6065 cp_parser_statement (parser, in_statement_expr);
a723baf1
MM
6066
6067 /* Return the label, in the case of a `case' or `default' label. */
6068 return statement;
6069}
6070
6071/* Parse an expression-statement.
6072
6073 expression-statement:
6074 expression [opt] ;
6075
6076 Returns the new EXPR_STMT -- or NULL_TREE if the expression
a5bcc582
NS
6077 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6078 indicates whether this expression-statement is part of an
6079 expression statement. */
a723baf1
MM
6080
6081static tree
325c3691 6082cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
a723baf1 6083{
a5bcc582 6084 tree statement = NULL_TREE;
a723baf1 6085
a5bcc582 6086 /* If the next token is a ';', then there is no expression
04c06002 6087 statement. */
a723baf1 6088 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
93678513 6089 statement = cp_parser_expression (parser, /*cast_p=*/false);
21526606 6090
a723baf1 6091 /* Consume the final `;'. */
e0860732 6092 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1 6093
325c3691 6094 if (in_statement_expr
a5bcc582 6095 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
93678513
MM
6096 /* This is the final expression statement of a statement
6097 expression. */
6098 statement = finish_stmt_expr_expr (statement, in_statement_expr);
a5bcc582
NS
6099 else if (statement)
6100 statement = finish_expr_stmt (statement);
6101 else
6102 finish_stmt ();
21526606 6103
a723baf1
MM
6104 return statement;
6105}
6106
6107/* Parse a compound-statement.
6108
6109 compound-statement:
6110 { statement-seq [opt] }
21526606 6111
5882f0f3 6112 Returns a tree representing the statement. */
a723baf1
MM
6113
6114static tree
325c3691
RH
6115cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6116 bool in_try)
a723baf1
MM
6117{
6118 tree compound_stmt;
6119
6120 /* Consume the `{'. */
6121 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6122 return error_mark_node;
6123 /* Begin the compound-statement. */
325c3691 6124 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
a723baf1 6125 /* Parse an (optional) statement-seq. */
325c3691 6126 cp_parser_statement_seq_opt (parser, in_statement_expr);
a723baf1 6127 /* Finish the compound-statement. */
7a3397c7 6128 finish_compound_stmt (compound_stmt);
a723baf1
MM
6129 /* Consume the `}'. */
6130 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6131
6132 return compound_stmt;
6133}
6134
6135/* Parse an (optional) statement-seq.
6136
6137 statement-seq:
6138 statement
6139 statement-seq [opt] statement */
6140
6141static void
325c3691 6142cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
a723baf1
MM
6143{
6144 /* Scan statements until there aren't any more. */
6145 while (true)
6146 {
6147 /* If we're looking at a `}', then we've run out of statements. */
6148 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6149 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6150 break;
6151
6152 /* Parse the statement. */
325c3691 6153 cp_parser_statement (parser, in_statement_expr);
a723baf1
MM
6154 }
6155}
6156
6157/* Parse a selection-statement.
6158
6159 selection-statement:
6160 if ( condition ) statement
6161 if ( condition ) statement else statement
21526606 6162 switch ( condition ) statement
a723baf1
MM
6163
6164 Returns the new IF_STMT or SWITCH_STMT. */
6165
6166static tree
94edc4ab 6167cp_parser_selection_statement (cp_parser* parser)
a723baf1
MM
6168{
6169 cp_token *token;
6170 enum rid keyword;
6171
6172 /* Peek at the next token. */
6173 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6174
6175 /* See what kind of keyword it is. */
6176 keyword = token->keyword;
6177 switch (keyword)
6178 {
6179 case RID_IF:
6180 case RID_SWITCH:
6181 {
6182 tree statement;
6183 tree condition;
6184
6185 /* Look for the `('. */
6186 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6187 {
6188 cp_parser_skip_to_end_of_statement (parser);
6189 return error_mark_node;
6190 }
6191
6192 /* Begin the selection-statement. */
6193 if (keyword == RID_IF)
6194 statement = begin_if_stmt ();
6195 else
6196 statement = begin_switch_stmt ();
6197
6198 /* Parse the condition. */
6199 condition = cp_parser_condition (parser);
6200 /* Look for the `)'. */
6201 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
6202 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6203 /*consume_paren=*/true);
a723baf1
MM
6204
6205 if (keyword == RID_IF)
6206 {
a723baf1
MM
6207 /* Add the condition. */
6208 finish_if_stmt_cond (condition, statement);
6209
6210 /* Parse the then-clause. */
325c3691 6211 cp_parser_implicitly_scoped_statement (parser);
a723baf1
MM
6212 finish_then_clause (statement);
6213
6214 /* If the next token is `else', parse the else-clause. */
6215 if (cp_lexer_next_token_is_keyword (parser->lexer,
6216 RID_ELSE))
6217 {
a723baf1
MM
6218 /* Consume the `else' keyword. */
6219 cp_lexer_consume_token (parser->lexer);
325c3691 6220 begin_else_clause (statement);
a723baf1 6221 /* Parse the else-clause. */
325c3691 6222 cp_parser_implicitly_scoped_statement (parser);
a723baf1
MM
6223 finish_else_clause (statement);
6224 }
6225
6226 /* Now we're all done with the if-statement. */
325c3691 6227 finish_if_stmt (statement);
a723baf1
MM
6228 }
6229 else
6230 {
0e59b3fb 6231 bool in_switch_statement_p;
a723baf1
MM
6232
6233 /* Add the condition. */
6234 finish_switch_cond (condition, statement);
6235
6236 /* Parse the body of the switch-statement. */
0e59b3fb
MM
6237 in_switch_statement_p = parser->in_switch_statement_p;
6238 parser->in_switch_statement_p = true;
325c3691 6239 cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 6240 parser->in_switch_statement_p = in_switch_statement_p;
a723baf1
MM
6241
6242 /* Now we're all done with the switch-statement. */
6243 finish_switch_stmt (statement);
6244 }
6245
6246 return statement;
6247 }
6248 break;
6249
6250 default:
6251 cp_parser_error (parser, "expected selection-statement");
6252 return error_mark_node;
6253 }
6254}
6255
21526606 6256/* Parse a condition.
a723baf1
MM
6257
6258 condition:
6259 expression
21526606 6260 type-specifier-seq declarator = assignment-expression
a723baf1
MM
6261
6262 GNU Extension:
21526606 6263
a723baf1 6264 condition:
21526606 6265 type-specifier-seq declarator asm-specification [opt]
a723baf1 6266 attributes [opt] = assignment-expression
21526606 6267
a723baf1
MM
6268 Returns the expression that should be tested. */
6269
6270static tree
94edc4ab 6271cp_parser_condition (cp_parser* parser)
a723baf1 6272{
62d1db17 6273 cp_decl_specifier_seq type_specifiers;
a723baf1
MM
6274 const char *saved_message;
6275
6276 /* Try the declaration first. */
6277 cp_parser_parse_tentatively (parser);
6278 /* New types are not allowed in the type-specifier-seq for a
6279 condition. */
6280 saved_message = parser->type_definition_forbidden_message;
6281 parser->type_definition_forbidden_message
6282 = "types may not be defined in conditions";
6283 /* Parse the type-specifier-seq. */
62d1db17 6284 cp_parser_type_specifier_seq (parser, &type_specifiers);
a723baf1
MM
6285 /* Restore the saved message. */
6286 parser->type_definition_forbidden_message = saved_message;
6287 /* If all is well, we might be looking at a declaration. */
6288 if (!cp_parser_error_occurred (parser))
6289 {
6290 tree decl;
6291 tree asm_specification;
6292 tree attributes;
058b15c1 6293 cp_declarator *declarator;
a723baf1 6294 tree initializer = NULL_TREE;
21526606 6295
a723baf1 6296 /* Parse the declarator. */
62b8a44e 6297 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 6298 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
6299 /*parenthesized_p=*/NULL,
6300 /*member_p=*/false);
a723baf1
MM
6301 /* Parse the attributes. */
6302 attributes = cp_parser_attributes_opt (parser);
6303 /* Parse the asm-specification. */
6304 asm_specification = cp_parser_asm_specification_opt (parser);
6305 /* If the next token is not an `=', then we might still be
6306 looking at an expression. For example:
21526606 6307
a723baf1 6308 if (A(a).x)
21526606 6309
a723baf1
MM
6310 looks like a decl-specifier-seq and a declarator -- but then
6311 there is no `=', so this is an expression. */
6312 cp_parser_require (parser, CPP_EQ, "`='");
6313 /* If we did see an `=', then we are looking at a declaration
6314 for sure. */
6315 if (cp_parser_parse_definitely (parser))
6316 {
4514aa8c 6317 tree pushed_scope;
73a8adb6 6318
a723baf1 6319 /* Create the declaration. */
62d1db17 6320 decl = start_decl (declarator, &type_specifiers,
a723baf1 6321 /*initialized_p=*/true,
73a8adb6 6322 attributes, /*prefix_attributes=*/NULL_TREE,
4514aa8c 6323 &pushed_scope);
a723baf1 6324 /* Parse the assignment-expression. */
93678513
MM
6325 initializer = cp_parser_assignment_expression (parser,
6326 /*cast_p=*/false);
21526606 6327
a723baf1 6328 /* Process the initializer. */
21526606
EC
6329 cp_finish_decl (decl,
6330 initializer,
6331 asm_specification,
a723baf1 6332 LOOKUP_ONLYCONVERTING);
c162c75e 6333
4514aa8c
NS
6334 if (pushed_scope)
6335 pop_scope (pushed_scope);
21526606 6336
a723baf1
MM
6337 return convert_from_reference (decl);
6338 }
6339 }
6340 /* If we didn't even get past the declarator successfully, we are
6341 definitely not looking at a declaration. */
6342 else
6343 cp_parser_abort_tentative_parse (parser);
6344
6345 /* Otherwise, we are looking at an expression. */
93678513 6346 return cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
6347}
6348
6349/* Parse an iteration-statement.
6350
6351 iteration-statement:
6352 while ( condition ) statement
6353 do statement while ( expression ) ;
6354 for ( for-init-statement condition [opt] ; expression [opt] )
6355 statement
6356
6357 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6358
6359static tree
94edc4ab 6360cp_parser_iteration_statement (cp_parser* parser)
a723baf1
MM
6361{
6362 cp_token *token;
6363 enum rid keyword;
6364 tree statement;
0e59b3fb
MM
6365 bool in_iteration_statement_p;
6366
a723baf1
MM
6367
6368 /* Peek at the next token. */
6369 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6370 if (!token)
6371 return error_mark_node;
6372
0e59b3fb 6373 /* Remember whether or not we are already within an iteration
21526606 6374 statement. */
0e59b3fb
MM
6375 in_iteration_statement_p = parser->in_iteration_statement_p;
6376
a723baf1
MM
6377 /* See what kind of keyword it is. */
6378 keyword = token->keyword;
6379 switch (keyword)
6380 {
6381 case RID_WHILE:
6382 {
6383 tree condition;
6384
6385 /* Begin the while-statement. */
6386 statement = begin_while_stmt ();
6387 /* Look for the `('. */
6388 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6389 /* Parse the condition. */
6390 condition = cp_parser_condition (parser);
6391 finish_while_stmt_cond (condition, statement);
6392 /* Look for the `)'. */
6393 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6394 /* Parse the dependent statement. */
0e59b3fb 6395 parser->in_iteration_statement_p = true;
a723baf1 6396 cp_parser_already_scoped_statement (parser);
0e59b3fb 6397 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
6398 /* We're done with the while-statement. */
6399 finish_while_stmt (statement);
6400 }
6401 break;
6402
6403 case RID_DO:
6404 {
6405 tree expression;
6406
6407 /* Begin the do-statement. */
6408 statement = begin_do_stmt ();
6409 /* Parse the body of the do-statement. */
0e59b3fb 6410 parser->in_iteration_statement_p = true;
a723baf1 6411 cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 6412 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
6413 finish_do_body (statement);
6414 /* Look for the `while' keyword. */
6415 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6416 /* Look for the `('. */
6417 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6418 /* Parse the expression. */
93678513 6419 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
6420 /* We're done with the do-statement. */
6421 finish_do_stmt (expression, statement);
6422 /* Look for the `)'. */
6423 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6424 /* Look for the `;'. */
6425 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6426 }
6427 break;
6428
6429 case RID_FOR:
6430 {
6431 tree condition = NULL_TREE;
6432 tree expression = NULL_TREE;
6433
6434 /* Begin the for-statement. */
6435 statement = begin_for_stmt ();
6436 /* Look for the `('. */
6437 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6438 /* Parse the initialization. */
6439 cp_parser_for_init_statement (parser);
6440 finish_for_init_stmt (statement);
6441
6442 /* If there's a condition, process it. */
6443 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6444 condition = cp_parser_condition (parser);
6445 finish_for_cond (condition, statement);
6446 /* Look for the `;'. */
6447 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6448
6449 /* If there's an expression, process it. */
6450 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
93678513 6451 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
6452 finish_for_expr (expression, statement);
6453 /* Look for the `)'. */
d5a10cf0 6454 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
98ca843c 6455
a723baf1 6456 /* Parse the body of the for-statement. */
0e59b3fb 6457 parser->in_iteration_statement_p = true;
a723baf1 6458 cp_parser_already_scoped_statement (parser);
0e59b3fb 6459 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
6460
6461 /* We're done with the for-statement. */
6462 finish_for_stmt (statement);
6463 }
6464 break;
6465
6466 default:
6467 cp_parser_error (parser, "expected iteration-statement");
6468 statement = error_mark_node;
6469 break;
6470 }
6471
6472 return statement;
6473}
6474
6475/* Parse a for-init-statement.
6476
6477 for-init-statement:
6478 expression-statement
6479 simple-declaration */
6480
6481static void
94edc4ab 6482cp_parser_for_init_statement (cp_parser* parser)
a723baf1
MM
6483{
6484 /* If the next token is a `;', then we have an empty
34cd5ae7 6485 expression-statement. Grammatically, this is also a
a723baf1
MM
6486 simple-declaration, but an invalid one, because it does not
6487 declare anything. Therefore, if we did not handle this case
6488 specially, we would issue an error message about an invalid
6489 declaration. */
6490 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6491 {
6492 /* We're going to speculatively look for a declaration, falling back
6493 to an expression, if necessary. */
6494 cp_parser_parse_tentatively (parser);
6495 /* Parse the declaration. */
6496 cp_parser_simple_declaration (parser,
6497 /*function_definition_allowed_p=*/false);
6498 /* If the tentative parse failed, then we shall need to look for an
6499 expression-statement. */
6500 if (cp_parser_parse_definitely (parser))
6501 return;
6502 }
6503
a5bcc582 6504 cp_parser_expression_statement (parser, false);
a723baf1
MM
6505}
6506
6507/* Parse a jump-statement.
6508
6509 jump-statement:
6510 break ;
6511 continue ;
6512 return expression [opt] ;
21526606 6513 goto identifier ;
a723baf1
MM
6514
6515 GNU extension:
6516
6517 jump-statement:
6518 goto * expression ;
6519
5088b058 6520 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
a723baf1
MM
6521
6522static tree
94edc4ab 6523cp_parser_jump_statement (cp_parser* parser)
a723baf1
MM
6524{
6525 tree statement = error_mark_node;
6526 cp_token *token;
6527 enum rid keyword;
6528
6529 /* Peek at the next token. */
6530 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6531 if (!token)
6532 return error_mark_node;
6533
6534 /* See what kind of keyword it is. */
6535 keyword = token->keyword;
6536 switch (keyword)
6537 {
6538 case RID_BREAK:
0e59b3fb
MM
6539 if (!parser->in_switch_statement_p
6540 && !parser->in_iteration_statement_p)
6541 {
6542 error ("break statement not within loop or switch");
6543 statement = error_mark_node;
6544 }
6545 else
6546 statement = finish_break_stmt ();
2a13a625 6547 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6548 break;
6549
6550 case RID_CONTINUE:
0e59b3fb
MM
6551 if (!parser->in_iteration_statement_p)
6552 {
6553 error ("continue statement not within a loop");
6554 statement = error_mark_node;
6555 }
6556 else
6557 statement = finish_continue_stmt ();
2a13a625 6558 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6559 break;
6560
6561 case RID_RETURN:
6562 {
6563 tree expr;
6564
21526606 6565 /* If the next token is a `;', then there is no
a723baf1
MM
6566 expression. */
6567 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
93678513 6568 expr = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
6569 else
6570 expr = NULL_TREE;
6571 /* Build the return-statement. */
6572 statement = finish_return_stmt (expr);
6573 /* Look for the final `;'. */
2a13a625 6574 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6575 }
6576 break;
6577
6578 case RID_GOTO:
6579 /* Create the goto-statement. */
6580 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6581 {
6582 /* Issue a warning about this use of a GNU extension. */
6583 if (pedantic)
6584 pedwarn ("ISO C++ forbids computed gotos");
6585 /* Consume the '*' token. */
6586 cp_lexer_consume_token (parser->lexer);
6587 /* Parse the dependent expression. */
93678513 6588 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
a723baf1
MM
6589 }
6590 else
6591 finish_goto_stmt (cp_parser_identifier (parser));
6592 /* Look for the final `;'. */
2a13a625 6593 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6594 break;
6595
6596 default:
6597 cp_parser_error (parser, "expected jump-statement");
6598 break;
6599 }
6600
6601 return statement;
6602}
6603
6604/* Parse a declaration-statement.
6605
6606 declaration-statement:
6607 block-declaration */
6608
6609static void
94edc4ab 6610cp_parser_declaration_statement (cp_parser* parser)
a723baf1 6611{
058b15c1
MM
6612 void *p;
6613
6614 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6615 p = obstack_alloc (&declarator_obstack, 0);
6616
6617 /* Parse the block-declaration. */
a723baf1
MM
6618 cp_parser_block_declaration (parser, /*statement_p=*/true);
6619
058b15c1
MM
6620 /* Free any declarators allocated. */
6621 obstack_free (&declarator_obstack, p);
6622
a723baf1
MM
6623 /* Finish off the statement. */
6624 finish_stmt ();
6625}
6626
6627/* Some dependent statements (like `if (cond) statement'), are
6628 implicitly in their own scope. In other words, if the statement is
6629 a single statement (as opposed to a compound-statement), it is
6630 none-the-less treated as if it were enclosed in braces. Any
6631 declarations appearing in the dependent statement are out of scope
6632 after control passes that point. This function parses a statement,
6633 but ensures that is in its own scope, even if it is not a
21526606 6634 compound-statement.
a723baf1
MM
6635
6636 Returns the new statement. */
6637
6638static tree
94edc4ab 6639cp_parser_implicitly_scoped_statement (cp_parser* parser)
a723baf1
MM
6640{
6641 tree statement;
6642
6643 /* If the token is not a `{', then we must take special action. */
6644 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6645 {
6646 /* Create a compound-statement. */
325c3691 6647 statement = begin_compound_stmt (0);
a723baf1 6648 /* Parse the dependent-statement. */
a5bcc582 6649 cp_parser_statement (parser, false);
a723baf1 6650 /* Finish the dummy compound-statement. */
7a3397c7 6651 finish_compound_stmt (statement);
a723baf1
MM
6652 }
6653 /* Otherwise, we simply parse the statement directly. */
6654 else
325c3691 6655 statement = cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
6656
6657 /* Return the statement. */
6658 return statement;
6659}
6660
6661/* For some dependent statements (like `while (cond) statement'), we
6662 have already created a scope. Therefore, even if the dependent
6663 statement is a compound-statement, we do not want to create another
6664 scope. */
6665
6666static void
94edc4ab 6667cp_parser_already_scoped_statement (cp_parser* parser)
a723baf1 6668{
325c3691
RH
6669 /* If the token is a `{', then we must take special action. */
6670 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6671 cp_parser_statement (parser, false);
6672 else
a723baf1 6673 {
325c3691
RH
6674 /* Avoid calling cp_parser_compound_statement, so that we
6675 don't create a new scope. Do everything else by hand. */
6676 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6677 cp_parser_statement_seq_opt (parser, false);
6678 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1 6679 }
a723baf1
MM
6680}
6681
6682/* Declarations [gram.dcl.dcl] */
6683
6684/* Parse an optional declaration-sequence.
6685
6686 declaration-seq:
6687 declaration
6688 declaration-seq declaration */
6689
6690static void
94edc4ab 6691cp_parser_declaration_seq_opt (cp_parser* parser)
a723baf1
MM
6692{
6693 while (true)
6694 {
6695 cp_token *token;
6696
6697 token = cp_lexer_peek_token (parser->lexer);
6698
6699 if (token->type == CPP_CLOSE_BRACE
6700 || token->type == CPP_EOF)
6701 break;
6702
21526606 6703 if (token->type == CPP_SEMICOLON)
a723baf1
MM
6704 {
6705 /* A declaration consisting of a single semicolon is
6706 invalid. Allow it unless we're being pedantic. */
a723baf1 6707 cp_lexer_consume_token (parser->lexer);
2cfe82fe
ZW
6708 if (pedantic && !in_system_header)
6709 pedwarn ("extra %<;%>");
a723baf1
MM
6710 continue;
6711 }
6712
7d381002 6713 /* If we're entering or exiting a region that's implicitly
03fd3f84 6714 extern "C", modify the lang context appropriately. */
7d381002
MA
6715 if (!parser->implicit_extern_c && token->implicit_extern_c)
6716 {
6717 push_lang_context (lang_name_c);
6718 parser->implicit_extern_c = true;
6719 }
6720 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6721 {
6722 pop_lang_context ();
6723 parser->implicit_extern_c = false;
6724 }
6725
36952dea
ZW
6726 if (token->type == CPP_PRAGMA)
6727 {
6728 /* A top-level declaration can consist solely of a #pragma.
6729 A nested declaration cannot, so this is done here and not
6730 in cp_parser_declaration. (A #pragma at block scope is
6731 handled in cp_parser_statement.) */
6732 cp_lexer_handle_pragma (parser->lexer);
6733 continue;
6734 }
6735
c838d82f 6736 /* Parse the declaration itself. */
a723baf1
MM
6737 cp_parser_declaration (parser);
6738 }
6739}
6740
6741/* Parse a declaration.
6742
6743 declaration:
6744 block-declaration
6745 function-definition
6746 template-declaration
6747 explicit-instantiation
6748 explicit-specialization
6749 linkage-specification
21526606 6750 namespace-definition
1092805d
MM
6751
6752 GNU extension:
6753
6754 declaration:
6755 __extension__ declaration */
a723baf1
MM
6756
6757static void
94edc4ab 6758cp_parser_declaration (cp_parser* parser)
a723baf1
MM
6759{
6760 cp_token token1;
6761 cp_token token2;
1092805d 6762 int saved_pedantic;
058b15c1 6763 void *p;
1092805d
MM
6764
6765 /* Check for the `__extension__' keyword. */
6766 if (cp_parser_extension_opt (parser, &saved_pedantic))
6767 {
6768 /* Parse the qualified declaration. */
6769 cp_parser_declaration (parser);
6770 /* Restore the PEDANTIC flag. */
6771 pedantic = saved_pedantic;
6772
6773 return;
6774 }
a723baf1
MM
6775
6776 /* Try to figure out what kind of declaration is present. */
6777 token1 = *cp_lexer_peek_token (parser->lexer);
21526606 6778
a723baf1
MM
6779 if (token1.type != CPP_EOF)
6780 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6781
058b15c1
MM
6782 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6783 p = obstack_alloc (&declarator_obstack, 0);
6784
a723baf1
MM
6785 /* If the next token is `extern' and the following token is a string
6786 literal, then we have a linkage specification. */
6787 if (token1.keyword == RID_EXTERN
6788 && cp_parser_is_string_literal (&token2))
6789 cp_parser_linkage_specification (parser);
6790 /* If the next token is `template', then we have either a template
6791 declaration, an explicit instantiation, or an explicit
6792 specialization. */
6793 else if (token1.keyword == RID_TEMPLATE)
6794 {
6795 /* `template <>' indicates a template specialization. */
6796 if (token2.type == CPP_LESS
6797 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6798 cp_parser_explicit_specialization (parser);
6799 /* `template <' indicates a template declaration. */
6800 else if (token2.type == CPP_LESS)
6801 cp_parser_template_declaration (parser, /*member_p=*/false);
6802 /* Anything else must be an explicit instantiation. */
6803 else
6804 cp_parser_explicit_instantiation (parser);
6805 }
6806 /* If the next token is `export', then we have a template
6807 declaration. */
6808 else if (token1.keyword == RID_EXPORT)
6809 cp_parser_template_declaration (parser, /*member_p=*/false);
6810 /* If the next token is `extern', 'static' or 'inline' and the one
6811 after that is `template', we have a GNU extended explicit
6812 instantiation directive. */
6813 else if (cp_parser_allow_gnu_extensions_p (parser)
6814 && (token1.keyword == RID_EXTERN
6815 || token1.keyword == RID_STATIC
6816 || token1.keyword == RID_INLINE)
6817 && token2.keyword == RID_TEMPLATE)
6818 cp_parser_explicit_instantiation (parser);
6819 /* If the next token is `namespace', check for a named or unnamed
6820 namespace definition. */
6821 else if (token1.keyword == RID_NAMESPACE
6822 && (/* A named namespace definition. */
6823 (token2.type == CPP_NAME
21526606 6824 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
a723baf1
MM
6825 == CPP_OPEN_BRACE))
6826 /* An unnamed namespace definition. */
6827 || token2.type == CPP_OPEN_BRACE))
6828 cp_parser_namespace_definition (parser);
6829 /* We must have either a block declaration or a function
6830 definition. */
6831 else
6832 /* Try to parse a block-declaration, or a function-definition. */
6833 cp_parser_block_declaration (parser, /*statement_p=*/false);
058b15c1
MM
6834
6835 /* Free any declarators allocated. */
6836 obstack_free (&declarator_obstack, p);
a723baf1
MM
6837}
6838
21526606 6839/* Parse a block-declaration.
a723baf1
MM
6840
6841 block-declaration:
6842 simple-declaration
6843 asm-definition
6844 namespace-alias-definition
6845 using-declaration
21526606 6846 using-directive
a723baf1
MM
6847
6848 GNU Extension:
6849
6850 block-declaration:
21526606 6851 __extension__ block-declaration
a723baf1
MM
6852 label-declaration
6853
34cd5ae7 6854 If STATEMENT_P is TRUE, then this block-declaration is occurring as
a723baf1
MM
6855 part of a declaration-statement. */
6856
6857static void
21526606 6858cp_parser_block_declaration (cp_parser *parser,
a723baf1
MM
6859 bool statement_p)
6860{
6861 cp_token *token1;
6862 int saved_pedantic;
6863
6864 /* Check for the `__extension__' keyword. */
6865 if (cp_parser_extension_opt (parser, &saved_pedantic))
6866 {
6867 /* Parse the qualified declaration. */
6868 cp_parser_block_declaration (parser, statement_p);
6869 /* Restore the PEDANTIC flag. */
6870 pedantic = saved_pedantic;
6871
6872 return;
6873 }
6874
6875 /* Peek at the next token to figure out which kind of declaration is
6876 present. */
6877 token1 = cp_lexer_peek_token (parser->lexer);
6878
6879 /* If the next keyword is `asm', we have an asm-definition. */
6880 if (token1->keyword == RID_ASM)
6881 {
6882 if (statement_p)
6883 cp_parser_commit_to_tentative_parse (parser);
6884 cp_parser_asm_definition (parser);
6885 }
6886 /* If the next keyword is `namespace', we have a
6887 namespace-alias-definition. */
6888 else if (token1->keyword == RID_NAMESPACE)
6889 cp_parser_namespace_alias_definition (parser);
6890 /* If the next keyword is `using', we have either a
6891 using-declaration or a using-directive. */
6892 else if (token1->keyword == RID_USING)
6893 {
6894 cp_token *token2;
6895
6896 if (statement_p)
6897 cp_parser_commit_to_tentative_parse (parser);
6898 /* If the token after `using' is `namespace', then we have a
6899 using-directive. */
6900 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6901 if (token2->keyword == RID_NAMESPACE)
6902 cp_parser_using_directive (parser);
6903 /* Otherwise, it's a using-declaration. */
6904 else
6905 cp_parser_using_declaration (parser);
6906 }
6907 /* If the next keyword is `__label__' we have a label declaration. */
6908 else if (token1->keyword == RID_LABEL)
6909 {
6910 if (statement_p)
6911 cp_parser_commit_to_tentative_parse (parser);
6912 cp_parser_label_declaration (parser);
6913 }
6914 /* Anything else must be a simple-declaration. */
6915 else
6916 cp_parser_simple_declaration (parser, !statement_p);
6917}
6918
6919/* Parse a simple-declaration.
6920
6921 simple-declaration:
21526606 6922 decl-specifier-seq [opt] init-declarator-list [opt] ;
a723baf1
MM
6923
6924 init-declarator-list:
6925 init-declarator
21526606 6926 init-declarator-list , init-declarator
a723baf1 6927
34cd5ae7 6928 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9bcb9aae 6929 function-definition as a simple-declaration. */
a723baf1
MM
6930
6931static void
21526606 6932cp_parser_simple_declaration (cp_parser* parser,
94edc4ab 6933 bool function_definition_allowed_p)
a723baf1 6934{
62d1db17 6935 cp_decl_specifier_seq decl_specifiers;
560ad596 6936 int declares_class_or_enum;
a723baf1
MM
6937 bool saw_declarator;
6938
6939 /* Defer access checks until we know what is being declared; the
6940 checks for names appearing in the decl-specifier-seq should be
6941 done as if we were in the scope of the thing being declared. */
8d241e0b 6942 push_deferring_access_checks (dk_deferred);
cf22909c 6943
a723baf1
MM
6944 /* Parse the decl-specifier-seq. We have to keep track of whether
6945 or not the decl-specifier-seq declares a named class or
6946 enumeration type, since that is the only case in which the
21526606 6947 init-declarator-list is allowed to be empty.
a723baf1
MM
6948
6949 [dcl.dcl]
6950
6951 In a simple-declaration, the optional init-declarator-list can be
6952 omitted only when declaring a class or enumeration, that is when
6953 the decl-specifier-seq contains either a class-specifier, an
6954 elaborated-type-specifier, or an enum-specifier. */
62d1db17
MM
6955 cp_parser_decl_specifier_seq (parser,
6956 CP_PARSER_FLAGS_OPTIONAL,
6957 &decl_specifiers,
6958 &declares_class_or_enum);
a723baf1 6959 /* We no longer need to defer access checks. */
cf22909c 6960 stop_deferring_access_checks ();
24c0ef37 6961
39703eb9
MM
6962 /* In a block scope, a valid declaration must always have a
6963 decl-specifier-seq. By not trying to parse declarators, we can
6964 resolve the declaration/expression ambiguity more quickly. */
98ca843c 6965 if (!function_definition_allowed_p
62d1db17 6966 && !decl_specifiers.any_specifiers_p)
39703eb9
MM
6967 {
6968 cp_parser_error (parser, "expected declaration");
6969 goto done;
6970 }
6971
8fbc5ae7
MM
6972 /* If the next two tokens are both identifiers, the code is
6973 erroneous. The usual cause of this situation is code like:
6974
6975 T t;
6976
6977 where "T" should name a type -- but does not. */
de3fe73c
MM
6978 if (!decl_specifiers.type
6979 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8fbc5ae7 6980 {
8d241e0b 6981 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
6982 looking at a declaration. */
6983 cp_parser_commit_to_tentative_parse (parser);
6984 /* Give up. */
39703eb9 6985 goto done;
8fbc5ae7 6986 }
996c2b52
MM
6987
6988 /* If we have seen at least one decl-specifier, and the next token
6989 is not a parenthesis, then we must be looking at a declaration.
6990 (After "int (" we might be looking at a functional cast.) */
6991 if (decl_specifiers.any_specifiers_p
6992 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6993 cp_parser_commit_to_tentative_parse (parser);
8fbc5ae7 6994
a723baf1
MM
6995 /* Keep going until we hit the `;' at the end of the simple
6996 declaration. */
6997 saw_declarator = false;
21526606 6998 while (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
6999 CPP_SEMICOLON))
7000 {
7001 cp_token *token;
7002 bool function_definition_p;
560ad596 7003 tree decl;
a723baf1
MM
7004
7005 saw_declarator = true;
7006 /* Parse the init-declarator. */
62d1db17 7007 decl = cp_parser_init_declarator (parser, &decl_specifiers,
560ad596
MM
7008 function_definition_allowed_p,
7009 /*member_p=*/false,
7010 declares_class_or_enum,
7011 &function_definition_p);
1fb3244a
MM
7012 /* If an error occurred while parsing tentatively, exit quickly.
7013 (That usually happens when in the body of a function; each
7014 statement is treated as a declaration-statement until proven
7015 otherwise.) */
7016 if (cp_parser_error_occurred (parser))
39703eb9 7017 goto done;
a723baf1
MM
7018 /* Handle function definitions specially. */
7019 if (function_definition_p)
7020 {
7021 /* If the next token is a `,', then we are probably
7022 processing something like:
7023
7024 void f() {}, *p;
7025
7026 which is erroneous. */
7027 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7028 error ("mixing declarations and function-definitions is forbidden");
7029 /* Otherwise, we're done with the list of declarators. */
7030 else
24c0ef37 7031 {
cf22909c 7032 pop_deferring_access_checks ();
24c0ef37
GS
7033 return;
7034 }
a723baf1
MM
7035 }
7036 /* The next token should be either a `,' or a `;'. */
7037 token = cp_lexer_peek_token (parser->lexer);
7038 /* If it's a `,', there are more declarators to come. */
7039 if (token->type == CPP_COMMA)
7040 cp_lexer_consume_token (parser->lexer);
7041 /* If it's a `;', we are done. */
7042 else if (token->type == CPP_SEMICOLON)
7043 break;
7044 /* Anything else is an error. */
7045 else
7046 {
996c2b52
MM
7047 /* If we have already issued an error message we don't need
7048 to issue another one. */
7049 if (decl != error_mark_node
0b16f8f4 7050 || cp_parser_uncommitted_to_tentative_parse_p (parser))
2a13a625 7051 cp_parser_error (parser, "expected %<,%> or %<;%>");
a723baf1
MM
7052 /* Skip tokens until we reach the end of the statement. */
7053 cp_parser_skip_to_end_of_statement (parser);
5a98fa7b
MM
7054 /* If the next token is now a `;', consume it. */
7055 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7056 cp_lexer_consume_token (parser->lexer);
39703eb9 7057 goto done;
a723baf1
MM
7058 }
7059 /* After the first time around, a function-definition is not
7060 allowed -- even if it was OK at first. For example:
7061
7062 int i, f() {}
7063
7064 is not valid. */
7065 function_definition_allowed_p = false;
7066 }
7067
7068 /* Issue an error message if no declarators are present, and the
7069 decl-specifier-seq does not itself declare a class or
7070 enumeration. */
7071 if (!saw_declarator)
7072 {
7073 if (cp_parser_declares_only_class_p (parser))
62d1db17 7074 shadow_tag (&decl_specifiers);
a723baf1 7075 /* Perform any deferred access checks. */
cf22909c 7076 perform_deferred_access_checks ();
a723baf1
MM
7077 }
7078
7079 /* Consume the `;'. */
7080 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7081
39703eb9
MM
7082 done:
7083 pop_deferring_access_checks ();
a723baf1
MM
7084}
7085
7086/* Parse a decl-specifier-seq.
7087
7088 decl-specifier-seq:
7089 decl-specifier-seq [opt] decl-specifier
7090
7091 decl-specifier:
7092 storage-class-specifier
7093 type-specifier
7094 function-specifier
7095 friend
21526606 7096 typedef
a723baf1
MM
7097
7098 GNU Extension:
7099
15077df5
MM
7100 decl-specifier:
7101 attributes
a723baf1 7102
62d1db17 7103 Set *DECL_SPECS to a representation of the decl-specifier-seq.
a723baf1 7104
eb1aef53 7105 The parser flags FLAGS is used to control type-specifier parsing.
560ad596
MM
7106
7107 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
543ca912 7108 flags:
560ad596
MM
7109
7110 1: one of the decl-specifiers is an elaborated-type-specifier
543ca912 7111 (i.e., a type declaration)
560ad596 7112 2: one of the decl-specifiers is an enum-specifier or a
543ca912 7113 class-specifier (i.e., a type definition)
98ca843c 7114
560ad596 7115 */
a723baf1 7116
62d1db17 7117static void
21526606 7118cp_parser_decl_specifier_seq (cp_parser* parser,
62d1db17
MM
7119 cp_parser_flags flags,
7120 cp_decl_specifier_seq *decl_specs,
560ad596 7121 int* declares_class_or_enum)
a723baf1 7122{
f2ce60b8 7123 bool constructor_possible_p = !parser->in_declarator_p;
21526606 7124
62d1db17
MM
7125 /* Clear DECL_SPECS. */
7126 clear_decl_specs (decl_specs);
7127
a723baf1 7128 /* Assume no class or enumeration type is declared. */
560ad596 7129 *declares_class_or_enum = 0;
a723baf1 7130
a723baf1
MM
7131 /* Keep reading specifiers until there are no more to read. */
7132 while (true)
7133 {
a723baf1 7134 bool constructor_p;
62d1db17 7135 bool found_decl_spec;
a723baf1
MM
7136 cp_token *token;
7137
7138 /* Peek at the next token. */
7139 token = cp_lexer_peek_token (parser->lexer);
7140 /* Handle attributes. */
7141 if (token->keyword == RID_ATTRIBUTE)
7142 {
7143 /* Parse the attributes. */
98ca843c 7144 decl_specs->attributes
62d1db17
MM
7145 = chainon (decl_specs->attributes,
7146 cp_parser_attributes_opt (parser));
a723baf1
MM
7147 continue;
7148 }
62d1db17
MM
7149 /* Assume we will find a decl-specifier keyword. */
7150 found_decl_spec = true;
a723baf1
MM
7151 /* If the next token is an appropriate keyword, we can simply
7152 add it to the list. */
7153 switch (token->keyword)
7154 {
a723baf1
MM
7155 /* decl-specifier:
7156 friend */
62d1db17
MM
7157 case RID_FRIEND:
7158 if (decl_specs->specs[(int) ds_friend]++)
2a13a625 7159 error ("duplicate %<friend%>");
a723baf1
MM
7160 /* Consume the token. */
7161 cp_lexer_consume_token (parser->lexer);
7162 break;
7163
7164 /* function-specifier:
7165 inline
7166 virtual
7167 explicit */
7168 case RID_INLINE:
7169 case RID_VIRTUAL:
7170 case RID_EXPLICIT:
62d1db17 7171 cp_parser_function_specifier_opt (parser, decl_specs);
a723baf1 7172 break;
21526606 7173
a723baf1
MM
7174 /* decl-specifier:
7175 typedef */
7176 case RID_TYPEDEF:
62d1db17 7177 ++decl_specs->specs[(int) ds_typedef];
a723baf1
MM
7178 /* Consume the token. */
7179 cp_lexer_consume_token (parser->lexer);
2050a1bb
MM
7180 /* A constructor declarator cannot appear in a typedef. */
7181 constructor_possible_p = false;
c006d942
MM
7182 /* The "typedef" keyword can only occur in a declaration; we
7183 may as well commit at this point. */
7184 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
7185 break;
7186
7187 /* storage-class-specifier:
7188 auto
7189 register
7190 static
7191 extern
21526606 7192 mutable
a723baf1
MM
7193
7194 GNU Extension:
7195 thread */
7196 case RID_AUTO:
62d1db17
MM
7197 /* Consume the token. */
7198 cp_lexer_consume_token (parser->lexer);
7199 cp_parser_set_storage_class (decl_specs, sc_auto);
7200 break;
a723baf1 7201 case RID_REGISTER:
62d1db17
MM
7202 /* Consume the token. */
7203 cp_lexer_consume_token (parser->lexer);
7204 cp_parser_set_storage_class (decl_specs, sc_register);
7205 break;
a723baf1 7206 case RID_STATIC:
62d1db17
MM
7207 /* Consume the token. */
7208 cp_lexer_consume_token (parser->lexer);
7209 if (decl_specs->specs[(int) ds_thread])
f1b90a04 7210 {
2d01edd7 7211 error ("%<__thread%> before %<static%>");
f1b90a04
MM
7212 decl_specs->specs[(int) ds_thread] = 0;
7213 }
7214 cp_parser_set_storage_class (decl_specs, sc_static);
62d1db17 7215 break;
a723baf1 7216 case RID_EXTERN:
62d1db17
MM
7217 /* Consume the token. */
7218 cp_lexer_consume_token (parser->lexer);
7219 if (decl_specs->specs[(int) ds_thread])
f1b90a04 7220 {
2d01edd7 7221 error ("%<__thread%> before %<extern%>");
f1b90a04
MM
7222 decl_specs->specs[(int) ds_thread] = 0;
7223 }
7224 cp_parser_set_storage_class (decl_specs, sc_extern);
62d1db17 7225 break;
a723baf1 7226 case RID_MUTABLE:
62d1db17
MM
7227 /* Consume the token. */
7228 cp_lexer_consume_token (parser->lexer);
7229 cp_parser_set_storage_class (decl_specs, sc_mutable);
7230 break;
a723baf1 7231 case RID_THREAD:
62d1db17
MM
7232 /* Consume the token. */
7233 cp_lexer_consume_token (parser->lexer);
7234 ++decl_specs->specs[(int) ds_thread];
a723baf1 7235 break;
21526606 7236
a723baf1 7237 default:
62d1db17
MM
7238 /* We did not yet find a decl-specifier yet. */
7239 found_decl_spec = false;
a723baf1
MM
7240 break;
7241 }
7242
7243 /* Constructors are a special case. The `S' in `S()' is not a
7244 decl-specifier; it is the beginning of the declarator. */
98ca843c 7245 constructor_p
62d1db17
MM
7246 = (!found_decl_spec
7247 && constructor_possible_p
98ca843c 7248 && (cp_parser_constructor_declarator_p
62d1db17 7249 (parser, decl_specs->specs[(int) ds_friend] != 0)));
a723baf1
MM
7250
7251 /* If we don't have a DECL_SPEC yet, then we must be looking at
7252 a type-specifier. */
62d1db17 7253 if (!found_decl_spec && !constructor_p)
a723baf1 7254 {
560ad596 7255 int decl_spec_declares_class_or_enum;
a723baf1 7256 bool is_cv_qualifier;
62d1db17 7257 tree type_spec;
a723baf1 7258
62d1db17 7259 type_spec
a723baf1 7260 = cp_parser_type_specifier (parser, flags,
62d1db17 7261 decl_specs,
a723baf1
MM
7262 /*is_declaration=*/true,
7263 &decl_spec_declares_class_or_enum,
7264 &is_cv_qualifier);
7265
7266 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7267
7268 /* If this type-specifier referenced a user-defined type
7269 (a typedef, class-name, etc.), then we can't allow any
7270 more such type-specifiers henceforth.
7271
7272 [dcl.spec]
7273
7274 The longest sequence of decl-specifiers that could
7275 possibly be a type name is taken as the
7276 decl-specifier-seq of a declaration. The sequence shall
7277 be self-consistent as described below.
7278
7279 [dcl.type]
7280
7281 As a general rule, at most one type-specifier is allowed
7282 in the complete decl-specifier-seq of a declaration. The
7283 only exceptions are the following:
7284
7285 -- const or volatile can be combined with any other
21526606 7286 type-specifier.
a723baf1
MM
7287
7288 -- signed or unsigned can be combined with char, long,
7289 short, or int.
7290
7291 -- ..
7292
7293 Example:
7294
7295 typedef char* Pc;
7296 void g (const int Pc);
7297
7298 Here, Pc is *not* part of the decl-specifier seq; it's
7299 the declarator. Therefore, once we see a type-specifier
7300 (other than a cv-qualifier), we forbid any additional
7301 user-defined types. We *do* still allow things like `int
7302 int' to be considered a decl-specifier-seq, and issue the
7303 error message later. */
62d1db17 7304 if (type_spec && !is_cv_qualifier)
a723baf1 7305 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
2050a1bb 7306 /* A constructor declarator cannot follow a type-specifier. */
62d1db17 7307 if (type_spec)
a723baf1 7308 {
62d1db17
MM
7309 constructor_possible_p = false;
7310 found_decl_spec = true;
a723baf1 7311 }
a723baf1
MM
7312 }
7313
62d1db17
MM
7314 /* If we still do not have a DECL_SPEC, then there are no more
7315 decl-specifiers. */
7316 if (!found_decl_spec)
7317 break;
a723baf1 7318
62d1db17 7319 decl_specs->any_specifiers_p = true;
a723baf1
MM
7320 /* After we see one decl-specifier, further decl-specifiers are
7321 always optional. */
7322 flags |= CP_PARSER_FLAGS_OPTIONAL;
7323 }
7324
0426c4ca 7325 /* Don't allow a friend specifier with a class definition. */
62d1db17
MM
7326 if (decl_specs->specs[(int) ds_friend] != 0
7327 && (*declares_class_or_enum & 2))
0426c4ca 7328 error ("class definition may not be declared a friend");
a723baf1
MM
7329}
7330
21526606 7331/* Parse an (optional) storage-class-specifier.
a723baf1
MM
7332
7333 storage-class-specifier:
7334 auto
7335 register
7336 static
7337 extern
21526606 7338 mutable
a723baf1
MM
7339
7340 GNU Extension:
7341
7342 storage-class-specifier:
7343 thread
7344
7345 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
21526606 7346
a723baf1 7347static tree
94edc4ab 7348cp_parser_storage_class_specifier_opt (cp_parser* parser)
a723baf1
MM
7349{
7350 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7351 {
7352 case RID_AUTO:
7353 case RID_REGISTER:
7354 case RID_STATIC:
7355 case RID_EXTERN:
7356 case RID_MUTABLE:
7357 case RID_THREAD:
7358 /* Consume the token. */
7359 return cp_lexer_consume_token (parser->lexer)->value;
7360
7361 default:
7362 return NULL_TREE;
7363 }
7364}
7365
21526606 7366/* Parse an (optional) function-specifier.
a723baf1
MM
7367
7368 function-specifier:
7369 inline
7370 virtual
7371 explicit
7372
62d1db17
MM
7373 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7374 Updates DECL_SPECS, if it is non-NULL. */
21526606 7375
a723baf1 7376static tree
62d1db17
MM
7377cp_parser_function_specifier_opt (cp_parser* parser,
7378 cp_decl_specifier_seq *decl_specs)
a723baf1
MM
7379{
7380 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7381 {
7382 case RID_INLINE:
62d1db17
MM
7383 if (decl_specs)
7384 ++decl_specs->specs[(int) ds_inline];
7385 break;
7386
a723baf1 7387 case RID_VIRTUAL:
62d1db17
MM
7388 if (decl_specs)
7389 ++decl_specs->specs[(int) ds_virtual];
7390 break;
7391
a723baf1 7392 case RID_EXPLICIT:
62d1db17
MM
7393 if (decl_specs)
7394 ++decl_specs->specs[(int) ds_explicit];
7395 break;
a723baf1
MM
7396
7397 default:
7398 return NULL_TREE;
7399 }
62d1db17
MM
7400
7401 /* Consume the token. */
7402 return cp_lexer_consume_token (parser->lexer)->value;
a723baf1
MM
7403}
7404
7405/* Parse a linkage-specification.
7406
7407 linkage-specification:
7408 extern string-literal { declaration-seq [opt] }
7409 extern string-literal declaration */
7410
7411static void
94edc4ab 7412cp_parser_linkage_specification (cp_parser* parser)
a723baf1 7413{
a723baf1
MM
7414 tree linkage;
7415
7416 /* Look for the `extern' keyword. */
7417 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7418
c162c75e
MA
7419 /* Look for the string-literal. */
7420 linkage = cp_parser_string_literal (parser, false, false);
a723baf1
MM
7421
7422 /* Transform the literal into an identifier. If the literal is a
7423 wide-character string, or contains embedded NULs, then we can't
7424 handle it as the user wants. */
c162c75e
MA
7425 if (strlen (TREE_STRING_POINTER (linkage))
7426 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
a723baf1
MM
7427 {
7428 cp_parser_error (parser, "invalid linkage-specification");
7429 /* Assume C++ linkage. */
c162c75e 7430 linkage = lang_name_cplusplus;
a723baf1 7431 }
a723baf1 7432 else
c162c75e 7433 linkage = get_identifier (TREE_STRING_POINTER (linkage));
a723baf1
MM
7434
7435 /* We're now using the new linkage. */
7436 push_lang_context (linkage);
7437
7438 /* If the next token is a `{', then we're using the first
7439 production. */
7440 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7441 {
7442 /* Consume the `{' token. */
7443 cp_lexer_consume_token (parser->lexer);
7444 /* Parse the declarations. */
7445 cp_parser_declaration_seq_opt (parser);
7446 /* Look for the closing `}'. */
7447 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7448 }
7449 /* Otherwise, there's just one declaration. */
7450 else
7451 {
7452 bool saved_in_unbraced_linkage_specification_p;
7453
21526606 7454 saved_in_unbraced_linkage_specification_p
a723baf1
MM
7455 = parser->in_unbraced_linkage_specification_p;
7456 parser->in_unbraced_linkage_specification_p = true;
7457 have_extern_spec = true;
7458 cp_parser_declaration (parser);
7459 have_extern_spec = false;
21526606 7460 parser->in_unbraced_linkage_specification_p
a723baf1
MM
7461 = saved_in_unbraced_linkage_specification_p;
7462 }
7463
7464 /* We're done with the linkage-specification. */
7465 pop_lang_context ();
7466}
7467
7468/* Special member functions [gram.special] */
7469
7470/* Parse a conversion-function-id.
7471
7472 conversion-function-id:
21526606 7473 operator conversion-type-id
a723baf1
MM
7474
7475 Returns an IDENTIFIER_NODE representing the operator. */
7476
21526606 7477static tree
94edc4ab 7478cp_parser_conversion_function_id (cp_parser* parser)
a723baf1
MM
7479{
7480 tree type;
7481 tree saved_scope;
7482 tree saved_qualifying_scope;
7483 tree saved_object_scope;
4514aa8c 7484 tree pushed_scope = NULL_TREE;
a723baf1
MM
7485
7486 /* Look for the `operator' token. */
7487 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7488 return error_mark_node;
7489 /* When we parse the conversion-type-id, the current scope will be
7490 reset. However, we need that information in able to look up the
7491 conversion function later, so we save it here. */
7492 saved_scope = parser->scope;
7493 saved_qualifying_scope = parser->qualifying_scope;
7494 saved_object_scope = parser->object_scope;
7495 /* We must enter the scope of the class so that the names of
7496 entities declared within the class are available in the
7497 conversion-type-id. For example, consider:
7498
21526606 7499 struct S {
a723baf1
MM
7500 typedef int I;
7501 operator I();
7502 };
7503
7504 S::operator I() { ... }
7505
7506 In order to see that `I' is a type-name in the definition, we
7507 must be in the scope of `S'. */
7508 if (saved_scope)
4514aa8c 7509 pushed_scope = push_scope (saved_scope);
a723baf1
MM
7510 /* Parse the conversion-type-id. */
7511 type = cp_parser_conversion_type_id (parser);
7512 /* Leave the scope of the class, if any. */
4514aa8c
NS
7513 if (pushed_scope)
7514 pop_scope (pushed_scope);
a723baf1
MM
7515 /* Restore the saved scope. */
7516 parser->scope = saved_scope;
7517 parser->qualifying_scope = saved_qualifying_scope;
7518 parser->object_scope = saved_object_scope;
7519 /* If the TYPE is invalid, indicate failure. */
7520 if (type == error_mark_node)
7521 return error_mark_node;
7522 return mangle_conv_op_name_for_type (type);
7523}
7524
7525/* Parse a conversion-type-id:
7526
7527 conversion-type-id:
7528 type-specifier-seq conversion-declarator [opt]
7529
7530 Returns the TYPE specified. */
7531
7532static tree
94edc4ab 7533cp_parser_conversion_type_id (cp_parser* parser)
a723baf1
MM
7534{
7535 tree attributes;
62d1db17 7536 cp_decl_specifier_seq type_specifiers;
058b15c1 7537 cp_declarator *declarator;
037cc9c5 7538 tree type_specified;
a723baf1
MM
7539
7540 /* Parse the attributes. */
7541 attributes = cp_parser_attributes_opt (parser);
7542 /* Parse the type-specifiers. */
62d1db17 7543 cp_parser_type_specifier_seq (parser, &type_specifiers);
a723baf1 7544 /* If that didn't work, stop. */
62d1db17 7545 if (type_specifiers.type == error_mark_node)
a723baf1
MM
7546 return error_mark_node;
7547 /* Parse the conversion-declarator. */
7548 declarator = cp_parser_conversion_declarator_opt (parser);
7549
037cc9c5
FJ
7550 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7551 /*initialized=*/0, &attributes);
7552 if (attributes)
7553 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7554 return type_specified;
a723baf1
MM
7555}
7556
7557/* Parse an (optional) conversion-declarator.
7558
7559 conversion-declarator:
21526606 7560 ptr-operator conversion-declarator [opt]
a723baf1 7561
058b15c1 7562 */
a723baf1 7563
058b15c1 7564static cp_declarator *
94edc4ab 7565cp_parser_conversion_declarator_opt (cp_parser* parser)
a723baf1
MM
7566{
7567 enum tree_code code;
7568 tree class_type;
3c01e5df 7569 cp_cv_quals cv_quals;
a723baf1
MM
7570
7571 /* We don't know if there's a ptr-operator next, or not. */
7572 cp_parser_parse_tentatively (parser);
7573 /* Try the ptr-operator. */
3c01e5df 7574 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
a723baf1
MM
7575 /* If it worked, look for more conversion-declarators. */
7576 if (cp_parser_parse_definitely (parser))
7577 {
058b15c1 7578 cp_declarator *declarator;
98ca843c 7579
058b15c1
MM
7580 /* Parse another optional declarator. */
7581 declarator = cp_parser_conversion_declarator_opt (parser);
98ca843c 7582
058b15c1
MM
7583 /* Create the representation of the declarator. */
7584 if (class_type)
3c01e5df 7585 declarator = make_ptrmem_declarator (cv_quals, class_type,
a723baf1 7586 declarator);
058b15c1 7587 else if (code == INDIRECT_REF)
3c01e5df 7588 declarator = make_pointer_declarator (cv_quals, declarator);
058b15c1 7589 else
3c01e5df 7590 declarator = make_reference_declarator (cv_quals, declarator);
98ca843c 7591
058b15c1 7592 return declarator;
a723baf1
MM
7593 }
7594
058b15c1 7595 return NULL;
a723baf1
MM
7596}
7597
7598/* Parse an (optional) ctor-initializer.
7599
7600 ctor-initializer:
21526606 7601 : mem-initializer-list
a723baf1
MM
7602
7603 Returns TRUE iff the ctor-initializer was actually present. */
7604
7605static bool
94edc4ab 7606cp_parser_ctor_initializer_opt (cp_parser* parser)
a723baf1
MM
7607{
7608 /* If the next token is not a `:', then there is no
7609 ctor-initializer. */
7610 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7611 {
7612 /* Do default initialization of any bases and members. */
7613 if (DECL_CONSTRUCTOR_P (current_function_decl))
7614 finish_mem_initializers (NULL_TREE);
7615
7616 return false;
7617 }
7618
7619 /* Consume the `:' token. */
7620 cp_lexer_consume_token (parser->lexer);
7621 /* And the mem-initializer-list. */
7622 cp_parser_mem_initializer_list (parser);
7623
7624 return true;
7625}
7626
7627/* Parse a mem-initializer-list.
7628
7629 mem-initializer-list:
7630 mem-initializer
7631 mem-initializer , mem-initializer-list */
7632
7633static void
94edc4ab 7634cp_parser_mem_initializer_list (cp_parser* parser)
a723baf1
MM
7635{
7636 tree mem_initializer_list = NULL_TREE;
7637
7638 /* Let the semantic analysis code know that we are starting the
7639 mem-initializer-list. */
0e136342
MM
7640 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7641 error ("only constructors take base initializers");
a723baf1
MM
7642
7643 /* Loop through the list. */
7644 while (true)
7645 {
7646 tree mem_initializer;
7647
7648 /* Parse the mem-initializer. */
7649 mem_initializer = cp_parser_mem_initializer (parser);
7650 /* Add it to the list, unless it was erroneous. */
7651 if (mem_initializer)
7652 {
7653 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7654 mem_initializer_list = mem_initializer;
7655 }
7656 /* If the next token is not a `,', we're done. */
7657 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7658 break;
7659 /* Consume the `,' token. */
7660 cp_lexer_consume_token (parser->lexer);
7661 }
7662
7663 /* Perform semantic analysis. */
0e136342
MM
7664 if (DECL_CONSTRUCTOR_P (current_function_decl))
7665 finish_mem_initializers (mem_initializer_list);
a723baf1
MM
7666}
7667
7668/* Parse a mem-initializer.
7669
7670 mem-initializer:
21526606 7671 mem-initializer-id ( expression-list [opt] )
a723baf1
MM
7672
7673 GNU extension:
21526606 7674
a723baf1 7675 mem-initializer:
34cd5ae7 7676 ( expression-list [opt] )
a723baf1
MM
7677
7678 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7679 class) or FIELD_DECL (for a non-static data member) to initialize;
7680 the TREE_VALUE is the expression-list. */
7681
7682static tree
94edc4ab 7683cp_parser_mem_initializer (cp_parser* parser)
a723baf1
MM
7684{
7685 tree mem_initializer_id;
7686 tree expression_list;
1f5a253a 7687 tree member;
21526606 7688
a723baf1
MM
7689 /* Find out what is being initialized. */
7690 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7691 {
7692 pedwarn ("anachronistic old-style base class initializer");
7693 mem_initializer_id = NULL_TREE;
7694 }
7695 else
7696 mem_initializer_id = cp_parser_mem_initializer_id (parser);
1f5a253a
NS
7697 member = expand_member_init (mem_initializer_id);
7698 if (member && !DECL_P (member))
7699 in_base_initializer = 1;
7efa3e22 7700
21526606 7701 expression_list
39703eb9 7702 = cp_parser_parenthesized_expression_list (parser, false,
93678513 7703 /*cast_p=*/false,
39703eb9 7704 /*non_constant_p=*/NULL);
7efa3e22 7705 if (!expression_list)
a723baf1 7706 expression_list = void_type_node;
a723baf1 7707
1f5a253a 7708 in_base_initializer = 0;
21526606 7709
1f5a253a 7710 return member ? build_tree_list (member, expression_list) : NULL_TREE;
a723baf1
MM
7711}
7712
7713/* Parse a mem-initializer-id.
7714
7715 mem-initializer-id:
7716 :: [opt] nested-name-specifier [opt] class-name
21526606 7717 identifier
a723baf1
MM
7718
7719 Returns a TYPE indicating the class to be initializer for the first
7720 production. Returns an IDENTIFIER_NODE indicating the data member
7721 to be initialized for the second production. */
7722
7723static tree
94edc4ab 7724cp_parser_mem_initializer_id (cp_parser* parser)
a723baf1
MM
7725{
7726 bool global_scope_p;
7727 bool nested_name_specifier_p;
8a83a693 7728 bool template_p = false;
a723baf1
MM
7729 tree id;
7730
8a83a693
GB
7731 /* `typename' is not allowed in this context ([temp.res]). */
7732 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7733 {
2a13a625 7734 error ("keyword %<typename%> not allowed in this context (a qualified "
8a83a693
GB
7735 "member initializer is implicitly a type)");
7736 cp_lexer_consume_token (parser->lexer);
7737 }
a723baf1 7738 /* Look for the optional `::' operator. */
21526606
EC
7739 global_scope_p
7740 = (cp_parser_global_scope_opt (parser,
7741 /*current_scope_valid_p=*/false)
a723baf1
MM
7742 != NULL_TREE);
7743 /* Look for the optional nested-name-specifier. The simplest way to
7744 implement:
7745
7746 [temp.res]
7747
7748 The keyword `typename' is not permitted in a base-specifier or
7749 mem-initializer; in these contexts a qualified name that
7750 depends on a template-parameter is implicitly assumed to be a
7751 type name.
7752
7753 is to assume that we have seen the `typename' keyword at this
7754 point. */
21526606 7755 nested_name_specifier_p
a723baf1
MM
7756 = (cp_parser_nested_name_specifier_opt (parser,
7757 /*typename_keyword_p=*/true,
7758 /*check_dependency_p=*/true,
a668c6ad
MM
7759 /*type_p=*/true,
7760 /*is_declaration=*/true)
a723baf1 7761 != NULL_TREE);
8a83a693
GB
7762 if (nested_name_specifier_p)
7763 template_p = cp_parser_optional_template_keyword (parser);
a723baf1
MM
7764 /* If there is a `::' operator or a nested-name-specifier, then we
7765 are definitely looking for a class-name. */
7766 if (global_scope_p || nested_name_specifier_p)
7767 return cp_parser_class_name (parser,
7768 /*typename_keyword_p=*/true,
8a83a693 7769 /*template_keyword_p=*/template_p,
fc6a28d7 7770 none_type,
a723baf1 7771 /*check_dependency_p=*/true,
a668c6ad
MM
7772 /*class_head_p=*/false,
7773 /*is_declaration=*/true);
a723baf1
MM
7774 /* Otherwise, we could also be looking for an ordinary identifier. */
7775 cp_parser_parse_tentatively (parser);
7776 /* Try a class-name. */
21526606 7777 id = cp_parser_class_name (parser,
a723baf1
MM
7778 /*typename_keyword_p=*/true,
7779 /*template_keyword_p=*/false,
fc6a28d7 7780 none_type,
a723baf1 7781 /*check_dependency_p=*/true,
a668c6ad
MM
7782 /*class_head_p=*/false,
7783 /*is_declaration=*/true);
a723baf1
MM
7784 /* If we found one, we're done. */
7785 if (cp_parser_parse_definitely (parser))
7786 return id;
7787 /* Otherwise, look for an ordinary identifier. */
7788 return cp_parser_identifier (parser);
7789}
7790
7791/* Overloading [gram.over] */
7792
7793/* Parse an operator-function-id.
7794
7795 operator-function-id:
21526606 7796 operator operator
a723baf1
MM
7797
7798 Returns an IDENTIFIER_NODE for the operator which is a
7799 human-readable spelling of the identifier, e.g., `operator +'. */
7800
21526606 7801static tree
94edc4ab 7802cp_parser_operator_function_id (cp_parser* parser)
a723baf1
MM
7803{
7804 /* Look for the `operator' keyword. */
7805 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7806 return error_mark_node;
7807 /* And then the name of the operator itself. */
7808 return cp_parser_operator (parser);
7809}
7810
7811/* Parse an operator.
7812
7813 operator:
7814 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7815 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7816 || ++ -- , ->* -> () []
7817
7818 GNU Extensions:
21526606 7819
a723baf1
MM
7820 operator:
7821 <? >? <?= >?=
7822
7823 Returns an IDENTIFIER_NODE for the operator which is a
7824 human-readable spelling of the identifier, e.g., `operator +'. */
21526606 7825
a723baf1 7826static tree
94edc4ab 7827cp_parser_operator (cp_parser* parser)
a723baf1
MM
7828{
7829 tree id = NULL_TREE;
7830 cp_token *token;
7831
7832 /* Peek at the next token. */
7833 token = cp_lexer_peek_token (parser->lexer);
7834 /* Figure out which operator we have. */
7835 switch (token->type)
7836 {
7837 case CPP_KEYWORD:
7838 {
7839 enum tree_code op;
7840
7841 /* The keyword should be either `new' or `delete'. */
7842 if (token->keyword == RID_NEW)
7843 op = NEW_EXPR;
7844 else if (token->keyword == RID_DELETE)
7845 op = DELETE_EXPR;
7846 else
7847 break;
7848
7849 /* Consume the `new' or `delete' token. */
7850 cp_lexer_consume_token (parser->lexer);
7851
7852 /* Peek at the next token. */
7853 token = cp_lexer_peek_token (parser->lexer);
7854 /* If it's a `[' token then this is the array variant of the
7855 operator. */
7856 if (token->type == CPP_OPEN_SQUARE)
7857 {
7858 /* Consume the `[' token. */
7859 cp_lexer_consume_token (parser->lexer);
7860 /* Look for the `]' token. */
7861 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
21526606 7862 id = ansi_opname (op == NEW_EXPR
a723baf1
MM
7863 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7864 }
7865 /* Otherwise, we have the non-array variant. */
7866 else
7867 id = ansi_opname (op);
7868
7869 return id;
7870 }
7871
7872 case CPP_PLUS:
7873 id = ansi_opname (PLUS_EXPR);
7874 break;
7875
7876 case CPP_MINUS:
7877 id = ansi_opname (MINUS_EXPR);
7878 break;
7879
7880 case CPP_MULT:
7881 id = ansi_opname (MULT_EXPR);
7882 break;
7883
7884 case CPP_DIV:
7885 id = ansi_opname (TRUNC_DIV_EXPR);
7886 break;
7887
7888 case CPP_MOD:
7889 id = ansi_opname (TRUNC_MOD_EXPR);
7890 break;
7891
7892 case CPP_XOR:
7893 id = ansi_opname (BIT_XOR_EXPR);
7894 break;
7895
7896 case CPP_AND:
7897 id = ansi_opname (BIT_AND_EXPR);
7898 break;
7899
7900 case CPP_OR:
7901 id = ansi_opname (BIT_IOR_EXPR);
7902 break;
7903
7904 case CPP_COMPL:
7905 id = ansi_opname (BIT_NOT_EXPR);
7906 break;
21526606 7907
a723baf1
MM
7908 case CPP_NOT:
7909 id = ansi_opname (TRUTH_NOT_EXPR);
7910 break;
7911
7912 case CPP_EQ:
7913 id = ansi_assopname (NOP_EXPR);
7914 break;
7915
7916 case CPP_LESS:
7917 id = ansi_opname (LT_EXPR);
7918 break;
7919
7920 case CPP_GREATER:
7921 id = ansi_opname (GT_EXPR);
7922 break;
7923
7924 case CPP_PLUS_EQ:
7925 id = ansi_assopname (PLUS_EXPR);
7926 break;
7927
7928 case CPP_MINUS_EQ:
7929 id = ansi_assopname (MINUS_EXPR);
7930 break;
7931
7932 case CPP_MULT_EQ:
7933 id = ansi_assopname (MULT_EXPR);
7934 break;
7935
7936 case CPP_DIV_EQ:
7937 id = ansi_assopname (TRUNC_DIV_EXPR);
7938 break;
7939
7940 case CPP_MOD_EQ:
7941 id = ansi_assopname (TRUNC_MOD_EXPR);
7942 break;
7943
7944 case CPP_XOR_EQ:
7945 id = ansi_assopname (BIT_XOR_EXPR);
7946 break;
7947
7948 case CPP_AND_EQ:
7949 id = ansi_assopname (BIT_AND_EXPR);
7950 break;
7951
7952 case CPP_OR_EQ:
7953 id = ansi_assopname (BIT_IOR_EXPR);
7954 break;
7955
7956 case CPP_LSHIFT:
7957 id = ansi_opname (LSHIFT_EXPR);
7958 break;
7959
7960 case CPP_RSHIFT:
7961 id = ansi_opname (RSHIFT_EXPR);
7962 break;
7963
7964 case CPP_LSHIFT_EQ:
7965 id = ansi_assopname (LSHIFT_EXPR);
7966 break;
7967
7968 case CPP_RSHIFT_EQ:
7969 id = ansi_assopname (RSHIFT_EXPR);
7970 break;
7971
7972 case CPP_EQ_EQ:
7973 id = ansi_opname (EQ_EXPR);
7974 break;
7975
7976 case CPP_NOT_EQ:
7977 id = ansi_opname (NE_EXPR);
7978 break;
7979
7980 case CPP_LESS_EQ:
7981 id = ansi_opname (LE_EXPR);
7982 break;
7983
7984 case CPP_GREATER_EQ:
7985 id = ansi_opname (GE_EXPR);
7986 break;
7987
7988 case CPP_AND_AND:
7989 id = ansi_opname (TRUTH_ANDIF_EXPR);
7990 break;
7991
7992 case CPP_OR_OR:
7993 id = ansi_opname (TRUTH_ORIF_EXPR);
7994 break;
21526606 7995
a723baf1
MM
7996 case CPP_PLUS_PLUS:
7997 id = ansi_opname (POSTINCREMENT_EXPR);
7998 break;
7999
8000 case CPP_MINUS_MINUS:
8001 id = ansi_opname (PREDECREMENT_EXPR);
8002 break;
8003
8004 case CPP_COMMA:
8005 id = ansi_opname (COMPOUND_EXPR);
8006 break;
8007
8008 case CPP_DEREF_STAR:
8009 id = ansi_opname (MEMBER_REF);
8010 break;
8011
8012 case CPP_DEREF:
8013 id = ansi_opname (COMPONENT_REF);
8014 break;
8015
8016 case CPP_OPEN_PAREN:
8017 /* Consume the `('. */
8018 cp_lexer_consume_token (parser->lexer);
8019 /* Look for the matching `)'. */
8020 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8021 return ansi_opname (CALL_EXPR);
8022
8023 case CPP_OPEN_SQUARE:
8024 /* Consume the `['. */
8025 cp_lexer_consume_token (parser->lexer);
8026 /* Look for the matching `]'. */
8027 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8028 return ansi_opname (ARRAY_REF);
8029
8030 /* Extensions. */
8031 case CPP_MIN:
8032 id = ansi_opname (MIN_EXPR);
8033 break;
8034
8035 case CPP_MAX:
8036 id = ansi_opname (MAX_EXPR);
8037 break;
8038
8039 case CPP_MIN_EQ:
8040 id = ansi_assopname (MIN_EXPR);
8041 break;
8042
8043 case CPP_MAX_EQ:
8044 id = ansi_assopname (MAX_EXPR);
8045 break;
8046
8047 default:
8048 /* Anything else is an error. */
8049 break;
8050 }
8051
8052 /* If we have selected an identifier, we need to consume the
8053 operator token. */
8054 if (id)
8055 cp_lexer_consume_token (parser->lexer);
8056 /* Otherwise, no valid operator name was present. */
8057 else
8058 {
8059 cp_parser_error (parser, "expected operator");
8060 id = error_mark_node;
8061 }
8062
8063 return id;
8064}
8065
8066/* Parse a template-declaration.
8067
8068 template-declaration:
21526606 8069 export [opt] template < template-parameter-list > declaration
a723baf1
MM
8070
8071 If MEMBER_P is TRUE, this template-declaration occurs within a
21526606 8072 class-specifier.
a723baf1
MM
8073
8074 The grammar rule given by the standard isn't correct. What
8075 is really meant is:
8076
8077 template-declaration:
21526606 8078 export [opt] template-parameter-list-seq
a723baf1 8079 decl-specifier-seq [opt] init-declarator [opt] ;
21526606 8080 export [opt] template-parameter-list-seq
a723baf1
MM
8081 function-definition
8082
8083 template-parameter-list-seq:
8084 template-parameter-list-seq [opt]
8085 template < template-parameter-list > */
8086
8087static void
94edc4ab 8088cp_parser_template_declaration (cp_parser* parser, bool member_p)
a723baf1
MM
8089{
8090 /* Check for `export'. */
8091 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8092 {
8093 /* Consume the `export' token. */
8094 cp_lexer_consume_token (parser->lexer);
8095 /* Warn that we do not support `export'. */
2a13a625 8096 warning ("keyword %<export%> not implemented, and will be ignored");
a723baf1
MM
8097 }
8098
8099 cp_parser_template_declaration_after_export (parser, member_p);
8100}
8101
8102/* Parse a template-parameter-list.
8103
8104 template-parameter-list:
8105 template-parameter
8106 template-parameter-list , template-parameter
8107
8108 Returns a TREE_LIST. Each node represents a template parameter.
8109 The nodes are connected via their TREE_CHAINs. */
8110
8111static tree
94edc4ab 8112cp_parser_template_parameter_list (cp_parser* parser)
a723baf1
MM
8113{
8114 tree parameter_list = NULL_TREE;
8115
8116 while (true)
8117 {
8118 tree parameter;
8119 cp_token *token;
058b15c1 8120 bool is_non_type;
a723baf1
MM
8121
8122 /* Parse the template-parameter. */
058b15c1 8123 parameter = cp_parser_template_parameter (parser, &is_non_type);
a723baf1 8124 /* Add it to the list. */
943e3ede
MM
8125 if (parameter != error_mark_node)
8126 parameter_list = process_template_parm (parameter_list,
8127 parameter,
8128 is_non_type);
a723baf1
MM
8129 /* Peek at the next token. */
8130 token = cp_lexer_peek_token (parser->lexer);
8131 /* If it's not a `,', we're done. */
8132 if (token->type != CPP_COMMA)
8133 break;
8134 /* Otherwise, consume the `,' token. */
8135 cp_lexer_consume_token (parser->lexer);
8136 }
8137
8138 return parameter_list;
8139}
8140
8141/* Parse a template-parameter.
8142
8143 template-parameter:
8144 type-parameter
8145 parameter-declaration
8146
943e3ede
MM
8147 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8148 the parameter. The TREE_PURPOSE is the default value, if any.
8149 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8150 iff this parameter is a non-type parameter. */
a723baf1
MM
8151
8152static tree
058b15c1 8153cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
a723baf1
MM
8154{
8155 cp_token *token;
62d1db17 8156 cp_parameter_declarator *parameter_declarator;
943e3ede 8157 tree parm;
a723baf1 8158
058b15c1
MM
8159 /* Assume it is a type parameter or a template parameter. */
8160 *is_non_type = false;
a723baf1
MM
8161 /* Peek at the next token. */
8162 token = cp_lexer_peek_token (parser->lexer);
8163 /* If it is `class' or `template', we have a type-parameter. */
8164 if (token->keyword == RID_TEMPLATE)
8165 return cp_parser_type_parameter (parser);
8166 /* If it is `class' or `typename' we do not know yet whether it is a
8167 type parameter or a non-type parameter. Consider:
8168
8169 template <typename T, typename T::X X> ...
8170
8171 or:
21526606 8172
a723baf1
MM
8173 template <class C, class D*> ...
8174
8175 Here, the first parameter is a type parameter, and the second is
8176 a non-type parameter. We can tell by looking at the token after
8177 the identifier -- if it is a `,', `=', or `>' then we have a type
8178 parameter. */
8179 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8180 {
8181 /* Peek at the token after `class' or `typename'. */
8182 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8183 /* If it's an identifier, skip it. */
8184 if (token->type == CPP_NAME)
8185 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8186 /* Now, see if the token looks like the end of a template
8187 parameter. */
21526606 8188 if (token->type == CPP_COMMA
a723baf1
MM
8189 || token->type == CPP_EQ
8190 || token->type == CPP_GREATER)
8191 return cp_parser_type_parameter (parser);
8192 }
8193
21526606 8194 /* Otherwise, it is a non-type parameter.
a723baf1
MM
8195
8196 [temp.param]
8197
8198 When parsing a default template-argument for a non-type
8199 template-parameter, the first non-nested `>' is taken as the end
8200 of the template parameter-list rather than a greater-than
8201 operator. */
058b15c1
MM
8202 *is_non_type = true;
8203 parameter_declarator
8204 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8205 /*parenthesized_p=*/NULL);
943e3ede
MM
8206 parm = grokdeclarator (parameter_declarator->declarator,
8207 &parameter_declarator->decl_specifiers,
8208 PARM, /*initialized=*/0,
8209 /*attrlist=*/NULL);
8210 if (parm == error_mark_node)
8211 return error_mark_node;
8212 return build_tree_list (parameter_declarator->default_argument, parm);
a723baf1
MM
8213}
8214
8215/* Parse a type-parameter.
8216
8217 type-parameter:
8218 class identifier [opt]
8219 class identifier [opt] = type-id
8220 typename identifier [opt]
8221 typename identifier [opt] = type-id
8222 template < template-parameter-list > class identifier [opt]
21526606
EC
8223 template < template-parameter-list > class identifier [opt]
8224 = id-expression
a723baf1
MM
8225
8226 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8227 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8228 the declaration of the parameter. */
8229
8230static tree
94edc4ab 8231cp_parser_type_parameter (cp_parser* parser)
a723baf1
MM
8232{
8233 cp_token *token;
8234 tree parameter;
8235
8236 /* Look for a keyword to tell us what kind of parameter this is. */
21526606 8237 token = cp_parser_require (parser, CPP_KEYWORD,
8a6393df 8238 "`class', `typename', or `template'");
a723baf1
MM
8239 if (!token)
8240 return error_mark_node;
8241
8242 switch (token->keyword)
8243 {
8244 case RID_CLASS:
8245 case RID_TYPENAME:
8246 {
8247 tree identifier;
8248 tree default_argument;
8249
8250 /* If the next token is an identifier, then it names the
8251 parameter. */
8252 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8253 identifier = cp_parser_identifier (parser);
8254 else
8255 identifier = NULL_TREE;
8256
8257 /* Create the parameter. */
8258 parameter = finish_template_type_parm (class_type_node, identifier);
8259
8260 /* If the next token is an `=', we have a default argument. */
8261 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8262 {
8263 /* Consume the `=' token. */
8264 cp_lexer_consume_token (parser->lexer);
34cd5ae7 8265 /* Parse the default-argument. */
a723baf1
MM
8266 default_argument = cp_parser_type_id (parser);
8267 }
8268 else
8269 default_argument = NULL_TREE;
8270
8271 /* Create the combined representation of the parameter and the
8272 default argument. */
c67d36d0 8273 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
8274 }
8275 break;
8276
8277 case RID_TEMPLATE:
8278 {
8279 tree parameter_list;
8280 tree identifier;
8281 tree default_argument;
8282
8283 /* Look for the `<'. */
8284 cp_parser_require (parser, CPP_LESS, "`<'");
8285 /* Parse the template-parameter-list. */
8286 begin_template_parm_list ();
21526606 8287 parameter_list
a723baf1
MM
8288 = cp_parser_template_parameter_list (parser);
8289 parameter_list = end_template_parm_list (parameter_list);
8290 /* Look for the `>'. */
8291 cp_parser_require (parser, CPP_GREATER, "`>'");
8292 /* Look for the `class' keyword. */
8293 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8294 /* If the next token is an `=', then there is a
8295 default-argument. If the next token is a `>', we are at
8296 the end of the parameter-list. If the next token is a `,',
8297 then we are at the end of this parameter. */
8298 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8299 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8300 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
71bd7186
MM
8301 {
8302 identifier = cp_parser_identifier (parser);
03fd3f84 8303 /* Treat invalid names as if the parameter were nameless. */
71bd7186
MM
8304 if (identifier == error_mark_node)
8305 identifier = NULL_TREE;
8306 }
a723baf1
MM
8307 else
8308 identifier = NULL_TREE;
71bd7186 8309
a723baf1
MM
8310 /* Create the template parameter. */
8311 parameter = finish_template_template_parm (class_type_node,
8312 identifier);
21526606 8313
a723baf1
MM
8314 /* If the next token is an `=', then there is a
8315 default-argument. */
8316 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8317 {
b0bc6e8e
KL
8318 bool is_template;
8319
a723baf1
MM
8320 /* Consume the `='. */
8321 cp_lexer_consume_token (parser->lexer);
8322 /* Parse the id-expression. */
21526606 8323 default_argument
a723baf1
MM
8324 = cp_parser_id_expression (parser,
8325 /*template_keyword_p=*/false,
8326 /*check_dependency_p=*/true,
b0bc6e8e 8327 /*template_p=*/&is_template,
f3c2dfc6 8328 /*declarator_p=*/false);
a3a503a5
GB
8329 if (TREE_CODE (default_argument) == TYPE_DECL)
8330 /* If the id-expression was a template-id that refers to
8331 a template-class, we already have the declaration here,
8332 so no further lookup is needed. */
8333 ;
8334 else
8335 /* Look up the name. */
21526606 8336 default_argument
a3a503a5 8337 = cp_parser_lookup_name (parser, default_argument,
fc6a28d7
MM
8338 none_type,
8339 /*is_template=*/is_template,
8340 /*is_namespace=*/false,
8341 /*check_dependency=*/true,
8342 /*ambiguous_p=*/NULL);
a723baf1
MM
8343 /* See if the default argument is valid. */
8344 default_argument
8345 = check_template_template_default_arg (default_argument);
8346 }
8347 else
8348 default_argument = NULL_TREE;
8349
8350 /* Create the combined representation of the parameter and the
8351 default argument. */
71bd7186 8352 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
8353 }
8354 break;
8355
8356 default:
71bd7186
MM
8357 gcc_unreachable ();
8358 break;
a723baf1 8359 }
21526606 8360
a723baf1
MM
8361 return parameter;
8362}
8363
8364/* Parse a template-id.
8365
8366 template-id:
8367 template-name < template-argument-list [opt] >
8368
8369 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8370 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8371 returned. Otherwise, if the template-name names a function, or set
8372 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
21526606 8373 names a class, returns a TYPE_DECL for the specialization.
a723baf1
MM
8374
8375 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8376 uninstantiated templates. */
8377
8378static tree
21526606
EC
8379cp_parser_template_id (cp_parser *parser,
8380 bool template_keyword_p,
a668c6ad
MM
8381 bool check_dependency_p,
8382 bool is_declaration)
a723baf1
MM
8383{
8384 tree template;
8385 tree arguments;
a723baf1 8386 tree template_id;
0c5e4866 8387 cp_token_position start_of_id = 0;
a723baf1 8388 tree access_check = NULL_TREE;
f4abade9 8389 cp_token *next_token, *next_token_2;
a668c6ad 8390 bool is_identifier;
a723baf1
MM
8391
8392 /* If the next token corresponds to a template-id, there is no need
8393 to reparse it. */
2050a1bb
MM
8394 next_token = cp_lexer_peek_token (parser->lexer);
8395 if (next_token->type == CPP_TEMPLATE_ID)
a723baf1
MM
8396 {
8397 tree value;
8398 tree check;
8399
8400 /* Get the stored value. */
8401 value = cp_lexer_consume_token (parser->lexer)->value;
8402 /* Perform any access checks that were deferred. */
8403 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c
KL
8404 perform_or_defer_access_check (TREE_PURPOSE (check),
8405 TREE_VALUE (check));
a723baf1
MM
8406 /* Return the stored value. */
8407 return TREE_VALUE (value);
8408 }
8409
2050a1bb
MM
8410 /* Avoid performing name lookup if there is no possibility of
8411 finding a template-id. */
8412 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8413 || (next_token->type == CPP_NAME
21526606 8414 && !cp_parser_nth_token_starts_template_argument_list_p
f4abade9 8415 (parser, 2)))
2050a1bb
MM
8416 {
8417 cp_parser_error (parser, "expected template-id");
8418 return error_mark_node;
8419 }
8420
a723baf1 8421 /* Remember where the template-id starts. */
0b16f8f4 8422 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
0c5e4866 8423 start_of_id = cp_lexer_token_position (parser->lexer, false);
a723baf1 8424
8d241e0b 8425 push_deferring_access_checks (dk_deferred);
cf22909c 8426
a723baf1 8427 /* Parse the template-name. */
a668c6ad 8428 is_identifier = false;
a723baf1 8429 template = cp_parser_template_name (parser, template_keyword_p,
a668c6ad
MM
8430 check_dependency_p,
8431 is_declaration,
8432 &is_identifier);
8433 if (template == error_mark_node || is_identifier)
cf22909c
KL
8434 {
8435 pop_deferring_access_checks ();
a668c6ad 8436 return template;
cf22909c 8437 }
a723baf1 8438
21526606 8439 /* If we find the sequence `[:' after a template-name, it's probably
f4abade9
GB
8440 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8441 parse correctly the argument list. */
2cfe82fe 8442 next_token = cp_lexer_peek_token (parser->lexer);
f4abade9 8443 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
21526606 8444 if (next_token->type == CPP_OPEN_SQUARE
f4abade9 8445 && next_token->flags & DIGRAPH
21526606 8446 && next_token_2->type == CPP_COLON
f4abade9 8447 && !(next_token_2->flags & PREV_WHITE))
cf22909c 8448 {
f4abade9
GB
8449 cp_parser_parse_tentatively (parser);
8450 /* Change `:' into `::'. */
8451 next_token_2->type = CPP_SCOPE;
8452 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8453 CPP_LESS. */
8454 cp_lexer_consume_token (parser->lexer);
8455 /* Parse the arguments. */
8456 arguments = cp_parser_enclosed_template_argument_list (parser);
8457 if (!cp_parser_parse_definitely (parser))
8458 {
8459 /* If we couldn't parse an argument list, then we revert our changes
8460 and return simply an error. Maybe this is not a template-id
8461 after all. */
8462 next_token_2->type = CPP_COLON;
2a13a625 8463 cp_parser_error (parser, "expected %<<%>");
f4abade9
GB
8464 pop_deferring_access_checks ();
8465 return error_mark_node;
8466 }
8467 /* Otherwise, emit an error about the invalid digraph, but continue
8468 parsing because we got our argument list. */
2a13a625
GDR
8469 pedwarn ("%<<::%> cannot begin a template-argument list");
8470 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8471 "between %<<%> and %<::%>");
f4abade9
GB
8472 if (!flag_permissive)
8473 {
8474 static bool hint;
8475 if (!hint)
8476 {
2a13a625 8477 inform ("(if you use -fpermissive G++ will accept your code)");
f4abade9
GB
8478 hint = true;
8479 }
8480 }
8481 }
8482 else
8483 {
8484 /* Look for the `<' that starts the template-argument-list. */
8485 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8486 {
8487 pop_deferring_access_checks ();
8488 return error_mark_node;
8489 }
8490 /* Parse the arguments. */
8491 arguments = cp_parser_enclosed_template_argument_list (parser);
cf22909c 8492 }
a723baf1
MM
8493
8494 /* Build a representation of the specialization. */
8495 if (TREE_CODE (template) == IDENTIFIER_NODE)
8496 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8497 else if (DECL_CLASS_TEMPLATE_P (template)
8498 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
21526606
EC
8499 template_id
8500 = finish_template_type (template, arguments,
8501 cp_lexer_next_token_is (parser->lexer,
a723baf1
MM
8502 CPP_SCOPE));
8503 else
8504 {
8505 /* If it's not a class-template or a template-template, it should be
8506 a function-template. */
50bc768d
NS
8507 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8508 || TREE_CODE (template) == OVERLOAD
8509 || BASELINK_P (template)));
21526606 8510
a723baf1
MM
8511 template_id = lookup_template_function (template, arguments);
8512 }
21526606 8513
cf22909c
KL
8514 /* Retrieve any deferred checks. Do not pop this access checks yet
8515 so the memory will not be reclaimed during token replacing below. */
8516 access_check = get_deferred_access_checks ();
8517
a723baf1
MM
8518 /* If parsing tentatively, replace the sequence of tokens that makes
8519 up the template-id with a CPP_TEMPLATE_ID token. That way,
8520 should we re-parse the token stream, we will not have to repeat
8521 the effort required to do the parse, nor will we issue duplicate
8522 error messages about problems during instantiation of the
e894ab29 8523 template. */
c8a7ed43 8524 if (start_of_id)
a723baf1 8525 {
0c5e4866
NS
8526 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8527
a723baf1
MM
8528 /* Reset the contents of the START_OF_ID token. */
8529 token->type = CPP_TEMPLATE_ID;
8530 token->value = build_tree_list (access_check, template_id);
8531 token->keyword = RID_MAX;
0c5e4866 8532
a723baf1 8533 /* Purge all subsequent tokens. */
0c5e4866 8534 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
c8a7ed43
AO
8535
8536 /* ??? Can we actually assume that, if template_id ==
8537 error_mark_node, we will have issued a diagnostic to the
8538 user, as opposed to simply marking the tentative parse as
8539 failed? */
8540 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8541 error ("parse error in template argument list");
a723baf1
MM
8542 }
8543
cf22909c 8544 pop_deferring_access_checks ();
a723baf1
MM
8545 return template_id;
8546}
8547
8548/* Parse a template-name.
8549
8550 template-name:
8551 identifier
21526606 8552
a723baf1
MM
8553 The standard should actually say:
8554
8555 template-name:
8556 identifier
8557 operator-function-id
a723baf1
MM
8558
8559 A defect report has been filed about this issue.
8560
0d956474
GB
8561 A conversion-function-id cannot be a template name because they cannot
8562 be part of a template-id. In fact, looking at this code:
8563
8564 a.operator K<int>()
8565
8566 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
21526606 8567 It is impossible to call a templated conversion-function-id with an
0d956474
GB
8568 explicit argument list, since the only allowed template parameter is
8569 the type to which it is converting.
8570
a723baf1
MM
8571 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8572 `template' keyword, in a construction like:
8573
8574 T::template f<3>()
8575
8576 In that case `f' is taken to be a template-name, even though there
8577 is no way of knowing for sure.
8578
8579 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8580 name refers to a set of overloaded functions, at least one of which
8581 is a template, or an IDENTIFIER_NODE with the name of the template,
8582 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8583 names are looked up inside uninstantiated templates. */
8584
8585static tree
21526606
EC
8586cp_parser_template_name (cp_parser* parser,
8587 bool template_keyword_p,
a668c6ad
MM
8588 bool check_dependency_p,
8589 bool is_declaration,
8590 bool *is_identifier)
a723baf1
MM
8591{
8592 tree identifier;
8593 tree decl;
8594 tree fns;
8595
8596 /* If the next token is `operator', then we have either an
8597 operator-function-id or a conversion-function-id. */
8598 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8599 {
8600 /* We don't know whether we're looking at an
8601 operator-function-id or a conversion-function-id. */
8602 cp_parser_parse_tentatively (parser);
8603 /* Try an operator-function-id. */
8604 identifier = cp_parser_operator_function_id (parser);
8605 /* If that didn't work, try a conversion-function-id. */
8606 if (!cp_parser_parse_definitely (parser))
0d956474
GB
8607 {
8608 cp_parser_error (parser, "expected template-name");
8609 return error_mark_node;
8610 }
a723baf1
MM
8611 }
8612 /* Look for the identifier. */
8613 else
8614 identifier = cp_parser_identifier (parser);
21526606 8615
a723baf1
MM
8616 /* If we didn't find an identifier, we don't have a template-id. */
8617 if (identifier == error_mark_node)
8618 return error_mark_node;
8619
8620 /* If the name immediately followed the `template' keyword, then it
8621 is a template-name. However, if the next token is not `<', then
8622 we do not treat it as a template-name, since it is not being used
8623 as part of a template-id. This enables us to handle constructs
8624 like:
8625
8626 template <typename T> struct S { S(); };
8627 template <typename T> S<T>::S();
8628
8629 correctly. We would treat `S' as a template -- if it were `S<T>'
8630 -- but we do not if there is no `<'. */
a668c6ad
MM
8631
8632 if (processing_template_decl
f4abade9 8633 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
a668c6ad
MM
8634 {
8635 /* In a declaration, in a dependent context, we pretend that the
8636 "template" keyword was present in order to improve error
8637 recovery. For example, given:
21526606 8638
a668c6ad 8639 template <typename T> void f(T::X<int>);
21526606 8640
a668c6ad 8641 we want to treat "X<int>" as a template-id. */
21526606
EC
8642 if (is_declaration
8643 && !template_keyword_p
a668c6ad 8644 && parser->scope && TYPE_P (parser->scope)
a52eb3bc 8645 && check_dependency_p
4e0f4df5
GB
8646 && dependent_type_p (parser->scope)
8647 /* Do not do this for dtors (or ctors), since they never
8648 need the template keyword before their name. */
8649 && !constructor_name_p (identifier, parser->scope))
a668c6ad 8650 {
0c5e4866
NS
8651 cp_token_position start = 0;
8652
a668c6ad 8653 /* Explain what went wrong. */
2a13a625
GDR
8654 error ("non-template %qD used as template", identifier);
8655 inform ("use %<%T::template %D%> to indicate that it is a template",
4e0f4df5 8656 parser->scope, identifier);
0b16f8f4
VR
8657 /* If parsing tentatively, find the location of the "<" token. */
8658 if (cp_parser_simulate_error (parser))
8659 start = cp_lexer_token_position (parser->lexer, true);
a668c6ad
MM
8660 /* Parse the template arguments so that we can issue error
8661 messages about them. */
8662 cp_lexer_consume_token (parser->lexer);
8663 cp_parser_enclosed_template_argument_list (parser);
8664 /* Skip tokens until we find a good place from which to
8665 continue parsing. */
8666 cp_parser_skip_to_closing_parenthesis (parser,
8667 /*recovering=*/true,
8668 /*or_comma=*/true,
8669 /*consume_paren=*/false);
8670 /* If parsing tentatively, permanently remove the
8671 template argument list. That will prevent duplicate
8672 error messages from being issued about the missing
8673 "template" keyword. */
0c5e4866
NS
8674 if (start)
8675 cp_lexer_purge_tokens_after (parser->lexer, start);
a668c6ad
MM
8676 if (is_identifier)
8677 *is_identifier = true;
8678 return identifier;
8679 }
9d363a56
MM
8680
8681 /* If the "template" keyword is present, then there is generally
8682 no point in doing name-lookup, so we just return IDENTIFIER.
8683 But, if the qualifying scope is non-dependent then we can
8684 (and must) do name-lookup normally. */
8685 if (template_keyword_p
8686 && (!parser->scope
98ca843c 8687 || (TYPE_P (parser->scope)
9d363a56 8688 && dependent_type_p (parser->scope))))
a668c6ad
MM
8689 return identifier;
8690 }
a723baf1
MM
8691
8692 /* Look up the name. */
8693 decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 8694 none_type,
b0bc6e8e 8695 /*is_template=*/false,
eea9800f 8696 /*is_namespace=*/false,
8f78f01f
MM
8697 check_dependency_p,
8698 /*ambiguous_p=*/NULL);
a723baf1
MM
8699 decl = maybe_get_template_decl_from_type_decl (decl);
8700
8701 /* If DECL is a template, then the name was a template-name. */
8702 if (TREE_CODE (decl) == TEMPLATE_DECL)
8703 ;
21526606 8704 else
a723baf1
MM
8705 {
8706 /* The standard does not explicitly indicate whether a name that
8707 names a set of overloaded declarations, some of which are
8708 templates, is a template-name. However, such a name should
8709 be a template-name; otherwise, there is no way to form a
8710 template-id for the overloaded templates. */
8711 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8712 if (TREE_CODE (fns) == OVERLOAD)
8713 {
8714 tree fn;
21526606 8715
a723baf1
MM
8716 for (fn = fns; fn; fn = OVL_NEXT (fn))
8717 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8718 break;
8719 }
8720 else
8721 {
8722 /* Otherwise, the name does not name a template. */
8723 cp_parser_error (parser, "expected template-name");
8724 return error_mark_node;
8725 }
8726 }
8727
8728 /* If DECL is dependent, and refers to a function, then just return
8729 its name; we will look it up again during template instantiation. */
8730 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8731 {
8732 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
1fb3244a 8733 if (TYPE_P (scope) && dependent_type_p (scope))
a723baf1
MM
8734 return identifier;
8735 }
8736
8737 return decl;
8738}
8739
8740/* Parse a template-argument-list.
8741
8742 template-argument-list:
8743 template-argument
8744 template-argument-list , template-argument
8745
04c06002 8746 Returns a TREE_VEC containing the arguments. */
a723baf1
MM
8747
8748static tree
94edc4ab 8749cp_parser_template_argument_list (cp_parser* parser)
a723baf1 8750{
bf12d54d
NS
8751 tree fixed_args[10];
8752 unsigned n_args = 0;
8753 unsigned alloced = 10;
8754 tree *arg_ary = fixed_args;
8755 tree vec;
4bb8ca28 8756 bool saved_in_template_argument_list_p;
a723baf1 8757
4bb8ca28
MM
8758 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8759 parser->in_template_argument_list_p = true;
bf12d54d 8760 do
a723baf1
MM
8761 {
8762 tree argument;
8763
bf12d54d 8764 if (n_args)
04c06002 8765 /* Consume the comma. */
bf12d54d 8766 cp_lexer_consume_token (parser->lexer);
21526606 8767
a723baf1
MM
8768 /* Parse the template-argument. */
8769 argument = cp_parser_template_argument (parser);
bf12d54d
NS
8770 if (n_args == alloced)
8771 {
8772 alloced *= 2;
21526606 8773
bf12d54d
NS
8774 if (arg_ary == fixed_args)
8775 {
8776 arg_ary = xmalloc (sizeof (tree) * alloced);
8777 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8778 }
8779 else
8780 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8781 }
8782 arg_ary[n_args++] = argument;
a723baf1 8783 }
bf12d54d
NS
8784 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8785
8786 vec = make_tree_vec (n_args);
a723baf1 8787
bf12d54d
NS
8788 while (n_args--)
8789 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
21526606 8790
bf12d54d
NS
8791 if (arg_ary != fixed_args)
8792 free (arg_ary);
4bb8ca28 8793 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
bf12d54d 8794 return vec;
a723baf1
MM
8795}
8796
8797/* Parse a template-argument.
8798
8799 template-argument:
8800 assignment-expression
8801 type-id
8802 id-expression
8803
8804 The representation is that of an assignment-expression, type-id, or
8805 id-expression -- except that the qualified id-expression is
8806 evaluated, so that the value returned is either a DECL or an
21526606 8807 OVERLOAD.
d17811fd
MM
8808
8809 Although the standard says "assignment-expression", it forbids
8810 throw-expressions or assignments in the template argument.
8811 Therefore, we use "conditional-expression" instead. */
a723baf1
MM
8812
8813static tree
94edc4ab 8814cp_parser_template_argument (cp_parser* parser)
a723baf1
MM
8815{
8816 tree argument;
8817 bool template_p;
d17811fd 8818 bool address_p;
4d5297fa 8819 bool maybe_type_id = false;
d17811fd 8820 cp_token *token;
b3445994 8821 cp_id_kind idk;
d17811fd 8822 tree qualifying_class;
a723baf1
MM
8823
8824 /* There's really no way to know what we're looking at, so we just
21526606 8825 try each alternative in order.
a723baf1
MM
8826
8827 [temp.arg]
8828
8829 In a template-argument, an ambiguity between a type-id and an
8830 expression is resolved to a type-id, regardless of the form of
21526606 8831 the corresponding template-parameter.
a723baf1
MM
8832
8833 Therefore, we try a type-id first. */
8834 cp_parser_parse_tentatively (parser);
a723baf1 8835 argument = cp_parser_type_id (parser);
4d5297fa 8836 /* If there was no error parsing the type-id but the next token is a '>>',
21526606 8837 we probably found a typo for '> >'. But there are type-id which are
4d5297fa
GB
8838 also valid expressions. For instance:
8839
8840 struct X { int operator >> (int); };
8841 template <int V> struct Foo {};
8842 Foo<X () >> 5> r;
8843
8844 Here 'X()' is a valid type-id of a function type, but the user just
8845 wanted to write the expression "X() >> 5". Thus, we remember that we
8846 found a valid type-id, but we still try to parse the argument as an
8847 expression to see what happens. */
8848 if (!cp_parser_error_occurred (parser)
8849 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8850 {
8851 maybe_type_id = true;
8852 cp_parser_abort_tentative_parse (parser);
8853 }
8854 else
8855 {
8856 /* If the next token isn't a `,' or a `>', then this argument wasn't
8857 really finished. This means that the argument is not a valid
8858 type-id. */
8859 if (!cp_parser_next_token_ends_template_argument_p (parser))
8860 cp_parser_error (parser, "expected template-argument");
8861 /* If that worked, we're done. */
8862 if (cp_parser_parse_definitely (parser))
8863 return argument;
8864 }
a723baf1
MM
8865 /* We're still not sure what the argument will be. */
8866 cp_parser_parse_tentatively (parser);
8867 /* Try a template. */
21526606 8868 argument = cp_parser_id_expression (parser,
a723baf1
MM
8869 /*template_keyword_p=*/false,
8870 /*check_dependency_p=*/true,
f3c2dfc6
MM
8871 &template_p,
8872 /*declarator_p=*/false);
a723baf1
MM
8873 /* If the next token isn't a `,' or a `>', then this argument wasn't
8874 really finished. */
d17811fd 8875 if (!cp_parser_next_token_ends_template_argument_p (parser))
a723baf1
MM
8876 cp_parser_error (parser, "expected template-argument");
8877 if (!cp_parser_error_occurred (parser))
8878 {
f746161e
MM
8879 /* Figure out what is being referred to. If the id-expression
8880 was for a class template specialization, then we will have a
8881 TYPE_DECL at this point. There is no need to do name lookup
8882 at this point in that case. */
8883 if (TREE_CODE (argument) != TYPE_DECL)
8884 argument = cp_parser_lookup_name (parser, argument,
fc6a28d7 8885 none_type,
f746161e
MM
8886 /*is_template=*/template_p,
8887 /*is_namespace=*/false,
8f78f01f
MM
8888 /*check_dependency=*/true,
8889 /*ambiguous_p=*/NULL);
5b4acce1
KL
8890 if (TREE_CODE (argument) != TEMPLATE_DECL
8891 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
a723baf1
MM
8892 cp_parser_error (parser, "expected template-name");
8893 }
8894 if (cp_parser_parse_definitely (parser))
8895 return argument;
d17811fd
MM
8896 /* It must be a non-type argument. There permitted cases are given
8897 in [temp.arg.nontype]:
8898
8899 -- an integral constant-expression of integral or enumeration
8900 type; or
8901
8902 -- the name of a non-type template-parameter; or
8903
8904 -- the name of an object or function with external linkage...
8905
8906 -- the address of an object or function with external linkage...
8907
04c06002 8908 -- a pointer to member... */
d17811fd
MM
8909 /* Look for a non-type template parameter. */
8910 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8911 {
8912 cp_parser_parse_tentatively (parser);
8913 argument = cp_parser_primary_expression (parser,
93678513 8914 /*cast_p=*/false,
d17811fd
MM
8915 &idk,
8916 &qualifying_class);
8917 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8918 || !cp_parser_next_token_ends_template_argument_p (parser))
8919 cp_parser_simulate_error (parser);
8920 if (cp_parser_parse_definitely (parser))
8921 return argument;
8922 }
db24eb1f 8923
d17811fd
MM
8924 /* If the next token is "&", the argument must be the address of an
8925 object or function with external linkage. */
8926 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8927 if (address_p)
8928 cp_lexer_consume_token (parser->lexer);
8929 /* See if we might have an id-expression. */
8930 token = cp_lexer_peek_token (parser->lexer);
8931 if (token->type == CPP_NAME
8932 || token->keyword == RID_OPERATOR
8933 || token->type == CPP_SCOPE
8934 || token->type == CPP_TEMPLATE_ID
8935 || token->type == CPP_NESTED_NAME_SPECIFIER)
8936 {
8937 cp_parser_parse_tentatively (parser);
8938 argument = cp_parser_primary_expression (parser,
93678513 8939 /*cast_p=*/false,
d17811fd
MM
8940 &idk,
8941 &qualifying_class);
8942 if (cp_parser_error_occurred (parser)
8943 || !cp_parser_next_token_ends_template_argument_p (parser))
8944 cp_parser_abort_tentative_parse (parser);
8945 else
8946 {
db24eb1f
NS
8947 if (TREE_CODE (argument) == INDIRECT_REF)
8948 {
8949 gcc_assert (REFERENCE_REF_P (argument));
8950 argument = TREE_OPERAND (argument, 0);
8951 }
8952
d17811fd
MM
8953 if (qualifying_class)
8954 argument = finish_qualified_id_expr (qualifying_class,
8955 argument,
8956 /*done=*/true,
8957 address_p);
8958 if (TREE_CODE (argument) == VAR_DECL)
8959 {
8960 /* A variable without external linkage might still be a
8961 valid constant-expression, so no error is issued here
8962 if the external-linkage check fails. */
8963 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8964 cp_parser_simulate_error (parser);
8965 }
8966 else if (is_overloaded_fn (argument))
8967 /* All overloaded functions are allowed; if the external
8968 linkage test does not pass, an error will be issued
8969 later. */
8970 ;
8971 else if (address_p
21526606 8972 && (TREE_CODE (argument) == OFFSET_REF
d17811fd
MM
8973 || TREE_CODE (argument) == SCOPE_REF))
8974 /* A pointer-to-member. */
8975 ;
db24eb1f
NS
8976 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
8977 ;
d17811fd
MM
8978 else
8979 cp_parser_simulate_error (parser);
8980
8981 if (cp_parser_parse_definitely (parser))
8982 {
8983 if (address_p)
8984 argument = build_x_unary_op (ADDR_EXPR, argument);
8985 return argument;
8986 }
8987 }
8988 }
8989 /* If the argument started with "&", there are no other valid
8990 alternatives at this point. */
8991 if (address_p)
8992 {
8993 cp_parser_error (parser, "invalid non-type template argument");
8994 return error_mark_node;
8995 }
db24eb1f 8996
4d5297fa 8997 /* If the argument wasn't successfully parsed as a type-id followed
21526606 8998 by '>>', the argument can only be a constant expression now.
4d5297fa
GB
8999 Otherwise, we try parsing the constant-expression tentatively,
9000 because the argument could really be a type-id. */
9001 if (maybe_type_id)
9002 cp_parser_parse_tentatively (parser);
21526606 9003 argument = cp_parser_constant_expression (parser,
d17811fd
MM
9004 /*allow_non_constant_p=*/false,
9005 /*non_constant_p=*/NULL);
9baa27a9 9006 argument = fold_non_dependent_expr (argument);
4d5297fa
GB
9007 if (!maybe_type_id)
9008 return argument;
9009 if (!cp_parser_next_token_ends_template_argument_p (parser))
9010 cp_parser_error (parser, "expected template-argument");
9011 if (cp_parser_parse_definitely (parser))
9012 return argument;
9013 /* We did our best to parse the argument as a non type-id, but that
9014 was the only alternative that matched (albeit with a '>' after
21526606 9015 it). We can assume it's just a typo from the user, and a
4d5297fa
GB
9016 diagnostic will then be issued. */
9017 return cp_parser_type_id (parser);
a723baf1
MM
9018}
9019
9020/* Parse an explicit-instantiation.
9021
9022 explicit-instantiation:
21526606 9023 template declaration
a723baf1
MM
9024
9025 Although the standard says `declaration', what it really means is:
9026
9027 explicit-instantiation:
21526606 9028 template decl-specifier-seq [opt] declarator [opt] ;
a723baf1
MM
9029
9030 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9031 supposed to be allowed. A defect report has been filed about this
21526606 9032 issue.
a723baf1
MM
9033
9034 GNU Extension:
21526606 9035
a723baf1 9036 explicit-instantiation:
21526606 9037 storage-class-specifier template
a723baf1 9038 decl-specifier-seq [opt] declarator [opt] ;
21526606 9039 function-specifier template
a723baf1
MM
9040 decl-specifier-seq [opt] declarator [opt] ; */
9041
9042static void
94edc4ab 9043cp_parser_explicit_instantiation (cp_parser* parser)
a723baf1 9044{
560ad596 9045 int declares_class_or_enum;
62d1db17 9046 cp_decl_specifier_seq decl_specifiers;
a723baf1
MM
9047 tree extension_specifier = NULL_TREE;
9048
9049 /* Look for an (optional) storage-class-specifier or
9050 function-specifier. */
9051 if (cp_parser_allow_gnu_extensions_p (parser))
9052 {
21526606 9053 extension_specifier
a723baf1
MM
9054 = cp_parser_storage_class_specifier_opt (parser);
9055 if (!extension_specifier)
98ca843c 9056 extension_specifier
62d1db17
MM
9057 = cp_parser_function_specifier_opt (parser,
9058 /*decl_specs=*/NULL);
a723baf1
MM
9059 }
9060
9061 /* Look for the `template' keyword. */
9062 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9063 /* Let the front end know that we are processing an explicit
9064 instantiation. */
9065 begin_explicit_instantiation ();
9066 /* [temp.explicit] says that we are supposed to ignore access
9067 control while processing explicit instantiation directives. */
78757caa 9068 push_deferring_access_checks (dk_no_check);
a723baf1 9069 /* Parse a decl-specifier-seq. */
62d1db17
MM
9070 cp_parser_decl_specifier_seq (parser,
9071 CP_PARSER_FLAGS_OPTIONAL,
9072 &decl_specifiers,
9073 &declares_class_or_enum);
a723baf1
MM
9074 /* If there was exactly one decl-specifier, and it declared a class,
9075 and there's no declarator, then we have an explicit type
9076 instantiation. */
9077 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9078 {
9079 tree type;
9080
62d1db17 9081 type = check_tag_decl (&decl_specifiers);
b7fc8b57
KL
9082 /* Turn access control back on for names used during
9083 template instantiation. */
9084 pop_deferring_access_checks ();
a723baf1
MM
9085 if (type)
9086 do_type_instantiation (type, extension_specifier, /*complain=*/1);
9087 }
9088 else
9089 {
058b15c1 9090 cp_declarator *declarator;
a723baf1
MM
9091 tree decl;
9092
9093 /* Parse the declarator. */
21526606 9094 declarator
62b8a44e 9095 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 9096 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
9097 /*parenthesized_p=*/NULL,
9098 /*member_p=*/false);
fc6a28d7
MM
9099 if (declares_class_or_enum & 2)
9100 cp_parser_check_for_definition_in_return_type (declarator,
9101 decl_specifiers.type);
058b15c1 9102 if (declarator != cp_error_declarator)
216bb6e1 9103 {
62d1db17 9104 decl = grokdeclarator (declarator, &decl_specifiers,
216bb6e1
MM
9105 NORMAL, 0, NULL);
9106 /* Turn access control back on for names used during
9107 template instantiation. */
9108 pop_deferring_access_checks ();
9109 /* Do the explicit instantiation. */
9110 do_decl_instantiation (decl, extension_specifier);
9111 }
9112 else
9113 {
9114 pop_deferring_access_checks ();
9115 /* Skip the body of the explicit instantiation. */
9116 cp_parser_skip_to_end_of_statement (parser);
9117 }
a723baf1
MM
9118 }
9119 /* We're done with the instantiation. */
9120 end_explicit_instantiation ();
a723baf1 9121
e0860732 9122 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1
MM
9123}
9124
9125/* Parse an explicit-specialization.
9126
9127 explicit-specialization:
21526606 9128 template < > declaration
a723baf1
MM
9129
9130 Although the standard says `declaration', what it really means is:
9131
9132 explicit-specialization:
9133 template <> decl-specifier [opt] init-declarator [opt] ;
21526606 9134 template <> function-definition
a723baf1
MM
9135 template <> explicit-specialization
9136 template <> template-declaration */
9137
9138static void
94edc4ab 9139cp_parser_explicit_specialization (cp_parser* parser)
a723baf1
MM
9140{
9141 /* Look for the `template' keyword. */
9142 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9143 /* Look for the `<'. */
9144 cp_parser_require (parser, CPP_LESS, "`<'");
9145 /* Look for the `>'. */
9146 cp_parser_require (parser, CPP_GREATER, "`>'");
9147 /* We have processed another parameter list. */
9148 ++parser->num_template_parameter_lists;
9149 /* Let the front end know that we are beginning a specialization. */
9150 begin_specialization ();
9151
9152 /* If the next keyword is `template', we need to figure out whether
9153 or not we're looking a template-declaration. */
9154 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9155 {
9156 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9157 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9158 cp_parser_template_declaration_after_export (parser,
9159 /*member_p=*/false);
9160 else
9161 cp_parser_explicit_specialization (parser);
9162 }
9163 else
9164 /* Parse the dependent declaration. */
21526606 9165 cp_parser_single_declaration (parser,
a723baf1
MM
9166 /*member_p=*/false,
9167 /*friend_p=*/NULL);
9168
9169 /* We're done with the specialization. */
9170 end_specialization ();
9171 /* We're done with this parameter list. */
9172 --parser->num_template_parameter_lists;
9173}
9174
9175/* Parse a type-specifier.
9176
9177 type-specifier:
9178 simple-type-specifier
9179 class-specifier
9180 enum-specifier
9181 elaborated-type-specifier
9182 cv-qualifier
9183
9184 GNU Extension:
9185
9186 type-specifier:
9187 __complex__
9188
62d1db17
MM
9189 Returns a representation of the type-specifier. For a
9190 class-specifier, enum-specifier, or elaborated-type-specifier, a
9191 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
a723baf1 9192
eb1aef53
KL
9193 The parser flags FLAGS is used to control type-specifier parsing.
9194
9195 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9196 in a decl-specifier-seq.
a723baf1
MM
9197
9198 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9199 class-specifier, enum-specifier, or elaborated-type-specifier, then
83a00410 9200 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
560ad596
MM
9201 if a type is declared; 2 if it is defined. Otherwise, it is set to
9202 zero.
a723baf1
MM
9203
9204 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9205 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9206 is set to FALSE. */
9207
9208static tree
21526606
EC
9209cp_parser_type_specifier (cp_parser* parser,
9210 cp_parser_flags flags,
62d1db17 9211 cp_decl_specifier_seq *decl_specs,
94edc4ab 9212 bool is_declaration,
560ad596 9213 int* declares_class_or_enum,
94edc4ab 9214 bool* is_cv_qualifier)
a723baf1
MM
9215{
9216 tree type_spec = NULL_TREE;
9217 cp_token *token;
9218 enum rid keyword;
62d1db17 9219 cp_decl_spec ds = ds_last;
a723baf1
MM
9220
9221 /* Assume this type-specifier does not declare a new type. */
9222 if (declares_class_or_enum)
543ca912 9223 *declares_class_or_enum = 0;
a723baf1
MM
9224 /* And that it does not specify a cv-qualifier. */
9225 if (is_cv_qualifier)
9226 *is_cv_qualifier = false;
9227 /* Peek at the next token. */
9228 token = cp_lexer_peek_token (parser->lexer);
9229
9230 /* If we're looking at a keyword, we can use that to guide the
9231 production we choose. */
9232 keyword = token->keyword;
9233 switch (keyword)
9234 {
ff4eb0b5
ZW
9235 case RID_ENUM:
9236 /* 'enum' [identifier] '{' introduces an enum-specifier;
9237 'enum' <anything else> introduces an elaborated-type-specifier. */
9238 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9239 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9240 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9241 == CPP_OPEN_BRACE))
9242 {
a5e51518
KL
9243 if (parser->num_template_parameter_lists)
9244 {
9245 error ("template declaration of %qs", "enum");
9246 cp_parser_skip_to_end_of_block_or_statement (parser);
9247 type_spec = error_mark_node;
9248 }
9249 else
9250 type_spec = cp_parser_enum_specifier (parser);
9251
ff4eb0b5
ZW
9252 if (declares_class_or_enum)
9253 *declares_class_or_enum = 2;
9254 if (decl_specs)
9255 cp_parser_set_decl_spec_type (decl_specs,
9256 type_spec,
9257 /*user_defined_p=*/true);
9258 return type_spec;
9259 }
9260 else
9261 goto elaborated_type_specifier;
9262
a723baf1
MM
9263 /* Any of these indicate either a class-specifier, or an
9264 elaborated-type-specifier. */
9265 case RID_CLASS:
9266 case RID_STRUCT:
9267 case RID_UNION:
a723baf1 9268 /* Parse tentatively so that we can back up if we don't find a
ff4eb0b5 9269 class-specifier. */
a723baf1 9270 cp_parser_parse_tentatively (parser);
ff4eb0b5
ZW
9271 /* Look for the class-specifier. */
9272 type_spec = cp_parser_class_specifier (parser);
a723baf1
MM
9273 /* If that worked, we're done. */
9274 if (cp_parser_parse_definitely (parser))
9275 {
9276 if (declares_class_or_enum)
560ad596 9277 *declares_class_or_enum = 2;
62d1db17
MM
9278 if (decl_specs)
9279 cp_parser_set_decl_spec_type (decl_specs,
9280 type_spec,
9281 /*user_defined_p=*/true);
a723baf1
MM
9282 return type_spec;
9283 }
9284
9285 /* Fall through. */
ff4eb0b5
ZW
9286 elaborated_type_specifier:
9287 /* We're declaring (not defining) a class or enum. */
9288 if (declares_class_or_enum)
9289 *declares_class_or_enum = 1;
a723baf1 9290
ff4eb0b5 9291 /* Fall through. */
a723baf1
MM
9292 case RID_TYPENAME:
9293 /* Look for an elaborated-type-specifier. */
98ca843c
EC
9294 type_spec
9295 = (cp_parser_elaborated_type_specifier
62d1db17
MM
9296 (parser,
9297 decl_specs && decl_specs->specs[(int) ds_friend],
9298 is_declaration));
62d1db17
MM
9299 if (decl_specs)
9300 cp_parser_set_decl_spec_type (decl_specs,
9301 type_spec,
9302 /*user_defined_p=*/true);
a723baf1
MM
9303 return type_spec;
9304
9305 case RID_CONST:
62d1db17
MM
9306 ds = ds_const;
9307 if (is_cv_qualifier)
9308 *is_cv_qualifier = true;
9309 break;
98ca843c 9310
a723baf1 9311 case RID_VOLATILE:
62d1db17 9312 ds = ds_volatile;
a723baf1
MM
9313 if (is_cv_qualifier)
9314 *is_cv_qualifier = true;
62d1db17 9315 break;
a723baf1 9316
62d1db17
MM
9317 case RID_RESTRICT:
9318 ds = ds_restrict;
9319 if (is_cv_qualifier)
9320 *is_cv_qualifier = true;
9321 break;
a723baf1
MM
9322
9323 case RID_COMPLEX:
9324 /* The `__complex__' keyword is a GNU extension. */
62d1db17
MM
9325 ds = ds_complex;
9326 break;
a723baf1
MM
9327
9328 default:
9329 break;
9330 }
9331
62d1db17
MM
9332 /* Handle simple keywords. */
9333 if (ds != ds_last)
9334 {
9335 if (decl_specs)
9336 {
9337 ++decl_specs->specs[(int)ds];
9338 decl_specs->any_specifiers_p = true;
9339 }
9340 return cp_lexer_consume_token (parser->lexer)->value;
9341 }
9342
a723baf1
MM
9343 /* If we do not already have a type-specifier, assume we are looking
9344 at a simple-type-specifier. */
98ca843c 9345 type_spec = cp_parser_simple_type_specifier (parser,
62d1db17
MM
9346 decl_specs,
9347 flags);
a723baf1
MM
9348
9349 /* If we didn't find a type-specifier, and a type-specifier was not
9350 optional in this context, issue an error message. */
9351 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9352 {
9353 cp_parser_error (parser, "expected type specifier");
9354 return error_mark_node;
9355 }
9356
9357 return type_spec;
9358}
9359
9360/* Parse a simple-type-specifier.
9361
9362 simple-type-specifier:
9363 :: [opt] nested-name-specifier [opt] type-name
9364 :: [opt] nested-name-specifier template template-id
9365 char
9366 wchar_t
9367 bool
9368 short
9369 int
9370 long
9371 signed
9372 unsigned
9373 float
9374 double
21526606 9375 void
a723baf1
MM
9376
9377 GNU Extension:
9378
9379 simple-type-specifier:
9380 __typeof__ unary-expression
9381 __typeof__ ( type-id )
9382
62d1db17
MM
9383 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9384 appropriately updated. */
a723baf1
MM
9385
9386static tree
98ca843c 9387cp_parser_simple_type_specifier (cp_parser* parser,
62d1db17
MM
9388 cp_decl_specifier_seq *decl_specs,
9389 cp_parser_flags flags)
a723baf1
MM
9390{
9391 tree type = NULL_TREE;
9392 cp_token *token;
9393
9394 /* Peek at the next token. */
9395 token = cp_lexer_peek_token (parser->lexer);
9396
9397 /* If we're looking at a keyword, things are easy. */
9398 switch (token->keyword)
9399 {
9400 case RID_CHAR:
62d1db17
MM
9401 if (decl_specs)
9402 decl_specs->explicit_char_p = true;
4b0d3cbe
MM
9403 type = char_type_node;
9404 break;
a723baf1 9405 case RID_WCHAR:
4b0d3cbe
MM
9406 type = wchar_type_node;
9407 break;
a723baf1 9408 case RID_BOOL:
4b0d3cbe
MM
9409 type = boolean_type_node;
9410 break;
a723baf1 9411 case RID_SHORT:
62d1db17
MM
9412 if (decl_specs)
9413 ++decl_specs->specs[(int) ds_short];
4b0d3cbe
MM
9414 type = short_integer_type_node;
9415 break;
a723baf1 9416 case RID_INT:
62d1db17
MM
9417 if (decl_specs)
9418 decl_specs->explicit_int_p = true;
4b0d3cbe
MM
9419 type = integer_type_node;
9420 break;
a723baf1 9421 case RID_LONG:
62d1db17
MM
9422 if (decl_specs)
9423 ++decl_specs->specs[(int) ds_long];
4b0d3cbe
MM
9424 type = long_integer_type_node;
9425 break;
a723baf1 9426 case RID_SIGNED:
62d1db17
MM
9427 if (decl_specs)
9428 ++decl_specs->specs[(int) ds_signed];
4b0d3cbe
MM
9429 type = integer_type_node;
9430 break;
a723baf1 9431 case RID_UNSIGNED:
62d1db17
MM
9432 if (decl_specs)
9433 ++decl_specs->specs[(int) ds_unsigned];
4b0d3cbe
MM
9434 type = unsigned_type_node;
9435 break;
a723baf1 9436 case RID_FLOAT:
4b0d3cbe
MM
9437 type = float_type_node;
9438 break;
a723baf1 9439 case RID_DOUBLE:
4b0d3cbe
MM
9440 type = double_type_node;
9441 break;
a723baf1 9442 case RID_VOID:
4b0d3cbe
MM
9443 type = void_type_node;
9444 break;
a723baf1
MM
9445
9446 case RID_TYPEOF:
62d1db17
MM
9447 /* Consume the `typeof' token. */
9448 cp_lexer_consume_token (parser->lexer);
9449 /* Parse the operand to `typeof'. */
9450 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9451 /* If it is not already a TYPE, take its type. */
9452 if (!TYPE_P (type))
9453 type = finish_typeof (type);
9454
9455 if (decl_specs)
9456 cp_parser_set_decl_spec_type (decl_specs, type,
9457 /*user_defined_p=*/true);
98ca843c 9458
62d1db17 9459 return type;
a723baf1
MM
9460
9461 default:
9462 break;
9463 }
9464
4b0d3cbe
MM
9465 /* If the type-specifier was for a built-in type, we're done. */
9466 if (type)
9467 {
9468 tree id;
9469
62d1db17
MM
9470 /* Record the type. */
9471 if (decl_specs
9472 && (token->keyword != RID_SIGNED
9473 && token->keyword != RID_UNSIGNED
9474 && token->keyword != RID_SHORT
9475 && token->keyword != RID_LONG))
98ca843c 9476 cp_parser_set_decl_spec_type (decl_specs,
62d1db17
MM
9477 type,
9478 /*user_defined=*/false);
9479 if (decl_specs)
9480 decl_specs->any_specifiers_p = true;
9481
4b0d3cbe
MM
9482 /* Consume the token. */
9483 id = cp_lexer_consume_token (parser->lexer)->value;
0d956474
GB
9484
9485 /* There is no valid C++ program where a non-template type is
9486 followed by a "<". That usually indicates that the user thought
9487 that the type was a template. */
9488 cp_parser_check_for_invalid_template_id (parser, type);
9489
62d1db17 9490 return TYPE_NAME (type);
4b0d3cbe
MM
9491 }
9492
a723baf1 9493 /* The type-specifier must be a user-defined type. */
21526606 9494 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
a723baf1 9495 {
0c1a1ecd 9496 bool qualified_p;
f68e4dc8 9497 bool global_p;
0c1a1ecd 9498
a723baf1
MM
9499 /* Don't gobble tokens or issue error messages if this is an
9500 optional type-specifier. */
9501 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9502 cp_parser_parse_tentatively (parser);
9503
9504 /* Look for the optional `::' operator. */
f68e4dc8 9505 global_p
da740453
MM
9506 = (cp_parser_global_scope_opt (parser,
9507 /*current_scope_valid_p=*/false)
9508 != NULL_TREE);
a723baf1 9509 /* Look for the nested-name specifier. */
0c1a1ecd
MM
9510 qualified_p
9511 = (cp_parser_nested_name_specifier_opt (parser,
9512 /*typename_keyword_p=*/false,
9513 /*check_dependency_p=*/true,
9514 /*type_p=*/false,
6661a85f
EB
9515 /*is_declaration=*/false)
9516 != NULL_TREE);
a723baf1
MM
9517 /* If we have seen a nested-name-specifier, and the next token
9518 is `template', then we are using the template-id production. */
21526606 9519 if (parser->scope
a723baf1
MM
9520 && cp_parser_optional_template_keyword (parser))
9521 {
9522 /* Look for the template-id. */
21526606 9523 type = cp_parser_template_id (parser,
a723baf1 9524 /*template_keyword_p=*/true,
a668c6ad
MM
9525 /*check_dependency_p=*/true,
9526 /*is_declaration=*/false);
a723baf1
MM
9527 /* If the template-id did not name a type, we are out of
9528 luck. */
9529 if (TREE_CODE (type) != TYPE_DECL)
9530 {
9531 cp_parser_error (parser, "expected template-id for type");
9532 type = NULL_TREE;
9533 }
9534 }
9535 /* Otherwise, look for a type-name. */
9536 else
4bb8ca28 9537 type = cp_parser_type_name (parser);
0c1a1ecd 9538 /* Keep track of all name-lookups performed in class scopes. */
98ca843c 9539 if (type
f68e4dc8 9540 && !global_p
0c1a1ecd 9541 && !qualified_p
98ca843c 9542 && TREE_CODE (type) == TYPE_DECL
0c1a1ecd
MM
9543 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9544 maybe_note_name_used_in_class (DECL_NAME (type), type);
a723baf1 9545 /* If it didn't work out, we don't have a TYPE. */
21526606 9546 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
a723baf1
MM
9547 && !cp_parser_parse_definitely (parser))
9548 type = NULL_TREE;
62d1db17
MM
9549 if (type && decl_specs)
9550 cp_parser_set_decl_spec_type (decl_specs, type,
9551 /*user_defined=*/true);
a723baf1
MM
9552 }
9553
9554 /* If we didn't get a type-name, issue an error message. */
9555 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9556 {
9557 cp_parser_error (parser, "expected type-name");
9558 return error_mark_node;
9559 }
9560
a668c6ad
MM
9561 /* There is no valid C++ program where a non-template type is
9562 followed by a "<". That usually indicates that the user thought
9563 that the type was a template. */
4bb8ca28 9564 if (type && type != error_mark_node)
ee43dab5 9565 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
ec75414f 9566
a723baf1
MM
9567 return type;
9568}
9569
9570/* Parse a type-name.
9571
9572 type-name:
9573 class-name
9574 enum-name
21526606 9575 typedef-name
a723baf1
MM
9576
9577 enum-name:
9578 identifier
9579
9580 typedef-name:
21526606 9581 identifier
a723baf1
MM
9582
9583 Returns a TYPE_DECL for the the type. */
9584
9585static tree
94edc4ab 9586cp_parser_type_name (cp_parser* parser)
a723baf1
MM
9587{
9588 tree type_decl;
9589 tree identifier;
9590
9591 /* We can't know yet whether it is a class-name or not. */
9592 cp_parser_parse_tentatively (parser);
9593 /* Try a class-name. */
21526606 9594 type_decl = cp_parser_class_name (parser,
a723baf1
MM
9595 /*typename_keyword_p=*/false,
9596 /*template_keyword_p=*/false,
fc6a28d7 9597 none_type,
a723baf1 9598 /*check_dependency_p=*/true,
a668c6ad
MM
9599 /*class_head_p=*/false,
9600 /*is_declaration=*/false);
a723baf1
MM
9601 /* If it's not a class-name, keep looking. */
9602 if (!cp_parser_parse_definitely (parser))
9603 {
9604 /* It must be a typedef-name or an enum-name. */
9605 identifier = cp_parser_identifier (parser);
9606 if (identifier == error_mark_node)
9607 return error_mark_node;
21526606 9608
a723baf1
MM
9609 /* Look up the type-name. */
9610 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9611 /* Issue an error if we did not find a type-name. */
9612 if (TREE_CODE (type_decl) != TYPE_DECL)
9613 {
4bb8ca28 9614 if (!cp_parser_simulate_error (parser))
21526606 9615 cp_parser_name_lookup_error (parser, identifier, type_decl,
4bb8ca28 9616 "is not a type");
a723baf1
MM
9617 type_decl = error_mark_node;
9618 }
9619 /* Remember that the name was used in the definition of the
9620 current class so that we can check later to see if the
9621 meaning would have been different after the class was
9622 entirely defined. */
9623 else if (type_decl != error_mark_node
9624 && !parser->scope)
9625 maybe_note_name_used_in_class (identifier, type_decl);
9626 }
21526606 9627
a723baf1
MM
9628 return type_decl;
9629}
9630
9631
9632/* Parse an elaborated-type-specifier. Note that the grammar given
9633 here incorporates the resolution to DR68.
9634
9635 elaborated-type-specifier:
9636 class-key :: [opt] nested-name-specifier [opt] identifier
9637 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9638 enum :: [opt] nested-name-specifier [opt] identifier
9639 typename :: [opt] nested-name-specifier identifier
21526606
EC
9640 typename :: [opt] nested-name-specifier template [opt]
9641 template-id
a723baf1 9642
360d1b99
MM
9643 GNU extension:
9644
9645 elaborated-type-specifier:
9646 class-key attributes :: [opt] nested-name-specifier [opt] identifier
21526606 9647 class-key attributes :: [opt] nested-name-specifier [opt]
360d1b99
MM
9648 template [opt] template-id
9649 enum attributes :: [opt] nested-name-specifier [opt] identifier
9650
a723baf1
MM
9651 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9652 declared `friend'. If IS_DECLARATION is TRUE, then this
9653 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9654 something is being declared.
9655
9656 Returns the TYPE specified. */
9657
9658static tree
21526606
EC
9659cp_parser_elaborated_type_specifier (cp_parser* parser,
9660 bool is_friend,
94edc4ab 9661 bool is_declaration)
a723baf1
MM
9662{
9663 enum tag_types tag_type;
9664 tree identifier;
9665 tree type = NULL_TREE;
360d1b99 9666 tree attributes = NULL_TREE;
a723baf1
MM
9667
9668 /* See if we're looking at the `enum' keyword. */
9669 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9670 {
9671 /* Consume the `enum' token. */
9672 cp_lexer_consume_token (parser->lexer);
9673 /* Remember that it's an enumeration type. */
9674 tag_type = enum_type;
360d1b99
MM
9675 /* Parse the attributes. */
9676 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
9677 }
9678 /* Or, it might be `typename'. */
9679 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9680 RID_TYPENAME))
9681 {
9682 /* Consume the `typename' token. */
9683 cp_lexer_consume_token (parser->lexer);
9684 /* Remember that it's a `typename' type. */
9685 tag_type = typename_type;
9686 /* The `typename' keyword is only allowed in templates. */
9687 if (!processing_template_decl)
2a13a625 9688 pedwarn ("using %<typename%> outside of template");
a723baf1
MM
9689 }
9690 /* Otherwise it must be a class-key. */
9691 else
9692 {
9693 tag_type = cp_parser_class_key (parser);
9694 if (tag_type == none_type)
9695 return error_mark_node;
360d1b99
MM
9696 /* Parse the attributes. */
9697 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
9698 }
9699
9700 /* Look for the `::' operator. */
21526606 9701 cp_parser_global_scope_opt (parser,
a723baf1
MM
9702 /*current_scope_valid_p=*/false);
9703 /* Look for the nested-name-specifier. */
9704 if (tag_type == typename_type)
8fa1ad0e
MM
9705 {
9706 if (cp_parser_nested_name_specifier (parser,
9707 /*typename_keyword_p=*/true,
9708 /*check_dependency_p=*/true,
a668c6ad 9709 /*type_p=*/true,
21526606 9710 is_declaration)
8fa1ad0e
MM
9711 == error_mark_node)
9712 return error_mark_node;
9713 }
a723baf1
MM
9714 else
9715 /* Even though `typename' is not present, the proposed resolution
9716 to Core Issue 180 says that in `class A<T>::B', `B' should be
9717 considered a type-name, even if `A<T>' is dependent. */
9718 cp_parser_nested_name_specifier_opt (parser,
9719 /*typename_keyword_p=*/true,
9720 /*check_dependency_p=*/true,
a668c6ad
MM
9721 /*type_p=*/true,
9722 is_declaration);
a723baf1
MM
9723 /* For everything but enumeration types, consider a template-id. */
9724 if (tag_type != enum_type)
9725 {
9726 bool template_p = false;
9727 tree decl;
9728
9729 /* Allow the `template' keyword. */
9730 template_p = cp_parser_optional_template_keyword (parser);
9731 /* If we didn't see `template', we don't know if there's a
9732 template-id or not. */
9733 if (!template_p)
9734 cp_parser_parse_tentatively (parser);
9735 /* Parse the template-id. */
9736 decl = cp_parser_template_id (parser, template_p,
a668c6ad
MM
9737 /*check_dependency_p=*/true,
9738 is_declaration);
a723baf1
MM
9739 /* If we didn't find a template-id, look for an ordinary
9740 identifier. */
9741 if (!template_p && !cp_parser_parse_definitely (parser))
9742 ;
9743 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9744 in effect, then we must assume that, upon instantiation, the
9745 template will correspond to a class. */
9746 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9747 && tag_type == typename_type)
9748 type = make_typename_type (parser->scope, decl,
fc6a28d7 9749 typename_type,
a723baf1 9750 /*complain=*/1);
21526606 9751 else
a723baf1
MM
9752 type = TREE_TYPE (decl);
9753 }
9754
9755 /* For an enumeration type, consider only a plain identifier. */
9756 if (!type)
9757 {
9758 identifier = cp_parser_identifier (parser);
9759
9760 if (identifier == error_mark_node)
eb5abb39
NS
9761 {
9762 parser->scope = NULL_TREE;
9763 return error_mark_node;
9764 }
a723baf1
MM
9765
9766 /* For a `typename', we needn't call xref_tag. */
0c88d886
MM
9767 if (tag_type == typename_type
9768 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
21526606 9769 return cp_parser_make_typename_type (parser, parser->scope,
2097b5f2 9770 identifier);
a723baf1
MM
9771 /* Look up a qualified name in the usual way. */
9772 if (parser->scope)
9773 {
9774 tree decl;
9775
21526606 9776 decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 9777 tag_type,
b0bc6e8e 9778 /*is_template=*/false,
eea9800f 9779 /*is_namespace=*/false,
8f78f01f
MM
9780 /*check_dependency=*/true,
9781 /*ambiguous_p=*/NULL);
710b73e6
KL
9782
9783 /* If we are parsing friend declaration, DECL may be a
9784 TEMPLATE_DECL tree node here. However, we need to check
9785 whether this TEMPLATE_DECL results in valid code. Consider
9786 the following example:
9787
9788 namespace N {
9789 template <class T> class C {};
9790 }
9791 class X {
9792 template <class T> friend class N::C; // #1, valid code
9793 };
9794 template <class T> class Y {
9795 friend class N::C; // #2, invalid code
9796 };
9797
9798 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9799 name lookup of `N::C'. We see that friend declaration must
9800 be template for the code to be valid. Note that
9801 processing_template_decl does not work here since it is
9802 always 1 for the above two cases. */
9803
21526606 9804 decl = (cp_parser_maybe_treat_template_as_class
710b73e6
KL
9805 (decl, /*tag_name_p=*/is_friend
9806 && parser->num_template_parameter_lists));
a723baf1
MM
9807
9808 if (TREE_CODE (decl) != TYPE_DECL)
9809 {
0c88d886
MM
9810 cp_parser_diagnose_invalid_type_name (parser,
9811 parser->scope,
9812 identifier);
a723baf1
MM
9813 return error_mark_node;
9814 }
560ad596
MM
9815
9816 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
21526606 9817 check_elaborated_type_specifier
4b0d3cbe 9818 (tag_type, decl,
560ad596
MM
9819 (parser->num_template_parameter_lists
9820 || DECL_SELF_REFERENCE_P (decl)));
a723baf1
MM
9821
9822 type = TREE_TYPE (decl);
9823 }
21526606 9824 else
a723baf1
MM
9825 {
9826 /* An elaborated-type-specifier sometimes introduces a new type and
9827 sometimes names an existing type. Normally, the rule is that it
9828 introduces a new type only if there is not an existing type of
9829 the same name already in scope. For example, given:
9830
9831 struct S {};
9832 void f() { struct S s; }
9833
9834 the `struct S' in the body of `f' is the same `struct S' as in
9835 the global scope; the existing definition is used. However, if
21526606 9836 there were no global declaration, this would introduce a new
a723baf1
MM
9837 local class named `S'.
9838
9839 An exception to this rule applies to the following code:
9840
9841 namespace N { struct S; }
9842
9843 Here, the elaborated-type-specifier names a new type
9844 unconditionally; even if there is already an `S' in the
9845 containing scope this declaration names a new type.
9846 This exception only applies if the elaborated-type-specifier
9847 forms the complete declaration:
9848
21526606 9849 [class.name]
a723baf1
MM
9850
9851 A declaration consisting solely of `class-key identifier ;' is
9852 either a redeclaration of the name in the current scope or a
9853 forward declaration of the identifier as a class name. It
9854 introduces the name into the current scope.
9855
9856 We are in this situation precisely when the next token is a `;'.
9857
9858 An exception to the exception is that a `friend' declaration does
9859 *not* name a new type; i.e., given:
9860
9861 struct S { friend struct T; };
9862
21526606 9863 `T' is not a new type in the scope of `S'.
a723baf1
MM
9864
9865 Also, `new struct S' or `sizeof (struct S)' never results in the
9866 definition of a new type; a new type can only be declared in a
9bcb9aae 9867 declaration context. */
a723baf1 9868
29ef83de
KL
9869 tag_scope ts;
9870 if (is_friend)
9871 /* Friends have special name lookup rules. */
9872 ts = ts_within_enclosing_non_class;
9873 else if (is_declaration
9874 && cp_lexer_next_token_is (parser->lexer,
9875 CPP_SEMICOLON))
9876 /* This is a `class-key identifier ;' */
9877 ts = ts_current;
9878 else
9879 ts = ts_global;
9880
e0fed25b
DS
9881 /* Warn about attributes. They are ignored. */
9882 if (attributes)
9883 warning ("type attributes are honored only at type definition");
9884
29ef83de 9885 type = xref_tag (tag_type, identifier, ts,
cbd63935 9886 parser->num_template_parameter_lists);
a723baf1
MM
9887 }
9888 }
9889 if (tag_type != enum_type)
9890 cp_parser_check_class_key (tag_type, type);
ee43dab5
MM
9891
9892 /* A "<" cannot follow an elaborated type specifier. If that
9893 happens, the user was probably trying to form a template-id. */
9894 cp_parser_check_for_invalid_template_id (parser, type);
9895
a723baf1
MM
9896 return type;
9897}
9898
9899/* Parse an enum-specifier.
9900
9901 enum-specifier:
9902 enum identifier [opt] { enumerator-list [opt] }
9903
f6af9a15
MA
9904 GNU Extensions:
9905 enum identifier [opt] { enumerator-list [opt] } attributes
9906
a723baf1
MM
9907 Returns an ENUM_TYPE representing the enumeration. */
9908
9909static tree
94edc4ab 9910cp_parser_enum_specifier (cp_parser* parser)
a723baf1 9911{
ff4eb0b5 9912 tree identifier;
a723baf1
MM
9913 tree type;
9914
ff4eb0b5
ZW
9915 /* Caller guarantees that the current token is 'enum', an identifier
9916 possibly follows, and the token after that is an opening brace.
9917 If we don't have an identifier, fabricate an anonymous name for
9918 the enumeration being defined. */
9919 cp_lexer_consume_token (parser->lexer);
a723baf1 9920
ff4eb0b5 9921 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
a723baf1 9922 identifier = cp_parser_identifier (parser);
ff4eb0b5
ZW
9923 else
9924 identifier = make_anon_name ();
a723baf1 9925
a723baf1
MM
9926 /* Issue an error message if type-definitions are forbidden here. */
9927 cp_parser_check_type_definition (parser);
9928
2cfe82fe
ZW
9929 /* Create the new type. We do this before consuming the opening brace
9930 so the enum will be recorded as being on the line of its tag (or the
9931 'enum' keyword, if there is no tag). */
ff4eb0b5 9932 type = start_enum (identifier);
a723baf1 9933
2cfe82fe
ZW
9934 /* Consume the opening brace. */
9935 cp_lexer_consume_token (parser->lexer);
9936
ff4eb0b5
ZW
9937 /* If the next token is not '}', then there are some enumerators. */
9938 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
a723baf1 9939 cp_parser_enumerator_list (parser, type);
ff4eb0b5
ZW
9940
9941 /* Consume the final '}'. */
a723baf1
MM
9942 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9943
f6af9a15 9944 /* Look for trailing attributes to apply to this enumeration, and
03fd3f84 9945 apply them if appropriate. */
f6af9a15
MA
9946 if (cp_parser_allow_gnu_extensions_p (parser))
9947 {
9948 tree trailing_attr = cp_parser_attributes_opt (parser);
9949 cplus_decl_attributes (&type,
9950 trailing_attr,
9951 (int) ATTR_FLAG_TYPE_IN_PLACE);
9952 }
9953
a723baf1
MM
9954 /* Finish up the enumeration. */
9955 finish_enum (type);
9956
9957 return type;
9958}
9959
9960/* Parse an enumerator-list. The enumerators all have the indicated
21526606 9961 TYPE.
a723baf1
MM
9962
9963 enumerator-list:
9964 enumerator-definition
9965 enumerator-list , enumerator-definition */
9966
9967static void
94edc4ab 9968cp_parser_enumerator_list (cp_parser* parser, tree type)
a723baf1
MM
9969{
9970 while (true)
9971 {
a723baf1
MM
9972 /* Parse an enumerator-definition. */
9973 cp_parser_enumerator_definition (parser, type);
ff4eb0b5
ZW
9974
9975 /* If the next token is not a ',', we've reached the end of
9976 the list. */
9977 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
a723baf1
MM
9978 break;
9979 /* Otherwise, consume the `,' and keep going. */
9980 cp_lexer_consume_token (parser->lexer);
9981 /* If the next token is a `}', there is a trailing comma. */
9982 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9983 {
9984 if (pedantic && !in_system_header)
9985 pedwarn ("comma at end of enumerator list");
9986 break;
9987 }
9988 }
9989}
9990
9991/* Parse an enumerator-definition. The enumerator has the indicated
9992 TYPE.
9993
9994 enumerator-definition:
9995 enumerator
9996 enumerator = constant-expression
21526606 9997
a723baf1
MM
9998 enumerator:
9999 identifier */
10000
10001static void
94edc4ab 10002cp_parser_enumerator_definition (cp_parser* parser, tree type)
a723baf1 10003{
a723baf1
MM
10004 tree identifier;
10005 tree value;
10006
10007 /* Look for the identifier. */
10008 identifier = cp_parser_identifier (parser);
10009 if (identifier == error_mark_node)
10010 return;
21526606 10011
ff4eb0b5
ZW
10012 /* If the next token is an '=', then there is an explicit value. */
10013 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
a723baf1
MM
10014 {
10015 /* Consume the `=' token. */
10016 cp_lexer_consume_token (parser->lexer);
10017 /* Parse the value. */
21526606 10018 value = cp_parser_constant_expression (parser,
d17811fd 10019 /*allow_non_constant_p=*/false,
14d22dd6 10020 NULL);
a723baf1
MM
10021 }
10022 else
10023 value = NULL_TREE;
10024
10025 /* Create the enumerator. */
10026 build_enumerator (identifier, value, type);
10027}
10028
10029/* Parse a namespace-name.
10030
10031 namespace-name:
10032 original-namespace-name
10033 namespace-alias
10034
10035 Returns the NAMESPACE_DECL for the namespace. */
10036
10037static tree
94edc4ab 10038cp_parser_namespace_name (cp_parser* parser)
a723baf1
MM
10039{
10040 tree identifier;
10041 tree namespace_decl;
10042
10043 /* Get the name of the namespace. */
10044 identifier = cp_parser_identifier (parser);
10045 if (identifier == error_mark_node)
10046 return error_mark_node;
10047
eea9800f
MM
10048 /* Look up the identifier in the currently active scope. Look only
10049 for namespaces, due to:
10050
10051 [basic.lookup.udir]
10052
10053 When looking up a namespace-name in a using-directive or alias
21526606 10054 definition, only namespace names are considered.
eea9800f
MM
10055
10056 And:
10057
10058 [basic.lookup.qual]
10059
10060 During the lookup of a name preceding the :: scope resolution
21526606 10061 operator, object, function, and enumerator names are ignored.
eea9800f
MM
10062
10063 (Note that cp_parser_class_or_namespace_name only calls this
10064 function if the token after the name is the scope resolution
10065 operator.) */
10066 namespace_decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 10067 none_type,
b0bc6e8e 10068 /*is_template=*/false,
eea9800f 10069 /*is_namespace=*/true,
8f78f01f
MM
10070 /*check_dependency=*/true,
10071 /*ambiguous_p=*/NULL);
a723baf1
MM
10072 /* If it's not a namespace, issue an error. */
10073 if (namespace_decl == error_mark_node
10074 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10075 {
10076 cp_parser_error (parser, "expected namespace-name");
10077 namespace_decl = error_mark_node;
10078 }
21526606 10079
a723baf1
MM
10080 return namespace_decl;
10081}
10082
10083/* Parse a namespace-definition.
10084
10085 namespace-definition:
10086 named-namespace-definition
21526606 10087 unnamed-namespace-definition
a723baf1
MM
10088
10089 named-namespace-definition:
10090 original-namespace-definition
10091 extension-namespace-definition
10092
10093 original-namespace-definition:
10094 namespace identifier { namespace-body }
21526606 10095
a723baf1
MM
10096 extension-namespace-definition:
10097 namespace original-namespace-name { namespace-body }
21526606 10098
a723baf1
MM
10099 unnamed-namespace-definition:
10100 namespace { namespace-body } */
10101
10102static void
94edc4ab 10103cp_parser_namespace_definition (cp_parser* parser)
a723baf1
MM
10104{
10105 tree identifier;
10106
10107 /* Look for the `namespace' keyword. */
10108 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10109
10110 /* Get the name of the namespace. We do not attempt to distinguish
10111 between an original-namespace-definition and an
10112 extension-namespace-definition at this point. The semantic
10113 analysis routines are responsible for that. */
10114 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10115 identifier = cp_parser_identifier (parser);
10116 else
10117 identifier = NULL_TREE;
10118
10119 /* Look for the `{' to start the namespace. */
10120 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10121 /* Start the namespace. */
10122 push_namespace (identifier);
10123 /* Parse the body of the namespace. */
10124 cp_parser_namespace_body (parser);
10125 /* Finish the namespace. */
10126 pop_namespace ();
10127 /* Look for the final `}'. */
10128 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10129}
10130
10131/* Parse a namespace-body.
10132
10133 namespace-body:
10134 declaration-seq [opt] */
10135
10136static void
94edc4ab 10137cp_parser_namespace_body (cp_parser* parser)
a723baf1
MM
10138{
10139 cp_parser_declaration_seq_opt (parser);
10140}
10141
10142/* Parse a namespace-alias-definition.
10143
10144 namespace-alias-definition:
10145 namespace identifier = qualified-namespace-specifier ; */
10146
10147static void
94edc4ab 10148cp_parser_namespace_alias_definition (cp_parser* parser)
a723baf1
MM
10149{
10150 tree identifier;
10151 tree namespace_specifier;
10152
10153 /* Look for the `namespace' keyword. */
10154 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10155 /* Look for the identifier. */
10156 identifier = cp_parser_identifier (parser);
10157 if (identifier == error_mark_node)
10158 return;
10159 /* Look for the `=' token. */
10160 cp_parser_require (parser, CPP_EQ, "`='");
10161 /* Look for the qualified-namespace-specifier. */
21526606 10162 namespace_specifier
a723baf1
MM
10163 = cp_parser_qualified_namespace_specifier (parser);
10164 /* Look for the `;' token. */
10165 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10166
10167 /* Register the alias in the symbol table. */
10168 do_namespace_alias (identifier, namespace_specifier);
10169}
10170
10171/* Parse a qualified-namespace-specifier.
10172
10173 qualified-namespace-specifier:
10174 :: [opt] nested-name-specifier [opt] namespace-name
10175
10176 Returns a NAMESPACE_DECL corresponding to the specified
10177 namespace. */
10178
10179static tree
94edc4ab 10180cp_parser_qualified_namespace_specifier (cp_parser* parser)
a723baf1
MM
10181{
10182 /* Look for the optional `::'. */
21526606 10183 cp_parser_global_scope_opt (parser,
a723baf1
MM
10184 /*current_scope_valid_p=*/false);
10185
10186 /* Look for the optional nested-name-specifier. */
10187 cp_parser_nested_name_specifier_opt (parser,
10188 /*typename_keyword_p=*/false,
10189 /*check_dependency_p=*/true,
a668c6ad
MM
10190 /*type_p=*/false,
10191 /*is_declaration=*/true);
a723baf1
MM
10192
10193 return cp_parser_namespace_name (parser);
10194}
10195
10196/* Parse a using-declaration.
10197
10198 using-declaration:
10199 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10200 using :: unqualified-id ; */
10201
10202static void
94edc4ab 10203cp_parser_using_declaration (cp_parser* parser)
a723baf1
MM
10204{
10205 cp_token *token;
10206 bool typename_p = false;
10207 bool global_scope_p;
10208 tree decl;
10209 tree identifier;
ed5f054f 10210 tree qscope;
a723baf1
MM
10211
10212 /* Look for the `using' keyword. */
10213 cp_parser_require_keyword (parser, RID_USING, "`using'");
21526606 10214
a723baf1
MM
10215 /* Peek at the next token. */
10216 token = cp_lexer_peek_token (parser->lexer);
10217 /* See if it's `typename'. */
10218 if (token->keyword == RID_TYPENAME)
10219 {
10220 /* Remember that we've seen it. */
10221 typename_p = true;
10222 /* Consume the `typename' token. */
10223 cp_lexer_consume_token (parser->lexer);
10224 }
10225
10226 /* Look for the optional global scope qualification. */
21526606 10227 global_scope_p
a723baf1 10228 = (cp_parser_global_scope_opt (parser,
21526606 10229 /*current_scope_valid_p=*/false)
a723baf1
MM
10230 != NULL_TREE);
10231
10232 /* If we saw `typename', or didn't see `::', then there must be a
10233 nested-name-specifier present. */
10234 if (typename_p || !global_scope_p)
21526606 10235 qscope = cp_parser_nested_name_specifier (parser, typename_p,
ed5f054f
AO
10236 /*check_dependency_p=*/true,
10237 /*type_p=*/false,
10238 /*is_declaration=*/true);
a723baf1
MM
10239 /* Otherwise, we could be in either of the two productions. In that
10240 case, treat the nested-name-specifier as optional. */
10241 else
ed5f054f
AO
10242 qscope = cp_parser_nested_name_specifier_opt (parser,
10243 /*typename_keyword_p=*/false,
10244 /*check_dependency_p=*/true,
10245 /*type_p=*/false,
10246 /*is_declaration=*/true);
10247 if (!qscope)
10248 qscope = global_namespace;
a723baf1
MM
10249
10250 /* Parse the unqualified-id. */
21526606 10251 identifier = cp_parser_unqualified_id (parser,
a723baf1 10252 /*template_keyword_p=*/false,
f3c2dfc6
MM
10253 /*check_dependency_p=*/true,
10254 /*declarator_p=*/true);
a723baf1
MM
10255
10256 /* The function we call to handle a using-declaration is different
10257 depending on what scope we are in. */
f3c2dfc6
MM
10258 if (identifier == error_mark_node)
10259 ;
10260 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10261 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10262 /* [namespace.udecl]
10263
10264 A using declaration shall not name a template-id. */
10265 error ("a template-id may not appear in a using-declaration");
a723baf1
MM
10266 else
10267 {
a5201a91 10268 if (at_class_scope_p ())
4eb6d609 10269 {
f3c2dfc6 10270 /* Create the USING_DECL. */
1d786913 10271 decl = do_class_using_decl (parser->scope, identifier);
f3c2dfc6
MM
10272 /* Add it to the list of members in this class. */
10273 finish_member_declaration (decl);
4eb6d609 10274 }
a723baf1 10275 else
f3c2dfc6
MM
10276 {
10277 decl = cp_parser_lookup_name_simple (parser, identifier);
10278 if (decl == error_mark_node)
4bb8ca28 10279 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
a5201a91 10280 else if (!at_namespace_scope_p ())
ed5f054f 10281 do_local_using_decl (decl, qscope, identifier);
f3c2dfc6 10282 else
ed5f054f 10283 do_toplevel_using_decl (decl, qscope, identifier);
f3c2dfc6 10284 }
a723baf1
MM
10285 }
10286
10287 /* Look for the final `;'. */
10288 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10289}
10290
21526606
EC
10291/* Parse a using-directive.
10292
a723baf1
MM
10293 using-directive:
10294 using namespace :: [opt] nested-name-specifier [opt]
10295 namespace-name ; */
10296
10297static void
94edc4ab 10298cp_parser_using_directive (cp_parser* parser)
a723baf1
MM
10299{
10300 tree namespace_decl;
86098eb8 10301 tree attribs;
a723baf1
MM
10302
10303 /* Look for the `using' keyword. */
10304 cp_parser_require_keyword (parser, RID_USING, "`using'");
10305 /* And the `namespace' keyword. */
10306 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10307 /* Look for the optional `::' operator. */
10308 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
34cd5ae7 10309 /* And the optional nested-name-specifier. */
a723baf1
MM
10310 cp_parser_nested_name_specifier_opt (parser,
10311 /*typename_keyword_p=*/false,
10312 /*check_dependency_p=*/true,
a668c6ad
MM
10313 /*type_p=*/false,
10314 /*is_declaration=*/true);
a723baf1
MM
10315 /* Get the namespace being used. */
10316 namespace_decl = cp_parser_namespace_name (parser);
86098eb8
JM
10317 /* And any specified attributes. */
10318 attribs = cp_parser_attributes_opt (parser);
a723baf1 10319 /* Update the symbol table. */
86098eb8 10320 parse_using_directive (namespace_decl, attribs);
a723baf1
MM
10321 /* Look for the final `;'. */
10322 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10323}
10324
10325/* Parse an asm-definition.
10326
10327 asm-definition:
21526606 10328 asm ( string-literal ) ;
a723baf1
MM
10329
10330 GNU Extension:
10331
10332 asm-definition:
10333 asm volatile [opt] ( string-literal ) ;
10334 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10335 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10336 : asm-operand-list [opt] ) ;
21526606
EC
10337 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10338 : asm-operand-list [opt]
a723baf1
MM
10339 : asm-operand-list [opt] ) ; */
10340
10341static void
94edc4ab 10342cp_parser_asm_definition (cp_parser* parser)
a723baf1 10343{
a723baf1
MM
10344 tree string;
10345 tree outputs = NULL_TREE;
10346 tree inputs = NULL_TREE;
10347 tree clobbers = NULL_TREE;
10348 tree asm_stmt;
10349 bool volatile_p = false;
10350 bool extended_p = false;
10351
10352 /* Look for the `asm' keyword. */
10353 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10354 /* See if the next token is `volatile'. */
10355 if (cp_parser_allow_gnu_extensions_p (parser)
10356 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10357 {
10358 /* Remember that we saw the `volatile' keyword. */
10359 volatile_p = true;
10360 /* Consume the token. */
10361 cp_lexer_consume_token (parser->lexer);
10362 }
10363 /* Look for the opening `('. */
c162c75e
MA
10364 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10365 return;
a723baf1 10366 /* Look for the string. */
c162c75e
MA
10367 string = cp_parser_string_literal (parser, false, false);
10368 if (string == error_mark_node)
10369 {
10370 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10371 /*consume_paren=*/true);
10372 return;
10373 }
10374
a723baf1 10375 /* If we're allowing GNU extensions, check for the extended assembly
21526606 10376 syntax. Unfortunately, the `:' tokens need not be separated by
a723baf1
MM
10377 a space in C, and so, for compatibility, we tolerate that here
10378 too. Doing that means that we have to treat the `::' operator as
10379 two `:' tokens. */
10380 if (cp_parser_allow_gnu_extensions_p (parser)
10381 && at_function_scope_p ()
10382 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10383 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10384 {
10385 bool inputs_p = false;
10386 bool clobbers_p = false;
10387
10388 /* The extended syntax was used. */
10389 extended_p = true;
10390
10391 /* Look for outputs. */
10392 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10393 {
10394 /* Consume the `:'. */
10395 cp_lexer_consume_token (parser->lexer);
10396 /* Parse the output-operands. */
21526606 10397 if (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
10398 CPP_COLON)
10399 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
10400 CPP_SCOPE)
10401 && cp_lexer_next_token_is_not (parser->lexer,
10402 CPP_CLOSE_PAREN))
a723baf1
MM
10403 outputs = cp_parser_asm_operand_list (parser);
10404 }
10405 /* If the next token is `::', there are no outputs, and the
10406 next token is the beginning of the inputs. */
10407 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
bf5930d4
JB
10408 /* The inputs are coming next. */
10409 inputs_p = true;
a723baf1
MM
10410
10411 /* Look for inputs. */
10412 if (inputs_p
10413 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10414 {
bf5930d4
JB
10415 /* Consume the `:' or `::'. */
10416 cp_lexer_consume_token (parser->lexer);
a723baf1 10417 /* Parse the output-operands. */
21526606 10418 if (cp_lexer_next_token_is_not (parser->lexer,
a723baf1 10419 CPP_COLON)
8caf4c38
MM
10420 && cp_lexer_next_token_is_not (parser->lexer,
10421 CPP_CLOSE_PAREN))
a723baf1
MM
10422 inputs = cp_parser_asm_operand_list (parser);
10423 }
10424 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10425 /* The clobbers are coming next. */
10426 clobbers_p = true;
10427
10428 /* Look for clobbers. */
21526606 10429 if (clobbers_p
a723baf1
MM
10430 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10431 {
bf5930d4
JB
10432 /* Consume the `:' or `::'. */
10433 cp_lexer_consume_token (parser->lexer);
a723baf1 10434 /* Parse the clobbers. */
8caf4c38
MM
10435 if (cp_lexer_next_token_is_not (parser->lexer,
10436 CPP_CLOSE_PAREN))
10437 clobbers = cp_parser_asm_clobber_list (parser);
a723baf1
MM
10438 }
10439 }
10440 /* Look for the closing `)'. */
10441 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
10442 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10443 /*consume_paren=*/true);
a723baf1
MM
10444 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10445
e130a54b 10446 /* Create the ASM_EXPR. */
a723baf1
MM
10447 if (at_function_scope_p ())
10448 {
6de9cd9a
DN
10449 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10450 inputs, clobbers);
e130a54b 10451 /* If the extended syntax was not used, mark the ASM_EXPR. */
a723baf1 10452 if (!extended_p)
ca059043
AP
10453 {
10454 tree temp = asm_stmt;
10455 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10456 temp = TREE_OPERAND (temp, 0);
10457
10458 ASM_INPUT_P (temp) = 1;
10459 }
a723baf1
MM
10460 }
10461 else
10462 assemble_asm (string);
10463}
10464
10465/* Declarators [gram.dcl.decl] */
10466
10467/* Parse an init-declarator.
10468
10469 init-declarator:
10470 declarator initializer [opt]
10471
10472 GNU Extension:
10473
10474 init-declarator:
10475 declarator asm-specification [opt] attributes [opt] initializer [opt]
10476
4bb8ca28
MM
10477 function-definition:
10478 decl-specifier-seq [opt] declarator ctor-initializer [opt]
21526606
EC
10479 function-body
10480 decl-specifier-seq [opt] declarator function-try-block
4bb8ca28
MM
10481
10482 GNU Extension:
10483
10484 function-definition:
21526606 10485 __extension__ function-definition
4bb8ca28 10486
a723baf1 10487 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
c8e4f0e9 10488 Returns a representation of the entity declared. If MEMBER_P is TRUE,
cf22909c
KL
10489 then this declarator appears in a class scope. The new DECL created
10490 by this declarator is returned.
a723baf1
MM
10491
10492 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10493 for a function-definition here as well. If the declarator is a
10494 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10495 be TRUE upon return. By that point, the function-definition will
10496 have been completely parsed.
10497
10498 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10499 is FALSE. */
10500
10501static tree
21526606 10502cp_parser_init_declarator (cp_parser* parser,
62d1db17 10503 cp_decl_specifier_seq *decl_specifiers,
94edc4ab
NN
10504 bool function_definition_allowed_p,
10505 bool member_p,
560ad596 10506 int declares_class_or_enum,
94edc4ab 10507 bool* function_definition_p)
a723baf1
MM
10508{
10509 cp_token *token;
058b15c1 10510 cp_declarator *declarator;
62d1db17 10511 tree prefix_attributes;
a723baf1
MM
10512 tree attributes;
10513 tree asm_specification;
10514 tree initializer;
10515 tree decl = NULL_TREE;
10516 tree scope;
a723baf1
MM
10517 bool is_initialized;
10518 bool is_parenthesized_init;
39703eb9 10519 bool is_non_constant_init;
7efa3e22 10520 int ctor_dtor_or_conv_p;
a723baf1 10521 bool friend_p;
4514aa8c 10522 tree pushed_scope = NULL;
a723baf1 10523
62d1db17
MM
10524 /* Gather the attributes that were provided with the
10525 decl-specifiers. */
10526 prefix_attributes = decl_specifiers->attributes;
62d1db17 10527
a723baf1
MM
10528 /* Assume that this is not the declarator for a function
10529 definition. */
10530 if (function_definition_p)
10531 *function_definition_p = false;
10532
10533 /* Defer access checks while parsing the declarator; we cannot know
21526606 10534 what names are accessible until we know what is being
a723baf1 10535 declared. */
cf22909c
KL
10536 resume_deferring_access_checks ();
10537
a723baf1 10538 /* Parse the declarator. */
21526606 10539 declarator
62b8a44e 10540 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 10541 &ctor_dtor_or_conv_p,
db86dd14
MM
10542 /*parenthesized_p=*/NULL,
10543 /*member_p=*/false);
a723baf1 10544 /* Gather up the deferred checks. */
cf22909c 10545 stop_deferring_access_checks ();
24c0ef37 10546
a723baf1
MM
10547 /* If the DECLARATOR was erroneous, there's no need to go
10548 further. */
058b15c1 10549 if (declarator == cp_error_declarator)
cf22909c 10550 return error_mark_node;
a723baf1 10551
fc6a28d7
MM
10552 if (declares_class_or_enum & 2)
10553 cp_parser_check_for_definition_in_return_type (declarator,
10554 decl_specifiers->type);
560ad596 10555
a723baf1
MM
10556 /* Figure out what scope the entity declared by the DECLARATOR is
10557 located in. `grokdeclarator' sometimes changes the scope, so
10558 we compute it now. */
10559 scope = get_scope_of_declarator (declarator);
10560
10561 /* If we're allowing GNU extensions, look for an asm-specification
10562 and attributes. */
10563 if (cp_parser_allow_gnu_extensions_p (parser))
10564 {
10565 /* Look for an asm-specification. */
10566 asm_specification = cp_parser_asm_specification_opt (parser);
10567 /* And attributes. */
10568 attributes = cp_parser_attributes_opt (parser);
10569 }
10570 else
10571 {
10572 asm_specification = NULL_TREE;
10573 attributes = NULL_TREE;
10574 }
10575
10576 /* Peek at the next token. */
10577 token = cp_lexer_peek_token (parser->lexer);
10578 /* Check to see if the token indicates the start of a
10579 function-definition. */
10580 if (cp_parser_token_starts_function_definition_p (token))
10581 {
10582 if (!function_definition_allowed_p)
10583 {
10584 /* If a function-definition should not appear here, issue an
10585 error message. */
10586 cp_parser_error (parser,
10587 "a function-definition is not allowed here");
10588 return error_mark_node;
10589 }
10590 else
10591 {
a723baf1
MM
10592 /* Neither attributes nor an asm-specification are allowed
10593 on a function-definition. */
10594 if (asm_specification)
10595 error ("an asm-specification is not allowed on a function-definition");
10596 if (attributes)
10597 error ("attributes are not allowed on a function-definition");
10598 /* This is a function-definition. */
10599 *function_definition_p = true;
10600
a723baf1 10601 /* Parse the function definition. */
4bb8ca28
MM
10602 if (member_p)
10603 decl = cp_parser_save_member_function_body (parser,
10604 decl_specifiers,
10605 declarator,
10606 prefix_attributes);
10607 else
21526606 10608 decl
4bb8ca28
MM
10609 = (cp_parser_function_definition_from_specifiers_and_declarator
10610 (parser, decl_specifiers, prefix_attributes, declarator));
24c0ef37 10611
a723baf1
MM
10612 return decl;
10613 }
10614 }
10615
10616 /* [dcl.dcl]
10617
10618 Only in function declarations for constructors, destructors, and
21526606 10619 type conversions can the decl-specifier-seq be omitted.
a723baf1
MM
10620
10621 We explicitly postpone this check past the point where we handle
10622 function-definitions because we tolerate function-definitions
10623 that are missing their return types in some modes. */
62d1db17 10624 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
a723baf1 10625 {
21526606 10626 cp_parser_error (parser,
a723baf1
MM
10627 "expected constructor, destructor, or type conversion");
10628 return error_mark_node;
10629 }
10630
10631 /* An `=' or an `(' indicates an initializer. */
21526606 10632 is_initialized = (token->type == CPP_EQ
a723baf1
MM
10633 || token->type == CPP_OPEN_PAREN);
10634 /* If the init-declarator isn't initialized and isn't followed by a
10635 `,' or `;', it's not a valid init-declarator. */
21526606 10636 if (!is_initialized
a723baf1
MM
10637 && token->type != CPP_COMMA
10638 && token->type != CPP_SEMICOLON)
10639 {
996c2b52 10640 cp_parser_error (parser, "expected initializer");
a723baf1
MM
10641 return error_mark_node;
10642 }
10643
10644 /* Because start_decl has side-effects, we should only call it if we
10645 know we're going ahead. By this point, we know that we cannot
10646 possibly be looking at any other construct. */
10647 cp_parser_commit_to_tentative_parse (parser);
10648
e90c7b84
ILT
10649 /* If the decl specifiers were bad, issue an error now that we're
10650 sure this was intended to be a declarator. Then continue
10651 declaring the variable(s), as int, to try to cut down on further
10652 errors. */
62d1db17
MM
10653 if (decl_specifiers->any_specifiers_p
10654 && decl_specifiers->type == error_mark_node)
e90c7b84
ILT
10655 {
10656 cp_parser_error (parser, "invalid type in declaration");
62d1db17 10657 decl_specifiers->type = integer_type_node;
e90c7b84
ILT
10658 }
10659
a723baf1
MM
10660 /* Check to see whether or not this declaration is a friend. */
10661 friend_p = cp_parser_friend_p (decl_specifiers);
10662
10663 /* Check that the number of template-parameter-lists is OK. */
ee3071ef 10664 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
cf22909c 10665 return error_mark_node;
a723baf1
MM
10666
10667 /* Enter the newly declared entry in the symbol table. If we're
10668 processing a declaration in a class-specifier, we wait until
10669 after processing the initializer. */
10670 if (!member_p)
10671 {
10672 if (parser->in_unbraced_linkage_specification_p)
10673 {
62d1db17 10674 decl_specifiers->storage_class = sc_extern;
a723baf1
MM
10675 have_extern_spec = false;
10676 }
ee3071ef 10677 decl = start_decl (declarator, decl_specifiers,
73a8adb6 10678 is_initialized, attributes, prefix_attributes,
4514aa8c 10679 &pushed_scope);
a723baf1 10680 }
73a8adb6
MM
10681 else if (scope)
10682 /* Enter the SCOPE. That way unqualified names appearing in the
10683 initializer will be looked up in SCOPE. */
4514aa8c 10684 pushed_scope = push_scope (scope);
a723baf1
MM
10685
10686 /* Perform deferred access control checks, now that we know in which
10687 SCOPE the declared entity resides. */
21526606 10688 if (!member_p && decl)
a723baf1
MM
10689 {
10690 tree saved_current_function_decl = NULL_TREE;
10691
10692 /* If the entity being declared is a function, pretend that we
10693 are in its scope. If it is a `friend', it may have access to
9bcb9aae 10694 things that would not otherwise be accessible. */
a723baf1
MM
10695 if (TREE_CODE (decl) == FUNCTION_DECL)
10696 {
10697 saved_current_function_decl = current_function_decl;
10698 current_function_decl = decl;
10699 }
21526606 10700
cf22909c
KL
10701 /* Perform the access control checks for the declarator and the
10702 the decl-specifiers. */
10703 perform_deferred_access_checks ();
a723baf1
MM
10704
10705 /* Restore the saved value. */
10706 if (TREE_CODE (decl) == FUNCTION_DECL)
10707 current_function_decl = saved_current_function_decl;
10708 }
10709
10710 /* Parse the initializer. */
10711 if (is_initialized)
21526606 10712 initializer = cp_parser_initializer (parser,
39703eb9
MM
10713 &is_parenthesized_init,
10714 &is_non_constant_init);
a723baf1
MM
10715 else
10716 {
10717 initializer = NULL_TREE;
10718 is_parenthesized_init = false;
39703eb9 10719 is_non_constant_init = true;
a723baf1
MM
10720 }
10721
10722 /* The old parser allows attributes to appear after a parenthesized
10723 initializer. Mark Mitchell proposed removing this functionality
10724 on the GCC mailing lists on 2002-08-13. This parser accepts the
10725 attributes -- but ignores them. */
10726 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10727 if (cp_parser_attributes_opt (parser))
10728 warning ("attributes after parenthesized initializer ignored");
10729
a723baf1
MM
10730 /* For an in-class declaration, use `grokfield' to create the
10731 declaration. */
10732 if (member_p)
8db1028e 10733 {
4514aa8c 10734 if (pushed_scope)
62a4d942 10735 {
4514aa8c
NS
10736 pop_scope (pushed_scope);
10737 pushed_scope = false;
62a4d942 10738 }
8db1028e
NS
10739 decl = grokfield (declarator, decl_specifiers,
10740 initializer, /*asmspec=*/NULL_TREE,
a723baf1 10741 /*attributes=*/NULL_TREE);
8db1028e
NS
10742 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10743 cp_parser_save_default_args (parser, decl);
10744 }
21526606 10745
a723baf1
MM
10746 /* Finish processing the declaration. But, skip friend
10747 declarations. */
550205c3 10748 if (!friend_p && decl && decl != error_mark_node)
73a8adb6
MM
10749 {
10750 cp_finish_decl (decl,
10751 initializer,
10752 asm_specification,
10753 /* If the initializer is in parentheses, then this is
10754 a direct-initialization, which means that an
10755 `explicit' constructor is OK. Otherwise, an
10756 `explicit' constructor cannot be used. */
10757 ((is_parenthesized_init || !is_initialized)
a723baf1 10758 ? 0 : LOOKUP_ONLYCONVERTING));
73a8adb6 10759 }
4514aa8c
NS
10760 if (!friend_p && pushed_scope)
10761 pop_scope (pushed_scope);
a723baf1 10762
39703eb9
MM
10763 /* Remember whether or not variables were initialized by
10764 constant-expressions. */
21526606 10765 if (decl && TREE_CODE (decl) == VAR_DECL
39703eb9
MM
10766 && is_initialized && !is_non_constant_init)
10767 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10768
a723baf1
MM
10769 return decl;
10770}
10771
10772/* Parse a declarator.
21526606 10773
a723baf1
MM
10774 declarator:
10775 direct-declarator
21526606 10776 ptr-operator declarator
a723baf1
MM
10777
10778 abstract-declarator:
10779 ptr-operator abstract-declarator [opt]
10780 direct-abstract-declarator
10781
10782 GNU Extensions:
10783
10784 declarator:
10785 attributes [opt] direct-declarator
21526606 10786 attributes [opt] ptr-operator declarator
a723baf1
MM
10787
10788 abstract-declarator:
10789 attributes [opt] ptr-operator abstract-declarator [opt]
10790 attributes [opt] direct-abstract-declarator
21526606 10791
7efa3e22
NS
10792 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10793 detect constructor, destructor or conversion operators. It is set
10794 to -1 if the declarator is a name, and +1 if it is a
10795 function. Otherwise it is set to zero. Usually you just want to
10796 test for >0, but internally the negative value is used.
21526606 10797
a723baf1
MM
10798 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10799 a decl-specifier-seq unless it declares a constructor, destructor,
10800 or conversion. It might seem that we could check this condition in
10801 semantic analysis, rather than parsing, but that makes it difficult
10802 to handle something like `f()'. We want to notice that there are
10803 no decl-specifiers, and therefore realize that this is an
21526606
EC
10804 expression, not a declaration.)
10805
4bb8ca28 10806 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
db86dd14
MM
10807 the declarator is a direct-declarator of the form "(...)".
10808
10809 MEMBER_P is true iff this declarator is a member-declarator. */
a723baf1 10810
058b15c1 10811static cp_declarator *
21526606
EC
10812cp_parser_declarator (cp_parser* parser,
10813 cp_parser_declarator_kind dcl_kind,
4bb8ca28 10814 int* ctor_dtor_or_conv_p,
db86dd14
MM
10815 bool* parenthesized_p,
10816 bool member_p)
a723baf1
MM
10817{
10818 cp_token *token;
058b15c1 10819 cp_declarator *declarator;
a723baf1 10820 enum tree_code code;
3c01e5df 10821 cp_cv_quals cv_quals;
a723baf1
MM
10822 tree class_type;
10823 tree attributes = NULL_TREE;
10824
10825 /* Assume this is not a constructor, destructor, or type-conversion
10826 operator. */
10827 if (ctor_dtor_or_conv_p)
7efa3e22 10828 *ctor_dtor_or_conv_p = 0;
a723baf1
MM
10829
10830 if (cp_parser_allow_gnu_extensions_p (parser))
10831 attributes = cp_parser_attributes_opt (parser);
21526606 10832
a723baf1
MM
10833 /* Peek at the next token. */
10834 token = cp_lexer_peek_token (parser->lexer);
21526606 10835
a723baf1
MM
10836 /* Check for the ptr-operator production. */
10837 cp_parser_parse_tentatively (parser);
10838 /* Parse the ptr-operator. */
21526606
EC
10839 code = cp_parser_ptr_operator (parser,
10840 &class_type,
3c01e5df 10841 &cv_quals);
a723baf1
MM
10842 /* If that worked, then we have a ptr-operator. */
10843 if (cp_parser_parse_definitely (parser))
10844 {
4bb8ca28
MM
10845 /* If a ptr-operator was found, then this declarator was not
10846 parenthesized. */
10847 if (parenthesized_p)
10848 *parenthesized_p = true;
a723baf1
MM
10849 /* The dependent declarator is optional if we are parsing an
10850 abstract-declarator. */
62b8a44e 10851 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1
MM
10852 cp_parser_parse_tentatively (parser);
10853
10854 /* Parse the dependent declarator. */
62b8a44e 10855 declarator = cp_parser_declarator (parser, dcl_kind,
4bb8ca28 10856 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
10857 /*parenthesized_p=*/NULL,
10858 /*member_p=*/false);
a723baf1
MM
10859
10860 /* If we are parsing an abstract-declarator, we must handle the
10861 case where the dependent declarator is absent. */
62b8a44e
NS
10862 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10863 && !cp_parser_parse_definitely (parser))
058b15c1 10864 declarator = NULL;
21526606 10865
a723baf1 10866 /* Build the representation of the ptr-operator. */
058b15c1 10867 if (class_type)
3c01e5df 10868 declarator = make_ptrmem_declarator (cv_quals,
058b15c1
MM
10869 class_type,
10870 declarator);
10871 else if (code == INDIRECT_REF)
3c01e5df 10872 declarator = make_pointer_declarator (cv_quals, declarator);
a723baf1 10873 else
3c01e5df 10874 declarator = make_reference_declarator (cv_quals, declarator);
a723baf1
MM
10875 }
10876 /* Everything else is a direct-declarator. */
10877 else
4bb8ca28
MM
10878 {
10879 if (parenthesized_p)
10880 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10881 CPP_OPEN_PAREN);
10882 declarator = cp_parser_direct_declarator (parser, dcl_kind,
db86dd14
MM
10883 ctor_dtor_or_conv_p,
10884 member_p);
4bb8ca28 10885 }
a723baf1 10886
058b15c1
MM
10887 if (attributes && declarator != cp_error_declarator)
10888 declarator->attributes = attributes;
21526606 10889
a723baf1
MM
10890 return declarator;
10891}
10892
10893/* Parse a direct-declarator or direct-abstract-declarator.
10894
10895 direct-declarator:
10896 declarator-id
10897 direct-declarator ( parameter-declaration-clause )
21526606 10898 cv-qualifier-seq [opt]
a723baf1
MM
10899 exception-specification [opt]
10900 direct-declarator [ constant-expression [opt] ]
21526606 10901 ( declarator )
a723baf1
MM
10902
10903 direct-abstract-declarator:
10904 direct-abstract-declarator [opt]
21526606 10905 ( parameter-declaration-clause )
a723baf1
MM
10906 cv-qualifier-seq [opt]
10907 exception-specification [opt]
10908 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10909 ( abstract-declarator )
10910
62b8a44e
NS
10911 Returns a representation of the declarator. DCL_KIND is
10912 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10913 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10914 we are parsing a direct-declarator. It is
10915 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10916 of ambiguity we prefer an abstract declarator, as per
db86dd14 10917 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
058b15c1 10918 cp_parser_declarator. */
a723baf1 10919
058b15c1 10920static cp_declarator *
94edc4ab
NN
10921cp_parser_direct_declarator (cp_parser* parser,
10922 cp_parser_declarator_kind dcl_kind,
db86dd14
MM
10923 int* ctor_dtor_or_conv_p,
10924 bool member_p)
a723baf1
MM
10925{
10926 cp_token *token;
058b15c1 10927 cp_declarator *declarator = NULL;
a723baf1
MM
10928 tree scope = NULL_TREE;
10929 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10930 bool saved_in_declarator_p = parser->in_declarator_p;
62b8a44e 10931 bool first = true;
4514aa8c 10932 tree pushed_scope = NULL_TREE;
21526606 10933
62b8a44e 10934 while (true)
a723baf1 10935 {
62b8a44e
NS
10936 /* Peek at the next token. */
10937 token = cp_lexer_peek_token (parser->lexer);
10938 if (token->type == CPP_OPEN_PAREN)
a723baf1 10939 {
62b8a44e
NS
10940 /* This is either a parameter-declaration-clause, or a
10941 parenthesized declarator. When we know we are parsing a
34cd5ae7 10942 named declarator, it must be a parenthesized declarator
62b8a44e
NS
10943 if FIRST is true. For instance, `(int)' is a
10944 parameter-declaration-clause, with an omitted
10945 direct-abstract-declarator. But `((*))', is a
10946 parenthesized abstract declarator. Finally, when T is a
10947 template parameter `(T)' is a
34cd5ae7 10948 parameter-declaration-clause, and not a parenthesized
62b8a44e 10949 named declarator.
21526606 10950
62b8a44e
NS
10951 We first try and parse a parameter-declaration-clause,
10952 and then try a nested declarator (if FIRST is true).
a723baf1 10953
62b8a44e
NS
10954 It is not an error for it not to be a
10955 parameter-declaration-clause, even when FIRST is
10956 false. Consider,
10957
10958 int i (int);
10959 int i (3);
10960
10961 The first is the declaration of a function while the
10962 second is a the definition of a variable, including its
10963 initializer.
10964
10965 Having seen only the parenthesis, we cannot know which of
10966 these two alternatives should be selected. Even more
10967 complex are examples like:
10968
10969 int i (int (a));
10970 int i (int (3));
10971
10972 The former is a function-declaration; the latter is a
21526606 10973 variable initialization.
62b8a44e 10974
34cd5ae7 10975 Thus again, we try a parameter-declaration-clause, and if
62b8a44e
NS
10976 that fails, we back out and return. */
10977
10978 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1 10979 {
058b15c1 10980 cp_parameter_declarator *params;
4047b164 10981 unsigned saved_num_template_parameter_lists;
21526606 10982
db86dd14
MM
10983 /* In a member-declarator, the only valid interpretation
10984 of a parenthesis is the start of a
10985 parameter-declaration-clause. (It is invalid to
10986 initialize a static data member with a parenthesized
10987 initializer; only the "=" form of initialization is
10988 permitted.) */
10989 if (!member_p)
10990 cp_parser_parse_tentatively (parser);
a723baf1 10991
62b8a44e
NS
10992 /* Consume the `('. */
10993 cp_lexer_consume_token (parser->lexer);
10994 if (first)
10995 {
10996 /* If this is going to be an abstract declarator, we're
10997 in a declarator and we can't have default args. */
10998 parser->default_arg_ok_p = false;
10999 parser->in_declarator_p = true;
11000 }
21526606 11001
4047b164
KL
11002 /* Inside the function parameter list, surrounding
11003 template-parameter-lists do not apply. */
11004 saved_num_template_parameter_lists
11005 = parser->num_template_parameter_lists;
11006 parser->num_template_parameter_lists = 0;
11007
62b8a44e
NS
11008 /* Parse the parameter-declaration-clause. */
11009 params = cp_parser_parameter_declaration_clause (parser);
11010
4047b164
KL
11011 parser->num_template_parameter_lists
11012 = saved_num_template_parameter_lists;
11013
62b8a44e 11014 /* If all went well, parse the cv-qualifier-seq and the
34cd5ae7 11015 exception-specification. */
db86dd14 11016 if (member_p || cp_parser_parse_definitely (parser))
62b8a44e 11017 {
3c01e5df 11018 cp_cv_quals cv_quals;
62b8a44e 11019 tree exception_specification;
7efa3e22
NS
11020
11021 if (ctor_dtor_or_conv_p)
11022 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
62b8a44e
NS
11023 first = false;
11024 /* Consume the `)'. */
11025 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11026
11027 /* Parse the cv-qualifier-seq. */
3c01e5df 11028 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
62b8a44e 11029 /* And the exception-specification. */
21526606 11030 exception_specification
62b8a44e
NS
11031 = cp_parser_exception_specification_opt (parser);
11032
11033 /* Create the function-declarator. */
11034 declarator = make_call_declarator (declarator,
11035 params,
3c01e5df 11036 cv_quals,
62b8a44e
NS
11037 exception_specification);
11038 /* Any subsequent parameter lists are to do with
11039 return type, so are not those of the declared
11040 function. */
11041 parser->default_arg_ok_p = false;
21526606 11042
62b8a44e
NS
11043 /* Repeat the main loop. */
11044 continue;
11045 }
11046 }
21526606 11047
62b8a44e
NS
11048 /* If this is the first, we can try a parenthesized
11049 declarator. */
11050 if (first)
a723baf1 11051 {
a7324e75
MM
11052 bool saved_in_type_id_in_expr_p;
11053
a723baf1 11054 parser->default_arg_ok_p = saved_default_arg_ok_p;
62b8a44e 11055 parser->in_declarator_p = saved_in_declarator_p;
21526606 11056
62b8a44e
NS
11057 /* Consume the `('. */
11058 cp_lexer_consume_token (parser->lexer);
11059 /* Parse the nested declarator. */
a7324e75
MM
11060 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11061 parser->in_type_id_in_expr_p = true;
21526606 11062 declarator
4bb8ca28 11063 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
db86dd14
MM
11064 /*parenthesized_p=*/NULL,
11065 member_p);
a7324e75 11066 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
62b8a44e
NS
11067 first = false;
11068 /* Expect a `)'. */
11069 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
058b15c1
MM
11070 declarator = cp_error_declarator;
11071 if (declarator == cp_error_declarator)
62b8a44e 11072 break;
21526606 11073
62b8a44e 11074 goto handle_declarator;
a723baf1 11075 }
9bcb9aae 11076 /* Otherwise, we must be done. */
62b8a44e
NS
11077 else
11078 break;
a723baf1 11079 }
62b8a44e
NS
11080 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11081 && token->type == CPP_OPEN_SQUARE)
a723baf1 11082 {
62b8a44e 11083 /* Parse an array-declarator. */
a723baf1
MM
11084 tree bounds;
11085
7efa3e22
NS
11086 if (ctor_dtor_or_conv_p)
11087 *ctor_dtor_or_conv_p = 0;
21526606 11088
62b8a44e
NS
11089 first = false;
11090 parser->default_arg_ok_p = false;
11091 parser->in_declarator_p = true;
a723baf1
MM
11092 /* Consume the `['. */
11093 cp_lexer_consume_token (parser->lexer);
11094 /* Peek at the next token. */
11095 token = cp_lexer_peek_token (parser->lexer);
11096 /* If the next token is `]', then there is no
11097 constant-expression. */
11098 if (token->type != CPP_CLOSE_SQUARE)
14d22dd6
MM
11099 {
11100 bool non_constant_p;
11101
21526606 11102 bounds
14d22dd6
MM
11103 = cp_parser_constant_expression (parser,
11104 /*allow_non_constant=*/true,
11105 &non_constant_p);
d17811fd 11106 if (!non_constant_p)
9baa27a9 11107 bounds = fold_non_dependent_expr (bounds);
88e95ee3
MM
11108 /* Normally, the array bound must be an integral constant
11109 expression. However, as an extension, we allow VLAs
11110 in function scopes. And, we allow type-dependent
11111 expressions in templates; sometimes we don't know for
11112 sure whether or not something is a valid integral
11113 constant expression until instantiation time. (It
11114 doesn't make sense to check for value-dependency, as
11115 an expression is only value-dependent when it is a
11116 constant expression.) */
11117 else if (!type_dependent_expression_p (bounds)
11118 && !at_function_scope_p ())
44370687
MM
11119 {
11120 error ("array bound is not an integer constant");
11121 bounds = error_mark_node;
11122 }
14d22dd6 11123 }
a723baf1
MM
11124 else
11125 bounds = NULL_TREE;
11126 /* Look for the closing `]'. */
62b8a44e
NS
11127 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11128 {
058b15c1 11129 declarator = cp_error_declarator;
62b8a44e
NS
11130 break;
11131 }
a723baf1 11132
058b15c1 11133 declarator = make_array_declarator (declarator, bounds);
a723baf1 11134 }
62b8a44e 11135 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
a723baf1 11136 {
1d786913
MM
11137 tree qualifying_scope;
11138 tree unqualified_name;
058b15c1 11139
a668c6ad 11140 /* Parse a declarator-id */
62b8a44e
NS
11141 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11142 cp_parser_parse_tentatively (parser);
1d786913
MM
11143 unqualified_name = cp_parser_declarator_id (parser);
11144 qualifying_scope = parser->scope;
712becab
NS
11145 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11146 {
11147 if (!cp_parser_parse_definitely (parser))
1d786913
MM
11148 unqualified_name = error_mark_node;
11149 else if (qualifying_scope
11150 || (TREE_CODE (unqualified_name)
11151 != IDENTIFIER_NODE))
712becab
NS
11152 {
11153 cp_parser_error (parser, "expected unqualified-id");
1d786913 11154 unqualified_name = error_mark_node;
712becab
NS
11155 }
11156 }
21526606 11157
1d786913 11158 if (unqualified_name == error_mark_node)
058b15c1
MM
11159 {
11160 declarator = cp_error_declarator;
11161 break;
11162 }
21526606 11163
1d786913
MM
11164 if (qualifying_scope && at_namespace_scope_p ()
11165 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
62b8a44e 11166 {
62b8a44e
NS
11167 /* In the declaration of a member of a template class
11168 outside of the class itself, the SCOPE will sometimes
11169 be a TYPENAME_TYPE. For example, given:
21526606 11170
62b8a44e
NS
11171 template <typename T>
11172 int S<T>::R::i = 3;
21526606 11173
62b8a44e
NS
11174 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11175 this context, we must resolve S<T>::R to an ordinary
11176 type, rather than a typename type.
21526606 11177
62b8a44e
NS
11178 The reason we normally avoid resolving TYPENAME_TYPEs
11179 is that a specialization of `S' might render
11180 `S<T>::R' not a type. However, if `S' is
11181 specialized, then this `i' will not be used, so there
11182 is no harm in resolving the types here. */
1d786913
MM
11183 tree type;
11184
11185 /* Resolve the TYPENAME_TYPE. */
11186 type = resolve_typename_type (qualifying_scope,
11187 /*only_current_p=*/false);
11188 /* If that failed, the declarator is invalid. */
11189 if (type == error_mark_node)
11190 error ("%<%T::%D%> is not a type",
11191 TYPE_CONTEXT (qualifying_scope),
11192 TYPE_IDENTIFIER (qualifying_scope));
11193 qualifying_scope = type;
62b8a44e 11194 }
21526606 11195
1d786913
MM
11196 declarator = make_id_declarator (qualifying_scope,
11197 unqualified_name);
11198 if (unqualified_name)
a723baf1 11199 {
62b8a44e
NS
11200 tree class_type;
11201
1d786913
MM
11202 if (qualifying_scope
11203 && CLASS_TYPE_P (qualifying_scope))
11204 class_type = qualifying_scope;
62b8a44e 11205 else
1d786913 11206 class_type = current_class_type;
62b8a44e 11207
058b15c1
MM
11208 if (class_type)
11209 {
11210 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11211 declarator->u.id.sfk = sfk_destructor;
11212 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11213 declarator->u.id.sfk = sfk_conversion;
27d6592c
MM
11214 else if (/* There's no way to declare a constructor
11215 for an anonymous type, even if the type
11216 got a name for linkage purposes. */
11217 !TYPE_WAS_ANONYMOUS (class_type)
11218 && (constructor_name_p (unqualified_name,
11219 class_type)
11220 || (TREE_CODE (unqualified_name) == TYPE_DECL
11221 && (same_type_p
11222 (TREE_TYPE (unqualified_name),
11223 class_type)))))
058b15c1
MM
11224 declarator->u.id.sfk = sfk_constructor;
11225
11226 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11227 *ctor_dtor_or_conv_p = -1;
1d786913 11228 if (qualifying_scope
98ca843c 11229 && TREE_CODE (unqualified_name) == TYPE_DECL
058b15c1
MM
11230 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11231 {
11232 error ("invalid use of constructor as a template");
2a13a625
GDR
11233 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11234 "the constructor in a qualified name",
11235 class_type,
058b15c1
MM
11236 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11237 class_type, class_type);
11238 }
11239 }
a723baf1 11240 }
62b8a44e
NS
11241
11242 handle_declarator:;
11243 scope = get_scope_of_declarator (declarator);
11244 if (scope)
91b004e5
MM
11245 /* Any names that appear after the declarator-id for a
11246 member are looked up in the containing scope. */
4514aa8c 11247 pushed_scope = push_scope (scope);
62b8a44e
NS
11248 parser->in_declarator_p = true;
11249 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
058b15c1 11250 || (declarator && declarator->kind == cdk_id))
62b8a44e
NS
11251 /* Default args are only allowed on function
11252 declarations. */
11253 parser->default_arg_ok_p = saved_default_arg_ok_p;
a723baf1 11254 else
62b8a44e
NS
11255 parser->default_arg_ok_p = false;
11256
11257 first = false;
a723baf1 11258 }
62b8a44e 11259 /* We're done. */
a723baf1
MM
11260 else
11261 break;
a723baf1
MM
11262 }
11263
11264 /* For an abstract declarator, we might wind up with nothing at this
11265 point. That's an error; the declarator is not optional. */
11266 if (!declarator)
11267 cp_parser_error (parser, "expected declarator");
11268
11269 /* If we entered a scope, we must exit it now. */
4514aa8c
NS
11270 if (pushed_scope)
11271 pop_scope (pushed_scope);
a723baf1
MM
11272
11273 parser->default_arg_ok_p = saved_default_arg_ok_p;
11274 parser->in_declarator_p = saved_in_declarator_p;
21526606 11275
a723baf1
MM
11276 return declarator;
11277}
11278
21526606 11279/* Parse a ptr-operator.
a723baf1
MM
11280
11281 ptr-operator:
11282 * cv-qualifier-seq [opt]
11283 &
11284 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11285
11286 GNU Extension:
11287
11288 ptr-operator:
11289 & cv-qualifier-seq [opt]
11290
3c01e5df
MM
11291 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11292 Returns ADDR_EXPR if a reference was used. In the case of a
11293 pointer-to-member, *TYPE is filled in with the TYPE containing the
11294 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11295 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11296 ERROR_MARK if an error occurred. */
21526606 11297
a723baf1 11298static enum tree_code
21526606
EC
11299cp_parser_ptr_operator (cp_parser* parser,
11300 tree* type,
3c01e5df 11301 cp_cv_quals *cv_quals)
a723baf1
MM
11302{
11303 enum tree_code code = ERROR_MARK;
11304 cp_token *token;
11305
11306 /* Assume that it's not a pointer-to-member. */
11307 *type = NULL_TREE;
11308 /* And that there are no cv-qualifiers. */
3c01e5df 11309 *cv_quals = TYPE_UNQUALIFIED;
a723baf1
MM
11310
11311 /* Peek at the next token. */
11312 token = cp_lexer_peek_token (parser->lexer);
11313 /* If it's a `*' or `&' we have a pointer or reference. */
11314 if (token->type == CPP_MULT || token->type == CPP_AND)
11315 {
11316 /* Remember which ptr-operator we were processing. */
11317 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11318
11319 /* Consume the `*' or `&'. */
11320 cp_lexer_consume_token (parser->lexer);
11321
11322 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11323 `&', if we are allowing GNU extensions. (The only qualifier
11324 that can legally appear after `&' is `restrict', but that is
11325 enforced during semantic analysis. */
21526606 11326 if (code == INDIRECT_REF
a723baf1 11327 || cp_parser_allow_gnu_extensions_p (parser))
3c01e5df 11328 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
a723baf1
MM
11329 }
11330 else
11331 {
11332 /* Try the pointer-to-member case. */
11333 cp_parser_parse_tentatively (parser);
11334 /* Look for the optional `::' operator. */
11335 cp_parser_global_scope_opt (parser,
11336 /*current_scope_valid_p=*/false);
11337 /* Look for the nested-name specifier. */
11338 cp_parser_nested_name_specifier (parser,
11339 /*typename_keyword_p=*/false,
11340 /*check_dependency_p=*/true,
a668c6ad
MM
11341 /*type_p=*/false,
11342 /*is_declaration=*/false);
a723baf1
MM
11343 /* If we found it, and the next token is a `*', then we are
11344 indeed looking at a pointer-to-member operator. */
11345 if (!cp_parser_error_occurred (parser)
11346 && cp_parser_require (parser, CPP_MULT, "`*'"))
11347 {
11348 /* The type of which the member is a member is given by the
11349 current SCOPE. */
11350 *type = parser->scope;
11351 /* The next name will not be qualified. */
11352 parser->scope = NULL_TREE;
11353 parser->qualifying_scope = NULL_TREE;
11354 parser->object_scope = NULL_TREE;
11355 /* Indicate that the `*' operator was used. */
11356 code = INDIRECT_REF;
11357 /* Look for the optional cv-qualifier-seq. */
3c01e5df 11358 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
a723baf1
MM
11359 }
11360 /* If that didn't work we don't have a ptr-operator. */
11361 if (!cp_parser_parse_definitely (parser))
11362 cp_parser_error (parser, "expected ptr-operator");
11363 }
11364
11365 return code;
11366}
11367
11368/* Parse an (optional) cv-qualifier-seq.
11369
11370 cv-qualifier-seq:
21526606 11371 cv-qualifier cv-qualifier-seq [opt]
a723baf1 11372
a723baf1
MM
11373 cv-qualifier:
11374 const
21526606 11375 volatile
a723baf1
MM
11376
11377 GNU Extension:
11378
11379 cv-qualifier:
98ca843c 11380 __restrict__
a723baf1 11381
3c01e5df
MM
11382 Returns a bitmask representing the cv-qualifiers. */
11383
11384static cp_cv_quals
11385cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
a723baf1 11386{
3c01e5df 11387 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
a723baf1 11388
3c01e5df 11389 while (true)
a723baf1 11390 {
3c01e5df
MM
11391 cp_token *token;
11392 cp_cv_quals cv_qualifier;
98ca843c 11393
3c01e5df
MM
11394 /* Peek at the next token. */
11395 token = cp_lexer_peek_token (parser->lexer);
11396 /* See if it's a cv-qualifier. */
11397 switch (token->keyword)
11398 {
11399 case RID_CONST:
11400 cv_qualifier = TYPE_QUAL_CONST;
11401 break;
98ca843c 11402
3c01e5df
MM
11403 case RID_VOLATILE:
11404 cv_qualifier = TYPE_QUAL_VOLATILE;
11405 break;
98ca843c 11406
3c01e5df
MM
11407 case RID_RESTRICT:
11408 cv_qualifier = TYPE_QUAL_RESTRICT;
11409 break;
98ca843c 11410
3c01e5df
MM
11411 default:
11412 cv_qualifier = TYPE_UNQUALIFIED;
11413 break;
11414 }
98ca843c 11415
3c01e5df
MM
11416 if (!cv_qualifier)
11417 break;
a723baf1 11418
3c01e5df
MM
11419 if (cv_quals & cv_qualifier)
11420 {
11421 error ("duplicate cv-qualifier");
11422 cp_lexer_purge_token (parser->lexer);
11423 }
11424 else
11425 {
11426 cp_lexer_consume_token (parser->lexer);
11427 cv_quals |= cv_qualifier;
11428 }
a723baf1
MM
11429 }
11430
3c01e5df 11431 return cv_quals;
a723baf1
MM
11432}
11433
11434/* Parse a declarator-id.
11435
11436 declarator-id:
11437 id-expression
21526606 11438 :: [opt] nested-name-specifier [opt] type-name
a723baf1
MM
11439
11440 In the `id-expression' case, the value returned is as for
11441 cp_parser_id_expression if the id-expression was an unqualified-id.
11442 If the id-expression was a qualified-id, then a SCOPE_REF is
11443 returned. The first operand is the scope (either a NAMESPACE_DECL
11444 or TREE_TYPE), but the second is still just a representation of an
11445 unqualified-id. */
11446
11447static tree
94edc4ab 11448cp_parser_declarator_id (cp_parser* parser)
a723baf1 11449{
a723baf1
MM
11450 /* The expression must be an id-expression. Assume that qualified
11451 names are the names of types so that:
11452
11453 template <class T>
11454 int S<T>::R::i = 3;
11455
11456 will work; we must treat `S<T>::R' as the name of a type.
11457 Similarly, assume that qualified names are templates, where
11458 required, so that:
11459
11460 template <class T>
11461 int S<T>::R<T>::i = 3;
11462
11463 will work, too. */
1d786913
MM
11464 return cp_parser_id_expression (parser,
11465 /*template_keyword_p=*/false,
11466 /*check_dependency_p=*/false,
11467 /*template_p=*/NULL,
11468 /*declarator_p=*/true);
a723baf1
MM
11469}
11470
11471/* Parse a type-id.
11472
11473 type-id:
11474 type-specifier-seq abstract-declarator [opt]
11475
11476 Returns the TYPE specified. */
11477
11478static tree
94edc4ab 11479cp_parser_type_id (cp_parser* parser)
a723baf1 11480{
62d1db17 11481 cp_decl_specifier_seq type_specifier_seq;
058b15c1 11482 cp_declarator *abstract_declarator;
a723baf1
MM
11483
11484 /* Parse the type-specifier-seq. */
62d1db17
MM
11485 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11486 if (type_specifier_seq.type == error_mark_node)
a723baf1
MM
11487 return error_mark_node;
11488
11489 /* There might or might not be an abstract declarator. */
11490 cp_parser_parse_tentatively (parser);
11491 /* Look for the declarator. */
21526606 11492 abstract_declarator
4bb8ca28 11493 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
db86dd14
MM
11494 /*parenthesized_p=*/NULL,
11495 /*member_p=*/false);
a723baf1
MM
11496 /* Check to see if there really was a declarator. */
11497 if (!cp_parser_parse_definitely (parser))
058b15c1 11498 abstract_declarator = NULL;
a723baf1 11499
62d1db17 11500 return groktypename (&type_specifier_seq, abstract_declarator);
a723baf1
MM
11501}
11502
11503/* Parse a type-specifier-seq.
11504
11505 type-specifier-seq:
11506 type-specifier type-specifier-seq [opt]
11507
11508 GNU extension:
11509
11510 type-specifier-seq:
11511 attributes type-specifier-seq [opt]
11512
62d1db17 11513 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
a723baf1 11514
62d1db17
MM
11515static void
11516cp_parser_type_specifier_seq (cp_parser* parser,
11517 cp_decl_specifier_seq *type_specifier_seq)
a723baf1
MM
11518{
11519 bool seen_type_specifier = false;
62d1db17
MM
11520
11521 /* Clear the TYPE_SPECIFIER_SEQ. */
11522 clear_decl_specs (type_specifier_seq);
a723baf1
MM
11523
11524 /* Parse the type-specifiers and attributes. */
11525 while (true)
11526 {
11527 tree type_specifier;
11528
11529 /* Check for attributes first. */
11530 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11531 {
98ca843c 11532 type_specifier_seq->attributes =
62d1db17
MM
11533 chainon (type_specifier_seq->attributes,
11534 cp_parser_attributes_opt (parser));
a723baf1
MM
11535 continue;
11536 }
11537
a723baf1 11538 /* Look for the type-specifier. */
21526606 11539 type_specifier = cp_parser_type_specifier (parser,
62d1db17
MM
11540 CP_PARSER_FLAGS_OPTIONAL,
11541 type_specifier_seq,
a723baf1
MM
11542 /*is_declaration=*/false,
11543 NULL,
11544 NULL);
11545 /* If the first type-specifier could not be found, this is not a
11546 type-specifier-seq at all. */
62d1db17
MM
11547 if (!seen_type_specifier && !type_specifier)
11548 {
11549 cp_parser_error (parser, "expected type-specifier");
11550 type_specifier_seq->type = error_mark_node;
11551 return;
11552 }
a723baf1
MM
11553 /* If subsequent type-specifiers could not be found, the
11554 type-specifier-seq is complete. */
62d1db17 11555 else if (seen_type_specifier && !type_specifier)
a723baf1
MM
11556 break;
11557
a723baf1
MM
11558 seen_type_specifier = true;
11559 }
11560
62d1db17 11561 return;
a723baf1
MM
11562}
11563
11564/* Parse a parameter-declaration-clause.
11565
11566 parameter-declaration-clause:
11567 parameter-declaration-list [opt] ... [opt]
11568 parameter-declaration-list , ...
11569
058b15c1
MM
11570 Returns a representation for the parameter declarations. A return
11571 value of NULL indicates a parameter-declaration-clause consisting
11572 only of an ellipsis. */
a723baf1 11573
058b15c1 11574static cp_parameter_declarator *
94edc4ab 11575cp_parser_parameter_declaration_clause (cp_parser* parser)
a723baf1 11576{
058b15c1 11577 cp_parameter_declarator *parameters;
a723baf1
MM
11578 cp_token *token;
11579 bool ellipsis_p;
058b15c1 11580 bool is_error;
a723baf1
MM
11581
11582 /* Peek at the next token. */
11583 token = cp_lexer_peek_token (parser->lexer);
11584 /* Check for trivial parameter-declaration-clauses. */
11585 if (token->type == CPP_ELLIPSIS)
11586 {
11587 /* Consume the `...' token. */
11588 cp_lexer_consume_token (parser->lexer);
058b15c1 11589 return NULL;
a723baf1
MM
11590 }
11591 else if (token->type == CPP_CLOSE_PAREN)
11592 /* There are no parameters. */
c73aecdf
DE
11593 {
11594#ifndef NO_IMPLICIT_EXTERN_C
11595 if (in_system_header && current_class_type == NULL
11596 && current_lang_name == lang_name_c)
058b15c1 11597 return NULL;
c73aecdf
DE
11598 else
11599#endif
058b15c1 11600 return no_parameters;
c73aecdf 11601 }
a723baf1
MM
11602 /* Check for `(void)', too, which is a special case. */
11603 else if (token->keyword == RID_VOID
21526606 11604 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1
MM
11605 == CPP_CLOSE_PAREN))
11606 {
11607 /* Consume the `void' token. */
11608 cp_lexer_consume_token (parser->lexer);
11609 /* There are no parameters. */
058b15c1 11610 return no_parameters;
a723baf1 11611 }
21526606 11612
a723baf1 11613 /* Parse the parameter-declaration-list. */
058b15c1 11614 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
a723baf1
MM
11615 /* If a parse error occurred while parsing the
11616 parameter-declaration-list, then the entire
11617 parameter-declaration-clause is erroneous. */
058b15c1
MM
11618 if (is_error)
11619 return NULL;
a723baf1
MM
11620
11621 /* Peek at the next token. */
11622 token = cp_lexer_peek_token (parser->lexer);
11623 /* If it's a `,', the clause should terminate with an ellipsis. */
11624 if (token->type == CPP_COMMA)
11625 {
11626 /* Consume the `,'. */
11627 cp_lexer_consume_token (parser->lexer);
11628 /* Expect an ellipsis. */
21526606 11629 ellipsis_p
a723baf1
MM
11630 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11631 }
21526606 11632 /* It might also be `...' if the optional trailing `,' was
a723baf1
MM
11633 omitted. */
11634 else if (token->type == CPP_ELLIPSIS)
11635 {
11636 /* Consume the `...' token. */
11637 cp_lexer_consume_token (parser->lexer);
11638 /* And remember that we saw it. */
11639 ellipsis_p = true;
11640 }
11641 else
11642 ellipsis_p = false;
11643
11644 /* Finish the parameter list. */
058b15c1
MM
11645 if (parameters && ellipsis_p)
11646 parameters->ellipsis_p = true;
98ca843c 11647
058b15c1 11648 return parameters;
a723baf1
MM
11649}
11650
11651/* Parse a parameter-declaration-list.
11652
11653 parameter-declaration-list:
11654 parameter-declaration
11655 parameter-declaration-list , parameter-declaration
11656
11657 Returns a representation of the parameter-declaration-list, as for
11658 cp_parser_parameter_declaration_clause. However, the
058b15c1
MM
11659 `void_list_node' is never appended to the list. Upon return,
11660 *IS_ERROR will be true iff an error occurred. */
a723baf1 11661
058b15c1
MM
11662static cp_parameter_declarator *
11663cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
a723baf1 11664{
058b15c1
MM
11665 cp_parameter_declarator *parameters = NULL;
11666 cp_parameter_declarator **tail = &parameters;
11667
11668 /* Assume all will go well. */
11669 *is_error = false;
a723baf1
MM
11670
11671 /* Look for more parameters. */
11672 while (true)
11673 {
058b15c1 11674 cp_parameter_declarator *parameter;
4bb8ca28 11675 bool parenthesized_p;
a723baf1 11676 /* Parse the parameter. */
21526606
EC
11677 parameter
11678 = cp_parser_parameter_declaration (parser,
4bb8ca28
MM
11679 /*template_parm_p=*/false,
11680 &parenthesized_p);
ec194454 11681
34cd5ae7 11682 /* If a parse error occurred parsing the parameter declaration,
a723baf1 11683 then the entire parameter-declaration-list is erroneous. */
058b15c1 11684 if (!parameter)
a723baf1 11685 {
058b15c1
MM
11686 *is_error = true;
11687 parameters = NULL;
a723baf1
MM
11688 break;
11689 }
11690 /* Add the new parameter to the list. */
058b15c1
MM
11691 *tail = parameter;
11692 tail = &parameter->next;
a723baf1
MM
11693
11694 /* Peek at the next token. */
11695 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11696 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11697 /* The parameter-declaration-list is complete. */
11698 break;
11699 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11700 {
11701 cp_token *token;
11702
11703 /* Peek at the next token. */
11704 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11705 /* If it's an ellipsis, then the list is complete. */
11706 if (token->type == CPP_ELLIPSIS)
11707 break;
11708 /* Otherwise, there must be more parameters. Consume the
11709 `,'. */
11710 cp_lexer_consume_token (parser->lexer);
4bb8ca28
MM
11711 /* When parsing something like:
11712
11713 int i(float f, double d)
21526606 11714
4bb8ca28
MM
11715 we can tell after seeing the declaration for "f" that we
11716 are not looking at an initialization of a variable "i",
21526606 11717 but rather at the declaration of a function "i".
4bb8ca28
MM
11718
11719 Due to the fact that the parsing of template arguments
11720 (as specified to a template-id) requires backtracking we
11721 cannot use this technique when inside a template argument
11722 list. */
11723 if (!parser->in_template_argument_list_p
4d5fe289 11724 && !parser->in_type_id_in_expr_p
0b16f8f4 11725 && cp_parser_uncommitted_to_tentative_parse_p (parser)
4bb8ca28
MM
11726 /* However, a parameter-declaration of the form
11727 "foat(f)" (which is a valid declaration of a
11728 parameter "f") can also be interpreted as an
11729 expression (the conversion of "f" to "float"). */
11730 && !parenthesized_p)
11731 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
11732 }
11733 else
11734 {
2a13a625 11735 cp_parser_error (parser, "expected %<,%> or %<...%>");
0b16f8f4 11736 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21526606 11737 cp_parser_skip_to_closing_parenthesis (parser,
4bb8ca28 11738 /*recovering=*/true,
5c832178 11739 /*or_comma=*/false,
4bb8ca28 11740 /*consume_paren=*/false);
a723baf1
MM
11741 break;
11742 }
11743 }
11744
058b15c1 11745 return parameters;
a723baf1
MM
11746}
11747
11748/* Parse a parameter declaration.
11749
11750 parameter-declaration:
11751 decl-specifier-seq declarator
11752 decl-specifier-seq declarator = assignment-expression
11753 decl-specifier-seq abstract-declarator [opt]
11754 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11755
ec194454
MM
11756 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11757 declares a template parameter. (In that case, a non-nested `>'
11758 token encountered during the parsing of the assignment-expression
11759 is not interpreted as a greater-than operator.)
a723baf1 11760
058b15c1
MM
11761 Returns a representation of the parameter, or NULL if an error
11762 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11763 true iff the declarator is of the form "(p)". */
a723baf1 11764
058b15c1 11765static cp_parameter_declarator *
21526606 11766cp_parser_parameter_declaration (cp_parser *parser,
4bb8ca28
MM
11767 bool template_parm_p,
11768 bool *parenthesized_p)
a723baf1 11769{
560ad596 11770 int declares_class_or_enum;
ec194454 11771 bool greater_than_is_operator_p;
62d1db17 11772 cp_decl_specifier_seq decl_specifiers;
058b15c1 11773 cp_declarator *declarator;
a723baf1 11774 tree default_argument;
a723baf1
MM
11775 cp_token *token;
11776 const char *saved_message;
11777
ec194454
MM
11778 /* In a template parameter, `>' is not an operator.
11779
11780 [temp.param]
11781
11782 When parsing a default template-argument for a non-type
11783 template-parameter, the first non-nested `>' is taken as the end
11784 of the template parameter-list rather than a greater-than
11785 operator. */
11786 greater_than_is_operator_p = !template_parm_p;
11787
a723baf1
MM
11788 /* Type definitions may not appear in parameter types. */
11789 saved_message = parser->type_definition_forbidden_message;
21526606 11790 parser->type_definition_forbidden_message
a723baf1
MM
11791 = "types may not be defined in parameter types";
11792
11793 /* Parse the declaration-specifiers. */
62d1db17
MM
11794 cp_parser_decl_specifier_seq (parser,
11795 CP_PARSER_FLAGS_NONE,
11796 &decl_specifiers,
11797 &declares_class_or_enum);
a723baf1
MM
11798 /* If an error occurred, there's no reason to attempt to parse the
11799 rest of the declaration. */
11800 if (cp_parser_error_occurred (parser))
11801 {
11802 parser->type_definition_forbidden_message = saved_message;
058b15c1 11803 return NULL;
a723baf1
MM
11804 }
11805
11806 /* Peek at the next token. */
11807 token = cp_lexer_peek_token (parser->lexer);
11808 /* If the next token is a `)', `,', `=', `>', or `...', then there
11809 is no declarator. */
21526606 11810 if (token->type == CPP_CLOSE_PAREN
a723baf1
MM
11811 || token->type == CPP_COMMA
11812 || token->type == CPP_EQ
11813 || token->type == CPP_ELLIPSIS
11814 || token->type == CPP_GREATER)
4bb8ca28 11815 {
058b15c1 11816 declarator = NULL;
4bb8ca28
MM
11817 if (parenthesized_p)
11818 *parenthesized_p = false;
11819 }
a723baf1
MM
11820 /* Otherwise, there should be a declarator. */
11821 else
11822 {
11823 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11824 parser->default_arg_ok_p = false;
21526606 11825
5c832178
MM
11826 /* After seeing a decl-specifier-seq, if the next token is not a
11827 "(", there is no possibility that the code is a valid
4f8163b1
MM
11828 expression. Therefore, if parsing tentatively, we commit at
11829 this point. */
5c832178 11830 if (!parser->in_template_argument_list_p
643aee72 11831 /* In an expression context, having seen:
4f8163b1 11832
a7324e75 11833 (int((char ...
4f8163b1
MM
11834
11835 we cannot be sure whether we are looking at a
a7324e75
MM
11836 function-type (taking a "char" as a parameter) or a cast
11837 of some object of type "char" to "int". */
4f8163b1 11838 && !parser->in_type_id_in_expr_p
0b16f8f4 11839 && cp_parser_uncommitted_to_tentative_parse_p (parser)
5c832178
MM
11840 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11841 cp_parser_commit_to_tentative_parse (parser);
11842 /* Parse the declarator. */
a723baf1 11843 declarator = cp_parser_declarator (parser,
62b8a44e 11844 CP_PARSER_DECLARATOR_EITHER,
4bb8ca28 11845 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
11846 parenthesized_p,
11847 /*member_p=*/false);
a723baf1 11848 parser->default_arg_ok_p = saved_default_arg_ok_p;
4971227d 11849 /* After the declarator, allow more attributes. */
62d1db17 11850 decl_specifiers.attributes
98ca843c 11851 = chainon (decl_specifiers.attributes,
62d1db17 11852 cp_parser_attributes_opt (parser));
a723baf1
MM
11853 }
11854
62b8a44e 11855 /* The restriction on defining new types applies only to the type
a723baf1
MM
11856 of the parameter, not to the default argument. */
11857 parser->type_definition_forbidden_message = saved_message;
11858
11859 /* If the next token is `=', then process a default argument. */
11860 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11861 {
11862 bool saved_greater_than_is_operator_p;
11863 /* Consume the `='. */
11864 cp_lexer_consume_token (parser->lexer);
11865
11866 /* If we are defining a class, then the tokens that make up the
11867 default argument must be saved and processed later. */
21526606 11868 if (!template_parm_p && at_class_scope_p ()
ec194454 11869 && TYPE_BEING_DEFINED (current_class_type))
a723baf1
MM
11870 {
11871 unsigned depth = 0;
c162c75e
MA
11872 cp_token *first_token;
11873 cp_token *token;
a723baf1
MM
11874
11875 /* Add tokens until we have processed the entire default
03fd3f84 11876 argument. We add the range [first_token, token). */
c162c75e 11877 first_token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
11878 while (true)
11879 {
11880 bool done = false;
a723baf1
MM
11881
11882 /* Peek at the next token. */
11883 token = cp_lexer_peek_token (parser->lexer);
11884 /* What we do depends on what token we have. */
11885 switch (token->type)
11886 {
11887 /* In valid code, a default argument must be
11888 immediately followed by a `,' `)', or `...'. */
11889 case CPP_COMMA:
11890 case CPP_CLOSE_PAREN:
11891 case CPP_ELLIPSIS:
11892 /* If we run into a non-nested `;', `}', or `]',
11893 then the code is invalid -- but the default
11894 argument is certainly over. */
11895 case CPP_SEMICOLON:
11896 case CPP_CLOSE_BRACE:
11897 case CPP_CLOSE_SQUARE:
11898 if (depth == 0)
11899 done = true;
11900 /* Update DEPTH, if necessary. */
11901 else if (token->type == CPP_CLOSE_PAREN
11902 || token->type == CPP_CLOSE_BRACE
11903 || token->type == CPP_CLOSE_SQUARE)
11904 --depth;
11905 break;
11906
11907 case CPP_OPEN_PAREN:
11908 case CPP_OPEN_SQUARE:
11909 case CPP_OPEN_BRACE:
11910 ++depth;
11911 break;
11912
11913 case CPP_GREATER:
11914 /* If we see a non-nested `>', and `>' is not an
11915 operator, then it marks the end of the default
11916 argument. */
11917 if (!depth && !greater_than_is_operator_p)
11918 done = true;
11919 break;
11920
11921 /* If we run out of tokens, issue an error message. */
11922 case CPP_EOF:
11923 error ("file ends in default argument");
11924 done = true;
11925 break;
11926
11927 case CPP_NAME:
11928 case CPP_SCOPE:
11929 /* In these cases, we should look for template-ids.
21526606 11930 For example, if the default argument is
a723baf1
MM
11931 `X<int, double>()', we need to do name lookup to
11932 figure out whether or not `X' is a template; if
34cd5ae7 11933 so, the `,' does not end the default argument.
a723baf1
MM
11934
11935 That is not yet done. */
11936 break;
11937
11938 default:
11939 break;
11940 }
11941
11942 /* If we've reached the end, stop. */
11943 if (done)
11944 break;
21526606 11945
a723baf1
MM
11946 /* Add the token to the token block. */
11947 token = cp_lexer_consume_token (parser->lexer);
a723baf1 11948 }
c162c75e
MA
11949
11950 /* Create a DEFAULT_ARG to represented the unparsed default
11951 argument. */
11952 default_argument = make_node (DEFAULT_ARG);
11953 DEFARG_TOKENS (default_argument)
11954 = cp_token_cache_new (first_token, token);
a723baf1
MM
11955 }
11956 /* Outside of a class definition, we can just parse the
11957 assignment-expression. */
11958 else
11959 {
11960 bool saved_local_variables_forbidden_p;
11961
11962 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11963 set correctly. */
21526606 11964 saved_greater_than_is_operator_p
a723baf1
MM
11965 = parser->greater_than_is_operator_p;
11966 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11967 /* Local variable names (and the `this' keyword) may not
11968 appear in a default argument. */
21526606 11969 saved_local_variables_forbidden_p
a723baf1
MM
11970 = parser->local_variables_forbidden_p;
11971 parser->local_variables_forbidden_p = true;
11972 /* Parse the assignment-expression. */
93678513
MM
11973 default_argument
11974 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
a723baf1 11975 /* Restore saved state. */
21526606 11976 parser->greater_than_is_operator_p
a723baf1 11977 = saved_greater_than_is_operator_p;
21526606
EC
11978 parser->local_variables_forbidden_p
11979 = saved_local_variables_forbidden_p;
a723baf1
MM
11980 }
11981 if (!parser->default_arg_ok_p)
11982 {
c67d36d0
NS
11983 if (!flag_pedantic_errors)
11984 warning ("deprecated use of default argument for parameter of non-function");
11985 else
11986 {
11987 error ("default arguments are only permitted for function parameters");
11988 default_argument = NULL_TREE;
11989 }
a723baf1
MM
11990 }
11991 }
11992 else
11993 default_argument = NULL_TREE;
21526606 11994
62d1db17 11995 return make_parameter_declarator (&decl_specifiers,
058b15c1
MM
11996 declarator,
11997 default_argument);
a723baf1
MM
11998}
11999
a723baf1
MM
12000/* Parse a function-body.
12001
12002 function-body:
12003 compound_statement */
12004
12005static void
12006cp_parser_function_body (cp_parser *parser)
12007{
325c3691 12008 cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
12009}
12010
12011/* Parse a ctor-initializer-opt followed by a function-body. Return
12012 true if a ctor-initializer was present. */
12013
12014static bool
12015cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12016{
12017 tree body;
12018 bool ctor_initializer_p;
12019
12020 /* Begin the function body. */
12021 body = begin_function_body ();
12022 /* Parse the optional ctor-initializer. */
12023 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12024 /* Parse the function-body. */
12025 cp_parser_function_body (parser);
12026 /* Finish the function body. */
12027 finish_function_body (body);
12028
12029 return ctor_initializer_p;
12030}
12031
12032/* Parse an initializer.
12033
12034 initializer:
12035 = initializer-clause
21526606 12036 ( expression-list )
a723baf1
MM
12037
12038 Returns a expression representing the initializer. If no
21526606 12039 initializer is present, NULL_TREE is returned.
a723baf1
MM
12040
12041 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12042 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
39703eb9
MM
12043 set to FALSE if there is no initializer present. If there is an
12044 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12045 is set to true; otherwise it is set to false. */
a723baf1
MM
12046
12047static tree
39703eb9
MM
12048cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12049 bool* non_constant_p)
a723baf1
MM
12050{
12051 cp_token *token;
12052 tree init;
12053
12054 /* Peek at the next token. */
12055 token = cp_lexer_peek_token (parser->lexer);
12056
12057 /* Let our caller know whether or not this initializer was
12058 parenthesized. */
12059 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
39703eb9
MM
12060 /* Assume that the initializer is constant. */
12061 *non_constant_p = false;
a723baf1
MM
12062
12063 if (token->type == CPP_EQ)
12064 {
12065 /* Consume the `='. */
12066 cp_lexer_consume_token (parser->lexer);
12067 /* Parse the initializer-clause. */
39703eb9 12068 init = cp_parser_initializer_clause (parser, non_constant_p);
a723baf1
MM
12069 }
12070 else if (token->type == CPP_OPEN_PAREN)
39703eb9 12071 init = cp_parser_parenthesized_expression_list (parser, false,
93678513 12072 /*cast_p=*/false,
39703eb9 12073 non_constant_p);
a723baf1
MM
12074 else
12075 {
12076 /* Anything else is an error. */
12077 cp_parser_error (parser, "expected initializer");
12078 init = error_mark_node;
12079 }
12080
12081 return init;
12082}
12083
21526606 12084/* Parse an initializer-clause.
a723baf1
MM
12085
12086 initializer-clause:
12087 assignment-expression
12088 { initializer-list , [opt] }
12089 { }
12090
21526606 12091 Returns an expression representing the initializer.
a723baf1
MM
12092
12093 If the `assignment-expression' production is used the value
21526606 12094 returned is simply a representation for the expression.
a723baf1
MM
12095
12096 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12097 the elements of the initializer-list (or NULL_TREE, if the last
12098 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12099 NULL_TREE. There is no way to detect whether or not the optional
39703eb9
MM
12100 trailing `,' was provided. NON_CONSTANT_P is as for
12101 cp_parser_initializer. */
a723baf1
MM
12102
12103static tree
39703eb9 12104cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
12105{
12106 tree initializer;
12107
b2802a4b
R
12108 /* Assume the expression is constant. */
12109 *non_constant_p = false;
12110
a723baf1
MM
12111 /* If it is not a `{', then we are looking at an
12112 assignment-expression. */
12113 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
0da99d4e 12114 {
98ca843c 12115 initializer
0da99d4e
GB
12116 = cp_parser_constant_expression (parser,
12117 /*allow_non_constant_p=*/true,
12118 non_constant_p);
12119 if (!*non_constant_p)
12120 initializer = fold_non_dependent_expr (initializer);
12121 }
a723baf1
MM
12122 else
12123 {
12124 /* Consume the `{' token. */
12125 cp_lexer_consume_token (parser->lexer);
12126 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12127 initializer = make_node (CONSTRUCTOR);
a723baf1
MM
12128 /* If it's not a `}', then there is a non-trivial initializer. */
12129 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12130 {
12131 /* Parse the initializer list. */
12132 CONSTRUCTOR_ELTS (initializer)
39703eb9 12133 = cp_parser_initializer_list (parser, non_constant_p);
a723baf1
MM
12134 /* A trailing `,' token is allowed. */
12135 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12136 cp_lexer_consume_token (parser->lexer);
12137 }
a723baf1
MM
12138 /* Now, there should be a trailing `}'. */
12139 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12140 }
12141
12142 return initializer;
12143}
12144
12145/* Parse an initializer-list.
12146
12147 initializer-list:
12148 initializer-clause
12149 initializer-list , initializer-clause
12150
12151 GNU Extension:
21526606 12152
a723baf1
MM
12153 initializer-list:
12154 identifier : initializer-clause
12155 initializer-list, identifier : initializer-clause
12156
12157 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12158 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
39703eb9
MM
12159 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12160 as for cp_parser_initializer. */
a723baf1
MM
12161
12162static tree
39703eb9 12163cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
12164{
12165 tree initializers = NULL_TREE;
12166
39703eb9
MM
12167 /* Assume all of the expressions are constant. */
12168 *non_constant_p = false;
12169
a723baf1
MM
12170 /* Parse the rest of the list. */
12171 while (true)
12172 {
12173 cp_token *token;
12174 tree identifier;
12175 tree initializer;
39703eb9
MM
12176 bool clause_non_constant_p;
12177
a723baf1
MM
12178 /* If the next token is an identifier and the following one is a
12179 colon, we are looking at the GNU designated-initializer
12180 syntax. */
12181 if (cp_parser_allow_gnu_extensions_p (parser)
12182 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12183 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12184 {
12185 /* Consume the identifier. */
12186 identifier = cp_lexer_consume_token (parser->lexer)->value;
12187 /* Consume the `:'. */
12188 cp_lexer_consume_token (parser->lexer);
12189 }
12190 else
12191 identifier = NULL_TREE;
12192
12193 /* Parse the initializer. */
21526606 12194 initializer = cp_parser_initializer_clause (parser,
39703eb9
MM
12195 &clause_non_constant_p);
12196 /* If any clause is non-constant, so is the entire initializer. */
12197 if (clause_non_constant_p)
12198 *non_constant_p = true;
a723baf1
MM
12199 /* Add it to the list. */
12200 initializers = tree_cons (identifier, initializer, initializers);
12201
12202 /* If the next token is not a comma, we have reached the end of
12203 the list. */
12204 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12205 break;
12206
12207 /* Peek at the next token. */
12208 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12209 /* If the next token is a `}', then we're still done. An
12210 initializer-clause can have a trailing `,' after the
12211 initializer-list and before the closing `}'. */
12212 if (token->type == CPP_CLOSE_BRACE)
12213 break;
12214
12215 /* Consume the `,' token. */
12216 cp_lexer_consume_token (parser->lexer);
12217 }
12218
12219 /* The initializers were built up in reverse order, so we need to
12220 reverse them now. */
12221 return nreverse (initializers);
12222}
12223
12224/* Classes [gram.class] */
12225
12226/* Parse a class-name.
12227
12228 class-name:
12229 identifier
12230 template-id
12231
12232 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12233 to indicate that names looked up in dependent types should be
12234 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12235 keyword has been used to indicate that the name that appears next
fc6a28d7
MM
12236 is a template. TAG_TYPE indicates the explicit tag given before
12237 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12238 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12239 is the class being defined in a class-head.
a723baf1
MM
12240
12241 Returns the TYPE_DECL representing the class. */
12242
12243static tree
21526606
EC
12244cp_parser_class_name (cp_parser *parser,
12245 bool typename_keyword_p,
12246 bool template_keyword_p,
fc6a28d7 12247 enum tag_types tag_type,
a723baf1 12248 bool check_dependency_p,
a668c6ad
MM
12249 bool class_head_p,
12250 bool is_declaration)
a723baf1
MM
12251{
12252 tree decl;
12253 tree scope;
12254 bool typename_p;
e5976695
MM
12255 cp_token *token;
12256
12257 /* All class-names start with an identifier. */
12258 token = cp_lexer_peek_token (parser->lexer);
12259 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12260 {
12261 cp_parser_error (parser, "expected class-name");
12262 return error_mark_node;
12263 }
21526606 12264
a723baf1
MM
12265 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12266 to a template-id, so we save it here. */
12267 scope = parser->scope;
3adee96c
KL
12268 if (scope == error_mark_node)
12269 return error_mark_node;
21526606 12270
a723baf1
MM
12271 /* Any name names a type if we're following the `typename' keyword
12272 in a qualified name where the enclosing scope is type-dependent. */
12273 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
1fb3244a 12274 && dependent_type_p (scope));
e5976695
MM
12275 /* Handle the common case (an identifier, but not a template-id)
12276 efficiently. */
21526606 12277 if (token->type == CPP_NAME
f4abade9 12278 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
a723baf1 12279 {
a723baf1
MM
12280 tree identifier;
12281
12282 /* Look for the identifier. */
12283 identifier = cp_parser_identifier (parser);
12284 /* If the next token isn't an identifier, we are certainly not
12285 looking at a class-name. */
12286 if (identifier == error_mark_node)
12287 decl = error_mark_node;
12288 /* If we know this is a type-name, there's no need to look it
12289 up. */
12290 else if (typename_p)
12291 decl = identifier;
12292 else
12293 {
12294 /* If the next token is a `::', then the name must be a type
12295 name.
12296
12297 [basic.lookup.qual]
12298
12299 During the lookup for a name preceding the :: scope
12300 resolution operator, object, function, and enumerator
12301 names are ignored. */
12302 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
fc6a28d7 12303 tag_type = typename_type;
a723baf1 12304 /* Look up the name. */
21526606 12305 decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 12306 tag_type,
b0bc6e8e 12307 /*is_template=*/false,
eea9800f 12308 /*is_namespace=*/false,
8f78f01f
MM
12309 check_dependency_p,
12310 /*ambiguous_p=*/NULL);
a723baf1
MM
12311 }
12312 }
e5976695
MM
12313 else
12314 {
12315 /* Try a template-id. */
12316 decl = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
12317 check_dependency_p,
12318 is_declaration);
e5976695
MM
12319 if (decl == error_mark_node)
12320 return error_mark_node;
12321 }
a723baf1
MM
12322
12323 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12324
12325 /* If this is a typename, create a TYPENAME_TYPE. */
12326 if (typename_p && decl != error_mark_node)
4bfb8bba 12327 {
fc6a28d7 12328 decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
4bfb8bba
MM
12329 if (decl != error_mark_node)
12330 decl = TYPE_NAME (decl);
12331 }
a723baf1
MM
12332
12333 /* Check to see that it is really the name of a class. */
21526606 12334 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
a723baf1
MM
12335 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12336 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12337 /* Situations like this:
12338
12339 template <typename T> struct A {
21526606 12340 typename T::template X<int>::I i;
a723baf1
MM
12341 };
12342
12343 are problematic. Is `T::template X<int>' a class-name? The
12344 standard does not seem to be definitive, but there is no other
12345 valid interpretation of the following `::'. Therefore, those
12346 names are considered class-names. */
fc6a28d7 12347 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
a723baf1
MM
12348 else if (decl == error_mark_node
12349 || TREE_CODE (decl) != TYPE_DECL
07c65e00 12350 || TREE_TYPE (decl) == error_mark_node
a723baf1
MM
12351 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12352 {
12353 cp_parser_error (parser, "expected class-name");
12354 return error_mark_node;
12355 }
12356
12357 return decl;
12358}
12359
12360/* Parse a class-specifier.
12361
12362 class-specifier:
12363 class-head { member-specification [opt] }
12364
12365 Returns the TREE_TYPE representing the class. */
12366
12367static tree
94edc4ab 12368cp_parser_class_specifier (cp_parser* parser)
a723baf1
MM
12369{
12370 cp_token *token;
12371 tree type;
6de9cd9a 12372 tree attributes = NULL_TREE;
a723baf1
MM
12373 int has_trailing_semicolon;
12374 bool nested_name_specifier_p;
a723baf1 12375 unsigned saved_num_template_parameter_lists;
87c465f5 12376 tree old_scope = NULL_TREE;
2436b51f 12377 tree scope = NULL_TREE;
a723baf1 12378
8d241e0b 12379 push_deferring_access_checks (dk_no_deferred);
cf22909c 12380
a723baf1
MM
12381 /* Parse the class-head. */
12382 type = cp_parser_class_head (parser,
38b305d0
JM
12383 &nested_name_specifier_p,
12384 &attributes);
a723baf1
MM
12385 /* If the class-head was a semantic disaster, skip the entire body
12386 of the class. */
12387 if (!type)
12388 {
12389 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 12390 pop_deferring_access_checks ();
a723baf1
MM
12391 return error_mark_node;
12392 }
cf22909c 12393
a723baf1
MM
12394 /* Look for the `{'. */
12395 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
cf22909c
KL
12396 {
12397 pop_deferring_access_checks ();
12398 return error_mark_node;
12399 }
12400
a723baf1
MM
12401 /* Issue an error message if type-definitions are forbidden here. */
12402 cp_parser_check_type_definition (parser);
12403 /* Remember that we are defining one more class. */
12404 ++parser->num_classes_being_defined;
12405 /* Inside the class, surrounding template-parameter-lists do not
12406 apply. */
21526606
EC
12407 saved_num_template_parameter_lists
12408 = parser->num_template_parameter_lists;
a723baf1 12409 parser->num_template_parameter_lists = 0;
78757caa 12410
a723baf1 12411 /* Start the class. */
eeb23c11 12412 if (nested_name_specifier_p)
2436b51f
MM
12413 {
12414 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
87c465f5 12415 old_scope = push_inner_scope (scope);
2436b51f 12416 }
a723baf1 12417 type = begin_class_definition (type);
98ca843c 12418
a723baf1 12419 if (type == error_mark_node)
9bcb9aae 12420 /* If the type is erroneous, skip the entire body of the class. */
a723baf1
MM
12421 cp_parser_skip_to_closing_brace (parser);
12422 else
12423 /* Parse the member-specification. */
12424 cp_parser_member_specification_opt (parser);
98ca843c 12425
a723baf1
MM
12426 /* Look for the trailing `}'. */
12427 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12428 /* We get better error messages by noticing a common problem: a
12429 missing trailing `;'. */
12430 token = cp_lexer_peek_token (parser->lexer);
12431 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
38b305d0 12432 /* Look for trailing attributes to apply to this class. */
a723baf1 12433 if (cp_parser_allow_gnu_extensions_p (parser))
560ad596 12434 {
38b305d0
JM
12435 tree sub_attr = cp_parser_attributes_opt (parser);
12436 attributes = chainon (attributes, sub_attr);
560ad596 12437 }
38b305d0
JM
12438 if (type != error_mark_node)
12439 type = finish_struct (type, attributes);
87c465f5
KL
12440 if (nested_name_specifier_p)
12441 pop_inner_scope (old_scope, scope);
a723baf1
MM
12442 /* If this class is not itself within the scope of another class,
12443 then we need to parse the bodies of all of the queued function
12444 definitions. Note that the queued functions defined in a class
12445 are not always processed immediately following the
12446 class-specifier for that class. Consider:
12447
12448 struct A {
12449 struct B { void f() { sizeof (A); } };
12450 };
12451
12452 If `f' were processed before the processing of `A' were
12453 completed, there would be no way to compute the size of `A'.
12454 Note that the nesting we are interested in here is lexical --
12455 not the semantic nesting given by TYPE_CONTEXT. In particular,
12456 for:
12457
12458 struct A { struct B; };
12459 struct A::B { void f() { } };
12460
12461 there is no need to delay the parsing of `A::B::f'. */
21526606 12462 if (--parser->num_classes_being_defined == 0)
a723baf1 12463 {
8218bd34
MM
12464 tree queue_entry;
12465 tree fn;
4514aa8c
NS
12466 tree class_type = NULL_TREE;
12467 tree pushed_scope = NULL_TREE;
a723baf1 12468
8218bd34
MM
12469 /* In a first pass, parse default arguments to the functions.
12470 Then, in a second pass, parse the bodies of the functions.
12471 This two-phased approach handles cases like:
21526606
EC
12472
12473 struct S {
12474 void f() { g(); }
8218bd34
MM
12475 void g(int i = 3);
12476 };
12477
12478 */
8db1028e
NS
12479 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12480 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12481 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12482 TREE_PURPOSE (parser->unparsed_functions_queues)
12483 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
8218bd34
MM
12484 {
12485 fn = TREE_VALUE (queue_entry);
8218bd34
MM
12486 /* If there are default arguments that have not yet been processed,
12487 take care of them now. */
f44b0c8e
MM
12488 if (class_type != TREE_PURPOSE (queue_entry))
12489 {
4514aa8c
NS
12490 if (pushed_scope)
12491 pop_scope (pushed_scope);
f44b0c8e 12492 class_type = TREE_PURPOSE (queue_entry);
4514aa8c 12493 pushed_scope = push_scope (class_type);
f44b0c8e
MM
12494 }
12495 /* Make sure that any template parameters are in scope. */
12496 maybe_begin_member_template_processing (fn);
12497 /* Parse the default argument expressions. */
8218bd34
MM
12498 cp_parser_late_parsing_default_args (parser, fn);
12499 /* Remove any template parameters from the symbol table. */
12500 maybe_end_member_template_processing ();
12501 }
4514aa8c
NS
12502 if (pushed_scope)
12503 pop_scope (pushed_scope);
8218bd34 12504 /* Now parse the body of the functions. */
8db1028e
NS
12505 for (TREE_VALUE (parser->unparsed_functions_queues)
12506 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12507 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12508 TREE_VALUE (parser->unparsed_functions_queues)
12509 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
a723baf1 12510 {
a723baf1 12511 /* Figure out which function we need to process. */
a723baf1
MM
12512 fn = TREE_VALUE (queue_entry);
12513
4543ee47
ZD
12514 /* A hack to prevent garbage collection. */
12515 function_depth++;
12516
a723baf1
MM
12517 /* Parse the function. */
12518 cp_parser_late_parsing_for_member (parser, fn);
4543ee47 12519 function_depth--;
a723baf1 12520 }
a723baf1
MM
12521 }
12522
12523 /* Put back any saved access checks. */
cf22909c 12524 pop_deferring_access_checks ();
a723baf1
MM
12525
12526 /* Restore the count of active template-parameter-lists. */
12527 parser->num_template_parameter_lists
12528 = saved_num_template_parameter_lists;
12529
12530 return type;
12531}
12532
12533/* Parse a class-head.
12534
12535 class-head:
12536 class-key identifier [opt] base-clause [opt]
12537 class-key nested-name-specifier identifier base-clause [opt]
21526606
EC
12538 class-key nested-name-specifier [opt] template-id
12539 base-clause [opt]
a723baf1
MM
12540
12541 GNU Extensions:
12542 class-key attributes identifier [opt] base-clause [opt]
12543 class-key attributes nested-name-specifier identifier base-clause [opt]
21526606
EC
12544 class-key attributes nested-name-specifier [opt] template-id
12545 base-clause [opt]
a723baf1
MM
12546
12547 Returns the TYPE of the indicated class. Sets
12548 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12549 involving a nested-name-specifier was used, and FALSE otherwise.
a723baf1 12550
55dcbc12
NS
12551 Returns error_mark_node if this is not a class-head.
12552
a723baf1
MM
12553 Returns NULL_TREE if the class-head is syntactically valid, but
12554 semantically invalid in a way that means we should skip the entire
12555 body of the class. */
12556
12557static tree
21526606 12558cp_parser_class_head (cp_parser* parser,
38b305d0
JM
12559 bool* nested_name_specifier_p,
12560 tree *attributes_p)
a723baf1 12561{
a723baf1
MM
12562 tree nested_name_specifier;
12563 enum tag_types class_key;
12564 tree id = NULL_TREE;
12565 tree type = NULL_TREE;
12566 tree attributes;
12567 bool template_id_p = false;
12568 bool qualified_p = false;
12569 bool invalid_nested_name_p = false;
afb0918a 12570 bool invalid_explicit_specialization_p = false;
4514aa8c 12571 tree pushed_scope = NULL_TREE;
a723baf1 12572 unsigned num_templates;
cad7e87b 12573 tree bases;
a723baf1
MM
12574
12575 /* Assume no nested-name-specifier will be present. */
12576 *nested_name_specifier_p = false;
12577 /* Assume no template parameter lists will be used in defining the
12578 type. */
12579 num_templates = 0;
12580
12581 /* Look for the class-key. */
12582 class_key = cp_parser_class_key (parser);
12583 if (class_key == none_type)
12584 return error_mark_node;
12585
12586 /* Parse the attributes. */
12587 attributes = cp_parser_attributes_opt (parser);
12588
12589 /* If the next token is `::', that is invalid -- but sometimes
12590 people do try to write:
12591
21526606 12592 struct ::S {};
a723baf1
MM
12593
12594 Handle this gracefully by accepting the extra qualifier, and then
12595 issuing an error about it later if this really is a
2050a1bb 12596 class-head. If it turns out just to be an elaborated type
a723baf1
MM
12597 specifier, remain silent. */
12598 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12599 qualified_p = true;
12600
8d241e0b
KL
12601 push_deferring_access_checks (dk_no_check);
12602
a723baf1
MM
12603 /* Determine the name of the class. Begin by looking for an
12604 optional nested-name-specifier. */
21526606 12605 nested_name_specifier
a723baf1
MM
12606 = cp_parser_nested_name_specifier_opt (parser,
12607 /*typename_keyword_p=*/false,
66d418e6 12608 /*check_dependency_p=*/false,
a668c6ad
MM
12609 /*type_p=*/false,
12610 /*is_declaration=*/false);
a723baf1
MM
12611 /* If there was a nested-name-specifier, then there *must* be an
12612 identifier. */
12613 if (nested_name_specifier)
12614 {
12615 /* Although the grammar says `identifier', it really means
12616 `class-name' or `template-name'. You are only allowed to
12617 define a class that has already been declared with this
21526606 12618 syntax.
a723baf1
MM
12619
12620 The proposed resolution for Core Issue 180 says that whever
12621 you see `class T::X' you should treat `X' as a type-name.
21526606 12622
a723baf1 12623 It is OK to define an inaccessible class; for example:
21526606 12624
a723baf1
MM
12625 class A { class B; };
12626 class A::B {};
21526606 12627
a723baf1
MM
12628 We do not know if we will see a class-name, or a
12629 template-name. We look for a class-name first, in case the
12630 class-name is a template-id; if we looked for the
12631 template-name first we would stop after the template-name. */
12632 cp_parser_parse_tentatively (parser);
12633 type = cp_parser_class_name (parser,
12634 /*typename_keyword_p=*/false,
12635 /*template_keyword_p=*/false,
fc6a28d7 12636 class_type,
a723baf1 12637 /*check_dependency_p=*/false,
a668c6ad
MM
12638 /*class_head_p=*/true,
12639 /*is_declaration=*/false);
a723baf1
MM
12640 /* If that didn't work, ignore the nested-name-specifier. */
12641 if (!cp_parser_parse_definitely (parser))
12642 {
12643 invalid_nested_name_p = true;
12644 id = cp_parser_identifier (parser);
12645 if (id == error_mark_node)
12646 id = NULL_TREE;
12647 }
12648 /* If we could not find a corresponding TYPE, treat this
12649 declaration like an unqualified declaration. */
12650 if (type == error_mark_node)
12651 nested_name_specifier = NULL_TREE;
12652 /* Otherwise, count the number of templates used in TYPE and its
12653 containing scopes. */
21526606 12654 else
a723baf1
MM
12655 {
12656 tree scope;
12657
21526606 12658 for (scope = TREE_TYPE (type);
a723baf1 12659 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21526606 12660 scope = (TYPE_P (scope)
a723baf1 12661 ? TYPE_CONTEXT (scope)
21526606
EC
12662 : DECL_CONTEXT (scope)))
12663 if (TYPE_P (scope)
a723baf1
MM
12664 && CLASS_TYPE_P (scope)
12665 && CLASSTYPE_TEMPLATE_INFO (scope)
2050a1bb
MM
12666 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12667 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
a723baf1
MM
12668 ++num_templates;
12669 }
12670 }
12671 /* Otherwise, the identifier is optional. */
12672 else
12673 {
12674 /* We don't know whether what comes next is a template-id,
12675 an identifier, or nothing at all. */
12676 cp_parser_parse_tentatively (parser);
12677 /* Check for a template-id. */
21526606 12678 id = cp_parser_template_id (parser,
a723baf1 12679 /*template_keyword_p=*/false,
a668c6ad
MM
12680 /*check_dependency_p=*/true,
12681 /*is_declaration=*/true);
a723baf1
MM
12682 /* If that didn't work, it could still be an identifier. */
12683 if (!cp_parser_parse_definitely (parser))
12684 {
12685 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12686 id = cp_parser_identifier (parser);
12687 else
12688 id = NULL_TREE;
12689 }
12690 else
12691 {
12692 template_id_p = true;
12693 ++num_templates;
12694 }
12695 }
12696
8d241e0b
KL
12697 pop_deferring_access_checks ();
12698
15077df5
MM
12699 if (id)
12700 cp_parser_check_for_invalid_template_id (parser, id);
ee43dab5 12701
a723baf1
MM
12702 /* If it's not a `:' or a `{' then we can't really be looking at a
12703 class-head, since a class-head only appears as part of a
12704 class-specifier. We have to detect this situation before calling
12705 xref_tag, since that has irreversible side-effects. */
12706 if (!cp_parser_next_token_starts_class_definition_p (parser))
12707 {
2a13a625 12708 cp_parser_error (parser, "expected %<{%> or %<:%>");
a723baf1
MM
12709 return error_mark_node;
12710 }
12711
12712 /* At this point, we're going ahead with the class-specifier, even
12713 if some other problem occurs. */
12714 cp_parser_commit_to_tentative_parse (parser);
12715 /* Issue the error about the overly-qualified name now. */
12716 if (qualified_p)
12717 cp_parser_error (parser,
12718 "global qualification of class name is invalid");
12719 else if (invalid_nested_name_p)
12720 cp_parser_error (parser,
12721 "qualified name does not name a class");
88081599
MM
12722 else if (nested_name_specifier)
12723 {
12724 tree scope;
9bf0e588
VR
12725
12726 /* Reject typedef-names in class heads. */
12727 if (!DECL_IMPLICIT_TYPEDEF_P (type))
12728 {
12729 error ("invalid class name in declaration of %qD", type);
12730 type = NULL_TREE;
12731 goto done;
12732 }
12733
88081599
MM
12734 /* Figure out in what scope the declaration is being placed. */
12735 scope = current_scope ();
88081599
MM
12736 /* If that scope does not contain the scope in which the
12737 class was originally declared, the program is invalid. */
12738 if (scope && !is_ancestor (scope, nested_name_specifier))
12739 {
2a13a625
GDR
12740 error ("declaration of %qD in %qD which does not enclose %qD",
12741 type, scope, nested_name_specifier);
88081599
MM
12742 type = NULL_TREE;
12743 goto done;
12744 }
12745 /* [dcl.meaning]
12746
12747 A declarator-id shall not be qualified exception of the
12748 definition of a ... nested class outside of its class
12749 ... [or] a the definition or explicit instantiation of a
12750 class member of a namespace outside of its namespace. */
12751 if (scope == nested_name_specifier)
12752 {
12753 pedwarn ("extra qualification ignored");
12754 nested_name_specifier = NULL_TREE;
12755 num_templates = 0;
12756 }
12757 }
afb0918a
MM
12758 /* An explicit-specialization must be preceded by "template <>". If
12759 it is not, try to recover gracefully. */
21526606 12760 if (at_namespace_scope_p ()
afb0918a 12761 && parser->num_template_parameter_lists == 0
eeb23c11 12762 && template_id_p)
afb0918a 12763 {
2a13a625 12764 error ("an explicit specialization must be preceded by %<template <>%>");
afb0918a
MM
12765 invalid_explicit_specialization_p = true;
12766 /* Take the same action that would have been taken by
12767 cp_parser_explicit_specialization. */
12768 ++parser->num_template_parameter_lists;
12769 begin_specialization ();
12770 }
12771 /* There must be no "return" statements between this point and the
12772 end of this function; set "type "to the correct return value and
12773 use "goto done;" to return. */
a723baf1
MM
12774 /* Make sure that the right number of template parameters were
12775 present. */
12776 if (!cp_parser_check_template_parameters (parser, num_templates))
afb0918a
MM
12777 {
12778 /* If something went wrong, there is no point in even trying to
12779 process the class-definition. */
12780 type = NULL_TREE;
12781 goto done;
12782 }
a723baf1 12783
a723baf1
MM
12784 /* Look up the type. */
12785 if (template_id_p)
12786 {
12787 type = TREE_TYPE (id);
12788 maybe_process_partial_specialization (type);
4514aa8c
NS
12789 if (nested_name_specifier)
12790 pushed_scope = push_scope (nested_name_specifier);
a723baf1 12791 }
4514aa8c 12792 else if (nested_name_specifier)
a723baf1 12793 {
a723baf1
MM
12794 tree class_type;
12795
12796 /* Given:
12797
12798 template <typename T> struct S { struct T };
14d22dd6 12799 template <typename T> struct S<T>::T { };
a723baf1
MM
12800
12801 we will get a TYPENAME_TYPE when processing the definition of
12802 `S::T'. We need to resolve it to the actual type before we
12803 try to define it. */
12804 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12805 {
14d22dd6
MM
12806 class_type = resolve_typename_type (TREE_TYPE (type),
12807 /*only_current_p=*/false);
12808 if (class_type != error_mark_node)
12809 type = TYPE_NAME (class_type);
12810 else
12811 {
12812 cp_parser_error (parser, "could not resolve typename type");
12813 type = error_mark_node;
12814 }
a723baf1
MM
12815 }
12816
560ad596
MM
12817 maybe_process_partial_specialization (TREE_TYPE (type));
12818 class_type = current_class_type;
12819 /* Enter the scope indicated by the nested-name-specifier. */
4514aa8c 12820 pushed_scope = push_scope (nested_name_specifier);
560ad596
MM
12821 /* Get the canonical version of this type. */
12822 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12823 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12824 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
55dcbc12
NS
12825 {
12826 type = push_template_decl (type);
12827 if (type == error_mark_node)
12828 {
12829 type = NULL_TREE;
12830 goto done;
12831 }
12832 }
12833
560ad596 12834 type = TREE_TYPE (type);
4514aa8c 12835 *nested_name_specifier_p = true;
a723baf1 12836 }
4514aa8c
NS
12837 else /* The name is not a nested name. */
12838 {
12839 /* If the class was unnamed, create a dummy name. */
12840 if (!id)
12841 id = make_anon_name ();
12842 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
12843 parser->num_template_parameter_lists);
12844 }
12845
a723baf1
MM
12846 /* Indicate whether this class was declared as a `class' or as a
12847 `struct'. */
12848 if (TREE_CODE (type) == RECORD_TYPE)
12849 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12850 cp_parser_check_class_key (class_key, type);
12851
4514aa8c
NS
12852 /* We will have entered the scope containing the class; the names of
12853 base classes should be looked up in that context. For example,
12854 given:
a723baf1
MM
12855
12856 struct A { struct B {}; struct C; };
12857 struct A::C : B {};
12858
12859 is valid. */
cad7e87b 12860 bases = NULL_TREE;
98ca843c 12861
cad7e87b
NS
12862 /* Get the list of base-classes, if there is one. */
12863 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12864 bases = cp_parser_base_clause (parser);
98ca843c 12865
cad7e87b
NS
12866 /* Process the base classes. */
12867 xref_basetypes (type, bases);
a723baf1 12868
4514aa8c 12869 done:
a723baf1
MM
12870 /* Leave the scope given by the nested-name-specifier. We will
12871 enter the class scope itself while processing the members. */
4514aa8c
NS
12872 if (pushed_scope)
12873 pop_scope (pushed_scope);
a723baf1 12874
afb0918a
MM
12875 if (invalid_explicit_specialization_p)
12876 {
12877 end_specialization ();
12878 --parser->num_template_parameter_lists;
12879 }
38b305d0 12880 *attributes_p = attributes;
a723baf1
MM
12881 return type;
12882}
12883
12884/* Parse a class-key.
12885
12886 class-key:
12887 class
12888 struct
12889 union
12890
12891 Returns the kind of class-key specified, or none_type to indicate
12892 error. */
12893
12894static enum tag_types
94edc4ab 12895cp_parser_class_key (cp_parser* parser)
a723baf1
MM
12896{
12897 cp_token *token;
12898 enum tag_types tag_type;
12899
12900 /* Look for the class-key. */
12901 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12902 if (!token)
12903 return none_type;
12904
12905 /* Check to see if the TOKEN is a class-key. */
12906 tag_type = cp_parser_token_is_class_key (token);
12907 if (!tag_type)
12908 cp_parser_error (parser, "expected class-key");
12909 return tag_type;
12910}
12911
12912/* Parse an (optional) member-specification.
12913
12914 member-specification:
12915 member-declaration member-specification [opt]
12916 access-specifier : member-specification [opt] */
12917
12918static void
94edc4ab 12919cp_parser_member_specification_opt (cp_parser* parser)
a723baf1
MM
12920{
12921 while (true)
12922 {
12923 cp_token *token;
12924 enum rid keyword;
12925
12926 /* Peek at the next token. */
12927 token = cp_lexer_peek_token (parser->lexer);
12928 /* If it's a `}', or EOF then we've seen all the members. */
12929 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12930 break;
12931
12932 /* See if this token is a keyword. */
12933 keyword = token->keyword;
12934 switch (keyword)
12935 {
12936 case RID_PUBLIC:
12937 case RID_PROTECTED:
12938 case RID_PRIVATE:
12939 /* Consume the access-specifier. */
12940 cp_lexer_consume_token (parser->lexer);
12941 /* Remember which access-specifier is active. */
12942 current_access_specifier = token->value;
12943 /* Look for the `:'. */
12944 cp_parser_require (parser, CPP_COLON, "`:'");
12945 break;
12946
12947 default:
de3fe73c
MM
12948 /* Accept #pragmas at class scope. */
12949 if (token->type == CPP_PRAGMA)
12950 {
12951 cp_lexer_handle_pragma (parser->lexer);
12952 break;
12953 }
12954
a723baf1
MM
12955 /* Otherwise, the next construction must be a
12956 member-declaration. */
12957 cp_parser_member_declaration (parser);
a723baf1
MM
12958 }
12959 }
12960}
12961
21526606 12962/* Parse a member-declaration.
a723baf1
MM
12963
12964 member-declaration:
12965 decl-specifier-seq [opt] member-declarator-list [opt] ;
12966 function-definition ; [opt]
12967 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12968 using-declaration
21526606 12969 template-declaration
a723baf1
MM
12970
12971 member-declarator-list:
12972 member-declarator
12973 member-declarator-list , member-declarator
12974
12975 member-declarator:
21526606 12976 declarator pure-specifier [opt]
a723baf1 12977 declarator constant-initializer [opt]
21526606 12978 identifier [opt] : constant-expression
a723baf1
MM
12979
12980 GNU Extensions:
12981
12982 member-declaration:
12983 __extension__ member-declaration
12984
12985 member-declarator:
12986 declarator attributes [opt] pure-specifier [opt]
12987 declarator attributes [opt] constant-initializer [opt]
12988 identifier [opt] attributes [opt] : constant-expression */
12989
12990static void
94edc4ab 12991cp_parser_member_declaration (cp_parser* parser)
a723baf1 12992{
62d1db17 12993 cp_decl_specifier_seq decl_specifiers;
a723baf1
MM
12994 tree prefix_attributes;
12995 tree decl;
560ad596 12996 int declares_class_or_enum;
a723baf1
MM
12997 bool friend_p;
12998 cp_token *token;
12999 int saved_pedantic;
13000
13001 /* Check for the `__extension__' keyword. */
13002 if (cp_parser_extension_opt (parser, &saved_pedantic))
13003 {
13004 /* Recurse. */
13005 cp_parser_member_declaration (parser);
13006 /* Restore the old value of the PEDANTIC flag. */
13007 pedantic = saved_pedantic;
13008
13009 return;
13010 }
13011
13012 /* Check for a template-declaration. */
13013 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13014 {
13015 /* Parse the template-declaration. */
13016 cp_parser_template_declaration (parser, /*member_p=*/true);
13017
13018 return;
13019 }
13020
13021 /* Check for a using-declaration. */
13022 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13023 {
13024 /* Parse the using-declaration. */
13025 cp_parser_using_declaration (parser);
13026
13027 return;
13028 }
21526606 13029
a723baf1 13030 /* Parse the decl-specifier-seq. */
62d1db17
MM
13031 cp_parser_decl_specifier_seq (parser,
13032 CP_PARSER_FLAGS_OPTIONAL,
13033 &decl_specifiers,
13034 &declares_class_or_enum);
13035 prefix_attributes = decl_specifiers.attributes;
13036 decl_specifiers.attributes = NULL_TREE;
8fbc5ae7 13037 /* Check for an invalid type-name. */
de3fe73c
MM
13038 if (!decl_specifiers.type
13039 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8fbc5ae7 13040 return;
a723baf1
MM
13041 /* If there is no declarator, then the decl-specifier-seq should
13042 specify a type. */
13043 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13044 {
13045 /* If there was no decl-specifier-seq, and the next token is a
13046 `;', then we have something like:
13047
13048 struct S { ; };
13049
13050 [class.mem]
13051
13052 Each member-declaration shall declare at least one member
13053 name of the class. */
62d1db17 13054 if (!decl_specifiers.any_specifiers_p)
a723baf1 13055 {
2cfe82fe
ZW
13056 cp_token *token = cp_lexer_peek_token (parser->lexer);
13057 if (pedantic && !token->in_system_header)
13058 pedwarn ("%Hextra %<;%>", &token->location);
a723baf1 13059 }
21526606 13060 else
a723baf1
MM
13061 {
13062 tree type;
21526606 13063
a723baf1 13064 /* See if this declaration is a friend. */
62d1db17 13065 friend_p = cp_parser_friend_p (&decl_specifiers);
a723baf1
MM
13066 /* If there were decl-specifiers, check to see if there was
13067 a class-declaration. */
62d1db17 13068 type = check_tag_decl (&decl_specifiers);
a723baf1
MM
13069 /* Nested classes have already been added to the class, but
13070 a `friend' needs to be explicitly registered. */
13071 if (friend_p)
13072 {
13073 /* If the `friend' keyword was present, the friend must
13074 be introduced with a class-key. */
13075 if (!declares_class_or_enum)
13076 error ("a class-key must be used when declaring a friend");
13077 /* In this case:
13078
21526606
EC
13079 template <typename T> struct A {
13080 friend struct A<T>::B;
a723baf1 13081 };
21526606 13082
a723baf1
MM
13083 A<T>::B will be represented by a TYPENAME_TYPE, and
13084 therefore not recognized by check_tag_decl. */
98ca843c 13085 if (!type
62d1db17
MM
13086 && decl_specifiers.type
13087 && TYPE_P (decl_specifiers.type))
13088 type = decl_specifiers.type;
fdd09134 13089 if (!type || !TYPE_P (type))
a723baf1
MM
13090 error ("friend declaration does not name a class or "
13091 "function");
13092 else
19db77ce
KL
13093 make_friend_class (current_class_type, type,
13094 /*complain=*/true);
a723baf1
MM
13095 }
13096 /* If there is no TYPE, an error message will already have
13097 been issued. */
62d1db17 13098 else if (!type || type == error_mark_node)
a723baf1
MM
13099 ;
13100 /* An anonymous aggregate has to be handled specially; such
13101 a declaration really declares a data member (with a
13102 particular type), as opposed to a nested class. */
13103 else if (ANON_AGGR_TYPE_P (type))
13104 {
13105 /* Remove constructors and such from TYPE, now that we
34cd5ae7 13106 know it is an anonymous aggregate. */
a723baf1
MM
13107 fixup_anonymous_aggr (type);
13108 /* And make the corresponding data member. */
13109 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13110 /* Add it to the class. */
13111 finish_member_declaration (decl);
13112 }
37d407a1
KL
13113 else
13114 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
a723baf1
MM
13115 }
13116 }
13117 else
13118 {
13119 /* See if these declarations will be friends. */
62d1db17 13120 friend_p = cp_parser_friend_p (&decl_specifiers);
a723baf1 13121
21526606 13122 /* Keep going until we hit the `;' at the end of the
a723baf1
MM
13123 declaration. */
13124 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13125 {
13126 tree attributes = NULL_TREE;
13127 tree first_attribute;
13128
13129 /* Peek at the next token. */
13130 token = cp_lexer_peek_token (parser->lexer);
13131
13132 /* Check for a bitfield declaration. */
13133 if (token->type == CPP_COLON
13134 || (token->type == CPP_NAME
21526606 13135 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1
MM
13136 == CPP_COLON))
13137 {
13138 tree identifier;
13139 tree width;
13140
13141 /* Get the name of the bitfield. Note that we cannot just
13142 check TOKEN here because it may have been invalidated by
13143 the call to cp_lexer_peek_nth_token above. */
13144 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13145 identifier = cp_parser_identifier (parser);
13146 else
13147 identifier = NULL_TREE;
13148
13149 /* Consume the `:' token. */
13150 cp_lexer_consume_token (parser->lexer);
13151 /* Get the width of the bitfield. */
21526606 13152 width
14d22dd6
MM
13153 = cp_parser_constant_expression (parser,
13154 /*allow_non_constant=*/false,
13155 NULL);
a723baf1
MM
13156
13157 /* Look for attributes that apply to the bitfield. */
13158 attributes = cp_parser_attributes_opt (parser);
13159 /* Remember which attributes are prefix attributes and
13160 which are not. */
13161 first_attribute = attributes;
13162 /* Combine the attributes. */
13163 attributes = chainon (prefix_attributes, attributes);
13164
13165 /* Create the bitfield declaration. */
98ca843c 13166 decl = grokbitfield (identifier
1d786913
MM
13167 ? make_id_declarator (NULL_TREE,
13168 identifier)
058b15c1 13169 : NULL,
62d1db17 13170 &decl_specifiers,
a723baf1
MM
13171 width);
13172 /* Apply the attributes. */
13173 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13174 }
13175 else
13176 {
058b15c1 13177 cp_declarator *declarator;
a723baf1
MM
13178 tree initializer;
13179 tree asm_specification;
7efa3e22 13180 int ctor_dtor_or_conv_p;
a723baf1
MM
13181
13182 /* Parse the declarator. */
21526606 13183 declarator
62b8a44e 13184 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 13185 &ctor_dtor_or_conv_p,
db86dd14
MM
13186 /*parenthesized_p=*/NULL,
13187 /*member_p=*/true);
a723baf1
MM
13188
13189 /* If something went wrong parsing the declarator, make sure
13190 that we at least consume some tokens. */
058b15c1 13191 if (declarator == cp_error_declarator)
a723baf1
MM
13192 {
13193 /* Skip to the end of the statement. */
13194 cp_parser_skip_to_end_of_statement (parser);
4bb8ca28
MM
13195 /* If the next token is not a semicolon, that is
13196 probably because we just skipped over the body of
13197 a function. So, we consume a semicolon if
13198 present, but do not issue an error message if it
13199 is not present. */
13200 if (cp_lexer_next_token_is (parser->lexer,
13201 CPP_SEMICOLON))
13202 cp_lexer_consume_token (parser->lexer);
13203 return;
a723baf1
MM
13204 }
13205
fc6a28d7
MM
13206 if (declares_class_or_enum & 2)
13207 cp_parser_check_for_definition_in_return_type
13208 (declarator, decl_specifiers.type);
560ad596 13209
a723baf1
MM
13210 /* Look for an asm-specification. */
13211 asm_specification = cp_parser_asm_specification_opt (parser);
13212 /* Look for attributes that apply to the declaration. */
13213 attributes = cp_parser_attributes_opt (parser);
13214 /* Remember which attributes are prefix attributes and
13215 which are not. */
13216 first_attribute = attributes;
13217 /* Combine the attributes. */
13218 attributes = chainon (prefix_attributes, attributes);
13219
13220 /* If it's an `=', then we have a constant-initializer or a
13221 pure-specifier. It is not correct to parse the
13222 initializer before registering the member declaration
13223 since the member declaration should be in scope while
13224 its initializer is processed. However, the rest of the
13225 front end does not yet provide an interface that allows
13226 us to handle this correctly. */
13227 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13228 {
13229 /* In [class.mem]:
13230
13231 A pure-specifier shall be used only in the declaration of
21526606 13232 a virtual function.
a723baf1
MM
13233
13234 A member-declarator can contain a constant-initializer
13235 only if it declares a static member of integral or
21526606 13236 enumeration type.
a723baf1
MM
13237
13238 Therefore, if the DECLARATOR is for a function, we look
13239 for a pure-specifier; otherwise, we look for a
13240 constant-initializer. When we call `grokfield', it will
13241 perform more stringent semantics checks. */
058b15c1 13242 if (declarator->kind == cdk_function)
a723baf1
MM
13243 initializer = cp_parser_pure_specifier (parser);
13244 else
4bb8ca28
MM
13245 /* Parse the initializer. */
13246 initializer = cp_parser_constant_initializer (parser);
a723baf1
MM
13247 }
13248 /* Otherwise, there is no initializer. */
13249 else
13250 initializer = NULL_TREE;
13251
13252 /* See if we are probably looking at a function
5a19910e 13253 definition. We are certainly not looking at a
a723baf1
MM
13254 member-declarator. Calling `grokfield' has
13255 side-effects, so we must not do it unless we are sure
13256 that we are looking at a member-declarator. */
21526606 13257 if (cp_parser_token_starts_function_definition_p
a723baf1 13258 (cp_lexer_peek_token (parser->lexer)))
4bb8ca28
MM
13259 {
13260 /* The grammar does not allow a pure-specifier to be
13261 used when a member function is defined. (It is
13262 possible that this fact is an oversight in the
13263 standard, since a pure function may be defined
13264 outside of the class-specifier. */
13265 if (initializer)
13266 error ("pure-specifier on function-definition");
13267 decl = cp_parser_save_member_function_body (parser,
62d1db17 13268 &decl_specifiers,
4bb8ca28
MM
13269 declarator,
13270 attributes);
13271 /* If the member was not a friend, declare it here. */
13272 if (!friend_p)
13273 finish_member_declaration (decl);
13274 /* Peek at the next token. */
13275 token = cp_lexer_peek_token (parser->lexer);
13276 /* If the next token is a semicolon, consume it. */
13277 if (token->type == CPP_SEMICOLON)
13278 cp_lexer_consume_token (parser->lexer);
13279 return;
13280 }
a723baf1 13281 else
39703eb9
MM
13282 {
13283 /* Create the declaration. */
62d1db17 13284 decl = grokfield (declarator, &decl_specifiers,
ee3071ef 13285 initializer, asm_specification,
39703eb9
MM
13286 attributes);
13287 /* Any initialization must have been from a
13288 constant-expression. */
13289 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13290 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13291 }
a723baf1
MM
13292 }
13293
13294 /* Reset PREFIX_ATTRIBUTES. */
13295 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13296 attributes = TREE_CHAIN (attributes);
13297 if (attributes)
13298 TREE_CHAIN (attributes) = NULL_TREE;
13299
13300 /* If there is any qualification still in effect, clear it
13301 now; we will be starting fresh with the next declarator. */
13302 parser->scope = NULL_TREE;
13303 parser->qualifying_scope = NULL_TREE;
13304 parser->object_scope = NULL_TREE;
13305 /* If it's a `,', then there are more declarators. */
13306 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13307 cp_lexer_consume_token (parser->lexer);
13308 /* If the next token isn't a `;', then we have a parse error. */
13309 else if (cp_lexer_next_token_is_not (parser->lexer,
13310 CPP_SEMICOLON))
13311 {
2a13a625 13312 cp_parser_error (parser, "expected %<;%>");
04c06002 13313 /* Skip tokens until we find a `;'. */
a723baf1
MM
13314 cp_parser_skip_to_end_of_statement (parser);
13315
13316 break;
13317 }
13318
13319 if (decl)
13320 {
13321 /* Add DECL to the list of members. */
13322 if (!friend_p)
13323 finish_member_declaration (decl);
13324
a723baf1 13325 if (TREE_CODE (decl) == FUNCTION_DECL)
8db1028e 13326 cp_parser_save_default_args (parser, decl);
a723baf1
MM
13327 }
13328 }
13329 }
13330
4bb8ca28 13331 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
a723baf1
MM
13332}
13333
13334/* Parse a pure-specifier.
13335
13336 pure-specifier:
13337 = 0
13338
13339 Returns INTEGER_ZERO_NODE if a pure specifier is found.
cd0be382 13340 Otherwise, ERROR_MARK_NODE is returned. */
a723baf1
MM
13341
13342static tree
94edc4ab 13343cp_parser_pure_specifier (cp_parser* parser)
a723baf1
MM
13344{
13345 cp_token *token;
13346
13347 /* Look for the `=' token. */
13348 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13349 return error_mark_node;
13350 /* Look for the `0' token. */
515e6a84
GB
13351 token = cp_lexer_consume_token (parser->lexer);
13352 if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13353 {
13354 cp_parser_error (parser,
13355 "invalid pure specifier (only `= 0' is allowed)");
13356 cp_parser_skip_to_end_of_statement (parser);
13357 return error_mark_node;
13358 }
a723baf1 13359
515e6a84
GB
13360 /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13361 We need to get information from the lexer about how the number
13362 was spelled in order to fix this problem. */
a723baf1
MM
13363 return integer_zero_node;
13364}
13365
13366/* Parse a constant-initializer.
13367
13368 constant-initializer:
13369 = constant-expression
13370
13371 Returns a representation of the constant-expression. */
13372
13373static tree
94edc4ab 13374cp_parser_constant_initializer (cp_parser* parser)
a723baf1
MM
13375{
13376 /* Look for the `=' token. */
13377 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13378 return error_mark_node;
13379
13380 /* It is invalid to write:
13381
13382 struct S { static const int i = { 7 }; };
13383
13384 */
13385 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13386 {
13387 cp_parser_error (parser,
13388 "a brace-enclosed initializer is not allowed here");
13389 /* Consume the opening brace. */
13390 cp_lexer_consume_token (parser->lexer);
13391 /* Skip the initializer. */
13392 cp_parser_skip_to_closing_brace (parser);
13393 /* Look for the trailing `}'. */
13394 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
21526606 13395
a723baf1
MM
13396 return error_mark_node;
13397 }
13398
21526606 13399 return cp_parser_constant_expression (parser,
14d22dd6
MM
13400 /*allow_non_constant=*/false,
13401 NULL);
a723baf1
MM
13402}
13403
13404/* Derived classes [gram.class.derived] */
13405
13406/* Parse a base-clause.
13407
13408 base-clause:
21526606 13409 : base-specifier-list
a723baf1
MM
13410
13411 base-specifier-list:
13412 base-specifier
13413 base-specifier-list , base-specifier
13414
13415 Returns a TREE_LIST representing the base-classes, in the order in
13416 which they were declared. The representation of each node is as
21526606 13417 described by cp_parser_base_specifier.
a723baf1
MM
13418
13419 In the case that no bases are specified, this function will return
13420 NULL_TREE, not ERROR_MARK_NODE. */
13421
13422static tree
94edc4ab 13423cp_parser_base_clause (cp_parser* parser)
a723baf1
MM
13424{
13425 tree bases = NULL_TREE;
13426
13427 /* Look for the `:' that begins the list. */
13428 cp_parser_require (parser, CPP_COLON, "`:'");
13429
13430 /* Scan the base-specifier-list. */
13431 while (true)
13432 {
13433 cp_token *token;
13434 tree base;
13435
13436 /* Look for the base-specifier. */
13437 base = cp_parser_base_specifier (parser);
13438 /* Add BASE to the front of the list. */
13439 if (base != error_mark_node)
13440 {
13441 TREE_CHAIN (base) = bases;
13442 bases = base;
13443 }
13444 /* Peek at the next token. */
13445 token = cp_lexer_peek_token (parser->lexer);
13446 /* If it's not a comma, then the list is complete. */
13447 if (token->type != CPP_COMMA)
13448 break;
13449 /* Consume the `,'. */
13450 cp_lexer_consume_token (parser->lexer);
13451 }
13452
13453 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13454 base class had a qualified name. However, the next name that
13455 appears is certainly not qualified. */
13456 parser->scope = NULL_TREE;
13457 parser->qualifying_scope = NULL_TREE;
13458 parser->object_scope = NULL_TREE;
13459
13460 return nreverse (bases);
13461}
13462
13463/* Parse a base-specifier.
13464
13465 base-specifier:
13466 :: [opt] nested-name-specifier [opt] class-name
13467 virtual access-specifier [opt] :: [opt] nested-name-specifier
13468 [opt] class-name
13469 access-specifier virtual [opt] :: [opt] nested-name-specifier
13470 [opt] class-name
13471
13472 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13473 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13474 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13475 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21526606 13476
a723baf1 13477static tree
94edc4ab 13478cp_parser_base_specifier (cp_parser* parser)
a723baf1
MM
13479{
13480 cp_token *token;
13481 bool done = false;
13482 bool virtual_p = false;
13483 bool duplicate_virtual_error_issued_p = false;
13484 bool duplicate_access_error_issued_p = false;
bbaab916 13485 bool class_scope_p, template_p;
dbbf88d1 13486 tree access = access_default_node;
a723baf1
MM
13487 tree type;
13488
13489 /* Process the optional `virtual' and `access-specifier'. */
13490 while (!done)
13491 {
13492 /* Peek at the next token. */
13493 token = cp_lexer_peek_token (parser->lexer);
13494 /* Process `virtual'. */
13495 switch (token->keyword)
13496 {
13497 case RID_VIRTUAL:
13498 /* If `virtual' appears more than once, issue an error. */
13499 if (virtual_p && !duplicate_virtual_error_issued_p)
13500 {
13501 cp_parser_error (parser,
2a13a625 13502 "%<virtual%> specified more than once in base-specified");
a723baf1
MM
13503 duplicate_virtual_error_issued_p = true;
13504 }
13505
13506 virtual_p = true;
13507
13508 /* Consume the `virtual' token. */
13509 cp_lexer_consume_token (parser->lexer);
13510
13511 break;
13512
13513 case RID_PUBLIC:
13514 case RID_PROTECTED:
13515 case RID_PRIVATE:
13516 /* If more than one access specifier appears, issue an
13517 error. */
dbbf88d1
NS
13518 if (access != access_default_node
13519 && !duplicate_access_error_issued_p)
a723baf1
MM
13520 {
13521 cp_parser_error (parser,
13522 "more than one access specifier in base-specified");
13523 duplicate_access_error_issued_p = true;
13524 }
13525
dbbf88d1 13526 access = ridpointers[(int) token->keyword];
a723baf1
MM
13527
13528 /* Consume the access-specifier. */
13529 cp_lexer_consume_token (parser->lexer);
13530
13531 break;
13532
13533 default:
13534 done = true;
13535 break;
13536 }
13537 }
852dcbdd 13538 /* It is not uncommon to see programs mechanically, erroneously, use
a3a503a5 13539 the 'typename' keyword to denote (dependent) qualified types
1ed53ef3
GB
13540 as base classes. */
13541 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13542 {
13543 if (!processing_template_decl)
2a13a625 13544 error ("keyword %<typename%> not allowed outside of templates");
1ed53ef3 13545 else
2a13a625 13546 error ("keyword %<typename%> not allowed in this context "
1ed53ef3
GB
13547 "(the base class is implicitly a type)");
13548 cp_lexer_consume_token (parser->lexer);
13549 }
a723baf1 13550
a723baf1
MM
13551 /* Look for the optional `::' operator. */
13552 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13553 /* Look for the nested-name-specifier. The simplest way to
13554 implement:
13555
13556 [temp.res]
13557
13558 The keyword `typename' is not permitted in a base-specifier or
13559 mem-initializer; in these contexts a qualified name that
13560 depends on a template-parameter is implicitly assumed to be a
13561 type name.
13562
13563 is to pretend that we have seen the `typename' keyword at this
21526606 13564 point. */
a723baf1
MM
13565 cp_parser_nested_name_specifier_opt (parser,
13566 /*typename_keyword_p=*/true,
13567 /*check_dependency_p=*/true,
fc6a28d7 13568 typename_type,
a668c6ad 13569 /*is_declaration=*/true);
a723baf1
MM
13570 /* If the base class is given by a qualified name, assume that names
13571 we see are type names or templates, as appropriate. */
13572 class_scope_p = (parser->scope && TYPE_P (parser->scope));
bbaab916 13573 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21526606 13574
a723baf1 13575 /* Finally, look for the class-name. */
21526606 13576 type = cp_parser_class_name (parser,
a723baf1 13577 class_scope_p,
bbaab916 13578 template_p,
fc6a28d7 13579 typename_type,
a723baf1 13580 /*check_dependency_p=*/true,
a668c6ad
MM
13581 /*class_head_p=*/false,
13582 /*is_declaration=*/true);
a723baf1
MM
13583
13584 if (type == error_mark_node)
13585 return error_mark_node;
13586
dbbf88d1 13587 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
a723baf1
MM
13588}
13589
13590/* Exception handling [gram.exception] */
13591
13592/* Parse an (optional) exception-specification.
13593
13594 exception-specification:
13595 throw ( type-id-list [opt] )
13596
13597 Returns a TREE_LIST representing the exception-specification. The
13598 TREE_VALUE of each node is a type. */
13599
13600static tree
94edc4ab 13601cp_parser_exception_specification_opt (cp_parser* parser)
a723baf1
MM
13602{
13603 cp_token *token;
13604 tree type_id_list;
13605
13606 /* Peek at the next token. */
13607 token = cp_lexer_peek_token (parser->lexer);
13608 /* If it's not `throw', then there's no exception-specification. */
13609 if (!cp_parser_is_keyword (token, RID_THROW))
13610 return NULL_TREE;
13611
13612 /* Consume the `throw'. */
13613 cp_lexer_consume_token (parser->lexer);
13614
13615 /* Look for the `('. */
13616 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13617
13618 /* Peek at the next token. */
13619 token = cp_lexer_peek_token (parser->lexer);
13620 /* If it's not a `)', then there is a type-id-list. */
13621 if (token->type != CPP_CLOSE_PAREN)
13622 {
13623 const char *saved_message;
13624
13625 /* Types may not be defined in an exception-specification. */
13626 saved_message = parser->type_definition_forbidden_message;
13627 parser->type_definition_forbidden_message
13628 = "types may not be defined in an exception-specification";
13629 /* Parse the type-id-list. */
13630 type_id_list = cp_parser_type_id_list (parser);
13631 /* Restore the saved message. */
13632 parser->type_definition_forbidden_message = saved_message;
13633 }
13634 else
13635 type_id_list = empty_except_spec;
13636
13637 /* Look for the `)'. */
13638 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13639
13640 return type_id_list;
13641}
13642
13643/* Parse an (optional) type-id-list.
13644
13645 type-id-list:
13646 type-id
13647 type-id-list , type-id
13648
13649 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13650 in the order that the types were presented. */
13651
13652static tree
94edc4ab 13653cp_parser_type_id_list (cp_parser* parser)
a723baf1
MM
13654{
13655 tree types = NULL_TREE;
13656
13657 while (true)
13658 {
13659 cp_token *token;
13660 tree type;
13661
13662 /* Get the next type-id. */
13663 type = cp_parser_type_id (parser);
13664 /* Add it to the list. */
13665 types = add_exception_specifier (types, type, /*complain=*/1);
13666 /* Peek at the next token. */
13667 token = cp_lexer_peek_token (parser->lexer);
13668 /* If it is not a `,', we are done. */
13669 if (token->type != CPP_COMMA)
13670 break;
13671 /* Consume the `,'. */
13672 cp_lexer_consume_token (parser->lexer);
13673 }
13674
13675 return nreverse (types);
13676}
13677
13678/* Parse a try-block.
13679
13680 try-block:
13681 try compound-statement handler-seq */
13682
13683static tree
94edc4ab 13684cp_parser_try_block (cp_parser* parser)
a723baf1
MM
13685{
13686 tree try_block;
13687
13688 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13689 try_block = begin_try_block ();
325c3691 13690 cp_parser_compound_statement (parser, NULL, true);
a723baf1
MM
13691 finish_try_block (try_block);
13692 cp_parser_handler_seq (parser);
13693 finish_handler_sequence (try_block);
13694
13695 return try_block;
13696}
13697
13698/* Parse a function-try-block.
13699
13700 function-try-block:
13701 try ctor-initializer [opt] function-body handler-seq */
13702
13703static bool
94edc4ab 13704cp_parser_function_try_block (cp_parser* parser)
a723baf1
MM
13705{
13706 tree try_block;
13707 bool ctor_initializer_p;
13708
13709 /* Look for the `try' keyword. */
13710 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13711 return false;
13712 /* Let the rest of the front-end know where we are. */
13713 try_block = begin_function_try_block ();
13714 /* Parse the function-body. */
21526606 13715 ctor_initializer_p
a723baf1
MM
13716 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13717 /* We're done with the `try' part. */
13718 finish_function_try_block (try_block);
13719 /* Parse the handlers. */
13720 cp_parser_handler_seq (parser);
13721 /* We're done with the handlers. */
13722 finish_function_handler_sequence (try_block);
13723
13724 return ctor_initializer_p;
13725}
13726
13727/* Parse a handler-seq.
13728
13729 handler-seq:
13730 handler handler-seq [opt] */
13731
13732static void
94edc4ab 13733cp_parser_handler_seq (cp_parser* parser)
a723baf1
MM
13734{
13735 while (true)
13736 {
13737 cp_token *token;
13738
13739 /* Parse the handler. */
13740 cp_parser_handler (parser);
13741 /* Peek at the next token. */
13742 token = cp_lexer_peek_token (parser->lexer);
13743 /* If it's not `catch' then there are no more handlers. */
13744 if (!cp_parser_is_keyword (token, RID_CATCH))
13745 break;
13746 }
13747}
13748
13749/* Parse a handler.
13750
13751 handler:
13752 catch ( exception-declaration ) compound-statement */
13753
13754static void
94edc4ab 13755cp_parser_handler (cp_parser* parser)
a723baf1
MM
13756{
13757 tree handler;
13758 tree declaration;
13759
13760 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13761 handler = begin_handler ();
13762 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13763 declaration = cp_parser_exception_declaration (parser);
13764 finish_handler_parms (declaration, handler);
13765 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
325c3691 13766 cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
13767 finish_handler (handler);
13768}
13769
13770/* Parse an exception-declaration.
13771
13772 exception-declaration:
13773 type-specifier-seq declarator
13774 type-specifier-seq abstract-declarator
13775 type-specifier-seq
21526606 13776 ...
a723baf1
MM
13777
13778 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13779 ellipsis variant is used. */
13780
13781static tree
94edc4ab 13782cp_parser_exception_declaration (cp_parser* parser)
a723baf1 13783{
058b15c1 13784 tree decl;
62d1db17 13785 cp_decl_specifier_seq type_specifiers;
058b15c1 13786 cp_declarator *declarator;
a723baf1
MM
13787 const char *saved_message;
13788
13789 /* If it's an ellipsis, it's easy to handle. */
13790 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13791 {
13792 /* Consume the `...' token. */
13793 cp_lexer_consume_token (parser->lexer);
13794 return NULL_TREE;
13795 }
13796
13797 /* Types may not be defined in exception-declarations. */
13798 saved_message = parser->type_definition_forbidden_message;
13799 parser->type_definition_forbidden_message
13800 = "types may not be defined in exception-declarations";
13801
13802 /* Parse the type-specifier-seq. */
62d1db17 13803 cp_parser_type_specifier_seq (parser, &type_specifiers);
a723baf1
MM
13804 /* If it's a `)', then there is no declarator. */
13805 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
058b15c1 13806 declarator = NULL;
a723baf1 13807 else
62b8a44e 13808 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
4bb8ca28 13809 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
13810 /*parenthesized_p=*/NULL,
13811 /*member_p=*/false);
a723baf1
MM
13812
13813 /* Restore the saved message. */
13814 parser->type_definition_forbidden_message = saved_message;
13815
62d1db17 13816 if (type_specifiers.any_specifiers_p)
058b15c1 13817 {
62d1db17 13818 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
058b15c1
MM
13819 if (decl == NULL_TREE)
13820 error ("invalid catch parameter");
13821 }
13822 else
13823 decl = NULL_TREE;
13824
13825 return decl;
a723baf1
MM
13826}
13827
21526606 13828/* Parse a throw-expression.
a723baf1
MM
13829
13830 throw-expression:
34cd5ae7 13831 throw assignment-expression [opt]
a723baf1
MM
13832
13833 Returns a THROW_EXPR representing the throw-expression. */
13834
13835static tree
94edc4ab 13836cp_parser_throw_expression (cp_parser* parser)
a723baf1
MM
13837{
13838 tree expression;
89f1a6ec 13839 cp_token* token;
a723baf1
MM
13840
13841 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
89f1a6ec
MM
13842 token = cp_lexer_peek_token (parser->lexer);
13843 /* Figure out whether or not there is an assignment-expression
13844 following the "throw" keyword. */
13845 if (token->type == CPP_COMMA
13846 || token->type == CPP_SEMICOLON
13847 || token->type == CPP_CLOSE_PAREN
13848 || token->type == CPP_CLOSE_SQUARE
13849 || token->type == CPP_CLOSE_BRACE
13850 || token->type == CPP_COLON)
a723baf1 13851 expression = NULL_TREE;
89f1a6ec 13852 else
93678513
MM
13853 expression = cp_parser_assignment_expression (parser,
13854 /*cast_p=*/false);
a723baf1
MM
13855
13856 return build_throw (expression);
13857}
13858
13859/* GNU Extensions */
13860
13861/* Parse an (optional) asm-specification.
13862
13863 asm-specification:
13864 asm ( string-literal )
13865
13866 If the asm-specification is present, returns a STRING_CST
13867 corresponding to the string-literal. Otherwise, returns
13868 NULL_TREE. */
13869
13870static tree
94edc4ab 13871cp_parser_asm_specification_opt (cp_parser* parser)
a723baf1
MM
13872{
13873 cp_token *token;
13874 tree asm_specification;
13875
13876 /* Peek at the next token. */
13877 token = cp_lexer_peek_token (parser->lexer);
21526606 13878 /* If the next token isn't the `asm' keyword, then there's no
a723baf1
MM
13879 asm-specification. */
13880 if (!cp_parser_is_keyword (token, RID_ASM))
13881 return NULL_TREE;
13882
13883 /* Consume the `asm' token. */
13884 cp_lexer_consume_token (parser->lexer);
13885 /* Look for the `('. */
13886 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13887
13888 /* Look for the string-literal. */
c162c75e 13889 asm_specification = cp_parser_string_literal (parser, false, false);
a723baf1
MM
13890
13891 /* Look for the `)'. */
13892 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13893
13894 return asm_specification;
13895}
13896
21526606 13897/* Parse an asm-operand-list.
a723baf1
MM
13898
13899 asm-operand-list:
13900 asm-operand
13901 asm-operand-list , asm-operand
21526606 13902
a723baf1 13903 asm-operand:
21526606 13904 string-literal ( expression )
a723baf1
MM
13905 [ string-literal ] string-literal ( expression )
13906
13907 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13908 each node is the expression. The TREE_PURPOSE is itself a
13909 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13910 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13911 is a STRING_CST for the string literal before the parenthesis. */
13912
13913static tree
94edc4ab 13914cp_parser_asm_operand_list (cp_parser* parser)
a723baf1
MM
13915{
13916 tree asm_operands = NULL_TREE;
13917
13918 while (true)
13919 {
13920 tree string_literal;
13921 tree expression;
13922 tree name;
21526606 13923
21526606 13924 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
a723baf1
MM
13925 {
13926 /* Consume the `[' token. */
13927 cp_lexer_consume_token (parser->lexer);
13928 /* Read the operand name. */
13929 name = cp_parser_identifier (parser);
21526606 13930 if (name != error_mark_node)
a723baf1
MM
13931 name = build_string (IDENTIFIER_LENGTH (name),
13932 IDENTIFIER_POINTER (name));
13933 /* Look for the closing `]'. */
13934 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13935 }
13936 else
13937 name = NULL_TREE;
13938 /* Look for the string-literal. */
c162c75e
MA
13939 string_literal = cp_parser_string_literal (parser, false, false);
13940
a723baf1
MM
13941 /* Look for the `('. */
13942 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13943 /* Parse the expression. */
93678513 13944 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
13945 /* Look for the `)'. */
13946 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
c162c75e 13947
a723baf1
MM
13948 /* Add this operand to the list. */
13949 asm_operands = tree_cons (build_tree_list (name, string_literal),
21526606 13950 expression,
a723baf1 13951 asm_operands);
21526606 13952 /* If the next token is not a `,', there are no more
a723baf1
MM
13953 operands. */
13954 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13955 break;
13956 /* Consume the `,'. */
13957 cp_lexer_consume_token (parser->lexer);
13958 }
13959
13960 return nreverse (asm_operands);
13961}
13962
21526606 13963/* Parse an asm-clobber-list.
a723baf1
MM
13964
13965 asm-clobber-list:
13966 string-literal
21526606 13967 asm-clobber-list , string-literal
a723baf1
MM
13968
13969 Returns a TREE_LIST, indicating the clobbers in the order that they
13970 appeared. The TREE_VALUE of each node is a STRING_CST. */
13971
13972static tree
94edc4ab 13973cp_parser_asm_clobber_list (cp_parser* parser)
a723baf1
MM
13974{
13975 tree clobbers = NULL_TREE;
13976
13977 while (true)
13978 {
a723baf1
MM
13979 tree string_literal;
13980
13981 /* Look for the string literal. */
c162c75e 13982 string_literal = cp_parser_string_literal (parser, false, false);
a723baf1
MM
13983 /* Add it to the list. */
13984 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21526606 13985 /* If the next token is not a `,', then the list is
a723baf1
MM
13986 complete. */
13987 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13988 break;
13989 /* Consume the `,' token. */
13990 cp_lexer_consume_token (parser->lexer);
13991 }
13992
13993 return clobbers;
13994}
13995
13996/* Parse an (optional) series of attributes.
13997
13998 attributes:
13999 attributes attribute
14000
14001 attribute:
21526606 14002 __attribute__ (( attribute-list [opt] ))
a723baf1
MM
14003
14004 The return value is as for cp_parser_attribute_list. */
21526606 14005
a723baf1 14006static tree
94edc4ab 14007cp_parser_attributes_opt (cp_parser* parser)
a723baf1
MM
14008{
14009 tree attributes = NULL_TREE;
14010
14011 while (true)
14012 {
14013 cp_token *token;
14014 tree attribute_list;
14015
14016 /* Peek at the next token. */
14017 token = cp_lexer_peek_token (parser->lexer);
14018 /* If it's not `__attribute__', then we're done. */
14019 if (token->keyword != RID_ATTRIBUTE)
14020 break;
14021
14022 /* Consume the `__attribute__' keyword. */
14023 cp_lexer_consume_token (parser->lexer);
14024 /* Look for the two `(' tokens. */
14025 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14026 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14027
14028 /* Peek at the next token. */
14029 token = cp_lexer_peek_token (parser->lexer);
14030 if (token->type != CPP_CLOSE_PAREN)
14031 /* Parse the attribute-list. */
14032 attribute_list = cp_parser_attribute_list (parser);
14033 else
14034 /* If the next token is a `)', then there is no attribute
14035 list. */
14036 attribute_list = NULL;
14037
14038 /* Look for the two `)' tokens. */
14039 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14040 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14041
14042 /* Add these new attributes to the list. */
14043 attributes = chainon (attributes, attribute_list);
14044 }
14045
14046 return attributes;
14047}
14048
21526606 14049/* Parse an attribute-list.
a723baf1 14050
21526606
EC
14051 attribute-list:
14052 attribute
a723baf1
MM
14053 attribute-list , attribute
14054
14055 attribute:
21526606 14056 identifier
a723baf1
MM
14057 identifier ( identifier )
14058 identifier ( identifier , expression-list )
21526606 14059 identifier ( expression-list )
a723baf1 14060
88e95ee3
MM
14061 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14062 to an attribute. The TREE_PURPOSE of each node is the identifier
14063 indicating which attribute is in use. The TREE_VALUE represents
14064 the arguments, if any. */
a723baf1
MM
14065
14066static tree
94edc4ab 14067cp_parser_attribute_list (cp_parser* parser)
a723baf1
MM
14068{
14069 tree attribute_list = NULL_TREE;
c162c75e 14070 bool save_translate_strings_p = parser->translate_strings_p;
a723baf1 14071
c162c75e 14072 parser->translate_strings_p = false;
a723baf1
MM
14073 while (true)
14074 {
14075 cp_token *token;
14076 tree identifier;
14077 tree attribute;
14078
14079 /* Look for the identifier. We also allow keywords here; for
14080 example `__attribute__ ((const))' is legal. */
14081 token = cp_lexer_peek_token (parser->lexer);
88e95ee3
MM
14082 if (token->type == CPP_NAME
14083 || token->type == CPP_KEYWORD)
14084 {
14085 /* Consume the token. */
14086 token = cp_lexer_consume_token (parser->lexer);
21526606 14087
88e95ee3
MM
14088 /* Save away the identifier that indicates which attribute
14089 this is. */
14090 identifier = token->value;
14091 attribute = build_tree_list (identifier, NULL_TREE);
a723baf1 14092
88e95ee3
MM
14093 /* Peek at the next token. */
14094 token = cp_lexer_peek_token (parser->lexer);
14095 /* If it's an `(', then parse the attribute arguments. */
14096 if (token->type == CPP_OPEN_PAREN)
14097 {
14098 tree arguments;
a723baf1 14099
88e95ee3
MM
14100 arguments = (cp_parser_parenthesized_expression_list
14101 (parser, true, /*cast_p=*/false,
14102 /*non_constant_p=*/NULL));
14103 /* Save the identifier and arguments away. */
14104 TREE_VALUE (attribute) = arguments;
14105 }
a723baf1 14106
88e95ee3
MM
14107 /* Add this attribute to the list. */
14108 TREE_CHAIN (attribute) = attribute_list;
14109 attribute_list = attribute;
a723baf1 14110
88e95ee3
MM
14111 token = cp_lexer_peek_token (parser->lexer);
14112 }
14113 /* Now, look for more attributes. If the next token isn't a
14114 `,', we're done. */
a723baf1
MM
14115 if (token->type != CPP_COMMA)
14116 break;
14117
cd0be382 14118 /* Consume the comma and keep going. */
a723baf1
MM
14119 cp_lexer_consume_token (parser->lexer);
14120 }
c162c75e 14121 parser->translate_strings_p = save_translate_strings_p;
a723baf1
MM
14122
14123 /* We built up the list in reverse order. */
14124 return nreverse (attribute_list);
14125}
14126
14127/* Parse an optional `__extension__' keyword. Returns TRUE if it is
14128 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14129 current value of the PEDANTIC flag, regardless of whether or not
14130 the `__extension__' keyword is present. The caller is responsible
14131 for restoring the value of the PEDANTIC flag. */
14132
14133static bool
94edc4ab 14134cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
a723baf1
MM
14135{
14136 /* Save the old value of the PEDANTIC flag. */
14137 *saved_pedantic = pedantic;
14138
14139 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14140 {
14141 /* Consume the `__extension__' token. */
14142 cp_lexer_consume_token (parser->lexer);
14143 /* We're not being pedantic while the `__extension__' keyword is
14144 in effect. */
14145 pedantic = 0;
14146
14147 return true;
14148 }
14149
14150 return false;
14151}
14152
14153/* Parse a label declaration.
14154
14155 label-declaration:
14156 __label__ label-declarator-seq ;
14157
14158 label-declarator-seq:
14159 identifier , label-declarator-seq
14160 identifier */
14161
14162static void
94edc4ab 14163cp_parser_label_declaration (cp_parser* parser)
a723baf1
MM
14164{
14165 /* Look for the `__label__' keyword. */
14166 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14167
14168 while (true)
14169 {
14170 tree identifier;
14171
14172 /* Look for an identifier. */
14173 identifier = cp_parser_identifier (parser);
14174 /* Declare it as a lobel. */
14175 finish_label_decl (identifier);
14176 /* If the next token is a `;', stop. */
14177 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14178 break;
14179 /* Look for the `,' separating the label declarations. */
14180 cp_parser_require (parser, CPP_COMMA, "`,'");
14181 }
14182
14183 /* Look for the final `;'. */
14184 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14185}
14186
14187/* Support Functions */
14188
14189/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14190 NAME should have one of the representations used for an
14191 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14192 is returned. If PARSER->SCOPE is a dependent type, then a
14193 SCOPE_REF is returned.
14194
14195 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14196 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14197 was formed. Abstractly, such entities should not be passed to this
14198 function, because they do not need to be looked up, but it is
14199 simpler to check for this special case here, rather than at the
14200 call-sites.
14201
14202 In cases not explicitly covered above, this function returns a
14203 DECL, OVERLOAD, or baselink representing the result of the lookup.
14204 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14205 is returned.
14206
472c29c3 14207 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
fc6a28d7
MM
14208 (e.g., "struct") that was used. In that case bindings that do not
14209 refer to types are ignored.
a723baf1 14210
b0bc6e8e
KL
14211 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14212 ignored.
14213
eea9800f
MM
14214 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14215 are ignored.
14216
a723baf1 14217 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
8f78f01f
MM
14218 types.
14219
14220 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14221 results in an ambiguity, and false otherwise. */
a723baf1
MM
14222
14223static tree
21526606 14224cp_parser_lookup_name (cp_parser *parser, tree name,
fc6a28d7
MM
14225 enum tag_types tag_type,
14226 bool is_template, bool is_namespace,
8f78f01f
MM
14227 bool check_dependency,
14228 bool *ambiguous_p)
a723baf1
MM
14229{
14230 tree decl;
14231 tree object_type = parser->context->object_type;
14232
8f78f01f
MM
14233 /* Assume that the lookup will be unambiguous. */
14234 if (ambiguous_p)
14235 *ambiguous_p = false;
14236
a723baf1
MM
14237 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14238 no longer valid. Note that if we are parsing tentatively, and
14239 the parse fails, OBJECT_TYPE will be automatically restored. */
14240 parser->context->object_type = NULL_TREE;
14241
14242 if (name == error_mark_node)
14243 return error_mark_node;
14244
14245 /* A template-id has already been resolved; there is no lookup to
14246 do. */
14247 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14248 return name;
14249 if (BASELINK_P (name))
14250 {
50bc768d
NS
14251 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14252 == TEMPLATE_ID_EXPR);
a723baf1
MM
14253 return name;
14254 }
14255
14256 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14257 it should already have been checked to make sure that the name
14258 used matches the type being destroyed. */
14259 if (TREE_CODE (name) == BIT_NOT_EXPR)
14260 {
14261 tree type;
14262
14263 /* Figure out to which type this destructor applies. */
14264 if (parser->scope)
14265 type = parser->scope;
14266 else if (object_type)
14267 type = object_type;
14268 else
14269 type = current_class_type;
14270 /* If that's not a class type, there is no destructor. */
14271 if (!type || !CLASS_TYPE_P (type))
14272 return error_mark_node;
9f4faeae
MM
14273 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14274 lazily_declare_fn (sfk_destructor, type);
fd6e3cce
GB
14275 if (!CLASSTYPE_DESTRUCTORS (type))
14276 return error_mark_node;
a723baf1
MM
14277 /* If it was a class type, return the destructor. */
14278 return CLASSTYPE_DESTRUCTORS (type);
14279 }
14280
14281 /* By this point, the NAME should be an ordinary identifier. If
14282 the id-expression was a qualified name, the qualifying scope is
14283 stored in PARSER->SCOPE at this point. */
50bc768d 14284 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
21526606 14285
a723baf1
MM
14286 /* Perform the lookup. */
14287 if (parser->scope)
21526606 14288 {
1fb3244a 14289 bool dependent_p;
a723baf1
MM
14290
14291 if (parser->scope == error_mark_node)
14292 return error_mark_node;
14293
14294 /* If the SCOPE is dependent, the lookup must be deferred until
14295 the template is instantiated -- unless we are explicitly
14296 looking up names in uninstantiated templates. Even then, we
14297 cannot look up the name if the scope is not a class type; it
14298 might, for example, be a template type parameter. */
1fb3244a
MM
14299 dependent_p = (TYPE_P (parser->scope)
14300 && !(parser->in_declarator_p
14301 && currently_open_class (parser->scope))
14302 && dependent_type_p (parser->scope));
a723baf1 14303 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
1fb3244a 14304 && dependent_p)
a723baf1 14305 {
fc6a28d7
MM
14306 if (tag_type)
14307 {
14308 tree type;
14309
14310 /* The resolution to Core Issue 180 says that `struct
14311 A::B' should be considered a type-name, even if `A'
14312 is dependent. */
14313 type = make_typename_type (parser->scope, name, tag_type,
14314 /*complain=*/1);
fc6a28d7
MM
14315 decl = TYPE_NAME (type);
14316 }
b0bc6e8e 14317 else if (is_template)
5b4acce1 14318 decl = make_unbound_class_template (parser->scope,
b939a023 14319 name, NULL_TREE,
5b4acce1 14320 /*complain=*/1);
b0bc6e8e
KL
14321 else
14322 decl = build_nt (SCOPE_REF, parser->scope, name);
a723baf1
MM
14323 }
14324 else
14325 {
4514aa8c 14326 tree pushed_scope = NULL_TREE;
91b004e5 14327
a723baf1
MM
14328 /* If PARSER->SCOPE is a dependent type, then it must be a
14329 class type, and we must not be checking dependencies;
14330 otherwise, we would have processed this lookup above. So
14331 that PARSER->SCOPE is not considered a dependent base by
14332 lookup_member, we must enter the scope here. */
1fb3244a 14333 if (dependent_p)
4514aa8c 14334 pushed_scope = push_scope (parser->scope);
a723baf1
MM
14335 /* If the PARSER->SCOPE is a a template specialization, it
14336 may be instantiated during name lookup. In that case,
14337 errors may be issued. Even if we rollback the current
14338 tentative parse, those errors are valid. */
fc6a28d7
MM
14339 decl = lookup_qualified_name (parser->scope, name,
14340 tag_type != none_type,
5e08432e 14341 /*complain=*/true);
4514aa8c
NS
14342 if (pushed_scope)
14343 pop_scope (pushed_scope);
a723baf1
MM
14344 }
14345 parser->qualifying_scope = parser->scope;
14346 parser->object_scope = NULL_TREE;
14347 }
14348 else if (object_type)
14349 {
14350 tree object_decl = NULL_TREE;
14351 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14352 OBJECT_TYPE is not a class. */
14353 if (CLASS_TYPE_P (object_type))
14354 /* If the OBJECT_TYPE is a template specialization, it may
14355 be instantiated during name lookup. In that case, errors
14356 may be issued. Even if we rollback the current tentative
14357 parse, those errors are valid. */
14358 object_decl = lookup_member (object_type,
14359 name,
fc6a28d7
MM
14360 /*protect=*/0,
14361 tag_type != none_type);
a723baf1 14362 /* Look it up in the enclosing context, too. */
fc6a28d7
MM
14363 decl = lookup_name_real (name, tag_type != none_type,
14364 /*nonclass=*/0,
12cf89fa 14365 /*block_p=*/true, is_namespace,
a723baf1
MM
14366 /*flags=*/0);
14367 parser->object_scope = object_type;
14368 parser->qualifying_scope = NULL_TREE;
14369 if (object_decl)
14370 decl = object_decl;
14371 }
14372 else
14373 {
fc6a28d7
MM
14374 decl = lookup_name_real (name, tag_type != none_type,
14375 /*nonclass=*/0,
12cf89fa 14376 /*block_p=*/true, is_namespace,
a723baf1
MM
14377 /*flags=*/0);
14378 parser->qualifying_scope = NULL_TREE;
14379 parser->object_scope = NULL_TREE;
14380 }
14381
14382 /* If the lookup failed, let our caller know. */
21526606 14383 if (!decl
a723baf1 14384 || decl == error_mark_node
21526606 14385 || (TREE_CODE (decl) == FUNCTION_DECL
a723baf1
MM
14386 && DECL_ANTICIPATED (decl)))
14387 return error_mark_node;
14388
14389 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14390 if (TREE_CODE (decl) == TREE_LIST)
14391 {
8f78f01f
MM
14392 if (ambiguous_p)
14393 *ambiguous_p = true;
a723baf1
MM
14394 /* The error message we have to print is too complicated for
14395 cp_parser_error, so we incorporate its actions directly. */
e5976695 14396 if (!cp_parser_simulate_error (parser))
a723baf1 14397 {
2a13a625 14398 error ("reference to %qD is ambiguous", name);
a723baf1
MM
14399 print_candidates (decl);
14400 }
14401 return error_mark_node;
14402 }
14403
50bc768d
NS
14404 gcc_assert (DECL_P (decl)
14405 || TREE_CODE (decl) == OVERLOAD
14406 || TREE_CODE (decl) == SCOPE_REF
14407 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14408 || BASELINK_P (decl));
a723baf1
MM
14409
14410 /* If we have resolved the name of a member declaration, check to
14411 see if the declaration is accessible. When the name resolves to
34cd5ae7 14412 set of overloaded functions, accessibility is checked when
21526606 14413 overload resolution is done.
a723baf1
MM
14414
14415 During an explicit instantiation, access is not checked at all,
14416 as per [temp.explicit]. */
8d241e0b 14417 if (DECL_P (decl))
ee76b931 14418 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
a723baf1
MM
14419
14420 return decl;
14421}
14422
14423/* Like cp_parser_lookup_name, but for use in the typical case where
b0bc6e8e
KL
14424 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14425 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
a723baf1
MM
14426
14427static tree
94edc4ab 14428cp_parser_lookup_name_simple (cp_parser* parser, tree name)
a723baf1 14429{
21526606 14430 return cp_parser_lookup_name (parser, name,
fc6a28d7 14431 none_type,
b0bc6e8e 14432 /*is_template=*/false,
eea9800f 14433 /*is_namespace=*/false,
8f78f01f
MM
14434 /*check_dependency=*/true,
14435 /*ambiguous_p=*/NULL);
a723baf1
MM
14436}
14437
a723baf1
MM
14438/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14439 the current context, return the TYPE_DECL. If TAG_NAME_P is
14440 true, the DECL indicates the class being defined in a class-head,
14441 or declared in an elaborated-type-specifier.
14442
14443 Otherwise, return DECL. */
14444
14445static tree
14446cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14447{
710b73e6
KL
14448 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14449 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
a723baf1 14450
21526606 14451 struct A {
a723baf1
MM
14452 template <typename T> struct B;
14453 };
14454
21526606
EC
14455 template <typename T> struct A::B {};
14456
a723baf1
MM
14457 Similarly, in a elaborated-type-specifier:
14458
14459 namespace N { struct X{}; }
14460
14461 struct A {
14462 template <typename T> friend struct N::X;
14463 };
14464
710b73e6
KL
14465 However, if the DECL refers to a class type, and we are in
14466 the scope of the class, then the name lookup automatically
14467 finds the TYPE_DECL created by build_self_reference rather
14468 than a TEMPLATE_DECL. For example, in:
14469
14470 template <class T> struct S {
14471 S s;
14472 };
14473
14474 there is no need to handle such case. */
14475
14476 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
a723baf1
MM
14477 return DECL_TEMPLATE_RESULT (decl);
14478
14479 return decl;
14480}
14481
14482/* If too many, or too few, template-parameter lists apply to the
14483 declarator, issue an error message. Returns TRUE if all went well,
14484 and FALSE otherwise. */
14485
14486static bool
21526606 14487cp_parser_check_declarator_template_parameters (cp_parser* parser,
058b15c1 14488 cp_declarator *declarator)
a723baf1
MM
14489{
14490 unsigned num_templates;
14491
14492 /* We haven't seen any classes that involve template parameters yet. */
14493 num_templates = 0;
14494
058b15c1 14495 switch (declarator->kind)
a723baf1 14496 {
058b15c1 14497 case cdk_id:
1d786913 14498 if (declarator->u.id.qualifying_scope)
058b15c1
MM
14499 {
14500 tree scope;
14501 tree member;
a723baf1 14502
1d786913
MM
14503 scope = declarator->u.id.qualifying_scope;
14504 member = declarator->u.id.unqualified_name;
a723baf1 14505
058b15c1
MM
14506 while (scope && CLASS_TYPE_P (scope))
14507 {
14508 /* You're supposed to have one `template <...>'
14509 for every template class, but you don't need one
14510 for a full specialization. For example:
14511
14512 template <class T> struct S{};
14513 template <> struct S<int> { void f(); };
14514 void S<int>::f () {}
14515
14516 is correct; there shouldn't be a `template <>' for
14517 the definition of `S<int>::f'. */
14518 if (CLASSTYPE_TEMPLATE_INFO (scope)
14519 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14520 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14521 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14522 ++num_templates;
14523
14524 scope = TYPE_CONTEXT (scope);
14525 }
14526 }
1d786913
MM
14527 else if (TREE_CODE (declarator->u.id.unqualified_name)
14528 == TEMPLATE_ID_EXPR)
14529 /* If the DECLARATOR has the form `X<y>' then it uses one
14530 additional level of template parameters. */
a723baf1
MM
14531 ++num_templates;
14532
21526606 14533 return cp_parser_check_template_parameters (parser,
a723baf1 14534 num_templates);
058b15c1
MM
14535
14536 case cdk_function:
14537 case cdk_array:
14538 case cdk_pointer:
14539 case cdk_reference:
14540 case cdk_ptrmem:
98ca843c 14541 return (cp_parser_check_declarator_template_parameters
058b15c1
MM
14542 (parser, declarator->declarator));
14543
14544 case cdk_error:
14545 return true;
14546
14547 default:
315fb5db 14548 gcc_unreachable ();
a723baf1 14549 }
315fb5db 14550 return false;
a723baf1
MM
14551}
14552
14553/* NUM_TEMPLATES were used in the current declaration. If that is
14554 invalid, return FALSE and issue an error messages. Otherwise,
14555 return TRUE. */
14556
14557static bool
94edc4ab
NN
14558cp_parser_check_template_parameters (cp_parser* parser,
14559 unsigned num_templates)
a723baf1
MM
14560{
14561 /* If there are more template classes than parameter lists, we have
14562 something like:
21526606 14563
a723baf1
MM
14564 template <class T> void S<T>::R<T>::f (); */
14565 if (parser->num_template_parameter_lists < num_templates)
14566 {
14567 error ("too few template-parameter-lists");
14568 return false;
14569 }
14570 /* If there are the same number of template classes and parameter
14571 lists, that's OK. */
14572 if (parser->num_template_parameter_lists == num_templates)
14573 return true;
14574 /* If there are more, but only one more, then we are referring to a
14575 member template. That's OK too. */
14576 if (parser->num_template_parameter_lists == num_templates + 1)
14577 return true;
14578 /* Otherwise, there are too many template parameter lists. We have
14579 something like:
14580
14581 template <class T> template <class U> void S::f(); */
14582 error ("too many template-parameter-lists");
14583 return false;
14584}
14585
a723baf1
MM
14586/* Parse an optional `::' token indicating that the following name is
14587 from the global namespace. If so, PARSER->SCOPE is set to the
14588 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14589 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14590 Returns the new value of PARSER->SCOPE, if the `::' token is
14591 present, and NULL_TREE otherwise. */
14592
14593static tree
94edc4ab 14594cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
a723baf1
MM
14595{
14596 cp_token *token;
14597
14598 /* Peek at the next token. */
14599 token = cp_lexer_peek_token (parser->lexer);
14600 /* If we're looking at a `::' token then we're starting from the
14601 global namespace, not our current location. */
14602 if (token->type == CPP_SCOPE)
14603 {
14604 /* Consume the `::' token. */
14605 cp_lexer_consume_token (parser->lexer);
14606 /* Set the SCOPE so that we know where to start the lookup. */
14607 parser->scope = global_namespace;
14608 parser->qualifying_scope = global_namespace;
14609 parser->object_scope = NULL_TREE;
14610
14611 return parser->scope;
14612 }
14613 else if (!current_scope_valid_p)
14614 {
14615 parser->scope = NULL_TREE;
14616 parser->qualifying_scope = NULL_TREE;
14617 parser->object_scope = NULL_TREE;
14618 }
14619
14620 return NULL_TREE;
14621}
14622
14623/* Returns TRUE if the upcoming token sequence is the start of a
14624 constructor declarator. If FRIEND_P is true, the declarator is
14625 preceded by the `friend' specifier. */
14626
14627static bool
14628cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14629{
14630 bool constructor_p;
14631 tree type_decl = NULL_TREE;
14632 bool nested_name_p;
2050a1bb
MM
14633 cp_token *next_token;
14634
14635 /* The common case is that this is not a constructor declarator, so
8fbc5ae7
MM
14636 try to avoid doing lots of work if at all possible. It's not
14637 valid declare a constructor at function scope. */
14638 if (at_function_scope_p ())
14639 return false;
14640 /* And only certain tokens can begin a constructor declarator. */
2050a1bb
MM
14641 next_token = cp_lexer_peek_token (parser->lexer);
14642 if (next_token->type != CPP_NAME
14643 && next_token->type != CPP_SCOPE
14644 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14645 && next_token->type != CPP_TEMPLATE_ID)
14646 return false;
a723baf1
MM
14647
14648 /* Parse tentatively; we are going to roll back all of the tokens
14649 consumed here. */
14650 cp_parser_parse_tentatively (parser);
14651 /* Assume that we are looking at a constructor declarator. */
14652 constructor_p = true;
8d241e0b 14653
a723baf1
MM
14654 /* Look for the optional `::' operator. */
14655 cp_parser_global_scope_opt (parser,
14656 /*current_scope_valid_p=*/false);
14657 /* Look for the nested-name-specifier. */
21526606 14658 nested_name_p
a723baf1
MM
14659 = (cp_parser_nested_name_specifier_opt (parser,
14660 /*typename_keyword_p=*/false,
14661 /*check_dependency_p=*/false,
a668c6ad
MM
14662 /*type_p=*/false,
14663 /*is_declaration=*/false)
a723baf1
MM
14664 != NULL_TREE);
14665 /* Outside of a class-specifier, there must be a
14666 nested-name-specifier. */
21526606 14667 if (!nested_name_p &&
a723baf1
MM
14668 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14669 || friend_p))
14670 constructor_p = false;
14671 /* If we still think that this might be a constructor-declarator,
14672 look for a class-name. */
14673 if (constructor_p)
14674 {
14675 /* If we have:
14676
8fbc5ae7 14677 template <typename T> struct S { S(); };
a723baf1
MM
14678 template <typename T> S<T>::S ();
14679
14680 we must recognize that the nested `S' names a class.
14681 Similarly, for:
14682
14683 template <typename T> S<T>::S<T> ();
14684
14685 we must recognize that the nested `S' names a template. */
14686 type_decl = cp_parser_class_name (parser,
14687 /*typename_keyword_p=*/false,
14688 /*template_keyword_p=*/false,
fc6a28d7 14689 none_type,
a723baf1 14690 /*check_dependency_p=*/false,
a668c6ad
MM
14691 /*class_head_p=*/false,
14692 /*is_declaration=*/false);
a723baf1
MM
14693 /* If there was no class-name, then this is not a constructor. */
14694 constructor_p = !cp_parser_error_occurred (parser);
14695 }
8d241e0b 14696
a723baf1
MM
14697 /* If we're still considering a constructor, we have to see a `(',
14698 to begin the parameter-declaration-clause, followed by either a
14699 `)', an `...', or a decl-specifier. We need to check for a
14700 type-specifier to avoid being fooled into thinking that:
14701
14702 S::S (f) (int);
14703
14704 is a constructor. (It is actually a function named `f' that
14705 takes one parameter (of type `int') and returns a value of type
14706 `S::S'. */
21526606 14707 if (constructor_p
a723baf1
MM
14708 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14709 {
14710 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14711 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15077df5
MM
14712 /* A parameter declaration begins with a decl-specifier,
14713 which is either the "attribute" keyword, a storage class
14714 specifier, or (usually) a type-specifier. */
14715 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
a723baf1
MM
14716 && !cp_parser_storage_class_specifier_opt (parser))
14717 {
5dae1114 14718 tree type;
4514aa8c 14719 tree pushed_scope = NULL_TREE;
4047b164 14720 unsigned saved_num_template_parameter_lists;
5dae1114
MM
14721
14722 /* Names appearing in the type-specifier should be looked up
14723 in the scope of the class. */
14724 if (current_class_type)
14725 type = NULL_TREE;
a723baf1
MM
14726 else
14727 {
5dae1114
MM
14728 type = TREE_TYPE (type_decl);
14729 if (TREE_CODE (type) == TYPENAME_TYPE)
14d22dd6 14730 {
21526606 14731 type = resolve_typename_type (type,
14d22dd6
MM
14732 /*only_current_p=*/false);
14733 if (type == error_mark_node)
14734 {
14735 cp_parser_abort_tentative_parse (parser);
14736 return false;
14737 }
14738 }
4514aa8c 14739 pushed_scope = push_scope (type);
a723baf1 14740 }
4047b164
KL
14741
14742 /* Inside the constructor parameter list, surrounding
14743 template-parameter-lists do not apply. */
14744 saved_num_template_parameter_lists
14745 = parser->num_template_parameter_lists;
14746 parser->num_template_parameter_lists = 0;
14747
5dae1114
MM
14748 /* Look for the type-specifier. */
14749 cp_parser_type_specifier (parser,
14750 CP_PARSER_FLAGS_NONE,
62d1db17 14751 /*decl_specs=*/NULL,
5dae1114
MM
14752 /*is_declarator=*/true,
14753 /*declares_class_or_enum=*/NULL,
14754 /*is_cv_qualifier=*/NULL);
4047b164
KL
14755
14756 parser->num_template_parameter_lists
14757 = saved_num_template_parameter_lists;
14758
5dae1114 14759 /* Leave the scope of the class. */
4514aa8c
NS
14760 if (pushed_scope)
14761 pop_scope (pushed_scope);
5dae1114
MM
14762
14763 constructor_p = !cp_parser_error_occurred (parser);
a723baf1
MM
14764 }
14765 }
14766 else
14767 constructor_p = false;
14768 /* We did not really want to consume any tokens. */
14769 cp_parser_abort_tentative_parse (parser);
14770
14771 return constructor_p;
14772}
14773
14774/* Parse the definition of the function given by the DECL_SPECIFIERS,
cf22909c 14775 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
a723baf1
MM
14776 they must be performed once we are in the scope of the function.
14777
14778 Returns the function defined. */
14779
14780static tree
14781cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab 14782 (cp_parser* parser,
62d1db17 14783 cp_decl_specifier_seq *decl_specifiers,
94edc4ab 14784 tree attributes,
058b15c1 14785 const cp_declarator *declarator)
a723baf1
MM
14786{
14787 tree fn;
14788 bool success_p;
14789
14790 /* Begin the function-definition. */
058b15c1
MM
14791 success_p = start_function (decl_specifiers, declarator, attributes);
14792
14793 /* The things we're about to see are not directly qualified by any
14794 template headers we've seen thus far. */
14795 reset_specialization ();
a723baf1
MM
14796
14797 /* If there were names looked up in the decl-specifier-seq that we
14798 did not check, check them now. We must wait until we are in the
14799 scope of the function to perform the checks, since the function
14800 might be a friend. */
cf22909c 14801 perform_deferred_access_checks ();
a723baf1
MM
14802
14803 if (!success_p)
14804 {
058b15c1 14805 /* Skip the entire function. */
a723baf1
MM
14806 error ("invalid function declaration");
14807 cp_parser_skip_to_end_of_block_or_statement (parser);
14808 fn = error_mark_node;
14809 }
14810 else
14811 fn = cp_parser_function_definition_after_declarator (parser,
14812 /*inline_p=*/false);
14813
14814 return fn;
14815}
14816
14817/* Parse the part of a function-definition that follows the
14818 declarator. INLINE_P is TRUE iff this function is an inline
14819 function defined with a class-specifier.
14820
14821 Returns the function defined. */
14822
21526606
EC
14823static tree
14824cp_parser_function_definition_after_declarator (cp_parser* parser,
94edc4ab 14825 bool inline_p)
a723baf1
MM
14826{
14827 tree fn;
14828 bool ctor_initializer_p = false;
14829 bool saved_in_unbraced_linkage_specification_p;
14830 unsigned saved_num_template_parameter_lists;
14831
14832 /* If the next token is `return', then the code may be trying to
14833 make use of the "named return value" extension that G++ used to
14834 support. */
14835 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14836 {
14837 /* Consume the `return' keyword. */
14838 cp_lexer_consume_token (parser->lexer);
14839 /* Look for the identifier that indicates what value is to be
14840 returned. */
14841 cp_parser_identifier (parser);
14842 /* Issue an error message. */
14843 error ("named return values are no longer supported");
14844 /* Skip tokens until we reach the start of the function body. */
21eb631b
MM
14845 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14846 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
a723baf1
MM
14847 cp_lexer_consume_token (parser->lexer);
14848 }
14849 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14850 anything declared inside `f'. */
21526606 14851 saved_in_unbraced_linkage_specification_p
a723baf1
MM
14852 = parser->in_unbraced_linkage_specification_p;
14853 parser->in_unbraced_linkage_specification_p = false;
14854 /* Inside the function, surrounding template-parameter-lists do not
14855 apply. */
21526606
EC
14856 saved_num_template_parameter_lists
14857 = parser->num_template_parameter_lists;
a723baf1
MM
14858 parser->num_template_parameter_lists = 0;
14859 /* If the next token is `try', then we are looking at a
14860 function-try-block. */
14861 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14862 ctor_initializer_p = cp_parser_function_try_block (parser);
14863 /* A function-try-block includes the function-body, so we only do
14864 this next part if we're not processing a function-try-block. */
14865 else
21526606 14866 ctor_initializer_p
a723baf1
MM
14867 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14868
14869 /* Finish the function. */
21526606 14870 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
a723baf1
MM
14871 (inline_p ? 2 : 0));
14872 /* Generate code for it, if necessary. */
8cd2462c 14873 expand_or_defer_fn (fn);
a723baf1 14874 /* Restore the saved values. */
21526606 14875 parser->in_unbraced_linkage_specification_p
a723baf1 14876 = saved_in_unbraced_linkage_specification_p;
21526606 14877 parser->num_template_parameter_lists
a723baf1
MM
14878 = saved_num_template_parameter_lists;
14879
14880 return fn;
14881}
14882
14883/* Parse a template-declaration, assuming that the `export' (and
14884 `extern') keywords, if present, has already been scanned. MEMBER_P
14885 is as for cp_parser_template_declaration. */
14886
14887static void
94edc4ab 14888cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
a723baf1
MM
14889{
14890 tree decl = NULL_TREE;
14891 tree parameter_list;
14892 bool friend_p = false;
14893
14894 /* Look for the `template' keyword. */
14895 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14896 return;
21526606 14897
a723baf1
MM
14898 /* And the `<'. */
14899 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14900 return;
21526606 14901
a723baf1
MM
14902 /* If the next token is `>', then we have an invalid
14903 specialization. Rather than complain about an invalid template
14904 parameter, issue an error message here. */
14905 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14906 {
14907 cp_parser_error (parser, "invalid explicit specialization");
2f9afd51 14908 begin_specialization ();
a723baf1
MM
14909 parameter_list = NULL_TREE;
14910 }
14911 else
2f9afd51
KL
14912 {
14913 /* Parse the template parameters. */
14914 begin_template_parm_list ();
14915 parameter_list = cp_parser_template_parameter_list (parser);
14916 parameter_list = end_template_parm_list (parameter_list);
14917 }
14918
a723baf1
MM
14919 /* Look for the `>'. */
14920 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14921 /* We just processed one more parameter list. */
14922 ++parser->num_template_parameter_lists;
14923 /* If the next token is `template', there are more template
14924 parameters. */
21526606 14925 if (cp_lexer_next_token_is_keyword (parser->lexer,
a723baf1
MM
14926 RID_TEMPLATE))
14927 cp_parser_template_declaration_after_export (parser, member_p);
14928 else
14929 {
fe88415f
NS
14930 /* There are no access checks when parsing a template, as we do not
14931 know if a specialization will be a friend. */
14932 push_deferring_access_checks (dk_no_check);
98ca843c 14933
a723baf1
MM
14934 decl = cp_parser_single_declaration (parser,
14935 member_p,
14936 &friend_p);
14937
fe88415f 14938 pop_deferring_access_checks ();
98ca843c 14939
a723baf1
MM
14940 /* If this is a member template declaration, let the front
14941 end know. */
14942 if (member_p && !friend_p && decl)
37d407a1
KL
14943 {
14944 if (TREE_CODE (decl) == TYPE_DECL)
14945 cp_parser_check_access_in_redeclaration (decl);
14946
14947 decl = finish_member_template_decl (decl);
14948 }
a723baf1 14949 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19db77ce
KL
14950 make_friend_class (current_class_type, TREE_TYPE (decl),
14951 /*complain=*/true);
a723baf1
MM
14952 }
14953 /* We are done with the current parameter list. */
14954 --parser->num_template_parameter_lists;
14955
14956 /* Finish up. */
14957 finish_template_decl (parameter_list);
14958
14959 /* Register member declarations. */
14960 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14961 finish_member_declaration (decl);
14962
14963 /* If DECL is a function template, we must return to parse it later.
14964 (Even though there is no definition, there might be default
14965 arguments that need handling.) */
21526606 14966 if (member_p && decl
a723baf1
MM
14967 && (TREE_CODE (decl) == FUNCTION_DECL
14968 || DECL_FUNCTION_TEMPLATE_P (decl)))
14969 TREE_VALUE (parser->unparsed_functions_queues)
21526606 14970 = tree_cons (NULL_TREE, decl,
a723baf1
MM
14971 TREE_VALUE (parser->unparsed_functions_queues));
14972}
14973
14974/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14975 `function-definition' sequence. MEMBER_P is true, this declaration
14976 appears in a class scope.
14977
14978 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14979 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14980
14981static tree
21526606 14982cp_parser_single_declaration (cp_parser* parser,
94edc4ab
NN
14983 bool member_p,
14984 bool* friend_p)
a723baf1 14985{
560ad596 14986 int declares_class_or_enum;
a723baf1 14987 tree decl = NULL_TREE;
62d1db17 14988 cp_decl_specifier_seq decl_specifiers;
4bb8ca28 14989 bool function_definition_p = false;
a723baf1 14990
71bd7186
MM
14991 /* This function is only used when processing a template
14992 declaration. */
14993 gcc_assert (innermost_scope_kind () == sk_template_parms
14994 || innermost_scope_kind () == sk_template_spec);
14995
a723baf1 14996 /* Defer access checks until we know what is being declared. */
8d241e0b 14997 push_deferring_access_checks (dk_deferred);
cf22909c 14998
a723baf1
MM
14999 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15000 alternative. */
62d1db17
MM
15001 cp_parser_decl_specifier_seq (parser,
15002 CP_PARSER_FLAGS_OPTIONAL,
15003 &decl_specifiers,
15004 &declares_class_or_enum);
4bb8ca28 15005 if (friend_p)
62d1db17 15006 *friend_p = cp_parser_friend_p (&decl_specifiers);
71bd7186
MM
15007
15008 /* There are no template typedefs. */
15009 if (decl_specifiers.specs[(int) ds_typedef])
15010 {
15011 error ("template declaration of %qs", "typedef");
15012 decl = error_mark_node;
15013 }
15014
a723baf1
MM
15015 /* Gather up the access checks that occurred the
15016 decl-specifier-seq. */
cf22909c
KL
15017 stop_deferring_access_checks ();
15018
a723baf1
MM
15019 /* Check for the declaration of a template class. */
15020 if (declares_class_or_enum)
15021 {
15022 if (cp_parser_declares_only_class_p (parser))
15023 {
62d1db17 15024 decl = shadow_tag (&decl_specifiers);
b939a023
KL
15025
15026 /* In this case:
15027
15028 struct C {
15029 friend template <typename T> struct A<T>::B;
15030 };
15031
15032 A<T>::B will be represented by a TYPENAME_TYPE, and
15033 therefore not recognized by shadow_tag. */
15034 if (friend_p && *friend_p
15035 && !decl
15036 && decl_specifiers.type
15037 && TYPE_P (decl_specifiers.type))
15038 decl = decl_specifiers.type;
15039
62d1db17 15040 if (decl && decl != error_mark_node)
a723baf1
MM
15041 decl = TYPE_NAME (decl);
15042 else
15043 decl = error_mark_node;
15044 }
15045 }
a723baf1
MM
15046 /* If it's not a template class, try for a template function. If
15047 the next token is a `;', then this declaration does not declare
15048 anything. But, if there were errors in the decl-specifiers, then
15049 the error might well have come from an attempted class-specifier.
15050 In that case, there's no need to warn about a missing declarator. */
15051 if (!decl
15052 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
62d1db17 15053 || decl_specifiers.type != error_mark_node))
21526606 15054 decl = cp_parser_init_declarator (parser,
62d1db17 15055 &decl_specifiers,
4bb8ca28 15056 /*function_definition_allowed_p=*/true,
a723baf1 15057 member_p,
560ad596 15058 declares_class_or_enum,
4bb8ca28 15059 &function_definition_p);
cf22909c
KL
15060
15061 pop_deferring_access_checks ();
15062
a723baf1
MM
15063 /* Clear any current qualification; whatever comes next is the start
15064 of something new. */
15065 parser->scope = NULL_TREE;
15066 parser->qualifying_scope = NULL_TREE;
15067 parser->object_scope = NULL_TREE;
15068 /* Look for a trailing `;' after the declaration. */
4bb8ca28 15069 if (!function_definition_p
71bd7186
MM
15070 && (decl == error_mark_node
15071 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
a723baf1 15072 cp_parser_skip_to_end_of_block_or_statement (parser);
a723baf1
MM
15073
15074 return decl;
15075}
15076
d6b4ea85
MM
15077/* Parse a cast-expression that is not the operand of a unary "&". */
15078
15079static tree
15080cp_parser_simple_cast_expression (cp_parser *parser)
15081{
93678513
MM
15082 return cp_parser_cast_expression (parser, /*address_p=*/false,
15083 /*cast_p=*/false);
d6b4ea85
MM
15084}
15085
a723baf1
MM
15086/* Parse a functional cast to TYPE. Returns an expression
15087 representing the cast. */
15088
15089static tree
94edc4ab 15090cp_parser_functional_cast (cp_parser* parser, tree type)
a723baf1
MM
15091{
15092 tree expression_list;
d36d5600 15093 tree cast;
a723baf1 15094
21526606 15095 expression_list
39703eb9 15096 = cp_parser_parenthesized_expression_list (parser, false,
93678513 15097 /*cast_p=*/true,
39703eb9 15098 /*non_constant_p=*/NULL);
a723baf1 15099
d36d5600
GB
15100 cast = build_functional_cast (type, expression_list);
15101 /* [expr.const]/1: In an integral constant expression "only type
15102 conversions to integral or enumeration type can be used". */
98ca843c 15103 if (cast != error_mark_node && !type_dependent_expression_p (type)
d36d5600
GB
15104 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15105 {
98ca843c 15106 if (cp_parser_non_integral_constant_expression
d36d5600
GB
15107 (parser, "a call to a constructor"))
15108 return error_mark_node;
15109 }
15110 return cast;
a723baf1
MM
15111}
15112
4bb8ca28
MM
15113/* Save the tokens that make up the body of a member function defined
15114 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15115 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15116 specifiers applied to the declaration. Returns the FUNCTION_DECL
15117 for the member function. */
15118
7ce27103 15119static tree
4bb8ca28 15120cp_parser_save_member_function_body (cp_parser* parser,
62d1db17 15121 cp_decl_specifier_seq *decl_specifiers,
058b15c1 15122 cp_declarator *declarator,
4bb8ca28
MM
15123 tree attributes)
15124{
c162c75e
MA
15125 cp_token *first;
15126 cp_token *last;
4bb8ca28
MM
15127 tree fn;
15128
15129 /* Create the function-declaration. */
15130 fn = start_method (decl_specifiers, declarator, attributes);
15131 /* If something went badly wrong, bail out now. */
15132 if (fn == error_mark_node)
15133 {
15134 /* If there's a function-body, skip it. */
21526606 15135 if (cp_parser_token_starts_function_definition_p
4bb8ca28
MM
15136 (cp_lexer_peek_token (parser->lexer)))
15137 cp_parser_skip_to_end_of_block_or_statement (parser);
15138 return error_mark_node;
15139 }
15140
15141 /* Remember it, if there default args to post process. */
15142 cp_parser_save_default_args (parser, fn);
15143
21526606 15144 /* Save away the tokens that make up the body of the
4bb8ca28 15145 function. */
c162c75e
MA
15146 first = parser->lexer->next_token;
15147 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
4bb8ca28
MM
15148 /* Handle function try blocks. */
15149 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
c162c75e
MA
15150 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15151 last = parser->lexer->next_token;
4bb8ca28
MM
15152
15153 /* Save away the inline definition; we will process it when the
15154 class is complete. */
c162c75e 15155 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
4bb8ca28
MM
15156 DECL_PENDING_INLINE_P (fn) = 1;
15157
15158 /* We need to know that this was defined in the class, so that
15159 friend templates are handled correctly. */
15160 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15161
15162 /* We're done with the inline definition. */
15163 finish_method (fn);
15164
15165 /* Add FN to the queue of functions to be parsed later. */
15166 TREE_VALUE (parser->unparsed_functions_queues)
21526606 15167 = tree_cons (NULL_TREE, fn,
4bb8ca28
MM
15168 TREE_VALUE (parser->unparsed_functions_queues));
15169
15170 return fn;
15171}
15172
ec75414f
MM
15173/* Parse a template-argument-list, as well as the trailing ">" (but
15174 not the opening ">"). See cp_parser_template_argument_list for the
15175 return value. */
15176
15177static tree
15178cp_parser_enclosed_template_argument_list (cp_parser* parser)
15179{
15180 tree arguments;
15181 tree saved_scope;
15182 tree saved_qualifying_scope;
15183 tree saved_object_scope;
15184 bool saved_greater_than_is_operator_p;
15185
15186 /* [temp.names]
15187
15188 When parsing a template-id, the first non-nested `>' is taken as
15189 the end of the template-argument-list rather than a greater-than
15190 operator. */
21526606 15191 saved_greater_than_is_operator_p
ec75414f
MM
15192 = parser->greater_than_is_operator_p;
15193 parser->greater_than_is_operator_p = false;
15194 /* Parsing the argument list may modify SCOPE, so we save it
15195 here. */
15196 saved_scope = parser->scope;
15197 saved_qualifying_scope = parser->qualifying_scope;
15198 saved_object_scope = parser->object_scope;
15199 /* Parse the template-argument-list itself. */
15200 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15201 arguments = NULL_TREE;
15202 else
15203 arguments = cp_parser_template_argument_list (parser);
4d5297fa
GB
15204 /* Look for the `>' that ends the template-argument-list. If we find
15205 a '>>' instead, it's probably just a typo. */
15206 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15207 {
15208 if (!saved_greater_than_is_operator_p)
15209 {
2cfe82fe
ZW
15210 /* If we're in a nested template argument list, the '>>' has
15211 to be a typo for '> >'. We emit the error message, but we
15212 continue parsing and we push a '>' as next token, so that
15213 the argument list will be parsed correctly. Note that the
15214 global source location is still on the token before the
15215 '>>', so we need to say explicitly where we want it. */
15216 cp_token *token = cp_lexer_peek_token (parser->lexer);
15217 error ("%H%<>>%> should be %<> >%> "
15218 "within a nested template argument list",
15219 &token->location);
15220
15221 /* ??? Proper recovery should terminate two levels of
15222 template argument list here. */
4d5297fa
GB
15223 token->type = CPP_GREATER;
15224 }
15225 else
15226 {
2cfe82fe
ZW
15227 /* If this is not a nested template argument list, the '>>'
15228 is a typo for '>'. Emit an error message and continue.
15229 Same deal about the token location, but here we can get it
15230 right by consuming the '>>' before issuing the diagnostic. */
4d5297fa 15231 cp_lexer_consume_token (parser->lexer);
2cfe82fe
ZW
15232 error ("spurious %<>>%>, use %<>%> to terminate "
15233 "a template argument list");
4d5297fa
GB
15234 }
15235 }
2cfe82fe
ZW
15236 else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15237 error ("missing %<>%> to terminate the template argument list");
15238 else
15239 /* It's what we want, a '>'; consume it. */
15240 cp_lexer_consume_token (parser->lexer);
ec75414f 15241 /* The `>' token might be a greater-than operator again now. */
21526606 15242 parser->greater_than_is_operator_p
ec75414f
MM
15243 = saved_greater_than_is_operator_p;
15244 /* Restore the SAVED_SCOPE. */
15245 parser->scope = saved_scope;
15246 parser->qualifying_scope = saved_qualifying_scope;
15247 parser->object_scope = saved_object_scope;
15248
15249 return arguments;
15250}
15251
a723baf1
MM
15252/* MEMBER_FUNCTION is a member function, or a friend. If default
15253 arguments, or the body of the function have not yet been parsed,
15254 parse them now. */
15255
15256static void
94edc4ab 15257cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
a723baf1 15258{
a723baf1
MM
15259 /* If this member is a template, get the underlying
15260 FUNCTION_DECL. */
15261 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15262 member_function = DECL_TEMPLATE_RESULT (member_function);
15263
15264 /* There should not be any class definitions in progress at this
15265 point; the bodies of members are only parsed outside of all class
15266 definitions. */
50bc768d 15267 gcc_assert (parser->num_classes_being_defined == 0);
a723baf1
MM
15268 /* While we're parsing the member functions we might encounter more
15269 classes. We want to handle them right away, but we don't want
15270 them getting mixed up with functions that are currently in the
15271 queue. */
15272 parser->unparsed_functions_queues
15273 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15274
15275 /* Make sure that any template parameters are in scope. */
15276 maybe_begin_member_template_processing (member_function);
15277
a723baf1
MM
15278 /* If the body of the function has not yet been parsed, parse it
15279 now. */
15280 if (DECL_PENDING_INLINE_P (member_function))
15281 {
15282 tree function_scope;
15283 cp_token_cache *tokens;
15284
15285 /* The function is no longer pending; we are processing it. */
15286 tokens = DECL_PENDING_INLINE_INFO (member_function);
15287 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15288 DECL_PENDING_INLINE_P (member_function) = 0;
15289 /* If this was an inline function in a local class, enter the scope
15290 of the containing function. */
15291 function_scope = decl_function_context (member_function);
15292 if (function_scope)
15293 push_function_context_to (function_scope);
21526606 15294
2cfe82fe
ZW
15295 /* Push the body of the function onto the lexer stack. */
15296 cp_parser_push_lexer_for_tokens (parser, tokens);
21526606 15297
a723baf1
MM
15298 /* Let the front end know that we going to be defining this
15299 function. */
058b15c1
MM
15300 start_preparsed_function (member_function, NULL_TREE,
15301 SF_PRE_PARSED | SF_INCLASS_INLINE);
21526606 15302
a723baf1
MM
15303 /* Now, parse the body of the function. */
15304 cp_parser_function_definition_after_declarator (parser,
15305 /*inline_p=*/true);
21526606 15306
a723baf1
MM
15307 /* Leave the scope of the containing function. */
15308 if (function_scope)
15309 pop_function_context_from (function_scope);
2cfe82fe 15310 cp_parser_pop_lexer (parser);
a723baf1
MM
15311 }
15312
15313 /* Remove any template parameters from the symbol table. */
15314 maybe_end_member_template_processing ();
15315
15316 /* Restore the queue. */
21526606 15317 parser->unparsed_functions_queues
a723baf1
MM
15318 = TREE_CHAIN (parser->unparsed_functions_queues);
15319}
15320
cd0be382 15321/* If DECL contains any default args, remember it on the unparsed
8db1028e
NS
15322 functions queue. */
15323
15324static void
15325cp_parser_save_default_args (cp_parser* parser, tree decl)
15326{
15327 tree probe;
15328
15329 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15330 probe;
15331 probe = TREE_CHAIN (probe))
15332 if (TREE_PURPOSE (probe))
15333 {
15334 TREE_PURPOSE (parser->unparsed_functions_queues)
f44b0c8e 15335 = tree_cons (current_class_type, decl,
8db1028e
NS
15336 TREE_PURPOSE (parser->unparsed_functions_queues));
15337 break;
15338 }
15339 return;
15340}
15341
8218bd34 15342/* FN is a FUNCTION_DECL which may contains a parameter with an
f44b0c8e
MM
15343 unparsed DEFAULT_ARG. Parse the default args now. This function
15344 assumes that the current scope is the scope in which the default
15345 argument should be processed. */
a723baf1
MM
15346
15347static void
8218bd34 15348cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
a723baf1 15349{
a723baf1 15350 bool saved_local_variables_forbidden_p;
2cfe82fe 15351 tree parm;
8218bd34 15352
b92bc2a0
NS
15353 /* While we're parsing the default args, we might (due to the
15354 statement expression extension) encounter more classes. We want
15355 to handle them right away, but we don't want them getting mixed
15356 up with default args that are currently in the queue. */
15357 parser->unparsed_functions_queues
15358 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15359
2cfe82fe
ZW
15360 /* Local variable names (and the `this' keyword) may not appear
15361 in a default argument. */
15362 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15363 parser->local_variables_forbidden_p = true;
15364
15365 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15366 parm;
15367 parm = TREE_CHAIN (parm))
a723baf1 15368 {
2cfe82fe 15369 cp_token_cache *tokens;
21526606 15370
2cfe82fe
ZW
15371 if (!TREE_PURPOSE (parm)
15372 || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15373 continue;
a723baf1 15374
2cfe82fe
ZW
15375 /* Push the saved tokens for the default argument onto the parser's
15376 lexer stack. */
15377 tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15378 cp_parser_push_lexer_for_tokens (parser, tokens);
a723baf1 15379
2cfe82fe 15380 /* Parse the assignment-expression. */
93678513
MM
15381 TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser,
15382 /*cast_p=*/false);
a723baf1 15383
676e33ca
MM
15384 /* If the token stream has not been completely used up, then
15385 there was extra junk after the end of the default
15386 argument. */
15387 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2a13a625 15388 cp_parser_error (parser, "expected %<,%>");
676e33ca 15389
2cfe82fe
ZW
15390 /* Revert to the main lexer. */
15391 cp_parser_pop_lexer (parser);
a723baf1 15392 }
b92bc2a0 15393
2cfe82fe
ZW
15394 /* Restore the state of local_variables_forbidden_p. */
15395 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15396
b92bc2a0 15397 /* Restore the queue. */
21526606 15398 parser->unparsed_functions_queues
b92bc2a0 15399 = TREE_CHAIN (parser->unparsed_functions_queues);
a723baf1
MM
15400}
15401
15402/* Parse the operand of `sizeof' (or a similar operator). Returns
15403 either a TYPE or an expression, depending on the form of the
15404 input. The KEYWORD indicates which kind of expression we have
15405 encountered. */
15406
15407static tree
94edc4ab 15408cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
a723baf1
MM
15409{
15410 static const char *format;
15411 tree expr = NULL_TREE;
15412 const char *saved_message;
67c03833 15413 bool saved_integral_constant_expression_p;
93678513 15414 bool saved_non_integral_constant_expression_p;
a723baf1
MM
15415
15416 /* Initialize FORMAT the first time we get here. */
15417 if (!format)
9e637a26 15418 format = "types may not be defined in '%s' expressions";
a723baf1
MM
15419
15420 /* Types cannot be defined in a `sizeof' expression. Save away the
15421 old message. */
15422 saved_message = parser->type_definition_forbidden_message;
15423 /* And create the new one. */
21526606
EC
15424 parser->type_definition_forbidden_message
15425 = xmalloc (strlen (format)
c68b0a84
KG
15426 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15427 + 1 /* `\0' */);
a723baf1
MM
15428 sprintf ((char *) parser->type_definition_forbidden_message,
15429 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15430
15431 /* The restrictions on constant-expressions do not apply inside
15432 sizeof expressions. */
93678513
MM
15433 saved_integral_constant_expression_p
15434 = parser->integral_constant_expression_p;
15435 saved_non_integral_constant_expression_p
15436 = parser->non_integral_constant_expression_p;
67c03833 15437 parser->integral_constant_expression_p = false;
a723baf1 15438
3beb3abf
MM
15439 /* Do not actually evaluate the expression. */
15440 ++skip_evaluation;
a723baf1
MM
15441 /* If it's a `(', then we might be looking at the type-id
15442 construction. */
15443 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15444 {
15445 tree type;
4f8163b1 15446 bool saved_in_type_id_in_expr_p;
a723baf1
MM
15447
15448 /* We can't be sure yet whether we're looking at a type-id or an
15449 expression. */
15450 cp_parser_parse_tentatively (parser);
15451 /* Consume the `('. */
15452 cp_lexer_consume_token (parser->lexer);
15453 /* Parse the type-id. */
4f8163b1
MM
15454 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15455 parser->in_type_id_in_expr_p = true;
a723baf1 15456 type = cp_parser_type_id (parser);
4f8163b1 15457 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1 15458 /* Now, look for the trailing `)'. */
9e637a26 15459 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
a723baf1
MM
15460 /* If all went well, then we're done. */
15461 if (cp_parser_parse_definitely (parser))
15462 {
62d1db17
MM
15463 cp_decl_specifier_seq decl_specs;
15464
15465 /* Build a trivial decl-specifier-seq. */
15466 clear_decl_specs (&decl_specs);
15467 decl_specs.type = type;
a723baf1
MM
15468
15469 /* Call grokdeclarator to figure out what type this is. */
058b15c1 15470 expr = grokdeclarator (NULL,
62d1db17 15471 &decl_specs,
a723baf1
MM
15472 TYPENAME,
15473 /*initialized=*/0,
15474 /*attrlist=*/NULL);
15475 }
15476 }
15477
15478 /* If the type-id production did not work out, then we must be
15479 looking at the unary-expression production. */
15480 if (!expr)
93678513
MM
15481 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15482 /*cast_p=*/false);
3beb3abf
MM
15483 /* Go back to evaluating expressions. */
15484 --skip_evaluation;
a723baf1
MM
15485
15486 /* Free the message we created. */
15487 free ((char *) parser->type_definition_forbidden_message);
15488 /* And restore the old one. */
15489 parser->type_definition_forbidden_message = saved_message;
93678513
MM
15490 parser->integral_constant_expression_p
15491 = saved_integral_constant_expression_p;
15492 parser->non_integral_constant_expression_p
15493 = saved_non_integral_constant_expression_p;
a723baf1
MM
15494
15495 return expr;
15496}
15497
15498/* If the current declaration has no declarator, return true. */
15499
15500static bool
15501cp_parser_declares_only_class_p (cp_parser *parser)
15502{
21526606 15503 /* If the next token is a `;' or a `,' then there is no
a723baf1
MM
15504 declarator. */
15505 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15506 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15507}
15508
62d1db17 15509/* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
a723baf1 15510
62d1db17
MM
15511static void
15512cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15513 cp_storage_class storage_class)
a723baf1 15514{
62d1db17
MM
15515 if (decl_specs->storage_class != sc_none)
15516 decl_specs->multiple_storage_classes_p = true;
15517 else
15518 decl_specs->storage_class = storage_class;
15519}
15520
15521/* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15522 is true, the type is a user-defined type; otherwise it is a
15523 built-in type specified by a keyword. */
a723baf1 15524
62d1db17
MM
15525static void
15526cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15527 tree type_spec,
15528 bool user_defined_p)
15529{
15530 decl_specs->any_specifiers_p = true;
98ca843c 15531
9306cccb
MM
15532 /* If the user tries to redeclare bool or wchar_t (with, for
15533 example, in "typedef int wchar_t;") we remember that this is what
f84b6c96
MM
15534 happened. In system headers, we ignore these declarations so
15535 that G++ can work with system headers that are not C++-safe. */
98ca843c 15536 if (decl_specs->specs[(int) ds_typedef]
f84b6c96 15537 && !user_defined_p
9306cccb
MM
15538 && (type_spec == boolean_type_node
15539 || type_spec == wchar_type_node)
f84b6c96
MM
15540 && (decl_specs->type
15541 || decl_specs->specs[(int) ds_long]
15542 || decl_specs->specs[(int) ds_short]
15543 || decl_specs->specs[(int) ds_unsigned]
15544 || decl_specs->specs[(int) ds_signed]))
0a73e37f
MM
15545 {
15546 decl_specs->redefined_builtin_type = type_spec;
15547 if (!decl_specs->type)
15548 {
15549 decl_specs->type = type_spec;
15550 decl_specs->user_defined_type_p = false;
15551 }
15552 }
f84b6c96
MM
15553 else if (decl_specs->type)
15554 decl_specs->multiple_types_p = true;
62d1db17
MM
15555 else
15556 {
15557 decl_specs->type = type_spec;
15558 decl_specs->user_defined_type_p = user_defined_p;
0a73e37f 15559 decl_specs->redefined_builtin_type = NULL_TREE;
a723baf1 15560 }
62d1db17 15561}
a723baf1 15562
62d1db17
MM
15563/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15564 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15565
15566static bool
15567cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15568{
15569 return decl_specifiers->specs[(int) ds_friend] != 0;
a723baf1
MM
15570}
15571
15572/* If the next token is of the indicated TYPE, consume it. Otherwise,
15573 issue an error message indicating that TOKEN_DESC was expected.
21526606 15574
a723baf1
MM
15575 Returns the token consumed, if the token had the appropriate type.
15576 Otherwise, returns NULL. */
15577
15578static cp_token *
94edc4ab
NN
15579cp_parser_require (cp_parser* parser,
15580 enum cpp_ttype type,
15581 const char* token_desc)
a723baf1
MM
15582{
15583 if (cp_lexer_next_token_is (parser->lexer, type))
15584 return cp_lexer_consume_token (parser->lexer);
15585 else
15586 {
e5976695
MM
15587 /* Output the MESSAGE -- unless we're parsing tentatively. */
15588 if (!cp_parser_simulate_error (parser))
216bb6e1
MM
15589 {
15590 char *message = concat ("expected ", token_desc, NULL);
15591 cp_parser_error (parser, message);
15592 free (message);
15593 }
a723baf1
MM
15594 return NULL;
15595 }
15596}
15597
15598/* Like cp_parser_require, except that tokens will be skipped until
15599 the desired token is found. An error message is still produced if
15600 the next token is not as expected. */
15601
15602static void
21526606
EC
15603cp_parser_skip_until_found (cp_parser* parser,
15604 enum cpp_ttype type,
94edc4ab 15605 const char* token_desc)
a723baf1
MM
15606{
15607 cp_token *token;
15608 unsigned nesting_depth = 0;
15609
15610 if (cp_parser_require (parser, type, token_desc))
15611 return;
15612
15613 /* Skip tokens until the desired token is found. */
15614 while (true)
15615 {
15616 /* Peek at the next token. */
15617 token = cp_lexer_peek_token (parser->lexer);
21526606 15618 /* If we've reached the token we want, consume it and
a723baf1
MM
15619 stop. */
15620 if (token->type == type && !nesting_depth)
15621 {
15622 cp_lexer_consume_token (parser->lexer);
15623 return;
15624 }
15625 /* If we've run out of tokens, stop. */
15626 if (token->type == CPP_EOF)
15627 return;
21526606 15628 if (token->type == CPP_OPEN_BRACE
a723baf1
MM
15629 || token->type == CPP_OPEN_PAREN
15630 || token->type == CPP_OPEN_SQUARE)
15631 ++nesting_depth;
21526606 15632 else if (token->type == CPP_CLOSE_BRACE
a723baf1
MM
15633 || token->type == CPP_CLOSE_PAREN
15634 || token->type == CPP_CLOSE_SQUARE)
15635 {
15636 if (nesting_depth-- == 0)
15637 return;
15638 }
15639 /* Consume this token. */
15640 cp_lexer_consume_token (parser->lexer);
15641 }
15642}
15643
15644/* If the next token is the indicated keyword, consume it. Otherwise,
15645 issue an error message indicating that TOKEN_DESC was expected.
21526606 15646
a723baf1
MM
15647 Returns the token consumed, if the token had the appropriate type.
15648 Otherwise, returns NULL. */
15649
15650static cp_token *
94edc4ab
NN
15651cp_parser_require_keyword (cp_parser* parser,
15652 enum rid keyword,
15653 const char* token_desc)
a723baf1
MM
15654{
15655 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15656
15657 if (token && token->keyword != keyword)
15658 {
15659 dyn_string_t error_msg;
15660
15661 /* Format the error message. */
15662 error_msg = dyn_string_new (0);
15663 dyn_string_append_cstr (error_msg, "expected ");
15664 dyn_string_append_cstr (error_msg, token_desc);
15665 cp_parser_error (parser, error_msg->s);
15666 dyn_string_delete (error_msg);
15667 return NULL;
15668 }
15669
15670 return token;
15671}
15672
15673/* Returns TRUE iff TOKEN is a token that can begin the body of a
15674 function-definition. */
15675
21526606 15676static bool
94edc4ab 15677cp_parser_token_starts_function_definition_p (cp_token* token)
a723baf1
MM
15678{
15679 return (/* An ordinary function-body begins with an `{'. */
15680 token->type == CPP_OPEN_BRACE
15681 /* A ctor-initializer begins with a `:'. */
15682 || token->type == CPP_COLON
15683 /* A function-try-block begins with `try'. */
15684 || token->keyword == RID_TRY
15685 /* The named return value extension begins with `return'. */
15686 || token->keyword == RID_RETURN);
15687}
15688
15689/* Returns TRUE iff the next token is the ":" or "{" beginning a class
15690 definition. */
15691
15692static bool
15693cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15694{
15695 cp_token *token;
15696
15697 token = cp_lexer_peek_token (parser->lexer);
15698 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15699}
15700
d17811fd 15701/* Returns TRUE iff the next token is the "," or ">" ending a
03fd3f84 15702 template-argument. */
d17811fd
MM
15703
15704static bool
15705cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15706{
15707 cp_token *token;
15708
15709 token = cp_lexer_peek_token (parser->lexer);
391c4bc5 15710 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
d17811fd 15711}
f4abade9
GB
15712
15713/* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15714 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15715
15716static bool
21526606 15717cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
f4abade9
GB
15718 size_t n)
15719{
15720 cp_token *token;
15721
15722 token = cp_lexer_peek_nth_token (parser->lexer, n);
15723 if (token->type == CPP_LESS)
15724 return true;
15725 /* Check for the sequence `<::' in the original code. It would be lexed as
15726 `[:', where `[' is a digraph, and there is no whitespace before
15727 `:'. */
15728 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15729 {
15730 cp_token *token2;
15731 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15732 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15733 return true;
15734 }
15735 return false;
15736}
21526606 15737
a723baf1
MM
15738/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15739 or none_type otherwise. */
15740
15741static enum tag_types
94edc4ab 15742cp_parser_token_is_class_key (cp_token* token)
a723baf1
MM
15743{
15744 switch (token->keyword)
15745 {
15746 case RID_CLASS:
15747 return class_type;
15748 case RID_STRUCT:
15749 return record_type;
15750 case RID_UNION:
15751 return union_type;
21526606 15752
a723baf1
MM
15753 default:
15754 return none_type;
15755 }
15756}
15757
15758/* Issue an error message if the CLASS_KEY does not match the TYPE. */
15759
15760static void
15761cp_parser_check_class_key (enum tag_types class_key, tree type)
15762{
15763 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
2a13a625 15764 pedwarn ("%qs tag used in naming %q#T",
a723baf1 15765 class_key == union_type ? "union"
21526606 15766 : class_key == record_type ? "struct" : "class",
a723baf1
MM
15767 type);
15768}
21526606 15769
cd0be382 15770/* Issue an error message if DECL is redeclared with different
37d407a1
KL
15771 access than its original declaration [class.access.spec/3].
15772 This applies to nested classes and nested class templates.
15773 [class.mem/1]. */
15774
2a13a625
GDR
15775static void
15776cp_parser_check_access_in_redeclaration (tree decl)
37d407a1
KL
15777{
15778 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15779 return;
15780
15781 if ((TREE_PRIVATE (decl)
15782 != (current_access_specifier == access_private_node))
15783 || (TREE_PROTECTED (decl)
15784 != (current_access_specifier == access_protected_node)))
2a13a625 15785 error ("%qD redeclared with different access", decl);
37d407a1
KL
15786}
15787
a723baf1 15788/* Look for the `template' keyword, as a syntactic disambiguator.
21526606 15789 Return TRUE iff it is present, in which case it will be
a723baf1
MM
15790 consumed. */
15791
15792static bool
15793cp_parser_optional_template_keyword (cp_parser *parser)
15794{
15795 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15796 {
15797 /* The `template' keyword can only be used within templates;
15798 outside templates the parser can always figure out what is a
15799 template and what is not. */
15800 if (!processing_template_decl)
15801 {
2a13a625 15802 error ("%<template%> (as a disambiguator) is only allowed "
a723baf1
MM
15803 "within templates");
15804 /* If this part of the token stream is rescanned, the same
15805 error message would be generated. So, we purge the token
15806 from the stream. */
15807 cp_lexer_purge_token (parser->lexer);
15808 return false;
15809 }
15810 else
15811 {
15812 /* Consume the `template' keyword. */
15813 cp_lexer_consume_token (parser->lexer);
15814 return true;
15815 }
15816 }
15817
15818 return false;
15819}
15820
2050a1bb
MM
15821/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15822 set PARSER->SCOPE, and perform other related actions. */
15823
15824static void
15825cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15826{
15827 tree value;
15828 tree check;
15829
15830 /* Get the stored value. */
15831 value = cp_lexer_consume_token (parser->lexer)->value;
15832 /* Perform any access checks that were deferred. */
15833 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c 15834 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
2050a1bb
MM
15835 /* Set the scope from the stored value. */
15836 parser->scope = TREE_VALUE (value);
15837 parser->qualifying_scope = TREE_TYPE (value);
15838 parser->object_scope = NULL_TREE;
15839}
15840
03fd3f84 15841/* Consume tokens up through a non-nested END token. */
a723baf1
MM
15842
15843static void
c162c75e
MA
15844cp_parser_cache_group (cp_parser *parser,
15845 enum cpp_ttype end,
15846 unsigned depth)
a723baf1
MM
15847{
15848 while (true)
15849 {
15850 cp_token *token;
15851
15852 /* Abort a parenthesized expression if we encounter a brace. */
15853 if ((end == CPP_CLOSE_PAREN || depth == 0)
15854 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15855 return;
a723baf1 15856 /* If we've reached the end of the file, stop. */
4bfb8bba 15857 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
a723baf1 15858 return;
4bfb8bba
MM
15859 /* Consume the next token. */
15860 token = cp_lexer_consume_token (parser->lexer);
a723baf1
MM
15861 /* See if it starts a new group. */
15862 if (token->type == CPP_OPEN_BRACE)
15863 {
c162c75e 15864 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
a723baf1
MM
15865 if (depth == 0)
15866 return;
15867 }
15868 else if (token->type == CPP_OPEN_PAREN)
c162c75e 15869 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
a723baf1
MM
15870 else if (token->type == end)
15871 return;
15872 }
15873}
15874
15875/* Begin parsing tentatively. We always save tokens while parsing
15876 tentatively so that if the tentative parsing fails we can restore the
15877 tokens. */
15878
15879static void
94edc4ab 15880cp_parser_parse_tentatively (cp_parser* parser)
a723baf1
MM
15881{
15882 /* Enter a new parsing context. */
15883 parser->context = cp_parser_context_new (parser->context);
15884 /* Begin saving tokens. */
15885 cp_lexer_save_tokens (parser->lexer);
15886 /* In order to avoid repetitive access control error messages,
15887 access checks are queued up until we are no longer parsing
15888 tentatively. */
8d241e0b 15889 push_deferring_access_checks (dk_deferred);
a723baf1
MM
15890}
15891
15892/* Commit to the currently active tentative parse. */
15893
15894static void
94edc4ab 15895cp_parser_commit_to_tentative_parse (cp_parser* parser)
a723baf1
MM
15896{
15897 cp_parser_context *context;
15898 cp_lexer *lexer;
15899
15900 /* Mark all of the levels as committed. */
15901 lexer = parser->lexer;
15902 for (context = parser->context; context->next; context = context->next)
15903 {
15904 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15905 break;
15906 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15907 while (!cp_lexer_saving_tokens (lexer))
15908 lexer = lexer->next;
15909 cp_lexer_commit_tokens (lexer);
15910 }
15911}
15912
15913/* Abort the currently active tentative parse. All consumed tokens
15914 will be rolled back, and no diagnostics will be issued. */
15915
15916static void
94edc4ab 15917cp_parser_abort_tentative_parse (cp_parser* parser)
a723baf1
MM
15918{
15919 cp_parser_simulate_error (parser);
15920 /* Now, pretend that we want to see if the construct was
15921 successfully parsed. */
15922 cp_parser_parse_definitely (parser);
15923}
15924
34cd5ae7 15925/* Stop parsing tentatively. If a parse error has occurred, restore the
a723baf1
MM
15926 token stream. Otherwise, commit to the tokens we have consumed.
15927 Returns true if no error occurred; false otherwise. */
15928
15929static bool
94edc4ab 15930cp_parser_parse_definitely (cp_parser* parser)
a723baf1
MM
15931{
15932 bool error_occurred;
15933 cp_parser_context *context;
15934
34cd5ae7 15935 /* Remember whether or not an error occurred, since we are about to
a723baf1
MM
15936 destroy that information. */
15937 error_occurred = cp_parser_error_occurred (parser);
15938 /* Remove the topmost context from the stack. */
15939 context = parser->context;
15940 parser->context = context->next;
15941 /* If no parse errors occurred, commit to the tentative parse. */
15942 if (!error_occurred)
15943 {
15944 /* Commit to the tokens read tentatively, unless that was
15945 already done. */
15946 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15947 cp_lexer_commit_tokens (parser->lexer);
cf22909c
KL
15948
15949 pop_to_parent_deferring_access_checks ();
a723baf1
MM
15950 }
15951 /* Otherwise, if errors occurred, roll back our state so that things
15952 are just as they were before we began the tentative parse. */
15953 else
cf22909c
KL
15954 {
15955 cp_lexer_rollback_tokens (parser->lexer);
15956 pop_deferring_access_checks ();
15957 }
e5976695
MM
15958 /* Add the context to the front of the free list. */
15959 context->next = cp_parser_context_free_list;
15960 cp_parser_context_free_list = context;
15961
15962 return !error_occurred;
a723baf1
MM
15963}
15964
0b16f8f4
VR
15965/* Returns true if we are parsing tentatively and are not committed to
15966 this tentative parse. */
a723baf1
MM
15967
15968static bool
0b16f8f4 15969cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
a723baf1
MM
15970{
15971 return (cp_parser_parsing_tentatively (parser)
0b16f8f4 15972 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
a723baf1
MM
15973}
15974
4de8668e 15975/* Returns nonzero iff an error has occurred during the most recent
a723baf1 15976 tentative parse. */
21526606 15977
a723baf1 15978static bool
94edc4ab 15979cp_parser_error_occurred (cp_parser* parser)
a723baf1
MM
15980{
15981 return (cp_parser_parsing_tentatively (parser)
15982 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15983}
15984
4de8668e 15985/* Returns nonzero if GNU extensions are allowed. */
a723baf1
MM
15986
15987static bool
94edc4ab 15988cp_parser_allow_gnu_extensions_p (cp_parser* parser)
a723baf1
MM
15989{
15990 return parser->allow_gnu_extensions_p;
15991}
15992
15993\f
a723baf1
MM
15994/* The parser. */
15995
15996static GTY (()) cp_parser *the_parser;
15997
15998/* External interface. */
15999
d1bd0ded 16000/* Parse one entire translation unit. */
a723baf1 16001
d1bd0ded
GK
16002void
16003c_parse_file (void)
a723baf1
MM
16004{
16005 bool error_occurred;
f75fbaf7
ZW
16006 static bool already_called = false;
16007
16008 if (already_called)
16009 {
16010 sorry ("inter-module optimizations not implemented for C++");
16011 return;
16012 }
16013 already_called = true;
a723baf1
MM
16014
16015 the_parser = cp_parser_new ();
78757caa
KL
16016 push_deferring_access_checks (flag_access_control
16017 ? dk_no_deferred : dk_no_check);
a723baf1
MM
16018 error_occurred = cp_parser_translation_unit (the_parser);
16019 the_parser = NULL;
a723baf1
MM
16020}
16021
a723baf1
MM
16022/* This variable must be provided by every front end. */
16023
16024int yydebug;
16025
16026#include "gt-cp-parser.h"