]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
re PR c++/28588 (static private function)
[thirdparty/gcc.git] / gcc / cp / parser.c
CommitLineData
a723baf1 1/* C++ Parser.
4514aa8c
NS
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
a723baf1
MM
4 Written by Mark Mitchell <mark@codesourcery.com>.
5
f5adbb8d 6 This file is part of GCC.
a723baf1 7
f5adbb8d 8 GCC is free software; you can redistribute it and/or modify it
a723baf1
MM
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
f5adbb8d 13 GCC is distributed in the hope that it will be useful, but
a723baf1
MM
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
f5adbb8d 19 along with GCC; see the file COPYING. If not, write to the Free
1788952f
KC
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
a723baf1
MM
22
23#include "config.h"
24#include "system.h"
25#include "coretypes.h"
26#include "tm.h"
27#include "dyn-string.h"
28#include "varray.h"
29#include "cpplib.h"
30#include "tree.h"
31#include "cp-tree.h"
32#include "c-pragma.h"
33#include "decl.h"
34#include "flags.h"
35#include "diagnostic.h"
a723baf1
MM
36#include "toplev.h"
37#include "output.h"
62d1db17 38#include "target.h"
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. */
5ff5e6c8 208#define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
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 515 gcc_assert (n > 0);
3db45ab5 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 },
b8b94c5b
PB
1153
1154 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1155 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1156
1157 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1158
1159 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1160
1161 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1162
1163 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1164
1165 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1166};
1167
1168/* The same as binops, but initialized by cp_parser_new so that
1169 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1170 for speed. */
1171static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1172
a723baf1
MM
1173/* Constructors and destructors. */
1174
1175/* Construct a new context. The context below this one on the stack
1176 is given by NEXT. */
1177
1178static cp_parser_context *
94edc4ab 1179cp_parser_context_new (cp_parser_context* next)
a723baf1
MM
1180{
1181 cp_parser_context *context;
1182
1183 /* Allocate the storage. */
e5976695
MM
1184 if (cp_parser_context_free_list != NULL)
1185 {
1186 /* Pull the first entry from the free list. */
1187 context = cp_parser_context_free_list;
1188 cp_parser_context_free_list = context->next;
c68b0a84 1189 memset (context, 0, sizeof (*context));
e5976695
MM
1190 }
1191 else
99dd239f 1192 context = GGC_CNEW (cp_parser_context);
b8b94c5b 1193
a723baf1
MM
1194 /* No errors have occurred yet in this context. */
1195 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1196 /* If this is not the bottomost context, copy information that we
1197 need from the previous context. */
1198 if (next)
1199 {
1200 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1201 expression, then we are parsing one in this context, too. */
1202 context->object_type = next->object_type;
a723baf1
MM
1203 /* Thread the stack. */
1204 context->next = next;
1205 }
1206
1207 return context;
1208}
1209
1210/* The cp_parser structure represents the C++ parser. */
1211
1212typedef struct cp_parser GTY(())
1213{
1214 /* The lexer from which we are obtaining tokens. */
1215 cp_lexer *lexer;
1216
1217 /* The scope in which names should be looked up. If NULL_TREE, then
1218 we look up names in the scope that is currently open in the
1219 source program. If non-NULL, this is either a TYPE or
8fe4d24b
NS
1220 NAMESPACE_DECL for the scope in which we should look. It can
1221 also be ERROR_MARK, when we've parsed a bogus scope.
a723baf1
MM
1222
1223 This value is not cleared automatically after a name is looked
1224 up, so we must be careful to clear it before starting a new look
1225 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1226 will look up `Z' in the scope of `X', rather than the current
1227 scope.) Unfortunately, it is difficult to tell when name lookup
1228 is complete, because we sometimes peek at a token, look it up,
8fe4d24b 1229 and then decide not to consume it. */
a723baf1
MM
1230 tree scope;
1231
1232 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1233 last lookup took place. OBJECT_SCOPE is used if an expression
1234 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
21526606 1235 respectively. QUALIFYING_SCOPE is used for an expression of the
a723baf1
MM
1236 form "X::Y"; it refers to X. */
1237 tree object_scope;
1238 tree qualifying_scope;
1239
1240 /* A stack of parsing contexts. All but the bottom entry on the
1241 stack will be tentative contexts.
1242
1243 We parse tentatively in order to determine which construct is in
1244 use in some situations. For example, in order to determine
1245 whether a statement is an expression-statement or a
1246 declaration-statement we parse it tentatively as a
1247 declaration-statement. If that fails, we then reparse the same
1248 token stream as an expression-statement. */
1249 cp_parser_context *context;
1250
1251 /* True if we are parsing GNU C++. If this flag is not set, then
1252 GNU extensions are not recognized. */
1253 bool allow_gnu_extensions_p;
1254
1255 /* TRUE if the `>' token should be interpreted as the greater-than
1256 operator. FALSE if it is the end of a template-id or
1257 template-parameter-list. */
1258 bool greater_than_is_operator_p;
1259
1260 /* TRUE if default arguments are allowed within a parameter list
1261 that starts at this point. FALSE if only a gnu extension makes
cd0be382 1262 them permissible. */
a723baf1 1263 bool default_arg_ok_p;
21526606 1264
a723baf1
MM
1265 /* TRUE if we are parsing an integral constant-expression. See
1266 [expr.const] for a precise definition. */
67c03833 1267 bool integral_constant_expression_p;
a723baf1 1268
14d22dd6
MM
1269 /* TRUE if we are parsing an integral constant-expression -- but a
1270 non-constant expression should be permitted as well. This flag
1271 is used when parsing an array bound so that GNU variable-length
1272 arrays are tolerated. */
67c03833 1273 bool allow_non_integral_constant_expression_p;
14d22dd6
MM
1274
1275 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1276 been seen that makes the expression non-constant. */
67c03833 1277 bool non_integral_constant_expression_p;
14d22dd6 1278
a723baf1
MM
1279 /* TRUE if local variable names and `this' are forbidden in the
1280 current context. */
1281 bool local_variables_forbidden_p;
1282
1283 /* TRUE if the declaration we are parsing is part of a
1284 linkage-specification of the form `extern string-literal
1285 declaration'. */
1286 bool in_unbraced_linkage_specification_p;
1287
1288 /* TRUE if we are presently parsing a declarator, after the
1289 direct-declarator. */
1290 bool in_declarator_p;
1291
4bb8ca28
MM
1292 /* TRUE if we are presently parsing a template-argument-list. */
1293 bool in_template_argument_list_p;
1294
1799e5d5
RH
1295 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1296 to IN_OMP_BLOCK if parsing OpenMP structured block and
1297 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1298 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1299 iteration-statement, OpenMP block or loop within that switch. */
1300#define IN_SWITCH_STMT 1
1301#define IN_ITERATION_STMT 2
1302#define IN_OMP_BLOCK 4
1303#define IN_OMP_FOR 8
1304 unsigned char in_statement;
1305
1306 /* TRUE if we are presently parsing the body of a switch statement.
1307 Note that this doesn't quite overlap with in_statement above.
1308 The difference relates to giving the right sets of error messages:
1309 "case not in switch" vs "break statement used with OpenMP...". */
0e59b3fb
MM
1310 bool in_switch_statement_p;
1311
4f8163b1
MM
1312 /* TRUE if we are parsing a type-id in an expression context. In
1313 such a situation, both "type (expr)" and "type (type)" are valid
1314 alternatives. */
1315 bool in_type_id_in_expr_p;
1316
7d381002 1317 /* TRUE if we are currently in a header file where declarations are
03fd3f84 1318 implicitly extern "C". */
7d381002
MA
1319 bool implicit_extern_c;
1320
c162c75e
MA
1321 /* TRUE if strings in expressions should be translated to the execution
1322 character set. */
1323 bool translate_strings_p;
1324
a723baf1
MM
1325 /* If non-NULL, then we are parsing a construct where new type
1326 definitions are not permitted. The string stored here will be
1327 issued as an error message if a type is defined. */
1328 const char *type_definition_forbidden_message;
1329
8db1028e
NS
1330 /* A list of lists. The outer list is a stack, used for member
1331 functions of local classes. At each level there are two sub-list,
1332 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1333 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1334 TREE_VALUE's. The functions are chained in reverse declaration
1335 order.
1336
1337 The TREE_PURPOSE sublist contains those functions with default
1338 arguments that need post processing, and the TREE_VALUE sublist
1339 contains those functions with definitions that need post
1340 processing.
1341
1342 These lists can only be processed once the outermost class being
9bcb9aae 1343 defined is complete. */
a723baf1
MM
1344 tree unparsed_functions_queues;
1345
1346 /* The number of classes whose definitions are currently in
1347 progress. */
1348 unsigned num_classes_being_defined;
1349
1350 /* The number of template parameter lists that apply directly to the
1351 current declaration. */
1352 unsigned num_template_parameter_lists;
1353} cp_parser;
1354
a723baf1
MM
1355/* Prototypes. */
1356
1357/* Constructors and destructors. */
1358
1359static cp_parser *cp_parser_new
94edc4ab 1360 (void);
a723baf1 1361
21526606 1362/* Routines to parse various constructs.
a723baf1
MM
1363
1364 Those that return `tree' will return the error_mark_node (rather
1365 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1366 Sometimes, they will return an ordinary node if error-recovery was
34cd5ae7 1367 attempted, even though a parse error occurred. So, to check
a723baf1
MM
1368 whether or not a parse error occurred, you should always use
1369 cp_parser_error_occurred. If the construct is optional (indicated
1370 either by an `_opt' in the name of the function that does the
1371 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1372 the construct is not present. */
1373
1374/* Lexical conventions [gram.lex] */
1375
1376static tree cp_parser_identifier
94edc4ab 1377 (cp_parser *);
c162c75e
MA
1378static tree cp_parser_string_literal
1379 (cp_parser *, bool, bool);
a723baf1
MM
1380
1381/* Basic concepts [gram.basic] */
1382
1383static bool cp_parser_translation_unit
94edc4ab 1384 (cp_parser *);
a723baf1
MM
1385
1386/* Expressions [gram.expr] */
1387
1388static tree cp_parser_primary_expression
02ed62dd 1389 (cp_parser *, bool, bool, bool, cp_id_kind *);
a723baf1 1390static tree cp_parser_id_expression
fa6098f8 1391 (cp_parser *, bool, bool, bool *, bool, bool);
a723baf1 1392static tree cp_parser_unqualified_id
fa6098f8 1393 (cp_parser *, bool, bool, bool, bool);
a723baf1 1394static tree cp_parser_nested_name_specifier_opt
a668c6ad 1395 (cp_parser *, bool, bool, bool, bool);
a723baf1 1396static tree cp_parser_nested_name_specifier
a723baf1 1397 (cp_parser *, bool, bool, bool, bool);
a668c6ad
MM
1398static tree cp_parser_class_or_namespace_name
1399 (cp_parser *, bool, bool, bool, bool, bool);
a723baf1 1400static tree cp_parser_postfix_expression
93678513 1401 (cp_parser *, bool, bool);
7a3ea201
RH
1402static tree cp_parser_postfix_open_square_expression
1403 (cp_parser *, tree, bool);
1404static tree cp_parser_postfix_dot_deref_expression
1405 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
7efa3e22 1406static tree cp_parser_parenthesized_expression_list
93678513 1407 (cp_parser *, bool, bool, bool *);
a723baf1 1408static void cp_parser_pseudo_destructor_name
94edc4ab 1409 (cp_parser *, tree *, tree *);
a723baf1 1410static tree cp_parser_unary_expression
93678513 1411 (cp_parser *, bool, bool);
a723baf1 1412static enum tree_code cp_parser_unary_operator
94edc4ab 1413 (cp_token *);
a723baf1 1414static tree cp_parser_new_expression
94edc4ab 1415 (cp_parser *);
a723baf1 1416static tree cp_parser_new_placement
94edc4ab 1417 (cp_parser *);
a723baf1 1418static tree cp_parser_new_type_id
058b15c1
MM
1419 (cp_parser *, tree *);
1420static cp_declarator *cp_parser_new_declarator_opt
94edc4ab 1421 (cp_parser *);
058b15c1 1422static cp_declarator *cp_parser_direct_new_declarator
94edc4ab 1423 (cp_parser *);
a723baf1 1424static tree cp_parser_new_initializer
94edc4ab 1425 (cp_parser *);
a723baf1 1426static tree cp_parser_delete_expression
94edc4ab 1427 (cp_parser *);
21526606 1428static tree cp_parser_cast_expression
93678513 1429 (cp_parser *, bool, bool);
b8b94c5b 1430static tree cp_parser_binary_expression
93678513 1431 (cp_parser *, bool);
a723baf1 1432static tree cp_parser_question_colon_clause
94edc4ab 1433 (cp_parser *, tree);
a723baf1 1434static tree cp_parser_assignment_expression
93678513 1435 (cp_parser *, bool);
a723baf1 1436static enum tree_code cp_parser_assignment_operator_opt
94edc4ab 1437 (cp_parser *);
a723baf1 1438static tree cp_parser_expression
93678513 1439 (cp_parser *, bool);
a723baf1 1440static tree cp_parser_constant_expression
14d22dd6 1441 (cp_parser *, bool, bool *);
7a3ea201
RH
1442static tree cp_parser_builtin_offsetof
1443 (cp_parser *);
a723baf1
MM
1444
1445/* Statements [gram.stmt.stmt] */
1446
1447static void cp_parser_statement
bc4071dd 1448 (cp_parser *, tree, bool);
a723baf1 1449static tree cp_parser_labeled_statement
bc4071dd 1450 (cp_parser *, tree, bool);
a723baf1 1451static tree cp_parser_expression_statement
325c3691 1452 (cp_parser *, tree);
a723baf1 1453static tree cp_parser_compound_statement
325c3691 1454 (cp_parser *, tree, bool);
a723baf1 1455static void cp_parser_statement_seq_opt
325c3691 1456 (cp_parser *, tree);
a723baf1 1457static tree cp_parser_selection_statement
94edc4ab 1458 (cp_parser *);
a723baf1 1459static tree cp_parser_condition
94edc4ab 1460 (cp_parser *);
a723baf1 1461static tree cp_parser_iteration_statement
94edc4ab 1462 (cp_parser *);
a723baf1 1463static void cp_parser_for_init_statement
94edc4ab 1464 (cp_parser *);
a723baf1 1465static tree cp_parser_jump_statement
94edc4ab 1466 (cp_parser *);
a723baf1 1467static void cp_parser_declaration_statement
94edc4ab 1468 (cp_parser *);
a723baf1
MM
1469
1470static tree cp_parser_implicitly_scoped_statement
94edc4ab 1471 (cp_parser *);
a723baf1 1472static void cp_parser_already_scoped_statement
94edc4ab 1473 (cp_parser *);
a723baf1
MM
1474
1475/* Declarations [gram.dcl.dcl] */
1476
1477static void cp_parser_declaration_seq_opt
94edc4ab 1478 (cp_parser *);
a723baf1 1479static void cp_parser_declaration
94edc4ab 1480 (cp_parser *);
a723baf1 1481static void cp_parser_block_declaration
94edc4ab 1482 (cp_parser *, bool);
a723baf1 1483static void cp_parser_simple_declaration
94edc4ab 1484 (cp_parser *, bool);
62d1db17
MM
1485static void cp_parser_decl_specifier_seq
1486 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
a723baf1 1487static tree cp_parser_storage_class_specifier_opt
94edc4ab 1488 (cp_parser *);
a723baf1 1489static tree cp_parser_function_specifier_opt
62d1db17 1490 (cp_parser *, cp_decl_specifier_seq *);
a723baf1 1491static tree cp_parser_type_specifier
98ca843c 1492 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
62d1db17 1493 int *, bool *);
a723baf1 1494static tree cp_parser_simple_type_specifier
62d1db17 1495 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
a723baf1 1496static tree cp_parser_type_name
94edc4ab 1497 (cp_parser *);
a723baf1 1498static tree cp_parser_elaborated_type_specifier
94edc4ab 1499 (cp_parser *, bool, bool);
a723baf1 1500static tree cp_parser_enum_specifier
94edc4ab 1501 (cp_parser *);
a723baf1 1502static void cp_parser_enumerator_list
94edc4ab 1503 (cp_parser *, tree);
21526606 1504static void cp_parser_enumerator_definition
94edc4ab 1505 (cp_parser *, tree);
a723baf1 1506static tree cp_parser_namespace_name
94edc4ab 1507 (cp_parser *);
a723baf1 1508static void cp_parser_namespace_definition
94edc4ab 1509 (cp_parser *);
a723baf1 1510static void cp_parser_namespace_body
94edc4ab 1511 (cp_parser *);
a723baf1 1512static tree cp_parser_qualified_namespace_specifier
94edc4ab 1513 (cp_parser *);
a723baf1 1514static void cp_parser_namespace_alias_definition
94edc4ab 1515 (cp_parser *);
a723baf1 1516static void cp_parser_using_declaration
94edc4ab 1517 (cp_parser *);
a723baf1 1518static void cp_parser_using_directive
94edc4ab 1519 (cp_parser *);
a723baf1 1520static void cp_parser_asm_definition
94edc4ab 1521 (cp_parser *);
a723baf1 1522static void cp_parser_linkage_specification
94edc4ab 1523 (cp_parser *);
a723baf1
MM
1524
1525/* Declarators [gram.dcl.decl] */
1526
1527static tree cp_parser_init_declarator
6b648482 1528 (cp_parser *, cp_decl_specifier_seq *, tree, bool, bool, int, bool *);
058b15c1 1529static cp_declarator *cp_parser_declarator
db86dd14 1530 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
058b15c1 1531static cp_declarator *cp_parser_direct_declarator
db86dd14 1532 (cp_parser *, cp_parser_declarator_kind, int *, bool);
a723baf1 1533static enum tree_code cp_parser_ptr_operator
3c01e5df
MM
1534 (cp_parser *, tree *, cp_cv_quals *);
1535static cp_cv_quals cp_parser_cv_qualifier_seq_opt
94edc4ab 1536 (cp_parser *);
a723baf1 1537static tree cp_parser_declarator_id
fa6098f8 1538 (cp_parser *, bool);
a723baf1 1539static tree cp_parser_type_id
94edc4ab 1540 (cp_parser *);
62d1db17 1541static void cp_parser_type_specifier_seq
d4113656 1542 (cp_parser *, bool, cp_decl_specifier_seq *);
058b15c1 1543static cp_parameter_declarator *cp_parser_parameter_declaration_clause
94edc4ab 1544 (cp_parser *);
058b15c1
MM
1545static cp_parameter_declarator *cp_parser_parameter_declaration_list
1546 (cp_parser *, bool *);
1547static cp_parameter_declarator *cp_parser_parameter_declaration
4bb8ca28 1548 (cp_parser *, bool, bool *);
a723baf1
MM
1549static void cp_parser_function_body
1550 (cp_parser *);
1551static tree cp_parser_initializer
39703eb9 1552 (cp_parser *, bool *, bool *);
a723baf1 1553static tree cp_parser_initializer_clause
39703eb9 1554 (cp_parser *, bool *);
4038c495 1555static VEC(constructor_elt,gc) *cp_parser_initializer_list
39703eb9 1556 (cp_parser *, bool *);
a723baf1
MM
1557
1558static bool cp_parser_ctor_initializer_opt_and_function_body
1559 (cp_parser *);
1560
1561/* Classes [gram.class] */
1562
1563static tree cp_parser_class_name
fc6a28d7 1564 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
a723baf1 1565static tree cp_parser_class_specifier
94edc4ab 1566 (cp_parser *);
a723baf1 1567static tree cp_parser_class_head
38b305d0 1568 (cp_parser *, bool *, tree *);
a723baf1 1569static enum tag_types cp_parser_class_key
94edc4ab 1570 (cp_parser *);
a723baf1 1571static void cp_parser_member_specification_opt
94edc4ab 1572 (cp_parser *);
a723baf1 1573static void cp_parser_member_declaration
94edc4ab 1574 (cp_parser *);
a723baf1 1575static tree cp_parser_pure_specifier
94edc4ab 1576 (cp_parser *);
a723baf1 1577static tree cp_parser_constant_initializer
94edc4ab 1578 (cp_parser *);
a723baf1
MM
1579
1580/* Derived classes [gram.class.derived] */
1581
1582static tree cp_parser_base_clause
94edc4ab 1583 (cp_parser *);
a723baf1 1584static tree cp_parser_base_specifier
94edc4ab 1585 (cp_parser *);
a723baf1
MM
1586
1587/* Special member functions [gram.special] */
1588
1589static tree cp_parser_conversion_function_id
94edc4ab 1590 (cp_parser *);
a723baf1 1591static tree cp_parser_conversion_type_id
94edc4ab 1592 (cp_parser *);
058b15c1 1593static cp_declarator *cp_parser_conversion_declarator_opt
94edc4ab 1594 (cp_parser *);
a723baf1 1595static bool cp_parser_ctor_initializer_opt
94edc4ab 1596 (cp_parser *);
a723baf1 1597static void cp_parser_mem_initializer_list
94edc4ab 1598 (cp_parser *);
a723baf1 1599static tree cp_parser_mem_initializer
94edc4ab 1600 (cp_parser *);
a723baf1 1601static tree cp_parser_mem_initializer_id
94edc4ab 1602 (cp_parser *);
a723baf1
MM
1603
1604/* Overloading [gram.over] */
1605
1606static tree cp_parser_operator_function_id
94edc4ab 1607 (cp_parser *);
a723baf1 1608static tree cp_parser_operator
94edc4ab 1609 (cp_parser *);
a723baf1
MM
1610
1611/* Templates [gram.temp] */
1612
1613static void cp_parser_template_declaration
94edc4ab 1614 (cp_parser *, bool);
a723baf1 1615static tree cp_parser_template_parameter_list
94edc4ab 1616 (cp_parser *);
a723baf1 1617static tree cp_parser_template_parameter
058b15c1 1618 (cp_parser *, bool *);
a723baf1 1619static tree cp_parser_type_parameter
94edc4ab 1620 (cp_parser *);
a723baf1 1621static tree cp_parser_template_id
a668c6ad 1622 (cp_parser *, bool, bool, bool);
a723baf1 1623static tree cp_parser_template_name
a668c6ad 1624 (cp_parser *, bool, bool, bool, bool *);
a723baf1 1625static tree cp_parser_template_argument_list
94edc4ab 1626 (cp_parser *);
a723baf1 1627static tree cp_parser_template_argument
94edc4ab 1628 (cp_parser *);
a723baf1 1629static void cp_parser_explicit_instantiation
94edc4ab 1630 (cp_parser *);
a723baf1 1631static void cp_parser_explicit_specialization
94edc4ab 1632 (cp_parser *);
a723baf1
MM
1633
1634/* Exception handling [gram.exception] */
1635
21526606 1636static tree cp_parser_try_block
94edc4ab 1637 (cp_parser *);
a723baf1 1638static bool cp_parser_function_try_block
94edc4ab 1639 (cp_parser *);
a723baf1 1640static void cp_parser_handler_seq
94edc4ab 1641 (cp_parser *);
a723baf1 1642static void cp_parser_handler
94edc4ab 1643 (cp_parser *);
a723baf1 1644static tree cp_parser_exception_declaration
94edc4ab 1645 (cp_parser *);
a723baf1 1646static tree cp_parser_throw_expression
94edc4ab 1647 (cp_parser *);
a723baf1 1648static tree cp_parser_exception_specification_opt
94edc4ab 1649 (cp_parser *);
a723baf1 1650static tree cp_parser_type_id_list
94edc4ab 1651 (cp_parser *);
a723baf1
MM
1652
1653/* GNU Extensions */
1654
1655static tree cp_parser_asm_specification_opt
94edc4ab 1656 (cp_parser *);
a723baf1 1657static tree cp_parser_asm_operand_list
94edc4ab 1658 (cp_parser *);
a723baf1 1659static tree cp_parser_asm_clobber_list
94edc4ab 1660 (cp_parser *);
a723baf1 1661static tree cp_parser_attributes_opt
94edc4ab 1662 (cp_parser *);
a723baf1 1663static tree cp_parser_attribute_list
94edc4ab 1664 (cp_parser *);
a723baf1 1665static bool cp_parser_extension_opt
94edc4ab 1666 (cp_parser *, int *);
a723baf1 1667static void cp_parser_label_declaration
94edc4ab 1668 (cp_parser *);
a723baf1 1669
bc4071dd
RH
1670enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1671static bool cp_parser_pragma
1672 (cp_parser *, enum pragma_context);
1673
e58a9aa1
ZL
1674/* Objective-C++ Productions */
1675
1676static tree cp_parser_objc_message_receiver
1677 (cp_parser *);
1678static tree cp_parser_objc_message_args
1679 (cp_parser *);
1680static tree cp_parser_objc_message_expression
1681 (cp_parser *);
1682static tree cp_parser_objc_encode_expression
1683 (cp_parser *);
c8094d83 1684static tree cp_parser_objc_defs_expression
e58a9aa1
ZL
1685 (cp_parser *);
1686static tree cp_parser_objc_protocol_expression
1687 (cp_parser *);
1688static tree cp_parser_objc_selector_expression
1689 (cp_parser *);
1690static tree cp_parser_objc_expression
1691 (cp_parser *);
1692static bool cp_parser_objc_selector_p
1693 (enum cpp_ttype);
1694static tree cp_parser_objc_selector
1695 (cp_parser *);
1696static tree cp_parser_objc_protocol_refs_opt
1697 (cp_parser *);
1698static void cp_parser_objc_declaration
1699 (cp_parser *);
1700static tree cp_parser_objc_statement
1701 (cp_parser *);
1702
a723baf1
MM
1703/* Utility Routines */
1704
1705static tree cp_parser_lookup_name
91b1ca65 1706 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
a723baf1 1707static tree cp_parser_lookup_name_simple
94edc4ab 1708 (cp_parser *, tree);
a723baf1
MM
1709static tree cp_parser_maybe_treat_template_as_class
1710 (tree, bool);
1711static bool cp_parser_check_declarator_template_parameters
058b15c1 1712 (cp_parser *, cp_declarator *);
a723baf1 1713static bool cp_parser_check_template_parameters
94edc4ab 1714 (cp_parser *, unsigned);
d6b4ea85
MM
1715static tree cp_parser_simple_cast_expression
1716 (cp_parser *);
a723baf1 1717static tree cp_parser_global_scope_opt
94edc4ab 1718 (cp_parser *, bool);
a723baf1
MM
1719static bool cp_parser_constructor_declarator_p
1720 (cp_parser *, bool);
1721static tree cp_parser_function_definition_from_specifiers_and_declarator
62d1db17 1722 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
a723baf1 1723static tree cp_parser_function_definition_after_declarator
94edc4ab 1724 (cp_parser *, bool);
a723baf1 1725static void cp_parser_template_declaration_after_export
94edc4ab 1726 (cp_parser *, bool);
6b648482
MM
1727static void cp_parser_perform_template_parameter_access_checks
1728 (tree);
a723baf1 1729static tree cp_parser_single_declaration
6b648482 1730 (cp_parser *, tree, 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 1747static void cp_parser_set_storage_class
bfce0853 1748 (cp_parser *, cp_decl_specifier_seq *, enum rid);
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
2cfe82fe
ZW
1856/* If not parsing tentatively, issue a diagnostic of the form
1857 FILE:LINE: MESSAGE before TOKEN
1858 where TOKEN is the next token in the input stream. MESSAGE
1859 (specified by the caller) is usually of the form "expected
1860 OTHER-TOKEN". */
a723baf1
MM
1861
1862static void
94edc4ab 1863cp_parser_error (cp_parser* parser, const char* message)
a723baf1 1864{
e5976695 1865 if (!cp_parser_simulate_error (parser))
4bb8ca28 1866 {
2cfe82fe
ZW
1867 cp_token *token = cp_lexer_peek_token (parser->lexer);
1868 /* This diagnostic makes more sense if it is tagged to the line
1869 of the token we just peeked at. */
1870 cp_lexer_set_source_position_from_token (token);
bc4071dd 1871
0d63048c
MM
1872 if (token->type == CPP_PRAGMA)
1873 {
c8094d83 1874 error ("%<#pragma%> is not allowed here");
bc4071dd 1875 cp_parser_skip_to_pragma_eol (parser, token);
0d63048c
MM
1876 return;
1877 }
bc4071dd 1878
21526606 1879 c_parse_error (message,
5c832178
MM
1880 /* Because c_parser_error does not understand
1881 CPP_KEYWORD, keywords are treated like
1882 identifiers. */
21526606 1883 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
5c832178 1884 token->value);
4bb8ca28
MM
1885 }
1886}
1887
1888/* Issue an error about name-lookup failing. NAME is the
1889 IDENTIFIER_NODE DECL is the result of
1890 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1891 the thing that we hoped to find. */
1892
1893static void
1894cp_parser_name_lookup_error (cp_parser* parser,
1895 tree name,
1896 tree decl,
1897 const char* desired)
1898{
1899 /* If name lookup completely failed, tell the user that NAME was not
1900 declared. */
1901 if (decl == error_mark_node)
1902 {
1903 if (parser->scope && parser->scope != global_namespace)
2a13a625 1904 error ("%<%D::%D%> has not been declared",
4bb8ca28
MM
1905 parser->scope, name);
1906 else if (parser->scope == global_namespace)
2a13a625 1907 error ("%<::%D%> has not been declared", name);
c8094d83 1908 else if (parser->object_scope
b14454ba 1909 && !CLASS_TYPE_P (parser->object_scope))
2a13a625 1910 error ("request for member %qD in non-class type %qT",
b14454ba
MM
1911 name, parser->object_scope);
1912 else if (parser->object_scope)
c8094d83 1913 error ("%<%T::%D%> has not been declared",
b14454ba 1914 parser->object_scope, name);
4bb8ca28 1915 else
9e637a26 1916 error ("%qD has not been declared", name);
4bb8ca28
MM
1917 }
1918 else if (parser->scope && parser->scope != global_namespace)
2a13a625 1919 error ("%<%D::%D%> %s", parser->scope, name, desired);
4bb8ca28 1920 else if (parser->scope == global_namespace)
2a13a625 1921 error ("%<::%D%> %s", name, desired);
4bb8ca28 1922 else
2a13a625 1923 error ("%qD %s", name, desired);
a723baf1
MM
1924}
1925
1926/* If we are parsing tentatively, remember that an error has occurred
e5976695 1927 during this tentative parse. Returns true if the error was
77077b39 1928 simulated; false if a message should be issued by the caller. */
a723baf1 1929
e5976695 1930static bool
94edc4ab 1931cp_parser_simulate_error (cp_parser* parser)
a723baf1 1932{
0b16f8f4 1933 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
e5976695
MM
1934 {
1935 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1936 return true;
1937 }
1938 return false;
a723baf1
MM
1939}
1940
856367df
VR
1941/* Check for repeated decl-specifiers. */
1942
1943static void
1944cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
1945{
1946 cp_decl_spec ds;
1947
1948 for (ds = ds_first; ds != ds_last; ++ds)
1949 {
1950 unsigned count = decl_specs->specs[(int)ds];
1951 if (count < 2)
1952 continue;
1953 /* The "long" specifier is a special case because of "long long". */
1954 if (ds == ds_long)
1955 {
1956 if (count > 2)
1957 error ("%<long long long%> is too long for GCC");
1958 else if (pedantic && !in_system_header && warn_long_long)
1959 pedwarn ("ISO C++ does not support %<long long%>");
1960 }
1961 else if (count > 1)
1962 {
1963 static const char *const decl_spec_names[] = {
1964 "signed",
1965 "unsigned",
1966 "short",
1967 "long",
1968 "const",
1969 "volatile",
1970 "restrict",
1971 "inline",
1972 "virtual",
1973 "explicit",
1974 "friend",
1975 "typedef",
1976 "__complex",
1977 "__thread"
1978 };
1979 error ("duplicate %qs", decl_spec_names[(int)ds]);
1980 }
1981 }
1982}
1983
a723baf1
MM
1984/* This function is called when a type is defined. If type
1985 definitions are forbidden at this point, an error message is
1986 issued. */
1987
1988static void
94edc4ab 1989cp_parser_check_type_definition (cp_parser* parser)
a723baf1
MM
1990{
1991 /* If types are forbidden here, issue a message. */
1992 if (parser->type_definition_forbidden_message)
1993 /* Use `%s' to print the string in case there are any escape
1994 characters in the message. */
1995 error ("%s", parser->type_definition_forbidden_message);
1996}
1997
fc6a28d7 1998/* This function is called when the DECLARATOR is processed. The TYPE
472c29c3 1999 was a type defined in the decl-specifiers. If it is invalid to
fc6a28d7
MM
2000 define a type in the decl-specifiers for DECLARATOR, an error is
2001 issued. */
560ad596
MM
2002
2003static void
058b15c1 2004cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
fc6a28d7 2005 tree type)
560ad596
MM
2006{
2007 /* [dcl.fct] forbids type definitions in return types.
2008 Unfortunately, it's not easy to know whether or not we are
2009 processing a return type until after the fact. */
2010 while (declarator
058b15c1
MM
2011 && (declarator->kind == cdk_pointer
2012 || declarator->kind == cdk_reference
2013 || declarator->kind == cdk_ptrmem))
2014 declarator = declarator->declarator;
560ad596 2015 if (declarator
fc6a28d7
MM
2016 && declarator->kind == cdk_function)
2017 {
2018 error ("new types may not be defined in a return type");
2019 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2020 type);
2021 }
560ad596
MM
2022}
2023
ee43dab5
MM
2024/* A type-specifier (TYPE) has been parsed which cannot be followed by
2025 "<" in any valid C++ program. If the next token is indeed "<",
2026 issue a message warning the user about what appears to be an
2027 invalid attempt to form a template-id. */
2028
2029static void
21526606 2030cp_parser_check_for_invalid_template_id (cp_parser* parser,
ee43dab5
MM
2031 tree type)
2032{
0c5e4866 2033 cp_token_position start = 0;
ee43dab5
MM
2034
2035 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2036 {
2037 if (TYPE_P (type))
2a13a625 2038 error ("%qT is not a template", type);
ee43dab5 2039 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2a13a625 2040 error ("%qE is not a template", type);
ee43dab5
MM
2041 else
2042 error ("invalid template-id");
2043 /* Remember the location of the invalid "<". */
0b16f8f4 2044 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
0c5e4866 2045 start = cp_lexer_token_position (parser->lexer, true);
ee43dab5
MM
2046 /* Consume the "<". */
2047 cp_lexer_consume_token (parser->lexer);
2048 /* Parse the template arguments. */
2049 cp_parser_enclosed_template_argument_list (parser);
da1d7781 2050 /* Permanently remove the invalid template arguments so that
ee43dab5 2051 this error message is not issued again. */
0c5e4866
NS
2052 if (start)
2053 cp_lexer_purge_tokens_after (parser->lexer, start);
ee43dab5
MM
2054 }
2055}
2056
625cbf93
MM
2057/* If parsing an integral constant-expression, issue an error message
2058 about the fact that THING appeared and return true. Otherwise,
93678513 2059 return false. In either case, set
c8094d83 2060 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
14d22dd6 2061
625cbf93
MM
2062static bool
2063cp_parser_non_integral_constant_expression (cp_parser *parser,
2064 const char *thing)
14d22dd6 2065{
93678513 2066 parser->non_integral_constant_expression_p = true;
625cbf93
MM
2067 if (parser->integral_constant_expression_p)
2068 {
2069 if (!parser->allow_non_integral_constant_expression_p)
2070 {
2071 error ("%s cannot appear in a constant-expression", thing);
2072 return true;
2073 }
625cbf93
MM
2074 }
2075 return false;
14d22dd6
MM
2076}
2077
0c88d886 2078/* Emit a diagnostic for an invalid type name. SCOPE is the
6ca2d67f
MM
2079 qualifying scope (or NULL, if none) for ID. This function commits
2080 to the current active tentative parse, if any. (Otherwise, the
2081 problematic construct might be encountered again later, resulting
2082 in duplicate error messages.) */
8fbc5ae7 2083
2097b5f2
GB
2084static void
2085cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
6c0cc713
GB
2086{
2087 tree decl, old_scope;
2097b5f2
GB
2088 /* Try to lookup the identifier. */
2089 old_scope = parser->scope;
2090 parser->scope = scope;
2091 decl = cp_parser_lookup_name_simple (parser, id);
2092 parser->scope = old_scope;
2093 /* If the lookup found a template-name, it means that the user forgot
c72a1a86 2094 to specify an argument list. Emit a useful error message. */
2097b5f2 2095 if (TREE_CODE (decl) == TEMPLATE_DECL)
04499540
VR
2096 error ("invalid use of template-name %qE without an argument list", decl);
2097 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2098 error ("invalid use of destructor %qD as a type", id);
91b1ca65 2099 else if (!parser->scope)
8fbc5ae7 2100 {
8fbc5ae7 2101 /* Issue an error message. */
2a13a625 2102 error ("%qE does not name a type", id);
8fbc5ae7
MM
2103 /* If we're in a template class, it's possible that the user was
2104 referring to a type from a base class. For example:
2105
2106 template <typename T> struct A { typedef T X; };
2107 template <typename T> struct B : public A<T> { X x; };
2108
2109 The user should have said "typename A<T>::X". */
32db39c0
PC
2110 if (processing_template_decl && current_class_type
2111 && TYPE_BINFO (current_class_type))
8fbc5ae7
MM
2112 {
2113 tree b;
2114
2115 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2116 b;
2117 b = TREE_CHAIN (b))
2118 {
2119 tree base_type = BINFO_TYPE (b);
21526606 2120 if (CLASS_TYPE_P (base_type)
1fb3244a 2121 && dependent_type_p (base_type))
8fbc5ae7
MM
2122 {
2123 tree field;
2124 /* Go from a particular instantiation of the
2125 template (which will have an empty TYPE_FIELDs),
2126 to the main version. */
353b4fc0 2127 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
8fbc5ae7
MM
2128 for (field = TYPE_FIELDS (base_type);
2129 field;
2130 field = TREE_CHAIN (field))
2131 if (TREE_CODE (field) == TYPE_DECL
2097b5f2 2132 && DECL_NAME (field) == id)
8fbc5ae7 2133 {
c4f73174 2134 inform ("(perhaps %<typename %T::%E%> was intended)",
0cbd7506 2135 BINFO_TYPE (b), id);
8fbc5ae7
MM
2136 break;
2137 }
2138 if (field)
2139 break;
2140 }
2141 }
2142 }
8fbc5ae7 2143 }
2097b5f2
GB
2144 /* Here we diagnose qualified-ids where the scope is actually correct,
2145 but the identifier does not resolve to a valid type name. */
91b1ca65 2146 else if (parser->scope != error_mark_node)
2097b5f2
GB
2147 {
2148 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2a13a625 2149 error ("%qE in namespace %qE does not name a type",
2097b5f2
GB
2150 id, parser->scope);
2151 else if (TYPE_P (parser->scope))
2fbe4889 2152 error ("%qE in class %qT does not name a type", id, parser->scope);
2097b5f2 2153 else
315fb5db 2154 gcc_unreachable ();
2097b5f2 2155 }
6ca2d67f 2156 cp_parser_commit_to_tentative_parse (parser);
2097b5f2 2157}
8fbc5ae7 2158
2097b5f2
GB
2159/* Check for a common situation where a type-name should be present,
2160 but is not, and issue a sensible error message. Returns true if an
2161 invalid type-name was detected.
21526606 2162
2097b5f2 2163 The situation handled by this function are variable declarations of the
21526606
EC
2164 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2165 Usually, `ID' should name a type, but if we got here it means that it
2097b5f2 2166 does not. We try to emit the best possible error message depending on
3db45ab5 2167 how exactly the id-expression looks like. */
2097b5f2
GB
2168
2169static bool
2170cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2171{
2172 tree id;
2173
2174 cp_parser_parse_tentatively (parser);
21526606 2175 id = cp_parser_id_expression (parser,
2097b5f2
GB
2176 /*template_keyword_p=*/false,
2177 /*check_dependency_p=*/true,
2178 /*template_p=*/NULL,
fa6098f8
MM
2179 /*declarator_p=*/true,
2180 /*optional_p=*/false);
2097b5f2
GB
2181 /* After the id-expression, there should be a plain identifier,
2182 otherwise this is not a simple variable declaration. Also, if
2183 the scope is dependent, we cannot do much. */
2184 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 2185 || (parser->scope && TYPE_P (parser->scope)
2097b5f2
GB
2186 && dependent_type_p (parser->scope)))
2187 {
2188 cp_parser_abort_tentative_parse (parser);
2189 return false;
2190 }
04499540 2191 if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2097b5f2
GB
2192 return false;
2193
2097b5f2
GB
2194 /* Emit a diagnostic for the invalid type. */
2195 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2196 /* Skip to the end of the declaration; there's no point in
2197 trying to process it. */
2198 cp_parser_skip_to_end_of_block_or_statement (parser);
2199 return true;
8fbc5ae7
MM
2200}
2201
21526606 2202/* Consume tokens up to, and including, the next non-nested closing `)'.
7efa3e22
NS
2203 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2204 are doing error recovery. Returns -1 if OR_COMMA is true and we
2205 found an unnested comma. */
a723baf1 2206
7efa3e22
NS
2207static int
2208cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
21526606 2209 bool recovering,
a668c6ad
MM
2210 bool or_comma,
2211 bool consume_paren)
a723baf1 2212{
7efa3e22
NS
2213 unsigned paren_depth = 0;
2214 unsigned brace_depth = 0;
a723baf1 2215
0b16f8f4
VR
2216 if (recovering && !or_comma
2217 && cp_parser_uncommitted_to_tentative_parse_p (parser))
7efa3e22 2218 return 0;
21526606 2219
a723baf1
MM
2220 while (true)
2221 {
bc4071dd 2222 cp_token * token = cp_lexer_peek_token (parser->lexer);
21526606 2223
bc4071dd 2224 switch (token->type)
0173bb6f 2225 {
bc4071dd
RH
2226 case CPP_EOF:
2227 case CPP_PRAGMA_EOL:
2228 /* If we've run out of tokens, then there is no closing `)'. */
2229 return 0;
a723baf1 2230
bc4071dd
RH
2231 case CPP_SEMICOLON:
2232 /* This matches the processing in skip_to_end_of_statement. */
2233 if (!brace_depth)
2234 return 0;
2235 break;
21526606 2236
bc4071dd
RH
2237 case CPP_OPEN_BRACE:
2238 ++brace_depth;
0173bb6f 2239 break;
bc4071dd 2240 case CPP_CLOSE_BRACE:
a668c6ad 2241 if (!brace_depth--)
bc4071dd 2242 return 0;
0173bb6f 2243 break;
21526606 2244
bc4071dd
RH
2245 case CPP_COMMA:
2246 if (recovering && or_comma && !brace_depth && !paren_depth)
2247 return -1;
2248 break;
2249
2250 case CPP_OPEN_PAREN:
2251 if (!brace_depth)
7efa3e22 2252 ++paren_depth;
bc4071dd
RH
2253 break;
2254
2255 case CPP_CLOSE_PAREN:
2256 if (!brace_depth && !paren_depth--)
a668c6ad
MM
2257 {
2258 if (consume_paren)
2259 cp_lexer_consume_token (parser->lexer);
bc4071dd 2260 return 1;
a668c6ad 2261 }
bc4071dd
RH
2262 break;
2263
2264 default:
2265 break;
7efa3e22 2266 }
21526606 2267
a668c6ad
MM
2268 /* Consume the token. */
2269 cp_lexer_consume_token (parser->lexer);
a723baf1
MM
2270 }
2271}
2272
2273/* Consume tokens until we reach the end of the current statement.
2274 Normally, that will be just before consuming a `;'. However, if a
2275 non-nested `}' comes first, then we stop before consuming that. */
2276
2277static void
94edc4ab 2278cp_parser_skip_to_end_of_statement (cp_parser* parser)
a723baf1
MM
2279{
2280 unsigned nesting_depth = 0;
2281
2282 while (true)
2283 {
bc4071dd 2284 cp_token *token = cp_lexer_peek_token (parser->lexer);
a723baf1 2285
bc4071dd 2286 switch (token->type)
a723baf1 2287 {
bc4071dd
RH
2288 case CPP_EOF:
2289 case CPP_PRAGMA_EOL:
2290 /* If we've run out of tokens, stop. */
2291 return;
2292
2293 case CPP_SEMICOLON:
2294 /* If the next token is a `;', we have reached the end of the
2295 statement. */
2296 if (!nesting_depth)
2297 return;
2298 break;
2299
2300 case CPP_CLOSE_BRACE:
2301 /* If this is a non-nested '}', stop before consuming it.
a723baf1
MM
2302 That way, when confronted with something like:
2303
21526606 2304 { 3 + }
a723baf1 2305
bc4071dd 2306 we stop before consuming the closing '}', even though we
a723baf1
MM
2307 have not yet reached a `;'. */
2308 if (nesting_depth == 0)
bc4071dd
RH
2309 return;
2310
2311 /* If it is the closing '}' for a block that we have
a723baf1
MM
2312 scanned, stop -- but only after consuming the token.
2313 That way given:
2314
0cbd7506 2315 void f g () { ... }
a723baf1
MM
2316 typedef int I;
2317
2318 we will stop after the body of the erroneously declared
2319 function, but before consuming the following `typedef'
2320 declaration. */
2321 if (--nesting_depth == 0)
2322 {
2323 cp_lexer_consume_token (parser->lexer);
bc4071dd 2324 return;
a723baf1 2325 }
bc4071dd
RH
2326
2327 case CPP_OPEN_BRACE:
2328 ++nesting_depth;
2329 break;
2330
2331 default:
2332 break;
a723baf1 2333 }
bc4071dd 2334
a723baf1
MM
2335 /* Consume the token. */
2336 cp_lexer_consume_token (parser->lexer);
2337 }
2338}
2339
e0860732
MM
2340/* This function is called at the end of a statement or declaration.
2341 If the next token is a semicolon, it is consumed; otherwise, error
2342 recovery is attempted. */
2343
2344static void
2345cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2346{
2347 /* Look for the trailing `;'. */
2348 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2349 {
2350 /* If there is additional (erroneous) input, skip to the end of
2351 the statement. */
2352 cp_parser_skip_to_end_of_statement (parser);
2353 /* If the next token is now a `;', consume it. */
2354 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2355 cp_lexer_consume_token (parser->lexer);
2356 }
2357}
2358
a723baf1
MM
2359/* Skip tokens until we have consumed an entire block, or until we
2360 have consumed a non-nested `;'. */
2361
2362static void
94edc4ab 2363cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
a723baf1 2364{
8fe4d24b 2365 int nesting_depth = 0;
a723baf1 2366
8fe4d24b 2367 while (nesting_depth >= 0)
a723baf1 2368 {
8fe4d24b 2369 cp_token *token = cp_lexer_peek_token (parser->lexer);
c8094d83 2370
8fe4d24b 2371 switch (token->type)
a723baf1 2372 {
8fe4d24b 2373 case CPP_EOF:
bc4071dd 2374 case CPP_PRAGMA_EOL:
8fe4d24b 2375 /* If we've run out of tokens, stop. */
bc4071dd 2376 return;
8fe4d24b
NS
2377
2378 case CPP_SEMICOLON:
2379 /* Stop if this is an unnested ';'. */
2380 if (!nesting_depth)
2381 nesting_depth = -1;
2382 break;
2383
2384 case CPP_CLOSE_BRACE:
2385 /* Stop if this is an unnested '}', or closes the outermost
2386 nesting level. */
2387 nesting_depth--;
2388 if (!nesting_depth)
2389 nesting_depth = -1;
2390 break;
c8094d83 2391
8fe4d24b
NS
2392 case CPP_OPEN_BRACE:
2393 /* Nest. */
2394 nesting_depth++;
2395 break;
2396
2397 default:
a723baf1
MM
2398 break;
2399 }
c8094d83 2400
a723baf1 2401 /* Consume the token. */
8fe4d24b 2402 cp_lexer_consume_token (parser->lexer);
a723baf1
MM
2403 }
2404}
2405
2406/* Skip tokens until a non-nested closing curly brace is the next
2407 token. */
2408
2409static void
2410cp_parser_skip_to_closing_brace (cp_parser *parser)
2411{
2412 unsigned nesting_depth = 0;
2413
2414 while (true)
2415 {
bc4071dd
RH
2416 cp_token *token = cp_lexer_peek_token (parser->lexer);
2417
2418 switch (token->type)
2419 {
2420 case CPP_EOF:
2421 case CPP_PRAGMA_EOL:
2422 /* If we've run out of tokens, stop. */
2423 return;
2424
2425 case CPP_CLOSE_BRACE:
2426 /* If the next token is a non-nested `}', then we have reached
2427 the end of the current block. */
2428 if (nesting_depth-- == 0)
2429 return;
2430 break;
2431
2432 case CPP_OPEN_BRACE:
2433 /* If it the next token is a `{', then we are entering a new
2434 block. Consume the entire block. */
2435 ++nesting_depth;
2436 break;
2437
2438 default:
2439 break;
2440 }
a723baf1 2441
a723baf1
MM
2442 /* Consume the token. */
2443 cp_lexer_consume_token (parser->lexer);
2444 }
2445}
2446
bc4071dd
RH
2447/* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2448 parameter is the PRAGMA token, allowing us to purge the entire pragma
2449 sequence. */
2450
2451static void
2452cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2453{
2454 cp_token *token;
2455
2456 parser->lexer->in_pragma = false;
2457
2458 do
2459 token = cp_lexer_consume_token (parser->lexer);
2460 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2461
2462 /* Ensure that the pragma is not parsed again. */
2463 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2464}
2465
1799e5d5
RH
2466/* Require pragma end of line, resyncing with it as necessary. The
2467 arguments are as for cp_parser_skip_to_pragma_eol. */
2468
2469static void
2470cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2471{
2472 parser->lexer->in_pragma = false;
2473 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2474 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2475}
2476
2097b5f2
GB
2477/* This is a simple wrapper around make_typename_type. When the id is
2478 an unresolved identifier node, we can provide a superior diagnostic
2479 using cp_parser_diagnose_invalid_type_name. */
2480
2481static tree
2482cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
6c0cc713
GB
2483{
2484 tree result;
2485 if (TREE_CODE (id) == IDENTIFIER_NODE)
2486 {
fc6a28d7 2487 result = make_typename_type (scope, id, typename_type,
3db45ab5 2488 /*complain=*/tf_none);
6c0cc713
GB
2489 if (result == error_mark_node)
2490 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2491 return result;
2492 }
fc6a28d7 2493 return make_typename_type (scope, id, typename_type, tf_error);
2097b5f2
GB
2494}
2495
2496
a723baf1
MM
2497/* Create a new C++ parser. */
2498
2499static cp_parser *
94edc4ab 2500cp_parser_new (void)
a723baf1
MM
2501{
2502 cp_parser *parser;
17211ab5 2503 cp_lexer *lexer;
b8b94c5b 2504 unsigned i;
17211ab5
GK
2505
2506 /* cp_lexer_new_main is called before calling ggc_alloc because
2507 cp_lexer_new_main might load a PCH file. */
2508 lexer = cp_lexer_new_main ();
a723baf1 2509
b8b94c5b
PB
2510 /* Initialize the binops_by_token so that we can get the tree
2511 directly from the token. */
2512 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2513 binops_by_token[binops[i].token_type] = binops[i];
2514
99dd239f 2515 parser = GGC_CNEW (cp_parser);
17211ab5 2516 parser->lexer = lexer;
a723baf1
MM
2517 parser->context = cp_parser_context_new (NULL);
2518
2519 /* For now, we always accept GNU extensions. */
2520 parser->allow_gnu_extensions_p = 1;
2521
2522 /* The `>' token is a greater-than operator, not the end of a
2523 template-id. */
2524 parser->greater_than_is_operator_p = true;
2525
2526 parser->default_arg_ok_p = true;
21526606 2527
a723baf1 2528 /* We are not parsing a constant-expression. */
67c03833
JM
2529 parser->integral_constant_expression_p = false;
2530 parser->allow_non_integral_constant_expression_p = false;
2531 parser->non_integral_constant_expression_p = false;
a723baf1
MM
2532
2533 /* Local variable names are not forbidden. */
2534 parser->local_variables_forbidden_p = false;
2535
34cd5ae7 2536 /* We are not processing an `extern "C"' declaration. */
a723baf1
MM
2537 parser->in_unbraced_linkage_specification_p = false;
2538
2539 /* We are not processing a declarator. */
2540 parser->in_declarator_p = false;
2541
4bb8ca28
MM
2542 /* We are not processing a template-argument-list. */
2543 parser->in_template_argument_list_p = false;
2544
0e59b3fb 2545 /* We are not in an iteration statement. */
1799e5d5 2546 parser->in_statement = 0;
0e59b3fb
MM
2547
2548 /* We are not in a switch statement. */
2549 parser->in_switch_statement_p = false;
2550
4f8163b1
MM
2551 /* We are not parsing a type-id inside an expression. */
2552 parser->in_type_id_in_expr_p = false;
2553
03fd3f84 2554 /* Declarations aren't implicitly extern "C". */
7d381002
MA
2555 parser->implicit_extern_c = false;
2556
c162c75e
MA
2557 /* String literals should be translated to the execution character set. */
2558 parser->translate_strings_p = true;
2559
a723baf1
MM
2560 /* The unparsed function queue is empty. */
2561 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2562
2563 /* There are no classes being defined. */
2564 parser->num_classes_being_defined = 0;
2565
2566 /* No template parameters apply. */
2567 parser->num_template_parameter_lists = 0;
2568
2569 return parser;
2570}
2571
2cfe82fe
ZW
2572/* Create a cp_lexer structure which will emit the tokens in CACHE
2573 and push it onto the parser's lexer stack. This is used for delayed
2574 parsing of in-class method bodies and default arguments, and should
2575 not be confused with tentative parsing. */
2576static void
2577cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2578{
2579 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2580 lexer->next = parser->lexer;
2581 parser->lexer = lexer;
2582
2583 /* Move the current source position to that of the first token in the
2584 new lexer. */
2585 cp_lexer_set_source_position_from_token (lexer->next_token);
2586}
2587
2588/* Pop the top lexer off the parser stack. This is never used for the
2589 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2590static void
2591cp_parser_pop_lexer (cp_parser *parser)
2592{
2593 cp_lexer *lexer = parser->lexer;
2594 parser->lexer = lexer->next;
2595 cp_lexer_destroy (lexer);
2596
2597 /* Put the current source position back where it was before this
2598 lexer was pushed. */
2599 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2600}
2601
a723baf1
MM
2602/* Lexical conventions [gram.lex] */
2603
2604/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2605 identifier. */
2606
21526606 2607static tree
94edc4ab 2608cp_parser_identifier (cp_parser* parser)
a723baf1
MM
2609{
2610 cp_token *token;
2611
2612 /* Look for the identifier. */
2613 token = cp_parser_require (parser, CPP_NAME, "identifier");
2614 /* Return the value. */
2615 return token ? token->value : error_mark_node;
2616}
2617
c162c75e
MA
2618/* Parse a sequence of adjacent string constants. Returns a
2619 TREE_STRING representing the combined, nul-terminated string
2620 constant. If TRANSLATE is true, translate the string to the
2621 execution character set. If WIDE_OK is true, a wide string is
2622 invalid here.
2623
2624 C++98 [lex.string] says that if a narrow string literal token is
2625 adjacent to a wide string literal token, the behavior is undefined.
2626 However, C99 6.4.5p4 says that this results in a wide string literal.
2627 We follow C99 here, for consistency with the C front end.
2628
2629 This code is largely lifted from lex_string() in c-lex.c.
2630
2631 FUTURE: ObjC++ will need to handle @-strings here. */
2632static tree
2633cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2634{
2635 tree value;
2636 bool wide = false;
2637 size_t count;
2638 struct obstack str_ob;
2639 cpp_string str, istr, *strs;
2640 cp_token *tok;
2641
2642 tok = cp_lexer_peek_token (parser->lexer);
2643 if (!cp_parser_is_string_literal (tok))
2644 {
2645 cp_parser_error (parser, "expected string-literal");
2646 return error_mark_node;
2647 }
2648
9688c3b8 2649 /* Try to avoid the overhead of creating and destroying an obstack
c162c75e 2650 for the common case of just one string. */
2cfe82fe
ZW
2651 if (!cp_parser_is_string_literal
2652 (cp_lexer_peek_nth_token (parser->lexer, 2)))
c162c75e 2653 {
2cfe82fe
ZW
2654 cp_lexer_consume_token (parser->lexer);
2655
c162c75e
MA
2656 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2657 str.len = TREE_STRING_LENGTH (tok->value);
2658 count = 1;
2659 if (tok->type == CPP_WSTRING)
2660 wide = true;
c162c75e
MA
2661
2662 strs = &str;
2663 }
2664 else
2665 {
2666 gcc_obstack_init (&str_ob);
2667 count = 0;
2668
2669 do
2670 {
2cfe82fe 2671 cp_lexer_consume_token (parser->lexer);
c162c75e
MA
2672 count++;
2673 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2674 str.len = TREE_STRING_LENGTH (tok->value);
2675 if (tok->type == CPP_WSTRING)
2676 wide = true;
2677
2678 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2679
2cfe82fe 2680 tok = cp_lexer_peek_token (parser->lexer);
c162c75e
MA
2681 }
2682 while (cp_parser_is_string_literal (tok));
2683
2684 strs = (cpp_string *) obstack_finish (&str_ob);
2685 }
2686
2687 if (wide && !wide_ok)
2688 {
2689 cp_parser_error (parser, "a wide string is invalid in this context");
2690 wide = false;
2691 }
2692
2693 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2694 (parse_in, strs, count, &istr, wide))
2695 {
2696 value = build_string (istr.len, (char *)istr.text);
2697 free ((void *)istr.text);
2698
2699 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2700 value = fix_string_type (value);
2701 }
2702 else
2703 /* cpp_interpret_string has issued an error. */
2704 value = error_mark_node;
2705
2706 if (count > 1)
2707 obstack_free (&str_ob, 0);
2708
2709 return value;
2710}
2711
2712
a723baf1
MM
2713/* Basic concepts [gram.basic] */
2714
2715/* Parse a translation-unit.
2716
2717 translation-unit:
21526606 2718 declaration-seq [opt]
a723baf1
MM
2719
2720 Returns TRUE if all went well. */
2721
2722static bool
94edc4ab 2723cp_parser_translation_unit (cp_parser* parser)
a723baf1 2724{
058b15c1
MM
2725 /* The address of the first non-permanent object on the declarator
2726 obstack. */
2727 static void *declarator_obstack_base;
2728
2729 bool success;
98ca843c 2730
058b15c1
MM
2731 /* Create the declarator obstack, if necessary. */
2732 if (!cp_error_declarator)
2733 {
2734 gcc_obstack_init (&declarator_obstack);
2735 /* Create the error declarator. */
2736 cp_error_declarator = make_declarator (cdk_error);
2737 /* Create the empty parameter list. */
62d1db17 2738 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
058b15c1
MM
2739 /* Remember where the base of the declarator obstack lies. */
2740 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2741 }
2742
a30efae8 2743 cp_parser_declaration_seq_opt (parser);
3db45ab5 2744
a30efae8
GDR
2745 /* If there are no tokens left then all went well. */
2746 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
a723baf1 2747 {
a30efae8
GDR
2748 /* Get rid of the token array; we don't need it any more. */
2749 cp_lexer_destroy (parser->lexer);
2750 parser->lexer = NULL;
3db45ab5 2751
a30efae8 2752 /* This file might have been a context that's implicitly extern
3db45ab5 2753 "C". If so, pop the lang context. (Only relevant for PCH.) */
a30efae8 2754 if (parser->implicit_extern_c)
3db45ab5
MS
2755 {
2756 pop_lang_context ();
2757 parser->implicit_extern_c = false;
2758 }
2759
a30efae8
GDR
2760 /* Finish up. */
2761 finish_translation_unit ();
3db45ab5 2762
a30efae8 2763 success = true;
a723baf1 2764 }
a30efae8
GDR
2765 else
2766 {
2767 cp_parser_error (parser, "expected declaration");
2768 success = false;
2769 }
3db45ab5 2770
058b15c1 2771 /* Make sure the declarator obstack was fully cleaned up. */
50bc768d
NS
2772 gcc_assert (obstack_next_free (&declarator_obstack)
2773 == declarator_obstack_base);
a723baf1
MM
2774
2775 /* All went well. */
058b15c1 2776 return success;
a723baf1
MM
2777}
2778
2779/* Expressions [gram.expr] */
2780
2781/* Parse a primary-expression.
2782
2783 primary-expression:
2784 literal
2785 this
2786 ( expression )
2787 id-expression
2788
2789 GNU Extensions:
2790
2791 primary-expression:
2792 ( compound-statement )
2793 __builtin_va_arg ( assignment-expression , type-id )
93846d56 2794 __builtin_offsetof ( type-id , offsetof-expression )
a723baf1 2795
e58a9aa1
ZL
2796 Objective-C++ Extension:
2797
2798 primary-expression:
2799 objc-expression
2800
a723baf1
MM
2801 literal:
2802 __null
2803
02ed62dd
MM
2804 ADDRESS_P is true iff this expression was immediately preceded by
2805 "&" and therefore might denote a pointer-to-member. CAST_P is true
2806 iff this expression is the target of a cast. TEMPLATE_ARG_P is
bcf51da2 2807 true iff this expression is a template argument.
93678513 2808
02ed62dd
MM
2809 Returns a representation of the expression. Upon return, *IDK
2810 indicates what kind of id-expression (if any) was present. */
a723baf1
MM
2811
2812static tree
21526606 2813cp_parser_primary_expression (cp_parser *parser,
02ed62dd 2814 bool address_p,
93678513 2815 bool cast_p,
02ed62dd
MM
2816 bool template_arg_p,
2817 cp_id_kind *idk)
a723baf1
MM
2818{
2819 cp_token *token;
2820
2821 /* Assume the primary expression is not an id-expression. */
b3445994 2822 *idk = CP_ID_KIND_NONE;
a723baf1
MM
2823
2824 /* Peek at the next token. */
2825 token = cp_lexer_peek_token (parser->lexer);
2826 switch (token->type)
2827 {
2828 /* literal:
2829 integer-literal
2830 character-literal
2831 floating-literal
2832 string-literal
2833 boolean-literal */
2834 case CPP_CHAR:
2835 case CPP_WCHAR:
a723baf1
MM
2836 case CPP_NUMBER:
2837 token = cp_lexer_consume_token (parser->lexer);
93678513
MM
2838 /* Floating-point literals are only allowed in an integral
2839 constant expression if they are cast to an integral or
2840 enumeration type. */
2841 if (TREE_CODE (token->value) == REAL_CST
8c94c75a
MM
2842 && parser->integral_constant_expression_p
2843 && pedantic)
93678513
MM
2844 {
2845 /* CAST_P will be set even in invalid code like "int(2.7 +
2846 ...)". Therefore, we have to check that the next token
2847 is sure to end the cast. */
2848 if (cast_p)
2849 {
2850 cp_token *next_token;
2851
2852 next_token = cp_lexer_peek_token (parser->lexer);
2853 if (/* The comma at the end of an
2854 enumerator-definition. */
2855 next_token->type != CPP_COMMA
2856 /* The curly brace at the end of an enum-specifier. */
2857 && next_token->type != CPP_CLOSE_BRACE
2858 /* The end of a statement. */
2859 && next_token->type != CPP_SEMICOLON
2860 /* The end of the cast-expression. */
2861 && next_token->type != CPP_CLOSE_PAREN
2862 /* The end of an array bound. */
060e7327
MM
2863 && next_token->type != CPP_CLOSE_SQUARE
2864 /* The closing ">" in a template-argument-list. */
2865 && (next_token->type != CPP_GREATER
2866 || parser->greater_than_is_operator_p))
93678513
MM
2867 cast_p = false;
2868 }
2869
2870 /* If we are within a cast, then the constraint that the
2871 cast is to an integral or enumeration type will be
2872 checked at that point. If we are not within a cast, then
2873 this code is invalid. */
2874 if (!cast_p)
c8094d83 2875 cp_parser_non_integral_constant_expression
93678513
MM
2876 (parser, "floating-point literal");
2877 }
a723baf1
MM
2878 return token->value;
2879
0173bb6f
AO
2880 case CPP_STRING:
2881 case CPP_WSTRING:
c162c75e 2882 /* ??? Should wide strings be allowed when parser->translate_strings_p
0cbd7506 2883 is false (i.e. in attributes)? If not, we can kill the third
c162c75e
MA
2884 argument to cp_parser_string_literal. */
2885 return cp_parser_string_literal (parser,
2886 parser->translate_strings_p,
2887 true);
0173bb6f 2888
a723baf1
MM
2889 case CPP_OPEN_PAREN:
2890 {
2891 tree expr;
2892 bool saved_greater_than_is_operator_p;
2893
2894 /* Consume the `('. */
2895 cp_lexer_consume_token (parser->lexer);
2896 /* Within a parenthesized expression, a `>' token is always
2897 the greater-than operator. */
21526606 2898 saved_greater_than_is_operator_p
a723baf1
MM
2899 = parser->greater_than_is_operator_p;
2900 parser->greater_than_is_operator_p = true;
2901 /* If we see `( { ' then we are looking at the beginning of
2902 a GNU statement-expression. */
2903 if (cp_parser_allow_gnu_extensions_p (parser)
2904 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2905 {
2906 /* Statement-expressions are not allowed by the standard. */
2907 if (pedantic)
21526606
EC
2908 pedwarn ("ISO C++ forbids braced-groups within expressions");
2909
a723baf1
MM
2910 /* And they're not allowed outside of a function-body; you
2911 cannot, for example, write:
21526606 2912
0cbd7506 2913 int i = ({ int j = 3; j + 1; });
21526606 2914
a723baf1
MM
2915 at class or namespace scope. */
2916 if (!at_function_scope_p ())
2917 error ("statement-expressions are allowed only inside functions");
2918 /* Start the statement-expression. */
2919 expr = begin_stmt_expr ();
2920 /* Parse the compound-statement. */
325c3691 2921 cp_parser_compound_statement (parser, expr, false);
a723baf1 2922 /* Finish up. */
303b7406 2923 expr = finish_stmt_expr (expr, false);
a723baf1
MM
2924 }
2925 else
2926 {
2927 /* Parse the parenthesized expression. */
93678513 2928 expr = cp_parser_expression (parser, cast_p);
a723baf1
MM
2929 /* Let the front end know that this expression was
2930 enclosed in parentheses. This matters in case, for
2931 example, the expression is of the form `A::B', since
2932 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2933 not. */
2934 finish_parenthesized_expr (expr);
2935 }
2936 /* The `>' token might be the end of a template-id or
2937 template-parameter-list now. */
21526606 2938 parser->greater_than_is_operator_p
a723baf1
MM
2939 = saved_greater_than_is_operator_p;
2940 /* Consume the `)'. */
2941 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2942 cp_parser_skip_to_end_of_statement (parser);
2943
2944 return expr;
2945 }
2946
2947 case CPP_KEYWORD:
2948 switch (token->keyword)
2949 {
2950 /* These two are the boolean literals. */
2951 case RID_TRUE:
2952 cp_lexer_consume_token (parser->lexer);
2953 return boolean_true_node;
2954 case RID_FALSE:
2955 cp_lexer_consume_token (parser->lexer);
2956 return boolean_false_node;
21526606 2957
a723baf1
MM
2958 /* The `__null' literal. */
2959 case RID_NULL:
2960 cp_lexer_consume_token (parser->lexer);
2961 return null_node;
2962
2963 /* Recognize the `this' keyword. */
2964 case RID_THIS:
2965 cp_lexer_consume_token (parser->lexer);
2966 if (parser->local_variables_forbidden_p)
2967 {
2a13a625 2968 error ("%<this%> may not be used in this context");
a723baf1
MM
2969 return error_mark_node;
2970 }
14d22dd6 2971 /* Pointers cannot appear in constant-expressions. */
625cbf93
MM
2972 if (cp_parser_non_integral_constant_expression (parser,
2973 "`this'"))
2974 return error_mark_node;
a723baf1
MM
2975 return finish_this_expr ();
2976
2977 /* The `operator' keyword can be the beginning of an
2978 id-expression. */
2979 case RID_OPERATOR:
2980 goto id_expression;
2981
2982 case RID_FUNCTION_NAME:
2983 case RID_PRETTY_FUNCTION_NAME:
2984 case RID_C99_FUNCTION_NAME:
2985 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2986 __func__ are the names of variables -- but they are
2987 treated specially. Therefore, they are handled here,
2988 rather than relying on the generic id-expression logic
21526606 2989 below. Grammatically, these names are id-expressions.
a723baf1
MM
2990
2991 Consume the token. */
2992 token = cp_lexer_consume_token (parser->lexer);
2993 /* Look up the name. */
2994 return finish_fname (token->value);
2995
2996 case RID_VA_ARG:
2997 {
2998 tree expression;
2999 tree type;
3000
3001 /* The `__builtin_va_arg' construct is used to handle
3002 `va_arg'. Consume the `__builtin_va_arg' token. */
3003 cp_lexer_consume_token (parser->lexer);
3004 /* Look for the opening `('. */
3005 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3006 /* Now, parse the assignment-expression. */
93678513
MM
3007 expression = cp_parser_assignment_expression (parser,
3008 /*cast_p=*/false);
a723baf1
MM
3009 /* Look for the `,'. */
3010 cp_parser_require (parser, CPP_COMMA, "`,'");
3011 /* Parse the type-id. */
3012 type = cp_parser_type_id (parser);
3013 /* Look for the closing `)'. */
3014 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14d22dd6
MM
3015 /* Using `va_arg' in a constant-expression is not
3016 allowed. */
625cbf93
MM
3017 if (cp_parser_non_integral_constant_expression (parser,
3018 "`va_arg'"))
3019 return error_mark_node;
a723baf1
MM
3020 return build_x_va_arg (expression, type);
3021 }
3022
263ee052 3023 case RID_OFFSETOF:
7a3ea201 3024 return cp_parser_builtin_offsetof (parser);
263ee052 3025
e58a9aa1
ZL
3026 /* Objective-C++ expressions. */
3027 case RID_AT_ENCODE:
3028 case RID_AT_PROTOCOL:
3029 case RID_AT_SELECTOR:
3030 return cp_parser_objc_expression (parser);
3031
a723baf1
MM
3032 default:
3033 cp_parser_error (parser, "expected primary-expression");
3034 return error_mark_node;
3035 }
a723baf1
MM
3036
3037 /* An id-expression can start with either an identifier, a
3038 `::' as the beginning of a qualified-id, or the "operator"
3039 keyword. */
3040 case CPP_NAME:
3041 case CPP_SCOPE:
3042 case CPP_TEMPLATE_ID:
3043 case CPP_NESTED_NAME_SPECIFIER:
3044 {
3045 tree id_expression;
3046 tree decl;
b3445994 3047 const char *error_msg;
02ed62dd
MM
3048 bool template_p;
3049 bool done;
a723baf1
MM
3050
3051 id_expression:
3052 /* Parse the id-expression. */
21526606
EC
3053 id_expression
3054 = cp_parser_id_expression (parser,
a723baf1
MM
3055 /*template_keyword_p=*/false,
3056 /*check_dependency_p=*/true,
02ed62dd 3057 &template_p,
fa6098f8
MM
3058 /*declarator_p=*/false,
3059 /*optional_p=*/false);
a723baf1
MM
3060 if (id_expression == error_mark_node)
3061 return error_mark_node;
02ed62dd
MM
3062 token = cp_lexer_peek_token (parser->lexer);
3063 done = (token->type != CPP_OPEN_SQUARE
3064 && token->type != CPP_OPEN_PAREN
3065 && token->type != CPP_DOT
3066 && token->type != CPP_DEREF
3067 && token->type != CPP_PLUS_PLUS
3068 && token->type != CPP_MINUS_MINUS);
a723baf1
MM
3069 /* If we have a template-id, then no further lookup is
3070 required. If the template-id was for a template-class, we
3071 will sometimes have a TYPE_DECL at this point. */
02ed62dd
MM
3072 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3073 || TREE_CODE (id_expression) == TYPE_DECL)
a723baf1
MM
3074 decl = id_expression;
3075 /* Look up the name. */
21526606 3076 else
a723baf1 3077 {
91b1ca65 3078 tree ambiguous_decls;
8f78f01f
MM
3079
3080 decl = cp_parser_lookup_name (parser, id_expression,
fc6a28d7 3081 none_type,
02ed62dd 3082 template_p,
8f78f01f
MM
3083 /*is_namespace=*/false,
3084 /*check_dependency=*/true,
91b1ca65 3085 &ambiguous_decls);
8f78f01f
MM
3086 /* If the lookup was ambiguous, an error will already have
3087 been issued. */
91b1ca65 3088 if (ambiguous_decls)
8f78f01f 3089 return error_mark_node;
e58a9aa1
ZL
3090
3091 /* In Objective-C++, an instance variable (ivar) may be preferred
3092 to whatever cp_parser_lookup_name() found. */
3093 decl = objc_lookup_ivar (decl, id_expression);
3094
a723baf1 3095 /* If name lookup gives us a SCOPE_REF, then the
02ed62dd 3096 qualifying scope was dependent. */
a723baf1 3097 if (TREE_CODE (decl) == SCOPE_REF)
02ed62dd 3098 return decl;
a723baf1
MM
3099 /* Check to see if DECL is a local variable in a context
3100 where that is forbidden. */
3101 if (parser->local_variables_forbidden_p
3102 && local_variable_p (decl))
3103 {
3104 /* It might be that we only found DECL because we are
3105 trying to be generous with pre-ISO scoping rules.
3106 For example, consider:
3107
3108 int i;
3109 void g() {
3110 for (int i = 0; i < 10; ++i) {}
3111 extern void f(int j = i);
3112 }
3113
21526606 3114 Here, name look up will originally find the out
a723baf1
MM
3115 of scope `i'. We need to issue a warning message,
3116 but then use the global `i'. */
3117 decl = check_for_out_of_scope_variable (decl);
3118 if (local_variable_p (decl))
3119 {
2a13a625 3120 error ("local variable %qD may not appear in this context",
a723baf1
MM
3121 decl);
3122 return error_mark_node;
3123 }
3124 }
c006d942 3125 }
21526606 3126
3db45ab5 3127 decl = (finish_id_expression
02ed62dd
MM
3128 (id_expression, decl, parser->scope,
3129 idk,
3130 parser->integral_constant_expression_p,
3131 parser->allow_non_integral_constant_expression_p,
3132 &parser->non_integral_constant_expression_p,
3133 template_p, done, address_p,
3134 template_arg_p,
3135 &error_msg));
b3445994
MM
3136 if (error_msg)
3137 cp_parser_error (parser, error_msg);
a723baf1
MM
3138 return decl;
3139 }
3140
3141 /* Anything else is an error. */
3142 default:
e58a9aa1 3143 /* ...unless we have an Objective-C++ message or string literal, that is. */
c8094d83 3144 if (c_dialect_objc ()
e58a9aa1
ZL
3145 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3146 return cp_parser_objc_expression (parser);
3147
a723baf1
MM
3148 cp_parser_error (parser, "expected primary-expression");
3149 return error_mark_node;
3150 }
3151}
3152
3153/* Parse an id-expression.
3154
3155 id-expression:
3156 unqualified-id
3157 qualified-id
3158
3159 qualified-id:
3160 :: [opt] nested-name-specifier template [opt] unqualified-id
3161 :: identifier
3162 :: operator-function-id
3163 :: template-id
3164
3165 Return a representation of the unqualified portion of the
3166 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3167 a `::' or nested-name-specifier.
3168
3169 Often, if the id-expression was a qualified-id, the caller will
3170 want to make a SCOPE_REF to represent the qualified-id. This
3171 function does not do this in order to avoid wastefully creating
3172 SCOPE_REFs when they are not required.
3173
a723baf1
MM
3174 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3175 `template' keyword.
3176
3177 If CHECK_DEPENDENCY_P is false, then names are looked up inside
21526606 3178 uninstantiated templates.
a723baf1 3179
15d2cb19 3180 If *TEMPLATE_P is non-NULL, it is set to true iff the
a723baf1 3181 `template' keyword is used to explicitly indicate that the entity
21526606 3182 named is a template.
f3c2dfc6
MM
3183
3184 If DECLARATOR_P is true, the id-expression is appearing as part of
cd0be382 3185 a declarator, rather than as part of an expression. */
a723baf1
MM
3186
3187static tree
3188cp_parser_id_expression (cp_parser *parser,
3189 bool template_keyword_p,
3190 bool check_dependency_p,
f3c2dfc6 3191 bool *template_p,
fa6098f8
MM
3192 bool declarator_p,
3193 bool optional_p)
a723baf1
MM
3194{
3195 bool global_scope_p;
3196 bool nested_name_specifier_p;
3197
3198 /* Assume the `template' keyword was not used. */
3199 if (template_p)
02ed62dd 3200 *template_p = template_keyword_p;
a723baf1
MM
3201
3202 /* Look for the optional `::' operator. */
21526606
EC
3203 global_scope_p
3204 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
a723baf1
MM
3205 != NULL_TREE);
3206 /* Look for the optional nested-name-specifier. */
21526606 3207 nested_name_specifier_p
a723baf1
MM
3208 = (cp_parser_nested_name_specifier_opt (parser,
3209 /*typename_keyword_p=*/false,
3210 check_dependency_p,
a668c6ad 3211 /*type_p=*/false,
a52eb3bc 3212 declarator_p)
a723baf1
MM
3213 != NULL_TREE);
3214 /* If there is a nested-name-specifier, then we are looking at
3215 the first qualified-id production. */
3216 if (nested_name_specifier_p)
3217 {
3218 tree saved_scope;
3219 tree saved_object_scope;
3220 tree saved_qualifying_scope;
3221 tree unqualified_id;
3222 bool is_template;
3223
3224 /* See if the next token is the `template' keyword. */
3225 if (!template_p)
3226 template_p = &is_template;
3227 *template_p = cp_parser_optional_template_keyword (parser);
3228 /* Name lookup we do during the processing of the
3229 unqualified-id might obliterate SCOPE. */
3230 saved_scope = parser->scope;
3231 saved_object_scope = parser->object_scope;
3232 saved_qualifying_scope = parser->qualifying_scope;
3233 /* Process the final unqualified-id. */
3234 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
f3c2dfc6 3235 check_dependency_p,
fa6098f8
MM
3236 declarator_p,
3237 /*optional_p=*/false);
a723baf1
MM
3238 /* Restore the SAVED_SCOPE for our caller. */
3239 parser->scope = saved_scope;
3240 parser->object_scope = saved_object_scope;
3241 parser->qualifying_scope = saved_qualifying_scope;
3242
3243 return unqualified_id;
3244 }
3245 /* Otherwise, if we are in global scope, then we are looking at one
3246 of the other qualified-id productions. */
3247 else if (global_scope_p)
3248 {
3249 cp_token *token;
3250 tree id;
3251
e5976695
MM
3252 /* Peek at the next token. */
3253 token = cp_lexer_peek_token (parser->lexer);
3254
3255 /* If it's an identifier, and the next token is not a "<", then
3256 we can avoid the template-id case. This is an optimization
3257 for this common case. */
21526606
EC
3258 if (token->type == CPP_NAME
3259 && !cp_parser_nth_token_starts_template_argument_list_p
f4abade9 3260 (parser, 2))
e5976695
MM
3261 return cp_parser_identifier (parser);
3262
a723baf1
MM
3263 cp_parser_parse_tentatively (parser);
3264 /* Try a template-id. */
21526606 3265 id = cp_parser_template_id (parser,
a723baf1 3266 /*template_keyword_p=*/false,
a668c6ad
MM
3267 /*check_dependency_p=*/true,
3268 declarator_p);
a723baf1
MM
3269 /* If that worked, we're done. */
3270 if (cp_parser_parse_definitely (parser))
3271 return id;
3272
e5976695
MM
3273 /* Peek at the next token. (Changes in the token buffer may
3274 have invalidated the pointer obtained above.) */
a723baf1
MM
3275 token = cp_lexer_peek_token (parser->lexer);
3276
3277 switch (token->type)
3278 {
3279 case CPP_NAME:
3280 return cp_parser_identifier (parser);
3281
3282 case CPP_KEYWORD:
3283 if (token->keyword == RID_OPERATOR)
3284 return cp_parser_operator_function_id (parser);
3285 /* Fall through. */
21526606 3286
a723baf1
MM
3287 default:
3288 cp_parser_error (parser, "expected id-expression");
3289 return error_mark_node;
3290 }
3291 }
3292 else
3293 return cp_parser_unqualified_id (parser, template_keyword_p,
f3c2dfc6 3294 /*check_dependency_p=*/true,
fa6098f8
MM
3295 declarator_p,
3296 optional_p);
a723baf1
MM
3297}
3298
3299/* Parse an unqualified-id.
3300
3301 unqualified-id:
3302 identifier
3303 operator-function-id
3304 conversion-function-id
3305 ~ class-name
3306 template-id
3307
3308 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3309 keyword, in a construct like `A::template ...'.
3310
3311 Returns a representation of unqualified-id. For the `identifier'
3312 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3313 production a BIT_NOT_EXPR is returned; the operand of the
3314 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3315 other productions, see the documentation accompanying the
3316 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
f3c2dfc6
MM
3317 names are looked up in uninstantiated templates. If DECLARATOR_P
3318 is true, the unqualified-id is appearing as part of a declarator,
3319 rather than as part of an expression. */
a723baf1
MM
3320
3321static tree
21526606 3322cp_parser_unqualified_id (cp_parser* parser,
0cbd7506 3323 bool template_keyword_p,
f3c2dfc6 3324 bool check_dependency_p,
3db45ab5 3325 bool declarator_p,
fa6098f8 3326 bool optional_p)
a723baf1
MM
3327{
3328 cp_token *token;
3329
3330 /* Peek at the next token. */
3331 token = cp_lexer_peek_token (parser->lexer);
21526606 3332
a723baf1
MM
3333 switch (token->type)
3334 {
3335 case CPP_NAME:
3336 {
3337 tree id;
3338
3339 /* We don't know yet whether or not this will be a
3340 template-id. */
3341 cp_parser_parse_tentatively (parser);
3342 /* Try a template-id. */
3343 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3344 check_dependency_p,
3345 declarator_p);
a723baf1
MM
3346 /* If it worked, we're done. */
3347 if (cp_parser_parse_definitely (parser))
3348 return id;
3349 /* Otherwise, it's an ordinary identifier. */
3350 return cp_parser_identifier (parser);
3351 }
3352
3353 case CPP_TEMPLATE_ID:
3354 return cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3355 check_dependency_p,
3356 declarator_p);
a723baf1
MM
3357
3358 case CPP_COMPL:
3359 {
3360 tree type_decl;
3361 tree qualifying_scope;
3362 tree object_scope;
3363 tree scope;
88e95ee3 3364 bool done;
a723baf1
MM
3365
3366 /* Consume the `~' token. */
3367 cp_lexer_consume_token (parser->lexer);
3368 /* Parse the class-name. The standard, as written, seems to
3369 say that:
3370
3371 template <typename T> struct S { ~S (); };
3372 template <typename T> S<T>::~S() {}
3373
0cbd7506 3374 is invalid, since `~' must be followed by a class-name, but
a723baf1
MM
3375 `S<T>' is dependent, and so not known to be a class.
3376 That's not right; we need to look in uninstantiated
3377 templates. A further complication arises from:
3378
3379 template <typename T> void f(T t) {
3380 t.T::~T();
21526606 3381 }
a723baf1
MM
3382
3383 Here, it is not possible to look up `T' in the scope of `T'
3384 itself. We must look in both the current scope, and the
21526606 3385 scope of the containing complete expression.
a723baf1
MM
3386
3387 Yet another issue is:
3388
0cbd7506
MS
3389 struct S {
3390 int S;
3391 ~S();
3392 };
a723baf1 3393
0cbd7506 3394 S::~S() {}
a723baf1 3395
0cbd7506 3396 The standard does not seem to say that the `S' in `~S'
a723baf1
MM
3397 should refer to the type `S' and not the data member
3398 `S::S'. */
3399
3400 /* DR 244 says that we look up the name after the "~" in the
3401 same scope as we looked up the qualifying name. That idea
3402 isn't fully worked out; it's more complicated than that. */
3403 scope = parser->scope;
3404 object_scope = parser->object_scope;
3405 qualifying_scope = parser->qualifying_scope;
3406
c0dc47a8
VR
3407 /* Check for invalid scopes. */
3408 if (scope == error_mark_node)
3409 {
04499540
VR
3410 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3411 cp_lexer_consume_token (parser->lexer);
c0dc47a8
VR
3412 return error_mark_node;
3413 }
3414 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3415 {
3416 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3417 error ("scope %qT before %<~%> is not a class-name", scope);
04499540
VR
3418 cp_parser_simulate_error (parser);
3419 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3420 cp_lexer_consume_token (parser->lexer);
c0dc47a8
VR
3421 return error_mark_node;
3422 }
3423 gcc_assert (!scope || TYPE_P (scope));
3424
a723baf1 3425 /* If the name is of the form "X::~X" it's OK. */
84019f23 3426 token = cp_lexer_peek_token (parser->lexer);
c0dc47a8 3427 if (scope
84019f23 3428 && token->type == CPP_NAME
21526606 3429 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1 3430 == CPP_OPEN_PAREN)
84019f23 3431 && constructor_name_p (token->value, scope))
a723baf1
MM
3432 {
3433 cp_lexer_consume_token (parser->lexer);
3434 return build_nt (BIT_NOT_EXPR, scope);
3435 }
3436
3437 /* If there was an explicit qualification (S::~T), first look
3438 in the scope given by the qualification (i.e., S). */
88e95ee3 3439 done = false;
e3754f9c 3440 type_decl = NULL_TREE;
a723baf1
MM
3441 if (scope)
3442 {
3443 cp_parser_parse_tentatively (parser);
21526606 3444 type_decl = cp_parser_class_name (parser,
a723baf1
MM
3445 /*typename_keyword_p=*/false,
3446 /*template_keyword_p=*/false,
fc6a28d7 3447 none_type,
a723baf1 3448 /*check_dependency=*/false,
a668c6ad
MM
3449 /*class_head_p=*/false,
3450 declarator_p);
a723baf1 3451 if (cp_parser_parse_definitely (parser))
88e95ee3 3452 done = true;
a723baf1
MM
3453 }
3454 /* In "N::S::~S", look in "N" as well. */
88e95ee3 3455 if (!done && scope && qualifying_scope)
a723baf1
MM
3456 {
3457 cp_parser_parse_tentatively (parser);
3458 parser->scope = qualifying_scope;
3459 parser->object_scope = NULL_TREE;
3460 parser->qualifying_scope = NULL_TREE;
21526606
EC
3461 type_decl
3462 = cp_parser_class_name (parser,
a723baf1
MM
3463 /*typename_keyword_p=*/false,
3464 /*template_keyword_p=*/false,
fc6a28d7 3465 none_type,
a723baf1 3466 /*check_dependency=*/false,
a668c6ad
MM
3467 /*class_head_p=*/false,
3468 declarator_p);
a723baf1 3469 if (cp_parser_parse_definitely (parser))
88e95ee3 3470 done = true;
a723baf1
MM
3471 }
3472 /* In "p->S::~T", look in the scope given by "*p" as well. */
88e95ee3 3473 else if (!done && object_scope)
a723baf1
MM
3474 {
3475 cp_parser_parse_tentatively (parser);
3476 parser->scope = object_scope;
3477 parser->object_scope = NULL_TREE;
3478 parser->qualifying_scope = NULL_TREE;
21526606
EC
3479 type_decl
3480 = cp_parser_class_name (parser,
a723baf1
MM
3481 /*typename_keyword_p=*/false,
3482 /*template_keyword_p=*/false,
fc6a28d7 3483 none_type,
a723baf1 3484 /*check_dependency=*/false,
a668c6ad
MM
3485 /*class_head_p=*/false,
3486 declarator_p);
a723baf1 3487 if (cp_parser_parse_definitely (parser))
88e95ee3 3488 done = true;
a723baf1
MM
3489 }
3490 /* Look in the surrounding context. */
88e95ee3
MM
3491 if (!done)
3492 {
3493 parser->scope = NULL_TREE;
3494 parser->object_scope = NULL_TREE;
3495 parser->qualifying_scope = NULL_TREE;
3496 type_decl
3497 = cp_parser_class_name (parser,
3498 /*typename_keyword_p=*/false,
3499 /*template_keyword_p=*/false,
3500 none_type,
3501 /*check_dependency=*/false,
3502 /*class_head_p=*/false,
3503 declarator_p);
3504 }
a723baf1
MM
3505 /* If an error occurred, assume that the name of the
3506 destructor is the same as the name of the qualifying
3507 class. That allows us to keep parsing after running
3508 into ill-formed destructor names. */
c0dc47a8 3509 if (type_decl == error_mark_node && scope)
a723baf1
MM
3510 return build_nt (BIT_NOT_EXPR, scope);
3511 else if (type_decl == error_mark_node)
3512 return error_mark_node;
3513
1b3d28a8
VR
3514 /* Check that destructor name and scope match. */
3515 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3516 {
3517 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3518 error ("declaration of %<~%T%> as member of %qT",
3519 type_decl, scope);
04499540 3520 cp_parser_simulate_error (parser);
1b3d28a8
VR
3521 return error_mark_node;
3522 }
3523
f3c2dfc6
MM
3524 /* [class.dtor]
3525
3526 A typedef-name that names a class shall not be used as the
3527 identifier in the declarator for a destructor declaration. */
21526606 3528 if (declarator_p
f3c2dfc6 3529 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
33a69702
VR
3530 && !DECL_SELF_REFERENCE_P (type_decl)
3531 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
2a13a625 3532 error ("typedef-name %qD used as destructor declarator",
f3c2dfc6
MM
3533 type_decl);
3534
a723baf1
MM
3535 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3536 }
3537
3538 case CPP_KEYWORD:
3539 if (token->keyword == RID_OPERATOR)
3540 {
3541 tree id;
3542
3543 /* This could be a template-id, so we try that first. */
3544 cp_parser_parse_tentatively (parser);
3545 /* Try a template-id. */
3546 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3547 /*check_dependency_p=*/true,
3548 declarator_p);
a723baf1
MM
3549 /* If that worked, we're done. */
3550 if (cp_parser_parse_definitely (parser))
3551 return id;
3552 /* We still don't know whether we're looking at an
3553 operator-function-id or a conversion-function-id. */
3554 cp_parser_parse_tentatively (parser);
3555 /* Try an operator-function-id. */
3556 id = cp_parser_operator_function_id (parser);
3557 /* If that didn't work, try a conversion-function-id. */
3558 if (!cp_parser_parse_definitely (parser))
3559 id = cp_parser_conversion_function_id (parser);
3560
3561 return id;
3562 }
3563 /* Fall through. */
3564
3565 default:
fa6098f8
MM
3566 if (optional_p)
3567 return NULL_TREE;
a723baf1
MM
3568 cp_parser_error (parser, "expected unqualified-id");
3569 return error_mark_node;
3570 }
3571}
3572
3573/* Parse an (optional) nested-name-specifier.
3574
3575 nested-name-specifier:
3576 class-or-namespace-name :: nested-name-specifier [opt]
3577 class-or-namespace-name :: template nested-name-specifier [opt]
3578
3579 PARSER->SCOPE should be set appropriately before this function is
3580 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3581 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3582 in name lookups.
3583
3584 Sets PARSER->SCOPE to the class (TYPE) or namespace
3585 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3586 it unchanged if there is no nested-name-specifier. Returns the new
21526606 3587 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
a668c6ad
MM
3588
3589 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3590 part of a declaration and/or decl-specifier. */
a723baf1
MM
3591
3592static tree
21526606
EC
3593cp_parser_nested_name_specifier_opt (cp_parser *parser,
3594 bool typename_keyword_p,
a723baf1 3595 bool check_dependency_p,
a668c6ad
MM
3596 bool type_p,
3597 bool is_declaration)
a723baf1
MM
3598{
3599 bool success = false;
0c5e4866
NS
3600 cp_token_position start = 0;
3601 cp_token *token;
a723baf1 3602
a723baf1 3603 /* Remember where the nested-name-specifier starts. */
0b16f8f4 3604 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
19e65f30
MM
3605 {
3606 start = cp_lexer_token_position (parser->lexer, false);
3607 push_deferring_access_checks (dk_deferred);
3608 }
cf22909c 3609
a723baf1
MM
3610 while (true)
3611 {
3612 tree new_scope;
3613 tree old_scope;
3614 tree saved_qualifying_scope;
a723baf1
MM
3615 bool template_keyword_p;
3616
2050a1bb
MM
3617 /* Spot cases that cannot be the beginning of a
3618 nested-name-specifier. */
3619 token = cp_lexer_peek_token (parser->lexer);
3620
3621 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3622 the already parsed nested-name-specifier. */
3623 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3624 {
3625 /* Grab the nested-name-specifier and continue the loop. */
3626 cp_parser_pre_parsed_nested_name_specifier (parser);
3627 success = true;
3628 continue;
3629 }
3630
a723baf1
MM
3631 /* Spot cases that cannot be the beginning of a
3632 nested-name-specifier. On the second and subsequent times
3633 through the loop, we look for the `template' keyword. */
f7b5ecd9 3634 if (success && token->keyword == RID_TEMPLATE)
a723baf1
MM
3635 ;
3636 /* A template-id can start a nested-name-specifier. */
f7b5ecd9 3637 else if (token->type == CPP_TEMPLATE_ID)
a723baf1
MM
3638 ;
3639 else
3640 {
3641 /* If the next token is not an identifier, then it is
3642 definitely not a class-or-namespace-name. */
f7b5ecd9 3643 if (token->type != CPP_NAME)
a723baf1
MM
3644 break;
3645 /* If the following token is neither a `<' (to begin a
3646 template-id), nor a `::', then we are not looking at a
3647 nested-name-specifier. */
3648 token = cp_lexer_peek_nth_token (parser->lexer, 2);
f4abade9
GB
3649 if (token->type != CPP_SCOPE
3650 && !cp_parser_nth_token_starts_template_argument_list_p
3651 (parser, 2))
a723baf1
MM
3652 break;
3653 }
3654
3655 /* The nested-name-specifier is optional, so we parse
3656 tentatively. */
3657 cp_parser_parse_tentatively (parser);
3658
3659 /* Look for the optional `template' keyword, if this isn't the
3660 first time through the loop. */
3661 if (success)
3662 template_keyword_p = cp_parser_optional_template_keyword (parser);
3663 else
3664 template_keyword_p = false;
3665
3666 /* Save the old scope since the name lookup we are about to do
3667 might destroy it. */
3668 old_scope = parser->scope;
3669 saved_qualifying_scope = parser->qualifying_scope;
a52eb3bc
MM
3670 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3671 look up names in "X<T>::I" in order to determine that "Y" is
3672 a template. So, if we have a typename at this point, we make
3673 an effort to look through it. */
c8094d83 3674 if (is_declaration
67bcc252 3675 && !typename_keyword_p
c8094d83 3676 && parser->scope
a52eb3bc 3677 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
c8094d83 3678 parser->scope = resolve_typename_type (parser->scope,
a52eb3bc 3679 /*only_current_p=*/false);
a723baf1 3680 /* Parse the qualifying entity. */
21526606 3681 new_scope
a723baf1
MM
3682 = cp_parser_class_or_namespace_name (parser,
3683 typename_keyword_p,
3684 template_keyword_p,
3685 check_dependency_p,
a668c6ad
MM
3686 type_p,
3687 is_declaration);
a723baf1
MM
3688 /* Look for the `::' token. */
3689 cp_parser_require (parser, CPP_SCOPE, "`::'");
3690
3691 /* If we found what we wanted, we keep going; otherwise, we're
3692 done. */
3693 if (!cp_parser_parse_definitely (parser))
3694 {
3695 bool error_p = false;
3696
3697 /* Restore the OLD_SCOPE since it was valid before the
3698 failed attempt at finding the last
3699 class-or-namespace-name. */
3700 parser->scope = old_scope;
3701 parser->qualifying_scope = saved_qualifying_scope;
84019f23
MM
3702 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3703 break;
a723baf1
MM
3704 /* If the next token is an identifier, and the one after
3705 that is a `::', then any valid interpretation would have
3706 found a class-or-namespace-name. */
3707 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 3708 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1 3709 == CPP_SCOPE)
21526606 3710 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
a723baf1
MM
3711 != CPP_COMPL))
3712 {
3713 token = cp_lexer_consume_token (parser->lexer);
21526606 3714 if (!error_p)
a723baf1 3715 {
91b1ca65
MM
3716 if (!token->ambiguous_p)
3717 {
3718 tree decl;
3719 tree ambiguous_decls;
3720
3721 decl = cp_parser_lookup_name (parser, token->value,
3722 none_type,
3723 /*is_template=*/false,
3724 /*is_namespace=*/false,
3725 /*check_dependency=*/true,
3726 &ambiguous_decls);
3727 if (TREE_CODE (decl) == TEMPLATE_DECL)
3728 error ("%qD used without template parameters", decl);
3729 else if (ambiguous_decls)
3730 {
3db45ab5 3731 error ("reference to %qD is ambiguous",
91b1ca65
MM
3732 token->value);
3733 print_candidates (ambiguous_decls);
3734 decl = error_mark_node;
3735 }
3736 else
3737 cp_parser_name_lookup_error
3738 (parser, token->value, decl,
3739 "is not a class or namespace");
3740 }
3741 parser->scope = error_mark_node;
a723baf1 3742 error_p = true;
eea9800f
MM
3743 /* Treat this as a successful nested-name-specifier
3744 due to:
3745
3746 [basic.lookup.qual]
3747
3748 If the name found is not a class-name (clause
3749 _class_) or namespace-name (_namespace.def_), the
3750 program is ill-formed. */
3751 success = true;
a723baf1
MM
3752 }
3753 cp_lexer_consume_token (parser->lexer);
3754 }
3755 break;
3756 }
a723baf1
MM
3757 /* We've found one valid nested-name-specifier. */
3758 success = true;
02ed62dd
MM
3759 /* Name lookup always gives us a DECL. */
3760 if (TREE_CODE (new_scope) == TYPE_DECL)
3761 new_scope = TREE_TYPE (new_scope);
3762 /* Uses of "template" must be followed by actual templates. */
3763 if (template_keyword_p
3764 && !(CLASS_TYPE_P (new_scope)
3765 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3766 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3767 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3768 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3769 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3770 == TEMPLATE_ID_EXPR)))
3771 pedwarn (TYPE_P (new_scope)
3772 ? "%qT is not a template"
3773 : "%qD is not a template",
3774 new_scope);
a723baf1
MM
3775 /* If it is a class scope, try to complete it; we are about to
3776 be looking up names inside the class. */
02ed62dd 3777 if (TYPE_P (new_scope)
8fbc5ae7
MM
3778 /* Since checking types for dependency can be expensive,
3779 avoid doing it if the type is already complete. */
02ed62dd 3780 && !COMPLETE_TYPE_P (new_scope)
8fbc5ae7 3781 /* Do not try to complete dependent types. */
02ed62dd
MM
3782 && !dependent_type_p (new_scope))
3783 new_scope = complete_type (new_scope);
3784 /* Make sure we look in the right scope the next time through
3785 the loop. */
3786 parser->scope = new_scope;
a723baf1
MM
3787 }
3788
3789 /* If parsing tentatively, replace the sequence of tokens that makes
3790 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3791 token. That way, should we re-parse the token stream, we will
3792 not have to repeat the effort required to do the parse, nor will
3793 we issue duplicate error messages. */
0c5e4866 3794 if (success && start)
a723baf1 3795 {
19e65f30
MM
3796 cp_token *token;
3797 tree access_checks;
c8094d83 3798
19e65f30 3799 token = cp_lexer_token_at (parser->lexer, start);
a723baf1
MM
3800 /* Reset the contents of the START token. */
3801 token->type = CPP_NESTED_NAME_SPECIFIER;
19e65f30
MM
3802 /* Retrieve any deferred checks. Do not pop this access checks yet
3803 so the memory will not be reclaimed during token replacing below. */
3804 access_checks = get_deferred_access_checks ();
3805 token->value = build_tree_list (copy_list (access_checks),
3806 parser->scope);
a723baf1
MM
3807 TREE_TYPE (token->value) = parser->qualifying_scope;
3808 token->keyword = RID_MAX;
c8094d83 3809
a723baf1 3810 /* Purge all subsequent tokens. */
0c5e4866 3811 cp_lexer_purge_tokens_after (parser->lexer, start);
a723baf1 3812 }
3db45ab5 3813
19e65f30
MM
3814 if (start)
3815 pop_to_parent_deferring_access_checks ();
a723baf1
MM
3816
3817 return success ? parser->scope : NULL_TREE;
3818}
3819
3820/* Parse a nested-name-specifier. See
3821 cp_parser_nested_name_specifier_opt for details. This function
3822 behaves identically, except that it will an issue an error if no
8fe4d24b 3823 nested-name-specifier is present. */
a723baf1
MM
3824
3825static tree
21526606
EC
3826cp_parser_nested_name_specifier (cp_parser *parser,
3827 bool typename_keyword_p,
a723baf1 3828 bool check_dependency_p,
a668c6ad
MM
3829 bool type_p,
3830 bool is_declaration)
a723baf1
MM
3831{
3832 tree scope;
3833
3834 /* Look for the nested-name-specifier. */
3835 scope = cp_parser_nested_name_specifier_opt (parser,
3836 typename_keyword_p,
3837 check_dependency_p,
a668c6ad
MM
3838 type_p,
3839 is_declaration);
a723baf1
MM
3840 /* If it was not present, issue an error message. */
3841 if (!scope)
3842 {
3843 cp_parser_error (parser, "expected nested-name-specifier");
eb5abb39 3844 parser->scope = NULL_TREE;
a723baf1
MM
3845 }
3846
3847 return scope;
3848}
3849
3850/* Parse a class-or-namespace-name.
3851
3852 class-or-namespace-name:
3853 class-name
3854 namespace-name
3855
3856 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3857 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3858 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3859 TYPE_P is TRUE iff the next name should be taken as a class-name,
3860 even the same name is declared to be another entity in the same
3861 scope.
3862
3863 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
eea9800f
MM
3864 specified by the class-or-namespace-name. If neither is found the
3865 ERROR_MARK_NODE is returned. */
a723baf1
MM
3866
3867static tree
21526606 3868cp_parser_class_or_namespace_name (cp_parser *parser,
a723baf1
MM
3869 bool typename_keyword_p,
3870 bool template_keyword_p,
3871 bool check_dependency_p,
a668c6ad
MM
3872 bool type_p,
3873 bool is_declaration)
a723baf1
MM
3874{
3875 tree saved_scope;
3876 tree saved_qualifying_scope;
3877 tree saved_object_scope;
3878 tree scope;
eea9800f 3879 bool only_class_p;
a723baf1 3880
a723baf1
MM
3881 /* Before we try to parse the class-name, we must save away the
3882 current PARSER->SCOPE since cp_parser_class_name will destroy
3883 it. */
3884 saved_scope = parser->scope;
3885 saved_qualifying_scope = parser->qualifying_scope;
3886 saved_object_scope = parser->object_scope;
eea9800f
MM
3887 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3888 there is no need to look for a namespace-name. */
bbaab916 3889 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
eea9800f
MM
3890 if (!only_class_p)
3891 cp_parser_parse_tentatively (parser);
21526606 3892 scope = cp_parser_class_name (parser,
a723baf1
MM
3893 typename_keyword_p,
3894 template_keyword_p,
fc6a28d7 3895 type_p ? class_type : none_type,
a723baf1 3896 check_dependency_p,
a668c6ad
MM
3897 /*class_head_p=*/false,
3898 is_declaration);
a723baf1 3899 /* If that didn't work, try for a namespace-name. */
eea9800f 3900 if (!only_class_p && !cp_parser_parse_definitely (parser))
a723baf1
MM
3901 {
3902 /* Restore the saved scope. */
3903 parser->scope = saved_scope;
3904 parser->qualifying_scope = saved_qualifying_scope;
3905 parser->object_scope = saved_object_scope;
eea9800f
MM
3906 /* If we are not looking at an identifier followed by the scope
3907 resolution operator, then this is not part of a
3908 nested-name-specifier. (Note that this function is only used
3909 to parse the components of a nested-name-specifier.) */
3910 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3911 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3912 return error_mark_node;
a723baf1
MM
3913 scope = cp_parser_namespace_name (parser);
3914 }
3915
3916 return scope;
3917}
3918
3919/* Parse a postfix-expression.
3920
3921 postfix-expression:
3922 primary-expression
3923 postfix-expression [ expression ]
3924 postfix-expression ( expression-list [opt] )
3925 simple-type-specifier ( expression-list [opt] )
21526606 3926 typename :: [opt] nested-name-specifier identifier
a723baf1
MM
3927 ( expression-list [opt] )
3928 typename :: [opt] nested-name-specifier template [opt] template-id
3929 ( expression-list [opt] )
3930 postfix-expression . template [opt] id-expression
3931 postfix-expression -> template [opt] id-expression
3932 postfix-expression . pseudo-destructor-name
3933 postfix-expression -> pseudo-destructor-name
3934 postfix-expression ++
3935 postfix-expression --
3936 dynamic_cast < type-id > ( expression )
3937 static_cast < type-id > ( expression )
3938 reinterpret_cast < type-id > ( expression )
3939 const_cast < type-id > ( expression )
3940 typeid ( expression )
3941 typeid ( type-id )
3942
3943 GNU Extension:
21526606 3944
a723baf1
MM
3945 postfix-expression:
3946 ( type-id ) { initializer-list , [opt] }
3947
3948 This extension is a GNU version of the C99 compound-literal
3949 construct. (The C99 grammar uses `type-name' instead of `type-id',
3950 but they are essentially the same concept.)
3951
3952 If ADDRESS_P is true, the postfix expression is the operand of the
93678513 3953 `&' operator. CAST_P is true if this expression is the target of a
c8094d83 3954 cast.
a723baf1
MM
3955
3956 Returns a representation of the expression. */
3957
3958static tree
93678513 3959cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
a723baf1
MM
3960{
3961 cp_token *token;
3962 enum rid keyword;
b3445994 3963 cp_id_kind idk = CP_ID_KIND_NONE;
a723baf1 3964 tree postfix_expression = NULL_TREE;
a723baf1
MM
3965
3966 /* Peek at the next token. */
3967 token = cp_lexer_peek_token (parser->lexer);
3968 /* Some of the productions are determined by keywords. */
3969 keyword = token->keyword;
3970 switch (keyword)
3971 {
3972 case RID_DYNCAST:
3973 case RID_STATCAST:
3974 case RID_REINTCAST:
3975 case RID_CONSTCAST:
3976 {
3977 tree type;
3978 tree expression;
3979 const char *saved_message;
3980
3981 /* All of these can be handled in the same way from the point
3982 of view of parsing. Begin by consuming the token
3983 identifying the cast. */
3984 cp_lexer_consume_token (parser->lexer);
21526606 3985
a723baf1
MM
3986 /* New types cannot be defined in the cast. */
3987 saved_message = parser->type_definition_forbidden_message;
3988 parser->type_definition_forbidden_message
3989 = "types may not be defined in casts";
3990
3991 /* Look for the opening `<'. */
3992 cp_parser_require (parser, CPP_LESS, "`<'");
3993 /* Parse the type to which we are casting. */
3994 type = cp_parser_type_id (parser);
3995 /* Look for the closing `>'. */
3996 cp_parser_require (parser, CPP_GREATER, "`>'");
3997 /* Restore the old message. */
3998 parser->type_definition_forbidden_message = saved_message;
3999
4000 /* And the expression which is being cast. */
4001 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
93678513 4002 expression = cp_parser_expression (parser, /*cast_p=*/true);
a723baf1
MM
4003 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4004
14d22dd6
MM
4005 /* Only type conversions to integral or enumeration types
4006 can be used in constant-expressions. */
015c2c66 4007 if (!cast_valid_in_integral_constant_expression_p (type)
98ca843c 4008 && (cp_parser_non_integral_constant_expression
625cbf93
MM
4009 (parser,
4010 "a cast to a type other than an integral or "
4011 "enumeration type")))
4012 return error_mark_node;
14d22dd6 4013
a723baf1
MM
4014 switch (keyword)
4015 {
4016 case RID_DYNCAST:
4017 postfix_expression
4018 = build_dynamic_cast (type, expression);
4019 break;
4020 case RID_STATCAST:
4021 postfix_expression
4022 = build_static_cast (type, expression);
4023 break;
4024 case RID_REINTCAST:
4025 postfix_expression
4026 = build_reinterpret_cast (type, expression);
4027 break;
4028 case RID_CONSTCAST:
4029 postfix_expression
4030 = build_const_cast (type, expression);
4031 break;
4032 default:
315fb5db 4033 gcc_unreachable ();
a723baf1
MM
4034 }
4035 }
4036 break;
4037
4038 case RID_TYPEID:
4039 {
4040 tree type;
4041 const char *saved_message;
4f8163b1 4042 bool saved_in_type_id_in_expr_p;
a723baf1
MM
4043
4044 /* Consume the `typeid' token. */
4045 cp_lexer_consume_token (parser->lexer);
4046 /* Look for the `(' token. */
4047 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4048 /* Types cannot be defined in a `typeid' expression. */
4049 saved_message = parser->type_definition_forbidden_message;
4050 parser->type_definition_forbidden_message
4051 = "types may not be defined in a `typeid\' expression";
4052 /* We can't be sure yet whether we're looking at a type-id or an
4053 expression. */
4054 cp_parser_parse_tentatively (parser);
4055 /* Try a type-id first. */
4f8163b1
MM
4056 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4057 parser->in_type_id_in_expr_p = true;
a723baf1 4058 type = cp_parser_type_id (parser);
4f8163b1 4059 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
4060 /* Look for the `)' token. Otherwise, we can't be sure that
4061 we're not looking at an expression: consider `typeid (int
4062 (3))', for example. */
4063 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4064 /* If all went well, simply lookup the type-id. */
4065 if (cp_parser_parse_definitely (parser))
4066 postfix_expression = get_typeid (type);
4067 /* Otherwise, fall back to the expression variant. */
4068 else
4069 {
4070 tree expression;
4071
4072 /* Look for an expression. */
93678513 4073 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
4074 /* Compute its typeid. */
4075 postfix_expression = build_typeid (expression);
4076 /* Look for the `)' token. */
4077 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4078 }
5e8c38c2
SM
4079 /* Restore the saved message. */
4080 parser->type_definition_forbidden_message = saved_message;
4424e0da 4081 /* `typeid' may not appear in an integral constant expression. */
98ca843c 4082 if (cp_parser_non_integral_constant_expression(parser,
4424e0da
GB
4083 "`typeid' operator"))
4084 return error_mark_node;
a723baf1
MM
4085 }
4086 break;
21526606 4087
a723baf1
MM
4088 case RID_TYPENAME:
4089 {
a723baf1 4090 tree type;
88a33c34
MM
4091 /* The syntax permitted here is the same permitted for an
4092 elaborated-type-specifier. */
4093 type = cp_parser_elaborated_type_specifier (parser,
4094 /*is_friend=*/false,
4095 /*is_declaration=*/false);
a723baf1
MM
4096 postfix_expression = cp_parser_functional_cast (parser, type);
4097 }
4098 break;
4099
4100 default:
4101 {
4102 tree type;
4103
4104 /* If the next thing is a simple-type-specifier, we may be
4105 looking at a functional cast. We could also be looking at
4106 an id-expression. So, we try the functional cast, and if
4107 that doesn't work we fall back to the primary-expression. */
4108 cp_parser_parse_tentatively (parser);
4109 /* Look for the simple-type-specifier. */
21526606 4110 type = cp_parser_simple_type_specifier (parser,
62d1db17
MM
4111 /*decl_specs=*/NULL,
4112 CP_PARSER_FLAGS_NONE);
a723baf1
MM
4113 /* Parse the cast itself. */
4114 if (!cp_parser_error_occurred (parser))
21526606 4115 postfix_expression
a723baf1
MM
4116 = cp_parser_functional_cast (parser, type);
4117 /* If that worked, we're done. */
4118 if (cp_parser_parse_definitely (parser))
4119 break;
4120
4121 /* If the functional-cast didn't work out, try a
4122 compound-literal. */
14d22dd6
MM
4123 if (cp_parser_allow_gnu_extensions_p (parser)
4124 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
a723baf1 4125 {
4038c495 4126 VEC(constructor_elt,gc) *initializer_list = NULL;
4f8163b1 4127 bool saved_in_type_id_in_expr_p;
a723baf1
MM
4128
4129 cp_parser_parse_tentatively (parser);
14d22dd6
MM
4130 /* Consume the `('. */
4131 cp_lexer_consume_token (parser->lexer);
4132 /* Parse the type. */
4f8163b1
MM
4133 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4134 parser->in_type_id_in_expr_p = true;
14d22dd6 4135 type = cp_parser_type_id (parser);
4f8163b1 4136 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14d22dd6
MM
4137 /* Look for the `)'. */
4138 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4139 /* Look for the `{'. */
4140 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4141 /* If things aren't going well, there's no need to
4142 keep going. */
4143 if (!cp_parser_error_occurred (parser))
a723baf1 4144 {
39703eb9 4145 bool non_constant_p;
14d22dd6 4146 /* Parse the initializer-list. */
21526606 4147 initializer_list
39703eb9 4148 = cp_parser_initializer_list (parser, &non_constant_p);
14d22dd6
MM
4149 /* Allow a trailing `,'. */
4150 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4151 cp_lexer_consume_token (parser->lexer);
4152 /* Look for the final `}'. */
4153 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1
MM
4154 }
4155 /* If that worked, we're definitely looking at a
4156 compound-literal expression. */
4157 if (cp_parser_parse_definitely (parser))
4158 {
4159 /* Warn the user that a compound literal is not
4160 allowed in standard C++. */
4161 if (pedantic)
4162 pedwarn ("ISO C++ forbids compound-literals");
4163 /* Form the representation of the compound-literal. */
21526606 4164 postfix_expression
a723baf1
MM
4165 = finish_compound_literal (type, initializer_list);
4166 break;
4167 }
4168 }
4169
4170 /* It must be a primary-expression. */
3db45ab5
MS
4171 postfix_expression
4172 = cp_parser_primary_expression (parser, address_p, cast_p,
02ed62dd
MM
4173 /*template_arg_p=*/false,
4174 &idk);
a723baf1
MM
4175 }
4176 break;
4177 }
4178
a723baf1
MM
4179 /* Keep looping until the postfix-expression is complete. */
4180 while (true)
4181 {
10b1d5e7
MM
4182 if (idk == CP_ID_KIND_UNQUALIFIED
4183 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
a723baf1 4184 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
b3445994 4185 /* It is not a Koenig lookup function call. */
21526606 4186 postfix_expression
b3445994 4187 = unqualified_name_lookup_error (postfix_expression);
21526606 4188
a723baf1
MM
4189 /* Peek at the next token. */
4190 token = cp_lexer_peek_token (parser->lexer);
4191
4192 switch (token->type)
4193 {
4194 case CPP_OPEN_SQUARE:
7a3ea201
RH
4195 postfix_expression
4196 = cp_parser_postfix_open_square_expression (parser,
4197 postfix_expression,
4198 false);
4199 idk = CP_ID_KIND_NONE;
a723baf1
MM
4200 break;
4201
4202 case CPP_OPEN_PAREN:
4203 /* postfix-expression ( expression-list [opt] ) */
4204 {
6d80c4b9 4205 bool koenig_p;
88a7beb7
MM
4206 bool is_builtin_constant_p;
4207 bool saved_integral_constant_expression_p = false;
4208 bool saved_non_integral_constant_expression_p = false;
4209 tree args;
4210
c8094d83 4211 is_builtin_constant_p
88a7beb7
MM
4212 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4213 if (is_builtin_constant_p)
4214 {
4215 /* The whole point of __builtin_constant_p is to allow
4216 non-constant expressions to appear as arguments. */
4217 saved_integral_constant_expression_p
4218 = parser->integral_constant_expression_p;
4219 saved_non_integral_constant_expression_p
4220 = parser->non_integral_constant_expression_p;
4221 parser->integral_constant_expression_p = false;
4222 }
4223 args = (cp_parser_parenthesized_expression_list
c8094d83 4224 (parser, /*is_attribute_list=*/false,
88a7beb7
MM
4225 /*cast_p=*/false,
4226 /*non_constant_p=*/NULL));
4227 if (is_builtin_constant_p)
4228 {
4229 parser->integral_constant_expression_p
4230 = saved_integral_constant_expression_p;
4231 parser->non_integral_constant_expression_p
4232 = saved_non_integral_constant_expression_p;
4233 }
a723baf1 4234
7efa3e22
NS
4235 if (args == error_mark_node)
4236 {
4237 postfix_expression = error_mark_node;
4238 break;
4239 }
21526606 4240
14d22dd6
MM
4241 /* Function calls are not permitted in
4242 constant-expressions. */
100d337a
MA
4243 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4244 && cp_parser_non_integral_constant_expression (parser,
4245 "a function call"))
14d22dd6 4246 {
625cbf93
MM
4247 postfix_expression = error_mark_node;
4248 break;
14d22dd6 4249 }
a723baf1 4250
6d80c4b9 4251 koenig_p = false;
399dedb9
NS
4252 if (idk == CP_ID_KIND_UNQUALIFIED)
4253 {
89d594a2
NS
4254 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4255 {
4256 if (args)
4257 {
4258 koenig_p = true;
4259 postfix_expression
4260 = perform_koenig_lookup (postfix_expression, args);
4261 }
4262 else
4263 postfix_expression
4264 = unqualified_fn_lookup_error (postfix_expression);
4265 }
676e33ca
MM
4266 /* We do not perform argument-dependent lookup if
4267 normal lookup finds a non-function, in accordance
4268 with the expected resolution of DR 218. */
89d594a2 4269 else if (args && is_overloaded_fn (postfix_expression))
6d80c4b9 4270 {
89d594a2
NS
4271 tree fn = get_first_fn (postfix_expression);
4272
4273 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4274 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4275
4276 /* Only do argument dependent lookup if regular
4277 lookup does not find a set of member functions.
4278 [basic.lookup.koenig]/2a */
4279 if (!DECL_FUNCTION_MEMBER_P (fn))
4280 {
4281 koenig_p = true;
4282 postfix_expression
4283 = perform_koenig_lookup (postfix_expression, args);
4284 }
6d80c4b9 4285 }
399dedb9 4286 }
21526606 4287
d17811fd 4288 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
a723baf1 4289 {
d17811fd
MM
4290 tree instance = TREE_OPERAND (postfix_expression, 0);
4291 tree fn = TREE_OPERAND (postfix_expression, 1);
4292
4293 if (processing_template_decl
4294 && (type_dependent_expression_p (instance)
4295 || (!BASELINK_P (fn)
4296 && TREE_CODE (fn) != FIELD_DECL)
584672ee 4297 || type_dependent_expression_p (fn)
d17811fd
MM
4298 || any_type_dependent_arguments_p (args)))
4299 {
4300 postfix_expression
6de9cd9a
DN
4301 = build_min_nt (CALL_EXPR, postfix_expression,
4302 args, NULL_TREE);
d17811fd
MM
4303 break;
4304 }
9f880ef9
MM
4305
4306 if (BASELINK_P (fn))
4307 postfix_expression
21526606
EC
4308 = (build_new_method_call
4309 (instance, fn, args, NULL_TREE,
4310 (idk == CP_ID_KIND_QUALIFIED
63c9a190
MM
4311 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4312 /*fn_p=*/NULL));
9f880ef9
MM
4313 else
4314 postfix_expression
4315 = finish_call_expr (postfix_expression, args,
4316 /*disallow_virtual=*/false,
4317 /*koenig_p=*/false);
a723baf1 4318 }
d17811fd
MM
4319 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4320 || TREE_CODE (postfix_expression) == MEMBER_REF
4321 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
a723baf1
MM
4322 postfix_expression = (build_offset_ref_call_from_tree
4323 (postfix_expression, args));
b3445994 4324 else if (idk == CP_ID_KIND_QUALIFIED)
2050a1bb
MM
4325 /* A call to a static class member, or a namespace-scope
4326 function. */
4327 postfix_expression
4328 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
4329 /*disallow_virtual=*/true,
4330 koenig_p);
a723baf1 4331 else
2050a1bb 4332 /* All other function calls. */
21526606
EC
4333 postfix_expression
4334 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
4335 /*disallow_virtual=*/false,
4336 koenig_p);
a723baf1
MM
4337
4338 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
b3445994 4339 idk = CP_ID_KIND_NONE;
a723baf1
MM
4340 }
4341 break;
21526606 4342
a723baf1
MM
4343 case CPP_DOT:
4344 case CPP_DEREF:
21526606
EC
4345 /* postfix-expression . template [opt] id-expression
4346 postfix-expression . pseudo-destructor-name
a723baf1
MM
4347 postfix-expression -> template [opt] id-expression
4348 postfix-expression -> pseudo-destructor-name */
98ca843c 4349
7a3ea201
RH
4350 /* Consume the `.' or `->' operator. */
4351 cp_lexer_consume_token (parser->lexer);
a723baf1 4352
7a3ea201
RH
4353 postfix_expression
4354 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4355 postfix_expression,
4356 false, &idk);
a723baf1
MM
4357 break;
4358
4359 case CPP_PLUS_PLUS:
4360 /* postfix-expression ++ */
4361 /* Consume the `++' token. */
4362 cp_lexer_consume_token (parser->lexer);
a5ac3982 4363 /* Generate a representation for the complete expression. */
21526606
EC
4364 postfix_expression
4365 = finish_increment_expr (postfix_expression,
a5ac3982 4366 POSTINCREMENT_EXPR);
14d22dd6 4367 /* Increments may not appear in constant-expressions. */
625cbf93
MM
4368 if (cp_parser_non_integral_constant_expression (parser,
4369 "an increment"))
4370 postfix_expression = error_mark_node;
b3445994 4371 idk = CP_ID_KIND_NONE;
a723baf1
MM
4372 break;
4373
4374 case CPP_MINUS_MINUS:
4375 /* postfix-expression -- */
4376 /* Consume the `--' token. */
4377 cp_lexer_consume_token (parser->lexer);
a5ac3982 4378 /* Generate a representation for the complete expression. */
21526606
EC
4379 postfix_expression
4380 = finish_increment_expr (postfix_expression,
a5ac3982 4381 POSTDECREMENT_EXPR);
14d22dd6 4382 /* Decrements may not appear in constant-expressions. */
625cbf93
MM
4383 if (cp_parser_non_integral_constant_expression (parser,
4384 "a decrement"))
4385 postfix_expression = error_mark_node;
b3445994 4386 idk = CP_ID_KIND_NONE;
a723baf1
MM
4387 break;
4388
4389 default:
4390 return postfix_expression;
4391 }
4392 }
4393
4394 /* We should never get here. */
315fb5db 4395 gcc_unreachable ();
a723baf1
MM
4396 return error_mark_node;
4397}
4398
7a3ea201
RH
4399/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4400 by cp_parser_builtin_offsetof. We're looking for
4401
4402 postfix-expression [ expression ]
4403
4404 FOR_OFFSETOF is set if we're being called in that context, which
4405 changes how we deal with integer constant expressions. */
4406
4407static tree
4408cp_parser_postfix_open_square_expression (cp_parser *parser,
4409 tree postfix_expression,
4410 bool for_offsetof)
4411{
4412 tree index;
4413
4414 /* Consume the `[' token. */
4415 cp_lexer_consume_token (parser->lexer);
4416
4417 /* Parse the index expression. */
4418 /* ??? For offsetof, there is a question of what to allow here. If
4419 offsetof is not being used in an integral constant expression context,
4420 then we *could* get the right answer by computing the value at runtime.
4421 If we are in an integral constant expression context, then we might
4422 could accept any constant expression; hard to say without analysis.
4423 Rather than open the barn door too wide right away, allow only integer
77880ae4 4424 constant expressions here. */
7a3ea201
RH
4425 if (for_offsetof)
4426 index = cp_parser_constant_expression (parser, false, NULL);
4427 else
93678513 4428 index = cp_parser_expression (parser, /*cast_p=*/false);
7a3ea201
RH
4429
4430 /* Look for the closing `]'. */
4431 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4432
4433 /* Build the ARRAY_REF. */
4434 postfix_expression = grok_array_decl (postfix_expression, index);
4435
4436 /* When not doing offsetof, array references are not permitted in
4437 constant-expressions. */
4438 if (!for_offsetof
4439 && (cp_parser_non_integral_constant_expression
4440 (parser, "an array reference")))
4441 postfix_expression = error_mark_node;
4442
4443 return postfix_expression;
4444}
4445
4446/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4447 by cp_parser_builtin_offsetof. We're looking for
4448
4449 postfix-expression . template [opt] id-expression
4450 postfix-expression . pseudo-destructor-name
4451 postfix-expression -> template [opt] id-expression
4452 postfix-expression -> pseudo-destructor-name
4453
4454 FOR_OFFSETOF is set if we're being called in that context. That sorta
4455 limits what of the above we'll actually accept, but nevermind.
4456 TOKEN_TYPE is the "." or "->" token, which will already have been
4457 removed from the stream. */
4458
4459static tree
4460cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4461 enum cpp_ttype token_type,
4462 tree postfix_expression,
4463 bool for_offsetof, cp_id_kind *idk)
4464{
4465 tree name;
4466 bool dependent_p;
17a27b4f 4467 bool pseudo_destructor_p;
7a3ea201
RH
4468 tree scope = NULL_TREE;
4469
4470 /* If this is a `->' operator, dereference the pointer. */
4471 if (token_type == CPP_DEREF)
4472 postfix_expression = build_x_arrow (postfix_expression);
4473 /* Check to see whether or not the expression is type-dependent. */
4474 dependent_p = type_dependent_expression_p (postfix_expression);
4475 /* The identifier following the `->' or `.' is not qualified. */
4476 parser->scope = NULL_TREE;
4477 parser->qualifying_scope = NULL_TREE;
4478 parser->object_scope = NULL_TREE;
4479 *idk = CP_ID_KIND_NONE;
4480 /* Enter the scope corresponding to the type of the object
4481 given by the POSTFIX_EXPRESSION. */
4482 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4483 {
4484 scope = TREE_TYPE (postfix_expression);
4485 /* According to the standard, no expression should ever have
4486 reference type. Unfortunately, we do not currently match
4487 the standard in this respect in that our internal representation
4488 of an expression may have reference type even when the standard
4489 says it does not. Therefore, we have to manually obtain the
4490 underlying type here. */
4491 scope = non_reference (scope);
4492 /* The type of the POSTFIX_EXPRESSION must be complete. */
e4ba2534
MM
4493 if (scope == unknown_type_node)
4494 {
4495 error ("%qE does not have class type", postfix_expression);
4496 scope = NULL_TREE;
4497 }
4498 else
4499 scope = complete_type_or_else (scope, NULL_TREE);
7a3ea201
RH
4500 /* Let the name lookup machinery know that we are processing a
4501 class member access expression. */
4502 parser->context->object_type = scope;
4503 /* If something went wrong, we want to be able to discern that case,
4504 as opposed to the case where there was no SCOPE due to the type
4505 of expression being dependent. */
4506 if (!scope)
4507 scope = error_mark_node;
4508 /* If the SCOPE was erroneous, make the various semantic analysis
4509 functions exit quickly -- and without issuing additional error
4510 messages. */
4511 if (scope == error_mark_node)
4512 postfix_expression = error_mark_node;
4513 }
4514
17a27b4f
MM
4515 /* Assume this expression is not a pseudo-destructor access. */
4516 pseudo_destructor_p = false;
4517
4518 /* If the SCOPE is a scalar type, then, if this is a valid program,
4519 we must be looking at a pseudo-destructor-name. */
4520 if (scope && SCALAR_TYPE_P (scope))
7a3ea201 4521 {
17a27b4f
MM
4522 tree s;
4523 tree type;
4524
4525 cp_parser_parse_tentatively (parser);
4526 /* Parse the pseudo-destructor-name. */
4527 s = NULL_TREE;
4528 cp_parser_pseudo_destructor_name (parser, &s, &type);
4529 if (cp_parser_parse_definitely (parser))
4530 {
4531 pseudo_destructor_p = true;
4532 postfix_expression
4533 = finish_pseudo_destructor_expr (postfix_expression,
4534 s, TREE_TYPE (type));
4535 }
4536 }
4537
4538 if (!pseudo_destructor_p)
4539 {
4540 /* If the SCOPE is not a scalar type, we are looking at an
4541 ordinary class member access expression, rather than a
4542 pseudo-destructor-name. */
02ed62dd 4543 bool template_p;
7a3ea201 4544 /* Parse the id-expression. */
3db45ab5
MS
4545 name = (cp_parser_id_expression
4546 (parser,
02ed62dd
MM
4547 cp_parser_optional_template_keyword (parser),
4548 /*check_dependency_p=*/true,
4549 &template_p,
fa6098f8
MM
4550 /*declarator_p=*/false,
4551 /*optional_p=*/false));
7a3ea201
RH
4552 /* In general, build a SCOPE_REF if the member name is qualified.
4553 However, if the name was not dependent and has already been
4554 resolved; there is no need to build the SCOPE_REF. For example;
4555
0cbd7506
MS
4556 struct X { void f(); };
4557 template <typename T> void f(T* t) { t->X::f(); }
7a3ea201
RH
4558
4559 Even though "t" is dependent, "X::f" is not and has been resolved
4560 to a BASELINK; there is no need to include scope information. */
4561
4562 /* But we do need to remember that there was an explicit scope for
4563 virtual function calls. */
4564 if (parser->scope)
4565 *idk = CP_ID_KIND_QUALIFIED;
4566
fc6a28d7
MM
4567 /* If the name is a template-id that names a type, we will get a
4568 TYPE_DECL here. That is invalid code. */
4569 if (TREE_CODE (name) == TYPE_DECL)
7a3ea201 4570 {
fc6a28d7
MM
4571 error ("invalid use of %qD", name);
4572 postfix_expression = error_mark_node;
4573 }
4574 else
4575 {
4576 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4577 {
02ed62dd
MM
4578 name = build_qualified_name (/*type=*/NULL_TREE,
4579 parser->scope,
4580 name,
4581 template_p);
fc6a28d7
MM
4582 parser->scope = NULL_TREE;
4583 parser->qualifying_scope = NULL_TREE;
4584 parser->object_scope = NULL_TREE;
4585 }
4586 if (scope && name && BASELINK_P (name))
4587 adjust_result_of_qualified_name_lookup
4588 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4589 postfix_expression
02ed62dd
MM
4590 = finish_class_member_access_expr (postfix_expression, name,
4591 template_p);
7a3ea201 4592 }
7a3ea201 4593 }
7a3ea201
RH
4594
4595 /* We no longer need to look up names in the scope of the object on
4596 the left-hand side of the `.' or `->' operator. */
4597 parser->context->object_type = NULL_TREE;
4598
4599 /* Outside of offsetof, these operators may not appear in
4600 constant-expressions. */
4601 if (!for_offsetof
98ca843c 4602 && (cp_parser_non_integral_constant_expression
7a3ea201
RH
4603 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4604 postfix_expression = error_mark_node;
4605
4606 return postfix_expression;
4607}
4608
7efa3e22 4609/* Parse a parenthesized expression-list.
a723baf1
MM
4610
4611 expression-list:
4612 assignment-expression
4613 expression-list, assignment-expression
4614
7efa3e22
NS
4615 attribute-list:
4616 expression-list
4617 identifier
4618 identifier, expression-list
4619
93678513
MM
4620 CAST_P is true if this expression is the target of a cast.
4621
a723baf1
MM
4622 Returns a TREE_LIST. The TREE_VALUE of each node is a
4623 representation of an assignment-expression. Note that a TREE_LIST
7efa3e22
NS
4624 is returned even if there is only a single expression in the list.
4625 error_mark_node is returned if the ( and or ) are
4626 missing. NULL_TREE is returned on no expressions. The parentheses
4627 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
39703eb9
MM
4628 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4629 indicates whether or not all of the expressions in the list were
4630 constant. */
a723baf1
MM
4631
4632static tree
21526606 4633cp_parser_parenthesized_expression_list (cp_parser* parser,
39703eb9 4634 bool is_attribute_list,
93678513 4635 bool cast_p,
39703eb9 4636 bool *non_constant_p)
a723baf1
MM
4637{
4638 tree expression_list = NULL_TREE;
1ed3dfd5 4639 bool fold_expr_p = is_attribute_list;
7efa3e22 4640 tree identifier = NULL_TREE;
39703eb9
MM
4641
4642 /* Assume all the expressions will be constant. */
4643 if (non_constant_p)
4644 *non_constant_p = false;
4645
7efa3e22
NS
4646 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4647 return error_mark_node;
21526606 4648
a723baf1 4649 /* Consume expressions until there are no more. */
7efa3e22
NS
4650 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4651 while (true)
4652 {
4653 tree expr;
21526606 4654
7efa3e22
NS
4655 /* At the beginning of attribute lists, check to see if the
4656 next token is an identifier. */
4657 if (is_attribute_list
4658 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4659 {
4660 cp_token *token;
21526606 4661
7efa3e22
NS
4662 /* Consume the identifier. */
4663 token = cp_lexer_consume_token (parser->lexer);
4664 /* Save the identifier. */
4665 identifier = token->value;
4666 }
4667 else
4668 {
4669 /* Parse the next assignment-expression. */
39703eb9
MM
4670 if (non_constant_p)
4671 {
4672 bool expr_non_constant_p;
21526606 4673 expr = (cp_parser_constant_expression
39703eb9
MM
4674 (parser, /*allow_non_constant_p=*/true,
4675 &expr_non_constant_p));
4676 if (expr_non_constant_p)
4677 *non_constant_p = true;
4678 }
4679 else
93678513 4680 expr = cp_parser_assignment_expression (parser, cast_p);
a723baf1 4681
1ed3dfd5
GB
4682 if (fold_expr_p)
4683 expr = fold_non_dependent_expr (expr);
4684
7efa3e22
NS
4685 /* Add it to the list. We add error_mark_node
4686 expressions to the list, so that we can still tell if
4687 the correct form for a parenthesized expression-list
4688 is found. That gives better errors. */
4689 expression_list = tree_cons (NULL_TREE, expr, expression_list);
a723baf1 4690
7efa3e22
NS
4691 if (expr == error_mark_node)
4692 goto skip_comma;
4693 }
a723baf1 4694
7efa3e22
NS
4695 /* After the first item, attribute lists look the same as
4696 expression lists. */
4697 is_attribute_list = false;
21526606 4698
7efa3e22
NS
4699 get_comma:;
4700 /* If the next token isn't a `,', then we are done. */
4701 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4702 break;
4703
4704 /* Otherwise, consume the `,' and keep going. */
4705 cp_lexer_consume_token (parser->lexer);
4706 }
21526606 4707
7efa3e22
NS
4708 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4709 {
4710 int ending;
21526606 4711
7efa3e22
NS
4712 skip_comma:;
4713 /* We try and resync to an unnested comma, as that will give the
4714 user better diagnostics. */
21526606
EC
4715 ending = cp_parser_skip_to_closing_parenthesis (parser,
4716 /*recovering=*/true,
4bb8ca28 4717 /*or_comma=*/true,
a668c6ad 4718 /*consume_paren=*/true);
7efa3e22
NS
4719 if (ending < 0)
4720 goto get_comma;
4721 if (!ending)
4722 return error_mark_node;
a723baf1
MM
4723 }
4724
4725 /* We built up the list in reverse order so we must reverse it now. */
7efa3e22
NS
4726 expression_list = nreverse (expression_list);
4727 if (identifier)
4728 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
21526606 4729
7efa3e22 4730 return expression_list;
a723baf1
MM
4731}
4732
4733/* Parse a pseudo-destructor-name.
4734
4735 pseudo-destructor-name:
4736 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4737 :: [opt] nested-name-specifier template template-id :: ~ type-name
4738 :: [opt] nested-name-specifier [opt] ~ type-name
4739
4740 If either of the first two productions is used, sets *SCOPE to the
4741 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4742 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
d6e57462 4743 or ERROR_MARK_NODE if the parse fails. */
a723baf1
MM
4744
4745static void
21526606 4746cp_parser_pseudo_destructor_name (cp_parser* parser,
0cbd7506
MS
4747 tree* scope,
4748 tree* type)
a723baf1
MM
4749{
4750 bool nested_name_specifier_p;
4751
b14454ba
MM
4752 /* Assume that things will not work out. */
4753 *type = error_mark_node;
4754
a723baf1
MM
4755 /* Look for the optional `::' operator. */
4756 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4757 /* Look for the optional nested-name-specifier. */
21526606 4758 nested_name_specifier_p
a723baf1
MM
4759 = (cp_parser_nested_name_specifier_opt (parser,
4760 /*typename_keyword_p=*/false,
4761 /*check_dependency_p=*/true,
a668c6ad 4762 /*type_p=*/false,
21526606 4763 /*is_declaration=*/true)
a723baf1
MM
4764 != NULL_TREE);
4765 /* Now, if we saw a nested-name-specifier, we might be doing the
4766 second production. */
21526606 4767 if (nested_name_specifier_p
a723baf1
MM
4768 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4769 {
4770 /* Consume the `template' keyword. */
4771 cp_lexer_consume_token (parser->lexer);
4772 /* Parse the template-id. */
21526606 4773 cp_parser_template_id (parser,
a723baf1 4774 /*template_keyword_p=*/true,
a668c6ad
MM
4775 /*check_dependency_p=*/false,
4776 /*is_declaration=*/true);
a723baf1
MM
4777 /* Look for the `::' token. */
4778 cp_parser_require (parser, CPP_SCOPE, "`::'");
4779 }
4780 /* If the next token is not a `~', then there might be some
9bcb9aae 4781 additional qualification. */
a723baf1
MM
4782 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4783 {
4784 /* Look for the type-name. */
4785 *scope = TREE_TYPE (cp_parser_type_name (parser));
d6e57462 4786
b14454ba
MM
4787 if (*scope == error_mark_node)
4788 return;
4789
4790 /* If we don't have ::~, then something has gone wrong. Since
4791 the only caller of this function is looking for something
4792 after `.' or `->' after a scalar type, most likely the
4793 program is trying to get a member of a non-aggregate
4794 type. */
4795 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
d6e57462
ILT
4796 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4797 {
4798 cp_parser_error (parser, "request for member of non-aggregate type");
d6e57462
ILT
4799 return;
4800 }
4801
a723baf1
MM
4802 /* Look for the `::' token. */
4803 cp_parser_require (parser, CPP_SCOPE, "`::'");
4804 }
4805 else
4806 *scope = NULL_TREE;
4807
4808 /* Look for the `~'. */
4809 cp_parser_require (parser, CPP_COMPL, "`~'");
4810 /* Look for the type-name again. We are not responsible for
4811 checking that it matches the first type-name. */
4812 *type = cp_parser_type_name (parser);
4813}
4814
4815/* Parse a unary-expression.
4816
4817 unary-expression:
4818 postfix-expression
4819 ++ cast-expression
4820 -- cast-expression
4821 unary-operator cast-expression
4822 sizeof unary-expression
4823 sizeof ( type-id )
4824 new-expression
4825 delete-expression
4826
4827 GNU Extensions:
4828
4829 unary-expression:
4830 __extension__ cast-expression
4831 __alignof__ unary-expression
4832 __alignof__ ( type-id )
4833 __real__ cast-expression
4834 __imag__ cast-expression
4835 && identifier
4836
4837 ADDRESS_P is true iff the unary-expression is appearing as the
93678513
MM
4838 operand of the `&' operator. CAST_P is true if this expression is
4839 the target of a cast.
a723baf1 4840
34cd5ae7 4841 Returns a representation of the expression. */
a723baf1
MM
4842
4843static tree
93678513 4844cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
a723baf1
MM
4845{
4846 cp_token *token;
4847 enum tree_code unary_operator;
4848
4849 /* Peek at the next token. */
4850 token = cp_lexer_peek_token (parser->lexer);
4851 /* Some keywords give away the kind of expression. */
4852 if (token->type == CPP_KEYWORD)
4853 {
4854 enum rid keyword = token->keyword;
4855
4856 switch (keyword)
4857 {
4858 case RID_ALIGNOF:
a723baf1
MM
4859 case RID_SIZEOF:
4860 {
4861 tree operand;
7a18b933 4862 enum tree_code op;
21526606 4863
7a18b933
NS
4864 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4865 /* Consume the token. */
a723baf1
MM
4866 cp_lexer_consume_token (parser->lexer);
4867 /* Parse the operand. */
4868 operand = cp_parser_sizeof_operand (parser, keyword);
4869
7a18b933
NS
4870 if (TYPE_P (operand))
4871 return cxx_sizeof_or_alignof_type (operand, op, true);
a723baf1 4872 else
7a18b933 4873 return cxx_sizeof_or_alignof_expr (operand, op);
a723baf1
MM
4874 }
4875
4876 case RID_NEW:
4877 return cp_parser_new_expression (parser);
4878
4879 case RID_DELETE:
4880 return cp_parser_delete_expression (parser);
21526606 4881
a723baf1
MM
4882 case RID_EXTENSION:
4883 {
4884 /* The saved value of the PEDANTIC flag. */
4885 int saved_pedantic;
4886 tree expr;
4887
4888 /* Save away the PEDANTIC flag. */
4889 cp_parser_extension_opt (parser, &saved_pedantic);
4890 /* Parse the cast-expression. */
d6b4ea85 4891 expr = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4892 /* Restore the PEDANTIC flag. */
4893 pedantic = saved_pedantic;
4894
4895 return expr;
4896 }
4897
4898 case RID_REALPART:
4899 case RID_IMAGPART:
4900 {
4901 tree expression;
4902
4903 /* Consume the `__real__' or `__imag__' token. */
4904 cp_lexer_consume_token (parser->lexer);
4905 /* Parse the cast-expression. */
d6b4ea85 4906 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4907 /* Create the complete representation. */
4908 return build_x_unary_op ((keyword == RID_REALPART
4909 ? REALPART_EXPR : IMAGPART_EXPR),
4910 expression);
4911 }
4912 break;
4913
4914 default:
4915 break;
4916 }
4917 }
4918
4919 /* Look for the `:: new' and `:: delete', which also signal the
4920 beginning of a new-expression, or delete-expression,
4921 respectively. If the next token is `::', then it might be one of
4922 these. */
4923 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4924 {
4925 enum rid keyword;
4926
4927 /* See if the token after the `::' is one of the keywords in
4928 which we're interested. */
4929 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4930 /* If it's `new', we have a new-expression. */
4931 if (keyword == RID_NEW)
4932 return cp_parser_new_expression (parser);
4933 /* Similarly, for `delete'. */
4934 else if (keyword == RID_DELETE)
4935 return cp_parser_delete_expression (parser);
4936 }
4937
4938 /* Look for a unary operator. */
4939 unary_operator = cp_parser_unary_operator (token);
4940 /* The `++' and `--' operators can be handled similarly, even though
4941 they are not technically unary-operators in the grammar. */
4942 if (unary_operator == ERROR_MARK)
4943 {
4944 if (token->type == CPP_PLUS_PLUS)
4945 unary_operator = PREINCREMENT_EXPR;
4946 else if (token->type == CPP_MINUS_MINUS)
4947 unary_operator = PREDECREMENT_EXPR;
4948 /* Handle the GNU address-of-label extension. */
4949 else if (cp_parser_allow_gnu_extensions_p (parser)
4950 && token->type == CPP_AND_AND)
4951 {
4952 tree identifier;
4953
4954 /* Consume the '&&' token. */
4955 cp_lexer_consume_token (parser->lexer);
4956 /* Look for the identifier. */
4957 identifier = cp_parser_identifier (parser);
4958 /* Create an expression representing the address. */
4959 return finish_label_address_expr (identifier);
4960 }
4961 }
4962 if (unary_operator != ERROR_MARK)
4963 {
4964 tree cast_expression;
a5ac3982
MM
4965 tree expression = error_mark_node;
4966 const char *non_constant_p = NULL;
a723baf1
MM
4967
4968 /* Consume the operator token. */
4969 token = cp_lexer_consume_token (parser->lexer);
4970 /* Parse the cast-expression. */
21526606 4971 cast_expression
c8094d83 4972 = cp_parser_cast_expression (parser,
93678513
MM
4973 unary_operator == ADDR_EXPR,
4974 /*cast_p=*/false);
a723baf1
MM
4975 /* Now, build an appropriate representation. */
4976 switch (unary_operator)
4977 {
4978 case INDIRECT_REF:
a5ac3982
MM
4979 non_constant_p = "`*'";
4980 expression = build_x_indirect_ref (cast_expression, "unary *");
4981 break;
4982
a723baf1 4983 case ADDR_EXPR:
7a3ea201 4984 non_constant_p = "`&'";
a5ac3982 4985 /* Fall through. */
d17811fd 4986 case BIT_NOT_EXPR:
a5ac3982
MM
4987 expression = build_x_unary_op (unary_operator, cast_expression);
4988 break;
4989
14d22dd6
MM
4990 case PREINCREMENT_EXPR:
4991 case PREDECREMENT_EXPR:
a5ac3982
MM
4992 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4993 ? "`++'" : "`--'");
14d22dd6 4994 /* Fall through. */
392e3d51 4995 case UNARY_PLUS_EXPR:
a723baf1
MM
4996 case NEGATE_EXPR:
4997 case TRUTH_NOT_EXPR:
a5ac3982
MM
4998 expression = finish_unary_op_expr (unary_operator, cast_expression);
4999 break;
a723baf1 5000
a723baf1 5001 default:
315fb5db 5002 gcc_unreachable ();
a723baf1 5003 }
a5ac3982 5004
98ca843c 5005 if (non_constant_p
625cbf93
MM
5006 && cp_parser_non_integral_constant_expression (parser,
5007 non_constant_p))
5008 expression = error_mark_node;
a5ac3982
MM
5009
5010 return expression;
a723baf1
MM
5011 }
5012
93678513 5013 return cp_parser_postfix_expression (parser, address_p, cast_p);
a723baf1
MM
5014}
5015
5016/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5017 unary-operator, the corresponding tree code is returned. */
5018
5019static enum tree_code
94edc4ab 5020cp_parser_unary_operator (cp_token* token)
a723baf1
MM
5021{
5022 switch (token->type)
5023 {
5024 case CPP_MULT:
5025 return INDIRECT_REF;
5026
5027 case CPP_AND:
5028 return ADDR_EXPR;
5029
5030 case CPP_PLUS:
392e3d51 5031 return UNARY_PLUS_EXPR;
a723baf1
MM
5032
5033 case CPP_MINUS:
5034 return NEGATE_EXPR;
5035
5036 case CPP_NOT:
5037 return TRUTH_NOT_EXPR;
21526606 5038
a723baf1
MM
5039 case CPP_COMPL:
5040 return BIT_NOT_EXPR;
5041
5042 default:
5043 return ERROR_MARK;
5044 }
5045}
5046
5047/* Parse a new-expression.
5048
ca099ac8 5049 new-expression:
a723baf1
MM
5050 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5051 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5052
5053 Returns a representation of the expression. */
5054
5055static tree
94edc4ab 5056cp_parser_new_expression (cp_parser* parser)
a723baf1
MM
5057{
5058 bool global_scope_p;
5059 tree placement;
5060 tree type;
5061 tree initializer;
058b15c1 5062 tree nelts;
a723baf1
MM
5063
5064 /* Look for the optional `::' operator. */
21526606 5065 global_scope_p
a723baf1
MM
5066 = (cp_parser_global_scope_opt (parser,
5067 /*current_scope_valid_p=*/false)
5068 != NULL_TREE);
5069 /* Look for the `new' operator. */
5070 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5071 /* There's no easy way to tell a new-placement from the
5072 `( type-id )' construct. */
5073 cp_parser_parse_tentatively (parser);
5074 /* Look for a new-placement. */
5075 placement = cp_parser_new_placement (parser);
5076 /* If that didn't work out, there's no new-placement. */
5077 if (!cp_parser_parse_definitely (parser))
5078 placement = NULL_TREE;
5079
5080 /* If the next token is a `(', then we have a parenthesized
5081 type-id. */
5082 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5083 {
5084 /* Consume the `('. */
5085 cp_lexer_consume_token (parser->lexer);
5086 /* Parse the type-id. */
5087 type = cp_parser_type_id (parser);
5088 /* Look for the closing `)'. */
5089 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
98ca843c 5090 /* There should not be a direct-new-declarator in this production,
0cbd7506 5091 but GCC used to allowed this, so we check and emit a sensible error
063e900f
GB
5092 message for this case. */
5093 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
0da99d4e
GB
5094 {
5095 error ("array bound forbidden after parenthesized type-id");
5096 inform ("try removing the parentheses around the type-id");
063e900f
GB
5097 cp_parser_direct_new_declarator (parser);
5098 }
17a27b4f 5099 nelts = NULL_TREE;
a723baf1
MM
5100 }
5101 /* Otherwise, there must be a new-type-id. */
5102 else
058b15c1 5103 type = cp_parser_new_type_id (parser, &nelts);
a723baf1
MM
5104
5105 /* If the next token is a `(', then we have a new-initializer. */
5106 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5107 initializer = cp_parser_new_initializer (parser);
5108 else
5109 initializer = NULL_TREE;
5110
625cbf93
MM
5111 /* A new-expression may not appear in an integral constant
5112 expression. */
5113 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5114 return error_mark_node;
5115
a723baf1 5116 /* Create a representation of the new-expression. */
058b15c1 5117 return build_new (placement, type, nelts, initializer, global_scope_p);
a723baf1
MM
5118}
5119
5120/* Parse a new-placement.
5121
5122 new-placement:
5123 ( expression-list )
5124
5125 Returns the same representation as for an expression-list. */
5126
5127static tree
94edc4ab 5128cp_parser_new_placement (cp_parser* parser)
a723baf1
MM
5129{
5130 tree expression_list;
5131
a723baf1 5132 /* Parse the expression-list. */
21526606 5133 expression_list = (cp_parser_parenthesized_expression_list
93678513
MM
5134 (parser, false, /*cast_p=*/false,
5135 /*non_constant_p=*/NULL));
a723baf1
MM
5136
5137 return expression_list;
5138}
5139
5140/* Parse a new-type-id.
5141
5142 new-type-id:
5143 type-specifier-seq new-declarator [opt]
5144
058b15c1
MM
5145 Returns the TYPE allocated. If the new-type-id indicates an array
5146 type, *NELTS is set to the number of elements in the last array
5147 bound; the TYPE will not include the last array bound. */
a723baf1
MM
5148
5149static tree
058b15c1 5150cp_parser_new_type_id (cp_parser* parser, tree *nelts)
a723baf1 5151{
62d1db17 5152 cp_decl_specifier_seq type_specifier_seq;
058b15c1
MM
5153 cp_declarator *new_declarator;
5154 cp_declarator *declarator;
5155 cp_declarator *outer_declarator;
a723baf1 5156 const char *saved_message;
058b15c1 5157 tree type;
a723baf1
MM
5158
5159 /* The type-specifier sequence must not contain type definitions.
5160 (It cannot contain declarations of new types either, but if they
5161 are not definitions we will catch that because they are not
5162 complete.) */
5163 saved_message = parser->type_definition_forbidden_message;
5164 parser->type_definition_forbidden_message
5165 = "types may not be defined in a new-type-id";
5166 /* Parse the type-specifier-seq. */
d4113656
MM
5167 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5168 &type_specifier_seq);
a723baf1
MM
5169 /* Restore the old message. */
5170 parser->type_definition_forbidden_message = saved_message;
5171 /* Parse the new-declarator. */
058b15c1
MM
5172 new_declarator = cp_parser_new_declarator_opt (parser);
5173
5174 /* Determine the number of elements in the last array dimension, if
5175 any. */
5176 *nelts = NULL_TREE;
5177 /* Skip down to the last array dimension. */
5178 declarator = new_declarator;
5179 outer_declarator = NULL;
5180 while (declarator && (declarator->kind == cdk_pointer
5181 || declarator->kind == cdk_ptrmem))
5182 {
5183 outer_declarator = declarator;
5184 declarator = declarator->declarator;
5185 }
98ca843c 5186 while (declarator
058b15c1
MM
5187 && declarator->kind == cdk_array
5188 && declarator->declarator
5189 && declarator->declarator->kind == cdk_array)
5190 {
5191 outer_declarator = declarator;
5192 declarator = declarator->declarator;
5193 }
98ca843c 5194
058b15c1
MM
5195 if (declarator && declarator->kind == cdk_array)
5196 {
5197 *nelts = declarator->u.array.bounds;
5198 if (*nelts == error_mark_node)
5199 *nelts = integer_one_node;
c8094d83 5200
058b15c1
MM
5201 if (outer_declarator)
5202 outer_declarator->declarator = declarator->declarator;
5203 else
5204 new_declarator = NULL;
5205 }
a723baf1 5206
62d1db17 5207 type = groktypename (&type_specifier_seq, new_declarator);
058b15c1
MM
5208 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5209 {
5210 *nelts = array_type_nelts_top (type);
5211 type = TREE_TYPE (type);
5212 }
5213 return type;
a723baf1
MM
5214}
5215
5216/* Parse an (optional) new-declarator.
5217
5218 new-declarator:
5219 ptr-operator new-declarator [opt]
5220 direct-new-declarator
5221
058b15c1 5222 Returns the declarator. */
a723baf1 5223
058b15c1 5224static cp_declarator *
94edc4ab 5225cp_parser_new_declarator_opt (cp_parser* parser)
a723baf1
MM
5226{
5227 enum tree_code code;
5228 tree type;
3c01e5df 5229 cp_cv_quals cv_quals;
a723baf1
MM
5230
5231 /* We don't know if there's a ptr-operator next, or not. */
5232 cp_parser_parse_tentatively (parser);
5233 /* Look for a ptr-operator. */
3c01e5df 5234 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
a723baf1
MM
5235 /* If that worked, look for more new-declarators. */
5236 if (cp_parser_parse_definitely (parser))
5237 {
058b15c1 5238 cp_declarator *declarator;
a723baf1
MM
5239
5240 /* Parse another optional declarator. */
5241 declarator = cp_parser_new_declarator_opt (parser);
5242
5243 /* Create the representation of the declarator. */
058b15c1 5244 if (type)
3c01e5df 5245 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
058b15c1 5246 else if (code == INDIRECT_REF)
3c01e5df 5247 declarator = make_pointer_declarator (cv_quals, declarator);
a723baf1 5248 else
3c01e5df 5249 declarator = make_reference_declarator (cv_quals, declarator);
a723baf1 5250
a723baf1
MM
5251 return declarator;
5252 }
5253
5254 /* If the next token is a `[', there is a direct-new-declarator. */
5255 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5256 return cp_parser_direct_new_declarator (parser);
5257
058b15c1 5258 return NULL;
a723baf1
MM
5259}
5260
5261/* Parse a direct-new-declarator.
5262
5263 direct-new-declarator:
5264 [ expression ]
21526606 5265 direct-new-declarator [constant-expression]
a723baf1 5266
058b15c1 5267 */
a723baf1 5268
058b15c1 5269static cp_declarator *
94edc4ab 5270cp_parser_direct_new_declarator (cp_parser* parser)
a723baf1 5271{
058b15c1 5272 cp_declarator *declarator = NULL;
a723baf1
MM
5273
5274 while (true)
5275 {
5276 tree expression;
5277
5278 /* Look for the opening `['. */
5279 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5280 /* The first expression is not required to be constant. */
5281 if (!declarator)
5282 {
93678513 5283 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
5284 /* The standard requires that the expression have integral
5285 type. DR 74 adds enumeration types. We believe that the
5286 real intent is that these expressions be handled like the
5287 expression in a `switch' condition, which also allows
5288 classes with a single conversion to integral or
5289 enumeration type. */
5290 if (!processing_template_decl)
5291 {
21526606 5292 expression
a723baf1
MM
5293 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5294 expression,
b746c5dc 5295 /*complain=*/true);
a723baf1
MM
5296 if (!expression)
5297 {
2a13a625 5298 error ("expression in new-declarator must have integral "
0cbd7506 5299 "or enumeration type");
a723baf1
MM
5300 expression = error_mark_node;
5301 }
5302 }
5303 }
5304 /* But all the other expressions must be. */
5305 else
21526606
EC
5306 expression
5307 = cp_parser_constant_expression (parser,
14d22dd6
MM
5308 /*allow_non_constant=*/false,
5309 NULL);
a723baf1
MM
5310 /* Look for the closing `]'. */
5311 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5312
5313 /* Add this bound to the declarator. */
058b15c1 5314 declarator = make_array_declarator (declarator, expression);
a723baf1
MM
5315
5316 /* If the next token is not a `[', then there are no more
5317 bounds. */
5318 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5319 break;
5320 }
5321
5322 return declarator;
5323}
5324
5325/* Parse a new-initializer.
5326
5327 new-initializer:
5328 ( expression-list [opt] )
5329
34cd5ae7 5330 Returns a representation of the expression-list. If there is no
a723baf1
MM
5331 expression-list, VOID_ZERO_NODE is returned. */
5332
5333static tree
94edc4ab 5334cp_parser_new_initializer (cp_parser* parser)
a723baf1
MM
5335{
5336 tree expression_list;
5337
21526606 5338 expression_list = (cp_parser_parenthesized_expression_list
93678513
MM
5339 (parser, false, /*cast_p=*/false,
5340 /*non_constant_p=*/NULL));
7efa3e22 5341 if (!expression_list)
a723baf1 5342 expression_list = void_zero_node;
a723baf1
MM
5343
5344 return expression_list;
5345}
5346
5347/* Parse a delete-expression.
5348
5349 delete-expression:
5350 :: [opt] delete cast-expression
5351 :: [opt] delete [ ] cast-expression
5352
5353 Returns a representation of the expression. */
5354
5355static tree
94edc4ab 5356cp_parser_delete_expression (cp_parser* parser)
a723baf1
MM
5357{
5358 bool global_scope_p;
5359 bool array_p;
5360 tree expression;
5361
5362 /* Look for the optional `::' operator. */
5363 global_scope_p
5364 = (cp_parser_global_scope_opt (parser,
5365 /*current_scope_valid_p=*/false)
5366 != NULL_TREE);
5367 /* Look for the `delete' keyword. */
5368 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5369 /* See if the array syntax is in use. */
5370 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5371 {
5372 /* Consume the `[' token. */
5373 cp_lexer_consume_token (parser->lexer);
5374 /* Look for the `]' token. */
5375 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5376 /* Remember that this is the `[]' construct. */
5377 array_p = true;
5378 }
5379 else
5380 array_p = false;
5381
5382 /* Parse the cast-expression. */
d6b4ea85 5383 expression = cp_parser_simple_cast_expression (parser);
a723baf1 5384
625cbf93
MM
5385 /* A delete-expression may not appear in an integral constant
5386 expression. */
5387 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5388 return error_mark_node;
5389
a723baf1
MM
5390 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5391}
5392
5393/* Parse a cast-expression.
5394
5395 cast-expression:
5396 unary-expression
5397 ( type-id ) cast-expression
5398
93678513
MM
5399 ADDRESS_P is true iff the unary-expression is appearing as the
5400 operand of the `&' operator. CAST_P is true if this expression is
5401 the target of a cast.
5402
a723baf1
MM
5403 Returns a representation of the expression. */
5404
5405static tree
93678513 5406cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
a723baf1
MM
5407{
5408 /* If it's a `(', then we might be looking at a cast. */
5409 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5410 {
5411 tree type = NULL_TREE;
5412 tree expr = NULL_TREE;
5413 bool compound_literal_p;
5414 const char *saved_message;
5415
5416 /* There's no way to know yet whether or not this is a cast.
5417 For example, `(int (3))' is a unary-expression, while `(int)
5418 3' is a cast. So, we resort to parsing tentatively. */
5419 cp_parser_parse_tentatively (parser);
5420 /* Types may not be defined in a cast. */
5421 saved_message = parser->type_definition_forbidden_message;
5422 parser->type_definition_forbidden_message
5423 = "types may not be defined in casts";
5424 /* Consume the `('. */
5425 cp_lexer_consume_token (parser->lexer);
5426 /* A very tricky bit is that `(struct S) { 3 }' is a
5427 compound-literal (which we permit in C++ as an extension).
5428 But, that construct is not a cast-expression -- it is a
5429 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5430 is legal; if the compound-literal were a cast-expression,
5431 you'd need an extra set of parentheses.) But, if we parse
5432 the type-id, and it happens to be a class-specifier, then we
5433 will commit to the parse at that point, because we cannot
5434 undo the action that is done when creating a new class. So,
21526606 5435 then we cannot back up and do a postfix-expression.
a723baf1
MM
5436
5437 Therefore, we scan ahead to the closing `)', and check to see
5438 if the token after the `)' is a `{'. If so, we are not
21526606 5439 looking at a cast-expression.
a723baf1
MM
5440
5441 Save tokens so that we can put them back. */
5442 cp_lexer_save_tokens (parser->lexer);
5443 /* Skip tokens until the next token is a closing parenthesis.
5444 If we find the closing `)', and the next token is a `{', then
5445 we are looking at a compound-literal. */
21526606 5446 compound_literal_p
a668c6ad
MM
5447 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5448 /*consume_paren=*/true)
a723baf1
MM
5449 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5450 /* Roll back the tokens we skipped. */
5451 cp_lexer_rollback_tokens (parser->lexer);
5452 /* If we were looking at a compound-literal, simulate an error
5453 so that the call to cp_parser_parse_definitely below will
5454 fail. */
5455 if (compound_literal_p)
5456 cp_parser_simulate_error (parser);
5457 else
5458 {
4f8163b1
MM
5459 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5460 parser->in_type_id_in_expr_p = true;
a723baf1
MM
5461 /* Look for the type-id. */
5462 type = cp_parser_type_id (parser);
5463 /* Look for the closing `)'. */
5464 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4f8163b1 5465 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
5466 }
5467
5468 /* Restore the saved message. */
5469 parser->type_definition_forbidden_message = saved_message;
5470
bbaab916 5471 /* If ok so far, parse the dependent expression. We cannot be
0cbd7506
MS
5472 sure it is a cast. Consider `(T ())'. It is a parenthesized
5473 ctor of T, but looks like a cast to function returning T
5474 without a dependent expression. */
bbaab916 5475 if (!cp_parser_error_occurred (parser))
c8094d83 5476 expr = cp_parser_cast_expression (parser,
93678513
MM
5477 /*address_p=*/false,
5478 /*cast_p=*/true);
bbaab916 5479
a723baf1
MM
5480 if (cp_parser_parse_definitely (parser))
5481 {
a723baf1 5482 /* Warn about old-style casts, if so requested. */
21526606
EC
5483 if (warn_old_style_cast
5484 && !in_system_header
5485 && !VOID_TYPE_P (type)
a723baf1 5486 && current_lang_name != lang_name_c)
b323323f 5487 warning (OPT_Wold_style_cast, "use of old-style cast");
14d22dd6
MM
5488
5489 /* Only type conversions to integral or enumeration types
5490 can be used in constant-expressions. */
015c2c66 5491 if (!cast_valid_in_integral_constant_expression_p (type)
98ca843c 5492 && (cp_parser_non_integral_constant_expression
625cbf93
MM
5493 (parser,
5494 "a cast to a type other than an integral or "
5495 "enumeration type")))
5496 return error_mark_node;
5497
a723baf1
MM
5498 /* Perform the cast. */
5499 expr = build_c_cast (type, expr);
bbaab916 5500 return expr;
a723baf1 5501 }
a723baf1
MM
5502 }
5503
5504 /* If we get here, then it's not a cast, so it must be a
5505 unary-expression. */
93678513 5506 return cp_parser_unary_expression (parser, address_p, cast_p);
a723baf1
MM
5507}
5508
b8b94c5b 5509/* Parse a binary expression of the general form:
a723baf1
MM
5510
5511 pm-expression:
5512 cast-expression
5513 pm-expression .* cast-expression
5514 pm-expression ->* cast-expression
5515
77077b39 5516 multiplicative-expression:
a723baf1
MM
5517 pm-expression
5518 multiplicative-expression * pm-expression
5519 multiplicative-expression / pm-expression
5520 multiplicative-expression % pm-expression
5521
a723baf1
MM
5522 additive-expression:
5523 multiplicative-expression
5524 additive-expression + multiplicative-expression
5525 additive-expression - multiplicative-expression
5526
a723baf1
MM
5527 shift-expression:
5528 additive-expression
5529 shift-expression << additive-expression
5530 shift-expression >> additive-expression
5531
a723baf1
MM
5532 relational-expression:
5533 shift-expression
5534 relational-expression < shift-expression
5535 relational-expression > shift-expression
5536 relational-expression <= shift-expression
5537 relational-expression >= shift-expression
5538
b8b94c5b 5539 GNU Extension:
c8094d83 5540
a723baf1
MM
5541 relational-expression:
5542 relational-expression <? shift-expression
5543 relational-expression >? shift-expression
5544
a723baf1
MM
5545 equality-expression:
5546 relational-expression
5547 equality-expression == relational-expression
5548 equality-expression != relational-expression
5549
a723baf1
MM
5550 and-expression:
5551 equality-expression
5552 and-expression & equality-expression
5553
a723baf1
MM
5554 exclusive-or-expression:
5555 and-expression
5556 exclusive-or-expression ^ and-expression
5557
b8b94c5b
PB
5558 inclusive-or-expression:
5559 exclusive-or-expression
5560 inclusive-or-expression | exclusive-or-expression
a723baf1 5561
b8b94c5b
PB
5562 logical-and-expression:
5563 inclusive-or-expression
5564 logical-and-expression && inclusive-or-expression
a723baf1 5565
b8b94c5b
PB
5566 logical-or-expression:
5567 logical-and-expression
5568 logical-or-expression || logical-and-expression
a723baf1 5569
b8b94c5b 5570 All these are implemented with a single function like:
a723baf1 5571
b8b94c5b
PB
5572 binary-expression:
5573 simple-cast-expression
5574 binary-expression <token> binary-expression
a723baf1 5575
93678513
MM
5576 CAST_P is true if this expression is the target of a cast.
5577
b8b94c5b
PB
5578 The binops_by_token map is used to get the tree codes for each <token> type.
5579 binary-expressions are associated according to a precedence table. */
a723baf1 5580
b8b94c5b
PB
5581#define TOKEN_PRECEDENCE(token) \
5582 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5583 ? PREC_NOT_OPERATOR \
5584 : binops_by_token[token->type].prec)
a723baf1
MM
5585
5586static tree
93678513 5587cp_parser_binary_expression (cp_parser* parser, bool cast_p)
a723baf1 5588{
b8b94c5b
PB
5589 cp_parser_expression_stack stack;
5590 cp_parser_expression_stack_entry *sp = &stack[0];
5591 tree lhs, rhs;
5592 cp_token *token;
5593 enum tree_code tree_type;
5594 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5595 bool overloaded_p;
a723baf1 5596
b8b94c5b 5597 /* Parse the first expression. */
93678513 5598 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
a723baf1 5599
b8b94c5b
PB
5600 for (;;)
5601 {
5602 /* Get an operator token. */
5603 token = cp_lexer_peek_token (parser->lexer);
8ff24a79 5604
b8b94c5b
PB
5605 new_prec = TOKEN_PRECEDENCE (token);
5606
5607 /* Popping an entry off the stack means we completed a subexpression:
0cbd7506
MS
5608 - either we found a token which is not an operator (`>' where it is not
5609 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5610 will happen repeatedly;
5611 - or, we found an operator which has lower priority. This is the case
5612 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5613 parsing `3 * 4'. */
b8b94c5b 5614 if (new_prec <= prec)
0cbd7506
MS
5615 {
5616 if (sp == stack)
b8b94c5b 5617 break;
0cbd7506 5618 else
b8b94c5b 5619 goto pop;
0cbd7506 5620 }
a723baf1 5621
b8b94c5b
PB
5622 get_rhs:
5623 tree_type = binops_by_token[token->type].tree_type;
a723baf1 5624
03fd3f84 5625 /* We used the operator token. */
b8b94c5b 5626 cp_lexer_consume_token (parser->lexer);
a723baf1 5627
b8b94c5b 5628 /* Extract another operand. It may be the RHS of this expression
0cbd7506 5629 or the LHS of a new, higher priority expression. */
b8b94c5b 5630 rhs = cp_parser_simple_cast_expression (parser);
a723baf1 5631
b8b94c5b 5632 /* Get another operator token. Look up its precedence to avoid
0cbd7506
MS
5633 building a useless (immediately popped) stack entry for common
5634 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
b8b94c5b
PB
5635 token = cp_lexer_peek_token (parser->lexer);
5636 lookahead_prec = TOKEN_PRECEDENCE (token);
5637 if (lookahead_prec > new_prec)
0cbd7506
MS
5638 {
5639 /* ... and prepare to parse the RHS of the new, higher priority
5640 expression. Since precedence levels on the stack are
43c2a69a
PB
5641 monotonically increasing, we do not have to care about
5642 stack overflows. */
0cbd7506
MS
5643 sp->prec = prec;
5644 sp->tree_type = tree_type;
5645 sp->lhs = lhs;
5646 sp++;
5647 lhs = rhs;
5648 prec = new_prec;
5649 new_prec = lookahead_prec;
5650 goto get_rhs;
5651
5652 pop:
5653 /* If the stack is not empty, we have parsed into LHS the right side
b8b94c5b 5654 (`4' in the example above) of an expression we had suspended.
c8094d83 5655 We can use the information on the stack to recover the LHS (`3')
b8b94c5b
PB
5656 from the stack together with the tree code (`MULT_EXPR'), and
5657 the precedence of the higher level subexpression
5658 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5659 which will be used to actually build the additive expression. */
0cbd7506 5660 --sp;
b8b94c5b 5661 prec = sp->prec;
0cbd7506
MS
5662 tree_type = sp->tree_type;
5663 rhs = lhs;
5664 lhs = sp->lhs;
5665 }
a723baf1 5666
b8b94c5b
PB
5667 overloaded_p = false;
5668 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
a723baf1 5669
b8b94c5b 5670 /* If the binary operator required the use of an overloaded operator,
0cbd7506
MS
5671 then this expression cannot be an integral constant-expression.
5672 An overloaded operator can be used even if both operands are
5673 otherwise permissible in an integral constant-expression if at
5674 least one of the operands is of enumeration type. */
a723baf1 5675
b8b94c5b 5676 if (overloaded_p
0cbd7506
MS
5677 && (cp_parser_non_integral_constant_expression
5678 (parser, "calls to overloaded operators")))
5679 return error_mark_node;
b8b94c5b 5680 }
a723baf1 5681
b8b94c5b 5682 return lhs;
a723baf1
MM
5683}
5684
b8b94c5b 5685
a723baf1
MM
5686/* Parse the `? expression : assignment-expression' part of a
5687 conditional-expression. The LOGICAL_OR_EXPR is the
5688 logical-or-expression that started the conditional-expression.
5689 Returns a representation of the entire conditional-expression.
5690
39703eb9 5691 This routine is used by cp_parser_assignment_expression.
a723baf1
MM
5692
5693 ? expression : assignment-expression
21526606 5694
a723baf1 5695 GNU Extensions:
21526606 5696
a723baf1
MM
5697 ? : assignment-expression */
5698
5699static tree
94edc4ab 5700cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
a723baf1
MM
5701{
5702 tree expr;
5703 tree assignment_expr;
5704
5705 /* Consume the `?' token. */
5706 cp_lexer_consume_token (parser->lexer);
5707 if (cp_parser_allow_gnu_extensions_p (parser)
5708 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5709 /* Implicit true clause. */
5710 expr = NULL_TREE;
5711 else
5712 /* Parse the expression. */
93678513 5713 expr = cp_parser_expression (parser, /*cast_p=*/false);
21526606 5714
a723baf1
MM
5715 /* The next token should be a `:'. */
5716 cp_parser_require (parser, CPP_COLON, "`:'");
5717 /* Parse the assignment-expression. */
93678513 5718 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
a723baf1
MM
5719
5720 /* Build the conditional-expression. */
5721 return build_x_conditional_expr (logical_or_expr,
5722 expr,
5723 assignment_expr);
5724}
5725
5726/* Parse an assignment-expression.
5727
5728 assignment-expression:
5729 conditional-expression
5730 logical-or-expression assignment-operator assignment_expression
5731 throw-expression
5732
93678513
MM
5733 CAST_P is true if this expression is the target of a cast.
5734
a723baf1
MM
5735 Returns a representation for the expression. */
5736
5737static tree
93678513 5738cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
a723baf1
MM
5739{
5740 tree expr;
5741
5742 /* If the next token is the `throw' keyword, then we're looking at
5743 a throw-expression. */
5744 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5745 expr = cp_parser_throw_expression (parser);
5746 /* Otherwise, it must be that we are looking at a
5747 logical-or-expression. */
5748 else
5749 {
b8b94c5b 5750 /* Parse the binary expressions (logical-or-expression). */
93678513 5751 expr = cp_parser_binary_expression (parser, cast_p);
a723baf1
MM
5752 /* If the next token is a `?' then we're actually looking at a
5753 conditional-expression. */
5754 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5755 return cp_parser_question_colon_clause (parser, expr);
21526606 5756 else
a723baf1
MM
5757 {
5758 enum tree_code assignment_operator;
5759
5760 /* If it's an assignment-operator, we're using the second
5761 production. */
21526606 5762 assignment_operator
a723baf1
MM
5763 = cp_parser_assignment_operator_opt (parser);
5764 if (assignment_operator != ERROR_MARK)
5765 {
5766 tree rhs;
5767
5768 /* Parse the right-hand side of the assignment. */
93678513 5769 rhs = cp_parser_assignment_expression (parser, cast_p);
14d22dd6
MM
5770 /* An assignment may not appear in a
5771 constant-expression. */
625cbf93
MM
5772 if (cp_parser_non_integral_constant_expression (parser,
5773 "an assignment"))
5774 return error_mark_node;
34cd5ae7 5775 /* Build the assignment expression. */
21526606
EC
5776 expr = build_x_modify_expr (expr,
5777 assignment_operator,
a723baf1
MM
5778 rhs);
5779 }
5780 }
5781 }
5782
5783 return expr;
5784}
5785
5786/* Parse an (optional) assignment-operator.
5787
21526606
EC
5788 assignment-operator: one of
5789 = *= /= %= += -= >>= <<= &= ^= |=
a723baf1
MM
5790
5791 GNU Extension:
21526606 5792
a723baf1
MM
5793 assignment-operator: one of
5794 <?= >?=
5795
5796 If the next token is an assignment operator, the corresponding tree
5797 code is returned, and the token is consumed. For example, for
5798 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5799 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5800 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5801 operator, ERROR_MARK is returned. */
5802
5803static enum tree_code
94edc4ab 5804cp_parser_assignment_operator_opt (cp_parser* parser)
a723baf1
MM
5805{
5806 enum tree_code op;
5807 cp_token *token;
5808
5809 /* Peek at the next toen. */
5810 token = cp_lexer_peek_token (parser->lexer);
5811
5812 switch (token->type)
5813 {
5814 case CPP_EQ:
5815 op = NOP_EXPR;
5816 break;
5817
5818 case CPP_MULT_EQ:
5819 op = MULT_EXPR;
5820 break;
5821
5822 case CPP_DIV_EQ:
5823 op = TRUNC_DIV_EXPR;
5824 break;
5825
5826 case CPP_MOD_EQ:
5827 op = TRUNC_MOD_EXPR;
5828 break;
5829
5830 case CPP_PLUS_EQ:
5831 op = PLUS_EXPR;
5832 break;
5833
5834 case CPP_MINUS_EQ:
5835 op = MINUS_EXPR;
5836 break;
5837
5838 case CPP_RSHIFT_EQ:
5839 op = RSHIFT_EXPR;
5840 break;
5841
5842 case CPP_LSHIFT_EQ:
5843 op = LSHIFT_EXPR;
5844 break;
5845
5846 case CPP_AND_EQ:
5847 op = BIT_AND_EXPR;
5848 break;
5849
5850 case CPP_XOR_EQ:
5851 op = BIT_XOR_EXPR;
5852 break;
5853
5854 case CPP_OR_EQ:
5855 op = BIT_IOR_EXPR;
5856 break;
5857
21526606 5858 default:
a723baf1
MM
5859 /* Nothing else is an assignment operator. */
5860 op = ERROR_MARK;
5861 }
5862
5863 /* If it was an assignment operator, consume it. */
5864 if (op != ERROR_MARK)
5865 cp_lexer_consume_token (parser->lexer);
5866
5867 return op;
5868}
5869
5870/* Parse an expression.
5871
5872 expression:
5873 assignment-expression
5874 expression , assignment-expression
5875
93678513
MM
5876 CAST_P is true if this expression is the target of a cast.
5877
a723baf1
MM
5878 Returns a representation of the expression. */
5879
5880static tree
93678513 5881cp_parser_expression (cp_parser* parser, bool cast_p)
a723baf1
MM
5882{
5883 tree expression = NULL_TREE;
a723baf1
MM
5884
5885 while (true)
5886 {
5887 tree assignment_expression;
5888
5889 /* Parse the next assignment-expression. */
21526606 5890 assignment_expression
93678513 5891 = cp_parser_assignment_expression (parser, cast_p);
a723baf1
MM
5892 /* If this is the first assignment-expression, we can just
5893 save it away. */
5894 if (!expression)
5895 expression = assignment_expression;
a723baf1 5896 else
d17811fd
MM
5897 expression = build_x_compound_expr (expression,
5898 assignment_expression);
a723baf1
MM
5899 /* If the next token is not a comma, then we are done with the
5900 expression. */
5901 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5902 break;
5903 /* Consume the `,'. */
5904 cp_lexer_consume_token (parser->lexer);
14d22dd6 5905 /* A comma operator cannot appear in a constant-expression. */
625cbf93
MM
5906 if (cp_parser_non_integral_constant_expression (parser,
5907 "a comma operator"))
5908 expression = error_mark_node;
14d22dd6 5909 }
a723baf1
MM
5910
5911 return expression;
5912}
5913
21526606 5914/* Parse a constant-expression.
a723baf1
MM
5915
5916 constant-expression:
21526606 5917 conditional-expression
14d22dd6
MM
5918
5919 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
d17811fd
MM
5920 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5921 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5922 is false, NON_CONSTANT_P should be NULL. */
a723baf1
MM
5923
5924static tree
21526606 5925cp_parser_constant_expression (cp_parser* parser,
14d22dd6
MM
5926 bool allow_non_constant_p,
5927 bool *non_constant_p)
a723baf1 5928{
67c03833
JM
5929 bool saved_integral_constant_expression_p;
5930 bool saved_allow_non_integral_constant_expression_p;
5931 bool saved_non_integral_constant_expression_p;
a723baf1
MM
5932 tree expression;
5933
5934 /* It might seem that we could simply parse the
5935 conditional-expression, and then check to see if it were
5936 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5937 one that the compiler can figure out is constant, possibly after
5938 doing some simplifications or optimizations. The standard has a
5939 precise definition of constant-expression, and we must honor
5940 that, even though it is somewhat more restrictive.
5941
5942 For example:
5943
5944 int i[(2, 3)];
5945
5946 is not a legal declaration, because `(2, 3)' is not a
5947 constant-expression. The `,' operator is forbidden in a
5948 constant-expression. However, GCC's constant-folding machinery
5949 will fold this operation to an INTEGER_CST for `3'. */
5950
14d22dd6 5951 /* Save the old settings. */
67c03833 5952 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
21526606 5953 saved_allow_non_integral_constant_expression_p
67c03833
JM
5954 = parser->allow_non_integral_constant_expression_p;
5955 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
a723baf1 5956 /* We are now parsing a constant-expression. */
67c03833
JM
5957 parser->integral_constant_expression_p = true;
5958 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5959 parser->non_integral_constant_expression_p = false;
39703eb9
MM
5960 /* Although the grammar says "conditional-expression", we parse an
5961 "assignment-expression", which also permits "throw-expression"
5962 and the use of assignment operators. In the case that
5963 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5964 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5965 actually essential that we look for an assignment-expression.
5966 For example, cp_parser_initializer_clauses uses this function to
5967 determine whether a particular assignment-expression is in fact
5968 constant. */
93678513 5969 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14d22dd6 5970 /* Restore the old settings. */
c8094d83 5971 parser->integral_constant_expression_p
93678513 5972 = saved_integral_constant_expression_p;
21526606 5973 parser->allow_non_integral_constant_expression_p
67c03833 5974 = saved_allow_non_integral_constant_expression_p;
14d22dd6 5975 if (allow_non_constant_p)
67c03833 5976 *non_constant_p = parser->non_integral_constant_expression_p;
93678513
MM
5977 else if (parser->non_integral_constant_expression_p)
5978 expression = error_mark_node;
c8094d83 5979 parser->non_integral_constant_expression_p
93678513 5980 = saved_non_integral_constant_expression_p;
a723baf1
MM
5981
5982 return expression;
5983}
5984
7a3ea201
RH
5985/* Parse __builtin_offsetof.
5986
5987 offsetof-expression:
5988 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5989
5990 offsetof-member-designator:
5991 id-expression
5992 | offsetof-member-designator "." id-expression
3db45ab5 5993 | offsetof-member-designator "[" expression "]" */
7a3ea201
RH
5994
5995static tree
5996cp_parser_builtin_offsetof (cp_parser *parser)
5997{
5998 int save_ice_p, save_non_ice_p;
5999 tree type, expr;
6000 cp_id_kind dummy;
6001
6002 /* We're about to accept non-integral-constant things, but will
6003 definitely yield an integral constant expression. Save and
6004 restore these values around our local parsing. */
6005 save_ice_p = parser->integral_constant_expression_p;
6006 save_non_ice_p = parser->non_integral_constant_expression_p;
6007
6008 /* Consume the "__builtin_offsetof" token. */
6009 cp_lexer_consume_token (parser->lexer);
6010 /* Consume the opening `('. */
6011 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6012 /* Parse the type-id. */
6013 type = cp_parser_type_id (parser);
6014 /* Look for the `,'. */
6015 cp_parser_require (parser, CPP_COMMA, "`,'");
6016
6017 /* Build the (type *)null that begins the traditional offsetof macro. */
6018 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6019
6020 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6021 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6022 true, &dummy);
6023 while (true)
6024 {
6025 cp_token *token = cp_lexer_peek_token (parser->lexer);
6026 switch (token->type)
6027 {
6028 case CPP_OPEN_SQUARE:
6029 /* offsetof-member-designator "[" expression "]" */
6030 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6031 break;
6032
6033 case CPP_DOT:
6034 /* offsetof-member-designator "." identifier */
6035 cp_lexer_consume_token (parser->lexer);
6036 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6037 true, &dummy);
6038 break;
6039
6040 case CPP_CLOSE_PAREN:
6041 /* Consume the ")" token. */
6042 cp_lexer_consume_token (parser->lexer);
6043 goto success;
6044
6045 default:
6046 /* Error. We know the following require will fail, but
6047 that gives the proper error message. */
6048 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6049 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6050 expr = error_mark_node;
6051 goto failure;
6052 }
6053 }
6054
6055 success:
42c244d8
RH
6056 /* If we're processing a template, we can't finish the semantics yet.
6057 Otherwise we can fold the entire expression now. */
6058 if (processing_template_decl)
6059 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6060 else
c291f8b1 6061 expr = finish_offsetof (expr);
7a3ea201
RH
6062
6063 failure:
6064 parser->integral_constant_expression_p = save_ice_p;
6065 parser->non_integral_constant_expression_p = save_non_ice_p;
6066
6067 return expr;
6068}
6069
a723baf1
MM
6070/* Statements [gram.stmt.stmt] */
6071
21526606 6072/* Parse a statement.
a723baf1
MM
6073
6074 statement:
6075 labeled-statement
6076 expression-statement
6077 compound-statement
6078 selection-statement
6079 iteration-statement
6080 jump-statement
6081 declaration-statement
bc4071dd
RH
6082 try-block
6083
3db45ab5 6084 IN_COMPOUND is true when the statement is nested inside a
bc4071dd 6085 cp_parser_compound_statement; this matters for certain pragmas. */
a723baf1
MM
6086
6087static void
bc4071dd
RH
6088cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6089 bool in_compound)
a723baf1
MM
6090{
6091 tree statement;
6092 cp_token *token;
93409b8c 6093 location_t statement_location;
a723baf1 6094
bc4071dd 6095 restart:
a723baf1
MM
6096 /* There is no statement yet. */
6097 statement = NULL_TREE;
6098 /* Peek at the next token. */
6099 token = cp_lexer_peek_token (parser->lexer);
6de9cd9a 6100 /* Remember the location of the first token in the statement. */
93409b8c 6101 statement_location = token->location;
a723baf1
MM
6102 /* If this is a keyword, then that will often determine what kind of
6103 statement we have. */
6104 if (token->type == CPP_KEYWORD)
6105 {
6106 enum rid keyword = token->keyword;
6107
6108 switch (keyword)
6109 {
6110 case RID_CASE:
6111 case RID_DEFAULT:
bc4071dd
RH
6112 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6113 in_compound);
a723baf1
MM
6114 break;
6115
6116 case RID_IF:
6117 case RID_SWITCH:
6118 statement = cp_parser_selection_statement (parser);
6119 break;
6120
6121 case RID_WHILE:
6122 case RID_DO:
6123 case RID_FOR:
6124 statement = cp_parser_iteration_statement (parser);
6125 break;
6126
6127 case RID_BREAK:
6128 case RID_CONTINUE:
6129 case RID_RETURN:
6130 case RID_GOTO:
6131 statement = cp_parser_jump_statement (parser);
6132 break;
6133
e58a9aa1
ZL
6134 /* Objective-C++ exception-handling constructs. */
6135 case RID_AT_TRY:
6136 case RID_AT_CATCH:
6137 case RID_AT_FINALLY:
6138 case RID_AT_SYNCHRONIZED:
6139 case RID_AT_THROW:
6140 statement = cp_parser_objc_statement (parser);
6141 break;
6142
a723baf1
MM
6143 case RID_TRY:
6144 statement = cp_parser_try_block (parser);
6145 break;
6146
6147 default:
6148 /* It might be a keyword like `int' that can start a
6149 declaration-statement. */
6150 break;
6151 }
6152 }
6153 else if (token->type == CPP_NAME)
6154 {
6155 /* If the next token is a `:', then we are looking at a
6156 labeled-statement. */
6157 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6158 if (token->type == CPP_COLON)
bc4071dd
RH
6159 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6160 in_compound);
a723baf1
MM
6161 }
6162 /* Anything that starts with a `{' must be a compound-statement. */
6163 else if (token->type == CPP_OPEN_BRACE)
325c3691 6164 statement = cp_parser_compound_statement (parser, NULL, false);
36952dea
ZW
6165 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6166 a statement all its own. */
6167 else if (token->type == CPP_PRAGMA)
6168 {
bc4071dd
RH
6169 /* Only certain OpenMP pragmas are attached to statements, and thus
6170 are considered statements themselves. All others are not. In
6171 the context of a compound, accept the pragma as a "statement" and
3db45ab5 6172 return so that we can check for a close brace. Otherwise we
bc4071dd
RH
6173 require a real statement and must go back and read one. */
6174 if (in_compound)
6175 cp_parser_pragma (parser, pragma_compound);
6176 else if (!cp_parser_pragma (parser, pragma_stmt))
6177 goto restart;
36952dea
ZW
6178 return;
6179 }
0ef8776d
VR
6180 else if (token->type == CPP_EOF)
6181 {
6182 cp_parser_error (parser, "expected statement");
6183 return;
6184 }
a723baf1
MM
6185
6186 /* Everything else must be a declaration-statement or an
21526606 6187 expression-statement. Try for the declaration-statement
a723baf1
MM
6188 first, unless we are looking at a `;', in which case we know that
6189 we have an expression-statement. */
6190 if (!statement)
6191 {
6192 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6193 {
6194 cp_parser_parse_tentatively (parser);
6195 /* Try to parse the declaration-statement. */
6196 cp_parser_declaration_statement (parser);
6197 /* If that worked, we're done. */
6198 if (cp_parser_parse_definitely (parser))
6199 return;
6200 }
6201 /* Look for an expression-statement instead. */
325c3691 6202 statement = cp_parser_expression_statement (parser, in_statement_expr);
a723baf1
MM
6203 }
6204
6205 /* Set the line number for the statement. */
009ed910 6206 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
93409b8c 6207 SET_EXPR_LOCATION (statement, statement_location);
a723baf1
MM
6208}
6209
6210/* Parse a labeled-statement.
6211
6212 labeled-statement:
6213 identifier : statement
6214 case constant-expression : statement
98ce043b
MM
6215 default : statement
6216
6217 GNU Extension:
21526606 6218
98ce043b
MM
6219 labeled-statement:
6220 case constant-expression ... constant-expression : statement
a723baf1 6221
8c161995 6222 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
bc4071dd
RH
6223 For an ordinary label, returns a LABEL_EXPR.
6224
6225 IN_COMPOUND is as for cp_parser_statement: true when we're nested
6226 inside a compound. */
a723baf1
MM
6227
6228static tree
bc4071dd
RH
6229cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr,
6230 bool in_compound)
a723baf1
MM
6231{
6232 cp_token *token;
0e59b3fb 6233 tree statement = error_mark_node;
a723baf1
MM
6234
6235 /* The next token should be an identifier. */
6236 token = cp_lexer_peek_token (parser->lexer);
6237 if (token->type != CPP_NAME
6238 && token->type != CPP_KEYWORD)
6239 {
6240 cp_parser_error (parser, "expected labeled-statement");
6241 return error_mark_node;
6242 }
6243
6244 switch (token->keyword)
6245 {
6246 case RID_CASE:
6247 {
98ce043b
MM
6248 tree expr, expr_hi;
6249 cp_token *ellipsis;
a723baf1
MM
6250
6251 /* Consume the `case' token. */
6252 cp_lexer_consume_token (parser->lexer);
6253 /* Parse the constant-expression. */
21526606 6254 expr = cp_parser_constant_expression (parser,
d17811fd 6255 /*allow_non_constant_p=*/false,
14d22dd6 6256 NULL);
98ce043b
MM
6257
6258 ellipsis = cp_lexer_peek_token (parser->lexer);
6259 if (ellipsis->type == CPP_ELLIPSIS)
6260 {
0cbd7506 6261 /* Consume the `...' token. */
98ce043b
MM
6262 cp_lexer_consume_token (parser->lexer);
6263 expr_hi =
6264 cp_parser_constant_expression (parser,
0cbd7506 6265 /*allow_non_constant_p=*/false,
98ce043b
MM
6266 NULL);
6267 /* We don't need to emit warnings here, as the common code
6268 will do this for us. */
6269 }
6270 else
6271 expr_hi = NULL_TREE;
6272
bc4071dd 6273 if (parser->in_switch_statement_p)
98ce043b 6274 statement = finish_case_label (expr, expr_hi);
bc4071dd
RH
6275 else
6276 error ("case label %qE not within a switch statement", expr);
a723baf1
MM
6277 }
6278 break;
6279
6280 case RID_DEFAULT:
6281 /* Consume the `default' token. */
6282 cp_lexer_consume_token (parser->lexer);
bc4071dd
RH
6283
6284 if (parser->in_switch_statement_p)
0e59b3fb 6285 statement = finish_case_label (NULL_TREE, NULL_TREE);
bc4071dd
RH
6286 else
6287 error ("case label not within a switch statement");
a723baf1
MM
6288 break;
6289
6290 default:
6291 /* Anything else must be an ordinary label. */
6292 statement = finish_label_stmt (cp_parser_identifier (parser));
6293 break;
6294 }
6295
6296 /* Require the `:' token. */
6297 cp_parser_require (parser, CPP_COLON, "`:'");
6298 /* Parse the labeled statement. */
bc4071dd 6299 cp_parser_statement (parser, in_statement_expr, in_compound);
a723baf1
MM
6300
6301 /* Return the label, in the case of a `case' or `default' label. */
6302 return statement;
6303}
6304
6305/* Parse an expression-statement.
6306
6307 expression-statement:
6308 expression [opt] ;
6309
6310 Returns the new EXPR_STMT -- or NULL_TREE if the expression
a5bcc582
NS
6311 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6312 indicates whether this expression-statement is part of an
6313 expression statement. */
a723baf1
MM
6314
6315static tree
325c3691 6316cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
a723baf1 6317{
a5bcc582 6318 tree statement = NULL_TREE;
a723baf1 6319
a5bcc582 6320 /* If the next token is a ';', then there is no expression
04c06002 6321 statement. */
a723baf1 6322 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
93678513 6323 statement = cp_parser_expression (parser, /*cast_p=*/false);
21526606 6324
a723baf1 6325 /* Consume the final `;'. */
e0860732 6326 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1 6327
325c3691 6328 if (in_statement_expr
a5bcc582 6329 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
93678513
MM
6330 /* This is the final expression statement of a statement
6331 expression. */
6332 statement = finish_stmt_expr_expr (statement, in_statement_expr);
a5bcc582
NS
6333 else if (statement)
6334 statement = finish_expr_stmt (statement);
6335 else
6336 finish_stmt ();
21526606 6337
a723baf1
MM
6338 return statement;
6339}
6340
6341/* Parse a compound-statement.
6342
6343 compound-statement:
6344 { statement-seq [opt] }
21526606 6345
5882f0f3 6346 Returns a tree representing the statement. */
a723baf1
MM
6347
6348static tree
325c3691
RH
6349cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6350 bool in_try)
a723baf1
MM
6351{
6352 tree compound_stmt;
6353
6354 /* Consume the `{'. */
6355 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6356 return error_mark_node;
6357 /* Begin the compound-statement. */
325c3691 6358 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
a723baf1 6359 /* Parse an (optional) statement-seq. */
325c3691 6360 cp_parser_statement_seq_opt (parser, in_statement_expr);
a723baf1 6361 /* Finish the compound-statement. */
7a3397c7 6362 finish_compound_stmt (compound_stmt);
a723baf1
MM
6363 /* Consume the `}'. */
6364 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6365
6366 return compound_stmt;
6367}
6368
6369/* Parse an (optional) statement-seq.
6370
6371 statement-seq:
6372 statement
6373 statement-seq [opt] statement */
6374
6375static void
325c3691 6376cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
a723baf1
MM
6377{
6378 /* Scan statements until there aren't any more. */
6379 while (true)
6380 {
bc4071dd
RH
6381 cp_token *token = cp_lexer_peek_token (parser->lexer);
6382
a723baf1 6383 /* If we're looking at a `}', then we've run out of statements. */
bc4071dd
RH
6384 if (token->type == CPP_CLOSE_BRACE
6385 || token->type == CPP_EOF
6386 || token->type == CPP_PRAGMA_EOL)
a723baf1
MM
6387 break;
6388
6389 /* Parse the statement. */
bc4071dd 6390 cp_parser_statement (parser, in_statement_expr, true);
a723baf1
MM
6391 }
6392}
6393
6394/* Parse a selection-statement.
6395
6396 selection-statement:
6397 if ( condition ) statement
6398 if ( condition ) statement else statement
21526606 6399 switch ( condition ) statement
a723baf1
MM
6400
6401 Returns the new IF_STMT or SWITCH_STMT. */
6402
6403static tree
94edc4ab 6404cp_parser_selection_statement (cp_parser* parser)
a723baf1
MM
6405{
6406 cp_token *token;
6407 enum rid keyword;
6408
6409 /* Peek at the next token. */
6410 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6411
6412 /* See what kind of keyword it is. */
6413 keyword = token->keyword;
6414 switch (keyword)
6415 {
6416 case RID_IF:
6417 case RID_SWITCH:
6418 {
6419 tree statement;
6420 tree condition;
6421
6422 /* Look for the `('. */
6423 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6424 {
6425 cp_parser_skip_to_end_of_statement (parser);
6426 return error_mark_node;
6427 }
6428
6429 /* Begin the selection-statement. */
6430 if (keyword == RID_IF)
6431 statement = begin_if_stmt ();
6432 else
6433 statement = begin_switch_stmt ();
6434
6435 /* Parse the condition. */
6436 condition = cp_parser_condition (parser);
6437 /* Look for the `)'. */
6438 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
6439 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6440 /*consume_paren=*/true);
a723baf1
MM
6441
6442 if (keyword == RID_IF)
6443 {
a723baf1
MM
6444 /* Add the condition. */
6445 finish_if_stmt_cond (condition, statement);
6446
6447 /* Parse the then-clause. */
325c3691 6448 cp_parser_implicitly_scoped_statement (parser);
a723baf1
MM
6449 finish_then_clause (statement);
6450
6451 /* If the next token is `else', parse the else-clause. */
6452 if (cp_lexer_next_token_is_keyword (parser->lexer,
6453 RID_ELSE))
6454 {
a723baf1
MM
6455 /* Consume the `else' keyword. */
6456 cp_lexer_consume_token (parser->lexer);
325c3691 6457 begin_else_clause (statement);
a723baf1 6458 /* Parse the else-clause. */
325c3691 6459 cp_parser_implicitly_scoped_statement (parser);
a723baf1
MM
6460 finish_else_clause (statement);
6461 }
6462
6463 /* Now we're all done with the if-statement. */
325c3691 6464 finish_if_stmt (statement);
a723baf1
MM
6465 }
6466 else
6467 {
0e59b3fb 6468 bool in_switch_statement_p;
1799e5d5 6469 unsigned char in_statement;
a723baf1
MM
6470
6471 /* Add the condition. */
6472 finish_switch_cond (condition, statement);
6473
6474 /* Parse the body of the switch-statement. */
0e59b3fb 6475 in_switch_statement_p = parser->in_switch_statement_p;
1799e5d5 6476 in_statement = parser->in_statement;
0e59b3fb 6477 parser->in_switch_statement_p = true;
1799e5d5 6478 parser->in_statement |= IN_SWITCH_STMT;
325c3691 6479 cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 6480 parser->in_switch_statement_p = in_switch_statement_p;
1799e5d5 6481 parser->in_statement = in_statement;
a723baf1
MM
6482
6483 /* Now we're all done with the switch-statement. */
6484 finish_switch_stmt (statement);
6485 }
6486
6487 return statement;
6488 }
6489 break;
6490
6491 default:
6492 cp_parser_error (parser, "expected selection-statement");
6493 return error_mark_node;
6494 }
6495}
6496
21526606 6497/* Parse a condition.
a723baf1
MM
6498
6499 condition:
6500 expression
21526606 6501 type-specifier-seq declarator = assignment-expression
a723baf1
MM
6502
6503 GNU Extension:
21526606 6504
a723baf1 6505 condition:
21526606 6506 type-specifier-seq declarator asm-specification [opt]
a723baf1 6507 attributes [opt] = assignment-expression
21526606 6508
a723baf1
MM
6509 Returns the expression that should be tested. */
6510
6511static tree
94edc4ab 6512cp_parser_condition (cp_parser* parser)
a723baf1 6513{
62d1db17 6514 cp_decl_specifier_seq type_specifiers;
a723baf1
MM
6515 const char *saved_message;
6516
6517 /* Try the declaration first. */
6518 cp_parser_parse_tentatively (parser);
6519 /* New types are not allowed in the type-specifier-seq for a
6520 condition. */
6521 saved_message = parser->type_definition_forbidden_message;
6522 parser->type_definition_forbidden_message
6523 = "types may not be defined in conditions";
6524 /* Parse the type-specifier-seq. */
d4113656
MM
6525 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6526 &type_specifiers);
a723baf1
MM
6527 /* Restore the saved message. */
6528 parser->type_definition_forbidden_message = saved_message;
6529 /* If all is well, we might be looking at a declaration. */
6530 if (!cp_parser_error_occurred (parser))
6531 {
6532 tree decl;
6533 tree asm_specification;
6534 tree attributes;
058b15c1 6535 cp_declarator *declarator;
a723baf1 6536 tree initializer = NULL_TREE;
21526606 6537
a723baf1 6538 /* Parse the declarator. */
62b8a44e 6539 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 6540 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
6541 /*parenthesized_p=*/NULL,
6542 /*member_p=*/false);
a723baf1
MM
6543 /* Parse the attributes. */
6544 attributes = cp_parser_attributes_opt (parser);
6545 /* Parse the asm-specification. */
6546 asm_specification = cp_parser_asm_specification_opt (parser);
6547 /* If the next token is not an `=', then we might still be
6548 looking at an expression. For example:
21526606 6549
a723baf1 6550 if (A(a).x)
21526606 6551
a723baf1
MM
6552 looks like a decl-specifier-seq and a declarator -- but then
6553 there is no `=', so this is an expression. */
6554 cp_parser_require (parser, CPP_EQ, "`='");
6555 /* If we did see an `=', then we are looking at a declaration
6556 for sure. */
6557 if (cp_parser_parse_definitely (parser))
6558 {
c8094d83 6559 tree pushed_scope;
d174af6c 6560 bool non_constant_p;
73a8adb6 6561
a723baf1 6562 /* Create the declaration. */
62d1db17 6563 decl = start_decl (declarator, &type_specifiers,
a723baf1 6564 /*initialized_p=*/true,
73a8adb6 6565 attributes, /*prefix_attributes=*/NULL_TREE,
4514aa8c 6566 &pushed_scope);
a723baf1 6567 /* Parse the assignment-expression. */
3db45ab5 6568 initializer
d174af6c
MM
6569 = cp_parser_constant_expression (parser,
6570 /*allow_non_constant_p=*/true,
6571 &non_constant_p);
6572 if (!non_constant_p)
6573 initializer = fold_non_dependent_expr (initializer);
21526606 6574
a723baf1 6575 /* Process the initializer. */
21526606 6576 cp_finish_decl (decl,
3db45ab5 6577 initializer, !non_constant_p,
21526606 6578 asm_specification,
a723baf1 6579 LOOKUP_ONLYCONVERTING);
c162c75e 6580
4514aa8c
NS
6581 if (pushed_scope)
6582 pop_scope (pushed_scope);
21526606 6583
a723baf1
MM
6584 return convert_from_reference (decl);
6585 }
6586 }
6587 /* If we didn't even get past the declarator successfully, we are
6588 definitely not looking at a declaration. */
6589 else
6590 cp_parser_abort_tentative_parse (parser);
6591
6592 /* Otherwise, we are looking at an expression. */
93678513 6593 return cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
6594}
6595
6596/* Parse an iteration-statement.
6597
6598 iteration-statement:
6599 while ( condition ) statement
6600 do statement while ( expression ) ;
6601 for ( for-init-statement condition [opt] ; expression [opt] )
6602 statement
6603
6604 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6605
6606static tree
94edc4ab 6607cp_parser_iteration_statement (cp_parser* parser)
a723baf1
MM
6608{
6609 cp_token *token;
6610 enum rid keyword;
6611 tree statement;
1799e5d5 6612 unsigned char in_statement;
a723baf1
MM
6613
6614 /* Peek at the next token. */
6615 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6616 if (!token)
6617 return error_mark_node;
6618
0e59b3fb 6619 /* Remember whether or not we are already within an iteration
21526606 6620 statement. */
1799e5d5 6621 in_statement = parser->in_statement;
0e59b3fb 6622
a723baf1
MM
6623 /* See what kind of keyword it is. */
6624 keyword = token->keyword;
6625 switch (keyword)
6626 {
6627 case RID_WHILE:
6628 {
6629 tree condition;
6630
6631 /* Begin the while-statement. */
6632 statement = begin_while_stmt ();
6633 /* Look for the `('. */
6634 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6635 /* Parse the condition. */
6636 condition = cp_parser_condition (parser);
6637 finish_while_stmt_cond (condition, statement);
6638 /* Look for the `)'. */
6639 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6640 /* Parse the dependent statement. */
1799e5d5 6641 parser->in_statement = IN_ITERATION_STMT;
a723baf1 6642 cp_parser_already_scoped_statement (parser);
1799e5d5 6643 parser->in_statement = in_statement;
a723baf1
MM
6644 /* We're done with the while-statement. */
6645 finish_while_stmt (statement);
6646 }
6647 break;
6648
6649 case RID_DO:
6650 {
6651 tree expression;
6652
6653 /* Begin the do-statement. */
6654 statement = begin_do_stmt ();
6655 /* Parse the body of the do-statement. */
1799e5d5 6656 parser->in_statement = IN_ITERATION_STMT;
a723baf1 6657 cp_parser_implicitly_scoped_statement (parser);
1799e5d5 6658 parser->in_statement = in_statement;
a723baf1
MM
6659 finish_do_body (statement);
6660 /* Look for the `while' keyword. */
6661 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6662 /* Look for the `('. */
6663 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6664 /* Parse the expression. */
93678513 6665 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
6666 /* We're done with the do-statement. */
6667 finish_do_stmt (expression, statement);
6668 /* Look for the `)'. */
6669 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6670 /* Look for the `;'. */
6671 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6672 }
6673 break;
6674
6675 case RID_FOR:
6676 {
6677 tree condition = NULL_TREE;
6678 tree expression = NULL_TREE;
6679
6680 /* Begin the for-statement. */
6681 statement = begin_for_stmt ();
6682 /* Look for the `('. */
6683 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6684 /* Parse the initialization. */
6685 cp_parser_for_init_statement (parser);
6686 finish_for_init_stmt (statement);
6687
6688 /* If there's a condition, process it. */
6689 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6690 condition = cp_parser_condition (parser);
6691 finish_for_cond (condition, statement);
6692 /* Look for the `;'. */
6693 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6694
6695 /* If there's an expression, process it. */
6696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
93678513 6697 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
6698 finish_for_expr (expression, statement);
6699 /* Look for the `)'. */
d5a10cf0 6700 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
98ca843c 6701
a723baf1 6702 /* Parse the body of the for-statement. */
1799e5d5 6703 parser->in_statement = IN_ITERATION_STMT;
a723baf1 6704 cp_parser_already_scoped_statement (parser);
1799e5d5 6705 parser->in_statement = in_statement;
a723baf1
MM
6706
6707 /* We're done with the for-statement. */
6708 finish_for_stmt (statement);
6709 }
6710 break;
6711
6712 default:
6713 cp_parser_error (parser, "expected iteration-statement");
6714 statement = error_mark_node;
6715 break;
6716 }
6717
6718 return statement;
6719}
6720
6721/* Parse a for-init-statement.
6722
6723 for-init-statement:
6724 expression-statement
6725 simple-declaration */
6726
6727static void
94edc4ab 6728cp_parser_for_init_statement (cp_parser* parser)
a723baf1
MM
6729{
6730 /* If the next token is a `;', then we have an empty
34cd5ae7 6731 expression-statement. Grammatically, this is also a
a723baf1
MM
6732 simple-declaration, but an invalid one, because it does not
6733 declare anything. Therefore, if we did not handle this case
6734 specially, we would issue an error message about an invalid
6735 declaration. */
6736 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6737 {
6738 /* We're going to speculatively look for a declaration, falling back
6739 to an expression, if necessary. */
6740 cp_parser_parse_tentatively (parser);
6741 /* Parse the declaration. */
6742 cp_parser_simple_declaration (parser,
6743 /*function_definition_allowed_p=*/false);
6744 /* If the tentative parse failed, then we shall need to look for an
6745 expression-statement. */
6746 if (cp_parser_parse_definitely (parser))
6747 return;
6748 }
6749
a5bcc582 6750 cp_parser_expression_statement (parser, false);
a723baf1
MM
6751}
6752
6753/* Parse a jump-statement.
6754
6755 jump-statement:
6756 break ;
6757 continue ;
6758 return expression [opt] ;
21526606 6759 goto identifier ;
a723baf1
MM
6760
6761 GNU extension:
6762
6763 jump-statement:
6764 goto * expression ;
6765
5088b058 6766 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
a723baf1
MM
6767
6768static tree
94edc4ab 6769cp_parser_jump_statement (cp_parser* parser)
a723baf1
MM
6770{
6771 tree statement = error_mark_node;
6772 cp_token *token;
6773 enum rid keyword;
6774
6775 /* Peek at the next token. */
6776 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6777 if (!token)
6778 return error_mark_node;
6779
6780 /* See what kind of keyword it is. */
6781 keyword = token->keyword;
6782 switch (keyword)
6783 {
6784 case RID_BREAK:
1799e5d5 6785 switch (parser->in_statement)
0e59b3fb 6786 {
1799e5d5 6787 case 0:
0e59b3fb 6788 error ("break statement not within loop or switch");
1799e5d5
RH
6789 break;
6790 default:
6791 gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6792 || parser->in_statement == IN_ITERATION_STMT);
6793 statement = finish_break_stmt ();
6794 break;
6795 case IN_OMP_BLOCK:
6796 error ("invalid exit from OpenMP structured block");
6797 break;
6798 case IN_OMP_FOR:
6799 error ("break statement used with OpenMP for loop");
6800 break;
0e59b3fb 6801 }
2a13a625 6802 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6803 break;
6804
6805 case RID_CONTINUE:
1799e5d5 6806 switch (parser->in_statement & ~IN_SWITCH_STMT)
0e59b3fb 6807 {
1799e5d5 6808 case 0:
0e59b3fb 6809 error ("continue statement not within a loop");
1799e5d5
RH
6810 break;
6811 case IN_ITERATION_STMT:
6812 case IN_OMP_FOR:
6813 statement = finish_continue_stmt ();
6814 break;
6815 case IN_OMP_BLOCK:
6816 error ("invalid exit from OpenMP structured block");
6817 break;
6818 default:
6819 gcc_unreachable ();
0e59b3fb 6820 }
2a13a625 6821 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6822 break;
6823
6824 case RID_RETURN:
6825 {
6826 tree expr;
6827
21526606 6828 /* If the next token is a `;', then there is no
a723baf1
MM
6829 expression. */
6830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
93678513 6831 expr = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
6832 else
6833 expr = NULL_TREE;
6834 /* Build the return-statement. */
6835 statement = finish_return_stmt (expr);
6836 /* Look for the final `;'. */
2a13a625 6837 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6838 }
6839 break;
6840
6841 case RID_GOTO:
6842 /* Create the goto-statement. */
6843 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6844 {
6845 /* Issue a warning about this use of a GNU extension. */
6846 if (pedantic)
6847 pedwarn ("ISO C++ forbids computed gotos");
6848 /* Consume the '*' token. */
6849 cp_lexer_consume_token (parser->lexer);
6850 /* Parse the dependent expression. */
93678513 6851 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
a723baf1
MM
6852 }
6853 else
6854 finish_goto_stmt (cp_parser_identifier (parser));
6855 /* Look for the final `;'. */
2a13a625 6856 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6857 break;
6858
6859 default:
6860 cp_parser_error (parser, "expected jump-statement");
6861 break;
6862 }
6863
6864 return statement;
6865}
6866
6867/* Parse a declaration-statement.
6868
6869 declaration-statement:
6870 block-declaration */
6871
6872static void
94edc4ab 6873cp_parser_declaration_statement (cp_parser* parser)
a723baf1 6874{
058b15c1
MM
6875 void *p;
6876
6877 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6878 p = obstack_alloc (&declarator_obstack, 0);
6879
6880 /* Parse the block-declaration. */
a723baf1
MM
6881 cp_parser_block_declaration (parser, /*statement_p=*/true);
6882
058b15c1
MM
6883 /* Free any declarators allocated. */
6884 obstack_free (&declarator_obstack, p);
6885
a723baf1
MM
6886 /* Finish off the statement. */
6887 finish_stmt ();
6888}
6889
6890/* Some dependent statements (like `if (cond) statement'), are
6891 implicitly in their own scope. In other words, if the statement is
6892 a single statement (as opposed to a compound-statement), it is
6893 none-the-less treated as if it were enclosed in braces. Any
6894 declarations appearing in the dependent statement are out of scope
6895 after control passes that point. This function parses a statement,
6896 but ensures that is in its own scope, even if it is not a
21526606 6897 compound-statement.
a723baf1
MM
6898
6899 Returns the new statement. */
6900
6901static tree
94edc4ab 6902cp_parser_implicitly_scoped_statement (cp_parser* parser)
a723baf1
MM
6903{
6904 tree statement;
6905
74ac79fa
DM
6906 /* Mark if () ; with a special NOP_EXPR. */
6907 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6908 {
6909 cp_lexer_consume_token (parser->lexer);
6910 statement = add_stmt (build_empty_stmt ());
6911 }
6912 /* if a compound is opened, we simply parse the statement directly. */
6913 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6914 statement = cp_parser_compound_statement (parser, NULL, false);
a723baf1 6915 /* If the token is not a `{', then we must take special action. */
74ac79fa 6916 else
a723baf1
MM
6917 {
6918 /* Create a compound-statement. */
325c3691 6919 statement = begin_compound_stmt (0);
a723baf1 6920 /* Parse the dependent-statement. */
bc4071dd 6921 cp_parser_statement (parser, NULL_TREE, false);
a723baf1 6922 /* Finish the dummy compound-statement. */
7a3397c7 6923 finish_compound_stmt (statement);
a723baf1 6924 }
a723baf1
MM
6925
6926 /* Return the statement. */
6927 return statement;
6928}
6929
6930/* For some dependent statements (like `while (cond) statement'), we
6931 have already created a scope. Therefore, even if the dependent
6932 statement is a compound-statement, we do not want to create another
6933 scope. */
6934
6935static void
94edc4ab 6936cp_parser_already_scoped_statement (cp_parser* parser)
a723baf1 6937{
325c3691
RH
6938 /* If the token is a `{', then we must take special action. */
6939 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
bc4071dd 6940 cp_parser_statement (parser, NULL_TREE, false);
325c3691 6941 else
a723baf1 6942 {
325c3691
RH
6943 /* Avoid calling cp_parser_compound_statement, so that we
6944 don't create a new scope. Do everything else by hand. */
6945 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
bc4071dd 6946 cp_parser_statement_seq_opt (parser, NULL_TREE);
325c3691 6947 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1 6948 }
a723baf1
MM
6949}
6950
6951/* Declarations [gram.dcl.dcl] */
6952
6953/* Parse an optional declaration-sequence.
6954
6955 declaration-seq:
6956 declaration
6957 declaration-seq declaration */
6958
6959static void
94edc4ab 6960cp_parser_declaration_seq_opt (cp_parser* parser)
a723baf1
MM
6961{
6962 while (true)
6963 {
6964 cp_token *token;
6965
6966 token = cp_lexer_peek_token (parser->lexer);
6967
6968 if (token->type == CPP_CLOSE_BRACE
bc4071dd
RH
6969 || token->type == CPP_EOF
6970 || token->type == CPP_PRAGMA_EOL)
a723baf1
MM
6971 break;
6972
21526606 6973 if (token->type == CPP_SEMICOLON)
a723baf1
MM
6974 {
6975 /* A declaration consisting of a single semicolon is
6976 invalid. Allow it unless we're being pedantic. */
a723baf1 6977 cp_lexer_consume_token (parser->lexer);
2cfe82fe
ZW
6978 if (pedantic && !in_system_header)
6979 pedwarn ("extra %<;%>");
a723baf1
MM
6980 continue;
6981 }
6982
7d381002 6983 /* If we're entering or exiting a region that's implicitly
03fd3f84 6984 extern "C", modify the lang context appropriately. */
7d381002
MA
6985 if (!parser->implicit_extern_c && token->implicit_extern_c)
6986 {
6987 push_lang_context (lang_name_c);
6988 parser->implicit_extern_c = true;
6989 }
6990 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6991 {
6992 pop_lang_context ();
6993 parser->implicit_extern_c = false;
6994 }
6995
36952dea
ZW
6996 if (token->type == CPP_PRAGMA)
6997 {
6998 /* A top-level declaration can consist solely of a #pragma.
6999 A nested declaration cannot, so this is done here and not
7000 in cp_parser_declaration. (A #pragma at block scope is
7001 handled in cp_parser_statement.) */
bc4071dd 7002 cp_parser_pragma (parser, pragma_external);
36952dea
ZW
7003 continue;
7004 }
7005
c838d82f 7006 /* Parse the declaration itself. */
a723baf1
MM
7007 cp_parser_declaration (parser);
7008 }
7009}
7010
7011/* Parse a declaration.
7012
7013 declaration:
7014 block-declaration
7015 function-definition
7016 template-declaration
7017 explicit-instantiation
7018 explicit-specialization
7019 linkage-specification
21526606 7020 namespace-definition
1092805d
MM
7021
7022 GNU extension:
7023
7024 declaration:
7025 __extension__ declaration */
a723baf1
MM
7026
7027static void
94edc4ab 7028cp_parser_declaration (cp_parser* parser)
a723baf1
MM
7029{
7030 cp_token token1;
7031 cp_token token2;
1092805d 7032 int saved_pedantic;
058b15c1 7033 void *p;
1092805d
MM
7034
7035 /* Check for the `__extension__' keyword. */
7036 if (cp_parser_extension_opt (parser, &saved_pedantic))
7037 {
7038 /* Parse the qualified declaration. */
7039 cp_parser_declaration (parser);
7040 /* Restore the PEDANTIC flag. */
7041 pedantic = saved_pedantic;
7042
7043 return;
7044 }
a723baf1
MM
7045
7046 /* Try to figure out what kind of declaration is present. */
7047 token1 = *cp_lexer_peek_token (parser->lexer);
21526606 7048
a723baf1
MM
7049 if (token1.type != CPP_EOF)
7050 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
32cafd73 7051 else
728cdd08
GDR
7052 {
7053 token2.type = CPP_EOF;
7054 token2.keyword = RID_MAX;
7055 }
a723baf1 7056
058b15c1
MM
7057 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7058 p = obstack_alloc (&declarator_obstack, 0);
7059
a723baf1
MM
7060 /* If the next token is `extern' and the following token is a string
7061 literal, then we have a linkage specification. */
7062 if (token1.keyword == RID_EXTERN
7063 && cp_parser_is_string_literal (&token2))
7064 cp_parser_linkage_specification (parser);
7065 /* If the next token is `template', then we have either a template
7066 declaration, an explicit instantiation, or an explicit
7067 specialization. */
7068 else if (token1.keyword == RID_TEMPLATE)
7069 {
7070 /* `template <>' indicates a template specialization. */
7071 if (token2.type == CPP_LESS
7072 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7073 cp_parser_explicit_specialization (parser);
7074 /* `template <' indicates a template declaration. */
7075 else if (token2.type == CPP_LESS)
7076 cp_parser_template_declaration (parser, /*member_p=*/false);
7077 /* Anything else must be an explicit instantiation. */
7078 else
7079 cp_parser_explicit_instantiation (parser);
7080 }
7081 /* If the next token is `export', then we have a template
7082 declaration. */
7083 else if (token1.keyword == RID_EXPORT)
7084 cp_parser_template_declaration (parser, /*member_p=*/false);
7085 /* If the next token is `extern', 'static' or 'inline' and the one
7086 after that is `template', we have a GNU extended explicit
7087 instantiation directive. */
7088 else if (cp_parser_allow_gnu_extensions_p (parser)
7089 && (token1.keyword == RID_EXTERN
7090 || token1.keyword == RID_STATIC
7091 || token1.keyword == RID_INLINE)
7092 && token2.keyword == RID_TEMPLATE)
7093 cp_parser_explicit_instantiation (parser);
7094 /* If the next token is `namespace', check for a named or unnamed
7095 namespace definition. */
7096 else if (token1.keyword == RID_NAMESPACE
7097 && (/* A named namespace definition. */
7098 (token2.type == CPP_NAME
21526606 7099 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
0ed5edac 7100 != CPP_EQ))
a723baf1 7101 /* An unnamed namespace definition. */
aa09f986
JM
7102 || token2.type == CPP_OPEN_BRACE
7103 || token2.keyword == RID_ATTRIBUTE))
a723baf1 7104 cp_parser_namespace_definition (parser);
e58a9aa1
ZL
7105 /* Objective-C++ declaration/definition. */
7106 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7107 cp_parser_objc_declaration (parser);
a723baf1
MM
7108 /* We must have either a block declaration or a function
7109 definition. */
7110 else
7111 /* Try to parse a block-declaration, or a function-definition. */
7112 cp_parser_block_declaration (parser, /*statement_p=*/false);
058b15c1
MM
7113
7114 /* Free any declarators allocated. */
7115 obstack_free (&declarator_obstack, p);
a723baf1
MM
7116}
7117
21526606 7118/* Parse a block-declaration.
a723baf1
MM
7119
7120 block-declaration:
7121 simple-declaration
7122 asm-definition
7123 namespace-alias-definition
7124 using-declaration
21526606 7125 using-directive
a723baf1
MM
7126
7127 GNU Extension:
7128
7129 block-declaration:
21526606 7130 __extension__ block-declaration
a723baf1
MM
7131 label-declaration
7132
34cd5ae7 7133 If STATEMENT_P is TRUE, then this block-declaration is occurring as
a723baf1
MM
7134 part of a declaration-statement. */
7135
7136static void
21526606 7137cp_parser_block_declaration (cp_parser *parser,
a723baf1
MM
7138 bool statement_p)
7139{
7140 cp_token *token1;
7141 int saved_pedantic;
7142
7143 /* Check for the `__extension__' keyword. */
7144 if (cp_parser_extension_opt (parser, &saved_pedantic))
7145 {
7146 /* Parse the qualified declaration. */
7147 cp_parser_block_declaration (parser, statement_p);
7148 /* Restore the PEDANTIC flag. */
7149 pedantic = saved_pedantic;
7150
7151 return;
7152 }
7153
7154 /* Peek at the next token to figure out which kind of declaration is
7155 present. */
7156 token1 = cp_lexer_peek_token (parser->lexer);
7157
7158 /* If the next keyword is `asm', we have an asm-definition. */
7159 if (token1->keyword == RID_ASM)
7160 {
7161 if (statement_p)
7162 cp_parser_commit_to_tentative_parse (parser);
7163 cp_parser_asm_definition (parser);
7164 }
7165 /* If the next keyword is `namespace', we have a
7166 namespace-alias-definition. */
7167 else if (token1->keyword == RID_NAMESPACE)
7168 cp_parser_namespace_alias_definition (parser);
7169 /* If the next keyword is `using', we have either a
7170 using-declaration or a using-directive. */
7171 else if (token1->keyword == RID_USING)
7172 {
7173 cp_token *token2;
7174
7175 if (statement_p)
7176 cp_parser_commit_to_tentative_parse (parser);
7177 /* If the token after `using' is `namespace', then we have a
7178 using-directive. */
7179 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7180 if (token2->keyword == RID_NAMESPACE)
7181 cp_parser_using_directive (parser);
7182 /* Otherwise, it's a using-declaration. */
7183 else
7184 cp_parser_using_declaration (parser);
7185 }
7186 /* If the next keyword is `__label__' we have a label declaration. */
7187 else if (token1->keyword == RID_LABEL)
7188 {
7189 if (statement_p)
7190 cp_parser_commit_to_tentative_parse (parser);
7191 cp_parser_label_declaration (parser);
7192 }
7193 /* Anything else must be a simple-declaration. */
7194 else
7195 cp_parser_simple_declaration (parser, !statement_p);
7196}
7197
7198/* Parse a simple-declaration.
7199
7200 simple-declaration:
21526606 7201 decl-specifier-seq [opt] init-declarator-list [opt] ;
a723baf1
MM
7202
7203 init-declarator-list:
7204 init-declarator
21526606 7205 init-declarator-list , init-declarator
a723baf1 7206
34cd5ae7 7207 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9bcb9aae 7208 function-definition as a simple-declaration. */
a723baf1
MM
7209
7210static void
21526606 7211cp_parser_simple_declaration (cp_parser* parser,
0cbd7506 7212 bool function_definition_allowed_p)
a723baf1 7213{
62d1db17 7214 cp_decl_specifier_seq decl_specifiers;
560ad596 7215 int declares_class_or_enum;
a723baf1
MM
7216 bool saw_declarator;
7217
7218 /* Defer access checks until we know what is being declared; the
7219 checks for names appearing in the decl-specifier-seq should be
7220 done as if we were in the scope of the thing being declared. */
8d241e0b 7221 push_deferring_access_checks (dk_deferred);
cf22909c 7222
a723baf1
MM
7223 /* Parse the decl-specifier-seq. We have to keep track of whether
7224 or not the decl-specifier-seq declares a named class or
7225 enumeration type, since that is the only case in which the
21526606 7226 init-declarator-list is allowed to be empty.
a723baf1
MM
7227
7228 [dcl.dcl]
7229
7230 In a simple-declaration, the optional init-declarator-list can be
7231 omitted only when declaring a class or enumeration, that is when
7232 the decl-specifier-seq contains either a class-specifier, an
7233 elaborated-type-specifier, or an enum-specifier. */
62d1db17
MM
7234 cp_parser_decl_specifier_seq (parser,
7235 CP_PARSER_FLAGS_OPTIONAL,
7236 &decl_specifiers,
7237 &declares_class_or_enum);
a723baf1 7238 /* We no longer need to defer access checks. */
cf22909c 7239 stop_deferring_access_checks ();
24c0ef37 7240
39703eb9
MM
7241 /* In a block scope, a valid declaration must always have a
7242 decl-specifier-seq. By not trying to parse declarators, we can
7243 resolve the declaration/expression ambiguity more quickly. */
98ca843c 7244 if (!function_definition_allowed_p
62d1db17 7245 && !decl_specifiers.any_specifiers_p)
39703eb9
MM
7246 {
7247 cp_parser_error (parser, "expected declaration");
7248 goto done;
7249 }
7250
8fbc5ae7
MM
7251 /* If the next two tokens are both identifiers, the code is
7252 erroneous. The usual cause of this situation is code like:
7253
7254 T t;
7255
7256 where "T" should name a type -- but does not. */
de3fe73c
MM
7257 if (!decl_specifiers.type
7258 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8fbc5ae7 7259 {
8d241e0b 7260 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
7261 looking at a declaration. */
7262 cp_parser_commit_to_tentative_parse (parser);
7263 /* Give up. */
39703eb9 7264 goto done;
8fbc5ae7 7265 }
c8094d83 7266
996c2b52
MM
7267 /* If we have seen at least one decl-specifier, and the next token
7268 is not a parenthesis, then we must be looking at a declaration.
7269 (After "int (" we might be looking at a functional cast.) */
c8094d83 7270 if (decl_specifiers.any_specifiers_p
996c2b52
MM
7271 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7272 cp_parser_commit_to_tentative_parse (parser);
8fbc5ae7 7273
a723baf1
MM
7274 /* Keep going until we hit the `;' at the end of the simple
7275 declaration. */
7276 saw_declarator = false;
21526606 7277 while (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
7278 CPP_SEMICOLON))
7279 {
7280 cp_token *token;
7281 bool function_definition_p;
560ad596 7282 tree decl;
a723baf1 7283
6d328225
PM
7284 if (saw_declarator)
7285 {
7286 /* If we are processing next declarator, coma is expected */
7287 token = cp_lexer_peek_token (parser->lexer);
7288 gcc_assert (token->type == CPP_COMMA);
7289 cp_lexer_consume_token (parser->lexer);
7290 }
7291 else
7292 saw_declarator = true;
7293
a723baf1 7294 /* Parse the init-declarator. */
62d1db17 7295 decl = cp_parser_init_declarator (parser, &decl_specifiers,
6b648482 7296 /*checks=*/NULL_TREE,
560ad596
MM
7297 function_definition_allowed_p,
7298 /*member_p=*/false,
7299 declares_class_or_enum,
7300 &function_definition_p);
1fb3244a
MM
7301 /* If an error occurred while parsing tentatively, exit quickly.
7302 (That usually happens when in the body of a function; each
7303 statement is treated as a declaration-statement until proven
7304 otherwise.) */
7305 if (cp_parser_error_occurred (parser))
39703eb9 7306 goto done;
a723baf1
MM
7307 /* Handle function definitions specially. */
7308 if (function_definition_p)
7309 {
7310 /* If the next token is a `,', then we are probably
7311 processing something like:
7312
7313 void f() {}, *p;
7314
7315 which is erroneous. */
7316 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7317 error ("mixing declarations and function-definitions is forbidden");
7318 /* Otherwise, we're done with the list of declarators. */
7319 else
24c0ef37 7320 {
cf22909c 7321 pop_deferring_access_checks ();
24c0ef37
GS
7322 return;
7323 }
a723baf1
MM
7324 }
7325 /* The next token should be either a `,' or a `;'. */
7326 token = cp_lexer_peek_token (parser->lexer);
7327 /* If it's a `,', there are more declarators to come. */
7328 if (token->type == CPP_COMMA)
6d328225 7329 /* will be consumed next time around */;
a723baf1
MM
7330 /* If it's a `;', we are done. */
7331 else if (token->type == CPP_SEMICOLON)
7332 break;
7333 /* Anything else is an error. */
7334 else
7335 {
996c2b52
MM
7336 /* If we have already issued an error message we don't need
7337 to issue another one. */
7338 if (decl != error_mark_node
0b16f8f4 7339 || cp_parser_uncommitted_to_tentative_parse_p (parser))
2a13a625 7340 cp_parser_error (parser, "expected %<,%> or %<;%>");
a723baf1
MM
7341 /* Skip tokens until we reach the end of the statement. */
7342 cp_parser_skip_to_end_of_statement (parser);
5a98fa7b
MM
7343 /* If the next token is now a `;', consume it. */
7344 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7345 cp_lexer_consume_token (parser->lexer);
39703eb9 7346 goto done;
a723baf1
MM
7347 }
7348 /* After the first time around, a function-definition is not
7349 allowed -- even if it was OK at first. For example:
7350
0cbd7506 7351 int i, f() {}
a723baf1 7352
0cbd7506 7353 is not valid. */
a723baf1
MM
7354 function_definition_allowed_p = false;
7355 }
7356
7357 /* Issue an error message if no declarators are present, and the
7358 decl-specifier-seq does not itself declare a class or
7359 enumeration. */
7360 if (!saw_declarator)
7361 {
7362 if (cp_parser_declares_only_class_p (parser))
62d1db17 7363 shadow_tag (&decl_specifiers);
a723baf1 7364 /* Perform any deferred access checks. */
cf22909c 7365 perform_deferred_access_checks ();
a723baf1
MM
7366 }
7367
7368 /* Consume the `;'. */
7369 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7370
39703eb9
MM
7371 done:
7372 pop_deferring_access_checks ();
a723baf1
MM
7373}
7374
7375/* Parse a decl-specifier-seq.
7376
7377 decl-specifier-seq:
7378 decl-specifier-seq [opt] decl-specifier
7379
7380 decl-specifier:
7381 storage-class-specifier
7382 type-specifier
7383 function-specifier
7384 friend
21526606 7385 typedef
a723baf1
MM
7386
7387 GNU Extension:
7388
15077df5
MM
7389 decl-specifier:
7390 attributes
a723baf1 7391
62d1db17 7392 Set *DECL_SPECS to a representation of the decl-specifier-seq.
a723baf1 7393
eb1aef53 7394 The parser flags FLAGS is used to control type-specifier parsing.
560ad596
MM
7395
7396 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
543ca912 7397 flags:
560ad596
MM
7398
7399 1: one of the decl-specifiers is an elaborated-type-specifier
0cbd7506 7400 (i.e., a type declaration)
560ad596 7401 2: one of the decl-specifiers is an enum-specifier or a
0cbd7506 7402 class-specifier (i.e., a type definition)
98ca843c 7403
560ad596 7404 */
a723baf1 7405
62d1db17 7406static void
21526606 7407cp_parser_decl_specifier_seq (cp_parser* parser,
62d1db17
MM
7408 cp_parser_flags flags,
7409 cp_decl_specifier_seq *decl_specs,
560ad596 7410 int* declares_class_or_enum)
a723baf1 7411{
f2ce60b8 7412 bool constructor_possible_p = !parser->in_declarator_p;
21526606 7413
62d1db17
MM
7414 /* Clear DECL_SPECS. */
7415 clear_decl_specs (decl_specs);
7416
a723baf1 7417 /* Assume no class or enumeration type is declared. */
560ad596 7418 *declares_class_or_enum = 0;
a723baf1 7419
a723baf1
MM
7420 /* Keep reading specifiers until there are no more to read. */
7421 while (true)
7422 {
a723baf1 7423 bool constructor_p;
62d1db17 7424 bool found_decl_spec;
a723baf1
MM
7425 cp_token *token;
7426
7427 /* Peek at the next token. */
7428 token = cp_lexer_peek_token (parser->lexer);
7429 /* Handle attributes. */
7430 if (token->keyword == RID_ATTRIBUTE)
7431 {
7432 /* Parse the attributes. */
98ca843c 7433 decl_specs->attributes
62d1db17
MM
7434 = chainon (decl_specs->attributes,
7435 cp_parser_attributes_opt (parser));
a723baf1
MM
7436 continue;
7437 }
62d1db17
MM
7438 /* Assume we will find a decl-specifier keyword. */
7439 found_decl_spec = true;
a723baf1
MM
7440 /* If the next token is an appropriate keyword, we can simply
7441 add it to the list. */
7442 switch (token->keyword)
7443 {
a723baf1
MM
7444 /* decl-specifier:
7445 friend */
62d1db17 7446 case RID_FRIEND:
c7baf9e9
MM
7447 if (!at_class_scope_p ())
7448 {
7449 error ("%<friend%> used outside of class");
7450 cp_lexer_purge_token (parser->lexer);
7451 }
7452 else
7453 {
7454 ++decl_specs->specs[(int) ds_friend];
7455 /* Consume the token. */
7456 cp_lexer_consume_token (parser->lexer);
7457 }
a723baf1
MM
7458 break;
7459
7460 /* function-specifier:
7461 inline
7462 virtual
7463 explicit */
7464 case RID_INLINE:
7465 case RID_VIRTUAL:
7466 case RID_EXPLICIT:
62d1db17 7467 cp_parser_function_specifier_opt (parser, decl_specs);
a723baf1 7468 break;
21526606 7469
a723baf1
MM
7470 /* decl-specifier:
7471 typedef */
7472 case RID_TYPEDEF:
62d1db17 7473 ++decl_specs->specs[(int) ds_typedef];
a723baf1
MM
7474 /* Consume the token. */
7475 cp_lexer_consume_token (parser->lexer);
2050a1bb
MM
7476 /* A constructor declarator cannot appear in a typedef. */
7477 constructor_possible_p = false;
c006d942
MM
7478 /* The "typedef" keyword can only occur in a declaration; we
7479 may as well commit at this point. */
7480 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
7481 break;
7482
7483 /* storage-class-specifier:
7484 auto
7485 register
7486 static
7487 extern
21526606 7488 mutable
a723baf1 7489
0cbd7506 7490 GNU Extension:
a723baf1
MM
7491 thread */
7492 case RID_AUTO:
7493 case RID_REGISTER:
7494 case RID_STATIC:
7495 case RID_EXTERN:
7496 case RID_MUTABLE:
62d1db17
MM
7497 /* Consume the token. */
7498 cp_lexer_consume_token (parser->lexer);
bfce0853 7499 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
62d1db17 7500 break;
a723baf1 7501 case RID_THREAD:
62d1db17
MM
7502 /* Consume the token. */
7503 cp_lexer_consume_token (parser->lexer);
7504 ++decl_specs->specs[(int) ds_thread];
a723baf1 7505 break;
21526606 7506
a723baf1 7507 default:
62d1db17
MM
7508 /* We did not yet find a decl-specifier yet. */
7509 found_decl_spec = false;
a723baf1
MM
7510 break;
7511 }
7512
7513 /* Constructors are a special case. The `S' in `S()' is not a
7514 decl-specifier; it is the beginning of the declarator. */
98ca843c 7515 constructor_p
62d1db17
MM
7516 = (!found_decl_spec
7517 && constructor_possible_p
98ca843c 7518 && (cp_parser_constructor_declarator_p
62d1db17 7519 (parser, decl_specs->specs[(int) ds_friend] != 0)));
a723baf1
MM
7520
7521 /* If we don't have a DECL_SPEC yet, then we must be looking at
7522 a type-specifier. */
62d1db17 7523 if (!found_decl_spec && !constructor_p)
a723baf1 7524 {
560ad596 7525 int decl_spec_declares_class_or_enum;
a723baf1 7526 bool is_cv_qualifier;
62d1db17 7527 tree type_spec;
a723baf1 7528
62d1db17 7529 type_spec
a723baf1 7530 = cp_parser_type_specifier (parser, flags,
62d1db17 7531 decl_specs,
a723baf1
MM
7532 /*is_declaration=*/true,
7533 &decl_spec_declares_class_or_enum,
7534 &is_cv_qualifier);
7535
7536 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7537
7538 /* If this type-specifier referenced a user-defined type
7539 (a typedef, class-name, etc.), then we can't allow any
7540 more such type-specifiers henceforth.
7541
7542 [dcl.spec]
7543
7544 The longest sequence of decl-specifiers that could
7545 possibly be a type name is taken as the
7546 decl-specifier-seq of a declaration. The sequence shall
7547 be self-consistent as described below.
7548
7549 [dcl.type]
7550
7551 As a general rule, at most one type-specifier is allowed
7552 in the complete decl-specifier-seq of a declaration. The
7553 only exceptions are the following:
7554
7555 -- const or volatile can be combined with any other
21526606 7556 type-specifier.
a723baf1
MM
7557
7558 -- signed or unsigned can be combined with char, long,
7559 short, or int.
7560
7561 -- ..
7562
7563 Example:
7564
7565 typedef char* Pc;
7566 void g (const int Pc);
7567
7568 Here, Pc is *not* part of the decl-specifier seq; it's
7569 the declarator. Therefore, once we see a type-specifier
7570 (other than a cv-qualifier), we forbid any additional
7571 user-defined types. We *do* still allow things like `int
7572 int' to be considered a decl-specifier-seq, and issue the
7573 error message later. */
62d1db17 7574 if (type_spec && !is_cv_qualifier)
a723baf1 7575 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
2050a1bb 7576 /* A constructor declarator cannot follow a type-specifier. */
62d1db17 7577 if (type_spec)
a723baf1 7578 {
62d1db17
MM
7579 constructor_possible_p = false;
7580 found_decl_spec = true;
a723baf1 7581 }
a723baf1
MM
7582 }
7583
62d1db17
MM
7584 /* If we still do not have a DECL_SPEC, then there are no more
7585 decl-specifiers. */
7586 if (!found_decl_spec)
7587 break;
a723baf1 7588
62d1db17 7589 decl_specs->any_specifiers_p = true;
a723baf1
MM
7590 /* After we see one decl-specifier, further decl-specifiers are
7591 always optional. */
7592 flags |= CP_PARSER_FLAGS_OPTIONAL;
7593 }
7594
856367df 7595 cp_parser_check_decl_spec (decl_specs);
28c84d63 7596
0426c4ca 7597 /* Don't allow a friend specifier with a class definition. */
62d1db17
MM
7598 if (decl_specs->specs[(int) ds_friend] != 0
7599 && (*declares_class_or_enum & 2))
0426c4ca 7600 error ("class definition may not be declared a friend");
a723baf1
MM
7601}
7602
21526606 7603/* Parse an (optional) storage-class-specifier.
a723baf1
MM
7604
7605 storage-class-specifier:
7606 auto
7607 register
7608 static
7609 extern
21526606 7610 mutable
a723baf1
MM
7611
7612 GNU Extension:
7613
7614 storage-class-specifier:
7615 thread
7616
7617 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
21526606 7618
a723baf1 7619static tree
94edc4ab 7620cp_parser_storage_class_specifier_opt (cp_parser* parser)
a723baf1
MM
7621{
7622 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7623 {
7624 case RID_AUTO:
7625 case RID_REGISTER:
7626 case RID_STATIC:
7627 case RID_EXTERN:
7628 case RID_MUTABLE:
7629 case RID_THREAD:
7630 /* Consume the token. */
7631 return cp_lexer_consume_token (parser->lexer)->value;
7632
7633 default:
7634 return NULL_TREE;
7635 }
7636}
7637
21526606 7638/* Parse an (optional) function-specifier.
a723baf1
MM
7639
7640 function-specifier:
7641 inline
7642 virtual
7643 explicit
7644
62d1db17
MM
7645 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7646 Updates DECL_SPECS, if it is non-NULL. */
21526606 7647
a723baf1 7648static tree
62d1db17
MM
7649cp_parser_function_specifier_opt (cp_parser* parser,
7650 cp_decl_specifier_seq *decl_specs)
a723baf1
MM
7651{
7652 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7653 {
7654 case RID_INLINE:
62d1db17
MM
7655 if (decl_specs)
7656 ++decl_specs->specs[(int) ds_inline];
7657 break;
7658
a723baf1 7659 case RID_VIRTUAL:
ceacde63 7660 /* 14.5.2.3 [temp.mem]
3db45ab5
MS
7661
7662 A member function template shall not be virtual. */
ceacde63
MM
7663 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7664 error ("templates may not be %<virtual%>");
7665 else if (decl_specs)
62d1db17
MM
7666 ++decl_specs->specs[(int) ds_virtual];
7667 break;
7668
a723baf1 7669 case RID_EXPLICIT:
62d1db17
MM
7670 if (decl_specs)
7671 ++decl_specs->specs[(int) ds_explicit];
7672 break;
a723baf1
MM
7673
7674 default:
7675 return NULL_TREE;
7676 }
62d1db17
MM
7677
7678 /* Consume the token. */
7679 return cp_lexer_consume_token (parser->lexer)->value;
a723baf1
MM
7680}
7681
7682/* Parse a linkage-specification.
7683
7684 linkage-specification:
7685 extern string-literal { declaration-seq [opt] }
7686 extern string-literal declaration */
7687
7688static void
94edc4ab 7689cp_parser_linkage_specification (cp_parser* parser)
a723baf1 7690{
a723baf1
MM
7691 tree linkage;
7692
7693 /* Look for the `extern' keyword. */
7694 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7695
c162c75e
MA
7696 /* Look for the string-literal. */
7697 linkage = cp_parser_string_literal (parser, false, false);
a723baf1
MM
7698
7699 /* Transform the literal into an identifier. If the literal is a
7700 wide-character string, or contains embedded NULs, then we can't
7701 handle it as the user wants. */
c162c75e
MA
7702 if (strlen (TREE_STRING_POINTER (linkage))
7703 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
a723baf1
MM
7704 {
7705 cp_parser_error (parser, "invalid linkage-specification");
7706 /* Assume C++ linkage. */
c162c75e 7707 linkage = lang_name_cplusplus;
a723baf1 7708 }
a723baf1 7709 else
c162c75e 7710 linkage = get_identifier (TREE_STRING_POINTER (linkage));
a723baf1
MM
7711
7712 /* We're now using the new linkage. */
7713 push_lang_context (linkage);
7714
7715 /* If the next token is a `{', then we're using the first
7716 production. */
7717 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7718 {
7719 /* Consume the `{' token. */
7720 cp_lexer_consume_token (parser->lexer);
7721 /* Parse the declarations. */
7722 cp_parser_declaration_seq_opt (parser);
7723 /* Look for the closing `}'. */
7724 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7725 }
7726 /* Otherwise, there's just one declaration. */
7727 else
7728 {
7729 bool saved_in_unbraced_linkage_specification_p;
7730
21526606 7731 saved_in_unbraced_linkage_specification_p
a723baf1
MM
7732 = parser->in_unbraced_linkage_specification_p;
7733 parser->in_unbraced_linkage_specification_p = true;
a723baf1 7734 cp_parser_declaration (parser);
21526606 7735 parser->in_unbraced_linkage_specification_p
a723baf1
MM
7736 = saved_in_unbraced_linkage_specification_p;
7737 }
7738
7739 /* We're done with the linkage-specification. */
7740 pop_lang_context ();
7741}
7742
7743/* Special member functions [gram.special] */
7744
7745/* Parse a conversion-function-id.
7746
7747 conversion-function-id:
21526606 7748 operator conversion-type-id
a723baf1
MM
7749
7750 Returns an IDENTIFIER_NODE representing the operator. */
7751
21526606 7752static tree
94edc4ab 7753cp_parser_conversion_function_id (cp_parser* parser)
a723baf1
MM
7754{
7755 tree type;
7756 tree saved_scope;
7757 tree saved_qualifying_scope;
7758 tree saved_object_scope;
4514aa8c 7759 tree pushed_scope = NULL_TREE;
a723baf1
MM
7760
7761 /* Look for the `operator' token. */
7762 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7763 return error_mark_node;
7764 /* When we parse the conversion-type-id, the current scope will be
7765 reset. However, we need that information in able to look up the
7766 conversion function later, so we save it here. */
7767 saved_scope = parser->scope;
7768 saved_qualifying_scope = parser->qualifying_scope;
7769 saved_object_scope = parser->object_scope;
7770 /* We must enter the scope of the class so that the names of
7771 entities declared within the class are available in the
7772 conversion-type-id. For example, consider:
7773
21526606 7774 struct S {
0cbd7506 7775 typedef int I;
a723baf1
MM
7776 operator I();
7777 };
7778
7779 S::operator I() { ... }
7780
7781 In order to see that `I' is a type-name in the definition, we
7782 must be in the scope of `S'. */
7783 if (saved_scope)
4514aa8c 7784 pushed_scope = push_scope (saved_scope);
a723baf1
MM
7785 /* Parse the conversion-type-id. */
7786 type = cp_parser_conversion_type_id (parser);
7787 /* Leave the scope of the class, if any. */
4514aa8c
NS
7788 if (pushed_scope)
7789 pop_scope (pushed_scope);
a723baf1
MM
7790 /* Restore the saved scope. */
7791 parser->scope = saved_scope;
7792 parser->qualifying_scope = saved_qualifying_scope;
7793 parser->object_scope = saved_object_scope;
7794 /* If the TYPE is invalid, indicate failure. */
7795 if (type == error_mark_node)
7796 return error_mark_node;
7797 return mangle_conv_op_name_for_type (type);
7798}
7799
7800/* Parse a conversion-type-id:
7801
7802 conversion-type-id:
7803 type-specifier-seq conversion-declarator [opt]
7804
7805 Returns the TYPE specified. */
7806
7807static tree
94edc4ab 7808cp_parser_conversion_type_id (cp_parser* parser)
a723baf1
MM
7809{
7810 tree attributes;
62d1db17 7811 cp_decl_specifier_seq type_specifiers;
058b15c1 7812 cp_declarator *declarator;
037cc9c5 7813 tree type_specified;
a723baf1
MM
7814
7815 /* Parse the attributes. */
7816 attributes = cp_parser_attributes_opt (parser);
7817 /* Parse the type-specifiers. */
d4113656
MM
7818 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7819 &type_specifiers);
a723baf1 7820 /* If that didn't work, stop. */
62d1db17 7821 if (type_specifiers.type == error_mark_node)
a723baf1
MM
7822 return error_mark_node;
7823 /* Parse the conversion-declarator. */
7824 declarator = cp_parser_conversion_declarator_opt (parser);
7825
037cc9c5 7826 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
0cbd7506 7827 /*initialized=*/0, &attributes);
037cc9c5
FJ
7828 if (attributes)
7829 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7830 return type_specified;
a723baf1
MM
7831}
7832
7833/* Parse an (optional) conversion-declarator.
7834
7835 conversion-declarator:
21526606 7836 ptr-operator conversion-declarator [opt]
a723baf1 7837
058b15c1 7838 */
a723baf1 7839
058b15c1 7840static cp_declarator *
94edc4ab 7841cp_parser_conversion_declarator_opt (cp_parser* parser)
a723baf1
MM
7842{
7843 enum tree_code code;
7844 tree class_type;
3c01e5df 7845 cp_cv_quals cv_quals;
a723baf1
MM
7846
7847 /* We don't know if there's a ptr-operator next, or not. */
7848 cp_parser_parse_tentatively (parser);
7849 /* Try the ptr-operator. */
3c01e5df 7850 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
a723baf1
MM
7851 /* If it worked, look for more conversion-declarators. */
7852 if (cp_parser_parse_definitely (parser))
7853 {
058b15c1 7854 cp_declarator *declarator;
98ca843c 7855
058b15c1
MM
7856 /* Parse another optional declarator. */
7857 declarator = cp_parser_conversion_declarator_opt (parser);
98ca843c 7858
058b15c1
MM
7859 /* Create the representation of the declarator. */
7860 if (class_type)
3c01e5df 7861 declarator = make_ptrmem_declarator (cv_quals, class_type,
a723baf1 7862 declarator);
058b15c1 7863 else if (code == INDIRECT_REF)
3c01e5df 7864 declarator = make_pointer_declarator (cv_quals, declarator);
058b15c1 7865 else
3c01e5df 7866 declarator = make_reference_declarator (cv_quals, declarator);
98ca843c 7867
058b15c1 7868 return declarator;
a723baf1
MM
7869 }
7870
058b15c1 7871 return NULL;
a723baf1
MM
7872}
7873
7874/* Parse an (optional) ctor-initializer.
7875
7876 ctor-initializer:
21526606 7877 : mem-initializer-list
a723baf1
MM
7878
7879 Returns TRUE iff the ctor-initializer was actually present. */
7880
7881static bool
94edc4ab 7882cp_parser_ctor_initializer_opt (cp_parser* parser)
a723baf1
MM
7883{
7884 /* If the next token is not a `:', then there is no
7885 ctor-initializer. */
7886 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7887 {
7888 /* Do default initialization of any bases and members. */
7889 if (DECL_CONSTRUCTOR_P (current_function_decl))
7890 finish_mem_initializers (NULL_TREE);
7891
7892 return false;
7893 }
7894
7895 /* Consume the `:' token. */
7896 cp_lexer_consume_token (parser->lexer);
7897 /* And the mem-initializer-list. */
7898 cp_parser_mem_initializer_list (parser);
7899
7900 return true;
7901}
7902
7903/* Parse a mem-initializer-list.
7904
7905 mem-initializer-list:
7906 mem-initializer
7907 mem-initializer , mem-initializer-list */
7908
7909static void
94edc4ab 7910cp_parser_mem_initializer_list (cp_parser* parser)
a723baf1
MM
7911{
7912 tree mem_initializer_list = NULL_TREE;
7913
7914 /* Let the semantic analysis code know that we are starting the
7915 mem-initializer-list. */
0e136342
MM
7916 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7917 error ("only constructors take base initializers");
a723baf1
MM
7918
7919 /* Loop through the list. */
7920 while (true)
7921 {
7922 tree mem_initializer;
7923
7924 /* Parse the mem-initializer. */
7925 mem_initializer = cp_parser_mem_initializer (parser);
7926 /* Add it to the list, unless it was erroneous. */
357d956e 7927 if (mem_initializer != error_mark_node)
a723baf1
MM
7928 {
7929 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7930 mem_initializer_list = mem_initializer;
7931 }
7932 /* If the next token is not a `,', we're done. */
7933 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7934 break;
7935 /* Consume the `,' token. */
7936 cp_lexer_consume_token (parser->lexer);
7937 }
7938
7939 /* Perform semantic analysis. */
0e136342
MM
7940 if (DECL_CONSTRUCTOR_P (current_function_decl))
7941 finish_mem_initializers (mem_initializer_list);
a723baf1
MM
7942}
7943
7944/* Parse a mem-initializer.
7945
7946 mem-initializer:
21526606 7947 mem-initializer-id ( expression-list [opt] )
a723baf1
MM
7948
7949 GNU extension:
21526606 7950
a723baf1 7951 mem-initializer:
34cd5ae7 7952 ( expression-list [opt] )
a723baf1
MM
7953
7954 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7955 class) or FIELD_DECL (for a non-static data member) to initialize;
357d956e
MM
7956 the TREE_VALUE is the expression-list. An empty initialization
7957 list is represented by void_list_node. */
a723baf1
MM
7958
7959static tree
94edc4ab 7960cp_parser_mem_initializer (cp_parser* parser)
a723baf1
MM
7961{
7962 tree mem_initializer_id;
7963 tree expression_list;
1f5a253a 7964 tree member;
21526606 7965
a723baf1
MM
7966 /* Find out what is being initialized. */
7967 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7968 {
7969 pedwarn ("anachronistic old-style base class initializer");
7970 mem_initializer_id = NULL_TREE;
7971 }
7972 else
7973 mem_initializer_id = cp_parser_mem_initializer_id (parser);
1f5a253a
NS
7974 member = expand_member_init (mem_initializer_id);
7975 if (member && !DECL_P (member))
7976 in_base_initializer = 1;
7efa3e22 7977
21526606 7978 expression_list
39703eb9 7979 = cp_parser_parenthesized_expression_list (parser, false,
93678513 7980 /*cast_p=*/false,
39703eb9 7981 /*non_constant_p=*/NULL);
357d956e
MM
7982 if (expression_list == error_mark_node)
7983 return error_mark_node;
7efa3e22 7984 if (!expression_list)
a723baf1 7985 expression_list = void_type_node;
a723baf1 7986
1f5a253a 7987 in_base_initializer = 0;
21526606 7988
357d956e 7989 return member ? build_tree_list (member, expression_list) : error_mark_node;
a723baf1
MM
7990}
7991
7992/* Parse a mem-initializer-id.
7993
7994 mem-initializer-id:
7995 :: [opt] nested-name-specifier [opt] class-name
21526606 7996 identifier
a723baf1
MM
7997
7998 Returns a TYPE indicating the class to be initializer for the first
7999 production. Returns an IDENTIFIER_NODE indicating the data member
8000 to be initialized for the second production. */
8001
8002static tree
94edc4ab 8003cp_parser_mem_initializer_id (cp_parser* parser)
a723baf1
MM
8004{
8005 bool global_scope_p;
8006 bool nested_name_specifier_p;
8a83a693 8007 bool template_p = false;
a723baf1
MM
8008 tree id;
8009
8a83a693
GB
8010 /* `typename' is not allowed in this context ([temp.res]). */
8011 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8012 {
2a13a625 8013 error ("keyword %<typename%> not allowed in this context (a qualified "
8a83a693
GB
8014 "member initializer is implicitly a type)");
8015 cp_lexer_consume_token (parser->lexer);
8016 }
a723baf1 8017 /* Look for the optional `::' operator. */
21526606
EC
8018 global_scope_p
8019 = (cp_parser_global_scope_opt (parser,
8020 /*current_scope_valid_p=*/false)
a723baf1
MM
8021 != NULL_TREE);
8022 /* Look for the optional nested-name-specifier. The simplest way to
8023 implement:
8024
8025 [temp.res]
8026
8027 The keyword `typename' is not permitted in a base-specifier or
8028 mem-initializer; in these contexts a qualified name that
8029 depends on a template-parameter is implicitly assumed to be a
8030 type name.
8031
8032 is to assume that we have seen the `typename' keyword at this
8033 point. */
21526606 8034 nested_name_specifier_p
a723baf1
MM
8035 = (cp_parser_nested_name_specifier_opt (parser,
8036 /*typename_keyword_p=*/true,
8037 /*check_dependency_p=*/true,
a668c6ad
MM
8038 /*type_p=*/true,
8039 /*is_declaration=*/true)
a723baf1 8040 != NULL_TREE);
8a83a693
GB
8041 if (nested_name_specifier_p)
8042 template_p = cp_parser_optional_template_keyword (parser);
a723baf1
MM
8043 /* If there is a `::' operator or a nested-name-specifier, then we
8044 are definitely looking for a class-name. */
8045 if (global_scope_p || nested_name_specifier_p)
8046 return cp_parser_class_name (parser,
8047 /*typename_keyword_p=*/true,
8a83a693 8048 /*template_keyword_p=*/template_p,
fc6a28d7 8049 none_type,
a723baf1 8050 /*check_dependency_p=*/true,
a668c6ad
MM
8051 /*class_head_p=*/false,
8052 /*is_declaration=*/true);
a723baf1
MM
8053 /* Otherwise, we could also be looking for an ordinary identifier. */
8054 cp_parser_parse_tentatively (parser);
8055 /* Try a class-name. */
21526606 8056 id = cp_parser_class_name (parser,
a723baf1
MM
8057 /*typename_keyword_p=*/true,
8058 /*template_keyword_p=*/false,
fc6a28d7 8059 none_type,
a723baf1 8060 /*check_dependency_p=*/true,
a668c6ad
MM
8061 /*class_head_p=*/false,
8062 /*is_declaration=*/true);
a723baf1
MM
8063 /* If we found one, we're done. */
8064 if (cp_parser_parse_definitely (parser))
8065 return id;
8066 /* Otherwise, look for an ordinary identifier. */
8067 return cp_parser_identifier (parser);
8068}
8069
8070/* Overloading [gram.over] */
8071
8072/* Parse an operator-function-id.
8073
8074 operator-function-id:
21526606 8075 operator operator
a723baf1
MM
8076
8077 Returns an IDENTIFIER_NODE for the operator which is a
8078 human-readable spelling of the identifier, e.g., `operator +'. */
8079
21526606 8080static tree
94edc4ab 8081cp_parser_operator_function_id (cp_parser* parser)
a723baf1
MM
8082{
8083 /* Look for the `operator' keyword. */
8084 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8085 return error_mark_node;
8086 /* And then the name of the operator itself. */
8087 return cp_parser_operator (parser);
8088}
8089
8090/* Parse an operator.
8091
8092 operator:
8093 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8094 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8095 || ++ -- , ->* -> () []
8096
8097 GNU Extensions:
21526606 8098
a723baf1
MM
8099 operator:
8100 <? >? <?= >?=
8101
8102 Returns an IDENTIFIER_NODE for the operator which is a
8103 human-readable spelling of the identifier, e.g., `operator +'. */
21526606 8104
a723baf1 8105static tree
94edc4ab 8106cp_parser_operator (cp_parser* parser)
a723baf1
MM
8107{
8108 tree id = NULL_TREE;
8109 cp_token *token;
8110
8111 /* Peek at the next token. */
8112 token = cp_lexer_peek_token (parser->lexer);
8113 /* Figure out which operator we have. */
8114 switch (token->type)
8115 {
8116 case CPP_KEYWORD:
8117 {
8118 enum tree_code op;
8119
8120 /* The keyword should be either `new' or `delete'. */
8121 if (token->keyword == RID_NEW)
8122 op = NEW_EXPR;
8123 else if (token->keyword == RID_DELETE)
8124 op = DELETE_EXPR;
8125 else
8126 break;
8127
8128 /* Consume the `new' or `delete' token. */
8129 cp_lexer_consume_token (parser->lexer);
8130
8131 /* Peek at the next token. */
8132 token = cp_lexer_peek_token (parser->lexer);
8133 /* If it's a `[' token then this is the array variant of the
8134 operator. */
8135 if (token->type == CPP_OPEN_SQUARE)
8136 {
8137 /* Consume the `[' token. */
8138 cp_lexer_consume_token (parser->lexer);
8139 /* Look for the `]' token. */
8140 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
21526606 8141 id = ansi_opname (op == NEW_EXPR
a723baf1
MM
8142 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8143 }
8144 /* Otherwise, we have the non-array variant. */
8145 else
8146 id = ansi_opname (op);
8147
8148 return id;
8149 }
8150
8151 case CPP_PLUS:
8152 id = ansi_opname (PLUS_EXPR);
8153 break;
8154
8155 case CPP_MINUS:
8156 id = ansi_opname (MINUS_EXPR);
8157 break;
8158
8159 case CPP_MULT:
8160 id = ansi_opname (MULT_EXPR);
8161 break;
8162
8163 case CPP_DIV:
8164 id = ansi_opname (TRUNC_DIV_EXPR);
8165 break;
8166
8167 case CPP_MOD:
8168 id = ansi_opname (TRUNC_MOD_EXPR);
8169 break;
8170
8171 case CPP_XOR:
8172 id = ansi_opname (BIT_XOR_EXPR);
8173 break;
8174
8175 case CPP_AND:
8176 id = ansi_opname (BIT_AND_EXPR);
8177 break;
8178
8179 case CPP_OR:
8180 id = ansi_opname (BIT_IOR_EXPR);
8181 break;
8182
8183 case CPP_COMPL:
8184 id = ansi_opname (BIT_NOT_EXPR);
8185 break;
21526606 8186
a723baf1
MM
8187 case CPP_NOT:
8188 id = ansi_opname (TRUTH_NOT_EXPR);
8189 break;
8190
8191 case CPP_EQ:
8192 id = ansi_assopname (NOP_EXPR);
8193 break;
8194
8195 case CPP_LESS:
8196 id = ansi_opname (LT_EXPR);
8197 break;
8198
8199 case CPP_GREATER:
8200 id = ansi_opname (GT_EXPR);
8201 break;
8202
8203 case CPP_PLUS_EQ:
8204 id = ansi_assopname (PLUS_EXPR);
8205 break;
8206
8207 case CPP_MINUS_EQ:
8208 id = ansi_assopname (MINUS_EXPR);
8209 break;
8210
8211 case CPP_MULT_EQ:
8212 id = ansi_assopname (MULT_EXPR);
8213 break;
8214
8215 case CPP_DIV_EQ:
8216 id = ansi_assopname (TRUNC_DIV_EXPR);
8217 break;
8218
8219 case CPP_MOD_EQ:
8220 id = ansi_assopname (TRUNC_MOD_EXPR);
8221 break;
8222
8223 case CPP_XOR_EQ:
8224 id = ansi_assopname (BIT_XOR_EXPR);
8225 break;
8226
8227 case CPP_AND_EQ:
8228 id = ansi_assopname (BIT_AND_EXPR);
8229 break;
8230
8231 case CPP_OR_EQ:
8232 id = ansi_assopname (BIT_IOR_EXPR);
8233 break;
8234
8235 case CPP_LSHIFT:
8236 id = ansi_opname (LSHIFT_EXPR);
8237 break;
8238
8239 case CPP_RSHIFT:
8240 id = ansi_opname (RSHIFT_EXPR);
8241 break;
8242
8243 case CPP_LSHIFT_EQ:
8244 id = ansi_assopname (LSHIFT_EXPR);
8245 break;
8246
8247 case CPP_RSHIFT_EQ:
8248 id = ansi_assopname (RSHIFT_EXPR);
8249 break;
8250
8251 case CPP_EQ_EQ:
8252 id = ansi_opname (EQ_EXPR);
8253 break;
8254
8255 case CPP_NOT_EQ:
8256 id = ansi_opname (NE_EXPR);
8257 break;
8258
8259 case CPP_LESS_EQ:
8260 id = ansi_opname (LE_EXPR);
8261 break;
8262
8263 case CPP_GREATER_EQ:
8264 id = ansi_opname (GE_EXPR);
8265 break;
8266
8267 case CPP_AND_AND:
8268 id = ansi_opname (TRUTH_ANDIF_EXPR);
8269 break;
8270
8271 case CPP_OR_OR:
8272 id = ansi_opname (TRUTH_ORIF_EXPR);
8273 break;
21526606 8274
a723baf1
MM
8275 case CPP_PLUS_PLUS:
8276 id = ansi_opname (POSTINCREMENT_EXPR);
8277 break;
8278
8279 case CPP_MINUS_MINUS:
8280 id = ansi_opname (PREDECREMENT_EXPR);
8281 break;
8282
8283 case CPP_COMMA:
8284 id = ansi_opname (COMPOUND_EXPR);
8285 break;
8286
8287 case CPP_DEREF_STAR:
8288 id = ansi_opname (MEMBER_REF);
8289 break;
8290
8291 case CPP_DEREF:
8292 id = ansi_opname (COMPONENT_REF);
8293 break;
8294
8295 case CPP_OPEN_PAREN:
8296 /* Consume the `('. */
8297 cp_lexer_consume_token (parser->lexer);
8298 /* Look for the matching `)'. */
8299 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8300 return ansi_opname (CALL_EXPR);
8301
8302 case CPP_OPEN_SQUARE:
8303 /* Consume the `['. */
8304 cp_lexer_consume_token (parser->lexer);
8305 /* Look for the matching `]'. */
8306 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8307 return ansi_opname (ARRAY_REF);
8308
a723baf1
MM
8309 default:
8310 /* Anything else is an error. */
8311 break;
8312 }
8313
8314 /* If we have selected an identifier, we need to consume the
8315 operator token. */
8316 if (id)
8317 cp_lexer_consume_token (parser->lexer);
8318 /* Otherwise, no valid operator name was present. */
8319 else
8320 {
8321 cp_parser_error (parser, "expected operator");
8322 id = error_mark_node;
8323 }
8324
8325 return id;
8326}
8327
8328/* Parse a template-declaration.
8329
8330 template-declaration:
21526606 8331 export [opt] template < template-parameter-list > declaration
a723baf1
MM
8332
8333 If MEMBER_P is TRUE, this template-declaration occurs within a
21526606 8334 class-specifier.
a723baf1
MM
8335
8336 The grammar rule given by the standard isn't correct. What
8337 is really meant is:
8338
8339 template-declaration:
21526606 8340 export [opt] template-parameter-list-seq
a723baf1 8341 decl-specifier-seq [opt] init-declarator [opt] ;
21526606 8342 export [opt] template-parameter-list-seq
a723baf1
MM
8343 function-definition
8344
8345 template-parameter-list-seq:
8346 template-parameter-list-seq [opt]
8347 template < template-parameter-list > */
8348
8349static void
94edc4ab 8350cp_parser_template_declaration (cp_parser* parser, bool member_p)
a723baf1
MM
8351{
8352 /* Check for `export'. */
8353 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8354 {
8355 /* Consume the `export' token. */
8356 cp_lexer_consume_token (parser->lexer);
8357 /* Warn that we do not support `export'. */
d4ee4d25 8358 warning (0, "keyword %<export%> not implemented, and will be ignored");
a723baf1
MM
8359 }
8360
8361 cp_parser_template_declaration_after_export (parser, member_p);
8362}
8363
8364/* Parse a template-parameter-list.
8365
8366 template-parameter-list:
8367 template-parameter
8368 template-parameter-list , template-parameter
8369
8370 Returns a TREE_LIST. Each node represents a template parameter.
8371 The nodes are connected via their TREE_CHAINs. */
8372
8373static tree
94edc4ab 8374cp_parser_template_parameter_list (cp_parser* parser)
a723baf1
MM
8375{
8376 tree parameter_list = NULL_TREE;
8377
357d956e 8378 begin_template_parm_list ();
a723baf1
MM
8379 while (true)
8380 {
8381 tree parameter;
8382 cp_token *token;
058b15c1 8383 bool is_non_type;
a723baf1
MM
8384
8385 /* Parse the template-parameter. */
058b15c1 8386 parameter = cp_parser_template_parameter (parser, &is_non_type);
a723baf1 8387 /* Add it to the list. */
943e3ede
MM
8388 if (parameter != error_mark_node)
8389 parameter_list = process_template_parm (parameter_list,
8390 parameter,
8391 is_non_type);
a723baf1
MM
8392 /* Peek at the next token. */
8393 token = cp_lexer_peek_token (parser->lexer);
8394 /* If it's not a `,', we're done. */
8395 if (token->type != CPP_COMMA)
8396 break;
8397 /* Otherwise, consume the `,' token. */
8398 cp_lexer_consume_token (parser->lexer);
8399 }
8400
357d956e 8401 return end_template_parm_list (parameter_list);
a723baf1
MM
8402}
8403
8404/* Parse a template-parameter.
8405
8406 template-parameter:
8407 type-parameter
8408 parameter-declaration
8409
943e3ede
MM
8410 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8411 the parameter. The TREE_PURPOSE is the default value, if any.
8412 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8413 iff this parameter is a non-type parameter. */
a723baf1
MM
8414
8415static tree
058b15c1 8416cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
a723baf1
MM
8417{
8418 cp_token *token;
62d1db17 8419 cp_parameter_declarator *parameter_declarator;
943e3ede 8420 tree parm;
a723baf1 8421
058b15c1
MM
8422 /* Assume it is a type parameter or a template parameter. */
8423 *is_non_type = false;
a723baf1
MM
8424 /* Peek at the next token. */
8425 token = cp_lexer_peek_token (parser->lexer);
8426 /* If it is `class' or `template', we have a type-parameter. */
8427 if (token->keyword == RID_TEMPLATE)
8428 return cp_parser_type_parameter (parser);
8429 /* If it is `class' or `typename' we do not know yet whether it is a
8430 type parameter or a non-type parameter. Consider:
8431
8432 template <typename T, typename T::X X> ...
8433
8434 or:
21526606 8435
a723baf1
MM
8436 template <class C, class D*> ...
8437
8438 Here, the first parameter is a type parameter, and the second is
8439 a non-type parameter. We can tell by looking at the token after
8440 the identifier -- if it is a `,', `=', or `>' then we have a type
8441 parameter. */
8442 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8443 {
8444 /* Peek at the token after `class' or `typename'. */
8445 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8446 /* If it's an identifier, skip it. */
8447 if (token->type == CPP_NAME)
8448 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8449 /* Now, see if the token looks like the end of a template
8450 parameter. */
21526606 8451 if (token->type == CPP_COMMA
a723baf1
MM
8452 || token->type == CPP_EQ
8453 || token->type == CPP_GREATER)
8454 return cp_parser_type_parameter (parser);
8455 }
8456
21526606 8457 /* Otherwise, it is a non-type parameter.
a723baf1
MM
8458
8459 [temp.param]
8460
8461 When parsing a default template-argument for a non-type
8462 template-parameter, the first non-nested `>' is taken as the end
8463 of the template parameter-list rather than a greater-than
8464 operator. */
058b15c1
MM
8465 *is_non_type = true;
8466 parameter_declarator
8467 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8468 /*parenthesized_p=*/NULL);
943e3ede
MM
8469 parm = grokdeclarator (parameter_declarator->declarator,
8470 &parameter_declarator->decl_specifiers,
8471 PARM, /*initialized=*/0,
8472 /*attrlist=*/NULL);
8473 if (parm == error_mark_node)
8474 return error_mark_node;
8475 return build_tree_list (parameter_declarator->default_argument, parm);
a723baf1
MM
8476}
8477
8478/* Parse a type-parameter.
8479
8480 type-parameter:
8481 class identifier [opt]
8482 class identifier [opt] = type-id
8483 typename identifier [opt]
8484 typename identifier [opt] = type-id
8485 template < template-parameter-list > class identifier [opt]
21526606
EC
8486 template < template-parameter-list > class identifier [opt]
8487 = id-expression
a723baf1
MM
8488
8489 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8490 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8491 the declaration of the parameter. */
8492
8493static tree
94edc4ab 8494cp_parser_type_parameter (cp_parser* parser)
a723baf1
MM
8495{
8496 cp_token *token;
8497 tree parameter;
8498
8499 /* Look for a keyword to tell us what kind of parameter this is. */
21526606 8500 token = cp_parser_require (parser, CPP_KEYWORD,
8a6393df 8501 "`class', `typename', or `template'");
a723baf1
MM
8502 if (!token)
8503 return error_mark_node;
8504
8505 switch (token->keyword)
8506 {
8507 case RID_CLASS:
8508 case RID_TYPENAME:
8509 {
8510 tree identifier;
8511 tree default_argument;
8512
8513 /* If the next token is an identifier, then it names the
0cbd7506 8514 parameter. */
a723baf1
MM
8515 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8516 identifier = cp_parser_identifier (parser);
8517 else
8518 identifier = NULL_TREE;
8519
8520 /* Create the parameter. */
8521 parameter = finish_template_type_parm (class_type_node, identifier);
8522
8523 /* If the next token is an `=', we have a default argument. */
8524 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8525 {
8526 /* Consume the `=' token. */
8527 cp_lexer_consume_token (parser->lexer);
34cd5ae7 8528 /* Parse the default-argument. */
6b648482 8529 push_deferring_access_checks (dk_no_deferred);
a723baf1 8530 default_argument = cp_parser_type_id (parser);
6b648482 8531 pop_deferring_access_checks ();
a723baf1
MM
8532 }
8533 else
8534 default_argument = NULL_TREE;
8535
8536 /* Create the combined representation of the parameter and the
8537 default argument. */
c67d36d0 8538 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
8539 }
8540 break;
8541
8542 case RID_TEMPLATE:
8543 {
8544 tree parameter_list;
8545 tree identifier;
8546 tree default_argument;
8547
8548 /* Look for the `<'. */
8549 cp_parser_require (parser, CPP_LESS, "`<'");
8550 /* Parse the template-parameter-list. */
357d956e 8551 parameter_list = cp_parser_template_parameter_list (parser);
a723baf1
MM
8552 /* Look for the `>'. */
8553 cp_parser_require (parser, CPP_GREATER, "`>'");
8554 /* Look for the `class' keyword. */
8555 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8556 /* If the next token is an `=', then there is a
8557 default-argument. If the next token is a `>', we are at
8558 the end of the parameter-list. If the next token is a `,',
8559 then we are at the end of this parameter. */
8560 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8561 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8562 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
71bd7186
MM
8563 {
8564 identifier = cp_parser_identifier (parser);
03fd3f84 8565 /* Treat invalid names as if the parameter were nameless. */
71bd7186
MM
8566 if (identifier == error_mark_node)
8567 identifier = NULL_TREE;
8568 }
a723baf1
MM
8569 else
8570 identifier = NULL_TREE;
71bd7186 8571
a723baf1
MM
8572 /* Create the template parameter. */
8573 parameter = finish_template_template_parm (class_type_node,
8574 identifier);
21526606 8575
a723baf1
MM
8576 /* If the next token is an `=', then there is a
8577 default-argument. */
8578 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8579 {
b0bc6e8e
KL
8580 bool is_template;
8581
a723baf1
MM
8582 /* Consume the `='. */
8583 cp_lexer_consume_token (parser->lexer);
8584 /* Parse the id-expression. */
6b648482 8585 push_deferring_access_checks (dk_no_deferred);
21526606 8586 default_argument
a723baf1
MM
8587 = cp_parser_id_expression (parser,
8588 /*template_keyword_p=*/false,
8589 /*check_dependency_p=*/true,
b0bc6e8e 8590 /*template_p=*/&is_template,
fa6098f8
MM
8591 /*declarator_p=*/false,
8592 /*optional_p=*/false);
a3a503a5
GB
8593 if (TREE_CODE (default_argument) == TYPE_DECL)
8594 /* If the id-expression was a template-id that refers to
8595 a template-class, we already have the declaration here,
8596 so no further lookup is needed. */
8597 ;
8598 else
8599 /* Look up the name. */
21526606 8600 default_argument
a3a503a5 8601 = cp_parser_lookup_name (parser, default_argument,
fc6a28d7
MM
8602 none_type,
8603 /*is_template=*/is_template,
8604 /*is_namespace=*/false,
8605 /*check_dependency=*/true,
91b1ca65 8606 /*ambiguous_decls=*/NULL);
a723baf1
MM
8607 /* See if the default argument is valid. */
8608 default_argument
8609 = check_template_template_default_arg (default_argument);
6b648482 8610 pop_deferring_access_checks ();
a723baf1
MM
8611 }
8612 else
8613 default_argument = NULL_TREE;
8614
8615 /* Create the combined representation of the parameter and the
8616 default argument. */
71bd7186 8617 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
8618 }
8619 break;
8620
8621 default:
71bd7186
MM
8622 gcc_unreachable ();
8623 break;
a723baf1 8624 }
21526606 8625
a723baf1
MM
8626 return parameter;
8627}
8628
8629/* Parse a template-id.
8630
8631 template-id:
8632 template-name < template-argument-list [opt] >
8633
8634 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8635 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8636 returned. Otherwise, if the template-name names a function, or set
8637 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
21526606 8638 names a class, returns a TYPE_DECL for the specialization.
a723baf1
MM
8639
8640 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8641 uninstantiated templates. */
8642
8643static tree
21526606
EC
8644cp_parser_template_id (cp_parser *parser,
8645 bool template_keyword_p,
a668c6ad
MM
8646 bool check_dependency_p,
8647 bool is_declaration)
a723baf1
MM
8648{
8649 tree template;
8650 tree arguments;
a723baf1 8651 tree template_id;
0c5e4866 8652 cp_token_position start_of_id = 0;
a723baf1 8653 tree access_check = NULL_TREE;
f4abade9 8654 cp_token *next_token, *next_token_2;
a668c6ad 8655 bool is_identifier;
a723baf1
MM
8656
8657 /* If the next token corresponds to a template-id, there is no need
8658 to reparse it. */
2050a1bb
MM
8659 next_token = cp_lexer_peek_token (parser->lexer);
8660 if (next_token->type == CPP_TEMPLATE_ID)
a723baf1
MM
8661 {
8662 tree value;
8663 tree check;
8664
8665 /* Get the stored value. */
8666 value = cp_lexer_consume_token (parser->lexer)->value;
8667 /* Perform any access checks that were deferred. */
8668 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c
KL
8669 perform_or_defer_access_check (TREE_PURPOSE (check),
8670 TREE_VALUE (check));
a723baf1
MM
8671 /* Return the stored value. */
8672 return TREE_VALUE (value);
8673 }
8674
2050a1bb
MM
8675 /* Avoid performing name lookup if there is no possibility of
8676 finding a template-id. */
8677 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8678 || (next_token->type == CPP_NAME
21526606 8679 && !cp_parser_nth_token_starts_template_argument_list_p
f4abade9 8680 (parser, 2)))
2050a1bb
MM
8681 {
8682 cp_parser_error (parser, "expected template-id");
8683 return error_mark_node;
8684 }
8685
a723baf1 8686 /* Remember where the template-id starts. */
0b16f8f4 8687 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
0c5e4866 8688 start_of_id = cp_lexer_token_position (parser->lexer, false);
a723baf1 8689
8d241e0b 8690 push_deferring_access_checks (dk_deferred);
cf22909c 8691
a723baf1 8692 /* Parse the template-name. */
a668c6ad 8693 is_identifier = false;
a723baf1 8694 template = cp_parser_template_name (parser, template_keyword_p,
a668c6ad
MM
8695 check_dependency_p,
8696 is_declaration,
8697 &is_identifier);
8698 if (template == error_mark_node || is_identifier)
cf22909c
KL
8699 {
8700 pop_deferring_access_checks ();
a668c6ad 8701 return template;
cf22909c 8702 }
a723baf1 8703
21526606 8704 /* If we find the sequence `[:' after a template-name, it's probably
f4abade9
GB
8705 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8706 parse correctly the argument list. */
2cfe82fe 8707 next_token = cp_lexer_peek_token (parser->lexer);
f4abade9 8708 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
21526606 8709 if (next_token->type == CPP_OPEN_SQUARE
f4abade9 8710 && next_token->flags & DIGRAPH
21526606 8711 && next_token_2->type == CPP_COLON
f4abade9 8712 && !(next_token_2->flags & PREV_WHITE))
cf22909c 8713 {
f4abade9
GB
8714 cp_parser_parse_tentatively (parser);
8715 /* Change `:' into `::'. */
8716 next_token_2->type = CPP_SCOPE;
8717 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
0cbd7506 8718 CPP_LESS. */
f4abade9
GB
8719 cp_lexer_consume_token (parser->lexer);
8720 /* Parse the arguments. */
8721 arguments = cp_parser_enclosed_template_argument_list (parser);
8722 if (!cp_parser_parse_definitely (parser))
8723 {
8724 /* If we couldn't parse an argument list, then we revert our changes
8725 and return simply an error. Maybe this is not a template-id
8726 after all. */
8727 next_token_2->type = CPP_COLON;
2a13a625 8728 cp_parser_error (parser, "expected %<<%>");
f4abade9
GB
8729 pop_deferring_access_checks ();
8730 return error_mark_node;
8731 }
8732 /* Otherwise, emit an error about the invalid digraph, but continue
0cbd7506 8733 parsing because we got our argument list. */
2a13a625
GDR
8734 pedwarn ("%<<::%> cannot begin a template-argument list");
8735 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8736 "between %<<%> and %<::%>");
f4abade9
GB
8737 if (!flag_permissive)
8738 {
8739 static bool hint;
8740 if (!hint)
8741 {
2a13a625 8742 inform ("(if you use -fpermissive G++ will accept your code)");
f4abade9
GB
8743 hint = true;
8744 }
8745 }
8746 }
8747 else
8748 {
8749 /* Look for the `<' that starts the template-argument-list. */
8750 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8751 {
8752 pop_deferring_access_checks ();
8753 return error_mark_node;
8754 }
8755 /* Parse the arguments. */
8756 arguments = cp_parser_enclosed_template_argument_list (parser);
cf22909c 8757 }
a723baf1
MM
8758
8759 /* Build a representation of the specialization. */
8760 if (TREE_CODE (template) == IDENTIFIER_NODE)
8761 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8762 else if (DECL_CLASS_TEMPLATE_P (template)
8763 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
84019f23
MM
8764 {
8765 bool entering_scope;
8766 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8767 template (rather than some instantiation thereof) only if
8768 is not nested within some other construct. For example, in
8769 "template <typename T> void f(T) { A<T>::", A<T> is just an
8770 instantiation of A. */
8771 entering_scope = (template_parm_scope_p ()
8772 && cp_lexer_next_token_is (parser->lexer,
8773 CPP_SCOPE));
8774 template_id
8775 = finish_template_type (template, arguments, entering_scope);
8776 }
a723baf1
MM
8777 else
8778 {
8779 /* If it's not a class-template or a template-template, it should be
8780 a function-template. */
50bc768d
NS
8781 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8782 || TREE_CODE (template) == OVERLOAD
8783 || BASELINK_P (template)));
21526606 8784
a723baf1
MM
8785 template_id = lookup_template_function (template, arguments);
8786 }
21526606 8787
cf22909c
KL
8788 /* Retrieve any deferred checks. Do not pop this access checks yet
8789 so the memory will not be reclaimed during token replacing below. */
8790 access_check = get_deferred_access_checks ();
8791
a723baf1
MM
8792 /* If parsing tentatively, replace the sequence of tokens that makes
8793 up the template-id with a CPP_TEMPLATE_ID token. That way,
8794 should we re-parse the token stream, we will not have to repeat
8795 the effort required to do the parse, nor will we issue duplicate
8796 error messages about problems during instantiation of the
e894ab29 8797 template. */
c8a7ed43 8798 if (start_of_id)
a723baf1 8799 {
0c5e4866 8800 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
c8094d83 8801
a723baf1
MM
8802 /* Reset the contents of the START_OF_ID token. */
8803 token->type = CPP_TEMPLATE_ID;
8804 token->value = build_tree_list (access_check, template_id);
8805 token->keyword = RID_MAX;
c8094d83 8806
a723baf1 8807 /* Purge all subsequent tokens. */
0c5e4866 8808 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
c8a7ed43
AO
8809
8810 /* ??? Can we actually assume that, if template_id ==
8811 error_mark_node, we will have issued a diagnostic to the
8812 user, as opposed to simply marking the tentative parse as
8813 failed? */
8814 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8815 error ("parse error in template argument list");
a723baf1
MM
8816 }
8817
cf22909c 8818 pop_deferring_access_checks ();
a723baf1
MM
8819 return template_id;
8820}
8821
8822/* Parse a template-name.
8823
8824 template-name:
8825 identifier
21526606 8826
a723baf1
MM
8827 The standard should actually say:
8828
8829 template-name:
8830 identifier
8831 operator-function-id
a723baf1
MM
8832
8833 A defect report has been filed about this issue.
8834
0d956474
GB
8835 A conversion-function-id cannot be a template name because they cannot
8836 be part of a template-id. In fact, looking at this code:
8837
8838 a.operator K<int>()
8839
8840 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
21526606 8841 It is impossible to call a templated conversion-function-id with an
0d956474
GB
8842 explicit argument list, since the only allowed template parameter is
8843 the type to which it is converting.
8844
a723baf1
MM
8845 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8846 `template' keyword, in a construction like:
8847
8848 T::template f<3>()
8849
8850 In that case `f' is taken to be a template-name, even though there
8851 is no way of knowing for sure.
8852
8853 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8854 name refers to a set of overloaded functions, at least one of which
8855 is a template, or an IDENTIFIER_NODE with the name of the template,
8856 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8857 names are looked up inside uninstantiated templates. */
8858
8859static tree
21526606 8860cp_parser_template_name (cp_parser* parser,
0cbd7506
MS
8861 bool template_keyword_p,
8862 bool check_dependency_p,
a668c6ad
MM
8863 bool is_declaration,
8864 bool *is_identifier)
a723baf1
MM
8865{
8866 tree identifier;
8867 tree decl;
8868 tree fns;
8869
8870 /* If the next token is `operator', then we have either an
8871 operator-function-id or a conversion-function-id. */
8872 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8873 {
8874 /* We don't know whether we're looking at an
8875 operator-function-id or a conversion-function-id. */
8876 cp_parser_parse_tentatively (parser);
8877 /* Try an operator-function-id. */
8878 identifier = cp_parser_operator_function_id (parser);
8879 /* If that didn't work, try a conversion-function-id. */
8880 if (!cp_parser_parse_definitely (parser))
0cbd7506 8881 {
0d956474
GB
8882 cp_parser_error (parser, "expected template-name");
8883 return error_mark_node;
0cbd7506 8884 }
a723baf1
MM
8885 }
8886 /* Look for the identifier. */
8887 else
8888 identifier = cp_parser_identifier (parser);
21526606 8889
a723baf1
MM
8890 /* If we didn't find an identifier, we don't have a template-id. */
8891 if (identifier == error_mark_node)
8892 return error_mark_node;
8893
8894 /* If the name immediately followed the `template' keyword, then it
8895 is a template-name. However, if the next token is not `<', then
8896 we do not treat it as a template-name, since it is not being used
8897 as part of a template-id. This enables us to handle constructs
8898 like:
8899
8900 template <typename T> struct S { S(); };
8901 template <typename T> S<T>::S();
8902
8903 correctly. We would treat `S' as a template -- if it were `S<T>'
8904 -- but we do not if there is no `<'. */
a668c6ad
MM
8905
8906 if (processing_template_decl
f4abade9 8907 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
a668c6ad
MM
8908 {
8909 /* In a declaration, in a dependent context, we pretend that the
8910 "template" keyword was present in order to improve error
8911 recovery. For example, given:
21526606 8912
a668c6ad 8913 template <typename T> void f(T::X<int>);
21526606 8914
a668c6ad 8915 we want to treat "X<int>" as a template-id. */
21526606
EC
8916 if (is_declaration
8917 && !template_keyword_p
a668c6ad 8918 && parser->scope && TYPE_P (parser->scope)
a52eb3bc 8919 && check_dependency_p
4e0f4df5
GB
8920 && dependent_type_p (parser->scope)
8921 /* Do not do this for dtors (or ctors), since they never
8922 need the template keyword before their name. */
8923 && !constructor_name_p (identifier, parser->scope))
a668c6ad 8924 {
0c5e4866 8925 cp_token_position start = 0;
c8094d83 8926
a668c6ad 8927 /* Explain what went wrong. */
2a13a625
GDR
8928 error ("non-template %qD used as template", identifier);
8929 inform ("use %<%T::template %D%> to indicate that it is a template",
4e0f4df5 8930 parser->scope, identifier);
0b16f8f4
VR
8931 /* If parsing tentatively, find the location of the "<" token. */
8932 if (cp_parser_simulate_error (parser))
8933 start = cp_lexer_token_position (parser->lexer, true);
a668c6ad
MM
8934 /* Parse the template arguments so that we can issue error
8935 messages about them. */
8936 cp_lexer_consume_token (parser->lexer);
8937 cp_parser_enclosed_template_argument_list (parser);
8938 /* Skip tokens until we find a good place from which to
8939 continue parsing. */
8940 cp_parser_skip_to_closing_parenthesis (parser,
8941 /*recovering=*/true,
8942 /*or_comma=*/true,
8943 /*consume_paren=*/false);
8944 /* If parsing tentatively, permanently remove the
8945 template argument list. That will prevent duplicate
8946 error messages from being issued about the missing
8947 "template" keyword. */
0c5e4866
NS
8948 if (start)
8949 cp_lexer_purge_tokens_after (parser->lexer, start);
a668c6ad
MM
8950 if (is_identifier)
8951 *is_identifier = true;
8952 return identifier;
8953 }
9d363a56
MM
8954
8955 /* If the "template" keyword is present, then there is generally
8956 no point in doing name-lookup, so we just return IDENTIFIER.
8957 But, if the qualifying scope is non-dependent then we can
8958 (and must) do name-lookup normally. */
8959 if (template_keyword_p
8960 && (!parser->scope
98ca843c 8961 || (TYPE_P (parser->scope)
9d363a56 8962 && dependent_type_p (parser->scope))))
a668c6ad
MM
8963 return identifier;
8964 }
a723baf1
MM
8965
8966 /* Look up the name. */
8967 decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 8968 none_type,
b0bc6e8e 8969 /*is_template=*/false,
eea9800f 8970 /*is_namespace=*/false,
8f78f01f 8971 check_dependency_p,
91b1ca65 8972 /*ambiguous_decls=*/NULL);
a723baf1
MM
8973 decl = maybe_get_template_decl_from_type_decl (decl);
8974
8975 /* If DECL is a template, then the name was a template-name. */
8976 if (TREE_CODE (decl) == TEMPLATE_DECL)
8977 ;
21526606 8978 else
a723baf1 8979 {
d58a2b83
MM
8980 tree fn = NULL_TREE;
8981
a723baf1
MM
8982 /* The standard does not explicitly indicate whether a name that
8983 names a set of overloaded declarations, some of which are
8984 templates, is a template-name. However, such a name should
8985 be a template-name; otherwise, there is no way to form a
8986 template-id for the overloaded templates. */
8987 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8988 if (TREE_CODE (fns) == OVERLOAD)
d58a2b83
MM
8989 for (fn = fns; fn; fn = OVL_NEXT (fn))
8990 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8991 break;
21526606 8992
d58a2b83 8993 if (!fn)
a723baf1 8994 {
d58a2b83 8995 /* The name does not name a template. */
a723baf1
MM
8996 cp_parser_error (parser, "expected template-name");
8997 return error_mark_node;
8998 }
8999 }
9000
9001 /* If DECL is dependent, and refers to a function, then just return
9002 its name; we will look it up again during template instantiation. */
9003 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9004 {
9005 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
1fb3244a 9006 if (TYPE_P (scope) && dependent_type_p (scope))
a723baf1
MM
9007 return identifier;
9008 }
9009
9010 return decl;
9011}
9012
9013/* Parse a template-argument-list.
9014
9015 template-argument-list:
9016 template-argument
9017 template-argument-list , template-argument
9018
04c06002 9019 Returns a TREE_VEC containing the arguments. */
a723baf1
MM
9020
9021static tree
94edc4ab 9022cp_parser_template_argument_list (cp_parser* parser)
a723baf1 9023{
bf12d54d
NS
9024 tree fixed_args[10];
9025 unsigned n_args = 0;
9026 unsigned alloced = 10;
9027 tree *arg_ary = fixed_args;
9028 tree vec;
4bb8ca28 9029 bool saved_in_template_argument_list_p;
5e9edb0f
MM
9030 bool saved_ice_p;
9031 bool saved_non_ice_p;
a723baf1 9032
4bb8ca28
MM
9033 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9034 parser->in_template_argument_list_p = true;
5e9edb0f 9035 /* Even if the template-id appears in an integral
3db45ab5
MS
9036 constant-expression, the contents of the argument list do
9037 not. */
5e9edb0f
MM
9038 saved_ice_p = parser->integral_constant_expression_p;
9039 parser->integral_constant_expression_p = false;
9040 saved_non_ice_p = parser->non_integral_constant_expression_p;
9041 parser->non_integral_constant_expression_p = false;
02ed62dd 9042 /* Parse the arguments. */
bf12d54d 9043 do
a723baf1
MM
9044 {
9045 tree argument;
9046
bf12d54d 9047 if (n_args)
04c06002 9048 /* Consume the comma. */
bf12d54d 9049 cp_lexer_consume_token (parser->lexer);
21526606 9050
a723baf1
MM
9051 /* Parse the template-argument. */
9052 argument = cp_parser_template_argument (parser);
bf12d54d
NS
9053 if (n_args == alloced)
9054 {
9055 alloced *= 2;
21526606 9056
bf12d54d
NS
9057 if (arg_ary == fixed_args)
9058 {
0ac1b889 9059 arg_ary = XNEWVEC (tree, alloced);
bf12d54d
NS
9060 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9061 }
9062 else
7767580e 9063 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
bf12d54d
NS
9064 }
9065 arg_ary[n_args++] = argument;
a723baf1 9066 }
bf12d54d
NS
9067 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9068
9069 vec = make_tree_vec (n_args);
a723baf1 9070
bf12d54d
NS
9071 while (n_args--)
9072 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
21526606 9073
bf12d54d
NS
9074 if (arg_ary != fixed_args)
9075 free (arg_ary);
5e9edb0f
MM
9076 parser->non_integral_constant_expression_p = saved_non_ice_p;
9077 parser->integral_constant_expression_p = saved_ice_p;
4bb8ca28 9078 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
bf12d54d 9079 return vec;
a723baf1
MM
9080}
9081
9082/* Parse a template-argument.
9083
9084 template-argument:
9085 assignment-expression
9086 type-id
9087 id-expression
9088
9089 The representation is that of an assignment-expression, type-id, or
9090 id-expression -- except that the qualified id-expression is
9091 evaluated, so that the value returned is either a DECL or an
21526606 9092 OVERLOAD.
d17811fd
MM
9093
9094 Although the standard says "assignment-expression", it forbids
9095 throw-expressions or assignments in the template argument.
9096 Therefore, we use "conditional-expression" instead. */
a723baf1
MM
9097
9098static tree
94edc4ab 9099cp_parser_template_argument (cp_parser* parser)
a723baf1
MM
9100{
9101 tree argument;
9102 bool template_p;
d17811fd 9103 bool address_p;
4d5297fa 9104 bool maybe_type_id = false;
d17811fd 9105 cp_token *token;
b3445994 9106 cp_id_kind idk;
a723baf1
MM
9107
9108 /* There's really no way to know what we're looking at, so we just
21526606 9109 try each alternative in order.
a723baf1
MM
9110
9111 [temp.arg]
9112
9113 In a template-argument, an ambiguity between a type-id and an
9114 expression is resolved to a type-id, regardless of the form of
21526606 9115 the corresponding template-parameter.
a723baf1
MM
9116
9117 Therefore, we try a type-id first. */
9118 cp_parser_parse_tentatively (parser);
a723baf1 9119 argument = cp_parser_type_id (parser);
4d5297fa 9120 /* If there was no error parsing the type-id but the next token is a '>>',
21526606 9121 we probably found a typo for '> >'. But there are type-id which are
4d5297fa
GB
9122 also valid expressions. For instance:
9123
9124 struct X { int operator >> (int); };
9125 template <int V> struct Foo {};
9126 Foo<X () >> 5> r;
9127
9128 Here 'X()' is a valid type-id of a function type, but the user just
9129 wanted to write the expression "X() >> 5". Thus, we remember that we
9130 found a valid type-id, but we still try to parse the argument as an
9131 expression to see what happens. */
9132 if (!cp_parser_error_occurred (parser)
9133 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9134 {
9135 maybe_type_id = true;
9136 cp_parser_abort_tentative_parse (parser);
9137 }
9138 else
9139 {
9140 /* If the next token isn't a `,' or a `>', then this argument wasn't
9141 really finished. This means that the argument is not a valid
9142 type-id. */
9143 if (!cp_parser_next_token_ends_template_argument_p (parser))
9144 cp_parser_error (parser, "expected template-argument");
9145 /* If that worked, we're done. */
9146 if (cp_parser_parse_definitely (parser))
9147 return argument;
9148 }
a723baf1
MM
9149 /* We're still not sure what the argument will be. */
9150 cp_parser_parse_tentatively (parser);
9151 /* Try a template. */
21526606 9152 argument = cp_parser_id_expression (parser,
a723baf1
MM
9153 /*template_keyword_p=*/false,
9154 /*check_dependency_p=*/true,
f3c2dfc6 9155 &template_p,
fa6098f8
MM
9156 /*declarator_p=*/false,
9157 /*optional_p=*/false);
a723baf1
MM
9158 /* If the next token isn't a `,' or a `>', then this argument wasn't
9159 really finished. */
d17811fd 9160 if (!cp_parser_next_token_ends_template_argument_p (parser))
a723baf1
MM
9161 cp_parser_error (parser, "expected template-argument");
9162 if (!cp_parser_error_occurred (parser))
9163 {
f746161e
MM
9164 /* Figure out what is being referred to. If the id-expression
9165 was for a class template specialization, then we will have a
9166 TYPE_DECL at this point. There is no need to do name lookup
9167 at this point in that case. */
9168 if (TREE_CODE (argument) != TYPE_DECL)
9169 argument = cp_parser_lookup_name (parser, argument,
fc6a28d7 9170 none_type,
f746161e
MM
9171 /*is_template=*/template_p,
9172 /*is_namespace=*/false,
8f78f01f 9173 /*check_dependency=*/true,
91b1ca65 9174 /*ambiguous_decls=*/NULL);
5b4acce1
KL
9175 if (TREE_CODE (argument) != TEMPLATE_DECL
9176 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
a723baf1
MM
9177 cp_parser_error (parser, "expected template-name");
9178 }
9179 if (cp_parser_parse_definitely (parser))
9180 return argument;
d17811fd
MM
9181 /* It must be a non-type argument. There permitted cases are given
9182 in [temp.arg.nontype]:
9183
9184 -- an integral constant-expression of integral or enumeration
0cbd7506 9185 type; or
d17811fd
MM
9186
9187 -- the name of a non-type template-parameter; or
9188
9189 -- the name of an object or function with external linkage...
9190
9191 -- the address of an object or function with external linkage...
9192
04c06002 9193 -- a pointer to member... */
d17811fd
MM
9194 /* Look for a non-type template parameter. */
9195 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9196 {
9197 cp_parser_parse_tentatively (parser);
9198 argument = cp_parser_primary_expression (parser,
02ed62dd 9199 /*adress_p=*/false,
93678513 9200 /*cast_p=*/false,
02ed62dd
MM
9201 /*template_arg_p=*/true,
9202 &idk);
d17811fd
MM
9203 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9204 || !cp_parser_next_token_ends_template_argument_p (parser))
9205 cp_parser_simulate_error (parser);
9206 if (cp_parser_parse_definitely (parser))
9207 return argument;
9208 }
db24eb1f 9209
d17811fd
MM
9210 /* If the next token is "&", the argument must be the address of an
9211 object or function with external linkage. */
9212 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9213 if (address_p)
9214 cp_lexer_consume_token (parser->lexer);
9215 /* See if we might have an id-expression. */
9216 token = cp_lexer_peek_token (parser->lexer);
9217 if (token->type == CPP_NAME
9218 || token->keyword == RID_OPERATOR
9219 || token->type == CPP_SCOPE
9220 || token->type == CPP_TEMPLATE_ID
9221 || token->type == CPP_NESTED_NAME_SPECIFIER)
9222 {
9223 cp_parser_parse_tentatively (parser);
9224 argument = cp_parser_primary_expression (parser,
02ed62dd 9225 address_p,
93678513 9226 /*cast_p=*/false,
02ed62dd
MM
9227 /*template_arg_p=*/true,
9228 &idk);
d17811fd
MM
9229 if (cp_parser_error_occurred (parser)
9230 || !cp_parser_next_token_ends_template_argument_p (parser))
9231 cp_parser_abort_tentative_parse (parser);
9232 else
9233 {
db24eb1f
NS
9234 if (TREE_CODE (argument) == INDIRECT_REF)
9235 {
9236 gcc_assert (REFERENCE_REF_P (argument));
9237 argument = TREE_OPERAND (argument, 0);
9238 }
c8094d83 9239
d17811fd
MM
9240 if (TREE_CODE (argument) == VAR_DECL)
9241 {
9242 /* A variable without external linkage might still be a
9243 valid constant-expression, so no error is issued here
9244 if the external-linkage check fails. */
9245 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9246 cp_parser_simulate_error (parser);
9247 }
9248 else if (is_overloaded_fn (argument))
9249 /* All overloaded functions are allowed; if the external
9250 linkage test does not pass, an error will be issued
9251 later. */
9252 ;
9253 else if (address_p
21526606 9254 && (TREE_CODE (argument) == OFFSET_REF
d17811fd
MM
9255 || TREE_CODE (argument) == SCOPE_REF))
9256 /* A pointer-to-member. */
9257 ;
db24eb1f
NS
9258 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9259 ;
d17811fd
MM
9260 else
9261 cp_parser_simulate_error (parser);
9262
9263 if (cp_parser_parse_definitely (parser))
9264 {
9265 if (address_p)
9266 argument = build_x_unary_op (ADDR_EXPR, argument);
9267 return argument;
9268 }
9269 }
9270 }
9271 /* If the argument started with "&", there are no other valid
9272 alternatives at this point. */
9273 if (address_p)
9274 {
9275 cp_parser_error (parser, "invalid non-type template argument");
9276 return error_mark_node;
9277 }
db24eb1f 9278
4d5297fa 9279 /* If the argument wasn't successfully parsed as a type-id followed
21526606 9280 by '>>', the argument can only be a constant expression now.
4d5297fa
GB
9281 Otherwise, we try parsing the constant-expression tentatively,
9282 because the argument could really be a type-id. */
9283 if (maybe_type_id)
9284 cp_parser_parse_tentatively (parser);
21526606 9285 argument = cp_parser_constant_expression (parser,
d17811fd
MM
9286 /*allow_non_constant_p=*/false,
9287 /*non_constant_p=*/NULL);
9baa27a9 9288 argument = fold_non_dependent_expr (argument);
4d5297fa
GB
9289 if (!maybe_type_id)
9290 return argument;
9291 if (!cp_parser_next_token_ends_template_argument_p (parser))
9292 cp_parser_error (parser, "expected template-argument");
9293 if (cp_parser_parse_definitely (parser))
9294 return argument;
9295 /* We did our best to parse the argument as a non type-id, but that
9296 was the only alternative that matched (albeit with a '>' after
21526606 9297 it). We can assume it's just a typo from the user, and a
4d5297fa
GB
9298 diagnostic will then be issued. */
9299 return cp_parser_type_id (parser);
a723baf1
MM
9300}
9301
9302/* Parse an explicit-instantiation.
9303
9304 explicit-instantiation:
21526606 9305 template declaration
a723baf1
MM
9306
9307 Although the standard says `declaration', what it really means is:
9308
9309 explicit-instantiation:
21526606 9310 template decl-specifier-seq [opt] declarator [opt] ;
a723baf1
MM
9311
9312 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9313 supposed to be allowed. A defect report has been filed about this
21526606 9314 issue.
a723baf1
MM
9315
9316 GNU Extension:
21526606 9317
a723baf1 9318 explicit-instantiation:
21526606 9319 storage-class-specifier template
a723baf1 9320 decl-specifier-seq [opt] declarator [opt] ;
21526606 9321 function-specifier template
a723baf1
MM
9322 decl-specifier-seq [opt] declarator [opt] ; */
9323
9324static void
94edc4ab 9325cp_parser_explicit_instantiation (cp_parser* parser)
a723baf1 9326{
560ad596 9327 int declares_class_or_enum;
62d1db17 9328 cp_decl_specifier_seq decl_specifiers;
a723baf1
MM
9329 tree extension_specifier = NULL_TREE;
9330
9331 /* Look for an (optional) storage-class-specifier or
9332 function-specifier. */
9333 if (cp_parser_allow_gnu_extensions_p (parser))
9334 {
21526606 9335 extension_specifier
a723baf1
MM
9336 = cp_parser_storage_class_specifier_opt (parser);
9337 if (!extension_specifier)
98ca843c 9338 extension_specifier
62d1db17
MM
9339 = cp_parser_function_specifier_opt (parser,
9340 /*decl_specs=*/NULL);
a723baf1
MM
9341 }
9342
9343 /* Look for the `template' keyword. */
9344 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9345 /* Let the front end know that we are processing an explicit
9346 instantiation. */
9347 begin_explicit_instantiation ();
9348 /* [temp.explicit] says that we are supposed to ignore access
9349 control while processing explicit instantiation directives. */
78757caa 9350 push_deferring_access_checks (dk_no_check);
a723baf1 9351 /* Parse a decl-specifier-seq. */
62d1db17
MM
9352 cp_parser_decl_specifier_seq (parser,
9353 CP_PARSER_FLAGS_OPTIONAL,
9354 &decl_specifiers,
9355 &declares_class_or_enum);
a723baf1
MM
9356 /* If there was exactly one decl-specifier, and it declared a class,
9357 and there's no declarator, then we have an explicit type
9358 instantiation. */
9359 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9360 {
9361 tree type;
9362
62d1db17 9363 type = check_tag_decl (&decl_specifiers);
b7fc8b57
KL
9364 /* Turn access control back on for names used during
9365 template instantiation. */
9366 pop_deferring_access_checks ();
a723baf1 9367 if (type)
8da15291 9368 do_type_instantiation (type, extension_specifier,
3db45ab5 9369 /*complain=*/tf_error);
a723baf1
MM
9370 }
9371 else
9372 {
058b15c1 9373 cp_declarator *declarator;
a723baf1
MM
9374 tree decl;
9375
9376 /* Parse the declarator. */
21526606 9377 declarator
62b8a44e 9378 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 9379 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
9380 /*parenthesized_p=*/NULL,
9381 /*member_p=*/false);
fc6a28d7
MM
9382 if (declares_class_or_enum & 2)
9383 cp_parser_check_for_definition_in_return_type (declarator,
9384 decl_specifiers.type);
058b15c1 9385 if (declarator != cp_error_declarator)
216bb6e1 9386 {
62d1db17 9387 decl = grokdeclarator (declarator, &decl_specifiers,
b9e75696 9388 NORMAL, 0, &decl_specifiers.attributes);
216bb6e1
MM
9389 /* Turn access control back on for names used during
9390 template instantiation. */
9391 pop_deferring_access_checks ();
9392 /* Do the explicit instantiation. */
9393 do_decl_instantiation (decl, extension_specifier);
9394 }
9395 else
9396 {
9397 pop_deferring_access_checks ();
9398 /* Skip the body of the explicit instantiation. */
9399 cp_parser_skip_to_end_of_statement (parser);
9400 }
a723baf1
MM
9401 }
9402 /* We're done with the instantiation. */
9403 end_explicit_instantiation ();
a723baf1 9404
e0860732 9405 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1
MM
9406}
9407
9408/* Parse an explicit-specialization.
9409
9410 explicit-specialization:
21526606 9411 template < > declaration
a723baf1
MM
9412
9413 Although the standard says `declaration', what it really means is:
9414
9415 explicit-specialization:
9416 template <> decl-specifier [opt] init-declarator [opt] ;
21526606 9417 template <> function-definition
a723baf1
MM
9418 template <> explicit-specialization
9419 template <> template-declaration */
9420
9421static void
94edc4ab 9422cp_parser_explicit_specialization (cp_parser* parser)
a723baf1 9423{
2f1b1731 9424 bool need_lang_pop;
a723baf1
MM
9425 /* Look for the `template' keyword. */
9426 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9427 /* Look for the `<'. */
9428 cp_parser_require (parser, CPP_LESS, "`<'");
9429 /* Look for the `>'. */
9430 cp_parser_require (parser, CPP_GREATER, "`>'");
9431 /* We have processed another parameter list. */
9432 ++parser->num_template_parameter_lists;
2f1b1731 9433 /* [temp]
3db45ab5 9434
2f1b1731 9435 A template ... explicit specialization ... shall not have C
3db45ab5 9436 linkage. */
2f1b1731
MM
9437 if (current_lang_name == lang_name_c)
9438 {
9439 error ("template specialization with C linkage");
9440 /* Give it C++ linkage to avoid confusing other parts of the
9441 front end. */
9442 push_lang_context (lang_name_cplusplus);
9443 need_lang_pop = true;
9444 }
9445 else
9446 need_lang_pop = false;
a723baf1
MM
9447 /* Let the front end know that we are beginning a specialization. */
9448 begin_specialization ();
a723baf1
MM
9449 /* If the next keyword is `template', we need to figure out whether
9450 or not we're looking a template-declaration. */
9451 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9452 {
9453 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9454 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9455 cp_parser_template_declaration_after_export (parser,
9456 /*member_p=*/false);
9457 else
9458 cp_parser_explicit_specialization (parser);
9459 }
9460 else
9461 /* Parse the dependent declaration. */
21526606 9462 cp_parser_single_declaration (parser,
6b648482 9463 /*checks=*/NULL_TREE,
a723baf1
MM
9464 /*member_p=*/false,
9465 /*friend_p=*/NULL);
a723baf1
MM
9466 /* We're done with the specialization. */
9467 end_specialization ();
2f1b1731
MM
9468 /* For the erroneous case of a template with C linkage, we pushed an
9469 implicit C++ linkage scope; exit that scope now. */
9470 if (need_lang_pop)
9471 pop_lang_context ();
a723baf1
MM
9472 /* We're done with this parameter list. */
9473 --parser->num_template_parameter_lists;
9474}
9475
9476/* Parse a type-specifier.
9477
9478 type-specifier:
9479 simple-type-specifier
9480 class-specifier
9481 enum-specifier
9482 elaborated-type-specifier
9483 cv-qualifier
9484
9485 GNU Extension:
9486
9487 type-specifier:
9488 __complex__
9489
62d1db17
MM
9490 Returns a representation of the type-specifier. For a
9491 class-specifier, enum-specifier, or elaborated-type-specifier, a
9492 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
a723baf1 9493
eb1aef53
KL
9494 The parser flags FLAGS is used to control type-specifier parsing.
9495
9496 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9497 in a decl-specifier-seq.
a723baf1
MM
9498
9499 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9500 class-specifier, enum-specifier, or elaborated-type-specifier, then
83a00410 9501 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
560ad596
MM
9502 if a type is declared; 2 if it is defined. Otherwise, it is set to
9503 zero.
a723baf1
MM
9504
9505 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9506 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9507 is set to FALSE. */
9508
9509static tree
21526606
EC
9510cp_parser_type_specifier (cp_parser* parser,
9511 cp_parser_flags flags,
62d1db17 9512 cp_decl_specifier_seq *decl_specs,
94edc4ab 9513 bool is_declaration,
560ad596 9514 int* declares_class_or_enum,
94edc4ab 9515 bool* is_cv_qualifier)
a723baf1
MM
9516{
9517 tree type_spec = NULL_TREE;
9518 cp_token *token;
9519 enum rid keyword;
62d1db17 9520 cp_decl_spec ds = ds_last;
a723baf1
MM
9521
9522 /* Assume this type-specifier does not declare a new type. */
9523 if (declares_class_or_enum)
543ca912 9524 *declares_class_or_enum = 0;
a723baf1
MM
9525 /* And that it does not specify a cv-qualifier. */
9526 if (is_cv_qualifier)
9527 *is_cv_qualifier = false;
9528 /* Peek at the next token. */
9529 token = cp_lexer_peek_token (parser->lexer);
9530
9531 /* If we're looking at a keyword, we can use that to guide the
9532 production we choose. */
9533 keyword = token->keyword;
9534 switch (keyword)
9535 {
ff4eb0b5 9536 case RID_ENUM:
b9e75696
JM
9537 /* Look for the enum-specifier. */
9538 type_spec = cp_parser_enum_specifier (parser);
9539 /* If that worked, we're done. */
9540 if (type_spec)
ff4eb0b5 9541 {
ff4eb0b5
ZW
9542 if (declares_class_or_enum)
9543 *declares_class_or_enum = 2;
9544 if (decl_specs)
9545 cp_parser_set_decl_spec_type (decl_specs,
9546 type_spec,
9547 /*user_defined_p=*/true);
9548 return type_spec;
9549 }
9550 else
9551 goto elaborated_type_specifier;
9552
a723baf1
MM
9553 /* Any of these indicate either a class-specifier, or an
9554 elaborated-type-specifier. */
9555 case RID_CLASS:
9556 case RID_STRUCT:
9557 case RID_UNION:
a723baf1 9558 /* Parse tentatively so that we can back up if we don't find a
ff4eb0b5 9559 class-specifier. */
a723baf1 9560 cp_parser_parse_tentatively (parser);
ff4eb0b5
ZW
9561 /* Look for the class-specifier. */
9562 type_spec = cp_parser_class_specifier (parser);
a723baf1
MM
9563 /* If that worked, we're done. */
9564 if (cp_parser_parse_definitely (parser))
9565 {
9566 if (declares_class_or_enum)
560ad596 9567 *declares_class_or_enum = 2;
62d1db17
MM
9568 if (decl_specs)
9569 cp_parser_set_decl_spec_type (decl_specs,
9570 type_spec,
9571 /*user_defined_p=*/true);
a723baf1
MM
9572 return type_spec;
9573 }
9574
9575 /* Fall through. */
ff4eb0b5
ZW
9576 elaborated_type_specifier:
9577 /* We're declaring (not defining) a class or enum. */
9578 if (declares_class_or_enum)
9579 *declares_class_or_enum = 1;
a723baf1 9580
ff4eb0b5 9581 /* Fall through. */
a723baf1
MM
9582 case RID_TYPENAME:
9583 /* Look for an elaborated-type-specifier. */
98ca843c
EC
9584 type_spec
9585 = (cp_parser_elaborated_type_specifier
62d1db17
MM
9586 (parser,
9587 decl_specs && decl_specs->specs[(int) ds_friend],
9588 is_declaration));
62d1db17
MM
9589 if (decl_specs)
9590 cp_parser_set_decl_spec_type (decl_specs,
9591 type_spec,
9592 /*user_defined_p=*/true);
a723baf1
MM
9593 return type_spec;
9594
9595 case RID_CONST:
62d1db17
MM
9596 ds = ds_const;
9597 if (is_cv_qualifier)
9598 *is_cv_qualifier = true;
9599 break;
98ca843c 9600
a723baf1 9601 case RID_VOLATILE:
62d1db17 9602 ds = ds_volatile;
a723baf1
MM
9603 if (is_cv_qualifier)
9604 *is_cv_qualifier = true;
62d1db17 9605 break;
a723baf1 9606
62d1db17
MM
9607 case RID_RESTRICT:
9608 ds = ds_restrict;
9609 if (is_cv_qualifier)
9610 *is_cv_qualifier = true;
9611 break;
a723baf1
MM
9612
9613 case RID_COMPLEX:
9614 /* The `__complex__' keyword is a GNU extension. */
62d1db17
MM
9615 ds = ds_complex;
9616 break;
a723baf1
MM
9617
9618 default:
9619 break;
9620 }
9621
62d1db17
MM
9622 /* Handle simple keywords. */
9623 if (ds != ds_last)
9624 {
9625 if (decl_specs)
9626 {
9627 ++decl_specs->specs[(int)ds];
9628 decl_specs->any_specifiers_p = true;
9629 }
9630 return cp_lexer_consume_token (parser->lexer)->value;
9631 }
9632
a723baf1
MM
9633 /* If we do not already have a type-specifier, assume we are looking
9634 at a simple-type-specifier. */
98ca843c 9635 type_spec = cp_parser_simple_type_specifier (parser,
62d1db17
MM
9636 decl_specs,
9637 flags);
a723baf1
MM
9638
9639 /* If we didn't find a type-specifier, and a type-specifier was not
9640 optional in this context, issue an error message. */
9641 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9642 {
9643 cp_parser_error (parser, "expected type specifier");
9644 return error_mark_node;
9645 }
9646
9647 return type_spec;
9648}
9649
9650/* Parse a simple-type-specifier.
9651
9652 simple-type-specifier:
9653 :: [opt] nested-name-specifier [opt] type-name
9654 :: [opt] nested-name-specifier template template-id
9655 char
9656 wchar_t
9657 bool
9658 short
9659 int
9660 long
9661 signed
9662 unsigned
9663 float
9664 double
21526606 9665 void
a723baf1
MM
9666
9667 GNU Extension:
9668
9669 simple-type-specifier:
9670 __typeof__ unary-expression
9671 __typeof__ ( type-id )
9672
62d1db17
MM
9673 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9674 appropriately updated. */
a723baf1
MM
9675
9676static tree
98ca843c 9677cp_parser_simple_type_specifier (cp_parser* parser,
62d1db17
MM
9678 cp_decl_specifier_seq *decl_specs,
9679 cp_parser_flags flags)
a723baf1
MM
9680{
9681 tree type = NULL_TREE;
9682 cp_token *token;
9683
9684 /* Peek at the next token. */
9685 token = cp_lexer_peek_token (parser->lexer);
9686
9687 /* If we're looking at a keyword, things are easy. */
9688 switch (token->keyword)
9689 {
9690 case RID_CHAR:
62d1db17
MM
9691 if (decl_specs)
9692 decl_specs->explicit_char_p = true;
4b0d3cbe
MM
9693 type = char_type_node;
9694 break;
a723baf1 9695 case RID_WCHAR:
4b0d3cbe
MM
9696 type = wchar_type_node;
9697 break;
a723baf1 9698 case RID_BOOL:
4b0d3cbe
MM
9699 type = boolean_type_node;
9700 break;
a723baf1 9701 case RID_SHORT:
62d1db17
MM
9702 if (decl_specs)
9703 ++decl_specs->specs[(int) ds_short];
4b0d3cbe
MM
9704 type = short_integer_type_node;
9705 break;
a723baf1 9706 case RID_INT:
62d1db17
MM
9707 if (decl_specs)
9708 decl_specs->explicit_int_p = true;
4b0d3cbe
MM
9709 type = integer_type_node;
9710 break;
a723baf1 9711 case RID_LONG:
62d1db17
MM
9712 if (decl_specs)
9713 ++decl_specs->specs[(int) ds_long];
4b0d3cbe
MM
9714 type = long_integer_type_node;
9715 break;
a723baf1 9716 case RID_SIGNED:
62d1db17
MM
9717 if (decl_specs)
9718 ++decl_specs->specs[(int) ds_signed];
4b0d3cbe
MM
9719 type = integer_type_node;
9720 break;
a723baf1 9721 case RID_UNSIGNED:
62d1db17
MM
9722 if (decl_specs)
9723 ++decl_specs->specs[(int) ds_unsigned];
4b0d3cbe
MM
9724 type = unsigned_type_node;
9725 break;
a723baf1 9726 case RID_FLOAT:
4b0d3cbe
MM
9727 type = float_type_node;
9728 break;
a723baf1 9729 case RID_DOUBLE:
4b0d3cbe
MM
9730 type = double_type_node;
9731 break;
a723baf1 9732 case RID_VOID:
4b0d3cbe
MM
9733 type = void_type_node;
9734 break;
a723baf1
MM
9735
9736 case RID_TYPEOF:
62d1db17
MM
9737 /* Consume the `typeof' token. */
9738 cp_lexer_consume_token (parser->lexer);
9739 /* Parse the operand to `typeof'. */
9740 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9741 /* If it is not already a TYPE, take its type. */
9742 if (!TYPE_P (type))
9743 type = finish_typeof (type);
9744
9745 if (decl_specs)
9746 cp_parser_set_decl_spec_type (decl_specs, type,
9747 /*user_defined_p=*/true);
98ca843c 9748
62d1db17 9749 return type;
a723baf1
MM
9750
9751 default:
9752 break;
9753 }
9754
4b0d3cbe
MM
9755 /* If the type-specifier was for a built-in type, we're done. */
9756 if (type)
9757 {
9758 tree id;
9759
62d1db17
MM
9760 /* Record the type. */
9761 if (decl_specs
9762 && (token->keyword != RID_SIGNED
9763 && token->keyword != RID_UNSIGNED
9764 && token->keyword != RID_SHORT
9765 && token->keyword != RID_LONG))
98ca843c 9766 cp_parser_set_decl_spec_type (decl_specs,
62d1db17
MM
9767 type,
9768 /*user_defined=*/false);
9769 if (decl_specs)
9770 decl_specs->any_specifiers_p = true;
9771
4b0d3cbe
MM
9772 /* Consume the token. */
9773 id = cp_lexer_consume_token (parser->lexer)->value;
0d956474
GB
9774
9775 /* There is no valid C++ program where a non-template type is
9776 followed by a "<". That usually indicates that the user thought
9777 that the type was a template. */
9778 cp_parser_check_for_invalid_template_id (parser, type);
9779
62d1db17 9780 return TYPE_NAME (type);
4b0d3cbe
MM
9781 }
9782
a723baf1 9783 /* The type-specifier must be a user-defined type. */
21526606 9784 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
a723baf1 9785 {
0c1a1ecd 9786 bool qualified_p;
f68e4dc8 9787 bool global_p;
0c1a1ecd 9788
a723baf1
MM
9789 /* Don't gobble tokens or issue error messages if this is an
9790 optional type-specifier. */
9791 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9792 cp_parser_parse_tentatively (parser);
9793
9794 /* Look for the optional `::' operator. */
f68e4dc8 9795 global_p
da740453
MM
9796 = (cp_parser_global_scope_opt (parser,
9797 /*current_scope_valid_p=*/false)
9798 != NULL_TREE);
a723baf1 9799 /* Look for the nested-name specifier. */
0c1a1ecd
MM
9800 qualified_p
9801 = (cp_parser_nested_name_specifier_opt (parser,
9802 /*typename_keyword_p=*/false,
9803 /*check_dependency_p=*/true,
9804 /*type_p=*/false,
6661a85f
EB
9805 /*is_declaration=*/false)
9806 != NULL_TREE);
a723baf1
MM
9807 /* If we have seen a nested-name-specifier, and the next token
9808 is `template', then we are using the template-id production. */
21526606 9809 if (parser->scope
a723baf1
MM
9810 && cp_parser_optional_template_keyword (parser))
9811 {
9812 /* Look for the template-id. */
21526606 9813 type = cp_parser_template_id (parser,
a723baf1 9814 /*template_keyword_p=*/true,
a668c6ad
MM
9815 /*check_dependency_p=*/true,
9816 /*is_declaration=*/false);
a723baf1
MM
9817 /* If the template-id did not name a type, we are out of
9818 luck. */
9819 if (TREE_CODE (type) != TYPE_DECL)
9820 {
9821 cp_parser_error (parser, "expected template-id for type");
9822 type = NULL_TREE;
9823 }
9824 }
9825 /* Otherwise, look for a type-name. */
9826 else
4bb8ca28 9827 type = cp_parser_type_name (parser);
0c1a1ecd 9828 /* Keep track of all name-lookups performed in class scopes. */
98ca843c 9829 if (type
f68e4dc8 9830 && !global_p
0c1a1ecd 9831 && !qualified_p
98ca843c 9832 && TREE_CODE (type) == TYPE_DECL
0c1a1ecd
MM
9833 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9834 maybe_note_name_used_in_class (DECL_NAME (type), type);
a723baf1 9835 /* If it didn't work out, we don't have a TYPE. */
21526606 9836 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
a723baf1
MM
9837 && !cp_parser_parse_definitely (parser))
9838 type = NULL_TREE;
62d1db17
MM
9839 if (type && decl_specs)
9840 cp_parser_set_decl_spec_type (decl_specs, type,
9841 /*user_defined=*/true);
a723baf1
MM
9842 }
9843
9844 /* If we didn't get a type-name, issue an error message. */
9845 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9846 {
9847 cp_parser_error (parser, "expected type-name");
9848 return error_mark_node;
9849 }
9850
a668c6ad
MM
9851 /* There is no valid C++ program where a non-template type is
9852 followed by a "<". That usually indicates that the user thought
9853 that the type was a template. */
4bb8ca28 9854 if (type && type != error_mark_node)
e58a9aa1
ZL
9855 {
9856 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9857 If it is, then the '<'...'>' enclose protocol names rather than
9858 template arguments, and so everything is fine. */
9859 if (c_dialect_objc ()
9860 && (objc_is_id (type) || objc_is_class_name (type)))
9861 {
9862 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9863 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9864
9865 /* Clobber the "unqualified" type previously entered into
128a79fb 9866 DECL_SPECS with the new, improved protocol-qualified version. */
e58a9aa1
ZL
9867 if (decl_specs)
9868 decl_specs->type = qual_type;
9869
9870 return qual_type;
9871 }
9872
9873 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
c8094d83 9874 }
ec75414f 9875
a723baf1
MM
9876 return type;
9877}
9878
9879/* Parse a type-name.
9880
9881 type-name:
9882 class-name
9883 enum-name
21526606 9884 typedef-name
a723baf1
MM
9885
9886 enum-name:
9887 identifier
9888
9889 typedef-name:
21526606 9890 identifier
a723baf1 9891
78dcd41a 9892 Returns a TYPE_DECL for the type. */
a723baf1
MM
9893
9894static tree
94edc4ab 9895cp_parser_type_name (cp_parser* parser)
a723baf1
MM
9896{
9897 tree type_decl;
9898 tree identifier;
9899
9900 /* We can't know yet whether it is a class-name or not. */
9901 cp_parser_parse_tentatively (parser);
9902 /* Try a class-name. */
21526606 9903 type_decl = cp_parser_class_name (parser,
a723baf1
MM
9904 /*typename_keyword_p=*/false,
9905 /*template_keyword_p=*/false,
fc6a28d7 9906 none_type,
a723baf1 9907 /*check_dependency_p=*/true,
a668c6ad
MM
9908 /*class_head_p=*/false,
9909 /*is_declaration=*/false);
a723baf1
MM
9910 /* If it's not a class-name, keep looking. */
9911 if (!cp_parser_parse_definitely (parser))
9912 {
9913 /* It must be a typedef-name or an enum-name. */
9914 identifier = cp_parser_identifier (parser);
9915 if (identifier == error_mark_node)
9916 return error_mark_node;
21526606 9917
a723baf1
MM
9918 /* Look up the type-name. */
9919 type_decl = cp_parser_lookup_name_simple (parser, identifier);
e58a9aa1
ZL
9920
9921 if (TREE_CODE (type_decl) != TYPE_DECL
9922 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9923 {
9924 /* See if this is an Objective-C type. */
9925 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9926 tree type = objc_get_protocol_qualified_type (identifier, protos);
c8094d83 9927 if (type)
e58a9aa1
ZL
9928 type_decl = TYPE_NAME (type);
9929 }
9930
a723baf1
MM
9931 /* Issue an error if we did not find a type-name. */
9932 if (TREE_CODE (type_decl) != TYPE_DECL)
9933 {
4bb8ca28 9934 if (!cp_parser_simulate_error (parser))
21526606 9935 cp_parser_name_lookup_error (parser, identifier, type_decl,
4bb8ca28 9936 "is not a type");
a723baf1
MM
9937 type_decl = error_mark_node;
9938 }
9939 /* Remember that the name was used in the definition of the
9940 current class so that we can check later to see if the
9941 meaning would have been different after the class was
9942 entirely defined. */
9943 else if (type_decl != error_mark_node
9944 && !parser->scope)
9945 maybe_note_name_used_in_class (identifier, type_decl);
9946 }
21526606 9947
a723baf1
MM
9948 return type_decl;
9949}
9950
9951
9952/* Parse an elaborated-type-specifier. Note that the grammar given
9953 here incorporates the resolution to DR68.
9954
9955 elaborated-type-specifier:
9956 class-key :: [opt] nested-name-specifier [opt] identifier
9957 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9958 enum :: [opt] nested-name-specifier [opt] identifier
9959 typename :: [opt] nested-name-specifier identifier
21526606
EC
9960 typename :: [opt] nested-name-specifier template [opt]
9961 template-id
a723baf1 9962
360d1b99
MM
9963 GNU extension:
9964
9965 elaborated-type-specifier:
9966 class-key attributes :: [opt] nested-name-specifier [opt] identifier
21526606 9967 class-key attributes :: [opt] nested-name-specifier [opt]
0cbd7506 9968 template [opt] template-id
360d1b99
MM
9969 enum attributes :: [opt] nested-name-specifier [opt] identifier
9970
a723baf1
MM
9971 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9972 declared `friend'. If IS_DECLARATION is TRUE, then this
9973 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9974 something is being declared.
9975
9976 Returns the TYPE specified. */
9977
9978static tree
21526606 9979cp_parser_elaborated_type_specifier (cp_parser* parser,
0cbd7506
MS
9980 bool is_friend,
9981 bool is_declaration)
a723baf1
MM
9982{
9983 enum tag_types tag_type;
9984 tree identifier;
9985 tree type = NULL_TREE;
360d1b99 9986 tree attributes = NULL_TREE;
a723baf1
MM
9987
9988 /* See if we're looking at the `enum' keyword. */
9989 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9990 {
9991 /* Consume the `enum' token. */
9992 cp_lexer_consume_token (parser->lexer);
9993 /* Remember that it's an enumeration type. */
9994 tag_type = enum_type;
360d1b99
MM
9995 /* Parse the attributes. */
9996 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
9997 }
9998 /* Or, it might be `typename'. */
9999 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10000 RID_TYPENAME))
10001 {
10002 /* Consume the `typename' token. */
10003 cp_lexer_consume_token (parser->lexer);
10004 /* Remember that it's a `typename' type. */
10005 tag_type = typename_type;
10006 /* The `typename' keyword is only allowed in templates. */
10007 if (!processing_template_decl)
2a13a625 10008 pedwarn ("using %<typename%> outside of template");
a723baf1
MM
10009 }
10010 /* Otherwise it must be a class-key. */
10011 else
10012 {
10013 tag_type = cp_parser_class_key (parser);
10014 if (tag_type == none_type)
10015 return error_mark_node;
360d1b99
MM
10016 /* Parse the attributes. */
10017 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
10018 }
10019
10020 /* Look for the `::' operator. */
21526606 10021 cp_parser_global_scope_opt (parser,
a723baf1
MM
10022 /*current_scope_valid_p=*/false);
10023 /* Look for the nested-name-specifier. */
10024 if (tag_type == typename_type)
8fa1ad0e 10025 {
8fe4d24b 10026 if (!cp_parser_nested_name_specifier (parser,
8fa1ad0e
MM
10027 /*typename_keyword_p=*/true,
10028 /*check_dependency_p=*/true,
a668c6ad 10029 /*type_p=*/true,
8fe4d24b 10030 is_declaration))
8fa1ad0e
MM
10031 return error_mark_node;
10032 }
a723baf1
MM
10033 else
10034 /* Even though `typename' is not present, the proposed resolution
10035 to Core Issue 180 says that in `class A<T>::B', `B' should be
10036 considered a type-name, even if `A<T>' is dependent. */
10037 cp_parser_nested_name_specifier_opt (parser,
10038 /*typename_keyword_p=*/true,
10039 /*check_dependency_p=*/true,
a668c6ad
MM
10040 /*type_p=*/true,
10041 is_declaration);
a723baf1 10042 /* For everything but enumeration types, consider a template-id. */
b9e75696 10043 /* For an enumeration type, consider only a plain identifier. */
a723baf1
MM
10044 if (tag_type != enum_type)
10045 {
10046 bool template_p = false;
10047 tree decl;
10048
10049 /* Allow the `template' keyword. */
10050 template_p = cp_parser_optional_template_keyword (parser);
10051 /* If we didn't see `template', we don't know if there's a
0cbd7506 10052 template-id or not. */
a723baf1
MM
10053 if (!template_p)
10054 cp_parser_parse_tentatively (parser);
10055 /* Parse the template-id. */
10056 decl = cp_parser_template_id (parser, template_p,
a668c6ad
MM
10057 /*check_dependency_p=*/true,
10058 is_declaration);
a723baf1 10059 /* If we didn't find a template-id, look for an ordinary
0cbd7506 10060 identifier. */
a723baf1
MM
10061 if (!template_p && !cp_parser_parse_definitely (parser))
10062 ;
10063 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10064 in effect, then we must assume that, upon instantiation, the
10065 template will correspond to a class. */
10066 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10067 && tag_type == typename_type)
10068 type = make_typename_type (parser->scope, decl,
fc6a28d7 10069 typename_type,
8da15291 10070 /*complain=*/tf_error);
21526606 10071 else
a723baf1
MM
10072 type = TREE_TYPE (decl);
10073 }
10074
a723baf1
MM
10075 if (!type)
10076 {
10077 identifier = cp_parser_identifier (parser);
10078
10079 if (identifier == error_mark_node)
eb5abb39
NS
10080 {
10081 parser->scope = NULL_TREE;
10082 return error_mark_node;
10083 }
a723baf1
MM
10084
10085 /* For a `typename', we needn't call xref_tag. */
c8094d83 10086 if (tag_type == typename_type
0c88d886 10087 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
21526606 10088 return cp_parser_make_typename_type (parser, parser->scope,
2097b5f2 10089 identifier);
a723baf1
MM
10090 /* Look up a qualified name in the usual way. */
10091 if (parser->scope)
10092 {
10093 tree decl;
10094
21526606 10095 decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 10096 tag_type,
b0bc6e8e 10097 /*is_template=*/false,
eea9800f 10098 /*is_namespace=*/false,
8f78f01f 10099 /*check_dependency=*/true,
91b1ca65 10100 /*ambiguous_decls=*/NULL);
710b73e6
KL
10101
10102 /* If we are parsing friend declaration, DECL may be a
10103 TEMPLATE_DECL tree node here. However, we need to check
10104 whether this TEMPLATE_DECL results in valid code. Consider
10105 the following example:
10106
10107 namespace N {
10108 template <class T> class C {};
10109 }
10110 class X {
10111 template <class T> friend class N::C; // #1, valid code
10112 };
10113 template <class T> class Y {
10114 friend class N::C; // #2, invalid code
10115 };
10116
10117 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10118 name lookup of `N::C'. We see that friend declaration must
10119 be template for the code to be valid. Note that
10120 processing_template_decl does not work here since it is
10121 always 1 for the above two cases. */
10122
21526606 10123 decl = (cp_parser_maybe_treat_template_as_class
710b73e6
KL
10124 (decl, /*tag_name_p=*/is_friend
10125 && parser->num_template_parameter_lists));
a723baf1
MM
10126
10127 if (TREE_CODE (decl) != TYPE_DECL)
10128 {
c8094d83 10129 cp_parser_diagnose_invalid_type_name (parser,
0c88d886
MM
10130 parser->scope,
10131 identifier);
a723baf1
MM
10132 return error_mark_node;
10133 }
560ad596
MM
10134
10135 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
21526606 10136 check_elaborated_type_specifier
4b0d3cbe 10137 (tag_type, decl,
560ad596
MM
10138 (parser->num_template_parameter_lists
10139 || DECL_SELF_REFERENCE_P (decl)));
a723baf1
MM
10140
10141 type = TREE_TYPE (decl);
10142 }
21526606 10143 else
a723baf1
MM
10144 {
10145 /* An elaborated-type-specifier sometimes introduces a new type and
10146 sometimes names an existing type. Normally, the rule is that it
10147 introduces a new type only if there is not an existing type of
10148 the same name already in scope. For example, given:
10149
10150 struct S {};
10151 void f() { struct S s; }
10152
10153 the `struct S' in the body of `f' is the same `struct S' as in
10154 the global scope; the existing definition is used. However, if
21526606 10155 there were no global declaration, this would introduce a new
a723baf1
MM
10156 local class named `S'.
10157
10158 An exception to this rule applies to the following code:
10159
10160 namespace N { struct S; }
10161
10162 Here, the elaborated-type-specifier names a new type
10163 unconditionally; even if there is already an `S' in the
10164 containing scope this declaration names a new type.
10165 This exception only applies if the elaborated-type-specifier
10166 forms the complete declaration:
10167
21526606 10168 [class.name]
a723baf1
MM
10169
10170 A declaration consisting solely of `class-key identifier ;' is
10171 either a redeclaration of the name in the current scope or a
10172 forward declaration of the identifier as a class name. It
10173 introduces the name into the current scope.
10174
10175 We are in this situation precisely when the next token is a `;'.
10176
10177 An exception to the exception is that a `friend' declaration does
10178 *not* name a new type; i.e., given:
10179
10180 struct S { friend struct T; };
10181
21526606 10182 `T' is not a new type in the scope of `S'.
a723baf1
MM
10183
10184 Also, `new struct S' or `sizeof (struct S)' never results in the
10185 definition of a new type; a new type can only be declared in a
9bcb9aae 10186 declaration context. */
a723baf1 10187
29ef83de 10188 tag_scope ts;
ca85f659
MM
10189 bool template_p;
10190
29ef83de
KL
10191 if (is_friend)
10192 /* Friends have special name lookup rules. */
10193 ts = ts_within_enclosing_non_class;
10194 else if (is_declaration
10195 && cp_lexer_next_token_is (parser->lexer,
10196 CPP_SEMICOLON))
10197 /* This is a `class-key identifier ;' */
10198 ts = ts_current;
10199 else
10200 ts = ts_global;
10201
3db45ab5 10202 template_p =
ca85f659
MM
10203 (parser->num_template_parameter_lists
10204 && (cp_parser_next_token_starts_class_definition_p (parser)
10205 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
357d956e
MM
10206 /* An unqualified name was used to reference this type, so
10207 there were no qualifying templates. */
3db45ab5 10208 if (!cp_parser_check_template_parameters (parser,
357d956e
MM
10209 /*num_templates=*/0))
10210 return error_mark_node;
ca85f659 10211 type = xref_tag (tag_type, identifier, ts, template_p);
a723baf1
MM
10212 }
10213 }
b9e75696 10214
31b29c62
LM
10215 if (type == error_mark_node)
10216 return error_mark_node;
10217
b9e75696
JM
10218 /* Allow attributes on forward declarations of classes. */
10219 if (attributes)
10220 {
abab4604
JM
10221 if (TREE_CODE (type) == TYPENAME_TYPE)
10222 warning (OPT_Wattributes,
10223 "attributes ignored on uninstantiated type");
10224 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10225 && ! processing_explicit_instantiation)
b9e75696
JM
10226 warning (OPT_Wattributes,
10227 "attributes ignored on template instantiation");
10228 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10229 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10230 else
10231 warning (OPT_Wattributes,
10232 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10233 }
10234
a723baf1
MM
10235 if (tag_type != enum_type)
10236 cp_parser_check_class_key (tag_type, type);
ee43dab5
MM
10237
10238 /* A "<" cannot follow an elaborated type specifier. If that
10239 happens, the user was probably trying to form a template-id. */
10240 cp_parser_check_for_invalid_template_id (parser, type);
10241
a723baf1
MM
10242 return type;
10243}
10244
10245/* Parse an enum-specifier.
10246
10247 enum-specifier:
10248 enum identifier [opt] { enumerator-list [opt] }
10249
f6af9a15 10250 GNU Extensions:
b9e75696
JM
10251 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10252 attributes[opt]
f6af9a15 10253
b9e75696
JM
10254 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10255 if the token stream isn't an enum-specifier after all. */
a723baf1
MM
10256
10257static tree
94edc4ab 10258cp_parser_enum_specifier (cp_parser* parser)
a723baf1 10259{
ff4eb0b5 10260 tree identifier;
a723baf1 10261 tree type;
b9e75696
JM
10262 tree attributes;
10263
10264 /* Parse tentatively so that we can back up if we don't find a
10265 enum-specifier. */
10266 cp_parser_parse_tentatively (parser);
a723baf1 10267
ff4eb0b5
ZW
10268 /* Caller guarantees that the current token is 'enum', an identifier
10269 possibly follows, and the token after that is an opening brace.
10270 If we don't have an identifier, fabricate an anonymous name for
10271 the enumeration being defined. */
10272 cp_lexer_consume_token (parser->lexer);
a723baf1 10273
b9e75696
JM
10274 attributes = cp_parser_attributes_opt (parser);
10275
ff4eb0b5 10276 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
a723baf1 10277 identifier = cp_parser_identifier (parser);
ff4eb0b5
ZW
10278 else
10279 identifier = make_anon_name ();
a723baf1 10280
b9e75696
JM
10281 /* Look for the `{' but don't consume it yet. */
10282 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10283 cp_parser_simulate_error (parser);
10284
10285 if (!cp_parser_parse_definitely (parser))
10286 return NULL_TREE;
10287
a723baf1
MM
10288 /* Issue an error message if type-definitions are forbidden here. */
10289 cp_parser_check_type_definition (parser);
10290
2cfe82fe
ZW
10291 /* Create the new type. We do this before consuming the opening brace
10292 so the enum will be recorded as being on the line of its tag (or the
10293 'enum' keyword, if there is no tag). */
ff4eb0b5 10294 type = start_enum (identifier);
a723baf1 10295
2cfe82fe
ZW
10296 /* Consume the opening brace. */
10297 cp_lexer_consume_token (parser->lexer);
10298
b9e75696
JM
10299 if (type == error_mark_node)
10300 {
10301 cp_parser_skip_to_end_of_block_or_statement (parser);
10302 return error_mark_node;
10303 }
10304
ff4eb0b5
ZW
10305 /* If the next token is not '}', then there are some enumerators. */
10306 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
a723baf1 10307 cp_parser_enumerator_list (parser, type);
ff4eb0b5
ZW
10308
10309 /* Consume the final '}'. */
a723baf1
MM
10310 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10311
f6af9a15 10312 /* Look for trailing attributes to apply to this enumeration, and
03fd3f84 10313 apply them if appropriate. */
f6af9a15
MA
10314 if (cp_parser_allow_gnu_extensions_p (parser))
10315 {
10316 tree trailing_attr = cp_parser_attributes_opt (parser);
10317 cplus_decl_attributes (&type,
10318 trailing_attr,
10319 (int) ATTR_FLAG_TYPE_IN_PLACE);
10320 }
10321
a723baf1
MM
10322 /* Finish up the enumeration. */
10323 finish_enum (type);
10324
10325 return type;
10326}
10327
10328/* Parse an enumerator-list. The enumerators all have the indicated
21526606 10329 TYPE.
a723baf1
MM
10330
10331 enumerator-list:
10332 enumerator-definition
10333 enumerator-list , enumerator-definition */
10334
10335static void
94edc4ab 10336cp_parser_enumerator_list (cp_parser* parser, tree type)
a723baf1
MM
10337{
10338 while (true)
10339 {
a723baf1
MM
10340 /* Parse an enumerator-definition. */
10341 cp_parser_enumerator_definition (parser, type);
ff4eb0b5
ZW
10342
10343 /* If the next token is not a ',', we've reached the end of
10344 the list. */
10345 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
a723baf1
MM
10346 break;
10347 /* Otherwise, consume the `,' and keep going. */
10348 cp_lexer_consume_token (parser->lexer);
10349 /* If the next token is a `}', there is a trailing comma. */
10350 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10351 {
10352 if (pedantic && !in_system_header)
10353 pedwarn ("comma at end of enumerator list");
10354 break;
10355 }
10356 }
10357}
10358
10359/* Parse an enumerator-definition. The enumerator has the indicated
10360 TYPE.
10361
10362 enumerator-definition:
10363 enumerator
10364 enumerator = constant-expression
21526606 10365
a723baf1
MM
10366 enumerator:
10367 identifier */
10368
10369static void
94edc4ab 10370cp_parser_enumerator_definition (cp_parser* parser, tree type)
a723baf1 10371{
a723baf1
MM
10372 tree identifier;
10373 tree value;
10374
10375 /* Look for the identifier. */
10376 identifier = cp_parser_identifier (parser);
10377 if (identifier == error_mark_node)
10378 return;
21526606 10379
ff4eb0b5
ZW
10380 /* If the next token is an '=', then there is an explicit value. */
10381 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
a723baf1
MM
10382 {
10383 /* Consume the `=' token. */
10384 cp_lexer_consume_token (parser->lexer);
10385 /* Parse the value. */
21526606 10386 value = cp_parser_constant_expression (parser,
d17811fd 10387 /*allow_non_constant_p=*/false,
14d22dd6 10388 NULL);
a723baf1
MM
10389 }
10390 else
10391 value = NULL_TREE;
10392
10393 /* Create the enumerator. */
10394 build_enumerator (identifier, value, type);
10395}
10396
10397/* Parse a namespace-name.
10398
10399 namespace-name:
10400 original-namespace-name
10401 namespace-alias
10402
10403 Returns the NAMESPACE_DECL for the namespace. */
10404
10405static tree
94edc4ab 10406cp_parser_namespace_name (cp_parser* parser)
a723baf1
MM
10407{
10408 tree identifier;
10409 tree namespace_decl;
10410
10411 /* Get the name of the namespace. */
10412 identifier = cp_parser_identifier (parser);
10413 if (identifier == error_mark_node)
10414 return error_mark_node;
10415
eea9800f
MM
10416 /* Look up the identifier in the currently active scope. Look only
10417 for namespaces, due to:
10418
10419 [basic.lookup.udir]
10420
10421 When looking up a namespace-name in a using-directive or alias
21526606 10422 definition, only namespace names are considered.
eea9800f
MM
10423
10424 And:
10425
10426 [basic.lookup.qual]
10427
10428 During the lookup of a name preceding the :: scope resolution
21526606 10429 operator, object, function, and enumerator names are ignored.
eea9800f
MM
10430
10431 (Note that cp_parser_class_or_namespace_name only calls this
10432 function if the token after the name is the scope resolution
10433 operator.) */
10434 namespace_decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 10435 none_type,
b0bc6e8e 10436 /*is_template=*/false,
eea9800f 10437 /*is_namespace=*/true,
8f78f01f 10438 /*check_dependency=*/true,
91b1ca65 10439 /*ambiguous_decls=*/NULL);
a723baf1
MM
10440 /* If it's not a namespace, issue an error. */
10441 if (namespace_decl == error_mark_node
10442 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10443 {
166206ce
VR
10444 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10445 error ("%qD is not a namespace-name", identifier);
a723baf1
MM
10446 cp_parser_error (parser, "expected namespace-name");
10447 namespace_decl = error_mark_node;
10448 }
21526606 10449
a723baf1
MM
10450 return namespace_decl;
10451}
10452
10453/* Parse a namespace-definition.
10454
10455 namespace-definition:
10456 named-namespace-definition
21526606 10457 unnamed-namespace-definition
a723baf1
MM
10458
10459 named-namespace-definition:
10460 original-namespace-definition
10461 extension-namespace-definition
10462
10463 original-namespace-definition:
10464 namespace identifier { namespace-body }
21526606 10465
a723baf1
MM
10466 extension-namespace-definition:
10467 namespace original-namespace-name { namespace-body }
21526606 10468
a723baf1
MM
10469 unnamed-namespace-definition:
10470 namespace { namespace-body } */
10471
10472static void
94edc4ab 10473cp_parser_namespace_definition (cp_parser* parser)
a723baf1 10474{
0ed5edac 10475 tree identifier, attribs;
a723baf1
MM
10476
10477 /* Look for the `namespace' keyword. */
10478 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10479
10480 /* Get the name of the namespace. We do not attempt to distinguish
10481 between an original-namespace-definition and an
10482 extension-namespace-definition at this point. The semantic
10483 analysis routines are responsible for that. */
10484 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10485 identifier = cp_parser_identifier (parser);
10486 else
10487 identifier = NULL_TREE;
10488
0ed5edac
JM
10489 /* Parse any specified attributes. */
10490 attribs = cp_parser_attributes_opt (parser);
10491
a723baf1
MM
10492 /* Look for the `{' to start the namespace. */
10493 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10494 /* Start the namespace. */
0ed5edac 10495 push_namespace_with_attribs (identifier, attribs);
a723baf1
MM
10496 /* Parse the body of the namespace. */
10497 cp_parser_namespace_body (parser);
10498 /* Finish the namespace. */
10499 pop_namespace ();
10500 /* Look for the final `}'. */
10501 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10502}
10503
10504/* Parse a namespace-body.
10505
10506 namespace-body:
10507 declaration-seq [opt] */
10508
10509static void
94edc4ab 10510cp_parser_namespace_body (cp_parser* parser)
a723baf1
MM
10511{
10512 cp_parser_declaration_seq_opt (parser);
10513}
10514
10515/* Parse a namespace-alias-definition.
10516
10517 namespace-alias-definition:
10518 namespace identifier = qualified-namespace-specifier ; */
10519
10520static void
94edc4ab 10521cp_parser_namespace_alias_definition (cp_parser* parser)
a723baf1
MM
10522{
10523 tree identifier;
10524 tree namespace_specifier;
10525
10526 /* Look for the `namespace' keyword. */
10527 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10528 /* Look for the identifier. */
10529 identifier = cp_parser_identifier (parser);
10530 if (identifier == error_mark_node)
10531 return;
10532 /* Look for the `=' token. */
10533 cp_parser_require (parser, CPP_EQ, "`='");
10534 /* Look for the qualified-namespace-specifier. */
21526606 10535 namespace_specifier
a723baf1
MM
10536 = cp_parser_qualified_namespace_specifier (parser);
10537 /* Look for the `;' token. */
10538 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10539
10540 /* Register the alias in the symbol table. */
10541 do_namespace_alias (identifier, namespace_specifier);
10542}
10543
10544/* Parse a qualified-namespace-specifier.
10545
10546 qualified-namespace-specifier:
10547 :: [opt] nested-name-specifier [opt] namespace-name
10548
10549 Returns a NAMESPACE_DECL corresponding to the specified
10550 namespace. */
10551
10552static tree
94edc4ab 10553cp_parser_qualified_namespace_specifier (cp_parser* parser)
a723baf1
MM
10554{
10555 /* Look for the optional `::'. */
21526606 10556 cp_parser_global_scope_opt (parser,
a723baf1
MM
10557 /*current_scope_valid_p=*/false);
10558
10559 /* Look for the optional nested-name-specifier. */
10560 cp_parser_nested_name_specifier_opt (parser,
10561 /*typename_keyword_p=*/false,
10562 /*check_dependency_p=*/true,
a668c6ad
MM
10563 /*type_p=*/false,
10564 /*is_declaration=*/true);
a723baf1
MM
10565
10566 return cp_parser_namespace_name (parser);
10567}
10568
10569/* Parse a using-declaration.
10570
10571 using-declaration:
10572 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10573 using :: unqualified-id ; */
10574
10575static void
94edc4ab 10576cp_parser_using_declaration (cp_parser* parser)
a723baf1
MM
10577{
10578 cp_token *token;
10579 bool typename_p = false;
10580 bool global_scope_p;
10581 tree decl;
10582 tree identifier;
ed5f054f 10583 tree qscope;
a723baf1
MM
10584
10585 /* Look for the `using' keyword. */
10586 cp_parser_require_keyword (parser, RID_USING, "`using'");
21526606 10587
a723baf1
MM
10588 /* Peek at the next token. */
10589 token = cp_lexer_peek_token (parser->lexer);
10590 /* See if it's `typename'. */
10591 if (token->keyword == RID_TYPENAME)
10592 {
10593 /* Remember that we've seen it. */
10594 typename_p = true;
10595 /* Consume the `typename' token. */
10596 cp_lexer_consume_token (parser->lexer);
10597 }
10598
10599 /* Look for the optional global scope qualification. */
21526606 10600 global_scope_p
a723baf1 10601 = (cp_parser_global_scope_opt (parser,
21526606 10602 /*current_scope_valid_p=*/false)
a723baf1
MM
10603 != NULL_TREE);
10604
10605 /* If we saw `typename', or didn't see `::', then there must be a
10606 nested-name-specifier present. */
10607 if (typename_p || !global_scope_p)
21526606 10608 qscope = cp_parser_nested_name_specifier (parser, typename_p,
ed5f054f
AO
10609 /*check_dependency_p=*/true,
10610 /*type_p=*/false,
10611 /*is_declaration=*/true);
a723baf1
MM
10612 /* Otherwise, we could be in either of the two productions. In that
10613 case, treat the nested-name-specifier as optional. */
10614 else
ed5f054f
AO
10615 qscope = cp_parser_nested_name_specifier_opt (parser,
10616 /*typename_keyword_p=*/false,
10617 /*check_dependency_p=*/true,
10618 /*type_p=*/false,
10619 /*is_declaration=*/true);
10620 if (!qscope)
10621 qscope = global_namespace;
a723baf1
MM
10622
10623 /* Parse the unqualified-id. */
21526606 10624 identifier = cp_parser_unqualified_id (parser,
a723baf1 10625 /*template_keyword_p=*/false,
f3c2dfc6 10626 /*check_dependency_p=*/true,
fa6098f8
MM
10627 /*declarator_p=*/true,
10628 /*optional_p=*/false);
a723baf1
MM
10629
10630 /* The function we call to handle a using-declaration is different
10631 depending on what scope we are in. */
56bbd9d6 10632 if (qscope == error_mark_node || identifier == error_mark_node)
f3c2dfc6
MM
10633 ;
10634 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10635 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10636 /* [namespace.udecl]
10637
10638 A using declaration shall not name a template-id. */
10639 error ("a template-id may not appear in a using-declaration");
a723baf1
MM
10640 else
10641 {
a5201a91 10642 if (at_class_scope_p ())
4eb6d609 10643 {
f3c2dfc6 10644 /* Create the USING_DECL. */
1d786913 10645 decl = do_class_using_decl (parser->scope, identifier);
f3c2dfc6
MM
10646 /* Add it to the list of members in this class. */
10647 finish_member_declaration (decl);
4eb6d609 10648 }
a723baf1 10649 else
f3c2dfc6
MM
10650 {
10651 decl = cp_parser_lookup_name_simple (parser, identifier);
10652 if (decl == error_mark_node)
4bb8ca28 10653 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
a5201a91 10654 else if (!at_namespace_scope_p ())
ed5f054f 10655 do_local_using_decl (decl, qscope, identifier);
f3c2dfc6 10656 else
ed5f054f 10657 do_toplevel_using_decl (decl, qscope, identifier);
f3c2dfc6 10658 }
a723baf1
MM
10659 }
10660
10661 /* Look for the final `;'. */
10662 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10663}
10664
21526606
EC
10665/* Parse a using-directive.
10666
a723baf1
MM
10667 using-directive:
10668 using namespace :: [opt] nested-name-specifier [opt]
10669 namespace-name ; */
10670
10671static void
94edc4ab 10672cp_parser_using_directive (cp_parser* parser)
a723baf1
MM
10673{
10674 tree namespace_decl;
86098eb8 10675 tree attribs;
a723baf1
MM
10676
10677 /* Look for the `using' keyword. */
10678 cp_parser_require_keyword (parser, RID_USING, "`using'");
10679 /* And the `namespace' keyword. */
10680 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10681 /* Look for the optional `::' operator. */
10682 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
34cd5ae7 10683 /* And the optional nested-name-specifier. */
a723baf1
MM
10684 cp_parser_nested_name_specifier_opt (parser,
10685 /*typename_keyword_p=*/false,
10686 /*check_dependency_p=*/true,
a668c6ad
MM
10687 /*type_p=*/false,
10688 /*is_declaration=*/true);
a723baf1
MM
10689 /* Get the namespace being used. */
10690 namespace_decl = cp_parser_namespace_name (parser);
86098eb8
JM
10691 /* And any specified attributes. */
10692 attribs = cp_parser_attributes_opt (parser);
a723baf1 10693 /* Update the symbol table. */
86098eb8 10694 parse_using_directive (namespace_decl, attribs);
a723baf1
MM
10695 /* Look for the final `;'. */
10696 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10697}
10698
10699/* Parse an asm-definition.
10700
10701 asm-definition:
21526606 10702 asm ( string-literal ) ;
a723baf1
MM
10703
10704 GNU Extension:
10705
10706 asm-definition:
10707 asm volatile [opt] ( string-literal ) ;
10708 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10709 asm volatile [opt] ( string-literal : asm-operand-list [opt]
0cbd7506 10710 : asm-operand-list [opt] ) ;
21526606 10711 asm volatile [opt] ( string-literal : asm-operand-list [opt]
0cbd7506
MS
10712 : asm-operand-list [opt]
10713 : asm-operand-list [opt] ) ; */
a723baf1
MM
10714
10715static void
94edc4ab 10716cp_parser_asm_definition (cp_parser* parser)
a723baf1 10717{
a723baf1
MM
10718 tree string;
10719 tree outputs = NULL_TREE;
10720 tree inputs = NULL_TREE;
10721 tree clobbers = NULL_TREE;
10722 tree asm_stmt;
10723 bool volatile_p = false;
10724 bool extended_p = false;
10725
10726 /* Look for the `asm' keyword. */
10727 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10728 /* See if the next token is `volatile'. */
10729 if (cp_parser_allow_gnu_extensions_p (parser)
10730 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10731 {
10732 /* Remember that we saw the `volatile' keyword. */
10733 volatile_p = true;
10734 /* Consume the token. */
10735 cp_lexer_consume_token (parser->lexer);
10736 }
10737 /* Look for the opening `('. */
c162c75e
MA
10738 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10739 return;
a723baf1 10740 /* Look for the string. */
c162c75e
MA
10741 string = cp_parser_string_literal (parser, false, false);
10742 if (string == error_mark_node)
10743 {
10744 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10745 /*consume_paren=*/true);
10746 return;
10747 }
10748
a723baf1 10749 /* If we're allowing GNU extensions, check for the extended assembly
21526606 10750 syntax. Unfortunately, the `:' tokens need not be separated by
a723baf1
MM
10751 a space in C, and so, for compatibility, we tolerate that here
10752 too. Doing that means that we have to treat the `::' operator as
10753 two `:' tokens. */
10754 if (cp_parser_allow_gnu_extensions_p (parser)
10755 && at_function_scope_p ()
10756 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10757 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10758 {
10759 bool inputs_p = false;
10760 bool clobbers_p = false;
10761
10762 /* The extended syntax was used. */
10763 extended_p = true;
10764
10765 /* Look for outputs. */
10766 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10767 {
10768 /* Consume the `:'. */
10769 cp_lexer_consume_token (parser->lexer);
10770 /* Parse the output-operands. */
21526606 10771 if (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
10772 CPP_COLON)
10773 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
10774 CPP_SCOPE)
10775 && cp_lexer_next_token_is_not (parser->lexer,
10776 CPP_CLOSE_PAREN))
a723baf1
MM
10777 outputs = cp_parser_asm_operand_list (parser);
10778 }
10779 /* If the next token is `::', there are no outputs, and the
10780 next token is the beginning of the inputs. */
10781 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
bf5930d4
JB
10782 /* The inputs are coming next. */
10783 inputs_p = true;
a723baf1
MM
10784
10785 /* Look for inputs. */
10786 if (inputs_p
10787 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10788 {
bf5930d4
JB
10789 /* Consume the `:' or `::'. */
10790 cp_lexer_consume_token (parser->lexer);
a723baf1 10791 /* Parse the output-operands. */
21526606 10792 if (cp_lexer_next_token_is_not (parser->lexer,
a723baf1 10793 CPP_COLON)
8caf4c38
MM
10794 && cp_lexer_next_token_is_not (parser->lexer,
10795 CPP_CLOSE_PAREN))
a723baf1
MM
10796 inputs = cp_parser_asm_operand_list (parser);
10797 }
10798 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10799 /* The clobbers are coming next. */
10800 clobbers_p = true;
10801
10802 /* Look for clobbers. */
21526606 10803 if (clobbers_p
a723baf1
MM
10804 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10805 {
bf5930d4
JB
10806 /* Consume the `:' or `::'. */
10807 cp_lexer_consume_token (parser->lexer);
a723baf1 10808 /* Parse the clobbers. */
8caf4c38
MM
10809 if (cp_lexer_next_token_is_not (parser->lexer,
10810 CPP_CLOSE_PAREN))
10811 clobbers = cp_parser_asm_clobber_list (parser);
a723baf1
MM
10812 }
10813 }
10814 /* Look for the closing `)'. */
10815 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
10816 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10817 /*consume_paren=*/true);
a723baf1
MM
10818 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10819
e130a54b 10820 /* Create the ASM_EXPR. */
a723baf1
MM
10821 if (at_function_scope_p ())
10822 {
6de9cd9a
DN
10823 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10824 inputs, clobbers);
e130a54b 10825 /* If the extended syntax was not used, mark the ASM_EXPR. */
a723baf1 10826 if (!extended_p)
ca059043
AP
10827 {
10828 tree temp = asm_stmt;
10829 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10830 temp = TREE_OPERAND (temp, 0);
c8094d83 10831
ca059043
AP
10832 ASM_INPUT_P (temp) = 1;
10833 }
a723baf1
MM
10834 }
10835 else
474eccc6 10836 cgraph_add_asm_node (string);
a723baf1
MM
10837}
10838
10839/* Declarators [gram.dcl.decl] */
10840
10841/* Parse an init-declarator.
10842
10843 init-declarator:
10844 declarator initializer [opt]
10845
10846 GNU Extension:
10847
10848 init-declarator:
10849 declarator asm-specification [opt] attributes [opt] initializer [opt]
10850
4bb8ca28
MM
10851 function-definition:
10852 decl-specifier-seq [opt] declarator ctor-initializer [opt]
21526606
EC
10853 function-body
10854 decl-specifier-seq [opt] declarator function-try-block
4bb8ca28
MM
10855
10856 GNU Extension:
10857
10858 function-definition:
21526606 10859 __extension__ function-definition
4bb8ca28 10860
6b648482
MM
10861 The DECL_SPECIFIERS apply to this declarator. Returns a
10862 representation of the entity declared. If MEMBER_P is TRUE, then
10863 this declarator appears in a class scope. The new DECL created by
10864 this declarator is returned.
10865
10866 The CHECKS are access checks that should be performed once we know
10867 what entity is being declared (and, therefore, what classes have
10868 befriended it).
a723baf1
MM
10869
10870 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10871 for a function-definition here as well. If the declarator is a
10872 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10873 be TRUE upon return. By that point, the function-definition will
10874 have been completely parsed.
10875
10876 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10877 is FALSE. */
10878
10879static tree
21526606 10880cp_parser_init_declarator (cp_parser* parser,
62d1db17 10881 cp_decl_specifier_seq *decl_specifiers,
6b648482 10882 tree checks,
94edc4ab
NN
10883 bool function_definition_allowed_p,
10884 bool member_p,
560ad596 10885 int declares_class_or_enum,
94edc4ab 10886 bool* function_definition_p)
a723baf1
MM
10887{
10888 cp_token *token;
058b15c1 10889 cp_declarator *declarator;
62d1db17 10890 tree prefix_attributes;
a723baf1
MM
10891 tree attributes;
10892 tree asm_specification;
10893 tree initializer;
10894 tree decl = NULL_TREE;
10895 tree scope;
a723baf1 10896 bool is_initialized;
63c9a190
MM
10897 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
10898 initialized with "= ..", CPP_OPEN_PAREN if initialized with
10899 "(...)". */
10900 enum cpp_ttype initialization_kind;
21137095 10901 bool is_parenthesized_init = false;
39703eb9 10902 bool is_non_constant_init;
7efa3e22 10903 int ctor_dtor_or_conv_p;
a723baf1 10904 bool friend_p;
4514aa8c 10905 tree pushed_scope = NULL;
a723baf1 10906
62d1db17
MM
10907 /* Gather the attributes that were provided with the
10908 decl-specifiers. */
10909 prefix_attributes = decl_specifiers->attributes;
62d1db17 10910
a723baf1
MM
10911 /* Assume that this is not the declarator for a function
10912 definition. */
10913 if (function_definition_p)
10914 *function_definition_p = false;
10915
10916 /* Defer access checks while parsing the declarator; we cannot know
21526606 10917 what names are accessible until we know what is being
a723baf1 10918 declared. */
cf22909c
KL
10919 resume_deferring_access_checks ();
10920
a723baf1 10921 /* Parse the declarator. */
21526606 10922 declarator
62b8a44e 10923 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 10924 &ctor_dtor_or_conv_p,
db86dd14
MM
10925 /*parenthesized_p=*/NULL,
10926 /*member_p=*/false);
a723baf1 10927 /* Gather up the deferred checks. */
cf22909c 10928 stop_deferring_access_checks ();
24c0ef37 10929
a723baf1
MM
10930 /* If the DECLARATOR was erroneous, there's no need to go
10931 further. */
058b15c1 10932 if (declarator == cp_error_declarator)
cf22909c 10933 return error_mark_node;
a723baf1 10934
fc6a28d7
MM
10935 if (declares_class_or_enum & 2)
10936 cp_parser_check_for_definition_in_return_type (declarator,
10937 decl_specifiers->type);
560ad596 10938
a723baf1
MM
10939 /* Figure out what scope the entity declared by the DECLARATOR is
10940 located in. `grokdeclarator' sometimes changes the scope, so
10941 we compute it now. */
10942 scope = get_scope_of_declarator (declarator);
10943
10944 /* If we're allowing GNU extensions, look for an asm-specification
10945 and attributes. */
10946 if (cp_parser_allow_gnu_extensions_p (parser))
10947 {
10948 /* Look for an asm-specification. */
10949 asm_specification = cp_parser_asm_specification_opt (parser);
10950 /* And attributes. */
10951 attributes = cp_parser_attributes_opt (parser);
10952 }
10953 else
10954 {
10955 asm_specification = NULL_TREE;
10956 attributes = NULL_TREE;
10957 }
10958
10959 /* Peek at the next token. */
10960 token = cp_lexer_peek_token (parser->lexer);
10961 /* Check to see if the token indicates the start of a
10962 function-definition. */
10963 if (cp_parser_token_starts_function_definition_p (token))
10964 {
10965 if (!function_definition_allowed_p)
10966 {
10967 /* If a function-definition should not appear here, issue an
10968 error message. */
10969 cp_parser_error (parser,
10970 "a function-definition is not allowed here");
10971 return error_mark_node;
10972 }
10973 else
10974 {
a723baf1
MM
10975 /* Neither attributes nor an asm-specification are allowed
10976 on a function-definition. */
10977 if (asm_specification)
10978 error ("an asm-specification is not allowed on a function-definition");
10979 if (attributes)
10980 error ("attributes are not allowed on a function-definition");
10981 /* This is a function-definition. */
10982 *function_definition_p = true;
10983
a723baf1 10984 /* Parse the function definition. */
4bb8ca28
MM
10985 if (member_p)
10986 decl = cp_parser_save_member_function_body (parser,
10987 decl_specifiers,
10988 declarator,
10989 prefix_attributes);
10990 else
21526606 10991 decl
4bb8ca28
MM
10992 = (cp_parser_function_definition_from_specifiers_and_declarator
10993 (parser, decl_specifiers, prefix_attributes, declarator));
24c0ef37 10994
a723baf1
MM
10995 return decl;
10996 }
10997 }
10998
10999 /* [dcl.dcl]
11000
11001 Only in function declarations for constructors, destructors, and
21526606 11002 type conversions can the decl-specifier-seq be omitted.
a723baf1
MM
11003
11004 We explicitly postpone this check past the point where we handle
11005 function-definitions because we tolerate function-definitions
11006 that are missing their return types in some modes. */
62d1db17 11007 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
a723baf1 11008 {
21526606 11009 cp_parser_error (parser,
a723baf1
MM
11010 "expected constructor, destructor, or type conversion");
11011 return error_mark_node;
11012 }
11013
11014 /* An `=' or an `(' indicates an initializer. */
63c9a190
MM
11015 if (token->type == CPP_EQ
11016 || token->type == CPP_OPEN_PAREN)
a723baf1 11017 {
63c9a190
MM
11018 is_initialized = true;
11019 initialization_kind = token->type;
11020 }
11021 else
11022 {
11023 /* If the init-declarator isn't initialized and isn't followed by a
11024 `,' or `;', it's not a valid init-declarator. */
11025 if (token->type != CPP_COMMA
11026 && token->type != CPP_SEMICOLON)
11027 {
11028 cp_parser_error (parser, "expected initializer");
11029 return error_mark_node;
11030 }
11031 is_initialized = false;
11032 initialization_kind = CPP_EOF;
a723baf1
MM
11033 }
11034
11035 /* Because start_decl has side-effects, we should only call it if we
11036 know we're going ahead. By this point, we know that we cannot
11037 possibly be looking at any other construct. */
11038 cp_parser_commit_to_tentative_parse (parser);
11039
e90c7b84
ILT
11040 /* If the decl specifiers were bad, issue an error now that we're
11041 sure this was intended to be a declarator. Then continue
11042 declaring the variable(s), as int, to try to cut down on further
11043 errors. */
62d1db17
MM
11044 if (decl_specifiers->any_specifiers_p
11045 && decl_specifiers->type == error_mark_node)
e90c7b84
ILT
11046 {
11047 cp_parser_error (parser, "invalid type in declaration");
62d1db17 11048 decl_specifiers->type = integer_type_node;
e90c7b84
ILT
11049 }
11050
a723baf1
MM
11051 /* Check to see whether or not this declaration is a friend. */
11052 friend_p = cp_parser_friend_p (decl_specifiers);
11053
11054 /* Check that the number of template-parameter-lists is OK. */
ee3071ef 11055 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
cf22909c 11056 return error_mark_node;
a723baf1
MM
11057
11058 /* Enter the newly declared entry in the symbol table. If we're
11059 processing a declaration in a class-specifier, we wait until
11060 after processing the initializer. */
11061 if (!member_p)
11062 {
11063 if (parser->in_unbraced_linkage_specification_p)
75a82978 11064 decl_specifiers->storage_class = sc_extern;
ee3071ef 11065 decl = start_decl (declarator, decl_specifiers,
73a8adb6 11066 is_initialized, attributes, prefix_attributes,
4514aa8c 11067 &pushed_scope);
a723baf1 11068 }
73a8adb6
MM
11069 else if (scope)
11070 /* Enter the SCOPE. That way unqualified names appearing in the
11071 initializer will be looked up in SCOPE. */
4514aa8c 11072 pushed_scope = push_scope (scope);
a723baf1
MM
11073
11074 /* Perform deferred access control checks, now that we know in which
11075 SCOPE the declared entity resides. */
21526606 11076 if (!member_p && decl)
a723baf1
MM
11077 {
11078 tree saved_current_function_decl = NULL_TREE;
11079
11080 /* If the entity being declared is a function, pretend that we
11081 are in its scope. If it is a `friend', it may have access to
9bcb9aae 11082 things that would not otherwise be accessible. */
a723baf1
MM
11083 if (TREE_CODE (decl) == FUNCTION_DECL)
11084 {
11085 saved_current_function_decl = current_function_decl;
11086 current_function_decl = decl;
11087 }
21526606 11088
6b648482
MM
11089 /* Perform access checks for template parameters. */
11090 cp_parser_perform_template_parameter_access_checks (checks);
11091
cf22909c
KL
11092 /* Perform the access control checks for the declarator and the
11093 the decl-specifiers. */
11094 perform_deferred_access_checks ();
a723baf1
MM
11095
11096 /* Restore the saved value. */
11097 if (TREE_CODE (decl) == FUNCTION_DECL)
11098 current_function_decl = saved_current_function_decl;
11099 }
11100
11101 /* Parse the initializer. */
aa9d194e
MM
11102 initializer = NULL_TREE;
11103 is_parenthesized_init = false;
11104 is_non_constant_init = true;
a723baf1 11105 if (is_initialized)
63c9a190
MM
11106 {
11107 if (declarator->kind == cdk_function
11108 && declarator->declarator->kind == cdk_id
11109 && initialization_kind == CPP_EQ)
11110 initializer = cp_parser_pure_specifier (parser);
11111 else
11112 initializer = cp_parser_initializer (parser,
11113 &is_parenthesized_init,
11114 &is_non_constant_init);
11115 }
a723baf1
MM
11116
11117 /* The old parser allows attributes to appear after a parenthesized
11118 initializer. Mark Mitchell proposed removing this functionality
11119 on the GCC mailing lists on 2002-08-13. This parser accepts the
11120 attributes -- but ignores them. */
11121 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11122 if (cp_parser_attributes_opt (parser))
5c498b10
DD
11123 warning (OPT_Wattributes,
11124 "attributes after parenthesized initializer ignored");
a723baf1 11125
a723baf1
MM
11126 /* For an in-class declaration, use `grokfield' to create the
11127 declaration. */
11128 if (member_p)
8db1028e 11129 {
4514aa8c 11130 if (pushed_scope)
62a4d942 11131 {
4514aa8c
NS
11132 pop_scope (pushed_scope);
11133 pushed_scope = false;
62a4d942 11134 }
8db1028e 11135 decl = grokfield (declarator, decl_specifiers,
d174af6c
MM
11136 initializer, !is_non_constant_init,
11137 /*asmspec=*/NULL_TREE,
d08fd9d6 11138 prefix_attributes);
8db1028e
NS
11139 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11140 cp_parser_save_default_args (parser, decl);
11141 }
21526606 11142
a723baf1
MM
11143 /* Finish processing the declaration. But, skip friend
11144 declarations. */
550205c3 11145 if (!friend_p && decl && decl != error_mark_node)
73a8adb6
MM
11146 {
11147 cp_finish_decl (decl,
d174af6c 11148 initializer, !is_non_constant_init,
73a8adb6
MM
11149 asm_specification,
11150 /* If the initializer is in parentheses, then this is
11151 a direct-initialization, which means that an
11152 `explicit' constructor is OK. Otherwise, an
11153 `explicit' constructor cannot be used. */
11154 ((is_parenthesized_init || !is_initialized)
a723baf1 11155 ? 0 : LOOKUP_ONLYCONVERTING));
73a8adb6 11156 }
4514aa8c
NS
11157 if (!friend_p && pushed_scope)
11158 pop_scope (pushed_scope);
a723baf1
MM
11159
11160 return decl;
11161}
11162
11163/* Parse a declarator.
21526606 11164
a723baf1
MM
11165 declarator:
11166 direct-declarator
21526606 11167 ptr-operator declarator
a723baf1
MM
11168
11169 abstract-declarator:
11170 ptr-operator abstract-declarator [opt]
11171 direct-abstract-declarator
11172
11173 GNU Extensions:
11174
11175 declarator:
11176 attributes [opt] direct-declarator
21526606 11177 attributes [opt] ptr-operator declarator
a723baf1
MM
11178
11179 abstract-declarator:
11180 attributes [opt] ptr-operator abstract-declarator [opt]
11181 attributes [opt] direct-abstract-declarator
21526606 11182
7efa3e22
NS
11183 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11184 detect constructor, destructor or conversion operators. It is set
11185 to -1 if the declarator is a name, and +1 if it is a
11186 function. Otherwise it is set to zero. Usually you just want to
11187 test for >0, but internally the negative value is used.
21526606 11188
a723baf1
MM
11189 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11190 a decl-specifier-seq unless it declares a constructor, destructor,
11191 or conversion. It might seem that we could check this condition in
11192 semantic analysis, rather than parsing, but that makes it difficult
11193 to handle something like `f()'. We want to notice that there are
11194 no decl-specifiers, and therefore realize that this is an
21526606
EC
11195 expression, not a declaration.)
11196
4bb8ca28 11197 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
c8094d83 11198 the declarator is a direct-declarator of the form "(...)".
db86dd14
MM
11199
11200 MEMBER_P is true iff this declarator is a member-declarator. */
a723baf1 11201
058b15c1 11202static cp_declarator *
21526606 11203cp_parser_declarator (cp_parser* parser,
0cbd7506
MS
11204 cp_parser_declarator_kind dcl_kind,
11205 int* ctor_dtor_or_conv_p,
db86dd14
MM
11206 bool* parenthesized_p,
11207 bool member_p)
a723baf1
MM
11208{
11209 cp_token *token;
058b15c1 11210 cp_declarator *declarator;
a723baf1 11211 enum tree_code code;
3c01e5df 11212 cp_cv_quals cv_quals;
a723baf1
MM
11213 tree class_type;
11214 tree attributes = NULL_TREE;
11215
11216 /* Assume this is not a constructor, destructor, or type-conversion
11217 operator. */
11218 if (ctor_dtor_or_conv_p)
7efa3e22 11219 *ctor_dtor_or_conv_p = 0;
a723baf1
MM
11220
11221 if (cp_parser_allow_gnu_extensions_p (parser))
11222 attributes = cp_parser_attributes_opt (parser);
21526606 11223
a723baf1
MM
11224 /* Peek at the next token. */
11225 token = cp_lexer_peek_token (parser->lexer);
21526606 11226
a723baf1
MM
11227 /* Check for the ptr-operator production. */
11228 cp_parser_parse_tentatively (parser);
11229 /* Parse the ptr-operator. */
21526606
EC
11230 code = cp_parser_ptr_operator (parser,
11231 &class_type,
3c01e5df 11232 &cv_quals);
a723baf1
MM
11233 /* If that worked, then we have a ptr-operator. */
11234 if (cp_parser_parse_definitely (parser))
11235 {
4bb8ca28
MM
11236 /* If a ptr-operator was found, then this declarator was not
11237 parenthesized. */
11238 if (parenthesized_p)
11239 *parenthesized_p = true;
a723baf1
MM
11240 /* The dependent declarator is optional if we are parsing an
11241 abstract-declarator. */
62b8a44e 11242 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1
MM
11243 cp_parser_parse_tentatively (parser);
11244
11245 /* Parse the dependent declarator. */
62b8a44e 11246 declarator = cp_parser_declarator (parser, dcl_kind,
4bb8ca28 11247 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
11248 /*parenthesized_p=*/NULL,
11249 /*member_p=*/false);
a723baf1
MM
11250
11251 /* If we are parsing an abstract-declarator, we must handle the
11252 case where the dependent declarator is absent. */
62b8a44e
NS
11253 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11254 && !cp_parser_parse_definitely (parser))
058b15c1 11255 declarator = NULL;
21526606 11256
a723baf1 11257 /* Build the representation of the ptr-operator. */
058b15c1 11258 if (class_type)
3c01e5df 11259 declarator = make_ptrmem_declarator (cv_quals,
058b15c1
MM
11260 class_type,
11261 declarator);
11262 else if (code == INDIRECT_REF)
3c01e5df 11263 declarator = make_pointer_declarator (cv_quals, declarator);
a723baf1 11264 else
3c01e5df 11265 declarator = make_reference_declarator (cv_quals, declarator);
a723baf1
MM
11266 }
11267 /* Everything else is a direct-declarator. */
11268 else
4bb8ca28
MM
11269 {
11270 if (parenthesized_p)
11271 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11272 CPP_OPEN_PAREN);
11273 declarator = cp_parser_direct_declarator (parser, dcl_kind,
db86dd14
MM
11274 ctor_dtor_or_conv_p,
11275 member_p);
4bb8ca28 11276 }
a723baf1 11277
84019f23 11278 if (attributes && declarator && declarator != cp_error_declarator)
058b15c1 11279 declarator->attributes = attributes;
21526606 11280
a723baf1
MM
11281 return declarator;
11282}
11283
11284/* Parse a direct-declarator or direct-abstract-declarator.
11285
11286 direct-declarator:
11287 declarator-id
11288 direct-declarator ( parameter-declaration-clause )
21526606 11289 cv-qualifier-seq [opt]
a723baf1
MM
11290 exception-specification [opt]
11291 direct-declarator [ constant-expression [opt] ]
21526606 11292 ( declarator )
a723baf1
MM
11293
11294 direct-abstract-declarator:
11295 direct-abstract-declarator [opt]
21526606 11296 ( parameter-declaration-clause )
a723baf1
MM
11297 cv-qualifier-seq [opt]
11298 exception-specification [opt]
11299 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11300 ( abstract-declarator )
11301
62b8a44e
NS
11302 Returns a representation of the declarator. DCL_KIND is
11303 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11304 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11305 we are parsing a direct-declarator. It is
11306 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11307 of ambiguity we prefer an abstract declarator, as per
db86dd14 11308 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
058b15c1 11309 cp_parser_declarator. */
a723baf1 11310
058b15c1 11311static cp_declarator *
94edc4ab 11312cp_parser_direct_declarator (cp_parser* parser,
0cbd7506
MS
11313 cp_parser_declarator_kind dcl_kind,
11314 int* ctor_dtor_or_conv_p,
db86dd14 11315 bool member_p)
a723baf1
MM
11316{
11317 cp_token *token;
058b15c1 11318 cp_declarator *declarator = NULL;
a723baf1
MM
11319 tree scope = NULL_TREE;
11320 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11321 bool saved_in_declarator_p = parser->in_declarator_p;
62b8a44e 11322 bool first = true;
4514aa8c 11323 tree pushed_scope = NULL_TREE;
21526606 11324
62b8a44e 11325 while (true)
a723baf1 11326 {
62b8a44e
NS
11327 /* Peek at the next token. */
11328 token = cp_lexer_peek_token (parser->lexer);
11329 if (token->type == CPP_OPEN_PAREN)
a723baf1 11330 {
62b8a44e 11331 /* This is either a parameter-declaration-clause, or a
0cbd7506
MS
11332 parenthesized declarator. When we know we are parsing a
11333 named declarator, it must be a parenthesized declarator
11334 if FIRST is true. For instance, `(int)' is a
11335 parameter-declaration-clause, with an omitted
11336 direct-abstract-declarator. But `((*))', is a
11337 parenthesized abstract declarator. Finally, when T is a
11338 template parameter `(T)' is a
11339 parameter-declaration-clause, and not a parenthesized
11340 named declarator.
21526606 11341
62b8a44e
NS
11342 We first try and parse a parameter-declaration-clause,
11343 and then try a nested declarator (if FIRST is true).
a723baf1 11344
62b8a44e
NS
11345 It is not an error for it not to be a
11346 parameter-declaration-clause, even when FIRST is
11347 false. Consider,
11348
11349 int i (int);
11350 int i (3);
11351
11352 The first is the declaration of a function while the
11353 second is a the definition of a variable, including its
11354 initializer.
11355
11356 Having seen only the parenthesis, we cannot know which of
11357 these two alternatives should be selected. Even more
11358 complex are examples like:
11359
0cbd7506 11360 int i (int (a));
62b8a44e
NS
11361 int i (int (3));
11362
11363 The former is a function-declaration; the latter is a
21526606 11364 variable initialization.
62b8a44e 11365
34cd5ae7 11366 Thus again, we try a parameter-declaration-clause, and if
62b8a44e
NS
11367 that fails, we back out and return. */
11368
11369 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1 11370 {
058b15c1 11371 cp_parameter_declarator *params;
4047b164 11372 unsigned saved_num_template_parameter_lists;
21526606 11373
db86dd14
MM
11374 /* In a member-declarator, the only valid interpretation
11375 of a parenthesis is the start of a
11376 parameter-declaration-clause. (It is invalid to
11377 initialize a static data member with a parenthesized
11378 initializer; only the "=" form of initialization is
11379 permitted.) */
11380 if (!member_p)
11381 cp_parser_parse_tentatively (parser);
a723baf1 11382
62b8a44e
NS
11383 /* Consume the `('. */
11384 cp_lexer_consume_token (parser->lexer);
11385 if (first)
11386 {
11387 /* If this is going to be an abstract declarator, we're
11388 in a declarator and we can't have default args. */
11389 parser->default_arg_ok_p = false;
11390 parser->in_declarator_p = true;
11391 }
21526606 11392
4047b164
KL
11393 /* Inside the function parameter list, surrounding
11394 template-parameter-lists do not apply. */
11395 saved_num_template_parameter_lists
11396 = parser->num_template_parameter_lists;
11397 parser->num_template_parameter_lists = 0;
11398
62b8a44e
NS
11399 /* Parse the parameter-declaration-clause. */
11400 params = cp_parser_parameter_declaration_clause (parser);
11401
4047b164
KL
11402 parser->num_template_parameter_lists
11403 = saved_num_template_parameter_lists;
11404
62b8a44e 11405 /* If all went well, parse the cv-qualifier-seq and the
0cbd7506 11406 exception-specification. */
db86dd14 11407 if (member_p || cp_parser_parse_definitely (parser))
62b8a44e 11408 {
3c01e5df 11409 cp_cv_quals cv_quals;
62b8a44e 11410 tree exception_specification;
7efa3e22
NS
11411
11412 if (ctor_dtor_or_conv_p)
11413 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
62b8a44e
NS
11414 first = false;
11415 /* Consume the `)'. */
11416 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11417
11418 /* Parse the cv-qualifier-seq. */
3c01e5df 11419 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
62b8a44e 11420 /* And the exception-specification. */
21526606 11421 exception_specification
62b8a44e
NS
11422 = cp_parser_exception_specification_opt (parser);
11423
11424 /* Create the function-declarator. */
11425 declarator = make_call_declarator (declarator,
11426 params,
3c01e5df 11427 cv_quals,
62b8a44e
NS
11428 exception_specification);
11429 /* Any subsequent parameter lists are to do with
0cbd7506
MS
11430 return type, so are not those of the declared
11431 function. */
62b8a44e 11432 parser->default_arg_ok_p = false;
21526606 11433
62b8a44e
NS
11434 /* Repeat the main loop. */
11435 continue;
11436 }
11437 }
21526606 11438
62b8a44e
NS
11439 /* If this is the first, we can try a parenthesized
11440 declarator. */
11441 if (first)
a723baf1 11442 {
a7324e75
MM
11443 bool saved_in_type_id_in_expr_p;
11444
a723baf1 11445 parser->default_arg_ok_p = saved_default_arg_ok_p;
62b8a44e 11446 parser->in_declarator_p = saved_in_declarator_p;
21526606 11447
62b8a44e
NS
11448 /* Consume the `('. */
11449 cp_lexer_consume_token (parser->lexer);
11450 /* Parse the nested declarator. */
a7324e75
MM
11451 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11452 parser->in_type_id_in_expr_p = true;
21526606 11453 declarator
4bb8ca28 11454 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
db86dd14
MM
11455 /*parenthesized_p=*/NULL,
11456 member_p);
a7324e75 11457 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
62b8a44e
NS
11458 first = false;
11459 /* Expect a `)'. */
11460 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
058b15c1
MM
11461 declarator = cp_error_declarator;
11462 if (declarator == cp_error_declarator)
62b8a44e 11463 break;
21526606 11464
62b8a44e 11465 goto handle_declarator;
a723baf1 11466 }
9bcb9aae 11467 /* Otherwise, we must be done. */
62b8a44e
NS
11468 else
11469 break;
a723baf1 11470 }
62b8a44e
NS
11471 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11472 && token->type == CPP_OPEN_SQUARE)
a723baf1 11473 {
62b8a44e 11474 /* Parse an array-declarator. */
a723baf1
MM
11475 tree bounds;
11476
7efa3e22
NS
11477 if (ctor_dtor_or_conv_p)
11478 *ctor_dtor_or_conv_p = 0;
21526606 11479
62b8a44e
NS
11480 first = false;
11481 parser->default_arg_ok_p = false;
11482 parser->in_declarator_p = true;
a723baf1
MM
11483 /* Consume the `['. */
11484 cp_lexer_consume_token (parser->lexer);
11485 /* Peek at the next token. */
11486 token = cp_lexer_peek_token (parser->lexer);
11487 /* If the next token is `]', then there is no
11488 constant-expression. */
11489 if (token->type != CPP_CLOSE_SQUARE)
14d22dd6
MM
11490 {
11491 bool non_constant_p;
11492
21526606 11493 bounds
14d22dd6
MM
11494 = cp_parser_constant_expression (parser,
11495 /*allow_non_constant=*/true,
11496 &non_constant_p);
d17811fd 11497 if (!non_constant_p)
9baa27a9 11498 bounds = fold_non_dependent_expr (bounds);
88e95ee3
MM
11499 /* Normally, the array bound must be an integral constant
11500 expression. However, as an extension, we allow VLAs
c8094d83 11501 in function scopes. */
b671e5a4 11502 else if (!at_function_scope_p ())
44370687
MM
11503 {
11504 error ("array bound is not an integer constant");
11505 bounds = error_mark_node;
11506 }
14d22dd6 11507 }
a723baf1
MM
11508 else
11509 bounds = NULL_TREE;
11510 /* Look for the closing `]'. */
62b8a44e
NS
11511 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11512 {
058b15c1 11513 declarator = cp_error_declarator;
62b8a44e
NS
11514 break;
11515 }
a723baf1 11516
058b15c1 11517 declarator = make_array_declarator (declarator, bounds);
a723baf1 11518 }
62b8a44e 11519 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
a723baf1 11520 {
1d786913
MM
11521 tree qualifying_scope;
11522 tree unqualified_name;
d85d3d57 11523 special_function_kind sfk;
fa6098f8 11524 bool abstract_ok;
058b15c1 11525
a668c6ad 11526 /* Parse a declarator-id */
fa6098f8
MM
11527 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11528 if (abstract_ok)
62b8a44e 11529 cp_parser_parse_tentatively (parser);
3db45ab5 11530 unqualified_name
fa6098f8 11531 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
1d786913 11532 qualifying_scope = parser->scope;
fa6098f8 11533 if (abstract_ok)
712becab
NS
11534 {
11535 if (!cp_parser_parse_definitely (parser))
1d786913 11536 unqualified_name = error_mark_node;
fa6098f8
MM
11537 else if (unqualified_name
11538 && (qualifying_scope
11539 || (TREE_CODE (unqualified_name)
11540 != IDENTIFIER_NODE)))
712becab
NS
11541 {
11542 cp_parser_error (parser, "expected unqualified-id");
1d786913 11543 unqualified_name = error_mark_node;
712becab
NS
11544 }
11545 }
21526606 11546
fa6098f8
MM
11547 if (!unqualified_name)
11548 return NULL;
1d786913 11549 if (unqualified_name == error_mark_node)
058b15c1
MM
11550 {
11551 declarator = cp_error_declarator;
11552 break;
11553 }
21526606 11554
1d786913
MM
11555 if (qualifying_scope && at_namespace_scope_p ()
11556 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
62b8a44e 11557 {
62b8a44e 11558 /* In the declaration of a member of a template class
0cbd7506
MS
11559 outside of the class itself, the SCOPE will sometimes
11560 be a TYPENAME_TYPE. For example, given:
21526606 11561
0cbd7506
MS
11562 template <typename T>
11563 int S<T>::R::i = 3;
21526606 11564
0cbd7506
MS
11565 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11566 this context, we must resolve S<T>::R to an ordinary
11567 type, rather than a typename type.
21526606 11568
0cbd7506
MS
11569 The reason we normally avoid resolving TYPENAME_TYPEs
11570 is that a specialization of `S' might render
11571 `S<T>::R' not a type. However, if `S' is
11572 specialized, then this `i' will not be used, so there
11573 is no harm in resolving the types here. */
1d786913 11574 tree type;
c8094d83 11575
1d786913
MM
11576 /* Resolve the TYPENAME_TYPE. */
11577 type = resolve_typename_type (qualifying_scope,
11578 /*only_current_p=*/false);
11579 /* If that failed, the declarator is invalid. */
11580 if (type == error_mark_node)
11581 error ("%<%T::%D%> is not a type",
11582 TYPE_CONTEXT (qualifying_scope),
11583 TYPE_IDENTIFIER (qualifying_scope));
11584 qualifying_scope = type;
62b8a44e 11585 }
21526606 11586
d85d3d57 11587 sfk = sfk_none;
1d786913 11588 if (unqualified_name)
a723baf1 11589 {
62b8a44e
NS
11590 tree class_type;
11591
1d786913
MM
11592 if (qualifying_scope
11593 && CLASS_TYPE_P (qualifying_scope))
11594 class_type = qualifying_scope;
62b8a44e 11595 else
1d786913 11596 class_type = current_class_type;
62b8a44e 11597
d85d3d57 11598 if (TREE_CODE (unqualified_name) == TYPE_DECL)
058b15c1 11599 {
0e686aa6
MM
11600 tree name_type = TREE_TYPE (unqualified_name);
11601 if (class_type && same_type_p (name_type, class_type))
058b15c1 11602 {
0e686aa6
MM
11603 if (qualifying_scope
11604 && CLASSTYPE_USE_TEMPLATE (name_type))
11605 {
11606 error ("invalid use of constructor as a template");
11607 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11608 "name the constructor in a qualified name",
11609 class_type,
11610 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11611 class_type, name_type);
11612 declarator = cp_error_declarator;
11613 break;
11614 }
11615 else
11616 unqualified_name = constructor_name (class_type);
d85d3d57 11617 }
d85d3d57
MM
11618 else
11619 {
11620 /* We do not attempt to print the declarator
11621 here because we do not have enough
11622 information about its original syntactic
11623 form. */
9e281320 11624 cp_parser_error (parser, "invalid declarator");
d85d3d57
MM
11625 declarator = cp_error_declarator;
11626 break;
058b15c1
MM
11627 }
11628 }
d85d3d57
MM
11629
11630 if (class_type)
11631 {
11632 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11633 sfk = sfk_destructor;
11634 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11635 sfk = sfk_conversion;
11636 else if (/* There's no way to declare a constructor
11637 for an anonymous type, even if the type
11638 got a name for linkage purposes. */
11639 !TYPE_WAS_ANONYMOUS (class_type)
11640 && constructor_name_p (unqualified_name,
11641 class_type))
11642 {
11643 unqualified_name = constructor_name (class_type);
11644 sfk = sfk_constructor;
11645 }
11646
11647 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11648 *ctor_dtor_or_conv_p = -1;
11649 }
a723baf1 11650 }
3db45ab5 11651 declarator = make_id_declarator (qualifying_scope,
d85d3d57
MM
11652 unqualified_name,
11653 sfk);
11654 declarator->id_loc = token->location;
62b8a44e
NS
11655
11656 handle_declarator:;
11657 scope = get_scope_of_declarator (declarator);
11658 if (scope)
91b004e5
MM
11659 /* Any names that appear after the declarator-id for a
11660 member are looked up in the containing scope. */
4514aa8c 11661 pushed_scope = push_scope (scope);
62b8a44e
NS
11662 parser->in_declarator_p = true;
11663 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
058b15c1 11664 || (declarator && declarator->kind == cdk_id))
62b8a44e
NS
11665 /* Default args are only allowed on function
11666 declarations. */
11667 parser->default_arg_ok_p = saved_default_arg_ok_p;
a723baf1 11668 else
62b8a44e
NS
11669 parser->default_arg_ok_p = false;
11670
11671 first = false;
a723baf1 11672 }
62b8a44e 11673 /* We're done. */
a723baf1
MM
11674 else
11675 break;
a723baf1
MM
11676 }
11677
11678 /* For an abstract declarator, we might wind up with nothing at this
11679 point. That's an error; the declarator is not optional. */
11680 if (!declarator)
11681 cp_parser_error (parser, "expected declarator");
11682
11683 /* If we entered a scope, we must exit it now. */
4514aa8c
NS
11684 if (pushed_scope)
11685 pop_scope (pushed_scope);
a723baf1
MM
11686
11687 parser->default_arg_ok_p = saved_default_arg_ok_p;
11688 parser->in_declarator_p = saved_in_declarator_p;
21526606 11689
a723baf1
MM
11690 return declarator;
11691}
11692
21526606 11693/* Parse a ptr-operator.
a723baf1
MM
11694
11695 ptr-operator:
11696 * cv-qualifier-seq [opt]
11697 &
11698 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11699
11700 GNU Extension:
11701
11702 ptr-operator:
11703 & cv-qualifier-seq [opt]
11704
3c01e5df
MM
11705 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11706 Returns ADDR_EXPR if a reference was used. In the case of a
11707 pointer-to-member, *TYPE is filled in with the TYPE containing the
11708 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11709 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11710 ERROR_MARK if an error occurred. */
21526606 11711
a723baf1 11712static enum tree_code
21526606 11713cp_parser_ptr_operator (cp_parser* parser,
0cbd7506 11714 tree* type,
3c01e5df 11715 cp_cv_quals *cv_quals)
a723baf1
MM
11716{
11717 enum tree_code code = ERROR_MARK;
11718 cp_token *token;
11719
11720 /* Assume that it's not a pointer-to-member. */
11721 *type = NULL_TREE;
11722 /* And that there are no cv-qualifiers. */
3c01e5df 11723 *cv_quals = TYPE_UNQUALIFIED;
a723baf1
MM
11724
11725 /* Peek at the next token. */
11726 token = cp_lexer_peek_token (parser->lexer);
11727 /* If it's a `*' or `&' we have a pointer or reference. */
11728 if (token->type == CPP_MULT || token->type == CPP_AND)
11729 {
11730 /* Remember which ptr-operator we were processing. */
11731 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11732
11733 /* Consume the `*' or `&'. */
11734 cp_lexer_consume_token (parser->lexer);
11735
11736 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11737 `&', if we are allowing GNU extensions. (The only qualifier
11738 that can legally appear after `&' is `restrict', but that is
11739 enforced during semantic analysis. */
21526606 11740 if (code == INDIRECT_REF
a723baf1 11741 || cp_parser_allow_gnu_extensions_p (parser))
3c01e5df 11742 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
a723baf1
MM
11743 }
11744 else
11745 {
11746 /* Try the pointer-to-member case. */
11747 cp_parser_parse_tentatively (parser);
11748 /* Look for the optional `::' operator. */
11749 cp_parser_global_scope_opt (parser,
11750 /*current_scope_valid_p=*/false);
11751 /* Look for the nested-name specifier. */
11752 cp_parser_nested_name_specifier (parser,
11753 /*typename_keyword_p=*/false,
11754 /*check_dependency_p=*/true,
a668c6ad
MM
11755 /*type_p=*/false,
11756 /*is_declaration=*/false);
a723baf1
MM
11757 /* If we found it, and the next token is a `*', then we are
11758 indeed looking at a pointer-to-member operator. */
11759 if (!cp_parser_error_occurred (parser)
11760 && cp_parser_require (parser, CPP_MULT, "`*'"))
11761 {
a723baf1
MM
11762 /* Indicate that the `*' operator was used. */
11763 code = INDIRECT_REF;
63c9a190
MM
11764
11765 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11766 error ("%qD is a namespace", parser->scope);
11767 else
11768 {
11769 /* The type of which the member is a member is given by the
11770 current SCOPE. */
11771 *type = parser->scope;
11772 /* The next name will not be qualified. */
11773 parser->scope = NULL_TREE;
11774 parser->qualifying_scope = NULL_TREE;
11775 parser->object_scope = NULL_TREE;
11776 /* Look for the optional cv-qualifier-seq. */
11777 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11778 }
a723baf1
MM
11779 }
11780 /* If that didn't work we don't have a ptr-operator. */
11781 if (!cp_parser_parse_definitely (parser))
11782 cp_parser_error (parser, "expected ptr-operator");
11783 }
11784
11785 return code;
11786}
11787
11788/* Parse an (optional) cv-qualifier-seq.
11789
11790 cv-qualifier-seq:
21526606 11791 cv-qualifier cv-qualifier-seq [opt]
a723baf1 11792
a723baf1
MM
11793 cv-qualifier:
11794 const
21526606 11795 volatile
a723baf1
MM
11796
11797 GNU Extension:
11798
11799 cv-qualifier:
98ca843c 11800 __restrict__
a723baf1 11801
3c01e5df
MM
11802 Returns a bitmask representing the cv-qualifiers. */
11803
11804static cp_cv_quals
11805cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
a723baf1 11806{
3c01e5df 11807 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
a723baf1 11808
3c01e5df 11809 while (true)
a723baf1 11810 {
3c01e5df
MM
11811 cp_token *token;
11812 cp_cv_quals cv_qualifier;
98ca843c 11813
3c01e5df
MM
11814 /* Peek at the next token. */
11815 token = cp_lexer_peek_token (parser->lexer);
11816 /* See if it's a cv-qualifier. */
11817 switch (token->keyword)
11818 {
11819 case RID_CONST:
11820 cv_qualifier = TYPE_QUAL_CONST;
11821 break;
98ca843c 11822
3c01e5df
MM
11823 case RID_VOLATILE:
11824 cv_qualifier = TYPE_QUAL_VOLATILE;
11825 break;
98ca843c 11826
3c01e5df
MM
11827 case RID_RESTRICT:
11828 cv_qualifier = TYPE_QUAL_RESTRICT;
11829 break;
98ca843c 11830
3c01e5df
MM
11831 default:
11832 cv_qualifier = TYPE_UNQUALIFIED;
11833 break;
11834 }
98ca843c 11835
3c01e5df
MM
11836 if (!cv_qualifier)
11837 break;
a723baf1 11838
3c01e5df
MM
11839 if (cv_quals & cv_qualifier)
11840 {
11841 error ("duplicate cv-qualifier");
11842 cp_lexer_purge_token (parser->lexer);
11843 }
11844 else
11845 {
11846 cp_lexer_consume_token (parser->lexer);
11847 cv_quals |= cv_qualifier;
11848 }
a723baf1
MM
11849 }
11850
3c01e5df 11851 return cv_quals;
a723baf1
MM
11852}
11853
11854/* Parse a declarator-id.
11855
11856 declarator-id:
11857 id-expression
21526606 11858 :: [opt] nested-name-specifier [opt] type-name
a723baf1
MM
11859
11860 In the `id-expression' case, the value returned is as for
11861 cp_parser_id_expression if the id-expression was an unqualified-id.
11862 If the id-expression was a qualified-id, then a SCOPE_REF is
11863 returned. The first operand is the scope (either a NAMESPACE_DECL
11864 or TREE_TYPE), but the second is still just a representation of an
11865 unqualified-id. */
11866
11867static tree
fa6098f8 11868cp_parser_declarator_id (cp_parser* parser, bool optional_p)
a723baf1 11869{
d85d3d57 11870 tree id;
a723baf1
MM
11871 /* The expression must be an id-expression. Assume that qualified
11872 names are the names of types so that:
11873
11874 template <class T>
11875 int S<T>::R::i = 3;
11876
11877 will work; we must treat `S<T>::R' as the name of a type.
11878 Similarly, assume that qualified names are templates, where
11879 required, so that:
11880
11881 template <class T>
11882 int S<T>::R<T>::i = 3;
11883
11884 will work, too. */
d85d3d57
MM
11885 id = cp_parser_id_expression (parser,
11886 /*template_keyword_p=*/false,
11887 /*check_dependency_p=*/false,
11888 /*template_p=*/NULL,
fa6098f8
MM
11889 /*declarator_p=*/true,
11890 optional_p);
11891 if (id && BASELINK_P (id))
d85d3d57
MM
11892 id = BASELINK_FUNCTIONS (id);
11893 return id;
a723baf1
MM
11894}
11895
11896/* Parse a type-id.
11897
11898 type-id:
11899 type-specifier-seq abstract-declarator [opt]
11900
11901 Returns the TYPE specified. */
11902
11903static tree
94edc4ab 11904cp_parser_type_id (cp_parser* parser)
a723baf1 11905{
62d1db17 11906 cp_decl_specifier_seq type_specifier_seq;
058b15c1 11907 cp_declarator *abstract_declarator;
a723baf1
MM
11908
11909 /* Parse the type-specifier-seq. */
d4113656
MM
11910 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11911 &type_specifier_seq);
62d1db17 11912 if (type_specifier_seq.type == error_mark_node)
a723baf1
MM
11913 return error_mark_node;
11914
11915 /* There might or might not be an abstract declarator. */
11916 cp_parser_parse_tentatively (parser);
11917 /* Look for the declarator. */
21526606 11918 abstract_declarator
4bb8ca28 11919 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
db86dd14
MM
11920 /*parenthesized_p=*/NULL,
11921 /*member_p=*/false);
a723baf1
MM
11922 /* Check to see if there really was a declarator. */
11923 if (!cp_parser_parse_definitely (parser))
058b15c1 11924 abstract_declarator = NULL;
a723baf1 11925
62d1db17 11926 return groktypename (&type_specifier_seq, abstract_declarator);
a723baf1
MM
11927}
11928
11929/* Parse a type-specifier-seq.
11930
11931 type-specifier-seq:
11932 type-specifier type-specifier-seq [opt]
11933
11934 GNU extension:
11935
11936 type-specifier-seq:
11937 attributes type-specifier-seq [opt]
11938
d4113656
MM
11939 If IS_CONDITION is true, we are at the start of a "condition",
11940 e.g., we've just seen "if (".
11941
62d1db17 11942 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
a723baf1 11943
62d1db17
MM
11944static void
11945cp_parser_type_specifier_seq (cp_parser* parser,
d4113656 11946 bool is_condition,
62d1db17 11947 cp_decl_specifier_seq *type_specifier_seq)
a723baf1
MM
11948{
11949 bool seen_type_specifier = false;
d4113656 11950 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
62d1db17
MM
11951
11952 /* Clear the TYPE_SPECIFIER_SEQ. */
11953 clear_decl_specs (type_specifier_seq);
a723baf1
MM
11954
11955 /* Parse the type-specifiers and attributes. */
11956 while (true)
11957 {
11958 tree type_specifier;
d4113656 11959 bool is_cv_qualifier;
a723baf1
MM
11960
11961 /* Check for attributes first. */
11962 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11963 {
98ca843c 11964 type_specifier_seq->attributes =
62d1db17
MM
11965 chainon (type_specifier_seq->attributes,
11966 cp_parser_attributes_opt (parser));
a723baf1
MM
11967 continue;
11968 }
11969
a723baf1 11970 /* Look for the type-specifier. */
21526606 11971 type_specifier = cp_parser_type_specifier (parser,
d4113656 11972 flags,
62d1db17 11973 type_specifier_seq,
a723baf1
MM
11974 /*is_declaration=*/false,
11975 NULL,
d4113656
MM
11976 &is_cv_qualifier);
11977 if (!type_specifier)
62d1db17 11978 {
d4113656
MM
11979 /* If the first type-specifier could not be found, this is not a
11980 type-specifier-seq at all. */
11981 if (!seen_type_specifier)
11982 {
11983 cp_parser_error (parser, "expected type-specifier");
11984 type_specifier_seq->type = error_mark_node;
11985 return;
11986 }
11987 /* If subsequent type-specifiers could not be found, the
11988 type-specifier-seq is complete. */
11989 break;
62d1db17 11990 }
a723baf1 11991
a723baf1 11992 seen_type_specifier = true;
d4113656
MM
11993 /* The standard says that a condition can be:
11994
0cbd7506 11995 type-specifier-seq declarator = assignment-expression
c8094d83 11996
d4113656
MM
11997 However, given:
11998
11999 struct S {};
12000 if (int S = ...)
12001
0cbd7506
MS
12002 we should treat the "S" as a declarator, not as a
12003 type-specifier. The standard doesn't say that explicitly for
12004 type-specifier-seq, but it does say that for
12005 decl-specifier-seq in an ordinary declaration. Perhaps it
12006 would be clearer just to allow a decl-specifier-seq here, and
12007 then add a semantic restriction that if any decl-specifiers
12008 that are not type-specifiers appear, the program is invalid. */
d4113656 12009 if (is_condition && !is_cv_qualifier)
c8094d83 12010 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
a723baf1 12011 }
856367df
VR
12012
12013 cp_parser_check_decl_spec (type_specifier_seq);
a723baf1
MM
12014}
12015
12016/* Parse a parameter-declaration-clause.
12017
12018 parameter-declaration-clause:
12019 parameter-declaration-list [opt] ... [opt]
12020 parameter-declaration-list , ...
12021
058b15c1
MM
12022 Returns a representation for the parameter declarations. A return
12023 value of NULL indicates a parameter-declaration-clause consisting
12024 only of an ellipsis. */
a723baf1 12025
058b15c1 12026static cp_parameter_declarator *
94edc4ab 12027cp_parser_parameter_declaration_clause (cp_parser* parser)
a723baf1 12028{
058b15c1 12029 cp_parameter_declarator *parameters;
a723baf1
MM
12030 cp_token *token;
12031 bool ellipsis_p;
058b15c1 12032 bool is_error;
a723baf1
MM
12033
12034 /* Peek at the next token. */
12035 token = cp_lexer_peek_token (parser->lexer);
12036 /* Check for trivial parameter-declaration-clauses. */
12037 if (token->type == CPP_ELLIPSIS)
12038 {
12039 /* Consume the `...' token. */
12040 cp_lexer_consume_token (parser->lexer);
058b15c1 12041 return NULL;
a723baf1
MM
12042 }
12043 else if (token->type == CPP_CLOSE_PAREN)
12044 /* There are no parameters. */
c73aecdf
DE
12045 {
12046#ifndef NO_IMPLICIT_EXTERN_C
12047 if (in_system_header && current_class_type == NULL
12048 && current_lang_name == lang_name_c)
058b15c1 12049 return NULL;
c73aecdf
DE
12050 else
12051#endif
058b15c1 12052 return no_parameters;
c73aecdf 12053 }
a723baf1
MM
12054 /* Check for `(void)', too, which is a special case. */
12055 else if (token->keyword == RID_VOID
21526606 12056 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1
MM
12057 == CPP_CLOSE_PAREN))
12058 {
12059 /* Consume the `void' token. */
12060 cp_lexer_consume_token (parser->lexer);
12061 /* There are no parameters. */
058b15c1 12062 return no_parameters;
a723baf1 12063 }
21526606 12064
a723baf1 12065 /* Parse the parameter-declaration-list. */
058b15c1 12066 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
a723baf1
MM
12067 /* If a parse error occurred while parsing the
12068 parameter-declaration-list, then the entire
12069 parameter-declaration-clause is erroneous. */
058b15c1
MM
12070 if (is_error)
12071 return NULL;
a723baf1
MM
12072
12073 /* Peek at the next token. */
12074 token = cp_lexer_peek_token (parser->lexer);
12075 /* If it's a `,', the clause should terminate with an ellipsis. */
12076 if (token->type == CPP_COMMA)
12077 {
12078 /* Consume the `,'. */
12079 cp_lexer_consume_token (parser->lexer);
12080 /* Expect an ellipsis. */
21526606 12081 ellipsis_p
a723baf1
MM
12082 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12083 }
21526606 12084 /* It might also be `...' if the optional trailing `,' was
a723baf1
MM
12085 omitted. */
12086 else if (token->type == CPP_ELLIPSIS)
12087 {
12088 /* Consume the `...' token. */
12089 cp_lexer_consume_token (parser->lexer);
12090 /* And remember that we saw it. */
12091 ellipsis_p = true;
12092 }
12093 else
12094 ellipsis_p = false;
12095
12096 /* Finish the parameter list. */
058b15c1
MM
12097 if (parameters && ellipsis_p)
12098 parameters->ellipsis_p = true;
98ca843c 12099
058b15c1 12100 return parameters;
a723baf1
MM
12101}
12102
12103/* Parse a parameter-declaration-list.
12104
12105 parameter-declaration-list:
12106 parameter-declaration
12107 parameter-declaration-list , parameter-declaration
12108
12109 Returns a representation of the parameter-declaration-list, as for
12110 cp_parser_parameter_declaration_clause. However, the
058b15c1
MM
12111 `void_list_node' is never appended to the list. Upon return,
12112 *IS_ERROR will be true iff an error occurred. */
a723baf1 12113
058b15c1
MM
12114static cp_parameter_declarator *
12115cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
a723baf1 12116{
058b15c1
MM
12117 cp_parameter_declarator *parameters = NULL;
12118 cp_parameter_declarator **tail = &parameters;
75a82978 12119 bool saved_in_unbraced_linkage_specification_p;
058b15c1
MM
12120
12121 /* Assume all will go well. */
12122 *is_error = false;
75a82978
MM
12123 /* The special considerations that apply to a function within an
12124 unbraced linkage specifications do not apply to the parameters
12125 to the function. */
12126 saved_in_unbraced_linkage_specification_p
12127 = parser->in_unbraced_linkage_specification_p;
12128 parser->in_unbraced_linkage_specification_p = false;
a723baf1
MM
12129
12130 /* Look for more parameters. */
12131 while (true)
12132 {
058b15c1 12133 cp_parameter_declarator *parameter;
4bb8ca28 12134 bool parenthesized_p;
a723baf1 12135 /* Parse the parameter. */
21526606
EC
12136 parameter
12137 = cp_parser_parameter_declaration (parser,
4bb8ca28
MM
12138 /*template_parm_p=*/false,
12139 &parenthesized_p);
ec194454 12140
34cd5ae7 12141 /* If a parse error occurred parsing the parameter declaration,
a723baf1 12142 then the entire parameter-declaration-list is erroneous. */
058b15c1 12143 if (!parameter)
a723baf1 12144 {
058b15c1
MM
12145 *is_error = true;
12146 parameters = NULL;
a723baf1
MM
12147 break;
12148 }
12149 /* Add the new parameter to the list. */
058b15c1
MM
12150 *tail = parameter;
12151 tail = &parameter->next;
a723baf1
MM
12152
12153 /* Peek at the next token. */
12154 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
e58a9aa1
ZL
12155 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12156 /* These are for Objective-C++ */
12157 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12158 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
a723baf1
MM
12159 /* The parameter-declaration-list is complete. */
12160 break;
12161 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12162 {
12163 cp_token *token;
12164
12165 /* Peek at the next token. */
12166 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12167 /* If it's an ellipsis, then the list is complete. */
12168 if (token->type == CPP_ELLIPSIS)
12169 break;
12170 /* Otherwise, there must be more parameters. Consume the
12171 `,'. */
12172 cp_lexer_consume_token (parser->lexer);
4bb8ca28
MM
12173 /* When parsing something like:
12174
0cbd7506 12175 int i(float f, double d)
21526606 12176
0cbd7506 12177 we can tell after seeing the declaration for "f" that we
4bb8ca28 12178 are not looking at an initialization of a variable "i",
21526606 12179 but rather at the declaration of a function "i".
4bb8ca28
MM
12180
12181 Due to the fact that the parsing of template arguments
12182 (as specified to a template-id) requires backtracking we
12183 cannot use this technique when inside a template argument
12184 list. */
12185 if (!parser->in_template_argument_list_p
4d5fe289 12186 && !parser->in_type_id_in_expr_p
0b16f8f4 12187 && cp_parser_uncommitted_to_tentative_parse_p (parser)
4bb8ca28
MM
12188 /* However, a parameter-declaration of the form
12189 "foat(f)" (which is a valid declaration of a
12190 parameter "f") can also be interpreted as an
12191 expression (the conversion of "f" to "float"). */
12192 && !parenthesized_p)
12193 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
12194 }
12195 else
12196 {
2a13a625 12197 cp_parser_error (parser, "expected %<,%> or %<...%>");
0b16f8f4 12198 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21526606 12199 cp_parser_skip_to_closing_parenthesis (parser,
4bb8ca28 12200 /*recovering=*/true,
5c832178 12201 /*or_comma=*/false,
4bb8ca28 12202 /*consume_paren=*/false);
a723baf1
MM
12203 break;
12204 }
12205 }
12206
75a82978
MM
12207 parser->in_unbraced_linkage_specification_p
12208 = saved_in_unbraced_linkage_specification_p;
12209
058b15c1 12210 return parameters;
a723baf1
MM
12211}
12212
12213/* Parse a parameter declaration.
12214
12215 parameter-declaration:
12216 decl-specifier-seq declarator
12217 decl-specifier-seq declarator = assignment-expression
12218 decl-specifier-seq abstract-declarator [opt]
12219 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12220
ec194454
MM
12221 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12222 declares a template parameter. (In that case, a non-nested `>'
12223 token encountered during the parsing of the assignment-expression
12224 is not interpreted as a greater-than operator.)
a723baf1 12225
058b15c1
MM
12226 Returns a representation of the parameter, or NULL if an error
12227 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12228 true iff the declarator is of the form "(p)". */
a723baf1 12229
058b15c1 12230static cp_parameter_declarator *
21526606 12231cp_parser_parameter_declaration (cp_parser *parser,
4bb8ca28
MM
12232 bool template_parm_p,
12233 bool *parenthesized_p)
a723baf1 12234{
560ad596 12235 int declares_class_or_enum;
ec194454 12236 bool greater_than_is_operator_p;
62d1db17 12237 cp_decl_specifier_seq decl_specifiers;
058b15c1 12238 cp_declarator *declarator;
a723baf1 12239 tree default_argument;
a723baf1
MM
12240 cp_token *token;
12241 const char *saved_message;
12242
ec194454
MM
12243 /* In a template parameter, `>' is not an operator.
12244
12245 [temp.param]
12246
12247 When parsing a default template-argument for a non-type
12248 template-parameter, the first non-nested `>' is taken as the end
12249 of the template parameter-list rather than a greater-than
12250 operator. */
12251 greater_than_is_operator_p = !template_parm_p;
12252
a723baf1
MM
12253 /* Type definitions may not appear in parameter types. */
12254 saved_message = parser->type_definition_forbidden_message;
21526606 12255 parser->type_definition_forbidden_message
a723baf1
MM
12256 = "types may not be defined in parameter types";
12257
12258 /* Parse the declaration-specifiers. */
62d1db17
MM
12259 cp_parser_decl_specifier_seq (parser,
12260 CP_PARSER_FLAGS_NONE,
12261 &decl_specifiers,
12262 &declares_class_or_enum);
a723baf1
MM
12263 /* If an error occurred, there's no reason to attempt to parse the
12264 rest of the declaration. */
12265 if (cp_parser_error_occurred (parser))
12266 {
12267 parser->type_definition_forbidden_message = saved_message;
058b15c1 12268 return NULL;
a723baf1
MM
12269 }
12270
12271 /* Peek at the next token. */
12272 token = cp_lexer_peek_token (parser->lexer);
12273 /* If the next token is a `)', `,', `=', `>', or `...', then there
12274 is no declarator. */
21526606 12275 if (token->type == CPP_CLOSE_PAREN
a723baf1
MM
12276 || token->type == CPP_COMMA
12277 || token->type == CPP_EQ
12278 || token->type == CPP_ELLIPSIS
12279 || token->type == CPP_GREATER)
4bb8ca28 12280 {
058b15c1 12281 declarator = NULL;
4bb8ca28
MM
12282 if (parenthesized_p)
12283 *parenthesized_p = false;
12284 }
a723baf1
MM
12285 /* Otherwise, there should be a declarator. */
12286 else
12287 {
12288 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12289 parser->default_arg_ok_p = false;
21526606 12290
5c832178
MM
12291 /* After seeing a decl-specifier-seq, if the next token is not a
12292 "(", there is no possibility that the code is a valid
4f8163b1
MM
12293 expression. Therefore, if parsing tentatively, we commit at
12294 this point. */
5c832178 12295 if (!parser->in_template_argument_list_p
643aee72 12296 /* In an expression context, having seen:
4f8163b1 12297
a7324e75 12298 (int((char ...
4f8163b1
MM
12299
12300 we cannot be sure whether we are looking at a
a7324e75
MM
12301 function-type (taking a "char" as a parameter) or a cast
12302 of some object of type "char" to "int". */
4f8163b1 12303 && !parser->in_type_id_in_expr_p
0b16f8f4 12304 && cp_parser_uncommitted_to_tentative_parse_p (parser)
5c832178
MM
12305 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12306 cp_parser_commit_to_tentative_parse (parser);
12307 /* Parse the declarator. */
a723baf1 12308 declarator = cp_parser_declarator (parser,
62b8a44e 12309 CP_PARSER_DECLARATOR_EITHER,
4bb8ca28 12310 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
12311 parenthesized_p,
12312 /*member_p=*/false);
a723baf1 12313 parser->default_arg_ok_p = saved_default_arg_ok_p;
4971227d 12314 /* After the declarator, allow more attributes. */
62d1db17 12315 decl_specifiers.attributes
98ca843c 12316 = chainon (decl_specifiers.attributes,
62d1db17 12317 cp_parser_attributes_opt (parser));
a723baf1
MM
12318 }
12319
62b8a44e 12320 /* The restriction on defining new types applies only to the type
a723baf1
MM
12321 of the parameter, not to the default argument. */
12322 parser->type_definition_forbidden_message = saved_message;
12323
12324 /* If the next token is `=', then process a default argument. */
12325 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12326 {
12327 bool saved_greater_than_is_operator_p;
12328 /* Consume the `='. */
12329 cp_lexer_consume_token (parser->lexer);
12330
12331 /* If we are defining a class, then the tokens that make up the
12332 default argument must be saved and processed later. */
21526606 12333 if (!template_parm_p && at_class_scope_p ()
ec194454 12334 && TYPE_BEING_DEFINED (current_class_type))
a723baf1
MM
12335 {
12336 unsigned depth = 0;
c162c75e
MA
12337 cp_token *first_token;
12338 cp_token *token;
a723baf1
MM
12339
12340 /* Add tokens until we have processed the entire default
03fd3f84 12341 argument. We add the range [first_token, token). */
c162c75e 12342 first_token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
12343 while (true)
12344 {
12345 bool done = false;
a723baf1
MM
12346
12347 /* Peek at the next token. */
12348 token = cp_lexer_peek_token (parser->lexer);
12349 /* What we do depends on what token we have. */
12350 switch (token->type)
12351 {
12352 /* In valid code, a default argument must be
12353 immediately followed by a `,' `)', or `...'. */
12354 case CPP_COMMA:
12355 case CPP_CLOSE_PAREN:
12356 case CPP_ELLIPSIS:
12357 /* If we run into a non-nested `;', `}', or `]',
12358 then the code is invalid -- but the default
12359 argument is certainly over. */
12360 case CPP_SEMICOLON:
12361 case CPP_CLOSE_BRACE:
12362 case CPP_CLOSE_SQUARE:
12363 if (depth == 0)
12364 done = true;
12365 /* Update DEPTH, if necessary. */
12366 else if (token->type == CPP_CLOSE_PAREN
12367 || token->type == CPP_CLOSE_BRACE
12368 || token->type == CPP_CLOSE_SQUARE)
12369 --depth;
12370 break;
12371
12372 case CPP_OPEN_PAREN:
12373 case CPP_OPEN_SQUARE:
12374 case CPP_OPEN_BRACE:
12375 ++depth;
12376 break;
12377
12378 case CPP_GREATER:
12379 /* If we see a non-nested `>', and `>' is not an
12380 operator, then it marks the end of the default
12381 argument. */
12382 if (!depth && !greater_than_is_operator_p)
12383 done = true;
12384 break;
12385
12386 /* If we run out of tokens, issue an error message. */
12387 case CPP_EOF:
bc4071dd 12388 case CPP_PRAGMA_EOL:
a723baf1
MM
12389 error ("file ends in default argument");
12390 done = true;
12391 break;
12392
12393 case CPP_NAME:
12394 case CPP_SCOPE:
12395 /* In these cases, we should look for template-ids.
21526606 12396 For example, if the default argument is
a723baf1
MM
12397 `X<int, double>()', we need to do name lookup to
12398 figure out whether or not `X' is a template; if
34cd5ae7 12399 so, the `,' does not end the default argument.
a723baf1
MM
12400
12401 That is not yet done. */
12402 break;
12403
12404 default:
12405 break;
12406 }
12407
12408 /* If we've reached the end, stop. */
12409 if (done)
12410 break;
21526606 12411
a723baf1
MM
12412 /* Add the token to the token block. */
12413 token = cp_lexer_consume_token (parser->lexer);
a723baf1 12414 }
c162c75e
MA
12415
12416 /* Create a DEFAULT_ARG to represented the unparsed default
0cbd7506 12417 argument. */
c162c75e
MA
12418 default_argument = make_node (DEFAULT_ARG);
12419 DEFARG_TOKENS (default_argument)
01ea1ea8
NS
12420 = cp_token_cache_new (first_token, token);
12421 DEFARG_INSTANTIATIONS (default_argument) = NULL;
a723baf1
MM
12422 }
12423 /* Outside of a class definition, we can just parse the
0cbd7506 12424 assignment-expression. */
a723baf1
MM
12425 else
12426 {
12427 bool saved_local_variables_forbidden_p;
12428
12429 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12430 set correctly. */
21526606 12431 saved_greater_than_is_operator_p
a723baf1
MM
12432 = parser->greater_than_is_operator_p;
12433 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12434 /* Local variable names (and the `this' keyword) may not
12435 appear in a default argument. */
21526606 12436 saved_local_variables_forbidden_p
a723baf1
MM
12437 = parser->local_variables_forbidden_p;
12438 parser->local_variables_forbidden_p = true;
5a8613b2
MM
12439 /* The default argument expression may cause implicitly
12440 defined member functions to be synthesized, which will
12441 result in garbage collection. We must treat this
12442 situation as if we were within the body of function so as
12443 to avoid collecting live data on the stack. */
12444 ++function_depth;
a723baf1 12445 /* Parse the assignment-expression. */
6b648482
MM
12446 if (template_parm_p)
12447 push_deferring_access_checks (dk_no_deferred);
c8094d83 12448 default_argument
93678513 12449 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6b648482
MM
12450 if (template_parm_p)
12451 pop_deferring_access_checks ();
a723baf1 12452 /* Restore saved state. */
5a8613b2 12453 --function_depth;
21526606 12454 parser->greater_than_is_operator_p
a723baf1 12455 = saved_greater_than_is_operator_p;
21526606
EC
12456 parser->local_variables_forbidden_p
12457 = saved_local_variables_forbidden_p;
a723baf1
MM
12458 }
12459 if (!parser->default_arg_ok_p)
12460 {
c67d36d0 12461 if (!flag_pedantic_errors)
d4ee4d25 12462 warning (0, "deprecated use of default argument for parameter of non-function");
c67d36d0
NS
12463 else
12464 {
12465 error ("default arguments are only permitted for function parameters");
12466 default_argument = NULL_TREE;
12467 }
a723baf1
MM
12468 }
12469 }
12470 else
12471 default_argument = NULL_TREE;
21526606 12472
62d1db17 12473 return make_parameter_declarator (&decl_specifiers,
058b15c1
MM
12474 declarator,
12475 default_argument);
a723baf1
MM
12476}
12477
a723baf1
MM
12478/* Parse a function-body.
12479
12480 function-body:
12481 compound_statement */
12482
12483static void
12484cp_parser_function_body (cp_parser *parser)
12485{
325c3691 12486 cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
12487}
12488
12489/* Parse a ctor-initializer-opt followed by a function-body. Return
12490 true if a ctor-initializer was present. */
12491
12492static bool
12493cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12494{
12495 tree body;
12496 bool ctor_initializer_p;
12497
12498 /* Begin the function body. */
12499 body = begin_function_body ();
12500 /* Parse the optional ctor-initializer. */
12501 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12502 /* Parse the function-body. */
12503 cp_parser_function_body (parser);
12504 /* Finish the function body. */
12505 finish_function_body (body);
12506
12507 return ctor_initializer_p;
12508}
12509
12510/* Parse an initializer.
12511
12512 initializer:
12513 = initializer-clause
21526606 12514 ( expression-list )
a723baf1 12515
c72a1a86 12516 Returns an expression representing the initializer. If no
21526606 12517 initializer is present, NULL_TREE is returned.
a723baf1
MM
12518
12519 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12520 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
39703eb9
MM
12521 set to FALSE if there is no initializer present. If there is an
12522 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12523 is set to true; otherwise it is set to false. */
a723baf1
MM
12524
12525static tree
39703eb9
MM
12526cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12527 bool* non_constant_p)
a723baf1
MM
12528{
12529 cp_token *token;
12530 tree init;
12531
12532 /* Peek at the next token. */
12533 token = cp_lexer_peek_token (parser->lexer);
12534
12535 /* Let our caller know whether or not this initializer was
12536 parenthesized. */
12537 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
39703eb9
MM
12538 /* Assume that the initializer is constant. */
12539 *non_constant_p = false;
a723baf1
MM
12540
12541 if (token->type == CPP_EQ)
12542 {
12543 /* Consume the `='. */
12544 cp_lexer_consume_token (parser->lexer);
12545 /* Parse the initializer-clause. */
39703eb9 12546 init = cp_parser_initializer_clause (parser, non_constant_p);
a723baf1
MM
12547 }
12548 else if (token->type == CPP_OPEN_PAREN)
39703eb9 12549 init = cp_parser_parenthesized_expression_list (parser, false,
93678513 12550 /*cast_p=*/false,
39703eb9 12551 non_constant_p);
a723baf1
MM
12552 else
12553 {
12554 /* Anything else is an error. */
12555 cp_parser_error (parser, "expected initializer");
12556 init = error_mark_node;
12557 }
12558
12559 return init;
12560}
12561
21526606 12562/* Parse an initializer-clause.
a723baf1
MM
12563
12564 initializer-clause:
12565 assignment-expression
12566 { initializer-list , [opt] }
12567 { }
12568
21526606 12569 Returns an expression representing the initializer.
a723baf1
MM
12570
12571 If the `assignment-expression' production is used the value
21526606 12572 returned is simply a representation for the expression.
a723baf1
MM
12573
12574 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
4038c495 12575 the elements of the initializer-list (or NULL, if the last
a723baf1
MM
12576 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12577 NULL_TREE. There is no way to detect whether or not the optional
39703eb9
MM
12578 trailing `,' was provided. NON_CONSTANT_P is as for
12579 cp_parser_initializer. */
a723baf1
MM
12580
12581static tree
39703eb9 12582cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
12583{
12584 tree initializer;
12585
b2802a4b
R
12586 /* Assume the expression is constant. */
12587 *non_constant_p = false;
12588
a723baf1
MM
12589 /* If it is not a `{', then we are looking at an
12590 assignment-expression. */
12591 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
0da99d4e 12592 {
98ca843c 12593 initializer
0da99d4e
GB
12594 = cp_parser_constant_expression (parser,
12595 /*allow_non_constant_p=*/true,
12596 non_constant_p);
12597 if (!*non_constant_p)
12598 initializer = fold_non_dependent_expr (initializer);
12599 }
a723baf1
MM
12600 else
12601 {
12602 /* Consume the `{' token. */
12603 cp_lexer_consume_token (parser->lexer);
12604 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12605 initializer = make_node (CONSTRUCTOR);
a723baf1
MM
12606 /* If it's not a `}', then there is a non-trivial initializer. */
12607 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12608 {
12609 /* Parse the initializer list. */
12610 CONSTRUCTOR_ELTS (initializer)
39703eb9 12611 = cp_parser_initializer_list (parser, non_constant_p);
a723baf1
MM
12612 /* A trailing `,' token is allowed. */
12613 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12614 cp_lexer_consume_token (parser->lexer);
12615 }
a723baf1
MM
12616 /* Now, there should be a trailing `}'. */
12617 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12618 }
12619
12620 return initializer;
12621}
12622
12623/* Parse an initializer-list.
12624
12625 initializer-list:
12626 initializer-clause
12627 initializer-list , initializer-clause
12628
12629 GNU Extension:
21526606 12630
a723baf1
MM
12631 initializer-list:
12632 identifier : initializer-clause
12633 initializer-list, identifier : initializer-clause
12634
4038c495
GB
12635 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12636 for the initializer. If the INDEX of the elt is non-NULL, it is the
39703eb9
MM
12637 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12638 as for cp_parser_initializer. */
a723baf1 12639
4038c495 12640static VEC(constructor_elt,gc) *
39703eb9 12641cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
a723baf1 12642{
4038c495 12643 VEC(constructor_elt,gc) *v = NULL;
a723baf1 12644
39703eb9
MM
12645 /* Assume all of the expressions are constant. */
12646 *non_constant_p = false;
12647
a723baf1
MM
12648 /* Parse the rest of the list. */
12649 while (true)
12650 {
12651 cp_token *token;
12652 tree identifier;
12653 tree initializer;
39703eb9
MM
12654 bool clause_non_constant_p;
12655
a723baf1
MM
12656 /* If the next token is an identifier and the following one is a
12657 colon, we are looking at the GNU designated-initializer
12658 syntax. */
12659 if (cp_parser_allow_gnu_extensions_p (parser)
12660 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12661 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12662 {
12663 /* Consume the identifier. */
12664 identifier = cp_lexer_consume_token (parser->lexer)->value;
12665 /* Consume the `:'. */
12666 cp_lexer_consume_token (parser->lexer);
12667 }
12668 else
12669 identifier = NULL_TREE;
12670
12671 /* Parse the initializer. */
21526606 12672 initializer = cp_parser_initializer_clause (parser,
39703eb9
MM
12673 &clause_non_constant_p);
12674 /* If any clause is non-constant, so is the entire initializer. */
12675 if (clause_non_constant_p)
12676 *non_constant_p = true;
4038c495
GB
12677
12678 /* Add it to the vector. */
12679 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
a723baf1
MM
12680
12681 /* If the next token is not a comma, we have reached the end of
12682 the list. */
12683 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12684 break;
12685
12686 /* Peek at the next token. */
12687 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12688 /* If the next token is a `}', then we're still done. An
12689 initializer-clause can have a trailing `,' after the
12690 initializer-list and before the closing `}'. */
12691 if (token->type == CPP_CLOSE_BRACE)
12692 break;
12693
12694 /* Consume the `,' token. */
12695 cp_lexer_consume_token (parser->lexer);
12696 }
12697
4038c495 12698 return v;
a723baf1
MM
12699}
12700
12701/* Classes [gram.class] */
12702
12703/* Parse a class-name.
12704
12705 class-name:
12706 identifier
12707 template-id
12708
12709 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12710 to indicate that names looked up in dependent types should be
12711 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12712 keyword has been used to indicate that the name that appears next
fc6a28d7
MM
12713 is a template. TAG_TYPE indicates the explicit tag given before
12714 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12715 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12716 is the class being defined in a class-head.
a723baf1
MM
12717
12718 Returns the TYPE_DECL representing the class. */
12719
12720static tree
21526606
EC
12721cp_parser_class_name (cp_parser *parser,
12722 bool typename_keyword_p,
12723 bool template_keyword_p,
fc6a28d7 12724 enum tag_types tag_type,
a723baf1 12725 bool check_dependency_p,
a668c6ad
MM
12726 bool class_head_p,
12727 bool is_declaration)
a723baf1
MM
12728{
12729 tree decl;
12730 tree scope;
12731 bool typename_p;
e5976695
MM
12732 cp_token *token;
12733
12734 /* All class-names start with an identifier. */
12735 token = cp_lexer_peek_token (parser->lexer);
12736 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12737 {
12738 cp_parser_error (parser, "expected class-name");
12739 return error_mark_node;
12740 }
21526606 12741
a723baf1
MM
12742 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12743 to a template-id, so we save it here. */
12744 scope = parser->scope;
3adee96c
KL
12745 if (scope == error_mark_node)
12746 return error_mark_node;
21526606 12747
a723baf1
MM
12748 /* Any name names a type if we're following the `typename' keyword
12749 in a qualified name where the enclosing scope is type-dependent. */
12750 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
1fb3244a 12751 && dependent_type_p (scope));
e5976695
MM
12752 /* Handle the common case (an identifier, but not a template-id)
12753 efficiently. */
21526606 12754 if (token->type == CPP_NAME
f4abade9 12755 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
a723baf1 12756 {
91b1ca65 12757 cp_token *identifier_token;
a723baf1 12758 tree identifier;
91b1ca65 12759 bool ambiguous_p;
a723baf1
MM
12760
12761 /* Look for the identifier. */
91b1ca65
MM
12762 identifier_token = cp_lexer_peek_token (parser->lexer);
12763 ambiguous_p = identifier_token->ambiguous_p;
a723baf1
MM
12764 identifier = cp_parser_identifier (parser);
12765 /* If the next token isn't an identifier, we are certainly not
12766 looking at a class-name. */
12767 if (identifier == error_mark_node)
12768 decl = error_mark_node;
12769 /* If we know this is a type-name, there's no need to look it
12770 up. */
12771 else if (typename_p)
12772 decl = identifier;
12773 else
12774 {
91b1ca65
MM
12775 tree ambiguous_decls;
12776 /* If we already know that this lookup is ambiguous, then
12777 we've already issued an error message; there's no reason
12778 to check again. */
12779 if (ambiguous_p)
12780 {
12781 cp_parser_simulate_error (parser);
12782 return error_mark_node;
12783 }
a723baf1
MM
12784 /* If the next token is a `::', then the name must be a type
12785 name.
12786
12787 [basic.lookup.qual]
12788
12789 During the lookup for a name preceding the :: scope
12790 resolution operator, object, function, and enumerator
12791 names are ignored. */
12792 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
fc6a28d7 12793 tag_type = typename_type;
a723baf1 12794 /* Look up the name. */
21526606 12795 decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 12796 tag_type,
b0bc6e8e 12797 /*is_template=*/false,
eea9800f 12798 /*is_namespace=*/false,
8f78f01f 12799 check_dependency_p,
91b1ca65
MM
12800 &ambiguous_decls);
12801 if (ambiguous_decls)
12802 {
12803 error ("reference to %qD is ambiguous", identifier);
12804 print_candidates (ambiguous_decls);
12805 if (cp_parser_parsing_tentatively (parser))
12806 {
12807 identifier_token->ambiguous_p = true;
12808 cp_parser_simulate_error (parser);
12809 }
12810 return error_mark_node;
12811 }
a723baf1
MM
12812 }
12813 }
e5976695
MM
12814 else
12815 {
12816 /* Try a template-id. */
12817 decl = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
12818 check_dependency_p,
12819 is_declaration);
e5976695
MM
12820 if (decl == error_mark_node)
12821 return error_mark_node;
12822 }
a723baf1
MM
12823
12824 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12825
12826 /* If this is a typename, create a TYPENAME_TYPE. */
12827 if (typename_p && decl != error_mark_node)
4bfb8bba 12828 {
8da15291 12829 decl = make_typename_type (scope, decl, typename_type,
3db45ab5 12830 /*complain=*/tf_error);
4bfb8bba
MM
12831 if (decl != error_mark_node)
12832 decl = TYPE_NAME (decl);
12833 }
a723baf1
MM
12834
12835 /* Check to see that it is really the name of a class. */
21526606 12836 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
a723baf1
MM
12837 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12838 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12839 /* Situations like this:
12840
12841 template <typename T> struct A {
21526606 12842 typename T::template X<int>::I i;
a723baf1
MM
12843 };
12844
12845 are problematic. Is `T::template X<int>' a class-name? The
12846 standard does not seem to be definitive, but there is no other
12847 valid interpretation of the following `::'. Therefore, those
12848 names are considered class-names. */
a723baf1 12849 {
94d285a5
VR
12850 decl = make_typename_type (scope, decl, tag_type, tf_error);
12851 if (decl != error_mark_node)
12852 decl = TYPE_NAME (decl);
a723baf1 12853 }
94d285a5
VR
12854 else if (TREE_CODE (decl) != TYPE_DECL
12855 || TREE_TYPE (decl) == error_mark_node
12856 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12857 decl = error_mark_node;
12858
12859 if (decl == error_mark_node)
12860 cp_parser_error (parser, "expected class-name");
a723baf1
MM
12861
12862 return decl;
12863}
12864
12865/* Parse a class-specifier.
12866
12867 class-specifier:
12868 class-head { member-specification [opt] }
12869
12870 Returns the TREE_TYPE representing the class. */
12871
12872static tree
94edc4ab 12873cp_parser_class_specifier (cp_parser* parser)
a723baf1
MM
12874{
12875 cp_token *token;
12876 tree type;
6de9cd9a 12877 tree attributes = NULL_TREE;
a723baf1
MM
12878 int has_trailing_semicolon;
12879 bool nested_name_specifier_p;
a723baf1 12880 unsigned saved_num_template_parameter_lists;
87c465f5 12881 tree old_scope = NULL_TREE;
2436b51f 12882 tree scope = NULL_TREE;
a723baf1 12883
8d241e0b 12884 push_deferring_access_checks (dk_no_deferred);
cf22909c 12885
a723baf1
MM
12886 /* Parse the class-head. */
12887 type = cp_parser_class_head (parser,
38b305d0
JM
12888 &nested_name_specifier_p,
12889 &attributes);
a723baf1
MM
12890 /* If the class-head was a semantic disaster, skip the entire body
12891 of the class. */
12892 if (!type)
12893 {
12894 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 12895 pop_deferring_access_checks ();
a723baf1
MM
12896 return error_mark_node;
12897 }
cf22909c 12898
a723baf1
MM
12899 /* Look for the `{'. */
12900 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
cf22909c
KL
12901 {
12902 pop_deferring_access_checks ();
12903 return error_mark_node;
12904 }
12905
a723baf1
MM
12906 /* Issue an error message if type-definitions are forbidden here. */
12907 cp_parser_check_type_definition (parser);
12908 /* Remember that we are defining one more class. */
12909 ++parser->num_classes_being_defined;
12910 /* Inside the class, surrounding template-parameter-lists do not
12911 apply. */
21526606
EC
12912 saved_num_template_parameter_lists
12913 = parser->num_template_parameter_lists;
a723baf1 12914 parser->num_template_parameter_lists = 0;
78757caa 12915
a723baf1 12916 /* Start the class. */
eeb23c11 12917 if (nested_name_specifier_p)
2436b51f
MM
12918 {
12919 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
87c465f5 12920 old_scope = push_inner_scope (scope);
2436b51f 12921 }
b9e75696 12922 type = begin_class_definition (type, attributes);
98ca843c 12923
a723baf1 12924 if (type == error_mark_node)
9bcb9aae 12925 /* If the type is erroneous, skip the entire body of the class. */
a723baf1
MM
12926 cp_parser_skip_to_closing_brace (parser);
12927 else
12928 /* Parse the member-specification. */
12929 cp_parser_member_specification_opt (parser);
98ca843c 12930
a723baf1
MM
12931 /* Look for the trailing `}'. */
12932 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12933 /* We get better error messages by noticing a common problem: a
12934 missing trailing `;'. */
12935 token = cp_lexer_peek_token (parser->lexer);
12936 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
38b305d0 12937 /* Look for trailing attributes to apply to this class. */
a723baf1 12938 if (cp_parser_allow_gnu_extensions_p (parser))
b9e75696 12939 attributes = cp_parser_attributes_opt (parser);
38b305d0
JM
12940 if (type != error_mark_node)
12941 type = finish_struct (type, attributes);
87c465f5
KL
12942 if (nested_name_specifier_p)
12943 pop_inner_scope (old_scope, scope);
a723baf1
MM
12944 /* If this class is not itself within the scope of another class,
12945 then we need to parse the bodies of all of the queued function
12946 definitions. Note that the queued functions defined in a class
12947 are not always processed immediately following the
12948 class-specifier for that class. Consider:
12949
12950 struct A {
0cbd7506 12951 struct B { void f() { sizeof (A); } };
a723baf1
MM
12952 };
12953
12954 If `f' were processed before the processing of `A' were
12955 completed, there would be no way to compute the size of `A'.
12956 Note that the nesting we are interested in here is lexical --
12957 not the semantic nesting given by TYPE_CONTEXT. In particular,
12958 for:
12959
12960 struct A { struct B; };
12961 struct A::B { void f() { } };
12962
12963 there is no need to delay the parsing of `A::B::f'. */
21526606 12964 if (--parser->num_classes_being_defined == 0)
a723baf1 12965 {
8218bd34
MM
12966 tree queue_entry;
12967 tree fn;
4514aa8c
NS
12968 tree class_type = NULL_TREE;
12969 tree pushed_scope = NULL_TREE;
3db45ab5 12970
8218bd34
MM
12971 /* In a first pass, parse default arguments to the functions.
12972 Then, in a second pass, parse the bodies of the functions.
12973 This two-phased approach handles cases like:
21526606
EC
12974
12975 struct S {
0cbd7506
MS
12976 void f() { g(); }
12977 void g(int i = 3);
12978 };
8218bd34 12979
0cbd7506 12980 */
8db1028e
NS
12981 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12982 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12983 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12984 TREE_PURPOSE (parser->unparsed_functions_queues)
12985 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
8218bd34
MM
12986 {
12987 fn = TREE_VALUE (queue_entry);
8218bd34
MM
12988 /* If there are default arguments that have not yet been processed,
12989 take care of them now. */
f44b0c8e
MM
12990 if (class_type != TREE_PURPOSE (queue_entry))
12991 {
4514aa8c
NS
12992 if (pushed_scope)
12993 pop_scope (pushed_scope);
f44b0c8e 12994 class_type = TREE_PURPOSE (queue_entry);
4514aa8c 12995 pushed_scope = push_scope (class_type);
f44b0c8e
MM
12996 }
12997 /* Make sure that any template parameters are in scope. */
12998 maybe_begin_member_template_processing (fn);
12999 /* Parse the default argument expressions. */
8218bd34
MM
13000 cp_parser_late_parsing_default_args (parser, fn);
13001 /* Remove any template parameters from the symbol table. */
13002 maybe_end_member_template_processing ();
13003 }
4514aa8c
NS
13004 if (pushed_scope)
13005 pop_scope (pushed_scope);
8218bd34 13006 /* Now parse the body of the functions. */
8db1028e
NS
13007 for (TREE_VALUE (parser->unparsed_functions_queues)
13008 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13009 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13010 TREE_VALUE (parser->unparsed_functions_queues)
13011 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
a723baf1 13012 {
a723baf1 13013 /* Figure out which function we need to process. */
a723baf1 13014 fn = TREE_VALUE (queue_entry);
a723baf1
MM
13015 /* Parse the function. */
13016 cp_parser_late_parsing_for_member (parser, fn);
a723baf1 13017 }
a723baf1
MM
13018 }
13019
13020 /* Put back any saved access checks. */
cf22909c 13021 pop_deferring_access_checks ();
a723baf1
MM
13022
13023 /* Restore the count of active template-parameter-lists. */
13024 parser->num_template_parameter_lists
13025 = saved_num_template_parameter_lists;
13026
13027 return type;
13028}
13029
13030/* Parse a class-head.
13031
13032 class-head:
13033 class-key identifier [opt] base-clause [opt]
13034 class-key nested-name-specifier identifier base-clause [opt]
21526606
EC
13035 class-key nested-name-specifier [opt] template-id
13036 base-clause [opt]
a723baf1
MM
13037
13038 GNU Extensions:
13039 class-key attributes identifier [opt] base-clause [opt]
13040 class-key attributes nested-name-specifier identifier base-clause [opt]
21526606
EC
13041 class-key attributes nested-name-specifier [opt] template-id
13042 base-clause [opt]
a723baf1
MM
13043
13044 Returns the TYPE of the indicated class. Sets
13045 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13046 involving a nested-name-specifier was used, and FALSE otherwise.
a723baf1 13047
55dcbc12 13048 Returns error_mark_node if this is not a class-head.
c8094d83 13049
a723baf1
MM
13050 Returns NULL_TREE if the class-head is syntactically valid, but
13051 semantically invalid in a way that means we should skip the entire
13052 body of the class. */
13053
13054static tree
21526606 13055cp_parser_class_head (cp_parser* parser,
38b305d0
JM
13056 bool* nested_name_specifier_p,
13057 tree *attributes_p)
a723baf1 13058{
a723baf1
MM
13059 tree nested_name_specifier;
13060 enum tag_types class_key;
13061 tree id = NULL_TREE;
13062 tree type = NULL_TREE;
13063 tree attributes;
13064 bool template_id_p = false;
13065 bool qualified_p = false;
13066 bool invalid_nested_name_p = false;
afb0918a 13067 bool invalid_explicit_specialization_p = false;
4514aa8c 13068 tree pushed_scope = NULL_TREE;
a723baf1 13069 unsigned num_templates;
cad7e87b 13070 tree bases;
a723baf1
MM
13071
13072 /* Assume no nested-name-specifier will be present. */
13073 *nested_name_specifier_p = false;
13074 /* Assume no template parameter lists will be used in defining the
13075 type. */
13076 num_templates = 0;
13077
13078 /* Look for the class-key. */
13079 class_key = cp_parser_class_key (parser);
13080 if (class_key == none_type)
13081 return error_mark_node;
13082
13083 /* Parse the attributes. */
13084 attributes = cp_parser_attributes_opt (parser);
13085
13086 /* If the next token is `::', that is invalid -- but sometimes
13087 people do try to write:
13088
21526606 13089 struct ::S {};
a723baf1
MM
13090
13091 Handle this gracefully by accepting the extra qualifier, and then
13092 issuing an error about it later if this really is a
2050a1bb 13093 class-head. If it turns out just to be an elaborated type
a723baf1
MM
13094 specifier, remain silent. */
13095 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13096 qualified_p = true;
13097
8d241e0b
KL
13098 push_deferring_access_checks (dk_no_check);
13099
a723baf1
MM
13100 /* Determine the name of the class. Begin by looking for an
13101 optional nested-name-specifier. */
21526606 13102 nested_name_specifier
a723baf1
MM
13103 = cp_parser_nested_name_specifier_opt (parser,
13104 /*typename_keyword_p=*/false,
66d418e6 13105 /*check_dependency_p=*/false,
a668c6ad
MM
13106 /*type_p=*/false,
13107 /*is_declaration=*/false);
a723baf1
MM
13108 /* If there was a nested-name-specifier, then there *must* be an
13109 identifier. */
13110 if (nested_name_specifier)
13111 {
13112 /* Although the grammar says `identifier', it really means
13113 `class-name' or `template-name'. You are only allowed to
13114 define a class that has already been declared with this
21526606 13115 syntax.
a723baf1 13116
ddd0b831 13117 The proposed resolution for Core Issue 180 says that wherever
a723baf1 13118 you see `class T::X' you should treat `X' as a type-name.
21526606 13119
a723baf1 13120 It is OK to define an inaccessible class; for example:
21526606 13121
0cbd7506
MS
13122 class A { class B; };
13123 class A::B {};
21526606 13124
0cbd7506 13125 We do not know if we will see a class-name, or a
a723baf1
MM
13126 template-name. We look for a class-name first, in case the
13127 class-name is a template-id; if we looked for the
13128 template-name first we would stop after the template-name. */
13129 cp_parser_parse_tentatively (parser);
13130 type = cp_parser_class_name (parser,
13131 /*typename_keyword_p=*/false,
13132 /*template_keyword_p=*/false,
fc6a28d7 13133 class_type,
a723baf1 13134 /*check_dependency_p=*/false,
a668c6ad
MM
13135 /*class_head_p=*/true,
13136 /*is_declaration=*/false);
a723baf1
MM
13137 /* If that didn't work, ignore the nested-name-specifier. */
13138 if (!cp_parser_parse_definitely (parser))
13139 {
13140 invalid_nested_name_p = true;
13141 id = cp_parser_identifier (parser);
13142 if (id == error_mark_node)
13143 id = NULL_TREE;
13144 }
13145 /* If we could not find a corresponding TYPE, treat this
13146 declaration like an unqualified declaration. */
13147 if (type == error_mark_node)
13148 nested_name_specifier = NULL_TREE;
13149 /* Otherwise, count the number of templates used in TYPE and its
13150 containing scopes. */
21526606 13151 else
a723baf1
MM
13152 {
13153 tree scope;
13154
21526606 13155 for (scope = TREE_TYPE (type);
a723baf1 13156 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21526606 13157 scope = (TYPE_P (scope)
a723baf1 13158 ? TYPE_CONTEXT (scope)
21526606
EC
13159 : DECL_CONTEXT (scope)))
13160 if (TYPE_P (scope)
a723baf1
MM
13161 && CLASS_TYPE_P (scope)
13162 && CLASSTYPE_TEMPLATE_INFO (scope)
2050a1bb
MM
13163 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13164 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
a723baf1
MM
13165 ++num_templates;
13166 }
13167 }
13168 /* Otherwise, the identifier is optional. */
13169 else
13170 {
13171 /* We don't know whether what comes next is a template-id,
13172 an identifier, or nothing at all. */
13173 cp_parser_parse_tentatively (parser);
13174 /* Check for a template-id. */
21526606 13175 id = cp_parser_template_id (parser,
a723baf1 13176 /*template_keyword_p=*/false,
a668c6ad
MM
13177 /*check_dependency_p=*/true,
13178 /*is_declaration=*/true);
a723baf1
MM
13179 /* If that didn't work, it could still be an identifier. */
13180 if (!cp_parser_parse_definitely (parser))
13181 {
13182 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13183 id = cp_parser_identifier (parser);
13184 else
13185 id = NULL_TREE;
13186 }
13187 else
13188 {
13189 template_id_p = true;
13190 ++num_templates;
13191 }
13192 }
13193
8d241e0b
KL
13194 pop_deferring_access_checks ();
13195
15077df5
MM
13196 if (id)
13197 cp_parser_check_for_invalid_template_id (parser, id);
ee43dab5 13198
a723baf1
MM
13199 /* If it's not a `:' or a `{' then we can't really be looking at a
13200 class-head, since a class-head only appears as part of a
13201 class-specifier. We have to detect this situation before calling
13202 xref_tag, since that has irreversible side-effects. */
13203 if (!cp_parser_next_token_starts_class_definition_p (parser))
13204 {
2a13a625 13205 cp_parser_error (parser, "expected %<{%> or %<:%>");
a723baf1
MM
13206 return error_mark_node;
13207 }
13208
13209 /* At this point, we're going ahead with the class-specifier, even
13210 if some other problem occurs. */
13211 cp_parser_commit_to_tentative_parse (parser);
13212 /* Issue the error about the overly-qualified name now. */
13213 if (qualified_p)
13214 cp_parser_error (parser,
13215 "global qualification of class name is invalid");
13216 else if (invalid_nested_name_p)
13217 cp_parser_error (parser,
13218 "qualified name does not name a class");
88081599
MM
13219 else if (nested_name_specifier)
13220 {
13221 tree scope;
9bf0e588
VR
13222
13223 /* Reject typedef-names in class heads. */
13224 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13225 {
13226 error ("invalid class name in declaration of %qD", type);
13227 type = NULL_TREE;
13228 goto done;
13229 }
13230
88081599
MM
13231 /* Figure out in what scope the declaration is being placed. */
13232 scope = current_scope ();
88081599
MM
13233 /* If that scope does not contain the scope in which the
13234 class was originally declared, the program is invalid. */
13235 if (scope && !is_ancestor (scope, nested_name_specifier))
13236 {
2a13a625 13237 error ("declaration of %qD in %qD which does not enclose %qD",
0cbd7506 13238 type, scope, nested_name_specifier);
88081599
MM
13239 type = NULL_TREE;
13240 goto done;
13241 }
13242 /* [dcl.meaning]
13243
0cbd7506 13244 A declarator-id shall not be qualified exception of the
88081599
MM
13245 definition of a ... nested class outside of its class
13246 ... [or] a the definition or explicit instantiation of a
13247 class member of a namespace outside of its namespace. */
13248 if (scope == nested_name_specifier)
13249 {
13250 pedwarn ("extra qualification ignored");
13251 nested_name_specifier = NULL_TREE;
13252 num_templates = 0;
13253 }
13254 }
afb0918a
MM
13255 /* An explicit-specialization must be preceded by "template <>". If
13256 it is not, try to recover gracefully. */
21526606 13257 if (at_namespace_scope_p ()
afb0918a 13258 && parser->num_template_parameter_lists == 0
eeb23c11 13259 && template_id_p)
afb0918a 13260 {
2a13a625 13261 error ("an explicit specialization must be preceded by %<template <>%>");
afb0918a
MM
13262 invalid_explicit_specialization_p = true;
13263 /* Take the same action that would have been taken by
13264 cp_parser_explicit_specialization. */
13265 ++parser->num_template_parameter_lists;
13266 begin_specialization ();
13267 }
13268 /* There must be no "return" statements between this point and the
13269 end of this function; set "type "to the correct return value and
13270 use "goto done;" to return. */
a723baf1
MM
13271 /* Make sure that the right number of template parameters were
13272 present. */
13273 if (!cp_parser_check_template_parameters (parser, num_templates))
afb0918a
MM
13274 {
13275 /* If something went wrong, there is no point in even trying to
13276 process the class-definition. */
13277 type = NULL_TREE;
13278 goto done;
13279 }
a723baf1 13280
a723baf1
MM
13281 /* Look up the type. */
13282 if (template_id_p)
13283 {
13284 type = TREE_TYPE (id);
13285 maybe_process_partial_specialization (type);
4514aa8c
NS
13286 if (nested_name_specifier)
13287 pushed_scope = push_scope (nested_name_specifier);
a723baf1 13288 }
4514aa8c 13289 else if (nested_name_specifier)
a723baf1 13290 {
a723baf1
MM
13291 tree class_type;
13292
13293 /* Given:
13294
13295 template <typename T> struct S { struct T };
14d22dd6 13296 template <typename T> struct S<T>::T { };
a723baf1
MM
13297
13298 we will get a TYPENAME_TYPE when processing the definition of
13299 `S::T'. We need to resolve it to the actual type before we
13300 try to define it. */
13301 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13302 {
14d22dd6
MM
13303 class_type = resolve_typename_type (TREE_TYPE (type),
13304 /*only_current_p=*/false);
13305 if (class_type != error_mark_node)
13306 type = TYPE_NAME (class_type);
13307 else
13308 {
13309 cp_parser_error (parser, "could not resolve typename type");
13310 type = error_mark_node;
13311 }
a723baf1
MM
13312 }
13313
560ad596
MM
13314 maybe_process_partial_specialization (TREE_TYPE (type));
13315 class_type = current_class_type;
13316 /* Enter the scope indicated by the nested-name-specifier. */
4514aa8c 13317 pushed_scope = push_scope (nested_name_specifier);
560ad596
MM
13318 /* Get the canonical version of this type. */
13319 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13320 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13321 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
55dcbc12
NS
13322 {
13323 type = push_template_decl (type);
13324 if (type == error_mark_node)
13325 {
13326 type = NULL_TREE;
13327 goto done;
13328 }
13329 }
c8094d83 13330
560ad596 13331 type = TREE_TYPE (type);
4514aa8c 13332 *nested_name_specifier_p = true;
a723baf1 13333 }
4514aa8c
NS
13334 else /* The name is not a nested name. */
13335 {
13336 /* If the class was unnamed, create a dummy name. */
13337 if (!id)
13338 id = make_anon_name ();
13339 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13340 parser->num_template_parameter_lists);
13341 }
13342
a723baf1
MM
13343 /* Indicate whether this class was declared as a `class' or as a
13344 `struct'. */
13345 if (TREE_CODE (type) == RECORD_TYPE)
13346 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13347 cp_parser_check_class_key (class_key, type);
13348
744b12b6
MM
13349 /* If this type was already complete, and we see another definition,
13350 that's an error. */
13351 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13352 {
13353 error ("redefinition of %q#T", type);
dee15844 13354 error ("previous definition of %q+#T", type);
0f3744f8
VR
13355 type = NULL_TREE;
13356 goto done;
744b12b6
MM
13357 }
13358
4514aa8c 13359 /* We will have entered the scope containing the class; the names of
744b12b6 13360 base classes should be looked up in that context. For example:
a723baf1
MM
13361
13362 struct A { struct B {}; struct C; };
13363 struct A::C : B {};
13364
13365 is valid. */
cad7e87b 13366 bases = NULL_TREE;
98ca843c 13367
cad7e87b
NS
13368 /* Get the list of base-classes, if there is one. */
13369 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13370 bases = cp_parser_base_clause (parser);
98ca843c 13371
cad7e87b
NS
13372 /* Process the base classes. */
13373 xref_basetypes (type, bases);
a723baf1 13374
4514aa8c 13375 done:
a723baf1
MM
13376 /* Leave the scope given by the nested-name-specifier. We will
13377 enter the class scope itself while processing the members. */
4514aa8c
NS
13378 if (pushed_scope)
13379 pop_scope (pushed_scope);
a723baf1 13380
afb0918a
MM
13381 if (invalid_explicit_specialization_p)
13382 {
13383 end_specialization ();
13384 --parser->num_template_parameter_lists;
13385 }
38b305d0 13386 *attributes_p = attributes;
a723baf1
MM
13387 return type;
13388}
13389
13390/* Parse a class-key.
13391
13392 class-key:
13393 class
13394 struct
13395 union
13396
13397 Returns the kind of class-key specified, or none_type to indicate
13398 error. */
13399
13400static enum tag_types
94edc4ab 13401cp_parser_class_key (cp_parser* parser)
a723baf1
MM
13402{
13403 cp_token *token;
13404 enum tag_types tag_type;
13405
13406 /* Look for the class-key. */
13407 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13408 if (!token)
13409 return none_type;
13410
13411 /* Check to see if the TOKEN is a class-key. */
13412 tag_type = cp_parser_token_is_class_key (token);
13413 if (!tag_type)
13414 cp_parser_error (parser, "expected class-key");
13415 return tag_type;
13416}
13417
13418/* Parse an (optional) member-specification.
13419
13420 member-specification:
13421 member-declaration member-specification [opt]
13422 access-specifier : member-specification [opt] */
13423
13424static void
94edc4ab 13425cp_parser_member_specification_opt (cp_parser* parser)
a723baf1
MM
13426{
13427 while (true)
13428 {
13429 cp_token *token;
13430 enum rid keyword;
13431
13432 /* Peek at the next token. */
13433 token = cp_lexer_peek_token (parser->lexer);
13434 /* If it's a `}', or EOF then we've seen all the members. */
bc4071dd
RH
13435 if (token->type == CPP_CLOSE_BRACE
13436 || token->type == CPP_EOF
13437 || token->type == CPP_PRAGMA_EOL)
a723baf1
MM
13438 break;
13439
13440 /* See if this token is a keyword. */
13441 keyword = token->keyword;
13442 switch (keyword)
13443 {
13444 case RID_PUBLIC:
13445 case RID_PROTECTED:
13446 case RID_PRIVATE:
13447 /* Consume the access-specifier. */
13448 cp_lexer_consume_token (parser->lexer);
13449 /* Remember which access-specifier is active. */
13450 current_access_specifier = token->value;
13451 /* Look for the `:'. */
13452 cp_parser_require (parser, CPP_COLON, "`:'");
13453 break;
13454
13455 default:
de3fe73c
MM
13456 /* Accept #pragmas at class scope. */
13457 if (token->type == CPP_PRAGMA)
13458 {
bc4071dd 13459 cp_parser_pragma (parser, pragma_external);
de3fe73c
MM
13460 break;
13461 }
13462
a723baf1
MM
13463 /* Otherwise, the next construction must be a
13464 member-declaration. */
13465 cp_parser_member_declaration (parser);
a723baf1
MM
13466 }
13467 }
13468}
13469
21526606 13470/* Parse a member-declaration.
a723baf1
MM
13471
13472 member-declaration:
13473 decl-specifier-seq [opt] member-declarator-list [opt] ;
13474 function-definition ; [opt]
13475 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13476 using-declaration
21526606 13477 template-declaration
a723baf1
MM
13478
13479 member-declarator-list:
13480 member-declarator
13481 member-declarator-list , member-declarator
13482
13483 member-declarator:
21526606 13484 declarator pure-specifier [opt]
a723baf1 13485 declarator constant-initializer [opt]
21526606 13486 identifier [opt] : constant-expression
a723baf1
MM
13487
13488 GNU Extensions:
13489
13490 member-declaration:
13491 __extension__ member-declaration
13492
13493 member-declarator:
13494 declarator attributes [opt] pure-specifier [opt]
13495 declarator attributes [opt] constant-initializer [opt]
13496 identifier [opt] attributes [opt] : constant-expression */
13497
13498static void
94edc4ab 13499cp_parser_member_declaration (cp_parser* parser)
a723baf1 13500{
62d1db17 13501 cp_decl_specifier_seq decl_specifiers;
a723baf1
MM
13502 tree prefix_attributes;
13503 tree decl;
560ad596 13504 int declares_class_or_enum;
a723baf1
MM
13505 bool friend_p;
13506 cp_token *token;
13507 int saved_pedantic;
13508
13509 /* Check for the `__extension__' keyword. */
13510 if (cp_parser_extension_opt (parser, &saved_pedantic))
13511 {
13512 /* Recurse. */
13513 cp_parser_member_declaration (parser);
13514 /* Restore the old value of the PEDANTIC flag. */
13515 pedantic = saved_pedantic;
13516
13517 return;
13518 }
13519
13520 /* Check for a template-declaration. */
13521 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13522 {
7e2a12d3
JC
13523 /* An explicit specialization here is an error condition, and we
13524 expect the specialization handler to detect and report this. */
13525 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13526 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13527 cp_parser_explicit_specialization (parser);
13528 else
13529 cp_parser_template_declaration (parser, /*member_p=*/true);
a723baf1
MM
13530
13531 return;
13532 }
13533
13534 /* Check for a using-declaration. */
13535 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13536 {
13537 /* Parse the using-declaration. */
13538 cp_parser_using_declaration (parser);
13539
13540 return;
13541 }
21526606 13542
e58a9aa1
ZL
13543 /* Check for @defs. */
13544 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13545 {
13546 tree ivar, member;
13547 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13548 ivar = ivar_chains;
13549 while (ivar)
13550 {
13551 member = ivar;
13552 ivar = TREE_CHAIN (member);
13553 TREE_CHAIN (member) = NULL_TREE;
13554 finish_member_declaration (member);
13555 }
13556 return;
13557 }
13558
a723baf1 13559 /* Parse the decl-specifier-seq. */
62d1db17
MM
13560 cp_parser_decl_specifier_seq (parser,
13561 CP_PARSER_FLAGS_OPTIONAL,
13562 &decl_specifiers,
13563 &declares_class_or_enum);
13564 prefix_attributes = decl_specifiers.attributes;
13565 decl_specifiers.attributes = NULL_TREE;
8fbc5ae7 13566 /* Check for an invalid type-name. */
de3fe73c
MM
13567 if (!decl_specifiers.type
13568 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8fbc5ae7 13569 return;
a723baf1
MM
13570 /* If there is no declarator, then the decl-specifier-seq should
13571 specify a type. */
13572 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13573 {
13574 /* If there was no decl-specifier-seq, and the next token is a
13575 `;', then we have something like:
13576
13577 struct S { ; };
13578
13579 [class.mem]
13580
13581 Each member-declaration shall declare at least one member
13582 name of the class. */
62d1db17 13583 if (!decl_specifiers.any_specifiers_p)
a723baf1 13584 {
2cfe82fe
ZW
13585 cp_token *token = cp_lexer_peek_token (parser->lexer);
13586 if (pedantic && !token->in_system_header)
13587 pedwarn ("%Hextra %<;%>", &token->location);
a723baf1 13588 }
21526606 13589 else
a723baf1
MM
13590 {
13591 tree type;
21526606 13592
a723baf1 13593 /* See if this declaration is a friend. */
62d1db17 13594 friend_p = cp_parser_friend_p (&decl_specifiers);
a723baf1
MM
13595 /* If there were decl-specifiers, check to see if there was
13596 a class-declaration. */
62d1db17 13597 type = check_tag_decl (&decl_specifiers);
a723baf1
MM
13598 /* Nested classes have already been added to the class, but
13599 a `friend' needs to be explicitly registered. */
13600 if (friend_p)
13601 {
13602 /* If the `friend' keyword was present, the friend must
13603 be introduced with a class-key. */
13604 if (!declares_class_or_enum)
13605 error ("a class-key must be used when declaring a friend");
13606 /* In this case:
13607
21526606 13608 template <typename T> struct A {
0cbd7506
MS
13609 friend struct A<T>::B;
13610 };
21526606 13611
a723baf1
MM
13612 A<T>::B will be represented by a TYPENAME_TYPE, and
13613 therefore not recognized by check_tag_decl. */
98ca843c 13614 if (!type
62d1db17
MM
13615 && decl_specifiers.type
13616 && TYPE_P (decl_specifiers.type))
13617 type = decl_specifiers.type;
fdd09134 13618 if (!type || !TYPE_P (type))
a723baf1
MM
13619 error ("friend declaration does not name a class or "
13620 "function");
13621 else
19db77ce
KL
13622 make_friend_class (current_class_type, type,
13623 /*complain=*/true);
a723baf1
MM
13624 }
13625 /* If there is no TYPE, an error message will already have
13626 been issued. */
62d1db17 13627 else if (!type || type == error_mark_node)
a723baf1
MM
13628 ;
13629 /* An anonymous aggregate has to be handled specially; such
13630 a declaration really declares a data member (with a
13631 particular type), as opposed to a nested class. */
13632 else if (ANON_AGGR_TYPE_P (type))
13633 {
13634 /* Remove constructors and such from TYPE, now that we
34cd5ae7 13635 know it is an anonymous aggregate. */
a723baf1
MM
13636 fixup_anonymous_aggr (type);
13637 /* And make the corresponding data member. */
13638 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13639 /* Add it to the class. */
13640 finish_member_declaration (decl);
13641 }
37d407a1
KL
13642 else
13643 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
a723baf1
MM
13644 }
13645 }
13646 else
13647 {
13648 /* See if these declarations will be friends. */
62d1db17 13649 friend_p = cp_parser_friend_p (&decl_specifiers);
a723baf1 13650
21526606 13651 /* Keep going until we hit the `;' at the end of the
a723baf1
MM
13652 declaration. */
13653 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13654 {
13655 tree attributes = NULL_TREE;
13656 tree first_attribute;
13657
13658 /* Peek at the next token. */
13659 token = cp_lexer_peek_token (parser->lexer);
13660
13661 /* Check for a bitfield declaration. */
13662 if (token->type == CPP_COLON
13663 || (token->type == CPP_NAME
21526606 13664 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1
MM
13665 == CPP_COLON))
13666 {
13667 tree identifier;
13668 tree width;
13669
13670 /* Get the name of the bitfield. Note that we cannot just
13671 check TOKEN here because it may have been invalidated by
13672 the call to cp_lexer_peek_nth_token above. */
13673 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13674 identifier = cp_parser_identifier (parser);
13675 else
13676 identifier = NULL_TREE;
13677
13678 /* Consume the `:' token. */
13679 cp_lexer_consume_token (parser->lexer);
13680 /* Get the width of the bitfield. */
21526606 13681 width
14d22dd6
MM
13682 = cp_parser_constant_expression (parser,
13683 /*allow_non_constant=*/false,
13684 NULL);
a723baf1
MM
13685
13686 /* Look for attributes that apply to the bitfield. */
13687 attributes = cp_parser_attributes_opt (parser);
13688 /* Remember which attributes are prefix attributes and
13689 which are not. */
13690 first_attribute = attributes;
13691 /* Combine the attributes. */
13692 attributes = chainon (prefix_attributes, attributes);
13693
13694 /* Create the bitfield declaration. */
98ca843c 13695 decl = grokbitfield (identifier
1d786913 13696 ? make_id_declarator (NULL_TREE,
d85d3d57
MM
13697 identifier,
13698 sfk_none)
058b15c1 13699 : NULL,
62d1db17 13700 &decl_specifiers,
a723baf1
MM
13701 width);
13702 /* Apply the attributes. */
13703 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13704 }
13705 else
13706 {
058b15c1 13707 cp_declarator *declarator;
a723baf1
MM
13708 tree initializer;
13709 tree asm_specification;
7efa3e22 13710 int ctor_dtor_or_conv_p;
a723baf1
MM
13711
13712 /* Parse the declarator. */
21526606 13713 declarator
62b8a44e 13714 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 13715 &ctor_dtor_or_conv_p,
db86dd14
MM
13716 /*parenthesized_p=*/NULL,
13717 /*member_p=*/true);
a723baf1
MM
13718
13719 /* If something went wrong parsing the declarator, make sure
13720 that we at least consume some tokens. */
058b15c1 13721 if (declarator == cp_error_declarator)
a723baf1
MM
13722 {
13723 /* Skip to the end of the statement. */
13724 cp_parser_skip_to_end_of_statement (parser);
4bb8ca28
MM
13725 /* If the next token is not a semicolon, that is
13726 probably because we just skipped over the body of
13727 a function. So, we consume a semicolon if
13728 present, but do not issue an error message if it
13729 is not present. */
13730 if (cp_lexer_next_token_is (parser->lexer,
13731 CPP_SEMICOLON))
13732 cp_lexer_consume_token (parser->lexer);
13733 return;
a723baf1
MM
13734 }
13735
fc6a28d7
MM
13736 if (declares_class_or_enum & 2)
13737 cp_parser_check_for_definition_in_return_type
13738 (declarator, decl_specifiers.type);
560ad596 13739
a723baf1
MM
13740 /* Look for an asm-specification. */
13741 asm_specification = cp_parser_asm_specification_opt (parser);
13742 /* Look for attributes that apply to the declaration. */
13743 attributes = cp_parser_attributes_opt (parser);
13744 /* Remember which attributes are prefix attributes and
13745 which are not. */
13746 first_attribute = attributes;
13747 /* Combine the attributes. */
13748 attributes = chainon (prefix_attributes, attributes);
13749
13750 /* If it's an `=', then we have a constant-initializer or a
13751 pure-specifier. It is not correct to parse the
13752 initializer before registering the member declaration
13753 since the member declaration should be in scope while
13754 its initializer is processed. However, the rest of the
13755 front end does not yet provide an interface that allows
13756 us to handle this correctly. */
13757 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13758 {
13759 /* In [class.mem]:
13760
13761 A pure-specifier shall be used only in the declaration of
21526606 13762 a virtual function.
a723baf1
MM
13763
13764 A member-declarator can contain a constant-initializer
13765 only if it declares a static member of integral or
21526606 13766 enumeration type.
a723baf1
MM
13767
13768 Therefore, if the DECLARATOR is for a function, we look
13769 for a pure-specifier; otherwise, we look for a
13770 constant-initializer. When we call `grokfield', it will
13771 perform more stringent semantics checks. */
63c9a190
MM
13772 if (declarator->kind == cdk_function
13773 && declarator->declarator->kind == cdk_id)
a723baf1
MM
13774 initializer = cp_parser_pure_specifier (parser);
13775 else
4bb8ca28
MM
13776 /* Parse the initializer. */
13777 initializer = cp_parser_constant_initializer (parser);
a723baf1
MM
13778 }
13779 /* Otherwise, there is no initializer. */
13780 else
13781 initializer = NULL_TREE;
13782
13783 /* See if we are probably looking at a function
5a19910e 13784 definition. We are certainly not looking at a
a723baf1
MM
13785 member-declarator. Calling `grokfield' has
13786 side-effects, so we must not do it unless we are sure
13787 that we are looking at a member-declarator. */
21526606 13788 if (cp_parser_token_starts_function_definition_p
a723baf1 13789 (cp_lexer_peek_token (parser->lexer)))
4bb8ca28
MM
13790 {
13791 /* The grammar does not allow a pure-specifier to be
13792 used when a member function is defined. (It is
13793 possible that this fact is an oversight in the
13794 standard, since a pure function may be defined
13795 outside of the class-specifier. */
13796 if (initializer)
13797 error ("pure-specifier on function-definition");
13798 decl = cp_parser_save_member_function_body (parser,
62d1db17 13799 &decl_specifiers,
4bb8ca28
MM
13800 declarator,
13801 attributes);
13802 /* If the member was not a friend, declare it here. */
13803 if (!friend_p)
13804 finish_member_declaration (decl);
13805 /* Peek at the next token. */
13806 token = cp_lexer_peek_token (parser->lexer);
13807 /* If the next token is a semicolon, consume it. */
13808 if (token->type == CPP_SEMICOLON)
13809 cp_lexer_consume_token (parser->lexer);
13810 return;
13811 }
a723baf1 13812 else
d174af6c
MM
13813 /* Create the declaration. */
13814 decl = grokfield (declarator, &decl_specifiers,
13815 initializer, /*init_const_expr_p=*/true,
13816 asm_specification,
13817 attributes);
a723baf1
MM
13818 }
13819
13820 /* Reset PREFIX_ATTRIBUTES. */
13821 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13822 attributes = TREE_CHAIN (attributes);
13823 if (attributes)
13824 TREE_CHAIN (attributes) = NULL_TREE;
13825
13826 /* If there is any qualification still in effect, clear it
13827 now; we will be starting fresh with the next declarator. */
13828 parser->scope = NULL_TREE;
13829 parser->qualifying_scope = NULL_TREE;
13830 parser->object_scope = NULL_TREE;
13831 /* If it's a `,', then there are more declarators. */
13832 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13833 cp_lexer_consume_token (parser->lexer);
13834 /* If the next token isn't a `;', then we have a parse error. */
13835 else if (cp_lexer_next_token_is_not (parser->lexer,
13836 CPP_SEMICOLON))
13837 {
2a13a625 13838 cp_parser_error (parser, "expected %<;%>");
04c06002 13839 /* Skip tokens until we find a `;'. */
a723baf1
MM
13840 cp_parser_skip_to_end_of_statement (parser);
13841
13842 break;
13843 }
13844
13845 if (decl)
13846 {
13847 /* Add DECL to the list of members. */
13848 if (!friend_p)
13849 finish_member_declaration (decl);
13850
a723baf1 13851 if (TREE_CODE (decl) == FUNCTION_DECL)
8db1028e 13852 cp_parser_save_default_args (parser, decl);
a723baf1
MM
13853 }
13854 }
13855 }
13856
4bb8ca28 13857 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
a723baf1
MM
13858}
13859
13860/* Parse a pure-specifier.
13861
13862 pure-specifier:
13863 = 0
13864
13865 Returns INTEGER_ZERO_NODE if a pure specifier is found.
cd0be382 13866 Otherwise, ERROR_MARK_NODE is returned. */
a723baf1
MM
13867
13868static tree
94edc4ab 13869cp_parser_pure_specifier (cp_parser* parser)
a723baf1
MM
13870{
13871 cp_token *token;
13872
13873 /* Look for the `=' token. */
13874 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13875 return error_mark_node;
13876 /* Look for the `0' token. */
515e6a84 13877 token = cp_lexer_consume_token (parser->lexer);
ab84748a 13878 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
ceacde63
MM
13879 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
13880 {
3db45ab5 13881 cp_parser_error (parser,
ceacde63
MM
13882 "invalid pure specifier (only `= 0' is allowed)");
13883 cp_parser_skip_to_end_of_statement (parser);
13884 return error_mark_node;
13885 }
13886 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
13887 {
13888 error ("templates may not be %<virtual%>");
13889 return error_mark_node;
13890 }
a723baf1 13891
ceacde63 13892 return integer_zero_node;
a723baf1
MM
13893}
13894
13895/* Parse a constant-initializer.
13896
13897 constant-initializer:
13898 = constant-expression
13899
13900 Returns a representation of the constant-expression. */
13901
13902static tree
94edc4ab 13903cp_parser_constant_initializer (cp_parser* parser)
a723baf1
MM
13904{
13905 /* Look for the `=' token. */
13906 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13907 return error_mark_node;
13908
13909 /* It is invalid to write:
13910
13911 struct S { static const int i = { 7 }; };
13912
13913 */
13914 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13915 {
13916 cp_parser_error (parser,
13917 "a brace-enclosed initializer is not allowed here");
13918 /* Consume the opening brace. */
13919 cp_lexer_consume_token (parser->lexer);
13920 /* Skip the initializer. */
13921 cp_parser_skip_to_closing_brace (parser);
13922 /* Look for the trailing `}'. */
13923 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
21526606 13924
a723baf1
MM
13925 return error_mark_node;
13926 }
13927
21526606 13928 return cp_parser_constant_expression (parser,
14d22dd6
MM
13929 /*allow_non_constant=*/false,
13930 NULL);
a723baf1
MM
13931}
13932
13933/* Derived classes [gram.class.derived] */
13934
13935/* Parse a base-clause.
13936
13937 base-clause:
21526606 13938 : base-specifier-list
a723baf1
MM
13939
13940 base-specifier-list:
13941 base-specifier
13942 base-specifier-list , base-specifier
13943
13944 Returns a TREE_LIST representing the base-classes, in the order in
13945 which they were declared. The representation of each node is as
21526606 13946 described by cp_parser_base_specifier.
a723baf1
MM
13947
13948 In the case that no bases are specified, this function will return
13949 NULL_TREE, not ERROR_MARK_NODE. */
13950
13951static tree
94edc4ab 13952cp_parser_base_clause (cp_parser* parser)
a723baf1
MM
13953{
13954 tree bases = NULL_TREE;
13955
13956 /* Look for the `:' that begins the list. */
13957 cp_parser_require (parser, CPP_COLON, "`:'");
13958
13959 /* Scan the base-specifier-list. */
13960 while (true)
13961 {
13962 cp_token *token;
13963 tree base;
13964
13965 /* Look for the base-specifier. */
13966 base = cp_parser_base_specifier (parser);
13967 /* Add BASE to the front of the list. */
13968 if (base != error_mark_node)
13969 {
13970 TREE_CHAIN (base) = bases;
13971 bases = base;
13972 }
13973 /* Peek at the next token. */
13974 token = cp_lexer_peek_token (parser->lexer);
13975 /* If it's not a comma, then the list is complete. */
13976 if (token->type != CPP_COMMA)
13977 break;
13978 /* Consume the `,'. */
13979 cp_lexer_consume_token (parser->lexer);
13980 }
13981
13982 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13983 base class had a qualified name. However, the next name that
13984 appears is certainly not qualified. */
13985 parser->scope = NULL_TREE;
13986 parser->qualifying_scope = NULL_TREE;
13987 parser->object_scope = NULL_TREE;
13988
13989 return nreverse (bases);
13990}
13991
13992/* Parse a base-specifier.
13993
13994 base-specifier:
13995 :: [opt] nested-name-specifier [opt] class-name
13996 virtual access-specifier [opt] :: [opt] nested-name-specifier
13997 [opt] class-name
13998 access-specifier virtual [opt] :: [opt] nested-name-specifier
13999 [opt] class-name
14000
14001 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14002 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14003 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14004 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21526606 14005
a723baf1 14006static tree
94edc4ab 14007cp_parser_base_specifier (cp_parser* parser)
a723baf1
MM
14008{
14009 cp_token *token;
14010 bool done = false;
14011 bool virtual_p = false;
14012 bool duplicate_virtual_error_issued_p = false;
14013 bool duplicate_access_error_issued_p = false;
bbaab916 14014 bool class_scope_p, template_p;
dbbf88d1 14015 tree access = access_default_node;
a723baf1
MM
14016 tree type;
14017
14018 /* Process the optional `virtual' and `access-specifier'. */
14019 while (!done)
14020 {
14021 /* Peek at the next token. */
14022 token = cp_lexer_peek_token (parser->lexer);
14023 /* Process `virtual'. */
14024 switch (token->keyword)
14025 {
14026 case RID_VIRTUAL:
14027 /* If `virtual' appears more than once, issue an error. */
14028 if (virtual_p && !duplicate_virtual_error_issued_p)
14029 {
14030 cp_parser_error (parser,
2a13a625 14031 "%<virtual%> specified more than once in base-specified");
a723baf1
MM
14032 duplicate_virtual_error_issued_p = true;
14033 }
14034
14035 virtual_p = true;
14036
14037 /* Consume the `virtual' token. */
14038 cp_lexer_consume_token (parser->lexer);
14039
14040 break;
14041
14042 case RID_PUBLIC:
14043 case RID_PROTECTED:
14044 case RID_PRIVATE:
14045 /* If more than one access specifier appears, issue an
14046 error. */
dbbf88d1
NS
14047 if (access != access_default_node
14048 && !duplicate_access_error_issued_p)
a723baf1
MM
14049 {
14050 cp_parser_error (parser,
14051 "more than one access specifier in base-specified");
14052 duplicate_access_error_issued_p = true;
14053 }
14054
dbbf88d1 14055 access = ridpointers[(int) token->keyword];
a723baf1
MM
14056
14057 /* Consume the access-specifier. */
14058 cp_lexer_consume_token (parser->lexer);
14059
14060 break;
14061
14062 default:
14063 done = true;
14064 break;
14065 }
14066 }
852dcbdd 14067 /* It is not uncommon to see programs mechanically, erroneously, use
a3a503a5 14068 the 'typename' keyword to denote (dependent) qualified types
1ed53ef3
GB
14069 as base classes. */
14070 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14071 {
14072 if (!processing_template_decl)
2a13a625 14073 error ("keyword %<typename%> not allowed outside of templates");
1ed53ef3 14074 else
2a13a625 14075 error ("keyword %<typename%> not allowed in this context "
1ed53ef3
GB
14076 "(the base class is implicitly a type)");
14077 cp_lexer_consume_token (parser->lexer);
14078 }
a723baf1 14079
a723baf1
MM
14080 /* Look for the optional `::' operator. */
14081 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14082 /* Look for the nested-name-specifier. The simplest way to
14083 implement:
14084
14085 [temp.res]
14086
14087 The keyword `typename' is not permitted in a base-specifier or
14088 mem-initializer; in these contexts a qualified name that
14089 depends on a template-parameter is implicitly assumed to be a
14090 type name.
14091
14092 is to pretend that we have seen the `typename' keyword at this
21526606 14093 point. */
a723baf1
MM
14094 cp_parser_nested_name_specifier_opt (parser,
14095 /*typename_keyword_p=*/true,
14096 /*check_dependency_p=*/true,
fc6a28d7 14097 typename_type,
a668c6ad 14098 /*is_declaration=*/true);
a723baf1
MM
14099 /* If the base class is given by a qualified name, assume that names
14100 we see are type names or templates, as appropriate. */
14101 class_scope_p = (parser->scope && TYPE_P (parser->scope));
bbaab916 14102 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21526606 14103
a723baf1 14104 /* Finally, look for the class-name. */
21526606 14105 type = cp_parser_class_name (parser,
a723baf1 14106 class_scope_p,
bbaab916 14107 template_p,
fc6a28d7 14108 typename_type,
a723baf1 14109 /*check_dependency_p=*/true,
a668c6ad
MM
14110 /*class_head_p=*/false,
14111 /*is_declaration=*/true);
a723baf1
MM
14112
14113 if (type == error_mark_node)
14114 return error_mark_node;
14115
dbbf88d1 14116 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
a723baf1
MM
14117}
14118
14119/* Exception handling [gram.exception] */
14120
14121/* Parse an (optional) exception-specification.
14122
14123 exception-specification:
14124 throw ( type-id-list [opt] )
14125
14126 Returns a TREE_LIST representing the exception-specification. The
14127 TREE_VALUE of each node is a type. */
14128
14129static tree
94edc4ab 14130cp_parser_exception_specification_opt (cp_parser* parser)
a723baf1
MM
14131{
14132 cp_token *token;
14133 tree type_id_list;
14134
14135 /* Peek at the next token. */
14136 token = cp_lexer_peek_token (parser->lexer);
14137 /* If it's not `throw', then there's no exception-specification. */
14138 if (!cp_parser_is_keyword (token, RID_THROW))
14139 return NULL_TREE;
14140
14141 /* Consume the `throw'. */
14142 cp_lexer_consume_token (parser->lexer);
14143
14144 /* Look for the `('. */
14145 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14146
14147 /* Peek at the next token. */
14148 token = cp_lexer_peek_token (parser->lexer);
14149 /* If it's not a `)', then there is a type-id-list. */
14150 if (token->type != CPP_CLOSE_PAREN)
14151 {
14152 const char *saved_message;
14153
14154 /* Types may not be defined in an exception-specification. */
14155 saved_message = parser->type_definition_forbidden_message;
14156 parser->type_definition_forbidden_message
14157 = "types may not be defined in an exception-specification";
14158 /* Parse the type-id-list. */
14159 type_id_list = cp_parser_type_id_list (parser);
14160 /* Restore the saved message. */
14161 parser->type_definition_forbidden_message = saved_message;
14162 }
14163 else
14164 type_id_list = empty_except_spec;
14165
14166 /* Look for the `)'. */
14167 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14168
14169 return type_id_list;
14170}
14171
14172/* Parse an (optional) type-id-list.
14173
14174 type-id-list:
14175 type-id
14176 type-id-list , type-id
14177
14178 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14179 in the order that the types were presented. */
14180
14181static tree
94edc4ab 14182cp_parser_type_id_list (cp_parser* parser)
a723baf1
MM
14183{
14184 tree types = NULL_TREE;
14185
14186 while (true)
14187 {
14188 cp_token *token;
14189 tree type;
14190
14191 /* Get the next type-id. */
14192 type = cp_parser_type_id (parser);
14193 /* Add it to the list. */
14194 types = add_exception_specifier (types, type, /*complain=*/1);
14195 /* Peek at the next token. */
14196 token = cp_lexer_peek_token (parser->lexer);
14197 /* If it is not a `,', we are done. */
14198 if (token->type != CPP_COMMA)
14199 break;
14200 /* Consume the `,'. */
14201 cp_lexer_consume_token (parser->lexer);
14202 }
14203
14204 return nreverse (types);
14205}
14206
14207/* Parse a try-block.
14208
14209 try-block:
14210 try compound-statement handler-seq */
14211
14212static tree
94edc4ab 14213cp_parser_try_block (cp_parser* parser)
a723baf1
MM
14214{
14215 tree try_block;
14216
14217 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14218 try_block = begin_try_block ();
325c3691 14219 cp_parser_compound_statement (parser, NULL, true);
a723baf1
MM
14220 finish_try_block (try_block);
14221 cp_parser_handler_seq (parser);
14222 finish_handler_sequence (try_block);
14223
14224 return try_block;
14225}
14226
14227/* Parse a function-try-block.
14228
14229 function-try-block:
14230 try ctor-initializer [opt] function-body handler-seq */
14231
14232static bool
94edc4ab 14233cp_parser_function_try_block (cp_parser* parser)
a723baf1 14234{
eaf6fb90 14235 tree compound_stmt;
a723baf1
MM
14236 tree try_block;
14237 bool ctor_initializer_p;
14238
14239 /* Look for the `try' keyword. */
14240 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14241 return false;
14242 /* Let the rest of the front-end know where we are. */
eaf6fb90 14243 try_block = begin_function_try_block (&compound_stmt);
a723baf1 14244 /* Parse the function-body. */
21526606 14245 ctor_initializer_p
a723baf1
MM
14246 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14247 /* We're done with the `try' part. */
14248 finish_function_try_block (try_block);
14249 /* Parse the handlers. */
14250 cp_parser_handler_seq (parser);
14251 /* We're done with the handlers. */
eaf6fb90 14252 finish_function_handler_sequence (try_block, compound_stmt);
a723baf1
MM
14253
14254 return ctor_initializer_p;
14255}
14256
14257/* Parse a handler-seq.
14258
14259 handler-seq:
14260 handler handler-seq [opt] */
14261
14262static void
94edc4ab 14263cp_parser_handler_seq (cp_parser* parser)
a723baf1
MM
14264{
14265 while (true)
14266 {
14267 cp_token *token;
14268
14269 /* Parse the handler. */
14270 cp_parser_handler (parser);
14271 /* Peek at the next token. */
14272 token = cp_lexer_peek_token (parser->lexer);
14273 /* If it's not `catch' then there are no more handlers. */
14274 if (!cp_parser_is_keyword (token, RID_CATCH))
14275 break;
14276 }
14277}
14278
14279/* Parse a handler.
14280
14281 handler:
14282 catch ( exception-declaration ) compound-statement */
14283
14284static void
94edc4ab 14285cp_parser_handler (cp_parser* parser)
a723baf1
MM
14286{
14287 tree handler;
14288 tree declaration;
14289
14290 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14291 handler = begin_handler ();
14292 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14293 declaration = cp_parser_exception_declaration (parser);
14294 finish_handler_parms (declaration, handler);
14295 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
325c3691 14296 cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
14297 finish_handler (handler);
14298}
14299
14300/* Parse an exception-declaration.
14301
14302 exception-declaration:
14303 type-specifier-seq declarator
14304 type-specifier-seq abstract-declarator
14305 type-specifier-seq
21526606 14306 ...
a723baf1
MM
14307
14308 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14309 ellipsis variant is used. */
14310
14311static tree
94edc4ab 14312cp_parser_exception_declaration (cp_parser* parser)
a723baf1 14313{
62d1db17 14314 cp_decl_specifier_seq type_specifiers;
058b15c1 14315 cp_declarator *declarator;
a723baf1
MM
14316 const char *saved_message;
14317
14318 /* If it's an ellipsis, it's easy to handle. */
14319 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14320 {
14321 /* Consume the `...' token. */
14322 cp_lexer_consume_token (parser->lexer);
14323 return NULL_TREE;
14324 }
14325
14326 /* Types may not be defined in exception-declarations. */
14327 saved_message = parser->type_definition_forbidden_message;
14328 parser->type_definition_forbidden_message
14329 = "types may not be defined in exception-declarations";
14330
14331 /* Parse the type-specifier-seq. */
d4113656
MM
14332 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14333 &type_specifiers);
a723baf1
MM
14334 /* If it's a `)', then there is no declarator. */
14335 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
058b15c1 14336 declarator = NULL;
a723baf1 14337 else
62b8a44e 14338 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
4bb8ca28 14339 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
14340 /*parenthesized_p=*/NULL,
14341 /*member_p=*/false);
a723baf1
MM
14342
14343 /* Restore the saved message. */
14344 parser->type_definition_forbidden_message = saved_message;
14345
2a50edcd
VR
14346 if (!type_specifiers.any_specifiers_p)
14347 return error_mark_node;
058b15c1 14348
2a50edcd 14349 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
a723baf1
MM
14350}
14351
21526606 14352/* Parse a throw-expression.
a723baf1
MM
14353
14354 throw-expression:
34cd5ae7 14355 throw assignment-expression [opt]
a723baf1
MM
14356
14357 Returns a THROW_EXPR representing the throw-expression. */
14358
14359static tree
94edc4ab 14360cp_parser_throw_expression (cp_parser* parser)
a723baf1
MM
14361{
14362 tree expression;
89f1a6ec 14363 cp_token* token;
a723baf1
MM
14364
14365 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
89f1a6ec
MM
14366 token = cp_lexer_peek_token (parser->lexer);
14367 /* Figure out whether or not there is an assignment-expression
14368 following the "throw" keyword. */
14369 if (token->type == CPP_COMMA
14370 || token->type == CPP_SEMICOLON
14371 || token->type == CPP_CLOSE_PAREN
14372 || token->type == CPP_CLOSE_SQUARE
14373 || token->type == CPP_CLOSE_BRACE
14374 || token->type == CPP_COLON)
a723baf1 14375 expression = NULL_TREE;
89f1a6ec 14376 else
93678513
MM
14377 expression = cp_parser_assignment_expression (parser,
14378 /*cast_p=*/false);
a723baf1
MM
14379
14380 return build_throw (expression);
14381}
14382
14383/* GNU Extensions */
14384
14385/* Parse an (optional) asm-specification.
14386
14387 asm-specification:
14388 asm ( string-literal )
14389
14390 If the asm-specification is present, returns a STRING_CST
14391 corresponding to the string-literal. Otherwise, returns
14392 NULL_TREE. */
14393
14394static tree
94edc4ab 14395cp_parser_asm_specification_opt (cp_parser* parser)
a723baf1
MM
14396{
14397 cp_token *token;
14398 tree asm_specification;
14399
14400 /* Peek at the next token. */
14401 token = cp_lexer_peek_token (parser->lexer);
21526606 14402 /* If the next token isn't the `asm' keyword, then there's no
a723baf1
MM
14403 asm-specification. */
14404 if (!cp_parser_is_keyword (token, RID_ASM))
14405 return NULL_TREE;
14406
14407 /* Consume the `asm' token. */
14408 cp_lexer_consume_token (parser->lexer);
14409 /* Look for the `('. */
14410 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14411
14412 /* Look for the string-literal. */
c162c75e 14413 asm_specification = cp_parser_string_literal (parser, false, false);
a723baf1
MM
14414
14415 /* Look for the `)'. */
14416 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14417
14418 return asm_specification;
14419}
14420
21526606 14421/* Parse an asm-operand-list.
a723baf1
MM
14422
14423 asm-operand-list:
14424 asm-operand
14425 asm-operand-list , asm-operand
21526606 14426
a723baf1 14427 asm-operand:
21526606 14428 string-literal ( expression )
a723baf1
MM
14429 [ string-literal ] string-literal ( expression )
14430
14431 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14432 each node is the expression. The TREE_PURPOSE is itself a
14433 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14434 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14435 is a STRING_CST for the string literal before the parenthesis. */
14436
14437static tree
94edc4ab 14438cp_parser_asm_operand_list (cp_parser* parser)
a723baf1
MM
14439{
14440 tree asm_operands = NULL_TREE;
14441
14442 while (true)
14443 {
14444 tree string_literal;
14445 tree expression;
14446 tree name;
21526606 14447
21526606 14448 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
a723baf1
MM
14449 {
14450 /* Consume the `[' token. */
14451 cp_lexer_consume_token (parser->lexer);
14452 /* Read the operand name. */
14453 name = cp_parser_identifier (parser);
21526606 14454 if (name != error_mark_node)
a723baf1
MM
14455 name = build_string (IDENTIFIER_LENGTH (name),
14456 IDENTIFIER_POINTER (name));
14457 /* Look for the closing `]'. */
14458 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14459 }
14460 else
14461 name = NULL_TREE;
14462 /* Look for the string-literal. */
c162c75e
MA
14463 string_literal = cp_parser_string_literal (parser, false, false);
14464
a723baf1
MM
14465 /* Look for the `('. */
14466 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14467 /* Parse the expression. */
93678513 14468 expression = cp_parser_expression (parser, /*cast_p=*/false);
a723baf1
MM
14469 /* Look for the `)'. */
14470 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
c162c75e 14471
a723baf1
MM
14472 /* Add this operand to the list. */
14473 asm_operands = tree_cons (build_tree_list (name, string_literal),
21526606 14474 expression,
a723baf1 14475 asm_operands);
21526606 14476 /* If the next token is not a `,', there are no more
a723baf1
MM
14477 operands. */
14478 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14479 break;
14480 /* Consume the `,'. */
14481 cp_lexer_consume_token (parser->lexer);
14482 }
14483
14484 return nreverse (asm_operands);
14485}
14486
21526606 14487/* Parse an asm-clobber-list.
a723baf1
MM
14488
14489 asm-clobber-list:
14490 string-literal
21526606 14491 asm-clobber-list , string-literal
a723baf1
MM
14492
14493 Returns a TREE_LIST, indicating the clobbers in the order that they
14494 appeared. The TREE_VALUE of each node is a STRING_CST. */
14495
14496static tree
94edc4ab 14497cp_parser_asm_clobber_list (cp_parser* parser)
a723baf1
MM
14498{
14499 tree clobbers = NULL_TREE;
14500
14501 while (true)
14502 {
a723baf1
MM
14503 tree string_literal;
14504
14505 /* Look for the string literal. */
c162c75e 14506 string_literal = cp_parser_string_literal (parser, false, false);
a723baf1
MM
14507 /* Add it to the list. */
14508 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21526606 14509 /* If the next token is not a `,', then the list is
a723baf1
MM
14510 complete. */
14511 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14512 break;
14513 /* Consume the `,' token. */
14514 cp_lexer_consume_token (parser->lexer);
14515 }
14516
14517 return clobbers;
14518}
14519
14520/* Parse an (optional) series of attributes.
14521
14522 attributes:
14523 attributes attribute
14524
14525 attribute:
21526606 14526 __attribute__ (( attribute-list [opt] ))
a723baf1
MM
14527
14528 The return value is as for cp_parser_attribute_list. */
21526606 14529
a723baf1 14530static tree
94edc4ab 14531cp_parser_attributes_opt (cp_parser* parser)
a723baf1
MM
14532{
14533 tree attributes = NULL_TREE;
14534
14535 while (true)
14536 {
14537 cp_token *token;
14538 tree attribute_list;
14539
14540 /* Peek at the next token. */
14541 token = cp_lexer_peek_token (parser->lexer);
14542 /* If it's not `__attribute__', then we're done. */
14543 if (token->keyword != RID_ATTRIBUTE)
14544 break;
14545
14546 /* Consume the `__attribute__' keyword. */
14547 cp_lexer_consume_token (parser->lexer);
14548 /* Look for the two `(' tokens. */
14549 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14550 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14551
14552 /* Peek at the next token. */
14553 token = cp_lexer_peek_token (parser->lexer);
14554 if (token->type != CPP_CLOSE_PAREN)
14555 /* Parse the attribute-list. */
14556 attribute_list = cp_parser_attribute_list (parser);
14557 else
14558 /* If the next token is a `)', then there is no attribute
14559 list. */
14560 attribute_list = NULL;
14561
14562 /* Look for the two `)' tokens. */
14563 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14564 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14565
14566 /* Add these new attributes to the list. */
14567 attributes = chainon (attributes, attribute_list);
14568 }
14569
14570 return attributes;
14571}
14572
21526606 14573/* Parse an attribute-list.
a723baf1 14574
21526606
EC
14575 attribute-list:
14576 attribute
a723baf1
MM
14577 attribute-list , attribute
14578
14579 attribute:
21526606 14580 identifier
a723baf1
MM
14581 identifier ( identifier )
14582 identifier ( identifier , expression-list )
21526606 14583 identifier ( expression-list )
a723baf1 14584
88e95ee3
MM
14585 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14586 to an attribute. The TREE_PURPOSE of each node is the identifier
14587 indicating which attribute is in use. The TREE_VALUE represents
14588 the arguments, if any. */
a723baf1
MM
14589
14590static tree
94edc4ab 14591cp_parser_attribute_list (cp_parser* parser)
a723baf1
MM
14592{
14593 tree attribute_list = NULL_TREE;
c162c75e 14594 bool save_translate_strings_p = parser->translate_strings_p;
a723baf1 14595
c162c75e 14596 parser->translate_strings_p = false;
a723baf1
MM
14597 while (true)
14598 {
14599 cp_token *token;
14600 tree identifier;
14601 tree attribute;
14602
14603 /* Look for the identifier. We also allow keywords here; for
14604 example `__attribute__ ((const))' is legal. */
14605 token = cp_lexer_peek_token (parser->lexer);
88e95ee3
MM
14606 if (token->type == CPP_NAME
14607 || token->type == CPP_KEYWORD)
14608 {
0b9cb8c2
VR
14609 tree arguments = NULL_TREE;
14610
88e95ee3
MM
14611 /* Consume the token. */
14612 token = cp_lexer_consume_token (parser->lexer);
21526606 14613
88e95ee3 14614 /* Save away the identifier that indicates which attribute
c8094d83 14615 this is. */
88e95ee3
MM
14616 identifier = token->value;
14617 attribute = build_tree_list (identifier, NULL_TREE);
a723baf1 14618
88e95ee3
MM
14619 /* Peek at the next token. */
14620 token = cp_lexer_peek_token (parser->lexer);
14621 /* If it's an `(', then parse the attribute arguments. */
14622 if (token->type == CPP_OPEN_PAREN)
14623 {
0b9cb8c2
VR
14624 arguments = cp_parser_parenthesized_expression_list
14625 (parser, true, /*cast_p=*/false,
14626 /*non_constant_p=*/NULL);
14627 /* Save the arguments away. */
88e95ee3
MM
14628 TREE_VALUE (attribute) = arguments;
14629 }
a723baf1 14630
0b9cb8c2
VR
14631 if (arguments != error_mark_node)
14632 {
14633 /* Add this attribute to the list. */
14634 TREE_CHAIN (attribute) = attribute_list;
14635 attribute_list = attribute;
14636 }
a723baf1 14637
88e95ee3
MM
14638 token = cp_lexer_peek_token (parser->lexer);
14639 }
14640 /* Now, look for more attributes. If the next token isn't a
14641 `,', we're done. */
a723baf1
MM
14642 if (token->type != CPP_COMMA)
14643 break;
14644
cd0be382 14645 /* Consume the comma and keep going. */
a723baf1
MM
14646 cp_lexer_consume_token (parser->lexer);
14647 }
c162c75e 14648 parser->translate_strings_p = save_translate_strings_p;
a723baf1
MM
14649
14650 /* We built up the list in reverse order. */
14651 return nreverse (attribute_list);
14652}
14653
14654/* Parse an optional `__extension__' keyword. Returns TRUE if it is
14655 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14656 current value of the PEDANTIC flag, regardless of whether or not
14657 the `__extension__' keyword is present. The caller is responsible
14658 for restoring the value of the PEDANTIC flag. */
14659
14660static bool
94edc4ab 14661cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
a723baf1
MM
14662{
14663 /* Save the old value of the PEDANTIC flag. */
14664 *saved_pedantic = pedantic;
14665
14666 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14667 {
14668 /* Consume the `__extension__' token. */
14669 cp_lexer_consume_token (parser->lexer);
14670 /* We're not being pedantic while the `__extension__' keyword is
14671 in effect. */
14672 pedantic = 0;
14673
14674 return true;
14675 }
14676
14677 return false;
14678}
14679
14680/* Parse a label declaration.
14681
14682 label-declaration:
14683 __label__ label-declarator-seq ;
14684
14685 label-declarator-seq:
14686 identifier , label-declarator-seq
14687 identifier */
14688
14689static void
94edc4ab 14690cp_parser_label_declaration (cp_parser* parser)
a723baf1
MM
14691{
14692 /* Look for the `__label__' keyword. */
14693 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14694
14695 while (true)
14696 {
14697 tree identifier;
14698
14699 /* Look for an identifier. */
14700 identifier = cp_parser_identifier (parser);
cb6d4a9f
VR
14701 /* If we failed, stop. */
14702 if (identifier == error_mark_node)
14703 break;
14704 /* Declare it as a label. */
a723baf1
MM
14705 finish_label_decl (identifier);
14706 /* If the next token is a `;', stop. */
14707 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14708 break;
14709 /* Look for the `,' separating the label declarations. */
14710 cp_parser_require (parser, CPP_COMMA, "`,'");
14711 }
14712
14713 /* Look for the final `;'. */
14714 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14715}
14716
14717/* Support Functions */
14718
14719/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14720 NAME should have one of the representations used for an
14721 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14722 is returned. If PARSER->SCOPE is a dependent type, then a
14723 SCOPE_REF is returned.
14724
14725 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14726 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14727 was formed. Abstractly, such entities should not be passed to this
14728 function, because they do not need to be looked up, but it is
14729 simpler to check for this special case here, rather than at the
14730 call-sites.
14731
14732 In cases not explicitly covered above, this function returns a
14733 DECL, OVERLOAD, or baselink representing the result of the lookup.
14734 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14735 is returned.
14736
472c29c3 14737 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
fc6a28d7
MM
14738 (e.g., "struct") that was used. In that case bindings that do not
14739 refer to types are ignored.
a723baf1 14740
b0bc6e8e
KL
14741 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14742 ignored.
14743
eea9800f
MM
14744 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14745 are ignored.
14746
a723baf1 14747 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
c8094d83 14748 types.
8f78f01f 14749
91b1ca65 14750 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
bcf51da2 14751 TREE_LIST of candidates if name-lookup results in an ambiguity, and
3db45ab5 14752 NULL_TREE otherwise. */
a723baf1
MM
14753
14754static tree
21526606 14755cp_parser_lookup_name (cp_parser *parser, tree name,
fc6a28d7 14756 enum tag_types tag_type,
3db45ab5 14757 bool is_template,
02ed62dd 14758 bool is_namespace,
8f78f01f 14759 bool check_dependency,
91b1ca65 14760 tree *ambiguous_decls)
a723baf1 14761{
ef07d61b 14762 int flags = 0;
a723baf1
MM
14763 tree decl;
14764 tree object_type = parser->context->object_type;
14765
ef07d61b
VR
14766 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14767 flags |= LOOKUP_COMPLAIN;
14768
8f78f01f 14769 /* Assume that the lookup will be unambiguous. */
91b1ca65
MM
14770 if (ambiguous_decls)
14771 *ambiguous_decls = NULL_TREE;
8f78f01f 14772
a723baf1
MM
14773 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14774 no longer valid. Note that if we are parsing tentatively, and
14775 the parse fails, OBJECT_TYPE will be automatically restored. */
14776 parser->context->object_type = NULL_TREE;
14777
14778 if (name == error_mark_node)
14779 return error_mark_node;
14780
14781 /* A template-id has already been resolved; there is no lookup to
14782 do. */
14783 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14784 return name;
14785 if (BASELINK_P (name))
14786 {
50bc768d
NS
14787 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14788 == TEMPLATE_ID_EXPR);
a723baf1
MM
14789 return name;
14790 }
14791
14792 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14793 it should already have been checked to make sure that the name
14794 used matches the type being destroyed. */
14795 if (TREE_CODE (name) == BIT_NOT_EXPR)
14796 {
14797 tree type;
14798
14799 /* Figure out to which type this destructor applies. */
14800 if (parser->scope)
14801 type = parser->scope;
14802 else if (object_type)
14803 type = object_type;
14804 else
14805 type = current_class_type;
14806 /* If that's not a class type, there is no destructor. */
14807 if (!type || !CLASS_TYPE_P (type))
14808 return error_mark_node;
9f4faeae
MM
14809 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14810 lazily_declare_fn (sfk_destructor, type);
fd6e3cce
GB
14811 if (!CLASSTYPE_DESTRUCTORS (type))
14812 return error_mark_node;
a723baf1
MM
14813 /* If it was a class type, return the destructor. */
14814 return CLASSTYPE_DESTRUCTORS (type);
14815 }
14816
14817 /* By this point, the NAME should be an ordinary identifier. If
14818 the id-expression was a qualified name, the qualifying scope is
14819 stored in PARSER->SCOPE at this point. */
50bc768d 14820 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
21526606 14821
a723baf1
MM
14822 /* Perform the lookup. */
14823 if (parser->scope)
21526606 14824 {
1fb3244a 14825 bool dependent_p;
a723baf1
MM
14826
14827 if (parser->scope == error_mark_node)
14828 return error_mark_node;
14829
14830 /* If the SCOPE is dependent, the lookup must be deferred until
14831 the template is instantiated -- unless we are explicitly
14832 looking up names in uninstantiated templates. Even then, we
14833 cannot look up the name if the scope is not a class type; it
14834 might, for example, be a template type parameter. */
1fb3244a
MM
14835 dependent_p = (TYPE_P (parser->scope)
14836 && !(parser->in_declarator_p
14837 && currently_open_class (parser->scope))
14838 && dependent_type_p (parser->scope));
a723baf1 14839 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
1fb3244a 14840 && dependent_p)
a723baf1 14841 {
fc6a28d7
MM
14842 if (tag_type)
14843 {
14844 tree type;
14845
14846 /* The resolution to Core Issue 180 says that `struct
14847 A::B' should be considered a type-name, even if `A'
14848 is dependent. */
14849 type = make_typename_type (parser->scope, name, tag_type,
8da15291 14850 /*complain=*/tf_error);
fc6a28d7
MM
14851 decl = TYPE_NAME (type);
14852 }
02ed62dd
MM
14853 else if (is_template
14854 && (cp_parser_next_token_ends_template_argument_p (parser)
14855 || cp_lexer_next_token_is (parser->lexer,
14856 CPP_CLOSE_PAREN)))
5b4acce1 14857 decl = make_unbound_class_template (parser->scope,
b939a023 14858 name, NULL_TREE,
8da15291 14859 /*complain=*/tf_error);
b0bc6e8e 14860 else
02ed62dd
MM
14861 decl = build_qualified_name (/*type=*/NULL_TREE,
14862 parser->scope, name,
14863 is_template);
a723baf1
MM
14864 }
14865 else
14866 {
4514aa8c 14867 tree pushed_scope = NULL_TREE;
91b004e5 14868
a723baf1
MM
14869 /* If PARSER->SCOPE is a dependent type, then it must be a
14870 class type, and we must not be checking dependencies;
14871 otherwise, we would have processed this lookup above. So
14872 that PARSER->SCOPE is not considered a dependent base by
14873 lookup_member, we must enter the scope here. */
1fb3244a 14874 if (dependent_p)
4514aa8c 14875 pushed_scope = push_scope (parser->scope);
78dcd41a 14876 /* If the PARSER->SCOPE is a template specialization, it
a723baf1
MM
14877 may be instantiated during name lookup. In that case,
14878 errors may be issued. Even if we rollback the current
14879 tentative parse, those errors are valid. */
c8094d83
MS
14880 decl = lookup_qualified_name (parser->scope, name,
14881 tag_type != none_type,
5e08432e 14882 /*complain=*/true);
4514aa8c
NS
14883 if (pushed_scope)
14884 pop_scope (pushed_scope);
a723baf1
MM
14885 }
14886 parser->qualifying_scope = parser->scope;
14887 parser->object_scope = NULL_TREE;
14888 }
14889 else if (object_type)
14890 {
14891 tree object_decl = NULL_TREE;
14892 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14893 OBJECT_TYPE is not a class. */
14894 if (CLASS_TYPE_P (object_type))
14895 /* If the OBJECT_TYPE is a template specialization, it may
14896 be instantiated during name lookup. In that case, errors
14897 may be issued. Even if we rollback the current tentative
14898 parse, those errors are valid. */
14899 object_decl = lookup_member (object_type,
14900 name,
c8094d83 14901 /*protect=*/0,
fc6a28d7 14902 tag_type != none_type);
a723baf1 14903 /* Look it up in the enclosing context, too. */
c8094d83 14904 decl = lookup_name_real (name, tag_type != none_type,
fc6a28d7 14905 /*nonclass=*/0,
ef07d61b 14906 /*block_p=*/true, is_namespace, flags);
a723baf1
MM
14907 parser->object_scope = object_type;
14908 parser->qualifying_scope = NULL_TREE;
14909 if (object_decl)
14910 decl = object_decl;
14911 }
14912 else
14913 {
c8094d83 14914 decl = lookup_name_real (name, tag_type != none_type,
fc6a28d7 14915 /*nonclass=*/0,
ef07d61b 14916 /*block_p=*/true, is_namespace, flags);
a723baf1
MM
14917 parser->qualifying_scope = NULL_TREE;
14918 parser->object_scope = NULL_TREE;
14919 }
14920
14921 /* If the lookup failed, let our caller know. */
bd3d082e 14922 if (!decl || decl == error_mark_node)
a723baf1
MM
14923 return error_mark_node;
14924
14925 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14926 if (TREE_CODE (decl) == TREE_LIST)
14927 {
91b1ca65
MM
14928 if (ambiguous_decls)
14929 *ambiguous_decls = decl;
a723baf1
MM
14930 /* The error message we have to print is too complicated for
14931 cp_parser_error, so we incorporate its actions directly. */
e5976695 14932 if (!cp_parser_simulate_error (parser))
a723baf1 14933 {
2a13a625 14934 error ("reference to %qD is ambiguous", name);
a723baf1
MM
14935 print_candidates (decl);
14936 }
14937 return error_mark_node;
14938 }
14939
50bc768d
NS
14940 gcc_assert (DECL_P (decl)
14941 || TREE_CODE (decl) == OVERLOAD
14942 || TREE_CODE (decl) == SCOPE_REF
14943 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14944 || BASELINK_P (decl));
a723baf1
MM
14945
14946 /* If we have resolved the name of a member declaration, check to
14947 see if the declaration is accessible. When the name resolves to
34cd5ae7 14948 set of overloaded functions, accessibility is checked when
21526606 14949 overload resolution is done.
a723baf1
MM
14950
14951 During an explicit instantiation, access is not checked at all,
14952 as per [temp.explicit]. */
8d241e0b 14953 if (DECL_P (decl))
ee76b931 14954 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
a723baf1
MM
14955
14956 return decl;
14957}
14958
14959/* Like cp_parser_lookup_name, but for use in the typical case where
b0bc6e8e
KL
14960 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14961 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
a723baf1
MM
14962
14963static tree
94edc4ab 14964cp_parser_lookup_name_simple (cp_parser* parser, tree name)
a723baf1 14965{
21526606 14966 return cp_parser_lookup_name (parser, name,
fc6a28d7 14967 none_type,
b0bc6e8e 14968 /*is_template=*/false,
eea9800f 14969 /*is_namespace=*/false,
8f78f01f 14970 /*check_dependency=*/true,
91b1ca65 14971 /*ambiguous_decls=*/NULL);
a723baf1
MM
14972}
14973
a723baf1
MM
14974/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14975 the current context, return the TYPE_DECL. If TAG_NAME_P is
14976 true, the DECL indicates the class being defined in a class-head,
14977 or declared in an elaborated-type-specifier.
14978
14979 Otherwise, return DECL. */
14980
14981static tree
14982cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14983{
710b73e6
KL
14984 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14985 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
a723baf1 14986
21526606 14987 struct A {
0cbd7506 14988 template <typename T> struct B;
a723baf1
MM
14989 };
14990
21526606
EC
14991 template <typename T> struct A::B {};
14992
c72a1a86 14993 Similarly, in an elaborated-type-specifier:
a723baf1
MM
14994
14995 namespace N { struct X{}; }
14996
14997 struct A {
0cbd7506 14998 template <typename T> friend struct N::X;
a723baf1
MM
14999 };
15000
710b73e6
KL
15001 However, if the DECL refers to a class type, and we are in
15002 the scope of the class, then the name lookup automatically
15003 finds the TYPE_DECL created by build_self_reference rather
15004 than a TEMPLATE_DECL. For example, in:
15005
15006 template <class T> struct S {
0cbd7506 15007 S s;
710b73e6
KL
15008 };
15009
15010 there is no need to handle such case. */
15011
15012 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
a723baf1
MM
15013 return DECL_TEMPLATE_RESULT (decl);
15014
15015 return decl;
15016}
15017
15018/* If too many, or too few, template-parameter lists apply to the
15019 declarator, issue an error message. Returns TRUE if all went well,
15020 and FALSE otherwise. */
15021
15022static bool
21526606 15023cp_parser_check_declarator_template_parameters (cp_parser* parser,
058b15c1 15024 cp_declarator *declarator)
a723baf1
MM
15025{
15026 unsigned num_templates;
15027
15028 /* We haven't seen any classes that involve template parameters yet. */
15029 num_templates = 0;
15030
058b15c1 15031 switch (declarator->kind)
a723baf1 15032 {
058b15c1 15033 case cdk_id:
1d786913 15034 if (declarator->u.id.qualifying_scope)
058b15c1
MM
15035 {
15036 tree scope;
15037 tree member;
a723baf1 15038
1d786913
MM
15039 scope = declarator->u.id.qualifying_scope;
15040 member = declarator->u.id.unqualified_name;
a723baf1 15041
058b15c1
MM
15042 while (scope && CLASS_TYPE_P (scope))
15043 {
15044 /* You're supposed to have one `template <...>'
15045 for every template class, but you don't need one
15046 for a full specialization. For example:
15047
15048 template <class T> struct S{};
15049 template <> struct S<int> { void f(); };
15050 void S<int>::f () {}
15051
15052 is correct; there shouldn't be a `template <>' for
15053 the definition of `S<int>::f'. */
15054 if (CLASSTYPE_TEMPLATE_INFO (scope)
15055 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
15056 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
15057 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15058 ++num_templates;
15059
15060 scope = TYPE_CONTEXT (scope);
15061 }
15062 }
c8094d83 15063 else if (TREE_CODE (declarator->u.id.unqualified_name)
1d786913
MM
15064 == TEMPLATE_ID_EXPR)
15065 /* If the DECLARATOR has the form `X<y>' then it uses one
15066 additional level of template parameters. */
a723baf1
MM
15067 ++num_templates;
15068
21526606 15069 return cp_parser_check_template_parameters (parser,
a723baf1 15070 num_templates);
058b15c1
MM
15071
15072 case cdk_function:
15073 case cdk_array:
15074 case cdk_pointer:
15075 case cdk_reference:
15076 case cdk_ptrmem:
98ca843c 15077 return (cp_parser_check_declarator_template_parameters
058b15c1
MM
15078 (parser, declarator->declarator));
15079
15080 case cdk_error:
15081 return true;
15082
15083 default:
315fb5db 15084 gcc_unreachable ();
a723baf1 15085 }
315fb5db 15086 return false;
a723baf1
MM
15087}
15088
15089/* NUM_TEMPLATES were used in the current declaration. If that is
15090 invalid, return FALSE and issue an error messages. Otherwise,
15091 return TRUE. */
15092
15093static bool
94edc4ab 15094cp_parser_check_template_parameters (cp_parser* parser,
0cbd7506 15095 unsigned num_templates)
a723baf1
MM
15096{
15097 /* If there are more template classes than parameter lists, we have
15098 something like:
21526606 15099
a723baf1
MM
15100 template <class T> void S<T>::R<T>::f (); */
15101 if (parser->num_template_parameter_lists < num_templates)
15102 {
15103 error ("too few template-parameter-lists");
15104 return false;
15105 }
15106 /* If there are the same number of template classes and parameter
15107 lists, that's OK. */
15108 if (parser->num_template_parameter_lists == num_templates)
15109 return true;
15110 /* If there are more, but only one more, then we are referring to a
15111 member template. That's OK too. */
15112 if (parser->num_template_parameter_lists == num_templates + 1)
15113 return true;
15114 /* Otherwise, there are too many template parameter lists. We have
15115 something like:
15116
15117 template <class T> template <class U> void S::f(); */
15118 error ("too many template-parameter-lists");
15119 return false;
15120}
15121
a723baf1
MM
15122/* Parse an optional `::' token indicating that the following name is
15123 from the global namespace. If so, PARSER->SCOPE is set to the
15124 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15125 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15126 Returns the new value of PARSER->SCOPE, if the `::' token is
15127 present, and NULL_TREE otherwise. */
15128
15129static tree
94edc4ab 15130cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
a723baf1
MM
15131{
15132 cp_token *token;
15133
15134 /* Peek at the next token. */
15135 token = cp_lexer_peek_token (parser->lexer);
15136 /* If we're looking at a `::' token then we're starting from the
15137 global namespace, not our current location. */
15138 if (token->type == CPP_SCOPE)
15139 {
15140 /* Consume the `::' token. */
15141 cp_lexer_consume_token (parser->lexer);
15142 /* Set the SCOPE so that we know where to start the lookup. */
15143 parser->scope = global_namespace;
15144 parser->qualifying_scope = global_namespace;
15145 parser->object_scope = NULL_TREE;
15146
15147 return parser->scope;
15148 }
15149 else if (!current_scope_valid_p)
15150 {
15151 parser->scope = NULL_TREE;
15152 parser->qualifying_scope = NULL_TREE;
15153 parser->object_scope = NULL_TREE;
15154 }
15155
15156 return NULL_TREE;
15157}
15158
15159/* Returns TRUE if the upcoming token sequence is the start of a
15160 constructor declarator. If FRIEND_P is true, the declarator is
15161 preceded by the `friend' specifier. */
15162
15163static bool
15164cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15165{
15166 bool constructor_p;
15167 tree type_decl = NULL_TREE;
15168 bool nested_name_p;
2050a1bb
MM
15169 cp_token *next_token;
15170
15171 /* The common case is that this is not a constructor declarator, so
8fbc5ae7
MM
15172 try to avoid doing lots of work if at all possible. It's not
15173 valid declare a constructor at function scope. */
15174 if (at_function_scope_p ())
15175 return false;
15176 /* And only certain tokens can begin a constructor declarator. */
2050a1bb
MM
15177 next_token = cp_lexer_peek_token (parser->lexer);
15178 if (next_token->type != CPP_NAME
15179 && next_token->type != CPP_SCOPE
15180 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15181 && next_token->type != CPP_TEMPLATE_ID)
15182 return false;
a723baf1
MM
15183
15184 /* Parse tentatively; we are going to roll back all of the tokens
15185 consumed here. */
15186 cp_parser_parse_tentatively (parser);
15187 /* Assume that we are looking at a constructor declarator. */
15188 constructor_p = true;
8d241e0b 15189
a723baf1
MM
15190 /* Look for the optional `::' operator. */
15191 cp_parser_global_scope_opt (parser,
15192 /*current_scope_valid_p=*/false);
15193 /* Look for the nested-name-specifier. */
21526606 15194 nested_name_p
a723baf1
MM
15195 = (cp_parser_nested_name_specifier_opt (parser,
15196 /*typename_keyword_p=*/false,
15197 /*check_dependency_p=*/false,
a668c6ad
MM
15198 /*type_p=*/false,
15199 /*is_declaration=*/false)
a723baf1
MM
15200 != NULL_TREE);
15201 /* Outside of a class-specifier, there must be a
15202 nested-name-specifier. */
21526606 15203 if (!nested_name_p &&
a723baf1
MM
15204 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15205 || friend_p))
15206 constructor_p = false;
15207 /* If we still think that this might be a constructor-declarator,
15208 look for a class-name. */
15209 if (constructor_p)
15210 {
15211 /* If we have:
15212
8fbc5ae7 15213 template <typename T> struct S { S(); };
a723baf1
MM
15214 template <typename T> S<T>::S ();
15215
15216 we must recognize that the nested `S' names a class.
15217 Similarly, for:
15218
15219 template <typename T> S<T>::S<T> ();
15220
15221 we must recognize that the nested `S' names a template. */
15222 type_decl = cp_parser_class_name (parser,
15223 /*typename_keyword_p=*/false,
15224 /*template_keyword_p=*/false,
fc6a28d7 15225 none_type,
a723baf1 15226 /*check_dependency_p=*/false,
a668c6ad
MM
15227 /*class_head_p=*/false,
15228 /*is_declaration=*/false);
a723baf1
MM
15229 /* If there was no class-name, then this is not a constructor. */
15230 constructor_p = !cp_parser_error_occurred (parser);
15231 }
8d241e0b 15232
a723baf1
MM
15233 /* If we're still considering a constructor, we have to see a `(',
15234 to begin the parameter-declaration-clause, followed by either a
15235 `)', an `...', or a decl-specifier. We need to check for a
15236 type-specifier to avoid being fooled into thinking that:
15237
15238 S::S (f) (int);
15239
15240 is a constructor. (It is actually a function named `f' that
15241 takes one parameter (of type `int') and returns a value of type
15242 `S::S'. */
21526606 15243 if (constructor_p
a723baf1
MM
15244 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15245 {
15246 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15247 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15077df5
MM
15248 /* A parameter declaration begins with a decl-specifier,
15249 which is either the "attribute" keyword, a storage class
15250 specifier, or (usually) a type-specifier. */
15251 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
a723baf1
MM
15252 && !cp_parser_storage_class_specifier_opt (parser))
15253 {
5dae1114 15254 tree type;
4514aa8c 15255 tree pushed_scope = NULL_TREE;
4047b164 15256 unsigned saved_num_template_parameter_lists;
5dae1114
MM
15257
15258 /* Names appearing in the type-specifier should be looked up
15259 in the scope of the class. */
15260 if (current_class_type)
15261 type = NULL_TREE;
a723baf1
MM
15262 else
15263 {
5dae1114
MM
15264 type = TREE_TYPE (type_decl);
15265 if (TREE_CODE (type) == TYPENAME_TYPE)
14d22dd6 15266 {
21526606 15267 type = resolve_typename_type (type,
14d22dd6
MM
15268 /*only_current_p=*/false);
15269 if (type == error_mark_node)
15270 {
15271 cp_parser_abort_tentative_parse (parser);
15272 return false;
15273 }
15274 }
4514aa8c 15275 pushed_scope = push_scope (type);
a723baf1 15276 }
4047b164
KL
15277
15278 /* Inside the constructor parameter list, surrounding
15279 template-parameter-lists do not apply. */
15280 saved_num_template_parameter_lists
15281 = parser->num_template_parameter_lists;
15282 parser->num_template_parameter_lists = 0;
15283
5dae1114
MM
15284 /* Look for the type-specifier. */
15285 cp_parser_type_specifier (parser,
15286 CP_PARSER_FLAGS_NONE,
62d1db17 15287 /*decl_specs=*/NULL,
5dae1114
MM
15288 /*is_declarator=*/true,
15289 /*declares_class_or_enum=*/NULL,
15290 /*is_cv_qualifier=*/NULL);
4047b164
KL
15291
15292 parser->num_template_parameter_lists
15293 = saved_num_template_parameter_lists;
15294
5dae1114 15295 /* Leave the scope of the class. */
4514aa8c
NS
15296 if (pushed_scope)
15297 pop_scope (pushed_scope);
5dae1114
MM
15298
15299 constructor_p = !cp_parser_error_occurred (parser);
a723baf1
MM
15300 }
15301 }
15302 else
15303 constructor_p = false;
15304 /* We did not really want to consume any tokens. */
15305 cp_parser_abort_tentative_parse (parser);
15306
15307 return constructor_p;
15308}
15309
15310/* Parse the definition of the function given by the DECL_SPECIFIERS,
cf22909c 15311 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
a723baf1
MM
15312 they must be performed once we are in the scope of the function.
15313
15314 Returns the function defined. */
15315
15316static tree
15317cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab 15318 (cp_parser* parser,
62d1db17 15319 cp_decl_specifier_seq *decl_specifiers,
94edc4ab 15320 tree attributes,
058b15c1 15321 const cp_declarator *declarator)
a723baf1
MM
15322{
15323 tree fn;
15324 bool success_p;
15325
15326 /* Begin the function-definition. */
058b15c1
MM
15327 success_p = start_function (decl_specifiers, declarator, attributes);
15328
15329 /* The things we're about to see are not directly qualified by any
15330 template headers we've seen thus far. */
15331 reset_specialization ();
a723baf1
MM
15332
15333 /* If there were names looked up in the decl-specifier-seq that we
15334 did not check, check them now. We must wait until we are in the
15335 scope of the function to perform the checks, since the function
15336 might be a friend. */
cf22909c 15337 perform_deferred_access_checks ();
a723baf1
MM
15338
15339 if (!success_p)
15340 {
058b15c1 15341 /* Skip the entire function. */
a723baf1
MM
15342 cp_parser_skip_to_end_of_block_or_statement (parser);
15343 fn = error_mark_node;
15344 }
15345 else
15346 fn = cp_parser_function_definition_after_declarator (parser,
15347 /*inline_p=*/false);
15348
15349 return fn;
15350}
15351
15352/* Parse the part of a function-definition that follows the
15353 declarator. INLINE_P is TRUE iff this function is an inline
15354 function defined with a class-specifier.
15355
15356 Returns the function defined. */
15357
21526606
EC
15358static tree
15359cp_parser_function_definition_after_declarator (cp_parser* parser,
94edc4ab 15360 bool inline_p)
a723baf1
MM
15361{
15362 tree fn;
15363 bool ctor_initializer_p = false;
15364 bool saved_in_unbraced_linkage_specification_p;
15365 unsigned saved_num_template_parameter_lists;
15366
15367 /* If the next token is `return', then the code may be trying to
15368 make use of the "named return value" extension that G++ used to
15369 support. */
15370 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15371 {
15372 /* Consume the `return' keyword. */
15373 cp_lexer_consume_token (parser->lexer);
15374 /* Look for the identifier that indicates what value is to be
15375 returned. */
15376 cp_parser_identifier (parser);
15377 /* Issue an error message. */
15378 error ("named return values are no longer supported");
15379 /* Skip tokens until we reach the start of the function body. */
bc4071dd
RH
15380 while (true)
15381 {
15382 cp_token *token = cp_lexer_peek_token (parser->lexer);
15383 if (token->type == CPP_OPEN_BRACE
15384 || token->type == CPP_EOF
15385 || token->type == CPP_PRAGMA_EOL)
15386 break;
15387 cp_lexer_consume_token (parser->lexer);
15388 }
a723baf1
MM
15389 }
15390 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15391 anything declared inside `f'. */
21526606 15392 saved_in_unbraced_linkage_specification_p
a723baf1
MM
15393 = parser->in_unbraced_linkage_specification_p;
15394 parser->in_unbraced_linkage_specification_p = false;
15395 /* Inside the function, surrounding template-parameter-lists do not
15396 apply. */
21526606
EC
15397 saved_num_template_parameter_lists
15398 = parser->num_template_parameter_lists;
a723baf1
MM
15399 parser->num_template_parameter_lists = 0;
15400 /* If the next token is `try', then we are looking at a
15401 function-try-block. */
15402 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15403 ctor_initializer_p = cp_parser_function_try_block (parser);
15404 /* A function-try-block includes the function-body, so we only do
15405 this next part if we're not processing a function-try-block. */
15406 else
21526606 15407 ctor_initializer_p
a723baf1
MM
15408 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15409
15410 /* Finish the function. */
21526606 15411 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
a723baf1
MM
15412 (inline_p ? 2 : 0));
15413 /* Generate code for it, if necessary. */
8cd2462c 15414 expand_or_defer_fn (fn);
a723baf1 15415 /* Restore the saved values. */
21526606 15416 parser->in_unbraced_linkage_specification_p
a723baf1 15417 = saved_in_unbraced_linkage_specification_p;
21526606 15418 parser->num_template_parameter_lists
a723baf1
MM
15419 = saved_num_template_parameter_lists;
15420
15421 return fn;
15422}
15423
15424/* Parse a template-declaration, assuming that the `export' (and
15425 `extern') keywords, if present, has already been scanned. MEMBER_P
15426 is as for cp_parser_template_declaration. */
15427
15428static void
94edc4ab 15429cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
a723baf1
MM
15430{
15431 tree decl = NULL_TREE;
6b648482 15432 tree checks;
a723baf1
MM
15433 tree parameter_list;
15434 bool friend_p = false;
2f1b1731 15435 bool need_lang_pop;
a723baf1
MM
15436
15437 /* Look for the `template' keyword. */
15438 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15439 return;
21526606 15440
a723baf1
MM
15441 /* And the `<'. */
15442 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15443 return;
2f1b1731 15444 /* [temp]
3db45ab5 15445
2f1b1731
MM
15446 A template ... shall not have C linkage. */
15447 if (current_lang_name == lang_name_c)
15448 {
15449 error ("template with C linkage");
15450 /* Give it C++ linkage to avoid confusing other parts of the
15451 front end. */
15452 push_lang_context (lang_name_cplusplus);
15453 need_lang_pop = true;
15454 }
15455 else
15456 need_lang_pop = false;
6b648482
MM
15457
15458 /* We cannot perform access checks on the template parameter
15459 declarations until we know what is being declared, just as we
15460 cannot check the decl-specifier list. */
15461 push_deferring_access_checks (dk_deferred);
15462
a723baf1
MM
15463 /* If the next token is `>', then we have an invalid
15464 specialization. Rather than complain about an invalid template
15465 parameter, issue an error message here. */
15466 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15467 {
15468 cp_parser_error (parser, "invalid explicit specialization");
2f9afd51 15469 begin_specialization ();
a723baf1
MM
15470 parameter_list = NULL_TREE;
15471 }
15472 else
357d956e
MM
15473 /* Parse the template parameters. */
15474 parameter_list = cp_parser_template_parameter_list (parser);
2f9afd51 15475
6b648482
MM
15476 /* Get the deferred access checks from the parameter list. These
15477 will be checked once we know what is being declared, as for a
15478 member template the checks must be performed in the scope of the
15479 class containing the member. */
15480 checks = get_deferred_access_checks ();
15481
a723baf1
MM
15482 /* Look for the `>'. */
15483 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15484 /* We just processed one more parameter list. */
15485 ++parser->num_template_parameter_lists;
15486 /* If the next token is `template', there are more template
15487 parameters. */
21526606 15488 if (cp_lexer_next_token_is_keyword (parser->lexer,
a723baf1
MM
15489 RID_TEMPLATE))
15490 cp_parser_template_declaration_after_export (parser, member_p);
15491 else
15492 {
fe88415f 15493 /* There are no access checks when parsing a template, as we do not
0cbd7506 15494 know if a specialization will be a friend. */
fe88415f 15495 push_deferring_access_checks (dk_no_check);
a723baf1 15496 decl = cp_parser_single_declaration (parser,
6b648482 15497 checks,
a723baf1
MM
15498 member_p,
15499 &friend_p);
fe88415f 15500 pop_deferring_access_checks ();
98ca843c 15501
a723baf1
MM
15502 /* If this is a member template declaration, let the front
15503 end know. */
15504 if (member_p && !friend_p && decl)
37d407a1
KL
15505 {
15506 if (TREE_CODE (decl) == TYPE_DECL)
15507 cp_parser_check_access_in_redeclaration (decl);
15508
15509 decl = finish_member_template_decl (decl);
15510 }
a723baf1 15511 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19db77ce
KL
15512 make_friend_class (current_class_type, TREE_TYPE (decl),
15513 /*complain=*/true);
a723baf1
MM
15514 }
15515 /* We are done with the current parameter list. */
15516 --parser->num_template_parameter_lists;
15517
6b648482
MM
15518 pop_deferring_access_checks ();
15519
a723baf1
MM
15520 /* Finish up. */
15521 finish_template_decl (parameter_list);
15522
15523 /* Register member declarations. */
15524 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15525 finish_member_declaration (decl);
2f1b1731
MM
15526 /* For the erroneous case of a template with C linkage, we pushed an
15527 implicit C++ linkage scope; exit that scope now. */
15528 if (need_lang_pop)
15529 pop_lang_context ();
a723baf1
MM
15530 /* If DECL is a function template, we must return to parse it later.
15531 (Even though there is no definition, there might be default
15532 arguments that need handling.) */
21526606 15533 if (member_p && decl
a723baf1
MM
15534 && (TREE_CODE (decl) == FUNCTION_DECL
15535 || DECL_FUNCTION_TEMPLATE_P (decl)))
15536 TREE_VALUE (parser->unparsed_functions_queues)
21526606 15537 = tree_cons (NULL_TREE, decl,
a723baf1
MM
15538 TREE_VALUE (parser->unparsed_functions_queues));
15539}
15540
6b648482
MM
15541/* Perform the deferred access checks from a template-parameter-list.
15542 CHECKS is a TREE_LIST of access checks, as returned by
15543 get_deferred_access_checks. */
15544
15545static void
15546cp_parser_perform_template_parameter_access_checks (tree checks)
15547{
15548 ++processing_template_parmlist;
15549 perform_access_checks (checks);
15550 --processing_template_parmlist;
15551}
15552
a723baf1
MM
15553/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15554 `function-definition' sequence. MEMBER_P is true, this declaration
15555 appears in a class scope.
15556
15557 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15558 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15559
15560static tree
21526606 15561cp_parser_single_declaration (cp_parser* parser,
6b648482 15562 tree checks,
94edc4ab
NN
15563 bool member_p,
15564 bool* friend_p)
a723baf1 15565{
560ad596 15566 int declares_class_or_enum;
a723baf1 15567 tree decl = NULL_TREE;
62d1db17 15568 cp_decl_specifier_seq decl_specifiers;
4bb8ca28 15569 bool function_definition_p = false;
a723baf1 15570
71bd7186
MM
15571 /* This function is only used when processing a template
15572 declaration. */
15573 gcc_assert (innermost_scope_kind () == sk_template_parms
15574 || innermost_scope_kind () == sk_template_spec);
15575
a723baf1 15576 /* Defer access checks until we know what is being declared. */
8d241e0b 15577 push_deferring_access_checks (dk_deferred);
cf22909c 15578
a723baf1
MM
15579 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15580 alternative. */
62d1db17
MM
15581 cp_parser_decl_specifier_seq (parser,
15582 CP_PARSER_FLAGS_OPTIONAL,
15583 &decl_specifiers,
15584 &declares_class_or_enum);
4bb8ca28 15585 if (friend_p)
62d1db17 15586 *friend_p = cp_parser_friend_p (&decl_specifiers);
71bd7186
MM
15587
15588 /* There are no template typedefs. */
15589 if (decl_specifiers.specs[(int) ds_typedef])
15590 {
15591 error ("template declaration of %qs", "typedef");
15592 decl = error_mark_node;
15593 }
15594
a723baf1
MM
15595 /* Gather up the access checks that occurred the
15596 decl-specifier-seq. */
cf22909c
KL
15597 stop_deferring_access_checks ();
15598
a723baf1
MM
15599 /* Check for the declaration of a template class. */
15600 if (declares_class_or_enum)
15601 {
15602 if (cp_parser_declares_only_class_p (parser))
15603 {
62d1db17 15604 decl = shadow_tag (&decl_specifiers);
b939a023
KL
15605
15606 /* In this case:
15607
15608 struct C {
15609 friend template <typename T> struct A<T>::B;
15610 };
15611
15612 A<T>::B will be represented by a TYPENAME_TYPE, and
15613 therefore not recognized by shadow_tag. */
15614 if (friend_p && *friend_p
15615 && !decl
15616 && decl_specifiers.type
15617 && TYPE_P (decl_specifiers.type))
15618 decl = decl_specifiers.type;
15619
62d1db17 15620 if (decl && decl != error_mark_node)
a723baf1
MM
15621 decl = TYPE_NAME (decl);
15622 else
15623 decl = error_mark_node;
6b648482
MM
15624
15625 /* Perform access checks for template parameters. */
15626 cp_parser_perform_template_parameter_access_checks (checks);
a723baf1
MM
15627 }
15628 }
a723baf1
MM
15629 /* If it's not a template class, try for a template function. If
15630 the next token is a `;', then this declaration does not declare
15631 anything. But, if there were errors in the decl-specifiers, then
15632 the error might well have come from an attempted class-specifier.
15633 In that case, there's no need to warn about a missing declarator. */
15634 if (!decl
15635 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
62d1db17 15636 || decl_specifiers.type != error_mark_node))
21526606 15637 decl = cp_parser_init_declarator (parser,
62d1db17 15638 &decl_specifiers,
6b648482 15639 checks,
4bb8ca28 15640 /*function_definition_allowed_p=*/true,
a723baf1 15641 member_p,
560ad596 15642 declares_class_or_enum,
4bb8ca28 15643 &function_definition_p);
cf22909c
KL
15644
15645 pop_deferring_access_checks ();
15646
a723baf1
MM
15647 /* Clear any current qualification; whatever comes next is the start
15648 of something new. */
15649 parser->scope = NULL_TREE;
15650 parser->qualifying_scope = NULL_TREE;
15651 parser->object_scope = NULL_TREE;
15652 /* Look for a trailing `;' after the declaration. */
4bb8ca28 15653 if (!function_definition_p
71bd7186
MM
15654 && (decl == error_mark_node
15655 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
a723baf1 15656 cp_parser_skip_to_end_of_block_or_statement (parser);
a723baf1
MM
15657
15658 return decl;
15659}
15660
d6b4ea85
MM
15661/* Parse a cast-expression that is not the operand of a unary "&". */
15662
15663static tree
15664cp_parser_simple_cast_expression (cp_parser *parser)
15665{
93678513
MM
15666 return cp_parser_cast_expression (parser, /*address_p=*/false,
15667 /*cast_p=*/false);
d6b4ea85
MM
15668}
15669
a723baf1
MM
15670/* Parse a functional cast to TYPE. Returns an expression
15671 representing the cast. */
15672
15673static tree
94edc4ab 15674cp_parser_functional_cast (cp_parser* parser, tree type)
a723baf1
MM
15675{
15676 tree expression_list;
d36d5600 15677 tree cast;
a723baf1 15678
21526606 15679 expression_list
39703eb9 15680 = cp_parser_parenthesized_expression_list (parser, false,
93678513 15681 /*cast_p=*/true,
39703eb9 15682 /*non_constant_p=*/NULL);
a723baf1 15683
d36d5600
GB
15684 cast = build_functional_cast (type, expression_list);
15685 /* [expr.const]/1: In an integral constant expression "only type
15686 conversions to integral or enumeration type can be used". */
3ce5fa4f
NS
15687 if (TREE_CODE (type) == TYPE_DECL)
15688 type = TREE_TYPE (type);
015c2c66
MM
15689 if (cast != error_mark_node
15690 && !cast_valid_in_integral_constant_expression_p (type)
15691 && (cp_parser_non_integral_constant_expression
15692 (parser, "a call to a constructor")))
15693 return error_mark_node;
d36d5600 15694 return cast;
a723baf1
MM
15695}
15696
4bb8ca28
MM
15697/* Save the tokens that make up the body of a member function defined
15698 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15699 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15700 specifiers applied to the declaration. Returns the FUNCTION_DECL
15701 for the member function. */
15702
7ce27103 15703static tree
4bb8ca28 15704cp_parser_save_member_function_body (cp_parser* parser,
62d1db17 15705 cp_decl_specifier_seq *decl_specifiers,
058b15c1 15706 cp_declarator *declarator,
4bb8ca28
MM
15707 tree attributes)
15708{
c162c75e
MA
15709 cp_token *first;
15710 cp_token *last;
4bb8ca28
MM
15711 tree fn;
15712
15713 /* Create the function-declaration. */
15714 fn = start_method (decl_specifiers, declarator, attributes);
15715 /* If something went badly wrong, bail out now. */
15716 if (fn == error_mark_node)
15717 {
15718 /* If there's a function-body, skip it. */
21526606 15719 if (cp_parser_token_starts_function_definition_p
4bb8ca28
MM
15720 (cp_lexer_peek_token (parser->lexer)))
15721 cp_parser_skip_to_end_of_block_or_statement (parser);
15722 return error_mark_node;
15723 }
15724
15725 /* Remember it, if there default args to post process. */
15726 cp_parser_save_default_args (parser, fn);
15727
21526606 15728 /* Save away the tokens that make up the body of the
4bb8ca28 15729 function. */
c162c75e
MA
15730 first = parser->lexer->next_token;
15731 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
4bb8ca28
MM
15732 /* Handle function try blocks. */
15733 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
c162c75e
MA
15734 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15735 last = parser->lexer->next_token;
4bb8ca28
MM
15736
15737 /* Save away the inline definition; we will process it when the
15738 class is complete. */
c162c75e 15739 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
4bb8ca28
MM
15740 DECL_PENDING_INLINE_P (fn) = 1;
15741
15742 /* We need to know that this was defined in the class, so that
15743 friend templates are handled correctly. */
15744 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15745
15746 /* We're done with the inline definition. */
15747 finish_method (fn);
15748
15749 /* Add FN to the queue of functions to be parsed later. */
15750 TREE_VALUE (parser->unparsed_functions_queues)
21526606 15751 = tree_cons (NULL_TREE, fn,
4bb8ca28
MM
15752 TREE_VALUE (parser->unparsed_functions_queues));
15753
15754 return fn;
15755}
15756
ec75414f
MM
15757/* Parse a template-argument-list, as well as the trailing ">" (but
15758 not the opening ">"). See cp_parser_template_argument_list for the
15759 return value. */
15760
15761static tree
15762cp_parser_enclosed_template_argument_list (cp_parser* parser)
15763{
15764 tree arguments;
15765 tree saved_scope;
15766 tree saved_qualifying_scope;
15767 tree saved_object_scope;
15768 bool saved_greater_than_is_operator_p;
49f210a2 15769 bool saved_skip_evaluation;
ec75414f
MM
15770
15771 /* [temp.names]
15772
15773 When parsing a template-id, the first non-nested `>' is taken as
15774 the end of the template-argument-list rather than a greater-than
15775 operator. */
21526606 15776 saved_greater_than_is_operator_p
ec75414f
MM
15777 = parser->greater_than_is_operator_p;
15778 parser->greater_than_is_operator_p = false;
15779 /* Parsing the argument list may modify SCOPE, so we save it
15780 here. */
15781 saved_scope = parser->scope;
15782 saved_qualifying_scope = parser->qualifying_scope;
15783 saved_object_scope = parser->object_scope;
49f210a2
MM
15784 /* We need to evaluate the template arguments, even though this
15785 template-id may be nested within a "sizeof". */
15786 saved_skip_evaluation = skip_evaluation;
15787 skip_evaluation = false;
ec75414f
MM
15788 /* Parse the template-argument-list itself. */
15789 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15790 arguments = NULL_TREE;
15791 else
15792 arguments = cp_parser_template_argument_list (parser);
4d5297fa
GB
15793 /* Look for the `>' that ends the template-argument-list. If we find
15794 a '>>' instead, it's probably just a typo. */
15795 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15796 {
15797 if (!saved_greater_than_is_operator_p)
15798 {
2cfe82fe
ZW
15799 /* If we're in a nested template argument list, the '>>' has
15800 to be a typo for '> >'. We emit the error message, but we
15801 continue parsing and we push a '>' as next token, so that
15802 the argument list will be parsed correctly. Note that the
15803 global source location is still on the token before the
15804 '>>', so we need to say explicitly where we want it. */
15805 cp_token *token = cp_lexer_peek_token (parser->lexer);
15806 error ("%H%<>>%> should be %<> >%> "
15807 "within a nested template argument list",
15808 &token->location);
15809
15810 /* ??? Proper recovery should terminate two levels of
15811 template argument list here. */
4d5297fa
GB
15812 token->type = CPP_GREATER;
15813 }
15814 else
15815 {
2cfe82fe
ZW
15816 /* If this is not a nested template argument list, the '>>'
15817 is a typo for '>'. Emit an error message and continue.
15818 Same deal about the token location, but here we can get it
15819 right by consuming the '>>' before issuing the diagnostic. */
4d5297fa 15820 cp_lexer_consume_token (parser->lexer);
2cfe82fe
ZW
15821 error ("spurious %<>>%>, use %<>%> to terminate "
15822 "a template argument list");
4d5297fa
GB
15823 }
15824 }
2cfe82fe 15825 else
88a33c34 15826 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
ec75414f 15827 /* The `>' token might be a greater-than operator again now. */
21526606 15828 parser->greater_than_is_operator_p
ec75414f
MM
15829 = saved_greater_than_is_operator_p;
15830 /* Restore the SAVED_SCOPE. */
15831 parser->scope = saved_scope;
15832 parser->qualifying_scope = saved_qualifying_scope;
15833 parser->object_scope = saved_object_scope;
49f210a2 15834 skip_evaluation = saved_skip_evaluation;
ec75414f
MM
15835
15836 return arguments;
15837}
15838
a723baf1
MM
15839/* MEMBER_FUNCTION is a member function, or a friend. If default
15840 arguments, or the body of the function have not yet been parsed,
15841 parse them now. */
15842
15843static void
94edc4ab 15844cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
a723baf1 15845{
a723baf1
MM
15846 /* If this member is a template, get the underlying
15847 FUNCTION_DECL. */
15848 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15849 member_function = DECL_TEMPLATE_RESULT (member_function);
15850
15851 /* There should not be any class definitions in progress at this
15852 point; the bodies of members are only parsed outside of all class
15853 definitions. */
50bc768d 15854 gcc_assert (parser->num_classes_being_defined == 0);
a723baf1
MM
15855 /* While we're parsing the member functions we might encounter more
15856 classes. We want to handle them right away, but we don't want
15857 them getting mixed up with functions that are currently in the
15858 queue. */
15859 parser->unparsed_functions_queues
15860 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15861
15862 /* Make sure that any template parameters are in scope. */
15863 maybe_begin_member_template_processing (member_function);
15864
a723baf1
MM
15865 /* If the body of the function has not yet been parsed, parse it
15866 now. */
15867 if (DECL_PENDING_INLINE_P (member_function))
15868 {
15869 tree function_scope;
15870 cp_token_cache *tokens;
15871
15872 /* The function is no longer pending; we are processing it. */
15873 tokens = DECL_PENDING_INLINE_INFO (member_function);
15874 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15875 DECL_PENDING_INLINE_P (member_function) = 0;
c8094d83 15876
f769035f
NS
15877 /* If this is a local class, enter the scope of the containing
15878 function. */
15879 function_scope = current_function_decl;
a723baf1
MM
15880 if (function_scope)
15881 push_function_context_to (function_scope);
21526606 15882
c8094d83 15883
2cfe82fe
ZW
15884 /* Push the body of the function onto the lexer stack. */
15885 cp_parser_push_lexer_for_tokens (parser, tokens);
21526606 15886
a723baf1
MM
15887 /* Let the front end know that we going to be defining this
15888 function. */
058b15c1
MM
15889 start_preparsed_function (member_function, NULL_TREE,
15890 SF_PRE_PARSED | SF_INCLASS_INLINE);
21526606 15891
2d637547
NS
15892 /* Don't do access checking if it is a templated function. */
15893 if (processing_template_decl)
15894 push_deferring_access_checks (dk_no_check);
c8094d83 15895
a723baf1
MM
15896 /* Now, parse the body of the function. */
15897 cp_parser_function_definition_after_declarator (parser,
15898 /*inline_p=*/true);
21526606 15899
2d637547
NS
15900 if (processing_template_decl)
15901 pop_deferring_access_checks ();
c8094d83 15902
a723baf1
MM
15903 /* Leave the scope of the containing function. */
15904 if (function_scope)
15905 pop_function_context_from (function_scope);
2cfe82fe 15906 cp_parser_pop_lexer (parser);
a723baf1
MM
15907 }
15908
15909 /* Remove any template parameters from the symbol table. */
15910 maybe_end_member_template_processing ();
15911
15912 /* Restore the queue. */
21526606 15913 parser->unparsed_functions_queues
a723baf1
MM
15914 = TREE_CHAIN (parser->unparsed_functions_queues);
15915}
15916
cd0be382 15917/* If DECL contains any default args, remember it on the unparsed
8db1028e
NS
15918 functions queue. */
15919
15920static void
15921cp_parser_save_default_args (cp_parser* parser, tree decl)
15922{
15923 tree probe;
15924
15925 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15926 probe;
15927 probe = TREE_CHAIN (probe))
15928 if (TREE_PURPOSE (probe))
15929 {
15930 TREE_PURPOSE (parser->unparsed_functions_queues)
f44b0c8e 15931 = tree_cons (current_class_type, decl,
8db1028e
NS
15932 TREE_PURPOSE (parser->unparsed_functions_queues));
15933 break;
15934 }
8db1028e
NS
15935}
15936
8218bd34 15937/* FN is a FUNCTION_DECL which may contains a parameter with an
f44b0c8e
MM
15938 unparsed DEFAULT_ARG. Parse the default args now. This function
15939 assumes that the current scope is the scope in which the default
15940 argument should be processed. */
a723baf1
MM
15941
15942static void
8218bd34 15943cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
a723baf1 15944{
a723baf1 15945 bool saved_local_variables_forbidden_p;
2cfe82fe 15946 tree parm;
8218bd34 15947
b92bc2a0
NS
15948 /* While we're parsing the default args, we might (due to the
15949 statement expression extension) encounter more classes. We want
15950 to handle them right away, but we don't want them getting mixed
15951 up with default args that are currently in the queue. */
15952 parser->unparsed_functions_queues
15953 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15954
2cfe82fe
ZW
15955 /* Local variable names (and the `this' keyword) may not appear
15956 in a default argument. */
15957 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15958 parser->local_variables_forbidden_p = true;
15959
15960 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15961 parm;
15962 parm = TREE_CHAIN (parm))
a723baf1 15963 {
2cfe82fe 15964 cp_token_cache *tokens;
5e97d404
NS
15965 tree default_arg = TREE_PURPOSE (parm);
15966 tree parsed_arg;
01ea1ea8
NS
15967 VEC(tree,gc) *insts;
15968 tree copy;
15969 unsigned ix;
c8094d83 15970
5e97d404 15971 if (!default_arg)
2cfe82fe 15972 continue;
a723baf1 15973
efb169b0
NS
15974 if (TREE_CODE (default_arg) != DEFAULT_ARG)
15975 /* This can happen for a friend declaration for a function
15976 already declared with default arguments. */
15977 continue;
5e97d404 15978
2cfe82fe
ZW
15979 /* Push the saved tokens for the default argument onto the parser's
15980 lexer stack. */
5e97d404 15981 tokens = DEFARG_TOKENS (default_arg);
2cfe82fe 15982 cp_parser_push_lexer_for_tokens (parser, tokens);
a723baf1 15983
2cfe82fe 15984 /* Parse the assignment-expression. */
5e97d404
NS
15985 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15986
c3ee4651
NS
15987 if (!processing_template_decl)
15988 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
3db45ab5 15989
5e97d404
NS
15990 TREE_PURPOSE (parm) = parsed_arg;
15991
15992 /* Update any instantiations we've already created. */
01ea1ea8
NS
15993 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15994 VEC_iterate (tree, insts, ix, copy); ix++)
15995 TREE_PURPOSE (copy) = parsed_arg;
a723baf1 15996
676e33ca
MM
15997 /* If the token stream has not been completely used up, then
15998 there was extra junk after the end of the default
15999 argument. */
16000 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2a13a625 16001 cp_parser_error (parser, "expected %<,%>");
676e33ca 16002
2cfe82fe
ZW
16003 /* Revert to the main lexer. */
16004 cp_parser_pop_lexer (parser);
a723baf1 16005 }
b92bc2a0 16006
607c855e
VR
16007 /* Make sure no default arg is missing. */
16008 check_default_args (fn);
16009
2cfe82fe
ZW
16010 /* Restore the state of local_variables_forbidden_p. */
16011 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16012
b92bc2a0 16013 /* Restore the queue. */
21526606 16014 parser->unparsed_functions_queues
b92bc2a0 16015 = TREE_CHAIN (parser->unparsed_functions_queues);
a723baf1
MM
16016}
16017
16018/* Parse the operand of `sizeof' (or a similar operator). Returns
16019 either a TYPE or an expression, depending on the form of the
16020 input. The KEYWORD indicates which kind of expression we have
16021 encountered. */
16022
16023static tree
94edc4ab 16024cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
a723baf1
MM
16025{
16026 static const char *format;
16027 tree expr = NULL_TREE;
16028 const char *saved_message;
67c03833 16029 bool saved_integral_constant_expression_p;
93678513 16030 bool saved_non_integral_constant_expression_p;
a723baf1
MM
16031
16032 /* Initialize FORMAT the first time we get here. */
16033 if (!format)
9e637a26 16034 format = "types may not be defined in '%s' expressions";
a723baf1
MM
16035
16036 /* Types cannot be defined in a `sizeof' expression. Save away the
16037 old message. */
16038 saved_message = parser->type_definition_forbidden_message;
16039 /* And create the new one. */
21526606 16040 parser->type_definition_forbidden_message
0ac1b889 16041 = XNEWVEC (const char, strlen (format)
c68b0a84
KG
16042 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16043 + 1 /* `\0' */);
a723baf1
MM
16044 sprintf ((char *) parser->type_definition_forbidden_message,
16045 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16046
16047 /* The restrictions on constant-expressions do not apply inside
16048 sizeof expressions. */
c8094d83 16049 saved_integral_constant_expression_p
93678513
MM
16050 = parser->integral_constant_expression_p;
16051 saved_non_integral_constant_expression_p
16052 = parser->non_integral_constant_expression_p;
67c03833 16053 parser->integral_constant_expression_p = false;
a723baf1 16054
3beb3abf
MM
16055 /* Do not actually evaluate the expression. */
16056 ++skip_evaluation;
a723baf1
MM
16057 /* If it's a `(', then we might be looking at the type-id
16058 construction. */
16059 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16060 {
16061 tree type;
4f8163b1 16062 bool saved_in_type_id_in_expr_p;
a723baf1
MM
16063
16064 /* We can't be sure yet whether we're looking at a type-id or an
16065 expression. */
16066 cp_parser_parse_tentatively (parser);
16067 /* Consume the `('. */
16068 cp_lexer_consume_token (parser->lexer);
16069 /* Parse the type-id. */
4f8163b1
MM
16070 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16071 parser->in_type_id_in_expr_p = true;
a723baf1 16072 type = cp_parser_type_id (parser);
4f8163b1 16073 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1 16074 /* Now, look for the trailing `)'. */
9e637a26 16075 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
a723baf1
MM
16076 /* If all went well, then we're done. */
16077 if (cp_parser_parse_definitely (parser))
16078 {
62d1db17
MM
16079 cp_decl_specifier_seq decl_specs;
16080
16081 /* Build a trivial decl-specifier-seq. */
16082 clear_decl_specs (&decl_specs);
16083 decl_specs.type = type;
a723baf1
MM
16084
16085 /* Call grokdeclarator to figure out what type this is. */
058b15c1 16086 expr = grokdeclarator (NULL,
62d1db17 16087 &decl_specs,
a723baf1
MM
16088 TYPENAME,
16089 /*initialized=*/0,
16090 /*attrlist=*/NULL);
16091 }
16092 }
16093
16094 /* If the type-id production did not work out, then we must be
16095 looking at the unary-expression production. */
16096 if (!expr)
93678513
MM
16097 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16098 /*cast_p=*/false);
3beb3abf
MM
16099 /* Go back to evaluating expressions. */
16100 --skip_evaluation;
a723baf1
MM
16101
16102 /* Free the message we created. */
16103 free ((char *) parser->type_definition_forbidden_message);
16104 /* And restore the old one. */
16105 parser->type_definition_forbidden_message = saved_message;
c8094d83 16106 parser->integral_constant_expression_p
93678513
MM
16107 = saved_integral_constant_expression_p;
16108 parser->non_integral_constant_expression_p
16109 = saved_non_integral_constant_expression_p;
a723baf1
MM
16110
16111 return expr;
16112}
16113
16114/* If the current declaration has no declarator, return true. */
16115
16116static bool
16117cp_parser_declares_only_class_p (cp_parser *parser)
16118{
21526606 16119 /* If the next token is a `;' or a `,' then there is no
a723baf1
MM
16120 declarator. */
16121 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16122 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16123}
16124
bfce0853
MM
16125/* Update the DECL_SPECS to reflect the storage class indicated by
16126 KEYWORD. */
a723baf1 16127
62d1db17 16128static void
bfce0853
MM
16129cp_parser_set_storage_class (cp_parser *parser,
16130 cp_decl_specifier_seq *decl_specs,
16131 enum rid keyword)
a723baf1 16132{
bfce0853
MM
16133 cp_storage_class storage_class;
16134
16135 if (parser->in_unbraced_linkage_specification_p)
16136 {
16137 error ("invalid use of %qD in linkage specification",
16138 ridpointers[keyword]);
16139 return;
16140 }
16141 else if (decl_specs->storage_class != sc_none)
16142 {
16143 decl_specs->multiple_storage_classes_p = true;
16144 return;
16145 }
16146
16147 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16148 && decl_specs->specs[(int) ds_thread])
16149 {
16150 error ("%<__thread%> before %qD", ridpointers[keyword]);
16151 decl_specs->specs[(int) ds_thread] = 0;
16152 }
16153
3db45ab5 16154 switch (keyword)
bfce0853
MM
16155 {
16156 case RID_AUTO:
16157 storage_class = sc_auto;
16158 break;
16159 case RID_REGISTER:
16160 storage_class = sc_register;
16161 break;
16162 case RID_STATIC:
16163 storage_class = sc_static;
16164 break;
16165 case RID_EXTERN:
16166 storage_class = sc_extern;
16167 break;
16168 case RID_MUTABLE:
16169 storage_class = sc_mutable;
16170 break;
16171 default:
16172 gcc_unreachable ();
16173 }
16174 decl_specs->storage_class = storage_class;
62d1db17
MM
16175}
16176
16177/* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16178 is true, the type is a user-defined type; otherwise it is a
16179 built-in type specified by a keyword. */
a723baf1 16180
62d1db17
MM
16181static void
16182cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16183 tree type_spec,
16184 bool user_defined_p)
16185{
16186 decl_specs->any_specifiers_p = true;
98ca843c 16187
9306cccb
MM
16188 /* If the user tries to redeclare bool or wchar_t (with, for
16189 example, in "typedef int wchar_t;") we remember that this is what
f84b6c96
MM
16190 happened. In system headers, we ignore these declarations so
16191 that G++ can work with system headers that are not C++-safe. */
98ca843c 16192 if (decl_specs->specs[(int) ds_typedef]
f84b6c96 16193 && !user_defined_p
9306cccb
MM
16194 && (type_spec == boolean_type_node
16195 || type_spec == wchar_type_node)
f84b6c96
MM
16196 && (decl_specs->type
16197 || decl_specs->specs[(int) ds_long]
16198 || decl_specs->specs[(int) ds_short]
16199 || decl_specs->specs[(int) ds_unsigned]
16200 || decl_specs->specs[(int) ds_signed]))
0a73e37f
MM
16201 {
16202 decl_specs->redefined_builtin_type = type_spec;
16203 if (!decl_specs->type)
16204 {
16205 decl_specs->type = type_spec;
16206 decl_specs->user_defined_type_p = false;
16207 }
16208 }
f84b6c96
MM
16209 else if (decl_specs->type)
16210 decl_specs->multiple_types_p = true;
62d1db17
MM
16211 else
16212 {
16213 decl_specs->type = type_spec;
16214 decl_specs->user_defined_type_p = user_defined_p;
0a73e37f 16215 decl_specs->redefined_builtin_type = NULL_TREE;
a723baf1 16216 }
62d1db17 16217}
a723baf1 16218
62d1db17
MM
16219/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16220 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16221
16222static bool
16223cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16224{
16225 return decl_specifiers->specs[(int) ds_friend] != 0;
a723baf1
MM
16226}
16227
16228/* If the next token is of the indicated TYPE, consume it. Otherwise,
16229 issue an error message indicating that TOKEN_DESC was expected.
21526606 16230
a723baf1
MM
16231 Returns the token consumed, if the token had the appropriate type.
16232 Otherwise, returns NULL. */
16233
16234static cp_token *
94edc4ab 16235cp_parser_require (cp_parser* parser,
0cbd7506
MS
16236 enum cpp_ttype type,
16237 const char* token_desc)
a723baf1
MM
16238{
16239 if (cp_lexer_next_token_is (parser->lexer, type))
16240 return cp_lexer_consume_token (parser->lexer);
16241 else
16242 {
e5976695
MM
16243 /* Output the MESSAGE -- unless we're parsing tentatively. */
16244 if (!cp_parser_simulate_error (parser))
216bb6e1
MM
16245 {
16246 char *message = concat ("expected ", token_desc, NULL);
16247 cp_parser_error (parser, message);
16248 free (message);
16249 }
a723baf1
MM
16250 return NULL;
16251 }
16252}
16253
16254/* Like cp_parser_require, except that tokens will be skipped until
16255 the desired token is found. An error message is still produced if
16256 the next token is not as expected. */
16257
16258static void
21526606 16259cp_parser_skip_until_found (cp_parser* parser,
0cbd7506
MS
16260 enum cpp_ttype type,
16261 const char* token_desc)
a723baf1
MM
16262{
16263 cp_token *token;
16264 unsigned nesting_depth = 0;
16265
16266 if (cp_parser_require (parser, type, token_desc))
16267 return;
16268
16269 /* Skip tokens until the desired token is found. */
16270 while (true)
16271 {
16272 /* Peek at the next token. */
16273 token = cp_lexer_peek_token (parser->lexer);
bc4071dd
RH
16274
16275 /* If we've reached the token we want, consume it and stop. */
a723baf1
MM
16276 if (token->type == type && !nesting_depth)
16277 {
16278 cp_lexer_consume_token (parser->lexer);
16279 return;
16280 }
bc4071dd
RH
16281
16282 switch (token->type)
a723baf1 16283 {
bc4071dd
RH
16284 case CPP_EOF:
16285 case CPP_PRAGMA_EOL:
16286 /* If we've run out of tokens, stop. */
16287 return;
16288
16289 case CPP_OPEN_BRACE:
16290 case CPP_OPEN_PAREN:
16291 case CPP_OPEN_SQUARE:
16292 ++nesting_depth;
16293 break;
16294
16295 case CPP_CLOSE_BRACE:
16296 case CPP_CLOSE_PAREN:
16297 case CPP_CLOSE_SQUARE:
a723baf1
MM
16298 if (nesting_depth-- == 0)
16299 return;
bc4071dd
RH
16300 break;
16301
16302 default:
16303 break;
a723baf1 16304 }
bc4071dd 16305
a723baf1
MM
16306 /* Consume this token. */
16307 cp_lexer_consume_token (parser->lexer);
16308 }
16309}
16310
16311/* If the next token is the indicated keyword, consume it. Otherwise,
16312 issue an error message indicating that TOKEN_DESC was expected.
21526606 16313
a723baf1
MM
16314 Returns the token consumed, if the token had the appropriate type.
16315 Otherwise, returns NULL. */
16316
16317static cp_token *
94edc4ab 16318cp_parser_require_keyword (cp_parser* parser,
0cbd7506
MS
16319 enum rid keyword,
16320 const char* token_desc)
a723baf1
MM
16321{
16322 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16323
16324 if (token && token->keyword != keyword)
16325 {
16326 dyn_string_t error_msg;
16327
16328 /* Format the error message. */
16329 error_msg = dyn_string_new (0);
16330 dyn_string_append_cstr (error_msg, "expected ");
16331 dyn_string_append_cstr (error_msg, token_desc);
16332 cp_parser_error (parser, error_msg->s);
16333 dyn_string_delete (error_msg);
16334 return NULL;
16335 }
16336
16337 return token;
16338}
16339
16340/* Returns TRUE iff TOKEN is a token that can begin the body of a
16341 function-definition. */
16342
21526606 16343static bool
94edc4ab 16344cp_parser_token_starts_function_definition_p (cp_token* token)
a723baf1
MM
16345{
16346 return (/* An ordinary function-body begins with an `{'. */
16347 token->type == CPP_OPEN_BRACE
16348 /* A ctor-initializer begins with a `:'. */
16349 || token->type == CPP_COLON
16350 /* A function-try-block begins with `try'. */
16351 || token->keyword == RID_TRY
16352 /* The named return value extension begins with `return'. */
16353 || token->keyword == RID_RETURN);
16354}
16355
16356/* Returns TRUE iff the next token is the ":" or "{" beginning a class
16357 definition. */
16358
16359static bool
16360cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16361{
16362 cp_token *token;
16363
16364 token = cp_lexer_peek_token (parser->lexer);
16365 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16366}
16367
d17811fd 16368/* Returns TRUE iff the next token is the "," or ">" ending a
03fd3f84 16369 template-argument. */
d17811fd
MM
16370
16371static bool
16372cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16373{
16374 cp_token *token;
16375
16376 token = cp_lexer_peek_token (parser->lexer);
391c4bc5 16377 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
d17811fd 16378}
f4abade9 16379
48b5c5c1 16380/* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
f4abade9
GB
16381 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16382
16383static bool
21526606 16384cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
f4abade9
GB
16385 size_t n)
16386{
16387 cp_token *token;
16388
16389 token = cp_lexer_peek_nth_token (parser->lexer, n);
16390 if (token->type == CPP_LESS)
16391 return true;
16392 /* Check for the sequence `<::' in the original code. It would be lexed as
16393 `[:', where `[' is a digraph, and there is no whitespace before
16394 `:'. */
16395 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16396 {
16397 cp_token *token2;
16398 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16399 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16400 return true;
16401 }
16402 return false;
16403}
21526606 16404
a723baf1
MM
16405/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16406 or none_type otherwise. */
16407
16408static enum tag_types
94edc4ab 16409cp_parser_token_is_class_key (cp_token* token)
a723baf1
MM
16410{
16411 switch (token->keyword)
16412 {
16413 case RID_CLASS:
16414 return class_type;
16415 case RID_STRUCT:
16416 return record_type;
16417 case RID_UNION:
16418 return union_type;
21526606 16419
a723baf1
MM
16420 default:
16421 return none_type;
16422 }
16423}
16424
16425/* Issue an error message if the CLASS_KEY does not match the TYPE. */
16426
16427static void
16428cp_parser_check_class_key (enum tag_types class_key, tree type)
16429{
16430 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
2a13a625 16431 pedwarn ("%qs tag used in naming %q#T",
a723baf1 16432 class_key == union_type ? "union"
21526606 16433 : class_key == record_type ? "struct" : "class",
a723baf1
MM
16434 type);
16435}
21526606 16436
cd0be382 16437/* Issue an error message if DECL is redeclared with different
37d407a1
KL
16438 access than its original declaration [class.access.spec/3].
16439 This applies to nested classes and nested class templates.
16440 [class.mem/1]. */
16441
2a13a625
GDR
16442static void
16443cp_parser_check_access_in_redeclaration (tree decl)
37d407a1
KL
16444{
16445 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16446 return;
16447
16448 if ((TREE_PRIVATE (decl)
16449 != (current_access_specifier == access_private_node))
16450 || (TREE_PROTECTED (decl)
16451 != (current_access_specifier == access_protected_node)))
2a13a625 16452 error ("%qD redeclared with different access", decl);
37d407a1
KL
16453}
16454
a723baf1 16455/* Look for the `template' keyword, as a syntactic disambiguator.
21526606 16456 Return TRUE iff it is present, in which case it will be
a723baf1
MM
16457 consumed. */
16458
16459static bool
16460cp_parser_optional_template_keyword (cp_parser *parser)
16461{
16462 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16463 {
16464 /* The `template' keyword can only be used within templates;
16465 outside templates the parser can always figure out what is a
16466 template and what is not. */
16467 if (!processing_template_decl)
16468 {
2a13a625 16469 error ("%<template%> (as a disambiguator) is only allowed "
a723baf1
MM
16470 "within templates");
16471 /* If this part of the token stream is rescanned, the same
16472 error message would be generated. So, we purge the token
16473 from the stream. */
16474 cp_lexer_purge_token (parser->lexer);
16475 return false;
16476 }
16477 else
16478 {
16479 /* Consume the `template' keyword. */
16480 cp_lexer_consume_token (parser->lexer);
16481 return true;
16482 }
16483 }
16484
16485 return false;
16486}
16487
2050a1bb
MM
16488/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16489 set PARSER->SCOPE, and perform other related actions. */
16490
16491static void
16492cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16493{
16494 tree value;
16495 tree check;
16496
16497 /* Get the stored value. */
16498 value = cp_lexer_consume_token (parser->lexer)->value;
16499 /* Perform any access checks that were deferred. */
16500 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c 16501 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
2050a1bb
MM
16502 /* Set the scope from the stored value. */
16503 parser->scope = TREE_VALUE (value);
16504 parser->qualifying_scope = TREE_TYPE (value);
16505 parser->object_scope = NULL_TREE;
16506}
16507
03fd3f84 16508/* Consume tokens up through a non-nested END token. */
a723baf1
MM
16509
16510static void
c162c75e
MA
16511cp_parser_cache_group (cp_parser *parser,
16512 enum cpp_ttype end,
16513 unsigned depth)
a723baf1
MM
16514{
16515 while (true)
16516 {
16517 cp_token *token;
16518
16519 /* Abort a parenthesized expression if we encounter a brace. */
16520 if ((end == CPP_CLOSE_PAREN || depth == 0)
16521 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16522 return;
a723baf1 16523 /* If we've reached the end of the file, stop. */
bc4071dd
RH
16524 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16525 || (end != CPP_PRAGMA_EOL
16526 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
a723baf1 16527 return;
4bfb8bba
MM
16528 /* Consume the next token. */
16529 token = cp_lexer_consume_token (parser->lexer);
a723baf1
MM
16530 /* See if it starts a new group. */
16531 if (token->type == CPP_OPEN_BRACE)
16532 {
c162c75e 16533 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
a723baf1
MM
16534 if (depth == 0)
16535 return;
16536 }
16537 else if (token->type == CPP_OPEN_PAREN)
c162c75e 16538 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
bc4071dd
RH
16539 else if (token->type == CPP_PRAGMA)
16540 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
a723baf1
MM
16541 else if (token->type == end)
16542 return;
16543 }
16544}
16545
16546/* Begin parsing tentatively. We always save tokens while parsing
16547 tentatively so that if the tentative parsing fails we can restore the
16548 tokens. */
16549
16550static void
94edc4ab 16551cp_parser_parse_tentatively (cp_parser* parser)
a723baf1
MM
16552{
16553 /* Enter a new parsing context. */
16554 parser->context = cp_parser_context_new (parser->context);
16555 /* Begin saving tokens. */
16556 cp_lexer_save_tokens (parser->lexer);
16557 /* In order to avoid repetitive access control error messages,
16558 access checks are queued up until we are no longer parsing
16559 tentatively. */
8d241e0b 16560 push_deferring_access_checks (dk_deferred);
a723baf1
MM
16561}
16562
16563/* Commit to the currently active tentative parse. */
16564
16565static void
94edc4ab 16566cp_parser_commit_to_tentative_parse (cp_parser* parser)
a723baf1
MM
16567{
16568 cp_parser_context *context;
16569 cp_lexer *lexer;
16570
16571 /* Mark all of the levels as committed. */
16572 lexer = parser->lexer;
16573 for (context = parser->context; context->next; context = context->next)
16574 {
16575 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16576 break;
16577 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16578 while (!cp_lexer_saving_tokens (lexer))
16579 lexer = lexer->next;
16580 cp_lexer_commit_tokens (lexer);
16581 }
16582}
16583
16584/* Abort the currently active tentative parse. All consumed tokens
16585 will be rolled back, and no diagnostics will be issued. */
16586
16587static void
94edc4ab 16588cp_parser_abort_tentative_parse (cp_parser* parser)
a723baf1
MM
16589{
16590 cp_parser_simulate_error (parser);
16591 /* Now, pretend that we want to see if the construct was
16592 successfully parsed. */
16593 cp_parser_parse_definitely (parser);
16594}
16595
34cd5ae7 16596/* Stop parsing tentatively. If a parse error has occurred, restore the
a723baf1
MM
16597 token stream. Otherwise, commit to the tokens we have consumed.
16598 Returns true if no error occurred; false otherwise. */
16599
16600static bool
94edc4ab 16601cp_parser_parse_definitely (cp_parser* parser)
a723baf1
MM
16602{
16603 bool error_occurred;
16604 cp_parser_context *context;
16605
34cd5ae7 16606 /* Remember whether or not an error occurred, since we are about to
a723baf1
MM
16607 destroy that information. */
16608 error_occurred = cp_parser_error_occurred (parser);
16609 /* Remove the topmost context from the stack. */
16610 context = parser->context;
16611 parser->context = context->next;
16612 /* If no parse errors occurred, commit to the tentative parse. */
16613 if (!error_occurred)
16614 {
16615 /* Commit to the tokens read tentatively, unless that was
16616 already done. */
16617 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16618 cp_lexer_commit_tokens (parser->lexer);
cf22909c
KL
16619
16620 pop_to_parent_deferring_access_checks ();
a723baf1
MM
16621 }
16622 /* Otherwise, if errors occurred, roll back our state so that things
16623 are just as they were before we began the tentative parse. */
16624 else
cf22909c
KL
16625 {
16626 cp_lexer_rollback_tokens (parser->lexer);
16627 pop_deferring_access_checks ();
16628 }
e5976695
MM
16629 /* Add the context to the front of the free list. */
16630 context->next = cp_parser_context_free_list;
16631 cp_parser_context_free_list = context;
16632
16633 return !error_occurred;
a723baf1
MM
16634}
16635
0b16f8f4
VR
16636/* Returns true if we are parsing tentatively and are not committed to
16637 this tentative parse. */
a723baf1
MM
16638
16639static bool
0b16f8f4 16640cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
a723baf1
MM
16641{
16642 return (cp_parser_parsing_tentatively (parser)
0b16f8f4 16643 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
a723baf1
MM
16644}
16645
4de8668e 16646/* Returns nonzero iff an error has occurred during the most recent
a723baf1 16647 tentative parse. */
21526606 16648
a723baf1 16649static bool
94edc4ab 16650cp_parser_error_occurred (cp_parser* parser)
a723baf1
MM
16651{
16652 return (cp_parser_parsing_tentatively (parser)
16653 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16654}
16655
4de8668e 16656/* Returns nonzero if GNU extensions are allowed. */
a723baf1
MM
16657
16658static bool
94edc4ab 16659cp_parser_allow_gnu_extensions_p (cp_parser* parser)
a723baf1
MM
16660{
16661 return parser->allow_gnu_extensions_p;
16662}
e58a9aa1
ZL
16663\f
16664/* Objective-C++ Productions */
16665
16666
16667/* Parse an Objective-C expression, which feeds into a primary-expression
16668 above.
16669
16670 objc-expression:
16671 objc-message-expression
16672 objc-string-literal
16673 objc-encode-expression
16674 objc-protocol-expression
16675 objc-selector-expression
16676
16677 Returns a tree representation of the expression. */
16678
16679static tree
16680cp_parser_objc_expression (cp_parser* parser)
16681{
16682 /* Try to figure out what kind of declaration is present. */
16683 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16684
16685 switch (kwd->type)
16686 {
16687 case CPP_OPEN_SQUARE:
16688 return cp_parser_objc_message_expression (parser);
16689
16690 case CPP_OBJC_STRING:
16691 kwd = cp_lexer_consume_token (parser->lexer);
16692 return objc_build_string_object (kwd->value);
16693
16694 case CPP_KEYWORD:
16695 switch (kwd->keyword)
16696 {
16697 case RID_AT_ENCODE:
16698 return cp_parser_objc_encode_expression (parser);
16699
16700 case RID_AT_PROTOCOL:
16701 return cp_parser_objc_protocol_expression (parser);
16702
16703 case RID_AT_SELECTOR:
16704 return cp_parser_objc_selector_expression (parser);
16705
16706 default:
16707 break;
16708 }
16709 default:
c85ce869 16710 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
e58a9aa1
ZL
16711 cp_parser_skip_to_end_of_block_or_statement (parser);
16712 }
16713
16714 return error_mark_node;
16715}
16716
16717/* Parse an Objective-C message expression.
16718
16719 objc-message-expression:
16720 [ objc-message-receiver objc-message-args ]
16721
16722 Returns a representation of an Objective-C message. */
16723
16724static tree
16725cp_parser_objc_message_expression (cp_parser* parser)
16726{
16727 tree receiver, messageargs;
16728
16729 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16730 receiver = cp_parser_objc_message_receiver (parser);
16731 messageargs = cp_parser_objc_message_args (parser);
16732 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16733
16734 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16735}
16736
16737/* Parse an objc-message-receiver.
16738
16739 objc-message-receiver:
e58a9aa1 16740 expression
660845bf 16741 simple-type-specifier
e58a9aa1
ZL
16742
16743 Returns a representation of the type or expression. */
16744
16745static tree
16746cp_parser_objc_message_receiver (cp_parser* parser)
16747{
16748 tree rcv;
e58a9aa1
ZL
16749
16750 /* An Objective-C message receiver may be either (1) a type
16751 or (2) an expression. */
16752 cp_parser_parse_tentatively (parser);
16753 rcv = cp_parser_expression (parser, false);
16754
16755 if (cp_parser_parse_definitely (parser))
16756 return rcv;
16757
660845bf
ZL
16758 rcv = cp_parser_simple_type_specifier (parser,
16759 /*decl_specs=*/NULL,
16760 CP_PARSER_FLAGS_NONE);
e58a9aa1
ZL
16761
16762 return objc_get_class_reference (rcv);
16763}
16764
16765/* Parse the arguments and selectors comprising an Objective-C message.
16766
16767 objc-message-args:
16768 objc-selector
16769 objc-selector-args
16770 objc-selector-args , objc-comma-args
16771
16772 objc-selector-args:
16773 objc-selector [opt] : assignment-expression
16774 objc-selector-args objc-selector [opt] : assignment-expression
16775
16776 objc-comma-args:
16777 assignment-expression
16778 objc-comma-args , assignment-expression
16779
16780 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16781 selector arguments and TREE_VALUE containing a list of comma
16782 arguments. */
16783
16784static tree
16785cp_parser_objc_message_args (cp_parser* parser)
16786{
16787 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16788 bool maybe_unary_selector_p = true;
16789 cp_token *token = cp_lexer_peek_token (parser->lexer);
16790
16791 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16792 {
16793 tree selector = NULL_TREE, arg;
16794
16795 if (token->type != CPP_COLON)
16796 selector = cp_parser_objc_selector (parser);
16797
16798 /* Detect if we have a unary selector. */
16799 if (maybe_unary_selector_p
16800 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16801 return build_tree_list (selector, NULL_TREE);
16802
16803 maybe_unary_selector_p = false;
16804 cp_parser_require (parser, CPP_COLON, "`:'");
16805 arg = cp_parser_assignment_expression (parser, false);
16806
16807 sel_args
16808 = chainon (sel_args,
16809 build_tree_list (selector, arg));
16810
16811 token = cp_lexer_peek_token (parser->lexer);
16812 }
16813
16814 /* Handle non-selector arguments, if any. */
16815 while (token->type == CPP_COMMA)
16816 {
16817 tree arg;
16818
16819 cp_lexer_consume_token (parser->lexer);
16820 arg = cp_parser_assignment_expression (parser, false);
16821
16822 addl_args
16823 = chainon (addl_args,
16824 build_tree_list (NULL_TREE, arg));
16825
16826 token = cp_lexer_peek_token (parser->lexer);
16827 }
16828
16829 return build_tree_list (sel_args, addl_args);
16830}
16831
16832/* Parse an Objective-C encode expression.
16833
16834 objc-encode-expression:
16835 @encode objc-typename
c8094d83 16836
e58a9aa1
ZL
16837 Returns an encoded representation of the type argument. */
16838
16839static tree
16840cp_parser_objc_encode_expression (cp_parser* parser)
16841{
16842 tree type;
16843
16844 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16845 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16846 type = complete_type (cp_parser_type_id (parser));
16847 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16848
16849 if (!type)
16850 {
c85ce869 16851 error ("%<@encode%> must specify a type as an argument");
e58a9aa1
ZL
16852 return error_mark_node;
16853 }
16854
16855 return objc_build_encode_expr (type);
16856}
16857
16858/* Parse an Objective-C @defs expression. */
16859
16860static tree
16861cp_parser_objc_defs_expression (cp_parser *parser)
16862{
16863 tree name;
16864
16865 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16866 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16867 name = cp_parser_identifier (parser);
16868 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16869
16870 return objc_get_class_ivars (name);
16871}
16872
16873/* Parse an Objective-C protocol expression.
16874
16875 objc-protocol-expression:
16876 @protocol ( identifier )
16877
16878 Returns a representation of the protocol expression. */
16879
16880static tree
16881cp_parser_objc_protocol_expression (cp_parser* parser)
16882{
16883 tree proto;
16884
16885 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16886 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16887 proto = cp_parser_identifier (parser);
16888 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16889
16890 return objc_build_protocol_expr (proto);
16891}
16892
16893/* Parse an Objective-C selector expression.
16894
16895 objc-selector-expression:
16896 @selector ( objc-method-signature )
16897
16898 objc-method-signature:
16899 objc-selector
16900 objc-selector-seq
16901
16902 objc-selector-seq:
16903 objc-selector :
16904 objc-selector-seq objc-selector :
16905
16906 Returns a representation of the method selector. */
16907
16908static tree
16909cp_parser_objc_selector_expression (cp_parser* parser)
16910{
16911 tree sel_seq = NULL_TREE;
16912 bool maybe_unary_selector_p = true;
16913 cp_token *token;
16914
16915 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16916 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16917 token = cp_lexer_peek_token (parser->lexer);
16918
d95036e3 16919 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
3db45ab5 16920 || token->type == CPP_SCOPE)
e58a9aa1
ZL
16921 {
16922 tree selector = NULL_TREE;
16923
d95036e3
AP
16924 if (token->type != CPP_COLON
16925 || token->type == CPP_SCOPE)
e58a9aa1
ZL
16926 selector = cp_parser_objc_selector (parser);
16927
d95036e3 16928 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
3db45ab5 16929 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
e58a9aa1 16930 {
d95036e3
AP
16931 /* Detect if we have a unary selector. */
16932 if (maybe_unary_selector_p)
16933 {
16934 sel_seq = selector;
16935 goto finish_selector;
16936 }
16937 else
16938 {
16939 cp_parser_error (parser, "expected %<:%>");
16940 }
e58a9aa1 16941 }
e58a9aa1 16942 maybe_unary_selector_p = false;
d95036e3 16943 token = cp_lexer_consume_token (parser->lexer);
3db45ab5 16944
d95036e3 16945 if (token->type == CPP_SCOPE)
3db45ab5 16946 {
d95036e3
AP
16947 sel_seq
16948 = chainon (sel_seq,
16949 build_tree_list (selector, NULL_TREE));
16950 sel_seq
16951 = chainon (sel_seq,
16952 build_tree_list (NULL_TREE, NULL_TREE));
16953 }
16954 else
16955 sel_seq
16956 = chainon (sel_seq,
16957 build_tree_list (selector, NULL_TREE));
e58a9aa1
ZL
16958
16959 token = cp_lexer_peek_token (parser->lexer);
16960 }
16961
16962 finish_selector:
16963 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16964
16965 return objc_build_selector_expr (sel_seq);
16966}
16967
16968/* Parse a list of identifiers.
16969
16970 objc-identifier-list:
16971 identifier
16972 objc-identifier-list , identifier
16973
16974 Returns a TREE_LIST of identifier nodes. */
16975
16976static tree
16977cp_parser_objc_identifier_list (cp_parser* parser)
16978{
16979 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16980 cp_token *sep = cp_lexer_peek_token (parser->lexer);
16981
16982 while (sep->type == CPP_COMMA)
16983 {
16984 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
c8094d83 16985 list = chainon (list,
e58a9aa1
ZL
16986 build_tree_list (NULL_TREE,
16987 cp_parser_identifier (parser)));
16988 sep = cp_lexer_peek_token (parser->lexer);
16989 }
c8094d83 16990
e58a9aa1
ZL
16991 return list;
16992}
16993
16994/* Parse an Objective-C alias declaration.
16995
16996 objc-alias-declaration:
16997 @compatibility_alias identifier identifier ;
16998
16999 This function registers the alias mapping with the Objective-C front-end.
17000 It returns nothing. */
17001
17002static void
17003cp_parser_objc_alias_declaration (cp_parser* parser)
17004{
17005 tree alias, orig;
17006
17007 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17008 alias = cp_parser_identifier (parser);
17009 orig = cp_parser_identifier (parser);
17010 objc_declare_alias (alias, orig);
17011 cp_parser_consume_semicolon_at_end_of_statement (parser);
17012}
17013
17014/* Parse an Objective-C class forward-declaration.
17015
17016 objc-class-declaration:
17017 @class objc-identifier-list ;
17018
17019 The function registers the forward declarations with the Objective-C
17020 front-end. It returns nothing. */
17021
17022static void
17023cp_parser_objc_class_declaration (cp_parser* parser)
17024{
17025 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17026 objc_declare_class (cp_parser_objc_identifier_list (parser));
17027 cp_parser_consume_semicolon_at_end_of_statement (parser);
17028}
17029
17030/* Parse a list of Objective-C protocol references.
17031
17032 objc-protocol-refs-opt:
17033 objc-protocol-refs [opt]
17034
17035 objc-protocol-refs:
17036 < objc-identifier-list >
17037
17038 Returns a TREE_LIST of identifiers, if any. */
17039
17040static tree
17041cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17042{
17043 tree protorefs = NULL_TREE;
17044
17045 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17046 {
17047 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17048 protorefs = cp_parser_objc_identifier_list (parser);
17049 cp_parser_require (parser, CPP_GREATER, "`>'");
17050 }
17051
17052 return protorefs;
17053}
17054
17055/* Parse a Objective-C visibility specification. */
17056
17057static void
17058cp_parser_objc_visibility_spec (cp_parser* parser)
17059{
17060 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17061
17062 switch (vis->keyword)
17063 {
17064 case RID_AT_PRIVATE:
17065 objc_set_visibility (2);
17066 break;
17067 case RID_AT_PROTECTED:
17068 objc_set_visibility (0);
17069 break;
17070 case RID_AT_PUBLIC:
17071 objc_set_visibility (1);
17072 break;
17073 default:
17074 return;
17075 }
a723baf1 17076
e58a9aa1
ZL
17077 /* Eat '@private'/'@protected'/'@public'. */
17078 cp_lexer_consume_token (parser->lexer);
17079}
17080
17081/* Parse an Objective-C method type. */
17082
17083static void
17084cp_parser_objc_method_type (cp_parser* parser)
17085{
17086 objc_set_method_type
17087 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17088 ? PLUS_EXPR
17089 : MINUS_EXPR);
17090}
17091
17092/* Parse an Objective-C protocol qualifier. */
17093
17094static tree
17095cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17096{
17097 tree quals = NULL_TREE, node;
17098 cp_token *token = cp_lexer_peek_token (parser->lexer);
17099
17100 node = token->value;
17101
17102 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17103 && (node == ridpointers [(int) RID_IN]
17104 || node == ridpointers [(int) RID_OUT]
17105 || node == ridpointers [(int) RID_INOUT]
17106 || node == ridpointers [(int) RID_BYCOPY]
0cbd7506 17107 || node == ridpointers [(int) RID_BYREF]
e58a9aa1
ZL
17108 || node == ridpointers [(int) RID_ONEWAY]))
17109 {
17110 quals = tree_cons (NULL_TREE, node, quals);
17111 cp_lexer_consume_token (parser->lexer);
17112 token = cp_lexer_peek_token (parser->lexer);
17113 node = token->value;
17114 }
17115
17116 return quals;
17117}
17118
17119/* Parse an Objective-C typename. */
17120
17121static tree
17122cp_parser_objc_typename (cp_parser* parser)
17123{
17124 tree typename = NULL_TREE;
17125
17126 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17127 {
17128 tree proto_quals, cp_type = NULL_TREE;
17129
17130 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17131 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17132
17133 /* An ObjC type name may consist of just protocol qualifiers, in which
17134 case the type shall default to 'id'. */
17135 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17136 cp_type = cp_parser_type_id (parser);
17137
17138 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17139 typename = build_tree_list (proto_quals, cp_type);
17140 }
17141
17142 return typename;
17143}
17144
17145/* Check to see if TYPE refers to an Objective-C selector name. */
17146
17147static bool
17148cp_parser_objc_selector_p (enum cpp_ttype type)
17149{
17150 return (type == CPP_NAME || type == CPP_KEYWORD
17151 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17152 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17153 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17154 || type == CPP_XOR || type == CPP_XOR_EQ);
17155}
17156
17157/* Parse an Objective-C selector. */
17158
17159static tree
17160cp_parser_objc_selector (cp_parser* parser)
17161{
17162 cp_token *token = cp_lexer_consume_token (parser->lexer);
c8094d83 17163
e58a9aa1
ZL
17164 if (!cp_parser_objc_selector_p (token->type))
17165 {
17166 error ("invalid Objective-C++ selector name");
17167 return error_mark_node;
17168 }
17169
17170 /* C++ operator names are allowed to appear in ObjC selectors. */
17171 switch (token->type)
17172 {
17173 case CPP_AND_AND: return get_identifier ("and");
17174 case CPP_AND_EQ: return get_identifier ("and_eq");
17175 case CPP_AND: return get_identifier ("bitand");
17176 case CPP_OR: return get_identifier ("bitor");
17177 case CPP_COMPL: return get_identifier ("compl");
17178 case CPP_NOT: return get_identifier ("not");
17179 case CPP_NOT_EQ: return get_identifier ("not_eq");
17180 case CPP_OR_OR: return get_identifier ("or");
17181 case CPP_OR_EQ: return get_identifier ("or_eq");
17182 case CPP_XOR: return get_identifier ("xor");
17183 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17184 default: return token->value;
17185 }
17186}
17187
17188/* Parse an Objective-C params list. */
17189
17190static tree
17191cp_parser_objc_method_keyword_params (cp_parser* parser)
17192{
17193 tree params = NULL_TREE;
17194 bool maybe_unary_selector_p = true;
17195 cp_token *token = cp_lexer_peek_token (parser->lexer);
17196
17197 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17198 {
17199 tree selector = NULL_TREE, typename, identifier;
17200
17201 if (token->type != CPP_COLON)
17202 selector = cp_parser_objc_selector (parser);
17203
17204 /* Detect if we have a unary selector. */
17205 if (maybe_unary_selector_p
17206 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17207 return selector;
17208
17209 maybe_unary_selector_p = false;
17210 cp_parser_require (parser, CPP_COLON, "`:'");
17211 typename = cp_parser_objc_typename (parser);
17212 identifier = cp_parser_identifier (parser);
17213
17214 params
17215 = chainon (params,
c8094d83 17216 objc_build_keyword_decl (selector,
e58a9aa1
ZL
17217 typename,
17218 identifier));
17219
17220 token = cp_lexer_peek_token (parser->lexer);
17221 }
17222
17223 return params;
17224}
17225
17226/* Parse the non-keyword Objective-C params. */
17227
17228static tree
17229cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17230{
17231 tree params = make_node (TREE_LIST);
17232 cp_token *token = cp_lexer_peek_token (parser->lexer);
17233 *ellipsisp = false; /* Initially, assume no ellipsis. */
17234
17235 while (token->type == CPP_COMMA)
17236 {
17237 cp_parameter_declarator *parmdecl;
17238 tree parm;
17239
17240 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17241 token = cp_lexer_peek_token (parser->lexer);
17242
17243 if (token->type == CPP_ELLIPSIS)
17244 {
17245 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17246 *ellipsisp = true;
17247 break;
17248 }
17249
17250 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17251 parm = grokdeclarator (parmdecl->declarator,
17252 &parmdecl->decl_specifiers,
c8094d83 17253 PARM, /*initialized=*/0,
e58a9aa1
ZL
17254 /*attrlist=*/NULL);
17255
17256 chainon (params, build_tree_list (NULL_TREE, parm));
17257 token = cp_lexer_peek_token (parser->lexer);
17258 }
17259
17260 return params;
17261}
17262
17263/* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17264
17265static void
17266cp_parser_objc_interstitial_code (cp_parser* parser)
17267{
17268 cp_token *token = cp_lexer_peek_token (parser->lexer);
17269
17270 /* If the next token is `extern' and the following token is a string
17271 literal, then we have a linkage specification. */
17272 if (token->keyword == RID_EXTERN
17273 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17274 cp_parser_linkage_specification (parser);
17275 /* Handle #pragma, if any. */
17276 else if (token->type == CPP_PRAGMA)
bc4071dd 17277 cp_parser_pragma (parser, pragma_external);
e58a9aa1
ZL
17278 /* Allow stray semicolons. */
17279 else if (token->type == CPP_SEMICOLON)
17280 cp_lexer_consume_token (parser->lexer);
17281 /* Finally, try to parse a block-declaration, or a function-definition. */
17282 else
17283 cp_parser_block_declaration (parser, /*statement_p=*/false);
17284}
17285
17286/* Parse a method signature. */
17287
17288static tree
17289cp_parser_objc_method_signature (cp_parser* parser)
17290{
17291 tree rettype, kwdparms, optparms;
17292 bool ellipsis = false;
17293
17294 cp_parser_objc_method_type (parser);
17295 rettype = cp_parser_objc_typename (parser);
17296 kwdparms = cp_parser_objc_method_keyword_params (parser);
17297 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17298
17299 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17300}
17301
17302/* Pars an Objective-C method prototype list. */
17303
17304static void
17305cp_parser_objc_method_prototype_list (cp_parser* parser)
17306{
17307 cp_token *token = cp_lexer_peek_token (parser->lexer);
17308
17309 while (token->keyword != RID_AT_END)
17310 {
17311 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17312 {
17313 objc_add_method_declaration
17314 (cp_parser_objc_method_signature (parser));
17315 cp_parser_consume_semicolon_at_end_of_statement (parser);
17316 }
17317 else
17318 /* Allow for interspersed non-ObjC++ code. */
17319 cp_parser_objc_interstitial_code (parser);
17320
17321 token = cp_lexer_peek_token (parser->lexer);
17322 }
17323
17324 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17325 objc_finish_interface ();
17326}
17327
17328/* Parse an Objective-C method definition list. */
17329
17330static void
17331cp_parser_objc_method_definition_list (cp_parser* parser)
17332{
17333 cp_token *token = cp_lexer_peek_token (parser->lexer);
17334
17335 while (token->keyword != RID_AT_END)
17336 {
17337 tree meth;
17338
17339 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17340 {
17341 push_deferring_access_checks (dk_deferred);
17342 objc_start_method_definition
17343 (cp_parser_objc_method_signature (parser));
17344
17345 /* For historical reasons, we accept an optional semicolon. */
17346 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17347 cp_lexer_consume_token (parser->lexer);
17348
17349 perform_deferred_access_checks ();
17350 stop_deferring_access_checks ();
17351 meth = cp_parser_function_definition_after_declarator (parser,
17352 false);
17353 pop_deferring_access_checks ();
17354 objc_finish_method_definition (meth);
17355 }
17356 else
17357 /* Allow for interspersed non-ObjC++ code. */
17358 cp_parser_objc_interstitial_code (parser);
17359
17360 token = cp_lexer_peek_token (parser->lexer);
17361 }
17362
17363 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17364 objc_finish_implementation ();
17365}
17366
17367/* Parse Objective-C ivars. */
17368
17369static void
17370cp_parser_objc_class_ivars (cp_parser* parser)
17371{
17372 cp_token *token = cp_lexer_peek_token (parser->lexer);
17373
17374 if (token->type != CPP_OPEN_BRACE)
17375 return; /* No ivars specified. */
17376
17377 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17378 token = cp_lexer_peek_token (parser->lexer);
17379
17380 while (token->type != CPP_CLOSE_BRACE)
17381 {
17382 cp_decl_specifier_seq declspecs;
17383 int decl_class_or_enum_p;
17384 tree prefix_attributes;
17385
17386 cp_parser_objc_visibility_spec (parser);
17387
17388 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17389 break;
17390
17391 cp_parser_decl_specifier_seq (parser,
17392 CP_PARSER_FLAGS_OPTIONAL,
17393 &declspecs,
17394 &decl_class_or_enum_p);
17395 prefix_attributes = declspecs.attributes;
17396 declspecs.attributes = NULL_TREE;
17397
17398 /* Keep going until we hit the `;' at the end of the
17399 declaration. */
17400 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17401 {
17402 tree width = NULL_TREE, attributes, first_attribute, decl;
17403 cp_declarator *declarator = NULL;
17404 int ctor_dtor_or_conv_p;
17405
17406 /* Check for a (possibly unnamed) bitfield declaration. */
17407 token = cp_lexer_peek_token (parser->lexer);
17408 if (token->type == CPP_COLON)
17409 goto eat_colon;
17410
17411 if (token->type == CPP_NAME
17412 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17413 == CPP_COLON))
17414 {
17415 /* Get the name of the bitfield. */
17416 declarator = make_id_declarator (NULL_TREE,
d85d3d57
MM
17417 cp_parser_identifier (parser),
17418 sfk_none);
e58a9aa1
ZL
17419
17420 eat_colon:
17421 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17422 /* Get the width of the bitfield. */
17423 width
17424 = cp_parser_constant_expression (parser,
17425 /*allow_non_constant=*/false,
17426 NULL);
17427 }
17428 else
17429 {
17430 /* Parse the declarator. */
c8094d83 17431 declarator
e58a9aa1
ZL
17432 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17433 &ctor_dtor_or_conv_p,
17434 /*parenthesized_p=*/NULL,
17435 /*member_p=*/false);
17436 }
17437
17438 /* Look for attributes that apply to the ivar. */
17439 attributes = cp_parser_attributes_opt (parser);
17440 /* Remember which attributes are prefix attributes and
17441 which are not. */
17442 first_attribute = attributes;
17443 /* Combine the attributes. */
17444 attributes = chainon (prefix_attributes, attributes);
17445
17446 if (width)
17447 {
17448 /* Create the bitfield declaration. */
17449 decl = grokbitfield (declarator, &declspecs, width);
17450 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17451 }
17452 else
3db45ab5 17453 decl = grokfield (declarator, &declspecs,
d174af6c 17454 NULL_TREE, /*init_const_expr_p=*/false,
e58a9aa1 17455 NULL_TREE, attributes);
c8094d83 17456
e58a9aa1
ZL
17457 /* Add the instance variable. */
17458 objc_add_instance_variable (decl);
17459
17460 /* Reset PREFIX_ATTRIBUTES. */
17461 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17462 attributes = TREE_CHAIN (attributes);
17463 if (attributes)
17464 TREE_CHAIN (attributes) = NULL_TREE;
17465
17466 token = cp_lexer_peek_token (parser->lexer);
17467
17468 if (token->type == CPP_COMMA)
17469 {
17470 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17471 continue;
17472 }
17473 break;
17474 }
17475
17476 cp_parser_consume_semicolon_at_end_of_statement (parser);
17477 token = cp_lexer_peek_token (parser->lexer);
17478 }
17479
17480 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17481 /* For historical reasons, we accept an optional semicolon. */
17482 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17483 cp_lexer_consume_token (parser->lexer);
17484}
17485
17486/* Parse an Objective-C protocol declaration. */
17487
17488static void
17489cp_parser_objc_protocol_declaration (cp_parser* parser)
17490{
17491 tree proto, protorefs;
17492 cp_token *tok;
17493
17494 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17495 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17496 {
c85ce869 17497 error ("identifier expected after %<@protocol%>");
e58a9aa1
ZL
17498 goto finish;
17499 }
17500
128a79fb 17501 /* See if we have a forward declaration or a definition. */
e58a9aa1 17502 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
c8094d83 17503
e58a9aa1
ZL
17504 /* Try a forward declaration first. */
17505 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17506 {
17507 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
c8094d83 17508 finish:
e58a9aa1 17509 cp_parser_consume_semicolon_at_end_of_statement (parser);
c8094d83 17510 }
e58a9aa1
ZL
17511
17512 /* Ok, we got a full-fledged definition (or at least should). */
17513 else
17514 {
17515 proto = cp_parser_identifier (parser);
17516 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17517 objc_start_protocol (proto, protorefs);
17518 cp_parser_objc_method_prototype_list (parser);
17519 }
17520}
17521
17522/* Parse an Objective-C superclass or category. */
17523
17524static void
17525cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17526 tree *categ)
17527{
17528 cp_token *next = cp_lexer_peek_token (parser->lexer);
17529
17530 *super = *categ = NULL_TREE;
17531 if (next->type == CPP_COLON)
17532 {
17533 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17534 *super = cp_parser_identifier (parser);
17535 }
17536 else if (next->type == CPP_OPEN_PAREN)
17537 {
17538 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17539 *categ = cp_parser_identifier (parser);
17540 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17541 }
17542}
17543
17544/* Parse an Objective-C class interface. */
17545
17546static void
17547cp_parser_objc_class_interface (cp_parser* parser)
17548{
17549 tree name, super, categ, protos;
17550
17551 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17552 name = cp_parser_identifier (parser);
17553 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17554 protos = cp_parser_objc_protocol_refs_opt (parser);
17555
17556 /* We have either a class or a category on our hands. */
17557 if (categ)
17558 objc_start_category_interface (name, categ, protos);
17559 else
17560 {
17561 objc_start_class_interface (name, super, protos);
17562 /* Handle instance variable declarations, if any. */
17563 cp_parser_objc_class_ivars (parser);
17564 objc_continue_interface ();
17565 }
17566
17567 cp_parser_objc_method_prototype_list (parser);
17568}
17569
17570/* Parse an Objective-C class implementation. */
17571
17572static void
17573cp_parser_objc_class_implementation (cp_parser* parser)
17574{
17575 tree name, super, categ;
17576
17577 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17578 name = cp_parser_identifier (parser);
17579 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17580
17581 /* We have either a class or a category on our hands. */
17582 if (categ)
17583 objc_start_category_implementation (name, categ);
17584 else
17585 {
17586 objc_start_class_implementation (name, super);
17587 /* Handle instance variable declarations, if any. */
17588 cp_parser_objc_class_ivars (parser);
17589 objc_continue_implementation ();
17590 }
17591
17592 cp_parser_objc_method_definition_list (parser);
17593}
17594
17595/* Consume the @end token and finish off the implementation. */
17596
17597static void
17598cp_parser_objc_end_implementation (cp_parser* parser)
17599{
17600 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17601 objc_finish_implementation ();
17602}
17603
17604/* Parse an Objective-C declaration. */
17605
17606static void
17607cp_parser_objc_declaration (cp_parser* parser)
17608{
17609 /* Try to figure out what kind of declaration is present. */
17610 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17611
17612 switch (kwd->keyword)
17613 {
17614 case RID_AT_ALIAS:
17615 cp_parser_objc_alias_declaration (parser);
17616 break;
17617 case RID_AT_CLASS:
17618 cp_parser_objc_class_declaration (parser);
17619 break;
17620 case RID_AT_PROTOCOL:
17621 cp_parser_objc_protocol_declaration (parser);
17622 break;
17623 case RID_AT_INTERFACE:
17624 cp_parser_objc_class_interface (parser);
17625 break;
17626 case RID_AT_IMPLEMENTATION:
17627 cp_parser_objc_class_implementation (parser);
17628 break;
17629 case RID_AT_END:
17630 cp_parser_objc_end_implementation (parser);
17631 break;
17632 default:
c85ce869 17633 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
e58a9aa1
ZL
17634 cp_parser_skip_to_end_of_block_or_statement (parser);
17635 }
17636}
17637
17638/* Parse an Objective-C try-catch-finally statement.
17639
17640 objc-try-catch-finally-stmt:
17641 @try compound-statement objc-catch-clause-seq [opt]
17642 objc-finally-clause [opt]
17643
17644 objc-catch-clause-seq:
17645 objc-catch-clause objc-catch-clause-seq [opt]
17646
17647 objc-catch-clause:
17648 @catch ( exception-declaration ) compound-statement
17649
17650 objc-finally-clause
17651 @finally compound-statement
17652
17653 Returns NULL_TREE. */
17654
17655static tree
17656cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17657 location_t location;
17658 tree stmt;
17659
17660 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17661 location = cp_lexer_peek_token (parser->lexer)->location;
17662 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17663 node, lest it get absorbed into the surrounding block. */
17664 stmt = push_stmt_list ();
17665 cp_parser_compound_statement (parser, NULL, false);
17666 objc_begin_try_stmt (location, pop_stmt_list (stmt));
c8094d83 17667
e58a9aa1
ZL
17668 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17669 {
17670 cp_parameter_declarator *parmdecl;
17671 tree parm;
17672
17673 cp_lexer_consume_token (parser->lexer);
17674 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17675 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17676 parm = grokdeclarator (parmdecl->declarator,
17677 &parmdecl->decl_specifiers,
c8094d83 17678 PARM, /*initialized=*/0,
e58a9aa1
ZL
17679 /*attrlist=*/NULL);
17680 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17681 objc_begin_catch_clause (parm);
17682 cp_parser_compound_statement (parser, NULL, false);
17683 objc_finish_catch_clause ();
17684 }
17685
17686 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17687 {
17688 cp_lexer_consume_token (parser->lexer);
17689 location = cp_lexer_peek_token (parser->lexer)->location;
17690 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17691 node, lest it get absorbed into the surrounding block. */
17692 stmt = push_stmt_list ();
17693 cp_parser_compound_statement (parser, NULL, false);
17694 objc_build_finally_clause (location, pop_stmt_list (stmt));
17695 }
17696
17697 return objc_finish_try_stmt ();
17698}
17699
17700/* Parse an Objective-C synchronized statement.
17701
17702 objc-synchronized-stmt:
17703 @synchronized ( expression ) compound-statement
17704
17705 Returns NULL_TREE. */
17706
17707static tree
17708cp_parser_objc_synchronized_statement (cp_parser *parser) {
17709 location_t location;
17710 tree lock, stmt;
17711
17712 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17713
17714 location = cp_lexer_peek_token (parser->lexer)->location;
17715 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17716 lock = cp_parser_expression (parser, false);
17717 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17718
17719 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17720 node, lest it get absorbed into the surrounding block. */
17721 stmt = push_stmt_list ();
17722 cp_parser_compound_statement (parser, NULL, false);
17723
17724 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17725}
17726
17727/* Parse an Objective-C throw statement.
17728
17729 objc-throw-stmt:
17730 @throw assignment-expression [opt] ;
17731
17732 Returns a constructed '@throw' statement. */
17733
17734static tree
17735cp_parser_objc_throw_statement (cp_parser *parser) {
17736 tree expr = NULL_TREE;
17737
17738 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17739
17740 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17741 expr = cp_parser_assignment_expression (parser, false);
17742
17743 cp_parser_consume_semicolon_at_end_of_statement (parser);
17744
17745 return objc_build_throw_stmt (expr);
17746}
17747
17748/* Parse an Objective-C statement. */
17749
17750static tree
17751cp_parser_objc_statement (cp_parser * parser) {
17752 /* Try to figure out what kind of declaration is present. */
17753 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17754
17755 switch (kwd->keyword)
17756 {
17757 case RID_AT_TRY:
17758 return cp_parser_objc_try_catch_finally_statement (parser);
17759 case RID_AT_SYNCHRONIZED:
17760 return cp_parser_objc_synchronized_statement (parser);
17761 case RID_AT_THROW:
17762 return cp_parser_objc_throw_statement (parser);
17763 default:
c85ce869 17764 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
e58a9aa1
ZL
17765 cp_parser_skip_to_end_of_block_or_statement (parser);
17766 }
17767
17768 return error_mark_node;
17769}
1799e5d5
RH
17770\f
17771/* OpenMP 2.5 parsing routines. */
17772
17773/* All OpenMP clauses. OpenMP 2.5. */
17774typedef enum pragma_omp_clause {
17775 PRAGMA_OMP_CLAUSE_NONE = 0,
17776
17777 PRAGMA_OMP_CLAUSE_COPYIN,
17778 PRAGMA_OMP_CLAUSE_COPYPRIVATE,
17779 PRAGMA_OMP_CLAUSE_DEFAULT,
17780 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
17781 PRAGMA_OMP_CLAUSE_IF,
17782 PRAGMA_OMP_CLAUSE_LASTPRIVATE,
17783 PRAGMA_OMP_CLAUSE_NOWAIT,
17784 PRAGMA_OMP_CLAUSE_NUM_THREADS,
17785 PRAGMA_OMP_CLAUSE_ORDERED,
17786 PRAGMA_OMP_CLAUSE_PRIVATE,
17787 PRAGMA_OMP_CLAUSE_REDUCTION,
17788 PRAGMA_OMP_CLAUSE_SCHEDULE,
17789 PRAGMA_OMP_CLAUSE_SHARED
17790} pragma_omp_clause;
17791
17792/* Returns name of the next clause.
17793 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17794 the token is not consumed. Otherwise appropriate pragma_omp_clause is
17795 returned and the token is consumed. */
17796
17797static pragma_omp_clause
17798cp_parser_omp_clause_name (cp_parser *parser)
17799{
17800 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
17801
17802 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
17803 result = PRAGMA_OMP_CLAUSE_IF;
17804 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
17805 result = PRAGMA_OMP_CLAUSE_DEFAULT;
17806 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
17807 result = PRAGMA_OMP_CLAUSE_PRIVATE;
17808 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17809 {
17810 tree id = cp_lexer_peek_token (parser->lexer)->value;
17811 const char *p = IDENTIFIER_POINTER (id);
17812
17813 switch (p[0])
17814 {
17815 case 'c':
17816 if (!strcmp ("copyin", p))
17817 result = PRAGMA_OMP_CLAUSE_COPYIN;
3db45ab5 17818 else if (!strcmp ("copyprivate", p))
1799e5d5
RH
17819 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
17820 break;
17821 case 'f':
17822 if (!strcmp ("firstprivate", p))
17823 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
17824 break;
17825 case 'l':
17826 if (!strcmp ("lastprivate", p))
17827 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
17828 break;
17829 case 'n':
17830 if (!strcmp ("nowait", p))
17831 result = PRAGMA_OMP_CLAUSE_NOWAIT;
17832 else if (!strcmp ("num_threads", p))
17833 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
17834 break;
17835 case 'o':
17836 if (!strcmp ("ordered", p))
17837 result = PRAGMA_OMP_CLAUSE_ORDERED;
17838 break;
17839 case 'r':
17840 if (!strcmp ("reduction", p))
17841 result = PRAGMA_OMP_CLAUSE_REDUCTION;
17842 break;
17843 case 's':
17844 if (!strcmp ("schedule", p))
17845 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
17846 else if (!strcmp ("shared", p))
17847 result = PRAGMA_OMP_CLAUSE_SHARED;
17848 break;
17849 }
17850 }
a723baf1 17851
1799e5d5
RH
17852 if (result != PRAGMA_OMP_CLAUSE_NONE)
17853 cp_lexer_consume_token (parser->lexer);
a723baf1 17854
1799e5d5
RH
17855 return result;
17856}
bc4071dd 17857
1799e5d5 17858/* Validate that a clause of the given type does not already exist. */
bc4071dd
RH
17859
17860static void
1799e5d5 17861check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
bc4071dd 17862{
1799e5d5 17863 tree c;
bc4071dd 17864
1799e5d5
RH
17865 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
17866 if (OMP_CLAUSE_CODE (c) == code)
17867 {
17868 error ("too many %qs clauses", name);
17869 break;
17870 }
17871}
bc4071dd 17872
1799e5d5
RH
17873/* OpenMP 2.5:
17874 variable-list:
17875 identifier
17876 variable-list , identifier
17877
17878 In addition, we match a closing parenthesis. An opening parenthesis
17879 will have been consumed by the caller.
17880
17881 If KIND is nonzero, create the appropriate node and install the decl
17882 in OMP_CLAUSE_DECL and add the node to the head of the list.
17883
17884 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17885 return the list created. */
17886
17887static tree
17888cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
17889 tree list)
17890{
17891 while (1)
bc4071dd 17892 {
1799e5d5 17893 tree name, decl;
bc4071dd 17894
1799e5d5
RH
17895 name = cp_parser_id_expression (parser, /*template_p=*/false,
17896 /*check_dependency_p=*/true,
17897 /*template_p=*/NULL,
fa6098f8
MM
17898 /*declarator_p=*/false,
17899 /*optional_p=*/false);
1799e5d5
RH
17900 if (name == error_mark_node)
17901 goto skip_comma;
17902
17903 decl = cp_parser_lookup_name_simple (parser, name);
17904 if (decl == error_mark_node)
17905 cp_parser_name_lookup_error (parser, name, decl, NULL);
17906 else if (kind != 0)
17907 {
17908 tree u = build_omp_clause (kind);
17909 OMP_CLAUSE_DECL (u) = decl;
17910 OMP_CLAUSE_CHAIN (u) = list;
17911 list = u;
17912 }
17913 else
17914 list = tree_cons (decl, NULL_TREE, list);
17915
17916 get_comma:
17917 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17918 break;
17919 cp_lexer_consume_token (parser->lexer);
17920 }
17921
17922 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17923 {
17924 int ending;
17925
17926 /* Try to resync to an unnested comma. Copied from
17927 cp_parser_parenthesized_expression_list. */
17928 skip_comma:
17929 ending = cp_parser_skip_to_closing_parenthesis (parser,
17930 /*recovering=*/true,
17931 /*or_comma=*/true,
17932 /*consume_paren=*/true);
17933 if (ending < 0)
17934 goto get_comma;
17935 }
17936
17937 return list;
17938}
17939
17940/* Similarly, but expect leading and trailing parenthesis. This is a very
17941 common case for omp clauses. */
17942
17943static tree
17944cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
17945{
17946 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17947 return cp_parser_omp_var_list_no_open (parser, kind, list);
17948 return list;
17949}
17950
17951/* OpenMP 2.5:
17952 default ( shared | none ) */
17953
17954static tree
17955cp_parser_omp_clause_default (cp_parser *parser, tree list)
17956{
17957 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
17958 tree c;
17959
17960 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17961 return list;
17962 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17963 {
17964 tree id = cp_lexer_peek_token (parser->lexer)->value;
17965 const char *p = IDENTIFIER_POINTER (id);
17966
17967 switch (p[0])
17968 {
17969 case 'n':
17970 if (strcmp ("none", p) != 0)
17971 goto invalid_kind;
17972 kind = OMP_CLAUSE_DEFAULT_NONE;
17973 break;
17974
17975 case 's':
17976 if (strcmp ("shared", p) != 0)
17977 goto invalid_kind;
17978 kind = OMP_CLAUSE_DEFAULT_SHARED;
17979 break;
17980
17981 default:
17982 goto invalid_kind;
17983 }
17984
17985 cp_lexer_consume_token (parser->lexer);
bc4071dd
RH
17986 }
17987 else
1799e5d5
RH
17988 {
17989 invalid_kind:
17990 cp_parser_error (parser, "expected %<none%> or %<shared%>");
17991 }
bc4071dd 17992
1799e5d5
RH
17993 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17994 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
17995 /*or_comma=*/false,
17996 /*consume_paren=*/true);
3db45ab5 17997
1799e5d5
RH
17998 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
17999 return list;
bc4071dd 18000
1799e5d5
RH
18001 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18002 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18003 OMP_CLAUSE_CHAIN (c) = list;
18004 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
bc4071dd 18005
1799e5d5 18006 return c;
bc4071dd
RH
18007}
18008
1799e5d5
RH
18009/* OpenMP 2.5:
18010 if ( expression ) */
bc4071dd 18011
1799e5d5
RH
18012static tree
18013cp_parser_omp_clause_if (cp_parser *parser, tree list)
bc4071dd 18014{
1799e5d5 18015 tree t, c;
bc4071dd 18016
1799e5d5
RH
18017 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18018 return list;
bc4071dd 18019
1799e5d5
RH
18020 t = cp_parser_condition (parser);
18021
18022 if (t == error_mark_node
18023 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18024 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18025 /*or_comma=*/false,
18026 /*consume_paren=*/true);
18027
18028 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18029
18030 c = build_omp_clause (OMP_CLAUSE_IF);
18031 OMP_CLAUSE_IF_EXPR (c) = t;
18032 OMP_CLAUSE_CHAIN (c) = list;
18033
18034 return c;
18035}
18036
18037/* OpenMP 2.5:
18038 nowait */
18039
18040static tree
18041cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18042{
18043 tree c;
18044
18045 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18046
18047 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18048 OMP_CLAUSE_CHAIN (c) = list;
18049 return c;
18050}
18051
18052/* OpenMP 2.5:
18053 num_threads ( expression ) */
18054
18055static tree
18056cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18057{
18058 tree t, c;
18059
18060 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18061 return list;
18062
18063 t = cp_parser_expression (parser, false);
18064
18065 if (t == error_mark_node
18066 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18067 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18068 /*or_comma=*/false,
18069 /*consume_paren=*/true);
18070
18071 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18072
18073 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18074 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18075 OMP_CLAUSE_CHAIN (c) = list;
18076
18077 return c;
18078}
18079
18080/* OpenMP 2.5:
18081 ordered */
18082
18083static tree
18084cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18085{
18086 tree c;
18087
18088 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18089
18090 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18091 OMP_CLAUSE_CHAIN (c) = list;
18092 return c;
18093}
18094
18095/* OpenMP 2.5:
18096 reduction ( reduction-operator : variable-list )
18097
18098 reduction-operator:
18099 One of: + * - & ^ | && || */
18100
18101static tree
18102cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18103{
18104 enum tree_code code;
18105 tree nlist, c;
18106
18107 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18108 return list;
18109
18110 switch (cp_lexer_peek_token (parser->lexer)->type)
bc4071dd 18111 {
1799e5d5
RH
18112 case CPP_PLUS:
18113 code = PLUS_EXPR;
18114 break;
18115 case CPP_MULT:
18116 code = MULT_EXPR;
18117 break;
18118 case CPP_MINUS:
18119 code = MINUS_EXPR;
18120 break;
18121 case CPP_AND:
18122 code = BIT_AND_EXPR;
18123 break;
18124 case CPP_XOR:
18125 code = BIT_XOR_EXPR;
18126 break;
18127 case CPP_OR:
18128 code = BIT_IOR_EXPR;
18129 break;
18130 case CPP_AND_AND:
18131 code = TRUTH_ANDIF_EXPR;
18132 break;
18133 case CPP_OR_OR:
18134 code = TRUTH_ORIF_EXPR;
bc4071dd 18135 break;
bc4071dd 18136 default:
1799e5d5
RH
18137 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18138 resync_fail:
18139 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18140 /*or_comma=*/false,
18141 /*consume_paren=*/true);
18142 return list;
18143 }
18144 cp_lexer_consume_token (parser->lexer);
18145
18146 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18147 goto resync_fail;
18148
18149 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18150 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18151 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18152
18153 return nlist;
18154}
18155
18156/* OpenMP 2.5:
18157 schedule ( schedule-kind )
18158 schedule ( schedule-kind , expression )
18159
18160 schedule-kind:
3db45ab5 18161 static | dynamic | guided | runtime */
1799e5d5
RH
18162
18163static tree
18164cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18165{
18166 tree c, t;
18167
18168 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18169 return list;
18170
18171 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18172
18173 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18174 {
18175 tree id = cp_lexer_peek_token (parser->lexer)->value;
18176 const char *p = IDENTIFIER_POINTER (id);
18177
18178 switch (p[0])
18179 {
18180 case 'd':
18181 if (strcmp ("dynamic", p) != 0)
18182 goto invalid_kind;
18183 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18184 break;
18185
3db45ab5 18186 case 'g':
1799e5d5
RH
18187 if (strcmp ("guided", p) != 0)
18188 goto invalid_kind;
18189 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18190 break;
18191
18192 case 'r':
18193 if (strcmp ("runtime", p) != 0)
18194 goto invalid_kind;
18195 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18196 break;
18197
18198 default:
18199 goto invalid_kind;
18200 }
18201 }
18202 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18203 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18204 else
18205 goto invalid_kind;
18206 cp_lexer_consume_token (parser->lexer);
18207
18208 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18209 {
18210 cp_lexer_consume_token (parser->lexer);
18211
18212 t = cp_parser_assignment_expression (parser, false);
18213
18214 if (t == error_mark_node)
18215 goto resync_fail;
18216 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18217 error ("schedule %<runtime%> does not take "
18218 "a %<chunk_size%> parameter");
18219 else
18220 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18221
18222 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18223 goto resync_fail;
18224 }
18225 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18226 goto resync_fail;
18227
18228 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18229 OMP_CLAUSE_CHAIN (c) = list;
18230 return c;
18231
18232 invalid_kind:
18233 cp_parser_error (parser, "invalid schedule kind");
18234 resync_fail:
18235 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18236 /*or_comma=*/false,
18237 /*consume_paren=*/true);
18238 return list;
18239}
18240
18241/* Parse all OpenMP clauses. The set clauses allowed by the directive
18242 is a bitmask in MASK. Return the list of clauses found; the result
18243 of clause default goes in *pdefault. */
18244
18245static tree
18246cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18247 const char *where, cp_token *pragma_tok)
18248{
18249 tree clauses = NULL;
18250
18251 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18252 {
18253 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18254 const char *c_name;
18255 tree prev = clauses;
18256
18257 switch (c_kind)
18258 {
18259 case PRAGMA_OMP_CLAUSE_COPYIN:
18260 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18261 c_name = "copyin";
18262 break;
18263 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18264 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18265 clauses);
18266 c_name = "copyprivate";
18267 break;
18268 case PRAGMA_OMP_CLAUSE_DEFAULT:
18269 clauses = cp_parser_omp_clause_default (parser, clauses);
18270 c_name = "default";
18271 break;
18272 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18273 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18274 clauses);
18275 c_name = "firstprivate";
18276 break;
18277 case PRAGMA_OMP_CLAUSE_IF:
18278 clauses = cp_parser_omp_clause_if (parser, clauses);
18279 c_name = "if";
18280 break;
18281 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18282 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18283 clauses);
18284 c_name = "lastprivate";
18285 break;
18286 case PRAGMA_OMP_CLAUSE_NOWAIT:
18287 clauses = cp_parser_omp_clause_nowait (parser, clauses);
18288 c_name = "nowait";
18289 break;
18290 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18291 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18292 c_name = "num_threads";
18293 break;
18294 case PRAGMA_OMP_CLAUSE_ORDERED:
18295 clauses = cp_parser_omp_clause_ordered (parser, clauses);
18296 c_name = "ordered";
18297 break;
18298 case PRAGMA_OMP_CLAUSE_PRIVATE:
18299 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18300 clauses);
18301 c_name = "private";
18302 break;
18303 case PRAGMA_OMP_CLAUSE_REDUCTION:
18304 clauses = cp_parser_omp_clause_reduction (parser, clauses);
18305 c_name = "reduction";
18306 break;
18307 case PRAGMA_OMP_CLAUSE_SCHEDULE:
18308 clauses = cp_parser_omp_clause_schedule (parser, clauses);
18309 c_name = "schedule";
18310 break;
18311 case PRAGMA_OMP_CLAUSE_SHARED:
18312 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18313 clauses);
18314 c_name = "shared";
18315 break;
18316 default:
18317 cp_parser_error (parser, "expected %<#pragma omp%> clause");
18318 goto saw_error;
18319 }
18320
18321 if (((mask >> c_kind) & 1) == 0)
18322 {
18323 /* Remove the invalid clause(s) from the list to avoid
18324 confusing the rest of the compiler. */
18325 clauses = prev;
18326 error ("%qs is not valid for %qs", c_name, where);
18327 }
18328 }
18329 saw_error:
18330 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18331 return finish_omp_clauses (clauses);
18332}
18333
18334/* OpenMP 2.5:
18335 structured-block:
18336 statement
18337
18338 In practice, we're also interested in adding the statement to an
18339 outer node. So it is convenient if we work around the fact that
18340 cp_parser_statement calls add_stmt. */
18341
18342static unsigned
18343cp_parser_begin_omp_structured_block (cp_parser *parser)
18344{
18345 unsigned save = parser->in_statement;
18346
18347 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18348 This preserves the "not within loop or switch" style error messages
18349 for nonsense cases like
18350 void foo() {
18351 #pragma omp single
18352 break;
18353 }
18354 */
18355 if (parser->in_statement)
18356 parser->in_statement = IN_OMP_BLOCK;
18357
18358 return save;
18359}
18360
18361static void
18362cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18363{
18364 parser->in_statement = save;
18365}
18366
18367static tree
18368cp_parser_omp_structured_block (cp_parser *parser)
18369{
18370 tree stmt = begin_omp_structured_block ();
18371 unsigned int save = cp_parser_begin_omp_structured_block (parser);
18372
18373 cp_parser_statement (parser, NULL_TREE, false);
18374
18375 cp_parser_end_omp_structured_block (parser, save);
18376 return finish_omp_structured_block (stmt);
18377}
18378
18379/* OpenMP 2.5:
18380 # pragma omp atomic new-line
18381 expression-stmt
18382
18383 expression-stmt:
18384 x binop= expr | x++ | ++x | x-- | --x
18385 binop:
18386 +, *, -, /, &, ^, |, <<, >>
18387
18388 where x is an lvalue expression with scalar type. */
18389
18390static void
18391cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18392{
18393 tree lhs, rhs;
18394 enum tree_code code;
18395
18396 cp_parser_require_pragma_eol (parser, pragma_tok);
18397
18398 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18399 /*cast_p=*/false);
18400 switch (TREE_CODE (lhs))
18401 {
18402 case ERROR_MARK:
18403 goto saw_error;
18404
18405 case PREINCREMENT_EXPR:
18406 case POSTINCREMENT_EXPR:
18407 lhs = TREE_OPERAND (lhs, 0);
18408 code = PLUS_EXPR;
18409 rhs = integer_one_node;
18410 break;
18411
18412 case PREDECREMENT_EXPR:
18413 case POSTDECREMENT_EXPR:
18414 lhs = TREE_OPERAND (lhs, 0);
18415 code = MINUS_EXPR;
18416 rhs = integer_one_node;
18417 break;
18418
18419 default:
18420 switch (cp_lexer_peek_token (parser->lexer)->type)
18421 {
18422 case CPP_MULT_EQ:
18423 code = MULT_EXPR;
18424 break;
18425 case CPP_DIV_EQ:
18426 code = TRUNC_DIV_EXPR;
18427 break;
18428 case CPP_PLUS_EQ:
18429 code = PLUS_EXPR;
18430 break;
18431 case CPP_MINUS_EQ:
18432 code = MINUS_EXPR;
18433 break;
18434 case CPP_LSHIFT_EQ:
18435 code = LSHIFT_EXPR;
18436 break;
18437 case CPP_RSHIFT_EQ:
18438 code = RSHIFT_EXPR;
18439 break;
18440 case CPP_AND_EQ:
18441 code = BIT_AND_EXPR;
18442 break;
18443 case CPP_OR_EQ:
18444 code = BIT_IOR_EXPR;
18445 break;
18446 case CPP_XOR_EQ:
18447 code = BIT_XOR_EXPR;
18448 break;
18449 default:
18450 cp_parser_error (parser,
18451 "invalid operator for %<#pragma omp atomic%>");
18452 goto saw_error;
18453 }
18454 cp_lexer_consume_token (parser->lexer);
18455
18456 rhs = cp_parser_expression (parser, false);
18457 if (rhs == error_mark_node)
18458 goto saw_error;
18459 break;
18460 }
18461 finish_omp_atomic (code, lhs, rhs);
18462 cp_parser_consume_semicolon_at_end_of_statement (parser);
18463 return;
18464
18465 saw_error:
18466 cp_parser_skip_to_end_of_block_or_statement (parser);
18467}
18468
18469
18470/* OpenMP 2.5:
3db45ab5 18471 # pragma omp barrier new-line */
1799e5d5
RH
18472
18473static void
18474cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18475{
18476 cp_parser_require_pragma_eol (parser, pragma_tok);
18477 finish_omp_barrier ();
18478}
18479
18480/* OpenMP 2.5:
18481 # pragma omp critical [(name)] new-line
3db45ab5 18482 structured-block */
1799e5d5
RH
18483
18484static tree
18485cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18486{
18487 tree stmt, name = NULL;
18488
18489 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18490 {
18491 cp_lexer_consume_token (parser->lexer);
18492
18493 name = cp_parser_identifier (parser);
3db45ab5 18494
1799e5d5
RH
18495 if (name == error_mark_node
18496 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18497 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18498 /*or_comma=*/false,
18499 /*consume_paren=*/true);
18500 if (name == error_mark_node)
18501 name = NULL;
18502 }
18503 cp_parser_require_pragma_eol (parser, pragma_tok);
18504
18505 stmt = cp_parser_omp_structured_block (parser);
18506 return c_finish_omp_critical (stmt, name);
18507}
18508
18509/* OpenMP 2.5:
18510 # pragma omp flush flush-vars[opt] new-line
18511
18512 flush-vars:
18513 ( variable-list ) */
18514
18515static void
18516cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18517{
18518 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18519 (void) cp_parser_omp_var_list (parser, 0, NULL);
18520 cp_parser_require_pragma_eol (parser, pragma_tok);
18521
18522 finish_omp_flush ();
18523}
18524
18525/* Parse the restricted form of the for statment allowed by OpenMP. */
18526
18527static tree
18528cp_parser_omp_for_loop (cp_parser *parser)
18529{
18530 tree init, cond, incr, body, decl, pre_body;
18531 location_t loc;
18532
18533 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18534 {
18535 cp_parser_error (parser, "for statement expected");
18536 return NULL;
18537 }
18538 loc = cp_lexer_consume_token (parser->lexer)->location;
18539 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18540 return NULL;
18541
18542 init = decl = NULL;
18543 pre_body = push_stmt_list ();
18544 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18545 {
18546 cp_decl_specifier_seq type_specifiers;
18547
18548 /* First, try to parse as an initialized declaration. See
18549 cp_parser_condition, from whence the bulk of this is copied. */
18550
18551 cp_parser_parse_tentatively (parser);
18552 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18553 &type_specifiers);
18554 if (!cp_parser_error_occurred (parser))
18555 {
18556 tree asm_specification, attributes;
18557 cp_declarator *declarator;
18558
18559 declarator = cp_parser_declarator (parser,
18560 CP_PARSER_DECLARATOR_NAMED,
18561 /*ctor_dtor_or_conv_p=*/NULL,
18562 /*parenthesized_p=*/NULL,
18563 /*member_p=*/false);
18564 attributes = cp_parser_attributes_opt (parser);
18565 asm_specification = cp_parser_asm_specification_opt (parser);
18566
18567 cp_parser_require (parser, CPP_EQ, "`='");
18568 if (cp_parser_parse_definitely (parser))
18569 {
18570 tree pushed_scope;
18571
18572 decl = start_decl (declarator, &type_specifiers,
18573 /*initialized_p=*/false, attributes,
18574 /*prefix_attributes=*/NULL_TREE,
18575 &pushed_scope);
18576
18577 init = cp_parser_assignment_expression (parser, false);
18578
18579 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18580 asm_specification, LOOKUP_ONLYCONVERTING);
18581
18582 if (pushed_scope)
18583 pop_scope (pushed_scope);
18584 }
18585 }
76c5e6e0
JJ
18586 else
18587 cp_parser_abort_tentative_parse (parser);
1799e5d5
RH
18588
18589 /* If parsing as an initialized declaration failed, try again as
18590 a simple expression. */
18591 if (decl == NULL)
76c5e6e0 18592 init = cp_parser_expression (parser, false);
1799e5d5
RH
18593 }
18594 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18595 pre_body = pop_stmt_list (pre_body);
18596
18597 cond = NULL;
18598 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18599 cond = cp_parser_condition (parser);
18600 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18601
18602 incr = NULL;
18603 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18604 incr = cp_parser_expression (parser, false);
18605
18606 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18607 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18608 /*or_comma=*/false,
18609 /*consume_paren=*/true);
18610
18611 /* Note that we saved the original contents of this flag when we entered
18612 the structured block, and so we don't need to re-save it here. */
18613 parser->in_statement = IN_OMP_FOR;
18614
18615 /* Note that the grammar doesn't call for a structured block here,
18616 though the loop as a whole is a structured block. */
18617 body = push_stmt_list ();
18618 cp_parser_statement (parser, NULL_TREE, false);
18619 body = pop_stmt_list (body);
18620
18621 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18622}
18623
18624/* OpenMP 2.5:
18625 #pragma omp for for-clause[optseq] new-line
3db45ab5 18626 for-loop */
1799e5d5
RH
18627
18628#define OMP_FOR_CLAUSE_MASK \
18629 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18630 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18631 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18632 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18633 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18634 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18635 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18636
18637static tree
18638cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18639{
18640 tree clauses, sb, ret;
18641 unsigned int save;
18642
18643 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18644 "#pragma omp for", pragma_tok);
18645
18646 sb = begin_omp_structured_block ();
18647 save = cp_parser_begin_omp_structured_block (parser);
18648
18649 ret = cp_parser_omp_for_loop (parser);
18650 if (ret)
18651 OMP_FOR_CLAUSES (ret) = clauses;
18652
18653 cp_parser_end_omp_structured_block (parser, save);
18654 add_stmt (finish_omp_structured_block (sb));
18655
18656 return ret;
18657}
18658
18659/* OpenMP 2.5:
18660 # pragma omp master new-line
3db45ab5 18661 structured-block */
1799e5d5
RH
18662
18663static tree
18664cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18665{
18666 cp_parser_require_pragma_eol (parser, pragma_tok);
18667 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18668}
18669
18670/* OpenMP 2.5:
18671 # pragma omp ordered new-line
3db45ab5 18672 structured-block */
1799e5d5
RH
18673
18674static tree
18675cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18676{
18677 cp_parser_require_pragma_eol (parser, pragma_tok);
18678 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18679}
18680
18681/* OpenMP 2.5:
18682
18683 section-scope:
18684 { section-sequence }
18685
18686 section-sequence:
18687 section-directive[opt] structured-block
18688 section-sequence section-directive structured-block */
18689
18690static tree
18691cp_parser_omp_sections_scope (cp_parser *parser)
18692{
18693 tree stmt, substmt;
18694 bool error_suppress = false;
18695 cp_token *tok;
18696
18697 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18698 return NULL_TREE;
18699
18700 stmt = push_stmt_list ();
18701
18702 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18703 {
18704 unsigned save;
18705
18706 substmt = begin_omp_structured_block ();
18707 save = cp_parser_begin_omp_structured_block (parser);
18708
18709 while (1)
18710 {
3db45ab5 18711 cp_parser_statement (parser, NULL_TREE, false);
1799e5d5
RH
18712
18713 tok = cp_lexer_peek_token (parser->lexer);
18714 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18715 break;
18716 if (tok->type == CPP_CLOSE_BRACE)
18717 break;
18718 if (tok->type == CPP_EOF)
18719 break;
18720 }
18721
18722 cp_parser_end_omp_structured_block (parser, save);
18723 substmt = finish_omp_structured_block (substmt);
18724 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18725 add_stmt (substmt);
18726 }
18727
18728 while (1)
18729 {
18730 tok = cp_lexer_peek_token (parser->lexer);
18731 if (tok->type == CPP_CLOSE_BRACE)
18732 break;
18733 if (tok->type == CPP_EOF)
18734 break;
18735
18736 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18737 {
18738 cp_lexer_consume_token (parser->lexer);
18739 cp_parser_require_pragma_eol (parser, tok);
18740 error_suppress = false;
18741 }
18742 else if (!error_suppress)
18743 {
18744 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
18745 error_suppress = true;
18746 }
18747
18748 substmt = cp_parser_omp_structured_block (parser);
18749 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18750 add_stmt (substmt);
18751 }
18752 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
18753
18754 substmt = pop_stmt_list (stmt);
18755
18756 stmt = make_node (OMP_SECTIONS);
18757 TREE_TYPE (stmt) = void_type_node;
18758 OMP_SECTIONS_BODY (stmt) = substmt;
18759
18760 add_stmt (stmt);
18761 return stmt;
18762}
18763
18764/* OpenMP 2.5:
18765 # pragma omp sections sections-clause[optseq] newline
3db45ab5 18766 sections-scope */
1799e5d5
RH
18767
18768#define OMP_SECTIONS_CLAUSE_MASK \
18769 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18770 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18771 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18772 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18773 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18774
18775static tree
18776cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
18777{
18778 tree clauses, ret;
18779
18780 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
18781 "#pragma omp sections", pragma_tok);
18782
18783 ret = cp_parser_omp_sections_scope (parser);
18784 if (ret)
18785 OMP_SECTIONS_CLAUSES (ret) = clauses;
18786
18787 return ret;
18788}
18789
18790/* OpenMP 2.5:
18791 # pragma parallel parallel-clause new-line
18792 # pragma parallel for parallel-for-clause new-line
3db45ab5 18793 # pragma parallel sections parallel-sections-clause new-line */
1799e5d5
RH
18794
18795#define OMP_PARALLEL_CLAUSE_MASK \
18796 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
18797 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18798 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18799 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
18800 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
18801 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
18802 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18803 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18804
18805static tree
18806cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
18807{
18808 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
18809 const char *p_name = "#pragma omp parallel";
18810 tree stmt, clauses, par_clause, ws_clause, block;
18811 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
18812 unsigned int save;
18813
18814 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18815 {
18816 cp_lexer_consume_token (parser->lexer);
18817 p_kind = PRAGMA_OMP_PARALLEL_FOR;
18818 p_name = "#pragma omp parallel for";
18819 mask |= OMP_FOR_CLAUSE_MASK;
18820 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18821 }
18822 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18823 {
18824 tree id = cp_lexer_peek_token (parser->lexer)->value;
18825 const char *p = IDENTIFIER_POINTER (id);
18826 if (strcmp (p, "sections") == 0)
18827 {
18828 cp_lexer_consume_token (parser->lexer);
18829 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
18830 p_name = "#pragma omp parallel sections";
18831 mask |= OMP_SECTIONS_CLAUSE_MASK;
18832 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18833 }
18834 }
18835
18836 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
18837 block = begin_omp_parallel ();
18838 save = cp_parser_begin_omp_structured_block (parser);
18839
18840 switch (p_kind)
18841 {
18842 case PRAGMA_OMP_PARALLEL:
18843 cp_parser_already_scoped_statement (parser);
18844 par_clause = clauses;
18845 break;
18846
18847 case PRAGMA_OMP_PARALLEL_FOR:
18848 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18849 stmt = cp_parser_omp_for_loop (parser);
18850 if (stmt)
18851 OMP_FOR_CLAUSES (stmt) = ws_clause;
18852 break;
18853
18854 case PRAGMA_OMP_PARALLEL_SECTIONS:
18855 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18856 stmt = cp_parser_omp_sections_scope (parser);
18857 if (stmt)
18858 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
18859 break;
18860
18861 default:
18862 gcc_unreachable ();
18863 }
18864
18865 cp_parser_end_omp_structured_block (parser, save);
761041be
JJ
18866 stmt = finish_omp_parallel (par_clause, block);
18867 if (p_kind != PRAGMA_OMP_PARALLEL)
18868 OMP_PARALLEL_COMBINED (stmt) = 1;
18869 return stmt;
1799e5d5
RH
18870}
18871
18872/* OpenMP 2.5:
18873 # pragma omp single single-clause[optseq] new-line
3db45ab5 18874 structured-block */
1799e5d5
RH
18875
18876#define OMP_SINGLE_CLAUSE_MASK \
18877 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18878 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18879 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
18880 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18881
18882static tree
18883cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
18884{
18885 tree stmt = make_node (OMP_SINGLE);
18886 TREE_TYPE (stmt) = void_type_node;
18887
18888 OMP_SINGLE_CLAUSES (stmt)
18889 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
18890 "#pragma omp single", pragma_tok);
18891 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
18892
18893 return add_stmt (stmt);
18894}
18895
18896/* OpenMP 2.5:
18897 # pragma omp threadprivate (variable-list) */
18898
18899static void
18900cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
18901{
18902 tree vars;
18903
18904 vars = cp_parser_omp_var_list (parser, 0, NULL);
18905 cp_parser_require_pragma_eol (parser, pragma_tok);
18906
18907 if (!targetm.have_tls)
18908 sorry ("threadprivate variables not supported in this target");
18909
18910 finish_omp_threadprivate (vars);
18911}
18912
18913/* Main entry point to OpenMP statement pragmas. */
18914
18915static void
18916cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
18917{
18918 tree stmt;
18919
18920 switch (pragma_tok->pragma_kind)
18921 {
18922 case PRAGMA_OMP_ATOMIC:
18923 cp_parser_omp_atomic (parser, pragma_tok);
18924 return;
18925 case PRAGMA_OMP_CRITICAL:
18926 stmt = cp_parser_omp_critical (parser, pragma_tok);
18927 break;
18928 case PRAGMA_OMP_FOR:
18929 stmt = cp_parser_omp_for (parser, pragma_tok);
18930 break;
18931 case PRAGMA_OMP_MASTER:
18932 stmt = cp_parser_omp_master (parser, pragma_tok);
18933 break;
18934 case PRAGMA_OMP_ORDERED:
18935 stmt = cp_parser_omp_ordered (parser, pragma_tok);
18936 break;
18937 case PRAGMA_OMP_PARALLEL:
18938 stmt = cp_parser_omp_parallel (parser, pragma_tok);
18939 break;
18940 case PRAGMA_OMP_SECTIONS:
18941 stmt = cp_parser_omp_sections (parser, pragma_tok);
18942 break;
18943 case PRAGMA_OMP_SINGLE:
18944 stmt = cp_parser_omp_single (parser, pragma_tok);
18945 break;
18946 default:
18947 gcc_unreachable ();
18948 }
18949
18950 if (stmt)
18951 SET_EXPR_LOCATION (stmt, pragma_tok->location);
18952}
18953\f
18954/* The parser. */
18955
18956static GTY (()) cp_parser *the_parser;
18957
18958\f
18959/* Special handling for the first token or line in the file. The first
18960 thing in the file might be #pragma GCC pch_preprocess, which loads a
18961 PCH file, which is a GC collection point. So we need to handle this
18962 first pragma without benefit of an existing lexer structure.
18963
3db45ab5 18964 Always returns one token to the caller in *FIRST_TOKEN. This is
1799e5d5
RH
18965 either the true first token of the file, or the first token after
18966 the initial pragma. */
18967
18968static void
18969cp_parser_initial_pragma (cp_token *first_token)
18970{
18971 tree name = NULL;
18972
18973 cp_lexer_get_preprocessor_token (NULL, first_token);
18974 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
18975 return;
18976
18977 cp_lexer_get_preprocessor_token (NULL, first_token);
18978 if (first_token->type == CPP_STRING)
18979 {
18980 name = first_token->value;
18981
18982 cp_lexer_get_preprocessor_token (NULL, first_token);
18983 if (first_token->type != CPP_PRAGMA_EOL)
18984 error ("junk at end of %<#pragma GCC pch_preprocess%>");
18985 }
18986 else
18987 error ("expected string literal");
18988
18989 /* Skip to the end of the pragma. */
18990 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
18991 cp_lexer_get_preprocessor_token (NULL, first_token);
18992
1799e5d5
RH
18993 /* Now actually load the PCH file. */
18994 if (name)
18995 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
f3d5aeeb
AO
18996
18997 /* Read one more token to return to our caller. We have to do this
18998 after reading the PCH file in, since its pointers have to be
18999 live. */
19000 cp_lexer_get_preprocessor_token (NULL, first_token);
1799e5d5
RH
19001}
19002
19003/* Normal parsing of a pragma token. Here we can (and must) use the
19004 regular lexer. */
19005
19006static bool
19007cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19008{
19009 cp_token *pragma_tok;
19010 unsigned int id;
19011
19012 pragma_tok = cp_lexer_consume_token (parser->lexer);
19013 gcc_assert (pragma_tok->type == CPP_PRAGMA);
19014 parser->lexer->in_pragma = true;
19015
19016 id = pragma_tok->pragma_kind;
19017 switch (id)
19018 {
19019 case PRAGMA_GCC_PCH_PREPROCESS:
19020 error ("%<#pragma GCC pch_preprocess%> must be first");
19021 break;
19022
19023 case PRAGMA_OMP_BARRIER:
19024 switch (context)
19025 {
19026 case pragma_compound:
19027 cp_parser_omp_barrier (parser, pragma_tok);
19028 return false;
19029 case pragma_stmt:
19030 error ("%<#pragma omp barrier%> may only be "
19031 "used in compound statements");
19032 break;
19033 default:
19034 goto bad_stmt;
19035 }
19036 break;
19037
19038 case PRAGMA_OMP_FLUSH:
19039 switch (context)
19040 {
19041 case pragma_compound:
19042 cp_parser_omp_flush (parser, pragma_tok);
19043 return false;
19044 case pragma_stmt:
19045 error ("%<#pragma omp flush%> may only be "
19046 "used in compound statements");
19047 break;
19048 default:
19049 goto bad_stmt;
19050 }
19051 break;
19052
19053 case PRAGMA_OMP_THREADPRIVATE:
19054 cp_parser_omp_threadprivate (parser, pragma_tok);
19055 return false;
19056
19057 case PRAGMA_OMP_ATOMIC:
19058 case PRAGMA_OMP_CRITICAL:
19059 case PRAGMA_OMP_FOR:
19060 case PRAGMA_OMP_MASTER:
19061 case PRAGMA_OMP_ORDERED:
19062 case PRAGMA_OMP_PARALLEL:
19063 case PRAGMA_OMP_SECTIONS:
19064 case PRAGMA_OMP_SINGLE:
19065 if (context == pragma_external)
19066 goto bad_stmt;
19067 cp_parser_omp_construct (parser, pragma_tok);
19068 return true;
19069
19070 case PRAGMA_OMP_SECTION:
19071 error ("%<#pragma omp section%> may only be used in "
19072 "%<#pragma omp sections%> construct");
19073 break;
19074
19075 default:
19076 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19077 c_invoke_pragma_handler (id);
19078 break;
19079
19080 bad_stmt:
19081 cp_parser_error (parser, "expected declaration specifiers");
bc4071dd
RH
19082 break;
19083 }
19084
19085 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19086 return false;
19087}
19088
19089/* The interface the pragma parsers have to the lexer. */
19090
19091enum cpp_ttype
19092pragma_lex (tree *value)
19093{
19094 cp_token *tok;
19095 enum cpp_ttype ret;
19096
19097 tok = cp_lexer_peek_token (the_parser->lexer);
19098
19099 ret = tok->type;
19100 *value = tok->value;
19101
19102 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19103 ret = CPP_EOF;
19104 else if (ret == CPP_STRING)
19105 *value = cp_parser_string_literal (the_parser, false, false);
19106 else
19107 {
19108 cp_lexer_consume_token (the_parser->lexer);
19109 if (ret == CPP_KEYWORD)
19110 ret = CPP_NAME;
19111 }
19112
19113 return ret;
19114}
19115
19116\f
a723baf1
MM
19117/* External interface. */
19118
d1bd0ded 19119/* Parse one entire translation unit. */
a723baf1 19120
d1bd0ded
GK
19121void
19122c_parse_file (void)
a723baf1
MM
19123{
19124 bool error_occurred;
f75fbaf7
ZW
19125 static bool already_called = false;
19126
19127 if (already_called)
19128 {
19129 sorry ("inter-module optimizations not implemented for C++");
19130 return;
19131 }
19132 already_called = true;
a723baf1
MM
19133
19134 the_parser = cp_parser_new ();
78757caa
KL
19135 push_deferring_access_checks (flag_access_control
19136 ? dk_no_deferred : dk_no_check);
a723baf1
MM
19137 error_occurred = cp_parser_translation_unit (the_parser);
19138 the_parser = NULL;
a723baf1
MM
19139}
19140
a723baf1
MM
19141/* This variable must be provided by every front end. */
19142
19143int yydebug;
19144
19145#include "gt-cp-parser.h"