]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
* cse.c (get_cse_reg_info): Update a comment.
[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
b3445994 1360 (cp_parser *, 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
MM
1371static tree cp_parser_postfix_expression
1372 (cp_parser *, 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
39703eb9 1378 (cp_parser *, bool, bool *);
a723baf1 1379static void cp_parser_pseudo_destructor_name
94edc4ab 1380 (cp_parser *, tree *, tree *);
a723baf1
MM
1381static tree cp_parser_unary_expression
1382 (cp_parser *, bool);
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
a723baf1 1400 (cp_parser *, bool);
b8b94c5b 1401static tree cp_parser_binary_expression
94edc4ab 1402 (cp_parser *);
a723baf1 1403static tree cp_parser_question_colon_clause
94edc4ab 1404 (cp_parser *, tree);
a723baf1 1405static tree cp_parser_assignment_expression
94edc4ab 1406 (cp_parser *);
a723baf1 1407static enum tree_code cp_parser_assignment_operator_opt
94edc4ab 1408 (cp_parser *);
a723baf1 1409static tree cp_parser_expression
94edc4ab 1410 (cp_parser *);
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,
1948 return false, marking the current expression as non-constant. */
14d22dd6 1949
625cbf93
MM
1950static bool
1951cp_parser_non_integral_constant_expression (cp_parser *parser,
1952 const char *thing)
14d22dd6 1953{
625cbf93
MM
1954 if (parser->integral_constant_expression_p)
1955 {
1956 if (!parser->allow_non_integral_constant_expression_p)
1957 {
1958 error ("%s cannot appear in a constant-expression", thing);
1959 return true;
1960 }
1961 parser->non_integral_constant_expression_p = true;
1962 }
1963 return false;
14d22dd6
MM
1964}
1965
0c88d886 1966/* Emit a diagnostic for an invalid type name. SCOPE is the
6ca2d67f
MM
1967 qualifying scope (or NULL, if none) for ID. This function commits
1968 to the current active tentative parse, if any. (Otherwise, the
1969 problematic construct might be encountered again later, resulting
1970 in duplicate error messages.) */
8fbc5ae7 1971
2097b5f2
GB
1972static void
1973cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
6c0cc713
GB
1974{
1975 tree decl, old_scope;
2097b5f2
GB
1976 /* Try to lookup the identifier. */
1977 old_scope = parser->scope;
1978 parser->scope = scope;
1979 decl = cp_parser_lookup_name_simple (parser, id);
1980 parser->scope = old_scope;
1981 /* If the lookup found a template-name, it means that the user forgot
1982 to specify an argument list. Emit an useful error message. */
1983 if (TREE_CODE (decl) == TEMPLATE_DECL)
2a13a625 1984 error ("invalid use of template-name %qE without an argument list",
6c0cc713 1985 decl);
2097b5f2 1986 else if (!parser->scope)
8fbc5ae7 1987 {
8fbc5ae7 1988 /* Issue an error message. */
2a13a625 1989 error ("%qE does not name a type", id);
8fbc5ae7
MM
1990 /* If we're in a template class, it's possible that the user was
1991 referring to a type from a base class. For example:
1992
1993 template <typename T> struct A { typedef T X; };
1994 template <typename T> struct B : public A<T> { X x; };
1995
1996 The user should have said "typename A<T>::X". */
1997 if (processing_template_decl && current_class_type)
1998 {
1999 tree b;
2000
2001 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2002 b;
2003 b = TREE_CHAIN (b))
2004 {
2005 tree base_type = BINFO_TYPE (b);
21526606 2006 if (CLASS_TYPE_P (base_type)
1fb3244a 2007 && dependent_type_p (base_type))
8fbc5ae7
MM
2008 {
2009 tree field;
2010 /* Go from a particular instantiation of the
2011 template (which will have an empty TYPE_FIELDs),
2012 to the main version. */
353b4fc0 2013 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
8fbc5ae7
MM
2014 for (field = TYPE_FIELDS (base_type);
2015 field;
2016 field = TREE_CHAIN (field))
2017 if (TREE_CODE (field) == TYPE_DECL
2097b5f2 2018 && DECL_NAME (field) == id)
8fbc5ae7 2019 {
c4f73174 2020 inform ("(perhaps %<typename %T::%E%> was intended)",
2097b5f2 2021 BINFO_TYPE (b), id);
8fbc5ae7
MM
2022 break;
2023 }
2024 if (field)
2025 break;
2026 }
2027 }
2028 }
8fbc5ae7 2029 }
2097b5f2
GB
2030 /* Here we diagnose qualified-ids where the scope is actually correct,
2031 but the identifier does not resolve to a valid type name. */
21526606 2032 else
2097b5f2
GB
2033 {
2034 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2a13a625 2035 error ("%qE in namespace %qE does not name a type",
2097b5f2
GB
2036 id, parser->scope);
2037 else if (TYPE_P (parser->scope))
2fbe4889 2038 error ("%qE in class %qT does not name a type", id, parser->scope);
2097b5f2 2039 else
315fb5db 2040 gcc_unreachable ();
2097b5f2 2041 }
6ca2d67f 2042 cp_parser_commit_to_tentative_parse (parser);
2097b5f2 2043}
8fbc5ae7 2044
2097b5f2
GB
2045/* Check for a common situation where a type-name should be present,
2046 but is not, and issue a sensible error message. Returns true if an
2047 invalid type-name was detected.
21526606 2048
2097b5f2 2049 The situation handled by this function are variable declarations of the
21526606
EC
2050 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2051 Usually, `ID' should name a type, but if we got here it means that it
2097b5f2
GB
2052 does not. We try to emit the best possible error message depending on
2053 how exactly the id-expression looks like.
2054*/
2055
2056static bool
2057cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2058{
2059 tree id;
2060
2061 cp_parser_parse_tentatively (parser);
21526606 2062 id = cp_parser_id_expression (parser,
2097b5f2
GB
2063 /*template_keyword_p=*/false,
2064 /*check_dependency_p=*/true,
2065 /*template_p=*/NULL,
2066 /*declarator_p=*/true);
2067 /* After the id-expression, there should be a plain identifier,
2068 otherwise this is not a simple variable declaration. Also, if
2069 the scope is dependent, we cannot do much. */
2070 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 2071 || (parser->scope && TYPE_P (parser->scope)
2097b5f2
GB
2072 && dependent_type_p (parser->scope)))
2073 {
2074 cp_parser_abort_tentative_parse (parser);
2075 return false;
2076 }
3590f0a6
MM
2077 if (!cp_parser_parse_definitely (parser)
2078 || TREE_CODE (id) != IDENTIFIER_NODE)
2097b5f2
GB
2079 return false;
2080
2097b5f2
GB
2081 /* Emit a diagnostic for the invalid type. */
2082 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2083 /* Skip to the end of the declaration; there's no point in
2084 trying to process it. */
2085 cp_parser_skip_to_end_of_block_or_statement (parser);
2086 return true;
8fbc5ae7
MM
2087}
2088
21526606 2089/* Consume tokens up to, and including, the next non-nested closing `)'.
7efa3e22
NS
2090 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2091 are doing error recovery. Returns -1 if OR_COMMA is true and we
2092 found an unnested comma. */
a723baf1 2093
7efa3e22
NS
2094static int
2095cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
21526606 2096 bool recovering,
a668c6ad
MM
2097 bool or_comma,
2098 bool consume_paren)
a723baf1 2099{
7efa3e22
NS
2100 unsigned paren_depth = 0;
2101 unsigned brace_depth = 0;
0173bb6f 2102 int result;
a723baf1 2103
0b16f8f4
VR
2104 if (recovering && !or_comma
2105 && cp_parser_uncommitted_to_tentative_parse_p (parser))
7efa3e22 2106 return 0;
21526606 2107
a723baf1
MM
2108 while (true)
2109 {
2110 cp_token *token;
21526606 2111
a723baf1
MM
2112 /* If we've run out of tokens, then there is no closing `)'. */
2113 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
0173bb6f
AO
2114 {
2115 result = 0;
2116 break;
2117 }
a723baf1 2118
a668c6ad 2119 token = cp_lexer_peek_token (parser->lexer);
21526606 2120
f4f206f4 2121 /* This matches the processing in skip_to_end_of_statement. */
a668c6ad 2122 if (token->type == CPP_SEMICOLON && !brace_depth)
0173bb6f
AO
2123 {
2124 result = 0;
2125 break;
2126 }
a668c6ad
MM
2127 if (token->type == CPP_OPEN_BRACE)
2128 ++brace_depth;
2129 if (token->type == CPP_CLOSE_BRACE)
7efa3e22 2130 {
a668c6ad 2131 if (!brace_depth--)
0173bb6f
AO
2132 {
2133 result = 0;
2134 break;
2135 }
7efa3e22 2136 }
a668c6ad
MM
2137 if (recovering && or_comma && token->type == CPP_COMMA
2138 && !brace_depth && !paren_depth)
0173bb6f
AO
2139 {
2140 result = -1;
2141 break;
2142 }
21526606 2143
7efa3e22
NS
2144 if (!brace_depth)
2145 {
2146 /* If it is an `(', we have entered another level of nesting. */
2147 if (token->type == CPP_OPEN_PAREN)
2148 ++paren_depth;
2149 /* If it is a `)', then we might be done. */
2150 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
a668c6ad
MM
2151 {
2152 if (consume_paren)
2153 cp_lexer_consume_token (parser->lexer);
0173bb6f
AO
2154 {
2155 result = 1;
2156 break;
2157 }
a668c6ad 2158 }
7efa3e22 2159 }
21526606 2160
a668c6ad
MM
2161 /* Consume the token. */
2162 cp_lexer_consume_token (parser->lexer);
a723baf1 2163 }
0173bb6f 2164
0173bb6f 2165 return result;
a723baf1
MM
2166}
2167
2168/* Consume tokens until we reach the end of the current statement.
2169 Normally, that will be just before consuming a `;'. However, if a
2170 non-nested `}' comes first, then we stop before consuming that. */
2171
2172static void
94edc4ab 2173cp_parser_skip_to_end_of_statement (cp_parser* parser)
a723baf1
MM
2174{
2175 unsigned nesting_depth = 0;
2176
2177 while (true)
2178 {
2179 cp_token *token;
2180
2181 /* Peek at the next token. */
2182 token = cp_lexer_peek_token (parser->lexer);
2183 /* If we've run out of tokens, stop. */
2184 if (token->type == CPP_EOF)
2185 break;
2186 /* If the next token is a `;', we have reached the end of the
2187 statement. */
2188 if (token->type == CPP_SEMICOLON && !nesting_depth)
2189 break;
2190 /* If the next token is a non-nested `}', then we have reached
2191 the end of the current block. */
2192 if (token->type == CPP_CLOSE_BRACE)
2193 {
2194 /* If this is a non-nested `}', stop before consuming it.
2195 That way, when confronted with something like:
2196
21526606 2197 { 3 + }
a723baf1
MM
2198
2199 we stop before consuming the closing `}', even though we
2200 have not yet reached a `;'. */
2201 if (nesting_depth == 0)
2202 break;
2203 /* If it is the closing `}' for a block that we have
2204 scanned, stop -- but only after consuming the token.
2205 That way given:
2206
2207 void f g () { ... }
2208 typedef int I;
2209
2210 we will stop after the body of the erroneously declared
2211 function, but before consuming the following `typedef'
2212 declaration. */
2213 if (--nesting_depth == 0)
2214 {
2215 cp_lexer_consume_token (parser->lexer);
2216 break;
2217 }
2218 }
2219 /* If it the next token is a `{', then we are entering a new
2220 block. Consume the entire block. */
2221 else if (token->type == CPP_OPEN_BRACE)
2222 ++nesting_depth;
2223 /* Consume the token. */
2224 cp_lexer_consume_token (parser->lexer);
2225 }
2226}
2227
e0860732
MM
2228/* This function is called at the end of a statement or declaration.
2229 If the next token is a semicolon, it is consumed; otherwise, error
2230 recovery is attempted. */
2231
2232static void
2233cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2234{
2235 /* Look for the trailing `;'. */
2236 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2237 {
2238 /* If there is additional (erroneous) input, skip to the end of
2239 the statement. */
2240 cp_parser_skip_to_end_of_statement (parser);
2241 /* If the next token is now a `;', consume it. */
2242 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2243 cp_lexer_consume_token (parser->lexer);
2244 }
2245}
2246
a723baf1
MM
2247/* Skip tokens until we have consumed an entire block, or until we
2248 have consumed a non-nested `;'. */
2249
2250static void
94edc4ab 2251cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
a723baf1
MM
2252{
2253 unsigned nesting_depth = 0;
2254
2255 while (true)
2256 {
2257 cp_token *token;
2258
2259 /* Peek at the next token. */
2260 token = cp_lexer_peek_token (parser->lexer);
2261 /* If we've run out of tokens, stop. */
2262 if (token->type == CPP_EOF)
2263 break;
2264 /* If the next token is a `;', we have reached the end of the
2265 statement. */
2266 if (token->type == CPP_SEMICOLON && !nesting_depth)
2267 {
2268 /* Consume the `;'. */
2269 cp_lexer_consume_token (parser->lexer);
2270 break;
2271 }
2272 /* Consume the token. */
2273 token = cp_lexer_consume_token (parser->lexer);
2274 /* If the next token is a non-nested `}', then we have reached
2275 the end of the current block. */
21526606 2276 if (token->type == CPP_CLOSE_BRACE
a723baf1
MM
2277 && (nesting_depth == 0 || --nesting_depth == 0))
2278 break;
2279 /* If it the next token is a `{', then we are entering a new
2280 block. Consume the entire block. */
2281 if (token->type == CPP_OPEN_BRACE)
2282 ++nesting_depth;
2283 }
2284}
2285
2286/* Skip tokens until a non-nested closing curly brace is the next
2287 token. */
2288
2289static void
2290cp_parser_skip_to_closing_brace (cp_parser *parser)
2291{
2292 unsigned nesting_depth = 0;
2293
2294 while (true)
2295 {
2296 cp_token *token;
2297
2298 /* Peek at the next token. */
2299 token = cp_lexer_peek_token (parser->lexer);
2300 /* If we've run out of tokens, stop. */
2301 if (token->type == CPP_EOF)
2302 break;
2303 /* If the next token is a non-nested `}', then we have reached
2304 the end of the current block. */
2305 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2306 break;
2307 /* If it the next token is a `{', then we are entering a new
2308 block. Consume the entire block. */
2309 else if (token->type == CPP_OPEN_BRACE)
2310 ++nesting_depth;
2311 /* Consume the token. */
2312 cp_lexer_consume_token (parser->lexer);
2313 }
2314}
2315
2097b5f2
GB
2316/* This is a simple wrapper around make_typename_type. When the id is
2317 an unresolved identifier node, we can provide a superior diagnostic
2318 using cp_parser_diagnose_invalid_type_name. */
2319
2320static tree
2321cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
6c0cc713
GB
2322{
2323 tree result;
2324 if (TREE_CODE (id) == IDENTIFIER_NODE)
2325 {
fc6a28d7
MM
2326 result = make_typename_type (scope, id, typename_type,
2327 /*complain=*/0);
6c0cc713
GB
2328 if (result == error_mark_node)
2329 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2330 return result;
2331 }
fc6a28d7 2332 return make_typename_type (scope, id, typename_type, tf_error);
2097b5f2
GB
2333}
2334
2335
a723baf1
MM
2336/* Create a new C++ parser. */
2337
2338static cp_parser *
94edc4ab 2339cp_parser_new (void)
a723baf1
MM
2340{
2341 cp_parser *parser;
17211ab5 2342 cp_lexer *lexer;
b8b94c5b 2343 unsigned i;
17211ab5
GK
2344
2345 /* cp_lexer_new_main is called before calling ggc_alloc because
2346 cp_lexer_new_main might load a PCH file. */
2347 lexer = cp_lexer_new_main ();
a723baf1 2348
b8b94c5b
PB
2349 /* Initialize the binops_by_token so that we can get the tree
2350 directly from the token. */
2351 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2352 binops_by_token[binops[i].token_type] = binops[i];
2353
99dd239f 2354 parser = GGC_CNEW (cp_parser);
17211ab5 2355 parser->lexer = lexer;
a723baf1
MM
2356 parser->context = cp_parser_context_new (NULL);
2357
2358 /* For now, we always accept GNU extensions. */
2359 parser->allow_gnu_extensions_p = 1;
2360
2361 /* The `>' token is a greater-than operator, not the end of a
2362 template-id. */
2363 parser->greater_than_is_operator_p = true;
2364
2365 parser->default_arg_ok_p = true;
21526606 2366
a723baf1 2367 /* We are not parsing a constant-expression. */
67c03833
JM
2368 parser->integral_constant_expression_p = false;
2369 parser->allow_non_integral_constant_expression_p = false;
2370 parser->non_integral_constant_expression_p = false;
a723baf1
MM
2371
2372 /* Local variable names are not forbidden. */
2373 parser->local_variables_forbidden_p = false;
2374
34cd5ae7 2375 /* We are not processing an `extern "C"' declaration. */
a723baf1
MM
2376 parser->in_unbraced_linkage_specification_p = false;
2377
2378 /* We are not processing a declarator. */
2379 parser->in_declarator_p = false;
2380
4bb8ca28
MM
2381 /* We are not processing a template-argument-list. */
2382 parser->in_template_argument_list_p = false;
2383
0e59b3fb
MM
2384 /* We are not in an iteration statement. */
2385 parser->in_iteration_statement_p = false;
2386
2387 /* We are not in a switch statement. */
2388 parser->in_switch_statement_p = false;
2389
4f8163b1
MM
2390 /* We are not parsing a type-id inside an expression. */
2391 parser->in_type_id_in_expr_p = false;
2392
03fd3f84 2393 /* Declarations aren't implicitly extern "C". */
7d381002
MA
2394 parser->implicit_extern_c = false;
2395
c162c75e
MA
2396 /* String literals should be translated to the execution character set. */
2397 parser->translate_strings_p = true;
2398
a723baf1
MM
2399 /* The unparsed function queue is empty. */
2400 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2401
2402 /* There are no classes being defined. */
2403 parser->num_classes_being_defined = 0;
2404
2405 /* No template parameters apply. */
2406 parser->num_template_parameter_lists = 0;
2407
2408 return parser;
2409}
2410
2cfe82fe
ZW
2411/* Create a cp_lexer structure which will emit the tokens in CACHE
2412 and push it onto the parser's lexer stack. This is used for delayed
2413 parsing of in-class method bodies and default arguments, and should
2414 not be confused with tentative parsing. */
2415static void
2416cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2417{
2418 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2419 lexer->next = parser->lexer;
2420 parser->lexer = lexer;
2421
2422 /* Move the current source position to that of the first token in the
2423 new lexer. */
2424 cp_lexer_set_source_position_from_token (lexer->next_token);
2425}
2426
2427/* Pop the top lexer off the parser stack. This is never used for the
2428 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2429static void
2430cp_parser_pop_lexer (cp_parser *parser)
2431{
2432 cp_lexer *lexer = parser->lexer;
2433 parser->lexer = lexer->next;
2434 cp_lexer_destroy (lexer);
2435
2436 /* Put the current source position back where it was before this
2437 lexer was pushed. */
2438 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2439}
2440
a723baf1
MM
2441/* Lexical conventions [gram.lex] */
2442
2443/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2444 identifier. */
2445
21526606 2446static tree
94edc4ab 2447cp_parser_identifier (cp_parser* parser)
a723baf1
MM
2448{
2449 cp_token *token;
2450
2451 /* Look for the identifier. */
2452 token = cp_parser_require (parser, CPP_NAME, "identifier");
2453 /* Return the value. */
2454 return token ? token->value : error_mark_node;
2455}
2456
c162c75e
MA
2457/* Parse a sequence of adjacent string constants. Returns a
2458 TREE_STRING representing the combined, nul-terminated string
2459 constant. If TRANSLATE is true, translate the string to the
2460 execution character set. If WIDE_OK is true, a wide string is
2461 invalid here.
2462
2463 C++98 [lex.string] says that if a narrow string literal token is
2464 adjacent to a wide string literal token, the behavior is undefined.
2465 However, C99 6.4.5p4 says that this results in a wide string literal.
2466 We follow C99 here, for consistency with the C front end.
2467
2468 This code is largely lifted from lex_string() in c-lex.c.
2469
2470 FUTURE: ObjC++ will need to handle @-strings here. */
2471static tree
2472cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2473{
2474 tree value;
2475 bool wide = false;
2476 size_t count;
2477 struct obstack str_ob;
2478 cpp_string str, istr, *strs;
2479 cp_token *tok;
2480
2481 tok = cp_lexer_peek_token (parser->lexer);
2482 if (!cp_parser_is_string_literal (tok))
2483 {
2484 cp_parser_error (parser, "expected string-literal");
2485 return error_mark_node;
2486 }
2487
9688c3b8 2488 /* Try to avoid the overhead of creating and destroying an obstack
c162c75e 2489 for the common case of just one string. */
2cfe82fe
ZW
2490 if (!cp_parser_is_string_literal
2491 (cp_lexer_peek_nth_token (parser->lexer, 2)))
c162c75e 2492 {
2cfe82fe
ZW
2493 cp_lexer_consume_token (parser->lexer);
2494
c162c75e
MA
2495 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2496 str.len = TREE_STRING_LENGTH (tok->value);
2497 count = 1;
2498 if (tok->type == CPP_WSTRING)
2499 wide = true;
c162c75e
MA
2500
2501 strs = &str;
2502 }
2503 else
2504 {
2505 gcc_obstack_init (&str_ob);
2506 count = 0;
2507
2508 do
2509 {
2cfe82fe 2510 cp_lexer_consume_token (parser->lexer);
c162c75e
MA
2511 count++;
2512 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2513 str.len = TREE_STRING_LENGTH (tok->value);
2514 if (tok->type == CPP_WSTRING)
2515 wide = true;
2516
2517 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2518
2cfe82fe 2519 tok = cp_lexer_peek_token (parser->lexer);
c162c75e
MA
2520 }
2521 while (cp_parser_is_string_literal (tok));
2522
2523 strs = (cpp_string *) obstack_finish (&str_ob);
2524 }
2525
2526 if (wide && !wide_ok)
2527 {
2528 cp_parser_error (parser, "a wide string is invalid in this context");
2529 wide = false;
2530 }
2531
2532 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2533 (parse_in, strs, count, &istr, wide))
2534 {
2535 value = build_string (istr.len, (char *)istr.text);
2536 free ((void *)istr.text);
2537
2538 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2539 value = fix_string_type (value);
2540 }
2541 else
2542 /* cpp_interpret_string has issued an error. */
2543 value = error_mark_node;
2544
2545 if (count > 1)
2546 obstack_free (&str_ob, 0);
2547
2548 return value;
2549}
2550
2551
a723baf1
MM
2552/* Basic concepts [gram.basic] */
2553
2554/* Parse a translation-unit.
2555
2556 translation-unit:
21526606 2557 declaration-seq [opt]
a723baf1
MM
2558
2559 Returns TRUE if all went well. */
2560
2561static bool
94edc4ab 2562cp_parser_translation_unit (cp_parser* parser)
a723baf1 2563{
058b15c1
MM
2564 /* The address of the first non-permanent object on the declarator
2565 obstack. */
2566 static void *declarator_obstack_base;
2567
2568 bool success;
98ca843c 2569
058b15c1
MM
2570 /* Create the declarator obstack, if necessary. */
2571 if (!cp_error_declarator)
2572 {
2573 gcc_obstack_init (&declarator_obstack);
2574 /* Create the error declarator. */
2575 cp_error_declarator = make_declarator (cdk_error);
2576 /* Create the empty parameter list. */
62d1db17 2577 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
058b15c1
MM
2578 /* Remember where the base of the declarator obstack lies. */
2579 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2580 }
2581
a723baf1
MM
2582 while (true)
2583 {
2584 cp_parser_declaration_seq_opt (parser);
2585
2586 /* If there are no tokens left then all went well. */
2587 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
058b15c1 2588 {
03fd3f84 2589 /* Get rid of the token array; we don't need it any more. */
c162c75e
MA
2590 cp_lexer_destroy (parser->lexer);
2591 parser->lexer = NULL;
2592
7d381002
MA
2593 /* This file might have been a context that's implicitly extern
2594 "C". If so, pop the lang context. (Only relevant for PCH.) */
2595 if (parser->implicit_extern_c)
2596 {
2597 pop_lang_context ();
2598 parser->implicit_extern_c = false;
2599 }
2600
058b15c1
MM
2601 /* Finish up. */
2602 finish_translation_unit ();
21526606 2603
058b15c1
MM
2604 success = true;
2605 break;
2606 }
2607 else
2608 {
2609 cp_parser_error (parser, "expected declaration");
2610 success = false;
2611 break;
2612 }
a723baf1
MM
2613 }
2614
058b15c1 2615 /* Make sure the declarator obstack was fully cleaned up. */
50bc768d
NS
2616 gcc_assert (obstack_next_free (&declarator_obstack)
2617 == declarator_obstack_base);
a723baf1
MM
2618
2619 /* All went well. */
058b15c1 2620 return success;
a723baf1
MM
2621}
2622
2623/* Expressions [gram.expr] */
2624
2625/* Parse a primary-expression.
2626
2627 primary-expression:
2628 literal
2629 this
2630 ( expression )
2631 id-expression
2632
2633 GNU Extensions:
2634
2635 primary-expression:
2636 ( compound-statement )
2637 __builtin_va_arg ( assignment-expression , type-id )
2638
2639 literal:
2640 __null
2641
21526606 2642 Returns a representation of the expression.
a723baf1 2643
21526606 2644 *IDK indicates what kind of id-expression (if any) was present.
a723baf1
MM
2645
2646 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2647 used as the operand of a pointer-to-member. In that case,
2648 *QUALIFYING_CLASS gives the class that is used as the qualifying
2649 class in the pointer-to-member. */
2650
2651static tree
21526606 2652cp_parser_primary_expression (cp_parser *parser,
b3445994 2653 cp_id_kind *idk,
a723baf1
MM
2654 tree *qualifying_class)
2655{
2656 cp_token *token;
2657
2658 /* Assume the primary expression is not an id-expression. */
b3445994 2659 *idk = CP_ID_KIND_NONE;
a723baf1
MM
2660 /* And that it cannot be used as pointer-to-member. */
2661 *qualifying_class = NULL_TREE;
2662
2663 /* Peek at the next token. */
2664 token = cp_lexer_peek_token (parser->lexer);
2665 switch (token->type)
2666 {
2667 /* literal:
2668 integer-literal
2669 character-literal
2670 floating-literal
2671 string-literal
2672 boolean-literal */
2673 case CPP_CHAR:
2674 case CPP_WCHAR:
a723baf1
MM
2675 case CPP_NUMBER:
2676 token = cp_lexer_consume_token (parser->lexer);
2677 return token->value;
2678
0173bb6f
AO
2679 case CPP_STRING:
2680 case CPP_WSTRING:
c162c75e
MA
2681 /* ??? Should wide strings be allowed when parser->translate_strings_p
2682 is false (i.e. in attributes)? If not, we can kill the third
2683 argument to cp_parser_string_literal. */
2684 return cp_parser_string_literal (parser,
2685 parser->translate_strings_p,
2686 true);
0173bb6f 2687
a723baf1
MM
2688 case CPP_OPEN_PAREN:
2689 {
2690 tree expr;
2691 bool saved_greater_than_is_operator_p;
2692
2693 /* Consume the `('. */
2694 cp_lexer_consume_token (parser->lexer);
2695 /* Within a parenthesized expression, a `>' token is always
2696 the greater-than operator. */
21526606 2697 saved_greater_than_is_operator_p
a723baf1
MM
2698 = parser->greater_than_is_operator_p;
2699 parser->greater_than_is_operator_p = true;
2700 /* If we see `( { ' then we are looking at the beginning of
2701 a GNU statement-expression. */
2702 if (cp_parser_allow_gnu_extensions_p (parser)
2703 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2704 {
2705 /* Statement-expressions are not allowed by the standard. */
2706 if (pedantic)
21526606
EC
2707 pedwarn ("ISO C++ forbids braced-groups within expressions");
2708
a723baf1
MM
2709 /* And they're not allowed outside of a function-body; you
2710 cannot, for example, write:
21526606 2711
a723baf1 2712 int i = ({ int j = 3; j + 1; });
21526606 2713
a723baf1
MM
2714 at class or namespace scope. */
2715 if (!at_function_scope_p ())
2716 error ("statement-expressions are allowed only inside functions");
2717 /* Start the statement-expression. */
2718 expr = begin_stmt_expr ();
2719 /* Parse the compound-statement. */
325c3691 2720 cp_parser_compound_statement (parser, expr, false);
a723baf1 2721 /* Finish up. */
303b7406 2722 expr = finish_stmt_expr (expr, false);
a723baf1
MM
2723 }
2724 else
2725 {
2726 /* Parse the parenthesized expression. */
2727 expr = cp_parser_expression (parser);
2728 /* Let the front end know that this expression was
2729 enclosed in parentheses. This matters in case, for
2730 example, the expression is of the form `A::B', since
2731 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2732 not. */
2733 finish_parenthesized_expr (expr);
2734 }
2735 /* The `>' token might be the end of a template-id or
2736 template-parameter-list now. */
21526606 2737 parser->greater_than_is_operator_p
a723baf1
MM
2738 = saved_greater_than_is_operator_p;
2739 /* Consume the `)'. */
2740 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2741 cp_parser_skip_to_end_of_statement (parser);
2742
2743 return expr;
2744 }
2745
2746 case CPP_KEYWORD:
2747 switch (token->keyword)
2748 {
2749 /* These two are the boolean literals. */
2750 case RID_TRUE:
2751 cp_lexer_consume_token (parser->lexer);
2752 return boolean_true_node;
2753 case RID_FALSE:
2754 cp_lexer_consume_token (parser->lexer);
2755 return boolean_false_node;
21526606 2756
a723baf1
MM
2757 /* The `__null' literal. */
2758 case RID_NULL:
2759 cp_lexer_consume_token (parser->lexer);
2760 return null_node;
2761
2762 /* Recognize the `this' keyword. */
2763 case RID_THIS:
2764 cp_lexer_consume_token (parser->lexer);
2765 if (parser->local_variables_forbidden_p)
2766 {
2a13a625 2767 error ("%<this%> may not be used in this context");
a723baf1
MM
2768 return error_mark_node;
2769 }
14d22dd6 2770 /* Pointers cannot appear in constant-expressions. */
625cbf93
MM
2771 if (cp_parser_non_integral_constant_expression (parser,
2772 "`this'"))
2773 return error_mark_node;
a723baf1
MM
2774 return finish_this_expr ();
2775
2776 /* The `operator' keyword can be the beginning of an
2777 id-expression. */
2778 case RID_OPERATOR:
2779 goto id_expression;
2780
2781 case RID_FUNCTION_NAME:
2782 case RID_PRETTY_FUNCTION_NAME:
2783 case RID_C99_FUNCTION_NAME:
2784 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2785 __func__ are the names of variables -- but they are
2786 treated specially. Therefore, they are handled here,
2787 rather than relying on the generic id-expression logic
21526606 2788 below. Grammatically, these names are id-expressions.
a723baf1
MM
2789
2790 Consume the token. */
2791 token = cp_lexer_consume_token (parser->lexer);
2792 /* Look up the name. */
2793 return finish_fname (token->value);
2794
2795 case RID_VA_ARG:
2796 {
2797 tree expression;
2798 tree type;
2799
2800 /* The `__builtin_va_arg' construct is used to handle
2801 `va_arg'. Consume the `__builtin_va_arg' token. */
2802 cp_lexer_consume_token (parser->lexer);
2803 /* Look for the opening `('. */
2804 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2805 /* Now, parse the assignment-expression. */
2806 expression = cp_parser_assignment_expression (parser);
2807 /* Look for the `,'. */
2808 cp_parser_require (parser, CPP_COMMA, "`,'");
2809 /* Parse the type-id. */
2810 type = cp_parser_type_id (parser);
2811 /* Look for the closing `)'. */
2812 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14d22dd6
MM
2813 /* Using `va_arg' in a constant-expression is not
2814 allowed. */
625cbf93
MM
2815 if (cp_parser_non_integral_constant_expression (parser,
2816 "`va_arg'"))
2817 return error_mark_node;
a723baf1
MM
2818 return build_x_va_arg (expression, type);
2819 }
2820
263ee052 2821 case RID_OFFSETOF:
7a3ea201 2822 return cp_parser_builtin_offsetof (parser);
263ee052 2823
a723baf1
MM
2824 default:
2825 cp_parser_error (parser, "expected primary-expression");
2826 return error_mark_node;
2827 }
a723baf1
MM
2828
2829 /* An id-expression can start with either an identifier, a
2830 `::' as the beginning of a qualified-id, or the "operator"
2831 keyword. */
2832 case CPP_NAME:
2833 case CPP_SCOPE:
2834 case CPP_TEMPLATE_ID:
2835 case CPP_NESTED_NAME_SPECIFIER:
2836 {
2837 tree id_expression;
2838 tree decl;
b3445994 2839 const char *error_msg;
a723baf1
MM
2840
2841 id_expression:
2842 /* Parse the id-expression. */
21526606
EC
2843 id_expression
2844 = cp_parser_id_expression (parser,
a723baf1
MM
2845 /*template_keyword_p=*/false,
2846 /*check_dependency_p=*/true,
f3c2dfc6
MM
2847 /*template_p=*/NULL,
2848 /*declarator_p=*/false);
a723baf1
MM
2849 if (id_expression == error_mark_node)
2850 return error_mark_node;
2851 /* If we have a template-id, then no further lookup is
2852 required. If the template-id was for a template-class, we
2853 will sometimes have a TYPE_DECL at this point. */
2854 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2855 || TREE_CODE (id_expression) == TYPE_DECL)
2856 decl = id_expression;
2857 /* Look up the name. */
21526606 2858 else
a723baf1 2859 {
8f78f01f
MM
2860 bool ambiguous_p;
2861
2862 decl = cp_parser_lookup_name (parser, id_expression,
fc6a28d7 2863 none_type,
8f78f01f
MM
2864 /*is_template=*/false,
2865 /*is_namespace=*/false,
2866 /*check_dependency=*/true,
2867 &ambiguous_p);
2868 /* If the lookup was ambiguous, an error will already have
2869 been issued. */
2870 if (ambiguous_p)
2871 return error_mark_node;
a723baf1
MM
2872 /* If name lookup gives us a SCOPE_REF, then the
2873 qualifying scope was dependent. Just propagate the
2874 name. */
2875 if (TREE_CODE (decl) == SCOPE_REF)
2876 {
2877 if (TYPE_P (TREE_OPERAND (decl, 0)))
2878 *qualifying_class = TREE_OPERAND (decl, 0);
2879 return decl;
2880 }
2881 /* Check to see if DECL is a local variable in a context
2882 where that is forbidden. */
2883 if (parser->local_variables_forbidden_p
2884 && local_variable_p (decl))
2885 {
2886 /* It might be that we only found DECL because we are
2887 trying to be generous with pre-ISO scoping rules.
2888 For example, consider:
2889
2890 int i;
2891 void g() {
2892 for (int i = 0; i < 10; ++i) {}
2893 extern void f(int j = i);
2894 }
2895
21526606 2896 Here, name look up will originally find the out
a723baf1
MM
2897 of scope `i'. We need to issue a warning message,
2898 but then use the global `i'. */
2899 decl = check_for_out_of_scope_variable (decl);
2900 if (local_variable_p (decl))
2901 {
2a13a625 2902 error ("local variable %qD may not appear in this context",
a723baf1
MM
2903 decl);
2904 return error_mark_node;
2905 }
2906 }
c006d942 2907 }
21526606
EC
2908
2909 decl = finish_id_expression (id_expression, decl, parser->scope,
b3445994 2910 idk, qualifying_class,
67c03833
JM
2911 parser->integral_constant_expression_p,
2912 parser->allow_non_integral_constant_expression_p,
2913 &parser->non_integral_constant_expression_p,
b3445994
MM
2914 &error_msg);
2915 if (error_msg)
2916 cp_parser_error (parser, error_msg);
a723baf1
MM
2917 return decl;
2918 }
2919
2920 /* Anything else is an error. */
2921 default:
2922 cp_parser_error (parser, "expected primary-expression");
2923 return error_mark_node;
2924 }
2925}
2926
2927/* Parse an id-expression.
2928
2929 id-expression:
2930 unqualified-id
2931 qualified-id
2932
2933 qualified-id:
2934 :: [opt] nested-name-specifier template [opt] unqualified-id
2935 :: identifier
2936 :: operator-function-id
2937 :: template-id
2938
2939 Return a representation of the unqualified portion of the
2940 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2941 a `::' or nested-name-specifier.
2942
2943 Often, if the id-expression was a qualified-id, the caller will
2944 want to make a SCOPE_REF to represent the qualified-id. This
2945 function does not do this in order to avoid wastefully creating
2946 SCOPE_REFs when they are not required.
2947
a723baf1
MM
2948 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2949 `template' keyword.
2950
2951 If CHECK_DEPENDENCY_P is false, then names are looked up inside
21526606 2952 uninstantiated templates.
a723baf1 2953
15d2cb19 2954 If *TEMPLATE_P is non-NULL, it is set to true iff the
a723baf1 2955 `template' keyword is used to explicitly indicate that the entity
21526606 2956 named is a template.
f3c2dfc6
MM
2957
2958 If DECLARATOR_P is true, the id-expression is appearing as part of
cd0be382 2959 a declarator, rather than as part of an expression. */
a723baf1
MM
2960
2961static tree
2962cp_parser_id_expression (cp_parser *parser,
2963 bool template_keyword_p,
2964 bool check_dependency_p,
f3c2dfc6
MM
2965 bool *template_p,
2966 bool declarator_p)
a723baf1
MM
2967{
2968 bool global_scope_p;
2969 bool nested_name_specifier_p;
2970
2971 /* Assume the `template' keyword was not used. */
2972 if (template_p)
2973 *template_p = false;
2974
2975 /* Look for the optional `::' operator. */
21526606
EC
2976 global_scope_p
2977 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
a723baf1
MM
2978 != NULL_TREE);
2979 /* Look for the optional nested-name-specifier. */
21526606 2980 nested_name_specifier_p
a723baf1
MM
2981 = (cp_parser_nested_name_specifier_opt (parser,
2982 /*typename_keyword_p=*/false,
2983 check_dependency_p,
a668c6ad 2984 /*type_p=*/false,
a52eb3bc 2985 declarator_p)
a723baf1
MM
2986 != NULL_TREE);
2987 /* If there is a nested-name-specifier, then we are looking at
2988 the first qualified-id production. */
2989 if (nested_name_specifier_p)
2990 {
2991 tree saved_scope;
2992 tree saved_object_scope;
2993 tree saved_qualifying_scope;
2994 tree unqualified_id;
2995 bool is_template;
2996
2997 /* See if the next token is the `template' keyword. */
2998 if (!template_p)
2999 template_p = &is_template;
3000 *template_p = cp_parser_optional_template_keyword (parser);
3001 /* Name lookup we do during the processing of the
3002 unqualified-id might obliterate SCOPE. */
3003 saved_scope = parser->scope;
3004 saved_object_scope = parser->object_scope;
3005 saved_qualifying_scope = parser->qualifying_scope;
3006 /* Process the final unqualified-id. */
3007 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
f3c2dfc6
MM
3008 check_dependency_p,
3009 declarator_p);
a723baf1
MM
3010 /* Restore the SAVED_SCOPE for our caller. */
3011 parser->scope = saved_scope;
3012 parser->object_scope = saved_object_scope;
3013 parser->qualifying_scope = saved_qualifying_scope;
3014
3015 return unqualified_id;
3016 }
3017 /* Otherwise, if we are in global scope, then we are looking at one
3018 of the other qualified-id productions. */
3019 else if (global_scope_p)
3020 {
3021 cp_token *token;
3022 tree id;
3023
e5976695
MM
3024 /* Peek at the next token. */
3025 token = cp_lexer_peek_token (parser->lexer);
3026
3027 /* If it's an identifier, and the next token is not a "<", then
3028 we can avoid the template-id case. This is an optimization
3029 for this common case. */
21526606
EC
3030 if (token->type == CPP_NAME
3031 && !cp_parser_nth_token_starts_template_argument_list_p
f4abade9 3032 (parser, 2))
e5976695
MM
3033 return cp_parser_identifier (parser);
3034
a723baf1
MM
3035 cp_parser_parse_tentatively (parser);
3036 /* Try a template-id. */
21526606 3037 id = cp_parser_template_id (parser,
a723baf1 3038 /*template_keyword_p=*/false,
a668c6ad
MM
3039 /*check_dependency_p=*/true,
3040 declarator_p);
a723baf1
MM
3041 /* If that worked, we're done. */
3042 if (cp_parser_parse_definitely (parser))
3043 return id;
3044
e5976695
MM
3045 /* Peek at the next token. (Changes in the token buffer may
3046 have invalidated the pointer obtained above.) */
a723baf1
MM
3047 token = cp_lexer_peek_token (parser->lexer);
3048
3049 switch (token->type)
3050 {
3051 case CPP_NAME:
3052 return cp_parser_identifier (parser);
3053
3054 case CPP_KEYWORD:
3055 if (token->keyword == RID_OPERATOR)
3056 return cp_parser_operator_function_id (parser);
3057 /* Fall through. */
21526606 3058
a723baf1
MM
3059 default:
3060 cp_parser_error (parser, "expected id-expression");
3061 return error_mark_node;
3062 }
3063 }
3064 else
3065 return cp_parser_unqualified_id (parser, template_keyword_p,
f3c2dfc6
MM
3066 /*check_dependency_p=*/true,
3067 declarator_p);
a723baf1
MM
3068}
3069
3070/* Parse an unqualified-id.
3071
3072 unqualified-id:
3073 identifier
3074 operator-function-id
3075 conversion-function-id
3076 ~ class-name
3077 template-id
3078
3079 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3080 keyword, in a construct like `A::template ...'.
3081
3082 Returns a representation of unqualified-id. For the `identifier'
3083 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3084 production a BIT_NOT_EXPR is returned; the operand of the
3085 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3086 other productions, see the documentation accompanying the
3087 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
f3c2dfc6
MM
3088 names are looked up in uninstantiated templates. If DECLARATOR_P
3089 is true, the unqualified-id is appearing as part of a declarator,
3090 rather than as part of an expression. */
a723baf1
MM
3091
3092static tree
21526606 3093cp_parser_unqualified_id (cp_parser* parser,
94edc4ab 3094 bool template_keyword_p,
f3c2dfc6
MM
3095 bool check_dependency_p,
3096 bool declarator_p)
a723baf1
MM
3097{
3098 cp_token *token;
3099
3100 /* Peek at the next token. */
3101 token = cp_lexer_peek_token (parser->lexer);
21526606 3102
a723baf1
MM
3103 switch (token->type)
3104 {
3105 case CPP_NAME:
3106 {
3107 tree id;
3108
3109 /* We don't know yet whether or not this will be a
3110 template-id. */
3111 cp_parser_parse_tentatively (parser);
3112 /* Try a template-id. */
3113 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3114 check_dependency_p,
3115 declarator_p);
a723baf1
MM
3116 /* If it worked, we're done. */
3117 if (cp_parser_parse_definitely (parser))
3118 return id;
3119 /* Otherwise, it's an ordinary identifier. */
3120 return cp_parser_identifier (parser);
3121 }
3122
3123 case CPP_TEMPLATE_ID:
3124 return cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3125 check_dependency_p,
3126 declarator_p);
a723baf1
MM
3127
3128 case CPP_COMPL:
3129 {
3130 tree type_decl;
3131 tree qualifying_scope;
3132 tree object_scope;
3133 tree scope;
3134
3135 /* Consume the `~' token. */
3136 cp_lexer_consume_token (parser->lexer);
3137 /* Parse the class-name. The standard, as written, seems to
3138 say that:
3139
3140 template <typename T> struct S { ~S (); };
3141 template <typename T> S<T>::~S() {}
3142
3143 is invalid, since `~' must be followed by a class-name, but
3144 `S<T>' is dependent, and so not known to be a class.
3145 That's not right; we need to look in uninstantiated
3146 templates. A further complication arises from:
3147
3148 template <typename T> void f(T t) {
3149 t.T::~T();
21526606 3150 }
a723baf1
MM
3151
3152 Here, it is not possible to look up `T' in the scope of `T'
3153 itself. We must look in both the current scope, and the
21526606 3154 scope of the containing complete expression.
a723baf1
MM
3155
3156 Yet another issue is:
3157
3158 struct S {
3159 int S;
3160 ~S();
3161 };
3162
3163 S::~S() {}
3164
3165 The standard does not seem to say that the `S' in `~S'
3166 should refer to the type `S' and not the data member
3167 `S::S'. */
3168
3169 /* DR 244 says that we look up the name after the "~" in the
3170 same scope as we looked up the qualifying name. That idea
3171 isn't fully worked out; it's more complicated than that. */
3172 scope = parser->scope;
3173 object_scope = parser->object_scope;
3174 qualifying_scope = parser->qualifying_scope;
3175
3176 /* If the name is of the form "X::~X" it's OK. */
3177 if (scope && TYPE_P (scope)
3178 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 3179 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1 3180 == CPP_OPEN_PAREN)
21526606 3181 && (cp_lexer_peek_token (parser->lexer)->value
a723baf1
MM
3182 == TYPE_IDENTIFIER (scope)))
3183 {
3184 cp_lexer_consume_token (parser->lexer);
3185 return build_nt (BIT_NOT_EXPR, scope);
3186 }
3187
3188 /* If there was an explicit qualification (S::~T), first look
3189 in the scope given by the qualification (i.e., S). */
3190 if (scope)
3191 {
3192 cp_parser_parse_tentatively (parser);
21526606 3193 type_decl = cp_parser_class_name (parser,
a723baf1
MM
3194 /*typename_keyword_p=*/false,
3195 /*template_keyword_p=*/false,
fc6a28d7 3196 none_type,
a723baf1 3197 /*check_dependency=*/false,
a668c6ad
MM
3198 /*class_head_p=*/false,
3199 declarator_p);
a723baf1
MM
3200 if (cp_parser_parse_definitely (parser))
3201 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3202 }
3203 /* In "N::S::~S", look in "N" as well. */
3204 if (scope && qualifying_scope)
3205 {
3206 cp_parser_parse_tentatively (parser);
3207 parser->scope = qualifying_scope;
3208 parser->object_scope = NULL_TREE;
3209 parser->qualifying_scope = NULL_TREE;
21526606
EC
3210 type_decl
3211 = cp_parser_class_name (parser,
a723baf1
MM
3212 /*typename_keyword_p=*/false,
3213 /*template_keyword_p=*/false,
fc6a28d7 3214 none_type,
a723baf1 3215 /*check_dependency=*/false,
a668c6ad
MM
3216 /*class_head_p=*/false,
3217 declarator_p);
a723baf1
MM
3218 if (cp_parser_parse_definitely (parser))
3219 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3220 }
3221 /* In "p->S::~T", look in the scope given by "*p" as well. */
3222 else if (object_scope)
3223 {
3224 cp_parser_parse_tentatively (parser);
3225 parser->scope = object_scope;
3226 parser->object_scope = NULL_TREE;
3227 parser->qualifying_scope = NULL_TREE;
21526606
EC
3228 type_decl
3229 = cp_parser_class_name (parser,
a723baf1
MM
3230 /*typename_keyword_p=*/false,
3231 /*template_keyword_p=*/false,
fc6a28d7 3232 none_type,
a723baf1 3233 /*check_dependency=*/false,
a668c6ad
MM
3234 /*class_head_p=*/false,
3235 declarator_p);
a723baf1
MM
3236 if (cp_parser_parse_definitely (parser))
3237 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3238 }
3239 /* Look in the surrounding context. */
3240 parser->scope = NULL_TREE;
3241 parser->object_scope = NULL_TREE;
3242 parser->qualifying_scope = NULL_TREE;
21526606
EC
3243 type_decl
3244 = cp_parser_class_name (parser,
a723baf1
MM
3245 /*typename_keyword_p=*/false,
3246 /*template_keyword_p=*/false,
fc6a28d7 3247 none_type,
a723baf1 3248 /*check_dependency=*/false,
a668c6ad
MM
3249 /*class_head_p=*/false,
3250 declarator_p);
a723baf1
MM
3251 /* If an error occurred, assume that the name of the
3252 destructor is the same as the name of the qualifying
3253 class. That allows us to keep parsing after running
3254 into ill-formed destructor names. */
3255 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3256 return build_nt (BIT_NOT_EXPR, scope);
3257 else if (type_decl == error_mark_node)
3258 return error_mark_node;
3259
f3c2dfc6
MM
3260 /* [class.dtor]
3261
3262 A typedef-name that names a class shall not be used as the
3263 identifier in the declarator for a destructor declaration. */
21526606 3264 if (declarator_p
f3c2dfc6 3265 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
33a69702
VR
3266 && !DECL_SELF_REFERENCE_P (type_decl)
3267 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
2a13a625 3268 error ("typedef-name %qD used as destructor declarator",
f3c2dfc6
MM
3269 type_decl);
3270
a723baf1
MM
3271 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3272 }
3273
3274 case CPP_KEYWORD:
3275 if (token->keyword == RID_OPERATOR)
3276 {
3277 tree id;
3278
3279 /* This could be a template-id, so we try that first. */
3280 cp_parser_parse_tentatively (parser);
3281 /* Try a template-id. */
3282 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3283 /*check_dependency_p=*/true,
3284 declarator_p);
a723baf1
MM
3285 /* If that worked, we're done. */
3286 if (cp_parser_parse_definitely (parser))
3287 return id;
3288 /* We still don't know whether we're looking at an
3289 operator-function-id or a conversion-function-id. */
3290 cp_parser_parse_tentatively (parser);
3291 /* Try an operator-function-id. */
3292 id = cp_parser_operator_function_id (parser);
3293 /* If that didn't work, try a conversion-function-id. */
3294 if (!cp_parser_parse_definitely (parser))
3295 id = cp_parser_conversion_function_id (parser);
3296
3297 return id;
3298 }
3299 /* Fall through. */
3300
3301 default:
3302 cp_parser_error (parser, "expected unqualified-id");
3303 return error_mark_node;
3304 }
3305}
3306
3307/* Parse an (optional) nested-name-specifier.
3308
3309 nested-name-specifier:
3310 class-or-namespace-name :: nested-name-specifier [opt]
3311 class-or-namespace-name :: template nested-name-specifier [opt]
3312
3313 PARSER->SCOPE should be set appropriately before this function is
3314 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3315 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3316 in name lookups.
3317
3318 Sets PARSER->SCOPE to the class (TYPE) or namespace
3319 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3320 it unchanged if there is no nested-name-specifier. Returns the new
21526606 3321 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
a668c6ad
MM
3322
3323 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3324 part of a declaration and/or decl-specifier. */
a723baf1
MM
3325
3326static tree
21526606
EC
3327cp_parser_nested_name_specifier_opt (cp_parser *parser,
3328 bool typename_keyword_p,
a723baf1 3329 bool check_dependency_p,
a668c6ad
MM
3330 bool type_p,
3331 bool is_declaration)
a723baf1
MM
3332{
3333 bool success = false;
3334 tree access_check = NULL_TREE;
0c5e4866
NS
3335 cp_token_position start = 0;
3336 cp_token *token;
a723baf1
MM
3337
3338 /* If the next token corresponds to a nested name specifier, there
2050a1bb 3339 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
21526606 3340 false, it may have been true before, in which case something
2050a1bb
MM
3341 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3342 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3343 CHECK_DEPENDENCY_P is false, we have to fall through into the
3344 main loop. */
3345 if (check_dependency_p
3346 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3347 {
3348 cp_parser_pre_parsed_nested_name_specifier (parser);
a723baf1
MM
3349 return parser->scope;
3350 }
3351
3352 /* Remember where the nested-name-specifier starts. */
0b16f8f4 3353 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
0c5e4866 3354 start = cp_lexer_token_position (parser->lexer, false);
a723baf1 3355
8d241e0b 3356 push_deferring_access_checks (dk_deferred);
cf22909c 3357
a723baf1
MM
3358 while (true)
3359 {
3360 tree new_scope;
3361 tree old_scope;
3362 tree saved_qualifying_scope;
a723baf1
MM
3363 bool template_keyword_p;
3364
2050a1bb
MM
3365 /* Spot cases that cannot be the beginning of a
3366 nested-name-specifier. */
3367 token = cp_lexer_peek_token (parser->lexer);
3368
3369 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3370 the already parsed nested-name-specifier. */
3371 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3372 {
3373 /* Grab the nested-name-specifier and continue the loop. */
3374 cp_parser_pre_parsed_nested_name_specifier (parser);
3375 success = true;
3376 continue;
3377 }
3378
a723baf1
MM
3379 /* Spot cases that cannot be the beginning of a
3380 nested-name-specifier. On the second and subsequent times
3381 through the loop, we look for the `template' keyword. */
f7b5ecd9 3382 if (success && token->keyword == RID_TEMPLATE)
a723baf1
MM
3383 ;
3384 /* A template-id can start a nested-name-specifier. */
f7b5ecd9 3385 else if (token->type == CPP_TEMPLATE_ID)
a723baf1
MM
3386 ;
3387 else
3388 {
3389 /* If the next token is not an identifier, then it is
3390 definitely not a class-or-namespace-name. */
f7b5ecd9 3391 if (token->type != CPP_NAME)
a723baf1
MM
3392 break;
3393 /* If the following token is neither a `<' (to begin a
3394 template-id), nor a `::', then we are not looking at a
3395 nested-name-specifier. */
3396 token = cp_lexer_peek_nth_token (parser->lexer, 2);
f4abade9
GB
3397 if (token->type != CPP_SCOPE
3398 && !cp_parser_nth_token_starts_template_argument_list_p
3399 (parser, 2))
a723baf1
MM
3400 break;
3401 }
3402
3403 /* The nested-name-specifier is optional, so we parse
3404 tentatively. */
3405 cp_parser_parse_tentatively (parser);
3406
3407 /* Look for the optional `template' keyword, if this isn't the
3408 first time through the loop. */
3409 if (success)
3410 template_keyword_p = cp_parser_optional_template_keyword (parser);
3411 else
3412 template_keyword_p = false;
3413
3414 /* Save the old scope since the name lookup we are about to do
3415 might destroy it. */
3416 old_scope = parser->scope;
3417 saved_qualifying_scope = parser->qualifying_scope;
a52eb3bc
MM
3418 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3419 look up names in "X<T>::I" in order to determine that "Y" is
3420 a template. So, if we have a typename at this point, we make
3421 an effort to look through it. */
67bcc252
MM
3422 if (is_declaration
3423 && !typename_keyword_p
3424 && parser->scope
a52eb3bc
MM
3425 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3426 parser->scope = resolve_typename_type (parser->scope,
3427 /*only_current_p=*/false);
a723baf1 3428 /* Parse the qualifying entity. */
21526606 3429 new_scope
a723baf1
MM
3430 = cp_parser_class_or_namespace_name (parser,
3431 typename_keyword_p,
3432 template_keyword_p,
3433 check_dependency_p,
a668c6ad
MM
3434 type_p,
3435 is_declaration);
a723baf1
MM
3436 /* Look for the `::' token. */
3437 cp_parser_require (parser, CPP_SCOPE, "`::'");
3438
3439 /* If we found what we wanted, we keep going; otherwise, we're
3440 done. */
3441 if (!cp_parser_parse_definitely (parser))
3442 {
3443 bool error_p = false;
3444
3445 /* Restore the OLD_SCOPE since it was valid before the
3446 failed attempt at finding the last
3447 class-or-namespace-name. */
3448 parser->scope = old_scope;
3449 parser->qualifying_scope = saved_qualifying_scope;
3450 /* If the next token is an identifier, and the one after
3451 that is a `::', then any valid interpretation would have
3452 found a class-or-namespace-name. */
3453 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 3454 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1 3455 == CPP_SCOPE)
21526606 3456 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
a723baf1
MM
3457 != CPP_COMPL))
3458 {
3459 token = cp_lexer_consume_token (parser->lexer);
21526606 3460 if (!error_p)
a723baf1
MM
3461 {
3462 tree decl;
3463
3464 decl = cp_parser_lookup_name_simple (parser, token->value);
3465 if (TREE_CODE (decl) == TEMPLATE_DECL)
2a13a625 3466 error ("%qD used without template parameters", decl);
a723baf1 3467 else
21526606
EC
3468 cp_parser_name_lookup_error
3469 (parser, token->value, decl,
4bb8ca28 3470 "is not a class or namespace");
a723baf1
MM
3471 parser->scope = NULL_TREE;
3472 error_p = true;
eea9800f
MM
3473 /* Treat this as a successful nested-name-specifier
3474 due to:
3475
3476 [basic.lookup.qual]
3477
3478 If the name found is not a class-name (clause
3479 _class_) or namespace-name (_namespace.def_), the
3480 program is ill-formed. */
3481 success = true;
a723baf1
MM
3482 }
3483 cp_lexer_consume_token (parser->lexer);
3484 }
3485 break;
3486 }
3487
3488 /* We've found one valid nested-name-specifier. */
3489 success = true;
3490 /* Make sure we look in the right scope the next time through
3491 the loop. */
21526606 3492 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
a723baf1
MM
3493 ? TREE_TYPE (new_scope)
3494 : new_scope);
3495 /* If it is a class scope, try to complete it; we are about to
3496 be looking up names inside the class. */
8fbc5ae7
MM
3497 if (TYPE_P (parser->scope)
3498 /* Since checking types for dependency can be expensive,
3499 avoid doing it if the type is already complete. */
3500 && !COMPLETE_TYPE_P (parser->scope)
3501 /* Do not try to complete dependent types. */
1fb3244a 3502 && !dependent_type_p (parser->scope))
a723baf1
MM
3503 complete_type (parser->scope);
3504 }
3505
cf22909c
KL
3506 /* Retrieve any deferred checks. Do not pop this access checks yet
3507 so the memory will not be reclaimed during token replacing below. */
3508 access_check = get_deferred_access_checks ();
3509
a723baf1
MM
3510 /* If parsing tentatively, replace the sequence of tokens that makes
3511 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3512 token. That way, should we re-parse the token stream, we will
3513 not have to repeat the effort required to do the parse, nor will
3514 we issue duplicate error messages. */
0c5e4866 3515 if (success && start)
a723baf1 3516 {
0c5e4866
NS
3517 cp_token *token = cp_lexer_token_at (parser->lexer, start);
3518
a723baf1
MM
3519 /* Reset the contents of the START token. */
3520 token->type = CPP_NESTED_NAME_SPECIFIER;
3521 token->value = build_tree_list (access_check, parser->scope);
3522 TREE_TYPE (token->value) = parser->qualifying_scope;
3523 token->keyword = RID_MAX;
0c5e4866 3524
a723baf1 3525 /* Purge all subsequent tokens. */
0c5e4866 3526 cp_lexer_purge_tokens_after (parser->lexer, start);
a723baf1
MM
3527 }
3528
cf22909c 3529 pop_deferring_access_checks ();
a723baf1
MM
3530 return success ? parser->scope : NULL_TREE;
3531}
3532
3533/* Parse a nested-name-specifier. See
3534 cp_parser_nested_name_specifier_opt for details. This function
3535 behaves identically, except that it will an issue an error if no
3536 nested-name-specifier is present, and it will return
3537 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3538 is present. */
3539
3540static tree
21526606
EC
3541cp_parser_nested_name_specifier (cp_parser *parser,
3542 bool typename_keyword_p,
a723baf1 3543 bool check_dependency_p,
a668c6ad
MM
3544 bool type_p,
3545 bool is_declaration)
a723baf1
MM
3546{
3547 tree scope;
3548
3549 /* Look for the nested-name-specifier. */
3550 scope = cp_parser_nested_name_specifier_opt (parser,
3551 typename_keyword_p,
3552 check_dependency_p,
a668c6ad
MM
3553 type_p,
3554 is_declaration);
a723baf1
MM
3555 /* If it was not present, issue an error message. */
3556 if (!scope)
3557 {
3558 cp_parser_error (parser, "expected nested-name-specifier");
eb5abb39 3559 parser->scope = NULL_TREE;
a723baf1
MM
3560 return error_mark_node;
3561 }
3562
3563 return scope;
3564}
3565
3566/* Parse a class-or-namespace-name.
3567
3568 class-or-namespace-name:
3569 class-name
3570 namespace-name
3571
3572 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3573 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3574 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3575 TYPE_P is TRUE iff the next name should be taken as a class-name,
3576 even the same name is declared to be another entity in the same
3577 scope.
3578
3579 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
eea9800f
MM
3580 specified by the class-or-namespace-name. If neither is found the
3581 ERROR_MARK_NODE is returned. */
a723baf1
MM
3582
3583static tree
21526606 3584cp_parser_class_or_namespace_name (cp_parser *parser,
a723baf1
MM
3585 bool typename_keyword_p,
3586 bool template_keyword_p,
3587 bool check_dependency_p,
a668c6ad
MM
3588 bool type_p,
3589 bool is_declaration)
a723baf1
MM
3590{
3591 tree saved_scope;
3592 tree saved_qualifying_scope;
3593 tree saved_object_scope;
3594 tree scope;
eea9800f 3595 bool only_class_p;
a723baf1 3596
a723baf1
MM
3597 /* Before we try to parse the class-name, we must save away the
3598 current PARSER->SCOPE since cp_parser_class_name will destroy
3599 it. */
3600 saved_scope = parser->scope;
3601 saved_qualifying_scope = parser->qualifying_scope;
3602 saved_object_scope = parser->object_scope;
eea9800f
MM
3603 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3604 there is no need to look for a namespace-name. */
bbaab916 3605 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
eea9800f
MM
3606 if (!only_class_p)
3607 cp_parser_parse_tentatively (parser);
21526606 3608 scope = cp_parser_class_name (parser,
a723baf1
MM
3609 typename_keyword_p,
3610 template_keyword_p,
fc6a28d7 3611 type_p ? class_type : none_type,
a723baf1 3612 check_dependency_p,
a668c6ad
MM
3613 /*class_head_p=*/false,
3614 is_declaration);
a723baf1 3615 /* If that didn't work, try for a namespace-name. */
eea9800f 3616 if (!only_class_p && !cp_parser_parse_definitely (parser))
a723baf1
MM
3617 {
3618 /* Restore the saved scope. */
3619 parser->scope = saved_scope;
3620 parser->qualifying_scope = saved_qualifying_scope;
3621 parser->object_scope = saved_object_scope;
eea9800f
MM
3622 /* If we are not looking at an identifier followed by the scope
3623 resolution operator, then this is not part of a
3624 nested-name-specifier. (Note that this function is only used
3625 to parse the components of a nested-name-specifier.) */
3626 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3627 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3628 return error_mark_node;
a723baf1
MM
3629 scope = cp_parser_namespace_name (parser);
3630 }
3631
3632 return scope;
3633}
3634
3635/* Parse a postfix-expression.
3636
3637 postfix-expression:
3638 primary-expression
3639 postfix-expression [ expression ]
3640 postfix-expression ( expression-list [opt] )
3641 simple-type-specifier ( expression-list [opt] )
21526606 3642 typename :: [opt] nested-name-specifier identifier
a723baf1
MM
3643 ( expression-list [opt] )
3644 typename :: [opt] nested-name-specifier template [opt] template-id
3645 ( expression-list [opt] )
3646 postfix-expression . template [opt] id-expression
3647 postfix-expression -> template [opt] id-expression
3648 postfix-expression . pseudo-destructor-name
3649 postfix-expression -> pseudo-destructor-name
3650 postfix-expression ++
3651 postfix-expression --
3652 dynamic_cast < type-id > ( expression )
3653 static_cast < type-id > ( expression )
3654 reinterpret_cast < type-id > ( expression )
3655 const_cast < type-id > ( expression )
3656 typeid ( expression )
3657 typeid ( type-id )
3658
3659 GNU Extension:
21526606 3660
a723baf1
MM
3661 postfix-expression:
3662 ( type-id ) { initializer-list , [opt] }
3663
3664 This extension is a GNU version of the C99 compound-literal
3665 construct. (The C99 grammar uses `type-name' instead of `type-id',
3666 but they are essentially the same concept.)
3667
3668 If ADDRESS_P is true, the postfix expression is the operand of the
3669 `&' operator.
3670
3671 Returns a representation of the expression. */
3672
3673static tree
3674cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3675{
3676 cp_token *token;
3677 enum rid keyword;
b3445994 3678 cp_id_kind idk = CP_ID_KIND_NONE;
a723baf1
MM
3679 tree postfix_expression = NULL_TREE;
3680 /* Non-NULL only if the current postfix-expression can be used to
3681 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3682 class used to qualify the member. */
3683 tree qualifying_class = NULL_TREE;
a723baf1
MM
3684
3685 /* Peek at the next token. */
3686 token = cp_lexer_peek_token (parser->lexer);
3687 /* Some of the productions are determined by keywords. */
3688 keyword = token->keyword;
3689 switch (keyword)
3690 {
3691 case RID_DYNCAST:
3692 case RID_STATCAST:
3693 case RID_REINTCAST:
3694 case RID_CONSTCAST:
3695 {
3696 tree type;
3697 tree expression;
3698 const char *saved_message;
3699
3700 /* All of these can be handled in the same way from the point
3701 of view of parsing. Begin by consuming the token
3702 identifying the cast. */
3703 cp_lexer_consume_token (parser->lexer);
21526606 3704
a723baf1
MM
3705 /* New types cannot be defined in the cast. */
3706 saved_message = parser->type_definition_forbidden_message;
3707 parser->type_definition_forbidden_message
3708 = "types may not be defined in casts";
3709
3710 /* Look for the opening `<'. */
3711 cp_parser_require (parser, CPP_LESS, "`<'");
3712 /* Parse the type to which we are casting. */
3713 type = cp_parser_type_id (parser);
3714 /* Look for the closing `>'. */
3715 cp_parser_require (parser, CPP_GREATER, "`>'");
3716 /* Restore the old message. */
3717 parser->type_definition_forbidden_message = saved_message;
3718
3719 /* And the expression which is being cast. */
3720 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3721 expression = cp_parser_expression (parser);
3722 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3723
14d22dd6
MM
3724 /* Only type conversions to integral or enumeration types
3725 can be used in constant-expressions. */
67c03833 3726 if (parser->integral_constant_expression_p
14d22dd6 3727 && !dependent_type_p (type)
263ee052 3728 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
98ca843c 3729 && (cp_parser_non_integral_constant_expression
625cbf93
MM
3730 (parser,
3731 "a cast to a type other than an integral or "
3732 "enumeration type")))
3733 return error_mark_node;
14d22dd6 3734
a723baf1
MM
3735 switch (keyword)
3736 {
3737 case RID_DYNCAST:
3738 postfix_expression
3739 = build_dynamic_cast (type, expression);
3740 break;
3741 case RID_STATCAST:
3742 postfix_expression
3743 = build_static_cast (type, expression);
3744 break;
3745 case RID_REINTCAST:
3746 postfix_expression
3747 = build_reinterpret_cast (type, expression);
3748 break;
3749 case RID_CONSTCAST:
3750 postfix_expression
3751 = build_const_cast (type, expression);
3752 break;
3753 default:
315fb5db 3754 gcc_unreachable ();
a723baf1
MM
3755 }
3756 }
3757 break;
3758
3759 case RID_TYPEID:
3760 {
3761 tree type;
3762 const char *saved_message;
4f8163b1 3763 bool saved_in_type_id_in_expr_p;
a723baf1
MM
3764
3765 /* Consume the `typeid' token. */
3766 cp_lexer_consume_token (parser->lexer);
3767 /* Look for the `(' token. */
3768 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3769 /* Types cannot be defined in a `typeid' expression. */
3770 saved_message = parser->type_definition_forbidden_message;
3771 parser->type_definition_forbidden_message
3772 = "types may not be defined in a `typeid\' expression";
3773 /* We can't be sure yet whether we're looking at a type-id or an
3774 expression. */
3775 cp_parser_parse_tentatively (parser);
3776 /* Try a type-id first. */
4f8163b1
MM
3777 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3778 parser->in_type_id_in_expr_p = true;
a723baf1 3779 type = cp_parser_type_id (parser);
4f8163b1 3780 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
3781 /* Look for the `)' token. Otherwise, we can't be sure that
3782 we're not looking at an expression: consider `typeid (int
3783 (3))', for example. */
3784 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3785 /* If all went well, simply lookup the type-id. */
3786 if (cp_parser_parse_definitely (parser))
3787 postfix_expression = get_typeid (type);
3788 /* Otherwise, fall back to the expression variant. */
3789 else
3790 {
3791 tree expression;
3792
3793 /* Look for an expression. */
3794 expression = cp_parser_expression (parser);
3795 /* Compute its typeid. */
3796 postfix_expression = build_typeid (expression);
3797 /* Look for the `)' token. */
3798 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3799 }
4424e0da 3800 /* `typeid' may not appear in an integral constant expression. */
98ca843c 3801 if (cp_parser_non_integral_constant_expression(parser,
4424e0da
GB
3802 "`typeid' operator"))
3803 return error_mark_node;
a723baf1
MM
3804 /* Restore the saved message. */
3805 parser->type_definition_forbidden_message = saved_message;
3806 }
3807 break;
21526606 3808
a723baf1
MM
3809 case RID_TYPENAME:
3810 {
3811 bool template_p = false;
3812 tree id;
3813 tree type;
3814
3815 /* Consume the `typename' token. */
3816 cp_lexer_consume_token (parser->lexer);
3817 /* Look for the optional `::' operator. */
21526606 3818 cp_parser_global_scope_opt (parser,
a723baf1
MM
3819 /*current_scope_valid_p=*/false);
3820 /* Look for the nested-name-specifier. */
3821 cp_parser_nested_name_specifier (parser,
3822 /*typename_keyword_p=*/true,
3823 /*check_dependency_p=*/true,
a668c6ad
MM
3824 /*type_p=*/true,
3825 /*is_declaration=*/true);
a723baf1
MM
3826 /* Look for the optional `template' keyword. */
3827 template_p = cp_parser_optional_template_keyword (parser);
3828 /* We don't know whether we're looking at a template-id or an
3829 identifier. */
3830 cp_parser_parse_tentatively (parser);
3831 /* Try a template-id. */
3832 id = cp_parser_template_id (parser, template_p,
a668c6ad
MM
3833 /*check_dependency_p=*/true,
3834 /*is_declaration=*/true);
a723baf1
MM
3835 /* If that didn't work, try an identifier. */
3836 if (!cp_parser_parse_definitely (parser))
3837 id = cp_parser_identifier (parser);
26bcf8fc
MM
3838 /* If we look up a template-id in a non-dependent qualifying
3839 scope, there's no need to create a dependent type. */
3840 if (TREE_CODE (id) == TYPE_DECL
3841 && !dependent_type_p (parser->scope))
3842 type = TREE_TYPE (id);
a723baf1
MM
3843 /* Create a TYPENAME_TYPE to represent the type to which the
3844 functional cast is being performed. */
26bcf8fc 3845 else
98ca843c 3846 type = make_typename_type (parser->scope, id,
fc6a28d7 3847 typename_type,
26bcf8fc 3848 /*complain=*/1);
a723baf1
MM
3849
3850 postfix_expression = cp_parser_functional_cast (parser, type);
3851 }
3852 break;
3853
3854 default:
3855 {
3856 tree type;
3857
3858 /* If the next thing is a simple-type-specifier, we may be
3859 looking at a functional cast. We could also be looking at
3860 an id-expression. So, we try the functional cast, and if
3861 that doesn't work we fall back to the primary-expression. */
3862 cp_parser_parse_tentatively (parser);
3863 /* Look for the simple-type-specifier. */
21526606 3864 type = cp_parser_simple_type_specifier (parser,
62d1db17
MM
3865 /*decl_specs=*/NULL,
3866 CP_PARSER_FLAGS_NONE);
a723baf1
MM
3867 /* Parse the cast itself. */
3868 if (!cp_parser_error_occurred (parser))
21526606 3869 postfix_expression
a723baf1
MM
3870 = cp_parser_functional_cast (parser, type);
3871 /* If that worked, we're done. */
3872 if (cp_parser_parse_definitely (parser))
3873 break;
3874
3875 /* If the functional-cast didn't work out, try a
3876 compound-literal. */
14d22dd6
MM
3877 if (cp_parser_allow_gnu_extensions_p (parser)
3878 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
a723baf1
MM
3879 {
3880 tree initializer_list = NULL_TREE;
4f8163b1 3881 bool saved_in_type_id_in_expr_p;
a723baf1
MM
3882
3883 cp_parser_parse_tentatively (parser);
14d22dd6
MM
3884 /* Consume the `('. */
3885 cp_lexer_consume_token (parser->lexer);
3886 /* Parse the type. */
4f8163b1
MM
3887 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3888 parser->in_type_id_in_expr_p = true;
14d22dd6 3889 type = cp_parser_type_id (parser);
4f8163b1 3890 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14d22dd6
MM
3891 /* Look for the `)'. */
3892 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3893 /* Look for the `{'. */
3894 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3895 /* If things aren't going well, there's no need to
3896 keep going. */
3897 if (!cp_parser_error_occurred (parser))
a723baf1 3898 {
39703eb9 3899 bool non_constant_p;
14d22dd6 3900 /* Parse the initializer-list. */
21526606 3901 initializer_list
39703eb9 3902 = cp_parser_initializer_list (parser, &non_constant_p);
14d22dd6
MM
3903 /* Allow a trailing `,'. */
3904 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3905 cp_lexer_consume_token (parser->lexer);
3906 /* Look for the final `}'. */
3907 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1
MM
3908 }
3909 /* If that worked, we're definitely looking at a
3910 compound-literal expression. */
3911 if (cp_parser_parse_definitely (parser))
3912 {
3913 /* Warn the user that a compound literal is not
3914 allowed in standard C++. */
3915 if (pedantic)
3916 pedwarn ("ISO C++ forbids compound-literals");
3917 /* Form the representation of the compound-literal. */
21526606 3918 postfix_expression
a723baf1
MM
3919 = finish_compound_literal (type, initializer_list);
3920 break;
3921 }
3922 }
3923
3924 /* It must be a primary-expression. */
21526606 3925 postfix_expression = cp_parser_primary_expression (parser,
a723baf1
MM
3926 &idk,
3927 &qualifying_class);
3928 }
3929 break;
3930 }
3931
ee76b931
MM
3932 /* If we were avoiding committing to the processing of a
3933 qualified-id until we knew whether or not we had a
3934 pointer-to-member, we now know. */
089d6ea7 3935 if (qualifying_class)
a723baf1 3936 {
ee76b931 3937 bool done;
a723baf1 3938
ee76b931
MM
3939 /* Peek at the next token. */
3940 token = cp_lexer_peek_token (parser->lexer);
3941 done = (token->type != CPP_OPEN_SQUARE
3942 && token->type != CPP_OPEN_PAREN
3943 && token->type != CPP_DOT
3944 && token->type != CPP_DEREF
3945 && token->type != CPP_PLUS_PLUS
3946 && token->type != CPP_MINUS_MINUS);
3947
3948 postfix_expression = finish_qualified_id_expr (qualifying_class,
3949 postfix_expression,
3950 done,
3951 address_p);
3952 if (done)
3953 return postfix_expression;
a723baf1
MM
3954 }
3955
a723baf1
MM
3956 /* Keep looping until the postfix-expression is complete. */
3957 while (true)
3958 {
10b1d5e7
MM
3959 if (idk == CP_ID_KIND_UNQUALIFIED
3960 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
a723baf1 3961 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
b3445994 3962 /* It is not a Koenig lookup function call. */
21526606 3963 postfix_expression
b3445994 3964 = unqualified_name_lookup_error (postfix_expression);
21526606 3965
a723baf1
MM
3966 /* Peek at the next token. */
3967 token = cp_lexer_peek_token (parser->lexer);
3968
3969 switch (token->type)
3970 {
3971 case CPP_OPEN_SQUARE:
7a3ea201
RH
3972 postfix_expression
3973 = cp_parser_postfix_open_square_expression (parser,
3974 postfix_expression,
3975 false);
3976 idk = CP_ID_KIND_NONE;
a723baf1
MM
3977 break;
3978
3979 case CPP_OPEN_PAREN:
3980 /* postfix-expression ( expression-list [opt] ) */
3981 {
6d80c4b9 3982 bool koenig_p;
21526606 3983 tree args = (cp_parser_parenthesized_expression_list
39703eb9 3984 (parser, false, /*non_constant_p=*/NULL));
a723baf1 3985
7efa3e22
NS
3986 if (args == error_mark_node)
3987 {
3988 postfix_expression = error_mark_node;
3989 break;
3990 }
21526606 3991
14d22dd6
MM
3992 /* Function calls are not permitted in
3993 constant-expressions. */
625cbf93
MM
3994 if (cp_parser_non_integral_constant_expression (parser,
3995 "a function call"))
14d22dd6 3996 {
625cbf93
MM
3997 postfix_expression = error_mark_node;
3998 break;
14d22dd6 3999 }
a723baf1 4000
6d80c4b9 4001 koenig_p = false;
399dedb9
NS
4002 if (idk == CP_ID_KIND_UNQUALIFIED)
4003 {
89d594a2
NS
4004 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4005 {
4006 if (args)
4007 {
4008 koenig_p = true;
4009 postfix_expression
4010 = perform_koenig_lookup (postfix_expression, args);
4011 }
4012 else
4013 postfix_expression
4014 = unqualified_fn_lookup_error (postfix_expression);
4015 }
676e33ca
MM
4016 /* We do not perform argument-dependent lookup if
4017 normal lookup finds a non-function, in accordance
4018 with the expected resolution of DR 218. */
89d594a2 4019 else if (args && is_overloaded_fn (postfix_expression))
6d80c4b9 4020 {
89d594a2
NS
4021 tree fn = get_first_fn (postfix_expression);
4022
4023 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4024 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4025
4026 /* Only do argument dependent lookup if regular
4027 lookup does not find a set of member functions.
4028 [basic.lookup.koenig]/2a */
4029 if (!DECL_FUNCTION_MEMBER_P (fn))
4030 {
4031 koenig_p = true;
4032 postfix_expression
4033 = perform_koenig_lookup (postfix_expression, args);
4034 }
6d80c4b9 4035 }
399dedb9 4036 }
21526606 4037
d17811fd 4038 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
a723baf1 4039 {
d17811fd
MM
4040 tree instance = TREE_OPERAND (postfix_expression, 0);
4041 tree fn = TREE_OPERAND (postfix_expression, 1);
4042
4043 if (processing_template_decl
4044 && (type_dependent_expression_p (instance)
4045 || (!BASELINK_P (fn)
4046 && TREE_CODE (fn) != FIELD_DECL)
584672ee 4047 || type_dependent_expression_p (fn)
d17811fd
MM
4048 || any_type_dependent_arguments_p (args)))
4049 {
4050 postfix_expression
6de9cd9a
DN
4051 = build_min_nt (CALL_EXPR, postfix_expression,
4052 args, NULL_TREE);
d17811fd
MM
4053 break;
4054 }
9f880ef9
MM
4055
4056 if (BASELINK_P (fn))
4057 postfix_expression
21526606
EC
4058 = (build_new_method_call
4059 (instance, fn, args, NULL_TREE,
4060 (idk == CP_ID_KIND_QUALIFIED
9f880ef9
MM
4061 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4062 else
4063 postfix_expression
4064 = finish_call_expr (postfix_expression, args,
4065 /*disallow_virtual=*/false,
4066 /*koenig_p=*/false);
a723baf1 4067 }
d17811fd
MM
4068 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4069 || TREE_CODE (postfix_expression) == MEMBER_REF
4070 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
a723baf1
MM
4071 postfix_expression = (build_offset_ref_call_from_tree
4072 (postfix_expression, args));
b3445994 4073 else if (idk == CP_ID_KIND_QUALIFIED)
2050a1bb
MM
4074 /* A call to a static class member, or a namespace-scope
4075 function. */
4076 postfix_expression
4077 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
4078 /*disallow_virtual=*/true,
4079 koenig_p);
a723baf1 4080 else
2050a1bb 4081 /* All other function calls. */
21526606
EC
4082 postfix_expression
4083 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
4084 /*disallow_virtual=*/false,
4085 koenig_p);
a723baf1
MM
4086
4087 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
b3445994 4088 idk = CP_ID_KIND_NONE;
a723baf1
MM
4089 }
4090 break;
21526606 4091
a723baf1
MM
4092 case CPP_DOT:
4093 case CPP_DEREF:
21526606
EC
4094 /* postfix-expression . template [opt] id-expression
4095 postfix-expression . pseudo-destructor-name
a723baf1
MM
4096 postfix-expression -> template [opt] id-expression
4097 postfix-expression -> pseudo-destructor-name */
98ca843c 4098
7a3ea201
RH
4099 /* Consume the `.' or `->' operator. */
4100 cp_lexer_consume_token (parser->lexer);
a723baf1 4101
7a3ea201
RH
4102 postfix_expression
4103 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4104 postfix_expression,
4105 false, &idk);
a723baf1
MM
4106 break;
4107
4108 case CPP_PLUS_PLUS:
4109 /* postfix-expression ++ */
4110 /* Consume the `++' token. */
4111 cp_lexer_consume_token (parser->lexer);
a5ac3982 4112 /* Generate a representation for the complete expression. */
21526606
EC
4113 postfix_expression
4114 = finish_increment_expr (postfix_expression,
a5ac3982 4115 POSTINCREMENT_EXPR);
14d22dd6 4116 /* Increments may not appear in constant-expressions. */
625cbf93
MM
4117 if (cp_parser_non_integral_constant_expression (parser,
4118 "an increment"))
4119 postfix_expression = error_mark_node;
b3445994 4120 idk = CP_ID_KIND_NONE;
a723baf1
MM
4121 break;
4122
4123 case CPP_MINUS_MINUS:
4124 /* postfix-expression -- */
4125 /* Consume the `--' token. */
4126 cp_lexer_consume_token (parser->lexer);
a5ac3982 4127 /* Generate a representation for the complete expression. */
21526606
EC
4128 postfix_expression
4129 = finish_increment_expr (postfix_expression,
a5ac3982 4130 POSTDECREMENT_EXPR);
14d22dd6 4131 /* Decrements may not appear in constant-expressions. */
625cbf93
MM
4132 if (cp_parser_non_integral_constant_expression (parser,
4133 "a decrement"))
4134 postfix_expression = error_mark_node;
b3445994 4135 idk = CP_ID_KIND_NONE;
a723baf1
MM
4136 break;
4137
4138 default:
4139 return postfix_expression;
4140 }
4141 }
4142
4143 /* We should never get here. */
315fb5db 4144 gcc_unreachable ();
a723baf1
MM
4145 return error_mark_node;
4146}
4147
7a3ea201
RH
4148/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4149 by cp_parser_builtin_offsetof. We're looking for
4150
4151 postfix-expression [ expression ]
4152
4153 FOR_OFFSETOF is set if we're being called in that context, which
4154 changes how we deal with integer constant expressions. */
4155
4156static tree
4157cp_parser_postfix_open_square_expression (cp_parser *parser,
4158 tree postfix_expression,
4159 bool for_offsetof)
4160{
4161 tree index;
4162
4163 /* Consume the `[' token. */
4164 cp_lexer_consume_token (parser->lexer);
4165
4166 /* Parse the index expression. */
4167 /* ??? For offsetof, there is a question of what to allow here. If
4168 offsetof is not being used in an integral constant expression context,
4169 then we *could* get the right answer by computing the value at runtime.
4170 If we are in an integral constant expression context, then we might
4171 could accept any constant expression; hard to say without analysis.
4172 Rather than open the barn door too wide right away, allow only integer
77880ae4 4173 constant expressions here. */
7a3ea201
RH
4174 if (for_offsetof)
4175 index = cp_parser_constant_expression (parser, false, NULL);
4176 else
4177 index = cp_parser_expression (parser);
4178
4179 /* Look for the closing `]'. */
4180 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4181
4182 /* Build the ARRAY_REF. */
4183 postfix_expression = grok_array_decl (postfix_expression, index);
4184
4185 /* When not doing offsetof, array references are not permitted in
4186 constant-expressions. */
4187 if (!for_offsetof
4188 && (cp_parser_non_integral_constant_expression
4189 (parser, "an array reference")))
4190 postfix_expression = error_mark_node;
4191
4192 return postfix_expression;
4193}
4194
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 . template [opt] id-expression
4199 postfix-expression . pseudo-destructor-name
4200 postfix-expression -> template [opt] id-expression
4201 postfix-expression -> pseudo-destructor-name
4202
4203 FOR_OFFSETOF is set if we're being called in that context. That sorta
4204 limits what of the above we'll actually accept, but nevermind.
4205 TOKEN_TYPE is the "." or "->" token, which will already have been
4206 removed from the stream. */
4207
4208static tree
4209cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4210 enum cpp_ttype token_type,
4211 tree postfix_expression,
4212 bool for_offsetof, cp_id_kind *idk)
4213{
4214 tree name;
4215 bool dependent_p;
4216 bool template_p;
17a27b4f 4217 bool pseudo_destructor_p;
7a3ea201
RH
4218 tree scope = NULL_TREE;
4219
4220 /* If this is a `->' operator, dereference the pointer. */
4221 if (token_type == CPP_DEREF)
4222 postfix_expression = build_x_arrow (postfix_expression);
4223 /* Check to see whether or not the expression is type-dependent. */
4224 dependent_p = type_dependent_expression_p (postfix_expression);
4225 /* The identifier following the `->' or `.' is not qualified. */
4226 parser->scope = NULL_TREE;
4227 parser->qualifying_scope = NULL_TREE;
4228 parser->object_scope = NULL_TREE;
4229 *idk = CP_ID_KIND_NONE;
4230 /* Enter the scope corresponding to the type of the object
4231 given by the POSTFIX_EXPRESSION. */
4232 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4233 {
4234 scope = TREE_TYPE (postfix_expression);
4235 /* According to the standard, no expression should ever have
4236 reference type. Unfortunately, we do not currently match
4237 the standard in this respect in that our internal representation
4238 of an expression may have reference type even when the standard
4239 says it does not. Therefore, we have to manually obtain the
4240 underlying type here. */
4241 scope = non_reference (scope);
4242 /* The type of the POSTFIX_EXPRESSION must be complete. */
4243 scope = complete_type_or_else (scope, NULL_TREE);
4244 /* Let the name lookup machinery know that we are processing a
4245 class member access expression. */
4246 parser->context->object_type = scope;
4247 /* If something went wrong, we want to be able to discern that case,
4248 as opposed to the case where there was no SCOPE due to the type
4249 of expression being dependent. */
4250 if (!scope)
4251 scope = error_mark_node;
4252 /* If the SCOPE was erroneous, make the various semantic analysis
4253 functions exit quickly -- and without issuing additional error
4254 messages. */
4255 if (scope == error_mark_node)
4256 postfix_expression = error_mark_node;
4257 }
4258
17a27b4f
MM
4259 /* Assume this expression is not a pseudo-destructor access. */
4260 pseudo_destructor_p = false;
4261
4262 /* If the SCOPE is a scalar type, then, if this is a valid program,
4263 we must be looking at a pseudo-destructor-name. */
4264 if (scope && SCALAR_TYPE_P (scope))
7a3ea201 4265 {
17a27b4f
MM
4266 tree s;
4267 tree type;
4268
4269 cp_parser_parse_tentatively (parser);
4270 /* Parse the pseudo-destructor-name. */
4271 s = NULL_TREE;
4272 cp_parser_pseudo_destructor_name (parser, &s, &type);
4273 if (cp_parser_parse_definitely (parser))
4274 {
4275 pseudo_destructor_p = true;
4276 postfix_expression
4277 = finish_pseudo_destructor_expr (postfix_expression,
4278 s, TREE_TYPE (type));
4279 }
4280 }
4281
4282 if (!pseudo_destructor_p)
4283 {
4284 /* If the SCOPE is not a scalar type, we are looking at an
4285 ordinary class member access expression, rather than a
4286 pseudo-destructor-name. */
7a3ea201
RH
4287 template_p = cp_parser_optional_template_keyword (parser);
4288 /* Parse the id-expression. */
4289 name = cp_parser_id_expression (parser, template_p,
4290 /*check_dependency_p=*/true,
4291 /*template_p=*/NULL,
4292 /*declarator_p=*/false);
4293 /* In general, build a SCOPE_REF if the member name is qualified.
4294 However, if the name was not dependent and has already been
4295 resolved; there is no need to build the SCOPE_REF. For example;
4296
4297 struct X { void f(); };
4298 template <typename T> void f(T* t) { t->X::f(); }
4299
4300 Even though "t" is dependent, "X::f" is not and has been resolved
4301 to a BASELINK; there is no need to include scope information. */
4302
4303 /* But we do need to remember that there was an explicit scope for
4304 virtual function calls. */
4305 if (parser->scope)
4306 *idk = CP_ID_KIND_QUALIFIED;
4307
fc6a28d7
MM
4308 /* If the name is a template-id that names a type, we will get a
4309 TYPE_DECL here. That is invalid code. */
4310 if (TREE_CODE (name) == TYPE_DECL)
7a3ea201 4311 {
fc6a28d7
MM
4312 error ("invalid use of %qD", name);
4313 postfix_expression = error_mark_node;
4314 }
4315 else
4316 {
4317 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4318 {
4319 name = build_nt (SCOPE_REF, parser->scope, name);
4320 parser->scope = NULL_TREE;
4321 parser->qualifying_scope = NULL_TREE;
4322 parser->object_scope = NULL_TREE;
4323 }
4324 if (scope && name && BASELINK_P (name))
4325 adjust_result_of_qualified_name_lookup
4326 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4327 postfix_expression
4328 = finish_class_member_access_expr (postfix_expression, name);
7a3ea201 4329 }
7a3ea201 4330 }
7a3ea201
RH
4331
4332 /* We no longer need to look up names in the scope of the object on
4333 the left-hand side of the `.' or `->' operator. */
4334 parser->context->object_type = NULL_TREE;
4335
4336 /* Outside of offsetof, these operators may not appear in
4337 constant-expressions. */
4338 if (!for_offsetof
98ca843c 4339 && (cp_parser_non_integral_constant_expression
7a3ea201
RH
4340 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4341 postfix_expression = error_mark_node;
4342
4343 return postfix_expression;
4344}
4345
7efa3e22 4346/* Parse a parenthesized expression-list.
a723baf1
MM
4347
4348 expression-list:
4349 assignment-expression
4350 expression-list, assignment-expression
4351
7efa3e22
NS
4352 attribute-list:
4353 expression-list
4354 identifier
4355 identifier, expression-list
4356
a723baf1
MM
4357 Returns a TREE_LIST. The TREE_VALUE of each node is a
4358 representation of an assignment-expression. Note that a TREE_LIST
7efa3e22
NS
4359 is returned even if there is only a single expression in the list.
4360 error_mark_node is returned if the ( and or ) are
4361 missing. NULL_TREE is returned on no expressions. The parentheses
4362 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
39703eb9
MM
4363 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4364 indicates whether or not all of the expressions in the list were
4365 constant. */
a723baf1
MM
4366
4367static tree
21526606 4368cp_parser_parenthesized_expression_list (cp_parser* parser,
39703eb9
MM
4369 bool is_attribute_list,
4370 bool *non_constant_p)
a723baf1
MM
4371{
4372 tree expression_list = NULL_TREE;
1ed3dfd5 4373 bool fold_expr_p = is_attribute_list;
7efa3e22 4374 tree identifier = NULL_TREE;
39703eb9
MM
4375
4376 /* Assume all the expressions will be constant. */
4377 if (non_constant_p)
4378 *non_constant_p = false;
4379
7efa3e22
NS
4380 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4381 return error_mark_node;
21526606 4382
a723baf1 4383 /* Consume expressions until there are no more. */
7efa3e22
NS
4384 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4385 while (true)
4386 {
4387 tree expr;
21526606 4388
7efa3e22
NS
4389 /* At the beginning of attribute lists, check to see if the
4390 next token is an identifier. */
4391 if (is_attribute_list
4392 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4393 {
4394 cp_token *token;
21526606 4395
7efa3e22
NS
4396 /* Consume the identifier. */
4397 token = cp_lexer_consume_token (parser->lexer);
4398 /* Save the identifier. */
4399 identifier = token->value;
4400 }
4401 else
4402 {
4403 /* Parse the next assignment-expression. */
39703eb9
MM
4404 if (non_constant_p)
4405 {
4406 bool expr_non_constant_p;
21526606 4407 expr = (cp_parser_constant_expression
39703eb9
MM
4408 (parser, /*allow_non_constant_p=*/true,
4409 &expr_non_constant_p));
4410 if (expr_non_constant_p)
4411 *non_constant_p = true;
4412 }
4413 else
4414 expr = cp_parser_assignment_expression (parser);
a723baf1 4415
1ed3dfd5
GB
4416 if (fold_expr_p)
4417 expr = fold_non_dependent_expr (expr);
4418
7efa3e22
NS
4419 /* Add it to the list. We add error_mark_node
4420 expressions to the list, so that we can still tell if
4421 the correct form for a parenthesized expression-list
4422 is found. That gives better errors. */
4423 expression_list = tree_cons (NULL_TREE, expr, expression_list);
a723baf1 4424
7efa3e22
NS
4425 if (expr == error_mark_node)
4426 goto skip_comma;
4427 }
a723baf1 4428
7efa3e22
NS
4429 /* After the first item, attribute lists look the same as
4430 expression lists. */
4431 is_attribute_list = false;
21526606 4432
7efa3e22
NS
4433 get_comma:;
4434 /* If the next token isn't a `,', then we are done. */
4435 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4436 break;
4437
4438 /* Otherwise, consume the `,' and keep going. */
4439 cp_lexer_consume_token (parser->lexer);
4440 }
21526606 4441
7efa3e22
NS
4442 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4443 {
4444 int ending;
21526606 4445
7efa3e22
NS
4446 skip_comma:;
4447 /* We try and resync to an unnested comma, as that will give the
4448 user better diagnostics. */
21526606
EC
4449 ending = cp_parser_skip_to_closing_parenthesis (parser,
4450 /*recovering=*/true,
4bb8ca28 4451 /*or_comma=*/true,
a668c6ad 4452 /*consume_paren=*/true);
7efa3e22
NS
4453 if (ending < 0)
4454 goto get_comma;
4455 if (!ending)
4456 return error_mark_node;
a723baf1
MM
4457 }
4458
4459 /* We built up the list in reverse order so we must reverse it now. */
7efa3e22
NS
4460 expression_list = nreverse (expression_list);
4461 if (identifier)
4462 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
21526606 4463
7efa3e22 4464 return expression_list;
a723baf1
MM
4465}
4466
4467/* Parse a pseudo-destructor-name.
4468
4469 pseudo-destructor-name:
4470 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4471 :: [opt] nested-name-specifier template template-id :: ~ type-name
4472 :: [opt] nested-name-specifier [opt] ~ type-name
4473
4474 If either of the first two productions is used, sets *SCOPE to the
4475 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4476 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
d6e57462 4477 or ERROR_MARK_NODE if the parse fails. */
a723baf1
MM
4478
4479static void
21526606
EC
4480cp_parser_pseudo_destructor_name (cp_parser* parser,
4481 tree* scope,
94edc4ab 4482 tree* type)
a723baf1
MM
4483{
4484 bool nested_name_specifier_p;
4485
b14454ba
MM
4486 /* Assume that things will not work out. */
4487 *type = error_mark_node;
4488
a723baf1
MM
4489 /* Look for the optional `::' operator. */
4490 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4491 /* Look for the optional nested-name-specifier. */
21526606 4492 nested_name_specifier_p
a723baf1
MM
4493 = (cp_parser_nested_name_specifier_opt (parser,
4494 /*typename_keyword_p=*/false,
4495 /*check_dependency_p=*/true,
a668c6ad 4496 /*type_p=*/false,
21526606 4497 /*is_declaration=*/true)
a723baf1
MM
4498 != NULL_TREE);
4499 /* Now, if we saw a nested-name-specifier, we might be doing the
4500 second production. */
21526606 4501 if (nested_name_specifier_p
a723baf1
MM
4502 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4503 {
4504 /* Consume the `template' keyword. */
4505 cp_lexer_consume_token (parser->lexer);
4506 /* Parse the template-id. */
21526606 4507 cp_parser_template_id (parser,
a723baf1 4508 /*template_keyword_p=*/true,
a668c6ad
MM
4509 /*check_dependency_p=*/false,
4510 /*is_declaration=*/true);
a723baf1
MM
4511 /* Look for the `::' token. */
4512 cp_parser_require (parser, CPP_SCOPE, "`::'");
4513 }
4514 /* If the next token is not a `~', then there might be some
9bcb9aae 4515 additional qualification. */
a723baf1
MM
4516 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4517 {
4518 /* Look for the type-name. */
4519 *scope = TREE_TYPE (cp_parser_type_name (parser));
d6e57462 4520
b14454ba
MM
4521 if (*scope == error_mark_node)
4522 return;
4523
4524 /* If we don't have ::~, then something has gone wrong. Since
4525 the only caller of this function is looking for something
4526 after `.' or `->' after a scalar type, most likely the
4527 program is trying to get a member of a non-aggregate
4528 type. */
4529 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
d6e57462
ILT
4530 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4531 {
4532 cp_parser_error (parser, "request for member of non-aggregate type");
d6e57462
ILT
4533 return;
4534 }
4535
a723baf1
MM
4536 /* Look for the `::' token. */
4537 cp_parser_require (parser, CPP_SCOPE, "`::'");
4538 }
4539 else
4540 *scope = NULL_TREE;
4541
4542 /* Look for the `~'. */
4543 cp_parser_require (parser, CPP_COMPL, "`~'");
4544 /* Look for the type-name again. We are not responsible for
4545 checking that it matches the first type-name. */
4546 *type = cp_parser_type_name (parser);
4547}
4548
4549/* Parse a unary-expression.
4550
4551 unary-expression:
4552 postfix-expression
4553 ++ cast-expression
4554 -- cast-expression
4555 unary-operator cast-expression
4556 sizeof unary-expression
4557 sizeof ( type-id )
4558 new-expression
4559 delete-expression
4560
4561 GNU Extensions:
4562
4563 unary-expression:
4564 __extension__ cast-expression
4565 __alignof__ unary-expression
4566 __alignof__ ( type-id )
4567 __real__ cast-expression
4568 __imag__ cast-expression
4569 && identifier
4570
4571 ADDRESS_P is true iff the unary-expression is appearing as the
4572 operand of the `&' operator.
4573
34cd5ae7 4574 Returns a representation of the expression. */
a723baf1
MM
4575
4576static tree
4577cp_parser_unary_expression (cp_parser *parser, bool address_p)
4578{
4579 cp_token *token;
4580 enum tree_code unary_operator;
4581
4582 /* Peek at the next token. */
4583 token = cp_lexer_peek_token (parser->lexer);
4584 /* Some keywords give away the kind of expression. */
4585 if (token->type == CPP_KEYWORD)
4586 {
4587 enum rid keyword = token->keyword;
4588
4589 switch (keyword)
4590 {
4591 case RID_ALIGNOF:
a723baf1
MM
4592 case RID_SIZEOF:
4593 {
4594 tree operand;
7a18b933 4595 enum tree_code op;
21526606 4596
7a18b933
NS
4597 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4598 /* Consume the token. */
a723baf1
MM
4599 cp_lexer_consume_token (parser->lexer);
4600 /* Parse the operand. */
4601 operand = cp_parser_sizeof_operand (parser, keyword);
4602
7a18b933
NS
4603 if (TYPE_P (operand))
4604 return cxx_sizeof_or_alignof_type (operand, op, true);
a723baf1 4605 else
7a18b933 4606 return cxx_sizeof_or_alignof_expr (operand, op);
a723baf1
MM
4607 }
4608
4609 case RID_NEW:
4610 return cp_parser_new_expression (parser);
4611
4612 case RID_DELETE:
4613 return cp_parser_delete_expression (parser);
21526606 4614
a723baf1
MM
4615 case RID_EXTENSION:
4616 {
4617 /* The saved value of the PEDANTIC flag. */
4618 int saved_pedantic;
4619 tree expr;
4620
4621 /* Save away the PEDANTIC flag. */
4622 cp_parser_extension_opt (parser, &saved_pedantic);
4623 /* Parse the cast-expression. */
d6b4ea85 4624 expr = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4625 /* Restore the PEDANTIC flag. */
4626 pedantic = saved_pedantic;
4627
4628 return expr;
4629 }
4630
4631 case RID_REALPART:
4632 case RID_IMAGPART:
4633 {
4634 tree expression;
4635
4636 /* Consume the `__real__' or `__imag__' token. */
4637 cp_lexer_consume_token (parser->lexer);
4638 /* Parse the cast-expression. */
d6b4ea85 4639 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4640 /* Create the complete representation. */
4641 return build_x_unary_op ((keyword == RID_REALPART
4642 ? REALPART_EXPR : IMAGPART_EXPR),
4643 expression);
4644 }
4645 break;
4646
4647 default:
4648 break;
4649 }
4650 }
4651
4652 /* Look for the `:: new' and `:: delete', which also signal the
4653 beginning of a new-expression, or delete-expression,
4654 respectively. If the next token is `::', then it might be one of
4655 these. */
4656 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4657 {
4658 enum rid keyword;
4659
4660 /* See if the token after the `::' is one of the keywords in
4661 which we're interested. */
4662 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4663 /* If it's `new', we have a new-expression. */
4664 if (keyword == RID_NEW)
4665 return cp_parser_new_expression (parser);
4666 /* Similarly, for `delete'. */
4667 else if (keyword == RID_DELETE)
4668 return cp_parser_delete_expression (parser);
4669 }
4670
4671 /* Look for a unary operator. */
4672 unary_operator = cp_parser_unary_operator (token);
4673 /* The `++' and `--' operators can be handled similarly, even though
4674 they are not technically unary-operators in the grammar. */
4675 if (unary_operator == ERROR_MARK)
4676 {
4677 if (token->type == CPP_PLUS_PLUS)
4678 unary_operator = PREINCREMENT_EXPR;
4679 else if (token->type == CPP_MINUS_MINUS)
4680 unary_operator = PREDECREMENT_EXPR;
4681 /* Handle the GNU address-of-label extension. */
4682 else if (cp_parser_allow_gnu_extensions_p (parser)
4683 && token->type == CPP_AND_AND)
4684 {
4685 tree identifier;
4686
4687 /* Consume the '&&' token. */
4688 cp_lexer_consume_token (parser->lexer);
4689 /* Look for the identifier. */
4690 identifier = cp_parser_identifier (parser);
4691 /* Create an expression representing the address. */
4692 return finish_label_address_expr (identifier);
4693 }
4694 }
4695 if (unary_operator != ERROR_MARK)
4696 {
4697 tree cast_expression;
a5ac3982
MM
4698 tree expression = error_mark_node;
4699 const char *non_constant_p = NULL;
a723baf1
MM
4700
4701 /* Consume the operator token. */
4702 token = cp_lexer_consume_token (parser->lexer);
4703 /* Parse the cast-expression. */
21526606 4704 cast_expression
a723baf1
MM
4705 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4706 /* Now, build an appropriate representation. */
4707 switch (unary_operator)
4708 {
4709 case INDIRECT_REF:
a5ac3982
MM
4710 non_constant_p = "`*'";
4711 expression = build_x_indirect_ref (cast_expression, "unary *");
4712 break;
4713
a723baf1 4714 case ADDR_EXPR:
7a3ea201 4715 non_constant_p = "`&'";
a5ac3982 4716 /* Fall through. */
d17811fd 4717 case BIT_NOT_EXPR:
a5ac3982
MM
4718 expression = build_x_unary_op (unary_operator, cast_expression);
4719 break;
4720
14d22dd6
MM
4721 case PREINCREMENT_EXPR:
4722 case PREDECREMENT_EXPR:
a5ac3982
MM
4723 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4724 ? "`++'" : "`--'");
14d22dd6 4725 /* Fall through. */
a723baf1
MM
4726 case CONVERT_EXPR:
4727 case NEGATE_EXPR:
4728 case TRUTH_NOT_EXPR:
a5ac3982
MM
4729 expression = finish_unary_op_expr (unary_operator, cast_expression);
4730 break;
a723baf1 4731
a723baf1 4732 default:
315fb5db 4733 gcc_unreachable ();
a723baf1 4734 }
a5ac3982 4735
98ca843c 4736 if (non_constant_p
625cbf93
MM
4737 && cp_parser_non_integral_constant_expression (parser,
4738 non_constant_p))
4739 expression = error_mark_node;
a5ac3982
MM
4740
4741 return expression;
a723baf1
MM
4742 }
4743
4744 return cp_parser_postfix_expression (parser, address_p);
4745}
4746
4747/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4748 unary-operator, the corresponding tree code is returned. */
4749
4750static enum tree_code
94edc4ab 4751cp_parser_unary_operator (cp_token* token)
a723baf1
MM
4752{
4753 switch (token->type)
4754 {
4755 case CPP_MULT:
4756 return INDIRECT_REF;
4757
4758 case CPP_AND:
4759 return ADDR_EXPR;
4760
4761 case CPP_PLUS:
4762 return CONVERT_EXPR;
4763
4764 case CPP_MINUS:
4765 return NEGATE_EXPR;
4766
4767 case CPP_NOT:
4768 return TRUTH_NOT_EXPR;
21526606 4769
a723baf1
MM
4770 case CPP_COMPL:
4771 return BIT_NOT_EXPR;
4772
4773 default:
4774 return ERROR_MARK;
4775 }
4776}
4777
4778/* Parse a new-expression.
4779
ca099ac8 4780 new-expression:
a723baf1
MM
4781 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4782 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4783
4784 Returns a representation of the expression. */
4785
4786static tree
94edc4ab 4787cp_parser_new_expression (cp_parser* parser)
a723baf1
MM
4788{
4789 bool global_scope_p;
4790 tree placement;
4791 tree type;
4792 tree initializer;
058b15c1 4793 tree nelts;
a723baf1
MM
4794
4795 /* Look for the optional `::' operator. */
21526606 4796 global_scope_p
a723baf1
MM
4797 = (cp_parser_global_scope_opt (parser,
4798 /*current_scope_valid_p=*/false)
4799 != NULL_TREE);
4800 /* Look for the `new' operator. */
4801 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4802 /* There's no easy way to tell a new-placement from the
4803 `( type-id )' construct. */
4804 cp_parser_parse_tentatively (parser);
4805 /* Look for a new-placement. */
4806 placement = cp_parser_new_placement (parser);
4807 /* If that didn't work out, there's no new-placement. */
4808 if (!cp_parser_parse_definitely (parser))
4809 placement = NULL_TREE;
4810
4811 /* If the next token is a `(', then we have a parenthesized
4812 type-id. */
4813 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4814 {
4815 /* Consume the `('. */
4816 cp_lexer_consume_token (parser->lexer);
4817 /* Parse the type-id. */
4818 type = cp_parser_type_id (parser);
4819 /* Look for the closing `)'. */
4820 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
98ca843c 4821 /* There should not be a direct-new-declarator in this production,
063e900f
GB
4822 but GCC used to allowed this, so we check and emit a sensible error
4823 message for this case. */
4824 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
0da99d4e
GB
4825 {
4826 error ("array bound forbidden after parenthesized type-id");
4827 inform ("try removing the parentheses around the type-id");
063e900f
GB
4828 cp_parser_direct_new_declarator (parser);
4829 }
17a27b4f 4830 nelts = NULL_TREE;
a723baf1
MM
4831 }
4832 /* Otherwise, there must be a new-type-id. */
4833 else
058b15c1 4834 type = cp_parser_new_type_id (parser, &nelts);
a723baf1
MM
4835
4836 /* If the next token is a `(', then we have a new-initializer. */
4837 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4838 initializer = cp_parser_new_initializer (parser);
4839 else
4840 initializer = NULL_TREE;
4841
625cbf93
MM
4842 /* A new-expression may not appear in an integral constant
4843 expression. */
4844 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4845 return error_mark_node;
4846
a723baf1 4847 /* Create a representation of the new-expression. */
058b15c1 4848 return build_new (placement, type, nelts, initializer, global_scope_p);
a723baf1
MM
4849}
4850
4851/* Parse a new-placement.
4852
4853 new-placement:
4854 ( expression-list )
4855
4856 Returns the same representation as for an expression-list. */
4857
4858static tree
94edc4ab 4859cp_parser_new_placement (cp_parser* parser)
a723baf1
MM
4860{
4861 tree expression_list;
4862
a723baf1 4863 /* Parse the expression-list. */
21526606 4864 expression_list = (cp_parser_parenthesized_expression_list
39703eb9 4865 (parser, false, /*non_constant_p=*/NULL));
a723baf1
MM
4866
4867 return expression_list;
4868}
4869
4870/* Parse a new-type-id.
4871
4872 new-type-id:
4873 type-specifier-seq new-declarator [opt]
4874
058b15c1
MM
4875 Returns the TYPE allocated. If the new-type-id indicates an array
4876 type, *NELTS is set to the number of elements in the last array
4877 bound; the TYPE will not include the last array bound. */
a723baf1
MM
4878
4879static tree
058b15c1 4880cp_parser_new_type_id (cp_parser* parser, tree *nelts)
a723baf1 4881{
62d1db17 4882 cp_decl_specifier_seq type_specifier_seq;
058b15c1
MM
4883 cp_declarator *new_declarator;
4884 cp_declarator *declarator;
4885 cp_declarator *outer_declarator;
a723baf1 4886 const char *saved_message;
058b15c1 4887 tree type;
a723baf1
MM
4888
4889 /* The type-specifier sequence must not contain type definitions.
4890 (It cannot contain declarations of new types either, but if they
4891 are not definitions we will catch that because they are not
4892 complete.) */
4893 saved_message = parser->type_definition_forbidden_message;
4894 parser->type_definition_forbidden_message
4895 = "types may not be defined in a new-type-id";
4896 /* Parse the type-specifier-seq. */
62d1db17 4897 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
a723baf1
MM
4898 /* Restore the old message. */
4899 parser->type_definition_forbidden_message = saved_message;
4900 /* Parse the new-declarator. */
058b15c1
MM
4901 new_declarator = cp_parser_new_declarator_opt (parser);
4902
4903 /* Determine the number of elements in the last array dimension, if
4904 any. */
4905 *nelts = NULL_TREE;
4906 /* Skip down to the last array dimension. */
4907 declarator = new_declarator;
4908 outer_declarator = NULL;
4909 while (declarator && (declarator->kind == cdk_pointer
4910 || declarator->kind == cdk_ptrmem))
4911 {
4912 outer_declarator = declarator;
4913 declarator = declarator->declarator;
4914 }
98ca843c 4915 while (declarator
058b15c1
MM
4916 && declarator->kind == cdk_array
4917 && declarator->declarator
4918 && declarator->declarator->kind == cdk_array)
4919 {
4920 outer_declarator = declarator;
4921 declarator = declarator->declarator;
4922 }
98ca843c 4923
058b15c1
MM
4924 if (declarator && declarator->kind == cdk_array)
4925 {
4926 *nelts = declarator->u.array.bounds;
4927 if (*nelts == error_mark_node)
4928 *nelts = integer_one_node;
ad1063d5 4929
058b15c1
MM
4930 if (outer_declarator)
4931 outer_declarator->declarator = declarator->declarator;
4932 else
4933 new_declarator = NULL;
4934 }
a723baf1 4935
62d1db17 4936 type = groktypename (&type_specifier_seq, new_declarator);
058b15c1
MM
4937 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4938 {
4939 *nelts = array_type_nelts_top (type);
4940 type = TREE_TYPE (type);
4941 }
4942 return type;
a723baf1
MM
4943}
4944
4945/* Parse an (optional) new-declarator.
4946
4947 new-declarator:
4948 ptr-operator new-declarator [opt]
4949 direct-new-declarator
4950
058b15c1 4951 Returns the declarator. */
a723baf1 4952
058b15c1 4953static cp_declarator *
94edc4ab 4954cp_parser_new_declarator_opt (cp_parser* parser)
a723baf1
MM
4955{
4956 enum tree_code code;
4957 tree type;
3c01e5df 4958 cp_cv_quals cv_quals;
a723baf1
MM
4959
4960 /* We don't know if there's a ptr-operator next, or not. */
4961 cp_parser_parse_tentatively (parser);
4962 /* Look for a ptr-operator. */
3c01e5df 4963 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
a723baf1
MM
4964 /* If that worked, look for more new-declarators. */
4965 if (cp_parser_parse_definitely (parser))
4966 {
058b15c1 4967 cp_declarator *declarator;
a723baf1
MM
4968
4969 /* Parse another optional declarator. */
4970 declarator = cp_parser_new_declarator_opt (parser);
4971
4972 /* Create the representation of the declarator. */
058b15c1 4973 if (type)
3c01e5df 4974 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
058b15c1 4975 else if (code == INDIRECT_REF)
3c01e5df 4976 declarator = make_pointer_declarator (cv_quals, declarator);
a723baf1 4977 else
3c01e5df 4978 declarator = make_reference_declarator (cv_quals, declarator);
a723baf1 4979
a723baf1
MM
4980 return declarator;
4981 }
4982
4983 /* If the next token is a `[', there is a direct-new-declarator. */
4984 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4985 return cp_parser_direct_new_declarator (parser);
4986
058b15c1 4987 return NULL;
a723baf1
MM
4988}
4989
4990/* Parse a direct-new-declarator.
4991
4992 direct-new-declarator:
4993 [ expression ]
21526606 4994 direct-new-declarator [constant-expression]
a723baf1 4995
058b15c1 4996 */
a723baf1 4997
058b15c1 4998static cp_declarator *
94edc4ab 4999cp_parser_direct_new_declarator (cp_parser* parser)
a723baf1 5000{
058b15c1 5001 cp_declarator *declarator = NULL;
a723baf1
MM
5002
5003 while (true)
5004 {
5005 tree expression;
5006
5007 /* Look for the opening `['. */
5008 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5009 /* The first expression is not required to be constant. */
5010 if (!declarator)
5011 {
5012 expression = cp_parser_expression (parser);
5013 /* The standard requires that the expression have integral
5014 type. DR 74 adds enumeration types. We believe that the
5015 real intent is that these expressions be handled like the
5016 expression in a `switch' condition, which also allows
5017 classes with a single conversion to integral or
5018 enumeration type. */
5019 if (!processing_template_decl)
5020 {
21526606 5021 expression
a723baf1
MM
5022 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5023 expression,
b746c5dc 5024 /*complain=*/true);
a723baf1
MM
5025 if (!expression)
5026 {
2a13a625
GDR
5027 error ("expression in new-declarator must have integral "
5028 "or enumeration type");
a723baf1
MM
5029 expression = error_mark_node;
5030 }
5031 }
5032 }
5033 /* But all the other expressions must be. */
5034 else
21526606
EC
5035 expression
5036 = cp_parser_constant_expression (parser,
14d22dd6
MM
5037 /*allow_non_constant=*/false,
5038 NULL);
a723baf1
MM
5039 /* Look for the closing `]'. */
5040 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5041
5042 /* Add this bound to the declarator. */
058b15c1 5043 declarator = make_array_declarator (declarator, expression);
a723baf1
MM
5044
5045 /* If the next token is not a `[', then there are no more
5046 bounds. */
5047 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5048 break;
5049 }
5050
5051 return declarator;
5052}
5053
5054/* Parse a new-initializer.
5055
5056 new-initializer:
5057 ( expression-list [opt] )
5058
34cd5ae7 5059 Returns a representation of the expression-list. If there is no
a723baf1
MM
5060 expression-list, VOID_ZERO_NODE is returned. */
5061
5062static tree
94edc4ab 5063cp_parser_new_initializer (cp_parser* parser)
a723baf1
MM
5064{
5065 tree expression_list;
5066
21526606 5067 expression_list = (cp_parser_parenthesized_expression_list
39703eb9 5068 (parser, false, /*non_constant_p=*/NULL));
7efa3e22 5069 if (!expression_list)
a723baf1 5070 expression_list = void_zero_node;
a723baf1
MM
5071
5072 return expression_list;
5073}
5074
5075/* Parse a delete-expression.
5076
5077 delete-expression:
5078 :: [opt] delete cast-expression
5079 :: [opt] delete [ ] cast-expression
5080
5081 Returns a representation of the expression. */
5082
5083static tree
94edc4ab 5084cp_parser_delete_expression (cp_parser* parser)
a723baf1
MM
5085{
5086 bool global_scope_p;
5087 bool array_p;
5088 tree expression;
5089
5090 /* Look for the optional `::' operator. */
5091 global_scope_p
5092 = (cp_parser_global_scope_opt (parser,
5093 /*current_scope_valid_p=*/false)
5094 != NULL_TREE);
5095 /* Look for the `delete' keyword. */
5096 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5097 /* See if the array syntax is in use. */
5098 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5099 {
5100 /* Consume the `[' token. */
5101 cp_lexer_consume_token (parser->lexer);
5102 /* Look for the `]' token. */
5103 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5104 /* Remember that this is the `[]' construct. */
5105 array_p = true;
5106 }
5107 else
5108 array_p = false;
5109
5110 /* Parse the cast-expression. */
d6b4ea85 5111 expression = cp_parser_simple_cast_expression (parser);
a723baf1 5112
625cbf93
MM
5113 /* A delete-expression may not appear in an integral constant
5114 expression. */
5115 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5116 return error_mark_node;
5117
a723baf1
MM
5118 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5119}
5120
5121/* Parse a cast-expression.
5122
5123 cast-expression:
5124 unary-expression
5125 ( type-id ) cast-expression
5126
5127 Returns a representation of the expression. */
5128
5129static tree
5130cp_parser_cast_expression (cp_parser *parser, bool address_p)
5131{
5132 /* If it's a `(', then we might be looking at a cast. */
5133 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5134 {
5135 tree type = NULL_TREE;
5136 tree expr = NULL_TREE;
5137 bool compound_literal_p;
5138 const char *saved_message;
5139
5140 /* There's no way to know yet whether or not this is a cast.
5141 For example, `(int (3))' is a unary-expression, while `(int)
5142 3' is a cast. So, we resort to parsing tentatively. */
5143 cp_parser_parse_tentatively (parser);
5144 /* Types may not be defined in a cast. */
5145 saved_message = parser->type_definition_forbidden_message;
5146 parser->type_definition_forbidden_message
5147 = "types may not be defined in casts";
5148 /* Consume the `('. */
5149 cp_lexer_consume_token (parser->lexer);
5150 /* A very tricky bit is that `(struct S) { 3 }' is a
5151 compound-literal (which we permit in C++ as an extension).
5152 But, that construct is not a cast-expression -- it is a
5153 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5154 is legal; if the compound-literal were a cast-expression,
5155 you'd need an extra set of parentheses.) But, if we parse
5156 the type-id, and it happens to be a class-specifier, then we
5157 will commit to the parse at that point, because we cannot
5158 undo the action that is done when creating a new class. So,
21526606 5159 then we cannot back up and do a postfix-expression.
a723baf1
MM
5160
5161 Therefore, we scan ahead to the closing `)', and check to see
5162 if the token after the `)' is a `{'. If so, we are not
21526606 5163 looking at a cast-expression.
a723baf1
MM
5164
5165 Save tokens so that we can put them back. */
5166 cp_lexer_save_tokens (parser->lexer);
5167 /* Skip tokens until the next token is a closing parenthesis.
5168 If we find the closing `)', and the next token is a `{', then
5169 we are looking at a compound-literal. */
21526606 5170 compound_literal_p
a668c6ad
MM
5171 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5172 /*consume_paren=*/true)
a723baf1
MM
5173 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5174 /* Roll back the tokens we skipped. */
5175 cp_lexer_rollback_tokens (parser->lexer);
5176 /* If we were looking at a compound-literal, simulate an error
5177 so that the call to cp_parser_parse_definitely below will
5178 fail. */
5179 if (compound_literal_p)
5180 cp_parser_simulate_error (parser);
5181 else
5182 {
4f8163b1
MM
5183 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5184 parser->in_type_id_in_expr_p = true;
a723baf1
MM
5185 /* Look for the type-id. */
5186 type = cp_parser_type_id (parser);
5187 /* Look for the closing `)'. */
5188 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4f8163b1 5189 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
5190 }
5191
5192 /* Restore the saved message. */
5193 parser->type_definition_forbidden_message = saved_message;
5194
bbaab916
NS
5195 /* If ok so far, parse the dependent expression. We cannot be
5196 sure it is a cast. Consider `(T ())'. It is a parenthesized
5197 ctor of T, but looks like a cast to function returning T
5198 without a dependent expression. */
5199 if (!cp_parser_error_occurred (parser))
d6b4ea85 5200 expr = cp_parser_simple_cast_expression (parser);
bbaab916 5201
a723baf1
MM
5202 if (cp_parser_parse_definitely (parser))
5203 {
a723baf1 5204 /* Warn about old-style casts, if so requested. */
21526606
EC
5205 if (warn_old_style_cast
5206 && !in_system_header
5207 && !VOID_TYPE_P (type)
a723baf1
MM
5208 && current_lang_name != lang_name_c)
5209 warning ("use of old-style cast");
14d22dd6
MM
5210
5211 /* Only type conversions to integral or enumeration types
5212 can be used in constant-expressions. */
67c03833 5213 if (parser->integral_constant_expression_p
14d22dd6 5214 && !dependent_type_p (type)
625cbf93 5215 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
98ca843c 5216 && (cp_parser_non_integral_constant_expression
625cbf93
MM
5217 (parser,
5218 "a cast to a type other than an integral or "
5219 "enumeration type")))
5220 return error_mark_node;
5221
a723baf1
MM
5222 /* Perform the cast. */
5223 expr = build_c_cast (type, expr);
bbaab916 5224 return expr;
a723baf1 5225 }
a723baf1
MM
5226 }
5227
5228 /* If we get here, then it's not a cast, so it must be a
5229 unary-expression. */
5230 return cp_parser_unary_expression (parser, address_p);
5231}
5232
b8b94c5b 5233/* Parse a binary expression of the general form:
a723baf1
MM
5234
5235 pm-expression:
5236 cast-expression
5237 pm-expression .* cast-expression
5238 pm-expression ->* cast-expression
5239
77077b39 5240 multiplicative-expression:
a723baf1
MM
5241 pm-expression
5242 multiplicative-expression * pm-expression
5243 multiplicative-expression / pm-expression
5244 multiplicative-expression % pm-expression
5245
a723baf1
MM
5246 additive-expression:
5247 multiplicative-expression
5248 additive-expression + multiplicative-expression
5249 additive-expression - multiplicative-expression
5250
a723baf1
MM
5251 shift-expression:
5252 additive-expression
5253 shift-expression << additive-expression
5254 shift-expression >> additive-expression
5255
a723baf1
MM
5256 relational-expression:
5257 shift-expression
5258 relational-expression < shift-expression
5259 relational-expression > shift-expression
5260 relational-expression <= shift-expression
5261 relational-expression >= shift-expression
5262
b8b94c5b
PB
5263 GNU Extension:
5264
a723baf1
MM
5265 relational-expression:
5266 relational-expression <? shift-expression
5267 relational-expression >? shift-expression
5268
a723baf1
MM
5269 equality-expression:
5270 relational-expression
5271 equality-expression == relational-expression
5272 equality-expression != relational-expression
5273
a723baf1
MM
5274 and-expression:
5275 equality-expression
5276 and-expression & equality-expression
5277
a723baf1
MM
5278 exclusive-or-expression:
5279 and-expression
5280 exclusive-or-expression ^ and-expression
5281
b8b94c5b
PB
5282 inclusive-or-expression:
5283 exclusive-or-expression
5284 inclusive-or-expression | exclusive-or-expression
a723baf1 5285
b8b94c5b
PB
5286 logical-and-expression:
5287 inclusive-or-expression
5288 logical-and-expression && inclusive-or-expression
a723baf1 5289
b8b94c5b
PB
5290 logical-or-expression:
5291 logical-and-expression
5292 logical-or-expression || logical-and-expression
a723baf1 5293
b8b94c5b 5294 All these are implemented with a single function like:
a723baf1 5295
b8b94c5b
PB
5296 binary-expression:
5297 simple-cast-expression
5298 binary-expression <token> binary-expression
a723baf1 5299
b8b94c5b
PB
5300 The binops_by_token map is used to get the tree codes for each <token> type.
5301 binary-expressions are associated according to a precedence table. */
a723baf1 5302
b8b94c5b
PB
5303#define TOKEN_PRECEDENCE(token) \
5304 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5305 ? PREC_NOT_OPERATOR \
5306 : binops_by_token[token->type].prec)
a723baf1
MM
5307
5308static tree
b8b94c5b 5309cp_parser_binary_expression (cp_parser* parser)
a723baf1 5310{
b8b94c5b
PB
5311 cp_parser_expression_stack stack;
5312 cp_parser_expression_stack_entry *sp = &stack[0];
5313 tree lhs, rhs;
5314 cp_token *token;
5315 enum tree_code tree_type;
5316 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5317 bool overloaded_p;
a723baf1 5318
b8b94c5b
PB
5319 /* Parse the first expression. */
5320 lhs = cp_parser_simple_cast_expression (parser);
a723baf1 5321
b8b94c5b
PB
5322 for (;;)
5323 {
5324 /* Get an operator token. */
5325 token = cp_lexer_peek_token (parser->lexer);
5326 new_prec = TOKEN_PRECEDENCE (token);
5327
5328 /* Popping an entry off the stack means we completed a subexpression:
5329 - either we found a token which is not an operator (`>' where it is not
5330 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5331 will happen repeatedly;
5332 - or, we found an operator which has lower priority. This is the case
5333 where the recursive descent *ascends*, as in `3 * 4 + 5' after
03fd3f84 5334 parsing `3 * 4'. */
b8b94c5b
PB
5335 if (new_prec <= prec)
5336 {
5337 if (sp == stack)
5338 break;
5339 else
5340 goto pop;
5341 }
a723baf1 5342
b8b94c5b
PB
5343 get_rhs:
5344 tree_type = binops_by_token[token->type].tree_type;
a723baf1 5345
03fd3f84 5346 /* We used the operator token. */
b8b94c5b 5347 cp_lexer_consume_token (parser->lexer);
a723baf1 5348
b8b94c5b
PB
5349 /* Extract another operand. It may be the RHS of this expression
5350 or the LHS of a new, higher priority expression. */
5351 rhs = cp_parser_simple_cast_expression (parser);
a723baf1 5352
b8b94c5b
PB
5353 /* Get another operator token. Look up its precedence to avoid
5354 building a useless (immediately popped) stack entry for common
03fd3f84 5355 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
b8b94c5b
PB
5356 token = cp_lexer_peek_token (parser->lexer);
5357 lookahead_prec = TOKEN_PRECEDENCE (token);
5358 if (lookahead_prec > new_prec)
5359 {
5360 /* ... and prepare to parse the RHS of the new, higher priority
43c2a69a
PB
5361 expression. Since precedence levels on the stack are
5362 monotonically increasing, we do not have to care about
5363 stack overflows. */
b8b94c5b
PB
5364 sp->prec = prec;
5365 sp->tree_type = tree_type;
5366 sp->lhs = lhs;
5367 sp++;
5368 lhs = rhs;
5369 prec = new_prec;
5370 new_prec = lookahead_prec;
5371 goto get_rhs;
5372
5373 pop:
5374 /* If the stack is not empty, we have parsed into LHS the right side
5375 (`4' in the example above) of an expression we had suspended.
5376 We can use the information on the stack to recover the LHS (`3')
5377 from the stack together with the tree code (`MULT_EXPR'), and
5378 the precedence of the higher level subexpression
5379 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5380 which will be used to actually build the additive expression. */
5381 --sp;
5382 prec = sp->prec;
5383 tree_type = sp->tree_type;
5384 rhs = lhs;
5385 lhs = sp->lhs;
5386 }
a723baf1 5387
b8b94c5b
PB
5388 overloaded_p = false;
5389 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
a723baf1 5390
b8b94c5b
PB
5391 /* If the binary operator required the use of an overloaded operator,
5392 then this expression cannot be an integral constant-expression.
5393 An overloaded operator can be used even if both operands are
5394 otherwise permissible in an integral constant-expression if at
5395 least one of the operands is of enumeration type. */
a723baf1 5396
b8b94c5b
PB
5397 if (overloaded_p
5398 && (cp_parser_non_integral_constant_expression
5399 (parser, "calls to overloaded operators")))
5400 return error_mark_node;
5401 }
a723baf1 5402
b8b94c5b 5403 return lhs;
a723baf1
MM
5404}
5405
b8b94c5b 5406
a723baf1
MM
5407/* Parse the `? expression : assignment-expression' part of a
5408 conditional-expression. The LOGICAL_OR_EXPR is the
5409 logical-or-expression that started the conditional-expression.
5410 Returns a representation of the entire conditional-expression.
5411
39703eb9 5412 This routine is used by cp_parser_assignment_expression.
a723baf1
MM
5413
5414 ? expression : assignment-expression
21526606 5415
a723baf1 5416 GNU Extensions:
21526606 5417
a723baf1
MM
5418 ? : assignment-expression */
5419
5420static tree
94edc4ab 5421cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
a723baf1
MM
5422{
5423 tree expr;
5424 tree assignment_expr;
5425
5426 /* Consume the `?' token. */
5427 cp_lexer_consume_token (parser->lexer);
5428 if (cp_parser_allow_gnu_extensions_p (parser)
5429 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5430 /* Implicit true clause. */
5431 expr = NULL_TREE;
5432 else
5433 /* Parse the expression. */
5434 expr = cp_parser_expression (parser);
21526606 5435
a723baf1
MM
5436 /* The next token should be a `:'. */
5437 cp_parser_require (parser, CPP_COLON, "`:'");
5438 /* Parse the assignment-expression. */
5439 assignment_expr = cp_parser_assignment_expression (parser);
5440
5441 /* Build the conditional-expression. */
5442 return build_x_conditional_expr (logical_or_expr,
5443 expr,
5444 assignment_expr);
5445}
5446
5447/* Parse an assignment-expression.
5448
5449 assignment-expression:
5450 conditional-expression
5451 logical-or-expression assignment-operator assignment_expression
5452 throw-expression
5453
5454 Returns a representation for the expression. */
5455
5456static tree
94edc4ab 5457cp_parser_assignment_expression (cp_parser* parser)
a723baf1
MM
5458{
5459 tree expr;
5460
5461 /* If the next token is the `throw' keyword, then we're looking at
5462 a throw-expression. */
5463 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5464 expr = cp_parser_throw_expression (parser);
5465 /* Otherwise, it must be that we are looking at a
5466 logical-or-expression. */
5467 else
5468 {
b8b94c5b
PB
5469 /* Parse the binary expressions (logical-or-expression). */
5470 expr = cp_parser_binary_expression (parser);
a723baf1
MM
5471 /* If the next token is a `?' then we're actually looking at a
5472 conditional-expression. */
5473 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5474 return cp_parser_question_colon_clause (parser, expr);
21526606 5475 else
a723baf1
MM
5476 {
5477 enum tree_code assignment_operator;
5478
5479 /* If it's an assignment-operator, we're using the second
5480 production. */
21526606 5481 assignment_operator
a723baf1
MM
5482 = cp_parser_assignment_operator_opt (parser);
5483 if (assignment_operator != ERROR_MARK)
5484 {
5485 tree rhs;
5486
5487 /* Parse the right-hand side of the assignment. */
5488 rhs = cp_parser_assignment_expression (parser);
14d22dd6
MM
5489 /* An assignment may not appear in a
5490 constant-expression. */
625cbf93
MM
5491 if (cp_parser_non_integral_constant_expression (parser,
5492 "an assignment"))
5493 return error_mark_node;
34cd5ae7 5494 /* Build the assignment expression. */
21526606
EC
5495 expr = build_x_modify_expr (expr,
5496 assignment_operator,
a723baf1
MM
5497 rhs);
5498 }
5499 }
5500 }
5501
5502 return expr;
5503}
5504
5505/* Parse an (optional) assignment-operator.
5506
21526606
EC
5507 assignment-operator: one of
5508 = *= /= %= += -= >>= <<= &= ^= |=
a723baf1
MM
5509
5510 GNU Extension:
21526606 5511
a723baf1
MM
5512 assignment-operator: one of
5513 <?= >?=
5514
5515 If the next token is an assignment operator, the corresponding tree
5516 code is returned, and the token is consumed. For example, for
5517 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5518 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5519 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5520 operator, ERROR_MARK is returned. */
5521
5522static enum tree_code
94edc4ab 5523cp_parser_assignment_operator_opt (cp_parser* parser)
a723baf1
MM
5524{
5525 enum tree_code op;
5526 cp_token *token;
5527
5528 /* Peek at the next toen. */
5529 token = cp_lexer_peek_token (parser->lexer);
5530
5531 switch (token->type)
5532 {
5533 case CPP_EQ:
5534 op = NOP_EXPR;
5535 break;
5536
5537 case CPP_MULT_EQ:
5538 op = MULT_EXPR;
5539 break;
5540
5541 case CPP_DIV_EQ:
5542 op = TRUNC_DIV_EXPR;
5543 break;
5544
5545 case CPP_MOD_EQ:
5546 op = TRUNC_MOD_EXPR;
5547 break;
5548
5549 case CPP_PLUS_EQ:
5550 op = PLUS_EXPR;
5551 break;
5552
5553 case CPP_MINUS_EQ:
5554 op = MINUS_EXPR;
5555 break;
5556
5557 case CPP_RSHIFT_EQ:
5558 op = RSHIFT_EXPR;
5559 break;
5560
5561 case CPP_LSHIFT_EQ:
5562 op = LSHIFT_EXPR;
5563 break;
5564
5565 case CPP_AND_EQ:
5566 op = BIT_AND_EXPR;
5567 break;
5568
5569 case CPP_XOR_EQ:
5570 op = BIT_XOR_EXPR;
5571 break;
5572
5573 case CPP_OR_EQ:
5574 op = BIT_IOR_EXPR;
5575 break;
5576
5577 case CPP_MIN_EQ:
5578 op = MIN_EXPR;
5579 break;
5580
5581 case CPP_MAX_EQ:
5582 op = MAX_EXPR;
5583 break;
5584
21526606 5585 default:
a723baf1
MM
5586 /* Nothing else is an assignment operator. */
5587 op = ERROR_MARK;
5588 }
5589
5590 /* If it was an assignment operator, consume it. */
5591 if (op != ERROR_MARK)
5592 cp_lexer_consume_token (parser->lexer);
5593
5594 return op;
5595}
5596
5597/* Parse an expression.
5598
5599 expression:
5600 assignment-expression
5601 expression , assignment-expression
5602
5603 Returns a representation of the expression. */
5604
5605static tree
94edc4ab 5606cp_parser_expression (cp_parser* parser)
a723baf1
MM
5607{
5608 tree expression = NULL_TREE;
a723baf1
MM
5609
5610 while (true)
5611 {
5612 tree assignment_expression;
5613
5614 /* Parse the next assignment-expression. */
21526606 5615 assignment_expression
a723baf1
MM
5616 = cp_parser_assignment_expression (parser);
5617 /* If this is the first assignment-expression, we can just
5618 save it away. */
5619 if (!expression)
5620 expression = assignment_expression;
a723baf1 5621 else
d17811fd
MM
5622 expression = build_x_compound_expr (expression,
5623 assignment_expression);
a723baf1
MM
5624 /* If the next token is not a comma, then we are done with the
5625 expression. */
5626 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5627 break;
5628 /* Consume the `,'. */
5629 cp_lexer_consume_token (parser->lexer);
14d22dd6 5630 /* A comma operator cannot appear in a constant-expression. */
625cbf93
MM
5631 if (cp_parser_non_integral_constant_expression (parser,
5632 "a comma operator"))
5633 expression = error_mark_node;
14d22dd6 5634 }
a723baf1
MM
5635
5636 return expression;
5637}
5638
21526606 5639/* Parse a constant-expression.
a723baf1
MM
5640
5641 constant-expression:
21526606 5642 conditional-expression
14d22dd6
MM
5643
5644 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
d17811fd
MM
5645 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5646 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5647 is false, NON_CONSTANT_P should be NULL. */
a723baf1
MM
5648
5649static tree
21526606 5650cp_parser_constant_expression (cp_parser* parser,
14d22dd6
MM
5651 bool allow_non_constant_p,
5652 bool *non_constant_p)
a723baf1 5653{
67c03833
JM
5654 bool saved_integral_constant_expression_p;
5655 bool saved_allow_non_integral_constant_expression_p;
5656 bool saved_non_integral_constant_expression_p;
a723baf1
MM
5657 tree expression;
5658
5659 /* It might seem that we could simply parse the
5660 conditional-expression, and then check to see if it were
5661 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5662 one that the compiler can figure out is constant, possibly after
5663 doing some simplifications or optimizations. The standard has a
5664 precise definition of constant-expression, and we must honor
5665 that, even though it is somewhat more restrictive.
5666
5667 For example:
5668
5669 int i[(2, 3)];
5670
5671 is not a legal declaration, because `(2, 3)' is not a
5672 constant-expression. The `,' operator is forbidden in a
5673 constant-expression. However, GCC's constant-folding machinery
5674 will fold this operation to an INTEGER_CST for `3'. */
5675
14d22dd6 5676 /* Save the old settings. */
67c03833 5677 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
21526606 5678 saved_allow_non_integral_constant_expression_p
67c03833
JM
5679 = parser->allow_non_integral_constant_expression_p;
5680 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
a723baf1 5681 /* We are now parsing a constant-expression. */
67c03833
JM
5682 parser->integral_constant_expression_p = true;
5683 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5684 parser->non_integral_constant_expression_p = false;
39703eb9
MM
5685 /* Although the grammar says "conditional-expression", we parse an
5686 "assignment-expression", which also permits "throw-expression"
5687 and the use of assignment operators. In the case that
5688 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5689 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5690 actually essential that we look for an assignment-expression.
5691 For example, cp_parser_initializer_clauses uses this function to
5692 determine whether a particular assignment-expression is in fact
5693 constant. */
5694 expression = cp_parser_assignment_expression (parser);
14d22dd6 5695 /* Restore the old settings. */
67c03833 5696 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
21526606 5697 parser->allow_non_integral_constant_expression_p
67c03833 5698 = saved_allow_non_integral_constant_expression_p;
14d22dd6 5699 if (allow_non_constant_p)
67c03833
JM
5700 *non_constant_p = parser->non_integral_constant_expression_p;
5701 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
a723baf1
MM
5702
5703 return expression;
5704}
5705
7a3ea201
RH
5706/* Parse __builtin_offsetof.
5707
5708 offsetof-expression:
5709 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5710
5711 offsetof-member-designator:
5712 id-expression
5713 | offsetof-member-designator "." id-expression
5714 | offsetof-member-designator "[" expression "]"
5715*/
5716
5717static tree
5718cp_parser_builtin_offsetof (cp_parser *parser)
5719{
5720 int save_ice_p, save_non_ice_p;
5721 tree type, expr;
5722 cp_id_kind dummy;
5723
5724 /* We're about to accept non-integral-constant things, but will
5725 definitely yield an integral constant expression. Save and
5726 restore these values around our local parsing. */
5727 save_ice_p = parser->integral_constant_expression_p;
5728 save_non_ice_p = parser->non_integral_constant_expression_p;
5729
5730 /* Consume the "__builtin_offsetof" token. */
5731 cp_lexer_consume_token (parser->lexer);
5732 /* Consume the opening `('. */
5733 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5734 /* Parse the type-id. */
5735 type = cp_parser_type_id (parser);
5736 /* Look for the `,'. */
5737 cp_parser_require (parser, CPP_COMMA, "`,'");
5738
5739 /* Build the (type *)null that begins the traditional offsetof macro. */
5740 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5741
5742 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5743 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5744 true, &dummy);
5745 while (true)
5746 {
5747 cp_token *token = cp_lexer_peek_token (parser->lexer);
5748 switch (token->type)
5749 {
5750 case CPP_OPEN_SQUARE:
5751 /* offsetof-member-designator "[" expression "]" */
5752 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5753 break;
5754
5755 case CPP_DOT:
5756 /* offsetof-member-designator "." identifier */
5757 cp_lexer_consume_token (parser->lexer);
5758 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5759 true, &dummy);
5760 break;
5761
5762 case CPP_CLOSE_PAREN:
5763 /* Consume the ")" token. */
5764 cp_lexer_consume_token (parser->lexer);
5765 goto success;
5766
5767 default:
5768 /* Error. We know the following require will fail, but
5769 that gives the proper error message. */
5770 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5771 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5772 expr = error_mark_node;
5773 goto failure;
5774 }
5775 }
5776
5777 success:
42c244d8
RH
5778 /* If we're processing a template, we can't finish the semantics yet.
5779 Otherwise we can fold the entire expression now. */
5780 if (processing_template_decl)
5781 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5782 else
5783 expr = fold_offsetof (expr);
7a3ea201
RH
5784
5785 failure:
5786 parser->integral_constant_expression_p = save_ice_p;
5787 parser->non_integral_constant_expression_p = save_non_ice_p;
5788
5789 return expr;
5790}
5791
a723baf1
MM
5792/* Statements [gram.stmt.stmt] */
5793
21526606 5794/* Parse a statement.
a723baf1
MM
5795
5796 statement:
5797 labeled-statement
5798 expression-statement
5799 compound-statement
5800 selection-statement
5801 iteration-statement
5802 jump-statement
5803 declaration-statement
5804 try-block */
5805
5806static void
325c3691 5807cp_parser_statement (cp_parser* parser, tree in_statement_expr)
a723baf1
MM
5808{
5809 tree statement;
5810 cp_token *token;
93409b8c 5811 location_t statement_location;
a723baf1
MM
5812
5813 /* There is no statement yet. */
5814 statement = NULL_TREE;
5815 /* Peek at the next token. */
5816 token = cp_lexer_peek_token (parser->lexer);
6de9cd9a 5817 /* Remember the location of the first token in the statement. */
93409b8c 5818 statement_location = token->location;
a723baf1
MM
5819 /* If this is a keyword, then that will often determine what kind of
5820 statement we have. */
5821 if (token->type == CPP_KEYWORD)
5822 {
5823 enum rid keyword = token->keyword;
5824
5825 switch (keyword)
5826 {
5827 case RID_CASE:
5828 case RID_DEFAULT:
a5bcc582 5829 statement = cp_parser_labeled_statement (parser,
325c3691 5830 in_statement_expr);
a723baf1
MM
5831 break;
5832
5833 case RID_IF:
5834 case RID_SWITCH:
5835 statement = cp_parser_selection_statement (parser);
5836 break;
5837
5838 case RID_WHILE:
5839 case RID_DO:
5840 case RID_FOR:
5841 statement = cp_parser_iteration_statement (parser);
5842 break;
5843
5844 case RID_BREAK:
5845 case RID_CONTINUE:
5846 case RID_RETURN:
5847 case RID_GOTO:
5848 statement = cp_parser_jump_statement (parser);
5849 break;
5850
5851 case RID_TRY:
5852 statement = cp_parser_try_block (parser);
5853 break;
5854
5855 default:
5856 /* It might be a keyword like `int' that can start a
5857 declaration-statement. */
5858 break;
5859 }
5860 }
5861 else if (token->type == CPP_NAME)
5862 {
5863 /* If the next token is a `:', then we are looking at a
5864 labeled-statement. */
5865 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5866 if (token->type == CPP_COLON)
325c3691 5867 statement = cp_parser_labeled_statement (parser, in_statement_expr);
a723baf1
MM
5868 }
5869 /* Anything that starts with a `{' must be a compound-statement. */
5870 else if (token->type == CPP_OPEN_BRACE)
325c3691 5871 statement = cp_parser_compound_statement (parser, NULL, false);
36952dea
ZW
5872 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5873 a statement all its own. */
5874 else if (token->type == CPP_PRAGMA)
5875 {
5876 cp_lexer_handle_pragma (parser->lexer);
5877 return;
5878 }
a723baf1
MM
5879
5880 /* Everything else must be a declaration-statement or an
21526606 5881 expression-statement. Try for the declaration-statement
a723baf1
MM
5882 first, unless we are looking at a `;', in which case we know that
5883 we have an expression-statement. */
5884 if (!statement)
5885 {
5886 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5887 {
5888 cp_parser_parse_tentatively (parser);
5889 /* Try to parse the declaration-statement. */
5890 cp_parser_declaration_statement (parser);
5891 /* If that worked, we're done. */
5892 if (cp_parser_parse_definitely (parser))
5893 return;
5894 }
5895 /* Look for an expression-statement instead. */
325c3691 5896 statement = cp_parser_expression_statement (parser, in_statement_expr);
a723baf1
MM
5897 }
5898
5899 /* Set the line number for the statement. */
009ed910 5900 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
93409b8c 5901 SET_EXPR_LOCATION (statement, statement_location);
a723baf1
MM
5902}
5903
5904/* Parse a labeled-statement.
5905
5906 labeled-statement:
5907 identifier : statement
5908 case constant-expression : statement
98ce043b
MM
5909 default : statement
5910
5911 GNU Extension:
21526606 5912
98ce043b
MM
5913 labeled-statement:
5914 case constant-expression ... constant-expression : statement
a723baf1 5915
8c161995
RH
5916 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5917 For an ordinary label, returns a LABEL_EXPR. */
a723baf1
MM
5918
5919static tree
325c3691 5920cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
a723baf1
MM
5921{
5922 cp_token *token;
0e59b3fb 5923 tree statement = error_mark_node;
a723baf1
MM
5924
5925 /* The next token should be an identifier. */
5926 token = cp_lexer_peek_token (parser->lexer);
5927 if (token->type != CPP_NAME
5928 && token->type != CPP_KEYWORD)
5929 {
5930 cp_parser_error (parser, "expected labeled-statement");
5931 return error_mark_node;
5932 }
5933
5934 switch (token->keyword)
5935 {
5936 case RID_CASE:
5937 {
98ce043b
MM
5938 tree expr, expr_hi;
5939 cp_token *ellipsis;
a723baf1
MM
5940
5941 /* Consume the `case' token. */
5942 cp_lexer_consume_token (parser->lexer);
5943 /* Parse the constant-expression. */
21526606 5944 expr = cp_parser_constant_expression (parser,
d17811fd 5945 /*allow_non_constant_p=*/false,
14d22dd6 5946 NULL);
98ce043b
MM
5947
5948 ellipsis = cp_lexer_peek_token (parser->lexer);
5949 if (ellipsis->type == CPP_ELLIPSIS)
5950 {
5951 /* Consume the `...' token. */
5952 cp_lexer_consume_token (parser->lexer);
5953 expr_hi =
5954 cp_parser_constant_expression (parser,
5955 /*allow_non_constant_p=*/false,
5956 NULL);
5957 /* We don't need to emit warnings here, as the common code
5958 will do this for us. */
5959 }
5960 else
5961 expr_hi = NULL_TREE;
5962
0e59b3fb 5963 if (!parser->in_switch_statement_p)
2a13a625 5964 error ("case label %qE not within a switch statement", expr);
0e59b3fb 5965 else
98ce043b 5966 statement = finish_case_label (expr, expr_hi);
a723baf1
MM
5967 }
5968 break;
5969
5970 case RID_DEFAULT:
5971 /* Consume the `default' token. */
5972 cp_lexer_consume_token (parser->lexer);
0e59b3fb
MM
5973 if (!parser->in_switch_statement_p)
5974 error ("case label not within a switch statement");
5975 else
5976 statement = finish_case_label (NULL_TREE, NULL_TREE);
a723baf1
MM
5977 break;
5978
5979 default:
5980 /* Anything else must be an ordinary label. */
5981 statement = finish_label_stmt (cp_parser_identifier (parser));
5982 break;
5983 }
5984
5985 /* Require the `:' token. */
5986 cp_parser_require (parser, CPP_COLON, "`:'");
5987 /* Parse the labeled statement. */
325c3691 5988 cp_parser_statement (parser, in_statement_expr);
a723baf1
MM
5989
5990 /* Return the label, in the case of a `case' or `default' label. */
5991 return statement;
5992}
5993
5994/* Parse an expression-statement.
5995
5996 expression-statement:
5997 expression [opt] ;
5998
5999 Returns the new EXPR_STMT -- or NULL_TREE if the expression
a5bcc582
NS
6000 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6001 indicates whether this expression-statement is part of an
6002 expression statement. */
a723baf1
MM
6003
6004static tree
325c3691 6005cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
a723baf1 6006{
a5bcc582 6007 tree statement = NULL_TREE;
a723baf1 6008
a5bcc582 6009 /* If the next token is a ';', then there is no expression
04c06002 6010 statement. */
a723baf1 6011 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
a5bcc582 6012 statement = cp_parser_expression (parser);
21526606 6013
a723baf1 6014 /* Consume the final `;'. */
e0860732 6015 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1 6016
325c3691 6017 if (in_statement_expr
a5bcc582
NS
6018 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6019 {
6020 /* This is the final expression statement of a statement
6021 expression. */
325c3691 6022 statement = finish_stmt_expr_expr (statement, in_statement_expr);
a5bcc582
NS
6023 }
6024 else if (statement)
6025 statement = finish_expr_stmt (statement);
6026 else
6027 finish_stmt ();
21526606 6028
a723baf1
MM
6029 return statement;
6030}
6031
6032/* Parse a compound-statement.
6033
6034 compound-statement:
6035 { statement-seq [opt] }
21526606 6036
5882f0f3 6037 Returns a tree representing the statement. */
a723baf1
MM
6038
6039static tree
325c3691
RH
6040cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6041 bool in_try)
a723baf1
MM
6042{
6043 tree compound_stmt;
6044
6045 /* Consume the `{'. */
6046 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6047 return error_mark_node;
6048 /* Begin the compound-statement. */
325c3691 6049 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
a723baf1 6050 /* Parse an (optional) statement-seq. */
325c3691 6051 cp_parser_statement_seq_opt (parser, in_statement_expr);
a723baf1 6052 /* Finish the compound-statement. */
7a3397c7 6053 finish_compound_stmt (compound_stmt);
a723baf1
MM
6054 /* Consume the `}'. */
6055 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6056
6057 return compound_stmt;
6058}
6059
6060/* Parse an (optional) statement-seq.
6061
6062 statement-seq:
6063 statement
6064 statement-seq [opt] statement */
6065
6066static void
325c3691 6067cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
a723baf1
MM
6068{
6069 /* Scan statements until there aren't any more. */
6070 while (true)
6071 {
6072 /* If we're looking at a `}', then we've run out of statements. */
6073 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6074 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6075 break;
6076
6077 /* Parse the statement. */
325c3691 6078 cp_parser_statement (parser, in_statement_expr);
a723baf1
MM
6079 }
6080}
6081
6082/* Parse a selection-statement.
6083
6084 selection-statement:
6085 if ( condition ) statement
6086 if ( condition ) statement else statement
21526606 6087 switch ( condition ) statement
a723baf1
MM
6088
6089 Returns the new IF_STMT or SWITCH_STMT. */
6090
6091static tree
94edc4ab 6092cp_parser_selection_statement (cp_parser* parser)
a723baf1
MM
6093{
6094 cp_token *token;
6095 enum rid keyword;
6096
6097 /* Peek at the next token. */
6098 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6099
6100 /* See what kind of keyword it is. */
6101 keyword = token->keyword;
6102 switch (keyword)
6103 {
6104 case RID_IF:
6105 case RID_SWITCH:
6106 {
6107 tree statement;
6108 tree condition;
6109
6110 /* Look for the `('. */
6111 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6112 {
6113 cp_parser_skip_to_end_of_statement (parser);
6114 return error_mark_node;
6115 }
6116
6117 /* Begin the selection-statement. */
6118 if (keyword == RID_IF)
6119 statement = begin_if_stmt ();
6120 else
6121 statement = begin_switch_stmt ();
6122
6123 /* Parse the condition. */
6124 condition = cp_parser_condition (parser);
6125 /* Look for the `)'. */
6126 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
6127 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6128 /*consume_paren=*/true);
a723baf1
MM
6129
6130 if (keyword == RID_IF)
6131 {
a723baf1
MM
6132 /* Add the condition. */
6133 finish_if_stmt_cond (condition, statement);
6134
6135 /* Parse the then-clause. */
325c3691 6136 cp_parser_implicitly_scoped_statement (parser);
a723baf1
MM
6137 finish_then_clause (statement);
6138
6139 /* If the next token is `else', parse the else-clause. */
6140 if (cp_lexer_next_token_is_keyword (parser->lexer,
6141 RID_ELSE))
6142 {
a723baf1
MM
6143 /* Consume the `else' keyword. */
6144 cp_lexer_consume_token (parser->lexer);
325c3691 6145 begin_else_clause (statement);
a723baf1 6146 /* Parse the else-clause. */
325c3691 6147 cp_parser_implicitly_scoped_statement (parser);
a723baf1
MM
6148 finish_else_clause (statement);
6149 }
6150
6151 /* Now we're all done with the if-statement. */
325c3691 6152 finish_if_stmt (statement);
a723baf1
MM
6153 }
6154 else
6155 {
0e59b3fb 6156 bool in_switch_statement_p;
a723baf1
MM
6157
6158 /* Add the condition. */
6159 finish_switch_cond (condition, statement);
6160
6161 /* Parse the body of the switch-statement. */
0e59b3fb
MM
6162 in_switch_statement_p = parser->in_switch_statement_p;
6163 parser->in_switch_statement_p = true;
325c3691 6164 cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 6165 parser->in_switch_statement_p = in_switch_statement_p;
a723baf1
MM
6166
6167 /* Now we're all done with the switch-statement. */
6168 finish_switch_stmt (statement);
6169 }
6170
6171 return statement;
6172 }
6173 break;
6174
6175 default:
6176 cp_parser_error (parser, "expected selection-statement");
6177 return error_mark_node;
6178 }
6179}
6180
21526606 6181/* Parse a condition.
a723baf1
MM
6182
6183 condition:
6184 expression
21526606 6185 type-specifier-seq declarator = assignment-expression
a723baf1
MM
6186
6187 GNU Extension:
21526606 6188
a723baf1 6189 condition:
21526606 6190 type-specifier-seq declarator asm-specification [opt]
a723baf1 6191 attributes [opt] = assignment-expression
21526606 6192
a723baf1
MM
6193 Returns the expression that should be tested. */
6194
6195static tree
94edc4ab 6196cp_parser_condition (cp_parser* parser)
a723baf1 6197{
62d1db17 6198 cp_decl_specifier_seq type_specifiers;
a723baf1
MM
6199 const char *saved_message;
6200
6201 /* Try the declaration first. */
6202 cp_parser_parse_tentatively (parser);
6203 /* New types are not allowed in the type-specifier-seq for a
6204 condition. */
6205 saved_message = parser->type_definition_forbidden_message;
6206 parser->type_definition_forbidden_message
6207 = "types may not be defined in conditions";
6208 /* Parse the type-specifier-seq. */
62d1db17 6209 cp_parser_type_specifier_seq (parser, &type_specifiers);
a723baf1
MM
6210 /* Restore the saved message. */
6211 parser->type_definition_forbidden_message = saved_message;
6212 /* If all is well, we might be looking at a declaration. */
6213 if (!cp_parser_error_occurred (parser))
6214 {
6215 tree decl;
6216 tree asm_specification;
6217 tree attributes;
058b15c1 6218 cp_declarator *declarator;
a723baf1 6219 tree initializer = NULL_TREE;
21526606 6220
a723baf1 6221 /* Parse the declarator. */
62b8a44e 6222 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 6223 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
6224 /*parenthesized_p=*/NULL,
6225 /*member_p=*/false);
a723baf1
MM
6226 /* Parse the attributes. */
6227 attributes = cp_parser_attributes_opt (parser);
6228 /* Parse the asm-specification. */
6229 asm_specification = cp_parser_asm_specification_opt (parser);
6230 /* If the next token is not an `=', then we might still be
6231 looking at an expression. For example:
21526606 6232
a723baf1 6233 if (A(a).x)
21526606 6234
a723baf1
MM
6235 looks like a decl-specifier-seq and a declarator -- but then
6236 there is no `=', so this is an expression. */
6237 cp_parser_require (parser, CPP_EQ, "`='");
6238 /* If we did see an `=', then we are looking at a declaration
6239 for sure. */
6240 if (cp_parser_parse_definitely (parser))
6241 {
4514aa8c 6242 tree pushed_scope;
73a8adb6 6243
a723baf1 6244 /* Create the declaration. */
62d1db17 6245 decl = start_decl (declarator, &type_specifiers,
a723baf1 6246 /*initialized_p=*/true,
73a8adb6 6247 attributes, /*prefix_attributes=*/NULL_TREE,
4514aa8c 6248 &pushed_scope);
a723baf1
MM
6249 /* Parse the assignment-expression. */
6250 initializer = cp_parser_assignment_expression (parser);
21526606 6251
a723baf1 6252 /* Process the initializer. */
21526606
EC
6253 cp_finish_decl (decl,
6254 initializer,
6255 asm_specification,
a723baf1 6256 LOOKUP_ONLYCONVERTING);
c162c75e 6257
4514aa8c
NS
6258 if (pushed_scope)
6259 pop_scope (pushed_scope);
21526606 6260
a723baf1
MM
6261 return convert_from_reference (decl);
6262 }
6263 }
6264 /* If we didn't even get past the declarator successfully, we are
6265 definitely not looking at a declaration. */
6266 else
6267 cp_parser_abort_tentative_parse (parser);
6268
6269 /* Otherwise, we are looking at an expression. */
6270 return cp_parser_expression (parser);
6271}
6272
6273/* Parse an iteration-statement.
6274
6275 iteration-statement:
6276 while ( condition ) statement
6277 do statement while ( expression ) ;
6278 for ( for-init-statement condition [opt] ; expression [opt] )
6279 statement
6280
6281 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6282
6283static tree
94edc4ab 6284cp_parser_iteration_statement (cp_parser* parser)
a723baf1
MM
6285{
6286 cp_token *token;
6287 enum rid keyword;
6288 tree statement;
0e59b3fb
MM
6289 bool in_iteration_statement_p;
6290
a723baf1
MM
6291
6292 /* Peek at the next token. */
6293 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6294 if (!token)
6295 return error_mark_node;
6296
0e59b3fb 6297 /* Remember whether or not we are already within an iteration
21526606 6298 statement. */
0e59b3fb
MM
6299 in_iteration_statement_p = parser->in_iteration_statement_p;
6300
a723baf1
MM
6301 /* See what kind of keyword it is. */
6302 keyword = token->keyword;
6303 switch (keyword)
6304 {
6305 case RID_WHILE:
6306 {
6307 tree condition;
6308
6309 /* Begin the while-statement. */
6310 statement = begin_while_stmt ();
6311 /* Look for the `('. */
6312 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6313 /* Parse the condition. */
6314 condition = cp_parser_condition (parser);
6315 finish_while_stmt_cond (condition, statement);
6316 /* Look for the `)'. */
6317 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6318 /* Parse the dependent statement. */
0e59b3fb 6319 parser->in_iteration_statement_p = true;
a723baf1 6320 cp_parser_already_scoped_statement (parser);
0e59b3fb 6321 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
6322 /* We're done with the while-statement. */
6323 finish_while_stmt (statement);
6324 }
6325 break;
6326
6327 case RID_DO:
6328 {
6329 tree expression;
6330
6331 /* Begin the do-statement. */
6332 statement = begin_do_stmt ();
6333 /* Parse the body of the do-statement. */
0e59b3fb 6334 parser->in_iteration_statement_p = true;
a723baf1 6335 cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 6336 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
6337 finish_do_body (statement);
6338 /* Look for the `while' keyword. */
6339 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6340 /* Look for the `('. */
6341 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6342 /* Parse the expression. */
6343 expression = cp_parser_expression (parser);
6344 /* We're done with the do-statement. */
6345 finish_do_stmt (expression, statement);
6346 /* Look for the `)'. */
6347 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6348 /* Look for the `;'. */
6349 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6350 }
6351 break;
6352
6353 case RID_FOR:
6354 {
6355 tree condition = NULL_TREE;
6356 tree expression = NULL_TREE;
6357
6358 /* Begin the for-statement. */
6359 statement = begin_for_stmt ();
6360 /* Look for the `('. */
6361 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6362 /* Parse the initialization. */
6363 cp_parser_for_init_statement (parser);
6364 finish_for_init_stmt (statement);
6365
6366 /* If there's a condition, process it. */
6367 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6368 condition = cp_parser_condition (parser);
6369 finish_for_cond (condition, statement);
6370 /* Look for the `;'. */
6371 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6372
6373 /* If there's an expression, process it. */
6374 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6375 expression = cp_parser_expression (parser);
6376 finish_for_expr (expression, statement);
6377 /* Look for the `)'. */
d5a10cf0 6378 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
98ca843c 6379
a723baf1 6380 /* Parse the body of the for-statement. */
0e59b3fb 6381 parser->in_iteration_statement_p = true;
a723baf1 6382 cp_parser_already_scoped_statement (parser);
0e59b3fb 6383 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
6384
6385 /* We're done with the for-statement. */
6386 finish_for_stmt (statement);
6387 }
6388 break;
6389
6390 default:
6391 cp_parser_error (parser, "expected iteration-statement");
6392 statement = error_mark_node;
6393 break;
6394 }
6395
6396 return statement;
6397}
6398
6399/* Parse a for-init-statement.
6400
6401 for-init-statement:
6402 expression-statement
6403 simple-declaration */
6404
6405static void
94edc4ab 6406cp_parser_for_init_statement (cp_parser* parser)
a723baf1
MM
6407{
6408 /* If the next token is a `;', then we have an empty
34cd5ae7 6409 expression-statement. Grammatically, this is also a
a723baf1
MM
6410 simple-declaration, but an invalid one, because it does not
6411 declare anything. Therefore, if we did not handle this case
6412 specially, we would issue an error message about an invalid
6413 declaration. */
6414 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6415 {
6416 /* We're going to speculatively look for a declaration, falling back
6417 to an expression, if necessary. */
6418 cp_parser_parse_tentatively (parser);
6419 /* Parse the declaration. */
6420 cp_parser_simple_declaration (parser,
6421 /*function_definition_allowed_p=*/false);
6422 /* If the tentative parse failed, then we shall need to look for an
6423 expression-statement. */
6424 if (cp_parser_parse_definitely (parser))
6425 return;
6426 }
6427
a5bcc582 6428 cp_parser_expression_statement (parser, false);
a723baf1
MM
6429}
6430
6431/* Parse a jump-statement.
6432
6433 jump-statement:
6434 break ;
6435 continue ;
6436 return expression [opt] ;
21526606 6437 goto identifier ;
a723baf1
MM
6438
6439 GNU extension:
6440
6441 jump-statement:
6442 goto * expression ;
6443
5088b058 6444 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
a723baf1
MM
6445
6446static tree
94edc4ab 6447cp_parser_jump_statement (cp_parser* parser)
a723baf1
MM
6448{
6449 tree statement = error_mark_node;
6450 cp_token *token;
6451 enum rid keyword;
6452
6453 /* Peek at the next token. */
6454 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6455 if (!token)
6456 return error_mark_node;
6457
6458 /* See what kind of keyword it is. */
6459 keyword = token->keyword;
6460 switch (keyword)
6461 {
6462 case RID_BREAK:
0e59b3fb
MM
6463 if (!parser->in_switch_statement_p
6464 && !parser->in_iteration_statement_p)
6465 {
6466 error ("break statement not within loop or switch");
6467 statement = error_mark_node;
6468 }
6469 else
6470 statement = finish_break_stmt ();
2a13a625 6471 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6472 break;
6473
6474 case RID_CONTINUE:
0e59b3fb
MM
6475 if (!parser->in_iteration_statement_p)
6476 {
6477 error ("continue statement not within a loop");
6478 statement = error_mark_node;
6479 }
6480 else
6481 statement = finish_continue_stmt ();
2a13a625 6482 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6483 break;
6484
6485 case RID_RETURN:
6486 {
6487 tree expr;
6488
21526606 6489 /* If the next token is a `;', then there is no
a723baf1
MM
6490 expression. */
6491 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6492 expr = cp_parser_expression (parser);
6493 else
6494 expr = NULL_TREE;
6495 /* Build the return-statement. */
6496 statement = finish_return_stmt (expr);
6497 /* Look for the final `;'. */
2a13a625 6498 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6499 }
6500 break;
6501
6502 case RID_GOTO:
6503 /* Create the goto-statement. */
6504 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6505 {
6506 /* Issue a warning about this use of a GNU extension. */
6507 if (pedantic)
6508 pedwarn ("ISO C++ forbids computed gotos");
6509 /* Consume the '*' token. */
6510 cp_lexer_consume_token (parser->lexer);
6511 /* Parse the dependent expression. */
6512 finish_goto_stmt (cp_parser_expression (parser));
6513 }
6514 else
6515 finish_goto_stmt (cp_parser_identifier (parser));
6516 /* Look for the final `;'. */
2a13a625 6517 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6518 break;
6519
6520 default:
6521 cp_parser_error (parser, "expected jump-statement");
6522 break;
6523 }
6524
6525 return statement;
6526}
6527
6528/* Parse a declaration-statement.
6529
6530 declaration-statement:
6531 block-declaration */
6532
6533static void
94edc4ab 6534cp_parser_declaration_statement (cp_parser* parser)
a723baf1 6535{
058b15c1
MM
6536 void *p;
6537
6538 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6539 p = obstack_alloc (&declarator_obstack, 0);
6540
6541 /* Parse the block-declaration. */
a723baf1
MM
6542 cp_parser_block_declaration (parser, /*statement_p=*/true);
6543
058b15c1
MM
6544 /* Free any declarators allocated. */
6545 obstack_free (&declarator_obstack, p);
6546
a723baf1
MM
6547 /* Finish off the statement. */
6548 finish_stmt ();
6549}
6550
6551/* Some dependent statements (like `if (cond) statement'), are
6552 implicitly in their own scope. In other words, if the statement is
6553 a single statement (as opposed to a compound-statement), it is
6554 none-the-less treated as if it were enclosed in braces. Any
6555 declarations appearing in the dependent statement are out of scope
6556 after control passes that point. This function parses a statement,
6557 but ensures that is in its own scope, even if it is not a
21526606 6558 compound-statement.
a723baf1
MM
6559
6560 Returns the new statement. */
6561
6562static tree
94edc4ab 6563cp_parser_implicitly_scoped_statement (cp_parser* parser)
a723baf1
MM
6564{
6565 tree statement;
6566
6567 /* If the token is not a `{', then we must take special action. */
6568 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6569 {
6570 /* Create a compound-statement. */
325c3691 6571 statement = begin_compound_stmt (0);
a723baf1 6572 /* Parse the dependent-statement. */
a5bcc582 6573 cp_parser_statement (parser, false);
a723baf1 6574 /* Finish the dummy compound-statement. */
7a3397c7 6575 finish_compound_stmt (statement);
a723baf1
MM
6576 }
6577 /* Otherwise, we simply parse the statement directly. */
6578 else
325c3691 6579 statement = cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
6580
6581 /* Return the statement. */
6582 return statement;
6583}
6584
6585/* For some dependent statements (like `while (cond) statement'), we
6586 have already created a scope. Therefore, even if the dependent
6587 statement is a compound-statement, we do not want to create another
6588 scope. */
6589
6590static void
94edc4ab 6591cp_parser_already_scoped_statement (cp_parser* parser)
a723baf1 6592{
325c3691
RH
6593 /* If the token is a `{', then we must take special action. */
6594 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6595 cp_parser_statement (parser, false);
6596 else
a723baf1 6597 {
325c3691
RH
6598 /* Avoid calling cp_parser_compound_statement, so that we
6599 don't create a new scope. Do everything else by hand. */
6600 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6601 cp_parser_statement_seq_opt (parser, false);
6602 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1 6603 }
a723baf1
MM
6604}
6605
6606/* Declarations [gram.dcl.dcl] */
6607
6608/* Parse an optional declaration-sequence.
6609
6610 declaration-seq:
6611 declaration
6612 declaration-seq declaration */
6613
6614static void
94edc4ab 6615cp_parser_declaration_seq_opt (cp_parser* parser)
a723baf1
MM
6616{
6617 while (true)
6618 {
6619 cp_token *token;
6620
6621 token = cp_lexer_peek_token (parser->lexer);
6622
6623 if (token->type == CPP_CLOSE_BRACE
6624 || token->type == CPP_EOF)
6625 break;
6626
21526606 6627 if (token->type == CPP_SEMICOLON)
a723baf1
MM
6628 {
6629 /* A declaration consisting of a single semicolon is
6630 invalid. Allow it unless we're being pedantic. */
a723baf1 6631 cp_lexer_consume_token (parser->lexer);
2cfe82fe
ZW
6632 if (pedantic && !in_system_header)
6633 pedwarn ("extra %<;%>");
a723baf1
MM
6634 continue;
6635 }
6636
7d381002 6637 /* If we're entering or exiting a region that's implicitly
03fd3f84 6638 extern "C", modify the lang context appropriately. */
7d381002
MA
6639 if (!parser->implicit_extern_c && token->implicit_extern_c)
6640 {
6641 push_lang_context (lang_name_c);
6642 parser->implicit_extern_c = true;
6643 }
6644 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6645 {
6646 pop_lang_context ();
6647 parser->implicit_extern_c = false;
6648 }
6649
36952dea
ZW
6650 if (token->type == CPP_PRAGMA)
6651 {
6652 /* A top-level declaration can consist solely of a #pragma.
6653 A nested declaration cannot, so this is done here and not
6654 in cp_parser_declaration. (A #pragma at block scope is
6655 handled in cp_parser_statement.) */
6656 cp_lexer_handle_pragma (parser->lexer);
6657 continue;
6658 }
6659
c838d82f 6660 /* Parse the declaration itself. */
a723baf1
MM
6661 cp_parser_declaration (parser);
6662 }
6663}
6664
6665/* Parse a declaration.
6666
6667 declaration:
6668 block-declaration
6669 function-definition
6670 template-declaration
6671 explicit-instantiation
6672 explicit-specialization
6673 linkage-specification
21526606 6674 namespace-definition
1092805d
MM
6675
6676 GNU extension:
6677
6678 declaration:
6679 __extension__ declaration */
a723baf1
MM
6680
6681static void
94edc4ab 6682cp_parser_declaration (cp_parser* parser)
a723baf1
MM
6683{
6684 cp_token token1;
6685 cp_token token2;
1092805d 6686 int saved_pedantic;
058b15c1 6687 void *p;
1092805d
MM
6688
6689 /* Check for the `__extension__' keyword. */
6690 if (cp_parser_extension_opt (parser, &saved_pedantic))
6691 {
6692 /* Parse the qualified declaration. */
6693 cp_parser_declaration (parser);
6694 /* Restore the PEDANTIC flag. */
6695 pedantic = saved_pedantic;
6696
6697 return;
6698 }
a723baf1
MM
6699
6700 /* Try to figure out what kind of declaration is present. */
6701 token1 = *cp_lexer_peek_token (parser->lexer);
21526606 6702
a723baf1
MM
6703 if (token1.type != CPP_EOF)
6704 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6705
058b15c1
MM
6706 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6707 p = obstack_alloc (&declarator_obstack, 0);
6708
a723baf1
MM
6709 /* If the next token is `extern' and the following token is a string
6710 literal, then we have a linkage specification. */
6711 if (token1.keyword == RID_EXTERN
6712 && cp_parser_is_string_literal (&token2))
6713 cp_parser_linkage_specification (parser);
6714 /* If the next token is `template', then we have either a template
6715 declaration, an explicit instantiation, or an explicit
6716 specialization. */
6717 else if (token1.keyword == RID_TEMPLATE)
6718 {
6719 /* `template <>' indicates a template specialization. */
6720 if (token2.type == CPP_LESS
6721 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6722 cp_parser_explicit_specialization (parser);
6723 /* `template <' indicates a template declaration. */
6724 else if (token2.type == CPP_LESS)
6725 cp_parser_template_declaration (parser, /*member_p=*/false);
6726 /* Anything else must be an explicit instantiation. */
6727 else
6728 cp_parser_explicit_instantiation (parser);
6729 }
6730 /* If the next token is `export', then we have a template
6731 declaration. */
6732 else if (token1.keyword == RID_EXPORT)
6733 cp_parser_template_declaration (parser, /*member_p=*/false);
6734 /* If the next token is `extern', 'static' or 'inline' and the one
6735 after that is `template', we have a GNU extended explicit
6736 instantiation directive. */
6737 else if (cp_parser_allow_gnu_extensions_p (parser)
6738 && (token1.keyword == RID_EXTERN
6739 || token1.keyword == RID_STATIC
6740 || token1.keyword == RID_INLINE)
6741 && token2.keyword == RID_TEMPLATE)
6742 cp_parser_explicit_instantiation (parser);
6743 /* If the next token is `namespace', check for a named or unnamed
6744 namespace definition. */
6745 else if (token1.keyword == RID_NAMESPACE
6746 && (/* A named namespace definition. */
6747 (token2.type == CPP_NAME
21526606 6748 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
a723baf1
MM
6749 == CPP_OPEN_BRACE))
6750 /* An unnamed namespace definition. */
6751 || token2.type == CPP_OPEN_BRACE))
6752 cp_parser_namespace_definition (parser);
6753 /* We must have either a block declaration or a function
6754 definition. */
6755 else
6756 /* Try to parse a block-declaration, or a function-definition. */
6757 cp_parser_block_declaration (parser, /*statement_p=*/false);
058b15c1
MM
6758
6759 /* Free any declarators allocated. */
6760 obstack_free (&declarator_obstack, p);
a723baf1
MM
6761}
6762
21526606 6763/* Parse a block-declaration.
a723baf1
MM
6764
6765 block-declaration:
6766 simple-declaration
6767 asm-definition
6768 namespace-alias-definition
6769 using-declaration
21526606 6770 using-directive
a723baf1
MM
6771
6772 GNU Extension:
6773
6774 block-declaration:
21526606 6775 __extension__ block-declaration
a723baf1
MM
6776 label-declaration
6777
34cd5ae7 6778 If STATEMENT_P is TRUE, then this block-declaration is occurring as
a723baf1
MM
6779 part of a declaration-statement. */
6780
6781static void
21526606 6782cp_parser_block_declaration (cp_parser *parser,
a723baf1
MM
6783 bool statement_p)
6784{
6785 cp_token *token1;
6786 int saved_pedantic;
6787
6788 /* Check for the `__extension__' keyword. */
6789 if (cp_parser_extension_opt (parser, &saved_pedantic))
6790 {
6791 /* Parse the qualified declaration. */
6792 cp_parser_block_declaration (parser, statement_p);
6793 /* Restore the PEDANTIC flag. */
6794 pedantic = saved_pedantic;
6795
6796 return;
6797 }
6798
6799 /* Peek at the next token to figure out which kind of declaration is
6800 present. */
6801 token1 = cp_lexer_peek_token (parser->lexer);
6802
6803 /* If the next keyword is `asm', we have an asm-definition. */
6804 if (token1->keyword == RID_ASM)
6805 {
6806 if (statement_p)
6807 cp_parser_commit_to_tentative_parse (parser);
6808 cp_parser_asm_definition (parser);
6809 }
6810 /* If the next keyword is `namespace', we have a
6811 namespace-alias-definition. */
6812 else if (token1->keyword == RID_NAMESPACE)
6813 cp_parser_namespace_alias_definition (parser);
6814 /* If the next keyword is `using', we have either a
6815 using-declaration or a using-directive. */
6816 else if (token1->keyword == RID_USING)
6817 {
6818 cp_token *token2;
6819
6820 if (statement_p)
6821 cp_parser_commit_to_tentative_parse (parser);
6822 /* If the token after `using' is `namespace', then we have a
6823 using-directive. */
6824 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6825 if (token2->keyword == RID_NAMESPACE)
6826 cp_parser_using_directive (parser);
6827 /* Otherwise, it's a using-declaration. */
6828 else
6829 cp_parser_using_declaration (parser);
6830 }
6831 /* If the next keyword is `__label__' we have a label declaration. */
6832 else if (token1->keyword == RID_LABEL)
6833 {
6834 if (statement_p)
6835 cp_parser_commit_to_tentative_parse (parser);
6836 cp_parser_label_declaration (parser);
6837 }
6838 /* Anything else must be a simple-declaration. */
6839 else
6840 cp_parser_simple_declaration (parser, !statement_p);
6841}
6842
6843/* Parse a simple-declaration.
6844
6845 simple-declaration:
21526606 6846 decl-specifier-seq [opt] init-declarator-list [opt] ;
a723baf1
MM
6847
6848 init-declarator-list:
6849 init-declarator
21526606 6850 init-declarator-list , init-declarator
a723baf1 6851
34cd5ae7 6852 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9bcb9aae 6853 function-definition as a simple-declaration. */
a723baf1
MM
6854
6855static void
21526606 6856cp_parser_simple_declaration (cp_parser* parser,
94edc4ab 6857 bool function_definition_allowed_p)
a723baf1 6858{
62d1db17 6859 cp_decl_specifier_seq decl_specifiers;
560ad596 6860 int declares_class_or_enum;
a723baf1
MM
6861 bool saw_declarator;
6862
6863 /* Defer access checks until we know what is being declared; the
6864 checks for names appearing in the decl-specifier-seq should be
6865 done as if we were in the scope of the thing being declared. */
8d241e0b 6866 push_deferring_access_checks (dk_deferred);
cf22909c 6867
a723baf1
MM
6868 /* Parse the decl-specifier-seq. We have to keep track of whether
6869 or not the decl-specifier-seq declares a named class or
6870 enumeration type, since that is the only case in which the
21526606 6871 init-declarator-list is allowed to be empty.
a723baf1
MM
6872
6873 [dcl.dcl]
6874
6875 In a simple-declaration, the optional init-declarator-list can be
6876 omitted only when declaring a class or enumeration, that is when
6877 the decl-specifier-seq contains either a class-specifier, an
6878 elaborated-type-specifier, or an enum-specifier. */
62d1db17
MM
6879 cp_parser_decl_specifier_seq (parser,
6880 CP_PARSER_FLAGS_OPTIONAL,
6881 &decl_specifiers,
6882 &declares_class_or_enum);
a723baf1 6883 /* We no longer need to defer access checks. */
cf22909c 6884 stop_deferring_access_checks ();
24c0ef37 6885
39703eb9
MM
6886 /* In a block scope, a valid declaration must always have a
6887 decl-specifier-seq. By not trying to parse declarators, we can
6888 resolve the declaration/expression ambiguity more quickly. */
98ca843c 6889 if (!function_definition_allowed_p
62d1db17 6890 && !decl_specifiers.any_specifiers_p)
39703eb9
MM
6891 {
6892 cp_parser_error (parser, "expected declaration");
6893 goto done;
6894 }
6895
8fbc5ae7
MM
6896 /* If the next two tokens are both identifiers, the code is
6897 erroneous. The usual cause of this situation is code like:
6898
6899 T t;
6900
6901 where "T" should name a type -- but does not. */
de3fe73c
MM
6902 if (!decl_specifiers.type
6903 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8fbc5ae7 6904 {
8d241e0b 6905 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
6906 looking at a declaration. */
6907 cp_parser_commit_to_tentative_parse (parser);
6908 /* Give up. */
39703eb9 6909 goto done;
8fbc5ae7 6910 }
996c2b52
MM
6911
6912 /* If we have seen at least one decl-specifier, and the next token
6913 is not a parenthesis, then we must be looking at a declaration.
6914 (After "int (" we might be looking at a functional cast.) */
6915 if (decl_specifiers.any_specifiers_p
6916 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6917 cp_parser_commit_to_tentative_parse (parser);
8fbc5ae7 6918
a723baf1
MM
6919 /* Keep going until we hit the `;' at the end of the simple
6920 declaration. */
6921 saw_declarator = false;
21526606 6922 while (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
6923 CPP_SEMICOLON))
6924 {
6925 cp_token *token;
6926 bool function_definition_p;
560ad596 6927 tree decl;
a723baf1
MM
6928
6929 saw_declarator = true;
6930 /* Parse the init-declarator. */
62d1db17 6931 decl = cp_parser_init_declarator (parser, &decl_specifiers,
560ad596
MM
6932 function_definition_allowed_p,
6933 /*member_p=*/false,
6934 declares_class_or_enum,
6935 &function_definition_p);
1fb3244a
MM
6936 /* If an error occurred while parsing tentatively, exit quickly.
6937 (That usually happens when in the body of a function; each
6938 statement is treated as a declaration-statement until proven
6939 otherwise.) */
6940 if (cp_parser_error_occurred (parser))
39703eb9 6941 goto done;
a723baf1
MM
6942 /* Handle function definitions specially. */
6943 if (function_definition_p)
6944 {
6945 /* If the next token is a `,', then we are probably
6946 processing something like:
6947
6948 void f() {}, *p;
6949
6950 which is erroneous. */
6951 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6952 error ("mixing declarations and function-definitions is forbidden");
6953 /* Otherwise, we're done with the list of declarators. */
6954 else
24c0ef37 6955 {
cf22909c 6956 pop_deferring_access_checks ();
24c0ef37
GS
6957 return;
6958 }
a723baf1
MM
6959 }
6960 /* The next token should be either a `,' or a `;'. */
6961 token = cp_lexer_peek_token (parser->lexer);
6962 /* If it's a `,', there are more declarators to come. */
6963 if (token->type == CPP_COMMA)
6964 cp_lexer_consume_token (parser->lexer);
6965 /* If it's a `;', we are done. */
6966 else if (token->type == CPP_SEMICOLON)
6967 break;
6968 /* Anything else is an error. */
6969 else
6970 {
996c2b52
MM
6971 /* If we have already issued an error message we don't need
6972 to issue another one. */
6973 if (decl != error_mark_node
0b16f8f4 6974 || cp_parser_uncommitted_to_tentative_parse_p (parser))
2a13a625 6975 cp_parser_error (parser, "expected %<,%> or %<;%>");
a723baf1
MM
6976 /* Skip tokens until we reach the end of the statement. */
6977 cp_parser_skip_to_end_of_statement (parser);
5a98fa7b
MM
6978 /* If the next token is now a `;', consume it. */
6979 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6980 cp_lexer_consume_token (parser->lexer);
39703eb9 6981 goto done;
a723baf1
MM
6982 }
6983 /* After the first time around, a function-definition is not
6984 allowed -- even if it was OK at first. For example:
6985
6986 int i, f() {}
6987
6988 is not valid. */
6989 function_definition_allowed_p = false;
6990 }
6991
6992 /* Issue an error message if no declarators are present, and the
6993 decl-specifier-seq does not itself declare a class or
6994 enumeration. */
6995 if (!saw_declarator)
6996 {
6997 if (cp_parser_declares_only_class_p (parser))
62d1db17 6998 shadow_tag (&decl_specifiers);
a723baf1 6999 /* Perform any deferred access checks. */
cf22909c 7000 perform_deferred_access_checks ();
a723baf1
MM
7001 }
7002
7003 /* Consume the `;'. */
7004 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7005
39703eb9
MM
7006 done:
7007 pop_deferring_access_checks ();
a723baf1
MM
7008}
7009
7010/* Parse a decl-specifier-seq.
7011
7012 decl-specifier-seq:
7013 decl-specifier-seq [opt] decl-specifier
7014
7015 decl-specifier:
7016 storage-class-specifier
7017 type-specifier
7018 function-specifier
7019 friend
21526606 7020 typedef
a723baf1
MM
7021
7022 GNU Extension:
7023
15077df5
MM
7024 decl-specifier:
7025 attributes
a723baf1 7026
62d1db17 7027 Set *DECL_SPECS to a representation of the decl-specifier-seq.
a723baf1 7028
eb1aef53 7029 The parser flags FLAGS is used to control type-specifier parsing.
560ad596
MM
7030
7031 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
543ca912 7032 flags:
560ad596
MM
7033
7034 1: one of the decl-specifiers is an elaborated-type-specifier
543ca912 7035 (i.e., a type declaration)
560ad596 7036 2: one of the decl-specifiers is an enum-specifier or a
543ca912 7037 class-specifier (i.e., a type definition)
98ca843c 7038
560ad596 7039 */
a723baf1 7040
62d1db17 7041static void
21526606 7042cp_parser_decl_specifier_seq (cp_parser* parser,
62d1db17
MM
7043 cp_parser_flags flags,
7044 cp_decl_specifier_seq *decl_specs,
560ad596 7045 int* declares_class_or_enum)
a723baf1 7046{
f2ce60b8 7047 bool constructor_possible_p = !parser->in_declarator_p;
21526606 7048
62d1db17
MM
7049 /* Clear DECL_SPECS. */
7050 clear_decl_specs (decl_specs);
7051
a723baf1 7052 /* Assume no class or enumeration type is declared. */
560ad596 7053 *declares_class_or_enum = 0;
a723baf1 7054
a723baf1
MM
7055 /* Keep reading specifiers until there are no more to read. */
7056 while (true)
7057 {
a723baf1 7058 bool constructor_p;
62d1db17 7059 bool found_decl_spec;
a723baf1
MM
7060 cp_token *token;
7061
7062 /* Peek at the next token. */
7063 token = cp_lexer_peek_token (parser->lexer);
7064 /* Handle attributes. */
7065 if (token->keyword == RID_ATTRIBUTE)
7066 {
7067 /* Parse the attributes. */
98ca843c 7068 decl_specs->attributes
62d1db17
MM
7069 = chainon (decl_specs->attributes,
7070 cp_parser_attributes_opt (parser));
a723baf1
MM
7071 continue;
7072 }
62d1db17
MM
7073 /* Assume we will find a decl-specifier keyword. */
7074 found_decl_spec = true;
a723baf1
MM
7075 /* If the next token is an appropriate keyword, we can simply
7076 add it to the list. */
7077 switch (token->keyword)
7078 {
a723baf1
MM
7079 /* decl-specifier:
7080 friend */
62d1db17
MM
7081 case RID_FRIEND:
7082 if (decl_specs->specs[(int) ds_friend]++)
2a13a625 7083 error ("duplicate %<friend%>");
a723baf1
MM
7084 /* Consume the token. */
7085 cp_lexer_consume_token (parser->lexer);
7086 break;
7087
7088 /* function-specifier:
7089 inline
7090 virtual
7091 explicit */
7092 case RID_INLINE:
7093 case RID_VIRTUAL:
7094 case RID_EXPLICIT:
62d1db17 7095 cp_parser_function_specifier_opt (parser, decl_specs);
a723baf1 7096 break;
21526606 7097
a723baf1
MM
7098 /* decl-specifier:
7099 typedef */
7100 case RID_TYPEDEF:
62d1db17 7101 ++decl_specs->specs[(int) ds_typedef];
a723baf1
MM
7102 /* Consume the token. */
7103 cp_lexer_consume_token (parser->lexer);
2050a1bb
MM
7104 /* A constructor declarator cannot appear in a typedef. */
7105 constructor_possible_p = false;
c006d942
MM
7106 /* The "typedef" keyword can only occur in a declaration; we
7107 may as well commit at this point. */
7108 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
7109 break;
7110
7111 /* storage-class-specifier:
7112 auto
7113 register
7114 static
7115 extern
21526606 7116 mutable
a723baf1
MM
7117
7118 GNU Extension:
7119 thread */
7120 case RID_AUTO:
62d1db17
MM
7121 /* Consume the token. */
7122 cp_lexer_consume_token (parser->lexer);
7123 cp_parser_set_storage_class (decl_specs, sc_auto);
7124 break;
a723baf1 7125 case RID_REGISTER:
62d1db17
MM
7126 /* Consume the token. */
7127 cp_lexer_consume_token (parser->lexer);
7128 cp_parser_set_storage_class (decl_specs, sc_register);
7129 break;
a723baf1 7130 case RID_STATIC:
62d1db17
MM
7131 /* Consume the token. */
7132 cp_lexer_consume_token (parser->lexer);
7133 if (decl_specs->specs[(int) ds_thread])
f1b90a04 7134 {
2d01edd7 7135 error ("%<__thread%> before %<static%>");
f1b90a04
MM
7136 decl_specs->specs[(int) ds_thread] = 0;
7137 }
7138 cp_parser_set_storage_class (decl_specs, sc_static);
62d1db17 7139 break;
a723baf1 7140 case RID_EXTERN:
62d1db17
MM
7141 /* Consume the token. */
7142 cp_lexer_consume_token (parser->lexer);
7143 if (decl_specs->specs[(int) ds_thread])
f1b90a04 7144 {
2d01edd7 7145 error ("%<__thread%> before %<extern%>");
f1b90a04
MM
7146 decl_specs->specs[(int) ds_thread] = 0;
7147 }
7148 cp_parser_set_storage_class (decl_specs, sc_extern);
62d1db17 7149 break;
a723baf1 7150 case RID_MUTABLE:
62d1db17
MM
7151 /* Consume the token. */
7152 cp_lexer_consume_token (parser->lexer);
7153 cp_parser_set_storage_class (decl_specs, sc_mutable);
7154 break;
a723baf1 7155 case RID_THREAD:
62d1db17
MM
7156 /* Consume the token. */
7157 cp_lexer_consume_token (parser->lexer);
7158 ++decl_specs->specs[(int) ds_thread];
a723baf1 7159 break;
21526606 7160
a723baf1 7161 default:
62d1db17
MM
7162 /* We did not yet find a decl-specifier yet. */
7163 found_decl_spec = false;
a723baf1
MM
7164 break;
7165 }
7166
7167 /* Constructors are a special case. The `S' in `S()' is not a
7168 decl-specifier; it is the beginning of the declarator. */
98ca843c 7169 constructor_p
62d1db17
MM
7170 = (!found_decl_spec
7171 && constructor_possible_p
98ca843c 7172 && (cp_parser_constructor_declarator_p
62d1db17 7173 (parser, decl_specs->specs[(int) ds_friend] != 0)));
a723baf1
MM
7174
7175 /* If we don't have a DECL_SPEC yet, then we must be looking at
7176 a type-specifier. */
62d1db17 7177 if (!found_decl_spec && !constructor_p)
a723baf1 7178 {
560ad596 7179 int decl_spec_declares_class_or_enum;
a723baf1 7180 bool is_cv_qualifier;
62d1db17 7181 tree type_spec;
a723baf1 7182
62d1db17 7183 type_spec
a723baf1 7184 = cp_parser_type_specifier (parser, flags,
62d1db17 7185 decl_specs,
a723baf1
MM
7186 /*is_declaration=*/true,
7187 &decl_spec_declares_class_or_enum,
7188 &is_cv_qualifier);
7189
7190 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7191
7192 /* If this type-specifier referenced a user-defined type
7193 (a typedef, class-name, etc.), then we can't allow any
7194 more such type-specifiers henceforth.
7195
7196 [dcl.spec]
7197
7198 The longest sequence of decl-specifiers that could
7199 possibly be a type name is taken as the
7200 decl-specifier-seq of a declaration. The sequence shall
7201 be self-consistent as described below.
7202
7203 [dcl.type]
7204
7205 As a general rule, at most one type-specifier is allowed
7206 in the complete decl-specifier-seq of a declaration. The
7207 only exceptions are the following:
7208
7209 -- const or volatile can be combined with any other
21526606 7210 type-specifier.
a723baf1
MM
7211
7212 -- signed or unsigned can be combined with char, long,
7213 short, or int.
7214
7215 -- ..
7216
7217 Example:
7218
7219 typedef char* Pc;
7220 void g (const int Pc);
7221
7222 Here, Pc is *not* part of the decl-specifier seq; it's
7223 the declarator. Therefore, once we see a type-specifier
7224 (other than a cv-qualifier), we forbid any additional
7225 user-defined types. We *do* still allow things like `int
7226 int' to be considered a decl-specifier-seq, and issue the
7227 error message later. */
62d1db17 7228 if (type_spec && !is_cv_qualifier)
a723baf1 7229 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
2050a1bb 7230 /* A constructor declarator cannot follow a type-specifier. */
62d1db17 7231 if (type_spec)
a723baf1 7232 {
62d1db17
MM
7233 constructor_possible_p = false;
7234 found_decl_spec = true;
a723baf1 7235 }
a723baf1
MM
7236 }
7237
62d1db17
MM
7238 /* If we still do not have a DECL_SPEC, then there are no more
7239 decl-specifiers. */
7240 if (!found_decl_spec)
7241 break;
a723baf1 7242
62d1db17 7243 decl_specs->any_specifiers_p = true;
a723baf1
MM
7244 /* After we see one decl-specifier, further decl-specifiers are
7245 always optional. */
7246 flags |= CP_PARSER_FLAGS_OPTIONAL;
7247 }
7248
0426c4ca 7249 /* Don't allow a friend specifier with a class definition. */
62d1db17
MM
7250 if (decl_specs->specs[(int) ds_friend] != 0
7251 && (*declares_class_or_enum & 2))
0426c4ca 7252 error ("class definition may not be declared a friend");
a723baf1
MM
7253}
7254
21526606 7255/* Parse an (optional) storage-class-specifier.
a723baf1
MM
7256
7257 storage-class-specifier:
7258 auto
7259 register
7260 static
7261 extern
21526606 7262 mutable
a723baf1
MM
7263
7264 GNU Extension:
7265
7266 storage-class-specifier:
7267 thread
7268
7269 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
21526606 7270
a723baf1 7271static tree
94edc4ab 7272cp_parser_storage_class_specifier_opt (cp_parser* parser)
a723baf1
MM
7273{
7274 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7275 {
7276 case RID_AUTO:
7277 case RID_REGISTER:
7278 case RID_STATIC:
7279 case RID_EXTERN:
7280 case RID_MUTABLE:
7281 case RID_THREAD:
7282 /* Consume the token. */
7283 return cp_lexer_consume_token (parser->lexer)->value;
7284
7285 default:
7286 return NULL_TREE;
7287 }
7288}
7289
21526606 7290/* Parse an (optional) function-specifier.
a723baf1
MM
7291
7292 function-specifier:
7293 inline
7294 virtual
7295 explicit
7296
62d1db17
MM
7297 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7298 Updates DECL_SPECS, if it is non-NULL. */
21526606 7299
a723baf1 7300static tree
62d1db17
MM
7301cp_parser_function_specifier_opt (cp_parser* parser,
7302 cp_decl_specifier_seq *decl_specs)
a723baf1
MM
7303{
7304 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7305 {
7306 case RID_INLINE:
62d1db17
MM
7307 if (decl_specs)
7308 ++decl_specs->specs[(int) ds_inline];
7309 break;
7310
a723baf1 7311 case RID_VIRTUAL:
62d1db17
MM
7312 if (decl_specs)
7313 ++decl_specs->specs[(int) ds_virtual];
7314 break;
7315
a723baf1 7316 case RID_EXPLICIT:
62d1db17
MM
7317 if (decl_specs)
7318 ++decl_specs->specs[(int) ds_explicit];
7319 break;
a723baf1
MM
7320
7321 default:
7322 return NULL_TREE;
7323 }
62d1db17
MM
7324
7325 /* Consume the token. */
7326 return cp_lexer_consume_token (parser->lexer)->value;
a723baf1
MM
7327}
7328
7329/* Parse a linkage-specification.
7330
7331 linkage-specification:
7332 extern string-literal { declaration-seq [opt] }
7333 extern string-literal declaration */
7334
7335static void
94edc4ab 7336cp_parser_linkage_specification (cp_parser* parser)
a723baf1 7337{
a723baf1
MM
7338 tree linkage;
7339
7340 /* Look for the `extern' keyword. */
7341 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7342
c162c75e
MA
7343 /* Look for the string-literal. */
7344 linkage = cp_parser_string_literal (parser, false, false);
a723baf1
MM
7345
7346 /* Transform the literal into an identifier. If the literal is a
7347 wide-character string, or contains embedded NULs, then we can't
7348 handle it as the user wants. */
c162c75e
MA
7349 if (strlen (TREE_STRING_POINTER (linkage))
7350 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
a723baf1
MM
7351 {
7352 cp_parser_error (parser, "invalid linkage-specification");
7353 /* Assume C++ linkage. */
c162c75e 7354 linkage = lang_name_cplusplus;
a723baf1 7355 }
a723baf1 7356 else
c162c75e 7357 linkage = get_identifier (TREE_STRING_POINTER (linkage));
a723baf1
MM
7358
7359 /* We're now using the new linkage. */
7360 push_lang_context (linkage);
7361
7362 /* If the next token is a `{', then we're using the first
7363 production. */
7364 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7365 {
7366 /* Consume the `{' token. */
7367 cp_lexer_consume_token (parser->lexer);
7368 /* Parse the declarations. */
7369 cp_parser_declaration_seq_opt (parser);
7370 /* Look for the closing `}'. */
7371 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7372 }
7373 /* Otherwise, there's just one declaration. */
7374 else
7375 {
7376 bool saved_in_unbraced_linkage_specification_p;
7377
21526606 7378 saved_in_unbraced_linkage_specification_p
a723baf1
MM
7379 = parser->in_unbraced_linkage_specification_p;
7380 parser->in_unbraced_linkage_specification_p = true;
7381 have_extern_spec = true;
7382 cp_parser_declaration (parser);
7383 have_extern_spec = false;
21526606 7384 parser->in_unbraced_linkage_specification_p
a723baf1
MM
7385 = saved_in_unbraced_linkage_specification_p;
7386 }
7387
7388 /* We're done with the linkage-specification. */
7389 pop_lang_context ();
7390}
7391
7392/* Special member functions [gram.special] */
7393
7394/* Parse a conversion-function-id.
7395
7396 conversion-function-id:
21526606 7397 operator conversion-type-id
a723baf1
MM
7398
7399 Returns an IDENTIFIER_NODE representing the operator. */
7400
21526606 7401static tree
94edc4ab 7402cp_parser_conversion_function_id (cp_parser* parser)
a723baf1
MM
7403{
7404 tree type;
7405 tree saved_scope;
7406 tree saved_qualifying_scope;
7407 tree saved_object_scope;
4514aa8c 7408 tree pushed_scope = NULL_TREE;
a723baf1
MM
7409
7410 /* Look for the `operator' token. */
7411 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7412 return error_mark_node;
7413 /* When we parse the conversion-type-id, the current scope will be
7414 reset. However, we need that information in able to look up the
7415 conversion function later, so we save it here. */
7416 saved_scope = parser->scope;
7417 saved_qualifying_scope = parser->qualifying_scope;
7418 saved_object_scope = parser->object_scope;
7419 /* We must enter the scope of the class so that the names of
7420 entities declared within the class are available in the
7421 conversion-type-id. For example, consider:
7422
21526606 7423 struct S {
a723baf1
MM
7424 typedef int I;
7425 operator I();
7426 };
7427
7428 S::operator I() { ... }
7429
7430 In order to see that `I' is a type-name in the definition, we
7431 must be in the scope of `S'. */
7432 if (saved_scope)
4514aa8c 7433 pushed_scope = push_scope (saved_scope);
a723baf1
MM
7434 /* Parse the conversion-type-id. */
7435 type = cp_parser_conversion_type_id (parser);
7436 /* Leave the scope of the class, if any. */
4514aa8c
NS
7437 if (pushed_scope)
7438 pop_scope (pushed_scope);
a723baf1
MM
7439 /* Restore the saved scope. */
7440 parser->scope = saved_scope;
7441 parser->qualifying_scope = saved_qualifying_scope;
7442 parser->object_scope = saved_object_scope;
7443 /* If the TYPE is invalid, indicate failure. */
7444 if (type == error_mark_node)
7445 return error_mark_node;
7446 return mangle_conv_op_name_for_type (type);
7447}
7448
7449/* Parse a conversion-type-id:
7450
7451 conversion-type-id:
7452 type-specifier-seq conversion-declarator [opt]
7453
7454 Returns the TYPE specified. */
7455
7456static tree
94edc4ab 7457cp_parser_conversion_type_id (cp_parser* parser)
a723baf1
MM
7458{
7459 tree attributes;
62d1db17 7460 cp_decl_specifier_seq type_specifiers;
058b15c1 7461 cp_declarator *declarator;
037cc9c5 7462 tree type_specified;
a723baf1
MM
7463
7464 /* Parse the attributes. */
7465 attributes = cp_parser_attributes_opt (parser);
7466 /* Parse the type-specifiers. */
62d1db17 7467 cp_parser_type_specifier_seq (parser, &type_specifiers);
a723baf1 7468 /* If that didn't work, stop. */
62d1db17 7469 if (type_specifiers.type == error_mark_node)
a723baf1
MM
7470 return error_mark_node;
7471 /* Parse the conversion-declarator. */
7472 declarator = cp_parser_conversion_declarator_opt (parser);
7473
037cc9c5
FJ
7474 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7475 /*initialized=*/0, &attributes);
7476 if (attributes)
7477 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7478 return type_specified;
a723baf1
MM
7479}
7480
7481/* Parse an (optional) conversion-declarator.
7482
7483 conversion-declarator:
21526606 7484 ptr-operator conversion-declarator [opt]
a723baf1 7485
058b15c1 7486 */
a723baf1 7487
058b15c1 7488static cp_declarator *
94edc4ab 7489cp_parser_conversion_declarator_opt (cp_parser* parser)
a723baf1
MM
7490{
7491 enum tree_code code;
7492 tree class_type;
3c01e5df 7493 cp_cv_quals cv_quals;
a723baf1
MM
7494
7495 /* We don't know if there's a ptr-operator next, or not. */
7496 cp_parser_parse_tentatively (parser);
7497 /* Try the ptr-operator. */
3c01e5df 7498 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
a723baf1
MM
7499 /* If it worked, look for more conversion-declarators. */
7500 if (cp_parser_parse_definitely (parser))
7501 {
058b15c1 7502 cp_declarator *declarator;
98ca843c 7503
058b15c1
MM
7504 /* Parse another optional declarator. */
7505 declarator = cp_parser_conversion_declarator_opt (parser);
98ca843c 7506
058b15c1
MM
7507 /* Create the representation of the declarator. */
7508 if (class_type)
3c01e5df 7509 declarator = make_ptrmem_declarator (cv_quals, class_type,
a723baf1 7510 declarator);
058b15c1 7511 else if (code == INDIRECT_REF)
3c01e5df 7512 declarator = make_pointer_declarator (cv_quals, declarator);
058b15c1 7513 else
3c01e5df 7514 declarator = make_reference_declarator (cv_quals, declarator);
98ca843c 7515
058b15c1 7516 return declarator;
a723baf1
MM
7517 }
7518
058b15c1 7519 return NULL;
a723baf1
MM
7520}
7521
7522/* Parse an (optional) ctor-initializer.
7523
7524 ctor-initializer:
21526606 7525 : mem-initializer-list
a723baf1
MM
7526
7527 Returns TRUE iff the ctor-initializer was actually present. */
7528
7529static bool
94edc4ab 7530cp_parser_ctor_initializer_opt (cp_parser* parser)
a723baf1
MM
7531{
7532 /* If the next token is not a `:', then there is no
7533 ctor-initializer. */
7534 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7535 {
7536 /* Do default initialization of any bases and members. */
7537 if (DECL_CONSTRUCTOR_P (current_function_decl))
7538 finish_mem_initializers (NULL_TREE);
7539
7540 return false;
7541 }
7542
7543 /* Consume the `:' token. */
7544 cp_lexer_consume_token (parser->lexer);
7545 /* And the mem-initializer-list. */
7546 cp_parser_mem_initializer_list (parser);
7547
7548 return true;
7549}
7550
7551/* Parse a mem-initializer-list.
7552
7553 mem-initializer-list:
7554 mem-initializer
7555 mem-initializer , mem-initializer-list */
7556
7557static void
94edc4ab 7558cp_parser_mem_initializer_list (cp_parser* parser)
a723baf1
MM
7559{
7560 tree mem_initializer_list = NULL_TREE;
7561
7562 /* Let the semantic analysis code know that we are starting the
7563 mem-initializer-list. */
0e136342
MM
7564 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7565 error ("only constructors take base initializers");
a723baf1
MM
7566
7567 /* Loop through the list. */
7568 while (true)
7569 {
7570 tree mem_initializer;
7571
7572 /* Parse the mem-initializer. */
7573 mem_initializer = cp_parser_mem_initializer (parser);
7574 /* Add it to the list, unless it was erroneous. */
7575 if (mem_initializer)
7576 {
7577 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7578 mem_initializer_list = mem_initializer;
7579 }
7580 /* If the next token is not a `,', we're done. */
7581 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7582 break;
7583 /* Consume the `,' token. */
7584 cp_lexer_consume_token (parser->lexer);
7585 }
7586
7587 /* Perform semantic analysis. */
0e136342
MM
7588 if (DECL_CONSTRUCTOR_P (current_function_decl))
7589 finish_mem_initializers (mem_initializer_list);
a723baf1
MM
7590}
7591
7592/* Parse a mem-initializer.
7593
7594 mem-initializer:
21526606 7595 mem-initializer-id ( expression-list [opt] )
a723baf1
MM
7596
7597 GNU extension:
21526606 7598
a723baf1 7599 mem-initializer:
34cd5ae7 7600 ( expression-list [opt] )
a723baf1
MM
7601
7602 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7603 class) or FIELD_DECL (for a non-static data member) to initialize;
7604 the TREE_VALUE is the expression-list. */
7605
7606static tree
94edc4ab 7607cp_parser_mem_initializer (cp_parser* parser)
a723baf1
MM
7608{
7609 tree mem_initializer_id;
7610 tree expression_list;
1f5a253a 7611 tree member;
21526606 7612
a723baf1
MM
7613 /* Find out what is being initialized. */
7614 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7615 {
7616 pedwarn ("anachronistic old-style base class initializer");
7617 mem_initializer_id = NULL_TREE;
7618 }
7619 else
7620 mem_initializer_id = cp_parser_mem_initializer_id (parser);
1f5a253a
NS
7621 member = expand_member_init (mem_initializer_id);
7622 if (member && !DECL_P (member))
7623 in_base_initializer = 1;
7efa3e22 7624
21526606 7625 expression_list
39703eb9
MM
7626 = cp_parser_parenthesized_expression_list (parser, false,
7627 /*non_constant_p=*/NULL);
7efa3e22 7628 if (!expression_list)
a723baf1 7629 expression_list = void_type_node;
a723baf1 7630
1f5a253a 7631 in_base_initializer = 0;
21526606 7632
1f5a253a 7633 return member ? build_tree_list (member, expression_list) : NULL_TREE;
a723baf1
MM
7634}
7635
7636/* Parse a mem-initializer-id.
7637
7638 mem-initializer-id:
7639 :: [opt] nested-name-specifier [opt] class-name
21526606 7640 identifier
a723baf1
MM
7641
7642 Returns a TYPE indicating the class to be initializer for the first
7643 production. Returns an IDENTIFIER_NODE indicating the data member
7644 to be initialized for the second production. */
7645
7646static tree
94edc4ab 7647cp_parser_mem_initializer_id (cp_parser* parser)
a723baf1
MM
7648{
7649 bool global_scope_p;
7650 bool nested_name_specifier_p;
8a83a693 7651 bool template_p = false;
a723baf1
MM
7652 tree id;
7653
8a83a693
GB
7654 /* `typename' is not allowed in this context ([temp.res]). */
7655 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7656 {
2a13a625 7657 error ("keyword %<typename%> not allowed in this context (a qualified "
8a83a693
GB
7658 "member initializer is implicitly a type)");
7659 cp_lexer_consume_token (parser->lexer);
7660 }
a723baf1 7661 /* Look for the optional `::' operator. */
21526606
EC
7662 global_scope_p
7663 = (cp_parser_global_scope_opt (parser,
7664 /*current_scope_valid_p=*/false)
a723baf1
MM
7665 != NULL_TREE);
7666 /* Look for the optional nested-name-specifier. The simplest way to
7667 implement:
7668
7669 [temp.res]
7670
7671 The keyword `typename' is not permitted in a base-specifier or
7672 mem-initializer; in these contexts a qualified name that
7673 depends on a template-parameter is implicitly assumed to be a
7674 type name.
7675
7676 is to assume that we have seen the `typename' keyword at this
7677 point. */
21526606 7678 nested_name_specifier_p
a723baf1
MM
7679 = (cp_parser_nested_name_specifier_opt (parser,
7680 /*typename_keyword_p=*/true,
7681 /*check_dependency_p=*/true,
a668c6ad
MM
7682 /*type_p=*/true,
7683 /*is_declaration=*/true)
a723baf1 7684 != NULL_TREE);
8a83a693
GB
7685 if (nested_name_specifier_p)
7686 template_p = cp_parser_optional_template_keyword (parser);
a723baf1
MM
7687 /* If there is a `::' operator or a nested-name-specifier, then we
7688 are definitely looking for a class-name. */
7689 if (global_scope_p || nested_name_specifier_p)
7690 return cp_parser_class_name (parser,
7691 /*typename_keyword_p=*/true,
8a83a693 7692 /*template_keyword_p=*/template_p,
fc6a28d7 7693 none_type,
a723baf1 7694 /*check_dependency_p=*/true,
a668c6ad
MM
7695 /*class_head_p=*/false,
7696 /*is_declaration=*/true);
a723baf1
MM
7697 /* Otherwise, we could also be looking for an ordinary identifier. */
7698 cp_parser_parse_tentatively (parser);
7699 /* Try a class-name. */
21526606 7700 id = cp_parser_class_name (parser,
a723baf1
MM
7701 /*typename_keyword_p=*/true,
7702 /*template_keyword_p=*/false,
fc6a28d7 7703 none_type,
a723baf1 7704 /*check_dependency_p=*/true,
a668c6ad
MM
7705 /*class_head_p=*/false,
7706 /*is_declaration=*/true);
a723baf1
MM
7707 /* If we found one, we're done. */
7708 if (cp_parser_parse_definitely (parser))
7709 return id;
7710 /* Otherwise, look for an ordinary identifier. */
7711 return cp_parser_identifier (parser);
7712}
7713
7714/* Overloading [gram.over] */
7715
7716/* Parse an operator-function-id.
7717
7718 operator-function-id:
21526606 7719 operator operator
a723baf1
MM
7720
7721 Returns an IDENTIFIER_NODE for the operator which is a
7722 human-readable spelling of the identifier, e.g., `operator +'. */
7723
21526606 7724static tree
94edc4ab 7725cp_parser_operator_function_id (cp_parser* parser)
a723baf1
MM
7726{
7727 /* Look for the `operator' keyword. */
7728 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7729 return error_mark_node;
7730 /* And then the name of the operator itself. */
7731 return cp_parser_operator (parser);
7732}
7733
7734/* Parse an operator.
7735
7736 operator:
7737 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7738 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7739 || ++ -- , ->* -> () []
7740
7741 GNU Extensions:
21526606 7742
a723baf1
MM
7743 operator:
7744 <? >? <?= >?=
7745
7746 Returns an IDENTIFIER_NODE for the operator which is a
7747 human-readable spelling of the identifier, e.g., `operator +'. */
21526606 7748
a723baf1 7749static tree
94edc4ab 7750cp_parser_operator (cp_parser* parser)
a723baf1
MM
7751{
7752 tree id = NULL_TREE;
7753 cp_token *token;
7754
7755 /* Peek at the next token. */
7756 token = cp_lexer_peek_token (parser->lexer);
7757 /* Figure out which operator we have. */
7758 switch (token->type)
7759 {
7760 case CPP_KEYWORD:
7761 {
7762 enum tree_code op;
7763
7764 /* The keyword should be either `new' or `delete'. */
7765 if (token->keyword == RID_NEW)
7766 op = NEW_EXPR;
7767 else if (token->keyword == RID_DELETE)
7768 op = DELETE_EXPR;
7769 else
7770 break;
7771
7772 /* Consume the `new' or `delete' token. */
7773 cp_lexer_consume_token (parser->lexer);
7774
7775 /* Peek at the next token. */
7776 token = cp_lexer_peek_token (parser->lexer);
7777 /* If it's a `[' token then this is the array variant of the
7778 operator. */
7779 if (token->type == CPP_OPEN_SQUARE)
7780 {
7781 /* Consume the `[' token. */
7782 cp_lexer_consume_token (parser->lexer);
7783 /* Look for the `]' token. */
7784 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
21526606 7785 id = ansi_opname (op == NEW_EXPR
a723baf1
MM
7786 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7787 }
7788 /* Otherwise, we have the non-array variant. */
7789 else
7790 id = ansi_opname (op);
7791
7792 return id;
7793 }
7794
7795 case CPP_PLUS:
7796 id = ansi_opname (PLUS_EXPR);
7797 break;
7798
7799 case CPP_MINUS:
7800 id = ansi_opname (MINUS_EXPR);
7801 break;
7802
7803 case CPP_MULT:
7804 id = ansi_opname (MULT_EXPR);
7805 break;
7806
7807 case CPP_DIV:
7808 id = ansi_opname (TRUNC_DIV_EXPR);
7809 break;
7810
7811 case CPP_MOD:
7812 id = ansi_opname (TRUNC_MOD_EXPR);
7813 break;
7814
7815 case CPP_XOR:
7816 id = ansi_opname (BIT_XOR_EXPR);
7817 break;
7818
7819 case CPP_AND:
7820 id = ansi_opname (BIT_AND_EXPR);
7821 break;
7822
7823 case CPP_OR:
7824 id = ansi_opname (BIT_IOR_EXPR);
7825 break;
7826
7827 case CPP_COMPL:
7828 id = ansi_opname (BIT_NOT_EXPR);
7829 break;
21526606 7830
a723baf1
MM
7831 case CPP_NOT:
7832 id = ansi_opname (TRUTH_NOT_EXPR);
7833 break;
7834
7835 case CPP_EQ:
7836 id = ansi_assopname (NOP_EXPR);
7837 break;
7838
7839 case CPP_LESS:
7840 id = ansi_opname (LT_EXPR);
7841 break;
7842
7843 case CPP_GREATER:
7844 id = ansi_opname (GT_EXPR);
7845 break;
7846
7847 case CPP_PLUS_EQ:
7848 id = ansi_assopname (PLUS_EXPR);
7849 break;
7850
7851 case CPP_MINUS_EQ:
7852 id = ansi_assopname (MINUS_EXPR);
7853 break;
7854
7855 case CPP_MULT_EQ:
7856 id = ansi_assopname (MULT_EXPR);
7857 break;
7858
7859 case CPP_DIV_EQ:
7860 id = ansi_assopname (TRUNC_DIV_EXPR);
7861 break;
7862
7863 case CPP_MOD_EQ:
7864 id = ansi_assopname (TRUNC_MOD_EXPR);
7865 break;
7866
7867 case CPP_XOR_EQ:
7868 id = ansi_assopname (BIT_XOR_EXPR);
7869 break;
7870
7871 case CPP_AND_EQ:
7872 id = ansi_assopname (BIT_AND_EXPR);
7873 break;
7874
7875 case CPP_OR_EQ:
7876 id = ansi_assopname (BIT_IOR_EXPR);
7877 break;
7878
7879 case CPP_LSHIFT:
7880 id = ansi_opname (LSHIFT_EXPR);
7881 break;
7882
7883 case CPP_RSHIFT:
7884 id = ansi_opname (RSHIFT_EXPR);
7885 break;
7886
7887 case CPP_LSHIFT_EQ:
7888 id = ansi_assopname (LSHIFT_EXPR);
7889 break;
7890
7891 case CPP_RSHIFT_EQ:
7892 id = ansi_assopname (RSHIFT_EXPR);
7893 break;
7894
7895 case CPP_EQ_EQ:
7896 id = ansi_opname (EQ_EXPR);
7897 break;
7898
7899 case CPP_NOT_EQ:
7900 id = ansi_opname (NE_EXPR);
7901 break;
7902
7903 case CPP_LESS_EQ:
7904 id = ansi_opname (LE_EXPR);
7905 break;
7906
7907 case CPP_GREATER_EQ:
7908 id = ansi_opname (GE_EXPR);
7909 break;
7910
7911 case CPP_AND_AND:
7912 id = ansi_opname (TRUTH_ANDIF_EXPR);
7913 break;
7914
7915 case CPP_OR_OR:
7916 id = ansi_opname (TRUTH_ORIF_EXPR);
7917 break;
21526606 7918
a723baf1
MM
7919 case CPP_PLUS_PLUS:
7920 id = ansi_opname (POSTINCREMENT_EXPR);
7921 break;
7922
7923 case CPP_MINUS_MINUS:
7924 id = ansi_opname (PREDECREMENT_EXPR);
7925 break;
7926
7927 case CPP_COMMA:
7928 id = ansi_opname (COMPOUND_EXPR);
7929 break;
7930
7931 case CPP_DEREF_STAR:
7932 id = ansi_opname (MEMBER_REF);
7933 break;
7934
7935 case CPP_DEREF:
7936 id = ansi_opname (COMPONENT_REF);
7937 break;
7938
7939 case CPP_OPEN_PAREN:
7940 /* Consume the `('. */
7941 cp_lexer_consume_token (parser->lexer);
7942 /* Look for the matching `)'. */
7943 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7944 return ansi_opname (CALL_EXPR);
7945
7946 case CPP_OPEN_SQUARE:
7947 /* Consume the `['. */
7948 cp_lexer_consume_token (parser->lexer);
7949 /* Look for the matching `]'. */
7950 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7951 return ansi_opname (ARRAY_REF);
7952
7953 /* Extensions. */
7954 case CPP_MIN:
7955 id = ansi_opname (MIN_EXPR);
7956 break;
7957
7958 case CPP_MAX:
7959 id = ansi_opname (MAX_EXPR);
7960 break;
7961
7962 case CPP_MIN_EQ:
7963 id = ansi_assopname (MIN_EXPR);
7964 break;
7965
7966 case CPP_MAX_EQ:
7967 id = ansi_assopname (MAX_EXPR);
7968 break;
7969
7970 default:
7971 /* Anything else is an error. */
7972 break;
7973 }
7974
7975 /* If we have selected an identifier, we need to consume the
7976 operator token. */
7977 if (id)
7978 cp_lexer_consume_token (parser->lexer);
7979 /* Otherwise, no valid operator name was present. */
7980 else
7981 {
7982 cp_parser_error (parser, "expected operator");
7983 id = error_mark_node;
7984 }
7985
7986 return id;
7987}
7988
7989/* Parse a template-declaration.
7990
7991 template-declaration:
21526606 7992 export [opt] template < template-parameter-list > declaration
a723baf1
MM
7993
7994 If MEMBER_P is TRUE, this template-declaration occurs within a
21526606 7995 class-specifier.
a723baf1
MM
7996
7997 The grammar rule given by the standard isn't correct. What
7998 is really meant is:
7999
8000 template-declaration:
21526606 8001 export [opt] template-parameter-list-seq
a723baf1 8002 decl-specifier-seq [opt] init-declarator [opt] ;
21526606 8003 export [opt] template-parameter-list-seq
a723baf1
MM
8004 function-definition
8005
8006 template-parameter-list-seq:
8007 template-parameter-list-seq [opt]
8008 template < template-parameter-list > */
8009
8010static void
94edc4ab 8011cp_parser_template_declaration (cp_parser* parser, bool member_p)
a723baf1
MM
8012{
8013 /* Check for `export'. */
8014 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8015 {
8016 /* Consume the `export' token. */
8017 cp_lexer_consume_token (parser->lexer);
8018 /* Warn that we do not support `export'. */
2a13a625 8019 warning ("keyword %<export%> not implemented, and will be ignored");
a723baf1
MM
8020 }
8021
8022 cp_parser_template_declaration_after_export (parser, member_p);
8023}
8024
8025/* Parse a template-parameter-list.
8026
8027 template-parameter-list:
8028 template-parameter
8029 template-parameter-list , template-parameter
8030
8031 Returns a TREE_LIST. Each node represents a template parameter.
8032 The nodes are connected via their TREE_CHAINs. */
8033
8034static tree
94edc4ab 8035cp_parser_template_parameter_list (cp_parser* parser)
a723baf1
MM
8036{
8037 tree parameter_list = NULL_TREE;
8038
8039 while (true)
8040 {
8041 tree parameter;
8042 cp_token *token;
058b15c1 8043 bool is_non_type;
a723baf1
MM
8044
8045 /* Parse the template-parameter. */
058b15c1 8046 parameter = cp_parser_template_parameter (parser, &is_non_type);
a723baf1 8047 /* Add it to the list. */
943e3ede
MM
8048 if (parameter != error_mark_node)
8049 parameter_list = process_template_parm (parameter_list,
8050 parameter,
8051 is_non_type);
a723baf1
MM
8052 /* Peek at the next token. */
8053 token = cp_lexer_peek_token (parser->lexer);
8054 /* If it's not a `,', we're done. */
8055 if (token->type != CPP_COMMA)
8056 break;
8057 /* Otherwise, consume the `,' token. */
8058 cp_lexer_consume_token (parser->lexer);
8059 }
8060
8061 return parameter_list;
8062}
8063
8064/* Parse a template-parameter.
8065
8066 template-parameter:
8067 type-parameter
8068 parameter-declaration
8069
943e3ede
MM
8070 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8071 the parameter. The TREE_PURPOSE is the default value, if any.
8072 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8073 iff this parameter is a non-type parameter. */
a723baf1
MM
8074
8075static tree
058b15c1 8076cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
a723baf1
MM
8077{
8078 cp_token *token;
62d1db17 8079 cp_parameter_declarator *parameter_declarator;
943e3ede 8080 tree parm;
a723baf1 8081
058b15c1
MM
8082 /* Assume it is a type parameter or a template parameter. */
8083 *is_non_type = false;
a723baf1
MM
8084 /* Peek at the next token. */
8085 token = cp_lexer_peek_token (parser->lexer);
8086 /* If it is `class' or `template', we have a type-parameter. */
8087 if (token->keyword == RID_TEMPLATE)
8088 return cp_parser_type_parameter (parser);
8089 /* If it is `class' or `typename' we do not know yet whether it is a
8090 type parameter or a non-type parameter. Consider:
8091
8092 template <typename T, typename T::X X> ...
8093
8094 or:
21526606 8095
a723baf1
MM
8096 template <class C, class D*> ...
8097
8098 Here, the first parameter is a type parameter, and the second is
8099 a non-type parameter. We can tell by looking at the token after
8100 the identifier -- if it is a `,', `=', or `>' then we have a type
8101 parameter. */
8102 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8103 {
8104 /* Peek at the token after `class' or `typename'. */
8105 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8106 /* If it's an identifier, skip it. */
8107 if (token->type == CPP_NAME)
8108 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8109 /* Now, see if the token looks like the end of a template
8110 parameter. */
21526606 8111 if (token->type == CPP_COMMA
a723baf1
MM
8112 || token->type == CPP_EQ
8113 || token->type == CPP_GREATER)
8114 return cp_parser_type_parameter (parser);
8115 }
8116
21526606 8117 /* Otherwise, it is a non-type parameter.
a723baf1
MM
8118
8119 [temp.param]
8120
8121 When parsing a default template-argument for a non-type
8122 template-parameter, the first non-nested `>' is taken as the end
8123 of the template parameter-list rather than a greater-than
8124 operator. */
058b15c1
MM
8125 *is_non_type = true;
8126 parameter_declarator
8127 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8128 /*parenthesized_p=*/NULL);
943e3ede
MM
8129 parm = grokdeclarator (parameter_declarator->declarator,
8130 &parameter_declarator->decl_specifiers,
8131 PARM, /*initialized=*/0,
8132 /*attrlist=*/NULL);
8133 if (parm == error_mark_node)
8134 return error_mark_node;
8135 return build_tree_list (parameter_declarator->default_argument, parm);
a723baf1
MM
8136}
8137
8138/* Parse a type-parameter.
8139
8140 type-parameter:
8141 class identifier [opt]
8142 class identifier [opt] = type-id
8143 typename identifier [opt]
8144 typename identifier [opt] = type-id
8145 template < template-parameter-list > class identifier [opt]
21526606
EC
8146 template < template-parameter-list > class identifier [opt]
8147 = id-expression
a723baf1
MM
8148
8149 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8150 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8151 the declaration of the parameter. */
8152
8153static tree
94edc4ab 8154cp_parser_type_parameter (cp_parser* parser)
a723baf1
MM
8155{
8156 cp_token *token;
8157 tree parameter;
8158
8159 /* Look for a keyword to tell us what kind of parameter this is. */
21526606 8160 token = cp_parser_require (parser, CPP_KEYWORD,
8a6393df 8161 "`class', `typename', or `template'");
a723baf1
MM
8162 if (!token)
8163 return error_mark_node;
8164
8165 switch (token->keyword)
8166 {
8167 case RID_CLASS:
8168 case RID_TYPENAME:
8169 {
8170 tree identifier;
8171 tree default_argument;
8172
8173 /* If the next token is an identifier, then it names the
8174 parameter. */
8175 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8176 identifier = cp_parser_identifier (parser);
8177 else
8178 identifier = NULL_TREE;
8179
8180 /* Create the parameter. */
8181 parameter = finish_template_type_parm (class_type_node, identifier);
8182
8183 /* If the next token is an `=', we have a default argument. */
8184 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8185 {
8186 /* Consume the `=' token. */
8187 cp_lexer_consume_token (parser->lexer);
34cd5ae7 8188 /* Parse the default-argument. */
a723baf1
MM
8189 default_argument = cp_parser_type_id (parser);
8190 }
8191 else
8192 default_argument = NULL_TREE;
8193
8194 /* Create the combined representation of the parameter and the
8195 default argument. */
c67d36d0 8196 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
8197 }
8198 break;
8199
8200 case RID_TEMPLATE:
8201 {
8202 tree parameter_list;
8203 tree identifier;
8204 tree default_argument;
8205
8206 /* Look for the `<'. */
8207 cp_parser_require (parser, CPP_LESS, "`<'");
8208 /* Parse the template-parameter-list. */
8209 begin_template_parm_list ();
21526606 8210 parameter_list
a723baf1
MM
8211 = cp_parser_template_parameter_list (parser);
8212 parameter_list = end_template_parm_list (parameter_list);
8213 /* Look for the `>'. */
8214 cp_parser_require (parser, CPP_GREATER, "`>'");
8215 /* Look for the `class' keyword. */
8216 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8217 /* If the next token is an `=', then there is a
8218 default-argument. If the next token is a `>', we are at
8219 the end of the parameter-list. If the next token is a `,',
8220 then we are at the end of this parameter. */
8221 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8222 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8223 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
71bd7186
MM
8224 {
8225 identifier = cp_parser_identifier (parser);
03fd3f84 8226 /* Treat invalid names as if the parameter were nameless. */
71bd7186
MM
8227 if (identifier == error_mark_node)
8228 identifier = NULL_TREE;
8229 }
a723baf1
MM
8230 else
8231 identifier = NULL_TREE;
71bd7186 8232
a723baf1
MM
8233 /* Create the template parameter. */
8234 parameter = finish_template_template_parm (class_type_node,
8235 identifier);
21526606 8236
a723baf1
MM
8237 /* If the next token is an `=', then there is a
8238 default-argument. */
8239 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8240 {
b0bc6e8e
KL
8241 bool is_template;
8242
a723baf1
MM
8243 /* Consume the `='. */
8244 cp_lexer_consume_token (parser->lexer);
8245 /* Parse the id-expression. */
21526606 8246 default_argument
a723baf1
MM
8247 = cp_parser_id_expression (parser,
8248 /*template_keyword_p=*/false,
8249 /*check_dependency_p=*/true,
b0bc6e8e 8250 /*template_p=*/&is_template,
f3c2dfc6 8251 /*declarator_p=*/false);
a3a503a5
GB
8252 if (TREE_CODE (default_argument) == TYPE_DECL)
8253 /* If the id-expression was a template-id that refers to
8254 a template-class, we already have the declaration here,
8255 so no further lookup is needed. */
8256 ;
8257 else
8258 /* Look up the name. */
21526606 8259 default_argument
a3a503a5 8260 = cp_parser_lookup_name (parser, default_argument,
fc6a28d7
MM
8261 none_type,
8262 /*is_template=*/is_template,
8263 /*is_namespace=*/false,
8264 /*check_dependency=*/true,
8265 /*ambiguous_p=*/NULL);
a723baf1
MM
8266 /* See if the default argument is valid. */
8267 default_argument
8268 = check_template_template_default_arg (default_argument);
8269 }
8270 else
8271 default_argument = NULL_TREE;
8272
8273 /* Create the combined representation of the parameter and the
8274 default argument. */
71bd7186 8275 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
8276 }
8277 break;
8278
8279 default:
71bd7186
MM
8280 gcc_unreachable ();
8281 break;
a723baf1 8282 }
21526606 8283
a723baf1
MM
8284 return parameter;
8285}
8286
8287/* Parse a template-id.
8288
8289 template-id:
8290 template-name < template-argument-list [opt] >
8291
8292 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8293 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8294 returned. Otherwise, if the template-name names a function, or set
8295 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
21526606 8296 names a class, returns a TYPE_DECL for the specialization.
a723baf1
MM
8297
8298 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8299 uninstantiated templates. */
8300
8301static tree
21526606
EC
8302cp_parser_template_id (cp_parser *parser,
8303 bool template_keyword_p,
a668c6ad
MM
8304 bool check_dependency_p,
8305 bool is_declaration)
a723baf1
MM
8306{
8307 tree template;
8308 tree arguments;
a723baf1 8309 tree template_id;
0c5e4866 8310 cp_token_position start_of_id = 0;
a723baf1 8311 tree access_check = NULL_TREE;
f4abade9 8312 cp_token *next_token, *next_token_2;
a668c6ad 8313 bool is_identifier;
a723baf1
MM
8314
8315 /* If the next token corresponds to a template-id, there is no need
8316 to reparse it. */
2050a1bb
MM
8317 next_token = cp_lexer_peek_token (parser->lexer);
8318 if (next_token->type == CPP_TEMPLATE_ID)
a723baf1
MM
8319 {
8320 tree value;
8321 tree check;
8322
8323 /* Get the stored value. */
8324 value = cp_lexer_consume_token (parser->lexer)->value;
8325 /* Perform any access checks that were deferred. */
8326 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c
KL
8327 perform_or_defer_access_check (TREE_PURPOSE (check),
8328 TREE_VALUE (check));
a723baf1
MM
8329 /* Return the stored value. */
8330 return TREE_VALUE (value);
8331 }
8332
2050a1bb
MM
8333 /* Avoid performing name lookup if there is no possibility of
8334 finding a template-id. */
8335 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8336 || (next_token->type == CPP_NAME
21526606 8337 && !cp_parser_nth_token_starts_template_argument_list_p
f4abade9 8338 (parser, 2)))
2050a1bb
MM
8339 {
8340 cp_parser_error (parser, "expected template-id");
8341 return error_mark_node;
8342 }
8343
a723baf1 8344 /* Remember where the template-id starts. */
0b16f8f4 8345 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
0c5e4866 8346 start_of_id = cp_lexer_token_position (parser->lexer, false);
a723baf1 8347
8d241e0b 8348 push_deferring_access_checks (dk_deferred);
cf22909c 8349
a723baf1 8350 /* Parse the template-name. */
a668c6ad 8351 is_identifier = false;
a723baf1 8352 template = cp_parser_template_name (parser, template_keyword_p,
a668c6ad
MM
8353 check_dependency_p,
8354 is_declaration,
8355 &is_identifier);
8356 if (template == error_mark_node || is_identifier)
cf22909c
KL
8357 {
8358 pop_deferring_access_checks ();
a668c6ad 8359 return template;
cf22909c 8360 }
a723baf1 8361
21526606 8362 /* If we find the sequence `[:' after a template-name, it's probably
f4abade9
GB
8363 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8364 parse correctly the argument list. */
2cfe82fe 8365 next_token = cp_lexer_peek_token (parser->lexer);
f4abade9 8366 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
21526606 8367 if (next_token->type == CPP_OPEN_SQUARE
f4abade9 8368 && next_token->flags & DIGRAPH
21526606 8369 && next_token_2->type == CPP_COLON
f4abade9 8370 && !(next_token_2->flags & PREV_WHITE))
cf22909c 8371 {
f4abade9
GB
8372 cp_parser_parse_tentatively (parser);
8373 /* Change `:' into `::'. */
8374 next_token_2->type = CPP_SCOPE;
8375 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8376 CPP_LESS. */
8377 cp_lexer_consume_token (parser->lexer);
8378 /* Parse the arguments. */
8379 arguments = cp_parser_enclosed_template_argument_list (parser);
8380 if (!cp_parser_parse_definitely (parser))
8381 {
8382 /* If we couldn't parse an argument list, then we revert our changes
8383 and return simply an error. Maybe this is not a template-id
8384 after all. */
8385 next_token_2->type = CPP_COLON;
2a13a625 8386 cp_parser_error (parser, "expected %<<%>");
f4abade9
GB
8387 pop_deferring_access_checks ();
8388 return error_mark_node;
8389 }
8390 /* Otherwise, emit an error about the invalid digraph, but continue
8391 parsing because we got our argument list. */
2a13a625
GDR
8392 pedwarn ("%<<::%> cannot begin a template-argument list");
8393 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8394 "between %<<%> and %<::%>");
f4abade9
GB
8395 if (!flag_permissive)
8396 {
8397 static bool hint;
8398 if (!hint)
8399 {
2a13a625 8400 inform ("(if you use -fpermissive G++ will accept your code)");
f4abade9
GB
8401 hint = true;
8402 }
8403 }
8404 }
8405 else
8406 {
8407 /* Look for the `<' that starts the template-argument-list. */
8408 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8409 {
8410 pop_deferring_access_checks ();
8411 return error_mark_node;
8412 }
8413 /* Parse the arguments. */
8414 arguments = cp_parser_enclosed_template_argument_list (parser);
cf22909c 8415 }
a723baf1
MM
8416
8417 /* Build a representation of the specialization. */
8418 if (TREE_CODE (template) == IDENTIFIER_NODE)
8419 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8420 else if (DECL_CLASS_TEMPLATE_P (template)
8421 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
21526606
EC
8422 template_id
8423 = finish_template_type (template, arguments,
8424 cp_lexer_next_token_is (parser->lexer,
a723baf1
MM
8425 CPP_SCOPE));
8426 else
8427 {
8428 /* If it's not a class-template or a template-template, it should be
8429 a function-template. */
50bc768d
NS
8430 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8431 || TREE_CODE (template) == OVERLOAD
8432 || BASELINK_P (template)));
21526606 8433
a723baf1
MM
8434 template_id = lookup_template_function (template, arguments);
8435 }
21526606 8436
cf22909c
KL
8437 /* Retrieve any deferred checks. Do not pop this access checks yet
8438 so the memory will not be reclaimed during token replacing below. */
8439 access_check = get_deferred_access_checks ();
8440
a723baf1
MM
8441 /* If parsing tentatively, replace the sequence of tokens that makes
8442 up the template-id with a CPP_TEMPLATE_ID token. That way,
8443 should we re-parse the token stream, we will not have to repeat
8444 the effort required to do the parse, nor will we issue duplicate
8445 error messages about problems during instantiation of the
354e22e1
AO
8446 template. Do so only if parsing succeeded, otherwise we may
8447 silently accept template arguments with syntax errors. */
8448 if (start_of_id && !cp_parser_error_occurred (parser))
a723baf1 8449 {
0c5e4866
NS
8450 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8451
a723baf1
MM
8452 /* Reset the contents of the START_OF_ID token. */
8453 token->type = CPP_TEMPLATE_ID;
8454 token->value = build_tree_list (access_check, template_id);
8455 token->keyword = RID_MAX;
0c5e4866 8456
a723baf1 8457 /* Purge all subsequent tokens. */
0c5e4866 8458 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
a723baf1
MM
8459 }
8460
cf22909c 8461 pop_deferring_access_checks ();
a723baf1
MM
8462 return template_id;
8463}
8464
8465/* Parse a template-name.
8466
8467 template-name:
8468 identifier
21526606 8469
a723baf1
MM
8470 The standard should actually say:
8471
8472 template-name:
8473 identifier
8474 operator-function-id
a723baf1
MM
8475
8476 A defect report has been filed about this issue.
8477
0d956474
GB
8478 A conversion-function-id cannot be a template name because they cannot
8479 be part of a template-id. In fact, looking at this code:
8480
8481 a.operator K<int>()
8482
8483 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
21526606 8484 It is impossible to call a templated conversion-function-id with an
0d956474
GB
8485 explicit argument list, since the only allowed template parameter is
8486 the type to which it is converting.
8487
a723baf1
MM
8488 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8489 `template' keyword, in a construction like:
8490
8491 T::template f<3>()
8492
8493 In that case `f' is taken to be a template-name, even though there
8494 is no way of knowing for sure.
8495
8496 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8497 name refers to a set of overloaded functions, at least one of which
8498 is a template, or an IDENTIFIER_NODE with the name of the template,
8499 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8500 names are looked up inside uninstantiated templates. */
8501
8502static tree
21526606
EC
8503cp_parser_template_name (cp_parser* parser,
8504 bool template_keyword_p,
a668c6ad
MM
8505 bool check_dependency_p,
8506 bool is_declaration,
8507 bool *is_identifier)
a723baf1
MM
8508{
8509 tree identifier;
8510 tree decl;
8511 tree fns;
8512
8513 /* If the next token is `operator', then we have either an
8514 operator-function-id or a conversion-function-id. */
8515 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8516 {
8517 /* We don't know whether we're looking at an
8518 operator-function-id or a conversion-function-id. */
8519 cp_parser_parse_tentatively (parser);
8520 /* Try an operator-function-id. */
8521 identifier = cp_parser_operator_function_id (parser);
8522 /* If that didn't work, try a conversion-function-id. */
8523 if (!cp_parser_parse_definitely (parser))
0d956474
GB
8524 {
8525 cp_parser_error (parser, "expected template-name");
8526 return error_mark_node;
8527 }
a723baf1
MM
8528 }
8529 /* Look for the identifier. */
8530 else
8531 identifier = cp_parser_identifier (parser);
21526606 8532
a723baf1
MM
8533 /* If we didn't find an identifier, we don't have a template-id. */
8534 if (identifier == error_mark_node)
8535 return error_mark_node;
8536
8537 /* If the name immediately followed the `template' keyword, then it
8538 is a template-name. However, if the next token is not `<', then
8539 we do not treat it as a template-name, since it is not being used
8540 as part of a template-id. This enables us to handle constructs
8541 like:
8542
8543 template <typename T> struct S { S(); };
8544 template <typename T> S<T>::S();
8545
8546 correctly. We would treat `S' as a template -- if it were `S<T>'
8547 -- but we do not if there is no `<'. */
a668c6ad
MM
8548
8549 if (processing_template_decl
f4abade9 8550 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
a668c6ad
MM
8551 {
8552 /* In a declaration, in a dependent context, we pretend that the
8553 "template" keyword was present in order to improve error
8554 recovery. For example, given:
21526606 8555
a668c6ad 8556 template <typename T> void f(T::X<int>);
21526606 8557
a668c6ad 8558 we want to treat "X<int>" as a template-id. */
21526606
EC
8559 if (is_declaration
8560 && !template_keyword_p
a668c6ad 8561 && parser->scope && TYPE_P (parser->scope)
a52eb3bc 8562 && check_dependency_p
4e0f4df5
GB
8563 && dependent_type_p (parser->scope)
8564 /* Do not do this for dtors (or ctors), since they never
8565 need the template keyword before their name. */
8566 && !constructor_name_p (identifier, parser->scope))
a668c6ad 8567 {
0c5e4866
NS
8568 cp_token_position start = 0;
8569
a668c6ad 8570 /* Explain what went wrong. */
2a13a625
GDR
8571 error ("non-template %qD used as template", identifier);
8572 inform ("use %<%T::template %D%> to indicate that it is a template",
4e0f4df5 8573 parser->scope, identifier);
0b16f8f4
VR
8574 /* If parsing tentatively, find the location of the "<" token. */
8575 if (cp_parser_simulate_error (parser))
8576 start = cp_lexer_token_position (parser->lexer, true);
a668c6ad
MM
8577 /* Parse the template arguments so that we can issue error
8578 messages about them. */
8579 cp_lexer_consume_token (parser->lexer);
8580 cp_parser_enclosed_template_argument_list (parser);
8581 /* Skip tokens until we find a good place from which to
8582 continue parsing. */
8583 cp_parser_skip_to_closing_parenthesis (parser,
8584 /*recovering=*/true,
8585 /*or_comma=*/true,
8586 /*consume_paren=*/false);
8587 /* If parsing tentatively, permanently remove the
8588 template argument list. That will prevent duplicate
8589 error messages from being issued about the missing
8590 "template" keyword. */
0c5e4866
NS
8591 if (start)
8592 cp_lexer_purge_tokens_after (parser->lexer, start);
a668c6ad
MM
8593 if (is_identifier)
8594 *is_identifier = true;
8595 return identifier;
8596 }
9d363a56
MM
8597
8598 /* If the "template" keyword is present, then there is generally
8599 no point in doing name-lookup, so we just return IDENTIFIER.
8600 But, if the qualifying scope is non-dependent then we can
8601 (and must) do name-lookup normally. */
8602 if (template_keyword_p
8603 && (!parser->scope
98ca843c 8604 || (TYPE_P (parser->scope)
9d363a56 8605 && dependent_type_p (parser->scope))))
a668c6ad
MM
8606 return identifier;
8607 }
a723baf1
MM
8608
8609 /* Look up the name. */
8610 decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 8611 none_type,
b0bc6e8e 8612 /*is_template=*/false,
eea9800f 8613 /*is_namespace=*/false,
8f78f01f
MM
8614 check_dependency_p,
8615 /*ambiguous_p=*/NULL);
a723baf1
MM
8616 decl = maybe_get_template_decl_from_type_decl (decl);
8617
8618 /* If DECL is a template, then the name was a template-name. */
8619 if (TREE_CODE (decl) == TEMPLATE_DECL)
8620 ;
21526606 8621 else
a723baf1
MM
8622 {
8623 /* The standard does not explicitly indicate whether a name that
8624 names a set of overloaded declarations, some of which are
8625 templates, is a template-name. However, such a name should
8626 be a template-name; otherwise, there is no way to form a
8627 template-id for the overloaded templates. */
8628 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8629 if (TREE_CODE (fns) == OVERLOAD)
8630 {
8631 tree fn;
21526606 8632
a723baf1
MM
8633 for (fn = fns; fn; fn = OVL_NEXT (fn))
8634 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8635 break;
8636 }
8637 else
8638 {
8639 /* Otherwise, the name does not name a template. */
8640 cp_parser_error (parser, "expected template-name");
8641 return error_mark_node;
8642 }
8643 }
8644
8645 /* If DECL is dependent, and refers to a function, then just return
8646 its name; we will look it up again during template instantiation. */
8647 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8648 {
8649 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
1fb3244a 8650 if (TYPE_P (scope) && dependent_type_p (scope))
a723baf1
MM
8651 return identifier;
8652 }
8653
8654 return decl;
8655}
8656
8657/* Parse a template-argument-list.
8658
8659 template-argument-list:
8660 template-argument
8661 template-argument-list , template-argument
8662
04c06002 8663 Returns a TREE_VEC containing the arguments. */
a723baf1
MM
8664
8665static tree
94edc4ab 8666cp_parser_template_argument_list (cp_parser* parser)
a723baf1 8667{
bf12d54d
NS
8668 tree fixed_args[10];
8669 unsigned n_args = 0;
8670 unsigned alloced = 10;
8671 tree *arg_ary = fixed_args;
8672 tree vec;
4bb8ca28 8673 bool saved_in_template_argument_list_p;
a723baf1 8674
4bb8ca28
MM
8675 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8676 parser->in_template_argument_list_p = true;
bf12d54d 8677 do
a723baf1
MM
8678 {
8679 tree argument;
8680
bf12d54d 8681 if (n_args)
04c06002 8682 /* Consume the comma. */
bf12d54d 8683 cp_lexer_consume_token (parser->lexer);
21526606 8684
a723baf1
MM
8685 /* Parse the template-argument. */
8686 argument = cp_parser_template_argument (parser);
bf12d54d
NS
8687 if (n_args == alloced)
8688 {
8689 alloced *= 2;
21526606 8690
bf12d54d
NS
8691 if (arg_ary == fixed_args)
8692 {
8693 arg_ary = xmalloc (sizeof (tree) * alloced);
8694 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8695 }
8696 else
8697 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8698 }
8699 arg_ary[n_args++] = argument;
a723baf1 8700 }
bf12d54d
NS
8701 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8702
8703 vec = make_tree_vec (n_args);
a723baf1 8704
bf12d54d
NS
8705 while (n_args--)
8706 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
21526606 8707
bf12d54d
NS
8708 if (arg_ary != fixed_args)
8709 free (arg_ary);
4bb8ca28 8710 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
bf12d54d 8711 return vec;
a723baf1
MM
8712}
8713
8714/* Parse a template-argument.
8715
8716 template-argument:
8717 assignment-expression
8718 type-id
8719 id-expression
8720
8721 The representation is that of an assignment-expression, type-id, or
8722 id-expression -- except that the qualified id-expression is
8723 evaluated, so that the value returned is either a DECL or an
21526606 8724 OVERLOAD.
d17811fd
MM
8725
8726 Although the standard says "assignment-expression", it forbids
8727 throw-expressions or assignments in the template argument.
8728 Therefore, we use "conditional-expression" instead. */
a723baf1
MM
8729
8730static tree
94edc4ab 8731cp_parser_template_argument (cp_parser* parser)
a723baf1
MM
8732{
8733 tree argument;
8734 bool template_p;
d17811fd 8735 bool address_p;
4d5297fa 8736 bool maybe_type_id = false;
d17811fd 8737 cp_token *token;
b3445994 8738 cp_id_kind idk;
d17811fd 8739 tree qualifying_class;
a723baf1
MM
8740
8741 /* There's really no way to know what we're looking at, so we just
21526606 8742 try each alternative in order.
a723baf1
MM
8743
8744 [temp.arg]
8745
8746 In a template-argument, an ambiguity between a type-id and an
8747 expression is resolved to a type-id, regardless of the form of
21526606 8748 the corresponding template-parameter.
a723baf1
MM
8749
8750 Therefore, we try a type-id first. */
8751 cp_parser_parse_tentatively (parser);
a723baf1 8752 argument = cp_parser_type_id (parser);
4d5297fa 8753 /* If there was no error parsing the type-id but the next token is a '>>',
21526606 8754 we probably found a typo for '> >'. But there are type-id which are
4d5297fa
GB
8755 also valid expressions. For instance:
8756
8757 struct X { int operator >> (int); };
8758 template <int V> struct Foo {};
8759 Foo<X () >> 5> r;
8760
8761 Here 'X()' is a valid type-id of a function type, but the user just
8762 wanted to write the expression "X() >> 5". Thus, we remember that we
8763 found a valid type-id, but we still try to parse the argument as an
8764 expression to see what happens. */
8765 if (!cp_parser_error_occurred (parser)
8766 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8767 {
8768 maybe_type_id = true;
8769 cp_parser_abort_tentative_parse (parser);
8770 }
8771 else
8772 {
8773 /* If the next token isn't a `,' or a `>', then this argument wasn't
8774 really finished. This means that the argument is not a valid
8775 type-id. */
8776 if (!cp_parser_next_token_ends_template_argument_p (parser))
8777 cp_parser_error (parser, "expected template-argument");
8778 /* If that worked, we're done. */
8779 if (cp_parser_parse_definitely (parser))
8780 return argument;
8781 }
a723baf1
MM
8782 /* We're still not sure what the argument will be. */
8783 cp_parser_parse_tentatively (parser);
8784 /* Try a template. */
21526606 8785 argument = cp_parser_id_expression (parser,
a723baf1
MM
8786 /*template_keyword_p=*/false,
8787 /*check_dependency_p=*/true,
f3c2dfc6
MM
8788 &template_p,
8789 /*declarator_p=*/false);
a723baf1
MM
8790 /* If the next token isn't a `,' or a `>', then this argument wasn't
8791 really finished. */
d17811fd 8792 if (!cp_parser_next_token_ends_template_argument_p (parser))
a723baf1
MM
8793 cp_parser_error (parser, "expected template-argument");
8794 if (!cp_parser_error_occurred (parser))
8795 {
f746161e
MM
8796 /* Figure out what is being referred to. If the id-expression
8797 was for a class template specialization, then we will have a
8798 TYPE_DECL at this point. There is no need to do name lookup
8799 at this point in that case. */
8800 if (TREE_CODE (argument) != TYPE_DECL)
8801 argument = cp_parser_lookup_name (parser, argument,
fc6a28d7 8802 none_type,
f746161e
MM
8803 /*is_template=*/template_p,
8804 /*is_namespace=*/false,
8f78f01f
MM
8805 /*check_dependency=*/true,
8806 /*ambiguous_p=*/NULL);
5b4acce1
KL
8807 if (TREE_CODE (argument) != TEMPLATE_DECL
8808 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
a723baf1
MM
8809 cp_parser_error (parser, "expected template-name");
8810 }
8811 if (cp_parser_parse_definitely (parser))
8812 return argument;
d17811fd
MM
8813 /* It must be a non-type argument. There permitted cases are given
8814 in [temp.arg.nontype]:
8815
8816 -- an integral constant-expression of integral or enumeration
8817 type; or
8818
8819 -- the name of a non-type template-parameter; or
8820
8821 -- the name of an object or function with external linkage...
8822
8823 -- the address of an object or function with external linkage...
8824
04c06002 8825 -- a pointer to member... */
d17811fd
MM
8826 /* Look for a non-type template parameter. */
8827 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8828 {
8829 cp_parser_parse_tentatively (parser);
8830 argument = cp_parser_primary_expression (parser,
8831 &idk,
8832 &qualifying_class);
8833 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8834 || !cp_parser_next_token_ends_template_argument_p (parser))
8835 cp_parser_simulate_error (parser);
8836 if (cp_parser_parse_definitely (parser))
8837 return argument;
8838 }
db24eb1f 8839
d17811fd
MM
8840 /* If the next token is "&", the argument must be the address of an
8841 object or function with external linkage. */
8842 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8843 if (address_p)
8844 cp_lexer_consume_token (parser->lexer);
8845 /* See if we might have an id-expression. */
8846 token = cp_lexer_peek_token (parser->lexer);
8847 if (token->type == CPP_NAME
8848 || token->keyword == RID_OPERATOR
8849 || token->type == CPP_SCOPE
8850 || token->type == CPP_TEMPLATE_ID
8851 || token->type == CPP_NESTED_NAME_SPECIFIER)
8852 {
8853 cp_parser_parse_tentatively (parser);
8854 argument = cp_parser_primary_expression (parser,
8855 &idk,
8856 &qualifying_class);
8857 if (cp_parser_error_occurred (parser)
8858 || !cp_parser_next_token_ends_template_argument_p (parser))
8859 cp_parser_abort_tentative_parse (parser);
8860 else
8861 {
db24eb1f
NS
8862 if (TREE_CODE (argument) == INDIRECT_REF)
8863 {
8864 gcc_assert (REFERENCE_REF_P (argument));
8865 argument = TREE_OPERAND (argument, 0);
8866 }
8867
d17811fd
MM
8868 if (qualifying_class)
8869 argument = finish_qualified_id_expr (qualifying_class,
8870 argument,
8871 /*done=*/true,
8872 address_p);
8873 if (TREE_CODE (argument) == VAR_DECL)
8874 {
8875 /* A variable without external linkage might still be a
8876 valid constant-expression, so no error is issued here
8877 if the external-linkage check fails. */
8878 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8879 cp_parser_simulate_error (parser);
8880 }
8881 else if (is_overloaded_fn (argument))
8882 /* All overloaded functions are allowed; if the external
8883 linkage test does not pass, an error will be issued
8884 later. */
8885 ;
8886 else if (address_p
21526606 8887 && (TREE_CODE (argument) == OFFSET_REF
d17811fd
MM
8888 || TREE_CODE (argument) == SCOPE_REF))
8889 /* A pointer-to-member. */
8890 ;
db24eb1f
NS
8891 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
8892 ;
d17811fd
MM
8893 else
8894 cp_parser_simulate_error (parser);
8895
8896 if (cp_parser_parse_definitely (parser))
8897 {
8898 if (address_p)
8899 argument = build_x_unary_op (ADDR_EXPR, argument);
8900 return argument;
8901 }
8902 }
8903 }
8904 /* If the argument started with "&", there are no other valid
8905 alternatives at this point. */
8906 if (address_p)
8907 {
8908 cp_parser_error (parser, "invalid non-type template argument");
8909 return error_mark_node;
8910 }
db24eb1f 8911
4d5297fa 8912 /* If the argument wasn't successfully parsed as a type-id followed
21526606 8913 by '>>', the argument can only be a constant expression now.
4d5297fa
GB
8914 Otherwise, we try parsing the constant-expression tentatively,
8915 because the argument could really be a type-id. */
8916 if (maybe_type_id)
8917 cp_parser_parse_tentatively (parser);
21526606 8918 argument = cp_parser_constant_expression (parser,
d17811fd
MM
8919 /*allow_non_constant_p=*/false,
8920 /*non_constant_p=*/NULL);
9baa27a9 8921 argument = fold_non_dependent_expr (argument);
4d5297fa
GB
8922 if (!maybe_type_id)
8923 return argument;
8924 if (!cp_parser_next_token_ends_template_argument_p (parser))
8925 cp_parser_error (parser, "expected template-argument");
8926 if (cp_parser_parse_definitely (parser))
8927 return argument;
8928 /* We did our best to parse the argument as a non type-id, but that
8929 was the only alternative that matched (albeit with a '>' after
21526606 8930 it). We can assume it's just a typo from the user, and a
4d5297fa
GB
8931 diagnostic will then be issued. */
8932 return cp_parser_type_id (parser);
a723baf1
MM
8933}
8934
8935/* Parse an explicit-instantiation.
8936
8937 explicit-instantiation:
21526606 8938 template declaration
a723baf1
MM
8939
8940 Although the standard says `declaration', what it really means is:
8941
8942 explicit-instantiation:
21526606 8943 template decl-specifier-seq [opt] declarator [opt] ;
a723baf1
MM
8944
8945 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8946 supposed to be allowed. A defect report has been filed about this
21526606 8947 issue.
a723baf1
MM
8948
8949 GNU Extension:
21526606 8950
a723baf1 8951 explicit-instantiation:
21526606 8952 storage-class-specifier template
a723baf1 8953 decl-specifier-seq [opt] declarator [opt] ;
21526606 8954 function-specifier template
a723baf1
MM
8955 decl-specifier-seq [opt] declarator [opt] ; */
8956
8957static void
94edc4ab 8958cp_parser_explicit_instantiation (cp_parser* parser)
a723baf1 8959{
560ad596 8960 int declares_class_or_enum;
62d1db17 8961 cp_decl_specifier_seq decl_specifiers;
a723baf1
MM
8962 tree extension_specifier = NULL_TREE;
8963
8964 /* Look for an (optional) storage-class-specifier or
8965 function-specifier. */
8966 if (cp_parser_allow_gnu_extensions_p (parser))
8967 {
21526606 8968 extension_specifier
a723baf1
MM
8969 = cp_parser_storage_class_specifier_opt (parser);
8970 if (!extension_specifier)
98ca843c 8971 extension_specifier
62d1db17
MM
8972 = cp_parser_function_specifier_opt (parser,
8973 /*decl_specs=*/NULL);
a723baf1
MM
8974 }
8975
8976 /* Look for the `template' keyword. */
8977 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8978 /* Let the front end know that we are processing an explicit
8979 instantiation. */
8980 begin_explicit_instantiation ();
8981 /* [temp.explicit] says that we are supposed to ignore access
8982 control while processing explicit instantiation directives. */
78757caa 8983 push_deferring_access_checks (dk_no_check);
a723baf1 8984 /* Parse a decl-specifier-seq. */
62d1db17
MM
8985 cp_parser_decl_specifier_seq (parser,
8986 CP_PARSER_FLAGS_OPTIONAL,
8987 &decl_specifiers,
8988 &declares_class_or_enum);
a723baf1
MM
8989 /* If there was exactly one decl-specifier, and it declared a class,
8990 and there's no declarator, then we have an explicit type
8991 instantiation. */
8992 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8993 {
8994 tree type;
8995
62d1db17 8996 type = check_tag_decl (&decl_specifiers);
b7fc8b57
KL
8997 /* Turn access control back on for names used during
8998 template instantiation. */
8999 pop_deferring_access_checks ();
a723baf1
MM
9000 if (type)
9001 do_type_instantiation (type, extension_specifier, /*complain=*/1);
9002 }
9003 else
9004 {
058b15c1 9005 cp_declarator *declarator;
a723baf1
MM
9006 tree decl;
9007
9008 /* Parse the declarator. */
21526606 9009 declarator
62b8a44e 9010 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 9011 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
9012 /*parenthesized_p=*/NULL,
9013 /*member_p=*/false);
fc6a28d7
MM
9014 if (declares_class_or_enum & 2)
9015 cp_parser_check_for_definition_in_return_type (declarator,
9016 decl_specifiers.type);
058b15c1 9017 if (declarator != cp_error_declarator)
216bb6e1 9018 {
62d1db17 9019 decl = grokdeclarator (declarator, &decl_specifiers,
216bb6e1
MM
9020 NORMAL, 0, NULL);
9021 /* Turn access control back on for names used during
9022 template instantiation. */
9023 pop_deferring_access_checks ();
9024 /* Do the explicit instantiation. */
9025 do_decl_instantiation (decl, extension_specifier);
9026 }
9027 else
9028 {
9029 pop_deferring_access_checks ();
9030 /* Skip the body of the explicit instantiation. */
9031 cp_parser_skip_to_end_of_statement (parser);
9032 }
a723baf1
MM
9033 }
9034 /* We're done with the instantiation. */
9035 end_explicit_instantiation ();
a723baf1 9036
e0860732 9037 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1
MM
9038}
9039
9040/* Parse an explicit-specialization.
9041
9042 explicit-specialization:
21526606 9043 template < > declaration
a723baf1
MM
9044
9045 Although the standard says `declaration', what it really means is:
9046
9047 explicit-specialization:
9048 template <> decl-specifier [opt] init-declarator [opt] ;
21526606 9049 template <> function-definition
a723baf1
MM
9050 template <> explicit-specialization
9051 template <> template-declaration */
9052
9053static void
94edc4ab 9054cp_parser_explicit_specialization (cp_parser* parser)
a723baf1
MM
9055{
9056 /* Look for the `template' keyword. */
9057 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9058 /* Look for the `<'. */
9059 cp_parser_require (parser, CPP_LESS, "`<'");
9060 /* Look for the `>'. */
9061 cp_parser_require (parser, CPP_GREATER, "`>'");
9062 /* We have processed another parameter list. */
9063 ++parser->num_template_parameter_lists;
9064 /* Let the front end know that we are beginning a specialization. */
9065 begin_specialization ();
9066
9067 /* If the next keyword is `template', we need to figure out whether
9068 or not we're looking a template-declaration. */
9069 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9070 {
9071 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9072 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9073 cp_parser_template_declaration_after_export (parser,
9074 /*member_p=*/false);
9075 else
9076 cp_parser_explicit_specialization (parser);
9077 }
9078 else
9079 /* Parse the dependent declaration. */
21526606 9080 cp_parser_single_declaration (parser,
a723baf1
MM
9081 /*member_p=*/false,
9082 /*friend_p=*/NULL);
9083
9084 /* We're done with the specialization. */
9085 end_specialization ();
9086 /* We're done with this parameter list. */
9087 --parser->num_template_parameter_lists;
9088}
9089
9090/* Parse a type-specifier.
9091
9092 type-specifier:
9093 simple-type-specifier
9094 class-specifier
9095 enum-specifier
9096 elaborated-type-specifier
9097 cv-qualifier
9098
9099 GNU Extension:
9100
9101 type-specifier:
9102 __complex__
9103
62d1db17
MM
9104 Returns a representation of the type-specifier. For a
9105 class-specifier, enum-specifier, or elaborated-type-specifier, a
9106 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
a723baf1 9107
eb1aef53
KL
9108 The parser flags FLAGS is used to control type-specifier parsing.
9109
9110 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9111 in a decl-specifier-seq.
a723baf1
MM
9112
9113 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9114 class-specifier, enum-specifier, or elaborated-type-specifier, then
83a00410 9115 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
560ad596
MM
9116 if a type is declared; 2 if it is defined. Otherwise, it is set to
9117 zero.
a723baf1
MM
9118
9119 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9120 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9121 is set to FALSE. */
9122
9123static tree
21526606
EC
9124cp_parser_type_specifier (cp_parser* parser,
9125 cp_parser_flags flags,
62d1db17 9126 cp_decl_specifier_seq *decl_specs,
94edc4ab 9127 bool is_declaration,
560ad596 9128 int* declares_class_or_enum,
94edc4ab 9129 bool* is_cv_qualifier)
a723baf1
MM
9130{
9131 tree type_spec = NULL_TREE;
9132 cp_token *token;
9133 enum rid keyword;
62d1db17 9134 cp_decl_spec ds = ds_last;
a723baf1
MM
9135
9136 /* Assume this type-specifier does not declare a new type. */
9137 if (declares_class_or_enum)
543ca912 9138 *declares_class_or_enum = 0;
a723baf1
MM
9139 /* And that it does not specify a cv-qualifier. */
9140 if (is_cv_qualifier)
9141 *is_cv_qualifier = false;
9142 /* Peek at the next token. */
9143 token = cp_lexer_peek_token (parser->lexer);
9144
9145 /* If we're looking at a keyword, we can use that to guide the
9146 production we choose. */
9147 keyword = token->keyword;
9148 switch (keyword)
9149 {
ff4eb0b5
ZW
9150 case RID_ENUM:
9151 /* 'enum' [identifier] '{' introduces an enum-specifier;
9152 'enum' <anything else> introduces an elaborated-type-specifier. */
9153 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9154 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9155 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9156 == CPP_OPEN_BRACE))
9157 {
a5e51518
KL
9158 if (parser->num_template_parameter_lists)
9159 {
9160 error ("template declaration of %qs", "enum");
9161 cp_parser_skip_to_end_of_block_or_statement (parser);
9162 type_spec = error_mark_node;
9163 }
9164 else
9165 type_spec = cp_parser_enum_specifier (parser);
9166
ff4eb0b5
ZW
9167 if (declares_class_or_enum)
9168 *declares_class_or_enum = 2;
9169 if (decl_specs)
9170 cp_parser_set_decl_spec_type (decl_specs,
9171 type_spec,
9172 /*user_defined_p=*/true);
9173 return type_spec;
9174 }
9175 else
9176 goto elaborated_type_specifier;
9177
a723baf1
MM
9178 /* Any of these indicate either a class-specifier, or an
9179 elaborated-type-specifier. */
9180 case RID_CLASS:
9181 case RID_STRUCT:
9182 case RID_UNION:
a723baf1 9183 /* Parse tentatively so that we can back up if we don't find a
ff4eb0b5 9184 class-specifier. */
a723baf1 9185 cp_parser_parse_tentatively (parser);
ff4eb0b5
ZW
9186 /* Look for the class-specifier. */
9187 type_spec = cp_parser_class_specifier (parser);
a723baf1
MM
9188 /* If that worked, we're done. */
9189 if (cp_parser_parse_definitely (parser))
9190 {
9191 if (declares_class_or_enum)
560ad596 9192 *declares_class_or_enum = 2;
62d1db17
MM
9193 if (decl_specs)
9194 cp_parser_set_decl_spec_type (decl_specs,
9195 type_spec,
9196 /*user_defined_p=*/true);
a723baf1
MM
9197 return type_spec;
9198 }
9199
9200 /* Fall through. */
ff4eb0b5
ZW
9201 elaborated_type_specifier:
9202 /* We're declaring (not defining) a class or enum. */
9203 if (declares_class_or_enum)
9204 *declares_class_or_enum = 1;
a723baf1 9205
ff4eb0b5 9206 /* Fall through. */
a723baf1
MM
9207 case RID_TYPENAME:
9208 /* Look for an elaborated-type-specifier. */
98ca843c
EC
9209 type_spec
9210 = (cp_parser_elaborated_type_specifier
62d1db17
MM
9211 (parser,
9212 decl_specs && decl_specs->specs[(int) ds_friend],
9213 is_declaration));
62d1db17
MM
9214 if (decl_specs)
9215 cp_parser_set_decl_spec_type (decl_specs,
9216 type_spec,
9217 /*user_defined_p=*/true);
a723baf1
MM
9218 return type_spec;
9219
9220 case RID_CONST:
62d1db17
MM
9221 ds = ds_const;
9222 if (is_cv_qualifier)
9223 *is_cv_qualifier = true;
9224 break;
98ca843c 9225
a723baf1 9226 case RID_VOLATILE:
62d1db17 9227 ds = ds_volatile;
a723baf1
MM
9228 if (is_cv_qualifier)
9229 *is_cv_qualifier = true;
62d1db17 9230 break;
a723baf1 9231
62d1db17
MM
9232 case RID_RESTRICT:
9233 ds = ds_restrict;
9234 if (is_cv_qualifier)
9235 *is_cv_qualifier = true;
9236 break;
a723baf1
MM
9237
9238 case RID_COMPLEX:
9239 /* The `__complex__' keyword is a GNU extension. */
62d1db17
MM
9240 ds = ds_complex;
9241 break;
a723baf1
MM
9242
9243 default:
9244 break;
9245 }
9246
62d1db17
MM
9247 /* Handle simple keywords. */
9248 if (ds != ds_last)
9249 {
9250 if (decl_specs)
9251 {
9252 ++decl_specs->specs[(int)ds];
9253 decl_specs->any_specifiers_p = true;
9254 }
9255 return cp_lexer_consume_token (parser->lexer)->value;
9256 }
9257
a723baf1
MM
9258 /* If we do not already have a type-specifier, assume we are looking
9259 at a simple-type-specifier. */
98ca843c 9260 type_spec = cp_parser_simple_type_specifier (parser,
62d1db17
MM
9261 decl_specs,
9262 flags);
a723baf1
MM
9263
9264 /* If we didn't find a type-specifier, and a type-specifier was not
9265 optional in this context, issue an error message. */
9266 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9267 {
9268 cp_parser_error (parser, "expected type specifier");
9269 return error_mark_node;
9270 }
9271
9272 return type_spec;
9273}
9274
9275/* Parse a simple-type-specifier.
9276
9277 simple-type-specifier:
9278 :: [opt] nested-name-specifier [opt] type-name
9279 :: [opt] nested-name-specifier template template-id
9280 char
9281 wchar_t
9282 bool
9283 short
9284 int
9285 long
9286 signed
9287 unsigned
9288 float
9289 double
21526606 9290 void
a723baf1
MM
9291
9292 GNU Extension:
9293
9294 simple-type-specifier:
9295 __typeof__ unary-expression
9296 __typeof__ ( type-id )
9297
62d1db17
MM
9298 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9299 appropriately updated. */
a723baf1
MM
9300
9301static tree
98ca843c 9302cp_parser_simple_type_specifier (cp_parser* parser,
62d1db17
MM
9303 cp_decl_specifier_seq *decl_specs,
9304 cp_parser_flags flags)
a723baf1
MM
9305{
9306 tree type = NULL_TREE;
9307 cp_token *token;
9308
9309 /* Peek at the next token. */
9310 token = cp_lexer_peek_token (parser->lexer);
9311
9312 /* If we're looking at a keyword, things are easy. */
9313 switch (token->keyword)
9314 {
9315 case RID_CHAR:
62d1db17
MM
9316 if (decl_specs)
9317 decl_specs->explicit_char_p = true;
4b0d3cbe
MM
9318 type = char_type_node;
9319 break;
a723baf1 9320 case RID_WCHAR:
4b0d3cbe
MM
9321 type = wchar_type_node;
9322 break;
a723baf1 9323 case RID_BOOL:
4b0d3cbe
MM
9324 type = boolean_type_node;
9325 break;
a723baf1 9326 case RID_SHORT:
62d1db17
MM
9327 if (decl_specs)
9328 ++decl_specs->specs[(int) ds_short];
4b0d3cbe
MM
9329 type = short_integer_type_node;
9330 break;
a723baf1 9331 case RID_INT:
62d1db17
MM
9332 if (decl_specs)
9333 decl_specs->explicit_int_p = true;
4b0d3cbe
MM
9334 type = integer_type_node;
9335 break;
a723baf1 9336 case RID_LONG:
62d1db17
MM
9337 if (decl_specs)
9338 ++decl_specs->specs[(int) ds_long];
4b0d3cbe
MM
9339 type = long_integer_type_node;
9340 break;
a723baf1 9341 case RID_SIGNED:
62d1db17
MM
9342 if (decl_specs)
9343 ++decl_specs->specs[(int) ds_signed];
4b0d3cbe
MM
9344 type = integer_type_node;
9345 break;
a723baf1 9346 case RID_UNSIGNED:
62d1db17
MM
9347 if (decl_specs)
9348 ++decl_specs->specs[(int) ds_unsigned];
4b0d3cbe
MM
9349 type = unsigned_type_node;
9350 break;
a723baf1 9351 case RID_FLOAT:
4b0d3cbe
MM
9352 type = float_type_node;
9353 break;
a723baf1 9354 case RID_DOUBLE:
4b0d3cbe
MM
9355 type = double_type_node;
9356 break;
a723baf1 9357 case RID_VOID:
4b0d3cbe
MM
9358 type = void_type_node;
9359 break;
a723baf1
MM
9360
9361 case RID_TYPEOF:
62d1db17
MM
9362 /* Consume the `typeof' token. */
9363 cp_lexer_consume_token (parser->lexer);
9364 /* Parse the operand to `typeof'. */
9365 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9366 /* If it is not already a TYPE, take its type. */
9367 if (!TYPE_P (type))
9368 type = finish_typeof (type);
9369
9370 if (decl_specs)
9371 cp_parser_set_decl_spec_type (decl_specs, type,
9372 /*user_defined_p=*/true);
98ca843c 9373
62d1db17 9374 return type;
a723baf1
MM
9375
9376 default:
9377 break;
9378 }
9379
4b0d3cbe
MM
9380 /* If the type-specifier was for a built-in type, we're done. */
9381 if (type)
9382 {
9383 tree id;
9384
62d1db17
MM
9385 /* Record the type. */
9386 if (decl_specs
9387 && (token->keyword != RID_SIGNED
9388 && token->keyword != RID_UNSIGNED
9389 && token->keyword != RID_SHORT
9390 && token->keyword != RID_LONG))
98ca843c 9391 cp_parser_set_decl_spec_type (decl_specs,
62d1db17
MM
9392 type,
9393 /*user_defined=*/false);
9394 if (decl_specs)
9395 decl_specs->any_specifiers_p = true;
9396
4b0d3cbe
MM
9397 /* Consume the token. */
9398 id = cp_lexer_consume_token (parser->lexer)->value;
0d956474
GB
9399
9400 /* There is no valid C++ program where a non-template type is
9401 followed by a "<". That usually indicates that the user thought
9402 that the type was a template. */
9403 cp_parser_check_for_invalid_template_id (parser, type);
9404
62d1db17 9405 return TYPE_NAME (type);
4b0d3cbe
MM
9406 }
9407
a723baf1 9408 /* The type-specifier must be a user-defined type. */
21526606 9409 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
a723baf1 9410 {
0c1a1ecd 9411 bool qualified_p;
f68e4dc8 9412 bool global_p;
0c1a1ecd 9413
a723baf1
MM
9414 /* Don't gobble tokens or issue error messages if this is an
9415 optional type-specifier. */
9416 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9417 cp_parser_parse_tentatively (parser);
9418
9419 /* Look for the optional `::' operator. */
f68e4dc8 9420 global_p
da740453
MM
9421 = (cp_parser_global_scope_opt (parser,
9422 /*current_scope_valid_p=*/false)
9423 != NULL_TREE);
a723baf1 9424 /* Look for the nested-name specifier. */
0c1a1ecd
MM
9425 qualified_p
9426 = (cp_parser_nested_name_specifier_opt (parser,
9427 /*typename_keyword_p=*/false,
9428 /*check_dependency_p=*/true,
9429 /*type_p=*/false,
6661a85f
EB
9430 /*is_declaration=*/false)
9431 != NULL_TREE);
a723baf1
MM
9432 /* If we have seen a nested-name-specifier, and the next token
9433 is `template', then we are using the template-id production. */
21526606 9434 if (parser->scope
a723baf1
MM
9435 && cp_parser_optional_template_keyword (parser))
9436 {
9437 /* Look for the template-id. */
21526606 9438 type = cp_parser_template_id (parser,
a723baf1 9439 /*template_keyword_p=*/true,
a668c6ad
MM
9440 /*check_dependency_p=*/true,
9441 /*is_declaration=*/false);
a723baf1
MM
9442 /* If the template-id did not name a type, we are out of
9443 luck. */
9444 if (TREE_CODE (type) != TYPE_DECL)
9445 {
9446 cp_parser_error (parser, "expected template-id for type");
9447 type = NULL_TREE;
9448 }
9449 }
9450 /* Otherwise, look for a type-name. */
9451 else
4bb8ca28 9452 type = cp_parser_type_name (parser);
0c1a1ecd 9453 /* Keep track of all name-lookups performed in class scopes. */
98ca843c 9454 if (type
f68e4dc8 9455 && !global_p
0c1a1ecd 9456 && !qualified_p
98ca843c 9457 && TREE_CODE (type) == TYPE_DECL
0c1a1ecd
MM
9458 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9459 maybe_note_name_used_in_class (DECL_NAME (type), type);
a723baf1 9460 /* If it didn't work out, we don't have a TYPE. */
21526606 9461 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
a723baf1
MM
9462 && !cp_parser_parse_definitely (parser))
9463 type = NULL_TREE;
62d1db17
MM
9464 if (type && decl_specs)
9465 cp_parser_set_decl_spec_type (decl_specs, type,
9466 /*user_defined=*/true);
a723baf1
MM
9467 }
9468
9469 /* If we didn't get a type-name, issue an error message. */
9470 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9471 {
9472 cp_parser_error (parser, "expected type-name");
9473 return error_mark_node;
9474 }
9475
a668c6ad
MM
9476 /* There is no valid C++ program where a non-template type is
9477 followed by a "<". That usually indicates that the user thought
9478 that the type was a template. */
4bb8ca28 9479 if (type && type != error_mark_node)
ee43dab5 9480 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
ec75414f 9481
a723baf1
MM
9482 return type;
9483}
9484
9485/* Parse a type-name.
9486
9487 type-name:
9488 class-name
9489 enum-name
21526606 9490 typedef-name
a723baf1
MM
9491
9492 enum-name:
9493 identifier
9494
9495 typedef-name:
21526606 9496 identifier
a723baf1
MM
9497
9498 Returns a TYPE_DECL for the the type. */
9499
9500static tree
94edc4ab 9501cp_parser_type_name (cp_parser* parser)
a723baf1
MM
9502{
9503 tree type_decl;
9504 tree identifier;
9505
9506 /* We can't know yet whether it is a class-name or not. */
9507 cp_parser_parse_tentatively (parser);
9508 /* Try a class-name. */
21526606 9509 type_decl = cp_parser_class_name (parser,
a723baf1
MM
9510 /*typename_keyword_p=*/false,
9511 /*template_keyword_p=*/false,
fc6a28d7 9512 none_type,
a723baf1 9513 /*check_dependency_p=*/true,
a668c6ad
MM
9514 /*class_head_p=*/false,
9515 /*is_declaration=*/false);
a723baf1
MM
9516 /* If it's not a class-name, keep looking. */
9517 if (!cp_parser_parse_definitely (parser))
9518 {
9519 /* It must be a typedef-name or an enum-name. */
9520 identifier = cp_parser_identifier (parser);
9521 if (identifier == error_mark_node)
9522 return error_mark_node;
21526606 9523
a723baf1
MM
9524 /* Look up the type-name. */
9525 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9526 /* Issue an error if we did not find a type-name. */
9527 if (TREE_CODE (type_decl) != TYPE_DECL)
9528 {
4bb8ca28 9529 if (!cp_parser_simulate_error (parser))
21526606 9530 cp_parser_name_lookup_error (parser, identifier, type_decl,
4bb8ca28 9531 "is not a type");
a723baf1
MM
9532 type_decl = error_mark_node;
9533 }
9534 /* Remember that the name was used in the definition of the
9535 current class so that we can check later to see if the
9536 meaning would have been different after the class was
9537 entirely defined. */
9538 else if (type_decl != error_mark_node
9539 && !parser->scope)
9540 maybe_note_name_used_in_class (identifier, type_decl);
9541 }
21526606 9542
a723baf1
MM
9543 return type_decl;
9544}
9545
9546
9547/* Parse an elaborated-type-specifier. Note that the grammar given
9548 here incorporates the resolution to DR68.
9549
9550 elaborated-type-specifier:
9551 class-key :: [opt] nested-name-specifier [opt] identifier
9552 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9553 enum :: [opt] nested-name-specifier [opt] identifier
9554 typename :: [opt] nested-name-specifier identifier
21526606
EC
9555 typename :: [opt] nested-name-specifier template [opt]
9556 template-id
a723baf1 9557
360d1b99
MM
9558 GNU extension:
9559
9560 elaborated-type-specifier:
9561 class-key attributes :: [opt] nested-name-specifier [opt] identifier
21526606 9562 class-key attributes :: [opt] nested-name-specifier [opt]
360d1b99
MM
9563 template [opt] template-id
9564 enum attributes :: [opt] nested-name-specifier [opt] identifier
9565
a723baf1
MM
9566 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9567 declared `friend'. If IS_DECLARATION is TRUE, then this
9568 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9569 something is being declared.
9570
9571 Returns the TYPE specified. */
9572
9573static tree
21526606
EC
9574cp_parser_elaborated_type_specifier (cp_parser* parser,
9575 bool is_friend,
94edc4ab 9576 bool is_declaration)
a723baf1
MM
9577{
9578 enum tag_types tag_type;
9579 tree identifier;
9580 tree type = NULL_TREE;
360d1b99 9581 tree attributes = NULL_TREE;
a723baf1
MM
9582
9583 /* See if we're looking at the `enum' keyword. */
9584 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9585 {
9586 /* Consume the `enum' token. */
9587 cp_lexer_consume_token (parser->lexer);
9588 /* Remember that it's an enumeration type. */
9589 tag_type = enum_type;
360d1b99
MM
9590 /* Parse the attributes. */
9591 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
9592 }
9593 /* Or, it might be `typename'. */
9594 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9595 RID_TYPENAME))
9596 {
9597 /* Consume the `typename' token. */
9598 cp_lexer_consume_token (parser->lexer);
9599 /* Remember that it's a `typename' type. */
9600 tag_type = typename_type;
9601 /* The `typename' keyword is only allowed in templates. */
9602 if (!processing_template_decl)
2a13a625 9603 pedwarn ("using %<typename%> outside of template");
a723baf1
MM
9604 }
9605 /* Otherwise it must be a class-key. */
9606 else
9607 {
9608 tag_type = cp_parser_class_key (parser);
9609 if (tag_type == none_type)
9610 return error_mark_node;
360d1b99
MM
9611 /* Parse the attributes. */
9612 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
9613 }
9614
9615 /* Look for the `::' operator. */
21526606 9616 cp_parser_global_scope_opt (parser,
a723baf1
MM
9617 /*current_scope_valid_p=*/false);
9618 /* Look for the nested-name-specifier. */
9619 if (tag_type == typename_type)
8fa1ad0e
MM
9620 {
9621 if (cp_parser_nested_name_specifier (parser,
9622 /*typename_keyword_p=*/true,
9623 /*check_dependency_p=*/true,
a668c6ad 9624 /*type_p=*/true,
21526606 9625 is_declaration)
8fa1ad0e
MM
9626 == error_mark_node)
9627 return error_mark_node;
9628 }
a723baf1
MM
9629 else
9630 /* Even though `typename' is not present, the proposed resolution
9631 to Core Issue 180 says that in `class A<T>::B', `B' should be
9632 considered a type-name, even if `A<T>' is dependent. */
9633 cp_parser_nested_name_specifier_opt (parser,
9634 /*typename_keyword_p=*/true,
9635 /*check_dependency_p=*/true,
a668c6ad
MM
9636 /*type_p=*/true,
9637 is_declaration);
a723baf1
MM
9638 /* For everything but enumeration types, consider a template-id. */
9639 if (tag_type != enum_type)
9640 {
9641 bool template_p = false;
9642 tree decl;
9643
9644 /* Allow the `template' keyword. */
9645 template_p = cp_parser_optional_template_keyword (parser);
9646 /* If we didn't see `template', we don't know if there's a
9647 template-id or not. */
9648 if (!template_p)
9649 cp_parser_parse_tentatively (parser);
9650 /* Parse the template-id. */
9651 decl = cp_parser_template_id (parser, template_p,
a668c6ad
MM
9652 /*check_dependency_p=*/true,
9653 is_declaration);
a723baf1
MM
9654 /* If we didn't find a template-id, look for an ordinary
9655 identifier. */
9656 if (!template_p && !cp_parser_parse_definitely (parser))
9657 ;
9658 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9659 in effect, then we must assume that, upon instantiation, the
9660 template will correspond to a class. */
9661 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9662 && tag_type == typename_type)
9663 type = make_typename_type (parser->scope, decl,
fc6a28d7 9664 typename_type,
a723baf1 9665 /*complain=*/1);
21526606 9666 else
a723baf1
MM
9667 type = TREE_TYPE (decl);
9668 }
9669
9670 /* For an enumeration type, consider only a plain identifier. */
9671 if (!type)
9672 {
9673 identifier = cp_parser_identifier (parser);
9674
9675 if (identifier == error_mark_node)
eb5abb39
NS
9676 {
9677 parser->scope = NULL_TREE;
9678 return error_mark_node;
9679 }
a723baf1
MM
9680
9681 /* For a `typename', we needn't call xref_tag. */
0c88d886
MM
9682 if (tag_type == typename_type
9683 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
21526606 9684 return cp_parser_make_typename_type (parser, parser->scope,
2097b5f2 9685 identifier);
a723baf1
MM
9686 /* Look up a qualified name in the usual way. */
9687 if (parser->scope)
9688 {
9689 tree decl;
9690
21526606 9691 decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 9692 tag_type,
b0bc6e8e 9693 /*is_template=*/false,
eea9800f 9694 /*is_namespace=*/false,
8f78f01f
MM
9695 /*check_dependency=*/true,
9696 /*ambiguous_p=*/NULL);
710b73e6
KL
9697
9698 /* If we are parsing friend declaration, DECL may be a
9699 TEMPLATE_DECL tree node here. However, we need to check
9700 whether this TEMPLATE_DECL results in valid code. Consider
9701 the following example:
9702
9703 namespace N {
9704 template <class T> class C {};
9705 }
9706 class X {
9707 template <class T> friend class N::C; // #1, valid code
9708 };
9709 template <class T> class Y {
9710 friend class N::C; // #2, invalid code
9711 };
9712
9713 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9714 name lookup of `N::C'. We see that friend declaration must
9715 be template for the code to be valid. Note that
9716 processing_template_decl does not work here since it is
9717 always 1 for the above two cases. */
9718
21526606 9719 decl = (cp_parser_maybe_treat_template_as_class
710b73e6
KL
9720 (decl, /*tag_name_p=*/is_friend
9721 && parser->num_template_parameter_lists));
a723baf1
MM
9722
9723 if (TREE_CODE (decl) != TYPE_DECL)
9724 {
0c88d886
MM
9725 cp_parser_diagnose_invalid_type_name (parser,
9726 parser->scope,
9727 identifier);
a723baf1
MM
9728 return error_mark_node;
9729 }
560ad596
MM
9730
9731 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
21526606 9732 check_elaborated_type_specifier
4b0d3cbe 9733 (tag_type, decl,
560ad596
MM
9734 (parser->num_template_parameter_lists
9735 || DECL_SELF_REFERENCE_P (decl)));
a723baf1
MM
9736
9737 type = TREE_TYPE (decl);
9738 }
21526606 9739 else
a723baf1
MM
9740 {
9741 /* An elaborated-type-specifier sometimes introduces a new type and
9742 sometimes names an existing type. Normally, the rule is that it
9743 introduces a new type only if there is not an existing type of
9744 the same name already in scope. For example, given:
9745
9746 struct S {};
9747 void f() { struct S s; }
9748
9749 the `struct S' in the body of `f' is the same `struct S' as in
9750 the global scope; the existing definition is used. However, if
21526606 9751 there were no global declaration, this would introduce a new
a723baf1
MM
9752 local class named `S'.
9753
9754 An exception to this rule applies to the following code:
9755
9756 namespace N { struct S; }
9757
9758 Here, the elaborated-type-specifier names a new type
9759 unconditionally; even if there is already an `S' in the
9760 containing scope this declaration names a new type.
9761 This exception only applies if the elaborated-type-specifier
9762 forms the complete declaration:
9763
21526606 9764 [class.name]
a723baf1
MM
9765
9766 A declaration consisting solely of `class-key identifier ;' is
9767 either a redeclaration of the name in the current scope or a
9768 forward declaration of the identifier as a class name. It
9769 introduces the name into the current scope.
9770
9771 We are in this situation precisely when the next token is a `;'.
9772
9773 An exception to the exception is that a `friend' declaration does
9774 *not* name a new type; i.e., given:
9775
9776 struct S { friend struct T; };
9777
21526606 9778 `T' is not a new type in the scope of `S'.
a723baf1
MM
9779
9780 Also, `new struct S' or `sizeof (struct S)' never results in the
9781 definition of a new type; a new type can only be declared in a
9bcb9aae 9782 declaration context. */
a723baf1 9783
29ef83de
KL
9784 tag_scope ts;
9785 if (is_friend)
9786 /* Friends have special name lookup rules. */
9787 ts = ts_within_enclosing_non_class;
9788 else if (is_declaration
9789 && cp_lexer_next_token_is (parser->lexer,
9790 CPP_SEMICOLON))
9791 /* This is a `class-key identifier ;' */
9792 ts = ts_current;
9793 else
9794 ts = ts_global;
9795
e0fed25b
DS
9796 /* Warn about attributes. They are ignored. */
9797 if (attributes)
9798 warning ("type attributes are honored only at type definition");
9799
29ef83de 9800 type = xref_tag (tag_type, identifier, ts,
cbd63935 9801 parser->num_template_parameter_lists);
a723baf1
MM
9802 }
9803 }
9804 if (tag_type != enum_type)
9805 cp_parser_check_class_key (tag_type, type);
ee43dab5
MM
9806
9807 /* A "<" cannot follow an elaborated type specifier. If that
9808 happens, the user was probably trying to form a template-id. */
9809 cp_parser_check_for_invalid_template_id (parser, type);
9810
a723baf1
MM
9811 return type;
9812}
9813
9814/* Parse an enum-specifier.
9815
9816 enum-specifier:
9817 enum identifier [opt] { enumerator-list [opt] }
9818
f6af9a15
MA
9819 GNU Extensions:
9820 enum identifier [opt] { enumerator-list [opt] } attributes
9821
a723baf1
MM
9822 Returns an ENUM_TYPE representing the enumeration. */
9823
9824static tree
94edc4ab 9825cp_parser_enum_specifier (cp_parser* parser)
a723baf1 9826{
ff4eb0b5 9827 tree identifier;
a723baf1
MM
9828 tree type;
9829
ff4eb0b5
ZW
9830 /* Caller guarantees that the current token is 'enum', an identifier
9831 possibly follows, and the token after that is an opening brace.
9832 If we don't have an identifier, fabricate an anonymous name for
9833 the enumeration being defined. */
9834 cp_lexer_consume_token (parser->lexer);
a723baf1 9835
ff4eb0b5 9836 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
a723baf1 9837 identifier = cp_parser_identifier (parser);
ff4eb0b5
ZW
9838 else
9839 identifier = make_anon_name ();
a723baf1 9840
a723baf1
MM
9841 /* Issue an error message if type-definitions are forbidden here. */
9842 cp_parser_check_type_definition (parser);
9843
2cfe82fe
ZW
9844 /* Create the new type. We do this before consuming the opening brace
9845 so the enum will be recorded as being on the line of its tag (or the
9846 'enum' keyword, if there is no tag). */
ff4eb0b5 9847 type = start_enum (identifier);
a723baf1 9848
2cfe82fe
ZW
9849 /* Consume the opening brace. */
9850 cp_lexer_consume_token (parser->lexer);
9851
ff4eb0b5
ZW
9852 /* If the next token is not '}', then there are some enumerators. */
9853 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
a723baf1 9854 cp_parser_enumerator_list (parser, type);
ff4eb0b5
ZW
9855
9856 /* Consume the final '}'. */
a723baf1
MM
9857 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9858
f6af9a15 9859 /* Look for trailing attributes to apply to this enumeration, and
03fd3f84 9860 apply them if appropriate. */
f6af9a15
MA
9861 if (cp_parser_allow_gnu_extensions_p (parser))
9862 {
9863 tree trailing_attr = cp_parser_attributes_opt (parser);
9864 cplus_decl_attributes (&type,
9865 trailing_attr,
9866 (int) ATTR_FLAG_TYPE_IN_PLACE);
9867 }
9868
a723baf1
MM
9869 /* Finish up the enumeration. */
9870 finish_enum (type);
9871
9872 return type;
9873}
9874
9875/* Parse an enumerator-list. The enumerators all have the indicated
21526606 9876 TYPE.
a723baf1
MM
9877
9878 enumerator-list:
9879 enumerator-definition
9880 enumerator-list , enumerator-definition */
9881
9882static void
94edc4ab 9883cp_parser_enumerator_list (cp_parser* parser, tree type)
a723baf1
MM
9884{
9885 while (true)
9886 {
a723baf1
MM
9887 /* Parse an enumerator-definition. */
9888 cp_parser_enumerator_definition (parser, type);
ff4eb0b5
ZW
9889
9890 /* If the next token is not a ',', we've reached the end of
9891 the list. */
9892 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
a723baf1
MM
9893 break;
9894 /* Otherwise, consume the `,' and keep going. */
9895 cp_lexer_consume_token (parser->lexer);
9896 /* If the next token is a `}', there is a trailing comma. */
9897 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9898 {
9899 if (pedantic && !in_system_header)
9900 pedwarn ("comma at end of enumerator list");
9901 break;
9902 }
9903 }
9904}
9905
9906/* Parse an enumerator-definition. The enumerator has the indicated
9907 TYPE.
9908
9909 enumerator-definition:
9910 enumerator
9911 enumerator = constant-expression
21526606 9912
a723baf1
MM
9913 enumerator:
9914 identifier */
9915
9916static void
94edc4ab 9917cp_parser_enumerator_definition (cp_parser* parser, tree type)
a723baf1 9918{
a723baf1
MM
9919 tree identifier;
9920 tree value;
9921
9922 /* Look for the identifier. */
9923 identifier = cp_parser_identifier (parser);
9924 if (identifier == error_mark_node)
9925 return;
21526606 9926
ff4eb0b5
ZW
9927 /* If the next token is an '=', then there is an explicit value. */
9928 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
a723baf1
MM
9929 {
9930 /* Consume the `=' token. */
9931 cp_lexer_consume_token (parser->lexer);
9932 /* Parse the value. */
21526606 9933 value = cp_parser_constant_expression (parser,
d17811fd 9934 /*allow_non_constant_p=*/false,
14d22dd6 9935 NULL);
a723baf1
MM
9936 }
9937 else
9938 value = NULL_TREE;
9939
9940 /* Create the enumerator. */
9941 build_enumerator (identifier, value, type);
9942}
9943
9944/* Parse a namespace-name.
9945
9946 namespace-name:
9947 original-namespace-name
9948 namespace-alias
9949
9950 Returns the NAMESPACE_DECL for the namespace. */
9951
9952static tree
94edc4ab 9953cp_parser_namespace_name (cp_parser* parser)
a723baf1
MM
9954{
9955 tree identifier;
9956 tree namespace_decl;
9957
9958 /* Get the name of the namespace. */
9959 identifier = cp_parser_identifier (parser);
9960 if (identifier == error_mark_node)
9961 return error_mark_node;
9962
eea9800f
MM
9963 /* Look up the identifier in the currently active scope. Look only
9964 for namespaces, due to:
9965
9966 [basic.lookup.udir]
9967
9968 When looking up a namespace-name in a using-directive or alias
21526606 9969 definition, only namespace names are considered.
eea9800f
MM
9970
9971 And:
9972
9973 [basic.lookup.qual]
9974
9975 During the lookup of a name preceding the :: scope resolution
21526606 9976 operator, object, function, and enumerator names are ignored.
eea9800f
MM
9977
9978 (Note that cp_parser_class_or_namespace_name only calls this
9979 function if the token after the name is the scope resolution
9980 operator.) */
9981 namespace_decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 9982 none_type,
b0bc6e8e 9983 /*is_template=*/false,
eea9800f 9984 /*is_namespace=*/true,
8f78f01f
MM
9985 /*check_dependency=*/true,
9986 /*ambiguous_p=*/NULL);
a723baf1
MM
9987 /* If it's not a namespace, issue an error. */
9988 if (namespace_decl == error_mark_node
9989 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9990 {
9991 cp_parser_error (parser, "expected namespace-name");
9992 namespace_decl = error_mark_node;
9993 }
21526606 9994
a723baf1
MM
9995 return namespace_decl;
9996}
9997
9998/* Parse a namespace-definition.
9999
10000 namespace-definition:
10001 named-namespace-definition
21526606 10002 unnamed-namespace-definition
a723baf1
MM
10003
10004 named-namespace-definition:
10005 original-namespace-definition
10006 extension-namespace-definition
10007
10008 original-namespace-definition:
10009 namespace identifier { namespace-body }
21526606 10010
a723baf1
MM
10011 extension-namespace-definition:
10012 namespace original-namespace-name { namespace-body }
21526606 10013
a723baf1
MM
10014 unnamed-namespace-definition:
10015 namespace { namespace-body } */
10016
10017static void
94edc4ab 10018cp_parser_namespace_definition (cp_parser* parser)
a723baf1
MM
10019{
10020 tree identifier;
10021
10022 /* Look for the `namespace' keyword. */
10023 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10024
10025 /* Get the name of the namespace. We do not attempt to distinguish
10026 between an original-namespace-definition and an
10027 extension-namespace-definition at this point. The semantic
10028 analysis routines are responsible for that. */
10029 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10030 identifier = cp_parser_identifier (parser);
10031 else
10032 identifier = NULL_TREE;
10033
10034 /* Look for the `{' to start the namespace. */
10035 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10036 /* Start the namespace. */
10037 push_namespace (identifier);
10038 /* Parse the body of the namespace. */
10039 cp_parser_namespace_body (parser);
10040 /* Finish the namespace. */
10041 pop_namespace ();
10042 /* Look for the final `}'. */
10043 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10044}
10045
10046/* Parse a namespace-body.
10047
10048 namespace-body:
10049 declaration-seq [opt] */
10050
10051static void
94edc4ab 10052cp_parser_namespace_body (cp_parser* parser)
a723baf1
MM
10053{
10054 cp_parser_declaration_seq_opt (parser);
10055}
10056
10057/* Parse a namespace-alias-definition.
10058
10059 namespace-alias-definition:
10060 namespace identifier = qualified-namespace-specifier ; */
10061
10062static void
94edc4ab 10063cp_parser_namespace_alias_definition (cp_parser* parser)
a723baf1
MM
10064{
10065 tree identifier;
10066 tree namespace_specifier;
10067
10068 /* Look for the `namespace' keyword. */
10069 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10070 /* Look for the identifier. */
10071 identifier = cp_parser_identifier (parser);
10072 if (identifier == error_mark_node)
10073 return;
10074 /* Look for the `=' token. */
10075 cp_parser_require (parser, CPP_EQ, "`='");
10076 /* Look for the qualified-namespace-specifier. */
21526606 10077 namespace_specifier
a723baf1
MM
10078 = cp_parser_qualified_namespace_specifier (parser);
10079 /* Look for the `;' token. */
10080 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10081
10082 /* Register the alias in the symbol table. */
10083 do_namespace_alias (identifier, namespace_specifier);
10084}
10085
10086/* Parse a qualified-namespace-specifier.
10087
10088 qualified-namespace-specifier:
10089 :: [opt] nested-name-specifier [opt] namespace-name
10090
10091 Returns a NAMESPACE_DECL corresponding to the specified
10092 namespace. */
10093
10094static tree
94edc4ab 10095cp_parser_qualified_namespace_specifier (cp_parser* parser)
a723baf1
MM
10096{
10097 /* Look for the optional `::'. */
21526606 10098 cp_parser_global_scope_opt (parser,
a723baf1
MM
10099 /*current_scope_valid_p=*/false);
10100
10101 /* Look for the optional nested-name-specifier. */
10102 cp_parser_nested_name_specifier_opt (parser,
10103 /*typename_keyword_p=*/false,
10104 /*check_dependency_p=*/true,
a668c6ad
MM
10105 /*type_p=*/false,
10106 /*is_declaration=*/true);
a723baf1
MM
10107
10108 return cp_parser_namespace_name (parser);
10109}
10110
10111/* Parse a using-declaration.
10112
10113 using-declaration:
10114 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10115 using :: unqualified-id ; */
10116
10117static void
94edc4ab 10118cp_parser_using_declaration (cp_parser* parser)
a723baf1
MM
10119{
10120 cp_token *token;
10121 bool typename_p = false;
10122 bool global_scope_p;
10123 tree decl;
10124 tree identifier;
ed5f054f 10125 tree qscope;
a723baf1
MM
10126
10127 /* Look for the `using' keyword. */
10128 cp_parser_require_keyword (parser, RID_USING, "`using'");
21526606 10129
a723baf1
MM
10130 /* Peek at the next token. */
10131 token = cp_lexer_peek_token (parser->lexer);
10132 /* See if it's `typename'. */
10133 if (token->keyword == RID_TYPENAME)
10134 {
10135 /* Remember that we've seen it. */
10136 typename_p = true;
10137 /* Consume the `typename' token. */
10138 cp_lexer_consume_token (parser->lexer);
10139 }
10140
10141 /* Look for the optional global scope qualification. */
21526606 10142 global_scope_p
a723baf1 10143 = (cp_parser_global_scope_opt (parser,
21526606 10144 /*current_scope_valid_p=*/false)
a723baf1
MM
10145 != NULL_TREE);
10146
10147 /* If we saw `typename', or didn't see `::', then there must be a
10148 nested-name-specifier present. */
10149 if (typename_p || !global_scope_p)
21526606 10150 qscope = cp_parser_nested_name_specifier (parser, typename_p,
ed5f054f
AO
10151 /*check_dependency_p=*/true,
10152 /*type_p=*/false,
10153 /*is_declaration=*/true);
a723baf1
MM
10154 /* Otherwise, we could be in either of the two productions. In that
10155 case, treat the nested-name-specifier as optional. */
10156 else
ed5f054f
AO
10157 qscope = cp_parser_nested_name_specifier_opt (parser,
10158 /*typename_keyword_p=*/false,
10159 /*check_dependency_p=*/true,
10160 /*type_p=*/false,
10161 /*is_declaration=*/true);
10162 if (!qscope)
10163 qscope = global_namespace;
a723baf1
MM
10164
10165 /* Parse the unqualified-id. */
21526606 10166 identifier = cp_parser_unqualified_id (parser,
a723baf1 10167 /*template_keyword_p=*/false,
f3c2dfc6
MM
10168 /*check_dependency_p=*/true,
10169 /*declarator_p=*/true);
a723baf1
MM
10170
10171 /* The function we call to handle a using-declaration is different
10172 depending on what scope we are in. */
f3c2dfc6
MM
10173 if (identifier == error_mark_node)
10174 ;
10175 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10176 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10177 /* [namespace.udecl]
10178
10179 A using declaration shall not name a template-id. */
10180 error ("a template-id may not appear in a using-declaration");
a723baf1
MM
10181 else
10182 {
a5201a91 10183 if (at_class_scope_p ())
4eb6d609 10184 {
f3c2dfc6 10185 /* Create the USING_DECL. */
1d786913 10186 decl = do_class_using_decl (parser->scope, identifier);
f3c2dfc6
MM
10187 /* Add it to the list of members in this class. */
10188 finish_member_declaration (decl);
4eb6d609 10189 }
a723baf1 10190 else
f3c2dfc6
MM
10191 {
10192 decl = cp_parser_lookup_name_simple (parser, identifier);
10193 if (decl == error_mark_node)
4bb8ca28 10194 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
a5201a91 10195 else if (!at_namespace_scope_p ())
ed5f054f 10196 do_local_using_decl (decl, qscope, identifier);
f3c2dfc6 10197 else
ed5f054f 10198 do_toplevel_using_decl (decl, qscope, identifier);
f3c2dfc6 10199 }
a723baf1
MM
10200 }
10201
10202 /* Look for the final `;'. */
10203 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10204}
10205
21526606
EC
10206/* Parse a using-directive.
10207
a723baf1
MM
10208 using-directive:
10209 using namespace :: [opt] nested-name-specifier [opt]
10210 namespace-name ; */
10211
10212static void
94edc4ab 10213cp_parser_using_directive (cp_parser* parser)
a723baf1
MM
10214{
10215 tree namespace_decl;
86098eb8 10216 tree attribs;
a723baf1
MM
10217
10218 /* Look for the `using' keyword. */
10219 cp_parser_require_keyword (parser, RID_USING, "`using'");
10220 /* And the `namespace' keyword. */
10221 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10222 /* Look for the optional `::' operator. */
10223 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
34cd5ae7 10224 /* And the optional nested-name-specifier. */
a723baf1
MM
10225 cp_parser_nested_name_specifier_opt (parser,
10226 /*typename_keyword_p=*/false,
10227 /*check_dependency_p=*/true,
a668c6ad
MM
10228 /*type_p=*/false,
10229 /*is_declaration=*/true);
a723baf1
MM
10230 /* Get the namespace being used. */
10231 namespace_decl = cp_parser_namespace_name (parser);
86098eb8
JM
10232 /* And any specified attributes. */
10233 attribs = cp_parser_attributes_opt (parser);
a723baf1 10234 /* Update the symbol table. */
86098eb8 10235 parse_using_directive (namespace_decl, attribs);
a723baf1
MM
10236 /* Look for the final `;'. */
10237 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10238}
10239
10240/* Parse an asm-definition.
10241
10242 asm-definition:
21526606 10243 asm ( string-literal ) ;
a723baf1
MM
10244
10245 GNU Extension:
10246
10247 asm-definition:
10248 asm volatile [opt] ( string-literal ) ;
10249 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10250 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10251 : asm-operand-list [opt] ) ;
21526606
EC
10252 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10253 : asm-operand-list [opt]
a723baf1
MM
10254 : asm-operand-list [opt] ) ; */
10255
10256static void
94edc4ab 10257cp_parser_asm_definition (cp_parser* parser)
a723baf1 10258{
a723baf1
MM
10259 tree string;
10260 tree outputs = NULL_TREE;
10261 tree inputs = NULL_TREE;
10262 tree clobbers = NULL_TREE;
10263 tree asm_stmt;
10264 bool volatile_p = false;
10265 bool extended_p = false;
10266
10267 /* Look for the `asm' keyword. */
10268 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10269 /* See if the next token is `volatile'. */
10270 if (cp_parser_allow_gnu_extensions_p (parser)
10271 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10272 {
10273 /* Remember that we saw the `volatile' keyword. */
10274 volatile_p = true;
10275 /* Consume the token. */
10276 cp_lexer_consume_token (parser->lexer);
10277 }
10278 /* Look for the opening `('. */
c162c75e
MA
10279 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10280 return;
a723baf1 10281 /* Look for the string. */
c162c75e
MA
10282 string = cp_parser_string_literal (parser, false, false);
10283 if (string == error_mark_node)
10284 {
10285 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10286 /*consume_paren=*/true);
10287 return;
10288 }
10289
a723baf1 10290 /* If we're allowing GNU extensions, check for the extended assembly
21526606 10291 syntax. Unfortunately, the `:' tokens need not be separated by
a723baf1
MM
10292 a space in C, and so, for compatibility, we tolerate that here
10293 too. Doing that means that we have to treat the `::' operator as
10294 two `:' tokens. */
10295 if (cp_parser_allow_gnu_extensions_p (parser)
10296 && at_function_scope_p ()
10297 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10298 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10299 {
10300 bool inputs_p = false;
10301 bool clobbers_p = false;
10302
10303 /* The extended syntax was used. */
10304 extended_p = true;
10305
10306 /* Look for outputs. */
10307 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10308 {
10309 /* Consume the `:'. */
10310 cp_lexer_consume_token (parser->lexer);
10311 /* Parse the output-operands. */
21526606 10312 if (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
10313 CPP_COLON)
10314 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
10315 CPP_SCOPE)
10316 && cp_lexer_next_token_is_not (parser->lexer,
10317 CPP_CLOSE_PAREN))
a723baf1
MM
10318 outputs = cp_parser_asm_operand_list (parser);
10319 }
10320 /* If the next token is `::', there are no outputs, and the
10321 next token is the beginning of the inputs. */
10322 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
bf5930d4
JB
10323 /* The inputs are coming next. */
10324 inputs_p = true;
a723baf1
MM
10325
10326 /* Look for inputs. */
10327 if (inputs_p
10328 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10329 {
bf5930d4
JB
10330 /* Consume the `:' or `::'. */
10331 cp_lexer_consume_token (parser->lexer);
a723baf1 10332 /* Parse the output-operands. */
21526606 10333 if (cp_lexer_next_token_is_not (parser->lexer,
a723baf1 10334 CPP_COLON)
8caf4c38
MM
10335 && cp_lexer_next_token_is_not (parser->lexer,
10336 CPP_CLOSE_PAREN))
a723baf1
MM
10337 inputs = cp_parser_asm_operand_list (parser);
10338 }
10339 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10340 /* The clobbers are coming next. */
10341 clobbers_p = true;
10342
10343 /* Look for clobbers. */
21526606 10344 if (clobbers_p
a723baf1
MM
10345 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10346 {
bf5930d4
JB
10347 /* Consume the `:' or `::'. */
10348 cp_lexer_consume_token (parser->lexer);
a723baf1 10349 /* Parse the clobbers. */
8caf4c38
MM
10350 if (cp_lexer_next_token_is_not (parser->lexer,
10351 CPP_CLOSE_PAREN))
10352 clobbers = cp_parser_asm_clobber_list (parser);
a723baf1
MM
10353 }
10354 }
10355 /* Look for the closing `)'. */
10356 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
10357 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10358 /*consume_paren=*/true);
a723baf1
MM
10359 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10360
e130a54b 10361 /* Create the ASM_EXPR. */
a723baf1
MM
10362 if (at_function_scope_p ())
10363 {
6de9cd9a
DN
10364 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10365 inputs, clobbers);
e130a54b 10366 /* If the extended syntax was not used, mark the ASM_EXPR. */
a723baf1 10367 if (!extended_p)
ca059043
AP
10368 {
10369 tree temp = asm_stmt;
10370 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10371 temp = TREE_OPERAND (temp, 0);
10372
10373 ASM_INPUT_P (temp) = 1;
10374 }
a723baf1
MM
10375 }
10376 else
10377 assemble_asm (string);
10378}
10379
10380/* Declarators [gram.dcl.decl] */
10381
10382/* Parse an init-declarator.
10383
10384 init-declarator:
10385 declarator initializer [opt]
10386
10387 GNU Extension:
10388
10389 init-declarator:
10390 declarator asm-specification [opt] attributes [opt] initializer [opt]
10391
4bb8ca28
MM
10392 function-definition:
10393 decl-specifier-seq [opt] declarator ctor-initializer [opt]
21526606
EC
10394 function-body
10395 decl-specifier-seq [opt] declarator function-try-block
4bb8ca28
MM
10396
10397 GNU Extension:
10398
10399 function-definition:
21526606 10400 __extension__ function-definition
4bb8ca28 10401
a723baf1 10402 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
c8e4f0e9 10403 Returns a representation of the entity declared. If MEMBER_P is TRUE,
cf22909c
KL
10404 then this declarator appears in a class scope. The new DECL created
10405 by this declarator is returned.
a723baf1
MM
10406
10407 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10408 for a function-definition here as well. If the declarator is a
10409 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10410 be TRUE upon return. By that point, the function-definition will
10411 have been completely parsed.
10412
10413 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10414 is FALSE. */
10415
10416static tree
21526606 10417cp_parser_init_declarator (cp_parser* parser,
62d1db17 10418 cp_decl_specifier_seq *decl_specifiers,
94edc4ab
NN
10419 bool function_definition_allowed_p,
10420 bool member_p,
560ad596 10421 int declares_class_or_enum,
94edc4ab 10422 bool* function_definition_p)
a723baf1
MM
10423{
10424 cp_token *token;
058b15c1 10425 cp_declarator *declarator;
62d1db17 10426 tree prefix_attributes;
a723baf1
MM
10427 tree attributes;
10428 tree asm_specification;
10429 tree initializer;
10430 tree decl = NULL_TREE;
10431 tree scope;
a723baf1
MM
10432 bool is_initialized;
10433 bool is_parenthesized_init;
39703eb9 10434 bool is_non_constant_init;
7efa3e22 10435 int ctor_dtor_or_conv_p;
a723baf1 10436 bool friend_p;
4514aa8c 10437 tree pushed_scope = NULL;
a723baf1 10438
62d1db17
MM
10439 /* Gather the attributes that were provided with the
10440 decl-specifiers. */
10441 prefix_attributes = decl_specifiers->attributes;
62d1db17 10442
a723baf1
MM
10443 /* Assume that this is not the declarator for a function
10444 definition. */
10445 if (function_definition_p)
10446 *function_definition_p = false;
10447
10448 /* Defer access checks while parsing the declarator; we cannot know
21526606 10449 what names are accessible until we know what is being
a723baf1 10450 declared. */
cf22909c
KL
10451 resume_deferring_access_checks ();
10452
a723baf1 10453 /* Parse the declarator. */
21526606 10454 declarator
62b8a44e 10455 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 10456 &ctor_dtor_or_conv_p,
db86dd14
MM
10457 /*parenthesized_p=*/NULL,
10458 /*member_p=*/false);
a723baf1 10459 /* Gather up the deferred checks. */
cf22909c 10460 stop_deferring_access_checks ();
24c0ef37 10461
a723baf1
MM
10462 /* If the DECLARATOR was erroneous, there's no need to go
10463 further. */
058b15c1 10464 if (declarator == cp_error_declarator)
cf22909c 10465 return error_mark_node;
a723baf1 10466
fc6a28d7
MM
10467 if (declares_class_or_enum & 2)
10468 cp_parser_check_for_definition_in_return_type (declarator,
10469 decl_specifiers->type);
560ad596 10470
a723baf1
MM
10471 /* Figure out what scope the entity declared by the DECLARATOR is
10472 located in. `grokdeclarator' sometimes changes the scope, so
10473 we compute it now. */
10474 scope = get_scope_of_declarator (declarator);
10475
10476 /* If we're allowing GNU extensions, look for an asm-specification
10477 and attributes. */
10478 if (cp_parser_allow_gnu_extensions_p (parser))
10479 {
10480 /* Look for an asm-specification. */
10481 asm_specification = cp_parser_asm_specification_opt (parser);
10482 /* And attributes. */
10483 attributes = cp_parser_attributes_opt (parser);
10484 }
10485 else
10486 {
10487 asm_specification = NULL_TREE;
10488 attributes = NULL_TREE;
10489 }
10490
10491 /* Peek at the next token. */
10492 token = cp_lexer_peek_token (parser->lexer);
10493 /* Check to see if the token indicates the start of a
10494 function-definition. */
10495 if (cp_parser_token_starts_function_definition_p (token))
10496 {
10497 if (!function_definition_allowed_p)
10498 {
10499 /* If a function-definition should not appear here, issue an
10500 error message. */
10501 cp_parser_error (parser,
10502 "a function-definition is not allowed here");
10503 return error_mark_node;
10504 }
10505 else
10506 {
a723baf1
MM
10507 /* Neither attributes nor an asm-specification are allowed
10508 on a function-definition. */
10509 if (asm_specification)
10510 error ("an asm-specification is not allowed on a function-definition");
10511 if (attributes)
10512 error ("attributes are not allowed on a function-definition");
10513 /* This is a function-definition. */
10514 *function_definition_p = true;
10515
a723baf1 10516 /* Parse the function definition. */
4bb8ca28
MM
10517 if (member_p)
10518 decl = cp_parser_save_member_function_body (parser,
10519 decl_specifiers,
10520 declarator,
10521 prefix_attributes);
10522 else
21526606 10523 decl
4bb8ca28
MM
10524 = (cp_parser_function_definition_from_specifiers_and_declarator
10525 (parser, decl_specifiers, prefix_attributes, declarator));
24c0ef37 10526
a723baf1
MM
10527 return decl;
10528 }
10529 }
10530
10531 /* [dcl.dcl]
10532
10533 Only in function declarations for constructors, destructors, and
21526606 10534 type conversions can the decl-specifier-seq be omitted.
a723baf1
MM
10535
10536 We explicitly postpone this check past the point where we handle
10537 function-definitions because we tolerate function-definitions
10538 that are missing their return types in some modes. */
62d1db17 10539 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
a723baf1 10540 {
21526606 10541 cp_parser_error (parser,
a723baf1
MM
10542 "expected constructor, destructor, or type conversion");
10543 return error_mark_node;
10544 }
10545
10546 /* An `=' or an `(' indicates an initializer. */
21526606 10547 is_initialized = (token->type == CPP_EQ
a723baf1
MM
10548 || token->type == CPP_OPEN_PAREN);
10549 /* If the init-declarator isn't initialized and isn't followed by a
10550 `,' or `;', it's not a valid init-declarator. */
21526606 10551 if (!is_initialized
a723baf1
MM
10552 && token->type != CPP_COMMA
10553 && token->type != CPP_SEMICOLON)
10554 {
996c2b52 10555 cp_parser_error (parser, "expected initializer");
a723baf1
MM
10556 return error_mark_node;
10557 }
10558
10559 /* Because start_decl has side-effects, we should only call it if we
10560 know we're going ahead. By this point, we know that we cannot
10561 possibly be looking at any other construct. */
10562 cp_parser_commit_to_tentative_parse (parser);
10563
e90c7b84
ILT
10564 /* If the decl specifiers were bad, issue an error now that we're
10565 sure this was intended to be a declarator. Then continue
10566 declaring the variable(s), as int, to try to cut down on further
10567 errors. */
62d1db17
MM
10568 if (decl_specifiers->any_specifiers_p
10569 && decl_specifiers->type == error_mark_node)
e90c7b84
ILT
10570 {
10571 cp_parser_error (parser, "invalid type in declaration");
62d1db17 10572 decl_specifiers->type = integer_type_node;
e90c7b84
ILT
10573 }
10574
a723baf1
MM
10575 /* Check to see whether or not this declaration is a friend. */
10576 friend_p = cp_parser_friend_p (decl_specifiers);
10577
10578 /* Check that the number of template-parameter-lists is OK. */
ee3071ef 10579 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
cf22909c 10580 return error_mark_node;
a723baf1
MM
10581
10582 /* Enter the newly declared entry in the symbol table. If we're
10583 processing a declaration in a class-specifier, we wait until
10584 after processing the initializer. */
10585 if (!member_p)
10586 {
10587 if (parser->in_unbraced_linkage_specification_p)
10588 {
62d1db17 10589 decl_specifiers->storage_class = sc_extern;
a723baf1
MM
10590 have_extern_spec = false;
10591 }
ee3071ef 10592 decl = start_decl (declarator, decl_specifiers,
73a8adb6 10593 is_initialized, attributes, prefix_attributes,
4514aa8c 10594 &pushed_scope);
a723baf1 10595 }
73a8adb6
MM
10596 else if (scope)
10597 /* Enter the SCOPE. That way unqualified names appearing in the
10598 initializer will be looked up in SCOPE. */
4514aa8c 10599 pushed_scope = push_scope (scope);
a723baf1
MM
10600
10601 /* Perform deferred access control checks, now that we know in which
10602 SCOPE the declared entity resides. */
21526606 10603 if (!member_p && decl)
a723baf1
MM
10604 {
10605 tree saved_current_function_decl = NULL_TREE;
10606
10607 /* If the entity being declared is a function, pretend that we
10608 are in its scope. If it is a `friend', it may have access to
9bcb9aae 10609 things that would not otherwise be accessible. */
a723baf1
MM
10610 if (TREE_CODE (decl) == FUNCTION_DECL)
10611 {
10612 saved_current_function_decl = current_function_decl;
10613 current_function_decl = decl;
10614 }
21526606 10615
cf22909c
KL
10616 /* Perform the access control checks for the declarator and the
10617 the decl-specifiers. */
10618 perform_deferred_access_checks ();
a723baf1
MM
10619
10620 /* Restore the saved value. */
10621 if (TREE_CODE (decl) == FUNCTION_DECL)
10622 current_function_decl = saved_current_function_decl;
10623 }
10624
10625 /* Parse the initializer. */
10626 if (is_initialized)
21526606 10627 initializer = cp_parser_initializer (parser,
39703eb9
MM
10628 &is_parenthesized_init,
10629 &is_non_constant_init);
a723baf1
MM
10630 else
10631 {
10632 initializer = NULL_TREE;
10633 is_parenthesized_init = false;
39703eb9 10634 is_non_constant_init = true;
a723baf1
MM
10635 }
10636
10637 /* The old parser allows attributes to appear after a parenthesized
10638 initializer. Mark Mitchell proposed removing this functionality
10639 on the GCC mailing lists on 2002-08-13. This parser accepts the
10640 attributes -- but ignores them. */
10641 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10642 if (cp_parser_attributes_opt (parser))
10643 warning ("attributes after parenthesized initializer ignored");
10644
a723baf1
MM
10645 /* For an in-class declaration, use `grokfield' to create the
10646 declaration. */
10647 if (member_p)
8db1028e 10648 {
4514aa8c 10649 if (pushed_scope)
62a4d942 10650 {
4514aa8c
NS
10651 pop_scope (pushed_scope);
10652 pushed_scope = false;
62a4d942 10653 }
8db1028e
NS
10654 decl = grokfield (declarator, decl_specifiers,
10655 initializer, /*asmspec=*/NULL_TREE,
a723baf1 10656 /*attributes=*/NULL_TREE);
8db1028e
NS
10657 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10658 cp_parser_save_default_args (parser, decl);
10659 }
21526606 10660
a723baf1
MM
10661 /* Finish processing the declaration. But, skip friend
10662 declarations. */
550205c3 10663 if (!friend_p && decl && decl != error_mark_node)
73a8adb6
MM
10664 {
10665 cp_finish_decl (decl,
10666 initializer,
10667 asm_specification,
10668 /* If the initializer is in parentheses, then this is
10669 a direct-initialization, which means that an
10670 `explicit' constructor is OK. Otherwise, an
10671 `explicit' constructor cannot be used. */
10672 ((is_parenthesized_init || !is_initialized)
a723baf1 10673 ? 0 : LOOKUP_ONLYCONVERTING));
73a8adb6 10674 }
4514aa8c
NS
10675 if (!friend_p && pushed_scope)
10676 pop_scope (pushed_scope);
a723baf1 10677
39703eb9
MM
10678 /* Remember whether or not variables were initialized by
10679 constant-expressions. */
21526606 10680 if (decl && TREE_CODE (decl) == VAR_DECL
39703eb9
MM
10681 && is_initialized && !is_non_constant_init)
10682 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10683
a723baf1
MM
10684 return decl;
10685}
10686
10687/* Parse a declarator.
21526606 10688
a723baf1
MM
10689 declarator:
10690 direct-declarator
21526606 10691 ptr-operator declarator
a723baf1
MM
10692
10693 abstract-declarator:
10694 ptr-operator abstract-declarator [opt]
10695 direct-abstract-declarator
10696
10697 GNU Extensions:
10698
10699 declarator:
10700 attributes [opt] direct-declarator
21526606 10701 attributes [opt] ptr-operator declarator
a723baf1
MM
10702
10703 abstract-declarator:
10704 attributes [opt] ptr-operator abstract-declarator [opt]
10705 attributes [opt] direct-abstract-declarator
21526606 10706
7efa3e22
NS
10707 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10708 detect constructor, destructor or conversion operators. It is set
10709 to -1 if the declarator is a name, and +1 if it is a
10710 function. Otherwise it is set to zero. Usually you just want to
10711 test for >0, but internally the negative value is used.
21526606 10712
a723baf1
MM
10713 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10714 a decl-specifier-seq unless it declares a constructor, destructor,
10715 or conversion. It might seem that we could check this condition in
10716 semantic analysis, rather than parsing, but that makes it difficult
10717 to handle something like `f()'. We want to notice that there are
10718 no decl-specifiers, and therefore realize that this is an
21526606
EC
10719 expression, not a declaration.)
10720
4bb8ca28 10721 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
db86dd14
MM
10722 the declarator is a direct-declarator of the form "(...)".
10723
10724 MEMBER_P is true iff this declarator is a member-declarator. */
a723baf1 10725
058b15c1 10726static cp_declarator *
21526606
EC
10727cp_parser_declarator (cp_parser* parser,
10728 cp_parser_declarator_kind dcl_kind,
4bb8ca28 10729 int* ctor_dtor_or_conv_p,
db86dd14
MM
10730 bool* parenthesized_p,
10731 bool member_p)
a723baf1
MM
10732{
10733 cp_token *token;
058b15c1 10734 cp_declarator *declarator;
a723baf1 10735 enum tree_code code;
3c01e5df 10736 cp_cv_quals cv_quals;
a723baf1
MM
10737 tree class_type;
10738 tree attributes = NULL_TREE;
10739
10740 /* Assume this is not a constructor, destructor, or type-conversion
10741 operator. */
10742 if (ctor_dtor_or_conv_p)
7efa3e22 10743 *ctor_dtor_or_conv_p = 0;
a723baf1
MM
10744
10745 if (cp_parser_allow_gnu_extensions_p (parser))
10746 attributes = cp_parser_attributes_opt (parser);
21526606 10747
a723baf1
MM
10748 /* Peek at the next token. */
10749 token = cp_lexer_peek_token (parser->lexer);
21526606 10750
a723baf1
MM
10751 /* Check for the ptr-operator production. */
10752 cp_parser_parse_tentatively (parser);
10753 /* Parse the ptr-operator. */
21526606
EC
10754 code = cp_parser_ptr_operator (parser,
10755 &class_type,
3c01e5df 10756 &cv_quals);
a723baf1
MM
10757 /* If that worked, then we have a ptr-operator. */
10758 if (cp_parser_parse_definitely (parser))
10759 {
4bb8ca28
MM
10760 /* If a ptr-operator was found, then this declarator was not
10761 parenthesized. */
10762 if (parenthesized_p)
10763 *parenthesized_p = true;
a723baf1
MM
10764 /* The dependent declarator is optional if we are parsing an
10765 abstract-declarator. */
62b8a44e 10766 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1
MM
10767 cp_parser_parse_tentatively (parser);
10768
10769 /* Parse the dependent declarator. */
62b8a44e 10770 declarator = cp_parser_declarator (parser, dcl_kind,
4bb8ca28 10771 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
10772 /*parenthesized_p=*/NULL,
10773 /*member_p=*/false);
a723baf1
MM
10774
10775 /* If we are parsing an abstract-declarator, we must handle the
10776 case where the dependent declarator is absent. */
62b8a44e
NS
10777 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10778 && !cp_parser_parse_definitely (parser))
058b15c1 10779 declarator = NULL;
21526606 10780
a723baf1 10781 /* Build the representation of the ptr-operator. */
058b15c1 10782 if (class_type)
3c01e5df 10783 declarator = make_ptrmem_declarator (cv_quals,
058b15c1
MM
10784 class_type,
10785 declarator);
10786 else if (code == INDIRECT_REF)
3c01e5df 10787 declarator = make_pointer_declarator (cv_quals, declarator);
a723baf1 10788 else
3c01e5df 10789 declarator = make_reference_declarator (cv_quals, declarator);
a723baf1
MM
10790 }
10791 /* Everything else is a direct-declarator. */
10792 else
4bb8ca28
MM
10793 {
10794 if (parenthesized_p)
10795 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10796 CPP_OPEN_PAREN);
10797 declarator = cp_parser_direct_declarator (parser, dcl_kind,
db86dd14
MM
10798 ctor_dtor_or_conv_p,
10799 member_p);
4bb8ca28 10800 }
a723baf1 10801
058b15c1
MM
10802 if (attributes && declarator != cp_error_declarator)
10803 declarator->attributes = attributes;
21526606 10804
a723baf1
MM
10805 return declarator;
10806}
10807
10808/* Parse a direct-declarator or direct-abstract-declarator.
10809
10810 direct-declarator:
10811 declarator-id
10812 direct-declarator ( parameter-declaration-clause )
21526606 10813 cv-qualifier-seq [opt]
a723baf1
MM
10814 exception-specification [opt]
10815 direct-declarator [ constant-expression [opt] ]
21526606 10816 ( declarator )
a723baf1
MM
10817
10818 direct-abstract-declarator:
10819 direct-abstract-declarator [opt]
21526606 10820 ( parameter-declaration-clause )
a723baf1
MM
10821 cv-qualifier-seq [opt]
10822 exception-specification [opt]
10823 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10824 ( abstract-declarator )
10825
62b8a44e
NS
10826 Returns a representation of the declarator. DCL_KIND is
10827 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10828 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10829 we are parsing a direct-declarator. It is
10830 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10831 of ambiguity we prefer an abstract declarator, as per
db86dd14 10832 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
058b15c1 10833 cp_parser_declarator. */
a723baf1 10834
058b15c1 10835static cp_declarator *
94edc4ab
NN
10836cp_parser_direct_declarator (cp_parser* parser,
10837 cp_parser_declarator_kind dcl_kind,
db86dd14
MM
10838 int* ctor_dtor_or_conv_p,
10839 bool member_p)
a723baf1
MM
10840{
10841 cp_token *token;
058b15c1 10842 cp_declarator *declarator = NULL;
a723baf1
MM
10843 tree scope = NULL_TREE;
10844 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10845 bool saved_in_declarator_p = parser->in_declarator_p;
62b8a44e 10846 bool first = true;
4514aa8c 10847 tree pushed_scope = NULL_TREE;
21526606 10848
62b8a44e 10849 while (true)
a723baf1 10850 {
62b8a44e
NS
10851 /* Peek at the next token. */
10852 token = cp_lexer_peek_token (parser->lexer);
10853 if (token->type == CPP_OPEN_PAREN)
a723baf1 10854 {
62b8a44e
NS
10855 /* This is either a parameter-declaration-clause, or a
10856 parenthesized declarator. When we know we are parsing a
34cd5ae7 10857 named declarator, it must be a parenthesized declarator
62b8a44e
NS
10858 if FIRST is true. For instance, `(int)' is a
10859 parameter-declaration-clause, with an omitted
10860 direct-abstract-declarator. But `((*))', is a
10861 parenthesized abstract declarator. Finally, when T is a
10862 template parameter `(T)' is a
34cd5ae7 10863 parameter-declaration-clause, and not a parenthesized
62b8a44e 10864 named declarator.
21526606 10865
62b8a44e
NS
10866 We first try and parse a parameter-declaration-clause,
10867 and then try a nested declarator (if FIRST is true).
a723baf1 10868
62b8a44e
NS
10869 It is not an error for it not to be a
10870 parameter-declaration-clause, even when FIRST is
10871 false. Consider,
10872
10873 int i (int);
10874 int i (3);
10875
10876 The first is the declaration of a function while the
10877 second is a the definition of a variable, including its
10878 initializer.
10879
10880 Having seen only the parenthesis, we cannot know which of
10881 these two alternatives should be selected. Even more
10882 complex are examples like:
10883
10884 int i (int (a));
10885 int i (int (3));
10886
10887 The former is a function-declaration; the latter is a
21526606 10888 variable initialization.
62b8a44e 10889
34cd5ae7 10890 Thus again, we try a parameter-declaration-clause, and if
62b8a44e
NS
10891 that fails, we back out and return. */
10892
10893 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1 10894 {
058b15c1 10895 cp_parameter_declarator *params;
4047b164 10896 unsigned saved_num_template_parameter_lists;
21526606 10897
db86dd14
MM
10898 /* In a member-declarator, the only valid interpretation
10899 of a parenthesis is the start of a
10900 parameter-declaration-clause. (It is invalid to
10901 initialize a static data member with a parenthesized
10902 initializer; only the "=" form of initialization is
10903 permitted.) */
10904 if (!member_p)
10905 cp_parser_parse_tentatively (parser);
a723baf1 10906
62b8a44e
NS
10907 /* Consume the `('. */
10908 cp_lexer_consume_token (parser->lexer);
10909 if (first)
10910 {
10911 /* If this is going to be an abstract declarator, we're
10912 in a declarator and we can't have default args. */
10913 parser->default_arg_ok_p = false;
10914 parser->in_declarator_p = true;
10915 }
21526606 10916
4047b164
KL
10917 /* Inside the function parameter list, surrounding
10918 template-parameter-lists do not apply. */
10919 saved_num_template_parameter_lists
10920 = parser->num_template_parameter_lists;
10921 parser->num_template_parameter_lists = 0;
10922
62b8a44e
NS
10923 /* Parse the parameter-declaration-clause. */
10924 params = cp_parser_parameter_declaration_clause (parser);
10925
4047b164
KL
10926 parser->num_template_parameter_lists
10927 = saved_num_template_parameter_lists;
10928
62b8a44e 10929 /* If all went well, parse the cv-qualifier-seq and the
34cd5ae7 10930 exception-specification. */
db86dd14 10931 if (member_p || cp_parser_parse_definitely (parser))
62b8a44e 10932 {
3c01e5df 10933 cp_cv_quals cv_quals;
62b8a44e 10934 tree exception_specification;
7efa3e22
NS
10935
10936 if (ctor_dtor_or_conv_p)
10937 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
62b8a44e
NS
10938 first = false;
10939 /* Consume the `)'. */
10940 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10941
10942 /* Parse the cv-qualifier-seq. */
3c01e5df 10943 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
62b8a44e 10944 /* And the exception-specification. */
21526606 10945 exception_specification
62b8a44e
NS
10946 = cp_parser_exception_specification_opt (parser);
10947
10948 /* Create the function-declarator. */
10949 declarator = make_call_declarator (declarator,
10950 params,
3c01e5df 10951 cv_quals,
62b8a44e
NS
10952 exception_specification);
10953 /* Any subsequent parameter lists are to do with
10954 return type, so are not those of the declared
10955 function. */
10956 parser->default_arg_ok_p = false;
21526606 10957
62b8a44e
NS
10958 /* Repeat the main loop. */
10959 continue;
10960 }
10961 }
21526606 10962
62b8a44e
NS
10963 /* If this is the first, we can try a parenthesized
10964 declarator. */
10965 if (first)
a723baf1 10966 {
a7324e75
MM
10967 bool saved_in_type_id_in_expr_p;
10968
a723baf1 10969 parser->default_arg_ok_p = saved_default_arg_ok_p;
62b8a44e 10970 parser->in_declarator_p = saved_in_declarator_p;
21526606 10971
62b8a44e
NS
10972 /* Consume the `('. */
10973 cp_lexer_consume_token (parser->lexer);
10974 /* Parse the nested declarator. */
a7324e75
MM
10975 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10976 parser->in_type_id_in_expr_p = true;
21526606 10977 declarator
4bb8ca28 10978 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
db86dd14
MM
10979 /*parenthesized_p=*/NULL,
10980 member_p);
a7324e75 10981 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
62b8a44e
NS
10982 first = false;
10983 /* Expect a `)'. */
10984 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
058b15c1
MM
10985 declarator = cp_error_declarator;
10986 if (declarator == cp_error_declarator)
62b8a44e 10987 break;
21526606 10988
62b8a44e 10989 goto handle_declarator;
a723baf1 10990 }
9bcb9aae 10991 /* Otherwise, we must be done. */
62b8a44e
NS
10992 else
10993 break;
a723baf1 10994 }
62b8a44e
NS
10995 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10996 && token->type == CPP_OPEN_SQUARE)
a723baf1 10997 {
62b8a44e 10998 /* Parse an array-declarator. */
a723baf1
MM
10999 tree bounds;
11000
7efa3e22
NS
11001 if (ctor_dtor_or_conv_p)
11002 *ctor_dtor_or_conv_p = 0;
21526606 11003
62b8a44e
NS
11004 first = false;
11005 parser->default_arg_ok_p = false;
11006 parser->in_declarator_p = true;
a723baf1
MM
11007 /* Consume the `['. */
11008 cp_lexer_consume_token (parser->lexer);
11009 /* Peek at the next token. */
11010 token = cp_lexer_peek_token (parser->lexer);
11011 /* If the next token is `]', then there is no
11012 constant-expression. */
11013 if (token->type != CPP_CLOSE_SQUARE)
14d22dd6
MM
11014 {
11015 bool non_constant_p;
11016
21526606 11017 bounds
14d22dd6
MM
11018 = cp_parser_constant_expression (parser,
11019 /*allow_non_constant=*/true,
11020 &non_constant_p);
d17811fd 11021 if (!non_constant_p)
9baa27a9 11022 bounds = fold_non_dependent_expr (bounds);
44370687
MM
11023 else if (!at_function_scope_p ())
11024 {
11025 error ("array bound is not an integer constant");
11026 bounds = error_mark_node;
11027 }
14d22dd6 11028 }
a723baf1
MM
11029 else
11030 bounds = NULL_TREE;
11031 /* Look for the closing `]'. */
62b8a44e
NS
11032 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11033 {
058b15c1 11034 declarator = cp_error_declarator;
62b8a44e
NS
11035 break;
11036 }
a723baf1 11037
058b15c1 11038 declarator = make_array_declarator (declarator, bounds);
a723baf1 11039 }
62b8a44e 11040 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
a723baf1 11041 {
1d786913
MM
11042 tree qualifying_scope;
11043 tree unqualified_name;
058b15c1 11044
a668c6ad 11045 /* Parse a declarator-id */
62b8a44e
NS
11046 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11047 cp_parser_parse_tentatively (parser);
1d786913
MM
11048 unqualified_name = cp_parser_declarator_id (parser);
11049 qualifying_scope = parser->scope;
712becab
NS
11050 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11051 {
11052 if (!cp_parser_parse_definitely (parser))
1d786913
MM
11053 unqualified_name = error_mark_node;
11054 else if (qualifying_scope
11055 || (TREE_CODE (unqualified_name)
11056 != IDENTIFIER_NODE))
712becab
NS
11057 {
11058 cp_parser_error (parser, "expected unqualified-id");
1d786913 11059 unqualified_name = error_mark_node;
712becab
NS
11060 }
11061 }
21526606 11062
1d786913 11063 if (unqualified_name == error_mark_node)
058b15c1
MM
11064 {
11065 declarator = cp_error_declarator;
11066 break;
11067 }
21526606 11068
1d786913
MM
11069 if (qualifying_scope && at_namespace_scope_p ()
11070 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
62b8a44e 11071 {
62b8a44e
NS
11072 /* In the declaration of a member of a template class
11073 outside of the class itself, the SCOPE will sometimes
11074 be a TYPENAME_TYPE. For example, given:
21526606 11075
62b8a44e
NS
11076 template <typename T>
11077 int S<T>::R::i = 3;
21526606 11078
62b8a44e
NS
11079 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11080 this context, we must resolve S<T>::R to an ordinary
11081 type, rather than a typename type.
21526606 11082
62b8a44e
NS
11083 The reason we normally avoid resolving TYPENAME_TYPEs
11084 is that a specialization of `S' might render
11085 `S<T>::R' not a type. However, if `S' is
11086 specialized, then this `i' will not be used, so there
11087 is no harm in resolving the types here. */
1d786913
MM
11088 tree type;
11089
11090 /* Resolve the TYPENAME_TYPE. */
11091 type = resolve_typename_type (qualifying_scope,
11092 /*only_current_p=*/false);
11093 /* If that failed, the declarator is invalid. */
11094 if (type == error_mark_node)
11095 error ("%<%T::%D%> is not a type",
11096 TYPE_CONTEXT (qualifying_scope),
11097 TYPE_IDENTIFIER (qualifying_scope));
11098 qualifying_scope = type;
62b8a44e 11099 }
21526606 11100
1d786913
MM
11101 declarator = make_id_declarator (qualifying_scope,
11102 unqualified_name);
11103 if (unqualified_name)
a723baf1 11104 {
62b8a44e
NS
11105 tree class_type;
11106
1d786913
MM
11107 if (qualifying_scope
11108 && CLASS_TYPE_P (qualifying_scope))
11109 class_type = qualifying_scope;
62b8a44e 11110 else
1d786913 11111 class_type = current_class_type;
62b8a44e 11112
058b15c1
MM
11113 if (class_type)
11114 {
11115 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11116 declarator->u.id.sfk = sfk_destructor;
11117 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11118 declarator->u.id.sfk = sfk_conversion;
27d6592c
MM
11119 else if (/* There's no way to declare a constructor
11120 for an anonymous type, even if the type
11121 got a name for linkage purposes. */
11122 !TYPE_WAS_ANONYMOUS (class_type)
11123 && (constructor_name_p (unqualified_name,
11124 class_type)
11125 || (TREE_CODE (unqualified_name) == TYPE_DECL
11126 && (same_type_p
11127 (TREE_TYPE (unqualified_name),
11128 class_type)))))
058b15c1
MM
11129 declarator->u.id.sfk = sfk_constructor;
11130
11131 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11132 *ctor_dtor_or_conv_p = -1;
1d786913 11133 if (qualifying_scope
98ca843c 11134 && TREE_CODE (unqualified_name) == TYPE_DECL
058b15c1
MM
11135 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11136 {
11137 error ("invalid use of constructor as a template");
2a13a625
GDR
11138 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11139 "the constructor in a qualified name",
11140 class_type,
058b15c1
MM
11141 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11142 class_type, class_type);
11143 }
11144 }
a723baf1 11145 }
62b8a44e
NS
11146
11147 handle_declarator:;
11148 scope = get_scope_of_declarator (declarator);
11149 if (scope)
91b004e5
MM
11150 /* Any names that appear after the declarator-id for a
11151 member are looked up in the containing scope. */
4514aa8c 11152 pushed_scope = push_scope (scope);
62b8a44e
NS
11153 parser->in_declarator_p = true;
11154 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
058b15c1 11155 || (declarator && declarator->kind == cdk_id))
62b8a44e
NS
11156 /* Default args are only allowed on function
11157 declarations. */
11158 parser->default_arg_ok_p = saved_default_arg_ok_p;
a723baf1 11159 else
62b8a44e
NS
11160 parser->default_arg_ok_p = false;
11161
11162 first = false;
a723baf1 11163 }
62b8a44e 11164 /* We're done. */
a723baf1
MM
11165 else
11166 break;
a723baf1
MM
11167 }
11168
11169 /* For an abstract declarator, we might wind up with nothing at this
11170 point. That's an error; the declarator is not optional. */
11171 if (!declarator)
11172 cp_parser_error (parser, "expected declarator");
11173
11174 /* If we entered a scope, we must exit it now. */
4514aa8c
NS
11175 if (pushed_scope)
11176 pop_scope (pushed_scope);
a723baf1
MM
11177
11178 parser->default_arg_ok_p = saved_default_arg_ok_p;
11179 parser->in_declarator_p = saved_in_declarator_p;
21526606 11180
a723baf1
MM
11181 return declarator;
11182}
11183
21526606 11184/* Parse a ptr-operator.
a723baf1
MM
11185
11186 ptr-operator:
11187 * cv-qualifier-seq [opt]
11188 &
11189 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11190
11191 GNU Extension:
11192
11193 ptr-operator:
11194 & cv-qualifier-seq [opt]
11195
3c01e5df
MM
11196 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11197 Returns ADDR_EXPR if a reference was used. In the case of a
11198 pointer-to-member, *TYPE is filled in with the TYPE containing the
11199 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11200 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11201 ERROR_MARK if an error occurred. */
21526606 11202
a723baf1 11203static enum tree_code
21526606
EC
11204cp_parser_ptr_operator (cp_parser* parser,
11205 tree* type,
3c01e5df 11206 cp_cv_quals *cv_quals)
a723baf1
MM
11207{
11208 enum tree_code code = ERROR_MARK;
11209 cp_token *token;
11210
11211 /* Assume that it's not a pointer-to-member. */
11212 *type = NULL_TREE;
11213 /* And that there are no cv-qualifiers. */
3c01e5df 11214 *cv_quals = TYPE_UNQUALIFIED;
a723baf1
MM
11215
11216 /* Peek at the next token. */
11217 token = cp_lexer_peek_token (parser->lexer);
11218 /* If it's a `*' or `&' we have a pointer or reference. */
11219 if (token->type == CPP_MULT || token->type == CPP_AND)
11220 {
11221 /* Remember which ptr-operator we were processing. */
11222 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11223
11224 /* Consume the `*' or `&'. */
11225 cp_lexer_consume_token (parser->lexer);
11226
11227 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11228 `&', if we are allowing GNU extensions. (The only qualifier
11229 that can legally appear after `&' is `restrict', but that is
11230 enforced during semantic analysis. */
21526606 11231 if (code == INDIRECT_REF
a723baf1 11232 || cp_parser_allow_gnu_extensions_p (parser))
3c01e5df 11233 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
a723baf1
MM
11234 }
11235 else
11236 {
11237 /* Try the pointer-to-member case. */
11238 cp_parser_parse_tentatively (parser);
11239 /* Look for the optional `::' operator. */
11240 cp_parser_global_scope_opt (parser,
11241 /*current_scope_valid_p=*/false);
11242 /* Look for the nested-name specifier. */
11243 cp_parser_nested_name_specifier (parser,
11244 /*typename_keyword_p=*/false,
11245 /*check_dependency_p=*/true,
a668c6ad
MM
11246 /*type_p=*/false,
11247 /*is_declaration=*/false);
a723baf1
MM
11248 /* If we found it, and the next token is a `*', then we are
11249 indeed looking at a pointer-to-member operator. */
11250 if (!cp_parser_error_occurred (parser)
11251 && cp_parser_require (parser, CPP_MULT, "`*'"))
11252 {
11253 /* The type of which the member is a member is given by the
11254 current SCOPE. */
11255 *type = parser->scope;
11256 /* The next name will not be qualified. */
11257 parser->scope = NULL_TREE;
11258 parser->qualifying_scope = NULL_TREE;
11259 parser->object_scope = NULL_TREE;
11260 /* Indicate that the `*' operator was used. */
11261 code = INDIRECT_REF;
11262 /* Look for the optional cv-qualifier-seq. */
3c01e5df 11263 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
a723baf1
MM
11264 }
11265 /* If that didn't work we don't have a ptr-operator. */
11266 if (!cp_parser_parse_definitely (parser))
11267 cp_parser_error (parser, "expected ptr-operator");
11268 }
11269
11270 return code;
11271}
11272
11273/* Parse an (optional) cv-qualifier-seq.
11274
11275 cv-qualifier-seq:
21526606 11276 cv-qualifier cv-qualifier-seq [opt]
a723baf1 11277
a723baf1
MM
11278 cv-qualifier:
11279 const
21526606 11280 volatile
a723baf1
MM
11281
11282 GNU Extension:
11283
11284 cv-qualifier:
98ca843c 11285 __restrict__
a723baf1 11286
3c01e5df
MM
11287 Returns a bitmask representing the cv-qualifiers. */
11288
11289static cp_cv_quals
11290cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
a723baf1 11291{
3c01e5df 11292 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
a723baf1 11293
3c01e5df 11294 while (true)
a723baf1 11295 {
3c01e5df
MM
11296 cp_token *token;
11297 cp_cv_quals cv_qualifier;
98ca843c 11298
3c01e5df
MM
11299 /* Peek at the next token. */
11300 token = cp_lexer_peek_token (parser->lexer);
11301 /* See if it's a cv-qualifier. */
11302 switch (token->keyword)
11303 {
11304 case RID_CONST:
11305 cv_qualifier = TYPE_QUAL_CONST;
11306 break;
98ca843c 11307
3c01e5df
MM
11308 case RID_VOLATILE:
11309 cv_qualifier = TYPE_QUAL_VOLATILE;
11310 break;
98ca843c 11311
3c01e5df
MM
11312 case RID_RESTRICT:
11313 cv_qualifier = TYPE_QUAL_RESTRICT;
11314 break;
98ca843c 11315
3c01e5df
MM
11316 default:
11317 cv_qualifier = TYPE_UNQUALIFIED;
11318 break;
11319 }
98ca843c 11320
3c01e5df
MM
11321 if (!cv_qualifier)
11322 break;
a723baf1 11323
3c01e5df
MM
11324 if (cv_quals & cv_qualifier)
11325 {
11326 error ("duplicate cv-qualifier");
11327 cp_lexer_purge_token (parser->lexer);
11328 }
11329 else
11330 {
11331 cp_lexer_consume_token (parser->lexer);
11332 cv_quals |= cv_qualifier;
11333 }
a723baf1
MM
11334 }
11335
3c01e5df 11336 return cv_quals;
a723baf1
MM
11337}
11338
11339/* Parse a declarator-id.
11340
11341 declarator-id:
11342 id-expression
21526606 11343 :: [opt] nested-name-specifier [opt] type-name
a723baf1
MM
11344
11345 In the `id-expression' case, the value returned is as for
11346 cp_parser_id_expression if the id-expression was an unqualified-id.
11347 If the id-expression was a qualified-id, then a SCOPE_REF is
11348 returned. The first operand is the scope (either a NAMESPACE_DECL
11349 or TREE_TYPE), but the second is still just a representation of an
11350 unqualified-id. */
11351
11352static tree
94edc4ab 11353cp_parser_declarator_id (cp_parser* parser)
a723baf1 11354{
a723baf1
MM
11355 /* The expression must be an id-expression. Assume that qualified
11356 names are the names of types so that:
11357
11358 template <class T>
11359 int S<T>::R::i = 3;
11360
11361 will work; we must treat `S<T>::R' as the name of a type.
11362 Similarly, assume that qualified names are templates, where
11363 required, so that:
11364
11365 template <class T>
11366 int S<T>::R<T>::i = 3;
11367
11368 will work, too. */
1d786913
MM
11369 return cp_parser_id_expression (parser,
11370 /*template_keyword_p=*/false,
11371 /*check_dependency_p=*/false,
11372 /*template_p=*/NULL,
11373 /*declarator_p=*/true);
a723baf1
MM
11374}
11375
11376/* Parse a type-id.
11377
11378 type-id:
11379 type-specifier-seq abstract-declarator [opt]
11380
11381 Returns the TYPE specified. */
11382
11383static tree
94edc4ab 11384cp_parser_type_id (cp_parser* parser)
a723baf1 11385{
62d1db17 11386 cp_decl_specifier_seq type_specifier_seq;
058b15c1 11387 cp_declarator *abstract_declarator;
a723baf1
MM
11388
11389 /* Parse the type-specifier-seq. */
62d1db17
MM
11390 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11391 if (type_specifier_seq.type == error_mark_node)
a723baf1
MM
11392 return error_mark_node;
11393
11394 /* There might or might not be an abstract declarator. */
11395 cp_parser_parse_tentatively (parser);
11396 /* Look for the declarator. */
21526606 11397 abstract_declarator
4bb8ca28 11398 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
db86dd14
MM
11399 /*parenthesized_p=*/NULL,
11400 /*member_p=*/false);
a723baf1
MM
11401 /* Check to see if there really was a declarator. */
11402 if (!cp_parser_parse_definitely (parser))
058b15c1 11403 abstract_declarator = NULL;
a723baf1 11404
62d1db17 11405 return groktypename (&type_specifier_seq, abstract_declarator);
a723baf1
MM
11406}
11407
11408/* Parse a type-specifier-seq.
11409
11410 type-specifier-seq:
11411 type-specifier type-specifier-seq [opt]
11412
11413 GNU extension:
11414
11415 type-specifier-seq:
11416 attributes type-specifier-seq [opt]
11417
62d1db17 11418 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
a723baf1 11419
62d1db17
MM
11420static void
11421cp_parser_type_specifier_seq (cp_parser* parser,
11422 cp_decl_specifier_seq *type_specifier_seq)
a723baf1
MM
11423{
11424 bool seen_type_specifier = false;
62d1db17
MM
11425
11426 /* Clear the TYPE_SPECIFIER_SEQ. */
11427 clear_decl_specs (type_specifier_seq);
a723baf1
MM
11428
11429 /* Parse the type-specifiers and attributes. */
11430 while (true)
11431 {
11432 tree type_specifier;
11433
11434 /* Check for attributes first. */
11435 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11436 {
98ca843c 11437 type_specifier_seq->attributes =
62d1db17
MM
11438 chainon (type_specifier_seq->attributes,
11439 cp_parser_attributes_opt (parser));
a723baf1
MM
11440 continue;
11441 }
11442
a723baf1 11443 /* Look for the type-specifier. */
21526606 11444 type_specifier = cp_parser_type_specifier (parser,
62d1db17
MM
11445 CP_PARSER_FLAGS_OPTIONAL,
11446 type_specifier_seq,
a723baf1
MM
11447 /*is_declaration=*/false,
11448 NULL,
11449 NULL);
11450 /* If the first type-specifier could not be found, this is not a
11451 type-specifier-seq at all. */
62d1db17
MM
11452 if (!seen_type_specifier && !type_specifier)
11453 {
11454 cp_parser_error (parser, "expected type-specifier");
11455 type_specifier_seq->type = error_mark_node;
11456 return;
11457 }
a723baf1
MM
11458 /* If subsequent type-specifiers could not be found, the
11459 type-specifier-seq is complete. */
62d1db17 11460 else if (seen_type_specifier && !type_specifier)
a723baf1
MM
11461 break;
11462
a723baf1
MM
11463 seen_type_specifier = true;
11464 }
11465
62d1db17 11466 return;
a723baf1
MM
11467}
11468
11469/* Parse a parameter-declaration-clause.
11470
11471 parameter-declaration-clause:
11472 parameter-declaration-list [opt] ... [opt]
11473 parameter-declaration-list , ...
11474
058b15c1
MM
11475 Returns a representation for the parameter declarations. A return
11476 value of NULL indicates a parameter-declaration-clause consisting
11477 only of an ellipsis. */
a723baf1 11478
058b15c1 11479static cp_parameter_declarator *
94edc4ab 11480cp_parser_parameter_declaration_clause (cp_parser* parser)
a723baf1 11481{
058b15c1 11482 cp_parameter_declarator *parameters;
a723baf1
MM
11483 cp_token *token;
11484 bool ellipsis_p;
058b15c1 11485 bool is_error;
a723baf1
MM
11486
11487 /* Peek at the next token. */
11488 token = cp_lexer_peek_token (parser->lexer);
11489 /* Check for trivial parameter-declaration-clauses. */
11490 if (token->type == CPP_ELLIPSIS)
11491 {
11492 /* Consume the `...' token. */
11493 cp_lexer_consume_token (parser->lexer);
058b15c1 11494 return NULL;
a723baf1
MM
11495 }
11496 else if (token->type == CPP_CLOSE_PAREN)
11497 /* There are no parameters. */
c73aecdf
DE
11498 {
11499#ifndef NO_IMPLICIT_EXTERN_C
11500 if (in_system_header && current_class_type == NULL
11501 && current_lang_name == lang_name_c)
058b15c1 11502 return NULL;
c73aecdf
DE
11503 else
11504#endif
058b15c1 11505 return no_parameters;
c73aecdf 11506 }
a723baf1
MM
11507 /* Check for `(void)', too, which is a special case. */
11508 else if (token->keyword == RID_VOID
21526606 11509 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1
MM
11510 == CPP_CLOSE_PAREN))
11511 {
11512 /* Consume the `void' token. */
11513 cp_lexer_consume_token (parser->lexer);
11514 /* There are no parameters. */
058b15c1 11515 return no_parameters;
a723baf1 11516 }
21526606 11517
a723baf1 11518 /* Parse the parameter-declaration-list. */
058b15c1 11519 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
a723baf1
MM
11520 /* If a parse error occurred while parsing the
11521 parameter-declaration-list, then the entire
11522 parameter-declaration-clause is erroneous. */
058b15c1
MM
11523 if (is_error)
11524 return NULL;
a723baf1
MM
11525
11526 /* Peek at the next token. */
11527 token = cp_lexer_peek_token (parser->lexer);
11528 /* If it's a `,', the clause should terminate with an ellipsis. */
11529 if (token->type == CPP_COMMA)
11530 {
11531 /* Consume the `,'. */
11532 cp_lexer_consume_token (parser->lexer);
11533 /* Expect an ellipsis. */
21526606 11534 ellipsis_p
a723baf1
MM
11535 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11536 }
21526606 11537 /* It might also be `...' if the optional trailing `,' was
a723baf1
MM
11538 omitted. */
11539 else if (token->type == CPP_ELLIPSIS)
11540 {
11541 /* Consume the `...' token. */
11542 cp_lexer_consume_token (parser->lexer);
11543 /* And remember that we saw it. */
11544 ellipsis_p = true;
11545 }
11546 else
11547 ellipsis_p = false;
11548
11549 /* Finish the parameter list. */
058b15c1
MM
11550 if (parameters && ellipsis_p)
11551 parameters->ellipsis_p = true;
98ca843c 11552
058b15c1 11553 return parameters;
a723baf1
MM
11554}
11555
11556/* Parse a parameter-declaration-list.
11557
11558 parameter-declaration-list:
11559 parameter-declaration
11560 parameter-declaration-list , parameter-declaration
11561
11562 Returns a representation of the parameter-declaration-list, as for
11563 cp_parser_parameter_declaration_clause. However, the
058b15c1
MM
11564 `void_list_node' is never appended to the list. Upon return,
11565 *IS_ERROR will be true iff an error occurred. */
a723baf1 11566
058b15c1
MM
11567static cp_parameter_declarator *
11568cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
a723baf1 11569{
058b15c1
MM
11570 cp_parameter_declarator *parameters = NULL;
11571 cp_parameter_declarator **tail = &parameters;
11572
11573 /* Assume all will go well. */
11574 *is_error = false;
a723baf1
MM
11575
11576 /* Look for more parameters. */
11577 while (true)
11578 {
058b15c1 11579 cp_parameter_declarator *parameter;
4bb8ca28 11580 bool parenthesized_p;
a723baf1 11581 /* Parse the parameter. */
21526606
EC
11582 parameter
11583 = cp_parser_parameter_declaration (parser,
4bb8ca28
MM
11584 /*template_parm_p=*/false,
11585 &parenthesized_p);
ec194454 11586
34cd5ae7 11587 /* If a parse error occurred parsing the parameter declaration,
a723baf1 11588 then the entire parameter-declaration-list is erroneous. */
058b15c1 11589 if (!parameter)
a723baf1 11590 {
058b15c1
MM
11591 *is_error = true;
11592 parameters = NULL;
a723baf1
MM
11593 break;
11594 }
11595 /* Add the new parameter to the list. */
058b15c1
MM
11596 *tail = parameter;
11597 tail = &parameter->next;
a723baf1
MM
11598
11599 /* Peek at the next token. */
11600 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11601 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11602 /* The parameter-declaration-list is complete. */
11603 break;
11604 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11605 {
11606 cp_token *token;
11607
11608 /* Peek at the next token. */
11609 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11610 /* If it's an ellipsis, then the list is complete. */
11611 if (token->type == CPP_ELLIPSIS)
11612 break;
11613 /* Otherwise, there must be more parameters. Consume the
11614 `,'. */
11615 cp_lexer_consume_token (parser->lexer);
4bb8ca28
MM
11616 /* When parsing something like:
11617
11618 int i(float f, double d)
21526606 11619
4bb8ca28
MM
11620 we can tell after seeing the declaration for "f" that we
11621 are not looking at an initialization of a variable "i",
21526606 11622 but rather at the declaration of a function "i".
4bb8ca28
MM
11623
11624 Due to the fact that the parsing of template arguments
11625 (as specified to a template-id) requires backtracking we
11626 cannot use this technique when inside a template argument
11627 list. */
11628 if (!parser->in_template_argument_list_p
4d5fe289 11629 && !parser->in_type_id_in_expr_p
0b16f8f4 11630 && cp_parser_uncommitted_to_tentative_parse_p (parser)
4bb8ca28
MM
11631 /* However, a parameter-declaration of the form
11632 "foat(f)" (which is a valid declaration of a
11633 parameter "f") can also be interpreted as an
11634 expression (the conversion of "f" to "float"). */
11635 && !parenthesized_p)
11636 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
11637 }
11638 else
11639 {
2a13a625 11640 cp_parser_error (parser, "expected %<,%> or %<...%>");
0b16f8f4 11641 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21526606 11642 cp_parser_skip_to_closing_parenthesis (parser,
4bb8ca28 11643 /*recovering=*/true,
5c832178 11644 /*or_comma=*/false,
4bb8ca28 11645 /*consume_paren=*/false);
a723baf1
MM
11646 break;
11647 }
11648 }
11649
058b15c1 11650 return parameters;
a723baf1
MM
11651}
11652
11653/* Parse a parameter declaration.
11654
11655 parameter-declaration:
11656 decl-specifier-seq declarator
11657 decl-specifier-seq declarator = assignment-expression
11658 decl-specifier-seq abstract-declarator [opt]
11659 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11660
ec194454
MM
11661 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11662 declares a template parameter. (In that case, a non-nested `>'
11663 token encountered during the parsing of the assignment-expression
11664 is not interpreted as a greater-than operator.)
a723baf1 11665
058b15c1
MM
11666 Returns a representation of the parameter, or NULL if an error
11667 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11668 true iff the declarator is of the form "(p)". */
a723baf1 11669
058b15c1 11670static cp_parameter_declarator *
21526606 11671cp_parser_parameter_declaration (cp_parser *parser,
4bb8ca28
MM
11672 bool template_parm_p,
11673 bool *parenthesized_p)
a723baf1 11674{
560ad596 11675 int declares_class_or_enum;
ec194454 11676 bool greater_than_is_operator_p;
62d1db17 11677 cp_decl_specifier_seq decl_specifiers;
058b15c1 11678 cp_declarator *declarator;
a723baf1 11679 tree default_argument;
a723baf1
MM
11680 cp_token *token;
11681 const char *saved_message;
11682
ec194454
MM
11683 /* In a template parameter, `>' is not an operator.
11684
11685 [temp.param]
11686
11687 When parsing a default template-argument for a non-type
11688 template-parameter, the first non-nested `>' is taken as the end
11689 of the template parameter-list rather than a greater-than
11690 operator. */
11691 greater_than_is_operator_p = !template_parm_p;
11692
a723baf1
MM
11693 /* Type definitions may not appear in parameter types. */
11694 saved_message = parser->type_definition_forbidden_message;
21526606 11695 parser->type_definition_forbidden_message
a723baf1
MM
11696 = "types may not be defined in parameter types";
11697
11698 /* Parse the declaration-specifiers. */
62d1db17
MM
11699 cp_parser_decl_specifier_seq (parser,
11700 CP_PARSER_FLAGS_NONE,
11701 &decl_specifiers,
11702 &declares_class_or_enum);
a723baf1
MM
11703 /* If an error occurred, there's no reason to attempt to parse the
11704 rest of the declaration. */
11705 if (cp_parser_error_occurred (parser))
11706 {
11707 parser->type_definition_forbidden_message = saved_message;
058b15c1 11708 return NULL;
a723baf1
MM
11709 }
11710
11711 /* Peek at the next token. */
11712 token = cp_lexer_peek_token (parser->lexer);
11713 /* If the next token is a `)', `,', `=', `>', or `...', then there
11714 is no declarator. */
21526606 11715 if (token->type == CPP_CLOSE_PAREN
a723baf1
MM
11716 || token->type == CPP_COMMA
11717 || token->type == CPP_EQ
11718 || token->type == CPP_ELLIPSIS
11719 || token->type == CPP_GREATER)
4bb8ca28 11720 {
058b15c1 11721 declarator = NULL;
4bb8ca28
MM
11722 if (parenthesized_p)
11723 *parenthesized_p = false;
11724 }
a723baf1
MM
11725 /* Otherwise, there should be a declarator. */
11726 else
11727 {
11728 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11729 parser->default_arg_ok_p = false;
21526606 11730
5c832178
MM
11731 /* After seeing a decl-specifier-seq, if the next token is not a
11732 "(", there is no possibility that the code is a valid
4f8163b1
MM
11733 expression. Therefore, if parsing tentatively, we commit at
11734 this point. */
5c832178 11735 if (!parser->in_template_argument_list_p
643aee72 11736 /* In an expression context, having seen:
4f8163b1 11737
a7324e75 11738 (int((char ...
4f8163b1
MM
11739
11740 we cannot be sure whether we are looking at a
a7324e75
MM
11741 function-type (taking a "char" as a parameter) or a cast
11742 of some object of type "char" to "int". */
4f8163b1 11743 && !parser->in_type_id_in_expr_p
0b16f8f4 11744 && cp_parser_uncommitted_to_tentative_parse_p (parser)
5c832178
MM
11745 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11746 cp_parser_commit_to_tentative_parse (parser);
11747 /* Parse the declarator. */
a723baf1 11748 declarator = cp_parser_declarator (parser,
62b8a44e 11749 CP_PARSER_DECLARATOR_EITHER,
4bb8ca28 11750 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
11751 parenthesized_p,
11752 /*member_p=*/false);
a723baf1 11753 parser->default_arg_ok_p = saved_default_arg_ok_p;
4971227d 11754 /* After the declarator, allow more attributes. */
62d1db17 11755 decl_specifiers.attributes
98ca843c 11756 = chainon (decl_specifiers.attributes,
62d1db17 11757 cp_parser_attributes_opt (parser));
a723baf1
MM
11758 }
11759
62b8a44e 11760 /* The restriction on defining new types applies only to the type
a723baf1
MM
11761 of the parameter, not to the default argument. */
11762 parser->type_definition_forbidden_message = saved_message;
11763
11764 /* If the next token is `=', then process a default argument. */
11765 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11766 {
11767 bool saved_greater_than_is_operator_p;
11768 /* Consume the `='. */
11769 cp_lexer_consume_token (parser->lexer);
11770
11771 /* If we are defining a class, then the tokens that make up the
11772 default argument must be saved and processed later. */
21526606 11773 if (!template_parm_p && at_class_scope_p ()
ec194454 11774 && TYPE_BEING_DEFINED (current_class_type))
a723baf1
MM
11775 {
11776 unsigned depth = 0;
c162c75e
MA
11777 cp_token *first_token;
11778 cp_token *token;
a723baf1
MM
11779
11780 /* Add tokens until we have processed the entire default
03fd3f84 11781 argument. We add the range [first_token, token). */
c162c75e 11782 first_token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
11783 while (true)
11784 {
11785 bool done = false;
a723baf1
MM
11786
11787 /* Peek at the next token. */
11788 token = cp_lexer_peek_token (parser->lexer);
11789 /* What we do depends on what token we have. */
11790 switch (token->type)
11791 {
11792 /* In valid code, a default argument must be
11793 immediately followed by a `,' `)', or `...'. */
11794 case CPP_COMMA:
11795 case CPP_CLOSE_PAREN:
11796 case CPP_ELLIPSIS:
11797 /* If we run into a non-nested `;', `}', or `]',
11798 then the code is invalid -- but the default
11799 argument is certainly over. */
11800 case CPP_SEMICOLON:
11801 case CPP_CLOSE_BRACE:
11802 case CPP_CLOSE_SQUARE:
11803 if (depth == 0)
11804 done = true;
11805 /* Update DEPTH, if necessary. */
11806 else if (token->type == CPP_CLOSE_PAREN
11807 || token->type == CPP_CLOSE_BRACE
11808 || token->type == CPP_CLOSE_SQUARE)
11809 --depth;
11810 break;
11811
11812 case CPP_OPEN_PAREN:
11813 case CPP_OPEN_SQUARE:
11814 case CPP_OPEN_BRACE:
11815 ++depth;
11816 break;
11817
11818 case CPP_GREATER:
11819 /* If we see a non-nested `>', and `>' is not an
11820 operator, then it marks the end of the default
11821 argument. */
11822 if (!depth && !greater_than_is_operator_p)
11823 done = true;
11824 break;
11825
11826 /* If we run out of tokens, issue an error message. */
11827 case CPP_EOF:
11828 error ("file ends in default argument");
11829 done = true;
11830 break;
11831
11832 case CPP_NAME:
11833 case CPP_SCOPE:
11834 /* In these cases, we should look for template-ids.
21526606 11835 For example, if the default argument is
a723baf1
MM
11836 `X<int, double>()', we need to do name lookup to
11837 figure out whether or not `X' is a template; if
34cd5ae7 11838 so, the `,' does not end the default argument.
a723baf1
MM
11839
11840 That is not yet done. */
11841 break;
11842
11843 default:
11844 break;
11845 }
11846
11847 /* If we've reached the end, stop. */
11848 if (done)
11849 break;
21526606 11850
a723baf1
MM
11851 /* Add the token to the token block. */
11852 token = cp_lexer_consume_token (parser->lexer);
a723baf1 11853 }
c162c75e
MA
11854
11855 /* Create a DEFAULT_ARG to represented the unparsed default
11856 argument. */
11857 default_argument = make_node (DEFAULT_ARG);
11858 DEFARG_TOKENS (default_argument)
11859 = cp_token_cache_new (first_token, token);
a723baf1
MM
11860 }
11861 /* Outside of a class definition, we can just parse the
11862 assignment-expression. */
11863 else
11864 {
11865 bool saved_local_variables_forbidden_p;
11866
11867 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11868 set correctly. */
21526606 11869 saved_greater_than_is_operator_p
a723baf1
MM
11870 = parser->greater_than_is_operator_p;
11871 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11872 /* Local variable names (and the `this' keyword) may not
11873 appear in a default argument. */
21526606 11874 saved_local_variables_forbidden_p
a723baf1
MM
11875 = parser->local_variables_forbidden_p;
11876 parser->local_variables_forbidden_p = true;
11877 /* Parse the assignment-expression. */
11878 default_argument = cp_parser_assignment_expression (parser);
11879 /* Restore saved state. */
21526606 11880 parser->greater_than_is_operator_p
a723baf1 11881 = saved_greater_than_is_operator_p;
21526606
EC
11882 parser->local_variables_forbidden_p
11883 = saved_local_variables_forbidden_p;
a723baf1
MM
11884 }
11885 if (!parser->default_arg_ok_p)
11886 {
c67d36d0
NS
11887 if (!flag_pedantic_errors)
11888 warning ("deprecated use of default argument for parameter of non-function");
11889 else
11890 {
11891 error ("default arguments are only permitted for function parameters");
11892 default_argument = NULL_TREE;
11893 }
a723baf1
MM
11894 }
11895 }
11896 else
11897 default_argument = NULL_TREE;
21526606 11898
62d1db17 11899 return make_parameter_declarator (&decl_specifiers,
058b15c1
MM
11900 declarator,
11901 default_argument);
a723baf1
MM
11902}
11903
a723baf1
MM
11904/* Parse a function-body.
11905
11906 function-body:
11907 compound_statement */
11908
11909static void
11910cp_parser_function_body (cp_parser *parser)
11911{
325c3691 11912 cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
11913}
11914
11915/* Parse a ctor-initializer-opt followed by a function-body. Return
11916 true if a ctor-initializer was present. */
11917
11918static bool
11919cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11920{
11921 tree body;
11922 bool ctor_initializer_p;
11923
11924 /* Begin the function body. */
11925 body = begin_function_body ();
11926 /* Parse the optional ctor-initializer. */
11927 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11928 /* Parse the function-body. */
11929 cp_parser_function_body (parser);
11930 /* Finish the function body. */
11931 finish_function_body (body);
11932
11933 return ctor_initializer_p;
11934}
11935
11936/* Parse an initializer.
11937
11938 initializer:
11939 = initializer-clause
21526606 11940 ( expression-list )
a723baf1
MM
11941
11942 Returns a expression representing the initializer. If no
21526606 11943 initializer is present, NULL_TREE is returned.
a723baf1
MM
11944
11945 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11946 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
39703eb9
MM
11947 set to FALSE if there is no initializer present. If there is an
11948 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11949 is set to true; otherwise it is set to false. */
a723baf1
MM
11950
11951static tree
39703eb9
MM
11952cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11953 bool* non_constant_p)
a723baf1
MM
11954{
11955 cp_token *token;
11956 tree init;
11957
11958 /* Peek at the next token. */
11959 token = cp_lexer_peek_token (parser->lexer);
11960
11961 /* Let our caller know whether or not this initializer was
11962 parenthesized. */
11963 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
39703eb9
MM
11964 /* Assume that the initializer is constant. */
11965 *non_constant_p = false;
a723baf1
MM
11966
11967 if (token->type == CPP_EQ)
11968 {
11969 /* Consume the `='. */
11970 cp_lexer_consume_token (parser->lexer);
11971 /* Parse the initializer-clause. */
39703eb9 11972 init = cp_parser_initializer_clause (parser, non_constant_p);
a723baf1
MM
11973 }
11974 else if (token->type == CPP_OPEN_PAREN)
39703eb9
MM
11975 init = cp_parser_parenthesized_expression_list (parser, false,
11976 non_constant_p);
a723baf1
MM
11977 else
11978 {
11979 /* Anything else is an error. */
11980 cp_parser_error (parser, "expected initializer");
11981 init = error_mark_node;
11982 }
11983
11984 return init;
11985}
11986
21526606 11987/* Parse an initializer-clause.
a723baf1
MM
11988
11989 initializer-clause:
11990 assignment-expression
11991 { initializer-list , [opt] }
11992 { }
11993
21526606 11994 Returns an expression representing the initializer.
a723baf1
MM
11995
11996 If the `assignment-expression' production is used the value
21526606 11997 returned is simply a representation for the expression.
a723baf1
MM
11998
11999 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12000 the elements of the initializer-list (or NULL_TREE, if the last
12001 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12002 NULL_TREE. There is no way to detect whether or not the optional
39703eb9
MM
12003 trailing `,' was provided. NON_CONSTANT_P is as for
12004 cp_parser_initializer. */
a723baf1
MM
12005
12006static tree
39703eb9 12007cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
12008{
12009 tree initializer;
12010
b2802a4b
R
12011 /* Assume the expression is constant. */
12012 *non_constant_p = false;
12013
a723baf1
MM
12014 /* If it is not a `{', then we are looking at an
12015 assignment-expression. */
12016 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
0da99d4e 12017 {
98ca843c 12018 initializer
0da99d4e
GB
12019 = cp_parser_constant_expression (parser,
12020 /*allow_non_constant_p=*/true,
12021 non_constant_p);
12022 if (!*non_constant_p)
12023 initializer = fold_non_dependent_expr (initializer);
12024 }
a723baf1
MM
12025 else
12026 {
12027 /* Consume the `{' token. */
12028 cp_lexer_consume_token (parser->lexer);
12029 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12030 initializer = make_node (CONSTRUCTOR);
a723baf1
MM
12031 /* If it's not a `}', then there is a non-trivial initializer. */
12032 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12033 {
12034 /* Parse the initializer list. */
12035 CONSTRUCTOR_ELTS (initializer)
39703eb9 12036 = cp_parser_initializer_list (parser, non_constant_p);
a723baf1
MM
12037 /* A trailing `,' token is allowed. */
12038 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12039 cp_lexer_consume_token (parser->lexer);
12040 }
a723baf1
MM
12041 /* Now, there should be a trailing `}'. */
12042 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12043 }
12044
12045 return initializer;
12046}
12047
12048/* Parse an initializer-list.
12049
12050 initializer-list:
12051 initializer-clause
12052 initializer-list , initializer-clause
12053
12054 GNU Extension:
21526606 12055
a723baf1
MM
12056 initializer-list:
12057 identifier : initializer-clause
12058 initializer-list, identifier : initializer-clause
12059
12060 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12061 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
39703eb9
MM
12062 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12063 as for cp_parser_initializer. */
a723baf1
MM
12064
12065static tree
39703eb9 12066cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
12067{
12068 tree initializers = NULL_TREE;
12069
39703eb9
MM
12070 /* Assume all of the expressions are constant. */
12071 *non_constant_p = false;
12072
a723baf1
MM
12073 /* Parse the rest of the list. */
12074 while (true)
12075 {
12076 cp_token *token;
12077 tree identifier;
12078 tree initializer;
39703eb9
MM
12079 bool clause_non_constant_p;
12080
a723baf1
MM
12081 /* If the next token is an identifier and the following one is a
12082 colon, we are looking at the GNU designated-initializer
12083 syntax. */
12084 if (cp_parser_allow_gnu_extensions_p (parser)
12085 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12086 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12087 {
12088 /* Consume the identifier. */
12089 identifier = cp_lexer_consume_token (parser->lexer)->value;
12090 /* Consume the `:'. */
12091 cp_lexer_consume_token (parser->lexer);
12092 }
12093 else
12094 identifier = NULL_TREE;
12095
12096 /* Parse the initializer. */
21526606 12097 initializer = cp_parser_initializer_clause (parser,
39703eb9
MM
12098 &clause_non_constant_p);
12099 /* If any clause is non-constant, so is the entire initializer. */
12100 if (clause_non_constant_p)
12101 *non_constant_p = true;
a723baf1
MM
12102 /* Add it to the list. */
12103 initializers = tree_cons (identifier, initializer, initializers);
12104
12105 /* If the next token is not a comma, we have reached the end of
12106 the list. */
12107 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12108 break;
12109
12110 /* Peek at the next token. */
12111 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12112 /* If the next token is a `}', then we're still done. An
12113 initializer-clause can have a trailing `,' after the
12114 initializer-list and before the closing `}'. */
12115 if (token->type == CPP_CLOSE_BRACE)
12116 break;
12117
12118 /* Consume the `,' token. */
12119 cp_lexer_consume_token (parser->lexer);
12120 }
12121
12122 /* The initializers were built up in reverse order, so we need to
12123 reverse them now. */
12124 return nreverse (initializers);
12125}
12126
12127/* Classes [gram.class] */
12128
12129/* Parse a class-name.
12130
12131 class-name:
12132 identifier
12133 template-id
12134
12135 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12136 to indicate that names looked up in dependent types should be
12137 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12138 keyword has been used to indicate that the name that appears next
fc6a28d7
MM
12139 is a template. TAG_TYPE indicates the explicit tag given before
12140 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12141 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12142 is the class being defined in a class-head.
a723baf1
MM
12143
12144 Returns the TYPE_DECL representing the class. */
12145
12146static tree
21526606
EC
12147cp_parser_class_name (cp_parser *parser,
12148 bool typename_keyword_p,
12149 bool template_keyword_p,
fc6a28d7 12150 enum tag_types tag_type,
a723baf1 12151 bool check_dependency_p,
a668c6ad
MM
12152 bool class_head_p,
12153 bool is_declaration)
a723baf1
MM
12154{
12155 tree decl;
12156 tree scope;
12157 bool typename_p;
e5976695
MM
12158 cp_token *token;
12159
12160 /* All class-names start with an identifier. */
12161 token = cp_lexer_peek_token (parser->lexer);
12162 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12163 {
12164 cp_parser_error (parser, "expected class-name");
12165 return error_mark_node;
12166 }
21526606 12167
a723baf1
MM
12168 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12169 to a template-id, so we save it here. */
12170 scope = parser->scope;
3adee96c
KL
12171 if (scope == error_mark_node)
12172 return error_mark_node;
21526606 12173
a723baf1
MM
12174 /* Any name names a type if we're following the `typename' keyword
12175 in a qualified name where the enclosing scope is type-dependent. */
12176 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
1fb3244a 12177 && dependent_type_p (scope));
e5976695
MM
12178 /* Handle the common case (an identifier, but not a template-id)
12179 efficiently. */
21526606 12180 if (token->type == CPP_NAME
f4abade9 12181 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
a723baf1 12182 {
a723baf1
MM
12183 tree identifier;
12184
12185 /* Look for the identifier. */
12186 identifier = cp_parser_identifier (parser);
12187 /* If the next token isn't an identifier, we are certainly not
12188 looking at a class-name. */
12189 if (identifier == error_mark_node)
12190 decl = error_mark_node;
12191 /* If we know this is a type-name, there's no need to look it
12192 up. */
12193 else if (typename_p)
12194 decl = identifier;
12195 else
12196 {
12197 /* If the next token is a `::', then the name must be a type
12198 name.
12199
12200 [basic.lookup.qual]
12201
12202 During the lookup for a name preceding the :: scope
12203 resolution operator, object, function, and enumerator
12204 names are ignored. */
12205 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
fc6a28d7 12206 tag_type = typename_type;
a723baf1 12207 /* Look up the name. */
21526606 12208 decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 12209 tag_type,
b0bc6e8e 12210 /*is_template=*/false,
eea9800f 12211 /*is_namespace=*/false,
8f78f01f
MM
12212 check_dependency_p,
12213 /*ambiguous_p=*/NULL);
a723baf1
MM
12214 }
12215 }
e5976695
MM
12216 else
12217 {
12218 /* Try a template-id. */
12219 decl = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
12220 check_dependency_p,
12221 is_declaration);
e5976695
MM
12222 if (decl == error_mark_node)
12223 return error_mark_node;
12224 }
a723baf1
MM
12225
12226 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12227
12228 /* If this is a typename, create a TYPENAME_TYPE. */
12229 if (typename_p && decl != error_mark_node)
4bfb8bba 12230 {
fc6a28d7 12231 decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
4bfb8bba
MM
12232 if (decl != error_mark_node)
12233 decl = TYPE_NAME (decl);
12234 }
a723baf1
MM
12235
12236 /* Check to see that it is really the name of a class. */
21526606 12237 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
a723baf1
MM
12238 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12239 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12240 /* Situations like this:
12241
12242 template <typename T> struct A {
21526606 12243 typename T::template X<int>::I i;
a723baf1
MM
12244 };
12245
12246 are problematic. Is `T::template X<int>' a class-name? The
12247 standard does not seem to be definitive, but there is no other
12248 valid interpretation of the following `::'. Therefore, those
12249 names are considered class-names. */
fc6a28d7 12250 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
a723baf1
MM
12251 else if (decl == error_mark_node
12252 || TREE_CODE (decl) != TYPE_DECL
07c65e00 12253 || TREE_TYPE (decl) == error_mark_node
a723baf1
MM
12254 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12255 {
12256 cp_parser_error (parser, "expected class-name");
12257 return error_mark_node;
12258 }
12259
12260 return decl;
12261}
12262
12263/* Parse a class-specifier.
12264
12265 class-specifier:
12266 class-head { member-specification [opt] }
12267
12268 Returns the TREE_TYPE representing the class. */
12269
12270static tree
94edc4ab 12271cp_parser_class_specifier (cp_parser* parser)
a723baf1
MM
12272{
12273 cp_token *token;
12274 tree type;
6de9cd9a 12275 tree attributes = NULL_TREE;
a723baf1
MM
12276 int has_trailing_semicolon;
12277 bool nested_name_specifier_p;
a723baf1 12278 unsigned saved_num_template_parameter_lists;
87c465f5 12279 tree old_scope = NULL_TREE;
2436b51f 12280 tree scope = NULL_TREE;
a723baf1 12281
8d241e0b 12282 push_deferring_access_checks (dk_no_deferred);
cf22909c 12283
a723baf1
MM
12284 /* Parse the class-head. */
12285 type = cp_parser_class_head (parser,
38b305d0
JM
12286 &nested_name_specifier_p,
12287 &attributes);
a723baf1
MM
12288 /* If the class-head was a semantic disaster, skip the entire body
12289 of the class. */
12290 if (!type)
12291 {
12292 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 12293 pop_deferring_access_checks ();
a723baf1
MM
12294 return error_mark_node;
12295 }
cf22909c 12296
a723baf1
MM
12297 /* Look for the `{'. */
12298 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
cf22909c
KL
12299 {
12300 pop_deferring_access_checks ();
12301 return error_mark_node;
12302 }
12303
a723baf1
MM
12304 /* Issue an error message if type-definitions are forbidden here. */
12305 cp_parser_check_type_definition (parser);
12306 /* Remember that we are defining one more class. */
12307 ++parser->num_classes_being_defined;
12308 /* Inside the class, surrounding template-parameter-lists do not
12309 apply. */
21526606
EC
12310 saved_num_template_parameter_lists
12311 = parser->num_template_parameter_lists;
a723baf1 12312 parser->num_template_parameter_lists = 0;
78757caa 12313
a723baf1 12314 /* Start the class. */
eeb23c11 12315 if (nested_name_specifier_p)
2436b51f
MM
12316 {
12317 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
87c465f5 12318 old_scope = push_inner_scope (scope);
2436b51f 12319 }
a723baf1 12320 type = begin_class_definition (type);
98ca843c 12321
a723baf1 12322 if (type == error_mark_node)
9bcb9aae 12323 /* If the type is erroneous, skip the entire body of the class. */
a723baf1
MM
12324 cp_parser_skip_to_closing_brace (parser);
12325 else
12326 /* Parse the member-specification. */
12327 cp_parser_member_specification_opt (parser);
98ca843c 12328
a723baf1
MM
12329 /* Look for the trailing `}'. */
12330 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12331 /* We get better error messages by noticing a common problem: a
12332 missing trailing `;'. */
12333 token = cp_lexer_peek_token (parser->lexer);
12334 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
38b305d0 12335 /* Look for trailing attributes to apply to this class. */
a723baf1 12336 if (cp_parser_allow_gnu_extensions_p (parser))
560ad596 12337 {
38b305d0
JM
12338 tree sub_attr = cp_parser_attributes_opt (parser);
12339 attributes = chainon (attributes, sub_attr);
560ad596 12340 }
38b305d0
JM
12341 if (type != error_mark_node)
12342 type = finish_struct (type, attributes);
87c465f5
KL
12343 if (nested_name_specifier_p)
12344 pop_inner_scope (old_scope, scope);
a723baf1
MM
12345 /* If this class is not itself within the scope of another class,
12346 then we need to parse the bodies of all of the queued function
12347 definitions. Note that the queued functions defined in a class
12348 are not always processed immediately following the
12349 class-specifier for that class. Consider:
12350
12351 struct A {
12352 struct B { void f() { sizeof (A); } };
12353 };
12354
12355 If `f' were processed before the processing of `A' were
12356 completed, there would be no way to compute the size of `A'.
12357 Note that the nesting we are interested in here is lexical --
12358 not the semantic nesting given by TYPE_CONTEXT. In particular,
12359 for:
12360
12361 struct A { struct B; };
12362 struct A::B { void f() { } };
12363
12364 there is no need to delay the parsing of `A::B::f'. */
21526606 12365 if (--parser->num_classes_being_defined == 0)
a723baf1 12366 {
8218bd34
MM
12367 tree queue_entry;
12368 tree fn;
4514aa8c
NS
12369 tree class_type = NULL_TREE;
12370 tree pushed_scope = NULL_TREE;
a723baf1 12371
8218bd34
MM
12372 /* In a first pass, parse default arguments to the functions.
12373 Then, in a second pass, parse the bodies of the functions.
12374 This two-phased approach handles cases like:
21526606
EC
12375
12376 struct S {
12377 void f() { g(); }
8218bd34
MM
12378 void g(int i = 3);
12379 };
12380
12381 */
8db1028e
NS
12382 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12383 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12384 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12385 TREE_PURPOSE (parser->unparsed_functions_queues)
12386 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
8218bd34
MM
12387 {
12388 fn = TREE_VALUE (queue_entry);
8218bd34
MM
12389 /* If there are default arguments that have not yet been processed,
12390 take care of them now. */
f44b0c8e
MM
12391 if (class_type != TREE_PURPOSE (queue_entry))
12392 {
4514aa8c
NS
12393 if (pushed_scope)
12394 pop_scope (pushed_scope);
f44b0c8e 12395 class_type = TREE_PURPOSE (queue_entry);
4514aa8c 12396 pushed_scope = push_scope (class_type);
f44b0c8e
MM
12397 }
12398 /* Make sure that any template parameters are in scope. */
12399 maybe_begin_member_template_processing (fn);
12400 /* Parse the default argument expressions. */
8218bd34
MM
12401 cp_parser_late_parsing_default_args (parser, fn);
12402 /* Remove any template parameters from the symbol table. */
12403 maybe_end_member_template_processing ();
12404 }
4514aa8c
NS
12405 if (pushed_scope)
12406 pop_scope (pushed_scope);
8218bd34 12407 /* Now parse the body of the functions. */
8db1028e
NS
12408 for (TREE_VALUE (parser->unparsed_functions_queues)
12409 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12410 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12411 TREE_VALUE (parser->unparsed_functions_queues)
12412 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
a723baf1 12413 {
a723baf1 12414 /* Figure out which function we need to process. */
a723baf1
MM
12415 fn = TREE_VALUE (queue_entry);
12416
4543ee47
ZD
12417 /* A hack to prevent garbage collection. */
12418 function_depth++;
12419
a723baf1
MM
12420 /* Parse the function. */
12421 cp_parser_late_parsing_for_member (parser, fn);
4543ee47 12422 function_depth--;
a723baf1 12423 }
a723baf1
MM
12424 }
12425
12426 /* Put back any saved access checks. */
cf22909c 12427 pop_deferring_access_checks ();
a723baf1
MM
12428
12429 /* Restore the count of active template-parameter-lists. */
12430 parser->num_template_parameter_lists
12431 = saved_num_template_parameter_lists;
12432
12433 return type;
12434}
12435
12436/* Parse a class-head.
12437
12438 class-head:
12439 class-key identifier [opt] base-clause [opt]
12440 class-key nested-name-specifier identifier base-clause [opt]
21526606
EC
12441 class-key nested-name-specifier [opt] template-id
12442 base-clause [opt]
a723baf1
MM
12443
12444 GNU Extensions:
12445 class-key attributes identifier [opt] base-clause [opt]
12446 class-key attributes nested-name-specifier identifier base-clause [opt]
21526606
EC
12447 class-key attributes nested-name-specifier [opt] template-id
12448 base-clause [opt]
a723baf1
MM
12449
12450 Returns the TYPE of the indicated class. Sets
12451 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12452 involving a nested-name-specifier was used, and FALSE otherwise.
a723baf1 12453
55dcbc12
NS
12454 Returns error_mark_node if this is not a class-head.
12455
a723baf1
MM
12456 Returns NULL_TREE if the class-head is syntactically valid, but
12457 semantically invalid in a way that means we should skip the entire
12458 body of the class. */
12459
12460static tree
21526606 12461cp_parser_class_head (cp_parser* parser,
38b305d0
JM
12462 bool* nested_name_specifier_p,
12463 tree *attributes_p)
a723baf1 12464{
a723baf1
MM
12465 tree nested_name_specifier;
12466 enum tag_types class_key;
12467 tree id = NULL_TREE;
12468 tree type = NULL_TREE;
12469 tree attributes;
12470 bool template_id_p = false;
12471 bool qualified_p = false;
12472 bool invalid_nested_name_p = false;
afb0918a 12473 bool invalid_explicit_specialization_p = false;
4514aa8c 12474 tree pushed_scope = NULL_TREE;
a723baf1 12475 unsigned num_templates;
cad7e87b 12476 tree bases;
a723baf1
MM
12477
12478 /* Assume no nested-name-specifier will be present. */
12479 *nested_name_specifier_p = false;
12480 /* Assume no template parameter lists will be used in defining the
12481 type. */
12482 num_templates = 0;
12483
12484 /* Look for the class-key. */
12485 class_key = cp_parser_class_key (parser);
12486 if (class_key == none_type)
12487 return error_mark_node;
12488
12489 /* Parse the attributes. */
12490 attributes = cp_parser_attributes_opt (parser);
12491
12492 /* If the next token is `::', that is invalid -- but sometimes
12493 people do try to write:
12494
21526606 12495 struct ::S {};
a723baf1
MM
12496
12497 Handle this gracefully by accepting the extra qualifier, and then
12498 issuing an error about it later if this really is a
2050a1bb 12499 class-head. If it turns out just to be an elaborated type
a723baf1
MM
12500 specifier, remain silent. */
12501 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12502 qualified_p = true;
12503
8d241e0b
KL
12504 push_deferring_access_checks (dk_no_check);
12505
a723baf1
MM
12506 /* Determine the name of the class. Begin by looking for an
12507 optional nested-name-specifier. */
21526606 12508 nested_name_specifier
a723baf1
MM
12509 = cp_parser_nested_name_specifier_opt (parser,
12510 /*typename_keyword_p=*/false,
66d418e6 12511 /*check_dependency_p=*/false,
a668c6ad
MM
12512 /*type_p=*/false,
12513 /*is_declaration=*/false);
a723baf1
MM
12514 /* If there was a nested-name-specifier, then there *must* be an
12515 identifier. */
12516 if (nested_name_specifier)
12517 {
12518 /* Although the grammar says `identifier', it really means
12519 `class-name' or `template-name'. You are only allowed to
12520 define a class that has already been declared with this
21526606 12521 syntax.
a723baf1
MM
12522
12523 The proposed resolution for Core Issue 180 says that whever
12524 you see `class T::X' you should treat `X' as a type-name.
21526606 12525
a723baf1 12526 It is OK to define an inaccessible class; for example:
21526606 12527
a723baf1
MM
12528 class A { class B; };
12529 class A::B {};
21526606 12530
a723baf1
MM
12531 We do not know if we will see a class-name, or a
12532 template-name. We look for a class-name first, in case the
12533 class-name is a template-id; if we looked for the
12534 template-name first we would stop after the template-name. */
12535 cp_parser_parse_tentatively (parser);
12536 type = cp_parser_class_name (parser,
12537 /*typename_keyword_p=*/false,
12538 /*template_keyword_p=*/false,
fc6a28d7 12539 class_type,
a723baf1 12540 /*check_dependency_p=*/false,
a668c6ad
MM
12541 /*class_head_p=*/true,
12542 /*is_declaration=*/false);
a723baf1
MM
12543 /* If that didn't work, ignore the nested-name-specifier. */
12544 if (!cp_parser_parse_definitely (parser))
12545 {
12546 invalid_nested_name_p = true;
12547 id = cp_parser_identifier (parser);
12548 if (id == error_mark_node)
12549 id = NULL_TREE;
12550 }
12551 /* If we could not find a corresponding TYPE, treat this
12552 declaration like an unqualified declaration. */
12553 if (type == error_mark_node)
12554 nested_name_specifier = NULL_TREE;
12555 /* Otherwise, count the number of templates used in TYPE and its
12556 containing scopes. */
21526606 12557 else
a723baf1
MM
12558 {
12559 tree scope;
12560
21526606 12561 for (scope = TREE_TYPE (type);
a723baf1 12562 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21526606 12563 scope = (TYPE_P (scope)
a723baf1 12564 ? TYPE_CONTEXT (scope)
21526606
EC
12565 : DECL_CONTEXT (scope)))
12566 if (TYPE_P (scope)
a723baf1
MM
12567 && CLASS_TYPE_P (scope)
12568 && CLASSTYPE_TEMPLATE_INFO (scope)
2050a1bb
MM
12569 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12570 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
a723baf1
MM
12571 ++num_templates;
12572 }
12573 }
12574 /* Otherwise, the identifier is optional. */
12575 else
12576 {
12577 /* We don't know whether what comes next is a template-id,
12578 an identifier, or nothing at all. */
12579 cp_parser_parse_tentatively (parser);
12580 /* Check for a template-id. */
21526606 12581 id = cp_parser_template_id (parser,
a723baf1 12582 /*template_keyword_p=*/false,
a668c6ad
MM
12583 /*check_dependency_p=*/true,
12584 /*is_declaration=*/true);
a723baf1
MM
12585 /* If that didn't work, it could still be an identifier. */
12586 if (!cp_parser_parse_definitely (parser))
12587 {
12588 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12589 id = cp_parser_identifier (parser);
12590 else
12591 id = NULL_TREE;
12592 }
12593 else
12594 {
12595 template_id_p = true;
12596 ++num_templates;
12597 }
12598 }
12599
8d241e0b
KL
12600 pop_deferring_access_checks ();
12601
15077df5
MM
12602 if (id)
12603 cp_parser_check_for_invalid_template_id (parser, id);
ee43dab5 12604
a723baf1
MM
12605 /* If it's not a `:' or a `{' then we can't really be looking at a
12606 class-head, since a class-head only appears as part of a
12607 class-specifier. We have to detect this situation before calling
12608 xref_tag, since that has irreversible side-effects. */
12609 if (!cp_parser_next_token_starts_class_definition_p (parser))
12610 {
2a13a625 12611 cp_parser_error (parser, "expected %<{%> or %<:%>");
a723baf1
MM
12612 return error_mark_node;
12613 }
12614
12615 /* At this point, we're going ahead with the class-specifier, even
12616 if some other problem occurs. */
12617 cp_parser_commit_to_tentative_parse (parser);
12618 /* Issue the error about the overly-qualified name now. */
12619 if (qualified_p)
12620 cp_parser_error (parser,
12621 "global qualification of class name is invalid");
12622 else if (invalid_nested_name_p)
12623 cp_parser_error (parser,
12624 "qualified name does not name a class");
88081599
MM
12625 else if (nested_name_specifier)
12626 {
12627 tree scope;
9bf0e588
VR
12628
12629 /* Reject typedef-names in class heads. */
12630 if (!DECL_IMPLICIT_TYPEDEF_P (type))
12631 {
12632 error ("invalid class name in declaration of %qD", type);
12633 type = NULL_TREE;
12634 goto done;
12635 }
12636
88081599
MM
12637 /* Figure out in what scope the declaration is being placed. */
12638 scope = current_scope ();
88081599
MM
12639 /* If that scope does not contain the scope in which the
12640 class was originally declared, the program is invalid. */
12641 if (scope && !is_ancestor (scope, nested_name_specifier))
12642 {
2a13a625
GDR
12643 error ("declaration of %qD in %qD which does not enclose %qD",
12644 type, scope, nested_name_specifier);
88081599
MM
12645 type = NULL_TREE;
12646 goto done;
12647 }
12648 /* [dcl.meaning]
12649
12650 A declarator-id shall not be qualified exception of the
12651 definition of a ... nested class outside of its class
12652 ... [or] a the definition or explicit instantiation of a
12653 class member of a namespace outside of its namespace. */
12654 if (scope == nested_name_specifier)
12655 {
12656 pedwarn ("extra qualification ignored");
12657 nested_name_specifier = NULL_TREE;
12658 num_templates = 0;
12659 }
12660 }
afb0918a
MM
12661 /* An explicit-specialization must be preceded by "template <>". If
12662 it is not, try to recover gracefully. */
21526606 12663 if (at_namespace_scope_p ()
afb0918a 12664 && parser->num_template_parameter_lists == 0
eeb23c11 12665 && template_id_p)
afb0918a 12666 {
2a13a625 12667 error ("an explicit specialization must be preceded by %<template <>%>");
afb0918a
MM
12668 invalid_explicit_specialization_p = true;
12669 /* Take the same action that would have been taken by
12670 cp_parser_explicit_specialization. */
12671 ++parser->num_template_parameter_lists;
12672 begin_specialization ();
12673 }
12674 /* There must be no "return" statements between this point and the
12675 end of this function; set "type "to the correct return value and
12676 use "goto done;" to return. */
a723baf1
MM
12677 /* Make sure that the right number of template parameters were
12678 present. */
12679 if (!cp_parser_check_template_parameters (parser, num_templates))
afb0918a
MM
12680 {
12681 /* If something went wrong, there is no point in even trying to
12682 process the class-definition. */
12683 type = NULL_TREE;
12684 goto done;
12685 }
a723baf1 12686
a723baf1
MM
12687 /* Look up the type. */
12688 if (template_id_p)
12689 {
12690 type = TREE_TYPE (id);
12691 maybe_process_partial_specialization (type);
4514aa8c
NS
12692 if (nested_name_specifier)
12693 pushed_scope = push_scope (nested_name_specifier);
a723baf1 12694 }
4514aa8c 12695 else if (nested_name_specifier)
a723baf1 12696 {
a723baf1
MM
12697 tree class_type;
12698
12699 /* Given:
12700
12701 template <typename T> struct S { struct T };
14d22dd6 12702 template <typename T> struct S<T>::T { };
a723baf1
MM
12703
12704 we will get a TYPENAME_TYPE when processing the definition of
12705 `S::T'. We need to resolve it to the actual type before we
12706 try to define it. */
12707 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12708 {
14d22dd6
MM
12709 class_type = resolve_typename_type (TREE_TYPE (type),
12710 /*only_current_p=*/false);
12711 if (class_type != error_mark_node)
12712 type = TYPE_NAME (class_type);
12713 else
12714 {
12715 cp_parser_error (parser, "could not resolve typename type");
12716 type = error_mark_node;
12717 }
a723baf1
MM
12718 }
12719
560ad596
MM
12720 maybe_process_partial_specialization (TREE_TYPE (type));
12721 class_type = current_class_type;
12722 /* Enter the scope indicated by the nested-name-specifier. */
4514aa8c 12723 pushed_scope = push_scope (nested_name_specifier);
560ad596
MM
12724 /* Get the canonical version of this type. */
12725 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12726 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12727 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
55dcbc12
NS
12728 {
12729 type = push_template_decl (type);
12730 if (type == error_mark_node)
12731 {
12732 type = NULL_TREE;
12733 goto done;
12734 }
12735 }
12736
560ad596 12737 type = TREE_TYPE (type);
4514aa8c 12738 *nested_name_specifier_p = true;
a723baf1 12739 }
4514aa8c
NS
12740 else /* The name is not a nested name. */
12741 {
12742 /* If the class was unnamed, create a dummy name. */
12743 if (!id)
12744 id = make_anon_name ();
12745 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
12746 parser->num_template_parameter_lists);
12747 }
12748
a723baf1
MM
12749 /* Indicate whether this class was declared as a `class' or as a
12750 `struct'. */
12751 if (TREE_CODE (type) == RECORD_TYPE)
12752 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12753 cp_parser_check_class_key (class_key, type);
12754
4514aa8c
NS
12755 /* We will have entered the scope containing the class; the names of
12756 base classes should be looked up in that context. For example,
12757 given:
a723baf1
MM
12758
12759 struct A { struct B {}; struct C; };
12760 struct A::C : B {};
12761
12762 is valid. */
cad7e87b 12763 bases = NULL_TREE;
98ca843c 12764
cad7e87b
NS
12765 /* Get the list of base-classes, if there is one. */
12766 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12767 bases = cp_parser_base_clause (parser);
98ca843c 12768
cad7e87b
NS
12769 /* Process the base classes. */
12770 xref_basetypes (type, bases);
a723baf1 12771
4514aa8c 12772 done:
a723baf1
MM
12773 /* Leave the scope given by the nested-name-specifier. We will
12774 enter the class scope itself while processing the members. */
4514aa8c
NS
12775 if (pushed_scope)
12776 pop_scope (pushed_scope);
a723baf1 12777
afb0918a
MM
12778 if (invalid_explicit_specialization_p)
12779 {
12780 end_specialization ();
12781 --parser->num_template_parameter_lists;
12782 }
38b305d0 12783 *attributes_p = attributes;
a723baf1
MM
12784 return type;
12785}
12786
12787/* Parse a class-key.
12788
12789 class-key:
12790 class
12791 struct
12792 union
12793
12794 Returns the kind of class-key specified, or none_type to indicate
12795 error. */
12796
12797static enum tag_types
94edc4ab 12798cp_parser_class_key (cp_parser* parser)
a723baf1
MM
12799{
12800 cp_token *token;
12801 enum tag_types tag_type;
12802
12803 /* Look for the class-key. */
12804 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12805 if (!token)
12806 return none_type;
12807
12808 /* Check to see if the TOKEN is a class-key. */
12809 tag_type = cp_parser_token_is_class_key (token);
12810 if (!tag_type)
12811 cp_parser_error (parser, "expected class-key");
12812 return tag_type;
12813}
12814
12815/* Parse an (optional) member-specification.
12816
12817 member-specification:
12818 member-declaration member-specification [opt]
12819 access-specifier : member-specification [opt] */
12820
12821static void
94edc4ab 12822cp_parser_member_specification_opt (cp_parser* parser)
a723baf1
MM
12823{
12824 while (true)
12825 {
12826 cp_token *token;
12827 enum rid keyword;
12828
12829 /* Peek at the next token. */
12830 token = cp_lexer_peek_token (parser->lexer);
12831 /* If it's a `}', or EOF then we've seen all the members. */
12832 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12833 break;
12834
12835 /* See if this token is a keyword. */
12836 keyword = token->keyword;
12837 switch (keyword)
12838 {
12839 case RID_PUBLIC:
12840 case RID_PROTECTED:
12841 case RID_PRIVATE:
12842 /* Consume the access-specifier. */
12843 cp_lexer_consume_token (parser->lexer);
12844 /* Remember which access-specifier is active. */
12845 current_access_specifier = token->value;
12846 /* Look for the `:'. */
12847 cp_parser_require (parser, CPP_COLON, "`:'");
12848 break;
12849
12850 default:
de3fe73c
MM
12851 /* Accept #pragmas at class scope. */
12852 if (token->type == CPP_PRAGMA)
12853 {
12854 cp_lexer_handle_pragma (parser->lexer);
12855 break;
12856 }
12857
a723baf1
MM
12858 /* Otherwise, the next construction must be a
12859 member-declaration. */
12860 cp_parser_member_declaration (parser);
a723baf1
MM
12861 }
12862 }
12863}
12864
21526606 12865/* Parse a member-declaration.
a723baf1
MM
12866
12867 member-declaration:
12868 decl-specifier-seq [opt] member-declarator-list [opt] ;
12869 function-definition ; [opt]
12870 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12871 using-declaration
21526606 12872 template-declaration
a723baf1
MM
12873
12874 member-declarator-list:
12875 member-declarator
12876 member-declarator-list , member-declarator
12877
12878 member-declarator:
21526606 12879 declarator pure-specifier [opt]
a723baf1 12880 declarator constant-initializer [opt]
21526606 12881 identifier [opt] : constant-expression
a723baf1
MM
12882
12883 GNU Extensions:
12884
12885 member-declaration:
12886 __extension__ member-declaration
12887
12888 member-declarator:
12889 declarator attributes [opt] pure-specifier [opt]
12890 declarator attributes [opt] constant-initializer [opt]
12891 identifier [opt] attributes [opt] : constant-expression */
12892
12893static void
94edc4ab 12894cp_parser_member_declaration (cp_parser* parser)
a723baf1 12895{
62d1db17 12896 cp_decl_specifier_seq decl_specifiers;
a723baf1
MM
12897 tree prefix_attributes;
12898 tree decl;
560ad596 12899 int declares_class_or_enum;
a723baf1
MM
12900 bool friend_p;
12901 cp_token *token;
12902 int saved_pedantic;
12903
12904 /* Check for the `__extension__' keyword. */
12905 if (cp_parser_extension_opt (parser, &saved_pedantic))
12906 {
12907 /* Recurse. */
12908 cp_parser_member_declaration (parser);
12909 /* Restore the old value of the PEDANTIC flag. */
12910 pedantic = saved_pedantic;
12911
12912 return;
12913 }
12914
12915 /* Check for a template-declaration. */
12916 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12917 {
12918 /* Parse the template-declaration. */
12919 cp_parser_template_declaration (parser, /*member_p=*/true);
12920
12921 return;
12922 }
12923
12924 /* Check for a using-declaration. */
12925 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12926 {
12927 /* Parse the using-declaration. */
12928 cp_parser_using_declaration (parser);
12929
12930 return;
12931 }
21526606 12932
a723baf1 12933 /* Parse the decl-specifier-seq. */
62d1db17
MM
12934 cp_parser_decl_specifier_seq (parser,
12935 CP_PARSER_FLAGS_OPTIONAL,
12936 &decl_specifiers,
12937 &declares_class_or_enum);
12938 prefix_attributes = decl_specifiers.attributes;
12939 decl_specifiers.attributes = NULL_TREE;
8fbc5ae7 12940 /* Check for an invalid type-name. */
de3fe73c
MM
12941 if (!decl_specifiers.type
12942 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8fbc5ae7 12943 return;
a723baf1
MM
12944 /* If there is no declarator, then the decl-specifier-seq should
12945 specify a type. */
12946 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12947 {
12948 /* If there was no decl-specifier-seq, and the next token is a
12949 `;', then we have something like:
12950
12951 struct S { ; };
12952
12953 [class.mem]
12954
12955 Each member-declaration shall declare at least one member
12956 name of the class. */
62d1db17 12957 if (!decl_specifiers.any_specifiers_p)
a723baf1 12958 {
2cfe82fe
ZW
12959 cp_token *token = cp_lexer_peek_token (parser->lexer);
12960 if (pedantic && !token->in_system_header)
12961 pedwarn ("%Hextra %<;%>", &token->location);
a723baf1 12962 }
21526606 12963 else
a723baf1
MM
12964 {
12965 tree type;
21526606 12966
a723baf1 12967 /* See if this declaration is a friend. */
62d1db17 12968 friend_p = cp_parser_friend_p (&decl_specifiers);
a723baf1
MM
12969 /* If there were decl-specifiers, check to see if there was
12970 a class-declaration. */
62d1db17 12971 type = check_tag_decl (&decl_specifiers);
a723baf1
MM
12972 /* Nested classes have already been added to the class, but
12973 a `friend' needs to be explicitly registered. */
12974 if (friend_p)
12975 {
12976 /* If the `friend' keyword was present, the friend must
12977 be introduced with a class-key. */
12978 if (!declares_class_or_enum)
12979 error ("a class-key must be used when declaring a friend");
12980 /* In this case:
12981
21526606
EC
12982 template <typename T> struct A {
12983 friend struct A<T>::B;
a723baf1 12984 };
21526606 12985
a723baf1
MM
12986 A<T>::B will be represented by a TYPENAME_TYPE, and
12987 therefore not recognized by check_tag_decl. */
98ca843c 12988 if (!type
62d1db17
MM
12989 && decl_specifiers.type
12990 && TYPE_P (decl_specifiers.type))
12991 type = decl_specifiers.type;
fdd09134 12992 if (!type || !TYPE_P (type))
a723baf1
MM
12993 error ("friend declaration does not name a class or "
12994 "function");
12995 else
19db77ce
KL
12996 make_friend_class (current_class_type, type,
12997 /*complain=*/true);
a723baf1
MM
12998 }
12999 /* If there is no TYPE, an error message will already have
13000 been issued. */
62d1db17 13001 else if (!type || type == error_mark_node)
a723baf1
MM
13002 ;
13003 /* An anonymous aggregate has to be handled specially; such
13004 a declaration really declares a data member (with a
13005 particular type), as opposed to a nested class. */
13006 else if (ANON_AGGR_TYPE_P (type))
13007 {
13008 /* Remove constructors and such from TYPE, now that we
34cd5ae7 13009 know it is an anonymous aggregate. */
a723baf1
MM
13010 fixup_anonymous_aggr (type);
13011 /* And make the corresponding data member. */
13012 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13013 /* Add it to the class. */
13014 finish_member_declaration (decl);
13015 }
37d407a1
KL
13016 else
13017 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
a723baf1
MM
13018 }
13019 }
13020 else
13021 {
13022 /* See if these declarations will be friends. */
62d1db17 13023 friend_p = cp_parser_friend_p (&decl_specifiers);
a723baf1 13024
21526606 13025 /* Keep going until we hit the `;' at the end of the
a723baf1
MM
13026 declaration. */
13027 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13028 {
13029 tree attributes = NULL_TREE;
13030 tree first_attribute;
13031
13032 /* Peek at the next token. */
13033 token = cp_lexer_peek_token (parser->lexer);
13034
13035 /* Check for a bitfield declaration. */
13036 if (token->type == CPP_COLON
13037 || (token->type == CPP_NAME
21526606 13038 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1
MM
13039 == CPP_COLON))
13040 {
13041 tree identifier;
13042 tree width;
13043
13044 /* Get the name of the bitfield. Note that we cannot just
13045 check TOKEN here because it may have been invalidated by
13046 the call to cp_lexer_peek_nth_token above. */
13047 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13048 identifier = cp_parser_identifier (parser);
13049 else
13050 identifier = NULL_TREE;
13051
13052 /* Consume the `:' token. */
13053 cp_lexer_consume_token (parser->lexer);
13054 /* Get the width of the bitfield. */
21526606 13055 width
14d22dd6
MM
13056 = cp_parser_constant_expression (parser,
13057 /*allow_non_constant=*/false,
13058 NULL);
a723baf1
MM
13059
13060 /* Look for attributes that apply to the bitfield. */
13061 attributes = cp_parser_attributes_opt (parser);
13062 /* Remember which attributes are prefix attributes and
13063 which are not. */
13064 first_attribute = attributes;
13065 /* Combine the attributes. */
13066 attributes = chainon (prefix_attributes, attributes);
13067
13068 /* Create the bitfield declaration. */
98ca843c 13069 decl = grokbitfield (identifier
1d786913
MM
13070 ? make_id_declarator (NULL_TREE,
13071 identifier)
058b15c1 13072 : NULL,
62d1db17 13073 &decl_specifiers,
a723baf1
MM
13074 width);
13075 /* Apply the attributes. */
13076 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13077 }
13078 else
13079 {
058b15c1 13080 cp_declarator *declarator;
a723baf1
MM
13081 tree initializer;
13082 tree asm_specification;
7efa3e22 13083 int ctor_dtor_or_conv_p;
a723baf1
MM
13084
13085 /* Parse the declarator. */
21526606 13086 declarator
62b8a44e 13087 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 13088 &ctor_dtor_or_conv_p,
db86dd14
MM
13089 /*parenthesized_p=*/NULL,
13090 /*member_p=*/true);
a723baf1
MM
13091
13092 /* If something went wrong parsing the declarator, make sure
13093 that we at least consume some tokens. */
058b15c1 13094 if (declarator == cp_error_declarator)
a723baf1
MM
13095 {
13096 /* Skip to the end of the statement. */
13097 cp_parser_skip_to_end_of_statement (parser);
4bb8ca28
MM
13098 /* If the next token is not a semicolon, that is
13099 probably because we just skipped over the body of
13100 a function. So, we consume a semicolon if
13101 present, but do not issue an error message if it
13102 is not present. */
13103 if (cp_lexer_next_token_is (parser->lexer,
13104 CPP_SEMICOLON))
13105 cp_lexer_consume_token (parser->lexer);
13106 return;
a723baf1
MM
13107 }
13108
fc6a28d7
MM
13109 if (declares_class_or_enum & 2)
13110 cp_parser_check_for_definition_in_return_type
13111 (declarator, decl_specifiers.type);
560ad596 13112
a723baf1
MM
13113 /* Look for an asm-specification. */
13114 asm_specification = cp_parser_asm_specification_opt (parser);
13115 /* Look for attributes that apply to the declaration. */
13116 attributes = cp_parser_attributes_opt (parser);
13117 /* Remember which attributes are prefix attributes and
13118 which are not. */
13119 first_attribute = attributes;
13120 /* Combine the attributes. */
13121 attributes = chainon (prefix_attributes, attributes);
13122
13123 /* If it's an `=', then we have a constant-initializer or a
13124 pure-specifier. It is not correct to parse the
13125 initializer before registering the member declaration
13126 since the member declaration should be in scope while
13127 its initializer is processed. However, the rest of the
13128 front end does not yet provide an interface that allows
13129 us to handle this correctly. */
13130 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13131 {
13132 /* In [class.mem]:
13133
13134 A pure-specifier shall be used only in the declaration of
21526606 13135 a virtual function.
a723baf1
MM
13136
13137 A member-declarator can contain a constant-initializer
13138 only if it declares a static member of integral or
21526606 13139 enumeration type.
a723baf1
MM
13140
13141 Therefore, if the DECLARATOR is for a function, we look
13142 for a pure-specifier; otherwise, we look for a
13143 constant-initializer. When we call `grokfield', it will
13144 perform more stringent semantics checks. */
058b15c1 13145 if (declarator->kind == cdk_function)
a723baf1
MM
13146 initializer = cp_parser_pure_specifier (parser);
13147 else
4bb8ca28
MM
13148 /* Parse the initializer. */
13149 initializer = cp_parser_constant_initializer (parser);
a723baf1
MM
13150 }
13151 /* Otherwise, there is no initializer. */
13152 else
13153 initializer = NULL_TREE;
13154
13155 /* See if we are probably looking at a function
5a19910e 13156 definition. We are certainly not looking at a
a723baf1
MM
13157 member-declarator. Calling `grokfield' has
13158 side-effects, so we must not do it unless we are sure
13159 that we are looking at a member-declarator. */
21526606 13160 if (cp_parser_token_starts_function_definition_p
a723baf1 13161 (cp_lexer_peek_token (parser->lexer)))
4bb8ca28
MM
13162 {
13163 /* The grammar does not allow a pure-specifier to be
13164 used when a member function is defined. (It is
13165 possible that this fact is an oversight in the
13166 standard, since a pure function may be defined
13167 outside of the class-specifier. */
13168 if (initializer)
13169 error ("pure-specifier on function-definition");
13170 decl = cp_parser_save_member_function_body (parser,
62d1db17 13171 &decl_specifiers,
4bb8ca28
MM
13172 declarator,
13173 attributes);
13174 /* If the member was not a friend, declare it here. */
13175 if (!friend_p)
13176 finish_member_declaration (decl);
13177 /* Peek at the next token. */
13178 token = cp_lexer_peek_token (parser->lexer);
13179 /* If the next token is a semicolon, consume it. */
13180 if (token->type == CPP_SEMICOLON)
13181 cp_lexer_consume_token (parser->lexer);
13182 return;
13183 }
a723baf1 13184 else
39703eb9
MM
13185 {
13186 /* Create the declaration. */
62d1db17 13187 decl = grokfield (declarator, &decl_specifiers,
ee3071ef 13188 initializer, asm_specification,
39703eb9
MM
13189 attributes);
13190 /* Any initialization must have been from a
13191 constant-expression. */
13192 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13193 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13194 }
a723baf1
MM
13195 }
13196
13197 /* Reset PREFIX_ATTRIBUTES. */
13198 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13199 attributes = TREE_CHAIN (attributes);
13200 if (attributes)
13201 TREE_CHAIN (attributes) = NULL_TREE;
13202
13203 /* If there is any qualification still in effect, clear it
13204 now; we will be starting fresh with the next declarator. */
13205 parser->scope = NULL_TREE;
13206 parser->qualifying_scope = NULL_TREE;
13207 parser->object_scope = NULL_TREE;
13208 /* If it's a `,', then there are more declarators. */
13209 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13210 cp_lexer_consume_token (parser->lexer);
13211 /* If the next token isn't a `;', then we have a parse error. */
13212 else if (cp_lexer_next_token_is_not (parser->lexer,
13213 CPP_SEMICOLON))
13214 {
2a13a625 13215 cp_parser_error (parser, "expected %<;%>");
04c06002 13216 /* Skip tokens until we find a `;'. */
a723baf1
MM
13217 cp_parser_skip_to_end_of_statement (parser);
13218
13219 break;
13220 }
13221
13222 if (decl)
13223 {
13224 /* Add DECL to the list of members. */
13225 if (!friend_p)
13226 finish_member_declaration (decl);
13227
a723baf1 13228 if (TREE_CODE (decl) == FUNCTION_DECL)
8db1028e 13229 cp_parser_save_default_args (parser, decl);
a723baf1
MM
13230 }
13231 }
13232 }
13233
4bb8ca28 13234 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
a723baf1
MM
13235}
13236
13237/* Parse a pure-specifier.
13238
13239 pure-specifier:
13240 = 0
13241
13242 Returns INTEGER_ZERO_NODE if a pure specifier is found.
cd0be382 13243 Otherwise, ERROR_MARK_NODE is returned. */
a723baf1
MM
13244
13245static tree
94edc4ab 13246cp_parser_pure_specifier (cp_parser* parser)
a723baf1
MM
13247{
13248 cp_token *token;
13249
13250 /* Look for the `=' token. */
13251 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13252 return error_mark_node;
13253 /* Look for the `0' token. */
13254 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13255 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
13256 to get information from the lexer about how the number was
13257 spelled in order to fix this problem. */
13258 if (!token || !integer_zerop (token->value))
13259 return error_mark_node;
13260
13261 return integer_zero_node;
13262}
13263
13264/* Parse a constant-initializer.
13265
13266 constant-initializer:
13267 = constant-expression
13268
13269 Returns a representation of the constant-expression. */
13270
13271static tree
94edc4ab 13272cp_parser_constant_initializer (cp_parser* parser)
a723baf1
MM
13273{
13274 /* Look for the `=' token. */
13275 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13276 return error_mark_node;
13277
13278 /* It is invalid to write:
13279
13280 struct S { static const int i = { 7 }; };
13281
13282 */
13283 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13284 {
13285 cp_parser_error (parser,
13286 "a brace-enclosed initializer is not allowed here");
13287 /* Consume the opening brace. */
13288 cp_lexer_consume_token (parser->lexer);
13289 /* Skip the initializer. */
13290 cp_parser_skip_to_closing_brace (parser);
13291 /* Look for the trailing `}'. */
13292 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
21526606 13293
a723baf1
MM
13294 return error_mark_node;
13295 }
13296
21526606 13297 return cp_parser_constant_expression (parser,
14d22dd6
MM
13298 /*allow_non_constant=*/false,
13299 NULL);
a723baf1
MM
13300}
13301
13302/* Derived classes [gram.class.derived] */
13303
13304/* Parse a base-clause.
13305
13306 base-clause:
21526606 13307 : base-specifier-list
a723baf1
MM
13308
13309 base-specifier-list:
13310 base-specifier
13311 base-specifier-list , base-specifier
13312
13313 Returns a TREE_LIST representing the base-classes, in the order in
13314 which they were declared. The representation of each node is as
21526606 13315 described by cp_parser_base_specifier.
a723baf1
MM
13316
13317 In the case that no bases are specified, this function will return
13318 NULL_TREE, not ERROR_MARK_NODE. */
13319
13320static tree
94edc4ab 13321cp_parser_base_clause (cp_parser* parser)
a723baf1
MM
13322{
13323 tree bases = NULL_TREE;
13324
13325 /* Look for the `:' that begins the list. */
13326 cp_parser_require (parser, CPP_COLON, "`:'");
13327
13328 /* Scan the base-specifier-list. */
13329 while (true)
13330 {
13331 cp_token *token;
13332 tree base;
13333
13334 /* Look for the base-specifier. */
13335 base = cp_parser_base_specifier (parser);
13336 /* Add BASE to the front of the list. */
13337 if (base != error_mark_node)
13338 {
13339 TREE_CHAIN (base) = bases;
13340 bases = base;
13341 }
13342 /* Peek at the next token. */
13343 token = cp_lexer_peek_token (parser->lexer);
13344 /* If it's not a comma, then the list is complete. */
13345 if (token->type != CPP_COMMA)
13346 break;
13347 /* Consume the `,'. */
13348 cp_lexer_consume_token (parser->lexer);
13349 }
13350
13351 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13352 base class had a qualified name. However, the next name that
13353 appears is certainly not qualified. */
13354 parser->scope = NULL_TREE;
13355 parser->qualifying_scope = NULL_TREE;
13356 parser->object_scope = NULL_TREE;
13357
13358 return nreverse (bases);
13359}
13360
13361/* Parse a base-specifier.
13362
13363 base-specifier:
13364 :: [opt] nested-name-specifier [opt] class-name
13365 virtual access-specifier [opt] :: [opt] nested-name-specifier
13366 [opt] class-name
13367 access-specifier virtual [opt] :: [opt] nested-name-specifier
13368 [opt] class-name
13369
13370 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13371 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13372 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13373 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21526606 13374
a723baf1 13375static tree
94edc4ab 13376cp_parser_base_specifier (cp_parser* parser)
a723baf1
MM
13377{
13378 cp_token *token;
13379 bool done = false;
13380 bool virtual_p = false;
13381 bool duplicate_virtual_error_issued_p = false;
13382 bool duplicate_access_error_issued_p = false;
bbaab916 13383 bool class_scope_p, template_p;
dbbf88d1 13384 tree access = access_default_node;
a723baf1
MM
13385 tree type;
13386
13387 /* Process the optional `virtual' and `access-specifier'. */
13388 while (!done)
13389 {
13390 /* Peek at the next token. */
13391 token = cp_lexer_peek_token (parser->lexer);
13392 /* Process `virtual'. */
13393 switch (token->keyword)
13394 {
13395 case RID_VIRTUAL:
13396 /* If `virtual' appears more than once, issue an error. */
13397 if (virtual_p && !duplicate_virtual_error_issued_p)
13398 {
13399 cp_parser_error (parser,
2a13a625 13400 "%<virtual%> specified more than once in base-specified");
a723baf1
MM
13401 duplicate_virtual_error_issued_p = true;
13402 }
13403
13404 virtual_p = true;
13405
13406 /* Consume the `virtual' token. */
13407 cp_lexer_consume_token (parser->lexer);
13408
13409 break;
13410
13411 case RID_PUBLIC:
13412 case RID_PROTECTED:
13413 case RID_PRIVATE:
13414 /* If more than one access specifier appears, issue an
13415 error. */
dbbf88d1
NS
13416 if (access != access_default_node
13417 && !duplicate_access_error_issued_p)
a723baf1
MM
13418 {
13419 cp_parser_error (parser,
13420 "more than one access specifier in base-specified");
13421 duplicate_access_error_issued_p = true;
13422 }
13423
dbbf88d1 13424 access = ridpointers[(int) token->keyword];
a723baf1
MM
13425
13426 /* Consume the access-specifier. */
13427 cp_lexer_consume_token (parser->lexer);
13428
13429 break;
13430
13431 default:
13432 done = true;
13433 break;
13434 }
13435 }
852dcbdd 13436 /* It is not uncommon to see programs mechanically, erroneously, use
a3a503a5 13437 the 'typename' keyword to denote (dependent) qualified types
1ed53ef3
GB
13438 as base classes. */
13439 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13440 {
13441 if (!processing_template_decl)
2a13a625 13442 error ("keyword %<typename%> not allowed outside of templates");
1ed53ef3 13443 else
2a13a625 13444 error ("keyword %<typename%> not allowed in this context "
1ed53ef3
GB
13445 "(the base class is implicitly a type)");
13446 cp_lexer_consume_token (parser->lexer);
13447 }
a723baf1 13448
a723baf1
MM
13449 /* Look for the optional `::' operator. */
13450 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13451 /* Look for the nested-name-specifier. The simplest way to
13452 implement:
13453
13454 [temp.res]
13455
13456 The keyword `typename' is not permitted in a base-specifier or
13457 mem-initializer; in these contexts a qualified name that
13458 depends on a template-parameter is implicitly assumed to be a
13459 type name.
13460
13461 is to pretend that we have seen the `typename' keyword at this
21526606 13462 point. */
a723baf1
MM
13463 cp_parser_nested_name_specifier_opt (parser,
13464 /*typename_keyword_p=*/true,
13465 /*check_dependency_p=*/true,
fc6a28d7 13466 typename_type,
a668c6ad 13467 /*is_declaration=*/true);
a723baf1
MM
13468 /* If the base class is given by a qualified name, assume that names
13469 we see are type names or templates, as appropriate. */
13470 class_scope_p = (parser->scope && TYPE_P (parser->scope));
bbaab916 13471 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21526606 13472
a723baf1 13473 /* Finally, look for the class-name. */
21526606 13474 type = cp_parser_class_name (parser,
a723baf1 13475 class_scope_p,
bbaab916 13476 template_p,
fc6a28d7 13477 typename_type,
a723baf1 13478 /*check_dependency_p=*/true,
a668c6ad
MM
13479 /*class_head_p=*/false,
13480 /*is_declaration=*/true);
a723baf1
MM
13481
13482 if (type == error_mark_node)
13483 return error_mark_node;
13484
dbbf88d1 13485 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
a723baf1
MM
13486}
13487
13488/* Exception handling [gram.exception] */
13489
13490/* Parse an (optional) exception-specification.
13491
13492 exception-specification:
13493 throw ( type-id-list [opt] )
13494
13495 Returns a TREE_LIST representing the exception-specification. The
13496 TREE_VALUE of each node is a type. */
13497
13498static tree
94edc4ab 13499cp_parser_exception_specification_opt (cp_parser* parser)
a723baf1
MM
13500{
13501 cp_token *token;
13502 tree type_id_list;
13503
13504 /* Peek at the next token. */
13505 token = cp_lexer_peek_token (parser->lexer);
13506 /* If it's not `throw', then there's no exception-specification. */
13507 if (!cp_parser_is_keyword (token, RID_THROW))
13508 return NULL_TREE;
13509
13510 /* Consume the `throw'. */
13511 cp_lexer_consume_token (parser->lexer);
13512
13513 /* Look for the `('. */
13514 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13515
13516 /* Peek at the next token. */
13517 token = cp_lexer_peek_token (parser->lexer);
13518 /* If it's not a `)', then there is a type-id-list. */
13519 if (token->type != CPP_CLOSE_PAREN)
13520 {
13521 const char *saved_message;
13522
13523 /* Types may not be defined in an exception-specification. */
13524 saved_message = parser->type_definition_forbidden_message;
13525 parser->type_definition_forbidden_message
13526 = "types may not be defined in an exception-specification";
13527 /* Parse the type-id-list. */
13528 type_id_list = cp_parser_type_id_list (parser);
13529 /* Restore the saved message. */
13530 parser->type_definition_forbidden_message = saved_message;
13531 }
13532 else
13533 type_id_list = empty_except_spec;
13534
13535 /* Look for the `)'. */
13536 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13537
13538 return type_id_list;
13539}
13540
13541/* Parse an (optional) type-id-list.
13542
13543 type-id-list:
13544 type-id
13545 type-id-list , type-id
13546
13547 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13548 in the order that the types were presented. */
13549
13550static tree
94edc4ab 13551cp_parser_type_id_list (cp_parser* parser)
a723baf1
MM
13552{
13553 tree types = NULL_TREE;
13554
13555 while (true)
13556 {
13557 cp_token *token;
13558 tree type;
13559
13560 /* Get the next type-id. */
13561 type = cp_parser_type_id (parser);
13562 /* Add it to the list. */
13563 types = add_exception_specifier (types, type, /*complain=*/1);
13564 /* Peek at the next token. */
13565 token = cp_lexer_peek_token (parser->lexer);
13566 /* If it is not a `,', we are done. */
13567 if (token->type != CPP_COMMA)
13568 break;
13569 /* Consume the `,'. */
13570 cp_lexer_consume_token (parser->lexer);
13571 }
13572
13573 return nreverse (types);
13574}
13575
13576/* Parse a try-block.
13577
13578 try-block:
13579 try compound-statement handler-seq */
13580
13581static tree
94edc4ab 13582cp_parser_try_block (cp_parser* parser)
a723baf1
MM
13583{
13584 tree try_block;
13585
13586 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13587 try_block = begin_try_block ();
325c3691 13588 cp_parser_compound_statement (parser, NULL, true);
a723baf1
MM
13589 finish_try_block (try_block);
13590 cp_parser_handler_seq (parser);
13591 finish_handler_sequence (try_block);
13592
13593 return try_block;
13594}
13595
13596/* Parse a function-try-block.
13597
13598 function-try-block:
13599 try ctor-initializer [opt] function-body handler-seq */
13600
13601static bool
94edc4ab 13602cp_parser_function_try_block (cp_parser* parser)
a723baf1
MM
13603{
13604 tree try_block;
13605 bool ctor_initializer_p;
13606
13607 /* Look for the `try' keyword. */
13608 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13609 return false;
13610 /* Let the rest of the front-end know where we are. */
13611 try_block = begin_function_try_block ();
13612 /* Parse the function-body. */
21526606 13613 ctor_initializer_p
a723baf1
MM
13614 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13615 /* We're done with the `try' part. */
13616 finish_function_try_block (try_block);
13617 /* Parse the handlers. */
13618 cp_parser_handler_seq (parser);
13619 /* We're done with the handlers. */
13620 finish_function_handler_sequence (try_block);
13621
13622 return ctor_initializer_p;
13623}
13624
13625/* Parse a handler-seq.
13626
13627 handler-seq:
13628 handler handler-seq [opt] */
13629
13630static void
94edc4ab 13631cp_parser_handler_seq (cp_parser* parser)
a723baf1
MM
13632{
13633 while (true)
13634 {
13635 cp_token *token;
13636
13637 /* Parse the handler. */
13638 cp_parser_handler (parser);
13639 /* Peek at the next token. */
13640 token = cp_lexer_peek_token (parser->lexer);
13641 /* If it's not `catch' then there are no more handlers. */
13642 if (!cp_parser_is_keyword (token, RID_CATCH))
13643 break;
13644 }
13645}
13646
13647/* Parse a handler.
13648
13649 handler:
13650 catch ( exception-declaration ) compound-statement */
13651
13652static void
94edc4ab 13653cp_parser_handler (cp_parser* parser)
a723baf1
MM
13654{
13655 tree handler;
13656 tree declaration;
13657
13658 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13659 handler = begin_handler ();
13660 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13661 declaration = cp_parser_exception_declaration (parser);
13662 finish_handler_parms (declaration, handler);
13663 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
325c3691 13664 cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
13665 finish_handler (handler);
13666}
13667
13668/* Parse an exception-declaration.
13669
13670 exception-declaration:
13671 type-specifier-seq declarator
13672 type-specifier-seq abstract-declarator
13673 type-specifier-seq
21526606 13674 ...
a723baf1
MM
13675
13676 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13677 ellipsis variant is used. */
13678
13679static tree
94edc4ab 13680cp_parser_exception_declaration (cp_parser* parser)
a723baf1 13681{
058b15c1 13682 tree decl;
62d1db17 13683 cp_decl_specifier_seq type_specifiers;
058b15c1 13684 cp_declarator *declarator;
a723baf1
MM
13685 const char *saved_message;
13686
13687 /* If it's an ellipsis, it's easy to handle. */
13688 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13689 {
13690 /* Consume the `...' token. */
13691 cp_lexer_consume_token (parser->lexer);
13692 return NULL_TREE;
13693 }
13694
13695 /* Types may not be defined in exception-declarations. */
13696 saved_message = parser->type_definition_forbidden_message;
13697 parser->type_definition_forbidden_message
13698 = "types may not be defined in exception-declarations";
13699
13700 /* Parse the type-specifier-seq. */
62d1db17 13701 cp_parser_type_specifier_seq (parser, &type_specifiers);
a723baf1
MM
13702 /* If it's a `)', then there is no declarator. */
13703 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
058b15c1 13704 declarator = NULL;
a723baf1 13705 else
62b8a44e 13706 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
4bb8ca28 13707 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
13708 /*parenthesized_p=*/NULL,
13709 /*member_p=*/false);
a723baf1
MM
13710
13711 /* Restore the saved message. */
13712 parser->type_definition_forbidden_message = saved_message;
13713
62d1db17 13714 if (type_specifiers.any_specifiers_p)
058b15c1 13715 {
62d1db17 13716 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
058b15c1
MM
13717 if (decl == NULL_TREE)
13718 error ("invalid catch parameter");
13719 }
13720 else
13721 decl = NULL_TREE;
13722
13723 return decl;
a723baf1
MM
13724}
13725
21526606 13726/* Parse a throw-expression.
a723baf1
MM
13727
13728 throw-expression:
34cd5ae7 13729 throw assignment-expression [opt]
a723baf1
MM
13730
13731 Returns a THROW_EXPR representing the throw-expression. */
13732
13733static tree
94edc4ab 13734cp_parser_throw_expression (cp_parser* parser)
a723baf1
MM
13735{
13736 tree expression;
89f1a6ec 13737 cp_token* token;
a723baf1
MM
13738
13739 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
89f1a6ec
MM
13740 token = cp_lexer_peek_token (parser->lexer);
13741 /* Figure out whether or not there is an assignment-expression
13742 following the "throw" keyword. */
13743 if (token->type == CPP_COMMA
13744 || token->type == CPP_SEMICOLON
13745 || token->type == CPP_CLOSE_PAREN
13746 || token->type == CPP_CLOSE_SQUARE
13747 || token->type == CPP_CLOSE_BRACE
13748 || token->type == CPP_COLON)
a723baf1 13749 expression = NULL_TREE;
89f1a6ec
MM
13750 else
13751 expression = cp_parser_assignment_expression (parser);
a723baf1
MM
13752
13753 return build_throw (expression);
13754}
13755
13756/* GNU Extensions */
13757
13758/* Parse an (optional) asm-specification.
13759
13760 asm-specification:
13761 asm ( string-literal )
13762
13763 If the asm-specification is present, returns a STRING_CST
13764 corresponding to the string-literal. Otherwise, returns
13765 NULL_TREE. */
13766
13767static tree
94edc4ab 13768cp_parser_asm_specification_opt (cp_parser* parser)
a723baf1
MM
13769{
13770 cp_token *token;
13771 tree asm_specification;
13772
13773 /* Peek at the next token. */
13774 token = cp_lexer_peek_token (parser->lexer);
21526606 13775 /* If the next token isn't the `asm' keyword, then there's no
a723baf1
MM
13776 asm-specification. */
13777 if (!cp_parser_is_keyword (token, RID_ASM))
13778 return NULL_TREE;
13779
13780 /* Consume the `asm' token. */
13781 cp_lexer_consume_token (parser->lexer);
13782 /* Look for the `('. */
13783 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13784
13785 /* Look for the string-literal. */
c162c75e 13786 asm_specification = cp_parser_string_literal (parser, false, false);
a723baf1
MM
13787
13788 /* Look for the `)'. */
13789 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13790
13791 return asm_specification;
13792}
13793
21526606 13794/* Parse an asm-operand-list.
a723baf1
MM
13795
13796 asm-operand-list:
13797 asm-operand
13798 asm-operand-list , asm-operand
21526606 13799
a723baf1 13800 asm-operand:
21526606 13801 string-literal ( expression )
a723baf1
MM
13802 [ string-literal ] string-literal ( expression )
13803
13804 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13805 each node is the expression. The TREE_PURPOSE is itself a
13806 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13807 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13808 is a STRING_CST for the string literal before the parenthesis. */
13809
13810static tree
94edc4ab 13811cp_parser_asm_operand_list (cp_parser* parser)
a723baf1
MM
13812{
13813 tree asm_operands = NULL_TREE;
13814
13815 while (true)
13816 {
13817 tree string_literal;
13818 tree expression;
13819 tree name;
21526606 13820
21526606 13821 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
a723baf1
MM
13822 {
13823 /* Consume the `[' token. */
13824 cp_lexer_consume_token (parser->lexer);
13825 /* Read the operand name. */
13826 name = cp_parser_identifier (parser);
21526606 13827 if (name != error_mark_node)
a723baf1
MM
13828 name = build_string (IDENTIFIER_LENGTH (name),
13829 IDENTIFIER_POINTER (name));
13830 /* Look for the closing `]'. */
13831 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13832 }
13833 else
13834 name = NULL_TREE;
13835 /* Look for the string-literal. */
c162c75e
MA
13836 string_literal = cp_parser_string_literal (parser, false, false);
13837
a723baf1
MM
13838 /* Look for the `('. */
13839 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13840 /* Parse the expression. */
13841 expression = cp_parser_expression (parser);
13842 /* Look for the `)'. */
13843 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
c162c75e 13844
a723baf1
MM
13845 /* Add this operand to the list. */
13846 asm_operands = tree_cons (build_tree_list (name, string_literal),
21526606 13847 expression,
a723baf1 13848 asm_operands);
21526606 13849 /* If the next token is not a `,', there are no more
a723baf1
MM
13850 operands. */
13851 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13852 break;
13853 /* Consume the `,'. */
13854 cp_lexer_consume_token (parser->lexer);
13855 }
13856
13857 return nreverse (asm_operands);
13858}
13859
21526606 13860/* Parse an asm-clobber-list.
a723baf1
MM
13861
13862 asm-clobber-list:
13863 string-literal
21526606 13864 asm-clobber-list , string-literal
a723baf1
MM
13865
13866 Returns a TREE_LIST, indicating the clobbers in the order that they
13867 appeared. The TREE_VALUE of each node is a STRING_CST. */
13868
13869static tree
94edc4ab 13870cp_parser_asm_clobber_list (cp_parser* parser)
a723baf1
MM
13871{
13872 tree clobbers = NULL_TREE;
13873
13874 while (true)
13875 {
a723baf1
MM
13876 tree string_literal;
13877
13878 /* Look for the string literal. */
c162c75e 13879 string_literal = cp_parser_string_literal (parser, false, false);
a723baf1
MM
13880 /* Add it to the list. */
13881 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21526606 13882 /* If the next token is not a `,', then the list is
a723baf1
MM
13883 complete. */
13884 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13885 break;
13886 /* Consume the `,' token. */
13887 cp_lexer_consume_token (parser->lexer);
13888 }
13889
13890 return clobbers;
13891}
13892
13893/* Parse an (optional) series of attributes.
13894
13895 attributes:
13896 attributes attribute
13897
13898 attribute:
21526606 13899 __attribute__ (( attribute-list [opt] ))
a723baf1
MM
13900
13901 The return value is as for cp_parser_attribute_list. */
21526606 13902
a723baf1 13903static tree
94edc4ab 13904cp_parser_attributes_opt (cp_parser* parser)
a723baf1
MM
13905{
13906 tree attributes = NULL_TREE;
13907
13908 while (true)
13909 {
13910 cp_token *token;
13911 tree attribute_list;
13912
13913 /* Peek at the next token. */
13914 token = cp_lexer_peek_token (parser->lexer);
13915 /* If it's not `__attribute__', then we're done. */
13916 if (token->keyword != RID_ATTRIBUTE)
13917 break;
13918
13919 /* Consume the `__attribute__' keyword. */
13920 cp_lexer_consume_token (parser->lexer);
13921 /* Look for the two `(' tokens. */
13922 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13923 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13924
13925 /* Peek at the next token. */
13926 token = cp_lexer_peek_token (parser->lexer);
13927 if (token->type != CPP_CLOSE_PAREN)
13928 /* Parse the attribute-list. */
13929 attribute_list = cp_parser_attribute_list (parser);
13930 else
13931 /* If the next token is a `)', then there is no attribute
13932 list. */
13933 attribute_list = NULL;
13934
13935 /* Look for the two `)' tokens. */
13936 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13937 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13938
13939 /* Add these new attributes to the list. */
13940 attributes = chainon (attributes, attribute_list);
13941 }
13942
13943 return attributes;
13944}
13945
21526606 13946/* Parse an attribute-list.
a723baf1 13947
21526606
EC
13948 attribute-list:
13949 attribute
a723baf1
MM
13950 attribute-list , attribute
13951
13952 attribute:
21526606 13953 identifier
a723baf1
MM
13954 identifier ( identifier )
13955 identifier ( identifier , expression-list )
21526606 13956 identifier ( expression-list )
a723baf1
MM
13957
13958 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13959 TREE_PURPOSE of each node is the identifier indicating which
13960 attribute is in use. The TREE_VALUE represents the arguments, if
13961 any. */
13962
13963static tree
94edc4ab 13964cp_parser_attribute_list (cp_parser* parser)
a723baf1
MM
13965{
13966 tree attribute_list = NULL_TREE;
c162c75e 13967 bool save_translate_strings_p = parser->translate_strings_p;
a723baf1 13968
c162c75e 13969 parser->translate_strings_p = false;
a723baf1
MM
13970 while (true)
13971 {
13972 cp_token *token;
13973 tree identifier;
13974 tree attribute;
13975
13976 /* Look for the identifier. We also allow keywords here; for
13977 example `__attribute__ ((const))' is legal. */
13978 token = cp_lexer_peek_token (parser->lexer);
21526606 13979 if (token->type != CPP_NAME
a723baf1
MM
13980 && token->type != CPP_KEYWORD)
13981 return error_mark_node;
13982 /* Consume the token. */
13983 token = cp_lexer_consume_token (parser->lexer);
21526606 13984
a723baf1
MM
13985 /* Save away the identifier that indicates which attribute this is. */
13986 identifier = token->value;
13987 attribute = build_tree_list (identifier, NULL_TREE);
13988
13989 /* Peek at the next token. */
13990 token = cp_lexer_peek_token (parser->lexer);
13991 /* If it's an `(', then parse the attribute arguments. */
13992 if (token->type == CPP_OPEN_PAREN)
13993 {
13994 tree arguments;
a723baf1 13995
21526606 13996 arguments = (cp_parser_parenthesized_expression_list
39703eb9 13997 (parser, true, /*non_constant_p=*/NULL));
a723baf1
MM
13998 /* Save the identifier and arguments away. */
13999 TREE_VALUE (attribute) = arguments;
a723baf1
MM
14000 }
14001
14002 /* Add this attribute to the list. */
14003 TREE_CHAIN (attribute) = attribute_list;
14004 attribute_list = attribute;
14005
14006 /* Now, look for more attributes. */
14007 token = cp_lexer_peek_token (parser->lexer);
14008 /* If the next token isn't a `,', we're done. */
14009 if (token->type != CPP_COMMA)
14010 break;
14011
cd0be382 14012 /* Consume the comma and keep going. */
a723baf1
MM
14013 cp_lexer_consume_token (parser->lexer);
14014 }
c162c75e 14015 parser->translate_strings_p = save_translate_strings_p;
a723baf1
MM
14016
14017 /* We built up the list in reverse order. */
14018 return nreverse (attribute_list);
14019}
14020
14021/* Parse an optional `__extension__' keyword. Returns TRUE if it is
14022 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14023 current value of the PEDANTIC flag, regardless of whether or not
14024 the `__extension__' keyword is present. The caller is responsible
14025 for restoring the value of the PEDANTIC flag. */
14026
14027static bool
94edc4ab 14028cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
a723baf1
MM
14029{
14030 /* Save the old value of the PEDANTIC flag. */
14031 *saved_pedantic = pedantic;
14032
14033 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14034 {
14035 /* Consume the `__extension__' token. */
14036 cp_lexer_consume_token (parser->lexer);
14037 /* We're not being pedantic while the `__extension__' keyword is
14038 in effect. */
14039 pedantic = 0;
14040
14041 return true;
14042 }
14043
14044 return false;
14045}
14046
14047/* Parse a label declaration.
14048
14049 label-declaration:
14050 __label__ label-declarator-seq ;
14051
14052 label-declarator-seq:
14053 identifier , label-declarator-seq
14054 identifier */
14055
14056static void
94edc4ab 14057cp_parser_label_declaration (cp_parser* parser)
a723baf1
MM
14058{
14059 /* Look for the `__label__' keyword. */
14060 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14061
14062 while (true)
14063 {
14064 tree identifier;
14065
14066 /* Look for an identifier. */
14067 identifier = cp_parser_identifier (parser);
14068 /* Declare it as a lobel. */
14069 finish_label_decl (identifier);
14070 /* If the next token is a `;', stop. */
14071 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14072 break;
14073 /* Look for the `,' separating the label declarations. */
14074 cp_parser_require (parser, CPP_COMMA, "`,'");
14075 }
14076
14077 /* Look for the final `;'. */
14078 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14079}
14080
14081/* Support Functions */
14082
14083/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14084 NAME should have one of the representations used for an
14085 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14086 is returned. If PARSER->SCOPE is a dependent type, then a
14087 SCOPE_REF is returned.
14088
14089 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14090 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14091 was formed. Abstractly, such entities should not be passed to this
14092 function, because they do not need to be looked up, but it is
14093 simpler to check for this special case here, rather than at the
14094 call-sites.
14095
14096 In cases not explicitly covered above, this function returns a
14097 DECL, OVERLOAD, or baselink representing the result of the lookup.
14098 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14099 is returned.
14100
472c29c3 14101 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
fc6a28d7
MM
14102 (e.g., "struct") that was used. In that case bindings that do not
14103 refer to types are ignored.
a723baf1 14104
b0bc6e8e
KL
14105 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14106 ignored.
14107
eea9800f
MM
14108 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14109 are ignored.
14110
a723baf1 14111 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
8f78f01f
MM
14112 types.
14113
14114 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14115 results in an ambiguity, and false otherwise. */
a723baf1
MM
14116
14117static tree
21526606 14118cp_parser_lookup_name (cp_parser *parser, tree name,
fc6a28d7
MM
14119 enum tag_types tag_type,
14120 bool is_template, bool is_namespace,
8f78f01f
MM
14121 bool check_dependency,
14122 bool *ambiguous_p)
a723baf1
MM
14123{
14124 tree decl;
14125 tree object_type = parser->context->object_type;
14126
8f78f01f
MM
14127 /* Assume that the lookup will be unambiguous. */
14128 if (ambiguous_p)
14129 *ambiguous_p = false;
14130
a723baf1
MM
14131 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14132 no longer valid. Note that if we are parsing tentatively, and
14133 the parse fails, OBJECT_TYPE will be automatically restored. */
14134 parser->context->object_type = NULL_TREE;
14135
14136 if (name == error_mark_node)
14137 return error_mark_node;
14138
14139 /* A template-id has already been resolved; there is no lookup to
14140 do. */
14141 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14142 return name;
14143 if (BASELINK_P (name))
14144 {
50bc768d
NS
14145 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14146 == TEMPLATE_ID_EXPR);
a723baf1
MM
14147 return name;
14148 }
14149
14150 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14151 it should already have been checked to make sure that the name
14152 used matches the type being destroyed. */
14153 if (TREE_CODE (name) == BIT_NOT_EXPR)
14154 {
14155 tree type;
14156
14157 /* Figure out to which type this destructor applies. */
14158 if (parser->scope)
14159 type = parser->scope;
14160 else if (object_type)
14161 type = object_type;
14162 else
14163 type = current_class_type;
14164 /* If that's not a class type, there is no destructor. */
14165 if (!type || !CLASS_TYPE_P (type))
14166 return error_mark_node;
fd6e3cce
GB
14167 if (!CLASSTYPE_DESTRUCTORS (type))
14168 return error_mark_node;
a723baf1
MM
14169 /* If it was a class type, return the destructor. */
14170 return CLASSTYPE_DESTRUCTORS (type);
14171 }
14172
14173 /* By this point, the NAME should be an ordinary identifier. If
14174 the id-expression was a qualified name, the qualifying scope is
14175 stored in PARSER->SCOPE at this point. */
50bc768d 14176 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
21526606 14177
a723baf1
MM
14178 /* Perform the lookup. */
14179 if (parser->scope)
21526606 14180 {
1fb3244a 14181 bool dependent_p;
a723baf1
MM
14182
14183 if (parser->scope == error_mark_node)
14184 return error_mark_node;
14185
14186 /* If the SCOPE is dependent, the lookup must be deferred until
14187 the template is instantiated -- unless we are explicitly
14188 looking up names in uninstantiated templates. Even then, we
14189 cannot look up the name if the scope is not a class type; it
14190 might, for example, be a template type parameter. */
1fb3244a
MM
14191 dependent_p = (TYPE_P (parser->scope)
14192 && !(parser->in_declarator_p
14193 && currently_open_class (parser->scope))
14194 && dependent_type_p (parser->scope));
a723baf1 14195 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
1fb3244a 14196 && dependent_p)
a723baf1 14197 {
fc6a28d7
MM
14198 if (tag_type)
14199 {
14200 tree type;
14201
14202 /* The resolution to Core Issue 180 says that `struct
14203 A::B' should be considered a type-name, even if `A'
14204 is dependent. */
14205 type = make_typename_type (parser->scope, name, tag_type,
14206 /*complain=*/1);
fc6a28d7
MM
14207 decl = TYPE_NAME (type);
14208 }
b0bc6e8e 14209 else if (is_template)
5b4acce1 14210 decl = make_unbound_class_template (parser->scope,
b939a023 14211 name, NULL_TREE,
5b4acce1 14212 /*complain=*/1);
b0bc6e8e
KL
14213 else
14214 decl = build_nt (SCOPE_REF, parser->scope, name);
a723baf1
MM
14215 }
14216 else
14217 {
4514aa8c 14218 tree pushed_scope = NULL_TREE;
91b004e5 14219
a723baf1
MM
14220 /* If PARSER->SCOPE is a dependent type, then it must be a
14221 class type, and we must not be checking dependencies;
14222 otherwise, we would have processed this lookup above. So
14223 that PARSER->SCOPE is not considered a dependent base by
14224 lookup_member, we must enter the scope here. */
1fb3244a 14225 if (dependent_p)
4514aa8c 14226 pushed_scope = push_scope (parser->scope);
a723baf1
MM
14227 /* If the PARSER->SCOPE is a a template specialization, it
14228 may be instantiated during name lookup. In that case,
14229 errors may be issued. Even if we rollback the current
14230 tentative parse, those errors are valid. */
fc6a28d7
MM
14231 decl = lookup_qualified_name (parser->scope, name,
14232 tag_type != none_type,
5e08432e 14233 /*complain=*/true);
4514aa8c
NS
14234 if (pushed_scope)
14235 pop_scope (pushed_scope);
a723baf1
MM
14236 }
14237 parser->qualifying_scope = parser->scope;
14238 parser->object_scope = NULL_TREE;
14239 }
14240 else if (object_type)
14241 {
14242 tree object_decl = NULL_TREE;
14243 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14244 OBJECT_TYPE is not a class. */
14245 if (CLASS_TYPE_P (object_type))
14246 /* If the OBJECT_TYPE is a template specialization, it may
14247 be instantiated during name lookup. In that case, errors
14248 may be issued. Even if we rollback the current tentative
14249 parse, those errors are valid. */
14250 object_decl = lookup_member (object_type,
14251 name,
fc6a28d7
MM
14252 /*protect=*/0,
14253 tag_type != none_type);
a723baf1 14254 /* Look it up in the enclosing context, too. */
fc6a28d7
MM
14255 decl = lookup_name_real (name, tag_type != none_type,
14256 /*nonclass=*/0,
12cf89fa 14257 /*block_p=*/true, is_namespace,
a723baf1
MM
14258 /*flags=*/0);
14259 parser->object_scope = object_type;
14260 parser->qualifying_scope = NULL_TREE;
14261 if (object_decl)
14262 decl = object_decl;
14263 }
14264 else
14265 {
fc6a28d7
MM
14266 decl = lookup_name_real (name, tag_type != none_type,
14267 /*nonclass=*/0,
12cf89fa 14268 /*block_p=*/true, is_namespace,
a723baf1
MM
14269 /*flags=*/0);
14270 parser->qualifying_scope = NULL_TREE;
14271 parser->object_scope = NULL_TREE;
14272 }
14273
14274 /* If the lookup failed, let our caller know. */
21526606 14275 if (!decl
a723baf1 14276 || decl == error_mark_node
21526606 14277 || (TREE_CODE (decl) == FUNCTION_DECL
a723baf1
MM
14278 && DECL_ANTICIPATED (decl)))
14279 return error_mark_node;
14280
14281 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14282 if (TREE_CODE (decl) == TREE_LIST)
14283 {
8f78f01f
MM
14284 if (ambiguous_p)
14285 *ambiguous_p = true;
a723baf1
MM
14286 /* The error message we have to print is too complicated for
14287 cp_parser_error, so we incorporate its actions directly. */
e5976695 14288 if (!cp_parser_simulate_error (parser))
a723baf1 14289 {
2a13a625 14290 error ("reference to %qD is ambiguous", name);
a723baf1
MM
14291 print_candidates (decl);
14292 }
14293 return error_mark_node;
14294 }
14295
50bc768d
NS
14296 gcc_assert (DECL_P (decl)
14297 || TREE_CODE (decl) == OVERLOAD
14298 || TREE_CODE (decl) == SCOPE_REF
14299 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14300 || BASELINK_P (decl));
a723baf1
MM
14301
14302 /* If we have resolved the name of a member declaration, check to
14303 see if the declaration is accessible. When the name resolves to
34cd5ae7 14304 set of overloaded functions, accessibility is checked when
21526606 14305 overload resolution is done.
a723baf1
MM
14306
14307 During an explicit instantiation, access is not checked at all,
14308 as per [temp.explicit]. */
8d241e0b 14309 if (DECL_P (decl))
ee76b931 14310 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
a723baf1
MM
14311
14312 return decl;
14313}
14314
14315/* Like cp_parser_lookup_name, but for use in the typical case where
b0bc6e8e
KL
14316 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14317 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
a723baf1
MM
14318
14319static tree
94edc4ab 14320cp_parser_lookup_name_simple (cp_parser* parser, tree name)
a723baf1 14321{
21526606 14322 return cp_parser_lookup_name (parser, name,
fc6a28d7 14323 none_type,
b0bc6e8e 14324 /*is_template=*/false,
eea9800f 14325 /*is_namespace=*/false,
8f78f01f
MM
14326 /*check_dependency=*/true,
14327 /*ambiguous_p=*/NULL);
a723baf1
MM
14328}
14329
a723baf1
MM
14330/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14331 the current context, return the TYPE_DECL. If TAG_NAME_P is
14332 true, the DECL indicates the class being defined in a class-head,
14333 or declared in an elaborated-type-specifier.
14334
14335 Otherwise, return DECL. */
14336
14337static tree
14338cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14339{
710b73e6
KL
14340 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14341 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
a723baf1 14342
21526606 14343 struct A {
a723baf1
MM
14344 template <typename T> struct B;
14345 };
14346
21526606
EC
14347 template <typename T> struct A::B {};
14348
a723baf1
MM
14349 Similarly, in a elaborated-type-specifier:
14350
14351 namespace N { struct X{}; }
14352
14353 struct A {
14354 template <typename T> friend struct N::X;
14355 };
14356
710b73e6
KL
14357 However, if the DECL refers to a class type, and we are in
14358 the scope of the class, then the name lookup automatically
14359 finds the TYPE_DECL created by build_self_reference rather
14360 than a TEMPLATE_DECL. For example, in:
14361
14362 template <class T> struct S {
14363 S s;
14364 };
14365
14366 there is no need to handle such case. */
14367
14368 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
a723baf1
MM
14369 return DECL_TEMPLATE_RESULT (decl);
14370
14371 return decl;
14372}
14373
14374/* If too many, or too few, template-parameter lists apply to the
14375 declarator, issue an error message. Returns TRUE if all went well,
14376 and FALSE otherwise. */
14377
14378static bool
21526606 14379cp_parser_check_declarator_template_parameters (cp_parser* parser,
058b15c1 14380 cp_declarator *declarator)
a723baf1
MM
14381{
14382 unsigned num_templates;
14383
14384 /* We haven't seen any classes that involve template parameters yet. */
14385 num_templates = 0;
14386
058b15c1 14387 switch (declarator->kind)
a723baf1 14388 {
058b15c1 14389 case cdk_id:
1d786913 14390 if (declarator->u.id.qualifying_scope)
058b15c1
MM
14391 {
14392 tree scope;
14393 tree member;
a723baf1 14394
1d786913
MM
14395 scope = declarator->u.id.qualifying_scope;
14396 member = declarator->u.id.unqualified_name;
a723baf1 14397
058b15c1
MM
14398 while (scope && CLASS_TYPE_P (scope))
14399 {
14400 /* You're supposed to have one `template <...>'
14401 for every template class, but you don't need one
14402 for a full specialization. For example:
14403
14404 template <class T> struct S{};
14405 template <> struct S<int> { void f(); };
14406 void S<int>::f () {}
14407
14408 is correct; there shouldn't be a `template <>' for
14409 the definition of `S<int>::f'. */
14410 if (CLASSTYPE_TEMPLATE_INFO (scope)
14411 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14412 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14413 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14414 ++num_templates;
14415
14416 scope = TYPE_CONTEXT (scope);
14417 }
14418 }
1d786913
MM
14419 else if (TREE_CODE (declarator->u.id.unqualified_name)
14420 == TEMPLATE_ID_EXPR)
14421 /* If the DECLARATOR has the form `X<y>' then it uses one
14422 additional level of template parameters. */
a723baf1
MM
14423 ++num_templates;
14424
21526606 14425 return cp_parser_check_template_parameters (parser,
a723baf1 14426 num_templates);
058b15c1
MM
14427
14428 case cdk_function:
14429 case cdk_array:
14430 case cdk_pointer:
14431 case cdk_reference:
14432 case cdk_ptrmem:
98ca843c 14433 return (cp_parser_check_declarator_template_parameters
058b15c1
MM
14434 (parser, declarator->declarator));
14435
14436 case cdk_error:
14437 return true;
14438
14439 default:
315fb5db 14440 gcc_unreachable ();
a723baf1 14441 }
315fb5db 14442 return false;
a723baf1
MM
14443}
14444
14445/* NUM_TEMPLATES were used in the current declaration. If that is
14446 invalid, return FALSE and issue an error messages. Otherwise,
14447 return TRUE. */
14448
14449static bool
94edc4ab
NN
14450cp_parser_check_template_parameters (cp_parser* parser,
14451 unsigned num_templates)
a723baf1
MM
14452{
14453 /* If there are more template classes than parameter lists, we have
14454 something like:
21526606 14455
a723baf1
MM
14456 template <class T> void S<T>::R<T>::f (); */
14457 if (parser->num_template_parameter_lists < num_templates)
14458 {
14459 error ("too few template-parameter-lists");
14460 return false;
14461 }
14462 /* If there are the same number of template classes and parameter
14463 lists, that's OK. */
14464 if (parser->num_template_parameter_lists == num_templates)
14465 return true;
14466 /* If there are more, but only one more, then we are referring to a
14467 member template. That's OK too. */
14468 if (parser->num_template_parameter_lists == num_templates + 1)
14469 return true;
14470 /* Otherwise, there are too many template parameter lists. We have
14471 something like:
14472
14473 template <class T> template <class U> void S::f(); */
14474 error ("too many template-parameter-lists");
14475 return false;
14476}
14477
a723baf1
MM
14478/* Parse an optional `::' token indicating that the following name is
14479 from the global namespace. If so, PARSER->SCOPE is set to the
14480 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14481 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14482 Returns the new value of PARSER->SCOPE, if the `::' token is
14483 present, and NULL_TREE otherwise. */
14484
14485static tree
94edc4ab 14486cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
a723baf1
MM
14487{
14488 cp_token *token;
14489
14490 /* Peek at the next token. */
14491 token = cp_lexer_peek_token (parser->lexer);
14492 /* If we're looking at a `::' token then we're starting from the
14493 global namespace, not our current location. */
14494 if (token->type == CPP_SCOPE)
14495 {
14496 /* Consume the `::' token. */
14497 cp_lexer_consume_token (parser->lexer);
14498 /* Set the SCOPE so that we know where to start the lookup. */
14499 parser->scope = global_namespace;
14500 parser->qualifying_scope = global_namespace;
14501 parser->object_scope = NULL_TREE;
14502
14503 return parser->scope;
14504 }
14505 else if (!current_scope_valid_p)
14506 {
14507 parser->scope = NULL_TREE;
14508 parser->qualifying_scope = NULL_TREE;
14509 parser->object_scope = NULL_TREE;
14510 }
14511
14512 return NULL_TREE;
14513}
14514
14515/* Returns TRUE if the upcoming token sequence is the start of a
14516 constructor declarator. If FRIEND_P is true, the declarator is
14517 preceded by the `friend' specifier. */
14518
14519static bool
14520cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14521{
14522 bool constructor_p;
14523 tree type_decl = NULL_TREE;
14524 bool nested_name_p;
2050a1bb
MM
14525 cp_token *next_token;
14526
14527 /* The common case is that this is not a constructor declarator, so
8fbc5ae7
MM
14528 try to avoid doing lots of work if at all possible. It's not
14529 valid declare a constructor at function scope. */
14530 if (at_function_scope_p ())
14531 return false;
14532 /* And only certain tokens can begin a constructor declarator. */
2050a1bb
MM
14533 next_token = cp_lexer_peek_token (parser->lexer);
14534 if (next_token->type != CPP_NAME
14535 && next_token->type != CPP_SCOPE
14536 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14537 && next_token->type != CPP_TEMPLATE_ID)
14538 return false;
a723baf1
MM
14539
14540 /* Parse tentatively; we are going to roll back all of the tokens
14541 consumed here. */
14542 cp_parser_parse_tentatively (parser);
14543 /* Assume that we are looking at a constructor declarator. */
14544 constructor_p = true;
8d241e0b 14545
a723baf1
MM
14546 /* Look for the optional `::' operator. */
14547 cp_parser_global_scope_opt (parser,
14548 /*current_scope_valid_p=*/false);
14549 /* Look for the nested-name-specifier. */
21526606 14550 nested_name_p
a723baf1
MM
14551 = (cp_parser_nested_name_specifier_opt (parser,
14552 /*typename_keyword_p=*/false,
14553 /*check_dependency_p=*/false,
a668c6ad
MM
14554 /*type_p=*/false,
14555 /*is_declaration=*/false)
a723baf1
MM
14556 != NULL_TREE);
14557 /* Outside of a class-specifier, there must be a
14558 nested-name-specifier. */
21526606 14559 if (!nested_name_p &&
a723baf1
MM
14560 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14561 || friend_p))
14562 constructor_p = false;
14563 /* If we still think that this might be a constructor-declarator,
14564 look for a class-name. */
14565 if (constructor_p)
14566 {
14567 /* If we have:
14568
8fbc5ae7 14569 template <typename T> struct S { S(); };
a723baf1
MM
14570 template <typename T> S<T>::S ();
14571
14572 we must recognize that the nested `S' names a class.
14573 Similarly, for:
14574
14575 template <typename T> S<T>::S<T> ();
14576
14577 we must recognize that the nested `S' names a template. */
14578 type_decl = cp_parser_class_name (parser,
14579 /*typename_keyword_p=*/false,
14580 /*template_keyword_p=*/false,
fc6a28d7 14581 none_type,
a723baf1 14582 /*check_dependency_p=*/false,
a668c6ad
MM
14583 /*class_head_p=*/false,
14584 /*is_declaration=*/false);
a723baf1
MM
14585 /* If there was no class-name, then this is not a constructor. */
14586 constructor_p = !cp_parser_error_occurred (parser);
14587 }
8d241e0b 14588
a723baf1
MM
14589 /* If we're still considering a constructor, we have to see a `(',
14590 to begin the parameter-declaration-clause, followed by either a
14591 `)', an `...', or a decl-specifier. We need to check for a
14592 type-specifier to avoid being fooled into thinking that:
14593
14594 S::S (f) (int);
14595
14596 is a constructor. (It is actually a function named `f' that
14597 takes one parameter (of type `int') and returns a value of type
14598 `S::S'. */
21526606 14599 if (constructor_p
a723baf1
MM
14600 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14601 {
14602 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14603 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15077df5
MM
14604 /* A parameter declaration begins with a decl-specifier,
14605 which is either the "attribute" keyword, a storage class
14606 specifier, or (usually) a type-specifier. */
14607 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
a723baf1
MM
14608 && !cp_parser_storage_class_specifier_opt (parser))
14609 {
5dae1114 14610 tree type;
4514aa8c 14611 tree pushed_scope = NULL_TREE;
4047b164 14612 unsigned saved_num_template_parameter_lists;
5dae1114
MM
14613
14614 /* Names appearing in the type-specifier should be looked up
14615 in the scope of the class. */
14616 if (current_class_type)
14617 type = NULL_TREE;
a723baf1
MM
14618 else
14619 {
5dae1114
MM
14620 type = TREE_TYPE (type_decl);
14621 if (TREE_CODE (type) == TYPENAME_TYPE)
14d22dd6 14622 {
21526606 14623 type = resolve_typename_type (type,
14d22dd6
MM
14624 /*only_current_p=*/false);
14625 if (type == error_mark_node)
14626 {
14627 cp_parser_abort_tentative_parse (parser);
14628 return false;
14629 }
14630 }
4514aa8c 14631 pushed_scope = push_scope (type);
a723baf1 14632 }
4047b164
KL
14633
14634 /* Inside the constructor parameter list, surrounding
14635 template-parameter-lists do not apply. */
14636 saved_num_template_parameter_lists
14637 = parser->num_template_parameter_lists;
14638 parser->num_template_parameter_lists = 0;
14639
5dae1114
MM
14640 /* Look for the type-specifier. */
14641 cp_parser_type_specifier (parser,
14642 CP_PARSER_FLAGS_NONE,
62d1db17 14643 /*decl_specs=*/NULL,
5dae1114
MM
14644 /*is_declarator=*/true,
14645 /*declares_class_or_enum=*/NULL,
14646 /*is_cv_qualifier=*/NULL);
4047b164
KL
14647
14648 parser->num_template_parameter_lists
14649 = saved_num_template_parameter_lists;
14650
5dae1114 14651 /* Leave the scope of the class. */
4514aa8c
NS
14652 if (pushed_scope)
14653 pop_scope (pushed_scope);
5dae1114
MM
14654
14655 constructor_p = !cp_parser_error_occurred (parser);
a723baf1
MM
14656 }
14657 }
14658 else
14659 constructor_p = false;
14660 /* We did not really want to consume any tokens. */
14661 cp_parser_abort_tentative_parse (parser);
14662
14663 return constructor_p;
14664}
14665
14666/* Parse the definition of the function given by the DECL_SPECIFIERS,
cf22909c 14667 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
a723baf1
MM
14668 they must be performed once we are in the scope of the function.
14669
14670 Returns the function defined. */
14671
14672static tree
14673cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab 14674 (cp_parser* parser,
62d1db17 14675 cp_decl_specifier_seq *decl_specifiers,
94edc4ab 14676 tree attributes,
058b15c1 14677 const cp_declarator *declarator)
a723baf1
MM
14678{
14679 tree fn;
14680 bool success_p;
14681
14682 /* Begin the function-definition. */
058b15c1
MM
14683 success_p = start_function (decl_specifiers, declarator, attributes);
14684
14685 /* The things we're about to see are not directly qualified by any
14686 template headers we've seen thus far. */
14687 reset_specialization ();
a723baf1
MM
14688
14689 /* If there were names looked up in the decl-specifier-seq that we
14690 did not check, check them now. We must wait until we are in the
14691 scope of the function to perform the checks, since the function
14692 might be a friend. */
cf22909c 14693 perform_deferred_access_checks ();
a723baf1
MM
14694
14695 if (!success_p)
14696 {
058b15c1 14697 /* Skip the entire function. */
a723baf1
MM
14698 error ("invalid function declaration");
14699 cp_parser_skip_to_end_of_block_or_statement (parser);
14700 fn = error_mark_node;
14701 }
14702 else
14703 fn = cp_parser_function_definition_after_declarator (parser,
14704 /*inline_p=*/false);
14705
14706 return fn;
14707}
14708
14709/* Parse the part of a function-definition that follows the
14710 declarator. INLINE_P is TRUE iff this function is an inline
14711 function defined with a class-specifier.
14712
14713 Returns the function defined. */
14714
21526606
EC
14715static tree
14716cp_parser_function_definition_after_declarator (cp_parser* parser,
94edc4ab 14717 bool inline_p)
a723baf1
MM
14718{
14719 tree fn;
14720 bool ctor_initializer_p = false;
14721 bool saved_in_unbraced_linkage_specification_p;
14722 unsigned saved_num_template_parameter_lists;
14723
14724 /* If the next token is `return', then the code may be trying to
14725 make use of the "named return value" extension that G++ used to
14726 support. */
14727 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14728 {
14729 /* Consume the `return' keyword. */
14730 cp_lexer_consume_token (parser->lexer);
14731 /* Look for the identifier that indicates what value is to be
14732 returned. */
14733 cp_parser_identifier (parser);
14734 /* Issue an error message. */
14735 error ("named return values are no longer supported");
14736 /* Skip tokens until we reach the start of the function body. */
21eb631b
MM
14737 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14738 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
a723baf1
MM
14739 cp_lexer_consume_token (parser->lexer);
14740 }
14741 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14742 anything declared inside `f'. */
21526606 14743 saved_in_unbraced_linkage_specification_p
a723baf1
MM
14744 = parser->in_unbraced_linkage_specification_p;
14745 parser->in_unbraced_linkage_specification_p = false;
14746 /* Inside the function, surrounding template-parameter-lists do not
14747 apply. */
21526606
EC
14748 saved_num_template_parameter_lists
14749 = parser->num_template_parameter_lists;
a723baf1
MM
14750 parser->num_template_parameter_lists = 0;
14751 /* If the next token is `try', then we are looking at a
14752 function-try-block. */
14753 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14754 ctor_initializer_p = cp_parser_function_try_block (parser);
14755 /* A function-try-block includes the function-body, so we only do
14756 this next part if we're not processing a function-try-block. */
14757 else
21526606 14758 ctor_initializer_p
a723baf1
MM
14759 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14760
14761 /* Finish the function. */
21526606 14762 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
a723baf1
MM
14763 (inline_p ? 2 : 0));
14764 /* Generate code for it, if necessary. */
8cd2462c 14765 expand_or_defer_fn (fn);
a723baf1 14766 /* Restore the saved values. */
21526606 14767 parser->in_unbraced_linkage_specification_p
a723baf1 14768 = saved_in_unbraced_linkage_specification_p;
21526606 14769 parser->num_template_parameter_lists
a723baf1
MM
14770 = saved_num_template_parameter_lists;
14771
14772 return fn;
14773}
14774
14775/* Parse a template-declaration, assuming that the `export' (and
14776 `extern') keywords, if present, has already been scanned. MEMBER_P
14777 is as for cp_parser_template_declaration. */
14778
14779static void
94edc4ab 14780cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
a723baf1
MM
14781{
14782 tree decl = NULL_TREE;
14783 tree parameter_list;
14784 bool friend_p = false;
14785
14786 /* Look for the `template' keyword. */
14787 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14788 return;
21526606 14789
a723baf1
MM
14790 /* And the `<'. */
14791 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14792 return;
21526606 14793
a723baf1
MM
14794 /* If the next token is `>', then we have an invalid
14795 specialization. Rather than complain about an invalid template
14796 parameter, issue an error message here. */
14797 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14798 {
14799 cp_parser_error (parser, "invalid explicit specialization");
2f9afd51 14800 begin_specialization ();
a723baf1
MM
14801 parameter_list = NULL_TREE;
14802 }
14803 else
2f9afd51
KL
14804 {
14805 /* Parse the template parameters. */
14806 begin_template_parm_list ();
14807 parameter_list = cp_parser_template_parameter_list (parser);
14808 parameter_list = end_template_parm_list (parameter_list);
14809 }
14810
a723baf1
MM
14811 /* Look for the `>'. */
14812 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14813 /* We just processed one more parameter list. */
14814 ++parser->num_template_parameter_lists;
14815 /* If the next token is `template', there are more template
14816 parameters. */
21526606 14817 if (cp_lexer_next_token_is_keyword (parser->lexer,
a723baf1
MM
14818 RID_TEMPLATE))
14819 cp_parser_template_declaration_after_export (parser, member_p);
14820 else
14821 {
fe88415f
NS
14822 /* There are no access checks when parsing a template, as we do not
14823 know if a specialization will be a friend. */
14824 push_deferring_access_checks (dk_no_check);
98ca843c 14825
a723baf1
MM
14826 decl = cp_parser_single_declaration (parser,
14827 member_p,
14828 &friend_p);
14829
fe88415f 14830 pop_deferring_access_checks ();
98ca843c 14831
a723baf1
MM
14832 /* If this is a member template declaration, let the front
14833 end know. */
14834 if (member_p && !friend_p && decl)
37d407a1
KL
14835 {
14836 if (TREE_CODE (decl) == TYPE_DECL)
14837 cp_parser_check_access_in_redeclaration (decl);
14838
14839 decl = finish_member_template_decl (decl);
14840 }
a723baf1 14841 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19db77ce
KL
14842 make_friend_class (current_class_type, TREE_TYPE (decl),
14843 /*complain=*/true);
a723baf1
MM
14844 }
14845 /* We are done with the current parameter list. */
14846 --parser->num_template_parameter_lists;
14847
14848 /* Finish up. */
14849 finish_template_decl (parameter_list);
14850
14851 /* Register member declarations. */
14852 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14853 finish_member_declaration (decl);
14854
14855 /* If DECL is a function template, we must return to parse it later.
14856 (Even though there is no definition, there might be default
14857 arguments that need handling.) */
21526606 14858 if (member_p && decl
a723baf1
MM
14859 && (TREE_CODE (decl) == FUNCTION_DECL
14860 || DECL_FUNCTION_TEMPLATE_P (decl)))
14861 TREE_VALUE (parser->unparsed_functions_queues)
21526606 14862 = tree_cons (NULL_TREE, decl,
a723baf1
MM
14863 TREE_VALUE (parser->unparsed_functions_queues));
14864}
14865
14866/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14867 `function-definition' sequence. MEMBER_P is true, this declaration
14868 appears in a class scope.
14869
14870 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14871 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14872
14873static tree
21526606 14874cp_parser_single_declaration (cp_parser* parser,
94edc4ab
NN
14875 bool member_p,
14876 bool* friend_p)
a723baf1 14877{
560ad596 14878 int declares_class_or_enum;
a723baf1 14879 tree decl = NULL_TREE;
62d1db17 14880 cp_decl_specifier_seq decl_specifiers;
4bb8ca28 14881 bool function_definition_p = false;
a723baf1 14882
71bd7186
MM
14883 /* This function is only used when processing a template
14884 declaration. */
14885 gcc_assert (innermost_scope_kind () == sk_template_parms
14886 || innermost_scope_kind () == sk_template_spec);
14887
a723baf1 14888 /* Defer access checks until we know what is being declared. */
8d241e0b 14889 push_deferring_access_checks (dk_deferred);
cf22909c 14890
a723baf1
MM
14891 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14892 alternative. */
62d1db17
MM
14893 cp_parser_decl_specifier_seq (parser,
14894 CP_PARSER_FLAGS_OPTIONAL,
14895 &decl_specifiers,
14896 &declares_class_or_enum);
4bb8ca28 14897 if (friend_p)
62d1db17 14898 *friend_p = cp_parser_friend_p (&decl_specifiers);
71bd7186
MM
14899
14900 /* There are no template typedefs. */
14901 if (decl_specifiers.specs[(int) ds_typedef])
14902 {
14903 error ("template declaration of %qs", "typedef");
14904 decl = error_mark_node;
14905 }
14906
a723baf1
MM
14907 /* Gather up the access checks that occurred the
14908 decl-specifier-seq. */
cf22909c
KL
14909 stop_deferring_access_checks ();
14910
a723baf1
MM
14911 /* Check for the declaration of a template class. */
14912 if (declares_class_or_enum)
14913 {
14914 if (cp_parser_declares_only_class_p (parser))
14915 {
62d1db17 14916 decl = shadow_tag (&decl_specifiers);
b939a023
KL
14917
14918 /* In this case:
14919
14920 struct C {
14921 friend template <typename T> struct A<T>::B;
14922 };
14923
14924 A<T>::B will be represented by a TYPENAME_TYPE, and
14925 therefore not recognized by shadow_tag. */
14926 if (friend_p && *friend_p
14927 && !decl
14928 && decl_specifiers.type
14929 && TYPE_P (decl_specifiers.type))
14930 decl = decl_specifiers.type;
14931
62d1db17 14932 if (decl && decl != error_mark_node)
a723baf1
MM
14933 decl = TYPE_NAME (decl);
14934 else
14935 decl = error_mark_node;
14936 }
14937 }
a723baf1
MM
14938 /* If it's not a template class, try for a template function. If
14939 the next token is a `;', then this declaration does not declare
14940 anything. But, if there were errors in the decl-specifiers, then
14941 the error might well have come from an attempted class-specifier.
14942 In that case, there's no need to warn about a missing declarator. */
14943 if (!decl
14944 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
62d1db17 14945 || decl_specifiers.type != error_mark_node))
21526606 14946 decl = cp_parser_init_declarator (parser,
62d1db17 14947 &decl_specifiers,
4bb8ca28 14948 /*function_definition_allowed_p=*/true,
a723baf1 14949 member_p,
560ad596 14950 declares_class_or_enum,
4bb8ca28 14951 &function_definition_p);
cf22909c
KL
14952
14953 pop_deferring_access_checks ();
14954
a723baf1
MM
14955 /* Clear any current qualification; whatever comes next is the start
14956 of something new. */
14957 parser->scope = NULL_TREE;
14958 parser->qualifying_scope = NULL_TREE;
14959 parser->object_scope = NULL_TREE;
14960 /* Look for a trailing `;' after the declaration. */
4bb8ca28 14961 if (!function_definition_p
71bd7186
MM
14962 && (decl == error_mark_node
14963 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
a723baf1 14964 cp_parser_skip_to_end_of_block_or_statement (parser);
a723baf1
MM
14965
14966 return decl;
14967}
14968
d6b4ea85
MM
14969/* Parse a cast-expression that is not the operand of a unary "&". */
14970
14971static tree
14972cp_parser_simple_cast_expression (cp_parser *parser)
14973{
14974 return cp_parser_cast_expression (parser, /*address_p=*/false);
14975}
14976
a723baf1
MM
14977/* Parse a functional cast to TYPE. Returns an expression
14978 representing the cast. */
14979
14980static tree
94edc4ab 14981cp_parser_functional_cast (cp_parser* parser, tree type)
a723baf1
MM
14982{
14983 tree expression_list;
d36d5600 14984 tree cast;
a723baf1 14985
21526606 14986 expression_list
39703eb9
MM
14987 = cp_parser_parenthesized_expression_list (parser, false,
14988 /*non_constant_p=*/NULL);
a723baf1 14989
d36d5600
GB
14990 cast = build_functional_cast (type, expression_list);
14991 /* [expr.const]/1: In an integral constant expression "only type
14992 conversions to integral or enumeration type can be used". */
98ca843c 14993 if (cast != error_mark_node && !type_dependent_expression_p (type)
d36d5600
GB
14994 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14995 {
98ca843c 14996 if (cp_parser_non_integral_constant_expression
d36d5600
GB
14997 (parser, "a call to a constructor"))
14998 return error_mark_node;
14999 }
15000 return cast;
a723baf1
MM
15001}
15002
4bb8ca28
MM
15003/* Save the tokens that make up the body of a member function defined
15004 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15005 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15006 specifiers applied to the declaration. Returns the FUNCTION_DECL
15007 for the member function. */
15008
7ce27103 15009static tree
4bb8ca28 15010cp_parser_save_member_function_body (cp_parser* parser,
62d1db17 15011 cp_decl_specifier_seq *decl_specifiers,
058b15c1 15012 cp_declarator *declarator,
4bb8ca28
MM
15013 tree attributes)
15014{
c162c75e
MA
15015 cp_token *first;
15016 cp_token *last;
4bb8ca28
MM
15017 tree fn;
15018
15019 /* Create the function-declaration. */
15020 fn = start_method (decl_specifiers, declarator, attributes);
15021 /* If something went badly wrong, bail out now. */
15022 if (fn == error_mark_node)
15023 {
15024 /* If there's a function-body, skip it. */
21526606 15025 if (cp_parser_token_starts_function_definition_p
4bb8ca28
MM
15026 (cp_lexer_peek_token (parser->lexer)))
15027 cp_parser_skip_to_end_of_block_or_statement (parser);
15028 return error_mark_node;
15029 }
15030
15031 /* Remember it, if there default args to post process. */
15032 cp_parser_save_default_args (parser, fn);
15033
21526606 15034 /* Save away the tokens that make up the body of the
4bb8ca28 15035 function. */
c162c75e
MA
15036 first = parser->lexer->next_token;
15037 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
4bb8ca28
MM
15038 /* Handle function try blocks. */
15039 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
c162c75e
MA
15040 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15041 last = parser->lexer->next_token;
4bb8ca28
MM
15042
15043 /* Save away the inline definition; we will process it when the
15044 class is complete. */
c162c75e 15045 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
4bb8ca28
MM
15046 DECL_PENDING_INLINE_P (fn) = 1;
15047
15048 /* We need to know that this was defined in the class, so that
15049 friend templates are handled correctly. */
15050 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15051
15052 /* We're done with the inline definition. */
15053 finish_method (fn);
15054
15055 /* Add FN to the queue of functions to be parsed later. */
15056 TREE_VALUE (parser->unparsed_functions_queues)
21526606 15057 = tree_cons (NULL_TREE, fn,
4bb8ca28
MM
15058 TREE_VALUE (parser->unparsed_functions_queues));
15059
15060 return fn;
15061}
15062
ec75414f
MM
15063/* Parse a template-argument-list, as well as the trailing ">" (but
15064 not the opening ">"). See cp_parser_template_argument_list for the
15065 return value. */
15066
15067static tree
15068cp_parser_enclosed_template_argument_list (cp_parser* parser)
15069{
15070 tree arguments;
15071 tree saved_scope;
15072 tree saved_qualifying_scope;
15073 tree saved_object_scope;
15074 bool saved_greater_than_is_operator_p;
15075
15076 /* [temp.names]
15077
15078 When parsing a template-id, the first non-nested `>' is taken as
15079 the end of the template-argument-list rather than a greater-than
15080 operator. */
21526606 15081 saved_greater_than_is_operator_p
ec75414f
MM
15082 = parser->greater_than_is_operator_p;
15083 parser->greater_than_is_operator_p = false;
15084 /* Parsing the argument list may modify SCOPE, so we save it
15085 here. */
15086 saved_scope = parser->scope;
15087 saved_qualifying_scope = parser->qualifying_scope;
15088 saved_object_scope = parser->object_scope;
15089 /* Parse the template-argument-list itself. */
15090 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15091 arguments = NULL_TREE;
15092 else
15093 arguments = cp_parser_template_argument_list (parser);
4d5297fa
GB
15094 /* Look for the `>' that ends the template-argument-list. If we find
15095 a '>>' instead, it's probably just a typo. */
15096 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15097 {
15098 if (!saved_greater_than_is_operator_p)
15099 {
2cfe82fe
ZW
15100 /* If we're in a nested template argument list, the '>>' has
15101 to be a typo for '> >'. We emit the error message, but we
15102 continue parsing and we push a '>' as next token, so that
15103 the argument list will be parsed correctly. Note that the
15104 global source location is still on the token before the
15105 '>>', so we need to say explicitly where we want it. */
15106 cp_token *token = cp_lexer_peek_token (parser->lexer);
15107 error ("%H%<>>%> should be %<> >%> "
15108 "within a nested template argument list",
15109 &token->location);
15110
15111 /* ??? Proper recovery should terminate two levels of
15112 template argument list here. */
4d5297fa
GB
15113 token->type = CPP_GREATER;
15114 }
15115 else
15116 {
2cfe82fe
ZW
15117 /* If this is not a nested template argument list, the '>>'
15118 is a typo for '>'. Emit an error message and continue.
15119 Same deal about the token location, but here we can get it
15120 right by consuming the '>>' before issuing the diagnostic. */
4d5297fa 15121 cp_lexer_consume_token (parser->lexer);
2cfe82fe
ZW
15122 error ("spurious %<>>%>, use %<>%> to terminate "
15123 "a template argument list");
4d5297fa
GB
15124 }
15125 }
2cfe82fe
ZW
15126 else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15127 error ("missing %<>%> to terminate the template argument list");
15128 else
15129 /* It's what we want, a '>'; consume it. */
15130 cp_lexer_consume_token (parser->lexer);
ec75414f 15131 /* The `>' token might be a greater-than operator again now. */
21526606 15132 parser->greater_than_is_operator_p
ec75414f
MM
15133 = saved_greater_than_is_operator_p;
15134 /* Restore the SAVED_SCOPE. */
15135 parser->scope = saved_scope;
15136 parser->qualifying_scope = saved_qualifying_scope;
15137 parser->object_scope = saved_object_scope;
15138
15139 return arguments;
15140}
15141
a723baf1
MM
15142/* MEMBER_FUNCTION is a member function, or a friend. If default
15143 arguments, or the body of the function have not yet been parsed,
15144 parse them now. */
15145
15146static void
94edc4ab 15147cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
a723baf1 15148{
a723baf1
MM
15149 /* If this member is a template, get the underlying
15150 FUNCTION_DECL. */
15151 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15152 member_function = DECL_TEMPLATE_RESULT (member_function);
15153
15154 /* There should not be any class definitions in progress at this
15155 point; the bodies of members are only parsed outside of all class
15156 definitions. */
50bc768d 15157 gcc_assert (parser->num_classes_being_defined == 0);
a723baf1
MM
15158 /* While we're parsing the member functions we might encounter more
15159 classes. We want to handle them right away, but we don't want
15160 them getting mixed up with functions that are currently in the
15161 queue. */
15162 parser->unparsed_functions_queues
15163 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15164
15165 /* Make sure that any template parameters are in scope. */
15166 maybe_begin_member_template_processing (member_function);
15167
a723baf1
MM
15168 /* If the body of the function has not yet been parsed, parse it
15169 now. */
15170 if (DECL_PENDING_INLINE_P (member_function))
15171 {
15172 tree function_scope;
15173 cp_token_cache *tokens;
15174
15175 /* The function is no longer pending; we are processing it. */
15176 tokens = DECL_PENDING_INLINE_INFO (member_function);
15177 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15178 DECL_PENDING_INLINE_P (member_function) = 0;
15179 /* If this was an inline function in a local class, enter the scope
15180 of the containing function. */
15181 function_scope = decl_function_context (member_function);
15182 if (function_scope)
15183 push_function_context_to (function_scope);
21526606 15184
2cfe82fe
ZW
15185 /* Push the body of the function onto the lexer stack. */
15186 cp_parser_push_lexer_for_tokens (parser, tokens);
21526606 15187
a723baf1
MM
15188 /* Let the front end know that we going to be defining this
15189 function. */
058b15c1
MM
15190 start_preparsed_function (member_function, NULL_TREE,
15191 SF_PRE_PARSED | SF_INCLASS_INLINE);
21526606 15192
a723baf1
MM
15193 /* Now, parse the body of the function. */
15194 cp_parser_function_definition_after_declarator (parser,
15195 /*inline_p=*/true);
21526606 15196
a723baf1
MM
15197 /* Leave the scope of the containing function. */
15198 if (function_scope)
15199 pop_function_context_from (function_scope);
2cfe82fe 15200 cp_parser_pop_lexer (parser);
a723baf1
MM
15201 }
15202
15203 /* Remove any template parameters from the symbol table. */
15204 maybe_end_member_template_processing ();
15205
15206 /* Restore the queue. */
21526606 15207 parser->unparsed_functions_queues
a723baf1
MM
15208 = TREE_CHAIN (parser->unparsed_functions_queues);
15209}
15210
cd0be382 15211/* If DECL contains any default args, remember it on the unparsed
8db1028e
NS
15212 functions queue. */
15213
15214static void
15215cp_parser_save_default_args (cp_parser* parser, tree decl)
15216{
15217 tree probe;
15218
15219 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15220 probe;
15221 probe = TREE_CHAIN (probe))
15222 if (TREE_PURPOSE (probe))
15223 {
15224 TREE_PURPOSE (parser->unparsed_functions_queues)
f44b0c8e 15225 = tree_cons (current_class_type, decl,
8db1028e
NS
15226 TREE_PURPOSE (parser->unparsed_functions_queues));
15227 break;
15228 }
15229 return;
15230}
15231
8218bd34 15232/* FN is a FUNCTION_DECL which may contains a parameter with an
f44b0c8e
MM
15233 unparsed DEFAULT_ARG. Parse the default args now. This function
15234 assumes that the current scope is the scope in which the default
15235 argument should be processed. */
a723baf1
MM
15236
15237static void
8218bd34 15238cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
a723baf1 15239{
a723baf1 15240 bool saved_local_variables_forbidden_p;
2cfe82fe 15241 tree parm;
8218bd34 15242
b92bc2a0
NS
15243 /* While we're parsing the default args, we might (due to the
15244 statement expression extension) encounter more classes. We want
15245 to handle them right away, but we don't want them getting mixed
15246 up with default args that are currently in the queue. */
15247 parser->unparsed_functions_queues
15248 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15249
2cfe82fe
ZW
15250 /* Local variable names (and the `this' keyword) may not appear
15251 in a default argument. */
15252 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15253 parser->local_variables_forbidden_p = true;
15254
15255 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15256 parm;
15257 parm = TREE_CHAIN (parm))
a723baf1 15258 {
2cfe82fe 15259 cp_token_cache *tokens;
21526606 15260
2cfe82fe
ZW
15261 if (!TREE_PURPOSE (parm)
15262 || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15263 continue;
a723baf1 15264
2cfe82fe
ZW
15265 /* Push the saved tokens for the default argument onto the parser's
15266 lexer stack. */
15267 tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15268 cp_parser_push_lexer_for_tokens (parser, tokens);
a723baf1 15269
2cfe82fe
ZW
15270 /* Parse the assignment-expression. */
15271 TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser);
a723baf1 15272
676e33ca
MM
15273 /* If the token stream has not been completely used up, then
15274 there was extra junk after the end of the default
15275 argument. */
15276 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2a13a625 15277 cp_parser_error (parser, "expected %<,%>");
676e33ca 15278
2cfe82fe
ZW
15279 /* Revert to the main lexer. */
15280 cp_parser_pop_lexer (parser);
a723baf1 15281 }
b92bc2a0 15282
2cfe82fe
ZW
15283 /* Restore the state of local_variables_forbidden_p. */
15284 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15285
b92bc2a0 15286 /* Restore the queue. */
21526606 15287 parser->unparsed_functions_queues
b92bc2a0 15288 = TREE_CHAIN (parser->unparsed_functions_queues);
a723baf1
MM
15289}
15290
15291/* Parse the operand of `sizeof' (or a similar operator). Returns
15292 either a TYPE or an expression, depending on the form of the
15293 input. The KEYWORD indicates which kind of expression we have
15294 encountered. */
15295
15296static tree
94edc4ab 15297cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
a723baf1
MM
15298{
15299 static const char *format;
15300 tree expr = NULL_TREE;
15301 const char *saved_message;
67c03833 15302 bool saved_integral_constant_expression_p;
a723baf1
MM
15303
15304 /* Initialize FORMAT the first time we get here. */
15305 if (!format)
9e637a26 15306 format = "types may not be defined in '%s' expressions";
a723baf1
MM
15307
15308 /* Types cannot be defined in a `sizeof' expression. Save away the
15309 old message. */
15310 saved_message = parser->type_definition_forbidden_message;
15311 /* And create the new one. */
21526606
EC
15312 parser->type_definition_forbidden_message
15313 = xmalloc (strlen (format)
c68b0a84
KG
15314 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15315 + 1 /* `\0' */);
a723baf1
MM
15316 sprintf ((char *) parser->type_definition_forbidden_message,
15317 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15318
15319 /* The restrictions on constant-expressions do not apply inside
15320 sizeof expressions. */
67c03833
JM
15321 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15322 parser->integral_constant_expression_p = false;
a723baf1 15323
3beb3abf
MM
15324 /* Do not actually evaluate the expression. */
15325 ++skip_evaluation;
a723baf1
MM
15326 /* If it's a `(', then we might be looking at the type-id
15327 construction. */
15328 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15329 {
15330 tree type;
4f8163b1 15331 bool saved_in_type_id_in_expr_p;
a723baf1
MM
15332
15333 /* We can't be sure yet whether we're looking at a type-id or an
15334 expression. */
15335 cp_parser_parse_tentatively (parser);
15336 /* Consume the `('. */
15337 cp_lexer_consume_token (parser->lexer);
15338 /* Parse the type-id. */
4f8163b1
MM
15339 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15340 parser->in_type_id_in_expr_p = true;
a723baf1 15341 type = cp_parser_type_id (parser);
4f8163b1 15342 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1 15343 /* Now, look for the trailing `)'. */
9e637a26 15344 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
a723baf1
MM
15345 /* If all went well, then we're done. */
15346 if (cp_parser_parse_definitely (parser))
15347 {
62d1db17
MM
15348 cp_decl_specifier_seq decl_specs;
15349
15350 /* Build a trivial decl-specifier-seq. */
15351 clear_decl_specs (&decl_specs);
15352 decl_specs.type = type;
a723baf1
MM
15353
15354 /* Call grokdeclarator to figure out what type this is. */
058b15c1 15355 expr = grokdeclarator (NULL,
62d1db17 15356 &decl_specs,
a723baf1
MM
15357 TYPENAME,
15358 /*initialized=*/0,
15359 /*attrlist=*/NULL);
15360 }
15361 }
15362
15363 /* If the type-id production did not work out, then we must be
15364 looking at the unary-expression production. */
15365 if (!expr)
15366 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
3beb3abf
MM
15367 /* Go back to evaluating expressions. */
15368 --skip_evaluation;
a723baf1
MM
15369
15370 /* Free the message we created. */
15371 free ((char *) parser->type_definition_forbidden_message);
15372 /* And restore the old one. */
15373 parser->type_definition_forbidden_message = saved_message;
67c03833 15374 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
a723baf1
MM
15375
15376 return expr;
15377}
15378
15379/* If the current declaration has no declarator, return true. */
15380
15381static bool
15382cp_parser_declares_only_class_p (cp_parser *parser)
15383{
21526606 15384 /* If the next token is a `;' or a `,' then there is no
a723baf1
MM
15385 declarator. */
15386 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15387 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15388}
15389
62d1db17 15390/* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
a723baf1 15391
62d1db17
MM
15392static void
15393cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15394 cp_storage_class storage_class)
a723baf1 15395{
62d1db17
MM
15396 if (decl_specs->storage_class != sc_none)
15397 decl_specs->multiple_storage_classes_p = true;
15398 else
15399 decl_specs->storage_class = storage_class;
15400}
15401
15402/* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15403 is true, the type is a user-defined type; otherwise it is a
15404 built-in type specified by a keyword. */
a723baf1 15405
62d1db17
MM
15406static void
15407cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15408 tree type_spec,
15409 bool user_defined_p)
15410{
15411 decl_specs->any_specifiers_p = true;
98ca843c 15412
9306cccb
MM
15413 /* If the user tries to redeclare bool or wchar_t (with, for
15414 example, in "typedef int wchar_t;") we remember that this is what
f84b6c96
MM
15415 happened. In system headers, we ignore these declarations so
15416 that G++ can work with system headers that are not C++-safe. */
98ca843c 15417 if (decl_specs->specs[(int) ds_typedef]
f84b6c96 15418 && !user_defined_p
9306cccb
MM
15419 && (type_spec == boolean_type_node
15420 || type_spec == wchar_type_node)
f84b6c96
MM
15421 && (decl_specs->type
15422 || decl_specs->specs[(int) ds_long]
15423 || decl_specs->specs[(int) ds_short]
15424 || decl_specs->specs[(int) ds_unsigned]
15425 || decl_specs->specs[(int) ds_signed]))
0a73e37f
MM
15426 {
15427 decl_specs->redefined_builtin_type = type_spec;
15428 if (!decl_specs->type)
15429 {
15430 decl_specs->type = type_spec;
15431 decl_specs->user_defined_type_p = false;
15432 }
15433 }
f84b6c96
MM
15434 else if (decl_specs->type)
15435 decl_specs->multiple_types_p = true;
62d1db17
MM
15436 else
15437 {
15438 decl_specs->type = type_spec;
15439 decl_specs->user_defined_type_p = user_defined_p;
0a73e37f 15440 decl_specs->redefined_builtin_type = NULL_TREE;
a723baf1 15441 }
62d1db17 15442}
a723baf1 15443
62d1db17
MM
15444/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15445 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15446
15447static bool
15448cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15449{
15450 return decl_specifiers->specs[(int) ds_friend] != 0;
a723baf1
MM
15451}
15452
15453/* If the next token is of the indicated TYPE, consume it. Otherwise,
15454 issue an error message indicating that TOKEN_DESC was expected.
21526606 15455
a723baf1
MM
15456 Returns the token consumed, if the token had the appropriate type.
15457 Otherwise, returns NULL. */
15458
15459static cp_token *
94edc4ab
NN
15460cp_parser_require (cp_parser* parser,
15461 enum cpp_ttype type,
15462 const char* token_desc)
a723baf1
MM
15463{
15464 if (cp_lexer_next_token_is (parser->lexer, type))
15465 return cp_lexer_consume_token (parser->lexer);
15466 else
15467 {
e5976695
MM
15468 /* Output the MESSAGE -- unless we're parsing tentatively. */
15469 if (!cp_parser_simulate_error (parser))
216bb6e1
MM
15470 {
15471 char *message = concat ("expected ", token_desc, NULL);
15472 cp_parser_error (parser, message);
15473 free (message);
15474 }
a723baf1
MM
15475 return NULL;
15476 }
15477}
15478
15479/* Like cp_parser_require, except that tokens will be skipped until
15480 the desired token is found. An error message is still produced if
15481 the next token is not as expected. */
15482
15483static void
21526606
EC
15484cp_parser_skip_until_found (cp_parser* parser,
15485 enum cpp_ttype type,
94edc4ab 15486 const char* token_desc)
a723baf1
MM
15487{
15488 cp_token *token;
15489 unsigned nesting_depth = 0;
15490
15491 if (cp_parser_require (parser, type, token_desc))
15492 return;
15493
15494 /* Skip tokens until the desired token is found. */
15495 while (true)
15496 {
15497 /* Peek at the next token. */
15498 token = cp_lexer_peek_token (parser->lexer);
21526606 15499 /* If we've reached the token we want, consume it and
a723baf1
MM
15500 stop. */
15501 if (token->type == type && !nesting_depth)
15502 {
15503 cp_lexer_consume_token (parser->lexer);
15504 return;
15505 }
15506 /* If we've run out of tokens, stop. */
15507 if (token->type == CPP_EOF)
15508 return;
21526606 15509 if (token->type == CPP_OPEN_BRACE
a723baf1
MM
15510 || token->type == CPP_OPEN_PAREN
15511 || token->type == CPP_OPEN_SQUARE)
15512 ++nesting_depth;
21526606 15513 else if (token->type == CPP_CLOSE_BRACE
a723baf1
MM
15514 || token->type == CPP_CLOSE_PAREN
15515 || token->type == CPP_CLOSE_SQUARE)
15516 {
15517 if (nesting_depth-- == 0)
15518 return;
15519 }
15520 /* Consume this token. */
15521 cp_lexer_consume_token (parser->lexer);
15522 }
15523}
15524
15525/* If the next token is the indicated keyword, consume it. Otherwise,
15526 issue an error message indicating that TOKEN_DESC was expected.
21526606 15527
a723baf1
MM
15528 Returns the token consumed, if the token had the appropriate type.
15529 Otherwise, returns NULL. */
15530
15531static cp_token *
94edc4ab
NN
15532cp_parser_require_keyword (cp_parser* parser,
15533 enum rid keyword,
15534 const char* token_desc)
a723baf1
MM
15535{
15536 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15537
15538 if (token && token->keyword != keyword)
15539 {
15540 dyn_string_t error_msg;
15541
15542 /* Format the error message. */
15543 error_msg = dyn_string_new (0);
15544 dyn_string_append_cstr (error_msg, "expected ");
15545 dyn_string_append_cstr (error_msg, token_desc);
15546 cp_parser_error (parser, error_msg->s);
15547 dyn_string_delete (error_msg);
15548 return NULL;
15549 }
15550
15551 return token;
15552}
15553
15554/* Returns TRUE iff TOKEN is a token that can begin the body of a
15555 function-definition. */
15556
21526606 15557static bool
94edc4ab 15558cp_parser_token_starts_function_definition_p (cp_token* token)
a723baf1
MM
15559{
15560 return (/* An ordinary function-body begins with an `{'. */
15561 token->type == CPP_OPEN_BRACE
15562 /* A ctor-initializer begins with a `:'. */
15563 || token->type == CPP_COLON
15564 /* A function-try-block begins with `try'. */
15565 || token->keyword == RID_TRY
15566 /* The named return value extension begins with `return'. */
15567 || token->keyword == RID_RETURN);
15568}
15569
15570/* Returns TRUE iff the next token is the ":" or "{" beginning a class
15571 definition. */
15572
15573static bool
15574cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15575{
15576 cp_token *token;
15577
15578 token = cp_lexer_peek_token (parser->lexer);
15579 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15580}
15581
d17811fd 15582/* Returns TRUE iff the next token is the "," or ">" ending a
03fd3f84 15583 template-argument. */
d17811fd
MM
15584
15585static bool
15586cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15587{
15588 cp_token *token;
15589
15590 token = cp_lexer_peek_token (parser->lexer);
391c4bc5 15591 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
d17811fd 15592}
f4abade9
GB
15593
15594/* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15595 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15596
15597static bool
21526606 15598cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
f4abade9
GB
15599 size_t n)
15600{
15601 cp_token *token;
15602
15603 token = cp_lexer_peek_nth_token (parser->lexer, n);
15604 if (token->type == CPP_LESS)
15605 return true;
15606 /* Check for the sequence `<::' in the original code. It would be lexed as
15607 `[:', where `[' is a digraph, and there is no whitespace before
15608 `:'. */
15609 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15610 {
15611 cp_token *token2;
15612 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15613 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15614 return true;
15615 }
15616 return false;
15617}
21526606 15618
a723baf1
MM
15619/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15620 or none_type otherwise. */
15621
15622static enum tag_types
94edc4ab 15623cp_parser_token_is_class_key (cp_token* token)
a723baf1
MM
15624{
15625 switch (token->keyword)
15626 {
15627 case RID_CLASS:
15628 return class_type;
15629 case RID_STRUCT:
15630 return record_type;
15631 case RID_UNION:
15632 return union_type;
21526606 15633
a723baf1
MM
15634 default:
15635 return none_type;
15636 }
15637}
15638
15639/* Issue an error message if the CLASS_KEY does not match the TYPE. */
15640
15641static void
15642cp_parser_check_class_key (enum tag_types class_key, tree type)
15643{
15644 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
2a13a625 15645 pedwarn ("%qs tag used in naming %q#T",
a723baf1 15646 class_key == union_type ? "union"
21526606 15647 : class_key == record_type ? "struct" : "class",
a723baf1
MM
15648 type);
15649}
21526606 15650
cd0be382 15651/* Issue an error message if DECL is redeclared with different
37d407a1
KL
15652 access than its original declaration [class.access.spec/3].
15653 This applies to nested classes and nested class templates.
15654 [class.mem/1]. */
15655
2a13a625
GDR
15656static void
15657cp_parser_check_access_in_redeclaration (tree decl)
37d407a1
KL
15658{
15659 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15660 return;
15661
15662 if ((TREE_PRIVATE (decl)
15663 != (current_access_specifier == access_private_node))
15664 || (TREE_PROTECTED (decl)
15665 != (current_access_specifier == access_protected_node)))
2a13a625 15666 error ("%qD redeclared with different access", decl);
37d407a1
KL
15667}
15668
a723baf1 15669/* Look for the `template' keyword, as a syntactic disambiguator.
21526606 15670 Return TRUE iff it is present, in which case it will be
a723baf1
MM
15671 consumed. */
15672
15673static bool
15674cp_parser_optional_template_keyword (cp_parser *parser)
15675{
15676 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15677 {
15678 /* The `template' keyword can only be used within templates;
15679 outside templates the parser can always figure out what is a
15680 template and what is not. */
15681 if (!processing_template_decl)
15682 {
2a13a625 15683 error ("%<template%> (as a disambiguator) is only allowed "
a723baf1
MM
15684 "within templates");
15685 /* If this part of the token stream is rescanned, the same
15686 error message would be generated. So, we purge the token
15687 from the stream. */
15688 cp_lexer_purge_token (parser->lexer);
15689 return false;
15690 }
15691 else
15692 {
15693 /* Consume the `template' keyword. */
15694 cp_lexer_consume_token (parser->lexer);
15695 return true;
15696 }
15697 }
15698
15699 return false;
15700}
15701
2050a1bb
MM
15702/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15703 set PARSER->SCOPE, and perform other related actions. */
15704
15705static void
15706cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15707{
15708 tree value;
15709 tree check;
15710
15711 /* Get the stored value. */
15712 value = cp_lexer_consume_token (parser->lexer)->value;
15713 /* Perform any access checks that were deferred. */
15714 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c 15715 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
2050a1bb
MM
15716 /* Set the scope from the stored value. */
15717 parser->scope = TREE_VALUE (value);
15718 parser->qualifying_scope = TREE_TYPE (value);
15719 parser->object_scope = NULL_TREE;
15720}
15721
03fd3f84 15722/* Consume tokens up through a non-nested END token. */
a723baf1
MM
15723
15724static void
c162c75e
MA
15725cp_parser_cache_group (cp_parser *parser,
15726 enum cpp_ttype end,
15727 unsigned depth)
a723baf1
MM
15728{
15729 while (true)
15730 {
15731 cp_token *token;
15732
15733 /* Abort a parenthesized expression if we encounter a brace. */
15734 if ((end == CPP_CLOSE_PAREN || depth == 0)
15735 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15736 return;
a723baf1 15737 /* If we've reached the end of the file, stop. */
4bfb8bba 15738 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
a723baf1 15739 return;
4bfb8bba
MM
15740 /* Consume the next token. */
15741 token = cp_lexer_consume_token (parser->lexer);
a723baf1
MM
15742 /* See if it starts a new group. */
15743 if (token->type == CPP_OPEN_BRACE)
15744 {
c162c75e 15745 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
a723baf1
MM
15746 if (depth == 0)
15747 return;
15748 }
15749 else if (token->type == CPP_OPEN_PAREN)
c162c75e 15750 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
a723baf1
MM
15751 else if (token->type == end)
15752 return;
15753 }
15754}
15755
15756/* Begin parsing tentatively. We always save tokens while parsing
15757 tentatively so that if the tentative parsing fails we can restore the
15758 tokens. */
15759
15760static void
94edc4ab 15761cp_parser_parse_tentatively (cp_parser* parser)
a723baf1
MM
15762{
15763 /* Enter a new parsing context. */
15764 parser->context = cp_parser_context_new (parser->context);
15765 /* Begin saving tokens. */
15766 cp_lexer_save_tokens (parser->lexer);
15767 /* In order to avoid repetitive access control error messages,
15768 access checks are queued up until we are no longer parsing
15769 tentatively. */
8d241e0b 15770 push_deferring_access_checks (dk_deferred);
a723baf1
MM
15771}
15772
15773/* Commit to the currently active tentative parse. */
15774
15775static void
94edc4ab 15776cp_parser_commit_to_tentative_parse (cp_parser* parser)
a723baf1
MM
15777{
15778 cp_parser_context *context;
15779 cp_lexer *lexer;
15780
15781 /* Mark all of the levels as committed. */
15782 lexer = parser->lexer;
15783 for (context = parser->context; context->next; context = context->next)
15784 {
15785 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15786 break;
15787 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15788 while (!cp_lexer_saving_tokens (lexer))
15789 lexer = lexer->next;
15790 cp_lexer_commit_tokens (lexer);
15791 }
15792}
15793
15794/* Abort the currently active tentative parse. All consumed tokens
15795 will be rolled back, and no diagnostics will be issued. */
15796
15797static void
94edc4ab 15798cp_parser_abort_tentative_parse (cp_parser* parser)
a723baf1
MM
15799{
15800 cp_parser_simulate_error (parser);
15801 /* Now, pretend that we want to see if the construct was
15802 successfully parsed. */
15803 cp_parser_parse_definitely (parser);
15804}
15805
34cd5ae7 15806/* Stop parsing tentatively. If a parse error has occurred, restore the
a723baf1
MM
15807 token stream. Otherwise, commit to the tokens we have consumed.
15808 Returns true if no error occurred; false otherwise. */
15809
15810static bool
94edc4ab 15811cp_parser_parse_definitely (cp_parser* parser)
a723baf1
MM
15812{
15813 bool error_occurred;
15814 cp_parser_context *context;
15815
34cd5ae7 15816 /* Remember whether or not an error occurred, since we are about to
a723baf1
MM
15817 destroy that information. */
15818 error_occurred = cp_parser_error_occurred (parser);
15819 /* Remove the topmost context from the stack. */
15820 context = parser->context;
15821 parser->context = context->next;
15822 /* If no parse errors occurred, commit to the tentative parse. */
15823 if (!error_occurred)
15824 {
15825 /* Commit to the tokens read tentatively, unless that was
15826 already done. */
15827 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15828 cp_lexer_commit_tokens (parser->lexer);
cf22909c
KL
15829
15830 pop_to_parent_deferring_access_checks ();
a723baf1
MM
15831 }
15832 /* Otherwise, if errors occurred, roll back our state so that things
15833 are just as they were before we began the tentative parse. */
15834 else
cf22909c
KL
15835 {
15836 cp_lexer_rollback_tokens (parser->lexer);
15837 pop_deferring_access_checks ();
15838 }
e5976695
MM
15839 /* Add the context to the front of the free list. */
15840 context->next = cp_parser_context_free_list;
15841 cp_parser_context_free_list = context;
15842
15843 return !error_occurred;
a723baf1
MM
15844}
15845
0b16f8f4
VR
15846/* Returns true if we are parsing tentatively and are not committed to
15847 this tentative parse. */
a723baf1
MM
15848
15849static bool
0b16f8f4 15850cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
a723baf1
MM
15851{
15852 return (cp_parser_parsing_tentatively (parser)
0b16f8f4 15853 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
a723baf1
MM
15854}
15855
4de8668e 15856/* Returns nonzero iff an error has occurred during the most recent
a723baf1 15857 tentative parse. */
21526606 15858
a723baf1 15859static bool
94edc4ab 15860cp_parser_error_occurred (cp_parser* parser)
a723baf1
MM
15861{
15862 return (cp_parser_parsing_tentatively (parser)
15863 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15864}
15865
4de8668e 15866/* Returns nonzero if GNU extensions are allowed. */
a723baf1
MM
15867
15868static bool
94edc4ab 15869cp_parser_allow_gnu_extensions_p (cp_parser* parser)
a723baf1
MM
15870{
15871 return parser->allow_gnu_extensions_p;
15872}
15873
15874\f
a723baf1
MM
15875/* The parser. */
15876
15877static GTY (()) cp_parser *the_parser;
15878
15879/* External interface. */
15880
d1bd0ded 15881/* Parse one entire translation unit. */
a723baf1 15882
d1bd0ded
GK
15883void
15884c_parse_file (void)
a723baf1
MM
15885{
15886 bool error_occurred;
f75fbaf7
ZW
15887 static bool already_called = false;
15888
15889 if (already_called)
15890 {
15891 sorry ("inter-module optimizations not implemented for C++");
15892 return;
15893 }
15894 already_called = true;
a723baf1
MM
15895
15896 the_parser = cp_parser_new ();
78757caa
KL
15897 push_deferring_access_checks (flag_access_control
15898 ? dk_no_deferred : dk_no_check);
a723baf1
MM
15899 error_occurred = cp_parser_translation_unit (the_parser);
15900 the_parser = NULL;
a723baf1
MM
15901}
15902
a723baf1
MM
15903/* This variable must be provided by every front end. */
15904
15905int yydebug;
15906
15907#include "gt-cp-parser.h"