]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/parser.c
re PR c++/26558 (segfault on syntax error)
[gcc.git] / gcc / cp / parser.c
CommitLineData
a723baf1 1/* C++ Parser.
4514aa8c
NS
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
a723baf1
MM
4 Written by Mark Mitchell <mark@codesourcery.com>.
5
f5adbb8d 6 This file is part of GCC.
a723baf1 7
f5adbb8d 8 GCC is free software; you can redistribute it and/or modify it
a723baf1
MM
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
f5adbb8d 13 GCC is distributed in the hope that it will be useful, but
a723baf1
MM
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
f5adbb8d 19 along with GCC; see the file COPYING. If not, write to the Free
1788952f
KC
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
a723baf1
MM
22
23#include "config.h"
24#include "system.h"
25#include "coretypes.h"
26#include "tm.h"
27#include "dyn-string.h"
28#include "varray.h"
29#include "cpplib.h"
30#include "tree.h"
31#include "cp-tree.h"
32#include "c-pragma.h"
33#include "decl.h"
34#include "flags.h"
35#include "diagnostic.h"
a723baf1
MM
36#include "toplev.h"
37#include "output.h"
62d1db17 38#include "target.h"
474eccc6 39#include "cgraph.h"
e58a9aa1 40#include "c-common.h"
a723baf1
MM
41
42\f
43/* The lexer. */
44
c162c75e
MA
45/* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
a723baf1
MM
47
48/* A C++ token. */
49
50typedef struct cp_token GTY (())
51{
52 /* The kind of token. */
df2b750f 53 ENUM_BITFIELD (cpp_ttype) type : 8;
a723baf1
MM
54 /* If this token is a keyword, this value indicates which keyword.
55 Otherwise, this value is RID_MAX. */
df2b750f 56 ENUM_BITFIELD (rid) keyword : 8;
f4abade9
GB
57 /* Token flags. */
58 unsigned char flags;
bc4071dd
RH
59 /* Identifier for the pragma. */
60 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
03fd3f84 61 /* True if this token is from a system header. */
c162c75e 62 BOOL_BITFIELD in_system_header : 1;
7d381002
MA
63 /* True if this token is from a context where it is implicitly extern "C" */
64 BOOL_BITFIELD implicit_extern_c : 1;
91b1ca65
MM
65 /* True for a CPP_NAME token that is not a keyword (i.e., for which
66 KEYWORD is RID_MAX) iff this name was looked up and found to be
67 ambiguous. An error has already been reported. */
68 BOOL_BITFIELD ambiguous_p : 1;
522df488
DN
69 /* The value associated with this token, if any. */
70 tree value;
82a98427
NS
71 /* The location at which this token was found. */
72 location_t location;
a723baf1
MM
73} cp_token;
74
0c5e4866
NS
75/* We use a stack of token pointer for saving token sets. */
76typedef struct cp_token *cp_token_position;
d4e6fecb
NS
77DEF_VEC_P (cp_token_position);
78DEF_VEC_ALLOC_P (cp_token_position,heap);
0c5e4866 79
76aebc9f
NS
80static const cp_token eof_token =
81{
bc4071dd 82 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, NULL_TREE,
76aebc9f
NS
83#if USE_MAPPED_LOCATION
84 0
85#else
86 {0, 0}
87#endif
88};
0c5e4866 89
a723baf1
MM
90/* The cp_lexer structure represents the C++ lexer. It is responsible
91 for managing the token stream from the preprocessor and supplying
c162c75e 92 it to the parser. Tokens are never added to the cp_lexer after
03fd3f84 93 it is created. */
a723baf1
MM
94
95typedef struct cp_lexer GTY (())
96{
0c5e4866
NS
97 /* The memory allocated for the buffer. NULL if this lexer does not
98 own the token buffer. */
76aebc9f
NS
99 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
100 /* If the lexer owns the buffer, this is the number of tokens in the
101 buffer. */
102 size_t buffer_length;
c8094d83 103
c162c75e 104 /* A pointer just past the last available token. The tokens
03fd3f84 105 in this lexer are [buffer, last_token). */
0c5e4866 106 cp_token_position GTY ((skip)) last_token;
c162c75e 107
0c5e4866 108 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
a723baf1 109 no more available tokens. */
0c5e4866 110 cp_token_position GTY ((skip)) next_token;
a723baf1
MM
111
112 /* A stack indicating positions at which cp_lexer_save_tokens was
113 called. The top entry is the most recent position at which we
0c5e4866
NS
114 began saving tokens. If the stack is non-empty, we are saving
115 tokens. */
d4e6fecb 116 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
a723baf1 117
bc4071dd
RH
118 /* The next lexer in a linked list of lexers. */
119 struct cp_lexer *next;
120
a723baf1
MM
121 /* True if we should output debugging information. */
122 bool debugging_p;
123
bc4071dd
RH
124 /* True if we're in the context of parsing a pragma, and should not
125 increment past the end-of-line marker. */
126 bool in_pragma;
a723baf1
MM
127} cp_lexer;
128
c162c75e
MA
129/* cp_token_cache is a range of tokens. There is no need to represent
130 allocate heap memory for it, since tokens are never removed from the
131 lexer's array. There is also no need for the GC to walk through
132 a cp_token_cache, since everything in here is referenced through
03fd3f84 133 a lexer. */
c162c75e
MA
134
135typedef struct cp_token_cache GTY(())
136{
03fd3f84 137 /* The beginning of the token range. */
c162c75e
MA
138 cp_token * GTY((skip)) first;
139
03fd3f84 140 /* Points immediately after the last token in the range. */
c162c75e
MA
141 cp_token * GTY ((skip)) last;
142} cp_token_cache;
143
a723baf1
MM
144/* Prototypes. */
145
17211ab5 146static cp_lexer *cp_lexer_new_main
94edc4ab 147 (void);
a723baf1 148static cp_lexer *cp_lexer_new_from_tokens
c162c75e
MA
149 (cp_token_cache *tokens);
150static void cp_lexer_destroy
151 (cp_lexer *);
a723baf1 152static int cp_lexer_saving_tokens
94edc4ab 153 (const cp_lexer *);
0c5e4866
NS
154static cp_token_position cp_lexer_token_position
155 (cp_lexer *, bool);
156static cp_token *cp_lexer_token_at
157 (cp_lexer *, cp_token_position);
a723baf1 158static void cp_lexer_get_preprocessor_token
94edc4ab 159 (cp_lexer *, cp_token *);
c162c75e
MA
160static inline cp_token *cp_lexer_peek_token
161 (cp_lexer *);
a723baf1 162static cp_token *cp_lexer_peek_nth_token
94edc4ab 163 (cp_lexer *, size_t);
f7b5ecd9 164static inline bool cp_lexer_next_token_is
94edc4ab 165 (cp_lexer *, enum cpp_ttype);
a723baf1 166static bool cp_lexer_next_token_is_not
94edc4ab 167 (cp_lexer *, enum cpp_ttype);
a723baf1 168static bool cp_lexer_next_token_is_keyword
94edc4ab 169 (cp_lexer *, enum rid);
21526606 170static cp_token *cp_lexer_consume_token
94edc4ab 171 (cp_lexer *);
a723baf1
MM
172static void cp_lexer_purge_token
173 (cp_lexer *);
174static void cp_lexer_purge_tokens_after
0c5e4866 175 (cp_lexer *, cp_token_position);
a723baf1 176static void cp_lexer_save_tokens
94edc4ab 177 (cp_lexer *);
a723baf1 178static void cp_lexer_commit_tokens
94edc4ab 179 (cp_lexer *);
a723baf1 180static void cp_lexer_rollback_tokens
94edc4ab 181 (cp_lexer *);
6983ea08 182#ifdef ENABLE_CHECKING
a723baf1 183static void cp_lexer_print_token
94edc4ab 184 (FILE *, cp_token *);
21526606 185static inline bool cp_lexer_debugging_p
94edc4ab 186 (cp_lexer *);
a723baf1 187static void cp_lexer_start_debugging
94edc4ab 188 (cp_lexer *) ATTRIBUTE_UNUSED;
a723baf1 189static void cp_lexer_stop_debugging
94edc4ab 190 (cp_lexer *) ATTRIBUTE_UNUSED;
6983ea08 191#else
2cfe82fe
ZW
192/* If we define cp_lexer_debug_stream to NULL it will provoke warnings
193 about passing NULL to functions that require non-NULL arguments
194 (fputs, fprintf). It will never be used, so all we need is a value
195 of the right type that's guaranteed not to be NULL. */
196#define cp_lexer_debug_stream stdout
197#define cp_lexer_print_token(str, tok) (void) 0
6983ea08
MA
198#define cp_lexer_debugging_p(lexer) 0
199#endif /* ENABLE_CHECKING */
a723baf1 200
c162c75e
MA
201static cp_token_cache *cp_token_cache_new
202 (cp_token *, cp_token *);
203
bc4071dd
RH
204static void cp_parser_initial_pragma
205 (cp_token *);
206
a723baf1 207/* Manifest constants. */
c162c75e 208#define CP_LEXER_BUFFER_SIZE 10000
0c5e4866 209#define CP_SAVED_TOKEN_STACK 5
a723baf1
MM
210
211/* A token type for keywords, as opposed to ordinary identifiers. */
212#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
213
214/* A token type for template-ids. If a template-id is processed while
215 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
216 the value of the CPP_TEMPLATE_ID is whatever was returned by
217 cp_parser_template_id. */
218#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
219
220/* A token type for nested-name-specifiers. If a
221 nested-name-specifier is processed while parsing tentatively, it is
222 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
223 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
224 cp_parser_nested_name_specifier_opt. */
225#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
226
227/* A token type for tokens that are not tokens at all; these are used
c162c75e 228 to represent slots in the array where there used to be a token
03fd3f84 229 that has now been deleted. */
b8b94c5b
PB
230#define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
231
232/* The number of token types, including C++-specific ones. */
233#define N_CP_TTYPES ((int) (CPP_PURGED + 1))
a723baf1
MM
234
235/* Variables. */
236
6983ea08 237#ifdef ENABLE_CHECKING
a723baf1
MM
238/* The stream to which debugging output should be written. */
239static FILE *cp_lexer_debug_stream;
6983ea08 240#endif /* ENABLE_CHECKING */
a723baf1 241
17211ab5
GK
242/* Create a new main C++ lexer, the lexer that gets tokens from the
243 preprocessor. */
a723baf1
MM
244
245static cp_lexer *
17211ab5 246cp_lexer_new_main (void)
a723baf1 247{
17211ab5 248 cp_token first_token;
76aebc9f
NS
249 cp_lexer *lexer;
250 cp_token *pos;
251 size_t alloc;
252 size_t space;
253 cp_token *buffer;
17211ab5 254
bc4071dd
RH
255 /* It's possible that parsing the first pragma will load a PCH file,
256 which is a GC collection point. So we have to do that before
257 allocating any memory. */
258 cp_parser_initial_pragma (&first_token);
c162c75e 259
bc4071dd 260 /* Tell c_lex_with_flags not to merge string constants. */
c162c75e
MA
261 c_lex_return_raw_strings = true;
262
18c81520 263 c_common_no_more_pch ();
a723baf1
MM
264
265 /* Allocate the memory. */
99dd239f 266 lexer = GGC_CNEW (cp_lexer);
a723baf1 267
c8094d83 268#ifdef ENABLE_CHECKING
c162c75e 269 /* Initially we are not debugging. */
a723baf1 270 lexer->debugging_p = false;
6983ea08 271#endif /* ENABLE_CHECKING */
d4e6fecb
NS
272 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
273 CP_SAVED_TOKEN_STACK);
c8094d83 274
76aebc9f
NS
275 /* Create the buffer. */
276 alloc = CP_LEXER_BUFFER_SIZE;
0ac1b889 277 buffer = GGC_NEWVEC (cp_token, alloc);
a723baf1 278
76aebc9f
NS
279 /* Put the first token in the buffer. */
280 space = alloc;
281 pos = buffer;
282 *pos = first_token;
c8094d83 283
03fd3f84 284 /* Get the remaining tokens from the preprocessor. */
76aebc9f 285 while (pos->type != CPP_EOF)
c162c75e 286 {
76aebc9f
NS
287 pos++;
288 if (!--space)
289 {
290 space = alloc;
291 alloc *= 2;
7767580e 292 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
76aebc9f
NS
293 pos = buffer + space;
294 }
295 cp_lexer_get_preprocessor_token (lexer, pos);
c162c75e 296 }
76aebc9f
NS
297 lexer->buffer = buffer;
298 lexer->buffer_length = alloc - space;
299 lexer->last_token = pos;
300 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
c162c75e 301
178b58b5
JM
302 /* Subsequent preprocessor diagnostics should use compiler
303 diagnostic functions to get the compiler source location. */
304 cpp_get_options (parse_in)->client_diagnostic = true;
305 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
306
2cfe82fe 307 gcc_assert (lexer->next_token->type != CPP_PURGED);
a723baf1
MM
308 return lexer;
309}
310
c162c75e 311/* Create a new lexer whose token stream is primed with the tokens in
2cfe82fe 312 CACHE. When these tokens are exhausted, no new tokens will be read. */
a723baf1
MM
313
314static cp_lexer *
2cfe82fe 315cp_lexer_new_from_tokens (cp_token_cache *cache)
a723baf1 316{
2cfe82fe
ZW
317 cp_token *first = cache->first;
318 cp_token *last = cache->last;
c162c75e 319 cp_lexer *lexer = GGC_CNEW (cp_lexer);
17211ab5 320
0c5e4866 321 /* We do not own the buffer. */
76aebc9f
NS
322 lexer->buffer = NULL;
323 lexer->buffer_length = 0;
324 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
0c5e4866 325 lexer->last_token = last;
c8094d83 326
d4e6fecb
NS
327 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
328 CP_SAVED_TOKEN_STACK);
21526606 329
6983ea08 330#ifdef ENABLE_CHECKING
c162c75e 331 /* Initially we are not debugging. */
17211ab5 332 lexer->debugging_p = false;
c162c75e 333#endif
a723baf1 334
2cfe82fe
ZW
335 gcc_assert (lexer->next_token->type != CPP_PURGED);
336 return lexer;
c162c75e
MA
337}
338
03fd3f84 339/* Frees all resources associated with LEXER. */
c162c75e
MA
340
341static void
342cp_lexer_destroy (cp_lexer *lexer)
343{
0c5e4866
NS
344 if (lexer->buffer)
345 ggc_free (lexer->buffer);
d4e6fecb 346 VEC_free (cp_token_position, heap, lexer->saved_tokens);
c162c75e
MA
347 ggc_free (lexer);
348}
349
4de8668e 350/* Returns nonzero if debugging information should be output. */
a723baf1 351
6983ea08
MA
352#ifdef ENABLE_CHECKING
353
f7b5ecd9
MM
354static inline bool
355cp_lexer_debugging_p (cp_lexer *lexer)
a723baf1 356{
f7b5ecd9
MM
357 return lexer->debugging_p;
358}
359
6983ea08
MA
360#endif /* ENABLE_CHECKING */
361
0c5e4866
NS
362static inline cp_token_position
363cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
a723baf1 364{
0c5e4866 365 gcc_assert (!previous_p || lexer->next_token != &eof_token);
c8094d83 366
0c5e4866 367 return lexer->next_token - previous_p;
a723baf1
MM
368}
369
a668c6ad 370static inline cp_token *
0c5e4866 371cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
a668c6ad 372{
0c5e4866 373 return pos;
a668c6ad
MM
374}
375
4de8668e 376/* nonzero if we are presently saving tokens. */
f7b5ecd9 377
0c5e4866 378static inline int
94edc4ab 379cp_lexer_saving_tokens (const cp_lexer* lexer)
f7b5ecd9 380{
0c5e4866 381 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
a723baf1
MM
382}
383
76aebc9f
NS
384/* Store the next token from the preprocessor in *TOKEN. Return true
385 if we reach EOF. */
a723baf1 386
21526606 387static void
94edc4ab 388cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
0cbd7506 389 cp_token *token)
a723baf1 390{
7d381002 391 static int is_extern_c = 0;
a723baf1 392
51e63e60 393 /* Get a new token from the preprocessor. */
b68b6828
PB
394 token->type
395 = c_lex_with_flags (&token->value, &token->location, &token->flags);
bc4071dd
RH
396 token->keyword = RID_MAX;
397 token->pragma_kind = PRAGMA_NONE;
c162c75e 398 token->in_system_header = in_system_header;
a723baf1 399
c8094d83 400 /* On some systems, some header files are surrounded by an
7d381002 401 implicit extern "C" block. Set a flag in the token if it
03fd3f84 402 comes from such a header. */
7d381002
MA
403 is_extern_c += pending_lang_change;
404 pending_lang_change = 0;
405 token->implicit_extern_c = is_extern_c > 0;
406
a723baf1 407 /* Check to see if this token is a keyword. */
91b1ca65 408 if (token->type == CPP_NAME)
a723baf1 409 {
91b1ca65
MM
410 if (C_IS_RESERVED_WORD (token->value))
411 {
412 /* Mark this token as a keyword. */
413 token->type = CPP_KEYWORD;
414 /* Record which keyword. */
415 token->keyword = C_RID_CODE (token->value);
416 /* Update the value. Some keywords are mapped to particular
417 entities, rather than simply having the value of the
418 corresponding IDENTIFIER_NODE. For example, `__const' is
419 mapped to `const'. */
420 token->value = ridpointers[token->keyword];
421 }
422 else
37edf0a6
MM
423 {
424 token->ambiguous_p = false;
425 token->keyword = RID_MAX;
426 }
a723baf1 427 }
e58a9aa1
ZL
428 /* Handle Objective-C++ keywords. */
429 else if (token->type == CPP_AT_NAME)
430 {
431 token->type = CPP_KEYWORD;
432 switch (C_RID_CODE (token->value))
433 {
434 /* Map 'class' to '@class', 'private' to '@private', etc. */
435 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
436 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
437 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
438 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
439 case RID_THROW: token->keyword = RID_AT_THROW; break;
440 case RID_TRY: token->keyword = RID_AT_TRY; break;
441 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
442 default: token->keyword = C_RID_CODE (token->value);
443 }
444 }
bc4071dd
RH
445 else if (token->type == CPP_PRAGMA)
446 {
447 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
448 token->pragma_kind = TREE_INT_CST_LOW (token->value);
449 token->value = NULL;
450 }
a723baf1
MM
451}
452
03fd3f84 453/* Update the globals input_location and in_system_header from TOKEN. */
2cfe82fe
ZW
454static inline void
455cp_lexer_set_source_position_from_token (cp_token *token)
456{
457 if (token->type != CPP_EOF)
458 {
459 input_location = token->location;
460 in_system_header = token->in_system_header;
461 }
462}
463
a723baf1
MM
464/* Return a pointer to the next token in the token stream, but do not
465 consume it. */
466
c162c75e
MA
467static inline cp_token *
468cp_lexer_peek_token (cp_lexer *lexer)
a723baf1 469{
a723baf1 470 if (cp_lexer_debugging_p (lexer))
0c5e4866
NS
471 {
472 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
473 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
474 putc ('\n', cp_lexer_debug_stream);
475 }
2cfe82fe 476 return lexer->next_token;
a723baf1
MM
477}
478
479/* Return true if the next token has the indicated TYPE. */
480
2cfe82fe 481static inline bool
94edc4ab 482cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
a723baf1 483{
2cfe82fe 484 return cp_lexer_peek_token (lexer)->type == type;
a723baf1
MM
485}
486
487/* Return true if the next token does not have the indicated TYPE. */
488
2cfe82fe 489static inline bool
94edc4ab 490cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
491{
492 return !cp_lexer_next_token_is (lexer, type);
493}
494
495/* Return true if the next token is the indicated KEYWORD. */
496
2cfe82fe 497static inline bool
94edc4ab 498cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
a723baf1 499{
cba43cb6 500 return cp_lexer_peek_token (lexer)->keyword == keyword;
a723baf1
MM
501}
502
503/* Return a pointer to the Nth token in the token stream. If N is 1,
2cfe82fe
ZW
504 then this is precisely equivalent to cp_lexer_peek_token (except
505 that it is not inline). One would like to disallow that case, but
506 there is one case (cp_parser_nth_token_starts_template_id) where
507 the caller passes a variable for N and it might be 1. */
a723baf1
MM
508
509static cp_token *
94edc4ab 510cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
a723baf1
MM
511{
512 cp_token *token;
513
514 /* N is 1-based, not zero-based. */
863a3314
NS
515 gcc_assert (n > 0);
516
2cfe82fe
ZW
517 if (cp_lexer_debugging_p (lexer))
518 fprintf (cp_lexer_debug_stream,
519 "cp_lexer: peeking ahead %ld at token: ", (long)n);
520
c162c75e 521 --n;
a723baf1 522 token = lexer->next_token;
863a3314 523 gcc_assert (!n || token != &eof_token);
c162c75e 524 while (n != 0)
a723baf1 525 {
c162c75e 526 ++token;
0c5e4866
NS
527 if (token == lexer->last_token)
528 {
76aebc9f 529 token = (cp_token *)&eof_token;
0c5e4866
NS
530 break;
531 }
c8094d83 532
c162c75e
MA
533 if (token->type != CPP_PURGED)
534 --n;
a723baf1
MM
535 }
536
2cfe82fe
ZW
537 if (cp_lexer_debugging_p (lexer))
538 {
539 cp_lexer_print_token (cp_lexer_debug_stream, token);
540 putc ('\n', cp_lexer_debug_stream);
541 }
542
a723baf1
MM
543 return token;
544}
545
2cfe82fe
ZW
546/* Return the next token, and advance the lexer's next_token pointer
547 to point to the next non-purged token. */
a723baf1
MM
548
549static cp_token *
94edc4ab 550cp_lexer_consume_token (cp_lexer* lexer)
a723baf1 551{
2cfe82fe 552 cp_token *token = lexer->next_token;
a723baf1 553
0c5e4866 554 gcc_assert (token != &eof_token);
bc4071dd 555 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
c8094d83 556
2cfe82fe 557 do
0c5e4866
NS
558 {
559 lexer->next_token++;
560 if (lexer->next_token == lexer->last_token)
561 {
76aebc9f 562 lexer->next_token = (cp_token *)&eof_token;
0c5e4866
NS
563 break;
564 }
c8094d83 565
0c5e4866 566 }
2cfe82fe 567 while (lexer->next_token->type == CPP_PURGED);
c8094d83 568
2cfe82fe 569 cp_lexer_set_source_position_from_token (token);
c8094d83 570
a723baf1
MM
571 /* Provide debugging output. */
572 if (cp_lexer_debugging_p (lexer))
573 {
2cfe82fe 574 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
a723baf1 575 cp_lexer_print_token (cp_lexer_debug_stream, token);
2cfe82fe 576 putc ('\n', cp_lexer_debug_stream);
a723baf1 577 }
c8094d83 578
a723baf1
MM
579 return token;
580}
581
2cfe82fe
ZW
582/* Permanently remove the next token from the token stream, and
583 advance the next_token pointer to refer to the next non-purged
584 token. */
a723baf1
MM
585
586static void
587cp_lexer_purge_token (cp_lexer *lexer)
588{
c162c75e 589 cp_token *tok = lexer->next_token;
c8094d83 590
0c5e4866 591 gcc_assert (tok != &eof_token);
c162c75e
MA
592 tok->type = CPP_PURGED;
593 tok->location = UNKNOWN_LOCATION;
594 tok->value = NULL_TREE;
595 tok->keyword = RID_MAX;
2cfe82fe
ZW
596
597 do
0c5e4866
NS
598 {
599 tok++;
600 if (tok == lexer->last_token)
601 {
76aebc9f 602 tok = (cp_token *)&eof_token;
0c5e4866
NS
603 break;
604 }
605 }
606 while (tok->type == CPP_PURGED);
607 lexer->next_token = tok;
a723baf1
MM
608}
609
c162c75e 610/* Permanently remove all tokens after TOK, up to, but not
a723baf1
MM
611 including, the token that will be returned next by
612 cp_lexer_peek_token. */
613
614static void
c162c75e 615cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
a723baf1 616{
0c5e4866 617 cp_token *peek = lexer->next_token;
a723baf1 618
0c5e4866
NS
619 if (peek == &eof_token)
620 peek = lexer->last_token;
c8094d83 621
c162c75e
MA
622 gcc_assert (tok < peek);
623
624 for ( tok += 1; tok != peek; tok += 1)
a723baf1 625 {
c162c75e
MA
626 tok->type = CPP_PURGED;
627 tok->location = UNKNOWN_LOCATION;
628 tok->value = NULL_TREE;
629 tok->keyword = RID_MAX;
a723baf1 630 }
c162c75e
MA
631}
632
a723baf1
MM
633/* Begin saving tokens. All tokens consumed after this point will be
634 preserved. */
635
636static void
94edc4ab 637cp_lexer_save_tokens (cp_lexer* lexer)
a723baf1
MM
638{
639 /* Provide debugging output. */
640 if (cp_lexer_debugging_p (lexer))
641 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
642
d4e6fecb
NS
643 VEC_safe_push (cp_token_position, heap,
644 lexer->saved_tokens, lexer->next_token);
a723baf1
MM
645}
646
647/* Commit to the portion of the token stream most recently saved. */
648
649static void
94edc4ab 650cp_lexer_commit_tokens (cp_lexer* lexer)
a723baf1
MM
651{
652 /* Provide debugging output. */
653 if (cp_lexer_debugging_p (lexer))
654 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
655
0c5e4866 656 VEC_pop (cp_token_position, lexer->saved_tokens);
a723baf1
MM
657}
658
659/* Return all tokens saved since the last call to cp_lexer_save_tokens
660 to the token stream. Stop saving tokens. */
661
662static void
94edc4ab 663cp_lexer_rollback_tokens (cp_lexer* lexer)
a723baf1 664{
a723baf1
MM
665 /* Provide debugging output. */
666 if (cp_lexer_debugging_p (lexer))
667 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
668
0c5e4866 669 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
a723baf1
MM
670}
671
a723baf1
MM
672/* Print a representation of the TOKEN on the STREAM. */
673
6983ea08
MA
674#ifdef ENABLE_CHECKING
675
a723baf1 676static void
c162c75e
MA
677cp_lexer_print_token (FILE * stream, cp_token *token)
678{
679 /* We don't use cpp_type2name here because the parser defines
680 a few tokens of its own. */
681 static const char *const token_names[] = {
682 /* cpplib-defined token types */
683#define OP(e, s) #e,
684#define TK(e, s) #e,
685 TTYPE_TABLE
686#undef OP
687#undef TK
688 /* C++ parser token types - see "Manifest constants", above. */
689 "KEYWORD",
690 "TEMPLATE_ID",
691 "NESTED_NAME_SPECIFIER",
692 "PURGED"
693 };
c8094d83 694
c162c75e
MA
695 /* If we have a name for the token, print it out. Otherwise, we
696 simply give the numeric code. */
697 gcc_assert (token->type < ARRAY_SIZE(token_names));
698 fputs (token_names[token->type], stream);
a723baf1 699
c162c75e 700 /* For some tokens, print the associated data. */
a723baf1
MM
701 switch (token->type)
702 {
c162c75e
MA
703 case CPP_KEYWORD:
704 /* Some keywords have a value that is not an IDENTIFIER_NODE.
705 For example, `struct' is mapped to an INTEGER_CST. */
706 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
707 break;
708 /* else fall through */
a723baf1 709 case CPP_NAME:
c162c75e 710 fputs (IDENTIFIER_POINTER (token->value), stream);
a723baf1
MM
711 break;
712
c162c75e
MA
713 case CPP_STRING:
714 case CPP_WSTRING:
c162c75e 715 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
a723baf1
MM
716 break;
717
a723baf1
MM
718 default:
719 break;
720 }
a723baf1
MM
721}
722
a723baf1
MM
723/* Start emitting debugging information. */
724
725static void
94edc4ab 726cp_lexer_start_debugging (cp_lexer* lexer)
a723baf1 727{
81122c44 728 lexer->debugging_p = true;
a723baf1 729}
21526606 730
a723baf1
MM
731/* Stop emitting debugging information. */
732
733static void
94edc4ab 734cp_lexer_stop_debugging (cp_lexer* lexer)
a723baf1 735{
81122c44 736 lexer->debugging_p = false;
a723baf1
MM
737}
738
6983ea08
MA
739#endif /* ENABLE_CHECKING */
740
03fd3f84 741/* Create a new cp_token_cache, representing a range of tokens. */
c162c75e
MA
742
743static cp_token_cache *
744cp_token_cache_new (cp_token *first, cp_token *last)
745{
746 cp_token_cache *cache = GGC_NEW (cp_token_cache);
747 cache->first = first;
748 cache->last = last;
749 return cache;
750}
751
a723baf1 752\f
62d1db17
MM
753/* Decl-specifiers. */
754
62d1db17
MM
755/* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
756
757static void
758clear_decl_specs (cp_decl_specifier_seq *decl_specs)
759{
760 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
761}
762
058b15c1
MM
763/* Declarators. */
764
765/* Nothing other than the parser should be creating declarators;
766 declarators are a semi-syntactic representation of C++ entities.
767 Other parts of the front end that need to create entities (like
768 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
769
98ca843c 770static cp_declarator *make_call_declarator
3c01e5df 771 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
98ca843c 772static cp_declarator *make_array_declarator
058b15c1 773 (cp_declarator *, tree);
98ca843c 774static cp_declarator *make_pointer_declarator
3c01e5df 775 (cp_cv_quals, cp_declarator *);
98ca843c 776static cp_declarator *make_reference_declarator
3c01e5df 777 (cp_cv_quals, cp_declarator *);
98ca843c 778static cp_parameter_declarator *make_parameter_declarator
62d1db17 779 (cp_decl_specifier_seq *, cp_declarator *, tree);
98ca843c 780static cp_declarator *make_ptrmem_declarator
3c01e5df 781 (cp_cv_quals, tree, cp_declarator *);
058b15c1 782
fa6098f8
MM
783/* An erroneous declarator. */
784static cp_declarator *cp_error_declarator;
058b15c1
MM
785
786/* The obstack on which declarators and related data structures are
787 allocated. */
788static struct obstack declarator_obstack;
789
790/* Alloc BYTES from the declarator memory pool. */
791
792static inline void *
793alloc_declarator (size_t bytes)
794{
795 return obstack_alloc (&declarator_obstack, bytes);
796}
797
798/* Allocate a declarator of the indicated KIND. Clear fields that are
799 common to all declarators. */
800
801static cp_declarator *
802make_declarator (cp_declarator_kind kind)
803{
804 cp_declarator *declarator;
805
806 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
807 declarator->kind = kind;
808 declarator->attributes = NULL_TREE;
809 declarator->declarator = NULL;
810
811 return declarator;
812}
813
d85d3d57
MM
814/* Make a declarator for a generalized identifier. If
815 QUALIFYING_SCOPE is non-NULL, the identifier is
816 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
817 UNQUALIFIED_NAME. SFK indicates the kind of special function this
818 is, if any. */
058b15c1 819
1d786913 820static cp_declarator *
d85d3d57
MM
821make_id_declarator (tree qualifying_scope, tree unqualified_name,
822 special_function_kind sfk)
058b15c1
MM
823{
824 cp_declarator *declarator;
98ca843c 825
1d786913
MM
826 /* It is valid to write:
827
828 class C { void f(); };
829 typedef C D;
830 void D::f();
831
832 The standard is not clear about whether `typedef const C D' is
833 legal; as of 2002-09-15 the committee is considering that
834 question. EDG 3.0 allows that syntax. Therefore, we do as
835 well. */
836 if (qualifying_scope && TYPE_P (qualifying_scope))
837 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
838
d85d3d57
MM
839 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
840 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
841 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
842
058b15c1 843 declarator = make_declarator (cdk_id);
1d786913
MM
844 declarator->u.id.qualifying_scope = qualifying_scope;
845 declarator->u.id.unqualified_name = unqualified_name;
d85d3d57 846 declarator->u.id.sfk = sfk;
058b15c1
MM
847
848 return declarator;
849}
850
851/* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
852 of modifiers such as const or volatile to apply to the pointer
853 type, represented as identifiers. */
854
855cp_declarator *
3c01e5df 856make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
058b15c1
MM
857{
858 cp_declarator *declarator;
859
860 declarator = make_declarator (cdk_pointer);
861 declarator->declarator = target;
862 declarator->u.pointer.qualifiers = cv_qualifiers;
863 declarator->u.pointer.class_type = NULL_TREE;
864
865 return declarator;
866}
867
868/* Like make_pointer_declarator -- but for references. */
869
870cp_declarator *
3c01e5df 871make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
058b15c1
MM
872{
873 cp_declarator *declarator;
874
875 declarator = make_declarator (cdk_reference);
876 declarator->declarator = target;
877 declarator->u.pointer.qualifiers = cv_qualifiers;
878 declarator->u.pointer.class_type = NULL_TREE;
879
880 return declarator;
881}
882
883/* Like make_pointer_declarator -- but for a pointer to a non-static
884 member of CLASS_TYPE. */
885
886cp_declarator *
3c01e5df 887make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
058b15c1
MM
888 cp_declarator *pointee)
889{
890 cp_declarator *declarator;
891
892 declarator = make_declarator (cdk_ptrmem);
893 declarator->declarator = pointee;
894 declarator->u.pointer.qualifiers = cv_qualifiers;
895 declarator->u.pointer.class_type = class_type;
896
897 return declarator;
898}
899
900/* Make a declarator for the function given by TARGET, with the
901 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
902 "const"-qualified member function. The EXCEPTION_SPECIFICATION
903 indicates what exceptions can be thrown. */
904
905cp_declarator *
98ca843c 906make_call_declarator (cp_declarator *target,
058b15c1 907 cp_parameter_declarator *parms,
3c01e5df 908 cp_cv_quals cv_qualifiers,
0cbd7506 909 tree exception_specification)
058b15c1
MM
910{
911 cp_declarator *declarator;
912
913 declarator = make_declarator (cdk_function);
914 declarator->declarator = target;
915 declarator->u.function.parameters = parms;
916 declarator->u.function.qualifiers = cv_qualifiers;
917 declarator->u.function.exception_specification = exception_specification;
918
919 return declarator;
920}
921
922/* Make a declarator for an array of BOUNDS elements, each of which is
923 defined by ELEMENT. */
924
925cp_declarator *
926make_array_declarator (cp_declarator *element, tree bounds)
927{
928 cp_declarator *declarator;
929
930 declarator = make_declarator (cdk_array);
931 declarator->declarator = element;
932 declarator->u.array.bounds = bounds;
933
934 return declarator;
935}
936
937cp_parameter_declarator *no_parameters;
938
939/* Create a parameter declarator with the indicated DECL_SPECIFIERS,
940 DECLARATOR and DEFAULT_ARGUMENT. */
941
942cp_parameter_declarator *
98ca843c 943make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
058b15c1
MM
944 cp_declarator *declarator,
945 tree default_argument)
946{
947 cp_parameter_declarator *parameter;
948
98ca843c 949 parameter = ((cp_parameter_declarator *)
058b15c1
MM
950 alloc_declarator (sizeof (cp_parameter_declarator)));
951 parameter->next = NULL;
62d1db17
MM
952 if (decl_specifiers)
953 parameter->decl_specifiers = *decl_specifiers;
954 else
955 clear_decl_specs (&parameter->decl_specifiers);
058b15c1
MM
956 parameter->declarator = declarator;
957 parameter->default_argument = default_argument;
958 parameter->ellipsis_p = false;
959
960 return parameter;
961}
962
a723baf1
MM
963/* The parser. */
964
965/* Overview
966 --------
967
968 A cp_parser parses the token stream as specified by the C++
969 grammar. Its job is purely parsing, not semantic analysis. For
970 example, the parser breaks the token stream into declarators,
971 expressions, statements, and other similar syntactic constructs.
972 It does not check that the types of the expressions on either side
973 of an assignment-statement are compatible, or that a function is
974 not declared with a parameter of type `void'.
975
976 The parser invokes routines elsewhere in the compiler to perform
977 semantic analysis and to build up the abstract syntax tree for the
21526606 978 code processed.
a723baf1
MM
979
980 The parser (and the template instantiation code, which is, in a
981 way, a close relative of parsing) are the only parts of the
982 compiler that should be calling push_scope and pop_scope, or
983 related functions. The parser (and template instantiation code)
984 keeps track of what scope is presently active; everything else
985 should simply honor that. (The code that generates static
986 initializers may also need to set the scope, in order to check
987 access control correctly when emitting the initializers.)
988
989 Methodology
990 -----------
21526606 991
a723baf1
MM
992 The parser is of the standard recursive-descent variety. Upcoming
993 tokens in the token stream are examined in order to determine which
994 production to use when parsing a non-terminal. Some C++ constructs
995 require arbitrary look ahead to disambiguate. For example, it is
996 impossible, in the general case, to tell whether a statement is an
997 expression or declaration without scanning the entire statement.
998 Therefore, the parser is capable of "parsing tentatively." When the
999 parser is not sure what construct comes next, it enters this mode.
1000 Then, while we attempt to parse the construct, the parser queues up
1001 error messages, rather than issuing them immediately, and saves the
1002 tokens it consumes. If the construct is parsed successfully, the
1003 parser "commits", i.e., it issues any queued error messages and
1004 the tokens that were being preserved are permanently discarded.
1005 If, however, the construct is not parsed successfully, the parser
1006 rolls back its state completely so that it can resume parsing using
1007 a different alternative.
1008
1009 Future Improvements
1010 -------------------
21526606 1011
b8b94c5b
PB
1012 The performance of the parser could probably be improved substantially.
1013 We could often eliminate the need to parse tentatively by looking ahead
1014 a little bit. In some places, this approach might not entirely eliminate
1015 the need to parse tentatively, but it might still speed up the average
1016 case. */
a723baf1
MM
1017
1018/* Flags that are passed to some parsing functions. These values can
1019 be bitwise-ored together. */
1020
1021typedef enum cp_parser_flags
1022{
1023 /* No flags. */
1024 CP_PARSER_FLAGS_NONE = 0x0,
1025 /* The construct is optional. If it is not present, then no error
1026 should be issued. */
1027 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1028 /* When parsing a type-specifier, do not allow user-defined types. */
1029 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1030} cp_parser_flags;
1031
62b8a44e
NS
1032/* The different kinds of declarators we want to parse. */
1033
1034typedef enum cp_parser_declarator_kind
1035{
852dcbdd 1036 /* We want an abstract declarator. */
62b8a44e
NS
1037 CP_PARSER_DECLARATOR_ABSTRACT,
1038 /* We want a named declarator. */
1039 CP_PARSER_DECLARATOR_NAMED,
04c06002 1040 /* We don't mind, but the name must be an unqualified-id. */
62b8a44e
NS
1041 CP_PARSER_DECLARATOR_EITHER
1042} cp_parser_declarator_kind;
1043
b8b94c5b
PB
1044/* The precedence values used to parse binary expressions. The minimum value
1045 of PREC must be 1, because zero is reserved to quickly discriminate
1046 binary operators from other tokens. */
a723baf1 1047
b8b94c5b 1048enum cp_parser_prec
a723baf1 1049{
b8b94c5b
PB
1050 PREC_NOT_OPERATOR,
1051 PREC_LOGICAL_OR_EXPRESSION,
1052 PREC_LOGICAL_AND_EXPRESSION,
1053 PREC_INCLUSIVE_OR_EXPRESSION,
1054 PREC_EXCLUSIVE_OR_EXPRESSION,
1055 PREC_AND_EXPRESSION,
b8b94c5b 1056 PREC_EQUALITY_EXPRESSION,
69475123 1057 PREC_RELATIONAL_EXPRESSION,
b8b94c5b
PB
1058 PREC_SHIFT_EXPRESSION,
1059 PREC_ADDITIVE_EXPRESSION,
1060 PREC_MULTIPLICATIVE_EXPRESSION,
1061 PREC_PM_EXPRESSION,
1062 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1063};
a723baf1 1064
b8b94c5b
PB
1065/* A mapping from a token type to a corresponding tree node type, with a
1066 precedence value. */
a723baf1 1067
b8b94c5b
PB
1068typedef struct cp_parser_binary_operations_map_node
1069{
1070 /* The token type. */
1071 enum cpp_ttype token_type;
1072 /* The corresponding tree code. */
1073 enum tree_code tree_type;
1074 /* The precedence of this operator. */
1075 enum cp_parser_prec prec;
1076} cp_parser_binary_operations_map_node;
a723baf1
MM
1077
1078/* The status of a tentative parse. */
1079
1080typedef enum cp_parser_status_kind
1081{
1082 /* No errors have occurred. */
1083 CP_PARSER_STATUS_KIND_NO_ERROR,
1084 /* An error has occurred. */
1085 CP_PARSER_STATUS_KIND_ERROR,
1086 /* We are committed to this tentative parse, whether or not an error
1087 has occurred. */
1088 CP_PARSER_STATUS_KIND_COMMITTED
1089} cp_parser_status_kind;
1090
b8b94c5b
PB
1091typedef struct cp_parser_expression_stack_entry
1092{
1093 tree lhs;
1094 enum tree_code tree_type;
1095 int prec;
1096} cp_parser_expression_stack_entry;
1097
43c2a69a
PB
1098/* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1099 entries because precedence levels on the stack are monotonically
1100 increasing. */
b8b94c5b
PB
1101typedef struct cp_parser_expression_stack_entry
1102 cp_parser_expression_stack[NUM_PREC_VALUES];
a723baf1 1103
b8b94c5b 1104/* Context that is saved and restored when parsing tentatively. */
a723baf1
MM
1105typedef struct cp_parser_context GTY (())
1106{
1107 /* If this is a tentative parsing context, the status of the
1108 tentative parse. */
1109 enum cp_parser_status_kind status;
1110 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1111 that are looked up in this context must be looked up both in the
1112 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1113 the context of the containing expression. */
1114 tree object_type;
b8b94c5b 1115
a723baf1
MM
1116 /* The next parsing context in the stack. */
1117 struct cp_parser_context *next;
1118} cp_parser_context;
1119
1120/* Prototypes. */
1121
1122/* Constructors and destructors. */
1123
1124static cp_parser_context *cp_parser_context_new
94edc4ab 1125 (cp_parser_context *);
a723baf1 1126
e5976695
MM
1127/* Class variables. */
1128
1431042e 1129static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
e5976695 1130
b8b94c5b
PB
1131/* The operator-precedence table used by cp_parser_binary_expression.
1132 Transformed into an associative array (binops_by_token) by
1133 cp_parser_new. */
1134
1135static const cp_parser_binary_operations_map_node binops[] = {
1136 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1137 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1138
1139 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1140 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1141 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1142
1143 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1144 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1145
1146 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1147 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1148
1149 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1150 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1151 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1152 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1153 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1154 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1155
1156 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1157 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1158
1159 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1160
1161 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1162
1163 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1164
1165 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1166
1167 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1168};
1169
1170/* The same as binops, but initialized by cp_parser_new so that
1171 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1172 for speed. */
1173static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1174
a723baf1
MM
1175/* Constructors and destructors. */
1176
1177/* Construct a new context. The context below this one on the stack
1178 is given by NEXT. */
1179
1180static cp_parser_context *
94edc4ab 1181cp_parser_context_new (cp_parser_context* next)
a723baf1
MM
1182{
1183 cp_parser_context *context;
1184
1185 /* Allocate the storage. */
e5976695
MM
1186 if (cp_parser_context_free_list != NULL)
1187 {
1188 /* Pull the first entry from the free list. */
1189 context = cp_parser_context_free_list;
1190 cp_parser_context_free_list = context->next;
c68b0a84 1191 memset (context, 0, sizeof (*context));
e5976695
MM
1192 }
1193 else
99dd239f 1194 context = GGC_CNEW (cp_parser_context);
b8b94c5b 1195
a723baf1
MM
1196 /* No errors have occurred yet in this context. */
1197 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1198 /* If this is not the bottomost context, copy information that we
1199 need from the previous context. */
1200 if (next)
1201 {
1202 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1203 expression, then we are parsing one in this context, too. */
1204 context->object_type = next->object_type;
a723baf1
MM
1205 /* Thread the stack. */
1206 context->next = next;
1207 }
1208
1209 return context;
1210}
1211
1212/* The cp_parser structure represents the C++ parser. */
1213
1214typedef struct cp_parser GTY(())
1215{
1216 /* The lexer from which we are obtaining tokens. */
1217 cp_lexer *lexer;
1218
1219 /* The scope in which names should be looked up. If NULL_TREE, then
1220 we look up names in the scope that is currently open in the
1221 source program. If non-NULL, this is either a TYPE or
8fe4d24b
NS
1222 NAMESPACE_DECL for the scope in which we should look. It can
1223 also be ERROR_MARK, when we've parsed a bogus scope.
a723baf1
MM
1224
1225 This value is not cleared automatically after a name is looked
1226 up, so we must be careful to clear it before starting a new look
1227 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1228 will look up `Z' in the scope of `X', rather than the current
1229 scope.) Unfortunately, it is difficult to tell when name lookup
1230 is complete, because we sometimes peek at a token, look it up,
8fe4d24b 1231 and then decide not to consume it. */
a723baf1
MM
1232 tree scope;
1233
1234 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1235 last lookup took place. OBJECT_SCOPE is used if an expression
1236 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
21526606 1237 respectively. QUALIFYING_SCOPE is used for an expression of the
a723baf1
MM
1238 form "X::Y"; it refers to X. */
1239 tree object_scope;
1240 tree qualifying_scope;
1241
1242 /* A stack of parsing contexts. All but the bottom entry on the
1243 stack will be tentative contexts.
1244
1245 We parse tentatively in order to determine which construct is in
1246 use in some situations. For example, in order to determine
1247 whether a statement is an expression-statement or a
1248 declaration-statement we parse it tentatively as a
1249 declaration-statement. If that fails, we then reparse the same
1250 token stream as an expression-statement. */
1251 cp_parser_context *context;
1252
1253 /* True if we are parsing GNU C++. If this flag is not set, then
1254 GNU extensions are not recognized. */
1255 bool allow_gnu_extensions_p;
1256
1257 /* TRUE if the `>' token should be interpreted as the greater-than
1258 operator. FALSE if it is the end of a template-id or
1259 template-parameter-list. */
1260 bool greater_than_is_operator_p;
1261
1262 /* TRUE if default arguments are allowed within a parameter list
1263 that starts at this point. FALSE if only a gnu extension makes
cd0be382 1264 them permissible. */
a723baf1 1265 bool default_arg_ok_p;
21526606 1266
a723baf1
MM
1267 /* TRUE if we are parsing an integral constant-expression. See
1268 [expr.const] for a precise definition. */
67c03833 1269 bool integral_constant_expression_p;
a723baf1 1270
14d22dd6
MM
1271 /* TRUE if we are parsing an integral constant-expression -- but a
1272 non-constant expression should be permitted as well. This flag
1273 is used when parsing an array bound so that GNU variable-length
1274 arrays are tolerated. */
67c03833 1275 bool allow_non_integral_constant_expression_p;
14d22dd6
MM
1276
1277 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1278 been seen that makes the expression non-constant. */
67c03833 1279 bool non_integral_constant_expression_p;
14d22dd6 1280
a723baf1
MM
1281 /* TRUE if local variable names and `this' are forbidden in the
1282 current context. */
1283 bool local_variables_forbidden_p;
1284
1285 /* TRUE if the declaration we are parsing is part of a
1286 linkage-specification of the form `extern string-literal
1287 declaration'. */
1288 bool in_unbraced_linkage_specification_p;
1289
1290 /* TRUE if we are presently parsing a declarator, after the
1291 direct-declarator. */
1292 bool in_declarator_p;
1293
4bb8ca28
MM
1294 /* TRUE if we are presently parsing a template-argument-list. */
1295 bool in_template_argument_list_p;
1296
1799e5d5
RH
1297 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1298 to IN_OMP_BLOCK if parsing OpenMP structured block and
1299 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1300 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1301 iteration-statement, OpenMP block or loop within that switch. */
1302#define IN_SWITCH_STMT 1
1303#define IN_ITERATION_STMT 2
1304#define IN_OMP_BLOCK 4
1305#define IN_OMP_FOR 8
1306 unsigned char in_statement;
1307
1308 /* TRUE if we are presently parsing the body of a switch statement.
1309 Note that this doesn't quite overlap with in_statement above.
1310 The difference relates to giving the right sets of error messages:
1311 "case not in switch" vs "break statement used with OpenMP...". */
0e59b3fb
MM
1312 bool in_switch_statement_p;
1313
4f8163b1
MM
1314 /* TRUE if we are parsing a type-id in an expression context. In
1315 such a situation, both "type (expr)" and "type (type)" are valid
1316 alternatives. */
1317 bool in_type_id_in_expr_p;
1318
7d381002 1319 /* TRUE if we are currently in a header file where declarations are
03fd3f84 1320 implicitly extern "C". */
7d381002
MA
1321 bool implicit_extern_c;
1322
c162c75e
MA
1323 /* TRUE if strings in expressions should be translated to the execution
1324 character set. */
1325 bool translate_strings_p;
1326
a723baf1
MM
1327 /* If non-NULL, then we are parsing a construct where new type
1328 definitions are not permitted. The string stored here will be
1329 issued as an error message if a type is defined. */
1330 const char *type_definition_forbidden_message;
1331
8db1028e
NS
1332 /* A list of lists. The outer list is a stack, used for member
1333 functions of local classes. At each level there are two sub-list,
1334 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1335 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1336 TREE_VALUE's. The functions are chained in reverse declaration
1337 order.
1338
1339 The TREE_PURPOSE sublist contains those functions with default
1340 arguments that need post processing, and the TREE_VALUE sublist
1341 contains those functions with definitions that need post
1342 processing.
1343
1344 These lists can only be processed once the outermost class being
9bcb9aae 1345 defined is complete. */
a723baf1
MM
1346 tree unparsed_functions_queues;
1347
1348 /* The number of classes whose definitions are currently in
1349 progress. */
1350 unsigned num_classes_being_defined;
1351
1352 /* The number of template parameter lists that apply directly to the
1353 current declaration. */
1354 unsigned num_template_parameter_lists;
1355} cp_parser;
1356
a723baf1
MM
1357/* Prototypes. */
1358
1359/* Constructors and destructors. */
1360
1361static cp_parser *cp_parser_new
94edc4ab 1362 (void);
a723baf1 1363
21526606 1364/* Routines to parse various constructs.
a723baf1
MM
1365
1366 Those that return `tree' will return the error_mark_node (rather
1367 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1368 Sometimes, they will return an ordinary node if error-recovery was
34cd5ae7 1369 attempted, even though a parse error occurred. So, to check
a723baf1
MM
1370 whether or not a parse error occurred, you should always use
1371 cp_parser_error_occurred. If the construct is optional (indicated
1372 either by an `_opt' in the name of the function that does the
1373 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1374 the construct is not present. */
1375
1376/* Lexical conventions [gram.lex] */
1377
1378static tree cp_parser_identifier
94edc4ab 1379 (cp_parser *);
c162c75e
MA
1380static tree cp_parser_string_literal
1381 (cp_parser *, bool, bool);
a723baf1
MM
1382
1383/* Basic concepts [gram.basic] */
1384
1385static bool cp_parser_translation_unit
94edc4ab 1386 (cp_parser *);
a723baf1
MM
1387
1388/* Expressions [gram.expr] */
1389
1390static tree cp_parser_primary_expression
02ed62dd 1391 (cp_parser *, bool, bool, bool, cp_id_kind *);
a723baf1 1392static tree cp_parser_id_expression
fa6098f8 1393 (cp_parser *, bool, bool, bool *, bool, bool);
a723baf1 1394static tree cp_parser_unqualified_id
fa6098f8 1395 (cp_parser *, bool, bool, bool, bool);
a723baf1 1396static tree cp_parser_nested_name_specifier_opt
a668c6ad 1397 (cp_parser *, bool, bool, bool, bool);
a723baf1 1398static tree cp_parser_nested_name_specifier
a723baf1 1399 (cp_parser *, bool, bool, bool, bool);
a668c6ad
MM
1400static tree cp_parser_class_or_namespace_name
1401 (cp_parser *, bool, bool, bool, bool, bool);
a723baf1 1402static tree cp_parser_postfix_expression
93678513 1403 (cp_parser *, bool, bool);
7a3ea201
RH
1404static tree cp_parser_postfix_open_square_expression
1405 (cp_parser *, tree, bool);
1406static tree cp_parser_postfix_dot_deref_expression
1407 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
7efa3e22 1408static tree cp_parser_parenthesized_expression_list
93678513 1409 (cp_parser *, bool, bool, bool *);
a723baf1 1410static void cp_parser_pseudo_destructor_name
94edc4ab 1411 (cp_parser *, tree *, tree *);
a723baf1 1412static tree cp_parser_unary_expression
93678513 1413 (cp_parser *, bool, bool);
a723baf1 1414static enum tree_code cp_parser_unary_operator
94edc4ab 1415 (cp_token *);
a723baf1 1416static tree cp_parser_new_expression
94edc4ab 1417 (cp_parser *);
a723baf1 1418static tree cp_parser_new_placement
94edc4ab 1419 (cp_parser *);
a723baf1 1420static tree cp_parser_new_type_id
058b15c1
MM
1421 (cp_parser *, tree *);
1422static cp_declarator *cp_parser_new_declarator_opt
94edc4ab 1423 (cp_parser *);
058b15c1 1424static cp_declarator *cp_parser_direct_new_declarator
94edc4ab 1425 (cp_parser *);
a723baf1 1426static tree cp_parser_new_initializer
94edc4ab 1427 (cp_parser *);
a723baf1 1428static tree cp_parser_delete_expression
94edc4ab 1429 (cp_parser *);
21526606 1430static tree cp_parser_cast_expression
93678513 1431 (cp_parser *, bool, bool);
b8b94c5b 1432static tree cp_parser_binary_expression
93678513 1433 (cp_parser *, bool);
a723baf1 1434static tree cp_parser_question_colon_clause
94edc4ab 1435 (cp_parser *, tree);
a723baf1 1436static tree cp_parser_assignment_expression
93678513 1437 (cp_parser *, bool);
a723baf1 1438static enum tree_code cp_parser_assignment_operator_opt
94edc4ab 1439 (cp_parser *);
a723baf1 1440static tree cp_parser_expression
93678513 1441 (cp_parser *, bool);
a723baf1 1442static tree cp_parser_constant_expression
14d22dd6 1443 (cp_parser *, bool, bool *);
7a3ea201
RH
1444static tree cp_parser_builtin_offsetof
1445 (cp_parser *);
a723baf1
MM
1446
1447/* Statements [gram.stmt.stmt] */
1448
1449static void cp_parser_statement
bc4071dd 1450 (cp_parser *, tree, bool);
a723baf1 1451static tree cp_parser_labeled_statement
bc4071dd 1452 (cp_parser *, tree, bool);
a723baf1 1453static tree cp_parser_expression_statement
325c3691 1454 (cp_parser *, tree);
a723baf1 1455static tree cp_parser_compound_statement
325c3691 1456 (cp_parser *, tree, bool);
a723baf1 1457static void cp_parser_statement_seq_opt
325c3691 1458 (cp_parser *, tree);
a723baf1 1459static tree cp_parser_selection_statement
94edc4ab 1460 (cp_parser *);
a723baf1 1461static tree cp_parser_condition
94edc4ab 1462 (cp_parser *);
a723baf1 1463static tree cp_parser_iteration_statement
94edc4ab 1464 (cp_parser *);
a723baf1 1465static void cp_parser_for_init_statement
94edc4ab 1466 (cp_parser *);
a723baf1 1467static tree cp_parser_jump_statement
94edc4ab 1468 (cp_parser *);
a723baf1 1469static void cp_parser_declaration_statement
94edc4ab 1470 (cp_parser *);
a723baf1
MM
1471
1472static tree cp_parser_implicitly_scoped_statement
94edc4ab 1473 (cp_parser *);
a723baf1 1474static void cp_parser_already_scoped_statement
94edc4ab 1475 (cp_parser *);
a723baf1
MM
1476
1477/* Declarations [gram.dcl.dcl] */
1478
1479static void cp_parser_declaration_seq_opt
94edc4ab 1480 (cp_parser *);
a723baf1 1481static void cp_parser_declaration
94edc4ab 1482 (cp_parser *);
a723baf1 1483static void cp_parser_block_declaration
94edc4ab 1484 (cp_parser *, bool);
a723baf1 1485static void cp_parser_simple_declaration
94edc4ab 1486 (cp_parser *, bool);
62d1db17
MM
1487static void cp_parser_decl_specifier_seq
1488 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
a723baf1 1489static tree cp_parser_storage_class_specifier_opt
94edc4ab 1490 (cp_parser *);
a723baf1 1491static tree cp_parser_function_specifier_opt
62d1db17 1492 (cp_parser *, cp_decl_specifier_seq *);
a723baf1 1493static tree cp_parser_type_specifier
98ca843c 1494 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
62d1db17 1495 int *, bool *);
a723baf1 1496static tree cp_parser_simple_type_specifier
62d1db17 1497 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
a723baf1 1498static tree cp_parser_type_name
94edc4ab 1499 (cp_parser *);
a723baf1 1500static tree cp_parser_elaborated_type_specifier
94edc4ab 1501 (cp_parser *, bool, bool);
a723baf1 1502static tree cp_parser_enum_specifier
94edc4ab 1503 (cp_parser *);
a723baf1 1504static void cp_parser_enumerator_list
94edc4ab 1505 (cp_parser *, tree);
21526606 1506static void cp_parser_enumerator_definition
94edc4ab 1507 (cp_parser *, tree);
a723baf1 1508static tree cp_parser_namespace_name
94edc4ab 1509 (cp_parser *);
a723baf1 1510static void cp_parser_namespace_definition
94edc4ab 1511 (cp_parser *);
a723baf1 1512static void cp_parser_namespace_body
94edc4ab 1513 (cp_parser *);
a723baf1 1514static tree cp_parser_qualified_namespace_specifier
94edc4ab 1515 (cp_parser *);
a723baf1 1516static void cp_parser_namespace_alias_definition
94edc4ab 1517 (cp_parser *);
a723baf1 1518static void cp_parser_using_declaration
94edc4ab 1519 (cp_parser *);
a723baf1 1520static void cp_parser_using_directive
94edc4ab 1521 (cp_parser *);
a723baf1 1522static void cp_parser_asm_definition
94edc4ab 1523 (cp_parser *);
a723baf1 1524static void cp_parser_linkage_specification
94edc4ab 1525 (cp_parser *);
a723baf1
MM
1526
1527/* Declarators [gram.dcl.decl] */
1528
1529static tree cp_parser_init_declarator
62d1db17 1530 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
058b15c1 1531static cp_declarator *cp_parser_declarator
db86dd14 1532 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
058b15c1 1533static cp_declarator *cp_parser_direct_declarator
db86dd14 1534 (cp_parser *, cp_parser_declarator_kind, int *, bool);
a723baf1 1535static enum tree_code cp_parser_ptr_operator
3c01e5df
MM
1536 (cp_parser *, tree *, cp_cv_quals *);
1537static cp_cv_quals cp_parser_cv_qualifier_seq_opt
94edc4ab 1538 (cp_parser *);
a723baf1 1539static tree cp_parser_declarator_id
fa6098f8 1540 (cp_parser *, bool);
a723baf1 1541static tree cp_parser_type_id
94edc4ab 1542 (cp_parser *);
62d1db17 1543static void cp_parser_type_specifier_seq
d4113656 1544 (cp_parser *, bool, cp_decl_specifier_seq *);
058b15c1 1545static cp_parameter_declarator *cp_parser_parameter_declaration_clause
94edc4ab 1546 (cp_parser *);
058b15c1
MM
1547static cp_parameter_declarator *cp_parser_parameter_declaration_list
1548 (cp_parser *, bool *);
1549static cp_parameter_declarator *cp_parser_parameter_declaration
4bb8ca28 1550 (cp_parser *, bool, bool *);
a723baf1
MM
1551static void cp_parser_function_body
1552 (cp_parser *);
1553static tree cp_parser_initializer
39703eb9 1554 (cp_parser *, bool *, bool *);
a723baf1 1555static tree cp_parser_initializer_clause
39703eb9 1556 (cp_parser *, bool *);
4038c495 1557static VEC(constructor_elt,gc) *cp_parser_initializer_list
39703eb9 1558 (cp_parser *, bool *);
a723baf1
MM
1559
1560static bool cp_parser_ctor_initializer_opt_and_function_body
1561 (cp_parser *);
1562
1563/* Classes [gram.class] */
1564
1565static tree cp_parser_class_name
fc6a28d7 1566 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
a723baf1 1567static tree cp_parser_class_specifier
94edc4ab 1568 (cp_parser *);
a723baf1 1569static tree cp_parser_class_head
38b305d0 1570 (cp_parser *, bool *, tree *);
a723baf1 1571static enum tag_types cp_parser_class_key
94edc4ab 1572 (cp_parser *);
a723baf1 1573static void cp_parser_member_specification_opt
94edc4ab 1574 (cp_parser *);
a723baf1 1575static void cp_parser_member_declaration
94edc4ab 1576 (cp_parser *);
a723baf1 1577static tree cp_parser_pure_specifier
94edc4ab 1578 (cp_parser *);
a723baf1 1579static tree cp_parser_constant_initializer
94edc4ab 1580 (cp_parser *);
a723baf1
MM
1581
1582/* Derived classes [gram.class.derived] */
1583
1584static tree cp_parser_base_clause
94edc4ab 1585 (cp_parser *);
a723baf1 1586static tree cp_parser_base_specifier
94edc4ab 1587 (cp_parser *);
a723baf1
MM
1588
1589/* Special member functions [gram.special] */
1590
1591static tree cp_parser_conversion_function_id
94edc4ab 1592 (cp_parser *);
a723baf1 1593static tree cp_parser_conversion_type_id
94edc4ab 1594 (cp_parser *);
058b15c1 1595static cp_declarator *cp_parser_conversion_declarator_opt
94edc4ab 1596 (cp_parser *);
a723baf1 1597static bool cp_parser_ctor_initializer_opt
94edc4ab 1598 (cp_parser *);
a723baf1 1599static void cp_parser_mem_initializer_list
94edc4ab 1600 (cp_parser *);
a723baf1 1601static tree cp_parser_mem_initializer
94edc4ab 1602 (cp_parser *);
a723baf1 1603static tree cp_parser_mem_initializer_id
94edc4ab 1604 (cp_parser *);
a723baf1
MM
1605
1606/* Overloading [gram.over] */
1607
1608static tree cp_parser_operator_function_id
94edc4ab 1609 (cp_parser *);
a723baf1 1610static tree cp_parser_operator
94edc4ab 1611 (cp_parser *);
a723baf1
MM
1612
1613/* Templates [gram.temp] */
1614
1615static void cp_parser_template_declaration
94edc4ab 1616 (cp_parser *, bool);
a723baf1 1617static tree cp_parser_template_parameter_list
94edc4ab 1618 (cp_parser *);
a723baf1 1619static tree cp_parser_template_parameter
058b15c1 1620 (cp_parser *, bool *);
a723baf1 1621static tree cp_parser_type_parameter
94edc4ab 1622 (cp_parser *);
a723baf1 1623static tree cp_parser_template_id
a668c6ad 1624 (cp_parser *, bool, bool, bool);
a723baf1 1625static tree cp_parser_template_name
a668c6ad 1626 (cp_parser *, bool, bool, bool, bool *);
a723baf1 1627static tree cp_parser_template_argument_list
94edc4ab 1628 (cp_parser *);
a723baf1 1629static tree cp_parser_template_argument
94edc4ab 1630 (cp_parser *);
a723baf1 1631static void cp_parser_explicit_instantiation
94edc4ab 1632 (cp_parser *);
a723baf1 1633static void cp_parser_explicit_specialization
94edc4ab 1634 (cp_parser *);
a723baf1
MM
1635
1636/* Exception handling [gram.exception] */
1637
21526606 1638static tree cp_parser_try_block
94edc4ab 1639 (cp_parser *);
a723baf1 1640static bool cp_parser_function_try_block
94edc4ab 1641 (cp_parser *);
a723baf1 1642static void cp_parser_handler_seq
94edc4ab 1643 (cp_parser *);
a723baf1 1644static void cp_parser_handler
94edc4ab 1645 (cp_parser *);
a723baf1 1646static tree cp_parser_exception_declaration
94edc4ab 1647 (cp_parser *);
a723baf1 1648static tree cp_parser_throw_expression
94edc4ab 1649 (cp_parser *);
a723baf1 1650static tree cp_parser_exception_specification_opt
94edc4ab 1651 (cp_parser *);
a723baf1 1652static tree cp_parser_type_id_list
94edc4ab 1653 (cp_parser *);
a723baf1
MM
1654
1655/* GNU Extensions */
1656
1657static tree cp_parser_asm_specification_opt
94edc4ab 1658 (cp_parser *);
a723baf1 1659static tree cp_parser_asm_operand_list
94edc4ab 1660 (cp_parser *);
a723baf1 1661static tree cp_parser_asm_clobber_list
94edc4ab 1662 (cp_parser *);
a723baf1 1663static tree cp_parser_attributes_opt
94edc4ab 1664 (cp_parser *);
a723baf1 1665static tree cp_parser_attribute_list
94edc4ab 1666 (cp_parser *);
a723baf1 1667static bool cp_parser_extension_opt
94edc4ab 1668 (cp_parser *, int *);
a723baf1 1669static void cp_parser_label_declaration
94edc4ab 1670 (cp_parser *);
a723baf1 1671
bc4071dd
RH
1672enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1673static bool cp_parser_pragma
1674 (cp_parser *, enum pragma_context);
1675
e58a9aa1
ZL
1676/* Objective-C++ Productions */
1677
1678static tree cp_parser_objc_message_receiver
1679 (cp_parser *);
1680static tree cp_parser_objc_message_args
1681 (cp_parser *);
1682static tree cp_parser_objc_message_expression
1683 (cp_parser *);
1684static tree cp_parser_objc_encode_expression
1685 (cp_parser *);
c8094d83 1686static tree cp_parser_objc_defs_expression
e58a9aa1
ZL
1687 (cp_parser *);
1688static tree cp_parser_objc_protocol_expression
1689 (cp_parser *);
1690static tree cp_parser_objc_selector_expression
1691 (cp_parser *);
1692static tree cp_parser_objc_expression
1693 (cp_parser *);
1694static bool cp_parser_objc_selector_p
1695 (enum cpp_ttype);
1696static tree cp_parser_objc_selector
1697 (cp_parser *);
1698static tree cp_parser_objc_protocol_refs_opt
1699 (cp_parser *);
1700static void cp_parser_objc_declaration
1701 (cp_parser *);
1702static tree cp_parser_objc_statement
1703 (cp_parser *);
1704
a723baf1
MM
1705/* Utility Routines */
1706
1707static tree cp_parser_lookup_name
91b1ca65 1708 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
a723baf1 1709static tree cp_parser_lookup_name_simple
94edc4ab 1710 (cp_parser *, tree);
a723baf1
MM
1711static tree cp_parser_maybe_treat_template_as_class
1712 (tree, bool);
1713static bool cp_parser_check_declarator_template_parameters
058b15c1 1714 (cp_parser *, cp_declarator *);
a723baf1 1715static bool cp_parser_check_template_parameters
94edc4ab 1716 (cp_parser *, unsigned);
d6b4ea85
MM
1717static tree cp_parser_simple_cast_expression
1718 (cp_parser *);
a723baf1 1719static tree cp_parser_global_scope_opt
94edc4ab 1720 (cp_parser *, bool);
a723baf1
MM
1721static bool cp_parser_constructor_declarator_p
1722 (cp_parser *, bool);
1723static tree cp_parser_function_definition_from_specifiers_and_declarator
62d1db17 1724 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
a723baf1 1725static tree cp_parser_function_definition_after_declarator
94edc4ab 1726 (cp_parser *, bool);
a723baf1 1727static void cp_parser_template_declaration_after_export
94edc4ab 1728 (cp_parser *, bool);
a723baf1 1729static tree cp_parser_single_declaration
94edc4ab 1730 (cp_parser *, bool, bool *);
a723baf1 1731static tree cp_parser_functional_cast
94edc4ab 1732 (cp_parser *, tree);
4bb8ca28 1733static tree cp_parser_save_member_function_body
62d1db17 1734 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
ec75414f
MM
1735static tree cp_parser_enclosed_template_argument_list
1736 (cp_parser *);
8db1028e
NS
1737static void cp_parser_save_default_args
1738 (cp_parser *, tree);
a723baf1 1739static void cp_parser_late_parsing_for_member
94edc4ab 1740 (cp_parser *, tree);
a723baf1 1741static void cp_parser_late_parsing_default_args
8218bd34 1742 (cp_parser *, tree);
a723baf1 1743static tree cp_parser_sizeof_operand
94edc4ab 1744 (cp_parser *, enum rid);
a723baf1 1745static bool cp_parser_declares_only_class_p
94edc4ab 1746 (cp_parser *);
62d1db17
MM
1747static void cp_parser_set_storage_class
1748 (cp_decl_specifier_seq *, cp_storage_class);
98ca843c 1749static void cp_parser_set_decl_spec_type
62d1db17 1750 (cp_decl_specifier_seq *, tree, bool);
a723baf1 1751static bool cp_parser_friend_p
62d1db17 1752 (const cp_decl_specifier_seq *);
a723baf1 1753static cp_token *cp_parser_require
94edc4ab 1754 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1755static cp_token *cp_parser_require_keyword
94edc4ab 1756 (cp_parser *, enum rid, const char *);
21526606 1757static bool cp_parser_token_starts_function_definition_p
94edc4ab 1758 (cp_token *);
a723baf1
MM
1759static bool cp_parser_next_token_starts_class_definition_p
1760 (cp_parser *);
d17811fd
MM
1761static bool cp_parser_next_token_ends_template_argument_p
1762 (cp_parser *);
f4abade9
GB
1763static bool cp_parser_nth_token_starts_template_argument_list_p
1764 (cp_parser *, size_t);
a723baf1 1765static enum tag_types cp_parser_token_is_class_key
94edc4ab 1766 (cp_token *);
a723baf1
MM
1767static void cp_parser_check_class_key
1768 (enum tag_types, tree type);
37d407a1
KL
1769static void cp_parser_check_access_in_redeclaration
1770 (tree type);
a723baf1
MM
1771static bool cp_parser_optional_template_keyword
1772 (cp_parser *);
21526606 1773static void cp_parser_pre_parsed_nested_name_specifier
2050a1bb 1774 (cp_parser *);
a723baf1 1775static void cp_parser_cache_group
c162c75e 1776 (cp_parser *, enum cpp_ttype, unsigned);
21526606 1777static void cp_parser_parse_tentatively
94edc4ab 1778 (cp_parser *);
a723baf1 1779static void cp_parser_commit_to_tentative_parse
94edc4ab 1780 (cp_parser *);
a723baf1 1781static void cp_parser_abort_tentative_parse
94edc4ab 1782 (cp_parser *);
a723baf1 1783static bool cp_parser_parse_definitely
94edc4ab 1784 (cp_parser *);
f7b5ecd9 1785static inline bool cp_parser_parsing_tentatively
94edc4ab 1786 (cp_parser *);
0b16f8f4 1787static bool cp_parser_uncommitted_to_tentative_parse_p
94edc4ab 1788 (cp_parser *);
a723baf1 1789static void cp_parser_error
94edc4ab 1790 (cp_parser *, const char *);
4bb8ca28
MM
1791static void cp_parser_name_lookup_error
1792 (cp_parser *, tree, tree, const char *);
e5976695 1793static bool cp_parser_simulate_error
94edc4ab 1794 (cp_parser *);
a723baf1 1795static void cp_parser_check_type_definition
94edc4ab 1796 (cp_parser *);
560ad596 1797static void cp_parser_check_for_definition_in_return_type
fc6a28d7 1798 (cp_declarator *, tree);
ee43dab5
MM
1799static void cp_parser_check_for_invalid_template_id
1800 (cp_parser *, tree);
625cbf93
MM
1801static bool cp_parser_non_integral_constant_expression
1802 (cp_parser *, const char *);
2097b5f2
GB
1803static void cp_parser_diagnose_invalid_type_name
1804 (cp_parser *, tree, tree);
1805static bool cp_parser_parse_and_diagnose_invalid_type_name
8fbc5ae7 1806 (cp_parser *);
7efa3e22 1807static int cp_parser_skip_to_closing_parenthesis
a668c6ad 1808 (cp_parser *, bool, bool, bool);
a723baf1 1809static void cp_parser_skip_to_end_of_statement
94edc4ab 1810 (cp_parser *);
e0860732
MM
1811static void cp_parser_consume_semicolon_at_end_of_statement
1812 (cp_parser *);
a723baf1 1813static void cp_parser_skip_to_end_of_block_or_statement
94edc4ab 1814 (cp_parser *);
a723baf1
MM
1815static void cp_parser_skip_to_closing_brace
1816 (cp_parser *);
1817static void cp_parser_skip_until_found
94edc4ab 1818 (cp_parser *, enum cpp_ttype, const char *);
bc4071dd
RH
1819static void cp_parser_skip_to_pragma_eol
1820 (cp_parser*, cp_token *);
a723baf1 1821static bool cp_parser_error_occurred
94edc4ab 1822 (cp_parser *);
a723baf1 1823static bool cp_parser_allow_gnu_extensions_p
94edc4ab 1824 (cp_parser *);
a723baf1 1825static bool cp_parser_is_string_literal
94edc4ab 1826 (cp_token *);
21526606 1827static bool cp_parser_is_keyword
94edc4ab 1828 (cp_token *, enum rid);
2097b5f2
GB
1829static tree cp_parser_make_typename_type
1830 (cp_parser *, tree, tree);
a723baf1 1831
4de8668e 1832/* Returns nonzero if we are parsing tentatively. */
f7b5ecd9
MM
1833
1834static inline bool
94edc4ab 1835cp_parser_parsing_tentatively (cp_parser* parser)
f7b5ecd9
MM
1836{
1837 return parser->context->next != NULL;
1838}
1839
4de8668e 1840/* Returns nonzero if TOKEN is a string literal. */
a723baf1
MM
1841
1842static bool
94edc4ab 1843cp_parser_is_string_literal (cp_token* token)
a723baf1
MM
1844{
1845 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1846}
1847
4de8668e 1848/* Returns nonzero if TOKEN is the indicated KEYWORD. */
a723baf1
MM
1849
1850static bool
94edc4ab 1851cp_parser_is_keyword (cp_token* token, enum rid keyword)
a723baf1
MM
1852{
1853 return token->keyword == keyword;
1854}
1855
8ff24a79
MM
1856/* A minimum or maximum operator has been seen. As these are
1857 deprecated, issue a warning. */
1858
1859static inline void
1860cp_parser_warn_min_max (void)
1861{
1862 if (warn_deprecated && !in_system_header)
b323323f 1863 warning (OPT_Wdeprecated, "minimum/maximum operators are deprecated");
8ff24a79
MM
1864}
1865
2cfe82fe
ZW
1866/* If not parsing tentatively, issue a diagnostic of the form
1867 FILE:LINE: MESSAGE before TOKEN
1868 where TOKEN is the next token in the input stream. MESSAGE
1869 (specified by the caller) is usually of the form "expected
1870 OTHER-TOKEN". */
a723baf1
MM
1871
1872static void
94edc4ab 1873cp_parser_error (cp_parser* parser, const char* message)
a723baf1 1874{
e5976695 1875 if (!cp_parser_simulate_error (parser))
4bb8ca28 1876 {
2cfe82fe
ZW
1877 cp_token *token = cp_lexer_peek_token (parser->lexer);
1878 /* This diagnostic makes more sense if it is tagged to the line
1879 of the token we just peeked at. */
1880 cp_lexer_set_source_position_from_token (token);
bc4071dd 1881
0d63048c
MM
1882 if (token->type == CPP_PRAGMA)
1883 {
c8094d83 1884 error ("%<#pragma%> is not allowed here");
bc4071dd 1885 cp_parser_skip_to_pragma_eol (parser, token);
0d63048c
MM
1886 return;
1887 }
bc4071dd 1888
21526606 1889 c_parse_error (message,
5c832178
MM
1890 /* Because c_parser_error does not understand
1891 CPP_KEYWORD, keywords are treated like
1892 identifiers. */
21526606 1893 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
5c832178 1894 token->value);
4bb8ca28
MM
1895 }
1896}
1897
1898/* Issue an error about name-lookup failing. NAME is the
1899 IDENTIFIER_NODE DECL is the result of
1900 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1901 the thing that we hoped to find. */
1902
1903static void
1904cp_parser_name_lookup_error (cp_parser* parser,
1905 tree name,
1906 tree decl,
1907 const char* desired)
1908{
1909 /* If name lookup completely failed, tell the user that NAME was not
1910 declared. */
1911 if (decl == error_mark_node)
1912 {
1913 if (parser->scope && parser->scope != global_namespace)
2a13a625 1914 error ("%<%D::%D%> has not been declared",
4bb8ca28
MM
1915 parser->scope, name);
1916 else if (parser->scope == global_namespace)
2a13a625 1917 error ("%<::%D%> has not been declared", name);
c8094d83 1918 else if (parser->object_scope
b14454ba 1919 && !CLASS_TYPE_P (parser->object_scope))
2a13a625 1920 error ("request for member %qD in non-class type %qT",
b14454ba
MM
1921 name, parser->object_scope);
1922 else if (parser->object_scope)
c8094d83 1923 error ("%<%T::%D%> has not been declared",
b14454ba 1924 parser->object_scope, name);
4bb8ca28 1925 else
9e637a26 1926 error ("%qD has not been declared", name);
4bb8ca28
MM
1927 }
1928 else if (parser->scope && parser->scope != global_namespace)
2a13a625 1929 error ("%<%D::%D%> %s", parser->scope, name, desired);
4bb8ca28 1930 else if (parser->scope == global_namespace)
2a13a625 1931 error ("%<::%D%> %s", name, desired);
4bb8ca28 1932 else
2a13a625 1933 error ("%qD %s", name, desired);
a723baf1
MM
1934}
1935
1936/* If we are parsing tentatively, remember that an error has occurred
e5976695 1937 during this tentative parse. Returns true if the error was
77077b39 1938 simulated; false if a message should be issued by the caller. */
a723baf1 1939
e5976695 1940static bool
94edc4ab 1941cp_parser_simulate_error (cp_parser* parser)
a723baf1 1942{
0b16f8f4 1943 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
e5976695
MM
1944 {
1945 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1946 return true;
1947 }
1948 return false;
a723baf1
MM
1949}
1950
1951/* This function is called when a type is defined. If type
1952 definitions are forbidden at this point, an error message is
1953 issued. */
1954
1955static void
94edc4ab 1956cp_parser_check_type_definition (cp_parser* parser)
a723baf1
MM
1957{
1958 /* If types are forbidden here, issue a message. */
1959 if (parser->type_definition_forbidden_message)
1960 /* Use `%s' to print the string in case there are any escape
1961 characters in the message. */
1962 error ("%s", parser->type_definition_forbidden_message);
1963}
1964
fc6a28d7 1965/* This function is called when the DECLARATOR is processed. The TYPE
472c29c3 1966 was a type defined in the decl-specifiers. If it is invalid to
fc6a28d7
MM
1967 define a type in the decl-specifiers for DECLARATOR, an error is
1968 issued. */
560ad596
MM
1969
1970static void
058b15c1 1971cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
fc6a28d7 1972 tree type)
560ad596
MM
1973{
1974 /* [dcl.fct] forbids type definitions in return types.
1975 Unfortunately, it's not easy to know whether or not we are
1976 processing a return type until after the fact. */
1977 while (declarator
058b15c1
MM
1978 && (declarator->kind == cdk_pointer
1979 || declarator->kind == cdk_reference
1980 || declarator->kind == cdk_ptrmem))
1981 declarator = declarator->declarator;
560ad596 1982 if (declarator
fc6a28d7
MM
1983 && declarator->kind == cdk_function)
1984 {
1985 error ("new types may not be defined in a return type");
1986 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1987 type);
1988 }
560ad596
MM
1989}
1990
ee43dab5
MM
1991/* A type-specifier (TYPE) has been parsed which cannot be followed by
1992 "<" in any valid C++ program. If the next token is indeed "<",
1993 issue a message warning the user about what appears to be an
1994 invalid attempt to form a template-id. */
1995
1996static void
21526606 1997cp_parser_check_for_invalid_template_id (cp_parser* parser,
ee43dab5
MM
1998 tree type)
1999{
0c5e4866 2000 cp_token_position start = 0;
ee43dab5
MM
2001
2002 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2003 {
2004 if (TYPE_P (type))
2a13a625 2005 error ("%qT is not a template", type);
ee43dab5 2006 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2a13a625 2007 error ("%qE is not a template", type);
ee43dab5
MM
2008 else
2009 error ("invalid template-id");
2010 /* Remember the location of the invalid "<". */
0b16f8f4 2011 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
0c5e4866 2012 start = cp_lexer_token_position (parser->lexer, true);
ee43dab5
MM
2013 /* Consume the "<". */
2014 cp_lexer_consume_token (parser->lexer);
2015 /* Parse the template arguments. */
2016 cp_parser_enclosed_template_argument_list (parser);
da1d7781 2017 /* Permanently remove the invalid template arguments so that
ee43dab5 2018 this error message is not issued again. */
0c5e4866
NS
2019 if (start)
2020 cp_lexer_purge_tokens_after (parser->lexer, start);
ee43dab5
MM
2021 }
2022}
2023
625cbf93
MM
2024/* If parsing an integral constant-expression, issue an error message
2025 about the fact that THING appeared and return true. Otherwise,
93678513 2026 return false. In either case, set
c8094d83 2027 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
14d22dd6 2028
625cbf93
MM
2029static bool
2030cp_parser_non_integral_constant_expression (cp_parser *parser,
2031 const char *thing)
14d22dd6 2032{
93678513 2033 parser->non_integral_constant_expression_p = true;
625cbf93
MM
2034 if (parser->integral_constant_expression_p)
2035 {
2036 if (!parser->allow_non_integral_constant_expression_p)
2037 {
2038 error ("%s cannot appear in a constant-expression", thing);
2039 return true;
2040 }
625cbf93
MM
2041 }
2042 return false;
14d22dd6
MM
2043}
2044
0c88d886 2045/* Emit a diagnostic for an invalid type name. SCOPE is the
6ca2d67f
MM
2046 qualifying scope (or NULL, if none) for ID. This function commits
2047 to the current active tentative parse, if any. (Otherwise, the
2048 problematic construct might be encountered again later, resulting
2049 in duplicate error messages.) */
8fbc5ae7 2050
2097b5f2
GB
2051static void
2052cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
6c0cc713
GB
2053{
2054 tree decl, old_scope;
2097b5f2
GB
2055 /* Try to lookup the identifier. */
2056 old_scope = parser->scope;
2057 parser->scope = scope;
2058 decl = cp_parser_lookup_name_simple (parser, id);
2059 parser->scope = old_scope;
2060 /* If the lookup found a template-name, it means that the user forgot
c72a1a86 2061 to specify an argument list. Emit a useful error message. */
2097b5f2 2062 if (TREE_CODE (decl) == TEMPLATE_DECL)
2a13a625 2063 error ("invalid use of template-name %qE without an argument list",
6c0cc713 2064 decl);
91b1ca65 2065 else if (!parser->scope)
8fbc5ae7 2066 {
8fbc5ae7 2067 /* Issue an error message. */
2a13a625 2068 error ("%qE does not name a type", id);
8fbc5ae7
MM
2069 /* If we're in a template class, it's possible that the user was
2070 referring to a type from a base class. For example:
2071
2072 template <typename T> struct A { typedef T X; };
2073 template <typename T> struct B : public A<T> { X x; };
2074
2075 The user should have said "typename A<T>::X". */
32db39c0
PC
2076 if (processing_template_decl && current_class_type
2077 && TYPE_BINFO (current_class_type))
8fbc5ae7
MM
2078 {
2079 tree b;
2080
2081 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2082 b;
2083 b = TREE_CHAIN (b))
2084 {
2085 tree base_type = BINFO_TYPE (b);
21526606 2086 if (CLASS_TYPE_P (base_type)
1fb3244a 2087 && dependent_type_p (base_type))
8fbc5ae7
MM
2088 {
2089 tree field;
2090 /* Go from a particular instantiation of the
2091 template (which will have an empty TYPE_FIELDs),
2092 to the main version. */
353b4fc0 2093 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
8fbc5ae7
MM
2094 for (field = TYPE_FIELDS (base_type);
2095 field;
2096 field = TREE_CHAIN (field))
2097 if (TREE_CODE (field) == TYPE_DECL
2097b5f2 2098 && DECL_NAME (field) == id)
8fbc5ae7 2099 {
c4f73174 2100 inform ("(perhaps %<typename %T::%E%> was intended)",
0cbd7506 2101 BINFO_TYPE (b), id);
8fbc5ae7
MM
2102 break;
2103 }
2104 if (field)
2105 break;
2106 }
2107 }
2108 }
8fbc5ae7 2109 }
2097b5f2
GB
2110 /* Here we diagnose qualified-ids where the scope is actually correct,
2111 but the identifier does not resolve to a valid type name. */
91b1ca65 2112 else if (parser->scope != error_mark_node)
2097b5f2
GB
2113 {
2114 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2a13a625 2115 error ("%qE in namespace %qE does not name a type",
2097b5f2
GB
2116 id, parser->scope);
2117 else if (TYPE_P (parser->scope))
2fbe4889 2118 error ("%qE in class %qT does not name a type", id, parser->scope);
2097b5f2 2119 else
315fb5db 2120 gcc_unreachable ();
2097b5f2 2121 }
6ca2d67f 2122 cp_parser_commit_to_tentative_parse (parser);
2097b5f2 2123}
8fbc5ae7 2124
2097b5f2
GB
2125/* Check for a common situation where a type-name should be present,
2126 but is not, and issue a sensible error message. Returns true if an
2127 invalid type-name was detected.
21526606 2128
2097b5f2 2129 The situation handled by this function are variable declarations of the
21526606
EC
2130 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2131 Usually, `ID' should name a type, but if we got here it means that it
2097b5f2
GB
2132 does not. We try to emit the best possible error message depending on
2133 how exactly the id-expression looks like.
2134*/
2135
2136static bool
2137cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2138{
2139 tree id;
2140
2141 cp_parser_parse_tentatively (parser);
21526606 2142 id = cp_parser_id_expression (parser,
2097b5f2
GB
2143 /*template_keyword_p=*/false,
2144 /*check_dependency_p=*/true,
2145 /*template_p=*/NULL,
fa6098f8
MM
2146 /*declarator_p=*/true,
2147 /*optional_p=*/false);
2097b5f2
GB
2148 /* After the id-expression, there should be a plain identifier,
2149 otherwise this is not a simple variable declaration. Also, if
2150 the scope is dependent, we cannot do much. */
2151 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 2152 || (parser->scope && TYPE_P (parser->scope)
2097b5f2
GB
2153 && dependent_type_p (parser->scope)))
2154 {
2155 cp_parser_abort_tentative_parse (parser);
2156 return false;
2157 }
3590f0a6
MM
2158 if (!cp_parser_parse_definitely (parser)
2159 || TREE_CODE (id) != IDENTIFIER_NODE)
2097b5f2
GB
2160 return false;
2161
2097b5f2
GB
2162 /* Emit a diagnostic for the invalid type. */
2163 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2164 /* Skip to the end of the declaration; there's no point in
2165 trying to process it. */
2166 cp_parser_skip_to_end_of_block_or_statement (parser);
2167 return true;
8fbc5ae7
MM
2168}
2169
21526606 2170/* Consume tokens up to, and including, the next non-nested closing `)'.
7efa3e22
NS
2171 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2172 are doing error recovery. Returns -1 if OR_COMMA is true and we
2173 found an unnested comma. */
a723baf1 2174
7efa3e22
NS
2175static int
2176cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
21526606 2177 bool recovering,
a668c6ad
MM
2178 bool or_comma,
2179 bool consume_paren)
a723baf1 2180{
7efa3e22
NS
2181 unsigned paren_depth = 0;
2182 unsigned brace_depth = 0;
a723baf1 2183
0b16f8f4
VR
2184 if (recovering && !or_comma
2185 && cp_parser_uncommitted_to_tentative_parse_p (parser))
7efa3e22 2186 return 0;
21526606 2187
a723baf1
MM
2188 while (true)
2189 {
bc4071dd 2190 cp_token * token = cp_lexer_peek_token (parser->lexer);
21526606 2191
bc4071dd 2192 switch (token->type)
0173bb6f 2193 {
bc4071dd
RH
2194 case CPP_EOF:
2195 case CPP_PRAGMA_EOL:
2196 /* If we've run out of tokens, then there is no closing `)'. */
2197 return 0;
a723baf1 2198
bc4071dd
RH
2199 case CPP_SEMICOLON:
2200 /* This matches the processing in skip_to_end_of_statement. */
2201 if (!brace_depth)
2202 return 0;
2203 break;
21526606 2204
bc4071dd
RH
2205 case CPP_OPEN_BRACE:
2206 ++brace_depth;
0173bb6f 2207 break;
bc4071dd 2208 case CPP_CLOSE_BRACE:
a668c6ad 2209 if (!brace_depth--)
bc4071dd 2210 return 0;
0173bb6f 2211 break;
21526606 2212
bc4071dd
RH
2213 case CPP_COMMA:
2214 if (recovering && or_comma && !brace_depth && !paren_depth)
2215 return -1;
2216 break;
2217
2218 case CPP_OPEN_PAREN:
2219 if (!brace_depth)
7efa3e22 2220 ++paren_depth;
bc4071dd
RH
2221 break;
2222
2223 case CPP_CLOSE_PAREN:
2224 if (!brace_depth && !paren_depth--)
a668c6ad
MM
2225 {
2226 if (consume_paren)
2227 cp_lexer_consume_token (parser->lexer);
bc4071dd 2228 return 1;
a668c6ad 2229 }
bc4071dd
RH
2230 break;
2231
2232 default:
2233 break;
7efa3e22 2234 }
21526606 2235
a668c6ad
MM
2236 /* Consume the token. */
2237 cp_lexer_consume_token (parser->lexer);
a723baf1
MM
2238 }
2239}
2240
2241/* Consume tokens until we reach the end of the current statement.
2242 Normally, that will be just before consuming a `;'. However, if a
2243 non-nested `}' comes first, then we stop before consuming that. */
2244
2245static void
94edc4ab 2246cp_parser_skip_to_end_of_statement (cp_parser* parser)
a723baf1
MM
2247{
2248 unsigned nesting_depth = 0;
2249
2250 while (true)
2251 {
bc4071dd 2252 cp_token *token = cp_lexer_peek_token (parser->lexer);
a723baf1 2253
bc4071dd 2254 switch (token->type)
a723baf1 2255 {
bc4071dd
RH
2256 case CPP_EOF:
2257 case CPP_PRAGMA_EOL:
2258 /* If we've run out of tokens, stop. */
2259 return;
2260
2261 case CPP_SEMICOLON:
2262 /* If the next token is a `;', we have reached the end of the
2263 statement. */
2264 if (!nesting_depth)
2265 return;
2266 break;
2267
2268 case CPP_CLOSE_BRACE:
2269 /* If this is a non-nested '}', stop before consuming it.
a723baf1
MM
2270 That way, when confronted with something like:
2271
21526606 2272 { 3 + }
a723baf1 2273
bc4071dd 2274 we stop before consuming the closing '}', even though we
a723baf1
MM
2275 have not yet reached a `;'. */
2276 if (nesting_depth == 0)
bc4071dd
RH
2277 return;
2278
2279 /* If it is the closing '}' for a block that we have
a723baf1
MM
2280 scanned, stop -- but only after consuming the token.
2281 That way given:
2282
0cbd7506 2283 void f g () { ... }
a723baf1
MM
2284 typedef int I;
2285
2286 we will stop after the body of the erroneously declared
2287 function, but before consuming the following `typedef'
2288 declaration. */
2289 if (--nesting_depth == 0)
2290 {
2291 cp_lexer_consume_token (parser->lexer);
bc4071dd 2292 return;
a723baf1 2293 }
bc4071dd
RH
2294
2295 case CPP_OPEN_BRACE:
2296 ++nesting_depth;
2297 break;
2298
2299 default:
2300 break;
a723baf1 2301 }
bc4071dd 2302
a723baf1
MM
2303 /* Consume the token. */
2304 cp_lexer_consume_token (parser->lexer);
2305 }
2306}
2307
e0860732
MM
2308/* This function is called at the end of a statement or declaration.
2309 If the next token is a semicolon, it is consumed; otherwise, error
2310 recovery is attempted. */
2311
2312static void
2313cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2314{
2315 /* Look for the trailing `;'. */
2316 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2317 {
2318 /* If there is additional (erroneous) input, skip to the end of
2319 the statement. */
2320 cp_parser_skip_to_end_of_statement (parser);
2321 /* If the next token is now a `;', consume it. */
2322 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2323 cp_lexer_consume_token (parser->lexer);
2324 }
2325}
2326
a723baf1
MM
2327/* Skip tokens until we have consumed an entire block, or until we
2328 have consumed a non-nested `;'. */
2329
2330static void
94edc4ab 2331cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
a723baf1 2332{
8fe4d24b 2333 int nesting_depth = 0;
a723baf1 2334
8fe4d24b 2335 while (nesting_depth >= 0)
a723baf1 2336 {
8fe4d24b 2337 cp_token *token = cp_lexer_peek_token (parser->lexer);
c8094d83 2338
8fe4d24b 2339 switch (token->type)
a723baf1 2340 {
8fe4d24b 2341 case CPP_EOF:
bc4071dd 2342 case CPP_PRAGMA_EOL:
8fe4d24b 2343 /* If we've run out of tokens, stop. */
bc4071dd 2344 return;
8fe4d24b
NS
2345
2346 case CPP_SEMICOLON:
2347 /* Stop if this is an unnested ';'. */
2348 if (!nesting_depth)
2349 nesting_depth = -1;
2350 break;
2351
2352 case CPP_CLOSE_BRACE:
2353 /* Stop if this is an unnested '}', or closes the outermost
2354 nesting level. */
2355 nesting_depth--;
2356 if (!nesting_depth)
2357 nesting_depth = -1;
2358 break;
c8094d83 2359
8fe4d24b
NS
2360 case CPP_OPEN_BRACE:
2361 /* Nest. */
2362 nesting_depth++;
2363 break;
2364
2365 default:
a723baf1
MM
2366 break;
2367 }
c8094d83 2368
a723baf1 2369 /* Consume the token. */
8fe4d24b 2370 cp_lexer_consume_token (parser->lexer);
a723baf1
MM
2371 }
2372}
2373
2374/* Skip tokens until a non-nested closing curly brace is the next
2375 token. */
2376
2377static void
2378cp_parser_skip_to_closing_brace (cp_parser *parser)
2379{
2380 unsigned nesting_depth = 0;
2381
2382 while (true)
2383 {
bc4071dd
RH
2384 cp_token *token = cp_lexer_peek_token (parser->lexer);
2385
2386 switch (token->type)
2387 {
2388 case CPP_EOF:
2389 case CPP_PRAGMA_EOL:
2390 /* If we've run out of tokens, stop. */
2391 return;
2392
2393 case CPP_CLOSE_BRACE:
2394 /* If the next token is a non-nested `}', then we have reached
2395 the end of the current block. */
2396 if (nesting_depth-- == 0)
2397 return;
2398 break;
2399
2400 case CPP_OPEN_BRACE:
2401 /* If it the next token is a `{', then we are entering a new
2402 block. Consume the entire block. */
2403 ++nesting_depth;
2404 break;
2405
2406 default:
2407 break;
2408 }
a723baf1 2409
a723baf1
MM
2410 /* Consume the token. */
2411 cp_lexer_consume_token (parser->lexer);
2412 }
2413}
2414
bc4071dd
RH
2415/* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2416 parameter is the PRAGMA token, allowing us to purge the entire pragma
2417 sequence. */
2418
2419static void
2420cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2421{
2422 cp_token *token;
2423
2424 parser->lexer->in_pragma = false;
2425
2426 do
2427 token = cp_lexer_consume_token (parser->lexer);
2428 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2429
2430 /* Ensure that the pragma is not parsed again. */
2431 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2432}
2433
1799e5d5
RH
2434/* Require pragma end of line, resyncing with it as necessary. The
2435 arguments are as for cp_parser_skip_to_pragma_eol. */
2436
2437static void
2438cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2439{
2440 parser->lexer->in_pragma = false;
2441 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2442 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2443}
2444
2097b5f2
GB
2445/* This is a simple wrapper around make_typename_type. When the id is
2446 an unresolved identifier node, we can provide a superior diagnostic
2447 using cp_parser_diagnose_invalid_type_name. */
2448
2449static tree
2450cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
6c0cc713
GB
2451{
2452 tree result;
2453 if (TREE_CODE (id) == IDENTIFIER_NODE)
2454 {
fc6a28d7 2455 result = make_typename_type (scope, id, typename_type,
8da15291 2456 /*complain=*/tf_none);
6c0cc713
GB
2457 if (result == error_mark_node)
2458 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2459 return result;
2460 }
fc6a28d7 2461 return make_typename_type (scope, id, typename_type, tf_error);
2097b5f2
GB
2462}
2463
2464
a723baf1
MM
2465/* Create a new C++ parser. */
2466
2467static cp_parser *
94edc4ab 2468cp_parser_new (void)
a723baf1
MM
2469{
2470 cp_parser *parser;
17211ab5 2471 cp_lexer *lexer;
b8b94c5b 2472 unsigned i;
17211ab5
GK
2473
2474 /* cp_lexer_new_main is called before calling ggc_alloc because
2475 cp_lexer_new_main might load a PCH file. */
2476 lexer = cp_lexer_new_main ();
a723baf1 2477
b8b94c5b
PB
2478 /* Initialize the binops_by_token so that we can get the tree
2479 directly from the token. */
2480 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2481 binops_by_token[binops[i].token_type] = binops[i];
2482
99dd239f 2483 parser = GGC_CNEW (cp_parser);
17211ab5 2484 parser->lexer = lexer;
a723baf1
MM
2485 parser->context = cp_parser_context_new (NULL);
2486
2487 /* For now, we always accept GNU extensions. */
2488 parser->allow_gnu_extensions_p = 1;
2489
2490 /* The `>' token is a greater-than operator, not the end of a
2491 template-id. */
2492 parser->greater_than_is_operator_p = true;
2493
2494 parser->default_arg_ok_p = true;
21526606 2495
a723baf1 2496 /* We are not parsing a constant-expression. */
67c03833
JM
2497 parser->integral_constant_expression_p = false;
2498 parser->allow_non_integral_constant_expression_p = false;
2499 parser->non_integral_constant_expression_p = false;
a723baf1
MM
2500
2501 /* Local variable names are not forbidden. */
2502 parser->local_variables_forbidden_p = false;
2503
34cd5ae7 2504 /* We are not processing an `extern "C"' declaration. */
a723baf1
MM
2505 parser->in_unbraced_linkage_specification_p = false;
2506
2507 /* We are not processing a declarator. */
2508 parser->in_declarator_p = false;
2509
4bb8ca28
MM
2510 /* We are not processing a template-argument-list. */
2511 parser->in_template_argument_list_p = false;
2512
0e59b3fb 2513 /* We are not in an iteration statement. */
1799e5d5 2514 parser->in_statement = 0;
0e59b3fb
MM
2515
2516 /* We are not in a switch statement. */
2517 parser->in_switch_statement_p = false;
2518
4f8163b1
MM
2519 /* We are not parsing a type-id inside an expression. */
2520 parser->in_type_id_in_expr_p = false;
2521
03fd3f84 2522 /* Declarations aren't implicitly extern "C". */
7d381002
MA
2523 parser->implicit_extern_c = false;
2524
c162c75e
MA
2525 /* String literals should be translated to the execution character set. */
2526 parser->translate_strings_p = true;
2527
a723baf1
MM
2528 /* The unparsed function queue is empty. */
2529 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2530
2531 /* There are no classes being defined. */
2532 parser->num_classes_being_defined = 0;
2533
2534 /* No template parameters apply. */
2535 parser->num_template_parameter_lists = 0;
2536
2537 return parser;
2538}
2539
2cfe82fe
ZW
2540/* Create a cp_lexer structure which will emit the tokens in CACHE
2541 and push it onto the parser's lexer stack. This is used for delayed
2542 parsing of in-class method bodies and default arguments, and should
2543 not be confused with tentative parsing. */
2544static void
2545cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2546{
2547 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2548 lexer->next = parser->lexer;
2549 parser->lexer = lexer;
2550
2551 /* Move the current source position to that of the first token in the
2552 new lexer. */
2553 cp_lexer_set_source_position_from_token (lexer->next_token);
2554}
2555
2556/* Pop the top lexer off the parser stack. This is never used for the
2557 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2558static void
2559cp_parser_pop_lexer (cp_parser *parser)
2560{
2561 cp_lexer *lexer = parser->lexer;
2562 parser->lexer = lexer->next;
2563 cp_lexer_destroy (lexer);
2564
2565 /* Put the current source position back where it was before this
2566 lexer was pushed. */
2567 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2568}
2569
a723baf1
MM
2570/* Lexical conventions [gram.lex] */
2571
2572/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2573 identifier. */
2574
21526606 2575static tree
94edc4ab 2576cp_parser_identifier (cp_parser* parser)
a723baf1
MM
2577{
2578 cp_token *token;
2579
2580 /* Look for the identifier. */
2581 token = cp_parser_require (parser, CPP_NAME, "identifier");
2582 /* Return the value. */
2583 return token ? token->value : error_mark_node;
2584}
2585
c162c75e
MA
2586/* Parse a sequence of adjacent string constants. Returns a
2587 TREE_STRING representing the combined, nul-terminated string
2588 constant. If TRANSLATE is true, translate the string to the
2589 execution character set. If WIDE_OK is true, a wide string is
2590 invalid here.
2591
2592 C++98 [lex.string] says that if a narrow string literal token is
2593 adjacent to a wide string literal token, the behavior is undefined.
2594 However, C99 6.4.5p4 says that this results in a wide string literal.
2595 We follow C99 here, for consistency with the C front end.
2596
2597 This code is largely lifted from lex_string() in c-lex.c.
2598
2599 FUTURE: ObjC++ will need to handle @-strings here. */
2600static tree
2601cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2602{
2603 tree value;
2604 bool wide = false;
2605 size_t count;
2606 struct obstack str_ob;
2607 cpp_string str, istr, *strs;
2608 cp_token *tok;
2609
2610 tok = cp_lexer_peek_token (parser->lexer);
2611 if (!cp_parser_is_string_literal (tok))
2612 {
2613 cp_parser_error (parser, "expected string-literal");
2614 return error_mark_node;
2615 }
2616
9688c3b8 2617 /* Try to avoid the overhead of creating and destroying an obstack
c162c75e 2618 for the common case of just one string. */
2cfe82fe
ZW
2619 if (!cp_parser_is_string_literal
2620 (cp_lexer_peek_nth_token (parser->lexer, 2)))
c162c75e 2621 {
2cfe82fe
ZW
2622 cp_lexer_consume_token (parser->lexer);
2623
c162c75e
MA
2624 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2625 str.len = TREE_STRING_LENGTH (tok->value);
2626 count = 1;
2627 if (tok->type == CPP_WSTRING)
2628 wide = true;
c162c75e
MA
2629
2630 strs = &str;
2631 }
2632 else
2633 {
2634 gcc_obstack_init (&str_ob);
2635 count = 0;
2636
2637 do
2638 {
2cfe82fe 2639 cp_lexer_consume_token (parser->lexer);
c162c75e
MA
2640 count++;
2641 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2642 str.len = TREE_STRING_LENGTH (tok->value);
2643 if (tok->type == CPP_WSTRING)
2644 wide = true;
2645
2646 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2647
2cfe82fe 2648 tok = cp_lexer_peek_token (parser->lexer);
c162c75e
MA
2649 }
2650 while (cp_parser_is_string_literal (tok));
2651
2652 strs = (cpp_string *) obstack_finish (&str_ob);
2653 }
2654
2655 if (wide && !wide_ok)
2656 {
2657 cp_parser_error (parser, "a wide string is invalid in this context");
2658 wide = false;
2659 }
2660
2661 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2662 (parse_in, strs, count, &istr, wide))
2663 {
2664 value = build_string (istr.len, (char *)istr.text);
2665 free ((void *)istr.text);
2666
2667 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2668 value = fix_string_type (value);
2669 }
2670 else
2671 /* cpp_interpret_string has issued an error. */
2672 value = error_mark_node;
2673
2674 if (count > 1)
2675 obstack_free (&str_ob, 0);
2676
2677 return value;
2678}
2679
2680
a723baf1
MM
2681/* Basic concepts [gram.basic] */
2682
2683/* Parse a translation-unit.
2684
2685 translation-unit:
21526606 2686 declaration-seq [opt]
a723baf1
MM
2687
2688 Returns TRUE if all went well. */
2689
2690static bool
94edc4ab 2691cp_parser_translation_unit (cp_parser* parser)
a723baf1 2692{
058b15c1
MM
2693 /* The address of the first non-permanent object on the declarator
2694 obstack. */
2695 static void *declarator_obstack_base;
2696
2697 bool success;
98ca843c 2698
058b15c1
MM
2699 /* Create the declarator obstack, if necessary. */
2700 if (!cp_error_declarator)
2701 {
2702 gcc_obstack_init (&declarator_obstack);
2703 /* Create the error declarator. */
2704 cp_error_declarator = make_declarator (cdk_error);
2705 /* Create the empty parameter list. */
62d1db17 2706 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
058b15c1
MM
2707 /* Remember where the base of the declarator obstack lies. */
2708 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2709 }
2710
a30efae8
GDR
2711 cp_parser_declaration_seq_opt (parser);
2712
2713 /* If there are no tokens left then all went well. */
2714 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
a723baf1 2715 {
a30efae8
GDR
2716 /* Get rid of the token array; we don't need it any more. */
2717 cp_lexer_destroy (parser->lexer);
2718 parser->lexer = NULL;
2719
2720 /* This file might have been a context that's implicitly extern
2721 "C". If so, pop the lang context. (Only relevant for PCH.) */
2722 if (parser->implicit_extern_c)
2723 {
2724 pop_lang_context ();
2725 parser->implicit_extern_c = false;
2726 }
2727
2728 /* Finish up. */
2729 finish_translation_unit ();
2730
2731 success = true;
a723baf1 2732 }
a30efae8
GDR
2733 else
2734 {
2735 cp_parser_error (parser, "expected declaration");
2736 success = false;
2737 }
2738
058b15c1 2739 /* Make sure the declarator obstack was fully cleaned up. */
50bc768d
NS
2740 gcc_assert (obstack_next_free (&declarator_obstack)
2741 == declarator_obstack_base);
a723baf1
MM
2742
2743 /* All went well. */
058b15c1 2744 return success;
a723baf1
MM
2745}
2746
2747/* Expressions [gram.expr] */
2748
2749/* Parse a primary-expression.
2750
2751 primary-expression:
2752 literal
2753 this
2754 ( expression )
2755 id-expression
2756
2757 GNU Extensions:
2758
2759 primary-expression:
2760 ( compound-statement )
2761 __builtin_va_arg ( assignment-expression , type-id )
93846d56 2762 __builtin_offsetof ( type-id , offsetof-expression )
a723baf1 2763
e58a9aa1
ZL
2764 Objective-C++ Extension:
2765
2766 primary-expression:
2767 objc-expression
2768
a723baf1
MM
2769 literal:
2770 __null
2771
02ed62dd
MM
2772 ADDRESS_P is true iff this expression was immediately preceded by
2773 "&" and therefore might denote a pointer-to-member. CAST_P is true
2774 iff this expression is the target of a cast. TEMPLATE_ARG_P is
bcf51da2 2775 true iff this expression is a template argument.
93678513 2776
02ed62dd
MM
2777 Returns a representation of the expression. Upon return, *IDK
2778 indicates what kind of id-expression (if any) was present. */
a723baf1
MM
2779
2780static tree
21526606 2781cp_parser_primary_expression (cp_parser *parser,
02ed62dd 2782 bool address_p,
93678513 2783 bool cast_p,
02ed62dd
MM
2784 bool template_arg_p,
2785 cp_id_kind *idk)
a723baf1
MM
2786{
2787 cp_token *token;
2788
2789 /* Assume the primary expression is not an id-expression. */
b3445994 2790 *idk = CP_ID_KIND_NONE;
a723baf1
MM
2791
2792 /* Peek at the next token. */
2793 token = cp_lexer_peek_token (parser->lexer);
2794 switch (token->type)
2795 {
2796 /* literal:
2797 integer-literal
2798 character-literal
2799 floating-literal
2800 string-literal
2801 boolean-literal */
2802 case CPP_CHAR:
2803 case CPP_WCHAR:
a723baf1
MM
2804 case CPP_NUMBER:
2805 token = cp_lexer_consume_token (parser->lexer);
93678513
MM
2806 /* Floating-point literals are only allowed in an integral
2807 constant expression if they are cast to an integral or
2808 enumeration type. */
2809 if (TREE_CODE (token->value) == REAL_CST
8c94c75a
MM
2810 && parser->integral_constant_expression_p
2811 && pedantic)
93678513
MM
2812 {
2813 /* CAST_P will be set even in invalid code like "int(2.7 +
2814 ...)". Therefore, we have to check that the next token
2815 is sure to end the cast. */
2816 if (cast_p)
2817 {
2818 cp_token *next_token;
2819
2820 next_token = cp_lexer_peek_token (parser->lexer);
2821 if (/* The comma at the end of an
2822 enumerator-definition. */
2823 next_token->type != CPP_COMMA
2824 /* The curly brace at the end of an enum-specifier. */
2825 && next_token->type != CPP_CLOSE_BRACE
2826 /* The end of a statement. */
2827 && next_token->type != CPP_SEMICOLON
2828 /* The end of the cast-expression. */
2829 && next_token->type != CPP_CLOSE_PAREN
2830 /* The end of an array bound. */
060e7327
MM
2831 && next_token->type != CPP_CLOSE_SQUARE
2832 /* The closing ">" in a template-argument-list. */
2833 && (next_token->type != CPP_GREATER
2834 || parser->greater_than_is_operator_p))
93678513
MM
2835 cast_p = false;
2836 }
2837
2838 /* If we are within a cast, then the constraint that the
2839 cast is to an integral or enumeration type will be
2840 checked at that point. If we are not within a cast, then
2841 this code is invalid. */
2842 if (!cast_p)
c8094d83 2843 cp_parser_non_integral_constant_expression
93678513
MM
2844 (parser, "floating-point literal");
2845 }
a723baf1
MM
2846 return token->value;
2847
0173bb6f
AO
2848 case CPP_STRING:
2849 case CPP_WSTRING:
c162c75e 2850 /* ??? Should wide strings be allowed when parser->translate_strings_p
0cbd7506 2851 is false (i.e. in attributes)? If not, we can kill the third
c162c75e
MA
2852 argument to cp_parser_string_literal. */
2853 return cp_parser_string_literal (parser,
2854 parser->translate_strings_p,
2855 true);
0173bb6f 2856
a723baf1
MM
2857 case CPP_OPEN_PAREN:
2858 {
2859 tree expr;
2860 bool saved_greater_than_is_operator_p;
2861
2862 /* Consume the `('. */
2863 cp_lexer_consume_token (parser->lexer);
2864 /* Within a parenthesized expression, a `>' token is always
2865 the greater-than operator. */
21526606 2866 saved_greater_than_is_operator_p
a723baf1
MM
2867 = parser->greater_than_is_operator_p;
2868 parser->greater_than_is_operator_p = true;
2869 /* If we see `( { ' then we are looking at the beginning of
2870 a GNU statement-expression. */
2871 if (cp_parser_allow_gnu_extensions_p (parser)
2872 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2873 {
2874 /* Statement-expressions are not allowed by the standard. */
2875 if (pedantic)
21526606
EC
2876 pedwarn ("ISO C++ forbids braced-groups within expressions");
2877
a723baf1
MM
2878 /* And they're not allowed outside of a function-body; you
2879 cannot, for example, write:
21526606 2880
0cbd7506 2881 int i = ({ int j = 3; j + 1; });
21526606 2882
a723baf1
MM
2883 at class or namespace scope. */
2884 if (!at_function_scope_p ())
2885 error ("statement-expressions are allowed only inside functions");
2886 /* Start the statement-expression. */
2887 expr = begin_stmt_expr ();
2888 /* Parse the compound-statement. */
325c3691 2889 cp_parser_compound_statement (parser, expr, false);
a723baf1 2890 /* Finish up. */
303b7406 2891 expr = finish_stmt_expr (expr, false);
a723baf1
MM
2892 }
2893 else
2894 {
2895 /* Parse the parenthesized expression. */
93678513 2896 expr = cp_parser_expression (parser, cast_p);
a723baf1
MM
2897 /* Let the front end know that this expression was
2898 enclosed in parentheses. This matters in case, for
2899 example, the expression is of the form `A::B', since
2900 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2901 not. */
2902 finish_parenthesized_expr (expr);
2903 }
2904 /* The `>' token might be the end of a template-id or
2905 template-parameter-list now. */
21526606 2906 parser->greater_than_is_operator_p
a723baf1
MM
2907 = saved_greater_than_is_operator_p;
2908 /* Consume the `)'. */
2909 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2910 cp_parser_skip_to_end_of_statement (parser);
2911
2912 return expr;
2913 }
2914
2915 case CPP_KEYWORD:
2916 switch (token->keyword)
2917 {
2918 /* These two are the boolean literals. */
2919 case RID_TRUE:
2920 cp_lexer_consume_token (parser->lexer);
2921 return boolean_true_node;
2922 case RID_FALSE:
2923 cp_lexer_consume_token (parser->lexer);
2924 return boolean_false_node;
21526606 2925
a723baf1
MM
2926 /* The `__null' literal. */
2927 case RID_NULL:
2928 cp_lexer_consume_token (parser->lexer);
2929 return null_node;
2930
2931 /* Recognize the `this' keyword. */
2932 case RID_THIS:
2933 cp_lexer_consume_token (parser->lexer);
2934 if (parser->local_variables_forbidden_p)
2935 {
2a13a625 2936 error ("%<this%> may not be used in this context");
a723baf1
MM
2937 return error_mark_node;
2938 }
14d22dd6 2939 /* Pointers cannot appear in constant-expressions. */
625cbf93
MM
2940 if (cp_parser_non_integral_constant_expression (parser,
2941 "`this'"))
2942 return error_mark_node;
a723baf1
MM
2943 return finish_this_expr ();
2944
2945 /* The `operator' keyword can be the beginning of an
2946 id-expression. */
2947 case RID_OPERATOR:
2948 goto id_expression;
2949
2950 case RID_FUNCTION_NAME:
2951 case RID_PRETTY_FUNCTION_NAME:
2952 case RID_C99_FUNCTION_NAME:
2953 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2954 __func__ are the names of variables -- but they are
2955 treated specially. Therefore, they are handled here,
2956 rather than relying on the generic id-expression logic
21526606 2957 below. Grammatically, these names are id-expressions.
a723baf1
MM
2958
2959 Consume the token. */
2960 token = cp_lexer_consume_token (parser->lexer);
2961 /* Look up the name. */
2962 return finish_fname (token->value);
2963
2964 case RID_VA_ARG:
2965 {
2966 tree expression;
2967 tree type;
2968
2969 /* The `__builtin_va_arg' construct is used to handle
2970 `va_arg'. Consume the `__builtin_va_arg' token. */
2971 cp_lexer_consume_token (parser->lexer);
2972 /* Look for the opening `('. */
2973 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2974 /* Now, parse the assignment-expression. */
93678513
MM
2975 expression = cp_parser_assignment_expression (parser,
2976 /*cast_p=*/false);
a723baf1
MM
2977 /* Look for the `,'. */
2978 cp_parser_require (parser, CPP_COMMA, "`,'");
2979 /* Parse the type-id. */
2980 type = cp_parser_type_id (parser);
2981 /* Look for the closing `)'. */
2982 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14d22dd6
MM
2983 /* Using `va_arg' in a constant-expression is not
2984 allowed. */
625cbf93
MM
2985 if (cp_parser_non_integral_constant_expression (parser,
2986 "`va_arg'"))
2987 return error_mark_node;
a723baf1
MM
2988 return build_x_va_arg (expression, type);
2989 }
2990
263ee052 2991 case RID_OFFSETOF:
7a3ea201 2992 return cp_parser_builtin_offsetof (parser);
263ee052 2993
e58a9aa1
ZL
2994 /* Objective-C++ expressions. */
2995 case RID_AT_ENCODE:
2996 case RID_AT_PROTOCOL:
2997 case RID_AT_SELECTOR:
2998 return cp_parser_objc_expression (parser);
2999
a723baf1
MM
3000 default:
3001 cp_parser_error (parser, "expected primary-expression");
3002 return error_mark_node;
3003 }
a723baf1
MM
3004
3005 /* An id-expression can start with either an identifier, a
3006 `::' as the beginning of a qualified-id, or the "operator"
3007 keyword. */
3008 case CPP_NAME:
3009 case CPP_SCOPE:
3010 case CPP_TEMPLATE_ID:
3011 case CPP_NESTED_NAME_SPECIFIER:
3012 {
3013 tree id_expression;
3014 tree decl;
b3445994 3015 const char *error_msg;
02ed62dd
MM
3016 bool template_p;
3017 bool done;
a723baf1
MM
3018
3019 id_expression:
3020 /* Parse the id-expression. */
21526606
EC
3021 id_expression
3022 = cp_parser_id_expression (parser,
a723baf1
MM
3023 /*template_keyword_p=*/false,
3024 /*check_dependency_p=*/true,
02ed62dd 3025 &template_p,
fa6098f8
MM
3026 /*declarator_p=*/false,
3027 /*optional_p=*/false);
a723baf1
MM
3028 if (id_expression == error_mark_node)
3029 return error_mark_node;
02ed62dd
MM
3030 token = cp_lexer_peek_token (parser->lexer);
3031 done = (token->type != CPP_OPEN_SQUARE
3032 && token->type != CPP_OPEN_PAREN
3033 && token->type != CPP_DOT
3034 && token->type != CPP_DEREF
3035 && token->type != CPP_PLUS_PLUS
3036 && token->type != CPP_MINUS_MINUS);
a723baf1
MM
3037 /* If we have a template-id, then no further lookup is
3038 required. If the template-id was for a template-class, we
3039 will sometimes have a TYPE_DECL at this point. */
02ed62dd
MM
3040 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3041 || TREE_CODE (id_expression) == TYPE_DECL)
a723baf1
MM
3042 decl = id_expression;
3043 /* Look up the name. */
21526606 3044 else
a723baf1 3045 {
91b1ca65 3046 tree ambiguous_decls;
8f78f01f
MM
3047
3048 decl = cp_parser_lookup_name (parser, id_expression,
fc6a28d7 3049 none_type,
02ed62dd 3050 template_p,
8f78f01f
MM
3051 /*is_namespace=*/false,
3052 /*check_dependency=*/true,
91b1ca65 3053 &ambiguous_decls);
8f78f01f
MM
3054 /* If the lookup was ambiguous, an error will already have
3055 been issued. */
91b1ca65 3056 if (ambiguous_decls)
8f78f01f 3057 return error_mark_node;
e58a9aa1
ZL
3058
3059 /* In Objective-C++, an instance variable (ivar) may be preferred
3060 to whatever cp_parser_lookup_name() found. */
3061 decl = objc_lookup_ivar (decl, id_expression);
3062
a723baf1 3063 /* If name lookup gives us a SCOPE_REF, then the
02ed62dd 3064 qualifying scope was dependent. */
a723baf1 3065 if (TREE_CODE (decl) == SCOPE_REF)
02ed62dd 3066 return decl;
a723baf1
MM
3067 /* Check to see if DECL is a local variable in a context
3068 where that is forbidden. */
3069 if (parser->local_variables_forbidden_p
3070 && local_variable_p (decl))
3071 {
3072 /* It might be that we only found DECL because we are
3073 trying to be generous with pre-ISO scoping rules.
3074 For example, consider:
3075
3076 int i;
3077 void g() {
3078 for (int i = 0; i < 10; ++i) {}
3079 extern void f(int j = i);
3080 }
3081
21526606 3082 Here, name look up will originally find the out
a723baf1
MM
3083 of scope `i'. We need to issue a warning message,
3084 but then use the global `i'. */
3085 decl = check_for_out_of_scope_variable (decl);
3086 if (local_variable_p (decl))
3087 {
2a13a625 3088 error ("local variable %qD may not appear in this context",
a723baf1
MM
3089 decl);
3090 return error_mark_node;
3091 }
3092 }
c006d942 3093 }
21526606 3094
02ed62dd
MM
3095 decl = (finish_id_expression
3096 (id_expression, decl, parser->scope,
3097 idk,
3098 parser->integral_constant_expression_p,
3099 parser->allow_non_integral_constant_expression_p,
3100 &parser->non_integral_constant_expression_p,
3101 template_p, done, address_p,
3102 template_arg_p,
3103 &error_msg));
b3445994
MM
3104 if (error_msg)
3105 cp_parser_error (parser, error_msg);
a723baf1
MM
3106 return decl;
3107 }
3108
3109 /* Anything else is an error. */
3110 default:
e58a9aa1 3111 /* ...unless we have an Objective-C++ message or string literal, that is. */
c8094d83 3112 if (c_dialect_objc ()
e58a9aa1
ZL
3113 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3114 return cp_parser_objc_expression (parser);
3115
a723baf1
MM
3116 cp_parser_error (parser, "expected primary-expression");
3117 return error_mark_node;
3118 }
3119}
3120
3121/* Parse an id-expression.
3122
3123 id-expression:
3124 unqualified-id
3125 qualified-id
3126
3127 qualified-id:
3128 :: [opt] nested-name-specifier template [opt] unqualified-id
3129 :: identifier
3130 :: operator-function-id
3131 :: template-id
3132
3133 Return a representation of the unqualified portion of the
3134 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3135 a `::' or nested-name-specifier.
3136
3137 Often, if the id-expression was a qualified-id, the caller will
3138 want to make a SCOPE_REF to represent the qualified-id. This
3139 function does not do this in order to avoid wastefully creating
3140 SCOPE_REFs when they are not required.
3141
a723baf1
MM
3142 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3143 `template' keyword.
3144
3145 If CHECK_DEPENDENCY_P is false, then names are looked up inside
21526606 3146 uninstantiated templates.
a723baf1 3147
15d2cb19 3148 If *TEMPLATE_P is non-NULL, it is set to true iff the
a723baf1 3149 `template' keyword is used to explicitly indicate that the entity
21526606 3150 named is a template.
f3c2dfc6
MM
3151
3152 If DECLARATOR_P is true, the id-expression is appearing as part of
cd0be382 3153 a declarator, rather than as part of an expression. */
a723baf1
MM
3154
3155static tree
3156cp_parser_id_expression (cp_parser *parser,
3157 bool template_keyword_p,
3158 bool check_dependency_p,
f3c2dfc6 3159 bool *template_p,
fa6098f8
MM
3160 bool declarator_p,
3161 bool optional_p)
a723baf1
MM
3162{
3163 bool global_scope_p;
3164 bool nested_name_specifier_p;
3165
3166 /* Assume the `template' keyword was not used. */
3167 if (template_p)
02ed62dd 3168 *template_p = template_keyword_p;
a723baf1
MM
3169
3170 /* Look for the optional `::' operator. */
21526606
EC
3171 global_scope_p
3172 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
a723baf1
MM
3173 != NULL_TREE);
3174 /* Look for the optional nested-name-specifier. */
21526606 3175 nested_name_specifier_p
a723baf1
MM
3176 = (cp_parser_nested_name_specifier_opt (parser,
3177 /*typename_keyword_p=*/false,
3178 check_dependency_p,
a668c6ad 3179 /*type_p=*/false,
a52eb3bc 3180 declarator_p)
a723baf1
MM
3181 != NULL_TREE);
3182 /* If there is a nested-name-specifier, then we are looking at
3183 the first qualified-id production. */
3184 if (nested_name_specifier_p)
3185 {
3186 tree saved_scope;
3187 tree saved_object_scope;
3188 tree saved_qualifying_scope;
3189 tree unqualified_id;
3190 bool is_template;
3191
3192 /* See if the next token is the `template' keyword. */
3193 if (!template_p)
3194 template_p = &is_template;
3195 *template_p = cp_parser_optional_template_keyword (parser);
3196 /* Name lookup we do during the processing of the
3197 unqualified-id might obliterate SCOPE. */
3198 saved_scope = parser->scope;
3199 saved_object_scope = parser->object_scope;
3200 saved_qualifying_scope = parser->qualifying_scope;
3201 /* Process the final unqualified-id. */
3202 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
f3c2dfc6 3203 check_dependency_p,
fa6098f8
MM
3204 declarator_p,
3205 /*optional_p=*/false);
a723baf1
MM
3206 /* Restore the SAVED_SCOPE for our caller. */
3207 parser->scope = saved_scope;
3208 parser->object_scope = saved_object_scope;
3209 parser->qualifying_scope = saved_qualifying_scope;
3210
3211 return unqualified_id;
3212 }
3213 /* Otherwise, if we are in global scope, then we are looking at one
3214 of the other qualified-id productions. */
3215 else if (global_scope_p)
3216 {
3217 cp_token *token;
3218 tree id;
3219
e5976695
MM
3220 /* Peek at the next token. */
3221 token = cp_lexer_peek_token (parser->lexer);
3222
3223 /* If it's an identifier, and the next token is not a "<", then
3224 we can avoid the template-id case. This is an optimization
3225 for this common case. */
21526606
EC
3226 if (token->type == CPP_NAME
3227 && !cp_parser_nth_token_starts_template_argument_list_p
f4abade9 3228 (parser, 2))
e5976695
MM
3229 return cp_parser_identifier (parser);
3230
a723baf1
MM
3231 cp_parser_parse_tentatively (parser);
3232 /* Try a template-id. */
21526606 3233 id = cp_parser_template_id (parser,
a723baf1 3234 /*template_keyword_p=*/false,
a668c6ad
MM
3235 /*check_dependency_p=*/true,
3236 declarator_p);
a723baf1
MM
3237 /* If that worked, we're done. */
3238 if (cp_parser_parse_definitely (parser))
3239 return id;
3240
e5976695
MM
3241 /* Peek at the next token. (Changes in the token buffer may
3242 have invalidated the pointer obtained above.) */
a723baf1
MM
3243 token = cp_lexer_peek_token (parser->lexer);
3244
3245 switch (token->type)
3246 {
3247 case CPP_NAME:
3248 return cp_parser_identifier (parser);
3249
3250 case CPP_KEYWORD:
3251 if (token->keyword == RID_OPERATOR)
3252 return cp_parser_operator_function_id (parser);
3253 /* Fall through. */
21526606 3254
a723baf1
MM
3255 default:
3256 cp_parser_error (parser, "expected id-expression");
3257 return error_mark_node;
3258 }
3259 }
3260 else
3261 return cp_parser_unqualified_id (parser, template_keyword_p,
f3c2dfc6 3262 /*check_dependency_p=*/true,
fa6098f8
MM
3263 declarator_p,
3264 optional_p);
a723baf1
MM
3265}
3266
3267/* Parse an unqualified-id.
3268
3269 unqualified-id:
3270 identifier
3271 operator-function-id
3272 conversion-function-id
3273 ~ class-name
3274 template-id
3275
3276 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3277 keyword, in a construct like `A::template ...'.
3278
3279 Returns a representation of unqualified-id. For the `identifier'
3280 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3281 production a BIT_NOT_EXPR is returned; the operand of the
3282 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3283 other productions, see the documentation accompanying the
3284 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
f3c2dfc6
MM
3285 names are looked up in uninstantiated templates. If DECLARATOR_P
3286 is true, the unqualified-id is appearing as part of a declarator,
3287 rather than as part of an expression. */
a723baf1
MM
3288
3289static tree
21526606 3290cp_parser_unqualified_id (cp_parser* parser,
0cbd7506 3291 bool template_keyword_p,
f3c2dfc6 3292 bool check_dependency_p,
fa6098f8
MM
3293 bool declarator_p,
3294 bool optional_p)
a723baf1
MM
3295{
3296 cp_token *token;
3297
3298 /* Peek at the next token. */
3299 token = cp_lexer_peek_token (parser->lexer);
21526606 3300
a723baf1
MM
3301 switch (token->type)
3302 {
3303 case CPP_NAME:
3304 {
3305 tree id;
3306
3307 /* We don't know yet whether or not this will be a
3308 template-id. */
3309 cp_parser_parse_tentatively (parser);
3310 /* Try a template-id. */
3311 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3312 check_dependency_p,
3313 declarator_p);
a723baf1
MM
3314 /* If it worked, we're done. */
3315 if (cp_parser_parse_definitely (parser))
3316 return id;
3317 /* Otherwise, it's an ordinary identifier. */
3318 return cp_parser_identifier (parser);
3319 }
3320
3321 case CPP_TEMPLATE_ID:
3322 return cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3323 check_dependency_p,
3324 declarator_p);
a723baf1
MM
3325
3326 case CPP_COMPL:
3327 {
3328 tree type_decl;
3329 tree qualifying_scope;
3330 tree object_scope;
3331 tree scope;
88e95ee3 3332 bool done;
a723baf1
MM
3333
3334 /* Consume the `~' token. */
3335 cp_lexer_consume_token (parser->lexer);
3336 /* Parse the class-name. The standard, as written, seems to
3337 say that:
3338
3339 template <typename T> struct S { ~S (); };
3340 template <typename T> S<T>::~S() {}
3341
0cbd7506 3342 is invalid, since `~' must be followed by a class-name, but
a723baf1
MM
3343 `S<T>' is dependent, and so not known to be a class.
3344 That's not right; we need to look in uninstantiated
3345 templates. A further complication arises from:
3346
3347 template <typename T> void f(T t) {
3348 t.T::~T();
21526606 3349 }
a723baf1
MM
3350
3351 Here, it is not possible to look up `T' in the scope of `T'
3352 itself. We must look in both the current scope, and the
21526606 3353 scope of the containing complete expression.
a723baf1
MM
3354
3355 Yet another issue is:
3356
0cbd7506
MS
3357 struct S {
3358 int S;
3359 ~S();
3360 };
a723baf1 3361
0cbd7506 3362 S::~S() {}
a723baf1 3363
0cbd7506 3364 The standard does not seem to say that the `S' in `~S'
a723baf1
MM
3365 should refer to the type `S' and not the data member
3366 `S::S'. */
3367
3368 /* DR 244 says that we look up the name after the "~" in the
3369 same scope as we looked up the qualifying name. That idea
3370 isn't fully worked out; it's more complicated than that. */
3371 scope = parser->scope;
3372 object_scope = parser->object_scope;
3373 qualifying_scope = parser->qualifying_scope;
3374
3375 /* If the name is of the form "X::~X" it's OK. */
3376 if (scope && TYPE_P (scope)
3377 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 3378 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1 3379 == CPP_OPEN_PAREN)
21526606 3380 && (cp_lexer_peek_token (parser->lexer)->value
a723baf1
MM
3381 == TYPE_IDENTIFIER (scope)))
3382 {
3383 cp_lexer_consume_token (parser->lexer);
3384 return build_nt (BIT_NOT_EXPR, scope);
3385 }
3386
3387 /* If there was an explicit qualification (S::~T), first look
3388 in the scope given by the qualification (i.e., S). */
88e95ee3 3389 done = false;
e3754f9c 3390 type_decl = NULL_TREE;
a723baf1
MM
3391 if (scope)
3392 {
3393 cp_parser_parse_tentatively (parser);
21526606 3394 type_decl = cp_parser_class_name (parser,
a723baf1
MM
3395 /*typename_keyword_p=*/false,
3396 /*template_keyword_p=*/false,
fc6a28d7 3397 none_type,
a723baf1 3398 /*check_dependency=*/false,
a668c6ad
MM
3399 /*class_head_p=*/false,
3400 declarator_p);
a723baf1 3401 if (cp_parser_parse_definitely (parser))
88e95ee3 3402 done = true;
a723baf1
MM
3403 }
3404 /* In "N::S::~S", look in "N" as well. */
88e95ee3 3405 if (!done && scope && qualifying_scope)
a723baf1
MM
3406 {
3407 cp_parser_parse_tentatively (parser);
3408 parser->scope = qualifying_scope;
3409 parser->object_scope = NULL_TREE;
3410 parser->qualifying_scope = NULL_TREE;
21526606
EC
3411 type_decl
3412 = cp_parser_class_name (parser,
a723baf1
MM
3413 /*typename_keyword_p=*/false,
3414 /*template_keyword_p=*/false,
fc6a28d7 3415 none_type,
a723baf1 3416 /*check_dependency=*/false,
a668c6ad
MM
3417 /*class_head_p=*/false,
3418 declarator_p);
a723baf1 3419 if (cp_parser_parse_definitely (parser))
88e95ee3 3420 done = true;
a723baf1
MM
3421 }
3422 /* In "p->S::~T", look in the scope given by "*p" as well. */
88e95ee3 3423 else if (!done && object_scope)
a723baf1
MM
3424 {
3425 cp_parser_parse_tentatively (parser);
3426 parser->scope = object_scope;
3427 parser->object_scope = NULL_TREE;
3428 parser->qualifying_scope = NULL_TREE;
21526606
EC
3429 type_decl
3430 = cp_parser_class_name (parser,
a723baf1
MM
3431 /*typename_keyword_p=*/false,
3432 /*template_keyword_p=*/false,
fc6a28d7 3433 none_type,
a723baf1 3434 /*check_dependency=*/false,
a668c6ad
MM
3435 /*class_head_p=*/false,
3436 declarator_p);
a723baf1 3437 if (cp_parser_parse_definitely (parser))
88e95ee3 3438 done = true;
a723baf1
MM
3439 }
3440 /* Look in the surrounding context. */
88e95ee3
MM
3441 if (!done)
3442 {
3443 parser->scope = NULL_TREE;
3444 parser->object_scope = NULL_TREE;
3445 parser->qualifying_scope = NULL_TREE;
3446 type_decl
3447 = cp_parser_class_name (parser,
3448 /*typename_keyword_p=*/false,
3449 /*template_keyword_p=*/false,
3450 none_type,
3451 /*check_dependency=*/false,
3452 /*class_head_p=*/false,
3453 declarator_p);
3454 }
a723baf1
MM
3455 /* If an error occurred, assume that the name of the
3456 destructor is the same as the name of the qualifying
3457 class. That allows us to keep parsing after running
3458 into ill-formed destructor names. */
3459 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3460 return build_nt (BIT_NOT_EXPR, scope);
3461 else if (type_decl == error_mark_node)
3462 return error_mark_node;
3463
1b3d28a8
VR
3464 /* Check that destructor name and scope match. */
3465 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3466 {
3467 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3468 error ("declaration of %<~%T%> as member of %qT",
3469 type_decl, scope);
3470 return error_mark_node;
3471 }
3472
f3c2dfc6
MM
3473 /* [class.dtor]
3474
3475 A typedef-name that names a class shall not be used as the
3476 identifier in the declarator for a destructor declaration. */
21526606 3477 if (declarator_p
f3c2dfc6 3478 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
33a69702
VR
3479 && !DECL_SELF_REFERENCE_P (type_decl)
3480 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
2a13a625 3481 error ("typedef-name %qD used as destructor declarator",
f3c2dfc6
MM
3482 type_decl);
3483
a723baf1
MM
3484 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3485 }
3486
3487 case CPP_KEYWORD:
3488 if (token->keyword == RID_OPERATOR)
3489 {
3490 tree id;
3491
3492 /* This could be a template-id, so we try that first. */
3493 cp_parser_parse_tentatively (parser);
3494 /* Try a template-id. */
3495 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3496 /*check_dependency_p=*/true,
3497 declarator_p);
a723baf1
MM
3498 /* If that worked, we're done. */
3499 if (cp_parser_parse_definitely (parser))
3500 return id;
3501 /* We still don't know whether we're looking at an
3502 operator-function-id or a conversion-function-id. */
3503 cp_parser_parse_tentatively (parser);
3504 /* Try an operator-function-id. */
3505 id = cp_parser_operator_function_id (parser);
3506 /* If that didn't work, try a conversion-function-id. */
3507 if (!cp_parser_parse_definitely (parser))
3508 id = cp_parser_conversion_function_id (parser);
3509
3510 return id;
3511 }
3512 /* Fall through. */
3513
3514 default:
fa6098f8
MM
3515 if (optional_p)
3516 return NULL_TREE;
a723baf1
MM
3517 cp_parser_error (parser, "expected unqualified-id");
3518 return error_mark_node;
3519 }
3520}
3521
3522/* Parse an (optional) nested-name-specifier.
3523
3524 nested-name-specifier:
3525 class-or-namespace-name :: nested-name-specifier [opt]
3526 class-or-namespace-name :: template nested-name-specifier [opt]
3527
3528 PARSER->SCOPE should be set appropriately before this function is
3529 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3530 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3531 in name lookups.
3532
3533 Sets PARSER->SCOPE to the class (TYPE) or namespace
3534 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3535 it unchanged if there is no nested-name-specifier. Returns the new
21526606 3536 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
a668c6ad
MM
3537
3538 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3539 part of a declaration and/or decl-specifier. */
a723baf1
MM
3540
3541static tree
21526606
EC
3542cp_parser_nested_name_specifier_opt (cp_parser *parser,
3543 bool typename_keyword_p,
a723baf1 3544 bool check_dependency_p,
a668c6ad
MM
3545 bool type_p,
3546 bool is_declaration)
a723baf1
MM
3547{
3548 bool success = false;
0c5e4866
NS
3549 cp_token_position start = 0;
3550 cp_token *token;
a723baf1
MM
3551
3552 /* If the next token corresponds to a nested name specifier, there
2050a1bb 3553 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
21526606 3554 false, it may have been true before, in which case something
2050a1bb
MM
3555 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3556 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3557 CHECK_DEPENDENCY_P is false, we have to fall through into the
3558 main loop. */
3559 if (check_dependency_p
3560 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3561 {
3562 cp_parser_pre_parsed_nested_name_specifier (parser);
a723baf1
MM
3563 return parser->scope;
3564 }
3565
3566 /* Remember where the nested-name-specifier starts. */
0b16f8f4 3567 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
19e65f30
MM
3568 {
3569 start = cp_lexer_token_position (parser->lexer, false);
3570 push_deferring_access_checks (dk_deferred);
3571 }
cf22909c 3572
a723baf1
MM
3573 while (true)
3574 {
3575 tree new_scope;
3576 tree old_scope;
3577 tree saved_qualifying_scope;
a723baf1
MM
3578 bool template_keyword_p;
3579
2050a1bb
MM
3580 /* Spot cases that cannot be the beginning of a
3581 nested-name-specifier. */
3582 token = cp_lexer_peek_token (parser->lexer);
3583
3584 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3585 the already parsed nested-name-specifier. */
3586 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3587 {
3588 /* Grab the nested-name-specifier and continue the loop. */
3589 cp_parser_pre_parsed_nested_name_specifier (parser);
3590 success = true;
3591 continue;
3592 }
3593
a723baf1
MM
3594 /* Spot cases that cannot be the beginning of a
3595 nested-name-specifier. On the second and subsequent times
3596 through the loop, we look for the `template' keyword. */
f7b5ecd9 3597 if (success && token->keyword == RID_TEMPLATE)
a723baf1
MM
3598 ;
3599 /* A template-id can start a nested-name-specifier. */
f7b5ecd9 3600 else if (token->type == CPP_TEMPLATE_ID)
a723baf1
MM
3601 ;
3602 else
3603 {
3604 /* If the next token is not an identifier, then it is
3605 definitely not a class-or-namespace-name. */
f7b5ecd9 3606 if (token->type != CPP_NAME)
a723baf1
MM
3607 break;
3608 /* If the following token is neither a `<' (to begin a
3609 template-id), nor a `::', then we are not looking at a
3610 nested-name-specifier. */
3611 token = cp_lexer_peek_nth_token (parser->lexer, 2);
f4abade9
GB
3612 if (token->type != CPP_SCOPE
3613 && !cp_parser_nth_token_starts_template_argument_list_p
3614 (parser, 2))
a723baf1
MM
3615 break;
3616 }
3617
3618 /* The nested-name-specifier is optional, so we parse
3619 tentatively. */
3620 cp_parser_parse_tentatively (parser);
3621
3622 /* Look for the optional `template' keyword, if this isn't the
3623 first time through the loop. */
3624 if (success)
3625 template_keyword_p = cp_parser_optional_template_keyword (parser);
3626 else
3627 template_keyword_p = false;
3628
3629 /* Save the old scope since the name lookup we are about to do
3630 might destroy it. */
3631 old_scope = parser->scope;
3632 saved_qualifying_scope = parser->qualifying_scope;
a52eb3bc
MM
3633 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3634 look up names in "X<T>::I" in order to determine that "Y" is
3635 a template. So, if we have a typename at this point, we make
3636 an effort to look through it. */
c8094d83 3637 if (is_declaration
67bcc252 3638 && !typename_keyword_p
c8094d83 3639 && parser->scope
a52eb3bc 3640 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
c8094d83 3641 parser->scope = resolve_typename_type (parser->scope,
a52eb3bc 3642 /*only_current_p=*/false);
a723baf1 3643 /* Parse the qualifying entity. */
21526606 3644 new_scope
a723baf1
MM
3645 = cp_parser_class_or_namespace_name (parser,
3646 typename_keyword_p,
3647 template_keyword_p,
3648 check_dependency_p,
a668c6ad
MM
3649 type_p,
3650 is_declaration);
a723baf1
MM
3651 /* Look for the `::' token. */
3652 cp_parser_require (parser, CPP_SCOPE, "`::'");
3653
3654 /* If we found what we wanted, we keep going; otherwise, we're
3655 done. */
3656 if (!cp_parser_parse_definitely (parser))
3657 {
3658 bool error_p = false;
3659
3660 /* Restore the OLD_SCOPE since it was valid before the
3661 failed attempt at finding the last
3662 class-or-namespace-name. */
3663 parser->scope = old_scope;
3664 parser->qualifying_scope = saved_qualifying_scope;
3665 /* If the next token is an identifier, and the one after
3666 that is a `::', then any valid interpretation would have
3667 found a class-or-namespace-name. */
3668 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 3669 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1 3670 == CPP_SCOPE)
21526606 3671 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
a723baf1
MM
3672 != CPP_COMPL))
3673 {
3674 token = cp_lexer_consume_token (parser->lexer);
21526606 3675 if (!error_p)
a723baf1 3676 {
91b1ca65
MM
3677 if (!token->ambiguous_p)
3678 {
3679 tree decl;
3680 tree ambiguous_decls;
3681
3682 decl = cp_parser_lookup_name (parser, token->value,
3683 none_type,
3684 /*is_template=*/false,
3685 /*is_namespace=*/false,
3686 /*check_dependency=*/true,
3687 &ambiguous_decls);
3688 if (TREE_CODE (decl) == TEMPLATE_DECL)
3689 error ("%qD used without template parameters", decl);
3690 else if (ambiguous_decls)
3691 {
3692 error ("reference to %qD is ambiguous",
3693 token->value);
3694 print_candidates (ambiguous_decls);
3695 decl = error_mark_node;
3696 }
3697 else
3698 cp_parser_name_lookup_error
3699 (parser, token->value, decl,
3700 "is not a class or namespace");
3701 }
3702 parser->scope = error_mark_node;
a723baf1 3703 error_p = true;
eea9800f
MM
3704 /* Treat this as a successful nested-name-specifier
3705 due to:
3706
3707 [basic.lookup.qual]
3708
3709 If the name found is not a class-name (clause
3710 _class_) or namespace-name (_namespace.def_), the
3711 program is ill-formed. */
3712 success = true;
a723baf1
MM
3713 }
3714 cp_lexer_consume_token (parser->lexer);
3715 }
3716 break;
3717 }
a723baf1
MM
3718 /* We've found one valid nested-name-specifier. */
3719 success = true;
02ed62dd
MM
3720 /* Name lookup always gives us a DECL. */
3721 if (TREE_CODE (new_scope) == TYPE_DECL)
3722 new_scope = TREE_TYPE (new_scope);
3723 /* Uses of "template" must be followed by actual templates. */
3724 if (template_keyword_p
3725 && !(CLASS_TYPE_P (new_scope)
3726 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3727 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3728 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3729 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3730 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3731 == TEMPLATE_ID_EXPR)))
3732 pedwarn (TYPE_P (new_scope)
3733 ? "%qT is not a template"
3734 : "%qD is not a template",
3735 new_scope);
a723baf1
MM
3736 /* If it is a class scope, try to complete it; we are about to
3737 be looking up names inside the class. */
02ed62dd 3738 if (TYPE_P (new_scope)
8fbc5ae7
MM
3739 /* Since checking types for dependency can be expensive,
3740 avoid doing it if the type is already complete. */
02ed62dd 3741 && !COMPLETE_TYPE_P (new_scope)
8fbc5ae7 3742 /* Do not try to complete dependent types. */
02ed62dd
MM
3743 && !dependent_type_p (new_scope))
3744 new_scope = complete_type (new_scope);
3745 /* Make sure we look in the right scope the next time through
3746 the loop. */
3747 parser->scope = new_scope;
a723baf1
MM
3748 }
3749
3750 /* If parsing tentatively, replace the sequence of tokens that makes
3751 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3752 token. That way, should we re-parse the token stream, we will
3753 not have to repeat the effort required to do the parse, nor will
3754 we issue duplicate error messages. */
0c5e4866 3755 if (success && start)
a723baf1 3756 {
19e65f30
MM
3757 cp_token *token;
3758 tree access_checks;
c8094d83 3759
19e65f30 3760 token = cp_lexer_token_at (parser->lexer, start);
a723baf1
MM
3761 /* Reset the contents of the START token. */
3762 token->type = CPP_NESTED_NAME_SPECIFIER;
19e65f30
MM
3763 /* Retrieve any deferred checks. Do not pop this access checks yet
3764 so the memory will not be reclaimed during token replacing below. */
3765 access_checks = get_deferred_access_checks ();
3766 token->value = build_tree_list (copy_list (access_checks),
3767 parser->scope);
a723baf1
MM
3768 TREE_TYPE (token->value) = parser->qualifying_scope;
3769 token->keyword = RID_MAX;
c8094d83 3770
a723baf1 3771 /* Purge all subsequent tokens. */
0c5e4866 3772 cp_lexer_purge_tokens_after (parser->lexer, start);
a723baf1 3773 }
19e65f30
MM
3774
3775 if (start)
3776 pop_to_parent_deferring_access_checks ();
a723baf1
MM
3777
3778 return success ? parser->scope : NULL_TREE;
3779}
3780
3781/* Parse a nested-name-specifier. See
3782 cp_parser_nested_name_specifier_opt for details. This function
3783 behaves identically, except that it will an issue an error if no
8fe4d24b 3784 nested-name-specifier is present. */
a723baf1
MM
3785
3786static tree
21526606
EC
3787cp_parser_nested_name_specifier (cp_parser *parser,
3788 bool typename_keyword_p,
a723baf1 3789 bool check_dependency_p,
a668c6ad
MM
3790 bool type_p,
3791 bool is_declaration)
a723baf1
MM
3792{
3793 tree scope;
3794
3795 /* Look for the nested-name-specifier. */
3796 scope = cp_parser_nested_name_specifier_opt (parser,
3797 typename_keyword_p,
3798 check_dependency_p,
a668c6ad
MM
3799 type_p,
3800 is_declaration);
a723baf1
MM
3801 /* If it was not present, issue an error message. */
3802 if (!scope)
3803 {
3804 cp_parser_error (parser, "expected nested-name-specifier");
eb5abb39 3805 parser->scope = NULL_TREE;
a723baf1
MM
3806 }
3807
3808 return scope;
3809}
3810
3811/* Parse a class-or-namespace-name.
3812
3813 class-or-namespace-name:
3814 class-name
3815 namespace-name
3816
3817 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3818 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3819 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3820 TYPE_P is TRUE iff the next name should be taken as a class-name,
3821 even the same name is declared to be another entity in the same
3822 scope.
3823
3824 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
eea9800f
MM
3825 specified by the class-or-namespace-name. If neither is found the
3826 ERROR_MARK_NODE is returned. */
a723baf1
MM
3827
3828static tree
21526606 3829cp_parser_class_or_namespace_name (cp_parser *parser,
a723baf1
MM
3830 bool typename_keyword_p,
3831 bool template_keyword_p,
3832 bool check_dependency_p,
a668c6ad
MM
3833 bool type_p,
3834 bool is_declaration)
a723baf1
MM
3835{
3836 tree saved_scope;
3837 tree saved_qualifying_scope;
3838 tree saved_object_scope;
3839 tree scope;
eea9800f 3840 bool only_class_p;
a723baf1 3841
a723baf1
MM
3842 /* Before we try to parse the class-name, we must save away the
3843 current PARSER->SCOPE since cp_parser_class_name will destroy
3844 it. */
3845 saved_scope = parser->scope;
3846 saved_qualifying_scope = parser->qualifying_scope;
3847 saved_object_scope = parser->object_scope;
eea9800f
MM
3848 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3849 there is no need to look for a namespace-name. */
bbaab916 3850 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
eea9800f
MM
3851 if (!only_class_p)
3852 cp_parser_parse_tentatively (parser);
21526606 3853 scope = cp_parser_class_name (parser,
a723baf1
MM
3854 typename_keyword_p,
3855 template_keyword_p,
fc6a28d7 3856 type_p ? class_type : none_type,
a723baf1 3857 check_dependency_p,
a668c6ad
MM
3858 /*class_head_p=*/false,
3859 is_declaration);
a723baf1 3860 /* If that didn't work, try for a namespace-name. */
eea9800f 3861 if (!only_class_p && !cp_parser_parse_definitely (parser))
a723baf1
MM
3862 {
3863 /* Restore the saved scope. */
3864 parser->scope = saved_scope;
3865 parser->qualifying_scope = saved_qualifying_scope;
3866 parser->object_scope = saved_object_scope;
eea9800f
MM
3867 /* If we are not looking at an identifier followed by the scope
3868 resolution operator, then this is not part of a
3869 nested-name-specifier. (Note that this function is only used
3870 to parse the components of a nested-name-specifier.) */
3871 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3872 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3873 return error_mark_node;
a723baf1
MM
3874 scope = cp_parser_namespace_name (parser);
3875 }
3876
3877 return scope;
3878}
3879
3880/* Parse a postfix-expression.
3881
3882 postfix-expression:
3883 primary-expression
3884 postfix-expression [ expression ]
3885 postfix-expression ( expression-list [opt] )
3886 simple-type-specifier ( expression-list [opt] )
21526606 3887 typename :: [opt] nested-name-specifier identifier
a723baf1
MM
3888 ( expression-list [opt] )
3889 typename :: [opt] nested-name-specifier template [opt] template-id
3890 ( expression-list [opt] )
3891 postfix-expression . template [opt] id-expression
3892 postfix-expression -> template [opt] id-expression
3893 postfix-expression . pseudo-destructor-name
3894 postfix-expression -> pseudo-destructor-name
3895 postfix-expression ++
3896 postfix-expression --
3897 dynamic_cast < type-id > ( expression )
3898 static_cast < type-id > ( expression )
3899 reinterpret_cast < type-id > ( expression )
3900 const_cast < type-id > ( expression )
3901 typeid ( expression )
3902 typeid ( type-id )
3903
3904 GNU Extension:
21526606 3905
a723baf1
MM
3906 postfix-expression:
3907 ( type-id ) { initializer-list , [opt] }
3908
3909 This extension is a GNU version of the C99 compound-literal
3910 construct. (The C99 grammar uses `type-name' instead of `type-id',
3911 but they are essentially the same concept.)
3912
3913 If ADDRESS_P is true, the postfix expression is the operand of the
93678513 3914 `&' operator. CAST_P is true if this expression is the target of a
c8094d83 3915 cast.
a723baf1
MM
3916
3917 Returns a representation of the expression. */
3918
3919static tree
93678513 3920cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
a723baf1
MM
3921{
3922 cp_token *token;
3923 enum rid keyword;
b3445994 3924 cp_id_kind idk = CP_ID_KIND_NONE;
a723baf1 3925 tree postfix_expression = NULL_TREE;
a723baf1
MM
3926
3927 /* Peek at the next token. */
3928 token = cp_lexer_peek_token (parser->lexer);
3929 /* Some of the productions are determined by keywords. */
3930 keyword = token->keyword;
3931 switch (keyword)
3932 {
3933 case RID_DYNCAST:
3934 case RID_STATCAST:
3935 case RID_REINTCAST:
3936 case RID_CONSTCAST:
3937 {
3938 tree type;
3939 tree expression;
3940 const char *saved_message;
3941
3942 /* All of these can be handled in the same way from the point
3943 of view of parsing. Begin by consuming the token
3944 identifying the cast. */
3945 cp_lexer_consume_token (parser->lexer);
21526606 3946
a723baf1
MM
3947 /* New types cannot be defined in the cast. */
3948 saved_message = parser->type_definition_forbidden_message;
3949 parser->type_definition_forbidden_message
3950 = "types may not be defined in casts";
3951
3952 /* Look for the opening `<'. */
3953 cp_parser_require (parser, CPP_LESS, "`<'");
3954 /* Parse the type to which we are casting. */
3955 type = cp_parser_type_id (parser);
3956 /* Look for the closing `>'. */
3957 cp_parser_require (parser, CPP_GREATER, "`>'");
3958 /* Restore the old message. */
3959 parser->type_definition_forbidden_message = saved_message;
3960
3961 /* And the expression which is being cast. */
3962 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
93678513 3963 expression = cp_parser_expression (parser, /*cast_p=*/true);
a723baf1
MM
3964 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3965
14d22dd6
MM
3966 /* Only type conversions to integral or enumeration types
3967 can be used in constant-expressions. */
67c03833 3968 if (parser->integral_constant_expression_p
14d22dd6 3969 && !dependent_type_p (type)
263ee052 3970 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
98ca843c 3971 && (cp_parser_non_integral_constant_expression
625cbf93
MM
3972 (parser,
3973 "a cast to a type other than an integral or "
3974 "enumeration type")))
3975 return error_mark_node;
14d22dd6 3976
a723baf1
MM
3977 switch (keyword)
3978 {
3979 case RID_DYNCAST:
3980 postfix_expression
3981 = build_dynamic_cast (type, expression);
3982 break;
3983 case RID_STATCAST:
3984 postfix_expression
3985 = build_static_cast (type, expression);
3986 break;
3987 case RID_REINTCAST:
3988 postfix_expression
3989 = build_reinterpret_cast (type, expression);
3990 break;
3991 case RID_CONSTCAST:
3992 postfix_expression
3993 = build_const_cast (type, expression);
3994 break;
3995 default:
315fb5db 3996 gcc_unreachable ();
a723baf1
MM
3997 }
3998 }
3999 break;
4000
4001 case RID_TYPEID:
4002 {
4003 tree type;
4004 const char *saved_message;
4f8163b1 4005 bool saved_in_type_id_in_expr_p;
a723baf1
MM
4006
4007 /* Consume the `typeid' token. */
4008 cp_lexer_consume_token (parser->lexer);
4009 /* Look for the `(' token. */
4010 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4011 /* Types cannot be defined in a `typeid' expression. */
4012 saved_message = parser->type_definition_forbidden_message;
4013 parser->type_definition_forbidden_message
4014 = "types may not be defined in a `typeid\' expression";
4015 /* We can't be sure yet whether we're looking at a type-id or an
4016 expression. */
4017 cp_parser_parse_tentatively (parser);
4018 /* Try a type-id first. */
4f8163b1
MM
4019 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4020 parser->in_type_id_in_expr_p = true;
a723baf1 4021 type = cp_parser_type_id (parser);
4f8163b1 4022 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
4023 /* Look for the `)' token. Otherwise, we can't be sure that
4024 we're not looking at an expression: consider `typeid (int
4025 (3))', for example. */
4026 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4027 /* If all went well, simply lookup the type-id. */
4028 if (cp_parser_parse_definitely (parser))
4029 postfix_expression = get_typeid (type);
4030 /* Otherwise, fall back to the expression variant. */
4031 else
4032 {
4033 tree expression;
4034
4035 /* Look for an expression. */
93678513 4036 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
4037 /* Compute its typeid. */
4038 postfix_expression = build_typeid (expression);
4039 /* Look for the `)' token. */
4040 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4041 }
4424e0da 4042 /* `typeid' may not appear in an integral constant expression. */
98ca843c 4043 if (cp_parser_non_integral_constant_expression(parser,
4424e0da
GB
4044 "`typeid' operator"))
4045 return error_mark_node;
a723baf1
MM
4046 /* Restore the saved message. */
4047 parser->type_definition_forbidden_message = saved_message;
4048 }
4049 break;
21526606 4050
a723baf1
MM
4051 case RID_TYPENAME:
4052 {
a723baf1 4053 tree type;
88a33c34
MM
4054 /* The syntax permitted here is the same permitted for an
4055 elaborated-type-specifier. */
4056 type = cp_parser_elaborated_type_specifier (parser,
4057 /*is_friend=*/false,
4058 /*is_declaration=*/false);
a723baf1
MM
4059 postfix_expression = cp_parser_functional_cast (parser, type);
4060 }
4061 break;
4062
4063 default:
4064 {
4065 tree type;
4066
4067 /* If the next thing is a simple-type-specifier, we may be
4068 looking at a functional cast. We could also be looking at
4069 an id-expression. So, we try the functional cast, and if
4070 that doesn't work we fall back to the primary-expression. */
4071 cp_parser_parse_tentatively (parser);
4072 /* Look for the simple-type-specifier. */
21526606 4073 type = cp_parser_simple_type_specifier (parser,
62d1db17
MM
4074 /*decl_specs=*/NULL,
4075 CP_PARSER_FLAGS_NONE);
a723baf1
MM
4076 /* Parse the cast itself. */
4077 if (!cp_parser_error_occurred (parser))
21526606 4078 postfix_expression
a723baf1
MM
4079 = cp_parser_functional_cast (parser, type);
4080 /* If that worked, we're done. */
4081 if (cp_parser_parse_definitely (parser))
4082 break;
4083
4084 /* If the functional-cast didn't work out, try a
4085 compound-literal. */
14d22dd6
MM
4086 if (cp_parser_allow_gnu_extensions_p (parser)
4087 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
a723baf1 4088 {
4038c495 4089 VEC(constructor_elt,gc) *initializer_list = NULL;
4f8163b1 4090 bool saved_in_type_id_in_expr_p;
a723baf1
MM
4091
4092 cp_parser_parse_tentatively (parser);
14d22dd6
MM
4093 /* Consume the `('. */
4094 cp_lexer_consume_token (parser->lexer);
4095 /* Parse the type. */
4f8163b1
MM
4096 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4097 parser->in_type_id_in_expr_p = true;
14d22dd6 4098 type = cp_parser_type_id (parser);
4f8163b1 4099 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14d22dd6
MM
4100 /* Look for the `)'. */
4101 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4102 /* Look for the `{'. */
4103 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4104 /* If things aren't going well, there's no need to
4105 keep going. */
4106 if (!cp_parser_error_occurred (parser))
a723baf1 4107 {
39703eb9 4108 bool non_constant_p;
14d22dd6 4109 /* Parse the initializer-list. */
21526606 4110 initializer_list
39703eb9 4111 = cp_parser_initializer_list (parser, &non_constant_p);
14d22dd6
MM
4112 /* Allow a trailing `,'. */
4113 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4114 cp_lexer_consume_token (parser->lexer);
4115 /* Look for the final `}'. */
4116 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1
MM
4117 }
4118 /* If that worked, we're definitely looking at a
4119 compound-literal expression. */
4120 if (cp_parser_parse_definitely (parser))
4121 {
4122 /* Warn the user that a compound literal is not
4123 allowed in standard C++. */
4124 if (pedantic)
4125 pedwarn ("ISO C++ forbids compound-literals");
4126 /* Form the representation of the compound-literal. */
21526606 4127 postfix_expression
a723baf1
MM
4128 = finish_compound_literal (type, initializer_list);
4129 break;
4130 }
4131 }
4132
4133 /* It must be a primary-expression. */
02ed62dd
MM
4134 postfix_expression
4135 = cp_parser_primary_expression (parser, address_p, cast_p,
4136 /*template_arg_p=*/false,
4137 &idk);
a723baf1
MM
4138 }
4139 break;
4140 }
4141
a723baf1
MM
4142 /* Keep looping until the postfix-expression is complete. */
4143 while (true)
4144 {
10b1d5e7
MM
4145 if (idk == CP_ID_KIND_UNQUALIFIED
4146 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
a723baf1 4147 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
b3445994 4148 /* It is not a Koenig lookup function call. */
21526606 4149 postfix_expression
b3445994 4150 = unqualified_name_lookup_error (postfix_expression);
21526606 4151
a723baf1
MM
4152 /* Peek at the next token. */
4153 token = cp_lexer_peek_token (parser->lexer);
4154
4155 switch (token->type)
4156 {
4157 case CPP_OPEN_SQUARE:
7a3ea201
RH
4158 postfix_expression
4159 = cp_parser_postfix_open_square_expression (parser,
4160 postfix_expression,
4161 false);
4162 idk = CP_ID_KIND_NONE;
a723baf1
MM
4163 break;
4164
4165 case CPP_OPEN_PAREN:
4166 /* postfix-expression ( expression-list [opt] ) */
4167 {
6d80c4b9 4168 bool koenig_p;
88a7beb7
MM
4169 bool is_builtin_constant_p;
4170 bool saved_integral_constant_expression_p = false;
4171 bool saved_non_integral_constant_expression_p = false;
4172 tree args;
4173
c8094d83 4174 is_builtin_constant_p
88a7beb7
MM
4175 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4176 if (is_builtin_constant_p)
4177 {
4178 /* The whole point of __builtin_constant_p is to allow
4179 non-constant expressions to appear as arguments. */
4180 saved_integral_constant_expression_p
4181 = parser->integral_constant_expression_p;
4182 saved_non_integral_constant_expression_p
4183 = parser->non_integral_constant_expression_p;
4184 parser->integral_constant_expression_p = false;
4185 }
4186 args = (cp_parser_parenthesized_expression_list
c8094d83 4187 (parser, /*is_attribute_list=*/false,
88a7beb7
MM
4188 /*cast_p=*/false,
4189 /*non_constant_p=*/NULL));
4190 if (is_builtin_constant_p)
4191 {
4192 parser->integral_constant_expression_p
4193 = saved_integral_constant_expression_p;
4194 parser->non_integral_constant_expression_p
4195 = saved_non_integral_constant_expression_p;
4196 }
a723baf1 4197
7efa3e22
NS
4198 if (args == error_mark_node)
4199 {
4200 postfix_expression = error_mark_node;
4201 break;
4202 }
21526606 4203
14d22dd6
MM
4204 /* Function calls are not permitted in
4205 constant-expressions. */
100d337a
MA
4206 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4207 && cp_parser_non_integral_constant_expression (parser,
4208 "a function call"))
14d22dd6 4209 {
625cbf93
MM
4210 postfix_expression = error_mark_node;
4211 break;
14d22dd6 4212 }
a723baf1 4213
6d80c4b9 4214 koenig_p = false;
399dedb9
NS
4215 if (idk == CP_ID_KIND_UNQUALIFIED)
4216 {
89d594a2
NS
4217 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4218 {
4219 if (args)
4220 {
4221 koenig_p = true;
4222 postfix_expression
4223 = perform_koenig_lookup (postfix_expression, args);
4224 }
4225 else
4226 postfix_expression
4227 = unqualified_fn_lookup_error (postfix_expression);
4228 }
676e33ca
MM
4229 /* We do not perform argument-dependent lookup if
4230 normal lookup finds a non-function, in accordance
4231 with the expected resolution of DR 218. */
89d594a2 4232 else if (args && is_overloaded_fn (postfix_expression))
6d80c4b9 4233 {
89d594a2
NS
4234 tree fn = get_first_fn (postfix_expression);
4235
4236 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4237 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4238
4239 /* Only do argument dependent lookup if regular
4240 lookup does not find a set of member functions.
4241 [basic.lookup.koenig]/2a */
4242 if (!DECL_FUNCTION_MEMBER_P (fn))
4243 {
4244 koenig_p = true;
4245 postfix_expression
4246 = perform_koenig_lookup (postfix_expression, args);
4247 }
6d80c4b9 4248 }
399dedb9 4249 }
21526606 4250
d17811fd 4251 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
a723baf1 4252 {
d17811fd
MM
4253 tree instance = TREE_OPERAND (postfix_expression, 0);
4254 tree fn = TREE_OPERAND (postfix_expression, 1);
4255
4256 if (processing_template_decl
4257 && (type_dependent_expression_p (instance)
4258 || (!BASELINK_P (fn)
4259 && TREE_CODE (fn) != FIELD_DECL)
584672ee 4260 || type_dependent_expression_p (fn)
d17811fd
MM
4261 || any_type_dependent_arguments_p (args)))
4262 {
4263 postfix_expression
6de9cd9a
DN
4264 = build_min_nt (CALL_EXPR, postfix_expression,
4265 args, NULL_TREE);
d17811fd
MM
4266 break;
4267 }
9f880ef9
MM
4268
4269 if (BASELINK_P (fn))
4270 postfix_expression
21526606
EC
4271 = (build_new_method_call
4272 (instance, fn, args, NULL_TREE,
4273 (idk == CP_ID_KIND_QUALIFIED
63c9a190
MM
4274 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4275 /*fn_p=*/NULL));
9f880ef9
MM
4276 else
4277 postfix_expression
4278 = finish_call_expr (postfix_expression, args,
4279 /*disallow_virtual=*/false,
4280 /*koenig_p=*/false);
a723baf1 4281 }
d17811fd
MM
4282 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4283 || TREE_CODE (postfix_expression) == MEMBER_REF
4284 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
a723baf1
MM
4285 postfix_expression = (build_offset_ref_call_from_tree
4286 (postfix_expression, args));
b3445994 4287 else if (idk == CP_ID_KIND_QUALIFIED)
2050a1bb
MM
4288 /* A call to a static class member, or a namespace-scope
4289 function. */
4290 postfix_expression
4291 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
4292 /*disallow_virtual=*/true,
4293 koenig_p);
a723baf1 4294 else
2050a1bb 4295 /* All other function calls. */
21526606
EC
4296 postfix_expression
4297 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
4298 /*disallow_virtual=*/false,
4299 koenig_p);
a723baf1
MM
4300
4301 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
b3445994 4302 idk = CP_ID_KIND_NONE;
a723baf1
MM
4303 }
4304 break;
21526606 4305
a723baf1
MM
4306 case CPP_DOT:
4307 case CPP_DEREF:
21526606
EC
4308 /* postfix-expression . template [opt] id-expression
4309 postfix-expression . pseudo-destructor-name
a723baf1
MM
4310 postfix-expression -> template [opt] id-expression
4311 postfix-expression -> pseudo-destructor-name */
98ca843c 4312
7a3ea201
RH
4313 /* Consume the `.' or `->' operator. */
4314 cp_lexer_consume_token (parser->lexer);
a723baf1 4315
7a3ea201
RH
4316 postfix_expression
4317 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4318 postfix_expression,
4319 false, &idk);
a723baf1
MM
4320 break;
4321
4322 case CPP_PLUS_PLUS:
4323 /* postfix-expression ++ */
4324 /* Consume the `++' token. */
4325 cp_lexer_consume_token (parser->lexer);
a5ac3982 4326 /* Generate a representation for the complete expression. */
21526606
EC
4327 postfix_expression
4328 = finish_increment_expr (postfix_expression,
a5ac3982 4329 POSTINCREMENT_EXPR);
14d22dd6 4330 /* Increments may not appear in constant-expressions. */
625cbf93
MM
4331 if (cp_parser_non_integral_constant_expression (parser,
4332 "an increment"))
4333 postfix_expression = error_mark_node;
b3445994 4334 idk = CP_ID_KIND_NONE;
a723baf1
MM
4335 break;
4336
4337 case CPP_MINUS_MINUS:
4338 /* postfix-expression -- */
4339 /* Consume the `--' token. */
4340 cp_lexer_consume_token (parser->lexer);
a5ac3982 4341 /* Generate a representation for the complete expression. */
21526606
EC
4342 postfix_expression
4343 = finish_increment_expr (postfix_expression,
a5ac3982 4344 POSTDECREMENT_EXPR);
14d22dd6 4345 /* Decrements may not appear in constant-expressions. */
625cbf93
MM
4346 if (cp_parser_non_integral_constant_expression (parser,
4347 "a decrement"))
4348 postfix_expression = error_mark_node;
b3445994 4349 idk = CP_ID_KIND_NONE;
a723baf1
MM
4350 break;
4351
4352 default:
4353 return postfix_expression;
4354 }
4355 }
4356
4357 /* We should never get here. */
315fb5db 4358 gcc_unreachable ();
a723baf1
MM
4359 return error_mark_node;
4360}
4361
7a3ea201
RH
4362/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4363 by cp_parser_builtin_offsetof. We're looking for
4364
4365 postfix-expression [ expression ]
4366
4367 FOR_OFFSETOF is set if we're being called in that context, which
4368 changes how we deal with integer constant expressions. */
4369
4370static tree
4371cp_parser_postfix_open_square_expression (cp_parser *parser,
4372 tree postfix_expression,
4373 bool for_offsetof)
4374{
4375 tree index;
4376
4377 /* Consume the `[' token. */
4378 cp_lexer_consume_token (parser->lexer);
4379
4380 /* Parse the index expression. */
4381 /* ??? For offsetof, there is a question of what to allow here. If
4382 offsetof is not being used in an integral constant expression context,
4383 then we *could* get the right answer by computing the value at runtime.
4384 If we are in an integral constant expression context, then we might
4385 could accept any constant expression; hard to say without analysis.
4386 Rather than open the barn door too wide right away, allow only integer
77880ae4 4387 constant expressions here. */
7a3ea201
RH
4388 if (for_offsetof)
4389 index = cp_parser_constant_expression (parser, false, NULL);
4390 else
93678513 4391 index = cp_parser_expression (parser, /*cast_p=*/false);
7a3ea201
RH
4392
4393 /* Look for the closing `]'. */
4394 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4395
4396 /* Build the ARRAY_REF. */
4397 postfix_expression = grok_array_decl (postfix_expression, index);
4398
4399 /* When not doing offsetof, array references are not permitted in
4400 constant-expressions. */
4401 if (!for_offsetof
4402 && (cp_parser_non_integral_constant_expression
4403 (parser, "an array reference")))
4404 postfix_expression = error_mark_node;
4405
4406 return postfix_expression;
4407}
4408
4409/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4410 by cp_parser_builtin_offsetof. We're looking for
4411
4412 postfix-expression . template [opt] id-expression
4413 postfix-expression . pseudo-destructor-name
4414 postfix-expression -> template [opt] id-expression
4415 postfix-expression -> pseudo-destructor-name
4416
4417 FOR_OFFSETOF is set if we're being called in that context. That sorta
4418 limits what of the above we'll actually accept, but nevermind.
4419 TOKEN_TYPE is the "." or "->" token, which will already have been
4420 removed from the stream. */
4421
4422static tree
4423cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4424 enum cpp_ttype token_type,
4425 tree postfix_expression,
4426 bool for_offsetof, cp_id_kind *idk)
4427{
4428 tree name;
4429 bool dependent_p;
17a27b4f 4430 bool pseudo_destructor_p;
7a3ea201
RH
4431 tree scope = NULL_TREE;
4432
4433 /* If this is a `->' operator, dereference the pointer. */
4434 if (token_type == CPP_DEREF)
4435 postfix_expression = build_x_arrow (postfix_expression);
4436 /* Check to see whether or not the expression is type-dependent. */
4437 dependent_p = type_dependent_expression_p (postfix_expression);
4438 /* The identifier following the `->' or `.' is not qualified. */
4439 parser->scope = NULL_TREE;
4440 parser->qualifying_scope = NULL_TREE;
4441 parser->object_scope = NULL_TREE;
4442 *idk = CP_ID_KIND_NONE;
4443 /* Enter the scope corresponding to the type of the object
4444 given by the POSTFIX_EXPRESSION. */
4445 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4446 {
4447 scope = TREE_TYPE (postfix_expression);
4448 /* According to the standard, no expression should ever have
4449 reference type. Unfortunately, we do not currently match
4450 the standard in this respect in that our internal representation
4451 of an expression may have reference type even when the standard
4452 says it does not. Therefore, we have to manually obtain the
4453 underlying type here. */
4454 scope = non_reference (scope);
4455 /* The type of the POSTFIX_EXPRESSION must be complete. */
e4ba2534
MM
4456 if (scope == unknown_type_node)
4457 {
4458 error ("%qE does not have class type", postfix_expression);
4459 scope = NULL_TREE;
4460 }
4461 else
4462 scope = complete_type_or_else (scope, NULL_TREE);
7a3ea201
RH
4463 /* Let the name lookup machinery know that we are processing a
4464 class member access expression. */
4465 parser->context->object_type = scope;
4466 /* If something went wrong, we want to be able to discern that case,
4467 as opposed to the case where there was no SCOPE due to the type
4468 of expression being dependent. */
4469 if (!scope)
4470 scope = error_mark_node;
4471 /* If the SCOPE was erroneous, make the various semantic analysis
4472 functions exit quickly -- and without issuing additional error
4473 messages. */
4474 if (scope == error_mark_node)
4475 postfix_expression = error_mark_node;
4476 }
4477
17a27b4f
MM
4478 /* Assume this expression is not a pseudo-destructor access. */
4479 pseudo_destructor_p = false;
4480
4481 /* If the SCOPE is a scalar type, then, if this is a valid program,
4482 we must be looking at a pseudo-destructor-name. */
4483 if (scope && SCALAR_TYPE_P (scope))
7a3ea201 4484 {
17a27b4f
MM
4485 tree s;
4486 tree type;
4487
4488 cp_parser_parse_tentatively (parser);
4489 /* Parse the pseudo-destructor-name. */
4490 s = NULL_TREE;
4491 cp_parser_pseudo_destructor_name (parser, &s, &type);
4492 if (cp_parser_parse_definitely (parser))
4493 {
4494 pseudo_destructor_p = true;
4495 postfix_expression
4496 = finish_pseudo_destructor_expr (postfix_expression,
4497 s, TREE_TYPE (type));
4498 }
4499 }
4500
4501 if (!pseudo_destructor_p)
4502 {
4503 /* If the SCOPE is not a scalar type, we are looking at an
4504 ordinary class member access expression, rather than a
4505 pseudo-destructor-name. */
02ed62dd 4506 bool template_p;
7a3ea201 4507 /* Parse the id-expression. */
02ed62dd
MM
4508 name = (cp_parser_id_expression
4509 (parser,
4510 cp_parser_optional_template_keyword (parser),
4511 /*check_dependency_p=*/true,
4512 &template_p,
fa6098f8
MM
4513 /*declarator_p=*/false,
4514 /*optional_p=*/false));
7a3ea201
RH
4515 /* In general, build a SCOPE_REF if the member name is qualified.
4516 However, if the name was not dependent and has already been
4517 resolved; there is no need to build the SCOPE_REF. For example;
4518
0cbd7506
MS
4519 struct X { void f(); };
4520 template <typename T> void f(T* t) { t->X::f(); }
7a3ea201
RH
4521
4522 Even though "t" is dependent, "X::f" is not and has been resolved
4523 to a BASELINK; there is no need to include scope information. */
4524
4525 /* But we do need to remember that there was an explicit scope for
4526 virtual function calls. */
4527 if (parser->scope)
4528 *idk = CP_ID_KIND_QUALIFIED;
4529
fc6a28d7
MM
4530 /* If the name is a template-id that names a type, we will get a
4531 TYPE_DECL here. That is invalid code. */
4532 if (TREE_CODE (name) == TYPE_DECL)
7a3ea201 4533 {
fc6a28d7
MM
4534 error ("invalid use of %qD", name);
4535 postfix_expression = error_mark_node;
4536 }
4537 else
4538 {
4539 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4540 {
02ed62dd
MM
4541 name = build_qualified_name (/*type=*/NULL_TREE,
4542 parser->scope,
4543 name,
4544 template_p);
fc6a28d7
MM
4545 parser->scope = NULL_TREE;
4546 parser->qualifying_scope = NULL_TREE;
4547 parser->object_scope = NULL_TREE;
4548 }
4549 if (scope && name && BASELINK_P (name))
4550 adjust_result_of_qualified_name_lookup
4551 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4552 postfix_expression
02ed62dd
MM
4553 = finish_class_member_access_expr (postfix_expression, name,
4554 template_p);
7a3ea201 4555 }
7a3ea201 4556 }
7a3ea201
RH
4557
4558 /* We no longer need to look up names in the scope of the object on
4559 the left-hand side of the `.' or `->' operator. */
4560 parser->context->object_type = NULL_TREE;
4561
4562 /* Outside of offsetof, these operators may not appear in
4563 constant-expressions. */
4564 if (!for_offsetof
98ca843c 4565 && (cp_parser_non_integral_constant_expression
7a3ea201
RH
4566 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4567 postfix_expression = error_mark_node;
4568
4569 return postfix_expression;
4570}
4571
7efa3e22 4572/* Parse a parenthesized expression-list.
a723baf1
MM
4573
4574 expression-list:
4575 assignment-expression
4576 expression-list, assignment-expression
4577
7efa3e22
NS
4578 attribute-list:
4579 expression-list
4580 identifier
4581 identifier, expression-list
4582
93678513
MM
4583 CAST_P is true if this expression is the target of a cast.
4584
a723baf1
MM
4585 Returns a TREE_LIST. The TREE_VALUE of each node is a
4586 representation of an assignment-expression. Note that a TREE_LIST
7efa3e22
NS
4587 is returned even if there is only a single expression in the list.
4588 error_mark_node is returned if the ( and or ) are
4589 missing. NULL_TREE is returned on no expressions. The parentheses
4590 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
39703eb9
MM
4591 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4592 indicates whether or not all of the expressions in the list were
4593 constant. */
a723baf1
MM
4594
4595static tree
21526606 4596cp_parser_parenthesized_expression_list (cp_parser* parser,
39703eb9 4597 bool is_attribute_list,
93678513 4598 bool cast_p,
39703eb9 4599 bool *non_constant_p)
a723baf1
MM
4600{
4601 tree expression_list = NULL_TREE;
1ed3dfd5 4602 bool fold_expr_p = is_attribute_list;
7efa3e22 4603 tree identifier = NULL_TREE;
39703eb9
MM
4604
4605 /* Assume all the expressions will be constant. */
4606 if (non_constant_p)
4607 *non_constant_p = false;
4608
7efa3e22
NS
4609 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4610 return error_mark_node;
21526606 4611
a723baf1 4612 /* Consume expressions until there are no more. */
7efa3e22
NS
4613 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4614 while (true)
4615 {
4616 tree expr;
21526606 4617
7efa3e22
NS
4618 /* At the beginning of attribute lists, check to see if the
4619 next token is an identifier. */
4620 if (is_attribute_list
4621 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4622 {
4623 cp_token *token;
21526606 4624
7efa3e22
NS
4625 /* Consume the identifier. */
4626 token = cp_lexer_consume_token (parser->lexer);
4627 /* Save the identifier. */
4628 identifier = token->value;
4629 }
4630 else
4631 {
4632 /* Parse the next assignment-expression. */
39703eb9
MM
4633 if (non_constant_p)
4634 {
4635 bool expr_non_constant_p;
21526606 4636 expr = (cp_parser_constant_expression
39703eb9
MM
4637 (parser, /*allow_non_constant_p=*/true,
4638 &expr_non_constant_p));
4639 if (expr_non_constant_p)
4640 *non_constant_p = true;
4641 }
4642 else
93678513 4643 expr = cp_parser_assignment_expression (parser, cast_p);
a723baf1 4644
1ed3dfd5
GB
4645 if (fold_expr_p)
4646 expr = fold_non_dependent_expr (expr);
4647
7efa3e22
NS
4648 /* Add it to the list. We add error_mark_node
4649 expressions to the list, so that we can still tell if
4650 the correct form for a parenthesized expression-list
4651 is found. That gives better errors. */
4652 expression_list = tree_cons (NULL_TREE, expr, expression_list);
a723baf1 4653
7efa3e22
NS
4654 if (expr == error_mark_node)
4655 goto skip_comma;
4656 }
a723baf1 4657
7efa3e22
NS
4658 /* After the first item, attribute lists look the same as
4659 expression lists. */
4660 is_attribute_list = false;
21526606 4661
7efa3e22
NS
4662 get_comma:;
4663 /* If the next token isn't a `,', then we are done. */
4664 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4665 break;
4666
4667 /* Otherwise, consume the `,' and keep going. */
4668 cp_lexer_consume_token (parser->lexer);
4669 }
21526606 4670
7efa3e22
NS
4671 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4672 {
4673 int ending;
21526606 4674
7efa3e22
NS
4675 skip_comma:;
4676 /* We try and resync to an unnested comma, as that will give the
4677 user better diagnostics. */
21526606
EC
4678 ending = cp_parser_skip_to_closing_parenthesis (parser,
4679 /*recovering=*/true,
4bb8ca28 4680 /*or_comma=*/true,
a668c6ad 4681 /*consume_paren=*/true);
7efa3e22
NS
4682 if (ending < 0)
4683 goto get_comma;
4684 if (!ending)
4685 return error_mark_node;
a723baf1
MM
4686 }
4687
4688 /* We built up the list in reverse order so we must reverse it now. */
7efa3e22
NS
4689 expression_list = nreverse (expression_list);
4690 if (identifier)
4691 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
21526606 4692
7efa3e22 4693 return expression_list;
a723baf1
MM
4694}
4695
4696/* Parse a pseudo-destructor-name.
4697
4698 pseudo-destructor-name:
4699 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4700 :: [opt] nested-name-specifier template template-id :: ~ type-name
4701 :: [opt] nested-name-specifier [opt] ~ type-name
4702
4703 If either of the first two productions is used, sets *SCOPE to the
4704 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4705 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
d6e57462 4706 or ERROR_MARK_NODE if the parse fails. */
a723baf1
MM
4707
4708static void
21526606 4709cp_parser_pseudo_destructor_name (cp_parser* parser,
0cbd7506
MS
4710 tree* scope,
4711 tree* type)
a723baf1
MM
4712{
4713 bool nested_name_specifier_p;
4714
b14454ba
MM
4715 /* Assume that things will not work out. */
4716 *type = error_mark_node;
4717
a723baf1
MM
4718 /* Look for the optional `::' operator. */
4719 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4720 /* Look for the optional nested-name-specifier. */
21526606 4721 nested_name_specifier_p
a723baf1
MM
4722 = (cp_parser_nested_name_specifier_opt (parser,
4723 /*typename_keyword_p=*/false,
4724 /*check_dependency_p=*/true,
a668c6ad 4725 /*type_p=*/false,
21526606 4726 /*is_declaration=*/true)
a723baf1
MM
4727 != NULL_TREE);
4728 /* Now, if we saw a nested-name-specifier, we might be doing the
4729 second production. */
21526606 4730 if (nested_name_specifier_p
a723baf1
MM
4731 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4732 {
4733 /* Consume the `template' keyword. */
4734 cp_lexer_consume_token (parser->lexer);
4735 /* Parse the template-id. */
21526606 4736 cp_parser_template_id (parser,
a723baf1 4737 /*template_keyword_p=*/true,
a668c6ad
MM
4738 /*check_dependency_p=*/false,
4739 /*is_declaration=*/true);
a723baf1
MM
4740 /* Look for the `::' token. */
4741 cp_parser_require (parser, CPP_SCOPE, "`::'");
4742 }
4743 /* If the next token is not a `~', then there might be some
9bcb9aae 4744 additional qualification. */
a723baf1
MM
4745 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4746 {
4747 /* Look for the type-name. */
4748 *scope = TREE_TYPE (cp_parser_type_name (parser));
d6e57462 4749
b14454ba
MM
4750 if (*scope == error_mark_node)
4751 return;
4752
4753 /* If we don't have ::~, then something has gone wrong. Since
4754 the only caller of this function is looking for something
4755 after `.' or `->' after a scalar type, most likely the
4756 program is trying to get a member of a non-aggregate
4757 type. */
4758 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
d6e57462
ILT
4759 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4760 {
4761 cp_parser_error (parser, "request for member of non-aggregate type");
d6e57462
ILT
4762 return;
4763 }
4764
a723baf1
MM
4765 /* Look for the `::' token. */
4766 cp_parser_require (parser, CPP_SCOPE, "`::'");
4767 }
4768 else
4769 *scope = NULL_TREE;
4770
4771 /* Look for the `~'. */
4772 cp_parser_require (parser, CPP_COMPL, "`~'");
4773 /* Look for the type-name again. We are not responsible for
4774 checking that it matches the first type-name. */
4775 *type = cp_parser_type_name (parser);
4776}
4777
4778/* Parse a unary-expression.
4779
4780 unary-expression:
4781 postfix-expression
4782 ++ cast-expression
4783 -- cast-expression
4784 unary-operator cast-expression
4785 sizeof unary-expression
4786 sizeof ( type-id )
4787 new-expression
4788 delete-expression
4789
4790 GNU Extensions:
4791
4792 unary-expression:
4793 __extension__ cast-expression
4794 __alignof__ unary-expression
4795 __alignof__ ( type-id )
4796 __real__ cast-expression
4797 __imag__ cast-expression
4798 && identifier
4799
4800 ADDRESS_P is true iff the unary-expression is appearing as the
93678513
MM
4801 operand of the `&' operator. CAST_P is true if this expression is
4802 the target of a cast.
a723baf1 4803
34cd5ae7 4804 Returns a representation of the expression. */
a723baf1
MM
4805
4806static tree
93678513 4807cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
a723baf1
MM
4808{
4809 cp_token *token;
4810 enum tree_code unary_operator;
4811
4812 /* Peek at the next token. */
4813 token = cp_lexer_peek_token (parser->lexer);
4814 /* Some keywords give away the kind of expression. */
4815 if (token->type == CPP_KEYWORD)
4816 {
4817 enum rid keyword = token->keyword;
4818
4819 switch (keyword)
4820 {
4821 case RID_ALIGNOF:
a723baf1
MM
4822 case RID_SIZEOF:
4823 {
4824 tree operand;
7a18b933 4825 enum tree_code op;
21526606 4826
7a18b933
NS
4827 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4828 /* Consume the token. */
a723baf1
MM
4829 cp_lexer_consume_token (parser->lexer);
4830 /* Parse the operand. */
4831 operand = cp_parser_sizeof_operand (parser, keyword);
4832
7a18b933
NS
4833 if (TYPE_P (operand))
4834 return cxx_sizeof_or_alignof_type (operand, op, true);
a723baf1 4835 else
7a18b933 4836 return cxx_sizeof_or_alignof_expr (operand, op);
a723baf1
MM
4837 }
4838
4839 case RID_NEW:
4840 return cp_parser_new_expression (parser);
4841
4842 case RID_DELETE:
4843 return cp_parser_delete_expression (parser);
21526606 4844
a723baf1
MM
4845 case RID_EXTENSION:
4846 {
4847 /* The saved value of the PEDANTIC flag. */
4848 int saved_pedantic;
4849 tree expr;
4850
4851 /* Save away the PEDANTIC flag. */
4852 cp_parser_extension_opt (parser, &saved_pedantic);
4853 /* Parse the cast-expression. */
d6b4ea85 4854 expr = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4855 /* Restore the PEDANTIC flag. */
4856 pedantic = saved_pedantic;
4857
4858 return expr;
4859 }
4860
4861 case RID_REALPART:
4862 case RID_IMAGPART:
4863 {
4864 tree expression;
4865
4866 /* Consume the `__real__' or `__imag__' token. */
4867 cp_lexer_consume_token (parser->lexer);
4868 /* Parse the cast-expression. */
d6b4ea85 4869 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4870 /* Create the complete representation. */
4871 return build_x_unary_op ((keyword == RID_REALPART
4872 ? REALPART_EXPR : IMAGPART_EXPR),
4873 expression);
4874 }
4875 break;
4876
4877 default:
4878 break;
4879 }
4880 }
4881
4882 /* Look for the `:: new' and `:: delete', which also signal the
4883 beginning of a new-expression, or delete-expression,
4884 respectively. If the next token is `::', then it might be one of
4885 these. */
4886 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4887 {
4888 enum rid keyword;
4889
4890 /* See if the token after the `::' is one of the keywords in
4891 which we're interested. */
4892 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4893 /* If it's `new', we have a new-expression. */
4894 if (keyword == RID_NEW)
4895 return cp_parser_new_expression (parser);
4896 /* Similarly, for `delete'. */
4897 else if (keyword == RID_DELETE)
4898 return cp_parser_delete_expression (parser);
4899 }
4900
4901 /* Look for a unary operator. */
4902 unary_operator = cp_parser_unary_operator (token);
4903 /* The `++' and `--' operators can be handled similarly, even though
4904 they are not technically unary-operators in the grammar. */
4905 if (unary_operator == ERROR_MARK)
4906 {
4907 if (token->type == CPP_PLUS_PLUS)
4908 unary_operator = PREINCREMENT_EXPR;
4909 else if (token->type == CPP_MINUS_MINUS)
4910 unary_operator = PREDECREMENT_EXPR;
4911 /* Handle the GNU address-of-label extension. */
4912 else if (cp_parser_allow_gnu_extensions_p (parser)
4913 && token->type == CPP_AND_AND)
4914 {
4915 tree identifier;
4916
4917 /* Consume the '&&' token. */
4918 cp_lexer_consume_token (parser->lexer);
4919 /* Look for the identifier. */
4920 identifier = cp_parser_identifier (parser);
4921 /* Create an expression representing the address. */
4922 return finish_label_address_expr (identifier);
4923 }
4924 }
4925 if (unary_operator != ERROR_MARK)
4926 {
4927 tree cast_expression;
a5ac3982
MM
4928 tree expression = error_mark_node;
4929 const char *non_constant_p = NULL;
a723baf1
MM
4930
4931 /* Consume the operator token. */
4932 token = cp_lexer_consume_token (parser->lexer);
4933 /* Parse the cast-expression. */
21526606 4934 cast_expression
c8094d83 4935 = cp_parser_cast_expression (parser,
93678513
MM
4936 unary_operator == ADDR_EXPR,
4937 /*cast_p=*/false);
a723baf1
MM
4938 /* Now, build an appropriate representation. */
4939 switch (unary_operator)
4940 {
4941 case INDIRECT_REF:
a5ac3982
MM
4942 non_constant_p = "`*'";
4943 expression = build_x_indirect_ref (cast_expression, "unary *");
4944 break;
4945
a723baf1 4946 case ADDR_EXPR:
7a3ea201 4947 non_constant_p = "`&'";
a5ac3982 4948 /* Fall through. */
d17811fd 4949 case BIT_NOT_EXPR:
a5ac3982
MM
4950 expression = build_x_unary_op (unary_operator, cast_expression);
4951 break;
4952
14d22dd6
MM
4953 case PREINCREMENT_EXPR:
4954 case PREDECREMENT_EXPR:
a5ac3982
MM
4955 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4956 ? "`++'" : "`--'");
14d22dd6 4957 /* Fall through. */
392e3d51 4958 case UNARY_PLUS_EXPR:
a723baf1
MM
4959 case NEGATE_EXPR:
4960 case TRUTH_NOT_EXPR:
a5ac3982
MM
4961 expression = finish_unary_op_expr (unary_operator, cast_expression);
4962 break;
a723baf1 4963
a723baf1 4964 default:
315fb5db 4965 gcc_unreachable ();
a723baf1 4966 }
a5ac3982 4967
98ca843c 4968 if (non_constant_p
625cbf93
MM
4969 && cp_parser_non_integral_constant_expression (parser,
4970 non_constant_p))
4971 expression = error_mark_node;
a5ac3982
MM
4972
4973 return expression;
a723baf1
MM
4974 }
4975
93678513 4976 return cp_parser_postfix_expression (parser, address_p, cast_p);
a723baf1
MM
4977}
4978
4979/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4980 unary-operator, the corresponding tree code is returned. */
4981
4982static enum tree_code
94edc4ab 4983cp_parser_unary_operator (cp_token* token)
a723baf1
MM
4984{
4985 switch (token->type)
4986 {
4987 case CPP_MULT:
4988 return INDIRECT_REF;
4989
4990 case CPP_AND:
4991 return ADDR_EXPR;
4992
4993 case CPP_PLUS:
392e3d51 4994 return UNARY_PLUS_EXPR;
a723baf1
MM
4995
4996 case CPP_MINUS:
4997 return NEGATE_EXPR;
4998
4999 case CPP_NOT:
5000 return TRUTH_NOT_EXPR;
21526606 5001
a723baf1
MM
5002 case CPP_COMPL:
5003 return BIT_NOT_EXPR;
5004
5005 default:
5006 return ERROR_MARK;
5007 }
5008}
5009
5010/* Parse a new-expression.
5011
ca099ac8 5012 new-expression:
a723baf1
MM
5013 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5014 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5015
5016 Returns a representation of the expression. */
5017
5018static tree
94edc4ab 5019cp_parser_new_expression (cp_parser* parser)
a723baf1
MM
5020{
5021 bool global_scope_p;
5022 tree placement;
5023 tree type;
5024 tree initializer;
058b15c1 5025 tree nelts;
a723baf1
MM
5026
5027 /* Look for the optional `::' operator. */
21526606 5028 global_scope_p
a723baf1
MM
5029 = (cp_parser_global_scope_opt (parser,
5030 /*current_scope_valid_p=*/false)
5031 != NULL_TREE);
5032 /* Look for the `new' operator. */
5033 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5034 /* There's no easy way to tell a new-placement from the
5035 `( type-id )' construct. */
5036 cp_parser_parse_tentatively (parser);
5037 /* Look for a new-placement. */
5038 placement = cp_parser_new_placement (parser);
5039 /* If that didn't work out, there's no new-placement. */
5040 if (!cp_parser_parse_definitely (parser))
5041 placement = NULL_TREE;
5042
5043 /* If the next token is a `(', then we have a parenthesized
5044 type-id. */
5045 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5046 {
5047 /* Consume the `('. */
5048 cp_lexer_consume_token (parser->lexer);
5049 /* Parse the type-id. */
5050 type = cp_parser_type_id (parser);
5051 /* Look for the closing `)'. */
5052 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
98ca843c 5053 /* There should not be a direct-new-declarator in this production,
0cbd7506 5054 but GCC used to allowed this, so we check and emit a sensible error
063e900f
GB
5055 message for this case. */
5056 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
0da99d4e
GB
5057 {
5058 error ("array bound forbidden after parenthesized type-id");
5059 inform ("try removing the parentheses around the type-id");
063e900f
GB
5060 cp_parser_direct_new_declarator (parser);
5061 }
17a27b4f 5062 nelts = NULL_TREE;
a723baf1
MM
5063 }
5064 /* Otherwise, there must be a new-type-id. */
5065 else
058b15c1 5066 type = cp_parser_new_type_id (parser, &nelts);
a723baf1
MM
5067
5068 /* If the next token is a `(', then we have a new-initializer. */
5069 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5070 initializer = cp_parser_new_initializer (parser);
5071 else
5072 initializer = NULL_TREE;
5073
625cbf93
MM
5074 /* A new-expression may not appear in an integral constant
5075 expression. */
5076 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5077 return error_mark_node;
5078
a723baf1 5079 /* Create a representation of the new-expression. */
058b15c1 5080 return build_new (placement, type, nelts, initializer, global_scope_p);
a723baf1
MM
5081}
5082
5083/* Parse a new-placement.
5084
5085 new-placement:
5086 ( expression-list )
5087
5088 Returns the same representation as for an expression-list. */
5089
5090static tree
94edc4ab 5091cp_parser_new_placement (cp_parser* parser)
a723baf1
MM
5092{
5093 tree expression_list;
5094
a723baf1 5095 /* Parse the expression-list. */
21526606 5096 expression_list = (cp_parser_parenthesized_expression_list
93678513
MM
5097 (parser, false, /*cast_p=*/false,
5098 /*non_constant_p=*/NULL));
a723baf1
MM
5099
5100 return expression_list;
5101}
5102
5103/* Parse a new-type-id.
5104
5105 new-type-id:
5106 type-specifier-seq new-declarator [opt]
5107
058b15c1
MM
5108 Returns the TYPE allocated. If the new-type-id indicates an array
5109 type, *NELTS is set to the number of elements in the last array
5110 bound; the TYPE will not include the last array bound. */
a723baf1
MM
5111
5112static tree
058b15c1 5113cp_parser_new_type_id (cp_parser* parser, tree *nelts)
a723baf1 5114{
62d1db17 5115 cp_decl_specifier_seq type_specifier_seq;
058b15c1
MM
5116 cp_declarator *new_declarator;
5117 cp_declarator *declarator;
5118 cp_declarator *outer_declarator;
a723baf1 5119 const char *saved_message;
058b15c1 5120 tree type;
a723baf1
MM
5121
5122 /* The type-specifier sequence must not contain type definitions.
5123 (It cannot contain declarations of new types either, but if they
5124 are not definitions we will catch that because they are not
5125 complete.) */
5126 saved_message = parser->type_definition_forbidden_message;
5127 parser->type_definition_forbidden_message
5128 = "types may not be defined in a new-type-id";
5129 /* Parse the type-specifier-seq. */
d4113656
MM
5130 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5131 &type_specifier_seq);
a723baf1
MM
5132 /* Restore the old message. */
5133 parser->type_definition_forbidden_message = saved_message;
5134 /* Parse the new-declarator. */
058b15c1
MM
5135 new_declarator = cp_parser_new_declarator_opt (parser);
5136
5137 /* Determine the number of elements in the last array dimension, if
5138 any. */
5139 *nelts = NULL_TREE;
5140 /* Skip down to the last array dimension. */
5141 declarator = new_declarator;
5142 outer_declarator = NULL;
5143 while (declarator && (declarator->kind == cdk_pointer
5144 || declarator->kind == cdk_ptrmem))
5145 {
5146 outer_declarator = declarator;
5147 declarator = declarator->declarator;
5148 }
98ca843c 5149 while (declarator
058b15c1
MM
5150 && declarator->kind == cdk_array
5151 && declarator->declarator
5152 && declarator->declarator->kind == cdk_array)
5153 {
5154 outer_declarator = declarator;
5155 declarator = declarator->declarator;
5156 }
98ca843c 5157
058b15c1
MM
5158 if (declarator && declarator->kind == cdk_array)
5159 {
5160 *nelts = declarator->u.array.bounds;
5161 if (*nelts == error_mark_node)
5162 *nelts = integer_one_node;
c8094d83 5163
058b15c1
MM
5164 if (outer_declarator)
5165 outer_declarator->declarator = declarator->declarator;
5166 else
5167 new_declarator = NULL;
5168 }
a723baf1 5169
62d1db17 5170 type = groktypename (&type_specifier_seq, new_declarator);
058b15c1
MM
5171 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5172 {
5173 *nelts = array_type_nelts_top (type);
5174 type = TREE_TYPE (type);
5175 }
5176 return type;
a723baf1
MM
5177}
5178
5179/* Parse an (optional) new-declarator.
5180
5181 new-declarator:
5182 ptr-operator new-declarator [opt]
5183 direct-new-declarator
5184
058b15c1 5185 Returns the declarator. */
a723baf1 5186
058b15c1 5187static cp_declarator *
94edc4ab 5188cp_parser_new_declarator_opt (cp_parser* parser)
a723baf1
MM
5189{
5190 enum tree_code code;
5191 tree type;
3c01e5df 5192 cp_cv_quals cv_quals;
a723baf1
MM
5193
5194 /* We don't know if there's a ptr-operator next, or not. */
5195 cp_parser_parse_tentatively (parser);
5196 /* Look for a ptr-operator. */
3c01e5df 5197 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
a723baf1
MM
5198 /* If that worked, look for more new-declarators. */
5199 if (cp_parser_parse_definitely (parser))
5200 {
058b15c1 5201 cp_declarator *declarator;
a723baf1
MM
5202
5203 /* Parse another optional declarator. */
5204 declarator = cp_parser_new_declarator_opt (parser);
5205
5206 /* Create the representation of the declarator. */
058b15c1 5207 if (type)
3c01e5df 5208 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
058b15c1 5209 else if (code == INDIRECT_REF)
3c01e5df 5210 declarator = make_pointer_declarator (cv_quals, declarator);
a723baf1 5211 else
3c01e5df 5212 declarator = make_reference_declarator (cv_quals, declarator);
a723baf1 5213
a723baf1
MM
5214 return declarator;
5215 }
5216
5217 /* If the next token is a `[', there is a direct-new-declarator. */
5218 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5219 return cp_parser_direct_new_declarator (parser);
5220
058b15c1 5221 return NULL;
a723baf1
MM
5222}
5223
5224/* Parse a direct-new-declarator.
5225
5226 direct-new-declarator:
5227 [ expression ]
21526606 5228 direct-new-declarator [constant-expression]
a723baf1 5229
058b15c1 5230 */
a723baf1 5231
058b15c1 5232static cp_declarator *
94edc4ab 5233cp_parser_direct_new_declarator (cp_parser* parser)
a723baf1 5234{
058b15c1 5235 cp_declarator *declarator = NULL;
a723baf1
MM
5236
5237 while (true)
5238 {
5239 tree expression;
5240
5241 /* Look for the opening `['. */
5242 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5243 /* The first expression is not required to be constant. */
5244 if (!declarator)
5245 {
93678513 5246 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
5247 /* The standard requires that the expression have integral
5248 type. DR 74 adds enumeration types. We believe that the
5249 real intent is that these expressions be handled like the
5250 expression in a `switch' condition, which also allows
5251 classes with a single conversion to integral or
5252 enumeration type. */
5253 if (!processing_template_decl)
5254 {
21526606 5255 expression
a723baf1
MM
5256 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5257 expression,
b746c5dc 5258 /*complain=*/true);
a723baf1
MM
5259 if (!expression)
5260 {
2a13a625 5261 error ("expression in new-declarator must have integral "
0cbd7506 5262 "or enumeration type");
a723baf1
MM
5263 expression = error_mark_node;
5264 }
5265 }
5266 }
5267 /* But all the other expressions must be. */
5268 else
21526606
EC
5269 expression
5270 = cp_parser_constant_expression (parser,
14d22dd6
MM
5271 /*allow_non_constant=*/false,
5272 NULL);
a723baf1
MM
5273 /* Look for the closing `]'. */
5274 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5275
5276 /* Add this bound to the declarator. */
058b15c1 5277 declarator = make_array_declarator (declarator, expression);
a723baf1
MM
5278
5279 /* If the next token is not a `[', then there are no more
5280 bounds. */
5281 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5282 break;
5283 }
5284
5285 return declarator;
5286}
5287
5288/* Parse a new-initializer.
5289
5290 new-initializer:
5291 ( expression-list [opt] )
5292
34cd5ae7 5293 Returns a representation of the expression-list. If there is no
a723baf1
MM
5294 expression-list, VOID_ZERO_NODE is returned. */
5295
5296static tree
94edc4ab 5297cp_parser_new_initializer (cp_parser* parser)
a723baf1
MM
5298{
5299 tree expression_list;
5300
21526606 5301 expression_list = (cp_parser_parenthesized_expression_list
93678513
MM
5302 (parser, false, /*cast_p=*/false,
5303 /*non_constant_p=*/NULL));
7efa3e22 5304 if (!expression_list)
a723baf1 5305 expression_list = void_zero_node;
a723baf1
MM
5306
5307 return expression_list;
5308}
5309
5310/* Parse a delete-expression.
5311
5312 delete-expression:
5313 :: [opt] delete cast-expression
5314 :: [opt] delete [ ] cast-expression
5315
5316 Returns a representation of the expression. */
5317
5318static tree
94edc4ab 5319cp_parser_delete_expression (cp_parser* parser)
a723baf1
MM
5320{
5321 bool global_scope_p;
5322 bool array_p;
5323 tree expression;
5324
5325 /* Look for the optional `::' operator. */
5326 global_scope_p
5327 = (cp_parser_global_scope_opt (parser,
5328 /*current_scope_valid_p=*/false)
5329 != NULL_TREE);
5330 /* Look for the `delete' keyword. */
5331 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5332 /* See if the array syntax is in use. */
5333 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5334 {
5335 /* Consume the `[' token. */
5336 cp_lexer_consume_token (parser->lexer);
5337 /* Look for the `]' token. */
5338 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5339 /* Remember that this is the `[]' construct. */
5340 array_p = true;
5341 }
5342 else
5343 array_p = false;
5344
5345 /* Parse the cast-expression. */
d6b4ea85 5346 expression = cp_parser_simple_cast_expression (parser);
a723baf1 5347
625cbf93
MM
5348 /* A delete-expression may not appear in an integral constant
5349 expression. */
5350 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5351 return error_mark_node;
5352
a723baf1
MM
5353 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5354}
5355
5356/* Parse a cast-expression.
5357
5358 cast-expression:
5359 unary-expression
5360 ( type-id ) cast-expression
5361
93678513
MM
5362 ADDRESS_P is true iff the unary-expression is appearing as the
5363 operand of the `&' operator. CAST_P is true if this expression is
5364 the target of a cast.
5365
a723baf1
MM
5366 Returns a representation of the expression. */
5367
5368static tree
93678513 5369cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
a723baf1
MM
5370{
5371 /* If it's a `(', then we might be looking at a cast. */
5372 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5373 {
5374 tree type = NULL_TREE;
5375 tree expr = NULL_TREE;
5376 bool compound_literal_p;
5377 const char *saved_message;
5378
5379 /* There's no way to know yet whether or not this is a cast.
5380 For example, `(int (3))' is a unary-expression, while `(int)
5381 3' is a cast. So, we resort to parsing tentatively. */
5382 cp_parser_parse_tentatively (parser);
5383 /* Types may not be defined in a cast. */
5384 saved_message = parser->type_definition_forbidden_message;
5385 parser->type_definition_forbidden_message
5386 = "types may not be defined in casts";
5387 /* Consume the `('. */
5388 cp_lexer_consume_token (parser->lexer);
5389 /* A very tricky bit is that `(struct S) { 3 }' is a
5390 compound-literal (which we permit in C++ as an extension).
5391 But, that construct is not a cast-expression -- it is a
5392 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5393 is legal; if the compound-literal were a cast-expression,
5394 you'd need an extra set of parentheses.) But, if we parse
5395 the type-id, and it happens to be a class-specifier, then we
5396 will commit to the parse at that point, because we cannot
5397 undo the action that is done when creating a new class. So,
21526606 5398 then we cannot back up and do a postfix-expression.
a723baf1
MM
5399
5400 Therefore, we scan ahead to the closing `)', and check to see
5401 if the token after the `)' is a `{'. If so, we are not
21526606 5402 looking at a cast-expression.
a723baf1
MM
5403
5404 Save tokens so that we can put them back. */
5405 cp_lexer_save_tokens (parser->lexer);
5406 /* Skip tokens until the next token is a closing parenthesis.
5407 If we find the closing `)', and the next token is a `{', then
5408 we are looking at a compound-literal. */
21526606 5409 compound_literal_p
a668c6ad
MM
5410 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5411 /*consume_paren=*/true)
a723baf1
MM
5412 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5413 /* Roll back the tokens we skipped. */
5414 cp_lexer_rollback_tokens (parser->lexer);
5415 /* If we were looking at a compound-literal, simulate an error
5416 so that the call to cp_parser_parse_definitely below will
5417 fail. */
5418 if (compound_literal_p)
5419 cp_parser_simulate_error (parser);
5420 else
5421 {
4f8163b1
MM
5422 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5423 parser->in_type_id_in_expr_p = true;
a723baf1
MM
5424 /* Look for the type-id. */
5425 type = cp_parser_type_id (parser);
5426 /* Look for the closing `)'. */
5427 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4f8163b1 5428 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
5429 }
5430
5431 /* Restore the saved message. */
5432 parser->type_definition_forbidden_message = saved_message;
5433
bbaab916 5434 /* If ok so far, parse the dependent expression. We cannot be
0cbd7506
MS
5435 sure it is a cast. Consider `(T ())'. It is a parenthesized
5436 ctor of T, but looks like a cast to function returning T
5437 without a dependent expression. */
bbaab916 5438 if (!cp_parser_error_occurred (parser))
c8094d83 5439 expr = cp_parser_cast_expression (parser,
93678513
MM
5440 /*address_p=*/false,
5441 /*cast_p=*/true);
bbaab916 5442
a723baf1
MM
5443 if (cp_parser_parse_definitely (parser))
5444 {
a723baf1 5445 /* Warn about old-style casts, if so requested. */
21526606
EC
5446 if (warn_old_style_cast
5447 && !in_system_header
5448 && !VOID_TYPE_P (type)
a723baf1 5449 && current_lang_name != lang_name_c)
b323323f 5450 warning (OPT_Wold_style_cast, "use of old-style cast");
14d22dd6
MM
5451
5452 /* Only type conversions to integral or enumeration types
5453 can be used in constant-expressions. */
67c03833 5454 if (parser->integral_constant_expression_p
14d22dd6 5455 && !dependent_type_p (type)
625cbf93 5456 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
98ca843c 5457 && (cp_parser_non_integral_constant_expression
625cbf93
MM
5458 (parser,
5459 "a cast to a type other than an integral or "
5460 "enumeration type")))
5461 return error_mark_node;
5462
a723baf1
MM
5463 /* Perform the cast. */
5464 expr = build_c_cast (type, expr);
bbaab916 5465 return expr;
a723baf1 5466 }
a723baf1
MM
5467 }
5468
5469 /* If we get here, then it's not a cast, so it must be a
5470 unary-expression. */
93678513 5471 return cp_parser_unary_expression (parser, address_p, cast_p);
a723baf1
MM
5472}
5473
b8b94c5b 5474/* Parse a binary expression of the general form:
a723baf1
MM
5475
5476 pm-expression:
5477 cast-expression
5478 pm-expression .* cast-expression
5479 pm-expression ->* cast-expression
5480
77077b39 5481 multiplicative-expression:
a723baf1
MM
5482 pm-expression
5483 multiplicative-expression * pm-expression
5484 multiplicative-expression / pm-expression
5485 multiplicative-expression % pm-expression
5486
a723baf1
MM
5487 additive-expression:
5488 multiplicative-expression
5489 additive-expression + multiplicative-expression
5490 additive-expression - multiplicative-expression
5491
a723baf1
MM
5492 shift-expression:
5493 additive-expression
5494 shift-expression << additive-expression
5495 shift-expression >> additive-expression
5496
a723baf1
MM
5497 relational-expression:
5498 shift-expression
5499 relational-expression < shift-expression
5500 relational-expression > shift-expression
5501 relational-expression <= shift-expression
5502 relational-expression >= shift-expression
5503
b8b94c5b 5504 GNU Extension:
c8094d83 5505
a723baf1
MM
5506 relational-expression:
5507 relational-expression <? shift-expression
5508 relational-expression >? shift-expression
5509
a723baf1
MM
5510 equality-expression:
5511 relational-expression
5512 equality-expression == relational-expression
5513 equality-expression != relational-expression
5514
a723baf1
MM
5515 and-expression:
5516 equality-expression
5517 and-expression & equality-expression
5518
a723baf1
MM
5519 exclusive-or-expression:
5520 and-expression
5521 exclusive-or-expression ^ and-expression
5522
b8b94c5b
PB
5523 inclusive-or-expression:
5524 exclusive-or-expression
5525 inclusive-or-expression | exclusive-or-expression
a723baf1 5526
b8b94c5b
PB
5527 logical-and-expression:
5528 inclusive-or-expression
5529 logical-and-expression && inclusive-or-expression
a723baf1 5530
b8b94c5b
PB
5531 logical-or-expression:
5532 logical-and-expression
5533 logical-or-expression || logical-and-expression
a723baf1 5534
b8b94c5b 5535 All these are implemented with a single function like:
a723baf1 5536
b8b94c5b
PB
5537 binary-expression:
5538 simple-cast-expression
5539 binary-expression <token> binary-expression
a723baf1 5540
93678513
MM
5541 CAST_P is true if this expression is the target of a cast.
5542
b8b94c5b
PB
5543 The binops_by_token map is used to get the tree codes for each <token> type.
5544 binary-expressions are associated according to a precedence table. */
a723baf1 5545
b8b94c5b
PB
5546#define TOKEN_PRECEDENCE(token) \
5547 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5548 ? PREC_NOT_OPERATOR \
5549 : binops_by_token[token->type].prec)
a723baf1
MM
5550
5551static tree
93678513 5552cp_parser_binary_expression (cp_parser* parser, bool cast_p)
a723baf1 5553{
b8b94c5b
PB
5554 cp_parser_expression_stack stack;
5555 cp_parser_expression_stack_entry *sp = &stack[0];
5556 tree lhs, rhs;
5557 cp_token *token;
5558 enum tree_code tree_type;
5559 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5560 bool overloaded_p;
a723baf1 5561
b8b94c5b 5562 /* Parse the first expression. */
93678513 5563 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
a723baf1 5564
b8b94c5b
PB
5565 for (;;)
5566 {
5567 /* Get an operator token. */
5568 token = cp_lexer_peek_token (parser->lexer);
8ff24a79
MM
5569 if (token->type == CPP_MIN || token->type == CPP_MAX)
5570 cp_parser_warn_min_max ();
5571
b8b94c5b
PB
5572 new_prec = TOKEN_PRECEDENCE (token);
5573
5574 /* Popping an entry off the stack means we completed a subexpression:
0cbd7506
MS
5575 - either we found a token which is not an operator (`>' where it is not
5576 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5577 will happen repeatedly;
5578 - or, we found an operator which has lower priority. This is the case
5579 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5580 parsing `3 * 4'. */
b8b94c5b 5581 if (new_prec <= prec)
0cbd7506
MS
5582 {
5583 if (sp == stack)
b8b94c5b 5584 break;
0cbd7506 5585 else
b8b94c5b 5586 goto pop;
0cbd7506 5587 }
a723baf1 5588
b8b94c5b
PB
5589 get_rhs:
5590 tree_type = binops_by_token[token->type].tree_type;
a723baf1 5591
03fd3f84 5592 /* We used the operator token. */
b8b94c5b 5593 cp_lexer_consume_token (parser->lexer);
a723baf1 5594
b8b94c5b 5595 /* Extract another operand. It may be the RHS of this expression
0cbd7506 5596 or the LHS of a new, higher priority expression. */
b8b94c5b 5597 rhs = cp_parser_simple_cast_expression (parser);
a723baf1 5598
b8b94c5b 5599 /* Get another operator token. Look up its precedence to avoid
0cbd7506
MS
5600 building a useless (immediately popped) stack entry for common
5601 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
b8b94c5b
PB
5602 token = cp_lexer_peek_token (parser->lexer);
5603 lookahead_prec = TOKEN_PRECEDENCE (token);
5604 if (lookahead_prec > new_prec)
0cbd7506
MS
5605 {
5606 /* ... and prepare to parse the RHS of the new, higher priority
5607 expression. Since precedence levels on the stack are
43c2a69a
PB
5608 monotonically increasing, we do not have to care about
5609 stack overflows. */
0cbd7506
MS
5610 sp->prec = prec;
5611 sp->tree_type = tree_type;
5612 sp->lhs = lhs;
5613 sp++;
5614 lhs = rhs;
5615 prec = new_prec;
5616 new_prec = lookahead_prec;
5617 goto get_rhs;
5618
5619 pop:
5620 /* If the stack is not empty, we have parsed into LHS the right side
b8b94c5b 5621 (`4' in the example above) of an expression we had suspended.
c8094d83 5622 We can use the information on the stack to recover the LHS (`3')
b8b94c5b
PB
5623 from the stack together with the tree code (`MULT_EXPR'), and
5624 the precedence of the higher level subexpression
5625 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5626 which will be used to actually build the additive expression. */
0cbd7506 5627 --sp;
b8b94c5b 5628 prec = sp->prec;
0cbd7506
MS
5629 tree_type = sp->tree_type;
5630 rhs = lhs;
5631 lhs = sp->lhs;
5632 }
a723baf1 5633
b8b94c5b
PB
5634 overloaded_p = false;
5635 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
a723baf1 5636
b8b94c5b 5637 /* If the binary operator required the use of an overloaded operator,
0cbd7506
MS
5638 then this expression cannot be an integral constant-expression.
5639 An overloaded operator can be used even if both operands are
5640 otherwise permissible in an integral constant-expression if at
5641 least one of the operands is of enumeration type. */
a723baf1 5642
b8b94c5b 5643 if (overloaded_p
0cbd7506
MS
5644 && (cp_parser_non_integral_constant_expression
5645 (parser, "calls to overloaded operators")))
5646 return error_mark_node;
b8b94c5b 5647 }
a723baf1 5648
b8b94c5b 5649 return lhs;
a723baf1
MM
5650}
5651
b8b94c5b 5652
a723baf1
MM
5653/* Parse the `? expression : assignment-expression' part of a
5654 conditional-expression. The LOGICAL_OR_EXPR is the
5655 logical-or-expression that started the conditional-expression.
5656 Returns a representation of the entire conditional-expression.
5657
39703eb9 5658 This routine is used by cp_parser_assignment_expression.
a723baf1
MM
5659
5660 ? expression : assignment-expression
21526606 5661
a723baf1 5662 GNU Extensions:
21526606 5663
a723baf1
MM
5664 ? : assignment-expression */
5665
5666static tree
94edc4ab 5667cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
a723baf1
MM
5668{
5669 tree expr;
5670 tree assignment_expr;
5671
5672 /* Consume the `?' token. */
5673 cp_lexer_consume_token (parser->lexer);
5674 if (cp_parser_allow_gnu_extensions_p (parser)
5675 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5676 /* Implicit true clause. */
5677 expr = NULL_TREE;
5678 else
5679 /* Parse the expression. */
93678513 5680 expr = cp_parser_expression (parser, /*cast_p=*/false);
21526606 5681
a723baf1
MM
5682 /* The next token should be a `:'. */
5683 cp_parser_require (parser, CPP_COLON, "`:'");
5684 /* Parse the assignment-expression. */
93678513 5685 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
a723baf1
MM
5686
5687 /* Build the conditional-expression. */
5688 return build_x_conditional_expr (logical_or_expr,
5689 expr,
5690 assignment_expr);
5691}
5692
5693/* Parse an assignment-expression.
5694
5695 assignment-expression:
5696 conditional-expression
5697 logical-or-expression assignment-operator assignment_expression
5698 throw-expression
5699
93678513
MM
5700 CAST_P is true if this expression is the target of a cast.
5701
a723baf1
MM
5702 Returns a representation for the expression. */
5703
5704static tree
93678513 5705cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
a723baf1
MM
5706{
5707 tree expr;
5708
5709 /* If the next token is the `throw' keyword, then we're looking at
5710 a throw-expression. */
5711 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5712 expr = cp_parser_throw_expression (parser);
5713 /* Otherwise, it must be that we are looking at a
5714 logical-or-expression. */
5715 else
5716 {
b8b94c5b 5717 /* Parse the binary expressions (logical-or-expression). */
93678513 5718 expr = cp_parser_binary_expression (parser, cast_p);
a723baf1
MM
5719 /* If the next token is a `?' then we're actually looking at a
5720 conditional-expression. */
5721 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5722 return cp_parser_question_colon_clause (parser, expr);
21526606 5723 else
a723baf1
MM
5724 {
5725 enum tree_code assignment_operator;
5726
5727 /* If it's an assignment-operator, we're using the second
5728 production. */
21526606 5729 assignment_operator
a723baf1
MM
5730 = cp_parser_assignment_operator_opt (parser);
5731 if (assignment_operator != ERROR_MARK)
5732 {
5733 tree rhs;
5734
5735 /* Parse the right-hand side of the assignment. */
93678513 5736 rhs = cp_parser_assignment_expression (parser, cast_p);
14d22dd6
MM
5737 /* An assignment may not appear in a
5738 constant-expression. */
625cbf93
MM
5739 if (cp_parser_non_integral_constant_expression (parser,
5740 "an assignment"))
5741 return error_mark_node;
34cd5ae7 5742 /* Build the assignment expression. */
21526606
EC
5743 expr = build_x_modify_expr (expr,
5744 assignment_operator,
a723baf1
MM
5745 rhs);
5746 }
5747 }
5748 }
5749
5750 return expr;
5751}
5752
5753/* Parse an (optional) assignment-operator.
5754
21526606
EC
5755 assignment-operator: one of
5756 = *= /= %= += -= >>= <<= &= ^= |=
a723baf1
MM
5757
5758 GNU Extension:
21526606 5759
a723baf1
MM
5760 assignment-operator: one of
5761 <?= >?=
5762
5763 If the next token is an assignment operator, the corresponding tree
5764 code is returned, and the token is consumed. For example, for
5765 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5766 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5767 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5768 operator, ERROR_MARK is returned. */
5769
5770static enum tree_code
94edc4ab 5771cp_parser_assignment_operator_opt (cp_parser* parser)
a723baf1
MM
5772{
5773 enum tree_code op;
5774 cp_token *token;
5775
5776 /* Peek at the next toen. */
5777 token = cp_lexer_peek_token (parser->lexer);
5778
5779 switch (token->type)
5780 {
5781 case CPP_EQ:
5782 op = NOP_EXPR;
5783 break;
5784
5785 case CPP_MULT_EQ:
5786 op = MULT_EXPR;
5787 break;
5788
5789 case CPP_DIV_EQ:
5790 op = TRUNC_DIV_EXPR;
5791 break;
5792
5793 case CPP_MOD_EQ:
5794 op = TRUNC_MOD_EXPR;
5795 break;
5796
5797 case CPP_PLUS_EQ:
5798 op = PLUS_EXPR;
5799 break;
5800
5801 case CPP_MINUS_EQ:
5802 op = MINUS_EXPR;
5803 break;
5804
5805 case CPP_RSHIFT_EQ:
5806 op = RSHIFT_EXPR;
5807 break;
5808
5809 case CPP_LSHIFT_EQ:
5810 op = LSHIFT_EXPR;
5811 break;
5812
5813 case CPP_AND_EQ:
5814 op = BIT_AND_EXPR;
5815 break;
5816
5817 case CPP_XOR_EQ:
5818 op = BIT_XOR_EXPR;
5819 break;
5820
5821 case CPP_OR_EQ:
5822 op = BIT_IOR_EXPR;
5823 break;
5824
5825 case CPP_MIN_EQ:
5826 op = MIN_EXPR;
8ff24a79 5827 cp_parser_warn_min_max ();
a723baf1
MM
5828 break;
5829
5830 case CPP_MAX_EQ:
5831 op = MAX_EXPR;
8ff24a79 5832 cp_parser_warn_min_max ();
a723baf1
MM
5833 break;
5834
21526606 5835 default:
a723baf1
MM
5836 /* Nothing else is an assignment operator. */
5837 op = ERROR_MARK;
5838 }
5839
5840 /* If it was an assignment operator, consume it. */
5841 if (op != ERROR_MARK)
5842 cp_lexer_consume_token (parser->lexer);
5843
5844 return op;
5845}
5846
5847/* Parse an expression.
5848
5849 expression:
5850 assignment-expression
5851 expression , assignment-expression
5852
93678513
MM
5853 CAST_P is true if this expression is the target of a cast.
5854
a723baf1
MM
5855 Returns a representation of the expression. */
5856
5857static tree
93678513 5858cp_parser_expression (cp_parser* parser, bool cast_p)
a723baf1
MM
5859{
5860 tree expression = NULL_TREE;
a723baf1
MM
5861
5862 while (true)
5863 {
5864 tree assignment_expression;
5865
5866 /* Parse the next assignment-expression. */
21526606 5867 assignment_expression
93678513 5868 = cp_parser_assignment_expression (parser, cast_p);
a723baf1
MM
5869 /* If this is the first assignment-expression, we can just
5870 save it away. */
5871 if (!expression)
5872 expression = assignment_expression;
a723baf1 5873 else
d17811fd
MM
5874 expression = build_x_compound_expr (expression,
5875 assignment_expression);
a723baf1
MM
5876 /* If the next token is not a comma, then we are done with the
5877 expression. */
5878 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5879 break;
5880 /* Consume the `,'. */
5881 cp_lexer_consume_token (parser->lexer);
14d22dd6 5882 /* A comma operator cannot appear in a constant-expression. */
625cbf93
MM
5883 if (cp_parser_non_integral_constant_expression (parser,
5884 "a comma operator"))
5885 expression = error_mark_node;
14d22dd6 5886 }
a723baf1
MM
5887
5888 return expression;
5889}
5890
21526606 5891/* Parse a constant-expression.
a723baf1
MM
5892
5893 constant-expression:
21526606 5894 conditional-expression
14d22dd6
MM
5895
5896 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
d17811fd
MM
5897 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5898 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5899 is false, NON_CONSTANT_P should be NULL. */
a723baf1
MM
5900
5901static tree
21526606 5902cp_parser_constant_expression (cp_parser* parser,
14d22dd6
MM
5903 bool allow_non_constant_p,
5904 bool *non_constant_p)
a723baf1 5905{
67c03833
JM
5906 bool saved_integral_constant_expression_p;
5907 bool saved_allow_non_integral_constant_expression_p;
5908 bool saved_non_integral_constant_expression_p;
a723baf1
MM
5909 tree expression;
5910
5911 /* It might seem that we could simply parse the
5912 conditional-expression, and then check to see if it were
5913 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5914 one that the compiler can figure out is constant, possibly after
5915 doing some simplifications or optimizations. The standard has a
5916 precise definition of constant-expression, and we must honor
5917 that, even though it is somewhat more restrictive.
5918
5919 For example:
5920
5921 int i[(2, 3)];
5922
5923 is not a legal declaration, because `(2, 3)' is not a
5924 constant-expression. The `,' operator is forbidden in a
5925 constant-expression. However, GCC's constant-folding machinery
5926 will fold this operation to an INTEGER_CST for `3'. */
5927
14d22dd6 5928 /* Save the old settings. */
67c03833 5929 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
21526606 5930 saved_allow_non_integral_constant_expression_p
67c03833
JM
5931 = parser->allow_non_integral_constant_expression_p;
5932 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
a723baf1 5933 /* We are now parsing a constant-expression. */
67c03833
JM
5934 parser->integral_constant_expression_p = true;
5935 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5936 parser->non_integral_constant_expression_p = false;
39703eb9
MM
5937 /* Although the grammar says "conditional-expression", we parse an
5938 "assignment-expression", which also permits "throw-expression"
5939 and the use of assignment operators. In the case that
5940 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5941 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5942 actually essential that we look for an assignment-expression.
5943 For example, cp_parser_initializer_clauses uses this function to
5944 determine whether a particular assignment-expression is in fact
5945 constant. */
93678513 5946 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14d22dd6 5947 /* Restore the old settings. */
c8094d83 5948 parser->integral_constant_expression_p
93678513 5949 = saved_integral_constant_expression_p;
21526606 5950 parser->allow_non_integral_constant_expression_p
67c03833 5951 = saved_allow_non_integral_constant_expression_p;
14d22dd6 5952 if (allow_non_constant_p)
67c03833 5953 *non_constant_p = parser->non_integral_constant_expression_p;
93678513
MM
5954 else if (parser->non_integral_constant_expression_p)
5955 expression = error_mark_node;
c8094d83 5956 parser->non_integral_constant_expression_p
93678513 5957 = saved_non_integral_constant_expression_p;
a723baf1
MM
5958
5959 return expression;
5960}
5961
7a3ea201
RH
5962/* Parse __builtin_offsetof.
5963
5964 offsetof-expression:
5965 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5966
5967 offsetof-member-designator:
5968 id-expression
5969 | offsetof-member-designator "." id-expression
5970 | offsetof-member-designator "[" expression "]"
5971*/
5972
5973static tree
5974cp_parser_builtin_offsetof (cp_parser *parser)
5975{
5976 int save_ice_p, save_non_ice_p;
5977 tree type, expr;
5978 cp_id_kind dummy;
5979
5980 /* We're about to accept non-integral-constant things, but will
5981 definitely yield an integral constant expression. Save and
5982 restore these values around our local parsing. */
5983 save_ice_p = parser->integral_constant_expression_p;
5984 save_non_ice_p = parser->non_integral_constant_expression_p;
5985
5986 /* Consume the "__builtin_offsetof" token. */
5987 cp_lexer_consume_token (parser->lexer);
5988 /* Consume the opening `('. */
5989 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5990 /* Parse the type-id. */
5991 type = cp_parser_type_id (parser);
5992 /* Look for the `,'. */
5993 cp_parser_require (parser, CPP_COMMA, "`,'");
5994
5995 /* Build the (type *)null that begins the traditional offsetof macro. */
5996 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5997
5998 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5999 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6000 true, &dummy);
6001 while (true)
6002 {
6003 cp_token *token = cp_lexer_peek_token (parser->lexer);
6004 switch (token->type)
6005 {
6006 case CPP_OPEN_SQUARE:
6007 /* offsetof-member-designator "[" expression "]" */
6008 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6009 break;
6010
6011 case CPP_DOT:
6012 /* offsetof-member-designator "." identifier */
6013 cp_lexer_consume_token (parser->lexer);
6014 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6015 true, &dummy);
6016 break;
6017
6018 case CPP_CLOSE_PAREN:
6019 /* Consume the ")" token. */
6020 cp_lexer_consume_token (parser->lexer);
6021 goto success;
6022
6023 default:
6024 /* Error. We know the following require will fail, but
6025 that gives the proper error message. */
6026 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6027 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6028 expr = error_mark_node;
6029 goto failure;
6030 }
6031 }
6032
6033 success:
42c244d8
RH
6034 /* If we're processing a template, we can't finish the semantics yet.
6035 Otherwise we can fold the entire expression now. */
6036 if (processing_template_decl)
6037 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6038 else
6039 expr = fold_offsetof (expr);
7a3ea201
RH
6040
6041 failure:
6042 parser->integral_constant_expression_p = save_ice_p;
6043 parser->non_integral_constant_expression_p = save_non_ice_p;
6044
6045 return expr;
6046}
6047
a723baf1
MM
6048/* Statements [gram.stmt.stmt] */
6049
21526606 6050/* Parse a statement.
a723baf1
MM
6051
6052 statement:
6053 labeled-statement
6054 expression-statement
6055 compound-statement
6056 selection-statement
6057 iteration-statement
6058 jump-statement
6059 declaration-statement
bc4071dd
RH
6060 try-block
6061
6062 IN_COMPOUND is true when the statement is nested inside a
6063 cp_parser_compound_statement; this matters for certain pragmas. */
a723baf1
MM
6064
6065static void
bc4071dd
RH
6066cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6067 bool in_compound)
a723baf1
MM
6068{
6069 tree statement;
6070 cp_token *token;
93409b8c 6071 location_t statement_location;
a723baf1 6072
bc4071dd 6073 restart:
a723baf1
MM
6074 /* There is no statement yet. */
6075 statement = NULL_TREE;
6076 /* Peek at the next token. */
6077 token = cp_lexer_peek_token (parser->lexer);
6de9cd9a 6078 /* Remember the location of the first token in the statement. */
93409b8c 6079 statement_location = token->location;
a723baf1
MM
6080 /* If this is a keyword, then that will often determine what kind of
6081 statement we have. */
6082 if (token->type == CPP_KEYWORD)
6083 {
6084 enum rid keyword = token->keyword;
6085
6086 switch (keyword)
6087 {
6088 case RID_CASE:
6089 case RID_DEFAULT:
bc4071dd
RH
6090 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6091 in_compound);
a723baf1
MM
6092 break;
6093
6094 case RID_IF:
6095 case RID_SWITCH:
6096 statement = cp_parser_selection_statement (parser);
6097 break;
6098
6099 case RID_WHILE:
6100 case RID_DO:
6101 case RID_FOR:
6102 statement = cp_parser_iteration_statement (parser);
6103 break;
6104
6105 case RID_BREAK:
6106 case RID_CONTINUE:
6107 case RID_RETURN:
6108 case RID_GOTO:
6109 statement = cp_parser_jump_statement (parser);
6110 break;
6111
e58a9aa1
ZL
6112 /* Objective-C++ exception-handling constructs. */
6113 case RID_AT_TRY:
6114 case RID_AT_CATCH:
6115 case RID_AT_FINALLY:
6116 case RID_AT_SYNCHRONIZED:
6117 case RID_AT_THROW:
6118 statement = cp_parser_objc_statement (parser);
6119 break;
6120
a723baf1
MM
6121 case RID_TRY:
6122 statement = cp_parser_try_block (parser);
6123 break;
6124
6125 default:
6126 /* It might be a keyword like `int' that can start a
6127 declaration-statement. */
6128 break;
6129 }
6130 }
6131 else if (token->type == CPP_NAME)
6132 {
6133 /* If the next token is a `:', then we are looking at a
6134 labeled-statement. */
6135 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6136 if (token->type == CPP_COLON)
bc4071dd
RH
6137 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6138 in_compound);
a723baf1
MM
6139 }
6140 /* Anything that starts with a `{' must be a compound-statement. */
6141 else if (token->type == CPP_OPEN_BRACE)
325c3691 6142 statement = cp_parser_compound_statement (parser, NULL, false);
36952dea
ZW
6143 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6144 a statement all its own. */
6145 else if (token->type == CPP_PRAGMA)
6146 {
bc4071dd
RH
6147 /* Only certain OpenMP pragmas are attached to statements, and thus
6148 are considered statements themselves. All others are not. In
6149 the context of a compound, accept the pragma as a "statement" and
6150 return so that we can check for a close brace. Otherwise we
6151 require a real statement and must go back and read one. */
6152 if (in_compound)
6153 cp_parser_pragma (parser, pragma_compound);
6154 else if (!cp_parser_pragma (parser, pragma_stmt))
6155 goto restart;
36952dea
ZW
6156 return;
6157 }
0ef8776d
VR
6158 else if (token->type == CPP_EOF)
6159 {
6160 cp_parser_error (parser, "expected statement");
6161 return;
6162 }
a723baf1
MM
6163
6164 /* Everything else must be a declaration-statement or an
21526606 6165 expression-statement. Try for the declaration-statement
a723baf1
MM
6166 first, unless we are looking at a `;', in which case we know that
6167 we have an expression-statement. */
6168 if (!statement)
6169 {
6170 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6171 {
6172 cp_parser_parse_tentatively (parser);
6173 /* Try to parse the declaration-statement. */
6174 cp_parser_declaration_statement (parser);
6175 /* If that worked, we're done. */
6176 if (cp_parser_parse_definitely (parser))
6177 return;
6178 }
6179 /* Look for an expression-statement instead. */
325c3691 6180 statement = cp_parser_expression_statement (parser, in_statement_expr);
a723baf1
MM
6181 }
6182
6183 /* Set the line number for the statement. */
009ed910 6184 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
93409b8c 6185 SET_EXPR_LOCATION (statement, statement_location);
a723baf1
MM
6186}
6187
6188/* Parse a labeled-statement.
6189
6190 labeled-statement:
6191 identifier : statement
6192 case constant-expression : statement
98ce043b
MM
6193 default : statement
6194
6195 GNU Extension:
21526606 6196
98ce043b
MM
6197 labeled-statement:
6198 case constant-expression ... constant-expression : statement
a723baf1 6199
8c161995 6200 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
bc4071dd
RH
6201 For an ordinary label, returns a LABEL_EXPR.
6202
6203 IN_COMPOUND is as for cp_parser_statement: true when we're nested
6204 inside a compound. */
a723baf1
MM
6205
6206static tree
bc4071dd
RH
6207cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr,
6208 bool in_compound)
a723baf1
MM
6209{
6210 cp_token *token;
0e59b3fb 6211 tree statement = error_mark_node;
a723baf1
MM
6212
6213 /* The next token should be an identifier. */
6214 token = cp_lexer_peek_token (parser->lexer);
6215 if (token->type != CPP_NAME
6216 && token->type != CPP_KEYWORD)
6217 {
6218 cp_parser_error (parser, "expected labeled-statement");
6219 return error_mark_node;
6220 }
6221
6222 switch (token->keyword)
6223 {
6224 case RID_CASE:
6225 {
98ce043b
MM
6226 tree expr, expr_hi;
6227 cp_token *ellipsis;
a723baf1
MM
6228
6229 /* Consume the `case' token. */
6230 cp_lexer_consume_token (parser->lexer);
6231 /* Parse the constant-expression. */
21526606 6232 expr = cp_parser_constant_expression (parser,
d17811fd 6233 /*allow_non_constant_p=*/false,
14d22dd6 6234 NULL);
98ce043b
MM
6235
6236 ellipsis = cp_lexer_peek_token (parser->lexer);
6237 if (ellipsis->type == CPP_ELLIPSIS)
6238 {
0cbd7506 6239 /* Consume the `...' token. */
98ce043b
MM
6240 cp_lexer_consume_token (parser->lexer);
6241 expr_hi =
6242 cp_parser_constant_expression (parser,
0cbd7506 6243 /*allow_non_constant_p=*/false,
98ce043b
MM
6244 NULL);
6245 /* We don't need to emit warnings here, as the common code
6246 will do this for us. */
6247 }
6248 else
6249 expr_hi = NULL_TREE;
6250
bc4071dd 6251 if (parser->in_switch_statement_p)
98ce043b 6252 statement = finish_case_label (expr, expr_hi);
bc4071dd
RH
6253 else
6254 error ("case label %qE not within a switch statement", expr);
a723baf1
MM
6255 }
6256 break;
6257
6258 case RID_DEFAULT:
6259 /* Consume the `default' token. */
6260 cp_lexer_consume_token (parser->lexer);
bc4071dd
RH
6261
6262 if (parser->in_switch_statement_p)
0e59b3fb 6263 statement = finish_case_label (NULL_TREE, NULL_TREE);
bc4071dd
RH
6264 else
6265 error ("case label not within a switch statement");
a723baf1
MM
6266 break;
6267
6268 default:
6269 /* Anything else must be an ordinary label. */
6270 statement = finish_label_stmt (cp_parser_identifier (parser));
6271 break;
6272 }
6273
6274 /* Require the `:' token. */
6275 cp_parser_require (parser, CPP_COLON, "`:'");
6276 /* Parse the labeled statement. */
bc4071dd 6277 cp_parser_statement (parser, in_statement_expr, in_compound);
a723baf1
MM
6278
6279 /* Return the label, in the case of a `case' or `default' label. */
6280 return statement;
6281}
6282
6283/* Parse an expression-statement.
6284
6285 expression-statement:
6286 expression [opt] ;
6287
6288 Returns the new EXPR_STMT -- or NULL_TREE if the expression
a5bcc582
NS
6289 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6290 indicates whether this expression-statement is part of an
6291 expression statement. */
a723baf1
MM
6292
6293static tree
325c3691 6294cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
a723baf1 6295{
a5bcc582 6296 tree statement = NULL_TREE;
a723baf1 6297
a5bcc582 6298 /* If the next token is a ';', then there is no expression
04c06002 6299 statement. */
a723baf1 6300 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
93678513 6301 statement = cp_parser_expression (parser, /*cast_p=*/false);
21526606 6302
a723baf1 6303 /* Consume the final `;'. */
e0860732 6304 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1 6305
325c3691 6306 if (in_statement_expr
a5bcc582 6307 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
93678513
MM
6308 /* This is the final expression statement of a statement
6309 expression. */
6310 statement = finish_stmt_expr_expr (statement, in_statement_expr);
a5bcc582
NS
6311 else if (statement)
6312 statement = finish_expr_stmt (statement);
6313 else
6314 finish_stmt ();
21526606 6315
a723baf1
MM
6316 return statement;
6317}
6318
6319/* Parse a compound-statement.
6320
6321 compound-statement:
6322 { statement-seq [opt] }
21526606 6323
5882f0f3 6324 Returns a tree representing the statement. */
a723baf1
MM
6325
6326static tree
325c3691
RH
6327cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6328 bool in_try)
a723baf1
MM
6329{
6330 tree compound_stmt;
6331
6332 /* Consume the `{'. */
6333 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6334 return error_mark_node;
6335 /* Begin the compound-statement. */
325c3691 6336 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
a723baf1 6337 /* Parse an (optional) statement-seq. */
325c3691 6338 cp_parser_statement_seq_opt (parser, in_statement_expr);
a723baf1 6339 /* Finish the compound-statement. */
7a3397c7 6340 finish_compound_stmt (compound_stmt);
a723baf1
MM
6341 /* Consume the `}'. */
6342 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6343
6344 return compound_stmt;
6345}
6346
6347/* Parse an (optional) statement-seq.
6348
6349 statement-seq:
6350 statement
6351 statement-seq [opt] statement */
6352
6353static void
325c3691 6354cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
a723baf1
MM
6355{
6356 /* Scan statements until there aren't any more. */
6357 while (true)
6358 {
bc4071dd
RH
6359 cp_token *token = cp_lexer_peek_token (parser->lexer);
6360
a723baf1 6361 /* If we're looking at a `}', then we've run out of statements. */
bc4071dd
RH
6362 if (token->type == CPP_CLOSE_BRACE
6363 || token->type == CPP_EOF
6364 || token->type == CPP_PRAGMA_EOL)
a723baf1
MM
6365 break;
6366
6367 /* Parse the statement. */
bc4071dd 6368 cp_parser_statement (parser, in_statement_expr, true);
a723baf1
MM
6369 }
6370}
6371
6372/* Parse a selection-statement.
6373
6374 selection-statement:
6375 if ( condition ) statement
6376 if ( condition ) statement else statement
21526606 6377 switch ( condition ) statement
a723baf1
MM
6378
6379 Returns the new IF_STMT or SWITCH_STMT. */
6380
6381static tree
94edc4ab 6382cp_parser_selection_statement (cp_parser* parser)
a723baf1
MM
6383{
6384 cp_token *token;
6385 enum rid keyword;
6386
6387 /* Peek at the next token. */
6388 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6389
6390 /* See what kind of keyword it is. */
6391 keyword = token->keyword;
6392 switch (keyword)
6393 {
6394 case RID_IF:
6395 case RID_SWITCH:
6396 {
6397 tree statement;
6398 tree condition;
6399
6400 /* Look for the `('. */
6401 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6402 {
6403 cp_parser_skip_to_end_of_statement (parser);
6404 return error_mark_node;
6405 }
6406
6407 /* Begin the selection-statement. */
6408 if (keyword == RID_IF)
6409 statement = begin_if_stmt ();
6410 else
6411 statement = begin_switch_stmt ();
6412
6413 /* Parse the condition. */
6414 condition = cp_parser_condition (parser);
6415 /* Look for the `)'. */
6416 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
6417 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6418 /*consume_paren=*/true);
a723baf1
MM
6419
6420 if (keyword == RID_IF)
6421 {
a723baf1
MM
6422 /* Add the condition. */
6423 finish_if_stmt_cond (condition, statement);
6424
6425 /* Parse the then-clause. */
325c3691 6426 cp_parser_implicitly_scoped_statement (parser);
a723baf1
MM
6427 finish_then_clause (statement);
6428
6429 /* If the next token is `else', parse the else-clause. */
6430 if (cp_lexer_next_token_is_keyword (parser->lexer,
6431 RID_ELSE))
6432 {
a723baf1
MM
6433 /* Consume the `else' keyword. */
6434 cp_lexer_consume_token (parser->lexer);
325c3691 6435 begin_else_clause (statement);
a723baf1 6436 /* Parse the else-clause. */
325c3691 6437 cp_parser_implicitly_scoped_statement (parser);
a723baf1
MM
6438 finish_else_clause (statement);
6439 }
6440
6441 /* Now we're all done with the if-statement. */
325c3691 6442 finish_if_stmt (statement);
a723baf1
MM
6443 }
6444 else
6445 {
0e59b3fb 6446 bool in_switch_statement_p;
1799e5d5 6447 unsigned char in_statement;
a723baf1
MM
6448
6449 /* Add the condition. */
6450 finish_switch_cond (condition, statement);
6451
6452 /* Parse the body of the switch-statement. */
0e59b3fb 6453 in_switch_statement_p = parser->in_switch_statement_p;
1799e5d5 6454 in_statement = parser->in_statement;
0e59b3fb 6455 parser->in_switch_statement_p = true;
1799e5d5 6456 parser->in_statement |= IN_SWITCH_STMT;
325c3691 6457 cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 6458 parser->in_switch_statement_p = in_switch_statement_p;
1799e5d5 6459 parser->in_statement = in_statement;
a723baf1
MM
6460
6461 /* Now we're all done with the switch-statement. */
6462 finish_switch_stmt (statement);
6463 }
6464
6465 return statement;
6466 }
6467 break;
6468
6469 default:
6470 cp_parser_error (parser, "expected selection-statement");
6471 return error_mark_node;
6472 }
6473}
6474
21526606 6475/* Parse a condition.
a723baf1
MM
6476
6477 condition:
6478 expression
21526606 6479 type-specifier-seq declarator = assignment-expression
a723baf1
MM
6480
6481 GNU Extension:
21526606 6482
a723baf1 6483 condition:
21526606 6484 type-specifier-seq declarator asm-specification [opt]
a723baf1 6485 attributes [opt] = assignment-expression
21526606 6486
a723baf1
MM
6487 Returns the expression that should be tested. */
6488
6489static tree
94edc4ab 6490cp_parser_condition (cp_parser* parser)
a723baf1 6491{
62d1db17 6492 cp_decl_specifier_seq type_specifiers;
a723baf1
MM
6493 const char *saved_message;
6494
6495 /* Try the declaration first. */
6496 cp_parser_parse_tentatively (parser);
6497 /* New types are not allowed in the type-specifier-seq for a
6498 condition. */
6499 saved_message = parser->type_definition_forbidden_message;
6500 parser->type_definition_forbidden_message
6501 = "types may not be defined in conditions";
6502 /* Parse the type-specifier-seq. */
d4113656
MM
6503 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6504 &type_specifiers);
a723baf1
MM
6505 /* Restore the saved message. */
6506 parser->type_definition_forbidden_message = saved_message;
6507 /* If all is well, we might be looking at a declaration. */
6508 if (!cp_parser_error_occurred (parser))
6509 {
6510 tree decl;
6511 tree asm_specification;
6512 tree attributes;
058b15c1 6513 cp_declarator *declarator;
a723baf1 6514 tree initializer = NULL_TREE;
21526606 6515
a723baf1 6516 /* Parse the declarator. */
62b8a44e 6517 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 6518 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
6519 /*parenthesized_p=*/NULL,
6520 /*member_p=*/false);
a723baf1
MM
6521 /* Parse the attributes. */
6522 attributes = cp_parser_attributes_opt (parser);
6523 /* Parse the asm-specification. */
6524 asm_specification = cp_parser_asm_specification_opt (parser);
6525 /* If the next token is not an `=', then we might still be
6526 looking at an expression. For example:
21526606 6527
a723baf1 6528 if (A(a).x)
21526606 6529
a723baf1
MM
6530 looks like a decl-specifier-seq and a declarator -- but then
6531 there is no `=', so this is an expression. */
6532 cp_parser_require (parser, CPP_EQ, "`='");
6533 /* If we did see an `=', then we are looking at a declaration
6534 for sure. */
6535 if (cp_parser_parse_definitely (parser))
6536 {
c8094d83 6537 tree pushed_scope;
d174af6c 6538 bool non_constant_p;
73a8adb6 6539
a723baf1 6540 /* Create the declaration. */
62d1db17 6541 decl = start_decl (declarator, &type_specifiers,
a723baf1 6542 /*initialized_p=*/true,
73a8adb6 6543 attributes, /*prefix_attributes=*/NULL_TREE,
4514aa8c 6544 &pushed_scope);
a723baf1 6545 /* Parse the assignment-expression. */
d174af6c
MM
6546 initializer
6547 = cp_parser_constant_expression (parser,
6548 /*allow_non_constant_p=*/true,
6549 &non_constant_p);
6550 if (!non_constant_p)
6551 initializer = fold_non_dependent_expr (initializer);
21526606 6552
a723baf1 6553 /* Process the initializer. */
21526606 6554 cp_finish_decl (decl,
d174af6c 6555 initializer, !non_constant_p,
21526606 6556 asm_specification,
a723baf1 6557 LOOKUP_ONLYCONVERTING);
c162c75e 6558
4514aa8c
NS
6559 if (pushed_scope)
6560 pop_scope (pushed_scope);
21526606 6561
a723baf1
MM
6562 return convert_from_reference (decl);
6563 }
6564 }
6565 /* If we didn't even get past the declarator successfully, we are
6566 definitely not looking at a declaration. */
6567 else
6568 cp_parser_abort_tentative_parse (parser);
6569
6570 /* Otherwise, we are looking at an expression. */
93678513 6571 return cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
6572}
6573
6574/* Parse an iteration-statement.
6575
6576 iteration-statement:
6577 while ( condition ) statement
6578 do statement while ( expression ) ;
6579 for ( for-init-statement condition [opt] ; expression [opt] )
6580 statement
6581
6582 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6583
6584static tree
94edc4ab 6585cp_parser_iteration_statement (cp_parser* parser)
a723baf1
MM
6586{
6587 cp_token *token;
6588 enum rid keyword;
6589 tree statement;
1799e5d5 6590 unsigned char in_statement;
a723baf1
MM
6591
6592 /* Peek at the next token. */
6593 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6594 if (!token)
6595 return error_mark_node;
6596
0e59b3fb 6597 /* Remember whether or not we are already within an iteration
21526606 6598 statement. */
1799e5d5 6599 in_statement = parser->in_statement;
0e59b3fb 6600
a723baf1
MM
6601 /* See what kind of keyword it is. */
6602 keyword = token->keyword;
6603 switch (keyword)
6604 {
6605 case RID_WHILE:
6606 {
6607 tree condition;
6608
6609 /* Begin the while-statement. */
6610 statement = begin_while_stmt ();
6611 /* Look for the `('. */
6612 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6613 /* Parse the condition. */
6614 condition = cp_parser_condition (parser);
6615 finish_while_stmt_cond (condition, statement);
6616 /* Look for the `)'. */
6617 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6618 /* Parse the dependent statement. */
1799e5d5 6619 parser->in_statement = IN_ITERATION_STMT;
a723baf1 6620 cp_parser_already_scoped_statement (parser);
1799e5d5 6621 parser->in_statement = in_statement;
a723baf1
MM
6622 /* We're done with the while-statement. */
6623 finish_while_stmt (statement);
6624 }
6625 break;
6626
6627 case RID_DO:
6628 {
6629 tree expression;
6630
6631 /* Begin the do-statement. */
6632 statement = begin_do_stmt ();
6633 /* Parse the body of the do-statement. */
1799e5d5 6634 parser->in_statement = IN_ITERATION_STMT;
a723baf1 6635 cp_parser_implicitly_scoped_statement (parser);
1799e5d5 6636 parser->in_statement = in_statement;
a723baf1
MM
6637 finish_do_body (statement);
6638 /* Look for the `while' keyword. */
6639 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6640 /* Look for the `('. */
6641 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6642 /* Parse the expression. */
93678513 6643 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
6644 /* We're done with the do-statement. */
6645 finish_do_stmt (expression, statement);
6646 /* Look for the `)'. */
6647 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6648 /* Look for the `;'. */
6649 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6650 }
6651 break;
6652
6653 case RID_FOR:
6654 {
6655 tree condition = NULL_TREE;
6656 tree expression = NULL_TREE;
6657
6658 /* Begin the for-statement. */
6659 statement = begin_for_stmt ();
6660 /* Look for the `('. */
6661 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6662 /* Parse the initialization. */
6663 cp_parser_for_init_statement (parser);
6664 finish_for_init_stmt (statement);
6665
6666 /* If there's a condition, process it. */
6667 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6668 condition = cp_parser_condition (parser);
6669 finish_for_cond (condition, statement);
6670 /* Look for the `;'. */
6671 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6672
6673 /* If there's an expression, process it. */
6674 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
93678513 6675 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
6676 finish_for_expr (expression, statement);
6677 /* Look for the `)'. */
d5a10cf0 6678 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
98ca843c 6679
a723baf1 6680 /* Parse the body of the for-statement. */
1799e5d5 6681 parser->in_statement = IN_ITERATION_STMT;
a723baf1 6682 cp_parser_already_scoped_statement (parser);
1799e5d5 6683 parser->in_statement = in_statement;
a723baf1
MM
6684
6685 /* We're done with the for-statement. */
6686 finish_for_stmt (statement);
6687 }
6688 break;
6689
6690 default:
6691 cp_parser_error (parser, "expected iteration-statement");
6692 statement = error_mark_node;
6693 break;
6694 }
6695
6696 return statement;
6697}
6698
6699/* Parse a for-init-statement.
6700
6701 for-init-statement:
6702 expression-statement
6703 simple-declaration */
6704
6705static void
94edc4ab 6706cp_parser_for_init_statement (cp_parser* parser)
a723baf1
MM
6707{
6708 /* If the next token is a `;', then we have an empty
34cd5ae7 6709 expression-statement. Grammatically, this is also a
a723baf1
MM
6710 simple-declaration, but an invalid one, because it does not
6711 declare anything. Therefore, if we did not handle this case
6712 specially, we would issue an error message about an invalid
6713 declaration. */
6714 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6715 {
6716 /* We're going to speculatively look for a declaration, falling back
6717 to an expression, if necessary. */
6718 cp_parser_parse_tentatively (parser);
6719 /* Parse the declaration. */
6720 cp_parser_simple_declaration (parser,
6721 /*function_definition_allowed_p=*/false);
6722 /* If the tentative parse failed, then we shall need to look for an
6723 expression-statement. */
6724 if (cp_parser_parse_definitely (parser))
6725 return;
6726 }
6727
a5bcc582 6728 cp_parser_expression_statement (parser, false);
a723baf1
MM
6729}
6730
6731/* Parse a jump-statement.
6732
6733 jump-statement:
6734 break ;
6735 continue ;
6736 return expression [opt] ;
21526606 6737 goto identifier ;
a723baf1
MM
6738
6739 GNU extension:
6740
6741 jump-statement:
6742 goto * expression ;
6743
5088b058 6744 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
a723baf1
MM
6745
6746static tree
94edc4ab 6747cp_parser_jump_statement (cp_parser* parser)
a723baf1
MM
6748{
6749 tree statement = error_mark_node;
6750 cp_token *token;
6751 enum rid keyword;
6752
6753 /* Peek at the next token. */
6754 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6755 if (!token)
6756 return error_mark_node;
6757
6758 /* See what kind of keyword it is. */
6759 keyword = token->keyword;
6760 switch (keyword)
6761 {
6762 case RID_BREAK:
1799e5d5 6763 switch (parser->in_statement)
0e59b3fb 6764 {
1799e5d5 6765 case 0:
0e59b3fb 6766 error ("break statement not within loop or switch");
1799e5d5
RH
6767 break;
6768 default:
6769 gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6770 || parser->in_statement == IN_ITERATION_STMT);
6771 statement = finish_break_stmt ();
6772 break;
6773 case IN_OMP_BLOCK:
6774 error ("invalid exit from OpenMP structured block");
6775 break;
6776 case IN_OMP_FOR:
6777 error ("break statement used with OpenMP for loop");
6778 break;
0e59b3fb 6779 }
2a13a625 6780 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6781 break;
6782
6783 case RID_CONTINUE:
1799e5d5 6784 switch (parser->in_statement & ~IN_SWITCH_STMT)
0e59b3fb 6785 {
1799e5d5 6786 case 0:
0e59b3fb 6787 error ("continue statement not within a loop");
1799e5d5
RH
6788 break;
6789 case IN_ITERATION_STMT:
6790 case IN_OMP_FOR:
6791 statement = finish_continue_stmt ();
6792 break;
6793 case IN_OMP_BLOCK:
6794 error ("invalid exit from OpenMP structured block");
6795 break;
6796 default:
6797 gcc_unreachable ();
0e59b3fb 6798 }
2a13a625 6799 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6800 break;
6801
6802 case RID_RETURN:
6803 {
6804 tree expr;
6805
21526606 6806 /* If the next token is a `;', then there is no
a723baf1
MM
6807 expression. */
6808 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
93678513 6809 expr = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
6810 else
6811 expr = NULL_TREE;
6812 /* Build the return-statement. */
6813 statement = finish_return_stmt (expr);
6814 /* Look for the final `;'. */
2a13a625 6815 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6816 }
6817 break;
6818
6819 case RID_GOTO:
6820 /* Create the goto-statement. */
6821 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6822 {
6823 /* Issue a warning about this use of a GNU extension. */
6824 if (pedantic)
6825 pedwarn ("ISO C++ forbids computed gotos");
6826 /* Consume the '*' token. */
6827 cp_lexer_consume_token (parser->lexer);
6828 /* Parse the dependent expression. */
93678513 6829 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
a723baf1
MM
6830 }
6831 else
6832 finish_goto_stmt (cp_parser_identifier (parser));
6833 /* Look for the final `;'. */
2a13a625 6834 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6835 break;
6836
6837 default:
6838 cp_parser_error (parser, "expected jump-statement");
6839 break;
6840 }
6841
6842 return statement;
6843}
6844
6845/* Parse a declaration-statement.
6846
6847 declaration-statement:
6848 block-declaration */
6849
6850static void
94edc4ab 6851cp_parser_declaration_statement (cp_parser* parser)
a723baf1 6852{
058b15c1
MM
6853 void *p;
6854
6855 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6856 p = obstack_alloc (&declarator_obstack, 0);
6857
6858 /* Parse the block-declaration. */
a723baf1
MM
6859 cp_parser_block_declaration (parser, /*statement_p=*/true);
6860
058b15c1
MM
6861 /* Free any declarators allocated. */
6862 obstack_free (&declarator_obstack, p);
6863
a723baf1
MM
6864 /* Finish off the statement. */
6865 finish_stmt ();
6866}
6867
6868/* Some dependent statements (like `if (cond) statement'), are
6869 implicitly in their own scope. In other words, if the statement is
6870 a single statement (as opposed to a compound-statement), it is
6871 none-the-less treated as if it were enclosed in braces. Any
6872 declarations appearing in the dependent statement are out of scope
6873 after control passes that point. This function parses a statement,
6874 but ensures that is in its own scope, even if it is not a
21526606 6875 compound-statement.
a723baf1
MM
6876
6877 Returns the new statement. */
6878
6879static tree
94edc4ab 6880cp_parser_implicitly_scoped_statement (cp_parser* parser)
a723baf1
MM
6881{
6882 tree statement;
6883
74ac79fa
DM
6884 /* Mark if () ; with a special NOP_EXPR. */
6885 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6886 {
6887 cp_lexer_consume_token (parser->lexer);
6888 statement = add_stmt (build_empty_stmt ());
6889 }
6890 /* if a compound is opened, we simply parse the statement directly. */
6891 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6892 statement = cp_parser_compound_statement (parser, NULL, false);
a723baf1 6893 /* If the token is not a `{', then we must take special action. */
74ac79fa 6894 else
a723baf1
MM
6895 {
6896 /* Create a compound-statement. */
325c3691 6897 statement = begin_compound_stmt (0);
a723baf1 6898 /* Parse the dependent-statement. */
bc4071dd 6899 cp_parser_statement (parser, NULL_TREE, false);
a723baf1 6900 /* Finish the dummy compound-statement. */
7a3397c7 6901 finish_compound_stmt (statement);
a723baf1 6902 }
a723baf1
MM
6903
6904 /* Return the statement. */
6905 return statement;
6906}
6907
6908/* For some dependent statements (like `while (cond) statement'), we
6909 have already created a scope. Therefore, even if the dependent
6910 statement is a compound-statement, we do not want to create another
6911 scope. */
6912
6913static void
94edc4ab 6914cp_parser_already_scoped_statement (cp_parser* parser)
a723baf1 6915{
325c3691
RH
6916 /* If the token is a `{', then we must take special action. */
6917 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
bc4071dd 6918 cp_parser_statement (parser, NULL_TREE, false);
325c3691 6919 else
a723baf1 6920 {
325c3691
RH
6921 /* Avoid calling cp_parser_compound_statement, so that we
6922 don't create a new scope. Do everything else by hand. */
6923 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
bc4071dd 6924 cp_parser_statement_seq_opt (parser, NULL_TREE);
325c3691 6925 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1 6926 }
a723baf1
MM
6927}
6928
6929/* Declarations [gram.dcl.dcl] */
6930
6931/* Parse an optional declaration-sequence.
6932
6933 declaration-seq:
6934 declaration
6935 declaration-seq declaration */
6936
6937static void
94edc4ab 6938cp_parser_declaration_seq_opt (cp_parser* parser)
a723baf1
MM
6939{
6940 while (true)
6941 {
6942 cp_token *token;
6943
6944 token = cp_lexer_peek_token (parser->lexer);
6945
6946 if (token->type == CPP_CLOSE_BRACE
bc4071dd
RH
6947 || token->type == CPP_EOF
6948 || token->type == CPP_PRAGMA_EOL)
a723baf1
MM
6949 break;
6950
21526606 6951 if (token->type == CPP_SEMICOLON)
a723baf1
MM
6952 {
6953 /* A declaration consisting of a single semicolon is
6954 invalid. Allow it unless we're being pedantic. */
a723baf1 6955 cp_lexer_consume_token (parser->lexer);
2cfe82fe
ZW
6956 if (pedantic && !in_system_header)
6957 pedwarn ("extra %<;%>");
a723baf1
MM
6958 continue;
6959 }
6960
7d381002 6961 /* If we're entering or exiting a region that's implicitly
03fd3f84 6962 extern "C", modify the lang context appropriately. */
7d381002
MA
6963 if (!parser->implicit_extern_c && token->implicit_extern_c)
6964 {
6965 push_lang_context (lang_name_c);
6966 parser->implicit_extern_c = true;
6967 }
6968 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6969 {
6970 pop_lang_context ();
6971 parser->implicit_extern_c = false;
6972 }
6973
36952dea
ZW
6974 if (token->type == CPP_PRAGMA)
6975 {
6976 /* A top-level declaration can consist solely of a #pragma.
6977 A nested declaration cannot, so this is done here and not
6978 in cp_parser_declaration. (A #pragma at block scope is
6979 handled in cp_parser_statement.) */
bc4071dd 6980 cp_parser_pragma (parser, pragma_external);
36952dea
ZW
6981 continue;
6982 }
6983
c838d82f 6984 /* Parse the declaration itself. */
a723baf1
MM
6985 cp_parser_declaration (parser);
6986 }
6987}
6988
6989/* Parse a declaration.
6990
6991 declaration:
6992 block-declaration
6993 function-definition
6994 template-declaration
6995 explicit-instantiation
6996 explicit-specialization
6997 linkage-specification
21526606 6998 namespace-definition
1092805d
MM
6999
7000 GNU extension:
7001
7002 declaration:
7003 __extension__ declaration */
a723baf1
MM
7004
7005static void
94edc4ab 7006cp_parser_declaration (cp_parser* parser)
a723baf1
MM
7007{
7008 cp_token token1;
7009 cp_token token2;
1092805d 7010 int saved_pedantic;
058b15c1 7011 void *p;
1092805d
MM
7012
7013 /* Check for the `__extension__' keyword. */
7014 if (cp_parser_extension_opt (parser, &saved_pedantic))
7015 {
7016 /* Parse the qualified declaration. */
7017 cp_parser_declaration (parser);
7018 /* Restore the PEDANTIC flag. */
7019 pedantic = saved_pedantic;
7020
7021 return;
7022 }
a723baf1
MM
7023
7024 /* Try to figure out what kind of declaration is present. */
7025 token1 = *cp_lexer_peek_token (parser->lexer);
21526606 7026
a723baf1
MM
7027 if (token1.type != CPP_EOF)
7028 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
32cafd73 7029 else
728cdd08
GDR
7030 {
7031 token2.type = CPP_EOF;
7032 token2.keyword = RID_MAX;
7033 }
a723baf1 7034
058b15c1
MM
7035 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7036 p = obstack_alloc (&declarator_obstack, 0);
7037
a723baf1
MM
7038 /* If the next token is `extern' and the following token is a string
7039 literal, then we have a linkage specification. */
7040 if (token1.keyword == RID_EXTERN
7041 && cp_parser_is_string_literal (&token2))
7042 cp_parser_linkage_specification (parser);
7043 /* If the next token is `template', then we have either a template
7044 declaration, an explicit instantiation, or an explicit
7045 specialization. */
7046 else if (token1.keyword == RID_TEMPLATE)
7047 {
7048 /* `template <>' indicates a template specialization. */
7049 if (token2.type == CPP_LESS
7050 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7051 cp_parser_explicit_specialization (parser);
7052 /* `template <' indicates a template declaration. */
7053 else if (token2.type == CPP_LESS)
7054 cp_parser_template_declaration (parser, /*member_p=*/false);
7055 /* Anything else must be an explicit instantiation. */
7056 else
7057 cp_parser_explicit_instantiation (parser);
7058 }
7059 /* If the next token is `export', then we have a template
7060 declaration. */
7061 else if (token1.keyword == RID_EXPORT)
7062 cp_parser_template_declaration (parser, /*member_p=*/false);
7063 /* If the next token is `extern', 'static' or 'inline' and the one
7064 after that is `template', we have a GNU extended explicit
7065 instantiation directive. */
7066 else if (cp_parser_allow_gnu_extensions_p (parser)
7067 && (token1.keyword == RID_EXTERN
7068 || token1.keyword == RID_STATIC
7069 || token1.keyword == RID_INLINE)
7070 && token2.keyword == RID_TEMPLATE)
7071 cp_parser_explicit_instantiation (parser);
7072 /* If the next token is `namespace', check for a named or unnamed
7073 namespace definition. */
7074 else if (token1.keyword == RID_NAMESPACE
7075 && (/* A named namespace definition. */
7076 (token2.type == CPP_NAME
21526606 7077 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
0ed5edac 7078 != CPP_EQ))
a723baf1 7079 /* An unnamed namespace definition. */
aa09f986
JM
7080 || token2.type == CPP_OPEN_BRACE
7081 || token2.keyword == RID_ATTRIBUTE))
a723baf1 7082 cp_parser_namespace_definition (parser);
e58a9aa1
ZL
7083 /* Objective-C++ declaration/definition. */
7084 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7085 cp_parser_objc_declaration (parser);
a723baf1
MM
7086 /* We must have either a block declaration or a function
7087 definition. */
7088 else
7089 /* Try to parse a block-declaration, or a function-definition. */
7090 cp_parser_block_declaration (parser, /*statement_p=*/false);
058b15c1
MM
7091
7092 /* Free any declarators allocated. */
7093 obstack_free (&declarator_obstack, p);
a723baf1
MM
7094}
7095
21526606 7096/* Parse a block-declaration.
a723baf1
MM
7097
7098 block-declaration:
7099 simple-declaration
7100 asm-definition
7101 namespace-alias-definition
7102 using-declaration
21526606 7103 using-directive
a723baf1
MM
7104
7105 GNU Extension:
7106
7107 block-declaration:
21526606 7108 __extension__ block-declaration
a723baf1
MM
7109 label-declaration
7110
34cd5ae7 7111 If STATEMENT_P is TRUE, then this block-declaration is occurring as
a723baf1
MM
7112 part of a declaration-statement. */
7113
7114static void
21526606 7115cp_parser_block_declaration (cp_parser *parser,
a723baf1
MM
7116 bool statement_p)
7117{
7118 cp_token *token1;
7119 int saved_pedantic;
7120
7121 /* Check for the `__extension__' keyword. */
7122 if (cp_parser_extension_opt (parser, &saved_pedantic))
7123 {
7124 /* Parse the qualified declaration. */
7125 cp_parser_block_declaration (parser, statement_p);
7126 /* Restore the PEDANTIC flag. */
7127 pedantic = saved_pedantic;
7128
7129 return;
7130 }
7131
7132 /* Peek at the next token to figure out which kind of declaration is
7133 present. */
7134 token1 = cp_lexer_peek_token (parser->lexer);
7135
7136 /* If the next keyword is `asm', we have an asm-definition. */
7137 if (token1->keyword == RID_ASM)
7138 {
7139 if (statement_p)
7140 cp_parser_commit_to_tentative_parse (parser);
7141 cp_parser_asm_definition (parser);
7142 }
7143 /* If the next keyword is `namespace', we have a
7144 namespace-alias-definition. */
7145 else if (token1->keyword == RID_NAMESPACE)
7146 cp_parser_namespace_alias_definition (parser);
7147 /* If the next keyword is `using', we have either a
7148 using-declaration or a using-directive. */
7149 else if (token1->keyword == RID_USING)
7150 {
7151 cp_token *token2;
7152
7153 if (statement_p)
7154 cp_parser_commit_to_tentative_parse (parser);
7155 /* If the token after `using' is `namespace', then we have a
7156 using-directive. */
7157 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7158 if (token2->keyword == RID_NAMESPACE)
7159 cp_parser_using_directive (parser);
7160 /* Otherwise, it's a using-declaration. */
7161 else
7162 cp_parser_using_declaration (parser);
7163 }
7164 /* If the next keyword is `__label__' we have a label declaration. */
7165 else if (token1->keyword == RID_LABEL)
7166 {
7167 if (statement_p)
7168 cp_parser_commit_to_tentative_parse (parser);
7169 cp_parser_label_declaration (parser);
7170 }
7171 /* Anything else must be a simple-declaration. */
7172 else
7173 cp_parser_simple_declaration (parser, !statement_p);
7174}
7175
7176/* Parse a simple-declaration.
7177
7178 simple-declaration:
21526606 7179 decl-specifier-seq [opt] init-declarator-list [opt] ;
a723baf1
MM
7180
7181 init-declarator-list:
7182 init-declarator
21526606 7183 init-declarator-list , init-declarator
a723baf1 7184
34cd5ae7 7185 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9bcb9aae 7186 function-definition as a simple-declaration. */
a723baf1
MM
7187
7188static void
21526606 7189cp_parser_simple_declaration (cp_parser* parser,
0cbd7506 7190 bool function_definition_allowed_p)
a723baf1 7191{
62d1db17 7192 cp_decl_specifier_seq decl_specifiers;
560ad596 7193 int declares_class_or_enum;
a723baf1
MM
7194 bool saw_declarator;
7195
7196 /* Defer access checks until we know what is being declared; the
7197 checks for names appearing in the decl-specifier-seq should be
7198 done as if we were in the scope of the thing being declared. */
8d241e0b 7199 push_deferring_access_checks (dk_deferred);
cf22909c 7200
a723baf1
MM
7201 /* Parse the decl-specifier-seq. We have to keep track of whether
7202 or not the decl-specifier-seq declares a named class or
7203 enumeration type, since that is the only case in which the
21526606 7204 init-declarator-list is allowed to be empty.
a723baf1
MM
7205
7206 [dcl.dcl]
7207
7208 In a simple-declaration, the optional init-declarator-list can be
7209 omitted only when declaring a class or enumeration, that is when
7210 the decl-specifier-seq contains either a class-specifier, an
7211 elaborated-type-specifier, or an enum-specifier. */
62d1db17
MM
7212 cp_parser_decl_specifier_seq (parser,
7213 CP_PARSER_FLAGS_OPTIONAL,
7214 &decl_specifiers,
7215 &declares_class_or_enum);
a723baf1 7216 /* We no longer need to defer access checks. */
cf22909c 7217 stop_deferring_access_checks ();
24c0ef37 7218
39703eb9
MM
7219 /* In a block scope, a valid declaration must always have a
7220 decl-specifier-seq. By not trying to parse declarators, we can
7221 resolve the declaration/expression ambiguity more quickly. */
98ca843c 7222 if (!function_definition_allowed_p
62d1db17 7223 && !decl_specifiers.any_specifiers_p)
39703eb9
MM
7224 {
7225 cp_parser_error (parser, "expected declaration");
7226 goto done;
7227 }
7228
8fbc5ae7
MM
7229 /* If the next two tokens are both identifiers, the code is
7230 erroneous. The usual cause of this situation is code like:
7231
7232 T t;
7233
7234 where "T" should name a type -- but does not. */
de3fe73c
MM
7235 if (!decl_specifiers.type
7236 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8fbc5ae7 7237 {
8d241e0b 7238 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
7239 looking at a declaration. */
7240 cp_parser_commit_to_tentative_parse (parser);
7241 /* Give up. */
39703eb9 7242 goto done;
8fbc5ae7 7243 }
c8094d83 7244
996c2b52
MM
7245 /* If we have seen at least one decl-specifier, and the next token
7246 is not a parenthesis, then we must be looking at a declaration.
7247 (After "int (" we might be looking at a functional cast.) */
c8094d83 7248 if (decl_specifiers.any_specifiers_p
996c2b52
MM
7249 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7250 cp_parser_commit_to_tentative_parse (parser);
8fbc5ae7 7251
a723baf1
MM
7252 /* Keep going until we hit the `;' at the end of the simple
7253 declaration. */
7254 saw_declarator = false;
21526606 7255 while (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
7256 CPP_SEMICOLON))
7257 {
7258 cp_token *token;
7259 bool function_definition_p;
560ad596 7260 tree decl;
a723baf1 7261
6d328225
PM
7262 if (saw_declarator)
7263 {
7264 /* If we are processing next declarator, coma is expected */
7265 token = cp_lexer_peek_token (parser->lexer);
7266 gcc_assert (token->type == CPP_COMMA);
7267 cp_lexer_consume_token (parser->lexer);
7268 }
7269 else
7270 saw_declarator = true;
7271
a723baf1 7272 /* Parse the init-declarator. */
62d1db17 7273 decl = cp_parser_init_declarator (parser, &decl_specifiers,
560ad596
MM
7274 function_definition_allowed_p,
7275 /*member_p=*/false,
7276 declares_class_or_enum,
7277 &function_definition_p);
1fb3244a
MM
7278 /* If an error occurred while parsing tentatively, exit quickly.
7279 (That usually happens when in the body of a function; each
7280 statement is treated as a declaration-statement until proven
7281 otherwise.) */
7282 if (cp_parser_error_occurred (parser))
39703eb9 7283 goto done;
a723baf1
MM
7284 /* Handle function definitions specially. */
7285 if (function_definition_p)
7286 {
7287 /* If the next token is a `,', then we are probably
7288 processing something like:
7289
7290 void f() {}, *p;
7291
7292 which is erroneous. */
7293 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7294 error ("mixing declarations and function-definitions is forbidden");
7295 /* Otherwise, we're done with the list of declarators. */
7296 else
24c0ef37 7297 {
cf22909c 7298 pop_deferring_access_checks ();
24c0ef37
GS
7299 return;
7300 }
a723baf1
MM
7301 }
7302 /* The next token should be either a `,' or a `;'. */
7303 token = cp_lexer_peek_token (parser->lexer);
7304 /* If it's a `,', there are more declarators to come. */
7305 if (token->type == CPP_COMMA)
6d328225 7306 /* will be consumed next time around */;
a723baf1
MM
7307 /* If it's a `;', we are done. */
7308 else if (token->type == CPP_SEMICOLON)
7309 break;
7310 /* Anything else is an error. */
7311 else
7312 {
996c2b52
MM
7313 /* If we have already issued an error message we don't need
7314 to issue another one. */
7315 if (decl != error_mark_node
0b16f8f4 7316 || cp_parser_uncommitted_to_tentative_parse_p (parser))
2a13a625 7317 cp_parser_error (parser, "expected %<,%> or %<;%>");
a723baf1
MM
7318 /* Skip tokens until we reach the end of the statement. */
7319 cp_parser_skip_to_end_of_statement (parser);
5a98fa7b
MM
7320 /* If the next token is now a `;', consume it. */
7321 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7322 cp_lexer_consume_token (parser->lexer);
39703eb9 7323 goto done;
a723baf1
MM
7324 }
7325 /* After the first time around, a function-definition is not
7326 allowed -- even if it was OK at first. For example:
7327
0cbd7506 7328 int i, f() {}
a723baf1 7329
0cbd7506 7330 is not valid. */
a723baf1
MM
7331 function_definition_allowed_p = false;
7332 }
7333
7334 /* Issue an error message if no declarators are present, and the
7335 decl-specifier-seq does not itself declare a class or
7336 enumeration. */
7337 if (!saw_declarator)
7338 {
7339 if (cp_parser_declares_only_class_p (parser))
62d1db17 7340 shadow_tag (&decl_specifiers);
a723baf1 7341 /* Perform any deferred access checks. */
cf22909c 7342 perform_deferred_access_checks ();
a723baf1
MM
7343 }
7344
7345 /* Consume the `;'. */
7346 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7347
39703eb9
MM
7348 done:
7349 pop_deferring_access_checks ();
a723baf1
MM
7350}
7351
7352/* Parse a decl-specifier-seq.
7353
7354 decl-specifier-seq:
7355 decl-specifier-seq [opt] decl-specifier
7356
7357 decl-specifier:
7358 storage-class-specifier
7359 type-specifier
7360 function-specifier
7361 friend
21526606 7362 typedef
a723baf1
MM
7363
7364 GNU Extension:
7365
15077df5
MM
7366 decl-specifier:
7367 attributes
a723baf1 7368
62d1db17 7369 Set *DECL_SPECS to a representation of the decl-specifier-seq.
a723baf1 7370
eb1aef53 7371 The parser flags FLAGS is used to control type-specifier parsing.
560ad596
MM
7372
7373 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
543ca912 7374 flags:
560ad596
MM
7375
7376 1: one of the decl-specifiers is an elaborated-type-specifier
0cbd7506 7377 (i.e., a type declaration)
560ad596 7378 2: one of the decl-specifiers is an enum-specifier or a
0cbd7506 7379 class-specifier (i.e., a type definition)
98ca843c 7380
560ad596 7381 */
a723baf1 7382
62d1db17 7383static void
21526606 7384cp_parser_decl_specifier_seq (cp_parser* parser,
62d1db17
MM
7385 cp_parser_flags flags,
7386 cp_decl_specifier_seq *decl_specs,
560ad596 7387 int* declares_class_or_enum)
a723baf1 7388{
f2ce60b8 7389 bool constructor_possible_p = !parser->in_declarator_p;
28c84d63 7390 cp_decl_spec ds;
21526606 7391
62d1db17
MM
7392 /* Clear DECL_SPECS. */
7393 clear_decl_specs (decl_specs);
7394
a723baf1 7395 /* Assume no class or enumeration type is declared. */
560ad596 7396 *declares_class_or_enum = 0;
a723baf1 7397
a723baf1
MM
7398 /* Keep reading specifiers until there are no more to read. */
7399 while (true)
7400 {
a723baf1 7401 bool constructor_p;
62d1db17 7402 bool found_decl_spec;
a723baf1
MM
7403 cp_token *token;
7404
7405 /* Peek at the next token. */
7406 token = cp_lexer_peek_token (parser->lexer);
7407 /* Handle attributes. */
7408 if (token->keyword == RID_ATTRIBUTE)
7409 {
7410 /* Parse the attributes. */
98ca843c 7411 decl_specs->attributes
62d1db17
MM
7412 = chainon (decl_specs->attributes,
7413 cp_parser_attributes_opt (parser));
a723baf1
MM
7414 continue;
7415 }
62d1db17
MM
7416 /* Assume we will find a decl-specifier keyword. */
7417 found_decl_spec = true;
a723baf1
MM
7418 /* If the next token is an appropriate keyword, we can simply
7419 add it to the list. */
7420 switch (token->keyword)
7421 {
a723baf1
MM
7422 /* decl-specifier:
7423 friend */
62d1db17 7424 case RID_FRIEND:
28c84d63 7425 ++decl_specs->specs[(int) ds_friend];
a723baf1
MM
7426 /* Consume the token. */
7427 cp_lexer_consume_token (parser->lexer);
7428 break;
7429
7430 /* function-specifier:
7431 inline
7432 virtual
7433 explicit */
7434 case RID_INLINE:
7435 case RID_VIRTUAL:
7436 case RID_EXPLICIT:
62d1db17 7437 cp_parser_function_specifier_opt (parser, decl_specs);
a723baf1 7438 break;
21526606 7439
a723baf1
MM
7440 /* decl-specifier:
7441 typedef */
7442 case RID_TYPEDEF:
62d1db17 7443 ++decl_specs->specs[(int) ds_typedef];
a723baf1
MM
7444 /* Consume the token. */
7445 cp_lexer_consume_token (parser->lexer);
2050a1bb
MM
7446 /* A constructor declarator cannot appear in a typedef. */
7447 constructor_possible_p = false;
c006d942
MM
7448 /* The "typedef" keyword can only occur in a declaration; we
7449 may as well commit at this point. */
7450 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
7451 break;
7452
7453 /* storage-class-specifier:
7454 auto
7455 register
7456 static
7457 extern
21526606 7458 mutable
a723baf1 7459
0cbd7506 7460 GNU Extension:
a723baf1
MM
7461 thread */
7462 case RID_AUTO:
62d1db17
MM
7463 /* Consume the token. */
7464 cp_lexer_consume_token (parser->lexer);
7465 cp_parser_set_storage_class (decl_specs, sc_auto);
7466 break;
a723baf1 7467 case RID_REGISTER:
62d1db17
MM
7468 /* Consume the token. */
7469 cp_lexer_consume_token (parser->lexer);
7470 cp_parser_set_storage_class (decl_specs, sc_register);
7471 break;
a723baf1 7472 case RID_STATIC:
62d1db17
MM
7473 /* Consume the token. */
7474 cp_lexer_consume_token (parser->lexer);
7475 if (decl_specs->specs[(int) ds_thread])
f1b90a04 7476 {
2d01edd7 7477 error ("%<__thread%> before %<static%>");
f1b90a04
MM
7478 decl_specs->specs[(int) ds_thread] = 0;
7479 }
7480 cp_parser_set_storage_class (decl_specs, sc_static);
62d1db17 7481 break;
a723baf1 7482 case RID_EXTERN:
62d1db17
MM
7483 /* Consume the token. */
7484 cp_lexer_consume_token (parser->lexer);
7485 if (decl_specs->specs[(int) ds_thread])
f1b90a04 7486 {
2d01edd7 7487 error ("%<__thread%> before %<extern%>");
f1b90a04
MM
7488 decl_specs->specs[(int) ds_thread] = 0;
7489 }
7490 cp_parser_set_storage_class (decl_specs, sc_extern);
62d1db17 7491 break;
a723baf1 7492 case RID_MUTABLE:
62d1db17
MM
7493 /* Consume the token. */
7494 cp_lexer_consume_token (parser->lexer);
7495 cp_parser_set_storage_class (decl_specs, sc_mutable);
7496 break;
a723baf1 7497 case RID_THREAD:
62d1db17
MM
7498 /* Consume the token. */
7499 cp_lexer_consume_token (parser->lexer);
7500 ++decl_specs->specs[(int) ds_thread];
a723baf1 7501 break;
21526606 7502
a723baf1 7503 default:
62d1db17
MM
7504 /* We did not yet find a decl-specifier yet. */
7505 found_decl_spec = false;
a723baf1
MM
7506 break;
7507 }
7508
7509 /* Constructors are a special case. The `S' in `S()' is not a
7510 decl-specifier; it is the beginning of the declarator. */
98ca843c 7511 constructor_p
62d1db17
MM
7512 = (!found_decl_spec
7513 && constructor_possible_p
98ca843c 7514 && (cp_parser_constructor_declarator_p
62d1db17 7515 (parser, decl_specs->specs[(int) ds_friend] != 0)));
a723baf1
MM
7516
7517 /* If we don't have a DECL_SPEC yet, then we must be looking at
7518 a type-specifier. */
62d1db17 7519 if (!found_decl_spec && !constructor_p)
a723baf1 7520 {
560ad596 7521 int decl_spec_declares_class_or_enum;
a723baf1 7522 bool is_cv_qualifier;
62d1db17 7523 tree type_spec;
a723baf1 7524
62d1db17 7525 type_spec
a723baf1 7526 = cp_parser_type_specifier (parser, flags,
62d1db17 7527 decl_specs,
a723baf1
MM
7528 /*is_declaration=*/true,
7529 &decl_spec_declares_class_or_enum,
7530 &is_cv_qualifier);
7531
7532 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7533
7534 /* If this type-specifier referenced a user-defined type
7535 (a typedef, class-name, etc.), then we can't allow any
7536 more such type-specifiers henceforth.
7537
7538 [dcl.spec]
7539
7540 The longest sequence of decl-specifiers that could
7541 possibly be a type name is taken as the
7542 decl-specifier-seq of a declaration. The sequence shall
7543 be self-consistent as described below.
7544
7545 [dcl.type]
7546
7547 As a general rule, at most one type-specifier is allowed
7548 in the complete decl-specifier-seq of a declaration. The
7549 only exceptions are the following:
7550
7551 -- const or volatile can be combined with any other
21526606 7552 type-specifier.
a723baf1
MM
7553
7554 -- signed or unsigned can be combined with char, long,
7555 short, or int.
7556
7557 -- ..
7558
7559 Example:
7560
7561 typedef char* Pc;
7562 void g (const int Pc);
7563
7564 Here, Pc is *not* part of the decl-specifier seq; it's
7565 the declarator. Therefore, once we see a type-specifier
7566 (other than a cv-qualifier), we forbid any additional
7567 user-defined types. We *do* still allow things like `int
7568 int' to be considered a decl-specifier-seq, and issue the
7569 error message later. */
62d1db17 7570 if (type_spec && !is_cv_qualifier)
a723baf1 7571 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
2050a1bb 7572 /* A constructor declarator cannot follow a type-specifier. */
62d1db17 7573 if (type_spec)
a723baf1 7574 {
62d1db17
MM
7575 constructor_possible_p = false;
7576 found_decl_spec = true;
a723baf1 7577 }
a723baf1
MM
7578 }
7579
62d1db17
MM
7580 /* If we still do not have a DECL_SPEC, then there are no more
7581 decl-specifiers. */
7582 if (!found_decl_spec)
7583 break;
a723baf1 7584
62d1db17 7585 decl_specs->any_specifiers_p = true;
a723baf1
MM
7586 /* After we see one decl-specifier, further decl-specifiers are
7587 always optional. */
7588 flags |= CP_PARSER_FLAGS_OPTIONAL;
7589 }
7590
28c84d63
VR
7591 /* Check for repeated decl-specifiers. */
7592 for (ds = ds_first; ds != ds_last; ++ds)
7593 {
7594 unsigned count = decl_specs->specs[(int)ds];
7595 if (count < 2)
7596 continue;
7597 /* The "long" specifier is a special case because of "long long". */
7598 if (ds == ds_long)
7599 {
7600 if (count > 2)
7601 error ("%<long long long%> is too long for GCC");
7602 else if (pedantic && !in_system_header && warn_long_long)
7603 pedwarn ("ISO C++ does not support %<long long%>");
7604 }
7605 else if (count > 1)
7606 {
7607 static const char *const decl_spec_names[] = {
7608 "signed",
7609 "unsigned",
7610 "short",
7611 "long",
7612 "const",
7613 "volatile",
7614 "restrict",
7615 "inline",
7616 "virtual",
7617 "explicit",
7618 "friend",
7619 "typedef",
7620 "__complex",
7621 "__thread"
7622 };
7623 error ("duplicate %qs", decl_spec_names[(int)ds]);
7624 }
7625 }
7626
0426c4ca 7627 /* Don't allow a friend specifier with a class definition. */
62d1db17
MM
7628 if (decl_specs->specs[(int) ds_friend] != 0
7629 && (*declares_class_or_enum & 2))
0426c4ca 7630 error ("class definition may not be declared a friend");
a723baf1
MM
7631}
7632
21526606 7633/* Parse an (optional) storage-class-specifier.
a723baf1
MM
7634
7635 storage-class-specifier:
7636 auto
7637 register
7638 static
7639 extern
21526606 7640 mutable
a723baf1
MM
7641
7642 GNU Extension:
7643
7644 storage-class-specifier:
7645 thread
7646
7647 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
21526606 7648
a723baf1 7649static tree
94edc4ab 7650cp_parser_storage_class_specifier_opt (cp_parser* parser)
a723baf1
MM
7651{
7652 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7653 {
7654 case RID_AUTO:
7655 case RID_REGISTER:
7656 case RID_STATIC:
7657 case RID_EXTERN:
7658 case RID_MUTABLE:
7659 case RID_THREAD:
7660 /* Consume the token. */
7661 return cp_lexer_consume_token (parser->lexer)->value;
7662
7663 default:
7664 return NULL_TREE;
7665 }
7666}
7667
21526606 7668/* Parse an (optional) function-specifier.
a723baf1
MM
7669
7670 function-specifier:
7671 inline
7672 virtual
7673 explicit
7674
62d1db17
MM
7675 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7676 Updates DECL_SPECS, if it is non-NULL. */
21526606 7677
a723baf1 7678static tree
62d1db17
MM
7679cp_parser_function_specifier_opt (cp_parser* parser,
7680 cp_decl_specifier_seq *decl_specs)
a723baf1
MM
7681{
7682 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7683 {
7684 case RID_INLINE:
62d1db17
MM
7685 if (decl_specs)
7686 ++decl_specs->specs[(int) ds_inline];
7687 break;
7688
a723baf1 7689 case RID_VIRTUAL:
62d1db17
MM
7690 if (decl_specs)
7691 ++decl_specs->specs[(int) ds_virtual];
7692 break;
7693
a723baf1 7694 case RID_EXPLICIT:
62d1db17
MM
7695 if (decl_specs)
7696 ++decl_specs->specs[(int) ds_explicit];
7697 break;
a723baf1
MM
7698
7699 default:
7700 return NULL_TREE;
7701 }
62d1db17
MM
7702
7703 /* Consume the token. */
7704 return cp_lexer_consume_token (parser->lexer)->value;
a723baf1
MM
7705}
7706
7707/* Parse a linkage-specification.
7708
7709 linkage-specification:
7710 extern string-literal { declaration-seq [opt] }
7711 extern string-literal declaration */
7712
7713static void
94edc4ab 7714cp_parser_linkage_specification (cp_parser* parser)
a723baf1 7715{
a723baf1
MM
7716 tree linkage;
7717
7718 /* Look for the `extern' keyword. */
7719 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7720
c162c75e
MA
7721 /* Look for the string-literal. */
7722 linkage = cp_parser_string_literal (parser, false, false);
a723baf1
MM
7723
7724 /* Transform the literal into an identifier. If the literal is a
7725 wide-character string, or contains embedded NULs, then we can't
7726 handle it as the user wants. */
c162c75e
MA
7727 if (strlen (TREE_STRING_POINTER (linkage))
7728 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
a723baf1
MM
7729 {
7730 cp_parser_error (parser, "invalid linkage-specification");
7731 /* Assume C++ linkage. */
c162c75e 7732 linkage = lang_name_cplusplus;
a723baf1 7733 }
a723baf1 7734 else
c162c75e 7735 linkage = get_identifier (TREE_STRING_POINTER (linkage));
a723baf1
MM
7736
7737 /* We're now using the new linkage. */
7738 push_lang_context (linkage);
7739
7740 /* If the next token is a `{', then we're using the first
7741 production. */
7742 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7743 {
7744 /* Consume the `{' token. */
7745 cp_lexer_consume_token (parser->lexer);
7746 /* Parse the declarations. */
7747 cp_parser_declaration_seq_opt (parser);
7748 /* Look for the closing `}'. */
7749 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7750 }
7751 /* Otherwise, there's just one declaration. */
7752 else
7753 {
7754 bool saved_in_unbraced_linkage_specification_p;
7755
21526606 7756 saved_in_unbraced_linkage_specification_p
a723baf1
MM
7757 = parser->in_unbraced_linkage_specification_p;
7758 parser->in_unbraced_linkage_specification_p = true;
7759 have_extern_spec = true;
7760 cp_parser_declaration (parser);
7761 have_extern_spec = false;
21526606 7762 parser->in_unbraced_linkage_specification_p
a723baf1
MM
7763 = saved_in_unbraced_linkage_specification_p;
7764 }
7765
7766 /* We're done with the linkage-specification. */
7767 pop_lang_context ();
7768}
7769
7770/* Special member functions [gram.special] */
7771
7772/* Parse a conversion-function-id.
7773
7774 conversion-function-id:
21526606 7775 operator conversion-type-id
a723baf1
MM
7776
7777 Returns an IDENTIFIER_NODE representing the operator. */
7778
21526606 7779static tree
94edc4ab 7780cp_parser_conversion_function_id (cp_parser* parser)
a723baf1
MM
7781{
7782 tree type;
7783 tree saved_scope;
7784 tree saved_qualifying_scope;
7785 tree saved_object_scope;
4514aa8c 7786 tree pushed_scope = NULL_TREE;
a723baf1
MM
7787
7788 /* Look for the `operator' token. */
7789 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7790 return error_mark_node;
7791 /* When we parse the conversion-type-id, the current scope will be
7792 reset. However, we need that information in able to look up the
7793 conversion function later, so we save it here. */
7794 saved_scope = parser->scope;
7795 saved_qualifying_scope = parser->qualifying_scope;
7796 saved_object_scope = parser->object_scope;
7797 /* We must enter the scope of the class so that the names of
7798 entities declared within the class are available in the
7799 conversion-type-id. For example, consider:
7800
21526606 7801 struct S {
0cbd7506 7802 typedef int I;
a723baf1
MM
7803 operator I();
7804 };
7805
7806 S::operator I() { ... }
7807
7808 In order to see that `I' is a type-name in the definition, we
7809 must be in the scope of `S'. */
7810 if (saved_scope)
4514aa8c 7811 pushed_scope = push_scope (saved_scope);
a723baf1
MM
7812 /* Parse the conversion-type-id. */
7813 type = cp_parser_conversion_type_id (parser);
7814 /* Leave the scope of the class, if any. */
4514aa8c
NS
7815 if (pushed_scope)
7816 pop_scope (pushed_scope);
a723baf1
MM
7817 /* Restore the saved scope. */
7818 parser->scope = saved_scope;
7819 parser->qualifying_scope = saved_qualifying_scope;
7820 parser->object_scope = saved_object_scope;
7821 /* If the TYPE is invalid, indicate failure. */
7822 if (type == error_mark_node)
7823 return error_mark_node;
7824 return mangle_conv_op_name_for_type (type);
7825}
7826
7827/* Parse a conversion-type-id:
7828
7829 conversion-type-id:
7830 type-specifier-seq conversion-declarator [opt]
7831
7832 Returns the TYPE specified. */
7833
7834static tree
94edc4ab 7835cp_parser_conversion_type_id (cp_parser* parser)
a723baf1
MM
7836{
7837 tree attributes;
62d1db17 7838 cp_decl_specifier_seq type_specifiers;
058b15c1 7839 cp_declarator *declarator;
037cc9c5 7840 tree type_specified;
a723baf1
MM
7841
7842 /* Parse the attributes. */
7843 attributes = cp_parser_attributes_opt (parser);
7844 /* Parse the type-specifiers. */
d4113656
MM
7845 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7846 &type_specifiers);
a723baf1 7847 /* If that didn't work, stop. */
62d1db17 7848 if (type_specifiers.type == error_mark_node)
a723baf1
MM
7849 return error_mark_node;
7850 /* Parse the conversion-declarator. */
7851 declarator = cp_parser_conversion_declarator_opt (parser);
7852
037cc9c5 7853 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
0cbd7506 7854 /*initialized=*/0, &attributes);
037cc9c5
FJ
7855 if (attributes)
7856 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7857 return type_specified;
a723baf1
MM
7858}
7859
7860/* Parse an (optional) conversion-declarator.
7861
7862 conversion-declarator:
21526606 7863 ptr-operator conversion-declarator [opt]
a723baf1 7864
058b15c1 7865 */
a723baf1 7866
058b15c1 7867static cp_declarator *
94edc4ab 7868cp_parser_conversion_declarator_opt (cp_parser* parser)
a723baf1
MM
7869{
7870 enum tree_code code;
7871 tree class_type;
3c01e5df 7872 cp_cv_quals cv_quals;
a723baf1
MM
7873
7874 /* We don't know if there's a ptr-operator next, or not. */
7875 cp_parser_parse_tentatively (parser);
7876 /* Try the ptr-operator. */
3c01e5df 7877 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
a723baf1
MM
7878 /* If it worked, look for more conversion-declarators. */
7879 if (cp_parser_parse_definitely (parser))
7880 {
058b15c1 7881 cp_declarator *declarator;
98ca843c 7882
058b15c1
MM
7883 /* Parse another optional declarator. */
7884 declarator = cp_parser_conversion_declarator_opt (parser);
98ca843c 7885
058b15c1
MM
7886 /* Create the representation of the declarator. */
7887 if (class_type)
3c01e5df 7888 declarator = make_ptrmem_declarator (cv_quals, class_type,
a723baf1 7889 declarator);
058b15c1 7890 else if (code == INDIRECT_REF)
3c01e5df 7891 declarator = make_pointer_declarator (cv_quals, declarator);
058b15c1 7892 else
3c01e5df 7893 declarator = make_reference_declarator (cv_quals, declarator);
98ca843c 7894
058b15c1 7895 return declarator;
a723baf1
MM
7896 }
7897
058b15c1 7898 return NULL;
a723baf1
MM
7899}
7900
7901/* Parse an (optional) ctor-initializer.
7902
7903 ctor-initializer:
21526606 7904 : mem-initializer-list
a723baf1
MM
7905
7906 Returns TRUE iff the ctor-initializer was actually present. */
7907
7908static bool
94edc4ab 7909cp_parser_ctor_initializer_opt (cp_parser* parser)
a723baf1
MM
7910{
7911 /* If the next token is not a `:', then there is no
7912 ctor-initializer. */
7913 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7914 {
7915 /* Do default initialization of any bases and members. */
7916 if (DECL_CONSTRUCTOR_P (current_function_decl))
7917 finish_mem_initializers (NULL_TREE);
7918
7919 return false;
7920 }
7921
7922 /* Consume the `:' token. */
7923 cp_lexer_consume_token (parser->lexer);
7924 /* And the mem-initializer-list. */
7925 cp_parser_mem_initializer_list (parser);
7926
7927 return true;
7928}
7929
7930/* Parse a mem-initializer-list.
7931
7932 mem-initializer-list:
7933 mem-initializer
7934 mem-initializer , mem-initializer-list */
7935
7936static void
94edc4ab 7937cp_parser_mem_initializer_list (cp_parser* parser)
a723baf1
MM
7938{
7939 tree mem_initializer_list = NULL_TREE;
7940
7941 /* Let the semantic analysis code know that we are starting the
7942 mem-initializer-list. */
0e136342
MM
7943 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7944 error ("only constructors take base initializers");
a723baf1
MM
7945
7946 /* Loop through the list. */
7947 while (true)
7948 {
7949 tree mem_initializer;
7950
7951 /* Parse the mem-initializer. */
7952 mem_initializer = cp_parser_mem_initializer (parser);
7953 /* Add it to the list, unless it was erroneous. */
357d956e 7954 if (mem_initializer != error_mark_node)
a723baf1
MM
7955 {
7956 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7957 mem_initializer_list = mem_initializer;
7958 }
7959 /* If the next token is not a `,', we're done. */
7960 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7961 break;
7962 /* Consume the `,' token. */
7963 cp_lexer_consume_token (parser->lexer);
7964 }
7965
7966 /* Perform semantic analysis. */
0e136342
MM
7967 if (DECL_CONSTRUCTOR_P (current_function_decl))
7968 finish_mem_initializers (mem_initializer_list);
a723baf1
MM
7969}
7970
7971/* Parse a mem-initializer.
7972
7973 mem-initializer:
21526606 7974 mem-initializer-id ( expression-list [opt] )
a723baf1
MM
7975
7976 GNU extension:
21526606 7977
a723baf1 7978 mem-initializer:
34cd5ae7 7979 ( expression-list [opt] )
a723baf1
MM
7980
7981 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7982 class) or FIELD_DECL (for a non-static data member) to initialize;
357d956e
MM
7983 the TREE_VALUE is the expression-list. An empty initialization
7984 list is represented by void_list_node. */
a723baf1
MM
7985
7986static tree
94edc4ab 7987cp_parser_mem_initializer (cp_parser* parser)
a723baf1
MM
7988{
7989 tree mem_initializer_id;
7990 tree expression_list;
1f5a253a 7991 tree member;
21526606 7992
a723baf1
MM
7993 /* Find out what is being initialized. */
7994 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7995 {
7996 pedwarn ("anachronistic old-style base class initializer");
7997 mem_initializer_id = NULL_TREE;
7998 }
7999 else
8000 mem_initializer_id = cp_parser_mem_initializer_id (parser);
1f5a253a
NS
8001 member = expand_member_init (mem_initializer_id);
8002 if (member && !DECL_P (member))
8003 in_base_initializer = 1;
7efa3e22 8004
21526606 8005 expression_list
39703eb9 8006 = cp_parser_parenthesized_expression_list (parser, false,
93678513 8007 /*cast_p=*/false,
39703eb9 8008 /*non_constant_p=*/NULL);
357d956e
MM
8009 if (expression_list == error_mark_node)
8010 return error_mark_node;
7efa3e22 8011 if (!expression_list)
a723baf1 8012 expression_list = void_type_node;
a723baf1 8013
1f5a253a 8014 in_base_initializer = 0;
21526606 8015
357d956e 8016 return member ? build_tree_list (member, expression_list) : error_mark_node;
a723baf1
MM
8017}
8018
8019/* Parse a mem-initializer-id.
8020
8021 mem-initializer-id:
8022 :: [opt] nested-name-specifier [opt] class-name
21526606 8023 identifier
a723baf1
MM
8024
8025 Returns a TYPE indicating the class to be initializer for the first
8026 production. Returns an IDENTIFIER_NODE indicating the data member
8027 to be initialized for the second production. */
8028
8029static tree
94edc4ab 8030cp_parser_mem_initializer_id (cp_parser* parser)
a723baf1
MM
8031{
8032 bool global_scope_p;
8033 bool nested_name_specifier_p;
8a83a693 8034 bool template_p = false;
a723baf1
MM
8035 tree id;
8036
8a83a693
GB
8037 /* `typename' is not allowed in this context ([temp.res]). */
8038 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8039 {
2a13a625 8040 error ("keyword %<typename%> not allowed in this context (a qualified "
8a83a693
GB
8041 "member initializer is implicitly a type)");
8042 cp_lexer_consume_token (parser->lexer);
8043 }
a723baf1 8044 /* Look for the optional `::' operator. */
21526606
EC
8045 global_scope_p
8046 = (cp_parser_global_scope_opt (parser,
8047 /*current_scope_valid_p=*/false)
a723baf1
MM
8048 != NULL_TREE);
8049 /* Look for the optional nested-name-specifier. The simplest way to
8050 implement:
8051
8052 [temp.res]
8053
8054 The keyword `typename' is not permitted in a base-specifier or
8055 mem-initializer; in these contexts a qualified name that
8056 depends on a template-parameter is implicitly assumed to be a
8057 type name.
8058
8059 is to assume that we have seen the `typename' keyword at this
8060 point. */
21526606 8061 nested_name_specifier_p
a723baf1
MM
8062 = (cp_parser_nested_name_specifier_opt (parser,
8063 /*typename_keyword_p=*/true,
8064 /*check_dependency_p=*/true,
a668c6ad
MM
8065 /*type_p=*/true,
8066 /*is_declaration=*/true)
a723baf1 8067 != NULL_TREE);
8a83a693
GB
8068 if (nested_name_specifier_p)
8069 template_p = cp_parser_optional_template_keyword (parser);
a723baf1
MM
8070 /* If there is a `::' operator or a nested-name-specifier, then we
8071 are definitely looking for a class-name. */
8072 if (global_scope_p || nested_name_specifier_p)
8073 return cp_parser_class_name (parser,
8074 /*typename_keyword_p=*/true,
8a83a693 8075 /*template_keyword_p=*/template_p,
fc6a28d7 8076 none_type,
a723baf1 8077 /*check_dependency_p=*/true,
a668c6ad
MM
8078 /*class_head_p=*/false,
8079 /*is_declaration=*/true);
a723baf1
MM
8080 /* Otherwise, we could also be looking for an ordinary identifier. */
8081 cp_parser_parse_tentatively (parser);
8082 /* Try a class-name. */
21526606 8083 id = cp_parser_class_name (parser,
a723baf1
MM
8084 /*typename_keyword_p=*/true,
8085 /*template_keyword_p=*/false,
fc6a28d7 8086 none_type,
a723baf1 8087 /*check_dependency_p=*/true,
a668c6ad
MM
8088 /*class_head_p=*/false,
8089 /*is_declaration=*/true);
a723baf1
MM
8090 /* If we found one, we're done. */
8091 if (cp_parser_parse_definitely (parser))
8092 return id;
8093 /* Otherwise, look for an ordinary identifier. */
8094 return cp_parser_identifier (parser);
8095}
8096
8097/* Overloading [gram.over] */
8098
8099/* Parse an operator-function-id.
8100
8101 operator-function-id:
21526606 8102 operator operator
a723baf1
MM
8103
8104 Returns an IDENTIFIER_NODE for the operator which is a
8105 human-readable spelling of the identifier, e.g., `operator +'. */
8106
21526606 8107static tree
94edc4ab 8108cp_parser_operator_function_id (cp_parser* parser)
a723baf1
MM
8109{
8110 /* Look for the `operator' keyword. */
8111 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8112 return error_mark_node;
8113 /* And then the name of the operator itself. */
8114 return cp_parser_operator (parser);
8115}
8116
8117/* Parse an operator.
8118
8119 operator:
8120 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8121 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8122 || ++ -- , ->* -> () []
8123
8124 GNU Extensions:
21526606 8125
a723baf1
MM
8126 operator:
8127 <? >? <?= >?=
8128
8129 Returns an IDENTIFIER_NODE for the operator which is a
8130 human-readable spelling of the identifier, e.g., `operator +'. */
21526606 8131
a723baf1 8132static tree
94edc4ab 8133cp_parser_operator (cp_parser* parser)
a723baf1
MM
8134{
8135 tree id = NULL_TREE;
8136 cp_token *token;
8137
8138 /* Peek at the next token. */
8139 token = cp_lexer_peek_token (parser->lexer);
8140 /* Figure out which operator we have. */
8141 switch (token->type)
8142 {
8143 case CPP_KEYWORD:
8144 {
8145 enum tree_code op;
8146
8147 /* The keyword should be either `new' or `delete'. */
8148 if (token->keyword == RID_NEW)
8149 op = NEW_EXPR;
8150 else if (token->keyword == RID_DELETE)
8151 op = DELETE_EXPR;
8152 else
8153 break;
8154
8155 /* Consume the `new' or `delete' token. */
8156 cp_lexer_consume_token (parser->lexer);
8157
8158 /* Peek at the next token. */
8159 token = cp_lexer_peek_token (parser->lexer);
8160 /* If it's a `[' token then this is the array variant of the
8161 operator. */
8162 if (token->type == CPP_OPEN_SQUARE)
8163 {
8164 /* Consume the `[' token. */
8165 cp_lexer_consume_token (parser->lexer);
8166 /* Look for the `]' token. */
8167 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
21526606 8168 id = ansi_opname (op == NEW_EXPR
a723baf1
MM
8169 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8170 }
8171 /* Otherwise, we have the non-array variant. */
8172 else
8173 id = ansi_opname (op);
8174
8175 return id;
8176 }
8177
8178 case CPP_PLUS:
8179 id = ansi_opname (PLUS_EXPR);
8180 break;
8181
8182 case CPP_MINUS:
8183 id = ansi_opname (MINUS_EXPR);
8184 break;
8185
8186 case CPP_MULT:
8187 id = ansi_opname (MULT_EXPR);
8188 break;
8189
8190 case CPP_DIV:
8191 id = ansi_opname (TRUNC_DIV_EXPR);
8192 break;
8193
8194 case CPP_MOD:
8195 id = ansi_opname (TRUNC_MOD_EXPR);
8196 break;
8197
8198 case CPP_XOR:
8199 id = ansi_opname (BIT_XOR_EXPR);
8200 break;
8201
8202 case CPP_AND:
8203 id = ansi_opname (BIT_AND_EXPR);
8204 break;
8205
8206 case CPP_OR:
8207 id = ansi_opname (BIT_IOR_EXPR);
8208 break;
8209
8210 case CPP_COMPL:
8211 id = ansi_opname (BIT_NOT_EXPR);
8212 break;
21526606 8213
a723baf1
MM
8214 case CPP_NOT:
8215 id = ansi_opname (TRUTH_NOT_EXPR);
8216 break;
8217
8218 case CPP_EQ:
8219 id = ansi_assopname (NOP_EXPR);
8220 break;
8221
8222 case CPP_LESS:
8223 id = ansi_opname (LT_EXPR);
8224 break;
8225
8226 case CPP_GREATER:
8227 id = ansi_opname (GT_EXPR);
8228 break;
8229
8230 case CPP_PLUS_EQ:
8231 id = ansi_assopname (PLUS_EXPR);
8232 break;
8233
8234 case CPP_MINUS_EQ:
8235 id = ansi_assopname (MINUS_EXPR);
8236 break;
8237
8238 case CPP_MULT_EQ:
8239 id = ansi_assopname (MULT_EXPR);
8240 break;
8241
8242 case CPP_DIV_EQ:
8243 id = ansi_assopname (TRUNC_DIV_EXPR);
8244 break;
8245
8246 case CPP_MOD_EQ:
8247 id = ansi_assopname (TRUNC_MOD_EXPR);
8248 break;
8249
8250 case CPP_XOR_EQ:
8251 id = ansi_assopname (BIT_XOR_EXPR);
8252 break;
8253
8254 case CPP_AND_EQ:
8255 id = ansi_assopname (BIT_AND_EXPR);
8256 break;
8257
8258 case CPP_OR_EQ:
8259 id = ansi_assopname (BIT_IOR_EXPR);
8260 break;
8261
8262 case CPP_LSHIFT:
8263 id = ansi_opname (LSHIFT_EXPR);
8264 break;
8265
8266 case CPP_RSHIFT:
8267 id = ansi_opname (RSHIFT_EXPR);
8268 break;
8269
8270 case CPP_LSHIFT_EQ:
8271 id = ansi_assopname (LSHIFT_EXPR);
8272 break;
8273
8274 case CPP_RSHIFT_EQ:
8275 id = ansi_assopname (RSHIFT_EXPR);
8276 break;
8277
8278 case CPP_EQ_EQ:
8279 id = ansi_opname (EQ_EXPR);
8280 break;
8281
8282 case CPP_NOT_EQ:
8283 id = ansi_opname (NE_EXPR);
8284 break;
8285
8286 case CPP_LESS_EQ:
8287 id = ansi_opname (LE_EXPR);
8288 break;
8289
8290 case CPP_GREATER_EQ:
8291 id = ansi_opname (GE_EXPR);
8292 break;
8293
8294 case CPP_AND_AND:
8295 id = ansi_opname (TRUTH_ANDIF_EXPR);
8296 break;
8297
8298 case CPP_OR_OR:
8299 id = ansi_opname (TRUTH_ORIF_EXPR);
8300 break;
21526606 8301
a723baf1
MM
8302 case CPP_PLUS_PLUS:
8303 id = ansi_opname (POSTINCREMENT_EXPR);
8304 break;
8305
8306 case CPP_MINUS_MINUS:
8307 id = ansi_opname (PREDECREMENT_EXPR);
8308 break;
8309
8310 case CPP_COMMA:
8311 id = ansi_opname (COMPOUND_EXPR);
8312 break;
8313
8314 case CPP_DEREF_STAR:
8315 id = ansi_opname (MEMBER_REF);
8316 break;
8317
8318 case CPP_DEREF:
8319 id = ansi_opname (COMPONENT_REF);
8320 break;
8321
8322 case CPP_OPEN_PAREN:
8323 /* Consume the `('. */
8324 cp_lexer_consume_token (parser->lexer);
8325 /* Look for the matching `)'. */
8326 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8327 return ansi_opname (CALL_EXPR);
8328
8329 case CPP_OPEN_SQUARE:
8330 /* Consume the `['. */
8331 cp_lexer_consume_token (parser->lexer);
8332 /* Look for the matching `]'. */
8333 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8334 return ansi_opname (ARRAY_REF);
8335
8336 /* Extensions. */
8337 case CPP_MIN:
8338 id = ansi_opname (MIN_EXPR);
8ff24a79 8339 cp_parser_warn_min_max ();
a723baf1
MM
8340 break;
8341
8342 case CPP_MAX:
8343 id = ansi_opname (MAX_EXPR);
8ff24a79 8344 cp_parser_warn_min_max ();
a723baf1
MM
8345 break;
8346
8347 case CPP_MIN_EQ:
8348 id = ansi_assopname (MIN_EXPR);
8ff24a79 8349 cp_parser_warn_min_max ();
a723baf1
MM
8350 break;
8351
8352 case CPP_MAX_EQ:
8353 id = ansi_assopname (MAX_EXPR);
8ff24a79 8354 cp_parser_warn_min_max ();
a723baf1
MM
8355 break;
8356
8357 default:
8358 /* Anything else is an error. */
8359 break;
8360 }
8361
8362 /* If we have selected an identifier, we need to consume the
8363 operator token. */
8364 if (id)
8365 cp_lexer_consume_token (parser->lexer);
8366 /* Otherwise, no valid operator name was present. */
8367 else
8368 {
8369 cp_parser_error (parser, "expected operator");
8370 id = error_mark_node;
8371 }
8372
8373 return id;
8374}
8375
8376/* Parse a template-declaration.
8377
8378 template-declaration:
21526606 8379 export [opt] template < template-parameter-list > declaration
a723baf1
MM
8380
8381 If MEMBER_P is TRUE, this template-declaration occurs within a
21526606 8382 class-specifier.
a723baf1
MM
8383
8384 The grammar rule given by the standard isn't correct. What
8385 is really meant is:
8386
8387 template-declaration:
21526606 8388 export [opt] template-parameter-list-seq
a723baf1 8389 decl-specifier-seq [opt] init-declarator [opt] ;
21526606 8390 export [opt] template-parameter-list-seq
a723baf1
MM
8391 function-definition
8392
8393 template-parameter-list-seq:
8394 template-parameter-list-seq [opt]
8395 template < template-parameter-list > */
8396
8397static void
94edc4ab 8398cp_parser_template_declaration (cp_parser* parser, bool member_p)
a723baf1
MM
8399{
8400 /* Check for `export'. */
8401 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8402 {
8403 /* Consume the `export' token. */
8404 cp_lexer_consume_token (parser->lexer);
8405 /* Warn that we do not support `export'. */
d4ee4d25 8406 warning (0, "keyword %<export%> not implemented, and will be ignored");
a723baf1
MM
8407 }
8408
8409 cp_parser_template_declaration_after_export (parser, member_p);
8410}
8411
8412/* Parse a template-parameter-list.
8413
8414 template-parameter-list:
8415 template-parameter
8416 template-parameter-list , template-parameter
8417
8418 Returns a TREE_LIST. Each node represents a template parameter.
8419 The nodes are connected via their TREE_CHAINs. */
8420
8421static tree
94edc4ab 8422cp_parser_template_parameter_list (cp_parser* parser)
a723baf1
MM
8423{
8424 tree parameter_list = NULL_TREE;
8425
357d956e 8426 begin_template_parm_list ();
a723baf1
MM
8427 while (true)
8428 {
8429 tree parameter;
8430 cp_token *token;
058b15c1 8431 bool is_non_type;
a723baf1
MM
8432
8433 /* Parse the template-parameter. */
058b15c1 8434 parameter = cp_parser_template_parameter (parser, &is_non_type);
a723baf1 8435 /* Add it to the list. */
943e3ede
MM
8436 if (parameter != error_mark_node)
8437 parameter_list = process_template_parm (parameter_list,
8438 parameter,
8439 is_non_type);
a723baf1
MM
8440 /* Peek at the next token. */
8441 token = cp_lexer_peek_token (parser->lexer);
8442 /* If it's not a `,', we're done. */
8443 if (token->type != CPP_COMMA)
8444 break;
8445 /* Otherwise, consume the `,' token. */
8446 cp_lexer_consume_token (parser->lexer);
8447 }
8448
357d956e 8449 return end_template_parm_list (parameter_list);
a723baf1
MM
8450}
8451
8452/* Parse a template-parameter.
8453
8454 template-parameter:
8455 type-parameter
8456 parameter-declaration
8457
943e3ede
MM
8458 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8459 the parameter. The TREE_PURPOSE is the default value, if any.
8460 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8461 iff this parameter is a non-type parameter. */
a723baf1
MM
8462
8463static tree
058b15c1 8464cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
a723baf1
MM
8465{
8466 cp_token *token;
62d1db17 8467 cp_parameter_declarator *parameter_declarator;
943e3ede 8468 tree parm;
a723baf1 8469
058b15c1
MM
8470 /* Assume it is a type parameter or a template parameter. */
8471 *is_non_type = false;
a723baf1
MM
8472 /* Peek at the next token. */
8473 token = cp_lexer_peek_token (parser->lexer);
8474 /* If it is `class' or `template', we have a type-parameter. */
8475 if (token->keyword == RID_TEMPLATE)
8476 return cp_parser_type_parameter (parser);
8477 /* If it is `class' or `typename' we do not know yet whether it is a
8478 type parameter or a non-type parameter. Consider:
8479
8480 template <typename T, typename T::X X> ...
8481
8482 or:
21526606 8483
a723baf1
MM
8484 template <class C, class D*> ...
8485
8486 Here, the first parameter is a type parameter, and the second is
8487 a non-type parameter. We can tell by looking at the token after
8488 the identifier -- if it is a `,', `=', or `>' then we have a type
8489 parameter. */
8490 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8491 {
8492 /* Peek at the token after `class' or `typename'. */
8493 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8494 /* If it's an identifier, skip it. */
8495 if (token->type == CPP_NAME)
8496 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8497 /* Now, see if the token looks like the end of a template
8498 parameter. */
21526606 8499 if (token->type == CPP_COMMA
a723baf1
MM
8500 || token->type == CPP_EQ
8501 || token->type == CPP_GREATER)
8502 return cp_parser_type_parameter (parser);
8503 }
8504
21526606 8505 /* Otherwise, it is a non-type parameter.
a723baf1
MM
8506
8507 [temp.param]
8508
8509 When parsing a default template-argument for a non-type
8510 template-parameter, the first non-nested `>' is taken as the end
8511 of the template parameter-list rather than a greater-than
8512 operator. */
058b15c1
MM
8513 *is_non_type = true;
8514 parameter_declarator
8515 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8516 /*parenthesized_p=*/NULL);
943e3ede
MM
8517 parm = grokdeclarator (parameter_declarator->declarator,
8518 &parameter_declarator->decl_specifiers,
8519 PARM, /*initialized=*/0,
8520 /*attrlist=*/NULL);
8521 if (parm == error_mark_node)
8522 return error_mark_node;
8523 return build_tree_list (parameter_declarator->default_argument, parm);
a723baf1
MM
8524}
8525
8526/* Parse a type-parameter.
8527
8528 type-parameter:
8529 class identifier [opt]
8530 class identifier [opt] = type-id
8531 typename identifier [opt]
8532 typename identifier [opt] = type-id
8533 template < template-parameter-list > class identifier [opt]
21526606
EC
8534 template < template-parameter-list > class identifier [opt]
8535 = id-expression
a723baf1
MM
8536
8537 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8538 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8539 the declaration of the parameter. */
8540
8541static tree
94edc4ab 8542cp_parser_type_parameter (cp_parser* parser)
a723baf1
MM
8543{
8544 cp_token *token;
8545 tree parameter;
8546
8547 /* Look for a keyword to tell us what kind of parameter this is. */
21526606 8548 token = cp_parser_require (parser, CPP_KEYWORD,
8a6393df 8549 "`class', `typename', or `template'");
a723baf1
MM
8550 if (!token)
8551 return error_mark_node;
8552
8553 switch (token->keyword)
8554 {
8555 case RID_CLASS:
8556 case RID_TYPENAME:
8557 {
8558 tree identifier;
8559 tree default_argument;
8560
8561 /* If the next token is an identifier, then it names the
0cbd7506 8562 parameter. */
a723baf1
MM
8563 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8564 identifier = cp_parser_identifier (parser);
8565 else
8566 identifier = NULL_TREE;
8567
8568 /* Create the parameter. */
8569 parameter = finish_template_type_parm (class_type_node, identifier);
8570
8571 /* If the next token is an `=', we have a default argument. */
8572 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8573 {
8574 /* Consume the `=' token. */
8575 cp_lexer_consume_token (parser->lexer);
34cd5ae7 8576 /* Parse the default-argument. */
a723baf1
MM
8577 default_argument = cp_parser_type_id (parser);
8578 }
8579 else
8580 default_argument = NULL_TREE;
8581
8582 /* Create the combined representation of the parameter and the
8583 default argument. */
c67d36d0 8584 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
8585 }
8586 break;
8587
8588 case RID_TEMPLATE:
8589 {
8590 tree parameter_list;
8591 tree identifier;
8592 tree default_argument;
8593
8594 /* Look for the `<'. */
8595 cp_parser_require (parser, CPP_LESS, "`<'");
8596 /* Parse the template-parameter-list. */
357d956e 8597 parameter_list = cp_parser_template_parameter_list (parser);
a723baf1
MM
8598 /* Look for the `>'. */
8599 cp_parser_require (parser, CPP_GREATER, "`>'");
8600 /* Look for the `class' keyword. */
8601 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8602 /* If the next token is an `=', then there is a
8603 default-argument. If the next token is a `>', we are at
8604 the end of the parameter-list. If the next token is a `,',
8605 then we are at the end of this parameter. */
8606 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8607 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8608 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
71bd7186
MM
8609 {
8610 identifier = cp_parser_identifier (parser);
03fd3f84 8611 /* Treat invalid names as if the parameter were nameless. */
71bd7186
MM
8612 if (identifier == error_mark_node)
8613 identifier = NULL_TREE;
8614 }
a723baf1
MM
8615 else
8616 identifier = NULL_TREE;
71bd7186 8617
a723baf1
MM
8618 /* Create the template parameter. */
8619 parameter = finish_template_template_parm (class_type_node,
8620 identifier);
21526606 8621
a723baf1
MM
8622 /* If the next token is an `=', then there is a
8623 default-argument. */
8624 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8625 {
b0bc6e8e
KL
8626 bool is_template;
8627
a723baf1
MM
8628 /* Consume the `='. */
8629 cp_lexer_consume_token (parser->lexer);
8630 /* Parse the id-expression. */
21526606 8631 default_argument
a723baf1
MM
8632 = cp_parser_id_expression (parser,
8633 /*template_keyword_p=*/false,
8634 /*check_dependency_p=*/true,
b0bc6e8e 8635 /*template_p=*/&is_template,
fa6098f8
MM
8636 /*declarator_p=*/false,
8637 /*optional_p=*/false);
a3a503a5
GB
8638 if (TREE_CODE (default_argument) == TYPE_DECL)
8639 /* If the id-expression was a template-id that refers to
8640 a template-class, we already have the declaration here,
8641 so no further lookup is needed. */
8642 ;
8643 else
8644 /* Look up the name. */
21526606 8645 default_argument
a3a503a5 8646 = cp_parser_lookup_name (parser, default_argument,
fc6a28d7
MM
8647 none_type,
8648 /*is_template=*/is_template,
8649 /*is_namespace=*/false,
8650 /*check_dependency=*/true,
91b1ca65 8651 /*ambiguous_decls=*/NULL);
a723baf1
MM
8652 /* See if the default argument is valid. */
8653 default_argument
8654 = check_template_template_default_arg (default_argument);
8655 }
8656 else
8657 default_argument = NULL_TREE;
8658
8659 /* Create the combined representation of the parameter and the
8660 default argument. */
71bd7186 8661 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
8662 }
8663 break;
8664
8665 default:
71bd7186
MM
8666 gcc_unreachable ();
8667 break;
a723baf1 8668 }
21526606 8669
a723baf1
MM
8670 return parameter;
8671}
8672
8673/* Parse a template-id.
8674
8675 template-id:
8676 template-name < template-argument-list [opt] >
8677
8678 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8679 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8680 returned. Otherwise, if the template-name names a function, or set
8681 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
21526606 8682 names a class, returns a TYPE_DECL for the specialization.
a723baf1
MM
8683
8684 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8685 uninstantiated templates. */
8686
8687static tree
21526606
EC
8688cp_parser_template_id (cp_parser *parser,
8689 bool template_keyword_p,
a668c6ad
MM
8690 bool check_dependency_p,
8691 bool is_declaration)
a723baf1
MM
8692{
8693 tree template;
8694 tree arguments;
a723baf1 8695 tree template_id;
0c5e4866 8696 cp_token_position start_of_id = 0;
a723baf1 8697 tree access_check = NULL_TREE;
f4abade9 8698 cp_token *next_token, *next_token_2;
a668c6ad 8699 bool is_identifier;
a723baf1
MM
8700
8701 /* If the next token corresponds to a template-id, there is no need
8702 to reparse it. */
2050a1bb
MM
8703 next_token = cp_lexer_peek_token (parser->lexer);
8704 if (next_token->type == CPP_TEMPLATE_ID)
a723baf1
MM
8705 {
8706 tree value;
8707 tree check;
8708
8709 /* Get the stored value. */
8710 value = cp_lexer_consume_token (parser->lexer)->value;
8711 /* Perform any access checks that were deferred. */
8712 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c
KL
8713 perform_or_defer_access_check (TREE_PURPOSE (check),
8714 TREE_VALUE (check));
a723baf1
MM
8715 /* Return the stored value. */
8716 return TREE_VALUE (value);
8717 }
8718
2050a1bb
MM
8719 /* Avoid performing name lookup if there is no possibility of
8720 finding a template-id. */
8721 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8722 || (next_token->type == CPP_NAME
21526606 8723 && !cp_parser_nth_token_starts_template_argument_list_p
f4abade9 8724 (parser, 2)))
2050a1bb
MM
8725 {
8726 cp_parser_error (parser, "expected template-id");
8727 return error_mark_node;
8728 }
8729
a723baf1 8730 /* Remember where the template-id starts. */
0b16f8f4 8731 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
0c5e4866 8732 start_of_id = cp_lexer_token_position (parser->lexer, false);
a723baf1 8733
8d241e0b 8734 push_deferring_access_checks (dk_deferred);
cf22909c 8735
a723baf1 8736 /* Parse the template-name. */
a668c6ad 8737 is_identifier = false;
a723baf1 8738 template = cp_parser_template_name (parser, template_keyword_p,
a668c6ad
MM
8739 check_dependency_p,
8740 is_declaration,
8741 &is_identifier);
8742 if (template == error_mark_node || is_identifier)
cf22909c
KL
8743 {
8744 pop_deferring_access_checks ();
a668c6ad 8745 return template;
cf22909c 8746 }
a723baf1 8747
21526606 8748 /* If we find the sequence `[:' after a template-name, it's probably
f4abade9
GB
8749 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8750 parse correctly the argument list. */
2cfe82fe 8751 next_token = cp_lexer_peek_token (parser->lexer);
f4abade9 8752 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
21526606 8753 if (next_token->type == CPP_OPEN_SQUARE
f4abade9 8754 && next_token->flags & DIGRAPH
21526606 8755 && next_token_2->type == CPP_COLON
f4abade9 8756 && !(next_token_2->flags & PREV_WHITE))
cf22909c 8757 {
f4abade9
GB
8758 cp_parser_parse_tentatively (parser);
8759 /* Change `:' into `::'. */
8760 next_token_2->type = CPP_SCOPE;
8761 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
0cbd7506 8762 CPP_LESS. */
f4abade9
GB
8763 cp_lexer_consume_token (parser->lexer);
8764 /* Parse the arguments. */
8765 arguments = cp_parser_enclosed_template_argument_list (parser);
8766 if (!cp_parser_parse_definitely (parser))
8767 {
8768 /* If we couldn't parse an argument list, then we revert our changes
8769 and return simply an error. Maybe this is not a template-id
8770 after all. */
8771 next_token_2->type = CPP_COLON;
2a13a625 8772 cp_parser_error (parser, "expected %<<%>");
f4abade9
GB
8773 pop_deferring_access_checks ();
8774 return error_mark_node;
8775 }
8776 /* Otherwise, emit an error about the invalid digraph, but continue
0cbd7506 8777 parsing because we got our argument list. */
2a13a625
GDR
8778 pedwarn ("%<<::%> cannot begin a template-argument list");
8779 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8780 "between %<<%> and %<::%>");
f4abade9
GB
8781 if (!flag_permissive)
8782 {
8783 static bool hint;
8784 if (!hint)
8785 {
2a13a625 8786 inform ("(if you use -fpermissive G++ will accept your code)");
f4abade9
GB
8787 hint = true;
8788 }
8789 }
8790 }
8791 else
8792 {
8793 /* Look for the `<' that starts the template-argument-list. */
8794 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8795 {
8796 pop_deferring_access_checks ();
8797 return error_mark_node;
8798 }
8799 /* Parse the arguments. */
8800 arguments = cp_parser_enclosed_template_argument_list (parser);
cf22909c 8801 }
a723baf1
MM
8802
8803 /* Build a representation of the specialization. */
8804 if (TREE_CODE (template) == IDENTIFIER_NODE)
8805 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8806 else if (DECL_CLASS_TEMPLATE_P (template)
8807 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
21526606
EC
8808 template_id
8809 = finish_template_type (template, arguments,
8810 cp_lexer_next_token_is (parser->lexer,
a723baf1
MM
8811 CPP_SCOPE));
8812 else
8813 {
8814 /* If it's not a class-template or a template-template, it should be
8815 a function-template. */
50bc768d
NS
8816 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8817 || TREE_CODE (template) == OVERLOAD
8818 || BASELINK_P (template)));
21526606 8819
a723baf1
MM
8820 template_id = lookup_template_function (template, arguments);
8821 }
21526606 8822
cf22909c
KL
8823 /* Retrieve any deferred checks. Do not pop this access checks yet
8824 so the memory will not be reclaimed during token replacing below. */
8825 access_check = get_deferred_access_checks ();
8826
a723baf1
MM
8827 /* If parsing tentatively, replace the sequence of tokens that makes
8828 up the template-id with a CPP_TEMPLATE_ID token. That way,
8829 should we re-parse the token stream, we will not have to repeat
8830 the effort required to do the parse, nor will we issue duplicate
8831 error messages about problems during instantiation of the
e894ab29 8832 template. */
c8a7ed43 8833 if (start_of_id)
a723baf1 8834 {
0c5e4866 8835 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
c8094d83 8836
a723baf1
MM
8837 /* Reset the contents of the START_OF_ID token. */
8838 token->type = CPP_TEMPLATE_ID;
8839 token->value = build_tree_list (access_check, template_id);
8840 token->keyword = RID_MAX;
c8094d83 8841
a723baf1 8842 /* Purge all subsequent tokens. */
0c5e4866 8843 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
c8a7ed43
AO
8844
8845 /* ??? Can we actually assume that, if template_id ==
8846 error_mark_node, we will have issued a diagnostic to the
8847 user, as opposed to simply marking the tentative parse as
8848 failed? */
8849 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8850 error ("parse error in template argument list");
a723baf1
MM
8851 }
8852
cf22909c 8853 pop_deferring_access_checks ();
a723baf1
MM
8854 return template_id;
8855}
8856
8857/* Parse a template-name.
8858
8859 template-name:
8860 identifier
21526606 8861
a723baf1
MM
8862 The standard should actually say:
8863
8864 template-name:
8865 identifier
8866 operator-function-id
a723baf1
MM
8867
8868 A defect report has been filed about this issue.
8869
0d956474
GB
8870 A conversion-function-id cannot be a template name because they cannot
8871 be part of a template-id. In fact, looking at this code:
8872
8873 a.operator K<int>()
8874
8875 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
21526606 8876 It is impossible to call a templated conversion-function-id with an
0d956474
GB
8877 explicit argument list, since the only allowed template parameter is
8878 the type to which it is converting.
8879
a723baf1
MM
8880 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8881 `template' keyword, in a construction like:
8882
8883 T::template f<3>()
8884
8885 In that case `f' is taken to be a template-name, even though there
8886 is no way of knowing for sure.
8887
8888 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8889 name refers to a set of overloaded functions, at least one of which
8890 is a template, or an IDENTIFIER_NODE with the name of the template,
8891 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8892 names are looked up inside uninstantiated templates. */
8893
8894static tree
21526606 8895cp_parser_template_name (cp_parser* parser,
0cbd7506
MS
8896 bool template_keyword_p,
8897 bool check_dependency_p,
a668c6ad
MM
8898 bool is_declaration,
8899 bool *is_identifier)
a723baf1
MM
8900{
8901 tree identifier;
8902 tree decl;
8903 tree fns;
8904
8905 /* If the next token is `operator', then we have either an
8906 operator-function-id or a conversion-function-id. */
8907 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8908 {
8909 /* We don't know whether we're looking at an
8910 operator-function-id or a conversion-function-id. */
8911 cp_parser_parse_tentatively (parser);
8912 /* Try an operator-function-id. */
8913 identifier = cp_parser_operator_function_id (parser);
8914 /* If that didn't work, try a conversion-function-id. */
8915 if (!cp_parser_parse_definitely (parser))
0cbd7506 8916 {
0d956474
GB
8917 cp_parser_error (parser, "expected template-name");
8918 return error_mark_node;
0cbd7506 8919 }
a723baf1
MM
8920 }
8921 /* Look for the identifier. */
8922 else
8923 identifier = cp_parser_identifier (parser);
21526606 8924
a723baf1
MM
8925 /* If we didn't find an identifier, we don't have a template-id. */
8926 if (identifier == error_mark_node)
8927 return error_mark_node;
8928
8929 /* If the name immediately followed the `template' keyword, then it
8930 is a template-name. However, if the next token is not `<', then
8931 we do not treat it as a template-name, since it is not being used
8932 as part of a template-id. This enables us to handle constructs
8933 like:
8934
8935 template <typename T> struct S { S(); };
8936 template <typename T> S<T>::S();
8937
8938 correctly. We would treat `S' as a template -- if it were `S<T>'
8939 -- but we do not if there is no `<'. */
a668c6ad
MM
8940
8941 if (processing_template_decl
f4abade9 8942 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
a668c6ad
MM
8943 {
8944 /* In a declaration, in a dependent context, we pretend that the
8945 "template" keyword was present in order to improve error
8946 recovery. For example, given:
21526606 8947
a668c6ad 8948 template <typename T> void f(T::X<int>);
21526606 8949
a668c6ad 8950 we want to treat "X<int>" as a template-id. */
21526606
EC
8951 if (is_declaration
8952 && !template_keyword_p
a668c6ad 8953 && parser->scope && TYPE_P (parser->scope)
a52eb3bc 8954 && check_dependency_p
4e0f4df5
GB
8955 && dependent_type_p (parser->scope)
8956 /* Do not do this for dtors (or ctors), since they never
8957 need the template keyword before their name. */
8958 && !constructor_name_p (identifier, parser->scope))
a668c6ad 8959 {
0c5e4866 8960 cp_token_position start = 0;
c8094d83 8961
a668c6ad 8962 /* Explain what went wrong. */
2a13a625
GDR
8963 error ("non-template %qD used as template", identifier);
8964 inform ("use %<%T::template %D%> to indicate that it is a template",
4e0f4df5 8965 parser->scope, identifier);
0b16f8f4
VR
8966 /* If parsing tentatively, find the location of the "<" token. */
8967 if (cp_parser_simulate_error (parser))
8968 start = cp_lexer_token_position (parser->lexer, true);
a668c6ad
MM
8969 /* Parse the template arguments so that we can issue error
8970 messages about them. */
8971 cp_lexer_consume_token (parser->lexer);
8972 cp_parser_enclosed_template_argument_list (parser);
8973 /* Skip tokens until we find a good place from which to
8974 continue parsing. */
8975 cp_parser_skip_to_closing_parenthesis (parser,
8976 /*recovering=*/true,
8977 /*or_comma=*/true,
8978 /*consume_paren=*/false);
8979 /* If parsing tentatively, permanently remove the
8980 template argument list. That will prevent duplicate
8981 error messages from being issued about the missing
8982 "template" keyword. */
0c5e4866
NS
8983 if (start)
8984 cp_lexer_purge_tokens_after (parser->lexer, start);
a668c6ad
MM
8985 if (is_identifier)
8986 *is_identifier = true;
8987 return identifier;
8988 }
9d363a56
MM
8989
8990 /* If the "template" keyword is present, then there is generally
8991 no point in doing name-lookup, so we just return IDENTIFIER.
8992 But, if the qualifying scope is non-dependent then we can
8993 (and must) do name-lookup normally. */
8994 if (template_keyword_p
8995 && (!parser->scope
98ca843c 8996 || (TYPE_P (parser->scope)
9d363a56 8997 && dependent_type_p (parser->scope))))
a668c6ad
MM
8998 return identifier;
8999 }
a723baf1
MM
9000
9001 /* Look up the name. */
9002 decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 9003 none_type,
b0bc6e8e 9004 /*is_template=*/false,
eea9800f 9005 /*is_namespace=*/false,
8f78f01f 9006 check_dependency_p,
91b1ca65 9007 /*ambiguous_decls=*/NULL);
a723baf1
MM
9008 decl = maybe_get_template_decl_from_type_decl (decl);
9009
9010 /* If DECL is a template, then the name was a template-name. */
9011 if (TREE_CODE (decl) == TEMPLATE_DECL)
9012 ;
21526606 9013 else
a723baf1 9014 {
d58a2b83
MM
9015 tree fn = NULL_TREE;
9016
a723baf1
MM
9017 /* The standard does not explicitly indicate whether a name that
9018 names a set of overloaded declarations, some of which are
9019 templates, is a template-name. However, such a name should
9020 be a template-name; otherwise, there is no way to form a
9021 template-id for the overloaded templates. */
9022 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9023 if (TREE_CODE (fns) == OVERLOAD)
d58a2b83
MM
9024 for (fn = fns; fn; fn = OVL_NEXT (fn))
9025 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9026 break;
21526606 9027
d58a2b83 9028 if (!fn)
a723baf1 9029 {
d58a2b83 9030 /* The name does not name a template. */
a723baf1
MM
9031 cp_parser_error (parser, "expected template-name");
9032 return error_mark_node;
9033 }
9034 }
9035
9036 /* If DECL is dependent, and refers to a function, then just return
9037 its name; we will look it up again during template instantiation. */
9038 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9039 {
9040 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
1fb3244a 9041 if (TYPE_P (scope) && dependent_type_p (scope))
a723baf1
MM
9042 return identifier;
9043 }
9044
9045 return decl;
9046}
9047
9048/* Parse a template-argument-list.
9049
9050 template-argument-list:
9051 template-argument
9052 template-argument-list , template-argument
9053
04c06002 9054 Returns a TREE_VEC containing the arguments. */
a723baf1
MM
9055
9056static tree
94edc4ab 9057cp_parser_template_argument_list (cp_parser* parser)
a723baf1 9058{
bf12d54d
NS
9059 tree fixed_args[10];
9060 unsigned n_args = 0;
9061 unsigned alloced = 10;
9062 tree *arg_ary = fixed_args;
9063 tree vec;
4bb8ca28 9064 bool saved_in_template_argument_list_p;
5e9edb0f
MM
9065 bool saved_ice_p;
9066 bool saved_non_ice_p;
a723baf1 9067
4bb8ca28
MM
9068 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9069 parser->in_template_argument_list_p = true;
5e9edb0f
MM
9070 /* Even if the template-id appears in an integral
9071 constant-expression, the contents of the argument list do
9072 not. */
9073 saved_ice_p = parser->integral_constant_expression_p;
9074 parser->integral_constant_expression_p = false;
9075 saved_non_ice_p = parser->non_integral_constant_expression_p;
9076 parser->non_integral_constant_expression_p = false;
02ed62dd 9077 /* Parse the arguments. */
bf12d54d 9078 do
a723baf1
MM
9079 {
9080 tree argument;
9081
bf12d54d 9082 if (n_args)
04c06002 9083 /* Consume the comma. */
bf12d54d 9084 cp_lexer_consume_token (parser->lexer);
21526606 9085
a723baf1
MM
9086 /* Parse the template-argument. */
9087 argument = cp_parser_template_argument (parser);
bf12d54d
NS
9088 if (n_args == alloced)
9089 {
9090 alloced *= 2;
21526606 9091
bf12d54d
NS
9092 if (arg_ary == fixed_args)
9093 {
0ac1b889 9094 arg_ary = XNEWVEC (tree, alloced);
bf12d54d
NS
9095 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9096 }
9097 else
7767580e 9098 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
bf12d54d
NS
9099 }
9100 arg_ary[n_args++] = argument;
a723baf1 9101 }
bf12d54d
NS
9102 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9103
9104 vec = make_tree_vec (n_args);
a723baf1 9105
bf12d54d
NS
9106 while (n_args--)
9107 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
21526606 9108
bf12d54d
NS
9109 if (arg_ary != fixed_args)
9110 free (arg_ary);
5e9edb0f
MM
9111 parser->non_integral_constant_expression_p = saved_non_ice_p;
9112 parser->integral_constant_expression_p = saved_ice_p;
4bb8ca28 9113 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
bf12d54d 9114 return vec;
a723baf1
MM
9115}
9116
9117/* Parse a template-argument.
9118
9119 template-argument:
9120 assignment-expression
9121 type-id
9122 id-expression
9123
9124 The representation is that of an assignment-expression, type-id, or
9125 id-expression -- except that the qualified id-expression is
9126 evaluated, so that the value returned is either a DECL or an
21526606 9127 OVERLOAD.
d17811fd
MM
9128
9129 Although the standard says "assignment-expression", it forbids
9130 throw-expressions or assignments in the template argument.
9131 Therefore, we use "conditional-expression" instead. */
a723baf1
MM
9132
9133static tree
94edc4ab 9134cp_parser_template_argument (cp_parser* parser)
a723baf1
MM
9135{
9136 tree argument;
9137 bool template_p;
d17811fd 9138 bool address_p;
4d5297fa 9139 bool maybe_type_id = false;
d17811fd 9140 cp_token *token;
b3445994 9141 cp_id_kind idk;
a723baf1
MM
9142
9143 /* There's really no way to know what we're looking at, so we just
21526606 9144 try each alternative in order.
a723baf1
MM
9145
9146 [temp.arg]
9147
9148 In a template-argument, an ambiguity between a type-id and an
9149 expression is resolved to a type-id, regardless of the form of
21526606 9150 the corresponding template-parameter.
a723baf1
MM
9151
9152 Therefore, we try a type-id first. */
9153 cp_parser_parse_tentatively (parser);
a723baf1 9154 argument = cp_parser_type_id (parser);
4d5297fa 9155 /* If there was no error parsing the type-id but the next token is a '>>',
21526606 9156 we probably found a typo for '> >'. But there are type-id which are
4d5297fa
GB
9157 also valid expressions. For instance:
9158
9159 struct X { int operator >> (int); };
9160 template <int V> struct Foo {};
9161 Foo<X () >> 5> r;
9162
9163 Here 'X()' is a valid type-id of a function type, but the user just
9164 wanted to write the expression "X() >> 5". Thus, we remember that we
9165 found a valid type-id, but we still try to parse the argument as an
9166 expression to see what happens. */
9167 if (!cp_parser_error_occurred (parser)
9168 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9169 {
9170 maybe_type_id = true;
9171 cp_parser_abort_tentative_parse (parser);
9172 }
9173 else
9174 {
9175 /* If the next token isn't a `,' or a `>', then this argument wasn't
9176 really finished. This means that the argument is not a valid
9177 type-id. */
9178 if (!cp_parser_next_token_ends_template_argument_p (parser))
9179 cp_parser_error (parser, "expected template-argument");
9180 /* If that worked, we're done. */
9181 if (cp_parser_parse_definitely (parser))
9182 return argument;
9183 }
a723baf1
MM
9184 /* We're still not sure what the argument will be. */
9185 cp_parser_parse_tentatively (parser);
9186 /* Try a template. */
21526606 9187 argument = cp_parser_id_expression (parser,
a723baf1
MM
9188 /*template_keyword_p=*/false,
9189 /*check_dependency_p=*/true,
f3c2dfc6 9190 &template_p,
fa6098f8
MM
9191 /*declarator_p=*/false,
9192 /*optional_p=*/false);
a723baf1
MM
9193 /* If the next token isn't a `,' or a `>', then this argument wasn't
9194 really finished. */
d17811fd 9195 if (!cp_parser_next_token_ends_template_argument_p (parser))
a723baf1
MM
9196 cp_parser_error (parser, "expected template-argument");
9197 if (!cp_parser_error_occurred (parser))
9198 {
f746161e
MM
9199 /* Figure out what is being referred to. If the id-expression
9200 was for a class template specialization, then we will have a
9201 TYPE_DECL at this point. There is no need to do name lookup
9202 at this point in that case. */
9203 if (TREE_CODE (argument) != TYPE_DECL)
9204 argument = cp_parser_lookup_name (parser, argument,
fc6a28d7 9205 none_type,
f746161e
MM
9206 /*is_template=*/template_p,
9207 /*is_namespace=*/false,
8f78f01f 9208 /*check_dependency=*/true,
91b1ca65 9209 /*ambiguous_decls=*/NULL);
5b4acce1
KL
9210 if (TREE_CODE (argument) != TEMPLATE_DECL
9211 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
a723baf1
MM
9212 cp_parser_error (parser, "expected template-name");
9213 }
9214 if (cp_parser_parse_definitely (parser))
9215 return argument;
d17811fd
MM
9216 /* It must be a non-type argument. There permitted cases are given
9217 in [temp.arg.nontype]:
9218
9219 -- an integral constant-expression of integral or enumeration
0cbd7506 9220 type; or
d17811fd
MM
9221
9222 -- the name of a non-type template-parameter; or
9223
9224 -- the name of an object or function with external linkage...
9225
9226 -- the address of an object or function with external linkage...
9227
04c06002 9228 -- a pointer to member... */
d17811fd
MM
9229 /* Look for a non-type template parameter. */
9230 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9231 {
9232 cp_parser_parse_tentatively (parser);
9233 argument = cp_parser_primary_expression (parser,
02ed62dd 9234 /*adress_p=*/false,
93678513 9235 /*cast_p=*/false,
02ed62dd
MM
9236 /*template_arg_p=*/true,
9237 &idk);
d17811fd
MM
9238 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9239 || !cp_parser_next_token_ends_template_argument_p (parser))
9240 cp_parser_simulate_error (parser);
9241 if (cp_parser_parse_definitely (parser))
9242 return argument;
9243 }
db24eb1f 9244
d17811fd
MM
9245 /* If the next token is "&", the argument must be the address of an
9246 object or function with external linkage. */
9247 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9248 if (address_p)
9249 cp_lexer_consume_token (parser->lexer);
9250 /* See if we might have an id-expression. */
9251 token = cp_lexer_peek_token (parser->lexer);
9252 if (token->type == CPP_NAME
9253 || token->keyword == RID_OPERATOR
9254 || token->type == CPP_SCOPE
9255 || token->type == CPP_TEMPLATE_ID
9256 || token->type == CPP_NESTED_NAME_SPECIFIER)
9257 {
9258 cp_parser_parse_tentatively (parser);
9259 argument = cp_parser_primary_expression (parser,
02ed62dd 9260 address_p,
93678513 9261 /*cast_p=*/false,
02ed62dd
MM
9262 /*template_arg_p=*/true,
9263 &idk);
d17811fd
MM
9264 if (cp_parser_error_occurred (parser)
9265 || !cp_parser_next_token_ends_template_argument_p (parser))
9266 cp_parser_abort_tentative_parse (parser);
9267 else
9268 {
db24eb1f
NS
9269 if (TREE_CODE (argument) == INDIRECT_REF)
9270 {
9271 gcc_assert (REFERENCE_REF_P (argument));
9272 argument = TREE_OPERAND (argument, 0);
9273 }
c8094d83 9274
02ed62dd 9275 if (TREE_CODE (argument) == BASELINK)
2c164de6
MM
9276 /* We don't need the information about what class was used
9277 to name the overloaded functions. */
9278 argument = BASELINK_FUNCTIONS (argument);
9279
d17811fd
MM
9280 if (TREE_CODE (argument) == VAR_DECL)
9281 {
9282 /* A variable without external linkage might still be a
9283 valid constant-expression, so no error is issued here
9284 if the external-linkage check fails. */
9285 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9286 cp_parser_simulate_error (parser);
9287 }
9288 else if (is_overloaded_fn (argument))
9289 /* All overloaded functions are allowed; if the external
9290 linkage test does not pass, an error will be issued
9291 later. */
9292 ;
9293 else if (address_p
21526606 9294 && (TREE_CODE (argument) == OFFSET_REF
d17811fd
MM
9295 || TREE_CODE (argument) == SCOPE_REF))
9296 /* A pointer-to-member. */
9297 ;
db24eb1f
NS
9298 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9299 ;
d17811fd
MM
9300 else
9301 cp_parser_simulate_error (parser);
9302
9303 if (cp_parser_parse_definitely (parser))
9304 {
9305 if (address_p)
9306 argument = build_x_unary_op (ADDR_EXPR, argument);
9307 return argument;
9308 }
9309 }
9310 }
9311 /* If the argument started with "&", there are no other valid
9312 alternatives at this point. */
9313 if (address_p)
9314 {
9315 cp_parser_error (parser, "invalid non-type template argument");
9316 return error_mark_node;
9317 }
db24eb1f 9318
4d5297fa 9319 /* If the argument wasn't successfully parsed as a type-id followed
21526606 9320 by '>>', the argument can only be a constant expression now.
4d5297fa
GB
9321 Otherwise, we try parsing the constant-expression tentatively,
9322 because the argument could really be a type-id. */
9323 if (maybe_type_id)
9324 cp_parser_parse_tentatively (parser);
21526606 9325 argument = cp_parser_constant_expression (parser,
d17811fd
MM
9326 /*allow_non_constant_p=*/false,
9327 /*non_constant_p=*/NULL);
9baa27a9 9328 argument = fold_non_dependent_expr (argument);
4d5297fa
GB
9329 if (!maybe_type_id)
9330 return argument;
9331 if (!cp_parser_next_token_ends_template_argument_p (parser))
9332 cp_parser_error (parser, "expected template-argument");
9333 if (cp_parser_parse_definitely (parser))
9334 return argument;
9335 /* We did our best to parse the argument as a non type-id, but that
9336 was the only alternative that matched (albeit with a '>' after
21526606 9337 it). We can assume it's just a typo from the user, and a
4d5297fa
GB
9338 diagnostic will then be issued. */
9339 return cp_parser_type_id (parser);
a723baf1
MM
9340}
9341
9342/* Parse an explicit-instantiation.
9343
9344 explicit-instantiation:
21526606 9345 template declaration
a723baf1
MM
9346
9347 Although the standard says `declaration', what it really means is:
9348
9349 explicit-instantiation:
21526606 9350 template decl-specifier-seq [opt] declarator [opt] ;
a723baf1
MM
9351
9352 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9353 supposed to be allowed. A defect report has been filed about this
21526606 9354 issue.
a723baf1
MM
9355
9356 GNU Extension:
21526606 9357
a723baf1 9358 explicit-instantiation:
21526606 9359 storage-class-specifier template
a723baf1 9360 decl-specifier-seq [opt] declarator [opt] ;
21526606 9361 function-specifier template
a723baf1
MM
9362 decl-specifier-seq [opt] declarator [opt] ; */
9363
9364static void
94edc4ab 9365cp_parser_explicit_instantiation (cp_parser* parser)
a723baf1 9366{
560ad596 9367 int declares_class_or_enum;
62d1db17 9368 cp_decl_specifier_seq decl_specifiers;
a723baf1
MM
9369 tree extension_specifier = NULL_TREE;
9370
9371 /* Look for an (optional) storage-class-specifier or
9372 function-specifier. */
9373 if (cp_parser_allow_gnu_extensions_p (parser))
9374 {
21526606 9375 extension_specifier
a723baf1
MM
9376 = cp_parser_storage_class_specifier_opt (parser);
9377 if (!extension_specifier)
98ca843c 9378 extension_specifier
62d1db17
MM
9379 = cp_parser_function_specifier_opt (parser,
9380 /*decl_specs=*/NULL);
a723baf1
MM
9381 }
9382
9383 /* Look for the `template' keyword. */
9384 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9385 /* Let the front end know that we are processing an explicit
9386 instantiation. */
9387 begin_explicit_instantiation ();
9388 /* [temp.explicit] says that we are supposed to ignore access
9389 control while processing explicit instantiation directives. */
78757caa 9390 push_deferring_access_checks (dk_no_check);
a723baf1 9391 /* Parse a decl-specifier-seq. */
62d1db17
MM
9392 cp_parser_decl_specifier_seq (parser,
9393 CP_PARSER_FLAGS_OPTIONAL,
9394 &decl_specifiers,
9395 &declares_class_or_enum);
a723baf1
MM
9396 /* If there was exactly one decl-specifier, and it declared a class,
9397 and there's no declarator, then we have an explicit type
9398 instantiation. */
9399 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9400 {
9401 tree type;
9402
62d1db17 9403 type = check_tag_decl (&decl_specifiers);
b7fc8b57
KL
9404 /* Turn access control back on for names used during
9405 template instantiation. */
9406 pop_deferring_access_checks ();
a723baf1 9407 if (type)
8da15291
GDR
9408 do_type_instantiation (type, extension_specifier,
9409 /*complain=*/tf_error);
a723baf1
MM
9410 }
9411 else
9412 {
058b15c1 9413 cp_declarator *declarator;
a723baf1
MM
9414 tree decl;
9415
9416 /* Parse the declarator. */
21526606 9417 declarator
62b8a44e 9418 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 9419 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
9420 /*parenthesized_p=*/NULL,
9421 /*member_p=*/false);
fc6a28d7
MM
9422 if (declares_class_or_enum & 2)
9423 cp_parser_check_for_definition_in_return_type (declarator,
9424 decl_specifiers.type);
058b15c1 9425 if (declarator != cp_error_declarator)
216bb6e1 9426 {
62d1db17 9427 decl = grokdeclarator (declarator, &decl_specifiers,
216bb6e1
MM
9428 NORMAL, 0, NULL);
9429 /* Turn access control back on for names used during
9430 template instantiation. */
9431 pop_deferring_access_checks ();
9432 /* Do the explicit instantiation. */
9433 do_decl_instantiation (decl, extension_specifier);
9434 }
9435 else
9436 {
9437 pop_deferring_access_checks ();
9438 /* Skip the body of the explicit instantiation. */
9439 cp_parser_skip_to_end_of_statement (parser);
9440 }
a723baf1
MM
9441 }
9442 /* We're done with the instantiation. */
9443 end_explicit_instantiation ();
a723baf1 9444
e0860732 9445 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1
MM
9446}
9447
9448/* Parse an explicit-specialization.
9449
9450 explicit-specialization:
21526606 9451 template < > declaration
a723baf1
MM
9452
9453 Although the standard says `declaration', what it really means is:
9454
9455 explicit-specialization:
9456 template <> decl-specifier [opt] init-declarator [opt] ;
21526606 9457 template <> function-definition
a723baf1
MM
9458 template <> explicit-specialization
9459 template <> template-declaration */
9460
9461static void
94edc4ab 9462cp_parser_explicit_specialization (cp_parser* parser)
a723baf1 9463{
2f1b1731 9464 bool need_lang_pop;
a723baf1
MM
9465 /* Look for the `template' keyword. */
9466 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9467 /* Look for the `<'. */
9468 cp_parser_require (parser, CPP_LESS, "`<'");
9469 /* Look for the `>'. */
9470 cp_parser_require (parser, CPP_GREATER, "`>'");
9471 /* We have processed another parameter list. */
9472 ++parser->num_template_parameter_lists;
2f1b1731
MM
9473 /* [temp]
9474
9475 A template ... explicit specialization ... shall not have C
9476 linkage. */
9477 if (current_lang_name == lang_name_c)
9478 {
9479 error ("template specialization with C linkage");
9480 /* Give it C++ linkage to avoid confusing other parts of the
9481 front end. */
9482 push_lang_context (lang_name_cplusplus);
9483 need_lang_pop = true;
9484 }
9485 else
9486 need_lang_pop = false;
a723baf1
MM
9487 /* Let the front end know that we are beginning a specialization. */
9488 begin_specialization ();
a723baf1
MM
9489 /* If the next keyword is `template', we need to figure out whether
9490 or not we're looking a template-declaration. */
9491 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9492 {
9493 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9494 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9495 cp_parser_template_declaration_after_export (parser,
9496 /*member_p=*/false);
9497 else
9498 cp_parser_explicit_specialization (parser);
9499 }
9500 else
9501 /* Parse the dependent declaration. */
21526606 9502 cp_parser_single_declaration (parser,
a723baf1
MM
9503 /*member_p=*/false,
9504 /*friend_p=*/NULL);
a723baf1
MM
9505 /* We're done with the specialization. */
9506 end_specialization ();
2f1b1731
MM
9507 /* For the erroneous case of a template with C linkage, we pushed an
9508 implicit C++ linkage scope; exit that scope now. */
9509 if (need_lang_pop)
9510 pop_lang_context ();
a723baf1
MM
9511 /* We're done with this parameter list. */
9512 --parser->num_template_parameter_lists;
9513}
9514
9515/* Parse a type-specifier.
9516
9517 type-specifier:
9518 simple-type-specifier
9519 class-specifier
9520 enum-specifier
9521 elaborated-type-specifier
9522 cv-qualifier
9523
9524 GNU Extension:
9525
9526 type-specifier:
9527 __complex__
9528
62d1db17
MM
9529 Returns a representation of the type-specifier. For a
9530 class-specifier, enum-specifier, or elaborated-type-specifier, a
9531 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
a723baf1 9532
eb1aef53
KL
9533 The parser flags FLAGS is used to control type-specifier parsing.
9534
9535 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9536 in a decl-specifier-seq.
a723baf1
MM
9537
9538 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9539 class-specifier, enum-specifier, or elaborated-type-specifier, then
83a00410 9540 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
560ad596
MM
9541 if a type is declared; 2 if it is defined. Otherwise, it is set to
9542 zero.
a723baf1
MM
9543
9544 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9545 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9546 is set to FALSE. */
9547
9548static tree
21526606
EC
9549cp_parser_type_specifier (cp_parser* parser,
9550 cp_parser_flags flags,
62d1db17 9551 cp_decl_specifier_seq *decl_specs,
94edc4ab 9552 bool is_declaration,
560ad596 9553 int* declares_class_or_enum,
94edc4ab 9554 bool* is_cv_qualifier)
a723baf1
MM
9555{
9556 tree type_spec = NULL_TREE;
9557 cp_token *token;
9558 enum rid keyword;
62d1db17 9559 cp_decl_spec ds = ds_last;
a723baf1
MM
9560
9561 /* Assume this type-specifier does not declare a new type. */
9562 if (declares_class_or_enum)
543ca912 9563 *declares_class_or_enum = 0;
a723baf1
MM
9564 /* And that it does not specify a cv-qualifier. */
9565 if (is_cv_qualifier)
9566 *is_cv_qualifier = false;
9567 /* Peek at the next token. */
9568 token = cp_lexer_peek_token (parser->lexer);
9569
9570 /* If we're looking at a keyword, we can use that to guide the
9571 production we choose. */
9572 keyword = token->keyword;
9573 switch (keyword)
9574 {
ff4eb0b5
ZW
9575 case RID_ENUM:
9576 /* 'enum' [identifier] '{' introduces an enum-specifier;
9577 'enum' <anything else> introduces an elaborated-type-specifier. */
9578 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9579 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9580 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
0cbd7506 9581 == CPP_OPEN_BRACE))
ff4eb0b5 9582 {
a5e51518
KL
9583 if (parser->num_template_parameter_lists)
9584 {
9585 error ("template declaration of %qs", "enum");
9586 cp_parser_skip_to_end_of_block_or_statement (parser);
9587 type_spec = error_mark_node;
9588 }
9589 else
9590 type_spec = cp_parser_enum_specifier (parser);
9591
ff4eb0b5
ZW
9592 if (declares_class_or_enum)
9593 *declares_class_or_enum = 2;
9594 if (decl_specs)
9595 cp_parser_set_decl_spec_type (decl_specs,
9596 type_spec,
9597 /*user_defined_p=*/true);
9598 return type_spec;
9599 }
9600 else
9601 goto elaborated_type_specifier;
9602
a723baf1
MM
9603 /* Any of these indicate either a class-specifier, or an
9604 elaborated-type-specifier. */
9605 case RID_CLASS:
9606 case RID_STRUCT:
9607 case RID_UNION:
a723baf1 9608 /* Parse tentatively so that we can back up if we don't find a
ff4eb0b5 9609 class-specifier. */
a723baf1 9610 cp_parser_parse_tentatively (parser);
ff4eb0b5
ZW
9611 /* Look for the class-specifier. */
9612 type_spec = cp_parser_class_specifier (parser);
a723baf1
MM
9613 /* If that worked, we're done. */
9614 if (cp_parser_parse_definitely (parser))
9615 {
9616 if (declares_class_or_enum)
560ad596 9617 *declares_class_or_enum = 2;
62d1db17
MM
9618 if (decl_specs)
9619 cp_parser_set_decl_spec_type (decl_specs,
9620 type_spec,
9621 /*user_defined_p=*/true);
a723baf1
MM
9622 return type_spec;
9623 }
9624
9625 /* Fall through. */
ff4eb0b5
ZW
9626 elaborated_type_specifier:
9627 /* We're declaring (not defining) a class or enum. */
9628 if (declares_class_or_enum)
9629 *declares_class_or_enum = 1;
a723baf1 9630
ff4eb0b5 9631 /* Fall through. */
a723baf1
MM
9632 case RID_TYPENAME:
9633 /* Look for an elaborated-type-specifier. */
98ca843c
EC
9634 type_spec
9635 = (cp_parser_elaborated_type_specifier
62d1db17
MM
9636 (parser,
9637 decl_specs && decl_specs->specs[(int) ds_friend],
9638 is_declaration));
62d1db17
MM
9639 if (decl_specs)
9640 cp_parser_set_decl_spec_type (decl_specs,
9641 type_spec,
9642 /*user_defined_p=*/true);
a723baf1
MM
9643 return type_spec;
9644
9645 case RID_CONST:
62d1db17
MM
9646 ds = ds_const;
9647 if (is_cv_qualifier)
9648 *is_cv_qualifier = true;
9649 break;
98ca843c 9650
a723baf1 9651 case RID_VOLATILE:
62d1db17 9652 ds = ds_volatile;
a723baf1
MM
9653 if (is_cv_qualifier)
9654 *is_cv_qualifier = true;
62d1db17 9655 break;
a723baf1 9656
62d1db17
MM
9657 case RID_RESTRICT:
9658 ds = ds_restrict;
9659 if (is_cv_qualifier)
9660 *is_cv_qualifier = true;
9661 break;
a723baf1
MM
9662
9663 case RID_COMPLEX:
9664 /* The `__complex__' keyword is a GNU extension. */
62d1db17
MM
9665 ds = ds_complex;
9666 break;
a723baf1
MM
9667
9668 default:
9669 break;
9670 }
9671
62d1db17
MM
9672 /* Handle simple keywords. */
9673 if (ds != ds_last)
9674 {
9675 if (decl_specs)
9676 {
9677 ++decl_specs->specs[(int)ds];
9678 decl_specs->any_specifiers_p = true;
9679 }
9680 return cp_lexer_consume_token (parser->lexer)->value;
9681 }
9682
a723baf1
MM
9683 /* If we do not already have a type-specifier, assume we are looking
9684 at a simple-type-specifier. */
98ca843c 9685 type_spec = cp_parser_simple_type_specifier (parser,
62d1db17
MM
9686 decl_specs,
9687 flags);
a723baf1
MM
9688
9689 /* If we didn't find a type-specifier, and a type-specifier was not
9690 optional in this context, issue an error message. */
9691 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9692 {
9693 cp_parser_error (parser, "expected type specifier");
9694 return error_mark_node;
9695 }
9696
9697 return type_spec;
9698}
9699
9700/* Parse a simple-type-specifier.
9701
9702 simple-type-specifier:
9703 :: [opt] nested-name-specifier [opt] type-name
9704 :: [opt] nested-name-specifier template template-id
9705 char
9706 wchar_t
9707 bool
9708 short
9709 int
9710 long
9711 signed
9712 unsigned
9713 float
9714 double
21526606 9715 void
a723baf1
MM
9716
9717 GNU Extension:
9718
9719 simple-type-specifier:
9720 __typeof__ unary-expression
9721 __typeof__ ( type-id )
9722
62d1db17
MM
9723 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9724 appropriately updated. */
a723baf1
MM
9725
9726static tree
98ca843c 9727cp_parser_simple_type_specifier (cp_parser* parser,
62d1db17
MM
9728 cp_decl_specifier_seq *decl_specs,
9729 cp_parser_flags flags)
a723baf1
MM
9730{
9731 tree type = NULL_TREE;
9732 cp_token *token;
9733
9734 /* Peek at the next token. */
9735 token = cp_lexer_peek_token (parser->lexer);
9736
9737 /* If we're looking at a keyword, things are easy. */
9738 switch (token->keyword)
9739 {
9740 case RID_CHAR:
62d1db17
MM
9741 if (decl_specs)
9742 decl_specs->explicit_char_p = true;
4b0d3cbe
MM
9743 type = char_type_node;
9744 break;
a723baf1 9745 case RID_WCHAR:
4b0d3cbe
MM
9746 type = wchar_type_node;
9747 break;
a723baf1 9748 case RID_BOOL:
4b0d3cbe
MM
9749 type = boolean_type_node;
9750 break;
a723baf1 9751 case RID_SHORT:
62d1db17
MM
9752 if (decl_specs)
9753 ++decl_specs->specs[(int) ds_short];
4b0d3cbe
MM
9754 type = short_integer_type_node;
9755 break;
a723baf1 9756 case RID_INT:
62d1db17
MM
9757 if (decl_specs)
9758 decl_specs->explicit_int_p = true;
4b0d3cbe
MM
9759 type = integer_type_node;
9760 break;
a723baf1 9761 case RID_LONG:
62d1db17
MM
9762 if (decl_specs)
9763 ++decl_specs->specs[(int) ds_long];
4b0d3cbe
MM
9764 type = long_integer_type_node;
9765 break;
a723baf1 9766 case RID_SIGNED:
62d1db17
MM
9767 if (decl_specs)
9768 ++decl_specs->specs[(int) ds_signed];
4b0d3cbe
MM
9769 type = integer_type_node;
9770 break;
a723baf1 9771 case RID_UNSIGNED:
62d1db17
MM
9772 if (decl_specs)
9773 ++decl_specs->specs[(int) ds_unsigned];
4b0d3cbe
MM
9774 type = unsigned_type_node;
9775 break;
a723baf1 9776 case RID_FLOAT:
4b0d3cbe
MM
9777 type = float_type_node;
9778 break;
a723baf1 9779 case RID_DOUBLE:
4b0d3cbe
MM
9780 type = double_type_node;
9781 break;
a723baf1 9782 case RID_VOID:
4b0d3cbe
MM
9783 type = void_type_node;
9784 break;
a723baf1
MM
9785
9786 case RID_TYPEOF:
62d1db17
MM
9787 /* Consume the `typeof' token. */
9788 cp_lexer_consume_token (parser->lexer);
9789 /* Parse the operand to `typeof'. */
9790 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9791 /* If it is not already a TYPE, take its type. */
9792 if (!TYPE_P (type))
9793 type = finish_typeof (type);
9794
9795 if (decl_specs)
9796 cp_parser_set_decl_spec_type (decl_specs, type,
9797 /*user_defined_p=*/true);
98ca843c 9798
62d1db17 9799 return type;
a723baf1
MM
9800
9801 default:
9802 break;
9803 }
9804
4b0d3cbe
MM
9805 /* If the type-specifier was for a built-in type, we're done. */
9806 if (type)
9807 {
9808 tree id;
9809
62d1db17
MM
9810 /* Record the type. */
9811 if (decl_specs
9812 && (token->keyword != RID_SIGNED
9813 && token->keyword != RID_UNSIGNED
9814 && token->keyword != RID_SHORT
9815 && token->keyword != RID_LONG))
98ca843c 9816 cp_parser_set_decl_spec_type (decl_specs,
62d1db17
MM
9817 type,
9818 /*user_defined=*/false);
9819 if (decl_specs)
9820 decl_specs->any_specifiers_p = true;
9821
4b0d3cbe
MM
9822 /* Consume the token. */
9823 id = cp_lexer_consume_token (parser->lexer)->value;
0d956474
GB
9824
9825 /* There is no valid C++ program where a non-template type is
9826 followed by a "<". That usually indicates that the user thought
9827 that the type was a template. */
9828 cp_parser_check_for_invalid_template_id (parser, type);
9829
62d1db17 9830 return TYPE_NAME (type);
4b0d3cbe
MM
9831 }
9832
a723baf1 9833 /* The type-specifier must be a user-defined type. */
21526606 9834 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
a723baf1 9835 {
0c1a1ecd 9836 bool qualified_p;
f68e4dc8 9837 bool global_p;
0c1a1ecd 9838
a723baf1
MM
9839 /* Don't gobble tokens or issue error messages if this is an
9840 optional type-specifier. */
9841 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9842 cp_parser_parse_tentatively (parser);
9843
9844 /* Look for the optional `::' operator. */
f68e4dc8 9845 global_p
da740453
MM
9846 = (cp_parser_global_scope_opt (parser,
9847 /*current_scope_valid_p=*/false)
9848 != NULL_TREE);
a723baf1 9849 /* Look for the nested-name specifier. */
0c1a1ecd
MM
9850 qualified_p
9851 = (cp_parser_nested_name_specifier_opt (parser,
9852 /*typename_keyword_p=*/false,
9853 /*check_dependency_p=*/true,
9854 /*type_p=*/false,
6661a85f
EB
9855 /*is_declaration=*/false)
9856 != NULL_TREE);
a723baf1
MM
9857 /* If we have seen a nested-name-specifier, and the next token
9858 is `template', then we are using the template-id production. */
21526606 9859 if (parser->scope
a723baf1
MM
9860 && cp_parser_optional_template_keyword (parser))
9861 {
9862 /* Look for the template-id. */
21526606 9863 type = cp_parser_template_id (parser,
a723baf1 9864 /*template_keyword_p=*/true,
a668c6ad
MM
9865 /*check_dependency_p=*/true,
9866 /*is_declaration=*/false);
a723baf1
MM
9867 /* If the template-id did not name a type, we are out of
9868 luck. */
9869 if (TREE_CODE (type) != TYPE_DECL)
9870 {
9871 cp_parser_error (parser, "expected template-id for type");
9872 type = NULL_TREE;
9873 }
9874 }
9875 /* Otherwise, look for a type-name. */
9876 else
4bb8ca28 9877 type = cp_parser_type_name (parser);
0c1a1ecd 9878 /* Keep track of all name-lookups performed in class scopes. */
98ca843c 9879 if (type
f68e4dc8 9880 && !global_p
0c1a1ecd 9881 && !qualified_p
98ca843c 9882 && TREE_CODE (type) == TYPE_DECL
0c1a1ecd
MM
9883 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9884 maybe_note_name_used_in_class (DECL_NAME (type), type);
a723baf1 9885 /* If it didn't work out, we don't have a TYPE. */
21526606 9886 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
a723baf1
MM
9887 && !cp_parser_parse_definitely (parser))
9888 type = NULL_TREE;
62d1db17
MM
9889 if (type && decl_specs)
9890 cp_parser_set_decl_spec_type (decl_specs, type,
9891 /*user_defined=*/true);
a723baf1
MM
9892 }
9893
9894 /* If we didn't get a type-name, issue an error message. */
9895 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9896 {
9897 cp_parser_error (parser, "expected type-name");
9898 return error_mark_node;
9899 }
9900
a668c6ad
MM
9901 /* There is no valid C++ program where a non-template type is
9902 followed by a "<". That usually indicates that the user thought
9903 that the type was a template. */
4bb8ca28 9904 if (type && type != error_mark_node)
e58a9aa1
ZL
9905 {
9906 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9907 If it is, then the '<'...'>' enclose protocol names rather than
9908 template arguments, and so everything is fine. */
9909 if (c_dialect_objc ()
9910 && (objc_is_id (type) || objc_is_class_name (type)))
9911 {
9912 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9913 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9914
9915 /* Clobber the "unqualified" type previously entered into
128a79fb 9916 DECL_SPECS with the new, improved protocol-qualified version. */
e58a9aa1
ZL
9917 if (decl_specs)
9918 decl_specs->type = qual_type;
9919
9920 return qual_type;
9921 }
9922
9923 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
c8094d83 9924 }
ec75414f 9925
a723baf1
MM
9926 return type;
9927}
9928
9929/* Parse a type-name.
9930
9931 type-name:
9932 class-name
9933 enum-name
21526606 9934 typedef-name
a723baf1
MM
9935
9936 enum-name:
9937 identifier
9938
9939 typedef-name:
21526606 9940 identifier
a723baf1 9941
78dcd41a 9942 Returns a TYPE_DECL for the type. */
a723baf1
MM
9943
9944static tree
94edc4ab 9945cp_parser_type_name (cp_parser* parser)
a723baf1
MM
9946{
9947 tree type_decl;
9948 tree identifier;
9949
9950 /* We can't know yet whether it is a class-name or not. */
9951 cp_parser_parse_tentatively (parser);
9952 /* Try a class-name. */
21526606 9953 type_decl = cp_parser_class_name (parser,
a723baf1
MM
9954 /*typename_keyword_p=*/false,
9955 /*template_keyword_p=*/false,
fc6a28d7 9956 none_type,
a723baf1 9957 /*check_dependency_p=*/true,
a668c6ad
MM
9958 /*class_head_p=*/false,
9959 /*is_declaration=*/false);
a723baf1
MM
9960 /* If it's not a class-name, keep looking. */
9961 if (!cp_parser_parse_definitely (parser))
9962 {
9963 /* It must be a typedef-name or an enum-name. */
9964 identifier = cp_parser_identifier (parser);
9965 if (identifier == error_mark_node)
9966 return error_mark_node;
21526606 9967
a723baf1
MM
9968 /* Look up the type-name. */
9969 type_decl = cp_parser_lookup_name_simple (parser, identifier);
e58a9aa1
ZL
9970
9971 if (TREE_CODE (type_decl) != TYPE_DECL
9972 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9973 {
9974 /* See if this is an Objective-C type. */
9975 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9976 tree type = objc_get_protocol_qualified_type (identifier, protos);
c8094d83 9977 if (type)
e58a9aa1
ZL
9978 type_decl = TYPE_NAME (type);
9979 }
9980
a723baf1
MM
9981 /* Issue an error if we did not find a type-name. */
9982 if (TREE_CODE (type_decl) != TYPE_DECL)
9983 {
4bb8ca28 9984 if (!cp_parser_simulate_error (parser))
21526606 9985 cp_parser_name_lookup_error (parser, identifier, type_decl,
4bb8ca28 9986 "is not a type");
a723baf1
MM
9987 type_decl = error_mark_node;
9988 }
9989 /* Remember that the name was used in the definition of the
9990 current class so that we can check later to see if the
9991 meaning would have been different after the class was
9992 entirely defined. */
9993 else if (type_decl != error_mark_node
9994 && !parser->scope)
9995 maybe_note_name_used_in_class (identifier, type_decl);
9996 }
21526606 9997
a723baf1
MM
9998 return type_decl;
9999}
10000
10001
10002/* Parse an elaborated-type-specifier. Note that the grammar given
10003 here incorporates the resolution to DR68.
10004
10005 elaborated-type-specifier:
10006 class-key :: [opt] nested-name-specifier [opt] identifier
10007 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10008 enum :: [opt] nested-name-specifier [opt] identifier
10009 typename :: [opt] nested-name-specifier identifier
21526606
EC
10010 typename :: [opt] nested-name-specifier template [opt]
10011 template-id
a723baf1 10012
360d1b99
MM
10013 GNU extension:
10014
10015 elaborated-type-specifier:
10016 class-key attributes :: [opt] nested-name-specifier [opt] identifier
21526606 10017 class-key attributes :: [opt] nested-name-specifier [opt]
0cbd7506 10018 template [opt] template-id
360d1b99
MM
10019 enum attributes :: [opt] nested-name-specifier [opt] identifier
10020
a723baf1
MM
10021 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10022 declared `friend'. If IS_DECLARATION is TRUE, then this
10023 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10024 something is being declared.
10025
10026 Returns the TYPE specified. */
10027
10028static tree
21526606 10029cp_parser_elaborated_type_specifier (cp_parser* parser,
0cbd7506
MS
10030 bool is_friend,
10031 bool is_declaration)
a723baf1
MM
10032{
10033 enum tag_types tag_type;
10034 tree identifier;
10035 tree type = NULL_TREE;
360d1b99 10036 tree attributes = NULL_TREE;
a723baf1
MM
10037
10038 /* See if we're looking at the `enum' keyword. */
10039 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10040 {
10041 /* Consume the `enum' token. */
10042 cp_lexer_consume_token (parser->lexer);
10043 /* Remember that it's an enumeration type. */
10044 tag_type = enum_type;
360d1b99
MM
10045 /* Parse the attributes. */
10046 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
10047 }
10048 /* Or, it might be `typename'. */
10049 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10050 RID_TYPENAME))
10051 {
10052 /* Consume the `typename' token. */
10053 cp_lexer_consume_token (parser->lexer);
10054 /* Remember that it's a `typename' type. */
10055 tag_type = typename_type;
10056 /* The `typename' keyword is only allowed in templates. */
10057 if (!processing_template_decl)
2a13a625 10058 pedwarn ("using %<typename%> outside of template");
a723baf1
MM
10059 }
10060 /* Otherwise it must be a class-key. */
10061 else
10062 {
10063 tag_type = cp_parser_class_key (parser);
10064 if (tag_type == none_type)
10065 return error_mark_node;
360d1b99
MM
10066 /* Parse the attributes. */
10067 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
10068 }
10069
10070 /* Look for the `::' operator. */
21526606 10071 cp_parser_global_scope_opt (parser,
a723baf1
MM
10072 /*current_scope_valid_p=*/false);
10073 /* Look for the nested-name-specifier. */
10074 if (tag_type == typename_type)
8fa1ad0e 10075 {
8fe4d24b 10076 if (!cp_parser_nested_name_specifier (parser,
8fa1ad0e
MM
10077 /*typename_keyword_p=*/true,
10078 /*check_dependency_p=*/true,
a668c6ad 10079 /*type_p=*/true,
8fe4d24b 10080 is_declaration))
8fa1ad0e
MM
10081 return error_mark_node;
10082 }
a723baf1
MM
10083 else
10084 /* Even though `typename' is not present, the proposed resolution
10085 to Core Issue 180 says that in `class A<T>::B', `B' should be
10086 considered a type-name, even if `A<T>' is dependent. */
10087 cp_parser_nested_name_specifier_opt (parser,
10088 /*typename_keyword_p=*/true,
10089 /*check_dependency_p=*/true,
a668c6ad
MM
10090 /*type_p=*/true,
10091 is_declaration);
a723baf1
MM
10092 /* For everything but enumeration types, consider a template-id. */
10093 if (tag_type != enum_type)
10094 {
10095 bool template_p = false;
10096 tree decl;
10097
10098 /* Allow the `template' keyword. */
10099 template_p = cp_parser_optional_template_keyword (parser);
10100 /* If we didn't see `template', we don't know if there's a
0cbd7506 10101 template-id or not. */
a723baf1
MM
10102 if (!template_p)
10103 cp_parser_parse_tentatively (parser);
10104 /* Parse the template-id. */
10105 decl = cp_parser_template_id (parser, template_p,
a668c6ad
MM
10106 /*check_dependency_p=*/true,
10107 is_declaration);
a723baf1 10108 /* If we didn't find a template-id, look for an ordinary
0cbd7506 10109 identifier. */
a723baf1
MM
10110 if (!template_p && !cp_parser_parse_definitely (parser))
10111 ;
10112 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10113 in effect, then we must assume that, upon instantiation, the
10114 template will correspond to a class. */
10115 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10116 && tag_type == typename_type)
10117 type = make_typename_type (parser->scope, decl,
fc6a28d7 10118 typename_type,
8da15291 10119 /*complain=*/tf_error);
21526606 10120 else
a723baf1
MM
10121 type = TREE_TYPE (decl);
10122 }
10123
10124 /* For an enumeration type, consider only a plain identifier. */
10125 if (!type)
10126 {
10127 identifier = cp_parser_identifier (parser);
10128
10129 if (identifier == error_mark_node)
eb5abb39
NS
10130 {
10131 parser->scope = NULL_TREE;
10132 return error_mark_node;
10133 }
a723baf1
MM
10134
10135 /* For a `typename', we needn't call xref_tag. */
c8094d83 10136 if (tag_type == typename_type
0c88d886 10137 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
21526606 10138 return cp_parser_make_typename_type (parser, parser->scope,
2097b5f2 10139 identifier);
a723baf1
MM
10140 /* Look up a qualified name in the usual way. */
10141 if (parser->scope)
10142 {
10143 tree decl;
10144
21526606 10145 decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 10146 tag_type,
b0bc6e8e 10147 /*is_template=*/false,
eea9800f 10148 /*is_namespace=*/false,
8f78f01f 10149 /*check_dependency=*/true,
91b1ca65 10150 /*ambiguous_decls=*/NULL);
710b73e6
KL
10151
10152 /* If we are parsing friend declaration, DECL may be a
10153 TEMPLATE_DECL tree node here. However, we need to check
10154 whether this TEMPLATE_DECL results in valid code. Consider
10155 the following example:
10156
10157 namespace N {
10158 template <class T> class C {};
10159 }
10160 class X {
10161 template <class T> friend class N::C; // #1, valid code
10162 };
10163 template <class T> class Y {
10164 friend class N::C; // #2, invalid code
10165 };
10166
10167 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10168 name lookup of `N::C'. We see that friend declaration must
10169 be template for the code to be valid. Note that
10170 processing_template_decl does not work here since it is
10171 always 1 for the above two cases. */
10172
21526606 10173 decl = (cp_parser_maybe_treat_template_as_class
710b73e6
KL
10174 (decl, /*tag_name_p=*/is_friend
10175 && parser->num_template_parameter_lists));
a723baf1
MM
10176
10177 if (TREE_CODE (decl) != TYPE_DECL)
10178 {
c8094d83 10179 cp_parser_diagnose_invalid_type_name (parser,
0c88d886
MM
10180 parser->scope,
10181 identifier);
a723baf1
MM
10182 return error_mark_node;
10183 }
560ad596
MM
10184
10185 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
21526606 10186 check_elaborated_type_specifier
4b0d3cbe 10187 (tag_type, decl,
560ad596
MM
10188 (parser->num_template_parameter_lists
10189 || DECL_SELF_REFERENCE_P (decl)));
a723baf1
MM
10190
10191 type = TREE_TYPE (decl);
10192 }
21526606 10193 else
a723baf1
MM
10194 {
10195 /* An elaborated-type-specifier sometimes introduces a new type and
10196 sometimes names an existing type. Normally, the rule is that it
10197 introduces a new type only if there is not an existing type of
10198 the same name already in scope. For example, given:
10199
10200 struct S {};
10201 void f() { struct S s; }
10202
10203 the `struct S' in the body of `f' is the same `struct S' as in
10204 the global scope; the existing definition is used. However, if
21526606 10205 there were no global declaration, this would introduce a new
a723baf1
MM
10206 local class named `S'.
10207
10208 An exception to this rule applies to the following code:
10209
10210 namespace N { struct S; }
10211
10212 Here, the elaborated-type-specifier names a new type
10213 unconditionally; even if there is already an `S' in the
10214 containing scope this declaration names a new type.
10215 This exception only applies if the elaborated-type-specifier
10216 forms the complete declaration:
10217
21526606 10218 [class.name]
a723baf1
MM
10219
10220 A declaration consisting solely of `class-key identifier ;' is
10221 either a redeclaration of the name in the current scope or a
10222 forward declaration of the identifier as a class name. It
10223 introduces the name into the current scope.
10224
10225 We are in this situation precisely when the next token is a `;'.
10226
10227 An exception to the exception is that a `friend' declaration does
10228 *not* name a new type; i.e., given:
10229
10230 struct S { friend struct T; };
10231
21526606 10232 `T' is not a new type in the scope of `S'.
a723baf1
MM
10233
10234 Also, `new struct S' or `sizeof (struct S)' never results in the
10235 definition of a new type; a new type can only be declared in a
9bcb9aae 10236 declaration context. */
a723baf1 10237
29ef83de 10238 tag_scope ts;
ca85f659
MM
10239 bool template_p;
10240
29ef83de
KL
10241 if (is_friend)
10242 /* Friends have special name lookup rules. */
10243 ts = ts_within_enclosing_non_class;
10244 else if (is_declaration
10245 && cp_lexer_next_token_is (parser->lexer,
10246 CPP_SEMICOLON))
10247 /* This is a `class-key identifier ;' */
10248 ts = ts_current;
10249 else
10250 ts = ts_global;
10251
0cbd7506
MS
10252 /* Warn about attributes. They are ignored. */
10253 if (attributes)
5c498b10
DD
10254 warning (OPT_Wattributes,
10255 "type attributes are honored only at type definition");
e0fed25b 10256
ca85f659
MM
10257 template_p =
10258 (parser->num_template_parameter_lists
10259 && (cp_parser_next_token_starts_class_definition_p (parser)
10260 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
357d956e
MM
10261 /* An unqualified name was used to reference this type, so
10262 there were no qualifying templates. */
10263 if (!cp_parser_check_template_parameters (parser,
10264 /*num_templates=*/0))
10265 return error_mark_node;
ca85f659 10266 type = xref_tag (tag_type, identifier, ts, template_p);
a723baf1
MM
10267 }
10268 }
10269 if (tag_type != enum_type)
10270 cp_parser_check_class_key (tag_type, type);
ee43dab5
MM
10271
10272 /* A "<" cannot follow an elaborated type specifier. If that
10273 happens, the user was probably trying to form a template-id. */
10274 cp_parser_check_for_invalid_template_id (parser, type);
10275
a723baf1
MM
10276 return type;
10277}
10278
10279/* Parse an enum-specifier.
10280
10281 enum-specifier:
10282 enum identifier [opt] { enumerator-list [opt] }
10283
f6af9a15
MA
10284 GNU Extensions:
10285 enum identifier [opt] { enumerator-list [opt] } attributes
10286
a723baf1
MM
10287 Returns an ENUM_TYPE representing the enumeration. */
10288
10289static tree
94edc4ab 10290cp_parser_enum_specifier (cp_parser* parser)
a723baf1 10291{
ff4eb0b5 10292 tree identifier;
a723baf1
MM
10293 tree type;
10294
ff4eb0b5
ZW
10295 /* Caller guarantees that the current token is 'enum', an identifier
10296 possibly follows, and the token after that is an opening brace.
10297 If we don't have an identifier, fabricate an anonymous name for
10298 the enumeration being defined. */
10299 cp_lexer_consume_token (parser->lexer);
a723baf1 10300
ff4eb0b5 10301 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
a723baf1 10302 identifier = cp_parser_identifier (parser);
ff4eb0b5
ZW
10303 else
10304 identifier = make_anon_name ();
a723baf1 10305
a723baf1
MM
10306 /* Issue an error message if type-definitions are forbidden here. */
10307 cp_parser_check_type_definition (parser);
10308
2cfe82fe
ZW
10309 /* Create the new type. We do this before consuming the opening brace
10310 so the enum will be recorded as being on the line of its tag (or the
10311 'enum' keyword, if there is no tag). */
ff4eb0b5 10312 type = start_enum (identifier);
a723baf1 10313
2cfe82fe
ZW
10314 /* Consume the opening brace. */
10315 cp_lexer_consume_token (parser->lexer);
10316
ff4eb0b5
ZW
10317 /* If the next token is not '}', then there are some enumerators. */
10318 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
a723baf1 10319 cp_parser_enumerator_list (parser, type);
ff4eb0b5
ZW
10320
10321 /* Consume the final '}'. */
a723baf1
MM
10322 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10323
f6af9a15 10324 /* Look for trailing attributes to apply to this enumeration, and
03fd3f84 10325 apply them if appropriate. */
f6af9a15
MA
10326 if (cp_parser_allow_gnu_extensions_p (parser))
10327 {
10328 tree trailing_attr = cp_parser_attributes_opt (parser);
10329 cplus_decl_attributes (&type,
10330 trailing_attr,
10331 (int) ATTR_FLAG_TYPE_IN_PLACE);
10332 }
10333
a723baf1
MM
10334 /* Finish up the enumeration. */
10335 finish_enum (type);
10336
10337 return type;
10338}
10339
10340/* Parse an enumerator-list. The enumerators all have the indicated
21526606 10341 TYPE.
a723baf1
MM
10342
10343 enumerator-list:
10344 enumerator-definition
10345 enumerator-list , enumerator-definition */
10346
10347static void
94edc4ab 10348cp_parser_enumerator_list (cp_parser* parser, tree type)
a723baf1
MM
10349{
10350 while (true)
10351 {
a723baf1
MM
10352 /* Parse an enumerator-definition. */
10353 cp_parser_enumerator_definition (parser, type);
ff4eb0b5
ZW
10354
10355 /* If the next token is not a ',', we've reached the end of
10356 the list. */
10357 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
a723baf1
MM
10358 break;
10359 /* Otherwise, consume the `,' and keep going. */
10360 cp_lexer_consume_token (parser->lexer);
10361 /* If the next token is a `}', there is a trailing comma. */
10362 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10363 {
10364 if (pedantic && !in_system_header)
10365 pedwarn ("comma at end of enumerator list");
10366 break;
10367 }
10368 }
10369}
10370
10371/* Parse an enumerator-definition. The enumerator has the indicated
10372 TYPE.
10373
10374 enumerator-definition:
10375 enumerator
10376 enumerator = constant-expression
21526606 10377
a723baf1
MM
10378 enumerator:
10379 identifier */
10380
10381static void
94edc4ab 10382cp_parser_enumerator_definition (cp_parser* parser, tree type)
a723baf1 10383{
a723baf1
MM
10384 tree identifier;
10385 tree value;
10386
10387 /* Look for the identifier. */
10388 identifier = cp_parser_identifier (parser);
10389 if (identifier == error_mark_node)
10390 return;
21526606 10391
ff4eb0b5
ZW
10392 /* If the next token is an '=', then there is an explicit value. */
10393 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
a723baf1
MM
10394 {
10395 /* Consume the `=' token. */
10396 cp_lexer_consume_token (parser->lexer);
10397 /* Parse the value. */
21526606 10398 value = cp_parser_constant_expression (parser,
d17811fd 10399 /*allow_non_constant_p=*/false,
14d22dd6 10400 NULL);
a723baf1
MM
10401 }
10402 else
10403 value = NULL_TREE;
10404
10405 /* Create the enumerator. */
10406 build_enumerator (identifier, value, type);
10407}
10408
10409/* Parse a namespace-name.
10410
10411 namespace-name:
10412 original-namespace-name
10413 namespace-alias
10414
10415 Returns the NAMESPACE_DECL for the namespace. */
10416
10417static tree
94edc4ab 10418cp_parser_namespace_name (cp_parser* parser)
a723baf1
MM
10419{
10420 tree identifier;
10421 tree namespace_decl;
10422
10423 /* Get the name of the namespace. */
10424 identifier = cp_parser_identifier (parser);
10425 if (identifier == error_mark_node)
10426 return error_mark_node;
10427
eea9800f
MM
10428 /* Look up the identifier in the currently active scope. Look only
10429 for namespaces, due to:
10430
10431 [basic.lookup.udir]
10432
10433 When looking up a namespace-name in a using-directive or alias
21526606 10434 definition, only namespace names are considered.
eea9800f
MM
10435
10436 And:
10437
10438 [basic.lookup.qual]
10439
10440 During the lookup of a name preceding the :: scope resolution
21526606 10441 operator, object, function, and enumerator names are ignored.
eea9800f
MM
10442
10443 (Note that cp_parser_class_or_namespace_name only calls this
10444 function if the token after the name is the scope resolution
10445 operator.) */
10446 namespace_decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 10447 none_type,
b0bc6e8e 10448 /*is_template=*/false,
eea9800f 10449 /*is_namespace=*/true,
8f78f01f 10450 /*check_dependency=*/true,
91b1ca65 10451 /*ambiguous_decls=*/NULL);
a723baf1
MM
10452 /* If it's not a namespace, issue an error. */
10453 if (namespace_decl == error_mark_node
10454 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10455 {
166206ce
VR
10456 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10457 error ("%qD is not a namespace-name", identifier);
a723baf1
MM
10458 cp_parser_error (parser, "expected namespace-name");
10459 namespace_decl = error_mark_node;
10460 }
21526606 10461
a723baf1
MM
10462 return namespace_decl;
10463}
10464
10465/* Parse a namespace-definition.
10466
10467 namespace-definition:
10468 named-namespace-definition
21526606 10469 unnamed-namespace-definition
a723baf1
MM
10470
10471 named-namespace-definition:
10472 original-namespace-definition
10473 extension-namespace-definition
10474
10475 original-namespace-definition:
10476 namespace identifier { namespace-body }
21526606 10477
a723baf1
MM
10478 extension-namespace-definition:
10479 namespace original-namespace-name { namespace-body }
21526606 10480
a723baf1
MM
10481 unnamed-namespace-definition:
10482 namespace { namespace-body } */
10483
10484static void
94edc4ab 10485cp_parser_namespace_definition (cp_parser* parser)
a723baf1 10486{
0ed5edac 10487 tree identifier, attribs;
a723baf1
MM
10488
10489 /* Look for the `namespace' keyword. */
10490 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10491
10492 /* Get the name of the namespace. We do not attempt to distinguish
10493 between an original-namespace-definition and an
10494 extension-namespace-definition at this point. The semantic
10495 analysis routines are responsible for that. */
10496 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10497 identifier = cp_parser_identifier (parser);
10498 else
10499 identifier = NULL_TREE;
10500
0ed5edac
JM
10501 /* Parse any specified attributes. */
10502 attribs = cp_parser_attributes_opt (parser);
10503
a723baf1
MM
10504 /* Look for the `{' to start the namespace. */
10505 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10506 /* Start the namespace. */
0ed5edac 10507 push_namespace_with_attribs (identifier, attribs);
a723baf1
MM
10508 /* Parse the body of the namespace. */
10509 cp_parser_namespace_body (parser);
10510 /* Finish the namespace. */
10511 pop_namespace ();
10512 /* Look for the final `}'. */
10513 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10514}
10515
10516/* Parse a namespace-body.
10517
10518 namespace-body:
10519 declaration-seq [opt] */
10520
10521static void
94edc4ab 10522cp_parser_namespace_body (cp_parser* parser)
a723baf1
MM
10523{
10524 cp_parser_declaration_seq_opt (parser);
10525}
10526
10527/* Parse a namespace-alias-definition.
10528
10529 namespace-alias-definition:
10530 namespace identifier = qualified-namespace-specifier ; */
10531
10532static void
94edc4ab 10533cp_parser_namespace_alias_definition (cp_parser* parser)
a723baf1
MM
10534{
10535 tree identifier;
10536 tree namespace_specifier;
10537
10538 /* Look for the `namespace' keyword. */
10539 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10540 /* Look for the identifier. */
10541 identifier = cp_parser_identifier (parser);
10542 if (identifier == error_mark_node)
10543 return;
10544 /* Look for the `=' token. */
10545 cp_parser_require (parser, CPP_EQ, "`='");
10546 /* Look for the qualified-namespace-specifier. */
21526606 10547 namespace_specifier
a723baf1
MM
10548 = cp_parser_qualified_namespace_specifier (parser);
10549 /* Look for the `;' token. */
10550 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10551
10552 /* Register the alias in the symbol table. */
10553 do_namespace_alias (identifier, namespace_specifier);
10554}
10555
10556/* Parse a qualified-namespace-specifier.
10557
10558 qualified-namespace-specifier:
10559 :: [opt] nested-name-specifier [opt] namespace-name
10560
10561 Returns a NAMESPACE_DECL corresponding to the specified
10562 namespace. */
10563
10564static tree
94edc4ab 10565cp_parser_qualified_namespace_specifier (cp_parser* parser)
a723baf1
MM
10566{
10567 /* Look for the optional `::'. */
21526606 10568 cp_parser_global_scope_opt (parser,
a723baf1
MM
10569 /*current_scope_valid_p=*/false);
10570
10571 /* Look for the optional nested-name-specifier. */
10572 cp_parser_nested_name_specifier_opt (parser,
10573 /*typename_keyword_p=*/false,
10574 /*check_dependency_p=*/true,
a668c6ad
MM
10575 /*type_p=*/false,
10576 /*is_declaration=*/true);
a723baf1
MM
10577
10578 return cp_parser_namespace_name (parser);
10579}
10580
10581/* Parse a using-declaration.
10582
10583 using-declaration:
10584 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10585 using :: unqualified-id ; */
10586
10587static void
94edc4ab 10588cp_parser_using_declaration (cp_parser* parser)
a723baf1
MM
10589{
10590 cp_token *token;
10591 bool typename_p = false;
10592 bool global_scope_p;
10593 tree decl;
10594 tree identifier;
ed5f054f 10595 tree qscope;
a723baf1
MM
10596
10597 /* Look for the `using' keyword. */
10598 cp_parser_require_keyword (parser, RID_USING, "`using'");
21526606 10599
a723baf1
MM
10600 /* Peek at the next token. */
10601 token = cp_lexer_peek_token (parser->lexer);
10602 /* See if it's `typename'. */
10603 if (token->keyword == RID_TYPENAME)
10604 {
10605 /* Remember that we've seen it. */
10606 typename_p = true;
10607 /* Consume the `typename' token. */
10608 cp_lexer_consume_token (parser->lexer);
10609 }
10610
10611 /* Look for the optional global scope qualification. */
21526606 10612 global_scope_p
a723baf1 10613 = (cp_parser_global_scope_opt (parser,
21526606 10614 /*current_scope_valid_p=*/false)
a723baf1
MM
10615 != NULL_TREE);
10616
10617 /* If we saw `typename', or didn't see `::', then there must be a
10618 nested-name-specifier present. */
10619 if (typename_p || !global_scope_p)
21526606 10620 qscope = cp_parser_nested_name_specifier (parser, typename_p,
ed5f054f
AO
10621 /*check_dependency_p=*/true,
10622 /*type_p=*/false,
10623 /*is_declaration=*/true);
a723baf1
MM
10624 /* Otherwise, we could be in either of the two productions. In that
10625 case, treat the nested-name-specifier as optional. */
10626 else
ed5f054f
AO
10627 qscope = cp_parser_nested_name_specifier_opt (parser,
10628 /*typename_keyword_p=*/false,
10629 /*check_dependency_p=*/true,
10630 /*type_p=*/false,
10631 /*is_declaration=*/true);
10632 if (!qscope)
10633 qscope = global_namespace;
a723baf1
MM
10634
10635 /* Parse the unqualified-id. */
21526606 10636 identifier = cp_parser_unqualified_id (parser,
a723baf1 10637 /*template_keyword_p=*/false,
f3c2dfc6 10638 /*check_dependency_p=*/true,
fa6098f8
MM
10639 /*declarator_p=*/true,
10640 /*optional_p=*/false);
a723baf1
MM
10641
10642 /* The function we call to handle a using-declaration is different
10643 depending on what scope we are in. */
56bbd9d6 10644 if (qscope == error_mark_node || identifier == error_mark_node)
f3c2dfc6
MM
10645 ;
10646 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10647 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10648 /* [namespace.udecl]
10649
10650 A using declaration shall not name a template-id. */
10651 error ("a template-id may not appear in a using-declaration");
a723baf1
MM
10652 else
10653 {
a5201a91 10654 if (at_class_scope_p ())
4eb6d609 10655 {
f3c2dfc6 10656 /* Create the USING_DECL. */
1d786913 10657 decl = do_class_using_decl (parser->scope, identifier);
f3c2dfc6
MM
10658 /* Add it to the list of members in this class. */
10659 finish_member_declaration (decl);
4eb6d609 10660 }
a723baf1 10661 else
f3c2dfc6
MM
10662 {
10663 decl = cp_parser_lookup_name_simple (parser, identifier);
10664 if (decl == error_mark_node)
4bb8ca28 10665 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
a5201a91 10666 else if (!at_namespace_scope_p ())
ed5f054f 10667 do_local_using_decl (decl, qscope, identifier);
f3c2dfc6 10668 else
ed5f054f 10669 do_toplevel_using_decl (decl, qscope, identifier);
f3c2dfc6 10670 }
a723baf1
MM
10671 }
10672
10673 /* Look for the final `;'. */
10674 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10675}
10676
21526606
EC
10677/* Parse a using-directive.
10678
a723baf1
MM
10679 using-directive:
10680 using namespace :: [opt] nested-name-specifier [opt]
10681 namespace-name ; */
10682
10683static void
94edc4ab 10684cp_parser_using_directive (cp_parser* parser)
a723baf1
MM
10685{
10686 tree namespace_decl;
86098eb8 10687 tree attribs;
a723baf1
MM
10688
10689 /* Look for the `using' keyword. */
10690 cp_parser_require_keyword (parser, RID_USING, "`using'");
10691 /* And the `namespace' keyword. */
10692 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10693 /* Look for the optional `::' operator. */
10694 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
34cd5ae7 10695 /* And the optional nested-name-specifier. */
a723baf1
MM
10696 cp_parser_nested_name_specifier_opt (parser,
10697 /*typename_keyword_p=*/false,
10698 /*check_dependency_p=*/true,
a668c6ad
MM
10699 /*type_p=*/false,
10700 /*is_declaration=*/true);
a723baf1
MM
10701 /* Get the namespace being used. */
10702 namespace_decl = cp_parser_namespace_name (parser);
86098eb8
JM
10703 /* And any specified attributes. */
10704 attribs = cp_parser_attributes_opt (parser);
a723baf1 10705 /* Update the symbol table. */
86098eb8 10706 parse_using_directive (namespace_decl, attribs);
a723baf1
MM
10707 /* Look for the final `;'. */
10708 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10709}
10710
10711/* Parse an asm-definition.
10712
10713 asm-definition:
21526606 10714 asm ( string-literal ) ;
a723baf1
MM
10715
10716 GNU Extension:
10717
10718 asm-definition:
10719 asm volatile [opt] ( string-literal ) ;
10720 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10721 asm volatile [opt] ( string-literal : asm-operand-list [opt]
0cbd7506 10722 : asm-operand-list [opt] ) ;
21526606 10723 asm volatile [opt] ( string-literal : asm-operand-list [opt]
0cbd7506
MS
10724 : asm-operand-list [opt]
10725 : asm-operand-list [opt] ) ; */
a723baf1
MM
10726
10727static void
94edc4ab 10728cp_parser_asm_definition (cp_parser* parser)
a723baf1 10729{
a723baf1
MM
10730 tree string;
10731 tree outputs = NULL_TREE;
10732 tree inputs = NULL_TREE;
10733 tree clobbers = NULL_TREE;
10734 tree asm_stmt;
10735 bool volatile_p = false;
10736 bool extended_p = false;
10737
10738 /* Look for the `asm' keyword. */
10739 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10740 /* See if the next token is `volatile'. */
10741 if (cp_parser_allow_gnu_extensions_p (parser)
10742 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10743 {
10744 /* Remember that we saw the `volatile' keyword. */
10745 volatile_p = true;
10746 /* Consume the token. */
10747 cp_lexer_consume_token (parser->lexer);
10748 }
10749 /* Look for the opening `('. */
c162c75e
MA
10750 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10751 return;
a723baf1 10752 /* Look for the string. */
c162c75e
MA
10753 string = cp_parser_string_literal (parser, false, false);
10754 if (string == error_mark_node)
10755 {
10756 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10757 /*consume_paren=*/true);
10758 return;
10759 }
10760
a723baf1 10761 /* If we're allowing GNU extensions, check for the extended assembly
21526606 10762 syntax. Unfortunately, the `:' tokens need not be separated by
a723baf1
MM
10763 a space in C, and so, for compatibility, we tolerate that here
10764 too. Doing that means that we have to treat the `::' operator as
10765 two `:' tokens. */
10766 if (cp_parser_allow_gnu_extensions_p (parser)
10767 && at_function_scope_p ()
10768 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10769 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10770 {
10771 bool inputs_p = false;
10772 bool clobbers_p = false;
10773
10774 /* The extended syntax was used. */
10775 extended_p = true;
10776
10777 /* Look for outputs. */
10778 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10779 {
10780 /* Consume the `:'. */
10781 cp_lexer_consume_token (parser->lexer);
10782 /* Parse the output-operands. */
21526606 10783 if (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
10784 CPP_COLON)
10785 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
10786 CPP_SCOPE)
10787 && cp_lexer_next_token_is_not (parser->lexer,
10788 CPP_CLOSE_PAREN))
a723baf1
MM
10789 outputs = cp_parser_asm_operand_list (parser);
10790 }
10791 /* If the next token is `::', there are no outputs, and the
10792 next token is the beginning of the inputs. */
10793 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
bf5930d4
JB
10794 /* The inputs are coming next. */
10795 inputs_p = true;
a723baf1
MM
10796
10797 /* Look for inputs. */
10798 if (inputs_p
10799 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10800 {
bf5930d4
JB
10801 /* Consume the `:' or `::'. */
10802 cp_lexer_consume_token (parser->lexer);
a723baf1 10803 /* Parse the output-operands. */
21526606 10804 if (cp_lexer_next_token_is_not (parser->lexer,
a723baf1 10805 CPP_COLON)
8caf4c38
MM
10806 && cp_lexer_next_token_is_not (parser->lexer,
10807 CPP_CLOSE_PAREN))
a723baf1
MM
10808 inputs = cp_parser_asm_operand_list (parser);
10809 }
10810 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10811 /* The clobbers are coming next. */
10812 clobbers_p = true;
10813
10814 /* Look for clobbers. */
21526606 10815 if (clobbers_p
a723baf1
MM
10816 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10817 {
bf5930d4
JB
10818 /* Consume the `:' or `::'. */
10819 cp_lexer_consume_token (parser->lexer);
a723baf1 10820 /* Parse the clobbers. */
8caf4c38
MM
10821 if (cp_lexer_next_token_is_not (parser->lexer,
10822 CPP_CLOSE_PAREN))
10823 clobbers = cp_parser_asm_clobber_list (parser);
a723baf1
MM
10824 }
10825 }
10826 /* Look for the closing `)'. */
10827 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
10828 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10829 /*consume_paren=*/true);
a723baf1
MM
10830 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10831
e130a54b 10832 /* Create the ASM_EXPR. */
a723baf1
MM
10833 if (at_function_scope_p ())
10834 {
6de9cd9a
DN
10835 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10836 inputs, clobbers);
e130a54b 10837 /* If the extended syntax was not used, mark the ASM_EXPR. */
a723baf1 10838 if (!extended_p)
ca059043
AP
10839 {
10840 tree temp = asm_stmt;
10841 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10842 temp = TREE_OPERAND (temp, 0);
c8094d83 10843
ca059043
AP
10844 ASM_INPUT_P (temp) = 1;
10845 }
a723baf1
MM
10846 }
10847 else
474eccc6 10848 cgraph_add_asm_node (string);
a723baf1
MM
10849}
10850
10851/* Declarators [gram.dcl.decl] */
10852
10853/* Parse an init-declarator.
10854
10855 init-declarator:
10856 declarator initializer [opt]
10857
10858 GNU Extension:
10859
10860 init-declarator:
10861 declarator asm-specification [opt] attributes [opt] initializer [opt]
10862
4bb8ca28
MM
10863 function-definition:
10864 decl-specifier-seq [opt] declarator ctor-initializer [opt]
21526606
EC
10865 function-body
10866 decl-specifier-seq [opt] declarator function-try-block
4bb8ca28
MM
10867
10868 GNU Extension:
10869
10870 function-definition:
21526606 10871 __extension__ function-definition
4bb8ca28 10872
a723baf1 10873 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
c8e4f0e9 10874 Returns a representation of the entity declared. If MEMBER_P is TRUE,
cf22909c
KL
10875 then this declarator appears in a class scope. The new DECL created
10876 by this declarator is returned.
a723baf1
MM
10877
10878 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10879 for a function-definition here as well. If the declarator is a
10880 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10881 be TRUE upon return. By that point, the function-definition will
10882 have been completely parsed.
10883
10884 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10885 is FALSE. */
10886
10887static tree
21526606 10888cp_parser_init_declarator (cp_parser* parser,
62d1db17 10889 cp_decl_specifier_seq *decl_specifiers,
94edc4ab
NN
10890 bool function_definition_allowed_p,
10891 bool member_p,
560ad596 10892 int declares_class_or_enum,
94edc4ab 10893 bool* function_definition_p)
a723baf1
MM
10894{
10895 cp_token *token;
058b15c1 10896 cp_declarator *declarator;
62d1db17 10897 tree prefix_attributes;
a723baf1
MM
10898 tree attributes;
10899 tree asm_specification;
10900 tree initializer;
10901 tree decl = NULL_TREE;
10902 tree scope;
a723baf1 10903 bool is_initialized;
63c9a190
MM
10904 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
10905 initialized with "= ..", CPP_OPEN_PAREN if initialized with
10906 "(...)". */
10907 enum cpp_ttype initialization_kind;
21137095 10908 bool is_parenthesized_init = false;
39703eb9 10909 bool is_non_constant_init;
7efa3e22 10910 int ctor_dtor_or_conv_p;
a723baf1 10911 bool friend_p;
4514aa8c 10912 tree pushed_scope = NULL;
a723baf1 10913
62d1db17
MM
10914 /* Gather the attributes that were provided with the
10915 decl-specifiers. */
10916 prefix_attributes = decl_specifiers->attributes;
62d1db17 10917
a723baf1
MM
10918 /* Assume that this is not the declarator for a function
10919 definition. */
10920 if (function_definition_p)
10921 *function_definition_p = false;
10922
10923 /* Defer access checks while parsing the declarator; we cannot know
21526606 10924 what names are accessible until we know what is being
a723baf1 10925 declared. */
cf22909c
KL
10926 resume_deferring_access_checks ();
10927
a723baf1 10928 /* Parse the declarator. */
21526606 10929 declarator
62b8a44e 10930 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 10931 &ctor_dtor_or_conv_p,
db86dd14
MM
10932 /*parenthesized_p=*/NULL,
10933 /*member_p=*/false);
a723baf1 10934 /* Gather up the deferred checks. */
cf22909c 10935 stop_deferring_access_checks ();
24c0ef37 10936
a723baf1
MM
10937 /* If the DECLARATOR was erroneous, there's no need to go
10938 further. */
058b15c1 10939 if (declarator == cp_error_declarator)
cf22909c 10940 return error_mark_node;
a723baf1 10941
fc6a28d7
MM
10942 if (declares_class_or_enum & 2)
10943 cp_parser_check_for_definition_in_return_type (declarator,
10944 decl_specifiers->type);
560ad596 10945
a723baf1
MM
10946 /* Figure out what scope the entity declared by the DECLARATOR is
10947 located in. `grokdeclarator' sometimes changes the scope, so
10948 we compute it now. */
10949 scope = get_scope_of_declarator (declarator);
10950
10951 /* If we're allowing GNU extensions, look for an asm-specification
10952 and attributes. */
10953 if (cp_parser_allow_gnu_extensions_p (parser))
10954 {
10955 /* Look for an asm-specification. */
10956 asm_specification = cp_parser_asm_specification_opt (parser);
10957 /* And attributes. */
10958 attributes = cp_parser_attributes_opt (parser);
10959 }
10960 else
10961 {
10962 asm_specification = NULL_TREE;
10963 attributes = NULL_TREE;
10964 }
10965
10966 /* Peek at the next token. */
10967 token = cp_lexer_peek_token (parser->lexer);
10968 /* Check to see if the token indicates the start of a
10969 function-definition. */
10970 if (cp_parser_token_starts_function_definition_p (token))
10971 {
10972 if (!function_definition_allowed_p)
10973 {
10974 /* If a function-definition should not appear here, issue an
10975 error message. */
10976 cp_parser_error (parser,
10977 "a function-definition is not allowed here");
10978 return error_mark_node;
10979 }
10980 else
10981 {
a723baf1
MM
10982 /* Neither attributes nor an asm-specification are allowed
10983 on a function-definition. */
10984 if (asm_specification)
10985 error ("an asm-specification is not allowed on a function-definition");
10986 if (attributes)
10987 error ("attributes are not allowed on a function-definition");
10988 /* This is a function-definition. */
10989 *function_definition_p = true;
10990
a723baf1 10991 /* Parse the function definition. */
4bb8ca28
MM
10992 if (member_p)
10993 decl = cp_parser_save_member_function_body (parser,
10994 decl_specifiers,
10995 declarator,
10996 prefix_attributes);
10997 else
21526606 10998 decl
4bb8ca28
MM
10999 = (cp_parser_function_definition_from_specifiers_and_declarator
11000 (parser, decl_specifiers, prefix_attributes, declarator));
24c0ef37 11001
a723baf1
MM
11002 return decl;
11003 }
11004 }
11005
11006 /* [dcl.dcl]
11007
11008 Only in function declarations for constructors, destructors, and
21526606 11009 type conversions can the decl-specifier-seq be omitted.
a723baf1
MM
11010
11011 We explicitly postpone this check past the point where we handle
11012 function-definitions because we tolerate function-definitions
11013 that are missing their return types in some modes. */
62d1db17 11014 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
a723baf1 11015 {
21526606 11016 cp_parser_error (parser,
a723baf1
MM
11017 "expected constructor, destructor, or type conversion");
11018 return error_mark_node;
11019 }
11020
11021 /* An `=' or an `(' indicates an initializer. */
63c9a190
MM
11022 if (token->type == CPP_EQ
11023 || token->type == CPP_OPEN_PAREN)
a723baf1 11024 {
63c9a190
MM
11025 is_initialized = true;
11026 initialization_kind = token->type;
11027 }
11028 else
11029 {
11030 /* If the init-declarator isn't initialized and isn't followed by a
11031 `,' or `;', it's not a valid init-declarator. */
11032 if (token->type != CPP_COMMA
11033 && token->type != CPP_SEMICOLON)
11034 {
11035 cp_parser_error (parser, "expected initializer");
11036 return error_mark_node;
11037 }
11038 is_initialized = false;
11039 initialization_kind = CPP_EOF;
a723baf1
MM
11040 }
11041
11042 /* Because start_decl has side-effects, we should only call it if we
11043 know we're going ahead. By this point, we know that we cannot
11044 possibly be looking at any other construct. */
11045 cp_parser_commit_to_tentative_parse (parser);
11046
e90c7b84
ILT
11047 /* If the decl specifiers were bad, issue an error now that we're
11048 sure this was intended to be a declarator. Then continue
11049 declaring the variable(s), as int, to try to cut down on further
11050 errors. */
62d1db17
MM
11051 if (decl_specifiers->any_specifiers_p
11052 && decl_specifiers->type == error_mark_node)
e90c7b84
ILT
11053 {
11054 cp_parser_error (parser, "invalid type in declaration");
62d1db17 11055 decl_specifiers->type = integer_type_node;
e90c7b84
ILT
11056 }
11057
a723baf1
MM
11058 /* Check to see whether or not this declaration is a friend. */
11059 friend_p = cp_parser_friend_p (decl_specifiers);
11060
11061 /* Check that the number of template-parameter-lists is OK. */
ee3071ef 11062 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
cf22909c 11063 return error_mark_node;
a723baf1
MM
11064
11065 /* Enter the newly declared entry in the symbol table. If we're
11066 processing a declaration in a class-specifier, we wait until
11067 after processing the initializer. */
11068 if (!member_p)
11069 {
11070 if (parser->in_unbraced_linkage_specification_p)
11071 {
62d1db17 11072 decl_specifiers->storage_class = sc_extern;
a723baf1
MM
11073 have_extern_spec = false;
11074 }
ee3071ef 11075 decl = start_decl (declarator, decl_specifiers,
73a8adb6 11076 is_initialized, attributes, prefix_attributes,
4514aa8c 11077 &pushed_scope);
a723baf1 11078 }
73a8adb6
MM
11079 else if (scope)
11080 /* Enter the SCOPE. That way unqualified names appearing in the
11081 initializer will be looked up in SCOPE. */
4514aa8c 11082 pushed_scope = push_scope (scope);
a723baf1
MM
11083
11084 /* Perform deferred access control checks, now that we know in which
11085 SCOPE the declared entity resides. */
21526606 11086 if (!member_p && decl)
a723baf1
MM
11087 {
11088 tree saved_current_function_decl = NULL_TREE;
11089
11090 /* If the entity being declared is a function, pretend that we
11091 are in its scope. If it is a `friend', it may have access to
9bcb9aae 11092 things that would not otherwise be accessible. */
a723baf1
MM
11093 if (TREE_CODE (decl) == FUNCTION_DECL)
11094 {
11095 saved_current_function_decl = current_function_decl;
11096 current_function_decl = decl;
11097 }
21526606 11098
cf22909c
KL
11099 /* Perform the access control checks for the declarator and the
11100 the decl-specifiers. */
11101 perform_deferred_access_checks ();
a723baf1
MM
11102
11103 /* Restore the saved value. */
11104 if (TREE_CODE (decl) == FUNCTION_DECL)
11105 current_function_decl = saved_current_function_decl;
11106 }
11107
11108 /* Parse the initializer. */
aa9d194e
MM
11109 initializer = NULL_TREE;
11110 is_parenthesized_init = false;
11111 is_non_constant_init = true;
a723baf1 11112 if (is_initialized)
63c9a190
MM
11113 {
11114 if (declarator->kind == cdk_function
11115 && declarator->declarator->kind == cdk_id
11116 && initialization_kind == CPP_EQ)
11117 initializer = cp_parser_pure_specifier (parser);
11118 else
11119 initializer = cp_parser_initializer (parser,
11120 &is_parenthesized_init,
11121 &is_non_constant_init);
11122 }
a723baf1
MM
11123
11124 /* The old parser allows attributes to appear after a parenthesized
11125 initializer. Mark Mitchell proposed removing this functionality
11126 on the GCC mailing lists on 2002-08-13. This parser accepts the
11127 attributes -- but ignores them. */
11128 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11129 if (cp_parser_attributes_opt (parser))
5c498b10
DD
11130 warning (OPT_Wattributes,
11131 "attributes after parenthesized initializer ignored");
a723baf1 11132
a723baf1
MM
11133 /* For an in-class declaration, use `grokfield' to create the
11134 declaration. */
11135 if (member_p)
8db1028e 11136 {
4514aa8c 11137 if (pushed_scope)
62a4d942 11138 {
4514aa8c
NS
11139 pop_scope (pushed_scope);
11140 pushed_scope = false;
62a4d942 11141 }
8db1028e 11142 decl = grokfield (declarator, decl_specifiers,
d174af6c
MM
11143 initializer, !is_non_constant_init,
11144 /*asmspec=*/NULL_TREE,
d08fd9d6 11145 prefix_attributes);
8db1028e
NS
11146 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11147 cp_parser_save_default_args (parser, decl);
11148 }
21526606 11149
a723baf1
MM
11150 /* Finish processing the declaration. But, skip friend
11151 declarations. */
550205c3 11152 if (!friend_p && decl && decl != error_mark_node)
73a8adb6
MM
11153 {
11154 cp_finish_decl (decl,
d174af6c 11155 initializer, !is_non_constant_init,
73a8adb6
MM
11156 asm_specification,
11157 /* If the initializer is in parentheses, then this is
11158 a direct-initialization, which means that an
11159 `explicit' constructor is OK. Otherwise, an
11160 `explicit' constructor cannot be used. */
11161 ((is_parenthesized_init || !is_initialized)
a723baf1 11162 ? 0 : LOOKUP_ONLYCONVERTING));
73a8adb6 11163 }
4514aa8c
NS
11164 if (!friend_p && pushed_scope)
11165 pop_scope (pushed_scope);
a723baf1
MM
11166
11167 return decl;
11168}
11169
11170/* Parse a declarator.
21526606 11171
a723baf1
MM
11172 declarator:
11173 direct-declarator
21526606 11174 ptr-operator declarator
a723baf1
MM
11175
11176 abstract-declarator:
11177 ptr-operator abstract-declarator [opt]
11178 direct-abstract-declarator
11179
11180 GNU Extensions:
11181
11182 declarator:
11183 attributes [opt] direct-declarator
21526606 11184 attributes [opt] ptr-operator declarator
a723baf1
MM
11185
11186 abstract-declarator:
11187 attributes [opt] ptr-operator abstract-declarator [opt]
11188 attributes [opt] direct-abstract-declarator
21526606 11189
7efa3e22
NS
11190 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11191 detect constructor, destructor or conversion operators. It is set
11192 to -1 if the declarator is a name, and +1 if it is a
11193 function. Otherwise it is set to zero. Usually you just want to
11194 test for >0, but internally the negative value is used.
21526606 11195
a723baf1
MM
11196 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11197 a decl-specifier-seq unless it declares a constructor, destructor,
11198 or conversion. It might seem that we could check this condition in
11199 semantic analysis, rather than parsing, but that makes it difficult
11200 to handle something like `f()'. We want to notice that there are
11201 no decl-specifiers, and therefore realize that this is an
21526606
EC
11202 expression, not a declaration.)
11203
4bb8ca28 11204 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
c8094d83 11205 the declarator is a direct-declarator of the form "(...)".
db86dd14
MM
11206
11207 MEMBER_P is true iff this declarator is a member-declarator. */
a723baf1 11208
058b15c1 11209static cp_declarator *
21526606 11210cp_parser_declarator (cp_parser* parser,
0cbd7506
MS
11211 cp_parser_declarator_kind dcl_kind,
11212 int* ctor_dtor_or_conv_p,
db86dd14
MM
11213 bool* parenthesized_p,
11214 bool member_p)
a723baf1
MM
11215{
11216 cp_token *token;
058b15c1 11217 cp_declarator *declarator;
a723baf1 11218 enum tree_code code;
3c01e5df 11219 cp_cv_quals cv_quals;
a723baf1
MM
11220 tree class_type;
11221 tree attributes = NULL_TREE;
11222
11223 /* Assume this is not a constructor, destructor, or type-conversion
11224 operator. */
11225 if (ctor_dtor_or_conv_p)
7efa3e22 11226 *ctor_dtor_or_conv_p = 0;
a723baf1
MM
11227
11228 if (cp_parser_allow_gnu_extensions_p (parser))
11229 attributes = cp_parser_attributes_opt (parser);
21526606 11230
a723baf1
MM
11231 /* Peek at the next token. */
11232 token = cp_lexer_peek_token (parser->lexer);
21526606 11233
a723baf1
MM
11234 /* Check for the ptr-operator production. */
11235 cp_parser_parse_tentatively (parser);
11236 /* Parse the ptr-operator. */
21526606
EC
11237 code = cp_parser_ptr_operator (parser,
11238 &class_type,
3c01e5df 11239 &cv_quals);
a723baf1
MM
11240 /* If that worked, then we have a ptr-operator. */
11241 if (cp_parser_parse_definitely (parser))
11242 {
4bb8ca28
MM
11243 /* If a ptr-operator was found, then this declarator was not
11244 parenthesized. */
11245 if (parenthesized_p)
11246 *parenthesized_p = true;
a723baf1
MM
11247 /* The dependent declarator is optional if we are parsing an
11248 abstract-declarator. */
62b8a44e 11249 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1
MM
11250 cp_parser_parse_tentatively (parser);
11251
11252 /* Parse the dependent declarator. */
62b8a44e 11253 declarator = cp_parser_declarator (parser, dcl_kind,
4bb8ca28 11254 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
11255 /*parenthesized_p=*/NULL,
11256 /*member_p=*/false);
a723baf1
MM
11257
11258 /* If we are parsing an abstract-declarator, we must handle the
11259 case where the dependent declarator is absent. */
62b8a44e
NS
11260 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11261 && !cp_parser_parse_definitely (parser))
058b15c1 11262 declarator = NULL;
21526606 11263
a723baf1 11264 /* Build the representation of the ptr-operator. */
058b15c1 11265 if (class_type)
3c01e5df 11266 declarator = make_ptrmem_declarator (cv_quals,
058b15c1
MM
11267 class_type,
11268 declarator);
11269 else if (code == INDIRECT_REF)
3c01e5df 11270 declarator = make_pointer_declarator (cv_quals, declarator);
a723baf1 11271 else
3c01e5df 11272 declarator = make_reference_declarator (cv_quals, declarator);
a723baf1
MM
11273 }
11274 /* Everything else is a direct-declarator. */
11275 else
4bb8ca28
MM
11276 {
11277 if (parenthesized_p)
11278 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11279 CPP_OPEN_PAREN);
11280 declarator = cp_parser_direct_declarator (parser, dcl_kind,
db86dd14
MM
11281 ctor_dtor_or_conv_p,
11282 member_p);
4bb8ca28 11283 }
a723baf1 11284
058b15c1
MM
11285 if (attributes && declarator != cp_error_declarator)
11286 declarator->attributes = attributes;
21526606 11287
a723baf1
MM
11288 return declarator;
11289}
11290
11291/* Parse a direct-declarator or direct-abstract-declarator.
11292
11293 direct-declarator:
11294 declarator-id
11295 direct-declarator ( parameter-declaration-clause )
21526606 11296 cv-qualifier-seq [opt]
a723baf1
MM
11297 exception-specification [opt]
11298 direct-declarator [ constant-expression [opt] ]
21526606 11299 ( declarator )
a723baf1
MM
11300
11301 direct-abstract-declarator:
11302 direct-abstract-declarator [opt]
21526606 11303 ( parameter-declaration-clause )
a723baf1
MM
11304 cv-qualifier-seq [opt]
11305 exception-specification [opt]
11306 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11307 ( abstract-declarator )
11308
62b8a44e
NS
11309 Returns a representation of the declarator. DCL_KIND is
11310 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11311 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11312 we are parsing a direct-declarator. It is
11313 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11314 of ambiguity we prefer an abstract declarator, as per
db86dd14 11315 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
058b15c1 11316 cp_parser_declarator. */
a723baf1 11317
058b15c1 11318static cp_declarator *
94edc4ab 11319cp_parser_direct_declarator (cp_parser* parser,
0cbd7506
MS
11320 cp_parser_declarator_kind dcl_kind,
11321 int* ctor_dtor_or_conv_p,
db86dd14 11322 bool member_p)
a723baf1
MM
11323{
11324 cp_token *token;
058b15c1 11325 cp_declarator *declarator = NULL;
a723baf1
MM
11326 tree scope = NULL_TREE;
11327 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11328 bool saved_in_declarator_p = parser->in_declarator_p;
62b8a44e 11329 bool first = true;
4514aa8c 11330 tree pushed_scope = NULL_TREE;
21526606 11331
62b8a44e 11332 while (true)
a723baf1 11333 {
62b8a44e
NS
11334 /* Peek at the next token. */
11335 token = cp_lexer_peek_token (parser->lexer);
11336 if (token->type == CPP_OPEN_PAREN)
a723baf1 11337 {
62b8a44e 11338 /* This is either a parameter-declaration-clause, or a
0cbd7506
MS
11339 parenthesized declarator. When we know we are parsing a
11340 named declarator, it must be a parenthesized declarator
11341 if FIRST is true. For instance, `(int)' is a
11342 parameter-declaration-clause, with an omitted
11343 direct-abstract-declarator. But `((*))', is a
11344 parenthesized abstract declarator. Finally, when T is a
11345 template parameter `(T)' is a
11346 parameter-declaration-clause, and not a parenthesized
11347 named declarator.
21526606 11348
62b8a44e
NS
11349 We first try and parse a parameter-declaration-clause,
11350 and then try a nested declarator (if FIRST is true).
a723baf1 11351
62b8a44e
NS
11352 It is not an error for it not to be a
11353 parameter-declaration-clause, even when FIRST is
11354 false. Consider,
11355
11356 int i (int);
11357 int i (3);
11358
11359 The first is the declaration of a function while the
11360 second is a the definition of a variable, including its
11361 initializer.
11362
11363 Having seen only the parenthesis, we cannot know which of
11364 these two alternatives should be selected. Even more
11365 complex are examples like:
11366
0cbd7506 11367 int i (int (a));
62b8a44e
NS
11368 int i (int (3));
11369
11370 The former is a function-declaration; the latter is a
21526606 11371 variable initialization.
62b8a44e 11372
34cd5ae7 11373 Thus again, we try a parameter-declaration-clause, and if
62b8a44e
NS
11374 that fails, we back out and return. */
11375
11376 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1 11377 {
058b15c1 11378 cp_parameter_declarator *params;
4047b164 11379 unsigned saved_num_template_parameter_lists;
21526606 11380
db86dd14
MM
11381 /* In a member-declarator, the only valid interpretation
11382 of a parenthesis is the start of a
11383 parameter-declaration-clause. (It is invalid to
11384 initialize a static data member with a parenthesized
11385 initializer; only the "=" form of initialization is
11386 permitted.) */
11387 if (!member_p)
11388 cp_parser_parse_tentatively (parser);
a723baf1 11389
62b8a44e
NS
11390 /* Consume the `('. */
11391 cp_lexer_consume_token (parser->lexer);
11392 if (first)
11393 {
11394 /* If this is going to be an abstract declarator, we're
11395 in a declarator and we can't have default args. */
11396 parser->default_arg_ok_p = false;
11397 parser->in_declarator_p = true;
11398 }
21526606 11399
4047b164
KL
11400 /* Inside the function parameter list, surrounding
11401 template-parameter-lists do not apply. */
11402 saved_num_template_parameter_lists
11403 = parser->num_template_parameter_lists;
11404 parser->num_template_parameter_lists = 0;
11405
62b8a44e
NS
11406 /* Parse the parameter-declaration-clause. */
11407 params = cp_parser_parameter_declaration_clause (parser);
11408
4047b164
KL
11409 parser->num_template_parameter_lists
11410 = saved_num_template_parameter_lists;
11411
62b8a44e 11412 /* If all went well, parse the cv-qualifier-seq and the
0cbd7506 11413 exception-specification. */
db86dd14 11414 if (member_p || cp_parser_parse_definitely (parser))
62b8a44e 11415 {
3c01e5df 11416 cp_cv_quals cv_quals;
62b8a44e 11417 tree exception_specification;
7efa3e22
NS
11418
11419 if (ctor_dtor_or_conv_p)
11420 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
62b8a44e
NS
11421 first = false;
11422 /* Consume the `)'. */
11423 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11424
11425 /* Parse the cv-qualifier-seq. */
3c01e5df 11426 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
62b8a44e 11427 /* And the exception-specification. */
21526606 11428 exception_specification
62b8a44e
NS
11429 = cp_parser_exception_specification_opt (parser);
11430
11431 /* Create the function-declarator. */
11432 declarator = make_call_declarator (declarator,
11433 params,
3c01e5df 11434 cv_quals,
62b8a44e
NS
11435 exception_specification);
11436 /* Any subsequent parameter lists are to do with
0cbd7506
MS
11437 return type, so are not those of the declared
11438 function. */
62b8a44e 11439 parser->default_arg_ok_p = false;
21526606 11440
62b8a44e
NS
11441 /* Repeat the main loop. */
11442 continue;
11443 }
11444 }
21526606 11445
62b8a44e
NS
11446 /* If this is the first, we can try a parenthesized
11447 declarator. */
11448 if (first)
a723baf1 11449 {
a7324e75
MM
11450 bool saved_in_type_id_in_expr_p;
11451
a723baf1 11452 parser->default_arg_ok_p = saved_default_arg_ok_p;
62b8a44e 11453 parser->in_declarator_p = saved_in_declarator_p;
21526606 11454
62b8a44e
NS
11455 /* Consume the `('. */
11456 cp_lexer_consume_token (parser->lexer);
11457 /* Parse the nested declarator. */
a7324e75
MM
11458 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11459 parser->in_type_id_in_expr_p = true;
21526606 11460 declarator
4bb8ca28 11461 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
db86dd14
MM
11462 /*parenthesized_p=*/NULL,
11463 member_p);
a7324e75 11464 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
62b8a44e
NS
11465 first = false;
11466 /* Expect a `)'. */
11467 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
058b15c1
MM
11468 declarator = cp_error_declarator;
11469 if (declarator == cp_error_declarator)
62b8a44e 11470 break;
21526606 11471
62b8a44e 11472 goto handle_declarator;
a723baf1 11473 }
9bcb9aae 11474 /* Otherwise, we must be done. */
62b8a44e
NS
11475 else
11476 break;
a723baf1 11477 }
62b8a44e
NS
11478 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11479 && token->type == CPP_OPEN_SQUARE)
a723baf1 11480 {
62b8a44e 11481 /* Parse an array-declarator. */
a723baf1
MM
11482 tree bounds;
11483
7efa3e22
NS
11484 if (ctor_dtor_or_conv_p)
11485 *ctor_dtor_or_conv_p = 0;
21526606 11486
62b8a44e
NS
11487 first = false;
11488 parser->default_arg_ok_p = false;
11489 parser->in_declarator_p = true;
a723baf1
MM
11490 /* Consume the `['. */
11491 cp_lexer_consume_token (parser->lexer);
11492 /* Peek at the next token. */
11493 token = cp_lexer_peek_token (parser->lexer);
11494 /* If the next token is `]', then there is no
11495 constant-expression. */
11496 if (token->type != CPP_CLOSE_SQUARE)
14d22dd6
MM
11497 {
11498 bool non_constant_p;
11499
21526606 11500 bounds
14d22dd6
MM
11501 = cp_parser_constant_expression (parser,
11502 /*allow_non_constant=*/true,
11503 &non_constant_p);
d17811fd 11504 if (!non_constant_p)
9baa27a9 11505 bounds = fold_non_dependent_expr (bounds);
88e95ee3
MM
11506 /* Normally, the array bound must be an integral constant
11507 expression. However, as an extension, we allow VLAs
c8094d83 11508 in function scopes. */
b671e5a4 11509 else if (!at_function_scope_p ())
44370687
MM
11510 {
11511 error ("array bound is not an integer constant");
11512 bounds = error_mark_node;
11513 }
14d22dd6 11514 }
a723baf1
MM
11515 else
11516 bounds = NULL_TREE;
11517 /* Look for the closing `]'. */
62b8a44e
NS
11518 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11519 {
058b15c1 11520 declarator = cp_error_declarator;
62b8a44e
NS
11521 break;
11522 }
a723baf1 11523
058b15c1 11524 declarator = make_array_declarator (declarator, bounds);
a723baf1 11525 }
62b8a44e 11526 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
a723baf1 11527 {
1d786913
MM
11528 tree qualifying_scope;
11529 tree unqualified_name;
d85d3d57 11530 special_function_kind sfk;
fa6098f8 11531 bool abstract_ok;
058b15c1 11532
a668c6ad 11533 /* Parse a declarator-id */
fa6098f8
MM
11534 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11535 if (abstract_ok)
62b8a44e 11536 cp_parser_parse_tentatively (parser);
fa6098f8
MM
11537 unqualified_name
11538 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
1d786913 11539 qualifying_scope = parser->scope;
fa6098f8 11540 if (abstract_ok)
712becab
NS
11541 {
11542 if (!cp_parser_parse_definitely (parser))
1d786913 11543 unqualified_name = error_mark_node;
fa6098f8
MM
11544 else if (unqualified_name
11545 && (qualifying_scope
11546 || (TREE_CODE (unqualified_name)
11547 != IDENTIFIER_NODE)))
712becab
NS
11548 {
11549 cp_parser_error (parser, "expected unqualified-id");
1d786913 11550 unqualified_name = error_mark_node;
712becab
NS
11551 }
11552 }
21526606 11553
fa6098f8
MM
11554 if (!unqualified_name)
11555 return NULL;
1d786913 11556 if (unqualified_name == error_mark_node)
058b15c1
MM
11557 {
11558 declarator = cp_error_declarator;
11559 break;
11560 }
21526606 11561
1d786913
MM
11562 if (qualifying_scope && at_namespace_scope_p ()
11563 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
62b8a44e 11564 {
62b8a44e 11565 /* In the declaration of a member of a template class
0cbd7506
MS
11566 outside of the class itself, the SCOPE will sometimes
11567 be a TYPENAME_TYPE. For example, given:
21526606 11568
0cbd7506
MS
11569 template <typename T>
11570 int S<T>::R::i = 3;
21526606 11571
0cbd7506
MS
11572 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11573 this context, we must resolve S<T>::R to an ordinary
11574 type, rather than a typename type.
21526606 11575
0cbd7506
MS
11576 The reason we normally avoid resolving TYPENAME_TYPEs
11577 is that a specialization of `S' might render
11578 `S<T>::R' not a type. However, if `S' is
11579 specialized, then this `i' will not be used, so there
11580 is no harm in resolving the types here. */
1d786913 11581 tree type;
c8094d83 11582
1d786913
MM
11583 /* Resolve the TYPENAME_TYPE. */
11584 type = resolve_typename_type (qualifying_scope,
11585 /*only_current_p=*/false);
11586 /* If that failed, the declarator is invalid. */
11587 if (type == error_mark_node)
11588 error ("%<%T::%D%> is not a type",
11589 TYPE_CONTEXT (qualifying_scope),
11590 TYPE_IDENTIFIER (qualifying_scope));
11591 qualifying_scope = type;
62b8a44e 11592 }
21526606 11593
d85d3d57 11594 sfk = sfk_none;
1d786913 11595 if (unqualified_name)
a723baf1 11596 {
62b8a44e
NS
11597 tree class_type;
11598
1d786913
MM
11599 if (qualifying_scope
11600 && CLASS_TYPE_P (qualifying_scope))
11601 class_type = qualifying_scope;
62b8a44e 11602 else
1d786913 11603 class_type = current_class_type;
62b8a44e 11604
d85d3d57 11605 if (TREE_CODE (unqualified_name) == TYPE_DECL)
058b15c1 11606 {
0e686aa6
MM
11607 tree name_type = TREE_TYPE (unqualified_name);
11608 if (class_type && same_type_p (name_type, class_type))
058b15c1 11609 {
0e686aa6
MM
11610 if (qualifying_scope
11611 && CLASSTYPE_USE_TEMPLATE (name_type))
11612 {
11613 error ("invalid use of constructor as a template");
11614 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11615 "name the constructor in a qualified name",
11616 class_type,
11617 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11618 class_type, name_type);
11619 declarator = cp_error_declarator;
11620 break;
11621 }
11622 else
11623 unqualified_name = constructor_name (class_type);
d85d3d57 11624 }
d85d3d57
MM
11625 else
11626 {
11627 /* We do not attempt to print the declarator
11628 here because we do not have enough
11629 information about its original syntactic
11630 form. */
9e281320 11631 cp_parser_error (parser, "invalid declarator");
d85d3d57
MM
11632 declarator = cp_error_declarator;
11633 break;
058b15c1
MM
11634 }
11635 }
d85d3d57
MM
11636
11637 if (class_type)
11638 {
11639 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11640 sfk = sfk_destructor;
11641 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11642 sfk = sfk_conversion;
11643 else if (/* There's no way to declare a constructor
11644 for an anonymous type, even if the type
11645 got a name for linkage purposes. */
11646 !TYPE_WAS_ANONYMOUS (class_type)
11647 && constructor_name_p (unqualified_name,
11648 class_type))
11649 {
11650 unqualified_name = constructor_name (class_type);
11651 sfk = sfk_constructor;
11652 }
11653
11654 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11655 *ctor_dtor_or_conv_p = -1;
11656 }
a723baf1 11657 }
d85d3d57
MM
11658 declarator = make_id_declarator (qualifying_scope,
11659 unqualified_name,
11660 sfk);
11661 declarator->id_loc = token->location;
62b8a44e
NS
11662
11663 handle_declarator:;
11664 scope = get_scope_of_declarator (declarator);
11665 if (scope)
91b004e5
MM
11666 /* Any names that appear after the declarator-id for a
11667 member are looked up in the containing scope. */
4514aa8c 11668 pushed_scope = push_scope (scope);
62b8a44e
NS
11669 parser->in_declarator_p = true;
11670 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
058b15c1 11671 || (declarator && declarator->kind == cdk_id))
62b8a44e
NS
11672 /* Default args are only allowed on function
11673 declarations. */
11674 parser->default_arg_ok_p = saved_default_arg_ok_p;
a723baf1 11675 else
62b8a44e
NS
11676 parser->default_arg_ok_p = false;
11677
11678 first = false;
a723baf1 11679 }
62b8a44e 11680 /* We're done. */
a723baf1
MM
11681 else
11682 break;
a723baf1
MM
11683 }
11684
11685 /* For an abstract declarator, we might wind up with nothing at this
11686 point. That's an error; the declarator is not optional. */
11687 if (!declarator)
11688 cp_parser_error (parser, "expected declarator");
11689
11690 /* If we entered a scope, we must exit it now. */
4514aa8c
NS
11691 if (pushed_scope)
11692 pop_scope (pushed_scope);
a723baf1
MM
11693
11694 parser->default_arg_ok_p = saved_default_arg_ok_p;
11695 parser->in_declarator_p = saved_in_declarator_p;
21526606 11696
a723baf1
MM
11697 return declarator;
11698}
11699
21526606 11700/* Parse a ptr-operator.
a723baf1
MM
11701
11702 ptr-operator:
11703 * cv-qualifier-seq [opt]
11704 &
11705 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11706
11707 GNU Extension:
11708
11709 ptr-operator:
11710 & cv-qualifier-seq [opt]
11711
3c01e5df
MM
11712 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11713 Returns ADDR_EXPR if a reference was used. In the case of a
11714 pointer-to-member, *TYPE is filled in with the TYPE containing the
11715 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11716 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11717 ERROR_MARK if an error occurred. */
21526606 11718
a723baf1 11719static enum tree_code
21526606 11720cp_parser_ptr_operator (cp_parser* parser,
0cbd7506 11721 tree* type,
3c01e5df 11722 cp_cv_quals *cv_quals)
a723baf1
MM
11723{
11724 enum tree_code code = ERROR_MARK;
11725 cp_token *token;
11726
11727 /* Assume that it's not a pointer-to-member. */
11728 *type = NULL_TREE;
11729 /* And that there are no cv-qualifiers. */
3c01e5df 11730 *cv_quals = TYPE_UNQUALIFIED;
a723baf1
MM
11731
11732 /* Peek at the next token. */
11733 token = cp_lexer_peek_token (parser->lexer);
11734 /* If it's a `*' or `&' we have a pointer or reference. */
11735 if (token->type == CPP_MULT || token->type == CPP_AND)
11736 {
11737 /* Remember which ptr-operator we were processing. */
11738 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11739
11740 /* Consume the `*' or `&'. */
11741 cp_lexer_consume_token (parser->lexer);
11742
11743 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11744 `&', if we are allowing GNU extensions. (The only qualifier
11745 that can legally appear after `&' is `restrict', but that is
11746 enforced during semantic analysis. */
21526606 11747 if (code == INDIRECT_REF
a723baf1 11748 || cp_parser_allow_gnu_extensions_p (parser))
3c01e5df 11749 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
a723baf1
MM
11750 }
11751 else
11752 {
11753 /* Try the pointer-to-member case. */
11754 cp_parser_parse_tentatively (parser);
11755 /* Look for the optional `::' operator. */
11756 cp_parser_global_scope_opt (parser,
11757 /*current_scope_valid_p=*/false);
11758 /* Look for the nested-name specifier. */
11759 cp_parser_nested_name_specifier (parser,
11760 /*typename_keyword_p=*/false,
11761 /*check_dependency_p=*/true,
a668c6ad
MM
11762 /*type_p=*/false,
11763 /*is_declaration=*/false);
a723baf1
MM
11764 /* If we found it, and the next token is a `*', then we are
11765 indeed looking at a pointer-to-member operator. */
11766 if (!cp_parser_error_occurred (parser)
11767 && cp_parser_require (parser, CPP_MULT, "`*'"))
11768 {
a723baf1
MM
11769 /* Indicate that the `*' operator was used. */
11770 code = INDIRECT_REF;
63c9a190
MM
11771
11772 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11773 error ("%qD is a namespace", parser->scope);
11774 else
11775 {
11776 /* The type of which the member is a member is given by the
11777 current SCOPE. */
11778 *type = parser->scope;
11779 /* The next name will not be qualified. */
11780 parser->scope = NULL_TREE;
11781 parser->qualifying_scope = NULL_TREE;
11782 parser->object_scope = NULL_TREE;
11783 /* Look for the optional cv-qualifier-seq. */
11784 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11785 }
a723baf1
MM
11786 }
11787 /* If that didn't work we don't have a ptr-operator. */
11788 if (!cp_parser_parse_definitely (parser))
11789 cp_parser_error (parser, "expected ptr-operator");
11790 }
11791
11792 return code;
11793}
11794
11795/* Parse an (optional) cv-qualifier-seq.
11796
11797 cv-qualifier-seq:
21526606 11798 cv-qualifier cv-qualifier-seq [opt]
a723baf1 11799
a723baf1
MM
11800 cv-qualifier:
11801 const
21526606 11802 volatile
a723baf1
MM
11803
11804 GNU Extension:
11805
11806 cv-qualifier:
98ca843c 11807 __restrict__
a723baf1 11808
3c01e5df
MM
11809 Returns a bitmask representing the cv-qualifiers. */
11810
11811static cp_cv_quals
11812cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
a723baf1 11813{
3c01e5df 11814 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
a723baf1 11815
3c01e5df 11816 while (true)
a723baf1 11817 {
3c01e5df
MM
11818 cp_token *token;
11819 cp_cv_quals cv_qualifier;
98ca843c 11820
3c01e5df
MM
11821 /* Peek at the next token. */
11822 token = cp_lexer_peek_token (parser->lexer);
11823 /* See if it's a cv-qualifier. */
11824 switch (token->keyword)
11825 {
11826 case RID_CONST:
11827 cv_qualifier = TYPE_QUAL_CONST;
11828 break;
98ca843c 11829
3c01e5df
MM
11830 case RID_VOLATILE:
11831 cv_qualifier = TYPE_QUAL_VOLATILE;
11832 break;
98ca843c 11833
3c01e5df
MM
11834 case RID_RESTRICT:
11835 cv_qualifier = TYPE_QUAL_RESTRICT;
11836 break;
98ca843c 11837
3c01e5df
MM
11838 default:
11839 cv_qualifier = TYPE_UNQUALIFIED;
11840 break;
11841 }
98ca843c 11842
3c01e5df
MM
11843 if (!cv_qualifier)
11844 break;
a723baf1 11845
3c01e5df
MM
11846 if (cv_quals & cv_qualifier)
11847 {
11848 error ("duplicate cv-qualifier");
11849 cp_lexer_purge_token (parser->lexer);
11850 }
11851 else
11852 {
11853 cp_lexer_consume_token (parser->lexer);
11854 cv_quals |= cv_qualifier;
11855 }
a723baf1
MM
11856 }
11857
3c01e5df 11858 return cv_quals;
a723baf1
MM
11859}
11860
11861/* Parse a declarator-id.
11862
11863 declarator-id:
11864 id-expression
21526606 11865 :: [opt] nested-name-specifier [opt] type-name
a723baf1
MM
11866
11867 In the `id-expression' case, the value returned is as for
11868 cp_parser_id_expression if the id-expression was an unqualified-id.
11869 If the id-expression was a qualified-id, then a SCOPE_REF is
11870 returned. The first operand is the scope (either a NAMESPACE_DECL
11871 or TREE_TYPE), but the second is still just a representation of an
11872 unqualified-id. */
11873
11874static tree
fa6098f8 11875cp_parser_declarator_id (cp_parser* parser, bool optional_p)
a723baf1 11876{
d85d3d57 11877 tree id;
a723baf1
MM
11878 /* The expression must be an id-expression. Assume that qualified
11879 names are the names of types so that:
11880
11881 template <class T>
11882 int S<T>::R::i = 3;
11883
11884 will work; we must treat `S<T>::R' as the name of a type.
11885 Similarly, assume that qualified names are templates, where
11886 required, so that:
11887
11888 template <class T>
11889 int S<T>::R<T>::i = 3;
11890
11891 will work, too. */
d85d3d57
MM
11892 id = cp_parser_id_expression (parser,
11893 /*template_keyword_p=*/false,
11894 /*check_dependency_p=*/false,
11895 /*template_p=*/NULL,
fa6098f8
MM
11896 /*declarator_p=*/true,
11897 optional_p);
11898 if (id && BASELINK_P (id))
d85d3d57
MM
11899 id = BASELINK_FUNCTIONS (id);
11900 return id;
a723baf1
MM
11901}
11902
11903/* Parse a type-id.
11904
11905 type-id:
11906 type-specifier-seq abstract-declarator [opt]
11907
11908 Returns the TYPE specified. */
11909
11910static tree
94edc4ab 11911cp_parser_type_id (cp_parser* parser)
a723baf1 11912{
62d1db17 11913 cp_decl_specifier_seq type_specifier_seq;
058b15c1 11914 cp_declarator *abstract_declarator;
a723baf1
MM
11915
11916 /* Parse the type-specifier-seq. */
d4113656
MM
11917 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11918 &type_specifier_seq);
62d1db17 11919 if (type_specifier_seq.type == error_mark_node)
a723baf1
MM
11920 return error_mark_node;
11921
11922 /* There might or might not be an abstract declarator. */
11923 cp_parser_parse_tentatively (parser);
11924 /* Look for the declarator. */
21526606 11925 abstract_declarator
4bb8ca28 11926 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
db86dd14
MM
11927 /*parenthesized_p=*/NULL,
11928 /*member_p=*/false);
a723baf1
MM
11929 /* Check to see if there really was a declarator. */
11930 if (!cp_parser_parse_definitely (parser))
058b15c1 11931 abstract_declarator = NULL;
a723baf1 11932
62d1db17 11933 return groktypename (&type_specifier_seq, abstract_declarator);
a723baf1
MM
11934}
11935
11936/* Parse a type-specifier-seq.
11937
11938 type-specifier-seq:
11939 type-specifier type-specifier-seq [opt]
11940
11941 GNU extension:
11942
11943 type-specifier-seq:
11944 attributes type-specifier-seq [opt]
11945
d4113656
MM
11946 If IS_CONDITION is true, we are at the start of a "condition",
11947 e.g., we've just seen "if (".
11948
62d1db17 11949 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
a723baf1 11950
62d1db17
MM
11951static void
11952cp_parser_type_specifier_seq (cp_parser* parser,
d4113656 11953 bool is_condition,
62d1db17 11954 cp_decl_specifier_seq *type_specifier_seq)
a723baf1
MM
11955{
11956 bool seen_type_specifier = false;
d4113656 11957 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
62d1db17
MM
11958
11959 /* Clear the TYPE_SPECIFIER_SEQ. */
11960 clear_decl_specs (type_specifier_seq);
a723baf1
MM
11961
11962 /* Parse the type-specifiers and attributes. */
11963 while (true)
11964 {
11965 tree type_specifier;
d4113656 11966 bool is_cv_qualifier;
a723baf1
MM
11967
11968 /* Check for attributes first. */
11969 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11970 {
98ca843c 11971 type_specifier_seq->attributes =
62d1db17
MM
11972 chainon (type_specifier_seq->attributes,
11973 cp_parser_attributes_opt (parser));
a723baf1
MM
11974 continue;
11975 }
11976
a723baf1 11977 /* Look for the type-specifier. */
21526606 11978 type_specifier = cp_parser_type_specifier (parser,
d4113656 11979 flags,
62d1db17 11980 type_specifier_seq,
a723baf1
MM
11981 /*is_declaration=*/false,
11982 NULL,
d4113656
MM
11983 &is_cv_qualifier);
11984 if (!type_specifier)
62d1db17 11985 {
d4113656
MM
11986 /* If the first type-specifier could not be found, this is not a
11987 type-specifier-seq at all. */
11988 if (!seen_type_specifier)
11989 {
11990 cp_parser_error (parser, "expected type-specifier");
11991 type_specifier_seq->type = error_mark_node;
11992 return;
11993 }
11994 /* If subsequent type-specifiers could not be found, the
11995 type-specifier-seq is complete. */
11996 break;
62d1db17 11997 }
a723baf1 11998
a723baf1 11999 seen_type_specifier = true;
d4113656
MM
12000 /* The standard says that a condition can be:
12001
0cbd7506 12002 type-specifier-seq declarator = assignment-expression
c8094d83 12003
d4113656
MM
12004 However, given:
12005
12006 struct S {};
12007 if (int S = ...)
12008
0cbd7506
MS
12009 we should treat the "S" as a declarator, not as a
12010 type-specifier. The standard doesn't say that explicitly for
12011 type-specifier-seq, but it does say that for
12012 decl-specifier-seq in an ordinary declaration. Perhaps it
12013 would be clearer just to allow a decl-specifier-seq here, and
12014 then add a semantic restriction that if any decl-specifiers
12015 that are not type-specifiers appear, the program is invalid. */
d4113656 12016 if (is_condition && !is_cv_qualifier)
c8094d83 12017 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
a723baf1 12018 }
a723baf1
MM
12019}
12020
12021/* Parse a parameter-declaration-clause.
12022
12023 parameter-declaration-clause:
12024 parameter-declaration-list [opt] ... [opt]
12025 parameter-declaration-list , ...
12026
058b15c1
MM
12027 Returns a representation for the parameter declarations. A return
12028 value of NULL indicates a parameter-declaration-clause consisting
12029 only of an ellipsis. */
a723baf1 12030
058b15c1 12031static cp_parameter_declarator *
94edc4ab 12032cp_parser_parameter_declaration_clause (cp_parser* parser)
a723baf1 12033{
058b15c1 12034 cp_parameter_declarator *parameters;
a723baf1
MM
12035 cp_token *token;
12036 bool ellipsis_p;
058b15c1 12037 bool is_error;
a723baf1
MM
12038
12039 /* Peek at the next token. */
12040 token = cp_lexer_peek_token (parser->lexer);
12041 /* Check for trivial parameter-declaration-clauses. */
12042 if (token->type == CPP_ELLIPSIS)
12043 {
12044 /* Consume the `...' token. */
12045 cp_lexer_consume_token (parser->lexer);
058b15c1 12046 return NULL;
a723baf1
MM
12047 }
12048 else if (token->type == CPP_CLOSE_PAREN)
12049 /* There are no parameters. */
c73aecdf
DE
12050 {
12051#ifndef NO_IMPLICIT_EXTERN_C
12052 if (in_system_header && current_class_type == NULL
12053 && current_lang_name == lang_name_c)
058b15c1 12054 return NULL;
c73aecdf
DE
12055 else
12056#endif
058b15c1 12057 return no_parameters;
c73aecdf 12058 }
a723baf1
MM
12059 /* Check for `(void)', too, which is a special case. */
12060 else if (token->keyword == RID_VOID
21526606 12061 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1
MM
12062 == CPP_CLOSE_PAREN))
12063 {
12064 /* Consume the `void' token. */
12065 cp_lexer_consume_token (parser->lexer);
12066 /* There are no parameters. */
058b15c1 12067 return no_parameters;
a723baf1 12068 }
21526606 12069
a723baf1 12070 /* Parse the parameter-declaration-list. */
058b15c1 12071 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
a723baf1
MM
12072 /* If a parse error occurred while parsing the
12073 parameter-declaration-list, then the entire
12074 parameter-declaration-clause is erroneous. */
058b15c1
MM
12075 if (is_error)
12076 return NULL;
a723baf1
MM
12077
12078 /* Peek at the next token. */
12079 token = cp_lexer_peek_token (parser->lexer);
12080 /* If it's a `,', the clause should terminate with an ellipsis. */
12081 if (token->type == CPP_COMMA)
12082 {
12083 /* Consume the `,'. */
12084 cp_lexer_consume_token (parser->lexer);
12085 /* Expect an ellipsis. */
21526606 12086 ellipsis_p
a723baf1
MM
12087 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12088 }
21526606 12089 /* It might also be `...' if the optional trailing `,' was
a723baf1
MM
12090 omitted. */
12091 else if (token->type == CPP_ELLIPSIS)
12092 {
12093 /* Consume the `...' token. */
12094 cp_lexer_consume_token (parser->lexer);
12095 /* And remember that we saw it. */
12096 ellipsis_p = true;
12097 }
12098 else
12099 ellipsis_p = false;
12100
12101 /* Finish the parameter list. */
058b15c1
MM
12102 if (parameters && ellipsis_p)
12103 parameters->ellipsis_p = true;
98ca843c 12104
058b15c1 12105 return parameters;
a723baf1
MM
12106}
12107
12108/* Parse a parameter-declaration-list.
12109
12110 parameter-declaration-list:
12111 parameter-declaration
12112 parameter-declaration-list , parameter-declaration
12113
12114 Returns a representation of the parameter-declaration-list, as for
12115 cp_parser_parameter_declaration_clause. However, the
058b15c1
MM
12116 `void_list_node' is never appended to the list. Upon return,
12117 *IS_ERROR will be true iff an error occurred. */
a723baf1 12118
058b15c1
MM
12119static cp_parameter_declarator *
12120cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
a723baf1 12121{
058b15c1
MM
12122 cp_parameter_declarator *parameters = NULL;
12123 cp_parameter_declarator **tail = &parameters;
12124
12125 /* Assume all will go well. */
12126 *is_error = false;
a723baf1
MM
12127
12128 /* Look for more parameters. */
12129 while (true)
12130 {
058b15c1 12131 cp_parameter_declarator *parameter;
4bb8ca28 12132 bool parenthesized_p;
a723baf1 12133 /* Parse the parameter. */
21526606
EC
12134 parameter
12135 = cp_parser_parameter_declaration (parser,
4bb8ca28
MM
12136 /*template_parm_p=*/false,
12137 &parenthesized_p);
ec194454 12138
34cd5ae7 12139 /* If a parse error occurred parsing the parameter declaration,
a723baf1 12140 then the entire parameter-declaration-list is erroneous. */
058b15c1 12141 if (!parameter)
a723baf1 12142 {
058b15c1
MM
12143 *is_error = true;
12144 parameters = NULL;
a723baf1
MM
12145 break;
12146 }
12147 /* Add the new parameter to the list. */
058b15c1
MM
12148 *tail = parameter;
12149 tail = &parameter->next;
a723baf1
MM
12150
12151 /* Peek at the next token. */
12152 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
e58a9aa1
ZL
12153 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12154 /* These are for Objective-C++ */
12155 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12156 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
a723baf1
MM
12157 /* The parameter-declaration-list is complete. */
12158 break;
12159 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12160 {
12161 cp_token *token;
12162
12163 /* Peek at the next token. */
12164 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12165 /* If it's an ellipsis, then the list is complete. */
12166 if (token->type == CPP_ELLIPSIS)
12167 break;
12168 /* Otherwise, there must be more parameters. Consume the
12169 `,'. */
12170 cp_lexer_consume_token (parser->lexer);
4bb8ca28
MM
12171 /* When parsing something like:
12172
0cbd7506 12173 int i(float f, double d)
21526606 12174
0cbd7506 12175 we can tell after seeing the declaration for "f" that we
4bb8ca28 12176 are not looking at an initialization of a variable "i",
21526606 12177 but rather at the declaration of a function "i".
4bb8ca28
MM
12178
12179 Due to the fact that the parsing of template arguments
12180 (as specified to a template-id) requires backtracking we
12181 cannot use this technique when inside a template argument
12182 list. */
12183 if (!parser->in_template_argument_list_p
4d5fe289 12184 && !parser->in_type_id_in_expr_p
0b16f8f4 12185 && cp_parser_uncommitted_to_tentative_parse_p (parser)
4bb8ca28
MM
12186 /* However, a parameter-declaration of the form
12187 "foat(f)" (which is a valid declaration of a
12188 parameter "f") can also be interpreted as an
12189 expression (the conversion of "f" to "float"). */
12190 && !parenthesized_p)
12191 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
12192 }
12193 else
12194 {
2a13a625 12195 cp_parser_error (parser, "expected %<,%> or %<...%>");
0b16f8f4 12196 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21526606 12197 cp_parser_skip_to_closing_parenthesis (parser,
4bb8ca28 12198 /*recovering=*/true,
5c832178 12199 /*or_comma=*/false,
4bb8ca28 12200 /*consume_paren=*/false);
a723baf1
MM
12201 break;
12202 }
12203 }
12204
058b15c1 12205 return parameters;
a723baf1
MM
12206}
12207
12208/* Parse a parameter declaration.
12209
12210 parameter-declaration:
12211 decl-specifier-seq declarator
12212 decl-specifier-seq declarator = assignment-expression
12213 decl-specifier-seq abstract-declarator [opt]
12214 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12215
ec194454
MM
12216 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12217 declares a template parameter. (In that case, a non-nested `>'
12218 token encountered during the parsing of the assignment-expression
12219 is not interpreted as a greater-than operator.)
a723baf1 12220
058b15c1
MM
12221 Returns a representation of the parameter, or NULL if an error
12222 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12223 true iff the declarator is of the form "(p)". */
a723baf1 12224
058b15c1 12225static cp_parameter_declarator *
21526606 12226cp_parser_parameter_declaration (cp_parser *parser,
4bb8ca28
MM
12227 bool template_parm_p,
12228 bool *parenthesized_p)
a723baf1 12229{
560ad596 12230 int declares_class_or_enum;
ec194454 12231 bool greater_than_is_operator_p;
62d1db17 12232 cp_decl_specifier_seq decl_specifiers;
058b15c1 12233 cp_declarator *declarator;
a723baf1 12234 tree default_argument;
a723baf1
MM
12235 cp_token *token;
12236 const char *saved_message;
12237
ec194454
MM
12238 /* In a template parameter, `>' is not an operator.
12239
12240 [temp.param]
12241
12242 When parsing a default template-argument for a non-type
12243 template-parameter, the first non-nested `>' is taken as the end
12244 of the template parameter-list rather than a greater-than
12245 operator. */
12246 greater_than_is_operator_p = !template_parm_p;
12247
a723baf1
MM
12248 /* Type definitions may not appear in parameter types. */
12249 saved_message = parser->type_definition_forbidden_message;
21526606 12250 parser->type_definition_forbidden_message
a723baf1
MM
12251 = "types may not be defined in parameter types";
12252
12253 /* Parse the declaration-specifiers. */
62d1db17
MM
12254 cp_parser_decl_specifier_seq (parser,
12255 CP_PARSER_FLAGS_NONE,
12256 &decl_specifiers,
12257 &declares_class_or_enum);
a723baf1
MM
12258 /* If an error occurred, there's no reason to attempt to parse the
12259 rest of the declaration. */
12260 if (cp_parser_error_occurred (parser))
12261 {
12262 parser->type_definition_forbidden_message = saved_message;
058b15c1 12263 return NULL;
a723baf1
MM
12264 }
12265
12266 /* Peek at the next token. */
12267 token = cp_lexer_peek_token (parser->lexer);
12268 /* If the next token is a `)', `,', `=', `>', or `...', then there
12269 is no declarator. */
21526606 12270 if (token->type == CPP_CLOSE_PAREN
a723baf1
MM
12271 || token->type == CPP_COMMA
12272 || token->type == CPP_EQ
12273 || token->type == CPP_ELLIPSIS
12274 || token->type == CPP_GREATER)
4bb8ca28 12275 {
058b15c1 12276 declarator = NULL;
4bb8ca28
MM
12277 if (parenthesized_p)
12278 *parenthesized_p = false;
12279 }
a723baf1
MM
12280 /* Otherwise, there should be a declarator. */
12281 else
12282 {
12283 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12284 parser->default_arg_ok_p = false;
21526606 12285
5c832178
MM
12286 /* After seeing a decl-specifier-seq, if the next token is not a
12287 "(", there is no possibility that the code is a valid
4f8163b1
MM
12288 expression. Therefore, if parsing tentatively, we commit at
12289 this point. */
5c832178 12290 if (!parser->in_template_argument_list_p
643aee72 12291 /* In an expression context, having seen:
4f8163b1 12292
a7324e75 12293 (int((char ...
4f8163b1
MM
12294
12295 we cannot be sure whether we are looking at a
a7324e75
MM
12296 function-type (taking a "char" as a parameter) or a cast
12297 of some object of type "char" to "int". */
4f8163b1 12298 && !parser->in_type_id_in_expr_p
0b16f8f4 12299 && cp_parser_uncommitted_to_tentative_parse_p (parser)
5c832178
MM
12300 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12301 cp_parser_commit_to_tentative_parse (parser);
12302 /* Parse the declarator. */
a723baf1 12303 declarator = cp_parser_declarator (parser,
62b8a44e 12304 CP_PARSER_DECLARATOR_EITHER,
4bb8ca28 12305 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
12306 parenthesized_p,
12307 /*member_p=*/false);
a723baf1 12308 parser->default_arg_ok_p = saved_default_arg_ok_p;
4971227d 12309 /* After the declarator, allow more attributes. */
62d1db17 12310 decl_specifiers.attributes
98ca843c 12311 = chainon (decl_specifiers.attributes,
62d1db17 12312 cp_parser_attributes_opt (parser));
a723baf1
MM
12313 }
12314
62b8a44e 12315 /* The restriction on defining new types applies only to the type
a723baf1
MM
12316 of the parameter, not to the default argument. */
12317 parser->type_definition_forbidden_message = saved_message;
12318
12319 /* If the next token is `=', then process a default argument. */
12320 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12321 {
12322 bool saved_greater_than_is_operator_p;
12323 /* Consume the `='. */
12324 cp_lexer_consume_token (parser->lexer);
12325
12326 /* If we are defining a class, then the tokens that make up the
12327 default argument must be saved and processed later. */
21526606 12328 if (!template_parm_p && at_class_scope_p ()
ec194454 12329 && TYPE_BEING_DEFINED (current_class_type))
a723baf1
MM
12330 {
12331 unsigned depth = 0;
c162c75e
MA
12332 cp_token *first_token;
12333 cp_token *token;
a723baf1
MM
12334
12335 /* Add tokens until we have processed the entire default
03fd3f84 12336 argument. We add the range [first_token, token). */
c162c75e 12337 first_token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
12338 while (true)
12339 {
12340 bool done = false;
a723baf1
MM
12341
12342 /* Peek at the next token. */
12343 token = cp_lexer_peek_token (parser->lexer);
12344 /* What we do depends on what token we have. */
12345 switch (token->type)
12346 {
12347 /* In valid code, a default argument must be
12348 immediately followed by a `,' `)', or `...'. */
12349 case CPP_COMMA:
12350 case CPP_CLOSE_PAREN:
12351 case CPP_ELLIPSIS:
12352 /* If we run into a non-nested `;', `}', or `]',
12353 then the code is invalid -- but the default
12354 argument is certainly over. */
12355 case CPP_SEMICOLON:
12356 case CPP_CLOSE_BRACE:
12357 case CPP_CLOSE_SQUARE:
12358 if (depth == 0)
12359 done = true;
12360 /* Update DEPTH, if necessary. */
12361 else if (token->type == CPP_CLOSE_PAREN
12362 || token->type == CPP_CLOSE_BRACE
12363 || token->type == CPP_CLOSE_SQUARE)
12364 --depth;
12365 break;
12366
12367 case CPP_OPEN_PAREN:
12368 case CPP_OPEN_SQUARE:
12369 case CPP_OPEN_BRACE:
12370 ++depth;
12371 break;
12372
12373 case CPP_GREATER:
12374 /* If we see a non-nested `>', and `>' is not an
12375 operator, then it marks the end of the default
12376 argument. */
12377 if (!depth && !greater_than_is_operator_p)
12378 done = true;
12379 break;
12380
12381 /* If we run out of tokens, issue an error message. */
12382 case CPP_EOF:
bc4071dd 12383 case CPP_PRAGMA_EOL:
a723baf1
MM
12384 error ("file ends in default argument");
12385 done = true;
12386 break;
12387
12388 case CPP_NAME:
12389 case CPP_SCOPE:
12390 /* In these cases, we should look for template-ids.
21526606 12391 For example, if the default argument is
a723baf1
MM
12392 `X<int, double>()', we need to do name lookup to
12393 figure out whether or not `X' is a template; if
34cd5ae7 12394 so, the `,' does not end the default argument.
a723baf1
MM
12395
12396 That is not yet done. */
12397 break;
12398
12399 default:
12400 break;
12401 }
12402
12403 /* If we've reached the end, stop. */
12404 if (done)
12405 break;
21526606 12406
a723baf1
MM
12407 /* Add the token to the token block. */
12408 token = cp_lexer_consume_token (parser->lexer);
a723baf1 12409 }
c162c75e
MA
12410
12411 /* Create a DEFAULT_ARG to represented the unparsed default
0cbd7506 12412 argument. */
c162c75e
MA
12413 default_argument = make_node (DEFAULT_ARG);
12414 DEFARG_TOKENS (default_argument)
01ea1ea8
NS
12415 = cp_token_cache_new (first_token, token);
12416 DEFARG_INSTANTIATIONS (default_argument) = NULL;
a723baf1
MM
12417 }
12418 /* Outside of a class definition, we can just parse the
0cbd7506 12419 assignment-expression. */
a723baf1
MM
12420 else
12421 {
12422 bool saved_local_variables_forbidden_p;
12423
12424 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12425 set correctly. */
21526606 12426 saved_greater_than_is_operator_p
a723baf1
MM
12427 = parser->greater_than_is_operator_p;
12428 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12429 /* Local variable names (and the `this' keyword) may not
12430 appear in a default argument. */
21526606 12431 saved_local_variables_forbidden_p
a723baf1
MM
12432 = parser->local_variables_forbidden_p;
12433 parser->local_variables_forbidden_p = true;
12434 /* Parse the assignment-expression. */
c8094d83 12435 default_argument
93678513 12436 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
a723baf1 12437 /* Restore saved state. */
21526606 12438 parser->greater_than_is_operator_p
a723baf1 12439 = saved_greater_than_is_operator_p;
21526606
EC
12440 parser->local_variables_forbidden_p
12441 = saved_local_variables_forbidden_p;
a723baf1
MM
12442 }
12443 if (!parser->default_arg_ok_p)
12444 {
c67d36d0 12445 if (!flag_pedantic_errors)
d4ee4d25 12446 warning (0, "deprecated use of default argument for parameter of non-function");
c67d36d0
NS
12447 else
12448 {
12449 error ("default arguments are only permitted for function parameters");
12450 default_argument = NULL_TREE;
12451 }
a723baf1
MM
12452 }
12453 }
12454 else
12455 default_argument = NULL_TREE;
21526606 12456
62d1db17 12457 return make_parameter_declarator (&decl_specifiers,
058b15c1
MM
12458 declarator,
12459 default_argument);
a723baf1
MM
12460}
12461
a723baf1
MM
12462/* Parse a function-body.
12463
12464 function-body:
12465 compound_statement */
12466
12467static void
12468cp_parser_function_body (cp_parser *parser)
12469{
325c3691 12470 cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
12471}
12472
12473/* Parse a ctor-initializer-opt followed by a function-body. Return
12474 true if a ctor-initializer was present. */
12475
12476static bool
12477cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12478{
12479 tree body;
12480 bool ctor_initializer_p;
12481
12482 /* Begin the function body. */
12483 body = begin_function_body ();
12484 /* Parse the optional ctor-initializer. */
12485 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12486 /* Parse the function-body. */
12487 cp_parser_function_body (parser);
12488 /* Finish the function body. */
12489 finish_function_body (body);
12490
12491 return ctor_initializer_p;
12492}
12493
12494/* Parse an initializer.
12495
12496 initializer:
12497 = initializer-clause
21526606 12498 ( expression-list )
a723baf1 12499
c72a1a86 12500 Returns an expression representing the initializer. If no
21526606 12501 initializer is present, NULL_TREE is returned.
a723baf1
MM
12502
12503 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12504 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
39703eb9
MM
12505 set to FALSE if there is no initializer present. If there is an
12506 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12507 is set to true; otherwise it is set to false. */
a723baf1
MM
12508
12509static tree
39703eb9
MM
12510cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12511 bool* non_constant_p)
a723baf1
MM
12512{
12513 cp_token *token;
12514 tree init;
12515
12516 /* Peek at the next token. */
12517 token = cp_lexer_peek_token (parser->lexer);
12518
12519 /* Let our caller know whether or not this initializer was
12520 parenthesized. */
12521 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
39703eb9
MM
12522 /* Assume that the initializer is constant. */
12523 *non_constant_p = false;
a723baf1
MM
12524
12525 if (token->type == CPP_EQ)
12526 {
12527 /* Consume the `='. */
12528 cp_lexer_consume_token (parser->lexer);
12529 /* Parse the initializer-clause. */
39703eb9 12530 init = cp_parser_initializer_clause (parser, non_constant_p);
a723baf1
MM
12531 }
12532 else if (token->type == CPP_OPEN_PAREN)
39703eb9 12533 init = cp_parser_parenthesized_expression_list (parser, false,
93678513 12534 /*cast_p=*/false,
39703eb9 12535 non_constant_p);
a723baf1
MM
12536 else
12537 {
12538 /* Anything else is an error. */
12539 cp_parser_error (parser, "expected initializer");
12540 init = error_mark_node;
12541 }
12542
12543 return init;
12544}
12545
21526606 12546/* Parse an initializer-clause.
a723baf1
MM
12547
12548 initializer-clause:
12549 assignment-expression
12550 { initializer-list , [opt] }
12551 { }
12552
21526606 12553 Returns an expression representing the initializer.
a723baf1
MM
12554
12555 If the `assignment-expression' production is used the value
21526606 12556 returned is simply a representation for the expression.
a723baf1
MM
12557
12558 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
4038c495 12559 the elements of the initializer-list (or NULL, if the last
a723baf1
MM
12560 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12561 NULL_TREE. There is no way to detect whether or not the optional
39703eb9
MM
12562 trailing `,' was provided. NON_CONSTANT_P is as for
12563 cp_parser_initializer. */
a723baf1
MM
12564
12565static tree
39703eb9 12566cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
12567{
12568 tree initializer;
12569
b2802a4b
R
12570 /* Assume the expression is constant. */
12571 *non_constant_p = false;
12572
a723baf1
MM
12573 /* If it is not a `{', then we are looking at an
12574 assignment-expression. */
12575 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
0da99d4e 12576 {
98ca843c 12577 initializer
0da99d4e
GB
12578 = cp_parser_constant_expression (parser,
12579 /*allow_non_constant_p=*/true,
12580 non_constant_p);
12581 if (!*non_constant_p)
12582 initializer = fold_non_dependent_expr (initializer);
12583 }
a723baf1
MM
12584 else
12585 {
12586 /* Consume the `{' token. */
12587 cp_lexer_consume_token (parser->lexer);
12588 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12589 initializer = make_node (CONSTRUCTOR);
a723baf1
MM
12590 /* If it's not a `}', then there is a non-trivial initializer. */
12591 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12592 {
12593 /* Parse the initializer list. */
12594 CONSTRUCTOR_ELTS (initializer)
39703eb9 12595 = cp_parser_initializer_list (parser, non_constant_p);
a723baf1
MM
12596 /* A trailing `,' token is allowed. */
12597 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12598 cp_lexer_consume_token (parser->lexer);
12599 }
a723baf1
MM
12600 /* Now, there should be a trailing `}'. */
12601 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12602 }
12603
12604 return initializer;
12605}
12606
12607/* Parse an initializer-list.
12608
12609 initializer-list:
12610 initializer-clause
12611 initializer-list , initializer-clause
12612
12613 GNU Extension:
21526606 12614
a723baf1
MM
12615 initializer-list:
12616 identifier : initializer-clause
12617 initializer-list, identifier : initializer-clause
12618
4038c495
GB
12619 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12620 for the initializer. If the INDEX of the elt is non-NULL, it is the
39703eb9
MM
12621 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12622 as for cp_parser_initializer. */
a723baf1 12623
4038c495 12624static VEC(constructor_elt,gc) *
39703eb9 12625cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
a723baf1 12626{
4038c495 12627 VEC(constructor_elt,gc) *v = NULL;
a723baf1 12628
39703eb9
MM
12629 /* Assume all of the expressions are constant. */
12630 *non_constant_p = false;
12631
a723baf1
MM
12632 /* Parse the rest of the list. */
12633 while (true)
12634 {
12635 cp_token *token;
12636 tree identifier;
12637 tree initializer;
39703eb9
MM
12638 bool clause_non_constant_p;
12639
a723baf1
MM
12640 /* If the next token is an identifier and the following one is a
12641 colon, we are looking at the GNU designated-initializer
12642 syntax. */
12643 if (cp_parser_allow_gnu_extensions_p (parser)
12644 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12645 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12646 {
12647 /* Consume the identifier. */
12648 identifier = cp_lexer_consume_token (parser->lexer)->value;
12649 /* Consume the `:'. */
12650 cp_lexer_consume_token (parser->lexer);
12651 }
12652 else
12653 identifier = NULL_TREE;
12654
12655 /* Parse the initializer. */
21526606 12656 initializer = cp_parser_initializer_clause (parser,
39703eb9
MM
12657 &clause_non_constant_p);
12658 /* If any clause is non-constant, so is the entire initializer. */
12659 if (clause_non_constant_p)
12660 *non_constant_p = true;
4038c495
GB
12661
12662 /* Add it to the vector. */
12663 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
a723baf1
MM
12664
12665 /* If the next token is not a comma, we have reached the end of
12666 the list. */
12667 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12668 break;
12669
12670 /* Peek at the next token. */
12671 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12672 /* If the next token is a `}', then we're still done. An
12673 initializer-clause can have a trailing `,' after the
12674 initializer-list and before the closing `}'. */
12675 if (token->type == CPP_CLOSE_BRACE)
12676 break;
12677
12678 /* Consume the `,' token. */
12679 cp_lexer_consume_token (parser->lexer);
12680 }
12681
4038c495 12682 return v;
a723baf1
MM
12683}
12684
12685/* Classes [gram.class] */
12686
12687/* Parse a class-name.
12688
12689 class-name:
12690 identifier
12691 template-id
12692
12693 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12694 to indicate that names looked up in dependent types should be
12695 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12696 keyword has been used to indicate that the name that appears next
fc6a28d7
MM
12697 is a template. TAG_TYPE indicates the explicit tag given before
12698 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12699 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12700 is the class being defined in a class-head.
a723baf1
MM
12701
12702 Returns the TYPE_DECL representing the class. */
12703
12704static tree
21526606
EC
12705cp_parser_class_name (cp_parser *parser,
12706 bool typename_keyword_p,
12707 bool template_keyword_p,
fc6a28d7 12708 enum tag_types tag_type,
a723baf1 12709 bool check_dependency_p,
a668c6ad
MM
12710 bool class_head_p,
12711 bool is_declaration)
a723baf1
MM
12712{
12713 tree decl;
12714 tree scope;
12715 bool typename_p;
e5976695
MM
12716 cp_token *token;
12717
12718 /* All class-names start with an identifier. */
12719 token = cp_lexer_peek_token (parser->lexer);
12720 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12721 {
12722 cp_parser_error (parser, "expected class-name");
12723 return error_mark_node;
12724 }
21526606 12725
a723baf1
MM
12726 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12727 to a template-id, so we save it here. */
12728 scope = parser->scope;
3adee96c
KL
12729 if (scope == error_mark_node)
12730 return error_mark_node;
21526606 12731
a723baf1
MM
12732 /* Any name names a type if we're following the `typename' keyword
12733 in a qualified name where the enclosing scope is type-dependent. */
12734 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
1fb3244a 12735 && dependent_type_p (scope));
e5976695
MM
12736 /* Handle the common case (an identifier, but not a template-id)
12737 efficiently. */
21526606 12738 if (token->type == CPP_NAME
f4abade9 12739 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
a723baf1 12740 {
91b1ca65 12741 cp_token *identifier_token;
a723baf1 12742 tree identifier;
91b1ca65 12743 bool ambiguous_p;
a723baf1
MM
12744
12745 /* Look for the identifier. */
91b1ca65
MM
12746 identifier_token = cp_lexer_peek_token (parser->lexer);
12747 ambiguous_p = identifier_token->ambiguous_p;
a723baf1
MM
12748 identifier = cp_parser_identifier (parser);
12749 /* If the next token isn't an identifier, we are certainly not
12750 looking at a class-name. */
12751 if (identifier == error_mark_node)
12752 decl = error_mark_node;
12753 /* If we know this is a type-name, there's no need to look it
12754 up. */
12755 else if (typename_p)
12756 decl = identifier;
12757 else
12758 {
91b1ca65
MM
12759 tree ambiguous_decls;
12760 /* If we already know that this lookup is ambiguous, then
12761 we've already issued an error message; there's no reason
12762 to check again. */
12763 if (ambiguous_p)
12764 {
12765 cp_parser_simulate_error (parser);
12766 return error_mark_node;
12767 }
a723baf1
MM
12768 /* If the next token is a `::', then the name must be a type
12769 name.
12770
12771 [basic.lookup.qual]
12772
12773 During the lookup for a name preceding the :: scope
12774 resolution operator, object, function, and enumerator
12775 names are ignored. */
12776 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
fc6a28d7 12777 tag_type = typename_type;
a723baf1 12778 /* Look up the name. */
21526606 12779 decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 12780 tag_type,
b0bc6e8e 12781 /*is_template=*/false,
eea9800f 12782 /*is_namespace=*/false,
8f78f01f 12783 check_dependency_p,
91b1ca65
MM
12784 &ambiguous_decls);
12785 if (ambiguous_decls)
12786 {
12787 error ("reference to %qD is ambiguous", identifier);
12788 print_candidates (ambiguous_decls);
12789 if (cp_parser_parsing_tentatively (parser))
12790 {
12791 identifier_token->ambiguous_p = true;
12792 cp_parser_simulate_error (parser);
12793 }
12794 return error_mark_node;
12795 }
a723baf1
MM
12796 }
12797 }
e5976695
MM
12798 else
12799 {
12800 /* Try a template-id. */
12801 decl = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
12802 check_dependency_p,
12803 is_declaration);
e5976695
MM
12804 if (decl == error_mark_node)
12805 return error_mark_node;
12806 }
a723baf1
MM
12807
12808 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12809
12810 /* If this is a typename, create a TYPENAME_TYPE. */
12811 if (typename_p && decl != error_mark_node)
4bfb8bba 12812 {
8da15291
GDR
12813 decl = make_typename_type (scope, decl, typename_type,
12814 /*complain=*/tf_error);
4bfb8bba
MM
12815 if (decl != error_mark_node)
12816 decl = TYPE_NAME (decl);
12817 }
a723baf1
MM
12818
12819 /* Check to see that it is really the name of a class. */
21526606 12820 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
a723baf1
MM
12821 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12822 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12823 /* Situations like this:
12824
12825 template <typename T> struct A {
21526606 12826 typename T::template X<int>::I i;
a723baf1
MM
12827 };
12828
12829 are problematic. Is `T::template X<int>' a class-name? The
12830 standard does not seem to be definitive, but there is no other
12831 valid interpretation of the following `::'. Therefore, those
12832 names are considered class-names. */
a723baf1 12833 {
94d285a5
VR
12834 decl = make_typename_type (scope, decl, tag_type, tf_error);
12835 if (decl != error_mark_node)
12836 decl = TYPE_NAME (decl);
a723baf1 12837 }
94d285a5
VR
12838 else if (TREE_CODE (decl) != TYPE_DECL
12839 || TREE_TYPE (decl) == error_mark_node
12840 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12841 decl = error_mark_node;
12842
12843 if (decl == error_mark_node)
12844 cp_parser_error (parser, "expected class-name");
a723baf1
MM
12845
12846 return decl;
12847}
12848
12849/* Parse a class-specifier.
12850
12851 class-specifier:
12852 class-head { member-specification [opt] }
12853
12854 Returns the TREE_TYPE representing the class. */
12855
12856static tree
94edc4ab 12857cp_parser_class_specifier (cp_parser* parser)
a723baf1
MM
12858{
12859 cp_token *token;
12860 tree type;
6de9cd9a 12861 tree attributes = NULL_TREE;
a723baf1
MM
12862 int has_trailing_semicolon;
12863 bool nested_name_specifier_p;
a723baf1 12864 unsigned saved_num_template_parameter_lists;
87c465f5 12865 tree old_scope = NULL_TREE;
2436b51f 12866 tree scope = NULL_TREE;
a723baf1 12867
8d241e0b 12868 push_deferring_access_checks (dk_no_deferred);
cf22909c 12869
a723baf1
MM
12870 /* Parse the class-head. */
12871 type = cp_parser_class_head (parser,
38b305d0
JM
12872 &nested_name_specifier_p,
12873 &attributes);
a723baf1
MM
12874 /* If the class-head was a semantic disaster, skip the entire body
12875 of the class. */
12876 if (!type)
12877 {
12878 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 12879 pop_deferring_access_checks ();
a723baf1
MM
12880 return error_mark_node;
12881 }
cf22909c 12882
a723baf1
MM
12883 /* Look for the `{'. */
12884 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
cf22909c
KL
12885 {
12886 pop_deferring_access_checks ();
12887 return error_mark_node;
12888 }
12889
a723baf1
MM
12890 /* Issue an error message if type-definitions are forbidden here. */
12891 cp_parser_check_type_definition (parser);
12892 /* Remember that we are defining one more class. */
12893 ++parser->num_classes_being_defined;
12894 /* Inside the class, surrounding template-parameter-lists do not
12895 apply. */
21526606
EC
12896 saved_num_template_parameter_lists
12897 = parser->num_template_parameter_lists;
a723baf1 12898 parser->num_template_parameter_lists = 0;
78757caa 12899
a723baf1 12900 /* Start the class. */
eeb23c11 12901 if (nested_name_specifier_p)
2436b51f
MM
12902 {
12903 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
87c465f5 12904 old_scope = push_inner_scope (scope);
2436b51f 12905 }
a723baf1 12906 type = begin_class_definition (type);
98ca843c 12907
a723baf1 12908 if (type == error_mark_node)
9bcb9aae 12909 /* If the type is erroneous, skip the entire body of the class. */
a723baf1
MM
12910 cp_parser_skip_to_closing_brace (parser);
12911 else
12912 /* Parse the member-specification. */
12913 cp_parser_member_specification_opt (parser);
98ca843c 12914
a723baf1
MM
12915 /* Look for the trailing `}'. */
12916 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12917 /* We get better error messages by noticing a common problem: a
12918 missing trailing `;'. */
12919 token = cp_lexer_peek_token (parser->lexer);
12920 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
38b305d0 12921 /* Look for trailing attributes to apply to this class. */
a723baf1 12922 if (cp_parser_allow_gnu_extensions_p (parser))
560ad596 12923 {
38b305d0
JM
12924 tree sub_attr = cp_parser_attributes_opt (parser);
12925 attributes = chainon (attributes, sub_attr);
560ad596 12926 }
38b305d0
JM
12927 if (type != error_mark_node)
12928 type = finish_struct (type, attributes);
87c465f5
KL
12929 if (nested_name_specifier_p)
12930 pop_inner_scope (old_scope, scope);
a723baf1
MM
12931 /* If this class is not itself within the scope of another class,
12932 then we need to parse the bodies of all of the queued function
12933 definitions. Note that the queued functions defined in a class
12934 are not always processed immediately following the
12935 class-specifier for that class. Consider:
12936
12937 struct A {
0cbd7506 12938 struct B { void f() { sizeof (A); } };
a723baf1
MM
12939 };
12940
12941 If `f' were processed before the processing of `A' were
12942 completed, there would be no way to compute the size of `A'.
12943 Note that the nesting we are interested in here is lexical --
12944 not the semantic nesting given by TYPE_CONTEXT. In particular,
12945 for:
12946
12947 struct A { struct B; };
12948 struct A::B { void f() { } };
12949
12950 there is no need to delay the parsing of `A::B::f'. */
21526606 12951 if (--parser->num_classes_being_defined == 0)
a723baf1 12952 {
8218bd34
MM
12953 tree queue_entry;
12954 tree fn;
4514aa8c
NS
12955 tree class_type = NULL_TREE;
12956 tree pushed_scope = NULL_TREE;
da611058 12957
8218bd34
MM
12958 /* In a first pass, parse default arguments to the functions.
12959 Then, in a second pass, parse the bodies of the functions.
12960 This two-phased approach handles cases like:
21526606
EC
12961
12962 struct S {
0cbd7506
MS
12963 void f() { g(); }
12964 void g(int i = 3);
12965 };
8218bd34 12966
0cbd7506 12967 */
8db1028e
NS
12968 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12969 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12970 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12971 TREE_PURPOSE (parser->unparsed_functions_queues)
12972 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
8218bd34
MM
12973 {
12974 fn = TREE_VALUE (queue_entry);
8218bd34
MM
12975 /* If there are default arguments that have not yet been processed,
12976 take care of them now. */
f44b0c8e
MM
12977 if (class_type != TREE_PURPOSE (queue_entry))
12978 {
4514aa8c
NS
12979 if (pushed_scope)
12980 pop_scope (pushed_scope);
f44b0c8e 12981 class_type = TREE_PURPOSE (queue_entry);
4514aa8c 12982 pushed_scope = push_scope (class_type);
f44b0c8e
MM
12983 }
12984 /* Make sure that any template parameters are in scope. */
12985 maybe_begin_member_template_processing (fn);
12986 /* Parse the default argument expressions. */
8218bd34
MM
12987 cp_parser_late_parsing_default_args (parser, fn);
12988 /* Remove any template parameters from the symbol table. */
12989 maybe_end_member_template_processing ();
12990 }
4514aa8c
NS
12991 if (pushed_scope)
12992 pop_scope (pushed_scope);
8218bd34 12993 /* Now parse the body of the functions. */
8db1028e
NS
12994 for (TREE_VALUE (parser->unparsed_functions_queues)
12995 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12996 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12997 TREE_VALUE (parser->unparsed_functions_queues)
12998 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
a723baf1 12999 {
a723baf1 13000 /* Figure out which function we need to process. */
a723baf1 13001 fn = TREE_VALUE (queue_entry);
a723baf1
MM
13002 /* Parse the function. */
13003 cp_parser_late_parsing_for_member (parser, fn);
a723baf1 13004 }
a723baf1
MM
13005 }
13006
13007 /* Put back any saved access checks. */
cf22909c 13008 pop_deferring_access_checks ();
a723baf1
MM
13009
13010 /* Restore the count of active template-parameter-lists. */
13011 parser->num_template_parameter_lists
13012 = saved_num_template_parameter_lists;
13013
13014 return type;
13015}
13016
13017/* Parse a class-head.
13018
13019 class-head:
13020 class-key identifier [opt] base-clause [opt]
13021 class-key nested-name-specifier identifier base-clause [opt]
21526606
EC
13022 class-key nested-name-specifier [opt] template-id
13023 base-clause [opt]
a723baf1
MM
13024
13025 GNU Extensions:
13026 class-key attributes identifier [opt] base-clause [opt]
13027 class-key attributes nested-name-specifier identifier base-clause [opt]
21526606
EC
13028 class-key attributes nested-name-specifier [opt] template-id
13029 base-clause [opt]
a723baf1
MM
13030
13031 Returns the TYPE of the indicated class. Sets
13032 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13033 involving a nested-name-specifier was used, and FALSE otherwise.
a723baf1 13034
55dcbc12 13035 Returns error_mark_node if this is not a class-head.
c8094d83 13036
a723baf1
MM
13037 Returns NULL_TREE if the class-head is syntactically valid, but
13038 semantically invalid in a way that means we should skip the entire
13039 body of the class. */
13040
13041static tree
21526606 13042cp_parser_class_head (cp_parser* parser,
38b305d0
JM
13043 bool* nested_name_specifier_p,
13044 tree *attributes_p)
a723baf1 13045{
a723baf1
MM
13046 tree nested_name_specifier;
13047 enum tag_types class_key;
13048 tree id = NULL_TREE;
13049 tree type = NULL_TREE;
13050 tree attributes;
13051 bool template_id_p = false;
13052 bool qualified_p = false;
13053 bool invalid_nested_name_p = false;
afb0918a 13054 bool invalid_explicit_specialization_p = false;
4514aa8c 13055 tree pushed_scope = NULL_TREE;
a723baf1 13056 unsigned num_templates;
cad7e87b 13057 tree bases;
a723baf1
MM
13058
13059 /* Assume no nested-name-specifier will be present. */
13060 *nested_name_specifier_p = false;
13061 /* Assume no template parameter lists will be used in defining the
13062 type. */
13063 num_templates = 0;
13064
13065 /* Look for the class-key. */
13066 class_key = cp_parser_class_key (parser);
13067 if (class_key == none_type)
13068 return error_mark_node;
13069
13070 /* Parse the attributes. */
13071 attributes = cp_parser_attributes_opt (parser);
13072
13073 /* If the next token is `::', that is invalid -- but sometimes
13074 people do try to write:
13075
21526606 13076 struct ::S {};
a723baf1
MM
13077
13078 Handle this gracefully by accepting the extra qualifier, and then
13079 issuing an error about it later if this really is a
2050a1bb 13080 class-head. If it turns out just to be an elaborated type
a723baf1
MM
13081 specifier, remain silent. */
13082 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13083 qualified_p = true;
13084
8d241e0b
KL
13085 push_deferring_access_checks (dk_no_check);
13086
a723baf1
MM
13087 /* Determine the name of the class. Begin by looking for an
13088 optional nested-name-specifier. */
21526606 13089 nested_name_specifier
a723baf1
MM
13090 = cp_parser_nested_name_specifier_opt (parser,
13091 /*typename_keyword_p=*/false,
66d418e6 13092 /*check_dependency_p=*/false,
a668c6ad
MM
13093 /*type_p=*/false,
13094 /*is_declaration=*/false);
a723baf1
MM
13095 /* If there was a nested-name-specifier, then there *must* be an
13096 identifier. */
13097 if (nested_name_specifier)
13098 {
13099 /* Although the grammar says `identifier', it really means
13100 `class-name' or `template-name'. You are only allowed to
13101 define a class that has already been declared with this
21526606 13102 syntax.
a723baf1
MM
13103
13104 The proposed resolution for Core Issue 180 says that whever
13105 you see `class T::X' you should treat `X' as a type-name.
21526606 13106
a723baf1 13107 It is OK to define an inaccessible class; for example:
21526606 13108
0cbd7506
MS
13109 class A { class B; };
13110 class A::B {};
21526606 13111
0cbd7506 13112 We do not know if we will see a class-name, or a
a723baf1
MM
13113 template-name. We look for a class-name first, in case the
13114 class-name is a template-id; if we looked for the
13115 template-name first we would stop after the template-name. */
13116 cp_parser_parse_tentatively (parser);
13117 type = cp_parser_class_name (parser,
13118 /*typename_keyword_p=*/false,
13119 /*template_keyword_p=*/false,
fc6a28d7 13120 class_type,
a723baf1 13121 /*check_dependency_p=*/false,
a668c6ad
MM
13122 /*class_head_p=*/true,
13123 /*is_declaration=*/false);
a723baf1
MM
13124 /* If that didn't work, ignore the nested-name-specifier. */
13125 if (!cp_parser_parse_definitely (parser))
13126 {
13127 invalid_nested_name_p = true;
13128 id = cp_parser_identifier (parser);
13129 if (id == error_mark_node)
13130 id = NULL_TREE;
13131 }
13132 /* If we could not find a corresponding TYPE, treat this
13133 declaration like an unqualified declaration. */
13134 if (type == error_mark_node)
13135 nested_name_specifier = NULL_TREE;
13136 /* Otherwise, count the number of templates used in TYPE and its
13137 containing scopes. */
21526606 13138 else
a723baf1
MM
13139 {
13140 tree scope;
13141
21526606 13142 for (scope = TREE_TYPE (type);
a723baf1 13143 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21526606 13144 scope = (TYPE_P (scope)
a723baf1 13145 ? TYPE_CONTEXT (scope)
21526606
EC
13146 : DECL_CONTEXT (scope)))
13147 if (TYPE_P (scope)
a723baf1
MM
13148 && CLASS_TYPE_P (scope)
13149 && CLASSTYPE_TEMPLATE_INFO (scope)
2050a1bb
MM
13150 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13151 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
a723baf1
MM
13152 ++num_templates;
13153 }
13154 }
13155 /* Otherwise, the identifier is optional. */
13156 else
13157 {
13158 /* We don't know whether what comes next is a template-id,
13159 an identifier, or nothing at all. */
13160 cp_parser_parse_tentatively (parser);
13161 /* Check for a template-id. */
21526606 13162 id = cp_parser_template_id (parser,
a723baf1 13163 /*template_keyword_p=*/false,
a668c6ad
MM
13164 /*check_dependency_p=*/true,
13165 /*is_declaration=*/true);
a723baf1
MM
13166 /* If that didn't work, it could still be an identifier. */
13167 if (!cp_parser_parse_definitely (parser))
13168 {
13169 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13170 id = cp_parser_identifier (parser);
13171 else
13172 id = NULL_TREE;
13173 }
13174 else
13175 {
13176 template_id_p = true;
13177 ++num_templates;
13178 }
13179 }
13180
8d241e0b
KL
13181 pop_deferring_access_checks ();
13182
15077df5
MM
13183 if (id)
13184 cp_parser_check_for_invalid_template_id (parser, id);
ee43dab5 13185
a723baf1
MM
13186 /* If it's not a `:' or a `{' then we can't really be looking at a
13187 class-head, since a class-head only appears as part of a
13188 class-specifier. We have to detect this situation before calling
13189 xref_tag, since that has irreversible side-effects. */
13190 if (!cp_parser_next_token_starts_class_definition_p (parser))
13191 {
2a13a625 13192 cp_parser_error (parser, "expected %<{%> or %<:%>");
a723baf1
MM
13193 return error_mark_node;
13194 }
13195
13196 /* At this point, we're going ahead with the class-specifier, even
13197 if some other problem occurs. */
13198 cp_parser_commit_to_tentative_parse (parser);
13199 /* Issue the error about the overly-qualified name now. */
13200 if (qualified_p)
13201 cp_parser_error (parser,
13202 "global qualification of class name is invalid");
13203 else if (invalid_nested_name_p)
13204 cp_parser_error (parser,
13205 "qualified name does not name a class");
88081599
MM
13206 else if (nested_name_specifier)
13207 {
13208 tree scope;
9bf0e588
VR
13209
13210 /* Reject typedef-names in class heads. */
13211 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13212 {
13213 error ("invalid class name in declaration of %qD", type);
13214 type = NULL_TREE;
13215 goto done;
13216 }
13217
88081599
MM
13218 /* Figure out in what scope the declaration is being placed. */
13219 scope = current_scope ();
88081599
MM
13220 /* If that scope does not contain the scope in which the
13221 class was originally declared, the program is invalid. */
13222 if (scope && !is_ancestor (scope, nested_name_specifier))
13223 {
2a13a625 13224 error ("declaration of %qD in %qD which does not enclose %qD",
0cbd7506 13225 type, scope, nested_name_specifier);
88081599
MM
13226 type = NULL_TREE;
13227 goto done;
13228 }
13229 /* [dcl.meaning]
13230
0cbd7506 13231 A declarator-id shall not be qualified exception of the
88081599
MM
13232 definition of a ... nested class outside of its class
13233 ... [or] a the definition or explicit instantiation of a
13234 class member of a namespace outside of its namespace. */
13235 if (scope == nested_name_specifier)
13236 {
13237 pedwarn ("extra qualification ignored");
13238 nested_name_specifier = NULL_TREE;
13239 num_templates = 0;
13240 }
13241 }
afb0918a
MM
13242 /* An explicit-specialization must be preceded by "template <>". If
13243 it is not, try to recover gracefully. */
21526606 13244 if (at_namespace_scope_p ()
afb0918a 13245 && parser->num_template_parameter_lists == 0
eeb23c11 13246 && template_id_p)
afb0918a 13247 {
2a13a625 13248 error ("an explicit specialization must be preceded by %<template <>%>");
afb0918a
MM
13249 invalid_explicit_specialization_p = true;
13250 /* Take the same action that would have been taken by
13251 cp_parser_explicit_specialization. */
13252 ++parser->num_template_parameter_lists;
13253 begin_specialization ();
13254 }
13255 /* There must be no "return" statements between this point and the
13256 end of this function; set "type "to the correct return value and
13257 use "goto done;" to return. */
a723baf1
MM
13258 /* Make sure that the right number of template parameters were
13259 present. */
13260 if (!cp_parser_check_template_parameters (parser, num_templates))
afb0918a
MM
13261 {
13262 /* If something went wrong, there is no point in even trying to
13263 process the class-definition. */
13264 type = NULL_TREE;
13265 goto done;
13266 }
a723baf1 13267
a723baf1
MM
13268 /* Look up the type. */
13269 if (template_id_p)
13270 {
13271 type = TREE_TYPE (id);
13272 maybe_process_partial_specialization (type);
4514aa8c
NS
13273 if (nested_name_specifier)
13274 pushed_scope = push_scope (nested_name_specifier);
a723baf1 13275 }
4514aa8c 13276 else if (nested_name_specifier)
a723baf1 13277 {
a723baf1
MM
13278 tree class_type;
13279
13280 /* Given:
13281
13282 template <typename T> struct S { struct T };
14d22dd6 13283 template <typename T> struct S<T>::T { };
a723baf1
MM
13284
13285 we will get a TYPENAME_TYPE when processing the definition of
13286 `S::T'. We need to resolve it to the actual type before we
13287 try to define it. */
13288 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13289 {
14d22dd6
MM
13290 class_type = resolve_typename_type (TREE_TYPE (type),
13291 /*only_current_p=*/false);
13292 if (class_type != error_mark_node)
13293 type = TYPE_NAME (class_type);
13294 else
13295 {
13296 cp_parser_error (parser, "could not resolve typename type");
13297 type = error_mark_node;
13298 }
a723baf1
MM
13299 }
13300
560ad596
MM
13301 maybe_process_partial_specialization (TREE_TYPE (type));
13302 class_type = current_class_type;
13303 /* Enter the scope indicated by the nested-name-specifier. */
4514aa8c 13304 pushed_scope = push_scope (nested_name_specifier);
560ad596
MM
13305 /* Get the canonical version of this type. */
13306 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13307 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13308 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
55dcbc12
NS
13309 {
13310 type = push_template_decl (type);
13311 if (type == error_mark_node)
13312 {
13313 type = NULL_TREE;
13314 goto done;
13315 }
13316 }
c8094d83 13317
560ad596 13318 type = TREE_TYPE (type);
4514aa8c 13319 *nested_name_specifier_p = true;
a723baf1 13320 }
4514aa8c
NS
13321 else /* The name is not a nested name. */
13322 {
13323 /* If the class was unnamed, create a dummy name. */
13324 if (!id)
13325 id = make_anon_name ();
13326 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13327 parser->num_template_parameter_lists);
13328 }
13329
a723baf1
MM
13330 /* Indicate whether this class was declared as a `class' or as a
13331 `struct'. */
13332 if (TREE_CODE (type) == RECORD_TYPE)
13333 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13334 cp_parser_check_class_key (class_key, type);
13335
744b12b6
MM
13336 /* If this type was already complete, and we see another definition,
13337 that's an error. */
13338 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13339 {
13340 error ("redefinition of %q#T", type);
dee15844 13341 error ("previous definition of %q+#T", type);
0f3744f8
VR
13342 type = NULL_TREE;
13343 goto done;
744b12b6
MM
13344 }
13345
4514aa8c 13346 /* We will have entered the scope containing the class; the names of
744b12b6 13347 base classes should be looked up in that context. For example:
a723baf1
MM
13348
13349 struct A { struct B {}; struct C; };
13350 struct A::C : B {};
13351
13352 is valid. */
cad7e87b 13353 bases = NULL_TREE;
98ca843c 13354
cad7e87b
NS
13355 /* Get the list of base-classes, if there is one. */
13356 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13357 bases = cp_parser_base_clause (parser);
98ca843c 13358
cad7e87b
NS
13359 /* Process the base classes. */
13360 xref_basetypes (type, bases);
a723baf1 13361
4514aa8c 13362 done:
a723baf1
MM
13363 /* Leave the scope given by the nested-name-specifier. We will
13364 enter the class scope itself while processing the members. */
4514aa8c
NS
13365 if (pushed_scope)
13366 pop_scope (pushed_scope);
a723baf1 13367
afb0918a
MM
13368 if (invalid_explicit_specialization_p)
13369 {
13370 end_specialization ();
13371 --parser->num_template_parameter_lists;
13372 }
38b305d0 13373 *attributes_p = attributes;
a723baf1
MM
13374 return type;
13375}
13376
13377/* Parse a class-key.
13378
13379 class-key:
13380 class
13381 struct
13382 union
13383
13384 Returns the kind of class-key specified, or none_type to indicate
13385 error. */
13386
13387static enum tag_types
94edc4ab 13388cp_parser_class_key (cp_parser* parser)
a723baf1
MM
13389{
13390 cp_token *token;
13391 enum tag_types tag_type;
13392
13393 /* Look for the class-key. */
13394 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13395 if (!token)
13396 return none_type;
13397
13398 /* Check to see if the TOKEN is a class-key. */
13399 tag_type = cp_parser_token_is_class_key (token);
13400 if (!tag_type)
13401 cp_parser_error (parser, "expected class-key");
13402 return tag_type;
13403}
13404
13405/* Parse an (optional) member-specification.
13406
13407 member-specification:
13408 member-declaration member-specification [opt]
13409 access-specifier : member-specification [opt] */
13410
13411static void
94edc4ab 13412cp_parser_member_specification_opt (cp_parser* parser)
a723baf1
MM
13413{
13414 while (true)
13415 {
13416 cp_token *token;
13417 enum rid keyword;
13418
13419 /* Peek at the next token. */
13420 token = cp_lexer_peek_token (parser->lexer);
13421 /* If it's a `}', or EOF then we've seen all the members. */
bc4071dd
RH
13422 if (token->type == CPP_CLOSE_BRACE
13423 || token->type == CPP_EOF
13424 || token->type == CPP_PRAGMA_EOL)
a723baf1
MM
13425 break;
13426
13427 /* See if this token is a keyword. */
13428 keyword = token->keyword;
13429 switch (keyword)
13430 {
13431 case RID_PUBLIC:
13432 case RID_PROTECTED:
13433 case RID_PRIVATE:
13434 /* Consume the access-specifier. */
13435 cp_lexer_consume_token (parser->lexer);
13436 /* Remember which access-specifier is active. */
13437 current_access_specifier = token->value;
13438 /* Look for the `:'. */
13439 cp_parser_require (parser, CPP_COLON, "`:'");
13440 break;
13441
13442 default:
de3fe73c
MM
13443 /* Accept #pragmas at class scope. */
13444 if (token->type == CPP_PRAGMA)
13445 {
bc4071dd 13446 cp_parser_pragma (parser, pragma_external);
de3fe73c
MM
13447 break;
13448 }
13449
a723baf1
MM
13450 /* Otherwise, the next construction must be a
13451 member-declaration. */
13452 cp_parser_member_declaration (parser);
a723baf1
MM
13453 }
13454 }
13455}
13456
21526606 13457/* Parse a member-declaration.
a723baf1
MM
13458
13459 member-declaration:
13460 decl-specifier-seq [opt] member-declarator-list [opt] ;
13461 function-definition ; [opt]
13462 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13463 using-declaration
21526606 13464 template-declaration
a723baf1
MM
13465
13466 member-declarator-list:
13467 member-declarator
13468 member-declarator-list , member-declarator
13469
13470 member-declarator:
21526606 13471 declarator pure-specifier [opt]
a723baf1 13472 declarator constant-initializer [opt]
21526606 13473 identifier [opt] : constant-expression
a723baf1
MM
13474
13475 GNU Extensions:
13476
13477 member-declaration:
13478 __extension__ member-declaration
13479
13480 member-declarator:
13481 declarator attributes [opt] pure-specifier [opt]
13482 declarator attributes [opt] constant-initializer [opt]
13483 identifier [opt] attributes [opt] : constant-expression */
13484
13485static void
94edc4ab 13486cp_parser_member_declaration (cp_parser* parser)
a723baf1 13487{
62d1db17 13488 cp_decl_specifier_seq decl_specifiers;
a723baf1
MM
13489 tree prefix_attributes;
13490 tree decl;
560ad596 13491 int declares_class_or_enum;
a723baf1
MM
13492 bool friend_p;
13493 cp_token *token;
13494 int saved_pedantic;
13495
13496 /* Check for the `__extension__' keyword. */
13497 if (cp_parser_extension_opt (parser, &saved_pedantic))
13498 {
13499 /* Recurse. */
13500 cp_parser_member_declaration (parser);
13501 /* Restore the old value of the PEDANTIC flag. */
13502 pedantic = saved_pedantic;
13503
13504 return;
13505 }
13506
13507 /* Check for a template-declaration. */
13508 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13509 {
7e2a12d3
JC
13510 /* An explicit specialization here is an error condition, and we
13511 expect the specialization handler to detect and report this. */
13512 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13513 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13514 cp_parser_explicit_specialization (parser);
13515 else
13516 cp_parser_template_declaration (parser, /*member_p=*/true);
a723baf1
MM
13517
13518 return;
13519 }
13520
13521 /* Check for a using-declaration. */
13522 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13523 {
13524 /* Parse the using-declaration. */
13525 cp_parser_using_declaration (parser);
13526
13527 return;
13528 }
21526606 13529
e58a9aa1
ZL
13530 /* Check for @defs. */
13531 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13532 {
13533 tree ivar, member;
13534 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13535 ivar = ivar_chains;
13536 while (ivar)
13537 {
13538 member = ivar;
13539 ivar = TREE_CHAIN (member);
13540 TREE_CHAIN (member) = NULL_TREE;
13541 finish_member_declaration (member);
13542 }
13543 return;
13544 }
13545
a723baf1 13546 /* Parse the decl-specifier-seq. */
62d1db17
MM
13547 cp_parser_decl_specifier_seq (parser,
13548 CP_PARSER_FLAGS_OPTIONAL,
13549 &decl_specifiers,
13550 &declares_class_or_enum);
13551 prefix_attributes = decl_specifiers.attributes;
13552 decl_specifiers.attributes = NULL_TREE;
8fbc5ae7 13553 /* Check for an invalid type-name. */
de3fe73c
MM
13554 if (!decl_specifiers.type
13555 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8fbc5ae7 13556 return;
a723baf1
MM
13557 /* If there is no declarator, then the decl-specifier-seq should
13558 specify a type. */
13559 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13560 {
13561 /* If there was no decl-specifier-seq, and the next token is a
13562 `;', then we have something like:
13563
13564 struct S { ; };
13565
13566 [class.mem]
13567
13568 Each member-declaration shall declare at least one member
13569 name of the class. */
62d1db17 13570 if (!decl_specifiers.any_specifiers_p)
a723baf1 13571 {
2cfe82fe
ZW
13572 cp_token *token = cp_lexer_peek_token (parser->lexer);
13573 if (pedantic && !token->in_system_header)
13574 pedwarn ("%Hextra %<;%>", &token->location);
a723baf1 13575 }
21526606 13576 else
a723baf1
MM
13577 {
13578 tree type;
21526606 13579
a723baf1 13580 /* See if this declaration is a friend. */
62d1db17 13581 friend_p = cp_parser_friend_p (&decl_specifiers);
a723baf1
MM
13582 /* If there were decl-specifiers, check to see if there was
13583 a class-declaration. */
62d1db17 13584 type = check_tag_decl (&decl_specifiers);
a723baf1
MM
13585 /* Nested classes have already been added to the class, but
13586 a `friend' needs to be explicitly registered. */
13587 if (friend_p)
13588 {
13589 /* If the `friend' keyword was present, the friend must
13590 be introduced with a class-key. */
13591 if (!declares_class_or_enum)
13592 error ("a class-key must be used when declaring a friend");
13593 /* In this case:
13594
21526606 13595 template <typename T> struct A {
0cbd7506
MS
13596 friend struct A<T>::B;
13597 };
21526606 13598
a723baf1
MM
13599 A<T>::B will be represented by a TYPENAME_TYPE, and
13600 therefore not recognized by check_tag_decl. */
98ca843c 13601 if (!type
62d1db17
MM
13602 && decl_specifiers.type
13603 && TYPE_P (decl_specifiers.type))
13604 type = decl_specifiers.type;
fdd09134 13605 if (!type || !TYPE_P (type))
a723baf1
MM
13606 error ("friend declaration does not name a class or "
13607 "function");
13608 else
19db77ce
KL
13609 make_friend_class (current_class_type, type,
13610 /*complain=*/true);
a723baf1
MM
13611 }
13612 /* If there is no TYPE, an error message will already have
13613 been issued. */
62d1db17 13614 else if (!type || type == error_mark_node)
a723baf1
MM
13615 ;
13616 /* An anonymous aggregate has to be handled specially; such
13617 a declaration really declares a data member (with a
13618 particular type), as opposed to a nested class. */
13619 else if (ANON_AGGR_TYPE_P (type))
13620 {
13621 /* Remove constructors and such from TYPE, now that we
34cd5ae7 13622 know it is an anonymous aggregate. */
a723baf1
MM
13623 fixup_anonymous_aggr (type);
13624 /* And make the corresponding data member. */
13625 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13626 /* Add it to the class. */
13627 finish_member_declaration (decl);
13628 }
37d407a1
KL
13629 else
13630 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
a723baf1
MM
13631 }
13632 }
13633 else
13634 {
13635 /* See if these declarations will be friends. */
62d1db17 13636 friend_p = cp_parser_friend_p (&decl_specifiers);
a723baf1 13637
21526606 13638 /* Keep going until we hit the `;' at the end of the
a723baf1
MM
13639 declaration. */
13640 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13641 {
13642 tree attributes = NULL_TREE;
13643 tree first_attribute;
13644
13645 /* Peek at the next token. */
13646 token = cp_lexer_peek_token (parser->lexer);
13647
13648 /* Check for a bitfield declaration. */
13649 if (token->type == CPP_COLON
13650 || (token->type == CPP_NAME
21526606 13651 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1
MM
13652 == CPP_COLON))
13653 {
13654 tree identifier;
13655 tree width;
13656
13657 /* Get the name of the bitfield. Note that we cannot just
13658 check TOKEN here because it may have been invalidated by
13659 the call to cp_lexer_peek_nth_token above. */
13660 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13661 identifier = cp_parser_identifier (parser);
13662 else
13663 identifier = NULL_TREE;
13664
13665 /* Consume the `:' token. */
13666 cp_lexer_consume_token (parser->lexer);
13667 /* Get the width of the bitfield. */
21526606 13668 width
14d22dd6
MM
13669 = cp_parser_constant_expression (parser,
13670 /*allow_non_constant=*/false,
13671 NULL);
a723baf1
MM
13672
13673 /* Look for attributes that apply to the bitfield. */
13674 attributes = cp_parser_attributes_opt (parser);
13675 /* Remember which attributes are prefix attributes and
13676 which are not. */
13677 first_attribute = attributes;
13678 /* Combine the attributes. */
13679 attributes = chainon (prefix_attributes, attributes);
13680
13681 /* Create the bitfield declaration. */
98ca843c 13682 decl = grokbitfield (identifier
1d786913 13683 ? make_id_declarator (NULL_TREE,
d85d3d57
MM
13684 identifier,
13685 sfk_none)
058b15c1 13686 : NULL,
62d1db17 13687 &decl_specifiers,
a723baf1
MM
13688 width);
13689 /* Apply the attributes. */
13690 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13691 }
13692 else
13693 {
058b15c1 13694 cp_declarator *declarator;
a723baf1
MM
13695 tree initializer;
13696 tree asm_specification;
7efa3e22 13697 int ctor_dtor_or_conv_p;
a723baf1
MM
13698
13699 /* Parse the declarator. */
21526606 13700 declarator
62b8a44e 13701 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 13702 &ctor_dtor_or_conv_p,
db86dd14
MM
13703 /*parenthesized_p=*/NULL,
13704 /*member_p=*/true);
a723baf1
MM
13705
13706 /* If something went wrong parsing the declarator, make sure
13707 that we at least consume some tokens. */
058b15c1 13708 if (declarator == cp_error_declarator)
a723baf1
MM
13709 {
13710 /* Skip to the end of the statement. */
13711 cp_parser_skip_to_end_of_statement (parser);
4bb8ca28
MM
13712 /* If the next token is not a semicolon, that is
13713 probably because we just skipped over the body of
13714 a function. So, we consume a semicolon if
13715 present, but do not issue an error message if it
13716 is not present. */
13717 if (cp_lexer_next_token_is (parser->lexer,
13718 CPP_SEMICOLON))
13719 cp_lexer_consume_token (parser->lexer);
13720 return;
a723baf1
MM
13721 }
13722
fc6a28d7
MM
13723 if (declares_class_or_enum & 2)
13724 cp_parser_check_for_definition_in_return_type
13725 (declarator, decl_specifiers.type);
560ad596 13726
a723baf1
MM
13727 /* Look for an asm-specification. */
13728 asm_specification = cp_parser_asm_specification_opt (parser);
13729 /* Look for attributes that apply to the declaration. */
13730 attributes = cp_parser_attributes_opt (parser);
13731 /* Remember which attributes are prefix attributes and
13732 which are not. */
13733 first_attribute = attributes;
13734 /* Combine the attributes. */
13735 attributes = chainon (prefix_attributes, attributes);
13736
13737 /* If it's an `=', then we have a constant-initializer or a
13738 pure-specifier. It is not correct to parse the
13739 initializer before registering the member declaration
13740 since the member declaration should be in scope while
13741 its initializer is processed. However, the rest of the
13742 front end does not yet provide an interface that allows
13743 us to handle this correctly. */
13744 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13745 {
13746 /* In [class.mem]:
13747
13748 A pure-specifier shall be used only in the declaration of
21526606 13749 a virtual function.
a723baf1
MM
13750
13751 A member-declarator can contain a constant-initializer
13752 only if it declares a static member of integral or
21526606 13753 enumeration type.
a723baf1
MM
13754
13755 Therefore, if the DECLARATOR is for a function, we look
13756 for a pure-specifier; otherwise, we look for a
13757 constant-initializer. When we call `grokfield', it will
13758 perform more stringent semantics checks. */
63c9a190
MM
13759 if (declarator->kind == cdk_function
13760 && declarator->declarator->kind == cdk_id)
a723baf1
MM
13761 initializer = cp_parser_pure_specifier (parser);
13762 else
4bb8ca28
MM
13763 /* Parse the initializer. */
13764 initializer = cp_parser_constant_initializer (parser);
a723baf1
MM
13765 }
13766 /* Otherwise, there is no initializer. */
13767 else
13768 initializer = NULL_TREE;
13769
13770 /* See if we are probably looking at a function
5a19910e 13771 definition. We are certainly not looking at a
a723baf1
MM
13772 member-declarator. Calling `grokfield' has
13773 side-effects, so we must not do it unless we are sure
13774 that we are looking at a member-declarator. */
21526606 13775 if (cp_parser_token_starts_function_definition_p
a723baf1 13776 (cp_lexer_peek_token (parser->lexer)))
4bb8ca28
MM
13777 {
13778 /* The grammar does not allow a pure-specifier to be
13779 used when a member function is defined. (It is
13780 possible that this fact is an oversight in the
13781 standard, since a pure function may be defined
13782 outside of the class-specifier. */
13783 if (initializer)
13784 error ("pure-specifier on function-definition");
13785 decl = cp_parser_save_member_function_body (parser,
62d1db17 13786 &decl_specifiers,
4bb8ca28
MM
13787 declarator,
13788 attributes);
13789 /* If the member was not a friend, declare it here. */
13790 if (!friend_p)
13791 finish_member_declaration (decl);
13792 /* Peek at the next token. */
13793 token = cp_lexer_peek_token (parser->lexer);
13794 /* If the next token is a semicolon, consume it. */
13795 if (token->type == CPP_SEMICOLON)
13796 cp_lexer_consume_token (parser->lexer);
13797 return;
13798 }
a723baf1 13799 else
d174af6c
MM
13800 /* Create the declaration. */
13801 decl = grokfield (declarator, &decl_specifiers,
13802 initializer, /*init_const_expr_p=*/true,
13803 asm_specification,
13804 attributes);
a723baf1
MM
13805 }
13806
13807 /* Reset PREFIX_ATTRIBUTES. */
13808 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13809 attributes = TREE_CHAIN (attributes);
13810 if (attributes)
13811 TREE_CHAIN (attributes) = NULL_TREE;
13812
13813 /* If there is any qualification still in effect, clear it
13814 now; we will be starting fresh with the next declarator. */
13815 parser->scope = NULL_TREE;
13816 parser->qualifying_scope = NULL_TREE;
13817 parser->object_scope = NULL_TREE;
13818 /* If it's a `,', then there are more declarators. */
13819 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13820 cp_lexer_consume_token (parser->lexer);
13821 /* If the next token isn't a `;', then we have a parse error. */
13822 else if (cp_lexer_next_token_is_not (parser->lexer,
13823 CPP_SEMICOLON))
13824 {
2a13a625 13825 cp_parser_error (parser, "expected %<;%>");
04c06002 13826 /* Skip tokens until we find a `;'. */
a723baf1
MM
13827 cp_parser_skip_to_end_of_statement (parser);
13828
13829 break;
13830 }
13831
13832 if (decl)
13833 {
13834 /* Add DECL to the list of members. */
13835 if (!friend_p)
13836 finish_member_declaration (decl);
13837
a723baf1 13838 if (TREE_CODE (decl) == FUNCTION_DECL)
8db1028e 13839 cp_parser_save_default_args (parser, decl);
a723baf1
MM
13840 }
13841 }
13842 }
13843
4bb8ca28 13844 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
a723baf1
MM
13845}
13846
13847/* Parse a pure-specifier.
13848
13849 pure-specifier:
13850 = 0
13851
13852 Returns INTEGER_ZERO_NODE if a pure specifier is found.
cd0be382 13853 Otherwise, ERROR_MARK_NODE is returned. */
a723baf1
MM
13854
13855static tree
94edc4ab 13856cp_parser_pure_specifier (cp_parser* parser)
a723baf1
MM
13857{
13858 cp_token *token;
13859
13860 /* Look for the `=' token. */
13861 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13862 return error_mark_node;
13863 /* Look for the `0' token. */
515e6a84 13864 token = cp_lexer_consume_token (parser->lexer);
ab84748a
VR
13865 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
13866 if (token->type == CPP_NUMBER && (token->flags & PURE_ZERO))
13867 return integer_zero_node;
a723baf1 13868
ab84748a
VR
13869 cp_parser_error (parser, "invalid pure specifier (only `= 0' is allowed)");
13870 cp_parser_skip_to_end_of_statement (parser);
13871 return error_mark_node;
a723baf1
MM
13872}
13873
13874/* Parse a constant-initializer.
13875
13876 constant-initializer:
13877 = constant-expression
13878
13879 Returns a representation of the constant-expression. */
13880
13881static tree
94edc4ab 13882cp_parser_constant_initializer (cp_parser* parser)
a723baf1
MM
13883{
13884 /* Look for the `=' token. */
13885 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13886 return error_mark_node;
13887
13888 /* It is invalid to write:
13889
13890 struct S { static const int i = { 7 }; };
13891
13892 */
13893 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13894 {
13895 cp_parser_error (parser,
13896 "a brace-enclosed initializer is not allowed here");
13897 /* Consume the opening brace. */
13898 cp_lexer_consume_token (parser->lexer);
13899 /* Skip the initializer. */
13900 cp_parser_skip_to_closing_brace (parser);
13901 /* Look for the trailing `}'. */
13902 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
21526606 13903
a723baf1
MM
13904 return error_mark_node;
13905 }
13906
21526606 13907 return cp_parser_constant_expression (parser,
14d22dd6
MM
13908 /*allow_non_constant=*/false,
13909 NULL);
a723baf1
MM
13910}
13911
13912/* Derived classes [gram.class.derived] */
13913
13914/* Parse a base-clause.
13915
13916 base-clause:
21526606 13917 : base-specifier-list
a723baf1
MM
13918
13919 base-specifier-list:
13920 base-specifier
13921 base-specifier-list , base-specifier
13922
13923 Returns a TREE_LIST representing the base-classes, in the order in
13924 which they were declared. The representation of each node is as
21526606 13925 described by cp_parser_base_specifier.
a723baf1
MM
13926
13927 In the case that no bases are specified, this function will return
13928 NULL_TREE, not ERROR_MARK_NODE. */
13929
13930static tree
94edc4ab 13931cp_parser_base_clause (cp_parser* parser)
a723baf1
MM
13932{
13933 tree bases = NULL_TREE;
13934
13935 /* Look for the `:' that begins the list. */
13936 cp_parser_require (parser, CPP_COLON, "`:'");
13937
13938 /* Scan the base-specifier-list. */
13939 while (true)
13940 {
13941 cp_token *token;
13942 tree base;
13943
13944 /* Look for the base-specifier. */
13945 base = cp_parser_base_specifier (parser);
13946 /* Add BASE to the front of the list. */
13947 if (base != error_mark_node)
13948 {
13949 TREE_CHAIN (base) = bases;
13950 bases = base;
13951 }
13952 /* Peek at the next token. */
13953 token = cp_lexer_peek_token (parser->lexer);
13954 /* If it's not a comma, then the list is complete. */
13955 if (token->type != CPP_COMMA)
13956 break;
13957 /* Consume the `,'. */
13958 cp_lexer_consume_token (parser->lexer);
13959 }
13960
13961 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13962 base class had a qualified name. However, the next name that
13963 appears is certainly not qualified. */
13964 parser->scope = NULL_TREE;
13965 parser->qualifying_scope = NULL_TREE;
13966 parser->object_scope = NULL_TREE;
13967
13968 return nreverse (bases);
13969}
13970
13971/* Parse a base-specifier.
13972
13973 base-specifier:
13974 :: [opt] nested-name-specifier [opt] class-name
13975 virtual access-specifier [opt] :: [opt] nested-name-specifier
13976 [opt] class-name
13977 access-specifier virtual [opt] :: [opt] nested-name-specifier
13978 [opt] class-name
13979
13980 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13981 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13982 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13983 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21526606 13984
a723baf1 13985static tree
94edc4ab 13986cp_parser_base_specifier (cp_parser* parser)
a723baf1
MM
13987{
13988 cp_token *token;
13989 bool done = false;
13990 bool virtual_p = false;
13991 bool duplicate_virtual_error_issued_p = false;
13992 bool duplicate_access_error_issued_p = false;
bbaab916 13993 bool class_scope_p, template_p;
dbbf88d1 13994 tree access = access_default_node;
a723baf1
MM
13995 tree type;
13996
13997 /* Process the optional `virtual' and `access-specifier'. */
13998 while (!done)
13999 {
14000 /* Peek at the next token. */
14001 token = cp_lexer_peek_token (parser->lexer);
14002 /* Process `virtual'. */
14003 switch (token->keyword)
14004 {
14005 case RID_VIRTUAL:
14006 /* If `virtual' appears more than once, issue an error. */
14007 if (virtual_p && !duplicate_virtual_error_issued_p)
14008 {
14009 cp_parser_error (parser,
2a13a625 14010 "%<virtual%> specified more than once in base-specified");
a723baf1
MM
14011 duplicate_virtual_error_issued_p = true;
14012 }
14013
14014 virtual_p = true;
14015
14016 /* Consume the `virtual' token. */
14017 cp_lexer_consume_token (parser->lexer);
14018
14019 break;
14020
14021 case RID_PUBLIC:
14022 case RID_PROTECTED:
14023 case RID_PRIVATE:
14024 /* If more than one access specifier appears, issue an
14025 error. */
dbbf88d1
NS
14026 if (access != access_default_node
14027 && !duplicate_access_error_issued_p)
a723baf1
MM
14028 {
14029 cp_parser_error (parser,
14030 "more than one access specifier in base-specified");
14031 duplicate_access_error_issued_p = true;
14032 }
14033
dbbf88d1 14034 access = ridpointers[(int) token->keyword];
a723baf1
MM
14035
14036 /* Consume the access-specifier. */
14037 cp_lexer_consume_token (parser->lexer);
14038
14039 break;
14040
14041 default:
14042 done = true;
14043 break;
14044 }
14045 }
852dcbdd 14046 /* It is not uncommon to see programs mechanically, erroneously, use
a3a503a5 14047 the 'typename' keyword to denote (dependent) qualified types
1ed53ef3
GB
14048 as base classes. */
14049 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14050 {
14051 if (!processing_template_decl)
2a13a625 14052 error ("keyword %<typename%> not allowed outside of templates");
1ed53ef3 14053 else
2a13a625 14054 error ("keyword %<typename%> not allowed in this context "
1ed53ef3
GB
14055 "(the base class is implicitly a type)");
14056 cp_lexer_consume_token (parser->lexer);
14057 }
a723baf1 14058
a723baf1
MM
14059 /* Look for the optional `::' operator. */
14060 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14061 /* Look for the nested-name-specifier. The simplest way to
14062 implement:
14063
14064 [temp.res]
14065
14066 The keyword `typename' is not permitted in a base-specifier or
14067 mem-initializer; in these contexts a qualified name that
14068 depends on a template-parameter is implicitly assumed to be a
14069 type name.
14070
14071 is to pretend that we have seen the `typename' keyword at this
21526606 14072 point. */
a723baf1
MM
14073 cp_parser_nested_name_specifier_opt (parser,
14074 /*typename_keyword_p=*/true,
14075 /*check_dependency_p=*/true,
fc6a28d7 14076 typename_type,
a668c6ad 14077 /*is_declaration=*/true);
a723baf1
MM
14078 /* If the base class is given by a qualified name, assume that names
14079 we see are type names or templates, as appropriate. */
14080 class_scope_p = (parser->scope && TYPE_P (parser->scope));
bbaab916 14081 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21526606 14082
a723baf1 14083 /* Finally, look for the class-name. */
21526606 14084 type = cp_parser_class_name (parser,
a723baf1 14085 class_scope_p,
bbaab916 14086 template_p,
fc6a28d7 14087 typename_type,
a723baf1 14088 /*check_dependency_p=*/true,
a668c6ad
MM
14089 /*class_head_p=*/false,
14090 /*is_declaration=*/true);
a723baf1
MM
14091
14092 if (type == error_mark_node)
14093 return error_mark_node;
14094
dbbf88d1 14095 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
a723baf1
MM
14096}
14097
14098/* Exception handling [gram.exception] */
14099
14100/* Parse an (optional) exception-specification.
14101
14102 exception-specification:
14103 throw ( type-id-list [opt] )
14104
14105 Returns a TREE_LIST representing the exception-specification. The
14106 TREE_VALUE of each node is a type. */
14107
14108static tree
94edc4ab 14109cp_parser_exception_specification_opt (cp_parser* parser)
a723baf1
MM
14110{
14111 cp_token *token;
14112 tree type_id_list;
14113
14114 /* Peek at the next token. */
14115 token = cp_lexer_peek_token (parser->lexer);
14116 /* If it's not `throw', then there's no exception-specification. */
14117 if (!cp_parser_is_keyword (token, RID_THROW))
14118 return NULL_TREE;
14119
14120 /* Consume the `throw'. */
14121 cp_lexer_consume_token (parser->lexer);
14122
14123 /* Look for the `('. */
14124 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14125
14126 /* Peek at the next token. */
14127 token = cp_lexer_peek_token (parser->lexer);
14128 /* If it's not a `)', then there is a type-id-list. */
14129 if (token->type != CPP_CLOSE_PAREN)
14130 {
14131 const char *saved_message;
14132
14133 /* Types may not be defined in an exception-specification. */
14134 saved_message = parser->type_definition_forbidden_message;
14135 parser->type_definition_forbidden_message
14136 = "types may not be defined in an exception-specification";
14137 /* Parse the type-id-list. */
14138 type_id_list = cp_parser_type_id_list (parser);
14139 /* Restore the saved message. */
14140 parser->type_definition_forbidden_message = saved_message;
14141 }
14142 else
14143 type_id_list = empty_except_spec;
14144
14145 /* Look for the `)'. */
14146 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14147
14148 return type_id_list;
14149}
14150
14151/* Parse an (optional) type-id-list.
14152
14153 type-id-list:
14154 type-id
14155 type-id-list , type-id
14156
14157 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14158 in the order that the types were presented. */
14159
14160static tree
94edc4ab 14161cp_parser_type_id_list (cp_parser* parser)
a723baf1
MM
14162{
14163 tree types = NULL_TREE;
14164
14165 while (true)
14166 {
14167 cp_token *token;
14168 tree type;
14169
14170 /* Get the next type-id. */
14171 type = cp_parser_type_id (parser);
14172 /* Add it to the list. */
14173 types = add_exception_specifier (types, type, /*complain=*/1);
14174 /* Peek at the next token. */
14175 token = cp_lexer_peek_token (parser->lexer);
14176 /* If it is not a `,', we are done. */
14177 if (token->type != CPP_COMMA)
14178 break;
14179 /* Consume the `,'. */
14180 cp_lexer_consume_token (parser->lexer);
14181 }
14182
14183 return nreverse (types);
14184}
14185
14186/* Parse a try-block.
14187
14188 try-block:
14189 try compound-statement handler-seq */
14190
14191static tree
94edc4ab 14192cp_parser_try_block (cp_parser* parser)
a723baf1
MM
14193{
14194 tree try_block;
14195
14196 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14197 try_block = begin_try_block ();
325c3691 14198 cp_parser_compound_statement (parser, NULL, true);
a723baf1
MM
14199 finish_try_block (try_block);
14200 cp_parser_handler_seq (parser);
14201 finish_handler_sequence (try_block);
14202
14203 return try_block;
14204}
14205
14206/* Parse a function-try-block.
14207
14208 function-try-block:
14209 try ctor-initializer [opt] function-body handler-seq */
14210
14211static bool
94edc4ab 14212cp_parser_function_try_block (cp_parser* parser)
a723baf1
MM
14213{
14214 tree try_block;
14215 bool ctor_initializer_p;
14216
14217 /* Look for the `try' keyword. */
14218 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14219 return false;
14220 /* Let the rest of the front-end know where we are. */
14221 try_block = begin_function_try_block ();
14222 /* Parse the function-body. */
21526606 14223 ctor_initializer_p
a723baf1
MM
14224 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14225 /* We're done with the `try' part. */
14226 finish_function_try_block (try_block);
14227 /* Parse the handlers. */
14228 cp_parser_handler_seq (parser);
14229 /* We're done with the handlers. */
14230 finish_function_handler_sequence (try_block);
14231
14232 return ctor_initializer_p;
14233}
14234
14235/* Parse a handler-seq.
14236
14237 handler-seq:
14238 handler handler-seq [opt] */
14239
14240static void
94edc4ab 14241cp_parser_handler_seq (cp_parser* parser)
a723baf1
MM
14242{
14243 while (true)
14244 {
14245 cp_token *token;
14246
14247 /* Parse the handler. */
14248 cp_parser_handler (parser);
14249 /* Peek at the next token. */
14250 token = cp_lexer_peek_token (parser->lexer);
14251 /* If it's not `catch' then there are no more handlers. */
14252 if (!cp_parser_is_keyword (token, RID_CATCH))
14253 break;
14254 }
14255}
14256
14257/* Parse a handler.
14258
14259 handler:
14260 catch ( exception-declaration ) compound-statement */
14261
14262static void
94edc4ab 14263cp_parser_handler (cp_parser* parser)
a723baf1
MM
14264{
14265 tree handler;
14266 tree declaration;
14267
14268 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14269 handler = begin_handler ();
14270 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14271 declaration = cp_parser_exception_declaration (parser);
14272 finish_handler_parms (declaration, handler);
14273 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
325c3691 14274 cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
14275 finish_handler (handler);
14276}
14277
14278/* Parse an exception-declaration.
14279
14280 exception-declaration:
14281 type-specifier-seq declarator
14282 type-specifier-seq abstract-declarator
14283 type-specifier-seq
21526606 14284 ...
a723baf1
MM
14285
14286 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14287 ellipsis variant is used. */
14288
14289static tree
94edc4ab 14290cp_parser_exception_declaration (cp_parser* parser)
a723baf1 14291{
058b15c1 14292 tree decl;
62d1db17 14293 cp_decl_specifier_seq type_specifiers;
058b15c1 14294 cp_declarator *declarator;
a723baf1
MM
14295 const char *saved_message;
14296
14297 /* If it's an ellipsis, it's easy to handle. */
14298 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14299 {
14300 /* Consume the `...' token. */
14301 cp_lexer_consume_token (parser->lexer);
14302 return NULL_TREE;
14303 }
14304
14305 /* Types may not be defined in exception-declarations. */
14306 saved_message = parser->type_definition_forbidden_message;
14307 parser->type_definition_forbidden_message
14308 = "types may not be defined in exception-declarations";
14309
14310 /* Parse the type-specifier-seq. */
d4113656
MM
14311 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14312 &type_specifiers);
a723baf1
MM
14313 /* If it's a `)', then there is no declarator. */
14314 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
058b15c1 14315 declarator = NULL;
a723baf1 14316 else
62b8a44e 14317 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
4bb8ca28 14318 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
14319 /*parenthesized_p=*/NULL,
14320 /*member_p=*/false);
a723baf1
MM
14321
14322 /* Restore the saved message. */
14323 parser->type_definition_forbidden_message = saved_message;
14324
62d1db17 14325 if (type_specifiers.any_specifiers_p)
058b15c1 14326 {
62d1db17 14327 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
058b15c1
MM
14328 if (decl == NULL_TREE)
14329 error ("invalid catch parameter");
14330 }
14331 else
14332 decl = NULL_TREE;
14333
14334 return decl;
a723baf1
MM
14335}
14336
21526606 14337/* Parse a throw-expression.
a723baf1
MM
14338
14339 throw-expression:
34cd5ae7 14340 throw assignment-expression [opt]
a723baf1
MM
14341
14342 Returns a THROW_EXPR representing the throw-expression. */
14343
14344static tree
94edc4ab 14345cp_parser_throw_expression (cp_parser* parser)
a723baf1
MM
14346{
14347 tree expression;
89f1a6ec 14348 cp_token* token;
a723baf1
MM
14349
14350 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
89f1a6ec
MM
14351 token = cp_lexer_peek_token (parser->lexer);
14352 /* Figure out whether or not there is an assignment-expression
14353 following the "throw" keyword. */
14354 if (token->type == CPP_COMMA
14355 || token->type == CPP_SEMICOLON
14356 || token->type == CPP_CLOSE_PAREN
14357 || token->type == CPP_CLOSE_SQUARE
14358 || token->type == CPP_CLOSE_BRACE
14359 || token->type == CPP_COLON)
a723baf1 14360 expression = NULL_TREE;
89f1a6ec 14361 else
93678513
MM
14362 expression = cp_parser_assignment_expression (parser,
14363 /*cast_p=*/false);
a723baf1
MM
14364
14365 return build_throw (expression);
14366}
14367
14368/* GNU Extensions */
14369
14370/* Parse an (optional) asm-specification.
14371
14372 asm-specification:
14373 asm ( string-literal )
14374
14375 If the asm-specification is present, returns a STRING_CST
14376 corresponding to the string-literal. Otherwise, returns
14377 NULL_TREE. */
14378
14379static tree
94edc4ab 14380cp_parser_asm_specification_opt (cp_parser* parser)
a723baf1
MM
14381{
14382 cp_token *token;
14383 tree asm_specification;
14384
14385 /* Peek at the next token. */
14386 token = cp_lexer_peek_token (parser->lexer);
21526606 14387 /* If the next token isn't the `asm' keyword, then there's no
a723baf1
MM
14388 asm-specification. */
14389 if (!cp_parser_is_keyword (token, RID_ASM))
14390 return NULL_TREE;
14391
14392 /* Consume the `asm' token. */
14393 cp_lexer_consume_token (parser->lexer);
14394 /* Look for the `('. */
14395 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14396
14397 /* Look for the string-literal. */
c162c75e 14398 asm_specification = cp_parser_string_literal (parser, false, false);
a723baf1
MM
14399
14400 /* Look for the `)'. */
14401 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14402
14403 return asm_specification;
14404}
14405
21526606 14406/* Parse an asm-operand-list.
a723baf1
MM
14407
14408 asm-operand-list:
14409 asm-operand
14410 asm-operand-list , asm-operand
21526606 14411
a723baf1 14412 asm-operand:
21526606 14413 string-literal ( expression )
a723baf1
MM
14414 [ string-literal ] string-literal ( expression )
14415
14416 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14417 each node is the expression. The TREE_PURPOSE is itself a
14418 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14419 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14420 is a STRING_CST for the string literal before the parenthesis. */
14421
14422static tree
94edc4ab 14423cp_parser_asm_operand_list (cp_parser* parser)
a723baf1
MM
14424{
14425 tree asm_operands = NULL_TREE;
14426
14427 while (true)
14428 {
14429 tree string_literal;
14430 tree expression;
14431 tree name;
21526606 14432
21526606 14433 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
a723baf1
MM
14434 {
14435 /* Consume the `[' token. */
14436 cp_lexer_consume_token (parser->lexer);
14437 /* Read the operand name. */
14438 name = cp_parser_identifier (parser);
21526606 14439 if (name != error_mark_node)
a723baf1
MM
14440 name = build_string (IDENTIFIER_LENGTH (name),
14441 IDENTIFIER_POINTER (name));
14442 /* Look for the closing `]'. */
14443 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14444 }
14445 else
14446 name = NULL_TREE;
14447 /* Look for the string-literal. */
c162c75e
MA
14448 string_literal = cp_parser_string_literal (parser, false, false);
14449
a723baf1
MM
14450 /* Look for the `('. */
14451 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14452 /* Parse the expression. */
93678513 14453 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
14454 /* Look for the `)'. */
14455 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
c162c75e 14456
a723baf1
MM
14457 /* Add this operand to the list. */
14458 asm_operands = tree_cons (build_tree_list (name, string_literal),
21526606 14459 expression,
a723baf1 14460 asm_operands);
21526606 14461 /* If the next token is not a `,', there are no more
a723baf1
MM
14462 operands. */
14463 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14464 break;
14465 /* Consume the `,'. */
14466 cp_lexer_consume_token (parser->lexer);
14467 }
14468
14469 return nreverse (asm_operands);
14470}
14471
21526606 14472/* Parse an asm-clobber-list.
a723baf1
MM
14473
14474 asm-clobber-list:
14475 string-literal
21526606 14476 asm-clobber-list , string-literal
a723baf1
MM
14477
14478 Returns a TREE_LIST, indicating the clobbers in the order that they
14479 appeared. The TREE_VALUE of each node is a STRING_CST. */
14480
14481static tree
94edc4ab 14482cp_parser_asm_clobber_list (cp_parser* parser)
a723baf1
MM
14483{
14484 tree clobbers = NULL_TREE;
14485
14486 while (true)
14487 {
a723baf1
MM
14488 tree string_literal;
14489
14490 /* Look for the string literal. */
c162c75e 14491 string_literal = cp_parser_string_literal (parser, false, false);
a723baf1
MM
14492 /* Add it to the list. */
14493 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21526606 14494 /* If the next token is not a `,', then the list is
a723baf1
MM
14495 complete. */
14496 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14497 break;
14498 /* Consume the `,' token. */
14499 cp_lexer_consume_token (parser->lexer);
14500 }
14501
14502 return clobbers;
14503}
14504
14505/* Parse an (optional) series of attributes.
14506
14507 attributes:
14508 attributes attribute
14509
14510 attribute:
21526606 14511 __attribute__ (( attribute-list [opt] ))
a723baf1
MM
14512
14513 The return value is as for cp_parser_attribute_list. */
21526606 14514
a723baf1 14515static tree
94edc4ab 14516cp_parser_attributes_opt (cp_parser* parser)
a723baf1
MM
14517{
14518 tree attributes = NULL_TREE;
14519
14520 while (true)
14521 {
14522 cp_token *token;
14523 tree attribute_list;
14524
14525 /* Peek at the next token. */
14526 token = cp_lexer_peek_token (parser->lexer);
14527 /* If it's not `__attribute__', then we're done. */
14528 if (token->keyword != RID_ATTRIBUTE)
14529 break;
14530
14531 /* Consume the `__attribute__' keyword. */
14532 cp_lexer_consume_token (parser->lexer);
14533 /* Look for the two `(' tokens. */
14534 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14535 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14536
14537 /* Peek at the next token. */
14538 token = cp_lexer_peek_token (parser->lexer);
14539 if (token->type != CPP_CLOSE_PAREN)
14540 /* Parse the attribute-list. */
14541 attribute_list = cp_parser_attribute_list (parser);
14542 else
14543 /* If the next token is a `)', then there is no attribute
14544 list. */
14545 attribute_list = NULL;
14546
14547 /* Look for the two `)' tokens. */
14548 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14549 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14550
14551 /* Add these new attributes to the list. */
14552 attributes = chainon (attributes, attribute_list);
14553 }
14554
14555 return attributes;
14556}
14557
21526606 14558/* Parse an attribute-list.
a723baf1 14559
21526606
EC
14560 attribute-list:
14561 attribute
a723baf1
MM
14562 attribute-list , attribute
14563
14564 attribute:
21526606 14565 identifier
a723baf1
MM
14566 identifier ( identifier )
14567 identifier ( identifier , expression-list )
21526606 14568 identifier ( expression-list )
a723baf1 14569
88e95ee3
MM
14570 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14571 to an attribute. The TREE_PURPOSE of each node is the identifier
14572 indicating which attribute is in use. The TREE_VALUE represents
14573 the arguments, if any. */
a723baf1
MM
14574
14575static tree
94edc4ab 14576cp_parser_attribute_list (cp_parser* parser)
a723baf1
MM
14577{
14578 tree attribute_list = NULL_TREE;
c162c75e 14579 bool save_translate_strings_p = parser->translate_strings_p;
a723baf1 14580
c162c75e 14581 parser->translate_strings_p = false;
a723baf1
MM
14582 while (true)
14583 {
14584 cp_token *token;
14585 tree identifier;
14586 tree attribute;
14587
14588 /* Look for the identifier. We also allow keywords here; for
14589 example `__attribute__ ((const))' is legal. */
14590 token = cp_lexer_peek_token (parser->lexer);
88e95ee3
MM
14591 if (token->type == CPP_NAME
14592 || token->type == CPP_KEYWORD)
14593 {
14594 /* Consume the token. */
14595 token = cp_lexer_consume_token (parser->lexer);
21526606 14596
88e95ee3 14597 /* Save away the identifier that indicates which attribute
c8094d83 14598 this is. */
88e95ee3
MM
14599 identifier = token->value;
14600 attribute = build_tree_list (identifier, NULL_TREE);
a723baf1 14601
88e95ee3
MM
14602 /* Peek at the next token. */
14603 token = cp_lexer_peek_token (parser->lexer);
14604 /* If it's an `(', then parse the attribute arguments. */
14605 if (token->type == CPP_OPEN_PAREN)
14606 {
14607 tree arguments;
a723baf1 14608
88e95ee3 14609 arguments = (cp_parser_parenthesized_expression_list
c8094d83 14610 (parser, true, /*cast_p=*/false,
88e95ee3
MM
14611 /*non_constant_p=*/NULL));
14612 /* Save the identifier and arguments away. */
14613 TREE_VALUE (attribute) = arguments;
14614 }
a723baf1 14615
88e95ee3
MM
14616 /* Add this attribute to the list. */
14617 TREE_CHAIN (attribute) = attribute_list;
14618 attribute_list = attribute;
a723baf1 14619
88e95ee3
MM
14620 token = cp_lexer_peek_token (parser->lexer);
14621 }
14622 /* Now, look for more attributes. If the next token isn't a
14623 `,', we're done. */
a723baf1
MM
14624 if (token->type != CPP_COMMA)
14625 break;
14626
cd0be382 14627 /* Consume the comma and keep going. */
a723baf1
MM
14628 cp_lexer_consume_token (parser->lexer);
14629 }
c162c75e 14630 parser->translate_strings_p = save_translate_strings_p;
a723baf1
MM
14631
14632 /* We built up the list in reverse order. */
14633 return nreverse (attribute_list);
14634}
14635
14636/* Parse an optional `__extension__' keyword. Returns TRUE if it is
14637 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14638 current value of the PEDANTIC flag, regardless of whether or not
14639 the `__extension__' keyword is present. The caller is responsible
14640 for restoring the value of the PEDANTIC flag. */
14641
14642static bool
94edc4ab 14643cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
a723baf1
MM
14644{
14645 /* Save the old value of the PEDANTIC flag. */
14646 *saved_pedantic = pedantic;
14647
14648 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14649 {
14650 /* Consume the `__extension__' token. */
14651 cp_lexer_consume_token (parser->lexer);
14652 /* We're not being pedantic while the `__extension__' keyword is
14653 in effect. */
14654 pedantic = 0;
14655
14656 return true;
14657 }
14658
14659 return false;
14660}
14661
14662/* Parse a label declaration.
14663
14664 label-declaration:
14665 __label__ label-declarator-seq ;
14666
14667 label-declarator-seq:
14668 identifier , label-declarator-seq
14669 identifier */
14670
14671static void
94edc4ab 14672cp_parser_label_declaration (cp_parser* parser)
a723baf1
MM
14673{
14674 /* Look for the `__label__' keyword. */
14675 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14676
14677 while (true)
14678 {
14679 tree identifier;
14680
14681 /* Look for an identifier. */
14682 identifier = cp_parser_identifier (parser);
cb6d4a9f
VR
14683 /* If we failed, stop. */
14684 if (identifier == error_mark_node)
14685 break;
14686 /* Declare it as a label. */
a723baf1
MM
14687 finish_label_decl (identifier);
14688 /* If the next token is a `;', stop. */
14689 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14690 break;
14691 /* Look for the `,' separating the label declarations. */
14692 cp_parser_require (parser, CPP_COMMA, "`,'");
14693 }
14694
14695 /* Look for the final `;'. */
14696 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14697}
14698
14699/* Support Functions */
14700
14701/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14702 NAME should have one of the representations used for an
14703 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14704 is returned. If PARSER->SCOPE is a dependent type, then a
14705 SCOPE_REF is returned.
14706
14707 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14708 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14709 was formed. Abstractly, such entities should not be passed to this
14710 function, because they do not need to be looked up, but it is
14711 simpler to check for this special case here, rather than at the
14712 call-sites.
14713
14714 In cases not explicitly covered above, this function returns a
14715 DECL, OVERLOAD, or baselink representing the result of the lookup.
14716 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14717 is returned.
14718
472c29c3 14719 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
fc6a28d7
MM
14720 (e.g., "struct") that was used. In that case bindings that do not
14721 refer to types are ignored.
a723baf1 14722
b0bc6e8e
KL
14723 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14724 ignored.
14725
eea9800f
MM
14726 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14727 are ignored.
14728
a723baf1 14729 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
c8094d83 14730 types.
8f78f01f 14731
91b1ca65 14732 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
bcf51da2 14733 TREE_LIST of candidates if name-lookup results in an ambiguity, and
91b1ca65 14734 NULL_TREE otherwise. */
a723baf1
MM
14735
14736static tree
21526606 14737cp_parser_lookup_name (cp_parser *parser, tree name,
fc6a28d7 14738 enum tag_types tag_type,
02ed62dd
MM
14739 bool is_template,
14740 bool is_namespace,
8f78f01f 14741 bool check_dependency,
91b1ca65 14742 tree *ambiguous_decls)
a723baf1 14743{
ef07d61b 14744 int flags = 0;
a723baf1
MM
14745 tree decl;
14746 tree object_type = parser->context->object_type;
14747
ef07d61b
VR
14748 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14749 flags |= LOOKUP_COMPLAIN;
14750
8f78f01f 14751 /* Assume that the lookup will be unambiguous. */
91b1ca65
MM
14752 if (ambiguous_decls)
14753 *ambiguous_decls = NULL_TREE;
8f78f01f 14754
a723baf1
MM
14755 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14756 no longer valid. Note that if we are parsing tentatively, and
14757 the parse fails, OBJECT_TYPE will be automatically restored. */
14758 parser->context->object_type = NULL_TREE;
14759
14760 if (name == error_mark_node)
14761 return error_mark_node;
14762
14763 /* A template-id has already been resolved; there is no lookup to
14764 do. */
14765 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14766 return name;
14767 if (BASELINK_P (name))
14768 {
50bc768d
NS
14769 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14770 == TEMPLATE_ID_EXPR);
a723baf1
MM
14771 return name;
14772 }
14773
14774 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14775 it should already have been checked to make sure that the name
14776 used matches the type being destroyed. */
14777 if (TREE_CODE (name) == BIT_NOT_EXPR)
14778 {
14779 tree type;
14780
14781 /* Figure out to which type this destructor applies. */
14782 if (parser->scope)
14783 type = parser->scope;
14784 else if (object_type)
14785 type = object_type;
14786 else
14787 type = current_class_type;
14788 /* If that's not a class type, there is no destructor. */
14789 if (!type || !CLASS_TYPE_P (type))
14790 return error_mark_node;
9f4faeae
MM
14791 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14792 lazily_declare_fn (sfk_destructor, type);
fd6e3cce
GB
14793 if (!CLASSTYPE_DESTRUCTORS (type))
14794 return error_mark_node;
a723baf1
MM
14795 /* If it was a class type, return the destructor. */
14796 return CLASSTYPE_DESTRUCTORS (type);
14797 }
14798
14799 /* By this point, the NAME should be an ordinary identifier. If
14800 the id-expression was a qualified name, the qualifying scope is
14801 stored in PARSER->SCOPE at this point. */
50bc768d 14802 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
21526606 14803
a723baf1
MM
14804 /* Perform the lookup. */
14805 if (parser->scope)
21526606 14806 {
1fb3244a 14807 bool dependent_p;
a723baf1
MM
14808
14809 if (parser->scope == error_mark_node)
14810 return error_mark_node;
14811
14812 /* If the SCOPE is dependent, the lookup must be deferred until
14813 the template is instantiated -- unless we are explicitly
14814 looking up names in uninstantiated templates. Even then, we
14815 cannot look up the name if the scope is not a class type; it
14816 might, for example, be a template type parameter. */
1fb3244a
MM
14817 dependent_p = (TYPE_P (parser->scope)
14818 && !(parser->in_declarator_p
14819 && currently_open_class (parser->scope))
14820 && dependent_type_p (parser->scope));
a723baf1 14821 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
1fb3244a 14822 && dependent_p)
a723baf1 14823 {
fc6a28d7
MM
14824 if (tag_type)
14825 {
14826 tree type;
14827
14828 /* The resolution to Core Issue 180 says that `struct
14829 A::B' should be considered a type-name, even if `A'
14830 is dependent. */
14831 type = make_typename_type (parser->scope, name, tag_type,
8da15291 14832 /*complain=*/tf_error);
fc6a28d7
MM
14833 decl = TYPE_NAME (type);
14834 }
02ed62dd
MM
14835 else if (is_template
14836 && (cp_parser_next_token_ends_template_argument_p (parser)
14837 || cp_lexer_next_token_is (parser->lexer,
14838 CPP_CLOSE_PAREN)))
5b4acce1 14839 decl = make_unbound_class_template (parser->scope,
b939a023 14840 name, NULL_TREE,
8da15291 14841 /*complain=*/tf_error);
b0bc6e8e 14842 else
02ed62dd
MM
14843 decl = build_qualified_name (/*type=*/NULL_TREE,
14844 parser->scope, name,
14845 is_template);
a723baf1
MM
14846 }
14847 else
14848 {
4514aa8c 14849 tree pushed_scope = NULL_TREE;
91b004e5 14850
a723baf1
MM
14851 /* If PARSER->SCOPE is a dependent type, then it must be a
14852 class type, and we must not be checking dependencies;
14853 otherwise, we would have processed this lookup above. So
14854 that PARSER->SCOPE is not considered a dependent base by
14855 lookup_member, we must enter the scope here. */
1fb3244a 14856 if (dependent_p)
4514aa8c 14857 pushed_scope = push_scope (parser->scope);
78dcd41a 14858 /* If the PARSER->SCOPE is a template specialization, it
a723baf1
MM
14859 may be instantiated during name lookup. In that case,
14860 errors may be issued. Even if we rollback the current
14861 tentative parse, those errors are valid. */
c8094d83
MS
14862 decl = lookup_qualified_name (parser->scope, name,
14863 tag_type != none_type,
5e08432e 14864 /*complain=*/true);
4514aa8c
NS
14865 if (pushed_scope)
14866 pop_scope (pushed_scope);
a723baf1
MM
14867 }
14868 parser->qualifying_scope = parser->scope;
14869 parser->object_scope = NULL_TREE;
14870 }
14871 else if (object_type)
14872 {
14873 tree object_decl = NULL_TREE;
14874 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14875 OBJECT_TYPE is not a class. */
14876 if (CLASS_TYPE_P (object_type))
14877 /* If the OBJECT_TYPE is a template specialization, it may
14878 be instantiated during name lookup. In that case, errors
14879 may be issued. Even if we rollback the current tentative
14880 parse, those errors are valid. */
14881 object_decl = lookup_member (object_type,
14882 name,
c8094d83 14883 /*protect=*/0,
fc6a28d7 14884 tag_type != none_type);
a723baf1 14885 /* Look it up in the enclosing context, too. */
c8094d83 14886 decl = lookup_name_real (name, tag_type != none_type,
fc6a28d7 14887 /*nonclass=*/0,
ef07d61b 14888 /*block_p=*/true, is_namespace, flags);
a723baf1
MM
14889 parser->object_scope = object_type;
14890 parser->qualifying_scope = NULL_TREE;
14891 if (object_decl)
14892 decl = object_decl;
14893 }
14894 else
14895 {
c8094d83 14896 decl = lookup_name_real (name, tag_type != none_type,
fc6a28d7 14897 /*nonclass=*/0,
ef07d61b 14898 /*block_p=*/true, is_namespace, flags);
a723baf1
MM
14899 parser->qualifying_scope = NULL_TREE;
14900 parser->object_scope = NULL_TREE;
14901 }
14902
14903 /* If the lookup failed, let our caller know. */
bd3d082e 14904 if (!decl || decl == error_mark_node)
a723baf1
MM
14905 return error_mark_node;
14906
14907 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14908 if (TREE_CODE (decl) == TREE_LIST)
14909 {
91b1ca65
MM
14910 if (ambiguous_decls)
14911 *ambiguous_decls = decl;
a723baf1
MM
14912 /* The error message we have to print is too complicated for
14913 cp_parser_error, so we incorporate its actions directly. */
e5976695 14914 if (!cp_parser_simulate_error (parser))
a723baf1 14915 {
2a13a625 14916 error ("reference to %qD is ambiguous", name);
a723baf1
MM
14917 print_candidates (decl);
14918 }
14919 return error_mark_node;
14920 }
14921
50bc768d
NS
14922 gcc_assert (DECL_P (decl)
14923 || TREE_CODE (decl) == OVERLOAD
14924 || TREE_CODE (decl) == SCOPE_REF
14925 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14926 || BASELINK_P (decl));
a723baf1
MM
14927
14928 /* If we have resolved the name of a member declaration, check to
14929 see if the declaration is accessible. When the name resolves to
34cd5ae7 14930 set of overloaded functions, accessibility is checked when
21526606 14931 overload resolution is done.
a723baf1
MM
14932
14933 During an explicit instantiation, access is not checked at all,
14934 as per [temp.explicit]. */
8d241e0b 14935 if (DECL_P (decl))
ee76b931 14936 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
a723baf1
MM
14937
14938 return decl;
14939}
14940
14941/* Like cp_parser_lookup_name, but for use in the typical case where
b0bc6e8e
KL
14942 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14943 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
a723baf1
MM
14944
14945static tree
94edc4ab 14946cp_parser_lookup_name_simple (cp_parser* parser, tree name)
a723baf1 14947{
21526606 14948 return cp_parser_lookup_name (parser, name,
fc6a28d7 14949 none_type,
b0bc6e8e 14950 /*is_template=*/false,
eea9800f 14951 /*is_namespace=*/false,
8f78f01f 14952 /*check_dependency=*/true,
91b1ca65 14953 /*ambiguous_decls=*/NULL);
a723baf1
MM
14954}
14955
a723baf1
MM
14956/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14957 the current context, return the TYPE_DECL. If TAG_NAME_P is
14958 true, the DECL indicates the class being defined in a class-head,
14959 or declared in an elaborated-type-specifier.
14960
14961 Otherwise, return DECL. */
14962
14963static tree
14964cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14965{
710b73e6
KL
14966 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14967 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
a723baf1 14968
21526606 14969 struct A {
0cbd7506 14970 template <typename T> struct B;
a723baf1
MM
14971 };
14972
21526606
EC
14973 template <typename T> struct A::B {};
14974
c72a1a86 14975 Similarly, in an elaborated-type-specifier:
a723baf1
MM
14976
14977 namespace N { struct X{}; }
14978
14979 struct A {
0cbd7506 14980 template <typename T> friend struct N::X;
a723baf1
MM
14981 };
14982
710b73e6
KL
14983 However, if the DECL refers to a class type, and we are in
14984 the scope of the class, then the name lookup automatically
14985 finds the TYPE_DECL created by build_self_reference rather
14986 than a TEMPLATE_DECL. For example, in:
14987
14988 template <class T> struct S {
0cbd7506 14989 S s;
710b73e6
KL
14990 };
14991
14992 there is no need to handle such case. */
14993
14994 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
a723baf1
MM
14995 return DECL_TEMPLATE_RESULT (decl);
14996
14997 return decl;
14998}
14999
15000/* If too many, or too few, template-parameter lists apply to the
15001 declarator, issue an error message. Returns TRUE if all went well,
15002 and FALSE otherwise. */
15003
15004static bool
21526606 15005cp_parser_check_declarator_template_parameters (cp_parser* parser,
058b15c1 15006 cp_declarator *declarator)
a723baf1
MM
15007{
15008 unsigned num_templates;
15009
15010 /* We haven't seen any classes that involve template parameters yet. */
15011 num_templates = 0;
15012
058b15c1 15013 switch (declarator->kind)
a723baf1 15014 {
058b15c1 15015 case cdk_id:
1d786913 15016 if (declarator->u.id.qualifying_scope)
058b15c1
MM
15017 {
15018 tree scope;
15019 tree member;
a723baf1 15020
1d786913
MM
15021 scope = declarator->u.id.qualifying_scope;
15022 member = declarator->u.id.unqualified_name;
a723baf1 15023
058b15c1
MM
15024 while (scope && CLASS_TYPE_P (scope))
15025 {
15026 /* You're supposed to have one `template <...>'
15027 for every template class, but you don't need one
15028 for a full specialization. For example:
15029
15030 template <class T> struct S{};
15031 template <> struct S<int> { void f(); };
15032 void S<int>::f () {}
15033
15034 is correct; there shouldn't be a `template <>' for
15035 the definition of `S<int>::f'. */
15036 if (CLASSTYPE_TEMPLATE_INFO (scope)
15037 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
15038 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
15039 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15040 ++num_templates;
15041
15042 scope = TYPE_CONTEXT (scope);
15043 }
15044 }
c8094d83 15045 else if (TREE_CODE (declarator->u.id.unqualified_name)
1d786913
MM
15046 == TEMPLATE_ID_EXPR)
15047 /* If the DECLARATOR has the form `X<y>' then it uses one
15048 additional level of template parameters. */
a723baf1
MM
15049 ++num_templates;
15050
21526606 15051 return cp_parser_check_template_parameters (parser,
a723baf1 15052 num_templates);
058b15c1
MM
15053
15054 case cdk_function:
15055 case cdk_array:
15056 case cdk_pointer:
15057 case cdk_reference:
15058 case cdk_ptrmem:
98ca843c 15059 return (cp_parser_check_declarator_template_parameters
058b15c1
MM
15060 (parser, declarator->declarator));
15061
15062 case cdk_error:
15063 return true;
15064
15065 default:
315fb5db 15066 gcc_unreachable ();
a723baf1 15067 }
315fb5db 15068 return false;
a723baf1
MM
15069}
15070
15071/* NUM_TEMPLATES were used in the current declaration. If that is
15072 invalid, return FALSE and issue an error messages. Otherwise,
15073 return TRUE. */
15074
15075static bool
94edc4ab 15076cp_parser_check_template_parameters (cp_parser* parser,
0cbd7506 15077 unsigned num_templates)
a723baf1
MM
15078{
15079 /* If there are more template classes than parameter lists, we have
15080 something like:
21526606 15081
a723baf1
MM
15082 template <class T> void S<T>::R<T>::f (); */
15083 if (parser->num_template_parameter_lists < num_templates)
15084 {
15085 error ("too few template-parameter-lists");
15086 return false;
15087 }
15088 /* If there are the same number of template classes and parameter
15089 lists, that's OK. */
15090 if (parser->num_template_parameter_lists == num_templates)
15091 return true;
15092 /* If there are more, but only one more, then we are referring to a
15093 member template. That's OK too. */
15094 if (parser->num_template_parameter_lists == num_templates + 1)
15095 return true;
15096 /* Otherwise, there are too many template parameter lists. We have
15097 something like:
15098
15099 template <class T> template <class U> void S::f(); */
15100 error ("too many template-parameter-lists");
15101 return false;
15102}
15103
a723baf1
MM
15104/* Parse an optional `::' token indicating that the following name is
15105 from the global namespace. If so, PARSER->SCOPE is set to the
15106 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15107 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15108 Returns the new value of PARSER->SCOPE, if the `::' token is
15109 present, and NULL_TREE otherwise. */
15110
15111static tree
94edc4ab 15112cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
a723baf1
MM
15113{
15114 cp_token *token;
15115
15116 /* Peek at the next token. */
15117 token = cp_lexer_peek_token (parser->lexer);
15118 /* If we're looking at a `::' token then we're starting from the
15119 global namespace, not our current location. */
15120 if (token->type == CPP_SCOPE)
15121 {
15122 /* Consume the `::' token. */
15123 cp_lexer_consume_token (parser->lexer);
15124 /* Set the SCOPE so that we know where to start the lookup. */
15125 parser->scope = global_namespace;
15126 parser->qualifying_scope = global_namespace;
15127 parser->object_scope = NULL_TREE;
15128
15129 return parser->scope;
15130 }
15131 else if (!current_scope_valid_p)
15132 {
15133 parser->scope = NULL_TREE;
15134 parser->qualifying_scope = NULL_TREE;
15135 parser->object_scope = NULL_TREE;
15136 }
15137
15138 return NULL_TREE;
15139}
15140
15141/* Returns TRUE if the upcoming token sequence is the start of a
15142 constructor declarator. If FRIEND_P is true, the declarator is
15143 preceded by the `friend' specifier. */
15144
15145static bool
15146cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15147{
15148 bool constructor_p;
15149 tree type_decl = NULL_TREE;
15150 bool nested_name_p;
2050a1bb
MM
15151 cp_token *next_token;
15152
15153 /* The common case is that this is not a constructor declarator, so
8fbc5ae7
MM
15154 try to avoid doing lots of work if at all possible. It's not
15155 valid declare a constructor at function scope. */
15156 if (at_function_scope_p ())
15157 return false;
15158 /* And only certain tokens can begin a constructor declarator. */
2050a1bb
MM
15159 next_token = cp_lexer_peek_token (parser->lexer);
15160 if (next_token->type != CPP_NAME
15161 && next_token->type != CPP_SCOPE
15162 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15163 && next_token->type != CPP_TEMPLATE_ID)
15164 return false;
a723baf1
MM
15165
15166 /* Parse tentatively; we are going to roll back all of the tokens
15167 consumed here. */
15168 cp_parser_parse_tentatively (parser);
15169 /* Assume that we are looking at a constructor declarator. */
15170 constructor_p = true;
8d241e0b 15171
a723baf1
MM
15172 /* Look for the optional `::' operator. */
15173 cp_parser_global_scope_opt (parser,
15174 /*current_scope_valid_p=*/false);
15175 /* Look for the nested-name-specifier. */
21526606 15176 nested_name_p
a723baf1
MM
15177 = (cp_parser_nested_name_specifier_opt (parser,
15178 /*typename_keyword_p=*/false,
15179 /*check_dependency_p=*/false,
a668c6ad
MM
15180 /*type_p=*/false,
15181 /*is_declaration=*/false)
a723baf1
MM
15182 != NULL_TREE);
15183 /* Outside of a class-specifier, there must be a
15184 nested-name-specifier. */
21526606 15185 if (!nested_name_p &&
a723baf1
MM
15186 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15187 || friend_p))
15188 constructor_p = false;
15189 /* If we still think that this might be a constructor-declarator,
15190 look for a class-name. */
15191 if (constructor_p)
15192 {
15193 /* If we have:
15194
8fbc5ae7 15195 template <typename T> struct S { S(); };
a723baf1
MM
15196 template <typename T> S<T>::S ();
15197
15198 we must recognize that the nested `S' names a class.
15199 Similarly, for:
15200
15201 template <typename T> S<T>::S<T> ();
15202
15203 we must recognize that the nested `S' names a template. */
15204 type_decl = cp_parser_class_name (parser,
15205 /*typename_keyword_p=*/false,
15206 /*template_keyword_p=*/false,
fc6a28d7 15207 none_type,
a723baf1 15208 /*check_dependency_p=*/false,
a668c6ad
MM
15209 /*class_head_p=*/false,
15210 /*is_declaration=*/false);
a723baf1
MM
15211 /* If there was no class-name, then this is not a constructor. */
15212 constructor_p = !cp_parser_error_occurred (parser);
15213 }
8d241e0b 15214
a723baf1
MM
15215 /* If we're still considering a constructor, we have to see a `(',
15216 to begin the parameter-declaration-clause, followed by either a
15217 `)', an `...', or a decl-specifier. We need to check for a
15218 type-specifier to avoid being fooled into thinking that:
15219
15220 S::S (f) (int);
15221
15222 is a constructor. (It is actually a function named `f' that
15223 takes one parameter (of type `int') and returns a value of type
15224 `S::S'. */
21526606 15225 if (constructor_p
a723baf1
MM
15226 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15227 {
15228 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15229 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15077df5
MM
15230 /* A parameter declaration begins with a decl-specifier,
15231 which is either the "attribute" keyword, a storage class
15232 specifier, or (usually) a type-specifier. */
15233 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
a723baf1
MM
15234 && !cp_parser_storage_class_specifier_opt (parser))
15235 {
5dae1114 15236 tree type;
4514aa8c 15237 tree pushed_scope = NULL_TREE;
4047b164 15238 unsigned saved_num_template_parameter_lists;
5dae1114
MM
15239
15240 /* Names appearing in the type-specifier should be looked up
15241 in the scope of the class. */
15242 if (current_class_type)
15243 type = NULL_TREE;
a723baf1
MM
15244 else
15245 {
5dae1114
MM
15246 type = TREE_TYPE (type_decl);
15247 if (TREE_CODE (type) == TYPENAME_TYPE)
14d22dd6 15248 {
21526606 15249 type = resolve_typename_type (type,
14d22dd6
MM
15250 /*only_current_p=*/false);
15251 if (type == error_mark_node)
15252 {
15253 cp_parser_abort_tentative_parse (parser);
15254 return false;
15255 }
15256 }
4514aa8c 15257 pushed_scope = push_scope (type);
a723baf1 15258 }
4047b164
KL
15259
15260 /* Inside the constructor parameter list, surrounding
15261 template-parameter-lists do not apply. */
15262 saved_num_template_parameter_lists
15263 = parser->num_template_parameter_lists;
15264 parser->num_template_parameter_lists = 0;
15265
5dae1114
MM
15266 /* Look for the type-specifier. */
15267 cp_parser_type_specifier (parser,
15268 CP_PARSER_FLAGS_NONE,
62d1db17 15269 /*decl_specs=*/NULL,
5dae1114
MM
15270 /*is_declarator=*/true,
15271 /*declares_class_or_enum=*/NULL,
15272 /*is_cv_qualifier=*/NULL);
4047b164
KL
15273
15274 parser->num_template_parameter_lists
15275 = saved_num_template_parameter_lists;
15276
5dae1114 15277 /* Leave the scope of the class. */
4514aa8c
NS
15278 if (pushed_scope)
15279 pop_scope (pushed_scope);
5dae1114
MM
15280
15281 constructor_p = !cp_parser_error_occurred (parser);
a723baf1
MM
15282 }
15283 }
15284 else
15285 constructor_p = false;
15286 /* We did not really want to consume any tokens. */
15287 cp_parser_abort_tentative_parse (parser);
15288
15289 return constructor_p;
15290}
15291
15292/* Parse the definition of the function given by the DECL_SPECIFIERS,
cf22909c 15293 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
a723baf1
MM
15294 they must be performed once we are in the scope of the function.
15295
15296 Returns the function defined. */
15297
15298static tree
15299cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab 15300 (cp_parser* parser,
62d1db17 15301 cp_decl_specifier_seq *decl_specifiers,
94edc4ab 15302 tree attributes,
058b15c1 15303 const cp_declarator *declarator)
a723baf1
MM
15304{
15305 tree fn;
15306 bool success_p;
15307
15308 /* Begin the function-definition. */
058b15c1
MM
15309 success_p = start_function (decl_specifiers, declarator, attributes);
15310
15311 /* The things we're about to see are not directly qualified by any
15312 template headers we've seen thus far. */
15313 reset_specialization ();
a723baf1
MM
15314
15315 /* If there were names looked up in the decl-specifier-seq that we
15316 did not check, check them now. We must wait until we are in the
15317 scope of the function to perform the checks, since the function
15318 might be a friend. */
cf22909c 15319 perform_deferred_access_checks ();
a723baf1
MM
15320
15321 if (!success_p)
15322 {
058b15c1 15323 /* Skip the entire function. */
a723baf1
MM
15324 cp_parser_skip_to_end_of_block_or_statement (parser);
15325 fn = error_mark_node;
15326 }
15327 else
15328 fn = cp_parser_function_definition_after_declarator (parser,
15329 /*inline_p=*/false);
15330
15331 return fn;
15332}
15333
15334/* Parse the part of a function-definition that follows the
15335 declarator. INLINE_P is TRUE iff this function is an inline
15336 function defined with a class-specifier.
15337
15338 Returns the function defined. */
15339
21526606
EC
15340static tree
15341cp_parser_function_definition_after_declarator (cp_parser* parser,
94edc4ab 15342 bool inline_p)
a723baf1
MM
15343{
15344 tree fn;
15345 bool ctor_initializer_p = false;
15346 bool saved_in_unbraced_linkage_specification_p;
15347 unsigned saved_num_template_parameter_lists;
15348
15349 /* If the next token is `return', then the code may be trying to
15350 make use of the "named return value" extension that G++ used to
15351 support. */
15352 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15353 {
15354 /* Consume the `return' keyword. */
15355 cp_lexer_consume_token (parser->lexer);
15356 /* Look for the identifier that indicates what value is to be
15357 returned. */
15358 cp_parser_identifier (parser);
15359 /* Issue an error message. */
15360 error ("named return values are no longer supported");
15361 /* Skip tokens until we reach the start of the function body. */
bc4071dd
RH
15362 while (true)
15363 {
15364 cp_token *token = cp_lexer_peek_token (parser->lexer);
15365 if (token->type == CPP_OPEN_BRACE
15366 || token->type == CPP_EOF
15367 || token->type == CPP_PRAGMA_EOL)
15368 break;
15369 cp_lexer_consume_token (parser->lexer);
15370 }
a723baf1
MM
15371 }
15372 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15373 anything declared inside `f'. */
21526606 15374 saved_in_unbraced_linkage_specification_p
a723baf1
MM
15375 = parser->in_unbraced_linkage_specification_p;
15376 parser->in_unbraced_linkage_specification_p = false;
15377 /* Inside the function, surrounding template-parameter-lists do not
15378 apply. */
21526606
EC
15379 saved_num_template_parameter_lists
15380 = parser->num_template_parameter_lists;
a723baf1
MM
15381 parser->num_template_parameter_lists = 0;
15382 /* If the next token is `try', then we are looking at a
15383 function-try-block. */
15384 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15385 ctor_initializer_p = cp_parser_function_try_block (parser);
15386 /* A function-try-block includes the function-body, so we only do
15387 this next part if we're not processing a function-try-block. */
15388 else
21526606 15389 ctor_initializer_p
a723baf1
MM
15390 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15391
15392 /* Finish the function. */
21526606 15393 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
a723baf1
MM
15394 (inline_p ? 2 : 0));
15395 /* Generate code for it, if necessary. */
8cd2462c 15396 expand_or_defer_fn (fn);
a723baf1 15397 /* Restore the saved values. */
21526606 15398 parser->in_unbraced_linkage_specification_p
a723baf1 15399 = saved_in_unbraced_linkage_specification_p;
21526606 15400 parser->num_template_parameter_lists
a723baf1
MM
15401 = saved_num_template_parameter_lists;
15402
15403 return fn;
15404}
15405
15406/* Parse a template-declaration, assuming that the `export' (and
15407 `extern') keywords, if present, has already been scanned. MEMBER_P
15408 is as for cp_parser_template_declaration. */
15409
15410static void
94edc4ab 15411cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
a723baf1
MM
15412{
15413 tree decl = NULL_TREE;
15414 tree parameter_list;
15415 bool friend_p = false;
2f1b1731 15416 bool need_lang_pop;
a723baf1
MM
15417
15418 /* Look for the `template' keyword. */
15419 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15420 return;
21526606 15421
a723baf1
MM
15422 /* And the `<'. */
15423 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15424 return;
2f1b1731
MM
15425 /* [temp]
15426
15427 A template ... shall not have C linkage. */
15428 if (current_lang_name == lang_name_c)
15429 {
15430 error ("template with C linkage");
15431 /* Give it C++ linkage to avoid confusing other parts of the
15432 front end. */
15433 push_lang_context (lang_name_cplusplus);
15434 need_lang_pop = true;
15435 }
15436 else
15437 need_lang_pop = false;
a723baf1
MM
15438 /* If the next token is `>', then we have an invalid
15439 specialization. Rather than complain about an invalid template
15440 parameter, issue an error message here. */
15441 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15442 {
15443 cp_parser_error (parser, "invalid explicit specialization");
2f9afd51 15444 begin_specialization ();
a723baf1
MM
15445 parameter_list = NULL_TREE;
15446 }
15447 else
357d956e
MM
15448 /* Parse the template parameters. */
15449 parameter_list = cp_parser_template_parameter_list (parser);
2f9afd51 15450
a723baf1
MM
15451 /* Look for the `>'. */
15452 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15453 /* We just processed one more parameter list. */
15454 ++parser->num_template_parameter_lists;
15455 /* If the next token is `template', there are more template
15456 parameters. */
21526606 15457 if (cp_lexer_next_token_is_keyword (parser->lexer,
a723baf1
MM
15458 RID_TEMPLATE))
15459 cp_parser_template_declaration_after_export (parser, member_p);
15460 else
15461 {
fe88415f 15462 /* There are no access checks when parsing a template, as we do not
0cbd7506 15463 know if a specialization will be a friend. */
fe88415f 15464 push_deferring_access_checks (dk_no_check);
98ca843c 15465
a723baf1
MM
15466 decl = cp_parser_single_declaration (parser,
15467 member_p,
15468 &friend_p);
15469
fe88415f 15470 pop_deferring_access_checks ();
98ca843c 15471
a723baf1
MM
15472 /* If this is a member template declaration, let the front
15473 end know. */
15474 if (member_p && !friend_p && decl)
37d407a1
KL
15475 {
15476 if (TREE_CODE (decl) == TYPE_DECL)
15477 cp_parser_check_access_in_redeclaration (decl);
15478
15479 decl = finish_member_template_decl (decl);
15480 }
a723baf1 15481 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19db77ce
KL
15482 make_friend_class (current_class_type, TREE_TYPE (decl),
15483 /*complain=*/true);
a723baf1
MM
15484 }
15485 /* We are done with the current parameter list. */
15486 --parser->num_template_parameter_lists;
15487
15488 /* Finish up. */
15489 finish_template_decl (parameter_list);
15490
15491 /* Register member declarations. */
15492 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15493 finish_member_declaration (decl);
2f1b1731
MM
15494 /* For the erroneous case of a template with C linkage, we pushed an
15495 implicit C++ linkage scope; exit that scope now. */
15496 if (need_lang_pop)
15497 pop_lang_context ();
a723baf1
MM
15498 /* If DECL is a function template, we must return to parse it later.
15499 (Even though there is no definition, there might be default
15500 arguments that need handling.) */
21526606 15501 if (member_p && decl
a723baf1
MM
15502 && (TREE_CODE (decl) == FUNCTION_DECL
15503 || DECL_FUNCTION_TEMPLATE_P (decl)))
15504 TREE_VALUE (parser->unparsed_functions_queues)
21526606 15505 = tree_cons (NULL_TREE, decl,
a723baf1
MM
15506 TREE_VALUE (parser->unparsed_functions_queues));
15507}
15508
15509/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15510 `function-definition' sequence. MEMBER_P is true, this declaration
15511 appears in a class scope.
15512
15513 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15514 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15515
15516static tree
21526606 15517cp_parser_single_declaration (cp_parser* parser,
94edc4ab
NN
15518 bool member_p,
15519 bool* friend_p)
a723baf1 15520{
560ad596 15521 int declares_class_or_enum;
a723baf1 15522 tree decl = NULL_TREE;
62d1db17 15523 cp_decl_specifier_seq decl_specifiers;
4bb8ca28 15524 bool function_definition_p = false;
a723baf1 15525
71bd7186
MM
15526 /* This function is only used when processing a template
15527 declaration. */
15528 gcc_assert (innermost_scope_kind () == sk_template_parms
15529 || innermost_scope_kind () == sk_template_spec);
15530
a723baf1 15531 /* Defer access checks until we know what is being declared. */
8d241e0b 15532 push_deferring_access_checks (dk_deferred);
cf22909c 15533
a723baf1
MM
15534 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15535 alternative. */
62d1db17
MM
15536 cp_parser_decl_specifier_seq (parser,
15537 CP_PARSER_FLAGS_OPTIONAL,
15538 &decl_specifiers,
15539 &declares_class_or_enum);
4bb8ca28 15540 if (friend_p)
62d1db17 15541 *friend_p = cp_parser_friend_p (&decl_specifiers);
71bd7186
MM
15542
15543 /* There are no template typedefs. */
15544 if (decl_specifiers.specs[(int) ds_typedef])
15545 {
15546 error ("template declaration of %qs", "typedef");
15547 decl = error_mark_node;
15548 }
15549
a723baf1
MM
15550 /* Gather up the access checks that occurred the
15551 decl-specifier-seq. */
cf22909c
KL
15552 stop_deferring_access_checks ();
15553
a723baf1
MM
15554 /* Check for the declaration of a template class. */
15555 if (declares_class_or_enum)
15556 {
15557 if (cp_parser_declares_only_class_p (parser))
15558 {
62d1db17 15559 decl = shadow_tag (&decl_specifiers);
b939a023
KL
15560
15561 /* In this case:
15562
15563 struct C {
15564 friend template <typename T> struct A<T>::B;
15565 };
15566
15567 A<T>::B will be represented by a TYPENAME_TYPE, and
15568 therefore not recognized by shadow_tag. */
15569 if (friend_p && *friend_p
15570 && !decl
15571 && decl_specifiers.type
15572 && TYPE_P (decl_specifiers.type))
15573 decl = decl_specifiers.type;
15574
62d1db17 15575 if (decl && decl != error_mark_node)
a723baf1
MM
15576 decl = TYPE_NAME (decl);
15577 else
15578 decl = error_mark_node;
15579 }
15580 }
a723baf1
MM
15581 /* If it's not a template class, try for a template function. If
15582 the next token is a `;', then this declaration does not declare
15583 anything. But, if there were errors in the decl-specifiers, then
15584 the error might well have come from an attempted class-specifier.
15585 In that case, there's no need to warn about a missing declarator. */
15586 if (!decl
15587 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
62d1db17 15588 || decl_specifiers.type != error_mark_node))
21526606 15589 decl = cp_parser_init_declarator (parser,
62d1db17 15590 &decl_specifiers,
4bb8ca28 15591 /*function_definition_allowed_p=*/true,
a723baf1 15592 member_p,
560ad596 15593 declares_class_or_enum,
4bb8ca28 15594 &function_definition_p);
cf22909c
KL
15595
15596 pop_deferring_access_checks ();
15597
a723baf1
MM
15598 /* Clear any current qualification; whatever comes next is the start
15599 of something new. */
15600 parser->scope = NULL_TREE;
15601 parser->qualifying_scope = NULL_TREE;
15602 parser->object_scope = NULL_TREE;
15603 /* Look for a trailing `;' after the declaration. */
4bb8ca28 15604 if (!function_definition_p
71bd7186
MM
15605 && (decl == error_mark_node
15606 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
a723baf1 15607 cp_parser_skip_to_end_of_block_or_statement (parser);
a723baf1
MM
15608
15609 return decl;
15610}
15611
d6b4ea85
MM
15612/* Parse a cast-expression that is not the operand of a unary "&". */
15613
15614static tree
15615cp_parser_simple_cast_expression (cp_parser *parser)
15616{
93678513
MM
15617 return cp_parser_cast_expression (parser, /*address_p=*/false,
15618 /*cast_p=*/false);
d6b4ea85
MM
15619}
15620
a723baf1
MM
15621/* Parse a functional cast to TYPE. Returns an expression
15622 representing the cast. */
15623
15624static tree
94edc4ab 15625cp_parser_functional_cast (cp_parser* parser, tree type)
a723baf1
MM
15626{
15627 tree expression_list;
d36d5600 15628 tree cast;
a723baf1 15629
21526606 15630 expression_list
39703eb9 15631 = cp_parser_parenthesized_expression_list (parser, false,
93678513 15632 /*cast_p=*/true,
39703eb9 15633 /*non_constant_p=*/NULL);
a723baf1 15634
d36d5600
GB
15635 cast = build_functional_cast (type, expression_list);
15636 /* [expr.const]/1: In an integral constant expression "only type
15637 conversions to integral or enumeration type can be used". */
3ce5fa4f
NS
15638 if (TREE_CODE (type) == TYPE_DECL)
15639 type = TREE_TYPE (type);
15640 if (cast != error_mark_node && !dependent_type_p (type)
15641 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
d36d5600 15642 {
98ca843c 15643 if (cp_parser_non_integral_constant_expression
d36d5600
GB
15644 (parser, "a call to a constructor"))
15645 return error_mark_node;
15646 }
15647 return cast;
a723baf1
MM
15648}
15649
4bb8ca28
MM
15650/* Save the tokens that make up the body of a member function defined
15651 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15652 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15653 specifiers applied to the declaration. Returns the FUNCTION_DECL
15654 for the member function. */
15655
7ce27103 15656static tree
4bb8ca28 15657cp_parser_save_member_function_body (cp_parser* parser,
62d1db17 15658 cp_decl_specifier_seq *decl_specifiers,
058b15c1 15659 cp_declarator *declarator,
4bb8ca28
MM
15660 tree attributes)
15661{
c162c75e
MA
15662 cp_token *first;
15663 cp_token *last;
4bb8ca28
MM
15664 tree fn;
15665
15666 /* Create the function-declaration. */
15667 fn = start_method (decl_specifiers, declarator, attributes);
15668 /* If something went badly wrong, bail out now. */
15669 if (fn == error_mark_node)
15670 {
15671 /* If there's a function-body, skip it. */
21526606 15672 if (cp_parser_token_starts_function_definition_p
4bb8ca28
MM
15673 (cp_lexer_peek_token (parser->lexer)))
15674 cp_parser_skip_to_end_of_block_or_statement (parser);
15675 return error_mark_node;
15676 }
15677
15678 /* Remember it, if there default args to post process. */
15679 cp_parser_save_default_args (parser, fn);
15680
21526606 15681 /* Save away the tokens that make up the body of the
4bb8ca28 15682 function. */
c162c75e
MA
15683 first = parser->lexer->next_token;
15684 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
4bb8ca28
MM
15685 /* Handle function try blocks. */
15686 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
c162c75e
MA
15687 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15688 last = parser->lexer->next_token;
4bb8ca28
MM
15689
15690 /* Save away the inline definition; we will process it when the
15691 class is complete. */
c162c75e 15692 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
4bb8ca28
MM
15693 DECL_PENDING_INLINE_P (fn) = 1;
15694
15695 /* We need to know that this was defined in the class, so that
15696 friend templates are handled correctly. */
15697 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15698
15699 /* We're done with the inline definition. */
15700 finish_method (fn);
15701
15702 /* Add FN to the queue of functions to be parsed later. */
15703 TREE_VALUE (parser->unparsed_functions_queues)
21526606 15704 = tree_cons (NULL_TREE, fn,
4bb8ca28
MM
15705 TREE_VALUE (parser->unparsed_functions_queues));
15706
15707 return fn;
15708}
15709
ec75414f
MM
15710/* Parse a template-argument-list, as well as the trailing ">" (but
15711 not the opening ">"). See cp_parser_template_argument_list for the
15712 return value. */
15713
15714static tree
15715cp_parser_enclosed_template_argument_list (cp_parser* parser)
15716{
15717 tree arguments;
15718 tree saved_scope;
15719 tree saved_qualifying_scope;
15720 tree saved_object_scope;
15721 bool saved_greater_than_is_operator_p;
49f210a2 15722 bool saved_skip_evaluation;
ec75414f
MM
15723
15724 /* [temp.names]
15725
15726 When parsing a template-id, the first non-nested `>' is taken as
15727 the end of the template-argument-list rather than a greater-than
15728 operator. */
21526606 15729 saved_greater_than_is_operator_p
ec75414f
MM
15730 = parser->greater_than_is_operator_p;
15731 parser->greater_than_is_operator_p = false;
15732 /* Parsing the argument list may modify SCOPE, so we save it
15733 here. */
15734 saved_scope = parser->scope;
15735 saved_qualifying_scope = parser->qualifying_scope;
15736 saved_object_scope = parser->object_scope;
49f210a2
MM
15737 /* We need to evaluate the template arguments, even though this
15738 template-id may be nested within a "sizeof". */
15739 saved_skip_evaluation = skip_evaluation;
15740 skip_evaluation = false;
ec75414f
MM
15741 /* Parse the template-argument-list itself. */
15742 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15743 arguments = NULL_TREE;
15744 else
15745 arguments = cp_parser_template_argument_list (parser);
4d5297fa
GB
15746 /* Look for the `>' that ends the template-argument-list. If we find
15747 a '>>' instead, it's probably just a typo. */
15748 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15749 {
15750 if (!saved_greater_than_is_operator_p)
15751 {
2cfe82fe
ZW
15752 /* If we're in a nested template argument list, the '>>' has
15753 to be a typo for '> >'. We emit the error message, but we
15754 continue parsing and we push a '>' as next token, so that
15755 the argument list will be parsed correctly. Note that the
15756 global source location is still on the token before the
15757 '>>', so we need to say explicitly where we want it. */
15758 cp_token *token = cp_lexer_peek_token (parser->lexer);
15759 error ("%H%<>>%> should be %<> >%> "
15760 "within a nested template argument list",
15761 &token->location);
15762
15763 /* ??? Proper recovery should terminate two levels of
15764 template argument list here. */
4d5297fa
GB
15765 token->type = CPP_GREATER;
15766 }
15767 else
15768 {
2cfe82fe
ZW
15769 /* If this is not a nested template argument list, the '>>'
15770 is a typo for '>'. Emit an error message and continue.
15771 Same deal about the token location, but here we can get it
15772 right by consuming the '>>' before issuing the diagnostic. */
4d5297fa 15773 cp_lexer_consume_token (parser->lexer);
2cfe82fe
ZW
15774 error ("spurious %<>>%>, use %<>%> to terminate "
15775 "a template argument list");
4d5297fa
GB
15776 }
15777 }
2cfe82fe 15778 else
88a33c34 15779 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
ec75414f 15780 /* The `>' token might be a greater-than operator again now. */
21526606 15781 parser->greater_than_is_operator_p
ec75414f
MM
15782 = saved_greater_than_is_operator_p;
15783 /* Restore the SAVED_SCOPE. */
15784 parser->scope = saved_scope;
15785 parser->qualifying_scope = saved_qualifying_scope;
15786 parser->object_scope = saved_object_scope;
49f210a2 15787 skip_evaluation = saved_skip_evaluation;
ec75414f
MM
15788
15789 return arguments;
15790}
15791
a723baf1
MM
15792/* MEMBER_FUNCTION is a member function, or a friend. If default
15793 arguments, or the body of the function have not yet been parsed,
15794 parse them now. */
15795
15796static void
94edc4ab 15797cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
a723baf1 15798{
a723baf1
MM
15799 /* If this member is a template, get the underlying
15800 FUNCTION_DECL. */
15801 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15802 member_function = DECL_TEMPLATE_RESULT (member_function);
15803
15804 /* There should not be any class definitions in progress at this
15805 point; the bodies of members are only parsed outside of all class
15806 definitions. */
50bc768d 15807 gcc_assert (parser->num_classes_being_defined == 0);
a723baf1
MM
15808 /* While we're parsing the member functions we might encounter more
15809 classes. We want to handle them right away, but we don't want
15810 them getting mixed up with functions that are currently in the
15811 queue. */
15812 parser->unparsed_functions_queues
15813 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15814
15815 /* Make sure that any template parameters are in scope. */
15816 maybe_begin_member_template_processing (member_function);
15817
a723baf1
MM
15818 /* If the body of the function has not yet been parsed, parse it
15819 now. */
15820 if (DECL_PENDING_INLINE_P (member_function))
15821 {
15822 tree function_scope;
15823 cp_token_cache *tokens;
15824
15825 /* The function is no longer pending; we are processing it. */
15826 tokens = DECL_PENDING_INLINE_INFO (member_function);
15827 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15828 DECL_PENDING_INLINE_P (member_function) = 0;
c8094d83 15829
f769035f
NS
15830 /* If this is a local class, enter the scope of the containing
15831 function. */
15832 function_scope = current_function_decl;
a723baf1
MM
15833 if (function_scope)
15834 push_function_context_to (function_scope);
21526606 15835
c8094d83 15836
2cfe82fe
ZW
15837 /* Push the body of the function onto the lexer stack. */
15838 cp_parser_push_lexer_for_tokens (parser, tokens);
21526606 15839
a723baf1
MM
15840 /* Let the front end know that we going to be defining this
15841 function. */
058b15c1
MM
15842 start_preparsed_function (member_function, NULL_TREE,
15843 SF_PRE_PARSED | SF_INCLASS_INLINE);
21526606 15844
2d637547
NS
15845 /* Don't do access checking if it is a templated function. */
15846 if (processing_template_decl)
15847 push_deferring_access_checks (dk_no_check);
c8094d83 15848
a723baf1
MM
15849 /* Now, parse the body of the function. */
15850 cp_parser_function_definition_after_declarator (parser,
15851 /*inline_p=*/true);
21526606 15852
2d637547
NS
15853 if (processing_template_decl)
15854 pop_deferring_access_checks ();
c8094d83 15855
a723baf1
MM
15856 /* Leave the scope of the containing function. */
15857 if (function_scope)
15858 pop_function_context_from (function_scope);
2cfe82fe 15859 cp_parser_pop_lexer (parser);
a723baf1
MM
15860 }
15861
15862 /* Remove any template parameters from the symbol table. */
15863 maybe_end_member_template_processing ();
15864
15865 /* Restore the queue. */
21526606 15866 parser->unparsed_functions_queues
a723baf1
MM
15867 = TREE_CHAIN (parser->unparsed_functions_queues);
15868}
15869
cd0be382 15870/* If DECL contains any default args, remember it on the unparsed
8db1028e
NS
15871 functions queue. */
15872
15873static void
15874cp_parser_save_default_args (cp_parser* parser, tree decl)
15875{
15876 tree probe;
15877
15878 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15879 probe;
15880 probe = TREE_CHAIN (probe))
15881 if (TREE_PURPOSE (probe))
15882 {
15883 TREE_PURPOSE (parser->unparsed_functions_queues)
f44b0c8e 15884 = tree_cons (current_class_type, decl,
8db1028e
NS
15885 TREE_PURPOSE (parser->unparsed_functions_queues));
15886 break;
15887 }
8db1028e
NS
15888}
15889
8218bd34 15890/* FN is a FUNCTION_DECL which may contains a parameter with an
f44b0c8e
MM
15891 unparsed DEFAULT_ARG. Parse the default args now. This function
15892 assumes that the current scope is the scope in which the default
15893 argument should be processed. */
a723baf1
MM
15894
15895static void
8218bd34 15896cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
a723baf1 15897{
a723baf1 15898 bool saved_local_variables_forbidden_p;
2cfe82fe 15899 tree parm;
8218bd34 15900
b92bc2a0
NS
15901 /* While we're parsing the default args, we might (due to the
15902 statement expression extension) encounter more classes. We want
15903 to handle them right away, but we don't want them getting mixed
15904 up with default args that are currently in the queue. */
15905 parser->unparsed_functions_queues
15906 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15907
2cfe82fe
ZW
15908 /* Local variable names (and the `this' keyword) may not appear
15909 in a default argument. */
15910 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15911 parser->local_variables_forbidden_p = true;
15912
15913 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15914 parm;
15915 parm = TREE_CHAIN (parm))
a723baf1 15916 {
2cfe82fe 15917 cp_token_cache *tokens;
5e97d404
NS
15918 tree default_arg = TREE_PURPOSE (parm);
15919 tree parsed_arg;
01ea1ea8
NS
15920 VEC(tree,gc) *insts;
15921 tree copy;
15922 unsigned ix;
c8094d83 15923
5e97d404 15924 if (!default_arg)
2cfe82fe 15925 continue;
a723baf1 15926
efb169b0
NS
15927 if (TREE_CODE (default_arg) != DEFAULT_ARG)
15928 /* This can happen for a friend declaration for a function
15929 already declared with default arguments. */
15930 continue;
5e97d404 15931
2cfe82fe
ZW
15932 /* Push the saved tokens for the default argument onto the parser's
15933 lexer stack. */
5e97d404 15934 tokens = DEFARG_TOKENS (default_arg);
2cfe82fe 15935 cp_parser_push_lexer_for_tokens (parser, tokens);
a723baf1 15936
2cfe82fe 15937 /* Parse the assignment-expression. */
5e97d404
NS
15938 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15939
c3ee4651
NS
15940 if (!processing_template_decl)
15941 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
15942
5e97d404
NS
15943 TREE_PURPOSE (parm) = parsed_arg;
15944
15945 /* Update any instantiations we've already created. */
01ea1ea8
NS
15946 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15947 VEC_iterate (tree, insts, ix, copy); ix++)
15948 TREE_PURPOSE (copy) = parsed_arg;
a723baf1 15949
676e33ca
MM
15950 /* If the token stream has not been completely used up, then
15951 there was extra junk after the end of the default
15952 argument. */
15953 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2a13a625 15954 cp_parser_error (parser, "expected %<,%>");
676e33ca 15955
2cfe82fe
ZW
15956 /* Revert to the main lexer. */
15957 cp_parser_pop_lexer (parser);
a723baf1 15958 }
b92bc2a0 15959
607c855e
VR
15960 /* Make sure no default arg is missing. */
15961 check_default_args (fn);
15962
2cfe82fe
ZW
15963 /* Restore the state of local_variables_forbidden_p. */
15964 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15965
b92bc2a0 15966 /* Restore the queue. */
21526606 15967 parser->unparsed_functions_queues
b92bc2a0 15968 = TREE_CHAIN (parser->unparsed_functions_queues);
a723baf1
MM
15969}
15970
15971/* Parse the operand of `sizeof' (or a similar operator). Returns
15972 either a TYPE or an expression, depending on the form of the
15973 input. The KEYWORD indicates which kind of expression we have
15974 encountered. */
15975
15976static tree
94edc4ab 15977cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
a723baf1
MM
15978{
15979 static const char *format;
15980 tree expr = NULL_TREE;
15981 const char *saved_message;
67c03833 15982 bool saved_integral_constant_expression_p;
93678513 15983 bool saved_non_integral_constant_expression_p;
a723baf1
MM
15984
15985 /* Initialize FORMAT the first time we get here. */
15986 if (!format)
9e637a26 15987 format = "types may not be defined in '%s' expressions";
a723baf1
MM
15988
15989 /* Types cannot be defined in a `sizeof' expression. Save away the
15990 old message. */
15991 saved_message = parser->type_definition_forbidden_message;
15992 /* And create the new one. */
21526606 15993 parser->type_definition_forbidden_message
0ac1b889 15994 = XNEWVEC (const char, strlen (format)
c68b0a84
KG
15995 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15996 + 1 /* `\0' */);
a723baf1
MM
15997 sprintf ((char *) parser->type_definition_forbidden_message,
15998 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15999
16000 /* The restrictions on constant-expressions do not apply inside
16001 sizeof expressions. */
c8094d83 16002 saved_integral_constant_expression_p
93678513
MM
16003 = parser->integral_constant_expression_p;
16004 saved_non_integral_constant_expression_p
16005 = parser->non_integral_constant_expression_p;
67c03833 16006 parser->integral_constant_expression_p = false;
a723baf1 16007
3beb3abf
MM
16008 /* Do not actually evaluate the expression. */
16009 ++skip_evaluation;
a723baf1
MM
16010 /* If it's a `(', then we might be looking at the type-id
16011 construction. */
16012 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16013 {
16014 tree type;
4f8163b1 16015 bool saved_in_type_id_in_expr_p;
a723baf1
MM
16016
16017 /* We can't be sure yet whether we're looking at a type-id or an
16018 expression. */
16019 cp_parser_parse_tentatively (parser);
16020 /* Consume the `('. */
16021 cp_lexer_consume_token (parser->lexer);
16022 /* Parse the type-id. */
4f8163b1
MM
16023 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16024 parser->in_type_id_in_expr_p = true;
a723baf1 16025 type = cp_parser_type_id (parser);
4f8163b1 16026 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1 16027 /* Now, look for the trailing `)'. */
9e637a26 16028 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
a723baf1
MM
16029 /* If all went well, then we're done. */
16030 if (cp_parser_parse_definitely (parser))
16031 {
62d1db17
MM
16032 cp_decl_specifier_seq decl_specs;
16033
16034 /* Build a trivial decl-specifier-seq. */
16035 clear_decl_specs (&decl_specs);
16036 decl_specs.type = type;
a723baf1
MM
16037
16038 /* Call grokdeclarator to figure out what type this is. */
058b15c1 16039 expr = grokdeclarator (NULL,
62d1db17 16040 &decl_specs,
a723baf1
MM
16041 TYPENAME,
16042 /*initialized=*/0,
16043 /*attrlist=*/NULL);
16044 }
16045 }
16046
16047 /* If the type-id production did not work out, then we must be
16048 looking at the unary-expression production. */
16049 if (!expr)
93678513
MM
16050 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16051 /*cast_p=*/false);
3beb3abf
MM
16052 /* Go back to evaluating expressions. */
16053 --skip_evaluation;
a723baf1
MM
16054
16055 /* Free the message we created. */
16056 free ((char *) parser->type_definition_forbidden_message);
16057 /* And restore the old one. */
16058 parser->type_definition_forbidden_message = saved_message;
c8094d83 16059 parser->integral_constant_expression_p
93678513
MM
16060 = saved_integral_constant_expression_p;
16061 parser->non_integral_constant_expression_p
16062 = saved_non_integral_constant_expression_p;
a723baf1
MM
16063
16064 return expr;
16065}
16066
16067/* If the current declaration has no declarator, return true. */
16068
16069static bool
16070cp_parser_declares_only_class_p (cp_parser *parser)
16071{
21526606 16072 /* If the next token is a `;' or a `,' then there is no
a723baf1
MM
16073 declarator. */
16074 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16075 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16076}
16077
62d1db17 16078/* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
a723baf1 16079
62d1db17
MM
16080static void
16081cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
16082 cp_storage_class storage_class)
a723baf1 16083{
62d1db17
MM
16084 if (decl_specs->storage_class != sc_none)
16085 decl_specs->multiple_storage_classes_p = true;
16086 else
16087 decl_specs->storage_class = storage_class;
16088}
16089
16090/* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16091 is true, the type is a user-defined type; otherwise it is a
16092 built-in type specified by a keyword. */
a723baf1 16093
62d1db17
MM
16094static void
16095cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16096 tree type_spec,
16097 bool user_defined_p)
16098{
16099 decl_specs->any_specifiers_p = true;
98ca843c 16100
9306cccb
MM
16101 /* If the user tries to redeclare bool or wchar_t (with, for
16102 example, in "typedef int wchar_t;") we remember that this is what
f84b6c96
MM
16103 happened. In system headers, we ignore these declarations so
16104 that G++ can work with system headers that are not C++-safe. */
98ca843c 16105 if (decl_specs->specs[(int) ds_typedef]
f84b6c96 16106 && !user_defined_p
9306cccb
MM
16107 && (type_spec == boolean_type_node
16108 || type_spec == wchar_type_node)
f84b6c96
MM
16109 && (decl_specs->type
16110 || decl_specs->specs[(int) ds_long]
16111 || decl_specs->specs[(int) ds_short]
16112 || decl_specs->specs[(int) ds_unsigned]
16113 || decl_specs->specs[(int) ds_signed]))
0a73e37f
MM
16114 {
16115 decl_specs->redefined_builtin_type = type_spec;
16116 if (!decl_specs->type)
16117 {
16118 decl_specs->type = type_spec;
16119 decl_specs->user_defined_type_p = false;
16120 }
16121 }
f84b6c96
MM
16122 else if (decl_specs->type)
16123 decl_specs->multiple_types_p = true;
62d1db17
MM
16124 else
16125 {
16126 decl_specs->type = type_spec;
16127 decl_specs->user_defined_type_p = user_defined_p;
0a73e37f 16128 decl_specs->redefined_builtin_type = NULL_TREE;
a723baf1 16129 }
62d1db17 16130}
a723baf1 16131
62d1db17
MM
16132/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16133 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16134
16135static bool
16136cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16137{
16138 return decl_specifiers->specs[(int) ds_friend] != 0;
a723baf1
MM
16139}
16140
16141/* If the next token is of the indicated TYPE, consume it. Otherwise,
16142 issue an error message indicating that TOKEN_DESC was expected.
21526606 16143
a723baf1
MM
16144 Returns the token consumed, if the token had the appropriate type.
16145 Otherwise, returns NULL. */
16146
16147static cp_token *
94edc4ab 16148cp_parser_require (cp_parser* parser,
0cbd7506
MS
16149 enum cpp_ttype type,
16150 const char* token_desc)
a723baf1
MM
16151{
16152 if (cp_lexer_next_token_is (parser->lexer, type))
16153 return cp_lexer_consume_token (parser->lexer);
16154 else
16155 {
e5976695
MM
16156 /* Output the MESSAGE -- unless we're parsing tentatively. */
16157 if (!cp_parser_simulate_error (parser))
216bb6e1
MM
16158 {
16159 char *message = concat ("expected ", token_desc, NULL);
16160 cp_parser_error (parser, message);
16161 free (message);
16162 }
a723baf1
MM
16163 return NULL;
16164 }
16165}
16166
16167/* Like cp_parser_require, except that tokens will be skipped until
16168 the desired token is found. An error message is still produced if
16169 the next token is not as expected. */
16170
16171static void
21526606 16172cp_parser_skip_until_found (cp_parser* parser,
0cbd7506
MS
16173 enum cpp_ttype type,
16174 const char* token_desc)
a723baf1
MM
16175{
16176 cp_token *token;
16177 unsigned nesting_depth = 0;
16178
16179 if (cp_parser_require (parser, type, token_desc))
16180 return;
16181
16182 /* Skip tokens until the desired token is found. */
16183 while (true)
16184 {
16185 /* Peek at the next token. */
16186 token = cp_lexer_peek_token (parser->lexer);
bc4071dd
RH
16187
16188 /* If we've reached the token we want, consume it and stop. */
a723baf1
MM
16189 if (token->type == type && !nesting_depth)
16190 {
16191 cp_lexer_consume_token (parser->lexer);
16192 return;
16193 }
bc4071dd
RH
16194
16195 switch (token->type)
a723baf1 16196 {
bc4071dd
RH
16197 case CPP_EOF:
16198 case CPP_PRAGMA_EOL:
16199 /* If we've run out of tokens, stop. */
16200 return;
16201
16202 case CPP_OPEN_BRACE:
16203 case CPP_OPEN_PAREN:
16204 case CPP_OPEN_SQUARE:
16205 ++nesting_depth;
16206 break;
16207
16208 case CPP_CLOSE_BRACE:
16209 case CPP_CLOSE_PAREN:
16210 case CPP_CLOSE_SQUARE:
a723baf1
MM
16211 if (nesting_depth-- == 0)
16212 return;
bc4071dd
RH
16213 break;
16214
16215 default:
16216 break;
a723baf1 16217 }
bc4071dd 16218
a723baf1
MM
16219 /* Consume this token. */
16220 cp_lexer_consume_token (parser->lexer);
16221 }
16222}
16223
16224/* If the next token is the indicated keyword, consume it. Otherwise,
16225 issue an error message indicating that TOKEN_DESC was expected.
21526606 16226
a723baf1
MM
16227 Returns the token consumed, if the token had the appropriate type.
16228 Otherwise, returns NULL. */
16229
16230static cp_token *
94edc4ab 16231cp_parser_require_keyword (cp_parser* parser,
0cbd7506
MS
16232 enum rid keyword,
16233 const char* token_desc)
a723baf1
MM
16234{
16235 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16236
16237 if (token && token->keyword != keyword)
16238 {
16239 dyn_string_t error_msg;
16240
16241 /* Format the error message. */
16242 error_msg = dyn_string_new (0);
16243 dyn_string_append_cstr (error_msg, "expected ");
16244 dyn_string_append_cstr (error_msg, token_desc);
16245 cp_parser_error (parser, error_msg->s);
16246 dyn_string_delete (error_msg);
16247 return NULL;
16248 }
16249
16250 return token;
16251}
16252
16253/* Returns TRUE iff TOKEN is a token that can begin the body of a
16254 function-definition. */
16255
21526606 16256static bool
94edc4ab 16257cp_parser_token_starts_function_definition_p (cp_token* token)
a723baf1
MM
16258{
16259 return (/* An ordinary function-body begins with an `{'. */
16260 token->type == CPP_OPEN_BRACE
16261 /* A ctor-initializer begins with a `:'. */
16262 || token->type == CPP_COLON
16263 /* A function-try-block begins with `try'. */
16264 || token->keyword == RID_TRY
16265 /* The named return value extension begins with `return'. */
16266 || token->keyword == RID_RETURN);
16267}
16268
16269/* Returns TRUE iff the next token is the ":" or "{" beginning a class
16270 definition. */
16271
16272static bool
16273cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16274{
16275 cp_token *token;
16276
16277 token = cp_lexer_peek_token (parser->lexer);
16278 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16279}
16280
d17811fd 16281/* Returns TRUE iff the next token is the "," or ">" ending a
03fd3f84 16282 template-argument. */
d17811fd
MM
16283
16284static bool
16285cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16286{
16287 cp_token *token;
16288
16289 token = cp_lexer_peek_token (parser->lexer);
391c4bc5 16290 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
d17811fd 16291}
f4abade9 16292
48b5c5c1 16293/* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
f4abade9
GB
16294 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16295
16296static bool
21526606 16297cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
f4abade9
GB
16298 size_t n)
16299{
16300 cp_token *token;
16301
16302 token = cp_lexer_peek_nth_token (parser->lexer, n);
16303 if (token->type == CPP_LESS)
16304 return true;
16305 /* Check for the sequence `<::' in the original code. It would be lexed as
16306 `[:', where `[' is a digraph, and there is no whitespace before
16307 `:'. */
16308 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16309 {
16310 cp_token *token2;
16311 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16312 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16313 return true;
16314 }
16315 return false;
16316}
21526606 16317
a723baf1
MM
16318/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16319 or none_type otherwise. */
16320
16321static enum tag_types
94edc4ab 16322cp_parser_token_is_class_key (cp_token* token)
a723baf1
MM
16323{
16324 switch (token->keyword)
16325 {
16326 case RID_CLASS:
16327 return class_type;
16328 case RID_STRUCT:
16329 return record_type;
16330 case RID_UNION:
16331 return union_type;
21526606 16332
a723baf1
MM
16333 default:
16334 return none_type;
16335 }
16336}
16337
16338/* Issue an error message if the CLASS_KEY does not match the TYPE. */
16339
16340static void
16341cp_parser_check_class_key (enum tag_types class_key, tree type)
16342{
16343 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
2a13a625 16344 pedwarn ("%qs tag used in naming %q#T",
a723baf1 16345 class_key == union_type ? "union"
21526606 16346 : class_key == record_type ? "struct" : "class",
a723baf1
MM
16347 type);
16348}
21526606 16349
cd0be382 16350/* Issue an error message if DECL is redeclared with different
37d407a1
KL
16351 access than its original declaration [class.access.spec/3].
16352 This applies to nested classes and nested class templates.
16353 [class.mem/1]. */
16354
2a13a625
GDR
16355static void
16356cp_parser_check_access_in_redeclaration (tree decl)
37d407a1
KL
16357{
16358 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16359 return;
16360
16361 if ((TREE_PRIVATE (decl)
16362 != (current_access_specifier == access_private_node))
16363 || (TREE_PROTECTED (decl)
16364 != (current_access_specifier == access_protected_node)))
2a13a625 16365 error ("%qD redeclared with different access", decl);
37d407a1
KL
16366}
16367
a723baf1 16368/* Look for the `template' keyword, as a syntactic disambiguator.
21526606 16369 Return TRUE iff it is present, in which case it will be
a723baf1
MM
16370 consumed. */
16371
16372static bool
16373cp_parser_optional_template_keyword (cp_parser *parser)
16374{
16375 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16376 {
16377 /* The `template' keyword can only be used within templates;
16378 outside templates the parser can always figure out what is a
16379 template and what is not. */
16380 if (!processing_template_decl)
16381 {
2a13a625 16382 error ("%<template%> (as a disambiguator) is only allowed "
a723baf1
MM
16383 "within templates");
16384 /* If this part of the token stream is rescanned, the same
16385 error message would be generated. So, we purge the token
16386 from the stream. */
16387 cp_lexer_purge_token (parser->lexer);
16388 return false;
16389 }
16390 else
16391 {
16392 /* Consume the `template' keyword. */
16393 cp_lexer_consume_token (parser->lexer);
16394 return true;
16395 }
16396 }
16397
16398 return false;
16399}
16400
2050a1bb
MM
16401/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16402 set PARSER->SCOPE, and perform other related actions. */
16403
16404static void
16405cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16406{
16407 tree value;
16408 tree check;
16409
16410 /* Get the stored value. */
16411 value = cp_lexer_consume_token (parser->lexer)->value;
16412 /* Perform any access checks that were deferred. */
16413 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c 16414 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
2050a1bb
MM
16415 /* Set the scope from the stored value. */
16416 parser->scope = TREE_VALUE (value);
16417 parser->qualifying_scope = TREE_TYPE (value);
16418 parser->object_scope = NULL_TREE;
16419}
16420
03fd3f84 16421/* Consume tokens up through a non-nested END token. */
a723baf1
MM
16422
16423static void
c162c75e
MA
16424cp_parser_cache_group (cp_parser *parser,
16425 enum cpp_ttype end,
16426 unsigned depth)
a723baf1
MM
16427{
16428 while (true)
16429 {
16430 cp_token *token;
16431
16432 /* Abort a parenthesized expression if we encounter a brace. */
16433 if ((end == CPP_CLOSE_PAREN || depth == 0)
16434 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16435 return;
a723baf1 16436 /* If we've reached the end of the file, stop. */
bc4071dd
RH
16437 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16438 || (end != CPP_PRAGMA_EOL
16439 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
a723baf1 16440 return;
4bfb8bba
MM
16441 /* Consume the next token. */
16442 token = cp_lexer_consume_token (parser->lexer);
a723baf1
MM
16443 /* See if it starts a new group. */
16444 if (token->type == CPP_OPEN_BRACE)
16445 {
c162c75e 16446 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
a723baf1
MM
16447 if (depth == 0)
16448 return;
16449 }
16450 else if (token->type == CPP_OPEN_PAREN)
c162c75e 16451 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
bc4071dd
RH
16452 else if (token->type == CPP_PRAGMA)
16453 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
a723baf1
MM
16454 else if (token->type == end)
16455 return;
16456 }
16457}
16458
16459/* Begin parsing tentatively. We always save tokens while parsing
16460 tentatively so that if the tentative parsing fails we can restore the
16461 tokens. */
16462
16463static void
94edc4ab 16464cp_parser_parse_tentatively (cp_parser* parser)
a723baf1
MM
16465{
16466 /* Enter a new parsing context. */
16467 parser->context = cp_parser_context_new (parser->context);
16468 /* Begin saving tokens. */
16469 cp_lexer_save_tokens (parser->lexer);
16470 /* In order to avoid repetitive access control error messages,
16471 access checks are queued up until we are no longer parsing
16472 tentatively. */
8d241e0b 16473 push_deferring_access_checks (dk_deferred);
a723baf1
MM
16474}
16475
16476/* Commit to the currently active tentative parse. */
16477
16478static void
94edc4ab 16479cp_parser_commit_to_tentative_parse (cp_parser* parser)
a723baf1
MM
16480{
16481 cp_parser_context *context;
16482 cp_lexer *lexer;
16483
16484 /* Mark all of the levels as committed. */
16485 lexer = parser->lexer;
16486 for (context = parser->context; context->next; context = context->next)
16487 {
16488 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16489 break;
16490 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16491 while (!cp_lexer_saving_tokens (lexer))
16492 lexer = lexer->next;
16493 cp_lexer_commit_tokens (lexer);
16494 }
16495}
16496
16497/* Abort the currently active tentative parse. All consumed tokens
16498 will be rolled back, and no diagnostics will be issued. */
16499
16500static void
94edc4ab 16501cp_parser_abort_tentative_parse (cp_parser* parser)
a723baf1
MM
16502{
16503 cp_parser_simulate_error (parser);
16504 /* Now, pretend that we want to see if the construct was
16505 successfully parsed. */
16506 cp_parser_parse_definitely (parser);
16507}
16508
34cd5ae7 16509/* Stop parsing tentatively. If a parse error has occurred, restore the
a723baf1
MM
16510 token stream. Otherwise, commit to the tokens we have consumed.
16511 Returns true if no error occurred; false otherwise. */
16512
16513static bool
94edc4ab 16514cp_parser_parse_definitely (cp_parser* parser)
a723baf1
MM
16515{
16516 bool error_occurred;
16517 cp_parser_context *context;
16518
34cd5ae7 16519 /* Remember whether or not an error occurred, since we are about to
a723baf1
MM
16520 destroy that information. */
16521 error_occurred = cp_parser_error_occurred (parser);
16522 /* Remove the topmost context from the stack. */
16523 context = parser->context;
16524 parser->context = context->next;
16525 /* If no parse errors occurred, commit to the tentative parse. */
16526 if (!error_occurred)
16527 {
16528 /* Commit to the tokens read tentatively, unless that was
16529 already done. */
16530 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16531 cp_lexer_commit_tokens (parser->lexer);
cf22909c
KL
16532
16533 pop_to_parent_deferring_access_checks ();
a723baf1
MM
16534 }
16535 /* Otherwise, if errors occurred, roll back our state so that things
16536 are just as they were before we began the tentative parse. */
16537 else
cf22909c
KL
16538 {
16539 cp_lexer_rollback_tokens (parser->lexer);
16540 pop_deferring_access_checks ();
16541 }
e5976695
MM
16542 /* Add the context to the front of the free list. */
16543 context->next = cp_parser_context_free_list;
16544 cp_parser_context_free_list = context;
16545
16546 return !error_occurred;
a723baf1
MM
16547}
16548
0b16f8f4
VR
16549/* Returns true if we are parsing tentatively and are not committed to
16550 this tentative parse. */
a723baf1
MM
16551
16552static bool
0b16f8f4 16553cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
a723baf1
MM
16554{
16555 return (cp_parser_parsing_tentatively (parser)
0b16f8f4 16556 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
a723baf1
MM
16557}
16558
4de8668e 16559/* Returns nonzero iff an error has occurred during the most recent
a723baf1 16560 tentative parse. */
21526606 16561
a723baf1 16562static bool
94edc4ab 16563cp_parser_error_occurred (cp_parser* parser)
a723baf1
MM
16564{
16565 return (cp_parser_parsing_tentatively (parser)
16566 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16567}
16568
4de8668e 16569/* Returns nonzero if GNU extensions are allowed. */
a723baf1
MM
16570
16571static bool
94edc4ab 16572cp_parser_allow_gnu_extensions_p (cp_parser* parser)
a723baf1
MM
16573{
16574 return parser->allow_gnu_extensions_p;
16575}
e58a9aa1
ZL
16576\f
16577/* Objective-C++ Productions */
16578
16579
16580/* Parse an Objective-C expression, which feeds into a primary-expression
16581 above.
16582
16583 objc-expression:
16584 objc-message-expression
16585 objc-string-literal
16586 objc-encode-expression
16587 objc-protocol-expression
16588 objc-selector-expression
16589
16590 Returns a tree representation of the expression. */
16591
16592static tree
16593cp_parser_objc_expression (cp_parser* parser)
16594{
16595 /* Try to figure out what kind of declaration is present. */
16596 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16597
16598 switch (kwd->type)
16599 {
16600 case CPP_OPEN_SQUARE:
16601 return cp_parser_objc_message_expression (parser);
16602
16603 case CPP_OBJC_STRING:
16604 kwd = cp_lexer_consume_token (parser->lexer);
16605 return objc_build_string_object (kwd->value);
16606
16607 case CPP_KEYWORD:
16608 switch (kwd->keyword)
16609 {
16610 case RID_AT_ENCODE:
16611 return cp_parser_objc_encode_expression (parser);
16612
16613 case RID_AT_PROTOCOL:
16614 return cp_parser_objc_protocol_expression (parser);
16615
16616 case RID_AT_SELECTOR:
16617 return cp_parser_objc_selector_expression (parser);
16618
16619 default:
16620 break;
16621 }
16622 default:
c85ce869 16623 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
e58a9aa1
ZL
16624 cp_parser_skip_to_end_of_block_or_statement (parser);
16625 }
16626
16627 return error_mark_node;
16628}
16629
16630/* Parse an Objective-C message expression.
16631
16632 objc-message-expression:
16633 [ objc-message-receiver objc-message-args ]
16634
16635 Returns a representation of an Objective-C message. */
16636
16637static tree
16638cp_parser_objc_message_expression (cp_parser* parser)
16639{
16640 tree receiver, messageargs;
16641
16642 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16643 receiver = cp_parser_objc_message_receiver (parser);
16644 messageargs = cp_parser_objc_message_args (parser);
16645 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16646
16647 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16648}
16649
16650/* Parse an objc-message-receiver.
16651
16652 objc-message-receiver:
e58a9aa1 16653 expression
660845bf 16654 simple-type-specifier
e58a9aa1
ZL
16655
16656 Returns a representation of the type or expression. */
16657
16658static tree
16659cp_parser_objc_message_receiver (cp_parser* parser)
16660{
16661 tree rcv;
e58a9aa1
ZL
16662
16663 /* An Objective-C message receiver may be either (1) a type
16664 or (2) an expression. */
16665 cp_parser_parse_tentatively (parser);
16666 rcv = cp_parser_expression (parser, false);
16667
16668 if (cp_parser_parse_definitely (parser))
16669 return rcv;
16670
660845bf
ZL
16671 rcv = cp_parser_simple_type_specifier (parser,
16672 /*decl_specs=*/NULL,
16673 CP_PARSER_FLAGS_NONE);
e58a9aa1
ZL
16674
16675 return objc_get_class_reference (rcv);
16676}
16677
16678/* Parse the arguments and selectors comprising an Objective-C message.
16679
16680 objc-message-args:
16681 objc-selector
16682 objc-selector-args
16683 objc-selector-args , objc-comma-args
16684
16685 objc-selector-args:
16686 objc-selector [opt] : assignment-expression
16687 objc-selector-args objc-selector [opt] : assignment-expression
16688
16689 objc-comma-args:
16690 assignment-expression
16691 objc-comma-args , assignment-expression
16692
16693 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16694 selector arguments and TREE_VALUE containing a list of comma
16695 arguments. */
16696
16697static tree
16698cp_parser_objc_message_args (cp_parser* parser)
16699{
16700 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16701 bool maybe_unary_selector_p = true;
16702 cp_token *token = cp_lexer_peek_token (parser->lexer);
16703
16704 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16705 {
16706 tree selector = NULL_TREE, arg;
16707
16708 if (token->type != CPP_COLON)
16709 selector = cp_parser_objc_selector (parser);
16710
16711 /* Detect if we have a unary selector. */
16712 if (maybe_unary_selector_p
16713 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16714 return build_tree_list (selector, NULL_TREE);
16715
16716 maybe_unary_selector_p = false;
16717 cp_parser_require (parser, CPP_COLON, "`:'");
16718 arg = cp_parser_assignment_expression (parser, false);
16719
16720 sel_args
16721 = chainon (sel_args,
16722 build_tree_list (selector, arg));
16723
16724 token = cp_lexer_peek_token (parser->lexer);
16725 }
16726
16727 /* Handle non-selector arguments, if any. */
16728 while (token->type == CPP_COMMA)
16729 {
16730 tree arg;
16731
16732 cp_lexer_consume_token (parser->lexer);
16733 arg = cp_parser_assignment_expression (parser, false);
16734
16735 addl_args
16736 = chainon (addl_args,
16737 build_tree_list (NULL_TREE, arg));
16738
16739 token = cp_lexer_peek_token (parser->lexer);
16740 }
16741
16742 return build_tree_list (sel_args, addl_args);
16743}
16744
16745/* Parse an Objective-C encode expression.
16746
16747 objc-encode-expression:
16748 @encode objc-typename
c8094d83 16749
e58a9aa1
ZL
16750 Returns an encoded representation of the type argument. */
16751
16752static tree
16753cp_parser_objc_encode_expression (cp_parser* parser)
16754{
16755 tree type;
16756
16757 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16758 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16759 type = complete_type (cp_parser_type_id (parser));
16760 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16761
16762 if (!type)
16763 {
c85ce869 16764 error ("%<@encode%> must specify a type as an argument");
e58a9aa1
ZL
16765 return error_mark_node;
16766 }
16767
16768 return objc_build_encode_expr (type);
16769}
16770
16771/* Parse an Objective-C @defs expression. */
16772
16773static tree
16774cp_parser_objc_defs_expression (cp_parser *parser)
16775{
16776 tree name;
16777
16778 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16779 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16780 name = cp_parser_identifier (parser);
16781 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16782
16783 return objc_get_class_ivars (name);
16784}
16785
16786/* Parse an Objective-C protocol expression.
16787
16788 objc-protocol-expression:
16789 @protocol ( identifier )
16790
16791 Returns a representation of the protocol expression. */
16792
16793static tree
16794cp_parser_objc_protocol_expression (cp_parser* parser)
16795{
16796 tree proto;
16797
16798 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16799 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16800 proto = cp_parser_identifier (parser);
16801 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16802
16803 return objc_build_protocol_expr (proto);
16804}
16805
16806/* Parse an Objective-C selector expression.
16807
16808 objc-selector-expression:
16809 @selector ( objc-method-signature )
16810
16811 objc-method-signature:
16812 objc-selector
16813 objc-selector-seq
16814
16815 objc-selector-seq:
16816 objc-selector :
16817 objc-selector-seq objc-selector :
16818
16819 Returns a representation of the method selector. */
16820
16821static tree
16822cp_parser_objc_selector_expression (cp_parser* parser)
16823{
16824 tree sel_seq = NULL_TREE;
16825 bool maybe_unary_selector_p = true;
16826 cp_token *token;
16827
16828 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16829 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16830 token = cp_lexer_peek_token (parser->lexer);
16831
d95036e3
AP
16832 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16833 || token->type == CPP_SCOPE)
e58a9aa1
ZL
16834 {
16835 tree selector = NULL_TREE;
16836
d95036e3
AP
16837 if (token->type != CPP_COLON
16838 || token->type == CPP_SCOPE)
e58a9aa1
ZL
16839 selector = cp_parser_objc_selector (parser);
16840
d95036e3
AP
16841 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16842 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
e58a9aa1 16843 {
d95036e3
AP
16844 /* Detect if we have a unary selector. */
16845 if (maybe_unary_selector_p)
16846 {
16847 sel_seq = selector;
16848 goto finish_selector;
16849 }
16850 else
16851 {
16852 cp_parser_error (parser, "expected %<:%>");
16853 }
e58a9aa1 16854 }
e58a9aa1 16855 maybe_unary_selector_p = false;
d95036e3
AP
16856 token = cp_lexer_consume_token (parser->lexer);
16857
16858 if (token->type == CPP_SCOPE)
16859 {
16860 sel_seq
16861 = chainon (sel_seq,
16862 build_tree_list (selector, NULL_TREE));
16863 sel_seq
16864 = chainon (sel_seq,
16865 build_tree_list (NULL_TREE, NULL_TREE));
16866 }
16867 else
16868 sel_seq
16869 = chainon (sel_seq,
16870 build_tree_list (selector, NULL_TREE));
e58a9aa1
ZL
16871
16872 token = cp_lexer_peek_token (parser->lexer);
16873 }
16874
16875 finish_selector:
16876 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16877
16878 return objc_build_selector_expr (sel_seq);
16879}
16880
16881/* Parse a list of identifiers.
16882
16883 objc-identifier-list:
16884 identifier
16885 objc-identifier-list , identifier
16886
16887 Returns a TREE_LIST of identifier nodes. */
16888
16889static tree
16890cp_parser_objc_identifier_list (cp_parser* parser)
16891{
16892 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16893 cp_token *sep = cp_lexer_peek_token (parser->lexer);
16894
16895 while (sep->type == CPP_COMMA)
16896 {
16897 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
c8094d83 16898 list = chainon (list,
e58a9aa1
ZL
16899 build_tree_list (NULL_TREE,
16900 cp_parser_identifier (parser)));
16901 sep = cp_lexer_peek_token (parser->lexer);
16902 }
c8094d83 16903
e58a9aa1
ZL
16904 return list;
16905}
16906
16907/* Parse an Objective-C alias declaration.
16908
16909 objc-alias-declaration:
16910 @compatibility_alias identifier identifier ;
16911
16912 This function registers the alias mapping with the Objective-C front-end.
16913 It returns nothing. */
16914
16915static void
16916cp_parser_objc_alias_declaration (cp_parser* parser)
16917{
16918 tree alias, orig;
16919
16920 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
16921 alias = cp_parser_identifier (parser);
16922 orig = cp_parser_identifier (parser);
16923 objc_declare_alias (alias, orig);
16924 cp_parser_consume_semicolon_at_end_of_statement (parser);
16925}
16926
16927/* Parse an Objective-C class forward-declaration.
16928
16929 objc-class-declaration:
16930 @class objc-identifier-list ;
16931
16932 The function registers the forward declarations with the Objective-C
16933 front-end. It returns nothing. */
16934
16935static void
16936cp_parser_objc_class_declaration (cp_parser* parser)
16937{
16938 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
16939 objc_declare_class (cp_parser_objc_identifier_list (parser));
16940 cp_parser_consume_semicolon_at_end_of_statement (parser);
16941}
16942
16943/* Parse a list of Objective-C protocol references.
16944
16945 objc-protocol-refs-opt:
16946 objc-protocol-refs [opt]
16947
16948 objc-protocol-refs:
16949 < objc-identifier-list >
16950
16951 Returns a TREE_LIST of identifiers, if any. */
16952
16953static tree
16954cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16955{
16956 tree protorefs = NULL_TREE;
16957
16958 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16959 {
16960 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
16961 protorefs = cp_parser_objc_identifier_list (parser);
16962 cp_parser_require (parser, CPP_GREATER, "`>'");
16963 }
16964
16965 return protorefs;
16966}
16967
16968/* Parse a Objective-C visibility specification. */
16969
16970static void
16971cp_parser_objc_visibility_spec (cp_parser* parser)
16972{
16973 cp_token *vis = cp_lexer_peek_token (parser->lexer);
16974
16975 switch (vis->keyword)
16976 {
16977 case RID_AT_PRIVATE:
16978 objc_set_visibility (2);
16979 break;
16980 case RID_AT_PROTECTED:
16981 objc_set_visibility (0);
16982 break;
16983 case RID_AT_PUBLIC:
16984 objc_set_visibility (1);
16985 break;
16986 default:
16987 return;
16988 }
a723baf1 16989
e58a9aa1
ZL
16990 /* Eat '@private'/'@protected'/'@public'. */
16991 cp_lexer_consume_token (parser->lexer);
16992}
16993
16994/* Parse an Objective-C method type. */
16995
16996static void
16997cp_parser_objc_method_type (cp_parser* parser)
16998{
16999 objc_set_method_type
17000 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17001 ? PLUS_EXPR
17002 : MINUS_EXPR);
17003}
17004
17005/* Parse an Objective-C protocol qualifier. */
17006
17007static tree
17008cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17009{
17010 tree quals = NULL_TREE, node;
17011 cp_token *token = cp_lexer_peek_token (parser->lexer);
17012
17013 node = token->value;
17014
17015 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17016 && (node == ridpointers [(int) RID_IN]
17017 || node == ridpointers [(int) RID_OUT]
17018 || node == ridpointers [(int) RID_INOUT]
17019 || node == ridpointers [(int) RID_BYCOPY]
0cbd7506 17020 || node == ridpointers [(int) RID_BYREF]
e58a9aa1
ZL
17021 || node == ridpointers [(int) RID_ONEWAY]))
17022 {
17023 quals = tree_cons (NULL_TREE, node, quals);
17024 cp_lexer_consume_token (parser->lexer);
17025 token = cp_lexer_peek_token (parser->lexer);
17026 node = token->value;
17027 }
17028
17029 return quals;
17030}
17031
17032/* Parse an Objective-C typename. */
17033
17034static tree
17035cp_parser_objc_typename (cp_parser* parser)
17036{
17037 tree typename = NULL_TREE;
17038
17039 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17040 {
17041 tree proto_quals, cp_type = NULL_TREE;
17042
17043 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17044 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17045
17046 /* An ObjC type name may consist of just protocol qualifiers, in which
17047 case the type shall default to 'id'. */
17048 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17049 cp_type = cp_parser_type_id (parser);
17050
17051 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17052 typename = build_tree_list (proto_quals, cp_type);
17053 }
17054
17055 return typename;
17056}
17057
17058/* Check to see if TYPE refers to an Objective-C selector name. */
17059
17060static bool
17061cp_parser_objc_selector_p (enum cpp_ttype type)
17062{
17063 return (type == CPP_NAME || type == CPP_KEYWORD
17064 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17065 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17066 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17067 || type == CPP_XOR || type == CPP_XOR_EQ);
17068}
17069
17070/* Parse an Objective-C selector. */
17071
17072static tree
17073cp_parser_objc_selector (cp_parser* parser)
17074{
17075 cp_token *token = cp_lexer_consume_token (parser->lexer);
c8094d83 17076
e58a9aa1
ZL
17077 if (!cp_parser_objc_selector_p (token->type))
17078 {
17079 error ("invalid Objective-C++ selector name");
17080 return error_mark_node;
17081 }
17082
17083 /* C++ operator names are allowed to appear in ObjC selectors. */
17084 switch (token->type)
17085 {
17086 case CPP_AND_AND: return get_identifier ("and");
17087 case CPP_AND_EQ: return get_identifier ("and_eq");
17088 case CPP_AND: return get_identifier ("bitand");
17089 case CPP_OR: return get_identifier ("bitor");
17090 case CPP_COMPL: return get_identifier ("compl");
17091 case CPP_NOT: return get_identifier ("not");
17092 case CPP_NOT_EQ: return get_identifier ("not_eq");
17093 case CPP_OR_OR: return get_identifier ("or");
17094 case CPP_OR_EQ: return get_identifier ("or_eq");
17095 case CPP_XOR: return get_identifier ("xor");
17096 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17097 default: return token->value;
17098 }
17099}
17100
17101/* Parse an Objective-C params list. */
17102
17103static tree
17104cp_parser_objc_method_keyword_params (cp_parser* parser)
17105{
17106 tree params = NULL_TREE;
17107 bool maybe_unary_selector_p = true;
17108 cp_token *token = cp_lexer_peek_token (parser->lexer);
17109
17110 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17111 {
17112 tree selector = NULL_TREE, typename, identifier;
17113
17114 if (token->type != CPP_COLON)
17115 selector = cp_parser_objc_selector (parser);
17116
17117 /* Detect if we have a unary selector. */
17118 if (maybe_unary_selector_p
17119 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17120 return selector;
17121
17122 maybe_unary_selector_p = false;
17123 cp_parser_require (parser, CPP_COLON, "`:'");
17124 typename = cp_parser_objc_typename (parser);
17125 identifier = cp_parser_identifier (parser);
17126
17127 params
17128 = chainon (params,
c8094d83 17129 objc_build_keyword_decl (selector,
e58a9aa1
ZL
17130 typename,
17131 identifier));
17132
17133 token = cp_lexer_peek_token (parser->lexer);
17134 }
17135
17136 return params;
17137}
17138
17139/* Parse the non-keyword Objective-C params. */
17140
17141static tree
17142cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17143{
17144 tree params = make_node (TREE_LIST);
17145 cp_token *token = cp_lexer_peek_token (parser->lexer);
17146 *ellipsisp = false; /* Initially, assume no ellipsis. */
17147
17148 while (token->type == CPP_COMMA)
17149 {
17150 cp_parameter_declarator *parmdecl;
17151 tree parm;
17152
17153 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17154 token = cp_lexer_peek_token (parser->lexer);
17155
17156 if (token->type == CPP_ELLIPSIS)
17157 {
17158 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17159 *ellipsisp = true;
17160 break;
17161 }
17162
17163 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17164 parm = grokdeclarator (parmdecl->declarator,
17165 &parmdecl->decl_specifiers,
c8094d83 17166 PARM, /*initialized=*/0,
e58a9aa1
ZL
17167 /*attrlist=*/NULL);
17168
17169 chainon (params, build_tree_list (NULL_TREE, parm));
17170 token = cp_lexer_peek_token (parser->lexer);
17171 }
17172
17173 return params;
17174}
17175
17176/* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17177
17178static void
17179cp_parser_objc_interstitial_code (cp_parser* parser)
17180{
17181 cp_token *token = cp_lexer_peek_token (parser->lexer);
17182
17183 /* If the next token is `extern' and the following token is a string
17184 literal, then we have a linkage specification. */
17185 if (token->keyword == RID_EXTERN
17186 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17187 cp_parser_linkage_specification (parser);
17188 /* Handle #pragma, if any. */
17189 else if (token->type == CPP_PRAGMA)
bc4071dd 17190 cp_parser_pragma (parser, pragma_external);
e58a9aa1
ZL
17191 /* Allow stray semicolons. */
17192 else if (token->type == CPP_SEMICOLON)
17193 cp_lexer_consume_token (parser->lexer);
17194 /* Finally, try to parse a block-declaration, or a function-definition. */
17195 else
17196 cp_parser_block_declaration (parser, /*statement_p=*/false);
17197}
17198
17199/* Parse a method signature. */
17200
17201static tree
17202cp_parser_objc_method_signature (cp_parser* parser)
17203{
17204 tree rettype, kwdparms, optparms;
17205 bool ellipsis = false;
17206
17207 cp_parser_objc_method_type (parser);
17208 rettype = cp_parser_objc_typename (parser);
17209 kwdparms = cp_parser_objc_method_keyword_params (parser);
17210 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17211
17212 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17213}
17214
17215/* Pars an Objective-C method prototype list. */
17216
17217static void
17218cp_parser_objc_method_prototype_list (cp_parser* parser)
17219{
17220 cp_token *token = cp_lexer_peek_token (parser->lexer);
17221
17222 while (token->keyword != RID_AT_END)
17223 {
17224 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17225 {
17226 objc_add_method_declaration
17227 (cp_parser_objc_method_signature (parser));
17228 cp_parser_consume_semicolon_at_end_of_statement (parser);
17229 }
17230 else
17231 /* Allow for interspersed non-ObjC++ code. */
17232 cp_parser_objc_interstitial_code (parser);
17233
17234 token = cp_lexer_peek_token (parser->lexer);
17235 }
17236
17237 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17238 objc_finish_interface ();
17239}
17240
17241/* Parse an Objective-C method definition list. */
17242
17243static void
17244cp_parser_objc_method_definition_list (cp_parser* parser)
17245{
17246 cp_token *token = cp_lexer_peek_token (parser->lexer);
17247
17248 while (token->keyword != RID_AT_END)
17249 {
17250 tree meth;
17251
17252 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17253 {
17254 push_deferring_access_checks (dk_deferred);
17255 objc_start_method_definition
17256 (cp_parser_objc_method_signature (parser));
17257
17258 /* For historical reasons, we accept an optional semicolon. */
17259 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17260 cp_lexer_consume_token (parser->lexer);
17261
17262 perform_deferred_access_checks ();
17263 stop_deferring_access_checks ();
17264 meth = cp_parser_function_definition_after_declarator (parser,
17265 false);
17266 pop_deferring_access_checks ();
17267 objc_finish_method_definition (meth);
17268 }
17269 else
17270 /* Allow for interspersed non-ObjC++ code. */
17271 cp_parser_objc_interstitial_code (parser);
17272
17273 token = cp_lexer_peek_token (parser->lexer);
17274 }
17275
17276 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17277 objc_finish_implementation ();
17278}
17279
17280/* Parse Objective-C ivars. */
17281
17282static void
17283cp_parser_objc_class_ivars (cp_parser* parser)
17284{
17285 cp_token *token = cp_lexer_peek_token (parser->lexer);
17286
17287 if (token->type != CPP_OPEN_BRACE)
17288 return; /* No ivars specified. */
17289
17290 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17291 token = cp_lexer_peek_token (parser->lexer);
17292
17293 while (token->type != CPP_CLOSE_BRACE)
17294 {
17295 cp_decl_specifier_seq declspecs;
17296 int decl_class_or_enum_p;
17297 tree prefix_attributes;
17298
17299 cp_parser_objc_visibility_spec (parser);
17300
17301 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17302 break;
17303
17304 cp_parser_decl_specifier_seq (parser,
17305 CP_PARSER_FLAGS_OPTIONAL,
17306 &declspecs,
17307 &decl_class_or_enum_p);
17308 prefix_attributes = declspecs.attributes;
17309 declspecs.attributes = NULL_TREE;
17310
17311 /* Keep going until we hit the `;' at the end of the
17312 declaration. */
17313 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17314 {
17315 tree width = NULL_TREE, attributes, first_attribute, decl;
17316 cp_declarator *declarator = NULL;
17317 int ctor_dtor_or_conv_p;
17318
17319 /* Check for a (possibly unnamed) bitfield declaration. */
17320 token = cp_lexer_peek_token (parser->lexer);
17321 if (token->type == CPP_COLON)
17322 goto eat_colon;
17323
17324 if (token->type == CPP_NAME
17325 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17326 == CPP_COLON))
17327 {
17328 /* Get the name of the bitfield. */
17329 declarator = make_id_declarator (NULL_TREE,
d85d3d57
MM
17330 cp_parser_identifier (parser),
17331 sfk_none);
e58a9aa1
ZL
17332
17333 eat_colon:
17334 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17335 /* Get the width of the bitfield. */
17336 width
17337 = cp_parser_constant_expression (parser,
17338 /*allow_non_constant=*/false,
17339 NULL);
17340 }
17341 else
17342 {
17343 /* Parse the declarator. */
c8094d83 17344 declarator
e58a9aa1
ZL
17345 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17346 &ctor_dtor_or_conv_p,
17347 /*parenthesized_p=*/NULL,
17348 /*member_p=*/false);
17349 }
17350
17351 /* Look for attributes that apply to the ivar. */
17352 attributes = cp_parser_attributes_opt (parser);
17353 /* Remember which attributes are prefix attributes and
17354 which are not. */
17355 first_attribute = attributes;
17356 /* Combine the attributes. */
17357 attributes = chainon (prefix_attributes, attributes);
17358
17359 if (width)
17360 {
17361 /* Create the bitfield declaration. */
17362 decl = grokbitfield (declarator, &declspecs, width);
17363 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17364 }
17365 else
d174af6c
MM
17366 decl = grokfield (declarator, &declspecs,
17367 NULL_TREE, /*init_const_expr_p=*/false,
e58a9aa1 17368 NULL_TREE, attributes);
c8094d83 17369
e58a9aa1
ZL
17370 /* Add the instance variable. */
17371 objc_add_instance_variable (decl);
17372
17373 /* Reset PREFIX_ATTRIBUTES. */
17374 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17375 attributes = TREE_CHAIN (attributes);
17376 if (attributes)
17377 TREE_CHAIN (attributes) = NULL_TREE;
17378
17379 token = cp_lexer_peek_token (parser->lexer);
17380
17381 if (token->type == CPP_COMMA)
17382 {
17383 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17384 continue;
17385 }
17386 break;
17387 }
17388
17389 cp_parser_consume_semicolon_at_end_of_statement (parser);
17390 token = cp_lexer_peek_token (parser->lexer);
17391 }
17392
17393 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17394 /* For historical reasons, we accept an optional semicolon. */
17395 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17396 cp_lexer_consume_token (parser->lexer);
17397}
17398
17399/* Parse an Objective-C protocol declaration. */
17400
17401static void
17402cp_parser_objc_protocol_declaration (cp_parser* parser)
17403{
17404 tree proto, protorefs;
17405 cp_token *tok;
17406
17407 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17408 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17409 {
c85ce869 17410 error ("identifier expected after %<@protocol%>");
e58a9aa1
ZL
17411 goto finish;
17412 }
17413
128a79fb 17414 /* See if we have a forward declaration or a definition. */
e58a9aa1 17415 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
c8094d83 17416
e58a9aa1
ZL
17417 /* Try a forward declaration first. */
17418 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17419 {
17420 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
c8094d83 17421 finish:
e58a9aa1 17422 cp_parser_consume_semicolon_at_end_of_statement (parser);
c8094d83 17423 }
e58a9aa1
ZL
17424
17425 /* Ok, we got a full-fledged definition (or at least should). */
17426 else
17427 {
17428 proto = cp_parser_identifier (parser);
17429 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17430 objc_start_protocol (proto, protorefs);
17431 cp_parser_objc_method_prototype_list (parser);
17432 }
17433}
17434
17435/* Parse an Objective-C superclass or category. */
17436
17437static void
17438cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17439 tree *categ)
17440{
17441 cp_token *next = cp_lexer_peek_token (parser->lexer);
17442
17443 *super = *categ = NULL_TREE;
17444 if (next->type == CPP_COLON)
17445 {
17446 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17447 *super = cp_parser_identifier (parser);
17448 }
17449 else if (next->type == CPP_OPEN_PAREN)
17450 {
17451 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17452 *categ = cp_parser_identifier (parser);
17453 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17454 }
17455}
17456
17457/* Parse an Objective-C class interface. */
17458
17459static void
17460cp_parser_objc_class_interface (cp_parser* parser)
17461{
17462 tree name, super, categ, protos;
17463
17464 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17465 name = cp_parser_identifier (parser);
17466 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17467 protos = cp_parser_objc_protocol_refs_opt (parser);
17468
17469 /* We have either a class or a category on our hands. */
17470 if (categ)
17471 objc_start_category_interface (name, categ, protos);
17472 else
17473 {
17474 objc_start_class_interface (name, super, protos);
17475 /* Handle instance variable declarations, if any. */
17476 cp_parser_objc_class_ivars (parser);
17477 objc_continue_interface ();
17478 }
17479
17480 cp_parser_objc_method_prototype_list (parser);
17481}
17482
17483/* Parse an Objective-C class implementation. */
17484
17485static void
17486cp_parser_objc_class_implementation (cp_parser* parser)
17487{
17488 tree name, super, categ;
17489
17490 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17491 name = cp_parser_identifier (parser);
17492 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17493
17494 /* We have either a class or a category on our hands. */
17495 if (categ)
17496 objc_start_category_implementation (name, categ);
17497 else
17498 {
17499 objc_start_class_implementation (name, super);
17500 /* Handle instance variable declarations, if any. */
17501 cp_parser_objc_class_ivars (parser);
17502 objc_continue_implementation ();
17503 }
17504
17505 cp_parser_objc_method_definition_list (parser);
17506}
17507
17508/* Consume the @end token and finish off the implementation. */
17509
17510static void
17511cp_parser_objc_end_implementation (cp_parser* parser)
17512{
17513 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17514 objc_finish_implementation ();
17515}
17516
17517/* Parse an Objective-C declaration. */
17518
17519static void
17520cp_parser_objc_declaration (cp_parser* parser)
17521{
17522 /* Try to figure out what kind of declaration is present. */
17523 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17524
17525 switch (kwd->keyword)
17526 {
17527 case RID_AT_ALIAS:
17528 cp_parser_objc_alias_declaration (parser);
17529 break;
17530 case RID_AT_CLASS:
17531 cp_parser_objc_class_declaration (parser);
17532 break;
17533 case RID_AT_PROTOCOL:
17534 cp_parser_objc_protocol_declaration (parser);
17535 break;
17536 case RID_AT_INTERFACE:
17537 cp_parser_objc_class_interface (parser);
17538 break;
17539 case RID_AT_IMPLEMENTATION:
17540 cp_parser_objc_class_implementation (parser);
17541 break;
17542 case RID_AT_END:
17543 cp_parser_objc_end_implementation (parser);
17544 break;
17545 default:
c85ce869 17546 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
e58a9aa1
ZL
17547 cp_parser_skip_to_end_of_block_or_statement (parser);
17548 }
17549}
17550
17551/* Parse an Objective-C try-catch-finally statement.
17552
17553 objc-try-catch-finally-stmt:
17554 @try compound-statement objc-catch-clause-seq [opt]
17555 objc-finally-clause [opt]
17556
17557 objc-catch-clause-seq:
17558 objc-catch-clause objc-catch-clause-seq [opt]
17559
17560 objc-catch-clause:
17561 @catch ( exception-declaration ) compound-statement
17562
17563 objc-finally-clause
17564 @finally compound-statement
17565
17566 Returns NULL_TREE. */
17567
17568static tree
17569cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17570 location_t location;
17571 tree stmt;
17572
17573 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17574 location = cp_lexer_peek_token (parser->lexer)->location;
17575 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17576 node, lest it get absorbed into the surrounding block. */
17577 stmt = push_stmt_list ();
17578 cp_parser_compound_statement (parser, NULL, false);
17579 objc_begin_try_stmt (location, pop_stmt_list (stmt));
c8094d83 17580
e58a9aa1
ZL
17581 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17582 {
17583 cp_parameter_declarator *parmdecl;
17584 tree parm;
17585
17586 cp_lexer_consume_token (parser->lexer);
17587 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17588 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17589 parm = grokdeclarator (parmdecl->declarator,
17590 &parmdecl->decl_specifiers,
c8094d83 17591 PARM, /*initialized=*/0,
e58a9aa1
ZL
17592 /*attrlist=*/NULL);
17593 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17594 objc_begin_catch_clause (parm);
17595 cp_parser_compound_statement (parser, NULL, false);
17596 objc_finish_catch_clause ();
17597 }
17598
17599 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17600 {
17601 cp_lexer_consume_token (parser->lexer);
17602 location = cp_lexer_peek_token (parser->lexer)->location;
17603 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17604 node, lest it get absorbed into the surrounding block. */
17605 stmt = push_stmt_list ();
17606 cp_parser_compound_statement (parser, NULL, false);
17607 objc_build_finally_clause (location, pop_stmt_list (stmt));
17608 }
17609
17610 return objc_finish_try_stmt ();
17611}
17612
17613/* Parse an Objective-C synchronized statement.
17614
17615 objc-synchronized-stmt:
17616 @synchronized ( expression ) compound-statement
17617
17618 Returns NULL_TREE. */
17619
17620static tree
17621cp_parser_objc_synchronized_statement (cp_parser *parser) {
17622 location_t location;
17623 tree lock, stmt;
17624
17625 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17626
17627 location = cp_lexer_peek_token (parser->lexer)->location;
17628 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17629 lock = cp_parser_expression (parser, false);
17630 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17631
17632 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17633 node, lest it get absorbed into the surrounding block. */
17634 stmt = push_stmt_list ();
17635 cp_parser_compound_statement (parser, NULL, false);
17636
17637 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17638}
17639
17640/* Parse an Objective-C throw statement.
17641
17642 objc-throw-stmt:
17643 @throw assignment-expression [opt] ;
17644
17645 Returns a constructed '@throw' statement. */
17646
17647static tree
17648cp_parser_objc_throw_statement (cp_parser *parser) {
17649 tree expr = NULL_TREE;
17650
17651 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17652
17653 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17654 expr = cp_parser_assignment_expression (parser, false);
17655
17656 cp_parser_consume_semicolon_at_end_of_statement (parser);
17657
17658 return objc_build_throw_stmt (expr);
17659}
17660
17661/* Parse an Objective-C statement. */
17662
17663static tree
17664cp_parser_objc_statement (cp_parser * parser) {
17665 /* Try to figure out what kind of declaration is present. */
17666 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17667
17668 switch (kwd->keyword)
17669 {
17670 case RID_AT_TRY:
17671 return cp_parser_objc_try_catch_finally_statement (parser);
17672 case RID_AT_SYNCHRONIZED:
17673 return cp_parser_objc_synchronized_statement (parser);
17674 case RID_AT_THROW:
17675 return cp_parser_objc_throw_statement (parser);
17676 default:
c85ce869 17677 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
e58a9aa1
ZL
17678 cp_parser_skip_to_end_of_block_or_statement (parser);
17679 }
17680
17681 return error_mark_node;
17682}
1799e5d5
RH
17683\f
17684/* OpenMP 2.5 parsing routines. */
17685
17686/* All OpenMP clauses. OpenMP 2.5. */
17687typedef enum pragma_omp_clause {
17688 PRAGMA_OMP_CLAUSE_NONE = 0,
17689
17690 PRAGMA_OMP_CLAUSE_COPYIN,
17691 PRAGMA_OMP_CLAUSE_COPYPRIVATE,
17692 PRAGMA_OMP_CLAUSE_DEFAULT,
17693 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
17694 PRAGMA_OMP_CLAUSE_IF,
17695 PRAGMA_OMP_CLAUSE_LASTPRIVATE,
17696 PRAGMA_OMP_CLAUSE_NOWAIT,
17697 PRAGMA_OMP_CLAUSE_NUM_THREADS,
17698 PRAGMA_OMP_CLAUSE_ORDERED,
17699 PRAGMA_OMP_CLAUSE_PRIVATE,
17700 PRAGMA_OMP_CLAUSE_REDUCTION,
17701 PRAGMA_OMP_CLAUSE_SCHEDULE,
17702 PRAGMA_OMP_CLAUSE_SHARED
17703} pragma_omp_clause;
17704
17705/* Returns name of the next clause.
17706 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17707 the token is not consumed. Otherwise appropriate pragma_omp_clause is
17708 returned and the token is consumed. */
17709
17710static pragma_omp_clause
17711cp_parser_omp_clause_name (cp_parser *parser)
17712{
17713 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
17714
17715 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
17716 result = PRAGMA_OMP_CLAUSE_IF;
17717 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
17718 result = PRAGMA_OMP_CLAUSE_DEFAULT;
17719 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
17720 result = PRAGMA_OMP_CLAUSE_PRIVATE;
17721 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17722 {
17723 tree id = cp_lexer_peek_token (parser->lexer)->value;
17724 const char *p = IDENTIFIER_POINTER (id);
17725
17726 switch (p[0])
17727 {
17728 case 'c':
17729 if (!strcmp ("copyin", p))
17730 result = PRAGMA_OMP_CLAUSE_COPYIN;
17731 else if (!strcmp ("copyprivate", p))
17732 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
17733 break;
17734 case 'f':
17735 if (!strcmp ("firstprivate", p))
17736 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
17737 break;
17738 case 'l':
17739 if (!strcmp ("lastprivate", p))
17740 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
17741 break;
17742 case 'n':
17743 if (!strcmp ("nowait", p))
17744 result = PRAGMA_OMP_CLAUSE_NOWAIT;
17745 else if (!strcmp ("num_threads", p))
17746 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
17747 break;
17748 case 'o':
17749 if (!strcmp ("ordered", p))
17750 result = PRAGMA_OMP_CLAUSE_ORDERED;
17751 break;
17752 case 'r':
17753 if (!strcmp ("reduction", p))
17754 result = PRAGMA_OMP_CLAUSE_REDUCTION;
17755 break;
17756 case 's':
17757 if (!strcmp ("schedule", p))
17758 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
17759 else if (!strcmp ("shared", p))
17760 result = PRAGMA_OMP_CLAUSE_SHARED;
17761 break;
17762 }
17763 }
a723baf1 17764
1799e5d5
RH
17765 if (result != PRAGMA_OMP_CLAUSE_NONE)
17766 cp_lexer_consume_token (parser->lexer);
a723baf1 17767
1799e5d5
RH
17768 return result;
17769}
bc4071dd 17770
1799e5d5 17771/* Validate that a clause of the given type does not already exist. */
bc4071dd
RH
17772
17773static void
1799e5d5 17774check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
bc4071dd 17775{
1799e5d5 17776 tree c;
bc4071dd 17777
1799e5d5
RH
17778 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
17779 if (OMP_CLAUSE_CODE (c) == code)
17780 {
17781 error ("too many %qs clauses", name);
17782 break;
17783 }
17784}
bc4071dd 17785
1799e5d5
RH
17786/* OpenMP 2.5:
17787 variable-list:
17788 identifier
17789 variable-list , identifier
17790
17791 In addition, we match a closing parenthesis. An opening parenthesis
17792 will have been consumed by the caller.
17793
17794 If KIND is nonzero, create the appropriate node and install the decl
17795 in OMP_CLAUSE_DECL and add the node to the head of the list.
17796
17797 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17798 return the list created. */
17799
17800static tree
17801cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
17802 tree list)
17803{
17804 while (1)
bc4071dd 17805 {
1799e5d5 17806 tree name, decl;
bc4071dd 17807
1799e5d5
RH
17808 name = cp_parser_id_expression (parser, /*template_p=*/false,
17809 /*check_dependency_p=*/true,
17810 /*template_p=*/NULL,
fa6098f8
MM
17811 /*declarator_p=*/false,
17812 /*optional_p=*/false);
1799e5d5
RH
17813 if (name == error_mark_node)
17814 goto skip_comma;
17815
17816 decl = cp_parser_lookup_name_simple (parser, name);
17817 if (decl == error_mark_node)
17818 cp_parser_name_lookup_error (parser, name, decl, NULL);
17819 else if (kind != 0)
17820 {
17821 tree u = build_omp_clause (kind);
17822 OMP_CLAUSE_DECL (u) = decl;
17823 OMP_CLAUSE_CHAIN (u) = list;
17824 list = u;
17825 }
17826 else
17827 list = tree_cons (decl, NULL_TREE, list);
17828
17829 get_comma:
17830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17831 break;
17832 cp_lexer_consume_token (parser->lexer);
17833 }
17834
17835 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17836 {
17837 int ending;
17838
17839 /* Try to resync to an unnested comma. Copied from
17840 cp_parser_parenthesized_expression_list. */
17841 skip_comma:
17842 ending = cp_parser_skip_to_closing_parenthesis (parser,
17843 /*recovering=*/true,
17844 /*or_comma=*/true,
17845 /*consume_paren=*/true);
17846 if (ending < 0)
17847 goto get_comma;
17848 }
17849
17850 return list;
17851}
17852
17853/* Similarly, but expect leading and trailing parenthesis. This is a very
17854 common case for omp clauses. */
17855
17856static tree
17857cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
17858{
17859 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17860 return cp_parser_omp_var_list_no_open (parser, kind, list);
17861 return list;
17862}
17863
17864/* OpenMP 2.5:
17865 default ( shared | none ) */
17866
17867static tree
17868cp_parser_omp_clause_default (cp_parser *parser, tree list)
17869{
17870 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
17871 tree c;
17872
17873 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17874 return list;
17875 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17876 {
17877 tree id = cp_lexer_peek_token (parser->lexer)->value;
17878 const char *p = IDENTIFIER_POINTER (id);
17879
17880 switch (p[0])
17881 {
17882 case 'n':
17883 if (strcmp ("none", p) != 0)
17884 goto invalid_kind;
17885 kind = OMP_CLAUSE_DEFAULT_NONE;
17886 break;
17887
17888 case 's':
17889 if (strcmp ("shared", p) != 0)
17890 goto invalid_kind;
17891 kind = OMP_CLAUSE_DEFAULT_SHARED;
17892 break;
17893
17894 default:
17895 goto invalid_kind;
17896 }
17897
17898 cp_lexer_consume_token (parser->lexer);
bc4071dd
RH
17899 }
17900 else
1799e5d5
RH
17901 {
17902 invalid_kind:
17903 cp_parser_error (parser, "expected %<none%> or %<shared%>");
17904 }
bc4071dd 17905
1799e5d5
RH
17906 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17907 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
17908 /*or_comma=*/false,
17909 /*consume_paren=*/true);
17910
17911 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
17912 return list;
bc4071dd 17913
1799e5d5
RH
17914 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
17915 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
17916 OMP_CLAUSE_CHAIN (c) = list;
17917 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
bc4071dd 17918
1799e5d5 17919 return c;
bc4071dd
RH
17920}
17921
1799e5d5
RH
17922/* OpenMP 2.5:
17923 if ( expression ) */
bc4071dd 17924
1799e5d5
RH
17925static tree
17926cp_parser_omp_clause_if (cp_parser *parser, tree list)
bc4071dd 17927{
1799e5d5 17928 tree t, c;
bc4071dd 17929
1799e5d5
RH
17930 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17931 return list;
bc4071dd 17932
1799e5d5
RH
17933 t = cp_parser_condition (parser);
17934
17935 if (t == error_mark_node
17936 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17937 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
17938 /*or_comma=*/false,
17939 /*consume_paren=*/true);
17940
17941 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
17942
17943 c = build_omp_clause (OMP_CLAUSE_IF);
17944 OMP_CLAUSE_IF_EXPR (c) = t;
17945 OMP_CLAUSE_CHAIN (c) = list;
17946
17947 return c;
17948}
17949
17950/* OpenMP 2.5:
17951 nowait */
17952
17953static tree
17954cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
17955{
17956 tree c;
17957
17958 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
17959
17960 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
17961 OMP_CLAUSE_CHAIN (c) = list;
17962 return c;
17963}
17964
17965/* OpenMP 2.5:
17966 num_threads ( expression ) */
17967
17968static tree
17969cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
17970{
17971 tree t, c;
17972
17973 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17974 return list;
17975
17976 t = cp_parser_expression (parser, false);
17977
17978 if (t == error_mark_node
17979 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17980 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
17981 /*or_comma=*/false,
17982 /*consume_paren=*/true);
17983
17984 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
17985
17986 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
17987 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
17988 OMP_CLAUSE_CHAIN (c) = list;
17989
17990 return c;
17991}
17992
17993/* OpenMP 2.5:
17994 ordered */
17995
17996static tree
17997cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
17998{
17999 tree c;
18000
18001 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18002
18003 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18004 OMP_CLAUSE_CHAIN (c) = list;
18005 return c;
18006}
18007
18008/* OpenMP 2.5:
18009 reduction ( reduction-operator : variable-list )
18010
18011 reduction-operator:
18012 One of: + * - & ^ | && || */
18013
18014static tree
18015cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18016{
18017 enum tree_code code;
18018 tree nlist, c;
18019
18020 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18021 return list;
18022
18023 switch (cp_lexer_peek_token (parser->lexer)->type)
bc4071dd 18024 {
1799e5d5
RH
18025 case CPP_PLUS:
18026 code = PLUS_EXPR;
18027 break;
18028 case CPP_MULT:
18029 code = MULT_EXPR;
18030 break;
18031 case CPP_MINUS:
18032 code = MINUS_EXPR;
18033 break;
18034 case CPP_AND:
18035 code = BIT_AND_EXPR;
18036 break;
18037 case CPP_XOR:
18038 code = BIT_XOR_EXPR;
18039 break;
18040 case CPP_OR:
18041 code = BIT_IOR_EXPR;
18042 break;
18043 case CPP_AND_AND:
18044 code = TRUTH_ANDIF_EXPR;
18045 break;
18046 case CPP_OR_OR:
18047 code = TRUTH_ORIF_EXPR;
bc4071dd 18048 break;
bc4071dd 18049 default:
1799e5d5
RH
18050 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18051 resync_fail:
18052 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18053 /*or_comma=*/false,
18054 /*consume_paren=*/true);
18055 return list;
18056 }
18057 cp_lexer_consume_token (parser->lexer);
18058
18059 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18060 goto resync_fail;
18061
18062 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18063 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18064 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18065
18066 return nlist;
18067}
18068
18069/* OpenMP 2.5:
18070 schedule ( schedule-kind )
18071 schedule ( schedule-kind , expression )
18072
18073 schedule-kind:
18074 static | dynamic | guided | runtime
18075*/
18076
18077static tree
18078cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18079{
18080 tree c, t;
18081
18082 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18083 return list;
18084
18085 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18086
18087 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18088 {
18089 tree id = cp_lexer_peek_token (parser->lexer)->value;
18090 const char *p = IDENTIFIER_POINTER (id);
18091
18092 switch (p[0])
18093 {
18094 case 'd':
18095 if (strcmp ("dynamic", p) != 0)
18096 goto invalid_kind;
18097 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18098 break;
18099
18100 case 'g':
18101 if (strcmp ("guided", p) != 0)
18102 goto invalid_kind;
18103 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18104 break;
18105
18106 case 'r':
18107 if (strcmp ("runtime", p) != 0)
18108 goto invalid_kind;
18109 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18110 break;
18111
18112 default:
18113 goto invalid_kind;
18114 }
18115 }
18116 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18117 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18118 else
18119 goto invalid_kind;
18120 cp_lexer_consume_token (parser->lexer);
18121
18122 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18123 {
18124 cp_lexer_consume_token (parser->lexer);
18125
18126 t = cp_parser_assignment_expression (parser, false);
18127
18128 if (t == error_mark_node)
18129 goto resync_fail;
18130 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18131 error ("schedule %<runtime%> does not take "
18132 "a %<chunk_size%> parameter");
18133 else
18134 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18135
18136 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18137 goto resync_fail;
18138 }
18139 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18140 goto resync_fail;
18141
18142 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18143 OMP_CLAUSE_CHAIN (c) = list;
18144 return c;
18145
18146 invalid_kind:
18147 cp_parser_error (parser, "invalid schedule kind");
18148 resync_fail:
18149 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18150 /*or_comma=*/false,
18151 /*consume_paren=*/true);
18152 return list;
18153}
18154
18155/* Parse all OpenMP clauses. The set clauses allowed by the directive
18156 is a bitmask in MASK. Return the list of clauses found; the result
18157 of clause default goes in *pdefault. */
18158
18159static tree
18160cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18161 const char *where, cp_token *pragma_tok)
18162{
18163 tree clauses = NULL;
18164
18165 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18166 {
18167 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18168 const char *c_name;
18169 tree prev = clauses;
18170
18171 switch (c_kind)
18172 {
18173 case PRAGMA_OMP_CLAUSE_COPYIN:
18174 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18175 c_name = "copyin";
18176 break;
18177 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18178 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18179 clauses);
18180 c_name = "copyprivate";
18181 break;
18182 case PRAGMA_OMP_CLAUSE_DEFAULT:
18183 clauses = cp_parser_omp_clause_default (parser, clauses);
18184 c_name = "default";
18185 break;
18186 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18187 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18188 clauses);
18189 c_name = "firstprivate";
18190 break;
18191 case PRAGMA_OMP_CLAUSE_IF:
18192 clauses = cp_parser_omp_clause_if (parser, clauses);
18193 c_name = "if";
18194 break;
18195 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18196 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18197 clauses);
18198 c_name = "lastprivate";
18199 break;
18200 case PRAGMA_OMP_CLAUSE_NOWAIT:
18201 clauses = cp_parser_omp_clause_nowait (parser, clauses);
18202 c_name = "nowait";
18203 break;
18204 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18205 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18206 c_name = "num_threads";
18207 break;
18208 case PRAGMA_OMP_CLAUSE_ORDERED:
18209 clauses = cp_parser_omp_clause_ordered (parser, clauses);
18210 c_name = "ordered";
18211 break;
18212 case PRAGMA_OMP_CLAUSE_PRIVATE:
18213 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18214 clauses);
18215 c_name = "private";
18216 break;
18217 case PRAGMA_OMP_CLAUSE_REDUCTION:
18218 clauses = cp_parser_omp_clause_reduction (parser, clauses);
18219 c_name = "reduction";
18220 break;
18221 case PRAGMA_OMP_CLAUSE_SCHEDULE:
18222 clauses = cp_parser_omp_clause_schedule (parser, clauses);
18223 c_name = "schedule";
18224 break;
18225 case PRAGMA_OMP_CLAUSE_SHARED:
18226 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18227 clauses);
18228 c_name = "shared";
18229 break;
18230 default:
18231 cp_parser_error (parser, "expected %<#pragma omp%> clause");
18232 goto saw_error;
18233 }
18234
18235 if (((mask >> c_kind) & 1) == 0)
18236 {
18237 /* Remove the invalid clause(s) from the list to avoid
18238 confusing the rest of the compiler. */
18239 clauses = prev;
18240 error ("%qs is not valid for %qs", c_name, where);
18241 }
18242 }
18243 saw_error:
18244 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18245 return finish_omp_clauses (clauses);
18246}
18247
18248/* OpenMP 2.5:
18249 structured-block:
18250 statement
18251
18252 In practice, we're also interested in adding the statement to an
18253 outer node. So it is convenient if we work around the fact that
18254 cp_parser_statement calls add_stmt. */
18255
18256static unsigned
18257cp_parser_begin_omp_structured_block (cp_parser *parser)
18258{
18259 unsigned save = parser->in_statement;
18260
18261 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18262 This preserves the "not within loop or switch" style error messages
18263 for nonsense cases like
18264 void foo() {
18265 #pragma omp single
18266 break;
18267 }
18268 */
18269 if (parser->in_statement)
18270 parser->in_statement = IN_OMP_BLOCK;
18271
18272 return save;
18273}
18274
18275static void
18276cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18277{
18278 parser->in_statement = save;
18279}
18280
18281static tree
18282cp_parser_omp_structured_block (cp_parser *parser)
18283{
18284 tree stmt = begin_omp_structured_block ();
18285 unsigned int save = cp_parser_begin_omp_structured_block (parser);
18286
18287 cp_parser_statement (parser, NULL_TREE, false);
18288
18289 cp_parser_end_omp_structured_block (parser, save);
18290 return finish_omp_structured_block (stmt);
18291}
18292
18293/* OpenMP 2.5:
18294 # pragma omp atomic new-line
18295 expression-stmt
18296
18297 expression-stmt:
18298 x binop= expr | x++ | ++x | x-- | --x
18299 binop:
18300 +, *, -, /, &, ^, |, <<, >>
18301
18302 where x is an lvalue expression with scalar type. */
18303
18304static void
18305cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18306{
18307 tree lhs, rhs;
18308 enum tree_code code;
18309
18310 cp_parser_require_pragma_eol (parser, pragma_tok);
18311
18312 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18313 /*cast_p=*/false);
18314 switch (TREE_CODE (lhs))
18315 {
18316 case ERROR_MARK:
18317 goto saw_error;
18318
18319 case PREINCREMENT_EXPR:
18320 case POSTINCREMENT_EXPR:
18321 lhs = TREE_OPERAND (lhs, 0);
18322 code = PLUS_EXPR;
18323 rhs = integer_one_node;
18324 break;
18325
18326 case PREDECREMENT_EXPR:
18327 case POSTDECREMENT_EXPR:
18328 lhs = TREE_OPERAND (lhs, 0);
18329 code = MINUS_EXPR;
18330 rhs = integer_one_node;
18331 break;
18332
18333 default:
18334 switch (cp_lexer_peek_token (parser->lexer)->type)
18335 {
18336 case CPP_MULT_EQ:
18337 code = MULT_EXPR;
18338 break;
18339 case CPP_DIV_EQ:
18340 code = TRUNC_DIV_EXPR;
18341 break;
18342 case CPP_PLUS_EQ:
18343 code = PLUS_EXPR;
18344 break;
18345 case CPP_MINUS_EQ:
18346 code = MINUS_EXPR;
18347 break;
18348 case CPP_LSHIFT_EQ:
18349 code = LSHIFT_EXPR;
18350 break;
18351 case CPP_RSHIFT_EQ:
18352 code = RSHIFT_EXPR;
18353 break;
18354 case CPP_AND_EQ:
18355 code = BIT_AND_EXPR;
18356 break;
18357 case CPP_OR_EQ:
18358 code = BIT_IOR_EXPR;
18359 break;
18360 case CPP_XOR_EQ:
18361 code = BIT_XOR_EXPR;
18362 break;
18363 default:
18364 cp_parser_error (parser,
18365 "invalid operator for %<#pragma omp atomic%>");
18366 goto saw_error;
18367 }
18368 cp_lexer_consume_token (parser->lexer);
18369
18370 rhs = cp_parser_expression (parser, false);
18371 if (rhs == error_mark_node)
18372 goto saw_error;
18373 break;
18374 }
18375 finish_omp_atomic (code, lhs, rhs);
18376 cp_parser_consume_semicolon_at_end_of_statement (parser);
18377 return;
18378
18379 saw_error:
18380 cp_parser_skip_to_end_of_block_or_statement (parser);
18381}
18382
18383
18384/* OpenMP 2.5:
18385 # pragma omp barrier new-line
18386*/
18387
18388static void
18389cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18390{
18391 cp_parser_require_pragma_eol (parser, pragma_tok);
18392 finish_omp_barrier ();
18393}
18394
18395/* OpenMP 2.5:
18396 # pragma omp critical [(name)] new-line
18397 structured-block
18398*/
18399
18400static tree
18401cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18402{
18403 tree stmt, name = NULL;
18404
18405 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18406 {
18407 cp_lexer_consume_token (parser->lexer);
18408
18409 name = cp_parser_identifier (parser);
18410
18411 if (name == error_mark_node
18412 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18413 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18414 /*or_comma=*/false,
18415 /*consume_paren=*/true);
18416 if (name == error_mark_node)
18417 name = NULL;
18418 }
18419 cp_parser_require_pragma_eol (parser, pragma_tok);
18420
18421 stmt = cp_parser_omp_structured_block (parser);
18422 return c_finish_omp_critical (stmt, name);
18423}
18424
18425/* OpenMP 2.5:
18426 # pragma omp flush flush-vars[opt] new-line
18427
18428 flush-vars:
18429 ( variable-list ) */
18430
18431static void
18432cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18433{
18434 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18435 (void) cp_parser_omp_var_list (parser, 0, NULL);
18436 cp_parser_require_pragma_eol (parser, pragma_tok);
18437
18438 finish_omp_flush ();
18439}
18440
18441/* Parse the restricted form of the for statment allowed by OpenMP. */
18442
18443static tree
18444cp_parser_omp_for_loop (cp_parser *parser)
18445{
18446 tree init, cond, incr, body, decl, pre_body;
18447 location_t loc;
18448
18449 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18450 {
18451 cp_parser_error (parser, "for statement expected");
18452 return NULL;
18453 }
18454 loc = cp_lexer_consume_token (parser->lexer)->location;
18455 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18456 return NULL;
18457
18458 init = decl = NULL;
18459 pre_body = push_stmt_list ();
18460 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18461 {
18462 cp_decl_specifier_seq type_specifiers;
18463
18464 /* First, try to parse as an initialized declaration. See
18465 cp_parser_condition, from whence the bulk of this is copied. */
18466
18467 cp_parser_parse_tentatively (parser);
18468 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18469 &type_specifiers);
18470 if (!cp_parser_error_occurred (parser))
18471 {
18472 tree asm_specification, attributes;
18473 cp_declarator *declarator;
18474
18475 declarator = cp_parser_declarator (parser,
18476 CP_PARSER_DECLARATOR_NAMED,
18477 /*ctor_dtor_or_conv_p=*/NULL,
18478 /*parenthesized_p=*/NULL,
18479 /*member_p=*/false);
18480 attributes = cp_parser_attributes_opt (parser);
18481 asm_specification = cp_parser_asm_specification_opt (parser);
18482
18483 cp_parser_require (parser, CPP_EQ, "`='");
18484 if (cp_parser_parse_definitely (parser))
18485 {
18486 tree pushed_scope;
18487
18488 decl = start_decl (declarator, &type_specifiers,
18489 /*initialized_p=*/false, attributes,
18490 /*prefix_attributes=*/NULL_TREE,
18491 &pushed_scope);
18492
18493 init = cp_parser_assignment_expression (parser, false);
18494
18495 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18496 asm_specification, LOOKUP_ONLYCONVERTING);
18497
18498 if (pushed_scope)
18499 pop_scope (pushed_scope);
18500 }
18501 }
18502
18503 /* If parsing as an initialized declaration failed, try again as
18504 a simple expression. */
18505 if (decl == NULL)
18506 {
18507 cp_parser_abort_tentative_parse (parser);
18508 init = cp_parser_expression (parser, false);
18509 }
18510 }
18511 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18512 pre_body = pop_stmt_list (pre_body);
18513
18514 cond = NULL;
18515 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18516 cond = cp_parser_condition (parser);
18517 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18518
18519 incr = NULL;
18520 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18521 incr = cp_parser_expression (parser, false);
18522
18523 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18524 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18525 /*or_comma=*/false,
18526 /*consume_paren=*/true);
18527
18528 /* Note that we saved the original contents of this flag when we entered
18529 the structured block, and so we don't need to re-save it here. */
18530 parser->in_statement = IN_OMP_FOR;
18531
18532 /* Note that the grammar doesn't call for a structured block here,
18533 though the loop as a whole is a structured block. */
18534 body = push_stmt_list ();
18535 cp_parser_statement (parser, NULL_TREE, false);
18536 body = pop_stmt_list (body);
18537
18538 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18539}
18540
18541/* OpenMP 2.5:
18542 #pragma omp for for-clause[optseq] new-line
18543 for-loop
18544*/
18545
18546#define OMP_FOR_CLAUSE_MASK \
18547 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18548 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18549 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18550 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18551 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18552 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18553 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18554
18555static tree
18556cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18557{
18558 tree clauses, sb, ret;
18559 unsigned int save;
18560
18561 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18562 "#pragma omp for", pragma_tok);
18563
18564 sb = begin_omp_structured_block ();
18565 save = cp_parser_begin_omp_structured_block (parser);
18566
18567 ret = cp_parser_omp_for_loop (parser);
18568 if (ret)
18569 OMP_FOR_CLAUSES (ret) = clauses;
18570
18571 cp_parser_end_omp_structured_block (parser, save);
18572 add_stmt (finish_omp_structured_block (sb));
18573
18574 return ret;
18575}
18576
18577/* OpenMP 2.5:
18578 # pragma omp master new-line
18579 structured-block
18580*/
18581
18582static tree
18583cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18584{
18585 cp_parser_require_pragma_eol (parser, pragma_tok);
18586 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18587}
18588
18589/* OpenMP 2.5:
18590 # pragma omp ordered new-line
18591 structured-block
18592*/
18593
18594static tree
18595cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18596{
18597 cp_parser_require_pragma_eol (parser, pragma_tok);
18598 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18599}
18600
18601/* OpenMP 2.5:
18602
18603 section-scope:
18604 { section-sequence }
18605
18606 section-sequence:
18607 section-directive[opt] structured-block
18608 section-sequence section-directive structured-block */
18609
18610static tree
18611cp_parser_omp_sections_scope (cp_parser *parser)
18612{
18613 tree stmt, substmt;
18614 bool error_suppress = false;
18615 cp_token *tok;
18616
18617 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18618 return NULL_TREE;
18619
18620 stmt = push_stmt_list ();
18621
18622 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18623 {
18624 unsigned save;
18625
18626 substmt = begin_omp_structured_block ();
18627 save = cp_parser_begin_omp_structured_block (parser);
18628
18629 while (1)
18630 {
18631 cp_parser_statement (parser, NULL_TREE, false);
18632
18633 tok = cp_lexer_peek_token (parser->lexer);
18634 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18635 break;
18636 if (tok->type == CPP_CLOSE_BRACE)
18637 break;
18638 if (tok->type == CPP_EOF)
18639 break;
18640 }
18641
18642 cp_parser_end_omp_structured_block (parser, save);
18643 substmt = finish_omp_structured_block (substmt);
18644 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18645 add_stmt (substmt);
18646 }
18647
18648 while (1)
18649 {
18650 tok = cp_lexer_peek_token (parser->lexer);
18651 if (tok->type == CPP_CLOSE_BRACE)
18652 break;
18653 if (tok->type == CPP_EOF)
18654 break;
18655
18656 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18657 {
18658 cp_lexer_consume_token (parser->lexer);
18659 cp_parser_require_pragma_eol (parser, tok);
18660 error_suppress = false;
18661 }
18662 else if (!error_suppress)
18663 {
18664 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
18665 error_suppress = true;
18666 }
18667
18668 substmt = cp_parser_omp_structured_block (parser);
18669 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18670 add_stmt (substmt);
18671 }
18672 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
18673
18674 substmt = pop_stmt_list (stmt);
18675
18676 stmt = make_node (OMP_SECTIONS);
18677 TREE_TYPE (stmt) = void_type_node;
18678 OMP_SECTIONS_BODY (stmt) = substmt;
18679
18680 add_stmt (stmt);
18681 return stmt;
18682}
18683
18684/* OpenMP 2.5:
18685 # pragma omp sections sections-clause[optseq] newline
18686 sections-scope
18687*/
18688
18689#define OMP_SECTIONS_CLAUSE_MASK \
18690 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18691 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18692 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18693 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18694 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18695
18696static tree
18697cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
18698{
18699 tree clauses, ret;
18700
18701 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
18702 "#pragma omp sections", pragma_tok);
18703
18704 ret = cp_parser_omp_sections_scope (parser);
18705 if (ret)
18706 OMP_SECTIONS_CLAUSES (ret) = clauses;
18707
18708 return ret;
18709}
18710
18711/* OpenMP 2.5:
18712 # pragma parallel parallel-clause new-line
18713 # pragma parallel for parallel-for-clause new-line
18714 # pragma parallel sections parallel-sections-clause new-line
18715*/
18716
18717#define OMP_PARALLEL_CLAUSE_MASK \
18718 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
18719 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18720 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18721 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
18722 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
18723 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
18724 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18725 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18726
18727static tree
18728cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
18729{
18730 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
18731 const char *p_name = "#pragma omp parallel";
18732 tree stmt, clauses, par_clause, ws_clause, block;
18733 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
18734 unsigned int save;
18735
18736 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18737 {
18738 cp_lexer_consume_token (parser->lexer);
18739 p_kind = PRAGMA_OMP_PARALLEL_FOR;
18740 p_name = "#pragma omp parallel for";
18741 mask |= OMP_FOR_CLAUSE_MASK;
18742 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18743 }
18744 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18745 {
18746 tree id = cp_lexer_peek_token (parser->lexer)->value;
18747 const char *p = IDENTIFIER_POINTER (id);
18748 if (strcmp (p, "sections") == 0)
18749 {
18750 cp_lexer_consume_token (parser->lexer);
18751 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
18752 p_name = "#pragma omp parallel sections";
18753 mask |= OMP_SECTIONS_CLAUSE_MASK;
18754 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18755 }
18756 }
18757
18758 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
18759 block = begin_omp_parallel ();
18760 save = cp_parser_begin_omp_structured_block (parser);
18761
18762 switch (p_kind)
18763 {
18764 case PRAGMA_OMP_PARALLEL:
18765 cp_parser_already_scoped_statement (parser);
18766 par_clause = clauses;
18767 break;
18768
18769 case PRAGMA_OMP_PARALLEL_FOR:
18770 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18771 stmt = cp_parser_omp_for_loop (parser);
18772 if (stmt)
18773 OMP_FOR_CLAUSES (stmt) = ws_clause;
18774 break;
18775
18776 case PRAGMA_OMP_PARALLEL_SECTIONS:
18777 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18778 stmt = cp_parser_omp_sections_scope (parser);
18779 if (stmt)
18780 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
18781 break;
18782
18783 default:
18784 gcc_unreachable ();
18785 }
18786
18787 cp_parser_end_omp_structured_block (parser, save);
18788 return finish_omp_parallel (par_clause, block);
18789}
18790
18791/* OpenMP 2.5:
18792 # pragma omp single single-clause[optseq] new-line
18793 structured-block
18794*/
18795
18796#define OMP_SINGLE_CLAUSE_MASK \
18797 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18798 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18799 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
18800 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18801
18802static tree
18803cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
18804{
18805 tree stmt = make_node (OMP_SINGLE);
18806 TREE_TYPE (stmt) = void_type_node;
18807
18808 OMP_SINGLE_CLAUSES (stmt)
18809 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
18810 "#pragma omp single", pragma_tok);
18811 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
18812
18813 return add_stmt (stmt);
18814}
18815
18816/* OpenMP 2.5:
18817 # pragma omp threadprivate (variable-list) */
18818
18819static void
18820cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
18821{
18822 tree vars;
18823
18824 vars = cp_parser_omp_var_list (parser, 0, NULL);
18825 cp_parser_require_pragma_eol (parser, pragma_tok);
18826
18827 if (!targetm.have_tls)
18828 sorry ("threadprivate variables not supported in this target");
18829
18830 finish_omp_threadprivate (vars);
18831}
18832
18833/* Main entry point to OpenMP statement pragmas. */
18834
18835static void
18836cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
18837{
18838 tree stmt;
18839
18840 switch (pragma_tok->pragma_kind)
18841 {
18842 case PRAGMA_OMP_ATOMIC:
18843 cp_parser_omp_atomic (parser, pragma_tok);
18844 return;
18845 case PRAGMA_OMP_CRITICAL:
18846 stmt = cp_parser_omp_critical (parser, pragma_tok);
18847 break;
18848 case PRAGMA_OMP_FOR:
18849 stmt = cp_parser_omp_for (parser, pragma_tok);
18850 break;
18851 case PRAGMA_OMP_MASTER:
18852 stmt = cp_parser_omp_master (parser, pragma_tok);
18853 break;
18854 case PRAGMA_OMP_ORDERED:
18855 stmt = cp_parser_omp_ordered (parser, pragma_tok);
18856 break;
18857 case PRAGMA_OMP_PARALLEL:
18858 stmt = cp_parser_omp_parallel (parser, pragma_tok);
18859 break;
18860 case PRAGMA_OMP_SECTIONS:
18861 stmt = cp_parser_omp_sections (parser, pragma_tok);
18862 break;
18863 case PRAGMA_OMP_SINGLE:
18864 stmt = cp_parser_omp_single (parser, pragma_tok);
18865 break;
18866 default:
18867 gcc_unreachable ();
18868 }
18869
18870 if (stmt)
18871 SET_EXPR_LOCATION (stmt, pragma_tok->location);
18872}
18873\f
18874/* The parser. */
18875
18876static GTY (()) cp_parser *the_parser;
18877
18878\f
18879/* Special handling for the first token or line in the file. The first
18880 thing in the file might be #pragma GCC pch_preprocess, which loads a
18881 PCH file, which is a GC collection point. So we need to handle this
18882 first pragma without benefit of an existing lexer structure.
18883
18884 Always returns one token to the caller in *FIRST_TOKEN. This is
18885 either the true first token of the file, or the first token after
18886 the initial pragma. */
18887
18888static void
18889cp_parser_initial_pragma (cp_token *first_token)
18890{
18891 tree name = NULL;
18892
18893 cp_lexer_get_preprocessor_token (NULL, first_token);
18894 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
18895 return;
18896
18897 cp_lexer_get_preprocessor_token (NULL, first_token);
18898 if (first_token->type == CPP_STRING)
18899 {
18900 name = first_token->value;
18901
18902 cp_lexer_get_preprocessor_token (NULL, first_token);
18903 if (first_token->type != CPP_PRAGMA_EOL)
18904 error ("junk at end of %<#pragma GCC pch_preprocess%>");
18905 }
18906 else
18907 error ("expected string literal");
18908
18909 /* Skip to the end of the pragma. */
18910 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
18911 cp_lexer_get_preprocessor_token (NULL, first_token);
18912
18913 /* Read one more token to return to our caller. */
18914 cp_lexer_get_preprocessor_token (NULL, first_token);
18915
18916 /* Now actually load the PCH file. */
18917 if (name)
18918 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
18919}
18920
18921/* Normal parsing of a pragma token. Here we can (and must) use the
18922 regular lexer. */
18923
18924static bool
18925cp_parser_pragma (cp_parser *parser, enum pragma_context context)
18926{
18927 cp_token *pragma_tok;
18928 unsigned int id;
18929
18930 pragma_tok = cp_lexer_consume_token (parser->lexer);
18931 gcc_assert (pragma_tok->type == CPP_PRAGMA);
18932 parser->lexer->in_pragma = true;
18933
18934 id = pragma_tok->pragma_kind;
18935 switch (id)
18936 {
18937 case PRAGMA_GCC_PCH_PREPROCESS:
18938 error ("%<#pragma GCC pch_preprocess%> must be first");
18939 break;
18940
18941 case PRAGMA_OMP_BARRIER:
18942 switch (context)
18943 {
18944 case pragma_compound:
18945 cp_parser_omp_barrier (parser, pragma_tok);
18946 return false;
18947 case pragma_stmt:
18948 error ("%<#pragma omp barrier%> may only be "
18949 "used in compound statements");
18950 break;
18951 default:
18952 goto bad_stmt;
18953 }
18954 break;
18955
18956 case PRAGMA_OMP_FLUSH:
18957 switch (context)
18958 {
18959 case pragma_compound:
18960 cp_parser_omp_flush (parser, pragma_tok);
18961 return false;
18962 case pragma_stmt:
18963 error ("%<#pragma omp flush%> may only be "
18964 "used in compound statements");
18965 break;
18966 default:
18967 goto bad_stmt;
18968 }
18969 break;
18970
18971 case PRAGMA_OMP_THREADPRIVATE:
18972 cp_parser_omp_threadprivate (parser, pragma_tok);
18973 return false;
18974
18975 case PRAGMA_OMP_ATOMIC:
18976 case PRAGMA_OMP_CRITICAL:
18977 case PRAGMA_OMP_FOR:
18978 case PRAGMA_OMP_MASTER:
18979 case PRAGMA_OMP_ORDERED:
18980 case PRAGMA_OMP_PARALLEL:
18981 case PRAGMA_OMP_SECTIONS:
18982 case PRAGMA_OMP_SINGLE:
18983 if (context == pragma_external)
18984 goto bad_stmt;
18985 cp_parser_omp_construct (parser, pragma_tok);
18986 return true;
18987
18988 case PRAGMA_OMP_SECTION:
18989 error ("%<#pragma omp section%> may only be used in "
18990 "%<#pragma omp sections%> construct");
18991 break;
18992
18993 default:
18994 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
18995 c_invoke_pragma_handler (id);
18996 break;
18997
18998 bad_stmt:
18999 cp_parser_error (parser, "expected declaration specifiers");
bc4071dd
RH
19000 break;
19001 }
19002
19003 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19004 return false;
19005}
19006
19007/* The interface the pragma parsers have to the lexer. */
19008
19009enum cpp_ttype
19010pragma_lex (tree *value)
19011{
19012 cp_token *tok;
19013 enum cpp_ttype ret;
19014
19015 tok = cp_lexer_peek_token (the_parser->lexer);
19016
19017 ret = tok->type;
19018 *value = tok->value;
19019
19020 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19021 ret = CPP_EOF;
19022 else if (ret == CPP_STRING)
19023 *value = cp_parser_string_literal (the_parser, false, false);
19024 else
19025 {
19026 cp_lexer_consume_token (the_parser->lexer);
19027 if (ret == CPP_KEYWORD)
19028 ret = CPP_NAME;
19029 }
19030
19031 return ret;
19032}
19033
19034\f
a723baf1
MM
19035/* External interface. */
19036
d1bd0ded 19037/* Parse one entire translation unit. */
a723baf1 19038
d1bd0ded
GK
19039void
19040c_parse_file (void)
a723baf1
MM
19041{
19042 bool error_occurred;
f75fbaf7
ZW
19043 static bool already_called = false;
19044
19045 if (already_called)
19046 {
19047 sorry ("inter-module optimizations not implemented for C++");
19048 return;
19049 }
19050 already_called = true;
a723baf1
MM
19051
19052 the_parser = cp_parser_new ();
78757caa
KL
19053 push_deferring_access_checks (flag_access_control
19054 ? dk_no_deferred : dk_no_check);
a723baf1
MM
19055 error_occurred = cp_parser_translation_unit (the_parser);
19056 the_parser = NULL;
a723baf1
MM
19057}
19058
a723baf1
MM
19059/* This variable must be provided by every front end. */
19060
19061int yydebug;
19062
19063#include "gt-cp-parser.h"
This page took 4.513127 seconds and 5 git commands to generate.