tree.c (cp_free_lang_data): Free DECL_NAMESPACE_USERS and clear DECL_CHAIN of NAMESPA...
[gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful, but
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
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "cpplib.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "intl.h"
30 #include "c-family/c-pragma.h"
31 #include "decl.h"
32 #include "flags.h"
33 #include "diagnostic-core.h"
34 #include "toplev.h"
35 #include "output.h"
36 #include "target.h"
37 #include "cgraph.h"
38 #include "c-family/c-common.h"
39 #include "plugin.h"
40
41 \f
42 /* The lexer. */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
46
47 /* A token's value and its associated deferred access checks and
48 qualifying scope. */
49
50 struct GTY(()) tree_check {
51 /* The value associated with the token. */
52 tree value;
53 /* The checks that have been associated with value. */
54 VEC (deferred_access_check, gc)* checks;
55 /* The token's qualifying scope (used when it is a
56 CPP_NESTED_NAME_SPECIFIER). */
57 tree qualifying_scope;
58 };
59
60 /* A C++ token. */
61
62 typedef struct GTY (()) cp_token {
63 /* The kind of token. */
64 ENUM_BITFIELD (cpp_ttype) type : 8;
65 /* If this token is a keyword, this value indicates which keyword.
66 Otherwise, this value is RID_MAX. */
67 ENUM_BITFIELD (rid) keyword : 8;
68 /* Token flags. */
69 unsigned char flags;
70 /* Identifier for the pragma. */
71 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
72 /* True if this token is from a context where it is implicitly extern "C" */
73 BOOL_BITFIELD implicit_extern_c : 1;
74 /* True for a CPP_NAME token that is not a keyword (i.e., for which
75 KEYWORD is RID_MAX) iff this name was looked up and found to be
76 ambiguous. An error has already been reported. */
77 BOOL_BITFIELD ambiguous_p : 1;
78 /* The location at which this token was found. */
79 location_t location;
80 /* The value associated with this token, if any. */
81 union cp_token_value {
82 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
83 struct tree_check* GTY((tag ("1"))) tree_check_value;
84 /* Use for all other tokens. */
85 tree GTY((tag ("0"))) value;
86 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87 } cp_token;
88
89 /* We use a stack of token pointer for saving token sets. */
90 typedef struct cp_token *cp_token_position;
91 DEF_VEC_P (cp_token_position);
92 DEF_VEC_ALLOC_P (cp_token_position,heap);
93
94 static cp_token eof_token =
95 {
96 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
97 };
98
99 /* The cp_lexer structure represents the C++ lexer. It is responsible
100 for managing the token stream from the preprocessor and supplying
101 it to the parser. Tokens are never added to the cp_lexer after
102 it is created. */
103
104 typedef struct GTY (()) cp_lexer {
105 /* The memory allocated for the buffer. NULL if this lexer does not
106 own the token buffer. */
107 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
108 /* If the lexer owns the buffer, this is the number of tokens in the
109 buffer. */
110 size_t buffer_length;
111
112 /* A pointer just past the last available token. The tokens
113 in this lexer are [buffer, last_token). */
114 cp_token_position GTY ((skip)) last_token;
115
116 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
117 no more available tokens. */
118 cp_token_position GTY ((skip)) next_token;
119
120 /* A stack indicating positions at which cp_lexer_save_tokens was
121 called. The top entry is the most recent position at which we
122 began saving tokens. If the stack is non-empty, we are saving
123 tokens. */
124 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
125
126 /* The next lexer in a linked list of lexers. */
127 struct cp_lexer *next;
128
129 /* True if we should output debugging information. */
130 bool debugging_p;
131
132 /* True if we're in the context of parsing a pragma, and should not
133 increment past the end-of-line marker. */
134 bool in_pragma;
135 } cp_lexer;
136
137 /* cp_token_cache is a range of tokens. There is no need to represent
138 allocate heap memory for it, since tokens are never removed from the
139 lexer's array. There is also no need for the GC to walk through
140 a cp_token_cache, since everything in here is referenced through
141 a lexer. */
142
143 typedef struct GTY(()) cp_token_cache {
144 /* The beginning of the token range. */
145 cp_token * GTY((skip)) first;
146
147 /* Points immediately after the last token in the range. */
148 cp_token * GTY ((skip)) last;
149 } cp_token_cache;
150
151 /* The various kinds of non integral constant we encounter. */
152 typedef enum non_integral_constant {
153 NIC_NONE,
154 /* floating-point literal */
155 NIC_FLOAT,
156 /* %<this%> */
157 NIC_THIS,
158 /* %<__FUNCTION__%> */
159 NIC_FUNC_NAME,
160 /* %<__PRETTY_FUNCTION__%> */
161 NIC_PRETTY_FUNC,
162 /* %<__func__%> */
163 NIC_C99_FUNC,
164 /* "%<va_arg%> */
165 NIC_VA_ARG,
166 /* a cast */
167 NIC_CAST,
168 /* %<typeid%> operator */
169 NIC_TYPEID,
170 /* non-constant compound literals */
171 NIC_NCC,
172 /* a function call */
173 NIC_FUNC_CALL,
174 /* an increment */
175 NIC_INC,
176 /* an decrement */
177 NIC_DEC,
178 /* an array reference */
179 NIC_ARRAY_REF,
180 /* %<->%> */
181 NIC_ARROW,
182 /* %<.%> */
183 NIC_POINT,
184 /* the address of a label */
185 NIC_ADDR_LABEL,
186 /* %<*%> */
187 NIC_STAR,
188 /* %<&%> */
189 NIC_ADDR,
190 /* %<++%> */
191 NIC_PREINCREMENT,
192 /* %<--%> */
193 NIC_PREDECREMENT,
194 /* %<new%> */
195 NIC_NEW,
196 /* %<delete%> */
197 NIC_DEL,
198 /* calls to overloaded operators */
199 NIC_OVERLOADED,
200 /* an assignment */
201 NIC_ASSIGNMENT,
202 /* a comma operator */
203 NIC_COMMA,
204 /* a call to a constructor */
205 NIC_CONSTRUCTOR
206 } non_integral_constant;
207
208 /* The various kinds of errors about name-lookup failing. */
209 typedef enum name_lookup_error {
210 /* NULL */
211 NLE_NULL,
212 /* is not a type */
213 NLE_TYPE,
214 /* is not a class or namespace */
215 NLE_CXX98,
216 /* is not a class, namespace, or enumeration */
217 NLE_NOT_CXX98
218 } name_lookup_error;
219
220 /* The various kinds of required token */
221 typedef enum required_token {
222 RT_NONE,
223 RT_SEMICOLON, /* ';' */
224 RT_OPEN_PAREN, /* '(' */
225 RT_CLOSE_BRACE, /* '}' */
226 RT_OPEN_BRACE, /* '{' */
227 RT_CLOSE_SQUARE, /* ']' */
228 RT_OPEN_SQUARE, /* '[' */
229 RT_COMMA, /* ',' */
230 RT_SCOPE, /* '::' */
231 RT_LESS, /* '<' */
232 RT_GREATER, /* '>' */
233 RT_EQ, /* '=' */
234 RT_ELLIPSIS, /* '...' */
235 RT_MULT, /* '*' */
236 RT_COMPL, /* '~' */
237 RT_COLON, /* ':' */
238 RT_COLON_SCOPE, /* ':' or '::' */
239 RT_CLOSE_PAREN, /* ')' */
240 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
241 RT_PRAGMA_EOL, /* end of line */
242 RT_NAME, /* identifier */
243
244 /* The type is CPP_KEYWORD */
245 RT_NEW, /* new */
246 RT_DELETE, /* delete */
247 RT_RETURN, /* return */
248 RT_WHILE, /* while */
249 RT_EXTERN, /* extern */
250 RT_STATIC_ASSERT, /* static_assert */
251 RT_DECLTYPE, /* decltype */
252 RT_OPERATOR, /* operator */
253 RT_CLASS, /* class */
254 RT_TEMPLATE, /* template */
255 RT_NAMESPACE, /* namespace */
256 RT_USING, /* using */
257 RT_ASM, /* asm */
258 RT_TRY, /* try */
259 RT_CATCH, /* catch */
260 RT_THROW, /* throw */
261 RT_LABEL, /* __label__ */
262 RT_AT_TRY, /* @try */
263 RT_AT_SYNCHRONIZED, /* @synchronized */
264 RT_AT_THROW, /* @throw */
265
266 RT_SELECT, /* selection-statement */
267 RT_INTERATION, /* iteration-statement */
268 RT_JUMP, /* jump-statement */
269 RT_CLASS_KEY, /* class-key */
270 RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
271 } required_token;
272
273 /* Prototypes. */
274
275 static cp_lexer *cp_lexer_new_main
276 (void);
277 static cp_lexer *cp_lexer_new_from_tokens
278 (cp_token_cache *tokens);
279 static void cp_lexer_destroy
280 (cp_lexer *);
281 static int cp_lexer_saving_tokens
282 (const cp_lexer *);
283 static cp_token_position cp_lexer_token_position
284 (cp_lexer *, bool);
285 static cp_token *cp_lexer_token_at
286 (cp_lexer *, cp_token_position);
287 static void cp_lexer_get_preprocessor_token
288 (cp_lexer *, cp_token *);
289 static inline cp_token *cp_lexer_peek_token
290 (cp_lexer *);
291 static cp_token *cp_lexer_peek_nth_token
292 (cp_lexer *, size_t);
293 static inline bool cp_lexer_next_token_is
294 (cp_lexer *, enum cpp_ttype);
295 static bool cp_lexer_next_token_is_not
296 (cp_lexer *, enum cpp_ttype);
297 static bool cp_lexer_next_token_is_keyword
298 (cp_lexer *, enum rid);
299 static cp_token *cp_lexer_consume_token
300 (cp_lexer *);
301 static void cp_lexer_purge_token
302 (cp_lexer *);
303 static void cp_lexer_purge_tokens_after
304 (cp_lexer *, cp_token_position);
305 static void cp_lexer_save_tokens
306 (cp_lexer *);
307 static void cp_lexer_commit_tokens
308 (cp_lexer *);
309 static void cp_lexer_rollback_tokens
310 (cp_lexer *);
311 #ifdef ENABLE_CHECKING
312 static void cp_lexer_print_token
313 (FILE *, cp_token *);
314 static inline bool cp_lexer_debugging_p
315 (cp_lexer *);
316 static void cp_lexer_start_debugging
317 (cp_lexer *) ATTRIBUTE_UNUSED;
318 static void cp_lexer_stop_debugging
319 (cp_lexer *) ATTRIBUTE_UNUSED;
320 #else
321 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
322 about passing NULL to functions that require non-NULL arguments
323 (fputs, fprintf). It will never be used, so all we need is a value
324 of the right type that's guaranteed not to be NULL. */
325 #define cp_lexer_debug_stream stdout
326 #define cp_lexer_print_token(str, tok) (void) 0
327 #define cp_lexer_debugging_p(lexer) 0
328 #endif /* ENABLE_CHECKING */
329
330 static cp_token_cache *cp_token_cache_new
331 (cp_token *, cp_token *);
332
333 static void cp_parser_initial_pragma
334 (cp_token *);
335
336 /* Manifest constants. */
337 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
338 #define CP_SAVED_TOKEN_STACK 5
339
340 /* A token type for keywords, as opposed to ordinary identifiers. */
341 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
342
343 /* A token type for template-ids. If a template-id is processed while
344 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
345 the value of the CPP_TEMPLATE_ID is whatever was returned by
346 cp_parser_template_id. */
347 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
348
349 /* A token type for nested-name-specifiers. If a
350 nested-name-specifier is processed while parsing tentatively, it is
351 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
352 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
353 cp_parser_nested_name_specifier_opt. */
354 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
355
356 /* A token type for tokens that are not tokens at all; these are used
357 to represent slots in the array where there used to be a token
358 that has now been deleted. */
359 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
360
361 /* The number of token types, including C++-specific ones. */
362 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
363
364 /* Variables. */
365
366 #ifdef ENABLE_CHECKING
367 /* The stream to which debugging output should be written. */
368 static FILE *cp_lexer_debug_stream;
369 #endif /* ENABLE_CHECKING */
370
371 /* Nonzero if we are parsing an unevaluated operand: an operand to
372 sizeof, typeof, or alignof. */
373 int cp_unevaluated_operand;
374
375 /* Create a new main C++ lexer, the lexer that gets tokens from the
376 preprocessor. */
377
378 static cp_lexer *
379 cp_lexer_new_main (void)
380 {
381 cp_token first_token;
382 cp_lexer *lexer;
383 cp_token *pos;
384 size_t alloc;
385 size_t space;
386 cp_token *buffer;
387
388 /* It's possible that parsing the first pragma will load a PCH file,
389 which is a GC collection point. So we have to do that before
390 allocating any memory. */
391 cp_parser_initial_pragma (&first_token);
392
393 c_common_no_more_pch ();
394
395 /* Allocate the memory. */
396 lexer = ggc_alloc_cleared_cp_lexer ();
397
398 #ifdef ENABLE_CHECKING
399 /* Initially we are not debugging. */
400 lexer->debugging_p = false;
401 #endif /* ENABLE_CHECKING */
402 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
403 CP_SAVED_TOKEN_STACK);
404
405 /* Create the buffer. */
406 alloc = CP_LEXER_BUFFER_SIZE;
407 buffer = ggc_alloc_vec_cp_token (alloc);
408
409 /* Put the first token in the buffer. */
410 space = alloc;
411 pos = buffer;
412 *pos = first_token;
413
414 /* Get the remaining tokens from the preprocessor. */
415 while (pos->type != CPP_EOF)
416 {
417 pos++;
418 if (!--space)
419 {
420 space = alloc;
421 alloc *= 2;
422 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
423 pos = buffer + space;
424 }
425 cp_lexer_get_preprocessor_token (lexer, pos);
426 }
427 lexer->buffer = buffer;
428 lexer->buffer_length = alloc - space;
429 lexer->last_token = pos;
430 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
431
432 /* Subsequent preprocessor diagnostics should use compiler
433 diagnostic functions to get the compiler source location. */
434 done_lexing = true;
435
436 gcc_assert (lexer->next_token->type != CPP_PURGED);
437 return lexer;
438 }
439
440 /* Create a new lexer whose token stream is primed with the tokens in
441 CACHE. When these tokens are exhausted, no new tokens will be read. */
442
443 static cp_lexer *
444 cp_lexer_new_from_tokens (cp_token_cache *cache)
445 {
446 cp_token *first = cache->first;
447 cp_token *last = cache->last;
448 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
449
450 /* We do not own the buffer. */
451 lexer->buffer = NULL;
452 lexer->buffer_length = 0;
453 lexer->next_token = first == last ? &eof_token : first;
454 lexer->last_token = last;
455
456 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
457 CP_SAVED_TOKEN_STACK);
458
459 #ifdef ENABLE_CHECKING
460 /* Initially we are not debugging. */
461 lexer->debugging_p = false;
462 #endif
463
464 gcc_assert (lexer->next_token->type != CPP_PURGED);
465 return lexer;
466 }
467
468 /* Frees all resources associated with LEXER. */
469
470 static void
471 cp_lexer_destroy (cp_lexer *lexer)
472 {
473 if (lexer->buffer)
474 ggc_free (lexer->buffer);
475 VEC_free (cp_token_position, heap, lexer->saved_tokens);
476 ggc_free (lexer);
477 }
478
479 /* Returns nonzero if debugging information should be output. */
480
481 #ifdef ENABLE_CHECKING
482
483 static inline bool
484 cp_lexer_debugging_p (cp_lexer *lexer)
485 {
486 return lexer->debugging_p;
487 }
488
489 #endif /* ENABLE_CHECKING */
490
491 static inline cp_token_position
492 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
493 {
494 gcc_assert (!previous_p || lexer->next_token != &eof_token);
495
496 return lexer->next_token - previous_p;
497 }
498
499 static inline cp_token *
500 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
501 {
502 return pos;
503 }
504
505 /* nonzero if we are presently saving tokens. */
506
507 static inline int
508 cp_lexer_saving_tokens (const cp_lexer* lexer)
509 {
510 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
511 }
512
513 /* Store the next token from the preprocessor in *TOKEN. Return true
514 if we reach EOF. If LEXER is NULL, assume we are handling an
515 initial #pragma pch_preprocess, and thus want the lexer to return
516 processed strings. */
517
518 static void
519 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
520 {
521 static int is_extern_c = 0;
522
523 /* Get a new token from the preprocessor. */
524 token->type
525 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
526 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
527 token->keyword = RID_MAX;
528 token->pragma_kind = PRAGMA_NONE;
529
530 /* On some systems, some header files are surrounded by an
531 implicit extern "C" block. Set a flag in the token if it
532 comes from such a header. */
533 is_extern_c += pending_lang_change;
534 pending_lang_change = 0;
535 token->implicit_extern_c = is_extern_c > 0;
536
537 /* Check to see if this token is a keyword. */
538 if (token->type == CPP_NAME)
539 {
540 if (C_IS_RESERVED_WORD (token->u.value))
541 {
542 /* Mark this token as a keyword. */
543 token->type = CPP_KEYWORD;
544 /* Record which keyword. */
545 token->keyword = C_RID_CODE (token->u.value);
546 }
547 else
548 {
549 if (warn_cxx0x_compat
550 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
551 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
552 {
553 /* Warn about the C++0x keyword (but still treat it as
554 an identifier). */
555 warning (OPT_Wc__0x_compat,
556 "identifier %qE will become a keyword in C++0x",
557 token->u.value);
558
559 /* Clear out the C_RID_CODE so we don't warn about this
560 particular identifier-turned-keyword again. */
561 C_SET_RID_CODE (token->u.value, RID_MAX);
562 }
563
564 token->ambiguous_p = false;
565 token->keyword = RID_MAX;
566 }
567 }
568 else if (token->type == CPP_AT_NAME)
569 {
570 /* This only happens in Objective-C++; it must be a keyword. */
571 token->type = CPP_KEYWORD;
572 switch (C_RID_CODE (token->u.value))
573 {
574 /* Replace 'class' with '@class', 'private' with '@private',
575 etc. This prevents confusion with the C++ keyword
576 'class', and makes the tokens consistent with other
577 Objective-C 'AT' keywords. For example '@class' is
578 reported as RID_AT_CLASS which is consistent with
579 '@synchronized', which is reported as
580 RID_AT_SYNCHRONIZED.
581 */
582 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
583 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
584 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
585 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
586 case RID_THROW: token->keyword = RID_AT_THROW; break;
587 case RID_TRY: token->keyword = RID_AT_TRY; break;
588 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
589 default: token->keyword = C_RID_CODE (token->u.value);
590 }
591 }
592 else if (token->type == CPP_PRAGMA)
593 {
594 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
595 token->pragma_kind = ((enum pragma_kind)
596 TREE_INT_CST_LOW (token->u.value));
597 token->u.value = NULL_TREE;
598 }
599 }
600
601 /* Update the globals input_location and the input file stack from TOKEN. */
602 static inline void
603 cp_lexer_set_source_position_from_token (cp_token *token)
604 {
605 if (token->type != CPP_EOF)
606 {
607 input_location = token->location;
608 }
609 }
610
611 /* Return a pointer to the next token in the token stream, but do not
612 consume it. */
613
614 static inline cp_token *
615 cp_lexer_peek_token (cp_lexer *lexer)
616 {
617 if (cp_lexer_debugging_p (lexer))
618 {
619 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
620 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
621 putc ('\n', cp_lexer_debug_stream);
622 }
623 return lexer->next_token;
624 }
625
626 /* Return true if the next token has the indicated TYPE. */
627
628 static inline bool
629 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
630 {
631 return cp_lexer_peek_token (lexer)->type == type;
632 }
633
634 /* Return true if the next token does not have the indicated TYPE. */
635
636 static inline bool
637 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
638 {
639 return !cp_lexer_next_token_is (lexer, type);
640 }
641
642 /* Return true if the next token is the indicated KEYWORD. */
643
644 static inline bool
645 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
646 {
647 return cp_lexer_peek_token (lexer)->keyword == keyword;
648 }
649
650 /* Return true if the next token is not the indicated KEYWORD. */
651
652 static inline bool
653 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
654 {
655 return cp_lexer_peek_token (lexer)->keyword != keyword;
656 }
657
658 /* Return true if the next token is a keyword for a decl-specifier. */
659
660 static bool
661 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
662 {
663 cp_token *token;
664
665 token = cp_lexer_peek_token (lexer);
666 switch (token->keyword)
667 {
668 /* auto specifier: storage-class-specifier in C++,
669 simple-type-specifier in C++0x. */
670 case RID_AUTO:
671 /* Storage classes. */
672 case RID_REGISTER:
673 case RID_STATIC:
674 case RID_EXTERN:
675 case RID_MUTABLE:
676 case RID_THREAD:
677 /* Elaborated type specifiers. */
678 case RID_ENUM:
679 case RID_CLASS:
680 case RID_STRUCT:
681 case RID_UNION:
682 case RID_TYPENAME:
683 /* Simple type specifiers. */
684 case RID_CHAR:
685 case RID_CHAR16:
686 case RID_CHAR32:
687 case RID_WCHAR:
688 case RID_BOOL:
689 case RID_SHORT:
690 case RID_INT:
691 case RID_LONG:
692 case RID_INT128:
693 case RID_SIGNED:
694 case RID_UNSIGNED:
695 case RID_FLOAT:
696 case RID_DOUBLE:
697 case RID_VOID:
698 /* GNU extensions. */
699 case RID_ATTRIBUTE:
700 case RID_TYPEOF:
701 /* C++0x extensions. */
702 case RID_DECLTYPE:
703 return true;
704
705 default:
706 return false;
707 }
708 }
709
710 /* Return a pointer to the Nth token in the token stream. If N is 1,
711 then this is precisely equivalent to cp_lexer_peek_token (except
712 that it is not inline). One would like to disallow that case, but
713 there is one case (cp_parser_nth_token_starts_template_id) where
714 the caller passes a variable for N and it might be 1. */
715
716 static cp_token *
717 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
718 {
719 cp_token *token;
720
721 /* N is 1-based, not zero-based. */
722 gcc_assert (n > 0);
723
724 if (cp_lexer_debugging_p (lexer))
725 fprintf (cp_lexer_debug_stream,
726 "cp_lexer: peeking ahead %ld at token: ", (long)n);
727
728 --n;
729 token = lexer->next_token;
730 gcc_assert (!n || token != &eof_token);
731 while (n != 0)
732 {
733 ++token;
734 if (token == lexer->last_token)
735 {
736 token = &eof_token;
737 break;
738 }
739
740 if (token->type != CPP_PURGED)
741 --n;
742 }
743
744 if (cp_lexer_debugging_p (lexer))
745 {
746 cp_lexer_print_token (cp_lexer_debug_stream, token);
747 putc ('\n', cp_lexer_debug_stream);
748 }
749
750 return token;
751 }
752
753 /* Return the next token, and advance the lexer's next_token pointer
754 to point to the next non-purged token. */
755
756 static cp_token *
757 cp_lexer_consume_token (cp_lexer* lexer)
758 {
759 cp_token *token = lexer->next_token;
760
761 gcc_assert (token != &eof_token);
762 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
763
764 do
765 {
766 lexer->next_token++;
767 if (lexer->next_token == lexer->last_token)
768 {
769 lexer->next_token = &eof_token;
770 break;
771 }
772
773 }
774 while (lexer->next_token->type == CPP_PURGED);
775
776 cp_lexer_set_source_position_from_token (token);
777
778 /* Provide debugging output. */
779 if (cp_lexer_debugging_p (lexer))
780 {
781 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
782 cp_lexer_print_token (cp_lexer_debug_stream, token);
783 putc ('\n', cp_lexer_debug_stream);
784 }
785
786 return token;
787 }
788
789 /* Permanently remove the next token from the token stream, and
790 advance the next_token pointer to refer to the next non-purged
791 token. */
792
793 static void
794 cp_lexer_purge_token (cp_lexer *lexer)
795 {
796 cp_token *tok = lexer->next_token;
797
798 gcc_assert (tok != &eof_token);
799 tok->type = CPP_PURGED;
800 tok->location = UNKNOWN_LOCATION;
801 tok->u.value = NULL_TREE;
802 tok->keyword = RID_MAX;
803
804 do
805 {
806 tok++;
807 if (tok == lexer->last_token)
808 {
809 tok = &eof_token;
810 break;
811 }
812 }
813 while (tok->type == CPP_PURGED);
814 lexer->next_token = tok;
815 }
816
817 /* Permanently remove all tokens after TOK, up to, but not
818 including, the token that will be returned next by
819 cp_lexer_peek_token. */
820
821 static void
822 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
823 {
824 cp_token *peek = lexer->next_token;
825
826 if (peek == &eof_token)
827 peek = lexer->last_token;
828
829 gcc_assert (tok < peek);
830
831 for ( tok += 1; tok != peek; tok += 1)
832 {
833 tok->type = CPP_PURGED;
834 tok->location = UNKNOWN_LOCATION;
835 tok->u.value = NULL_TREE;
836 tok->keyword = RID_MAX;
837 }
838 }
839
840 /* Begin saving tokens. All tokens consumed after this point will be
841 preserved. */
842
843 static void
844 cp_lexer_save_tokens (cp_lexer* lexer)
845 {
846 /* Provide debugging output. */
847 if (cp_lexer_debugging_p (lexer))
848 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
849
850 VEC_safe_push (cp_token_position, heap,
851 lexer->saved_tokens, lexer->next_token);
852 }
853
854 /* Commit to the portion of the token stream most recently saved. */
855
856 static void
857 cp_lexer_commit_tokens (cp_lexer* lexer)
858 {
859 /* Provide debugging output. */
860 if (cp_lexer_debugging_p (lexer))
861 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
862
863 VEC_pop (cp_token_position, lexer->saved_tokens);
864 }
865
866 /* Return all tokens saved since the last call to cp_lexer_save_tokens
867 to the token stream. Stop saving tokens. */
868
869 static void
870 cp_lexer_rollback_tokens (cp_lexer* lexer)
871 {
872 /* Provide debugging output. */
873 if (cp_lexer_debugging_p (lexer))
874 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
875
876 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
877 }
878
879 /* Print a representation of the TOKEN on the STREAM. */
880
881 #ifdef ENABLE_CHECKING
882
883 static void
884 cp_lexer_print_token (FILE * stream, cp_token *token)
885 {
886 /* We don't use cpp_type2name here because the parser defines
887 a few tokens of its own. */
888 static const char *const token_names[] = {
889 /* cpplib-defined token types */
890 #define OP(e, s) #e,
891 #define TK(e, s) #e,
892 TTYPE_TABLE
893 #undef OP
894 #undef TK
895 /* C++ parser token types - see "Manifest constants", above. */
896 "KEYWORD",
897 "TEMPLATE_ID",
898 "NESTED_NAME_SPECIFIER",
899 "PURGED"
900 };
901
902 /* If we have a name for the token, print it out. Otherwise, we
903 simply give the numeric code. */
904 gcc_assert (token->type < ARRAY_SIZE(token_names));
905 fputs (token_names[token->type], stream);
906
907 /* For some tokens, print the associated data. */
908 switch (token->type)
909 {
910 case CPP_KEYWORD:
911 /* Some keywords have a value that is not an IDENTIFIER_NODE.
912 For example, `struct' is mapped to an INTEGER_CST. */
913 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
914 break;
915 /* else fall through */
916 case CPP_NAME:
917 fputs (IDENTIFIER_POINTER (token->u.value), stream);
918 break;
919
920 case CPP_STRING:
921 case CPP_STRING16:
922 case CPP_STRING32:
923 case CPP_WSTRING:
924 case CPP_UTF8STRING:
925 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
926 break;
927
928 default:
929 break;
930 }
931 }
932
933 /* Start emitting debugging information. */
934
935 static void
936 cp_lexer_start_debugging (cp_lexer* lexer)
937 {
938 lexer->debugging_p = true;
939 }
940
941 /* Stop emitting debugging information. */
942
943 static void
944 cp_lexer_stop_debugging (cp_lexer* lexer)
945 {
946 lexer->debugging_p = false;
947 }
948
949 #endif /* ENABLE_CHECKING */
950
951 /* Create a new cp_token_cache, representing a range of tokens. */
952
953 static cp_token_cache *
954 cp_token_cache_new (cp_token *first, cp_token *last)
955 {
956 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
957 cache->first = first;
958 cache->last = last;
959 return cache;
960 }
961
962 \f
963 /* Decl-specifiers. */
964
965 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
966
967 static void
968 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
969 {
970 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
971 }
972
973 /* Declarators. */
974
975 /* Nothing other than the parser should be creating declarators;
976 declarators are a semi-syntactic representation of C++ entities.
977 Other parts of the front end that need to create entities (like
978 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
979
980 static cp_declarator *make_call_declarator
981 (cp_declarator *, tree, cp_cv_quals, tree, tree);
982 static cp_declarator *make_array_declarator
983 (cp_declarator *, tree);
984 static cp_declarator *make_pointer_declarator
985 (cp_cv_quals, cp_declarator *);
986 static cp_declarator *make_reference_declarator
987 (cp_cv_quals, cp_declarator *, bool);
988 static cp_parameter_declarator *make_parameter_declarator
989 (cp_decl_specifier_seq *, cp_declarator *, tree);
990 static cp_declarator *make_ptrmem_declarator
991 (cp_cv_quals, tree, cp_declarator *);
992
993 /* An erroneous declarator. */
994 static cp_declarator *cp_error_declarator;
995
996 /* The obstack on which declarators and related data structures are
997 allocated. */
998 static struct obstack declarator_obstack;
999
1000 /* Alloc BYTES from the declarator memory pool. */
1001
1002 static inline void *
1003 alloc_declarator (size_t bytes)
1004 {
1005 return obstack_alloc (&declarator_obstack, bytes);
1006 }
1007
1008 /* Allocate a declarator of the indicated KIND. Clear fields that are
1009 common to all declarators. */
1010
1011 static cp_declarator *
1012 make_declarator (cp_declarator_kind kind)
1013 {
1014 cp_declarator *declarator;
1015
1016 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1017 declarator->kind = kind;
1018 declarator->attributes = NULL_TREE;
1019 declarator->declarator = NULL;
1020 declarator->parameter_pack_p = false;
1021 declarator->id_loc = UNKNOWN_LOCATION;
1022
1023 return declarator;
1024 }
1025
1026 /* Make a declarator for a generalized identifier. If
1027 QUALIFYING_SCOPE is non-NULL, the identifier is
1028 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1029 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1030 is, if any. */
1031
1032 static cp_declarator *
1033 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1034 special_function_kind sfk)
1035 {
1036 cp_declarator *declarator;
1037
1038 /* It is valid to write:
1039
1040 class C { void f(); };
1041 typedef C D;
1042 void D::f();
1043
1044 The standard is not clear about whether `typedef const C D' is
1045 legal; as of 2002-09-15 the committee is considering that
1046 question. EDG 3.0 allows that syntax. Therefore, we do as
1047 well. */
1048 if (qualifying_scope && TYPE_P (qualifying_scope))
1049 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1050
1051 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1052 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1053 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1054
1055 declarator = make_declarator (cdk_id);
1056 declarator->u.id.qualifying_scope = qualifying_scope;
1057 declarator->u.id.unqualified_name = unqualified_name;
1058 declarator->u.id.sfk = sfk;
1059
1060 return declarator;
1061 }
1062
1063 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1064 of modifiers such as const or volatile to apply to the pointer
1065 type, represented as identifiers. */
1066
1067 cp_declarator *
1068 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1069 {
1070 cp_declarator *declarator;
1071
1072 declarator = make_declarator (cdk_pointer);
1073 declarator->declarator = target;
1074 declarator->u.pointer.qualifiers = cv_qualifiers;
1075 declarator->u.pointer.class_type = NULL_TREE;
1076 if (target)
1077 {
1078 declarator->id_loc = target->id_loc;
1079 declarator->parameter_pack_p = target->parameter_pack_p;
1080 target->parameter_pack_p = false;
1081 }
1082 else
1083 declarator->parameter_pack_p = false;
1084
1085 return declarator;
1086 }
1087
1088 /* Like make_pointer_declarator -- but for references. */
1089
1090 cp_declarator *
1091 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1092 bool rvalue_ref)
1093 {
1094 cp_declarator *declarator;
1095
1096 declarator = make_declarator (cdk_reference);
1097 declarator->declarator = target;
1098 declarator->u.reference.qualifiers = cv_qualifiers;
1099 declarator->u.reference.rvalue_ref = rvalue_ref;
1100 if (target)
1101 {
1102 declarator->id_loc = target->id_loc;
1103 declarator->parameter_pack_p = target->parameter_pack_p;
1104 target->parameter_pack_p = false;
1105 }
1106 else
1107 declarator->parameter_pack_p = false;
1108
1109 return declarator;
1110 }
1111
1112 /* Like make_pointer_declarator -- but for a pointer to a non-static
1113 member of CLASS_TYPE. */
1114
1115 cp_declarator *
1116 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1117 cp_declarator *pointee)
1118 {
1119 cp_declarator *declarator;
1120
1121 declarator = make_declarator (cdk_ptrmem);
1122 declarator->declarator = pointee;
1123 declarator->u.pointer.qualifiers = cv_qualifiers;
1124 declarator->u.pointer.class_type = class_type;
1125
1126 if (pointee)
1127 {
1128 declarator->parameter_pack_p = pointee->parameter_pack_p;
1129 pointee->parameter_pack_p = false;
1130 }
1131 else
1132 declarator->parameter_pack_p = false;
1133
1134 return declarator;
1135 }
1136
1137 /* Make a declarator for the function given by TARGET, with the
1138 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1139 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1140 indicates what exceptions can be thrown. */
1141
1142 cp_declarator *
1143 make_call_declarator (cp_declarator *target,
1144 tree parms,
1145 cp_cv_quals cv_qualifiers,
1146 tree exception_specification,
1147 tree late_return_type)
1148 {
1149 cp_declarator *declarator;
1150
1151 declarator = make_declarator (cdk_function);
1152 declarator->declarator = target;
1153 declarator->u.function.parameters = parms;
1154 declarator->u.function.qualifiers = cv_qualifiers;
1155 declarator->u.function.exception_specification = exception_specification;
1156 declarator->u.function.late_return_type = late_return_type;
1157 if (target)
1158 {
1159 declarator->id_loc = target->id_loc;
1160 declarator->parameter_pack_p = target->parameter_pack_p;
1161 target->parameter_pack_p = false;
1162 }
1163 else
1164 declarator->parameter_pack_p = false;
1165
1166 return declarator;
1167 }
1168
1169 /* Make a declarator for an array of BOUNDS elements, each of which is
1170 defined by ELEMENT. */
1171
1172 cp_declarator *
1173 make_array_declarator (cp_declarator *element, tree bounds)
1174 {
1175 cp_declarator *declarator;
1176
1177 declarator = make_declarator (cdk_array);
1178 declarator->declarator = element;
1179 declarator->u.array.bounds = bounds;
1180 if (element)
1181 {
1182 declarator->id_loc = element->id_loc;
1183 declarator->parameter_pack_p = element->parameter_pack_p;
1184 element->parameter_pack_p = false;
1185 }
1186 else
1187 declarator->parameter_pack_p = false;
1188
1189 return declarator;
1190 }
1191
1192 /* Determine whether the declarator we've seen so far can be a
1193 parameter pack, when followed by an ellipsis. */
1194 static bool
1195 declarator_can_be_parameter_pack (cp_declarator *declarator)
1196 {
1197 /* Search for a declarator name, or any other declarator that goes
1198 after the point where the ellipsis could appear in a parameter
1199 pack. If we find any of these, then this declarator can not be
1200 made into a parameter pack. */
1201 bool found = false;
1202 while (declarator && !found)
1203 {
1204 switch ((int)declarator->kind)
1205 {
1206 case cdk_id:
1207 case cdk_array:
1208 found = true;
1209 break;
1210
1211 case cdk_error:
1212 return true;
1213
1214 default:
1215 declarator = declarator->declarator;
1216 break;
1217 }
1218 }
1219
1220 return !found;
1221 }
1222
1223 cp_parameter_declarator *no_parameters;
1224
1225 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1226 DECLARATOR and DEFAULT_ARGUMENT. */
1227
1228 cp_parameter_declarator *
1229 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1230 cp_declarator *declarator,
1231 tree default_argument)
1232 {
1233 cp_parameter_declarator *parameter;
1234
1235 parameter = ((cp_parameter_declarator *)
1236 alloc_declarator (sizeof (cp_parameter_declarator)));
1237 parameter->next = NULL;
1238 if (decl_specifiers)
1239 parameter->decl_specifiers = *decl_specifiers;
1240 else
1241 clear_decl_specs (&parameter->decl_specifiers);
1242 parameter->declarator = declarator;
1243 parameter->default_argument = default_argument;
1244 parameter->ellipsis_p = false;
1245
1246 return parameter;
1247 }
1248
1249 /* Returns true iff DECLARATOR is a declaration for a function. */
1250
1251 static bool
1252 function_declarator_p (const cp_declarator *declarator)
1253 {
1254 while (declarator)
1255 {
1256 if (declarator->kind == cdk_function
1257 && declarator->declarator->kind == cdk_id)
1258 return true;
1259 if (declarator->kind == cdk_id
1260 || declarator->kind == cdk_error)
1261 return false;
1262 declarator = declarator->declarator;
1263 }
1264 return false;
1265 }
1266
1267 /* The parser. */
1268
1269 /* Overview
1270 --------
1271
1272 A cp_parser parses the token stream as specified by the C++
1273 grammar. Its job is purely parsing, not semantic analysis. For
1274 example, the parser breaks the token stream into declarators,
1275 expressions, statements, and other similar syntactic constructs.
1276 It does not check that the types of the expressions on either side
1277 of an assignment-statement are compatible, or that a function is
1278 not declared with a parameter of type `void'.
1279
1280 The parser invokes routines elsewhere in the compiler to perform
1281 semantic analysis and to build up the abstract syntax tree for the
1282 code processed.
1283
1284 The parser (and the template instantiation code, which is, in a
1285 way, a close relative of parsing) are the only parts of the
1286 compiler that should be calling push_scope and pop_scope, or
1287 related functions. The parser (and template instantiation code)
1288 keeps track of what scope is presently active; everything else
1289 should simply honor that. (The code that generates static
1290 initializers may also need to set the scope, in order to check
1291 access control correctly when emitting the initializers.)
1292
1293 Methodology
1294 -----------
1295
1296 The parser is of the standard recursive-descent variety. Upcoming
1297 tokens in the token stream are examined in order to determine which
1298 production to use when parsing a non-terminal. Some C++ constructs
1299 require arbitrary look ahead to disambiguate. For example, it is
1300 impossible, in the general case, to tell whether a statement is an
1301 expression or declaration without scanning the entire statement.
1302 Therefore, the parser is capable of "parsing tentatively." When the
1303 parser is not sure what construct comes next, it enters this mode.
1304 Then, while we attempt to parse the construct, the parser queues up
1305 error messages, rather than issuing them immediately, and saves the
1306 tokens it consumes. If the construct is parsed successfully, the
1307 parser "commits", i.e., it issues any queued error messages and
1308 the tokens that were being preserved are permanently discarded.
1309 If, however, the construct is not parsed successfully, the parser
1310 rolls back its state completely so that it can resume parsing using
1311 a different alternative.
1312
1313 Future Improvements
1314 -------------------
1315
1316 The performance of the parser could probably be improved substantially.
1317 We could often eliminate the need to parse tentatively by looking ahead
1318 a little bit. In some places, this approach might not entirely eliminate
1319 the need to parse tentatively, but it might still speed up the average
1320 case. */
1321
1322 /* Flags that are passed to some parsing functions. These values can
1323 be bitwise-ored together. */
1324
1325 enum
1326 {
1327 /* No flags. */
1328 CP_PARSER_FLAGS_NONE = 0x0,
1329 /* The construct is optional. If it is not present, then no error
1330 should be issued. */
1331 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1332 /* When parsing a type-specifier, treat user-defined type-names
1333 as non-type identifiers. */
1334 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1335 /* When parsing a type-specifier, do not try to parse a class-specifier
1336 or enum-specifier. */
1337 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1338 };
1339
1340 /* This type is used for parameters and variables which hold
1341 combinations of the above flags. */
1342 typedef int cp_parser_flags;
1343
1344 /* The different kinds of declarators we want to parse. */
1345
1346 typedef enum cp_parser_declarator_kind
1347 {
1348 /* We want an abstract declarator. */
1349 CP_PARSER_DECLARATOR_ABSTRACT,
1350 /* We want a named declarator. */
1351 CP_PARSER_DECLARATOR_NAMED,
1352 /* We don't mind, but the name must be an unqualified-id. */
1353 CP_PARSER_DECLARATOR_EITHER
1354 } cp_parser_declarator_kind;
1355
1356 /* The precedence values used to parse binary expressions. The minimum value
1357 of PREC must be 1, because zero is reserved to quickly discriminate
1358 binary operators from other tokens. */
1359
1360 enum cp_parser_prec
1361 {
1362 PREC_NOT_OPERATOR,
1363 PREC_LOGICAL_OR_EXPRESSION,
1364 PREC_LOGICAL_AND_EXPRESSION,
1365 PREC_INCLUSIVE_OR_EXPRESSION,
1366 PREC_EXCLUSIVE_OR_EXPRESSION,
1367 PREC_AND_EXPRESSION,
1368 PREC_EQUALITY_EXPRESSION,
1369 PREC_RELATIONAL_EXPRESSION,
1370 PREC_SHIFT_EXPRESSION,
1371 PREC_ADDITIVE_EXPRESSION,
1372 PREC_MULTIPLICATIVE_EXPRESSION,
1373 PREC_PM_EXPRESSION,
1374 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1375 };
1376
1377 /* A mapping from a token type to a corresponding tree node type, with a
1378 precedence value. */
1379
1380 typedef struct cp_parser_binary_operations_map_node
1381 {
1382 /* The token type. */
1383 enum cpp_ttype token_type;
1384 /* The corresponding tree code. */
1385 enum tree_code tree_type;
1386 /* The precedence of this operator. */
1387 enum cp_parser_prec prec;
1388 } cp_parser_binary_operations_map_node;
1389
1390 /* The status of a tentative parse. */
1391
1392 typedef enum cp_parser_status_kind
1393 {
1394 /* No errors have occurred. */
1395 CP_PARSER_STATUS_KIND_NO_ERROR,
1396 /* An error has occurred. */
1397 CP_PARSER_STATUS_KIND_ERROR,
1398 /* We are committed to this tentative parse, whether or not an error
1399 has occurred. */
1400 CP_PARSER_STATUS_KIND_COMMITTED
1401 } cp_parser_status_kind;
1402
1403 typedef struct cp_parser_expression_stack_entry
1404 {
1405 /* Left hand side of the binary operation we are currently
1406 parsing. */
1407 tree lhs;
1408 /* Original tree code for left hand side, if it was a binary
1409 expression itself (used for -Wparentheses). */
1410 enum tree_code lhs_type;
1411 /* Tree code for the binary operation we are parsing. */
1412 enum tree_code tree_type;
1413 /* Precedence of the binary operation we are parsing. */
1414 enum cp_parser_prec prec;
1415 } cp_parser_expression_stack_entry;
1416
1417 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1418 entries because precedence levels on the stack are monotonically
1419 increasing. */
1420 typedef struct cp_parser_expression_stack_entry
1421 cp_parser_expression_stack[NUM_PREC_VALUES];
1422
1423 /* Context that is saved and restored when parsing tentatively. */
1424 typedef struct GTY (()) cp_parser_context {
1425 /* If this is a tentative parsing context, the status of the
1426 tentative parse. */
1427 enum cp_parser_status_kind status;
1428 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1429 that are looked up in this context must be looked up both in the
1430 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1431 the context of the containing expression. */
1432 tree object_type;
1433
1434 /* The next parsing context in the stack. */
1435 struct cp_parser_context *next;
1436 } cp_parser_context;
1437
1438 /* Prototypes. */
1439
1440 /* Constructors and destructors. */
1441
1442 static cp_parser_context *cp_parser_context_new
1443 (cp_parser_context *);
1444
1445 /* Class variables. */
1446
1447 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1448
1449 /* The operator-precedence table used by cp_parser_binary_expression.
1450 Transformed into an associative array (binops_by_token) by
1451 cp_parser_new. */
1452
1453 static const cp_parser_binary_operations_map_node binops[] = {
1454 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1455 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1456
1457 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1458 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1459 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1460
1461 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1462 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1463
1464 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1465 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1466
1467 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1468 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1469 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1470 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1471
1472 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1473 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1474
1475 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1476
1477 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1478
1479 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1480
1481 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1482
1483 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1484 };
1485
1486 /* The same as binops, but initialized by cp_parser_new so that
1487 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1488 for speed. */
1489 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1490
1491 /* Constructors and destructors. */
1492
1493 /* Construct a new context. The context below this one on the stack
1494 is given by NEXT. */
1495
1496 static cp_parser_context *
1497 cp_parser_context_new (cp_parser_context* next)
1498 {
1499 cp_parser_context *context;
1500
1501 /* Allocate the storage. */
1502 if (cp_parser_context_free_list != NULL)
1503 {
1504 /* Pull the first entry from the free list. */
1505 context = cp_parser_context_free_list;
1506 cp_parser_context_free_list = context->next;
1507 memset (context, 0, sizeof (*context));
1508 }
1509 else
1510 context = ggc_alloc_cleared_cp_parser_context ();
1511
1512 /* No errors have occurred yet in this context. */
1513 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1514 /* If this is not the bottommost context, copy information that we
1515 need from the previous context. */
1516 if (next)
1517 {
1518 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1519 expression, then we are parsing one in this context, too. */
1520 context->object_type = next->object_type;
1521 /* Thread the stack. */
1522 context->next = next;
1523 }
1524
1525 return context;
1526 }
1527
1528 /* An entry in a queue of function arguments that require post-processing. */
1529
1530 typedef struct GTY(()) cp_default_arg_entry_d {
1531 /* The current_class_type when we parsed this arg. */
1532 tree class_type;
1533
1534 /* The function decl itself. */
1535 tree decl;
1536 } cp_default_arg_entry;
1537
1538 DEF_VEC_O(cp_default_arg_entry);
1539 DEF_VEC_ALLOC_O(cp_default_arg_entry,gc);
1540
1541 /* An entry in a stack for member functions of local classes. */
1542
1543 typedef struct GTY(()) cp_unparsed_functions_entry_d {
1544 /* Functions with default arguments that require post-processing.
1545 Functions appear in this list in declaration order. */
1546 VEC(cp_default_arg_entry,gc) *funs_with_default_args;
1547
1548 /* Functions with defintions that require post-processing. Functions
1549 appear in this list in declaration order. */
1550 VEC(tree,gc) *funs_with_definitions;
1551 } cp_unparsed_functions_entry;
1552
1553 DEF_VEC_O(cp_unparsed_functions_entry);
1554 DEF_VEC_ALLOC_O(cp_unparsed_functions_entry,gc);
1555
1556 /* The cp_parser structure represents the C++ parser. */
1557
1558 typedef struct GTY(()) cp_parser {
1559 /* The lexer from which we are obtaining tokens. */
1560 cp_lexer *lexer;
1561
1562 /* The scope in which names should be looked up. If NULL_TREE, then
1563 we look up names in the scope that is currently open in the
1564 source program. If non-NULL, this is either a TYPE or
1565 NAMESPACE_DECL for the scope in which we should look. It can
1566 also be ERROR_MARK, when we've parsed a bogus scope.
1567
1568 This value is not cleared automatically after a name is looked
1569 up, so we must be careful to clear it before starting a new look
1570 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1571 will look up `Z' in the scope of `X', rather than the current
1572 scope.) Unfortunately, it is difficult to tell when name lookup
1573 is complete, because we sometimes peek at a token, look it up,
1574 and then decide not to consume it. */
1575 tree scope;
1576
1577 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1578 last lookup took place. OBJECT_SCOPE is used if an expression
1579 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1580 respectively. QUALIFYING_SCOPE is used for an expression of the
1581 form "X::Y"; it refers to X. */
1582 tree object_scope;
1583 tree qualifying_scope;
1584
1585 /* A stack of parsing contexts. All but the bottom entry on the
1586 stack will be tentative contexts.
1587
1588 We parse tentatively in order to determine which construct is in
1589 use in some situations. For example, in order to determine
1590 whether a statement is an expression-statement or a
1591 declaration-statement we parse it tentatively as a
1592 declaration-statement. If that fails, we then reparse the same
1593 token stream as an expression-statement. */
1594 cp_parser_context *context;
1595
1596 /* True if we are parsing GNU C++. If this flag is not set, then
1597 GNU extensions are not recognized. */
1598 bool allow_gnu_extensions_p;
1599
1600 /* TRUE if the `>' token should be interpreted as the greater-than
1601 operator. FALSE if it is the end of a template-id or
1602 template-parameter-list. In C++0x mode, this flag also applies to
1603 `>>' tokens, which are viewed as two consecutive `>' tokens when
1604 this flag is FALSE. */
1605 bool greater_than_is_operator_p;
1606
1607 /* TRUE if default arguments are allowed within a parameter list
1608 that starts at this point. FALSE if only a gnu extension makes
1609 them permissible. */
1610 bool default_arg_ok_p;
1611
1612 /* TRUE if we are parsing an integral constant-expression. See
1613 [expr.const] for a precise definition. */
1614 bool integral_constant_expression_p;
1615
1616 /* TRUE if we are parsing an integral constant-expression -- but a
1617 non-constant expression should be permitted as well. This flag
1618 is used when parsing an array bound so that GNU variable-length
1619 arrays are tolerated. */
1620 bool allow_non_integral_constant_expression_p;
1621
1622 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1623 been seen that makes the expression non-constant. */
1624 bool non_integral_constant_expression_p;
1625
1626 /* TRUE if local variable names and `this' are forbidden in the
1627 current context. */
1628 bool local_variables_forbidden_p;
1629
1630 /* TRUE if the declaration we are parsing is part of a
1631 linkage-specification of the form `extern string-literal
1632 declaration'. */
1633 bool in_unbraced_linkage_specification_p;
1634
1635 /* TRUE if we are presently parsing a declarator, after the
1636 direct-declarator. */
1637 bool in_declarator_p;
1638
1639 /* TRUE if we are presently parsing a template-argument-list. */
1640 bool in_template_argument_list_p;
1641
1642 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1643 to IN_OMP_BLOCK if parsing OpenMP structured block and
1644 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1645 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1646 iteration-statement, OpenMP block or loop within that switch. */
1647 #define IN_SWITCH_STMT 1
1648 #define IN_ITERATION_STMT 2
1649 #define IN_OMP_BLOCK 4
1650 #define IN_OMP_FOR 8
1651 #define IN_IF_STMT 16
1652 unsigned char in_statement;
1653
1654 /* TRUE if we are presently parsing the body of a switch statement.
1655 Note that this doesn't quite overlap with in_statement above.
1656 The difference relates to giving the right sets of error messages:
1657 "case not in switch" vs "break statement used with OpenMP...". */
1658 bool in_switch_statement_p;
1659
1660 /* TRUE if we are parsing a type-id in an expression context. In
1661 such a situation, both "type (expr)" and "type (type)" are valid
1662 alternatives. */
1663 bool in_type_id_in_expr_p;
1664
1665 /* TRUE if we are currently in a header file where declarations are
1666 implicitly extern "C". */
1667 bool implicit_extern_c;
1668
1669 /* TRUE if strings in expressions should be translated to the execution
1670 character set. */
1671 bool translate_strings_p;
1672
1673 /* TRUE if we are presently parsing the body of a function, but not
1674 a local class. */
1675 bool in_function_body;
1676
1677 /* If non-NULL, then we are parsing a construct where new type
1678 definitions are not permitted. The string stored here will be
1679 issued as an error message if a type is defined. */
1680 const char *type_definition_forbidden_message;
1681
1682 /* A stack used for member functions of local classes. The lists
1683 contained in an individual entry can only be processed once the
1684 outermost class being defined is complete. */
1685 VEC(cp_unparsed_functions_entry,gc) *unparsed_queues;
1686
1687 /* The number of classes whose definitions are currently in
1688 progress. */
1689 unsigned num_classes_being_defined;
1690
1691 /* The number of template parameter lists that apply directly to the
1692 current declaration. */
1693 unsigned num_template_parameter_lists;
1694 } cp_parser;
1695
1696 /* Managing the unparsed function queues. */
1697
1698 #define unparsed_funs_with_default_args \
1699 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1700 #define unparsed_funs_with_definitions \
1701 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1702
1703 static void
1704 push_unparsed_function_queues (cp_parser *parser)
1705 {
1706 VEC_safe_push (cp_unparsed_functions_entry, gc,
1707 parser->unparsed_queues, NULL);
1708 unparsed_funs_with_default_args = NULL;
1709 unparsed_funs_with_definitions = make_tree_vector ();
1710 }
1711
1712 static void
1713 pop_unparsed_function_queues (cp_parser *parser)
1714 {
1715 release_tree_vector (unparsed_funs_with_definitions);
1716 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1717 }
1718
1719 /* Prototypes. */
1720
1721 /* Constructors and destructors. */
1722
1723 static cp_parser *cp_parser_new
1724 (void);
1725
1726 /* Routines to parse various constructs.
1727
1728 Those that return `tree' will return the error_mark_node (rather
1729 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1730 Sometimes, they will return an ordinary node if error-recovery was
1731 attempted, even though a parse error occurred. So, to check
1732 whether or not a parse error occurred, you should always use
1733 cp_parser_error_occurred. If the construct is optional (indicated
1734 either by an `_opt' in the name of the function that does the
1735 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1736 the construct is not present. */
1737
1738 /* Lexical conventions [gram.lex] */
1739
1740 static tree cp_parser_identifier
1741 (cp_parser *);
1742 static tree cp_parser_string_literal
1743 (cp_parser *, bool, bool);
1744
1745 /* Basic concepts [gram.basic] */
1746
1747 static bool cp_parser_translation_unit
1748 (cp_parser *);
1749
1750 /* Expressions [gram.expr] */
1751
1752 static tree cp_parser_primary_expression
1753 (cp_parser *, bool, bool, bool, cp_id_kind *);
1754 static tree cp_parser_id_expression
1755 (cp_parser *, bool, bool, bool *, bool, bool);
1756 static tree cp_parser_unqualified_id
1757 (cp_parser *, bool, bool, bool, bool);
1758 static tree cp_parser_nested_name_specifier_opt
1759 (cp_parser *, bool, bool, bool, bool);
1760 static tree cp_parser_nested_name_specifier
1761 (cp_parser *, bool, bool, bool, bool);
1762 static tree cp_parser_qualifying_entity
1763 (cp_parser *, bool, bool, bool, bool, bool);
1764 static tree cp_parser_postfix_expression
1765 (cp_parser *, bool, bool, bool, cp_id_kind *);
1766 static tree cp_parser_postfix_open_square_expression
1767 (cp_parser *, tree, bool);
1768 static tree cp_parser_postfix_dot_deref_expression
1769 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1770 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1771 (cp_parser *, int, bool, bool, bool *);
1772 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1773 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1774 static void cp_parser_pseudo_destructor_name
1775 (cp_parser *, tree *, tree *);
1776 static tree cp_parser_unary_expression
1777 (cp_parser *, bool, bool, cp_id_kind *);
1778 static enum tree_code cp_parser_unary_operator
1779 (cp_token *);
1780 static tree cp_parser_new_expression
1781 (cp_parser *);
1782 static VEC(tree,gc) *cp_parser_new_placement
1783 (cp_parser *);
1784 static tree cp_parser_new_type_id
1785 (cp_parser *, tree *);
1786 static cp_declarator *cp_parser_new_declarator_opt
1787 (cp_parser *);
1788 static cp_declarator *cp_parser_direct_new_declarator
1789 (cp_parser *);
1790 static VEC(tree,gc) *cp_parser_new_initializer
1791 (cp_parser *);
1792 static tree cp_parser_delete_expression
1793 (cp_parser *);
1794 static tree cp_parser_cast_expression
1795 (cp_parser *, bool, bool, cp_id_kind *);
1796 static tree cp_parser_binary_expression
1797 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1798 static tree cp_parser_question_colon_clause
1799 (cp_parser *, tree);
1800 static tree cp_parser_assignment_expression
1801 (cp_parser *, bool, cp_id_kind *);
1802 static enum tree_code cp_parser_assignment_operator_opt
1803 (cp_parser *);
1804 static tree cp_parser_expression
1805 (cp_parser *, bool, cp_id_kind *);
1806 static tree cp_parser_constant_expression
1807 (cp_parser *, bool, bool *);
1808 static tree cp_parser_builtin_offsetof
1809 (cp_parser *);
1810 static tree cp_parser_lambda_expression
1811 (cp_parser *);
1812 static void cp_parser_lambda_introducer
1813 (cp_parser *, tree);
1814 static void cp_parser_lambda_declarator_opt
1815 (cp_parser *, tree);
1816 static void cp_parser_lambda_body
1817 (cp_parser *, tree);
1818
1819 /* Statements [gram.stmt.stmt] */
1820
1821 static void cp_parser_statement
1822 (cp_parser *, tree, bool, bool *);
1823 static void cp_parser_label_for_labeled_statement
1824 (cp_parser *);
1825 static tree cp_parser_expression_statement
1826 (cp_parser *, tree);
1827 static tree cp_parser_compound_statement
1828 (cp_parser *, tree, bool);
1829 static void cp_parser_statement_seq_opt
1830 (cp_parser *, tree);
1831 static tree cp_parser_selection_statement
1832 (cp_parser *, bool *);
1833 static tree cp_parser_condition
1834 (cp_parser *);
1835 static tree cp_parser_iteration_statement
1836 (cp_parser *);
1837 static void cp_parser_for_init_statement
1838 (cp_parser *);
1839 static tree cp_parser_c_for
1840 (cp_parser *);
1841 static tree cp_parser_range_for
1842 (cp_parser *);
1843 static tree cp_parser_jump_statement
1844 (cp_parser *);
1845 static void cp_parser_declaration_statement
1846 (cp_parser *);
1847
1848 static tree cp_parser_implicitly_scoped_statement
1849 (cp_parser *, bool *);
1850 static void cp_parser_already_scoped_statement
1851 (cp_parser *);
1852
1853 /* Declarations [gram.dcl.dcl] */
1854
1855 static void cp_parser_declaration_seq_opt
1856 (cp_parser *);
1857 static void cp_parser_declaration
1858 (cp_parser *);
1859 static void cp_parser_block_declaration
1860 (cp_parser *, bool);
1861 static void cp_parser_simple_declaration
1862 (cp_parser *, bool);
1863 static void cp_parser_decl_specifier_seq
1864 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1865 static tree cp_parser_storage_class_specifier_opt
1866 (cp_parser *);
1867 static tree cp_parser_function_specifier_opt
1868 (cp_parser *, cp_decl_specifier_seq *);
1869 static tree cp_parser_type_specifier
1870 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1871 int *, bool *);
1872 static tree cp_parser_simple_type_specifier
1873 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1874 static tree cp_parser_type_name
1875 (cp_parser *);
1876 static tree cp_parser_nonclass_name
1877 (cp_parser* parser);
1878 static tree cp_parser_elaborated_type_specifier
1879 (cp_parser *, bool, bool);
1880 static tree cp_parser_enum_specifier
1881 (cp_parser *);
1882 static void cp_parser_enumerator_list
1883 (cp_parser *, tree);
1884 static void cp_parser_enumerator_definition
1885 (cp_parser *, tree);
1886 static tree cp_parser_namespace_name
1887 (cp_parser *);
1888 static void cp_parser_namespace_definition
1889 (cp_parser *);
1890 static void cp_parser_namespace_body
1891 (cp_parser *);
1892 static tree cp_parser_qualified_namespace_specifier
1893 (cp_parser *);
1894 static void cp_parser_namespace_alias_definition
1895 (cp_parser *);
1896 static bool cp_parser_using_declaration
1897 (cp_parser *, bool);
1898 static void cp_parser_using_directive
1899 (cp_parser *);
1900 static void cp_parser_asm_definition
1901 (cp_parser *);
1902 static void cp_parser_linkage_specification
1903 (cp_parser *);
1904 static void cp_parser_static_assert
1905 (cp_parser *, bool);
1906 static tree cp_parser_decltype
1907 (cp_parser *);
1908
1909 /* Declarators [gram.dcl.decl] */
1910
1911 static tree cp_parser_init_declarator
1912 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1913 static cp_declarator *cp_parser_declarator
1914 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1915 static cp_declarator *cp_parser_direct_declarator
1916 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1917 static enum tree_code cp_parser_ptr_operator
1918 (cp_parser *, tree *, cp_cv_quals *);
1919 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1920 (cp_parser *);
1921 static tree cp_parser_late_return_type_opt
1922 (cp_parser *);
1923 static tree cp_parser_declarator_id
1924 (cp_parser *, bool);
1925 static tree cp_parser_type_id
1926 (cp_parser *);
1927 static tree cp_parser_template_type_arg
1928 (cp_parser *);
1929 static tree cp_parser_trailing_type_id (cp_parser *);
1930 static tree cp_parser_type_id_1
1931 (cp_parser *, bool, bool);
1932 static void cp_parser_type_specifier_seq
1933 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1934 static tree cp_parser_parameter_declaration_clause
1935 (cp_parser *);
1936 static tree cp_parser_parameter_declaration_list
1937 (cp_parser *, bool *);
1938 static cp_parameter_declarator *cp_parser_parameter_declaration
1939 (cp_parser *, bool, bool *);
1940 static tree cp_parser_default_argument
1941 (cp_parser *, bool);
1942 static void cp_parser_function_body
1943 (cp_parser *);
1944 static tree cp_parser_initializer
1945 (cp_parser *, bool *, bool *);
1946 static tree cp_parser_initializer_clause
1947 (cp_parser *, bool *);
1948 static tree cp_parser_braced_list
1949 (cp_parser*, bool*);
1950 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1951 (cp_parser *, bool *);
1952
1953 static bool cp_parser_ctor_initializer_opt_and_function_body
1954 (cp_parser *);
1955
1956 /* Classes [gram.class] */
1957
1958 static tree cp_parser_class_name
1959 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1960 static tree cp_parser_class_specifier
1961 (cp_parser *);
1962 static tree cp_parser_class_head
1963 (cp_parser *, bool *, tree *, tree *);
1964 static enum tag_types cp_parser_class_key
1965 (cp_parser *);
1966 static void cp_parser_member_specification_opt
1967 (cp_parser *);
1968 static void cp_parser_member_declaration
1969 (cp_parser *);
1970 static tree cp_parser_pure_specifier
1971 (cp_parser *);
1972 static tree cp_parser_constant_initializer
1973 (cp_parser *);
1974
1975 /* Derived classes [gram.class.derived] */
1976
1977 static tree cp_parser_base_clause
1978 (cp_parser *);
1979 static tree cp_parser_base_specifier
1980 (cp_parser *);
1981
1982 /* Special member functions [gram.special] */
1983
1984 static tree cp_parser_conversion_function_id
1985 (cp_parser *);
1986 static tree cp_parser_conversion_type_id
1987 (cp_parser *);
1988 static cp_declarator *cp_parser_conversion_declarator_opt
1989 (cp_parser *);
1990 static bool cp_parser_ctor_initializer_opt
1991 (cp_parser *);
1992 static void cp_parser_mem_initializer_list
1993 (cp_parser *);
1994 static tree cp_parser_mem_initializer
1995 (cp_parser *);
1996 static tree cp_parser_mem_initializer_id
1997 (cp_parser *);
1998
1999 /* Overloading [gram.over] */
2000
2001 static tree cp_parser_operator_function_id
2002 (cp_parser *);
2003 static tree cp_parser_operator
2004 (cp_parser *);
2005
2006 /* Templates [gram.temp] */
2007
2008 static void cp_parser_template_declaration
2009 (cp_parser *, bool);
2010 static tree cp_parser_template_parameter_list
2011 (cp_parser *);
2012 static tree cp_parser_template_parameter
2013 (cp_parser *, bool *, bool *);
2014 static tree cp_parser_type_parameter
2015 (cp_parser *, bool *);
2016 static tree cp_parser_template_id
2017 (cp_parser *, bool, bool, bool);
2018 static tree cp_parser_template_name
2019 (cp_parser *, bool, bool, bool, bool *);
2020 static tree cp_parser_template_argument_list
2021 (cp_parser *);
2022 static tree cp_parser_template_argument
2023 (cp_parser *);
2024 static void cp_parser_explicit_instantiation
2025 (cp_parser *);
2026 static void cp_parser_explicit_specialization
2027 (cp_parser *);
2028
2029 /* Exception handling [gram.exception] */
2030
2031 static tree cp_parser_try_block
2032 (cp_parser *);
2033 static bool cp_parser_function_try_block
2034 (cp_parser *);
2035 static void cp_parser_handler_seq
2036 (cp_parser *);
2037 static void cp_parser_handler
2038 (cp_parser *);
2039 static tree cp_parser_exception_declaration
2040 (cp_parser *);
2041 static tree cp_parser_throw_expression
2042 (cp_parser *);
2043 static tree cp_parser_exception_specification_opt
2044 (cp_parser *);
2045 static tree cp_parser_type_id_list
2046 (cp_parser *);
2047
2048 /* GNU Extensions */
2049
2050 static tree cp_parser_asm_specification_opt
2051 (cp_parser *);
2052 static tree cp_parser_asm_operand_list
2053 (cp_parser *);
2054 static tree cp_parser_asm_clobber_list
2055 (cp_parser *);
2056 static tree cp_parser_asm_label_list
2057 (cp_parser *);
2058 static tree cp_parser_attributes_opt
2059 (cp_parser *);
2060 static tree cp_parser_attribute_list
2061 (cp_parser *);
2062 static bool cp_parser_extension_opt
2063 (cp_parser *, int *);
2064 static void cp_parser_label_declaration
2065 (cp_parser *);
2066
2067 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2068 static bool cp_parser_pragma
2069 (cp_parser *, enum pragma_context);
2070
2071 /* Objective-C++ Productions */
2072
2073 static tree cp_parser_objc_message_receiver
2074 (cp_parser *);
2075 static tree cp_parser_objc_message_args
2076 (cp_parser *);
2077 static tree cp_parser_objc_message_expression
2078 (cp_parser *);
2079 static tree cp_parser_objc_encode_expression
2080 (cp_parser *);
2081 static tree cp_parser_objc_defs_expression
2082 (cp_parser *);
2083 static tree cp_parser_objc_protocol_expression
2084 (cp_parser *);
2085 static tree cp_parser_objc_selector_expression
2086 (cp_parser *);
2087 static tree cp_parser_objc_expression
2088 (cp_parser *);
2089 static bool cp_parser_objc_selector_p
2090 (enum cpp_ttype);
2091 static tree cp_parser_objc_selector
2092 (cp_parser *);
2093 static tree cp_parser_objc_protocol_refs_opt
2094 (cp_parser *);
2095 static void cp_parser_objc_declaration
2096 (cp_parser *, tree);
2097 static tree cp_parser_objc_statement
2098 (cp_parser *);
2099 static bool cp_parser_objc_valid_prefix_attributes
2100 (cp_parser* parser, tree *attrib);
2101
2102 /* Utility Routines */
2103
2104 static tree cp_parser_lookup_name
2105 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2106 static tree cp_parser_lookup_name_simple
2107 (cp_parser *, tree, location_t);
2108 static tree cp_parser_maybe_treat_template_as_class
2109 (tree, bool);
2110 static bool cp_parser_check_declarator_template_parameters
2111 (cp_parser *, cp_declarator *, location_t);
2112 static bool cp_parser_check_template_parameters
2113 (cp_parser *, unsigned, location_t, cp_declarator *);
2114 static tree cp_parser_simple_cast_expression
2115 (cp_parser *);
2116 static tree cp_parser_global_scope_opt
2117 (cp_parser *, bool);
2118 static bool cp_parser_constructor_declarator_p
2119 (cp_parser *, bool);
2120 static tree cp_parser_function_definition_from_specifiers_and_declarator
2121 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2122 static tree cp_parser_function_definition_after_declarator
2123 (cp_parser *, bool);
2124 static void cp_parser_template_declaration_after_export
2125 (cp_parser *, bool);
2126 static void cp_parser_perform_template_parameter_access_checks
2127 (VEC (deferred_access_check,gc)*);
2128 static tree cp_parser_single_declaration
2129 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2130 static tree cp_parser_functional_cast
2131 (cp_parser *, tree);
2132 static tree cp_parser_save_member_function_body
2133 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2134 static tree cp_parser_enclosed_template_argument_list
2135 (cp_parser *);
2136 static void cp_parser_save_default_args
2137 (cp_parser *, tree);
2138 static void cp_parser_late_parsing_for_member
2139 (cp_parser *, tree);
2140 static void cp_parser_late_parsing_default_args
2141 (cp_parser *, tree);
2142 static tree cp_parser_sizeof_operand
2143 (cp_parser *, enum rid);
2144 static tree cp_parser_trait_expr
2145 (cp_parser *, enum rid);
2146 static bool cp_parser_declares_only_class_p
2147 (cp_parser *);
2148 static void cp_parser_set_storage_class
2149 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2150 static void cp_parser_set_decl_spec_type
2151 (cp_decl_specifier_seq *, tree, location_t, bool);
2152 static bool cp_parser_friend_p
2153 (const cp_decl_specifier_seq *);
2154 static void cp_parser_required_error
2155 (cp_parser *, required_token, bool);
2156 static cp_token *cp_parser_require
2157 (cp_parser *, enum cpp_ttype, required_token);
2158 static cp_token *cp_parser_require_keyword
2159 (cp_parser *, enum rid, required_token);
2160 static bool cp_parser_token_starts_function_definition_p
2161 (cp_token *);
2162 static bool cp_parser_next_token_starts_class_definition_p
2163 (cp_parser *);
2164 static bool cp_parser_next_token_ends_template_argument_p
2165 (cp_parser *);
2166 static bool cp_parser_nth_token_starts_template_argument_list_p
2167 (cp_parser *, size_t);
2168 static enum tag_types cp_parser_token_is_class_key
2169 (cp_token *);
2170 static void cp_parser_check_class_key
2171 (enum tag_types, tree type);
2172 static void cp_parser_check_access_in_redeclaration
2173 (tree type, location_t location);
2174 static bool cp_parser_optional_template_keyword
2175 (cp_parser *);
2176 static void cp_parser_pre_parsed_nested_name_specifier
2177 (cp_parser *);
2178 static bool cp_parser_cache_group
2179 (cp_parser *, enum cpp_ttype, unsigned);
2180 static void cp_parser_parse_tentatively
2181 (cp_parser *);
2182 static void cp_parser_commit_to_tentative_parse
2183 (cp_parser *);
2184 static void cp_parser_abort_tentative_parse
2185 (cp_parser *);
2186 static bool cp_parser_parse_definitely
2187 (cp_parser *);
2188 static inline bool cp_parser_parsing_tentatively
2189 (cp_parser *);
2190 static bool cp_parser_uncommitted_to_tentative_parse_p
2191 (cp_parser *);
2192 static void cp_parser_error
2193 (cp_parser *, const char *);
2194 static void cp_parser_name_lookup_error
2195 (cp_parser *, tree, tree, name_lookup_error, location_t);
2196 static bool cp_parser_simulate_error
2197 (cp_parser *);
2198 static bool cp_parser_check_type_definition
2199 (cp_parser *);
2200 static void cp_parser_check_for_definition_in_return_type
2201 (cp_declarator *, tree, location_t type_location);
2202 static void cp_parser_check_for_invalid_template_id
2203 (cp_parser *, tree, location_t location);
2204 static bool cp_parser_non_integral_constant_expression
2205 (cp_parser *, non_integral_constant);
2206 static void cp_parser_diagnose_invalid_type_name
2207 (cp_parser *, tree, tree, location_t);
2208 static bool cp_parser_parse_and_diagnose_invalid_type_name
2209 (cp_parser *);
2210 static int cp_parser_skip_to_closing_parenthesis
2211 (cp_parser *, bool, bool, bool);
2212 static void cp_parser_skip_to_end_of_statement
2213 (cp_parser *);
2214 static void cp_parser_consume_semicolon_at_end_of_statement
2215 (cp_parser *);
2216 static void cp_parser_skip_to_end_of_block_or_statement
2217 (cp_parser *);
2218 static bool cp_parser_skip_to_closing_brace
2219 (cp_parser *);
2220 static void cp_parser_skip_to_end_of_template_parameter_list
2221 (cp_parser *);
2222 static void cp_parser_skip_to_pragma_eol
2223 (cp_parser*, cp_token *);
2224 static bool cp_parser_error_occurred
2225 (cp_parser *);
2226 static bool cp_parser_allow_gnu_extensions_p
2227 (cp_parser *);
2228 static bool cp_parser_is_string_literal
2229 (cp_token *);
2230 static bool cp_parser_is_keyword
2231 (cp_token *, enum rid);
2232 static tree cp_parser_make_typename_type
2233 (cp_parser *, tree, tree, location_t location);
2234 static cp_declarator * cp_parser_make_indirect_declarator
2235 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2236
2237 /* Returns nonzero if we are parsing tentatively. */
2238
2239 static inline bool
2240 cp_parser_parsing_tentatively (cp_parser* parser)
2241 {
2242 return parser->context->next != NULL;
2243 }
2244
2245 /* Returns nonzero if TOKEN is a string literal. */
2246
2247 static bool
2248 cp_parser_is_string_literal (cp_token* token)
2249 {
2250 return (token->type == CPP_STRING ||
2251 token->type == CPP_STRING16 ||
2252 token->type == CPP_STRING32 ||
2253 token->type == CPP_WSTRING ||
2254 token->type == CPP_UTF8STRING);
2255 }
2256
2257 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2258
2259 static bool
2260 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2261 {
2262 return token->keyword == keyword;
2263 }
2264
2265 /* If not parsing tentatively, issue a diagnostic of the form
2266 FILE:LINE: MESSAGE before TOKEN
2267 where TOKEN is the next token in the input stream. MESSAGE
2268 (specified by the caller) is usually of the form "expected
2269 OTHER-TOKEN". */
2270
2271 static void
2272 cp_parser_error (cp_parser* parser, const char* gmsgid)
2273 {
2274 if (!cp_parser_simulate_error (parser))
2275 {
2276 cp_token *token = cp_lexer_peek_token (parser->lexer);
2277 /* This diagnostic makes more sense if it is tagged to the line
2278 of the token we just peeked at. */
2279 cp_lexer_set_source_position_from_token (token);
2280
2281 if (token->type == CPP_PRAGMA)
2282 {
2283 error_at (token->location,
2284 "%<#pragma%> is not allowed here");
2285 cp_parser_skip_to_pragma_eol (parser, token);
2286 return;
2287 }
2288
2289 c_parse_error (gmsgid,
2290 /* Because c_parser_error does not understand
2291 CPP_KEYWORD, keywords are treated like
2292 identifiers. */
2293 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2294 token->u.value, token->flags);
2295 }
2296 }
2297
2298 /* Issue an error about name-lookup failing. NAME is the
2299 IDENTIFIER_NODE DECL is the result of
2300 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2301 the thing that we hoped to find. */
2302
2303 static void
2304 cp_parser_name_lookup_error (cp_parser* parser,
2305 tree name,
2306 tree decl,
2307 name_lookup_error desired,
2308 location_t location)
2309 {
2310 /* If name lookup completely failed, tell the user that NAME was not
2311 declared. */
2312 if (decl == error_mark_node)
2313 {
2314 if (parser->scope && parser->scope != global_namespace)
2315 error_at (location, "%<%E::%E%> has not been declared",
2316 parser->scope, name);
2317 else if (parser->scope == global_namespace)
2318 error_at (location, "%<::%E%> has not been declared", name);
2319 else if (parser->object_scope
2320 && !CLASS_TYPE_P (parser->object_scope))
2321 error_at (location, "request for member %qE in non-class type %qT",
2322 name, parser->object_scope);
2323 else if (parser->object_scope)
2324 error_at (location, "%<%T::%E%> has not been declared",
2325 parser->object_scope, name);
2326 else
2327 error_at (location, "%qE has not been declared", name);
2328 }
2329 else if (parser->scope && parser->scope != global_namespace)
2330 {
2331 switch (desired)
2332 {
2333 case NLE_TYPE:
2334 error_at (location, "%<%E::%E%> is not a type",
2335 parser->scope, name);
2336 break;
2337 case NLE_CXX98:
2338 error_at (location, "%<%E::%E%> is not a class or namespace",
2339 parser->scope, name);
2340 break;
2341 case NLE_NOT_CXX98:
2342 error_at (location,
2343 "%<%E::%E%> is not a class, namespace, or enumeration",
2344 parser->scope, name);
2345 break;
2346 default:
2347 gcc_unreachable ();
2348
2349 }
2350 }
2351 else if (parser->scope == global_namespace)
2352 {
2353 switch (desired)
2354 {
2355 case NLE_TYPE:
2356 error_at (location, "%<::%E%> is not a type", name);
2357 break;
2358 case NLE_CXX98:
2359 error_at (location, "%<::%E%> is not a class or namespace", name);
2360 break;
2361 case NLE_NOT_CXX98:
2362 error_at (location,
2363 "%<::%E%> is not a class, namespace, or enumeration",
2364 name);
2365 break;
2366 default:
2367 gcc_unreachable ();
2368 }
2369 }
2370 else
2371 {
2372 switch (desired)
2373 {
2374 case NLE_TYPE:
2375 error_at (location, "%qE is not a type", name);
2376 break;
2377 case NLE_CXX98:
2378 error_at (location, "%qE is not a class or namespace", name);
2379 break;
2380 case NLE_NOT_CXX98:
2381 error_at (location,
2382 "%qE is not a class, namespace, or enumeration", name);
2383 break;
2384 default:
2385 gcc_unreachable ();
2386 }
2387 }
2388 }
2389
2390 /* If we are parsing tentatively, remember that an error has occurred
2391 during this tentative parse. Returns true if the error was
2392 simulated; false if a message should be issued by the caller. */
2393
2394 static bool
2395 cp_parser_simulate_error (cp_parser* parser)
2396 {
2397 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2398 {
2399 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2400 return true;
2401 }
2402 return false;
2403 }
2404
2405 /* Check for repeated decl-specifiers. */
2406
2407 static void
2408 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2409 location_t location)
2410 {
2411 int ds;
2412
2413 for (ds = ds_first; ds != ds_last; ++ds)
2414 {
2415 unsigned count = decl_specs->specs[ds];
2416 if (count < 2)
2417 continue;
2418 /* The "long" specifier is a special case because of "long long". */
2419 if (ds == ds_long)
2420 {
2421 if (count > 2)
2422 error_at (location, "%<long long long%> is too long for GCC");
2423 else
2424 pedwarn_cxx98 (location, OPT_Wlong_long,
2425 "ISO C++ 1998 does not support %<long long%>");
2426 }
2427 else if (count > 1)
2428 {
2429 static const char *const decl_spec_names[] = {
2430 "signed",
2431 "unsigned",
2432 "short",
2433 "long",
2434 "const",
2435 "volatile",
2436 "restrict",
2437 "inline",
2438 "virtual",
2439 "explicit",
2440 "friend",
2441 "typedef",
2442 "constexpr",
2443 "__complex",
2444 "__thread"
2445 };
2446 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2447 }
2448 }
2449 }
2450
2451 /* This function is called when a type is defined. If type
2452 definitions are forbidden at this point, an error message is
2453 issued. */
2454
2455 static bool
2456 cp_parser_check_type_definition (cp_parser* parser)
2457 {
2458 /* If types are forbidden here, issue a message. */
2459 if (parser->type_definition_forbidden_message)
2460 {
2461 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2462 in the message need to be interpreted. */
2463 error (parser->type_definition_forbidden_message);
2464 return false;
2465 }
2466 return true;
2467 }
2468
2469 /* This function is called when the DECLARATOR is processed. The TYPE
2470 was a type defined in the decl-specifiers. If it is invalid to
2471 define a type in the decl-specifiers for DECLARATOR, an error is
2472 issued. TYPE_LOCATION is the location of TYPE and is used
2473 for error reporting. */
2474
2475 static void
2476 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2477 tree type, location_t type_location)
2478 {
2479 /* [dcl.fct] forbids type definitions in return types.
2480 Unfortunately, it's not easy to know whether or not we are
2481 processing a return type until after the fact. */
2482 while (declarator
2483 && (declarator->kind == cdk_pointer
2484 || declarator->kind == cdk_reference
2485 || declarator->kind == cdk_ptrmem))
2486 declarator = declarator->declarator;
2487 if (declarator
2488 && declarator->kind == cdk_function)
2489 {
2490 error_at (type_location,
2491 "new types may not be defined in a return type");
2492 inform (type_location,
2493 "(perhaps a semicolon is missing after the definition of %qT)",
2494 type);
2495 }
2496 }
2497
2498 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2499 "<" in any valid C++ program. If the next token is indeed "<",
2500 issue a message warning the user about what appears to be an
2501 invalid attempt to form a template-id. LOCATION is the location
2502 of the type-specifier (TYPE) */
2503
2504 static void
2505 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2506 tree type, location_t location)
2507 {
2508 cp_token_position start = 0;
2509
2510 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2511 {
2512 if (TYPE_P (type))
2513 error_at (location, "%qT is not a template", type);
2514 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2515 error_at (location, "%qE is not a template", type);
2516 else
2517 error_at (location, "invalid template-id");
2518 /* Remember the location of the invalid "<". */
2519 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2520 start = cp_lexer_token_position (parser->lexer, true);
2521 /* Consume the "<". */
2522 cp_lexer_consume_token (parser->lexer);
2523 /* Parse the template arguments. */
2524 cp_parser_enclosed_template_argument_list (parser);
2525 /* Permanently remove the invalid template arguments so that
2526 this error message is not issued again. */
2527 if (start)
2528 cp_lexer_purge_tokens_after (parser->lexer, start);
2529 }
2530 }
2531
2532 /* If parsing an integral constant-expression, issue an error message
2533 about the fact that THING appeared and return true. Otherwise,
2534 return false. In either case, set
2535 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2536
2537 static bool
2538 cp_parser_non_integral_constant_expression (cp_parser *parser,
2539 non_integral_constant thing)
2540 {
2541 parser->non_integral_constant_expression_p = true;
2542 if (parser->integral_constant_expression_p)
2543 {
2544 if (!parser->allow_non_integral_constant_expression_p)
2545 {
2546 const char *msg = NULL;
2547 switch (thing)
2548 {
2549 case NIC_FLOAT:
2550 error ("floating-point literal "
2551 "cannot appear in a constant-expression");
2552 return true;
2553 case NIC_CAST:
2554 error ("a cast to a type other than an integral or "
2555 "enumeration type cannot appear in a "
2556 "constant-expression");
2557 return true;
2558 case NIC_TYPEID:
2559 error ("%<typeid%> operator "
2560 "cannot appear in a constant-expression");
2561 return true;
2562 case NIC_NCC:
2563 error ("non-constant compound literals "
2564 "cannot appear in a constant-expression");
2565 return true;
2566 case NIC_FUNC_CALL:
2567 error ("a function call "
2568 "cannot appear in a constant-expression");
2569 return true;
2570 case NIC_INC:
2571 error ("an increment "
2572 "cannot appear in a constant-expression");
2573 return true;
2574 case NIC_DEC:
2575 error ("an decrement "
2576 "cannot appear in a constant-expression");
2577 return true;
2578 case NIC_ARRAY_REF:
2579 error ("an array reference "
2580 "cannot appear in a constant-expression");
2581 return true;
2582 case NIC_ADDR_LABEL:
2583 error ("the address of a label "
2584 "cannot appear in a constant-expression");
2585 return true;
2586 case NIC_OVERLOADED:
2587 error ("calls to overloaded operators "
2588 "cannot appear in a constant-expression");
2589 return true;
2590 case NIC_ASSIGNMENT:
2591 error ("an assignment cannot appear in a constant-expression");
2592 return true;
2593 case NIC_COMMA:
2594 error ("a comma operator "
2595 "cannot appear in a constant-expression");
2596 return true;
2597 case NIC_CONSTRUCTOR:
2598 error ("a call to a constructor "
2599 "cannot appear in a constant-expression");
2600 return true;
2601 case NIC_THIS:
2602 msg = "this";
2603 break;
2604 case NIC_FUNC_NAME:
2605 msg = "__FUNCTION__";
2606 break;
2607 case NIC_PRETTY_FUNC:
2608 msg = "__PRETTY_FUNCTION__";
2609 break;
2610 case NIC_C99_FUNC:
2611 msg = "__func__";
2612 break;
2613 case NIC_VA_ARG:
2614 msg = "va_arg";
2615 break;
2616 case NIC_ARROW:
2617 msg = "->";
2618 break;
2619 case NIC_POINT:
2620 msg = ".";
2621 break;
2622 case NIC_STAR:
2623 msg = "*";
2624 break;
2625 case NIC_ADDR:
2626 msg = "&";
2627 break;
2628 case NIC_PREINCREMENT:
2629 msg = "++";
2630 break;
2631 case NIC_PREDECREMENT:
2632 msg = "--";
2633 break;
2634 case NIC_NEW:
2635 msg = "new";
2636 break;
2637 case NIC_DEL:
2638 msg = "delete";
2639 break;
2640 default:
2641 gcc_unreachable ();
2642 }
2643 if (msg)
2644 error ("%qs cannot appear in a constant-expression", msg);
2645 return true;
2646 }
2647 }
2648 return false;
2649 }
2650
2651 /* Emit a diagnostic for an invalid type name. SCOPE is the
2652 qualifying scope (or NULL, if none) for ID. This function commits
2653 to the current active tentative parse, if any. (Otherwise, the
2654 problematic construct might be encountered again later, resulting
2655 in duplicate error messages.) LOCATION is the location of ID. */
2656
2657 static void
2658 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2659 tree scope, tree id,
2660 location_t location)
2661 {
2662 tree decl, old_scope;
2663 /* Try to lookup the identifier. */
2664 old_scope = parser->scope;
2665 parser->scope = scope;
2666 decl = cp_parser_lookup_name_simple (parser, id, location);
2667 parser->scope = old_scope;
2668 /* If the lookup found a template-name, it means that the user forgot
2669 to specify an argument list. Emit a useful error message. */
2670 if (TREE_CODE (decl) == TEMPLATE_DECL)
2671 error_at (location,
2672 "invalid use of template-name %qE without an argument list",
2673 decl);
2674 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2675 error_at (location, "invalid use of destructor %qD as a type", id);
2676 else if (TREE_CODE (decl) == TYPE_DECL)
2677 /* Something like 'unsigned A a;' */
2678 error_at (location, "invalid combination of multiple type-specifiers");
2679 else if (!parser->scope)
2680 {
2681 /* Issue an error message. */
2682 error_at (location, "%qE does not name a type", id);
2683 /* If we're in a template class, it's possible that the user was
2684 referring to a type from a base class. For example:
2685
2686 template <typename T> struct A { typedef T X; };
2687 template <typename T> struct B : public A<T> { X x; };
2688
2689 The user should have said "typename A<T>::X". */
2690 if (processing_template_decl && current_class_type
2691 && TYPE_BINFO (current_class_type))
2692 {
2693 tree b;
2694
2695 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2696 b;
2697 b = TREE_CHAIN (b))
2698 {
2699 tree base_type = BINFO_TYPE (b);
2700 if (CLASS_TYPE_P (base_type)
2701 && dependent_type_p (base_type))
2702 {
2703 tree field;
2704 /* Go from a particular instantiation of the
2705 template (which will have an empty TYPE_FIELDs),
2706 to the main version. */
2707 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2708 for (field = TYPE_FIELDS (base_type);
2709 field;
2710 field = DECL_CHAIN (field))
2711 if (TREE_CODE (field) == TYPE_DECL
2712 && DECL_NAME (field) == id)
2713 {
2714 inform (location,
2715 "(perhaps %<typename %T::%E%> was intended)",
2716 BINFO_TYPE (b), id);
2717 break;
2718 }
2719 if (field)
2720 break;
2721 }
2722 }
2723 }
2724 }
2725 /* Here we diagnose qualified-ids where the scope is actually correct,
2726 but the identifier does not resolve to a valid type name. */
2727 else if (parser->scope != error_mark_node)
2728 {
2729 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2730 error_at (location, "%qE in namespace %qE does not name a type",
2731 id, parser->scope);
2732 else if (CLASS_TYPE_P (parser->scope)
2733 && constructor_name_p (id, parser->scope))
2734 {
2735 /* A<T>::A<T>() */
2736 error_at (location, "%<%T::%E%> names the constructor, not"
2737 " the type", parser->scope, id);
2738 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2739 error_at (location, "and %qT has no template constructors",
2740 parser->scope);
2741 }
2742 else if (TYPE_P (parser->scope)
2743 && dependent_scope_p (parser->scope))
2744 error_at (location, "need %<typename%> before %<%T::%E%> because "
2745 "%qT is a dependent scope",
2746 parser->scope, id, parser->scope);
2747 else if (TYPE_P (parser->scope))
2748 error_at (location, "%qE in class %qT does not name a type",
2749 id, parser->scope);
2750 else
2751 gcc_unreachable ();
2752 }
2753 cp_parser_commit_to_tentative_parse (parser);
2754 }
2755
2756 /* Check for a common situation where a type-name should be present,
2757 but is not, and issue a sensible error message. Returns true if an
2758 invalid type-name was detected.
2759
2760 The situation handled by this function are variable declarations of the
2761 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2762 Usually, `ID' should name a type, but if we got here it means that it
2763 does not. We try to emit the best possible error message depending on
2764 how exactly the id-expression looks like. */
2765
2766 static bool
2767 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2768 {
2769 tree id;
2770 cp_token *token = cp_lexer_peek_token (parser->lexer);
2771
2772 /* Avoid duplicate error about ambiguous lookup. */
2773 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2774 {
2775 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2776 if (next->type == CPP_NAME && next->ambiguous_p)
2777 goto out;
2778 }
2779
2780 cp_parser_parse_tentatively (parser);
2781 id = cp_parser_id_expression (parser,
2782 /*template_keyword_p=*/false,
2783 /*check_dependency_p=*/true,
2784 /*template_p=*/NULL,
2785 /*declarator_p=*/true,
2786 /*optional_p=*/false);
2787 /* If the next token is a (, this is a function with no explicit return
2788 type, i.e. constructor, destructor or conversion op. */
2789 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2790 || TREE_CODE (id) == TYPE_DECL)
2791 {
2792 cp_parser_abort_tentative_parse (parser);
2793 return false;
2794 }
2795 if (!cp_parser_parse_definitely (parser))
2796 return false;
2797
2798 /* Emit a diagnostic for the invalid type. */
2799 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2800 id, token->location);
2801 out:
2802 /* If we aren't in the middle of a declarator (i.e. in a
2803 parameter-declaration-clause), skip to the end of the declaration;
2804 there's no point in trying to process it. */
2805 if (!parser->in_declarator_p)
2806 cp_parser_skip_to_end_of_block_or_statement (parser);
2807 return true;
2808 }
2809
2810 /* Consume tokens up to, and including, the next non-nested closing `)'.
2811 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2812 are doing error recovery. Returns -1 if OR_COMMA is true and we
2813 found an unnested comma. */
2814
2815 static int
2816 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2817 bool recovering,
2818 bool or_comma,
2819 bool consume_paren)
2820 {
2821 unsigned paren_depth = 0;
2822 unsigned brace_depth = 0;
2823 unsigned square_depth = 0;
2824
2825 if (recovering && !or_comma
2826 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2827 return 0;
2828
2829 while (true)
2830 {
2831 cp_token * token = cp_lexer_peek_token (parser->lexer);
2832
2833 switch (token->type)
2834 {
2835 case CPP_EOF:
2836 case CPP_PRAGMA_EOL:
2837 /* If we've run out of tokens, then there is no closing `)'. */
2838 return 0;
2839
2840 /* This is good for lambda expression capture-lists. */
2841 case CPP_OPEN_SQUARE:
2842 ++square_depth;
2843 break;
2844 case CPP_CLOSE_SQUARE:
2845 if (!square_depth--)
2846 return 0;
2847 break;
2848
2849 case CPP_SEMICOLON:
2850 /* This matches the processing in skip_to_end_of_statement. */
2851 if (!brace_depth)
2852 return 0;
2853 break;
2854
2855 case CPP_OPEN_BRACE:
2856 ++brace_depth;
2857 break;
2858 case CPP_CLOSE_BRACE:
2859 if (!brace_depth--)
2860 return 0;
2861 break;
2862
2863 case CPP_COMMA:
2864 if (recovering && or_comma && !brace_depth && !paren_depth
2865 && !square_depth)
2866 return -1;
2867 break;
2868
2869 case CPP_OPEN_PAREN:
2870 if (!brace_depth)
2871 ++paren_depth;
2872 break;
2873
2874 case CPP_CLOSE_PAREN:
2875 if (!brace_depth && !paren_depth--)
2876 {
2877 if (consume_paren)
2878 cp_lexer_consume_token (parser->lexer);
2879 return 1;
2880 }
2881 break;
2882
2883 default:
2884 break;
2885 }
2886
2887 /* Consume the token. */
2888 cp_lexer_consume_token (parser->lexer);
2889 }
2890 }
2891
2892 /* Consume tokens until we reach the end of the current statement.
2893 Normally, that will be just before consuming a `;'. However, if a
2894 non-nested `}' comes first, then we stop before consuming that. */
2895
2896 static void
2897 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2898 {
2899 unsigned nesting_depth = 0;
2900
2901 while (true)
2902 {
2903 cp_token *token = cp_lexer_peek_token (parser->lexer);
2904
2905 switch (token->type)
2906 {
2907 case CPP_EOF:
2908 case CPP_PRAGMA_EOL:
2909 /* If we've run out of tokens, stop. */
2910 return;
2911
2912 case CPP_SEMICOLON:
2913 /* If the next token is a `;', we have reached the end of the
2914 statement. */
2915 if (!nesting_depth)
2916 return;
2917 break;
2918
2919 case CPP_CLOSE_BRACE:
2920 /* If this is a non-nested '}', stop before consuming it.
2921 That way, when confronted with something like:
2922
2923 { 3 + }
2924
2925 we stop before consuming the closing '}', even though we
2926 have not yet reached a `;'. */
2927 if (nesting_depth == 0)
2928 return;
2929
2930 /* If it is the closing '}' for a block that we have
2931 scanned, stop -- but only after consuming the token.
2932 That way given:
2933
2934 void f g () { ... }
2935 typedef int I;
2936
2937 we will stop after the body of the erroneously declared
2938 function, but before consuming the following `typedef'
2939 declaration. */
2940 if (--nesting_depth == 0)
2941 {
2942 cp_lexer_consume_token (parser->lexer);
2943 return;
2944 }
2945
2946 case CPP_OPEN_BRACE:
2947 ++nesting_depth;
2948 break;
2949
2950 default:
2951 break;
2952 }
2953
2954 /* Consume the token. */
2955 cp_lexer_consume_token (parser->lexer);
2956 }
2957 }
2958
2959 /* This function is called at the end of a statement or declaration.
2960 If the next token is a semicolon, it is consumed; otherwise, error
2961 recovery is attempted. */
2962
2963 static void
2964 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2965 {
2966 /* Look for the trailing `;'. */
2967 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2968 {
2969 /* If there is additional (erroneous) input, skip to the end of
2970 the statement. */
2971 cp_parser_skip_to_end_of_statement (parser);
2972 /* If the next token is now a `;', consume it. */
2973 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2974 cp_lexer_consume_token (parser->lexer);
2975 }
2976 }
2977
2978 /* Skip tokens until we have consumed an entire block, or until we
2979 have consumed a non-nested `;'. */
2980
2981 static void
2982 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2983 {
2984 int nesting_depth = 0;
2985
2986 while (nesting_depth >= 0)
2987 {
2988 cp_token *token = cp_lexer_peek_token (parser->lexer);
2989
2990 switch (token->type)
2991 {
2992 case CPP_EOF:
2993 case CPP_PRAGMA_EOL:
2994 /* If we've run out of tokens, stop. */
2995 return;
2996
2997 case CPP_SEMICOLON:
2998 /* Stop if this is an unnested ';'. */
2999 if (!nesting_depth)
3000 nesting_depth = -1;
3001 break;
3002
3003 case CPP_CLOSE_BRACE:
3004 /* Stop if this is an unnested '}', or closes the outermost
3005 nesting level. */
3006 nesting_depth--;
3007 if (nesting_depth < 0)
3008 return;
3009 if (!nesting_depth)
3010 nesting_depth = -1;
3011 break;
3012
3013 case CPP_OPEN_BRACE:
3014 /* Nest. */
3015 nesting_depth++;
3016 break;
3017
3018 default:
3019 break;
3020 }
3021
3022 /* Consume the token. */
3023 cp_lexer_consume_token (parser->lexer);
3024 }
3025 }
3026
3027 /* Skip tokens until a non-nested closing curly brace is the next
3028 token, or there are no more tokens. Return true in the first case,
3029 false otherwise. */
3030
3031 static bool
3032 cp_parser_skip_to_closing_brace (cp_parser *parser)
3033 {
3034 unsigned nesting_depth = 0;
3035
3036 while (true)
3037 {
3038 cp_token *token = cp_lexer_peek_token (parser->lexer);
3039
3040 switch (token->type)
3041 {
3042 case CPP_EOF:
3043 case CPP_PRAGMA_EOL:
3044 /* If we've run out of tokens, stop. */
3045 return false;
3046
3047 case CPP_CLOSE_BRACE:
3048 /* If the next token is a non-nested `}', then we have reached
3049 the end of the current block. */
3050 if (nesting_depth-- == 0)
3051 return true;
3052 break;
3053
3054 case CPP_OPEN_BRACE:
3055 /* If it the next token is a `{', then we are entering a new
3056 block. Consume the entire block. */
3057 ++nesting_depth;
3058 break;
3059
3060 default:
3061 break;
3062 }
3063
3064 /* Consume the token. */
3065 cp_lexer_consume_token (parser->lexer);
3066 }
3067 }
3068
3069 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3070 parameter is the PRAGMA token, allowing us to purge the entire pragma
3071 sequence. */
3072
3073 static void
3074 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3075 {
3076 cp_token *token;
3077
3078 parser->lexer->in_pragma = false;
3079
3080 do
3081 token = cp_lexer_consume_token (parser->lexer);
3082 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3083
3084 /* Ensure that the pragma is not parsed again. */
3085 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3086 }
3087
3088 /* Require pragma end of line, resyncing with it as necessary. The
3089 arguments are as for cp_parser_skip_to_pragma_eol. */
3090
3091 static void
3092 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3093 {
3094 parser->lexer->in_pragma = false;
3095 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3096 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3097 }
3098
3099 /* This is a simple wrapper around make_typename_type. When the id is
3100 an unresolved identifier node, we can provide a superior diagnostic
3101 using cp_parser_diagnose_invalid_type_name. */
3102
3103 static tree
3104 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3105 tree id, location_t id_location)
3106 {
3107 tree result;
3108 if (TREE_CODE (id) == IDENTIFIER_NODE)
3109 {
3110 result = make_typename_type (scope, id, typename_type,
3111 /*complain=*/tf_none);
3112 if (result == error_mark_node)
3113 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3114 return result;
3115 }
3116 return make_typename_type (scope, id, typename_type, tf_error);
3117 }
3118
3119 /* This is a wrapper around the
3120 make_{pointer,ptrmem,reference}_declarator functions that decides
3121 which one to call based on the CODE and CLASS_TYPE arguments. The
3122 CODE argument should be one of the values returned by
3123 cp_parser_ptr_operator. */
3124 static cp_declarator *
3125 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3126 cp_cv_quals cv_qualifiers,
3127 cp_declarator *target)
3128 {
3129 if (code == ERROR_MARK)
3130 return cp_error_declarator;
3131
3132 if (code == INDIRECT_REF)
3133 if (class_type == NULL_TREE)
3134 return make_pointer_declarator (cv_qualifiers, target);
3135 else
3136 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3137 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3138 return make_reference_declarator (cv_qualifiers, target, false);
3139 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3140 return make_reference_declarator (cv_qualifiers, target, true);
3141 gcc_unreachable ();
3142 }
3143
3144 /* Create a new C++ parser. */
3145
3146 static cp_parser *
3147 cp_parser_new (void)
3148 {
3149 cp_parser *parser;
3150 cp_lexer *lexer;
3151 unsigned i;
3152
3153 /* cp_lexer_new_main is called before doing GC allocation because
3154 cp_lexer_new_main might load a PCH file. */
3155 lexer = cp_lexer_new_main ();
3156
3157 /* Initialize the binops_by_token so that we can get the tree
3158 directly from the token. */
3159 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3160 binops_by_token[binops[i].token_type] = binops[i];
3161
3162 parser = ggc_alloc_cleared_cp_parser ();
3163 parser->lexer = lexer;
3164 parser->context = cp_parser_context_new (NULL);
3165
3166 /* For now, we always accept GNU extensions. */
3167 parser->allow_gnu_extensions_p = 1;
3168
3169 /* The `>' token is a greater-than operator, not the end of a
3170 template-id. */
3171 parser->greater_than_is_operator_p = true;
3172
3173 parser->default_arg_ok_p = true;
3174
3175 /* We are not parsing a constant-expression. */
3176 parser->integral_constant_expression_p = false;
3177 parser->allow_non_integral_constant_expression_p = false;
3178 parser->non_integral_constant_expression_p = false;
3179
3180 /* Local variable names are not forbidden. */
3181 parser->local_variables_forbidden_p = false;
3182
3183 /* We are not processing an `extern "C"' declaration. */
3184 parser->in_unbraced_linkage_specification_p = false;
3185
3186 /* We are not processing a declarator. */
3187 parser->in_declarator_p = false;
3188
3189 /* We are not processing a template-argument-list. */
3190 parser->in_template_argument_list_p = false;
3191
3192 /* We are not in an iteration statement. */
3193 parser->in_statement = 0;
3194
3195 /* We are not in a switch statement. */
3196 parser->in_switch_statement_p = false;
3197
3198 /* We are not parsing a type-id inside an expression. */
3199 parser->in_type_id_in_expr_p = false;
3200
3201 /* Declarations aren't implicitly extern "C". */
3202 parser->implicit_extern_c = false;
3203
3204 /* String literals should be translated to the execution character set. */
3205 parser->translate_strings_p = true;
3206
3207 /* We are not parsing a function body. */
3208 parser->in_function_body = false;
3209
3210 /* The unparsed function queue is empty. */
3211 push_unparsed_function_queues (parser);
3212
3213 /* There are no classes being defined. */
3214 parser->num_classes_being_defined = 0;
3215
3216 /* No template parameters apply. */
3217 parser->num_template_parameter_lists = 0;
3218
3219 return parser;
3220 }
3221
3222 /* Create a cp_lexer structure which will emit the tokens in CACHE
3223 and push it onto the parser's lexer stack. This is used for delayed
3224 parsing of in-class method bodies and default arguments, and should
3225 not be confused with tentative parsing. */
3226 static void
3227 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3228 {
3229 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3230 lexer->next = parser->lexer;
3231 parser->lexer = lexer;
3232
3233 /* Move the current source position to that of the first token in the
3234 new lexer. */
3235 cp_lexer_set_source_position_from_token (lexer->next_token);
3236 }
3237
3238 /* Pop the top lexer off the parser stack. This is never used for the
3239 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3240 static void
3241 cp_parser_pop_lexer (cp_parser *parser)
3242 {
3243 cp_lexer *lexer = parser->lexer;
3244 parser->lexer = lexer->next;
3245 cp_lexer_destroy (lexer);
3246
3247 /* Put the current source position back where it was before this
3248 lexer was pushed. */
3249 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3250 }
3251
3252 /* Lexical conventions [gram.lex] */
3253
3254 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3255 identifier. */
3256
3257 static tree
3258 cp_parser_identifier (cp_parser* parser)
3259 {
3260 cp_token *token;
3261
3262 /* Look for the identifier. */
3263 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3264 /* Return the value. */
3265 return token ? token->u.value : error_mark_node;
3266 }
3267
3268 /* Parse a sequence of adjacent string constants. Returns a
3269 TREE_STRING representing the combined, nul-terminated string
3270 constant. If TRANSLATE is true, translate the string to the
3271 execution character set. If WIDE_OK is true, a wide string is
3272 invalid here.
3273
3274 C++98 [lex.string] says that if a narrow string literal token is
3275 adjacent to a wide string literal token, the behavior is undefined.
3276 However, C99 6.4.5p4 says that this results in a wide string literal.
3277 We follow C99 here, for consistency with the C front end.
3278
3279 This code is largely lifted from lex_string() in c-lex.c.
3280
3281 FUTURE: ObjC++ will need to handle @-strings here. */
3282 static tree
3283 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3284 {
3285 tree value;
3286 size_t count;
3287 struct obstack str_ob;
3288 cpp_string str, istr, *strs;
3289 cp_token *tok;
3290 enum cpp_ttype type;
3291
3292 tok = cp_lexer_peek_token (parser->lexer);
3293 if (!cp_parser_is_string_literal (tok))
3294 {
3295 cp_parser_error (parser, "expected string-literal");
3296 return error_mark_node;
3297 }
3298
3299 type = tok->type;
3300
3301 /* Try to avoid the overhead of creating and destroying an obstack
3302 for the common case of just one string. */
3303 if (!cp_parser_is_string_literal
3304 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3305 {
3306 cp_lexer_consume_token (parser->lexer);
3307
3308 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3309 str.len = TREE_STRING_LENGTH (tok->u.value);
3310 count = 1;
3311
3312 strs = &str;
3313 }
3314 else
3315 {
3316 gcc_obstack_init (&str_ob);
3317 count = 0;
3318
3319 do
3320 {
3321 cp_lexer_consume_token (parser->lexer);
3322 count++;
3323 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3324 str.len = TREE_STRING_LENGTH (tok->u.value);
3325
3326 if (type != tok->type)
3327 {
3328 if (type == CPP_STRING)
3329 type = tok->type;
3330 else if (tok->type != CPP_STRING)
3331 error_at (tok->location,
3332 "unsupported non-standard concatenation "
3333 "of string literals");
3334 }
3335
3336 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3337
3338 tok = cp_lexer_peek_token (parser->lexer);
3339 }
3340 while (cp_parser_is_string_literal (tok));
3341
3342 strs = (cpp_string *) obstack_finish (&str_ob);
3343 }
3344
3345 if (type != CPP_STRING && !wide_ok)
3346 {
3347 cp_parser_error (parser, "a wide string is invalid in this context");
3348 type = CPP_STRING;
3349 }
3350
3351 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3352 (parse_in, strs, count, &istr, type))
3353 {
3354 value = build_string (istr.len, (const char *)istr.text);
3355 free (CONST_CAST (unsigned char *, istr.text));
3356
3357 switch (type)
3358 {
3359 default:
3360 case CPP_STRING:
3361 case CPP_UTF8STRING:
3362 TREE_TYPE (value) = char_array_type_node;
3363 break;
3364 case CPP_STRING16:
3365 TREE_TYPE (value) = char16_array_type_node;
3366 break;
3367 case CPP_STRING32:
3368 TREE_TYPE (value) = char32_array_type_node;
3369 break;
3370 case CPP_WSTRING:
3371 TREE_TYPE (value) = wchar_array_type_node;
3372 break;
3373 }
3374
3375 value = fix_string_type (value);
3376 }
3377 else
3378 /* cpp_interpret_string has issued an error. */
3379 value = error_mark_node;
3380
3381 if (count > 1)
3382 obstack_free (&str_ob, 0);
3383
3384 return value;
3385 }
3386
3387
3388 /* Basic concepts [gram.basic] */
3389
3390 /* Parse a translation-unit.
3391
3392 translation-unit:
3393 declaration-seq [opt]
3394
3395 Returns TRUE if all went well. */
3396
3397 static bool
3398 cp_parser_translation_unit (cp_parser* parser)
3399 {
3400 /* The address of the first non-permanent object on the declarator
3401 obstack. */
3402 static void *declarator_obstack_base;
3403
3404 bool success;
3405
3406 /* Create the declarator obstack, if necessary. */
3407 if (!cp_error_declarator)
3408 {
3409 gcc_obstack_init (&declarator_obstack);
3410 /* Create the error declarator. */
3411 cp_error_declarator = make_declarator (cdk_error);
3412 /* Create the empty parameter list. */
3413 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3414 /* Remember where the base of the declarator obstack lies. */
3415 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3416 }
3417
3418 cp_parser_declaration_seq_opt (parser);
3419
3420 /* If there are no tokens left then all went well. */
3421 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3422 {
3423 /* Get rid of the token array; we don't need it any more. */
3424 cp_lexer_destroy (parser->lexer);
3425 parser->lexer = NULL;
3426
3427 /* This file might have been a context that's implicitly extern
3428 "C". If so, pop the lang context. (Only relevant for PCH.) */
3429 if (parser->implicit_extern_c)
3430 {
3431 pop_lang_context ();
3432 parser->implicit_extern_c = false;
3433 }
3434
3435 /* Finish up. */
3436 finish_translation_unit ();
3437
3438 success = true;
3439 }
3440 else
3441 {
3442 cp_parser_error (parser, "expected declaration");
3443 success = false;
3444 }
3445
3446 /* Make sure the declarator obstack was fully cleaned up. */
3447 gcc_assert (obstack_next_free (&declarator_obstack)
3448 == declarator_obstack_base);
3449
3450 /* All went well. */
3451 return success;
3452 }
3453
3454 /* Expressions [gram.expr] */
3455
3456 /* Parse a primary-expression.
3457
3458 primary-expression:
3459 literal
3460 this
3461 ( expression )
3462 id-expression
3463
3464 GNU Extensions:
3465
3466 primary-expression:
3467 ( compound-statement )
3468 __builtin_va_arg ( assignment-expression , type-id )
3469 __builtin_offsetof ( type-id , offsetof-expression )
3470
3471 C++ Extensions:
3472 __has_nothrow_assign ( type-id )
3473 __has_nothrow_constructor ( type-id )
3474 __has_nothrow_copy ( type-id )
3475 __has_trivial_assign ( type-id )
3476 __has_trivial_constructor ( type-id )
3477 __has_trivial_copy ( type-id )
3478 __has_trivial_destructor ( type-id )
3479 __has_virtual_destructor ( type-id )
3480 __is_abstract ( type-id )
3481 __is_base_of ( type-id , type-id )
3482 __is_class ( type-id )
3483 __is_convertible_to ( type-id , type-id )
3484 __is_empty ( type-id )
3485 __is_enum ( type-id )
3486 __is_pod ( type-id )
3487 __is_polymorphic ( type-id )
3488 __is_union ( type-id )
3489
3490 Objective-C++ Extension:
3491
3492 primary-expression:
3493 objc-expression
3494
3495 literal:
3496 __null
3497
3498 ADDRESS_P is true iff this expression was immediately preceded by
3499 "&" and therefore might denote a pointer-to-member. CAST_P is true
3500 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3501 true iff this expression is a template argument.
3502
3503 Returns a representation of the expression. Upon return, *IDK
3504 indicates what kind of id-expression (if any) was present. */
3505
3506 static tree
3507 cp_parser_primary_expression (cp_parser *parser,
3508 bool address_p,
3509 bool cast_p,
3510 bool template_arg_p,
3511 cp_id_kind *idk)
3512 {
3513 cp_token *token = NULL;
3514
3515 /* Assume the primary expression is not an id-expression. */
3516 *idk = CP_ID_KIND_NONE;
3517
3518 /* Peek at the next token. */
3519 token = cp_lexer_peek_token (parser->lexer);
3520 switch (token->type)
3521 {
3522 /* literal:
3523 integer-literal
3524 character-literal
3525 floating-literal
3526 string-literal
3527 boolean-literal */
3528 case CPP_CHAR:
3529 case CPP_CHAR16:
3530 case CPP_CHAR32:
3531 case CPP_WCHAR:
3532 case CPP_NUMBER:
3533 token = cp_lexer_consume_token (parser->lexer);
3534 if (TREE_CODE (token->u.value) == FIXED_CST)
3535 {
3536 error_at (token->location,
3537 "fixed-point types not supported in C++");
3538 return error_mark_node;
3539 }
3540 /* Floating-point literals are only allowed in an integral
3541 constant expression if they are cast to an integral or
3542 enumeration type. */
3543 if (TREE_CODE (token->u.value) == REAL_CST
3544 && parser->integral_constant_expression_p
3545 && pedantic)
3546 {
3547 /* CAST_P will be set even in invalid code like "int(2.7 +
3548 ...)". Therefore, we have to check that the next token
3549 is sure to end the cast. */
3550 if (cast_p)
3551 {
3552 cp_token *next_token;
3553
3554 next_token = cp_lexer_peek_token (parser->lexer);
3555 if (/* The comma at the end of an
3556 enumerator-definition. */
3557 next_token->type != CPP_COMMA
3558 /* The curly brace at the end of an enum-specifier. */
3559 && next_token->type != CPP_CLOSE_BRACE
3560 /* The end of a statement. */
3561 && next_token->type != CPP_SEMICOLON
3562 /* The end of the cast-expression. */
3563 && next_token->type != CPP_CLOSE_PAREN
3564 /* The end of an array bound. */
3565 && next_token->type != CPP_CLOSE_SQUARE
3566 /* The closing ">" in a template-argument-list. */
3567 && (next_token->type != CPP_GREATER
3568 || parser->greater_than_is_operator_p)
3569 /* C++0x only: A ">>" treated like two ">" tokens,
3570 in a template-argument-list. */
3571 && (next_token->type != CPP_RSHIFT
3572 || (cxx_dialect == cxx98)
3573 || parser->greater_than_is_operator_p))
3574 cast_p = false;
3575 }
3576
3577 /* If we are within a cast, then the constraint that the
3578 cast is to an integral or enumeration type will be
3579 checked at that point. If we are not within a cast, then
3580 this code is invalid. */
3581 if (!cast_p)
3582 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3583 }
3584 return token->u.value;
3585
3586 case CPP_STRING:
3587 case CPP_STRING16:
3588 case CPP_STRING32:
3589 case CPP_WSTRING:
3590 case CPP_UTF8STRING:
3591 /* ??? Should wide strings be allowed when parser->translate_strings_p
3592 is false (i.e. in attributes)? If not, we can kill the third
3593 argument to cp_parser_string_literal. */
3594 return cp_parser_string_literal (parser,
3595 parser->translate_strings_p,
3596 true);
3597
3598 case CPP_OPEN_PAREN:
3599 {
3600 tree expr;
3601 bool saved_greater_than_is_operator_p;
3602
3603 /* Consume the `('. */
3604 cp_lexer_consume_token (parser->lexer);
3605 /* Within a parenthesized expression, a `>' token is always
3606 the greater-than operator. */
3607 saved_greater_than_is_operator_p
3608 = parser->greater_than_is_operator_p;
3609 parser->greater_than_is_operator_p = true;
3610 /* If we see `( { ' then we are looking at the beginning of
3611 a GNU statement-expression. */
3612 if (cp_parser_allow_gnu_extensions_p (parser)
3613 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3614 {
3615 /* Statement-expressions are not allowed by the standard. */
3616 pedwarn (token->location, OPT_pedantic,
3617 "ISO C++ forbids braced-groups within expressions");
3618
3619 /* And they're not allowed outside of a function-body; you
3620 cannot, for example, write:
3621
3622 int i = ({ int j = 3; j + 1; });
3623
3624 at class or namespace scope. */
3625 if (!parser->in_function_body
3626 || parser->in_template_argument_list_p)
3627 {
3628 error_at (token->location,
3629 "statement-expressions are not allowed outside "
3630 "functions nor in template-argument lists");
3631 cp_parser_skip_to_end_of_block_or_statement (parser);
3632 expr = error_mark_node;
3633 }
3634 else
3635 {
3636 /* Start the statement-expression. */
3637 expr = begin_stmt_expr ();
3638 /* Parse the compound-statement. */
3639 cp_parser_compound_statement (parser, expr, false);
3640 /* Finish up. */
3641 expr = finish_stmt_expr (expr, false);
3642 }
3643 }
3644 else
3645 {
3646 /* Parse the parenthesized expression. */
3647 expr = cp_parser_expression (parser, cast_p, idk);
3648 /* Let the front end know that this expression was
3649 enclosed in parentheses. This matters in case, for
3650 example, the expression is of the form `A::B', since
3651 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3652 not. */
3653 finish_parenthesized_expr (expr);
3654 }
3655 /* The `>' token might be the end of a template-id or
3656 template-parameter-list now. */
3657 parser->greater_than_is_operator_p
3658 = saved_greater_than_is_operator_p;
3659 /* Consume the `)'. */
3660 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3661 cp_parser_skip_to_end_of_statement (parser);
3662
3663 return expr;
3664 }
3665
3666 case CPP_OPEN_SQUARE:
3667 if (c_dialect_objc ())
3668 /* We have an Objective-C++ message. */
3669 return cp_parser_objc_expression (parser);
3670 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3671 return cp_parser_lambda_expression (parser);
3672
3673 case CPP_OBJC_STRING:
3674 if (c_dialect_objc ())
3675 /* We have an Objective-C++ string literal. */
3676 return cp_parser_objc_expression (parser);
3677 cp_parser_error (parser, "expected primary-expression");
3678 return error_mark_node;
3679
3680 case CPP_KEYWORD:
3681 switch (token->keyword)
3682 {
3683 /* These two are the boolean literals. */
3684 case RID_TRUE:
3685 cp_lexer_consume_token (parser->lexer);
3686 return boolean_true_node;
3687 case RID_FALSE:
3688 cp_lexer_consume_token (parser->lexer);
3689 return boolean_false_node;
3690
3691 /* The `__null' literal. */
3692 case RID_NULL:
3693 cp_lexer_consume_token (parser->lexer);
3694 return null_node;
3695
3696 /* The `nullptr' literal. */
3697 case RID_NULLPTR:
3698 cp_lexer_consume_token (parser->lexer);
3699 return nullptr_node;
3700
3701 /* Recognize the `this' keyword. */
3702 case RID_THIS:
3703 cp_lexer_consume_token (parser->lexer);
3704 if (parser->local_variables_forbidden_p)
3705 {
3706 error_at (token->location,
3707 "%<this%> may not be used in this context");
3708 return error_mark_node;
3709 }
3710 /* Pointers cannot appear in constant-expressions. */
3711 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3712 return error_mark_node;
3713 return finish_this_expr ();
3714
3715 /* The `operator' keyword can be the beginning of an
3716 id-expression. */
3717 case RID_OPERATOR:
3718 goto id_expression;
3719
3720 case RID_FUNCTION_NAME:
3721 case RID_PRETTY_FUNCTION_NAME:
3722 case RID_C99_FUNCTION_NAME:
3723 {
3724 non_integral_constant name;
3725
3726 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3727 __func__ are the names of variables -- but they are
3728 treated specially. Therefore, they are handled here,
3729 rather than relying on the generic id-expression logic
3730 below. Grammatically, these names are id-expressions.
3731
3732 Consume the token. */
3733 token = cp_lexer_consume_token (parser->lexer);
3734
3735 switch (token->keyword)
3736 {
3737 case RID_FUNCTION_NAME:
3738 name = NIC_FUNC_NAME;
3739 break;
3740 case RID_PRETTY_FUNCTION_NAME:
3741 name = NIC_PRETTY_FUNC;
3742 break;
3743 case RID_C99_FUNCTION_NAME:
3744 name = NIC_C99_FUNC;
3745 break;
3746 default:
3747 gcc_unreachable ();
3748 }
3749
3750 if (cp_parser_non_integral_constant_expression (parser, name))
3751 return error_mark_node;
3752
3753 /* Look up the name. */
3754 return finish_fname (token->u.value);
3755 }
3756
3757 case RID_VA_ARG:
3758 {
3759 tree expression;
3760 tree type;
3761
3762 /* The `__builtin_va_arg' construct is used to handle
3763 `va_arg'. Consume the `__builtin_va_arg' token. */
3764 cp_lexer_consume_token (parser->lexer);
3765 /* Look for the opening `('. */
3766 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3767 /* Now, parse the assignment-expression. */
3768 expression = cp_parser_assignment_expression (parser,
3769 /*cast_p=*/false, NULL);
3770 /* Look for the `,'. */
3771 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3772 /* Parse the type-id. */
3773 type = cp_parser_type_id (parser);
3774 /* Look for the closing `)'. */
3775 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3776 /* Using `va_arg' in a constant-expression is not
3777 allowed. */
3778 if (cp_parser_non_integral_constant_expression (parser,
3779 NIC_VA_ARG))
3780 return error_mark_node;
3781 return build_x_va_arg (expression, type);
3782 }
3783
3784 case RID_OFFSETOF:
3785 return cp_parser_builtin_offsetof (parser);
3786
3787 case RID_HAS_NOTHROW_ASSIGN:
3788 case RID_HAS_NOTHROW_CONSTRUCTOR:
3789 case RID_HAS_NOTHROW_COPY:
3790 case RID_HAS_TRIVIAL_ASSIGN:
3791 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3792 case RID_HAS_TRIVIAL_COPY:
3793 case RID_HAS_TRIVIAL_DESTRUCTOR:
3794 case RID_HAS_VIRTUAL_DESTRUCTOR:
3795 case RID_IS_ABSTRACT:
3796 case RID_IS_BASE_OF:
3797 case RID_IS_CLASS:
3798 case RID_IS_CONVERTIBLE_TO:
3799 case RID_IS_EMPTY:
3800 case RID_IS_ENUM:
3801 case RID_IS_POD:
3802 case RID_IS_POLYMORPHIC:
3803 case RID_IS_STD_LAYOUT:
3804 case RID_IS_TRIVIAL:
3805 case RID_IS_UNION:
3806 return cp_parser_trait_expr (parser, token->keyword);
3807
3808 /* Objective-C++ expressions. */
3809 case RID_AT_ENCODE:
3810 case RID_AT_PROTOCOL:
3811 case RID_AT_SELECTOR:
3812 return cp_parser_objc_expression (parser);
3813
3814 case RID_TEMPLATE:
3815 if (parser->in_function_body
3816 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3817 == CPP_LESS))
3818 {
3819 error_at (token->location,
3820 "a template declaration cannot appear at block scope");
3821 cp_parser_skip_to_end_of_block_or_statement (parser);
3822 return error_mark_node;
3823 }
3824 default:
3825 cp_parser_error (parser, "expected primary-expression");
3826 return error_mark_node;
3827 }
3828
3829 /* An id-expression can start with either an identifier, a
3830 `::' as the beginning of a qualified-id, or the "operator"
3831 keyword. */
3832 case CPP_NAME:
3833 case CPP_SCOPE:
3834 case CPP_TEMPLATE_ID:
3835 case CPP_NESTED_NAME_SPECIFIER:
3836 {
3837 tree id_expression;
3838 tree decl;
3839 const char *error_msg;
3840 bool template_p;
3841 bool done;
3842 cp_token *id_expr_token;
3843
3844 id_expression:
3845 /* Parse the id-expression. */
3846 id_expression
3847 = cp_parser_id_expression (parser,
3848 /*template_keyword_p=*/false,
3849 /*check_dependency_p=*/true,
3850 &template_p,
3851 /*declarator_p=*/false,
3852 /*optional_p=*/false);
3853 if (id_expression == error_mark_node)
3854 return error_mark_node;
3855 id_expr_token = token;
3856 token = cp_lexer_peek_token (parser->lexer);
3857 done = (token->type != CPP_OPEN_SQUARE
3858 && token->type != CPP_OPEN_PAREN
3859 && token->type != CPP_DOT
3860 && token->type != CPP_DEREF
3861 && token->type != CPP_PLUS_PLUS
3862 && token->type != CPP_MINUS_MINUS);
3863 /* If we have a template-id, then no further lookup is
3864 required. If the template-id was for a template-class, we
3865 will sometimes have a TYPE_DECL at this point. */
3866 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3867 || TREE_CODE (id_expression) == TYPE_DECL)
3868 decl = id_expression;
3869 /* Look up the name. */
3870 else
3871 {
3872 tree ambiguous_decls;
3873
3874 /* If we already know that this lookup is ambiguous, then
3875 we've already issued an error message; there's no reason
3876 to check again. */
3877 if (id_expr_token->type == CPP_NAME
3878 && id_expr_token->ambiguous_p)
3879 {
3880 cp_parser_simulate_error (parser);
3881 return error_mark_node;
3882 }
3883
3884 decl = cp_parser_lookup_name (parser, id_expression,
3885 none_type,
3886 template_p,
3887 /*is_namespace=*/false,
3888 /*check_dependency=*/true,
3889 &ambiguous_decls,
3890 id_expr_token->location);
3891 /* If the lookup was ambiguous, an error will already have
3892 been issued. */
3893 if (ambiguous_decls)
3894 return error_mark_node;
3895
3896 /* In Objective-C++, an instance variable (ivar) may be preferred
3897 to whatever cp_parser_lookup_name() found. */
3898 decl = objc_lookup_ivar (decl, id_expression);
3899
3900 /* If name lookup gives us a SCOPE_REF, then the
3901 qualifying scope was dependent. */
3902 if (TREE_CODE (decl) == SCOPE_REF)
3903 {
3904 /* At this point, we do not know if DECL is a valid
3905 integral constant expression. We assume that it is
3906 in fact such an expression, so that code like:
3907
3908 template <int N> struct A {
3909 int a[B<N>::i];
3910 };
3911
3912 is accepted. At template-instantiation time, we
3913 will check that B<N>::i is actually a constant. */
3914 return decl;
3915 }
3916 /* Check to see if DECL is a local variable in a context
3917 where that is forbidden. */
3918 if (parser->local_variables_forbidden_p
3919 && local_variable_p (decl))
3920 {
3921 /* It might be that we only found DECL because we are
3922 trying to be generous with pre-ISO scoping rules.
3923 For example, consider:
3924
3925 int i;
3926 void g() {
3927 for (int i = 0; i < 10; ++i) {}
3928 extern void f(int j = i);
3929 }
3930
3931 Here, name look up will originally find the out
3932 of scope `i'. We need to issue a warning message,
3933 but then use the global `i'. */
3934 decl = check_for_out_of_scope_variable (decl);
3935 if (local_variable_p (decl))
3936 {
3937 error_at (id_expr_token->location,
3938 "local variable %qD may not appear in this context",
3939 decl);
3940 return error_mark_node;
3941 }
3942 }
3943 }
3944
3945 decl = (finish_id_expression
3946 (id_expression, decl, parser->scope,
3947 idk,
3948 parser->integral_constant_expression_p,
3949 parser->allow_non_integral_constant_expression_p,
3950 &parser->non_integral_constant_expression_p,
3951 template_p, done, address_p,
3952 template_arg_p,
3953 &error_msg,
3954 id_expr_token->location));
3955 if (error_msg)
3956 cp_parser_error (parser, error_msg);
3957 return decl;
3958 }
3959
3960 /* Anything else is an error. */
3961 default:
3962 cp_parser_error (parser, "expected primary-expression");
3963 return error_mark_node;
3964 }
3965 }
3966
3967 /* Parse an id-expression.
3968
3969 id-expression:
3970 unqualified-id
3971 qualified-id
3972
3973 qualified-id:
3974 :: [opt] nested-name-specifier template [opt] unqualified-id
3975 :: identifier
3976 :: operator-function-id
3977 :: template-id
3978
3979 Return a representation of the unqualified portion of the
3980 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3981 a `::' or nested-name-specifier.
3982
3983 Often, if the id-expression was a qualified-id, the caller will
3984 want to make a SCOPE_REF to represent the qualified-id. This
3985 function does not do this in order to avoid wastefully creating
3986 SCOPE_REFs when they are not required.
3987
3988 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3989 `template' keyword.
3990
3991 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3992 uninstantiated templates.
3993
3994 If *TEMPLATE_P is non-NULL, it is set to true iff the
3995 `template' keyword is used to explicitly indicate that the entity
3996 named is a template.
3997
3998 If DECLARATOR_P is true, the id-expression is appearing as part of
3999 a declarator, rather than as part of an expression. */
4000
4001 static tree
4002 cp_parser_id_expression (cp_parser *parser,
4003 bool template_keyword_p,
4004 bool check_dependency_p,
4005 bool *template_p,
4006 bool declarator_p,
4007 bool optional_p)
4008 {
4009 bool global_scope_p;
4010 bool nested_name_specifier_p;
4011
4012 /* Assume the `template' keyword was not used. */
4013 if (template_p)
4014 *template_p = template_keyword_p;
4015
4016 /* Look for the optional `::' operator. */
4017 global_scope_p
4018 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4019 != NULL_TREE);
4020 /* Look for the optional nested-name-specifier. */
4021 nested_name_specifier_p
4022 = (cp_parser_nested_name_specifier_opt (parser,
4023 /*typename_keyword_p=*/false,
4024 check_dependency_p,
4025 /*type_p=*/false,
4026 declarator_p)
4027 != NULL_TREE);
4028 /* If there is a nested-name-specifier, then we are looking at
4029 the first qualified-id production. */
4030 if (nested_name_specifier_p)
4031 {
4032 tree saved_scope;
4033 tree saved_object_scope;
4034 tree saved_qualifying_scope;
4035 tree unqualified_id;
4036 bool is_template;
4037
4038 /* See if the next token is the `template' keyword. */
4039 if (!template_p)
4040 template_p = &is_template;
4041 *template_p = cp_parser_optional_template_keyword (parser);
4042 /* Name lookup we do during the processing of the
4043 unqualified-id might obliterate SCOPE. */
4044 saved_scope = parser->scope;
4045 saved_object_scope = parser->object_scope;
4046 saved_qualifying_scope = parser->qualifying_scope;
4047 /* Process the final unqualified-id. */
4048 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4049 check_dependency_p,
4050 declarator_p,
4051 /*optional_p=*/false);
4052 /* Restore the SAVED_SCOPE for our caller. */
4053 parser->scope = saved_scope;
4054 parser->object_scope = saved_object_scope;
4055 parser->qualifying_scope = saved_qualifying_scope;
4056
4057 return unqualified_id;
4058 }
4059 /* Otherwise, if we are in global scope, then we are looking at one
4060 of the other qualified-id productions. */
4061 else if (global_scope_p)
4062 {
4063 cp_token *token;
4064 tree id;
4065
4066 /* Peek at the next token. */
4067 token = cp_lexer_peek_token (parser->lexer);
4068
4069 /* If it's an identifier, and the next token is not a "<", then
4070 we can avoid the template-id case. This is an optimization
4071 for this common case. */
4072 if (token->type == CPP_NAME
4073 && !cp_parser_nth_token_starts_template_argument_list_p
4074 (parser, 2))
4075 return cp_parser_identifier (parser);
4076
4077 cp_parser_parse_tentatively (parser);
4078 /* Try a template-id. */
4079 id = cp_parser_template_id (parser,
4080 /*template_keyword_p=*/false,
4081 /*check_dependency_p=*/true,
4082 declarator_p);
4083 /* If that worked, we're done. */
4084 if (cp_parser_parse_definitely (parser))
4085 return id;
4086
4087 /* Peek at the next token. (Changes in the token buffer may
4088 have invalidated the pointer obtained above.) */
4089 token = cp_lexer_peek_token (parser->lexer);
4090
4091 switch (token->type)
4092 {
4093 case CPP_NAME:
4094 return cp_parser_identifier (parser);
4095
4096 case CPP_KEYWORD:
4097 if (token->keyword == RID_OPERATOR)
4098 return cp_parser_operator_function_id (parser);
4099 /* Fall through. */
4100
4101 default:
4102 cp_parser_error (parser, "expected id-expression");
4103 return error_mark_node;
4104 }
4105 }
4106 else
4107 return cp_parser_unqualified_id (parser, template_keyword_p,
4108 /*check_dependency_p=*/true,
4109 declarator_p,
4110 optional_p);
4111 }
4112
4113 /* Parse an unqualified-id.
4114
4115 unqualified-id:
4116 identifier
4117 operator-function-id
4118 conversion-function-id
4119 ~ class-name
4120 template-id
4121
4122 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4123 keyword, in a construct like `A::template ...'.
4124
4125 Returns a representation of unqualified-id. For the `identifier'
4126 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4127 production a BIT_NOT_EXPR is returned; the operand of the
4128 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4129 other productions, see the documentation accompanying the
4130 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4131 names are looked up in uninstantiated templates. If DECLARATOR_P
4132 is true, the unqualified-id is appearing as part of a declarator,
4133 rather than as part of an expression. */
4134
4135 static tree
4136 cp_parser_unqualified_id (cp_parser* parser,
4137 bool template_keyword_p,
4138 bool check_dependency_p,
4139 bool declarator_p,
4140 bool optional_p)
4141 {
4142 cp_token *token;
4143
4144 /* Peek at the next token. */
4145 token = cp_lexer_peek_token (parser->lexer);
4146
4147 switch (token->type)
4148 {
4149 case CPP_NAME:
4150 {
4151 tree id;
4152
4153 /* We don't know yet whether or not this will be a
4154 template-id. */
4155 cp_parser_parse_tentatively (parser);
4156 /* Try a template-id. */
4157 id = cp_parser_template_id (parser, template_keyword_p,
4158 check_dependency_p,
4159 declarator_p);
4160 /* If it worked, we're done. */
4161 if (cp_parser_parse_definitely (parser))
4162 return id;
4163 /* Otherwise, it's an ordinary identifier. */
4164 return cp_parser_identifier (parser);
4165 }
4166
4167 case CPP_TEMPLATE_ID:
4168 return cp_parser_template_id (parser, template_keyword_p,
4169 check_dependency_p,
4170 declarator_p);
4171
4172 case CPP_COMPL:
4173 {
4174 tree type_decl;
4175 tree qualifying_scope;
4176 tree object_scope;
4177 tree scope;
4178 bool done;
4179
4180 /* Consume the `~' token. */
4181 cp_lexer_consume_token (parser->lexer);
4182 /* Parse the class-name. The standard, as written, seems to
4183 say that:
4184
4185 template <typename T> struct S { ~S (); };
4186 template <typename T> S<T>::~S() {}
4187
4188 is invalid, since `~' must be followed by a class-name, but
4189 `S<T>' is dependent, and so not known to be a class.
4190 That's not right; we need to look in uninstantiated
4191 templates. A further complication arises from:
4192
4193 template <typename T> void f(T t) {
4194 t.T::~T();
4195 }
4196
4197 Here, it is not possible to look up `T' in the scope of `T'
4198 itself. We must look in both the current scope, and the
4199 scope of the containing complete expression.
4200
4201 Yet another issue is:
4202
4203 struct S {
4204 int S;
4205 ~S();
4206 };
4207
4208 S::~S() {}
4209
4210 The standard does not seem to say that the `S' in `~S'
4211 should refer to the type `S' and not the data member
4212 `S::S'. */
4213
4214 /* DR 244 says that we look up the name after the "~" in the
4215 same scope as we looked up the qualifying name. That idea
4216 isn't fully worked out; it's more complicated than that. */
4217 scope = parser->scope;
4218 object_scope = parser->object_scope;
4219 qualifying_scope = parser->qualifying_scope;
4220
4221 /* Check for invalid scopes. */
4222 if (scope == error_mark_node)
4223 {
4224 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4225 cp_lexer_consume_token (parser->lexer);
4226 return error_mark_node;
4227 }
4228 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4229 {
4230 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4231 error_at (token->location,
4232 "scope %qT before %<~%> is not a class-name",
4233 scope);
4234 cp_parser_simulate_error (parser);
4235 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4236 cp_lexer_consume_token (parser->lexer);
4237 return error_mark_node;
4238 }
4239 gcc_assert (!scope || TYPE_P (scope));
4240
4241 /* If the name is of the form "X::~X" it's OK even if X is a
4242 typedef. */
4243 token = cp_lexer_peek_token (parser->lexer);
4244 if (scope
4245 && token->type == CPP_NAME
4246 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4247 != CPP_LESS)
4248 && (token->u.value == TYPE_IDENTIFIER (scope)
4249 || constructor_name_p (token->u.value, scope)))
4250 {
4251 cp_lexer_consume_token (parser->lexer);
4252 return build_nt (BIT_NOT_EXPR, scope);
4253 }
4254
4255 /* If there was an explicit qualification (S::~T), first look
4256 in the scope given by the qualification (i.e., S).
4257
4258 Note: in the calls to cp_parser_class_name below we pass
4259 typename_type so that lookup finds the injected-class-name
4260 rather than the constructor. */
4261 done = false;
4262 type_decl = NULL_TREE;
4263 if (scope)
4264 {
4265 cp_parser_parse_tentatively (parser);
4266 type_decl = cp_parser_class_name (parser,
4267 /*typename_keyword_p=*/false,
4268 /*template_keyword_p=*/false,
4269 typename_type,
4270 /*check_dependency=*/false,
4271 /*class_head_p=*/false,
4272 declarator_p);
4273 if (cp_parser_parse_definitely (parser))
4274 done = true;
4275 }
4276 /* In "N::S::~S", look in "N" as well. */
4277 if (!done && scope && qualifying_scope)
4278 {
4279 cp_parser_parse_tentatively (parser);
4280 parser->scope = qualifying_scope;
4281 parser->object_scope = NULL_TREE;
4282 parser->qualifying_scope = NULL_TREE;
4283 type_decl
4284 = cp_parser_class_name (parser,
4285 /*typename_keyword_p=*/false,
4286 /*template_keyword_p=*/false,
4287 typename_type,
4288 /*check_dependency=*/false,
4289 /*class_head_p=*/false,
4290 declarator_p);
4291 if (cp_parser_parse_definitely (parser))
4292 done = true;
4293 }
4294 /* In "p->S::~T", look in the scope given by "*p" as well. */
4295 else if (!done && object_scope)
4296 {
4297 cp_parser_parse_tentatively (parser);
4298 parser->scope = object_scope;
4299 parser->object_scope = NULL_TREE;
4300 parser->qualifying_scope = NULL_TREE;
4301 type_decl
4302 = cp_parser_class_name (parser,
4303 /*typename_keyword_p=*/false,
4304 /*template_keyword_p=*/false,
4305 typename_type,
4306 /*check_dependency=*/false,
4307 /*class_head_p=*/false,
4308 declarator_p);
4309 if (cp_parser_parse_definitely (parser))
4310 done = true;
4311 }
4312 /* Look in the surrounding context. */
4313 if (!done)
4314 {
4315 parser->scope = NULL_TREE;
4316 parser->object_scope = NULL_TREE;
4317 parser->qualifying_scope = NULL_TREE;
4318 if (processing_template_decl)
4319 cp_parser_parse_tentatively (parser);
4320 type_decl
4321 = cp_parser_class_name (parser,
4322 /*typename_keyword_p=*/false,
4323 /*template_keyword_p=*/false,
4324 typename_type,
4325 /*check_dependency=*/false,
4326 /*class_head_p=*/false,
4327 declarator_p);
4328 if (processing_template_decl
4329 && ! cp_parser_parse_definitely (parser))
4330 {
4331 /* We couldn't find a type with this name, so just accept
4332 it and check for a match at instantiation time. */
4333 type_decl = cp_parser_identifier (parser);
4334 if (type_decl != error_mark_node)
4335 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4336 return type_decl;
4337 }
4338 }
4339 /* If an error occurred, assume that the name of the
4340 destructor is the same as the name of the qualifying
4341 class. That allows us to keep parsing after running
4342 into ill-formed destructor names. */
4343 if (type_decl == error_mark_node && scope)
4344 return build_nt (BIT_NOT_EXPR, scope);
4345 else if (type_decl == error_mark_node)
4346 return error_mark_node;
4347
4348 /* Check that destructor name and scope match. */
4349 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4350 {
4351 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4352 error_at (token->location,
4353 "declaration of %<~%T%> as member of %qT",
4354 type_decl, scope);
4355 cp_parser_simulate_error (parser);
4356 return error_mark_node;
4357 }
4358
4359 /* [class.dtor]
4360
4361 A typedef-name that names a class shall not be used as the
4362 identifier in the declarator for a destructor declaration. */
4363 if (declarator_p
4364 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4365 && !DECL_SELF_REFERENCE_P (type_decl)
4366 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4367 error_at (token->location,
4368 "typedef-name %qD used as destructor declarator",
4369 type_decl);
4370
4371 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4372 }
4373
4374 case CPP_KEYWORD:
4375 if (token->keyword == RID_OPERATOR)
4376 {
4377 tree id;
4378
4379 /* This could be a template-id, so we try that first. */
4380 cp_parser_parse_tentatively (parser);
4381 /* Try a template-id. */
4382 id = cp_parser_template_id (parser, template_keyword_p,
4383 /*check_dependency_p=*/true,
4384 declarator_p);
4385 /* If that worked, we're done. */
4386 if (cp_parser_parse_definitely (parser))
4387 return id;
4388 /* We still don't know whether we're looking at an
4389 operator-function-id or a conversion-function-id. */
4390 cp_parser_parse_tentatively (parser);
4391 /* Try an operator-function-id. */
4392 id = cp_parser_operator_function_id (parser);
4393 /* If that didn't work, try a conversion-function-id. */
4394 if (!cp_parser_parse_definitely (parser))
4395 id = cp_parser_conversion_function_id (parser);
4396
4397 return id;
4398 }
4399 /* Fall through. */
4400
4401 default:
4402 if (optional_p)
4403 return NULL_TREE;
4404 cp_parser_error (parser, "expected unqualified-id");
4405 return error_mark_node;
4406 }
4407 }
4408
4409 /* Parse an (optional) nested-name-specifier.
4410
4411 nested-name-specifier: [C++98]
4412 class-or-namespace-name :: nested-name-specifier [opt]
4413 class-or-namespace-name :: template nested-name-specifier [opt]
4414
4415 nested-name-specifier: [C++0x]
4416 type-name ::
4417 namespace-name ::
4418 nested-name-specifier identifier ::
4419 nested-name-specifier template [opt] simple-template-id ::
4420
4421 PARSER->SCOPE should be set appropriately before this function is
4422 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4423 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4424 in name lookups.
4425
4426 Sets PARSER->SCOPE to the class (TYPE) or namespace
4427 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4428 it unchanged if there is no nested-name-specifier. Returns the new
4429 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4430
4431 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4432 part of a declaration and/or decl-specifier. */
4433
4434 static tree
4435 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4436 bool typename_keyword_p,
4437 bool check_dependency_p,
4438 bool type_p,
4439 bool is_declaration)
4440 {
4441 bool success = false;
4442 cp_token_position start = 0;
4443 cp_token *token;
4444
4445 /* Remember where the nested-name-specifier starts. */
4446 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4447 {
4448 start = cp_lexer_token_position (parser->lexer, false);
4449 push_deferring_access_checks (dk_deferred);
4450 }
4451
4452 while (true)
4453 {
4454 tree new_scope;
4455 tree old_scope;
4456 tree saved_qualifying_scope;
4457 bool template_keyword_p;
4458
4459 /* Spot cases that cannot be the beginning of a
4460 nested-name-specifier. */
4461 token = cp_lexer_peek_token (parser->lexer);
4462
4463 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4464 the already parsed nested-name-specifier. */
4465 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4466 {
4467 /* Grab the nested-name-specifier and continue the loop. */
4468 cp_parser_pre_parsed_nested_name_specifier (parser);
4469 /* If we originally encountered this nested-name-specifier
4470 with IS_DECLARATION set to false, we will not have
4471 resolved TYPENAME_TYPEs, so we must do so here. */
4472 if (is_declaration
4473 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4474 {
4475 new_scope = resolve_typename_type (parser->scope,
4476 /*only_current_p=*/false);
4477 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4478 parser->scope = new_scope;
4479 }
4480 success = true;
4481 continue;
4482 }
4483
4484 /* Spot cases that cannot be the beginning of a
4485 nested-name-specifier. On the second and subsequent times
4486 through the loop, we look for the `template' keyword. */
4487 if (success && token->keyword == RID_TEMPLATE)
4488 ;
4489 /* A template-id can start a nested-name-specifier. */
4490 else if (token->type == CPP_TEMPLATE_ID)
4491 ;
4492 else
4493 {
4494 /* If the next token is not an identifier, then it is
4495 definitely not a type-name or namespace-name. */
4496 if (token->type != CPP_NAME)
4497 break;
4498 /* If the following token is neither a `<' (to begin a
4499 template-id), nor a `::', then we are not looking at a
4500 nested-name-specifier. */
4501 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4502 if (token->type != CPP_SCOPE
4503 && !cp_parser_nth_token_starts_template_argument_list_p
4504 (parser, 2))
4505 break;
4506 }
4507
4508 /* The nested-name-specifier is optional, so we parse
4509 tentatively. */
4510 cp_parser_parse_tentatively (parser);
4511
4512 /* Look for the optional `template' keyword, if this isn't the
4513 first time through the loop. */
4514 if (success)
4515 template_keyword_p = cp_parser_optional_template_keyword (parser);
4516 else
4517 template_keyword_p = false;
4518
4519 /* Save the old scope since the name lookup we are about to do
4520 might destroy it. */
4521 old_scope = parser->scope;
4522 saved_qualifying_scope = parser->qualifying_scope;
4523 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4524 look up names in "X<T>::I" in order to determine that "Y" is
4525 a template. So, if we have a typename at this point, we make
4526 an effort to look through it. */
4527 if (is_declaration
4528 && !typename_keyword_p
4529 && parser->scope
4530 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4531 parser->scope = resolve_typename_type (parser->scope,
4532 /*only_current_p=*/false);
4533 /* Parse the qualifying entity. */
4534 new_scope
4535 = cp_parser_qualifying_entity (parser,
4536 typename_keyword_p,
4537 template_keyword_p,
4538 check_dependency_p,
4539 type_p,
4540 is_declaration);
4541 /* Look for the `::' token. */
4542 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4543
4544 /* If we found what we wanted, we keep going; otherwise, we're
4545 done. */
4546 if (!cp_parser_parse_definitely (parser))
4547 {
4548 bool error_p = false;
4549
4550 /* Restore the OLD_SCOPE since it was valid before the
4551 failed attempt at finding the last
4552 class-or-namespace-name. */
4553 parser->scope = old_scope;
4554 parser->qualifying_scope = saved_qualifying_scope;
4555 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4556 break;
4557 /* If the next token is an identifier, and the one after
4558 that is a `::', then any valid interpretation would have
4559 found a class-or-namespace-name. */
4560 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4561 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4562 == CPP_SCOPE)
4563 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4564 != CPP_COMPL))
4565 {
4566 token = cp_lexer_consume_token (parser->lexer);
4567 if (!error_p)
4568 {
4569 if (!token->ambiguous_p)
4570 {
4571 tree decl;
4572 tree ambiguous_decls;
4573
4574 decl = cp_parser_lookup_name (parser, token->u.value,
4575 none_type,
4576 /*is_template=*/false,
4577 /*is_namespace=*/false,
4578 /*check_dependency=*/true,
4579 &ambiguous_decls,
4580 token->location);
4581 if (TREE_CODE (decl) == TEMPLATE_DECL)
4582 error_at (token->location,
4583 "%qD used without template parameters",
4584 decl);
4585 else if (ambiguous_decls)
4586 {
4587 error_at (token->location,
4588 "reference to %qD is ambiguous",
4589 token->u.value);
4590 print_candidates (ambiguous_decls);
4591 decl = error_mark_node;
4592 }
4593 else
4594 {
4595 if (cxx_dialect != cxx98)
4596 cp_parser_name_lookup_error
4597 (parser, token->u.value, decl, NLE_NOT_CXX98,
4598 token->location);
4599 else
4600 cp_parser_name_lookup_error
4601 (parser, token->u.value, decl, NLE_CXX98,
4602 token->location);
4603 }
4604 }
4605 parser->scope = error_mark_node;
4606 error_p = true;
4607 /* Treat this as a successful nested-name-specifier
4608 due to:
4609
4610 [basic.lookup.qual]
4611
4612 If the name found is not a class-name (clause
4613 _class_) or namespace-name (_namespace.def_), the
4614 program is ill-formed. */
4615 success = true;
4616 }
4617 cp_lexer_consume_token (parser->lexer);
4618 }
4619 break;
4620 }
4621 /* We've found one valid nested-name-specifier. */
4622 success = true;
4623 /* Name lookup always gives us a DECL. */
4624 if (TREE_CODE (new_scope) == TYPE_DECL)
4625 new_scope = TREE_TYPE (new_scope);
4626 /* Uses of "template" must be followed by actual templates. */
4627 if (template_keyword_p
4628 && !(CLASS_TYPE_P (new_scope)
4629 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4630 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4631 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4632 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4633 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4634 == TEMPLATE_ID_EXPR)))
4635 permerror (input_location, TYPE_P (new_scope)
4636 ? "%qT is not a template"
4637 : "%qD is not a template",
4638 new_scope);
4639 /* If it is a class scope, try to complete it; we are about to
4640 be looking up names inside the class. */
4641 if (TYPE_P (new_scope)
4642 /* Since checking types for dependency can be expensive,
4643 avoid doing it if the type is already complete. */
4644 && !COMPLETE_TYPE_P (new_scope)
4645 /* Do not try to complete dependent types. */
4646 && !dependent_type_p (new_scope))
4647 {
4648 new_scope = complete_type (new_scope);
4649 /* If it is a typedef to current class, use the current
4650 class instead, as the typedef won't have any names inside
4651 it yet. */
4652 if (!COMPLETE_TYPE_P (new_scope)
4653 && currently_open_class (new_scope))
4654 new_scope = TYPE_MAIN_VARIANT (new_scope);
4655 }
4656 /* Make sure we look in the right scope the next time through
4657 the loop. */
4658 parser->scope = new_scope;
4659 }
4660
4661 /* If parsing tentatively, replace the sequence of tokens that makes
4662 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4663 token. That way, should we re-parse the token stream, we will
4664 not have to repeat the effort required to do the parse, nor will
4665 we issue duplicate error messages. */
4666 if (success && start)
4667 {
4668 cp_token *token;
4669
4670 token = cp_lexer_token_at (parser->lexer, start);
4671 /* Reset the contents of the START token. */
4672 token->type = CPP_NESTED_NAME_SPECIFIER;
4673 /* Retrieve any deferred checks. Do not pop this access checks yet
4674 so the memory will not be reclaimed during token replacing below. */
4675 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4676 token->u.tree_check_value->value = parser->scope;
4677 token->u.tree_check_value->checks = get_deferred_access_checks ();
4678 token->u.tree_check_value->qualifying_scope =
4679 parser->qualifying_scope;
4680 token->keyword = RID_MAX;
4681
4682 /* Purge all subsequent tokens. */
4683 cp_lexer_purge_tokens_after (parser->lexer, start);
4684 }
4685
4686 if (start)
4687 pop_to_parent_deferring_access_checks ();
4688
4689 return success ? parser->scope : NULL_TREE;
4690 }
4691
4692 /* Parse a nested-name-specifier. See
4693 cp_parser_nested_name_specifier_opt for details. This function
4694 behaves identically, except that it will an issue an error if no
4695 nested-name-specifier is present. */
4696
4697 static tree
4698 cp_parser_nested_name_specifier (cp_parser *parser,
4699 bool typename_keyword_p,
4700 bool check_dependency_p,
4701 bool type_p,
4702 bool is_declaration)
4703 {
4704 tree scope;
4705
4706 /* Look for the nested-name-specifier. */
4707 scope = cp_parser_nested_name_specifier_opt (parser,
4708 typename_keyword_p,
4709 check_dependency_p,
4710 type_p,
4711 is_declaration);
4712 /* If it was not present, issue an error message. */
4713 if (!scope)
4714 {
4715 cp_parser_error (parser, "expected nested-name-specifier");
4716 parser->scope = NULL_TREE;
4717 }
4718
4719 return scope;
4720 }
4721
4722 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4723 this is either a class-name or a namespace-name (which corresponds
4724 to the class-or-namespace-name production in the grammar). For
4725 C++0x, it can also be a type-name that refers to an enumeration
4726 type.
4727
4728 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4729 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4730 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4731 TYPE_P is TRUE iff the next name should be taken as a class-name,
4732 even the same name is declared to be another entity in the same
4733 scope.
4734
4735 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4736 specified by the class-or-namespace-name. If neither is found the
4737 ERROR_MARK_NODE is returned. */
4738
4739 static tree
4740 cp_parser_qualifying_entity (cp_parser *parser,
4741 bool typename_keyword_p,
4742 bool template_keyword_p,
4743 bool check_dependency_p,
4744 bool type_p,
4745 bool is_declaration)
4746 {
4747 tree saved_scope;
4748 tree saved_qualifying_scope;
4749 tree saved_object_scope;
4750 tree scope;
4751 bool only_class_p;
4752 bool successful_parse_p;
4753
4754 /* Before we try to parse the class-name, we must save away the
4755 current PARSER->SCOPE since cp_parser_class_name will destroy
4756 it. */
4757 saved_scope = parser->scope;
4758 saved_qualifying_scope = parser->qualifying_scope;
4759 saved_object_scope = parser->object_scope;
4760 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4761 there is no need to look for a namespace-name. */
4762 only_class_p = template_keyword_p
4763 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4764 if (!only_class_p)
4765 cp_parser_parse_tentatively (parser);
4766 scope = cp_parser_class_name (parser,
4767 typename_keyword_p,
4768 template_keyword_p,
4769 type_p ? class_type : none_type,
4770 check_dependency_p,
4771 /*class_head_p=*/false,
4772 is_declaration);
4773 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4774 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4775 if (!only_class_p
4776 && cxx_dialect != cxx98
4777 && !successful_parse_p)
4778 {
4779 /* Restore the saved scope. */
4780 parser->scope = saved_scope;
4781 parser->qualifying_scope = saved_qualifying_scope;
4782 parser->object_scope = saved_object_scope;
4783
4784 /* Parse tentatively. */
4785 cp_parser_parse_tentatively (parser);
4786
4787 /* Parse a typedef-name or enum-name. */
4788 scope = cp_parser_nonclass_name (parser);
4789
4790 /* "If the name found does not designate a namespace or a class,
4791 enumeration, or dependent type, the program is ill-formed."
4792
4793 We cover classes and dependent types above and namespaces below,
4794 so this code is only looking for enums. */
4795 if (!scope || TREE_CODE (scope) != TYPE_DECL
4796 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4797 cp_parser_simulate_error (parser);
4798
4799 successful_parse_p = cp_parser_parse_definitely (parser);
4800 }
4801 /* If that didn't work, try for a namespace-name. */
4802 if (!only_class_p && !successful_parse_p)
4803 {
4804 /* Restore the saved scope. */
4805 parser->scope = saved_scope;
4806 parser->qualifying_scope = saved_qualifying_scope;
4807 parser->object_scope = saved_object_scope;
4808 /* If we are not looking at an identifier followed by the scope
4809 resolution operator, then this is not part of a
4810 nested-name-specifier. (Note that this function is only used
4811 to parse the components of a nested-name-specifier.) */
4812 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4813 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4814 return error_mark_node;
4815 scope = cp_parser_namespace_name (parser);
4816 }
4817
4818 return scope;
4819 }
4820
4821 /* Parse a postfix-expression.
4822
4823 postfix-expression:
4824 primary-expression
4825 postfix-expression [ expression ]
4826 postfix-expression ( expression-list [opt] )
4827 simple-type-specifier ( expression-list [opt] )
4828 typename :: [opt] nested-name-specifier identifier
4829 ( expression-list [opt] )
4830 typename :: [opt] nested-name-specifier template [opt] template-id
4831 ( expression-list [opt] )
4832 postfix-expression . template [opt] id-expression
4833 postfix-expression -> template [opt] id-expression
4834 postfix-expression . pseudo-destructor-name
4835 postfix-expression -> pseudo-destructor-name
4836 postfix-expression ++
4837 postfix-expression --
4838 dynamic_cast < type-id > ( expression )
4839 static_cast < type-id > ( expression )
4840 reinterpret_cast < type-id > ( expression )
4841 const_cast < type-id > ( expression )
4842 typeid ( expression )
4843 typeid ( type-id )
4844
4845 GNU Extension:
4846
4847 postfix-expression:
4848 ( type-id ) { initializer-list , [opt] }
4849
4850 This extension is a GNU version of the C99 compound-literal
4851 construct. (The C99 grammar uses `type-name' instead of `type-id',
4852 but they are essentially the same concept.)
4853
4854 If ADDRESS_P is true, the postfix expression is the operand of the
4855 `&' operator. CAST_P is true if this expression is the target of a
4856 cast.
4857
4858 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4859 class member access expressions [expr.ref].
4860
4861 Returns a representation of the expression. */
4862
4863 static tree
4864 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4865 bool member_access_only_p,
4866 cp_id_kind * pidk_return)
4867 {
4868 cp_token *token;
4869 enum rid keyword;
4870 cp_id_kind idk = CP_ID_KIND_NONE;
4871 tree postfix_expression = NULL_TREE;
4872 bool is_member_access = false;
4873
4874 /* Peek at the next token. */
4875 token = cp_lexer_peek_token (parser->lexer);
4876 /* Some of the productions are determined by keywords. */
4877 keyword = token->keyword;
4878 switch (keyword)
4879 {
4880 case RID_DYNCAST:
4881 case RID_STATCAST:
4882 case RID_REINTCAST:
4883 case RID_CONSTCAST:
4884 {
4885 tree type;
4886 tree expression;
4887 const char *saved_message;
4888
4889 /* All of these can be handled in the same way from the point
4890 of view of parsing. Begin by consuming the token
4891 identifying the cast. */
4892 cp_lexer_consume_token (parser->lexer);
4893
4894 /* New types cannot be defined in the cast. */
4895 saved_message = parser->type_definition_forbidden_message;
4896 parser->type_definition_forbidden_message
4897 = G_("types may not be defined in casts");
4898
4899 /* Look for the opening `<'. */
4900 cp_parser_require (parser, CPP_LESS, RT_LESS);
4901 /* Parse the type to which we are casting. */
4902 type = cp_parser_type_id (parser);
4903 /* Look for the closing `>'. */
4904 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4905 /* Restore the old message. */
4906 parser->type_definition_forbidden_message = saved_message;
4907
4908 /* And the expression which is being cast. */
4909 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4910 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4911 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4912
4913 /* Only type conversions to integral or enumeration types
4914 can be used in constant-expressions. */
4915 if (!cast_valid_in_integral_constant_expression_p (type)
4916 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4917 return error_mark_node;
4918
4919 switch (keyword)
4920 {
4921 case RID_DYNCAST:
4922 postfix_expression
4923 = build_dynamic_cast (type, expression, tf_warning_or_error);
4924 break;
4925 case RID_STATCAST:
4926 postfix_expression
4927 = build_static_cast (type, expression, tf_warning_or_error);
4928 break;
4929 case RID_REINTCAST:
4930 postfix_expression
4931 = build_reinterpret_cast (type, expression,
4932 tf_warning_or_error);
4933 break;
4934 case RID_CONSTCAST:
4935 postfix_expression
4936 = build_const_cast (type, expression, tf_warning_or_error);
4937 break;
4938 default:
4939 gcc_unreachable ();
4940 }
4941 }
4942 break;
4943
4944 case RID_TYPEID:
4945 {
4946 tree type;
4947 const char *saved_message;
4948 bool saved_in_type_id_in_expr_p;
4949
4950 /* Consume the `typeid' token. */
4951 cp_lexer_consume_token (parser->lexer);
4952 /* Look for the `(' token. */
4953 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4954 /* Types cannot be defined in a `typeid' expression. */
4955 saved_message = parser->type_definition_forbidden_message;
4956 parser->type_definition_forbidden_message
4957 = G_("types may not be defined in a %<typeid%> expression");
4958 /* We can't be sure yet whether we're looking at a type-id or an
4959 expression. */
4960 cp_parser_parse_tentatively (parser);
4961 /* Try a type-id first. */
4962 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4963 parser->in_type_id_in_expr_p = true;
4964 type = cp_parser_type_id (parser);
4965 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4966 /* Look for the `)' token. Otherwise, we can't be sure that
4967 we're not looking at an expression: consider `typeid (int
4968 (3))', for example. */
4969 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4970 /* If all went well, simply lookup the type-id. */
4971 if (cp_parser_parse_definitely (parser))
4972 postfix_expression = get_typeid (type);
4973 /* Otherwise, fall back to the expression variant. */
4974 else
4975 {
4976 tree expression;
4977
4978 /* Look for an expression. */
4979 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4980 /* Compute its typeid. */
4981 postfix_expression = build_typeid (expression);
4982 /* Look for the `)' token. */
4983 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4984 }
4985 /* Restore the saved message. */
4986 parser->type_definition_forbidden_message = saved_message;
4987 /* `typeid' may not appear in an integral constant expression. */
4988 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4989 return error_mark_node;
4990 }
4991 break;
4992
4993 case RID_TYPENAME:
4994 {
4995 tree type;
4996 /* The syntax permitted here is the same permitted for an
4997 elaborated-type-specifier. */
4998 type = cp_parser_elaborated_type_specifier (parser,
4999 /*is_friend=*/false,
5000 /*is_declaration=*/false);
5001 postfix_expression = cp_parser_functional_cast (parser, type);
5002 }
5003 break;
5004
5005 default:
5006 {
5007 tree type;
5008
5009 /* If the next thing is a simple-type-specifier, we may be
5010 looking at a functional cast. We could also be looking at
5011 an id-expression. So, we try the functional cast, and if
5012 that doesn't work we fall back to the primary-expression. */
5013 cp_parser_parse_tentatively (parser);
5014 /* Look for the simple-type-specifier. */
5015 type = cp_parser_simple_type_specifier (parser,
5016 /*decl_specs=*/NULL,
5017 CP_PARSER_FLAGS_NONE);
5018 /* Parse the cast itself. */
5019 if (!cp_parser_error_occurred (parser))
5020 postfix_expression
5021 = cp_parser_functional_cast (parser, type);
5022 /* If that worked, we're done. */
5023 if (cp_parser_parse_definitely (parser))
5024 break;
5025
5026 /* If the functional-cast didn't work out, try a
5027 compound-literal. */
5028 if (cp_parser_allow_gnu_extensions_p (parser)
5029 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5030 {
5031 VEC(constructor_elt,gc) *initializer_list = NULL;
5032 bool saved_in_type_id_in_expr_p;
5033
5034 cp_parser_parse_tentatively (parser);
5035 /* Consume the `('. */
5036 cp_lexer_consume_token (parser->lexer);
5037 /* Parse the type. */
5038 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5039 parser->in_type_id_in_expr_p = true;
5040 type = cp_parser_type_id (parser);
5041 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5042 /* Look for the `)'. */
5043 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5044 /* Look for the `{'. */
5045 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5046 /* If things aren't going well, there's no need to
5047 keep going. */
5048 if (!cp_parser_error_occurred (parser))
5049 {
5050 bool non_constant_p;
5051 /* Parse the initializer-list. */
5052 initializer_list
5053 = cp_parser_initializer_list (parser, &non_constant_p);
5054 /* Allow a trailing `,'. */
5055 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5056 cp_lexer_consume_token (parser->lexer);
5057 /* Look for the final `}'. */
5058 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5059 }
5060 /* If that worked, we're definitely looking at a
5061 compound-literal expression. */
5062 if (cp_parser_parse_definitely (parser))
5063 {
5064 /* Warn the user that a compound literal is not
5065 allowed in standard C++. */
5066 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5067 /* For simplicity, we disallow compound literals in
5068 constant-expressions. We could
5069 allow compound literals of integer type, whose
5070 initializer was a constant, in constant
5071 expressions. Permitting that usage, as a further
5072 extension, would not change the meaning of any
5073 currently accepted programs. (Of course, as
5074 compound literals are not part of ISO C++, the
5075 standard has nothing to say.) */
5076 if (cp_parser_non_integral_constant_expression (parser,
5077 NIC_NCC))
5078 {
5079 postfix_expression = error_mark_node;
5080 break;
5081 }
5082 /* Form the representation of the compound-literal. */
5083 postfix_expression
5084 = (finish_compound_literal
5085 (type, build_constructor (init_list_type_node,
5086 initializer_list)));
5087 break;
5088 }
5089 }
5090
5091 /* It must be a primary-expression. */
5092 postfix_expression
5093 = cp_parser_primary_expression (parser, address_p, cast_p,
5094 /*template_arg_p=*/false,
5095 &idk);
5096 }
5097 break;
5098 }
5099
5100 /* Keep looping until the postfix-expression is complete. */
5101 while (true)
5102 {
5103 if (idk == CP_ID_KIND_UNQUALIFIED
5104 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5105 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5106 /* It is not a Koenig lookup function call. */
5107 postfix_expression
5108 = unqualified_name_lookup_error (postfix_expression);
5109
5110 /* Peek at the next token. */
5111 token = cp_lexer_peek_token (parser->lexer);
5112
5113 switch (token->type)
5114 {
5115 case CPP_OPEN_SQUARE:
5116 postfix_expression
5117 = cp_parser_postfix_open_square_expression (parser,
5118 postfix_expression,
5119 false);
5120 idk = CP_ID_KIND_NONE;
5121 is_member_access = false;
5122 break;
5123
5124 case CPP_OPEN_PAREN:
5125 /* postfix-expression ( expression-list [opt] ) */
5126 {
5127 bool koenig_p;
5128 bool is_builtin_constant_p;
5129 bool saved_integral_constant_expression_p = false;
5130 bool saved_non_integral_constant_expression_p = false;
5131 VEC(tree,gc) *args;
5132
5133 is_member_access = false;
5134
5135 is_builtin_constant_p
5136 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5137 if (is_builtin_constant_p)
5138 {
5139 /* The whole point of __builtin_constant_p is to allow
5140 non-constant expressions to appear as arguments. */
5141 saved_integral_constant_expression_p
5142 = parser->integral_constant_expression_p;
5143 saved_non_integral_constant_expression_p
5144 = parser->non_integral_constant_expression_p;
5145 parser->integral_constant_expression_p = false;
5146 }
5147 args = (cp_parser_parenthesized_expression_list
5148 (parser, non_attr,
5149 /*cast_p=*/false, /*allow_expansion_p=*/true,
5150 /*non_constant_p=*/NULL));
5151 if (is_builtin_constant_p)
5152 {
5153 parser->integral_constant_expression_p
5154 = saved_integral_constant_expression_p;
5155 parser->non_integral_constant_expression_p
5156 = saved_non_integral_constant_expression_p;
5157 }
5158
5159 if (args == NULL)
5160 {
5161 postfix_expression = error_mark_node;
5162 break;
5163 }
5164
5165 /* Function calls are not permitted in
5166 constant-expressions. */
5167 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5168 && cp_parser_non_integral_constant_expression (parser,
5169 NIC_FUNC_CALL))
5170 {
5171 postfix_expression = error_mark_node;
5172 release_tree_vector (args);
5173 break;
5174 }
5175
5176 koenig_p = false;
5177 if (idk == CP_ID_KIND_UNQUALIFIED
5178 || idk == CP_ID_KIND_TEMPLATE_ID)
5179 {
5180 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5181 {
5182 if (!VEC_empty (tree, args))
5183 {
5184 koenig_p = true;
5185 if (!any_type_dependent_arguments_p (args))
5186 postfix_expression
5187 = perform_koenig_lookup (postfix_expression, args,
5188 /*include_std=*/false);
5189 }
5190 else
5191 postfix_expression
5192 = unqualified_fn_lookup_error (postfix_expression);
5193 }
5194 /* We do not perform argument-dependent lookup if
5195 normal lookup finds a non-function, in accordance
5196 with the expected resolution of DR 218. */
5197 else if (!VEC_empty (tree, args)
5198 && is_overloaded_fn (postfix_expression))
5199 {
5200 tree fn = get_first_fn (postfix_expression);
5201 fn = STRIP_TEMPLATE (fn);
5202
5203 /* Do not do argument dependent lookup if regular
5204 lookup finds a member function or a block-scope
5205 function declaration. [basic.lookup.argdep]/3 */
5206 if (!DECL_FUNCTION_MEMBER_P (fn)
5207 && !DECL_LOCAL_FUNCTION_P (fn))
5208 {
5209 koenig_p = true;
5210 if (!any_type_dependent_arguments_p (args))
5211 postfix_expression
5212 = perform_koenig_lookup (postfix_expression, args,
5213 /*include_std=*/false);
5214 }
5215 }
5216 }
5217
5218 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5219 {
5220 tree instance = TREE_OPERAND (postfix_expression, 0);
5221 tree fn = TREE_OPERAND (postfix_expression, 1);
5222
5223 if (processing_template_decl
5224 && (type_dependent_expression_p (instance)
5225 || (!BASELINK_P (fn)
5226 && TREE_CODE (fn) != FIELD_DECL)
5227 || type_dependent_expression_p (fn)
5228 || any_type_dependent_arguments_p (args)))
5229 {
5230 postfix_expression
5231 = build_nt_call_vec (postfix_expression, args);
5232 release_tree_vector (args);
5233 break;
5234 }
5235
5236 if (BASELINK_P (fn))
5237 {
5238 postfix_expression
5239 = (build_new_method_call
5240 (instance, fn, &args, NULL_TREE,
5241 (idk == CP_ID_KIND_QUALIFIED
5242 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5243 /*fn_p=*/NULL,
5244 tf_warning_or_error));
5245 }
5246 else
5247 postfix_expression
5248 = finish_call_expr (postfix_expression, &args,
5249 /*disallow_virtual=*/false,
5250 /*koenig_p=*/false,
5251 tf_warning_or_error);
5252 }
5253 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5254 || TREE_CODE (postfix_expression) == MEMBER_REF
5255 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5256 postfix_expression = (build_offset_ref_call_from_tree
5257 (postfix_expression, &args));
5258 else if (idk == CP_ID_KIND_QUALIFIED)
5259 /* A call to a static class member, or a namespace-scope
5260 function. */
5261 postfix_expression
5262 = finish_call_expr (postfix_expression, &args,
5263 /*disallow_virtual=*/true,
5264 koenig_p,
5265 tf_warning_or_error);
5266 else
5267 /* All other function calls. */
5268 postfix_expression
5269 = finish_call_expr (postfix_expression, &args,
5270 /*disallow_virtual=*/false,
5271 koenig_p,
5272 tf_warning_or_error);
5273
5274 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5275 idk = CP_ID_KIND_NONE;
5276
5277 release_tree_vector (args);
5278 }
5279 break;
5280
5281 case CPP_DOT:
5282 case CPP_DEREF:
5283 /* postfix-expression . template [opt] id-expression
5284 postfix-expression . pseudo-destructor-name
5285 postfix-expression -> template [opt] id-expression
5286 postfix-expression -> pseudo-destructor-name */
5287
5288 /* Consume the `.' or `->' operator. */
5289 cp_lexer_consume_token (parser->lexer);
5290
5291 postfix_expression
5292 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5293 postfix_expression,
5294 false, &idk,
5295 token->location);
5296
5297 is_member_access = true;
5298 break;
5299
5300 case CPP_PLUS_PLUS:
5301 /* postfix-expression ++ */
5302 /* Consume the `++' token. */
5303 cp_lexer_consume_token (parser->lexer);
5304 /* Generate a representation for the complete expression. */
5305 postfix_expression
5306 = finish_increment_expr (postfix_expression,
5307 POSTINCREMENT_EXPR);
5308 /* Increments may not appear in constant-expressions. */
5309 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5310 postfix_expression = error_mark_node;
5311 idk = CP_ID_KIND_NONE;
5312 is_member_access = false;
5313 break;
5314
5315 case CPP_MINUS_MINUS:
5316 /* postfix-expression -- */
5317 /* Consume the `--' token. */
5318 cp_lexer_consume_token (parser->lexer);
5319 /* Generate a representation for the complete expression. */
5320 postfix_expression
5321 = finish_increment_expr (postfix_expression,
5322 POSTDECREMENT_EXPR);
5323 /* Decrements may not appear in constant-expressions. */
5324 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5325 postfix_expression = error_mark_node;
5326 idk = CP_ID_KIND_NONE;
5327 is_member_access = false;
5328 break;
5329
5330 default:
5331 if (pidk_return != NULL)
5332 * pidk_return = idk;
5333 if (member_access_only_p)
5334 return is_member_access? postfix_expression : error_mark_node;
5335 else
5336 return postfix_expression;
5337 }
5338 }
5339
5340 /* We should never get here. */
5341 gcc_unreachable ();
5342 return error_mark_node;
5343 }
5344
5345 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5346 by cp_parser_builtin_offsetof. We're looking for
5347
5348 postfix-expression [ expression ]
5349
5350 FOR_OFFSETOF is set if we're being called in that context, which
5351 changes how we deal with integer constant expressions. */
5352
5353 static tree
5354 cp_parser_postfix_open_square_expression (cp_parser *parser,
5355 tree postfix_expression,
5356 bool for_offsetof)
5357 {
5358 tree index;
5359
5360 /* Consume the `[' token. */
5361 cp_lexer_consume_token (parser->lexer);
5362
5363 /* Parse the index expression. */
5364 /* ??? For offsetof, there is a question of what to allow here. If
5365 offsetof is not being used in an integral constant expression context,
5366 then we *could* get the right answer by computing the value at runtime.
5367 If we are in an integral constant expression context, then we might
5368 could accept any constant expression; hard to say without analysis.
5369 Rather than open the barn door too wide right away, allow only integer
5370 constant expressions here. */
5371 if (for_offsetof)
5372 index = cp_parser_constant_expression (parser, false, NULL);
5373 else
5374 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5375
5376 /* Look for the closing `]'. */
5377 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5378
5379 /* Build the ARRAY_REF. */
5380 postfix_expression = grok_array_decl (postfix_expression, index);
5381
5382 /* When not doing offsetof, array references are not permitted in
5383 constant-expressions. */
5384 if (!for_offsetof
5385 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5386 postfix_expression = error_mark_node;
5387
5388 return postfix_expression;
5389 }
5390
5391 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5392 by cp_parser_builtin_offsetof. We're looking for
5393
5394 postfix-expression . template [opt] id-expression
5395 postfix-expression . pseudo-destructor-name
5396 postfix-expression -> template [opt] id-expression
5397 postfix-expression -> pseudo-destructor-name
5398
5399 FOR_OFFSETOF is set if we're being called in that context. That sorta
5400 limits what of the above we'll actually accept, but nevermind.
5401 TOKEN_TYPE is the "." or "->" token, which will already have been
5402 removed from the stream. */
5403
5404 static tree
5405 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5406 enum cpp_ttype token_type,
5407 tree postfix_expression,
5408 bool for_offsetof, cp_id_kind *idk,
5409 location_t location)
5410 {
5411 tree name;
5412 bool dependent_p;
5413 bool pseudo_destructor_p;
5414 tree scope = NULL_TREE;
5415
5416 /* If this is a `->' operator, dereference the pointer. */
5417 if (token_type == CPP_DEREF)
5418 postfix_expression = build_x_arrow (postfix_expression);
5419 /* Check to see whether or not the expression is type-dependent. */
5420 dependent_p = type_dependent_expression_p (postfix_expression);
5421 /* The identifier following the `->' or `.' is not qualified. */
5422 parser->scope = NULL_TREE;
5423 parser->qualifying_scope = NULL_TREE;
5424 parser->object_scope = NULL_TREE;
5425 *idk = CP_ID_KIND_NONE;
5426
5427 /* Enter the scope corresponding to the type of the object
5428 given by the POSTFIX_EXPRESSION. */
5429 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5430 {
5431 scope = TREE_TYPE (postfix_expression);
5432 /* According to the standard, no expression should ever have
5433 reference type. Unfortunately, we do not currently match
5434 the standard in this respect in that our internal representation
5435 of an expression may have reference type even when the standard
5436 says it does not. Therefore, we have to manually obtain the
5437 underlying type here. */
5438 scope = non_reference (scope);
5439 /* The type of the POSTFIX_EXPRESSION must be complete. */
5440 if (scope == unknown_type_node)
5441 {
5442 error_at (location, "%qE does not have class type",
5443 postfix_expression);
5444 scope = NULL_TREE;
5445 }
5446 else
5447 scope = complete_type_or_else (scope, NULL_TREE);
5448 /* Let the name lookup machinery know that we are processing a
5449 class member access expression. */
5450 parser->context->object_type = scope;
5451 /* If something went wrong, we want to be able to discern that case,
5452 as opposed to the case where there was no SCOPE due to the type
5453 of expression being dependent. */
5454 if (!scope)
5455 scope = error_mark_node;
5456 /* If the SCOPE was erroneous, make the various semantic analysis
5457 functions exit quickly -- and without issuing additional error
5458 messages. */
5459 if (scope == error_mark_node)
5460 postfix_expression = error_mark_node;
5461 }
5462
5463 /* Assume this expression is not a pseudo-destructor access. */
5464 pseudo_destructor_p = false;
5465
5466 /* If the SCOPE is a scalar type, then, if this is a valid program,
5467 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5468 is type dependent, it can be pseudo-destructor-name or something else.
5469 Try to parse it as pseudo-destructor-name first. */
5470 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5471 {
5472 tree s;
5473 tree type;
5474
5475 cp_parser_parse_tentatively (parser);
5476 /* Parse the pseudo-destructor-name. */
5477 s = NULL_TREE;
5478 cp_parser_pseudo_destructor_name (parser, &s, &type);
5479 if (dependent_p
5480 && (cp_parser_error_occurred (parser)
5481 || TREE_CODE (type) != TYPE_DECL
5482 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5483 cp_parser_abort_tentative_parse (parser);
5484 else if (cp_parser_parse_definitely (parser))
5485 {
5486 pseudo_destructor_p = true;
5487 postfix_expression
5488 = finish_pseudo_destructor_expr (postfix_expression,
5489 s, TREE_TYPE (type));
5490 }
5491 }
5492
5493 if (!pseudo_destructor_p)
5494 {
5495 /* If the SCOPE is not a scalar type, we are looking at an
5496 ordinary class member access expression, rather than a
5497 pseudo-destructor-name. */
5498 bool template_p;
5499 cp_token *token = cp_lexer_peek_token (parser->lexer);
5500 /* Parse the id-expression. */
5501 name = (cp_parser_id_expression
5502 (parser,
5503 cp_parser_optional_template_keyword (parser),
5504 /*check_dependency_p=*/true,
5505 &template_p,
5506 /*declarator_p=*/false,
5507 /*optional_p=*/false));
5508 /* In general, build a SCOPE_REF if the member name is qualified.
5509 However, if the name was not dependent and has already been
5510 resolved; there is no need to build the SCOPE_REF. For example;
5511
5512 struct X { void f(); };
5513 template <typename T> void f(T* t) { t->X::f(); }
5514
5515 Even though "t" is dependent, "X::f" is not and has been resolved
5516 to a BASELINK; there is no need to include scope information. */
5517
5518 /* But we do need to remember that there was an explicit scope for
5519 virtual function calls. */
5520 if (parser->scope)
5521 *idk = CP_ID_KIND_QUALIFIED;
5522
5523 /* If the name is a template-id that names a type, we will get a
5524 TYPE_DECL here. That is invalid code. */
5525 if (TREE_CODE (name) == TYPE_DECL)
5526 {
5527 error_at (token->location, "invalid use of %qD", name);
5528 postfix_expression = error_mark_node;
5529 }
5530 else
5531 {
5532 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5533 {
5534 name = build_qualified_name (/*type=*/NULL_TREE,
5535 parser->scope,
5536 name,
5537 template_p);
5538 parser->scope = NULL_TREE;
5539 parser->qualifying_scope = NULL_TREE;
5540 parser->object_scope = NULL_TREE;
5541 }
5542 if (scope && name && BASELINK_P (name))
5543 adjust_result_of_qualified_name_lookup
5544 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5545 postfix_expression
5546 = finish_class_member_access_expr (postfix_expression, name,
5547 template_p,
5548 tf_warning_or_error);
5549 }
5550 }
5551
5552 /* We no longer need to look up names in the scope of the object on
5553 the left-hand side of the `.' or `->' operator. */
5554 parser->context->object_type = NULL_TREE;
5555
5556 /* Outside of offsetof, these operators may not appear in
5557 constant-expressions. */
5558 if (!for_offsetof
5559 && (cp_parser_non_integral_constant_expression
5560 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5561 postfix_expression = error_mark_node;
5562
5563 return postfix_expression;
5564 }
5565
5566 /* Parse a parenthesized expression-list.
5567
5568 expression-list:
5569 assignment-expression
5570 expression-list, assignment-expression
5571
5572 attribute-list:
5573 expression-list
5574 identifier
5575 identifier, expression-list
5576
5577 CAST_P is true if this expression is the target of a cast.
5578
5579 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5580 argument pack.
5581
5582 Returns a vector of trees. Each element is a representation of an
5583 assignment-expression. NULL is returned if the ( and or ) are
5584 missing. An empty, but allocated, vector is returned on no
5585 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5586 if we are parsing an attribute list for an attribute that wants a
5587 plain identifier argument, normal_attr for an attribute that wants
5588 an expression, or non_attr if we aren't parsing an attribute list. If
5589 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5590 not all of the expressions in the list were constant. */
5591
5592 static VEC(tree,gc) *
5593 cp_parser_parenthesized_expression_list (cp_parser* parser,
5594 int is_attribute_list,
5595 bool cast_p,
5596 bool allow_expansion_p,
5597 bool *non_constant_p)
5598 {
5599 VEC(tree,gc) *expression_list;
5600 bool fold_expr_p = is_attribute_list != non_attr;
5601 tree identifier = NULL_TREE;
5602 bool saved_greater_than_is_operator_p;
5603
5604 /* Assume all the expressions will be constant. */
5605 if (non_constant_p)
5606 *non_constant_p = false;
5607
5608 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5609 return NULL;
5610
5611 expression_list = make_tree_vector ();
5612
5613 /* Within a parenthesized expression, a `>' token is always
5614 the greater-than operator. */
5615 saved_greater_than_is_operator_p
5616 = parser->greater_than_is_operator_p;
5617 parser->greater_than_is_operator_p = true;
5618
5619 /* Consume expressions until there are no more. */
5620 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5621 while (true)
5622 {
5623 tree expr;
5624
5625 /* At the beginning of attribute lists, check to see if the
5626 next token is an identifier. */
5627 if (is_attribute_list == id_attr
5628 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5629 {
5630 cp_token *token;
5631
5632 /* Consume the identifier. */
5633 token = cp_lexer_consume_token (parser->lexer);
5634 /* Save the identifier. */
5635 identifier = token->u.value;
5636 }
5637 else
5638 {
5639 bool expr_non_constant_p;
5640
5641 /* Parse the next assignment-expression. */
5642 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5643 {
5644 /* A braced-init-list. */
5645 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5646 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5647 if (non_constant_p && expr_non_constant_p)
5648 *non_constant_p = true;
5649 }
5650 else if (non_constant_p)
5651 {
5652 expr = (cp_parser_constant_expression
5653 (parser, /*allow_non_constant_p=*/true,
5654 &expr_non_constant_p));
5655 if (expr_non_constant_p)
5656 *non_constant_p = true;
5657 }
5658 else
5659 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5660
5661 if (fold_expr_p)
5662 expr = fold_non_dependent_expr (expr);
5663
5664 /* If we have an ellipsis, then this is an expression
5665 expansion. */
5666 if (allow_expansion_p
5667 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5668 {
5669 /* Consume the `...'. */
5670 cp_lexer_consume_token (parser->lexer);
5671
5672 /* Build the argument pack. */
5673 expr = make_pack_expansion (expr);
5674 }
5675
5676 /* Add it to the list. We add error_mark_node
5677 expressions to the list, so that we can still tell if
5678 the correct form for a parenthesized expression-list
5679 is found. That gives better errors. */
5680 VEC_safe_push (tree, gc, expression_list, expr);
5681
5682 if (expr == error_mark_node)
5683 goto skip_comma;
5684 }
5685
5686 /* After the first item, attribute lists look the same as
5687 expression lists. */
5688 is_attribute_list = non_attr;
5689
5690 get_comma:;
5691 /* If the next token isn't a `,', then we are done. */
5692 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5693 break;
5694
5695 /* Otherwise, consume the `,' and keep going. */
5696 cp_lexer_consume_token (parser->lexer);
5697 }
5698
5699 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5700 {
5701 int ending;
5702
5703 skip_comma:;
5704 /* We try and resync to an unnested comma, as that will give the
5705 user better diagnostics. */
5706 ending = cp_parser_skip_to_closing_parenthesis (parser,
5707 /*recovering=*/true,
5708 /*or_comma=*/true,
5709 /*consume_paren=*/true);
5710 if (ending < 0)
5711 goto get_comma;
5712 if (!ending)
5713 {
5714 parser->greater_than_is_operator_p
5715 = saved_greater_than_is_operator_p;
5716 return NULL;
5717 }
5718 }
5719
5720 parser->greater_than_is_operator_p
5721 = saved_greater_than_is_operator_p;
5722
5723 if (identifier)
5724 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5725
5726 return expression_list;
5727 }
5728
5729 /* Parse a pseudo-destructor-name.
5730
5731 pseudo-destructor-name:
5732 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5733 :: [opt] nested-name-specifier template template-id :: ~ type-name
5734 :: [opt] nested-name-specifier [opt] ~ type-name
5735
5736 If either of the first two productions is used, sets *SCOPE to the
5737 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5738 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5739 or ERROR_MARK_NODE if the parse fails. */
5740
5741 static void
5742 cp_parser_pseudo_destructor_name (cp_parser* parser,
5743 tree* scope,
5744 tree* type)
5745 {
5746 bool nested_name_specifier_p;
5747
5748 /* Assume that things will not work out. */
5749 *type = error_mark_node;
5750
5751 /* Look for the optional `::' operator. */
5752 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5753 /* Look for the optional nested-name-specifier. */
5754 nested_name_specifier_p
5755 = (cp_parser_nested_name_specifier_opt (parser,
5756 /*typename_keyword_p=*/false,
5757 /*check_dependency_p=*/true,
5758 /*type_p=*/false,
5759 /*is_declaration=*/false)
5760 != NULL_TREE);
5761 /* Now, if we saw a nested-name-specifier, we might be doing the
5762 second production. */
5763 if (nested_name_specifier_p
5764 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5765 {
5766 /* Consume the `template' keyword. */
5767 cp_lexer_consume_token (parser->lexer);
5768 /* Parse the template-id. */
5769 cp_parser_template_id (parser,
5770 /*template_keyword_p=*/true,
5771 /*check_dependency_p=*/false,
5772 /*is_declaration=*/true);
5773 /* Look for the `::' token. */
5774 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5775 }
5776 /* If the next token is not a `~', then there might be some
5777 additional qualification. */
5778 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5779 {
5780 /* At this point, we're looking for "type-name :: ~". The type-name
5781 must not be a class-name, since this is a pseudo-destructor. So,
5782 it must be either an enum-name, or a typedef-name -- both of which
5783 are just identifiers. So, we peek ahead to check that the "::"
5784 and "~" tokens are present; if they are not, then we can avoid
5785 calling type_name. */
5786 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5787 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5788 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5789 {
5790 cp_parser_error (parser, "non-scalar type");
5791 return;
5792 }
5793
5794 /* Look for the type-name. */
5795 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5796 if (*scope == error_mark_node)
5797 return;
5798
5799 /* Look for the `::' token. */
5800 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5801 }
5802 else
5803 *scope = NULL_TREE;
5804
5805 /* Look for the `~'. */
5806 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5807 /* Look for the type-name again. We are not responsible for
5808 checking that it matches the first type-name. */
5809 *type = cp_parser_nonclass_name (parser);
5810 }
5811
5812 /* Parse a unary-expression.
5813
5814 unary-expression:
5815 postfix-expression
5816 ++ cast-expression
5817 -- cast-expression
5818 unary-operator cast-expression
5819 sizeof unary-expression
5820 sizeof ( type-id )
5821 new-expression
5822 delete-expression
5823
5824 GNU Extensions:
5825
5826 unary-expression:
5827 __extension__ cast-expression
5828 __alignof__ unary-expression
5829 __alignof__ ( type-id )
5830 __real__ cast-expression
5831 __imag__ cast-expression
5832 && identifier
5833
5834 ADDRESS_P is true iff the unary-expression is appearing as the
5835 operand of the `&' operator. CAST_P is true if this expression is
5836 the target of a cast.
5837
5838 Returns a representation of the expression. */
5839
5840 static tree
5841 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5842 cp_id_kind * pidk)
5843 {
5844 cp_token *token;
5845 enum tree_code unary_operator;
5846
5847 /* Peek at the next token. */
5848 token = cp_lexer_peek_token (parser->lexer);
5849 /* Some keywords give away the kind of expression. */
5850 if (token->type == CPP_KEYWORD)
5851 {
5852 enum rid keyword = token->keyword;
5853
5854 switch (keyword)
5855 {
5856 case RID_ALIGNOF:
5857 case RID_SIZEOF:
5858 {
5859 tree operand;
5860 enum tree_code op;
5861
5862 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5863 /* Consume the token. */
5864 cp_lexer_consume_token (parser->lexer);
5865 /* Parse the operand. */
5866 operand = cp_parser_sizeof_operand (parser, keyword);
5867
5868 if (TYPE_P (operand))
5869 return cxx_sizeof_or_alignof_type (operand, op, true);
5870 else
5871 return cxx_sizeof_or_alignof_expr (operand, op, true);
5872 }
5873
5874 case RID_NEW:
5875 return cp_parser_new_expression (parser);
5876
5877 case RID_DELETE:
5878 return cp_parser_delete_expression (parser);
5879
5880 case RID_EXTENSION:
5881 {
5882 /* The saved value of the PEDANTIC flag. */
5883 int saved_pedantic;
5884 tree expr;
5885
5886 /* Save away the PEDANTIC flag. */
5887 cp_parser_extension_opt (parser, &saved_pedantic);
5888 /* Parse the cast-expression. */
5889 expr = cp_parser_simple_cast_expression (parser);
5890 /* Restore the PEDANTIC flag. */
5891 pedantic = saved_pedantic;
5892
5893 return expr;
5894 }
5895
5896 case RID_REALPART:
5897 case RID_IMAGPART:
5898 {
5899 tree expression;
5900
5901 /* Consume the `__real__' or `__imag__' token. */
5902 cp_lexer_consume_token (parser->lexer);
5903 /* Parse the cast-expression. */
5904 expression = cp_parser_simple_cast_expression (parser);
5905 /* Create the complete representation. */
5906 return build_x_unary_op ((keyword == RID_REALPART
5907 ? REALPART_EXPR : IMAGPART_EXPR),
5908 expression,
5909 tf_warning_or_error);
5910 }
5911 break;
5912
5913 case RID_NOEXCEPT:
5914 {
5915 tree expr;
5916 const char *saved_message;
5917 bool saved_integral_constant_expression_p;
5918 bool saved_non_integral_constant_expression_p;
5919 bool saved_greater_than_is_operator_p;
5920
5921 cp_lexer_consume_token (parser->lexer);
5922 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5923
5924 saved_message = parser->type_definition_forbidden_message;
5925 parser->type_definition_forbidden_message
5926 = G_("types may not be defined in %<noexcept%> expressions");
5927
5928 saved_integral_constant_expression_p
5929 = parser->integral_constant_expression_p;
5930 saved_non_integral_constant_expression_p
5931 = parser->non_integral_constant_expression_p;
5932 parser->integral_constant_expression_p = false;
5933
5934 saved_greater_than_is_operator_p
5935 = parser->greater_than_is_operator_p;
5936 parser->greater_than_is_operator_p = true;
5937
5938 ++cp_unevaluated_operand;
5939 ++c_inhibit_evaluation_warnings;
5940 expr = cp_parser_expression (parser, false, NULL);
5941 --c_inhibit_evaluation_warnings;
5942 --cp_unevaluated_operand;
5943
5944 parser->greater_than_is_operator_p
5945 = saved_greater_than_is_operator_p;
5946
5947 parser->integral_constant_expression_p
5948 = saved_integral_constant_expression_p;
5949 parser->non_integral_constant_expression_p
5950 = saved_non_integral_constant_expression_p;
5951
5952 parser->type_definition_forbidden_message = saved_message;
5953
5954 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5955 return finish_noexcept_expr (expr, tf_warning_or_error);
5956 }
5957
5958 default:
5959 break;
5960 }
5961 }
5962
5963 /* Look for the `:: new' and `:: delete', which also signal the
5964 beginning of a new-expression, or delete-expression,
5965 respectively. If the next token is `::', then it might be one of
5966 these. */
5967 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5968 {
5969 enum rid keyword;
5970
5971 /* See if the token after the `::' is one of the keywords in
5972 which we're interested. */
5973 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5974 /* If it's `new', we have a new-expression. */
5975 if (keyword == RID_NEW)
5976 return cp_parser_new_expression (parser);
5977 /* Similarly, for `delete'. */
5978 else if (keyword == RID_DELETE)
5979 return cp_parser_delete_expression (parser);
5980 }
5981
5982 /* Look for a unary operator. */
5983 unary_operator = cp_parser_unary_operator (token);
5984 /* The `++' and `--' operators can be handled similarly, even though
5985 they are not technically unary-operators in the grammar. */
5986 if (unary_operator == ERROR_MARK)
5987 {
5988 if (token->type == CPP_PLUS_PLUS)
5989 unary_operator = PREINCREMENT_EXPR;
5990 else if (token->type == CPP_MINUS_MINUS)
5991 unary_operator = PREDECREMENT_EXPR;
5992 /* Handle the GNU address-of-label extension. */
5993 else if (cp_parser_allow_gnu_extensions_p (parser)
5994 && token->type == CPP_AND_AND)
5995 {
5996 tree identifier;
5997 tree expression;
5998 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5999
6000 /* Consume the '&&' token. */
6001 cp_lexer_consume_token (parser->lexer);
6002 /* Look for the identifier. */
6003 identifier = cp_parser_identifier (parser);
6004 /* Create an expression representing the address. */
6005 expression = finish_label_address_expr (identifier, loc);
6006 if (cp_parser_non_integral_constant_expression (parser,
6007 NIC_ADDR_LABEL))
6008 expression = error_mark_node;
6009 return expression;
6010 }
6011 }
6012 if (unary_operator != ERROR_MARK)
6013 {
6014 tree cast_expression;
6015 tree expression = error_mark_node;
6016 non_integral_constant non_constant_p = NIC_NONE;
6017
6018 /* Consume the operator token. */
6019 token = cp_lexer_consume_token (parser->lexer);
6020 /* Parse the cast-expression. */
6021 cast_expression
6022 = cp_parser_cast_expression (parser,
6023 unary_operator == ADDR_EXPR,
6024 /*cast_p=*/false, pidk);
6025 /* Now, build an appropriate representation. */
6026 switch (unary_operator)
6027 {
6028 case INDIRECT_REF:
6029 non_constant_p = NIC_STAR;
6030 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6031 tf_warning_or_error);
6032 break;
6033
6034 case ADDR_EXPR:
6035 non_constant_p = NIC_ADDR;
6036 /* Fall through. */
6037 case BIT_NOT_EXPR:
6038 expression = build_x_unary_op (unary_operator, cast_expression,
6039 tf_warning_or_error);
6040 break;
6041
6042 case PREINCREMENT_EXPR:
6043 case PREDECREMENT_EXPR:
6044 non_constant_p = unary_operator == PREINCREMENT_EXPR
6045 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6046 /* Fall through. */
6047 case UNARY_PLUS_EXPR:
6048 case NEGATE_EXPR:
6049 case TRUTH_NOT_EXPR:
6050 expression = finish_unary_op_expr (unary_operator, cast_expression);
6051 break;
6052
6053 default:
6054 gcc_unreachable ();
6055 }
6056
6057 if (non_constant_p != NIC_NONE
6058 && cp_parser_non_integral_constant_expression (parser,
6059 non_constant_p))
6060 expression = error_mark_node;
6061
6062 return expression;
6063 }
6064
6065 return cp_parser_postfix_expression (parser, address_p, cast_p,
6066 /*member_access_only_p=*/false,
6067 pidk);
6068 }
6069
6070 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6071 unary-operator, the corresponding tree code is returned. */
6072
6073 static enum tree_code
6074 cp_parser_unary_operator (cp_token* token)
6075 {
6076 switch (token->type)
6077 {
6078 case CPP_MULT:
6079 return INDIRECT_REF;
6080
6081 case CPP_AND:
6082 return ADDR_EXPR;
6083
6084 case CPP_PLUS:
6085 return UNARY_PLUS_EXPR;
6086
6087 case CPP_MINUS:
6088 return NEGATE_EXPR;
6089
6090 case CPP_NOT:
6091 return TRUTH_NOT_EXPR;
6092
6093 case CPP_COMPL:
6094 return BIT_NOT_EXPR;
6095
6096 default:
6097 return ERROR_MARK;
6098 }
6099 }
6100
6101 /* Parse a new-expression.
6102
6103 new-expression:
6104 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6105 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6106
6107 Returns a representation of the expression. */
6108
6109 static tree
6110 cp_parser_new_expression (cp_parser* parser)
6111 {
6112 bool global_scope_p;
6113 VEC(tree,gc) *placement;
6114 tree type;
6115 VEC(tree,gc) *initializer;
6116 tree nelts;
6117 tree ret;
6118
6119 /* Look for the optional `::' operator. */
6120 global_scope_p
6121 = (cp_parser_global_scope_opt (parser,
6122 /*current_scope_valid_p=*/false)
6123 != NULL_TREE);
6124 /* Look for the `new' operator. */
6125 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6126 /* There's no easy way to tell a new-placement from the
6127 `( type-id )' construct. */
6128 cp_parser_parse_tentatively (parser);
6129 /* Look for a new-placement. */
6130 placement = cp_parser_new_placement (parser);
6131 /* If that didn't work out, there's no new-placement. */
6132 if (!cp_parser_parse_definitely (parser))
6133 {
6134 if (placement != NULL)
6135 release_tree_vector (placement);
6136 placement = NULL;
6137 }
6138
6139 /* If the next token is a `(', then we have a parenthesized
6140 type-id. */
6141 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6142 {
6143 cp_token *token;
6144 /* Consume the `('. */
6145 cp_lexer_consume_token (parser->lexer);
6146 /* Parse the type-id. */
6147 type = cp_parser_type_id (parser);
6148 /* Look for the closing `)'. */
6149 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6150 token = cp_lexer_peek_token (parser->lexer);
6151 /* There should not be a direct-new-declarator in this production,
6152 but GCC used to allowed this, so we check and emit a sensible error
6153 message for this case. */
6154 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6155 {
6156 error_at (token->location,
6157 "array bound forbidden after parenthesized type-id");
6158 inform (token->location,
6159 "try removing the parentheses around the type-id");
6160 cp_parser_direct_new_declarator (parser);
6161 }
6162 nelts = NULL_TREE;
6163 }
6164 /* Otherwise, there must be a new-type-id. */
6165 else
6166 type = cp_parser_new_type_id (parser, &nelts);
6167
6168 /* If the next token is a `(' or '{', then we have a new-initializer. */
6169 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6170 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6171 initializer = cp_parser_new_initializer (parser);
6172 else
6173 initializer = NULL;
6174
6175 /* A new-expression may not appear in an integral constant
6176 expression. */
6177 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6178 ret = error_mark_node;
6179 else
6180 {
6181 /* Create a representation of the new-expression. */
6182 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6183 tf_warning_or_error);
6184 }
6185
6186 if (placement != NULL)
6187 release_tree_vector (placement);
6188 if (initializer != NULL)
6189 release_tree_vector (initializer);
6190
6191 return ret;
6192 }
6193
6194 /* Parse a new-placement.
6195
6196 new-placement:
6197 ( expression-list )
6198
6199 Returns the same representation as for an expression-list. */
6200
6201 static VEC(tree,gc) *
6202 cp_parser_new_placement (cp_parser* parser)
6203 {
6204 VEC(tree,gc) *expression_list;
6205
6206 /* Parse the expression-list. */
6207 expression_list = (cp_parser_parenthesized_expression_list
6208 (parser, non_attr, /*cast_p=*/false,
6209 /*allow_expansion_p=*/true,
6210 /*non_constant_p=*/NULL));
6211
6212 return expression_list;
6213 }
6214
6215 /* Parse a new-type-id.
6216
6217 new-type-id:
6218 type-specifier-seq new-declarator [opt]
6219
6220 Returns the TYPE allocated. If the new-type-id indicates an array
6221 type, *NELTS is set to the number of elements in the last array
6222 bound; the TYPE will not include the last array bound. */
6223
6224 static tree
6225 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6226 {
6227 cp_decl_specifier_seq type_specifier_seq;
6228 cp_declarator *new_declarator;
6229 cp_declarator *declarator;
6230 cp_declarator *outer_declarator;
6231 const char *saved_message;
6232 tree type;
6233
6234 /* The type-specifier sequence must not contain type definitions.
6235 (It cannot contain declarations of new types either, but if they
6236 are not definitions we will catch that because they are not
6237 complete.) */
6238 saved_message = parser->type_definition_forbidden_message;
6239 parser->type_definition_forbidden_message
6240 = G_("types may not be defined in a new-type-id");
6241 /* Parse the type-specifier-seq. */
6242 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6243 /*is_trailing_return=*/false,
6244 &type_specifier_seq);
6245 /* Restore the old message. */
6246 parser->type_definition_forbidden_message = saved_message;
6247 /* Parse the new-declarator. */
6248 new_declarator = cp_parser_new_declarator_opt (parser);
6249
6250 /* Determine the number of elements in the last array dimension, if
6251 any. */
6252 *nelts = NULL_TREE;
6253 /* Skip down to the last array dimension. */
6254 declarator = new_declarator;
6255 outer_declarator = NULL;
6256 while (declarator && (declarator->kind == cdk_pointer
6257 || declarator->kind == cdk_ptrmem))
6258 {
6259 outer_declarator = declarator;
6260 declarator = declarator->declarator;
6261 }
6262 while (declarator
6263 && declarator->kind == cdk_array
6264 && declarator->declarator
6265 && declarator->declarator->kind == cdk_array)
6266 {
6267 outer_declarator = declarator;
6268 declarator = declarator->declarator;
6269 }
6270
6271 if (declarator && declarator->kind == cdk_array)
6272 {
6273 *nelts = declarator->u.array.bounds;
6274 if (*nelts == error_mark_node)
6275 *nelts = integer_one_node;
6276
6277 if (outer_declarator)
6278 outer_declarator->declarator = declarator->declarator;
6279 else
6280 new_declarator = NULL;
6281 }
6282
6283 type = groktypename (&type_specifier_seq, new_declarator, false);
6284 return type;
6285 }
6286
6287 /* Parse an (optional) new-declarator.
6288
6289 new-declarator:
6290 ptr-operator new-declarator [opt]
6291 direct-new-declarator
6292
6293 Returns the declarator. */
6294
6295 static cp_declarator *
6296 cp_parser_new_declarator_opt (cp_parser* parser)
6297 {
6298 enum tree_code code;
6299 tree type;
6300 cp_cv_quals cv_quals;
6301
6302 /* We don't know if there's a ptr-operator next, or not. */
6303 cp_parser_parse_tentatively (parser);
6304 /* Look for a ptr-operator. */
6305 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6306 /* If that worked, look for more new-declarators. */
6307 if (cp_parser_parse_definitely (parser))
6308 {
6309 cp_declarator *declarator;
6310
6311 /* Parse another optional declarator. */
6312 declarator = cp_parser_new_declarator_opt (parser);
6313
6314 return cp_parser_make_indirect_declarator
6315 (code, type, cv_quals, declarator);
6316 }
6317
6318 /* If the next token is a `[', there is a direct-new-declarator. */
6319 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6320 return cp_parser_direct_new_declarator (parser);
6321
6322 return NULL;
6323 }
6324
6325 /* Parse a direct-new-declarator.
6326
6327 direct-new-declarator:
6328 [ expression ]
6329 direct-new-declarator [constant-expression]
6330
6331 */
6332
6333 static cp_declarator *
6334 cp_parser_direct_new_declarator (cp_parser* parser)
6335 {
6336 cp_declarator *declarator = NULL;
6337
6338 while (true)
6339 {
6340 tree expression;
6341
6342 /* Look for the opening `['. */
6343 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6344 /* The first expression is not required to be constant. */
6345 if (!declarator)
6346 {
6347 cp_token *token = cp_lexer_peek_token (parser->lexer);
6348 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6349 /* The standard requires that the expression have integral
6350 type. DR 74 adds enumeration types. We believe that the
6351 real intent is that these expressions be handled like the
6352 expression in a `switch' condition, which also allows
6353 classes with a single conversion to integral or
6354 enumeration type. */
6355 if (!processing_template_decl)
6356 {
6357 expression
6358 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6359 expression,
6360 /*complain=*/true);
6361 if (!expression)
6362 {
6363 error_at (token->location,
6364 "expression in new-declarator must have integral "
6365 "or enumeration type");
6366 expression = error_mark_node;
6367 }
6368 }
6369 }
6370 /* But all the other expressions must be. */
6371 else
6372 expression
6373 = cp_parser_constant_expression (parser,
6374 /*allow_non_constant=*/false,
6375 NULL);
6376 /* Look for the closing `]'. */
6377 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6378
6379 /* Add this bound to the declarator. */
6380 declarator = make_array_declarator (declarator, expression);
6381
6382 /* If the next token is not a `[', then there are no more
6383 bounds. */
6384 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6385 break;
6386 }
6387
6388 return declarator;
6389 }
6390
6391 /* Parse a new-initializer.
6392
6393 new-initializer:
6394 ( expression-list [opt] )
6395 braced-init-list
6396
6397 Returns a representation of the expression-list. */
6398
6399 static VEC(tree,gc) *
6400 cp_parser_new_initializer (cp_parser* parser)
6401 {
6402 VEC(tree,gc) *expression_list;
6403
6404 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6405 {
6406 tree t;
6407 bool expr_non_constant_p;
6408 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6409 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6410 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6411 expression_list = make_tree_vector_single (t);
6412 }
6413 else
6414 expression_list = (cp_parser_parenthesized_expression_list
6415 (parser, non_attr, /*cast_p=*/false,
6416 /*allow_expansion_p=*/true,
6417 /*non_constant_p=*/NULL));
6418
6419 return expression_list;
6420 }
6421
6422 /* Parse a delete-expression.
6423
6424 delete-expression:
6425 :: [opt] delete cast-expression
6426 :: [opt] delete [ ] cast-expression
6427
6428 Returns a representation of the expression. */
6429
6430 static tree
6431 cp_parser_delete_expression (cp_parser* parser)
6432 {
6433 bool global_scope_p;
6434 bool array_p;
6435 tree expression;
6436
6437 /* Look for the optional `::' operator. */
6438 global_scope_p
6439 = (cp_parser_global_scope_opt (parser,
6440 /*current_scope_valid_p=*/false)
6441 != NULL_TREE);
6442 /* Look for the `delete' keyword. */
6443 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6444 /* See if the array syntax is in use. */
6445 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6446 {
6447 /* Consume the `[' token. */
6448 cp_lexer_consume_token (parser->lexer);
6449 /* Look for the `]' token. */
6450 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6451 /* Remember that this is the `[]' construct. */
6452 array_p = true;
6453 }
6454 else
6455 array_p = false;
6456
6457 /* Parse the cast-expression. */
6458 expression = cp_parser_simple_cast_expression (parser);
6459
6460 /* A delete-expression may not appear in an integral constant
6461 expression. */
6462 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6463 return error_mark_node;
6464
6465 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6466 }
6467
6468 /* Returns true if TOKEN may start a cast-expression and false
6469 otherwise. */
6470
6471 static bool
6472 cp_parser_token_starts_cast_expression (cp_token *token)
6473 {
6474 switch (token->type)
6475 {
6476 case CPP_COMMA:
6477 case CPP_SEMICOLON:
6478 case CPP_QUERY:
6479 case CPP_COLON:
6480 case CPP_CLOSE_SQUARE:
6481 case CPP_CLOSE_PAREN:
6482 case CPP_CLOSE_BRACE:
6483 case CPP_DOT:
6484 case CPP_DOT_STAR:
6485 case CPP_DEREF:
6486 case CPP_DEREF_STAR:
6487 case CPP_DIV:
6488 case CPP_MOD:
6489 case CPP_LSHIFT:
6490 case CPP_RSHIFT:
6491 case CPP_LESS:
6492 case CPP_GREATER:
6493 case CPP_LESS_EQ:
6494 case CPP_GREATER_EQ:
6495 case CPP_EQ_EQ:
6496 case CPP_NOT_EQ:
6497 case CPP_EQ:
6498 case CPP_MULT_EQ:
6499 case CPP_DIV_EQ:
6500 case CPP_MOD_EQ:
6501 case CPP_PLUS_EQ:
6502 case CPP_MINUS_EQ:
6503 case CPP_RSHIFT_EQ:
6504 case CPP_LSHIFT_EQ:
6505 case CPP_AND_EQ:
6506 case CPP_XOR_EQ:
6507 case CPP_OR_EQ:
6508 case CPP_XOR:
6509 case CPP_OR:
6510 case CPP_OR_OR:
6511 case CPP_EOF:
6512 return false;
6513
6514 /* '[' may start a primary-expression in obj-c++. */
6515 case CPP_OPEN_SQUARE:
6516 return c_dialect_objc ();
6517
6518 default:
6519 return true;
6520 }
6521 }
6522
6523 /* Parse a cast-expression.
6524
6525 cast-expression:
6526 unary-expression
6527 ( type-id ) cast-expression
6528
6529 ADDRESS_P is true iff the unary-expression is appearing as the
6530 operand of the `&' operator. CAST_P is true if this expression is
6531 the target of a cast.
6532
6533 Returns a representation of the expression. */
6534
6535 static tree
6536 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6537 cp_id_kind * pidk)
6538 {
6539 /* If it's a `(', then we might be looking at a cast. */
6540 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6541 {
6542 tree type = NULL_TREE;
6543 tree expr = NULL_TREE;
6544 bool compound_literal_p;
6545 const char *saved_message;
6546
6547 /* There's no way to know yet whether or not this is a cast.
6548 For example, `(int (3))' is a unary-expression, while `(int)
6549 3' is a cast. So, we resort to parsing tentatively. */
6550 cp_parser_parse_tentatively (parser);
6551 /* Types may not be defined in a cast. */
6552 saved_message = parser->type_definition_forbidden_message;
6553 parser->type_definition_forbidden_message
6554 = G_("types may not be defined in casts");
6555 /* Consume the `('. */
6556 cp_lexer_consume_token (parser->lexer);
6557 /* A very tricky bit is that `(struct S) { 3 }' is a
6558 compound-literal (which we permit in C++ as an extension).
6559 But, that construct is not a cast-expression -- it is a
6560 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6561 is legal; if the compound-literal were a cast-expression,
6562 you'd need an extra set of parentheses.) But, if we parse
6563 the type-id, and it happens to be a class-specifier, then we
6564 will commit to the parse at that point, because we cannot
6565 undo the action that is done when creating a new class. So,
6566 then we cannot back up and do a postfix-expression.
6567
6568 Therefore, we scan ahead to the closing `)', and check to see
6569 if the token after the `)' is a `{'. If so, we are not
6570 looking at a cast-expression.
6571
6572 Save tokens so that we can put them back. */
6573 cp_lexer_save_tokens (parser->lexer);
6574 /* Skip tokens until the next token is a closing parenthesis.
6575 If we find the closing `)', and the next token is a `{', then
6576 we are looking at a compound-literal. */
6577 compound_literal_p
6578 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6579 /*consume_paren=*/true)
6580 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6581 /* Roll back the tokens we skipped. */
6582 cp_lexer_rollback_tokens (parser->lexer);
6583 /* If we were looking at a compound-literal, simulate an error
6584 so that the call to cp_parser_parse_definitely below will
6585 fail. */
6586 if (compound_literal_p)
6587 cp_parser_simulate_error (parser);
6588 else
6589 {
6590 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6591 parser->in_type_id_in_expr_p = true;
6592 /* Look for the type-id. */
6593 type = cp_parser_type_id (parser);
6594 /* Look for the closing `)'. */
6595 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6596 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6597 }
6598
6599 /* Restore the saved message. */
6600 parser->type_definition_forbidden_message = saved_message;
6601
6602 /* At this point this can only be either a cast or a
6603 parenthesized ctor such as `(T ())' that looks like a cast to
6604 function returning T. */
6605 if (!cp_parser_error_occurred (parser)
6606 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6607 (parser->lexer)))
6608 {
6609 cp_parser_parse_definitely (parser);
6610 expr = cp_parser_cast_expression (parser,
6611 /*address_p=*/false,
6612 /*cast_p=*/true, pidk);
6613
6614 /* Warn about old-style casts, if so requested. */
6615 if (warn_old_style_cast
6616 && !in_system_header
6617 && !VOID_TYPE_P (type)
6618 && current_lang_name != lang_name_c)
6619 warning (OPT_Wold_style_cast, "use of old-style cast");
6620
6621 /* Only type conversions to integral or enumeration types
6622 can be used in constant-expressions. */
6623 if (!cast_valid_in_integral_constant_expression_p (type)
6624 && cp_parser_non_integral_constant_expression (parser,
6625 NIC_CAST))
6626 return error_mark_node;
6627
6628 /* Perform the cast. */
6629 expr = build_c_cast (input_location, type, expr);
6630 return expr;
6631 }
6632 else
6633 cp_parser_abort_tentative_parse (parser);
6634 }
6635
6636 /* If we get here, then it's not a cast, so it must be a
6637 unary-expression. */
6638 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6639 }
6640
6641 /* Parse a binary expression of the general form:
6642
6643 pm-expression:
6644 cast-expression
6645 pm-expression .* cast-expression
6646 pm-expression ->* cast-expression
6647
6648 multiplicative-expression:
6649 pm-expression
6650 multiplicative-expression * pm-expression
6651 multiplicative-expression / pm-expression
6652 multiplicative-expression % pm-expression
6653
6654 additive-expression:
6655 multiplicative-expression
6656 additive-expression + multiplicative-expression
6657 additive-expression - multiplicative-expression
6658
6659 shift-expression:
6660 additive-expression
6661 shift-expression << additive-expression
6662 shift-expression >> additive-expression
6663
6664 relational-expression:
6665 shift-expression
6666 relational-expression < shift-expression
6667 relational-expression > shift-expression
6668 relational-expression <= shift-expression
6669 relational-expression >= shift-expression
6670
6671 GNU Extension:
6672
6673 relational-expression:
6674 relational-expression <? shift-expression
6675 relational-expression >? shift-expression
6676
6677 equality-expression:
6678 relational-expression
6679 equality-expression == relational-expression
6680 equality-expression != relational-expression
6681
6682 and-expression:
6683 equality-expression
6684 and-expression & equality-expression
6685
6686 exclusive-or-expression:
6687 and-expression
6688 exclusive-or-expression ^ and-expression
6689
6690 inclusive-or-expression:
6691 exclusive-or-expression
6692 inclusive-or-expression | exclusive-or-expression
6693
6694 logical-and-expression:
6695 inclusive-or-expression
6696 logical-and-expression && inclusive-or-expression
6697
6698 logical-or-expression:
6699 logical-and-expression
6700 logical-or-expression || logical-and-expression
6701
6702 All these are implemented with a single function like:
6703
6704 binary-expression:
6705 simple-cast-expression
6706 binary-expression <token> binary-expression
6707
6708 CAST_P is true if this expression is the target of a cast.
6709
6710 The binops_by_token map is used to get the tree codes for each <token> type.
6711 binary-expressions are associated according to a precedence table. */
6712
6713 #define TOKEN_PRECEDENCE(token) \
6714 (((token->type == CPP_GREATER \
6715 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6716 && !parser->greater_than_is_operator_p) \
6717 ? PREC_NOT_OPERATOR \
6718 : binops_by_token[token->type].prec)
6719
6720 static tree
6721 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6722 bool no_toplevel_fold_p,
6723 enum cp_parser_prec prec,
6724 cp_id_kind * pidk)
6725 {
6726 cp_parser_expression_stack stack;
6727 cp_parser_expression_stack_entry *sp = &stack[0];
6728 tree lhs, rhs;
6729 cp_token *token;
6730 enum tree_code tree_type, lhs_type, rhs_type;
6731 enum cp_parser_prec new_prec, lookahead_prec;
6732 bool overloaded_p;
6733
6734 /* Parse the first expression. */
6735 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6736 lhs_type = ERROR_MARK;
6737
6738 for (;;)
6739 {
6740 /* Get an operator token. */
6741 token = cp_lexer_peek_token (parser->lexer);
6742
6743 if (warn_cxx0x_compat
6744 && token->type == CPP_RSHIFT
6745 && !parser->greater_than_is_operator_p)
6746 {
6747 if (warning_at (token->location, OPT_Wc__0x_compat,
6748 "%<>>%> operator will be treated as"
6749 " two right angle brackets in C++0x"))
6750 inform (token->location,
6751 "suggest parentheses around %<>>%> expression");
6752 }
6753
6754 new_prec = TOKEN_PRECEDENCE (token);
6755
6756 /* Popping an entry off the stack means we completed a subexpression:
6757 - either we found a token which is not an operator (`>' where it is not
6758 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6759 will happen repeatedly;
6760 - or, we found an operator which has lower priority. This is the case
6761 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6762 parsing `3 * 4'. */
6763 if (new_prec <= prec)
6764 {
6765 if (sp == stack)
6766 break;
6767 else
6768 goto pop;
6769 }
6770
6771 get_rhs:
6772 tree_type = binops_by_token[token->type].tree_type;
6773
6774 /* We used the operator token. */
6775 cp_lexer_consume_token (parser->lexer);
6776
6777 /* For "false && x" or "true || x", x will never be executed;
6778 disable warnings while evaluating it. */
6779 if (tree_type == TRUTH_ANDIF_EXPR)
6780 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6781 else if (tree_type == TRUTH_ORIF_EXPR)
6782 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6783
6784 /* Extract another operand. It may be the RHS of this expression
6785 or the LHS of a new, higher priority expression. */
6786 rhs = cp_parser_simple_cast_expression (parser);
6787 rhs_type = ERROR_MARK;
6788
6789 /* Get another operator token. Look up its precedence to avoid
6790 building a useless (immediately popped) stack entry for common
6791 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6792 token = cp_lexer_peek_token (parser->lexer);
6793 lookahead_prec = TOKEN_PRECEDENCE (token);
6794 if (lookahead_prec > new_prec)
6795 {
6796 /* ... and prepare to parse the RHS of the new, higher priority
6797 expression. Since precedence levels on the stack are
6798 monotonically increasing, we do not have to care about
6799 stack overflows. */
6800 sp->prec = prec;
6801 sp->tree_type = tree_type;
6802 sp->lhs = lhs;
6803 sp->lhs_type = lhs_type;
6804 sp++;
6805 lhs = rhs;
6806 lhs_type = rhs_type;
6807 prec = new_prec;
6808 new_prec = lookahead_prec;
6809 goto get_rhs;
6810
6811 pop:
6812 lookahead_prec = new_prec;
6813 /* If the stack is not empty, we have parsed into LHS the right side
6814 (`4' in the example above) of an expression we had suspended.
6815 We can use the information on the stack to recover the LHS (`3')
6816 from the stack together with the tree code (`MULT_EXPR'), and
6817 the precedence of the higher level subexpression
6818 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6819 which will be used to actually build the additive expression. */
6820 --sp;
6821 prec = sp->prec;
6822 tree_type = sp->tree_type;
6823 rhs = lhs;
6824 rhs_type = lhs_type;
6825 lhs = sp->lhs;
6826 lhs_type = sp->lhs_type;
6827 }
6828
6829 /* Undo the disabling of warnings done above. */
6830 if (tree_type == TRUTH_ANDIF_EXPR)
6831 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6832 else if (tree_type == TRUTH_ORIF_EXPR)
6833 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6834
6835 overloaded_p = false;
6836 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6837 ERROR_MARK for everything that is not a binary expression.
6838 This makes warn_about_parentheses miss some warnings that
6839 involve unary operators. For unary expressions we should
6840 pass the correct tree_code unless the unary expression was
6841 surrounded by parentheses.
6842 */
6843 if (no_toplevel_fold_p
6844 && lookahead_prec <= prec
6845 && sp == stack
6846 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6847 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6848 else
6849 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6850 &overloaded_p, tf_warning_or_error);
6851 lhs_type = tree_type;
6852
6853 /* If the binary operator required the use of an overloaded operator,
6854 then this expression cannot be an integral constant-expression.
6855 An overloaded operator can be used even if both operands are
6856 otherwise permissible in an integral constant-expression if at
6857 least one of the operands is of enumeration type. */
6858
6859 if (overloaded_p
6860 && cp_parser_non_integral_constant_expression (parser,
6861 NIC_OVERLOADED))
6862 return error_mark_node;
6863 }
6864
6865 return lhs;
6866 }
6867
6868
6869 /* Parse the `? expression : assignment-expression' part of a
6870 conditional-expression. The LOGICAL_OR_EXPR is the
6871 logical-or-expression that started the conditional-expression.
6872 Returns a representation of the entire conditional-expression.
6873
6874 This routine is used by cp_parser_assignment_expression.
6875
6876 ? expression : assignment-expression
6877
6878 GNU Extensions:
6879
6880 ? : assignment-expression */
6881
6882 static tree
6883 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6884 {
6885 tree expr;
6886 tree assignment_expr;
6887 struct cp_token *token;
6888
6889 /* Consume the `?' token. */
6890 cp_lexer_consume_token (parser->lexer);
6891 token = cp_lexer_peek_token (parser->lexer);
6892 if (cp_parser_allow_gnu_extensions_p (parser)
6893 && token->type == CPP_COLON)
6894 {
6895 pedwarn (token->location, OPT_pedantic,
6896 "ISO C++ does not allow ?: with omitted middle operand");
6897 /* Implicit true clause. */
6898 expr = NULL_TREE;
6899 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6900 warn_for_omitted_condop (token->location, logical_or_expr);
6901 }
6902 else
6903 {
6904 /* Parse the expression. */
6905 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6906 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6907 c_inhibit_evaluation_warnings +=
6908 ((logical_or_expr == truthvalue_true_node)
6909 - (logical_or_expr == truthvalue_false_node));
6910 }
6911
6912 /* The next token should be a `:'. */
6913 cp_parser_require (parser, CPP_COLON, RT_COLON);
6914 /* Parse the assignment-expression. */
6915 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6916 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6917
6918 /* Build the conditional-expression. */
6919 return build_x_conditional_expr (logical_or_expr,
6920 expr,
6921 assignment_expr,
6922 tf_warning_or_error);
6923 }
6924
6925 /* Parse an assignment-expression.
6926
6927 assignment-expression:
6928 conditional-expression
6929 logical-or-expression assignment-operator assignment_expression
6930 throw-expression
6931
6932 CAST_P is true if this expression is the target of a cast.
6933
6934 Returns a representation for the expression. */
6935
6936 static tree
6937 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6938 cp_id_kind * pidk)
6939 {
6940 tree expr;
6941
6942 /* If the next token is the `throw' keyword, then we're looking at
6943 a throw-expression. */
6944 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6945 expr = cp_parser_throw_expression (parser);
6946 /* Otherwise, it must be that we are looking at a
6947 logical-or-expression. */
6948 else
6949 {
6950 /* Parse the binary expressions (logical-or-expression). */
6951 expr = cp_parser_binary_expression (parser, cast_p, false,
6952 PREC_NOT_OPERATOR, pidk);
6953 /* If the next token is a `?' then we're actually looking at a
6954 conditional-expression. */
6955 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6956 return cp_parser_question_colon_clause (parser, expr);
6957 else
6958 {
6959 enum tree_code assignment_operator;
6960
6961 /* If it's an assignment-operator, we're using the second
6962 production. */
6963 assignment_operator
6964 = cp_parser_assignment_operator_opt (parser);
6965 if (assignment_operator != ERROR_MARK)
6966 {
6967 bool non_constant_p;
6968
6969 /* Parse the right-hand side of the assignment. */
6970 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6971
6972 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6973 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6974
6975 /* An assignment may not appear in a
6976 constant-expression. */
6977 if (cp_parser_non_integral_constant_expression (parser,
6978 NIC_ASSIGNMENT))
6979 return error_mark_node;
6980 /* Build the assignment expression. */
6981 expr = build_x_modify_expr (expr,
6982 assignment_operator,
6983 rhs,
6984 tf_warning_or_error);
6985 }
6986 }
6987 }
6988
6989 return expr;
6990 }
6991
6992 /* Parse an (optional) assignment-operator.
6993
6994 assignment-operator: one of
6995 = *= /= %= += -= >>= <<= &= ^= |=
6996
6997 GNU Extension:
6998
6999 assignment-operator: one of
7000 <?= >?=
7001
7002 If the next token is an assignment operator, the corresponding tree
7003 code is returned, and the token is consumed. For example, for
7004 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7005 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7006 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7007 operator, ERROR_MARK is returned. */
7008
7009 static enum tree_code
7010 cp_parser_assignment_operator_opt (cp_parser* parser)
7011 {
7012 enum tree_code op;
7013 cp_token *token;
7014
7015 /* Peek at the next token. */
7016 token = cp_lexer_peek_token (parser->lexer);
7017
7018 switch (token->type)
7019 {
7020 case CPP_EQ:
7021 op = NOP_EXPR;
7022 break;
7023
7024 case CPP_MULT_EQ:
7025 op = MULT_EXPR;
7026 break;
7027
7028 case CPP_DIV_EQ:
7029 op = TRUNC_DIV_EXPR;
7030 break;
7031
7032 case CPP_MOD_EQ:
7033 op = TRUNC_MOD_EXPR;
7034 break;
7035
7036 case CPP_PLUS_EQ:
7037 op = PLUS_EXPR;
7038 break;
7039
7040 case CPP_MINUS_EQ:
7041 op = MINUS_EXPR;
7042 break;
7043
7044 case CPP_RSHIFT_EQ:
7045 op = RSHIFT_EXPR;
7046 break;
7047
7048 case CPP_LSHIFT_EQ:
7049 op = LSHIFT_EXPR;
7050 break;
7051
7052 case CPP_AND_EQ:
7053 op = BIT_AND_EXPR;
7054 break;
7055
7056 case CPP_XOR_EQ:
7057 op = BIT_XOR_EXPR;
7058 break;
7059
7060 case CPP_OR_EQ:
7061 op = BIT_IOR_EXPR;
7062 break;
7063
7064 default:
7065 /* Nothing else is an assignment operator. */
7066 op = ERROR_MARK;
7067 }
7068
7069 /* If it was an assignment operator, consume it. */
7070 if (op != ERROR_MARK)
7071 cp_lexer_consume_token (parser->lexer);
7072
7073 return op;
7074 }
7075
7076 /* Parse an expression.
7077
7078 expression:
7079 assignment-expression
7080 expression , assignment-expression
7081
7082 CAST_P is true if this expression is the target of a cast.
7083
7084 Returns a representation of the expression. */
7085
7086 static tree
7087 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7088 {
7089 tree expression = NULL_TREE;
7090
7091 while (true)
7092 {
7093 tree assignment_expression;
7094
7095 /* Parse the next assignment-expression. */
7096 assignment_expression
7097 = cp_parser_assignment_expression (parser, cast_p, pidk);
7098 /* If this is the first assignment-expression, we can just
7099 save it away. */
7100 if (!expression)
7101 expression = assignment_expression;
7102 else
7103 expression = build_x_compound_expr (expression,
7104 assignment_expression,
7105 tf_warning_or_error);
7106 /* If the next token is not a comma, then we are done with the
7107 expression. */
7108 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7109 break;
7110 /* Consume the `,'. */
7111 cp_lexer_consume_token (parser->lexer);
7112 /* A comma operator cannot appear in a constant-expression. */
7113 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7114 expression = error_mark_node;
7115 }
7116
7117 return expression;
7118 }
7119
7120 /* Parse a constant-expression.
7121
7122 constant-expression:
7123 conditional-expression
7124
7125 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7126 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7127 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7128 is false, NON_CONSTANT_P should be NULL. */
7129
7130 static tree
7131 cp_parser_constant_expression (cp_parser* parser,
7132 bool allow_non_constant_p,
7133 bool *non_constant_p)
7134 {
7135 bool saved_integral_constant_expression_p;
7136 bool saved_allow_non_integral_constant_expression_p;
7137 bool saved_non_integral_constant_expression_p;
7138 tree expression;
7139
7140 /* It might seem that we could simply parse the
7141 conditional-expression, and then check to see if it were
7142 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7143 one that the compiler can figure out is constant, possibly after
7144 doing some simplifications or optimizations. The standard has a
7145 precise definition of constant-expression, and we must honor
7146 that, even though it is somewhat more restrictive.
7147
7148 For example:
7149
7150 int i[(2, 3)];
7151
7152 is not a legal declaration, because `(2, 3)' is not a
7153 constant-expression. The `,' operator is forbidden in a
7154 constant-expression. However, GCC's constant-folding machinery
7155 will fold this operation to an INTEGER_CST for `3'. */
7156
7157 /* Save the old settings. */
7158 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7159 saved_allow_non_integral_constant_expression_p
7160 = parser->allow_non_integral_constant_expression_p;
7161 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7162 /* We are now parsing a constant-expression. */
7163 parser->integral_constant_expression_p = true;
7164 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
7165 parser->non_integral_constant_expression_p = false;
7166 /* Although the grammar says "conditional-expression", we parse an
7167 "assignment-expression", which also permits "throw-expression"
7168 and the use of assignment operators. In the case that
7169 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7170 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7171 actually essential that we look for an assignment-expression.
7172 For example, cp_parser_initializer_clauses uses this function to
7173 determine whether a particular assignment-expression is in fact
7174 constant. */
7175 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7176 /* Restore the old settings. */
7177 parser->integral_constant_expression_p
7178 = saved_integral_constant_expression_p;
7179 parser->allow_non_integral_constant_expression_p
7180 = saved_allow_non_integral_constant_expression_p;
7181 if (allow_non_constant_p)
7182 *non_constant_p = parser->non_integral_constant_expression_p;
7183 else if (parser->non_integral_constant_expression_p)
7184 expression = error_mark_node;
7185 parser->non_integral_constant_expression_p
7186 = saved_non_integral_constant_expression_p;
7187
7188 return expression;
7189 }
7190
7191 /* Parse __builtin_offsetof.
7192
7193 offsetof-expression:
7194 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7195
7196 offsetof-member-designator:
7197 id-expression
7198 | offsetof-member-designator "." id-expression
7199 | offsetof-member-designator "[" expression "]"
7200 | offsetof-member-designator "->" id-expression */
7201
7202 static tree
7203 cp_parser_builtin_offsetof (cp_parser *parser)
7204 {
7205 int save_ice_p, save_non_ice_p;
7206 tree type, expr;
7207 cp_id_kind dummy;
7208 cp_token *token;
7209
7210 /* We're about to accept non-integral-constant things, but will
7211 definitely yield an integral constant expression. Save and
7212 restore these values around our local parsing. */
7213 save_ice_p = parser->integral_constant_expression_p;
7214 save_non_ice_p = parser->non_integral_constant_expression_p;
7215
7216 /* Consume the "__builtin_offsetof" token. */
7217 cp_lexer_consume_token (parser->lexer);
7218 /* Consume the opening `('. */
7219 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7220 /* Parse the type-id. */
7221 type = cp_parser_type_id (parser);
7222 /* Look for the `,'. */
7223 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7224 token = cp_lexer_peek_token (parser->lexer);
7225
7226 /* Build the (type *)null that begins the traditional offsetof macro. */
7227 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7228 tf_warning_or_error);
7229
7230 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7231 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7232 true, &dummy, token->location);
7233 while (true)
7234 {
7235 token = cp_lexer_peek_token (parser->lexer);
7236 switch (token->type)
7237 {
7238 case CPP_OPEN_SQUARE:
7239 /* offsetof-member-designator "[" expression "]" */
7240 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7241 break;
7242
7243 case CPP_DEREF:
7244 /* offsetof-member-designator "->" identifier */
7245 expr = grok_array_decl (expr, integer_zero_node);
7246 /* FALLTHRU */
7247
7248 case CPP_DOT:
7249 /* offsetof-member-designator "." identifier */
7250 cp_lexer_consume_token (parser->lexer);
7251 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7252 expr, true, &dummy,
7253 token->location);
7254 break;
7255
7256 case CPP_CLOSE_PAREN:
7257 /* Consume the ")" token. */
7258 cp_lexer_consume_token (parser->lexer);
7259 goto success;
7260
7261 default:
7262 /* Error. We know the following require will fail, but
7263 that gives the proper error message. */
7264 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7265 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7266 expr = error_mark_node;
7267 goto failure;
7268 }
7269 }
7270
7271 success:
7272 /* If we're processing a template, we can't finish the semantics yet.
7273 Otherwise we can fold the entire expression now. */
7274 if (processing_template_decl)
7275 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7276 else
7277 expr = finish_offsetof (expr);
7278
7279 failure:
7280 parser->integral_constant_expression_p = save_ice_p;
7281 parser->non_integral_constant_expression_p = save_non_ice_p;
7282
7283 return expr;
7284 }
7285
7286 /* Parse a trait expression. */
7287
7288 static tree
7289 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7290 {
7291 cp_trait_kind kind;
7292 tree type1, type2 = NULL_TREE;
7293 bool binary = false;
7294 cp_decl_specifier_seq decl_specs;
7295
7296 switch (keyword)
7297 {
7298 case RID_HAS_NOTHROW_ASSIGN:
7299 kind = CPTK_HAS_NOTHROW_ASSIGN;
7300 break;
7301 case RID_HAS_NOTHROW_CONSTRUCTOR:
7302 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7303 break;
7304 case RID_HAS_NOTHROW_COPY:
7305 kind = CPTK_HAS_NOTHROW_COPY;
7306 break;
7307 case RID_HAS_TRIVIAL_ASSIGN:
7308 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7309 break;
7310 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7311 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7312 break;
7313 case RID_HAS_TRIVIAL_COPY:
7314 kind = CPTK_HAS_TRIVIAL_COPY;
7315 break;
7316 case RID_HAS_TRIVIAL_DESTRUCTOR:
7317 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7318 break;
7319 case RID_HAS_VIRTUAL_DESTRUCTOR:
7320 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7321 break;
7322 case RID_IS_ABSTRACT:
7323 kind = CPTK_IS_ABSTRACT;
7324 break;
7325 case RID_IS_BASE_OF:
7326 kind = CPTK_IS_BASE_OF;
7327 binary = true;
7328 break;
7329 case RID_IS_CLASS:
7330 kind = CPTK_IS_CLASS;
7331 break;
7332 case RID_IS_CONVERTIBLE_TO:
7333 kind = CPTK_IS_CONVERTIBLE_TO;
7334 binary = true;
7335 break;
7336 case RID_IS_EMPTY:
7337 kind = CPTK_IS_EMPTY;
7338 break;
7339 case RID_IS_ENUM:
7340 kind = CPTK_IS_ENUM;
7341 break;
7342 case RID_IS_POD:
7343 kind = CPTK_IS_POD;
7344 break;
7345 case RID_IS_POLYMORPHIC:
7346 kind = CPTK_IS_POLYMORPHIC;
7347 break;
7348 case RID_IS_STD_LAYOUT:
7349 kind = CPTK_IS_STD_LAYOUT;
7350 break;
7351 case RID_IS_TRIVIAL:
7352 kind = CPTK_IS_TRIVIAL;
7353 break;
7354 case RID_IS_UNION:
7355 kind = CPTK_IS_UNION;
7356 break;
7357 default:
7358 gcc_unreachable ();
7359 }
7360
7361 /* Consume the token. */
7362 cp_lexer_consume_token (parser->lexer);
7363
7364 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7365
7366 type1 = cp_parser_type_id (parser);
7367
7368 if (type1 == error_mark_node)
7369 return error_mark_node;
7370
7371 /* Build a trivial decl-specifier-seq. */
7372 clear_decl_specs (&decl_specs);
7373 decl_specs.type = type1;
7374
7375 /* Call grokdeclarator to figure out what type this is. */
7376 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7377 /*initialized=*/0, /*attrlist=*/NULL);
7378
7379 if (binary)
7380 {
7381 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7382
7383 type2 = cp_parser_type_id (parser);
7384
7385 if (type2 == error_mark_node)
7386 return error_mark_node;
7387
7388 /* Build a trivial decl-specifier-seq. */
7389 clear_decl_specs (&decl_specs);
7390 decl_specs.type = type2;
7391
7392 /* Call grokdeclarator to figure out what type this is. */
7393 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7394 /*initialized=*/0, /*attrlist=*/NULL);
7395 }
7396
7397 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7398
7399 /* Complete the trait expression, which may mean either processing
7400 the trait expr now or saving it for template instantiation. */
7401 return finish_trait_expr (kind, type1, type2);
7402 }
7403
7404 /* Lambdas that appear in variable initializer or default argument scope
7405 get that in their mangling, so we need to record it. We might as well
7406 use the count for function and namespace scopes as well. */
7407 static GTY(()) tree lambda_scope;
7408 static GTY(()) int lambda_count;
7409 typedef struct GTY(()) tree_int
7410 {
7411 tree t;
7412 int i;
7413 } tree_int;
7414 DEF_VEC_O(tree_int);
7415 DEF_VEC_ALLOC_O(tree_int,gc);
7416 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7417
7418 static void
7419 start_lambda_scope (tree decl)
7420 {
7421 tree_int ti;
7422 gcc_assert (decl);
7423 /* Once we're inside a function, we ignore other scopes and just push
7424 the function again so that popping works properly. */
7425 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7426 decl = current_function_decl;
7427 ti.t = lambda_scope;
7428 ti.i = lambda_count;
7429 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7430 if (lambda_scope != decl)
7431 {
7432 /* Don't reset the count if we're still in the same function. */
7433 lambda_scope = decl;
7434 lambda_count = 0;
7435 }
7436 }
7437
7438 static void
7439 record_lambda_scope (tree lambda)
7440 {
7441 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7442 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7443 }
7444
7445 static void
7446 finish_lambda_scope (void)
7447 {
7448 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7449 if (lambda_scope != p->t)
7450 {
7451 lambda_scope = p->t;
7452 lambda_count = p->i;
7453 }
7454 VEC_pop (tree_int, lambda_scope_stack);
7455 }
7456
7457 /* Parse a lambda expression.
7458
7459 lambda-expression:
7460 lambda-introducer lambda-declarator [opt] compound-statement
7461
7462 Returns a representation of the expression. */
7463
7464 static tree
7465 cp_parser_lambda_expression (cp_parser* parser)
7466 {
7467 tree lambda_expr = build_lambda_expr ();
7468 tree type;
7469
7470 LAMBDA_EXPR_LOCATION (lambda_expr)
7471 = cp_lexer_peek_token (parser->lexer)->location;
7472
7473 if (cp_unevaluated_operand)
7474 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7475 "lambda-expression in unevaluated context");
7476
7477 /* We may be in the middle of deferred access check. Disable
7478 it now. */
7479 push_deferring_access_checks (dk_no_deferred);
7480
7481 cp_parser_lambda_introducer (parser, lambda_expr);
7482
7483 type = begin_lambda_type (lambda_expr);
7484
7485 record_lambda_scope (lambda_expr);
7486
7487 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7488 determine_visibility (TYPE_NAME (type));
7489
7490 /* Now that we've started the type, add the capture fields for any
7491 explicit captures. */
7492 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7493
7494 {
7495 /* Inside the class, surrounding template-parameter-lists do not apply. */
7496 unsigned int saved_num_template_parameter_lists
7497 = parser->num_template_parameter_lists;
7498
7499 parser->num_template_parameter_lists = 0;
7500
7501 /* By virtue of defining a local class, a lambda expression has access to
7502 the private variables of enclosing classes. */
7503
7504 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7505
7506 cp_parser_lambda_body (parser, lambda_expr);
7507
7508 /* The capture list was built up in reverse order; fix that now. */
7509 {
7510 tree newlist = NULL_TREE;
7511 tree elt, next;
7512
7513 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7514 elt; elt = next)
7515 {
7516 tree field = TREE_PURPOSE (elt);
7517 char *buf;
7518
7519 next = TREE_CHAIN (elt);
7520 TREE_CHAIN (elt) = newlist;
7521 newlist = elt;
7522
7523 /* Also add __ to the beginning of the field name so that code
7524 outside the lambda body can't see the captured name. We could
7525 just remove the name entirely, but this is more useful for
7526 debugging. */
7527 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7528 /* The 'this' capture already starts with __. */
7529 continue;
7530
7531 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7532 buf[1] = buf[0] = '_';
7533 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7534 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7535 DECL_NAME (field) = get_identifier (buf);
7536 }
7537 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7538 }
7539
7540 maybe_add_lambda_conv_op (type);
7541
7542 type = finish_struct (type, /*attributes=*/NULL_TREE);
7543
7544 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7545 }
7546
7547 pop_deferring_access_checks ();
7548
7549 return build_lambda_object (lambda_expr);
7550 }
7551
7552 /* Parse the beginning of a lambda expression.
7553
7554 lambda-introducer:
7555 [ lambda-capture [opt] ]
7556
7557 LAMBDA_EXPR is the current representation of the lambda expression. */
7558
7559 static void
7560 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7561 {
7562 /* Need commas after the first capture. */
7563 bool first = true;
7564
7565 /* Eat the leading `['. */
7566 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7567
7568 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7569 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7570 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7571 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7572 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7573 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7574
7575 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7576 {
7577 cp_lexer_consume_token (parser->lexer);
7578 first = false;
7579 }
7580
7581 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7582 {
7583 cp_token* capture_token;
7584 tree capture_id;
7585 tree capture_init_expr;
7586 cp_id_kind idk = CP_ID_KIND_NONE;
7587 bool explicit_init_p = false;
7588
7589 enum capture_kind_type
7590 {
7591 BY_COPY,
7592 BY_REFERENCE
7593 };
7594 enum capture_kind_type capture_kind = BY_COPY;
7595
7596 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7597 {
7598 error ("expected end of capture-list");
7599 return;
7600 }
7601
7602 if (first)
7603 first = false;
7604 else
7605 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7606
7607 /* Possibly capture `this'. */
7608 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7609 {
7610 cp_lexer_consume_token (parser->lexer);
7611 add_capture (lambda_expr,
7612 /*id=*/get_identifier ("__this"),
7613 /*initializer=*/finish_this_expr(),
7614 /*by_reference_p=*/false,
7615 explicit_init_p);
7616 continue;
7617 }
7618
7619 /* Remember whether we want to capture as a reference or not. */
7620 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7621 {
7622 capture_kind = BY_REFERENCE;
7623 cp_lexer_consume_token (parser->lexer);
7624 }
7625
7626 /* Get the identifier. */
7627 capture_token = cp_lexer_peek_token (parser->lexer);
7628 capture_id = cp_parser_identifier (parser);
7629
7630 if (capture_id == error_mark_node)
7631 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7632 delimiters, but I modified this to stop on unnested ']' as well. It
7633 was already changed to stop on unnested '}', so the
7634 "closing_parenthesis" name is no more misleading with my change. */
7635 {
7636 cp_parser_skip_to_closing_parenthesis (parser,
7637 /*recovering=*/true,
7638 /*or_comma=*/true,
7639 /*consume_paren=*/true);
7640 break;
7641 }
7642
7643 /* Find the initializer for this capture. */
7644 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7645 {
7646 /* An explicit expression exists. */
7647 cp_lexer_consume_token (parser->lexer);
7648 pedwarn (input_location, OPT_pedantic,
7649 "ISO C++ does not allow initializers "
7650 "in lambda expression capture lists");
7651 capture_init_expr = cp_parser_assignment_expression (parser,
7652 /*cast_p=*/true,
7653 &idk);
7654 explicit_init_p = true;
7655 }
7656 else
7657 {
7658 const char* error_msg;
7659
7660 /* Turn the identifier into an id-expression. */
7661 capture_init_expr
7662 = cp_parser_lookup_name
7663 (parser,
7664 capture_id,
7665 none_type,
7666 /*is_template=*/false,
7667 /*is_namespace=*/false,
7668 /*check_dependency=*/true,
7669 /*ambiguous_decls=*/NULL,
7670 capture_token->location);
7671
7672 capture_init_expr
7673 = finish_id_expression
7674 (capture_id,
7675 capture_init_expr,
7676 parser->scope,
7677 &idk,
7678 /*integral_constant_expression_p=*/false,
7679 /*allow_non_integral_constant_expression_p=*/false,
7680 /*non_integral_constant_expression_p=*/NULL,
7681 /*template_p=*/false,
7682 /*done=*/true,
7683 /*address_p=*/false,
7684 /*template_arg_p=*/false,
7685 &error_msg,
7686 capture_token->location);
7687 }
7688
7689 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7690 capture_init_expr
7691 = unqualified_name_lookup_error (capture_init_expr);
7692
7693 add_capture (lambda_expr,
7694 capture_id,
7695 capture_init_expr,
7696 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7697 explicit_init_p);
7698 }
7699
7700 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7701 }
7702
7703 /* Parse the (optional) middle of a lambda expression.
7704
7705 lambda-declarator:
7706 ( parameter-declaration-clause [opt] )
7707 attribute-specifier [opt]
7708 mutable [opt]
7709 exception-specification [opt]
7710 lambda-return-type-clause [opt]
7711
7712 LAMBDA_EXPR is the current representation of the lambda expression. */
7713
7714 static void
7715 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7716 {
7717 /* 5.1.1.4 of the standard says:
7718 If a lambda-expression does not include a lambda-declarator, it is as if
7719 the lambda-declarator were ().
7720 This means an empty parameter list, no attributes, and no exception
7721 specification. */
7722 tree param_list = void_list_node;
7723 tree attributes = NULL_TREE;
7724 tree exception_spec = NULL_TREE;
7725 tree t;
7726
7727 /* The lambda-declarator is optional, but must begin with an opening
7728 parenthesis if present. */
7729 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7730 {
7731 cp_lexer_consume_token (parser->lexer);
7732
7733 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7734
7735 /* Parse parameters. */
7736 param_list = cp_parser_parameter_declaration_clause (parser);
7737
7738 /* Default arguments shall not be specified in the
7739 parameter-declaration-clause of a lambda-declarator. */
7740 for (t = param_list; t; t = TREE_CHAIN (t))
7741 if (TREE_PURPOSE (t))
7742 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7743 "default argument specified for lambda parameter");
7744
7745 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7746
7747 attributes = cp_parser_attributes_opt (parser);
7748
7749 /* Parse optional `mutable' keyword. */
7750 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7751 {
7752 cp_lexer_consume_token (parser->lexer);
7753 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7754 }
7755
7756 /* Parse optional exception specification. */
7757 exception_spec = cp_parser_exception_specification_opt (parser);
7758
7759 /* Parse optional trailing return type. */
7760 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7761 {
7762 cp_lexer_consume_token (parser->lexer);
7763 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7764 }
7765
7766 /* The function parameters must be in scope all the way until after the
7767 trailing-return-type in case of decltype. */
7768 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7769 pop_binding (DECL_NAME (t), t);
7770
7771 leave_scope ();
7772 }
7773
7774 /* Create the function call operator.
7775
7776 Messing with declarators like this is no uglier than building up the
7777 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7778 other code. */
7779 {
7780 cp_decl_specifier_seq return_type_specs;
7781 cp_declarator* declarator;
7782 tree fco;
7783 int quals;
7784 void *p;
7785
7786 clear_decl_specs (&return_type_specs);
7787 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7788 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7789 else
7790 /* Maybe we will deduce the return type later, but we can use void
7791 as a placeholder return type anyways. */
7792 return_type_specs.type = void_type_node;
7793
7794 p = obstack_alloc (&declarator_obstack, 0);
7795
7796 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7797 sfk_none);
7798
7799 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7800 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7801 declarator = make_call_declarator (declarator, param_list, quals,
7802 exception_spec,
7803 /*late_return_type=*/NULL_TREE);
7804 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7805
7806 fco = grokmethod (&return_type_specs,
7807 declarator,
7808 attributes);
7809 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7810 DECL_ARTIFICIAL (fco) = 1;
7811
7812 finish_member_declaration (fco);
7813
7814 obstack_free (&declarator_obstack, p);
7815 }
7816 }
7817
7818 /* Parse the body of a lambda expression, which is simply
7819
7820 compound-statement
7821
7822 but which requires special handling.
7823 LAMBDA_EXPR is the current representation of the lambda expression. */
7824
7825 static void
7826 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7827 {
7828 bool nested = (current_function_decl != NULL_TREE);
7829 if (nested)
7830 push_function_context ();
7831
7832 /* Finish the function call operator
7833 - class_specifier
7834 + late_parsing_for_member
7835 + function_definition_after_declarator
7836 + ctor_initializer_opt_and_function_body */
7837 {
7838 tree fco = lambda_function (lambda_expr);
7839 tree body;
7840 bool done = false;
7841
7842 /* Let the front end know that we are going to be defining this
7843 function. */
7844 start_preparsed_function (fco,
7845 NULL_TREE,
7846 SF_PRE_PARSED | SF_INCLASS_INLINE);
7847
7848 start_lambda_scope (fco);
7849 body = begin_function_body ();
7850
7851 /* 5.1.1.4 of the standard says:
7852 If a lambda-expression does not include a trailing-return-type, it
7853 is as if the trailing-return-type denotes the following type:
7854 * if the compound-statement is of the form
7855 { return attribute-specifier [opt] expression ; }
7856 the type of the returned expression after lvalue-to-rvalue
7857 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7858 (_conv.array_ 4.2), and function-to-pointer conversion
7859 (_conv.func_ 4.3);
7860 * otherwise, void. */
7861
7862 /* In a lambda that has neither a lambda-return-type-clause
7863 nor a deducible form, errors should be reported for return statements
7864 in the body. Since we used void as the placeholder return type, parsing
7865 the body as usual will give such desired behavior. */
7866 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7867 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7868 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7869 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7870 {
7871 tree compound_stmt;
7872 tree expr = NULL_TREE;
7873 cp_id_kind idk = CP_ID_KIND_NONE;
7874
7875 /* Parse tentatively in case there's more after the initial return
7876 statement. */
7877 cp_parser_parse_tentatively (parser);
7878
7879 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7880 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7881
7882 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7883
7884 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7885 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7886
7887 if (cp_parser_parse_definitely (parser))
7888 {
7889 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7890
7891 compound_stmt = begin_compound_stmt (0);
7892 /* Will get error here if type not deduced yet. */
7893 finish_return_stmt (expr);
7894 finish_compound_stmt (compound_stmt);
7895
7896 done = true;
7897 }
7898 }
7899
7900 if (!done)
7901 {
7902 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7903 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7904 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7905 cp_parser_compound_stmt does not pass it. */
7906 cp_parser_function_body (parser);
7907 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7908 }
7909
7910 finish_function_body (body);
7911 finish_lambda_scope ();
7912
7913 /* Finish the function and generate code for it if necessary. */
7914 expand_or_defer_fn (finish_function (/*inline*/2));
7915 }
7916
7917 if (nested)
7918 pop_function_context();
7919 }
7920
7921 /* Statements [gram.stmt.stmt] */
7922
7923 /* Parse a statement.
7924
7925 statement:
7926 labeled-statement
7927 expression-statement
7928 compound-statement
7929 selection-statement
7930 iteration-statement
7931 jump-statement
7932 declaration-statement
7933 try-block
7934
7935 IN_COMPOUND is true when the statement is nested inside a
7936 cp_parser_compound_statement; this matters for certain pragmas.
7937
7938 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7939 is a (possibly labeled) if statement which is not enclosed in braces
7940 and has an else clause. This is used to implement -Wparentheses. */
7941
7942 static void
7943 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7944 bool in_compound, bool *if_p)
7945 {
7946 tree statement;
7947 cp_token *token;
7948 location_t statement_location;
7949
7950 restart:
7951 if (if_p != NULL)
7952 *if_p = false;
7953 /* There is no statement yet. */
7954 statement = NULL_TREE;
7955 /* Peek at the next token. */
7956 token = cp_lexer_peek_token (parser->lexer);
7957 /* Remember the location of the first token in the statement. */
7958 statement_location = token->location;
7959 /* If this is a keyword, then that will often determine what kind of
7960 statement we have. */
7961 if (token->type == CPP_KEYWORD)
7962 {
7963 enum rid keyword = token->keyword;
7964
7965 switch (keyword)
7966 {
7967 case RID_CASE:
7968 case RID_DEFAULT:
7969 /* Looks like a labeled-statement with a case label.
7970 Parse the label, and then use tail recursion to parse
7971 the statement. */
7972 cp_parser_label_for_labeled_statement (parser);
7973 goto restart;
7974
7975 case RID_IF:
7976 case RID_SWITCH:
7977 statement = cp_parser_selection_statement (parser, if_p);
7978 break;
7979
7980 case RID_WHILE:
7981 case RID_DO:
7982 case RID_FOR:
7983 statement = cp_parser_iteration_statement (parser);
7984 break;
7985
7986 case RID_BREAK:
7987 case RID_CONTINUE:
7988 case RID_RETURN:
7989 case RID_GOTO:
7990 statement = cp_parser_jump_statement (parser);
7991 break;
7992
7993 /* Objective-C++ exception-handling constructs. */
7994 case RID_AT_TRY:
7995 case RID_AT_CATCH:
7996 case RID_AT_FINALLY:
7997 case RID_AT_SYNCHRONIZED:
7998 case RID_AT_THROW:
7999 statement = cp_parser_objc_statement (parser);
8000 break;
8001
8002 case RID_TRY:
8003 statement = cp_parser_try_block (parser);
8004 break;
8005
8006 case RID_NAMESPACE:
8007 /* This must be a namespace alias definition. */
8008 cp_parser_declaration_statement (parser);
8009 return;
8010
8011 default:
8012 /* It might be a keyword like `int' that can start a
8013 declaration-statement. */
8014 break;
8015 }
8016 }
8017 else if (token->type == CPP_NAME)
8018 {
8019 /* If the next token is a `:', then we are looking at a
8020 labeled-statement. */
8021 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8022 if (token->type == CPP_COLON)
8023 {
8024 /* Looks like a labeled-statement with an ordinary label.
8025 Parse the label, and then use tail recursion to parse
8026 the statement. */
8027 cp_parser_label_for_labeled_statement (parser);
8028 goto restart;
8029 }
8030 }
8031 /* Anything that starts with a `{' must be a compound-statement. */
8032 else if (token->type == CPP_OPEN_BRACE)
8033 statement = cp_parser_compound_statement (parser, NULL, false);
8034 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8035 a statement all its own. */
8036 else if (token->type == CPP_PRAGMA)
8037 {
8038 /* Only certain OpenMP pragmas are attached to statements, and thus
8039 are considered statements themselves. All others are not. In
8040 the context of a compound, accept the pragma as a "statement" and
8041 return so that we can check for a close brace. Otherwise we
8042 require a real statement and must go back and read one. */
8043 if (in_compound)
8044 cp_parser_pragma (parser, pragma_compound);
8045 else if (!cp_parser_pragma (parser, pragma_stmt))
8046 goto restart;
8047 return;
8048 }
8049 else if (token->type == CPP_EOF)
8050 {
8051 cp_parser_error (parser, "expected statement");
8052 return;
8053 }
8054
8055 /* Everything else must be a declaration-statement or an
8056 expression-statement. Try for the declaration-statement
8057 first, unless we are looking at a `;', in which case we know that
8058 we have an expression-statement. */
8059 if (!statement)
8060 {
8061 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8062 {
8063 cp_parser_parse_tentatively (parser);
8064 /* Try to parse the declaration-statement. */
8065 cp_parser_declaration_statement (parser);
8066 /* If that worked, we're done. */
8067 if (cp_parser_parse_definitely (parser))
8068 return;
8069 }
8070 /* Look for an expression-statement instead. */
8071 statement = cp_parser_expression_statement (parser, in_statement_expr);
8072 }
8073
8074 /* Set the line number for the statement. */
8075 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8076 SET_EXPR_LOCATION (statement, statement_location);
8077 }
8078
8079 /* Parse the label for a labeled-statement, i.e.
8080
8081 identifier :
8082 case constant-expression :
8083 default :
8084
8085 GNU Extension:
8086 case constant-expression ... constant-expression : statement
8087
8088 When a label is parsed without errors, the label is added to the
8089 parse tree by the finish_* functions, so this function doesn't
8090 have to return the label. */
8091
8092 static void
8093 cp_parser_label_for_labeled_statement (cp_parser* parser)
8094 {
8095 cp_token *token;
8096 tree label = NULL_TREE;
8097
8098 /* The next token should be an identifier. */
8099 token = cp_lexer_peek_token (parser->lexer);
8100 if (token->type != CPP_NAME
8101 && token->type != CPP_KEYWORD)
8102 {
8103 cp_parser_error (parser, "expected labeled-statement");
8104 return;
8105 }
8106
8107 switch (token->keyword)
8108 {
8109 case RID_CASE:
8110 {
8111 tree expr, expr_hi;
8112 cp_token *ellipsis;
8113
8114 /* Consume the `case' token. */
8115 cp_lexer_consume_token (parser->lexer);
8116 /* Parse the constant-expression. */
8117 expr = cp_parser_constant_expression (parser,
8118 /*allow_non_constant_p=*/false,
8119 NULL);
8120
8121 ellipsis = cp_lexer_peek_token (parser->lexer);
8122 if (ellipsis->type == CPP_ELLIPSIS)
8123 {
8124 /* Consume the `...' token. */
8125 cp_lexer_consume_token (parser->lexer);
8126 expr_hi =
8127 cp_parser_constant_expression (parser,
8128 /*allow_non_constant_p=*/false,
8129 NULL);
8130 /* We don't need to emit warnings here, as the common code
8131 will do this for us. */
8132 }
8133 else
8134 expr_hi = NULL_TREE;
8135
8136 if (parser->in_switch_statement_p)
8137 finish_case_label (token->location, expr, expr_hi);
8138 else
8139 error_at (token->location,
8140 "case label %qE not within a switch statement",
8141 expr);
8142 }
8143 break;
8144
8145 case RID_DEFAULT:
8146 /* Consume the `default' token. */
8147 cp_lexer_consume_token (parser->lexer);
8148
8149 if (parser->in_switch_statement_p)
8150 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8151 else
8152 error_at (token->location, "case label not within a switch statement");
8153 break;
8154
8155 default:
8156 /* Anything else must be an ordinary label. */
8157 label = finish_label_stmt (cp_parser_identifier (parser));
8158 break;
8159 }
8160
8161 /* Require the `:' token. */
8162 cp_parser_require (parser, CPP_COLON, RT_COLON);
8163
8164 /* An ordinary label may optionally be followed by attributes.
8165 However, this is only permitted if the attributes are then
8166 followed by a semicolon. This is because, for backward
8167 compatibility, when parsing
8168 lab: __attribute__ ((unused)) int i;
8169 we want the attribute to attach to "i", not "lab". */
8170 if (label != NULL_TREE
8171 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8172 {
8173 tree attrs;
8174
8175 cp_parser_parse_tentatively (parser);
8176 attrs = cp_parser_attributes_opt (parser);
8177 if (attrs == NULL_TREE
8178 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8179 cp_parser_abort_tentative_parse (parser);
8180 else if (!cp_parser_parse_definitely (parser))
8181 ;
8182 else
8183 cplus_decl_attributes (&label, attrs, 0);
8184 }
8185 }
8186
8187 /* Parse an expression-statement.
8188
8189 expression-statement:
8190 expression [opt] ;
8191
8192 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8193 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8194 indicates whether this expression-statement is part of an
8195 expression statement. */
8196
8197 static tree
8198 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8199 {
8200 tree statement = NULL_TREE;
8201 cp_token *token = cp_lexer_peek_token (parser->lexer);
8202
8203 /* If the next token is a ';', then there is no expression
8204 statement. */
8205 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8206 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8207
8208 /* Give a helpful message for "A<T>::type t;" and the like. */
8209 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8210 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8211 {
8212 if (TREE_CODE (statement) == SCOPE_REF)
8213 error_at (token->location, "need %<typename%> before %qE because "
8214 "%qT is a dependent scope",
8215 statement, TREE_OPERAND (statement, 0));
8216 else if (is_overloaded_fn (statement)
8217 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8218 {
8219 /* A::A a; */
8220 tree fn = get_first_fn (statement);
8221 error_at (token->location,
8222 "%<%T::%D%> names the constructor, not the type",
8223 DECL_CONTEXT (fn), DECL_NAME (fn));
8224 }
8225 }
8226
8227 /* Consume the final `;'. */
8228 cp_parser_consume_semicolon_at_end_of_statement (parser);
8229
8230 if (in_statement_expr
8231 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8232 /* This is the final expression statement of a statement
8233 expression. */
8234 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8235 else if (statement)
8236 statement = finish_expr_stmt (statement);
8237 else
8238 finish_stmt ();
8239
8240 return statement;
8241 }
8242
8243 /* Parse a compound-statement.
8244
8245 compound-statement:
8246 { statement-seq [opt] }
8247
8248 GNU extension:
8249
8250 compound-statement:
8251 { label-declaration-seq [opt] statement-seq [opt] }
8252
8253 label-declaration-seq:
8254 label-declaration
8255 label-declaration-seq label-declaration
8256
8257 Returns a tree representing the statement. */
8258
8259 static tree
8260 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8261 bool in_try)
8262 {
8263 tree compound_stmt;
8264
8265 /* Consume the `{'. */
8266 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8267 return error_mark_node;
8268 /* Begin the compound-statement. */
8269 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8270 /* If the next keyword is `__label__' we have a label declaration. */
8271 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8272 cp_parser_label_declaration (parser);
8273 /* Parse an (optional) statement-seq. */
8274 cp_parser_statement_seq_opt (parser, in_statement_expr);
8275 /* Finish the compound-statement. */
8276 finish_compound_stmt (compound_stmt);
8277 /* Consume the `}'. */
8278 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8279
8280 return compound_stmt;
8281 }
8282
8283 /* Parse an (optional) statement-seq.
8284
8285 statement-seq:
8286 statement
8287 statement-seq [opt] statement */
8288
8289 static void
8290 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8291 {
8292 /* Scan statements until there aren't any more. */
8293 while (true)
8294 {
8295 cp_token *token = cp_lexer_peek_token (parser->lexer);
8296
8297 /* If we are looking at a `}', then we have run out of
8298 statements; the same is true if we have reached the end
8299 of file, or have stumbled upon a stray '@end'. */
8300 if (token->type == CPP_CLOSE_BRACE
8301 || token->type == CPP_EOF
8302 || token->type == CPP_PRAGMA_EOL
8303 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8304 break;
8305
8306 /* If we are in a compound statement and find 'else' then
8307 something went wrong. */
8308 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8309 {
8310 if (parser->in_statement & IN_IF_STMT)
8311 break;
8312 else
8313 {
8314 token = cp_lexer_consume_token (parser->lexer);
8315 error_at (token->location, "%<else%> without a previous %<if%>");
8316 }
8317 }
8318
8319 /* Parse the statement. */
8320 cp_parser_statement (parser, in_statement_expr, true, NULL);
8321 }
8322 }
8323
8324 /* Parse a selection-statement.
8325
8326 selection-statement:
8327 if ( condition ) statement
8328 if ( condition ) statement else statement
8329 switch ( condition ) statement
8330
8331 Returns the new IF_STMT or SWITCH_STMT.
8332
8333 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8334 is a (possibly labeled) if statement which is not enclosed in
8335 braces and has an else clause. This is used to implement
8336 -Wparentheses. */
8337
8338 static tree
8339 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8340 {
8341 cp_token *token;
8342 enum rid keyword;
8343
8344 if (if_p != NULL)
8345 *if_p = false;
8346
8347 /* Peek at the next token. */
8348 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8349
8350 /* See what kind of keyword it is. */
8351 keyword = token->keyword;
8352 switch (keyword)
8353 {
8354 case RID_IF:
8355 case RID_SWITCH:
8356 {
8357 tree statement;
8358 tree condition;
8359
8360 /* Look for the `('. */
8361 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8362 {
8363 cp_parser_skip_to_end_of_statement (parser);
8364 return error_mark_node;
8365 }
8366
8367 /* Begin the selection-statement. */
8368 if (keyword == RID_IF)
8369 statement = begin_if_stmt ();
8370 else
8371 statement = begin_switch_stmt ();
8372
8373 /* Parse the condition. */
8374 condition = cp_parser_condition (parser);
8375 /* Look for the `)'. */
8376 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8377 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8378 /*consume_paren=*/true);
8379
8380 if (keyword == RID_IF)
8381 {
8382 bool nested_if;
8383 unsigned char in_statement;
8384
8385 /* Add the condition. */
8386 finish_if_stmt_cond (condition, statement);
8387
8388 /* Parse the then-clause. */
8389 in_statement = parser->in_statement;
8390 parser->in_statement |= IN_IF_STMT;
8391 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8392 {
8393 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8394 add_stmt (build_empty_stmt (loc));
8395 cp_lexer_consume_token (parser->lexer);
8396 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8397 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8398 "empty body in an %<if%> statement");
8399 nested_if = false;
8400 }
8401 else
8402 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8403 parser->in_statement = in_statement;
8404
8405 finish_then_clause (statement);
8406
8407 /* If the next token is `else', parse the else-clause. */
8408 if (cp_lexer_next_token_is_keyword (parser->lexer,
8409 RID_ELSE))
8410 {
8411 /* Consume the `else' keyword. */
8412 cp_lexer_consume_token (parser->lexer);
8413 begin_else_clause (statement);
8414 /* Parse the else-clause. */
8415 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8416 {
8417 location_t loc;
8418 loc = cp_lexer_peek_token (parser->lexer)->location;
8419 warning_at (loc,
8420 OPT_Wempty_body, "suggest braces around "
8421 "empty body in an %<else%> statement");
8422 add_stmt (build_empty_stmt (loc));
8423 cp_lexer_consume_token (parser->lexer);
8424 }
8425 else
8426 cp_parser_implicitly_scoped_statement (parser, NULL);
8427
8428 finish_else_clause (statement);
8429
8430 /* If we are currently parsing a then-clause, then
8431 IF_P will not be NULL. We set it to true to
8432 indicate that this if statement has an else clause.
8433 This may trigger the Wparentheses warning below
8434 when we get back up to the parent if statement. */
8435 if (if_p != NULL)
8436 *if_p = true;
8437 }
8438 else
8439 {
8440 /* This if statement does not have an else clause. If
8441 NESTED_IF is true, then the then-clause is an if
8442 statement which does have an else clause. We warn
8443 about the potential ambiguity. */
8444 if (nested_if)
8445 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8446 "suggest explicit braces to avoid ambiguous"
8447 " %<else%>");
8448 }
8449
8450 /* Now we're all done with the if-statement. */
8451 finish_if_stmt (statement);
8452 }
8453 else
8454 {
8455 bool in_switch_statement_p;
8456 unsigned char in_statement;
8457
8458 /* Add the condition. */
8459 finish_switch_cond (condition, statement);
8460
8461 /* Parse the body of the switch-statement. */
8462 in_switch_statement_p = parser->in_switch_statement_p;
8463 in_statement = parser->in_statement;
8464 parser->in_switch_statement_p = true;
8465 parser->in_statement |= IN_SWITCH_STMT;
8466 cp_parser_implicitly_scoped_statement (parser, NULL);
8467 parser->in_switch_statement_p = in_switch_statement_p;
8468 parser->in_statement = in_statement;
8469
8470 /* Now we're all done with the switch-statement. */
8471 finish_switch_stmt (statement);
8472 }
8473
8474 return statement;
8475 }
8476 break;
8477
8478 default:
8479 cp_parser_error (parser, "expected selection-statement");
8480 return error_mark_node;
8481 }
8482 }
8483
8484 /* Parse a condition.
8485
8486 condition:
8487 expression
8488 type-specifier-seq declarator = initializer-clause
8489 type-specifier-seq declarator braced-init-list
8490
8491 GNU Extension:
8492
8493 condition:
8494 type-specifier-seq declarator asm-specification [opt]
8495 attributes [opt] = assignment-expression
8496
8497 Returns the expression that should be tested. */
8498
8499 static tree
8500 cp_parser_condition (cp_parser* parser)
8501 {
8502 cp_decl_specifier_seq type_specifiers;
8503 const char *saved_message;
8504
8505 /* Try the declaration first. */
8506 cp_parser_parse_tentatively (parser);
8507 /* New types are not allowed in the type-specifier-seq for a
8508 condition. */
8509 saved_message = parser->type_definition_forbidden_message;
8510 parser->type_definition_forbidden_message
8511 = G_("types may not be defined in conditions");
8512 /* Parse the type-specifier-seq. */
8513 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8514 /*is_trailing_return=*/false,
8515 &type_specifiers);
8516 /* Restore the saved message. */
8517 parser->type_definition_forbidden_message = saved_message;
8518 /* If all is well, we might be looking at a declaration. */
8519 if (!cp_parser_error_occurred (parser))
8520 {
8521 tree decl;
8522 tree asm_specification;
8523 tree attributes;
8524 cp_declarator *declarator;
8525 tree initializer = NULL_TREE;
8526
8527 /* Parse the declarator. */
8528 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8529 /*ctor_dtor_or_conv_p=*/NULL,
8530 /*parenthesized_p=*/NULL,
8531 /*member_p=*/false);
8532 /* Parse the attributes. */
8533 attributes = cp_parser_attributes_opt (parser);
8534 /* Parse the asm-specification. */
8535 asm_specification = cp_parser_asm_specification_opt (parser);
8536 /* If the next token is not an `=' or '{', then we might still be
8537 looking at an expression. For example:
8538
8539 if (A(a).x)
8540
8541 looks like a decl-specifier-seq and a declarator -- but then
8542 there is no `=', so this is an expression. */
8543 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8544 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8545 cp_parser_simulate_error (parser);
8546
8547 /* If we did see an `=' or '{', then we are looking at a declaration
8548 for sure. */
8549 if (cp_parser_parse_definitely (parser))
8550 {
8551 tree pushed_scope;
8552 bool non_constant_p;
8553 bool flags = LOOKUP_ONLYCONVERTING;
8554
8555 /* Create the declaration. */
8556 decl = start_decl (declarator, &type_specifiers,
8557 /*initialized_p=*/true,
8558 attributes, /*prefix_attributes=*/NULL_TREE,
8559 &pushed_scope);
8560
8561 /* Parse the initializer. */
8562 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8563 {
8564 initializer = cp_parser_braced_list (parser, &non_constant_p);
8565 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8566 flags = 0;
8567 }
8568 else
8569 {
8570 /* Consume the `='. */
8571 cp_parser_require (parser, CPP_EQ, RT_EQ);
8572 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8573 }
8574 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8575 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8576
8577 if (!non_constant_p)
8578 initializer = fold_non_dependent_expr (initializer);
8579
8580 /* Process the initializer. */
8581 cp_finish_decl (decl,
8582 initializer, !non_constant_p,
8583 asm_specification,
8584 flags);
8585
8586 if (pushed_scope)
8587 pop_scope (pushed_scope);
8588
8589 return convert_from_reference (decl);
8590 }
8591 }
8592 /* If we didn't even get past the declarator successfully, we are
8593 definitely not looking at a declaration. */
8594 else
8595 cp_parser_abort_tentative_parse (parser);
8596
8597 /* Otherwise, we are looking at an expression. */
8598 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8599 }
8600
8601 /* Parses a traditional for-statement until the closing ')', not included. */
8602
8603 static tree
8604 cp_parser_c_for (cp_parser *parser)
8605 {
8606 /* Normal for loop */
8607 tree stmt;
8608 tree condition = NULL_TREE;
8609 tree expression = NULL_TREE;
8610
8611 /* Begin the for-statement. */
8612 stmt = begin_for_stmt ();
8613
8614 /* Parse the initialization. */
8615 cp_parser_for_init_statement (parser);
8616 finish_for_init_stmt (stmt);
8617
8618 /* If there's a condition, process it. */
8619 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8620 condition = cp_parser_condition (parser);
8621 finish_for_cond (condition, stmt);
8622 /* Look for the `;'. */
8623 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8624
8625 /* If there's an expression, process it. */
8626 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8627 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8628 finish_for_expr (expression, stmt);
8629
8630 return stmt;
8631 }
8632
8633 /* Tries to parse a range-based for-statement:
8634
8635 range-based-for:
8636 type-specifier-seq declarator : expression
8637
8638 If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8639 expression. Note that the *DECL is returned unfinished, so
8640 later you should call cp_finish_decl().
8641
8642 Returns TRUE iff a range-based for is parsed. */
8643
8644 static tree
8645 cp_parser_range_for (cp_parser *parser)
8646 {
8647 tree stmt, range_decl, range_expr;
8648 cp_decl_specifier_seq type_specifiers;
8649 cp_declarator *declarator;
8650 const char *saved_message;
8651 tree attributes, pushed_scope;
8652
8653 cp_parser_parse_tentatively (parser);
8654 /* New types are not allowed in the type-specifier-seq for a
8655 range-based for loop. */
8656 saved_message = parser->type_definition_forbidden_message;
8657 parser->type_definition_forbidden_message
8658 = G_("types may not be defined in range-based for loops");
8659 /* Parse the type-specifier-seq. */
8660 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8661 /*is_trailing_return=*/false,
8662 &type_specifiers);
8663 /* Restore the saved message. */
8664 parser->type_definition_forbidden_message = saved_message;
8665 /* If all is well, we might be looking at a declaration. */
8666 if (cp_parser_error_occurred (parser))
8667 {
8668 cp_parser_abort_tentative_parse (parser);
8669 return NULL_TREE;
8670 }
8671 /* Parse the declarator. */
8672 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8673 /*ctor_dtor_or_conv_p=*/NULL,
8674 /*parenthesized_p=*/NULL,
8675 /*member_p=*/false);
8676 /* Parse the attributes. */
8677 attributes = cp_parser_attributes_opt (parser);
8678 /* The next token should be `:'. */
8679 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8680 cp_parser_simulate_error (parser);
8681
8682 /* Check if it is a range-based for */
8683 if (!cp_parser_parse_definitely (parser))
8684 return NULL_TREE;
8685
8686 cp_parser_require (parser, CPP_COLON, RT_COLON);
8687 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8688 {
8689 bool expr_non_constant_p;
8690 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8691 }
8692 else
8693 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8694
8695 /* If in template, STMT is converted to a normal for-statements
8696 at instantiation. If not, it is done just ahead. */
8697 if (processing_template_decl)
8698 stmt = begin_range_for_stmt ();
8699 else
8700 stmt = begin_for_stmt ();
8701
8702 /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8703 range_decl = start_decl (declarator, &type_specifiers,
8704 /*initialized_p=*/SD_INITIALIZED,
8705 attributes, /*prefix_attributes=*/NULL_TREE,
8706 &pushed_scope);
8707 /* No scope allowed here */
8708 pop_scope (pushed_scope);
8709
8710 if (TREE_CODE (stmt) == RANGE_FOR_STMT)
8711 finish_range_for_decl (stmt, range_decl, range_expr);
8712 else
8713 /* Convert the range-based for loop into a normal for-statement. */
8714 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8715
8716 return stmt;
8717 }
8718
8719 /* Converts a range-based for-statement into a normal
8720 for-statement, as per the definition.
8721
8722 for (RANGE_DECL : RANGE_EXPR)
8723 BLOCK
8724
8725 should be equivalent to:
8726
8727 {
8728 auto &&__range = RANGE_EXPR;
8729 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8730 __begin != __end;
8731 ++__begin)
8732 {
8733 RANGE_DECL = *__begin;
8734 BLOCK
8735 }
8736 }
8737
8738 If RANGE_EXPR is an array:
8739 BEGIN_EXPR = __range
8740 END_EXPR = __range + ARRAY_SIZE(__range)
8741 Else:
8742 BEGIN_EXPR = begin(__range)
8743 END_EXPR = end(__range);
8744
8745 When calling begin()/end() we must use argument dependent
8746 lookup, but always considering 'std' as an associated namespace. */
8747
8748 tree
8749 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8750 {
8751 tree range_type, range_temp;
8752 tree begin, end;
8753 tree iter_type, begin_expr, end_expr;
8754 tree condition, expression;
8755
8756 /* Find out the type deduced by the declaration
8757 * `auto &&__range = range_expr' */
8758 range_type = cp_build_reference_type (make_auto (), true);
8759 range_type = do_auto_deduction (range_type, range_expr,
8760 type_uses_auto (range_type));
8761
8762 /* Create the __range variable */
8763 range_temp = build_decl (input_location, VAR_DECL,
8764 get_identifier ("__for_range"), range_type);
8765 TREE_USED (range_temp) = 1;
8766 DECL_ARTIFICIAL (range_temp) = 1;
8767 pushdecl (range_temp);
8768 finish_expr_stmt (cp_build_modify_expr (range_temp, INIT_EXPR, range_expr,
8769 tf_warning_or_error));
8770 range_temp = convert_from_reference (range_temp);
8771
8772 if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8773 {
8774 /* If RANGE_TEMP is an array we will use pointer arithmetic */
8775 iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8776 begin_expr = range_temp;
8777 end_expr
8778 = build_binary_op (input_location, PLUS_EXPR,
8779 range_temp,
8780 array_type_nelts_top (TREE_TYPE (range_temp)), 0);
8781 }
8782 else
8783 {
8784 /* If it is not an array, we must call begin(__range)/end__range() */
8785 VEC(tree,gc) *vec;
8786
8787 begin_expr = get_identifier ("begin");
8788 vec = make_tree_vector ();
8789 VEC_safe_push (tree, gc, vec, range_temp);
8790 begin_expr = perform_koenig_lookup (begin_expr, vec,
8791 /*include_std=*/true);
8792 begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8793 tf_warning_or_error);
8794 release_tree_vector (vec);
8795
8796 end_expr = get_identifier ("end");
8797 vec = make_tree_vector ();
8798 VEC_safe_push (tree, gc, vec, range_temp);
8799 end_expr = perform_koenig_lookup (end_expr, vec,
8800 /*include_std=*/true);
8801 end_expr = finish_call_expr (end_expr, &vec, false, true,
8802 tf_warning_or_error);
8803 release_tree_vector (vec);
8804
8805 /* The unqualified type of the __begin and __end temporaries should
8806 * be the same as required by the multiple auto declaration */
8807 iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8808 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8809 error ("inconsistent begin/end types in range-based for: %qT and %qT",
8810 TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8811 }
8812
8813 /* The new for initialization statement */
8814 begin = build_decl (input_location, VAR_DECL,
8815 get_identifier ("__for_begin"), iter_type);
8816 TREE_USED (begin) = 1;
8817 DECL_ARTIFICIAL (begin) = 1;
8818 pushdecl (begin);
8819 finish_expr_stmt (cp_build_modify_expr (begin, INIT_EXPR, begin_expr,
8820 tf_warning_or_error));
8821 end = build_decl (input_location, VAR_DECL,
8822 get_identifier ("__for_end"), iter_type);
8823 TREE_USED (end) = 1;
8824 DECL_ARTIFICIAL (end) = 1;
8825 pushdecl (end);
8826
8827 finish_expr_stmt (cp_build_modify_expr (end, INIT_EXPR, end_expr,
8828 tf_warning_or_error));
8829
8830 finish_for_init_stmt (statement);
8831
8832 /* The new for condition */
8833 condition = build_x_binary_op (NE_EXPR,
8834 begin, ERROR_MARK,
8835 end, ERROR_MARK,
8836 NULL, tf_warning_or_error);
8837 finish_for_cond (condition, statement);
8838
8839 /* The new increment expression */
8840 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8841 finish_for_expr (expression, statement);
8842
8843 /* The declaration is initialized with *__begin inside the loop body */
8844 cp_finish_decl (range_decl,
8845 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8846 /*is_constant_init*/false, NULL_TREE,
8847 LOOKUP_ONLYCONVERTING);
8848
8849 return statement;
8850 }
8851
8852
8853 /* Parse an iteration-statement.
8854
8855 iteration-statement:
8856 while ( condition ) statement
8857 do statement while ( expression ) ;
8858 for ( for-init-statement condition [opt] ; expression [opt] )
8859 statement
8860
8861 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8862
8863 static tree
8864 cp_parser_iteration_statement (cp_parser* parser)
8865 {
8866 cp_token *token;
8867 enum rid keyword;
8868 tree statement;
8869 unsigned char in_statement;
8870
8871 /* Peek at the next token. */
8872 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8873 if (!token)
8874 return error_mark_node;
8875
8876 /* Remember whether or not we are already within an iteration
8877 statement. */
8878 in_statement = parser->in_statement;
8879
8880 /* See what kind of keyword it is. */
8881 keyword = token->keyword;
8882 switch (keyword)
8883 {
8884 case RID_WHILE:
8885 {
8886 tree condition;
8887
8888 /* Begin the while-statement. */
8889 statement = begin_while_stmt ();
8890 /* Look for the `('. */
8891 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8892 /* Parse the condition. */
8893 condition = cp_parser_condition (parser);
8894 finish_while_stmt_cond (condition, statement);
8895 /* Look for the `)'. */
8896 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8897 /* Parse the dependent statement. */
8898 parser->in_statement = IN_ITERATION_STMT;
8899 cp_parser_already_scoped_statement (parser);
8900 parser->in_statement = in_statement;
8901 /* We're done with the while-statement. */
8902 finish_while_stmt (statement);
8903 }
8904 break;
8905
8906 case RID_DO:
8907 {
8908 tree expression;
8909
8910 /* Begin the do-statement. */
8911 statement = begin_do_stmt ();
8912 /* Parse the body of the do-statement. */
8913 parser->in_statement = IN_ITERATION_STMT;
8914 cp_parser_implicitly_scoped_statement (parser, NULL);
8915 parser->in_statement = in_statement;
8916 finish_do_body (statement);
8917 /* Look for the `while' keyword. */
8918 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8919 /* Look for the `('. */
8920 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8921 /* Parse the expression. */
8922 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8923 /* We're done with the do-statement. */
8924 finish_do_stmt (expression, statement);
8925 /* Look for the `)'. */
8926 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8927 /* Look for the `;'. */
8928 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8929 }
8930 break;
8931
8932 case RID_FOR:
8933 {
8934 /* Look for the `('. */
8935 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8936
8937 if (cxx_dialect == cxx0x)
8938 statement = cp_parser_range_for (parser);
8939 else
8940 statement = NULL_TREE;
8941 if (statement == NULL_TREE)
8942 statement = cp_parser_c_for (parser);
8943
8944 /* Look for the `)'. */
8945 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8946
8947 /* Parse the body of the for-statement. */
8948 parser->in_statement = IN_ITERATION_STMT;
8949 cp_parser_already_scoped_statement (parser);
8950 parser->in_statement = in_statement;
8951
8952 /* We're done with the for-statement. */
8953 finish_for_stmt (statement);
8954 }
8955 break;
8956
8957 default:
8958 cp_parser_error (parser, "expected iteration-statement");
8959 statement = error_mark_node;
8960 break;
8961 }
8962
8963 return statement;
8964 }
8965
8966 /* Parse a for-init-statement.
8967
8968 for-init-statement:
8969 expression-statement
8970 simple-declaration */
8971
8972 static void
8973 cp_parser_for_init_statement (cp_parser* parser)
8974 {
8975 /* If the next token is a `;', then we have an empty
8976 expression-statement. Grammatically, this is also a
8977 simple-declaration, but an invalid one, because it does not
8978 declare anything. Therefore, if we did not handle this case
8979 specially, we would issue an error message about an invalid
8980 declaration. */
8981 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8982 {
8983 /* We're going to speculatively look for a declaration, falling back
8984 to an expression, if necessary. */
8985 cp_parser_parse_tentatively (parser);
8986 /* Parse the declaration. */
8987 cp_parser_simple_declaration (parser,
8988 /*function_definition_allowed_p=*/false);
8989 /* If the tentative parse failed, then we shall need to look for an
8990 expression-statement. */
8991 if (cp_parser_parse_definitely (parser))
8992 return;
8993 }
8994
8995 cp_parser_expression_statement (parser, NULL_TREE);
8996 }
8997
8998 /* Parse a jump-statement.
8999
9000 jump-statement:
9001 break ;
9002 continue ;
9003 return expression [opt] ;
9004 return braced-init-list ;
9005 goto identifier ;
9006
9007 GNU extension:
9008
9009 jump-statement:
9010 goto * expression ;
9011
9012 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9013
9014 static tree
9015 cp_parser_jump_statement (cp_parser* parser)
9016 {
9017 tree statement = error_mark_node;
9018 cp_token *token;
9019 enum rid keyword;
9020 unsigned char in_statement;
9021
9022 /* Peek at the next token. */
9023 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9024 if (!token)
9025 return error_mark_node;
9026
9027 /* See what kind of keyword it is. */
9028 keyword = token->keyword;
9029 switch (keyword)
9030 {
9031 case RID_BREAK:
9032 in_statement = parser->in_statement & ~IN_IF_STMT;
9033 switch (in_statement)
9034 {
9035 case 0:
9036 error_at (token->location, "break statement not within loop or switch");
9037 break;
9038 default:
9039 gcc_assert ((in_statement & IN_SWITCH_STMT)
9040 || in_statement == IN_ITERATION_STMT);
9041 statement = finish_break_stmt ();
9042 break;
9043 case IN_OMP_BLOCK:
9044 error_at (token->location, "invalid exit from OpenMP structured block");
9045 break;
9046 case IN_OMP_FOR:
9047 error_at (token->location, "break statement used with OpenMP for loop");
9048 break;
9049 }
9050 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9051 break;
9052
9053 case RID_CONTINUE:
9054 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9055 {
9056 case 0:
9057 error_at (token->location, "continue statement not within a loop");
9058 break;
9059 case IN_ITERATION_STMT:
9060 case IN_OMP_FOR:
9061 statement = finish_continue_stmt ();
9062 break;
9063 case IN_OMP_BLOCK:
9064 error_at (token->location, "invalid exit from OpenMP structured block");
9065 break;
9066 default:
9067 gcc_unreachable ();
9068 }
9069 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9070 break;
9071
9072 case RID_RETURN:
9073 {
9074 tree expr;
9075 bool expr_non_constant_p;
9076
9077 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9078 {
9079 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9080 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9081 }
9082 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9083 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9084 else
9085 /* If the next token is a `;', then there is no
9086 expression. */
9087 expr = NULL_TREE;
9088 /* Build the return-statement. */
9089 statement = finish_return_stmt (expr);
9090 /* Look for the final `;'. */
9091 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9092 }
9093 break;
9094
9095 case RID_GOTO:
9096 /* Create the goto-statement. */
9097 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9098 {
9099 /* Issue a warning about this use of a GNU extension. */
9100 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9101 /* Consume the '*' token. */
9102 cp_lexer_consume_token (parser->lexer);
9103 /* Parse the dependent expression. */
9104 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9105 }
9106 else
9107 finish_goto_stmt (cp_parser_identifier (parser));
9108 /* Look for the final `;'. */
9109 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9110 break;
9111
9112 default:
9113 cp_parser_error (parser, "expected jump-statement");
9114 break;
9115 }
9116
9117 return statement;
9118 }
9119
9120 /* Parse a declaration-statement.
9121
9122 declaration-statement:
9123 block-declaration */
9124
9125 static void
9126 cp_parser_declaration_statement (cp_parser* parser)
9127 {
9128 void *p;
9129
9130 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9131 p = obstack_alloc (&declarator_obstack, 0);
9132
9133 /* Parse the block-declaration. */
9134 cp_parser_block_declaration (parser, /*statement_p=*/true);
9135
9136 /* Free any declarators allocated. */
9137 obstack_free (&declarator_obstack, p);
9138
9139 /* Finish off the statement. */
9140 finish_stmt ();
9141 }
9142
9143 /* Some dependent statements (like `if (cond) statement'), are
9144 implicitly in their own scope. In other words, if the statement is
9145 a single statement (as opposed to a compound-statement), it is
9146 none-the-less treated as if it were enclosed in braces. Any
9147 declarations appearing in the dependent statement are out of scope
9148 after control passes that point. This function parses a statement,
9149 but ensures that is in its own scope, even if it is not a
9150 compound-statement.
9151
9152 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9153 is a (possibly labeled) if statement which is not enclosed in
9154 braces and has an else clause. This is used to implement
9155 -Wparentheses.
9156
9157 Returns the new statement. */
9158
9159 static tree
9160 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9161 {
9162 tree statement;
9163
9164 if (if_p != NULL)
9165 *if_p = false;
9166
9167 /* Mark if () ; with a special NOP_EXPR. */
9168 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9169 {
9170 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9171 cp_lexer_consume_token (parser->lexer);
9172 statement = add_stmt (build_empty_stmt (loc));
9173 }
9174 /* if a compound is opened, we simply parse the statement directly. */
9175 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9176 statement = cp_parser_compound_statement (parser, NULL, false);
9177 /* If the token is not a `{', then we must take special action. */
9178 else
9179 {
9180 /* Create a compound-statement. */
9181 statement = begin_compound_stmt (0);
9182 /* Parse the dependent-statement. */
9183 cp_parser_statement (parser, NULL_TREE, false, if_p);
9184 /* Finish the dummy compound-statement. */
9185 finish_compound_stmt (statement);
9186 }
9187
9188 /* Return the statement. */
9189 return statement;
9190 }
9191
9192 /* For some dependent statements (like `while (cond) statement'), we
9193 have already created a scope. Therefore, even if the dependent
9194 statement is a compound-statement, we do not want to create another
9195 scope. */
9196
9197 static void
9198 cp_parser_already_scoped_statement (cp_parser* parser)
9199 {
9200 /* If the token is a `{', then we must take special action. */
9201 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9202 cp_parser_statement (parser, NULL_TREE, false, NULL);
9203 else
9204 {
9205 /* Avoid calling cp_parser_compound_statement, so that we
9206 don't create a new scope. Do everything else by hand. */
9207 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9208 /* If the next keyword is `__label__' we have a label declaration. */
9209 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9210 cp_parser_label_declaration (parser);
9211 /* Parse an (optional) statement-seq. */
9212 cp_parser_statement_seq_opt (parser, NULL_TREE);
9213 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9214 }
9215 }
9216
9217 /* Declarations [gram.dcl.dcl] */
9218
9219 /* Parse an optional declaration-sequence.
9220
9221 declaration-seq:
9222 declaration
9223 declaration-seq declaration */
9224
9225 static void
9226 cp_parser_declaration_seq_opt (cp_parser* parser)
9227 {
9228 while (true)
9229 {
9230 cp_token *token;
9231
9232 token = cp_lexer_peek_token (parser->lexer);
9233
9234 if (token->type == CPP_CLOSE_BRACE
9235 || token->type == CPP_EOF
9236 || token->type == CPP_PRAGMA_EOL)
9237 break;
9238
9239 if (token->type == CPP_SEMICOLON)
9240 {
9241 /* A declaration consisting of a single semicolon is
9242 invalid. Allow it unless we're being pedantic. */
9243 cp_lexer_consume_token (parser->lexer);
9244 if (!in_system_header)
9245 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9246 continue;
9247 }
9248
9249 /* If we're entering or exiting a region that's implicitly
9250 extern "C", modify the lang context appropriately. */
9251 if (!parser->implicit_extern_c && token->implicit_extern_c)
9252 {
9253 push_lang_context (lang_name_c);
9254 parser->implicit_extern_c = true;
9255 }
9256 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9257 {
9258 pop_lang_context ();
9259 parser->implicit_extern_c = false;
9260 }
9261
9262 if (token->type == CPP_PRAGMA)
9263 {
9264 /* A top-level declaration can consist solely of a #pragma.
9265 A nested declaration cannot, so this is done here and not
9266 in cp_parser_declaration. (A #pragma at block scope is
9267 handled in cp_parser_statement.) */
9268 cp_parser_pragma (parser, pragma_external);
9269 continue;
9270 }
9271
9272 /* Parse the declaration itself. */
9273 cp_parser_declaration (parser);
9274 }
9275 }
9276
9277 /* Parse a declaration.
9278
9279 declaration:
9280 block-declaration
9281 function-definition
9282 template-declaration
9283 explicit-instantiation
9284 explicit-specialization
9285 linkage-specification
9286 namespace-definition
9287
9288 GNU extension:
9289
9290 declaration:
9291 __extension__ declaration */
9292
9293 static void
9294 cp_parser_declaration (cp_parser* parser)
9295 {
9296 cp_token token1;
9297 cp_token token2;
9298 int saved_pedantic;
9299 void *p;
9300 tree attributes = NULL_TREE;
9301
9302 /* Check for the `__extension__' keyword. */
9303 if (cp_parser_extension_opt (parser, &saved_pedantic))
9304 {
9305 /* Parse the qualified declaration. */
9306 cp_parser_declaration (parser);
9307 /* Restore the PEDANTIC flag. */
9308 pedantic = saved_pedantic;
9309
9310 return;
9311 }
9312
9313 /* Try to figure out what kind of declaration is present. */
9314 token1 = *cp_lexer_peek_token (parser->lexer);
9315
9316 if (token1.type != CPP_EOF)
9317 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9318 else
9319 {
9320 token2.type = CPP_EOF;
9321 token2.keyword = RID_MAX;
9322 }
9323
9324 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9325 p = obstack_alloc (&declarator_obstack, 0);
9326
9327 /* If the next token is `extern' and the following token is a string
9328 literal, then we have a linkage specification. */
9329 if (token1.keyword == RID_EXTERN
9330 && cp_parser_is_string_literal (&token2))
9331 cp_parser_linkage_specification (parser);
9332 /* If the next token is `template', then we have either a template
9333 declaration, an explicit instantiation, or an explicit
9334 specialization. */
9335 else if (token1.keyword == RID_TEMPLATE)
9336 {
9337 /* `template <>' indicates a template specialization. */
9338 if (token2.type == CPP_LESS
9339 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9340 cp_parser_explicit_specialization (parser);
9341 /* `template <' indicates a template declaration. */
9342 else if (token2.type == CPP_LESS)
9343 cp_parser_template_declaration (parser, /*member_p=*/false);
9344 /* Anything else must be an explicit instantiation. */
9345 else
9346 cp_parser_explicit_instantiation (parser);
9347 }
9348 /* If the next token is `export', then we have a template
9349 declaration. */
9350 else if (token1.keyword == RID_EXPORT)
9351 cp_parser_template_declaration (parser, /*member_p=*/false);
9352 /* If the next token is `extern', 'static' or 'inline' and the one
9353 after that is `template', we have a GNU extended explicit
9354 instantiation directive. */
9355 else if (cp_parser_allow_gnu_extensions_p (parser)
9356 && (token1.keyword == RID_EXTERN
9357 || token1.keyword == RID_STATIC
9358 || token1.keyword == RID_INLINE)
9359 && token2.keyword == RID_TEMPLATE)
9360 cp_parser_explicit_instantiation (parser);
9361 /* If the next token is `namespace', check for a named or unnamed
9362 namespace definition. */
9363 else if (token1.keyword == RID_NAMESPACE
9364 && (/* A named namespace definition. */
9365 (token2.type == CPP_NAME
9366 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9367 != CPP_EQ))
9368 /* An unnamed namespace definition. */
9369 || token2.type == CPP_OPEN_BRACE
9370 || token2.keyword == RID_ATTRIBUTE))
9371 cp_parser_namespace_definition (parser);
9372 /* An inline (associated) namespace definition. */
9373 else if (token1.keyword == RID_INLINE
9374 && token2.keyword == RID_NAMESPACE)
9375 cp_parser_namespace_definition (parser);
9376 /* Objective-C++ declaration/definition. */
9377 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9378 cp_parser_objc_declaration (parser, NULL_TREE);
9379 else if (c_dialect_objc ()
9380 && token1.keyword == RID_ATTRIBUTE
9381 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9382 cp_parser_objc_declaration (parser, attributes);
9383 /* We must have either a block declaration or a function
9384 definition. */
9385 else
9386 /* Try to parse a block-declaration, or a function-definition. */
9387 cp_parser_block_declaration (parser, /*statement_p=*/false);
9388
9389 /* Free any declarators allocated. */
9390 obstack_free (&declarator_obstack, p);
9391 }
9392
9393 /* Parse a block-declaration.
9394
9395 block-declaration:
9396 simple-declaration
9397 asm-definition
9398 namespace-alias-definition
9399 using-declaration
9400 using-directive
9401
9402 GNU Extension:
9403
9404 block-declaration:
9405 __extension__ block-declaration
9406
9407 C++0x Extension:
9408
9409 block-declaration:
9410 static_assert-declaration
9411
9412 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9413 part of a declaration-statement. */
9414
9415 static void
9416 cp_parser_block_declaration (cp_parser *parser,
9417 bool statement_p)
9418 {
9419 cp_token *token1;
9420 int saved_pedantic;
9421
9422 /* Check for the `__extension__' keyword. */
9423 if (cp_parser_extension_opt (parser, &saved_pedantic))
9424 {
9425 /* Parse the qualified declaration. */
9426 cp_parser_block_declaration (parser, statement_p);
9427 /* Restore the PEDANTIC flag. */
9428 pedantic = saved_pedantic;
9429
9430 return;
9431 }
9432
9433 /* Peek at the next token to figure out which kind of declaration is
9434 present. */
9435 token1 = cp_lexer_peek_token (parser->lexer);
9436
9437 /* If the next keyword is `asm', we have an asm-definition. */
9438 if (token1->keyword == RID_ASM)
9439 {
9440 if (statement_p)
9441 cp_parser_commit_to_tentative_parse (parser);
9442 cp_parser_asm_definition (parser);
9443 }
9444 /* If the next keyword is `namespace', we have a
9445 namespace-alias-definition. */
9446 else if (token1->keyword == RID_NAMESPACE)
9447 cp_parser_namespace_alias_definition (parser);
9448 /* If the next keyword is `using', we have either a
9449 using-declaration or a using-directive. */
9450 else if (token1->keyword == RID_USING)
9451 {
9452 cp_token *token2;
9453
9454 if (statement_p)
9455 cp_parser_commit_to_tentative_parse (parser);
9456 /* If the token after `using' is `namespace', then we have a
9457 using-directive. */
9458 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9459 if (token2->keyword == RID_NAMESPACE)
9460 cp_parser_using_directive (parser);
9461 /* Otherwise, it's a using-declaration. */
9462 else
9463 cp_parser_using_declaration (parser,
9464 /*access_declaration_p=*/false);
9465 }
9466 /* If the next keyword is `__label__' we have a misplaced label
9467 declaration. */
9468 else if (token1->keyword == RID_LABEL)
9469 {
9470 cp_lexer_consume_token (parser->lexer);
9471 error_at (token1->location, "%<__label__%> not at the beginning of a block");
9472 cp_parser_skip_to_end_of_statement (parser);
9473 /* If the next token is now a `;', consume it. */
9474 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9475 cp_lexer_consume_token (parser->lexer);
9476 }
9477 /* If the next token is `static_assert' we have a static assertion. */
9478 else if (token1->keyword == RID_STATIC_ASSERT)
9479 cp_parser_static_assert (parser, /*member_p=*/false);
9480 /* Anything else must be a simple-declaration. */
9481 else
9482 cp_parser_simple_declaration (parser, !statement_p);
9483 }
9484
9485 /* Parse a simple-declaration.
9486
9487 simple-declaration:
9488 decl-specifier-seq [opt] init-declarator-list [opt] ;
9489
9490 init-declarator-list:
9491 init-declarator
9492 init-declarator-list , init-declarator
9493
9494 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9495 function-definition as a simple-declaration. */
9496
9497 static void
9498 cp_parser_simple_declaration (cp_parser* parser,
9499 bool function_definition_allowed_p)
9500 {
9501 cp_decl_specifier_seq decl_specifiers;
9502 int declares_class_or_enum;
9503 bool saw_declarator;
9504
9505 /* Defer access checks until we know what is being declared; the
9506 checks for names appearing in the decl-specifier-seq should be
9507 done as if we were in the scope of the thing being declared. */
9508 push_deferring_access_checks (dk_deferred);
9509
9510 /* Parse the decl-specifier-seq. We have to keep track of whether
9511 or not the decl-specifier-seq declares a named class or
9512 enumeration type, since that is the only case in which the
9513 init-declarator-list is allowed to be empty.
9514
9515 [dcl.dcl]
9516
9517 In a simple-declaration, the optional init-declarator-list can be
9518 omitted only when declaring a class or enumeration, that is when
9519 the decl-specifier-seq contains either a class-specifier, an
9520 elaborated-type-specifier, or an enum-specifier. */
9521 cp_parser_decl_specifier_seq (parser,
9522 CP_PARSER_FLAGS_OPTIONAL,
9523 &decl_specifiers,
9524 &declares_class_or_enum);
9525 /* We no longer need to defer access checks. */
9526 stop_deferring_access_checks ();
9527
9528 /* In a block scope, a valid declaration must always have a
9529 decl-specifier-seq. By not trying to parse declarators, we can
9530 resolve the declaration/expression ambiguity more quickly. */
9531 if (!function_definition_allowed_p
9532 && !decl_specifiers.any_specifiers_p)
9533 {
9534 cp_parser_error (parser, "expected declaration");
9535 goto done;
9536 }
9537
9538 /* If the next two tokens are both identifiers, the code is
9539 erroneous. The usual cause of this situation is code like:
9540
9541 T t;
9542
9543 where "T" should name a type -- but does not. */
9544 if (!decl_specifiers.any_type_specifiers_p
9545 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9546 {
9547 /* If parsing tentatively, we should commit; we really are
9548 looking at a declaration. */
9549 cp_parser_commit_to_tentative_parse (parser);
9550 /* Give up. */
9551 goto done;
9552 }
9553
9554 /* If we have seen at least one decl-specifier, and the next token
9555 is not a parenthesis, then we must be looking at a declaration.
9556 (After "int (" we might be looking at a functional cast.) */
9557 if (decl_specifiers.any_specifiers_p
9558 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9559 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9560 && !cp_parser_error_occurred (parser))
9561 cp_parser_commit_to_tentative_parse (parser);
9562
9563 /* Keep going until we hit the `;' at the end of the simple
9564 declaration. */
9565 saw_declarator = false;
9566 while (cp_lexer_next_token_is_not (parser->lexer,
9567 CPP_SEMICOLON))
9568 {
9569 cp_token *token;
9570 bool function_definition_p;
9571 tree decl;
9572
9573 if (saw_declarator)
9574 {
9575 /* If we are processing next declarator, coma is expected */
9576 token = cp_lexer_peek_token (parser->lexer);
9577 gcc_assert (token->type == CPP_COMMA);
9578 cp_lexer_consume_token (parser->lexer);
9579 }
9580 else
9581 saw_declarator = true;
9582
9583 /* Parse the init-declarator. */
9584 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9585 /*checks=*/NULL,
9586 function_definition_allowed_p,
9587 /*member_p=*/false,
9588 declares_class_or_enum,
9589 &function_definition_p);
9590 /* If an error occurred while parsing tentatively, exit quickly.
9591 (That usually happens when in the body of a function; each
9592 statement is treated as a declaration-statement until proven
9593 otherwise.) */
9594 if (cp_parser_error_occurred (parser))
9595 goto done;
9596 /* Handle function definitions specially. */
9597 if (function_definition_p)
9598 {
9599 /* If the next token is a `,', then we are probably
9600 processing something like:
9601
9602 void f() {}, *p;
9603
9604 which is erroneous. */
9605 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9606 {
9607 cp_token *token = cp_lexer_peek_token (parser->lexer);
9608 error_at (token->location,
9609 "mixing"
9610 " declarations and function-definitions is forbidden");
9611 }
9612 /* Otherwise, we're done with the list of declarators. */
9613 else
9614 {
9615 pop_deferring_access_checks ();
9616 return;
9617 }
9618 }
9619 /* The next token should be either a `,' or a `;'. */
9620 token = cp_lexer_peek_token (parser->lexer);
9621 /* If it's a `,', there are more declarators to come. */
9622 if (token->type == CPP_COMMA)
9623 /* will be consumed next time around */;
9624 /* If it's a `;', we are done. */
9625 else if (token->type == CPP_SEMICOLON)
9626 break;
9627 /* Anything else is an error. */
9628 else
9629 {
9630 /* If we have already issued an error message we don't need
9631 to issue another one. */
9632 if (decl != error_mark_node
9633 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9634 cp_parser_error (parser, "expected %<,%> or %<;%>");
9635 /* Skip tokens until we reach the end of the statement. */
9636 cp_parser_skip_to_end_of_statement (parser);
9637 /* If the next token is now a `;', consume it. */
9638 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9639 cp_lexer_consume_token (parser->lexer);
9640 goto done;
9641 }
9642 /* After the first time around, a function-definition is not
9643 allowed -- even if it was OK at first. For example:
9644
9645 int i, f() {}
9646
9647 is not valid. */
9648 function_definition_allowed_p = false;
9649 }
9650
9651 /* Issue an error message if no declarators are present, and the
9652 decl-specifier-seq does not itself declare a class or
9653 enumeration. */
9654 if (!saw_declarator)
9655 {
9656 if (cp_parser_declares_only_class_p (parser))
9657 shadow_tag (&decl_specifiers);
9658 /* Perform any deferred access checks. */
9659 perform_deferred_access_checks ();
9660 }
9661
9662 /* Consume the `;'. */
9663 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9664
9665 done:
9666 pop_deferring_access_checks ();
9667 }
9668
9669 /* Parse a decl-specifier-seq.
9670
9671 decl-specifier-seq:
9672 decl-specifier-seq [opt] decl-specifier
9673
9674 decl-specifier:
9675 storage-class-specifier
9676 type-specifier
9677 function-specifier
9678 friend
9679 typedef
9680
9681 GNU Extension:
9682
9683 decl-specifier:
9684 attributes
9685
9686 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9687
9688 The parser flags FLAGS is used to control type-specifier parsing.
9689
9690 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9691 flags:
9692
9693 1: one of the decl-specifiers is an elaborated-type-specifier
9694 (i.e., a type declaration)
9695 2: one of the decl-specifiers is an enum-specifier or a
9696 class-specifier (i.e., a type definition)
9697
9698 */
9699
9700 static void
9701 cp_parser_decl_specifier_seq (cp_parser* parser,
9702 cp_parser_flags flags,
9703 cp_decl_specifier_seq *decl_specs,
9704 int* declares_class_or_enum)
9705 {
9706 bool constructor_possible_p = !parser->in_declarator_p;
9707 cp_token *start_token = NULL;
9708
9709 /* Clear DECL_SPECS. */
9710 clear_decl_specs (decl_specs);
9711
9712 /* Assume no class or enumeration type is declared. */
9713 *declares_class_or_enum = 0;
9714
9715 /* Keep reading specifiers until there are no more to read. */
9716 while (true)
9717 {
9718 bool constructor_p;
9719 bool found_decl_spec;
9720 cp_token *token;
9721
9722 /* Peek at the next token. */
9723 token = cp_lexer_peek_token (parser->lexer);
9724
9725 /* Save the first token of the decl spec list for error
9726 reporting. */
9727 if (!start_token)
9728 start_token = token;
9729 /* Handle attributes. */
9730 if (token->keyword == RID_ATTRIBUTE)
9731 {
9732 /* Parse the attributes. */
9733 decl_specs->attributes
9734 = chainon (decl_specs->attributes,
9735 cp_parser_attributes_opt (parser));
9736 continue;
9737 }
9738 /* Assume we will find a decl-specifier keyword. */
9739 found_decl_spec = true;
9740 /* If the next token is an appropriate keyword, we can simply
9741 add it to the list. */
9742 switch (token->keyword)
9743 {
9744 /* decl-specifier:
9745 friend
9746 constexpr */
9747 case RID_FRIEND:
9748 if (!at_class_scope_p ())
9749 {
9750 error_at (token->location, "%<friend%> used outside of class");
9751 cp_lexer_purge_token (parser->lexer);
9752 }
9753 else
9754 {
9755 ++decl_specs->specs[(int) ds_friend];
9756 /* Consume the token. */
9757 cp_lexer_consume_token (parser->lexer);
9758 }
9759 break;
9760
9761 case RID_CONSTEXPR:
9762 ++decl_specs->specs[(int) ds_constexpr];
9763 cp_lexer_consume_token (parser->lexer);
9764 break;
9765
9766 /* function-specifier:
9767 inline
9768 virtual
9769 explicit */
9770 case RID_INLINE:
9771 case RID_VIRTUAL:
9772 case RID_EXPLICIT:
9773 cp_parser_function_specifier_opt (parser, decl_specs);
9774 break;
9775
9776 /* decl-specifier:
9777 typedef */
9778 case RID_TYPEDEF:
9779 ++decl_specs->specs[(int) ds_typedef];
9780 /* Consume the token. */
9781 cp_lexer_consume_token (parser->lexer);
9782 /* A constructor declarator cannot appear in a typedef. */
9783 constructor_possible_p = false;
9784 /* The "typedef" keyword can only occur in a declaration; we
9785 may as well commit at this point. */
9786 cp_parser_commit_to_tentative_parse (parser);
9787
9788 if (decl_specs->storage_class != sc_none)
9789 decl_specs->conflicting_specifiers_p = true;
9790 break;
9791
9792 /* storage-class-specifier:
9793 auto
9794 register
9795 static
9796 extern
9797 mutable
9798
9799 GNU Extension:
9800 thread */
9801 case RID_AUTO:
9802 if (cxx_dialect == cxx98)
9803 {
9804 /* Consume the token. */
9805 cp_lexer_consume_token (parser->lexer);
9806
9807 /* Complain about `auto' as a storage specifier, if
9808 we're complaining about C++0x compatibility. */
9809 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9810 " will change meaning in C++0x; please remove it");
9811
9812 /* Set the storage class anyway. */
9813 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9814 token->location);
9815 }
9816 else
9817 /* C++0x auto type-specifier. */
9818 found_decl_spec = false;
9819 break;
9820
9821 case RID_REGISTER:
9822 case RID_STATIC:
9823 case RID_EXTERN:
9824 case RID_MUTABLE:
9825 /* Consume the token. */
9826 cp_lexer_consume_token (parser->lexer);
9827 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9828 token->location);
9829 break;
9830 case RID_THREAD:
9831 /* Consume the token. */
9832 cp_lexer_consume_token (parser->lexer);
9833 ++decl_specs->specs[(int) ds_thread];
9834 break;
9835
9836 default:
9837 /* We did not yet find a decl-specifier yet. */
9838 found_decl_spec = false;
9839 break;
9840 }
9841
9842 /* Constructors are a special case. The `S' in `S()' is not a
9843 decl-specifier; it is the beginning of the declarator. */
9844 constructor_p
9845 = (!found_decl_spec
9846 && constructor_possible_p
9847 && (cp_parser_constructor_declarator_p
9848 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9849
9850 /* If we don't have a DECL_SPEC yet, then we must be looking at
9851 a type-specifier. */
9852 if (!found_decl_spec && !constructor_p)
9853 {
9854 int decl_spec_declares_class_or_enum;
9855 bool is_cv_qualifier;
9856 tree type_spec;
9857
9858 type_spec
9859 = cp_parser_type_specifier (parser, flags,
9860 decl_specs,
9861 /*is_declaration=*/true,
9862 &decl_spec_declares_class_or_enum,
9863 &is_cv_qualifier);
9864 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9865
9866 /* If this type-specifier referenced a user-defined type
9867 (a typedef, class-name, etc.), then we can't allow any
9868 more such type-specifiers henceforth.
9869
9870 [dcl.spec]
9871
9872 The longest sequence of decl-specifiers that could
9873 possibly be a type name is taken as the
9874 decl-specifier-seq of a declaration. The sequence shall
9875 be self-consistent as described below.
9876
9877 [dcl.type]
9878
9879 As a general rule, at most one type-specifier is allowed
9880 in the complete decl-specifier-seq of a declaration. The
9881 only exceptions are the following:
9882
9883 -- const or volatile can be combined with any other
9884 type-specifier.
9885
9886 -- signed or unsigned can be combined with char, long,
9887 short, or int.
9888
9889 -- ..
9890
9891 Example:
9892
9893 typedef char* Pc;
9894 void g (const int Pc);
9895
9896 Here, Pc is *not* part of the decl-specifier seq; it's
9897 the declarator. Therefore, once we see a type-specifier
9898 (other than a cv-qualifier), we forbid any additional
9899 user-defined types. We *do* still allow things like `int
9900 int' to be considered a decl-specifier-seq, and issue the
9901 error message later. */
9902 if (type_spec && !is_cv_qualifier)
9903 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9904 /* A constructor declarator cannot follow a type-specifier. */
9905 if (type_spec)
9906 {
9907 constructor_possible_p = false;
9908 found_decl_spec = true;
9909 if (!is_cv_qualifier)
9910 decl_specs->any_type_specifiers_p = true;
9911 }
9912 }
9913
9914 /* If we still do not have a DECL_SPEC, then there are no more
9915 decl-specifiers. */
9916 if (!found_decl_spec)
9917 break;
9918
9919 decl_specs->any_specifiers_p = true;
9920 /* After we see one decl-specifier, further decl-specifiers are
9921 always optional. */
9922 flags |= CP_PARSER_FLAGS_OPTIONAL;
9923 }
9924
9925 cp_parser_check_decl_spec (decl_specs, start_token->location);
9926
9927 /* Don't allow a friend specifier with a class definition. */
9928 if (decl_specs->specs[(int) ds_friend] != 0
9929 && (*declares_class_or_enum & 2))
9930 error_at (start_token->location,
9931 "class definition may not be declared a friend");
9932 }
9933
9934 /* Parse an (optional) storage-class-specifier.
9935
9936 storage-class-specifier:
9937 auto
9938 register
9939 static
9940 extern
9941 mutable
9942
9943 GNU Extension:
9944
9945 storage-class-specifier:
9946 thread
9947
9948 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9949
9950 static tree
9951 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9952 {
9953 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9954 {
9955 case RID_AUTO:
9956 if (cxx_dialect != cxx98)
9957 return NULL_TREE;
9958 /* Fall through for C++98. */
9959
9960 case RID_REGISTER:
9961 case RID_STATIC:
9962 case RID_EXTERN:
9963 case RID_MUTABLE:
9964 case RID_THREAD:
9965 /* Consume the token. */
9966 return cp_lexer_consume_token (parser->lexer)->u.value;
9967
9968 default:
9969 return NULL_TREE;
9970 }
9971 }
9972
9973 /* Parse an (optional) function-specifier.
9974
9975 function-specifier:
9976 inline
9977 virtual
9978 explicit
9979
9980 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9981 Updates DECL_SPECS, if it is non-NULL. */
9982
9983 static tree
9984 cp_parser_function_specifier_opt (cp_parser* parser,
9985 cp_decl_specifier_seq *decl_specs)
9986 {
9987 cp_token *token = cp_lexer_peek_token (parser->lexer);
9988 switch (token->keyword)
9989 {
9990 case RID_INLINE:
9991 if (decl_specs)
9992 ++decl_specs->specs[(int) ds_inline];
9993 break;
9994
9995 case RID_VIRTUAL:
9996 /* 14.5.2.3 [temp.mem]
9997
9998 A member function template shall not be virtual. */
9999 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10000 error_at (token->location, "templates may not be %<virtual%>");
10001 else if (decl_specs)
10002 ++decl_specs->specs[(int) ds_virtual];
10003 break;
10004
10005 case RID_EXPLICIT:
10006 if (decl_specs)
10007 ++decl_specs->specs[(int) ds_explicit];
10008 break;
10009
10010 default:
10011 return NULL_TREE;
10012 }
10013
10014 /* Consume the token. */
10015 return cp_lexer_consume_token (parser->lexer)->u.value;
10016 }
10017
10018 /* Parse a linkage-specification.
10019
10020 linkage-specification:
10021 extern string-literal { declaration-seq [opt] }
10022 extern string-literal declaration */
10023
10024 static void
10025 cp_parser_linkage_specification (cp_parser* parser)
10026 {
10027 tree linkage;
10028
10029 /* Look for the `extern' keyword. */
10030 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10031
10032 /* Look for the string-literal. */
10033 linkage = cp_parser_string_literal (parser, false, false);
10034
10035 /* Transform the literal into an identifier. If the literal is a
10036 wide-character string, or contains embedded NULs, then we can't
10037 handle it as the user wants. */
10038 if (strlen (TREE_STRING_POINTER (linkage))
10039 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10040 {
10041 cp_parser_error (parser, "invalid linkage-specification");
10042 /* Assume C++ linkage. */
10043 linkage = lang_name_cplusplus;
10044 }
10045 else
10046 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10047
10048 /* We're now using the new linkage. */
10049 push_lang_context (linkage);
10050
10051 /* If the next token is a `{', then we're using the first
10052 production. */
10053 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10054 {
10055 /* Consume the `{' token. */
10056 cp_lexer_consume_token (parser->lexer);
10057 /* Parse the declarations. */
10058 cp_parser_declaration_seq_opt (parser);
10059 /* Look for the closing `}'. */
10060 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10061 }
10062 /* Otherwise, there's just one declaration. */
10063 else
10064 {
10065 bool saved_in_unbraced_linkage_specification_p;
10066
10067 saved_in_unbraced_linkage_specification_p
10068 = parser->in_unbraced_linkage_specification_p;
10069 parser->in_unbraced_linkage_specification_p = true;
10070 cp_parser_declaration (parser);
10071 parser->in_unbraced_linkage_specification_p
10072 = saved_in_unbraced_linkage_specification_p;
10073 }
10074
10075 /* We're done with the linkage-specification. */
10076 pop_lang_context ();
10077 }
10078
10079 /* Parse a static_assert-declaration.
10080
10081 static_assert-declaration:
10082 static_assert ( constant-expression , string-literal ) ;
10083
10084 If MEMBER_P, this static_assert is a class member. */
10085
10086 static void
10087 cp_parser_static_assert(cp_parser *parser, bool member_p)
10088 {
10089 tree condition;
10090 tree message;
10091 cp_token *token;
10092 location_t saved_loc;
10093
10094 /* Peek at the `static_assert' token so we can keep track of exactly
10095 where the static assertion started. */
10096 token = cp_lexer_peek_token (parser->lexer);
10097 saved_loc = token->location;
10098
10099 /* Look for the `static_assert' keyword. */
10100 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10101 RT_STATIC_ASSERT))
10102 return;
10103
10104 /* We know we are in a static assertion; commit to any tentative
10105 parse. */
10106 if (cp_parser_parsing_tentatively (parser))
10107 cp_parser_commit_to_tentative_parse (parser);
10108
10109 /* Parse the `(' starting the static assertion condition. */
10110 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10111
10112 /* Parse the constant-expression. */
10113 condition =
10114 cp_parser_constant_expression (parser,
10115 /*allow_non_constant_p=*/false,
10116 /*non_constant_p=*/NULL);
10117
10118 /* Parse the separating `,'. */
10119 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10120
10121 /* Parse the string-literal message. */
10122 message = cp_parser_string_literal (parser,
10123 /*translate=*/false,
10124 /*wide_ok=*/true);
10125
10126 /* A `)' completes the static assertion. */
10127 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10128 cp_parser_skip_to_closing_parenthesis (parser,
10129 /*recovering=*/true,
10130 /*or_comma=*/false,
10131 /*consume_paren=*/true);
10132
10133 /* A semicolon terminates the declaration. */
10134 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10135
10136 /* Complete the static assertion, which may mean either processing
10137 the static assert now or saving it for template instantiation. */
10138 finish_static_assert (condition, message, saved_loc, member_p);
10139 }
10140
10141 /* Parse a `decltype' type. Returns the type.
10142
10143 simple-type-specifier:
10144 decltype ( expression ) */
10145
10146 static tree
10147 cp_parser_decltype (cp_parser *parser)
10148 {
10149 tree expr;
10150 bool id_expression_or_member_access_p = false;
10151 const char *saved_message;
10152 bool saved_integral_constant_expression_p;
10153 bool saved_non_integral_constant_expression_p;
10154 cp_token *id_expr_start_token;
10155
10156 /* Look for the `decltype' token. */
10157 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10158 return error_mark_node;
10159
10160 /* Types cannot be defined in a `decltype' expression. Save away the
10161 old message. */
10162 saved_message = parser->type_definition_forbidden_message;
10163
10164 /* And create the new one. */
10165 parser->type_definition_forbidden_message
10166 = G_("types may not be defined in %<decltype%> expressions");
10167
10168 /* The restrictions on constant-expressions do not apply inside
10169 decltype expressions. */
10170 saved_integral_constant_expression_p
10171 = parser->integral_constant_expression_p;
10172 saved_non_integral_constant_expression_p
10173 = parser->non_integral_constant_expression_p;
10174 parser->integral_constant_expression_p = false;
10175
10176 /* Do not actually evaluate the expression. */
10177 ++cp_unevaluated_operand;
10178
10179 /* Do not warn about problems with the expression. */
10180 ++c_inhibit_evaluation_warnings;
10181
10182 /* Parse the opening `('. */
10183 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10184 return error_mark_node;
10185
10186 /* First, try parsing an id-expression. */
10187 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10188 cp_parser_parse_tentatively (parser);
10189 expr = cp_parser_id_expression (parser,
10190 /*template_keyword_p=*/false,
10191 /*check_dependency_p=*/true,
10192 /*template_p=*/NULL,
10193 /*declarator_p=*/false,
10194 /*optional_p=*/false);
10195
10196 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10197 {
10198 bool non_integral_constant_expression_p = false;
10199 tree id_expression = expr;
10200 cp_id_kind idk;
10201 const char *error_msg;
10202
10203 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10204 /* Lookup the name we got back from the id-expression. */
10205 expr = cp_parser_lookup_name (parser, expr,
10206 none_type,
10207 /*is_template=*/false,
10208 /*is_namespace=*/false,
10209 /*check_dependency=*/true,
10210 /*ambiguous_decls=*/NULL,
10211 id_expr_start_token->location);
10212
10213 if (expr
10214 && expr != error_mark_node
10215 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10216 && TREE_CODE (expr) != TYPE_DECL
10217 && (TREE_CODE (expr) != BIT_NOT_EXPR
10218 || !TYPE_P (TREE_OPERAND (expr, 0)))
10219 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10220 {
10221 /* Complete lookup of the id-expression. */
10222 expr = (finish_id_expression
10223 (id_expression, expr, parser->scope, &idk,
10224 /*integral_constant_expression_p=*/false,
10225 /*allow_non_integral_constant_expression_p=*/true,
10226 &non_integral_constant_expression_p,
10227 /*template_p=*/false,
10228 /*done=*/true,
10229 /*address_p=*/false,
10230 /*template_arg_p=*/false,
10231 &error_msg,
10232 id_expr_start_token->location));
10233
10234 if (expr == error_mark_node)
10235 /* We found an id-expression, but it was something that we
10236 should not have found. This is an error, not something
10237 we can recover from, so note that we found an
10238 id-expression and we'll recover as gracefully as
10239 possible. */
10240 id_expression_or_member_access_p = true;
10241 }
10242
10243 if (expr
10244 && expr != error_mark_node
10245 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10246 /* We have an id-expression. */
10247 id_expression_or_member_access_p = true;
10248 }
10249
10250 if (!id_expression_or_member_access_p)
10251 {
10252 /* Abort the id-expression parse. */
10253 cp_parser_abort_tentative_parse (parser);
10254
10255 /* Parsing tentatively, again. */
10256 cp_parser_parse_tentatively (parser);
10257
10258 /* Parse a class member access. */
10259 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10260 /*cast_p=*/false,
10261 /*member_access_only_p=*/true, NULL);
10262
10263 if (expr
10264 && expr != error_mark_node
10265 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10266 /* We have an id-expression. */
10267 id_expression_or_member_access_p = true;
10268 }
10269
10270 if (id_expression_or_member_access_p)
10271 /* We have parsed the complete id-expression or member access. */
10272 cp_parser_parse_definitely (parser);
10273 else
10274 {
10275 bool saved_greater_than_is_operator_p;
10276
10277 /* Abort our attempt to parse an id-expression or member access
10278 expression. */
10279 cp_parser_abort_tentative_parse (parser);
10280
10281 /* Within a parenthesized expression, a `>' token is always
10282 the greater-than operator. */
10283 saved_greater_than_is_operator_p
10284 = parser->greater_than_is_operator_p;
10285 parser->greater_than_is_operator_p = true;
10286
10287 /* Parse a full expression. */
10288 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10289
10290 /* The `>' token might be the end of a template-id or
10291 template-parameter-list now. */
10292 parser->greater_than_is_operator_p
10293 = saved_greater_than_is_operator_p;
10294 }
10295
10296 /* Go back to evaluating expressions. */
10297 --cp_unevaluated_operand;
10298 --c_inhibit_evaluation_warnings;
10299
10300 /* Restore the old message and the integral constant expression
10301 flags. */
10302 parser->type_definition_forbidden_message = saved_message;
10303 parser->integral_constant_expression_p
10304 = saved_integral_constant_expression_p;
10305 parser->non_integral_constant_expression_p
10306 = saved_non_integral_constant_expression_p;
10307
10308 if (expr == error_mark_node)
10309 {
10310 /* Skip everything up to the closing `)'. */
10311 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10312 /*consume_paren=*/true);
10313 return error_mark_node;
10314 }
10315
10316 /* Parse to the closing `)'. */
10317 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10318 {
10319 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10320 /*consume_paren=*/true);
10321 return error_mark_node;
10322 }
10323
10324 return finish_decltype_type (expr, id_expression_or_member_access_p);
10325 }
10326
10327 /* Special member functions [gram.special] */
10328
10329 /* Parse a conversion-function-id.
10330
10331 conversion-function-id:
10332 operator conversion-type-id
10333
10334 Returns an IDENTIFIER_NODE representing the operator. */
10335
10336 static tree
10337 cp_parser_conversion_function_id (cp_parser* parser)
10338 {
10339 tree type;
10340 tree saved_scope;
10341 tree saved_qualifying_scope;
10342 tree saved_object_scope;
10343 tree pushed_scope = NULL_TREE;
10344
10345 /* Look for the `operator' token. */
10346 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10347 return error_mark_node;
10348 /* When we parse the conversion-type-id, the current scope will be
10349 reset. However, we need that information in able to look up the
10350 conversion function later, so we save it here. */
10351 saved_scope = parser->scope;
10352 saved_qualifying_scope = parser->qualifying_scope;
10353 saved_object_scope = parser->object_scope;
10354 /* We must enter the scope of the class so that the names of
10355 entities declared within the class are available in the
10356 conversion-type-id. For example, consider:
10357
10358 struct S {
10359 typedef int I;
10360 operator I();
10361 };
10362
10363 S::operator I() { ... }
10364
10365 In order to see that `I' is a type-name in the definition, we
10366 must be in the scope of `S'. */
10367 if (saved_scope)
10368 pushed_scope = push_scope (saved_scope);
10369 /* Parse the conversion-type-id. */
10370 type = cp_parser_conversion_type_id (parser);
10371 /* Leave the scope of the class, if any. */
10372 if (pushed_scope)
10373 pop_scope (pushed_scope);
10374 /* Restore the saved scope. */
10375 parser->scope = saved_scope;
10376 parser->qualifying_scope = saved_qualifying_scope;
10377 parser->object_scope = saved_object_scope;
10378 /* If the TYPE is invalid, indicate failure. */
10379 if (type == error_mark_node)
10380 return error_mark_node;
10381 return mangle_conv_op_name_for_type (type);
10382 }
10383
10384 /* Parse a conversion-type-id:
10385
10386 conversion-type-id:
10387 type-specifier-seq conversion-declarator [opt]
10388
10389 Returns the TYPE specified. */
10390
10391 static tree
10392 cp_parser_conversion_type_id (cp_parser* parser)
10393 {
10394 tree attributes;
10395 cp_decl_specifier_seq type_specifiers;
10396 cp_declarator *declarator;
10397 tree type_specified;
10398
10399 /* Parse the attributes. */
10400 attributes = cp_parser_attributes_opt (parser);
10401 /* Parse the type-specifiers. */
10402 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10403 /*is_trailing_return=*/false,
10404 &type_specifiers);
10405 /* If that didn't work, stop. */
10406 if (type_specifiers.type == error_mark_node)
10407 return error_mark_node;
10408 /* Parse the conversion-declarator. */
10409 declarator = cp_parser_conversion_declarator_opt (parser);
10410
10411 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10412 /*initialized=*/0, &attributes);
10413 if (attributes)
10414 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10415
10416 /* Don't give this error when parsing tentatively. This happens to
10417 work because we always parse this definitively once. */
10418 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10419 && type_uses_auto (type_specified))
10420 {
10421 error ("invalid use of %<auto%> in conversion operator");
10422 return error_mark_node;
10423 }
10424
10425 return type_specified;
10426 }
10427
10428 /* Parse an (optional) conversion-declarator.
10429
10430 conversion-declarator:
10431 ptr-operator conversion-declarator [opt]
10432
10433 */
10434
10435 static cp_declarator *
10436 cp_parser_conversion_declarator_opt (cp_parser* parser)
10437 {
10438 enum tree_code code;
10439 tree class_type;
10440 cp_cv_quals cv_quals;
10441
10442 /* We don't know if there's a ptr-operator next, or not. */
10443 cp_parser_parse_tentatively (parser);
10444 /* Try the ptr-operator. */
10445 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10446 /* If it worked, look for more conversion-declarators. */
10447 if (cp_parser_parse_definitely (parser))
10448 {
10449 cp_declarator *declarator;
10450
10451 /* Parse another optional declarator. */
10452 declarator = cp_parser_conversion_declarator_opt (parser);
10453
10454 return cp_parser_make_indirect_declarator
10455 (code, class_type, cv_quals, declarator);
10456 }
10457
10458 return NULL;
10459 }
10460
10461 /* Parse an (optional) ctor-initializer.
10462
10463 ctor-initializer:
10464 : mem-initializer-list
10465
10466 Returns TRUE iff the ctor-initializer was actually present. */
10467
10468 static bool
10469 cp_parser_ctor_initializer_opt (cp_parser* parser)
10470 {
10471 /* If the next token is not a `:', then there is no
10472 ctor-initializer. */
10473 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10474 {
10475 /* Do default initialization of any bases and members. */
10476 if (DECL_CONSTRUCTOR_P (current_function_decl))
10477 finish_mem_initializers (NULL_TREE);
10478
10479 return false;
10480 }
10481
10482 /* Consume the `:' token. */
10483 cp_lexer_consume_token (parser->lexer);
10484 /* And the mem-initializer-list. */
10485 cp_parser_mem_initializer_list (parser);
10486
10487 return true;
10488 }
10489
10490 /* Parse a mem-initializer-list.
10491
10492 mem-initializer-list:
10493 mem-initializer ... [opt]
10494 mem-initializer ... [opt] , mem-initializer-list */
10495
10496 static void
10497 cp_parser_mem_initializer_list (cp_parser* parser)
10498 {
10499 tree mem_initializer_list = NULL_TREE;
10500 cp_token *token = cp_lexer_peek_token (parser->lexer);
10501
10502 /* Let the semantic analysis code know that we are starting the
10503 mem-initializer-list. */
10504 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10505 error_at (token->location,
10506 "only constructors take member initializers");
10507
10508 /* Loop through the list. */
10509 while (true)
10510 {
10511 tree mem_initializer;
10512
10513 token = cp_lexer_peek_token (parser->lexer);
10514 /* Parse the mem-initializer. */
10515 mem_initializer = cp_parser_mem_initializer (parser);
10516 /* If the next token is a `...', we're expanding member initializers. */
10517 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10518 {
10519 /* Consume the `...'. */
10520 cp_lexer_consume_token (parser->lexer);
10521
10522 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10523 can be expanded but members cannot. */
10524 if (mem_initializer != error_mark_node
10525 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10526 {
10527 error_at (token->location,
10528 "cannot expand initializer for member %<%D%>",
10529 TREE_PURPOSE (mem_initializer));
10530 mem_initializer = error_mark_node;
10531 }
10532
10533 /* Construct the pack expansion type. */
10534 if (mem_initializer != error_mark_node)
10535 mem_initializer = make_pack_expansion (mem_initializer);
10536 }
10537 /* Add it to the list, unless it was erroneous. */
10538 if (mem_initializer != error_mark_node)
10539 {
10540 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10541 mem_initializer_list = mem_initializer;
10542 }
10543 /* If the next token is not a `,', we're done. */
10544 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10545 break;
10546 /* Consume the `,' token. */
10547 cp_lexer_consume_token (parser->lexer);
10548 }
10549
10550 /* Perform semantic analysis. */
10551 if (DECL_CONSTRUCTOR_P (current_function_decl))
10552 finish_mem_initializers (mem_initializer_list);
10553 }
10554
10555 /* Parse a mem-initializer.
10556
10557 mem-initializer:
10558 mem-initializer-id ( expression-list [opt] )
10559 mem-initializer-id braced-init-list
10560
10561 GNU extension:
10562
10563 mem-initializer:
10564 ( expression-list [opt] )
10565
10566 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10567 class) or FIELD_DECL (for a non-static data member) to initialize;
10568 the TREE_VALUE is the expression-list. An empty initialization
10569 list is represented by void_list_node. */
10570
10571 static tree
10572 cp_parser_mem_initializer (cp_parser* parser)
10573 {
10574 tree mem_initializer_id;
10575 tree expression_list;
10576 tree member;
10577 cp_token *token = cp_lexer_peek_token (parser->lexer);
10578
10579 /* Find out what is being initialized. */
10580 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10581 {
10582 permerror (token->location,
10583 "anachronistic old-style base class initializer");
10584 mem_initializer_id = NULL_TREE;
10585 }
10586 else
10587 {
10588 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10589 if (mem_initializer_id == error_mark_node)
10590 return mem_initializer_id;
10591 }
10592 member = expand_member_init (mem_initializer_id);
10593 if (member && !DECL_P (member))
10594 in_base_initializer = 1;
10595
10596 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10597 {
10598 bool expr_non_constant_p;
10599 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10600 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10601 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10602 expression_list = build_tree_list (NULL_TREE, expression_list);
10603 }
10604 else
10605 {
10606 VEC(tree,gc)* vec;
10607 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10608 /*cast_p=*/false,
10609 /*allow_expansion_p=*/true,
10610 /*non_constant_p=*/NULL);
10611 if (vec == NULL)
10612 return error_mark_node;
10613 expression_list = build_tree_list_vec (vec);
10614 release_tree_vector (vec);
10615 }
10616
10617 if (expression_list == error_mark_node)
10618 return error_mark_node;
10619 if (!expression_list)
10620 expression_list = void_type_node;
10621
10622 in_base_initializer = 0;
10623
10624 return member ? build_tree_list (member, expression_list) : error_mark_node;
10625 }
10626
10627 /* Parse a mem-initializer-id.
10628
10629 mem-initializer-id:
10630 :: [opt] nested-name-specifier [opt] class-name
10631 identifier
10632
10633 Returns a TYPE indicating the class to be initializer for the first
10634 production. Returns an IDENTIFIER_NODE indicating the data member
10635 to be initialized for the second production. */
10636
10637 static tree
10638 cp_parser_mem_initializer_id (cp_parser* parser)
10639 {
10640 bool global_scope_p;
10641 bool nested_name_specifier_p;
10642 bool template_p = false;
10643 tree id;
10644
10645 cp_token *token = cp_lexer_peek_token (parser->lexer);
10646
10647 /* `typename' is not allowed in this context ([temp.res]). */
10648 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10649 {
10650 error_at (token->location,
10651 "keyword %<typename%> not allowed in this context (a qualified "
10652 "member initializer is implicitly a type)");
10653 cp_lexer_consume_token (parser->lexer);
10654 }
10655 /* Look for the optional `::' operator. */
10656 global_scope_p
10657 = (cp_parser_global_scope_opt (parser,
10658 /*current_scope_valid_p=*/false)
10659 != NULL_TREE);
10660 /* Look for the optional nested-name-specifier. The simplest way to
10661 implement:
10662
10663 [temp.res]
10664
10665 The keyword `typename' is not permitted in a base-specifier or
10666 mem-initializer; in these contexts a qualified name that
10667 depends on a template-parameter is implicitly assumed to be a
10668 type name.
10669
10670 is to assume that we have seen the `typename' keyword at this
10671 point. */
10672 nested_name_specifier_p
10673 = (cp_parser_nested_name_specifier_opt (parser,
10674 /*typename_keyword_p=*/true,
10675 /*check_dependency_p=*/true,
10676 /*type_p=*/true,
10677 /*is_declaration=*/true)
10678 != NULL_TREE);
10679 if (nested_name_specifier_p)
10680 template_p = cp_parser_optional_template_keyword (parser);
10681 /* If there is a `::' operator or a nested-name-specifier, then we
10682 are definitely looking for a class-name. */
10683 if (global_scope_p || nested_name_specifier_p)
10684 return cp_parser_class_name (parser,
10685 /*typename_keyword_p=*/true,
10686 /*template_keyword_p=*/template_p,
10687 typename_type,
10688 /*check_dependency_p=*/true,
10689 /*class_head_p=*/false,
10690 /*is_declaration=*/true);
10691 /* Otherwise, we could also be looking for an ordinary identifier. */
10692 cp_parser_parse_tentatively (parser);
10693 /* Try a class-name. */
10694 id = cp_parser_class_name (parser,
10695 /*typename_keyword_p=*/true,
10696 /*template_keyword_p=*/false,
10697 none_type,
10698 /*check_dependency_p=*/true,
10699 /*class_head_p=*/false,
10700 /*is_declaration=*/true);
10701 /* If we found one, we're done. */
10702 if (cp_parser_parse_definitely (parser))
10703 return id;
10704 /* Otherwise, look for an ordinary identifier. */
10705 return cp_parser_identifier (parser);
10706 }
10707
10708 /* Overloading [gram.over] */
10709
10710 /* Parse an operator-function-id.
10711
10712 operator-function-id:
10713 operator operator
10714
10715 Returns an IDENTIFIER_NODE for the operator which is a
10716 human-readable spelling of the identifier, e.g., `operator +'. */
10717
10718 static tree
10719 cp_parser_operator_function_id (cp_parser* parser)
10720 {
10721 /* Look for the `operator' keyword. */
10722 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10723 return error_mark_node;
10724 /* And then the name of the operator itself. */
10725 return cp_parser_operator (parser);
10726 }
10727
10728 /* Parse an operator.
10729
10730 operator:
10731 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10732 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10733 || ++ -- , ->* -> () []
10734
10735 GNU Extensions:
10736
10737 operator:
10738 <? >? <?= >?=
10739
10740 Returns an IDENTIFIER_NODE for the operator which is a
10741 human-readable spelling of the identifier, e.g., `operator +'. */
10742
10743 static tree
10744 cp_parser_operator (cp_parser* parser)
10745 {
10746 tree id = NULL_TREE;
10747 cp_token *token;
10748
10749 /* Peek at the next token. */
10750 token = cp_lexer_peek_token (parser->lexer);
10751 /* Figure out which operator we have. */
10752 switch (token->type)
10753 {
10754 case CPP_KEYWORD:
10755 {
10756 enum tree_code op;
10757
10758 /* The keyword should be either `new' or `delete'. */
10759 if (token->keyword == RID_NEW)
10760 op = NEW_EXPR;
10761 else if (token->keyword == RID_DELETE)
10762 op = DELETE_EXPR;
10763 else
10764 break;
10765
10766 /* Consume the `new' or `delete' token. */
10767 cp_lexer_consume_token (parser->lexer);
10768
10769 /* Peek at the next token. */
10770 token = cp_lexer_peek_token (parser->lexer);
10771 /* If it's a `[' token then this is the array variant of the
10772 operator. */
10773 if (token->type == CPP_OPEN_SQUARE)
10774 {
10775 /* Consume the `[' token. */
10776 cp_lexer_consume_token (parser->lexer);
10777 /* Look for the `]' token. */
10778 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10779 id = ansi_opname (op == NEW_EXPR
10780 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10781 }
10782 /* Otherwise, we have the non-array variant. */
10783 else
10784 id = ansi_opname (op);
10785
10786 return id;
10787 }
10788
10789 case CPP_PLUS:
10790 id = ansi_opname (PLUS_EXPR);
10791 break;
10792
10793 case CPP_MINUS:
10794 id = ansi_opname (MINUS_EXPR);
10795 break;
10796
10797 case CPP_MULT:
10798 id = ansi_opname (MULT_EXPR);
10799 break;
10800
10801 case CPP_DIV:
10802 id = ansi_opname (TRUNC_DIV_EXPR);
10803 break;
10804
10805 case CPP_MOD:
10806 id = ansi_opname (TRUNC_MOD_EXPR);
10807 break;
10808
10809 case CPP_XOR:
10810 id = ansi_opname (BIT_XOR_EXPR);
10811 break;
10812
10813 case CPP_AND:
10814 id = ansi_opname (BIT_AND_EXPR);
10815 break;
10816
10817 case CPP_OR:
10818 id = ansi_opname (BIT_IOR_EXPR);
10819 break;
10820
10821 case CPP_COMPL:
10822 id = ansi_opname (BIT_NOT_EXPR);
10823 break;
10824
10825 case CPP_NOT:
10826 id = ansi_opname (TRUTH_NOT_EXPR);
10827 break;
10828
10829 case CPP_EQ:
10830 id = ansi_assopname (NOP_EXPR);
10831 break;
10832
10833 case CPP_LESS:
10834 id = ansi_opname (LT_EXPR);
10835 break;
10836
10837 case CPP_GREATER:
10838 id = ansi_opname (GT_EXPR);
10839 break;
10840
10841 case CPP_PLUS_EQ:
10842 id = ansi_assopname (PLUS_EXPR);
10843 break;
10844
10845 case CPP_MINUS_EQ:
10846 id = ansi_assopname (MINUS_EXPR);
10847 break;
10848
10849 case CPP_MULT_EQ:
10850 id = ansi_assopname (MULT_EXPR);
10851 break;
10852
10853 case CPP_DIV_EQ:
10854 id = ansi_assopname (TRUNC_DIV_EXPR);
10855 break;
10856
10857 case CPP_MOD_EQ:
10858 id = ansi_assopname (TRUNC_MOD_EXPR);
10859 break;
10860
10861 case CPP_XOR_EQ:
10862 id = ansi_assopname (BIT_XOR_EXPR);
10863 break;
10864
10865 case CPP_AND_EQ:
10866 id = ansi_assopname (BIT_AND_EXPR);
10867 break;
10868
10869 case CPP_OR_EQ:
10870 id = ansi_assopname (BIT_IOR_EXPR);
10871 break;
10872
10873 case CPP_LSHIFT:
10874 id = ansi_opname (LSHIFT_EXPR);
10875 break;
10876
10877 case CPP_RSHIFT:
10878 id = ansi_opname (RSHIFT_EXPR);
10879 break;
10880
10881 case CPP_LSHIFT_EQ:
10882 id = ansi_assopname (LSHIFT_EXPR);
10883 break;
10884
10885 case CPP_RSHIFT_EQ:
10886 id = ansi_assopname (RSHIFT_EXPR);
10887 break;
10888
10889 case CPP_EQ_EQ:
10890 id = ansi_opname (EQ_EXPR);
10891 break;
10892
10893 case CPP_NOT_EQ:
10894 id = ansi_opname (NE_EXPR);
10895 break;
10896
10897 case CPP_LESS_EQ:
10898 id = ansi_opname (LE_EXPR);
10899 break;
10900
10901 case CPP_GREATER_EQ:
10902 id = ansi_opname (GE_EXPR);
10903 break;
10904
10905 case CPP_AND_AND:
10906 id = ansi_opname (TRUTH_ANDIF_EXPR);
10907 break;
10908
10909 case CPP_OR_OR:
10910 id = ansi_opname (TRUTH_ORIF_EXPR);
10911 break;
10912
10913 case CPP_PLUS_PLUS:
10914 id = ansi_opname (POSTINCREMENT_EXPR);
10915 break;
10916
10917 case CPP_MINUS_MINUS:
10918 id = ansi_opname (PREDECREMENT_EXPR);
10919 break;
10920
10921 case CPP_COMMA:
10922 id = ansi_opname (COMPOUND_EXPR);
10923 break;
10924
10925 case CPP_DEREF_STAR:
10926 id = ansi_opname (MEMBER_REF);
10927 break;
10928
10929 case CPP_DEREF:
10930 id = ansi_opname (COMPONENT_REF);
10931 break;
10932
10933 case CPP_OPEN_PAREN:
10934 /* Consume the `('. */
10935 cp_lexer_consume_token (parser->lexer);
10936 /* Look for the matching `)'. */
10937 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10938 return ansi_opname (CALL_EXPR);
10939
10940 case CPP_OPEN_SQUARE:
10941 /* Consume the `['. */
10942 cp_lexer_consume_token (parser->lexer);
10943 /* Look for the matching `]'. */
10944 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10945 return ansi_opname (ARRAY_REF);
10946
10947 default:
10948 /* Anything else is an error. */
10949 break;
10950 }
10951
10952 /* If we have selected an identifier, we need to consume the
10953 operator token. */
10954 if (id)
10955 cp_lexer_consume_token (parser->lexer);
10956 /* Otherwise, no valid operator name was present. */
10957 else
10958 {
10959 cp_parser_error (parser, "expected operator");
10960 id = error_mark_node;
10961 }
10962
10963 return id;
10964 }
10965
10966 /* Parse a template-declaration.
10967
10968 template-declaration:
10969 export [opt] template < template-parameter-list > declaration
10970
10971 If MEMBER_P is TRUE, this template-declaration occurs within a
10972 class-specifier.
10973
10974 The grammar rule given by the standard isn't correct. What
10975 is really meant is:
10976
10977 template-declaration:
10978 export [opt] template-parameter-list-seq
10979 decl-specifier-seq [opt] init-declarator [opt] ;
10980 export [opt] template-parameter-list-seq
10981 function-definition
10982
10983 template-parameter-list-seq:
10984 template-parameter-list-seq [opt]
10985 template < template-parameter-list > */
10986
10987 static void
10988 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10989 {
10990 /* Check for `export'. */
10991 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10992 {
10993 /* Consume the `export' token. */
10994 cp_lexer_consume_token (parser->lexer);
10995 /* Warn that we do not support `export'. */
10996 warning (0, "keyword %<export%> not implemented, and will be ignored");
10997 }
10998
10999 cp_parser_template_declaration_after_export (parser, member_p);
11000 }
11001
11002 /* Parse a template-parameter-list.
11003
11004 template-parameter-list:
11005 template-parameter
11006 template-parameter-list , template-parameter
11007
11008 Returns a TREE_LIST. Each node represents a template parameter.
11009 The nodes are connected via their TREE_CHAINs. */
11010
11011 static tree
11012 cp_parser_template_parameter_list (cp_parser* parser)
11013 {
11014 tree parameter_list = NULL_TREE;
11015
11016 begin_template_parm_list ();
11017 while (true)
11018 {
11019 tree parameter;
11020 bool is_non_type;
11021 bool is_parameter_pack;
11022 location_t parm_loc;
11023
11024 /* Parse the template-parameter. */
11025 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11026 parameter = cp_parser_template_parameter (parser,
11027 &is_non_type,
11028 &is_parameter_pack);
11029 /* Add it to the list. */
11030 if (parameter != error_mark_node)
11031 parameter_list = process_template_parm (parameter_list,
11032 parm_loc,
11033 parameter,
11034 is_non_type,
11035 is_parameter_pack);
11036 else
11037 {
11038 tree err_parm = build_tree_list (parameter, parameter);
11039 TREE_VALUE (err_parm) = error_mark_node;
11040 parameter_list = chainon (parameter_list, err_parm);
11041 }
11042
11043 /* If the next token is not a `,', we're done. */
11044 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11045 break;
11046 /* Otherwise, consume the `,' token. */
11047 cp_lexer_consume_token (parser->lexer);
11048 }
11049
11050 return end_template_parm_list (parameter_list);
11051 }
11052
11053 /* Parse a template-parameter.
11054
11055 template-parameter:
11056 type-parameter
11057 parameter-declaration
11058
11059 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11060 the parameter. The TREE_PURPOSE is the default value, if any.
11061 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11062 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11063 set to true iff this parameter is a parameter pack. */
11064
11065 static tree
11066 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11067 bool *is_parameter_pack)
11068 {
11069 cp_token *token;
11070 cp_parameter_declarator *parameter_declarator;
11071 cp_declarator *id_declarator;
11072 tree parm;
11073
11074 /* Assume it is a type parameter or a template parameter. */
11075 *is_non_type = false;
11076 /* Assume it not a parameter pack. */
11077 *is_parameter_pack = false;
11078 /* Peek at the next token. */
11079 token = cp_lexer_peek_token (parser->lexer);
11080 /* If it is `class' or `template', we have a type-parameter. */
11081 if (token->keyword == RID_TEMPLATE)
11082 return cp_parser_type_parameter (parser, is_parameter_pack);
11083 /* If it is `class' or `typename' we do not know yet whether it is a
11084 type parameter or a non-type parameter. Consider:
11085
11086 template <typename T, typename T::X X> ...
11087
11088 or:
11089
11090 template <class C, class D*> ...
11091
11092 Here, the first parameter is a type parameter, and the second is
11093 a non-type parameter. We can tell by looking at the token after
11094 the identifier -- if it is a `,', `=', or `>' then we have a type
11095 parameter. */
11096 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11097 {
11098 /* Peek at the token after `class' or `typename'. */
11099 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11100 /* If it's an ellipsis, we have a template type parameter
11101 pack. */
11102 if (token->type == CPP_ELLIPSIS)
11103 return cp_parser_type_parameter (parser, is_parameter_pack);
11104 /* If it's an identifier, skip it. */
11105 if (token->type == CPP_NAME)
11106 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11107 /* Now, see if the token looks like the end of a template
11108 parameter. */
11109 if (token->type == CPP_COMMA
11110 || token->type == CPP_EQ
11111 || token->type == CPP_GREATER)
11112 return cp_parser_type_parameter (parser, is_parameter_pack);
11113 }
11114
11115 /* Otherwise, it is a non-type parameter.
11116
11117 [temp.param]
11118
11119 When parsing a default template-argument for a non-type
11120 template-parameter, the first non-nested `>' is taken as the end
11121 of the template parameter-list rather than a greater-than
11122 operator. */
11123 *is_non_type = true;
11124 parameter_declarator
11125 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11126 /*parenthesized_p=*/NULL);
11127
11128 /* If the parameter declaration is marked as a parameter pack, set
11129 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11130 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11131 grokdeclarator. */
11132 if (parameter_declarator
11133 && parameter_declarator->declarator
11134 && parameter_declarator->declarator->parameter_pack_p)
11135 {
11136 *is_parameter_pack = true;
11137 parameter_declarator->declarator->parameter_pack_p = false;
11138 }
11139
11140 /* If the next token is an ellipsis, and we don't already have it
11141 marked as a parameter pack, then we have a parameter pack (that
11142 has no declarator). */
11143 if (!*is_parameter_pack
11144 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11145 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11146 {
11147 /* Consume the `...'. */
11148 cp_lexer_consume_token (parser->lexer);
11149 maybe_warn_variadic_templates ();
11150
11151 *is_parameter_pack = true;
11152 }
11153 /* We might end up with a pack expansion as the type of the non-type
11154 template parameter, in which case this is a non-type template
11155 parameter pack. */
11156 else if (parameter_declarator
11157 && parameter_declarator->decl_specifiers.type
11158 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11159 {
11160 *is_parameter_pack = true;
11161 parameter_declarator->decl_specifiers.type =
11162 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11163 }
11164
11165 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11166 {
11167 /* Parameter packs cannot have default arguments. However, a
11168 user may try to do so, so we'll parse them and give an
11169 appropriate diagnostic here. */
11170
11171 /* Consume the `='. */
11172 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11173 cp_lexer_consume_token (parser->lexer);
11174
11175 /* Find the name of the parameter pack. */
11176 id_declarator = parameter_declarator->declarator;
11177 while (id_declarator && id_declarator->kind != cdk_id)
11178 id_declarator = id_declarator->declarator;
11179
11180 if (id_declarator && id_declarator->kind == cdk_id)
11181 error_at (start_token->location,
11182 "template parameter pack %qD cannot have a default argument",
11183 id_declarator->u.id.unqualified_name);
11184 else
11185 error_at (start_token->location,
11186 "template parameter pack cannot have a default argument");
11187
11188 /* Parse the default argument, but throw away the result. */
11189 cp_parser_default_argument (parser, /*template_parm_p=*/true);
11190 }
11191
11192 parm = grokdeclarator (parameter_declarator->declarator,
11193 &parameter_declarator->decl_specifiers,
11194 TPARM, /*initialized=*/0,
11195 /*attrlist=*/NULL);
11196 if (parm == error_mark_node)
11197 return error_mark_node;
11198
11199 return build_tree_list (parameter_declarator->default_argument, parm);
11200 }
11201
11202 /* Parse a type-parameter.
11203
11204 type-parameter:
11205 class identifier [opt]
11206 class identifier [opt] = type-id
11207 typename identifier [opt]
11208 typename identifier [opt] = type-id
11209 template < template-parameter-list > class identifier [opt]
11210 template < template-parameter-list > class identifier [opt]
11211 = id-expression
11212
11213 GNU Extension (variadic templates):
11214
11215 type-parameter:
11216 class ... identifier [opt]
11217 typename ... identifier [opt]
11218
11219 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11220 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11221 the declaration of the parameter.
11222
11223 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11224
11225 static tree
11226 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11227 {
11228 cp_token *token;
11229 tree parameter;
11230
11231 /* Look for a keyword to tell us what kind of parameter this is. */
11232 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11233 if (!token)
11234 return error_mark_node;
11235
11236 switch (token->keyword)
11237 {
11238 case RID_CLASS:
11239 case RID_TYPENAME:
11240 {
11241 tree identifier;
11242 tree default_argument;
11243
11244 /* If the next token is an ellipsis, we have a template
11245 argument pack. */
11246 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11247 {
11248 /* Consume the `...' token. */
11249 cp_lexer_consume_token (parser->lexer);
11250 maybe_warn_variadic_templates ();
11251
11252 *is_parameter_pack = true;
11253 }
11254
11255 /* If the next token is an identifier, then it names the
11256 parameter. */
11257 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11258 identifier = cp_parser_identifier (parser);
11259 else
11260 identifier = NULL_TREE;
11261
11262 /* Create the parameter. */
11263 parameter = finish_template_type_parm (class_type_node, identifier);
11264
11265 /* If the next token is an `=', we have a default argument. */
11266 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11267 {
11268 /* Consume the `=' token. */
11269 cp_lexer_consume_token (parser->lexer);
11270 /* Parse the default-argument. */
11271 push_deferring_access_checks (dk_no_deferred);
11272 default_argument = cp_parser_type_id (parser);
11273
11274 /* Template parameter packs cannot have default
11275 arguments. */
11276 if (*is_parameter_pack)
11277 {
11278 if (identifier)
11279 error_at (token->location,
11280 "template parameter pack %qD cannot have a "
11281 "default argument", identifier);
11282 else
11283 error_at (token->location,
11284 "template parameter packs cannot have "
11285 "default arguments");
11286 default_argument = NULL_TREE;
11287 }
11288 pop_deferring_access_checks ();
11289 }
11290 else
11291 default_argument = NULL_TREE;
11292
11293 /* Create the combined representation of the parameter and the
11294 default argument. */
11295 parameter = build_tree_list (default_argument, parameter);
11296 }
11297 break;
11298
11299 case RID_TEMPLATE:
11300 {
11301 tree identifier;
11302 tree default_argument;
11303
11304 /* Look for the `<'. */
11305 cp_parser_require (parser, CPP_LESS, RT_LESS);
11306 /* Parse the template-parameter-list. */
11307 cp_parser_template_parameter_list (parser);
11308 /* Look for the `>'. */
11309 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11310 /* Look for the `class' keyword. */
11311 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11312 /* If the next token is an ellipsis, we have a template
11313 argument pack. */
11314 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11315 {
11316 /* Consume the `...' token. */
11317 cp_lexer_consume_token (parser->lexer);
11318 maybe_warn_variadic_templates ();
11319
11320 *is_parameter_pack = true;
11321 }
11322 /* If the next token is an `=', then there is a
11323 default-argument. If the next token is a `>', we are at
11324 the end of the parameter-list. If the next token is a `,',
11325 then we are at the end of this parameter. */
11326 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11327 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11328 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11329 {
11330 identifier = cp_parser_identifier (parser);
11331 /* Treat invalid names as if the parameter were nameless. */
11332 if (identifier == error_mark_node)
11333 identifier = NULL_TREE;
11334 }
11335 else
11336 identifier = NULL_TREE;
11337
11338 /* Create the template parameter. */
11339 parameter = finish_template_template_parm (class_type_node,
11340 identifier);
11341
11342 /* If the next token is an `=', then there is a
11343 default-argument. */
11344 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11345 {
11346 bool is_template;
11347
11348 /* Consume the `='. */
11349 cp_lexer_consume_token (parser->lexer);
11350 /* Parse the id-expression. */
11351 push_deferring_access_checks (dk_no_deferred);
11352 /* save token before parsing the id-expression, for error
11353 reporting */
11354 token = cp_lexer_peek_token (parser->lexer);
11355 default_argument
11356 = cp_parser_id_expression (parser,
11357 /*template_keyword_p=*/false,
11358 /*check_dependency_p=*/true,
11359 /*template_p=*/&is_template,
11360 /*declarator_p=*/false,
11361 /*optional_p=*/false);
11362 if (TREE_CODE (default_argument) == TYPE_DECL)
11363 /* If the id-expression was a template-id that refers to
11364 a template-class, we already have the declaration here,
11365 so no further lookup is needed. */
11366 ;
11367 else
11368 /* Look up the name. */
11369 default_argument
11370 = cp_parser_lookup_name (parser, default_argument,
11371 none_type,
11372 /*is_template=*/is_template,
11373 /*is_namespace=*/false,
11374 /*check_dependency=*/true,
11375 /*ambiguous_decls=*/NULL,
11376 token->location);
11377 /* See if the default argument is valid. */
11378 default_argument
11379 = check_template_template_default_arg (default_argument);
11380
11381 /* Template parameter packs cannot have default
11382 arguments. */
11383 if (*is_parameter_pack)
11384 {
11385 if (identifier)
11386 error_at (token->location,
11387 "template parameter pack %qD cannot "
11388 "have a default argument",
11389 identifier);
11390 else
11391 error_at (token->location, "template parameter packs cannot "
11392 "have default arguments");
11393 default_argument = NULL_TREE;
11394 }
11395 pop_deferring_access_checks ();
11396 }
11397 else
11398 default_argument = NULL_TREE;
11399
11400 /* Create the combined representation of the parameter and the
11401 default argument. */
11402 parameter = build_tree_list (default_argument, parameter);
11403 }
11404 break;
11405
11406 default:
11407 gcc_unreachable ();
11408 break;
11409 }
11410
11411 return parameter;
11412 }
11413
11414 /* Parse a template-id.
11415
11416 template-id:
11417 template-name < template-argument-list [opt] >
11418
11419 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11420 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11421 returned. Otherwise, if the template-name names a function, or set
11422 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11423 names a class, returns a TYPE_DECL for the specialization.
11424
11425 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11426 uninstantiated templates. */
11427
11428 static tree
11429 cp_parser_template_id (cp_parser *parser,
11430 bool template_keyword_p,
11431 bool check_dependency_p,
11432 bool is_declaration)
11433 {
11434 int i;
11435 tree templ;
11436 tree arguments;
11437 tree template_id;
11438 cp_token_position start_of_id = 0;
11439 deferred_access_check *chk;
11440 VEC (deferred_access_check,gc) *access_check;
11441 cp_token *next_token = NULL, *next_token_2 = NULL;
11442 bool is_identifier;
11443
11444 /* If the next token corresponds to a template-id, there is no need
11445 to reparse it. */
11446 next_token = cp_lexer_peek_token (parser->lexer);
11447 if (next_token->type == CPP_TEMPLATE_ID)
11448 {
11449 struct tree_check *check_value;
11450
11451 /* Get the stored value. */
11452 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11453 /* Perform any access checks that were deferred. */
11454 access_check = check_value->checks;
11455 if (access_check)
11456 {
11457 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11458 perform_or_defer_access_check (chk->binfo,
11459 chk->decl,
11460 chk->diag_decl);
11461 }
11462 /* Return the stored value. */
11463 return check_value->value;
11464 }
11465
11466 /* Avoid performing name lookup if there is no possibility of
11467 finding a template-id. */
11468 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11469 || (next_token->type == CPP_NAME
11470 && !cp_parser_nth_token_starts_template_argument_list_p
11471 (parser, 2)))
11472 {
11473 cp_parser_error (parser, "expected template-id");
11474 return error_mark_node;
11475 }
11476
11477 /* Remember where the template-id starts. */
11478 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11479 start_of_id = cp_lexer_token_position (parser->lexer, false);
11480
11481 push_deferring_access_checks (dk_deferred);
11482
11483 /* Parse the template-name. */
11484 is_identifier = false;
11485 templ = cp_parser_template_name (parser, template_keyword_p,
11486 check_dependency_p,
11487 is_declaration,
11488 &is_identifier);
11489 if (templ == error_mark_node || is_identifier)
11490 {
11491 pop_deferring_access_checks ();
11492 return templ;
11493 }
11494
11495 /* If we find the sequence `[:' after a template-name, it's probably
11496 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11497 parse correctly the argument list. */
11498 next_token = cp_lexer_peek_token (parser->lexer);
11499 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11500 if (next_token->type == CPP_OPEN_SQUARE
11501 && next_token->flags & DIGRAPH
11502 && next_token_2->type == CPP_COLON
11503 && !(next_token_2->flags & PREV_WHITE))
11504 {
11505 cp_parser_parse_tentatively (parser);
11506 /* Change `:' into `::'. */
11507 next_token_2->type = CPP_SCOPE;
11508 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11509 CPP_LESS. */
11510 cp_lexer_consume_token (parser->lexer);
11511
11512 /* Parse the arguments. */
11513 arguments = cp_parser_enclosed_template_argument_list (parser);
11514 if (!cp_parser_parse_definitely (parser))
11515 {
11516 /* If we couldn't parse an argument list, then we revert our changes
11517 and return simply an error. Maybe this is not a template-id
11518 after all. */
11519 next_token_2->type = CPP_COLON;
11520 cp_parser_error (parser, "expected %<<%>");
11521 pop_deferring_access_checks ();
11522 return error_mark_node;
11523 }
11524 /* Otherwise, emit an error about the invalid digraph, but continue
11525 parsing because we got our argument list. */
11526 if (permerror (next_token->location,
11527 "%<<::%> cannot begin a template-argument list"))
11528 {
11529 static bool hint = false;
11530 inform (next_token->location,
11531 "%<<:%> is an alternate spelling for %<[%>."
11532 " Insert whitespace between %<<%> and %<::%>");
11533 if (!hint && !flag_permissive)
11534 {
11535 inform (next_token->location, "(if you use %<-fpermissive%>"
11536 " G++ will accept your code)");
11537 hint = true;
11538 }
11539 }
11540 }
11541 else
11542 {
11543 /* Look for the `<' that starts the template-argument-list. */
11544 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11545 {
11546 pop_deferring_access_checks ();
11547 return error_mark_node;
11548 }
11549 /* Parse the arguments. */
11550 arguments = cp_parser_enclosed_template_argument_list (parser);
11551 }
11552
11553 /* Build a representation of the specialization. */
11554 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11555 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11556 else if (DECL_CLASS_TEMPLATE_P (templ)
11557 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11558 {
11559 bool entering_scope;
11560 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11561 template (rather than some instantiation thereof) only if
11562 is not nested within some other construct. For example, in
11563 "template <typename T> void f(T) { A<T>::", A<T> is just an
11564 instantiation of A. */
11565 entering_scope = (template_parm_scope_p ()
11566 && cp_lexer_next_token_is (parser->lexer,
11567 CPP_SCOPE));
11568 template_id
11569 = finish_template_type (templ, arguments, entering_scope);
11570 }
11571 else
11572 {
11573 /* If it's not a class-template or a template-template, it should be
11574 a function-template. */
11575 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11576 || TREE_CODE (templ) == OVERLOAD
11577 || BASELINK_P (templ)));
11578
11579 template_id = lookup_template_function (templ, arguments);
11580 }
11581
11582 /* If parsing tentatively, replace the sequence of tokens that makes
11583 up the template-id with a CPP_TEMPLATE_ID token. That way,
11584 should we re-parse the token stream, we will not have to repeat
11585 the effort required to do the parse, nor will we issue duplicate
11586 error messages about problems during instantiation of the
11587 template. */
11588 if (start_of_id)
11589 {
11590 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11591
11592 /* Reset the contents of the START_OF_ID token. */
11593 token->type = CPP_TEMPLATE_ID;
11594 /* Retrieve any deferred checks. Do not pop this access checks yet
11595 so the memory will not be reclaimed during token replacing below. */
11596 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11597 token->u.tree_check_value->value = template_id;
11598 token->u.tree_check_value->checks = get_deferred_access_checks ();
11599 token->keyword = RID_MAX;
11600
11601 /* Purge all subsequent tokens. */
11602 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11603
11604 /* ??? Can we actually assume that, if template_id ==
11605 error_mark_node, we will have issued a diagnostic to the
11606 user, as opposed to simply marking the tentative parse as
11607 failed? */
11608 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11609 error_at (token->location, "parse error in template argument list");
11610 }
11611
11612 pop_deferring_access_checks ();
11613 return template_id;
11614 }
11615
11616 /* Parse a template-name.
11617
11618 template-name:
11619 identifier
11620
11621 The standard should actually say:
11622
11623 template-name:
11624 identifier
11625 operator-function-id
11626
11627 A defect report has been filed about this issue.
11628
11629 A conversion-function-id cannot be a template name because they cannot
11630 be part of a template-id. In fact, looking at this code:
11631
11632 a.operator K<int>()
11633
11634 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11635 It is impossible to call a templated conversion-function-id with an
11636 explicit argument list, since the only allowed template parameter is
11637 the type to which it is converting.
11638
11639 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11640 `template' keyword, in a construction like:
11641
11642 T::template f<3>()
11643
11644 In that case `f' is taken to be a template-name, even though there
11645 is no way of knowing for sure.
11646
11647 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11648 name refers to a set of overloaded functions, at least one of which
11649 is a template, or an IDENTIFIER_NODE with the name of the template,
11650 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11651 names are looked up inside uninstantiated templates. */
11652
11653 static tree
11654 cp_parser_template_name (cp_parser* parser,
11655 bool template_keyword_p,
11656 bool check_dependency_p,
11657 bool is_declaration,
11658 bool *is_identifier)
11659 {
11660 tree identifier;
11661 tree decl;
11662 tree fns;
11663 cp_token *token = cp_lexer_peek_token (parser->lexer);
11664
11665 /* If the next token is `operator', then we have either an
11666 operator-function-id or a conversion-function-id. */
11667 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11668 {
11669 /* We don't know whether we're looking at an
11670 operator-function-id or a conversion-function-id. */
11671 cp_parser_parse_tentatively (parser);
11672 /* Try an operator-function-id. */
11673 identifier = cp_parser_operator_function_id (parser);
11674 /* If that didn't work, try a conversion-function-id. */
11675 if (!cp_parser_parse_definitely (parser))
11676 {
11677 cp_parser_error (parser, "expected template-name");
11678 return error_mark_node;
11679 }
11680 }
11681 /* Look for the identifier. */
11682 else
11683 identifier = cp_parser_identifier (parser);
11684
11685 /* If we didn't find an identifier, we don't have a template-id. */
11686 if (identifier == error_mark_node)
11687 return error_mark_node;
11688
11689 /* If the name immediately followed the `template' keyword, then it
11690 is a template-name. However, if the next token is not `<', then
11691 we do not treat it as a template-name, since it is not being used
11692 as part of a template-id. This enables us to handle constructs
11693 like:
11694
11695 template <typename T> struct S { S(); };
11696 template <typename T> S<T>::S();
11697
11698 correctly. We would treat `S' as a template -- if it were `S<T>'
11699 -- but we do not if there is no `<'. */
11700
11701 if (processing_template_decl
11702 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11703 {
11704 /* In a declaration, in a dependent context, we pretend that the
11705 "template" keyword was present in order to improve error
11706 recovery. For example, given:
11707
11708 template <typename T> void f(T::X<int>);
11709
11710 we want to treat "X<int>" as a template-id. */
11711 if (is_declaration
11712 && !template_keyword_p
11713 && parser->scope && TYPE_P (parser->scope)
11714 && check_dependency_p
11715 && dependent_scope_p (parser->scope)
11716 /* Do not do this for dtors (or ctors), since they never
11717 need the template keyword before their name. */
11718 && !constructor_name_p (identifier, parser->scope))
11719 {
11720 cp_token_position start = 0;
11721
11722 /* Explain what went wrong. */
11723 error_at (token->location, "non-template %qD used as template",
11724 identifier);
11725 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11726 parser->scope, identifier);
11727 /* If parsing tentatively, find the location of the "<" token. */
11728 if (cp_parser_simulate_error (parser))
11729 start = cp_lexer_token_position (parser->lexer, true);
11730 /* Parse the template arguments so that we can issue error
11731 messages about them. */
11732 cp_lexer_consume_token (parser->lexer);
11733 cp_parser_enclosed_template_argument_list (parser);
11734 /* Skip tokens until we find a good place from which to
11735 continue parsing. */
11736 cp_parser_skip_to_closing_parenthesis (parser,
11737 /*recovering=*/true,
11738 /*or_comma=*/true,
11739 /*consume_paren=*/false);
11740 /* If parsing tentatively, permanently remove the
11741 template argument list. That will prevent duplicate
11742 error messages from being issued about the missing
11743 "template" keyword. */
11744 if (start)
11745 cp_lexer_purge_tokens_after (parser->lexer, start);
11746 if (is_identifier)
11747 *is_identifier = true;
11748 return identifier;
11749 }
11750
11751 /* If the "template" keyword is present, then there is generally
11752 no point in doing name-lookup, so we just return IDENTIFIER.
11753 But, if the qualifying scope is non-dependent then we can
11754 (and must) do name-lookup normally. */
11755 if (template_keyword_p
11756 && (!parser->scope
11757 || (TYPE_P (parser->scope)
11758 && dependent_type_p (parser->scope))))
11759 return identifier;
11760 }
11761
11762 /* Look up the name. */
11763 decl = cp_parser_lookup_name (parser, identifier,
11764 none_type,
11765 /*is_template=*/true,
11766 /*is_namespace=*/false,
11767 check_dependency_p,
11768 /*ambiguous_decls=*/NULL,
11769 token->location);
11770
11771 /* If DECL is a template, then the name was a template-name. */
11772 if (TREE_CODE (decl) == TEMPLATE_DECL)
11773 ;
11774 else
11775 {
11776 tree fn = NULL_TREE;
11777
11778 /* The standard does not explicitly indicate whether a name that
11779 names a set of overloaded declarations, some of which are
11780 templates, is a template-name. However, such a name should
11781 be a template-name; otherwise, there is no way to form a
11782 template-id for the overloaded templates. */
11783 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11784 if (TREE_CODE (fns) == OVERLOAD)
11785 for (fn = fns; fn; fn = OVL_NEXT (fn))
11786 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11787 break;
11788
11789 if (!fn)
11790 {
11791 /* The name does not name a template. */
11792 cp_parser_error (parser, "expected template-name");
11793 return error_mark_node;
11794 }
11795 }
11796
11797 /* If DECL is dependent, and refers to a function, then just return
11798 its name; we will look it up again during template instantiation. */
11799 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11800 {
11801 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11802 if (TYPE_P (scope) && dependent_type_p (scope))
11803 return identifier;
11804 }
11805
11806 return decl;
11807 }
11808
11809 /* Parse a template-argument-list.
11810
11811 template-argument-list:
11812 template-argument ... [opt]
11813 template-argument-list , template-argument ... [opt]
11814
11815 Returns a TREE_VEC containing the arguments. */
11816
11817 static tree
11818 cp_parser_template_argument_list (cp_parser* parser)
11819 {
11820 tree fixed_args[10];
11821 unsigned n_args = 0;
11822 unsigned alloced = 10;
11823 tree *arg_ary = fixed_args;
11824 tree vec;
11825 bool saved_in_template_argument_list_p;
11826 bool saved_ice_p;
11827 bool saved_non_ice_p;
11828
11829 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11830 parser->in_template_argument_list_p = true;
11831 /* Even if the template-id appears in an integral
11832 constant-expression, the contents of the argument list do
11833 not. */
11834 saved_ice_p = parser->integral_constant_expression_p;
11835 parser->integral_constant_expression_p = false;
11836 saved_non_ice_p = parser->non_integral_constant_expression_p;
11837 parser->non_integral_constant_expression_p = false;
11838 /* Parse the arguments. */
11839 do
11840 {
11841 tree argument;
11842
11843 if (n_args)
11844 /* Consume the comma. */
11845 cp_lexer_consume_token (parser->lexer);
11846
11847 /* Parse the template-argument. */
11848 argument = cp_parser_template_argument (parser);
11849
11850 /* If the next token is an ellipsis, we're expanding a template
11851 argument pack. */
11852 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11853 {
11854 if (argument == error_mark_node)
11855 {
11856 cp_token *token = cp_lexer_peek_token (parser->lexer);
11857 error_at (token->location,
11858 "expected parameter pack before %<...%>");
11859 }
11860 /* Consume the `...' token. */
11861 cp_lexer_consume_token (parser->lexer);
11862
11863 /* Make the argument into a TYPE_PACK_EXPANSION or
11864 EXPR_PACK_EXPANSION. */
11865 argument = make_pack_expansion (argument);
11866 }
11867
11868 if (n_args == alloced)
11869 {
11870 alloced *= 2;
11871
11872 if (arg_ary == fixed_args)
11873 {
11874 arg_ary = XNEWVEC (tree, alloced);
11875 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11876 }
11877 else
11878 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11879 }
11880 arg_ary[n_args++] = argument;
11881 }
11882 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11883
11884 vec = make_tree_vec (n_args);
11885
11886 while (n_args--)
11887 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11888
11889 if (arg_ary != fixed_args)
11890 free (arg_ary);
11891 parser->non_integral_constant_expression_p = saved_non_ice_p;
11892 parser->integral_constant_expression_p = saved_ice_p;
11893 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11894 #ifdef ENABLE_CHECKING
11895 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11896 #endif
11897 return vec;
11898 }
11899
11900 /* Parse a template-argument.
11901
11902 template-argument:
11903 assignment-expression
11904 type-id
11905 id-expression
11906
11907 The representation is that of an assignment-expression, type-id, or
11908 id-expression -- except that the qualified id-expression is
11909 evaluated, so that the value returned is either a DECL or an
11910 OVERLOAD.
11911
11912 Although the standard says "assignment-expression", it forbids
11913 throw-expressions or assignments in the template argument.
11914 Therefore, we use "conditional-expression" instead. */
11915
11916 static tree
11917 cp_parser_template_argument (cp_parser* parser)
11918 {
11919 tree argument;
11920 bool template_p;
11921 bool address_p;
11922 bool maybe_type_id = false;
11923 cp_token *token = NULL, *argument_start_token = NULL;
11924 cp_id_kind idk;
11925
11926 /* There's really no way to know what we're looking at, so we just
11927 try each alternative in order.
11928
11929 [temp.arg]
11930
11931 In a template-argument, an ambiguity between a type-id and an
11932 expression is resolved to a type-id, regardless of the form of
11933 the corresponding template-parameter.
11934
11935 Therefore, we try a type-id first. */
11936 cp_parser_parse_tentatively (parser);
11937 argument = cp_parser_template_type_arg (parser);
11938 /* If there was no error parsing the type-id but the next token is a
11939 '>>', our behavior depends on which dialect of C++ we're
11940 parsing. In C++98, we probably found a typo for '> >'. But there
11941 are type-id which are also valid expressions. For instance:
11942
11943 struct X { int operator >> (int); };
11944 template <int V> struct Foo {};
11945 Foo<X () >> 5> r;
11946
11947 Here 'X()' is a valid type-id of a function type, but the user just
11948 wanted to write the expression "X() >> 5". Thus, we remember that we
11949 found a valid type-id, but we still try to parse the argument as an
11950 expression to see what happens.
11951
11952 In C++0x, the '>>' will be considered two separate '>'
11953 tokens. */
11954 if (!cp_parser_error_occurred (parser)
11955 && cxx_dialect == cxx98
11956 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11957 {
11958 maybe_type_id = true;
11959 cp_parser_abort_tentative_parse (parser);
11960 }
11961 else
11962 {
11963 /* If the next token isn't a `,' or a `>', then this argument wasn't
11964 really finished. This means that the argument is not a valid
11965 type-id. */
11966 if (!cp_parser_next_token_ends_template_argument_p (parser))
11967 cp_parser_error (parser, "expected template-argument");
11968 /* If that worked, we're done. */
11969 if (cp_parser_parse_definitely (parser))
11970 return argument;
11971 }
11972 /* We're still not sure what the argument will be. */
11973 cp_parser_parse_tentatively (parser);
11974 /* Try a template. */
11975 argument_start_token = cp_lexer_peek_token (parser->lexer);
11976 argument = cp_parser_id_expression (parser,
11977 /*template_keyword_p=*/false,
11978 /*check_dependency_p=*/true,
11979 &template_p,
11980 /*declarator_p=*/false,
11981 /*optional_p=*/false);
11982 /* If the next token isn't a `,' or a `>', then this argument wasn't
11983 really finished. */
11984 if (!cp_parser_next_token_ends_template_argument_p (parser))
11985 cp_parser_error (parser, "expected template-argument");
11986 if (!cp_parser_error_occurred (parser))
11987 {
11988 /* Figure out what is being referred to. If the id-expression
11989 was for a class template specialization, then we will have a
11990 TYPE_DECL at this point. There is no need to do name lookup
11991 at this point in that case. */
11992 if (TREE_CODE (argument) != TYPE_DECL)
11993 argument = cp_parser_lookup_name (parser, argument,
11994 none_type,
11995 /*is_template=*/template_p,
11996 /*is_namespace=*/false,
11997 /*check_dependency=*/true,
11998 /*ambiguous_decls=*/NULL,
11999 argument_start_token->location);
12000 if (TREE_CODE (argument) != TEMPLATE_DECL
12001 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12002 cp_parser_error (parser, "expected template-name");
12003 }
12004 if (cp_parser_parse_definitely (parser))
12005 return argument;
12006 /* It must be a non-type argument. There permitted cases are given
12007 in [temp.arg.nontype]:
12008
12009 -- an integral constant-expression of integral or enumeration
12010 type; or
12011
12012 -- the name of a non-type template-parameter; or
12013
12014 -- the name of an object or function with external linkage...
12015
12016 -- the address of an object or function with external linkage...
12017
12018 -- a pointer to member... */
12019 /* Look for a non-type template parameter. */
12020 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12021 {
12022 cp_parser_parse_tentatively (parser);
12023 argument = cp_parser_primary_expression (parser,
12024 /*address_p=*/false,
12025 /*cast_p=*/false,
12026 /*template_arg_p=*/true,
12027 &idk);
12028 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12029 || !cp_parser_next_token_ends_template_argument_p (parser))
12030 cp_parser_simulate_error (parser);
12031 if (cp_parser_parse_definitely (parser))
12032 return argument;
12033 }
12034
12035 /* If the next token is "&", the argument must be the address of an
12036 object or function with external linkage. */
12037 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12038 if (address_p)
12039 cp_lexer_consume_token (parser->lexer);
12040 /* See if we might have an id-expression. */
12041 token = cp_lexer_peek_token (parser->lexer);
12042 if (token->type == CPP_NAME
12043 || token->keyword == RID_OPERATOR
12044 || token->type == CPP_SCOPE
12045 || token->type == CPP_TEMPLATE_ID
12046 || token->type == CPP_NESTED_NAME_SPECIFIER)
12047 {
12048 cp_parser_parse_tentatively (parser);
12049 argument = cp_parser_primary_expression (parser,
12050 address_p,
12051 /*cast_p=*/false,
12052 /*template_arg_p=*/true,
12053 &idk);
12054 if (cp_parser_error_occurred (parser)
12055 || !cp_parser_next_token_ends_template_argument_p (parser))
12056 cp_parser_abort_tentative_parse (parser);
12057 else
12058 {
12059 tree probe;
12060
12061 if (TREE_CODE (argument) == INDIRECT_REF)
12062 {
12063 gcc_assert (REFERENCE_REF_P (argument));
12064 argument = TREE_OPERAND (argument, 0);
12065 }
12066
12067 /* If we're in a template, we represent a qualified-id referring
12068 to a static data member as a SCOPE_REF even if the scope isn't
12069 dependent so that we can check access control later. */
12070 probe = argument;
12071 if (TREE_CODE (probe) == SCOPE_REF)
12072 probe = TREE_OPERAND (probe, 1);
12073 if (TREE_CODE (probe) == VAR_DECL)
12074 {
12075 /* A variable without external linkage might still be a
12076 valid constant-expression, so no error is issued here
12077 if the external-linkage check fails. */
12078 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12079 cp_parser_simulate_error (parser);
12080 }
12081 else if (is_overloaded_fn (argument))
12082 /* All overloaded functions are allowed; if the external
12083 linkage test does not pass, an error will be issued
12084 later. */
12085 ;
12086 else if (address_p
12087 && (TREE_CODE (argument) == OFFSET_REF
12088 || TREE_CODE (argument) == SCOPE_REF))
12089 /* A pointer-to-member. */
12090 ;
12091 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12092 ;
12093 else
12094 cp_parser_simulate_error (parser);
12095
12096 if (cp_parser_parse_definitely (parser))
12097 {
12098 if (address_p)
12099 argument = build_x_unary_op (ADDR_EXPR, argument,
12100 tf_warning_or_error);
12101 return argument;
12102 }
12103 }
12104 }
12105 /* If the argument started with "&", there are no other valid
12106 alternatives at this point. */
12107 if (address_p)
12108 {
12109 cp_parser_error (parser, "invalid non-type template argument");
12110 return error_mark_node;
12111 }
12112
12113 /* If the argument wasn't successfully parsed as a type-id followed
12114 by '>>', the argument can only be a constant expression now.
12115 Otherwise, we try parsing the constant-expression tentatively,
12116 because the argument could really be a type-id. */
12117 if (maybe_type_id)
12118 cp_parser_parse_tentatively (parser);
12119 argument = cp_parser_constant_expression (parser,
12120 /*allow_non_constant_p=*/false,
12121 /*non_constant_p=*/NULL);
12122 argument = fold_non_dependent_expr (argument);
12123 if (!maybe_type_id)
12124 return argument;
12125 if (!cp_parser_next_token_ends_template_argument_p (parser))
12126 cp_parser_error (parser, "expected template-argument");
12127 if (cp_parser_parse_definitely (parser))
12128 return argument;
12129 /* We did our best to parse the argument as a non type-id, but that
12130 was the only alternative that matched (albeit with a '>' after
12131 it). We can assume it's just a typo from the user, and a
12132 diagnostic will then be issued. */
12133 return cp_parser_template_type_arg (parser);
12134 }
12135
12136 /* Parse an explicit-instantiation.
12137
12138 explicit-instantiation:
12139 template declaration
12140
12141 Although the standard says `declaration', what it really means is:
12142
12143 explicit-instantiation:
12144 template decl-specifier-seq [opt] declarator [opt] ;
12145
12146 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12147 supposed to be allowed. A defect report has been filed about this
12148 issue.
12149
12150 GNU Extension:
12151
12152 explicit-instantiation:
12153 storage-class-specifier template
12154 decl-specifier-seq [opt] declarator [opt] ;
12155 function-specifier template
12156 decl-specifier-seq [opt] declarator [opt] ; */
12157
12158 static void
12159 cp_parser_explicit_instantiation (cp_parser* parser)
12160 {
12161 int declares_class_or_enum;
12162 cp_decl_specifier_seq decl_specifiers;
12163 tree extension_specifier = NULL_TREE;
12164
12165 /* Look for an (optional) storage-class-specifier or
12166 function-specifier. */
12167 if (cp_parser_allow_gnu_extensions_p (parser))
12168 {
12169 extension_specifier
12170 = cp_parser_storage_class_specifier_opt (parser);
12171 if (!extension_specifier)
12172 extension_specifier
12173 = cp_parser_function_specifier_opt (parser,
12174 /*decl_specs=*/NULL);
12175 }
12176
12177 /* Look for the `template' keyword. */
12178 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12179 /* Let the front end know that we are processing an explicit
12180 instantiation. */
12181 begin_explicit_instantiation ();
12182 /* [temp.explicit] says that we are supposed to ignore access
12183 control while processing explicit instantiation directives. */
12184 push_deferring_access_checks (dk_no_check);
12185 /* Parse a decl-specifier-seq. */
12186 cp_parser_decl_specifier_seq (parser,
12187 CP_PARSER_FLAGS_OPTIONAL,
12188 &decl_specifiers,
12189 &declares_class_or_enum);
12190 /* If there was exactly one decl-specifier, and it declared a class,
12191 and there's no declarator, then we have an explicit type
12192 instantiation. */
12193 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12194 {
12195 tree type;
12196
12197 type = check_tag_decl (&decl_specifiers);
12198 /* Turn access control back on for names used during
12199 template instantiation. */
12200 pop_deferring_access_checks ();
12201 if (type)
12202 do_type_instantiation (type, extension_specifier,
12203 /*complain=*/tf_error);
12204 }
12205 else
12206 {
12207 cp_declarator *declarator;
12208 tree decl;
12209
12210 /* Parse the declarator. */
12211 declarator
12212 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12213 /*ctor_dtor_or_conv_p=*/NULL,
12214 /*parenthesized_p=*/NULL,
12215 /*member_p=*/false);
12216 if (declares_class_or_enum & 2)
12217 cp_parser_check_for_definition_in_return_type (declarator,
12218 decl_specifiers.type,
12219 decl_specifiers.type_location);
12220 if (declarator != cp_error_declarator)
12221 {
12222 decl = grokdeclarator (declarator, &decl_specifiers,
12223 NORMAL, 0, &decl_specifiers.attributes);
12224 /* Turn access control back on for names used during
12225 template instantiation. */
12226 pop_deferring_access_checks ();
12227 /* Do the explicit instantiation. */
12228 do_decl_instantiation (decl, extension_specifier);
12229 }
12230 else
12231 {
12232 pop_deferring_access_checks ();
12233 /* Skip the body of the explicit instantiation. */
12234 cp_parser_skip_to_end_of_statement (parser);
12235 }
12236 }
12237 /* We're done with the instantiation. */
12238 end_explicit_instantiation ();
12239
12240 cp_parser_consume_semicolon_at_end_of_statement (parser);
12241 }
12242
12243 /* Parse an explicit-specialization.
12244
12245 explicit-specialization:
12246 template < > declaration
12247
12248 Although the standard says `declaration', what it really means is:
12249
12250 explicit-specialization:
12251 template <> decl-specifier [opt] init-declarator [opt] ;
12252 template <> function-definition
12253 template <> explicit-specialization
12254 template <> template-declaration */
12255
12256 static void
12257 cp_parser_explicit_specialization (cp_parser* parser)
12258 {
12259 bool need_lang_pop;
12260 cp_token *token = cp_lexer_peek_token (parser->lexer);
12261
12262 /* Look for the `template' keyword. */
12263 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12264 /* Look for the `<'. */
12265 cp_parser_require (parser, CPP_LESS, RT_LESS);
12266 /* Look for the `>'. */
12267 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12268 /* We have processed another parameter list. */
12269 ++parser->num_template_parameter_lists;
12270 /* [temp]
12271
12272 A template ... explicit specialization ... shall not have C
12273 linkage. */
12274 if (current_lang_name == lang_name_c)
12275 {
12276 error_at (token->location, "template specialization with C linkage");
12277 /* Give it C++ linkage to avoid confusing other parts of the
12278 front end. */
12279 push_lang_context (lang_name_cplusplus);
12280 need_lang_pop = true;
12281 }
12282 else
12283 need_lang_pop = false;
12284 /* Let the front end know that we are beginning a specialization. */
12285 if (!begin_specialization ())
12286 {
12287 end_specialization ();
12288 return;
12289 }
12290
12291 /* If the next keyword is `template', we need to figure out whether
12292 or not we're looking a template-declaration. */
12293 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12294 {
12295 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12296 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12297 cp_parser_template_declaration_after_export (parser,
12298 /*member_p=*/false);
12299 else
12300 cp_parser_explicit_specialization (parser);
12301 }
12302 else
12303 /* Parse the dependent declaration. */
12304 cp_parser_single_declaration (parser,
12305 /*checks=*/NULL,
12306 /*member_p=*/false,
12307 /*explicit_specialization_p=*/true,
12308 /*friend_p=*/NULL);
12309 /* We're done with the specialization. */
12310 end_specialization ();
12311 /* For the erroneous case of a template with C linkage, we pushed an
12312 implicit C++ linkage scope; exit that scope now. */
12313 if (need_lang_pop)
12314 pop_lang_context ();
12315 /* We're done with this parameter list. */
12316 --parser->num_template_parameter_lists;
12317 }
12318
12319 /* Parse a type-specifier.
12320
12321 type-specifier:
12322 simple-type-specifier
12323 class-specifier
12324 enum-specifier
12325 elaborated-type-specifier
12326 cv-qualifier
12327
12328 GNU Extension:
12329
12330 type-specifier:
12331 __complex__
12332
12333 Returns a representation of the type-specifier. For a
12334 class-specifier, enum-specifier, or elaborated-type-specifier, a
12335 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12336
12337 The parser flags FLAGS is used to control type-specifier parsing.
12338
12339 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12340 in a decl-specifier-seq.
12341
12342 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12343 class-specifier, enum-specifier, or elaborated-type-specifier, then
12344 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12345 if a type is declared; 2 if it is defined. Otherwise, it is set to
12346 zero.
12347
12348 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12349 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12350 is set to FALSE. */
12351
12352 static tree
12353 cp_parser_type_specifier (cp_parser* parser,
12354 cp_parser_flags flags,
12355 cp_decl_specifier_seq *decl_specs,
12356 bool is_declaration,
12357 int* declares_class_or_enum,
12358 bool* is_cv_qualifier)
12359 {
12360 tree type_spec = NULL_TREE;
12361 cp_token *token;
12362 enum rid keyword;
12363 cp_decl_spec ds = ds_last;
12364
12365 /* Assume this type-specifier does not declare a new type. */
12366 if (declares_class_or_enum)
12367 *declares_class_or_enum = 0;
12368 /* And that it does not specify a cv-qualifier. */
12369 if (is_cv_qualifier)
12370 *is_cv_qualifier = false;
12371 /* Peek at the next token. */
12372 token = cp_lexer_peek_token (parser->lexer);
12373
12374 /* If we're looking at a keyword, we can use that to guide the
12375 production we choose. */
12376 keyword = token->keyword;
12377 switch (keyword)
12378 {
12379 case RID_ENUM:
12380 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12381 goto elaborated_type_specifier;
12382
12383 /* Look for the enum-specifier. */
12384 type_spec = cp_parser_enum_specifier (parser);
12385 /* If that worked, we're done. */
12386 if (type_spec)
12387 {
12388 if (declares_class_or_enum)
12389 *declares_class_or_enum = 2;
12390 if (decl_specs)
12391 cp_parser_set_decl_spec_type (decl_specs,
12392 type_spec,
12393 token->location,
12394 /*user_defined_p=*/true);
12395 return type_spec;
12396 }
12397 else
12398 goto elaborated_type_specifier;
12399
12400 /* Any of these indicate either a class-specifier, or an
12401 elaborated-type-specifier. */
12402 case RID_CLASS:
12403 case RID_STRUCT:
12404 case RID_UNION:
12405 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12406 goto elaborated_type_specifier;
12407
12408 /* Parse tentatively so that we can back up if we don't find a
12409 class-specifier. */
12410 cp_parser_parse_tentatively (parser);
12411 /* Look for the class-specifier. */
12412 type_spec = cp_parser_class_specifier (parser);
12413 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12414 /* If that worked, we're done. */
12415 if (cp_parser_parse_definitely (parser))
12416 {
12417 if (declares_class_or_enum)
12418 *declares_class_or_enum = 2;
12419 if (decl_specs)
12420 cp_parser_set_decl_spec_type (decl_specs,
12421 type_spec,
12422 token->location,
12423 /*user_defined_p=*/true);
12424 return type_spec;
12425 }
12426
12427 /* Fall through. */
12428 elaborated_type_specifier:
12429 /* We're declaring (not defining) a class or enum. */
12430 if (declares_class_or_enum)
12431 *declares_class_or_enum = 1;
12432
12433 /* Fall through. */
12434 case RID_TYPENAME:
12435 /* Look for an elaborated-type-specifier. */
12436 type_spec
12437 = (cp_parser_elaborated_type_specifier
12438 (parser,
12439 decl_specs && decl_specs->specs[(int) ds_friend],
12440 is_declaration));
12441 if (decl_specs)
12442 cp_parser_set_decl_spec_type (decl_specs,
12443 type_spec,
12444 token->location,
12445 /*user_defined_p=*/true);
12446 return type_spec;
12447
12448 case RID_CONST:
12449 ds = ds_const;
12450 if (is_cv_qualifier)
12451 *is_cv_qualifier = true;
12452 break;
12453
12454 case RID_VOLATILE:
12455 ds = ds_volatile;
12456 if (is_cv_qualifier)
12457 *is_cv_qualifier = true;
12458 break;
12459
12460 case RID_RESTRICT:
12461 ds = ds_restrict;
12462 if (is_cv_qualifier)
12463 *is_cv_qualifier = true;
12464 break;
12465
12466 case RID_COMPLEX:
12467 /* The `__complex__' keyword is a GNU extension. */
12468 ds = ds_complex;
12469 break;
12470
12471 default:
12472 break;
12473 }
12474
12475 /* Handle simple keywords. */
12476 if (ds != ds_last)
12477 {
12478 if (decl_specs)
12479 {
12480 ++decl_specs->specs[(int)ds];
12481 decl_specs->any_specifiers_p = true;
12482 }
12483 return cp_lexer_consume_token (parser->lexer)->u.value;
12484 }
12485
12486 /* If we do not already have a type-specifier, assume we are looking
12487 at a simple-type-specifier. */
12488 type_spec = cp_parser_simple_type_specifier (parser,
12489 decl_specs,
12490 flags);
12491
12492 /* If we didn't find a type-specifier, and a type-specifier was not
12493 optional in this context, issue an error message. */
12494 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12495 {
12496 cp_parser_error (parser, "expected type specifier");
12497 return error_mark_node;
12498 }
12499
12500 return type_spec;
12501 }
12502
12503 /* Parse a simple-type-specifier.
12504
12505 simple-type-specifier:
12506 :: [opt] nested-name-specifier [opt] type-name
12507 :: [opt] nested-name-specifier template template-id
12508 char
12509 wchar_t
12510 bool
12511 short
12512 int
12513 long
12514 signed
12515 unsigned
12516 float
12517 double
12518 void
12519
12520 C++0x Extension:
12521
12522 simple-type-specifier:
12523 auto
12524 decltype ( expression )
12525 char16_t
12526 char32_t
12527
12528 GNU Extension:
12529
12530 simple-type-specifier:
12531 __int128
12532 __typeof__ unary-expression
12533 __typeof__ ( type-id )
12534
12535 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12536 appropriately updated. */
12537
12538 static tree
12539 cp_parser_simple_type_specifier (cp_parser* parser,
12540 cp_decl_specifier_seq *decl_specs,
12541 cp_parser_flags flags)
12542 {
12543 tree type = NULL_TREE;
12544 cp_token *token;
12545
12546 /* Peek at the next token. */
12547 token = cp_lexer_peek_token (parser->lexer);
12548
12549 /* If we're looking at a keyword, things are easy. */
12550 switch (token->keyword)
12551 {
12552 case RID_CHAR:
12553 if (decl_specs)
12554 decl_specs->explicit_char_p = true;
12555 type = char_type_node;
12556 break;
12557 case RID_CHAR16:
12558 type = char16_type_node;
12559 break;
12560 case RID_CHAR32:
12561 type = char32_type_node;
12562 break;
12563 case RID_WCHAR:
12564 type = wchar_type_node;
12565 break;
12566 case RID_BOOL:
12567 type = boolean_type_node;
12568 break;
12569 case RID_SHORT:
12570 if (decl_specs)
12571 ++decl_specs->specs[(int) ds_short];
12572 type = short_integer_type_node;
12573 break;
12574 case RID_INT:
12575 if (decl_specs)
12576 decl_specs->explicit_int_p = true;
12577 type = integer_type_node;
12578 break;
12579 case RID_INT128:
12580 if (!int128_integer_type_node)
12581 break;
12582 if (decl_specs)
12583 decl_specs->explicit_int128_p = true;
12584 type = int128_integer_type_node;
12585 break;
12586 case RID_LONG:
12587 if (decl_specs)
12588 ++decl_specs->specs[(int) ds_long];
12589 type = long_integer_type_node;
12590 break;
12591 case RID_SIGNED:
12592 if (decl_specs)
12593 ++decl_specs->specs[(int) ds_signed];
12594 type = integer_type_node;
12595 break;
12596 case RID_UNSIGNED:
12597 if (decl_specs)
12598 ++decl_specs->specs[(int) ds_unsigned];
12599 type = unsigned_type_node;
12600 break;
12601 case RID_FLOAT:
12602 type = float_type_node;
12603 break;
12604 case RID_DOUBLE:
12605 type = double_type_node;
12606 break;
12607 case RID_VOID:
12608 type = void_type_node;
12609 break;
12610
12611 case RID_AUTO:
12612 maybe_warn_cpp0x (CPP0X_AUTO);
12613 type = make_auto ();
12614 break;
12615
12616 case RID_DECLTYPE:
12617 /* Parse the `decltype' type. */
12618 type = cp_parser_decltype (parser);
12619
12620 if (decl_specs)
12621 cp_parser_set_decl_spec_type (decl_specs, type,
12622 token->location,
12623 /*user_defined_p=*/true);
12624
12625 return type;
12626
12627 case RID_TYPEOF:
12628 /* Consume the `typeof' token. */
12629 cp_lexer_consume_token (parser->lexer);
12630 /* Parse the operand to `typeof'. */
12631 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12632 /* If it is not already a TYPE, take its type. */
12633 if (!TYPE_P (type))
12634 type = finish_typeof (type);
12635
12636 if (decl_specs)
12637 cp_parser_set_decl_spec_type (decl_specs, type,
12638 token->location,
12639 /*user_defined_p=*/true);
12640
12641 return type;
12642
12643 default:
12644 break;
12645 }
12646
12647 /* If the type-specifier was for a built-in type, we're done. */
12648 if (type)
12649 {
12650 /* Record the type. */
12651 if (decl_specs
12652 && (token->keyword != RID_SIGNED
12653 && token->keyword != RID_UNSIGNED
12654 && token->keyword != RID_SHORT
12655 && token->keyword != RID_LONG))
12656 cp_parser_set_decl_spec_type (decl_specs,
12657 type,
12658 token->location,
12659 /*user_defined=*/false);
12660 if (decl_specs)
12661 decl_specs->any_specifiers_p = true;
12662
12663 /* Consume the token. */
12664 cp_lexer_consume_token (parser->lexer);
12665
12666 /* There is no valid C++ program where a non-template type is
12667 followed by a "<". That usually indicates that the user thought
12668 that the type was a template. */
12669 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12670
12671 return TYPE_NAME (type);
12672 }
12673
12674 /* The type-specifier must be a user-defined type. */
12675 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12676 {
12677 bool qualified_p;
12678 bool global_p;
12679
12680 /* Don't gobble tokens or issue error messages if this is an
12681 optional type-specifier. */
12682 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12683 cp_parser_parse_tentatively (parser);
12684
12685 /* Look for the optional `::' operator. */
12686 global_p
12687 = (cp_parser_global_scope_opt (parser,
12688 /*current_scope_valid_p=*/false)
12689 != NULL_TREE);
12690 /* Look for the nested-name specifier. */
12691 qualified_p
12692 = (cp_parser_nested_name_specifier_opt (parser,
12693 /*typename_keyword_p=*/false,
12694 /*check_dependency_p=*/true,
12695 /*type_p=*/false,
12696 /*is_declaration=*/false)
12697 != NULL_TREE);
12698 token = cp_lexer_peek_token (parser->lexer);
12699 /* If we have seen a nested-name-specifier, and the next token
12700 is `template', then we are using the template-id production. */
12701 if (parser->scope
12702 && cp_parser_optional_template_keyword (parser))
12703 {
12704 /* Look for the template-id. */
12705 type = cp_parser_template_id (parser,
12706 /*template_keyword_p=*/true,
12707 /*check_dependency_p=*/true,
12708 /*is_declaration=*/false);
12709 /* If the template-id did not name a type, we are out of
12710 luck. */
12711 if (TREE_CODE (type) != TYPE_DECL)
12712 {
12713 cp_parser_error (parser, "expected template-id for type");
12714 type = NULL_TREE;
12715 }
12716 }
12717 /* Otherwise, look for a type-name. */
12718 else
12719 type = cp_parser_type_name (parser);
12720 /* Keep track of all name-lookups performed in class scopes. */
12721 if (type
12722 && !global_p
12723 && !qualified_p
12724 && TREE_CODE (type) == TYPE_DECL
12725 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12726 maybe_note_name_used_in_class (DECL_NAME (type), type);
12727 /* If it didn't work out, we don't have a TYPE. */
12728 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12729 && !cp_parser_parse_definitely (parser))
12730 type = NULL_TREE;
12731 if (type && decl_specs)
12732 cp_parser_set_decl_spec_type (decl_specs, type,
12733 token->location,
12734 /*user_defined=*/true);
12735 }
12736
12737 /* If we didn't get a type-name, issue an error message. */
12738 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12739 {
12740 cp_parser_error (parser, "expected type-name");
12741 return error_mark_node;
12742 }
12743
12744 /* There is no valid C++ program where a non-template type is
12745 followed by a "<". That usually indicates that the user thought
12746 that the type was a template. */
12747 if (type && type != error_mark_node)
12748 {
12749 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12750 If it is, then the '<'...'>' enclose protocol names rather than
12751 template arguments, and so everything is fine. */
12752 if (c_dialect_objc ()
12753 && (objc_is_id (type) || objc_is_class_name (type)))
12754 {
12755 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12756 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12757
12758 /* Clobber the "unqualified" type previously entered into
12759 DECL_SPECS with the new, improved protocol-qualified version. */
12760 if (decl_specs)
12761 decl_specs->type = qual_type;
12762
12763 return qual_type;
12764 }
12765
12766 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12767 token->location);
12768 }
12769
12770 return type;
12771 }
12772
12773 /* Parse a type-name.
12774
12775 type-name:
12776 class-name
12777 enum-name
12778 typedef-name
12779
12780 enum-name:
12781 identifier
12782
12783 typedef-name:
12784 identifier
12785
12786 Returns a TYPE_DECL for the type. */
12787
12788 static tree
12789 cp_parser_type_name (cp_parser* parser)
12790 {
12791 tree type_decl;
12792
12793 /* We can't know yet whether it is a class-name or not. */
12794 cp_parser_parse_tentatively (parser);
12795 /* Try a class-name. */
12796 type_decl = cp_parser_class_name (parser,
12797 /*typename_keyword_p=*/false,
12798 /*template_keyword_p=*/false,
12799 none_type,
12800 /*check_dependency_p=*/true,
12801 /*class_head_p=*/false,
12802 /*is_declaration=*/false);
12803 /* If it's not a class-name, keep looking. */
12804 if (!cp_parser_parse_definitely (parser))
12805 {
12806 /* It must be a typedef-name or an enum-name. */
12807 return cp_parser_nonclass_name (parser);
12808 }
12809
12810 return type_decl;
12811 }
12812
12813 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12814
12815 enum-name:
12816 identifier
12817
12818 typedef-name:
12819 identifier
12820
12821 Returns a TYPE_DECL for the type. */
12822
12823 static tree
12824 cp_parser_nonclass_name (cp_parser* parser)
12825 {
12826 tree type_decl;
12827 tree identifier;
12828
12829 cp_token *token = cp_lexer_peek_token (parser->lexer);
12830 identifier = cp_parser_identifier (parser);
12831 if (identifier == error_mark_node)
12832 return error_mark_node;
12833
12834 /* Look up the type-name. */
12835 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12836
12837 if (TREE_CODE (type_decl) != TYPE_DECL
12838 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12839 {
12840 /* See if this is an Objective-C type. */
12841 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12842 tree type = objc_get_protocol_qualified_type (identifier, protos);
12843 if (type)
12844 type_decl = TYPE_NAME (type);
12845 }
12846
12847 /* Issue an error if we did not find a type-name. */
12848 if (TREE_CODE (type_decl) != TYPE_DECL)
12849 {
12850 if (!cp_parser_simulate_error (parser))
12851 cp_parser_name_lookup_error (parser, identifier, type_decl,
12852 NLE_TYPE, token->location);
12853 return error_mark_node;
12854 }
12855 /* Remember that the name was used in the definition of the
12856 current class so that we can check later to see if the
12857 meaning would have been different after the class was
12858 entirely defined. */
12859 else if (type_decl != error_mark_node
12860 && !parser->scope)
12861 maybe_note_name_used_in_class (identifier, type_decl);
12862
12863 return type_decl;
12864 }
12865
12866 /* Parse an elaborated-type-specifier. Note that the grammar given
12867 here incorporates the resolution to DR68.
12868
12869 elaborated-type-specifier:
12870 class-key :: [opt] nested-name-specifier [opt] identifier
12871 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12872 enum-key :: [opt] nested-name-specifier [opt] identifier
12873 typename :: [opt] nested-name-specifier identifier
12874 typename :: [opt] nested-name-specifier template [opt]
12875 template-id
12876
12877 GNU extension:
12878
12879 elaborated-type-specifier:
12880 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12881 class-key attributes :: [opt] nested-name-specifier [opt]
12882 template [opt] template-id
12883 enum attributes :: [opt] nested-name-specifier [opt] identifier
12884
12885 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12886 declared `friend'. If IS_DECLARATION is TRUE, then this
12887 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12888 something is being declared.
12889
12890 Returns the TYPE specified. */
12891
12892 static tree
12893 cp_parser_elaborated_type_specifier (cp_parser* parser,
12894 bool is_friend,
12895 bool is_declaration)
12896 {
12897 enum tag_types tag_type;
12898 tree identifier;
12899 tree type = NULL_TREE;
12900 tree attributes = NULL_TREE;
12901 tree globalscope;
12902 cp_token *token = NULL;
12903
12904 /* See if we're looking at the `enum' keyword. */
12905 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12906 {
12907 /* Consume the `enum' token. */
12908 cp_lexer_consume_token (parser->lexer);
12909 /* Remember that it's an enumeration type. */
12910 tag_type = enum_type;
12911 /* Parse the optional `struct' or `class' key (for C++0x scoped
12912 enums). */
12913 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12914 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12915 {
12916 if (cxx_dialect == cxx98)
12917 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12918
12919 /* Consume the `struct' or `class'. */
12920 cp_lexer_consume_token (parser->lexer);
12921 }
12922 /* Parse the attributes. */
12923 attributes = cp_parser_attributes_opt (parser);
12924 }
12925 /* Or, it might be `typename'. */
12926 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12927 RID_TYPENAME))
12928 {
12929 /* Consume the `typename' token. */
12930 cp_lexer_consume_token (parser->lexer);
12931 /* Remember that it's a `typename' type. */
12932 tag_type = typename_type;
12933 }
12934 /* Otherwise it must be a class-key. */
12935 else
12936 {
12937 tag_type = cp_parser_class_key (parser);
12938 if (tag_type == none_type)
12939 return error_mark_node;
12940 /* Parse the attributes. */
12941 attributes = cp_parser_attributes_opt (parser);
12942 }
12943
12944 /* Look for the `::' operator. */
12945 globalscope = cp_parser_global_scope_opt (parser,
12946 /*current_scope_valid_p=*/false);
12947 /* Look for the nested-name-specifier. */
12948 if (tag_type == typename_type && !globalscope)
12949 {
12950 if (!cp_parser_nested_name_specifier (parser,
12951 /*typename_keyword_p=*/true,
12952 /*check_dependency_p=*/true,
12953 /*type_p=*/true,
12954 is_declaration))
12955 return error_mark_node;
12956 }
12957 else
12958 /* Even though `typename' is not present, the proposed resolution
12959 to Core Issue 180 says that in `class A<T>::B', `B' should be
12960 considered a type-name, even if `A<T>' is dependent. */
12961 cp_parser_nested_name_specifier_opt (parser,
12962 /*typename_keyword_p=*/true,
12963 /*check_dependency_p=*/true,
12964 /*type_p=*/true,
12965 is_declaration);
12966 /* For everything but enumeration types, consider a template-id.
12967 For an enumeration type, consider only a plain identifier. */
12968 if (tag_type != enum_type)
12969 {
12970 bool template_p = false;
12971 tree decl;
12972
12973 /* Allow the `template' keyword. */
12974 template_p = cp_parser_optional_template_keyword (parser);
12975 /* If we didn't see `template', we don't know if there's a
12976 template-id or not. */
12977 if (!template_p)
12978 cp_parser_parse_tentatively (parser);
12979 /* Parse the template-id. */
12980 token = cp_lexer_peek_token (parser->lexer);
12981 decl = cp_parser_template_id (parser, template_p,
12982 /*check_dependency_p=*/true,
12983 is_declaration);
12984 /* If we didn't find a template-id, look for an ordinary
12985 identifier. */
12986 if (!template_p && !cp_parser_parse_definitely (parser))
12987 ;
12988 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12989 in effect, then we must assume that, upon instantiation, the
12990 template will correspond to a class. */
12991 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12992 && tag_type == typename_type)
12993 type = make_typename_type (parser->scope, decl,
12994 typename_type,
12995 /*complain=*/tf_error);
12996 /* If the `typename' keyword is in effect and DECL is not a type
12997 decl. Then type is non existant. */
12998 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12999 type = NULL_TREE;
13000 else
13001 type = TREE_TYPE (decl);
13002 }
13003
13004 if (!type)
13005 {
13006 token = cp_lexer_peek_token (parser->lexer);
13007 identifier = cp_parser_identifier (parser);
13008
13009 if (identifier == error_mark_node)
13010 {
13011 parser->scope = NULL_TREE;
13012 return error_mark_node;
13013 }
13014
13015 /* For a `typename', we needn't call xref_tag. */
13016 if (tag_type == typename_type
13017 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13018 return cp_parser_make_typename_type (parser, parser->scope,
13019 identifier,
13020 token->location);
13021 /* Look up a qualified name in the usual way. */
13022 if (parser->scope)
13023 {
13024 tree decl;
13025 tree ambiguous_decls;
13026
13027 decl = cp_parser_lookup_name (parser, identifier,
13028 tag_type,
13029 /*is_template=*/false,
13030 /*is_namespace=*/false,
13031 /*check_dependency=*/true,
13032 &ambiguous_decls,
13033 token->location);
13034
13035 /* If the lookup was ambiguous, an error will already have been
13036 issued. */
13037 if (ambiguous_decls)
13038 return error_mark_node;
13039
13040 /* If we are parsing friend declaration, DECL may be a
13041 TEMPLATE_DECL tree node here. However, we need to check
13042 whether this TEMPLATE_DECL results in valid code. Consider
13043 the following example:
13044
13045 namespace N {
13046 template <class T> class C {};
13047 }
13048 class X {
13049 template <class T> friend class N::C; // #1, valid code
13050 };
13051 template <class T> class Y {
13052 friend class N::C; // #2, invalid code
13053 };
13054
13055 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13056 name lookup of `N::C'. We see that friend declaration must
13057 be template for the code to be valid. Note that
13058 processing_template_decl does not work here since it is
13059 always 1 for the above two cases. */
13060
13061 decl = (cp_parser_maybe_treat_template_as_class
13062 (decl, /*tag_name_p=*/is_friend
13063 && parser->num_template_parameter_lists));
13064
13065 if (TREE_CODE (decl) != TYPE_DECL)
13066 {
13067 cp_parser_diagnose_invalid_type_name (parser,
13068 parser->scope,
13069 identifier,
13070 token->location);
13071 return error_mark_node;
13072 }
13073
13074 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13075 {
13076 bool allow_template = (parser->num_template_parameter_lists
13077 || DECL_SELF_REFERENCE_P (decl));
13078 type = check_elaborated_type_specifier (tag_type, decl,
13079 allow_template);
13080
13081 if (type == error_mark_node)
13082 return error_mark_node;
13083 }
13084
13085 /* Forward declarations of nested types, such as
13086
13087 class C1::C2;
13088 class C1::C2::C3;
13089
13090 are invalid unless all components preceding the final '::'
13091 are complete. If all enclosing types are complete, these
13092 declarations become merely pointless.
13093
13094 Invalid forward declarations of nested types are errors
13095 caught elsewhere in parsing. Those that are pointless arrive
13096 here. */
13097
13098 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13099 && !is_friend && !processing_explicit_instantiation)
13100 warning (0, "declaration %qD does not declare anything", decl);
13101
13102 type = TREE_TYPE (decl);
13103 }
13104 else
13105 {
13106 /* An elaborated-type-specifier sometimes introduces a new type and
13107 sometimes names an existing type. Normally, the rule is that it
13108 introduces a new type only if there is not an existing type of
13109 the same name already in scope. For example, given:
13110
13111 struct S {};
13112 void f() { struct S s; }
13113
13114 the `struct S' in the body of `f' is the same `struct S' as in
13115 the global scope; the existing definition is used. However, if
13116 there were no global declaration, this would introduce a new
13117 local class named `S'.
13118
13119 An exception to this rule applies to the following code:
13120
13121 namespace N { struct S; }
13122
13123 Here, the elaborated-type-specifier names a new type
13124 unconditionally; even if there is already an `S' in the
13125 containing scope this declaration names a new type.
13126 This exception only applies if the elaborated-type-specifier
13127 forms the complete declaration:
13128
13129 [class.name]
13130
13131 A declaration consisting solely of `class-key identifier ;' is
13132 either a redeclaration of the name in the current scope or a
13133 forward declaration of the identifier as a class name. It
13134 introduces the name into the current scope.
13135
13136 We are in this situation precisely when the next token is a `;'.
13137
13138 An exception to the exception is that a `friend' declaration does
13139 *not* name a new type; i.e., given:
13140
13141 struct S { friend struct T; };
13142
13143 `T' is not a new type in the scope of `S'.
13144
13145 Also, `new struct S' or `sizeof (struct S)' never results in the
13146 definition of a new type; a new type can only be declared in a
13147 declaration context. */
13148
13149 tag_scope ts;
13150 bool template_p;
13151
13152 if (is_friend)
13153 /* Friends have special name lookup rules. */
13154 ts = ts_within_enclosing_non_class;
13155 else if (is_declaration
13156 && cp_lexer_next_token_is (parser->lexer,
13157 CPP_SEMICOLON))
13158 /* This is a `class-key identifier ;' */
13159 ts = ts_current;
13160 else
13161 ts = ts_global;
13162
13163 template_p =
13164 (parser->num_template_parameter_lists
13165 && (cp_parser_next_token_starts_class_definition_p (parser)
13166 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13167 /* An unqualified name was used to reference this type, so
13168 there were no qualifying templates. */
13169 if (!cp_parser_check_template_parameters (parser,
13170 /*num_templates=*/0,
13171 token->location,
13172 /*declarator=*/NULL))
13173 return error_mark_node;
13174 type = xref_tag (tag_type, identifier, ts, template_p);
13175 }
13176 }
13177
13178 if (type == error_mark_node)
13179 return error_mark_node;
13180
13181 /* Allow attributes on forward declarations of classes. */
13182 if (attributes)
13183 {
13184 if (TREE_CODE (type) == TYPENAME_TYPE)
13185 warning (OPT_Wattributes,
13186 "attributes ignored on uninstantiated type");
13187 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13188 && ! processing_explicit_instantiation)
13189 warning (OPT_Wattributes,
13190 "attributes ignored on template instantiation");
13191 else if (is_declaration && cp_parser_declares_only_class_p (parser))
13192 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13193 else
13194 warning (OPT_Wattributes,
13195 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13196 }
13197
13198 if (tag_type != enum_type)
13199 cp_parser_check_class_key (tag_type, type);
13200
13201 /* A "<" cannot follow an elaborated type specifier. If that
13202 happens, the user was probably trying to form a template-id. */
13203 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13204
13205 return type;
13206 }
13207
13208 /* Parse an enum-specifier.
13209
13210 enum-specifier:
13211 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
13212
13213 enum-key:
13214 enum
13215 enum class [C++0x]
13216 enum struct [C++0x]
13217
13218 enum-base: [C++0x]
13219 : type-specifier-seq
13220
13221 GNU Extensions:
13222 enum-key attributes[opt] identifier [opt] enum-base [opt]
13223 { enumerator-list [opt] }attributes[opt]
13224
13225 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13226 if the token stream isn't an enum-specifier after all. */
13227
13228 static tree
13229 cp_parser_enum_specifier (cp_parser* parser)
13230 {
13231 tree identifier;
13232 tree type;
13233 tree attributes;
13234 bool scoped_enum_p = false;
13235 bool has_underlying_type = false;
13236 tree underlying_type = NULL_TREE;
13237
13238 /* Parse tentatively so that we can back up if we don't find a
13239 enum-specifier. */
13240 cp_parser_parse_tentatively (parser);
13241
13242 /* Caller guarantees that the current token is 'enum', an identifier
13243 possibly follows, and the token after that is an opening brace.
13244 If we don't have an identifier, fabricate an anonymous name for
13245 the enumeration being defined. */
13246 cp_lexer_consume_token (parser->lexer);
13247
13248 /* Parse the "class" or "struct", which indicates a scoped
13249 enumeration type in C++0x. */
13250 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13251 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13252 {
13253 if (cxx_dialect == cxx98)
13254 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13255
13256 /* Consume the `struct' or `class' token. */
13257 cp_lexer_consume_token (parser->lexer);
13258
13259 scoped_enum_p = true;
13260 }
13261
13262 attributes = cp_parser_attributes_opt (parser);
13263
13264 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13265 identifier = cp_parser_identifier (parser);
13266 else
13267 identifier = make_anon_name ();
13268
13269 /* Check for the `:' that denotes a specified underlying type in C++0x.
13270 Note that a ':' could also indicate a bitfield width, however. */
13271 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13272 {
13273 cp_decl_specifier_seq type_specifiers;
13274
13275 /* Consume the `:'. */
13276 cp_lexer_consume_token (parser->lexer);
13277
13278 /* Parse the type-specifier-seq. */
13279 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13280 /*is_trailing_return=*/false,
13281 &type_specifiers);
13282
13283 /* At this point this is surely not elaborated type specifier. */
13284 if (!cp_parser_parse_definitely (parser))
13285 return NULL_TREE;
13286
13287 if (cxx_dialect == cxx98)
13288 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13289
13290 has_underlying_type = true;
13291
13292 /* If that didn't work, stop. */
13293 if (type_specifiers.type != error_mark_node)
13294 {
13295 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13296 /*initialized=*/0, NULL);
13297 if (underlying_type == error_mark_node)
13298 underlying_type = NULL_TREE;
13299 }
13300 }
13301
13302 /* Look for the `{' but don't consume it yet. */
13303 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13304 {
13305 cp_parser_error (parser, "expected %<{%>");
13306 if (has_underlying_type)
13307 return NULL_TREE;
13308 }
13309
13310 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13311 return NULL_TREE;
13312
13313 /* Issue an error message if type-definitions are forbidden here. */
13314 if (!cp_parser_check_type_definition (parser))
13315 type = error_mark_node;
13316 else
13317 /* Create the new type. We do this before consuming the opening
13318 brace so the enum will be recorded as being on the line of its
13319 tag (or the 'enum' keyword, if there is no tag). */
13320 type = start_enum (identifier, underlying_type, scoped_enum_p);
13321
13322 /* Consume the opening brace. */
13323 cp_lexer_consume_token (parser->lexer);
13324
13325 if (type == error_mark_node)
13326 {
13327 cp_parser_skip_to_end_of_block_or_statement (parser);
13328 return error_mark_node;
13329 }
13330
13331 /* If the next token is not '}', then there are some enumerators. */
13332 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13333 cp_parser_enumerator_list (parser, type);
13334
13335 /* Consume the final '}'. */
13336 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13337
13338 /* Look for trailing attributes to apply to this enumeration, and
13339 apply them if appropriate. */
13340 if (cp_parser_allow_gnu_extensions_p (parser))
13341 {
13342 tree trailing_attr = cp_parser_attributes_opt (parser);
13343 trailing_attr = chainon (trailing_attr, attributes);
13344 cplus_decl_attributes (&type,
13345 trailing_attr,
13346 (int) ATTR_FLAG_TYPE_IN_PLACE);
13347 }
13348
13349 /* Finish up the enumeration. */
13350 finish_enum (type);
13351
13352 return type;
13353 }
13354
13355 /* Parse an enumerator-list. The enumerators all have the indicated
13356 TYPE.
13357
13358 enumerator-list:
13359 enumerator-definition
13360 enumerator-list , enumerator-definition */
13361
13362 static void
13363 cp_parser_enumerator_list (cp_parser* parser, tree type)
13364 {
13365 while (true)
13366 {
13367 /* Parse an enumerator-definition. */
13368 cp_parser_enumerator_definition (parser, type);
13369
13370 /* If the next token is not a ',', we've reached the end of
13371 the list. */
13372 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13373 break;
13374 /* Otherwise, consume the `,' and keep going. */
13375 cp_lexer_consume_token (parser->lexer);
13376 /* If the next token is a `}', there is a trailing comma. */
13377 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13378 {
13379 if (!in_system_header)
13380 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13381 break;
13382 }
13383 }
13384 }
13385
13386 /* Parse an enumerator-definition. The enumerator has the indicated
13387 TYPE.
13388
13389 enumerator-definition:
13390 enumerator
13391 enumerator = constant-expression
13392
13393 enumerator:
13394 identifier */
13395
13396 static void
13397 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13398 {
13399 tree identifier;
13400 tree value;
13401 location_t loc;
13402
13403 /* Save the input location because we are interested in the location
13404 of the identifier and not the location of the explicit value. */
13405 loc = cp_lexer_peek_token (parser->lexer)->location;
13406
13407 /* Look for the identifier. */
13408 identifier = cp_parser_identifier (parser);
13409 if (identifier == error_mark_node)
13410 return;
13411
13412 /* If the next token is an '=', then there is an explicit value. */
13413 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13414 {
13415 /* Consume the `=' token. */
13416 cp_lexer_consume_token (parser->lexer);
13417 /* Parse the value. */
13418 value = cp_parser_constant_expression (parser,
13419 /*allow_non_constant_p=*/false,
13420 NULL);
13421 }
13422 else
13423 value = NULL_TREE;
13424
13425 /* If we are processing a template, make sure the initializer of the
13426 enumerator doesn't contain any bare template parameter pack. */
13427 if (check_for_bare_parameter_packs (value))
13428 value = error_mark_node;
13429
13430 /* Create the enumerator. */
13431 build_enumerator (identifier, value, type, loc);
13432 }
13433
13434 /* Parse a namespace-name.
13435
13436 namespace-name:
13437 original-namespace-name
13438 namespace-alias
13439
13440 Returns the NAMESPACE_DECL for the namespace. */
13441
13442 static tree
13443 cp_parser_namespace_name (cp_parser* parser)
13444 {
13445 tree identifier;
13446 tree namespace_decl;
13447
13448 cp_token *token = cp_lexer_peek_token (parser->lexer);
13449
13450 /* Get the name of the namespace. */
13451 identifier = cp_parser_identifier (parser);
13452 if (identifier == error_mark_node)
13453 return error_mark_node;
13454
13455 /* Look up the identifier in the currently active scope. Look only
13456 for namespaces, due to:
13457
13458 [basic.lookup.udir]
13459
13460 When looking up a namespace-name in a using-directive or alias
13461 definition, only namespace names are considered.
13462
13463 And:
13464
13465 [basic.lookup.qual]
13466
13467 During the lookup of a name preceding the :: scope resolution
13468 operator, object, function, and enumerator names are ignored.
13469
13470 (Note that cp_parser_qualifying_entity only calls this
13471 function if the token after the name is the scope resolution
13472 operator.) */
13473 namespace_decl = cp_parser_lookup_name (parser, identifier,
13474 none_type,
13475 /*is_template=*/false,
13476 /*is_namespace=*/true,
13477 /*check_dependency=*/true,
13478 /*ambiguous_decls=*/NULL,
13479 token->location);
13480 /* If it's not a namespace, issue an error. */
13481 if (namespace_decl == error_mark_node
13482 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13483 {
13484 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13485 error_at (token->location, "%qD is not a namespace-name", identifier);
13486 cp_parser_error (parser, "expected namespace-name");
13487 namespace_decl = error_mark_node;
13488 }
13489
13490 return namespace_decl;
13491 }
13492
13493 /* Parse a namespace-definition.
13494
13495 namespace-definition:
13496 named-namespace-definition
13497 unnamed-namespace-definition
13498
13499 named-namespace-definition:
13500 original-namespace-definition
13501 extension-namespace-definition
13502
13503 original-namespace-definition:
13504 namespace identifier { namespace-body }
13505
13506 extension-namespace-definition:
13507 namespace original-namespace-name { namespace-body }
13508
13509 unnamed-namespace-definition:
13510 namespace { namespace-body } */
13511
13512 static void
13513 cp_parser_namespace_definition (cp_parser* parser)
13514 {
13515 tree identifier, attribs;
13516 bool has_visibility;
13517 bool is_inline;
13518
13519 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13520 {
13521 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13522 is_inline = true;
13523 cp_lexer_consume_token (parser->lexer);
13524 }
13525 else
13526 is_inline = false;
13527
13528 /* Look for the `namespace' keyword. */
13529 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13530
13531 /* Get the name of the namespace. We do not attempt to distinguish
13532 between an original-namespace-definition and an
13533 extension-namespace-definition at this point. The semantic
13534 analysis routines are responsible for that. */
13535 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13536 identifier = cp_parser_identifier (parser);
13537 else
13538 identifier = NULL_TREE;
13539
13540 /* Parse any specified attributes. */
13541 attribs = cp_parser_attributes_opt (parser);
13542
13543 /* Look for the `{' to start the namespace. */
13544 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13545 /* Start the namespace. */
13546 push_namespace (identifier);
13547
13548 /* "inline namespace" is equivalent to a stub namespace definition
13549 followed by a strong using directive. */
13550 if (is_inline)
13551 {
13552 tree name_space = current_namespace;
13553 /* Set up namespace association. */
13554 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13555 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13556 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13557 /* Import the contents of the inline namespace. */
13558 pop_namespace ();
13559 do_using_directive (name_space);
13560 push_namespace (identifier);
13561 }
13562
13563 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13564
13565 /* Parse the body of the namespace. */
13566 cp_parser_namespace_body (parser);
13567
13568 #ifdef HANDLE_PRAGMA_VISIBILITY
13569 if (has_visibility)
13570 pop_visibility (1);
13571 #endif
13572
13573 /* Finish the namespace. */
13574 pop_namespace ();
13575 /* Look for the final `}'. */
13576 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13577 }
13578
13579 /* Parse a namespace-body.
13580
13581 namespace-body:
13582 declaration-seq [opt] */
13583
13584 static void
13585 cp_parser_namespace_body (cp_parser* parser)
13586 {
13587 cp_parser_declaration_seq_opt (parser);
13588 }
13589
13590 /* Parse a namespace-alias-definition.
13591
13592 namespace-alias-definition:
13593 namespace identifier = qualified-namespace-specifier ; */
13594
13595 static void
13596 cp_parser_namespace_alias_definition (cp_parser* parser)
13597 {
13598 tree identifier;
13599 tree namespace_specifier;
13600
13601 cp_token *token = cp_lexer_peek_token (parser->lexer);
13602
13603 /* Look for the `namespace' keyword. */
13604 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13605 /* Look for the identifier. */
13606 identifier = cp_parser_identifier (parser);
13607 if (identifier == error_mark_node)
13608 return;
13609 /* Look for the `=' token. */
13610 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13611 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13612 {
13613 error_at (token->location, "%<namespace%> definition is not allowed here");
13614 /* Skip the definition. */
13615 cp_lexer_consume_token (parser->lexer);
13616 if (cp_parser_skip_to_closing_brace (parser))
13617 cp_lexer_consume_token (parser->lexer);
13618 return;
13619 }
13620 cp_parser_require (parser, CPP_EQ, RT_EQ);
13621 /* Look for the qualified-namespace-specifier. */
13622 namespace_specifier
13623 = cp_parser_qualified_namespace_specifier (parser);
13624 /* Look for the `;' token. */
13625 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13626
13627 /* Register the alias in the symbol table. */
13628 do_namespace_alias (identifier, namespace_specifier);
13629 }
13630
13631 /* Parse a qualified-namespace-specifier.
13632
13633 qualified-namespace-specifier:
13634 :: [opt] nested-name-specifier [opt] namespace-name
13635
13636 Returns a NAMESPACE_DECL corresponding to the specified
13637 namespace. */
13638
13639 static tree
13640 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13641 {
13642 /* Look for the optional `::'. */
13643 cp_parser_global_scope_opt (parser,
13644 /*current_scope_valid_p=*/false);
13645
13646 /* Look for the optional nested-name-specifier. */
13647 cp_parser_nested_name_specifier_opt (parser,
13648 /*typename_keyword_p=*/false,
13649 /*check_dependency_p=*/true,
13650 /*type_p=*/false,
13651 /*is_declaration=*/true);
13652
13653 return cp_parser_namespace_name (parser);
13654 }
13655
13656 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13657 access declaration.
13658
13659 using-declaration:
13660 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13661 using :: unqualified-id ;
13662
13663 access-declaration:
13664 qualified-id ;
13665
13666 */
13667
13668 static bool
13669 cp_parser_using_declaration (cp_parser* parser,
13670 bool access_declaration_p)
13671 {
13672 cp_token *token;
13673 bool typename_p = false;
13674 bool global_scope_p;
13675 tree decl;
13676 tree identifier;
13677 tree qscope;
13678
13679 if (access_declaration_p)
13680 cp_parser_parse_tentatively (parser);
13681 else
13682 {
13683 /* Look for the `using' keyword. */
13684 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13685
13686 /* Peek at the next token. */
13687 token = cp_lexer_peek_token (parser->lexer);
13688 /* See if it's `typename'. */
13689 if (token->keyword == RID_TYPENAME)
13690 {
13691 /* Remember that we've seen it. */
13692 typename_p = true;
13693 /* Consume the `typename' token. */
13694 cp_lexer_consume_token (parser->lexer);
13695 }
13696 }
13697
13698 /* Look for the optional global scope qualification. */
13699 global_scope_p
13700 = (cp_parser_global_scope_opt (parser,
13701 /*current_scope_valid_p=*/false)
13702 != NULL_TREE);
13703
13704 /* If we saw `typename', or didn't see `::', then there must be a
13705 nested-name-specifier present. */
13706 if (typename_p || !global_scope_p)
13707 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13708 /*check_dependency_p=*/true,
13709 /*type_p=*/false,
13710 /*is_declaration=*/true);
13711 /* Otherwise, we could be in either of the two productions. In that
13712 case, treat the nested-name-specifier as optional. */
13713 else
13714 qscope = cp_parser_nested_name_specifier_opt (parser,
13715 /*typename_keyword_p=*/false,
13716 /*check_dependency_p=*/true,
13717 /*type_p=*/false,
13718 /*is_declaration=*/true);
13719 if (!qscope)
13720 qscope = global_namespace;
13721
13722 if (access_declaration_p && cp_parser_error_occurred (parser))
13723 /* Something has already gone wrong; there's no need to parse
13724 further. Since an error has occurred, the return value of
13725 cp_parser_parse_definitely will be false, as required. */
13726 return cp_parser_parse_definitely (parser);
13727
13728 token = cp_lexer_peek_token (parser->lexer);
13729 /* Parse the unqualified-id. */
13730 identifier = cp_parser_unqualified_id (parser,
13731 /*template_keyword_p=*/false,
13732 /*check_dependency_p=*/true,
13733 /*declarator_p=*/true,
13734 /*optional_p=*/false);
13735
13736 if (access_declaration_p)
13737 {
13738 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13739 cp_parser_simulate_error (parser);
13740 if (!cp_parser_parse_definitely (parser))
13741 return false;
13742 }
13743
13744 /* The function we call to handle a using-declaration is different
13745 depending on what scope we are in. */
13746 if (qscope == error_mark_node || identifier == error_mark_node)
13747 ;
13748 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13749 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13750 /* [namespace.udecl]
13751
13752 A using declaration shall not name a template-id. */
13753 error_at (token->location,
13754 "a template-id may not appear in a using-declaration");
13755 else
13756 {
13757 if (at_class_scope_p ())
13758 {
13759 /* Create the USING_DECL. */
13760 decl = do_class_using_decl (parser->scope, identifier);
13761
13762 if (check_for_bare_parameter_packs (decl))
13763 return false;
13764 else
13765 /* Add it to the list of members in this class. */
13766 finish_member_declaration (decl);
13767 }
13768 else
13769 {
13770 decl = cp_parser_lookup_name_simple (parser,
13771 identifier,
13772 token->location);
13773 if (decl == error_mark_node)
13774 cp_parser_name_lookup_error (parser, identifier,
13775 decl, NLE_NULL,
13776 token->location);
13777 else if (check_for_bare_parameter_packs (decl))
13778 return false;
13779 else if (!at_namespace_scope_p ())
13780 do_local_using_decl (decl, qscope, identifier);
13781 else
13782 do_toplevel_using_decl (decl, qscope, identifier);
13783 }
13784 }
13785
13786 /* Look for the final `;'. */
13787 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13788
13789 return true;
13790 }
13791
13792 /* Parse a using-directive.
13793
13794 using-directive:
13795 using namespace :: [opt] nested-name-specifier [opt]
13796 namespace-name ; */
13797
13798 static void
13799 cp_parser_using_directive (cp_parser* parser)
13800 {
13801 tree namespace_decl;
13802 tree attribs;
13803
13804 /* Look for the `using' keyword. */
13805 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13806 /* And the `namespace' keyword. */
13807 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13808 /* Look for the optional `::' operator. */
13809 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13810 /* And the optional nested-name-specifier. */
13811 cp_parser_nested_name_specifier_opt (parser,
13812 /*typename_keyword_p=*/false,
13813 /*check_dependency_p=*/true,
13814 /*type_p=*/false,
13815 /*is_declaration=*/true);
13816 /* Get the namespace being used. */
13817 namespace_decl = cp_parser_namespace_name (parser);
13818 /* And any specified attributes. */
13819 attribs = cp_parser_attributes_opt (parser);
13820 /* Update the symbol table. */
13821 parse_using_directive (namespace_decl, attribs);
13822 /* Look for the final `;'. */
13823 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13824 }
13825
13826 /* Parse an asm-definition.
13827
13828 asm-definition:
13829 asm ( string-literal ) ;
13830
13831 GNU Extension:
13832
13833 asm-definition:
13834 asm volatile [opt] ( string-literal ) ;
13835 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13836 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13837 : asm-operand-list [opt] ) ;
13838 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13839 : asm-operand-list [opt]
13840 : asm-clobber-list [opt] ) ;
13841 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13842 : asm-clobber-list [opt]
13843 : asm-goto-list ) ; */
13844
13845 static void
13846 cp_parser_asm_definition (cp_parser* parser)
13847 {
13848 tree string;
13849 tree outputs = NULL_TREE;
13850 tree inputs = NULL_TREE;
13851 tree clobbers = NULL_TREE;
13852 tree labels = NULL_TREE;
13853 tree asm_stmt;
13854 bool volatile_p = false;
13855 bool extended_p = false;
13856 bool invalid_inputs_p = false;
13857 bool invalid_outputs_p = false;
13858 bool goto_p = false;
13859 required_token missing = RT_NONE;
13860
13861 /* Look for the `asm' keyword. */
13862 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
13863 /* See if the next token is `volatile'. */
13864 if (cp_parser_allow_gnu_extensions_p (parser)
13865 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13866 {
13867 /* Remember that we saw the `volatile' keyword. */
13868 volatile_p = true;
13869 /* Consume the token. */
13870 cp_lexer_consume_token (parser->lexer);
13871 }
13872 if (cp_parser_allow_gnu_extensions_p (parser)
13873 && parser->in_function_body
13874 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13875 {
13876 /* Remember that we saw the `goto' keyword. */
13877 goto_p = true;
13878 /* Consume the token. */
13879 cp_lexer_consume_token (parser->lexer);
13880 }
13881 /* Look for the opening `('. */
13882 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13883 return;
13884 /* Look for the string. */
13885 string = cp_parser_string_literal (parser, false, false);
13886 if (string == error_mark_node)
13887 {
13888 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13889 /*consume_paren=*/true);
13890 return;
13891 }
13892
13893 /* If we're allowing GNU extensions, check for the extended assembly
13894 syntax. Unfortunately, the `:' tokens need not be separated by
13895 a space in C, and so, for compatibility, we tolerate that here
13896 too. Doing that means that we have to treat the `::' operator as
13897 two `:' tokens. */
13898 if (cp_parser_allow_gnu_extensions_p (parser)
13899 && parser->in_function_body
13900 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13901 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13902 {
13903 bool inputs_p = false;
13904 bool clobbers_p = false;
13905 bool labels_p = false;
13906
13907 /* The extended syntax was used. */
13908 extended_p = true;
13909
13910 /* Look for outputs. */
13911 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13912 {
13913 /* Consume the `:'. */
13914 cp_lexer_consume_token (parser->lexer);
13915 /* Parse the output-operands. */
13916 if (cp_lexer_next_token_is_not (parser->lexer,
13917 CPP_COLON)
13918 && cp_lexer_next_token_is_not (parser->lexer,
13919 CPP_SCOPE)
13920 && cp_lexer_next_token_is_not (parser->lexer,
13921 CPP_CLOSE_PAREN)
13922 && !goto_p)
13923 outputs = cp_parser_asm_operand_list (parser);
13924
13925 if (outputs == error_mark_node)
13926 invalid_outputs_p = true;
13927 }
13928 /* If the next token is `::', there are no outputs, and the
13929 next token is the beginning of the inputs. */
13930 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13931 /* The inputs are coming next. */
13932 inputs_p = true;
13933
13934 /* Look for inputs. */
13935 if (inputs_p
13936 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13937 {
13938 /* Consume the `:' or `::'. */
13939 cp_lexer_consume_token (parser->lexer);
13940 /* Parse the output-operands. */
13941 if (cp_lexer_next_token_is_not (parser->lexer,
13942 CPP_COLON)
13943 && cp_lexer_next_token_is_not (parser->lexer,
13944 CPP_SCOPE)
13945 && cp_lexer_next_token_is_not (parser->lexer,
13946 CPP_CLOSE_PAREN))
13947 inputs = cp_parser_asm_operand_list (parser);
13948
13949 if (inputs == error_mark_node)
13950 invalid_inputs_p = true;
13951 }
13952 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13953 /* The clobbers are coming next. */
13954 clobbers_p = true;
13955
13956 /* Look for clobbers. */
13957 if (clobbers_p
13958 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13959 {
13960 clobbers_p = true;
13961 /* Consume the `:' or `::'. */
13962 cp_lexer_consume_token (parser->lexer);
13963 /* Parse the clobbers. */
13964 if (cp_lexer_next_token_is_not (parser->lexer,
13965 CPP_COLON)
13966 && cp_lexer_next_token_is_not (parser->lexer,
13967 CPP_CLOSE_PAREN))
13968 clobbers = cp_parser_asm_clobber_list (parser);
13969 }
13970 else if (goto_p
13971 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13972 /* The labels are coming next. */
13973 labels_p = true;
13974
13975 /* Look for labels. */
13976 if (labels_p
13977 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13978 {
13979 labels_p = true;
13980 /* Consume the `:' or `::'. */
13981 cp_lexer_consume_token (parser->lexer);
13982 /* Parse the labels. */
13983 labels = cp_parser_asm_label_list (parser);
13984 }
13985
13986 if (goto_p && !labels_p)
13987 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
13988 }
13989 else if (goto_p)
13990 missing = RT_COLON_SCOPE;
13991
13992 /* Look for the closing `)'. */
13993 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13994 missing ? missing : RT_CLOSE_PAREN))
13995 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13996 /*consume_paren=*/true);
13997 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13998
13999 if (!invalid_inputs_p && !invalid_outputs_p)
14000 {
14001 /* Create the ASM_EXPR. */
14002 if (parser->in_function_body)
14003 {
14004 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14005 inputs, clobbers, labels);
14006 /* If the extended syntax was not used, mark the ASM_EXPR. */
14007 if (!extended_p)
14008 {
14009 tree temp = asm_stmt;
14010 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14011 temp = TREE_OPERAND (temp, 0);
14012
14013 ASM_INPUT_P (temp) = 1;
14014 }
14015 }
14016 else
14017 cgraph_add_asm_node (string);
14018 }
14019 }
14020
14021 /* Declarators [gram.dcl.decl] */
14022
14023 /* Parse an init-declarator.
14024
14025 init-declarator:
14026 declarator initializer [opt]
14027
14028 GNU Extension:
14029
14030 init-declarator:
14031 declarator asm-specification [opt] attributes [opt] initializer [opt]
14032
14033 function-definition:
14034 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14035 function-body
14036 decl-specifier-seq [opt] declarator function-try-block
14037
14038 GNU Extension:
14039
14040 function-definition:
14041 __extension__ function-definition
14042
14043 The DECL_SPECIFIERS apply to this declarator. Returns a
14044 representation of the entity declared. If MEMBER_P is TRUE, then
14045 this declarator appears in a class scope. The new DECL created by
14046 this declarator is returned.
14047
14048 The CHECKS are access checks that should be performed once we know
14049 what entity is being declared (and, therefore, what classes have
14050 befriended it).
14051
14052 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14053 for a function-definition here as well. If the declarator is a
14054 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14055 be TRUE upon return. By that point, the function-definition will
14056 have been completely parsed.
14057
14058 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14059 is FALSE. */
14060
14061 static tree
14062 cp_parser_init_declarator (cp_parser* parser,
14063 cp_decl_specifier_seq *decl_specifiers,
14064 VEC (deferred_access_check,gc)* checks,
14065 bool function_definition_allowed_p,
14066 bool member_p,
14067 int declares_class_or_enum,
14068 bool* function_definition_p)
14069 {
14070 cp_token *token = NULL, *asm_spec_start_token = NULL,
14071 *attributes_start_token = NULL;
14072 cp_declarator *declarator;
14073 tree prefix_attributes;
14074 tree attributes;
14075 tree asm_specification;
14076 tree initializer;
14077 tree decl = NULL_TREE;
14078 tree scope;
14079 int is_initialized;
14080 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14081 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14082 "(...)". */
14083 enum cpp_ttype initialization_kind;
14084 bool is_direct_init = false;
14085 bool is_non_constant_init;
14086 int ctor_dtor_or_conv_p;
14087 bool friend_p;
14088 tree pushed_scope = NULL;
14089
14090 /* Gather the attributes that were provided with the
14091 decl-specifiers. */
14092 prefix_attributes = decl_specifiers->attributes;
14093
14094 /* Assume that this is not the declarator for a function
14095 definition. */
14096 if (function_definition_p)
14097 *function_definition_p = false;
14098
14099 /* Defer access checks while parsing the declarator; we cannot know
14100 what names are accessible until we know what is being
14101 declared. */
14102 resume_deferring_access_checks ();
14103
14104 /* Parse the declarator. */
14105 token = cp_lexer_peek_token (parser->lexer);
14106 declarator
14107 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14108 &ctor_dtor_or_conv_p,
14109 /*parenthesized_p=*/NULL,
14110 /*member_p=*/false);
14111 /* Gather up the deferred checks. */
14112 stop_deferring_access_checks ();
14113
14114 /* If the DECLARATOR was erroneous, there's no need to go
14115 further. */
14116 if (declarator == cp_error_declarator)
14117 return error_mark_node;
14118
14119 /* Check that the number of template-parameter-lists is OK. */
14120 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14121 token->location))
14122 return error_mark_node;
14123
14124 if (declares_class_or_enum & 2)
14125 cp_parser_check_for_definition_in_return_type (declarator,
14126 decl_specifiers->type,
14127 decl_specifiers->type_location);
14128
14129 /* Figure out what scope the entity declared by the DECLARATOR is
14130 located in. `grokdeclarator' sometimes changes the scope, so
14131 we compute it now. */
14132 scope = get_scope_of_declarator (declarator);
14133
14134 /* Perform any lookups in the declared type which were thought to be
14135 dependent, but are not in the scope of the declarator. */
14136 decl_specifiers->type
14137 = maybe_update_decl_type (decl_specifiers->type, scope);
14138
14139 /* If we're allowing GNU extensions, look for an asm-specification
14140 and attributes. */
14141 if (cp_parser_allow_gnu_extensions_p (parser))
14142 {
14143 /* Look for an asm-specification. */
14144 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14145 asm_specification = cp_parser_asm_specification_opt (parser);
14146 /* And attributes. */
14147 attributes_start_token = cp_lexer_peek_token (parser->lexer);
14148 attributes = cp_parser_attributes_opt (parser);
14149 }
14150 else
14151 {
14152 asm_specification = NULL_TREE;
14153 attributes = NULL_TREE;
14154 }
14155
14156 /* Peek at the next token. */
14157 token = cp_lexer_peek_token (parser->lexer);
14158 /* Check to see if the token indicates the start of a
14159 function-definition. */
14160 if (function_declarator_p (declarator)
14161 && cp_parser_token_starts_function_definition_p (token))
14162 {
14163 if (!function_definition_allowed_p)
14164 {
14165 /* If a function-definition should not appear here, issue an
14166 error message. */
14167 cp_parser_error (parser,
14168 "a function-definition is not allowed here");
14169 return error_mark_node;
14170 }
14171 else
14172 {
14173 location_t func_brace_location
14174 = cp_lexer_peek_token (parser->lexer)->location;
14175
14176 /* Neither attributes nor an asm-specification are allowed
14177 on a function-definition. */
14178 if (asm_specification)
14179 error_at (asm_spec_start_token->location,
14180 "an asm-specification is not allowed "
14181 "on a function-definition");
14182 if (attributes)
14183 error_at (attributes_start_token->location,
14184 "attributes are not allowed on a function-definition");
14185 /* This is a function-definition. */
14186 *function_definition_p = true;
14187
14188 /* Parse the function definition. */
14189 if (member_p)
14190 decl = cp_parser_save_member_function_body (parser,
14191 decl_specifiers,
14192 declarator,
14193 prefix_attributes);
14194 else
14195 decl
14196 = (cp_parser_function_definition_from_specifiers_and_declarator
14197 (parser, decl_specifiers, prefix_attributes, declarator));
14198
14199 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14200 {
14201 /* This is where the prologue starts... */
14202 DECL_STRUCT_FUNCTION (decl)->function_start_locus
14203 = func_brace_location;
14204 }
14205
14206 return decl;
14207 }
14208 }
14209
14210 /* [dcl.dcl]
14211
14212 Only in function declarations for constructors, destructors, and
14213 type conversions can the decl-specifier-seq be omitted.
14214
14215 We explicitly postpone this check past the point where we handle
14216 function-definitions because we tolerate function-definitions
14217 that are missing their return types in some modes. */
14218 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14219 {
14220 cp_parser_error (parser,
14221 "expected constructor, destructor, or type conversion");
14222 return error_mark_node;
14223 }
14224
14225 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14226 if (token->type == CPP_EQ
14227 || token->type == CPP_OPEN_PAREN
14228 || token->type == CPP_OPEN_BRACE)
14229 {
14230 is_initialized = SD_INITIALIZED;
14231 initialization_kind = token->type;
14232
14233 if (token->type == CPP_EQ
14234 && function_declarator_p (declarator))
14235 {
14236 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14237 if (t2->keyword == RID_DEFAULT)
14238 is_initialized = SD_DEFAULTED;
14239 else if (t2->keyword == RID_DELETE)
14240 is_initialized = SD_DELETED;
14241 }
14242 }
14243 else
14244 {
14245 /* If the init-declarator isn't initialized and isn't followed by a
14246 `,' or `;', it's not a valid init-declarator. */
14247 if (token->type != CPP_COMMA
14248 && token->type != CPP_SEMICOLON)
14249 {
14250 cp_parser_error (parser, "expected initializer");
14251 return error_mark_node;
14252 }
14253 is_initialized = SD_UNINITIALIZED;
14254 initialization_kind = CPP_EOF;
14255 }
14256
14257 /* Because start_decl has side-effects, we should only call it if we
14258 know we're going ahead. By this point, we know that we cannot
14259 possibly be looking at any other construct. */
14260 cp_parser_commit_to_tentative_parse (parser);
14261
14262 /* If the decl specifiers were bad, issue an error now that we're
14263 sure this was intended to be a declarator. Then continue
14264 declaring the variable(s), as int, to try to cut down on further
14265 errors. */
14266 if (decl_specifiers->any_specifiers_p
14267 && decl_specifiers->type == error_mark_node)
14268 {
14269 cp_parser_error (parser, "invalid type in declaration");
14270 decl_specifiers->type = integer_type_node;
14271 }
14272
14273 /* Check to see whether or not this declaration is a friend. */
14274 friend_p = cp_parser_friend_p (decl_specifiers);
14275
14276 /* Enter the newly declared entry in the symbol table. If we're
14277 processing a declaration in a class-specifier, we wait until
14278 after processing the initializer. */
14279 if (!member_p)
14280 {
14281 if (parser->in_unbraced_linkage_specification_p)
14282 decl_specifiers->storage_class = sc_extern;
14283 decl = start_decl (declarator, decl_specifiers,
14284 is_initialized, attributes, prefix_attributes,
14285 &pushed_scope);
14286 /* Adjust location of decl if declarator->id_loc is more appropriate:
14287 set, and decl wasn't merged with another decl, in which case its
14288 location would be different from input_location, and more accurate. */
14289 if (DECL_P (decl)
14290 && declarator->id_loc != UNKNOWN_LOCATION
14291 && DECL_SOURCE_LOCATION (decl) == input_location)
14292 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14293 }
14294 else if (scope)
14295 /* Enter the SCOPE. That way unqualified names appearing in the
14296 initializer will be looked up in SCOPE. */
14297 pushed_scope = push_scope (scope);
14298
14299 /* Perform deferred access control checks, now that we know in which
14300 SCOPE the declared entity resides. */
14301 if (!member_p && decl)
14302 {
14303 tree saved_current_function_decl = NULL_TREE;
14304
14305 /* If the entity being declared is a function, pretend that we
14306 are in its scope. If it is a `friend', it may have access to
14307 things that would not otherwise be accessible. */
14308 if (TREE_CODE (decl) == FUNCTION_DECL)
14309 {
14310 saved_current_function_decl = current_function_decl;
14311 current_function_decl = decl;
14312 }
14313
14314 /* Perform access checks for template parameters. */
14315 cp_parser_perform_template_parameter_access_checks (checks);
14316
14317 /* Perform the access control checks for the declarator and the
14318 decl-specifiers. */
14319 perform_deferred_access_checks ();
14320
14321 /* Restore the saved value. */
14322 if (TREE_CODE (decl) == FUNCTION_DECL)
14323 current_function_decl = saved_current_function_decl;
14324 }
14325
14326 /* Parse the initializer. */
14327 initializer = NULL_TREE;
14328 is_direct_init = false;
14329 is_non_constant_init = true;
14330 if (is_initialized)
14331 {
14332 if (function_declarator_p (declarator))
14333 {
14334 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14335 if (initialization_kind == CPP_EQ)
14336 initializer = cp_parser_pure_specifier (parser);
14337 else
14338 {
14339 /* If the declaration was erroneous, we don't really
14340 know what the user intended, so just silently
14341 consume the initializer. */
14342 if (decl != error_mark_node)
14343 error_at (initializer_start_token->location,
14344 "initializer provided for function");
14345 cp_parser_skip_to_closing_parenthesis (parser,
14346 /*recovering=*/true,
14347 /*or_comma=*/false,
14348 /*consume_paren=*/true);
14349 }
14350 }
14351 else
14352 {
14353 /* We want to record the extra mangling scope for in-class
14354 initializers of class members and initializers of static data
14355 member templates. The former is a C++0x feature which isn't
14356 implemented yet, and I expect it will involve deferring
14357 parsing of the initializer until end of class as with default
14358 arguments. So right here we only handle the latter. */
14359 if (!member_p && processing_template_decl)
14360 start_lambda_scope (decl);
14361 initializer = cp_parser_initializer (parser,
14362 &is_direct_init,
14363 &is_non_constant_init);
14364 if (!member_p && processing_template_decl)
14365 finish_lambda_scope ();
14366 }
14367 }
14368
14369 /* The old parser allows attributes to appear after a parenthesized
14370 initializer. Mark Mitchell proposed removing this functionality
14371 on the GCC mailing lists on 2002-08-13. This parser accepts the
14372 attributes -- but ignores them. */
14373 if (cp_parser_allow_gnu_extensions_p (parser)
14374 && initialization_kind == CPP_OPEN_PAREN)
14375 if (cp_parser_attributes_opt (parser))
14376 warning (OPT_Wattributes,
14377 "attributes after parenthesized initializer ignored");
14378
14379 /* For an in-class declaration, use `grokfield' to create the
14380 declaration. */
14381 if (member_p)
14382 {
14383 if (pushed_scope)
14384 {
14385 pop_scope (pushed_scope);
14386 pushed_scope = false;
14387 }
14388 decl = grokfield (declarator, decl_specifiers,
14389 initializer, !is_non_constant_init,
14390 /*asmspec=*/NULL_TREE,
14391 prefix_attributes);
14392 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14393 cp_parser_save_default_args (parser, decl);
14394 }
14395
14396 /* Finish processing the declaration. But, skip friend
14397 declarations. */
14398 if (!friend_p && decl && decl != error_mark_node)
14399 {
14400 cp_finish_decl (decl,
14401 initializer, !is_non_constant_init,
14402 asm_specification,
14403 /* If the initializer is in parentheses, then this is
14404 a direct-initialization, which means that an
14405 `explicit' constructor is OK. Otherwise, an
14406 `explicit' constructor cannot be used. */
14407 ((is_direct_init || !is_initialized)
14408 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14409 }
14410 else if ((cxx_dialect != cxx98) && friend_p
14411 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14412 /* Core issue #226 (C++0x only): A default template-argument
14413 shall not be specified in a friend class template
14414 declaration. */
14415 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14416 /*is_partial=*/0, /*is_friend_decl=*/1);
14417
14418 if (!friend_p && pushed_scope)
14419 pop_scope (pushed_scope);
14420
14421 return decl;
14422 }
14423
14424 /* Parse a declarator.
14425
14426 declarator:
14427 direct-declarator
14428 ptr-operator declarator
14429
14430 abstract-declarator:
14431 ptr-operator abstract-declarator [opt]
14432 direct-abstract-declarator
14433
14434 GNU Extensions:
14435
14436 declarator:
14437 attributes [opt] direct-declarator
14438 attributes [opt] ptr-operator declarator
14439
14440 abstract-declarator:
14441 attributes [opt] ptr-operator abstract-declarator [opt]
14442 attributes [opt] direct-abstract-declarator
14443
14444 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14445 detect constructor, destructor or conversion operators. It is set
14446 to -1 if the declarator is a name, and +1 if it is a
14447 function. Otherwise it is set to zero. Usually you just want to
14448 test for >0, but internally the negative value is used.
14449
14450 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14451 a decl-specifier-seq unless it declares a constructor, destructor,
14452 or conversion. It might seem that we could check this condition in
14453 semantic analysis, rather than parsing, but that makes it difficult
14454 to handle something like `f()'. We want to notice that there are
14455 no decl-specifiers, and therefore realize that this is an
14456 expression, not a declaration.)
14457
14458 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14459 the declarator is a direct-declarator of the form "(...)".
14460
14461 MEMBER_P is true iff this declarator is a member-declarator. */
14462
14463 static cp_declarator *
14464 cp_parser_declarator (cp_parser* parser,
14465 cp_parser_declarator_kind dcl_kind,
14466 int* ctor_dtor_or_conv_p,
14467 bool* parenthesized_p,
14468 bool member_p)
14469 {
14470 cp_declarator *declarator;
14471 enum tree_code code;
14472 cp_cv_quals cv_quals;
14473 tree class_type;
14474 tree attributes = NULL_TREE;
14475
14476 /* Assume this is not a constructor, destructor, or type-conversion
14477 operator. */
14478 if (ctor_dtor_or_conv_p)
14479 *ctor_dtor_or_conv_p = 0;
14480
14481 if (cp_parser_allow_gnu_extensions_p (parser))
14482 attributes = cp_parser_attributes_opt (parser);
14483
14484 /* Check for the ptr-operator production. */
14485 cp_parser_parse_tentatively (parser);
14486 /* Parse the ptr-operator. */
14487 code = cp_parser_ptr_operator (parser,
14488 &class_type,
14489 &cv_quals);
14490 /* If that worked, then we have a ptr-operator. */
14491 if (cp_parser_parse_definitely (parser))
14492 {
14493 /* If a ptr-operator was found, then this declarator was not
14494 parenthesized. */
14495 if (parenthesized_p)
14496 *parenthesized_p = true;
14497 /* The dependent declarator is optional if we are parsing an
14498 abstract-declarator. */
14499 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14500 cp_parser_parse_tentatively (parser);
14501
14502 /* Parse the dependent declarator. */
14503 declarator = cp_parser_declarator (parser, dcl_kind,
14504 /*ctor_dtor_or_conv_p=*/NULL,
14505 /*parenthesized_p=*/NULL,
14506 /*member_p=*/false);
14507
14508 /* If we are parsing an abstract-declarator, we must handle the
14509 case where the dependent declarator is absent. */
14510 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14511 && !cp_parser_parse_definitely (parser))
14512 declarator = NULL;
14513
14514 declarator = cp_parser_make_indirect_declarator
14515 (code, class_type, cv_quals, declarator);
14516 }
14517 /* Everything else is a direct-declarator. */
14518 else
14519 {
14520 if (parenthesized_p)
14521 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14522 CPP_OPEN_PAREN);
14523 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14524 ctor_dtor_or_conv_p,
14525 member_p);
14526 }
14527
14528 if (attributes && declarator && declarator != cp_error_declarator)
14529 declarator->attributes = attributes;
14530
14531 return declarator;
14532 }
14533
14534 /* Parse a direct-declarator or direct-abstract-declarator.
14535
14536 direct-declarator:
14537 declarator-id
14538 direct-declarator ( parameter-declaration-clause )
14539 cv-qualifier-seq [opt]
14540 exception-specification [opt]
14541 direct-declarator [ constant-expression [opt] ]
14542 ( declarator )
14543
14544 direct-abstract-declarator:
14545 direct-abstract-declarator [opt]
14546 ( parameter-declaration-clause )
14547 cv-qualifier-seq [opt]
14548 exception-specification [opt]
14549 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14550 ( abstract-declarator )
14551
14552 Returns a representation of the declarator. DCL_KIND is
14553 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14554 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14555 we are parsing a direct-declarator. It is
14556 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14557 of ambiguity we prefer an abstract declarator, as per
14558 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14559 cp_parser_declarator. */
14560
14561 static cp_declarator *
14562 cp_parser_direct_declarator (cp_parser* parser,
14563 cp_parser_declarator_kind dcl_kind,
14564 int* ctor_dtor_or_conv_p,
14565 bool member_p)
14566 {
14567 cp_token *token;
14568 cp_declarator *declarator = NULL;
14569 tree scope = NULL_TREE;
14570 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14571 bool saved_in_declarator_p = parser->in_declarator_p;
14572 bool first = true;
14573 tree pushed_scope = NULL_TREE;
14574
14575 while (true)
14576 {
14577 /* Peek at the next token. */
14578 token = cp_lexer_peek_token (parser->lexer);
14579 if (token->type == CPP_OPEN_PAREN)
14580 {
14581 /* This is either a parameter-declaration-clause, or a
14582 parenthesized declarator. When we know we are parsing a
14583 named declarator, it must be a parenthesized declarator
14584 if FIRST is true. For instance, `(int)' is a
14585 parameter-declaration-clause, with an omitted
14586 direct-abstract-declarator. But `((*))', is a
14587 parenthesized abstract declarator. Finally, when T is a
14588 template parameter `(T)' is a
14589 parameter-declaration-clause, and not a parenthesized
14590 named declarator.
14591
14592 We first try and parse a parameter-declaration-clause,
14593 and then try a nested declarator (if FIRST is true).
14594
14595 It is not an error for it not to be a
14596 parameter-declaration-clause, even when FIRST is
14597 false. Consider,
14598
14599 int i (int);
14600 int i (3);
14601
14602 The first is the declaration of a function while the
14603 second is the definition of a variable, including its
14604 initializer.
14605
14606 Having seen only the parenthesis, we cannot know which of
14607 these two alternatives should be selected. Even more
14608 complex are examples like:
14609
14610 int i (int (a));
14611 int i (int (3));
14612
14613 The former is a function-declaration; the latter is a
14614 variable initialization.
14615
14616 Thus again, we try a parameter-declaration-clause, and if
14617 that fails, we back out and return. */
14618
14619 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14620 {
14621 tree params;
14622 unsigned saved_num_template_parameter_lists;
14623 bool is_declarator = false;
14624 tree t;
14625
14626 /* In a member-declarator, the only valid interpretation
14627 of a parenthesis is the start of a
14628 parameter-declaration-clause. (It is invalid to
14629 initialize a static data member with a parenthesized
14630 initializer; only the "=" form of initialization is
14631 permitted.) */
14632 if (!member_p)
14633 cp_parser_parse_tentatively (parser);
14634
14635 /* Consume the `('. */
14636 cp_lexer_consume_token (parser->lexer);
14637 if (first)
14638 {
14639 /* If this is going to be an abstract declarator, we're
14640 in a declarator and we can't have default args. */
14641 parser->default_arg_ok_p = false;
14642 parser->in_declarator_p = true;
14643 }
14644
14645 /* Inside the function parameter list, surrounding
14646 template-parameter-lists do not apply. */
14647 saved_num_template_parameter_lists
14648 = parser->num_template_parameter_lists;
14649 parser->num_template_parameter_lists = 0;
14650
14651 begin_scope (sk_function_parms, NULL_TREE);
14652
14653 /* Parse the parameter-declaration-clause. */
14654 params = cp_parser_parameter_declaration_clause (parser);
14655
14656 parser->num_template_parameter_lists
14657 = saved_num_template_parameter_lists;
14658
14659 /* If all went well, parse the cv-qualifier-seq and the
14660 exception-specification. */
14661 if (member_p || cp_parser_parse_definitely (parser))
14662 {
14663 cp_cv_quals cv_quals;
14664 tree exception_specification;
14665 tree late_return;
14666
14667 is_declarator = true;
14668
14669 if (ctor_dtor_or_conv_p)
14670 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14671 first = false;
14672 /* Consume the `)'. */
14673 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14674
14675 /* Parse the cv-qualifier-seq. */
14676 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14677 /* And the exception-specification. */
14678 exception_specification
14679 = cp_parser_exception_specification_opt (parser);
14680
14681 late_return
14682 = cp_parser_late_return_type_opt (parser);
14683
14684 /* Create the function-declarator. */
14685 declarator = make_call_declarator (declarator,
14686 params,
14687 cv_quals,
14688 exception_specification,
14689 late_return);
14690 /* Any subsequent parameter lists are to do with
14691 return type, so are not those of the declared
14692 function. */
14693 parser->default_arg_ok_p = false;
14694 }
14695
14696 /* Remove the function parms from scope. */
14697 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14698 pop_binding (DECL_NAME (t), t);
14699 leave_scope();
14700
14701 if (is_declarator)
14702 /* Repeat the main loop. */
14703 continue;
14704 }
14705
14706 /* If this is the first, we can try a parenthesized
14707 declarator. */
14708 if (first)
14709 {
14710 bool saved_in_type_id_in_expr_p;
14711
14712 parser->default_arg_ok_p = saved_default_arg_ok_p;
14713 parser->in_declarator_p = saved_in_declarator_p;
14714
14715 /* Consume the `('. */
14716 cp_lexer_consume_token (parser->lexer);
14717 /* Parse the nested declarator. */
14718 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14719 parser->in_type_id_in_expr_p = true;
14720 declarator
14721 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14722 /*parenthesized_p=*/NULL,
14723 member_p);
14724 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14725 first = false;
14726 /* Expect a `)'. */
14727 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14728 declarator = cp_error_declarator;
14729 if (declarator == cp_error_declarator)
14730 break;
14731
14732 goto handle_declarator;
14733 }
14734 /* Otherwise, we must be done. */
14735 else
14736 break;
14737 }
14738 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14739 && token->type == CPP_OPEN_SQUARE)
14740 {
14741 /* Parse an array-declarator. */
14742 tree bounds;
14743
14744 if (ctor_dtor_or_conv_p)
14745 *ctor_dtor_or_conv_p = 0;
14746
14747 first = false;
14748 parser->default_arg_ok_p = false;
14749 parser->in_declarator_p = true;
14750 /* Consume the `['. */
14751 cp_lexer_consume_token (parser->lexer);
14752 /* Peek at the next token. */
14753 token = cp_lexer_peek_token (parser->lexer);
14754 /* If the next token is `]', then there is no
14755 constant-expression. */
14756 if (token->type != CPP_CLOSE_SQUARE)
14757 {
14758 bool non_constant_p;
14759
14760 bounds
14761 = cp_parser_constant_expression (parser,
14762 /*allow_non_constant=*/true,
14763 &non_constant_p);
14764 if (!non_constant_p)
14765 bounds = fold_non_dependent_expr (bounds);
14766 /* Normally, the array bound must be an integral constant
14767 expression. However, as an extension, we allow VLAs
14768 in function scopes as long as they aren't part of a
14769 parameter declaration. */
14770 else if (!parser->in_function_body
14771 || current_binding_level->kind == sk_function_parms)
14772 {
14773 cp_parser_error (parser,
14774 "array bound is not an integer constant");
14775 bounds = error_mark_node;
14776 }
14777 else if (processing_template_decl && !error_operand_p (bounds))
14778 {
14779 /* Remember this wasn't a constant-expression. */
14780 bounds = build_nop (TREE_TYPE (bounds), bounds);
14781 TREE_SIDE_EFFECTS (bounds) = 1;
14782 }
14783 }
14784 else
14785 bounds = NULL_TREE;
14786 /* Look for the closing `]'. */
14787 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14788 {
14789 declarator = cp_error_declarator;
14790 break;
14791 }
14792
14793 declarator = make_array_declarator (declarator, bounds);
14794 }
14795 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14796 {
14797 {
14798 tree qualifying_scope;
14799 tree unqualified_name;
14800 special_function_kind sfk;
14801 bool abstract_ok;
14802 bool pack_expansion_p = false;
14803 cp_token *declarator_id_start_token;
14804
14805 /* Parse a declarator-id */
14806 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14807 if (abstract_ok)
14808 {
14809 cp_parser_parse_tentatively (parser);
14810
14811 /* If we see an ellipsis, we should be looking at a
14812 parameter pack. */
14813 if (token->type == CPP_ELLIPSIS)
14814 {
14815 /* Consume the `...' */
14816 cp_lexer_consume_token (parser->lexer);
14817
14818 pack_expansion_p = true;
14819 }
14820 }
14821
14822 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14823 unqualified_name
14824 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14825 qualifying_scope = parser->scope;
14826 if (abstract_ok)
14827 {
14828 bool okay = false;
14829
14830 if (!unqualified_name && pack_expansion_p)
14831 {
14832 /* Check whether an error occurred. */
14833 okay = !cp_parser_error_occurred (parser);
14834
14835 /* We already consumed the ellipsis to mark a
14836 parameter pack, but we have no way to report it,
14837 so abort the tentative parse. We will be exiting
14838 immediately anyway. */
14839 cp_parser_abort_tentative_parse (parser);
14840 }
14841 else
14842 okay = cp_parser_parse_definitely (parser);
14843
14844 if (!okay)
14845 unqualified_name = error_mark_node;
14846 else if (unqualified_name
14847 && (qualifying_scope
14848 || (TREE_CODE (unqualified_name)
14849 != IDENTIFIER_NODE)))
14850 {
14851 cp_parser_error (parser, "expected unqualified-id");
14852 unqualified_name = error_mark_node;
14853 }
14854 }
14855
14856 if (!unqualified_name)
14857 return NULL;
14858 if (unqualified_name == error_mark_node)
14859 {
14860 declarator = cp_error_declarator;
14861 pack_expansion_p = false;
14862 declarator->parameter_pack_p = false;
14863 break;
14864 }
14865
14866 if (qualifying_scope && at_namespace_scope_p ()
14867 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14868 {
14869 /* In the declaration of a member of a template class
14870 outside of the class itself, the SCOPE will sometimes
14871 be a TYPENAME_TYPE. For example, given:
14872
14873 template <typename T>
14874 int S<T>::R::i = 3;
14875
14876 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14877 this context, we must resolve S<T>::R to an ordinary
14878 type, rather than a typename type.
14879
14880 The reason we normally avoid resolving TYPENAME_TYPEs
14881 is that a specialization of `S' might render
14882 `S<T>::R' not a type. However, if `S' is
14883 specialized, then this `i' will not be used, so there
14884 is no harm in resolving the types here. */
14885 tree type;
14886
14887 /* Resolve the TYPENAME_TYPE. */
14888 type = resolve_typename_type (qualifying_scope,
14889 /*only_current_p=*/false);
14890 /* If that failed, the declarator is invalid. */
14891 if (TREE_CODE (type) == TYPENAME_TYPE)
14892 {
14893 if (typedef_variant_p (type))
14894 error_at (declarator_id_start_token->location,
14895 "cannot define member of dependent typedef "
14896 "%qT", type);
14897 else
14898 error_at (declarator_id_start_token->location,
14899 "%<%T::%E%> is not a type",
14900 TYPE_CONTEXT (qualifying_scope),
14901 TYPE_IDENTIFIER (qualifying_scope));
14902 }
14903 qualifying_scope = type;
14904 }
14905
14906 sfk = sfk_none;
14907
14908 if (unqualified_name)
14909 {
14910 tree class_type;
14911
14912 if (qualifying_scope
14913 && CLASS_TYPE_P (qualifying_scope))
14914 class_type = qualifying_scope;
14915 else
14916 class_type = current_class_type;
14917
14918 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14919 {
14920 tree name_type = TREE_TYPE (unqualified_name);
14921 if (class_type && same_type_p (name_type, class_type))
14922 {
14923 if (qualifying_scope
14924 && CLASSTYPE_USE_TEMPLATE (name_type))
14925 {
14926 error_at (declarator_id_start_token->location,
14927 "invalid use of constructor as a template");
14928 inform (declarator_id_start_token->location,
14929 "use %<%T::%D%> instead of %<%T::%D%> to "
14930 "name the constructor in a qualified name",
14931 class_type,
14932 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14933 class_type, name_type);
14934 declarator = cp_error_declarator;
14935 break;
14936 }
14937 else
14938 unqualified_name = constructor_name (class_type);
14939 }
14940 else
14941 {
14942 /* We do not attempt to print the declarator
14943 here because we do not have enough
14944 information about its original syntactic
14945 form. */
14946 cp_parser_error (parser, "invalid declarator");
14947 declarator = cp_error_declarator;
14948 break;
14949 }
14950 }
14951
14952 if (class_type)
14953 {
14954 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14955 sfk = sfk_destructor;
14956 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14957 sfk = sfk_conversion;
14958 else if (/* There's no way to declare a constructor
14959 for an anonymous type, even if the type
14960 got a name for linkage purposes. */
14961 !TYPE_WAS_ANONYMOUS (class_type)
14962 && constructor_name_p (unqualified_name,
14963 class_type))
14964 {
14965 unqualified_name = constructor_name (class_type);
14966 sfk = sfk_constructor;
14967 }
14968 else if (is_overloaded_fn (unqualified_name)
14969 && DECL_CONSTRUCTOR_P (get_first_fn
14970 (unqualified_name)))
14971 sfk = sfk_constructor;
14972
14973 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14974 *ctor_dtor_or_conv_p = -1;
14975 }
14976 }
14977 declarator = make_id_declarator (qualifying_scope,
14978 unqualified_name,
14979 sfk);
14980 declarator->id_loc = token->location;
14981 declarator->parameter_pack_p = pack_expansion_p;
14982
14983 if (pack_expansion_p)
14984 maybe_warn_variadic_templates ();
14985 }
14986
14987 handle_declarator:;
14988 scope = get_scope_of_declarator (declarator);
14989 if (scope)
14990 /* Any names that appear after the declarator-id for a
14991 member are looked up in the containing scope. */
14992 pushed_scope = push_scope (scope);
14993 parser->in_declarator_p = true;
14994 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14995 || (declarator && declarator->kind == cdk_id))
14996 /* Default args are only allowed on function
14997 declarations. */
14998 parser->default_arg_ok_p = saved_default_arg_ok_p;
14999 else
15000 parser->default_arg_ok_p = false;
15001
15002 first = false;
15003 }
15004 /* We're done. */
15005 else
15006 break;
15007 }
15008
15009 /* For an abstract declarator, we might wind up with nothing at this
15010 point. That's an error; the declarator is not optional. */
15011 if (!declarator)
15012 cp_parser_error (parser, "expected declarator");
15013
15014 /* If we entered a scope, we must exit it now. */
15015 if (pushed_scope)
15016 pop_scope (pushed_scope);
15017
15018 parser->default_arg_ok_p = saved_default_arg_ok_p;
15019 parser->in_declarator_p = saved_in_declarator_p;
15020
15021 return declarator;
15022 }
15023
15024 /* Parse a ptr-operator.
15025
15026 ptr-operator:
15027 * cv-qualifier-seq [opt]
15028 &
15029 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15030
15031 GNU Extension:
15032
15033 ptr-operator:
15034 & cv-qualifier-seq [opt]
15035
15036 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15037 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15038 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15039 filled in with the TYPE containing the member. *CV_QUALS is
15040 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15041 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15042 Note that the tree codes returned by this function have nothing
15043 to do with the types of trees that will be eventually be created
15044 to represent the pointer or reference type being parsed. They are
15045 just constants with suggestive names. */
15046 static enum tree_code
15047 cp_parser_ptr_operator (cp_parser* parser,
15048 tree* type,
15049 cp_cv_quals *cv_quals)
15050 {
15051 enum tree_code code = ERROR_MARK;
15052 cp_token *token;
15053
15054 /* Assume that it's not a pointer-to-member. */
15055 *type = NULL_TREE;
15056 /* And that there are no cv-qualifiers. */
15057 *cv_quals = TYPE_UNQUALIFIED;
15058
15059 /* Peek at the next token. */
15060 token = cp_lexer_peek_token (parser->lexer);
15061
15062 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15063 if (token->type == CPP_MULT)
15064 code = INDIRECT_REF;
15065 else if (token->type == CPP_AND)
15066 code = ADDR_EXPR;
15067 else if ((cxx_dialect != cxx98) &&
15068 token->type == CPP_AND_AND) /* C++0x only */
15069 code = NON_LVALUE_EXPR;
15070
15071 if (code != ERROR_MARK)
15072 {
15073 /* Consume the `*', `&' or `&&'. */
15074 cp_lexer_consume_token (parser->lexer);
15075
15076 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15077 `&', if we are allowing GNU extensions. (The only qualifier
15078 that can legally appear after `&' is `restrict', but that is
15079 enforced during semantic analysis. */
15080 if (code == INDIRECT_REF
15081 || cp_parser_allow_gnu_extensions_p (parser))
15082 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15083 }
15084 else
15085 {
15086 /* Try the pointer-to-member case. */
15087 cp_parser_parse_tentatively (parser);
15088 /* Look for the optional `::' operator. */
15089 cp_parser_global_scope_opt (parser,
15090 /*current_scope_valid_p=*/false);
15091 /* Look for the nested-name specifier. */
15092 token = cp_lexer_peek_token (parser->lexer);
15093 cp_parser_nested_name_specifier (parser,
15094 /*typename_keyword_p=*/false,
15095 /*check_dependency_p=*/true,
15096 /*type_p=*/false,
15097 /*is_declaration=*/false);
15098 /* If we found it, and the next token is a `*', then we are
15099 indeed looking at a pointer-to-member operator. */
15100 if (!cp_parser_error_occurred (parser)
15101 && cp_parser_require (parser, CPP_MULT, RT_MULT))
15102 {
15103 /* Indicate that the `*' operator was used. */
15104 code = INDIRECT_REF;
15105
15106 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15107 error_at (token->location, "%qD is a namespace", parser->scope);
15108 else
15109 {
15110 /* The type of which the member is a member is given by the
15111 current SCOPE. */
15112 *type = parser->scope;
15113 /* The next name will not be qualified. */
15114 parser->scope = NULL_TREE;
15115 parser->qualifying_scope = NULL_TREE;
15116 parser->object_scope = NULL_TREE;
15117 /* Look for the optional cv-qualifier-seq. */
15118 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15119 }
15120 }
15121 /* If that didn't work we don't have a ptr-operator. */
15122 if (!cp_parser_parse_definitely (parser))
15123 cp_parser_error (parser, "expected ptr-operator");
15124 }
15125
15126 return code;
15127 }
15128
15129 /* Parse an (optional) cv-qualifier-seq.
15130
15131 cv-qualifier-seq:
15132 cv-qualifier cv-qualifier-seq [opt]
15133
15134 cv-qualifier:
15135 const
15136 volatile
15137
15138 GNU Extension:
15139
15140 cv-qualifier:
15141 __restrict__
15142
15143 Returns a bitmask representing the cv-qualifiers. */
15144
15145 static cp_cv_quals
15146 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15147 {
15148 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15149
15150 while (true)
15151 {
15152 cp_token *token;
15153 cp_cv_quals cv_qualifier;
15154
15155 /* Peek at the next token. */
15156 token = cp_lexer_peek_token (parser->lexer);
15157 /* See if it's a cv-qualifier. */
15158 switch (token->keyword)
15159 {
15160 case RID_CONST:
15161 cv_qualifier = TYPE_QUAL_CONST;
15162 break;
15163
15164 case RID_VOLATILE:
15165 cv_qualifier = TYPE_QUAL_VOLATILE;
15166 break;
15167
15168 case RID_RESTRICT:
15169 cv_qualifier = TYPE_QUAL_RESTRICT;
15170 break;
15171
15172 default:
15173 cv_qualifier = TYPE_UNQUALIFIED;
15174 break;
15175 }
15176
15177 if (!cv_qualifier)
15178 break;
15179
15180 if (cv_quals & cv_qualifier)
15181 {
15182 error_at (token->location, "duplicate cv-qualifier");
15183 cp_lexer_purge_token (parser->lexer);
15184 }
15185 else
15186 {
15187 cp_lexer_consume_token (parser->lexer);
15188 cv_quals |= cv_qualifier;
15189 }
15190 }
15191
15192 return cv_quals;
15193 }
15194
15195 /* Parse a late-specified return type, if any. This is not a separate
15196 non-terminal, but part of a function declarator, which looks like
15197
15198 -> trailing-type-specifier-seq abstract-declarator(opt)
15199
15200 Returns the type indicated by the type-id. */
15201
15202 static tree
15203 cp_parser_late_return_type_opt (cp_parser* parser)
15204 {
15205 cp_token *token;
15206
15207 /* Peek at the next token. */
15208 token = cp_lexer_peek_token (parser->lexer);
15209 /* A late-specified return type is indicated by an initial '->'. */
15210 if (token->type != CPP_DEREF)
15211 return NULL_TREE;
15212
15213 /* Consume the ->. */
15214 cp_lexer_consume_token (parser->lexer);
15215
15216 return cp_parser_trailing_type_id (parser);
15217 }
15218
15219 /* Parse a declarator-id.
15220
15221 declarator-id:
15222 id-expression
15223 :: [opt] nested-name-specifier [opt] type-name
15224
15225 In the `id-expression' case, the value returned is as for
15226 cp_parser_id_expression if the id-expression was an unqualified-id.
15227 If the id-expression was a qualified-id, then a SCOPE_REF is
15228 returned. The first operand is the scope (either a NAMESPACE_DECL
15229 or TREE_TYPE), but the second is still just a representation of an
15230 unqualified-id. */
15231
15232 static tree
15233 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15234 {
15235 tree id;
15236 /* The expression must be an id-expression. Assume that qualified
15237 names are the names of types so that:
15238
15239 template <class T>
15240 int S<T>::R::i = 3;
15241
15242 will work; we must treat `S<T>::R' as the name of a type.
15243 Similarly, assume that qualified names are templates, where
15244 required, so that:
15245
15246 template <class T>
15247 int S<T>::R<T>::i = 3;
15248
15249 will work, too. */
15250 id = cp_parser_id_expression (parser,
15251 /*template_keyword_p=*/false,
15252 /*check_dependency_p=*/false,
15253 /*template_p=*/NULL,
15254 /*declarator_p=*/true,
15255 optional_p);
15256 if (id && BASELINK_P (id))
15257 id = BASELINK_FUNCTIONS (id);
15258 return id;
15259 }
15260
15261 /* Parse a type-id.
15262
15263 type-id:
15264 type-specifier-seq abstract-declarator [opt]
15265
15266 Returns the TYPE specified. */
15267
15268 static tree
15269 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15270 bool is_trailing_return)
15271 {
15272 cp_decl_specifier_seq type_specifier_seq;
15273 cp_declarator *abstract_declarator;
15274
15275 /* Parse the type-specifier-seq. */
15276 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15277 is_trailing_return,
15278 &type_specifier_seq);
15279 if (type_specifier_seq.type == error_mark_node)
15280 return error_mark_node;
15281
15282 /* There might or might not be an abstract declarator. */
15283 cp_parser_parse_tentatively (parser);
15284 /* Look for the declarator. */
15285 abstract_declarator
15286 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15287 /*parenthesized_p=*/NULL,
15288 /*member_p=*/false);
15289 /* Check to see if there really was a declarator. */
15290 if (!cp_parser_parse_definitely (parser))
15291 abstract_declarator = NULL;
15292
15293 if (type_specifier_seq.type
15294 && type_uses_auto (type_specifier_seq.type))
15295 {
15296 /* A type-id with type 'auto' is only ok if the abstract declarator
15297 is a function declarator with a late-specified return type. */
15298 if (abstract_declarator
15299 && abstract_declarator->kind == cdk_function
15300 && abstract_declarator->u.function.late_return_type)
15301 /* OK */;
15302 else
15303 {
15304 error ("invalid use of %<auto%>");
15305 return error_mark_node;
15306 }
15307 }
15308
15309 return groktypename (&type_specifier_seq, abstract_declarator,
15310 is_template_arg);
15311 }
15312
15313 static tree cp_parser_type_id (cp_parser *parser)
15314 {
15315 return cp_parser_type_id_1 (parser, false, false);
15316 }
15317
15318 static tree cp_parser_template_type_arg (cp_parser *parser)
15319 {
15320 return cp_parser_type_id_1 (parser, true, false);
15321 }
15322
15323 static tree cp_parser_trailing_type_id (cp_parser *parser)
15324 {
15325 return cp_parser_type_id_1 (parser, false, true);
15326 }
15327
15328 /* Parse a type-specifier-seq.
15329
15330 type-specifier-seq:
15331 type-specifier type-specifier-seq [opt]
15332
15333 GNU extension:
15334
15335 type-specifier-seq:
15336 attributes type-specifier-seq [opt]
15337
15338 If IS_DECLARATION is true, we are at the start of a "condition" or
15339 exception-declaration, so we might be followed by a declarator-id.
15340
15341 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15342 i.e. we've just seen "->".
15343
15344 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15345
15346 static void
15347 cp_parser_type_specifier_seq (cp_parser* parser,
15348 bool is_declaration,
15349 bool is_trailing_return,
15350 cp_decl_specifier_seq *type_specifier_seq)
15351 {
15352 bool seen_type_specifier = false;
15353 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15354 cp_token *start_token = NULL;
15355
15356 /* Clear the TYPE_SPECIFIER_SEQ. */
15357 clear_decl_specs (type_specifier_seq);
15358
15359 /* In the context of a trailing return type, enum E { } is an
15360 elaborated-type-specifier followed by a function-body, not an
15361 enum-specifier. */
15362 if (is_trailing_return)
15363 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15364
15365 /* Parse the type-specifiers and attributes. */
15366 while (true)
15367 {
15368 tree type_specifier;
15369 bool is_cv_qualifier;
15370
15371 /* Check for attributes first. */
15372 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15373 {
15374 type_specifier_seq->attributes =
15375 chainon (type_specifier_seq->attributes,
15376 cp_parser_attributes_opt (parser));
15377 continue;
15378 }
15379
15380 /* record the token of the beginning of the type specifier seq,
15381 for error reporting purposes*/
15382 if (!start_token)
15383 start_token = cp_lexer_peek_token (parser->lexer);
15384
15385 /* Look for the type-specifier. */
15386 type_specifier = cp_parser_type_specifier (parser,
15387 flags,
15388 type_specifier_seq,
15389 /*is_declaration=*/false,
15390 NULL,
15391 &is_cv_qualifier);
15392 if (!type_specifier)
15393 {
15394 /* If the first type-specifier could not be found, this is not a
15395 type-specifier-seq at all. */
15396 if (!seen_type_specifier)
15397 {
15398 cp_parser_error (parser, "expected type-specifier");
15399 type_specifier_seq->type = error_mark_node;
15400 return;
15401 }
15402 /* If subsequent type-specifiers could not be found, the
15403 type-specifier-seq is complete. */
15404 break;
15405 }
15406
15407 seen_type_specifier = true;
15408 /* The standard says that a condition can be:
15409
15410 type-specifier-seq declarator = assignment-expression
15411
15412 However, given:
15413
15414 struct S {};
15415 if (int S = ...)
15416
15417 we should treat the "S" as a declarator, not as a
15418 type-specifier. The standard doesn't say that explicitly for
15419 type-specifier-seq, but it does say that for
15420 decl-specifier-seq in an ordinary declaration. Perhaps it
15421 would be clearer just to allow a decl-specifier-seq here, and
15422 then add a semantic restriction that if any decl-specifiers
15423 that are not type-specifiers appear, the program is invalid. */
15424 if (is_declaration && !is_cv_qualifier)
15425 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15426 }
15427
15428 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15429 }
15430
15431 /* Parse a parameter-declaration-clause.
15432
15433 parameter-declaration-clause:
15434 parameter-declaration-list [opt] ... [opt]
15435 parameter-declaration-list , ...
15436
15437 Returns a representation for the parameter declarations. A return
15438 value of NULL indicates a parameter-declaration-clause consisting
15439 only of an ellipsis. */
15440
15441 static tree
15442 cp_parser_parameter_declaration_clause (cp_parser* parser)
15443 {
15444 tree parameters;
15445 cp_token *token;
15446 bool ellipsis_p;
15447 bool is_error;
15448
15449 /* Peek at the next token. */
15450 token = cp_lexer_peek_token (parser->lexer);
15451 /* Check for trivial parameter-declaration-clauses. */
15452 if (token->type == CPP_ELLIPSIS)
15453 {
15454 /* Consume the `...' token. */
15455 cp_lexer_consume_token (parser->lexer);
15456 return NULL_TREE;
15457 }
15458 else if (token->type == CPP_CLOSE_PAREN)
15459 /* There are no parameters. */
15460 {
15461 #ifndef NO_IMPLICIT_EXTERN_C
15462 if (in_system_header && current_class_type == NULL
15463 && current_lang_name == lang_name_c)
15464 return NULL_TREE;
15465 else
15466 #endif
15467 return void_list_node;
15468 }
15469 /* Check for `(void)', too, which is a special case. */
15470 else if (token->keyword == RID_VOID
15471 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15472 == CPP_CLOSE_PAREN))
15473 {
15474 /* Consume the `void' token. */
15475 cp_lexer_consume_token (parser->lexer);
15476 /* There are no parameters. */
15477 return void_list_node;
15478 }
15479
15480 /* Parse the parameter-declaration-list. */
15481 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15482 /* If a parse error occurred while parsing the
15483 parameter-declaration-list, then the entire
15484 parameter-declaration-clause is erroneous. */
15485 if (is_error)
15486 return NULL;
15487
15488 /* Peek at the next token. */
15489 token = cp_lexer_peek_token (parser->lexer);
15490 /* If it's a `,', the clause should terminate with an ellipsis. */
15491 if (token->type == CPP_COMMA)
15492 {
15493 /* Consume the `,'. */
15494 cp_lexer_consume_token (parser->lexer);
15495 /* Expect an ellipsis. */
15496 ellipsis_p
15497 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15498 }
15499 /* It might also be `...' if the optional trailing `,' was
15500 omitted. */
15501 else if (token->type == CPP_ELLIPSIS)
15502 {
15503 /* Consume the `...' token. */
15504 cp_lexer_consume_token (parser->lexer);
15505 /* And remember that we saw it. */
15506 ellipsis_p = true;
15507 }
15508 else
15509 ellipsis_p = false;
15510
15511 /* Finish the parameter list. */
15512 if (!ellipsis_p)
15513 parameters = chainon (parameters, void_list_node);
15514
15515 return parameters;
15516 }
15517
15518 /* Parse a parameter-declaration-list.
15519
15520 parameter-declaration-list:
15521 parameter-declaration
15522 parameter-declaration-list , parameter-declaration
15523
15524 Returns a representation of the parameter-declaration-list, as for
15525 cp_parser_parameter_declaration_clause. However, the
15526 `void_list_node' is never appended to the list. Upon return,
15527 *IS_ERROR will be true iff an error occurred. */
15528
15529 static tree
15530 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15531 {
15532 tree parameters = NULL_TREE;
15533 tree *tail = &parameters;
15534 bool saved_in_unbraced_linkage_specification_p;
15535 int index = 0;
15536
15537 /* Assume all will go well. */
15538 *is_error = false;
15539 /* The special considerations that apply to a function within an
15540 unbraced linkage specifications do not apply to the parameters
15541 to the function. */
15542 saved_in_unbraced_linkage_specification_p
15543 = parser->in_unbraced_linkage_specification_p;
15544 parser->in_unbraced_linkage_specification_p = false;
15545
15546 /* Look for more parameters. */
15547 while (true)
15548 {
15549 cp_parameter_declarator *parameter;
15550 tree decl = error_mark_node;
15551 bool parenthesized_p;
15552 /* Parse the parameter. */
15553 parameter
15554 = cp_parser_parameter_declaration (parser,
15555 /*template_parm_p=*/false,
15556 &parenthesized_p);
15557
15558 /* We don't know yet if the enclosing context is deprecated, so wait
15559 and warn in grokparms if appropriate. */
15560 deprecated_state = DEPRECATED_SUPPRESS;
15561
15562 if (parameter)
15563 decl = grokdeclarator (parameter->declarator,
15564 &parameter->decl_specifiers,
15565 PARM,
15566 parameter->default_argument != NULL_TREE,
15567 &parameter->decl_specifiers.attributes);
15568
15569 deprecated_state = DEPRECATED_NORMAL;
15570
15571 /* If a parse error occurred parsing the parameter declaration,
15572 then the entire parameter-declaration-list is erroneous. */
15573 if (decl == error_mark_node)
15574 {
15575 *is_error = true;
15576 parameters = error_mark_node;
15577 break;
15578 }
15579
15580 if (parameter->decl_specifiers.attributes)
15581 cplus_decl_attributes (&decl,
15582 parameter->decl_specifiers.attributes,
15583 0);
15584 if (DECL_NAME (decl))
15585 decl = pushdecl (decl);
15586
15587 if (decl != error_mark_node)
15588 {
15589 retrofit_lang_decl (decl);
15590 DECL_PARM_INDEX (decl) = ++index;
15591 }
15592
15593 /* Add the new parameter to the list. */
15594 *tail = build_tree_list (parameter->default_argument, decl);
15595 tail = &TREE_CHAIN (*tail);
15596
15597 /* Peek at the next token. */
15598 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15599 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15600 /* These are for Objective-C++ */
15601 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15602 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15603 /* The parameter-declaration-list is complete. */
15604 break;
15605 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15606 {
15607 cp_token *token;
15608
15609 /* Peek at the next token. */
15610 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15611 /* If it's an ellipsis, then the list is complete. */
15612 if (token->type == CPP_ELLIPSIS)
15613 break;
15614 /* Otherwise, there must be more parameters. Consume the
15615 `,'. */
15616 cp_lexer_consume_token (parser->lexer);
15617 /* When parsing something like:
15618
15619 int i(float f, double d)
15620
15621 we can tell after seeing the declaration for "f" that we
15622 are not looking at an initialization of a variable "i",
15623 but rather at the declaration of a function "i".
15624
15625 Due to the fact that the parsing of template arguments
15626 (as specified to a template-id) requires backtracking we
15627 cannot use this technique when inside a template argument
15628 list. */
15629 if (!parser->in_template_argument_list_p
15630 && !parser->in_type_id_in_expr_p
15631 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15632 /* However, a parameter-declaration of the form
15633 "foat(f)" (which is a valid declaration of a
15634 parameter "f") can also be interpreted as an
15635 expression (the conversion of "f" to "float"). */
15636 && !parenthesized_p)
15637 cp_parser_commit_to_tentative_parse (parser);
15638 }
15639 else
15640 {
15641 cp_parser_error (parser, "expected %<,%> or %<...%>");
15642 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15643 cp_parser_skip_to_closing_parenthesis (parser,
15644 /*recovering=*/true,
15645 /*or_comma=*/false,
15646 /*consume_paren=*/false);
15647 break;
15648 }
15649 }
15650
15651 parser->in_unbraced_linkage_specification_p
15652 = saved_in_unbraced_linkage_specification_p;
15653
15654 return parameters;
15655 }
15656
15657 /* Parse a parameter declaration.
15658
15659 parameter-declaration:
15660 decl-specifier-seq ... [opt] declarator
15661 decl-specifier-seq declarator = assignment-expression
15662 decl-specifier-seq ... [opt] abstract-declarator [opt]
15663 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15664
15665 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15666 declares a template parameter. (In that case, a non-nested `>'
15667 token encountered during the parsing of the assignment-expression
15668 is not interpreted as a greater-than operator.)
15669
15670 Returns a representation of the parameter, or NULL if an error
15671 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15672 true iff the declarator is of the form "(p)". */
15673
15674 static cp_parameter_declarator *
15675 cp_parser_parameter_declaration (cp_parser *parser,
15676 bool template_parm_p,
15677 bool *parenthesized_p)
15678 {
15679 int declares_class_or_enum;
15680 cp_decl_specifier_seq decl_specifiers;
15681 cp_declarator *declarator;
15682 tree default_argument;
15683 cp_token *token = NULL, *declarator_token_start = NULL;
15684 const char *saved_message;
15685
15686 /* In a template parameter, `>' is not an operator.
15687
15688 [temp.param]
15689
15690 When parsing a default template-argument for a non-type
15691 template-parameter, the first non-nested `>' is taken as the end
15692 of the template parameter-list rather than a greater-than
15693 operator. */
15694
15695 /* Type definitions may not appear in parameter types. */
15696 saved_message = parser->type_definition_forbidden_message;
15697 parser->type_definition_forbidden_message
15698 = G_("types may not be defined in parameter types");
15699
15700 /* Parse the declaration-specifiers. */
15701 cp_parser_decl_specifier_seq (parser,
15702 CP_PARSER_FLAGS_NONE,
15703 &decl_specifiers,
15704 &declares_class_or_enum);
15705
15706 /* Complain about missing 'typename' or other invalid type names. */
15707 if (!decl_specifiers.any_type_specifiers_p)
15708 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15709
15710 /* If an error occurred, there's no reason to attempt to parse the
15711 rest of the declaration. */
15712 if (cp_parser_error_occurred (parser))
15713 {
15714 parser->type_definition_forbidden_message = saved_message;
15715 return NULL;
15716 }
15717
15718 /* Peek at the next token. */
15719 token = cp_lexer_peek_token (parser->lexer);
15720
15721 /* If the next token is a `)', `,', `=', `>', or `...', then there
15722 is no declarator. However, when variadic templates are enabled,
15723 there may be a declarator following `...'. */
15724 if (token->type == CPP_CLOSE_PAREN
15725 || token->type == CPP_COMMA
15726 || token->type == CPP_EQ
15727 || token->type == CPP_GREATER)
15728 {
15729 declarator = NULL;
15730 if (parenthesized_p)
15731 *parenthesized_p = false;
15732 }
15733 /* Otherwise, there should be a declarator. */
15734 else
15735 {
15736 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15737 parser->default_arg_ok_p = false;
15738
15739 /* After seeing a decl-specifier-seq, if the next token is not a
15740 "(", there is no possibility that the code is a valid
15741 expression. Therefore, if parsing tentatively, we commit at
15742 this point. */
15743 if (!parser->in_template_argument_list_p
15744 /* In an expression context, having seen:
15745
15746 (int((char ...
15747
15748 we cannot be sure whether we are looking at a
15749 function-type (taking a "char" as a parameter) or a cast
15750 of some object of type "char" to "int". */
15751 && !parser->in_type_id_in_expr_p
15752 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15753 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15754 cp_parser_commit_to_tentative_parse (parser);
15755 /* Parse the declarator. */
15756 declarator_token_start = token;
15757 declarator = cp_parser_declarator (parser,
15758 CP_PARSER_DECLARATOR_EITHER,
15759 /*ctor_dtor_or_conv_p=*/NULL,
15760 parenthesized_p,
15761 /*member_p=*/false);
15762 parser->default_arg_ok_p = saved_default_arg_ok_p;
15763 /* After the declarator, allow more attributes. */
15764 decl_specifiers.attributes
15765 = chainon (decl_specifiers.attributes,
15766 cp_parser_attributes_opt (parser));
15767 }
15768
15769 /* If the next token is an ellipsis, and we have not seen a
15770 declarator name, and the type of the declarator contains parameter
15771 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15772 a parameter pack expansion expression. Otherwise, leave the
15773 ellipsis for a C-style variadic function. */
15774 token = cp_lexer_peek_token (parser->lexer);
15775 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15776 {
15777 tree type = decl_specifiers.type;
15778
15779 if (type && DECL_P (type))
15780 type = TREE_TYPE (type);
15781
15782 if (type
15783 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15784 && declarator_can_be_parameter_pack (declarator)
15785 && (!declarator || !declarator->parameter_pack_p)
15786 && uses_parameter_packs (type))
15787 {
15788 /* Consume the `...'. */
15789 cp_lexer_consume_token (parser->lexer);
15790 maybe_warn_variadic_templates ();
15791
15792 /* Build a pack expansion type */
15793 if (declarator)
15794 declarator->parameter_pack_p = true;
15795 else
15796 decl_specifiers.type = make_pack_expansion (type);
15797 }
15798 }
15799
15800 /* The restriction on defining new types applies only to the type
15801 of the parameter, not to the default argument. */
15802 parser->type_definition_forbidden_message = saved_message;
15803
15804 /* If the next token is `=', then process a default argument. */
15805 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15806 {
15807 /* Consume the `='. */
15808 cp_lexer_consume_token (parser->lexer);
15809
15810 /* If we are defining a class, then the tokens that make up the
15811 default argument must be saved and processed later. */
15812 if (!template_parm_p && at_class_scope_p ()
15813 && TYPE_BEING_DEFINED (current_class_type)
15814 && !LAMBDA_TYPE_P (current_class_type))
15815 {
15816 unsigned depth = 0;
15817 int maybe_template_id = 0;
15818 cp_token *first_token;
15819 cp_token *token;
15820
15821 /* Add tokens until we have processed the entire default
15822 argument. We add the range [first_token, token). */
15823 first_token = cp_lexer_peek_token (parser->lexer);
15824 while (true)
15825 {
15826 bool done = false;
15827
15828 /* Peek at the next token. */
15829 token = cp_lexer_peek_token (parser->lexer);
15830 /* What we do depends on what token we have. */
15831 switch (token->type)
15832 {
15833 /* In valid code, a default argument must be
15834 immediately followed by a `,' `)', or `...'. */
15835 case CPP_COMMA:
15836 if (depth == 0 && maybe_template_id)
15837 {
15838 /* If we've seen a '<', we might be in a
15839 template-argument-list. Until Core issue 325 is
15840 resolved, we don't know how this situation ought
15841 to be handled, so try to DTRT. We check whether
15842 what comes after the comma is a valid parameter
15843 declaration list. If it is, then the comma ends
15844 the default argument; otherwise the default
15845 argument continues. */
15846 bool error = false;
15847 tree t;
15848
15849 /* Set ITALP so cp_parser_parameter_declaration_list
15850 doesn't decide to commit to this parse. */
15851 bool saved_italp = parser->in_template_argument_list_p;
15852 parser->in_template_argument_list_p = true;
15853
15854 cp_parser_parse_tentatively (parser);
15855 cp_lexer_consume_token (parser->lexer);
15856 begin_scope (sk_function_parms, NULL_TREE);
15857 cp_parser_parameter_declaration_list (parser, &error);
15858 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15859 pop_binding (DECL_NAME (t), t);
15860 leave_scope ();
15861 if (!cp_parser_error_occurred (parser) && !error)
15862 done = true;
15863 cp_parser_abort_tentative_parse (parser);
15864
15865 parser->in_template_argument_list_p = saved_italp;
15866 break;
15867 }
15868 case CPP_CLOSE_PAREN:
15869 case CPP_ELLIPSIS:
15870 /* If we run into a non-nested `;', `}', or `]',
15871 then the code is invalid -- but the default
15872 argument is certainly over. */
15873 case CPP_SEMICOLON:
15874 case CPP_CLOSE_BRACE:
15875 case CPP_CLOSE_SQUARE:
15876 if (depth == 0)
15877 done = true;
15878 /* Update DEPTH, if necessary. */
15879 else if (token->type == CPP_CLOSE_PAREN
15880 || token->type == CPP_CLOSE_BRACE
15881 || token->type == CPP_CLOSE_SQUARE)
15882 --depth;
15883 break;
15884
15885 case CPP_OPEN_PAREN:
15886 case CPP_OPEN_SQUARE:
15887 case CPP_OPEN_BRACE:
15888 ++depth;
15889 break;
15890
15891 case CPP_LESS:
15892 if (depth == 0)
15893 /* This might be the comparison operator, or it might
15894 start a template argument list. */
15895 ++maybe_template_id;
15896 break;
15897
15898 case CPP_RSHIFT:
15899 if (cxx_dialect == cxx98)
15900 break;
15901 /* Fall through for C++0x, which treats the `>>'
15902 operator like two `>' tokens in certain
15903 cases. */
15904
15905 case CPP_GREATER:
15906 if (depth == 0)
15907 {
15908 /* This might be an operator, or it might close a
15909 template argument list. But if a previous '<'
15910 started a template argument list, this will have
15911 closed it, so we can't be in one anymore. */
15912 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15913 if (maybe_template_id < 0)
15914 maybe_template_id = 0;
15915 }
15916 break;
15917
15918 /* If we run out of tokens, issue an error message. */
15919 case CPP_EOF:
15920 case CPP_PRAGMA_EOL:
15921 error_at (token->location, "file ends in default argument");
15922 done = true;
15923 break;
15924
15925 case CPP_NAME:
15926 case CPP_SCOPE:
15927 /* In these cases, we should look for template-ids.
15928 For example, if the default argument is
15929 `X<int, double>()', we need to do name lookup to
15930 figure out whether or not `X' is a template; if
15931 so, the `,' does not end the default argument.
15932
15933 That is not yet done. */
15934 break;
15935
15936 default:
15937 break;
15938 }
15939
15940 /* If we've reached the end, stop. */
15941 if (done)
15942 break;
15943
15944 /* Add the token to the token block. */
15945 token = cp_lexer_consume_token (parser->lexer);
15946 }
15947
15948 /* Create a DEFAULT_ARG to represent the unparsed default
15949 argument. */
15950 default_argument = make_node (DEFAULT_ARG);
15951 DEFARG_TOKENS (default_argument)
15952 = cp_token_cache_new (first_token, token);
15953 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15954 }
15955 /* Outside of a class definition, we can just parse the
15956 assignment-expression. */
15957 else
15958 {
15959 token = cp_lexer_peek_token (parser->lexer);
15960 default_argument
15961 = cp_parser_default_argument (parser, template_parm_p);
15962 }
15963
15964 if (!parser->default_arg_ok_p)
15965 {
15966 if (flag_permissive)
15967 warning (0, "deprecated use of default argument for parameter of non-function");
15968 else
15969 {
15970 error_at (token->location,
15971 "default arguments are only "
15972 "permitted for function parameters");
15973 default_argument = NULL_TREE;
15974 }
15975 }
15976 else if ((declarator && declarator->parameter_pack_p)
15977 || (decl_specifiers.type
15978 && PACK_EXPANSION_P (decl_specifiers.type)))
15979 {
15980 /* Find the name of the parameter pack. */
15981 cp_declarator *id_declarator = declarator;
15982 while (id_declarator && id_declarator->kind != cdk_id)
15983 id_declarator = id_declarator->declarator;
15984
15985 if (id_declarator && id_declarator->kind == cdk_id)
15986 error_at (declarator_token_start->location,
15987 template_parm_p
15988 ? "template parameter pack %qD"
15989 " cannot have a default argument"
15990 : "parameter pack %qD cannot have a default argument",
15991 id_declarator->u.id.unqualified_name);
15992 else
15993 error_at (declarator_token_start->location,
15994 template_parm_p
15995 ? "template parameter pack cannot have a default argument"
15996 : "parameter pack cannot have a default argument");
15997
15998 default_argument = NULL_TREE;
15999 }
16000 }
16001 else
16002 default_argument = NULL_TREE;
16003
16004 return make_parameter_declarator (&decl_specifiers,
16005 declarator,
16006 default_argument);
16007 }
16008
16009 /* Parse a default argument and return it.
16010
16011 TEMPLATE_PARM_P is true if this is a default argument for a
16012 non-type template parameter. */
16013 static tree
16014 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16015 {
16016 tree default_argument = NULL_TREE;
16017 bool saved_greater_than_is_operator_p;
16018 bool saved_local_variables_forbidden_p;
16019
16020 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16021 set correctly. */
16022 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16023 parser->greater_than_is_operator_p = !template_parm_p;
16024 /* Local variable names (and the `this' keyword) may not
16025 appear in a default argument. */
16026 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16027 parser->local_variables_forbidden_p = true;
16028 /* Parse the assignment-expression. */
16029 if (template_parm_p)
16030 push_deferring_access_checks (dk_no_deferred);
16031 default_argument
16032 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16033 if (template_parm_p)
16034 pop_deferring_access_checks ();
16035 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16036 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16037
16038 return default_argument;
16039 }
16040
16041 /* Parse a function-body.
16042
16043 function-body:
16044 compound_statement */
16045
16046 static void
16047 cp_parser_function_body (cp_parser *parser)
16048 {
16049 cp_parser_compound_statement (parser, NULL, false);
16050 }
16051
16052 /* Parse a ctor-initializer-opt followed by a function-body. Return
16053 true if a ctor-initializer was present. */
16054
16055 static bool
16056 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16057 {
16058 tree body;
16059 bool ctor_initializer_p;
16060
16061 /* Begin the function body. */
16062 body = begin_function_body ();
16063 /* Parse the optional ctor-initializer. */
16064 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16065 /* Parse the function-body. */
16066 cp_parser_function_body (parser);
16067 /* Finish the function body. */
16068 finish_function_body (body);
16069
16070 return ctor_initializer_p;
16071 }
16072
16073 /* Parse an initializer.
16074
16075 initializer:
16076 = initializer-clause
16077 ( expression-list )
16078
16079 Returns an expression representing the initializer. If no
16080 initializer is present, NULL_TREE is returned.
16081
16082 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16083 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16084 set to TRUE if there is no initializer present. If there is an
16085 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16086 is set to true; otherwise it is set to false. */
16087
16088 static tree
16089 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16090 bool* non_constant_p)
16091 {
16092 cp_token *token;
16093 tree init;
16094
16095 /* Peek at the next token. */
16096 token = cp_lexer_peek_token (parser->lexer);
16097
16098 /* Let our caller know whether or not this initializer was
16099 parenthesized. */
16100 *is_direct_init = (token->type != CPP_EQ);
16101 /* Assume that the initializer is constant. */
16102 *non_constant_p = false;
16103
16104 if (token->type == CPP_EQ)
16105 {
16106 /* Consume the `='. */
16107 cp_lexer_consume_token (parser->lexer);
16108 /* Parse the initializer-clause. */
16109 init = cp_parser_initializer_clause (parser, non_constant_p);
16110 }
16111 else if (token->type == CPP_OPEN_PAREN)
16112 {
16113 VEC(tree,gc) *vec;
16114 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16115 /*cast_p=*/false,
16116 /*allow_expansion_p=*/true,
16117 non_constant_p);
16118 if (vec == NULL)
16119 return error_mark_node;
16120 init = build_tree_list_vec (vec);
16121 release_tree_vector (vec);
16122 }
16123 else if (token->type == CPP_OPEN_BRACE)
16124 {
16125 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16126 init = cp_parser_braced_list (parser, non_constant_p);
16127 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16128 }
16129 else
16130 {
16131 /* Anything else is an error. */
16132 cp_parser_error (parser, "expected initializer");
16133 init = error_mark_node;
16134 }
16135
16136 return init;
16137 }
16138
16139 /* Parse an initializer-clause.
16140
16141 initializer-clause:
16142 assignment-expression
16143 braced-init-list
16144
16145 Returns an expression representing the initializer.
16146
16147 If the `assignment-expression' production is used the value
16148 returned is simply a representation for the expression.
16149
16150 Otherwise, calls cp_parser_braced_list. */
16151
16152 static tree
16153 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16154 {
16155 tree initializer;
16156
16157 /* Assume the expression is constant. */
16158 *non_constant_p = false;
16159
16160 /* If it is not a `{', then we are looking at an
16161 assignment-expression. */
16162 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16163 {
16164 initializer
16165 = cp_parser_constant_expression (parser,
16166 /*allow_non_constant_p=*/true,
16167 non_constant_p);
16168 if (!*non_constant_p)
16169 initializer = fold_non_dependent_expr (initializer);
16170 }
16171 else
16172 initializer = cp_parser_braced_list (parser, non_constant_p);
16173
16174 return initializer;
16175 }
16176
16177 /* Parse a brace-enclosed initializer list.
16178
16179 braced-init-list:
16180 { initializer-list , [opt] }
16181 { }
16182
16183 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16184 the elements of the initializer-list (or NULL, if the last
16185 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16186 NULL_TREE. There is no way to detect whether or not the optional
16187 trailing `,' was provided. NON_CONSTANT_P is as for
16188 cp_parser_initializer. */
16189
16190 static tree
16191 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16192 {
16193 tree initializer;
16194
16195 /* Consume the `{' token. */
16196 cp_lexer_consume_token (parser->lexer);
16197 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16198 initializer = make_node (CONSTRUCTOR);
16199 /* If it's not a `}', then there is a non-trivial initializer. */
16200 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16201 {
16202 /* Parse the initializer list. */
16203 CONSTRUCTOR_ELTS (initializer)
16204 = cp_parser_initializer_list (parser, non_constant_p);
16205 /* A trailing `,' token is allowed. */
16206 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16207 cp_lexer_consume_token (parser->lexer);
16208 }
16209 /* Now, there should be a trailing `}'. */
16210 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16211 TREE_TYPE (initializer) = init_list_type_node;
16212 return initializer;
16213 }
16214
16215 /* Parse an initializer-list.
16216
16217 initializer-list:
16218 initializer-clause ... [opt]
16219 initializer-list , initializer-clause ... [opt]
16220
16221 GNU Extension:
16222
16223 initializer-list:
16224 identifier : initializer-clause
16225 initializer-list, identifier : initializer-clause
16226
16227 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16228 for the initializer. If the INDEX of the elt is non-NULL, it is the
16229 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16230 as for cp_parser_initializer. */
16231
16232 static VEC(constructor_elt,gc) *
16233 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16234 {
16235 VEC(constructor_elt,gc) *v = NULL;
16236
16237 /* Assume all of the expressions are constant. */
16238 *non_constant_p = false;
16239
16240 /* Parse the rest of the list. */
16241 while (true)
16242 {
16243 cp_token *token;
16244 tree identifier;
16245 tree initializer;
16246 bool clause_non_constant_p;
16247
16248 /* If the next token is an identifier and the following one is a
16249 colon, we are looking at the GNU designated-initializer
16250 syntax. */
16251 if (cp_parser_allow_gnu_extensions_p (parser)
16252 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16253 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16254 {
16255 /* Warn the user that they are using an extension. */
16256 pedwarn (input_location, OPT_pedantic,
16257 "ISO C++ does not allow designated initializers");
16258 /* Consume the identifier. */
16259 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16260 /* Consume the `:'. */
16261 cp_lexer_consume_token (parser->lexer);
16262 }
16263 else
16264 identifier = NULL_TREE;
16265
16266 /* Parse the initializer. */
16267 initializer = cp_parser_initializer_clause (parser,
16268 &clause_non_constant_p);
16269 /* If any clause is non-constant, so is the entire initializer. */
16270 if (clause_non_constant_p)
16271 *non_constant_p = true;
16272
16273 /* If we have an ellipsis, this is an initializer pack
16274 expansion. */
16275 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16276 {
16277 /* Consume the `...'. */
16278 cp_lexer_consume_token (parser->lexer);
16279
16280 /* Turn the initializer into an initializer expansion. */
16281 initializer = make_pack_expansion (initializer);
16282 }
16283
16284 /* Add it to the vector. */
16285 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16286
16287 /* If the next token is not a comma, we have reached the end of
16288 the list. */
16289 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16290 break;
16291
16292 /* Peek at the next token. */
16293 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16294 /* If the next token is a `}', then we're still done. An
16295 initializer-clause can have a trailing `,' after the
16296 initializer-list and before the closing `}'. */
16297 if (token->type == CPP_CLOSE_BRACE)
16298 break;
16299
16300 /* Consume the `,' token. */
16301 cp_lexer_consume_token (parser->lexer);
16302 }
16303
16304 return v;
16305 }
16306
16307 /* Classes [gram.class] */
16308
16309 /* Parse a class-name.
16310
16311 class-name:
16312 identifier
16313 template-id
16314
16315 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16316 to indicate that names looked up in dependent types should be
16317 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16318 keyword has been used to indicate that the name that appears next
16319 is a template. TAG_TYPE indicates the explicit tag given before
16320 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16321 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16322 is the class being defined in a class-head.
16323
16324 Returns the TYPE_DECL representing the class. */
16325
16326 static tree
16327 cp_parser_class_name (cp_parser *parser,
16328 bool typename_keyword_p,
16329 bool template_keyword_p,
16330 enum tag_types tag_type,
16331 bool check_dependency_p,
16332 bool class_head_p,
16333 bool is_declaration)
16334 {
16335 tree decl;
16336 tree scope;
16337 bool typename_p;
16338 cp_token *token;
16339 tree identifier = NULL_TREE;
16340
16341 /* All class-names start with an identifier. */
16342 token = cp_lexer_peek_token (parser->lexer);
16343 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16344 {
16345 cp_parser_error (parser, "expected class-name");
16346 return error_mark_node;
16347 }
16348
16349 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16350 to a template-id, so we save it here. */
16351 scope = parser->scope;
16352 if (scope == error_mark_node)
16353 return error_mark_node;
16354
16355 /* Any name names a type if we're following the `typename' keyword
16356 in a qualified name where the enclosing scope is type-dependent. */
16357 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16358 && dependent_type_p (scope));
16359 /* Handle the common case (an identifier, but not a template-id)
16360 efficiently. */
16361 if (token->type == CPP_NAME
16362 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16363 {
16364 cp_token *identifier_token;
16365 bool ambiguous_p;
16366
16367 /* Look for the identifier. */
16368 identifier_token = cp_lexer_peek_token (parser->lexer);
16369 ambiguous_p = identifier_token->ambiguous_p;
16370 identifier = cp_parser_identifier (parser);
16371 /* If the next token isn't an identifier, we are certainly not
16372 looking at a class-name. */
16373 if (identifier == error_mark_node)
16374 decl = error_mark_node;
16375 /* If we know this is a type-name, there's no need to look it
16376 up. */
16377 else if (typename_p)
16378 decl = identifier;
16379 else
16380 {
16381 tree ambiguous_decls;
16382 /* If we already know that this lookup is ambiguous, then
16383 we've already issued an error message; there's no reason
16384 to check again. */
16385 if (ambiguous_p)
16386 {
16387 cp_parser_simulate_error (parser);
16388 return error_mark_node;
16389 }
16390 /* If the next token is a `::', then the name must be a type
16391 name.
16392
16393 [basic.lookup.qual]
16394
16395 During the lookup for a name preceding the :: scope
16396 resolution operator, object, function, and enumerator
16397 names are ignored. */
16398 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16399 tag_type = typename_type;
16400 /* Look up the name. */
16401 decl = cp_parser_lookup_name (parser, identifier,
16402 tag_type,
16403 /*is_template=*/false,
16404 /*is_namespace=*/false,
16405 check_dependency_p,
16406 &ambiguous_decls,
16407 identifier_token->location);
16408 if (ambiguous_decls)
16409 {
16410 if (cp_parser_parsing_tentatively (parser))
16411 cp_parser_simulate_error (parser);
16412 return error_mark_node;
16413 }
16414 }
16415 }
16416 else
16417 {
16418 /* Try a template-id. */
16419 decl = cp_parser_template_id (parser, template_keyword_p,
16420 check_dependency_p,
16421 is_declaration);
16422 if (decl == error_mark_node)
16423 return error_mark_node;
16424 }
16425
16426 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16427
16428 /* If this is a typename, create a TYPENAME_TYPE. */
16429 if (typename_p && decl != error_mark_node)
16430 {
16431 decl = make_typename_type (scope, decl, typename_type,
16432 /*complain=*/tf_error);
16433 if (decl != error_mark_node)
16434 decl = TYPE_NAME (decl);
16435 }
16436
16437 /* Check to see that it is really the name of a class. */
16438 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16439 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16440 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16441 /* Situations like this:
16442
16443 template <typename T> struct A {
16444 typename T::template X<int>::I i;
16445 };
16446
16447 are problematic. Is `T::template X<int>' a class-name? The
16448 standard does not seem to be definitive, but there is no other
16449 valid interpretation of the following `::'. Therefore, those
16450 names are considered class-names. */
16451 {
16452 decl = make_typename_type (scope, decl, tag_type, tf_error);
16453 if (decl != error_mark_node)
16454 decl = TYPE_NAME (decl);
16455 }
16456 else if (TREE_CODE (decl) != TYPE_DECL
16457 || TREE_TYPE (decl) == error_mark_node
16458 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
16459 decl = error_mark_node;
16460
16461 if (decl == error_mark_node)
16462 cp_parser_error (parser, "expected class-name");
16463 else if (identifier && !parser->scope)
16464 maybe_note_name_used_in_class (identifier, decl);
16465
16466 return decl;
16467 }
16468
16469 /* Parse a class-specifier.
16470
16471 class-specifier:
16472 class-head { member-specification [opt] }
16473
16474 Returns the TREE_TYPE representing the class. */
16475
16476 static tree
16477 cp_parser_class_specifier (cp_parser* parser)
16478 {
16479 tree type;
16480 tree attributes = NULL_TREE;
16481 bool nested_name_specifier_p;
16482 unsigned saved_num_template_parameter_lists;
16483 bool saved_in_function_body;
16484 bool saved_in_unbraced_linkage_specification_p;
16485 tree old_scope = NULL_TREE;
16486 tree scope = NULL_TREE;
16487 tree bases;
16488
16489 push_deferring_access_checks (dk_no_deferred);
16490
16491 /* Parse the class-head. */
16492 type = cp_parser_class_head (parser,
16493 &nested_name_specifier_p,
16494 &attributes,
16495 &bases);
16496 /* If the class-head was a semantic disaster, skip the entire body
16497 of the class. */
16498 if (!type)
16499 {
16500 cp_parser_skip_to_end_of_block_or_statement (parser);
16501 pop_deferring_access_checks ();
16502 return error_mark_node;
16503 }
16504
16505 /* Look for the `{'. */
16506 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16507 {
16508 pop_deferring_access_checks ();
16509 return error_mark_node;
16510 }
16511
16512 /* Process the base classes. If they're invalid, skip the
16513 entire class body. */
16514 if (!xref_basetypes (type, bases))
16515 {
16516 /* Consuming the closing brace yields better error messages
16517 later on. */
16518 if (cp_parser_skip_to_closing_brace (parser))
16519 cp_lexer_consume_token (parser->lexer);
16520 pop_deferring_access_checks ();
16521 return error_mark_node;
16522 }
16523
16524 /* Issue an error message if type-definitions are forbidden here. */
16525 cp_parser_check_type_definition (parser);
16526 /* Remember that we are defining one more class. */
16527 ++parser->num_classes_being_defined;
16528 /* Inside the class, surrounding template-parameter-lists do not
16529 apply. */
16530 saved_num_template_parameter_lists
16531 = parser->num_template_parameter_lists;
16532 parser->num_template_parameter_lists = 0;
16533 /* We are not in a function body. */
16534 saved_in_function_body = parser->in_function_body;
16535 parser->in_function_body = false;
16536 /* We are not immediately inside an extern "lang" block. */
16537 saved_in_unbraced_linkage_specification_p
16538 = parser->in_unbraced_linkage_specification_p;
16539 parser->in_unbraced_linkage_specification_p = false;
16540
16541 /* Start the class. */
16542 if (nested_name_specifier_p)
16543 {
16544 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16545 old_scope = push_inner_scope (scope);
16546 }
16547 type = begin_class_definition (type, attributes);
16548
16549 if (type == error_mark_node)
16550 /* If the type is erroneous, skip the entire body of the class. */
16551 cp_parser_skip_to_closing_brace (parser);
16552 else
16553 /* Parse the member-specification. */
16554 cp_parser_member_specification_opt (parser);
16555
16556 /* Look for the trailing `}'. */
16557 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16558 /* Look for trailing attributes to apply to this class. */
16559 if (cp_parser_allow_gnu_extensions_p (parser))
16560 attributes = cp_parser_attributes_opt (parser);
16561 if (type != error_mark_node)
16562 type = finish_struct (type, attributes);
16563 if (nested_name_specifier_p)
16564 pop_inner_scope (old_scope, scope);
16565 /* If this class is not itself within the scope of another class,
16566 then we need to parse the bodies of all of the queued function
16567 definitions. Note that the queued functions defined in a class
16568 are not always processed immediately following the
16569 class-specifier for that class. Consider:
16570
16571 struct A {
16572 struct B { void f() { sizeof (A); } };
16573 };
16574
16575 If `f' were processed before the processing of `A' were
16576 completed, there would be no way to compute the size of `A'.
16577 Note that the nesting we are interested in here is lexical --
16578 not the semantic nesting given by TYPE_CONTEXT. In particular,
16579 for:
16580
16581 struct A { struct B; };
16582 struct A::B { void f() { } };
16583
16584 there is no need to delay the parsing of `A::B::f'. */
16585 if (--parser->num_classes_being_defined == 0)
16586 {
16587 tree fn;
16588 tree class_type = NULL_TREE;
16589 tree pushed_scope = NULL_TREE;
16590 unsigned ix;
16591 cp_default_arg_entry *e;
16592
16593 /* In a first pass, parse default arguments to the functions.
16594 Then, in a second pass, parse the bodies of the functions.
16595 This two-phased approach handles cases like:
16596
16597 struct S {
16598 void f() { g(); }
16599 void g(int i = 3);
16600 };
16601
16602 */
16603 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
16604 ix, e)
16605 {
16606 fn = e->decl;
16607 /* If there are default arguments that have not yet been processed,
16608 take care of them now. */
16609 if (class_type != e->class_type)
16610 {
16611 if (pushed_scope)
16612 pop_scope (pushed_scope);
16613 class_type = e->class_type;
16614 pushed_scope = push_scope (class_type);
16615 }
16616 /* Make sure that any template parameters are in scope. */
16617 maybe_begin_member_template_processing (fn);
16618 /* Parse the default argument expressions. */
16619 cp_parser_late_parsing_default_args (parser, fn);
16620 /* Remove any template parameters from the symbol table. */
16621 maybe_end_member_template_processing ();
16622 }
16623 if (pushed_scope)
16624 pop_scope (pushed_scope);
16625 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
16626 /* Now parse the body of the functions. */
16627 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
16628 cp_parser_late_parsing_for_member (parser, fn);
16629 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
16630 }
16631
16632 /* Put back any saved access checks. */
16633 pop_deferring_access_checks ();
16634
16635 /* Restore saved state. */
16636 parser->in_function_body = saved_in_function_body;
16637 parser->num_template_parameter_lists
16638 = saved_num_template_parameter_lists;
16639 parser->in_unbraced_linkage_specification_p
16640 = saved_in_unbraced_linkage_specification_p;
16641
16642 return type;
16643 }
16644
16645 /* Parse a class-head.
16646
16647 class-head:
16648 class-key identifier [opt] base-clause [opt]
16649 class-key nested-name-specifier identifier base-clause [opt]
16650 class-key nested-name-specifier [opt] template-id
16651 base-clause [opt]
16652
16653 GNU Extensions:
16654 class-key attributes identifier [opt] base-clause [opt]
16655 class-key attributes nested-name-specifier identifier base-clause [opt]
16656 class-key attributes nested-name-specifier [opt] template-id
16657 base-clause [opt]
16658
16659 Upon return BASES is initialized to the list of base classes (or
16660 NULL, if there are none) in the same form returned by
16661 cp_parser_base_clause.
16662
16663 Returns the TYPE of the indicated class. Sets
16664 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16665 involving a nested-name-specifier was used, and FALSE otherwise.
16666
16667 Returns error_mark_node if this is not a class-head.
16668
16669 Returns NULL_TREE if the class-head is syntactically valid, but
16670 semantically invalid in a way that means we should skip the entire
16671 body of the class. */
16672
16673 static tree
16674 cp_parser_class_head (cp_parser* parser,
16675 bool* nested_name_specifier_p,
16676 tree *attributes_p,
16677 tree *bases)
16678 {
16679 tree nested_name_specifier;
16680 enum tag_types class_key;
16681 tree id = NULL_TREE;
16682 tree type = NULL_TREE;
16683 tree attributes;
16684 bool template_id_p = false;
16685 bool qualified_p = false;
16686 bool invalid_nested_name_p = false;
16687 bool invalid_explicit_specialization_p = false;
16688 tree pushed_scope = NULL_TREE;
16689 unsigned num_templates;
16690 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16691 /* Assume no nested-name-specifier will be present. */
16692 *nested_name_specifier_p = false;
16693 /* Assume no template parameter lists will be used in defining the
16694 type. */
16695 num_templates = 0;
16696
16697 *bases = NULL_TREE;
16698
16699 /* Look for the class-key. */
16700 class_key = cp_parser_class_key (parser);
16701 if (class_key == none_type)
16702 return error_mark_node;
16703
16704 /* Parse the attributes. */
16705 attributes = cp_parser_attributes_opt (parser);
16706
16707 /* If the next token is `::', that is invalid -- but sometimes
16708 people do try to write:
16709
16710 struct ::S {};
16711
16712 Handle this gracefully by accepting the extra qualifier, and then
16713 issuing an error about it later if this really is a
16714 class-head. If it turns out just to be an elaborated type
16715 specifier, remain silent. */
16716 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16717 qualified_p = true;
16718
16719 push_deferring_access_checks (dk_no_check);
16720
16721 /* Determine the name of the class. Begin by looking for an
16722 optional nested-name-specifier. */
16723 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16724 nested_name_specifier
16725 = cp_parser_nested_name_specifier_opt (parser,
16726 /*typename_keyword_p=*/false,
16727 /*check_dependency_p=*/false,
16728 /*type_p=*/false,
16729 /*is_declaration=*/false);
16730 /* If there was a nested-name-specifier, then there *must* be an
16731 identifier. */
16732 if (nested_name_specifier)
16733 {
16734 type_start_token = cp_lexer_peek_token (parser->lexer);
16735 /* Although the grammar says `identifier', it really means
16736 `class-name' or `template-name'. You are only allowed to
16737 define a class that has already been declared with this
16738 syntax.
16739
16740 The proposed resolution for Core Issue 180 says that wherever
16741 you see `class T::X' you should treat `X' as a type-name.
16742
16743 It is OK to define an inaccessible class; for example:
16744
16745 class A { class B; };
16746 class A::B {};
16747
16748 We do not know if we will see a class-name, or a
16749 template-name. We look for a class-name first, in case the
16750 class-name is a template-id; if we looked for the
16751 template-name first we would stop after the template-name. */
16752 cp_parser_parse_tentatively (parser);
16753 type = cp_parser_class_name (parser,
16754 /*typename_keyword_p=*/false,
16755 /*template_keyword_p=*/false,
16756 class_type,
16757 /*check_dependency_p=*/false,
16758 /*class_head_p=*/true,
16759 /*is_declaration=*/false);
16760 /* If that didn't work, ignore the nested-name-specifier. */
16761 if (!cp_parser_parse_definitely (parser))
16762 {
16763 invalid_nested_name_p = true;
16764 type_start_token = cp_lexer_peek_token (parser->lexer);
16765 id = cp_parser_identifier (parser);
16766 if (id == error_mark_node)
16767 id = NULL_TREE;
16768 }
16769 /* If we could not find a corresponding TYPE, treat this
16770 declaration like an unqualified declaration. */
16771 if (type == error_mark_node)
16772 nested_name_specifier = NULL_TREE;
16773 /* Otherwise, count the number of templates used in TYPE and its
16774 containing scopes. */
16775 else
16776 {
16777 tree scope;
16778
16779 for (scope = TREE_TYPE (type);
16780 scope && TREE_CODE (scope) != NAMESPACE_DECL;
16781 scope = (TYPE_P (scope)
16782 ? TYPE_CONTEXT (scope)
16783 : DECL_CONTEXT (scope)))
16784 if (TYPE_P (scope)
16785 && CLASS_TYPE_P (scope)
16786 && CLASSTYPE_TEMPLATE_INFO (scope)
16787 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16788 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16789 ++num_templates;
16790 }
16791 }
16792 /* Otherwise, the identifier is optional. */
16793 else
16794 {
16795 /* We don't know whether what comes next is a template-id,
16796 an identifier, or nothing at all. */
16797 cp_parser_parse_tentatively (parser);
16798 /* Check for a template-id. */
16799 type_start_token = cp_lexer_peek_token (parser->lexer);
16800 id = cp_parser_template_id (parser,
16801 /*template_keyword_p=*/false,
16802 /*check_dependency_p=*/true,
16803 /*is_declaration=*/true);
16804 /* If that didn't work, it could still be an identifier. */
16805 if (!cp_parser_parse_definitely (parser))
16806 {
16807 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16808 {
16809 type_start_token = cp_lexer_peek_token (parser->lexer);
16810 id = cp_parser_identifier (parser);
16811 }
16812 else
16813 id = NULL_TREE;
16814 }
16815 else
16816 {
16817 template_id_p = true;
16818 ++num_templates;
16819 }
16820 }
16821
16822 pop_deferring_access_checks ();
16823
16824 if (id)
16825 cp_parser_check_for_invalid_template_id (parser, id,
16826 type_start_token->location);
16827
16828 /* If it's not a `:' or a `{' then we can't really be looking at a
16829 class-head, since a class-head only appears as part of a
16830 class-specifier. We have to detect this situation before calling
16831 xref_tag, since that has irreversible side-effects. */
16832 if (!cp_parser_next_token_starts_class_definition_p (parser))
16833 {
16834 cp_parser_error (parser, "expected %<{%> or %<:%>");
16835 return error_mark_node;
16836 }
16837
16838 /* At this point, we're going ahead with the class-specifier, even
16839 if some other problem occurs. */
16840 cp_parser_commit_to_tentative_parse (parser);
16841 /* Issue the error about the overly-qualified name now. */
16842 if (qualified_p)
16843 {
16844 cp_parser_error (parser,
16845 "global qualification of class name is invalid");
16846 return error_mark_node;
16847 }
16848 else if (invalid_nested_name_p)
16849 {
16850 cp_parser_error (parser,
16851 "qualified name does not name a class");
16852 return error_mark_node;
16853 }
16854 else if (nested_name_specifier)
16855 {
16856 tree scope;
16857
16858 /* Reject typedef-names in class heads. */
16859 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16860 {
16861 error_at (type_start_token->location,
16862 "invalid class name in declaration of %qD",
16863 type);
16864 type = NULL_TREE;
16865 goto done;
16866 }
16867
16868 /* Figure out in what scope the declaration is being placed. */
16869 scope = current_scope ();
16870 /* If that scope does not contain the scope in which the
16871 class was originally declared, the program is invalid. */
16872 if (scope && !is_ancestor (scope, nested_name_specifier))
16873 {
16874 if (at_namespace_scope_p ())
16875 error_at (type_start_token->location,
16876 "declaration of %qD in namespace %qD which does not "
16877 "enclose %qD",
16878 type, scope, nested_name_specifier);
16879 else
16880 error_at (type_start_token->location,
16881 "declaration of %qD in %qD which does not enclose %qD",
16882 type, scope, nested_name_specifier);
16883 type = NULL_TREE;
16884 goto done;
16885 }
16886 /* [dcl.meaning]
16887
16888 A declarator-id shall not be qualified except for the
16889 definition of a ... nested class outside of its class
16890 ... [or] the definition or explicit instantiation of a
16891 class member of a namespace outside of its namespace. */
16892 if (scope == nested_name_specifier)
16893 {
16894 permerror (nested_name_specifier_token_start->location,
16895 "extra qualification not allowed");
16896 nested_name_specifier = NULL_TREE;
16897 num_templates = 0;
16898 }
16899 }
16900 /* An explicit-specialization must be preceded by "template <>". If
16901 it is not, try to recover gracefully. */
16902 if (at_namespace_scope_p ()
16903 && parser->num_template_parameter_lists == 0
16904 && template_id_p)
16905 {
16906 error_at (type_start_token->location,
16907 "an explicit specialization must be preceded by %<template <>%>");
16908 invalid_explicit_specialization_p = true;
16909 /* Take the same action that would have been taken by
16910 cp_parser_explicit_specialization. */
16911 ++parser->num_template_parameter_lists;
16912 begin_specialization ();
16913 }
16914 /* There must be no "return" statements between this point and the
16915 end of this function; set "type "to the correct return value and
16916 use "goto done;" to return. */
16917 /* Make sure that the right number of template parameters were
16918 present. */
16919 if (!cp_parser_check_template_parameters (parser, num_templates,
16920 type_start_token->location,
16921 /*declarator=*/NULL))
16922 {
16923 /* If something went wrong, there is no point in even trying to
16924 process the class-definition. */
16925 type = NULL_TREE;
16926 goto done;
16927 }
16928
16929 /* Look up the type. */
16930 if (template_id_p)
16931 {
16932 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16933 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16934 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16935 {
16936 error_at (type_start_token->location,
16937 "function template %qD redeclared as a class template", id);
16938 type = error_mark_node;
16939 }
16940 else
16941 {
16942 type = TREE_TYPE (id);
16943 type = maybe_process_partial_specialization (type);
16944 }
16945 if (nested_name_specifier)
16946 pushed_scope = push_scope (nested_name_specifier);
16947 }
16948 else if (nested_name_specifier)
16949 {
16950 tree class_type;
16951
16952 /* Given:
16953
16954 template <typename T> struct S { struct T };
16955 template <typename T> struct S<T>::T { };
16956
16957 we will get a TYPENAME_TYPE when processing the definition of
16958 `S::T'. We need to resolve it to the actual type before we
16959 try to define it. */
16960 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16961 {
16962 class_type = resolve_typename_type (TREE_TYPE (type),
16963 /*only_current_p=*/false);
16964 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16965 type = TYPE_NAME (class_type);
16966 else
16967 {
16968 cp_parser_error (parser, "could not resolve typename type");
16969 type = error_mark_node;
16970 }
16971 }
16972
16973 if (maybe_process_partial_specialization (TREE_TYPE (type))
16974 == error_mark_node)
16975 {
16976 type = NULL_TREE;
16977 goto done;
16978 }
16979
16980 class_type = current_class_type;
16981 /* Enter the scope indicated by the nested-name-specifier. */
16982 pushed_scope = push_scope (nested_name_specifier);
16983 /* Get the canonical version of this type. */
16984 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16985 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16986 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16987 {
16988 type = push_template_decl (type);
16989 if (type == error_mark_node)
16990 {
16991 type = NULL_TREE;
16992 goto done;
16993 }
16994 }
16995
16996 type = TREE_TYPE (type);
16997 *nested_name_specifier_p = true;
16998 }
16999 else /* The name is not a nested name. */
17000 {
17001 /* If the class was unnamed, create a dummy name. */
17002 if (!id)
17003 id = make_anon_name ();
17004 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17005 parser->num_template_parameter_lists);
17006 }
17007
17008 /* Indicate whether this class was declared as a `class' or as a
17009 `struct'. */
17010 if (TREE_CODE (type) == RECORD_TYPE)
17011 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17012 cp_parser_check_class_key (class_key, type);
17013
17014 /* If this type was already complete, and we see another definition,
17015 that's an error. */
17016 if (type != error_mark_node && COMPLETE_TYPE_P (type))
17017 {
17018 error_at (type_start_token->location, "redefinition of %q#T",
17019 type);
17020 error_at (type_start_token->location, "previous definition of %q+#T",
17021 type);
17022 type = NULL_TREE;
17023 goto done;
17024 }
17025 else if (type == error_mark_node)
17026 type = NULL_TREE;
17027
17028 /* We will have entered the scope containing the class; the names of
17029 base classes should be looked up in that context. For example:
17030
17031 struct A { struct B {}; struct C; };
17032 struct A::C : B {};
17033
17034 is valid. */
17035
17036 /* Get the list of base-classes, if there is one. */
17037 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17038 *bases = cp_parser_base_clause (parser);
17039
17040 done:
17041 /* Leave the scope given by the nested-name-specifier. We will
17042 enter the class scope itself while processing the members. */
17043 if (pushed_scope)
17044 pop_scope (pushed_scope);
17045
17046 if (invalid_explicit_specialization_p)
17047 {
17048 end_specialization ();
17049 --parser->num_template_parameter_lists;
17050 }
17051
17052 if (type)
17053 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17054 *attributes_p = attributes;
17055 return type;
17056 }
17057
17058 /* Parse a class-key.
17059
17060 class-key:
17061 class
17062 struct
17063 union
17064
17065 Returns the kind of class-key specified, or none_type to indicate
17066 error. */
17067
17068 static enum tag_types
17069 cp_parser_class_key (cp_parser* parser)
17070 {
17071 cp_token *token;
17072 enum tag_types tag_type;
17073
17074 /* Look for the class-key. */
17075 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17076 if (!token)
17077 return none_type;
17078
17079 /* Check to see if the TOKEN is a class-key. */
17080 tag_type = cp_parser_token_is_class_key (token);
17081 if (!tag_type)
17082 cp_parser_error (parser, "expected class-key");
17083 return tag_type;
17084 }
17085
17086 /* Parse an (optional) member-specification.
17087
17088 member-specification:
17089 member-declaration member-specification [opt]
17090 access-specifier : member-specification [opt] */
17091
17092 static void
17093 cp_parser_member_specification_opt (cp_parser* parser)
17094 {
17095 while (true)
17096 {
17097 cp_token *token;
17098 enum rid keyword;
17099
17100 /* Peek at the next token. */
17101 token = cp_lexer_peek_token (parser->lexer);
17102 /* If it's a `}', or EOF then we've seen all the members. */
17103 if (token->type == CPP_CLOSE_BRACE
17104 || token->type == CPP_EOF
17105 || token->type == CPP_PRAGMA_EOL)
17106 break;
17107
17108 /* See if this token is a keyword. */
17109 keyword = token->keyword;
17110 switch (keyword)
17111 {
17112 case RID_PUBLIC:
17113 case RID_PROTECTED:
17114 case RID_PRIVATE:
17115 /* Consume the access-specifier. */
17116 cp_lexer_consume_token (parser->lexer);
17117 /* Remember which access-specifier is active. */
17118 current_access_specifier = token->u.value;
17119 /* Look for the `:'. */
17120 cp_parser_require (parser, CPP_COLON, RT_COLON);
17121 break;
17122
17123 default:
17124 /* Accept #pragmas at class scope. */
17125 if (token->type == CPP_PRAGMA)
17126 {
17127 cp_parser_pragma (parser, pragma_external);
17128 break;
17129 }
17130
17131 /* Otherwise, the next construction must be a
17132 member-declaration. */
17133 cp_parser_member_declaration (parser);
17134 }
17135 }
17136 }
17137
17138 /* Parse a member-declaration.
17139
17140 member-declaration:
17141 decl-specifier-seq [opt] member-declarator-list [opt] ;
17142 function-definition ; [opt]
17143 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17144 using-declaration
17145 template-declaration
17146
17147 member-declarator-list:
17148 member-declarator
17149 member-declarator-list , member-declarator
17150
17151 member-declarator:
17152 declarator pure-specifier [opt]
17153 declarator constant-initializer [opt]
17154 identifier [opt] : constant-expression
17155
17156 GNU Extensions:
17157
17158 member-declaration:
17159 __extension__ member-declaration
17160
17161 member-declarator:
17162 declarator attributes [opt] pure-specifier [opt]
17163 declarator attributes [opt] constant-initializer [opt]
17164 identifier [opt] attributes [opt] : constant-expression
17165
17166 C++0x Extensions:
17167
17168 member-declaration:
17169 static_assert-declaration */
17170
17171 static void
17172 cp_parser_member_declaration (cp_parser* parser)
17173 {
17174 cp_decl_specifier_seq decl_specifiers;
17175 tree prefix_attributes;
17176 tree decl;
17177 int declares_class_or_enum;
17178 bool friend_p;
17179 cp_token *token = NULL;
17180 cp_token *decl_spec_token_start = NULL;
17181 cp_token *initializer_token_start = NULL;
17182 int saved_pedantic;
17183
17184 /* Check for the `__extension__' keyword. */
17185 if (cp_parser_extension_opt (parser, &saved_pedantic))
17186 {
17187 /* Recurse. */
17188 cp_parser_member_declaration (parser);
17189 /* Restore the old value of the PEDANTIC flag. */
17190 pedantic = saved_pedantic;
17191
17192 return;
17193 }
17194
17195 /* Check for a template-declaration. */
17196 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17197 {
17198 /* An explicit specialization here is an error condition, and we
17199 expect the specialization handler to detect and report this. */
17200 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17201 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17202 cp_parser_explicit_specialization (parser);
17203 else
17204 cp_parser_template_declaration (parser, /*member_p=*/true);
17205
17206 return;
17207 }
17208
17209 /* Check for a using-declaration. */
17210 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17211 {
17212 /* Parse the using-declaration. */
17213 cp_parser_using_declaration (parser,
17214 /*access_declaration_p=*/false);
17215 return;
17216 }
17217
17218 /* Check for @defs. */
17219 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17220 {
17221 tree ivar, member;
17222 tree ivar_chains = cp_parser_objc_defs_expression (parser);
17223 ivar = ivar_chains;
17224 while (ivar)
17225 {
17226 member = ivar;
17227 ivar = TREE_CHAIN (member);
17228 TREE_CHAIN (member) = NULL_TREE;
17229 finish_member_declaration (member);
17230 }
17231 return;
17232 }
17233
17234 /* If the next token is `static_assert' we have a static assertion. */
17235 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17236 {
17237 cp_parser_static_assert (parser, /*member_p=*/true);
17238 return;
17239 }
17240
17241 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17242 return;
17243
17244 /* Parse the decl-specifier-seq. */
17245 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17246 cp_parser_decl_specifier_seq (parser,
17247 CP_PARSER_FLAGS_OPTIONAL,
17248 &decl_specifiers,
17249 &declares_class_or_enum);
17250 prefix_attributes = decl_specifiers.attributes;
17251 decl_specifiers.attributes = NULL_TREE;
17252 /* Check for an invalid type-name. */
17253 if (!decl_specifiers.any_type_specifiers_p
17254 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17255 return;
17256 /* If there is no declarator, then the decl-specifier-seq should
17257 specify a type. */
17258 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17259 {
17260 /* If there was no decl-specifier-seq, and the next token is a
17261 `;', then we have something like:
17262
17263 struct S { ; };
17264
17265 [class.mem]
17266
17267 Each member-declaration shall declare at least one member
17268 name of the class. */
17269 if (!decl_specifiers.any_specifiers_p)
17270 {
17271 cp_token *token = cp_lexer_peek_token (parser->lexer);
17272 if (!in_system_header_at (token->location))
17273 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17274 }
17275 else
17276 {
17277 tree type;
17278
17279 /* See if this declaration is a friend. */
17280 friend_p = cp_parser_friend_p (&decl_specifiers);
17281 /* If there were decl-specifiers, check to see if there was
17282 a class-declaration. */
17283 type = check_tag_decl (&decl_specifiers);
17284 /* Nested classes have already been added to the class, but
17285 a `friend' needs to be explicitly registered. */
17286 if (friend_p)
17287 {
17288 /* If the `friend' keyword was present, the friend must
17289 be introduced with a class-key. */
17290 if (!declares_class_or_enum)
17291 error_at (decl_spec_token_start->location,
17292 "a class-key must be used when declaring a friend");
17293 /* In this case:
17294
17295 template <typename T> struct A {
17296 friend struct A<T>::B;
17297 };
17298
17299 A<T>::B will be represented by a TYPENAME_TYPE, and
17300 therefore not recognized by check_tag_decl. */
17301 if (!type
17302 && decl_specifiers.type
17303 && TYPE_P (decl_specifiers.type))
17304 type = decl_specifiers.type;
17305 if (!type || !TYPE_P (type))
17306 error_at (decl_spec_token_start->location,
17307 "friend declaration does not name a class or "
17308 "function");
17309 else
17310 make_friend_class (current_class_type, type,
17311 /*complain=*/true);
17312 }
17313 /* If there is no TYPE, an error message will already have
17314 been issued. */
17315 else if (!type || type == error_mark_node)
17316 ;
17317 /* An anonymous aggregate has to be handled specially; such
17318 a declaration really declares a data member (with a
17319 particular type), as opposed to a nested class. */
17320 else if (ANON_AGGR_TYPE_P (type))
17321 {
17322 /* Remove constructors and such from TYPE, now that we
17323 know it is an anonymous aggregate. */
17324 fixup_anonymous_aggr (type);
17325 /* And make the corresponding data member. */
17326 decl = build_decl (decl_spec_token_start->location,
17327 FIELD_DECL, NULL_TREE, type);
17328 /* Add it to the class. */
17329 finish_member_declaration (decl);
17330 }
17331 else
17332 cp_parser_check_access_in_redeclaration
17333 (TYPE_NAME (type),
17334 decl_spec_token_start->location);
17335 }
17336 }
17337 else
17338 {
17339 /* See if these declarations will be friends. */
17340 friend_p = cp_parser_friend_p (&decl_specifiers);
17341
17342 /* Keep going until we hit the `;' at the end of the
17343 declaration. */
17344 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17345 {
17346 tree attributes = NULL_TREE;
17347 tree first_attribute;
17348
17349 /* Peek at the next token. */
17350 token = cp_lexer_peek_token (parser->lexer);
17351
17352 /* Check for a bitfield declaration. */
17353 if (token->type == CPP_COLON
17354 || (token->type == CPP_NAME
17355 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17356 == CPP_COLON))
17357 {
17358 tree identifier;
17359 tree width;
17360
17361 /* Get the name of the bitfield. Note that we cannot just
17362 check TOKEN here because it may have been invalidated by
17363 the call to cp_lexer_peek_nth_token above. */
17364 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17365 identifier = cp_parser_identifier (parser);
17366 else
17367 identifier = NULL_TREE;
17368
17369 /* Consume the `:' token. */
17370 cp_lexer_consume_token (parser->lexer);
17371 /* Get the width of the bitfield. */
17372 width
17373 = cp_parser_constant_expression (parser,
17374 /*allow_non_constant=*/false,
17375 NULL);
17376
17377 /* Look for attributes that apply to the bitfield. */
17378 attributes = cp_parser_attributes_opt (parser);
17379 /* Remember which attributes are prefix attributes and
17380 which are not. */
17381 first_attribute = attributes;
17382 /* Combine the attributes. */
17383 attributes = chainon (prefix_attributes, attributes);
17384
17385 /* Create the bitfield declaration. */
17386 decl = grokbitfield (identifier
17387 ? make_id_declarator (NULL_TREE,
17388 identifier,
17389 sfk_none)
17390 : NULL,
17391 &decl_specifiers,
17392 width,
17393 attributes);
17394 }
17395 else
17396 {
17397 cp_declarator *declarator;
17398 tree initializer;
17399 tree asm_specification;
17400 int ctor_dtor_or_conv_p;
17401
17402 /* Parse the declarator. */
17403 declarator
17404 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17405 &ctor_dtor_or_conv_p,
17406 /*parenthesized_p=*/NULL,
17407 /*member_p=*/true);
17408
17409 /* If something went wrong parsing the declarator, make sure
17410 that we at least consume some tokens. */
17411 if (declarator == cp_error_declarator)
17412 {
17413 /* Skip to the end of the statement. */
17414 cp_parser_skip_to_end_of_statement (parser);
17415 /* If the next token is not a semicolon, that is
17416 probably because we just skipped over the body of
17417 a function. So, we consume a semicolon if
17418 present, but do not issue an error message if it
17419 is not present. */
17420 if (cp_lexer_next_token_is (parser->lexer,
17421 CPP_SEMICOLON))
17422 cp_lexer_consume_token (parser->lexer);
17423 return;
17424 }
17425
17426 if (declares_class_or_enum & 2)
17427 cp_parser_check_for_definition_in_return_type
17428 (declarator, decl_specifiers.type,
17429 decl_specifiers.type_location);
17430
17431 /* Look for an asm-specification. */
17432 asm_specification = cp_parser_asm_specification_opt (parser);
17433 /* Look for attributes that apply to the declaration. */
17434 attributes = cp_parser_attributes_opt (parser);
17435 /* Remember which attributes are prefix attributes and
17436 which are not. */
17437 first_attribute = attributes;
17438 /* Combine the attributes. */
17439 attributes = chainon (prefix_attributes, attributes);
17440
17441 /* If it's an `=', then we have a constant-initializer or a
17442 pure-specifier. It is not correct to parse the
17443 initializer before registering the member declaration
17444 since the member declaration should be in scope while
17445 its initializer is processed. However, the rest of the
17446 front end does not yet provide an interface that allows
17447 us to handle this correctly. */
17448 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17449 {
17450 /* In [class.mem]:
17451
17452 A pure-specifier shall be used only in the declaration of
17453 a virtual function.
17454
17455 A member-declarator can contain a constant-initializer
17456 only if it declares a static member of integral or
17457 enumeration type.
17458
17459 Therefore, if the DECLARATOR is for a function, we look
17460 for a pure-specifier; otherwise, we look for a
17461 constant-initializer. When we call `grokfield', it will
17462 perform more stringent semantics checks. */
17463 initializer_token_start = cp_lexer_peek_token (parser->lexer);
17464 if (function_declarator_p (declarator))
17465 initializer = cp_parser_pure_specifier (parser);
17466 else
17467 /* Parse the initializer. */
17468 initializer = cp_parser_constant_initializer (parser);
17469 }
17470 /* Otherwise, there is no initializer. */
17471 else
17472 initializer = NULL_TREE;
17473
17474 /* See if we are probably looking at a function
17475 definition. We are certainly not looking at a
17476 member-declarator. Calling `grokfield' has
17477 side-effects, so we must not do it unless we are sure
17478 that we are looking at a member-declarator. */
17479 if (cp_parser_token_starts_function_definition_p
17480 (cp_lexer_peek_token (parser->lexer)))
17481 {
17482 /* The grammar does not allow a pure-specifier to be
17483 used when a member function is defined. (It is
17484 possible that this fact is an oversight in the
17485 standard, since a pure function may be defined
17486 outside of the class-specifier. */
17487 if (initializer)
17488 error_at (initializer_token_start->location,
17489 "pure-specifier on function-definition");
17490 decl = cp_parser_save_member_function_body (parser,
17491 &decl_specifiers,
17492 declarator,
17493 attributes);
17494 /* If the member was not a friend, declare it here. */
17495 if (!friend_p)
17496 finish_member_declaration (decl);
17497 /* Peek at the next token. */
17498 token = cp_lexer_peek_token (parser->lexer);
17499 /* If the next token is a semicolon, consume it. */
17500 if (token->type == CPP_SEMICOLON)
17501 cp_lexer_consume_token (parser->lexer);
17502 return;
17503 }
17504 else
17505 if (declarator->kind == cdk_function)
17506 declarator->id_loc = token->location;
17507 /* Create the declaration. */
17508 decl = grokfield (declarator, &decl_specifiers,
17509 initializer, /*init_const_expr_p=*/true,
17510 asm_specification,
17511 attributes);
17512 }
17513
17514 /* Reset PREFIX_ATTRIBUTES. */
17515 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17516 attributes = TREE_CHAIN (attributes);
17517 if (attributes)
17518 TREE_CHAIN (attributes) = NULL_TREE;
17519
17520 /* If there is any qualification still in effect, clear it
17521 now; we will be starting fresh with the next declarator. */
17522 parser->scope = NULL_TREE;
17523 parser->qualifying_scope = NULL_TREE;
17524 parser->object_scope = NULL_TREE;
17525 /* If it's a `,', then there are more declarators. */
17526 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17527 cp_lexer_consume_token (parser->lexer);
17528 /* If the next token isn't a `;', then we have a parse error. */
17529 else if (cp_lexer_next_token_is_not (parser->lexer,
17530 CPP_SEMICOLON))
17531 {
17532 cp_parser_error (parser, "expected %<;%>");
17533 /* Skip tokens until we find a `;'. */
17534 cp_parser_skip_to_end_of_statement (parser);
17535
17536 break;
17537 }
17538
17539 if (decl)
17540 {
17541 /* Add DECL to the list of members. */
17542 if (!friend_p)
17543 finish_member_declaration (decl);
17544
17545 if (TREE_CODE (decl) == FUNCTION_DECL)
17546 cp_parser_save_default_args (parser, decl);
17547 }
17548 }
17549 }
17550
17551 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17552 }
17553
17554 /* Parse a pure-specifier.
17555
17556 pure-specifier:
17557 = 0
17558
17559 Returns INTEGER_ZERO_NODE if a pure specifier is found.
17560 Otherwise, ERROR_MARK_NODE is returned. */
17561
17562 static tree
17563 cp_parser_pure_specifier (cp_parser* parser)
17564 {
17565 cp_token *token;
17566
17567 /* Look for the `=' token. */
17568 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17569 return error_mark_node;
17570 /* Look for the `0' token. */
17571 token = cp_lexer_peek_token (parser->lexer);
17572
17573 if (token->type == CPP_EOF
17574 || token->type == CPP_PRAGMA_EOL)
17575 return error_mark_node;
17576
17577 cp_lexer_consume_token (parser->lexer);
17578
17579 /* Accept = default or = delete in c++0x mode. */
17580 if (token->keyword == RID_DEFAULT
17581 || token->keyword == RID_DELETE)
17582 {
17583 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17584 return token->u.value;
17585 }
17586
17587 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
17588 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
17589 {
17590 cp_parser_error (parser,
17591 "invalid pure specifier (only %<= 0%> is allowed)");
17592 cp_parser_skip_to_end_of_statement (parser);
17593 return error_mark_node;
17594 }
17595 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17596 {
17597 error_at (token->location, "templates may not be %<virtual%>");
17598 return error_mark_node;
17599 }
17600
17601 return integer_zero_node;
17602 }
17603
17604 /* Parse a constant-initializer.
17605
17606 constant-initializer:
17607 = constant-expression
17608
17609 Returns a representation of the constant-expression. */
17610
17611 static tree
17612 cp_parser_constant_initializer (cp_parser* parser)
17613 {
17614 /* Look for the `=' token. */
17615 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17616 return error_mark_node;
17617
17618 /* It is invalid to write:
17619
17620 struct S { static const int i = { 7 }; };
17621
17622 */
17623 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17624 {
17625 cp_parser_error (parser,
17626 "a brace-enclosed initializer is not allowed here");
17627 /* Consume the opening brace. */
17628 cp_lexer_consume_token (parser->lexer);
17629 /* Skip the initializer. */
17630 cp_parser_skip_to_closing_brace (parser);
17631 /* Look for the trailing `}'. */
17632 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17633
17634 return error_mark_node;
17635 }
17636
17637 return cp_parser_constant_expression (parser,
17638 /*allow_non_constant=*/false,
17639 NULL);
17640 }
17641
17642 /* Derived classes [gram.class.derived] */
17643
17644 /* Parse a base-clause.
17645
17646 base-clause:
17647 : base-specifier-list
17648
17649 base-specifier-list:
17650 base-specifier ... [opt]
17651 base-specifier-list , base-specifier ... [opt]
17652
17653 Returns a TREE_LIST representing the base-classes, in the order in
17654 which they were declared. The representation of each node is as
17655 described by cp_parser_base_specifier.
17656
17657 In the case that no bases are specified, this function will return
17658 NULL_TREE, not ERROR_MARK_NODE. */
17659
17660 static tree
17661 cp_parser_base_clause (cp_parser* parser)
17662 {
17663 tree bases = NULL_TREE;
17664
17665 /* Look for the `:' that begins the list. */
17666 cp_parser_require (parser, CPP_COLON, RT_COLON);
17667
17668 /* Scan the base-specifier-list. */
17669 while (true)
17670 {
17671 cp_token *token;
17672 tree base;
17673 bool pack_expansion_p = false;
17674
17675 /* Look for the base-specifier. */
17676 base = cp_parser_base_specifier (parser);
17677 /* Look for the (optional) ellipsis. */
17678 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17679 {
17680 /* Consume the `...'. */
17681 cp_lexer_consume_token (parser->lexer);
17682
17683 pack_expansion_p = true;
17684 }
17685
17686 /* Add BASE to the front of the list. */
17687 if (base != error_mark_node)
17688 {
17689 if (pack_expansion_p)
17690 /* Make this a pack expansion type. */
17691 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17692
17693
17694 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17695 {
17696 TREE_CHAIN (base) = bases;
17697 bases = base;
17698 }
17699 }
17700 /* Peek at the next token. */
17701 token = cp_lexer_peek_token (parser->lexer);
17702 /* If it's not a comma, then the list is complete. */
17703 if (token->type != CPP_COMMA)
17704 break;
17705 /* Consume the `,'. */
17706 cp_lexer_consume_token (parser->lexer);
17707 }
17708
17709 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17710 base class had a qualified name. However, the next name that
17711 appears is certainly not qualified. */
17712 parser->scope = NULL_TREE;
17713 parser->qualifying_scope = NULL_TREE;
17714 parser->object_scope = NULL_TREE;
17715
17716 return nreverse (bases);
17717 }
17718
17719 /* Parse a base-specifier.
17720
17721 base-specifier:
17722 :: [opt] nested-name-specifier [opt] class-name
17723 virtual access-specifier [opt] :: [opt] nested-name-specifier
17724 [opt] class-name
17725 access-specifier virtual [opt] :: [opt] nested-name-specifier
17726 [opt] class-name
17727
17728 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17729 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17730 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17731 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17732
17733 static tree
17734 cp_parser_base_specifier (cp_parser* parser)
17735 {
17736 cp_token *token;
17737 bool done = false;
17738 bool virtual_p = false;
17739 bool duplicate_virtual_error_issued_p = false;
17740 bool duplicate_access_error_issued_p = false;
17741 bool class_scope_p, template_p;
17742 tree access = access_default_node;
17743 tree type;
17744
17745 /* Process the optional `virtual' and `access-specifier'. */
17746 while (!done)
17747 {
17748 /* Peek at the next token. */
17749 token = cp_lexer_peek_token (parser->lexer);
17750 /* Process `virtual'. */
17751 switch (token->keyword)
17752 {
17753 case RID_VIRTUAL:
17754 /* If `virtual' appears more than once, issue an error. */
17755 if (virtual_p && !duplicate_virtual_error_issued_p)
17756 {
17757 cp_parser_error (parser,
17758 "%<virtual%> specified more than once in base-specified");
17759 duplicate_virtual_error_issued_p = true;
17760 }
17761
17762 virtual_p = true;
17763
17764 /* Consume the `virtual' token. */
17765 cp_lexer_consume_token (parser->lexer);
17766
17767 break;
17768
17769 case RID_PUBLIC:
17770 case RID_PROTECTED:
17771 case RID_PRIVATE:
17772 /* If more than one access specifier appears, issue an
17773 error. */
17774 if (access != access_default_node
17775 && !duplicate_access_error_issued_p)
17776 {
17777 cp_parser_error (parser,
17778 "more than one access specifier in base-specified");
17779 duplicate_access_error_issued_p = true;
17780 }
17781
17782 access = ridpointers[(int) token->keyword];
17783
17784 /* Consume the access-specifier. */
17785 cp_lexer_consume_token (parser->lexer);
17786
17787 break;
17788
17789 default:
17790 done = true;
17791 break;
17792 }
17793 }
17794 /* It is not uncommon to see programs mechanically, erroneously, use
17795 the 'typename' keyword to denote (dependent) qualified types
17796 as base classes. */
17797 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17798 {
17799 token = cp_lexer_peek_token (parser->lexer);
17800 if (!processing_template_decl)
17801 error_at (token->location,
17802 "keyword %<typename%> not allowed outside of templates");
17803 else
17804 error_at (token->location,
17805 "keyword %<typename%> not allowed in this context "
17806 "(the base class is implicitly a type)");
17807 cp_lexer_consume_token (parser->lexer);
17808 }
17809
17810 /* Look for the optional `::' operator. */
17811 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17812 /* Look for the nested-name-specifier. The simplest way to
17813 implement:
17814
17815 [temp.res]
17816
17817 The keyword `typename' is not permitted in a base-specifier or
17818 mem-initializer; in these contexts a qualified name that
17819 depends on a template-parameter is implicitly assumed to be a
17820 type name.
17821
17822 is to pretend that we have seen the `typename' keyword at this
17823 point. */
17824 cp_parser_nested_name_specifier_opt (parser,
17825 /*typename_keyword_p=*/true,
17826 /*check_dependency_p=*/true,
17827 typename_type,
17828 /*is_declaration=*/true);
17829 /* If the base class is given by a qualified name, assume that names
17830 we see are type names or templates, as appropriate. */
17831 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17832 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17833
17834 /* Finally, look for the class-name. */
17835 type = cp_parser_class_name (parser,
17836 class_scope_p,
17837 template_p,
17838 typename_type,
17839 /*check_dependency_p=*/true,
17840 /*class_head_p=*/false,
17841 /*is_declaration=*/true);
17842
17843 if (type == error_mark_node)
17844 return error_mark_node;
17845
17846 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17847 }
17848
17849 /* Exception handling [gram.exception] */
17850
17851 /* Parse an (optional) exception-specification.
17852
17853 exception-specification:
17854 throw ( type-id-list [opt] )
17855
17856 Returns a TREE_LIST representing the exception-specification. The
17857 TREE_VALUE of each node is a type. */
17858
17859 static tree
17860 cp_parser_exception_specification_opt (cp_parser* parser)
17861 {
17862 cp_token *token;
17863 tree type_id_list;
17864 const char *saved_message;
17865
17866 /* Peek at the next token. */
17867 token = cp_lexer_peek_token (parser->lexer);
17868
17869 /* Is it a noexcept-specification? */
17870 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
17871 {
17872 tree expr;
17873 cp_lexer_consume_token (parser->lexer);
17874
17875 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
17876 {
17877 cp_lexer_consume_token (parser->lexer);
17878
17879 /* Types may not be defined in an exception-specification. */
17880 saved_message = parser->type_definition_forbidden_message;
17881 parser->type_definition_forbidden_message
17882 = G_("types may not be defined in an exception-specification");
17883
17884 expr = cp_parser_constant_expression (parser, false, NULL);
17885
17886 /* Restore the saved message. */
17887 parser->type_definition_forbidden_message = saved_message;
17888
17889 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17890 }
17891 else
17892 expr = boolean_true_node;
17893
17894 return build_noexcept_spec (expr, tf_warning_or_error);
17895 }
17896
17897 /* If it's not `throw', then there's no exception-specification. */
17898 if (!cp_parser_is_keyword (token, RID_THROW))
17899 return NULL_TREE;
17900
17901 #if 0
17902 /* Enable this once a lot of code has transitioned to noexcept? */
17903 if (cxx_dialect == cxx0x && !in_system_header)
17904 warning (OPT_Wdeprecated, "dynamic exception specifications are "
17905 "deprecated in C++0x; use %<noexcept%> instead.");
17906 #endif
17907
17908 /* Consume the `throw'. */
17909 cp_lexer_consume_token (parser->lexer);
17910
17911 /* Look for the `('. */
17912 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17913
17914 /* Peek at the next token. */
17915 token = cp_lexer_peek_token (parser->lexer);
17916 /* If it's not a `)', then there is a type-id-list. */
17917 if (token->type != CPP_CLOSE_PAREN)
17918 {
17919 /* Types may not be defined in an exception-specification. */
17920 saved_message = parser->type_definition_forbidden_message;
17921 parser->type_definition_forbidden_message
17922 = G_("types may not be defined in an exception-specification");
17923 /* Parse the type-id-list. */
17924 type_id_list = cp_parser_type_id_list (parser);
17925 /* Restore the saved message. */
17926 parser->type_definition_forbidden_message = saved_message;
17927 }
17928 else
17929 type_id_list = empty_except_spec;
17930
17931 /* Look for the `)'. */
17932 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17933
17934 return type_id_list;
17935 }
17936
17937 /* Parse an (optional) type-id-list.
17938
17939 type-id-list:
17940 type-id ... [opt]
17941 type-id-list , type-id ... [opt]
17942
17943 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17944 in the order that the types were presented. */
17945
17946 static tree
17947 cp_parser_type_id_list (cp_parser* parser)
17948 {
17949 tree types = NULL_TREE;
17950
17951 while (true)
17952 {
17953 cp_token *token;
17954 tree type;
17955
17956 /* Get the next type-id. */
17957 type = cp_parser_type_id (parser);
17958 /* Parse the optional ellipsis. */
17959 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17960 {
17961 /* Consume the `...'. */
17962 cp_lexer_consume_token (parser->lexer);
17963
17964 /* Turn the type into a pack expansion expression. */
17965 type = make_pack_expansion (type);
17966 }
17967 /* Add it to the list. */
17968 types = add_exception_specifier (types, type, /*complain=*/1);
17969 /* Peek at the next token. */
17970 token = cp_lexer_peek_token (parser->lexer);
17971 /* If it is not a `,', we are done. */
17972 if (token->type != CPP_COMMA)
17973 break;
17974 /* Consume the `,'. */
17975 cp_lexer_consume_token (parser->lexer);
17976 }
17977
17978 return nreverse (types);
17979 }
17980
17981 /* Parse a try-block.
17982
17983 try-block:
17984 try compound-statement handler-seq */
17985
17986 static tree
17987 cp_parser_try_block (cp_parser* parser)
17988 {
17989 tree try_block;
17990
17991 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
17992 try_block = begin_try_block ();
17993 cp_parser_compound_statement (parser, NULL, true);
17994 finish_try_block (try_block);
17995 cp_parser_handler_seq (parser);
17996 finish_handler_sequence (try_block);
17997
17998 return try_block;
17999 }
18000
18001 /* Parse a function-try-block.
18002
18003 function-try-block:
18004 try ctor-initializer [opt] function-body handler-seq */
18005
18006 static bool
18007 cp_parser_function_try_block (cp_parser* parser)
18008 {
18009 tree compound_stmt;
18010 tree try_block;
18011 bool ctor_initializer_p;
18012
18013 /* Look for the `try' keyword. */
18014 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18015 return false;
18016 /* Let the rest of the front end know where we are. */
18017 try_block = begin_function_try_block (&compound_stmt);
18018 /* Parse the function-body. */
18019 ctor_initializer_p
18020 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18021 /* We're done with the `try' part. */
18022 finish_function_try_block (try_block);
18023 /* Parse the handlers. */
18024 cp_parser_handler_seq (parser);
18025 /* We're done with the handlers. */
18026 finish_function_handler_sequence (try_block, compound_stmt);
18027
18028 return ctor_initializer_p;
18029 }
18030
18031 /* Parse a handler-seq.
18032
18033 handler-seq:
18034 handler handler-seq [opt] */
18035
18036 static void
18037 cp_parser_handler_seq (cp_parser* parser)
18038 {
18039 while (true)
18040 {
18041 cp_token *token;
18042
18043 /* Parse the handler. */
18044 cp_parser_handler (parser);
18045 /* Peek at the next token. */
18046 token = cp_lexer_peek_token (parser->lexer);
18047 /* If it's not `catch' then there are no more handlers. */
18048 if (!cp_parser_is_keyword (token, RID_CATCH))
18049 break;
18050 }
18051 }
18052
18053 /* Parse a handler.
18054
18055 handler:
18056 catch ( exception-declaration ) compound-statement */
18057
18058 static void
18059 cp_parser_handler (cp_parser* parser)
18060 {
18061 tree handler;
18062 tree declaration;
18063
18064 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18065 handler = begin_handler ();
18066 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18067 declaration = cp_parser_exception_declaration (parser);
18068 finish_handler_parms (declaration, handler);
18069 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18070 cp_parser_compound_statement (parser, NULL, false);
18071 finish_handler (handler);
18072 }
18073
18074 /* Parse an exception-declaration.
18075
18076 exception-declaration:
18077 type-specifier-seq declarator
18078 type-specifier-seq abstract-declarator
18079 type-specifier-seq
18080 ...
18081
18082 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18083 ellipsis variant is used. */
18084
18085 static tree
18086 cp_parser_exception_declaration (cp_parser* parser)
18087 {
18088 cp_decl_specifier_seq type_specifiers;
18089 cp_declarator *declarator;
18090 const char *saved_message;
18091
18092 /* If it's an ellipsis, it's easy to handle. */
18093 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18094 {
18095 /* Consume the `...' token. */
18096 cp_lexer_consume_token (parser->lexer);
18097 return NULL_TREE;
18098 }
18099
18100 /* Types may not be defined in exception-declarations. */
18101 saved_message = parser->type_definition_forbidden_message;
18102 parser->type_definition_forbidden_message
18103 = G_("types may not be defined in exception-declarations");
18104
18105 /* Parse the type-specifier-seq. */
18106 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18107 /*is_trailing_return=*/false,
18108 &type_specifiers);
18109 /* If it's a `)', then there is no declarator. */
18110 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18111 declarator = NULL;
18112 else
18113 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18114 /*ctor_dtor_or_conv_p=*/NULL,
18115 /*parenthesized_p=*/NULL,
18116 /*member_p=*/false);
18117
18118 /* Restore the saved message. */
18119 parser->type_definition_forbidden_message = saved_message;
18120
18121 if (!type_specifiers.any_specifiers_p)
18122 return error_mark_node;
18123
18124 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18125 }
18126
18127 /* Parse a throw-expression.
18128
18129 throw-expression:
18130 throw assignment-expression [opt]
18131
18132 Returns a THROW_EXPR representing the throw-expression. */
18133
18134 static tree
18135 cp_parser_throw_expression (cp_parser* parser)
18136 {
18137 tree expression;
18138 cp_token* token;
18139
18140 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18141 token = cp_lexer_peek_token (parser->lexer);
18142 /* Figure out whether or not there is an assignment-expression
18143 following the "throw" keyword. */
18144 if (token->type == CPP_COMMA
18145 || token->type == CPP_SEMICOLON
18146 || token->type == CPP_CLOSE_PAREN
18147 || token->type == CPP_CLOSE_SQUARE
18148 || token->type == CPP_CLOSE_BRACE
18149 || token->type == CPP_COLON)
18150 expression = NULL_TREE;
18151 else
18152 expression = cp_parser_assignment_expression (parser,
18153 /*cast_p=*/false, NULL);
18154
18155 return build_throw (expression);
18156 }
18157
18158 /* GNU Extensions */
18159
18160 /* Parse an (optional) asm-specification.
18161
18162 asm-specification:
18163 asm ( string-literal )
18164
18165 If the asm-specification is present, returns a STRING_CST
18166 corresponding to the string-literal. Otherwise, returns
18167 NULL_TREE. */
18168
18169 static tree
18170 cp_parser_asm_specification_opt (cp_parser* parser)
18171 {
18172 cp_token *token;
18173 tree asm_specification;
18174
18175 /* Peek at the next token. */
18176 token = cp_lexer_peek_token (parser->lexer);
18177 /* If the next token isn't the `asm' keyword, then there's no
18178 asm-specification. */
18179 if (!cp_parser_is_keyword (token, RID_ASM))
18180 return NULL_TREE;
18181
18182 /* Consume the `asm' token. */
18183 cp_lexer_consume_token (parser->lexer);
18184 /* Look for the `('. */
18185 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18186
18187 /* Look for the string-literal. */
18188 asm_specification = cp_parser_string_literal (parser, false, false);
18189
18190 /* Look for the `)'. */
18191 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18192
18193 return asm_specification;
18194 }
18195
18196 /* Parse an asm-operand-list.
18197
18198 asm-operand-list:
18199 asm-operand
18200 asm-operand-list , asm-operand
18201
18202 asm-operand:
18203 string-literal ( expression )
18204 [ string-literal ] string-literal ( expression )
18205
18206 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18207 each node is the expression. The TREE_PURPOSE is itself a
18208 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18209 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18210 is a STRING_CST for the string literal before the parenthesis. Returns
18211 ERROR_MARK_NODE if any of the operands are invalid. */
18212
18213 static tree
18214 cp_parser_asm_operand_list (cp_parser* parser)
18215 {
18216 tree asm_operands = NULL_TREE;
18217 bool invalid_operands = false;
18218
18219 while (true)
18220 {
18221 tree string_literal;
18222 tree expression;
18223 tree name;
18224
18225 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18226 {
18227 /* Consume the `[' token. */
18228 cp_lexer_consume_token (parser->lexer);
18229 /* Read the operand name. */
18230 name = cp_parser_identifier (parser);
18231 if (name != error_mark_node)
18232 name = build_string (IDENTIFIER_LENGTH (name),
18233 IDENTIFIER_POINTER (name));
18234 /* Look for the closing `]'. */
18235 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18236 }
18237 else
18238 name = NULL_TREE;
18239 /* Look for the string-literal. */
18240 string_literal = cp_parser_string_literal (parser, false, false);
18241
18242 /* Look for the `('. */
18243 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18244 /* Parse the expression. */
18245 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18246 /* Look for the `)'. */
18247 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18248
18249 if (name == error_mark_node
18250 || string_literal == error_mark_node
18251 || expression == error_mark_node)
18252 invalid_operands = true;
18253
18254 /* Add this operand to the list. */
18255 asm_operands = tree_cons (build_tree_list (name, string_literal),
18256 expression,
18257 asm_operands);
18258 /* If the next token is not a `,', there are no more
18259 operands. */
18260 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18261 break;
18262 /* Consume the `,'. */
18263 cp_lexer_consume_token (parser->lexer);
18264 }
18265
18266 return invalid_operands ? error_mark_node : nreverse (asm_operands);
18267 }
18268
18269 /* Parse an asm-clobber-list.
18270
18271 asm-clobber-list:
18272 string-literal
18273 asm-clobber-list , string-literal
18274
18275 Returns a TREE_LIST, indicating the clobbers in the order that they
18276 appeared. The TREE_VALUE of each node is a STRING_CST. */
18277
18278 static tree
18279 cp_parser_asm_clobber_list (cp_parser* parser)
18280 {
18281 tree clobbers = NULL_TREE;
18282
18283 while (true)
18284 {
18285 tree string_literal;
18286
18287 /* Look for the string literal. */
18288 string_literal = cp_parser_string_literal (parser, false, false);
18289 /* Add it to the list. */
18290 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18291 /* If the next token is not a `,', then the list is
18292 complete. */
18293 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18294 break;
18295 /* Consume the `,' token. */
18296 cp_lexer_consume_token (parser->lexer);
18297 }
18298
18299 return clobbers;
18300 }
18301
18302 /* Parse an asm-label-list.
18303
18304 asm-label-list:
18305 identifier
18306 asm-label-list , identifier
18307
18308 Returns a TREE_LIST, indicating the labels in the order that they
18309 appeared. The TREE_VALUE of each node is a label. */
18310
18311 static tree
18312 cp_parser_asm_label_list (cp_parser* parser)
18313 {
18314 tree labels = NULL_TREE;
18315
18316 while (true)
18317 {
18318 tree identifier, label, name;
18319
18320 /* Look for the identifier. */
18321 identifier = cp_parser_identifier (parser);
18322 if (!error_operand_p (identifier))
18323 {
18324 label = lookup_label (identifier);
18325 if (TREE_CODE (label) == LABEL_DECL)
18326 {
18327 TREE_USED (label) = 1;
18328 check_goto (label);
18329 name = build_string (IDENTIFIER_LENGTH (identifier),
18330 IDENTIFIER_POINTER (identifier));
18331 labels = tree_cons (name, label, labels);
18332 }
18333 }
18334 /* If the next token is not a `,', then the list is
18335 complete. */
18336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18337 break;
18338 /* Consume the `,' token. */
18339 cp_lexer_consume_token (parser->lexer);
18340 }
18341
18342 return nreverse (labels);
18343 }
18344
18345 /* Parse an (optional) series of attributes.
18346
18347 attributes:
18348 attributes attribute
18349
18350 attribute:
18351 __attribute__ (( attribute-list [opt] ))
18352
18353 The return value is as for cp_parser_attribute_list. */
18354
18355 static tree
18356 cp_parser_attributes_opt (cp_parser* parser)
18357 {
18358 tree attributes = NULL_TREE;
18359
18360 while (true)
18361 {
18362 cp_token *token;
18363 tree attribute_list;
18364
18365 /* Peek at the next token. */
18366 token = cp_lexer_peek_token (parser->lexer);
18367 /* If it's not `__attribute__', then we're done. */
18368 if (token->keyword != RID_ATTRIBUTE)
18369 break;
18370
18371 /* Consume the `__attribute__' keyword. */
18372 cp_lexer_consume_token (parser->lexer);
18373 /* Look for the two `(' tokens. */
18374 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18375 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18376
18377 /* Peek at the next token. */
18378 token = cp_lexer_peek_token (parser->lexer);
18379 if (token->type != CPP_CLOSE_PAREN)
18380 /* Parse the attribute-list. */
18381 attribute_list = cp_parser_attribute_list (parser);
18382 else
18383 /* If the next token is a `)', then there is no attribute
18384 list. */
18385 attribute_list = NULL;
18386
18387 /* Look for the two `)' tokens. */
18388 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18389 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18390
18391 /* Add these new attributes to the list. */
18392 attributes = chainon (attributes, attribute_list);
18393 }
18394
18395 return attributes;
18396 }
18397
18398 /* Parse an attribute-list.
18399
18400 attribute-list:
18401 attribute
18402 attribute-list , attribute
18403
18404 attribute:
18405 identifier
18406 identifier ( identifier )
18407 identifier ( identifier , expression-list )
18408 identifier ( expression-list )
18409
18410 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18411 to an attribute. The TREE_PURPOSE of each node is the identifier
18412 indicating which attribute is in use. The TREE_VALUE represents
18413 the arguments, if any. */
18414
18415 static tree
18416 cp_parser_attribute_list (cp_parser* parser)
18417 {
18418 tree attribute_list = NULL_TREE;
18419 bool save_translate_strings_p = parser->translate_strings_p;
18420
18421 parser->translate_strings_p = false;
18422 while (true)
18423 {
18424 cp_token *token;
18425 tree identifier;
18426 tree attribute;
18427
18428 /* Look for the identifier. We also allow keywords here; for
18429 example `__attribute__ ((const))' is legal. */
18430 token = cp_lexer_peek_token (parser->lexer);
18431 if (token->type == CPP_NAME
18432 || token->type == CPP_KEYWORD)
18433 {
18434 tree arguments = NULL_TREE;
18435
18436 /* Consume the token. */
18437 token = cp_lexer_consume_token (parser->lexer);
18438
18439 /* Save away the identifier that indicates which attribute
18440 this is. */
18441 identifier = (token->type == CPP_KEYWORD)
18442 /* For keywords, use the canonical spelling, not the
18443 parsed identifier. */
18444 ? ridpointers[(int) token->keyword]
18445 : token->u.value;
18446
18447 attribute = build_tree_list (identifier, NULL_TREE);
18448
18449 /* Peek at the next token. */
18450 token = cp_lexer_peek_token (parser->lexer);
18451 /* If it's an `(', then parse the attribute arguments. */
18452 if (token->type == CPP_OPEN_PAREN)
18453 {
18454 VEC(tree,gc) *vec;
18455 int attr_flag = (attribute_takes_identifier_p (identifier)
18456 ? id_attr : normal_attr);
18457 vec = cp_parser_parenthesized_expression_list
18458 (parser, attr_flag, /*cast_p=*/false,
18459 /*allow_expansion_p=*/false,
18460 /*non_constant_p=*/NULL);
18461 if (vec == NULL)
18462 arguments = error_mark_node;
18463 else
18464 {
18465 arguments = build_tree_list_vec (vec);
18466 release_tree_vector (vec);
18467 }
18468 /* Save the arguments away. */
18469 TREE_VALUE (attribute) = arguments;
18470 }
18471
18472 if (arguments != error_mark_node)
18473 {
18474 /* Add this attribute to the list. */
18475 TREE_CHAIN (attribute) = attribute_list;
18476 attribute_list = attribute;
18477 }
18478
18479 token = cp_lexer_peek_token (parser->lexer);
18480 }
18481 /* Now, look for more attributes. If the next token isn't a
18482 `,', we're done. */
18483 if (token->type != CPP_COMMA)
18484 break;
18485
18486 /* Consume the comma and keep going. */
18487 cp_lexer_consume_token (parser->lexer);
18488 }
18489 parser->translate_strings_p = save_translate_strings_p;
18490
18491 /* We built up the list in reverse order. */
18492 return nreverse (attribute_list);
18493 }
18494
18495 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18496 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18497 current value of the PEDANTIC flag, regardless of whether or not
18498 the `__extension__' keyword is present. The caller is responsible
18499 for restoring the value of the PEDANTIC flag. */
18500
18501 static bool
18502 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18503 {
18504 /* Save the old value of the PEDANTIC flag. */
18505 *saved_pedantic = pedantic;
18506
18507 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18508 {
18509 /* Consume the `__extension__' token. */
18510 cp_lexer_consume_token (parser->lexer);
18511 /* We're not being pedantic while the `__extension__' keyword is
18512 in effect. */
18513 pedantic = 0;
18514
18515 return true;
18516 }
18517
18518 return false;
18519 }
18520
18521 /* Parse a label declaration.
18522
18523 label-declaration:
18524 __label__ label-declarator-seq ;
18525
18526 label-declarator-seq:
18527 identifier , label-declarator-seq
18528 identifier */
18529
18530 static void
18531 cp_parser_label_declaration (cp_parser* parser)
18532 {
18533 /* Look for the `__label__' keyword. */
18534 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18535
18536 while (true)
18537 {
18538 tree identifier;
18539
18540 /* Look for an identifier. */
18541 identifier = cp_parser_identifier (parser);
18542 /* If we failed, stop. */
18543 if (identifier == error_mark_node)
18544 break;
18545 /* Declare it as a label. */
18546 finish_label_decl (identifier);
18547 /* If the next token is a `;', stop. */
18548 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18549 break;
18550 /* Look for the `,' separating the label declarations. */
18551 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18552 }
18553
18554 /* Look for the final `;'. */
18555 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18556 }
18557
18558 /* Support Functions */
18559
18560 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18561 NAME should have one of the representations used for an
18562 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18563 is returned. If PARSER->SCOPE is a dependent type, then a
18564 SCOPE_REF is returned.
18565
18566 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18567 returned; the name was already resolved when the TEMPLATE_ID_EXPR
18568 was formed. Abstractly, such entities should not be passed to this
18569 function, because they do not need to be looked up, but it is
18570 simpler to check for this special case here, rather than at the
18571 call-sites.
18572
18573 In cases not explicitly covered above, this function returns a
18574 DECL, OVERLOAD, or baselink representing the result of the lookup.
18575 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18576 is returned.
18577
18578 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18579 (e.g., "struct") that was used. In that case bindings that do not
18580 refer to types are ignored.
18581
18582 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18583 ignored.
18584
18585 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18586 are ignored.
18587
18588 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18589 types.
18590
18591 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18592 TREE_LIST of candidates if name-lookup results in an ambiguity, and
18593 NULL_TREE otherwise. */
18594
18595 static tree
18596 cp_parser_lookup_name (cp_parser *parser, tree name,
18597 enum tag_types tag_type,
18598 bool is_template,
18599 bool is_namespace,
18600 bool check_dependency,
18601 tree *ambiguous_decls,
18602 location_t name_location)
18603 {
18604 int flags = 0;
18605 tree decl;
18606 tree object_type = parser->context->object_type;
18607
18608 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18609 flags |= LOOKUP_COMPLAIN;
18610
18611 /* Assume that the lookup will be unambiguous. */
18612 if (ambiguous_decls)
18613 *ambiguous_decls = NULL_TREE;
18614
18615 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
18616 no longer valid. Note that if we are parsing tentatively, and
18617 the parse fails, OBJECT_TYPE will be automatically restored. */
18618 parser->context->object_type = NULL_TREE;
18619
18620 if (name == error_mark_node)
18621 return error_mark_node;
18622
18623 /* A template-id has already been resolved; there is no lookup to
18624 do. */
18625 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
18626 return name;
18627 if (BASELINK_P (name))
18628 {
18629 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
18630 == TEMPLATE_ID_EXPR);
18631 return name;
18632 }
18633
18634 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
18635 it should already have been checked to make sure that the name
18636 used matches the type being destroyed. */
18637 if (TREE_CODE (name) == BIT_NOT_EXPR)
18638 {
18639 tree type;
18640
18641 /* Figure out to which type this destructor applies. */
18642 if (parser->scope)
18643 type = parser->scope;
18644 else if (object_type)
18645 type = object_type;
18646 else
18647 type = current_class_type;
18648 /* If that's not a class type, there is no destructor. */
18649 if (!type || !CLASS_TYPE_P (type))
18650 return error_mark_node;
18651 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
18652 lazily_declare_fn (sfk_destructor, type);
18653 if (!CLASSTYPE_DESTRUCTORS (type))
18654 return error_mark_node;
18655 /* If it was a class type, return the destructor. */
18656 return CLASSTYPE_DESTRUCTORS (type);
18657 }
18658
18659 /* By this point, the NAME should be an ordinary identifier. If
18660 the id-expression was a qualified name, the qualifying scope is
18661 stored in PARSER->SCOPE at this point. */
18662 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
18663
18664 /* Perform the lookup. */
18665 if (parser->scope)
18666 {
18667 bool dependent_p;
18668
18669 if (parser->scope == error_mark_node)
18670 return error_mark_node;
18671
18672 /* If the SCOPE is dependent, the lookup must be deferred until
18673 the template is instantiated -- unless we are explicitly
18674 looking up names in uninstantiated templates. Even then, we
18675 cannot look up the name if the scope is not a class type; it
18676 might, for example, be a template type parameter. */
18677 dependent_p = (TYPE_P (parser->scope)
18678 && dependent_scope_p (parser->scope));
18679 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
18680 && dependent_p)
18681 /* Defer lookup. */
18682 decl = error_mark_node;
18683 else
18684 {
18685 tree pushed_scope = NULL_TREE;
18686
18687 /* If PARSER->SCOPE is a dependent type, then it must be a
18688 class type, and we must not be checking dependencies;
18689 otherwise, we would have processed this lookup above. So
18690 that PARSER->SCOPE is not considered a dependent base by
18691 lookup_member, we must enter the scope here. */
18692 if (dependent_p)
18693 pushed_scope = push_scope (parser->scope);
18694
18695 /* If the PARSER->SCOPE is a template specialization, it
18696 may be instantiated during name lookup. In that case,
18697 errors may be issued. Even if we rollback the current
18698 tentative parse, those errors are valid. */
18699 decl = lookup_qualified_name (parser->scope, name,
18700 tag_type != none_type,
18701 /*complain=*/true);
18702
18703 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18704 lookup result and the nested-name-specifier nominates a class C:
18705 * if the name specified after the nested-name-specifier, when
18706 looked up in C, is the injected-class-name of C (Clause 9), or
18707 * if the name specified after the nested-name-specifier is the
18708 same as the identifier or the simple-template-id's template-
18709 name in the last component of the nested-name-specifier,
18710 the name is instead considered to name the constructor of
18711 class C. [ Note: for example, the constructor is not an
18712 acceptable lookup result in an elaborated-type-specifier so
18713 the constructor would not be used in place of the
18714 injected-class-name. --end note ] Such a constructor name
18715 shall be used only in the declarator-id of a declaration that
18716 names a constructor or in a using-declaration. */
18717 if (tag_type == none_type
18718 && DECL_SELF_REFERENCE_P (decl)
18719 && same_type_p (DECL_CONTEXT (decl), parser->scope))
18720 decl = lookup_qualified_name (parser->scope, ctor_identifier,
18721 tag_type != none_type,
18722 /*complain=*/true);
18723
18724 /* If we have a single function from a using decl, pull it out. */
18725 if (TREE_CODE (decl) == OVERLOAD
18726 && !really_overloaded_fn (decl))
18727 decl = OVL_FUNCTION (decl);
18728
18729 if (pushed_scope)
18730 pop_scope (pushed_scope);
18731 }
18732
18733 /* If the scope is a dependent type and either we deferred lookup or
18734 we did lookup but didn't find the name, rememeber the name. */
18735 if (decl == error_mark_node && TYPE_P (parser->scope)
18736 && dependent_type_p (parser->scope))
18737 {
18738 if (tag_type)
18739 {
18740 tree type;
18741
18742 /* The resolution to Core Issue 180 says that `struct
18743 A::B' should be considered a type-name, even if `A'
18744 is dependent. */
18745 type = make_typename_type (parser->scope, name, tag_type,
18746 /*complain=*/tf_error);
18747 decl = TYPE_NAME (type);
18748 }
18749 else if (is_template
18750 && (cp_parser_next_token_ends_template_argument_p (parser)
18751 || cp_lexer_next_token_is (parser->lexer,
18752 CPP_CLOSE_PAREN)))
18753 decl = make_unbound_class_template (parser->scope,
18754 name, NULL_TREE,
18755 /*complain=*/tf_error);
18756 else
18757 decl = build_qualified_name (/*type=*/NULL_TREE,
18758 parser->scope, name,
18759 is_template);
18760 }
18761 parser->qualifying_scope = parser->scope;
18762 parser->object_scope = NULL_TREE;
18763 }
18764 else if (object_type)
18765 {
18766 tree object_decl = NULL_TREE;
18767 /* Look up the name in the scope of the OBJECT_TYPE, unless the
18768 OBJECT_TYPE is not a class. */
18769 if (CLASS_TYPE_P (object_type))
18770 /* If the OBJECT_TYPE is a template specialization, it may
18771 be instantiated during name lookup. In that case, errors
18772 may be issued. Even if we rollback the current tentative
18773 parse, those errors are valid. */
18774 object_decl = lookup_member (object_type,
18775 name,
18776 /*protect=*/0,
18777 tag_type != none_type);
18778 /* Look it up in the enclosing context, too. */
18779 decl = lookup_name_real (name, tag_type != none_type,
18780 /*nonclass=*/0,
18781 /*block_p=*/true, is_namespace, flags);
18782 parser->object_scope = object_type;
18783 parser->qualifying_scope = NULL_TREE;
18784 if (object_decl)
18785 decl = object_decl;
18786 }
18787 else
18788 {
18789 decl = lookup_name_real (name, tag_type != none_type,
18790 /*nonclass=*/0,
18791 /*block_p=*/true, is_namespace, flags);
18792 parser->qualifying_scope = NULL_TREE;
18793 parser->object_scope = NULL_TREE;
18794 }
18795
18796 /* If the lookup failed, let our caller know. */
18797 if (!decl || decl == error_mark_node)
18798 return error_mark_node;
18799
18800 /* Pull out the template from an injected-class-name (or multiple). */
18801 if (is_template)
18802 decl = maybe_get_template_decl_from_type_decl (decl);
18803
18804 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18805 if (TREE_CODE (decl) == TREE_LIST)
18806 {
18807 if (ambiguous_decls)
18808 *ambiguous_decls = decl;
18809 /* The error message we have to print is too complicated for
18810 cp_parser_error, so we incorporate its actions directly. */
18811 if (!cp_parser_simulate_error (parser))
18812 {
18813 error_at (name_location, "reference to %qD is ambiguous",
18814 name);
18815 print_candidates (decl);
18816 }
18817 return error_mark_node;
18818 }
18819
18820 gcc_assert (DECL_P (decl)
18821 || TREE_CODE (decl) == OVERLOAD
18822 || TREE_CODE (decl) == SCOPE_REF
18823 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18824 || BASELINK_P (decl));
18825
18826 /* If we have resolved the name of a member declaration, check to
18827 see if the declaration is accessible. When the name resolves to
18828 set of overloaded functions, accessibility is checked when
18829 overload resolution is done.
18830
18831 During an explicit instantiation, access is not checked at all,
18832 as per [temp.explicit]. */
18833 if (DECL_P (decl))
18834 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18835
18836 return decl;
18837 }
18838
18839 /* Like cp_parser_lookup_name, but for use in the typical case where
18840 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18841 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18842
18843 static tree
18844 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18845 {
18846 return cp_parser_lookup_name (parser, name,
18847 none_type,
18848 /*is_template=*/false,
18849 /*is_namespace=*/false,
18850 /*check_dependency=*/true,
18851 /*ambiguous_decls=*/NULL,
18852 location);
18853 }
18854
18855 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18856 the current context, return the TYPE_DECL. If TAG_NAME_P is
18857 true, the DECL indicates the class being defined in a class-head,
18858 or declared in an elaborated-type-specifier.
18859
18860 Otherwise, return DECL. */
18861
18862 static tree
18863 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18864 {
18865 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18866 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18867
18868 struct A {
18869 template <typename T> struct B;
18870 };
18871
18872 template <typename T> struct A::B {};
18873
18874 Similarly, in an elaborated-type-specifier:
18875
18876 namespace N { struct X{}; }
18877
18878 struct A {
18879 template <typename T> friend struct N::X;
18880 };
18881
18882 However, if the DECL refers to a class type, and we are in
18883 the scope of the class, then the name lookup automatically
18884 finds the TYPE_DECL created by build_self_reference rather
18885 than a TEMPLATE_DECL. For example, in:
18886
18887 template <class T> struct S {
18888 S s;
18889 };
18890
18891 there is no need to handle such case. */
18892
18893 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18894 return DECL_TEMPLATE_RESULT (decl);
18895
18896 return decl;
18897 }
18898
18899 /* If too many, or too few, template-parameter lists apply to the
18900 declarator, issue an error message. Returns TRUE if all went well,
18901 and FALSE otherwise. */
18902
18903 static bool
18904 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18905 cp_declarator *declarator,
18906 location_t declarator_location)
18907 {
18908 unsigned num_templates;
18909
18910 /* We haven't seen any classes that involve template parameters yet. */
18911 num_templates = 0;
18912
18913 switch (declarator->kind)
18914 {
18915 case cdk_id:
18916 if (declarator->u.id.qualifying_scope)
18917 {
18918 tree scope;
18919
18920 scope = declarator->u.id.qualifying_scope;
18921
18922 while (scope && CLASS_TYPE_P (scope))
18923 {
18924 /* You're supposed to have one `template <...>'
18925 for every template class, but you don't need one
18926 for a full specialization. For example:
18927
18928 template <class T> struct S{};
18929 template <> struct S<int> { void f(); };
18930 void S<int>::f () {}
18931
18932 is correct; there shouldn't be a `template <>' for
18933 the definition of `S<int>::f'. */
18934 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18935 /* If SCOPE does not have template information of any
18936 kind, then it is not a template, nor is it nested
18937 within a template. */
18938 break;
18939 if (explicit_class_specialization_p (scope))
18940 break;
18941 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18942 ++num_templates;
18943
18944 scope = TYPE_CONTEXT (scope);
18945 }
18946 }
18947 else if (TREE_CODE (declarator->u.id.unqualified_name)
18948 == TEMPLATE_ID_EXPR)
18949 /* If the DECLARATOR has the form `X<y>' then it uses one
18950 additional level of template parameters. */
18951 ++num_templates;
18952
18953 return cp_parser_check_template_parameters
18954 (parser, num_templates, declarator_location, declarator);
18955
18956
18957 case cdk_function:
18958 case cdk_array:
18959 case cdk_pointer:
18960 case cdk_reference:
18961 case cdk_ptrmem:
18962 return (cp_parser_check_declarator_template_parameters
18963 (parser, declarator->declarator, declarator_location));
18964
18965 case cdk_error:
18966 return true;
18967
18968 default:
18969 gcc_unreachable ();
18970 }
18971 return false;
18972 }
18973
18974 /* NUM_TEMPLATES were used in the current declaration. If that is
18975 invalid, return FALSE and issue an error messages. Otherwise,
18976 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18977 declarator and we can print more accurate diagnostics. */
18978
18979 static bool
18980 cp_parser_check_template_parameters (cp_parser* parser,
18981 unsigned num_templates,
18982 location_t location,
18983 cp_declarator *declarator)
18984 {
18985 /* If there are the same number of template classes and parameter
18986 lists, that's OK. */
18987 if (parser->num_template_parameter_lists == num_templates)
18988 return true;
18989 /* If there are more, but only one more, then we are referring to a
18990 member template. That's OK too. */
18991 if (parser->num_template_parameter_lists == num_templates + 1)
18992 return true;
18993 /* If there are more template classes than parameter lists, we have
18994 something like:
18995
18996 template <class T> void S<T>::R<T>::f (); */
18997 if (parser->num_template_parameter_lists < num_templates)
18998 {
18999 if (declarator && !current_function_decl)
19000 error_at (location, "specializing member %<%T::%E%> "
19001 "requires %<template<>%> syntax",
19002 declarator->u.id.qualifying_scope,
19003 declarator->u.id.unqualified_name);
19004 else if (declarator)
19005 error_at (location, "invalid declaration of %<%T::%E%>",
19006 declarator->u.id.qualifying_scope,
19007 declarator->u.id.unqualified_name);
19008 else
19009 error_at (location, "too few template-parameter-lists");
19010 return false;
19011 }
19012 /* Otherwise, there are too many template parameter lists. We have
19013 something like:
19014
19015 template <class T> template <class U> void S::f(); */
19016 error_at (location, "too many template-parameter-lists");
19017 return false;
19018 }
19019
19020 /* Parse an optional `::' token indicating that the following name is
19021 from the global namespace. If so, PARSER->SCOPE is set to the
19022 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19023 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19024 Returns the new value of PARSER->SCOPE, if the `::' token is
19025 present, and NULL_TREE otherwise. */
19026
19027 static tree
19028 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19029 {
19030 cp_token *token;
19031
19032 /* Peek at the next token. */
19033 token = cp_lexer_peek_token (parser->lexer);
19034 /* If we're looking at a `::' token then we're starting from the
19035 global namespace, not our current location. */
19036 if (token->type == CPP_SCOPE)
19037 {
19038 /* Consume the `::' token. */
19039 cp_lexer_consume_token (parser->lexer);
19040 /* Set the SCOPE so that we know where to start the lookup. */
19041 parser->scope = global_namespace;
19042 parser->qualifying_scope = global_namespace;
19043 parser->object_scope = NULL_TREE;
19044
19045 return parser->scope;
19046 }
19047 else if (!current_scope_valid_p)
19048 {
19049 parser->scope = NULL_TREE;
19050 parser->qualifying_scope = NULL_TREE;
19051 parser->object_scope = NULL_TREE;
19052 }
19053
19054 return NULL_TREE;
19055 }
19056
19057 /* Returns TRUE if the upcoming token sequence is the start of a
19058 constructor declarator. If FRIEND_P is true, the declarator is
19059 preceded by the `friend' specifier. */
19060
19061 static bool
19062 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19063 {
19064 bool constructor_p;
19065 tree nested_name_specifier;
19066 cp_token *next_token;
19067
19068 /* The common case is that this is not a constructor declarator, so
19069 try to avoid doing lots of work if at all possible. It's not
19070 valid declare a constructor at function scope. */
19071 if (parser->in_function_body)
19072 return false;
19073 /* And only certain tokens can begin a constructor declarator. */
19074 next_token = cp_lexer_peek_token (parser->lexer);
19075 if (next_token->type != CPP_NAME
19076 && next_token->type != CPP_SCOPE
19077 && next_token->type != CPP_NESTED_NAME_SPECIFIER
19078 && next_token->type != CPP_TEMPLATE_ID)
19079 return false;
19080
19081 /* Parse tentatively; we are going to roll back all of the tokens
19082 consumed here. */
19083 cp_parser_parse_tentatively (parser);
19084 /* Assume that we are looking at a constructor declarator. */
19085 constructor_p = true;
19086
19087 /* Look for the optional `::' operator. */
19088 cp_parser_global_scope_opt (parser,
19089 /*current_scope_valid_p=*/false);
19090 /* Look for the nested-name-specifier. */
19091 nested_name_specifier
19092 = (cp_parser_nested_name_specifier_opt (parser,
19093 /*typename_keyword_p=*/false,
19094 /*check_dependency_p=*/false,
19095 /*type_p=*/false,
19096 /*is_declaration=*/false));
19097 /* Outside of a class-specifier, there must be a
19098 nested-name-specifier. */
19099 if (!nested_name_specifier &&
19100 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19101 || friend_p))
19102 constructor_p = false;
19103 else if (nested_name_specifier == error_mark_node)
19104 constructor_p = false;
19105
19106 /* If we have a class scope, this is easy; DR 147 says that S::S always
19107 names the constructor, and no other qualified name could. */
19108 if (constructor_p && nested_name_specifier
19109 && TYPE_P (nested_name_specifier))
19110 {
19111 tree id = cp_parser_unqualified_id (parser,
19112 /*template_keyword_p=*/false,
19113 /*check_dependency_p=*/false,
19114 /*declarator_p=*/true,
19115 /*optional_p=*/false);
19116 if (is_overloaded_fn (id))
19117 id = DECL_NAME (get_first_fn (id));
19118 if (!constructor_name_p (id, nested_name_specifier))
19119 constructor_p = false;
19120 }
19121 /* If we still think that this might be a constructor-declarator,
19122 look for a class-name. */
19123 else if (constructor_p)
19124 {
19125 /* If we have:
19126
19127 template <typename T> struct S {
19128 S();
19129 };
19130
19131 we must recognize that the nested `S' names a class. */
19132 tree type_decl;
19133 type_decl = cp_parser_class_name (parser,
19134 /*typename_keyword_p=*/false,
19135 /*template_keyword_p=*/false,
19136 none_type,
19137 /*check_dependency_p=*/false,
19138 /*class_head_p=*/false,
19139 /*is_declaration=*/false);
19140 /* If there was no class-name, then this is not a constructor. */
19141 constructor_p = !cp_parser_error_occurred (parser);
19142
19143 /* If we're still considering a constructor, we have to see a `(',
19144 to begin the parameter-declaration-clause, followed by either a
19145 `)', an `...', or a decl-specifier. We need to check for a
19146 type-specifier to avoid being fooled into thinking that:
19147
19148 S (f) (int);
19149
19150 is a constructor. (It is actually a function named `f' that
19151 takes one parameter (of type `int') and returns a value of type
19152 `S'. */
19153 if (constructor_p
19154 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19155 constructor_p = false;
19156
19157 if (constructor_p
19158 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19159 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19160 /* A parameter declaration begins with a decl-specifier,
19161 which is either the "attribute" keyword, a storage class
19162 specifier, or (usually) a type-specifier. */
19163 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19164 {
19165 tree type;
19166 tree pushed_scope = NULL_TREE;
19167 unsigned saved_num_template_parameter_lists;
19168
19169 /* Names appearing in the type-specifier should be looked up
19170 in the scope of the class. */
19171 if (current_class_type)
19172 type = NULL_TREE;
19173 else
19174 {
19175 type = TREE_TYPE (type_decl);
19176 if (TREE_CODE (type) == TYPENAME_TYPE)
19177 {
19178 type = resolve_typename_type (type,
19179 /*only_current_p=*/false);
19180 if (TREE_CODE (type) == TYPENAME_TYPE)
19181 {
19182 cp_parser_abort_tentative_parse (parser);
19183 return false;
19184 }
19185 }
19186 pushed_scope = push_scope (type);
19187 }
19188
19189 /* Inside the constructor parameter list, surrounding
19190 template-parameter-lists do not apply. */
19191 saved_num_template_parameter_lists
19192 = parser->num_template_parameter_lists;
19193 parser->num_template_parameter_lists = 0;
19194
19195 /* Look for the type-specifier. */
19196 cp_parser_type_specifier (parser,
19197 CP_PARSER_FLAGS_NONE,
19198 /*decl_specs=*/NULL,
19199 /*is_declarator=*/true,
19200 /*declares_class_or_enum=*/NULL,
19201 /*is_cv_qualifier=*/NULL);
19202
19203 parser->num_template_parameter_lists
19204 = saved_num_template_parameter_lists;
19205
19206 /* Leave the scope of the class. */
19207 if (pushed_scope)
19208 pop_scope (pushed_scope);
19209
19210 constructor_p = !cp_parser_error_occurred (parser);
19211 }
19212 }
19213
19214 /* We did not really want to consume any tokens. */
19215 cp_parser_abort_tentative_parse (parser);
19216
19217 return constructor_p;
19218 }
19219
19220 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19221 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19222 they must be performed once we are in the scope of the function.
19223
19224 Returns the function defined. */
19225
19226 static tree
19227 cp_parser_function_definition_from_specifiers_and_declarator
19228 (cp_parser* parser,
19229 cp_decl_specifier_seq *decl_specifiers,
19230 tree attributes,
19231 const cp_declarator *declarator)
19232 {
19233 tree fn;
19234 bool success_p;
19235
19236 /* Begin the function-definition. */
19237 success_p = start_function (decl_specifiers, declarator, attributes);
19238
19239 /* The things we're about to see are not directly qualified by any
19240 template headers we've seen thus far. */
19241 reset_specialization ();
19242
19243 /* If there were names looked up in the decl-specifier-seq that we
19244 did not check, check them now. We must wait until we are in the
19245 scope of the function to perform the checks, since the function
19246 might be a friend. */
19247 perform_deferred_access_checks ();
19248
19249 if (!success_p)
19250 {
19251 /* Skip the entire function. */
19252 cp_parser_skip_to_end_of_block_or_statement (parser);
19253 fn = error_mark_node;
19254 }
19255 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19256 {
19257 /* Seen already, skip it. An error message has already been output. */
19258 cp_parser_skip_to_end_of_block_or_statement (parser);
19259 fn = current_function_decl;
19260 current_function_decl = NULL_TREE;
19261 /* If this is a function from a class, pop the nested class. */
19262 if (current_class_name)
19263 pop_nested_class ();
19264 }
19265 else
19266 fn = cp_parser_function_definition_after_declarator (parser,
19267 /*inline_p=*/false);
19268
19269 return fn;
19270 }
19271
19272 /* Parse the part of a function-definition that follows the
19273 declarator. INLINE_P is TRUE iff this function is an inline
19274 function defined within a class-specifier.
19275
19276 Returns the function defined. */
19277
19278 static tree
19279 cp_parser_function_definition_after_declarator (cp_parser* parser,
19280 bool inline_p)
19281 {
19282 tree fn;
19283 bool ctor_initializer_p = false;
19284 bool saved_in_unbraced_linkage_specification_p;
19285 bool saved_in_function_body;
19286 unsigned saved_num_template_parameter_lists;
19287 cp_token *token;
19288
19289 saved_in_function_body = parser->in_function_body;
19290 parser->in_function_body = true;
19291 /* If the next token is `return', then the code may be trying to
19292 make use of the "named return value" extension that G++ used to
19293 support. */
19294 token = cp_lexer_peek_token (parser->lexer);
19295 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19296 {
19297 /* Consume the `return' keyword. */
19298 cp_lexer_consume_token (parser->lexer);
19299 /* Look for the identifier that indicates what value is to be
19300 returned. */
19301 cp_parser_identifier (parser);
19302 /* Issue an error message. */
19303 error_at (token->location,
19304 "named return values are no longer supported");
19305 /* Skip tokens until we reach the start of the function body. */
19306 while (true)
19307 {
19308 cp_token *token = cp_lexer_peek_token (parser->lexer);
19309 if (token->type == CPP_OPEN_BRACE
19310 || token->type == CPP_EOF
19311 || token->type == CPP_PRAGMA_EOL)
19312 break;
19313 cp_lexer_consume_token (parser->lexer);
19314 }
19315 }
19316 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19317 anything declared inside `f'. */
19318 saved_in_unbraced_linkage_specification_p
19319 = parser->in_unbraced_linkage_specification_p;
19320 parser->in_unbraced_linkage_specification_p = false;
19321 /* Inside the function, surrounding template-parameter-lists do not
19322 apply. */
19323 saved_num_template_parameter_lists
19324 = parser->num_template_parameter_lists;
19325 parser->num_template_parameter_lists = 0;
19326
19327 start_lambda_scope (current_function_decl);
19328
19329 /* If the next token is `try', then we are looking at a
19330 function-try-block. */
19331 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19332 ctor_initializer_p = cp_parser_function_try_block (parser);
19333 /* A function-try-block includes the function-body, so we only do
19334 this next part if we're not processing a function-try-block. */
19335 else
19336 ctor_initializer_p
19337 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19338
19339 finish_lambda_scope ();
19340
19341 /* Finish the function. */
19342 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19343 (inline_p ? 2 : 0));
19344 /* Generate code for it, if necessary. */
19345 expand_or_defer_fn (fn);
19346 /* Restore the saved values. */
19347 parser->in_unbraced_linkage_specification_p
19348 = saved_in_unbraced_linkage_specification_p;
19349 parser->num_template_parameter_lists
19350 = saved_num_template_parameter_lists;
19351 parser->in_function_body = saved_in_function_body;
19352
19353 return fn;
19354 }
19355
19356 /* Parse a template-declaration, assuming that the `export' (and
19357 `extern') keywords, if present, has already been scanned. MEMBER_P
19358 is as for cp_parser_template_declaration. */
19359
19360 static void
19361 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19362 {
19363 tree decl = NULL_TREE;
19364 VEC (deferred_access_check,gc) *checks;
19365 tree parameter_list;
19366 bool friend_p = false;
19367 bool need_lang_pop;
19368 cp_token *token;
19369
19370 /* Look for the `template' keyword. */
19371 token = cp_lexer_peek_token (parser->lexer);
19372 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19373 return;
19374
19375 /* And the `<'. */
19376 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19377 return;
19378 if (at_class_scope_p () && current_function_decl)
19379 {
19380 /* 14.5.2.2 [temp.mem]
19381
19382 A local class shall not have member templates. */
19383 error_at (token->location,
19384 "invalid declaration of member template in local class");
19385 cp_parser_skip_to_end_of_block_or_statement (parser);
19386 return;
19387 }
19388 /* [temp]
19389
19390 A template ... shall not have C linkage. */
19391 if (current_lang_name == lang_name_c)
19392 {
19393 error_at (token->location, "template with C linkage");
19394 /* Give it C++ linkage to avoid confusing other parts of the
19395 front end. */
19396 push_lang_context (lang_name_cplusplus);
19397 need_lang_pop = true;
19398 }
19399 else
19400 need_lang_pop = false;
19401
19402 /* We cannot perform access checks on the template parameter
19403 declarations until we know what is being declared, just as we
19404 cannot check the decl-specifier list. */
19405 push_deferring_access_checks (dk_deferred);
19406
19407 /* If the next token is `>', then we have an invalid
19408 specialization. Rather than complain about an invalid template
19409 parameter, issue an error message here. */
19410 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19411 {
19412 cp_parser_error (parser, "invalid explicit specialization");
19413 begin_specialization ();
19414 parameter_list = NULL_TREE;
19415 }
19416 else
19417 /* Parse the template parameters. */
19418 parameter_list = cp_parser_template_parameter_list (parser);
19419
19420 /* Get the deferred access checks from the parameter list. These
19421 will be checked once we know what is being declared, as for a
19422 member template the checks must be performed in the scope of the
19423 class containing the member. */
19424 checks = get_deferred_access_checks ();
19425
19426 /* Look for the `>'. */
19427 cp_parser_skip_to_end_of_template_parameter_list (parser);
19428 /* We just processed one more parameter list. */
19429 ++parser->num_template_parameter_lists;
19430 /* If the next token is `template', there are more template
19431 parameters. */
19432 if (cp_lexer_next_token_is_keyword (parser->lexer,
19433 RID_TEMPLATE))
19434 cp_parser_template_declaration_after_export (parser, member_p);
19435 else
19436 {
19437 /* There are no access checks when parsing a template, as we do not
19438 know if a specialization will be a friend. */
19439 push_deferring_access_checks (dk_no_check);
19440 token = cp_lexer_peek_token (parser->lexer);
19441 decl = cp_parser_single_declaration (parser,
19442 checks,
19443 member_p,
19444 /*explicit_specialization_p=*/false,
19445 &friend_p);
19446 pop_deferring_access_checks ();
19447
19448 /* If this is a member template declaration, let the front
19449 end know. */
19450 if (member_p && !friend_p && decl)
19451 {
19452 if (TREE_CODE (decl) == TYPE_DECL)
19453 cp_parser_check_access_in_redeclaration (decl, token->location);
19454
19455 decl = finish_member_template_decl (decl);
19456 }
19457 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19458 make_friend_class (current_class_type, TREE_TYPE (decl),
19459 /*complain=*/true);
19460 }
19461 /* We are done with the current parameter list. */
19462 --parser->num_template_parameter_lists;
19463
19464 pop_deferring_access_checks ();
19465
19466 /* Finish up. */
19467 finish_template_decl (parameter_list);
19468
19469 /* Register member declarations. */
19470 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19471 finish_member_declaration (decl);
19472 /* For the erroneous case of a template with C linkage, we pushed an
19473 implicit C++ linkage scope; exit that scope now. */
19474 if (need_lang_pop)
19475 pop_lang_context ();
19476 /* If DECL is a function template, we must return to parse it later.
19477 (Even though there is no definition, there might be default
19478 arguments that need handling.) */
19479 if (member_p && decl
19480 && (TREE_CODE (decl) == FUNCTION_DECL
19481 || DECL_FUNCTION_TEMPLATE_P (decl)))
19482 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19483 }
19484
19485 /* Perform the deferred access checks from a template-parameter-list.
19486 CHECKS is a TREE_LIST of access checks, as returned by
19487 get_deferred_access_checks. */
19488
19489 static void
19490 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19491 {
19492 ++processing_template_parmlist;
19493 perform_access_checks (checks);
19494 --processing_template_parmlist;
19495 }
19496
19497 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19498 `function-definition' sequence. MEMBER_P is true, this declaration
19499 appears in a class scope.
19500
19501 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19502 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19503
19504 static tree
19505 cp_parser_single_declaration (cp_parser* parser,
19506 VEC (deferred_access_check,gc)* checks,
19507 bool member_p,
19508 bool explicit_specialization_p,
19509 bool* friend_p)
19510 {
19511 int declares_class_or_enum;
19512 tree decl = NULL_TREE;
19513 cp_decl_specifier_seq decl_specifiers;
19514 bool function_definition_p = false;
19515 cp_token *decl_spec_token_start;
19516
19517 /* This function is only used when processing a template
19518 declaration. */
19519 gcc_assert (innermost_scope_kind () == sk_template_parms
19520 || innermost_scope_kind () == sk_template_spec);
19521
19522 /* Defer access checks until we know what is being declared. */
19523 push_deferring_access_checks (dk_deferred);
19524
19525 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19526 alternative. */
19527 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19528 cp_parser_decl_specifier_seq (parser,
19529 CP_PARSER_FLAGS_OPTIONAL,
19530 &decl_specifiers,
19531 &declares_class_or_enum);
19532 if (friend_p)
19533 *friend_p = cp_parser_friend_p (&decl_specifiers);
19534
19535 /* There are no template typedefs. */
19536 if (decl_specifiers.specs[(int) ds_typedef])
19537 {
19538 error_at (decl_spec_token_start->location,
19539 "template declaration of %<typedef%>");
19540 decl = error_mark_node;
19541 }
19542
19543 /* Gather up the access checks that occurred the
19544 decl-specifier-seq. */
19545 stop_deferring_access_checks ();
19546
19547 /* Check for the declaration of a template class. */
19548 if (declares_class_or_enum)
19549 {
19550 if (cp_parser_declares_only_class_p (parser))
19551 {
19552 decl = shadow_tag (&decl_specifiers);
19553
19554 /* In this case:
19555
19556 struct C {
19557 friend template <typename T> struct A<T>::B;
19558 };
19559
19560 A<T>::B will be represented by a TYPENAME_TYPE, and
19561 therefore not recognized by shadow_tag. */
19562 if (friend_p && *friend_p
19563 && !decl
19564 && decl_specifiers.type
19565 && TYPE_P (decl_specifiers.type))
19566 decl = decl_specifiers.type;
19567
19568 if (decl && decl != error_mark_node)
19569 decl = TYPE_NAME (decl);
19570 else
19571 decl = error_mark_node;
19572
19573 /* Perform access checks for template parameters. */
19574 cp_parser_perform_template_parameter_access_checks (checks);
19575 }
19576 }
19577
19578 /* Complain about missing 'typename' or other invalid type names. */
19579 if (!decl_specifiers.any_type_specifiers_p)
19580 cp_parser_parse_and_diagnose_invalid_type_name (parser);
19581
19582 /* If it's not a template class, try for a template function. If
19583 the next token is a `;', then this declaration does not declare
19584 anything. But, if there were errors in the decl-specifiers, then
19585 the error might well have come from an attempted class-specifier.
19586 In that case, there's no need to warn about a missing declarator. */
19587 if (!decl
19588 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
19589 || decl_specifiers.type != error_mark_node))
19590 {
19591 decl = cp_parser_init_declarator (parser,
19592 &decl_specifiers,
19593 checks,
19594 /*function_definition_allowed_p=*/true,
19595 member_p,
19596 declares_class_or_enum,
19597 &function_definition_p);
19598
19599 /* 7.1.1-1 [dcl.stc]
19600
19601 A storage-class-specifier shall not be specified in an explicit
19602 specialization... */
19603 if (decl
19604 && explicit_specialization_p
19605 && decl_specifiers.storage_class != sc_none)
19606 {
19607 error_at (decl_spec_token_start->location,
19608 "explicit template specialization cannot have a storage class");
19609 decl = error_mark_node;
19610 }
19611 }
19612
19613 pop_deferring_access_checks ();
19614
19615 /* Clear any current qualification; whatever comes next is the start
19616 of something new. */
19617 parser->scope = NULL_TREE;
19618 parser->qualifying_scope = NULL_TREE;
19619 parser->object_scope = NULL_TREE;
19620 /* Look for a trailing `;' after the declaration. */
19621 if (!function_definition_p
19622 && (decl == error_mark_node
19623 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
19624 cp_parser_skip_to_end_of_block_or_statement (parser);
19625
19626 return decl;
19627 }
19628
19629 /* Parse a cast-expression that is not the operand of a unary "&". */
19630
19631 static tree
19632 cp_parser_simple_cast_expression (cp_parser *parser)
19633 {
19634 return cp_parser_cast_expression (parser, /*address_p=*/false,
19635 /*cast_p=*/false, NULL);
19636 }
19637
19638 /* Parse a functional cast to TYPE. Returns an expression
19639 representing the cast. */
19640
19641 static tree
19642 cp_parser_functional_cast (cp_parser* parser, tree type)
19643 {
19644 VEC(tree,gc) *vec;
19645 tree expression_list;
19646 tree cast;
19647 bool nonconst_p;
19648
19649 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19650 {
19651 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19652 expression_list = cp_parser_braced_list (parser, &nonconst_p);
19653 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
19654 if (TREE_CODE (type) == TYPE_DECL)
19655 type = TREE_TYPE (type);
19656 return finish_compound_literal (type, expression_list);
19657 }
19658
19659
19660 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19661 /*cast_p=*/true,
19662 /*allow_expansion_p=*/true,
19663 /*non_constant_p=*/NULL);
19664 if (vec == NULL)
19665 expression_list = error_mark_node;
19666 else
19667 {
19668 expression_list = build_tree_list_vec (vec);
19669 release_tree_vector (vec);
19670 }
19671
19672 cast = build_functional_cast (type, expression_list,
19673 tf_warning_or_error);
19674 /* [expr.const]/1: In an integral constant expression "only type
19675 conversions to integral or enumeration type can be used". */
19676 if (TREE_CODE (type) == TYPE_DECL)
19677 type = TREE_TYPE (type);
19678 if (cast != error_mark_node
19679 && !cast_valid_in_integral_constant_expression_p (type)
19680 && cp_parser_non_integral_constant_expression (parser,
19681 NIC_CONSTRUCTOR))
19682 return error_mark_node;
19683 return cast;
19684 }
19685
19686 /* Save the tokens that make up the body of a member function defined
19687 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
19688 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
19689 specifiers applied to the declaration. Returns the FUNCTION_DECL
19690 for the member function. */
19691
19692 static tree
19693 cp_parser_save_member_function_body (cp_parser* parser,
19694 cp_decl_specifier_seq *decl_specifiers,
19695 cp_declarator *declarator,
19696 tree attributes)
19697 {
19698 cp_token *first;
19699 cp_token *last;
19700 tree fn;
19701
19702 /* Create the FUNCTION_DECL. */
19703 fn = grokmethod (decl_specifiers, declarator, attributes);
19704 /* If something went badly wrong, bail out now. */
19705 if (fn == error_mark_node)
19706 {
19707 /* If there's a function-body, skip it. */
19708 if (cp_parser_token_starts_function_definition_p
19709 (cp_lexer_peek_token (parser->lexer)))
19710 cp_parser_skip_to_end_of_block_or_statement (parser);
19711 return error_mark_node;
19712 }
19713
19714 /* Remember it, if there default args to post process. */
19715 cp_parser_save_default_args (parser, fn);
19716
19717 /* Save away the tokens that make up the body of the
19718 function. */
19719 first = parser->lexer->next_token;
19720 /* We can have braced-init-list mem-initializers before the fn body. */
19721 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19722 {
19723 cp_lexer_consume_token (parser->lexer);
19724 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19725 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19726 {
19727 /* cache_group will stop after an un-nested { } pair, too. */
19728 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19729 break;
19730
19731 /* variadic mem-inits have ... after the ')'. */
19732 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19733 cp_lexer_consume_token (parser->lexer);
19734 }
19735 }
19736 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19737 /* Handle function try blocks. */
19738 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19739 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19740 last = parser->lexer->next_token;
19741
19742 /* Save away the inline definition; we will process it when the
19743 class is complete. */
19744 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19745 DECL_PENDING_INLINE_P (fn) = 1;
19746
19747 /* We need to know that this was defined in the class, so that
19748 friend templates are handled correctly. */
19749 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19750
19751 /* Add FN to the queue of functions to be parsed later. */
19752 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
19753
19754 return fn;
19755 }
19756
19757 /* Parse a template-argument-list, as well as the trailing ">" (but
19758 not the opening ">"). See cp_parser_template_argument_list for the
19759 return value. */
19760
19761 static tree
19762 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19763 {
19764 tree arguments;
19765 tree saved_scope;
19766 tree saved_qualifying_scope;
19767 tree saved_object_scope;
19768 bool saved_greater_than_is_operator_p;
19769 int saved_unevaluated_operand;
19770 int saved_inhibit_evaluation_warnings;
19771
19772 /* [temp.names]
19773
19774 When parsing a template-id, the first non-nested `>' is taken as
19775 the end of the template-argument-list rather than a greater-than
19776 operator. */
19777 saved_greater_than_is_operator_p
19778 = parser->greater_than_is_operator_p;
19779 parser->greater_than_is_operator_p = false;
19780 /* Parsing the argument list may modify SCOPE, so we save it
19781 here. */
19782 saved_scope = parser->scope;
19783 saved_qualifying_scope = parser->qualifying_scope;
19784 saved_object_scope = parser->object_scope;
19785 /* We need to evaluate the template arguments, even though this
19786 template-id may be nested within a "sizeof". */
19787 saved_unevaluated_operand = cp_unevaluated_operand;
19788 cp_unevaluated_operand = 0;
19789 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19790 c_inhibit_evaluation_warnings = 0;
19791 /* Parse the template-argument-list itself. */
19792 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19793 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19794 arguments = NULL_TREE;
19795 else
19796 arguments = cp_parser_template_argument_list (parser);
19797 /* Look for the `>' that ends the template-argument-list. If we find
19798 a '>>' instead, it's probably just a typo. */
19799 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19800 {
19801 if (cxx_dialect != cxx98)
19802 {
19803 /* In C++0x, a `>>' in a template argument list or cast
19804 expression is considered to be two separate `>'
19805 tokens. So, change the current token to a `>', but don't
19806 consume it: it will be consumed later when the outer
19807 template argument list (or cast expression) is parsed.
19808 Note that this replacement of `>' for `>>' is necessary
19809 even if we are parsing tentatively: in the tentative
19810 case, after calling
19811 cp_parser_enclosed_template_argument_list we will always
19812 throw away all of the template arguments and the first
19813 closing `>', either because the template argument list
19814 was erroneous or because we are replacing those tokens
19815 with a CPP_TEMPLATE_ID token. The second `>' (which will
19816 not have been thrown away) is needed either to close an
19817 outer template argument list or to complete a new-style
19818 cast. */
19819 cp_token *token = cp_lexer_peek_token (parser->lexer);
19820 token->type = CPP_GREATER;
19821 }
19822 else if (!saved_greater_than_is_operator_p)
19823 {
19824 /* If we're in a nested template argument list, the '>>' has
19825 to be a typo for '> >'. We emit the error message, but we
19826 continue parsing and we push a '>' as next token, so that
19827 the argument list will be parsed correctly. Note that the
19828 global source location is still on the token before the
19829 '>>', so we need to say explicitly where we want it. */
19830 cp_token *token = cp_lexer_peek_token (parser->lexer);
19831 error_at (token->location, "%<>>%> should be %<> >%> "
19832 "within a nested template argument list");
19833
19834 token->type = CPP_GREATER;
19835 }
19836 else
19837 {
19838 /* If this is not a nested template argument list, the '>>'
19839 is a typo for '>'. Emit an error message and continue.
19840 Same deal about the token location, but here we can get it
19841 right by consuming the '>>' before issuing the diagnostic. */
19842 cp_token *token = cp_lexer_consume_token (parser->lexer);
19843 error_at (token->location,
19844 "spurious %<>>%>, use %<>%> to terminate "
19845 "a template argument list");
19846 }
19847 }
19848 else
19849 cp_parser_skip_to_end_of_template_parameter_list (parser);
19850 /* The `>' token might be a greater-than operator again now. */
19851 parser->greater_than_is_operator_p
19852 = saved_greater_than_is_operator_p;
19853 /* Restore the SAVED_SCOPE. */
19854 parser->scope = saved_scope;
19855 parser->qualifying_scope = saved_qualifying_scope;
19856 parser->object_scope = saved_object_scope;
19857 cp_unevaluated_operand = saved_unevaluated_operand;
19858 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19859
19860 return arguments;
19861 }
19862
19863 /* MEMBER_FUNCTION is a member function, or a friend. If default
19864 arguments, or the body of the function have not yet been parsed,
19865 parse them now. */
19866
19867 static void
19868 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19869 {
19870 /* If this member is a template, get the underlying
19871 FUNCTION_DECL. */
19872 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19873 member_function = DECL_TEMPLATE_RESULT (member_function);
19874
19875 /* There should not be any class definitions in progress at this
19876 point; the bodies of members are only parsed outside of all class
19877 definitions. */
19878 gcc_assert (parser->num_classes_being_defined == 0);
19879 /* While we're parsing the member functions we might encounter more
19880 classes. We want to handle them right away, but we don't want
19881 them getting mixed up with functions that are currently in the
19882 queue. */
19883 push_unparsed_function_queues (parser);
19884
19885 /* Make sure that any template parameters are in scope. */
19886 maybe_begin_member_template_processing (member_function);
19887
19888 /* If the body of the function has not yet been parsed, parse it
19889 now. */
19890 if (DECL_PENDING_INLINE_P (member_function))
19891 {
19892 tree function_scope;
19893 cp_token_cache *tokens;
19894
19895 /* The function is no longer pending; we are processing it. */
19896 tokens = DECL_PENDING_INLINE_INFO (member_function);
19897 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19898 DECL_PENDING_INLINE_P (member_function) = 0;
19899
19900 /* If this is a local class, enter the scope of the containing
19901 function. */
19902 function_scope = current_function_decl;
19903 if (function_scope)
19904 push_function_context ();
19905
19906 /* Push the body of the function onto the lexer stack. */
19907 cp_parser_push_lexer_for_tokens (parser, tokens);
19908
19909 /* Let the front end know that we going to be defining this
19910 function. */
19911 start_preparsed_function (member_function, NULL_TREE,
19912 SF_PRE_PARSED | SF_INCLASS_INLINE);
19913
19914 /* Don't do access checking if it is a templated function. */
19915 if (processing_template_decl)
19916 push_deferring_access_checks (dk_no_check);
19917
19918 /* Now, parse the body of the function. */
19919 cp_parser_function_definition_after_declarator (parser,
19920 /*inline_p=*/true);
19921
19922 if (processing_template_decl)
19923 pop_deferring_access_checks ();
19924
19925 /* Leave the scope of the containing function. */
19926 if (function_scope)
19927 pop_function_context ();
19928 cp_parser_pop_lexer (parser);
19929 }
19930
19931 /* Remove any template parameters from the symbol table. */
19932 maybe_end_member_template_processing ();
19933
19934 /* Restore the queue. */
19935 pop_unparsed_function_queues (parser);
19936 }
19937
19938 /* If DECL contains any default args, remember it on the unparsed
19939 functions queue. */
19940
19941 static void
19942 cp_parser_save_default_args (cp_parser* parser, tree decl)
19943 {
19944 tree probe;
19945
19946 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19947 probe;
19948 probe = TREE_CHAIN (probe))
19949 if (TREE_PURPOSE (probe))
19950 {
19951 cp_default_arg_entry *entry
19952 = VEC_safe_push (cp_default_arg_entry, gc,
19953 unparsed_funs_with_default_args, NULL);
19954 entry->class_type = current_class_type;
19955 entry->decl = decl;
19956 break;
19957 }
19958 }
19959
19960 /* FN is a FUNCTION_DECL which may contains a parameter with an
19961 unparsed DEFAULT_ARG. Parse the default args now. This function
19962 assumes that the current scope is the scope in which the default
19963 argument should be processed. */
19964
19965 static void
19966 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19967 {
19968 bool saved_local_variables_forbidden_p;
19969 tree parm, parmdecl;
19970
19971 /* While we're parsing the default args, we might (due to the
19972 statement expression extension) encounter more classes. We want
19973 to handle them right away, but we don't want them getting mixed
19974 up with default args that are currently in the queue. */
19975 push_unparsed_function_queues (parser);
19976
19977 /* Local variable names (and the `this' keyword) may not appear
19978 in a default argument. */
19979 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19980 parser->local_variables_forbidden_p = true;
19981
19982 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19983 parmdecl = DECL_ARGUMENTS (fn);
19984 parm && parm != void_list_node;
19985 parm = TREE_CHAIN (parm),
19986 parmdecl = DECL_CHAIN (parmdecl))
19987 {
19988 cp_token_cache *tokens;
19989 tree default_arg = TREE_PURPOSE (parm);
19990 tree parsed_arg;
19991 VEC(tree,gc) *insts;
19992 tree copy;
19993 unsigned ix;
19994
19995 if (!default_arg)
19996 continue;
19997
19998 if (TREE_CODE (default_arg) != DEFAULT_ARG)
19999 /* This can happen for a friend declaration for a function
20000 already declared with default arguments. */
20001 continue;
20002
20003 /* Push the saved tokens for the default argument onto the parser's
20004 lexer stack. */
20005 tokens = DEFARG_TOKENS (default_arg);
20006 cp_parser_push_lexer_for_tokens (parser, tokens);
20007
20008 start_lambda_scope (parmdecl);
20009
20010 /* Parse the assignment-expression. */
20011 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20012 if (parsed_arg == error_mark_node)
20013 {
20014 cp_parser_pop_lexer (parser);
20015 continue;
20016 }
20017
20018 if (!processing_template_decl)
20019 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20020
20021 TREE_PURPOSE (parm) = parsed_arg;
20022
20023 /* Update any instantiations we've already created. */
20024 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20025 VEC_iterate (tree, insts, ix, copy); ix++)
20026 TREE_PURPOSE (copy) = parsed_arg;
20027
20028 finish_lambda_scope ();
20029
20030 /* If the token stream has not been completely used up, then
20031 there was extra junk after the end of the default
20032 argument. */
20033 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20034 cp_parser_error (parser, "expected %<,%>");
20035
20036 /* Revert to the main lexer. */
20037 cp_parser_pop_lexer (parser);
20038 }
20039
20040 /* Make sure no default arg is missing. */
20041 check_default_args (fn);
20042
20043 /* Restore the state of local_variables_forbidden_p. */
20044 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20045
20046 /* Restore the queue. */
20047 pop_unparsed_function_queues (parser);
20048 }
20049
20050 /* Parse the operand of `sizeof' (or a similar operator). Returns
20051 either a TYPE or an expression, depending on the form of the
20052 input. The KEYWORD indicates which kind of expression we have
20053 encountered. */
20054
20055 static tree
20056 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20057 {
20058 tree expr = NULL_TREE;
20059 const char *saved_message;
20060 char *tmp;
20061 bool saved_integral_constant_expression_p;
20062 bool saved_non_integral_constant_expression_p;
20063 bool pack_expansion_p = false;
20064
20065 /* Types cannot be defined in a `sizeof' expression. Save away the
20066 old message. */
20067 saved_message = parser->type_definition_forbidden_message;
20068 /* And create the new one. */
20069 tmp = concat ("types may not be defined in %<",
20070 IDENTIFIER_POINTER (ridpointers[keyword]),
20071 "%> expressions", NULL);
20072 parser->type_definition_forbidden_message = tmp;
20073
20074 /* The restrictions on constant-expressions do not apply inside
20075 sizeof expressions. */
20076 saved_integral_constant_expression_p
20077 = parser->integral_constant_expression_p;
20078 saved_non_integral_constant_expression_p
20079 = parser->non_integral_constant_expression_p;
20080 parser->integral_constant_expression_p = false;
20081
20082 /* If it's a `...', then we are computing the length of a parameter
20083 pack. */
20084 if (keyword == RID_SIZEOF
20085 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20086 {
20087 /* Consume the `...'. */
20088 cp_lexer_consume_token (parser->lexer);
20089 maybe_warn_variadic_templates ();
20090
20091 /* Note that this is an expansion. */
20092 pack_expansion_p = true;
20093 }
20094
20095 /* Do not actually evaluate the expression. */
20096 ++cp_unevaluated_operand;
20097 ++c_inhibit_evaluation_warnings;
20098 /* If it's a `(', then we might be looking at the type-id
20099 construction. */
20100 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20101 {
20102 tree type;
20103 bool saved_in_type_id_in_expr_p;
20104
20105 /* We can't be sure yet whether we're looking at a type-id or an
20106 expression. */
20107 cp_parser_parse_tentatively (parser);
20108 /* Consume the `('. */
20109 cp_lexer_consume_token (parser->lexer);
20110 /* Parse the type-id. */
20111 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20112 parser->in_type_id_in_expr_p = true;
20113 type = cp_parser_type_id (parser);
20114 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20115 /* Now, look for the trailing `)'. */
20116 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20117 /* If all went well, then we're done. */
20118 if (cp_parser_parse_definitely (parser))
20119 {
20120 cp_decl_specifier_seq decl_specs;
20121
20122 /* Build a trivial decl-specifier-seq. */
20123 clear_decl_specs (&decl_specs);
20124 decl_specs.type = type;
20125
20126 /* Call grokdeclarator to figure out what type this is. */
20127 expr = grokdeclarator (NULL,
20128 &decl_specs,
20129 TYPENAME,
20130 /*initialized=*/0,
20131 /*attrlist=*/NULL);
20132 }
20133 }
20134
20135 /* If the type-id production did not work out, then we must be
20136 looking at the unary-expression production. */
20137 if (!expr)
20138 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20139 /*cast_p=*/false, NULL);
20140
20141 if (pack_expansion_p)
20142 /* Build a pack expansion. */
20143 expr = make_pack_expansion (expr);
20144
20145 /* Go back to evaluating expressions. */
20146 --cp_unevaluated_operand;
20147 --c_inhibit_evaluation_warnings;
20148
20149 /* Free the message we created. */
20150 free (tmp);
20151 /* And restore the old one. */
20152 parser->type_definition_forbidden_message = saved_message;
20153 parser->integral_constant_expression_p
20154 = saved_integral_constant_expression_p;
20155 parser->non_integral_constant_expression_p
20156 = saved_non_integral_constant_expression_p;
20157
20158 return expr;
20159 }
20160
20161 /* If the current declaration has no declarator, return true. */
20162
20163 static bool
20164 cp_parser_declares_only_class_p (cp_parser *parser)
20165 {
20166 /* If the next token is a `;' or a `,' then there is no
20167 declarator. */
20168 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20169 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20170 }
20171
20172 /* Update the DECL_SPECS to reflect the storage class indicated by
20173 KEYWORD. */
20174
20175 static void
20176 cp_parser_set_storage_class (cp_parser *parser,
20177 cp_decl_specifier_seq *decl_specs,
20178 enum rid keyword,
20179 location_t location)
20180 {
20181 cp_storage_class storage_class;
20182
20183 if (parser->in_unbraced_linkage_specification_p)
20184 {
20185 error_at (location, "invalid use of %qD in linkage specification",
20186 ridpointers[keyword]);
20187 return;
20188 }
20189 else if (decl_specs->storage_class != sc_none)
20190 {
20191 decl_specs->conflicting_specifiers_p = true;
20192 return;
20193 }
20194
20195 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20196 && decl_specs->specs[(int) ds_thread])
20197 {
20198 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20199 decl_specs->specs[(int) ds_thread] = 0;
20200 }
20201
20202 switch (keyword)
20203 {
20204 case RID_AUTO:
20205 storage_class = sc_auto;
20206 break;
20207 case RID_REGISTER:
20208 storage_class = sc_register;
20209 break;
20210 case RID_STATIC:
20211 storage_class = sc_static;
20212 break;
20213 case RID_EXTERN:
20214 storage_class = sc_extern;
20215 break;
20216 case RID_MUTABLE:
20217 storage_class = sc_mutable;
20218 break;
20219 default:
20220 gcc_unreachable ();
20221 }
20222 decl_specs->storage_class = storage_class;
20223
20224 /* A storage class specifier cannot be applied alongside a typedef
20225 specifier. If there is a typedef specifier present then set
20226 conflicting_specifiers_p which will trigger an error later
20227 on in grokdeclarator. */
20228 if (decl_specs->specs[(int)ds_typedef])
20229 decl_specs->conflicting_specifiers_p = true;
20230 }
20231
20232 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20233 is true, the type is a user-defined type; otherwise it is a
20234 built-in type specified by a keyword. */
20235
20236 static void
20237 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20238 tree type_spec,
20239 location_t location,
20240 bool user_defined_p)
20241 {
20242 decl_specs->any_specifiers_p = true;
20243
20244 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20245 (with, for example, in "typedef int wchar_t;") we remember that
20246 this is what happened. In system headers, we ignore these
20247 declarations so that G++ can work with system headers that are not
20248 C++-safe. */
20249 if (decl_specs->specs[(int) ds_typedef]
20250 && !user_defined_p
20251 && (type_spec == boolean_type_node
20252 || type_spec == char16_type_node
20253 || type_spec == char32_type_node
20254 || type_spec == wchar_type_node)
20255 && (decl_specs->type
20256 || decl_specs->specs[(int) ds_long]
20257 || decl_specs->specs[(int) ds_short]
20258 || decl_specs->specs[(int) ds_unsigned]
20259 || decl_specs->specs[(int) ds_signed]))
20260 {
20261 decl_specs->redefined_builtin_type = type_spec;
20262 if (!decl_specs->type)
20263 {
20264 decl_specs->type = type_spec;
20265 decl_specs->user_defined_type_p = false;
20266 decl_specs->type_location = location;
20267 }
20268 }
20269 else if (decl_specs->type)
20270 decl_specs->multiple_types_p = true;
20271 else
20272 {
20273 decl_specs->type = type_spec;
20274 decl_specs->user_defined_type_p = user_defined_p;
20275 decl_specs->redefined_builtin_type = NULL_TREE;
20276 decl_specs->type_location = location;
20277 }
20278 }
20279
20280 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20281 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20282
20283 static bool
20284 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20285 {
20286 return decl_specifiers->specs[(int) ds_friend] != 0;
20287 }
20288
20289 /* Issue an error message indicating that TOKEN_DESC was expected.
20290 If KEYWORD is true, it indicated this function is called by
20291 cp_parser_require_keword and the required token can only be
20292 a indicated keyword. */
20293
20294 static void
20295 cp_parser_required_error (cp_parser *parser,
20296 required_token token_desc,
20297 bool keyword)
20298 {
20299 switch (token_desc)
20300 {
20301 case RT_NEW:
20302 cp_parser_error (parser, "expected %<new%>");
20303 return;
20304 case RT_DELETE:
20305 cp_parser_error (parser, "expected %<delete%>");
20306 return;
20307 case RT_RETURN:
20308 cp_parser_error (parser, "expected %<return%>");
20309 return;
20310 case RT_WHILE:
20311 cp_parser_error (parser, "expected %<while%>");
20312 return;
20313 case RT_EXTERN:
20314 cp_parser_error (parser, "expected %<extern%>");
20315 return;
20316 case RT_STATIC_ASSERT:
20317 cp_parser_error (parser, "expected %<static_assert%>");
20318 return;
20319 case RT_DECLTYPE:
20320 cp_parser_error (parser, "expected %<decltype%>");
20321 return;
20322 case RT_OPERATOR:
20323 cp_parser_error (parser, "expected %<operator%>");
20324 return;
20325 case RT_CLASS:
20326 cp_parser_error (parser, "expected %<class%>");
20327 return;
20328 case RT_TEMPLATE:
20329 cp_parser_error (parser, "expected %<template%>");
20330 return;
20331 case RT_NAMESPACE:
20332 cp_parser_error (parser, "expected %<namespace%>");
20333 return;
20334 case RT_USING:
20335 cp_parser_error (parser, "expected %<using%>");
20336 return;
20337 case RT_ASM:
20338 cp_parser_error (parser, "expected %<asm%>");
20339 return;
20340 case RT_TRY:
20341 cp_parser_error (parser, "expected %<try%>");
20342 return;
20343 case RT_CATCH:
20344 cp_parser_error (parser, "expected %<catch%>");
20345 return;
20346 case RT_THROW:
20347 cp_parser_error (parser, "expected %<throw%>");
20348 return;
20349 case RT_LABEL:
20350 cp_parser_error (parser, "expected %<__label__%>");
20351 return;
20352 case RT_AT_TRY:
20353 cp_parser_error (parser, "expected %<@try%>");
20354 return;
20355 case RT_AT_SYNCHRONIZED:
20356 cp_parser_error (parser, "expected %<@synchronized%>");
20357 return;
20358 case RT_AT_THROW:
20359 cp_parser_error (parser, "expected %<@throw%>");
20360 return;
20361 default:
20362 break;
20363 }
20364 if (!keyword)
20365 {
20366 switch (token_desc)
20367 {
20368 case RT_SEMICOLON:
20369 cp_parser_error (parser, "expected %<;%>");
20370 return;
20371 case RT_OPEN_PAREN:
20372 cp_parser_error (parser, "expected %<(%>");
20373 return;
20374 case RT_CLOSE_BRACE:
20375 cp_parser_error (parser, "expected %<}%>");
20376 return;
20377 case RT_OPEN_BRACE:
20378 cp_parser_error (parser, "expected %<{%>");
20379 return;
20380 case RT_CLOSE_SQUARE:
20381 cp_parser_error (parser, "expected %<]%>");
20382 return;
20383 case RT_OPEN_SQUARE:
20384 cp_parser_error (parser, "expected %<[%>");
20385 return;
20386 case RT_COMMA:
20387 cp_parser_error (parser, "expected %<,%>");
20388 return;
20389 case RT_SCOPE:
20390 cp_parser_error (parser, "expected %<::%>");
20391 return;
20392 case RT_LESS:
20393 cp_parser_error (parser, "expected %<<%>");
20394 return;
20395 case RT_GREATER:
20396 cp_parser_error (parser, "expected %<>%>");
20397 return;
20398 case RT_EQ:
20399 cp_parser_error (parser, "expected %<=%>");
20400 return;
20401 case RT_ELLIPSIS:
20402 cp_parser_error (parser, "expected %<...%>");
20403 return;
20404 case RT_MULT:
20405 cp_parser_error (parser, "expected %<*%>");
20406 return;
20407 case RT_COMPL:
20408 cp_parser_error (parser, "expected %<~%>");
20409 return;
20410 case RT_COLON:
20411 cp_parser_error (parser, "expected %<:%>");
20412 return;
20413 case RT_COLON_SCOPE:
20414 cp_parser_error (parser, "expected %<:%> or %<::%>");
20415 return;
20416 case RT_CLOSE_PAREN:
20417 cp_parser_error (parser, "expected %<)%>");
20418 return;
20419 case RT_COMMA_CLOSE_PAREN:
20420 cp_parser_error (parser, "expected %<,%> or %<)%>");
20421 return;
20422 case RT_PRAGMA_EOL:
20423 cp_parser_error (parser, "expected end of line");
20424 return;
20425 case RT_NAME:
20426 cp_parser_error (parser, "expected identifier");
20427 return;
20428 case RT_SELECT:
20429 cp_parser_error (parser, "expected selection-statement");
20430 return;
20431 case RT_INTERATION:
20432 cp_parser_error (parser, "expected iteration-statement");
20433 return;
20434 case RT_JUMP:
20435 cp_parser_error (parser, "expected jump-statement");
20436 return;
20437 case RT_CLASS_KEY:
20438 cp_parser_error (parser, "expected class-key");
20439 return;
20440 case RT_CLASS_TYPENAME_TEMPLATE:
20441 cp_parser_error (parser,
20442 "expected %<class%>, %<typename%>, or %<template%>");
20443 return;
20444 default:
20445 gcc_unreachable ();
20446 }
20447 }
20448 else
20449 gcc_unreachable ();
20450 }
20451
20452
20453
20454 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20455 issue an error message indicating that TOKEN_DESC was expected.
20456
20457 Returns the token consumed, if the token had the appropriate type.
20458 Otherwise, returns NULL. */
20459
20460 static cp_token *
20461 cp_parser_require (cp_parser* parser,
20462 enum cpp_ttype type,
20463 required_token token_desc)
20464 {
20465 if (cp_lexer_next_token_is (parser->lexer, type))
20466 return cp_lexer_consume_token (parser->lexer);
20467 else
20468 {
20469 /* Output the MESSAGE -- unless we're parsing tentatively. */
20470 if (!cp_parser_simulate_error (parser))
20471 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20472 return NULL;
20473 }
20474 }
20475
20476 /* An error message is produced if the next token is not '>'.
20477 All further tokens are skipped until the desired token is
20478 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20479
20480 static void
20481 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20482 {
20483 /* Current level of '< ... >'. */
20484 unsigned level = 0;
20485 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20486 unsigned nesting_depth = 0;
20487
20488 /* Are we ready, yet? If not, issue error message. */
20489 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20490 return;
20491
20492 /* Skip tokens until the desired token is found. */
20493 while (true)
20494 {
20495 /* Peek at the next token. */
20496 switch (cp_lexer_peek_token (parser->lexer)->type)
20497 {
20498 case CPP_LESS:
20499 if (!nesting_depth)
20500 ++level;
20501 break;
20502
20503 case CPP_RSHIFT:
20504 if (cxx_dialect == cxx98)
20505 /* C++0x views the `>>' operator as two `>' tokens, but
20506 C++98 does not. */
20507 break;
20508 else if (!nesting_depth && level-- == 0)
20509 {
20510 /* We've hit a `>>' where the first `>' closes the
20511 template argument list, and the second `>' is
20512 spurious. Just consume the `>>' and stop; we've
20513 already produced at least one error. */
20514 cp_lexer_consume_token (parser->lexer);
20515 return;
20516 }
20517 /* Fall through for C++0x, so we handle the second `>' in
20518 the `>>'. */
20519
20520 case CPP_GREATER:
20521 if (!nesting_depth && level-- == 0)
20522 {
20523 /* We've reached the token we want, consume it and stop. */
20524 cp_lexer_consume_token (parser->lexer);
20525 return;
20526 }
20527 break;
20528
20529 case CPP_OPEN_PAREN:
20530 case CPP_OPEN_SQUARE:
20531 ++nesting_depth;
20532 break;
20533
20534 case CPP_CLOSE_PAREN:
20535 case CPP_CLOSE_SQUARE:
20536 if (nesting_depth-- == 0)
20537 return;
20538 break;
20539
20540 case CPP_EOF:
20541 case CPP_PRAGMA_EOL:
20542 case CPP_SEMICOLON:
20543 case CPP_OPEN_BRACE:
20544 case CPP_CLOSE_BRACE:
20545 /* The '>' was probably forgotten, don't look further. */
20546 return;
20547
20548 default:
20549 break;
20550 }
20551
20552 /* Consume this token. */
20553 cp_lexer_consume_token (parser->lexer);
20554 }
20555 }
20556
20557 /* If the next token is the indicated keyword, consume it. Otherwise,
20558 issue an error message indicating that TOKEN_DESC was expected.
20559
20560 Returns the token consumed, if the token had the appropriate type.
20561 Otherwise, returns NULL. */
20562
20563 static cp_token *
20564 cp_parser_require_keyword (cp_parser* parser,
20565 enum rid keyword,
20566 required_token token_desc)
20567 {
20568 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20569
20570 if (token && token->keyword != keyword)
20571 {
20572 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
20573 return NULL;
20574 }
20575
20576 return token;
20577 }
20578
20579 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20580 function-definition. */
20581
20582 static bool
20583 cp_parser_token_starts_function_definition_p (cp_token* token)
20584 {
20585 return (/* An ordinary function-body begins with an `{'. */
20586 token->type == CPP_OPEN_BRACE
20587 /* A ctor-initializer begins with a `:'. */
20588 || token->type == CPP_COLON
20589 /* A function-try-block begins with `try'. */
20590 || token->keyword == RID_TRY
20591 /* The named return value extension begins with `return'. */
20592 || token->keyword == RID_RETURN);
20593 }
20594
20595 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
20596 definition. */
20597
20598 static bool
20599 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
20600 {
20601 cp_token *token;
20602
20603 token = cp_lexer_peek_token (parser->lexer);
20604 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
20605 }
20606
20607 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
20608 C++0x) ending a template-argument. */
20609
20610 static bool
20611 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
20612 {
20613 cp_token *token;
20614
20615 token = cp_lexer_peek_token (parser->lexer);
20616 return (token->type == CPP_COMMA
20617 || token->type == CPP_GREATER
20618 || token->type == CPP_ELLIPSIS
20619 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
20620 }
20621
20622 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
20623 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
20624
20625 static bool
20626 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
20627 size_t n)
20628 {
20629 cp_token *token;
20630
20631 token = cp_lexer_peek_nth_token (parser->lexer, n);
20632 if (token->type == CPP_LESS)
20633 return true;
20634 /* Check for the sequence `<::' in the original code. It would be lexed as
20635 `[:', where `[' is a digraph, and there is no whitespace before
20636 `:'. */
20637 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
20638 {
20639 cp_token *token2;
20640 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
20641 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
20642 return true;
20643 }
20644 return false;
20645 }
20646
20647 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
20648 or none_type otherwise. */
20649
20650 static enum tag_types
20651 cp_parser_token_is_class_key (cp_token* token)
20652 {
20653 switch (token->keyword)
20654 {
20655 case RID_CLASS:
20656 return class_type;
20657 case RID_STRUCT:
20658 return record_type;
20659 case RID_UNION:
20660 return union_type;
20661
20662 default:
20663 return none_type;
20664 }
20665 }
20666
20667 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
20668
20669 static void
20670 cp_parser_check_class_key (enum tag_types class_key, tree type)
20671 {
20672 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
20673 permerror (input_location, "%qs tag used in naming %q#T",
20674 class_key == union_type ? "union"
20675 : class_key == record_type ? "struct" : "class",
20676 type);
20677 }
20678
20679 /* Issue an error message if DECL is redeclared with different
20680 access than its original declaration [class.access.spec/3].
20681 This applies to nested classes and nested class templates.
20682 [class.mem/1]. */
20683
20684 static void
20685 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
20686 {
20687 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
20688 return;
20689
20690 if ((TREE_PRIVATE (decl)
20691 != (current_access_specifier == access_private_node))
20692 || (TREE_PROTECTED (decl)
20693 != (current_access_specifier == access_protected_node)))
20694 error_at (location, "%qD redeclared with different access", decl);
20695 }
20696
20697 /* Look for the `template' keyword, as a syntactic disambiguator.
20698 Return TRUE iff it is present, in which case it will be
20699 consumed. */
20700
20701 static bool
20702 cp_parser_optional_template_keyword (cp_parser *parser)
20703 {
20704 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20705 {
20706 /* The `template' keyword can only be used within templates;
20707 outside templates the parser can always figure out what is a
20708 template and what is not. */
20709 if (!processing_template_decl)
20710 {
20711 cp_token *token = cp_lexer_peek_token (parser->lexer);
20712 error_at (token->location,
20713 "%<template%> (as a disambiguator) is only allowed "
20714 "within templates");
20715 /* If this part of the token stream is rescanned, the same
20716 error message would be generated. So, we purge the token
20717 from the stream. */
20718 cp_lexer_purge_token (parser->lexer);
20719 return false;
20720 }
20721 else
20722 {
20723 /* Consume the `template' keyword. */
20724 cp_lexer_consume_token (parser->lexer);
20725 return true;
20726 }
20727 }
20728
20729 return false;
20730 }
20731
20732 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
20733 set PARSER->SCOPE, and perform other related actions. */
20734
20735 static void
20736 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
20737 {
20738 int i;
20739 struct tree_check *check_value;
20740 deferred_access_check *chk;
20741 VEC (deferred_access_check,gc) *checks;
20742
20743 /* Get the stored value. */
20744 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
20745 /* Perform any access checks that were deferred. */
20746 checks = check_value->checks;
20747 if (checks)
20748 {
20749 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
20750 perform_or_defer_access_check (chk->binfo,
20751 chk->decl,
20752 chk->diag_decl);
20753 }
20754 /* Set the scope from the stored value. */
20755 parser->scope = check_value->value;
20756 parser->qualifying_scope = check_value->qualifying_scope;
20757 parser->object_scope = NULL_TREE;
20758 }
20759
20760 /* Consume tokens up through a non-nested END token. Returns TRUE if we
20761 encounter the end of a block before what we were looking for. */
20762
20763 static bool
20764 cp_parser_cache_group (cp_parser *parser,
20765 enum cpp_ttype end,
20766 unsigned depth)
20767 {
20768 while (true)
20769 {
20770 cp_token *token = cp_lexer_peek_token (parser->lexer);
20771
20772 /* Abort a parenthesized expression if we encounter a semicolon. */
20773 if ((end == CPP_CLOSE_PAREN || depth == 0)
20774 && token->type == CPP_SEMICOLON)
20775 return true;
20776 /* If we've reached the end of the file, stop. */
20777 if (token->type == CPP_EOF
20778 || (end != CPP_PRAGMA_EOL
20779 && token->type == CPP_PRAGMA_EOL))
20780 return true;
20781 if (token->type == CPP_CLOSE_BRACE && depth == 0)
20782 /* We've hit the end of an enclosing block, so there's been some
20783 kind of syntax error. */
20784 return true;
20785
20786 /* Consume the token. */
20787 cp_lexer_consume_token (parser->lexer);
20788 /* See if it starts a new group. */
20789 if (token->type == CPP_OPEN_BRACE)
20790 {
20791 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
20792 /* In theory this should probably check end == '}', but
20793 cp_parser_save_member_function_body needs it to exit
20794 after either '}' or ')' when called with ')'. */
20795 if (depth == 0)
20796 return false;
20797 }
20798 else if (token->type == CPP_OPEN_PAREN)
20799 {
20800 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
20801 if (depth == 0 && end == CPP_CLOSE_PAREN)
20802 return false;
20803 }
20804 else if (token->type == CPP_PRAGMA)
20805 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
20806 else if (token->type == end)
20807 return false;
20808 }
20809 }
20810
20811 /* Begin parsing tentatively. We always save tokens while parsing
20812 tentatively so that if the tentative parsing fails we can restore the
20813 tokens. */
20814
20815 static void
20816 cp_parser_parse_tentatively (cp_parser* parser)
20817 {
20818 /* Enter a new parsing context. */
20819 parser->context = cp_parser_context_new (parser->context);
20820 /* Begin saving tokens. */
20821 cp_lexer_save_tokens (parser->lexer);
20822 /* In order to avoid repetitive access control error messages,
20823 access checks are queued up until we are no longer parsing
20824 tentatively. */
20825 push_deferring_access_checks (dk_deferred);
20826 }
20827
20828 /* Commit to the currently active tentative parse. */
20829
20830 static void
20831 cp_parser_commit_to_tentative_parse (cp_parser* parser)
20832 {
20833 cp_parser_context *context;
20834 cp_lexer *lexer;
20835
20836 /* Mark all of the levels as committed. */
20837 lexer = parser->lexer;
20838 for (context = parser->context; context->next; context = context->next)
20839 {
20840 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
20841 break;
20842 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
20843 while (!cp_lexer_saving_tokens (lexer))
20844 lexer = lexer->next;
20845 cp_lexer_commit_tokens (lexer);
20846 }
20847 }
20848
20849 /* Abort the currently active tentative parse. All consumed tokens
20850 will be rolled back, and no diagnostics will be issued. */
20851
20852 static void
20853 cp_parser_abort_tentative_parse (cp_parser* parser)
20854 {
20855 cp_parser_simulate_error (parser);
20856 /* Now, pretend that we want to see if the construct was
20857 successfully parsed. */
20858 cp_parser_parse_definitely (parser);
20859 }
20860
20861 /* Stop parsing tentatively. If a parse error has occurred, restore the
20862 token stream. Otherwise, commit to the tokens we have consumed.
20863 Returns true if no error occurred; false otherwise. */
20864
20865 static bool
20866 cp_parser_parse_definitely (cp_parser* parser)
20867 {
20868 bool error_occurred;
20869 cp_parser_context *context;
20870
20871 /* Remember whether or not an error occurred, since we are about to
20872 destroy that information. */
20873 error_occurred = cp_parser_error_occurred (parser);
20874 /* Remove the topmost context from the stack. */
20875 context = parser->context;
20876 parser->context = context->next;
20877 /* If no parse errors occurred, commit to the tentative parse. */
20878 if (!error_occurred)
20879 {
20880 /* Commit to the tokens read tentatively, unless that was
20881 already done. */
20882 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20883 cp_lexer_commit_tokens (parser->lexer);
20884
20885 pop_to_parent_deferring_access_checks ();
20886 }
20887 /* Otherwise, if errors occurred, roll back our state so that things
20888 are just as they were before we began the tentative parse. */
20889 else
20890 {
20891 cp_lexer_rollback_tokens (parser->lexer);
20892 pop_deferring_access_checks ();
20893 }
20894 /* Add the context to the front of the free list. */
20895 context->next = cp_parser_context_free_list;
20896 cp_parser_context_free_list = context;
20897
20898 return !error_occurred;
20899 }
20900
20901 /* Returns true if we are parsing tentatively and are not committed to
20902 this tentative parse. */
20903
20904 static bool
20905 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20906 {
20907 return (cp_parser_parsing_tentatively (parser)
20908 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20909 }
20910
20911 /* Returns nonzero iff an error has occurred during the most recent
20912 tentative parse. */
20913
20914 static bool
20915 cp_parser_error_occurred (cp_parser* parser)
20916 {
20917 return (cp_parser_parsing_tentatively (parser)
20918 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20919 }
20920
20921 /* Returns nonzero if GNU extensions are allowed. */
20922
20923 static bool
20924 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20925 {
20926 return parser->allow_gnu_extensions_p;
20927 }
20928 \f
20929 /* Objective-C++ Productions */
20930
20931
20932 /* Parse an Objective-C expression, which feeds into a primary-expression
20933 above.
20934
20935 objc-expression:
20936 objc-message-expression
20937 objc-string-literal
20938 objc-encode-expression
20939 objc-protocol-expression
20940 objc-selector-expression
20941
20942 Returns a tree representation of the expression. */
20943
20944 static tree
20945 cp_parser_objc_expression (cp_parser* parser)
20946 {
20947 /* Try to figure out what kind of declaration is present. */
20948 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20949
20950 switch (kwd->type)
20951 {
20952 case CPP_OPEN_SQUARE:
20953 return cp_parser_objc_message_expression (parser);
20954
20955 case CPP_OBJC_STRING:
20956 kwd = cp_lexer_consume_token (parser->lexer);
20957 return objc_build_string_object (kwd->u.value);
20958
20959 case CPP_KEYWORD:
20960 switch (kwd->keyword)
20961 {
20962 case RID_AT_ENCODE:
20963 return cp_parser_objc_encode_expression (parser);
20964
20965 case RID_AT_PROTOCOL:
20966 return cp_parser_objc_protocol_expression (parser);
20967
20968 case RID_AT_SELECTOR:
20969 return cp_parser_objc_selector_expression (parser);
20970
20971 default:
20972 break;
20973 }
20974 default:
20975 error_at (kwd->location,
20976 "misplaced %<@%D%> Objective-C++ construct",
20977 kwd->u.value);
20978 cp_parser_skip_to_end_of_block_or_statement (parser);
20979 }
20980
20981 return error_mark_node;
20982 }
20983
20984 /* Parse an Objective-C message expression.
20985
20986 objc-message-expression:
20987 [ objc-message-receiver objc-message-args ]
20988
20989 Returns a representation of an Objective-C message. */
20990
20991 static tree
20992 cp_parser_objc_message_expression (cp_parser* parser)
20993 {
20994 tree receiver, messageargs;
20995
20996 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
20997 receiver = cp_parser_objc_message_receiver (parser);
20998 messageargs = cp_parser_objc_message_args (parser);
20999 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21000
21001 return objc_build_message_expr (build_tree_list (receiver, messageargs));
21002 }
21003
21004 /* Parse an objc-message-receiver.
21005
21006 objc-message-receiver:
21007 expression
21008 simple-type-specifier
21009
21010 Returns a representation of the type or expression. */
21011
21012 static tree
21013 cp_parser_objc_message_receiver (cp_parser* parser)
21014 {
21015 tree rcv;
21016
21017 /* An Objective-C message receiver may be either (1) a type
21018 or (2) an expression. */
21019 cp_parser_parse_tentatively (parser);
21020 rcv = cp_parser_expression (parser, false, NULL);
21021
21022 if (cp_parser_parse_definitely (parser))
21023 return rcv;
21024
21025 rcv = cp_parser_simple_type_specifier (parser,
21026 /*decl_specs=*/NULL,
21027 CP_PARSER_FLAGS_NONE);
21028
21029 return objc_get_class_reference (rcv);
21030 }
21031
21032 /* Parse the arguments and selectors comprising an Objective-C message.
21033
21034 objc-message-args:
21035 objc-selector
21036 objc-selector-args
21037 objc-selector-args , objc-comma-args
21038
21039 objc-selector-args:
21040 objc-selector [opt] : assignment-expression
21041 objc-selector-args objc-selector [opt] : assignment-expression
21042
21043 objc-comma-args:
21044 assignment-expression
21045 objc-comma-args , assignment-expression
21046
21047 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21048 selector arguments and TREE_VALUE containing a list of comma
21049 arguments. */
21050
21051 static tree
21052 cp_parser_objc_message_args (cp_parser* parser)
21053 {
21054 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21055 bool maybe_unary_selector_p = true;
21056 cp_token *token = cp_lexer_peek_token (parser->lexer);
21057
21058 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21059 {
21060 tree selector = NULL_TREE, arg;
21061
21062 if (token->type != CPP_COLON)
21063 selector = cp_parser_objc_selector (parser);
21064
21065 /* Detect if we have a unary selector. */
21066 if (maybe_unary_selector_p
21067 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21068 return build_tree_list (selector, NULL_TREE);
21069
21070 maybe_unary_selector_p = false;
21071 cp_parser_require (parser, CPP_COLON, RT_COLON);
21072 arg = cp_parser_assignment_expression (parser, false, NULL);
21073
21074 sel_args
21075 = chainon (sel_args,
21076 build_tree_list (selector, arg));
21077
21078 token = cp_lexer_peek_token (parser->lexer);
21079 }
21080
21081 /* Handle non-selector arguments, if any. */
21082 while (token->type == CPP_COMMA)
21083 {
21084 tree arg;
21085
21086 cp_lexer_consume_token (parser->lexer);
21087 arg = cp_parser_assignment_expression (parser, false, NULL);
21088
21089 addl_args
21090 = chainon (addl_args,
21091 build_tree_list (NULL_TREE, arg));
21092
21093 token = cp_lexer_peek_token (parser->lexer);
21094 }
21095
21096 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21097 {
21098 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21099 return build_tree_list (error_mark_node, error_mark_node);
21100 }
21101
21102 return build_tree_list (sel_args, addl_args);
21103 }
21104
21105 /* Parse an Objective-C encode expression.
21106
21107 objc-encode-expression:
21108 @encode objc-typename
21109
21110 Returns an encoded representation of the type argument. */
21111
21112 static tree
21113 cp_parser_objc_encode_expression (cp_parser* parser)
21114 {
21115 tree type;
21116 cp_token *token;
21117
21118 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
21119 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21120 token = cp_lexer_peek_token (parser->lexer);
21121 type = complete_type (cp_parser_type_id (parser));
21122 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21123
21124 if (!type)
21125 {
21126 error_at (token->location,
21127 "%<@encode%> must specify a type as an argument");
21128 return error_mark_node;
21129 }
21130
21131 /* This happens if we find @encode(T) (where T is a template
21132 typename or something dependent on a template typename) when
21133 parsing a template. In that case, we can't compile it
21134 immediately, but we rather create an AT_ENCODE_EXPR which will
21135 need to be instantiated when the template is used.
21136 */
21137 if (dependent_type_p (type))
21138 {
21139 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21140 TREE_READONLY (value) = 1;
21141 return value;
21142 }
21143
21144 return objc_build_encode_expr (type);
21145 }
21146
21147 /* Parse an Objective-C @defs expression. */
21148
21149 static tree
21150 cp_parser_objc_defs_expression (cp_parser *parser)
21151 {
21152 tree name;
21153
21154 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
21155 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21156 name = cp_parser_identifier (parser);
21157 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21158
21159 return objc_get_class_ivars (name);
21160 }
21161
21162 /* Parse an Objective-C protocol expression.
21163
21164 objc-protocol-expression:
21165 @protocol ( identifier )
21166
21167 Returns a representation of the protocol expression. */
21168
21169 static tree
21170 cp_parser_objc_protocol_expression (cp_parser* parser)
21171 {
21172 tree proto;
21173
21174 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21175 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21176 proto = cp_parser_identifier (parser);
21177 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21178
21179 return objc_build_protocol_expr (proto);
21180 }
21181
21182 /* Parse an Objective-C selector expression.
21183
21184 objc-selector-expression:
21185 @selector ( objc-method-signature )
21186
21187 objc-method-signature:
21188 objc-selector
21189 objc-selector-seq
21190
21191 objc-selector-seq:
21192 objc-selector :
21193 objc-selector-seq objc-selector :
21194
21195 Returns a representation of the method selector. */
21196
21197 static tree
21198 cp_parser_objc_selector_expression (cp_parser* parser)
21199 {
21200 tree sel_seq = NULL_TREE;
21201 bool maybe_unary_selector_p = true;
21202 cp_token *token;
21203 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21204
21205 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
21206 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21207 token = cp_lexer_peek_token (parser->lexer);
21208
21209 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21210 || token->type == CPP_SCOPE)
21211 {
21212 tree selector = NULL_TREE;
21213
21214 if (token->type != CPP_COLON
21215 || token->type == CPP_SCOPE)
21216 selector = cp_parser_objc_selector (parser);
21217
21218 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21219 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21220 {
21221 /* Detect if we have a unary selector. */
21222 if (maybe_unary_selector_p)
21223 {
21224 sel_seq = selector;
21225 goto finish_selector;
21226 }
21227 else
21228 {
21229 cp_parser_error (parser, "expected %<:%>");
21230 }
21231 }
21232 maybe_unary_selector_p = false;
21233 token = cp_lexer_consume_token (parser->lexer);
21234
21235 if (token->type == CPP_SCOPE)
21236 {
21237 sel_seq
21238 = chainon (sel_seq,
21239 build_tree_list (selector, NULL_TREE));
21240 sel_seq
21241 = chainon (sel_seq,
21242 build_tree_list (NULL_TREE, NULL_TREE));
21243 }
21244 else
21245 sel_seq
21246 = chainon (sel_seq,
21247 build_tree_list (selector, NULL_TREE));
21248
21249 token = cp_lexer_peek_token (parser->lexer);
21250 }
21251
21252 finish_selector:
21253 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21254
21255 return objc_build_selector_expr (loc, sel_seq);
21256 }
21257
21258 /* Parse a list of identifiers.
21259
21260 objc-identifier-list:
21261 identifier
21262 objc-identifier-list , identifier
21263
21264 Returns a TREE_LIST of identifier nodes. */
21265
21266 static tree
21267 cp_parser_objc_identifier_list (cp_parser* parser)
21268 {
21269 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
21270 cp_token *sep = cp_lexer_peek_token (parser->lexer);
21271
21272 while (sep->type == CPP_COMMA)
21273 {
21274 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21275 list = chainon (list,
21276 build_tree_list (NULL_TREE,
21277 cp_parser_identifier (parser)));
21278 sep = cp_lexer_peek_token (parser->lexer);
21279 }
21280
21281 return list;
21282 }
21283
21284 /* Parse an Objective-C alias declaration.
21285
21286 objc-alias-declaration:
21287 @compatibility_alias identifier identifier ;
21288
21289 This function registers the alias mapping with the Objective-C front end.
21290 It returns nothing. */
21291
21292 static void
21293 cp_parser_objc_alias_declaration (cp_parser* parser)
21294 {
21295 tree alias, orig;
21296
21297 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
21298 alias = cp_parser_identifier (parser);
21299 orig = cp_parser_identifier (parser);
21300 objc_declare_alias (alias, orig);
21301 cp_parser_consume_semicolon_at_end_of_statement (parser);
21302 }
21303
21304 /* Parse an Objective-C class forward-declaration.
21305
21306 objc-class-declaration:
21307 @class objc-identifier-list ;
21308
21309 The function registers the forward declarations with the Objective-C
21310 front end. It returns nothing. */
21311
21312 static void
21313 cp_parser_objc_class_declaration (cp_parser* parser)
21314 {
21315 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
21316 objc_declare_class (cp_parser_objc_identifier_list (parser));
21317 cp_parser_consume_semicolon_at_end_of_statement (parser);
21318 }
21319
21320 /* Parse a list of Objective-C protocol references.
21321
21322 objc-protocol-refs-opt:
21323 objc-protocol-refs [opt]
21324
21325 objc-protocol-refs:
21326 < objc-identifier-list >
21327
21328 Returns a TREE_LIST of identifiers, if any. */
21329
21330 static tree
21331 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21332 {
21333 tree protorefs = NULL_TREE;
21334
21335 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21336 {
21337 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
21338 protorefs = cp_parser_objc_identifier_list (parser);
21339 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21340 }
21341
21342 return protorefs;
21343 }
21344
21345 /* Parse a Objective-C visibility specification. */
21346
21347 static void
21348 cp_parser_objc_visibility_spec (cp_parser* parser)
21349 {
21350 cp_token *vis = cp_lexer_peek_token (parser->lexer);
21351
21352 switch (vis->keyword)
21353 {
21354 case RID_AT_PRIVATE:
21355 objc_set_visibility (2);
21356 break;
21357 case RID_AT_PROTECTED:
21358 objc_set_visibility (0);
21359 break;
21360 case RID_AT_PUBLIC:
21361 objc_set_visibility (1);
21362 break;
21363 default:
21364 return;
21365 }
21366
21367 /* Eat '@private'/'@protected'/'@public'. */
21368 cp_lexer_consume_token (parser->lexer);
21369 }
21370
21371 /* Parse an Objective-C method type. */
21372
21373 static void
21374 cp_parser_objc_method_type (cp_parser* parser)
21375 {
21376 objc_set_method_type
21377 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
21378 ? PLUS_EXPR
21379 : MINUS_EXPR);
21380 }
21381
21382 /* Parse an Objective-C protocol qualifier. */
21383
21384 static tree
21385 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21386 {
21387 tree quals = NULL_TREE, node;
21388 cp_token *token = cp_lexer_peek_token (parser->lexer);
21389
21390 node = token->u.value;
21391
21392 while (node && TREE_CODE (node) == IDENTIFIER_NODE
21393 && (node == ridpointers [(int) RID_IN]
21394 || node == ridpointers [(int) RID_OUT]
21395 || node == ridpointers [(int) RID_INOUT]
21396 || node == ridpointers [(int) RID_BYCOPY]
21397 || node == ridpointers [(int) RID_BYREF]
21398 || node == ridpointers [(int) RID_ONEWAY]))
21399 {
21400 quals = tree_cons (NULL_TREE, node, quals);
21401 cp_lexer_consume_token (parser->lexer);
21402 token = cp_lexer_peek_token (parser->lexer);
21403 node = token->u.value;
21404 }
21405
21406 return quals;
21407 }
21408
21409 /* Parse an Objective-C typename. */
21410
21411 static tree
21412 cp_parser_objc_typename (cp_parser* parser)
21413 {
21414 tree type_name = NULL_TREE;
21415
21416 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21417 {
21418 tree proto_quals, cp_type = NULL_TREE;
21419
21420 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21421 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21422
21423 /* An ObjC type name may consist of just protocol qualifiers, in which
21424 case the type shall default to 'id'. */
21425 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21426 cp_type = cp_parser_type_id (parser);
21427
21428 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21429 type_name = build_tree_list (proto_quals, cp_type);
21430 }
21431
21432 return type_name;
21433 }
21434
21435 /* Check to see if TYPE refers to an Objective-C selector name. */
21436
21437 static bool
21438 cp_parser_objc_selector_p (enum cpp_ttype type)
21439 {
21440 return (type == CPP_NAME || type == CPP_KEYWORD
21441 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21442 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21443 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21444 || type == CPP_XOR || type == CPP_XOR_EQ);
21445 }
21446
21447 /* Parse an Objective-C selector. */
21448
21449 static tree
21450 cp_parser_objc_selector (cp_parser* parser)
21451 {
21452 cp_token *token = cp_lexer_consume_token (parser->lexer);
21453
21454 if (!cp_parser_objc_selector_p (token->type))
21455 {
21456 error_at (token->location, "invalid Objective-C++ selector name");
21457 return error_mark_node;
21458 }
21459
21460 /* C++ operator names are allowed to appear in ObjC selectors. */
21461 switch (token->type)
21462 {
21463 case CPP_AND_AND: return get_identifier ("and");
21464 case CPP_AND_EQ: return get_identifier ("and_eq");
21465 case CPP_AND: return get_identifier ("bitand");
21466 case CPP_OR: return get_identifier ("bitor");
21467 case CPP_COMPL: return get_identifier ("compl");
21468 case CPP_NOT: return get_identifier ("not");
21469 case CPP_NOT_EQ: return get_identifier ("not_eq");
21470 case CPP_OR_OR: return get_identifier ("or");
21471 case CPP_OR_EQ: return get_identifier ("or_eq");
21472 case CPP_XOR: return get_identifier ("xor");
21473 case CPP_XOR_EQ: return get_identifier ("xor_eq");
21474 default: return token->u.value;
21475 }
21476 }
21477
21478 /* Parse an Objective-C params list. */
21479
21480 static tree
21481 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21482 {
21483 tree params = NULL_TREE;
21484 bool maybe_unary_selector_p = true;
21485 cp_token *token = cp_lexer_peek_token (parser->lexer);
21486
21487 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21488 {
21489 tree selector = NULL_TREE, type_name, identifier;
21490 tree parm_attr = NULL_TREE;
21491
21492 if (token->keyword == RID_ATTRIBUTE)
21493 break;
21494
21495 if (token->type != CPP_COLON)
21496 selector = cp_parser_objc_selector (parser);
21497
21498 /* Detect if we have a unary selector. */
21499 if (maybe_unary_selector_p
21500 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21501 {
21502 params = selector; /* Might be followed by attributes. */
21503 break;
21504 }
21505
21506 maybe_unary_selector_p = false;
21507 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
21508 {
21509 /* Something went quite wrong. There should be a colon
21510 here, but there is not. Stop parsing parameters. */
21511 break;
21512 }
21513 type_name = cp_parser_objc_typename (parser);
21514 /* New ObjC allows attributes on parameters too. */
21515 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
21516 parm_attr = cp_parser_attributes_opt (parser);
21517 identifier = cp_parser_identifier (parser);
21518
21519 params
21520 = chainon (params,
21521 objc_build_keyword_decl (selector,
21522 type_name,
21523 identifier,
21524 parm_attr));
21525
21526 token = cp_lexer_peek_token (parser->lexer);
21527 }
21528
21529 if (params == NULL_TREE)
21530 {
21531 cp_parser_error (parser, "objective-c++ method declaration is expected");
21532 return error_mark_node;
21533 }
21534
21535 /* We allow tail attributes for the method. */
21536 if (token->keyword == RID_ATTRIBUTE)
21537 {
21538 *attributes = cp_parser_attributes_opt (parser);
21539 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21540 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21541 return params;
21542 cp_parser_error (parser,
21543 "method attributes must be specified at the end");
21544 return error_mark_node;
21545 }
21546
21547 if (params == NULL_TREE)
21548 {
21549 cp_parser_error (parser, "objective-c++ method declaration is expected");
21550 return error_mark_node;
21551 }
21552 return params;
21553 }
21554
21555 /* Parse the non-keyword Objective-C params. */
21556
21557 static tree
21558 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
21559 tree* attributes)
21560 {
21561 tree params = make_node (TREE_LIST);
21562 cp_token *token = cp_lexer_peek_token (parser->lexer);
21563 *ellipsisp = false; /* Initially, assume no ellipsis. */
21564
21565 while (token->type == CPP_COMMA)
21566 {
21567 cp_parameter_declarator *parmdecl;
21568 tree parm;
21569
21570 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21571 token = cp_lexer_peek_token (parser->lexer);
21572
21573 if (token->type == CPP_ELLIPSIS)
21574 {
21575 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
21576 *ellipsisp = true;
21577 token = cp_lexer_peek_token (parser->lexer);
21578 break;
21579 }
21580
21581 /* TODO: parse attributes for tail parameters. */
21582 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21583 parm = grokdeclarator (parmdecl->declarator,
21584 &parmdecl->decl_specifiers,
21585 PARM, /*initialized=*/0,
21586 /*attrlist=*/NULL);
21587
21588 chainon (params, build_tree_list (NULL_TREE, parm));
21589 token = cp_lexer_peek_token (parser->lexer);
21590 }
21591
21592 /* We allow tail attributes for the method. */
21593 if (token->keyword == RID_ATTRIBUTE)
21594 {
21595 if (*attributes == NULL_TREE)
21596 {
21597 *attributes = cp_parser_attributes_opt (parser);
21598 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21599 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21600 return params;
21601 }
21602 else
21603 /* We have an error, but parse the attributes, so that we can
21604 carry on. */
21605 *attributes = cp_parser_attributes_opt (parser);
21606
21607 cp_parser_error (parser,
21608 "method attributes must be specified at the end");
21609 return error_mark_node;
21610 }
21611
21612 return params;
21613 }
21614
21615 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
21616
21617 static void
21618 cp_parser_objc_interstitial_code (cp_parser* parser)
21619 {
21620 cp_token *token = cp_lexer_peek_token (parser->lexer);
21621
21622 /* If the next token is `extern' and the following token is a string
21623 literal, then we have a linkage specification. */
21624 if (token->keyword == RID_EXTERN
21625 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
21626 cp_parser_linkage_specification (parser);
21627 /* Handle #pragma, if any. */
21628 else if (token->type == CPP_PRAGMA)
21629 cp_parser_pragma (parser, pragma_external);
21630 /* Allow stray semicolons. */
21631 else if (token->type == CPP_SEMICOLON)
21632 cp_lexer_consume_token (parser->lexer);
21633 /* Mark methods as optional or required, when building protocols. */
21634 else if (token->keyword == RID_AT_OPTIONAL)
21635 {
21636 cp_lexer_consume_token (parser->lexer);
21637 objc_set_method_opt (true);
21638 }
21639 else if (token->keyword == RID_AT_REQUIRED)
21640 {
21641 cp_lexer_consume_token (parser->lexer);
21642 objc_set_method_opt (false);
21643 }
21644 else if (token->keyword == RID_NAMESPACE)
21645 cp_parser_namespace_definition (parser);
21646 /* Other stray characters must generate errors. */
21647 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
21648 {
21649 cp_lexer_consume_token (parser->lexer);
21650 error ("stray `%s' between Objective-C++ methods",
21651 token->type == CPP_OPEN_BRACE ? "{" : "}");
21652 }
21653 /* Finally, try to parse a block-declaration, or a function-definition. */
21654 else
21655 cp_parser_block_declaration (parser, /*statement_p=*/false);
21656 }
21657
21658 /* Parse a method signature. */
21659
21660 static tree
21661 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
21662 {
21663 tree rettype, kwdparms, optparms;
21664 bool ellipsis = false;
21665
21666 cp_parser_objc_method_type (parser);
21667 rettype = cp_parser_objc_typename (parser);
21668 *attributes = NULL_TREE;
21669 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
21670 if (kwdparms == error_mark_node)
21671 return error_mark_node;
21672 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
21673 if (optparms == error_mark_node)
21674 return error_mark_node;
21675
21676 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
21677 }
21678
21679 static bool
21680 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
21681 {
21682 tree tattr;
21683 cp_lexer_save_tokens (parser->lexer);
21684 tattr = cp_parser_attributes_opt (parser);
21685 gcc_assert (tattr) ;
21686
21687 /* If the attributes are followed by a method introducer, this is not allowed.
21688 Dump the attributes and flag the situation. */
21689 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
21690 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
21691 return true;
21692
21693 /* Otherwise, the attributes introduce some interstitial code, possibly so
21694 rewind to allow that check. */
21695 cp_lexer_rollback_tokens (parser->lexer);
21696 return false;
21697 }
21698
21699 /* Parse an Objective-C method prototype list. */
21700
21701 static void
21702 cp_parser_objc_method_prototype_list (cp_parser* parser)
21703 {
21704 cp_token *token = cp_lexer_peek_token (parser->lexer);
21705
21706 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
21707 {
21708 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21709 {
21710 tree attributes, sig;
21711 sig = cp_parser_objc_method_signature (parser, &attributes);
21712 if (sig == error_mark_node)
21713 {
21714 cp_parser_skip_to_end_of_block_or_statement (parser);
21715 token = cp_lexer_peek_token (parser->lexer);
21716 continue;
21717 }
21718 objc_add_method_declaration (sig, attributes);
21719 cp_parser_consume_semicolon_at_end_of_statement (parser);
21720 }
21721 else if (token->keyword == RID_ATTRIBUTE
21722 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
21723 warning_at (cp_lexer_peek_token (parser->lexer)->location,
21724 OPT_Wattributes,
21725 "prefix attributes are ignored for methods");
21726 else
21727 /* Allow for interspersed non-ObjC++ code. */
21728 cp_parser_objc_interstitial_code (parser);
21729
21730 token = cp_lexer_peek_token (parser->lexer);
21731 }
21732
21733 if (token->type != CPP_EOF)
21734 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21735 else
21736 cp_parser_error (parser, "expected %<@end%>");
21737
21738 objc_finish_interface ();
21739 }
21740
21741 /* Parse an Objective-C method definition list. */
21742
21743 static void
21744 cp_parser_objc_method_definition_list (cp_parser* parser)
21745 {
21746 cp_token *token = cp_lexer_peek_token (parser->lexer);
21747
21748 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
21749 {
21750 tree meth;
21751
21752 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21753 {
21754 cp_token *ptk;
21755 tree sig, attribute;
21756 push_deferring_access_checks (dk_deferred);
21757 sig = cp_parser_objc_method_signature (parser, &attribute);
21758 if (sig == error_mark_node)
21759 {
21760 cp_parser_skip_to_end_of_block_or_statement (parser);
21761 token = cp_lexer_peek_token (parser->lexer);
21762 continue;
21763 }
21764 objc_start_method_definition (sig, attribute);
21765
21766 /* For historical reasons, we accept an optional semicolon. */
21767 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21768 cp_lexer_consume_token (parser->lexer);
21769
21770 ptk = cp_lexer_peek_token (parser->lexer);
21771 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
21772 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
21773 {
21774 perform_deferred_access_checks ();
21775 stop_deferring_access_checks ();
21776 meth = cp_parser_function_definition_after_declarator (parser,
21777 false);
21778 pop_deferring_access_checks ();
21779 objc_finish_method_definition (meth);
21780 }
21781 }
21782 else if (token->keyword == RID_ATTRIBUTE
21783 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
21784 warning_at (token->location, OPT_Wattributes,
21785 "prefix attributes are ignored for methods");
21786 else
21787 /* Allow for interspersed non-ObjC++ code. */
21788 cp_parser_objc_interstitial_code (parser);
21789
21790 token = cp_lexer_peek_token (parser->lexer);
21791 }
21792
21793 if (token->type != CPP_EOF)
21794 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21795 else
21796 cp_parser_error (parser, "expected %<@end%>");
21797
21798 objc_finish_implementation ();
21799 }
21800
21801 /* Parse Objective-C ivars. */
21802
21803 static void
21804 cp_parser_objc_class_ivars (cp_parser* parser)
21805 {
21806 cp_token *token = cp_lexer_peek_token (parser->lexer);
21807
21808 if (token->type != CPP_OPEN_BRACE)
21809 return; /* No ivars specified. */
21810
21811 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
21812 token = cp_lexer_peek_token (parser->lexer);
21813
21814 while (token->type != CPP_CLOSE_BRACE
21815 && token->keyword != RID_AT_END && token->type != CPP_EOF)
21816 {
21817 cp_decl_specifier_seq declspecs;
21818 int decl_class_or_enum_p;
21819 tree prefix_attributes;
21820
21821 cp_parser_objc_visibility_spec (parser);
21822
21823 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21824 break;
21825
21826 cp_parser_decl_specifier_seq (parser,
21827 CP_PARSER_FLAGS_OPTIONAL,
21828 &declspecs,
21829 &decl_class_or_enum_p);
21830 prefix_attributes = declspecs.attributes;
21831 declspecs.attributes = NULL_TREE;
21832
21833 /* Keep going until we hit the `;' at the end of the
21834 declaration. */
21835 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21836 {
21837 tree width = NULL_TREE, attributes, first_attribute, decl;
21838 cp_declarator *declarator = NULL;
21839 int ctor_dtor_or_conv_p;
21840
21841 /* Check for a (possibly unnamed) bitfield declaration. */
21842 token = cp_lexer_peek_token (parser->lexer);
21843 if (token->type == CPP_COLON)
21844 goto eat_colon;
21845
21846 if (token->type == CPP_NAME
21847 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21848 == CPP_COLON))
21849 {
21850 /* Get the name of the bitfield. */
21851 declarator = make_id_declarator (NULL_TREE,
21852 cp_parser_identifier (parser),
21853 sfk_none);
21854
21855 eat_colon:
21856 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
21857 /* Get the width of the bitfield. */
21858 width
21859 = cp_parser_constant_expression (parser,
21860 /*allow_non_constant=*/false,
21861 NULL);
21862 }
21863 else
21864 {
21865 /* Parse the declarator. */
21866 declarator
21867 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21868 &ctor_dtor_or_conv_p,
21869 /*parenthesized_p=*/NULL,
21870 /*member_p=*/false);
21871 }
21872
21873 /* Look for attributes that apply to the ivar. */
21874 attributes = cp_parser_attributes_opt (parser);
21875 /* Remember which attributes are prefix attributes and
21876 which are not. */
21877 first_attribute = attributes;
21878 /* Combine the attributes. */
21879 attributes = chainon (prefix_attributes, attributes);
21880
21881 if (width)
21882 /* Create the bitfield declaration. */
21883 decl = grokbitfield (declarator, &declspecs,
21884 width,
21885 attributes);
21886 else
21887 decl = grokfield (declarator, &declspecs,
21888 NULL_TREE, /*init_const_expr_p=*/false,
21889 NULL_TREE, attributes);
21890
21891 /* Add the instance variable. */
21892 objc_add_instance_variable (decl);
21893
21894 /* Reset PREFIX_ATTRIBUTES. */
21895 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21896 attributes = TREE_CHAIN (attributes);
21897 if (attributes)
21898 TREE_CHAIN (attributes) = NULL_TREE;
21899
21900 token = cp_lexer_peek_token (parser->lexer);
21901
21902 if (token->type == CPP_COMMA)
21903 {
21904 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21905 continue;
21906 }
21907 break;
21908 }
21909
21910 cp_parser_consume_semicolon_at_end_of_statement (parser);
21911 token = cp_lexer_peek_token (parser->lexer);
21912 }
21913
21914 if (token->keyword == RID_AT_END)
21915 cp_parser_error (parser, "expected %<}%>");
21916
21917 /* Do not consume the RID_AT_END, so it will be read again as terminating
21918 the @interface of @implementation. */
21919 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
21920 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
21921
21922 /* For historical reasons, we accept an optional semicolon. */
21923 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21924 cp_lexer_consume_token (parser->lexer);
21925 }
21926
21927 /* Parse an Objective-C protocol declaration. */
21928
21929 static void
21930 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
21931 {
21932 tree proto, protorefs;
21933 cp_token *tok;
21934
21935 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21936 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
21937 {
21938 tok = cp_lexer_peek_token (parser->lexer);
21939 error_at (tok->location, "identifier expected after %<@protocol%>");
21940 goto finish;
21941 }
21942
21943 /* See if we have a forward declaration or a definition. */
21944 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
21945
21946 /* Try a forward declaration first. */
21947 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
21948 {
21949 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
21950 finish:
21951 cp_parser_consume_semicolon_at_end_of_statement (parser);
21952 }
21953
21954 /* Ok, we got a full-fledged definition (or at least should). */
21955 else
21956 {
21957 proto = cp_parser_identifier (parser);
21958 protorefs = cp_parser_objc_protocol_refs_opt (parser);
21959 objc_start_protocol (proto, protorefs, attributes);
21960 cp_parser_objc_method_prototype_list (parser);
21961 }
21962 }
21963
21964 /* Parse an Objective-C superclass or category. */
21965
21966 static void
21967 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
21968 tree *categ)
21969 {
21970 cp_token *next = cp_lexer_peek_token (parser->lexer);
21971
21972 *super = *categ = NULL_TREE;
21973 if (next->type == CPP_COLON)
21974 {
21975 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
21976 *super = cp_parser_identifier (parser);
21977 }
21978 else if (next->type == CPP_OPEN_PAREN)
21979 {
21980 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21981 *categ = cp_parser_identifier (parser);
21982 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21983 }
21984 }
21985
21986 /* Parse an Objective-C class interface. */
21987
21988 static void
21989 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
21990 {
21991 tree name, super, categ, protos;
21992
21993 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
21994 name = cp_parser_identifier (parser);
21995 if (name == error_mark_node)
21996 {
21997 /* It's hard to recover because even if valid @interface stuff
21998 is to follow, we can't compile it (or validate it) if we
21999 don't even know which class it refers to. Let's assume this
22000 was a stray '@interface' token in the stream and skip it.
22001 */
22002 return;
22003 }
22004 cp_parser_objc_superclass_or_category (parser, &super, &categ);
22005 protos = cp_parser_objc_protocol_refs_opt (parser);
22006
22007 /* We have either a class or a category on our hands. */
22008 if (categ)
22009 objc_start_category_interface (name, categ, protos, attributes);
22010 else
22011 {
22012 objc_start_class_interface (name, super, protos, attributes);
22013 /* Handle instance variable declarations, if any. */
22014 cp_parser_objc_class_ivars (parser);
22015 objc_continue_interface ();
22016 }
22017
22018 cp_parser_objc_method_prototype_list (parser);
22019 }
22020
22021 /* Parse an Objective-C class implementation. */
22022
22023 static void
22024 cp_parser_objc_class_implementation (cp_parser* parser)
22025 {
22026 tree name, super, categ;
22027
22028 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
22029 name = cp_parser_identifier (parser);
22030 if (name == error_mark_node)
22031 {
22032 /* It's hard to recover because even if valid @implementation
22033 stuff is to follow, we can't compile it (or validate it) if
22034 we don't even know which class it refers to. Let's assume
22035 this was a stray '@implementation' token in the stream and
22036 skip it.
22037 */
22038 return;
22039 }
22040 cp_parser_objc_superclass_or_category (parser, &super, &categ);
22041
22042 /* We have either a class or a category on our hands. */
22043 if (categ)
22044 objc_start_category_implementation (name, categ);
22045 else
22046 {
22047 objc_start_class_implementation (name, super);
22048 /* Handle instance variable declarations, if any. */
22049 cp_parser_objc_class_ivars (parser);
22050 objc_continue_implementation ();
22051 }
22052
22053 cp_parser_objc_method_definition_list (parser);
22054 }
22055
22056 /* Consume the @end token and finish off the implementation. */
22057
22058 static void
22059 cp_parser_objc_end_implementation (cp_parser* parser)
22060 {
22061 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22062 objc_finish_implementation ();
22063 }
22064
22065 /* Parse an Objective-C declaration. */
22066
22067 static void
22068 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22069 {
22070 /* Try to figure out what kind of declaration is present. */
22071 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22072
22073 if (attributes)
22074 switch (kwd->keyword)
22075 {
22076 case RID_AT_ALIAS:
22077 case RID_AT_CLASS:
22078 case RID_AT_END:
22079 error_at (kwd->location, "attributes may not be specified before"
22080 " the %<@%D%> Objective-C++ keyword",
22081 kwd->u.value);
22082 attributes = NULL;
22083 break;
22084 case RID_AT_IMPLEMENTATION:
22085 warning_at (kwd->location, OPT_Wattributes,
22086 "prefix attributes are ignored before %<@%D%>",
22087 kwd->u.value);
22088 attributes = NULL;
22089 default:
22090 break;
22091 }
22092
22093 switch (kwd->keyword)
22094 {
22095 case RID_AT_ALIAS:
22096 cp_parser_objc_alias_declaration (parser);
22097 break;
22098 case RID_AT_CLASS:
22099 cp_parser_objc_class_declaration (parser);
22100 break;
22101 case RID_AT_PROTOCOL:
22102 cp_parser_objc_protocol_declaration (parser, attributes);
22103 break;
22104 case RID_AT_INTERFACE:
22105 cp_parser_objc_class_interface (parser, attributes);
22106 break;
22107 case RID_AT_IMPLEMENTATION:
22108 cp_parser_objc_class_implementation (parser);
22109 break;
22110 case RID_AT_END:
22111 cp_parser_objc_end_implementation (parser);
22112 break;
22113 default:
22114 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22115 kwd->u.value);
22116 cp_parser_skip_to_end_of_block_or_statement (parser);
22117 }
22118 }
22119
22120 /* Parse an Objective-C try-catch-finally statement.
22121
22122 objc-try-catch-finally-stmt:
22123 @try compound-statement objc-catch-clause-seq [opt]
22124 objc-finally-clause [opt]
22125
22126 objc-catch-clause-seq:
22127 objc-catch-clause objc-catch-clause-seq [opt]
22128
22129 objc-catch-clause:
22130 @catch ( exception-declaration ) compound-statement
22131
22132 objc-finally-clause
22133 @finally compound-statement
22134
22135 Returns NULL_TREE. */
22136
22137 static tree
22138 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
22139 location_t location;
22140 tree stmt;
22141
22142 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22143 location = cp_lexer_peek_token (parser->lexer)->location;
22144 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22145 node, lest it get absorbed into the surrounding block. */
22146 stmt = push_stmt_list ();
22147 cp_parser_compound_statement (parser, NULL, false);
22148 objc_begin_try_stmt (location, pop_stmt_list (stmt));
22149
22150 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22151 {
22152 cp_parameter_declarator *parmdecl;
22153 tree parm;
22154
22155 cp_lexer_consume_token (parser->lexer);
22156 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22157 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22158 parm = grokdeclarator (parmdecl->declarator,
22159 &parmdecl->decl_specifiers,
22160 PARM, /*initialized=*/0,
22161 /*attrlist=*/NULL);
22162 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22163 objc_begin_catch_clause (parm);
22164 cp_parser_compound_statement (parser, NULL, false);
22165 objc_finish_catch_clause ();
22166 }
22167
22168 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22169 {
22170 cp_lexer_consume_token (parser->lexer);
22171 location = cp_lexer_peek_token (parser->lexer)->location;
22172 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22173 node, lest it get absorbed into the surrounding block. */
22174 stmt = push_stmt_list ();
22175 cp_parser_compound_statement (parser, NULL, false);
22176 objc_build_finally_clause (location, pop_stmt_list (stmt));
22177 }
22178
22179 return objc_finish_try_stmt ();
22180 }
22181
22182 /* Parse an Objective-C synchronized statement.
22183
22184 objc-synchronized-stmt:
22185 @synchronized ( expression ) compound-statement
22186
22187 Returns NULL_TREE. */
22188
22189 static tree
22190 cp_parser_objc_synchronized_statement (cp_parser *parser) {
22191 location_t location;
22192 tree lock, stmt;
22193
22194 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22195
22196 location = cp_lexer_peek_token (parser->lexer)->location;
22197 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22198 lock = cp_parser_expression (parser, false, NULL);
22199 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22200
22201 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22202 node, lest it get absorbed into the surrounding block. */
22203 stmt = push_stmt_list ();
22204 cp_parser_compound_statement (parser, NULL, false);
22205
22206 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22207 }
22208
22209 /* Parse an Objective-C throw statement.
22210
22211 objc-throw-stmt:
22212 @throw assignment-expression [opt] ;
22213
22214 Returns a constructed '@throw' statement. */
22215
22216 static tree
22217 cp_parser_objc_throw_statement (cp_parser *parser) {
22218 tree expr = NULL_TREE;
22219 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22220
22221 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22222
22223 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22224 expr = cp_parser_assignment_expression (parser, false, NULL);
22225
22226 cp_parser_consume_semicolon_at_end_of_statement (parser);
22227
22228 return objc_build_throw_stmt (loc, expr);
22229 }
22230
22231 /* Parse an Objective-C statement. */
22232
22233 static tree
22234 cp_parser_objc_statement (cp_parser * parser) {
22235 /* Try to figure out what kind of declaration is present. */
22236 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22237
22238 switch (kwd->keyword)
22239 {
22240 case RID_AT_TRY:
22241 return cp_parser_objc_try_catch_finally_statement (parser);
22242 case RID_AT_SYNCHRONIZED:
22243 return cp_parser_objc_synchronized_statement (parser);
22244 case RID_AT_THROW:
22245 return cp_parser_objc_throw_statement (parser);
22246 default:
22247 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22248 kwd->u.value);
22249 cp_parser_skip_to_end_of_block_or_statement (parser);
22250 }
22251
22252 return error_mark_node;
22253 }
22254
22255 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22256 look ahead to see if an objc keyword follows the attributes. This
22257 is to detect the use of prefix attributes on ObjC @interface and
22258 @protocol. */
22259
22260 static bool
22261 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22262 {
22263 cp_lexer_save_tokens (parser->lexer);
22264 *attrib = cp_parser_attributes_opt (parser);
22265 gcc_assert (*attrib);
22266 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22267 {
22268 cp_lexer_commit_tokens (parser->lexer);
22269 return true;
22270 }
22271 cp_lexer_rollback_tokens (parser->lexer);
22272 return false;
22273 }
22274 \f
22275 /* OpenMP 2.5 parsing routines. */
22276
22277 /* Returns name of the next clause.
22278 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
22279 the token is not consumed. Otherwise appropriate pragma_omp_clause is
22280 returned and the token is consumed. */
22281
22282 static pragma_omp_clause
22283 cp_parser_omp_clause_name (cp_parser *parser)
22284 {
22285 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
22286
22287 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
22288 result = PRAGMA_OMP_CLAUSE_IF;
22289 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
22290 result = PRAGMA_OMP_CLAUSE_DEFAULT;
22291 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
22292 result = PRAGMA_OMP_CLAUSE_PRIVATE;
22293 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22294 {
22295 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22296 const char *p = IDENTIFIER_POINTER (id);
22297
22298 switch (p[0])
22299 {
22300 case 'c':
22301 if (!strcmp ("collapse", p))
22302 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
22303 else if (!strcmp ("copyin", p))
22304 result = PRAGMA_OMP_CLAUSE_COPYIN;
22305 else if (!strcmp ("copyprivate", p))
22306 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
22307 break;
22308 case 'f':
22309 if (!strcmp ("firstprivate", p))
22310 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
22311 break;
22312 case 'l':
22313 if (!strcmp ("lastprivate", p))
22314 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
22315 break;
22316 case 'n':
22317 if (!strcmp ("nowait", p))
22318 result = PRAGMA_OMP_CLAUSE_NOWAIT;
22319 else if (!strcmp ("num_threads", p))
22320 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
22321 break;
22322 case 'o':
22323 if (!strcmp ("ordered", p))
22324 result = PRAGMA_OMP_CLAUSE_ORDERED;
22325 break;
22326 case 'r':
22327 if (!strcmp ("reduction", p))
22328 result = PRAGMA_OMP_CLAUSE_REDUCTION;
22329 break;
22330 case 's':
22331 if (!strcmp ("schedule", p))
22332 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
22333 else if (!strcmp ("shared", p))
22334 result = PRAGMA_OMP_CLAUSE_SHARED;
22335 break;
22336 case 'u':
22337 if (!strcmp ("untied", p))
22338 result = PRAGMA_OMP_CLAUSE_UNTIED;
22339 break;
22340 }
22341 }
22342
22343 if (result != PRAGMA_OMP_CLAUSE_NONE)
22344 cp_lexer_consume_token (parser->lexer);
22345
22346 return result;
22347 }
22348
22349 /* Validate that a clause of the given type does not already exist. */
22350
22351 static void
22352 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
22353 const char *name, location_t location)
22354 {
22355 tree c;
22356
22357 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22358 if (OMP_CLAUSE_CODE (c) == code)
22359 {
22360 error_at (location, "too many %qs clauses", name);
22361 break;
22362 }
22363 }
22364
22365 /* OpenMP 2.5:
22366 variable-list:
22367 identifier
22368 variable-list , identifier
22369
22370 In addition, we match a closing parenthesis. An opening parenthesis
22371 will have been consumed by the caller.
22372
22373 If KIND is nonzero, create the appropriate node and install the decl
22374 in OMP_CLAUSE_DECL and add the node to the head of the list.
22375
22376 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
22377 return the list created. */
22378
22379 static tree
22380 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
22381 tree list)
22382 {
22383 cp_token *token;
22384 while (1)
22385 {
22386 tree name, decl;
22387
22388 token = cp_lexer_peek_token (parser->lexer);
22389 name = cp_parser_id_expression (parser, /*template_p=*/false,
22390 /*check_dependency_p=*/true,
22391 /*template_p=*/NULL,
22392 /*declarator_p=*/false,
22393 /*optional_p=*/false);
22394 if (name == error_mark_node)
22395 goto skip_comma;
22396
22397 decl = cp_parser_lookup_name_simple (parser, name, token->location);
22398 if (decl == error_mark_node)
22399 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
22400 token->location);
22401 else if (kind != 0)
22402 {
22403 tree u = build_omp_clause (token->location, kind);
22404 OMP_CLAUSE_DECL (u) = decl;
22405 OMP_CLAUSE_CHAIN (u) = list;
22406 list = u;
22407 }
22408 else
22409 list = tree_cons (decl, NULL_TREE, list);
22410
22411 get_comma:
22412 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22413 break;
22414 cp_lexer_consume_token (parser->lexer);
22415 }
22416
22417 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22418 {
22419 int ending;
22420
22421 /* Try to resync to an unnested comma. Copied from
22422 cp_parser_parenthesized_expression_list. */
22423 skip_comma:
22424 ending = cp_parser_skip_to_closing_parenthesis (parser,
22425 /*recovering=*/true,
22426 /*or_comma=*/true,
22427 /*consume_paren=*/true);
22428 if (ending < 0)
22429 goto get_comma;
22430 }
22431
22432 return list;
22433 }
22434
22435 /* Similarly, but expect leading and trailing parenthesis. This is a very
22436 common case for omp clauses. */
22437
22438 static tree
22439 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
22440 {
22441 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22442 return cp_parser_omp_var_list_no_open (parser, kind, list);
22443 return list;
22444 }
22445
22446 /* OpenMP 3.0:
22447 collapse ( constant-expression ) */
22448
22449 static tree
22450 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
22451 {
22452 tree c, num;
22453 location_t loc;
22454 HOST_WIDE_INT n;
22455
22456 loc = cp_lexer_peek_token (parser->lexer)->location;
22457 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22458 return list;
22459
22460 num = cp_parser_constant_expression (parser, false, NULL);
22461
22462 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22463 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22464 /*or_comma=*/false,
22465 /*consume_paren=*/true);
22466
22467 if (num == error_mark_node)
22468 return list;
22469 num = fold_non_dependent_expr (num);
22470 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
22471 || !host_integerp (num, 0)
22472 || (n = tree_low_cst (num, 0)) <= 0
22473 || (int) n != n)
22474 {
22475 error_at (loc, "collapse argument needs positive constant integer expression");
22476 return list;
22477 }
22478
22479 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
22480 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
22481 OMP_CLAUSE_CHAIN (c) = list;
22482 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
22483
22484 return c;
22485 }
22486
22487 /* OpenMP 2.5:
22488 default ( shared | none ) */
22489
22490 static tree
22491 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
22492 {
22493 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
22494 tree c;
22495
22496 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22497 return list;
22498 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22499 {
22500 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22501 const char *p = IDENTIFIER_POINTER (id);
22502
22503 switch (p[0])
22504 {
22505 case 'n':
22506 if (strcmp ("none", p) != 0)
22507 goto invalid_kind;
22508 kind = OMP_CLAUSE_DEFAULT_NONE;
22509 break;
22510
22511 case 's':
22512 if (strcmp ("shared", p) != 0)
22513 goto invalid_kind;
22514 kind = OMP_CLAUSE_DEFAULT_SHARED;
22515 break;
22516
22517 default:
22518 goto invalid_kind;
22519 }
22520
22521 cp_lexer_consume_token (parser->lexer);
22522 }
22523 else
22524 {
22525 invalid_kind:
22526 cp_parser_error (parser, "expected %<none%> or %<shared%>");
22527 }
22528
22529 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22530 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22531 /*or_comma=*/false,
22532 /*consume_paren=*/true);
22533
22534 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
22535 return list;
22536
22537 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
22538 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
22539 OMP_CLAUSE_CHAIN (c) = list;
22540 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
22541
22542 return c;
22543 }
22544
22545 /* OpenMP 2.5:
22546 if ( expression ) */
22547
22548 static tree
22549 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
22550 {
22551 tree t, c;
22552
22553 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22554 return list;
22555
22556 t = cp_parser_condition (parser);
22557
22558 if (t == error_mark_node
22559 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22560 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22561 /*or_comma=*/false,
22562 /*consume_paren=*/true);
22563
22564 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
22565
22566 c = build_omp_clause (location, OMP_CLAUSE_IF);
22567 OMP_CLAUSE_IF_EXPR (c) = t;
22568 OMP_CLAUSE_CHAIN (c) = list;
22569
22570 return c;
22571 }
22572
22573 /* OpenMP 2.5:
22574 nowait */
22575
22576 static tree
22577 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
22578 tree list, location_t location)
22579 {
22580 tree c;
22581
22582 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
22583
22584 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
22585 OMP_CLAUSE_CHAIN (c) = list;
22586 return c;
22587 }
22588
22589 /* OpenMP 2.5:
22590 num_threads ( expression ) */
22591
22592 static tree
22593 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
22594 location_t location)
22595 {
22596 tree t, c;
22597
22598 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22599 return list;
22600
22601 t = cp_parser_expression (parser, false, NULL);
22602
22603 if (t == error_mark_node
22604 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22605 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22606 /*or_comma=*/false,
22607 /*consume_paren=*/true);
22608
22609 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
22610 "num_threads", location);
22611
22612 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
22613 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
22614 OMP_CLAUSE_CHAIN (c) = list;
22615
22616 return c;
22617 }
22618
22619 /* OpenMP 2.5:
22620 ordered */
22621
22622 static tree
22623 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
22624 tree list, location_t location)
22625 {
22626 tree c;
22627
22628 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
22629 "ordered", location);
22630
22631 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
22632 OMP_CLAUSE_CHAIN (c) = list;
22633 return c;
22634 }
22635
22636 /* OpenMP 2.5:
22637 reduction ( reduction-operator : variable-list )
22638
22639 reduction-operator:
22640 One of: + * - & ^ | && || */
22641
22642 static tree
22643 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
22644 {
22645 enum tree_code code;
22646 tree nlist, c;
22647
22648 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22649 return list;
22650
22651 switch (cp_lexer_peek_token (parser->lexer)->type)
22652 {
22653 case CPP_PLUS:
22654 code = PLUS_EXPR;
22655 break;
22656 case CPP_MULT:
22657 code = MULT_EXPR;
22658 break;
22659 case CPP_MINUS:
22660 code = MINUS_EXPR;
22661 break;
22662 case CPP_AND:
22663 code = BIT_AND_EXPR;
22664 break;
22665 case CPP_XOR:
22666 code = BIT_XOR_EXPR;
22667 break;
22668 case CPP_OR:
22669 code = BIT_IOR_EXPR;
22670 break;
22671 case CPP_AND_AND:
22672 code = TRUTH_ANDIF_EXPR;
22673 break;
22674 case CPP_OR_OR:
22675 code = TRUTH_ORIF_EXPR;
22676 break;
22677 default:
22678 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
22679 "%<|%>, %<&&%>, or %<||%>");
22680 resync_fail:
22681 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22682 /*or_comma=*/false,
22683 /*consume_paren=*/true);
22684 return list;
22685 }
22686 cp_lexer_consume_token (parser->lexer);
22687
22688 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22689 goto resync_fail;
22690
22691 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
22692 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
22693 OMP_CLAUSE_REDUCTION_CODE (c) = code;
22694
22695 return nlist;
22696 }
22697
22698 /* OpenMP 2.5:
22699 schedule ( schedule-kind )
22700 schedule ( schedule-kind , expression )
22701
22702 schedule-kind:
22703 static | dynamic | guided | runtime | auto */
22704
22705 static tree
22706 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
22707 {
22708 tree c, t;
22709
22710 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22711 return list;
22712
22713 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
22714
22715 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22716 {
22717 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22718 const char *p = IDENTIFIER_POINTER (id);
22719
22720 switch (p[0])
22721 {
22722 case 'd':
22723 if (strcmp ("dynamic", p) != 0)
22724 goto invalid_kind;
22725 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
22726 break;
22727
22728 case 'g':
22729 if (strcmp ("guided", p) != 0)
22730 goto invalid_kind;
22731 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
22732 break;
22733
22734 case 'r':
22735 if (strcmp ("runtime", p) != 0)
22736 goto invalid_kind;
22737 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
22738 break;
22739
22740 default:
22741 goto invalid_kind;
22742 }
22743 }
22744 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
22745 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
22746 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
22747 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
22748 else
22749 goto invalid_kind;
22750 cp_lexer_consume_token (parser->lexer);
22751
22752 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22753 {
22754 cp_token *token;
22755 cp_lexer_consume_token (parser->lexer);
22756
22757 token = cp_lexer_peek_token (parser->lexer);
22758 t = cp_parser_assignment_expression (parser, false, NULL);
22759
22760 if (t == error_mark_node)
22761 goto resync_fail;
22762 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
22763 error_at (token->location, "schedule %<runtime%> does not take "
22764 "a %<chunk_size%> parameter");
22765 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
22766 error_at (token->location, "schedule %<auto%> does not take "
22767 "a %<chunk_size%> parameter");
22768 else
22769 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
22770
22771 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22772 goto resync_fail;
22773 }
22774 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
22775 goto resync_fail;
22776
22777 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
22778 OMP_CLAUSE_CHAIN (c) = list;
22779 return c;
22780
22781 invalid_kind:
22782 cp_parser_error (parser, "invalid schedule kind");
22783 resync_fail:
22784 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22785 /*or_comma=*/false,
22786 /*consume_paren=*/true);
22787 return list;
22788 }
22789
22790 /* OpenMP 3.0:
22791 untied */
22792
22793 static tree
22794 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
22795 tree list, location_t location)
22796 {
22797 tree c;
22798
22799 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
22800
22801 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
22802 OMP_CLAUSE_CHAIN (c) = list;
22803 return c;
22804 }
22805
22806 /* Parse all OpenMP clauses. The set clauses allowed by the directive
22807 is a bitmask in MASK. Return the list of clauses found; the result
22808 of clause default goes in *pdefault. */
22809
22810 static tree
22811 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
22812 const char *where, cp_token *pragma_tok)
22813 {
22814 tree clauses = NULL;
22815 bool first = true;
22816 cp_token *token = NULL;
22817
22818 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
22819 {
22820 pragma_omp_clause c_kind;
22821 const char *c_name;
22822 tree prev = clauses;
22823
22824 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22825 cp_lexer_consume_token (parser->lexer);
22826
22827 token = cp_lexer_peek_token (parser->lexer);
22828 c_kind = cp_parser_omp_clause_name (parser);
22829 first = false;
22830
22831 switch (c_kind)
22832 {
22833 case PRAGMA_OMP_CLAUSE_COLLAPSE:
22834 clauses = cp_parser_omp_clause_collapse (parser, clauses,
22835 token->location);
22836 c_name = "collapse";
22837 break;
22838 case PRAGMA_OMP_CLAUSE_COPYIN:
22839 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
22840 c_name = "copyin";
22841 break;
22842 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
22843 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
22844 clauses);
22845 c_name = "copyprivate";
22846 break;
22847 case PRAGMA_OMP_CLAUSE_DEFAULT:
22848 clauses = cp_parser_omp_clause_default (parser, clauses,
22849 token->location);
22850 c_name = "default";
22851 break;
22852 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
22853 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
22854 clauses);
22855 c_name = "firstprivate";
22856 break;
22857 case PRAGMA_OMP_CLAUSE_IF:
22858 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
22859 c_name = "if";
22860 break;
22861 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
22862 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
22863 clauses);
22864 c_name = "lastprivate";
22865 break;
22866 case PRAGMA_OMP_CLAUSE_NOWAIT:
22867 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
22868 c_name = "nowait";
22869 break;
22870 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
22871 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
22872 token->location);
22873 c_name = "num_threads";
22874 break;
22875 case PRAGMA_OMP_CLAUSE_ORDERED:
22876 clauses = cp_parser_omp_clause_ordered (parser, clauses,
22877 token->location);
22878 c_name = "ordered";
22879 break;
22880 case PRAGMA_OMP_CLAUSE_PRIVATE:
22881 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
22882 clauses);
22883 c_name = "private";
22884 break;
22885 case PRAGMA_OMP_CLAUSE_REDUCTION:
22886 clauses = cp_parser_omp_clause_reduction (parser, clauses);
22887 c_name = "reduction";
22888 break;
22889 case PRAGMA_OMP_CLAUSE_SCHEDULE:
22890 clauses = cp_parser_omp_clause_schedule (parser, clauses,
22891 token->location);
22892 c_name = "schedule";
22893 break;
22894 case PRAGMA_OMP_CLAUSE_SHARED:
22895 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
22896 clauses);
22897 c_name = "shared";
22898 break;
22899 case PRAGMA_OMP_CLAUSE_UNTIED:
22900 clauses = cp_parser_omp_clause_untied (parser, clauses,
22901 token->location);
22902 c_name = "nowait";
22903 break;
22904 default:
22905 cp_parser_error (parser, "expected %<#pragma omp%> clause");
22906 goto saw_error;
22907 }
22908
22909 if (((mask >> c_kind) & 1) == 0)
22910 {
22911 /* Remove the invalid clause(s) from the list to avoid
22912 confusing the rest of the compiler. */
22913 clauses = prev;
22914 error_at (token->location, "%qs is not valid for %qs", c_name, where);
22915 }
22916 }
22917 saw_error:
22918 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22919 return finish_omp_clauses (clauses);
22920 }
22921
22922 /* OpenMP 2.5:
22923 structured-block:
22924 statement
22925
22926 In practice, we're also interested in adding the statement to an
22927 outer node. So it is convenient if we work around the fact that
22928 cp_parser_statement calls add_stmt. */
22929
22930 static unsigned
22931 cp_parser_begin_omp_structured_block (cp_parser *parser)
22932 {
22933 unsigned save = parser->in_statement;
22934
22935 /* Only move the values to IN_OMP_BLOCK if they weren't false.
22936 This preserves the "not within loop or switch" style error messages
22937 for nonsense cases like
22938 void foo() {
22939 #pragma omp single
22940 break;
22941 }
22942 */
22943 if (parser->in_statement)
22944 parser->in_statement = IN_OMP_BLOCK;
22945
22946 return save;
22947 }
22948
22949 static void
22950 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
22951 {
22952 parser->in_statement = save;
22953 }
22954
22955 static tree
22956 cp_parser_omp_structured_block (cp_parser *parser)
22957 {
22958 tree stmt = begin_omp_structured_block ();
22959 unsigned int save = cp_parser_begin_omp_structured_block (parser);
22960
22961 cp_parser_statement (parser, NULL_TREE, false, NULL);
22962
22963 cp_parser_end_omp_structured_block (parser, save);
22964 return finish_omp_structured_block (stmt);
22965 }
22966
22967 /* OpenMP 2.5:
22968 # pragma omp atomic new-line
22969 expression-stmt
22970
22971 expression-stmt:
22972 x binop= expr | x++ | ++x | x-- | --x
22973 binop:
22974 +, *, -, /, &, ^, |, <<, >>
22975
22976 where x is an lvalue expression with scalar type. */
22977
22978 static void
22979 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
22980 {
22981 tree lhs, rhs;
22982 enum tree_code code;
22983
22984 cp_parser_require_pragma_eol (parser, pragma_tok);
22985
22986 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
22987 /*cast_p=*/false, NULL);
22988 switch (TREE_CODE (lhs))
22989 {
22990 case ERROR_MARK:
22991 goto saw_error;
22992
22993 case PREINCREMENT_EXPR:
22994 case POSTINCREMENT_EXPR:
22995 lhs = TREE_OPERAND (lhs, 0);
22996 code = PLUS_EXPR;
22997 rhs = integer_one_node;
22998 break;
22999
23000 case PREDECREMENT_EXPR:
23001 case POSTDECREMENT_EXPR:
23002 lhs = TREE_OPERAND (lhs, 0);
23003 code = MINUS_EXPR;
23004 rhs = integer_one_node;
23005 break;
23006
23007 case COMPOUND_EXPR:
23008 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
23009 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
23010 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
23011 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
23012 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23013 (TREE_OPERAND (lhs, 1), 0), 0)))
23014 == BOOLEAN_TYPE)
23015 /* Undo effects of boolean_increment for post {in,de}crement. */
23016 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
23017 /* FALLTHRU */
23018 case MODIFY_EXPR:
23019 if (TREE_CODE (lhs) == MODIFY_EXPR
23020 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
23021 {
23022 /* Undo effects of boolean_increment. */
23023 if (integer_onep (TREE_OPERAND (lhs, 1)))
23024 {
23025 /* This is pre or post increment. */
23026 rhs = TREE_OPERAND (lhs, 1);
23027 lhs = TREE_OPERAND (lhs, 0);
23028 code = NOP_EXPR;
23029 break;
23030 }
23031 }
23032 /* FALLTHRU */
23033 default:
23034 switch (cp_lexer_peek_token (parser->lexer)->type)
23035 {
23036 case CPP_MULT_EQ:
23037 code = MULT_EXPR;
23038 break;
23039 case CPP_DIV_EQ:
23040 code = TRUNC_DIV_EXPR;
23041 break;
23042 case CPP_PLUS_EQ:
23043 code = PLUS_EXPR;
23044 break;
23045 case CPP_MINUS_EQ:
23046 code = MINUS_EXPR;
23047 break;
23048 case CPP_LSHIFT_EQ:
23049 code = LSHIFT_EXPR;
23050 break;
23051 case CPP_RSHIFT_EQ:
23052 code = RSHIFT_EXPR;
23053 break;
23054 case CPP_AND_EQ:
23055 code = BIT_AND_EXPR;
23056 break;
23057 case CPP_OR_EQ:
23058 code = BIT_IOR_EXPR;
23059 break;
23060 case CPP_XOR_EQ:
23061 code = BIT_XOR_EXPR;
23062 break;
23063 default:
23064 cp_parser_error (parser,
23065 "invalid operator for %<#pragma omp atomic%>");
23066 goto saw_error;
23067 }
23068 cp_lexer_consume_token (parser->lexer);
23069
23070 rhs = cp_parser_expression (parser, false, NULL);
23071 if (rhs == error_mark_node)
23072 goto saw_error;
23073 break;
23074 }
23075 finish_omp_atomic (code, lhs, rhs);
23076 cp_parser_consume_semicolon_at_end_of_statement (parser);
23077 return;
23078
23079 saw_error:
23080 cp_parser_skip_to_end_of_block_or_statement (parser);
23081 }
23082
23083
23084 /* OpenMP 2.5:
23085 # pragma omp barrier new-line */
23086
23087 static void
23088 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
23089 {
23090 cp_parser_require_pragma_eol (parser, pragma_tok);
23091 finish_omp_barrier ();
23092 }
23093
23094 /* OpenMP 2.5:
23095 # pragma omp critical [(name)] new-line
23096 structured-block */
23097
23098 static tree
23099 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
23100 {
23101 tree stmt, name = NULL;
23102
23103 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23104 {
23105 cp_lexer_consume_token (parser->lexer);
23106
23107 name = cp_parser_identifier (parser);
23108
23109 if (name == error_mark_node
23110 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23111 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23112 /*or_comma=*/false,
23113 /*consume_paren=*/true);
23114 if (name == error_mark_node)
23115 name = NULL;
23116 }
23117 cp_parser_require_pragma_eol (parser, pragma_tok);
23118
23119 stmt = cp_parser_omp_structured_block (parser);
23120 return c_finish_omp_critical (input_location, stmt, name);
23121 }
23122
23123 /* OpenMP 2.5:
23124 # pragma omp flush flush-vars[opt] new-line
23125
23126 flush-vars:
23127 ( variable-list ) */
23128
23129 static void
23130 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
23131 {
23132 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23133 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23134 cp_parser_require_pragma_eol (parser, pragma_tok);
23135
23136 finish_omp_flush ();
23137 }
23138
23139 /* Helper function, to parse omp for increment expression. */
23140
23141 static tree
23142 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
23143 {
23144 tree cond = cp_parser_binary_expression (parser, false, true,
23145 PREC_NOT_OPERATOR, NULL);
23146 bool overloaded_p;
23147
23148 if (cond == error_mark_node
23149 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23150 {
23151 cp_parser_skip_to_end_of_statement (parser);
23152 return error_mark_node;
23153 }
23154
23155 switch (TREE_CODE (cond))
23156 {
23157 case GT_EXPR:
23158 case GE_EXPR:
23159 case LT_EXPR:
23160 case LE_EXPR:
23161 break;
23162 default:
23163 return error_mark_node;
23164 }
23165
23166 /* If decl is an iterator, preserve LHS and RHS of the relational
23167 expr until finish_omp_for. */
23168 if (decl
23169 && (type_dependent_expression_p (decl)
23170 || CLASS_TYPE_P (TREE_TYPE (decl))))
23171 return cond;
23172
23173 return build_x_binary_op (TREE_CODE (cond),
23174 TREE_OPERAND (cond, 0), ERROR_MARK,
23175 TREE_OPERAND (cond, 1), ERROR_MARK,
23176 &overloaded_p, tf_warning_or_error);
23177 }
23178
23179 /* Helper function, to parse omp for increment expression. */
23180
23181 static tree
23182 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
23183 {
23184 cp_token *token = cp_lexer_peek_token (parser->lexer);
23185 enum tree_code op;
23186 tree lhs, rhs;
23187 cp_id_kind idk;
23188 bool decl_first;
23189
23190 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23191 {
23192 op = (token->type == CPP_PLUS_PLUS
23193 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
23194 cp_lexer_consume_token (parser->lexer);
23195 lhs = cp_parser_cast_expression (parser, false, false, NULL);
23196 if (lhs != decl)
23197 return error_mark_node;
23198 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23199 }
23200
23201 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
23202 if (lhs != decl)
23203 return error_mark_node;
23204
23205 token = cp_lexer_peek_token (parser->lexer);
23206 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23207 {
23208 op = (token->type == CPP_PLUS_PLUS
23209 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
23210 cp_lexer_consume_token (parser->lexer);
23211 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23212 }
23213
23214 op = cp_parser_assignment_operator_opt (parser);
23215 if (op == ERROR_MARK)
23216 return error_mark_node;
23217
23218 if (op != NOP_EXPR)
23219 {
23220 rhs = cp_parser_assignment_expression (parser, false, NULL);
23221 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
23222 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
23223 }
23224
23225 lhs = cp_parser_binary_expression (parser, false, false,
23226 PREC_ADDITIVE_EXPRESSION, NULL);
23227 token = cp_lexer_peek_token (parser->lexer);
23228 decl_first = lhs == decl;
23229 if (decl_first)
23230 lhs = NULL_TREE;
23231 if (token->type != CPP_PLUS
23232 && token->type != CPP_MINUS)
23233 return error_mark_node;
23234
23235 do
23236 {
23237 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
23238 cp_lexer_consume_token (parser->lexer);
23239 rhs = cp_parser_binary_expression (parser, false, false,
23240 PREC_ADDITIVE_EXPRESSION, NULL);
23241 token = cp_lexer_peek_token (parser->lexer);
23242 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
23243 {
23244 if (lhs == NULL_TREE)
23245 {
23246 if (op == PLUS_EXPR)
23247 lhs = rhs;
23248 else
23249 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
23250 }
23251 else
23252 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
23253 NULL, tf_warning_or_error);
23254 }
23255 }
23256 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
23257
23258 if (!decl_first)
23259 {
23260 if (rhs != decl || op == MINUS_EXPR)
23261 return error_mark_node;
23262 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
23263 }
23264 else
23265 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
23266
23267 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
23268 }
23269
23270 /* Parse the restricted form of the for statement allowed by OpenMP. */
23271
23272 static tree
23273 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
23274 {
23275 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
23276 tree real_decl, initv, condv, incrv, declv;
23277 tree this_pre_body, cl;
23278 location_t loc_first;
23279 bool collapse_err = false;
23280 int i, collapse = 1, nbraces = 0;
23281 VEC(tree,gc) *for_block = make_tree_vector ();
23282
23283 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
23284 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
23285 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
23286
23287 gcc_assert (collapse >= 1);
23288
23289 declv = make_tree_vec (collapse);
23290 initv = make_tree_vec (collapse);
23291 condv = make_tree_vec (collapse);
23292 incrv = make_tree_vec (collapse);
23293
23294 loc_first = cp_lexer_peek_token (parser->lexer)->location;
23295
23296 for (i = 0; i < collapse; i++)
23297 {
23298 int bracecount = 0;
23299 bool add_private_clause = false;
23300 location_t loc;
23301
23302 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23303 {
23304 cp_parser_error (parser, "for statement expected");
23305 return NULL;
23306 }
23307 loc = cp_lexer_consume_token (parser->lexer)->location;
23308
23309 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23310 return NULL;
23311
23312 init = decl = real_decl = NULL;
23313 this_pre_body = push_stmt_list ();
23314 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23315 {
23316 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
23317
23318 init-expr:
23319 var = lb
23320 integer-type var = lb
23321 random-access-iterator-type var = lb
23322 pointer-type var = lb
23323 */
23324 cp_decl_specifier_seq type_specifiers;
23325
23326 /* First, try to parse as an initialized declaration. See
23327 cp_parser_condition, from whence the bulk of this is copied. */
23328
23329 cp_parser_parse_tentatively (parser);
23330 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
23331 /*is_trailing_return=*/false,
23332 &type_specifiers);
23333 if (cp_parser_parse_definitely (parser))
23334 {
23335 /* If parsing a type specifier seq succeeded, then this
23336 MUST be a initialized declaration. */
23337 tree asm_specification, attributes;
23338 cp_declarator *declarator;
23339
23340 declarator = cp_parser_declarator (parser,
23341 CP_PARSER_DECLARATOR_NAMED,
23342 /*ctor_dtor_or_conv_p=*/NULL,
23343 /*parenthesized_p=*/NULL,
23344 /*member_p=*/false);
23345 attributes = cp_parser_attributes_opt (parser);
23346 asm_specification = cp_parser_asm_specification_opt (parser);
23347
23348 if (declarator == cp_error_declarator)
23349 cp_parser_skip_to_end_of_statement (parser);
23350
23351 else
23352 {
23353 tree pushed_scope, auto_node;
23354
23355 decl = start_decl (declarator, &type_specifiers,
23356 SD_INITIALIZED, attributes,
23357 /*prefix_attributes=*/NULL_TREE,
23358 &pushed_scope);
23359
23360 auto_node = type_uses_auto (TREE_TYPE (decl));
23361 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23362 {
23363 if (cp_lexer_next_token_is (parser->lexer,
23364 CPP_OPEN_PAREN))
23365 error ("parenthesized initialization is not allowed in "
23366 "OpenMP %<for%> loop");
23367 else
23368 /* Trigger an error. */
23369 cp_parser_require (parser, CPP_EQ, RT_EQ);
23370
23371 init = error_mark_node;
23372 cp_parser_skip_to_end_of_statement (parser);
23373 }
23374 else if (CLASS_TYPE_P (TREE_TYPE (decl))
23375 || type_dependent_expression_p (decl)
23376 || auto_node)
23377 {
23378 bool is_direct_init, is_non_constant_init;
23379
23380 init = cp_parser_initializer (parser,
23381 &is_direct_init,
23382 &is_non_constant_init);
23383
23384 if (auto_node && describable_type (init))
23385 {
23386 TREE_TYPE (decl)
23387 = do_auto_deduction (TREE_TYPE (decl), init,
23388 auto_node);
23389
23390 if (!CLASS_TYPE_P (TREE_TYPE (decl))
23391 && !type_dependent_expression_p (decl))
23392 goto non_class;
23393 }
23394
23395 cp_finish_decl (decl, init, !is_non_constant_init,
23396 asm_specification,
23397 LOOKUP_ONLYCONVERTING);
23398 if (CLASS_TYPE_P (TREE_TYPE (decl)))
23399 {
23400 VEC_safe_push (tree, gc, for_block, this_pre_body);
23401 init = NULL_TREE;
23402 }
23403 else
23404 init = pop_stmt_list (this_pre_body);
23405 this_pre_body = NULL_TREE;
23406 }
23407 else
23408 {
23409 /* Consume '='. */
23410 cp_lexer_consume_token (parser->lexer);
23411 init = cp_parser_assignment_expression (parser, false, NULL);
23412
23413 non_class:
23414 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
23415 init = error_mark_node;
23416 else
23417 cp_finish_decl (decl, NULL_TREE,
23418 /*init_const_expr_p=*/false,
23419 asm_specification,
23420 LOOKUP_ONLYCONVERTING);
23421 }
23422
23423 if (pushed_scope)
23424 pop_scope (pushed_scope);
23425 }
23426 }
23427 else
23428 {
23429 cp_id_kind idk;
23430 /* If parsing a type specifier sequence failed, then
23431 this MUST be a simple expression. */
23432 cp_parser_parse_tentatively (parser);
23433 decl = cp_parser_primary_expression (parser, false, false,
23434 false, &idk);
23435 if (!cp_parser_error_occurred (parser)
23436 && decl
23437 && DECL_P (decl)
23438 && CLASS_TYPE_P (TREE_TYPE (decl)))
23439 {
23440 tree rhs;
23441
23442 cp_parser_parse_definitely (parser);
23443 cp_parser_require (parser, CPP_EQ, RT_EQ);
23444 rhs = cp_parser_assignment_expression (parser, false, NULL);
23445 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
23446 rhs,
23447 tf_warning_or_error));
23448 add_private_clause = true;
23449 }
23450 else
23451 {
23452 decl = NULL;
23453 cp_parser_abort_tentative_parse (parser);
23454 init = cp_parser_expression (parser, false, NULL);
23455 if (init)
23456 {
23457 if (TREE_CODE (init) == MODIFY_EXPR
23458 || TREE_CODE (init) == MODOP_EXPR)
23459 real_decl = TREE_OPERAND (init, 0);
23460 }
23461 }
23462 }
23463 }
23464 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23465 if (this_pre_body)
23466 {
23467 this_pre_body = pop_stmt_list (this_pre_body);
23468 if (pre_body)
23469 {
23470 tree t = pre_body;
23471 pre_body = push_stmt_list ();
23472 add_stmt (t);
23473 add_stmt (this_pre_body);
23474 pre_body = pop_stmt_list (pre_body);
23475 }
23476 else
23477 pre_body = this_pre_body;
23478 }
23479
23480 if (decl)
23481 real_decl = decl;
23482 if (par_clauses != NULL && real_decl != NULL_TREE)
23483 {
23484 tree *c;
23485 for (c = par_clauses; *c ; )
23486 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
23487 && OMP_CLAUSE_DECL (*c) == real_decl)
23488 {
23489 error_at (loc, "iteration variable %qD"
23490 " should not be firstprivate", real_decl);
23491 *c = OMP_CLAUSE_CHAIN (*c);
23492 }
23493 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
23494 && OMP_CLAUSE_DECL (*c) == real_decl)
23495 {
23496 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
23497 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
23498 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
23499 OMP_CLAUSE_DECL (l) = real_decl;
23500 OMP_CLAUSE_CHAIN (l) = clauses;
23501 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
23502 clauses = l;
23503 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
23504 CP_OMP_CLAUSE_INFO (*c) = NULL;
23505 add_private_clause = false;
23506 }
23507 else
23508 {
23509 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
23510 && OMP_CLAUSE_DECL (*c) == real_decl)
23511 add_private_clause = false;
23512 c = &OMP_CLAUSE_CHAIN (*c);
23513 }
23514 }
23515
23516 if (add_private_clause)
23517 {
23518 tree c;
23519 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23520 {
23521 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
23522 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
23523 && OMP_CLAUSE_DECL (c) == decl)
23524 break;
23525 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
23526 && OMP_CLAUSE_DECL (c) == decl)
23527 error_at (loc, "iteration variable %qD "
23528 "should not be firstprivate",
23529 decl);
23530 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
23531 && OMP_CLAUSE_DECL (c) == decl)
23532 error_at (loc, "iteration variable %qD should not be reduction",
23533 decl);
23534 }
23535 if (c == NULL)
23536 {
23537 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
23538 OMP_CLAUSE_DECL (c) = decl;
23539 c = finish_omp_clauses (c);
23540 if (c)
23541 {
23542 OMP_CLAUSE_CHAIN (c) = clauses;
23543 clauses = c;
23544 }
23545 }
23546 }
23547
23548 cond = NULL;
23549 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23550 cond = cp_parser_omp_for_cond (parser, decl);
23551 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23552
23553 incr = NULL;
23554 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23555 {
23556 /* If decl is an iterator, preserve the operator on decl
23557 until finish_omp_for. */
23558 if (decl
23559 && (type_dependent_expression_p (decl)
23560 || CLASS_TYPE_P (TREE_TYPE (decl))))
23561 incr = cp_parser_omp_for_incr (parser, decl);
23562 else
23563 incr = cp_parser_expression (parser, false, NULL);
23564 }
23565
23566 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23567 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23568 /*or_comma=*/false,
23569 /*consume_paren=*/true);
23570
23571 TREE_VEC_ELT (declv, i) = decl;
23572 TREE_VEC_ELT (initv, i) = init;
23573 TREE_VEC_ELT (condv, i) = cond;
23574 TREE_VEC_ELT (incrv, i) = incr;
23575
23576 if (i == collapse - 1)
23577 break;
23578
23579 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
23580 in between the collapsed for loops to be still considered perfectly
23581 nested. Hopefully the final version clarifies this.
23582 For now handle (multiple) {'s and empty statements. */
23583 cp_parser_parse_tentatively (parser);
23584 do
23585 {
23586 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23587 break;
23588 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23589 {
23590 cp_lexer_consume_token (parser->lexer);
23591 bracecount++;
23592 }
23593 else if (bracecount
23594 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23595 cp_lexer_consume_token (parser->lexer);
23596 else
23597 {
23598 loc = cp_lexer_peek_token (parser->lexer)->location;
23599 error_at (loc, "not enough collapsed for loops");
23600 collapse_err = true;
23601 cp_parser_abort_tentative_parse (parser);
23602 declv = NULL_TREE;
23603 break;
23604 }
23605 }
23606 while (1);
23607
23608 if (declv)
23609 {
23610 cp_parser_parse_definitely (parser);
23611 nbraces += bracecount;
23612 }
23613 }
23614
23615 /* Note that we saved the original contents of this flag when we entered
23616 the structured block, and so we don't need to re-save it here. */
23617 parser->in_statement = IN_OMP_FOR;
23618
23619 /* Note that the grammar doesn't call for a structured block here,
23620 though the loop as a whole is a structured block. */
23621 body = push_stmt_list ();
23622 cp_parser_statement (parser, NULL_TREE, false, NULL);
23623 body = pop_stmt_list (body);
23624
23625 if (declv == NULL_TREE)
23626 ret = NULL_TREE;
23627 else
23628 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
23629 pre_body, clauses);
23630
23631 while (nbraces)
23632 {
23633 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23634 {
23635 cp_lexer_consume_token (parser->lexer);
23636 nbraces--;
23637 }
23638 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23639 cp_lexer_consume_token (parser->lexer);
23640 else
23641 {
23642 if (!collapse_err)
23643 {
23644 error_at (cp_lexer_peek_token (parser->lexer)->location,
23645 "collapsed loops not perfectly nested");
23646 }
23647 collapse_err = true;
23648 cp_parser_statement_seq_opt (parser, NULL);
23649 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23650 break;
23651 }
23652 }
23653
23654 while (!VEC_empty (tree, for_block))
23655 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
23656 release_tree_vector (for_block);
23657
23658 return ret;
23659 }
23660
23661 /* OpenMP 2.5:
23662 #pragma omp for for-clause[optseq] new-line
23663 for-loop */
23664
23665 #define OMP_FOR_CLAUSE_MASK \
23666 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23667 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23668 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
23669 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23670 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
23671 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
23672 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
23673 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
23674
23675 static tree
23676 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
23677 {
23678 tree clauses, sb, ret;
23679 unsigned int save;
23680
23681 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
23682 "#pragma omp for", pragma_tok);
23683
23684 sb = begin_omp_structured_block ();
23685 save = cp_parser_begin_omp_structured_block (parser);
23686
23687 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
23688
23689 cp_parser_end_omp_structured_block (parser, save);
23690 add_stmt (finish_omp_structured_block (sb));
23691
23692 return ret;
23693 }
23694
23695 /* OpenMP 2.5:
23696 # pragma omp master new-line
23697 structured-block */
23698
23699 static tree
23700 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
23701 {
23702 cp_parser_require_pragma_eol (parser, pragma_tok);
23703 return c_finish_omp_master (input_location,
23704 cp_parser_omp_structured_block (parser));
23705 }
23706
23707 /* OpenMP 2.5:
23708 # pragma omp ordered new-line
23709 structured-block */
23710
23711 static tree
23712 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
23713 {
23714 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23715 cp_parser_require_pragma_eol (parser, pragma_tok);
23716 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
23717 }
23718
23719 /* OpenMP 2.5:
23720
23721 section-scope:
23722 { section-sequence }
23723
23724 section-sequence:
23725 section-directive[opt] structured-block
23726 section-sequence section-directive structured-block */
23727
23728 static tree
23729 cp_parser_omp_sections_scope (cp_parser *parser)
23730 {
23731 tree stmt, substmt;
23732 bool error_suppress = false;
23733 cp_token *tok;
23734
23735 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
23736 return NULL_TREE;
23737
23738 stmt = push_stmt_list ();
23739
23740 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
23741 {
23742 unsigned save;
23743
23744 substmt = begin_omp_structured_block ();
23745 save = cp_parser_begin_omp_structured_block (parser);
23746
23747 while (1)
23748 {
23749 cp_parser_statement (parser, NULL_TREE, false, NULL);
23750
23751 tok = cp_lexer_peek_token (parser->lexer);
23752 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
23753 break;
23754 if (tok->type == CPP_CLOSE_BRACE)
23755 break;
23756 if (tok->type == CPP_EOF)
23757 break;
23758 }
23759
23760 cp_parser_end_omp_structured_block (parser, save);
23761 substmt = finish_omp_structured_block (substmt);
23762 substmt = build1 (OMP_SECTION, void_type_node, substmt);
23763 add_stmt (substmt);
23764 }
23765
23766 while (1)
23767 {
23768 tok = cp_lexer_peek_token (parser->lexer);
23769 if (tok->type == CPP_CLOSE_BRACE)
23770 break;
23771 if (tok->type == CPP_EOF)
23772 break;
23773
23774 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
23775 {
23776 cp_lexer_consume_token (parser->lexer);
23777 cp_parser_require_pragma_eol (parser, tok);
23778 error_suppress = false;
23779 }
23780 else if (!error_suppress)
23781 {
23782 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
23783 error_suppress = true;
23784 }
23785
23786 substmt = cp_parser_omp_structured_block (parser);
23787 substmt = build1 (OMP_SECTION, void_type_node, substmt);
23788 add_stmt (substmt);
23789 }
23790 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23791
23792 substmt = pop_stmt_list (stmt);
23793
23794 stmt = make_node (OMP_SECTIONS);
23795 TREE_TYPE (stmt) = void_type_node;
23796 OMP_SECTIONS_BODY (stmt) = substmt;
23797
23798 add_stmt (stmt);
23799 return stmt;
23800 }
23801
23802 /* OpenMP 2.5:
23803 # pragma omp sections sections-clause[optseq] newline
23804 sections-scope */
23805
23806 #define OMP_SECTIONS_CLAUSE_MASK \
23807 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23808 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23809 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
23810 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23811 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
23812
23813 static tree
23814 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
23815 {
23816 tree clauses, ret;
23817
23818 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
23819 "#pragma omp sections", pragma_tok);
23820
23821 ret = cp_parser_omp_sections_scope (parser);
23822 if (ret)
23823 OMP_SECTIONS_CLAUSES (ret) = clauses;
23824
23825 return ret;
23826 }
23827
23828 /* OpenMP 2.5:
23829 # pragma parallel parallel-clause new-line
23830 # pragma parallel for parallel-for-clause new-line
23831 # pragma parallel sections parallel-sections-clause new-line */
23832
23833 #define OMP_PARALLEL_CLAUSE_MASK \
23834 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
23835 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23836 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23837 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
23838 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
23839 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
23840 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23841 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
23842
23843 static tree
23844 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
23845 {
23846 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
23847 const char *p_name = "#pragma omp parallel";
23848 tree stmt, clauses, par_clause, ws_clause, block;
23849 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
23850 unsigned int save;
23851 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23852
23853 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23854 {
23855 cp_lexer_consume_token (parser->lexer);
23856 p_kind = PRAGMA_OMP_PARALLEL_FOR;
23857 p_name = "#pragma omp parallel for";
23858 mask |= OMP_FOR_CLAUSE_MASK;
23859 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
23860 }
23861 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23862 {
23863 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23864 const char *p = IDENTIFIER_POINTER (id);
23865 if (strcmp (p, "sections") == 0)
23866 {
23867 cp_lexer_consume_token (parser->lexer);
23868 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
23869 p_name = "#pragma omp parallel sections";
23870 mask |= OMP_SECTIONS_CLAUSE_MASK;
23871 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
23872 }
23873 }
23874
23875 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
23876 block = begin_omp_parallel ();
23877 save = cp_parser_begin_omp_structured_block (parser);
23878
23879 switch (p_kind)
23880 {
23881 case PRAGMA_OMP_PARALLEL:
23882 cp_parser_statement (parser, NULL_TREE, false, NULL);
23883 par_clause = clauses;
23884 break;
23885
23886 case PRAGMA_OMP_PARALLEL_FOR:
23887 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
23888 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
23889 break;
23890
23891 case PRAGMA_OMP_PARALLEL_SECTIONS:
23892 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
23893 stmt = cp_parser_omp_sections_scope (parser);
23894 if (stmt)
23895 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
23896 break;
23897
23898 default:
23899 gcc_unreachable ();
23900 }
23901
23902 cp_parser_end_omp_structured_block (parser, save);
23903 stmt = finish_omp_parallel (par_clause, block);
23904 if (p_kind != PRAGMA_OMP_PARALLEL)
23905 OMP_PARALLEL_COMBINED (stmt) = 1;
23906 return stmt;
23907 }
23908
23909 /* OpenMP 2.5:
23910 # pragma omp single single-clause[optseq] new-line
23911 structured-block */
23912
23913 #define OMP_SINGLE_CLAUSE_MASK \
23914 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23915 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23916 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
23917 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
23918
23919 static tree
23920 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
23921 {
23922 tree stmt = make_node (OMP_SINGLE);
23923 TREE_TYPE (stmt) = void_type_node;
23924
23925 OMP_SINGLE_CLAUSES (stmt)
23926 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
23927 "#pragma omp single", pragma_tok);
23928 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
23929
23930 return add_stmt (stmt);
23931 }
23932
23933 /* OpenMP 3.0:
23934 # pragma omp task task-clause[optseq] new-line
23935 structured-block */
23936
23937 #define OMP_TASK_CLAUSE_MASK \
23938 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
23939 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
23940 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
23941 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23942 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23943 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
23944
23945 static tree
23946 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
23947 {
23948 tree clauses, block;
23949 unsigned int save;
23950
23951 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
23952 "#pragma omp task", pragma_tok);
23953 block = begin_omp_task ();
23954 save = cp_parser_begin_omp_structured_block (parser);
23955 cp_parser_statement (parser, NULL_TREE, false, NULL);
23956 cp_parser_end_omp_structured_block (parser, save);
23957 return finish_omp_task (clauses, block);
23958 }
23959
23960 /* OpenMP 3.0:
23961 # pragma omp taskwait new-line */
23962
23963 static void
23964 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
23965 {
23966 cp_parser_require_pragma_eol (parser, pragma_tok);
23967 finish_omp_taskwait ();
23968 }
23969
23970 /* OpenMP 2.5:
23971 # pragma omp threadprivate (variable-list) */
23972
23973 static void
23974 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
23975 {
23976 tree vars;
23977
23978 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23979 cp_parser_require_pragma_eol (parser, pragma_tok);
23980
23981 finish_omp_threadprivate (vars);
23982 }
23983
23984 /* Main entry point to OpenMP statement pragmas. */
23985
23986 static void
23987 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
23988 {
23989 tree stmt;
23990
23991 switch (pragma_tok->pragma_kind)
23992 {
23993 case PRAGMA_OMP_ATOMIC:
23994 cp_parser_omp_atomic (parser, pragma_tok);
23995 return;
23996 case PRAGMA_OMP_CRITICAL:
23997 stmt = cp_parser_omp_critical (parser, pragma_tok);
23998 break;
23999 case PRAGMA_OMP_FOR:
24000 stmt = cp_parser_omp_for (parser, pragma_tok);
24001 break;
24002 case PRAGMA_OMP_MASTER:
24003 stmt = cp_parser_omp_master (parser, pragma_tok);
24004 break;
24005 case PRAGMA_OMP_ORDERED:
24006 stmt = cp_parser_omp_ordered (parser, pragma_tok);
24007 break;
24008 case PRAGMA_OMP_PARALLEL:
24009 stmt = cp_parser_omp_parallel (parser, pragma_tok);
24010 break;
24011 case PRAGMA_OMP_SECTIONS:
24012 stmt = cp_parser_omp_sections (parser, pragma_tok);
24013 break;
24014 case PRAGMA_OMP_SINGLE:
24015 stmt = cp_parser_omp_single (parser, pragma_tok);
24016 break;
24017 case PRAGMA_OMP_TASK:
24018 stmt = cp_parser_omp_task (parser, pragma_tok);
24019 break;
24020 default:
24021 gcc_unreachable ();
24022 }
24023
24024 if (stmt)
24025 SET_EXPR_LOCATION (stmt, pragma_tok->location);
24026 }
24027 \f
24028 /* The parser. */
24029
24030 static GTY (()) cp_parser *the_parser;
24031
24032 \f
24033 /* Special handling for the first token or line in the file. The first
24034 thing in the file might be #pragma GCC pch_preprocess, which loads a
24035 PCH file, which is a GC collection point. So we need to handle this
24036 first pragma without benefit of an existing lexer structure.
24037
24038 Always returns one token to the caller in *FIRST_TOKEN. This is
24039 either the true first token of the file, or the first token after
24040 the initial pragma. */
24041
24042 static void
24043 cp_parser_initial_pragma (cp_token *first_token)
24044 {
24045 tree name = NULL;
24046
24047 cp_lexer_get_preprocessor_token (NULL, first_token);
24048 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
24049 return;
24050
24051 cp_lexer_get_preprocessor_token (NULL, first_token);
24052 if (first_token->type == CPP_STRING)
24053 {
24054 name = first_token->u.value;
24055
24056 cp_lexer_get_preprocessor_token (NULL, first_token);
24057 if (first_token->type != CPP_PRAGMA_EOL)
24058 error_at (first_token->location,
24059 "junk at end of %<#pragma GCC pch_preprocess%>");
24060 }
24061 else
24062 error_at (first_token->location, "expected string literal");
24063
24064 /* Skip to the end of the pragma. */
24065 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
24066 cp_lexer_get_preprocessor_token (NULL, first_token);
24067
24068 /* Now actually load the PCH file. */
24069 if (name)
24070 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
24071
24072 /* Read one more token to return to our caller. We have to do this
24073 after reading the PCH file in, since its pointers have to be
24074 live. */
24075 cp_lexer_get_preprocessor_token (NULL, first_token);
24076 }
24077
24078 /* Normal parsing of a pragma token. Here we can (and must) use the
24079 regular lexer. */
24080
24081 static bool
24082 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
24083 {
24084 cp_token *pragma_tok;
24085 unsigned int id;
24086
24087 pragma_tok = cp_lexer_consume_token (parser->lexer);
24088 gcc_assert (pragma_tok->type == CPP_PRAGMA);
24089 parser->lexer->in_pragma = true;
24090
24091 id = pragma_tok->pragma_kind;
24092 switch (id)
24093 {
24094 case PRAGMA_GCC_PCH_PREPROCESS:
24095 error_at (pragma_tok->location,
24096 "%<#pragma GCC pch_preprocess%> must be first");
24097 break;
24098
24099 case PRAGMA_OMP_BARRIER:
24100 switch (context)
24101 {
24102 case pragma_compound:
24103 cp_parser_omp_barrier (parser, pragma_tok);
24104 return false;
24105 case pragma_stmt:
24106 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
24107 "used in compound statements");
24108 break;
24109 default:
24110 goto bad_stmt;
24111 }
24112 break;
24113
24114 case PRAGMA_OMP_FLUSH:
24115 switch (context)
24116 {
24117 case pragma_compound:
24118 cp_parser_omp_flush (parser, pragma_tok);
24119 return false;
24120 case pragma_stmt:
24121 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
24122 "used in compound statements");
24123 break;
24124 default:
24125 goto bad_stmt;
24126 }
24127 break;
24128
24129 case PRAGMA_OMP_TASKWAIT:
24130 switch (context)
24131 {
24132 case pragma_compound:
24133 cp_parser_omp_taskwait (parser, pragma_tok);
24134 return false;
24135 case pragma_stmt:
24136 error_at (pragma_tok->location,
24137 "%<#pragma omp taskwait%> may only be "
24138 "used in compound statements");
24139 break;
24140 default:
24141 goto bad_stmt;
24142 }
24143 break;
24144
24145 case PRAGMA_OMP_THREADPRIVATE:
24146 cp_parser_omp_threadprivate (parser, pragma_tok);
24147 return false;
24148
24149 case PRAGMA_OMP_ATOMIC:
24150 case PRAGMA_OMP_CRITICAL:
24151 case PRAGMA_OMP_FOR:
24152 case PRAGMA_OMP_MASTER:
24153 case PRAGMA_OMP_ORDERED:
24154 case PRAGMA_OMP_PARALLEL:
24155 case PRAGMA_OMP_SECTIONS:
24156 case PRAGMA_OMP_SINGLE:
24157 case PRAGMA_OMP_TASK:
24158 if (context == pragma_external)
24159 goto bad_stmt;
24160 cp_parser_omp_construct (parser, pragma_tok);
24161 return true;
24162
24163 case PRAGMA_OMP_SECTION:
24164 error_at (pragma_tok->location,
24165 "%<#pragma omp section%> may only be used in "
24166 "%<#pragma omp sections%> construct");
24167 break;
24168
24169 default:
24170 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
24171 c_invoke_pragma_handler (id);
24172 break;
24173
24174 bad_stmt:
24175 cp_parser_error (parser, "expected declaration specifiers");
24176 break;
24177 }
24178
24179 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
24180 return false;
24181 }
24182
24183 /* The interface the pragma parsers have to the lexer. */
24184
24185 enum cpp_ttype
24186 pragma_lex (tree *value)
24187 {
24188 cp_token *tok;
24189 enum cpp_ttype ret;
24190
24191 tok = cp_lexer_peek_token (the_parser->lexer);
24192
24193 ret = tok->type;
24194 *value = tok->u.value;
24195
24196 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
24197 ret = CPP_EOF;
24198 else if (ret == CPP_STRING)
24199 *value = cp_parser_string_literal (the_parser, false, false);
24200 else
24201 {
24202 cp_lexer_consume_token (the_parser->lexer);
24203 if (ret == CPP_KEYWORD)
24204 ret = CPP_NAME;
24205 }
24206
24207 return ret;
24208 }
24209
24210 \f
24211 /* External interface. */
24212
24213 /* Parse one entire translation unit. */
24214
24215 void
24216 c_parse_file (void)
24217 {
24218 static bool already_called = false;
24219
24220 if (already_called)
24221 {
24222 sorry ("inter-module optimizations not implemented for C++");
24223 return;
24224 }
24225 already_called = true;
24226
24227 the_parser = cp_parser_new ();
24228 push_deferring_access_checks (flag_access_control
24229 ? dk_no_deferred : dk_no_check);
24230 cp_parser_translation_unit (the_parser);
24231 the_parser = NULL;
24232 }
24233
24234 #include "gt-cp-parser.h"