]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
simplify-rtx.c (simplify_relational_operation_1): Avoid creating zero extensions...
[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
MM
708{
709 ++lexer->debugging_p;
710}
21526606 711
a723baf1
MM
712/* Stop emitting debugging information. */
713
714static void
94edc4ab 715cp_lexer_stop_debugging (cp_lexer* lexer)
a723baf1
MM
716{
717 --lexer->debugging_p;
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;
3176
3177 /* Consume the `~' token. */
3178 cp_lexer_consume_token (parser->lexer);
3179 /* Parse the class-name. The standard, as written, seems to
3180 say that:
3181
3182 template <typename T> struct S { ~S (); };
3183 template <typename T> S<T>::~S() {}
3184
3185 is invalid, since `~' must be followed by a class-name, but
3186 `S<T>' is dependent, and so not known to be a class.
3187 That's not right; we need to look in uninstantiated
3188 templates. A further complication arises from:
3189
3190 template <typename T> void f(T t) {
3191 t.T::~T();
21526606 3192 }
a723baf1
MM
3193
3194 Here, it is not possible to look up `T' in the scope of `T'
3195 itself. We must look in both the current scope, and the
21526606 3196 scope of the containing complete expression.
a723baf1
MM
3197
3198 Yet another issue is:
3199
3200 struct S {
3201 int S;
3202 ~S();
3203 };
3204
3205 S::~S() {}
3206
3207 The standard does not seem to say that the `S' in `~S'
3208 should refer to the type `S' and not the data member
3209 `S::S'. */
3210
3211 /* DR 244 says that we look up the name after the "~" in the
3212 same scope as we looked up the qualifying name. That idea
3213 isn't fully worked out; it's more complicated than that. */
3214 scope = parser->scope;
3215 object_scope = parser->object_scope;
3216 qualifying_scope = parser->qualifying_scope;
3217
3218 /* If the name is of the form "X::~X" it's OK. */
3219 if (scope && TYPE_P (scope)
3220 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 3221 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1 3222 == CPP_OPEN_PAREN)
21526606 3223 && (cp_lexer_peek_token (parser->lexer)->value
a723baf1
MM
3224 == TYPE_IDENTIFIER (scope)))
3225 {
3226 cp_lexer_consume_token (parser->lexer);
3227 return build_nt (BIT_NOT_EXPR, scope);
3228 }
3229
3230 /* If there was an explicit qualification (S::~T), first look
3231 in the scope given by the qualification (i.e., S). */
3232 if (scope)
3233 {
3234 cp_parser_parse_tentatively (parser);
21526606 3235 type_decl = cp_parser_class_name (parser,
a723baf1
MM
3236 /*typename_keyword_p=*/false,
3237 /*template_keyword_p=*/false,
fc6a28d7 3238 none_type,
a723baf1 3239 /*check_dependency=*/false,
a668c6ad
MM
3240 /*class_head_p=*/false,
3241 declarator_p);
a723baf1
MM
3242 if (cp_parser_parse_definitely (parser))
3243 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3244 }
3245 /* In "N::S::~S", look in "N" as well. */
3246 if (scope && qualifying_scope)
3247 {
3248 cp_parser_parse_tentatively (parser);
3249 parser->scope = qualifying_scope;
3250 parser->object_scope = NULL_TREE;
3251 parser->qualifying_scope = NULL_TREE;
21526606
EC
3252 type_decl
3253 = cp_parser_class_name (parser,
a723baf1
MM
3254 /*typename_keyword_p=*/false,
3255 /*template_keyword_p=*/false,
fc6a28d7 3256 none_type,
a723baf1 3257 /*check_dependency=*/false,
a668c6ad
MM
3258 /*class_head_p=*/false,
3259 declarator_p);
a723baf1
MM
3260 if (cp_parser_parse_definitely (parser))
3261 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3262 }
3263 /* In "p->S::~T", look in the scope given by "*p" as well. */
3264 else if (object_scope)
3265 {
3266 cp_parser_parse_tentatively (parser);
3267 parser->scope = object_scope;
3268 parser->object_scope = NULL_TREE;
3269 parser->qualifying_scope = NULL_TREE;
21526606
EC
3270 type_decl
3271 = cp_parser_class_name (parser,
a723baf1
MM
3272 /*typename_keyword_p=*/false,
3273 /*template_keyword_p=*/false,
fc6a28d7 3274 none_type,
a723baf1 3275 /*check_dependency=*/false,
a668c6ad
MM
3276 /*class_head_p=*/false,
3277 declarator_p);
a723baf1
MM
3278 if (cp_parser_parse_definitely (parser))
3279 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3280 }
3281 /* Look in the surrounding context. */
3282 parser->scope = NULL_TREE;
3283 parser->object_scope = NULL_TREE;
3284 parser->qualifying_scope = NULL_TREE;
21526606
EC
3285 type_decl
3286 = cp_parser_class_name (parser,
a723baf1
MM
3287 /*typename_keyword_p=*/false,
3288 /*template_keyword_p=*/false,
fc6a28d7 3289 none_type,
a723baf1 3290 /*check_dependency=*/false,
a668c6ad
MM
3291 /*class_head_p=*/false,
3292 declarator_p);
a723baf1
MM
3293 /* If an error occurred, assume that the name of the
3294 destructor is the same as the name of the qualifying
3295 class. That allows us to keep parsing after running
3296 into ill-formed destructor names. */
3297 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3298 return build_nt (BIT_NOT_EXPR, scope);
3299 else if (type_decl == error_mark_node)
3300 return error_mark_node;
3301
f3c2dfc6
MM
3302 /* [class.dtor]
3303
3304 A typedef-name that names a class shall not be used as the
3305 identifier in the declarator for a destructor declaration. */
21526606 3306 if (declarator_p
f3c2dfc6 3307 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
33a69702
VR
3308 && !DECL_SELF_REFERENCE_P (type_decl)
3309 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
2a13a625 3310 error ("typedef-name %qD used as destructor declarator",
f3c2dfc6
MM
3311 type_decl);
3312
a723baf1
MM
3313 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3314 }
3315
3316 case CPP_KEYWORD:
3317 if (token->keyword == RID_OPERATOR)
3318 {
3319 tree id;
3320
3321 /* This could be a template-id, so we try that first. */
3322 cp_parser_parse_tentatively (parser);
3323 /* Try a template-id. */
3324 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3325 /*check_dependency_p=*/true,
3326 declarator_p);
a723baf1
MM
3327 /* If that worked, we're done. */
3328 if (cp_parser_parse_definitely (parser))
3329 return id;
3330 /* We still don't know whether we're looking at an
3331 operator-function-id or a conversion-function-id. */
3332 cp_parser_parse_tentatively (parser);
3333 /* Try an operator-function-id. */
3334 id = cp_parser_operator_function_id (parser);
3335 /* If that didn't work, try a conversion-function-id. */
3336 if (!cp_parser_parse_definitely (parser))
3337 id = cp_parser_conversion_function_id (parser);
3338
3339 return id;
3340 }
3341 /* Fall through. */
3342
3343 default:
3344 cp_parser_error (parser, "expected unqualified-id");
3345 return error_mark_node;
3346 }
3347}
3348
3349/* Parse an (optional) nested-name-specifier.
3350
3351 nested-name-specifier:
3352 class-or-namespace-name :: nested-name-specifier [opt]
3353 class-or-namespace-name :: template nested-name-specifier [opt]
3354
3355 PARSER->SCOPE should be set appropriately before this function is
3356 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3357 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3358 in name lookups.
3359
3360 Sets PARSER->SCOPE to the class (TYPE) or namespace
3361 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3362 it unchanged if there is no nested-name-specifier. Returns the new
21526606 3363 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
a668c6ad
MM
3364
3365 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3366 part of a declaration and/or decl-specifier. */
a723baf1
MM
3367
3368static tree
21526606
EC
3369cp_parser_nested_name_specifier_opt (cp_parser *parser,
3370 bool typename_keyword_p,
a723baf1 3371 bool check_dependency_p,
a668c6ad
MM
3372 bool type_p,
3373 bool is_declaration)
a723baf1
MM
3374{
3375 bool success = false;
3376 tree access_check = NULL_TREE;
0c5e4866
NS
3377 cp_token_position start = 0;
3378 cp_token *token;
a723baf1
MM
3379
3380 /* If the next token corresponds to a nested name specifier, there
2050a1bb 3381 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
21526606 3382 false, it may have been true before, in which case something
2050a1bb
MM
3383 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3384 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3385 CHECK_DEPENDENCY_P is false, we have to fall through into the
3386 main loop. */
3387 if (check_dependency_p
3388 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3389 {
3390 cp_parser_pre_parsed_nested_name_specifier (parser);
a723baf1
MM
3391 return parser->scope;
3392 }
3393
3394 /* Remember where the nested-name-specifier starts. */
0b16f8f4 3395 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
0c5e4866 3396 start = cp_lexer_token_position (parser->lexer, false);
a723baf1 3397
8d241e0b 3398 push_deferring_access_checks (dk_deferred);
cf22909c 3399
a723baf1
MM
3400 while (true)
3401 {
3402 tree new_scope;
3403 tree old_scope;
3404 tree saved_qualifying_scope;
a723baf1
MM
3405 bool template_keyword_p;
3406
2050a1bb
MM
3407 /* Spot cases that cannot be the beginning of a
3408 nested-name-specifier. */
3409 token = cp_lexer_peek_token (parser->lexer);
3410
3411 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3412 the already parsed nested-name-specifier. */
3413 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3414 {
3415 /* Grab the nested-name-specifier and continue the loop. */
3416 cp_parser_pre_parsed_nested_name_specifier (parser);
3417 success = true;
3418 continue;
3419 }
3420
a723baf1
MM
3421 /* Spot cases that cannot be the beginning of a
3422 nested-name-specifier. On the second and subsequent times
3423 through the loop, we look for the `template' keyword. */
f7b5ecd9 3424 if (success && token->keyword == RID_TEMPLATE)
a723baf1
MM
3425 ;
3426 /* A template-id can start a nested-name-specifier. */
f7b5ecd9 3427 else if (token->type == CPP_TEMPLATE_ID)
a723baf1
MM
3428 ;
3429 else
3430 {
3431 /* If the next token is not an identifier, then it is
3432 definitely not a class-or-namespace-name. */
f7b5ecd9 3433 if (token->type != CPP_NAME)
a723baf1
MM
3434 break;
3435 /* If the following token is neither a `<' (to begin a
3436 template-id), nor a `::', then we are not looking at a
3437 nested-name-specifier. */
3438 token = cp_lexer_peek_nth_token (parser->lexer, 2);
f4abade9
GB
3439 if (token->type != CPP_SCOPE
3440 && !cp_parser_nth_token_starts_template_argument_list_p
3441 (parser, 2))
a723baf1
MM
3442 break;
3443 }
3444
3445 /* The nested-name-specifier is optional, so we parse
3446 tentatively. */
3447 cp_parser_parse_tentatively (parser);
3448
3449 /* Look for the optional `template' keyword, if this isn't the
3450 first time through the loop. */
3451 if (success)
3452 template_keyword_p = cp_parser_optional_template_keyword (parser);
3453 else
3454 template_keyword_p = false;
3455
3456 /* Save the old scope since the name lookup we are about to do
3457 might destroy it. */
3458 old_scope = parser->scope;
3459 saved_qualifying_scope = parser->qualifying_scope;
a52eb3bc
MM
3460 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3461 look up names in "X<T>::I" in order to determine that "Y" is
3462 a template. So, if we have a typename at this point, we make
3463 an effort to look through it. */
67bcc252
MM
3464 if (is_declaration
3465 && !typename_keyword_p
3466 && parser->scope
a52eb3bc
MM
3467 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3468 parser->scope = resolve_typename_type (parser->scope,
3469 /*only_current_p=*/false);
a723baf1 3470 /* Parse the qualifying entity. */
21526606 3471 new_scope
a723baf1
MM
3472 = cp_parser_class_or_namespace_name (parser,
3473 typename_keyword_p,
3474 template_keyword_p,
3475 check_dependency_p,
a668c6ad
MM
3476 type_p,
3477 is_declaration);
a723baf1
MM
3478 /* Look for the `::' token. */
3479 cp_parser_require (parser, CPP_SCOPE, "`::'");
3480
3481 /* If we found what we wanted, we keep going; otherwise, we're
3482 done. */
3483 if (!cp_parser_parse_definitely (parser))
3484 {
3485 bool error_p = false;
3486
3487 /* Restore the OLD_SCOPE since it was valid before the
3488 failed attempt at finding the last
3489 class-or-namespace-name. */
3490 parser->scope = old_scope;
3491 parser->qualifying_scope = saved_qualifying_scope;
3492 /* If the next token is an identifier, and the one after
3493 that is a `::', then any valid interpretation would have
3494 found a class-or-namespace-name. */
3495 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 3496 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1 3497 == CPP_SCOPE)
21526606 3498 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
a723baf1
MM
3499 != CPP_COMPL))
3500 {
3501 token = cp_lexer_consume_token (parser->lexer);
21526606 3502 if (!error_p)
a723baf1
MM
3503 {
3504 tree decl;
3505
3506 decl = cp_parser_lookup_name_simple (parser, token->value);
3507 if (TREE_CODE (decl) == TEMPLATE_DECL)
2a13a625 3508 error ("%qD used without template parameters", decl);
a723baf1 3509 else
21526606
EC
3510 cp_parser_name_lookup_error
3511 (parser, token->value, decl,
4bb8ca28 3512 "is not a class or namespace");
a723baf1
MM
3513 parser->scope = NULL_TREE;
3514 error_p = true;
eea9800f
MM
3515 /* Treat this as a successful nested-name-specifier
3516 due to:
3517
3518 [basic.lookup.qual]
3519
3520 If the name found is not a class-name (clause
3521 _class_) or namespace-name (_namespace.def_), the
3522 program is ill-formed. */
3523 success = true;
a723baf1
MM
3524 }
3525 cp_lexer_consume_token (parser->lexer);
3526 }
3527 break;
3528 }
3529
3530 /* We've found one valid nested-name-specifier. */
3531 success = true;
3532 /* Make sure we look in the right scope the next time through
3533 the loop. */
21526606 3534 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
a723baf1
MM
3535 ? TREE_TYPE (new_scope)
3536 : new_scope);
3537 /* If it is a class scope, try to complete it; we are about to
3538 be looking up names inside the class. */
8fbc5ae7
MM
3539 if (TYPE_P (parser->scope)
3540 /* Since checking types for dependency can be expensive,
3541 avoid doing it if the type is already complete. */
3542 && !COMPLETE_TYPE_P (parser->scope)
3543 /* Do not try to complete dependent types. */
1fb3244a 3544 && !dependent_type_p (parser->scope))
a723baf1
MM
3545 complete_type (parser->scope);
3546 }
3547
cf22909c
KL
3548 /* Retrieve any deferred checks. Do not pop this access checks yet
3549 so the memory will not be reclaimed during token replacing below. */
3550 access_check = get_deferred_access_checks ();
3551
a723baf1
MM
3552 /* If parsing tentatively, replace the sequence of tokens that makes
3553 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3554 token. That way, should we re-parse the token stream, we will
3555 not have to repeat the effort required to do the parse, nor will
3556 we issue duplicate error messages. */
0c5e4866 3557 if (success && start)
a723baf1 3558 {
0c5e4866
NS
3559 cp_token *token = cp_lexer_token_at (parser->lexer, start);
3560
a723baf1
MM
3561 /* Reset the contents of the START token. */
3562 token->type = CPP_NESTED_NAME_SPECIFIER;
3563 token->value = build_tree_list (access_check, parser->scope);
3564 TREE_TYPE (token->value) = parser->qualifying_scope;
3565 token->keyword = RID_MAX;
0c5e4866 3566
a723baf1 3567 /* Purge all subsequent tokens. */
0c5e4866 3568 cp_lexer_purge_tokens_after (parser->lexer, start);
a723baf1
MM
3569 }
3570
cf22909c 3571 pop_deferring_access_checks ();
a723baf1
MM
3572 return success ? parser->scope : NULL_TREE;
3573}
3574
3575/* Parse a nested-name-specifier. See
3576 cp_parser_nested_name_specifier_opt for details. This function
3577 behaves identically, except that it will an issue an error if no
3578 nested-name-specifier is present, and it will return
3579 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3580 is present. */
3581
3582static tree
21526606
EC
3583cp_parser_nested_name_specifier (cp_parser *parser,
3584 bool typename_keyword_p,
a723baf1 3585 bool check_dependency_p,
a668c6ad
MM
3586 bool type_p,
3587 bool is_declaration)
a723baf1
MM
3588{
3589 tree scope;
3590
3591 /* Look for the nested-name-specifier. */
3592 scope = cp_parser_nested_name_specifier_opt (parser,
3593 typename_keyword_p,
3594 check_dependency_p,
a668c6ad
MM
3595 type_p,
3596 is_declaration);
a723baf1
MM
3597 /* If it was not present, issue an error message. */
3598 if (!scope)
3599 {
3600 cp_parser_error (parser, "expected nested-name-specifier");
eb5abb39 3601 parser->scope = NULL_TREE;
a723baf1
MM
3602 return error_mark_node;
3603 }
3604
3605 return scope;
3606}
3607
3608/* Parse a class-or-namespace-name.
3609
3610 class-or-namespace-name:
3611 class-name
3612 namespace-name
3613
3614 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3615 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3616 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3617 TYPE_P is TRUE iff the next name should be taken as a class-name,
3618 even the same name is declared to be another entity in the same
3619 scope.
3620
3621 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
eea9800f
MM
3622 specified by the class-or-namespace-name. If neither is found the
3623 ERROR_MARK_NODE is returned. */
a723baf1
MM
3624
3625static tree
21526606 3626cp_parser_class_or_namespace_name (cp_parser *parser,
a723baf1
MM
3627 bool typename_keyword_p,
3628 bool template_keyword_p,
3629 bool check_dependency_p,
a668c6ad
MM
3630 bool type_p,
3631 bool is_declaration)
a723baf1
MM
3632{
3633 tree saved_scope;
3634 tree saved_qualifying_scope;
3635 tree saved_object_scope;
3636 tree scope;
eea9800f 3637 bool only_class_p;
a723baf1 3638
a723baf1
MM
3639 /* Before we try to parse the class-name, we must save away the
3640 current PARSER->SCOPE since cp_parser_class_name will destroy
3641 it. */
3642 saved_scope = parser->scope;
3643 saved_qualifying_scope = parser->qualifying_scope;
3644 saved_object_scope = parser->object_scope;
eea9800f
MM
3645 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3646 there is no need to look for a namespace-name. */
bbaab916 3647 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
eea9800f
MM
3648 if (!only_class_p)
3649 cp_parser_parse_tentatively (parser);
21526606 3650 scope = cp_parser_class_name (parser,
a723baf1
MM
3651 typename_keyword_p,
3652 template_keyword_p,
fc6a28d7 3653 type_p ? class_type : none_type,
a723baf1 3654 check_dependency_p,
a668c6ad
MM
3655 /*class_head_p=*/false,
3656 is_declaration);
a723baf1 3657 /* If that didn't work, try for a namespace-name. */
eea9800f 3658 if (!only_class_p && !cp_parser_parse_definitely (parser))
a723baf1
MM
3659 {
3660 /* Restore the saved scope. */
3661 parser->scope = saved_scope;
3662 parser->qualifying_scope = saved_qualifying_scope;
3663 parser->object_scope = saved_object_scope;
eea9800f
MM
3664 /* If we are not looking at an identifier followed by the scope
3665 resolution operator, then this is not part of a
3666 nested-name-specifier. (Note that this function is only used
3667 to parse the components of a nested-name-specifier.) */
3668 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3669 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3670 return error_mark_node;
a723baf1
MM
3671 scope = cp_parser_namespace_name (parser);
3672 }
3673
3674 return scope;
3675}
3676
3677/* Parse a postfix-expression.
3678
3679 postfix-expression:
3680 primary-expression
3681 postfix-expression [ expression ]
3682 postfix-expression ( expression-list [opt] )
3683 simple-type-specifier ( expression-list [opt] )
21526606 3684 typename :: [opt] nested-name-specifier identifier
a723baf1
MM
3685 ( expression-list [opt] )
3686 typename :: [opt] nested-name-specifier template [opt] template-id
3687 ( expression-list [opt] )
3688 postfix-expression . template [opt] id-expression
3689 postfix-expression -> template [opt] id-expression
3690 postfix-expression . pseudo-destructor-name
3691 postfix-expression -> pseudo-destructor-name
3692 postfix-expression ++
3693 postfix-expression --
3694 dynamic_cast < type-id > ( expression )
3695 static_cast < type-id > ( expression )
3696 reinterpret_cast < type-id > ( expression )
3697 const_cast < type-id > ( expression )
3698 typeid ( expression )
3699 typeid ( type-id )
3700
3701 GNU Extension:
21526606 3702
a723baf1
MM
3703 postfix-expression:
3704 ( type-id ) { initializer-list , [opt] }
3705
3706 This extension is a GNU version of the C99 compound-literal
3707 construct. (The C99 grammar uses `type-name' instead of `type-id',
3708 but they are essentially the same concept.)
3709
3710 If ADDRESS_P is true, the postfix expression is the operand of the
93678513
MM
3711 `&' operator. CAST_P is true if this expression is the target of a
3712 cast.
a723baf1
MM
3713
3714 Returns a representation of the expression. */
3715
3716static tree
93678513 3717cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
a723baf1
MM
3718{
3719 cp_token *token;
3720 enum rid keyword;
b3445994 3721 cp_id_kind idk = CP_ID_KIND_NONE;
a723baf1
MM
3722 tree postfix_expression = NULL_TREE;
3723 /* Non-NULL only if the current postfix-expression can be used to
3724 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3725 class used to qualify the member. */
3726 tree qualifying_class = NULL_TREE;
a723baf1
MM
3727
3728 /* Peek at the next token. */
3729 token = cp_lexer_peek_token (parser->lexer);
3730 /* Some of the productions are determined by keywords. */
3731 keyword = token->keyword;
3732 switch (keyword)
3733 {
3734 case RID_DYNCAST:
3735 case RID_STATCAST:
3736 case RID_REINTCAST:
3737 case RID_CONSTCAST:
3738 {
3739 tree type;
3740 tree expression;
3741 const char *saved_message;
3742
3743 /* All of these can be handled in the same way from the point
3744 of view of parsing. Begin by consuming the token
3745 identifying the cast. */
3746 cp_lexer_consume_token (parser->lexer);
21526606 3747
a723baf1
MM
3748 /* New types cannot be defined in the cast. */
3749 saved_message = parser->type_definition_forbidden_message;
3750 parser->type_definition_forbidden_message
3751 = "types may not be defined in casts";
3752
3753 /* Look for the opening `<'. */
3754 cp_parser_require (parser, CPP_LESS, "`<'");
3755 /* Parse the type to which we are casting. */
3756 type = cp_parser_type_id (parser);
3757 /* Look for the closing `>'. */
3758 cp_parser_require (parser, CPP_GREATER, "`>'");
3759 /* Restore the old message. */
3760 parser->type_definition_forbidden_message = saved_message;
3761
3762 /* And the expression which is being cast. */
3763 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
93678513 3764 expression = cp_parser_expression (parser, /*cast_p=*/true);
a723baf1
MM
3765 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3766
14d22dd6
MM
3767 /* Only type conversions to integral or enumeration types
3768 can be used in constant-expressions. */
67c03833 3769 if (parser->integral_constant_expression_p
14d22dd6 3770 && !dependent_type_p (type)
263ee052 3771 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
98ca843c 3772 && (cp_parser_non_integral_constant_expression
625cbf93
MM
3773 (parser,
3774 "a cast to a type other than an integral or "
3775 "enumeration type")))
3776 return error_mark_node;
14d22dd6 3777
a723baf1
MM
3778 switch (keyword)
3779 {
3780 case RID_DYNCAST:
3781 postfix_expression
3782 = build_dynamic_cast (type, expression);
3783 break;
3784 case RID_STATCAST:
3785 postfix_expression
3786 = build_static_cast (type, expression);
3787 break;
3788 case RID_REINTCAST:
3789 postfix_expression
3790 = build_reinterpret_cast (type, expression);
3791 break;
3792 case RID_CONSTCAST:
3793 postfix_expression
3794 = build_const_cast (type, expression);
3795 break;
3796 default:
315fb5db 3797 gcc_unreachable ();
a723baf1
MM
3798 }
3799 }
3800 break;
3801
3802 case RID_TYPEID:
3803 {
3804 tree type;
3805 const char *saved_message;
4f8163b1 3806 bool saved_in_type_id_in_expr_p;
a723baf1
MM
3807
3808 /* Consume the `typeid' token. */
3809 cp_lexer_consume_token (parser->lexer);
3810 /* Look for the `(' token. */
3811 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3812 /* Types cannot be defined in a `typeid' expression. */
3813 saved_message = parser->type_definition_forbidden_message;
3814 parser->type_definition_forbidden_message
3815 = "types may not be defined in a `typeid\' expression";
3816 /* We can't be sure yet whether we're looking at a type-id or an
3817 expression. */
3818 cp_parser_parse_tentatively (parser);
3819 /* Try a type-id first. */
4f8163b1
MM
3820 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3821 parser->in_type_id_in_expr_p = true;
a723baf1 3822 type = cp_parser_type_id (parser);
4f8163b1 3823 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
3824 /* Look for the `)' token. Otherwise, we can't be sure that
3825 we're not looking at an expression: consider `typeid (int
3826 (3))', for example. */
3827 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3828 /* If all went well, simply lookup the type-id. */
3829 if (cp_parser_parse_definitely (parser))
3830 postfix_expression = get_typeid (type);
3831 /* Otherwise, fall back to the expression variant. */
3832 else
3833 {
3834 tree expression;
3835
3836 /* Look for an expression. */
93678513 3837 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
3838 /* Compute its typeid. */
3839 postfix_expression = build_typeid (expression);
3840 /* Look for the `)' token. */
3841 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3842 }
4424e0da 3843 /* `typeid' may not appear in an integral constant expression. */
98ca843c 3844 if (cp_parser_non_integral_constant_expression(parser,
4424e0da
GB
3845 "`typeid' operator"))
3846 return error_mark_node;
a723baf1
MM
3847 /* Restore the saved message. */
3848 parser->type_definition_forbidden_message = saved_message;
3849 }
3850 break;
21526606 3851
a723baf1
MM
3852 case RID_TYPENAME:
3853 {
3854 bool template_p = false;
3855 tree id;
3856 tree type;
3857
3858 /* Consume the `typename' token. */
3859 cp_lexer_consume_token (parser->lexer);
3860 /* Look for the optional `::' operator. */
21526606 3861 cp_parser_global_scope_opt (parser,
a723baf1
MM
3862 /*current_scope_valid_p=*/false);
3863 /* Look for the nested-name-specifier. */
3864 cp_parser_nested_name_specifier (parser,
3865 /*typename_keyword_p=*/true,
3866 /*check_dependency_p=*/true,
a668c6ad
MM
3867 /*type_p=*/true,
3868 /*is_declaration=*/true);
a723baf1
MM
3869 /* Look for the optional `template' keyword. */
3870 template_p = cp_parser_optional_template_keyword (parser);
3871 /* We don't know whether we're looking at a template-id or an
3872 identifier. */
3873 cp_parser_parse_tentatively (parser);
3874 /* Try a template-id. */
3875 id = cp_parser_template_id (parser, template_p,
a668c6ad
MM
3876 /*check_dependency_p=*/true,
3877 /*is_declaration=*/true);
a723baf1
MM
3878 /* If that didn't work, try an identifier. */
3879 if (!cp_parser_parse_definitely (parser))
3880 id = cp_parser_identifier (parser);
26bcf8fc
MM
3881 /* If we look up a template-id in a non-dependent qualifying
3882 scope, there's no need to create a dependent type. */
3883 if (TREE_CODE (id) == TYPE_DECL
3884 && !dependent_type_p (parser->scope))
3885 type = TREE_TYPE (id);
a723baf1
MM
3886 /* Create a TYPENAME_TYPE to represent the type to which the
3887 functional cast is being performed. */
26bcf8fc 3888 else
98ca843c 3889 type = make_typename_type (parser->scope, id,
fc6a28d7 3890 typename_type,
26bcf8fc 3891 /*complain=*/1);
a723baf1
MM
3892
3893 postfix_expression = cp_parser_functional_cast (parser, type);
3894 }
3895 break;
3896
3897 default:
3898 {
3899 tree type;
3900
3901 /* If the next thing is a simple-type-specifier, we may be
3902 looking at a functional cast. We could also be looking at
3903 an id-expression. So, we try the functional cast, and if
3904 that doesn't work we fall back to the primary-expression. */
3905 cp_parser_parse_tentatively (parser);
3906 /* Look for the simple-type-specifier. */
21526606 3907 type = cp_parser_simple_type_specifier (parser,
62d1db17
MM
3908 /*decl_specs=*/NULL,
3909 CP_PARSER_FLAGS_NONE);
a723baf1
MM
3910 /* Parse the cast itself. */
3911 if (!cp_parser_error_occurred (parser))
21526606 3912 postfix_expression
a723baf1
MM
3913 = cp_parser_functional_cast (parser, type);
3914 /* If that worked, we're done. */
3915 if (cp_parser_parse_definitely (parser))
3916 break;
3917
3918 /* If the functional-cast didn't work out, try a
3919 compound-literal. */
14d22dd6
MM
3920 if (cp_parser_allow_gnu_extensions_p (parser)
3921 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
a723baf1
MM
3922 {
3923 tree initializer_list = NULL_TREE;
4f8163b1 3924 bool saved_in_type_id_in_expr_p;
a723baf1
MM
3925
3926 cp_parser_parse_tentatively (parser);
14d22dd6
MM
3927 /* Consume the `('. */
3928 cp_lexer_consume_token (parser->lexer);
3929 /* Parse the type. */
4f8163b1
MM
3930 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3931 parser->in_type_id_in_expr_p = true;
14d22dd6 3932 type = cp_parser_type_id (parser);
4f8163b1 3933 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14d22dd6
MM
3934 /* Look for the `)'. */
3935 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3936 /* Look for the `{'. */
3937 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3938 /* If things aren't going well, there's no need to
3939 keep going. */
3940 if (!cp_parser_error_occurred (parser))
a723baf1 3941 {
39703eb9 3942 bool non_constant_p;
14d22dd6 3943 /* Parse the initializer-list. */
21526606 3944 initializer_list
39703eb9 3945 = cp_parser_initializer_list (parser, &non_constant_p);
14d22dd6
MM
3946 /* Allow a trailing `,'. */
3947 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3948 cp_lexer_consume_token (parser->lexer);
3949 /* Look for the final `}'. */
3950 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1
MM
3951 }
3952 /* If that worked, we're definitely looking at a
3953 compound-literal expression. */
3954 if (cp_parser_parse_definitely (parser))
3955 {
3956 /* Warn the user that a compound literal is not
3957 allowed in standard C++. */
3958 if (pedantic)
3959 pedwarn ("ISO C++ forbids compound-literals");
3960 /* Form the representation of the compound-literal. */
21526606 3961 postfix_expression
a723baf1
MM
3962 = finish_compound_literal (type, initializer_list);
3963 break;
3964 }
3965 }
3966
3967 /* It must be a primary-expression. */
21526606 3968 postfix_expression = cp_parser_primary_expression (parser,
93678513 3969 cast_p,
a723baf1
MM
3970 &idk,
3971 &qualifying_class);
3972 }
3973 break;
3974 }
3975
ee76b931
MM
3976 /* If we were avoiding committing to the processing of a
3977 qualified-id until we knew whether or not we had a
3978 pointer-to-member, we now know. */
089d6ea7 3979 if (qualifying_class)
a723baf1 3980 {
ee76b931 3981 bool done;
a723baf1 3982
ee76b931
MM
3983 /* Peek at the next token. */
3984 token = cp_lexer_peek_token (parser->lexer);
3985 done = (token->type != CPP_OPEN_SQUARE
3986 && token->type != CPP_OPEN_PAREN
3987 && token->type != CPP_DOT
3988 && token->type != CPP_DEREF
3989 && token->type != CPP_PLUS_PLUS
3990 && token->type != CPP_MINUS_MINUS);
3991
3992 postfix_expression = finish_qualified_id_expr (qualifying_class,
3993 postfix_expression,
3994 done,
3995 address_p);
3996 if (done)
3997 return postfix_expression;
a723baf1
MM
3998 }
3999
a723baf1
MM
4000 /* Keep looping until the postfix-expression is complete. */
4001 while (true)
4002 {
10b1d5e7
MM
4003 if (idk == CP_ID_KIND_UNQUALIFIED
4004 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
a723baf1 4005 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
b3445994 4006 /* It is not a Koenig lookup function call. */
21526606 4007 postfix_expression
b3445994 4008 = unqualified_name_lookup_error (postfix_expression);
21526606 4009
a723baf1
MM
4010 /* Peek at the next token. */
4011 token = cp_lexer_peek_token (parser->lexer);
4012
4013 switch (token->type)
4014 {
4015 case CPP_OPEN_SQUARE:
7a3ea201
RH
4016 postfix_expression
4017 = cp_parser_postfix_open_square_expression (parser,
4018 postfix_expression,
4019 false);
4020 idk = CP_ID_KIND_NONE;
a723baf1
MM
4021 break;
4022
4023 case CPP_OPEN_PAREN:
4024 /* postfix-expression ( expression-list [opt] ) */
4025 {
6d80c4b9 4026 bool koenig_p;
21526606 4027 tree args = (cp_parser_parenthesized_expression_list
93678513
MM
4028 (parser, false,
4029 /*cast_p=*/false,
4030 /*non_constant_p=*/NULL));
a723baf1 4031
7efa3e22
NS
4032 if (args == error_mark_node)
4033 {
4034 postfix_expression = error_mark_node;
4035 break;
4036 }
21526606 4037
14d22dd6
MM
4038 /* Function calls are not permitted in
4039 constant-expressions. */
100d337a
MA
4040 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4041 && cp_parser_non_integral_constant_expression (parser,
4042 "a function call"))
14d22dd6 4043 {
625cbf93
MM
4044 postfix_expression = error_mark_node;
4045 break;
14d22dd6 4046 }
a723baf1 4047
6d80c4b9 4048 koenig_p = false;
399dedb9
NS
4049 if (idk == CP_ID_KIND_UNQUALIFIED)
4050 {
89d594a2
NS
4051 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4052 {
4053 if (args)
4054 {
4055 koenig_p = true;
4056 postfix_expression
4057 = perform_koenig_lookup (postfix_expression, args);
4058 }
4059 else
4060 postfix_expression
4061 = unqualified_fn_lookup_error (postfix_expression);
4062 }
676e33ca
MM
4063 /* We do not perform argument-dependent lookup if
4064 normal lookup finds a non-function, in accordance
4065 with the expected resolution of DR 218. */
89d594a2 4066 else if (args && is_overloaded_fn (postfix_expression))
6d80c4b9 4067 {
89d594a2
NS
4068 tree fn = get_first_fn (postfix_expression);
4069
4070 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4071 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4072
4073 /* Only do argument dependent lookup if regular
4074 lookup does not find a set of member functions.
4075 [basic.lookup.koenig]/2a */
4076 if (!DECL_FUNCTION_MEMBER_P (fn))
4077 {
4078 koenig_p = true;
4079 postfix_expression
4080 = perform_koenig_lookup (postfix_expression, args);
4081 }
6d80c4b9 4082 }
399dedb9 4083 }
21526606 4084
d17811fd 4085 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
a723baf1 4086 {
d17811fd
MM
4087 tree instance = TREE_OPERAND (postfix_expression, 0);
4088 tree fn = TREE_OPERAND (postfix_expression, 1);
4089
4090 if (processing_template_decl
4091 && (type_dependent_expression_p (instance)
4092 || (!BASELINK_P (fn)
4093 && TREE_CODE (fn) != FIELD_DECL)
584672ee 4094 || type_dependent_expression_p (fn)
d17811fd
MM
4095 || any_type_dependent_arguments_p (args)))
4096 {
4097 postfix_expression
6de9cd9a
DN
4098 = build_min_nt (CALL_EXPR, postfix_expression,
4099 args, NULL_TREE);
d17811fd
MM
4100 break;
4101 }
9f880ef9
MM
4102
4103 if (BASELINK_P (fn))
4104 postfix_expression
21526606
EC
4105 = (build_new_method_call
4106 (instance, fn, args, NULL_TREE,
4107 (idk == CP_ID_KIND_QUALIFIED
9f880ef9
MM
4108 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4109 else
4110 postfix_expression
4111 = finish_call_expr (postfix_expression, args,
4112 /*disallow_virtual=*/false,
4113 /*koenig_p=*/false);
a723baf1 4114 }
d17811fd
MM
4115 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4116 || TREE_CODE (postfix_expression) == MEMBER_REF
4117 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
a723baf1
MM
4118 postfix_expression = (build_offset_ref_call_from_tree
4119 (postfix_expression, args));
b3445994 4120 else if (idk == CP_ID_KIND_QUALIFIED)
2050a1bb
MM
4121 /* A call to a static class member, or a namespace-scope
4122 function. */
4123 postfix_expression
4124 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
4125 /*disallow_virtual=*/true,
4126 koenig_p);
a723baf1 4127 else
2050a1bb 4128 /* All other function calls. */
21526606
EC
4129 postfix_expression
4130 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
4131 /*disallow_virtual=*/false,
4132 koenig_p);
a723baf1
MM
4133
4134 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
b3445994 4135 idk = CP_ID_KIND_NONE;
a723baf1
MM
4136 }
4137 break;
21526606 4138
a723baf1
MM
4139 case CPP_DOT:
4140 case CPP_DEREF:
21526606
EC
4141 /* postfix-expression . template [opt] id-expression
4142 postfix-expression . pseudo-destructor-name
a723baf1
MM
4143 postfix-expression -> template [opt] id-expression
4144 postfix-expression -> pseudo-destructor-name */
98ca843c 4145
7a3ea201
RH
4146 /* Consume the `.' or `->' operator. */
4147 cp_lexer_consume_token (parser->lexer);
a723baf1 4148
7a3ea201
RH
4149 postfix_expression
4150 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4151 postfix_expression,
4152 false, &idk);
a723baf1
MM
4153 break;
4154
4155 case CPP_PLUS_PLUS:
4156 /* postfix-expression ++ */
4157 /* Consume the `++' token. */
4158 cp_lexer_consume_token (parser->lexer);
a5ac3982 4159 /* Generate a representation for the complete expression. */
21526606
EC
4160 postfix_expression
4161 = finish_increment_expr (postfix_expression,
a5ac3982 4162 POSTINCREMENT_EXPR);
14d22dd6 4163 /* Increments may not appear in constant-expressions. */
625cbf93
MM
4164 if (cp_parser_non_integral_constant_expression (parser,
4165 "an increment"))
4166 postfix_expression = error_mark_node;
b3445994 4167 idk = CP_ID_KIND_NONE;
a723baf1
MM
4168 break;
4169
4170 case CPP_MINUS_MINUS:
4171 /* postfix-expression -- */
4172 /* Consume the `--' token. */
4173 cp_lexer_consume_token (parser->lexer);
a5ac3982 4174 /* Generate a representation for the complete expression. */
21526606
EC
4175 postfix_expression
4176 = finish_increment_expr (postfix_expression,
a5ac3982 4177 POSTDECREMENT_EXPR);
14d22dd6 4178 /* Decrements may not appear in constant-expressions. */
625cbf93
MM
4179 if (cp_parser_non_integral_constant_expression (parser,
4180 "a decrement"))
4181 postfix_expression = error_mark_node;
b3445994 4182 idk = CP_ID_KIND_NONE;
a723baf1
MM
4183 break;
4184
4185 default:
4186 return postfix_expression;
4187 }
4188 }
4189
4190 /* We should never get here. */
315fb5db 4191 gcc_unreachable ();
a723baf1
MM
4192 return error_mark_node;
4193}
4194
7a3ea201
RH
4195/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4196 by cp_parser_builtin_offsetof. We're looking for
4197
4198 postfix-expression [ expression ]
4199
4200 FOR_OFFSETOF is set if we're being called in that context, which
4201 changes how we deal with integer constant expressions. */
4202
4203static tree
4204cp_parser_postfix_open_square_expression (cp_parser *parser,
4205 tree postfix_expression,
4206 bool for_offsetof)
4207{
4208 tree index;
4209
4210 /* Consume the `[' token. */
4211 cp_lexer_consume_token (parser->lexer);
4212
4213 /* Parse the index expression. */
4214 /* ??? For offsetof, there is a question of what to allow here. If
4215 offsetof is not being used in an integral constant expression context,
4216 then we *could* get the right answer by computing the value at runtime.
4217 If we are in an integral constant expression context, then we might
4218 could accept any constant expression; hard to say without analysis.
4219 Rather than open the barn door too wide right away, allow only integer
77880ae4 4220 constant expressions here. */
7a3ea201
RH
4221 if (for_offsetof)
4222 index = cp_parser_constant_expression (parser, false, NULL);
4223 else
93678513 4224 index = cp_parser_expression (parser, /*cast_p=*/false);
7a3ea201
RH
4225
4226 /* Look for the closing `]'. */
4227 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4228
4229 /* Build the ARRAY_REF. */
4230 postfix_expression = grok_array_decl (postfix_expression, index);
4231
4232 /* When not doing offsetof, array references are not permitted in
4233 constant-expressions. */
4234 if (!for_offsetof
4235 && (cp_parser_non_integral_constant_expression
4236 (parser, "an array reference")))
4237 postfix_expression = error_mark_node;
4238
4239 return postfix_expression;
4240}
4241
4242/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4243 by cp_parser_builtin_offsetof. We're looking for
4244
4245 postfix-expression . template [opt] id-expression
4246 postfix-expression . pseudo-destructor-name
4247 postfix-expression -> template [opt] id-expression
4248 postfix-expression -> pseudo-destructor-name
4249
4250 FOR_OFFSETOF is set if we're being called in that context. That sorta
4251 limits what of the above we'll actually accept, but nevermind.
4252 TOKEN_TYPE is the "." or "->" token, which will already have been
4253 removed from the stream. */
4254
4255static tree
4256cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4257 enum cpp_ttype token_type,
4258 tree postfix_expression,
4259 bool for_offsetof, cp_id_kind *idk)
4260{
4261 tree name;
4262 bool dependent_p;
4263 bool template_p;
17a27b4f 4264 bool pseudo_destructor_p;
7a3ea201
RH
4265 tree scope = NULL_TREE;
4266
4267 /* If this is a `->' operator, dereference the pointer. */
4268 if (token_type == CPP_DEREF)
4269 postfix_expression = build_x_arrow (postfix_expression);
4270 /* Check to see whether or not the expression is type-dependent. */
4271 dependent_p = type_dependent_expression_p (postfix_expression);
4272 /* The identifier following the `->' or `.' is not qualified. */
4273 parser->scope = NULL_TREE;
4274 parser->qualifying_scope = NULL_TREE;
4275 parser->object_scope = NULL_TREE;
4276 *idk = CP_ID_KIND_NONE;
4277 /* Enter the scope corresponding to the type of the object
4278 given by the POSTFIX_EXPRESSION. */
4279 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4280 {
4281 scope = TREE_TYPE (postfix_expression);
4282 /* According to the standard, no expression should ever have
4283 reference type. Unfortunately, we do not currently match
4284 the standard in this respect in that our internal representation
4285 of an expression may have reference type even when the standard
4286 says it does not. Therefore, we have to manually obtain the
4287 underlying type here. */
4288 scope = non_reference (scope);
4289 /* The type of the POSTFIX_EXPRESSION must be complete. */
4290 scope = complete_type_or_else (scope, NULL_TREE);
4291 /* Let the name lookup machinery know that we are processing a
4292 class member access expression. */
4293 parser->context->object_type = scope;
4294 /* If something went wrong, we want to be able to discern that case,
4295 as opposed to the case where there was no SCOPE due to the type
4296 of expression being dependent. */
4297 if (!scope)
4298 scope = error_mark_node;
4299 /* If the SCOPE was erroneous, make the various semantic analysis
4300 functions exit quickly -- and without issuing additional error
4301 messages. */
4302 if (scope == error_mark_node)
4303 postfix_expression = error_mark_node;
4304 }
4305
17a27b4f
MM
4306 /* Assume this expression is not a pseudo-destructor access. */
4307 pseudo_destructor_p = false;
4308
4309 /* If the SCOPE is a scalar type, then, if this is a valid program,
4310 we must be looking at a pseudo-destructor-name. */
4311 if (scope && SCALAR_TYPE_P (scope))
7a3ea201 4312 {
17a27b4f
MM
4313 tree s;
4314 tree type;
4315
4316 cp_parser_parse_tentatively (parser);
4317 /* Parse the pseudo-destructor-name. */
4318 s = NULL_TREE;
4319 cp_parser_pseudo_destructor_name (parser, &s, &type);
4320 if (cp_parser_parse_definitely (parser))
4321 {
4322 pseudo_destructor_p = true;
4323 postfix_expression
4324 = finish_pseudo_destructor_expr (postfix_expression,
4325 s, TREE_TYPE (type));
4326 }
4327 }
4328
4329 if (!pseudo_destructor_p)
4330 {
4331 /* If the SCOPE is not a scalar type, we are looking at an
4332 ordinary class member access expression, rather than a
4333 pseudo-destructor-name. */
7a3ea201
RH
4334 template_p = cp_parser_optional_template_keyword (parser);
4335 /* Parse the id-expression. */
4336 name = cp_parser_id_expression (parser, template_p,
4337 /*check_dependency_p=*/true,
4338 /*template_p=*/NULL,
4339 /*declarator_p=*/false);
4340 /* In general, build a SCOPE_REF if the member name is qualified.
4341 However, if the name was not dependent and has already been
4342 resolved; there is no need to build the SCOPE_REF. For example;
4343
4344 struct X { void f(); };
4345 template <typename T> void f(T* t) { t->X::f(); }
4346
4347 Even though "t" is dependent, "X::f" is not and has been resolved
4348 to a BASELINK; there is no need to include scope information. */
4349
4350 /* But we do need to remember that there was an explicit scope for
4351 virtual function calls. */
4352 if (parser->scope)
4353 *idk = CP_ID_KIND_QUALIFIED;
4354
fc6a28d7
MM
4355 /* If the name is a template-id that names a type, we will get a
4356 TYPE_DECL here. That is invalid code. */
4357 if (TREE_CODE (name) == TYPE_DECL)
7a3ea201 4358 {
fc6a28d7
MM
4359 error ("invalid use of %qD", name);
4360 postfix_expression = error_mark_node;
4361 }
4362 else
4363 {
4364 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4365 {
4366 name = build_nt (SCOPE_REF, parser->scope, name);
4367 parser->scope = NULL_TREE;
4368 parser->qualifying_scope = NULL_TREE;
4369 parser->object_scope = NULL_TREE;
4370 }
4371 if (scope && name && BASELINK_P (name))
4372 adjust_result_of_qualified_name_lookup
4373 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4374 postfix_expression
4375 = finish_class_member_access_expr (postfix_expression, name);
7a3ea201 4376 }
7a3ea201 4377 }
7a3ea201
RH
4378
4379 /* We no longer need to look up names in the scope of the object on
4380 the left-hand side of the `.' or `->' operator. */
4381 parser->context->object_type = NULL_TREE;
4382
4383 /* Outside of offsetof, these operators may not appear in
4384 constant-expressions. */
4385 if (!for_offsetof
98ca843c 4386 && (cp_parser_non_integral_constant_expression
7a3ea201
RH
4387 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4388 postfix_expression = error_mark_node;
4389
4390 return postfix_expression;
4391}
4392
7efa3e22 4393/* Parse a parenthesized expression-list.
a723baf1
MM
4394
4395 expression-list:
4396 assignment-expression
4397 expression-list, assignment-expression
4398
7efa3e22
NS
4399 attribute-list:
4400 expression-list
4401 identifier
4402 identifier, expression-list
4403
93678513
MM
4404 CAST_P is true if this expression is the target of a cast.
4405
a723baf1
MM
4406 Returns a TREE_LIST. The TREE_VALUE of each node is a
4407 representation of an assignment-expression. Note that a TREE_LIST
7efa3e22
NS
4408 is returned even if there is only a single expression in the list.
4409 error_mark_node is returned if the ( and or ) are
4410 missing. NULL_TREE is returned on no expressions. The parentheses
4411 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
39703eb9
MM
4412 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4413 indicates whether or not all of the expressions in the list were
4414 constant. */
a723baf1
MM
4415
4416static tree
21526606 4417cp_parser_parenthesized_expression_list (cp_parser* parser,
39703eb9 4418 bool is_attribute_list,
93678513 4419 bool cast_p,
39703eb9 4420 bool *non_constant_p)
a723baf1
MM
4421{
4422 tree expression_list = NULL_TREE;
1ed3dfd5 4423 bool fold_expr_p = is_attribute_list;
7efa3e22 4424 tree identifier = NULL_TREE;
39703eb9
MM
4425
4426 /* Assume all the expressions will be constant. */
4427 if (non_constant_p)
4428 *non_constant_p = false;
4429
7efa3e22
NS
4430 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4431 return error_mark_node;
21526606 4432
a723baf1 4433 /* Consume expressions until there are no more. */
7efa3e22
NS
4434 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4435 while (true)
4436 {
4437 tree expr;
21526606 4438
7efa3e22
NS
4439 /* At the beginning of attribute lists, check to see if the
4440 next token is an identifier. */
4441 if (is_attribute_list
4442 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4443 {
4444 cp_token *token;
21526606 4445
7efa3e22
NS
4446 /* Consume the identifier. */
4447 token = cp_lexer_consume_token (parser->lexer);
4448 /* Save the identifier. */
4449 identifier = token->value;
4450 }
4451 else
4452 {
4453 /* Parse the next assignment-expression. */
39703eb9
MM
4454 if (non_constant_p)
4455 {
4456 bool expr_non_constant_p;
21526606 4457 expr = (cp_parser_constant_expression
39703eb9
MM
4458 (parser, /*allow_non_constant_p=*/true,
4459 &expr_non_constant_p));
4460 if (expr_non_constant_p)
4461 *non_constant_p = true;
4462 }
4463 else
93678513 4464 expr = cp_parser_assignment_expression (parser, cast_p);
a723baf1 4465
1ed3dfd5
GB
4466 if (fold_expr_p)
4467 expr = fold_non_dependent_expr (expr);
4468
7efa3e22
NS
4469 /* Add it to the list. We add error_mark_node
4470 expressions to the list, so that we can still tell if
4471 the correct form for a parenthesized expression-list
4472 is found. That gives better errors. */
4473 expression_list = tree_cons (NULL_TREE, expr, expression_list);
a723baf1 4474
7efa3e22
NS
4475 if (expr == error_mark_node)
4476 goto skip_comma;
4477 }
a723baf1 4478
7efa3e22
NS
4479 /* After the first item, attribute lists look the same as
4480 expression lists. */
4481 is_attribute_list = false;
21526606 4482
7efa3e22
NS
4483 get_comma:;
4484 /* If the next token isn't a `,', then we are done. */
4485 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4486 break;
4487
4488 /* Otherwise, consume the `,' and keep going. */
4489 cp_lexer_consume_token (parser->lexer);
4490 }
21526606 4491
7efa3e22
NS
4492 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4493 {
4494 int ending;
21526606 4495
7efa3e22
NS
4496 skip_comma:;
4497 /* We try and resync to an unnested comma, as that will give the
4498 user better diagnostics. */
21526606
EC
4499 ending = cp_parser_skip_to_closing_parenthesis (parser,
4500 /*recovering=*/true,
4bb8ca28 4501 /*or_comma=*/true,
a668c6ad 4502 /*consume_paren=*/true);
7efa3e22
NS
4503 if (ending < 0)
4504 goto get_comma;
4505 if (!ending)
4506 return error_mark_node;
a723baf1
MM
4507 }
4508
4509 /* We built up the list in reverse order so we must reverse it now. */
7efa3e22
NS
4510 expression_list = nreverse (expression_list);
4511 if (identifier)
4512 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
21526606 4513
7efa3e22 4514 return expression_list;
a723baf1
MM
4515}
4516
4517/* Parse a pseudo-destructor-name.
4518
4519 pseudo-destructor-name:
4520 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4521 :: [opt] nested-name-specifier template template-id :: ~ type-name
4522 :: [opt] nested-name-specifier [opt] ~ type-name
4523
4524 If either of the first two productions is used, sets *SCOPE to the
4525 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4526 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
d6e57462 4527 or ERROR_MARK_NODE if the parse fails. */
a723baf1
MM
4528
4529static void
21526606
EC
4530cp_parser_pseudo_destructor_name (cp_parser* parser,
4531 tree* scope,
94edc4ab 4532 tree* type)
a723baf1
MM
4533{
4534 bool nested_name_specifier_p;
4535
b14454ba
MM
4536 /* Assume that things will not work out. */
4537 *type = error_mark_node;
4538
a723baf1
MM
4539 /* Look for the optional `::' operator. */
4540 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4541 /* Look for the optional nested-name-specifier. */
21526606 4542 nested_name_specifier_p
a723baf1
MM
4543 = (cp_parser_nested_name_specifier_opt (parser,
4544 /*typename_keyword_p=*/false,
4545 /*check_dependency_p=*/true,
a668c6ad 4546 /*type_p=*/false,
21526606 4547 /*is_declaration=*/true)
a723baf1
MM
4548 != NULL_TREE);
4549 /* Now, if we saw a nested-name-specifier, we might be doing the
4550 second production. */
21526606 4551 if (nested_name_specifier_p
a723baf1
MM
4552 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4553 {
4554 /* Consume the `template' keyword. */
4555 cp_lexer_consume_token (parser->lexer);
4556 /* Parse the template-id. */
21526606 4557 cp_parser_template_id (parser,
a723baf1 4558 /*template_keyword_p=*/true,
a668c6ad
MM
4559 /*check_dependency_p=*/false,
4560 /*is_declaration=*/true);
a723baf1
MM
4561 /* Look for the `::' token. */
4562 cp_parser_require (parser, CPP_SCOPE, "`::'");
4563 }
4564 /* If the next token is not a `~', then there might be some
9bcb9aae 4565 additional qualification. */
a723baf1
MM
4566 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4567 {
4568 /* Look for the type-name. */
4569 *scope = TREE_TYPE (cp_parser_type_name (parser));
d6e57462 4570
b14454ba
MM
4571 if (*scope == error_mark_node)
4572 return;
4573
4574 /* If we don't have ::~, then something has gone wrong. Since
4575 the only caller of this function is looking for something
4576 after `.' or `->' after a scalar type, most likely the
4577 program is trying to get a member of a non-aggregate
4578 type. */
4579 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
d6e57462
ILT
4580 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4581 {
4582 cp_parser_error (parser, "request for member of non-aggregate type");
d6e57462
ILT
4583 return;
4584 }
4585
a723baf1
MM
4586 /* Look for the `::' token. */
4587 cp_parser_require (parser, CPP_SCOPE, "`::'");
4588 }
4589 else
4590 *scope = NULL_TREE;
4591
4592 /* Look for the `~'. */
4593 cp_parser_require (parser, CPP_COMPL, "`~'");
4594 /* Look for the type-name again. We are not responsible for
4595 checking that it matches the first type-name. */
4596 *type = cp_parser_type_name (parser);
4597}
4598
4599/* Parse a unary-expression.
4600
4601 unary-expression:
4602 postfix-expression
4603 ++ cast-expression
4604 -- cast-expression
4605 unary-operator cast-expression
4606 sizeof unary-expression
4607 sizeof ( type-id )
4608 new-expression
4609 delete-expression
4610
4611 GNU Extensions:
4612
4613 unary-expression:
4614 __extension__ cast-expression
4615 __alignof__ unary-expression
4616 __alignof__ ( type-id )
4617 __real__ cast-expression
4618 __imag__ cast-expression
4619 && identifier
4620
4621 ADDRESS_P is true iff the unary-expression is appearing as the
93678513
MM
4622 operand of the `&' operator. CAST_P is true if this expression is
4623 the target of a cast.
a723baf1 4624
34cd5ae7 4625 Returns a representation of the expression. */
a723baf1
MM
4626
4627static tree
93678513 4628cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
a723baf1
MM
4629{
4630 cp_token *token;
4631 enum tree_code unary_operator;
4632
4633 /* Peek at the next token. */
4634 token = cp_lexer_peek_token (parser->lexer);
4635 /* Some keywords give away the kind of expression. */
4636 if (token->type == CPP_KEYWORD)
4637 {
4638 enum rid keyword = token->keyword;
4639
4640 switch (keyword)
4641 {
4642 case RID_ALIGNOF:
a723baf1
MM
4643 case RID_SIZEOF:
4644 {
4645 tree operand;
7a18b933 4646 enum tree_code op;
21526606 4647
7a18b933
NS
4648 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4649 /* Consume the token. */
a723baf1
MM
4650 cp_lexer_consume_token (parser->lexer);
4651 /* Parse the operand. */
4652 operand = cp_parser_sizeof_operand (parser, keyword);
4653
7a18b933
NS
4654 if (TYPE_P (operand))
4655 return cxx_sizeof_or_alignof_type (operand, op, true);
a723baf1 4656 else
7a18b933 4657 return cxx_sizeof_or_alignof_expr (operand, op);
a723baf1
MM
4658 }
4659
4660 case RID_NEW:
4661 return cp_parser_new_expression (parser);
4662
4663 case RID_DELETE:
4664 return cp_parser_delete_expression (parser);
21526606 4665
a723baf1
MM
4666 case RID_EXTENSION:
4667 {
4668 /* The saved value of the PEDANTIC flag. */
4669 int saved_pedantic;
4670 tree expr;
4671
4672 /* Save away the PEDANTIC flag. */
4673 cp_parser_extension_opt (parser, &saved_pedantic);
4674 /* Parse the cast-expression. */
d6b4ea85 4675 expr = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4676 /* Restore the PEDANTIC flag. */
4677 pedantic = saved_pedantic;
4678
4679 return expr;
4680 }
4681
4682 case RID_REALPART:
4683 case RID_IMAGPART:
4684 {
4685 tree expression;
4686
4687 /* Consume the `__real__' or `__imag__' token. */
4688 cp_lexer_consume_token (parser->lexer);
4689 /* Parse the cast-expression. */
d6b4ea85 4690 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4691 /* Create the complete representation. */
4692 return build_x_unary_op ((keyword == RID_REALPART
4693 ? REALPART_EXPR : IMAGPART_EXPR),
4694 expression);
4695 }
4696 break;
4697
4698 default:
4699 break;
4700 }
4701 }
4702
4703 /* Look for the `:: new' and `:: delete', which also signal the
4704 beginning of a new-expression, or delete-expression,
4705 respectively. If the next token is `::', then it might be one of
4706 these. */
4707 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4708 {
4709 enum rid keyword;
4710
4711 /* See if the token after the `::' is one of the keywords in
4712 which we're interested. */
4713 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4714 /* If it's `new', we have a new-expression. */
4715 if (keyword == RID_NEW)
4716 return cp_parser_new_expression (parser);
4717 /* Similarly, for `delete'. */
4718 else if (keyword == RID_DELETE)
4719 return cp_parser_delete_expression (parser);
4720 }
4721
4722 /* Look for a unary operator. */
4723 unary_operator = cp_parser_unary_operator (token);
4724 /* The `++' and `--' operators can be handled similarly, even though
4725 they are not technically unary-operators in the grammar. */
4726 if (unary_operator == ERROR_MARK)
4727 {
4728 if (token->type == CPP_PLUS_PLUS)
4729 unary_operator = PREINCREMENT_EXPR;
4730 else if (token->type == CPP_MINUS_MINUS)
4731 unary_operator = PREDECREMENT_EXPR;
4732 /* Handle the GNU address-of-label extension. */
4733 else if (cp_parser_allow_gnu_extensions_p (parser)
4734 && token->type == CPP_AND_AND)
4735 {
4736 tree identifier;
4737
4738 /* Consume the '&&' token. */
4739 cp_lexer_consume_token (parser->lexer);
4740 /* Look for the identifier. */
4741 identifier = cp_parser_identifier (parser);
4742 /* Create an expression representing the address. */
4743 return finish_label_address_expr (identifier);
4744 }
4745 }
4746 if (unary_operator != ERROR_MARK)
4747 {
4748 tree cast_expression;
a5ac3982
MM
4749 tree expression = error_mark_node;
4750 const char *non_constant_p = NULL;
a723baf1
MM
4751
4752 /* Consume the operator token. */
4753 token = cp_lexer_consume_token (parser->lexer);
4754 /* Parse the cast-expression. */
21526606 4755 cast_expression
93678513
MM
4756 = cp_parser_cast_expression (parser,
4757 unary_operator == ADDR_EXPR,
4758 /*cast_p=*/false);
a723baf1
MM
4759 /* Now, build an appropriate representation. */
4760 switch (unary_operator)
4761 {
4762 case INDIRECT_REF:
a5ac3982
MM
4763 non_constant_p = "`*'";
4764 expression = build_x_indirect_ref (cast_expression, "unary *");
4765 break;
4766
a723baf1 4767 case ADDR_EXPR:
7a3ea201 4768 non_constant_p = "`&'";
a5ac3982 4769 /* Fall through. */
d17811fd 4770 case BIT_NOT_EXPR:
a5ac3982
MM
4771 expression = build_x_unary_op (unary_operator, cast_expression);
4772 break;
4773
14d22dd6
MM
4774 case PREINCREMENT_EXPR:
4775 case PREDECREMENT_EXPR:
a5ac3982
MM
4776 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4777 ? "`++'" : "`--'");
14d22dd6 4778 /* Fall through. */
a723baf1
MM
4779 case CONVERT_EXPR:
4780 case NEGATE_EXPR:
4781 case TRUTH_NOT_EXPR:
a5ac3982
MM
4782 expression = finish_unary_op_expr (unary_operator, cast_expression);
4783 break;
a723baf1 4784
a723baf1 4785 default:
315fb5db 4786 gcc_unreachable ();
a723baf1 4787 }
a5ac3982 4788
98ca843c 4789 if (non_constant_p
625cbf93
MM
4790 && cp_parser_non_integral_constant_expression (parser,
4791 non_constant_p))
4792 expression = error_mark_node;
a5ac3982
MM
4793
4794 return expression;
a723baf1
MM
4795 }
4796
93678513 4797 return cp_parser_postfix_expression (parser, address_p, cast_p);
a723baf1
MM
4798}
4799
4800/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4801 unary-operator, the corresponding tree code is returned. */
4802
4803static enum tree_code
94edc4ab 4804cp_parser_unary_operator (cp_token* token)
a723baf1
MM
4805{
4806 switch (token->type)
4807 {
4808 case CPP_MULT:
4809 return INDIRECT_REF;
4810
4811 case CPP_AND:
4812 return ADDR_EXPR;
4813
4814 case CPP_PLUS:
4815 return CONVERT_EXPR;
4816
4817 case CPP_MINUS:
4818 return NEGATE_EXPR;
4819
4820 case CPP_NOT:
4821 return TRUTH_NOT_EXPR;
21526606 4822
a723baf1
MM
4823 case CPP_COMPL:
4824 return BIT_NOT_EXPR;
4825
4826 default:
4827 return ERROR_MARK;
4828 }
4829}
4830
4831/* Parse a new-expression.
4832
ca099ac8 4833 new-expression:
a723baf1
MM
4834 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4835 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4836
4837 Returns a representation of the expression. */
4838
4839static tree
94edc4ab 4840cp_parser_new_expression (cp_parser* parser)
a723baf1
MM
4841{
4842 bool global_scope_p;
4843 tree placement;
4844 tree type;
4845 tree initializer;
058b15c1 4846 tree nelts;
a723baf1
MM
4847
4848 /* Look for the optional `::' operator. */
21526606 4849 global_scope_p
a723baf1
MM
4850 = (cp_parser_global_scope_opt (parser,
4851 /*current_scope_valid_p=*/false)
4852 != NULL_TREE);
4853 /* Look for the `new' operator. */
4854 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4855 /* There's no easy way to tell a new-placement from the
4856 `( type-id )' construct. */
4857 cp_parser_parse_tentatively (parser);
4858 /* Look for a new-placement. */
4859 placement = cp_parser_new_placement (parser);
4860 /* If that didn't work out, there's no new-placement. */
4861 if (!cp_parser_parse_definitely (parser))
4862 placement = NULL_TREE;
4863
4864 /* If the next token is a `(', then we have a parenthesized
4865 type-id. */
4866 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4867 {
4868 /* Consume the `('. */
4869 cp_lexer_consume_token (parser->lexer);
4870 /* Parse the type-id. */
4871 type = cp_parser_type_id (parser);
4872 /* Look for the closing `)'. */
4873 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
98ca843c 4874 /* There should not be a direct-new-declarator in this production,
063e900f
GB
4875 but GCC used to allowed this, so we check and emit a sensible error
4876 message for this case. */
4877 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
0da99d4e
GB
4878 {
4879 error ("array bound forbidden after parenthesized type-id");
4880 inform ("try removing the parentheses around the type-id");
063e900f
GB
4881 cp_parser_direct_new_declarator (parser);
4882 }
17a27b4f 4883 nelts = NULL_TREE;
a723baf1
MM
4884 }
4885 /* Otherwise, there must be a new-type-id. */
4886 else
058b15c1 4887 type = cp_parser_new_type_id (parser, &nelts);
a723baf1
MM
4888
4889 /* If the next token is a `(', then we have a new-initializer. */
4890 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4891 initializer = cp_parser_new_initializer (parser);
4892 else
4893 initializer = NULL_TREE;
4894
625cbf93
MM
4895 /* A new-expression may not appear in an integral constant
4896 expression. */
4897 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4898 return error_mark_node;
4899
a723baf1 4900 /* Create a representation of the new-expression. */
058b15c1 4901 return build_new (placement, type, nelts, initializer, global_scope_p);
a723baf1
MM
4902}
4903
4904/* Parse a new-placement.
4905
4906 new-placement:
4907 ( expression-list )
4908
4909 Returns the same representation as for an expression-list. */
4910
4911static tree
94edc4ab 4912cp_parser_new_placement (cp_parser* parser)
a723baf1
MM
4913{
4914 tree expression_list;
4915
a723baf1 4916 /* Parse the expression-list. */
21526606 4917 expression_list = (cp_parser_parenthesized_expression_list
93678513
MM
4918 (parser, false, /*cast_p=*/false,
4919 /*non_constant_p=*/NULL));
a723baf1
MM
4920
4921 return expression_list;
4922}
4923
4924/* Parse a new-type-id.
4925
4926 new-type-id:
4927 type-specifier-seq new-declarator [opt]
4928
058b15c1
MM
4929 Returns the TYPE allocated. If the new-type-id indicates an array
4930 type, *NELTS is set to the number of elements in the last array
4931 bound; the TYPE will not include the last array bound. */
a723baf1
MM
4932
4933static tree
058b15c1 4934cp_parser_new_type_id (cp_parser* parser, tree *nelts)
a723baf1 4935{
62d1db17 4936 cp_decl_specifier_seq type_specifier_seq;
058b15c1
MM
4937 cp_declarator *new_declarator;
4938 cp_declarator *declarator;
4939 cp_declarator *outer_declarator;
a723baf1 4940 const char *saved_message;
058b15c1 4941 tree type;
a723baf1
MM
4942
4943 /* The type-specifier sequence must not contain type definitions.
4944 (It cannot contain declarations of new types either, but if they
4945 are not definitions we will catch that because they are not
4946 complete.) */
4947 saved_message = parser->type_definition_forbidden_message;
4948 parser->type_definition_forbidden_message
4949 = "types may not be defined in a new-type-id";
4950 /* Parse the type-specifier-seq. */
62d1db17 4951 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
a723baf1
MM
4952 /* Restore the old message. */
4953 parser->type_definition_forbidden_message = saved_message;
4954 /* Parse the new-declarator. */
058b15c1
MM
4955 new_declarator = cp_parser_new_declarator_opt (parser);
4956
4957 /* Determine the number of elements in the last array dimension, if
4958 any. */
4959 *nelts = NULL_TREE;
4960 /* Skip down to the last array dimension. */
4961 declarator = new_declarator;
4962 outer_declarator = NULL;
4963 while (declarator && (declarator->kind == cdk_pointer
4964 || declarator->kind == cdk_ptrmem))
4965 {
4966 outer_declarator = declarator;
4967 declarator = declarator->declarator;
4968 }
98ca843c 4969 while (declarator
058b15c1
MM
4970 && declarator->kind == cdk_array
4971 && declarator->declarator
4972 && declarator->declarator->kind == cdk_array)
4973 {
4974 outer_declarator = declarator;
4975 declarator = declarator->declarator;
4976 }
98ca843c 4977
058b15c1
MM
4978 if (declarator && declarator->kind == cdk_array)
4979 {
4980 *nelts = declarator->u.array.bounds;
4981 if (*nelts == error_mark_node)
4982 *nelts = integer_one_node;
ad1063d5 4983
058b15c1
MM
4984 if (outer_declarator)
4985 outer_declarator->declarator = declarator->declarator;
4986 else
4987 new_declarator = NULL;
4988 }
a723baf1 4989
62d1db17 4990 type = groktypename (&type_specifier_seq, new_declarator);
058b15c1
MM
4991 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4992 {
4993 *nelts = array_type_nelts_top (type);
4994 type = TREE_TYPE (type);
4995 }
4996 return type;
a723baf1
MM
4997}
4998
4999/* Parse an (optional) new-declarator.
5000
5001 new-declarator:
5002 ptr-operator new-declarator [opt]
5003 direct-new-declarator
5004
058b15c1 5005 Returns the declarator. */
a723baf1 5006
058b15c1 5007static cp_declarator *
94edc4ab 5008cp_parser_new_declarator_opt (cp_parser* parser)
a723baf1
MM
5009{
5010 enum tree_code code;
5011 tree type;
3c01e5df 5012 cp_cv_quals cv_quals;
a723baf1
MM
5013
5014 /* We don't know if there's a ptr-operator next, or not. */
5015 cp_parser_parse_tentatively (parser);
5016 /* Look for a ptr-operator. */
3c01e5df 5017 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
a723baf1
MM
5018 /* If that worked, look for more new-declarators. */
5019 if (cp_parser_parse_definitely (parser))
5020 {
058b15c1 5021 cp_declarator *declarator;
a723baf1
MM
5022
5023 /* Parse another optional declarator. */
5024 declarator = cp_parser_new_declarator_opt (parser);
5025
5026 /* Create the representation of the declarator. */
058b15c1 5027 if (type)
3c01e5df 5028 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
058b15c1 5029 else if (code == INDIRECT_REF)
3c01e5df 5030 declarator = make_pointer_declarator (cv_quals, declarator);
a723baf1 5031 else
3c01e5df 5032 declarator = make_reference_declarator (cv_quals, declarator);
a723baf1 5033
a723baf1
MM
5034 return declarator;
5035 }
5036
5037 /* If the next token is a `[', there is a direct-new-declarator. */
5038 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5039 return cp_parser_direct_new_declarator (parser);
5040
058b15c1 5041 return NULL;
a723baf1
MM
5042}
5043
5044/* Parse a direct-new-declarator.
5045
5046 direct-new-declarator:
5047 [ expression ]
21526606 5048 direct-new-declarator [constant-expression]
a723baf1 5049
058b15c1 5050 */
a723baf1 5051
058b15c1 5052static cp_declarator *
94edc4ab 5053cp_parser_direct_new_declarator (cp_parser* parser)
a723baf1 5054{
058b15c1 5055 cp_declarator *declarator = NULL;
a723baf1
MM
5056
5057 while (true)
5058 {
5059 tree expression;
5060
5061 /* Look for the opening `['. */
5062 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5063 /* The first expression is not required to be constant. */
5064 if (!declarator)
5065 {
93678513 5066 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
5067 /* The standard requires that the expression have integral
5068 type. DR 74 adds enumeration types. We believe that the
5069 real intent is that these expressions be handled like the
5070 expression in a `switch' condition, which also allows
5071 classes with a single conversion to integral or
5072 enumeration type. */
5073 if (!processing_template_decl)
5074 {
21526606 5075 expression
a723baf1
MM
5076 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5077 expression,
b746c5dc 5078 /*complain=*/true);
a723baf1
MM
5079 if (!expression)
5080 {
2a13a625
GDR
5081 error ("expression in new-declarator must have integral "
5082 "or enumeration type");
a723baf1
MM
5083 expression = error_mark_node;
5084 }
5085 }
5086 }
5087 /* But all the other expressions must be. */
5088 else
21526606
EC
5089 expression
5090 = cp_parser_constant_expression (parser,
14d22dd6
MM
5091 /*allow_non_constant=*/false,
5092 NULL);
a723baf1
MM
5093 /* Look for the closing `]'. */
5094 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5095
5096 /* Add this bound to the declarator. */
058b15c1 5097 declarator = make_array_declarator (declarator, expression);
a723baf1
MM
5098
5099 /* If the next token is not a `[', then there are no more
5100 bounds. */
5101 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5102 break;
5103 }
5104
5105 return declarator;
5106}
5107
5108/* Parse a new-initializer.
5109
5110 new-initializer:
5111 ( expression-list [opt] )
5112
34cd5ae7 5113 Returns a representation of the expression-list. If there is no
a723baf1
MM
5114 expression-list, VOID_ZERO_NODE is returned. */
5115
5116static tree
94edc4ab 5117cp_parser_new_initializer (cp_parser* parser)
a723baf1
MM
5118{
5119 tree expression_list;
5120
21526606 5121 expression_list = (cp_parser_parenthesized_expression_list
93678513
MM
5122 (parser, false, /*cast_p=*/false,
5123 /*non_constant_p=*/NULL));
7efa3e22 5124 if (!expression_list)
a723baf1 5125 expression_list = void_zero_node;
a723baf1
MM
5126
5127 return expression_list;
5128}
5129
5130/* Parse a delete-expression.
5131
5132 delete-expression:
5133 :: [opt] delete cast-expression
5134 :: [opt] delete [ ] cast-expression
5135
5136 Returns a representation of the expression. */
5137
5138static tree
94edc4ab 5139cp_parser_delete_expression (cp_parser* parser)
a723baf1
MM
5140{
5141 bool global_scope_p;
5142 bool array_p;
5143 tree expression;
5144
5145 /* Look for the optional `::' operator. */
5146 global_scope_p
5147 = (cp_parser_global_scope_opt (parser,
5148 /*current_scope_valid_p=*/false)
5149 != NULL_TREE);
5150 /* Look for the `delete' keyword. */
5151 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5152 /* See if the array syntax is in use. */
5153 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5154 {
5155 /* Consume the `[' token. */
5156 cp_lexer_consume_token (parser->lexer);
5157 /* Look for the `]' token. */
5158 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5159 /* Remember that this is the `[]' construct. */
5160 array_p = true;
5161 }
5162 else
5163 array_p = false;
5164
5165 /* Parse the cast-expression. */
d6b4ea85 5166 expression = cp_parser_simple_cast_expression (parser);
a723baf1 5167
625cbf93
MM
5168 /* A delete-expression may not appear in an integral constant
5169 expression. */
5170 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5171 return error_mark_node;
5172
a723baf1
MM
5173 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5174}
5175
5176/* Parse a cast-expression.
5177
5178 cast-expression:
5179 unary-expression
5180 ( type-id ) cast-expression
5181
93678513
MM
5182 ADDRESS_P is true iff the unary-expression is appearing as the
5183 operand of the `&' operator. CAST_P is true if this expression is
5184 the target of a cast.
5185
a723baf1
MM
5186 Returns a representation of the expression. */
5187
5188static tree
93678513 5189cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
a723baf1
MM
5190{
5191 /* If it's a `(', then we might be looking at a cast. */
5192 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5193 {
5194 tree type = NULL_TREE;
5195 tree expr = NULL_TREE;
5196 bool compound_literal_p;
5197 const char *saved_message;
5198
5199 /* There's no way to know yet whether or not this is a cast.
5200 For example, `(int (3))' is a unary-expression, while `(int)
5201 3' is a cast. So, we resort to parsing tentatively. */
5202 cp_parser_parse_tentatively (parser);
5203 /* Types may not be defined in a cast. */
5204 saved_message = parser->type_definition_forbidden_message;
5205 parser->type_definition_forbidden_message
5206 = "types may not be defined in casts";
5207 /* Consume the `('. */
5208 cp_lexer_consume_token (parser->lexer);
5209 /* A very tricky bit is that `(struct S) { 3 }' is a
5210 compound-literal (which we permit in C++ as an extension).
5211 But, that construct is not a cast-expression -- it is a
5212 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5213 is legal; if the compound-literal were a cast-expression,
5214 you'd need an extra set of parentheses.) But, if we parse
5215 the type-id, and it happens to be a class-specifier, then we
5216 will commit to the parse at that point, because we cannot
5217 undo the action that is done when creating a new class. So,
21526606 5218 then we cannot back up and do a postfix-expression.
a723baf1
MM
5219
5220 Therefore, we scan ahead to the closing `)', and check to see
5221 if the token after the `)' is a `{'. If so, we are not
21526606 5222 looking at a cast-expression.
a723baf1
MM
5223
5224 Save tokens so that we can put them back. */
5225 cp_lexer_save_tokens (parser->lexer);
5226 /* Skip tokens until the next token is a closing parenthesis.
5227 If we find the closing `)', and the next token is a `{', then
5228 we are looking at a compound-literal. */
21526606 5229 compound_literal_p
a668c6ad
MM
5230 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5231 /*consume_paren=*/true)
a723baf1
MM
5232 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5233 /* Roll back the tokens we skipped. */
5234 cp_lexer_rollback_tokens (parser->lexer);
5235 /* If we were looking at a compound-literal, simulate an error
5236 so that the call to cp_parser_parse_definitely below will
5237 fail. */
5238 if (compound_literal_p)
5239 cp_parser_simulate_error (parser);
5240 else
5241 {
4f8163b1
MM
5242 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5243 parser->in_type_id_in_expr_p = true;
a723baf1
MM
5244 /* Look for the type-id. */
5245 type = cp_parser_type_id (parser);
5246 /* Look for the closing `)'. */
5247 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4f8163b1 5248 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
5249 }
5250
5251 /* Restore the saved message. */
5252 parser->type_definition_forbidden_message = saved_message;
5253
bbaab916
NS
5254 /* If ok so far, parse the dependent expression. We cannot be
5255 sure it is a cast. Consider `(T ())'. It is a parenthesized
5256 ctor of T, but looks like a cast to function returning T
5257 without a dependent expression. */
5258 if (!cp_parser_error_occurred (parser))
93678513
MM
5259 expr = cp_parser_cast_expression (parser,
5260 /*address_p=*/false,
5261 /*cast_p=*/true);
bbaab916 5262
a723baf1
MM
5263 if (cp_parser_parse_definitely (parser))
5264 {
a723baf1 5265 /* Warn about old-style casts, if so requested. */
21526606
EC
5266 if (warn_old_style_cast
5267 && !in_system_header
5268 && !VOID_TYPE_P (type)
a723baf1
MM
5269 && current_lang_name != lang_name_c)
5270 warning ("use of old-style cast");
14d22dd6
MM
5271
5272 /* Only type conversions to integral or enumeration types
5273 can be used in constant-expressions. */
67c03833 5274 if (parser->integral_constant_expression_p
14d22dd6 5275 && !dependent_type_p (type)
625cbf93 5276 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
98ca843c 5277 && (cp_parser_non_integral_constant_expression
625cbf93
MM
5278 (parser,
5279 "a cast to a type other than an integral or "
5280 "enumeration type")))
5281 return error_mark_node;
5282
a723baf1
MM
5283 /* Perform the cast. */
5284 expr = build_c_cast (type, expr);
bbaab916 5285 return expr;
a723baf1 5286 }
a723baf1
MM
5287 }
5288
5289 /* If we get here, then it's not a cast, so it must be a
5290 unary-expression. */
93678513 5291 return cp_parser_unary_expression (parser, address_p, cast_p);
a723baf1
MM
5292}
5293
b8b94c5b 5294/* Parse a binary expression of the general form:
a723baf1
MM
5295
5296 pm-expression:
5297 cast-expression
5298 pm-expression .* cast-expression
5299 pm-expression ->* cast-expression
5300
77077b39 5301 multiplicative-expression:
a723baf1
MM
5302 pm-expression
5303 multiplicative-expression * pm-expression
5304 multiplicative-expression / pm-expression
5305 multiplicative-expression % pm-expression
5306
a723baf1
MM
5307 additive-expression:
5308 multiplicative-expression
5309 additive-expression + multiplicative-expression
5310 additive-expression - multiplicative-expression
5311
a723baf1
MM
5312 shift-expression:
5313 additive-expression
5314 shift-expression << additive-expression
5315 shift-expression >> additive-expression
5316
a723baf1
MM
5317 relational-expression:
5318 shift-expression
5319 relational-expression < shift-expression
5320 relational-expression > shift-expression
5321 relational-expression <= shift-expression
5322 relational-expression >= shift-expression
5323
b8b94c5b
PB
5324 GNU Extension:
5325
a723baf1
MM
5326 relational-expression:
5327 relational-expression <? shift-expression
5328 relational-expression >? shift-expression
5329
a723baf1
MM
5330 equality-expression:
5331 relational-expression
5332 equality-expression == relational-expression
5333 equality-expression != relational-expression
5334
a723baf1
MM
5335 and-expression:
5336 equality-expression
5337 and-expression & equality-expression
5338
a723baf1
MM
5339 exclusive-or-expression:
5340 and-expression
5341 exclusive-or-expression ^ and-expression
5342
b8b94c5b
PB
5343 inclusive-or-expression:
5344 exclusive-or-expression
5345 inclusive-or-expression | exclusive-or-expression
a723baf1 5346
b8b94c5b
PB
5347 logical-and-expression:
5348 inclusive-or-expression
5349 logical-and-expression && inclusive-or-expression
a723baf1 5350
b8b94c5b
PB
5351 logical-or-expression:
5352 logical-and-expression
5353 logical-or-expression || logical-and-expression
a723baf1 5354
b8b94c5b 5355 All these are implemented with a single function like:
a723baf1 5356
b8b94c5b
PB
5357 binary-expression:
5358 simple-cast-expression
5359 binary-expression <token> binary-expression
a723baf1 5360
93678513
MM
5361 CAST_P is true if this expression is the target of a cast.
5362
b8b94c5b
PB
5363 The binops_by_token map is used to get the tree codes for each <token> type.
5364 binary-expressions are associated according to a precedence table. */
a723baf1 5365
b8b94c5b
PB
5366#define TOKEN_PRECEDENCE(token) \
5367 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5368 ? PREC_NOT_OPERATOR \
5369 : binops_by_token[token->type].prec)
a723baf1
MM
5370
5371static tree
93678513 5372cp_parser_binary_expression (cp_parser* parser, bool cast_p)
a723baf1 5373{
b8b94c5b
PB
5374 cp_parser_expression_stack stack;
5375 cp_parser_expression_stack_entry *sp = &stack[0];
5376 tree lhs, rhs;
5377 cp_token *token;
5378 enum tree_code tree_type;
5379 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5380 bool overloaded_p;
a723baf1 5381
b8b94c5b 5382 /* Parse the first expression. */
93678513 5383 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
a723baf1 5384
b8b94c5b
PB
5385 for (;;)
5386 {
5387 /* Get an operator token. */
5388 token = cp_lexer_peek_token (parser->lexer);
5389 new_prec = TOKEN_PRECEDENCE (token);
5390
5391 /* Popping an entry off the stack means we completed a subexpression:
5392 - either we found a token which is not an operator (`>' where it is not
5393 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5394 will happen repeatedly;
5395 - or, we found an operator which has lower priority. This is the case
5396 where the recursive descent *ascends*, as in `3 * 4 + 5' after
03fd3f84 5397 parsing `3 * 4'. */
b8b94c5b
PB
5398 if (new_prec <= prec)
5399 {
5400 if (sp == stack)
5401 break;
5402 else
5403 goto pop;
5404 }
a723baf1 5405
b8b94c5b
PB
5406 get_rhs:
5407 tree_type = binops_by_token[token->type].tree_type;
a723baf1 5408
03fd3f84 5409 /* We used the operator token. */
b8b94c5b 5410 cp_lexer_consume_token (parser->lexer);
a723baf1 5411
b8b94c5b
PB
5412 /* Extract another operand. It may be the RHS of this expression
5413 or the LHS of a new, higher priority expression. */
5414 rhs = cp_parser_simple_cast_expression (parser);
a723baf1 5415
b8b94c5b
PB
5416 /* Get another operator token. Look up its precedence to avoid
5417 building a useless (immediately popped) stack entry for common
03fd3f84 5418 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
b8b94c5b
PB
5419 token = cp_lexer_peek_token (parser->lexer);
5420 lookahead_prec = TOKEN_PRECEDENCE (token);
5421 if (lookahead_prec > new_prec)
5422 {
5423 /* ... and prepare to parse the RHS of the new, higher priority
43c2a69a
PB
5424 expression. Since precedence levels on the stack are
5425 monotonically increasing, we do not have to care about
5426 stack overflows. */
b8b94c5b
PB
5427 sp->prec = prec;
5428 sp->tree_type = tree_type;
5429 sp->lhs = lhs;
5430 sp++;
5431 lhs = rhs;
5432 prec = new_prec;
5433 new_prec = lookahead_prec;
5434 goto get_rhs;
5435
5436 pop:
5437 /* If the stack is not empty, we have parsed into LHS the right side
5438 (`4' in the example above) of an expression we had suspended.
5439 We can use the information on the stack to recover the LHS (`3')
5440 from the stack together with the tree code (`MULT_EXPR'), and
5441 the precedence of the higher level subexpression
5442 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5443 which will be used to actually build the additive expression. */
5444 --sp;
5445 prec = sp->prec;
5446 tree_type = sp->tree_type;
5447 rhs = lhs;
5448 lhs = sp->lhs;
5449 }
a723baf1 5450
b8b94c5b
PB
5451 overloaded_p = false;
5452 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
a723baf1 5453
b8b94c5b
PB
5454 /* If the binary operator required the use of an overloaded operator,
5455 then this expression cannot be an integral constant-expression.
5456 An overloaded operator can be used even if both operands are
5457 otherwise permissible in an integral constant-expression if at
5458 least one of the operands is of enumeration type. */
a723baf1 5459
b8b94c5b
PB
5460 if (overloaded_p
5461 && (cp_parser_non_integral_constant_expression
5462 (parser, "calls to overloaded operators")))
5463 return error_mark_node;
5464 }
a723baf1 5465
b8b94c5b 5466 return lhs;
a723baf1
MM
5467}
5468
b8b94c5b 5469
a723baf1
MM
5470/* Parse the `? expression : assignment-expression' part of a
5471 conditional-expression. The LOGICAL_OR_EXPR is the
5472 logical-or-expression that started the conditional-expression.
5473 Returns a representation of the entire conditional-expression.
5474
39703eb9 5475 This routine is used by cp_parser_assignment_expression.
a723baf1
MM
5476
5477 ? expression : assignment-expression
21526606 5478
a723baf1 5479 GNU Extensions:
21526606 5480
a723baf1
MM
5481 ? : assignment-expression */
5482
5483static tree
94edc4ab 5484cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
a723baf1
MM
5485{
5486 tree expr;
5487 tree assignment_expr;
5488
5489 /* Consume the `?' token. */
5490 cp_lexer_consume_token (parser->lexer);
5491 if (cp_parser_allow_gnu_extensions_p (parser)
5492 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5493 /* Implicit true clause. */
5494 expr = NULL_TREE;
5495 else
5496 /* Parse the expression. */
93678513 5497 expr = cp_parser_expression (parser, /*cast_p=*/false);
21526606 5498
a723baf1
MM
5499 /* The next token should be a `:'. */
5500 cp_parser_require (parser, CPP_COLON, "`:'");
5501 /* Parse the assignment-expression. */
93678513 5502 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
a723baf1
MM
5503
5504 /* Build the conditional-expression. */
5505 return build_x_conditional_expr (logical_or_expr,
5506 expr,
5507 assignment_expr);
5508}
5509
5510/* Parse an assignment-expression.
5511
5512 assignment-expression:
5513 conditional-expression
5514 logical-or-expression assignment-operator assignment_expression
5515 throw-expression
5516
93678513
MM
5517 CAST_P is true if this expression is the target of a cast.
5518
a723baf1
MM
5519 Returns a representation for the expression. */
5520
5521static tree
93678513 5522cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
a723baf1
MM
5523{
5524 tree expr;
5525
5526 /* If the next token is the `throw' keyword, then we're looking at
5527 a throw-expression. */
5528 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5529 expr = cp_parser_throw_expression (parser);
5530 /* Otherwise, it must be that we are looking at a
5531 logical-or-expression. */
5532 else
5533 {
b8b94c5b 5534 /* Parse the binary expressions (logical-or-expression). */
93678513 5535 expr = cp_parser_binary_expression (parser, cast_p);
a723baf1
MM
5536 /* If the next token is a `?' then we're actually looking at a
5537 conditional-expression. */
5538 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5539 return cp_parser_question_colon_clause (parser, expr);
21526606 5540 else
a723baf1
MM
5541 {
5542 enum tree_code assignment_operator;
5543
5544 /* If it's an assignment-operator, we're using the second
5545 production. */
21526606 5546 assignment_operator
a723baf1
MM
5547 = cp_parser_assignment_operator_opt (parser);
5548 if (assignment_operator != ERROR_MARK)
5549 {
5550 tree rhs;
5551
5552 /* Parse the right-hand side of the assignment. */
93678513 5553 rhs = cp_parser_assignment_expression (parser, cast_p);
14d22dd6
MM
5554 /* An assignment may not appear in a
5555 constant-expression. */
625cbf93
MM
5556 if (cp_parser_non_integral_constant_expression (parser,
5557 "an assignment"))
5558 return error_mark_node;
34cd5ae7 5559 /* Build the assignment expression. */
21526606
EC
5560 expr = build_x_modify_expr (expr,
5561 assignment_operator,
a723baf1
MM
5562 rhs);
5563 }
5564 }
5565 }
5566
5567 return expr;
5568}
5569
5570/* Parse an (optional) assignment-operator.
5571
21526606
EC
5572 assignment-operator: one of
5573 = *= /= %= += -= >>= <<= &= ^= |=
a723baf1
MM
5574
5575 GNU Extension:
21526606 5576
a723baf1
MM
5577 assignment-operator: one of
5578 <?= >?=
5579
5580 If the next token is an assignment operator, the corresponding tree
5581 code is returned, and the token is consumed. For example, for
5582 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5583 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5584 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5585 operator, ERROR_MARK is returned. */
5586
5587static enum tree_code
94edc4ab 5588cp_parser_assignment_operator_opt (cp_parser* parser)
a723baf1
MM
5589{
5590 enum tree_code op;
5591 cp_token *token;
5592
5593 /* Peek at the next toen. */
5594 token = cp_lexer_peek_token (parser->lexer);
5595
5596 switch (token->type)
5597 {
5598 case CPP_EQ:
5599 op = NOP_EXPR;
5600 break;
5601
5602 case CPP_MULT_EQ:
5603 op = MULT_EXPR;
5604 break;
5605
5606 case CPP_DIV_EQ:
5607 op = TRUNC_DIV_EXPR;
5608 break;
5609
5610 case CPP_MOD_EQ:
5611 op = TRUNC_MOD_EXPR;
5612 break;
5613
5614 case CPP_PLUS_EQ:
5615 op = PLUS_EXPR;
5616 break;
5617
5618 case CPP_MINUS_EQ:
5619 op = MINUS_EXPR;
5620 break;
5621
5622 case CPP_RSHIFT_EQ:
5623 op = RSHIFT_EXPR;
5624 break;
5625
5626 case CPP_LSHIFT_EQ:
5627 op = LSHIFT_EXPR;
5628 break;
5629
5630 case CPP_AND_EQ:
5631 op = BIT_AND_EXPR;
5632 break;
5633
5634 case CPP_XOR_EQ:
5635 op = BIT_XOR_EXPR;
5636 break;
5637
5638 case CPP_OR_EQ:
5639 op = BIT_IOR_EXPR;
5640 break;
5641
5642 case CPP_MIN_EQ:
5643 op = MIN_EXPR;
5644 break;
5645
5646 case CPP_MAX_EQ:
5647 op = MAX_EXPR;
5648 break;
5649
21526606 5650 default:
a723baf1
MM
5651 /* Nothing else is an assignment operator. */
5652 op = ERROR_MARK;
5653 }
5654
5655 /* If it was an assignment operator, consume it. */
5656 if (op != ERROR_MARK)
5657 cp_lexer_consume_token (parser->lexer);
5658
5659 return op;
5660}
5661
5662/* Parse an expression.
5663
5664 expression:
5665 assignment-expression
5666 expression , assignment-expression
5667
93678513
MM
5668 CAST_P is true if this expression is the target of a cast.
5669
a723baf1
MM
5670 Returns a representation of the expression. */
5671
5672static tree
93678513 5673cp_parser_expression (cp_parser* parser, bool cast_p)
a723baf1
MM
5674{
5675 tree expression = NULL_TREE;
a723baf1
MM
5676
5677 while (true)
5678 {
5679 tree assignment_expression;
5680
5681 /* Parse the next assignment-expression. */
21526606 5682 assignment_expression
93678513 5683 = cp_parser_assignment_expression (parser, cast_p);
a723baf1
MM
5684 /* If this is the first assignment-expression, we can just
5685 save it away. */
5686 if (!expression)
5687 expression = assignment_expression;
a723baf1 5688 else
d17811fd
MM
5689 expression = build_x_compound_expr (expression,
5690 assignment_expression);
a723baf1
MM
5691 /* If the next token is not a comma, then we are done with the
5692 expression. */
5693 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5694 break;
5695 /* Consume the `,'. */
5696 cp_lexer_consume_token (parser->lexer);
14d22dd6 5697 /* A comma operator cannot appear in a constant-expression. */
625cbf93
MM
5698 if (cp_parser_non_integral_constant_expression (parser,
5699 "a comma operator"))
5700 expression = error_mark_node;
14d22dd6 5701 }
a723baf1
MM
5702
5703 return expression;
5704}
5705
21526606 5706/* Parse a constant-expression.
a723baf1
MM
5707
5708 constant-expression:
21526606 5709 conditional-expression
14d22dd6
MM
5710
5711 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
d17811fd
MM
5712 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5713 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5714 is false, NON_CONSTANT_P should be NULL. */
a723baf1
MM
5715
5716static tree
21526606 5717cp_parser_constant_expression (cp_parser* parser,
14d22dd6
MM
5718 bool allow_non_constant_p,
5719 bool *non_constant_p)
a723baf1 5720{
67c03833
JM
5721 bool saved_integral_constant_expression_p;
5722 bool saved_allow_non_integral_constant_expression_p;
5723 bool saved_non_integral_constant_expression_p;
a723baf1
MM
5724 tree expression;
5725
5726 /* It might seem that we could simply parse the
5727 conditional-expression, and then check to see if it were
5728 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5729 one that the compiler can figure out is constant, possibly after
5730 doing some simplifications or optimizations. The standard has a
5731 precise definition of constant-expression, and we must honor
5732 that, even though it is somewhat more restrictive.
5733
5734 For example:
5735
5736 int i[(2, 3)];
5737
5738 is not a legal declaration, because `(2, 3)' is not a
5739 constant-expression. The `,' operator is forbidden in a
5740 constant-expression. However, GCC's constant-folding machinery
5741 will fold this operation to an INTEGER_CST for `3'. */
5742
14d22dd6 5743 /* Save the old settings. */
67c03833 5744 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
21526606 5745 saved_allow_non_integral_constant_expression_p
67c03833
JM
5746 = parser->allow_non_integral_constant_expression_p;
5747 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
a723baf1 5748 /* We are now parsing a constant-expression. */
67c03833
JM
5749 parser->integral_constant_expression_p = true;
5750 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5751 parser->non_integral_constant_expression_p = false;
39703eb9
MM
5752 /* Although the grammar says "conditional-expression", we parse an
5753 "assignment-expression", which also permits "throw-expression"
5754 and the use of assignment operators. In the case that
5755 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5756 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5757 actually essential that we look for an assignment-expression.
5758 For example, cp_parser_initializer_clauses uses this function to
5759 determine whether a particular assignment-expression is in fact
5760 constant. */
93678513 5761 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14d22dd6 5762 /* Restore the old settings. */
93678513
MM
5763 parser->integral_constant_expression_p
5764 = saved_integral_constant_expression_p;
21526606 5765 parser->allow_non_integral_constant_expression_p
67c03833 5766 = saved_allow_non_integral_constant_expression_p;
14d22dd6 5767 if (allow_non_constant_p)
67c03833 5768 *non_constant_p = parser->non_integral_constant_expression_p;
93678513
MM
5769 else if (parser->non_integral_constant_expression_p)
5770 expression = error_mark_node;
5771 parser->non_integral_constant_expression_p
5772 = saved_non_integral_constant_expression_p;
a723baf1
MM
5773
5774 return expression;
5775}
5776
7a3ea201
RH
5777/* Parse __builtin_offsetof.
5778
5779 offsetof-expression:
5780 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5781
5782 offsetof-member-designator:
5783 id-expression
5784 | offsetof-member-designator "." id-expression
5785 | offsetof-member-designator "[" expression "]"
5786*/
5787
5788static tree
5789cp_parser_builtin_offsetof (cp_parser *parser)
5790{
5791 int save_ice_p, save_non_ice_p;
5792 tree type, expr;
5793 cp_id_kind dummy;
5794
5795 /* We're about to accept non-integral-constant things, but will
5796 definitely yield an integral constant expression. Save and
5797 restore these values around our local parsing. */
5798 save_ice_p = parser->integral_constant_expression_p;
5799 save_non_ice_p = parser->non_integral_constant_expression_p;
5800
5801 /* Consume the "__builtin_offsetof" token. */
5802 cp_lexer_consume_token (parser->lexer);
5803 /* Consume the opening `('. */
5804 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5805 /* Parse the type-id. */
5806 type = cp_parser_type_id (parser);
5807 /* Look for the `,'. */
5808 cp_parser_require (parser, CPP_COMMA, "`,'");
5809
5810 /* Build the (type *)null that begins the traditional offsetof macro. */
5811 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5812
5813 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5814 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5815 true, &dummy);
5816 while (true)
5817 {
5818 cp_token *token = cp_lexer_peek_token (parser->lexer);
5819 switch (token->type)
5820 {
5821 case CPP_OPEN_SQUARE:
5822 /* offsetof-member-designator "[" expression "]" */
5823 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5824 break;
5825
5826 case CPP_DOT:
5827 /* offsetof-member-designator "." identifier */
5828 cp_lexer_consume_token (parser->lexer);
5829 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5830 true, &dummy);
5831 break;
5832
5833 case CPP_CLOSE_PAREN:
5834 /* Consume the ")" token. */
5835 cp_lexer_consume_token (parser->lexer);
5836 goto success;
5837
5838 default:
5839 /* Error. We know the following require will fail, but
5840 that gives the proper error message. */
5841 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5842 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5843 expr = error_mark_node;
5844 goto failure;
5845 }
5846 }
5847
5848 success:
42c244d8
RH
5849 /* If we're processing a template, we can't finish the semantics yet.
5850 Otherwise we can fold the entire expression now. */
5851 if (processing_template_decl)
5852 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5853 else
5854 expr = fold_offsetof (expr);
7a3ea201
RH
5855
5856 failure:
5857 parser->integral_constant_expression_p = save_ice_p;
5858 parser->non_integral_constant_expression_p = save_non_ice_p;
5859
5860 return expr;
5861}
5862
a723baf1
MM
5863/* Statements [gram.stmt.stmt] */
5864
21526606 5865/* Parse a statement.
a723baf1
MM
5866
5867 statement:
5868 labeled-statement
5869 expression-statement
5870 compound-statement
5871 selection-statement
5872 iteration-statement
5873 jump-statement
5874 declaration-statement
5875 try-block */
5876
5877static void
325c3691 5878cp_parser_statement (cp_parser* parser, tree in_statement_expr)
a723baf1
MM
5879{
5880 tree statement;
5881 cp_token *token;
93409b8c 5882 location_t statement_location;
a723baf1
MM
5883
5884 /* There is no statement yet. */
5885 statement = NULL_TREE;
5886 /* Peek at the next token. */
5887 token = cp_lexer_peek_token (parser->lexer);
6de9cd9a 5888 /* Remember the location of the first token in the statement. */
93409b8c 5889 statement_location = token->location;
a723baf1
MM
5890 /* If this is a keyword, then that will often determine what kind of
5891 statement we have. */
5892 if (token->type == CPP_KEYWORD)
5893 {
5894 enum rid keyword = token->keyword;
5895
5896 switch (keyword)
5897 {
5898 case RID_CASE:
5899 case RID_DEFAULT:
a5bcc582 5900 statement = cp_parser_labeled_statement (parser,
325c3691 5901 in_statement_expr);
a723baf1
MM
5902 break;
5903
5904 case RID_IF:
5905 case RID_SWITCH:
5906 statement = cp_parser_selection_statement (parser);
5907 break;
5908
5909 case RID_WHILE:
5910 case RID_DO:
5911 case RID_FOR:
5912 statement = cp_parser_iteration_statement (parser);
5913 break;
5914
5915 case RID_BREAK:
5916 case RID_CONTINUE:
5917 case RID_RETURN:
5918 case RID_GOTO:
5919 statement = cp_parser_jump_statement (parser);
5920 break;
5921
5922 case RID_TRY:
5923 statement = cp_parser_try_block (parser);
5924 break;
5925
5926 default:
5927 /* It might be a keyword like `int' that can start a
5928 declaration-statement. */
5929 break;
5930 }
5931 }
5932 else if (token->type == CPP_NAME)
5933 {
5934 /* If the next token is a `:', then we are looking at a
5935 labeled-statement. */
5936 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5937 if (token->type == CPP_COLON)
325c3691 5938 statement = cp_parser_labeled_statement (parser, in_statement_expr);
a723baf1
MM
5939 }
5940 /* Anything that starts with a `{' must be a compound-statement. */
5941 else if (token->type == CPP_OPEN_BRACE)
325c3691 5942 statement = cp_parser_compound_statement (parser, NULL, false);
36952dea
ZW
5943 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5944 a statement all its own. */
5945 else if (token->type == CPP_PRAGMA)
5946 {
5947 cp_lexer_handle_pragma (parser->lexer);
5948 return;
5949 }
a723baf1
MM
5950
5951 /* Everything else must be a declaration-statement or an
21526606 5952 expression-statement. Try for the declaration-statement
a723baf1
MM
5953 first, unless we are looking at a `;', in which case we know that
5954 we have an expression-statement. */
5955 if (!statement)
5956 {
5957 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5958 {
5959 cp_parser_parse_tentatively (parser);
5960 /* Try to parse the declaration-statement. */
5961 cp_parser_declaration_statement (parser);
5962 /* If that worked, we're done. */
5963 if (cp_parser_parse_definitely (parser))
5964 return;
5965 }
5966 /* Look for an expression-statement instead. */
325c3691 5967 statement = cp_parser_expression_statement (parser, in_statement_expr);
a723baf1
MM
5968 }
5969
5970 /* Set the line number for the statement. */
009ed910 5971 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
93409b8c 5972 SET_EXPR_LOCATION (statement, statement_location);
a723baf1
MM
5973}
5974
5975/* Parse a labeled-statement.
5976
5977 labeled-statement:
5978 identifier : statement
5979 case constant-expression : statement
98ce043b
MM
5980 default : statement
5981
5982 GNU Extension:
21526606 5983
98ce043b
MM
5984 labeled-statement:
5985 case constant-expression ... constant-expression : statement
a723baf1 5986
8c161995
RH
5987 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5988 For an ordinary label, returns a LABEL_EXPR. */
a723baf1
MM
5989
5990static tree
325c3691 5991cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
a723baf1
MM
5992{
5993 cp_token *token;
0e59b3fb 5994 tree statement = error_mark_node;
a723baf1
MM
5995
5996 /* The next token should be an identifier. */
5997 token = cp_lexer_peek_token (parser->lexer);
5998 if (token->type != CPP_NAME
5999 && token->type != CPP_KEYWORD)
6000 {
6001 cp_parser_error (parser, "expected labeled-statement");
6002 return error_mark_node;
6003 }
6004
6005 switch (token->keyword)
6006 {
6007 case RID_CASE:
6008 {
98ce043b
MM
6009 tree expr, expr_hi;
6010 cp_token *ellipsis;
a723baf1
MM
6011
6012 /* Consume the `case' token. */
6013 cp_lexer_consume_token (parser->lexer);
6014 /* Parse the constant-expression. */
21526606 6015 expr = cp_parser_constant_expression (parser,
d17811fd 6016 /*allow_non_constant_p=*/false,
14d22dd6 6017 NULL);
98ce043b
MM
6018
6019 ellipsis = cp_lexer_peek_token (parser->lexer);
6020 if (ellipsis->type == CPP_ELLIPSIS)
6021 {
6022 /* Consume the `...' token. */
6023 cp_lexer_consume_token (parser->lexer);
6024 expr_hi =
6025 cp_parser_constant_expression (parser,
6026 /*allow_non_constant_p=*/false,
6027 NULL);
6028 /* We don't need to emit warnings here, as the common code
6029 will do this for us. */
6030 }
6031 else
6032 expr_hi = NULL_TREE;
6033
0e59b3fb 6034 if (!parser->in_switch_statement_p)
2a13a625 6035 error ("case label %qE not within a switch statement", expr);
0e59b3fb 6036 else
98ce043b 6037 statement = finish_case_label (expr, expr_hi);
a723baf1
MM
6038 }
6039 break;
6040
6041 case RID_DEFAULT:
6042 /* Consume the `default' token. */
6043 cp_lexer_consume_token (parser->lexer);
0e59b3fb
MM
6044 if (!parser->in_switch_statement_p)
6045 error ("case label not within a switch statement");
6046 else
6047 statement = finish_case_label (NULL_TREE, NULL_TREE);
a723baf1
MM
6048 break;
6049
6050 default:
6051 /* Anything else must be an ordinary label. */
6052 statement = finish_label_stmt (cp_parser_identifier (parser));
6053 break;
6054 }
6055
6056 /* Require the `:' token. */
6057 cp_parser_require (parser, CPP_COLON, "`:'");
6058 /* Parse the labeled statement. */
325c3691 6059 cp_parser_statement (parser, in_statement_expr);
a723baf1
MM
6060
6061 /* Return the label, in the case of a `case' or `default' label. */
6062 return statement;
6063}
6064
6065/* Parse an expression-statement.
6066
6067 expression-statement:
6068 expression [opt] ;
6069
6070 Returns the new EXPR_STMT -- or NULL_TREE if the expression
a5bcc582
NS
6071 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6072 indicates whether this expression-statement is part of an
6073 expression statement. */
a723baf1
MM
6074
6075static tree
325c3691 6076cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
a723baf1 6077{
a5bcc582 6078 tree statement = NULL_TREE;
a723baf1 6079
a5bcc582 6080 /* If the next token is a ';', then there is no expression
04c06002 6081 statement. */
a723baf1 6082 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
93678513 6083 statement = cp_parser_expression (parser, /*cast_p=*/false);
21526606 6084
a723baf1 6085 /* Consume the final `;'. */
e0860732 6086 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1 6087
325c3691 6088 if (in_statement_expr
a5bcc582 6089 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
93678513
MM
6090 /* This is the final expression statement of a statement
6091 expression. */
6092 statement = finish_stmt_expr_expr (statement, in_statement_expr);
a5bcc582
NS
6093 else if (statement)
6094 statement = finish_expr_stmt (statement);
6095 else
6096 finish_stmt ();
21526606 6097
a723baf1
MM
6098 return statement;
6099}
6100
6101/* Parse a compound-statement.
6102
6103 compound-statement:
6104 { statement-seq [opt] }
21526606 6105
5882f0f3 6106 Returns a tree representing the statement. */
a723baf1
MM
6107
6108static tree
325c3691
RH
6109cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6110 bool in_try)
a723baf1
MM
6111{
6112 tree compound_stmt;
6113
6114 /* Consume the `{'. */
6115 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6116 return error_mark_node;
6117 /* Begin the compound-statement. */
325c3691 6118 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
a723baf1 6119 /* Parse an (optional) statement-seq. */
325c3691 6120 cp_parser_statement_seq_opt (parser, in_statement_expr);
a723baf1 6121 /* Finish the compound-statement. */
7a3397c7 6122 finish_compound_stmt (compound_stmt);
a723baf1
MM
6123 /* Consume the `}'. */
6124 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6125
6126 return compound_stmt;
6127}
6128
6129/* Parse an (optional) statement-seq.
6130
6131 statement-seq:
6132 statement
6133 statement-seq [opt] statement */
6134
6135static void
325c3691 6136cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
a723baf1
MM
6137{
6138 /* Scan statements until there aren't any more. */
6139 while (true)
6140 {
6141 /* If we're looking at a `}', then we've run out of statements. */
6142 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6143 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6144 break;
6145
6146 /* Parse the statement. */
325c3691 6147 cp_parser_statement (parser, in_statement_expr);
a723baf1
MM
6148 }
6149}
6150
6151/* Parse a selection-statement.
6152
6153 selection-statement:
6154 if ( condition ) statement
6155 if ( condition ) statement else statement
21526606 6156 switch ( condition ) statement
a723baf1
MM
6157
6158 Returns the new IF_STMT or SWITCH_STMT. */
6159
6160static tree
94edc4ab 6161cp_parser_selection_statement (cp_parser* parser)
a723baf1
MM
6162{
6163 cp_token *token;
6164 enum rid keyword;
6165
6166 /* Peek at the next token. */
6167 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6168
6169 /* See what kind of keyword it is. */
6170 keyword = token->keyword;
6171 switch (keyword)
6172 {
6173 case RID_IF:
6174 case RID_SWITCH:
6175 {
6176 tree statement;
6177 tree condition;
6178
6179 /* Look for the `('. */
6180 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6181 {
6182 cp_parser_skip_to_end_of_statement (parser);
6183 return error_mark_node;
6184 }
6185
6186 /* Begin the selection-statement. */
6187 if (keyword == RID_IF)
6188 statement = begin_if_stmt ();
6189 else
6190 statement = begin_switch_stmt ();
6191
6192 /* Parse the condition. */
6193 condition = cp_parser_condition (parser);
6194 /* Look for the `)'. */
6195 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
6196 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6197 /*consume_paren=*/true);
a723baf1
MM
6198
6199 if (keyword == RID_IF)
6200 {
a723baf1
MM
6201 /* Add the condition. */
6202 finish_if_stmt_cond (condition, statement);
6203
6204 /* Parse the then-clause. */
325c3691 6205 cp_parser_implicitly_scoped_statement (parser);
a723baf1
MM
6206 finish_then_clause (statement);
6207
6208 /* If the next token is `else', parse the else-clause. */
6209 if (cp_lexer_next_token_is_keyword (parser->lexer,
6210 RID_ELSE))
6211 {
a723baf1
MM
6212 /* Consume the `else' keyword. */
6213 cp_lexer_consume_token (parser->lexer);
325c3691 6214 begin_else_clause (statement);
a723baf1 6215 /* Parse the else-clause. */
325c3691 6216 cp_parser_implicitly_scoped_statement (parser);
a723baf1
MM
6217 finish_else_clause (statement);
6218 }
6219
6220 /* Now we're all done with the if-statement. */
325c3691 6221 finish_if_stmt (statement);
a723baf1
MM
6222 }
6223 else
6224 {
0e59b3fb 6225 bool in_switch_statement_p;
a723baf1
MM
6226
6227 /* Add the condition. */
6228 finish_switch_cond (condition, statement);
6229
6230 /* Parse the body of the switch-statement. */
0e59b3fb
MM
6231 in_switch_statement_p = parser->in_switch_statement_p;
6232 parser->in_switch_statement_p = true;
325c3691 6233 cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 6234 parser->in_switch_statement_p = in_switch_statement_p;
a723baf1
MM
6235
6236 /* Now we're all done with the switch-statement. */
6237 finish_switch_stmt (statement);
6238 }
6239
6240 return statement;
6241 }
6242 break;
6243
6244 default:
6245 cp_parser_error (parser, "expected selection-statement");
6246 return error_mark_node;
6247 }
6248}
6249
21526606 6250/* Parse a condition.
a723baf1
MM
6251
6252 condition:
6253 expression
21526606 6254 type-specifier-seq declarator = assignment-expression
a723baf1
MM
6255
6256 GNU Extension:
21526606 6257
a723baf1 6258 condition:
21526606 6259 type-specifier-seq declarator asm-specification [opt]
a723baf1 6260 attributes [opt] = assignment-expression
21526606 6261
a723baf1
MM
6262 Returns the expression that should be tested. */
6263
6264static tree
94edc4ab 6265cp_parser_condition (cp_parser* parser)
a723baf1 6266{
62d1db17 6267 cp_decl_specifier_seq type_specifiers;
a723baf1
MM
6268 const char *saved_message;
6269
6270 /* Try the declaration first. */
6271 cp_parser_parse_tentatively (parser);
6272 /* New types are not allowed in the type-specifier-seq for a
6273 condition. */
6274 saved_message = parser->type_definition_forbidden_message;
6275 parser->type_definition_forbidden_message
6276 = "types may not be defined in conditions";
6277 /* Parse the type-specifier-seq. */
62d1db17 6278 cp_parser_type_specifier_seq (parser, &type_specifiers);
a723baf1
MM
6279 /* Restore the saved message. */
6280 parser->type_definition_forbidden_message = saved_message;
6281 /* If all is well, we might be looking at a declaration. */
6282 if (!cp_parser_error_occurred (parser))
6283 {
6284 tree decl;
6285 tree asm_specification;
6286 tree attributes;
058b15c1 6287 cp_declarator *declarator;
a723baf1 6288 tree initializer = NULL_TREE;
21526606 6289
a723baf1 6290 /* Parse the declarator. */
62b8a44e 6291 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 6292 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
6293 /*parenthesized_p=*/NULL,
6294 /*member_p=*/false);
a723baf1
MM
6295 /* Parse the attributes. */
6296 attributes = cp_parser_attributes_opt (parser);
6297 /* Parse the asm-specification. */
6298 asm_specification = cp_parser_asm_specification_opt (parser);
6299 /* If the next token is not an `=', then we might still be
6300 looking at an expression. For example:
21526606 6301
a723baf1 6302 if (A(a).x)
21526606 6303
a723baf1
MM
6304 looks like a decl-specifier-seq and a declarator -- but then
6305 there is no `=', so this is an expression. */
6306 cp_parser_require (parser, CPP_EQ, "`='");
6307 /* If we did see an `=', then we are looking at a declaration
6308 for sure. */
6309 if (cp_parser_parse_definitely (parser))
6310 {
4514aa8c 6311 tree pushed_scope;
73a8adb6 6312
a723baf1 6313 /* Create the declaration. */
62d1db17 6314 decl = start_decl (declarator, &type_specifiers,
a723baf1 6315 /*initialized_p=*/true,
73a8adb6 6316 attributes, /*prefix_attributes=*/NULL_TREE,
4514aa8c 6317 &pushed_scope);
a723baf1 6318 /* Parse the assignment-expression. */
93678513
MM
6319 initializer = cp_parser_assignment_expression (parser,
6320 /*cast_p=*/false);
21526606 6321
a723baf1 6322 /* Process the initializer. */
21526606
EC
6323 cp_finish_decl (decl,
6324 initializer,
6325 asm_specification,
a723baf1 6326 LOOKUP_ONLYCONVERTING);
c162c75e 6327
4514aa8c
NS
6328 if (pushed_scope)
6329 pop_scope (pushed_scope);
21526606 6330
a723baf1
MM
6331 return convert_from_reference (decl);
6332 }
6333 }
6334 /* If we didn't even get past the declarator successfully, we are
6335 definitely not looking at a declaration. */
6336 else
6337 cp_parser_abort_tentative_parse (parser);
6338
6339 /* Otherwise, we are looking at an expression. */
93678513 6340 return cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
6341}
6342
6343/* Parse an iteration-statement.
6344
6345 iteration-statement:
6346 while ( condition ) statement
6347 do statement while ( expression ) ;
6348 for ( for-init-statement condition [opt] ; expression [opt] )
6349 statement
6350
6351 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6352
6353static tree
94edc4ab 6354cp_parser_iteration_statement (cp_parser* parser)
a723baf1
MM
6355{
6356 cp_token *token;
6357 enum rid keyword;
6358 tree statement;
0e59b3fb
MM
6359 bool in_iteration_statement_p;
6360
a723baf1
MM
6361
6362 /* Peek at the next token. */
6363 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6364 if (!token)
6365 return error_mark_node;
6366
0e59b3fb 6367 /* Remember whether or not we are already within an iteration
21526606 6368 statement. */
0e59b3fb
MM
6369 in_iteration_statement_p = parser->in_iteration_statement_p;
6370
a723baf1
MM
6371 /* See what kind of keyword it is. */
6372 keyword = token->keyword;
6373 switch (keyword)
6374 {
6375 case RID_WHILE:
6376 {
6377 tree condition;
6378
6379 /* Begin the while-statement. */
6380 statement = begin_while_stmt ();
6381 /* Look for the `('. */
6382 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6383 /* Parse the condition. */
6384 condition = cp_parser_condition (parser);
6385 finish_while_stmt_cond (condition, statement);
6386 /* Look for the `)'. */
6387 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6388 /* Parse the dependent statement. */
0e59b3fb 6389 parser->in_iteration_statement_p = true;
a723baf1 6390 cp_parser_already_scoped_statement (parser);
0e59b3fb 6391 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
6392 /* We're done with the while-statement. */
6393 finish_while_stmt (statement);
6394 }
6395 break;
6396
6397 case RID_DO:
6398 {
6399 tree expression;
6400
6401 /* Begin the do-statement. */
6402 statement = begin_do_stmt ();
6403 /* Parse the body of the do-statement. */
0e59b3fb 6404 parser->in_iteration_statement_p = true;
a723baf1 6405 cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 6406 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
6407 finish_do_body (statement);
6408 /* Look for the `while' keyword. */
6409 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6410 /* Look for the `('. */
6411 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6412 /* Parse the expression. */
93678513 6413 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
6414 /* We're done with the do-statement. */
6415 finish_do_stmt (expression, statement);
6416 /* Look for the `)'. */
6417 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6418 /* Look for the `;'. */
6419 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6420 }
6421 break;
6422
6423 case RID_FOR:
6424 {
6425 tree condition = NULL_TREE;
6426 tree expression = NULL_TREE;
6427
6428 /* Begin the for-statement. */
6429 statement = begin_for_stmt ();
6430 /* Look for the `('. */
6431 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6432 /* Parse the initialization. */
6433 cp_parser_for_init_statement (parser);
6434 finish_for_init_stmt (statement);
6435
6436 /* If there's a condition, process it. */
6437 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6438 condition = cp_parser_condition (parser);
6439 finish_for_cond (condition, statement);
6440 /* Look for the `;'. */
6441 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6442
6443 /* If there's an expression, process it. */
6444 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
93678513 6445 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
6446 finish_for_expr (expression, statement);
6447 /* Look for the `)'. */
d5a10cf0 6448 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
98ca843c 6449
a723baf1 6450 /* Parse the body of the for-statement. */
0e59b3fb 6451 parser->in_iteration_statement_p = true;
a723baf1 6452 cp_parser_already_scoped_statement (parser);
0e59b3fb 6453 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
6454
6455 /* We're done with the for-statement. */
6456 finish_for_stmt (statement);
6457 }
6458 break;
6459
6460 default:
6461 cp_parser_error (parser, "expected iteration-statement");
6462 statement = error_mark_node;
6463 break;
6464 }
6465
6466 return statement;
6467}
6468
6469/* Parse a for-init-statement.
6470
6471 for-init-statement:
6472 expression-statement
6473 simple-declaration */
6474
6475static void
94edc4ab 6476cp_parser_for_init_statement (cp_parser* parser)
a723baf1
MM
6477{
6478 /* If the next token is a `;', then we have an empty
34cd5ae7 6479 expression-statement. Grammatically, this is also a
a723baf1
MM
6480 simple-declaration, but an invalid one, because it does not
6481 declare anything. Therefore, if we did not handle this case
6482 specially, we would issue an error message about an invalid
6483 declaration. */
6484 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6485 {
6486 /* We're going to speculatively look for a declaration, falling back
6487 to an expression, if necessary. */
6488 cp_parser_parse_tentatively (parser);
6489 /* Parse the declaration. */
6490 cp_parser_simple_declaration (parser,
6491 /*function_definition_allowed_p=*/false);
6492 /* If the tentative parse failed, then we shall need to look for an
6493 expression-statement. */
6494 if (cp_parser_parse_definitely (parser))
6495 return;
6496 }
6497
a5bcc582 6498 cp_parser_expression_statement (parser, false);
a723baf1
MM
6499}
6500
6501/* Parse a jump-statement.
6502
6503 jump-statement:
6504 break ;
6505 continue ;
6506 return expression [opt] ;
21526606 6507 goto identifier ;
a723baf1
MM
6508
6509 GNU extension:
6510
6511 jump-statement:
6512 goto * expression ;
6513
5088b058 6514 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
a723baf1
MM
6515
6516static tree
94edc4ab 6517cp_parser_jump_statement (cp_parser* parser)
a723baf1
MM
6518{
6519 tree statement = error_mark_node;
6520 cp_token *token;
6521 enum rid keyword;
6522
6523 /* Peek at the next token. */
6524 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6525 if (!token)
6526 return error_mark_node;
6527
6528 /* See what kind of keyword it is. */
6529 keyword = token->keyword;
6530 switch (keyword)
6531 {
6532 case RID_BREAK:
0e59b3fb
MM
6533 if (!parser->in_switch_statement_p
6534 && !parser->in_iteration_statement_p)
6535 {
6536 error ("break statement not within loop or switch");
6537 statement = error_mark_node;
6538 }
6539 else
6540 statement = finish_break_stmt ();
2a13a625 6541 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6542 break;
6543
6544 case RID_CONTINUE:
0e59b3fb
MM
6545 if (!parser->in_iteration_statement_p)
6546 {
6547 error ("continue statement not within a loop");
6548 statement = error_mark_node;
6549 }
6550 else
6551 statement = finish_continue_stmt ();
2a13a625 6552 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6553 break;
6554
6555 case RID_RETURN:
6556 {
6557 tree expr;
6558
21526606 6559 /* If the next token is a `;', then there is no
a723baf1
MM
6560 expression. */
6561 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
93678513 6562 expr = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
6563 else
6564 expr = NULL_TREE;
6565 /* Build the return-statement. */
6566 statement = finish_return_stmt (expr);
6567 /* Look for the final `;'. */
2a13a625 6568 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6569 }
6570 break;
6571
6572 case RID_GOTO:
6573 /* Create the goto-statement. */
6574 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6575 {
6576 /* Issue a warning about this use of a GNU extension. */
6577 if (pedantic)
6578 pedwarn ("ISO C++ forbids computed gotos");
6579 /* Consume the '*' token. */
6580 cp_lexer_consume_token (parser->lexer);
6581 /* Parse the dependent expression. */
93678513 6582 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
a723baf1
MM
6583 }
6584 else
6585 finish_goto_stmt (cp_parser_identifier (parser));
6586 /* Look for the final `;'. */
2a13a625 6587 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6588 break;
6589
6590 default:
6591 cp_parser_error (parser, "expected jump-statement");
6592 break;
6593 }
6594
6595 return statement;
6596}
6597
6598/* Parse a declaration-statement.
6599
6600 declaration-statement:
6601 block-declaration */
6602
6603static void
94edc4ab 6604cp_parser_declaration_statement (cp_parser* parser)
a723baf1 6605{
058b15c1
MM
6606 void *p;
6607
6608 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6609 p = obstack_alloc (&declarator_obstack, 0);
6610
6611 /* Parse the block-declaration. */
a723baf1
MM
6612 cp_parser_block_declaration (parser, /*statement_p=*/true);
6613
058b15c1
MM
6614 /* Free any declarators allocated. */
6615 obstack_free (&declarator_obstack, p);
6616
a723baf1
MM
6617 /* Finish off the statement. */
6618 finish_stmt ();
6619}
6620
6621/* Some dependent statements (like `if (cond) statement'), are
6622 implicitly in their own scope. In other words, if the statement is
6623 a single statement (as opposed to a compound-statement), it is
6624 none-the-less treated as if it were enclosed in braces. Any
6625 declarations appearing in the dependent statement are out of scope
6626 after control passes that point. This function parses a statement,
6627 but ensures that is in its own scope, even if it is not a
21526606 6628 compound-statement.
a723baf1
MM
6629
6630 Returns the new statement. */
6631
6632static tree
94edc4ab 6633cp_parser_implicitly_scoped_statement (cp_parser* parser)
a723baf1
MM
6634{
6635 tree statement;
6636
6637 /* If the token is not a `{', then we must take special action. */
6638 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6639 {
6640 /* Create a compound-statement. */
325c3691 6641 statement = begin_compound_stmt (0);
a723baf1 6642 /* Parse the dependent-statement. */
a5bcc582 6643 cp_parser_statement (parser, false);
a723baf1 6644 /* Finish the dummy compound-statement. */
7a3397c7 6645 finish_compound_stmt (statement);
a723baf1
MM
6646 }
6647 /* Otherwise, we simply parse the statement directly. */
6648 else
325c3691 6649 statement = cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
6650
6651 /* Return the statement. */
6652 return statement;
6653}
6654
6655/* For some dependent statements (like `while (cond) statement'), we
6656 have already created a scope. Therefore, even if the dependent
6657 statement is a compound-statement, we do not want to create another
6658 scope. */
6659
6660static void
94edc4ab 6661cp_parser_already_scoped_statement (cp_parser* parser)
a723baf1 6662{
325c3691
RH
6663 /* If the token is a `{', then we must take special action. */
6664 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6665 cp_parser_statement (parser, false);
6666 else
a723baf1 6667 {
325c3691
RH
6668 /* Avoid calling cp_parser_compound_statement, so that we
6669 don't create a new scope. Do everything else by hand. */
6670 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6671 cp_parser_statement_seq_opt (parser, false);
6672 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1 6673 }
a723baf1
MM
6674}
6675
6676/* Declarations [gram.dcl.dcl] */
6677
6678/* Parse an optional declaration-sequence.
6679
6680 declaration-seq:
6681 declaration
6682 declaration-seq declaration */
6683
6684static void
94edc4ab 6685cp_parser_declaration_seq_opt (cp_parser* parser)
a723baf1
MM
6686{
6687 while (true)
6688 {
6689 cp_token *token;
6690
6691 token = cp_lexer_peek_token (parser->lexer);
6692
6693 if (token->type == CPP_CLOSE_BRACE
6694 || token->type == CPP_EOF)
6695 break;
6696
21526606 6697 if (token->type == CPP_SEMICOLON)
a723baf1
MM
6698 {
6699 /* A declaration consisting of a single semicolon is
6700 invalid. Allow it unless we're being pedantic. */
a723baf1 6701 cp_lexer_consume_token (parser->lexer);
2cfe82fe
ZW
6702 if (pedantic && !in_system_header)
6703 pedwarn ("extra %<;%>");
a723baf1
MM
6704 continue;
6705 }
6706
7d381002 6707 /* If we're entering or exiting a region that's implicitly
03fd3f84 6708 extern "C", modify the lang context appropriately. */
7d381002
MA
6709 if (!parser->implicit_extern_c && token->implicit_extern_c)
6710 {
6711 push_lang_context (lang_name_c);
6712 parser->implicit_extern_c = true;
6713 }
6714 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6715 {
6716 pop_lang_context ();
6717 parser->implicit_extern_c = false;
6718 }
6719
36952dea
ZW
6720 if (token->type == CPP_PRAGMA)
6721 {
6722 /* A top-level declaration can consist solely of a #pragma.
6723 A nested declaration cannot, so this is done here and not
6724 in cp_parser_declaration. (A #pragma at block scope is
6725 handled in cp_parser_statement.) */
6726 cp_lexer_handle_pragma (parser->lexer);
6727 continue;
6728 }
6729
c838d82f 6730 /* Parse the declaration itself. */
a723baf1
MM
6731 cp_parser_declaration (parser);
6732 }
6733}
6734
6735/* Parse a declaration.
6736
6737 declaration:
6738 block-declaration
6739 function-definition
6740 template-declaration
6741 explicit-instantiation
6742 explicit-specialization
6743 linkage-specification
21526606 6744 namespace-definition
1092805d
MM
6745
6746 GNU extension:
6747
6748 declaration:
6749 __extension__ declaration */
a723baf1
MM
6750
6751static void
94edc4ab 6752cp_parser_declaration (cp_parser* parser)
a723baf1
MM
6753{
6754 cp_token token1;
6755 cp_token token2;
1092805d 6756 int saved_pedantic;
058b15c1 6757 void *p;
1092805d
MM
6758
6759 /* Check for the `__extension__' keyword. */
6760 if (cp_parser_extension_opt (parser, &saved_pedantic))
6761 {
6762 /* Parse the qualified declaration. */
6763 cp_parser_declaration (parser);
6764 /* Restore the PEDANTIC flag. */
6765 pedantic = saved_pedantic;
6766
6767 return;
6768 }
a723baf1
MM
6769
6770 /* Try to figure out what kind of declaration is present. */
6771 token1 = *cp_lexer_peek_token (parser->lexer);
21526606 6772
a723baf1
MM
6773 if (token1.type != CPP_EOF)
6774 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6775
058b15c1
MM
6776 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6777 p = obstack_alloc (&declarator_obstack, 0);
6778
a723baf1
MM
6779 /* If the next token is `extern' and the following token is a string
6780 literal, then we have a linkage specification. */
6781 if (token1.keyword == RID_EXTERN
6782 && cp_parser_is_string_literal (&token2))
6783 cp_parser_linkage_specification (parser);
6784 /* If the next token is `template', then we have either a template
6785 declaration, an explicit instantiation, or an explicit
6786 specialization. */
6787 else if (token1.keyword == RID_TEMPLATE)
6788 {
6789 /* `template <>' indicates a template specialization. */
6790 if (token2.type == CPP_LESS
6791 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6792 cp_parser_explicit_specialization (parser);
6793 /* `template <' indicates a template declaration. */
6794 else if (token2.type == CPP_LESS)
6795 cp_parser_template_declaration (parser, /*member_p=*/false);
6796 /* Anything else must be an explicit instantiation. */
6797 else
6798 cp_parser_explicit_instantiation (parser);
6799 }
6800 /* If the next token is `export', then we have a template
6801 declaration. */
6802 else if (token1.keyword == RID_EXPORT)
6803 cp_parser_template_declaration (parser, /*member_p=*/false);
6804 /* If the next token is `extern', 'static' or 'inline' and the one
6805 after that is `template', we have a GNU extended explicit
6806 instantiation directive. */
6807 else if (cp_parser_allow_gnu_extensions_p (parser)
6808 && (token1.keyword == RID_EXTERN
6809 || token1.keyword == RID_STATIC
6810 || token1.keyword == RID_INLINE)
6811 && token2.keyword == RID_TEMPLATE)
6812 cp_parser_explicit_instantiation (parser);
6813 /* If the next token is `namespace', check for a named or unnamed
6814 namespace definition. */
6815 else if (token1.keyword == RID_NAMESPACE
6816 && (/* A named namespace definition. */
6817 (token2.type == CPP_NAME
21526606 6818 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
a723baf1
MM
6819 == CPP_OPEN_BRACE))
6820 /* An unnamed namespace definition. */
6821 || token2.type == CPP_OPEN_BRACE))
6822 cp_parser_namespace_definition (parser);
6823 /* We must have either a block declaration or a function
6824 definition. */
6825 else
6826 /* Try to parse a block-declaration, or a function-definition. */
6827 cp_parser_block_declaration (parser, /*statement_p=*/false);
058b15c1
MM
6828
6829 /* Free any declarators allocated. */
6830 obstack_free (&declarator_obstack, p);
a723baf1
MM
6831}
6832
21526606 6833/* Parse a block-declaration.
a723baf1
MM
6834
6835 block-declaration:
6836 simple-declaration
6837 asm-definition
6838 namespace-alias-definition
6839 using-declaration
21526606 6840 using-directive
a723baf1
MM
6841
6842 GNU Extension:
6843
6844 block-declaration:
21526606 6845 __extension__ block-declaration
a723baf1
MM
6846 label-declaration
6847
34cd5ae7 6848 If STATEMENT_P is TRUE, then this block-declaration is occurring as
a723baf1
MM
6849 part of a declaration-statement. */
6850
6851static void
21526606 6852cp_parser_block_declaration (cp_parser *parser,
a723baf1
MM
6853 bool statement_p)
6854{
6855 cp_token *token1;
6856 int saved_pedantic;
6857
6858 /* Check for the `__extension__' keyword. */
6859 if (cp_parser_extension_opt (parser, &saved_pedantic))
6860 {
6861 /* Parse the qualified declaration. */
6862 cp_parser_block_declaration (parser, statement_p);
6863 /* Restore the PEDANTIC flag. */
6864 pedantic = saved_pedantic;
6865
6866 return;
6867 }
6868
6869 /* Peek at the next token to figure out which kind of declaration is
6870 present. */
6871 token1 = cp_lexer_peek_token (parser->lexer);
6872
6873 /* If the next keyword is `asm', we have an asm-definition. */
6874 if (token1->keyword == RID_ASM)
6875 {
6876 if (statement_p)
6877 cp_parser_commit_to_tentative_parse (parser);
6878 cp_parser_asm_definition (parser);
6879 }
6880 /* If the next keyword is `namespace', we have a
6881 namespace-alias-definition. */
6882 else if (token1->keyword == RID_NAMESPACE)
6883 cp_parser_namespace_alias_definition (parser);
6884 /* If the next keyword is `using', we have either a
6885 using-declaration or a using-directive. */
6886 else if (token1->keyword == RID_USING)
6887 {
6888 cp_token *token2;
6889
6890 if (statement_p)
6891 cp_parser_commit_to_tentative_parse (parser);
6892 /* If the token after `using' is `namespace', then we have a
6893 using-directive. */
6894 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6895 if (token2->keyword == RID_NAMESPACE)
6896 cp_parser_using_directive (parser);
6897 /* Otherwise, it's a using-declaration. */
6898 else
6899 cp_parser_using_declaration (parser);
6900 }
6901 /* If the next keyword is `__label__' we have a label declaration. */
6902 else if (token1->keyword == RID_LABEL)
6903 {
6904 if (statement_p)
6905 cp_parser_commit_to_tentative_parse (parser);
6906 cp_parser_label_declaration (parser);
6907 }
6908 /* Anything else must be a simple-declaration. */
6909 else
6910 cp_parser_simple_declaration (parser, !statement_p);
6911}
6912
6913/* Parse a simple-declaration.
6914
6915 simple-declaration:
21526606 6916 decl-specifier-seq [opt] init-declarator-list [opt] ;
a723baf1
MM
6917
6918 init-declarator-list:
6919 init-declarator
21526606 6920 init-declarator-list , init-declarator
a723baf1 6921
34cd5ae7 6922 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9bcb9aae 6923 function-definition as a simple-declaration. */
a723baf1
MM
6924
6925static void
21526606 6926cp_parser_simple_declaration (cp_parser* parser,
94edc4ab 6927 bool function_definition_allowed_p)
a723baf1 6928{
62d1db17 6929 cp_decl_specifier_seq decl_specifiers;
560ad596 6930 int declares_class_or_enum;
a723baf1
MM
6931 bool saw_declarator;
6932
6933 /* Defer access checks until we know what is being declared; the
6934 checks for names appearing in the decl-specifier-seq should be
6935 done as if we were in the scope of the thing being declared. */
8d241e0b 6936 push_deferring_access_checks (dk_deferred);
cf22909c 6937
a723baf1
MM
6938 /* Parse the decl-specifier-seq. We have to keep track of whether
6939 or not the decl-specifier-seq declares a named class or
6940 enumeration type, since that is the only case in which the
21526606 6941 init-declarator-list is allowed to be empty.
a723baf1
MM
6942
6943 [dcl.dcl]
6944
6945 In a simple-declaration, the optional init-declarator-list can be
6946 omitted only when declaring a class or enumeration, that is when
6947 the decl-specifier-seq contains either a class-specifier, an
6948 elaborated-type-specifier, or an enum-specifier. */
62d1db17
MM
6949 cp_parser_decl_specifier_seq (parser,
6950 CP_PARSER_FLAGS_OPTIONAL,
6951 &decl_specifiers,
6952 &declares_class_or_enum);
a723baf1 6953 /* We no longer need to defer access checks. */
cf22909c 6954 stop_deferring_access_checks ();
24c0ef37 6955
39703eb9
MM
6956 /* In a block scope, a valid declaration must always have a
6957 decl-specifier-seq. By not trying to parse declarators, we can
6958 resolve the declaration/expression ambiguity more quickly. */
98ca843c 6959 if (!function_definition_allowed_p
62d1db17 6960 && !decl_specifiers.any_specifiers_p)
39703eb9
MM
6961 {
6962 cp_parser_error (parser, "expected declaration");
6963 goto done;
6964 }
6965
8fbc5ae7
MM
6966 /* If the next two tokens are both identifiers, the code is
6967 erroneous. The usual cause of this situation is code like:
6968
6969 T t;
6970
6971 where "T" should name a type -- but does not. */
de3fe73c
MM
6972 if (!decl_specifiers.type
6973 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8fbc5ae7 6974 {
8d241e0b 6975 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
6976 looking at a declaration. */
6977 cp_parser_commit_to_tentative_parse (parser);
6978 /* Give up. */
39703eb9 6979 goto done;
8fbc5ae7 6980 }
996c2b52
MM
6981
6982 /* If we have seen at least one decl-specifier, and the next token
6983 is not a parenthesis, then we must be looking at a declaration.
6984 (After "int (" we might be looking at a functional cast.) */
6985 if (decl_specifiers.any_specifiers_p
6986 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6987 cp_parser_commit_to_tentative_parse (parser);
8fbc5ae7 6988
a723baf1
MM
6989 /* Keep going until we hit the `;' at the end of the simple
6990 declaration. */
6991 saw_declarator = false;
21526606 6992 while (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
6993 CPP_SEMICOLON))
6994 {
6995 cp_token *token;
6996 bool function_definition_p;
560ad596 6997 tree decl;
a723baf1
MM
6998
6999 saw_declarator = true;
7000 /* Parse the init-declarator. */
62d1db17 7001 decl = cp_parser_init_declarator (parser, &decl_specifiers,
560ad596
MM
7002 function_definition_allowed_p,
7003 /*member_p=*/false,
7004 declares_class_or_enum,
7005 &function_definition_p);
1fb3244a
MM
7006 /* If an error occurred while parsing tentatively, exit quickly.
7007 (That usually happens when in the body of a function; each
7008 statement is treated as a declaration-statement until proven
7009 otherwise.) */
7010 if (cp_parser_error_occurred (parser))
39703eb9 7011 goto done;
a723baf1
MM
7012 /* Handle function definitions specially. */
7013 if (function_definition_p)
7014 {
7015 /* If the next token is a `,', then we are probably
7016 processing something like:
7017
7018 void f() {}, *p;
7019
7020 which is erroneous. */
7021 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7022 error ("mixing declarations and function-definitions is forbidden");
7023 /* Otherwise, we're done with the list of declarators. */
7024 else
24c0ef37 7025 {
cf22909c 7026 pop_deferring_access_checks ();
24c0ef37
GS
7027 return;
7028 }
a723baf1
MM
7029 }
7030 /* The next token should be either a `,' or a `;'. */
7031 token = cp_lexer_peek_token (parser->lexer);
7032 /* If it's a `,', there are more declarators to come. */
7033 if (token->type == CPP_COMMA)
7034 cp_lexer_consume_token (parser->lexer);
7035 /* If it's a `;', we are done. */
7036 else if (token->type == CPP_SEMICOLON)
7037 break;
7038 /* Anything else is an error. */
7039 else
7040 {
996c2b52
MM
7041 /* If we have already issued an error message we don't need
7042 to issue another one. */
7043 if (decl != error_mark_node
0b16f8f4 7044 || cp_parser_uncommitted_to_tentative_parse_p (parser))
2a13a625 7045 cp_parser_error (parser, "expected %<,%> or %<;%>");
a723baf1
MM
7046 /* Skip tokens until we reach the end of the statement. */
7047 cp_parser_skip_to_end_of_statement (parser);
5a98fa7b
MM
7048 /* If the next token is now a `;', consume it. */
7049 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7050 cp_lexer_consume_token (parser->lexer);
39703eb9 7051 goto done;
a723baf1
MM
7052 }
7053 /* After the first time around, a function-definition is not
7054 allowed -- even if it was OK at first. For example:
7055
7056 int i, f() {}
7057
7058 is not valid. */
7059 function_definition_allowed_p = false;
7060 }
7061
7062 /* Issue an error message if no declarators are present, and the
7063 decl-specifier-seq does not itself declare a class or
7064 enumeration. */
7065 if (!saw_declarator)
7066 {
7067 if (cp_parser_declares_only_class_p (parser))
62d1db17 7068 shadow_tag (&decl_specifiers);
a723baf1 7069 /* Perform any deferred access checks. */
cf22909c 7070 perform_deferred_access_checks ();
a723baf1
MM
7071 }
7072
7073 /* Consume the `;'. */
7074 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7075
39703eb9
MM
7076 done:
7077 pop_deferring_access_checks ();
a723baf1
MM
7078}
7079
7080/* Parse a decl-specifier-seq.
7081
7082 decl-specifier-seq:
7083 decl-specifier-seq [opt] decl-specifier
7084
7085 decl-specifier:
7086 storage-class-specifier
7087 type-specifier
7088 function-specifier
7089 friend
21526606 7090 typedef
a723baf1
MM
7091
7092 GNU Extension:
7093
15077df5
MM
7094 decl-specifier:
7095 attributes
a723baf1 7096
62d1db17 7097 Set *DECL_SPECS to a representation of the decl-specifier-seq.
a723baf1 7098
eb1aef53 7099 The parser flags FLAGS is used to control type-specifier parsing.
560ad596
MM
7100
7101 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
543ca912 7102 flags:
560ad596
MM
7103
7104 1: one of the decl-specifiers is an elaborated-type-specifier
543ca912 7105 (i.e., a type declaration)
560ad596 7106 2: one of the decl-specifiers is an enum-specifier or a
543ca912 7107 class-specifier (i.e., a type definition)
98ca843c 7108
560ad596 7109 */
a723baf1 7110
62d1db17 7111static void
21526606 7112cp_parser_decl_specifier_seq (cp_parser* parser,
62d1db17
MM
7113 cp_parser_flags flags,
7114 cp_decl_specifier_seq *decl_specs,
560ad596 7115 int* declares_class_or_enum)
a723baf1 7116{
f2ce60b8 7117 bool constructor_possible_p = !parser->in_declarator_p;
21526606 7118
62d1db17
MM
7119 /* Clear DECL_SPECS. */
7120 clear_decl_specs (decl_specs);
7121
a723baf1 7122 /* Assume no class or enumeration type is declared. */
560ad596 7123 *declares_class_or_enum = 0;
a723baf1 7124
a723baf1
MM
7125 /* Keep reading specifiers until there are no more to read. */
7126 while (true)
7127 {
a723baf1 7128 bool constructor_p;
62d1db17 7129 bool found_decl_spec;
a723baf1
MM
7130 cp_token *token;
7131
7132 /* Peek at the next token. */
7133 token = cp_lexer_peek_token (parser->lexer);
7134 /* Handle attributes. */
7135 if (token->keyword == RID_ATTRIBUTE)
7136 {
7137 /* Parse the attributes. */
98ca843c 7138 decl_specs->attributes
62d1db17
MM
7139 = chainon (decl_specs->attributes,
7140 cp_parser_attributes_opt (parser));
a723baf1
MM
7141 continue;
7142 }
62d1db17
MM
7143 /* Assume we will find a decl-specifier keyword. */
7144 found_decl_spec = true;
a723baf1
MM
7145 /* If the next token is an appropriate keyword, we can simply
7146 add it to the list. */
7147 switch (token->keyword)
7148 {
a723baf1
MM
7149 /* decl-specifier:
7150 friend */
62d1db17
MM
7151 case RID_FRIEND:
7152 if (decl_specs->specs[(int) ds_friend]++)
2a13a625 7153 error ("duplicate %<friend%>");
a723baf1
MM
7154 /* Consume the token. */
7155 cp_lexer_consume_token (parser->lexer);
7156 break;
7157
7158 /* function-specifier:
7159 inline
7160 virtual
7161 explicit */
7162 case RID_INLINE:
7163 case RID_VIRTUAL:
7164 case RID_EXPLICIT:
62d1db17 7165 cp_parser_function_specifier_opt (parser, decl_specs);
a723baf1 7166 break;
21526606 7167
a723baf1
MM
7168 /* decl-specifier:
7169 typedef */
7170 case RID_TYPEDEF:
62d1db17 7171 ++decl_specs->specs[(int) ds_typedef];
a723baf1
MM
7172 /* Consume the token. */
7173 cp_lexer_consume_token (parser->lexer);
2050a1bb
MM
7174 /* A constructor declarator cannot appear in a typedef. */
7175 constructor_possible_p = false;
c006d942
MM
7176 /* The "typedef" keyword can only occur in a declaration; we
7177 may as well commit at this point. */
7178 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
7179 break;
7180
7181 /* storage-class-specifier:
7182 auto
7183 register
7184 static
7185 extern
21526606 7186 mutable
a723baf1
MM
7187
7188 GNU Extension:
7189 thread */
7190 case RID_AUTO:
62d1db17
MM
7191 /* Consume the token. */
7192 cp_lexer_consume_token (parser->lexer);
7193 cp_parser_set_storage_class (decl_specs, sc_auto);
7194 break;
a723baf1 7195 case RID_REGISTER:
62d1db17
MM
7196 /* Consume the token. */
7197 cp_lexer_consume_token (parser->lexer);
7198 cp_parser_set_storage_class (decl_specs, sc_register);
7199 break;
a723baf1 7200 case RID_STATIC:
62d1db17
MM
7201 /* Consume the token. */
7202 cp_lexer_consume_token (parser->lexer);
7203 if (decl_specs->specs[(int) ds_thread])
f1b90a04 7204 {
2d01edd7 7205 error ("%<__thread%> before %<static%>");
f1b90a04
MM
7206 decl_specs->specs[(int) ds_thread] = 0;
7207 }
7208 cp_parser_set_storage_class (decl_specs, sc_static);
62d1db17 7209 break;
a723baf1 7210 case RID_EXTERN:
62d1db17
MM
7211 /* Consume the token. */
7212 cp_lexer_consume_token (parser->lexer);
7213 if (decl_specs->specs[(int) ds_thread])
f1b90a04 7214 {
2d01edd7 7215 error ("%<__thread%> before %<extern%>");
f1b90a04
MM
7216 decl_specs->specs[(int) ds_thread] = 0;
7217 }
7218 cp_parser_set_storage_class (decl_specs, sc_extern);
62d1db17 7219 break;
a723baf1 7220 case RID_MUTABLE:
62d1db17
MM
7221 /* Consume the token. */
7222 cp_lexer_consume_token (parser->lexer);
7223 cp_parser_set_storage_class (decl_specs, sc_mutable);
7224 break;
a723baf1 7225 case RID_THREAD:
62d1db17
MM
7226 /* Consume the token. */
7227 cp_lexer_consume_token (parser->lexer);
7228 ++decl_specs->specs[(int) ds_thread];
a723baf1 7229 break;
21526606 7230
a723baf1 7231 default:
62d1db17
MM
7232 /* We did not yet find a decl-specifier yet. */
7233 found_decl_spec = false;
a723baf1
MM
7234 break;
7235 }
7236
7237 /* Constructors are a special case. The `S' in `S()' is not a
7238 decl-specifier; it is the beginning of the declarator. */
98ca843c 7239 constructor_p
62d1db17
MM
7240 = (!found_decl_spec
7241 && constructor_possible_p
98ca843c 7242 && (cp_parser_constructor_declarator_p
62d1db17 7243 (parser, decl_specs->specs[(int) ds_friend] != 0)));
a723baf1
MM
7244
7245 /* If we don't have a DECL_SPEC yet, then we must be looking at
7246 a type-specifier. */
62d1db17 7247 if (!found_decl_spec && !constructor_p)
a723baf1 7248 {
560ad596 7249 int decl_spec_declares_class_or_enum;
a723baf1 7250 bool is_cv_qualifier;
62d1db17 7251 tree type_spec;
a723baf1 7252
62d1db17 7253 type_spec
a723baf1 7254 = cp_parser_type_specifier (parser, flags,
62d1db17 7255 decl_specs,
a723baf1
MM
7256 /*is_declaration=*/true,
7257 &decl_spec_declares_class_or_enum,
7258 &is_cv_qualifier);
7259
7260 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7261
7262 /* If this type-specifier referenced a user-defined type
7263 (a typedef, class-name, etc.), then we can't allow any
7264 more such type-specifiers henceforth.
7265
7266 [dcl.spec]
7267
7268 The longest sequence of decl-specifiers that could
7269 possibly be a type name is taken as the
7270 decl-specifier-seq of a declaration. The sequence shall
7271 be self-consistent as described below.
7272
7273 [dcl.type]
7274
7275 As a general rule, at most one type-specifier is allowed
7276 in the complete decl-specifier-seq of a declaration. The
7277 only exceptions are the following:
7278
7279 -- const or volatile can be combined with any other
21526606 7280 type-specifier.
a723baf1
MM
7281
7282 -- signed or unsigned can be combined with char, long,
7283 short, or int.
7284
7285 -- ..
7286
7287 Example:
7288
7289 typedef char* Pc;
7290 void g (const int Pc);
7291
7292 Here, Pc is *not* part of the decl-specifier seq; it's
7293 the declarator. Therefore, once we see a type-specifier
7294 (other than a cv-qualifier), we forbid any additional
7295 user-defined types. We *do* still allow things like `int
7296 int' to be considered a decl-specifier-seq, and issue the
7297 error message later. */
62d1db17 7298 if (type_spec && !is_cv_qualifier)
a723baf1 7299 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
2050a1bb 7300 /* A constructor declarator cannot follow a type-specifier. */
62d1db17 7301 if (type_spec)
a723baf1 7302 {
62d1db17
MM
7303 constructor_possible_p = false;
7304 found_decl_spec = true;
a723baf1 7305 }
a723baf1
MM
7306 }
7307
62d1db17
MM
7308 /* If we still do not have a DECL_SPEC, then there are no more
7309 decl-specifiers. */
7310 if (!found_decl_spec)
7311 break;
a723baf1 7312
62d1db17 7313 decl_specs->any_specifiers_p = true;
a723baf1
MM
7314 /* After we see one decl-specifier, further decl-specifiers are
7315 always optional. */
7316 flags |= CP_PARSER_FLAGS_OPTIONAL;
7317 }
7318
0426c4ca 7319 /* Don't allow a friend specifier with a class definition. */
62d1db17
MM
7320 if (decl_specs->specs[(int) ds_friend] != 0
7321 && (*declares_class_or_enum & 2))
0426c4ca 7322 error ("class definition may not be declared a friend");
a723baf1
MM
7323}
7324
21526606 7325/* Parse an (optional) storage-class-specifier.
a723baf1
MM
7326
7327 storage-class-specifier:
7328 auto
7329 register
7330 static
7331 extern
21526606 7332 mutable
a723baf1
MM
7333
7334 GNU Extension:
7335
7336 storage-class-specifier:
7337 thread
7338
7339 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
21526606 7340
a723baf1 7341static tree
94edc4ab 7342cp_parser_storage_class_specifier_opt (cp_parser* parser)
a723baf1
MM
7343{
7344 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7345 {
7346 case RID_AUTO:
7347 case RID_REGISTER:
7348 case RID_STATIC:
7349 case RID_EXTERN:
7350 case RID_MUTABLE:
7351 case RID_THREAD:
7352 /* Consume the token. */
7353 return cp_lexer_consume_token (parser->lexer)->value;
7354
7355 default:
7356 return NULL_TREE;
7357 }
7358}
7359
21526606 7360/* Parse an (optional) function-specifier.
a723baf1
MM
7361
7362 function-specifier:
7363 inline
7364 virtual
7365 explicit
7366
62d1db17
MM
7367 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7368 Updates DECL_SPECS, if it is non-NULL. */
21526606 7369
a723baf1 7370static tree
62d1db17
MM
7371cp_parser_function_specifier_opt (cp_parser* parser,
7372 cp_decl_specifier_seq *decl_specs)
a723baf1
MM
7373{
7374 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7375 {
7376 case RID_INLINE:
62d1db17
MM
7377 if (decl_specs)
7378 ++decl_specs->specs[(int) ds_inline];
7379 break;
7380
a723baf1 7381 case RID_VIRTUAL:
62d1db17
MM
7382 if (decl_specs)
7383 ++decl_specs->specs[(int) ds_virtual];
7384 break;
7385
a723baf1 7386 case RID_EXPLICIT:
62d1db17
MM
7387 if (decl_specs)
7388 ++decl_specs->specs[(int) ds_explicit];
7389 break;
a723baf1
MM
7390
7391 default:
7392 return NULL_TREE;
7393 }
62d1db17
MM
7394
7395 /* Consume the token. */
7396 return cp_lexer_consume_token (parser->lexer)->value;
a723baf1
MM
7397}
7398
7399/* Parse a linkage-specification.
7400
7401 linkage-specification:
7402 extern string-literal { declaration-seq [opt] }
7403 extern string-literal declaration */
7404
7405static void
94edc4ab 7406cp_parser_linkage_specification (cp_parser* parser)
a723baf1 7407{
a723baf1
MM
7408 tree linkage;
7409
7410 /* Look for the `extern' keyword. */
7411 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7412
c162c75e
MA
7413 /* Look for the string-literal. */
7414 linkage = cp_parser_string_literal (parser, false, false);
a723baf1
MM
7415
7416 /* Transform the literal into an identifier. If the literal is a
7417 wide-character string, or contains embedded NULs, then we can't
7418 handle it as the user wants. */
c162c75e
MA
7419 if (strlen (TREE_STRING_POINTER (linkage))
7420 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
a723baf1
MM
7421 {
7422 cp_parser_error (parser, "invalid linkage-specification");
7423 /* Assume C++ linkage. */
c162c75e 7424 linkage = lang_name_cplusplus;
a723baf1 7425 }
a723baf1 7426 else
c162c75e 7427 linkage = get_identifier (TREE_STRING_POINTER (linkage));
a723baf1
MM
7428
7429 /* We're now using the new linkage. */
7430 push_lang_context (linkage);
7431
7432 /* If the next token is a `{', then we're using the first
7433 production. */
7434 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7435 {
7436 /* Consume the `{' token. */
7437 cp_lexer_consume_token (parser->lexer);
7438 /* Parse the declarations. */
7439 cp_parser_declaration_seq_opt (parser);
7440 /* Look for the closing `}'. */
7441 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7442 }
7443 /* Otherwise, there's just one declaration. */
7444 else
7445 {
7446 bool saved_in_unbraced_linkage_specification_p;
7447
21526606 7448 saved_in_unbraced_linkage_specification_p
a723baf1
MM
7449 = parser->in_unbraced_linkage_specification_p;
7450 parser->in_unbraced_linkage_specification_p = true;
7451 have_extern_spec = true;
7452 cp_parser_declaration (parser);
7453 have_extern_spec = false;
21526606 7454 parser->in_unbraced_linkage_specification_p
a723baf1
MM
7455 = saved_in_unbraced_linkage_specification_p;
7456 }
7457
7458 /* We're done with the linkage-specification. */
7459 pop_lang_context ();
7460}
7461
7462/* Special member functions [gram.special] */
7463
7464/* Parse a conversion-function-id.
7465
7466 conversion-function-id:
21526606 7467 operator conversion-type-id
a723baf1
MM
7468
7469 Returns an IDENTIFIER_NODE representing the operator. */
7470
21526606 7471static tree
94edc4ab 7472cp_parser_conversion_function_id (cp_parser* parser)
a723baf1
MM
7473{
7474 tree type;
7475 tree saved_scope;
7476 tree saved_qualifying_scope;
7477 tree saved_object_scope;
4514aa8c 7478 tree pushed_scope = NULL_TREE;
a723baf1
MM
7479
7480 /* Look for the `operator' token. */
7481 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7482 return error_mark_node;
7483 /* When we parse the conversion-type-id, the current scope will be
7484 reset. However, we need that information in able to look up the
7485 conversion function later, so we save it here. */
7486 saved_scope = parser->scope;
7487 saved_qualifying_scope = parser->qualifying_scope;
7488 saved_object_scope = parser->object_scope;
7489 /* We must enter the scope of the class so that the names of
7490 entities declared within the class are available in the
7491 conversion-type-id. For example, consider:
7492
21526606 7493 struct S {
a723baf1
MM
7494 typedef int I;
7495 operator I();
7496 };
7497
7498 S::operator I() { ... }
7499
7500 In order to see that `I' is a type-name in the definition, we
7501 must be in the scope of `S'. */
7502 if (saved_scope)
4514aa8c 7503 pushed_scope = push_scope (saved_scope);
a723baf1
MM
7504 /* Parse the conversion-type-id. */
7505 type = cp_parser_conversion_type_id (parser);
7506 /* Leave the scope of the class, if any. */
4514aa8c
NS
7507 if (pushed_scope)
7508 pop_scope (pushed_scope);
a723baf1
MM
7509 /* Restore the saved scope. */
7510 parser->scope = saved_scope;
7511 parser->qualifying_scope = saved_qualifying_scope;
7512 parser->object_scope = saved_object_scope;
7513 /* If the TYPE is invalid, indicate failure. */
7514 if (type == error_mark_node)
7515 return error_mark_node;
7516 return mangle_conv_op_name_for_type (type);
7517}
7518
7519/* Parse a conversion-type-id:
7520
7521 conversion-type-id:
7522 type-specifier-seq conversion-declarator [opt]
7523
7524 Returns the TYPE specified. */
7525
7526static tree
94edc4ab 7527cp_parser_conversion_type_id (cp_parser* parser)
a723baf1
MM
7528{
7529 tree attributes;
62d1db17 7530 cp_decl_specifier_seq type_specifiers;
058b15c1 7531 cp_declarator *declarator;
037cc9c5 7532 tree type_specified;
a723baf1
MM
7533
7534 /* Parse the attributes. */
7535 attributes = cp_parser_attributes_opt (parser);
7536 /* Parse the type-specifiers. */
62d1db17 7537 cp_parser_type_specifier_seq (parser, &type_specifiers);
a723baf1 7538 /* If that didn't work, stop. */
62d1db17 7539 if (type_specifiers.type == error_mark_node)
a723baf1
MM
7540 return error_mark_node;
7541 /* Parse the conversion-declarator. */
7542 declarator = cp_parser_conversion_declarator_opt (parser);
7543
037cc9c5
FJ
7544 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7545 /*initialized=*/0, &attributes);
7546 if (attributes)
7547 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7548 return type_specified;
a723baf1
MM
7549}
7550
7551/* Parse an (optional) conversion-declarator.
7552
7553 conversion-declarator:
21526606 7554 ptr-operator conversion-declarator [opt]
a723baf1 7555
058b15c1 7556 */
a723baf1 7557
058b15c1 7558static cp_declarator *
94edc4ab 7559cp_parser_conversion_declarator_opt (cp_parser* parser)
a723baf1
MM
7560{
7561 enum tree_code code;
7562 tree class_type;
3c01e5df 7563 cp_cv_quals cv_quals;
a723baf1
MM
7564
7565 /* We don't know if there's a ptr-operator next, or not. */
7566 cp_parser_parse_tentatively (parser);
7567 /* Try the ptr-operator. */
3c01e5df 7568 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
a723baf1
MM
7569 /* If it worked, look for more conversion-declarators. */
7570 if (cp_parser_parse_definitely (parser))
7571 {
058b15c1 7572 cp_declarator *declarator;
98ca843c 7573
058b15c1
MM
7574 /* Parse another optional declarator. */
7575 declarator = cp_parser_conversion_declarator_opt (parser);
98ca843c 7576
058b15c1
MM
7577 /* Create the representation of the declarator. */
7578 if (class_type)
3c01e5df 7579 declarator = make_ptrmem_declarator (cv_quals, class_type,
a723baf1 7580 declarator);
058b15c1 7581 else if (code == INDIRECT_REF)
3c01e5df 7582 declarator = make_pointer_declarator (cv_quals, declarator);
058b15c1 7583 else
3c01e5df 7584 declarator = make_reference_declarator (cv_quals, declarator);
98ca843c 7585
058b15c1 7586 return declarator;
a723baf1
MM
7587 }
7588
058b15c1 7589 return NULL;
a723baf1
MM
7590}
7591
7592/* Parse an (optional) ctor-initializer.
7593
7594 ctor-initializer:
21526606 7595 : mem-initializer-list
a723baf1
MM
7596
7597 Returns TRUE iff the ctor-initializer was actually present. */
7598
7599static bool
94edc4ab 7600cp_parser_ctor_initializer_opt (cp_parser* parser)
a723baf1
MM
7601{
7602 /* If the next token is not a `:', then there is no
7603 ctor-initializer. */
7604 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7605 {
7606 /* Do default initialization of any bases and members. */
7607 if (DECL_CONSTRUCTOR_P (current_function_decl))
7608 finish_mem_initializers (NULL_TREE);
7609
7610 return false;
7611 }
7612
7613 /* Consume the `:' token. */
7614 cp_lexer_consume_token (parser->lexer);
7615 /* And the mem-initializer-list. */
7616 cp_parser_mem_initializer_list (parser);
7617
7618 return true;
7619}
7620
7621/* Parse a mem-initializer-list.
7622
7623 mem-initializer-list:
7624 mem-initializer
7625 mem-initializer , mem-initializer-list */
7626
7627static void
94edc4ab 7628cp_parser_mem_initializer_list (cp_parser* parser)
a723baf1
MM
7629{
7630 tree mem_initializer_list = NULL_TREE;
7631
7632 /* Let the semantic analysis code know that we are starting the
7633 mem-initializer-list. */
0e136342
MM
7634 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7635 error ("only constructors take base initializers");
a723baf1
MM
7636
7637 /* Loop through the list. */
7638 while (true)
7639 {
7640 tree mem_initializer;
7641
7642 /* Parse the mem-initializer. */
7643 mem_initializer = cp_parser_mem_initializer (parser);
7644 /* Add it to the list, unless it was erroneous. */
7645 if (mem_initializer)
7646 {
7647 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7648 mem_initializer_list = mem_initializer;
7649 }
7650 /* If the next token is not a `,', we're done. */
7651 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7652 break;
7653 /* Consume the `,' token. */
7654 cp_lexer_consume_token (parser->lexer);
7655 }
7656
7657 /* Perform semantic analysis. */
0e136342
MM
7658 if (DECL_CONSTRUCTOR_P (current_function_decl))
7659 finish_mem_initializers (mem_initializer_list);
a723baf1
MM
7660}
7661
7662/* Parse a mem-initializer.
7663
7664 mem-initializer:
21526606 7665 mem-initializer-id ( expression-list [opt] )
a723baf1
MM
7666
7667 GNU extension:
21526606 7668
a723baf1 7669 mem-initializer:
34cd5ae7 7670 ( expression-list [opt] )
a723baf1
MM
7671
7672 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7673 class) or FIELD_DECL (for a non-static data member) to initialize;
7674 the TREE_VALUE is the expression-list. */
7675
7676static tree
94edc4ab 7677cp_parser_mem_initializer (cp_parser* parser)
a723baf1
MM
7678{
7679 tree mem_initializer_id;
7680 tree expression_list;
1f5a253a 7681 tree member;
21526606 7682
a723baf1
MM
7683 /* Find out what is being initialized. */
7684 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7685 {
7686 pedwarn ("anachronistic old-style base class initializer");
7687 mem_initializer_id = NULL_TREE;
7688 }
7689 else
7690 mem_initializer_id = cp_parser_mem_initializer_id (parser);
1f5a253a
NS
7691 member = expand_member_init (mem_initializer_id);
7692 if (member && !DECL_P (member))
7693 in_base_initializer = 1;
7efa3e22 7694
21526606 7695 expression_list
39703eb9 7696 = cp_parser_parenthesized_expression_list (parser, false,
93678513 7697 /*cast_p=*/false,
39703eb9 7698 /*non_constant_p=*/NULL);
7efa3e22 7699 if (!expression_list)
a723baf1 7700 expression_list = void_type_node;
a723baf1 7701
1f5a253a 7702 in_base_initializer = 0;
21526606 7703
1f5a253a 7704 return member ? build_tree_list (member, expression_list) : NULL_TREE;
a723baf1
MM
7705}
7706
7707/* Parse a mem-initializer-id.
7708
7709 mem-initializer-id:
7710 :: [opt] nested-name-specifier [opt] class-name
21526606 7711 identifier
a723baf1
MM
7712
7713 Returns a TYPE indicating the class to be initializer for the first
7714 production. Returns an IDENTIFIER_NODE indicating the data member
7715 to be initialized for the second production. */
7716
7717static tree
94edc4ab 7718cp_parser_mem_initializer_id (cp_parser* parser)
a723baf1
MM
7719{
7720 bool global_scope_p;
7721 bool nested_name_specifier_p;
8a83a693 7722 bool template_p = false;
a723baf1
MM
7723 tree id;
7724
8a83a693
GB
7725 /* `typename' is not allowed in this context ([temp.res]). */
7726 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7727 {
2a13a625 7728 error ("keyword %<typename%> not allowed in this context (a qualified "
8a83a693
GB
7729 "member initializer is implicitly a type)");
7730 cp_lexer_consume_token (parser->lexer);
7731 }
a723baf1 7732 /* Look for the optional `::' operator. */
21526606
EC
7733 global_scope_p
7734 = (cp_parser_global_scope_opt (parser,
7735 /*current_scope_valid_p=*/false)
a723baf1
MM
7736 != NULL_TREE);
7737 /* Look for the optional nested-name-specifier. The simplest way to
7738 implement:
7739
7740 [temp.res]
7741
7742 The keyword `typename' is not permitted in a base-specifier or
7743 mem-initializer; in these contexts a qualified name that
7744 depends on a template-parameter is implicitly assumed to be a
7745 type name.
7746
7747 is to assume that we have seen the `typename' keyword at this
7748 point. */
21526606 7749 nested_name_specifier_p
a723baf1
MM
7750 = (cp_parser_nested_name_specifier_opt (parser,
7751 /*typename_keyword_p=*/true,
7752 /*check_dependency_p=*/true,
a668c6ad
MM
7753 /*type_p=*/true,
7754 /*is_declaration=*/true)
a723baf1 7755 != NULL_TREE);
8a83a693
GB
7756 if (nested_name_specifier_p)
7757 template_p = cp_parser_optional_template_keyword (parser);
a723baf1
MM
7758 /* If there is a `::' operator or a nested-name-specifier, then we
7759 are definitely looking for a class-name. */
7760 if (global_scope_p || nested_name_specifier_p)
7761 return cp_parser_class_name (parser,
7762 /*typename_keyword_p=*/true,
8a83a693 7763 /*template_keyword_p=*/template_p,
fc6a28d7 7764 none_type,
a723baf1 7765 /*check_dependency_p=*/true,
a668c6ad
MM
7766 /*class_head_p=*/false,
7767 /*is_declaration=*/true);
a723baf1
MM
7768 /* Otherwise, we could also be looking for an ordinary identifier. */
7769 cp_parser_parse_tentatively (parser);
7770 /* Try a class-name. */
21526606 7771 id = cp_parser_class_name (parser,
a723baf1
MM
7772 /*typename_keyword_p=*/true,
7773 /*template_keyword_p=*/false,
fc6a28d7 7774 none_type,
a723baf1 7775 /*check_dependency_p=*/true,
a668c6ad
MM
7776 /*class_head_p=*/false,
7777 /*is_declaration=*/true);
a723baf1
MM
7778 /* If we found one, we're done. */
7779 if (cp_parser_parse_definitely (parser))
7780 return id;
7781 /* Otherwise, look for an ordinary identifier. */
7782 return cp_parser_identifier (parser);
7783}
7784
7785/* Overloading [gram.over] */
7786
7787/* Parse an operator-function-id.
7788
7789 operator-function-id:
21526606 7790 operator operator
a723baf1
MM
7791
7792 Returns an IDENTIFIER_NODE for the operator which is a
7793 human-readable spelling of the identifier, e.g., `operator +'. */
7794
21526606 7795static tree
94edc4ab 7796cp_parser_operator_function_id (cp_parser* parser)
a723baf1
MM
7797{
7798 /* Look for the `operator' keyword. */
7799 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7800 return error_mark_node;
7801 /* And then the name of the operator itself. */
7802 return cp_parser_operator (parser);
7803}
7804
7805/* Parse an operator.
7806
7807 operator:
7808 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7809 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7810 || ++ -- , ->* -> () []
7811
7812 GNU Extensions:
21526606 7813
a723baf1
MM
7814 operator:
7815 <? >? <?= >?=
7816
7817 Returns an IDENTIFIER_NODE for the operator which is a
7818 human-readable spelling of the identifier, e.g., `operator +'. */
21526606 7819
a723baf1 7820static tree
94edc4ab 7821cp_parser_operator (cp_parser* parser)
a723baf1
MM
7822{
7823 tree id = NULL_TREE;
7824 cp_token *token;
7825
7826 /* Peek at the next token. */
7827 token = cp_lexer_peek_token (parser->lexer);
7828 /* Figure out which operator we have. */
7829 switch (token->type)
7830 {
7831 case CPP_KEYWORD:
7832 {
7833 enum tree_code op;
7834
7835 /* The keyword should be either `new' or `delete'. */
7836 if (token->keyword == RID_NEW)
7837 op = NEW_EXPR;
7838 else if (token->keyword == RID_DELETE)
7839 op = DELETE_EXPR;
7840 else
7841 break;
7842
7843 /* Consume the `new' or `delete' token. */
7844 cp_lexer_consume_token (parser->lexer);
7845
7846 /* Peek at the next token. */
7847 token = cp_lexer_peek_token (parser->lexer);
7848 /* If it's a `[' token then this is the array variant of the
7849 operator. */
7850 if (token->type == CPP_OPEN_SQUARE)
7851 {
7852 /* Consume the `[' token. */
7853 cp_lexer_consume_token (parser->lexer);
7854 /* Look for the `]' token. */
7855 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
21526606 7856 id = ansi_opname (op == NEW_EXPR
a723baf1
MM
7857 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7858 }
7859 /* Otherwise, we have the non-array variant. */
7860 else
7861 id = ansi_opname (op);
7862
7863 return id;
7864 }
7865
7866 case CPP_PLUS:
7867 id = ansi_opname (PLUS_EXPR);
7868 break;
7869
7870 case CPP_MINUS:
7871 id = ansi_opname (MINUS_EXPR);
7872 break;
7873
7874 case CPP_MULT:
7875 id = ansi_opname (MULT_EXPR);
7876 break;
7877
7878 case CPP_DIV:
7879 id = ansi_opname (TRUNC_DIV_EXPR);
7880 break;
7881
7882 case CPP_MOD:
7883 id = ansi_opname (TRUNC_MOD_EXPR);
7884 break;
7885
7886 case CPP_XOR:
7887 id = ansi_opname (BIT_XOR_EXPR);
7888 break;
7889
7890 case CPP_AND:
7891 id = ansi_opname (BIT_AND_EXPR);
7892 break;
7893
7894 case CPP_OR:
7895 id = ansi_opname (BIT_IOR_EXPR);
7896 break;
7897
7898 case CPP_COMPL:
7899 id = ansi_opname (BIT_NOT_EXPR);
7900 break;
21526606 7901
a723baf1
MM
7902 case CPP_NOT:
7903 id = ansi_opname (TRUTH_NOT_EXPR);
7904 break;
7905
7906 case CPP_EQ:
7907 id = ansi_assopname (NOP_EXPR);
7908 break;
7909
7910 case CPP_LESS:
7911 id = ansi_opname (LT_EXPR);
7912 break;
7913
7914 case CPP_GREATER:
7915 id = ansi_opname (GT_EXPR);
7916 break;
7917
7918 case CPP_PLUS_EQ:
7919 id = ansi_assopname (PLUS_EXPR);
7920 break;
7921
7922 case CPP_MINUS_EQ:
7923 id = ansi_assopname (MINUS_EXPR);
7924 break;
7925
7926 case CPP_MULT_EQ:
7927 id = ansi_assopname (MULT_EXPR);
7928 break;
7929
7930 case CPP_DIV_EQ:
7931 id = ansi_assopname (TRUNC_DIV_EXPR);
7932 break;
7933
7934 case CPP_MOD_EQ:
7935 id = ansi_assopname (TRUNC_MOD_EXPR);
7936 break;
7937
7938 case CPP_XOR_EQ:
7939 id = ansi_assopname (BIT_XOR_EXPR);
7940 break;
7941
7942 case CPP_AND_EQ:
7943 id = ansi_assopname (BIT_AND_EXPR);
7944 break;
7945
7946 case CPP_OR_EQ:
7947 id = ansi_assopname (BIT_IOR_EXPR);
7948 break;
7949
7950 case CPP_LSHIFT:
7951 id = ansi_opname (LSHIFT_EXPR);
7952 break;
7953
7954 case CPP_RSHIFT:
7955 id = ansi_opname (RSHIFT_EXPR);
7956 break;
7957
7958 case CPP_LSHIFT_EQ:
7959 id = ansi_assopname (LSHIFT_EXPR);
7960 break;
7961
7962 case CPP_RSHIFT_EQ:
7963 id = ansi_assopname (RSHIFT_EXPR);
7964 break;
7965
7966 case CPP_EQ_EQ:
7967 id = ansi_opname (EQ_EXPR);
7968 break;
7969
7970 case CPP_NOT_EQ:
7971 id = ansi_opname (NE_EXPR);
7972 break;
7973
7974 case CPP_LESS_EQ:
7975 id = ansi_opname (LE_EXPR);
7976 break;
7977
7978 case CPP_GREATER_EQ:
7979 id = ansi_opname (GE_EXPR);
7980 break;
7981
7982 case CPP_AND_AND:
7983 id = ansi_opname (TRUTH_ANDIF_EXPR);
7984 break;
7985
7986 case CPP_OR_OR:
7987 id = ansi_opname (TRUTH_ORIF_EXPR);
7988 break;
21526606 7989
a723baf1
MM
7990 case CPP_PLUS_PLUS:
7991 id = ansi_opname (POSTINCREMENT_EXPR);
7992 break;
7993
7994 case CPP_MINUS_MINUS:
7995 id = ansi_opname (PREDECREMENT_EXPR);
7996 break;
7997
7998 case CPP_COMMA:
7999 id = ansi_opname (COMPOUND_EXPR);
8000 break;
8001
8002 case CPP_DEREF_STAR:
8003 id = ansi_opname (MEMBER_REF);
8004 break;
8005
8006 case CPP_DEREF:
8007 id = ansi_opname (COMPONENT_REF);
8008 break;
8009
8010 case CPP_OPEN_PAREN:
8011 /* Consume the `('. */
8012 cp_lexer_consume_token (parser->lexer);
8013 /* Look for the matching `)'. */
8014 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8015 return ansi_opname (CALL_EXPR);
8016
8017 case CPP_OPEN_SQUARE:
8018 /* Consume the `['. */
8019 cp_lexer_consume_token (parser->lexer);
8020 /* Look for the matching `]'. */
8021 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8022 return ansi_opname (ARRAY_REF);
8023
8024 /* Extensions. */
8025 case CPP_MIN:
8026 id = ansi_opname (MIN_EXPR);
8027 break;
8028
8029 case CPP_MAX:
8030 id = ansi_opname (MAX_EXPR);
8031 break;
8032
8033 case CPP_MIN_EQ:
8034 id = ansi_assopname (MIN_EXPR);
8035 break;
8036
8037 case CPP_MAX_EQ:
8038 id = ansi_assopname (MAX_EXPR);
8039 break;
8040
8041 default:
8042 /* Anything else is an error. */
8043 break;
8044 }
8045
8046 /* If we have selected an identifier, we need to consume the
8047 operator token. */
8048 if (id)
8049 cp_lexer_consume_token (parser->lexer);
8050 /* Otherwise, no valid operator name was present. */
8051 else
8052 {
8053 cp_parser_error (parser, "expected operator");
8054 id = error_mark_node;
8055 }
8056
8057 return id;
8058}
8059
8060/* Parse a template-declaration.
8061
8062 template-declaration:
21526606 8063 export [opt] template < template-parameter-list > declaration
a723baf1
MM
8064
8065 If MEMBER_P is TRUE, this template-declaration occurs within a
21526606 8066 class-specifier.
a723baf1
MM
8067
8068 The grammar rule given by the standard isn't correct. What
8069 is really meant is:
8070
8071 template-declaration:
21526606 8072 export [opt] template-parameter-list-seq
a723baf1 8073 decl-specifier-seq [opt] init-declarator [opt] ;
21526606 8074 export [opt] template-parameter-list-seq
a723baf1
MM
8075 function-definition
8076
8077 template-parameter-list-seq:
8078 template-parameter-list-seq [opt]
8079 template < template-parameter-list > */
8080
8081static void
94edc4ab 8082cp_parser_template_declaration (cp_parser* parser, bool member_p)
a723baf1
MM
8083{
8084 /* Check for `export'. */
8085 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8086 {
8087 /* Consume the `export' token. */
8088 cp_lexer_consume_token (parser->lexer);
8089 /* Warn that we do not support `export'. */
2a13a625 8090 warning ("keyword %<export%> not implemented, and will be ignored");
a723baf1
MM
8091 }
8092
8093 cp_parser_template_declaration_after_export (parser, member_p);
8094}
8095
8096/* Parse a template-parameter-list.
8097
8098 template-parameter-list:
8099 template-parameter
8100 template-parameter-list , template-parameter
8101
8102 Returns a TREE_LIST. Each node represents a template parameter.
8103 The nodes are connected via their TREE_CHAINs. */
8104
8105static tree
94edc4ab 8106cp_parser_template_parameter_list (cp_parser* parser)
a723baf1
MM
8107{
8108 tree parameter_list = NULL_TREE;
8109
8110 while (true)
8111 {
8112 tree parameter;
8113 cp_token *token;
058b15c1 8114 bool is_non_type;
a723baf1
MM
8115
8116 /* Parse the template-parameter. */
058b15c1 8117 parameter = cp_parser_template_parameter (parser, &is_non_type);
a723baf1 8118 /* Add it to the list. */
943e3ede
MM
8119 if (parameter != error_mark_node)
8120 parameter_list = process_template_parm (parameter_list,
8121 parameter,
8122 is_non_type);
a723baf1
MM
8123 /* Peek at the next token. */
8124 token = cp_lexer_peek_token (parser->lexer);
8125 /* If it's not a `,', we're done. */
8126 if (token->type != CPP_COMMA)
8127 break;
8128 /* Otherwise, consume the `,' token. */
8129 cp_lexer_consume_token (parser->lexer);
8130 }
8131
8132 return parameter_list;
8133}
8134
8135/* Parse a template-parameter.
8136
8137 template-parameter:
8138 type-parameter
8139 parameter-declaration
8140
943e3ede
MM
8141 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8142 the parameter. The TREE_PURPOSE is the default value, if any.
8143 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8144 iff this parameter is a non-type parameter. */
a723baf1
MM
8145
8146static tree
058b15c1 8147cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
a723baf1
MM
8148{
8149 cp_token *token;
62d1db17 8150 cp_parameter_declarator *parameter_declarator;
943e3ede 8151 tree parm;
a723baf1 8152
058b15c1
MM
8153 /* Assume it is a type parameter or a template parameter. */
8154 *is_non_type = false;
a723baf1
MM
8155 /* Peek at the next token. */
8156 token = cp_lexer_peek_token (parser->lexer);
8157 /* If it is `class' or `template', we have a type-parameter. */
8158 if (token->keyword == RID_TEMPLATE)
8159 return cp_parser_type_parameter (parser);
8160 /* If it is `class' or `typename' we do not know yet whether it is a
8161 type parameter or a non-type parameter. Consider:
8162
8163 template <typename T, typename T::X X> ...
8164
8165 or:
21526606 8166
a723baf1
MM
8167 template <class C, class D*> ...
8168
8169 Here, the first parameter is a type parameter, and the second is
8170 a non-type parameter. We can tell by looking at the token after
8171 the identifier -- if it is a `,', `=', or `>' then we have a type
8172 parameter. */
8173 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8174 {
8175 /* Peek at the token after `class' or `typename'. */
8176 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8177 /* If it's an identifier, skip it. */
8178 if (token->type == CPP_NAME)
8179 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8180 /* Now, see if the token looks like the end of a template
8181 parameter. */
21526606 8182 if (token->type == CPP_COMMA
a723baf1
MM
8183 || token->type == CPP_EQ
8184 || token->type == CPP_GREATER)
8185 return cp_parser_type_parameter (parser);
8186 }
8187
21526606 8188 /* Otherwise, it is a non-type parameter.
a723baf1
MM
8189
8190 [temp.param]
8191
8192 When parsing a default template-argument for a non-type
8193 template-parameter, the first non-nested `>' is taken as the end
8194 of the template parameter-list rather than a greater-than
8195 operator. */
058b15c1
MM
8196 *is_non_type = true;
8197 parameter_declarator
8198 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8199 /*parenthesized_p=*/NULL);
943e3ede
MM
8200 parm = grokdeclarator (parameter_declarator->declarator,
8201 &parameter_declarator->decl_specifiers,
8202 PARM, /*initialized=*/0,
8203 /*attrlist=*/NULL);
8204 if (parm == error_mark_node)
8205 return error_mark_node;
8206 return build_tree_list (parameter_declarator->default_argument, parm);
a723baf1
MM
8207}
8208
8209/* Parse a type-parameter.
8210
8211 type-parameter:
8212 class identifier [opt]
8213 class identifier [opt] = type-id
8214 typename identifier [opt]
8215 typename identifier [opt] = type-id
8216 template < template-parameter-list > class identifier [opt]
21526606
EC
8217 template < template-parameter-list > class identifier [opt]
8218 = id-expression
a723baf1
MM
8219
8220 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8221 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8222 the declaration of the parameter. */
8223
8224static tree
94edc4ab 8225cp_parser_type_parameter (cp_parser* parser)
a723baf1
MM
8226{
8227 cp_token *token;
8228 tree parameter;
8229
8230 /* Look for a keyword to tell us what kind of parameter this is. */
21526606 8231 token = cp_parser_require (parser, CPP_KEYWORD,
8a6393df 8232 "`class', `typename', or `template'");
a723baf1
MM
8233 if (!token)
8234 return error_mark_node;
8235
8236 switch (token->keyword)
8237 {
8238 case RID_CLASS:
8239 case RID_TYPENAME:
8240 {
8241 tree identifier;
8242 tree default_argument;
8243
8244 /* If the next token is an identifier, then it names the
8245 parameter. */
8246 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8247 identifier = cp_parser_identifier (parser);
8248 else
8249 identifier = NULL_TREE;
8250
8251 /* Create the parameter. */
8252 parameter = finish_template_type_parm (class_type_node, identifier);
8253
8254 /* If the next token is an `=', we have a default argument. */
8255 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8256 {
8257 /* Consume the `=' token. */
8258 cp_lexer_consume_token (parser->lexer);
34cd5ae7 8259 /* Parse the default-argument. */
a723baf1
MM
8260 default_argument = cp_parser_type_id (parser);
8261 }
8262 else
8263 default_argument = NULL_TREE;
8264
8265 /* Create the combined representation of the parameter and the
8266 default argument. */
c67d36d0 8267 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
8268 }
8269 break;
8270
8271 case RID_TEMPLATE:
8272 {
8273 tree parameter_list;
8274 tree identifier;
8275 tree default_argument;
8276
8277 /* Look for the `<'. */
8278 cp_parser_require (parser, CPP_LESS, "`<'");
8279 /* Parse the template-parameter-list. */
8280 begin_template_parm_list ();
21526606 8281 parameter_list
a723baf1
MM
8282 = cp_parser_template_parameter_list (parser);
8283 parameter_list = end_template_parm_list (parameter_list);
8284 /* Look for the `>'. */
8285 cp_parser_require (parser, CPP_GREATER, "`>'");
8286 /* Look for the `class' keyword. */
8287 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8288 /* If the next token is an `=', then there is a
8289 default-argument. If the next token is a `>', we are at
8290 the end of the parameter-list. If the next token is a `,',
8291 then we are at the end of this parameter. */
8292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8293 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8294 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
71bd7186
MM
8295 {
8296 identifier = cp_parser_identifier (parser);
03fd3f84 8297 /* Treat invalid names as if the parameter were nameless. */
71bd7186
MM
8298 if (identifier == error_mark_node)
8299 identifier = NULL_TREE;
8300 }
a723baf1
MM
8301 else
8302 identifier = NULL_TREE;
71bd7186 8303
a723baf1
MM
8304 /* Create the template parameter. */
8305 parameter = finish_template_template_parm (class_type_node,
8306 identifier);
21526606 8307
a723baf1
MM
8308 /* If the next token is an `=', then there is a
8309 default-argument. */
8310 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8311 {
b0bc6e8e
KL
8312 bool is_template;
8313
a723baf1
MM
8314 /* Consume the `='. */
8315 cp_lexer_consume_token (parser->lexer);
8316 /* Parse the id-expression. */
21526606 8317 default_argument
a723baf1
MM
8318 = cp_parser_id_expression (parser,
8319 /*template_keyword_p=*/false,
8320 /*check_dependency_p=*/true,
b0bc6e8e 8321 /*template_p=*/&is_template,
f3c2dfc6 8322 /*declarator_p=*/false);
a3a503a5
GB
8323 if (TREE_CODE (default_argument) == TYPE_DECL)
8324 /* If the id-expression was a template-id that refers to
8325 a template-class, we already have the declaration here,
8326 so no further lookup is needed. */
8327 ;
8328 else
8329 /* Look up the name. */
21526606 8330 default_argument
a3a503a5 8331 = cp_parser_lookup_name (parser, default_argument,
fc6a28d7
MM
8332 none_type,
8333 /*is_template=*/is_template,
8334 /*is_namespace=*/false,
8335 /*check_dependency=*/true,
8336 /*ambiguous_p=*/NULL);
a723baf1
MM
8337 /* See if the default argument is valid. */
8338 default_argument
8339 = check_template_template_default_arg (default_argument);
8340 }
8341 else
8342 default_argument = NULL_TREE;
8343
8344 /* Create the combined representation of the parameter and the
8345 default argument. */
71bd7186 8346 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
8347 }
8348 break;
8349
8350 default:
71bd7186
MM
8351 gcc_unreachable ();
8352 break;
a723baf1 8353 }
21526606 8354
a723baf1
MM
8355 return parameter;
8356}
8357
8358/* Parse a template-id.
8359
8360 template-id:
8361 template-name < template-argument-list [opt] >
8362
8363 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8364 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8365 returned. Otherwise, if the template-name names a function, or set
8366 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
21526606 8367 names a class, returns a TYPE_DECL for the specialization.
a723baf1
MM
8368
8369 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8370 uninstantiated templates. */
8371
8372static tree
21526606
EC
8373cp_parser_template_id (cp_parser *parser,
8374 bool template_keyword_p,
a668c6ad
MM
8375 bool check_dependency_p,
8376 bool is_declaration)
a723baf1
MM
8377{
8378 tree template;
8379 tree arguments;
a723baf1 8380 tree template_id;
0c5e4866 8381 cp_token_position start_of_id = 0;
a723baf1 8382 tree access_check = NULL_TREE;
f4abade9 8383 cp_token *next_token, *next_token_2;
a668c6ad 8384 bool is_identifier;
a723baf1
MM
8385
8386 /* If the next token corresponds to a template-id, there is no need
8387 to reparse it. */
2050a1bb
MM
8388 next_token = cp_lexer_peek_token (parser->lexer);
8389 if (next_token->type == CPP_TEMPLATE_ID)
a723baf1
MM
8390 {
8391 tree value;
8392 tree check;
8393
8394 /* Get the stored value. */
8395 value = cp_lexer_consume_token (parser->lexer)->value;
8396 /* Perform any access checks that were deferred. */
8397 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c
KL
8398 perform_or_defer_access_check (TREE_PURPOSE (check),
8399 TREE_VALUE (check));
a723baf1
MM
8400 /* Return the stored value. */
8401 return TREE_VALUE (value);
8402 }
8403
2050a1bb
MM
8404 /* Avoid performing name lookup if there is no possibility of
8405 finding a template-id. */
8406 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8407 || (next_token->type == CPP_NAME
21526606 8408 && !cp_parser_nth_token_starts_template_argument_list_p
f4abade9 8409 (parser, 2)))
2050a1bb
MM
8410 {
8411 cp_parser_error (parser, "expected template-id");
8412 return error_mark_node;
8413 }
8414
a723baf1 8415 /* Remember where the template-id starts. */
0b16f8f4 8416 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
0c5e4866 8417 start_of_id = cp_lexer_token_position (parser->lexer, false);
a723baf1 8418
8d241e0b 8419 push_deferring_access_checks (dk_deferred);
cf22909c 8420
a723baf1 8421 /* Parse the template-name. */
a668c6ad 8422 is_identifier = false;
a723baf1 8423 template = cp_parser_template_name (parser, template_keyword_p,
a668c6ad
MM
8424 check_dependency_p,
8425 is_declaration,
8426 &is_identifier);
8427 if (template == error_mark_node || is_identifier)
cf22909c
KL
8428 {
8429 pop_deferring_access_checks ();
a668c6ad 8430 return template;
cf22909c 8431 }
a723baf1 8432
21526606 8433 /* If we find the sequence `[:' after a template-name, it's probably
f4abade9
GB
8434 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8435 parse correctly the argument list. */
2cfe82fe 8436 next_token = cp_lexer_peek_token (parser->lexer);
f4abade9 8437 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
21526606 8438 if (next_token->type == CPP_OPEN_SQUARE
f4abade9 8439 && next_token->flags & DIGRAPH
21526606 8440 && next_token_2->type == CPP_COLON
f4abade9 8441 && !(next_token_2->flags & PREV_WHITE))
cf22909c 8442 {
f4abade9
GB
8443 cp_parser_parse_tentatively (parser);
8444 /* Change `:' into `::'. */
8445 next_token_2->type = CPP_SCOPE;
8446 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8447 CPP_LESS. */
8448 cp_lexer_consume_token (parser->lexer);
8449 /* Parse the arguments. */
8450 arguments = cp_parser_enclosed_template_argument_list (parser);
8451 if (!cp_parser_parse_definitely (parser))
8452 {
8453 /* If we couldn't parse an argument list, then we revert our changes
8454 and return simply an error. Maybe this is not a template-id
8455 after all. */
8456 next_token_2->type = CPP_COLON;
2a13a625 8457 cp_parser_error (parser, "expected %<<%>");
f4abade9
GB
8458 pop_deferring_access_checks ();
8459 return error_mark_node;
8460 }
8461 /* Otherwise, emit an error about the invalid digraph, but continue
8462 parsing because we got our argument list. */
2a13a625
GDR
8463 pedwarn ("%<<::%> cannot begin a template-argument list");
8464 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8465 "between %<<%> and %<::%>");
f4abade9
GB
8466 if (!flag_permissive)
8467 {
8468 static bool hint;
8469 if (!hint)
8470 {
2a13a625 8471 inform ("(if you use -fpermissive G++ will accept your code)");
f4abade9
GB
8472 hint = true;
8473 }
8474 }
8475 }
8476 else
8477 {
8478 /* Look for the `<' that starts the template-argument-list. */
8479 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8480 {
8481 pop_deferring_access_checks ();
8482 return error_mark_node;
8483 }
8484 /* Parse the arguments. */
8485 arguments = cp_parser_enclosed_template_argument_list (parser);
cf22909c 8486 }
a723baf1
MM
8487
8488 /* Build a representation of the specialization. */
8489 if (TREE_CODE (template) == IDENTIFIER_NODE)
8490 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8491 else if (DECL_CLASS_TEMPLATE_P (template)
8492 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
21526606
EC
8493 template_id
8494 = finish_template_type (template, arguments,
8495 cp_lexer_next_token_is (parser->lexer,
a723baf1
MM
8496 CPP_SCOPE));
8497 else
8498 {
8499 /* If it's not a class-template or a template-template, it should be
8500 a function-template. */
50bc768d
NS
8501 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8502 || TREE_CODE (template) == OVERLOAD
8503 || BASELINK_P (template)));
21526606 8504
a723baf1
MM
8505 template_id = lookup_template_function (template, arguments);
8506 }
21526606 8507
cf22909c
KL
8508 /* Retrieve any deferred checks. Do not pop this access checks yet
8509 so the memory will not be reclaimed during token replacing below. */
8510 access_check = get_deferred_access_checks ();
8511
a723baf1
MM
8512 /* If parsing tentatively, replace the sequence of tokens that makes
8513 up the template-id with a CPP_TEMPLATE_ID token. That way,
8514 should we re-parse the token stream, we will not have to repeat
8515 the effort required to do the parse, nor will we issue duplicate
8516 error messages about problems during instantiation of the
e894ab29 8517 template. */
c8a7ed43 8518 if (start_of_id)
a723baf1 8519 {
0c5e4866
NS
8520 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8521
a723baf1
MM
8522 /* Reset the contents of the START_OF_ID token. */
8523 token->type = CPP_TEMPLATE_ID;
8524 token->value = build_tree_list (access_check, template_id);
8525 token->keyword = RID_MAX;
0c5e4866 8526
a723baf1 8527 /* Purge all subsequent tokens. */
0c5e4866 8528 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
c8a7ed43
AO
8529
8530 /* ??? Can we actually assume that, if template_id ==
8531 error_mark_node, we will have issued a diagnostic to the
8532 user, as opposed to simply marking the tentative parse as
8533 failed? */
8534 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8535 error ("parse error in template argument list");
a723baf1
MM
8536 }
8537
cf22909c 8538 pop_deferring_access_checks ();
a723baf1
MM
8539 return template_id;
8540}
8541
8542/* Parse a template-name.
8543
8544 template-name:
8545 identifier
21526606 8546
a723baf1
MM
8547 The standard should actually say:
8548
8549 template-name:
8550 identifier
8551 operator-function-id
a723baf1
MM
8552
8553 A defect report has been filed about this issue.
8554
0d956474
GB
8555 A conversion-function-id cannot be a template name because they cannot
8556 be part of a template-id. In fact, looking at this code:
8557
8558 a.operator K<int>()
8559
8560 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
21526606 8561 It is impossible to call a templated conversion-function-id with an
0d956474
GB
8562 explicit argument list, since the only allowed template parameter is
8563 the type to which it is converting.
8564
a723baf1
MM
8565 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8566 `template' keyword, in a construction like:
8567
8568 T::template f<3>()
8569
8570 In that case `f' is taken to be a template-name, even though there
8571 is no way of knowing for sure.
8572
8573 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8574 name refers to a set of overloaded functions, at least one of which
8575 is a template, or an IDENTIFIER_NODE with the name of the template,
8576 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8577 names are looked up inside uninstantiated templates. */
8578
8579static tree
21526606
EC
8580cp_parser_template_name (cp_parser* parser,
8581 bool template_keyword_p,
a668c6ad
MM
8582 bool check_dependency_p,
8583 bool is_declaration,
8584 bool *is_identifier)
a723baf1
MM
8585{
8586 tree identifier;
8587 tree decl;
8588 tree fns;
8589
8590 /* If the next token is `operator', then we have either an
8591 operator-function-id or a conversion-function-id. */
8592 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8593 {
8594 /* We don't know whether we're looking at an
8595 operator-function-id or a conversion-function-id. */
8596 cp_parser_parse_tentatively (parser);
8597 /* Try an operator-function-id. */
8598 identifier = cp_parser_operator_function_id (parser);
8599 /* If that didn't work, try a conversion-function-id. */
8600 if (!cp_parser_parse_definitely (parser))
0d956474
GB
8601 {
8602 cp_parser_error (parser, "expected template-name");
8603 return error_mark_node;
8604 }
a723baf1
MM
8605 }
8606 /* Look for the identifier. */
8607 else
8608 identifier = cp_parser_identifier (parser);
21526606 8609
a723baf1
MM
8610 /* If we didn't find an identifier, we don't have a template-id. */
8611 if (identifier == error_mark_node)
8612 return error_mark_node;
8613
8614 /* If the name immediately followed the `template' keyword, then it
8615 is a template-name. However, if the next token is not `<', then
8616 we do not treat it as a template-name, since it is not being used
8617 as part of a template-id. This enables us to handle constructs
8618 like:
8619
8620 template <typename T> struct S { S(); };
8621 template <typename T> S<T>::S();
8622
8623 correctly. We would treat `S' as a template -- if it were `S<T>'
8624 -- but we do not if there is no `<'. */
a668c6ad
MM
8625
8626 if (processing_template_decl
f4abade9 8627 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
a668c6ad
MM
8628 {
8629 /* In a declaration, in a dependent context, we pretend that the
8630 "template" keyword was present in order to improve error
8631 recovery. For example, given:
21526606 8632
a668c6ad 8633 template <typename T> void f(T::X<int>);
21526606 8634
a668c6ad 8635 we want to treat "X<int>" as a template-id. */
21526606
EC
8636 if (is_declaration
8637 && !template_keyword_p
a668c6ad 8638 && parser->scope && TYPE_P (parser->scope)
a52eb3bc 8639 && check_dependency_p
4e0f4df5
GB
8640 && dependent_type_p (parser->scope)
8641 /* Do not do this for dtors (or ctors), since they never
8642 need the template keyword before their name. */
8643 && !constructor_name_p (identifier, parser->scope))
a668c6ad 8644 {
0c5e4866
NS
8645 cp_token_position start = 0;
8646
a668c6ad 8647 /* Explain what went wrong. */
2a13a625
GDR
8648 error ("non-template %qD used as template", identifier);
8649 inform ("use %<%T::template %D%> to indicate that it is a template",
4e0f4df5 8650 parser->scope, identifier);
0b16f8f4
VR
8651 /* If parsing tentatively, find the location of the "<" token. */
8652 if (cp_parser_simulate_error (parser))
8653 start = cp_lexer_token_position (parser->lexer, true);
a668c6ad
MM
8654 /* Parse the template arguments so that we can issue error
8655 messages about them. */
8656 cp_lexer_consume_token (parser->lexer);
8657 cp_parser_enclosed_template_argument_list (parser);
8658 /* Skip tokens until we find a good place from which to
8659 continue parsing. */
8660 cp_parser_skip_to_closing_parenthesis (parser,
8661 /*recovering=*/true,
8662 /*or_comma=*/true,
8663 /*consume_paren=*/false);
8664 /* If parsing tentatively, permanently remove the
8665 template argument list. That will prevent duplicate
8666 error messages from being issued about the missing
8667 "template" keyword. */
0c5e4866
NS
8668 if (start)
8669 cp_lexer_purge_tokens_after (parser->lexer, start);
a668c6ad
MM
8670 if (is_identifier)
8671 *is_identifier = true;
8672 return identifier;
8673 }
9d363a56
MM
8674
8675 /* If the "template" keyword is present, then there is generally
8676 no point in doing name-lookup, so we just return IDENTIFIER.
8677 But, if the qualifying scope is non-dependent then we can
8678 (and must) do name-lookup normally. */
8679 if (template_keyword_p
8680 && (!parser->scope
98ca843c 8681 || (TYPE_P (parser->scope)
9d363a56 8682 && dependent_type_p (parser->scope))))
a668c6ad
MM
8683 return identifier;
8684 }
a723baf1
MM
8685
8686 /* Look up the name. */
8687 decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 8688 none_type,
b0bc6e8e 8689 /*is_template=*/false,
eea9800f 8690 /*is_namespace=*/false,
8f78f01f
MM
8691 check_dependency_p,
8692 /*ambiguous_p=*/NULL);
a723baf1
MM
8693 decl = maybe_get_template_decl_from_type_decl (decl);
8694
8695 /* If DECL is a template, then the name was a template-name. */
8696 if (TREE_CODE (decl) == TEMPLATE_DECL)
8697 ;
21526606 8698 else
a723baf1
MM
8699 {
8700 /* The standard does not explicitly indicate whether a name that
8701 names a set of overloaded declarations, some of which are
8702 templates, is a template-name. However, such a name should
8703 be a template-name; otherwise, there is no way to form a
8704 template-id for the overloaded templates. */
8705 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8706 if (TREE_CODE (fns) == OVERLOAD)
8707 {
8708 tree fn;
21526606 8709
a723baf1
MM
8710 for (fn = fns; fn; fn = OVL_NEXT (fn))
8711 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8712 break;
8713 }
8714 else
8715 {
8716 /* Otherwise, the name does not name a template. */
8717 cp_parser_error (parser, "expected template-name");
8718 return error_mark_node;
8719 }
8720 }
8721
8722 /* If DECL is dependent, and refers to a function, then just return
8723 its name; we will look it up again during template instantiation. */
8724 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8725 {
8726 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
1fb3244a 8727 if (TYPE_P (scope) && dependent_type_p (scope))
a723baf1
MM
8728 return identifier;
8729 }
8730
8731 return decl;
8732}
8733
8734/* Parse a template-argument-list.
8735
8736 template-argument-list:
8737 template-argument
8738 template-argument-list , template-argument
8739
04c06002 8740 Returns a TREE_VEC containing the arguments. */
a723baf1
MM
8741
8742static tree
94edc4ab 8743cp_parser_template_argument_list (cp_parser* parser)
a723baf1 8744{
bf12d54d
NS
8745 tree fixed_args[10];
8746 unsigned n_args = 0;
8747 unsigned alloced = 10;
8748 tree *arg_ary = fixed_args;
8749 tree vec;
4bb8ca28 8750 bool saved_in_template_argument_list_p;
a723baf1 8751
4bb8ca28
MM
8752 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8753 parser->in_template_argument_list_p = true;
bf12d54d 8754 do
a723baf1
MM
8755 {
8756 tree argument;
8757
bf12d54d 8758 if (n_args)
04c06002 8759 /* Consume the comma. */
bf12d54d 8760 cp_lexer_consume_token (parser->lexer);
21526606 8761
a723baf1
MM
8762 /* Parse the template-argument. */
8763 argument = cp_parser_template_argument (parser);
bf12d54d
NS
8764 if (n_args == alloced)
8765 {
8766 alloced *= 2;
21526606 8767
bf12d54d
NS
8768 if (arg_ary == fixed_args)
8769 {
8770 arg_ary = xmalloc (sizeof (tree) * alloced);
8771 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8772 }
8773 else
8774 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8775 }
8776 arg_ary[n_args++] = argument;
a723baf1 8777 }
bf12d54d
NS
8778 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8779
8780 vec = make_tree_vec (n_args);
a723baf1 8781
bf12d54d
NS
8782 while (n_args--)
8783 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
21526606 8784
bf12d54d
NS
8785 if (arg_ary != fixed_args)
8786 free (arg_ary);
4bb8ca28 8787 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
bf12d54d 8788 return vec;
a723baf1
MM
8789}
8790
8791/* Parse a template-argument.
8792
8793 template-argument:
8794 assignment-expression
8795 type-id
8796 id-expression
8797
8798 The representation is that of an assignment-expression, type-id, or
8799 id-expression -- except that the qualified id-expression is
8800 evaluated, so that the value returned is either a DECL or an
21526606 8801 OVERLOAD.
d17811fd
MM
8802
8803 Although the standard says "assignment-expression", it forbids
8804 throw-expressions or assignments in the template argument.
8805 Therefore, we use "conditional-expression" instead. */
a723baf1
MM
8806
8807static tree
94edc4ab 8808cp_parser_template_argument (cp_parser* parser)
a723baf1
MM
8809{
8810 tree argument;
8811 bool template_p;
d17811fd 8812 bool address_p;
4d5297fa 8813 bool maybe_type_id = false;
d17811fd 8814 cp_token *token;
b3445994 8815 cp_id_kind idk;
d17811fd 8816 tree qualifying_class;
a723baf1
MM
8817
8818 /* There's really no way to know what we're looking at, so we just
21526606 8819 try each alternative in order.
a723baf1
MM
8820
8821 [temp.arg]
8822
8823 In a template-argument, an ambiguity between a type-id and an
8824 expression is resolved to a type-id, regardless of the form of
21526606 8825 the corresponding template-parameter.
a723baf1
MM
8826
8827 Therefore, we try a type-id first. */
8828 cp_parser_parse_tentatively (parser);
a723baf1 8829 argument = cp_parser_type_id (parser);
4d5297fa 8830 /* If there was no error parsing the type-id but the next token is a '>>',
21526606 8831 we probably found a typo for '> >'. But there are type-id which are
4d5297fa
GB
8832 also valid expressions. For instance:
8833
8834 struct X { int operator >> (int); };
8835 template <int V> struct Foo {};
8836 Foo<X () >> 5> r;
8837
8838 Here 'X()' is a valid type-id of a function type, but the user just
8839 wanted to write the expression "X() >> 5". Thus, we remember that we
8840 found a valid type-id, but we still try to parse the argument as an
8841 expression to see what happens. */
8842 if (!cp_parser_error_occurred (parser)
8843 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8844 {
8845 maybe_type_id = true;
8846 cp_parser_abort_tentative_parse (parser);
8847 }
8848 else
8849 {
8850 /* If the next token isn't a `,' or a `>', then this argument wasn't
8851 really finished. This means that the argument is not a valid
8852 type-id. */
8853 if (!cp_parser_next_token_ends_template_argument_p (parser))
8854 cp_parser_error (parser, "expected template-argument");
8855 /* If that worked, we're done. */
8856 if (cp_parser_parse_definitely (parser))
8857 return argument;
8858 }
a723baf1
MM
8859 /* We're still not sure what the argument will be. */
8860 cp_parser_parse_tentatively (parser);
8861 /* Try a template. */
21526606 8862 argument = cp_parser_id_expression (parser,
a723baf1
MM
8863 /*template_keyword_p=*/false,
8864 /*check_dependency_p=*/true,
f3c2dfc6
MM
8865 &template_p,
8866 /*declarator_p=*/false);
a723baf1
MM
8867 /* If the next token isn't a `,' or a `>', then this argument wasn't
8868 really finished. */
d17811fd 8869 if (!cp_parser_next_token_ends_template_argument_p (parser))
a723baf1
MM
8870 cp_parser_error (parser, "expected template-argument");
8871 if (!cp_parser_error_occurred (parser))
8872 {
f746161e
MM
8873 /* Figure out what is being referred to. If the id-expression
8874 was for a class template specialization, then we will have a
8875 TYPE_DECL at this point. There is no need to do name lookup
8876 at this point in that case. */
8877 if (TREE_CODE (argument) != TYPE_DECL)
8878 argument = cp_parser_lookup_name (parser, argument,
fc6a28d7 8879 none_type,
f746161e
MM
8880 /*is_template=*/template_p,
8881 /*is_namespace=*/false,
8f78f01f
MM
8882 /*check_dependency=*/true,
8883 /*ambiguous_p=*/NULL);
5b4acce1
KL
8884 if (TREE_CODE (argument) != TEMPLATE_DECL
8885 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
a723baf1
MM
8886 cp_parser_error (parser, "expected template-name");
8887 }
8888 if (cp_parser_parse_definitely (parser))
8889 return argument;
d17811fd
MM
8890 /* It must be a non-type argument. There permitted cases are given
8891 in [temp.arg.nontype]:
8892
8893 -- an integral constant-expression of integral or enumeration
8894 type; or
8895
8896 -- the name of a non-type template-parameter; or
8897
8898 -- the name of an object or function with external linkage...
8899
8900 -- the address of an object or function with external linkage...
8901
04c06002 8902 -- a pointer to member... */
d17811fd
MM
8903 /* Look for a non-type template parameter. */
8904 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8905 {
8906 cp_parser_parse_tentatively (parser);
8907 argument = cp_parser_primary_expression (parser,
93678513 8908 /*cast_p=*/false,
d17811fd
MM
8909 &idk,
8910 &qualifying_class);
8911 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8912 || !cp_parser_next_token_ends_template_argument_p (parser))
8913 cp_parser_simulate_error (parser);
8914 if (cp_parser_parse_definitely (parser))
8915 return argument;
8916 }
db24eb1f 8917
d17811fd
MM
8918 /* If the next token is "&", the argument must be the address of an
8919 object or function with external linkage. */
8920 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8921 if (address_p)
8922 cp_lexer_consume_token (parser->lexer);
8923 /* See if we might have an id-expression. */
8924 token = cp_lexer_peek_token (parser->lexer);
8925 if (token->type == CPP_NAME
8926 || token->keyword == RID_OPERATOR
8927 || token->type == CPP_SCOPE
8928 || token->type == CPP_TEMPLATE_ID
8929 || token->type == CPP_NESTED_NAME_SPECIFIER)
8930 {
8931 cp_parser_parse_tentatively (parser);
8932 argument = cp_parser_primary_expression (parser,
93678513 8933 /*cast_p=*/false,
d17811fd
MM
8934 &idk,
8935 &qualifying_class);
8936 if (cp_parser_error_occurred (parser)
8937 || !cp_parser_next_token_ends_template_argument_p (parser))
8938 cp_parser_abort_tentative_parse (parser);
8939 else
8940 {
db24eb1f
NS
8941 if (TREE_CODE (argument) == INDIRECT_REF)
8942 {
8943 gcc_assert (REFERENCE_REF_P (argument));
8944 argument = TREE_OPERAND (argument, 0);
8945 }
8946
d17811fd
MM
8947 if (qualifying_class)
8948 argument = finish_qualified_id_expr (qualifying_class,
8949 argument,
8950 /*done=*/true,
8951 address_p);
8952 if (TREE_CODE (argument) == VAR_DECL)
8953 {
8954 /* A variable without external linkage might still be a
8955 valid constant-expression, so no error is issued here
8956 if the external-linkage check fails. */
8957 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8958 cp_parser_simulate_error (parser);
8959 }
8960 else if (is_overloaded_fn (argument))
8961 /* All overloaded functions are allowed; if the external
8962 linkage test does not pass, an error will be issued
8963 later. */
8964 ;
8965 else if (address_p
21526606 8966 && (TREE_CODE (argument) == OFFSET_REF
d17811fd
MM
8967 || TREE_CODE (argument) == SCOPE_REF))
8968 /* A pointer-to-member. */
8969 ;
db24eb1f
NS
8970 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
8971 ;
d17811fd
MM
8972 else
8973 cp_parser_simulate_error (parser);
8974
8975 if (cp_parser_parse_definitely (parser))
8976 {
8977 if (address_p)
8978 argument = build_x_unary_op (ADDR_EXPR, argument);
8979 return argument;
8980 }
8981 }
8982 }
8983 /* If the argument started with "&", there are no other valid
8984 alternatives at this point. */
8985 if (address_p)
8986 {
8987 cp_parser_error (parser, "invalid non-type template argument");
8988 return error_mark_node;
8989 }
db24eb1f 8990
4d5297fa 8991 /* If the argument wasn't successfully parsed as a type-id followed
21526606 8992 by '>>', the argument can only be a constant expression now.
4d5297fa
GB
8993 Otherwise, we try parsing the constant-expression tentatively,
8994 because the argument could really be a type-id. */
8995 if (maybe_type_id)
8996 cp_parser_parse_tentatively (parser);
21526606 8997 argument = cp_parser_constant_expression (parser,
d17811fd
MM
8998 /*allow_non_constant_p=*/false,
8999 /*non_constant_p=*/NULL);
9baa27a9 9000 argument = fold_non_dependent_expr (argument);
4d5297fa
GB
9001 if (!maybe_type_id)
9002 return argument;
9003 if (!cp_parser_next_token_ends_template_argument_p (parser))
9004 cp_parser_error (parser, "expected template-argument");
9005 if (cp_parser_parse_definitely (parser))
9006 return argument;
9007 /* We did our best to parse the argument as a non type-id, but that
9008 was the only alternative that matched (albeit with a '>' after
21526606 9009 it). We can assume it's just a typo from the user, and a
4d5297fa
GB
9010 diagnostic will then be issued. */
9011 return cp_parser_type_id (parser);
a723baf1
MM
9012}
9013
9014/* Parse an explicit-instantiation.
9015
9016 explicit-instantiation:
21526606 9017 template declaration
a723baf1
MM
9018
9019 Although the standard says `declaration', what it really means is:
9020
9021 explicit-instantiation:
21526606 9022 template decl-specifier-seq [opt] declarator [opt] ;
a723baf1
MM
9023
9024 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9025 supposed to be allowed. A defect report has been filed about this
21526606 9026 issue.
a723baf1
MM
9027
9028 GNU Extension:
21526606 9029
a723baf1 9030 explicit-instantiation:
21526606 9031 storage-class-specifier template
a723baf1 9032 decl-specifier-seq [opt] declarator [opt] ;
21526606 9033 function-specifier template
a723baf1
MM
9034 decl-specifier-seq [opt] declarator [opt] ; */
9035
9036static void
94edc4ab 9037cp_parser_explicit_instantiation (cp_parser* parser)
a723baf1 9038{
560ad596 9039 int declares_class_or_enum;
62d1db17 9040 cp_decl_specifier_seq decl_specifiers;
a723baf1
MM
9041 tree extension_specifier = NULL_TREE;
9042
9043 /* Look for an (optional) storage-class-specifier or
9044 function-specifier. */
9045 if (cp_parser_allow_gnu_extensions_p (parser))
9046 {
21526606 9047 extension_specifier
a723baf1
MM
9048 = cp_parser_storage_class_specifier_opt (parser);
9049 if (!extension_specifier)
98ca843c 9050 extension_specifier
62d1db17
MM
9051 = cp_parser_function_specifier_opt (parser,
9052 /*decl_specs=*/NULL);
a723baf1
MM
9053 }
9054
9055 /* Look for the `template' keyword. */
9056 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9057 /* Let the front end know that we are processing an explicit
9058 instantiation. */
9059 begin_explicit_instantiation ();
9060 /* [temp.explicit] says that we are supposed to ignore access
9061 control while processing explicit instantiation directives. */
78757caa 9062 push_deferring_access_checks (dk_no_check);
a723baf1 9063 /* Parse a decl-specifier-seq. */
62d1db17
MM
9064 cp_parser_decl_specifier_seq (parser,
9065 CP_PARSER_FLAGS_OPTIONAL,
9066 &decl_specifiers,
9067 &declares_class_or_enum);
a723baf1
MM
9068 /* If there was exactly one decl-specifier, and it declared a class,
9069 and there's no declarator, then we have an explicit type
9070 instantiation. */
9071 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9072 {
9073 tree type;
9074
62d1db17 9075 type = check_tag_decl (&decl_specifiers);
b7fc8b57
KL
9076 /* Turn access control back on for names used during
9077 template instantiation. */
9078 pop_deferring_access_checks ();
a723baf1
MM
9079 if (type)
9080 do_type_instantiation (type, extension_specifier, /*complain=*/1);
9081 }
9082 else
9083 {
058b15c1 9084 cp_declarator *declarator;
a723baf1
MM
9085 tree decl;
9086
9087 /* Parse the declarator. */
21526606 9088 declarator
62b8a44e 9089 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 9090 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
9091 /*parenthesized_p=*/NULL,
9092 /*member_p=*/false);
fc6a28d7
MM
9093 if (declares_class_or_enum & 2)
9094 cp_parser_check_for_definition_in_return_type (declarator,
9095 decl_specifiers.type);
058b15c1 9096 if (declarator != cp_error_declarator)
216bb6e1 9097 {
62d1db17 9098 decl = grokdeclarator (declarator, &decl_specifiers,
216bb6e1
MM
9099 NORMAL, 0, NULL);
9100 /* Turn access control back on for names used during
9101 template instantiation. */
9102 pop_deferring_access_checks ();
9103 /* Do the explicit instantiation. */
9104 do_decl_instantiation (decl, extension_specifier);
9105 }
9106 else
9107 {
9108 pop_deferring_access_checks ();
9109 /* Skip the body of the explicit instantiation. */
9110 cp_parser_skip_to_end_of_statement (parser);
9111 }
a723baf1
MM
9112 }
9113 /* We're done with the instantiation. */
9114 end_explicit_instantiation ();
a723baf1 9115
e0860732 9116 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1
MM
9117}
9118
9119/* Parse an explicit-specialization.
9120
9121 explicit-specialization:
21526606 9122 template < > declaration
a723baf1
MM
9123
9124 Although the standard says `declaration', what it really means is:
9125
9126 explicit-specialization:
9127 template <> decl-specifier [opt] init-declarator [opt] ;
21526606 9128 template <> function-definition
a723baf1
MM
9129 template <> explicit-specialization
9130 template <> template-declaration */
9131
9132static void
94edc4ab 9133cp_parser_explicit_specialization (cp_parser* parser)
a723baf1
MM
9134{
9135 /* Look for the `template' keyword. */
9136 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9137 /* Look for the `<'. */
9138 cp_parser_require (parser, CPP_LESS, "`<'");
9139 /* Look for the `>'. */
9140 cp_parser_require (parser, CPP_GREATER, "`>'");
9141 /* We have processed another parameter list. */
9142 ++parser->num_template_parameter_lists;
9143 /* Let the front end know that we are beginning a specialization. */
9144 begin_specialization ();
9145
9146 /* If the next keyword is `template', we need to figure out whether
9147 or not we're looking a template-declaration. */
9148 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9149 {
9150 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9151 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9152 cp_parser_template_declaration_after_export (parser,
9153 /*member_p=*/false);
9154 else
9155 cp_parser_explicit_specialization (parser);
9156 }
9157 else
9158 /* Parse the dependent declaration. */
21526606 9159 cp_parser_single_declaration (parser,
a723baf1
MM
9160 /*member_p=*/false,
9161 /*friend_p=*/NULL);
9162
9163 /* We're done with the specialization. */
9164 end_specialization ();
9165 /* We're done with this parameter list. */
9166 --parser->num_template_parameter_lists;
9167}
9168
9169/* Parse a type-specifier.
9170
9171 type-specifier:
9172 simple-type-specifier
9173 class-specifier
9174 enum-specifier
9175 elaborated-type-specifier
9176 cv-qualifier
9177
9178 GNU Extension:
9179
9180 type-specifier:
9181 __complex__
9182
62d1db17
MM
9183 Returns a representation of the type-specifier. For a
9184 class-specifier, enum-specifier, or elaborated-type-specifier, a
9185 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
a723baf1 9186
eb1aef53
KL
9187 The parser flags FLAGS is used to control type-specifier parsing.
9188
9189 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9190 in a decl-specifier-seq.
a723baf1
MM
9191
9192 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9193 class-specifier, enum-specifier, or elaborated-type-specifier, then
83a00410 9194 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
560ad596
MM
9195 if a type is declared; 2 if it is defined. Otherwise, it is set to
9196 zero.
a723baf1
MM
9197
9198 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9199 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9200 is set to FALSE. */
9201
9202static tree
21526606
EC
9203cp_parser_type_specifier (cp_parser* parser,
9204 cp_parser_flags flags,
62d1db17 9205 cp_decl_specifier_seq *decl_specs,
94edc4ab 9206 bool is_declaration,
560ad596 9207 int* declares_class_or_enum,
94edc4ab 9208 bool* is_cv_qualifier)
a723baf1
MM
9209{
9210 tree type_spec = NULL_TREE;
9211 cp_token *token;
9212 enum rid keyword;
62d1db17 9213 cp_decl_spec ds = ds_last;
a723baf1
MM
9214
9215 /* Assume this type-specifier does not declare a new type. */
9216 if (declares_class_or_enum)
543ca912 9217 *declares_class_or_enum = 0;
a723baf1
MM
9218 /* And that it does not specify a cv-qualifier. */
9219 if (is_cv_qualifier)
9220 *is_cv_qualifier = false;
9221 /* Peek at the next token. */
9222 token = cp_lexer_peek_token (parser->lexer);
9223
9224 /* If we're looking at a keyword, we can use that to guide the
9225 production we choose. */
9226 keyword = token->keyword;
9227 switch (keyword)
9228 {
ff4eb0b5
ZW
9229 case RID_ENUM:
9230 /* 'enum' [identifier] '{' introduces an enum-specifier;
9231 'enum' <anything else> introduces an elaborated-type-specifier. */
9232 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9233 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9234 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9235 == CPP_OPEN_BRACE))
9236 {
a5e51518
KL
9237 if (parser->num_template_parameter_lists)
9238 {
9239 error ("template declaration of %qs", "enum");
9240 cp_parser_skip_to_end_of_block_or_statement (parser);
9241 type_spec = error_mark_node;
9242 }
9243 else
9244 type_spec = cp_parser_enum_specifier (parser);
9245
ff4eb0b5
ZW
9246 if (declares_class_or_enum)
9247 *declares_class_or_enum = 2;
9248 if (decl_specs)
9249 cp_parser_set_decl_spec_type (decl_specs,
9250 type_spec,
9251 /*user_defined_p=*/true);
9252 return type_spec;
9253 }
9254 else
9255 goto elaborated_type_specifier;
9256
a723baf1
MM
9257 /* Any of these indicate either a class-specifier, or an
9258 elaborated-type-specifier. */
9259 case RID_CLASS:
9260 case RID_STRUCT:
9261 case RID_UNION:
a723baf1 9262 /* Parse tentatively so that we can back up if we don't find a
ff4eb0b5 9263 class-specifier. */
a723baf1 9264 cp_parser_parse_tentatively (parser);
ff4eb0b5
ZW
9265 /* Look for the class-specifier. */
9266 type_spec = cp_parser_class_specifier (parser);
a723baf1
MM
9267 /* If that worked, we're done. */
9268 if (cp_parser_parse_definitely (parser))
9269 {
9270 if (declares_class_or_enum)
560ad596 9271 *declares_class_or_enum = 2;
62d1db17
MM
9272 if (decl_specs)
9273 cp_parser_set_decl_spec_type (decl_specs,
9274 type_spec,
9275 /*user_defined_p=*/true);
a723baf1
MM
9276 return type_spec;
9277 }
9278
9279 /* Fall through. */
ff4eb0b5
ZW
9280 elaborated_type_specifier:
9281 /* We're declaring (not defining) a class or enum. */
9282 if (declares_class_or_enum)
9283 *declares_class_or_enum = 1;
a723baf1 9284
ff4eb0b5 9285 /* Fall through. */
a723baf1
MM
9286 case RID_TYPENAME:
9287 /* Look for an elaborated-type-specifier. */
98ca843c
EC
9288 type_spec
9289 = (cp_parser_elaborated_type_specifier
62d1db17
MM
9290 (parser,
9291 decl_specs && decl_specs->specs[(int) ds_friend],
9292 is_declaration));
62d1db17
MM
9293 if (decl_specs)
9294 cp_parser_set_decl_spec_type (decl_specs,
9295 type_spec,
9296 /*user_defined_p=*/true);
a723baf1
MM
9297 return type_spec;
9298
9299 case RID_CONST:
62d1db17
MM
9300 ds = ds_const;
9301 if (is_cv_qualifier)
9302 *is_cv_qualifier = true;
9303 break;
98ca843c 9304
a723baf1 9305 case RID_VOLATILE:
62d1db17 9306 ds = ds_volatile;
a723baf1
MM
9307 if (is_cv_qualifier)
9308 *is_cv_qualifier = true;
62d1db17 9309 break;
a723baf1 9310
62d1db17
MM
9311 case RID_RESTRICT:
9312 ds = ds_restrict;
9313 if (is_cv_qualifier)
9314 *is_cv_qualifier = true;
9315 break;
a723baf1
MM
9316
9317 case RID_COMPLEX:
9318 /* The `__complex__' keyword is a GNU extension. */
62d1db17
MM
9319 ds = ds_complex;
9320 break;
a723baf1
MM
9321
9322 default:
9323 break;
9324 }
9325
62d1db17
MM
9326 /* Handle simple keywords. */
9327 if (ds != ds_last)
9328 {
9329 if (decl_specs)
9330 {
9331 ++decl_specs->specs[(int)ds];
9332 decl_specs->any_specifiers_p = true;
9333 }
9334 return cp_lexer_consume_token (parser->lexer)->value;
9335 }
9336
a723baf1
MM
9337 /* If we do not already have a type-specifier, assume we are looking
9338 at a simple-type-specifier. */
98ca843c 9339 type_spec = cp_parser_simple_type_specifier (parser,
62d1db17
MM
9340 decl_specs,
9341 flags);
a723baf1
MM
9342
9343 /* If we didn't find a type-specifier, and a type-specifier was not
9344 optional in this context, issue an error message. */
9345 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9346 {
9347 cp_parser_error (parser, "expected type specifier");
9348 return error_mark_node;
9349 }
9350
9351 return type_spec;
9352}
9353
9354/* Parse a simple-type-specifier.
9355
9356 simple-type-specifier:
9357 :: [opt] nested-name-specifier [opt] type-name
9358 :: [opt] nested-name-specifier template template-id
9359 char
9360 wchar_t
9361 bool
9362 short
9363 int
9364 long
9365 signed
9366 unsigned
9367 float
9368 double
21526606 9369 void
a723baf1
MM
9370
9371 GNU Extension:
9372
9373 simple-type-specifier:
9374 __typeof__ unary-expression
9375 __typeof__ ( type-id )
9376
62d1db17
MM
9377 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9378 appropriately updated. */
a723baf1
MM
9379
9380static tree
98ca843c 9381cp_parser_simple_type_specifier (cp_parser* parser,
62d1db17
MM
9382 cp_decl_specifier_seq *decl_specs,
9383 cp_parser_flags flags)
a723baf1
MM
9384{
9385 tree type = NULL_TREE;
9386 cp_token *token;
9387
9388 /* Peek at the next token. */
9389 token = cp_lexer_peek_token (parser->lexer);
9390
9391 /* If we're looking at a keyword, things are easy. */
9392 switch (token->keyword)
9393 {
9394 case RID_CHAR:
62d1db17
MM
9395 if (decl_specs)
9396 decl_specs->explicit_char_p = true;
4b0d3cbe
MM
9397 type = char_type_node;
9398 break;
a723baf1 9399 case RID_WCHAR:
4b0d3cbe
MM
9400 type = wchar_type_node;
9401 break;
a723baf1 9402 case RID_BOOL:
4b0d3cbe
MM
9403 type = boolean_type_node;
9404 break;
a723baf1 9405 case RID_SHORT:
62d1db17
MM
9406 if (decl_specs)
9407 ++decl_specs->specs[(int) ds_short];
4b0d3cbe
MM
9408 type = short_integer_type_node;
9409 break;
a723baf1 9410 case RID_INT:
62d1db17
MM
9411 if (decl_specs)
9412 decl_specs->explicit_int_p = true;
4b0d3cbe
MM
9413 type = integer_type_node;
9414 break;
a723baf1 9415 case RID_LONG:
62d1db17
MM
9416 if (decl_specs)
9417 ++decl_specs->specs[(int) ds_long];
4b0d3cbe
MM
9418 type = long_integer_type_node;
9419 break;
a723baf1 9420 case RID_SIGNED:
62d1db17
MM
9421 if (decl_specs)
9422 ++decl_specs->specs[(int) ds_signed];
4b0d3cbe
MM
9423 type = integer_type_node;
9424 break;
a723baf1 9425 case RID_UNSIGNED:
62d1db17
MM
9426 if (decl_specs)
9427 ++decl_specs->specs[(int) ds_unsigned];
4b0d3cbe
MM
9428 type = unsigned_type_node;
9429 break;
a723baf1 9430 case RID_FLOAT:
4b0d3cbe
MM
9431 type = float_type_node;
9432 break;
a723baf1 9433 case RID_DOUBLE:
4b0d3cbe
MM
9434 type = double_type_node;
9435 break;
a723baf1 9436 case RID_VOID:
4b0d3cbe
MM
9437 type = void_type_node;
9438 break;
a723baf1
MM
9439
9440 case RID_TYPEOF:
62d1db17
MM
9441 /* Consume the `typeof' token. */
9442 cp_lexer_consume_token (parser->lexer);
9443 /* Parse the operand to `typeof'. */
9444 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9445 /* If it is not already a TYPE, take its type. */
9446 if (!TYPE_P (type))
9447 type = finish_typeof (type);
9448
9449 if (decl_specs)
9450 cp_parser_set_decl_spec_type (decl_specs, type,
9451 /*user_defined_p=*/true);
98ca843c 9452
62d1db17 9453 return type;
a723baf1
MM
9454
9455 default:
9456 break;
9457 }
9458
4b0d3cbe
MM
9459 /* If the type-specifier was for a built-in type, we're done. */
9460 if (type)
9461 {
9462 tree id;
9463
62d1db17
MM
9464 /* Record the type. */
9465 if (decl_specs
9466 && (token->keyword != RID_SIGNED
9467 && token->keyword != RID_UNSIGNED
9468 && token->keyword != RID_SHORT
9469 && token->keyword != RID_LONG))
98ca843c 9470 cp_parser_set_decl_spec_type (decl_specs,
62d1db17
MM
9471 type,
9472 /*user_defined=*/false);
9473 if (decl_specs)
9474 decl_specs->any_specifiers_p = true;
9475
4b0d3cbe
MM
9476 /* Consume the token. */
9477 id = cp_lexer_consume_token (parser->lexer)->value;
0d956474
GB
9478
9479 /* There is no valid C++ program where a non-template type is
9480 followed by a "<". That usually indicates that the user thought
9481 that the type was a template. */
9482 cp_parser_check_for_invalid_template_id (parser, type);
9483
62d1db17 9484 return TYPE_NAME (type);
4b0d3cbe
MM
9485 }
9486
a723baf1 9487 /* The type-specifier must be a user-defined type. */
21526606 9488 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
a723baf1 9489 {
0c1a1ecd 9490 bool qualified_p;
f68e4dc8 9491 bool global_p;
0c1a1ecd 9492
a723baf1
MM
9493 /* Don't gobble tokens or issue error messages if this is an
9494 optional type-specifier. */
9495 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9496 cp_parser_parse_tentatively (parser);
9497
9498 /* Look for the optional `::' operator. */
f68e4dc8 9499 global_p
da740453
MM
9500 = (cp_parser_global_scope_opt (parser,
9501 /*current_scope_valid_p=*/false)
9502 != NULL_TREE);
a723baf1 9503 /* Look for the nested-name specifier. */
0c1a1ecd
MM
9504 qualified_p
9505 = (cp_parser_nested_name_specifier_opt (parser,
9506 /*typename_keyword_p=*/false,
9507 /*check_dependency_p=*/true,
9508 /*type_p=*/false,
6661a85f
EB
9509 /*is_declaration=*/false)
9510 != NULL_TREE);
a723baf1
MM
9511 /* If we have seen a nested-name-specifier, and the next token
9512 is `template', then we are using the template-id production. */
21526606 9513 if (parser->scope
a723baf1
MM
9514 && cp_parser_optional_template_keyword (parser))
9515 {
9516 /* Look for the template-id. */
21526606 9517 type = cp_parser_template_id (parser,
a723baf1 9518 /*template_keyword_p=*/true,
a668c6ad
MM
9519 /*check_dependency_p=*/true,
9520 /*is_declaration=*/false);
a723baf1
MM
9521 /* If the template-id did not name a type, we are out of
9522 luck. */
9523 if (TREE_CODE (type) != TYPE_DECL)
9524 {
9525 cp_parser_error (parser, "expected template-id for type");
9526 type = NULL_TREE;
9527 }
9528 }
9529 /* Otherwise, look for a type-name. */
9530 else
4bb8ca28 9531 type = cp_parser_type_name (parser);
0c1a1ecd 9532 /* Keep track of all name-lookups performed in class scopes. */
98ca843c 9533 if (type
f68e4dc8 9534 && !global_p
0c1a1ecd 9535 && !qualified_p
98ca843c 9536 && TREE_CODE (type) == TYPE_DECL
0c1a1ecd
MM
9537 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9538 maybe_note_name_used_in_class (DECL_NAME (type), type);
a723baf1 9539 /* If it didn't work out, we don't have a TYPE. */
21526606 9540 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
a723baf1
MM
9541 && !cp_parser_parse_definitely (parser))
9542 type = NULL_TREE;
62d1db17
MM
9543 if (type && decl_specs)
9544 cp_parser_set_decl_spec_type (decl_specs, type,
9545 /*user_defined=*/true);
a723baf1
MM
9546 }
9547
9548 /* If we didn't get a type-name, issue an error message. */
9549 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9550 {
9551 cp_parser_error (parser, "expected type-name");
9552 return error_mark_node;
9553 }
9554
a668c6ad
MM
9555 /* There is no valid C++ program where a non-template type is
9556 followed by a "<". That usually indicates that the user thought
9557 that the type was a template. */
4bb8ca28 9558 if (type && type != error_mark_node)
ee43dab5 9559 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
ec75414f 9560
a723baf1
MM
9561 return type;
9562}
9563
9564/* Parse a type-name.
9565
9566 type-name:
9567 class-name
9568 enum-name
21526606 9569 typedef-name
a723baf1
MM
9570
9571 enum-name:
9572 identifier
9573
9574 typedef-name:
21526606 9575 identifier
a723baf1
MM
9576
9577 Returns a TYPE_DECL for the the type. */
9578
9579static tree
94edc4ab 9580cp_parser_type_name (cp_parser* parser)
a723baf1
MM
9581{
9582 tree type_decl;
9583 tree identifier;
9584
9585 /* We can't know yet whether it is a class-name or not. */
9586 cp_parser_parse_tentatively (parser);
9587 /* Try a class-name. */
21526606 9588 type_decl = cp_parser_class_name (parser,
a723baf1
MM
9589 /*typename_keyword_p=*/false,
9590 /*template_keyword_p=*/false,
fc6a28d7 9591 none_type,
a723baf1 9592 /*check_dependency_p=*/true,
a668c6ad
MM
9593 /*class_head_p=*/false,
9594 /*is_declaration=*/false);
a723baf1
MM
9595 /* If it's not a class-name, keep looking. */
9596 if (!cp_parser_parse_definitely (parser))
9597 {
9598 /* It must be a typedef-name or an enum-name. */
9599 identifier = cp_parser_identifier (parser);
9600 if (identifier == error_mark_node)
9601 return error_mark_node;
21526606 9602
a723baf1
MM
9603 /* Look up the type-name. */
9604 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9605 /* Issue an error if we did not find a type-name. */
9606 if (TREE_CODE (type_decl) != TYPE_DECL)
9607 {
4bb8ca28 9608 if (!cp_parser_simulate_error (parser))
21526606 9609 cp_parser_name_lookup_error (parser, identifier, type_decl,
4bb8ca28 9610 "is not a type");
a723baf1
MM
9611 type_decl = error_mark_node;
9612 }
9613 /* Remember that the name was used in the definition of the
9614 current class so that we can check later to see if the
9615 meaning would have been different after the class was
9616 entirely defined. */
9617 else if (type_decl != error_mark_node
9618 && !parser->scope)
9619 maybe_note_name_used_in_class (identifier, type_decl);
9620 }
21526606 9621
a723baf1
MM
9622 return type_decl;
9623}
9624
9625
9626/* Parse an elaborated-type-specifier. Note that the grammar given
9627 here incorporates the resolution to DR68.
9628
9629 elaborated-type-specifier:
9630 class-key :: [opt] nested-name-specifier [opt] identifier
9631 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9632 enum :: [opt] nested-name-specifier [opt] identifier
9633 typename :: [opt] nested-name-specifier identifier
21526606
EC
9634 typename :: [opt] nested-name-specifier template [opt]
9635 template-id
a723baf1 9636
360d1b99
MM
9637 GNU extension:
9638
9639 elaborated-type-specifier:
9640 class-key attributes :: [opt] nested-name-specifier [opt] identifier
21526606 9641 class-key attributes :: [opt] nested-name-specifier [opt]
360d1b99
MM
9642 template [opt] template-id
9643 enum attributes :: [opt] nested-name-specifier [opt] identifier
9644
a723baf1
MM
9645 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9646 declared `friend'. If IS_DECLARATION is TRUE, then this
9647 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9648 something is being declared.
9649
9650 Returns the TYPE specified. */
9651
9652static tree
21526606
EC
9653cp_parser_elaborated_type_specifier (cp_parser* parser,
9654 bool is_friend,
94edc4ab 9655 bool is_declaration)
a723baf1
MM
9656{
9657 enum tag_types tag_type;
9658 tree identifier;
9659 tree type = NULL_TREE;
360d1b99 9660 tree attributes = NULL_TREE;
a723baf1
MM
9661
9662 /* See if we're looking at the `enum' keyword. */
9663 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9664 {
9665 /* Consume the `enum' token. */
9666 cp_lexer_consume_token (parser->lexer);
9667 /* Remember that it's an enumeration type. */
9668 tag_type = enum_type;
360d1b99
MM
9669 /* Parse the attributes. */
9670 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
9671 }
9672 /* Or, it might be `typename'. */
9673 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9674 RID_TYPENAME))
9675 {
9676 /* Consume the `typename' token. */
9677 cp_lexer_consume_token (parser->lexer);
9678 /* Remember that it's a `typename' type. */
9679 tag_type = typename_type;
9680 /* The `typename' keyword is only allowed in templates. */
9681 if (!processing_template_decl)
2a13a625 9682 pedwarn ("using %<typename%> outside of template");
a723baf1
MM
9683 }
9684 /* Otherwise it must be a class-key. */
9685 else
9686 {
9687 tag_type = cp_parser_class_key (parser);
9688 if (tag_type == none_type)
9689 return error_mark_node;
360d1b99
MM
9690 /* Parse the attributes. */
9691 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
9692 }
9693
9694 /* Look for the `::' operator. */
21526606 9695 cp_parser_global_scope_opt (parser,
a723baf1
MM
9696 /*current_scope_valid_p=*/false);
9697 /* Look for the nested-name-specifier. */
9698 if (tag_type == typename_type)
8fa1ad0e
MM
9699 {
9700 if (cp_parser_nested_name_specifier (parser,
9701 /*typename_keyword_p=*/true,
9702 /*check_dependency_p=*/true,
a668c6ad 9703 /*type_p=*/true,
21526606 9704 is_declaration)
8fa1ad0e
MM
9705 == error_mark_node)
9706 return error_mark_node;
9707 }
a723baf1
MM
9708 else
9709 /* Even though `typename' is not present, the proposed resolution
9710 to Core Issue 180 says that in `class A<T>::B', `B' should be
9711 considered a type-name, even if `A<T>' is dependent. */
9712 cp_parser_nested_name_specifier_opt (parser,
9713 /*typename_keyword_p=*/true,
9714 /*check_dependency_p=*/true,
a668c6ad
MM
9715 /*type_p=*/true,
9716 is_declaration);
a723baf1
MM
9717 /* For everything but enumeration types, consider a template-id. */
9718 if (tag_type != enum_type)
9719 {
9720 bool template_p = false;
9721 tree decl;
9722
9723 /* Allow the `template' keyword. */
9724 template_p = cp_parser_optional_template_keyword (parser);
9725 /* If we didn't see `template', we don't know if there's a
9726 template-id or not. */
9727 if (!template_p)
9728 cp_parser_parse_tentatively (parser);
9729 /* Parse the template-id. */
9730 decl = cp_parser_template_id (parser, template_p,
a668c6ad
MM
9731 /*check_dependency_p=*/true,
9732 is_declaration);
a723baf1
MM
9733 /* If we didn't find a template-id, look for an ordinary
9734 identifier. */
9735 if (!template_p && !cp_parser_parse_definitely (parser))
9736 ;
9737 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9738 in effect, then we must assume that, upon instantiation, the
9739 template will correspond to a class. */
9740 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9741 && tag_type == typename_type)
9742 type = make_typename_type (parser->scope, decl,
fc6a28d7 9743 typename_type,
a723baf1 9744 /*complain=*/1);
21526606 9745 else
a723baf1
MM
9746 type = TREE_TYPE (decl);
9747 }
9748
9749 /* For an enumeration type, consider only a plain identifier. */
9750 if (!type)
9751 {
9752 identifier = cp_parser_identifier (parser);
9753
9754 if (identifier == error_mark_node)
eb5abb39
NS
9755 {
9756 parser->scope = NULL_TREE;
9757 return error_mark_node;
9758 }
a723baf1
MM
9759
9760 /* For a `typename', we needn't call xref_tag. */
0c88d886
MM
9761 if (tag_type == typename_type
9762 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
21526606 9763 return cp_parser_make_typename_type (parser, parser->scope,
2097b5f2 9764 identifier);
a723baf1
MM
9765 /* Look up a qualified name in the usual way. */
9766 if (parser->scope)
9767 {
9768 tree decl;
9769
21526606 9770 decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 9771 tag_type,
b0bc6e8e 9772 /*is_template=*/false,
eea9800f 9773 /*is_namespace=*/false,
8f78f01f
MM
9774 /*check_dependency=*/true,
9775 /*ambiguous_p=*/NULL);
710b73e6
KL
9776
9777 /* If we are parsing friend declaration, DECL may be a
9778 TEMPLATE_DECL tree node here. However, we need to check
9779 whether this TEMPLATE_DECL results in valid code. Consider
9780 the following example:
9781
9782 namespace N {
9783 template <class T> class C {};
9784 }
9785 class X {
9786 template <class T> friend class N::C; // #1, valid code
9787 };
9788 template <class T> class Y {
9789 friend class N::C; // #2, invalid code
9790 };
9791
9792 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9793 name lookup of `N::C'. We see that friend declaration must
9794 be template for the code to be valid. Note that
9795 processing_template_decl does not work here since it is
9796 always 1 for the above two cases. */
9797
21526606 9798 decl = (cp_parser_maybe_treat_template_as_class
710b73e6
KL
9799 (decl, /*tag_name_p=*/is_friend
9800 && parser->num_template_parameter_lists));
a723baf1
MM
9801
9802 if (TREE_CODE (decl) != TYPE_DECL)
9803 {
0c88d886
MM
9804 cp_parser_diagnose_invalid_type_name (parser,
9805 parser->scope,
9806 identifier);
a723baf1
MM
9807 return error_mark_node;
9808 }
560ad596
MM
9809
9810 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
21526606 9811 check_elaborated_type_specifier
4b0d3cbe 9812 (tag_type, decl,
560ad596
MM
9813 (parser->num_template_parameter_lists
9814 || DECL_SELF_REFERENCE_P (decl)));
a723baf1
MM
9815
9816 type = TREE_TYPE (decl);
9817 }
21526606 9818 else
a723baf1
MM
9819 {
9820 /* An elaborated-type-specifier sometimes introduces a new type and
9821 sometimes names an existing type. Normally, the rule is that it
9822 introduces a new type only if there is not an existing type of
9823 the same name already in scope. For example, given:
9824
9825 struct S {};
9826 void f() { struct S s; }
9827
9828 the `struct S' in the body of `f' is the same `struct S' as in
9829 the global scope; the existing definition is used. However, if
21526606 9830 there were no global declaration, this would introduce a new
a723baf1
MM
9831 local class named `S'.
9832
9833 An exception to this rule applies to the following code:
9834
9835 namespace N { struct S; }
9836
9837 Here, the elaborated-type-specifier names a new type
9838 unconditionally; even if there is already an `S' in the
9839 containing scope this declaration names a new type.
9840 This exception only applies if the elaborated-type-specifier
9841 forms the complete declaration:
9842
21526606 9843 [class.name]
a723baf1
MM
9844
9845 A declaration consisting solely of `class-key identifier ;' is
9846 either a redeclaration of the name in the current scope or a
9847 forward declaration of the identifier as a class name. It
9848 introduces the name into the current scope.
9849
9850 We are in this situation precisely when the next token is a `;'.
9851
9852 An exception to the exception is that a `friend' declaration does
9853 *not* name a new type; i.e., given:
9854
9855 struct S { friend struct T; };
9856
21526606 9857 `T' is not a new type in the scope of `S'.
a723baf1
MM
9858
9859 Also, `new struct S' or `sizeof (struct S)' never results in the
9860 definition of a new type; a new type can only be declared in a
9bcb9aae 9861 declaration context. */
a723baf1 9862
29ef83de
KL
9863 tag_scope ts;
9864 if (is_friend)
9865 /* Friends have special name lookup rules. */
9866 ts = ts_within_enclosing_non_class;
9867 else if (is_declaration
9868 && cp_lexer_next_token_is (parser->lexer,
9869 CPP_SEMICOLON))
9870 /* This is a `class-key identifier ;' */
9871 ts = ts_current;
9872 else
9873 ts = ts_global;
9874
e0fed25b
DS
9875 /* Warn about attributes. They are ignored. */
9876 if (attributes)
9877 warning ("type attributes are honored only at type definition");
9878
29ef83de 9879 type = xref_tag (tag_type, identifier, ts,
cbd63935 9880 parser->num_template_parameter_lists);
a723baf1
MM
9881 }
9882 }
9883 if (tag_type != enum_type)
9884 cp_parser_check_class_key (tag_type, type);
ee43dab5
MM
9885
9886 /* A "<" cannot follow an elaborated type specifier. If that
9887 happens, the user was probably trying to form a template-id. */
9888 cp_parser_check_for_invalid_template_id (parser, type);
9889
a723baf1
MM
9890 return type;
9891}
9892
9893/* Parse an enum-specifier.
9894
9895 enum-specifier:
9896 enum identifier [opt] { enumerator-list [opt] }
9897
f6af9a15
MA
9898 GNU Extensions:
9899 enum identifier [opt] { enumerator-list [opt] } attributes
9900
a723baf1
MM
9901 Returns an ENUM_TYPE representing the enumeration. */
9902
9903static tree
94edc4ab 9904cp_parser_enum_specifier (cp_parser* parser)
a723baf1 9905{
ff4eb0b5 9906 tree identifier;
a723baf1
MM
9907 tree type;
9908
ff4eb0b5
ZW
9909 /* Caller guarantees that the current token is 'enum', an identifier
9910 possibly follows, and the token after that is an opening brace.
9911 If we don't have an identifier, fabricate an anonymous name for
9912 the enumeration being defined. */
9913 cp_lexer_consume_token (parser->lexer);
a723baf1 9914
ff4eb0b5 9915 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
a723baf1 9916 identifier = cp_parser_identifier (parser);
ff4eb0b5
ZW
9917 else
9918 identifier = make_anon_name ();
a723baf1 9919
a723baf1
MM
9920 /* Issue an error message if type-definitions are forbidden here. */
9921 cp_parser_check_type_definition (parser);
9922
2cfe82fe
ZW
9923 /* Create the new type. We do this before consuming the opening brace
9924 so the enum will be recorded as being on the line of its tag (or the
9925 'enum' keyword, if there is no tag). */
ff4eb0b5 9926 type = start_enum (identifier);
a723baf1 9927
2cfe82fe
ZW
9928 /* Consume the opening brace. */
9929 cp_lexer_consume_token (parser->lexer);
9930
ff4eb0b5
ZW
9931 /* If the next token is not '}', then there are some enumerators. */
9932 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
a723baf1 9933 cp_parser_enumerator_list (parser, type);
ff4eb0b5
ZW
9934
9935 /* Consume the final '}'. */
a723baf1
MM
9936 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9937
f6af9a15 9938 /* Look for trailing attributes to apply to this enumeration, and
03fd3f84 9939 apply them if appropriate. */
f6af9a15
MA
9940 if (cp_parser_allow_gnu_extensions_p (parser))
9941 {
9942 tree trailing_attr = cp_parser_attributes_opt (parser);
9943 cplus_decl_attributes (&type,
9944 trailing_attr,
9945 (int) ATTR_FLAG_TYPE_IN_PLACE);
9946 }
9947
a723baf1
MM
9948 /* Finish up the enumeration. */
9949 finish_enum (type);
9950
9951 return type;
9952}
9953
9954/* Parse an enumerator-list. The enumerators all have the indicated
21526606 9955 TYPE.
a723baf1
MM
9956
9957 enumerator-list:
9958 enumerator-definition
9959 enumerator-list , enumerator-definition */
9960
9961static void
94edc4ab 9962cp_parser_enumerator_list (cp_parser* parser, tree type)
a723baf1
MM
9963{
9964 while (true)
9965 {
a723baf1
MM
9966 /* Parse an enumerator-definition. */
9967 cp_parser_enumerator_definition (parser, type);
ff4eb0b5
ZW
9968
9969 /* If the next token is not a ',', we've reached the end of
9970 the list. */
9971 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
a723baf1
MM
9972 break;
9973 /* Otherwise, consume the `,' and keep going. */
9974 cp_lexer_consume_token (parser->lexer);
9975 /* If the next token is a `}', there is a trailing comma. */
9976 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9977 {
9978 if (pedantic && !in_system_header)
9979 pedwarn ("comma at end of enumerator list");
9980 break;
9981 }
9982 }
9983}
9984
9985/* Parse an enumerator-definition. The enumerator has the indicated
9986 TYPE.
9987
9988 enumerator-definition:
9989 enumerator
9990 enumerator = constant-expression
21526606 9991
a723baf1
MM
9992 enumerator:
9993 identifier */
9994
9995static void
94edc4ab 9996cp_parser_enumerator_definition (cp_parser* parser, tree type)
a723baf1 9997{
a723baf1
MM
9998 tree identifier;
9999 tree value;
10000
10001 /* Look for the identifier. */
10002 identifier = cp_parser_identifier (parser);
10003 if (identifier == error_mark_node)
10004 return;
21526606 10005
ff4eb0b5
ZW
10006 /* If the next token is an '=', then there is an explicit value. */
10007 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
a723baf1
MM
10008 {
10009 /* Consume the `=' token. */
10010 cp_lexer_consume_token (parser->lexer);
10011 /* Parse the value. */
21526606 10012 value = cp_parser_constant_expression (parser,
d17811fd 10013 /*allow_non_constant_p=*/false,
14d22dd6 10014 NULL);
a723baf1
MM
10015 }
10016 else
10017 value = NULL_TREE;
10018
10019 /* Create the enumerator. */
10020 build_enumerator (identifier, value, type);
10021}
10022
10023/* Parse a namespace-name.
10024
10025 namespace-name:
10026 original-namespace-name
10027 namespace-alias
10028
10029 Returns the NAMESPACE_DECL for the namespace. */
10030
10031static tree
94edc4ab 10032cp_parser_namespace_name (cp_parser* parser)
a723baf1
MM
10033{
10034 tree identifier;
10035 tree namespace_decl;
10036
10037 /* Get the name of the namespace. */
10038 identifier = cp_parser_identifier (parser);
10039 if (identifier == error_mark_node)
10040 return error_mark_node;
10041
eea9800f
MM
10042 /* Look up the identifier in the currently active scope. Look only
10043 for namespaces, due to:
10044
10045 [basic.lookup.udir]
10046
10047 When looking up a namespace-name in a using-directive or alias
21526606 10048 definition, only namespace names are considered.
eea9800f
MM
10049
10050 And:
10051
10052 [basic.lookup.qual]
10053
10054 During the lookup of a name preceding the :: scope resolution
21526606 10055 operator, object, function, and enumerator names are ignored.
eea9800f
MM
10056
10057 (Note that cp_parser_class_or_namespace_name only calls this
10058 function if the token after the name is the scope resolution
10059 operator.) */
10060 namespace_decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 10061 none_type,
b0bc6e8e 10062 /*is_template=*/false,
eea9800f 10063 /*is_namespace=*/true,
8f78f01f
MM
10064 /*check_dependency=*/true,
10065 /*ambiguous_p=*/NULL);
a723baf1
MM
10066 /* If it's not a namespace, issue an error. */
10067 if (namespace_decl == error_mark_node
10068 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10069 {
10070 cp_parser_error (parser, "expected namespace-name");
10071 namespace_decl = error_mark_node;
10072 }
21526606 10073
a723baf1
MM
10074 return namespace_decl;
10075}
10076
10077/* Parse a namespace-definition.
10078
10079 namespace-definition:
10080 named-namespace-definition
21526606 10081 unnamed-namespace-definition
a723baf1
MM
10082
10083 named-namespace-definition:
10084 original-namespace-definition
10085 extension-namespace-definition
10086
10087 original-namespace-definition:
10088 namespace identifier { namespace-body }
21526606 10089
a723baf1
MM
10090 extension-namespace-definition:
10091 namespace original-namespace-name { namespace-body }
21526606 10092
a723baf1
MM
10093 unnamed-namespace-definition:
10094 namespace { namespace-body } */
10095
10096static void
94edc4ab 10097cp_parser_namespace_definition (cp_parser* parser)
a723baf1
MM
10098{
10099 tree identifier;
10100
10101 /* Look for the `namespace' keyword. */
10102 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10103
10104 /* Get the name of the namespace. We do not attempt to distinguish
10105 between an original-namespace-definition and an
10106 extension-namespace-definition at this point. The semantic
10107 analysis routines are responsible for that. */
10108 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10109 identifier = cp_parser_identifier (parser);
10110 else
10111 identifier = NULL_TREE;
10112
10113 /* Look for the `{' to start the namespace. */
10114 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10115 /* Start the namespace. */
10116 push_namespace (identifier);
10117 /* Parse the body of the namespace. */
10118 cp_parser_namespace_body (parser);
10119 /* Finish the namespace. */
10120 pop_namespace ();
10121 /* Look for the final `}'. */
10122 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10123}
10124
10125/* Parse a namespace-body.
10126
10127 namespace-body:
10128 declaration-seq [opt] */
10129
10130static void
94edc4ab 10131cp_parser_namespace_body (cp_parser* parser)
a723baf1
MM
10132{
10133 cp_parser_declaration_seq_opt (parser);
10134}
10135
10136/* Parse a namespace-alias-definition.
10137
10138 namespace-alias-definition:
10139 namespace identifier = qualified-namespace-specifier ; */
10140
10141static void
94edc4ab 10142cp_parser_namespace_alias_definition (cp_parser* parser)
a723baf1
MM
10143{
10144 tree identifier;
10145 tree namespace_specifier;
10146
10147 /* Look for the `namespace' keyword. */
10148 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10149 /* Look for the identifier. */
10150 identifier = cp_parser_identifier (parser);
10151 if (identifier == error_mark_node)
10152 return;
10153 /* Look for the `=' token. */
10154 cp_parser_require (parser, CPP_EQ, "`='");
10155 /* Look for the qualified-namespace-specifier. */
21526606 10156 namespace_specifier
a723baf1
MM
10157 = cp_parser_qualified_namespace_specifier (parser);
10158 /* Look for the `;' token. */
10159 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10160
10161 /* Register the alias in the symbol table. */
10162 do_namespace_alias (identifier, namespace_specifier);
10163}
10164
10165/* Parse a qualified-namespace-specifier.
10166
10167 qualified-namespace-specifier:
10168 :: [opt] nested-name-specifier [opt] namespace-name
10169
10170 Returns a NAMESPACE_DECL corresponding to the specified
10171 namespace. */
10172
10173static tree
94edc4ab 10174cp_parser_qualified_namespace_specifier (cp_parser* parser)
a723baf1
MM
10175{
10176 /* Look for the optional `::'. */
21526606 10177 cp_parser_global_scope_opt (parser,
a723baf1
MM
10178 /*current_scope_valid_p=*/false);
10179
10180 /* Look for the optional nested-name-specifier. */
10181 cp_parser_nested_name_specifier_opt (parser,
10182 /*typename_keyword_p=*/false,
10183 /*check_dependency_p=*/true,
a668c6ad
MM
10184 /*type_p=*/false,
10185 /*is_declaration=*/true);
a723baf1
MM
10186
10187 return cp_parser_namespace_name (parser);
10188}
10189
10190/* Parse a using-declaration.
10191
10192 using-declaration:
10193 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10194 using :: unqualified-id ; */
10195
10196static void
94edc4ab 10197cp_parser_using_declaration (cp_parser* parser)
a723baf1
MM
10198{
10199 cp_token *token;
10200 bool typename_p = false;
10201 bool global_scope_p;
10202 tree decl;
10203 tree identifier;
ed5f054f 10204 tree qscope;
a723baf1
MM
10205
10206 /* Look for the `using' keyword. */
10207 cp_parser_require_keyword (parser, RID_USING, "`using'");
21526606 10208
a723baf1
MM
10209 /* Peek at the next token. */
10210 token = cp_lexer_peek_token (parser->lexer);
10211 /* See if it's `typename'. */
10212 if (token->keyword == RID_TYPENAME)
10213 {
10214 /* Remember that we've seen it. */
10215 typename_p = true;
10216 /* Consume the `typename' token. */
10217 cp_lexer_consume_token (parser->lexer);
10218 }
10219
10220 /* Look for the optional global scope qualification. */
21526606 10221 global_scope_p
a723baf1 10222 = (cp_parser_global_scope_opt (parser,
21526606 10223 /*current_scope_valid_p=*/false)
a723baf1
MM
10224 != NULL_TREE);
10225
10226 /* If we saw `typename', or didn't see `::', then there must be a
10227 nested-name-specifier present. */
10228 if (typename_p || !global_scope_p)
21526606 10229 qscope = cp_parser_nested_name_specifier (parser, typename_p,
ed5f054f
AO
10230 /*check_dependency_p=*/true,
10231 /*type_p=*/false,
10232 /*is_declaration=*/true);
a723baf1
MM
10233 /* Otherwise, we could be in either of the two productions. In that
10234 case, treat the nested-name-specifier as optional. */
10235 else
ed5f054f
AO
10236 qscope = cp_parser_nested_name_specifier_opt (parser,
10237 /*typename_keyword_p=*/false,
10238 /*check_dependency_p=*/true,
10239 /*type_p=*/false,
10240 /*is_declaration=*/true);
10241 if (!qscope)
10242 qscope = global_namespace;
a723baf1
MM
10243
10244 /* Parse the unqualified-id. */
21526606 10245 identifier = cp_parser_unqualified_id (parser,
a723baf1 10246 /*template_keyword_p=*/false,
f3c2dfc6
MM
10247 /*check_dependency_p=*/true,
10248 /*declarator_p=*/true);
a723baf1
MM
10249
10250 /* The function we call to handle a using-declaration is different
10251 depending on what scope we are in. */
f3c2dfc6
MM
10252 if (identifier == error_mark_node)
10253 ;
10254 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10255 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10256 /* [namespace.udecl]
10257
10258 A using declaration shall not name a template-id. */
10259 error ("a template-id may not appear in a using-declaration");
a723baf1
MM
10260 else
10261 {
a5201a91 10262 if (at_class_scope_p ())
4eb6d609 10263 {
f3c2dfc6 10264 /* Create the USING_DECL. */
1d786913 10265 decl = do_class_using_decl (parser->scope, identifier);
f3c2dfc6
MM
10266 /* Add it to the list of members in this class. */
10267 finish_member_declaration (decl);
4eb6d609 10268 }
a723baf1 10269 else
f3c2dfc6
MM
10270 {
10271 decl = cp_parser_lookup_name_simple (parser, identifier);
10272 if (decl == error_mark_node)
4bb8ca28 10273 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
a5201a91 10274 else if (!at_namespace_scope_p ())
ed5f054f 10275 do_local_using_decl (decl, qscope, identifier);
f3c2dfc6 10276 else
ed5f054f 10277 do_toplevel_using_decl (decl, qscope, identifier);
f3c2dfc6 10278 }
a723baf1
MM
10279 }
10280
10281 /* Look for the final `;'. */
10282 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10283}
10284
21526606
EC
10285/* Parse a using-directive.
10286
a723baf1
MM
10287 using-directive:
10288 using namespace :: [opt] nested-name-specifier [opt]
10289 namespace-name ; */
10290
10291static void
94edc4ab 10292cp_parser_using_directive (cp_parser* parser)
a723baf1
MM
10293{
10294 tree namespace_decl;
86098eb8 10295 tree attribs;
a723baf1
MM
10296
10297 /* Look for the `using' keyword. */
10298 cp_parser_require_keyword (parser, RID_USING, "`using'");
10299 /* And the `namespace' keyword. */
10300 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10301 /* Look for the optional `::' operator. */
10302 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
34cd5ae7 10303 /* And the optional nested-name-specifier. */
a723baf1
MM
10304 cp_parser_nested_name_specifier_opt (parser,
10305 /*typename_keyword_p=*/false,
10306 /*check_dependency_p=*/true,
a668c6ad
MM
10307 /*type_p=*/false,
10308 /*is_declaration=*/true);
a723baf1
MM
10309 /* Get the namespace being used. */
10310 namespace_decl = cp_parser_namespace_name (parser);
86098eb8
JM
10311 /* And any specified attributes. */
10312 attribs = cp_parser_attributes_opt (parser);
a723baf1 10313 /* Update the symbol table. */
86098eb8 10314 parse_using_directive (namespace_decl, attribs);
a723baf1
MM
10315 /* Look for the final `;'. */
10316 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10317}
10318
10319/* Parse an asm-definition.
10320
10321 asm-definition:
21526606 10322 asm ( string-literal ) ;
a723baf1
MM
10323
10324 GNU Extension:
10325
10326 asm-definition:
10327 asm volatile [opt] ( string-literal ) ;
10328 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10329 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10330 : asm-operand-list [opt] ) ;
21526606
EC
10331 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10332 : asm-operand-list [opt]
a723baf1
MM
10333 : asm-operand-list [opt] ) ; */
10334
10335static void
94edc4ab 10336cp_parser_asm_definition (cp_parser* parser)
a723baf1 10337{
a723baf1
MM
10338 tree string;
10339 tree outputs = NULL_TREE;
10340 tree inputs = NULL_TREE;
10341 tree clobbers = NULL_TREE;
10342 tree asm_stmt;
10343 bool volatile_p = false;
10344 bool extended_p = false;
10345
10346 /* Look for the `asm' keyword. */
10347 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10348 /* See if the next token is `volatile'. */
10349 if (cp_parser_allow_gnu_extensions_p (parser)
10350 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10351 {
10352 /* Remember that we saw the `volatile' keyword. */
10353 volatile_p = true;
10354 /* Consume the token. */
10355 cp_lexer_consume_token (parser->lexer);
10356 }
10357 /* Look for the opening `('. */
c162c75e
MA
10358 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10359 return;
a723baf1 10360 /* Look for the string. */
c162c75e
MA
10361 string = cp_parser_string_literal (parser, false, false);
10362 if (string == error_mark_node)
10363 {
10364 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10365 /*consume_paren=*/true);
10366 return;
10367 }
10368
a723baf1 10369 /* If we're allowing GNU extensions, check for the extended assembly
21526606 10370 syntax. Unfortunately, the `:' tokens need not be separated by
a723baf1
MM
10371 a space in C, and so, for compatibility, we tolerate that here
10372 too. Doing that means that we have to treat the `::' operator as
10373 two `:' tokens. */
10374 if (cp_parser_allow_gnu_extensions_p (parser)
10375 && at_function_scope_p ()
10376 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10377 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10378 {
10379 bool inputs_p = false;
10380 bool clobbers_p = false;
10381
10382 /* The extended syntax was used. */
10383 extended_p = true;
10384
10385 /* Look for outputs. */
10386 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10387 {
10388 /* Consume the `:'. */
10389 cp_lexer_consume_token (parser->lexer);
10390 /* Parse the output-operands. */
21526606 10391 if (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
10392 CPP_COLON)
10393 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
10394 CPP_SCOPE)
10395 && cp_lexer_next_token_is_not (parser->lexer,
10396 CPP_CLOSE_PAREN))
a723baf1
MM
10397 outputs = cp_parser_asm_operand_list (parser);
10398 }
10399 /* If the next token is `::', there are no outputs, and the
10400 next token is the beginning of the inputs. */
10401 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
bf5930d4
JB
10402 /* The inputs are coming next. */
10403 inputs_p = true;
a723baf1
MM
10404
10405 /* Look for inputs. */
10406 if (inputs_p
10407 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10408 {
bf5930d4
JB
10409 /* Consume the `:' or `::'. */
10410 cp_lexer_consume_token (parser->lexer);
a723baf1 10411 /* Parse the output-operands. */
21526606 10412 if (cp_lexer_next_token_is_not (parser->lexer,
a723baf1 10413 CPP_COLON)
8caf4c38
MM
10414 && cp_lexer_next_token_is_not (parser->lexer,
10415 CPP_CLOSE_PAREN))
a723baf1
MM
10416 inputs = cp_parser_asm_operand_list (parser);
10417 }
10418 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10419 /* The clobbers are coming next. */
10420 clobbers_p = true;
10421
10422 /* Look for clobbers. */
21526606 10423 if (clobbers_p
a723baf1
MM
10424 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10425 {
bf5930d4
JB
10426 /* Consume the `:' or `::'. */
10427 cp_lexer_consume_token (parser->lexer);
a723baf1 10428 /* Parse the clobbers. */
8caf4c38
MM
10429 if (cp_lexer_next_token_is_not (parser->lexer,
10430 CPP_CLOSE_PAREN))
10431 clobbers = cp_parser_asm_clobber_list (parser);
a723baf1
MM
10432 }
10433 }
10434 /* Look for the closing `)'. */
10435 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
10436 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10437 /*consume_paren=*/true);
a723baf1
MM
10438 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10439
e130a54b 10440 /* Create the ASM_EXPR. */
a723baf1
MM
10441 if (at_function_scope_p ())
10442 {
6de9cd9a
DN
10443 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10444 inputs, clobbers);
e130a54b 10445 /* If the extended syntax was not used, mark the ASM_EXPR. */
a723baf1 10446 if (!extended_p)
ca059043
AP
10447 {
10448 tree temp = asm_stmt;
10449 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10450 temp = TREE_OPERAND (temp, 0);
10451
10452 ASM_INPUT_P (temp) = 1;
10453 }
a723baf1
MM
10454 }
10455 else
10456 assemble_asm (string);
10457}
10458
10459/* Declarators [gram.dcl.decl] */
10460
10461/* Parse an init-declarator.
10462
10463 init-declarator:
10464 declarator initializer [opt]
10465
10466 GNU Extension:
10467
10468 init-declarator:
10469 declarator asm-specification [opt] attributes [opt] initializer [opt]
10470
4bb8ca28
MM
10471 function-definition:
10472 decl-specifier-seq [opt] declarator ctor-initializer [opt]
21526606
EC
10473 function-body
10474 decl-specifier-seq [opt] declarator function-try-block
4bb8ca28
MM
10475
10476 GNU Extension:
10477
10478 function-definition:
21526606 10479 __extension__ function-definition
4bb8ca28 10480
a723baf1 10481 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
c8e4f0e9 10482 Returns a representation of the entity declared. If MEMBER_P is TRUE,
cf22909c
KL
10483 then this declarator appears in a class scope. The new DECL created
10484 by this declarator is returned.
a723baf1
MM
10485
10486 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10487 for a function-definition here as well. If the declarator is a
10488 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10489 be TRUE upon return. By that point, the function-definition will
10490 have been completely parsed.
10491
10492 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10493 is FALSE. */
10494
10495static tree
21526606 10496cp_parser_init_declarator (cp_parser* parser,
62d1db17 10497 cp_decl_specifier_seq *decl_specifiers,
94edc4ab
NN
10498 bool function_definition_allowed_p,
10499 bool member_p,
560ad596 10500 int declares_class_or_enum,
94edc4ab 10501 bool* function_definition_p)
a723baf1
MM
10502{
10503 cp_token *token;
058b15c1 10504 cp_declarator *declarator;
62d1db17 10505 tree prefix_attributes;
a723baf1
MM
10506 tree attributes;
10507 tree asm_specification;
10508 tree initializer;
10509 tree decl = NULL_TREE;
10510 tree scope;
a723baf1
MM
10511 bool is_initialized;
10512 bool is_parenthesized_init;
39703eb9 10513 bool is_non_constant_init;
7efa3e22 10514 int ctor_dtor_or_conv_p;
a723baf1 10515 bool friend_p;
4514aa8c 10516 tree pushed_scope = NULL;
a723baf1 10517
62d1db17
MM
10518 /* Gather the attributes that were provided with the
10519 decl-specifiers. */
10520 prefix_attributes = decl_specifiers->attributes;
62d1db17 10521
a723baf1
MM
10522 /* Assume that this is not the declarator for a function
10523 definition. */
10524 if (function_definition_p)
10525 *function_definition_p = false;
10526
10527 /* Defer access checks while parsing the declarator; we cannot know
21526606 10528 what names are accessible until we know what is being
a723baf1 10529 declared. */
cf22909c
KL
10530 resume_deferring_access_checks ();
10531
a723baf1 10532 /* Parse the declarator. */
21526606 10533 declarator
62b8a44e 10534 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 10535 &ctor_dtor_or_conv_p,
db86dd14
MM
10536 /*parenthesized_p=*/NULL,
10537 /*member_p=*/false);
a723baf1 10538 /* Gather up the deferred checks. */
cf22909c 10539 stop_deferring_access_checks ();
24c0ef37 10540
a723baf1
MM
10541 /* If the DECLARATOR was erroneous, there's no need to go
10542 further. */
058b15c1 10543 if (declarator == cp_error_declarator)
cf22909c 10544 return error_mark_node;
a723baf1 10545
fc6a28d7
MM
10546 if (declares_class_or_enum & 2)
10547 cp_parser_check_for_definition_in_return_type (declarator,
10548 decl_specifiers->type);
560ad596 10549
a723baf1
MM
10550 /* Figure out what scope the entity declared by the DECLARATOR is
10551 located in. `grokdeclarator' sometimes changes the scope, so
10552 we compute it now. */
10553 scope = get_scope_of_declarator (declarator);
10554
10555 /* If we're allowing GNU extensions, look for an asm-specification
10556 and attributes. */
10557 if (cp_parser_allow_gnu_extensions_p (parser))
10558 {
10559 /* Look for an asm-specification. */
10560 asm_specification = cp_parser_asm_specification_opt (parser);
10561 /* And attributes. */
10562 attributes = cp_parser_attributes_opt (parser);
10563 }
10564 else
10565 {
10566 asm_specification = NULL_TREE;
10567 attributes = NULL_TREE;
10568 }
10569
10570 /* Peek at the next token. */
10571 token = cp_lexer_peek_token (parser->lexer);
10572 /* Check to see if the token indicates the start of a
10573 function-definition. */
10574 if (cp_parser_token_starts_function_definition_p (token))
10575 {
10576 if (!function_definition_allowed_p)
10577 {
10578 /* If a function-definition should not appear here, issue an
10579 error message. */
10580 cp_parser_error (parser,
10581 "a function-definition is not allowed here");
10582 return error_mark_node;
10583 }
10584 else
10585 {
a723baf1
MM
10586 /* Neither attributes nor an asm-specification are allowed
10587 on a function-definition. */
10588 if (asm_specification)
10589 error ("an asm-specification is not allowed on a function-definition");
10590 if (attributes)
10591 error ("attributes are not allowed on a function-definition");
10592 /* This is a function-definition. */
10593 *function_definition_p = true;
10594
a723baf1 10595 /* Parse the function definition. */
4bb8ca28
MM
10596 if (member_p)
10597 decl = cp_parser_save_member_function_body (parser,
10598 decl_specifiers,
10599 declarator,
10600 prefix_attributes);
10601 else
21526606 10602 decl
4bb8ca28
MM
10603 = (cp_parser_function_definition_from_specifiers_and_declarator
10604 (parser, decl_specifiers, prefix_attributes, declarator));
24c0ef37 10605
a723baf1
MM
10606 return decl;
10607 }
10608 }
10609
10610 /* [dcl.dcl]
10611
10612 Only in function declarations for constructors, destructors, and
21526606 10613 type conversions can the decl-specifier-seq be omitted.
a723baf1
MM
10614
10615 We explicitly postpone this check past the point where we handle
10616 function-definitions because we tolerate function-definitions
10617 that are missing their return types in some modes. */
62d1db17 10618 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
a723baf1 10619 {
21526606 10620 cp_parser_error (parser,
a723baf1
MM
10621 "expected constructor, destructor, or type conversion");
10622 return error_mark_node;
10623 }
10624
10625 /* An `=' or an `(' indicates an initializer. */
21526606 10626 is_initialized = (token->type == CPP_EQ
a723baf1
MM
10627 || token->type == CPP_OPEN_PAREN);
10628 /* If the init-declarator isn't initialized and isn't followed by a
10629 `,' or `;', it's not a valid init-declarator. */
21526606 10630 if (!is_initialized
a723baf1
MM
10631 && token->type != CPP_COMMA
10632 && token->type != CPP_SEMICOLON)
10633 {
996c2b52 10634 cp_parser_error (parser, "expected initializer");
a723baf1
MM
10635 return error_mark_node;
10636 }
10637
10638 /* Because start_decl has side-effects, we should only call it if we
10639 know we're going ahead. By this point, we know that we cannot
10640 possibly be looking at any other construct. */
10641 cp_parser_commit_to_tentative_parse (parser);
10642
e90c7b84
ILT
10643 /* If the decl specifiers were bad, issue an error now that we're
10644 sure this was intended to be a declarator. Then continue
10645 declaring the variable(s), as int, to try to cut down on further
10646 errors. */
62d1db17
MM
10647 if (decl_specifiers->any_specifiers_p
10648 && decl_specifiers->type == error_mark_node)
e90c7b84
ILT
10649 {
10650 cp_parser_error (parser, "invalid type in declaration");
62d1db17 10651 decl_specifiers->type = integer_type_node;
e90c7b84
ILT
10652 }
10653
a723baf1
MM
10654 /* Check to see whether or not this declaration is a friend. */
10655 friend_p = cp_parser_friend_p (decl_specifiers);
10656
10657 /* Check that the number of template-parameter-lists is OK. */
ee3071ef 10658 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
cf22909c 10659 return error_mark_node;
a723baf1
MM
10660
10661 /* Enter the newly declared entry in the symbol table. If we're
10662 processing a declaration in a class-specifier, we wait until
10663 after processing the initializer. */
10664 if (!member_p)
10665 {
10666 if (parser->in_unbraced_linkage_specification_p)
10667 {
62d1db17 10668 decl_specifiers->storage_class = sc_extern;
a723baf1
MM
10669 have_extern_spec = false;
10670 }
ee3071ef 10671 decl = start_decl (declarator, decl_specifiers,
73a8adb6 10672 is_initialized, attributes, prefix_attributes,
4514aa8c 10673 &pushed_scope);
a723baf1 10674 }
73a8adb6
MM
10675 else if (scope)
10676 /* Enter the SCOPE. That way unqualified names appearing in the
10677 initializer will be looked up in SCOPE. */
4514aa8c 10678 pushed_scope = push_scope (scope);
a723baf1
MM
10679
10680 /* Perform deferred access control checks, now that we know in which
10681 SCOPE the declared entity resides. */
21526606 10682 if (!member_p && decl)
a723baf1
MM
10683 {
10684 tree saved_current_function_decl = NULL_TREE;
10685
10686 /* If the entity being declared is a function, pretend that we
10687 are in its scope. If it is a `friend', it may have access to
9bcb9aae 10688 things that would not otherwise be accessible. */
a723baf1
MM
10689 if (TREE_CODE (decl) == FUNCTION_DECL)
10690 {
10691 saved_current_function_decl = current_function_decl;
10692 current_function_decl = decl;
10693 }
21526606 10694
cf22909c
KL
10695 /* Perform the access control checks for the declarator and the
10696 the decl-specifiers. */
10697 perform_deferred_access_checks ();
a723baf1
MM
10698
10699 /* Restore the saved value. */
10700 if (TREE_CODE (decl) == FUNCTION_DECL)
10701 current_function_decl = saved_current_function_decl;
10702 }
10703
10704 /* Parse the initializer. */
10705 if (is_initialized)
21526606 10706 initializer = cp_parser_initializer (parser,
39703eb9
MM
10707 &is_parenthesized_init,
10708 &is_non_constant_init);
a723baf1
MM
10709 else
10710 {
10711 initializer = NULL_TREE;
10712 is_parenthesized_init = false;
39703eb9 10713 is_non_constant_init = true;
a723baf1
MM
10714 }
10715
10716 /* The old parser allows attributes to appear after a parenthesized
10717 initializer. Mark Mitchell proposed removing this functionality
10718 on the GCC mailing lists on 2002-08-13. This parser accepts the
10719 attributes -- but ignores them. */
10720 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10721 if (cp_parser_attributes_opt (parser))
10722 warning ("attributes after parenthesized initializer ignored");
10723
a723baf1
MM
10724 /* For an in-class declaration, use `grokfield' to create the
10725 declaration. */
10726 if (member_p)
8db1028e 10727 {
4514aa8c 10728 if (pushed_scope)
62a4d942 10729 {
4514aa8c
NS
10730 pop_scope (pushed_scope);
10731 pushed_scope = false;
62a4d942 10732 }
8db1028e
NS
10733 decl = grokfield (declarator, decl_specifiers,
10734 initializer, /*asmspec=*/NULL_TREE,
a723baf1 10735 /*attributes=*/NULL_TREE);
8db1028e
NS
10736 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10737 cp_parser_save_default_args (parser, decl);
10738 }
21526606 10739
a723baf1
MM
10740 /* Finish processing the declaration. But, skip friend
10741 declarations. */
550205c3 10742 if (!friend_p && decl && decl != error_mark_node)
73a8adb6
MM
10743 {
10744 cp_finish_decl (decl,
10745 initializer,
10746 asm_specification,
10747 /* If the initializer is in parentheses, then this is
10748 a direct-initialization, which means that an
10749 `explicit' constructor is OK. Otherwise, an
10750 `explicit' constructor cannot be used. */
10751 ((is_parenthesized_init || !is_initialized)
a723baf1 10752 ? 0 : LOOKUP_ONLYCONVERTING));
73a8adb6 10753 }
4514aa8c
NS
10754 if (!friend_p && pushed_scope)
10755 pop_scope (pushed_scope);
a723baf1 10756
39703eb9
MM
10757 /* Remember whether or not variables were initialized by
10758 constant-expressions. */
21526606 10759 if (decl && TREE_CODE (decl) == VAR_DECL
39703eb9
MM
10760 && is_initialized && !is_non_constant_init)
10761 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10762
a723baf1
MM
10763 return decl;
10764}
10765
10766/* Parse a declarator.
21526606 10767
a723baf1
MM
10768 declarator:
10769 direct-declarator
21526606 10770 ptr-operator declarator
a723baf1
MM
10771
10772 abstract-declarator:
10773 ptr-operator abstract-declarator [opt]
10774 direct-abstract-declarator
10775
10776 GNU Extensions:
10777
10778 declarator:
10779 attributes [opt] direct-declarator
21526606 10780 attributes [opt] ptr-operator declarator
a723baf1
MM
10781
10782 abstract-declarator:
10783 attributes [opt] ptr-operator abstract-declarator [opt]
10784 attributes [opt] direct-abstract-declarator
21526606 10785
7efa3e22
NS
10786 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10787 detect constructor, destructor or conversion operators. It is set
10788 to -1 if the declarator is a name, and +1 if it is a
10789 function. Otherwise it is set to zero. Usually you just want to
10790 test for >0, but internally the negative value is used.
21526606 10791
a723baf1
MM
10792 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10793 a decl-specifier-seq unless it declares a constructor, destructor,
10794 or conversion. It might seem that we could check this condition in
10795 semantic analysis, rather than parsing, but that makes it difficult
10796 to handle something like `f()'. We want to notice that there are
10797 no decl-specifiers, and therefore realize that this is an
21526606
EC
10798 expression, not a declaration.)
10799
4bb8ca28 10800 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
db86dd14
MM
10801 the declarator is a direct-declarator of the form "(...)".
10802
10803 MEMBER_P is true iff this declarator is a member-declarator. */
a723baf1 10804
058b15c1 10805static cp_declarator *
21526606
EC
10806cp_parser_declarator (cp_parser* parser,
10807 cp_parser_declarator_kind dcl_kind,
4bb8ca28 10808 int* ctor_dtor_or_conv_p,
db86dd14
MM
10809 bool* parenthesized_p,
10810 bool member_p)
a723baf1
MM
10811{
10812 cp_token *token;
058b15c1 10813 cp_declarator *declarator;
a723baf1 10814 enum tree_code code;
3c01e5df 10815 cp_cv_quals cv_quals;
a723baf1
MM
10816 tree class_type;
10817 tree attributes = NULL_TREE;
10818
10819 /* Assume this is not a constructor, destructor, or type-conversion
10820 operator. */
10821 if (ctor_dtor_or_conv_p)
7efa3e22 10822 *ctor_dtor_or_conv_p = 0;
a723baf1
MM
10823
10824 if (cp_parser_allow_gnu_extensions_p (parser))
10825 attributes = cp_parser_attributes_opt (parser);
21526606 10826
a723baf1
MM
10827 /* Peek at the next token. */
10828 token = cp_lexer_peek_token (parser->lexer);
21526606 10829
a723baf1
MM
10830 /* Check for the ptr-operator production. */
10831 cp_parser_parse_tentatively (parser);
10832 /* Parse the ptr-operator. */
21526606
EC
10833 code = cp_parser_ptr_operator (parser,
10834 &class_type,
3c01e5df 10835 &cv_quals);
a723baf1
MM
10836 /* If that worked, then we have a ptr-operator. */
10837 if (cp_parser_parse_definitely (parser))
10838 {
4bb8ca28
MM
10839 /* If a ptr-operator was found, then this declarator was not
10840 parenthesized. */
10841 if (parenthesized_p)
10842 *parenthesized_p = true;
a723baf1
MM
10843 /* The dependent declarator is optional if we are parsing an
10844 abstract-declarator. */
62b8a44e 10845 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1
MM
10846 cp_parser_parse_tentatively (parser);
10847
10848 /* Parse the dependent declarator. */
62b8a44e 10849 declarator = cp_parser_declarator (parser, dcl_kind,
4bb8ca28 10850 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
10851 /*parenthesized_p=*/NULL,
10852 /*member_p=*/false);
a723baf1
MM
10853
10854 /* If we are parsing an abstract-declarator, we must handle the
10855 case where the dependent declarator is absent. */
62b8a44e
NS
10856 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10857 && !cp_parser_parse_definitely (parser))
058b15c1 10858 declarator = NULL;
21526606 10859
a723baf1 10860 /* Build the representation of the ptr-operator. */
058b15c1 10861 if (class_type)
3c01e5df 10862 declarator = make_ptrmem_declarator (cv_quals,
058b15c1
MM
10863 class_type,
10864 declarator);
10865 else if (code == INDIRECT_REF)
3c01e5df 10866 declarator = make_pointer_declarator (cv_quals, declarator);
a723baf1 10867 else
3c01e5df 10868 declarator = make_reference_declarator (cv_quals, declarator);
a723baf1
MM
10869 }
10870 /* Everything else is a direct-declarator. */
10871 else
4bb8ca28
MM
10872 {
10873 if (parenthesized_p)
10874 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10875 CPP_OPEN_PAREN);
10876 declarator = cp_parser_direct_declarator (parser, dcl_kind,
db86dd14
MM
10877 ctor_dtor_or_conv_p,
10878 member_p);
4bb8ca28 10879 }
a723baf1 10880
058b15c1
MM
10881 if (attributes && declarator != cp_error_declarator)
10882 declarator->attributes = attributes;
21526606 10883
a723baf1
MM
10884 return declarator;
10885}
10886
10887/* Parse a direct-declarator or direct-abstract-declarator.
10888
10889 direct-declarator:
10890 declarator-id
10891 direct-declarator ( parameter-declaration-clause )
21526606 10892 cv-qualifier-seq [opt]
a723baf1
MM
10893 exception-specification [opt]
10894 direct-declarator [ constant-expression [opt] ]
21526606 10895 ( declarator )
a723baf1
MM
10896
10897 direct-abstract-declarator:
10898 direct-abstract-declarator [opt]
21526606 10899 ( parameter-declaration-clause )
a723baf1
MM
10900 cv-qualifier-seq [opt]
10901 exception-specification [opt]
10902 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10903 ( abstract-declarator )
10904
62b8a44e
NS
10905 Returns a representation of the declarator. DCL_KIND is
10906 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10907 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10908 we are parsing a direct-declarator. It is
10909 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10910 of ambiguity we prefer an abstract declarator, as per
db86dd14 10911 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
058b15c1 10912 cp_parser_declarator. */
a723baf1 10913
058b15c1 10914static cp_declarator *
94edc4ab
NN
10915cp_parser_direct_declarator (cp_parser* parser,
10916 cp_parser_declarator_kind dcl_kind,
db86dd14
MM
10917 int* ctor_dtor_or_conv_p,
10918 bool member_p)
a723baf1
MM
10919{
10920 cp_token *token;
058b15c1 10921 cp_declarator *declarator = NULL;
a723baf1
MM
10922 tree scope = NULL_TREE;
10923 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10924 bool saved_in_declarator_p = parser->in_declarator_p;
62b8a44e 10925 bool first = true;
4514aa8c 10926 tree pushed_scope = NULL_TREE;
21526606 10927
62b8a44e 10928 while (true)
a723baf1 10929 {
62b8a44e
NS
10930 /* Peek at the next token. */
10931 token = cp_lexer_peek_token (parser->lexer);
10932 if (token->type == CPP_OPEN_PAREN)
a723baf1 10933 {
62b8a44e
NS
10934 /* This is either a parameter-declaration-clause, or a
10935 parenthesized declarator. When we know we are parsing a
34cd5ae7 10936 named declarator, it must be a parenthesized declarator
62b8a44e
NS
10937 if FIRST is true. For instance, `(int)' is a
10938 parameter-declaration-clause, with an omitted
10939 direct-abstract-declarator. But `((*))', is a
10940 parenthesized abstract declarator. Finally, when T is a
10941 template parameter `(T)' is a
34cd5ae7 10942 parameter-declaration-clause, and not a parenthesized
62b8a44e 10943 named declarator.
21526606 10944
62b8a44e
NS
10945 We first try and parse a parameter-declaration-clause,
10946 and then try a nested declarator (if FIRST is true).
a723baf1 10947
62b8a44e
NS
10948 It is not an error for it not to be a
10949 parameter-declaration-clause, even when FIRST is
10950 false. Consider,
10951
10952 int i (int);
10953 int i (3);
10954
10955 The first is the declaration of a function while the
10956 second is a the definition of a variable, including its
10957 initializer.
10958
10959 Having seen only the parenthesis, we cannot know which of
10960 these two alternatives should be selected. Even more
10961 complex are examples like:
10962
10963 int i (int (a));
10964 int i (int (3));
10965
10966 The former is a function-declaration; the latter is a
21526606 10967 variable initialization.
62b8a44e 10968
34cd5ae7 10969 Thus again, we try a parameter-declaration-clause, and if
62b8a44e
NS
10970 that fails, we back out and return. */
10971
10972 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1 10973 {
058b15c1 10974 cp_parameter_declarator *params;
4047b164 10975 unsigned saved_num_template_parameter_lists;
21526606 10976
db86dd14
MM
10977 /* In a member-declarator, the only valid interpretation
10978 of a parenthesis is the start of a
10979 parameter-declaration-clause. (It is invalid to
10980 initialize a static data member with a parenthesized
10981 initializer; only the "=" form of initialization is
10982 permitted.) */
10983 if (!member_p)
10984 cp_parser_parse_tentatively (parser);
a723baf1 10985
62b8a44e
NS
10986 /* Consume the `('. */
10987 cp_lexer_consume_token (parser->lexer);
10988 if (first)
10989 {
10990 /* If this is going to be an abstract declarator, we're
10991 in a declarator and we can't have default args. */
10992 parser->default_arg_ok_p = false;
10993 parser->in_declarator_p = true;
10994 }
21526606 10995
4047b164
KL
10996 /* Inside the function parameter list, surrounding
10997 template-parameter-lists do not apply. */
10998 saved_num_template_parameter_lists
10999 = parser->num_template_parameter_lists;
11000 parser->num_template_parameter_lists = 0;
11001
62b8a44e
NS
11002 /* Parse the parameter-declaration-clause. */
11003 params = cp_parser_parameter_declaration_clause (parser);
11004
4047b164
KL
11005 parser->num_template_parameter_lists
11006 = saved_num_template_parameter_lists;
11007
62b8a44e 11008 /* If all went well, parse the cv-qualifier-seq and the
34cd5ae7 11009 exception-specification. */
db86dd14 11010 if (member_p || cp_parser_parse_definitely (parser))
62b8a44e 11011 {
3c01e5df 11012 cp_cv_quals cv_quals;
62b8a44e 11013 tree exception_specification;
7efa3e22
NS
11014
11015 if (ctor_dtor_or_conv_p)
11016 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
62b8a44e
NS
11017 first = false;
11018 /* Consume the `)'. */
11019 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11020
11021 /* Parse the cv-qualifier-seq. */
3c01e5df 11022 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
62b8a44e 11023 /* And the exception-specification. */
21526606 11024 exception_specification
62b8a44e
NS
11025 = cp_parser_exception_specification_opt (parser);
11026
11027 /* Create the function-declarator. */
11028 declarator = make_call_declarator (declarator,
11029 params,
3c01e5df 11030 cv_quals,
62b8a44e
NS
11031 exception_specification);
11032 /* Any subsequent parameter lists are to do with
11033 return type, so are not those of the declared
11034 function. */
11035 parser->default_arg_ok_p = false;
21526606 11036
62b8a44e
NS
11037 /* Repeat the main loop. */
11038 continue;
11039 }
11040 }
21526606 11041
62b8a44e
NS
11042 /* If this is the first, we can try a parenthesized
11043 declarator. */
11044 if (first)
a723baf1 11045 {
a7324e75
MM
11046 bool saved_in_type_id_in_expr_p;
11047
a723baf1 11048 parser->default_arg_ok_p = saved_default_arg_ok_p;
62b8a44e 11049 parser->in_declarator_p = saved_in_declarator_p;
21526606 11050
62b8a44e
NS
11051 /* Consume the `('. */
11052 cp_lexer_consume_token (parser->lexer);
11053 /* Parse the nested declarator. */
a7324e75
MM
11054 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11055 parser->in_type_id_in_expr_p = true;
21526606 11056 declarator
4bb8ca28 11057 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
db86dd14
MM
11058 /*parenthesized_p=*/NULL,
11059 member_p);
a7324e75 11060 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
62b8a44e
NS
11061 first = false;
11062 /* Expect a `)'. */
11063 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
058b15c1
MM
11064 declarator = cp_error_declarator;
11065 if (declarator == cp_error_declarator)
62b8a44e 11066 break;
21526606 11067
62b8a44e 11068 goto handle_declarator;
a723baf1 11069 }
9bcb9aae 11070 /* Otherwise, we must be done. */
62b8a44e
NS
11071 else
11072 break;
a723baf1 11073 }
62b8a44e
NS
11074 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11075 && token->type == CPP_OPEN_SQUARE)
a723baf1 11076 {
62b8a44e 11077 /* Parse an array-declarator. */
a723baf1
MM
11078 tree bounds;
11079
7efa3e22
NS
11080 if (ctor_dtor_or_conv_p)
11081 *ctor_dtor_or_conv_p = 0;
21526606 11082
62b8a44e
NS
11083 first = false;
11084 parser->default_arg_ok_p = false;
11085 parser->in_declarator_p = true;
a723baf1
MM
11086 /* Consume the `['. */
11087 cp_lexer_consume_token (parser->lexer);
11088 /* Peek at the next token. */
11089 token = cp_lexer_peek_token (parser->lexer);
11090 /* If the next token is `]', then there is no
11091 constant-expression. */
11092 if (token->type != CPP_CLOSE_SQUARE)
14d22dd6
MM
11093 {
11094 bool non_constant_p;
11095
21526606 11096 bounds
14d22dd6
MM
11097 = cp_parser_constant_expression (parser,
11098 /*allow_non_constant=*/true,
11099 &non_constant_p);
d17811fd 11100 if (!non_constant_p)
9baa27a9 11101 bounds = fold_non_dependent_expr (bounds);
44370687
MM
11102 else if (!at_function_scope_p ())
11103 {
11104 error ("array bound is not an integer constant");
11105 bounds = error_mark_node;
11106 }
14d22dd6 11107 }
a723baf1
MM
11108 else
11109 bounds = NULL_TREE;
11110 /* Look for the closing `]'. */
62b8a44e
NS
11111 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11112 {
058b15c1 11113 declarator = cp_error_declarator;
62b8a44e
NS
11114 break;
11115 }
a723baf1 11116
058b15c1 11117 declarator = make_array_declarator (declarator, bounds);
a723baf1 11118 }
62b8a44e 11119 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
a723baf1 11120 {
1d786913
MM
11121 tree qualifying_scope;
11122 tree unqualified_name;
058b15c1 11123
a668c6ad 11124 /* Parse a declarator-id */
62b8a44e
NS
11125 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11126 cp_parser_parse_tentatively (parser);
1d786913
MM
11127 unqualified_name = cp_parser_declarator_id (parser);
11128 qualifying_scope = parser->scope;
712becab
NS
11129 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11130 {
11131 if (!cp_parser_parse_definitely (parser))
1d786913
MM
11132 unqualified_name = error_mark_node;
11133 else if (qualifying_scope
11134 || (TREE_CODE (unqualified_name)
11135 != IDENTIFIER_NODE))
712becab
NS
11136 {
11137 cp_parser_error (parser, "expected unqualified-id");
1d786913 11138 unqualified_name = error_mark_node;
712becab
NS
11139 }
11140 }
21526606 11141
1d786913 11142 if (unqualified_name == error_mark_node)
058b15c1
MM
11143 {
11144 declarator = cp_error_declarator;
11145 break;
11146 }
21526606 11147
1d786913
MM
11148 if (qualifying_scope && at_namespace_scope_p ()
11149 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
62b8a44e 11150 {
62b8a44e
NS
11151 /* In the declaration of a member of a template class
11152 outside of the class itself, the SCOPE will sometimes
11153 be a TYPENAME_TYPE. For example, given:
21526606 11154
62b8a44e
NS
11155 template <typename T>
11156 int S<T>::R::i = 3;
21526606 11157
62b8a44e
NS
11158 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11159 this context, we must resolve S<T>::R to an ordinary
11160 type, rather than a typename type.
21526606 11161
62b8a44e
NS
11162 The reason we normally avoid resolving TYPENAME_TYPEs
11163 is that a specialization of `S' might render
11164 `S<T>::R' not a type. However, if `S' is
11165 specialized, then this `i' will not be used, so there
11166 is no harm in resolving the types here. */
1d786913
MM
11167 tree type;
11168
11169 /* Resolve the TYPENAME_TYPE. */
11170 type = resolve_typename_type (qualifying_scope,
11171 /*only_current_p=*/false);
11172 /* If that failed, the declarator is invalid. */
11173 if (type == error_mark_node)
11174 error ("%<%T::%D%> is not a type",
11175 TYPE_CONTEXT (qualifying_scope),
11176 TYPE_IDENTIFIER (qualifying_scope));
11177 qualifying_scope = type;
62b8a44e 11178 }
21526606 11179
1d786913
MM
11180 declarator = make_id_declarator (qualifying_scope,
11181 unqualified_name);
11182 if (unqualified_name)
a723baf1 11183 {
62b8a44e
NS
11184 tree class_type;
11185
1d786913
MM
11186 if (qualifying_scope
11187 && CLASS_TYPE_P (qualifying_scope))
11188 class_type = qualifying_scope;
62b8a44e 11189 else
1d786913 11190 class_type = current_class_type;
62b8a44e 11191
058b15c1
MM
11192 if (class_type)
11193 {
11194 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11195 declarator->u.id.sfk = sfk_destructor;
11196 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11197 declarator->u.id.sfk = sfk_conversion;
27d6592c
MM
11198 else if (/* There's no way to declare a constructor
11199 for an anonymous type, even if the type
11200 got a name for linkage purposes. */
11201 !TYPE_WAS_ANONYMOUS (class_type)
11202 && (constructor_name_p (unqualified_name,
11203 class_type)
11204 || (TREE_CODE (unqualified_name) == TYPE_DECL
11205 && (same_type_p
11206 (TREE_TYPE (unqualified_name),
11207 class_type)))))
058b15c1
MM
11208 declarator->u.id.sfk = sfk_constructor;
11209
11210 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11211 *ctor_dtor_or_conv_p = -1;
1d786913 11212 if (qualifying_scope
98ca843c 11213 && TREE_CODE (unqualified_name) == TYPE_DECL
058b15c1
MM
11214 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11215 {
11216 error ("invalid use of constructor as a template");
2a13a625
GDR
11217 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11218 "the constructor in a qualified name",
11219 class_type,
058b15c1
MM
11220 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11221 class_type, class_type);
11222 }
11223 }
a723baf1 11224 }
62b8a44e
NS
11225
11226 handle_declarator:;
11227 scope = get_scope_of_declarator (declarator);
11228 if (scope)
91b004e5
MM
11229 /* Any names that appear after the declarator-id for a
11230 member are looked up in the containing scope. */
4514aa8c 11231 pushed_scope = push_scope (scope);
62b8a44e
NS
11232 parser->in_declarator_p = true;
11233 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
058b15c1 11234 || (declarator && declarator->kind == cdk_id))
62b8a44e
NS
11235 /* Default args are only allowed on function
11236 declarations. */
11237 parser->default_arg_ok_p = saved_default_arg_ok_p;
a723baf1 11238 else
62b8a44e
NS
11239 parser->default_arg_ok_p = false;
11240
11241 first = false;
a723baf1 11242 }
62b8a44e 11243 /* We're done. */
a723baf1
MM
11244 else
11245 break;
a723baf1
MM
11246 }
11247
11248 /* For an abstract declarator, we might wind up with nothing at this
11249 point. That's an error; the declarator is not optional. */
11250 if (!declarator)
11251 cp_parser_error (parser, "expected declarator");
11252
11253 /* If we entered a scope, we must exit it now. */
4514aa8c
NS
11254 if (pushed_scope)
11255 pop_scope (pushed_scope);
a723baf1
MM
11256
11257 parser->default_arg_ok_p = saved_default_arg_ok_p;
11258 parser->in_declarator_p = saved_in_declarator_p;
21526606 11259
a723baf1
MM
11260 return declarator;
11261}
11262
21526606 11263/* Parse a ptr-operator.
a723baf1
MM
11264
11265 ptr-operator:
11266 * cv-qualifier-seq [opt]
11267 &
11268 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11269
11270 GNU Extension:
11271
11272 ptr-operator:
11273 & cv-qualifier-seq [opt]
11274
3c01e5df
MM
11275 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11276 Returns ADDR_EXPR if a reference was used. In the case of a
11277 pointer-to-member, *TYPE is filled in with the TYPE containing the
11278 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11279 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11280 ERROR_MARK if an error occurred. */
21526606 11281
a723baf1 11282static enum tree_code
21526606
EC
11283cp_parser_ptr_operator (cp_parser* parser,
11284 tree* type,
3c01e5df 11285 cp_cv_quals *cv_quals)
a723baf1
MM
11286{
11287 enum tree_code code = ERROR_MARK;
11288 cp_token *token;
11289
11290 /* Assume that it's not a pointer-to-member. */
11291 *type = NULL_TREE;
11292 /* And that there are no cv-qualifiers. */
3c01e5df 11293 *cv_quals = TYPE_UNQUALIFIED;
a723baf1
MM
11294
11295 /* Peek at the next token. */
11296 token = cp_lexer_peek_token (parser->lexer);
11297 /* If it's a `*' or `&' we have a pointer or reference. */
11298 if (token->type == CPP_MULT || token->type == CPP_AND)
11299 {
11300 /* Remember which ptr-operator we were processing. */
11301 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11302
11303 /* Consume the `*' or `&'. */
11304 cp_lexer_consume_token (parser->lexer);
11305
11306 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11307 `&', if we are allowing GNU extensions. (The only qualifier
11308 that can legally appear after `&' is `restrict', but that is
11309 enforced during semantic analysis. */
21526606 11310 if (code == INDIRECT_REF
a723baf1 11311 || cp_parser_allow_gnu_extensions_p (parser))
3c01e5df 11312 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
a723baf1
MM
11313 }
11314 else
11315 {
11316 /* Try the pointer-to-member case. */
11317 cp_parser_parse_tentatively (parser);
11318 /* Look for the optional `::' operator. */
11319 cp_parser_global_scope_opt (parser,
11320 /*current_scope_valid_p=*/false);
11321 /* Look for the nested-name specifier. */
11322 cp_parser_nested_name_specifier (parser,
11323 /*typename_keyword_p=*/false,
11324 /*check_dependency_p=*/true,
a668c6ad
MM
11325 /*type_p=*/false,
11326 /*is_declaration=*/false);
a723baf1
MM
11327 /* If we found it, and the next token is a `*', then we are
11328 indeed looking at a pointer-to-member operator. */
11329 if (!cp_parser_error_occurred (parser)
11330 && cp_parser_require (parser, CPP_MULT, "`*'"))
11331 {
11332 /* The type of which the member is a member is given by the
11333 current SCOPE. */
11334 *type = parser->scope;
11335 /* The next name will not be qualified. */
11336 parser->scope = NULL_TREE;
11337 parser->qualifying_scope = NULL_TREE;
11338 parser->object_scope = NULL_TREE;
11339 /* Indicate that the `*' operator was used. */
11340 code = INDIRECT_REF;
11341 /* Look for the optional cv-qualifier-seq. */
3c01e5df 11342 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
a723baf1
MM
11343 }
11344 /* If that didn't work we don't have a ptr-operator. */
11345 if (!cp_parser_parse_definitely (parser))
11346 cp_parser_error (parser, "expected ptr-operator");
11347 }
11348
11349 return code;
11350}
11351
11352/* Parse an (optional) cv-qualifier-seq.
11353
11354 cv-qualifier-seq:
21526606 11355 cv-qualifier cv-qualifier-seq [opt]
a723baf1 11356
a723baf1
MM
11357 cv-qualifier:
11358 const
21526606 11359 volatile
a723baf1
MM
11360
11361 GNU Extension:
11362
11363 cv-qualifier:
98ca843c 11364 __restrict__
a723baf1 11365
3c01e5df
MM
11366 Returns a bitmask representing the cv-qualifiers. */
11367
11368static cp_cv_quals
11369cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
a723baf1 11370{
3c01e5df 11371 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
a723baf1 11372
3c01e5df 11373 while (true)
a723baf1 11374 {
3c01e5df
MM
11375 cp_token *token;
11376 cp_cv_quals cv_qualifier;
98ca843c 11377
3c01e5df
MM
11378 /* Peek at the next token. */
11379 token = cp_lexer_peek_token (parser->lexer);
11380 /* See if it's a cv-qualifier. */
11381 switch (token->keyword)
11382 {
11383 case RID_CONST:
11384 cv_qualifier = TYPE_QUAL_CONST;
11385 break;
98ca843c 11386
3c01e5df
MM
11387 case RID_VOLATILE:
11388 cv_qualifier = TYPE_QUAL_VOLATILE;
11389 break;
98ca843c 11390
3c01e5df
MM
11391 case RID_RESTRICT:
11392 cv_qualifier = TYPE_QUAL_RESTRICT;
11393 break;
98ca843c 11394
3c01e5df
MM
11395 default:
11396 cv_qualifier = TYPE_UNQUALIFIED;
11397 break;
11398 }
98ca843c 11399
3c01e5df
MM
11400 if (!cv_qualifier)
11401 break;
a723baf1 11402
3c01e5df
MM
11403 if (cv_quals & cv_qualifier)
11404 {
11405 error ("duplicate cv-qualifier");
11406 cp_lexer_purge_token (parser->lexer);
11407 }
11408 else
11409 {
11410 cp_lexer_consume_token (parser->lexer);
11411 cv_quals |= cv_qualifier;
11412 }
a723baf1
MM
11413 }
11414
3c01e5df 11415 return cv_quals;
a723baf1
MM
11416}
11417
11418/* Parse a declarator-id.
11419
11420 declarator-id:
11421 id-expression
21526606 11422 :: [opt] nested-name-specifier [opt] type-name
a723baf1
MM
11423
11424 In the `id-expression' case, the value returned is as for
11425 cp_parser_id_expression if the id-expression was an unqualified-id.
11426 If the id-expression was a qualified-id, then a SCOPE_REF is
11427 returned. The first operand is the scope (either a NAMESPACE_DECL
11428 or TREE_TYPE), but the second is still just a representation of an
11429 unqualified-id. */
11430
11431static tree
94edc4ab 11432cp_parser_declarator_id (cp_parser* parser)
a723baf1 11433{
a723baf1
MM
11434 /* The expression must be an id-expression. Assume that qualified
11435 names are the names of types so that:
11436
11437 template <class T>
11438 int S<T>::R::i = 3;
11439
11440 will work; we must treat `S<T>::R' as the name of a type.
11441 Similarly, assume that qualified names are templates, where
11442 required, so that:
11443
11444 template <class T>
11445 int S<T>::R<T>::i = 3;
11446
11447 will work, too. */
1d786913
MM
11448 return cp_parser_id_expression (parser,
11449 /*template_keyword_p=*/false,
11450 /*check_dependency_p=*/false,
11451 /*template_p=*/NULL,
11452 /*declarator_p=*/true);
a723baf1
MM
11453}
11454
11455/* Parse a type-id.
11456
11457 type-id:
11458 type-specifier-seq abstract-declarator [opt]
11459
11460 Returns the TYPE specified. */
11461
11462static tree
94edc4ab 11463cp_parser_type_id (cp_parser* parser)
a723baf1 11464{
62d1db17 11465 cp_decl_specifier_seq type_specifier_seq;
058b15c1 11466 cp_declarator *abstract_declarator;
a723baf1
MM
11467
11468 /* Parse the type-specifier-seq. */
62d1db17
MM
11469 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11470 if (type_specifier_seq.type == error_mark_node)
a723baf1
MM
11471 return error_mark_node;
11472
11473 /* There might or might not be an abstract declarator. */
11474 cp_parser_parse_tentatively (parser);
11475 /* Look for the declarator. */
21526606 11476 abstract_declarator
4bb8ca28 11477 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
db86dd14
MM
11478 /*parenthesized_p=*/NULL,
11479 /*member_p=*/false);
a723baf1
MM
11480 /* Check to see if there really was a declarator. */
11481 if (!cp_parser_parse_definitely (parser))
058b15c1 11482 abstract_declarator = NULL;
a723baf1 11483
62d1db17 11484 return groktypename (&type_specifier_seq, abstract_declarator);
a723baf1
MM
11485}
11486
11487/* Parse a type-specifier-seq.
11488
11489 type-specifier-seq:
11490 type-specifier type-specifier-seq [opt]
11491
11492 GNU extension:
11493
11494 type-specifier-seq:
11495 attributes type-specifier-seq [opt]
11496
62d1db17 11497 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
a723baf1 11498
62d1db17
MM
11499static void
11500cp_parser_type_specifier_seq (cp_parser* parser,
11501 cp_decl_specifier_seq *type_specifier_seq)
a723baf1
MM
11502{
11503 bool seen_type_specifier = false;
62d1db17
MM
11504
11505 /* Clear the TYPE_SPECIFIER_SEQ. */
11506 clear_decl_specs (type_specifier_seq);
a723baf1
MM
11507
11508 /* Parse the type-specifiers and attributes. */
11509 while (true)
11510 {
11511 tree type_specifier;
11512
11513 /* Check for attributes first. */
11514 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11515 {
98ca843c 11516 type_specifier_seq->attributes =
62d1db17
MM
11517 chainon (type_specifier_seq->attributes,
11518 cp_parser_attributes_opt (parser));
a723baf1
MM
11519 continue;
11520 }
11521
a723baf1 11522 /* Look for the type-specifier. */
21526606 11523 type_specifier = cp_parser_type_specifier (parser,
62d1db17
MM
11524 CP_PARSER_FLAGS_OPTIONAL,
11525 type_specifier_seq,
a723baf1
MM
11526 /*is_declaration=*/false,
11527 NULL,
11528 NULL);
11529 /* If the first type-specifier could not be found, this is not a
11530 type-specifier-seq at all. */
62d1db17
MM
11531 if (!seen_type_specifier && !type_specifier)
11532 {
11533 cp_parser_error (parser, "expected type-specifier");
11534 type_specifier_seq->type = error_mark_node;
11535 return;
11536 }
a723baf1
MM
11537 /* If subsequent type-specifiers could not be found, the
11538 type-specifier-seq is complete. */
62d1db17 11539 else if (seen_type_specifier && !type_specifier)
a723baf1
MM
11540 break;
11541
a723baf1
MM
11542 seen_type_specifier = true;
11543 }
11544
62d1db17 11545 return;
a723baf1
MM
11546}
11547
11548/* Parse a parameter-declaration-clause.
11549
11550 parameter-declaration-clause:
11551 parameter-declaration-list [opt] ... [opt]
11552 parameter-declaration-list , ...
11553
058b15c1
MM
11554 Returns a representation for the parameter declarations. A return
11555 value of NULL indicates a parameter-declaration-clause consisting
11556 only of an ellipsis. */
a723baf1 11557
058b15c1 11558static cp_parameter_declarator *
94edc4ab 11559cp_parser_parameter_declaration_clause (cp_parser* parser)
a723baf1 11560{
058b15c1 11561 cp_parameter_declarator *parameters;
a723baf1
MM
11562 cp_token *token;
11563 bool ellipsis_p;
058b15c1 11564 bool is_error;
a723baf1
MM
11565
11566 /* Peek at the next token. */
11567 token = cp_lexer_peek_token (parser->lexer);
11568 /* Check for trivial parameter-declaration-clauses. */
11569 if (token->type == CPP_ELLIPSIS)
11570 {
11571 /* Consume the `...' token. */
11572 cp_lexer_consume_token (parser->lexer);
058b15c1 11573 return NULL;
a723baf1
MM
11574 }
11575 else if (token->type == CPP_CLOSE_PAREN)
11576 /* There are no parameters. */
c73aecdf
DE
11577 {
11578#ifndef NO_IMPLICIT_EXTERN_C
11579 if (in_system_header && current_class_type == NULL
11580 && current_lang_name == lang_name_c)
058b15c1 11581 return NULL;
c73aecdf
DE
11582 else
11583#endif
058b15c1 11584 return no_parameters;
c73aecdf 11585 }
a723baf1
MM
11586 /* Check for `(void)', too, which is a special case. */
11587 else if (token->keyword == RID_VOID
21526606 11588 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1
MM
11589 == CPP_CLOSE_PAREN))
11590 {
11591 /* Consume the `void' token. */
11592 cp_lexer_consume_token (parser->lexer);
11593 /* There are no parameters. */
058b15c1 11594 return no_parameters;
a723baf1 11595 }
21526606 11596
a723baf1 11597 /* Parse the parameter-declaration-list. */
058b15c1 11598 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
a723baf1
MM
11599 /* If a parse error occurred while parsing the
11600 parameter-declaration-list, then the entire
11601 parameter-declaration-clause is erroneous. */
058b15c1
MM
11602 if (is_error)
11603 return NULL;
a723baf1
MM
11604
11605 /* Peek at the next token. */
11606 token = cp_lexer_peek_token (parser->lexer);
11607 /* If it's a `,', the clause should terminate with an ellipsis. */
11608 if (token->type == CPP_COMMA)
11609 {
11610 /* Consume the `,'. */
11611 cp_lexer_consume_token (parser->lexer);
11612 /* Expect an ellipsis. */
21526606 11613 ellipsis_p
a723baf1
MM
11614 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11615 }
21526606 11616 /* It might also be `...' if the optional trailing `,' was
a723baf1
MM
11617 omitted. */
11618 else if (token->type == CPP_ELLIPSIS)
11619 {
11620 /* Consume the `...' token. */
11621 cp_lexer_consume_token (parser->lexer);
11622 /* And remember that we saw it. */
11623 ellipsis_p = true;
11624 }
11625 else
11626 ellipsis_p = false;
11627
11628 /* Finish the parameter list. */
058b15c1
MM
11629 if (parameters && ellipsis_p)
11630 parameters->ellipsis_p = true;
98ca843c 11631
058b15c1 11632 return parameters;
a723baf1
MM
11633}
11634
11635/* Parse a parameter-declaration-list.
11636
11637 parameter-declaration-list:
11638 parameter-declaration
11639 parameter-declaration-list , parameter-declaration
11640
11641 Returns a representation of the parameter-declaration-list, as for
11642 cp_parser_parameter_declaration_clause. However, the
058b15c1
MM
11643 `void_list_node' is never appended to the list. Upon return,
11644 *IS_ERROR will be true iff an error occurred. */
a723baf1 11645
058b15c1
MM
11646static cp_parameter_declarator *
11647cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
a723baf1 11648{
058b15c1
MM
11649 cp_parameter_declarator *parameters = NULL;
11650 cp_parameter_declarator **tail = &parameters;
11651
11652 /* Assume all will go well. */
11653 *is_error = false;
a723baf1
MM
11654
11655 /* Look for more parameters. */
11656 while (true)
11657 {
058b15c1 11658 cp_parameter_declarator *parameter;
4bb8ca28 11659 bool parenthesized_p;
a723baf1 11660 /* Parse the parameter. */
21526606
EC
11661 parameter
11662 = cp_parser_parameter_declaration (parser,
4bb8ca28
MM
11663 /*template_parm_p=*/false,
11664 &parenthesized_p);
ec194454 11665
34cd5ae7 11666 /* If a parse error occurred parsing the parameter declaration,
a723baf1 11667 then the entire parameter-declaration-list is erroneous. */
058b15c1 11668 if (!parameter)
a723baf1 11669 {
058b15c1
MM
11670 *is_error = true;
11671 parameters = NULL;
a723baf1
MM
11672 break;
11673 }
11674 /* Add the new parameter to the list. */
058b15c1
MM
11675 *tail = parameter;
11676 tail = &parameter->next;
a723baf1
MM
11677
11678 /* Peek at the next token. */
11679 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11680 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11681 /* The parameter-declaration-list is complete. */
11682 break;
11683 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11684 {
11685 cp_token *token;
11686
11687 /* Peek at the next token. */
11688 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11689 /* If it's an ellipsis, then the list is complete. */
11690 if (token->type == CPP_ELLIPSIS)
11691 break;
11692 /* Otherwise, there must be more parameters. Consume the
11693 `,'. */
11694 cp_lexer_consume_token (parser->lexer);
4bb8ca28
MM
11695 /* When parsing something like:
11696
11697 int i(float f, double d)
21526606 11698
4bb8ca28
MM
11699 we can tell after seeing the declaration for "f" that we
11700 are not looking at an initialization of a variable "i",
21526606 11701 but rather at the declaration of a function "i".
4bb8ca28
MM
11702
11703 Due to the fact that the parsing of template arguments
11704 (as specified to a template-id) requires backtracking we
11705 cannot use this technique when inside a template argument
11706 list. */
11707 if (!parser->in_template_argument_list_p
4d5fe289 11708 && !parser->in_type_id_in_expr_p
0b16f8f4 11709 && cp_parser_uncommitted_to_tentative_parse_p (parser)
4bb8ca28
MM
11710 /* However, a parameter-declaration of the form
11711 "foat(f)" (which is a valid declaration of a
11712 parameter "f") can also be interpreted as an
11713 expression (the conversion of "f" to "float"). */
11714 && !parenthesized_p)
11715 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
11716 }
11717 else
11718 {
2a13a625 11719 cp_parser_error (parser, "expected %<,%> or %<...%>");
0b16f8f4 11720 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21526606 11721 cp_parser_skip_to_closing_parenthesis (parser,
4bb8ca28 11722 /*recovering=*/true,
5c832178 11723 /*or_comma=*/false,
4bb8ca28 11724 /*consume_paren=*/false);
a723baf1
MM
11725 break;
11726 }
11727 }
11728
058b15c1 11729 return parameters;
a723baf1
MM
11730}
11731
11732/* Parse a parameter declaration.
11733
11734 parameter-declaration:
11735 decl-specifier-seq declarator
11736 decl-specifier-seq declarator = assignment-expression
11737 decl-specifier-seq abstract-declarator [opt]
11738 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11739
ec194454
MM
11740 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11741 declares a template parameter. (In that case, a non-nested `>'
11742 token encountered during the parsing of the assignment-expression
11743 is not interpreted as a greater-than operator.)
a723baf1 11744
058b15c1
MM
11745 Returns a representation of the parameter, or NULL if an error
11746 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11747 true iff the declarator is of the form "(p)". */
a723baf1 11748
058b15c1 11749static cp_parameter_declarator *
21526606 11750cp_parser_parameter_declaration (cp_parser *parser,
4bb8ca28
MM
11751 bool template_parm_p,
11752 bool *parenthesized_p)
a723baf1 11753{
560ad596 11754 int declares_class_or_enum;
ec194454 11755 bool greater_than_is_operator_p;
62d1db17 11756 cp_decl_specifier_seq decl_specifiers;
058b15c1 11757 cp_declarator *declarator;
a723baf1 11758 tree default_argument;
a723baf1
MM
11759 cp_token *token;
11760 const char *saved_message;
11761
ec194454
MM
11762 /* In a template parameter, `>' is not an operator.
11763
11764 [temp.param]
11765
11766 When parsing a default template-argument for a non-type
11767 template-parameter, the first non-nested `>' is taken as the end
11768 of the template parameter-list rather than a greater-than
11769 operator. */
11770 greater_than_is_operator_p = !template_parm_p;
11771
a723baf1
MM
11772 /* Type definitions may not appear in parameter types. */
11773 saved_message = parser->type_definition_forbidden_message;
21526606 11774 parser->type_definition_forbidden_message
a723baf1
MM
11775 = "types may not be defined in parameter types";
11776
11777 /* Parse the declaration-specifiers. */
62d1db17
MM
11778 cp_parser_decl_specifier_seq (parser,
11779 CP_PARSER_FLAGS_NONE,
11780 &decl_specifiers,
11781 &declares_class_or_enum);
a723baf1
MM
11782 /* If an error occurred, there's no reason to attempt to parse the
11783 rest of the declaration. */
11784 if (cp_parser_error_occurred (parser))
11785 {
11786 parser->type_definition_forbidden_message = saved_message;
058b15c1 11787 return NULL;
a723baf1
MM
11788 }
11789
11790 /* Peek at the next token. */
11791 token = cp_lexer_peek_token (parser->lexer);
11792 /* If the next token is a `)', `,', `=', `>', or `...', then there
11793 is no declarator. */
21526606 11794 if (token->type == CPP_CLOSE_PAREN
a723baf1
MM
11795 || token->type == CPP_COMMA
11796 || token->type == CPP_EQ
11797 || token->type == CPP_ELLIPSIS
11798 || token->type == CPP_GREATER)
4bb8ca28 11799 {
058b15c1 11800 declarator = NULL;
4bb8ca28
MM
11801 if (parenthesized_p)
11802 *parenthesized_p = false;
11803 }
a723baf1
MM
11804 /* Otherwise, there should be a declarator. */
11805 else
11806 {
11807 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11808 parser->default_arg_ok_p = false;
21526606 11809
5c832178
MM
11810 /* After seeing a decl-specifier-seq, if the next token is not a
11811 "(", there is no possibility that the code is a valid
4f8163b1
MM
11812 expression. Therefore, if parsing tentatively, we commit at
11813 this point. */
5c832178 11814 if (!parser->in_template_argument_list_p
643aee72 11815 /* In an expression context, having seen:
4f8163b1 11816
a7324e75 11817 (int((char ...
4f8163b1
MM
11818
11819 we cannot be sure whether we are looking at a
a7324e75
MM
11820 function-type (taking a "char" as a parameter) or a cast
11821 of some object of type "char" to "int". */
4f8163b1 11822 && !parser->in_type_id_in_expr_p
0b16f8f4 11823 && cp_parser_uncommitted_to_tentative_parse_p (parser)
5c832178
MM
11824 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11825 cp_parser_commit_to_tentative_parse (parser);
11826 /* Parse the declarator. */
a723baf1 11827 declarator = cp_parser_declarator (parser,
62b8a44e 11828 CP_PARSER_DECLARATOR_EITHER,
4bb8ca28 11829 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
11830 parenthesized_p,
11831 /*member_p=*/false);
a723baf1 11832 parser->default_arg_ok_p = saved_default_arg_ok_p;
4971227d 11833 /* After the declarator, allow more attributes. */
62d1db17 11834 decl_specifiers.attributes
98ca843c 11835 = chainon (decl_specifiers.attributes,
62d1db17 11836 cp_parser_attributes_opt (parser));
a723baf1
MM
11837 }
11838
62b8a44e 11839 /* The restriction on defining new types applies only to the type
a723baf1
MM
11840 of the parameter, not to the default argument. */
11841 parser->type_definition_forbidden_message = saved_message;
11842
11843 /* If the next token is `=', then process a default argument. */
11844 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11845 {
11846 bool saved_greater_than_is_operator_p;
11847 /* Consume the `='. */
11848 cp_lexer_consume_token (parser->lexer);
11849
11850 /* If we are defining a class, then the tokens that make up the
11851 default argument must be saved and processed later. */
21526606 11852 if (!template_parm_p && at_class_scope_p ()
ec194454 11853 && TYPE_BEING_DEFINED (current_class_type))
a723baf1
MM
11854 {
11855 unsigned depth = 0;
c162c75e
MA
11856 cp_token *first_token;
11857 cp_token *token;
a723baf1
MM
11858
11859 /* Add tokens until we have processed the entire default
03fd3f84 11860 argument. We add the range [first_token, token). */
c162c75e 11861 first_token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
11862 while (true)
11863 {
11864 bool done = false;
a723baf1
MM
11865
11866 /* Peek at the next token. */
11867 token = cp_lexer_peek_token (parser->lexer);
11868 /* What we do depends on what token we have. */
11869 switch (token->type)
11870 {
11871 /* In valid code, a default argument must be
11872 immediately followed by a `,' `)', or `...'. */
11873 case CPP_COMMA:
11874 case CPP_CLOSE_PAREN:
11875 case CPP_ELLIPSIS:
11876 /* If we run into a non-nested `;', `}', or `]',
11877 then the code is invalid -- but the default
11878 argument is certainly over. */
11879 case CPP_SEMICOLON:
11880 case CPP_CLOSE_BRACE:
11881 case CPP_CLOSE_SQUARE:
11882 if (depth == 0)
11883 done = true;
11884 /* Update DEPTH, if necessary. */
11885 else if (token->type == CPP_CLOSE_PAREN
11886 || token->type == CPP_CLOSE_BRACE
11887 || token->type == CPP_CLOSE_SQUARE)
11888 --depth;
11889 break;
11890
11891 case CPP_OPEN_PAREN:
11892 case CPP_OPEN_SQUARE:
11893 case CPP_OPEN_BRACE:
11894 ++depth;
11895 break;
11896
11897 case CPP_GREATER:
11898 /* If we see a non-nested `>', and `>' is not an
11899 operator, then it marks the end of the default
11900 argument. */
11901 if (!depth && !greater_than_is_operator_p)
11902 done = true;
11903 break;
11904
11905 /* If we run out of tokens, issue an error message. */
11906 case CPP_EOF:
11907 error ("file ends in default argument");
11908 done = true;
11909 break;
11910
11911 case CPP_NAME:
11912 case CPP_SCOPE:
11913 /* In these cases, we should look for template-ids.
21526606 11914 For example, if the default argument is
a723baf1
MM
11915 `X<int, double>()', we need to do name lookup to
11916 figure out whether or not `X' is a template; if
34cd5ae7 11917 so, the `,' does not end the default argument.
a723baf1
MM
11918
11919 That is not yet done. */
11920 break;
11921
11922 default:
11923 break;
11924 }
11925
11926 /* If we've reached the end, stop. */
11927 if (done)
11928 break;
21526606 11929
a723baf1
MM
11930 /* Add the token to the token block. */
11931 token = cp_lexer_consume_token (parser->lexer);
a723baf1 11932 }
c162c75e
MA
11933
11934 /* Create a DEFAULT_ARG to represented the unparsed default
11935 argument. */
11936 default_argument = make_node (DEFAULT_ARG);
11937 DEFARG_TOKENS (default_argument)
11938 = cp_token_cache_new (first_token, token);
a723baf1
MM
11939 }
11940 /* Outside of a class definition, we can just parse the
11941 assignment-expression. */
11942 else
11943 {
11944 bool saved_local_variables_forbidden_p;
11945
11946 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11947 set correctly. */
21526606 11948 saved_greater_than_is_operator_p
a723baf1
MM
11949 = parser->greater_than_is_operator_p;
11950 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11951 /* Local variable names (and the `this' keyword) may not
11952 appear in a default argument. */
21526606 11953 saved_local_variables_forbidden_p
a723baf1
MM
11954 = parser->local_variables_forbidden_p;
11955 parser->local_variables_forbidden_p = true;
11956 /* Parse the assignment-expression. */
93678513
MM
11957 default_argument
11958 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
a723baf1 11959 /* Restore saved state. */
21526606 11960 parser->greater_than_is_operator_p
a723baf1 11961 = saved_greater_than_is_operator_p;
21526606
EC
11962 parser->local_variables_forbidden_p
11963 = saved_local_variables_forbidden_p;
a723baf1
MM
11964 }
11965 if (!parser->default_arg_ok_p)
11966 {
c67d36d0
NS
11967 if (!flag_pedantic_errors)
11968 warning ("deprecated use of default argument for parameter of non-function");
11969 else
11970 {
11971 error ("default arguments are only permitted for function parameters");
11972 default_argument = NULL_TREE;
11973 }
a723baf1
MM
11974 }
11975 }
11976 else
11977 default_argument = NULL_TREE;
21526606 11978
62d1db17 11979 return make_parameter_declarator (&decl_specifiers,
058b15c1
MM
11980 declarator,
11981 default_argument);
a723baf1
MM
11982}
11983
a723baf1
MM
11984/* Parse a function-body.
11985
11986 function-body:
11987 compound_statement */
11988
11989static void
11990cp_parser_function_body (cp_parser *parser)
11991{
325c3691 11992 cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
11993}
11994
11995/* Parse a ctor-initializer-opt followed by a function-body. Return
11996 true if a ctor-initializer was present. */
11997
11998static bool
11999cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12000{
12001 tree body;
12002 bool ctor_initializer_p;
12003
12004 /* Begin the function body. */
12005 body = begin_function_body ();
12006 /* Parse the optional ctor-initializer. */
12007 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12008 /* Parse the function-body. */
12009 cp_parser_function_body (parser);
12010 /* Finish the function body. */
12011 finish_function_body (body);
12012
12013 return ctor_initializer_p;
12014}
12015
12016/* Parse an initializer.
12017
12018 initializer:
12019 = initializer-clause
21526606 12020 ( expression-list )
a723baf1
MM
12021
12022 Returns a expression representing the initializer. If no
21526606 12023 initializer is present, NULL_TREE is returned.
a723baf1
MM
12024
12025 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12026 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
39703eb9
MM
12027 set to FALSE if there is no initializer present. If there is an
12028 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12029 is set to true; otherwise it is set to false. */
a723baf1
MM
12030
12031static tree
39703eb9
MM
12032cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12033 bool* non_constant_p)
a723baf1
MM
12034{
12035 cp_token *token;
12036 tree init;
12037
12038 /* Peek at the next token. */
12039 token = cp_lexer_peek_token (parser->lexer);
12040
12041 /* Let our caller know whether or not this initializer was
12042 parenthesized. */
12043 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
39703eb9
MM
12044 /* Assume that the initializer is constant. */
12045 *non_constant_p = false;
a723baf1
MM
12046
12047 if (token->type == CPP_EQ)
12048 {
12049 /* Consume the `='. */
12050 cp_lexer_consume_token (parser->lexer);
12051 /* Parse the initializer-clause. */
39703eb9 12052 init = cp_parser_initializer_clause (parser, non_constant_p);
a723baf1
MM
12053 }
12054 else if (token->type == CPP_OPEN_PAREN)
39703eb9 12055 init = cp_parser_parenthesized_expression_list (parser, false,
93678513 12056 /*cast_p=*/false,
39703eb9 12057 non_constant_p);
a723baf1
MM
12058 else
12059 {
12060 /* Anything else is an error. */
12061 cp_parser_error (parser, "expected initializer");
12062 init = error_mark_node;
12063 }
12064
12065 return init;
12066}
12067
21526606 12068/* Parse an initializer-clause.
a723baf1
MM
12069
12070 initializer-clause:
12071 assignment-expression
12072 { initializer-list , [opt] }
12073 { }
12074
21526606 12075 Returns an expression representing the initializer.
a723baf1
MM
12076
12077 If the `assignment-expression' production is used the value
21526606 12078 returned is simply a representation for the expression.
a723baf1
MM
12079
12080 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12081 the elements of the initializer-list (or NULL_TREE, if the last
12082 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12083 NULL_TREE. There is no way to detect whether or not the optional
39703eb9
MM
12084 trailing `,' was provided. NON_CONSTANT_P is as for
12085 cp_parser_initializer. */
a723baf1
MM
12086
12087static tree
39703eb9 12088cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
12089{
12090 tree initializer;
12091
b2802a4b
R
12092 /* Assume the expression is constant. */
12093 *non_constant_p = false;
12094
a723baf1
MM
12095 /* If it is not a `{', then we are looking at an
12096 assignment-expression. */
12097 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
0da99d4e 12098 {
98ca843c 12099 initializer
0da99d4e
GB
12100 = cp_parser_constant_expression (parser,
12101 /*allow_non_constant_p=*/true,
12102 non_constant_p);
12103 if (!*non_constant_p)
12104 initializer = fold_non_dependent_expr (initializer);
12105 }
a723baf1
MM
12106 else
12107 {
12108 /* Consume the `{' token. */
12109 cp_lexer_consume_token (parser->lexer);
12110 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12111 initializer = make_node (CONSTRUCTOR);
a723baf1
MM
12112 /* If it's not a `}', then there is a non-trivial initializer. */
12113 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12114 {
12115 /* Parse the initializer list. */
12116 CONSTRUCTOR_ELTS (initializer)
39703eb9 12117 = cp_parser_initializer_list (parser, non_constant_p);
a723baf1
MM
12118 /* A trailing `,' token is allowed. */
12119 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12120 cp_lexer_consume_token (parser->lexer);
12121 }
a723baf1
MM
12122 /* Now, there should be a trailing `}'. */
12123 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12124 }
12125
12126 return initializer;
12127}
12128
12129/* Parse an initializer-list.
12130
12131 initializer-list:
12132 initializer-clause
12133 initializer-list , initializer-clause
12134
12135 GNU Extension:
21526606 12136
a723baf1
MM
12137 initializer-list:
12138 identifier : initializer-clause
12139 initializer-list, identifier : initializer-clause
12140
12141 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12142 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
39703eb9
MM
12143 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12144 as for cp_parser_initializer. */
a723baf1
MM
12145
12146static tree
39703eb9 12147cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
12148{
12149 tree initializers = NULL_TREE;
12150
39703eb9
MM
12151 /* Assume all of the expressions are constant. */
12152 *non_constant_p = false;
12153
a723baf1
MM
12154 /* Parse the rest of the list. */
12155 while (true)
12156 {
12157 cp_token *token;
12158 tree identifier;
12159 tree initializer;
39703eb9
MM
12160 bool clause_non_constant_p;
12161
a723baf1
MM
12162 /* If the next token is an identifier and the following one is a
12163 colon, we are looking at the GNU designated-initializer
12164 syntax. */
12165 if (cp_parser_allow_gnu_extensions_p (parser)
12166 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12167 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12168 {
12169 /* Consume the identifier. */
12170 identifier = cp_lexer_consume_token (parser->lexer)->value;
12171 /* Consume the `:'. */
12172 cp_lexer_consume_token (parser->lexer);
12173 }
12174 else
12175 identifier = NULL_TREE;
12176
12177 /* Parse the initializer. */
21526606 12178 initializer = cp_parser_initializer_clause (parser,
39703eb9
MM
12179 &clause_non_constant_p);
12180 /* If any clause is non-constant, so is the entire initializer. */
12181 if (clause_non_constant_p)
12182 *non_constant_p = true;
a723baf1
MM
12183 /* Add it to the list. */
12184 initializers = tree_cons (identifier, initializer, initializers);
12185
12186 /* If the next token is not a comma, we have reached the end of
12187 the list. */
12188 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12189 break;
12190
12191 /* Peek at the next token. */
12192 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12193 /* If the next token is a `}', then we're still done. An
12194 initializer-clause can have a trailing `,' after the
12195 initializer-list and before the closing `}'. */
12196 if (token->type == CPP_CLOSE_BRACE)
12197 break;
12198
12199 /* Consume the `,' token. */
12200 cp_lexer_consume_token (parser->lexer);
12201 }
12202
12203 /* The initializers were built up in reverse order, so we need to
12204 reverse them now. */
12205 return nreverse (initializers);
12206}
12207
12208/* Classes [gram.class] */
12209
12210/* Parse a class-name.
12211
12212 class-name:
12213 identifier
12214 template-id
12215
12216 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12217 to indicate that names looked up in dependent types should be
12218 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12219 keyword has been used to indicate that the name that appears next
fc6a28d7
MM
12220 is a template. TAG_TYPE indicates the explicit tag given before
12221 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12222 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12223 is the class being defined in a class-head.
a723baf1
MM
12224
12225 Returns the TYPE_DECL representing the class. */
12226
12227static tree
21526606
EC
12228cp_parser_class_name (cp_parser *parser,
12229 bool typename_keyword_p,
12230 bool template_keyword_p,
fc6a28d7 12231 enum tag_types tag_type,
a723baf1 12232 bool check_dependency_p,
a668c6ad
MM
12233 bool class_head_p,
12234 bool is_declaration)
a723baf1
MM
12235{
12236 tree decl;
12237 tree scope;
12238 bool typename_p;
e5976695
MM
12239 cp_token *token;
12240
12241 /* All class-names start with an identifier. */
12242 token = cp_lexer_peek_token (parser->lexer);
12243 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12244 {
12245 cp_parser_error (parser, "expected class-name");
12246 return error_mark_node;
12247 }
21526606 12248
a723baf1
MM
12249 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12250 to a template-id, so we save it here. */
12251 scope = parser->scope;
3adee96c
KL
12252 if (scope == error_mark_node)
12253 return error_mark_node;
21526606 12254
a723baf1
MM
12255 /* Any name names a type if we're following the `typename' keyword
12256 in a qualified name where the enclosing scope is type-dependent. */
12257 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
1fb3244a 12258 && dependent_type_p (scope));
e5976695
MM
12259 /* Handle the common case (an identifier, but not a template-id)
12260 efficiently. */
21526606 12261 if (token->type == CPP_NAME
f4abade9 12262 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
a723baf1 12263 {
a723baf1
MM
12264 tree identifier;
12265
12266 /* Look for the identifier. */
12267 identifier = cp_parser_identifier (parser);
12268 /* If the next token isn't an identifier, we are certainly not
12269 looking at a class-name. */
12270 if (identifier == error_mark_node)
12271 decl = error_mark_node;
12272 /* If we know this is a type-name, there's no need to look it
12273 up. */
12274 else if (typename_p)
12275 decl = identifier;
12276 else
12277 {
12278 /* If the next token is a `::', then the name must be a type
12279 name.
12280
12281 [basic.lookup.qual]
12282
12283 During the lookup for a name preceding the :: scope
12284 resolution operator, object, function, and enumerator
12285 names are ignored. */
12286 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
fc6a28d7 12287 tag_type = typename_type;
a723baf1 12288 /* Look up the name. */
21526606 12289 decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 12290 tag_type,
b0bc6e8e 12291 /*is_template=*/false,
eea9800f 12292 /*is_namespace=*/false,
8f78f01f
MM
12293 check_dependency_p,
12294 /*ambiguous_p=*/NULL);
a723baf1
MM
12295 }
12296 }
e5976695
MM
12297 else
12298 {
12299 /* Try a template-id. */
12300 decl = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
12301 check_dependency_p,
12302 is_declaration);
e5976695
MM
12303 if (decl == error_mark_node)
12304 return error_mark_node;
12305 }
a723baf1
MM
12306
12307 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12308
12309 /* If this is a typename, create a TYPENAME_TYPE. */
12310 if (typename_p && decl != error_mark_node)
4bfb8bba 12311 {
fc6a28d7 12312 decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
4bfb8bba
MM
12313 if (decl != error_mark_node)
12314 decl = TYPE_NAME (decl);
12315 }
a723baf1
MM
12316
12317 /* Check to see that it is really the name of a class. */
21526606 12318 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
a723baf1
MM
12319 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12320 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12321 /* Situations like this:
12322
12323 template <typename T> struct A {
21526606 12324 typename T::template X<int>::I i;
a723baf1
MM
12325 };
12326
12327 are problematic. Is `T::template X<int>' a class-name? The
12328 standard does not seem to be definitive, but there is no other
12329 valid interpretation of the following `::'. Therefore, those
12330 names are considered class-names. */
fc6a28d7 12331 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
a723baf1
MM
12332 else if (decl == error_mark_node
12333 || TREE_CODE (decl) != TYPE_DECL
07c65e00 12334 || TREE_TYPE (decl) == error_mark_node
a723baf1
MM
12335 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12336 {
12337 cp_parser_error (parser, "expected class-name");
12338 return error_mark_node;
12339 }
12340
12341 return decl;
12342}
12343
12344/* Parse a class-specifier.
12345
12346 class-specifier:
12347 class-head { member-specification [opt] }
12348
12349 Returns the TREE_TYPE representing the class. */
12350
12351static tree
94edc4ab 12352cp_parser_class_specifier (cp_parser* parser)
a723baf1
MM
12353{
12354 cp_token *token;
12355 tree type;
6de9cd9a 12356 tree attributes = NULL_TREE;
a723baf1
MM
12357 int has_trailing_semicolon;
12358 bool nested_name_specifier_p;
a723baf1 12359 unsigned saved_num_template_parameter_lists;
87c465f5 12360 tree old_scope = NULL_TREE;
2436b51f 12361 tree scope = NULL_TREE;
a723baf1 12362
8d241e0b 12363 push_deferring_access_checks (dk_no_deferred);
cf22909c 12364
a723baf1
MM
12365 /* Parse the class-head. */
12366 type = cp_parser_class_head (parser,
38b305d0
JM
12367 &nested_name_specifier_p,
12368 &attributes);
a723baf1
MM
12369 /* If the class-head was a semantic disaster, skip the entire body
12370 of the class. */
12371 if (!type)
12372 {
12373 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 12374 pop_deferring_access_checks ();
a723baf1
MM
12375 return error_mark_node;
12376 }
cf22909c 12377
a723baf1
MM
12378 /* Look for the `{'. */
12379 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
cf22909c
KL
12380 {
12381 pop_deferring_access_checks ();
12382 return error_mark_node;
12383 }
12384
a723baf1
MM
12385 /* Issue an error message if type-definitions are forbidden here. */
12386 cp_parser_check_type_definition (parser);
12387 /* Remember that we are defining one more class. */
12388 ++parser->num_classes_being_defined;
12389 /* Inside the class, surrounding template-parameter-lists do not
12390 apply. */
21526606
EC
12391 saved_num_template_parameter_lists
12392 = parser->num_template_parameter_lists;
a723baf1 12393 parser->num_template_parameter_lists = 0;
78757caa 12394
a723baf1 12395 /* Start the class. */
eeb23c11 12396 if (nested_name_specifier_p)
2436b51f
MM
12397 {
12398 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
87c465f5 12399 old_scope = push_inner_scope (scope);
2436b51f 12400 }
a723baf1 12401 type = begin_class_definition (type);
98ca843c 12402
a723baf1 12403 if (type == error_mark_node)
9bcb9aae 12404 /* If the type is erroneous, skip the entire body of the class. */
a723baf1
MM
12405 cp_parser_skip_to_closing_brace (parser);
12406 else
12407 /* Parse the member-specification. */
12408 cp_parser_member_specification_opt (parser);
98ca843c 12409
a723baf1
MM
12410 /* Look for the trailing `}'. */
12411 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12412 /* We get better error messages by noticing a common problem: a
12413 missing trailing `;'. */
12414 token = cp_lexer_peek_token (parser->lexer);
12415 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
38b305d0 12416 /* Look for trailing attributes to apply to this class. */
a723baf1 12417 if (cp_parser_allow_gnu_extensions_p (parser))
560ad596 12418 {
38b305d0
JM
12419 tree sub_attr = cp_parser_attributes_opt (parser);
12420 attributes = chainon (attributes, sub_attr);
560ad596 12421 }
38b305d0
JM
12422 if (type != error_mark_node)
12423 type = finish_struct (type, attributes);
87c465f5
KL
12424 if (nested_name_specifier_p)
12425 pop_inner_scope (old_scope, scope);
a723baf1
MM
12426 /* If this class is not itself within the scope of another class,
12427 then we need to parse the bodies of all of the queued function
12428 definitions. Note that the queued functions defined in a class
12429 are not always processed immediately following the
12430 class-specifier for that class. Consider:
12431
12432 struct A {
12433 struct B { void f() { sizeof (A); } };
12434 };
12435
12436 If `f' were processed before the processing of `A' were
12437 completed, there would be no way to compute the size of `A'.
12438 Note that the nesting we are interested in here is lexical --
12439 not the semantic nesting given by TYPE_CONTEXT. In particular,
12440 for:
12441
12442 struct A { struct B; };
12443 struct A::B { void f() { } };
12444
12445 there is no need to delay the parsing of `A::B::f'. */
21526606 12446 if (--parser->num_classes_being_defined == 0)
a723baf1 12447 {
8218bd34
MM
12448 tree queue_entry;
12449 tree fn;
4514aa8c
NS
12450 tree class_type = NULL_TREE;
12451 tree pushed_scope = NULL_TREE;
a723baf1 12452
8218bd34
MM
12453 /* In a first pass, parse default arguments to the functions.
12454 Then, in a second pass, parse the bodies of the functions.
12455 This two-phased approach handles cases like:
21526606
EC
12456
12457 struct S {
12458 void f() { g(); }
8218bd34
MM
12459 void g(int i = 3);
12460 };
12461
12462 */
8db1028e
NS
12463 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12464 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12465 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12466 TREE_PURPOSE (parser->unparsed_functions_queues)
12467 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
8218bd34
MM
12468 {
12469 fn = TREE_VALUE (queue_entry);
8218bd34
MM
12470 /* If there are default arguments that have not yet been processed,
12471 take care of them now. */
f44b0c8e
MM
12472 if (class_type != TREE_PURPOSE (queue_entry))
12473 {
4514aa8c
NS
12474 if (pushed_scope)
12475 pop_scope (pushed_scope);
f44b0c8e 12476 class_type = TREE_PURPOSE (queue_entry);
4514aa8c 12477 pushed_scope = push_scope (class_type);
f44b0c8e
MM
12478 }
12479 /* Make sure that any template parameters are in scope. */
12480 maybe_begin_member_template_processing (fn);
12481 /* Parse the default argument expressions. */
8218bd34
MM
12482 cp_parser_late_parsing_default_args (parser, fn);
12483 /* Remove any template parameters from the symbol table. */
12484 maybe_end_member_template_processing ();
12485 }
4514aa8c
NS
12486 if (pushed_scope)
12487 pop_scope (pushed_scope);
8218bd34 12488 /* Now parse the body of the functions. */
8db1028e
NS
12489 for (TREE_VALUE (parser->unparsed_functions_queues)
12490 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12491 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12492 TREE_VALUE (parser->unparsed_functions_queues)
12493 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
a723baf1 12494 {
a723baf1 12495 /* Figure out which function we need to process. */
a723baf1
MM
12496 fn = TREE_VALUE (queue_entry);
12497
4543ee47
ZD
12498 /* A hack to prevent garbage collection. */
12499 function_depth++;
12500
a723baf1
MM
12501 /* Parse the function. */
12502 cp_parser_late_parsing_for_member (parser, fn);
4543ee47 12503 function_depth--;
a723baf1 12504 }
a723baf1
MM
12505 }
12506
12507 /* Put back any saved access checks. */
cf22909c 12508 pop_deferring_access_checks ();
a723baf1
MM
12509
12510 /* Restore the count of active template-parameter-lists. */
12511 parser->num_template_parameter_lists
12512 = saved_num_template_parameter_lists;
12513
12514 return type;
12515}
12516
12517/* Parse a class-head.
12518
12519 class-head:
12520 class-key identifier [opt] base-clause [opt]
12521 class-key nested-name-specifier identifier base-clause [opt]
21526606
EC
12522 class-key nested-name-specifier [opt] template-id
12523 base-clause [opt]
a723baf1
MM
12524
12525 GNU Extensions:
12526 class-key attributes identifier [opt] base-clause [opt]
12527 class-key attributes nested-name-specifier identifier base-clause [opt]
21526606
EC
12528 class-key attributes nested-name-specifier [opt] template-id
12529 base-clause [opt]
a723baf1
MM
12530
12531 Returns the TYPE of the indicated class. Sets
12532 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12533 involving a nested-name-specifier was used, and FALSE otherwise.
a723baf1 12534
55dcbc12
NS
12535 Returns error_mark_node if this is not a class-head.
12536
a723baf1
MM
12537 Returns NULL_TREE if the class-head is syntactically valid, but
12538 semantically invalid in a way that means we should skip the entire
12539 body of the class. */
12540
12541static tree
21526606 12542cp_parser_class_head (cp_parser* parser,
38b305d0
JM
12543 bool* nested_name_specifier_p,
12544 tree *attributes_p)
a723baf1 12545{
a723baf1
MM
12546 tree nested_name_specifier;
12547 enum tag_types class_key;
12548 tree id = NULL_TREE;
12549 tree type = NULL_TREE;
12550 tree attributes;
12551 bool template_id_p = false;
12552 bool qualified_p = false;
12553 bool invalid_nested_name_p = false;
afb0918a 12554 bool invalid_explicit_specialization_p = false;
4514aa8c 12555 tree pushed_scope = NULL_TREE;
a723baf1 12556 unsigned num_templates;
cad7e87b 12557 tree bases;
a723baf1
MM
12558
12559 /* Assume no nested-name-specifier will be present. */
12560 *nested_name_specifier_p = false;
12561 /* Assume no template parameter lists will be used in defining the
12562 type. */
12563 num_templates = 0;
12564
12565 /* Look for the class-key. */
12566 class_key = cp_parser_class_key (parser);
12567 if (class_key == none_type)
12568 return error_mark_node;
12569
12570 /* Parse the attributes. */
12571 attributes = cp_parser_attributes_opt (parser);
12572
12573 /* If the next token is `::', that is invalid -- but sometimes
12574 people do try to write:
12575
21526606 12576 struct ::S {};
a723baf1
MM
12577
12578 Handle this gracefully by accepting the extra qualifier, and then
12579 issuing an error about it later if this really is a
2050a1bb 12580 class-head. If it turns out just to be an elaborated type
a723baf1
MM
12581 specifier, remain silent. */
12582 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12583 qualified_p = true;
12584
8d241e0b
KL
12585 push_deferring_access_checks (dk_no_check);
12586
a723baf1
MM
12587 /* Determine the name of the class. Begin by looking for an
12588 optional nested-name-specifier. */
21526606 12589 nested_name_specifier
a723baf1
MM
12590 = cp_parser_nested_name_specifier_opt (parser,
12591 /*typename_keyword_p=*/false,
66d418e6 12592 /*check_dependency_p=*/false,
a668c6ad
MM
12593 /*type_p=*/false,
12594 /*is_declaration=*/false);
a723baf1
MM
12595 /* If there was a nested-name-specifier, then there *must* be an
12596 identifier. */
12597 if (nested_name_specifier)
12598 {
12599 /* Although the grammar says `identifier', it really means
12600 `class-name' or `template-name'. You are only allowed to
12601 define a class that has already been declared with this
21526606 12602 syntax.
a723baf1
MM
12603
12604 The proposed resolution for Core Issue 180 says that whever
12605 you see `class T::X' you should treat `X' as a type-name.
21526606 12606
a723baf1 12607 It is OK to define an inaccessible class; for example:
21526606 12608
a723baf1
MM
12609 class A { class B; };
12610 class A::B {};
21526606 12611
a723baf1
MM
12612 We do not know if we will see a class-name, or a
12613 template-name. We look for a class-name first, in case the
12614 class-name is a template-id; if we looked for the
12615 template-name first we would stop after the template-name. */
12616 cp_parser_parse_tentatively (parser);
12617 type = cp_parser_class_name (parser,
12618 /*typename_keyword_p=*/false,
12619 /*template_keyword_p=*/false,
fc6a28d7 12620 class_type,
a723baf1 12621 /*check_dependency_p=*/false,
a668c6ad
MM
12622 /*class_head_p=*/true,
12623 /*is_declaration=*/false);
a723baf1
MM
12624 /* If that didn't work, ignore the nested-name-specifier. */
12625 if (!cp_parser_parse_definitely (parser))
12626 {
12627 invalid_nested_name_p = true;
12628 id = cp_parser_identifier (parser);
12629 if (id == error_mark_node)
12630 id = NULL_TREE;
12631 }
12632 /* If we could not find a corresponding TYPE, treat this
12633 declaration like an unqualified declaration. */
12634 if (type == error_mark_node)
12635 nested_name_specifier = NULL_TREE;
12636 /* Otherwise, count the number of templates used in TYPE and its
12637 containing scopes. */
21526606 12638 else
a723baf1
MM
12639 {
12640 tree scope;
12641
21526606 12642 for (scope = TREE_TYPE (type);
a723baf1 12643 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21526606 12644 scope = (TYPE_P (scope)
a723baf1 12645 ? TYPE_CONTEXT (scope)
21526606
EC
12646 : DECL_CONTEXT (scope)))
12647 if (TYPE_P (scope)
a723baf1
MM
12648 && CLASS_TYPE_P (scope)
12649 && CLASSTYPE_TEMPLATE_INFO (scope)
2050a1bb
MM
12650 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12651 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
a723baf1
MM
12652 ++num_templates;
12653 }
12654 }
12655 /* Otherwise, the identifier is optional. */
12656 else
12657 {
12658 /* We don't know whether what comes next is a template-id,
12659 an identifier, or nothing at all. */
12660 cp_parser_parse_tentatively (parser);
12661 /* Check for a template-id. */
21526606 12662 id = cp_parser_template_id (parser,
a723baf1 12663 /*template_keyword_p=*/false,
a668c6ad
MM
12664 /*check_dependency_p=*/true,
12665 /*is_declaration=*/true);
a723baf1
MM
12666 /* If that didn't work, it could still be an identifier. */
12667 if (!cp_parser_parse_definitely (parser))
12668 {
12669 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12670 id = cp_parser_identifier (parser);
12671 else
12672 id = NULL_TREE;
12673 }
12674 else
12675 {
12676 template_id_p = true;
12677 ++num_templates;
12678 }
12679 }
12680
8d241e0b
KL
12681 pop_deferring_access_checks ();
12682
15077df5
MM
12683 if (id)
12684 cp_parser_check_for_invalid_template_id (parser, id);
ee43dab5 12685
a723baf1
MM
12686 /* If it's not a `:' or a `{' then we can't really be looking at a
12687 class-head, since a class-head only appears as part of a
12688 class-specifier. We have to detect this situation before calling
12689 xref_tag, since that has irreversible side-effects. */
12690 if (!cp_parser_next_token_starts_class_definition_p (parser))
12691 {
2a13a625 12692 cp_parser_error (parser, "expected %<{%> or %<:%>");
a723baf1
MM
12693 return error_mark_node;
12694 }
12695
12696 /* At this point, we're going ahead with the class-specifier, even
12697 if some other problem occurs. */
12698 cp_parser_commit_to_tentative_parse (parser);
12699 /* Issue the error about the overly-qualified name now. */
12700 if (qualified_p)
12701 cp_parser_error (parser,
12702 "global qualification of class name is invalid");
12703 else if (invalid_nested_name_p)
12704 cp_parser_error (parser,
12705 "qualified name does not name a class");
88081599
MM
12706 else if (nested_name_specifier)
12707 {
12708 tree scope;
9bf0e588
VR
12709
12710 /* Reject typedef-names in class heads. */
12711 if (!DECL_IMPLICIT_TYPEDEF_P (type))
12712 {
12713 error ("invalid class name in declaration of %qD", type);
12714 type = NULL_TREE;
12715 goto done;
12716 }
12717
88081599
MM
12718 /* Figure out in what scope the declaration is being placed. */
12719 scope = current_scope ();
88081599
MM
12720 /* If that scope does not contain the scope in which the
12721 class was originally declared, the program is invalid. */
12722 if (scope && !is_ancestor (scope, nested_name_specifier))
12723 {
2a13a625
GDR
12724 error ("declaration of %qD in %qD which does not enclose %qD",
12725 type, scope, nested_name_specifier);
88081599
MM
12726 type = NULL_TREE;
12727 goto done;
12728 }
12729 /* [dcl.meaning]
12730
12731 A declarator-id shall not be qualified exception of the
12732 definition of a ... nested class outside of its class
12733 ... [or] a the definition or explicit instantiation of a
12734 class member of a namespace outside of its namespace. */
12735 if (scope == nested_name_specifier)
12736 {
12737 pedwarn ("extra qualification ignored");
12738 nested_name_specifier = NULL_TREE;
12739 num_templates = 0;
12740 }
12741 }
afb0918a
MM
12742 /* An explicit-specialization must be preceded by "template <>". If
12743 it is not, try to recover gracefully. */
21526606 12744 if (at_namespace_scope_p ()
afb0918a 12745 && parser->num_template_parameter_lists == 0
eeb23c11 12746 && template_id_p)
afb0918a 12747 {
2a13a625 12748 error ("an explicit specialization must be preceded by %<template <>%>");
afb0918a
MM
12749 invalid_explicit_specialization_p = true;
12750 /* Take the same action that would have been taken by
12751 cp_parser_explicit_specialization. */
12752 ++parser->num_template_parameter_lists;
12753 begin_specialization ();
12754 }
12755 /* There must be no "return" statements between this point and the
12756 end of this function; set "type "to the correct return value and
12757 use "goto done;" to return. */
a723baf1
MM
12758 /* Make sure that the right number of template parameters were
12759 present. */
12760 if (!cp_parser_check_template_parameters (parser, num_templates))
afb0918a
MM
12761 {
12762 /* If something went wrong, there is no point in even trying to
12763 process the class-definition. */
12764 type = NULL_TREE;
12765 goto done;
12766 }
a723baf1 12767
a723baf1
MM
12768 /* Look up the type. */
12769 if (template_id_p)
12770 {
12771 type = TREE_TYPE (id);
12772 maybe_process_partial_specialization (type);
4514aa8c
NS
12773 if (nested_name_specifier)
12774 pushed_scope = push_scope (nested_name_specifier);
a723baf1 12775 }
4514aa8c 12776 else if (nested_name_specifier)
a723baf1 12777 {
a723baf1
MM
12778 tree class_type;
12779
12780 /* Given:
12781
12782 template <typename T> struct S { struct T };
14d22dd6 12783 template <typename T> struct S<T>::T { };
a723baf1
MM
12784
12785 we will get a TYPENAME_TYPE when processing the definition of
12786 `S::T'. We need to resolve it to the actual type before we
12787 try to define it. */
12788 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12789 {
14d22dd6
MM
12790 class_type = resolve_typename_type (TREE_TYPE (type),
12791 /*only_current_p=*/false);
12792 if (class_type != error_mark_node)
12793 type = TYPE_NAME (class_type);
12794 else
12795 {
12796 cp_parser_error (parser, "could not resolve typename type");
12797 type = error_mark_node;
12798 }
a723baf1
MM
12799 }
12800
560ad596
MM
12801 maybe_process_partial_specialization (TREE_TYPE (type));
12802 class_type = current_class_type;
12803 /* Enter the scope indicated by the nested-name-specifier. */
4514aa8c 12804 pushed_scope = push_scope (nested_name_specifier);
560ad596
MM
12805 /* Get the canonical version of this type. */
12806 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12807 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12808 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
55dcbc12
NS
12809 {
12810 type = push_template_decl (type);
12811 if (type == error_mark_node)
12812 {
12813 type = NULL_TREE;
12814 goto done;
12815 }
12816 }
12817
560ad596 12818 type = TREE_TYPE (type);
4514aa8c 12819 *nested_name_specifier_p = true;
a723baf1 12820 }
4514aa8c
NS
12821 else /* The name is not a nested name. */
12822 {
12823 /* If the class was unnamed, create a dummy name. */
12824 if (!id)
12825 id = make_anon_name ();
12826 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
12827 parser->num_template_parameter_lists);
12828 }
12829
a723baf1
MM
12830 /* Indicate whether this class was declared as a `class' or as a
12831 `struct'. */
12832 if (TREE_CODE (type) == RECORD_TYPE)
12833 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12834 cp_parser_check_class_key (class_key, type);
12835
4514aa8c
NS
12836 /* We will have entered the scope containing the class; the names of
12837 base classes should be looked up in that context. For example,
12838 given:
a723baf1
MM
12839
12840 struct A { struct B {}; struct C; };
12841 struct A::C : B {};
12842
12843 is valid. */
cad7e87b 12844 bases = NULL_TREE;
98ca843c 12845
cad7e87b
NS
12846 /* Get the list of base-classes, if there is one. */
12847 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12848 bases = cp_parser_base_clause (parser);
98ca843c 12849
cad7e87b
NS
12850 /* Process the base classes. */
12851 xref_basetypes (type, bases);
a723baf1 12852
4514aa8c 12853 done:
a723baf1
MM
12854 /* Leave the scope given by the nested-name-specifier. We will
12855 enter the class scope itself while processing the members. */
4514aa8c
NS
12856 if (pushed_scope)
12857 pop_scope (pushed_scope);
a723baf1 12858
afb0918a
MM
12859 if (invalid_explicit_specialization_p)
12860 {
12861 end_specialization ();
12862 --parser->num_template_parameter_lists;
12863 }
38b305d0 12864 *attributes_p = attributes;
a723baf1
MM
12865 return type;
12866}
12867
12868/* Parse a class-key.
12869
12870 class-key:
12871 class
12872 struct
12873 union
12874
12875 Returns the kind of class-key specified, or none_type to indicate
12876 error. */
12877
12878static enum tag_types
94edc4ab 12879cp_parser_class_key (cp_parser* parser)
a723baf1
MM
12880{
12881 cp_token *token;
12882 enum tag_types tag_type;
12883
12884 /* Look for the class-key. */
12885 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12886 if (!token)
12887 return none_type;
12888
12889 /* Check to see if the TOKEN is a class-key. */
12890 tag_type = cp_parser_token_is_class_key (token);
12891 if (!tag_type)
12892 cp_parser_error (parser, "expected class-key");
12893 return tag_type;
12894}
12895
12896/* Parse an (optional) member-specification.
12897
12898 member-specification:
12899 member-declaration member-specification [opt]
12900 access-specifier : member-specification [opt] */
12901
12902static void
94edc4ab 12903cp_parser_member_specification_opt (cp_parser* parser)
a723baf1
MM
12904{
12905 while (true)
12906 {
12907 cp_token *token;
12908 enum rid keyword;
12909
12910 /* Peek at the next token. */
12911 token = cp_lexer_peek_token (parser->lexer);
12912 /* If it's a `}', or EOF then we've seen all the members. */
12913 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12914 break;
12915
12916 /* See if this token is a keyword. */
12917 keyword = token->keyword;
12918 switch (keyword)
12919 {
12920 case RID_PUBLIC:
12921 case RID_PROTECTED:
12922 case RID_PRIVATE:
12923 /* Consume the access-specifier. */
12924 cp_lexer_consume_token (parser->lexer);
12925 /* Remember which access-specifier is active. */
12926 current_access_specifier = token->value;
12927 /* Look for the `:'. */
12928 cp_parser_require (parser, CPP_COLON, "`:'");
12929 break;
12930
12931 default:
de3fe73c
MM
12932 /* Accept #pragmas at class scope. */
12933 if (token->type == CPP_PRAGMA)
12934 {
12935 cp_lexer_handle_pragma (parser->lexer);
12936 break;
12937 }
12938
a723baf1
MM
12939 /* Otherwise, the next construction must be a
12940 member-declaration. */
12941 cp_parser_member_declaration (parser);
a723baf1
MM
12942 }
12943 }
12944}
12945
21526606 12946/* Parse a member-declaration.
a723baf1
MM
12947
12948 member-declaration:
12949 decl-specifier-seq [opt] member-declarator-list [opt] ;
12950 function-definition ; [opt]
12951 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12952 using-declaration
21526606 12953 template-declaration
a723baf1
MM
12954
12955 member-declarator-list:
12956 member-declarator
12957 member-declarator-list , member-declarator
12958
12959 member-declarator:
21526606 12960 declarator pure-specifier [opt]
a723baf1 12961 declarator constant-initializer [opt]
21526606 12962 identifier [opt] : constant-expression
a723baf1
MM
12963
12964 GNU Extensions:
12965
12966 member-declaration:
12967 __extension__ member-declaration
12968
12969 member-declarator:
12970 declarator attributes [opt] pure-specifier [opt]
12971 declarator attributes [opt] constant-initializer [opt]
12972 identifier [opt] attributes [opt] : constant-expression */
12973
12974static void
94edc4ab 12975cp_parser_member_declaration (cp_parser* parser)
a723baf1 12976{
62d1db17 12977 cp_decl_specifier_seq decl_specifiers;
a723baf1
MM
12978 tree prefix_attributes;
12979 tree decl;
560ad596 12980 int declares_class_or_enum;
a723baf1
MM
12981 bool friend_p;
12982 cp_token *token;
12983 int saved_pedantic;
12984
12985 /* Check for the `__extension__' keyword. */
12986 if (cp_parser_extension_opt (parser, &saved_pedantic))
12987 {
12988 /* Recurse. */
12989 cp_parser_member_declaration (parser);
12990 /* Restore the old value of the PEDANTIC flag. */
12991 pedantic = saved_pedantic;
12992
12993 return;
12994 }
12995
12996 /* Check for a template-declaration. */
12997 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12998 {
12999 /* Parse the template-declaration. */
13000 cp_parser_template_declaration (parser, /*member_p=*/true);
13001
13002 return;
13003 }
13004
13005 /* Check for a using-declaration. */
13006 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13007 {
13008 /* Parse the using-declaration. */
13009 cp_parser_using_declaration (parser);
13010
13011 return;
13012 }
21526606 13013
a723baf1 13014 /* Parse the decl-specifier-seq. */
62d1db17
MM
13015 cp_parser_decl_specifier_seq (parser,
13016 CP_PARSER_FLAGS_OPTIONAL,
13017 &decl_specifiers,
13018 &declares_class_or_enum);
13019 prefix_attributes = decl_specifiers.attributes;
13020 decl_specifiers.attributes = NULL_TREE;
8fbc5ae7 13021 /* Check for an invalid type-name. */
de3fe73c
MM
13022 if (!decl_specifiers.type
13023 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8fbc5ae7 13024 return;
a723baf1
MM
13025 /* If there is no declarator, then the decl-specifier-seq should
13026 specify a type. */
13027 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13028 {
13029 /* If there was no decl-specifier-seq, and the next token is a
13030 `;', then we have something like:
13031
13032 struct S { ; };
13033
13034 [class.mem]
13035
13036 Each member-declaration shall declare at least one member
13037 name of the class. */
62d1db17 13038 if (!decl_specifiers.any_specifiers_p)
a723baf1 13039 {
2cfe82fe
ZW
13040 cp_token *token = cp_lexer_peek_token (parser->lexer);
13041 if (pedantic && !token->in_system_header)
13042 pedwarn ("%Hextra %<;%>", &token->location);
a723baf1 13043 }
21526606 13044 else
a723baf1
MM
13045 {
13046 tree type;
21526606 13047
a723baf1 13048 /* See if this declaration is a friend. */
62d1db17 13049 friend_p = cp_parser_friend_p (&decl_specifiers);
a723baf1
MM
13050 /* If there were decl-specifiers, check to see if there was
13051 a class-declaration. */
62d1db17 13052 type = check_tag_decl (&decl_specifiers);
a723baf1
MM
13053 /* Nested classes have already been added to the class, but
13054 a `friend' needs to be explicitly registered. */
13055 if (friend_p)
13056 {
13057 /* If the `friend' keyword was present, the friend must
13058 be introduced with a class-key. */
13059 if (!declares_class_or_enum)
13060 error ("a class-key must be used when declaring a friend");
13061 /* In this case:
13062
21526606
EC
13063 template <typename T> struct A {
13064 friend struct A<T>::B;
a723baf1 13065 };
21526606 13066
a723baf1
MM
13067 A<T>::B will be represented by a TYPENAME_TYPE, and
13068 therefore not recognized by check_tag_decl. */
98ca843c 13069 if (!type
62d1db17
MM
13070 && decl_specifiers.type
13071 && TYPE_P (decl_specifiers.type))
13072 type = decl_specifiers.type;
fdd09134 13073 if (!type || !TYPE_P (type))
a723baf1
MM
13074 error ("friend declaration does not name a class or "
13075 "function");
13076 else
19db77ce
KL
13077 make_friend_class (current_class_type, type,
13078 /*complain=*/true);
a723baf1
MM
13079 }
13080 /* If there is no TYPE, an error message will already have
13081 been issued. */
62d1db17 13082 else if (!type || type == error_mark_node)
a723baf1
MM
13083 ;
13084 /* An anonymous aggregate has to be handled specially; such
13085 a declaration really declares a data member (with a
13086 particular type), as opposed to a nested class. */
13087 else if (ANON_AGGR_TYPE_P (type))
13088 {
13089 /* Remove constructors and such from TYPE, now that we
34cd5ae7 13090 know it is an anonymous aggregate. */
a723baf1
MM
13091 fixup_anonymous_aggr (type);
13092 /* And make the corresponding data member. */
13093 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13094 /* Add it to the class. */
13095 finish_member_declaration (decl);
13096 }
37d407a1
KL
13097 else
13098 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
a723baf1
MM
13099 }
13100 }
13101 else
13102 {
13103 /* See if these declarations will be friends. */
62d1db17 13104 friend_p = cp_parser_friend_p (&decl_specifiers);
a723baf1 13105
21526606 13106 /* Keep going until we hit the `;' at the end of the
a723baf1
MM
13107 declaration. */
13108 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13109 {
13110 tree attributes = NULL_TREE;
13111 tree first_attribute;
13112
13113 /* Peek at the next token. */
13114 token = cp_lexer_peek_token (parser->lexer);
13115
13116 /* Check for a bitfield declaration. */
13117 if (token->type == CPP_COLON
13118 || (token->type == CPP_NAME
21526606 13119 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1
MM
13120 == CPP_COLON))
13121 {
13122 tree identifier;
13123 tree width;
13124
13125 /* Get the name of the bitfield. Note that we cannot just
13126 check TOKEN here because it may have been invalidated by
13127 the call to cp_lexer_peek_nth_token above. */
13128 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13129 identifier = cp_parser_identifier (parser);
13130 else
13131 identifier = NULL_TREE;
13132
13133 /* Consume the `:' token. */
13134 cp_lexer_consume_token (parser->lexer);
13135 /* Get the width of the bitfield. */
21526606 13136 width
14d22dd6
MM
13137 = cp_parser_constant_expression (parser,
13138 /*allow_non_constant=*/false,
13139 NULL);
a723baf1
MM
13140
13141 /* Look for attributes that apply to the bitfield. */
13142 attributes = cp_parser_attributes_opt (parser);
13143 /* Remember which attributes are prefix attributes and
13144 which are not. */
13145 first_attribute = attributes;
13146 /* Combine the attributes. */
13147 attributes = chainon (prefix_attributes, attributes);
13148
13149 /* Create the bitfield declaration. */
98ca843c 13150 decl = grokbitfield (identifier
1d786913
MM
13151 ? make_id_declarator (NULL_TREE,
13152 identifier)
058b15c1 13153 : NULL,
62d1db17 13154 &decl_specifiers,
a723baf1
MM
13155 width);
13156 /* Apply the attributes. */
13157 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13158 }
13159 else
13160 {
058b15c1 13161 cp_declarator *declarator;
a723baf1
MM
13162 tree initializer;
13163 tree asm_specification;
7efa3e22 13164 int ctor_dtor_or_conv_p;
a723baf1
MM
13165
13166 /* Parse the declarator. */
21526606 13167 declarator
62b8a44e 13168 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 13169 &ctor_dtor_or_conv_p,
db86dd14
MM
13170 /*parenthesized_p=*/NULL,
13171 /*member_p=*/true);
a723baf1
MM
13172
13173 /* If something went wrong parsing the declarator, make sure
13174 that we at least consume some tokens. */
058b15c1 13175 if (declarator == cp_error_declarator)
a723baf1
MM
13176 {
13177 /* Skip to the end of the statement. */
13178 cp_parser_skip_to_end_of_statement (parser);
4bb8ca28
MM
13179 /* If the next token is not a semicolon, that is
13180 probably because we just skipped over the body of
13181 a function. So, we consume a semicolon if
13182 present, but do not issue an error message if it
13183 is not present. */
13184 if (cp_lexer_next_token_is (parser->lexer,
13185 CPP_SEMICOLON))
13186 cp_lexer_consume_token (parser->lexer);
13187 return;
a723baf1
MM
13188 }
13189
fc6a28d7
MM
13190 if (declares_class_or_enum & 2)
13191 cp_parser_check_for_definition_in_return_type
13192 (declarator, decl_specifiers.type);
560ad596 13193
a723baf1
MM
13194 /* Look for an asm-specification. */
13195 asm_specification = cp_parser_asm_specification_opt (parser);
13196 /* Look for attributes that apply to the declaration. */
13197 attributes = cp_parser_attributes_opt (parser);
13198 /* Remember which attributes are prefix attributes and
13199 which are not. */
13200 first_attribute = attributes;
13201 /* Combine the attributes. */
13202 attributes = chainon (prefix_attributes, attributes);
13203
13204 /* If it's an `=', then we have a constant-initializer or a
13205 pure-specifier. It is not correct to parse the
13206 initializer before registering the member declaration
13207 since the member declaration should be in scope while
13208 its initializer is processed. However, the rest of the
13209 front end does not yet provide an interface that allows
13210 us to handle this correctly. */
13211 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13212 {
13213 /* In [class.mem]:
13214
13215 A pure-specifier shall be used only in the declaration of
21526606 13216 a virtual function.
a723baf1
MM
13217
13218 A member-declarator can contain a constant-initializer
13219 only if it declares a static member of integral or
21526606 13220 enumeration type.
a723baf1
MM
13221
13222 Therefore, if the DECLARATOR is for a function, we look
13223 for a pure-specifier; otherwise, we look for a
13224 constant-initializer. When we call `grokfield', it will
13225 perform more stringent semantics checks. */
058b15c1 13226 if (declarator->kind == cdk_function)
a723baf1
MM
13227 initializer = cp_parser_pure_specifier (parser);
13228 else
4bb8ca28
MM
13229 /* Parse the initializer. */
13230 initializer = cp_parser_constant_initializer (parser);
a723baf1
MM
13231 }
13232 /* Otherwise, there is no initializer. */
13233 else
13234 initializer = NULL_TREE;
13235
13236 /* See if we are probably looking at a function
5a19910e 13237 definition. We are certainly not looking at a
a723baf1
MM
13238 member-declarator. Calling `grokfield' has
13239 side-effects, so we must not do it unless we are sure
13240 that we are looking at a member-declarator. */
21526606 13241 if (cp_parser_token_starts_function_definition_p
a723baf1 13242 (cp_lexer_peek_token (parser->lexer)))
4bb8ca28
MM
13243 {
13244 /* The grammar does not allow a pure-specifier to be
13245 used when a member function is defined. (It is
13246 possible that this fact is an oversight in the
13247 standard, since a pure function may be defined
13248 outside of the class-specifier. */
13249 if (initializer)
13250 error ("pure-specifier on function-definition");
13251 decl = cp_parser_save_member_function_body (parser,
62d1db17 13252 &decl_specifiers,
4bb8ca28
MM
13253 declarator,
13254 attributes);
13255 /* If the member was not a friend, declare it here. */
13256 if (!friend_p)
13257 finish_member_declaration (decl);
13258 /* Peek at the next token. */
13259 token = cp_lexer_peek_token (parser->lexer);
13260 /* If the next token is a semicolon, consume it. */
13261 if (token->type == CPP_SEMICOLON)
13262 cp_lexer_consume_token (parser->lexer);
13263 return;
13264 }
a723baf1 13265 else
39703eb9
MM
13266 {
13267 /* Create the declaration. */
62d1db17 13268 decl = grokfield (declarator, &decl_specifiers,
ee3071ef 13269 initializer, asm_specification,
39703eb9
MM
13270 attributes);
13271 /* Any initialization must have been from a
13272 constant-expression. */
13273 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13274 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13275 }
a723baf1
MM
13276 }
13277
13278 /* Reset PREFIX_ATTRIBUTES. */
13279 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13280 attributes = TREE_CHAIN (attributes);
13281 if (attributes)
13282 TREE_CHAIN (attributes) = NULL_TREE;
13283
13284 /* If there is any qualification still in effect, clear it
13285 now; we will be starting fresh with the next declarator. */
13286 parser->scope = NULL_TREE;
13287 parser->qualifying_scope = NULL_TREE;
13288 parser->object_scope = NULL_TREE;
13289 /* If it's a `,', then there are more declarators. */
13290 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13291 cp_lexer_consume_token (parser->lexer);
13292 /* If the next token isn't a `;', then we have a parse error. */
13293 else if (cp_lexer_next_token_is_not (parser->lexer,
13294 CPP_SEMICOLON))
13295 {
2a13a625 13296 cp_parser_error (parser, "expected %<;%>");
04c06002 13297 /* Skip tokens until we find a `;'. */
a723baf1
MM
13298 cp_parser_skip_to_end_of_statement (parser);
13299
13300 break;
13301 }
13302
13303 if (decl)
13304 {
13305 /* Add DECL to the list of members. */
13306 if (!friend_p)
13307 finish_member_declaration (decl);
13308
a723baf1 13309 if (TREE_CODE (decl) == FUNCTION_DECL)
8db1028e 13310 cp_parser_save_default_args (parser, decl);
a723baf1
MM
13311 }
13312 }
13313 }
13314
4bb8ca28 13315 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
a723baf1
MM
13316}
13317
13318/* Parse a pure-specifier.
13319
13320 pure-specifier:
13321 = 0
13322
13323 Returns INTEGER_ZERO_NODE if a pure specifier is found.
cd0be382 13324 Otherwise, ERROR_MARK_NODE is returned. */
a723baf1
MM
13325
13326static tree
94edc4ab 13327cp_parser_pure_specifier (cp_parser* parser)
a723baf1
MM
13328{
13329 cp_token *token;
13330
13331 /* Look for the `=' token. */
13332 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13333 return error_mark_node;
13334 /* Look for the `0' token. */
515e6a84
GB
13335 token = cp_lexer_consume_token (parser->lexer);
13336 if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13337 {
13338 cp_parser_error (parser,
13339 "invalid pure specifier (only `= 0' is allowed)");
13340 cp_parser_skip_to_end_of_statement (parser);
13341 return error_mark_node;
13342 }
a723baf1 13343
515e6a84
GB
13344 /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13345 We need to get information from the lexer about how the number
13346 was spelled in order to fix this problem. */
a723baf1
MM
13347 return integer_zero_node;
13348}
13349
13350/* Parse a constant-initializer.
13351
13352 constant-initializer:
13353 = constant-expression
13354
13355 Returns a representation of the constant-expression. */
13356
13357static tree
94edc4ab 13358cp_parser_constant_initializer (cp_parser* parser)
a723baf1
MM
13359{
13360 /* Look for the `=' token. */
13361 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13362 return error_mark_node;
13363
13364 /* It is invalid to write:
13365
13366 struct S { static const int i = { 7 }; };
13367
13368 */
13369 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13370 {
13371 cp_parser_error (parser,
13372 "a brace-enclosed initializer is not allowed here");
13373 /* Consume the opening brace. */
13374 cp_lexer_consume_token (parser->lexer);
13375 /* Skip the initializer. */
13376 cp_parser_skip_to_closing_brace (parser);
13377 /* Look for the trailing `}'. */
13378 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
21526606 13379
a723baf1
MM
13380 return error_mark_node;
13381 }
13382
21526606 13383 return cp_parser_constant_expression (parser,
14d22dd6
MM
13384 /*allow_non_constant=*/false,
13385 NULL);
a723baf1
MM
13386}
13387
13388/* Derived classes [gram.class.derived] */
13389
13390/* Parse a base-clause.
13391
13392 base-clause:
21526606 13393 : base-specifier-list
a723baf1
MM
13394
13395 base-specifier-list:
13396 base-specifier
13397 base-specifier-list , base-specifier
13398
13399 Returns a TREE_LIST representing the base-classes, in the order in
13400 which they were declared. The representation of each node is as
21526606 13401 described by cp_parser_base_specifier.
a723baf1
MM
13402
13403 In the case that no bases are specified, this function will return
13404 NULL_TREE, not ERROR_MARK_NODE. */
13405
13406static tree
94edc4ab 13407cp_parser_base_clause (cp_parser* parser)
a723baf1
MM
13408{
13409 tree bases = NULL_TREE;
13410
13411 /* Look for the `:' that begins the list. */
13412 cp_parser_require (parser, CPP_COLON, "`:'");
13413
13414 /* Scan the base-specifier-list. */
13415 while (true)
13416 {
13417 cp_token *token;
13418 tree base;
13419
13420 /* Look for the base-specifier. */
13421 base = cp_parser_base_specifier (parser);
13422 /* Add BASE to the front of the list. */
13423 if (base != error_mark_node)
13424 {
13425 TREE_CHAIN (base) = bases;
13426 bases = base;
13427 }
13428 /* Peek at the next token. */
13429 token = cp_lexer_peek_token (parser->lexer);
13430 /* If it's not a comma, then the list is complete. */
13431 if (token->type != CPP_COMMA)
13432 break;
13433 /* Consume the `,'. */
13434 cp_lexer_consume_token (parser->lexer);
13435 }
13436
13437 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13438 base class had a qualified name. However, the next name that
13439 appears is certainly not qualified. */
13440 parser->scope = NULL_TREE;
13441 parser->qualifying_scope = NULL_TREE;
13442 parser->object_scope = NULL_TREE;
13443
13444 return nreverse (bases);
13445}
13446
13447/* Parse a base-specifier.
13448
13449 base-specifier:
13450 :: [opt] nested-name-specifier [opt] class-name
13451 virtual access-specifier [opt] :: [opt] nested-name-specifier
13452 [opt] class-name
13453 access-specifier virtual [opt] :: [opt] nested-name-specifier
13454 [opt] class-name
13455
13456 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13457 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13458 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13459 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21526606 13460
a723baf1 13461static tree
94edc4ab 13462cp_parser_base_specifier (cp_parser* parser)
a723baf1
MM
13463{
13464 cp_token *token;
13465 bool done = false;
13466 bool virtual_p = false;
13467 bool duplicate_virtual_error_issued_p = false;
13468 bool duplicate_access_error_issued_p = false;
bbaab916 13469 bool class_scope_p, template_p;
dbbf88d1 13470 tree access = access_default_node;
a723baf1
MM
13471 tree type;
13472
13473 /* Process the optional `virtual' and `access-specifier'. */
13474 while (!done)
13475 {
13476 /* Peek at the next token. */
13477 token = cp_lexer_peek_token (parser->lexer);
13478 /* Process `virtual'. */
13479 switch (token->keyword)
13480 {
13481 case RID_VIRTUAL:
13482 /* If `virtual' appears more than once, issue an error. */
13483 if (virtual_p && !duplicate_virtual_error_issued_p)
13484 {
13485 cp_parser_error (parser,
2a13a625 13486 "%<virtual%> specified more than once in base-specified");
a723baf1
MM
13487 duplicate_virtual_error_issued_p = true;
13488 }
13489
13490 virtual_p = true;
13491
13492 /* Consume the `virtual' token. */
13493 cp_lexer_consume_token (parser->lexer);
13494
13495 break;
13496
13497 case RID_PUBLIC:
13498 case RID_PROTECTED:
13499 case RID_PRIVATE:
13500 /* If more than one access specifier appears, issue an
13501 error. */
dbbf88d1
NS
13502 if (access != access_default_node
13503 && !duplicate_access_error_issued_p)
a723baf1
MM
13504 {
13505 cp_parser_error (parser,
13506 "more than one access specifier in base-specified");
13507 duplicate_access_error_issued_p = true;
13508 }
13509
dbbf88d1 13510 access = ridpointers[(int) token->keyword];
a723baf1
MM
13511
13512 /* Consume the access-specifier. */
13513 cp_lexer_consume_token (parser->lexer);
13514
13515 break;
13516
13517 default:
13518 done = true;
13519 break;
13520 }
13521 }
852dcbdd 13522 /* It is not uncommon to see programs mechanically, erroneously, use
a3a503a5 13523 the 'typename' keyword to denote (dependent) qualified types
1ed53ef3
GB
13524 as base classes. */
13525 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13526 {
13527 if (!processing_template_decl)
2a13a625 13528 error ("keyword %<typename%> not allowed outside of templates");
1ed53ef3 13529 else
2a13a625 13530 error ("keyword %<typename%> not allowed in this context "
1ed53ef3
GB
13531 "(the base class is implicitly a type)");
13532 cp_lexer_consume_token (parser->lexer);
13533 }
a723baf1 13534
a723baf1
MM
13535 /* Look for the optional `::' operator. */
13536 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13537 /* Look for the nested-name-specifier. The simplest way to
13538 implement:
13539
13540 [temp.res]
13541
13542 The keyword `typename' is not permitted in a base-specifier or
13543 mem-initializer; in these contexts a qualified name that
13544 depends on a template-parameter is implicitly assumed to be a
13545 type name.
13546
13547 is to pretend that we have seen the `typename' keyword at this
21526606 13548 point. */
a723baf1
MM
13549 cp_parser_nested_name_specifier_opt (parser,
13550 /*typename_keyword_p=*/true,
13551 /*check_dependency_p=*/true,
fc6a28d7 13552 typename_type,
a668c6ad 13553 /*is_declaration=*/true);
a723baf1
MM
13554 /* If the base class is given by a qualified name, assume that names
13555 we see are type names or templates, as appropriate. */
13556 class_scope_p = (parser->scope && TYPE_P (parser->scope));
bbaab916 13557 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21526606 13558
a723baf1 13559 /* Finally, look for the class-name. */
21526606 13560 type = cp_parser_class_name (parser,
a723baf1 13561 class_scope_p,
bbaab916 13562 template_p,
fc6a28d7 13563 typename_type,
a723baf1 13564 /*check_dependency_p=*/true,
a668c6ad
MM
13565 /*class_head_p=*/false,
13566 /*is_declaration=*/true);
a723baf1
MM
13567
13568 if (type == error_mark_node)
13569 return error_mark_node;
13570
dbbf88d1 13571 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
a723baf1
MM
13572}
13573
13574/* Exception handling [gram.exception] */
13575
13576/* Parse an (optional) exception-specification.
13577
13578 exception-specification:
13579 throw ( type-id-list [opt] )
13580
13581 Returns a TREE_LIST representing the exception-specification. The
13582 TREE_VALUE of each node is a type. */
13583
13584static tree
94edc4ab 13585cp_parser_exception_specification_opt (cp_parser* parser)
a723baf1
MM
13586{
13587 cp_token *token;
13588 tree type_id_list;
13589
13590 /* Peek at the next token. */
13591 token = cp_lexer_peek_token (parser->lexer);
13592 /* If it's not `throw', then there's no exception-specification. */
13593 if (!cp_parser_is_keyword (token, RID_THROW))
13594 return NULL_TREE;
13595
13596 /* Consume the `throw'. */
13597 cp_lexer_consume_token (parser->lexer);
13598
13599 /* Look for the `('. */
13600 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13601
13602 /* Peek at the next token. */
13603 token = cp_lexer_peek_token (parser->lexer);
13604 /* If it's not a `)', then there is a type-id-list. */
13605 if (token->type != CPP_CLOSE_PAREN)
13606 {
13607 const char *saved_message;
13608
13609 /* Types may not be defined in an exception-specification. */
13610 saved_message = parser->type_definition_forbidden_message;
13611 parser->type_definition_forbidden_message
13612 = "types may not be defined in an exception-specification";
13613 /* Parse the type-id-list. */
13614 type_id_list = cp_parser_type_id_list (parser);
13615 /* Restore the saved message. */
13616 parser->type_definition_forbidden_message = saved_message;
13617 }
13618 else
13619 type_id_list = empty_except_spec;
13620
13621 /* Look for the `)'. */
13622 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13623
13624 return type_id_list;
13625}
13626
13627/* Parse an (optional) type-id-list.
13628
13629 type-id-list:
13630 type-id
13631 type-id-list , type-id
13632
13633 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13634 in the order that the types were presented. */
13635
13636static tree
94edc4ab 13637cp_parser_type_id_list (cp_parser* parser)
a723baf1
MM
13638{
13639 tree types = NULL_TREE;
13640
13641 while (true)
13642 {
13643 cp_token *token;
13644 tree type;
13645
13646 /* Get the next type-id. */
13647 type = cp_parser_type_id (parser);
13648 /* Add it to the list. */
13649 types = add_exception_specifier (types, type, /*complain=*/1);
13650 /* Peek at the next token. */
13651 token = cp_lexer_peek_token (parser->lexer);
13652 /* If it is not a `,', we are done. */
13653 if (token->type != CPP_COMMA)
13654 break;
13655 /* Consume the `,'. */
13656 cp_lexer_consume_token (parser->lexer);
13657 }
13658
13659 return nreverse (types);
13660}
13661
13662/* Parse a try-block.
13663
13664 try-block:
13665 try compound-statement handler-seq */
13666
13667static tree
94edc4ab 13668cp_parser_try_block (cp_parser* parser)
a723baf1
MM
13669{
13670 tree try_block;
13671
13672 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13673 try_block = begin_try_block ();
325c3691 13674 cp_parser_compound_statement (parser, NULL, true);
a723baf1
MM
13675 finish_try_block (try_block);
13676 cp_parser_handler_seq (parser);
13677 finish_handler_sequence (try_block);
13678
13679 return try_block;
13680}
13681
13682/* Parse a function-try-block.
13683
13684 function-try-block:
13685 try ctor-initializer [opt] function-body handler-seq */
13686
13687static bool
94edc4ab 13688cp_parser_function_try_block (cp_parser* parser)
a723baf1
MM
13689{
13690 tree try_block;
13691 bool ctor_initializer_p;
13692
13693 /* Look for the `try' keyword. */
13694 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13695 return false;
13696 /* Let the rest of the front-end know where we are. */
13697 try_block = begin_function_try_block ();
13698 /* Parse the function-body. */
21526606 13699 ctor_initializer_p
a723baf1
MM
13700 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13701 /* We're done with the `try' part. */
13702 finish_function_try_block (try_block);
13703 /* Parse the handlers. */
13704 cp_parser_handler_seq (parser);
13705 /* We're done with the handlers. */
13706 finish_function_handler_sequence (try_block);
13707
13708 return ctor_initializer_p;
13709}
13710
13711/* Parse a handler-seq.
13712
13713 handler-seq:
13714 handler handler-seq [opt] */
13715
13716static void
94edc4ab 13717cp_parser_handler_seq (cp_parser* parser)
a723baf1
MM
13718{
13719 while (true)
13720 {
13721 cp_token *token;
13722
13723 /* Parse the handler. */
13724 cp_parser_handler (parser);
13725 /* Peek at the next token. */
13726 token = cp_lexer_peek_token (parser->lexer);
13727 /* If it's not `catch' then there are no more handlers. */
13728 if (!cp_parser_is_keyword (token, RID_CATCH))
13729 break;
13730 }
13731}
13732
13733/* Parse a handler.
13734
13735 handler:
13736 catch ( exception-declaration ) compound-statement */
13737
13738static void
94edc4ab 13739cp_parser_handler (cp_parser* parser)
a723baf1
MM
13740{
13741 tree handler;
13742 tree declaration;
13743
13744 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13745 handler = begin_handler ();
13746 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13747 declaration = cp_parser_exception_declaration (parser);
13748 finish_handler_parms (declaration, handler);
13749 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
325c3691 13750 cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
13751 finish_handler (handler);
13752}
13753
13754/* Parse an exception-declaration.
13755
13756 exception-declaration:
13757 type-specifier-seq declarator
13758 type-specifier-seq abstract-declarator
13759 type-specifier-seq
21526606 13760 ...
a723baf1
MM
13761
13762 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13763 ellipsis variant is used. */
13764
13765static tree
94edc4ab 13766cp_parser_exception_declaration (cp_parser* parser)
a723baf1 13767{
058b15c1 13768 tree decl;
62d1db17 13769 cp_decl_specifier_seq type_specifiers;
058b15c1 13770 cp_declarator *declarator;
a723baf1
MM
13771 const char *saved_message;
13772
13773 /* If it's an ellipsis, it's easy to handle. */
13774 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13775 {
13776 /* Consume the `...' token. */
13777 cp_lexer_consume_token (parser->lexer);
13778 return NULL_TREE;
13779 }
13780
13781 /* Types may not be defined in exception-declarations. */
13782 saved_message = parser->type_definition_forbidden_message;
13783 parser->type_definition_forbidden_message
13784 = "types may not be defined in exception-declarations";
13785
13786 /* Parse the type-specifier-seq. */
62d1db17 13787 cp_parser_type_specifier_seq (parser, &type_specifiers);
a723baf1
MM
13788 /* If it's a `)', then there is no declarator. */
13789 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
058b15c1 13790 declarator = NULL;
a723baf1 13791 else
62b8a44e 13792 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
4bb8ca28 13793 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
13794 /*parenthesized_p=*/NULL,
13795 /*member_p=*/false);
a723baf1
MM
13796
13797 /* Restore the saved message. */
13798 parser->type_definition_forbidden_message = saved_message;
13799
62d1db17 13800 if (type_specifiers.any_specifiers_p)
058b15c1 13801 {
62d1db17 13802 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
058b15c1
MM
13803 if (decl == NULL_TREE)
13804 error ("invalid catch parameter");
13805 }
13806 else
13807 decl = NULL_TREE;
13808
13809 return decl;
a723baf1
MM
13810}
13811
21526606 13812/* Parse a throw-expression.
a723baf1
MM
13813
13814 throw-expression:
34cd5ae7 13815 throw assignment-expression [opt]
a723baf1
MM
13816
13817 Returns a THROW_EXPR representing the throw-expression. */
13818
13819static tree
94edc4ab 13820cp_parser_throw_expression (cp_parser* parser)
a723baf1
MM
13821{
13822 tree expression;
89f1a6ec 13823 cp_token* token;
a723baf1
MM
13824
13825 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
89f1a6ec
MM
13826 token = cp_lexer_peek_token (parser->lexer);
13827 /* Figure out whether or not there is an assignment-expression
13828 following the "throw" keyword. */
13829 if (token->type == CPP_COMMA
13830 || token->type == CPP_SEMICOLON
13831 || token->type == CPP_CLOSE_PAREN
13832 || token->type == CPP_CLOSE_SQUARE
13833 || token->type == CPP_CLOSE_BRACE
13834 || token->type == CPP_COLON)
a723baf1 13835 expression = NULL_TREE;
89f1a6ec 13836 else
93678513
MM
13837 expression = cp_parser_assignment_expression (parser,
13838 /*cast_p=*/false);
a723baf1
MM
13839
13840 return build_throw (expression);
13841}
13842
13843/* GNU Extensions */
13844
13845/* Parse an (optional) asm-specification.
13846
13847 asm-specification:
13848 asm ( string-literal )
13849
13850 If the asm-specification is present, returns a STRING_CST
13851 corresponding to the string-literal. Otherwise, returns
13852 NULL_TREE. */
13853
13854static tree
94edc4ab 13855cp_parser_asm_specification_opt (cp_parser* parser)
a723baf1
MM
13856{
13857 cp_token *token;
13858 tree asm_specification;
13859
13860 /* Peek at the next token. */
13861 token = cp_lexer_peek_token (parser->lexer);
21526606 13862 /* If the next token isn't the `asm' keyword, then there's no
a723baf1
MM
13863 asm-specification. */
13864 if (!cp_parser_is_keyword (token, RID_ASM))
13865 return NULL_TREE;
13866
13867 /* Consume the `asm' token. */
13868 cp_lexer_consume_token (parser->lexer);
13869 /* Look for the `('. */
13870 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13871
13872 /* Look for the string-literal. */
c162c75e 13873 asm_specification = cp_parser_string_literal (parser, false, false);
a723baf1
MM
13874
13875 /* Look for the `)'. */
13876 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13877
13878 return asm_specification;
13879}
13880
21526606 13881/* Parse an asm-operand-list.
a723baf1
MM
13882
13883 asm-operand-list:
13884 asm-operand
13885 asm-operand-list , asm-operand
21526606 13886
a723baf1 13887 asm-operand:
21526606 13888 string-literal ( expression )
a723baf1
MM
13889 [ string-literal ] string-literal ( expression )
13890
13891 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13892 each node is the expression. The TREE_PURPOSE is itself a
13893 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13894 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13895 is a STRING_CST for the string literal before the parenthesis. */
13896
13897static tree
94edc4ab 13898cp_parser_asm_operand_list (cp_parser* parser)
a723baf1
MM
13899{
13900 tree asm_operands = NULL_TREE;
13901
13902 while (true)
13903 {
13904 tree string_literal;
13905 tree expression;
13906 tree name;
21526606 13907
21526606 13908 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
a723baf1
MM
13909 {
13910 /* Consume the `[' token. */
13911 cp_lexer_consume_token (parser->lexer);
13912 /* Read the operand name. */
13913 name = cp_parser_identifier (parser);
21526606 13914 if (name != error_mark_node)
a723baf1
MM
13915 name = build_string (IDENTIFIER_LENGTH (name),
13916 IDENTIFIER_POINTER (name));
13917 /* Look for the closing `]'. */
13918 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13919 }
13920 else
13921 name = NULL_TREE;
13922 /* Look for the string-literal. */
c162c75e
MA
13923 string_literal = cp_parser_string_literal (parser, false, false);
13924
a723baf1
MM
13925 /* Look for the `('. */
13926 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13927 /* Parse the expression. */
93678513 13928 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
13929 /* Look for the `)'. */
13930 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
c162c75e 13931
a723baf1
MM
13932 /* Add this operand to the list. */
13933 asm_operands = tree_cons (build_tree_list (name, string_literal),
21526606 13934 expression,
a723baf1 13935 asm_operands);
21526606 13936 /* If the next token is not a `,', there are no more
a723baf1
MM
13937 operands. */
13938 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13939 break;
13940 /* Consume the `,'. */
13941 cp_lexer_consume_token (parser->lexer);
13942 }
13943
13944 return nreverse (asm_operands);
13945}
13946
21526606 13947/* Parse an asm-clobber-list.
a723baf1
MM
13948
13949 asm-clobber-list:
13950 string-literal
21526606 13951 asm-clobber-list , string-literal
a723baf1
MM
13952
13953 Returns a TREE_LIST, indicating the clobbers in the order that they
13954 appeared. The TREE_VALUE of each node is a STRING_CST. */
13955
13956static tree
94edc4ab 13957cp_parser_asm_clobber_list (cp_parser* parser)
a723baf1
MM
13958{
13959 tree clobbers = NULL_TREE;
13960
13961 while (true)
13962 {
a723baf1
MM
13963 tree string_literal;
13964
13965 /* Look for the string literal. */
c162c75e 13966 string_literal = cp_parser_string_literal (parser, false, false);
a723baf1
MM
13967 /* Add it to the list. */
13968 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21526606 13969 /* If the next token is not a `,', then the list is
a723baf1
MM
13970 complete. */
13971 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13972 break;
13973 /* Consume the `,' token. */
13974 cp_lexer_consume_token (parser->lexer);
13975 }
13976
13977 return clobbers;
13978}
13979
13980/* Parse an (optional) series of attributes.
13981
13982 attributes:
13983 attributes attribute
13984
13985 attribute:
21526606 13986 __attribute__ (( attribute-list [opt] ))
a723baf1
MM
13987
13988 The return value is as for cp_parser_attribute_list. */
21526606 13989
a723baf1 13990static tree
94edc4ab 13991cp_parser_attributes_opt (cp_parser* parser)
a723baf1
MM
13992{
13993 tree attributes = NULL_TREE;
13994
13995 while (true)
13996 {
13997 cp_token *token;
13998 tree attribute_list;
13999
14000 /* Peek at the next token. */
14001 token = cp_lexer_peek_token (parser->lexer);
14002 /* If it's not `__attribute__', then we're done. */
14003 if (token->keyword != RID_ATTRIBUTE)
14004 break;
14005
14006 /* Consume the `__attribute__' keyword. */
14007 cp_lexer_consume_token (parser->lexer);
14008 /* Look for the two `(' tokens. */
14009 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14010 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14011
14012 /* Peek at the next token. */
14013 token = cp_lexer_peek_token (parser->lexer);
14014 if (token->type != CPP_CLOSE_PAREN)
14015 /* Parse the attribute-list. */
14016 attribute_list = cp_parser_attribute_list (parser);
14017 else
14018 /* If the next token is a `)', then there is no attribute
14019 list. */
14020 attribute_list = NULL;
14021
14022 /* Look for the two `)' tokens. */
14023 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14024 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14025
14026 /* Add these new attributes to the list. */
14027 attributes = chainon (attributes, attribute_list);
14028 }
14029
14030 return attributes;
14031}
14032
21526606 14033/* Parse an attribute-list.
a723baf1 14034
21526606
EC
14035 attribute-list:
14036 attribute
a723baf1
MM
14037 attribute-list , attribute
14038
14039 attribute:
21526606 14040 identifier
a723baf1
MM
14041 identifier ( identifier )
14042 identifier ( identifier , expression-list )
21526606 14043 identifier ( expression-list )
a723baf1
MM
14044
14045 Returns a TREE_LIST. Each node corresponds to an attribute. THe
14046 TREE_PURPOSE of each node is the identifier indicating which
14047 attribute is in use. The TREE_VALUE represents the arguments, if
14048 any. */
14049
14050static tree
94edc4ab 14051cp_parser_attribute_list (cp_parser* parser)
a723baf1
MM
14052{
14053 tree attribute_list = NULL_TREE;
c162c75e 14054 bool save_translate_strings_p = parser->translate_strings_p;
a723baf1 14055
c162c75e 14056 parser->translate_strings_p = false;
a723baf1
MM
14057 while (true)
14058 {
14059 cp_token *token;
14060 tree identifier;
14061 tree attribute;
14062
14063 /* Look for the identifier. We also allow keywords here; for
14064 example `__attribute__ ((const))' is legal. */
14065 token = cp_lexer_peek_token (parser->lexer);
21526606 14066 if (token->type != CPP_NAME
a723baf1
MM
14067 && token->type != CPP_KEYWORD)
14068 return error_mark_node;
14069 /* Consume the token. */
14070 token = cp_lexer_consume_token (parser->lexer);
21526606 14071
a723baf1
MM
14072 /* Save away the identifier that indicates which attribute this is. */
14073 identifier = token->value;
14074 attribute = build_tree_list (identifier, NULL_TREE);
14075
14076 /* Peek at the next token. */
14077 token = cp_lexer_peek_token (parser->lexer);
14078 /* If it's an `(', then parse the attribute arguments. */
14079 if (token->type == CPP_OPEN_PAREN)
14080 {
14081 tree arguments;
a723baf1 14082
21526606 14083 arguments = (cp_parser_parenthesized_expression_list
93678513
MM
14084 (parser, true, /*cast_p=*/false,
14085 /*non_constant_p=*/NULL));
a723baf1
MM
14086 /* Save the identifier and arguments away. */
14087 TREE_VALUE (attribute) = arguments;
a723baf1
MM
14088 }
14089
14090 /* Add this attribute to the list. */
14091 TREE_CHAIN (attribute) = attribute_list;
14092 attribute_list = attribute;
14093
14094 /* Now, look for more attributes. */
14095 token = cp_lexer_peek_token (parser->lexer);
14096 /* If the next token isn't a `,', we're done. */
14097 if (token->type != CPP_COMMA)
14098 break;
14099
cd0be382 14100 /* Consume the comma and keep going. */
a723baf1
MM
14101 cp_lexer_consume_token (parser->lexer);
14102 }
c162c75e 14103 parser->translate_strings_p = save_translate_strings_p;
a723baf1
MM
14104
14105 /* We built up the list in reverse order. */
14106 return nreverse (attribute_list);
14107}
14108
14109/* Parse an optional `__extension__' keyword. Returns TRUE if it is
14110 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14111 current value of the PEDANTIC flag, regardless of whether or not
14112 the `__extension__' keyword is present. The caller is responsible
14113 for restoring the value of the PEDANTIC flag. */
14114
14115static bool
94edc4ab 14116cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
a723baf1
MM
14117{
14118 /* Save the old value of the PEDANTIC flag. */
14119 *saved_pedantic = pedantic;
14120
14121 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14122 {
14123 /* Consume the `__extension__' token. */
14124 cp_lexer_consume_token (parser->lexer);
14125 /* We're not being pedantic while the `__extension__' keyword is
14126 in effect. */
14127 pedantic = 0;
14128
14129 return true;
14130 }
14131
14132 return false;
14133}
14134
14135/* Parse a label declaration.
14136
14137 label-declaration:
14138 __label__ label-declarator-seq ;
14139
14140 label-declarator-seq:
14141 identifier , label-declarator-seq
14142 identifier */
14143
14144static void
94edc4ab 14145cp_parser_label_declaration (cp_parser* parser)
a723baf1
MM
14146{
14147 /* Look for the `__label__' keyword. */
14148 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14149
14150 while (true)
14151 {
14152 tree identifier;
14153
14154 /* Look for an identifier. */
14155 identifier = cp_parser_identifier (parser);
14156 /* Declare it as a lobel. */
14157 finish_label_decl (identifier);
14158 /* If the next token is a `;', stop. */
14159 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14160 break;
14161 /* Look for the `,' separating the label declarations. */
14162 cp_parser_require (parser, CPP_COMMA, "`,'");
14163 }
14164
14165 /* Look for the final `;'. */
14166 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14167}
14168
14169/* Support Functions */
14170
14171/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14172 NAME should have one of the representations used for an
14173 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14174 is returned. If PARSER->SCOPE is a dependent type, then a
14175 SCOPE_REF is returned.
14176
14177 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14178 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14179 was formed. Abstractly, such entities should not be passed to this
14180 function, because they do not need to be looked up, but it is
14181 simpler to check for this special case here, rather than at the
14182 call-sites.
14183
14184 In cases not explicitly covered above, this function returns a
14185 DECL, OVERLOAD, or baselink representing the result of the lookup.
14186 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14187 is returned.
14188
472c29c3 14189 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
fc6a28d7
MM
14190 (e.g., "struct") that was used. In that case bindings that do not
14191 refer to types are ignored.
a723baf1 14192
b0bc6e8e
KL
14193 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14194 ignored.
14195
eea9800f
MM
14196 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14197 are ignored.
14198
a723baf1 14199 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
8f78f01f
MM
14200 types.
14201
14202 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14203 results in an ambiguity, and false otherwise. */
a723baf1
MM
14204
14205static tree
21526606 14206cp_parser_lookup_name (cp_parser *parser, tree name,
fc6a28d7
MM
14207 enum tag_types tag_type,
14208 bool is_template, bool is_namespace,
8f78f01f
MM
14209 bool check_dependency,
14210 bool *ambiguous_p)
a723baf1
MM
14211{
14212 tree decl;
14213 tree object_type = parser->context->object_type;
14214
8f78f01f
MM
14215 /* Assume that the lookup will be unambiguous. */
14216 if (ambiguous_p)
14217 *ambiguous_p = false;
14218
a723baf1
MM
14219 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14220 no longer valid. Note that if we are parsing tentatively, and
14221 the parse fails, OBJECT_TYPE will be automatically restored. */
14222 parser->context->object_type = NULL_TREE;
14223
14224 if (name == error_mark_node)
14225 return error_mark_node;
14226
14227 /* A template-id has already been resolved; there is no lookup to
14228 do. */
14229 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14230 return name;
14231 if (BASELINK_P (name))
14232 {
50bc768d
NS
14233 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14234 == TEMPLATE_ID_EXPR);
a723baf1
MM
14235 return name;
14236 }
14237
14238 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14239 it should already have been checked to make sure that the name
14240 used matches the type being destroyed. */
14241 if (TREE_CODE (name) == BIT_NOT_EXPR)
14242 {
14243 tree type;
14244
14245 /* Figure out to which type this destructor applies. */
14246 if (parser->scope)
14247 type = parser->scope;
14248 else if (object_type)
14249 type = object_type;
14250 else
14251 type = current_class_type;
14252 /* If that's not a class type, there is no destructor. */
14253 if (!type || !CLASS_TYPE_P (type))
14254 return error_mark_node;
fd6e3cce
GB
14255 if (!CLASSTYPE_DESTRUCTORS (type))
14256 return error_mark_node;
a723baf1
MM
14257 /* If it was a class type, return the destructor. */
14258 return CLASSTYPE_DESTRUCTORS (type);
14259 }
14260
14261 /* By this point, the NAME should be an ordinary identifier. If
14262 the id-expression was a qualified name, the qualifying scope is
14263 stored in PARSER->SCOPE at this point. */
50bc768d 14264 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
21526606 14265
a723baf1
MM
14266 /* Perform the lookup. */
14267 if (parser->scope)
21526606 14268 {
1fb3244a 14269 bool dependent_p;
a723baf1
MM
14270
14271 if (parser->scope == error_mark_node)
14272 return error_mark_node;
14273
14274 /* If the SCOPE is dependent, the lookup must be deferred until
14275 the template is instantiated -- unless we are explicitly
14276 looking up names in uninstantiated templates. Even then, we
14277 cannot look up the name if the scope is not a class type; it
14278 might, for example, be a template type parameter. */
1fb3244a
MM
14279 dependent_p = (TYPE_P (parser->scope)
14280 && !(parser->in_declarator_p
14281 && currently_open_class (parser->scope))
14282 && dependent_type_p (parser->scope));
a723baf1 14283 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
1fb3244a 14284 && dependent_p)
a723baf1 14285 {
fc6a28d7
MM
14286 if (tag_type)
14287 {
14288 tree type;
14289
14290 /* The resolution to Core Issue 180 says that `struct
14291 A::B' should be considered a type-name, even if `A'
14292 is dependent. */
14293 type = make_typename_type (parser->scope, name, tag_type,
14294 /*complain=*/1);
fc6a28d7
MM
14295 decl = TYPE_NAME (type);
14296 }
b0bc6e8e 14297 else if (is_template)
5b4acce1 14298 decl = make_unbound_class_template (parser->scope,
b939a023 14299 name, NULL_TREE,
5b4acce1 14300 /*complain=*/1);
b0bc6e8e
KL
14301 else
14302 decl = build_nt (SCOPE_REF, parser->scope, name);
a723baf1
MM
14303 }
14304 else
14305 {
4514aa8c 14306 tree pushed_scope = NULL_TREE;
91b004e5 14307
a723baf1
MM
14308 /* If PARSER->SCOPE is a dependent type, then it must be a
14309 class type, and we must not be checking dependencies;
14310 otherwise, we would have processed this lookup above. So
14311 that PARSER->SCOPE is not considered a dependent base by
14312 lookup_member, we must enter the scope here. */
1fb3244a 14313 if (dependent_p)
4514aa8c 14314 pushed_scope = push_scope (parser->scope);
a723baf1
MM
14315 /* If the PARSER->SCOPE is a a template specialization, it
14316 may be instantiated during name lookup. In that case,
14317 errors may be issued. Even if we rollback the current
14318 tentative parse, those errors are valid. */
fc6a28d7
MM
14319 decl = lookup_qualified_name (parser->scope, name,
14320 tag_type != none_type,
5e08432e 14321 /*complain=*/true);
4514aa8c
NS
14322 if (pushed_scope)
14323 pop_scope (pushed_scope);
a723baf1
MM
14324 }
14325 parser->qualifying_scope = parser->scope;
14326 parser->object_scope = NULL_TREE;
14327 }
14328 else if (object_type)
14329 {
14330 tree object_decl = NULL_TREE;
14331 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14332 OBJECT_TYPE is not a class. */
14333 if (CLASS_TYPE_P (object_type))
14334 /* If the OBJECT_TYPE is a template specialization, it may
14335 be instantiated during name lookup. In that case, errors
14336 may be issued. Even if we rollback the current tentative
14337 parse, those errors are valid. */
14338 object_decl = lookup_member (object_type,
14339 name,
fc6a28d7
MM
14340 /*protect=*/0,
14341 tag_type != none_type);
a723baf1 14342 /* Look it up in the enclosing context, too. */
fc6a28d7
MM
14343 decl = lookup_name_real (name, tag_type != none_type,
14344 /*nonclass=*/0,
12cf89fa 14345 /*block_p=*/true, is_namespace,
a723baf1
MM
14346 /*flags=*/0);
14347 parser->object_scope = object_type;
14348 parser->qualifying_scope = NULL_TREE;
14349 if (object_decl)
14350 decl = object_decl;
14351 }
14352 else
14353 {
fc6a28d7
MM
14354 decl = lookup_name_real (name, tag_type != none_type,
14355 /*nonclass=*/0,
12cf89fa 14356 /*block_p=*/true, is_namespace,
a723baf1
MM
14357 /*flags=*/0);
14358 parser->qualifying_scope = NULL_TREE;
14359 parser->object_scope = NULL_TREE;
14360 }
14361
14362 /* If the lookup failed, let our caller know. */
21526606 14363 if (!decl
a723baf1 14364 || decl == error_mark_node
21526606 14365 || (TREE_CODE (decl) == FUNCTION_DECL
a723baf1
MM
14366 && DECL_ANTICIPATED (decl)))
14367 return error_mark_node;
14368
14369 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14370 if (TREE_CODE (decl) == TREE_LIST)
14371 {
8f78f01f
MM
14372 if (ambiguous_p)
14373 *ambiguous_p = true;
a723baf1
MM
14374 /* The error message we have to print is too complicated for
14375 cp_parser_error, so we incorporate its actions directly. */
e5976695 14376 if (!cp_parser_simulate_error (parser))
a723baf1 14377 {
2a13a625 14378 error ("reference to %qD is ambiguous", name);
a723baf1
MM
14379 print_candidates (decl);
14380 }
14381 return error_mark_node;
14382 }
14383
50bc768d
NS
14384 gcc_assert (DECL_P (decl)
14385 || TREE_CODE (decl) == OVERLOAD
14386 || TREE_CODE (decl) == SCOPE_REF
14387 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14388 || BASELINK_P (decl));
a723baf1
MM
14389
14390 /* If we have resolved the name of a member declaration, check to
14391 see if the declaration is accessible. When the name resolves to
34cd5ae7 14392 set of overloaded functions, accessibility is checked when
21526606 14393 overload resolution is done.
a723baf1
MM
14394
14395 During an explicit instantiation, access is not checked at all,
14396 as per [temp.explicit]. */
8d241e0b 14397 if (DECL_P (decl))
ee76b931 14398 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
a723baf1
MM
14399
14400 return decl;
14401}
14402
14403/* Like cp_parser_lookup_name, but for use in the typical case where
b0bc6e8e
KL
14404 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14405 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
a723baf1
MM
14406
14407static tree
94edc4ab 14408cp_parser_lookup_name_simple (cp_parser* parser, tree name)
a723baf1 14409{
21526606 14410 return cp_parser_lookup_name (parser, name,
fc6a28d7 14411 none_type,
b0bc6e8e 14412 /*is_template=*/false,
eea9800f 14413 /*is_namespace=*/false,
8f78f01f
MM
14414 /*check_dependency=*/true,
14415 /*ambiguous_p=*/NULL);
a723baf1
MM
14416}
14417
a723baf1
MM
14418/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14419 the current context, return the TYPE_DECL. If TAG_NAME_P is
14420 true, the DECL indicates the class being defined in a class-head,
14421 or declared in an elaborated-type-specifier.
14422
14423 Otherwise, return DECL. */
14424
14425static tree
14426cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14427{
710b73e6
KL
14428 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14429 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
a723baf1 14430
21526606 14431 struct A {
a723baf1
MM
14432 template <typename T> struct B;
14433 };
14434
21526606
EC
14435 template <typename T> struct A::B {};
14436
a723baf1
MM
14437 Similarly, in a elaborated-type-specifier:
14438
14439 namespace N { struct X{}; }
14440
14441 struct A {
14442 template <typename T> friend struct N::X;
14443 };
14444
710b73e6
KL
14445 However, if the DECL refers to a class type, and we are in
14446 the scope of the class, then the name lookup automatically
14447 finds the TYPE_DECL created by build_self_reference rather
14448 than a TEMPLATE_DECL. For example, in:
14449
14450 template <class T> struct S {
14451 S s;
14452 };
14453
14454 there is no need to handle such case. */
14455
14456 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
a723baf1
MM
14457 return DECL_TEMPLATE_RESULT (decl);
14458
14459 return decl;
14460}
14461
14462/* If too many, or too few, template-parameter lists apply to the
14463 declarator, issue an error message. Returns TRUE if all went well,
14464 and FALSE otherwise. */
14465
14466static bool
21526606 14467cp_parser_check_declarator_template_parameters (cp_parser* parser,
058b15c1 14468 cp_declarator *declarator)
a723baf1
MM
14469{
14470 unsigned num_templates;
14471
14472 /* We haven't seen any classes that involve template parameters yet. */
14473 num_templates = 0;
14474
058b15c1 14475 switch (declarator->kind)
a723baf1 14476 {
058b15c1 14477 case cdk_id:
1d786913 14478 if (declarator->u.id.qualifying_scope)
058b15c1
MM
14479 {
14480 tree scope;
14481 tree member;
a723baf1 14482
1d786913
MM
14483 scope = declarator->u.id.qualifying_scope;
14484 member = declarator->u.id.unqualified_name;
a723baf1 14485
058b15c1
MM
14486 while (scope && CLASS_TYPE_P (scope))
14487 {
14488 /* You're supposed to have one `template <...>'
14489 for every template class, but you don't need one
14490 for a full specialization. For example:
14491
14492 template <class T> struct S{};
14493 template <> struct S<int> { void f(); };
14494 void S<int>::f () {}
14495
14496 is correct; there shouldn't be a `template <>' for
14497 the definition of `S<int>::f'. */
14498 if (CLASSTYPE_TEMPLATE_INFO (scope)
14499 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14500 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14501 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14502 ++num_templates;
14503
14504 scope = TYPE_CONTEXT (scope);
14505 }
14506 }
1d786913
MM
14507 else if (TREE_CODE (declarator->u.id.unqualified_name)
14508 == TEMPLATE_ID_EXPR)
14509 /* If the DECLARATOR has the form `X<y>' then it uses one
14510 additional level of template parameters. */
a723baf1
MM
14511 ++num_templates;
14512
21526606 14513 return cp_parser_check_template_parameters (parser,
a723baf1 14514 num_templates);
058b15c1
MM
14515
14516 case cdk_function:
14517 case cdk_array:
14518 case cdk_pointer:
14519 case cdk_reference:
14520 case cdk_ptrmem:
98ca843c 14521 return (cp_parser_check_declarator_template_parameters
058b15c1
MM
14522 (parser, declarator->declarator));
14523
14524 case cdk_error:
14525 return true;
14526
14527 default:
315fb5db 14528 gcc_unreachable ();
a723baf1 14529 }
315fb5db 14530 return false;
a723baf1
MM
14531}
14532
14533/* NUM_TEMPLATES were used in the current declaration. If that is
14534 invalid, return FALSE and issue an error messages. Otherwise,
14535 return TRUE. */
14536
14537static bool
94edc4ab
NN
14538cp_parser_check_template_parameters (cp_parser* parser,
14539 unsigned num_templates)
a723baf1
MM
14540{
14541 /* If there are more template classes than parameter lists, we have
14542 something like:
21526606 14543
a723baf1
MM
14544 template <class T> void S<T>::R<T>::f (); */
14545 if (parser->num_template_parameter_lists < num_templates)
14546 {
14547 error ("too few template-parameter-lists");
14548 return false;
14549 }
14550 /* If there are the same number of template classes and parameter
14551 lists, that's OK. */
14552 if (parser->num_template_parameter_lists == num_templates)
14553 return true;
14554 /* If there are more, but only one more, then we are referring to a
14555 member template. That's OK too. */
14556 if (parser->num_template_parameter_lists == num_templates + 1)
14557 return true;
14558 /* Otherwise, there are too many template parameter lists. We have
14559 something like:
14560
14561 template <class T> template <class U> void S::f(); */
14562 error ("too many template-parameter-lists");
14563 return false;
14564}
14565
a723baf1
MM
14566/* Parse an optional `::' token indicating that the following name is
14567 from the global namespace. If so, PARSER->SCOPE is set to the
14568 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14569 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14570 Returns the new value of PARSER->SCOPE, if the `::' token is
14571 present, and NULL_TREE otherwise. */
14572
14573static tree
94edc4ab 14574cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
a723baf1
MM
14575{
14576 cp_token *token;
14577
14578 /* Peek at the next token. */
14579 token = cp_lexer_peek_token (parser->lexer);
14580 /* If we're looking at a `::' token then we're starting from the
14581 global namespace, not our current location. */
14582 if (token->type == CPP_SCOPE)
14583 {
14584 /* Consume the `::' token. */
14585 cp_lexer_consume_token (parser->lexer);
14586 /* Set the SCOPE so that we know where to start the lookup. */
14587 parser->scope = global_namespace;
14588 parser->qualifying_scope = global_namespace;
14589 parser->object_scope = NULL_TREE;
14590
14591 return parser->scope;
14592 }
14593 else if (!current_scope_valid_p)
14594 {
14595 parser->scope = NULL_TREE;
14596 parser->qualifying_scope = NULL_TREE;
14597 parser->object_scope = NULL_TREE;
14598 }
14599
14600 return NULL_TREE;
14601}
14602
14603/* Returns TRUE if the upcoming token sequence is the start of a
14604 constructor declarator. If FRIEND_P is true, the declarator is
14605 preceded by the `friend' specifier. */
14606
14607static bool
14608cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14609{
14610 bool constructor_p;
14611 tree type_decl = NULL_TREE;
14612 bool nested_name_p;
2050a1bb
MM
14613 cp_token *next_token;
14614
14615 /* The common case is that this is not a constructor declarator, so
8fbc5ae7
MM
14616 try to avoid doing lots of work if at all possible. It's not
14617 valid declare a constructor at function scope. */
14618 if (at_function_scope_p ())
14619 return false;
14620 /* And only certain tokens can begin a constructor declarator. */
2050a1bb
MM
14621 next_token = cp_lexer_peek_token (parser->lexer);
14622 if (next_token->type != CPP_NAME
14623 && next_token->type != CPP_SCOPE
14624 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14625 && next_token->type != CPP_TEMPLATE_ID)
14626 return false;
a723baf1
MM
14627
14628 /* Parse tentatively; we are going to roll back all of the tokens
14629 consumed here. */
14630 cp_parser_parse_tentatively (parser);
14631 /* Assume that we are looking at a constructor declarator. */
14632 constructor_p = true;
8d241e0b 14633
a723baf1
MM
14634 /* Look for the optional `::' operator. */
14635 cp_parser_global_scope_opt (parser,
14636 /*current_scope_valid_p=*/false);
14637 /* Look for the nested-name-specifier. */
21526606 14638 nested_name_p
a723baf1
MM
14639 = (cp_parser_nested_name_specifier_opt (parser,
14640 /*typename_keyword_p=*/false,
14641 /*check_dependency_p=*/false,
a668c6ad
MM
14642 /*type_p=*/false,
14643 /*is_declaration=*/false)
a723baf1
MM
14644 != NULL_TREE);
14645 /* Outside of a class-specifier, there must be a
14646 nested-name-specifier. */
21526606 14647 if (!nested_name_p &&
a723baf1
MM
14648 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14649 || friend_p))
14650 constructor_p = false;
14651 /* If we still think that this might be a constructor-declarator,
14652 look for a class-name. */
14653 if (constructor_p)
14654 {
14655 /* If we have:
14656
8fbc5ae7 14657 template <typename T> struct S { S(); };
a723baf1
MM
14658 template <typename T> S<T>::S ();
14659
14660 we must recognize that the nested `S' names a class.
14661 Similarly, for:
14662
14663 template <typename T> S<T>::S<T> ();
14664
14665 we must recognize that the nested `S' names a template. */
14666 type_decl = cp_parser_class_name (parser,
14667 /*typename_keyword_p=*/false,
14668 /*template_keyword_p=*/false,
fc6a28d7 14669 none_type,
a723baf1 14670 /*check_dependency_p=*/false,
a668c6ad
MM
14671 /*class_head_p=*/false,
14672 /*is_declaration=*/false);
a723baf1
MM
14673 /* If there was no class-name, then this is not a constructor. */
14674 constructor_p = !cp_parser_error_occurred (parser);
14675 }
8d241e0b 14676
a723baf1
MM
14677 /* If we're still considering a constructor, we have to see a `(',
14678 to begin the parameter-declaration-clause, followed by either a
14679 `)', an `...', or a decl-specifier. We need to check for a
14680 type-specifier to avoid being fooled into thinking that:
14681
14682 S::S (f) (int);
14683
14684 is a constructor. (It is actually a function named `f' that
14685 takes one parameter (of type `int') and returns a value of type
14686 `S::S'. */
21526606 14687 if (constructor_p
a723baf1
MM
14688 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14689 {
14690 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14691 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15077df5
MM
14692 /* A parameter declaration begins with a decl-specifier,
14693 which is either the "attribute" keyword, a storage class
14694 specifier, or (usually) a type-specifier. */
14695 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
a723baf1
MM
14696 && !cp_parser_storage_class_specifier_opt (parser))
14697 {
5dae1114 14698 tree type;
4514aa8c 14699 tree pushed_scope = NULL_TREE;
4047b164 14700 unsigned saved_num_template_parameter_lists;
5dae1114
MM
14701
14702 /* Names appearing in the type-specifier should be looked up
14703 in the scope of the class. */
14704 if (current_class_type)
14705 type = NULL_TREE;
a723baf1
MM
14706 else
14707 {
5dae1114
MM
14708 type = TREE_TYPE (type_decl);
14709 if (TREE_CODE (type) == TYPENAME_TYPE)
14d22dd6 14710 {
21526606 14711 type = resolve_typename_type (type,
14d22dd6
MM
14712 /*only_current_p=*/false);
14713 if (type == error_mark_node)
14714 {
14715 cp_parser_abort_tentative_parse (parser);
14716 return false;
14717 }
14718 }
4514aa8c 14719 pushed_scope = push_scope (type);
a723baf1 14720 }
4047b164
KL
14721
14722 /* Inside the constructor parameter list, surrounding
14723 template-parameter-lists do not apply. */
14724 saved_num_template_parameter_lists
14725 = parser->num_template_parameter_lists;
14726 parser->num_template_parameter_lists = 0;
14727
5dae1114
MM
14728 /* Look for the type-specifier. */
14729 cp_parser_type_specifier (parser,
14730 CP_PARSER_FLAGS_NONE,
62d1db17 14731 /*decl_specs=*/NULL,
5dae1114
MM
14732 /*is_declarator=*/true,
14733 /*declares_class_or_enum=*/NULL,
14734 /*is_cv_qualifier=*/NULL);
4047b164
KL
14735
14736 parser->num_template_parameter_lists
14737 = saved_num_template_parameter_lists;
14738
5dae1114 14739 /* Leave the scope of the class. */
4514aa8c
NS
14740 if (pushed_scope)
14741 pop_scope (pushed_scope);
5dae1114
MM
14742
14743 constructor_p = !cp_parser_error_occurred (parser);
a723baf1
MM
14744 }
14745 }
14746 else
14747 constructor_p = false;
14748 /* We did not really want to consume any tokens. */
14749 cp_parser_abort_tentative_parse (parser);
14750
14751 return constructor_p;
14752}
14753
14754/* Parse the definition of the function given by the DECL_SPECIFIERS,
cf22909c 14755 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
a723baf1
MM
14756 they must be performed once we are in the scope of the function.
14757
14758 Returns the function defined. */
14759
14760static tree
14761cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab 14762 (cp_parser* parser,
62d1db17 14763 cp_decl_specifier_seq *decl_specifiers,
94edc4ab 14764 tree attributes,
058b15c1 14765 const cp_declarator *declarator)
a723baf1
MM
14766{
14767 tree fn;
14768 bool success_p;
14769
14770 /* Begin the function-definition. */
058b15c1
MM
14771 success_p = start_function (decl_specifiers, declarator, attributes);
14772
14773 /* The things we're about to see are not directly qualified by any
14774 template headers we've seen thus far. */
14775 reset_specialization ();
a723baf1
MM
14776
14777 /* If there were names looked up in the decl-specifier-seq that we
14778 did not check, check them now. We must wait until we are in the
14779 scope of the function to perform the checks, since the function
14780 might be a friend. */
cf22909c 14781 perform_deferred_access_checks ();
a723baf1
MM
14782
14783 if (!success_p)
14784 {
058b15c1 14785 /* Skip the entire function. */
a723baf1
MM
14786 error ("invalid function declaration");
14787 cp_parser_skip_to_end_of_block_or_statement (parser);
14788 fn = error_mark_node;
14789 }
14790 else
14791 fn = cp_parser_function_definition_after_declarator (parser,
14792 /*inline_p=*/false);
14793
14794 return fn;
14795}
14796
14797/* Parse the part of a function-definition that follows the
14798 declarator. INLINE_P is TRUE iff this function is an inline
14799 function defined with a class-specifier.
14800
14801 Returns the function defined. */
14802
21526606
EC
14803static tree
14804cp_parser_function_definition_after_declarator (cp_parser* parser,
94edc4ab 14805 bool inline_p)
a723baf1
MM
14806{
14807 tree fn;
14808 bool ctor_initializer_p = false;
14809 bool saved_in_unbraced_linkage_specification_p;
14810 unsigned saved_num_template_parameter_lists;
14811
14812 /* If the next token is `return', then the code may be trying to
14813 make use of the "named return value" extension that G++ used to
14814 support. */
14815 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14816 {
14817 /* Consume the `return' keyword. */
14818 cp_lexer_consume_token (parser->lexer);
14819 /* Look for the identifier that indicates what value is to be
14820 returned. */
14821 cp_parser_identifier (parser);
14822 /* Issue an error message. */
14823 error ("named return values are no longer supported");
14824 /* Skip tokens until we reach the start of the function body. */
21eb631b
MM
14825 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14826 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
a723baf1
MM
14827 cp_lexer_consume_token (parser->lexer);
14828 }
14829 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14830 anything declared inside `f'. */
21526606 14831 saved_in_unbraced_linkage_specification_p
a723baf1
MM
14832 = parser->in_unbraced_linkage_specification_p;
14833 parser->in_unbraced_linkage_specification_p = false;
14834 /* Inside the function, surrounding template-parameter-lists do not
14835 apply. */
21526606
EC
14836 saved_num_template_parameter_lists
14837 = parser->num_template_parameter_lists;
a723baf1
MM
14838 parser->num_template_parameter_lists = 0;
14839 /* If the next token is `try', then we are looking at a
14840 function-try-block. */
14841 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14842 ctor_initializer_p = cp_parser_function_try_block (parser);
14843 /* A function-try-block includes the function-body, so we only do
14844 this next part if we're not processing a function-try-block. */
14845 else
21526606 14846 ctor_initializer_p
a723baf1
MM
14847 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14848
14849 /* Finish the function. */
21526606 14850 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
a723baf1
MM
14851 (inline_p ? 2 : 0));
14852 /* Generate code for it, if necessary. */
8cd2462c 14853 expand_or_defer_fn (fn);
a723baf1 14854 /* Restore the saved values. */
21526606 14855 parser->in_unbraced_linkage_specification_p
a723baf1 14856 = saved_in_unbraced_linkage_specification_p;
21526606 14857 parser->num_template_parameter_lists
a723baf1
MM
14858 = saved_num_template_parameter_lists;
14859
14860 return fn;
14861}
14862
14863/* Parse a template-declaration, assuming that the `export' (and
14864 `extern') keywords, if present, has already been scanned. MEMBER_P
14865 is as for cp_parser_template_declaration. */
14866
14867static void
94edc4ab 14868cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
a723baf1
MM
14869{
14870 tree decl = NULL_TREE;
14871 tree parameter_list;
14872 bool friend_p = false;
14873
14874 /* Look for the `template' keyword. */
14875 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14876 return;
21526606 14877
a723baf1
MM
14878 /* And the `<'. */
14879 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14880 return;
21526606 14881
a723baf1
MM
14882 /* If the next token is `>', then we have an invalid
14883 specialization. Rather than complain about an invalid template
14884 parameter, issue an error message here. */
14885 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14886 {
14887 cp_parser_error (parser, "invalid explicit specialization");
2f9afd51 14888 begin_specialization ();
a723baf1
MM
14889 parameter_list = NULL_TREE;
14890 }
14891 else
2f9afd51
KL
14892 {
14893 /* Parse the template parameters. */
14894 begin_template_parm_list ();
14895 parameter_list = cp_parser_template_parameter_list (parser);
14896 parameter_list = end_template_parm_list (parameter_list);
14897 }
14898
a723baf1
MM
14899 /* Look for the `>'. */
14900 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14901 /* We just processed one more parameter list. */
14902 ++parser->num_template_parameter_lists;
14903 /* If the next token is `template', there are more template
14904 parameters. */
21526606 14905 if (cp_lexer_next_token_is_keyword (parser->lexer,
a723baf1
MM
14906 RID_TEMPLATE))
14907 cp_parser_template_declaration_after_export (parser, member_p);
14908 else
14909 {
fe88415f
NS
14910 /* There are no access checks when parsing a template, as we do not
14911 know if a specialization will be a friend. */
14912 push_deferring_access_checks (dk_no_check);
98ca843c 14913
a723baf1
MM
14914 decl = cp_parser_single_declaration (parser,
14915 member_p,
14916 &friend_p);
14917
fe88415f 14918 pop_deferring_access_checks ();
98ca843c 14919
a723baf1
MM
14920 /* If this is a member template declaration, let the front
14921 end know. */
14922 if (member_p && !friend_p && decl)
37d407a1
KL
14923 {
14924 if (TREE_CODE (decl) == TYPE_DECL)
14925 cp_parser_check_access_in_redeclaration (decl);
14926
14927 decl = finish_member_template_decl (decl);
14928 }
a723baf1 14929 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19db77ce
KL
14930 make_friend_class (current_class_type, TREE_TYPE (decl),
14931 /*complain=*/true);
a723baf1
MM
14932 }
14933 /* We are done with the current parameter list. */
14934 --parser->num_template_parameter_lists;
14935
14936 /* Finish up. */
14937 finish_template_decl (parameter_list);
14938
14939 /* Register member declarations. */
14940 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14941 finish_member_declaration (decl);
14942
14943 /* If DECL is a function template, we must return to parse it later.
14944 (Even though there is no definition, there might be default
14945 arguments that need handling.) */
21526606 14946 if (member_p && decl
a723baf1
MM
14947 && (TREE_CODE (decl) == FUNCTION_DECL
14948 || DECL_FUNCTION_TEMPLATE_P (decl)))
14949 TREE_VALUE (parser->unparsed_functions_queues)
21526606 14950 = tree_cons (NULL_TREE, decl,
a723baf1
MM
14951 TREE_VALUE (parser->unparsed_functions_queues));
14952}
14953
14954/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14955 `function-definition' sequence. MEMBER_P is true, this declaration
14956 appears in a class scope.
14957
14958 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14959 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14960
14961static tree
21526606 14962cp_parser_single_declaration (cp_parser* parser,
94edc4ab
NN
14963 bool member_p,
14964 bool* friend_p)
a723baf1 14965{
560ad596 14966 int declares_class_or_enum;
a723baf1 14967 tree decl = NULL_TREE;
62d1db17 14968 cp_decl_specifier_seq decl_specifiers;
4bb8ca28 14969 bool function_definition_p = false;
a723baf1 14970
71bd7186
MM
14971 /* This function is only used when processing a template
14972 declaration. */
14973 gcc_assert (innermost_scope_kind () == sk_template_parms
14974 || innermost_scope_kind () == sk_template_spec);
14975
a723baf1 14976 /* Defer access checks until we know what is being declared. */
8d241e0b 14977 push_deferring_access_checks (dk_deferred);
cf22909c 14978
a723baf1
MM
14979 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14980 alternative. */
62d1db17
MM
14981 cp_parser_decl_specifier_seq (parser,
14982 CP_PARSER_FLAGS_OPTIONAL,
14983 &decl_specifiers,
14984 &declares_class_or_enum);
4bb8ca28 14985 if (friend_p)
62d1db17 14986 *friend_p = cp_parser_friend_p (&decl_specifiers);
71bd7186
MM
14987
14988 /* There are no template typedefs. */
14989 if (decl_specifiers.specs[(int) ds_typedef])
14990 {
14991 error ("template declaration of %qs", "typedef");
14992 decl = error_mark_node;
14993 }
14994
a723baf1
MM
14995 /* Gather up the access checks that occurred the
14996 decl-specifier-seq. */
cf22909c
KL
14997 stop_deferring_access_checks ();
14998
a723baf1
MM
14999 /* Check for the declaration of a template class. */
15000 if (declares_class_or_enum)
15001 {
15002 if (cp_parser_declares_only_class_p (parser))
15003 {
62d1db17 15004 decl = shadow_tag (&decl_specifiers);
b939a023
KL
15005
15006 /* In this case:
15007
15008 struct C {
15009 friend template <typename T> struct A<T>::B;
15010 };
15011
15012 A<T>::B will be represented by a TYPENAME_TYPE, and
15013 therefore not recognized by shadow_tag. */
15014 if (friend_p && *friend_p
15015 && !decl
15016 && decl_specifiers.type
15017 && TYPE_P (decl_specifiers.type))
15018 decl = decl_specifiers.type;
15019
62d1db17 15020 if (decl && decl != error_mark_node)
a723baf1
MM
15021 decl = TYPE_NAME (decl);
15022 else
15023 decl = error_mark_node;
15024 }
15025 }
a723baf1
MM
15026 /* If it's not a template class, try for a template function. If
15027 the next token is a `;', then this declaration does not declare
15028 anything. But, if there were errors in the decl-specifiers, then
15029 the error might well have come from an attempted class-specifier.
15030 In that case, there's no need to warn about a missing declarator. */
15031 if (!decl
15032 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
62d1db17 15033 || decl_specifiers.type != error_mark_node))
21526606 15034 decl = cp_parser_init_declarator (parser,
62d1db17 15035 &decl_specifiers,
4bb8ca28 15036 /*function_definition_allowed_p=*/true,
a723baf1 15037 member_p,
560ad596 15038 declares_class_or_enum,
4bb8ca28 15039 &function_definition_p);
cf22909c
KL
15040
15041 pop_deferring_access_checks ();
15042
a723baf1
MM
15043 /* Clear any current qualification; whatever comes next is the start
15044 of something new. */
15045 parser->scope = NULL_TREE;
15046 parser->qualifying_scope = NULL_TREE;
15047 parser->object_scope = NULL_TREE;
15048 /* Look for a trailing `;' after the declaration. */
4bb8ca28 15049 if (!function_definition_p
71bd7186
MM
15050 && (decl == error_mark_node
15051 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
a723baf1 15052 cp_parser_skip_to_end_of_block_or_statement (parser);
a723baf1
MM
15053
15054 return decl;
15055}
15056
d6b4ea85
MM
15057/* Parse a cast-expression that is not the operand of a unary "&". */
15058
15059static tree
15060cp_parser_simple_cast_expression (cp_parser *parser)
15061{
93678513
MM
15062 return cp_parser_cast_expression (parser, /*address_p=*/false,
15063 /*cast_p=*/false);
d6b4ea85
MM
15064}
15065
a723baf1
MM
15066/* Parse a functional cast to TYPE. Returns an expression
15067 representing the cast. */
15068
15069static tree
94edc4ab 15070cp_parser_functional_cast (cp_parser* parser, tree type)
a723baf1
MM
15071{
15072 tree expression_list;
d36d5600 15073 tree cast;
a723baf1 15074
21526606 15075 expression_list
39703eb9 15076 = cp_parser_parenthesized_expression_list (parser, false,
93678513 15077 /*cast_p=*/true,
39703eb9 15078 /*non_constant_p=*/NULL);
a723baf1 15079
d36d5600
GB
15080 cast = build_functional_cast (type, expression_list);
15081 /* [expr.const]/1: In an integral constant expression "only type
15082 conversions to integral or enumeration type can be used". */
98ca843c 15083 if (cast != error_mark_node && !type_dependent_expression_p (type)
d36d5600
GB
15084 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15085 {
98ca843c 15086 if (cp_parser_non_integral_constant_expression
d36d5600
GB
15087 (parser, "a call to a constructor"))
15088 return error_mark_node;
15089 }
15090 return cast;
a723baf1
MM
15091}
15092
4bb8ca28
MM
15093/* Save the tokens that make up the body of a member function defined
15094 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15095 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15096 specifiers applied to the declaration. Returns the FUNCTION_DECL
15097 for the member function. */
15098
7ce27103 15099static tree
4bb8ca28 15100cp_parser_save_member_function_body (cp_parser* parser,
62d1db17 15101 cp_decl_specifier_seq *decl_specifiers,
058b15c1 15102 cp_declarator *declarator,
4bb8ca28
MM
15103 tree attributes)
15104{
c162c75e
MA
15105 cp_token *first;
15106 cp_token *last;
4bb8ca28
MM
15107 tree fn;
15108
15109 /* Create the function-declaration. */
15110 fn = start_method (decl_specifiers, declarator, attributes);
15111 /* If something went badly wrong, bail out now. */
15112 if (fn == error_mark_node)
15113 {
15114 /* If there's a function-body, skip it. */
21526606 15115 if (cp_parser_token_starts_function_definition_p
4bb8ca28
MM
15116 (cp_lexer_peek_token (parser->lexer)))
15117 cp_parser_skip_to_end_of_block_or_statement (parser);
15118 return error_mark_node;
15119 }
15120
15121 /* Remember it, if there default args to post process. */
15122 cp_parser_save_default_args (parser, fn);
15123
21526606 15124 /* Save away the tokens that make up the body of the
4bb8ca28 15125 function. */
c162c75e
MA
15126 first = parser->lexer->next_token;
15127 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
4bb8ca28
MM
15128 /* Handle function try blocks. */
15129 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
c162c75e
MA
15130 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15131 last = parser->lexer->next_token;
4bb8ca28
MM
15132
15133 /* Save away the inline definition; we will process it when the
15134 class is complete. */
c162c75e 15135 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
4bb8ca28
MM
15136 DECL_PENDING_INLINE_P (fn) = 1;
15137
15138 /* We need to know that this was defined in the class, so that
15139 friend templates are handled correctly. */
15140 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15141
15142 /* We're done with the inline definition. */
15143 finish_method (fn);
15144
15145 /* Add FN to the queue of functions to be parsed later. */
15146 TREE_VALUE (parser->unparsed_functions_queues)
21526606 15147 = tree_cons (NULL_TREE, fn,
4bb8ca28
MM
15148 TREE_VALUE (parser->unparsed_functions_queues));
15149
15150 return fn;
15151}
15152
ec75414f
MM
15153/* Parse a template-argument-list, as well as the trailing ">" (but
15154 not the opening ">"). See cp_parser_template_argument_list for the
15155 return value. */
15156
15157static tree
15158cp_parser_enclosed_template_argument_list (cp_parser* parser)
15159{
15160 tree arguments;
15161 tree saved_scope;
15162 tree saved_qualifying_scope;
15163 tree saved_object_scope;
15164 bool saved_greater_than_is_operator_p;
15165
15166 /* [temp.names]
15167
15168 When parsing a template-id, the first non-nested `>' is taken as
15169 the end of the template-argument-list rather than a greater-than
15170 operator. */
21526606 15171 saved_greater_than_is_operator_p
ec75414f
MM
15172 = parser->greater_than_is_operator_p;
15173 parser->greater_than_is_operator_p = false;
15174 /* Parsing the argument list may modify SCOPE, so we save it
15175 here. */
15176 saved_scope = parser->scope;
15177 saved_qualifying_scope = parser->qualifying_scope;
15178 saved_object_scope = parser->object_scope;
15179 /* Parse the template-argument-list itself. */
15180 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15181 arguments = NULL_TREE;
15182 else
15183 arguments = cp_parser_template_argument_list (parser);
4d5297fa
GB
15184 /* Look for the `>' that ends the template-argument-list. If we find
15185 a '>>' instead, it's probably just a typo. */
15186 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15187 {
15188 if (!saved_greater_than_is_operator_p)
15189 {
2cfe82fe
ZW
15190 /* If we're in a nested template argument list, the '>>' has
15191 to be a typo for '> >'. We emit the error message, but we
15192 continue parsing and we push a '>' as next token, so that
15193 the argument list will be parsed correctly. Note that the
15194 global source location is still on the token before the
15195 '>>', so we need to say explicitly where we want it. */
15196 cp_token *token = cp_lexer_peek_token (parser->lexer);
15197 error ("%H%<>>%> should be %<> >%> "
15198 "within a nested template argument list",
15199 &token->location);
15200
15201 /* ??? Proper recovery should terminate two levels of
15202 template argument list here. */
4d5297fa
GB
15203 token->type = CPP_GREATER;
15204 }
15205 else
15206 {
2cfe82fe
ZW
15207 /* If this is not a nested template argument list, the '>>'
15208 is a typo for '>'. Emit an error message and continue.
15209 Same deal about the token location, but here we can get it
15210 right by consuming the '>>' before issuing the diagnostic. */
4d5297fa 15211 cp_lexer_consume_token (parser->lexer);
2cfe82fe
ZW
15212 error ("spurious %<>>%>, use %<>%> to terminate "
15213 "a template argument list");
4d5297fa
GB
15214 }
15215 }
2cfe82fe
ZW
15216 else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15217 error ("missing %<>%> to terminate the template argument list");
15218 else
15219 /* It's what we want, a '>'; consume it. */
15220 cp_lexer_consume_token (parser->lexer);
ec75414f 15221 /* The `>' token might be a greater-than operator again now. */
21526606 15222 parser->greater_than_is_operator_p
ec75414f
MM
15223 = saved_greater_than_is_operator_p;
15224 /* Restore the SAVED_SCOPE. */
15225 parser->scope = saved_scope;
15226 parser->qualifying_scope = saved_qualifying_scope;
15227 parser->object_scope = saved_object_scope;
15228
15229 return arguments;
15230}
15231
a723baf1
MM
15232/* MEMBER_FUNCTION is a member function, or a friend. If default
15233 arguments, or the body of the function have not yet been parsed,
15234 parse them now. */
15235
15236static void
94edc4ab 15237cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
a723baf1 15238{
a723baf1
MM
15239 /* If this member is a template, get the underlying
15240 FUNCTION_DECL. */
15241 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15242 member_function = DECL_TEMPLATE_RESULT (member_function);
15243
15244 /* There should not be any class definitions in progress at this
15245 point; the bodies of members are only parsed outside of all class
15246 definitions. */
50bc768d 15247 gcc_assert (parser->num_classes_being_defined == 0);
a723baf1
MM
15248 /* While we're parsing the member functions we might encounter more
15249 classes. We want to handle them right away, but we don't want
15250 them getting mixed up with functions that are currently in the
15251 queue. */
15252 parser->unparsed_functions_queues
15253 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15254
15255 /* Make sure that any template parameters are in scope. */
15256 maybe_begin_member_template_processing (member_function);
15257
a723baf1
MM
15258 /* If the body of the function has not yet been parsed, parse it
15259 now. */
15260 if (DECL_PENDING_INLINE_P (member_function))
15261 {
15262 tree function_scope;
15263 cp_token_cache *tokens;
15264
15265 /* The function is no longer pending; we are processing it. */
15266 tokens = DECL_PENDING_INLINE_INFO (member_function);
15267 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15268 DECL_PENDING_INLINE_P (member_function) = 0;
15269 /* If this was an inline function in a local class, enter the scope
15270 of the containing function. */
15271 function_scope = decl_function_context (member_function);
15272 if (function_scope)
15273 push_function_context_to (function_scope);
21526606 15274
2cfe82fe
ZW
15275 /* Push the body of the function onto the lexer stack. */
15276 cp_parser_push_lexer_for_tokens (parser, tokens);
21526606 15277
a723baf1
MM
15278 /* Let the front end know that we going to be defining this
15279 function. */
058b15c1
MM
15280 start_preparsed_function (member_function, NULL_TREE,
15281 SF_PRE_PARSED | SF_INCLASS_INLINE);
21526606 15282
a723baf1
MM
15283 /* Now, parse the body of the function. */
15284 cp_parser_function_definition_after_declarator (parser,
15285 /*inline_p=*/true);
21526606 15286
a723baf1
MM
15287 /* Leave the scope of the containing function. */
15288 if (function_scope)
15289 pop_function_context_from (function_scope);
2cfe82fe 15290 cp_parser_pop_lexer (parser);
a723baf1
MM
15291 }
15292
15293 /* Remove any template parameters from the symbol table. */
15294 maybe_end_member_template_processing ();
15295
15296 /* Restore the queue. */
21526606 15297 parser->unparsed_functions_queues
a723baf1
MM
15298 = TREE_CHAIN (parser->unparsed_functions_queues);
15299}
15300
cd0be382 15301/* If DECL contains any default args, remember it on the unparsed
8db1028e
NS
15302 functions queue. */
15303
15304static void
15305cp_parser_save_default_args (cp_parser* parser, tree decl)
15306{
15307 tree probe;
15308
15309 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15310 probe;
15311 probe = TREE_CHAIN (probe))
15312 if (TREE_PURPOSE (probe))
15313 {
15314 TREE_PURPOSE (parser->unparsed_functions_queues)
f44b0c8e 15315 = tree_cons (current_class_type, decl,
8db1028e
NS
15316 TREE_PURPOSE (parser->unparsed_functions_queues));
15317 break;
15318 }
15319 return;
15320}
15321
8218bd34 15322/* FN is a FUNCTION_DECL which may contains a parameter with an
f44b0c8e
MM
15323 unparsed DEFAULT_ARG. Parse the default args now. This function
15324 assumes that the current scope is the scope in which the default
15325 argument should be processed. */
a723baf1
MM
15326
15327static void
8218bd34 15328cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
a723baf1 15329{
a723baf1 15330 bool saved_local_variables_forbidden_p;
2cfe82fe 15331 tree parm;
8218bd34 15332
b92bc2a0
NS
15333 /* While we're parsing the default args, we might (due to the
15334 statement expression extension) encounter more classes. We want
15335 to handle them right away, but we don't want them getting mixed
15336 up with default args that are currently in the queue. */
15337 parser->unparsed_functions_queues
15338 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15339
2cfe82fe
ZW
15340 /* Local variable names (and the `this' keyword) may not appear
15341 in a default argument. */
15342 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15343 parser->local_variables_forbidden_p = true;
15344
15345 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15346 parm;
15347 parm = TREE_CHAIN (parm))
a723baf1 15348 {
2cfe82fe 15349 cp_token_cache *tokens;
21526606 15350
2cfe82fe
ZW
15351 if (!TREE_PURPOSE (parm)
15352 || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15353 continue;
a723baf1 15354
2cfe82fe
ZW
15355 /* Push the saved tokens for the default argument onto the parser's
15356 lexer stack. */
15357 tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15358 cp_parser_push_lexer_for_tokens (parser, tokens);
a723baf1 15359
2cfe82fe 15360 /* Parse the assignment-expression. */
93678513
MM
15361 TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser,
15362 /*cast_p=*/false);
a723baf1 15363
676e33ca
MM
15364 /* If the token stream has not been completely used up, then
15365 there was extra junk after the end of the default
15366 argument. */
15367 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2a13a625 15368 cp_parser_error (parser, "expected %<,%>");
676e33ca 15369
2cfe82fe
ZW
15370 /* Revert to the main lexer. */
15371 cp_parser_pop_lexer (parser);
a723baf1 15372 }
b92bc2a0 15373
2cfe82fe
ZW
15374 /* Restore the state of local_variables_forbidden_p. */
15375 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15376
b92bc2a0 15377 /* Restore the queue. */
21526606 15378 parser->unparsed_functions_queues
b92bc2a0 15379 = TREE_CHAIN (parser->unparsed_functions_queues);
a723baf1
MM
15380}
15381
15382/* Parse the operand of `sizeof' (or a similar operator). Returns
15383 either a TYPE or an expression, depending on the form of the
15384 input. The KEYWORD indicates which kind of expression we have
15385 encountered. */
15386
15387static tree
94edc4ab 15388cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
a723baf1
MM
15389{
15390 static const char *format;
15391 tree expr = NULL_TREE;
15392 const char *saved_message;
67c03833 15393 bool saved_integral_constant_expression_p;
93678513 15394 bool saved_non_integral_constant_expression_p;
a723baf1
MM
15395
15396 /* Initialize FORMAT the first time we get here. */
15397 if (!format)
9e637a26 15398 format = "types may not be defined in '%s' expressions";
a723baf1
MM
15399
15400 /* Types cannot be defined in a `sizeof' expression. Save away the
15401 old message. */
15402 saved_message = parser->type_definition_forbidden_message;
15403 /* And create the new one. */
21526606
EC
15404 parser->type_definition_forbidden_message
15405 = xmalloc (strlen (format)
c68b0a84
KG
15406 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15407 + 1 /* `\0' */);
a723baf1
MM
15408 sprintf ((char *) parser->type_definition_forbidden_message,
15409 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15410
15411 /* The restrictions on constant-expressions do not apply inside
15412 sizeof expressions. */
93678513
MM
15413 saved_integral_constant_expression_p
15414 = parser->integral_constant_expression_p;
15415 saved_non_integral_constant_expression_p
15416 = parser->non_integral_constant_expression_p;
67c03833 15417 parser->integral_constant_expression_p = false;
a723baf1 15418
3beb3abf
MM
15419 /* Do not actually evaluate the expression. */
15420 ++skip_evaluation;
a723baf1
MM
15421 /* If it's a `(', then we might be looking at the type-id
15422 construction. */
15423 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15424 {
15425 tree type;
4f8163b1 15426 bool saved_in_type_id_in_expr_p;
a723baf1
MM
15427
15428 /* We can't be sure yet whether we're looking at a type-id or an
15429 expression. */
15430 cp_parser_parse_tentatively (parser);
15431 /* Consume the `('. */
15432 cp_lexer_consume_token (parser->lexer);
15433 /* Parse the type-id. */
4f8163b1
MM
15434 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15435 parser->in_type_id_in_expr_p = true;
a723baf1 15436 type = cp_parser_type_id (parser);
4f8163b1 15437 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1 15438 /* Now, look for the trailing `)'. */
9e637a26 15439 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
a723baf1
MM
15440 /* If all went well, then we're done. */
15441 if (cp_parser_parse_definitely (parser))
15442 {
62d1db17
MM
15443 cp_decl_specifier_seq decl_specs;
15444
15445 /* Build a trivial decl-specifier-seq. */
15446 clear_decl_specs (&decl_specs);
15447 decl_specs.type = type;
a723baf1
MM
15448
15449 /* Call grokdeclarator to figure out what type this is. */
058b15c1 15450 expr = grokdeclarator (NULL,
62d1db17 15451 &decl_specs,
a723baf1
MM
15452 TYPENAME,
15453 /*initialized=*/0,
15454 /*attrlist=*/NULL);
15455 }
15456 }
15457
15458 /* If the type-id production did not work out, then we must be
15459 looking at the unary-expression production. */
15460 if (!expr)
93678513
MM
15461 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15462 /*cast_p=*/false);
3beb3abf
MM
15463 /* Go back to evaluating expressions. */
15464 --skip_evaluation;
a723baf1
MM
15465
15466 /* Free the message we created. */
15467 free ((char *) parser->type_definition_forbidden_message);
15468 /* And restore the old one. */
15469 parser->type_definition_forbidden_message = saved_message;
93678513
MM
15470 parser->integral_constant_expression_p
15471 = saved_integral_constant_expression_p;
15472 parser->non_integral_constant_expression_p
15473 = saved_non_integral_constant_expression_p;
a723baf1
MM
15474
15475 return expr;
15476}
15477
15478/* If the current declaration has no declarator, return true. */
15479
15480static bool
15481cp_parser_declares_only_class_p (cp_parser *parser)
15482{
21526606 15483 /* If the next token is a `;' or a `,' then there is no
a723baf1
MM
15484 declarator. */
15485 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15486 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15487}
15488
62d1db17 15489/* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
a723baf1 15490
62d1db17
MM
15491static void
15492cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15493 cp_storage_class storage_class)
a723baf1 15494{
62d1db17
MM
15495 if (decl_specs->storage_class != sc_none)
15496 decl_specs->multiple_storage_classes_p = true;
15497 else
15498 decl_specs->storage_class = storage_class;
15499}
15500
15501/* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15502 is true, the type is a user-defined type; otherwise it is a
15503 built-in type specified by a keyword. */
a723baf1 15504
62d1db17
MM
15505static void
15506cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15507 tree type_spec,
15508 bool user_defined_p)
15509{
15510 decl_specs->any_specifiers_p = true;
98ca843c 15511
9306cccb
MM
15512 /* If the user tries to redeclare bool or wchar_t (with, for
15513 example, in "typedef int wchar_t;") we remember that this is what
f84b6c96
MM
15514 happened. In system headers, we ignore these declarations so
15515 that G++ can work with system headers that are not C++-safe. */
98ca843c 15516 if (decl_specs->specs[(int) ds_typedef]
f84b6c96 15517 && !user_defined_p
9306cccb
MM
15518 && (type_spec == boolean_type_node
15519 || type_spec == wchar_type_node)
f84b6c96
MM
15520 && (decl_specs->type
15521 || decl_specs->specs[(int) ds_long]
15522 || decl_specs->specs[(int) ds_short]
15523 || decl_specs->specs[(int) ds_unsigned]
15524 || decl_specs->specs[(int) ds_signed]))
0a73e37f
MM
15525 {
15526 decl_specs->redefined_builtin_type = type_spec;
15527 if (!decl_specs->type)
15528 {
15529 decl_specs->type = type_spec;
15530 decl_specs->user_defined_type_p = false;
15531 }
15532 }
f84b6c96
MM
15533 else if (decl_specs->type)
15534 decl_specs->multiple_types_p = true;
62d1db17
MM
15535 else
15536 {
15537 decl_specs->type = type_spec;
15538 decl_specs->user_defined_type_p = user_defined_p;
0a73e37f 15539 decl_specs->redefined_builtin_type = NULL_TREE;
a723baf1 15540 }
62d1db17 15541}
a723baf1 15542
62d1db17
MM
15543/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15544 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15545
15546static bool
15547cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15548{
15549 return decl_specifiers->specs[(int) ds_friend] != 0;
a723baf1
MM
15550}
15551
15552/* If the next token is of the indicated TYPE, consume it. Otherwise,
15553 issue an error message indicating that TOKEN_DESC was expected.
21526606 15554
a723baf1
MM
15555 Returns the token consumed, if the token had the appropriate type.
15556 Otherwise, returns NULL. */
15557
15558static cp_token *
94edc4ab
NN
15559cp_parser_require (cp_parser* parser,
15560 enum cpp_ttype type,
15561 const char* token_desc)
a723baf1
MM
15562{
15563 if (cp_lexer_next_token_is (parser->lexer, type))
15564 return cp_lexer_consume_token (parser->lexer);
15565 else
15566 {
e5976695
MM
15567 /* Output the MESSAGE -- unless we're parsing tentatively. */
15568 if (!cp_parser_simulate_error (parser))
216bb6e1
MM
15569 {
15570 char *message = concat ("expected ", token_desc, NULL);
15571 cp_parser_error (parser, message);
15572 free (message);
15573 }
a723baf1
MM
15574 return NULL;
15575 }
15576}
15577
15578/* Like cp_parser_require, except that tokens will be skipped until
15579 the desired token is found. An error message is still produced if
15580 the next token is not as expected. */
15581
15582static void
21526606
EC
15583cp_parser_skip_until_found (cp_parser* parser,
15584 enum cpp_ttype type,
94edc4ab 15585 const char* token_desc)
a723baf1
MM
15586{
15587 cp_token *token;
15588 unsigned nesting_depth = 0;
15589
15590 if (cp_parser_require (parser, type, token_desc))
15591 return;
15592
15593 /* Skip tokens until the desired token is found. */
15594 while (true)
15595 {
15596 /* Peek at the next token. */
15597 token = cp_lexer_peek_token (parser->lexer);
21526606 15598 /* If we've reached the token we want, consume it and
a723baf1
MM
15599 stop. */
15600 if (token->type == type && !nesting_depth)
15601 {
15602 cp_lexer_consume_token (parser->lexer);
15603 return;
15604 }
15605 /* If we've run out of tokens, stop. */
15606 if (token->type == CPP_EOF)
15607 return;
21526606 15608 if (token->type == CPP_OPEN_BRACE
a723baf1
MM
15609 || token->type == CPP_OPEN_PAREN
15610 || token->type == CPP_OPEN_SQUARE)
15611 ++nesting_depth;
21526606 15612 else if (token->type == CPP_CLOSE_BRACE
a723baf1
MM
15613 || token->type == CPP_CLOSE_PAREN
15614 || token->type == CPP_CLOSE_SQUARE)
15615 {
15616 if (nesting_depth-- == 0)
15617 return;
15618 }
15619 /* Consume this token. */
15620 cp_lexer_consume_token (parser->lexer);
15621 }
15622}
15623
15624/* If the next token is the indicated keyword, consume it. Otherwise,
15625 issue an error message indicating that TOKEN_DESC was expected.
21526606 15626
a723baf1
MM
15627 Returns the token consumed, if the token had the appropriate type.
15628 Otherwise, returns NULL. */
15629
15630static cp_token *
94edc4ab
NN
15631cp_parser_require_keyword (cp_parser* parser,
15632 enum rid keyword,
15633 const char* token_desc)
a723baf1
MM
15634{
15635 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15636
15637 if (token && token->keyword != keyword)
15638 {
15639 dyn_string_t error_msg;
15640
15641 /* Format the error message. */
15642 error_msg = dyn_string_new (0);
15643 dyn_string_append_cstr (error_msg, "expected ");
15644 dyn_string_append_cstr (error_msg, token_desc);
15645 cp_parser_error (parser, error_msg->s);
15646 dyn_string_delete (error_msg);
15647 return NULL;
15648 }
15649
15650 return token;
15651}
15652
15653/* Returns TRUE iff TOKEN is a token that can begin the body of a
15654 function-definition. */
15655
21526606 15656static bool
94edc4ab 15657cp_parser_token_starts_function_definition_p (cp_token* token)
a723baf1
MM
15658{
15659 return (/* An ordinary function-body begins with an `{'. */
15660 token->type == CPP_OPEN_BRACE
15661 /* A ctor-initializer begins with a `:'. */
15662 || token->type == CPP_COLON
15663 /* A function-try-block begins with `try'. */
15664 || token->keyword == RID_TRY
15665 /* The named return value extension begins with `return'. */
15666 || token->keyword == RID_RETURN);
15667}
15668
15669/* Returns TRUE iff the next token is the ":" or "{" beginning a class
15670 definition. */
15671
15672static bool
15673cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15674{
15675 cp_token *token;
15676
15677 token = cp_lexer_peek_token (parser->lexer);
15678 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15679}
15680
d17811fd 15681/* Returns TRUE iff the next token is the "," or ">" ending a
03fd3f84 15682 template-argument. */
d17811fd
MM
15683
15684static bool
15685cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15686{
15687 cp_token *token;
15688
15689 token = cp_lexer_peek_token (parser->lexer);
391c4bc5 15690 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
d17811fd 15691}
f4abade9
GB
15692
15693/* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15694 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15695
15696static bool
21526606 15697cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
f4abade9
GB
15698 size_t n)
15699{
15700 cp_token *token;
15701
15702 token = cp_lexer_peek_nth_token (parser->lexer, n);
15703 if (token->type == CPP_LESS)
15704 return true;
15705 /* Check for the sequence `<::' in the original code. It would be lexed as
15706 `[:', where `[' is a digraph, and there is no whitespace before
15707 `:'. */
15708 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15709 {
15710 cp_token *token2;
15711 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15712 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15713 return true;
15714 }
15715 return false;
15716}
21526606 15717
a723baf1
MM
15718/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15719 or none_type otherwise. */
15720
15721static enum tag_types
94edc4ab 15722cp_parser_token_is_class_key (cp_token* token)
a723baf1
MM
15723{
15724 switch (token->keyword)
15725 {
15726 case RID_CLASS:
15727 return class_type;
15728 case RID_STRUCT:
15729 return record_type;
15730 case RID_UNION:
15731 return union_type;
21526606 15732
a723baf1
MM
15733 default:
15734 return none_type;
15735 }
15736}
15737
15738/* Issue an error message if the CLASS_KEY does not match the TYPE. */
15739
15740static void
15741cp_parser_check_class_key (enum tag_types class_key, tree type)
15742{
15743 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
2a13a625 15744 pedwarn ("%qs tag used in naming %q#T",
a723baf1 15745 class_key == union_type ? "union"
21526606 15746 : class_key == record_type ? "struct" : "class",
a723baf1
MM
15747 type);
15748}
21526606 15749
cd0be382 15750/* Issue an error message if DECL is redeclared with different
37d407a1
KL
15751 access than its original declaration [class.access.spec/3].
15752 This applies to nested classes and nested class templates.
15753 [class.mem/1]. */
15754
2a13a625
GDR
15755static void
15756cp_parser_check_access_in_redeclaration (tree decl)
37d407a1
KL
15757{
15758 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15759 return;
15760
15761 if ((TREE_PRIVATE (decl)
15762 != (current_access_specifier == access_private_node))
15763 || (TREE_PROTECTED (decl)
15764 != (current_access_specifier == access_protected_node)))
2a13a625 15765 error ("%qD redeclared with different access", decl);
37d407a1
KL
15766}
15767
a723baf1 15768/* Look for the `template' keyword, as a syntactic disambiguator.
21526606 15769 Return TRUE iff it is present, in which case it will be
a723baf1
MM
15770 consumed. */
15771
15772static bool
15773cp_parser_optional_template_keyword (cp_parser *parser)
15774{
15775 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15776 {
15777 /* The `template' keyword can only be used within templates;
15778 outside templates the parser can always figure out what is a
15779 template and what is not. */
15780 if (!processing_template_decl)
15781 {
2a13a625 15782 error ("%<template%> (as a disambiguator) is only allowed "
a723baf1
MM
15783 "within templates");
15784 /* If this part of the token stream is rescanned, the same
15785 error message would be generated. So, we purge the token
15786 from the stream. */
15787 cp_lexer_purge_token (parser->lexer);
15788 return false;
15789 }
15790 else
15791 {
15792 /* Consume the `template' keyword. */
15793 cp_lexer_consume_token (parser->lexer);
15794 return true;
15795 }
15796 }
15797
15798 return false;
15799}
15800
2050a1bb
MM
15801/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15802 set PARSER->SCOPE, and perform other related actions. */
15803
15804static void
15805cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15806{
15807 tree value;
15808 tree check;
15809
15810 /* Get the stored value. */
15811 value = cp_lexer_consume_token (parser->lexer)->value;
15812 /* Perform any access checks that were deferred. */
15813 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c 15814 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
2050a1bb
MM
15815 /* Set the scope from the stored value. */
15816 parser->scope = TREE_VALUE (value);
15817 parser->qualifying_scope = TREE_TYPE (value);
15818 parser->object_scope = NULL_TREE;
15819}
15820
03fd3f84 15821/* Consume tokens up through a non-nested END token. */
a723baf1
MM
15822
15823static void
c162c75e
MA
15824cp_parser_cache_group (cp_parser *parser,
15825 enum cpp_ttype end,
15826 unsigned depth)
a723baf1
MM
15827{
15828 while (true)
15829 {
15830 cp_token *token;
15831
15832 /* Abort a parenthesized expression if we encounter a brace. */
15833 if ((end == CPP_CLOSE_PAREN || depth == 0)
15834 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15835 return;
a723baf1 15836 /* If we've reached the end of the file, stop. */
4bfb8bba 15837 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
a723baf1 15838 return;
4bfb8bba
MM
15839 /* Consume the next token. */
15840 token = cp_lexer_consume_token (parser->lexer);
a723baf1
MM
15841 /* See if it starts a new group. */
15842 if (token->type == CPP_OPEN_BRACE)
15843 {
c162c75e 15844 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
a723baf1
MM
15845 if (depth == 0)
15846 return;
15847 }
15848 else if (token->type == CPP_OPEN_PAREN)
c162c75e 15849 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
a723baf1
MM
15850 else if (token->type == end)
15851 return;
15852 }
15853}
15854
15855/* Begin parsing tentatively. We always save tokens while parsing
15856 tentatively so that if the tentative parsing fails we can restore the
15857 tokens. */
15858
15859static void
94edc4ab 15860cp_parser_parse_tentatively (cp_parser* parser)
a723baf1
MM
15861{
15862 /* Enter a new parsing context. */
15863 parser->context = cp_parser_context_new (parser->context);
15864 /* Begin saving tokens. */
15865 cp_lexer_save_tokens (parser->lexer);
15866 /* In order to avoid repetitive access control error messages,
15867 access checks are queued up until we are no longer parsing
15868 tentatively. */
8d241e0b 15869 push_deferring_access_checks (dk_deferred);
a723baf1
MM
15870}
15871
15872/* Commit to the currently active tentative parse. */
15873
15874static void
94edc4ab 15875cp_parser_commit_to_tentative_parse (cp_parser* parser)
a723baf1
MM
15876{
15877 cp_parser_context *context;
15878 cp_lexer *lexer;
15879
15880 /* Mark all of the levels as committed. */
15881 lexer = parser->lexer;
15882 for (context = parser->context; context->next; context = context->next)
15883 {
15884 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15885 break;
15886 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15887 while (!cp_lexer_saving_tokens (lexer))
15888 lexer = lexer->next;
15889 cp_lexer_commit_tokens (lexer);
15890 }
15891}
15892
15893/* Abort the currently active tentative parse. All consumed tokens
15894 will be rolled back, and no diagnostics will be issued. */
15895
15896static void
94edc4ab 15897cp_parser_abort_tentative_parse (cp_parser* parser)
a723baf1
MM
15898{
15899 cp_parser_simulate_error (parser);
15900 /* Now, pretend that we want to see if the construct was
15901 successfully parsed. */
15902 cp_parser_parse_definitely (parser);
15903}
15904
34cd5ae7 15905/* Stop parsing tentatively. If a parse error has occurred, restore the
a723baf1
MM
15906 token stream. Otherwise, commit to the tokens we have consumed.
15907 Returns true if no error occurred; false otherwise. */
15908
15909static bool
94edc4ab 15910cp_parser_parse_definitely (cp_parser* parser)
a723baf1
MM
15911{
15912 bool error_occurred;
15913 cp_parser_context *context;
15914
34cd5ae7 15915 /* Remember whether or not an error occurred, since we are about to
a723baf1
MM
15916 destroy that information. */
15917 error_occurred = cp_parser_error_occurred (parser);
15918 /* Remove the topmost context from the stack. */
15919 context = parser->context;
15920 parser->context = context->next;
15921 /* If no parse errors occurred, commit to the tentative parse. */
15922 if (!error_occurred)
15923 {
15924 /* Commit to the tokens read tentatively, unless that was
15925 already done. */
15926 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15927 cp_lexer_commit_tokens (parser->lexer);
cf22909c
KL
15928
15929 pop_to_parent_deferring_access_checks ();
a723baf1
MM
15930 }
15931 /* Otherwise, if errors occurred, roll back our state so that things
15932 are just as they were before we began the tentative parse. */
15933 else
cf22909c
KL
15934 {
15935 cp_lexer_rollback_tokens (parser->lexer);
15936 pop_deferring_access_checks ();
15937 }
e5976695
MM
15938 /* Add the context to the front of the free list. */
15939 context->next = cp_parser_context_free_list;
15940 cp_parser_context_free_list = context;
15941
15942 return !error_occurred;
a723baf1
MM
15943}
15944
0b16f8f4
VR
15945/* Returns true if we are parsing tentatively and are not committed to
15946 this tentative parse. */
a723baf1
MM
15947
15948static bool
0b16f8f4 15949cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
a723baf1
MM
15950{
15951 return (cp_parser_parsing_tentatively (parser)
0b16f8f4 15952 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
a723baf1
MM
15953}
15954
4de8668e 15955/* Returns nonzero iff an error has occurred during the most recent
a723baf1 15956 tentative parse. */
21526606 15957
a723baf1 15958static bool
94edc4ab 15959cp_parser_error_occurred (cp_parser* parser)
a723baf1
MM
15960{
15961 return (cp_parser_parsing_tentatively (parser)
15962 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15963}
15964
4de8668e 15965/* Returns nonzero if GNU extensions are allowed. */
a723baf1
MM
15966
15967static bool
94edc4ab 15968cp_parser_allow_gnu_extensions_p (cp_parser* parser)
a723baf1
MM
15969{
15970 return parser->allow_gnu_extensions_p;
15971}
15972
15973\f
a723baf1
MM
15974/* The parser. */
15975
15976static GTY (()) cp_parser *the_parser;
15977
15978/* External interface. */
15979
d1bd0ded 15980/* Parse one entire translation unit. */
a723baf1 15981
d1bd0ded
GK
15982void
15983c_parse_file (void)
a723baf1
MM
15984{
15985 bool error_occurred;
f75fbaf7
ZW
15986 static bool already_called = false;
15987
15988 if (already_called)
15989 {
15990 sorry ("inter-module optimizations not implemented for C++");
15991 return;
15992 }
15993 already_called = true;
a723baf1
MM
15994
15995 the_parser = cp_parser_new ();
78757caa
KL
15996 push_deferring_access_checks (flag_access_control
15997 ? dk_no_deferred : dk_no_check);
a723baf1
MM
15998 error_occurred = cp_parser_translation_unit (the_parser);
15999 the_parser = NULL;
a723baf1
MM
16000}
16001
a723baf1
MM
16002/* This variable must be provided by every front end. */
16003
16004int yydebug;
16005
16006#include "gt-cp-parser.h"