In gcc/cp/: 2010-10-17 Nicola Pero <nicola.pero@meta-innovation.com>
[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 *, tree *);
2101 static void cp_parser_objc_at_property
2102 (cp_parser *) ;
2103 static void cp_parser_objc_property_decl
2104 (cp_parser *) ;
2105
2106 /* Utility Routines */
2107
2108 static tree cp_parser_lookup_name
2109 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2110 static tree cp_parser_lookup_name_simple
2111 (cp_parser *, tree, location_t);
2112 static tree cp_parser_maybe_treat_template_as_class
2113 (tree, bool);
2114 static bool cp_parser_check_declarator_template_parameters
2115 (cp_parser *, cp_declarator *, location_t);
2116 static bool cp_parser_check_template_parameters
2117 (cp_parser *, unsigned, location_t, cp_declarator *);
2118 static tree cp_parser_simple_cast_expression
2119 (cp_parser *);
2120 static tree cp_parser_global_scope_opt
2121 (cp_parser *, bool);
2122 static bool cp_parser_constructor_declarator_p
2123 (cp_parser *, bool);
2124 static tree cp_parser_function_definition_from_specifiers_and_declarator
2125 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2126 static tree cp_parser_function_definition_after_declarator
2127 (cp_parser *, bool);
2128 static void cp_parser_template_declaration_after_export
2129 (cp_parser *, bool);
2130 static void cp_parser_perform_template_parameter_access_checks
2131 (VEC (deferred_access_check,gc)*);
2132 static tree cp_parser_single_declaration
2133 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2134 static tree cp_parser_functional_cast
2135 (cp_parser *, tree);
2136 static tree cp_parser_save_member_function_body
2137 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2138 static tree cp_parser_enclosed_template_argument_list
2139 (cp_parser *);
2140 static void cp_parser_save_default_args
2141 (cp_parser *, tree);
2142 static void cp_parser_late_parsing_for_member
2143 (cp_parser *, tree);
2144 static void cp_parser_late_parsing_default_args
2145 (cp_parser *, tree);
2146 static tree cp_parser_sizeof_operand
2147 (cp_parser *, enum rid);
2148 static tree cp_parser_trait_expr
2149 (cp_parser *, enum rid);
2150 static bool cp_parser_declares_only_class_p
2151 (cp_parser *);
2152 static void cp_parser_set_storage_class
2153 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2154 static void cp_parser_set_decl_spec_type
2155 (cp_decl_specifier_seq *, tree, location_t, bool);
2156 static bool cp_parser_friend_p
2157 (const cp_decl_specifier_seq *);
2158 static void cp_parser_required_error
2159 (cp_parser *, required_token, bool);
2160 static cp_token *cp_parser_require
2161 (cp_parser *, enum cpp_ttype, required_token);
2162 static cp_token *cp_parser_require_keyword
2163 (cp_parser *, enum rid, required_token);
2164 static bool cp_parser_token_starts_function_definition_p
2165 (cp_token *);
2166 static bool cp_parser_next_token_starts_class_definition_p
2167 (cp_parser *);
2168 static bool cp_parser_next_token_ends_template_argument_p
2169 (cp_parser *);
2170 static bool cp_parser_nth_token_starts_template_argument_list_p
2171 (cp_parser *, size_t);
2172 static enum tag_types cp_parser_token_is_class_key
2173 (cp_token *);
2174 static void cp_parser_check_class_key
2175 (enum tag_types, tree type);
2176 static void cp_parser_check_access_in_redeclaration
2177 (tree type, location_t location);
2178 static bool cp_parser_optional_template_keyword
2179 (cp_parser *);
2180 static void cp_parser_pre_parsed_nested_name_specifier
2181 (cp_parser *);
2182 static bool cp_parser_cache_group
2183 (cp_parser *, enum cpp_ttype, unsigned);
2184 static void cp_parser_parse_tentatively
2185 (cp_parser *);
2186 static void cp_parser_commit_to_tentative_parse
2187 (cp_parser *);
2188 static void cp_parser_abort_tentative_parse
2189 (cp_parser *);
2190 static bool cp_parser_parse_definitely
2191 (cp_parser *);
2192 static inline bool cp_parser_parsing_tentatively
2193 (cp_parser *);
2194 static bool cp_parser_uncommitted_to_tentative_parse_p
2195 (cp_parser *);
2196 static void cp_parser_error
2197 (cp_parser *, const char *);
2198 static void cp_parser_name_lookup_error
2199 (cp_parser *, tree, tree, name_lookup_error, location_t);
2200 static bool cp_parser_simulate_error
2201 (cp_parser *);
2202 static bool cp_parser_check_type_definition
2203 (cp_parser *);
2204 static void cp_parser_check_for_definition_in_return_type
2205 (cp_declarator *, tree, location_t type_location);
2206 static void cp_parser_check_for_invalid_template_id
2207 (cp_parser *, tree, location_t location);
2208 static bool cp_parser_non_integral_constant_expression
2209 (cp_parser *, non_integral_constant);
2210 static void cp_parser_diagnose_invalid_type_name
2211 (cp_parser *, tree, tree, location_t);
2212 static bool cp_parser_parse_and_diagnose_invalid_type_name
2213 (cp_parser *);
2214 static int cp_parser_skip_to_closing_parenthesis
2215 (cp_parser *, bool, bool, bool);
2216 static void cp_parser_skip_to_end_of_statement
2217 (cp_parser *);
2218 static void cp_parser_consume_semicolon_at_end_of_statement
2219 (cp_parser *);
2220 static void cp_parser_skip_to_end_of_block_or_statement
2221 (cp_parser *);
2222 static bool cp_parser_skip_to_closing_brace
2223 (cp_parser *);
2224 static void cp_parser_skip_to_end_of_template_parameter_list
2225 (cp_parser *);
2226 static void cp_parser_skip_to_pragma_eol
2227 (cp_parser*, cp_token *);
2228 static bool cp_parser_error_occurred
2229 (cp_parser *);
2230 static bool cp_parser_allow_gnu_extensions_p
2231 (cp_parser *);
2232 static bool cp_parser_is_string_literal
2233 (cp_token *);
2234 static bool cp_parser_is_keyword
2235 (cp_token *, enum rid);
2236 static tree cp_parser_make_typename_type
2237 (cp_parser *, tree, tree, location_t location);
2238 static cp_declarator * cp_parser_make_indirect_declarator
2239 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2240
2241 /* Returns nonzero if we are parsing tentatively. */
2242
2243 static inline bool
2244 cp_parser_parsing_tentatively (cp_parser* parser)
2245 {
2246 return parser->context->next != NULL;
2247 }
2248
2249 /* Returns nonzero if TOKEN is a string literal. */
2250
2251 static bool
2252 cp_parser_is_string_literal (cp_token* token)
2253 {
2254 return (token->type == CPP_STRING ||
2255 token->type == CPP_STRING16 ||
2256 token->type == CPP_STRING32 ||
2257 token->type == CPP_WSTRING ||
2258 token->type == CPP_UTF8STRING);
2259 }
2260
2261 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2262
2263 static bool
2264 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2265 {
2266 return token->keyword == keyword;
2267 }
2268
2269 /* If not parsing tentatively, issue a diagnostic of the form
2270 FILE:LINE: MESSAGE before TOKEN
2271 where TOKEN is the next token in the input stream. MESSAGE
2272 (specified by the caller) is usually of the form "expected
2273 OTHER-TOKEN". */
2274
2275 static void
2276 cp_parser_error (cp_parser* parser, const char* gmsgid)
2277 {
2278 if (!cp_parser_simulate_error (parser))
2279 {
2280 cp_token *token = cp_lexer_peek_token (parser->lexer);
2281 /* This diagnostic makes more sense if it is tagged to the line
2282 of the token we just peeked at. */
2283 cp_lexer_set_source_position_from_token (token);
2284
2285 if (token->type == CPP_PRAGMA)
2286 {
2287 error_at (token->location,
2288 "%<#pragma%> is not allowed here");
2289 cp_parser_skip_to_pragma_eol (parser, token);
2290 return;
2291 }
2292
2293 c_parse_error (gmsgid,
2294 /* Because c_parser_error does not understand
2295 CPP_KEYWORD, keywords are treated like
2296 identifiers. */
2297 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2298 token->u.value, token->flags);
2299 }
2300 }
2301
2302 /* Issue an error about name-lookup failing. NAME is the
2303 IDENTIFIER_NODE DECL is the result of
2304 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2305 the thing that we hoped to find. */
2306
2307 static void
2308 cp_parser_name_lookup_error (cp_parser* parser,
2309 tree name,
2310 tree decl,
2311 name_lookup_error desired,
2312 location_t location)
2313 {
2314 /* If name lookup completely failed, tell the user that NAME was not
2315 declared. */
2316 if (decl == error_mark_node)
2317 {
2318 if (parser->scope && parser->scope != global_namespace)
2319 error_at (location, "%<%E::%E%> has not been declared",
2320 parser->scope, name);
2321 else if (parser->scope == global_namespace)
2322 error_at (location, "%<::%E%> has not been declared", name);
2323 else if (parser->object_scope
2324 && !CLASS_TYPE_P (parser->object_scope))
2325 error_at (location, "request for member %qE in non-class type %qT",
2326 name, parser->object_scope);
2327 else if (parser->object_scope)
2328 error_at (location, "%<%T::%E%> has not been declared",
2329 parser->object_scope, name);
2330 else
2331 error_at (location, "%qE has not been declared", name);
2332 }
2333 else if (parser->scope && parser->scope != global_namespace)
2334 {
2335 switch (desired)
2336 {
2337 case NLE_TYPE:
2338 error_at (location, "%<%E::%E%> is not a type",
2339 parser->scope, name);
2340 break;
2341 case NLE_CXX98:
2342 error_at (location, "%<%E::%E%> is not a class or namespace",
2343 parser->scope, name);
2344 break;
2345 case NLE_NOT_CXX98:
2346 error_at (location,
2347 "%<%E::%E%> is not a class, namespace, or enumeration",
2348 parser->scope, name);
2349 break;
2350 default:
2351 gcc_unreachable ();
2352
2353 }
2354 }
2355 else if (parser->scope == global_namespace)
2356 {
2357 switch (desired)
2358 {
2359 case NLE_TYPE:
2360 error_at (location, "%<::%E%> is not a type", name);
2361 break;
2362 case NLE_CXX98:
2363 error_at (location, "%<::%E%> is not a class or namespace", name);
2364 break;
2365 case NLE_NOT_CXX98:
2366 error_at (location,
2367 "%<::%E%> is not a class, namespace, or enumeration",
2368 name);
2369 break;
2370 default:
2371 gcc_unreachable ();
2372 }
2373 }
2374 else
2375 {
2376 switch (desired)
2377 {
2378 case NLE_TYPE:
2379 error_at (location, "%qE is not a type", name);
2380 break;
2381 case NLE_CXX98:
2382 error_at (location, "%qE is not a class or namespace", name);
2383 break;
2384 case NLE_NOT_CXX98:
2385 error_at (location,
2386 "%qE is not a class, namespace, or enumeration", name);
2387 break;
2388 default:
2389 gcc_unreachable ();
2390 }
2391 }
2392 }
2393
2394 /* If we are parsing tentatively, remember that an error has occurred
2395 during this tentative parse. Returns true if the error was
2396 simulated; false if a message should be issued by the caller. */
2397
2398 static bool
2399 cp_parser_simulate_error (cp_parser* parser)
2400 {
2401 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2402 {
2403 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2404 return true;
2405 }
2406 return false;
2407 }
2408
2409 /* Check for repeated decl-specifiers. */
2410
2411 static void
2412 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2413 location_t location)
2414 {
2415 int ds;
2416
2417 for (ds = ds_first; ds != ds_last; ++ds)
2418 {
2419 unsigned count = decl_specs->specs[ds];
2420 if (count < 2)
2421 continue;
2422 /* The "long" specifier is a special case because of "long long". */
2423 if (ds == ds_long)
2424 {
2425 if (count > 2)
2426 error_at (location, "%<long long long%> is too long for GCC");
2427 else
2428 pedwarn_cxx98 (location, OPT_Wlong_long,
2429 "ISO C++ 1998 does not support %<long long%>");
2430 }
2431 else if (count > 1)
2432 {
2433 static const char *const decl_spec_names[] = {
2434 "signed",
2435 "unsigned",
2436 "short",
2437 "long",
2438 "const",
2439 "volatile",
2440 "restrict",
2441 "inline",
2442 "virtual",
2443 "explicit",
2444 "friend",
2445 "typedef",
2446 "constexpr",
2447 "__complex",
2448 "__thread"
2449 };
2450 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2451 }
2452 }
2453 }
2454
2455 /* This function is called when a type is defined. If type
2456 definitions are forbidden at this point, an error message is
2457 issued. */
2458
2459 static bool
2460 cp_parser_check_type_definition (cp_parser* parser)
2461 {
2462 /* If types are forbidden here, issue a message. */
2463 if (parser->type_definition_forbidden_message)
2464 {
2465 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2466 in the message need to be interpreted. */
2467 error (parser->type_definition_forbidden_message);
2468 return false;
2469 }
2470 return true;
2471 }
2472
2473 /* This function is called when the DECLARATOR is processed. The TYPE
2474 was a type defined in the decl-specifiers. If it is invalid to
2475 define a type in the decl-specifiers for DECLARATOR, an error is
2476 issued. TYPE_LOCATION is the location of TYPE and is used
2477 for error reporting. */
2478
2479 static void
2480 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2481 tree type, location_t type_location)
2482 {
2483 /* [dcl.fct] forbids type definitions in return types.
2484 Unfortunately, it's not easy to know whether or not we are
2485 processing a return type until after the fact. */
2486 while (declarator
2487 && (declarator->kind == cdk_pointer
2488 || declarator->kind == cdk_reference
2489 || declarator->kind == cdk_ptrmem))
2490 declarator = declarator->declarator;
2491 if (declarator
2492 && declarator->kind == cdk_function)
2493 {
2494 error_at (type_location,
2495 "new types may not be defined in a return type");
2496 inform (type_location,
2497 "(perhaps a semicolon is missing after the definition of %qT)",
2498 type);
2499 }
2500 }
2501
2502 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2503 "<" in any valid C++ program. If the next token is indeed "<",
2504 issue a message warning the user about what appears to be an
2505 invalid attempt to form a template-id. LOCATION is the location
2506 of the type-specifier (TYPE) */
2507
2508 static void
2509 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2510 tree type, location_t location)
2511 {
2512 cp_token_position start = 0;
2513
2514 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2515 {
2516 if (TYPE_P (type))
2517 error_at (location, "%qT is not a template", type);
2518 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2519 error_at (location, "%qE is not a template", type);
2520 else
2521 error_at (location, "invalid template-id");
2522 /* Remember the location of the invalid "<". */
2523 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2524 start = cp_lexer_token_position (parser->lexer, true);
2525 /* Consume the "<". */
2526 cp_lexer_consume_token (parser->lexer);
2527 /* Parse the template arguments. */
2528 cp_parser_enclosed_template_argument_list (parser);
2529 /* Permanently remove the invalid template arguments so that
2530 this error message is not issued again. */
2531 if (start)
2532 cp_lexer_purge_tokens_after (parser->lexer, start);
2533 }
2534 }
2535
2536 /* If parsing an integral constant-expression, issue an error message
2537 about the fact that THING appeared and return true. Otherwise,
2538 return false. In either case, set
2539 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2540
2541 static bool
2542 cp_parser_non_integral_constant_expression (cp_parser *parser,
2543 non_integral_constant thing)
2544 {
2545 parser->non_integral_constant_expression_p = true;
2546 if (parser->integral_constant_expression_p)
2547 {
2548 if (!parser->allow_non_integral_constant_expression_p)
2549 {
2550 const char *msg = NULL;
2551 switch (thing)
2552 {
2553 case NIC_FLOAT:
2554 error ("floating-point literal "
2555 "cannot appear in a constant-expression");
2556 return true;
2557 case NIC_CAST:
2558 error ("a cast to a type other than an integral or "
2559 "enumeration type cannot appear in a "
2560 "constant-expression");
2561 return true;
2562 case NIC_TYPEID:
2563 error ("%<typeid%> operator "
2564 "cannot appear in a constant-expression");
2565 return true;
2566 case NIC_NCC:
2567 error ("non-constant compound literals "
2568 "cannot appear in a constant-expression");
2569 return true;
2570 case NIC_FUNC_CALL:
2571 error ("a function call "
2572 "cannot appear in a constant-expression");
2573 return true;
2574 case NIC_INC:
2575 error ("an increment "
2576 "cannot appear in a constant-expression");
2577 return true;
2578 case NIC_DEC:
2579 error ("an decrement "
2580 "cannot appear in a constant-expression");
2581 return true;
2582 case NIC_ARRAY_REF:
2583 error ("an array reference "
2584 "cannot appear in a constant-expression");
2585 return true;
2586 case NIC_ADDR_LABEL:
2587 error ("the address of a label "
2588 "cannot appear in a constant-expression");
2589 return true;
2590 case NIC_OVERLOADED:
2591 error ("calls to overloaded operators "
2592 "cannot appear in a constant-expression");
2593 return true;
2594 case NIC_ASSIGNMENT:
2595 error ("an assignment cannot appear in a constant-expression");
2596 return true;
2597 case NIC_COMMA:
2598 error ("a comma operator "
2599 "cannot appear in a constant-expression");
2600 return true;
2601 case NIC_CONSTRUCTOR:
2602 error ("a call to a constructor "
2603 "cannot appear in a constant-expression");
2604 return true;
2605 case NIC_THIS:
2606 msg = "this";
2607 break;
2608 case NIC_FUNC_NAME:
2609 msg = "__FUNCTION__";
2610 break;
2611 case NIC_PRETTY_FUNC:
2612 msg = "__PRETTY_FUNCTION__";
2613 break;
2614 case NIC_C99_FUNC:
2615 msg = "__func__";
2616 break;
2617 case NIC_VA_ARG:
2618 msg = "va_arg";
2619 break;
2620 case NIC_ARROW:
2621 msg = "->";
2622 break;
2623 case NIC_POINT:
2624 msg = ".";
2625 break;
2626 case NIC_STAR:
2627 msg = "*";
2628 break;
2629 case NIC_ADDR:
2630 msg = "&";
2631 break;
2632 case NIC_PREINCREMENT:
2633 msg = "++";
2634 break;
2635 case NIC_PREDECREMENT:
2636 msg = "--";
2637 break;
2638 case NIC_NEW:
2639 msg = "new";
2640 break;
2641 case NIC_DEL:
2642 msg = "delete";
2643 break;
2644 default:
2645 gcc_unreachable ();
2646 }
2647 if (msg)
2648 error ("%qs cannot appear in a constant-expression", msg);
2649 return true;
2650 }
2651 }
2652 return false;
2653 }
2654
2655 /* Emit a diagnostic for an invalid type name. SCOPE is the
2656 qualifying scope (or NULL, if none) for ID. This function commits
2657 to the current active tentative parse, if any. (Otherwise, the
2658 problematic construct might be encountered again later, resulting
2659 in duplicate error messages.) LOCATION is the location of ID. */
2660
2661 static void
2662 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2663 tree scope, tree id,
2664 location_t location)
2665 {
2666 tree decl, old_scope;
2667 /* Try to lookup the identifier. */
2668 old_scope = parser->scope;
2669 parser->scope = scope;
2670 decl = cp_parser_lookup_name_simple (parser, id, location);
2671 parser->scope = old_scope;
2672 /* If the lookup found a template-name, it means that the user forgot
2673 to specify an argument list. Emit a useful error message. */
2674 if (TREE_CODE (decl) == TEMPLATE_DECL)
2675 error_at (location,
2676 "invalid use of template-name %qE without an argument list",
2677 decl);
2678 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2679 error_at (location, "invalid use of destructor %qD as a type", id);
2680 else if (TREE_CODE (decl) == TYPE_DECL)
2681 /* Something like 'unsigned A a;' */
2682 error_at (location, "invalid combination of multiple type-specifiers");
2683 else if (!parser->scope)
2684 {
2685 /* Issue an error message. */
2686 error_at (location, "%qE does not name a type", id);
2687 /* If we're in a template class, it's possible that the user was
2688 referring to a type from a base class. For example:
2689
2690 template <typename T> struct A { typedef T X; };
2691 template <typename T> struct B : public A<T> { X x; };
2692
2693 The user should have said "typename A<T>::X". */
2694 if (processing_template_decl && current_class_type
2695 && TYPE_BINFO (current_class_type))
2696 {
2697 tree b;
2698
2699 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2700 b;
2701 b = TREE_CHAIN (b))
2702 {
2703 tree base_type = BINFO_TYPE (b);
2704 if (CLASS_TYPE_P (base_type)
2705 && dependent_type_p (base_type))
2706 {
2707 tree field;
2708 /* Go from a particular instantiation of the
2709 template (which will have an empty TYPE_FIELDs),
2710 to the main version. */
2711 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2712 for (field = TYPE_FIELDS (base_type);
2713 field;
2714 field = DECL_CHAIN (field))
2715 if (TREE_CODE (field) == TYPE_DECL
2716 && DECL_NAME (field) == id)
2717 {
2718 inform (location,
2719 "(perhaps %<typename %T::%E%> was intended)",
2720 BINFO_TYPE (b), id);
2721 break;
2722 }
2723 if (field)
2724 break;
2725 }
2726 }
2727 }
2728 }
2729 /* Here we diagnose qualified-ids where the scope is actually correct,
2730 but the identifier does not resolve to a valid type name. */
2731 else if (parser->scope != error_mark_node)
2732 {
2733 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2734 error_at (location, "%qE in namespace %qE does not name a type",
2735 id, parser->scope);
2736 else if (CLASS_TYPE_P (parser->scope)
2737 && constructor_name_p (id, parser->scope))
2738 {
2739 /* A<T>::A<T>() */
2740 error_at (location, "%<%T::%E%> names the constructor, not"
2741 " the type", parser->scope, id);
2742 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2743 error_at (location, "and %qT has no template constructors",
2744 parser->scope);
2745 }
2746 else if (TYPE_P (parser->scope)
2747 && dependent_scope_p (parser->scope))
2748 error_at (location, "need %<typename%> before %<%T::%E%> because "
2749 "%qT is a dependent scope",
2750 parser->scope, id, parser->scope);
2751 else if (TYPE_P (parser->scope))
2752 error_at (location, "%qE in class %qT does not name a type",
2753 id, parser->scope);
2754 else
2755 gcc_unreachable ();
2756 }
2757 cp_parser_commit_to_tentative_parse (parser);
2758 }
2759
2760 /* Check for a common situation where a type-name should be present,
2761 but is not, and issue a sensible error message. Returns true if an
2762 invalid type-name was detected.
2763
2764 The situation handled by this function are variable declarations of the
2765 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2766 Usually, `ID' should name a type, but if we got here it means that it
2767 does not. We try to emit the best possible error message depending on
2768 how exactly the id-expression looks like. */
2769
2770 static bool
2771 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2772 {
2773 tree id;
2774 cp_token *token = cp_lexer_peek_token (parser->lexer);
2775
2776 /* Avoid duplicate error about ambiguous lookup. */
2777 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2778 {
2779 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2780 if (next->type == CPP_NAME && next->ambiguous_p)
2781 goto out;
2782 }
2783
2784 cp_parser_parse_tentatively (parser);
2785 id = cp_parser_id_expression (parser,
2786 /*template_keyword_p=*/false,
2787 /*check_dependency_p=*/true,
2788 /*template_p=*/NULL,
2789 /*declarator_p=*/true,
2790 /*optional_p=*/false);
2791 /* If the next token is a (, this is a function with no explicit return
2792 type, i.e. constructor, destructor or conversion op. */
2793 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2794 || TREE_CODE (id) == TYPE_DECL)
2795 {
2796 cp_parser_abort_tentative_parse (parser);
2797 return false;
2798 }
2799 if (!cp_parser_parse_definitely (parser))
2800 return false;
2801
2802 /* Emit a diagnostic for the invalid type. */
2803 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2804 id, token->location);
2805 out:
2806 /* If we aren't in the middle of a declarator (i.e. in a
2807 parameter-declaration-clause), skip to the end of the declaration;
2808 there's no point in trying to process it. */
2809 if (!parser->in_declarator_p)
2810 cp_parser_skip_to_end_of_block_or_statement (parser);
2811 return true;
2812 }
2813
2814 /* Consume tokens up to, and including, the next non-nested closing `)'.
2815 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2816 are doing error recovery. Returns -1 if OR_COMMA is true and we
2817 found an unnested comma. */
2818
2819 static int
2820 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2821 bool recovering,
2822 bool or_comma,
2823 bool consume_paren)
2824 {
2825 unsigned paren_depth = 0;
2826 unsigned brace_depth = 0;
2827 unsigned square_depth = 0;
2828
2829 if (recovering && !or_comma
2830 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2831 return 0;
2832
2833 while (true)
2834 {
2835 cp_token * token = cp_lexer_peek_token (parser->lexer);
2836
2837 switch (token->type)
2838 {
2839 case CPP_EOF:
2840 case CPP_PRAGMA_EOL:
2841 /* If we've run out of tokens, then there is no closing `)'. */
2842 return 0;
2843
2844 /* This is good for lambda expression capture-lists. */
2845 case CPP_OPEN_SQUARE:
2846 ++square_depth;
2847 break;
2848 case CPP_CLOSE_SQUARE:
2849 if (!square_depth--)
2850 return 0;
2851 break;
2852
2853 case CPP_SEMICOLON:
2854 /* This matches the processing in skip_to_end_of_statement. */
2855 if (!brace_depth)
2856 return 0;
2857 break;
2858
2859 case CPP_OPEN_BRACE:
2860 ++brace_depth;
2861 break;
2862 case CPP_CLOSE_BRACE:
2863 if (!brace_depth--)
2864 return 0;
2865 break;
2866
2867 case CPP_COMMA:
2868 if (recovering && or_comma && !brace_depth && !paren_depth
2869 && !square_depth)
2870 return -1;
2871 break;
2872
2873 case CPP_OPEN_PAREN:
2874 if (!brace_depth)
2875 ++paren_depth;
2876 break;
2877
2878 case CPP_CLOSE_PAREN:
2879 if (!brace_depth && !paren_depth--)
2880 {
2881 if (consume_paren)
2882 cp_lexer_consume_token (parser->lexer);
2883 return 1;
2884 }
2885 break;
2886
2887 default:
2888 break;
2889 }
2890
2891 /* Consume the token. */
2892 cp_lexer_consume_token (parser->lexer);
2893 }
2894 }
2895
2896 /* Consume tokens until we reach the end of the current statement.
2897 Normally, that will be just before consuming a `;'. However, if a
2898 non-nested `}' comes first, then we stop before consuming that. */
2899
2900 static void
2901 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2902 {
2903 unsigned nesting_depth = 0;
2904
2905 while (true)
2906 {
2907 cp_token *token = cp_lexer_peek_token (parser->lexer);
2908
2909 switch (token->type)
2910 {
2911 case CPP_EOF:
2912 case CPP_PRAGMA_EOL:
2913 /* If we've run out of tokens, stop. */
2914 return;
2915
2916 case CPP_SEMICOLON:
2917 /* If the next token is a `;', we have reached the end of the
2918 statement. */
2919 if (!nesting_depth)
2920 return;
2921 break;
2922
2923 case CPP_CLOSE_BRACE:
2924 /* If this is a non-nested '}', stop before consuming it.
2925 That way, when confronted with something like:
2926
2927 { 3 + }
2928
2929 we stop before consuming the closing '}', even though we
2930 have not yet reached a `;'. */
2931 if (nesting_depth == 0)
2932 return;
2933
2934 /* If it is the closing '}' for a block that we have
2935 scanned, stop -- but only after consuming the token.
2936 That way given:
2937
2938 void f g () { ... }
2939 typedef int I;
2940
2941 we will stop after the body of the erroneously declared
2942 function, but before consuming the following `typedef'
2943 declaration. */
2944 if (--nesting_depth == 0)
2945 {
2946 cp_lexer_consume_token (parser->lexer);
2947 return;
2948 }
2949
2950 case CPP_OPEN_BRACE:
2951 ++nesting_depth;
2952 break;
2953
2954 default:
2955 break;
2956 }
2957
2958 /* Consume the token. */
2959 cp_lexer_consume_token (parser->lexer);
2960 }
2961 }
2962
2963 /* This function is called at the end of a statement or declaration.
2964 If the next token is a semicolon, it is consumed; otherwise, error
2965 recovery is attempted. */
2966
2967 static void
2968 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2969 {
2970 /* Look for the trailing `;'. */
2971 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2972 {
2973 /* If there is additional (erroneous) input, skip to the end of
2974 the statement. */
2975 cp_parser_skip_to_end_of_statement (parser);
2976 /* If the next token is now a `;', consume it. */
2977 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2978 cp_lexer_consume_token (parser->lexer);
2979 }
2980 }
2981
2982 /* Skip tokens until we have consumed an entire block, or until we
2983 have consumed a non-nested `;'. */
2984
2985 static void
2986 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2987 {
2988 int nesting_depth = 0;
2989
2990 while (nesting_depth >= 0)
2991 {
2992 cp_token *token = cp_lexer_peek_token (parser->lexer);
2993
2994 switch (token->type)
2995 {
2996 case CPP_EOF:
2997 case CPP_PRAGMA_EOL:
2998 /* If we've run out of tokens, stop. */
2999 return;
3000
3001 case CPP_SEMICOLON:
3002 /* Stop if this is an unnested ';'. */
3003 if (!nesting_depth)
3004 nesting_depth = -1;
3005 break;
3006
3007 case CPP_CLOSE_BRACE:
3008 /* Stop if this is an unnested '}', or closes the outermost
3009 nesting level. */
3010 nesting_depth--;
3011 if (nesting_depth < 0)
3012 return;
3013 if (!nesting_depth)
3014 nesting_depth = -1;
3015 break;
3016
3017 case CPP_OPEN_BRACE:
3018 /* Nest. */
3019 nesting_depth++;
3020 break;
3021
3022 default:
3023 break;
3024 }
3025
3026 /* Consume the token. */
3027 cp_lexer_consume_token (parser->lexer);
3028 }
3029 }
3030
3031 /* Skip tokens until a non-nested closing curly brace is the next
3032 token, or there are no more tokens. Return true in the first case,
3033 false otherwise. */
3034
3035 static bool
3036 cp_parser_skip_to_closing_brace (cp_parser *parser)
3037 {
3038 unsigned nesting_depth = 0;
3039
3040 while (true)
3041 {
3042 cp_token *token = cp_lexer_peek_token (parser->lexer);
3043
3044 switch (token->type)
3045 {
3046 case CPP_EOF:
3047 case CPP_PRAGMA_EOL:
3048 /* If we've run out of tokens, stop. */
3049 return false;
3050
3051 case CPP_CLOSE_BRACE:
3052 /* If the next token is a non-nested `}', then we have reached
3053 the end of the current block. */
3054 if (nesting_depth-- == 0)
3055 return true;
3056 break;
3057
3058 case CPP_OPEN_BRACE:
3059 /* If it the next token is a `{', then we are entering a new
3060 block. Consume the entire block. */
3061 ++nesting_depth;
3062 break;
3063
3064 default:
3065 break;
3066 }
3067
3068 /* Consume the token. */
3069 cp_lexer_consume_token (parser->lexer);
3070 }
3071 }
3072
3073 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3074 parameter is the PRAGMA token, allowing us to purge the entire pragma
3075 sequence. */
3076
3077 static void
3078 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3079 {
3080 cp_token *token;
3081
3082 parser->lexer->in_pragma = false;
3083
3084 do
3085 token = cp_lexer_consume_token (parser->lexer);
3086 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3087
3088 /* Ensure that the pragma is not parsed again. */
3089 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3090 }
3091
3092 /* Require pragma end of line, resyncing with it as necessary. The
3093 arguments are as for cp_parser_skip_to_pragma_eol. */
3094
3095 static void
3096 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3097 {
3098 parser->lexer->in_pragma = false;
3099 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3100 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3101 }
3102
3103 /* This is a simple wrapper around make_typename_type. When the id is
3104 an unresolved identifier node, we can provide a superior diagnostic
3105 using cp_parser_diagnose_invalid_type_name. */
3106
3107 static tree
3108 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3109 tree id, location_t id_location)
3110 {
3111 tree result;
3112 if (TREE_CODE (id) == IDENTIFIER_NODE)
3113 {
3114 result = make_typename_type (scope, id, typename_type,
3115 /*complain=*/tf_none);
3116 if (result == error_mark_node)
3117 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3118 return result;
3119 }
3120 return make_typename_type (scope, id, typename_type, tf_error);
3121 }
3122
3123 /* This is a wrapper around the
3124 make_{pointer,ptrmem,reference}_declarator functions that decides
3125 which one to call based on the CODE and CLASS_TYPE arguments. The
3126 CODE argument should be one of the values returned by
3127 cp_parser_ptr_operator. */
3128 static cp_declarator *
3129 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3130 cp_cv_quals cv_qualifiers,
3131 cp_declarator *target)
3132 {
3133 if (code == ERROR_MARK)
3134 return cp_error_declarator;
3135
3136 if (code == INDIRECT_REF)
3137 if (class_type == NULL_TREE)
3138 return make_pointer_declarator (cv_qualifiers, target);
3139 else
3140 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3141 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3142 return make_reference_declarator (cv_qualifiers, target, false);
3143 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3144 return make_reference_declarator (cv_qualifiers, target, true);
3145 gcc_unreachable ();
3146 }
3147
3148 /* Create a new C++ parser. */
3149
3150 static cp_parser *
3151 cp_parser_new (void)
3152 {
3153 cp_parser *parser;
3154 cp_lexer *lexer;
3155 unsigned i;
3156
3157 /* cp_lexer_new_main is called before doing GC allocation because
3158 cp_lexer_new_main might load a PCH file. */
3159 lexer = cp_lexer_new_main ();
3160
3161 /* Initialize the binops_by_token so that we can get the tree
3162 directly from the token. */
3163 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3164 binops_by_token[binops[i].token_type] = binops[i];
3165
3166 parser = ggc_alloc_cleared_cp_parser ();
3167 parser->lexer = lexer;
3168 parser->context = cp_parser_context_new (NULL);
3169
3170 /* For now, we always accept GNU extensions. */
3171 parser->allow_gnu_extensions_p = 1;
3172
3173 /* The `>' token is a greater-than operator, not the end of a
3174 template-id. */
3175 parser->greater_than_is_operator_p = true;
3176
3177 parser->default_arg_ok_p = true;
3178
3179 /* We are not parsing a constant-expression. */
3180 parser->integral_constant_expression_p = false;
3181 parser->allow_non_integral_constant_expression_p = false;
3182 parser->non_integral_constant_expression_p = false;
3183
3184 /* Local variable names are not forbidden. */
3185 parser->local_variables_forbidden_p = false;
3186
3187 /* We are not processing an `extern "C"' declaration. */
3188 parser->in_unbraced_linkage_specification_p = false;
3189
3190 /* We are not processing a declarator. */
3191 parser->in_declarator_p = false;
3192
3193 /* We are not processing a template-argument-list. */
3194 parser->in_template_argument_list_p = false;
3195
3196 /* We are not in an iteration statement. */
3197 parser->in_statement = 0;
3198
3199 /* We are not in a switch statement. */
3200 parser->in_switch_statement_p = false;
3201
3202 /* We are not parsing a type-id inside an expression. */
3203 parser->in_type_id_in_expr_p = false;
3204
3205 /* Declarations aren't implicitly extern "C". */
3206 parser->implicit_extern_c = false;
3207
3208 /* String literals should be translated to the execution character set. */
3209 parser->translate_strings_p = true;
3210
3211 /* We are not parsing a function body. */
3212 parser->in_function_body = false;
3213
3214 /* The unparsed function queue is empty. */
3215 push_unparsed_function_queues (parser);
3216
3217 /* There are no classes being defined. */
3218 parser->num_classes_being_defined = 0;
3219
3220 /* No template parameters apply. */
3221 parser->num_template_parameter_lists = 0;
3222
3223 return parser;
3224 }
3225
3226 /* Create a cp_lexer structure which will emit the tokens in CACHE
3227 and push it onto the parser's lexer stack. This is used for delayed
3228 parsing of in-class method bodies and default arguments, and should
3229 not be confused with tentative parsing. */
3230 static void
3231 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3232 {
3233 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3234 lexer->next = parser->lexer;
3235 parser->lexer = lexer;
3236
3237 /* Move the current source position to that of the first token in the
3238 new lexer. */
3239 cp_lexer_set_source_position_from_token (lexer->next_token);
3240 }
3241
3242 /* Pop the top lexer off the parser stack. This is never used for the
3243 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3244 static void
3245 cp_parser_pop_lexer (cp_parser *parser)
3246 {
3247 cp_lexer *lexer = parser->lexer;
3248 parser->lexer = lexer->next;
3249 cp_lexer_destroy (lexer);
3250
3251 /* Put the current source position back where it was before this
3252 lexer was pushed. */
3253 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3254 }
3255
3256 /* Lexical conventions [gram.lex] */
3257
3258 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3259 identifier. */
3260
3261 static tree
3262 cp_parser_identifier (cp_parser* parser)
3263 {
3264 cp_token *token;
3265
3266 /* Look for the identifier. */
3267 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3268 /* Return the value. */
3269 return token ? token->u.value : error_mark_node;
3270 }
3271
3272 /* Parse a sequence of adjacent string constants. Returns a
3273 TREE_STRING representing the combined, nul-terminated string
3274 constant. If TRANSLATE is true, translate the string to the
3275 execution character set. If WIDE_OK is true, a wide string is
3276 invalid here.
3277
3278 C++98 [lex.string] says that if a narrow string literal token is
3279 adjacent to a wide string literal token, the behavior is undefined.
3280 However, C99 6.4.5p4 says that this results in a wide string literal.
3281 We follow C99 here, for consistency with the C front end.
3282
3283 This code is largely lifted from lex_string() in c-lex.c.
3284
3285 FUTURE: ObjC++ will need to handle @-strings here. */
3286 static tree
3287 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3288 {
3289 tree value;
3290 size_t count;
3291 struct obstack str_ob;
3292 cpp_string str, istr, *strs;
3293 cp_token *tok;
3294 enum cpp_ttype type;
3295
3296 tok = cp_lexer_peek_token (parser->lexer);
3297 if (!cp_parser_is_string_literal (tok))
3298 {
3299 cp_parser_error (parser, "expected string-literal");
3300 return error_mark_node;
3301 }
3302
3303 type = tok->type;
3304
3305 /* Try to avoid the overhead of creating and destroying an obstack
3306 for the common case of just one string. */
3307 if (!cp_parser_is_string_literal
3308 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3309 {
3310 cp_lexer_consume_token (parser->lexer);
3311
3312 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3313 str.len = TREE_STRING_LENGTH (tok->u.value);
3314 count = 1;
3315
3316 strs = &str;
3317 }
3318 else
3319 {
3320 gcc_obstack_init (&str_ob);
3321 count = 0;
3322
3323 do
3324 {
3325 cp_lexer_consume_token (parser->lexer);
3326 count++;
3327 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3328 str.len = TREE_STRING_LENGTH (tok->u.value);
3329
3330 if (type != tok->type)
3331 {
3332 if (type == CPP_STRING)
3333 type = tok->type;
3334 else if (tok->type != CPP_STRING)
3335 error_at (tok->location,
3336 "unsupported non-standard concatenation "
3337 "of string literals");
3338 }
3339
3340 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3341
3342 tok = cp_lexer_peek_token (parser->lexer);
3343 }
3344 while (cp_parser_is_string_literal (tok));
3345
3346 strs = (cpp_string *) obstack_finish (&str_ob);
3347 }
3348
3349 if (type != CPP_STRING && !wide_ok)
3350 {
3351 cp_parser_error (parser, "a wide string is invalid in this context");
3352 type = CPP_STRING;
3353 }
3354
3355 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3356 (parse_in, strs, count, &istr, type))
3357 {
3358 value = build_string (istr.len, (const char *)istr.text);
3359 free (CONST_CAST (unsigned char *, istr.text));
3360
3361 switch (type)
3362 {
3363 default:
3364 case CPP_STRING:
3365 case CPP_UTF8STRING:
3366 TREE_TYPE (value) = char_array_type_node;
3367 break;
3368 case CPP_STRING16:
3369 TREE_TYPE (value) = char16_array_type_node;
3370 break;
3371 case CPP_STRING32:
3372 TREE_TYPE (value) = char32_array_type_node;
3373 break;
3374 case CPP_WSTRING:
3375 TREE_TYPE (value) = wchar_array_type_node;
3376 break;
3377 }
3378
3379 value = fix_string_type (value);
3380 }
3381 else
3382 /* cpp_interpret_string has issued an error. */
3383 value = error_mark_node;
3384
3385 if (count > 1)
3386 obstack_free (&str_ob, 0);
3387
3388 return value;
3389 }
3390
3391
3392 /* Basic concepts [gram.basic] */
3393
3394 /* Parse a translation-unit.
3395
3396 translation-unit:
3397 declaration-seq [opt]
3398
3399 Returns TRUE if all went well. */
3400
3401 static bool
3402 cp_parser_translation_unit (cp_parser* parser)
3403 {
3404 /* The address of the first non-permanent object on the declarator
3405 obstack. */
3406 static void *declarator_obstack_base;
3407
3408 bool success;
3409
3410 /* Create the declarator obstack, if necessary. */
3411 if (!cp_error_declarator)
3412 {
3413 gcc_obstack_init (&declarator_obstack);
3414 /* Create the error declarator. */
3415 cp_error_declarator = make_declarator (cdk_error);
3416 /* Create the empty parameter list. */
3417 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3418 /* Remember where the base of the declarator obstack lies. */
3419 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3420 }
3421
3422 cp_parser_declaration_seq_opt (parser);
3423
3424 /* If there are no tokens left then all went well. */
3425 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3426 {
3427 /* Get rid of the token array; we don't need it any more. */
3428 cp_lexer_destroy (parser->lexer);
3429 parser->lexer = NULL;
3430
3431 /* This file might have been a context that's implicitly extern
3432 "C". If so, pop the lang context. (Only relevant for PCH.) */
3433 if (parser->implicit_extern_c)
3434 {
3435 pop_lang_context ();
3436 parser->implicit_extern_c = false;
3437 }
3438
3439 /* Finish up. */
3440 finish_translation_unit ();
3441
3442 success = true;
3443 }
3444 else
3445 {
3446 cp_parser_error (parser, "expected declaration");
3447 success = false;
3448 }
3449
3450 /* Make sure the declarator obstack was fully cleaned up. */
3451 gcc_assert (obstack_next_free (&declarator_obstack)
3452 == declarator_obstack_base);
3453
3454 /* All went well. */
3455 return success;
3456 }
3457
3458 /* Expressions [gram.expr] */
3459
3460 /* Parse a primary-expression.
3461
3462 primary-expression:
3463 literal
3464 this
3465 ( expression )
3466 id-expression
3467
3468 GNU Extensions:
3469
3470 primary-expression:
3471 ( compound-statement )
3472 __builtin_va_arg ( assignment-expression , type-id )
3473 __builtin_offsetof ( type-id , offsetof-expression )
3474
3475 C++ Extensions:
3476 __has_nothrow_assign ( type-id )
3477 __has_nothrow_constructor ( type-id )
3478 __has_nothrow_copy ( type-id )
3479 __has_trivial_assign ( type-id )
3480 __has_trivial_constructor ( type-id )
3481 __has_trivial_copy ( type-id )
3482 __has_trivial_destructor ( type-id )
3483 __has_virtual_destructor ( type-id )
3484 __is_abstract ( type-id )
3485 __is_base_of ( type-id , type-id )
3486 __is_class ( type-id )
3487 __is_convertible_to ( type-id , type-id )
3488 __is_empty ( type-id )
3489 __is_enum ( type-id )
3490 __is_pod ( type-id )
3491 __is_polymorphic ( type-id )
3492 __is_union ( type-id )
3493
3494 Objective-C++ Extension:
3495
3496 primary-expression:
3497 objc-expression
3498
3499 literal:
3500 __null
3501
3502 ADDRESS_P is true iff this expression was immediately preceded by
3503 "&" and therefore might denote a pointer-to-member. CAST_P is true
3504 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3505 true iff this expression is a template argument.
3506
3507 Returns a representation of the expression. Upon return, *IDK
3508 indicates what kind of id-expression (if any) was present. */
3509
3510 static tree
3511 cp_parser_primary_expression (cp_parser *parser,
3512 bool address_p,
3513 bool cast_p,
3514 bool template_arg_p,
3515 cp_id_kind *idk)
3516 {
3517 cp_token *token = NULL;
3518
3519 /* Assume the primary expression is not an id-expression. */
3520 *idk = CP_ID_KIND_NONE;
3521
3522 /* Peek at the next token. */
3523 token = cp_lexer_peek_token (parser->lexer);
3524 switch (token->type)
3525 {
3526 /* literal:
3527 integer-literal
3528 character-literal
3529 floating-literal
3530 string-literal
3531 boolean-literal */
3532 case CPP_CHAR:
3533 case CPP_CHAR16:
3534 case CPP_CHAR32:
3535 case CPP_WCHAR:
3536 case CPP_NUMBER:
3537 token = cp_lexer_consume_token (parser->lexer);
3538 if (TREE_CODE (token->u.value) == FIXED_CST)
3539 {
3540 error_at (token->location,
3541 "fixed-point types not supported in C++");
3542 return error_mark_node;
3543 }
3544 /* Floating-point literals are only allowed in an integral
3545 constant expression if they are cast to an integral or
3546 enumeration type. */
3547 if (TREE_CODE (token->u.value) == REAL_CST
3548 && parser->integral_constant_expression_p
3549 && pedantic)
3550 {
3551 /* CAST_P will be set even in invalid code like "int(2.7 +
3552 ...)". Therefore, we have to check that the next token
3553 is sure to end the cast. */
3554 if (cast_p)
3555 {
3556 cp_token *next_token;
3557
3558 next_token = cp_lexer_peek_token (parser->lexer);
3559 if (/* The comma at the end of an
3560 enumerator-definition. */
3561 next_token->type != CPP_COMMA
3562 /* The curly brace at the end of an enum-specifier. */
3563 && next_token->type != CPP_CLOSE_BRACE
3564 /* The end of a statement. */
3565 && next_token->type != CPP_SEMICOLON
3566 /* The end of the cast-expression. */
3567 && next_token->type != CPP_CLOSE_PAREN
3568 /* The end of an array bound. */
3569 && next_token->type != CPP_CLOSE_SQUARE
3570 /* The closing ">" in a template-argument-list. */
3571 && (next_token->type != CPP_GREATER
3572 || parser->greater_than_is_operator_p)
3573 /* C++0x only: A ">>" treated like two ">" tokens,
3574 in a template-argument-list. */
3575 && (next_token->type != CPP_RSHIFT
3576 || (cxx_dialect == cxx98)
3577 || parser->greater_than_is_operator_p))
3578 cast_p = false;
3579 }
3580
3581 /* If we are within a cast, then the constraint that the
3582 cast is to an integral or enumeration type will be
3583 checked at that point. If we are not within a cast, then
3584 this code is invalid. */
3585 if (!cast_p)
3586 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3587 }
3588 return token->u.value;
3589
3590 case CPP_STRING:
3591 case CPP_STRING16:
3592 case CPP_STRING32:
3593 case CPP_WSTRING:
3594 case CPP_UTF8STRING:
3595 /* ??? Should wide strings be allowed when parser->translate_strings_p
3596 is false (i.e. in attributes)? If not, we can kill the third
3597 argument to cp_parser_string_literal. */
3598 return cp_parser_string_literal (parser,
3599 parser->translate_strings_p,
3600 true);
3601
3602 case CPP_OPEN_PAREN:
3603 {
3604 tree expr;
3605 bool saved_greater_than_is_operator_p;
3606
3607 /* Consume the `('. */
3608 cp_lexer_consume_token (parser->lexer);
3609 /* Within a parenthesized expression, a `>' token is always
3610 the greater-than operator. */
3611 saved_greater_than_is_operator_p
3612 = parser->greater_than_is_operator_p;
3613 parser->greater_than_is_operator_p = true;
3614 /* If we see `( { ' then we are looking at the beginning of
3615 a GNU statement-expression. */
3616 if (cp_parser_allow_gnu_extensions_p (parser)
3617 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3618 {
3619 /* Statement-expressions are not allowed by the standard. */
3620 pedwarn (token->location, OPT_pedantic,
3621 "ISO C++ forbids braced-groups within expressions");
3622
3623 /* And they're not allowed outside of a function-body; you
3624 cannot, for example, write:
3625
3626 int i = ({ int j = 3; j + 1; });
3627
3628 at class or namespace scope. */
3629 if (!parser->in_function_body
3630 || parser->in_template_argument_list_p)
3631 {
3632 error_at (token->location,
3633 "statement-expressions are not allowed outside "
3634 "functions nor in template-argument lists");
3635 cp_parser_skip_to_end_of_block_or_statement (parser);
3636 expr = error_mark_node;
3637 }
3638 else
3639 {
3640 /* Start the statement-expression. */
3641 expr = begin_stmt_expr ();
3642 /* Parse the compound-statement. */
3643 cp_parser_compound_statement (parser, expr, false);
3644 /* Finish up. */
3645 expr = finish_stmt_expr (expr, false);
3646 }
3647 }
3648 else
3649 {
3650 /* Parse the parenthesized expression. */
3651 expr = cp_parser_expression (parser, cast_p, idk);
3652 /* Let the front end know that this expression was
3653 enclosed in parentheses. This matters in case, for
3654 example, the expression is of the form `A::B', since
3655 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3656 not. */
3657 finish_parenthesized_expr (expr);
3658 }
3659 /* The `>' token might be the end of a template-id or
3660 template-parameter-list now. */
3661 parser->greater_than_is_operator_p
3662 = saved_greater_than_is_operator_p;
3663 /* Consume the `)'. */
3664 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3665 cp_parser_skip_to_end_of_statement (parser);
3666
3667 return expr;
3668 }
3669
3670 case CPP_OPEN_SQUARE:
3671 if (c_dialect_objc ())
3672 /* We have an Objective-C++ message. */
3673 return cp_parser_objc_expression (parser);
3674 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3675 return cp_parser_lambda_expression (parser);
3676
3677 case CPP_OBJC_STRING:
3678 if (c_dialect_objc ())
3679 /* We have an Objective-C++ string literal. */
3680 return cp_parser_objc_expression (parser);
3681 cp_parser_error (parser, "expected primary-expression");
3682 return error_mark_node;
3683
3684 case CPP_KEYWORD:
3685 switch (token->keyword)
3686 {
3687 /* These two are the boolean literals. */
3688 case RID_TRUE:
3689 cp_lexer_consume_token (parser->lexer);
3690 return boolean_true_node;
3691 case RID_FALSE:
3692 cp_lexer_consume_token (parser->lexer);
3693 return boolean_false_node;
3694
3695 /* The `__null' literal. */
3696 case RID_NULL:
3697 cp_lexer_consume_token (parser->lexer);
3698 return null_node;
3699
3700 /* The `nullptr' literal. */
3701 case RID_NULLPTR:
3702 cp_lexer_consume_token (parser->lexer);
3703 return nullptr_node;
3704
3705 /* Recognize the `this' keyword. */
3706 case RID_THIS:
3707 cp_lexer_consume_token (parser->lexer);
3708 if (parser->local_variables_forbidden_p)
3709 {
3710 error_at (token->location,
3711 "%<this%> may not be used in this context");
3712 return error_mark_node;
3713 }
3714 /* Pointers cannot appear in constant-expressions. */
3715 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3716 return error_mark_node;
3717 return finish_this_expr ();
3718
3719 /* The `operator' keyword can be the beginning of an
3720 id-expression. */
3721 case RID_OPERATOR:
3722 goto id_expression;
3723
3724 case RID_FUNCTION_NAME:
3725 case RID_PRETTY_FUNCTION_NAME:
3726 case RID_C99_FUNCTION_NAME:
3727 {
3728 non_integral_constant name;
3729
3730 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3731 __func__ are the names of variables -- but they are
3732 treated specially. Therefore, they are handled here,
3733 rather than relying on the generic id-expression logic
3734 below. Grammatically, these names are id-expressions.
3735
3736 Consume the token. */
3737 token = cp_lexer_consume_token (parser->lexer);
3738
3739 switch (token->keyword)
3740 {
3741 case RID_FUNCTION_NAME:
3742 name = NIC_FUNC_NAME;
3743 break;
3744 case RID_PRETTY_FUNCTION_NAME:
3745 name = NIC_PRETTY_FUNC;
3746 break;
3747 case RID_C99_FUNCTION_NAME:
3748 name = NIC_C99_FUNC;
3749 break;
3750 default:
3751 gcc_unreachable ();
3752 }
3753
3754 if (cp_parser_non_integral_constant_expression (parser, name))
3755 return error_mark_node;
3756
3757 /* Look up the name. */
3758 return finish_fname (token->u.value);
3759 }
3760
3761 case RID_VA_ARG:
3762 {
3763 tree expression;
3764 tree type;
3765
3766 /* The `__builtin_va_arg' construct is used to handle
3767 `va_arg'. Consume the `__builtin_va_arg' token. */
3768 cp_lexer_consume_token (parser->lexer);
3769 /* Look for the opening `('. */
3770 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3771 /* Now, parse the assignment-expression. */
3772 expression = cp_parser_assignment_expression (parser,
3773 /*cast_p=*/false, NULL);
3774 /* Look for the `,'. */
3775 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3776 /* Parse the type-id. */
3777 type = cp_parser_type_id (parser);
3778 /* Look for the closing `)'. */
3779 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3780 /* Using `va_arg' in a constant-expression is not
3781 allowed. */
3782 if (cp_parser_non_integral_constant_expression (parser,
3783 NIC_VA_ARG))
3784 return error_mark_node;
3785 return build_x_va_arg (expression, type);
3786 }
3787
3788 case RID_OFFSETOF:
3789 return cp_parser_builtin_offsetof (parser);
3790
3791 case RID_HAS_NOTHROW_ASSIGN:
3792 case RID_HAS_NOTHROW_CONSTRUCTOR:
3793 case RID_HAS_NOTHROW_COPY:
3794 case RID_HAS_TRIVIAL_ASSIGN:
3795 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3796 case RID_HAS_TRIVIAL_COPY:
3797 case RID_HAS_TRIVIAL_DESTRUCTOR:
3798 case RID_HAS_VIRTUAL_DESTRUCTOR:
3799 case RID_IS_ABSTRACT:
3800 case RID_IS_BASE_OF:
3801 case RID_IS_CLASS:
3802 case RID_IS_CONVERTIBLE_TO:
3803 case RID_IS_EMPTY:
3804 case RID_IS_ENUM:
3805 case RID_IS_POD:
3806 case RID_IS_POLYMORPHIC:
3807 case RID_IS_STD_LAYOUT:
3808 case RID_IS_TRIVIAL:
3809 case RID_IS_UNION:
3810 return cp_parser_trait_expr (parser, token->keyword);
3811
3812 /* Objective-C++ expressions. */
3813 case RID_AT_ENCODE:
3814 case RID_AT_PROTOCOL:
3815 case RID_AT_SELECTOR:
3816 return cp_parser_objc_expression (parser);
3817
3818 case RID_TEMPLATE:
3819 if (parser->in_function_body
3820 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3821 == CPP_LESS))
3822 {
3823 error_at (token->location,
3824 "a template declaration cannot appear at block scope");
3825 cp_parser_skip_to_end_of_block_or_statement (parser);
3826 return error_mark_node;
3827 }
3828 default:
3829 cp_parser_error (parser, "expected primary-expression");
3830 return error_mark_node;
3831 }
3832
3833 /* An id-expression can start with either an identifier, a
3834 `::' as the beginning of a qualified-id, or the "operator"
3835 keyword. */
3836 case CPP_NAME:
3837 case CPP_SCOPE:
3838 case CPP_TEMPLATE_ID:
3839 case CPP_NESTED_NAME_SPECIFIER:
3840 {
3841 tree id_expression;
3842 tree decl;
3843 const char *error_msg;
3844 bool template_p;
3845 bool done;
3846 cp_token *id_expr_token;
3847
3848 id_expression:
3849 /* Parse the id-expression. */
3850 id_expression
3851 = cp_parser_id_expression (parser,
3852 /*template_keyword_p=*/false,
3853 /*check_dependency_p=*/true,
3854 &template_p,
3855 /*declarator_p=*/false,
3856 /*optional_p=*/false);
3857 if (id_expression == error_mark_node)
3858 return error_mark_node;
3859 id_expr_token = token;
3860 token = cp_lexer_peek_token (parser->lexer);
3861 done = (token->type != CPP_OPEN_SQUARE
3862 && token->type != CPP_OPEN_PAREN
3863 && token->type != CPP_DOT
3864 && token->type != CPP_DEREF
3865 && token->type != CPP_PLUS_PLUS
3866 && token->type != CPP_MINUS_MINUS);
3867 /* If we have a template-id, then no further lookup is
3868 required. If the template-id was for a template-class, we
3869 will sometimes have a TYPE_DECL at this point. */
3870 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3871 || TREE_CODE (id_expression) == TYPE_DECL)
3872 decl = id_expression;
3873 /* Look up the name. */
3874 else
3875 {
3876 tree ambiguous_decls;
3877
3878 /* If we already know that this lookup is ambiguous, then
3879 we've already issued an error message; there's no reason
3880 to check again. */
3881 if (id_expr_token->type == CPP_NAME
3882 && id_expr_token->ambiguous_p)
3883 {
3884 cp_parser_simulate_error (parser);
3885 return error_mark_node;
3886 }
3887
3888 decl = cp_parser_lookup_name (parser, id_expression,
3889 none_type,
3890 template_p,
3891 /*is_namespace=*/false,
3892 /*check_dependency=*/true,
3893 &ambiguous_decls,
3894 id_expr_token->location);
3895 /* If the lookup was ambiguous, an error will already have
3896 been issued. */
3897 if (ambiguous_decls)
3898 return error_mark_node;
3899
3900 /* In Objective-C++, an instance variable (ivar) may be preferred
3901 to whatever cp_parser_lookup_name() found. */
3902 decl = objc_lookup_ivar (decl, id_expression);
3903
3904 /* If name lookup gives us a SCOPE_REF, then the
3905 qualifying scope was dependent. */
3906 if (TREE_CODE (decl) == SCOPE_REF)
3907 {
3908 /* At this point, we do not know if DECL is a valid
3909 integral constant expression. We assume that it is
3910 in fact such an expression, so that code like:
3911
3912 template <int N> struct A {
3913 int a[B<N>::i];
3914 };
3915
3916 is accepted. At template-instantiation time, we
3917 will check that B<N>::i is actually a constant. */
3918 return decl;
3919 }
3920 /* Check to see if DECL is a local variable in a context
3921 where that is forbidden. */
3922 if (parser->local_variables_forbidden_p
3923 && local_variable_p (decl))
3924 {
3925 /* It might be that we only found DECL because we are
3926 trying to be generous with pre-ISO scoping rules.
3927 For example, consider:
3928
3929 int i;
3930 void g() {
3931 for (int i = 0; i < 10; ++i) {}
3932 extern void f(int j = i);
3933 }
3934
3935 Here, name look up will originally find the out
3936 of scope `i'. We need to issue a warning message,
3937 but then use the global `i'. */
3938 decl = check_for_out_of_scope_variable (decl);
3939 if (local_variable_p (decl))
3940 {
3941 error_at (id_expr_token->location,
3942 "local variable %qD may not appear in this context",
3943 decl);
3944 return error_mark_node;
3945 }
3946 }
3947 }
3948
3949 decl = (finish_id_expression
3950 (id_expression, decl, parser->scope,
3951 idk,
3952 parser->integral_constant_expression_p,
3953 parser->allow_non_integral_constant_expression_p,
3954 &parser->non_integral_constant_expression_p,
3955 template_p, done, address_p,
3956 template_arg_p,
3957 &error_msg,
3958 id_expr_token->location));
3959 if (error_msg)
3960 cp_parser_error (parser, error_msg);
3961 return decl;
3962 }
3963
3964 /* Anything else is an error. */
3965 default:
3966 cp_parser_error (parser, "expected primary-expression");
3967 return error_mark_node;
3968 }
3969 }
3970
3971 /* Parse an id-expression.
3972
3973 id-expression:
3974 unqualified-id
3975 qualified-id
3976
3977 qualified-id:
3978 :: [opt] nested-name-specifier template [opt] unqualified-id
3979 :: identifier
3980 :: operator-function-id
3981 :: template-id
3982
3983 Return a representation of the unqualified portion of the
3984 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3985 a `::' or nested-name-specifier.
3986
3987 Often, if the id-expression was a qualified-id, the caller will
3988 want to make a SCOPE_REF to represent the qualified-id. This
3989 function does not do this in order to avoid wastefully creating
3990 SCOPE_REFs when they are not required.
3991
3992 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3993 `template' keyword.
3994
3995 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3996 uninstantiated templates.
3997
3998 If *TEMPLATE_P is non-NULL, it is set to true iff the
3999 `template' keyword is used to explicitly indicate that the entity
4000 named is a template.
4001
4002 If DECLARATOR_P is true, the id-expression is appearing as part of
4003 a declarator, rather than as part of an expression. */
4004
4005 static tree
4006 cp_parser_id_expression (cp_parser *parser,
4007 bool template_keyword_p,
4008 bool check_dependency_p,
4009 bool *template_p,
4010 bool declarator_p,
4011 bool optional_p)
4012 {
4013 bool global_scope_p;
4014 bool nested_name_specifier_p;
4015
4016 /* Assume the `template' keyword was not used. */
4017 if (template_p)
4018 *template_p = template_keyword_p;
4019
4020 /* Look for the optional `::' operator. */
4021 global_scope_p
4022 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4023 != NULL_TREE);
4024 /* Look for the optional nested-name-specifier. */
4025 nested_name_specifier_p
4026 = (cp_parser_nested_name_specifier_opt (parser,
4027 /*typename_keyword_p=*/false,
4028 check_dependency_p,
4029 /*type_p=*/false,
4030 declarator_p)
4031 != NULL_TREE);
4032 /* If there is a nested-name-specifier, then we are looking at
4033 the first qualified-id production. */
4034 if (nested_name_specifier_p)
4035 {
4036 tree saved_scope;
4037 tree saved_object_scope;
4038 tree saved_qualifying_scope;
4039 tree unqualified_id;
4040 bool is_template;
4041
4042 /* See if the next token is the `template' keyword. */
4043 if (!template_p)
4044 template_p = &is_template;
4045 *template_p = cp_parser_optional_template_keyword (parser);
4046 /* Name lookup we do during the processing of the
4047 unqualified-id might obliterate SCOPE. */
4048 saved_scope = parser->scope;
4049 saved_object_scope = parser->object_scope;
4050 saved_qualifying_scope = parser->qualifying_scope;
4051 /* Process the final unqualified-id. */
4052 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4053 check_dependency_p,
4054 declarator_p,
4055 /*optional_p=*/false);
4056 /* Restore the SAVED_SCOPE for our caller. */
4057 parser->scope = saved_scope;
4058 parser->object_scope = saved_object_scope;
4059 parser->qualifying_scope = saved_qualifying_scope;
4060
4061 return unqualified_id;
4062 }
4063 /* Otherwise, if we are in global scope, then we are looking at one
4064 of the other qualified-id productions. */
4065 else if (global_scope_p)
4066 {
4067 cp_token *token;
4068 tree id;
4069
4070 /* Peek at the next token. */
4071 token = cp_lexer_peek_token (parser->lexer);
4072
4073 /* If it's an identifier, and the next token is not a "<", then
4074 we can avoid the template-id case. This is an optimization
4075 for this common case. */
4076 if (token->type == CPP_NAME
4077 && !cp_parser_nth_token_starts_template_argument_list_p
4078 (parser, 2))
4079 return cp_parser_identifier (parser);
4080
4081 cp_parser_parse_tentatively (parser);
4082 /* Try a template-id. */
4083 id = cp_parser_template_id (parser,
4084 /*template_keyword_p=*/false,
4085 /*check_dependency_p=*/true,
4086 declarator_p);
4087 /* If that worked, we're done. */
4088 if (cp_parser_parse_definitely (parser))
4089 return id;
4090
4091 /* Peek at the next token. (Changes in the token buffer may
4092 have invalidated the pointer obtained above.) */
4093 token = cp_lexer_peek_token (parser->lexer);
4094
4095 switch (token->type)
4096 {
4097 case CPP_NAME:
4098 return cp_parser_identifier (parser);
4099
4100 case CPP_KEYWORD:
4101 if (token->keyword == RID_OPERATOR)
4102 return cp_parser_operator_function_id (parser);
4103 /* Fall through. */
4104
4105 default:
4106 cp_parser_error (parser, "expected id-expression");
4107 return error_mark_node;
4108 }
4109 }
4110 else
4111 return cp_parser_unqualified_id (parser, template_keyword_p,
4112 /*check_dependency_p=*/true,
4113 declarator_p,
4114 optional_p);
4115 }
4116
4117 /* Parse an unqualified-id.
4118
4119 unqualified-id:
4120 identifier
4121 operator-function-id
4122 conversion-function-id
4123 ~ class-name
4124 template-id
4125
4126 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4127 keyword, in a construct like `A::template ...'.
4128
4129 Returns a representation of unqualified-id. For the `identifier'
4130 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4131 production a BIT_NOT_EXPR is returned; the operand of the
4132 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4133 other productions, see the documentation accompanying the
4134 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4135 names are looked up in uninstantiated templates. If DECLARATOR_P
4136 is true, the unqualified-id is appearing as part of a declarator,
4137 rather than as part of an expression. */
4138
4139 static tree
4140 cp_parser_unqualified_id (cp_parser* parser,
4141 bool template_keyword_p,
4142 bool check_dependency_p,
4143 bool declarator_p,
4144 bool optional_p)
4145 {
4146 cp_token *token;
4147
4148 /* Peek at the next token. */
4149 token = cp_lexer_peek_token (parser->lexer);
4150
4151 switch (token->type)
4152 {
4153 case CPP_NAME:
4154 {
4155 tree id;
4156
4157 /* We don't know yet whether or not this will be a
4158 template-id. */
4159 cp_parser_parse_tentatively (parser);
4160 /* Try a template-id. */
4161 id = cp_parser_template_id (parser, template_keyword_p,
4162 check_dependency_p,
4163 declarator_p);
4164 /* If it worked, we're done. */
4165 if (cp_parser_parse_definitely (parser))
4166 return id;
4167 /* Otherwise, it's an ordinary identifier. */
4168 return cp_parser_identifier (parser);
4169 }
4170
4171 case CPP_TEMPLATE_ID:
4172 return cp_parser_template_id (parser, template_keyword_p,
4173 check_dependency_p,
4174 declarator_p);
4175
4176 case CPP_COMPL:
4177 {
4178 tree type_decl;
4179 tree qualifying_scope;
4180 tree object_scope;
4181 tree scope;
4182 bool done;
4183
4184 /* Consume the `~' token. */
4185 cp_lexer_consume_token (parser->lexer);
4186 /* Parse the class-name. The standard, as written, seems to
4187 say that:
4188
4189 template <typename T> struct S { ~S (); };
4190 template <typename T> S<T>::~S() {}
4191
4192 is invalid, since `~' must be followed by a class-name, but
4193 `S<T>' is dependent, and so not known to be a class.
4194 That's not right; we need to look in uninstantiated
4195 templates. A further complication arises from:
4196
4197 template <typename T> void f(T t) {
4198 t.T::~T();
4199 }
4200
4201 Here, it is not possible to look up `T' in the scope of `T'
4202 itself. We must look in both the current scope, and the
4203 scope of the containing complete expression.
4204
4205 Yet another issue is:
4206
4207 struct S {
4208 int S;
4209 ~S();
4210 };
4211
4212 S::~S() {}
4213
4214 The standard does not seem to say that the `S' in `~S'
4215 should refer to the type `S' and not the data member
4216 `S::S'. */
4217
4218 /* DR 244 says that we look up the name after the "~" in the
4219 same scope as we looked up the qualifying name. That idea
4220 isn't fully worked out; it's more complicated than that. */
4221 scope = parser->scope;
4222 object_scope = parser->object_scope;
4223 qualifying_scope = parser->qualifying_scope;
4224
4225 /* Check for invalid scopes. */
4226 if (scope == error_mark_node)
4227 {
4228 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4229 cp_lexer_consume_token (parser->lexer);
4230 return error_mark_node;
4231 }
4232 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4233 {
4234 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4235 error_at (token->location,
4236 "scope %qT before %<~%> is not a class-name",
4237 scope);
4238 cp_parser_simulate_error (parser);
4239 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4240 cp_lexer_consume_token (parser->lexer);
4241 return error_mark_node;
4242 }
4243 gcc_assert (!scope || TYPE_P (scope));
4244
4245 /* If the name is of the form "X::~X" it's OK even if X is a
4246 typedef. */
4247 token = cp_lexer_peek_token (parser->lexer);
4248 if (scope
4249 && token->type == CPP_NAME
4250 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4251 != CPP_LESS)
4252 && (token->u.value == TYPE_IDENTIFIER (scope)
4253 || constructor_name_p (token->u.value, scope)))
4254 {
4255 cp_lexer_consume_token (parser->lexer);
4256 return build_nt (BIT_NOT_EXPR, scope);
4257 }
4258
4259 /* If there was an explicit qualification (S::~T), first look
4260 in the scope given by the qualification (i.e., S).
4261
4262 Note: in the calls to cp_parser_class_name below we pass
4263 typename_type so that lookup finds the injected-class-name
4264 rather than the constructor. */
4265 done = false;
4266 type_decl = NULL_TREE;
4267 if (scope)
4268 {
4269 cp_parser_parse_tentatively (parser);
4270 type_decl = cp_parser_class_name (parser,
4271 /*typename_keyword_p=*/false,
4272 /*template_keyword_p=*/false,
4273 typename_type,
4274 /*check_dependency=*/false,
4275 /*class_head_p=*/false,
4276 declarator_p);
4277 if (cp_parser_parse_definitely (parser))
4278 done = true;
4279 }
4280 /* In "N::S::~S", look in "N" as well. */
4281 if (!done && scope && qualifying_scope)
4282 {
4283 cp_parser_parse_tentatively (parser);
4284 parser->scope = qualifying_scope;
4285 parser->object_scope = NULL_TREE;
4286 parser->qualifying_scope = NULL_TREE;
4287 type_decl
4288 = cp_parser_class_name (parser,
4289 /*typename_keyword_p=*/false,
4290 /*template_keyword_p=*/false,
4291 typename_type,
4292 /*check_dependency=*/false,
4293 /*class_head_p=*/false,
4294 declarator_p);
4295 if (cp_parser_parse_definitely (parser))
4296 done = true;
4297 }
4298 /* In "p->S::~T", look in the scope given by "*p" as well. */
4299 else if (!done && object_scope)
4300 {
4301 cp_parser_parse_tentatively (parser);
4302 parser->scope = object_scope;
4303 parser->object_scope = NULL_TREE;
4304 parser->qualifying_scope = NULL_TREE;
4305 type_decl
4306 = cp_parser_class_name (parser,
4307 /*typename_keyword_p=*/false,
4308 /*template_keyword_p=*/false,
4309 typename_type,
4310 /*check_dependency=*/false,
4311 /*class_head_p=*/false,
4312 declarator_p);
4313 if (cp_parser_parse_definitely (parser))
4314 done = true;
4315 }
4316 /* Look in the surrounding context. */
4317 if (!done)
4318 {
4319 parser->scope = NULL_TREE;
4320 parser->object_scope = NULL_TREE;
4321 parser->qualifying_scope = NULL_TREE;
4322 if (processing_template_decl)
4323 cp_parser_parse_tentatively (parser);
4324 type_decl
4325 = cp_parser_class_name (parser,
4326 /*typename_keyword_p=*/false,
4327 /*template_keyword_p=*/false,
4328 typename_type,
4329 /*check_dependency=*/false,
4330 /*class_head_p=*/false,
4331 declarator_p);
4332 if (processing_template_decl
4333 && ! cp_parser_parse_definitely (parser))
4334 {
4335 /* We couldn't find a type with this name, so just accept
4336 it and check for a match at instantiation time. */
4337 type_decl = cp_parser_identifier (parser);
4338 if (type_decl != error_mark_node)
4339 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4340 return type_decl;
4341 }
4342 }
4343 /* If an error occurred, assume that the name of the
4344 destructor is the same as the name of the qualifying
4345 class. That allows us to keep parsing after running
4346 into ill-formed destructor names. */
4347 if (type_decl == error_mark_node && scope)
4348 return build_nt (BIT_NOT_EXPR, scope);
4349 else if (type_decl == error_mark_node)
4350 return error_mark_node;
4351
4352 /* Check that destructor name and scope match. */
4353 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4354 {
4355 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4356 error_at (token->location,
4357 "declaration of %<~%T%> as member of %qT",
4358 type_decl, scope);
4359 cp_parser_simulate_error (parser);
4360 return error_mark_node;
4361 }
4362
4363 /* [class.dtor]
4364
4365 A typedef-name that names a class shall not be used as the
4366 identifier in the declarator for a destructor declaration. */
4367 if (declarator_p
4368 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4369 && !DECL_SELF_REFERENCE_P (type_decl)
4370 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4371 error_at (token->location,
4372 "typedef-name %qD used as destructor declarator",
4373 type_decl);
4374
4375 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4376 }
4377
4378 case CPP_KEYWORD:
4379 if (token->keyword == RID_OPERATOR)
4380 {
4381 tree id;
4382
4383 /* This could be a template-id, so we try that first. */
4384 cp_parser_parse_tentatively (parser);
4385 /* Try a template-id. */
4386 id = cp_parser_template_id (parser, template_keyword_p,
4387 /*check_dependency_p=*/true,
4388 declarator_p);
4389 /* If that worked, we're done. */
4390 if (cp_parser_parse_definitely (parser))
4391 return id;
4392 /* We still don't know whether we're looking at an
4393 operator-function-id or a conversion-function-id. */
4394 cp_parser_parse_tentatively (parser);
4395 /* Try an operator-function-id. */
4396 id = cp_parser_operator_function_id (parser);
4397 /* If that didn't work, try a conversion-function-id. */
4398 if (!cp_parser_parse_definitely (parser))
4399 id = cp_parser_conversion_function_id (parser);
4400
4401 return id;
4402 }
4403 /* Fall through. */
4404
4405 default:
4406 if (optional_p)
4407 return NULL_TREE;
4408 cp_parser_error (parser, "expected unqualified-id");
4409 return error_mark_node;
4410 }
4411 }
4412
4413 /* Parse an (optional) nested-name-specifier.
4414
4415 nested-name-specifier: [C++98]
4416 class-or-namespace-name :: nested-name-specifier [opt]
4417 class-or-namespace-name :: template nested-name-specifier [opt]
4418
4419 nested-name-specifier: [C++0x]
4420 type-name ::
4421 namespace-name ::
4422 nested-name-specifier identifier ::
4423 nested-name-specifier template [opt] simple-template-id ::
4424
4425 PARSER->SCOPE should be set appropriately before this function is
4426 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4427 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4428 in name lookups.
4429
4430 Sets PARSER->SCOPE to the class (TYPE) or namespace
4431 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4432 it unchanged if there is no nested-name-specifier. Returns the new
4433 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4434
4435 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4436 part of a declaration and/or decl-specifier. */
4437
4438 static tree
4439 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4440 bool typename_keyword_p,
4441 bool check_dependency_p,
4442 bool type_p,
4443 bool is_declaration)
4444 {
4445 bool success = false;
4446 cp_token_position start = 0;
4447 cp_token *token;
4448
4449 /* Remember where the nested-name-specifier starts. */
4450 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4451 {
4452 start = cp_lexer_token_position (parser->lexer, false);
4453 push_deferring_access_checks (dk_deferred);
4454 }
4455
4456 while (true)
4457 {
4458 tree new_scope;
4459 tree old_scope;
4460 tree saved_qualifying_scope;
4461 bool template_keyword_p;
4462
4463 /* Spot cases that cannot be the beginning of a
4464 nested-name-specifier. */
4465 token = cp_lexer_peek_token (parser->lexer);
4466
4467 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4468 the already parsed nested-name-specifier. */
4469 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4470 {
4471 /* Grab the nested-name-specifier and continue the loop. */
4472 cp_parser_pre_parsed_nested_name_specifier (parser);
4473 /* If we originally encountered this nested-name-specifier
4474 with IS_DECLARATION set to false, we will not have
4475 resolved TYPENAME_TYPEs, so we must do so here. */
4476 if (is_declaration
4477 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4478 {
4479 new_scope = resolve_typename_type (parser->scope,
4480 /*only_current_p=*/false);
4481 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4482 parser->scope = new_scope;
4483 }
4484 success = true;
4485 continue;
4486 }
4487
4488 /* Spot cases that cannot be the beginning of a
4489 nested-name-specifier. On the second and subsequent times
4490 through the loop, we look for the `template' keyword. */
4491 if (success && token->keyword == RID_TEMPLATE)
4492 ;
4493 /* A template-id can start a nested-name-specifier. */
4494 else if (token->type == CPP_TEMPLATE_ID)
4495 ;
4496 else
4497 {
4498 /* If the next token is not an identifier, then it is
4499 definitely not a type-name or namespace-name. */
4500 if (token->type != CPP_NAME)
4501 break;
4502 /* If the following token is neither a `<' (to begin a
4503 template-id), nor a `::', then we are not looking at a
4504 nested-name-specifier. */
4505 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4506 if (token->type != CPP_SCOPE
4507 && !cp_parser_nth_token_starts_template_argument_list_p
4508 (parser, 2))
4509 break;
4510 }
4511
4512 /* The nested-name-specifier is optional, so we parse
4513 tentatively. */
4514 cp_parser_parse_tentatively (parser);
4515
4516 /* Look for the optional `template' keyword, if this isn't the
4517 first time through the loop. */
4518 if (success)
4519 template_keyword_p = cp_parser_optional_template_keyword (parser);
4520 else
4521 template_keyword_p = false;
4522
4523 /* Save the old scope since the name lookup we are about to do
4524 might destroy it. */
4525 old_scope = parser->scope;
4526 saved_qualifying_scope = parser->qualifying_scope;
4527 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4528 look up names in "X<T>::I" in order to determine that "Y" is
4529 a template. So, if we have a typename at this point, we make
4530 an effort to look through it. */
4531 if (is_declaration
4532 && !typename_keyword_p
4533 && parser->scope
4534 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4535 parser->scope = resolve_typename_type (parser->scope,
4536 /*only_current_p=*/false);
4537 /* Parse the qualifying entity. */
4538 new_scope
4539 = cp_parser_qualifying_entity (parser,
4540 typename_keyword_p,
4541 template_keyword_p,
4542 check_dependency_p,
4543 type_p,
4544 is_declaration);
4545 /* Look for the `::' token. */
4546 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4547
4548 /* If we found what we wanted, we keep going; otherwise, we're
4549 done. */
4550 if (!cp_parser_parse_definitely (parser))
4551 {
4552 bool error_p = false;
4553
4554 /* Restore the OLD_SCOPE since it was valid before the
4555 failed attempt at finding the last
4556 class-or-namespace-name. */
4557 parser->scope = old_scope;
4558 parser->qualifying_scope = saved_qualifying_scope;
4559 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4560 break;
4561 /* If the next token is an identifier, and the one after
4562 that is a `::', then any valid interpretation would have
4563 found a class-or-namespace-name. */
4564 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4565 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4566 == CPP_SCOPE)
4567 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4568 != CPP_COMPL))
4569 {
4570 token = cp_lexer_consume_token (parser->lexer);
4571 if (!error_p)
4572 {
4573 if (!token->ambiguous_p)
4574 {
4575 tree decl;
4576 tree ambiguous_decls;
4577
4578 decl = cp_parser_lookup_name (parser, token->u.value,
4579 none_type,
4580 /*is_template=*/false,
4581 /*is_namespace=*/false,
4582 /*check_dependency=*/true,
4583 &ambiguous_decls,
4584 token->location);
4585 if (TREE_CODE (decl) == TEMPLATE_DECL)
4586 error_at (token->location,
4587 "%qD used without template parameters",
4588 decl);
4589 else if (ambiguous_decls)
4590 {
4591 error_at (token->location,
4592 "reference to %qD is ambiguous",
4593 token->u.value);
4594 print_candidates (ambiguous_decls);
4595 decl = error_mark_node;
4596 }
4597 else
4598 {
4599 if (cxx_dialect != cxx98)
4600 cp_parser_name_lookup_error
4601 (parser, token->u.value, decl, NLE_NOT_CXX98,
4602 token->location);
4603 else
4604 cp_parser_name_lookup_error
4605 (parser, token->u.value, decl, NLE_CXX98,
4606 token->location);
4607 }
4608 }
4609 parser->scope = error_mark_node;
4610 error_p = true;
4611 /* Treat this as a successful nested-name-specifier
4612 due to:
4613
4614 [basic.lookup.qual]
4615
4616 If the name found is not a class-name (clause
4617 _class_) or namespace-name (_namespace.def_), the
4618 program is ill-formed. */
4619 success = true;
4620 }
4621 cp_lexer_consume_token (parser->lexer);
4622 }
4623 break;
4624 }
4625 /* We've found one valid nested-name-specifier. */
4626 success = true;
4627 /* Name lookup always gives us a DECL. */
4628 if (TREE_CODE (new_scope) == TYPE_DECL)
4629 new_scope = TREE_TYPE (new_scope);
4630 /* Uses of "template" must be followed by actual templates. */
4631 if (template_keyword_p
4632 && !(CLASS_TYPE_P (new_scope)
4633 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4634 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4635 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4636 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4637 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4638 == TEMPLATE_ID_EXPR)))
4639 permerror (input_location, TYPE_P (new_scope)
4640 ? "%qT is not a template"
4641 : "%qD is not a template",
4642 new_scope);
4643 /* If it is a class scope, try to complete it; we are about to
4644 be looking up names inside the class. */
4645 if (TYPE_P (new_scope)
4646 /* Since checking types for dependency can be expensive,
4647 avoid doing it if the type is already complete. */
4648 && !COMPLETE_TYPE_P (new_scope)
4649 /* Do not try to complete dependent types. */
4650 && !dependent_type_p (new_scope))
4651 {
4652 new_scope = complete_type (new_scope);
4653 /* If it is a typedef to current class, use the current
4654 class instead, as the typedef won't have any names inside
4655 it yet. */
4656 if (!COMPLETE_TYPE_P (new_scope)
4657 && currently_open_class (new_scope))
4658 new_scope = TYPE_MAIN_VARIANT (new_scope);
4659 }
4660 /* Make sure we look in the right scope the next time through
4661 the loop. */
4662 parser->scope = new_scope;
4663 }
4664
4665 /* If parsing tentatively, replace the sequence of tokens that makes
4666 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4667 token. That way, should we re-parse the token stream, we will
4668 not have to repeat the effort required to do the parse, nor will
4669 we issue duplicate error messages. */
4670 if (success && start)
4671 {
4672 cp_token *token;
4673
4674 token = cp_lexer_token_at (parser->lexer, start);
4675 /* Reset the contents of the START token. */
4676 token->type = CPP_NESTED_NAME_SPECIFIER;
4677 /* Retrieve any deferred checks. Do not pop this access checks yet
4678 so the memory will not be reclaimed during token replacing below. */
4679 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4680 token->u.tree_check_value->value = parser->scope;
4681 token->u.tree_check_value->checks = get_deferred_access_checks ();
4682 token->u.tree_check_value->qualifying_scope =
4683 parser->qualifying_scope;
4684 token->keyword = RID_MAX;
4685
4686 /* Purge all subsequent tokens. */
4687 cp_lexer_purge_tokens_after (parser->lexer, start);
4688 }
4689
4690 if (start)
4691 pop_to_parent_deferring_access_checks ();
4692
4693 return success ? parser->scope : NULL_TREE;
4694 }
4695
4696 /* Parse a nested-name-specifier. See
4697 cp_parser_nested_name_specifier_opt for details. This function
4698 behaves identically, except that it will an issue an error if no
4699 nested-name-specifier is present. */
4700
4701 static tree
4702 cp_parser_nested_name_specifier (cp_parser *parser,
4703 bool typename_keyword_p,
4704 bool check_dependency_p,
4705 bool type_p,
4706 bool is_declaration)
4707 {
4708 tree scope;
4709
4710 /* Look for the nested-name-specifier. */
4711 scope = cp_parser_nested_name_specifier_opt (parser,
4712 typename_keyword_p,
4713 check_dependency_p,
4714 type_p,
4715 is_declaration);
4716 /* If it was not present, issue an error message. */
4717 if (!scope)
4718 {
4719 cp_parser_error (parser, "expected nested-name-specifier");
4720 parser->scope = NULL_TREE;
4721 }
4722
4723 return scope;
4724 }
4725
4726 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4727 this is either a class-name or a namespace-name (which corresponds
4728 to the class-or-namespace-name production in the grammar). For
4729 C++0x, it can also be a type-name that refers to an enumeration
4730 type.
4731
4732 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4733 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4734 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4735 TYPE_P is TRUE iff the next name should be taken as a class-name,
4736 even the same name is declared to be another entity in the same
4737 scope.
4738
4739 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4740 specified by the class-or-namespace-name. If neither is found the
4741 ERROR_MARK_NODE is returned. */
4742
4743 static tree
4744 cp_parser_qualifying_entity (cp_parser *parser,
4745 bool typename_keyword_p,
4746 bool template_keyword_p,
4747 bool check_dependency_p,
4748 bool type_p,
4749 bool is_declaration)
4750 {
4751 tree saved_scope;
4752 tree saved_qualifying_scope;
4753 tree saved_object_scope;
4754 tree scope;
4755 bool only_class_p;
4756 bool successful_parse_p;
4757
4758 /* Before we try to parse the class-name, we must save away the
4759 current PARSER->SCOPE since cp_parser_class_name will destroy
4760 it. */
4761 saved_scope = parser->scope;
4762 saved_qualifying_scope = parser->qualifying_scope;
4763 saved_object_scope = parser->object_scope;
4764 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4765 there is no need to look for a namespace-name. */
4766 only_class_p = template_keyword_p
4767 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4768 if (!only_class_p)
4769 cp_parser_parse_tentatively (parser);
4770 scope = cp_parser_class_name (parser,
4771 typename_keyword_p,
4772 template_keyword_p,
4773 type_p ? class_type : none_type,
4774 check_dependency_p,
4775 /*class_head_p=*/false,
4776 is_declaration);
4777 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4778 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4779 if (!only_class_p
4780 && cxx_dialect != cxx98
4781 && !successful_parse_p)
4782 {
4783 /* Restore the saved scope. */
4784 parser->scope = saved_scope;
4785 parser->qualifying_scope = saved_qualifying_scope;
4786 parser->object_scope = saved_object_scope;
4787
4788 /* Parse tentatively. */
4789 cp_parser_parse_tentatively (parser);
4790
4791 /* Parse a typedef-name or enum-name. */
4792 scope = cp_parser_nonclass_name (parser);
4793
4794 /* "If the name found does not designate a namespace or a class,
4795 enumeration, or dependent type, the program is ill-formed."
4796
4797 We cover classes and dependent types above and namespaces below,
4798 so this code is only looking for enums. */
4799 if (!scope || TREE_CODE (scope) != TYPE_DECL
4800 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4801 cp_parser_simulate_error (parser);
4802
4803 successful_parse_p = cp_parser_parse_definitely (parser);
4804 }
4805 /* If that didn't work, try for a namespace-name. */
4806 if (!only_class_p && !successful_parse_p)
4807 {
4808 /* Restore the saved scope. */
4809 parser->scope = saved_scope;
4810 parser->qualifying_scope = saved_qualifying_scope;
4811 parser->object_scope = saved_object_scope;
4812 /* If we are not looking at an identifier followed by the scope
4813 resolution operator, then this is not part of a
4814 nested-name-specifier. (Note that this function is only used
4815 to parse the components of a nested-name-specifier.) */
4816 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4817 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4818 return error_mark_node;
4819 scope = cp_parser_namespace_name (parser);
4820 }
4821
4822 return scope;
4823 }
4824
4825 /* Parse a postfix-expression.
4826
4827 postfix-expression:
4828 primary-expression
4829 postfix-expression [ expression ]
4830 postfix-expression ( expression-list [opt] )
4831 simple-type-specifier ( expression-list [opt] )
4832 typename :: [opt] nested-name-specifier identifier
4833 ( expression-list [opt] )
4834 typename :: [opt] nested-name-specifier template [opt] template-id
4835 ( expression-list [opt] )
4836 postfix-expression . template [opt] id-expression
4837 postfix-expression -> template [opt] id-expression
4838 postfix-expression . pseudo-destructor-name
4839 postfix-expression -> pseudo-destructor-name
4840 postfix-expression ++
4841 postfix-expression --
4842 dynamic_cast < type-id > ( expression )
4843 static_cast < type-id > ( expression )
4844 reinterpret_cast < type-id > ( expression )
4845 const_cast < type-id > ( expression )
4846 typeid ( expression )
4847 typeid ( type-id )
4848
4849 GNU Extension:
4850
4851 postfix-expression:
4852 ( type-id ) { initializer-list , [opt] }
4853
4854 This extension is a GNU version of the C99 compound-literal
4855 construct. (The C99 grammar uses `type-name' instead of `type-id',
4856 but they are essentially the same concept.)
4857
4858 If ADDRESS_P is true, the postfix expression is the operand of the
4859 `&' operator. CAST_P is true if this expression is the target of a
4860 cast.
4861
4862 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4863 class member access expressions [expr.ref].
4864
4865 Returns a representation of the expression. */
4866
4867 static tree
4868 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4869 bool member_access_only_p,
4870 cp_id_kind * pidk_return)
4871 {
4872 cp_token *token;
4873 enum rid keyword;
4874 cp_id_kind idk = CP_ID_KIND_NONE;
4875 tree postfix_expression = NULL_TREE;
4876 bool is_member_access = false;
4877
4878 /* Peek at the next token. */
4879 token = cp_lexer_peek_token (parser->lexer);
4880 /* Some of the productions are determined by keywords. */
4881 keyword = token->keyword;
4882 switch (keyword)
4883 {
4884 case RID_DYNCAST:
4885 case RID_STATCAST:
4886 case RID_REINTCAST:
4887 case RID_CONSTCAST:
4888 {
4889 tree type;
4890 tree expression;
4891 const char *saved_message;
4892
4893 /* All of these can be handled in the same way from the point
4894 of view of parsing. Begin by consuming the token
4895 identifying the cast. */
4896 cp_lexer_consume_token (parser->lexer);
4897
4898 /* New types cannot be defined in the cast. */
4899 saved_message = parser->type_definition_forbidden_message;
4900 parser->type_definition_forbidden_message
4901 = G_("types may not be defined in casts");
4902
4903 /* Look for the opening `<'. */
4904 cp_parser_require (parser, CPP_LESS, RT_LESS);
4905 /* Parse the type to which we are casting. */
4906 type = cp_parser_type_id (parser);
4907 /* Look for the closing `>'. */
4908 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4909 /* Restore the old message. */
4910 parser->type_definition_forbidden_message = saved_message;
4911
4912 /* And the expression which is being cast. */
4913 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4914 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4915 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4916
4917 /* Only type conversions to integral or enumeration types
4918 can be used in constant-expressions. */
4919 if (!cast_valid_in_integral_constant_expression_p (type)
4920 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4921 return error_mark_node;
4922
4923 switch (keyword)
4924 {
4925 case RID_DYNCAST:
4926 postfix_expression
4927 = build_dynamic_cast (type, expression, tf_warning_or_error);
4928 break;
4929 case RID_STATCAST:
4930 postfix_expression
4931 = build_static_cast (type, expression, tf_warning_or_error);
4932 break;
4933 case RID_REINTCAST:
4934 postfix_expression
4935 = build_reinterpret_cast (type, expression,
4936 tf_warning_or_error);
4937 break;
4938 case RID_CONSTCAST:
4939 postfix_expression
4940 = build_const_cast (type, expression, tf_warning_or_error);
4941 break;
4942 default:
4943 gcc_unreachable ();
4944 }
4945 }
4946 break;
4947
4948 case RID_TYPEID:
4949 {
4950 tree type;
4951 const char *saved_message;
4952 bool saved_in_type_id_in_expr_p;
4953
4954 /* Consume the `typeid' token. */
4955 cp_lexer_consume_token (parser->lexer);
4956 /* Look for the `(' token. */
4957 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4958 /* Types cannot be defined in a `typeid' expression. */
4959 saved_message = parser->type_definition_forbidden_message;
4960 parser->type_definition_forbidden_message
4961 = G_("types may not be defined in a %<typeid%> expression");
4962 /* We can't be sure yet whether we're looking at a type-id or an
4963 expression. */
4964 cp_parser_parse_tentatively (parser);
4965 /* Try a type-id first. */
4966 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4967 parser->in_type_id_in_expr_p = true;
4968 type = cp_parser_type_id (parser);
4969 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4970 /* Look for the `)' token. Otherwise, we can't be sure that
4971 we're not looking at an expression: consider `typeid (int
4972 (3))', for example. */
4973 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4974 /* If all went well, simply lookup the type-id. */
4975 if (cp_parser_parse_definitely (parser))
4976 postfix_expression = get_typeid (type);
4977 /* Otherwise, fall back to the expression variant. */
4978 else
4979 {
4980 tree expression;
4981
4982 /* Look for an expression. */
4983 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4984 /* Compute its typeid. */
4985 postfix_expression = build_typeid (expression);
4986 /* Look for the `)' token. */
4987 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4988 }
4989 /* Restore the saved message. */
4990 parser->type_definition_forbidden_message = saved_message;
4991 /* `typeid' may not appear in an integral constant expression. */
4992 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4993 return error_mark_node;
4994 }
4995 break;
4996
4997 case RID_TYPENAME:
4998 {
4999 tree type;
5000 /* The syntax permitted here is the same permitted for an
5001 elaborated-type-specifier. */
5002 type = cp_parser_elaborated_type_specifier (parser,
5003 /*is_friend=*/false,
5004 /*is_declaration=*/false);
5005 postfix_expression = cp_parser_functional_cast (parser, type);
5006 }
5007 break;
5008
5009 default:
5010 {
5011 tree type;
5012
5013 /* If the next thing is a simple-type-specifier, we may be
5014 looking at a functional cast. We could also be looking at
5015 an id-expression. So, we try the functional cast, and if
5016 that doesn't work we fall back to the primary-expression. */
5017 cp_parser_parse_tentatively (parser);
5018 /* Look for the simple-type-specifier. */
5019 type = cp_parser_simple_type_specifier (parser,
5020 /*decl_specs=*/NULL,
5021 CP_PARSER_FLAGS_NONE);
5022 /* Parse the cast itself. */
5023 if (!cp_parser_error_occurred (parser))
5024 postfix_expression
5025 = cp_parser_functional_cast (parser, type);
5026 /* If that worked, we're done. */
5027 if (cp_parser_parse_definitely (parser))
5028 break;
5029
5030 /* If the functional-cast didn't work out, try a
5031 compound-literal. */
5032 if (cp_parser_allow_gnu_extensions_p (parser)
5033 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5034 {
5035 VEC(constructor_elt,gc) *initializer_list = NULL;
5036 bool saved_in_type_id_in_expr_p;
5037
5038 cp_parser_parse_tentatively (parser);
5039 /* Consume the `('. */
5040 cp_lexer_consume_token (parser->lexer);
5041 /* Parse the type. */
5042 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5043 parser->in_type_id_in_expr_p = true;
5044 type = cp_parser_type_id (parser);
5045 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5046 /* Look for the `)'. */
5047 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5048 /* Look for the `{'. */
5049 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5050 /* If things aren't going well, there's no need to
5051 keep going. */
5052 if (!cp_parser_error_occurred (parser))
5053 {
5054 bool non_constant_p;
5055 /* Parse the initializer-list. */
5056 initializer_list
5057 = cp_parser_initializer_list (parser, &non_constant_p);
5058 /* Allow a trailing `,'. */
5059 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5060 cp_lexer_consume_token (parser->lexer);
5061 /* Look for the final `}'. */
5062 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5063 }
5064 /* If that worked, we're definitely looking at a
5065 compound-literal expression. */
5066 if (cp_parser_parse_definitely (parser))
5067 {
5068 /* Warn the user that a compound literal is not
5069 allowed in standard C++. */
5070 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5071 /* For simplicity, we disallow compound literals in
5072 constant-expressions. We could
5073 allow compound literals of integer type, whose
5074 initializer was a constant, in constant
5075 expressions. Permitting that usage, as a further
5076 extension, would not change the meaning of any
5077 currently accepted programs. (Of course, as
5078 compound literals are not part of ISO C++, the
5079 standard has nothing to say.) */
5080 if (cp_parser_non_integral_constant_expression (parser,
5081 NIC_NCC))
5082 {
5083 postfix_expression = error_mark_node;
5084 break;
5085 }
5086 /* Form the representation of the compound-literal. */
5087 postfix_expression
5088 = (finish_compound_literal
5089 (type, build_constructor (init_list_type_node,
5090 initializer_list)));
5091 break;
5092 }
5093 }
5094
5095 /* It must be a primary-expression. */
5096 postfix_expression
5097 = cp_parser_primary_expression (parser, address_p, cast_p,
5098 /*template_arg_p=*/false,
5099 &idk);
5100 }
5101 break;
5102 }
5103
5104 /* Keep looping until the postfix-expression is complete. */
5105 while (true)
5106 {
5107 if (idk == CP_ID_KIND_UNQUALIFIED
5108 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5109 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5110 /* It is not a Koenig lookup function call. */
5111 postfix_expression
5112 = unqualified_name_lookup_error (postfix_expression);
5113
5114 /* Peek at the next token. */
5115 token = cp_lexer_peek_token (parser->lexer);
5116
5117 switch (token->type)
5118 {
5119 case CPP_OPEN_SQUARE:
5120 postfix_expression
5121 = cp_parser_postfix_open_square_expression (parser,
5122 postfix_expression,
5123 false);
5124 idk = CP_ID_KIND_NONE;
5125 is_member_access = false;
5126 break;
5127
5128 case CPP_OPEN_PAREN:
5129 /* postfix-expression ( expression-list [opt] ) */
5130 {
5131 bool koenig_p;
5132 bool is_builtin_constant_p;
5133 bool saved_integral_constant_expression_p = false;
5134 bool saved_non_integral_constant_expression_p = false;
5135 VEC(tree,gc) *args;
5136
5137 is_member_access = false;
5138
5139 is_builtin_constant_p
5140 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5141 if (is_builtin_constant_p)
5142 {
5143 /* The whole point of __builtin_constant_p is to allow
5144 non-constant expressions to appear as arguments. */
5145 saved_integral_constant_expression_p
5146 = parser->integral_constant_expression_p;
5147 saved_non_integral_constant_expression_p
5148 = parser->non_integral_constant_expression_p;
5149 parser->integral_constant_expression_p = false;
5150 }
5151 args = (cp_parser_parenthesized_expression_list
5152 (parser, non_attr,
5153 /*cast_p=*/false, /*allow_expansion_p=*/true,
5154 /*non_constant_p=*/NULL));
5155 if (is_builtin_constant_p)
5156 {
5157 parser->integral_constant_expression_p
5158 = saved_integral_constant_expression_p;
5159 parser->non_integral_constant_expression_p
5160 = saved_non_integral_constant_expression_p;
5161 }
5162
5163 if (args == NULL)
5164 {
5165 postfix_expression = error_mark_node;
5166 break;
5167 }
5168
5169 /* Function calls are not permitted in
5170 constant-expressions. */
5171 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5172 && cp_parser_non_integral_constant_expression (parser,
5173 NIC_FUNC_CALL))
5174 {
5175 postfix_expression = error_mark_node;
5176 release_tree_vector (args);
5177 break;
5178 }
5179
5180 koenig_p = false;
5181 if (idk == CP_ID_KIND_UNQUALIFIED
5182 || idk == CP_ID_KIND_TEMPLATE_ID)
5183 {
5184 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5185 {
5186 if (!VEC_empty (tree, args))
5187 {
5188 koenig_p = true;
5189 if (!any_type_dependent_arguments_p (args))
5190 postfix_expression
5191 = perform_koenig_lookup (postfix_expression, args,
5192 /*include_std=*/false);
5193 }
5194 else
5195 postfix_expression
5196 = unqualified_fn_lookup_error (postfix_expression);
5197 }
5198 /* We do not perform argument-dependent lookup if
5199 normal lookup finds a non-function, in accordance
5200 with the expected resolution of DR 218. */
5201 else if (!VEC_empty (tree, args)
5202 && is_overloaded_fn (postfix_expression))
5203 {
5204 tree fn = get_first_fn (postfix_expression);
5205 fn = STRIP_TEMPLATE (fn);
5206
5207 /* Do not do argument dependent lookup if regular
5208 lookup finds a member function or a block-scope
5209 function declaration. [basic.lookup.argdep]/3 */
5210 if (!DECL_FUNCTION_MEMBER_P (fn)
5211 && !DECL_LOCAL_FUNCTION_P (fn))
5212 {
5213 koenig_p = true;
5214 if (!any_type_dependent_arguments_p (args))
5215 postfix_expression
5216 = perform_koenig_lookup (postfix_expression, args,
5217 /*include_std=*/false);
5218 }
5219 }
5220 }
5221
5222 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5223 {
5224 tree instance = TREE_OPERAND (postfix_expression, 0);
5225 tree fn = TREE_OPERAND (postfix_expression, 1);
5226
5227 if (processing_template_decl
5228 && (type_dependent_expression_p (instance)
5229 || (!BASELINK_P (fn)
5230 && TREE_CODE (fn) != FIELD_DECL)
5231 || type_dependent_expression_p (fn)
5232 || any_type_dependent_arguments_p (args)))
5233 {
5234 postfix_expression
5235 = build_nt_call_vec (postfix_expression, args);
5236 release_tree_vector (args);
5237 break;
5238 }
5239
5240 if (BASELINK_P (fn))
5241 {
5242 postfix_expression
5243 = (build_new_method_call
5244 (instance, fn, &args, NULL_TREE,
5245 (idk == CP_ID_KIND_QUALIFIED
5246 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5247 /*fn_p=*/NULL,
5248 tf_warning_or_error));
5249 }
5250 else
5251 postfix_expression
5252 = finish_call_expr (postfix_expression, &args,
5253 /*disallow_virtual=*/false,
5254 /*koenig_p=*/false,
5255 tf_warning_or_error);
5256 }
5257 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5258 || TREE_CODE (postfix_expression) == MEMBER_REF
5259 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5260 postfix_expression = (build_offset_ref_call_from_tree
5261 (postfix_expression, &args));
5262 else if (idk == CP_ID_KIND_QUALIFIED)
5263 /* A call to a static class member, or a namespace-scope
5264 function. */
5265 postfix_expression
5266 = finish_call_expr (postfix_expression, &args,
5267 /*disallow_virtual=*/true,
5268 koenig_p,
5269 tf_warning_or_error);
5270 else
5271 /* All other function calls. */
5272 postfix_expression
5273 = finish_call_expr (postfix_expression, &args,
5274 /*disallow_virtual=*/false,
5275 koenig_p,
5276 tf_warning_or_error);
5277
5278 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5279 idk = CP_ID_KIND_NONE;
5280
5281 release_tree_vector (args);
5282 }
5283 break;
5284
5285 case CPP_DOT:
5286 case CPP_DEREF:
5287 /* postfix-expression . template [opt] id-expression
5288 postfix-expression . pseudo-destructor-name
5289 postfix-expression -> template [opt] id-expression
5290 postfix-expression -> pseudo-destructor-name */
5291
5292 /* Consume the `.' or `->' operator. */
5293 cp_lexer_consume_token (parser->lexer);
5294
5295 postfix_expression
5296 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5297 postfix_expression,
5298 false, &idk,
5299 token->location);
5300
5301 is_member_access = true;
5302 break;
5303
5304 case CPP_PLUS_PLUS:
5305 /* postfix-expression ++ */
5306 /* Consume the `++' token. */
5307 cp_lexer_consume_token (parser->lexer);
5308 /* Generate a representation for the complete expression. */
5309 postfix_expression
5310 = finish_increment_expr (postfix_expression,
5311 POSTINCREMENT_EXPR);
5312 /* Increments may not appear in constant-expressions. */
5313 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5314 postfix_expression = error_mark_node;
5315 idk = CP_ID_KIND_NONE;
5316 is_member_access = false;
5317 break;
5318
5319 case CPP_MINUS_MINUS:
5320 /* postfix-expression -- */
5321 /* Consume the `--' token. */
5322 cp_lexer_consume_token (parser->lexer);
5323 /* Generate a representation for the complete expression. */
5324 postfix_expression
5325 = finish_increment_expr (postfix_expression,
5326 POSTDECREMENT_EXPR);
5327 /* Decrements may not appear in constant-expressions. */
5328 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5329 postfix_expression = error_mark_node;
5330 idk = CP_ID_KIND_NONE;
5331 is_member_access = false;
5332 break;
5333
5334 default:
5335 if (pidk_return != NULL)
5336 * pidk_return = idk;
5337 if (member_access_only_p)
5338 return is_member_access? postfix_expression : error_mark_node;
5339 else
5340 return postfix_expression;
5341 }
5342 }
5343
5344 /* We should never get here. */
5345 gcc_unreachable ();
5346 return error_mark_node;
5347 }
5348
5349 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5350 by cp_parser_builtin_offsetof. We're looking for
5351
5352 postfix-expression [ expression ]
5353
5354 FOR_OFFSETOF is set if we're being called in that context, which
5355 changes how we deal with integer constant expressions. */
5356
5357 static tree
5358 cp_parser_postfix_open_square_expression (cp_parser *parser,
5359 tree postfix_expression,
5360 bool for_offsetof)
5361 {
5362 tree index;
5363
5364 /* Consume the `[' token. */
5365 cp_lexer_consume_token (parser->lexer);
5366
5367 /* Parse the index expression. */
5368 /* ??? For offsetof, there is a question of what to allow here. If
5369 offsetof is not being used in an integral constant expression context,
5370 then we *could* get the right answer by computing the value at runtime.
5371 If we are in an integral constant expression context, then we might
5372 could accept any constant expression; hard to say without analysis.
5373 Rather than open the barn door too wide right away, allow only integer
5374 constant expressions here. */
5375 if (for_offsetof)
5376 index = cp_parser_constant_expression (parser, false, NULL);
5377 else
5378 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5379
5380 /* Look for the closing `]'. */
5381 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5382
5383 /* Build the ARRAY_REF. */
5384 postfix_expression = grok_array_decl (postfix_expression, index);
5385
5386 /* When not doing offsetof, array references are not permitted in
5387 constant-expressions. */
5388 if (!for_offsetof
5389 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5390 postfix_expression = error_mark_node;
5391
5392 return postfix_expression;
5393 }
5394
5395 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5396 by cp_parser_builtin_offsetof. We're looking for
5397
5398 postfix-expression . template [opt] id-expression
5399 postfix-expression . pseudo-destructor-name
5400 postfix-expression -> template [opt] id-expression
5401 postfix-expression -> pseudo-destructor-name
5402
5403 FOR_OFFSETOF is set if we're being called in that context. That sorta
5404 limits what of the above we'll actually accept, but nevermind.
5405 TOKEN_TYPE is the "." or "->" token, which will already have been
5406 removed from the stream. */
5407
5408 static tree
5409 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5410 enum cpp_ttype token_type,
5411 tree postfix_expression,
5412 bool for_offsetof, cp_id_kind *idk,
5413 location_t location)
5414 {
5415 tree name;
5416 bool dependent_p;
5417 bool pseudo_destructor_p;
5418 tree scope = NULL_TREE;
5419
5420 /* If this is a `->' operator, dereference the pointer. */
5421 if (token_type == CPP_DEREF)
5422 postfix_expression = build_x_arrow (postfix_expression);
5423 /* Check to see whether or not the expression is type-dependent. */
5424 dependent_p = type_dependent_expression_p (postfix_expression);
5425 /* The identifier following the `->' or `.' is not qualified. */
5426 parser->scope = NULL_TREE;
5427 parser->qualifying_scope = NULL_TREE;
5428 parser->object_scope = NULL_TREE;
5429 *idk = CP_ID_KIND_NONE;
5430
5431 /* Enter the scope corresponding to the type of the object
5432 given by the POSTFIX_EXPRESSION. */
5433 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5434 {
5435 scope = TREE_TYPE (postfix_expression);
5436 /* According to the standard, no expression should ever have
5437 reference type. Unfortunately, we do not currently match
5438 the standard in this respect in that our internal representation
5439 of an expression may have reference type even when the standard
5440 says it does not. Therefore, we have to manually obtain the
5441 underlying type here. */
5442 scope = non_reference (scope);
5443 /* The type of the POSTFIX_EXPRESSION must be complete. */
5444 if (scope == unknown_type_node)
5445 {
5446 error_at (location, "%qE does not have class type",
5447 postfix_expression);
5448 scope = NULL_TREE;
5449 }
5450 else
5451 scope = complete_type_or_else (scope, NULL_TREE);
5452 /* Let the name lookup machinery know that we are processing a
5453 class member access expression. */
5454 parser->context->object_type = scope;
5455 /* If something went wrong, we want to be able to discern that case,
5456 as opposed to the case where there was no SCOPE due to the type
5457 of expression being dependent. */
5458 if (!scope)
5459 scope = error_mark_node;
5460 /* If the SCOPE was erroneous, make the various semantic analysis
5461 functions exit quickly -- and without issuing additional error
5462 messages. */
5463 if (scope == error_mark_node)
5464 postfix_expression = error_mark_node;
5465 }
5466
5467 /* Assume this expression is not a pseudo-destructor access. */
5468 pseudo_destructor_p = false;
5469
5470 /* If the SCOPE is a scalar type, then, if this is a valid program,
5471 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5472 is type dependent, it can be pseudo-destructor-name or something else.
5473 Try to parse it as pseudo-destructor-name first. */
5474 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5475 {
5476 tree s;
5477 tree type;
5478
5479 cp_parser_parse_tentatively (parser);
5480 /* Parse the pseudo-destructor-name. */
5481 s = NULL_TREE;
5482 cp_parser_pseudo_destructor_name (parser, &s, &type);
5483 if (dependent_p
5484 && (cp_parser_error_occurred (parser)
5485 || TREE_CODE (type) != TYPE_DECL
5486 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5487 cp_parser_abort_tentative_parse (parser);
5488 else if (cp_parser_parse_definitely (parser))
5489 {
5490 pseudo_destructor_p = true;
5491 postfix_expression
5492 = finish_pseudo_destructor_expr (postfix_expression,
5493 s, TREE_TYPE (type));
5494 }
5495 }
5496
5497 if (!pseudo_destructor_p)
5498 {
5499 /* If the SCOPE is not a scalar type, we are looking at an
5500 ordinary class member access expression, rather than a
5501 pseudo-destructor-name. */
5502 bool template_p;
5503 cp_token *token = cp_lexer_peek_token (parser->lexer);
5504 /* Parse the id-expression. */
5505 name = (cp_parser_id_expression
5506 (parser,
5507 cp_parser_optional_template_keyword (parser),
5508 /*check_dependency_p=*/true,
5509 &template_p,
5510 /*declarator_p=*/false,
5511 /*optional_p=*/false));
5512 /* In general, build a SCOPE_REF if the member name is qualified.
5513 However, if the name was not dependent and has already been
5514 resolved; there is no need to build the SCOPE_REF. For example;
5515
5516 struct X { void f(); };
5517 template <typename T> void f(T* t) { t->X::f(); }
5518
5519 Even though "t" is dependent, "X::f" is not and has been resolved
5520 to a BASELINK; there is no need to include scope information. */
5521
5522 /* But we do need to remember that there was an explicit scope for
5523 virtual function calls. */
5524 if (parser->scope)
5525 *idk = CP_ID_KIND_QUALIFIED;
5526
5527 /* If the name is a template-id that names a type, we will get a
5528 TYPE_DECL here. That is invalid code. */
5529 if (TREE_CODE (name) == TYPE_DECL)
5530 {
5531 error_at (token->location, "invalid use of %qD", name);
5532 postfix_expression = error_mark_node;
5533 }
5534 else
5535 {
5536 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5537 {
5538 name = build_qualified_name (/*type=*/NULL_TREE,
5539 parser->scope,
5540 name,
5541 template_p);
5542 parser->scope = NULL_TREE;
5543 parser->qualifying_scope = NULL_TREE;
5544 parser->object_scope = NULL_TREE;
5545 }
5546 if (scope && name && BASELINK_P (name))
5547 adjust_result_of_qualified_name_lookup
5548 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5549 postfix_expression
5550 = finish_class_member_access_expr (postfix_expression, name,
5551 template_p,
5552 tf_warning_or_error);
5553 }
5554 }
5555
5556 /* We no longer need to look up names in the scope of the object on
5557 the left-hand side of the `.' or `->' operator. */
5558 parser->context->object_type = NULL_TREE;
5559
5560 /* Outside of offsetof, these operators may not appear in
5561 constant-expressions. */
5562 if (!for_offsetof
5563 && (cp_parser_non_integral_constant_expression
5564 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5565 postfix_expression = error_mark_node;
5566
5567 return postfix_expression;
5568 }
5569
5570 /* Parse a parenthesized expression-list.
5571
5572 expression-list:
5573 assignment-expression
5574 expression-list, assignment-expression
5575
5576 attribute-list:
5577 expression-list
5578 identifier
5579 identifier, expression-list
5580
5581 CAST_P is true if this expression is the target of a cast.
5582
5583 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5584 argument pack.
5585
5586 Returns a vector of trees. Each element is a representation of an
5587 assignment-expression. NULL is returned if the ( and or ) are
5588 missing. An empty, but allocated, vector is returned on no
5589 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5590 if we are parsing an attribute list for an attribute that wants a
5591 plain identifier argument, normal_attr for an attribute that wants
5592 an expression, or non_attr if we aren't parsing an attribute list. If
5593 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5594 not all of the expressions in the list were constant. */
5595
5596 static VEC(tree,gc) *
5597 cp_parser_parenthesized_expression_list (cp_parser* parser,
5598 int is_attribute_list,
5599 bool cast_p,
5600 bool allow_expansion_p,
5601 bool *non_constant_p)
5602 {
5603 VEC(tree,gc) *expression_list;
5604 bool fold_expr_p = is_attribute_list != non_attr;
5605 tree identifier = NULL_TREE;
5606 bool saved_greater_than_is_operator_p;
5607
5608 /* Assume all the expressions will be constant. */
5609 if (non_constant_p)
5610 *non_constant_p = false;
5611
5612 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5613 return NULL;
5614
5615 expression_list = make_tree_vector ();
5616
5617 /* Within a parenthesized expression, a `>' token is always
5618 the greater-than operator. */
5619 saved_greater_than_is_operator_p
5620 = parser->greater_than_is_operator_p;
5621 parser->greater_than_is_operator_p = true;
5622
5623 /* Consume expressions until there are no more. */
5624 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5625 while (true)
5626 {
5627 tree expr;
5628
5629 /* At the beginning of attribute lists, check to see if the
5630 next token is an identifier. */
5631 if (is_attribute_list == id_attr
5632 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5633 {
5634 cp_token *token;
5635
5636 /* Consume the identifier. */
5637 token = cp_lexer_consume_token (parser->lexer);
5638 /* Save the identifier. */
5639 identifier = token->u.value;
5640 }
5641 else
5642 {
5643 bool expr_non_constant_p;
5644
5645 /* Parse the next assignment-expression. */
5646 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5647 {
5648 /* A braced-init-list. */
5649 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5650 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5651 if (non_constant_p && expr_non_constant_p)
5652 *non_constant_p = true;
5653 }
5654 else if (non_constant_p)
5655 {
5656 expr = (cp_parser_constant_expression
5657 (parser, /*allow_non_constant_p=*/true,
5658 &expr_non_constant_p));
5659 if (expr_non_constant_p)
5660 *non_constant_p = true;
5661 }
5662 else
5663 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5664
5665 if (fold_expr_p)
5666 expr = fold_non_dependent_expr (expr);
5667
5668 /* If we have an ellipsis, then this is an expression
5669 expansion. */
5670 if (allow_expansion_p
5671 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5672 {
5673 /* Consume the `...'. */
5674 cp_lexer_consume_token (parser->lexer);
5675
5676 /* Build the argument pack. */
5677 expr = make_pack_expansion (expr);
5678 }
5679
5680 /* Add it to the list. We add error_mark_node
5681 expressions to the list, so that we can still tell if
5682 the correct form for a parenthesized expression-list
5683 is found. That gives better errors. */
5684 VEC_safe_push (tree, gc, expression_list, expr);
5685
5686 if (expr == error_mark_node)
5687 goto skip_comma;
5688 }
5689
5690 /* After the first item, attribute lists look the same as
5691 expression lists. */
5692 is_attribute_list = non_attr;
5693
5694 get_comma:;
5695 /* If the next token isn't a `,', then we are done. */
5696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5697 break;
5698
5699 /* Otherwise, consume the `,' and keep going. */
5700 cp_lexer_consume_token (parser->lexer);
5701 }
5702
5703 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5704 {
5705 int ending;
5706
5707 skip_comma:;
5708 /* We try and resync to an unnested comma, as that will give the
5709 user better diagnostics. */
5710 ending = cp_parser_skip_to_closing_parenthesis (parser,
5711 /*recovering=*/true,
5712 /*or_comma=*/true,
5713 /*consume_paren=*/true);
5714 if (ending < 0)
5715 goto get_comma;
5716 if (!ending)
5717 {
5718 parser->greater_than_is_operator_p
5719 = saved_greater_than_is_operator_p;
5720 return NULL;
5721 }
5722 }
5723
5724 parser->greater_than_is_operator_p
5725 = saved_greater_than_is_operator_p;
5726
5727 if (identifier)
5728 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5729
5730 return expression_list;
5731 }
5732
5733 /* Parse a pseudo-destructor-name.
5734
5735 pseudo-destructor-name:
5736 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5737 :: [opt] nested-name-specifier template template-id :: ~ type-name
5738 :: [opt] nested-name-specifier [opt] ~ type-name
5739
5740 If either of the first two productions is used, sets *SCOPE to the
5741 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5742 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5743 or ERROR_MARK_NODE if the parse fails. */
5744
5745 static void
5746 cp_parser_pseudo_destructor_name (cp_parser* parser,
5747 tree* scope,
5748 tree* type)
5749 {
5750 bool nested_name_specifier_p;
5751
5752 /* Assume that things will not work out. */
5753 *type = error_mark_node;
5754
5755 /* Look for the optional `::' operator. */
5756 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5757 /* Look for the optional nested-name-specifier. */
5758 nested_name_specifier_p
5759 = (cp_parser_nested_name_specifier_opt (parser,
5760 /*typename_keyword_p=*/false,
5761 /*check_dependency_p=*/true,
5762 /*type_p=*/false,
5763 /*is_declaration=*/false)
5764 != NULL_TREE);
5765 /* Now, if we saw a nested-name-specifier, we might be doing the
5766 second production. */
5767 if (nested_name_specifier_p
5768 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5769 {
5770 /* Consume the `template' keyword. */
5771 cp_lexer_consume_token (parser->lexer);
5772 /* Parse the template-id. */
5773 cp_parser_template_id (parser,
5774 /*template_keyword_p=*/true,
5775 /*check_dependency_p=*/false,
5776 /*is_declaration=*/true);
5777 /* Look for the `::' token. */
5778 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5779 }
5780 /* If the next token is not a `~', then there might be some
5781 additional qualification. */
5782 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5783 {
5784 /* At this point, we're looking for "type-name :: ~". The type-name
5785 must not be a class-name, since this is a pseudo-destructor. So,
5786 it must be either an enum-name, or a typedef-name -- both of which
5787 are just identifiers. So, we peek ahead to check that the "::"
5788 and "~" tokens are present; if they are not, then we can avoid
5789 calling type_name. */
5790 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5791 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5792 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5793 {
5794 cp_parser_error (parser, "non-scalar type");
5795 return;
5796 }
5797
5798 /* Look for the type-name. */
5799 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5800 if (*scope == error_mark_node)
5801 return;
5802
5803 /* Look for the `::' token. */
5804 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5805 }
5806 else
5807 *scope = NULL_TREE;
5808
5809 /* Look for the `~'. */
5810 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5811 /* Look for the type-name again. We are not responsible for
5812 checking that it matches the first type-name. */
5813 *type = cp_parser_nonclass_name (parser);
5814 }
5815
5816 /* Parse a unary-expression.
5817
5818 unary-expression:
5819 postfix-expression
5820 ++ cast-expression
5821 -- cast-expression
5822 unary-operator cast-expression
5823 sizeof unary-expression
5824 sizeof ( type-id )
5825 new-expression
5826 delete-expression
5827
5828 GNU Extensions:
5829
5830 unary-expression:
5831 __extension__ cast-expression
5832 __alignof__ unary-expression
5833 __alignof__ ( type-id )
5834 __real__ cast-expression
5835 __imag__ cast-expression
5836 && identifier
5837
5838 ADDRESS_P is true iff the unary-expression is appearing as the
5839 operand of the `&' operator. CAST_P is true if this expression is
5840 the target of a cast.
5841
5842 Returns a representation of the expression. */
5843
5844 static tree
5845 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5846 cp_id_kind * pidk)
5847 {
5848 cp_token *token;
5849 enum tree_code unary_operator;
5850
5851 /* Peek at the next token. */
5852 token = cp_lexer_peek_token (parser->lexer);
5853 /* Some keywords give away the kind of expression. */
5854 if (token->type == CPP_KEYWORD)
5855 {
5856 enum rid keyword = token->keyword;
5857
5858 switch (keyword)
5859 {
5860 case RID_ALIGNOF:
5861 case RID_SIZEOF:
5862 {
5863 tree operand;
5864 enum tree_code op;
5865
5866 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5867 /* Consume the token. */
5868 cp_lexer_consume_token (parser->lexer);
5869 /* Parse the operand. */
5870 operand = cp_parser_sizeof_operand (parser, keyword);
5871
5872 if (TYPE_P (operand))
5873 return cxx_sizeof_or_alignof_type (operand, op, true);
5874 else
5875 return cxx_sizeof_or_alignof_expr (operand, op, true);
5876 }
5877
5878 case RID_NEW:
5879 return cp_parser_new_expression (parser);
5880
5881 case RID_DELETE:
5882 return cp_parser_delete_expression (parser);
5883
5884 case RID_EXTENSION:
5885 {
5886 /* The saved value of the PEDANTIC flag. */
5887 int saved_pedantic;
5888 tree expr;
5889
5890 /* Save away the PEDANTIC flag. */
5891 cp_parser_extension_opt (parser, &saved_pedantic);
5892 /* Parse the cast-expression. */
5893 expr = cp_parser_simple_cast_expression (parser);
5894 /* Restore the PEDANTIC flag. */
5895 pedantic = saved_pedantic;
5896
5897 return expr;
5898 }
5899
5900 case RID_REALPART:
5901 case RID_IMAGPART:
5902 {
5903 tree expression;
5904
5905 /* Consume the `__real__' or `__imag__' token. */
5906 cp_lexer_consume_token (parser->lexer);
5907 /* Parse the cast-expression. */
5908 expression = cp_parser_simple_cast_expression (parser);
5909 /* Create the complete representation. */
5910 return build_x_unary_op ((keyword == RID_REALPART
5911 ? REALPART_EXPR : IMAGPART_EXPR),
5912 expression,
5913 tf_warning_or_error);
5914 }
5915 break;
5916
5917 case RID_NOEXCEPT:
5918 {
5919 tree expr;
5920 const char *saved_message;
5921 bool saved_integral_constant_expression_p;
5922 bool saved_non_integral_constant_expression_p;
5923 bool saved_greater_than_is_operator_p;
5924
5925 cp_lexer_consume_token (parser->lexer);
5926 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5927
5928 saved_message = parser->type_definition_forbidden_message;
5929 parser->type_definition_forbidden_message
5930 = G_("types may not be defined in %<noexcept%> expressions");
5931
5932 saved_integral_constant_expression_p
5933 = parser->integral_constant_expression_p;
5934 saved_non_integral_constant_expression_p
5935 = parser->non_integral_constant_expression_p;
5936 parser->integral_constant_expression_p = false;
5937
5938 saved_greater_than_is_operator_p
5939 = parser->greater_than_is_operator_p;
5940 parser->greater_than_is_operator_p = true;
5941
5942 ++cp_unevaluated_operand;
5943 ++c_inhibit_evaluation_warnings;
5944 expr = cp_parser_expression (parser, false, NULL);
5945 --c_inhibit_evaluation_warnings;
5946 --cp_unevaluated_operand;
5947
5948 parser->greater_than_is_operator_p
5949 = saved_greater_than_is_operator_p;
5950
5951 parser->integral_constant_expression_p
5952 = saved_integral_constant_expression_p;
5953 parser->non_integral_constant_expression_p
5954 = saved_non_integral_constant_expression_p;
5955
5956 parser->type_definition_forbidden_message = saved_message;
5957
5958 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5959 return finish_noexcept_expr (expr, tf_warning_or_error);
5960 }
5961
5962 default:
5963 break;
5964 }
5965 }
5966
5967 /* Look for the `:: new' and `:: delete', which also signal the
5968 beginning of a new-expression, or delete-expression,
5969 respectively. If the next token is `::', then it might be one of
5970 these. */
5971 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5972 {
5973 enum rid keyword;
5974
5975 /* See if the token after the `::' is one of the keywords in
5976 which we're interested. */
5977 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5978 /* If it's `new', we have a new-expression. */
5979 if (keyword == RID_NEW)
5980 return cp_parser_new_expression (parser);
5981 /* Similarly, for `delete'. */
5982 else if (keyword == RID_DELETE)
5983 return cp_parser_delete_expression (parser);
5984 }
5985
5986 /* Look for a unary operator. */
5987 unary_operator = cp_parser_unary_operator (token);
5988 /* The `++' and `--' operators can be handled similarly, even though
5989 they are not technically unary-operators in the grammar. */
5990 if (unary_operator == ERROR_MARK)
5991 {
5992 if (token->type == CPP_PLUS_PLUS)
5993 unary_operator = PREINCREMENT_EXPR;
5994 else if (token->type == CPP_MINUS_MINUS)
5995 unary_operator = PREDECREMENT_EXPR;
5996 /* Handle the GNU address-of-label extension. */
5997 else if (cp_parser_allow_gnu_extensions_p (parser)
5998 && token->type == CPP_AND_AND)
5999 {
6000 tree identifier;
6001 tree expression;
6002 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6003
6004 /* Consume the '&&' token. */
6005 cp_lexer_consume_token (parser->lexer);
6006 /* Look for the identifier. */
6007 identifier = cp_parser_identifier (parser);
6008 /* Create an expression representing the address. */
6009 expression = finish_label_address_expr (identifier, loc);
6010 if (cp_parser_non_integral_constant_expression (parser,
6011 NIC_ADDR_LABEL))
6012 expression = error_mark_node;
6013 return expression;
6014 }
6015 }
6016 if (unary_operator != ERROR_MARK)
6017 {
6018 tree cast_expression;
6019 tree expression = error_mark_node;
6020 non_integral_constant non_constant_p = NIC_NONE;
6021
6022 /* Consume the operator token. */
6023 token = cp_lexer_consume_token (parser->lexer);
6024 /* Parse the cast-expression. */
6025 cast_expression
6026 = cp_parser_cast_expression (parser,
6027 unary_operator == ADDR_EXPR,
6028 /*cast_p=*/false, pidk);
6029 /* Now, build an appropriate representation. */
6030 switch (unary_operator)
6031 {
6032 case INDIRECT_REF:
6033 non_constant_p = NIC_STAR;
6034 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6035 tf_warning_or_error);
6036 break;
6037
6038 case ADDR_EXPR:
6039 non_constant_p = NIC_ADDR;
6040 /* Fall through. */
6041 case BIT_NOT_EXPR:
6042 expression = build_x_unary_op (unary_operator, cast_expression,
6043 tf_warning_or_error);
6044 break;
6045
6046 case PREINCREMENT_EXPR:
6047 case PREDECREMENT_EXPR:
6048 non_constant_p = unary_operator == PREINCREMENT_EXPR
6049 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6050 /* Fall through. */
6051 case UNARY_PLUS_EXPR:
6052 case NEGATE_EXPR:
6053 case TRUTH_NOT_EXPR:
6054 expression = finish_unary_op_expr (unary_operator, cast_expression);
6055 break;
6056
6057 default:
6058 gcc_unreachable ();
6059 }
6060
6061 if (non_constant_p != NIC_NONE
6062 && cp_parser_non_integral_constant_expression (parser,
6063 non_constant_p))
6064 expression = error_mark_node;
6065
6066 return expression;
6067 }
6068
6069 return cp_parser_postfix_expression (parser, address_p, cast_p,
6070 /*member_access_only_p=*/false,
6071 pidk);
6072 }
6073
6074 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6075 unary-operator, the corresponding tree code is returned. */
6076
6077 static enum tree_code
6078 cp_parser_unary_operator (cp_token* token)
6079 {
6080 switch (token->type)
6081 {
6082 case CPP_MULT:
6083 return INDIRECT_REF;
6084
6085 case CPP_AND:
6086 return ADDR_EXPR;
6087
6088 case CPP_PLUS:
6089 return UNARY_PLUS_EXPR;
6090
6091 case CPP_MINUS:
6092 return NEGATE_EXPR;
6093
6094 case CPP_NOT:
6095 return TRUTH_NOT_EXPR;
6096
6097 case CPP_COMPL:
6098 return BIT_NOT_EXPR;
6099
6100 default:
6101 return ERROR_MARK;
6102 }
6103 }
6104
6105 /* Parse a new-expression.
6106
6107 new-expression:
6108 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6109 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6110
6111 Returns a representation of the expression. */
6112
6113 static tree
6114 cp_parser_new_expression (cp_parser* parser)
6115 {
6116 bool global_scope_p;
6117 VEC(tree,gc) *placement;
6118 tree type;
6119 VEC(tree,gc) *initializer;
6120 tree nelts;
6121 tree ret;
6122
6123 /* Look for the optional `::' operator. */
6124 global_scope_p
6125 = (cp_parser_global_scope_opt (parser,
6126 /*current_scope_valid_p=*/false)
6127 != NULL_TREE);
6128 /* Look for the `new' operator. */
6129 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6130 /* There's no easy way to tell a new-placement from the
6131 `( type-id )' construct. */
6132 cp_parser_parse_tentatively (parser);
6133 /* Look for a new-placement. */
6134 placement = cp_parser_new_placement (parser);
6135 /* If that didn't work out, there's no new-placement. */
6136 if (!cp_parser_parse_definitely (parser))
6137 {
6138 if (placement != NULL)
6139 release_tree_vector (placement);
6140 placement = NULL;
6141 }
6142
6143 /* If the next token is a `(', then we have a parenthesized
6144 type-id. */
6145 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6146 {
6147 cp_token *token;
6148 /* Consume the `('. */
6149 cp_lexer_consume_token (parser->lexer);
6150 /* Parse the type-id. */
6151 type = cp_parser_type_id (parser);
6152 /* Look for the closing `)'. */
6153 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6154 token = cp_lexer_peek_token (parser->lexer);
6155 /* There should not be a direct-new-declarator in this production,
6156 but GCC used to allowed this, so we check and emit a sensible error
6157 message for this case. */
6158 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6159 {
6160 error_at (token->location,
6161 "array bound forbidden after parenthesized type-id");
6162 inform (token->location,
6163 "try removing the parentheses around the type-id");
6164 cp_parser_direct_new_declarator (parser);
6165 }
6166 nelts = NULL_TREE;
6167 }
6168 /* Otherwise, there must be a new-type-id. */
6169 else
6170 type = cp_parser_new_type_id (parser, &nelts);
6171
6172 /* If the next token is a `(' or '{', then we have a new-initializer. */
6173 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6174 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6175 initializer = cp_parser_new_initializer (parser);
6176 else
6177 initializer = NULL;
6178
6179 /* A new-expression may not appear in an integral constant
6180 expression. */
6181 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6182 ret = error_mark_node;
6183 else
6184 {
6185 /* Create a representation of the new-expression. */
6186 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6187 tf_warning_or_error);
6188 }
6189
6190 if (placement != NULL)
6191 release_tree_vector (placement);
6192 if (initializer != NULL)
6193 release_tree_vector (initializer);
6194
6195 return ret;
6196 }
6197
6198 /* Parse a new-placement.
6199
6200 new-placement:
6201 ( expression-list )
6202
6203 Returns the same representation as for an expression-list. */
6204
6205 static VEC(tree,gc) *
6206 cp_parser_new_placement (cp_parser* parser)
6207 {
6208 VEC(tree,gc) *expression_list;
6209
6210 /* Parse the expression-list. */
6211 expression_list = (cp_parser_parenthesized_expression_list
6212 (parser, non_attr, /*cast_p=*/false,
6213 /*allow_expansion_p=*/true,
6214 /*non_constant_p=*/NULL));
6215
6216 return expression_list;
6217 }
6218
6219 /* Parse a new-type-id.
6220
6221 new-type-id:
6222 type-specifier-seq new-declarator [opt]
6223
6224 Returns the TYPE allocated. If the new-type-id indicates an array
6225 type, *NELTS is set to the number of elements in the last array
6226 bound; the TYPE will not include the last array bound. */
6227
6228 static tree
6229 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6230 {
6231 cp_decl_specifier_seq type_specifier_seq;
6232 cp_declarator *new_declarator;
6233 cp_declarator *declarator;
6234 cp_declarator *outer_declarator;
6235 const char *saved_message;
6236 tree type;
6237
6238 /* The type-specifier sequence must not contain type definitions.
6239 (It cannot contain declarations of new types either, but if they
6240 are not definitions we will catch that because they are not
6241 complete.) */
6242 saved_message = parser->type_definition_forbidden_message;
6243 parser->type_definition_forbidden_message
6244 = G_("types may not be defined in a new-type-id");
6245 /* Parse the type-specifier-seq. */
6246 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6247 /*is_trailing_return=*/false,
6248 &type_specifier_seq);
6249 /* Restore the old message. */
6250 parser->type_definition_forbidden_message = saved_message;
6251 /* Parse the new-declarator. */
6252 new_declarator = cp_parser_new_declarator_opt (parser);
6253
6254 /* Determine the number of elements in the last array dimension, if
6255 any. */
6256 *nelts = NULL_TREE;
6257 /* Skip down to the last array dimension. */
6258 declarator = new_declarator;
6259 outer_declarator = NULL;
6260 while (declarator && (declarator->kind == cdk_pointer
6261 || declarator->kind == cdk_ptrmem))
6262 {
6263 outer_declarator = declarator;
6264 declarator = declarator->declarator;
6265 }
6266 while (declarator
6267 && declarator->kind == cdk_array
6268 && declarator->declarator
6269 && declarator->declarator->kind == cdk_array)
6270 {
6271 outer_declarator = declarator;
6272 declarator = declarator->declarator;
6273 }
6274
6275 if (declarator && declarator->kind == cdk_array)
6276 {
6277 *nelts = declarator->u.array.bounds;
6278 if (*nelts == error_mark_node)
6279 *nelts = integer_one_node;
6280
6281 if (outer_declarator)
6282 outer_declarator->declarator = declarator->declarator;
6283 else
6284 new_declarator = NULL;
6285 }
6286
6287 type = groktypename (&type_specifier_seq, new_declarator, false);
6288 return type;
6289 }
6290
6291 /* Parse an (optional) new-declarator.
6292
6293 new-declarator:
6294 ptr-operator new-declarator [opt]
6295 direct-new-declarator
6296
6297 Returns the declarator. */
6298
6299 static cp_declarator *
6300 cp_parser_new_declarator_opt (cp_parser* parser)
6301 {
6302 enum tree_code code;
6303 tree type;
6304 cp_cv_quals cv_quals;
6305
6306 /* We don't know if there's a ptr-operator next, or not. */
6307 cp_parser_parse_tentatively (parser);
6308 /* Look for a ptr-operator. */
6309 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6310 /* If that worked, look for more new-declarators. */
6311 if (cp_parser_parse_definitely (parser))
6312 {
6313 cp_declarator *declarator;
6314
6315 /* Parse another optional declarator. */
6316 declarator = cp_parser_new_declarator_opt (parser);
6317
6318 return cp_parser_make_indirect_declarator
6319 (code, type, cv_quals, declarator);
6320 }
6321
6322 /* If the next token is a `[', there is a direct-new-declarator. */
6323 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6324 return cp_parser_direct_new_declarator (parser);
6325
6326 return NULL;
6327 }
6328
6329 /* Parse a direct-new-declarator.
6330
6331 direct-new-declarator:
6332 [ expression ]
6333 direct-new-declarator [constant-expression]
6334
6335 */
6336
6337 static cp_declarator *
6338 cp_parser_direct_new_declarator (cp_parser* parser)
6339 {
6340 cp_declarator *declarator = NULL;
6341
6342 while (true)
6343 {
6344 tree expression;
6345
6346 /* Look for the opening `['. */
6347 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6348 /* The first expression is not required to be constant. */
6349 if (!declarator)
6350 {
6351 cp_token *token = cp_lexer_peek_token (parser->lexer);
6352 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6353 /* The standard requires that the expression have integral
6354 type. DR 74 adds enumeration types. We believe that the
6355 real intent is that these expressions be handled like the
6356 expression in a `switch' condition, which also allows
6357 classes with a single conversion to integral or
6358 enumeration type. */
6359 if (!processing_template_decl)
6360 {
6361 expression
6362 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6363 expression,
6364 /*complain=*/true);
6365 if (!expression)
6366 {
6367 error_at (token->location,
6368 "expression in new-declarator must have integral "
6369 "or enumeration type");
6370 expression = error_mark_node;
6371 }
6372 }
6373 }
6374 /* But all the other expressions must be. */
6375 else
6376 expression
6377 = cp_parser_constant_expression (parser,
6378 /*allow_non_constant=*/false,
6379 NULL);
6380 /* Look for the closing `]'. */
6381 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6382
6383 /* Add this bound to the declarator. */
6384 declarator = make_array_declarator (declarator, expression);
6385
6386 /* If the next token is not a `[', then there are no more
6387 bounds. */
6388 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6389 break;
6390 }
6391
6392 return declarator;
6393 }
6394
6395 /* Parse a new-initializer.
6396
6397 new-initializer:
6398 ( expression-list [opt] )
6399 braced-init-list
6400
6401 Returns a representation of the expression-list. */
6402
6403 static VEC(tree,gc) *
6404 cp_parser_new_initializer (cp_parser* parser)
6405 {
6406 VEC(tree,gc) *expression_list;
6407
6408 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6409 {
6410 tree t;
6411 bool expr_non_constant_p;
6412 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6413 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6414 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6415 expression_list = make_tree_vector_single (t);
6416 }
6417 else
6418 expression_list = (cp_parser_parenthesized_expression_list
6419 (parser, non_attr, /*cast_p=*/false,
6420 /*allow_expansion_p=*/true,
6421 /*non_constant_p=*/NULL));
6422
6423 return expression_list;
6424 }
6425
6426 /* Parse a delete-expression.
6427
6428 delete-expression:
6429 :: [opt] delete cast-expression
6430 :: [opt] delete [ ] cast-expression
6431
6432 Returns a representation of the expression. */
6433
6434 static tree
6435 cp_parser_delete_expression (cp_parser* parser)
6436 {
6437 bool global_scope_p;
6438 bool array_p;
6439 tree expression;
6440
6441 /* Look for the optional `::' operator. */
6442 global_scope_p
6443 = (cp_parser_global_scope_opt (parser,
6444 /*current_scope_valid_p=*/false)
6445 != NULL_TREE);
6446 /* Look for the `delete' keyword. */
6447 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6448 /* See if the array syntax is in use. */
6449 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6450 {
6451 /* Consume the `[' token. */
6452 cp_lexer_consume_token (parser->lexer);
6453 /* Look for the `]' token. */
6454 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6455 /* Remember that this is the `[]' construct. */
6456 array_p = true;
6457 }
6458 else
6459 array_p = false;
6460
6461 /* Parse the cast-expression. */
6462 expression = cp_parser_simple_cast_expression (parser);
6463
6464 /* A delete-expression may not appear in an integral constant
6465 expression. */
6466 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6467 return error_mark_node;
6468
6469 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6470 }
6471
6472 /* Returns true if TOKEN may start a cast-expression and false
6473 otherwise. */
6474
6475 static bool
6476 cp_parser_token_starts_cast_expression (cp_token *token)
6477 {
6478 switch (token->type)
6479 {
6480 case CPP_COMMA:
6481 case CPP_SEMICOLON:
6482 case CPP_QUERY:
6483 case CPP_COLON:
6484 case CPP_CLOSE_SQUARE:
6485 case CPP_CLOSE_PAREN:
6486 case CPP_CLOSE_BRACE:
6487 case CPP_DOT:
6488 case CPP_DOT_STAR:
6489 case CPP_DEREF:
6490 case CPP_DEREF_STAR:
6491 case CPP_DIV:
6492 case CPP_MOD:
6493 case CPP_LSHIFT:
6494 case CPP_RSHIFT:
6495 case CPP_LESS:
6496 case CPP_GREATER:
6497 case CPP_LESS_EQ:
6498 case CPP_GREATER_EQ:
6499 case CPP_EQ_EQ:
6500 case CPP_NOT_EQ:
6501 case CPP_EQ:
6502 case CPP_MULT_EQ:
6503 case CPP_DIV_EQ:
6504 case CPP_MOD_EQ:
6505 case CPP_PLUS_EQ:
6506 case CPP_MINUS_EQ:
6507 case CPP_RSHIFT_EQ:
6508 case CPP_LSHIFT_EQ:
6509 case CPP_AND_EQ:
6510 case CPP_XOR_EQ:
6511 case CPP_OR_EQ:
6512 case CPP_XOR:
6513 case CPP_OR:
6514 case CPP_OR_OR:
6515 case CPP_EOF:
6516 return false;
6517
6518 /* '[' may start a primary-expression in obj-c++. */
6519 case CPP_OPEN_SQUARE:
6520 return c_dialect_objc ();
6521
6522 default:
6523 return true;
6524 }
6525 }
6526
6527 /* Parse a cast-expression.
6528
6529 cast-expression:
6530 unary-expression
6531 ( type-id ) cast-expression
6532
6533 ADDRESS_P is true iff the unary-expression is appearing as the
6534 operand of the `&' operator. CAST_P is true if this expression is
6535 the target of a cast.
6536
6537 Returns a representation of the expression. */
6538
6539 static tree
6540 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6541 cp_id_kind * pidk)
6542 {
6543 /* If it's a `(', then we might be looking at a cast. */
6544 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6545 {
6546 tree type = NULL_TREE;
6547 tree expr = NULL_TREE;
6548 bool compound_literal_p;
6549 const char *saved_message;
6550
6551 /* There's no way to know yet whether or not this is a cast.
6552 For example, `(int (3))' is a unary-expression, while `(int)
6553 3' is a cast. So, we resort to parsing tentatively. */
6554 cp_parser_parse_tentatively (parser);
6555 /* Types may not be defined in a cast. */
6556 saved_message = parser->type_definition_forbidden_message;
6557 parser->type_definition_forbidden_message
6558 = G_("types may not be defined in casts");
6559 /* Consume the `('. */
6560 cp_lexer_consume_token (parser->lexer);
6561 /* A very tricky bit is that `(struct S) { 3 }' is a
6562 compound-literal (which we permit in C++ as an extension).
6563 But, that construct is not a cast-expression -- it is a
6564 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6565 is legal; if the compound-literal were a cast-expression,
6566 you'd need an extra set of parentheses.) But, if we parse
6567 the type-id, and it happens to be a class-specifier, then we
6568 will commit to the parse at that point, because we cannot
6569 undo the action that is done when creating a new class. So,
6570 then we cannot back up and do a postfix-expression.
6571
6572 Therefore, we scan ahead to the closing `)', and check to see
6573 if the token after the `)' is a `{'. If so, we are not
6574 looking at a cast-expression.
6575
6576 Save tokens so that we can put them back. */
6577 cp_lexer_save_tokens (parser->lexer);
6578 /* Skip tokens until the next token is a closing parenthesis.
6579 If we find the closing `)', and the next token is a `{', then
6580 we are looking at a compound-literal. */
6581 compound_literal_p
6582 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6583 /*consume_paren=*/true)
6584 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6585 /* Roll back the tokens we skipped. */
6586 cp_lexer_rollback_tokens (parser->lexer);
6587 /* If we were looking at a compound-literal, simulate an error
6588 so that the call to cp_parser_parse_definitely below will
6589 fail. */
6590 if (compound_literal_p)
6591 cp_parser_simulate_error (parser);
6592 else
6593 {
6594 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6595 parser->in_type_id_in_expr_p = true;
6596 /* Look for the type-id. */
6597 type = cp_parser_type_id (parser);
6598 /* Look for the closing `)'. */
6599 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6600 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6601 }
6602
6603 /* Restore the saved message. */
6604 parser->type_definition_forbidden_message = saved_message;
6605
6606 /* At this point this can only be either a cast or a
6607 parenthesized ctor such as `(T ())' that looks like a cast to
6608 function returning T. */
6609 if (!cp_parser_error_occurred (parser)
6610 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6611 (parser->lexer)))
6612 {
6613 cp_parser_parse_definitely (parser);
6614 expr = cp_parser_cast_expression (parser,
6615 /*address_p=*/false,
6616 /*cast_p=*/true, pidk);
6617
6618 /* Warn about old-style casts, if so requested. */
6619 if (warn_old_style_cast
6620 && !in_system_header
6621 && !VOID_TYPE_P (type)
6622 && current_lang_name != lang_name_c)
6623 warning (OPT_Wold_style_cast, "use of old-style cast");
6624
6625 /* Only type conversions to integral or enumeration types
6626 can be used in constant-expressions. */
6627 if (!cast_valid_in_integral_constant_expression_p (type)
6628 && cp_parser_non_integral_constant_expression (parser,
6629 NIC_CAST))
6630 return error_mark_node;
6631
6632 /* Perform the cast. */
6633 expr = build_c_cast (input_location, type, expr);
6634 return expr;
6635 }
6636 else
6637 cp_parser_abort_tentative_parse (parser);
6638 }
6639
6640 /* If we get here, then it's not a cast, so it must be a
6641 unary-expression. */
6642 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6643 }
6644
6645 /* Parse a binary expression of the general form:
6646
6647 pm-expression:
6648 cast-expression
6649 pm-expression .* cast-expression
6650 pm-expression ->* cast-expression
6651
6652 multiplicative-expression:
6653 pm-expression
6654 multiplicative-expression * pm-expression
6655 multiplicative-expression / pm-expression
6656 multiplicative-expression % pm-expression
6657
6658 additive-expression:
6659 multiplicative-expression
6660 additive-expression + multiplicative-expression
6661 additive-expression - multiplicative-expression
6662
6663 shift-expression:
6664 additive-expression
6665 shift-expression << additive-expression
6666 shift-expression >> additive-expression
6667
6668 relational-expression:
6669 shift-expression
6670 relational-expression < shift-expression
6671 relational-expression > shift-expression
6672 relational-expression <= shift-expression
6673 relational-expression >= shift-expression
6674
6675 GNU Extension:
6676
6677 relational-expression:
6678 relational-expression <? shift-expression
6679 relational-expression >? shift-expression
6680
6681 equality-expression:
6682 relational-expression
6683 equality-expression == relational-expression
6684 equality-expression != relational-expression
6685
6686 and-expression:
6687 equality-expression
6688 and-expression & equality-expression
6689
6690 exclusive-or-expression:
6691 and-expression
6692 exclusive-or-expression ^ and-expression
6693
6694 inclusive-or-expression:
6695 exclusive-or-expression
6696 inclusive-or-expression | exclusive-or-expression
6697
6698 logical-and-expression:
6699 inclusive-or-expression
6700 logical-and-expression && inclusive-or-expression
6701
6702 logical-or-expression:
6703 logical-and-expression
6704 logical-or-expression || logical-and-expression
6705
6706 All these are implemented with a single function like:
6707
6708 binary-expression:
6709 simple-cast-expression
6710 binary-expression <token> binary-expression
6711
6712 CAST_P is true if this expression is the target of a cast.
6713
6714 The binops_by_token map is used to get the tree codes for each <token> type.
6715 binary-expressions are associated according to a precedence table. */
6716
6717 #define TOKEN_PRECEDENCE(token) \
6718 (((token->type == CPP_GREATER \
6719 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6720 && !parser->greater_than_is_operator_p) \
6721 ? PREC_NOT_OPERATOR \
6722 : binops_by_token[token->type].prec)
6723
6724 static tree
6725 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6726 bool no_toplevel_fold_p,
6727 enum cp_parser_prec prec,
6728 cp_id_kind * pidk)
6729 {
6730 cp_parser_expression_stack stack;
6731 cp_parser_expression_stack_entry *sp = &stack[0];
6732 tree lhs, rhs;
6733 cp_token *token;
6734 enum tree_code tree_type, lhs_type, rhs_type;
6735 enum cp_parser_prec new_prec, lookahead_prec;
6736 bool overloaded_p;
6737
6738 /* Parse the first expression. */
6739 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6740 lhs_type = ERROR_MARK;
6741
6742 for (;;)
6743 {
6744 /* Get an operator token. */
6745 token = cp_lexer_peek_token (parser->lexer);
6746
6747 if (warn_cxx0x_compat
6748 && token->type == CPP_RSHIFT
6749 && !parser->greater_than_is_operator_p)
6750 {
6751 if (warning_at (token->location, OPT_Wc__0x_compat,
6752 "%<>>%> operator will be treated as"
6753 " two right angle brackets in C++0x"))
6754 inform (token->location,
6755 "suggest parentheses around %<>>%> expression");
6756 }
6757
6758 new_prec = TOKEN_PRECEDENCE (token);
6759
6760 /* Popping an entry off the stack means we completed a subexpression:
6761 - either we found a token which is not an operator (`>' where it is not
6762 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6763 will happen repeatedly;
6764 - or, we found an operator which has lower priority. This is the case
6765 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6766 parsing `3 * 4'. */
6767 if (new_prec <= prec)
6768 {
6769 if (sp == stack)
6770 break;
6771 else
6772 goto pop;
6773 }
6774
6775 get_rhs:
6776 tree_type = binops_by_token[token->type].tree_type;
6777
6778 /* We used the operator token. */
6779 cp_lexer_consume_token (parser->lexer);
6780
6781 /* For "false && x" or "true || x", x will never be executed;
6782 disable warnings while evaluating it. */
6783 if (tree_type == TRUTH_ANDIF_EXPR)
6784 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6785 else if (tree_type == TRUTH_ORIF_EXPR)
6786 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6787
6788 /* Extract another operand. It may be the RHS of this expression
6789 or the LHS of a new, higher priority expression. */
6790 rhs = cp_parser_simple_cast_expression (parser);
6791 rhs_type = ERROR_MARK;
6792
6793 /* Get another operator token. Look up its precedence to avoid
6794 building a useless (immediately popped) stack entry for common
6795 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6796 token = cp_lexer_peek_token (parser->lexer);
6797 lookahead_prec = TOKEN_PRECEDENCE (token);
6798 if (lookahead_prec > new_prec)
6799 {
6800 /* ... and prepare to parse the RHS of the new, higher priority
6801 expression. Since precedence levels on the stack are
6802 monotonically increasing, we do not have to care about
6803 stack overflows. */
6804 sp->prec = prec;
6805 sp->tree_type = tree_type;
6806 sp->lhs = lhs;
6807 sp->lhs_type = lhs_type;
6808 sp++;
6809 lhs = rhs;
6810 lhs_type = rhs_type;
6811 prec = new_prec;
6812 new_prec = lookahead_prec;
6813 goto get_rhs;
6814
6815 pop:
6816 lookahead_prec = new_prec;
6817 /* If the stack is not empty, we have parsed into LHS the right side
6818 (`4' in the example above) of an expression we had suspended.
6819 We can use the information on the stack to recover the LHS (`3')
6820 from the stack together with the tree code (`MULT_EXPR'), and
6821 the precedence of the higher level subexpression
6822 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6823 which will be used to actually build the additive expression. */
6824 --sp;
6825 prec = sp->prec;
6826 tree_type = sp->tree_type;
6827 rhs = lhs;
6828 rhs_type = lhs_type;
6829 lhs = sp->lhs;
6830 lhs_type = sp->lhs_type;
6831 }
6832
6833 /* Undo the disabling of warnings done above. */
6834 if (tree_type == TRUTH_ANDIF_EXPR)
6835 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6836 else if (tree_type == TRUTH_ORIF_EXPR)
6837 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6838
6839 overloaded_p = false;
6840 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6841 ERROR_MARK for everything that is not a binary expression.
6842 This makes warn_about_parentheses miss some warnings that
6843 involve unary operators. For unary expressions we should
6844 pass the correct tree_code unless the unary expression was
6845 surrounded by parentheses.
6846 */
6847 if (no_toplevel_fold_p
6848 && lookahead_prec <= prec
6849 && sp == stack
6850 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6851 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6852 else
6853 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6854 &overloaded_p, tf_warning_or_error);
6855 lhs_type = tree_type;
6856
6857 /* If the binary operator required the use of an overloaded operator,
6858 then this expression cannot be an integral constant-expression.
6859 An overloaded operator can be used even if both operands are
6860 otherwise permissible in an integral constant-expression if at
6861 least one of the operands is of enumeration type. */
6862
6863 if (overloaded_p
6864 && cp_parser_non_integral_constant_expression (parser,
6865 NIC_OVERLOADED))
6866 return error_mark_node;
6867 }
6868
6869 return lhs;
6870 }
6871
6872
6873 /* Parse the `? expression : assignment-expression' part of a
6874 conditional-expression. The LOGICAL_OR_EXPR is the
6875 logical-or-expression that started the conditional-expression.
6876 Returns a representation of the entire conditional-expression.
6877
6878 This routine is used by cp_parser_assignment_expression.
6879
6880 ? expression : assignment-expression
6881
6882 GNU Extensions:
6883
6884 ? : assignment-expression */
6885
6886 static tree
6887 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6888 {
6889 tree expr;
6890 tree assignment_expr;
6891 struct cp_token *token;
6892
6893 /* Consume the `?' token. */
6894 cp_lexer_consume_token (parser->lexer);
6895 token = cp_lexer_peek_token (parser->lexer);
6896 if (cp_parser_allow_gnu_extensions_p (parser)
6897 && token->type == CPP_COLON)
6898 {
6899 pedwarn (token->location, OPT_pedantic,
6900 "ISO C++ does not allow ?: with omitted middle operand");
6901 /* Implicit true clause. */
6902 expr = NULL_TREE;
6903 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6904 warn_for_omitted_condop (token->location, logical_or_expr);
6905 }
6906 else
6907 {
6908 /* Parse the expression. */
6909 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6910 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6911 c_inhibit_evaluation_warnings +=
6912 ((logical_or_expr == truthvalue_true_node)
6913 - (logical_or_expr == truthvalue_false_node));
6914 }
6915
6916 /* The next token should be a `:'. */
6917 cp_parser_require (parser, CPP_COLON, RT_COLON);
6918 /* Parse the assignment-expression. */
6919 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6920 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6921
6922 /* Build the conditional-expression. */
6923 return build_x_conditional_expr (logical_or_expr,
6924 expr,
6925 assignment_expr,
6926 tf_warning_or_error);
6927 }
6928
6929 /* Parse an assignment-expression.
6930
6931 assignment-expression:
6932 conditional-expression
6933 logical-or-expression assignment-operator assignment_expression
6934 throw-expression
6935
6936 CAST_P is true if this expression is the target of a cast.
6937
6938 Returns a representation for the expression. */
6939
6940 static tree
6941 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6942 cp_id_kind * pidk)
6943 {
6944 tree expr;
6945
6946 /* If the next token is the `throw' keyword, then we're looking at
6947 a throw-expression. */
6948 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6949 expr = cp_parser_throw_expression (parser);
6950 /* Otherwise, it must be that we are looking at a
6951 logical-or-expression. */
6952 else
6953 {
6954 /* Parse the binary expressions (logical-or-expression). */
6955 expr = cp_parser_binary_expression (parser, cast_p, false,
6956 PREC_NOT_OPERATOR, pidk);
6957 /* If the next token is a `?' then we're actually looking at a
6958 conditional-expression. */
6959 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6960 return cp_parser_question_colon_clause (parser, expr);
6961 else
6962 {
6963 enum tree_code assignment_operator;
6964
6965 /* If it's an assignment-operator, we're using the second
6966 production. */
6967 assignment_operator
6968 = cp_parser_assignment_operator_opt (parser);
6969 if (assignment_operator != ERROR_MARK)
6970 {
6971 bool non_constant_p;
6972
6973 /* Parse the right-hand side of the assignment. */
6974 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6975
6976 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6977 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6978
6979 /* An assignment may not appear in a
6980 constant-expression. */
6981 if (cp_parser_non_integral_constant_expression (parser,
6982 NIC_ASSIGNMENT))
6983 return error_mark_node;
6984 /* Build the assignment expression. */
6985 expr = build_x_modify_expr (expr,
6986 assignment_operator,
6987 rhs,
6988 tf_warning_or_error);
6989 }
6990 }
6991 }
6992
6993 return expr;
6994 }
6995
6996 /* Parse an (optional) assignment-operator.
6997
6998 assignment-operator: one of
6999 = *= /= %= += -= >>= <<= &= ^= |=
7000
7001 GNU Extension:
7002
7003 assignment-operator: one of
7004 <?= >?=
7005
7006 If the next token is an assignment operator, the corresponding tree
7007 code is returned, and the token is consumed. For example, for
7008 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7009 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7010 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7011 operator, ERROR_MARK is returned. */
7012
7013 static enum tree_code
7014 cp_parser_assignment_operator_opt (cp_parser* parser)
7015 {
7016 enum tree_code op;
7017 cp_token *token;
7018
7019 /* Peek at the next token. */
7020 token = cp_lexer_peek_token (parser->lexer);
7021
7022 switch (token->type)
7023 {
7024 case CPP_EQ:
7025 op = NOP_EXPR;
7026 break;
7027
7028 case CPP_MULT_EQ:
7029 op = MULT_EXPR;
7030 break;
7031
7032 case CPP_DIV_EQ:
7033 op = TRUNC_DIV_EXPR;
7034 break;
7035
7036 case CPP_MOD_EQ:
7037 op = TRUNC_MOD_EXPR;
7038 break;
7039
7040 case CPP_PLUS_EQ:
7041 op = PLUS_EXPR;
7042 break;
7043
7044 case CPP_MINUS_EQ:
7045 op = MINUS_EXPR;
7046 break;
7047
7048 case CPP_RSHIFT_EQ:
7049 op = RSHIFT_EXPR;
7050 break;
7051
7052 case CPP_LSHIFT_EQ:
7053 op = LSHIFT_EXPR;
7054 break;
7055
7056 case CPP_AND_EQ:
7057 op = BIT_AND_EXPR;
7058 break;
7059
7060 case CPP_XOR_EQ:
7061 op = BIT_XOR_EXPR;
7062 break;
7063
7064 case CPP_OR_EQ:
7065 op = BIT_IOR_EXPR;
7066 break;
7067
7068 default:
7069 /* Nothing else is an assignment operator. */
7070 op = ERROR_MARK;
7071 }
7072
7073 /* If it was an assignment operator, consume it. */
7074 if (op != ERROR_MARK)
7075 cp_lexer_consume_token (parser->lexer);
7076
7077 return op;
7078 }
7079
7080 /* Parse an expression.
7081
7082 expression:
7083 assignment-expression
7084 expression , assignment-expression
7085
7086 CAST_P is true if this expression is the target of a cast.
7087
7088 Returns a representation of the expression. */
7089
7090 static tree
7091 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7092 {
7093 tree expression = NULL_TREE;
7094
7095 while (true)
7096 {
7097 tree assignment_expression;
7098
7099 /* Parse the next assignment-expression. */
7100 assignment_expression
7101 = cp_parser_assignment_expression (parser, cast_p, pidk);
7102 /* If this is the first assignment-expression, we can just
7103 save it away. */
7104 if (!expression)
7105 expression = assignment_expression;
7106 else
7107 expression = build_x_compound_expr (expression,
7108 assignment_expression,
7109 tf_warning_or_error);
7110 /* If the next token is not a comma, then we are done with the
7111 expression. */
7112 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7113 break;
7114 /* Consume the `,'. */
7115 cp_lexer_consume_token (parser->lexer);
7116 /* A comma operator cannot appear in a constant-expression. */
7117 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7118 expression = error_mark_node;
7119 }
7120
7121 return expression;
7122 }
7123
7124 /* Parse a constant-expression.
7125
7126 constant-expression:
7127 conditional-expression
7128
7129 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7130 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7131 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7132 is false, NON_CONSTANT_P should be NULL. */
7133
7134 static tree
7135 cp_parser_constant_expression (cp_parser* parser,
7136 bool allow_non_constant_p,
7137 bool *non_constant_p)
7138 {
7139 bool saved_integral_constant_expression_p;
7140 bool saved_allow_non_integral_constant_expression_p;
7141 bool saved_non_integral_constant_expression_p;
7142 tree expression;
7143
7144 /* It might seem that we could simply parse the
7145 conditional-expression, and then check to see if it were
7146 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7147 one that the compiler can figure out is constant, possibly after
7148 doing some simplifications or optimizations. The standard has a
7149 precise definition of constant-expression, and we must honor
7150 that, even though it is somewhat more restrictive.
7151
7152 For example:
7153
7154 int i[(2, 3)];
7155
7156 is not a legal declaration, because `(2, 3)' is not a
7157 constant-expression. The `,' operator is forbidden in a
7158 constant-expression. However, GCC's constant-folding machinery
7159 will fold this operation to an INTEGER_CST for `3'. */
7160
7161 /* Save the old settings. */
7162 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7163 saved_allow_non_integral_constant_expression_p
7164 = parser->allow_non_integral_constant_expression_p;
7165 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7166 /* We are now parsing a constant-expression. */
7167 parser->integral_constant_expression_p = true;
7168 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
7169 parser->non_integral_constant_expression_p = false;
7170 /* Although the grammar says "conditional-expression", we parse an
7171 "assignment-expression", which also permits "throw-expression"
7172 and the use of assignment operators. In the case that
7173 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7174 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7175 actually essential that we look for an assignment-expression.
7176 For example, cp_parser_initializer_clauses uses this function to
7177 determine whether a particular assignment-expression is in fact
7178 constant. */
7179 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7180 /* Restore the old settings. */
7181 parser->integral_constant_expression_p
7182 = saved_integral_constant_expression_p;
7183 parser->allow_non_integral_constant_expression_p
7184 = saved_allow_non_integral_constant_expression_p;
7185 if (allow_non_constant_p)
7186 *non_constant_p = parser->non_integral_constant_expression_p;
7187 else if (parser->non_integral_constant_expression_p)
7188 expression = error_mark_node;
7189 parser->non_integral_constant_expression_p
7190 = saved_non_integral_constant_expression_p;
7191
7192 return expression;
7193 }
7194
7195 /* Parse __builtin_offsetof.
7196
7197 offsetof-expression:
7198 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7199
7200 offsetof-member-designator:
7201 id-expression
7202 | offsetof-member-designator "." id-expression
7203 | offsetof-member-designator "[" expression "]"
7204 | offsetof-member-designator "->" id-expression */
7205
7206 static tree
7207 cp_parser_builtin_offsetof (cp_parser *parser)
7208 {
7209 int save_ice_p, save_non_ice_p;
7210 tree type, expr;
7211 cp_id_kind dummy;
7212 cp_token *token;
7213
7214 /* We're about to accept non-integral-constant things, but will
7215 definitely yield an integral constant expression. Save and
7216 restore these values around our local parsing. */
7217 save_ice_p = parser->integral_constant_expression_p;
7218 save_non_ice_p = parser->non_integral_constant_expression_p;
7219
7220 /* Consume the "__builtin_offsetof" token. */
7221 cp_lexer_consume_token (parser->lexer);
7222 /* Consume the opening `('. */
7223 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7224 /* Parse the type-id. */
7225 type = cp_parser_type_id (parser);
7226 /* Look for the `,'. */
7227 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7228 token = cp_lexer_peek_token (parser->lexer);
7229
7230 /* Build the (type *)null that begins the traditional offsetof macro. */
7231 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7232 tf_warning_or_error);
7233
7234 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7235 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7236 true, &dummy, token->location);
7237 while (true)
7238 {
7239 token = cp_lexer_peek_token (parser->lexer);
7240 switch (token->type)
7241 {
7242 case CPP_OPEN_SQUARE:
7243 /* offsetof-member-designator "[" expression "]" */
7244 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7245 break;
7246
7247 case CPP_DEREF:
7248 /* offsetof-member-designator "->" identifier */
7249 expr = grok_array_decl (expr, integer_zero_node);
7250 /* FALLTHRU */
7251
7252 case CPP_DOT:
7253 /* offsetof-member-designator "." identifier */
7254 cp_lexer_consume_token (parser->lexer);
7255 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7256 expr, true, &dummy,
7257 token->location);
7258 break;
7259
7260 case CPP_CLOSE_PAREN:
7261 /* Consume the ")" token. */
7262 cp_lexer_consume_token (parser->lexer);
7263 goto success;
7264
7265 default:
7266 /* Error. We know the following require will fail, but
7267 that gives the proper error message. */
7268 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7269 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7270 expr = error_mark_node;
7271 goto failure;
7272 }
7273 }
7274
7275 success:
7276 /* If we're processing a template, we can't finish the semantics yet.
7277 Otherwise we can fold the entire expression now. */
7278 if (processing_template_decl)
7279 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7280 else
7281 expr = finish_offsetof (expr);
7282
7283 failure:
7284 parser->integral_constant_expression_p = save_ice_p;
7285 parser->non_integral_constant_expression_p = save_non_ice_p;
7286
7287 return expr;
7288 }
7289
7290 /* Parse a trait expression. */
7291
7292 static tree
7293 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7294 {
7295 cp_trait_kind kind;
7296 tree type1, type2 = NULL_TREE;
7297 bool binary = false;
7298 cp_decl_specifier_seq decl_specs;
7299
7300 switch (keyword)
7301 {
7302 case RID_HAS_NOTHROW_ASSIGN:
7303 kind = CPTK_HAS_NOTHROW_ASSIGN;
7304 break;
7305 case RID_HAS_NOTHROW_CONSTRUCTOR:
7306 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7307 break;
7308 case RID_HAS_NOTHROW_COPY:
7309 kind = CPTK_HAS_NOTHROW_COPY;
7310 break;
7311 case RID_HAS_TRIVIAL_ASSIGN:
7312 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7313 break;
7314 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7315 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7316 break;
7317 case RID_HAS_TRIVIAL_COPY:
7318 kind = CPTK_HAS_TRIVIAL_COPY;
7319 break;
7320 case RID_HAS_TRIVIAL_DESTRUCTOR:
7321 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7322 break;
7323 case RID_HAS_VIRTUAL_DESTRUCTOR:
7324 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7325 break;
7326 case RID_IS_ABSTRACT:
7327 kind = CPTK_IS_ABSTRACT;
7328 break;
7329 case RID_IS_BASE_OF:
7330 kind = CPTK_IS_BASE_OF;
7331 binary = true;
7332 break;
7333 case RID_IS_CLASS:
7334 kind = CPTK_IS_CLASS;
7335 break;
7336 case RID_IS_CONVERTIBLE_TO:
7337 kind = CPTK_IS_CONVERTIBLE_TO;
7338 binary = true;
7339 break;
7340 case RID_IS_EMPTY:
7341 kind = CPTK_IS_EMPTY;
7342 break;
7343 case RID_IS_ENUM:
7344 kind = CPTK_IS_ENUM;
7345 break;
7346 case RID_IS_POD:
7347 kind = CPTK_IS_POD;
7348 break;
7349 case RID_IS_POLYMORPHIC:
7350 kind = CPTK_IS_POLYMORPHIC;
7351 break;
7352 case RID_IS_STD_LAYOUT:
7353 kind = CPTK_IS_STD_LAYOUT;
7354 break;
7355 case RID_IS_TRIVIAL:
7356 kind = CPTK_IS_TRIVIAL;
7357 break;
7358 case RID_IS_UNION:
7359 kind = CPTK_IS_UNION;
7360 break;
7361 default:
7362 gcc_unreachable ();
7363 }
7364
7365 /* Consume the token. */
7366 cp_lexer_consume_token (parser->lexer);
7367
7368 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7369
7370 type1 = cp_parser_type_id (parser);
7371
7372 if (type1 == error_mark_node)
7373 return error_mark_node;
7374
7375 /* Build a trivial decl-specifier-seq. */
7376 clear_decl_specs (&decl_specs);
7377 decl_specs.type = type1;
7378
7379 /* Call grokdeclarator to figure out what type this is. */
7380 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7381 /*initialized=*/0, /*attrlist=*/NULL);
7382
7383 if (binary)
7384 {
7385 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7386
7387 type2 = cp_parser_type_id (parser);
7388
7389 if (type2 == error_mark_node)
7390 return error_mark_node;
7391
7392 /* Build a trivial decl-specifier-seq. */
7393 clear_decl_specs (&decl_specs);
7394 decl_specs.type = type2;
7395
7396 /* Call grokdeclarator to figure out what type this is. */
7397 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7398 /*initialized=*/0, /*attrlist=*/NULL);
7399 }
7400
7401 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7402
7403 /* Complete the trait expression, which may mean either processing
7404 the trait expr now or saving it for template instantiation. */
7405 return finish_trait_expr (kind, type1, type2);
7406 }
7407
7408 /* Lambdas that appear in variable initializer or default argument scope
7409 get that in their mangling, so we need to record it. We might as well
7410 use the count for function and namespace scopes as well. */
7411 static GTY(()) tree lambda_scope;
7412 static GTY(()) int lambda_count;
7413 typedef struct GTY(()) tree_int
7414 {
7415 tree t;
7416 int i;
7417 } tree_int;
7418 DEF_VEC_O(tree_int);
7419 DEF_VEC_ALLOC_O(tree_int,gc);
7420 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7421
7422 static void
7423 start_lambda_scope (tree decl)
7424 {
7425 tree_int ti;
7426 gcc_assert (decl);
7427 /* Once we're inside a function, we ignore other scopes and just push
7428 the function again so that popping works properly. */
7429 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7430 decl = current_function_decl;
7431 ti.t = lambda_scope;
7432 ti.i = lambda_count;
7433 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7434 if (lambda_scope != decl)
7435 {
7436 /* Don't reset the count if we're still in the same function. */
7437 lambda_scope = decl;
7438 lambda_count = 0;
7439 }
7440 }
7441
7442 static void
7443 record_lambda_scope (tree lambda)
7444 {
7445 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7446 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7447 }
7448
7449 static void
7450 finish_lambda_scope (void)
7451 {
7452 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7453 if (lambda_scope != p->t)
7454 {
7455 lambda_scope = p->t;
7456 lambda_count = p->i;
7457 }
7458 VEC_pop (tree_int, lambda_scope_stack);
7459 }
7460
7461 /* Parse a lambda expression.
7462
7463 lambda-expression:
7464 lambda-introducer lambda-declarator [opt] compound-statement
7465
7466 Returns a representation of the expression. */
7467
7468 static tree
7469 cp_parser_lambda_expression (cp_parser* parser)
7470 {
7471 tree lambda_expr = build_lambda_expr ();
7472 tree type;
7473
7474 LAMBDA_EXPR_LOCATION (lambda_expr)
7475 = cp_lexer_peek_token (parser->lexer)->location;
7476
7477 if (cp_unevaluated_operand)
7478 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7479 "lambda-expression in unevaluated context");
7480
7481 /* We may be in the middle of deferred access check. Disable
7482 it now. */
7483 push_deferring_access_checks (dk_no_deferred);
7484
7485 cp_parser_lambda_introducer (parser, lambda_expr);
7486
7487 type = begin_lambda_type (lambda_expr);
7488
7489 record_lambda_scope (lambda_expr);
7490
7491 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7492 determine_visibility (TYPE_NAME (type));
7493
7494 /* Now that we've started the type, add the capture fields for any
7495 explicit captures. */
7496 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7497
7498 {
7499 /* Inside the class, surrounding template-parameter-lists do not apply. */
7500 unsigned int saved_num_template_parameter_lists
7501 = parser->num_template_parameter_lists;
7502
7503 parser->num_template_parameter_lists = 0;
7504
7505 /* By virtue of defining a local class, a lambda expression has access to
7506 the private variables of enclosing classes. */
7507
7508 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7509
7510 cp_parser_lambda_body (parser, lambda_expr);
7511
7512 /* The capture list was built up in reverse order; fix that now. */
7513 {
7514 tree newlist = NULL_TREE;
7515 tree elt, next;
7516
7517 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7518 elt; elt = next)
7519 {
7520 tree field = TREE_PURPOSE (elt);
7521 char *buf;
7522
7523 next = TREE_CHAIN (elt);
7524 TREE_CHAIN (elt) = newlist;
7525 newlist = elt;
7526
7527 /* Also add __ to the beginning of the field name so that code
7528 outside the lambda body can't see the captured name. We could
7529 just remove the name entirely, but this is more useful for
7530 debugging. */
7531 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7532 /* The 'this' capture already starts with __. */
7533 continue;
7534
7535 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7536 buf[1] = buf[0] = '_';
7537 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7538 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7539 DECL_NAME (field) = get_identifier (buf);
7540 }
7541 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7542 }
7543
7544 maybe_add_lambda_conv_op (type);
7545
7546 type = finish_struct (type, /*attributes=*/NULL_TREE);
7547
7548 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7549 }
7550
7551 pop_deferring_access_checks ();
7552
7553 return build_lambda_object (lambda_expr);
7554 }
7555
7556 /* Parse the beginning of a lambda expression.
7557
7558 lambda-introducer:
7559 [ lambda-capture [opt] ]
7560
7561 LAMBDA_EXPR is the current representation of the lambda expression. */
7562
7563 static void
7564 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7565 {
7566 /* Need commas after the first capture. */
7567 bool first = true;
7568
7569 /* Eat the leading `['. */
7570 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7571
7572 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7573 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7574 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7575 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7576 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7577 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7578
7579 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7580 {
7581 cp_lexer_consume_token (parser->lexer);
7582 first = false;
7583 }
7584
7585 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7586 {
7587 cp_token* capture_token;
7588 tree capture_id;
7589 tree capture_init_expr;
7590 cp_id_kind idk = CP_ID_KIND_NONE;
7591 bool explicit_init_p = false;
7592
7593 enum capture_kind_type
7594 {
7595 BY_COPY,
7596 BY_REFERENCE
7597 };
7598 enum capture_kind_type capture_kind = BY_COPY;
7599
7600 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7601 {
7602 error ("expected end of capture-list");
7603 return;
7604 }
7605
7606 if (first)
7607 first = false;
7608 else
7609 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7610
7611 /* Possibly capture `this'. */
7612 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7613 {
7614 cp_lexer_consume_token (parser->lexer);
7615 add_capture (lambda_expr,
7616 /*id=*/get_identifier ("__this"),
7617 /*initializer=*/finish_this_expr(),
7618 /*by_reference_p=*/false,
7619 explicit_init_p);
7620 continue;
7621 }
7622
7623 /* Remember whether we want to capture as a reference or not. */
7624 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7625 {
7626 capture_kind = BY_REFERENCE;
7627 cp_lexer_consume_token (parser->lexer);
7628 }
7629
7630 /* Get the identifier. */
7631 capture_token = cp_lexer_peek_token (parser->lexer);
7632 capture_id = cp_parser_identifier (parser);
7633
7634 if (capture_id == error_mark_node)
7635 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7636 delimiters, but I modified this to stop on unnested ']' as well. It
7637 was already changed to stop on unnested '}', so the
7638 "closing_parenthesis" name is no more misleading with my change. */
7639 {
7640 cp_parser_skip_to_closing_parenthesis (parser,
7641 /*recovering=*/true,
7642 /*or_comma=*/true,
7643 /*consume_paren=*/true);
7644 break;
7645 }
7646
7647 /* Find the initializer for this capture. */
7648 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7649 {
7650 /* An explicit expression exists. */
7651 cp_lexer_consume_token (parser->lexer);
7652 pedwarn (input_location, OPT_pedantic,
7653 "ISO C++ does not allow initializers "
7654 "in lambda expression capture lists");
7655 capture_init_expr = cp_parser_assignment_expression (parser,
7656 /*cast_p=*/true,
7657 &idk);
7658 explicit_init_p = true;
7659 }
7660 else
7661 {
7662 const char* error_msg;
7663
7664 /* Turn the identifier into an id-expression. */
7665 capture_init_expr
7666 = cp_parser_lookup_name
7667 (parser,
7668 capture_id,
7669 none_type,
7670 /*is_template=*/false,
7671 /*is_namespace=*/false,
7672 /*check_dependency=*/true,
7673 /*ambiguous_decls=*/NULL,
7674 capture_token->location);
7675
7676 capture_init_expr
7677 = finish_id_expression
7678 (capture_id,
7679 capture_init_expr,
7680 parser->scope,
7681 &idk,
7682 /*integral_constant_expression_p=*/false,
7683 /*allow_non_integral_constant_expression_p=*/false,
7684 /*non_integral_constant_expression_p=*/NULL,
7685 /*template_p=*/false,
7686 /*done=*/true,
7687 /*address_p=*/false,
7688 /*template_arg_p=*/false,
7689 &error_msg,
7690 capture_token->location);
7691 }
7692
7693 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7694 capture_init_expr
7695 = unqualified_name_lookup_error (capture_init_expr);
7696
7697 add_capture (lambda_expr,
7698 capture_id,
7699 capture_init_expr,
7700 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7701 explicit_init_p);
7702 }
7703
7704 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7705 }
7706
7707 /* Parse the (optional) middle of a lambda expression.
7708
7709 lambda-declarator:
7710 ( parameter-declaration-clause [opt] )
7711 attribute-specifier [opt]
7712 mutable [opt]
7713 exception-specification [opt]
7714 lambda-return-type-clause [opt]
7715
7716 LAMBDA_EXPR is the current representation of the lambda expression. */
7717
7718 static void
7719 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7720 {
7721 /* 5.1.1.4 of the standard says:
7722 If a lambda-expression does not include a lambda-declarator, it is as if
7723 the lambda-declarator were ().
7724 This means an empty parameter list, no attributes, and no exception
7725 specification. */
7726 tree param_list = void_list_node;
7727 tree attributes = NULL_TREE;
7728 tree exception_spec = NULL_TREE;
7729 tree t;
7730
7731 /* The lambda-declarator is optional, but must begin with an opening
7732 parenthesis if present. */
7733 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7734 {
7735 cp_lexer_consume_token (parser->lexer);
7736
7737 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7738
7739 /* Parse parameters. */
7740 param_list = cp_parser_parameter_declaration_clause (parser);
7741
7742 /* Default arguments shall not be specified in the
7743 parameter-declaration-clause of a lambda-declarator. */
7744 for (t = param_list; t; t = TREE_CHAIN (t))
7745 if (TREE_PURPOSE (t))
7746 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7747 "default argument specified for lambda parameter");
7748
7749 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7750
7751 attributes = cp_parser_attributes_opt (parser);
7752
7753 /* Parse optional `mutable' keyword. */
7754 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7755 {
7756 cp_lexer_consume_token (parser->lexer);
7757 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7758 }
7759
7760 /* Parse optional exception specification. */
7761 exception_spec = cp_parser_exception_specification_opt (parser);
7762
7763 /* Parse optional trailing return type. */
7764 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7765 {
7766 cp_lexer_consume_token (parser->lexer);
7767 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7768 }
7769
7770 /* The function parameters must be in scope all the way until after the
7771 trailing-return-type in case of decltype. */
7772 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7773 pop_binding (DECL_NAME (t), t);
7774
7775 leave_scope ();
7776 }
7777
7778 /* Create the function call operator.
7779
7780 Messing with declarators like this is no uglier than building up the
7781 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7782 other code. */
7783 {
7784 cp_decl_specifier_seq return_type_specs;
7785 cp_declarator* declarator;
7786 tree fco;
7787 int quals;
7788 void *p;
7789
7790 clear_decl_specs (&return_type_specs);
7791 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7792 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7793 else
7794 /* Maybe we will deduce the return type later, but we can use void
7795 as a placeholder return type anyways. */
7796 return_type_specs.type = void_type_node;
7797
7798 p = obstack_alloc (&declarator_obstack, 0);
7799
7800 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7801 sfk_none);
7802
7803 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7804 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7805 declarator = make_call_declarator (declarator, param_list, quals,
7806 exception_spec,
7807 /*late_return_type=*/NULL_TREE);
7808 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7809
7810 fco = grokmethod (&return_type_specs,
7811 declarator,
7812 attributes);
7813 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7814 DECL_ARTIFICIAL (fco) = 1;
7815
7816 finish_member_declaration (fco);
7817
7818 obstack_free (&declarator_obstack, p);
7819 }
7820 }
7821
7822 /* Parse the body of a lambda expression, which is simply
7823
7824 compound-statement
7825
7826 but which requires special handling.
7827 LAMBDA_EXPR is the current representation of the lambda expression. */
7828
7829 static void
7830 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7831 {
7832 bool nested = (current_function_decl != NULL_TREE);
7833 if (nested)
7834 push_function_context ();
7835
7836 /* Finish the function call operator
7837 - class_specifier
7838 + late_parsing_for_member
7839 + function_definition_after_declarator
7840 + ctor_initializer_opt_and_function_body */
7841 {
7842 tree fco = lambda_function (lambda_expr);
7843 tree body;
7844 bool done = false;
7845
7846 /* Let the front end know that we are going to be defining this
7847 function. */
7848 start_preparsed_function (fco,
7849 NULL_TREE,
7850 SF_PRE_PARSED | SF_INCLASS_INLINE);
7851
7852 start_lambda_scope (fco);
7853 body = begin_function_body ();
7854
7855 /* 5.1.1.4 of the standard says:
7856 If a lambda-expression does not include a trailing-return-type, it
7857 is as if the trailing-return-type denotes the following type:
7858 * if the compound-statement is of the form
7859 { return attribute-specifier [opt] expression ; }
7860 the type of the returned expression after lvalue-to-rvalue
7861 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7862 (_conv.array_ 4.2), and function-to-pointer conversion
7863 (_conv.func_ 4.3);
7864 * otherwise, void. */
7865
7866 /* In a lambda that has neither a lambda-return-type-clause
7867 nor a deducible form, errors should be reported for return statements
7868 in the body. Since we used void as the placeholder return type, parsing
7869 the body as usual will give such desired behavior. */
7870 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7871 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7872 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7873 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7874 {
7875 tree compound_stmt;
7876 tree expr = NULL_TREE;
7877 cp_id_kind idk = CP_ID_KIND_NONE;
7878
7879 /* Parse tentatively in case there's more after the initial return
7880 statement. */
7881 cp_parser_parse_tentatively (parser);
7882
7883 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7884 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7885
7886 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7887
7888 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7889 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7890
7891 if (cp_parser_parse_definitely (parser))
7892 {
7893 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7894
7895 compound_stmt = begin_compound_stmt (0);
7896 /* Will get error here if type not deduced yet. */
7897 finish_return_stmt (expr);
7898 finish_compound_stmt (compound_stmt);
7899
7900 done = true;
7901 }
7902 }
7903
7904 if (!done)
7905 {
7906 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7907 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7908 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7909 cp_parser_compound_stmt does not pass it. */
7910 cp_parser_function_body (parser);
7911 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7912 }
7913
7914 finish_function_body (body);
7915 finish_lambda_scope ();
7916
7917 /* Finish the function and generate code for it if necessary. */
7918 expand_or_defer_fn (finish_function (/*inline*/2));
7919 }
7920
7921 if (nested)
7922 pop_function_context();
7923 }
7924
7925 /* Statements [gram.stmt.stmt] */
7926
7927 /* Parse a statement.
7928
7929 statement:
7930 labeled-statement
7931 expression-statement
7932 compound-statement
7933 selection-statement
7934 iteration-statement
7935 jump-statement
7936 declaration-statement
7937 try-block
7938
7939 IN_COMPOUND is true when the statement is nested inside a
7940 cp_parser_compound_statement; this matters for certain pragmas.
7941
7942 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7943 is a (possibly labeled) if statement which is not enclosed in braces
7944 and has an else clause. This is used to implement -Wparentheses. */
7945
7946 static void
7947 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7948 bool in_compound, bool *if_p)
7949 {
7950 tree statement;
7951 cp_token *token;
7952 location_t statement_location;
7953
7954 restart:
7955 if (if_p != NULL)
7956 *if_p = false;
7957 /* There is no statement yet. */
7958 statement = NULL_TREE;
7959 /* Peek at the next token. */
7960 token = cp_lexer_peek_token (parser->lexer);
7961 /* Remember the location of the first token in the statement. */
7962 statement_location = token->location;
7963 /* If this is a keyword, then that will often determine what kind of
7964 statement we have. */
7965 if (token->type == CPP_KEYWORD)
7966 {
7967 enum rid keyword = token->keyword;
7968
7969 switch (keyword)
7970 {
7971 case RID_CASE:
7972 case RID_DEFAULT:
7973 /* Looks like a labeled-statement with a case label.
7974 Parse the label, and then use tail recursion to parse
7975 the statement. */
7976 cp_parser_label_for_labeled_statement (parser);
7977 goto restart;
7978
7979 case RID_IF:
7980 case RID_SWITCH:
7981 statement = cp_parser_selection_statement (parser, if_p);
7982 break;
7983
7984 case RID_WHILE:
7985 case RID_DO:
7986 case RID_FOR:
7987 statement = cp_parser_iteration_statement (parser);
7988 break;
7989
7990 case RID_BREAK:
7991 case RID_CONTINUE:
7992 case RID_RETURN:
7993 case RID_GOTO:
7994 statement = cp_parser_jump_statement (parser);
7995 break;
7996
7997 /* Objective-C++ exception-handling constructs. */
7998 case RID_AT_TRY:
7999 case RID_AT_CATCH:
8000 case RID_AT_FINALLY:
8001 case RID_AT_SYNCHRONIZED:
8002 case RID_AT_THROW:
8003 statement = cp_parser_objc_statement (parser);
8004 break;
8005
8006 case RID_TRY:
8007 statement = cp_parser_try_block (parser);
8008 break;
8009
8010 case RID_NAMESPACE:
8011 /* This must be a namespace alias definition. */
8012 cp_parser_declaration_statement (parser);
8013 return;
8014
8015 default:
8016 /* It might be a keyword like `int' that can start a
8017 declaration-statement. */
8018 break;
8019 }
8020 }
8021 else if (token->type == CPP_NAME)
8022 {
8023 /* If the next token is a `:', then we are looking at a
8024 labeled-statement. */
8025 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8026 if (token->type == CPP_COLON)
8027 {
8028 /* Looks like a labeled-statement with an ordinary label.
8029 Parse the label, and then use tail recursion to parse
8030 the statement. */
8031 cp_parser_label_for_labeled_statement (parser);
8032 goto restart;
8033 }
8034 }
8035 /* Anything that starts with a `{' must be a compound-statement. */
8036 else if (token->type == CPP_OPEN_BRACE)
8037 statement = cp_parser_compound_statement (parser, NULL, false);
8038 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8039 a statement all its own. */
8040 else if (token->type == CPP_PRAGMA)
8041 {
8042 /* Only certain OpenMP pragmas are attached to statements, and thus
8043 are considered statements themselves. All others are not. In
8044 the context of a compound, accept the pragma as a "statement" and
8045 return so that we can check for a close brace. Otherwise we
8046 require a real statement and must go back and read one. */
8047 if (in_compound)
8048 cp_parser_pragma (parser, pragma_compound);
8049 else if (!cp_parser_pragma (parser, pragma_stmt))
8050 goto restart;
8051 return;
8052 }
8053 else if (token->type == CPP_EOF)
8054 {
8055 cp_parser_error (parser, "expected statement");
8056 return;
8057 }
8058
8059 /* Everything else must be a declaration-statement or an
8060 expression-statement. Try for the declaration-statement
8061 first, unless we are looking at a `;', in which case we know that
8062 we have an expression-statement. */
8063 if (!statement)
8064 {
8065 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8066 {
8067 cp_parser_parse_tentatively (parser);
8068 /* Try to parse the declaration-statement. */
8069 cp_parser_declaration_statement (parser);
8070 /* If that worked, we're done. */
8071 if (cp_parser_parse_definitely (parser))
8072 return;
8073 }
8074 /* Look for an expression-statement instead. */
8075 statement = cp_parser_expression_statement (parser, in_statement_expr);
8076 }
8077
8078 /* Set the line number for the statement. */
8079 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8080 SET_EXPR_LOCATION (statement, statement_location);
8081 }
8082
8083 /* Parse the label for a labeled-statement, i.e.
8084
8085 identifier :
8086 case constant-expression :
8087 default :
8088
8089 GNU Extension:
8090 case constant-expression ... constant-expression : statement
8091
8092 When a label is parsed without errors, the label is added to the
8093 parse tree by the finish_* functions, so this function doesn't
8094 have to return the label. */
8095
8096 static void
8097 cp_parser_label_for_labeled_statement (cp_parser* parser)
8098 {
8099 cp_token *token;
8100 tree label = NULL_TREE;
8101
8102 /* The next token should be an identifier. */
8103 token = cp_lexer_peek_token (parser->lexer);
8104 if (token->type != CPP_NAME
8105 && token->type != CPP_KEYWORD)
8106 {
8107 cp_parser_error (parser, "expected labeled-statement");
8108 return;
8109 }
8110
8111 switch (token->keyword)
8112 {
8113 case RID_CASE:
8114 {
8115 tree expr, expr_hi;
8116 cp_token *ellipsis;
8117
8118 /* Consume the `case' token. */
8119 cp_lexer_consume_token (parser->lexer);
8120 /* Parse the constant-expression. */
8121 expr = cp_parser_constant_expression (parser,
8122 /*allow_non_constant_p=*/false,
8123 NULL);
8124
8125 ellipsis = cp_lexer_peek_token (parser->lexer);
8126 if (ellipsis->type == CPP_ELLIPSIS)
8127 {
8128 /* Consume the `...' token. */
8129 cp_lexer_consume_token (parser->lexer);
8130 expr_hi =
8131 cp_parser_constant_expression (parser,
8132 /*allow_non_constant_p=*/false,
8133 NULL);
8134 /* We don't need to emit warnings here, as the common code
8135 will do this for us. */
8136 }
8137 else
8138 expr_hi = NULL_TREE;
8139
8140 if (parser->in_switch_statement_p)
8141 finish_case_label (token->location, expr, expr_hi);
8142 else
8143 error_at (token->location,
8144 "case label %qE not within a switch statement",
8145 expr);
8146 }
8147 break;
8148
8149 case RID_DEFAULT:
8150 /* Consume the `default' token. */
8151 cp_lexer_consume_token (parser->lexer);
8152
8153 if (parser->in_switch_statement_p)
8154 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8155 else
8156 error_at (token->location, "case label not within a switch statement");
8157 break;
8158
8159 default:
8160 /* Anything else must be an ordinary label. */
8161 label = finish_label_stmt (cp_parser_identifier (parser));
8162 break;
8163 }
8164
8165 /* Require the `:' token. */
8166 cp_parser_require (parser, CPP_COLON, RT_COLON);
8167
8168 /* An ordinary label may optionally be followed by attributes.
8169 However, this is only permitted if the attributes are then
8170 followed by a semicolon. This is because, for backward
8171 compatibility, when parsing
8172 lab: __attribute__ ((unused)) int i;
8173 we want the attribute to attach to "i", not "lab". */
8174 if (label != NULL_TREE
8175 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8176 {
8177 tree attrs;
8178
8179 cp_parser_parse_tentatively (parser);
8180 attrs = cp_parser_attributes_opt (parser);
8181 if (attrs == NULL_TREE
8182 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8183 cp_parser_abort_tentative_parse (parser);
8184 else if (!cp_parser_parse_definitely (parser))
8185 ;
8186 else
8187 cplus_decl_attributes (&label, attrs, 0);
8188 }
8189 }
8190
8191 /* Parse an expression-statement.
8192
8193 expression-statement:
8194 expression [opt] ;
8195
8196 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8197 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8198 indicates whether this expression-statement is part of an
8199 expression statement. */
8200
8201 static tree
8202 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8203 {
8204 tree statement = NULL_TREE;
8205 cp_token *token = cp_lexer_peek_token (parser->lexer);
8206
8207 /* If the next token is a ';', then there is no expression
8208 statement. */
8209 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8210 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8211
8212 /* Give a helpful message for "A<T>::type t;" and the like. */
8213 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8214 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8215 {
8216 if (TREE_CODE (statement) == SCOPE_REF)
8217 error_at (token->location, "need %<typename%> before %qE because "
8218 "%qT is a dependent scope",
8219 statement, TREE_OPERAND (statement, 0));
8220 else if (is_overloaded_fn (statement)
8221 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8222 {
8223 /* A::A a; */
8224 tree fn = get_first_fn (statement);
8225 error_at (token->location,
8226 "%<%T::%D%> names the constructor, not the type",
8227 DECL_CONTEXT (fn), DECL_NAME (fn));
8228 }
8229 }
8230
8231 /* Consume the final `;'. */
8232 cp_parser_consume_semicolon_at_end_of_statement (parser);
8233
8234 if (in_statement_expr
8235 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8236 /* This is the final expression statement of a statement
8237 expression. */
8238 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8239 else if (statement)
8240 statement = finish_expr_stmt (statement);
8241 else
8242 finish_stmt ();
8243
8244 return statement;
8245 }
8246
8247 /* Parse a compound-statement.
8248
8249 compound-statement:
8250 { statement-seq [opt] }
8251
8252 GNU extension:
8253
8254 compound-statement:
8255 { label-declaration-seq [opt] statement-seq [opt] }
8256
8257 label-declaration-seq:
8258 label-declaration
8259 label-declaration-seq label-declaration
8260
8261 Returns a tree representing the statement. */
8262
8263 static tree
8264 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8265 bool in_try)
8266 {
8267 tree compound_stmt;
8268
8269 /* Consume the `{'. */
8270 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8271 return error_mark_node;
8272 /* Begin the compound-statement. */
8273 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8274 /* If the next keyword is `__label__' we have a label declaration. */
8275 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8276 cp_parser_label_declaration (parser);
8277 /* Parse an (optional) statement-seq. */
8278 cp_parser_statement_seq_opt (parser, in_statement_expr);
8279 /* Finish the compound-statement. */
8280 finish_compound_stmt (compound_stmt);
8281 /* Consume the `}'. */
8282 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8283
8284 return compound_stmt;
8285 }
8286
8287 /* Parse an (optional) statement-seq.
8288
8289 statement-seq:
8290 statement
8291 statement-seq [opt] statement */
8292
8293 static void
8294 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8295 {
8296 /* Scan statements until there aren't any more. */
8297 while (true)
8298 {
8299 cp_token *token = cp_lexer_peek_token (parser->lexer);
8300
8301 /* If we are looking at a `}', then we have run out of
8302 statements; the same is true if we have reached the end
8303 of file, or have stumbled upon a stray '@end'. */
8304 if (token->type == CPP_CLOSE_BRACE
8305 || token->type == CPP_EOF
8306 || token->type == CPP_PRAGMA_EOL
8307 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8308 break;
8309
8310 /* If we are in a compound statement and find 'else' then
8311 something went wrong. */
8312 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8313 {
8314 if (parser->in_statement & IN_IF_STMT)
8315 break;
8316 else
8317 {
8318 token = cp_lexer_consume_token (parser->lexer);
8319 error_at (token->location, "%<else%> without a previous %<if%>");
8320 }
8321 }
8322
8323 /* Parse the statement. */
8324 cp_parser_statement (parser, in_statement_expr, true, NULL);
8325 }
8326 }
8327
8328 /* Parse a selection-statement.
8329
8330 selection-statement:
8331 if ( condition ) statement
8332 if ( condition ) statement else statement
8333 switch ( condition ) statement
8334
8335 Returns the new IF_STMT or SWITCH_STMT.
8336
8337 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8338 is a (possibly labeled) if statement which is not enclosed in
8339 braces and has an else clause. This is used to implement
8340 -Wparentheses. */
8341
8342 static tree
8343 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8344 {
8345 cp_token *token;
8346 enum rid keyword;
8347
8348 if (if_p != NULL)
8349 *if_p = false;
8350
8351 /* Peek at the next token. */
8352 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8353
8354 /* See what kind of keyword it is. */
8355 keyword = token->keyword;
8356 switch (keyword)
8357 {
8358 case RID_IF:
8359 case RID_SWITCH:
8360 {
8361 tree statement;
8362 tree condition;
8363
8364 /* Look for the `('. */
8365 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8366 {
8367 cp_parser_skip_to_end_of_statement (parser);
8368 return error_mark_node;
8369 }
8370
8371 /* Begin the selection-statement. */
8372 if (keyword == RID_IF)
8373 statement = begin_if_stmt ();
8374 else
8375 statement = begin_switch_stmt ();
8376
8377 /* Parse the condition. */
8378 condition = cp_parser_condition (parser);
8379 /* Look for the `)'. */
8380 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8381 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8382 /*consume_paren=*/true);
8383
8384 if (keyword == RID_IF)
8385 {
8386 bool nested_if;
8387 unsigned char in_statement;
8388
8389 /* Add the condition. */
8390 finish_if_stmt_cond (condition, statement);
8391
8392 /* Parse the then-clause. */
8393 in_statement = parser->in_statement;
8394 parser->in_statement |= IN_IF_STMT;
8395 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8396 {
8397 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8398 add_stmt (build_empty_stmt (loc));
8399 cp_lexer_consume_token (parser->lexer);
8400 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8401 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8402 "empty body in an %<if%> statement");
8403 nested_if = false;
8404 }
8405 else
8406 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8407 parser->in_statement = in_statement;
8408
8409 finish_then_clause (statement);
8410
8411 /* If the next token is `else', parse the else-clause. */
8412 if (cp_lexer_next_token_is_keyword (parser->lexer,
8413 RID_ELSE))
8414 {
8415 /* Consume the `else' keyword. */
8416 cp_lexer_consume_token (parser->lexer);
8417 begin_else_clause (statement);
8418 /* Parse the else-clause. */
8419 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8420 {
8421 location_t loc;
8422 loc = cp_lexer_peek_token (parser->lexer)->location;
8423 warning_at (loc,
8424 OPT_Wempty_body, "suggest braces around "
8425 "empty body in an %<else%> statement");
8426 add_stmt (build_empty_stmt (loc));
8427 cp_lexer_consume_token (parser->lexer);
8428 }
8429 else
8430 cp_parser_implicitly_scoped_statement (parser, NULL);
8431
8432 finish_else_clause (statement);
8433
8434 /* If we are currently parsing a then-clause, then
8435 IF_P will not be NULL. We set it to true to
8436 indicate that this if statement has an else clause.
8437 This may trigger the Wparentheses warning below
8438 when we get back up to the parent if statement. */
8439 if (if_p != NULL)
8440 *if_p = true;
8441 }
8442 else
8443 {
8444 /* This if statement does not have an else clause. If
8445 NESTED_IF is true, then the then-clause is an if
8446 statement which does have an else clause. We warn
8447 about the potential ambiguity. */
8448 if (nested_if)
8449 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8450 "suggest explicit braces to avoid ambiguous"
8451 " %<else%>");
8452 }
8453
8454 /* Now we're all done with the if-statement. */
8455 finish_if_stmt (statement);
8456 }
8457 else
8458 {
8459 bool in_switch_statement_p;
8460 unsigned char in_statement;
8461
8462 /* Add the condition. */
8463 finish_switch_cond (condition, statement);
8464
8465 /* Parse the body of the switch-statement. */
8466 in_switch_statement_p = parser->in_switch_statement_p;
8467 in_statement = parser->in_statement;
8468 parser->in_switch_statement_p = true;
8469 parser->in_statement |= IN_SWITCH_STMT;
8470 cp_parser_implicitly_scoped_statement (parser, NULL);
8471 parser->in_switch_statement_p = in_switch_statement_p;
8472 parser->in_statement = in_statement;
8473
8474 /* Now we're all done with the switch-statement. */
8475 finish_switch_stmt (statement);
8476 }
8477
8478 return statement;
8479 }
8480 break;
8481
8482 default:
8483 cp_parser_error (parser, "expected selection-statement");
8484 return error_mark_node;
8485 }
8486 }
8487
8488 /* Parse a condition.
8489
8490 condition:
8491 expression
8492 type-specifier-seq declarator = initializer-clause
8493 type-specifier-seq declarator braced-init-list
8494
8495 GNU Extension:
8496
8497 condition:
8498 type-specifier-seq declarator asm-specification [opt]
8499 attributes [opt] = assignment-expression
8500
8501 Returns the expression that should be tested. */
8502
8503 static tree
8504 cp_parser_condition (cp_parser* parser)
8505 {
8506 cp_decl_specifier_seq type_specifiers;
8507 const char *saved_message;
8508
8509 /* Try the declaration first. */
8510 cp_parser_parse_tentatively (parser);
8511 /* New types are not allowed in the type-specifier-seq for a
8512 condition. */
8513 saved_message = parser->type_definition_forbidden_message;
8514 parser->type_definition_forbidden_message
8515 = G_("types may not be defined in conditions");
8516 /* Parse the type-specifier-seq. */
8517 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8518 /*is_trailing_return=*/false,
8519 &type_specifiers);
8520 /* Restore the saved message. */
8521 parser->type_definition_forbidden_message = saved_message;
8522 /* If all is well, we might be looking at a declaration. */
8523 if (!cp_parser_error_occurred (parser))
8524 {
8525 tree decl;
8526 tree asm_specification;
8527 tree attributes;
8528 cp_declarator *declarator;
8529 tree initializer = NULL_TREE;
8530
8531 /* Parse the declarator. */
8532 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8533 /*ctor_dtor_or_conv_p=*/NULL,
8534 /*parenthesized_p=*/NULL,
8535 /*member_p=*/false);
8536 /* Parse the attributes. */
8537 attributes = cp_parser_attributes_opt (parser);
8538 /* Parse the asm-specification. */
8539 asm_specification = cp_parser_asm_specification_opt (parser);
8540 /* If the next token is not an `=' or '{', then we might still be
8541 looking at an expression. For example:
8542
8543 if (A(a).x)
8544
8545 looks like a decl-specifier-seq and a declarator -- but then
8546 there is no `=', so this is an expression. */
8547 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8548 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8549 cp_parser_simulate_error (parser);
8550
8551 /* If we did see an `=' or '{', then we are looking at a declaration
8552 for sure. */
8553 if (cp_parser_parse_definitely (parser))
8554 {
8555 tree pushed_scope;
8556 bool non_constant_p;
8557 bool flags = LOOKUP_ONLYCONVERTING;
8558
8559 /* Create the declaration. */
8560 decl = start_decl (declarator, &type_specifiers,
8561 /*initialized_p=*/true,
8562 attributes, /*prefix_attributes=*/NULL_TREE,
8563 &pushed_scope);
8564
8565 /* Parse the initializer. */
8566 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8567 {
8568 initializer = cp_parser_braced_list (parser, &non_constant_p);
8569 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8570 flags = 0;
8571 }
8572 else
8573 {
8574 /* Consume the `='. */
8575 cp_parser_require (parser, CPP_EQ, RT_EQ);
8576 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8577 }
8578 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8579 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8580
8581 if (!non_constant_p)
8582 initializer = fold_non_dependent_expr (initializer);
8583
8584 /* Process the initializer. */
8585 cp_finish_decl (decl,
8586 initializer, !non_constant_p,
8587 asm_specification,
8588 flags);
8589
8590 if (pushed_scope)
8591 pop_scope (pushed_scope);
8592
8593 return convert_from_reference (decl);
8594 }
8595 }
8596 /* If we didn't even get past the declarator successfully, we are
8597 definitely not looking at a declaration. */
8598 else
8599 cp_parser_abort_tentative_parse (parser);
8600
8601 /* Otherwise, we are looking at an expression. */
8602 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8603 }
8604
8605 /* Parses a traditional for-statement until the closing ')', not included. */
8606
8607 static tree
8608 cp_parser_c_for (cp_parser *parser)
8609 {
8610 /* Normal for loop */
8611 tree stmt;
8612 tree condition = NULL_TREE;
8613 tree expression = NULL_TREE;
8614
8615 /* Begin the for-statement. */
8616 stmt = begin_for_stmt ();
8617
8618 /* Parse the initialization. */
8619 cp_parser_for_init_statement (parser);
8620 finish_for_init_stmt (stmt);
8621
8622 /* If there's a condition, process it. */
8623 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8624 condition = cp_parser_condition (parser);
8625 finish_for_cond (condition, stmt);
8626 /* Look for the `;'. */
8627 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8628
8629 /* If there's an expression, process it. */
8630 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8631 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8632 finish_for_expr (expression, stmt);
8633
8634 return stmt;
8635 }
8636
8637 /* Tries to parse a range-based for-statement:
8638
8639 range-based-for:
8640 type-specifier-seq declarator : expression
8641
8642 If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8643 expression. Note that the *DECL is returned unfinished, so
8644 later you should call cp_finish_decl().
8645
8646 Returns TRUE iff a range-based for is parsed. */
8647
8648 static tree
8649 cp_parser_range_for (cp_parser *parser)
8650 {
8651 tree stmt, range_decl, range_expr;
8652 cp_decl_specifier_seq type_specifiers;
8653 cp_declarator *declarator;
8654 const char *saved_message;
8655 tree attributes, pushed_scope;
8656
8657 cp_parser_parse_tentatively (parser);
8658 /* New types are not allowed in the type-specifier-seq for a
8659 range-based for loop. */
8660 saved_message = parser->type_definition_forbidden_message;
8661 parser->type_definition_forbidden_message
8662 = G_("types may not be defined in range-based for loops");
8663 /* Parse the type-specifier-seq. */
8664 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8665 /*is_trailing_return=*/false,
8666 &type_specifiers);
8667 /* Restore the saved message. */
8668 parser->type_definition_forbidden_message = saved_message;
8669 /* If all is well, we might be looking at a declaration. */
8670 if (cp_parser_error_occurred (parser))
8671 {
8672 cp_parser_abort_tentative_parse (parser);
8673 return NULL_TREE;
8674 }
8675 /* Parse the declarator. */
8676 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8677 /*ctor_dtor_or_conv_p=*/NULL,
8678 /*parenthesized_p=*/NULL,
8679 /*member_p=*/false);
8680 /* Parse the attributes. */
8681 attributes = cp_parser_attributes_opt (parser);
8682 /* The next token should be `:'. */
8683 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8684 cp_parser_simulate_error (parser);
8685
8686 /* Check if it is a range-based for */
8687 if (!cp_parser_parse_definitely (parser))
8688 return NULL_TREE;
8689
8690 cp_parser_require (parser, CPP_COLON, RT_COLON);
8691 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8692 {
8693 bool expr_non_constant_p;
8694 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8695 }
8696 else
8697 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8698
8699 /* If in template, STMT is converted to a normal for-statements
8700 at instantiation. If not, it is done just ahead. */
8701 if (processing_template_decl)
8702 stmt = begin_range_for_stmt ();
8703 else
8704 stmt = begin_for_stmt ();
8705
8706 /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8707 range_decl = start_decl (declarator, &type_specifiers,
8708 /*initialized_p=*/SD_INITIALIZED,
8709 attributes, /*prefix_attributes=*/NULL_TREE,
8710 &pushed_scope);
8711 /* No scope allowed here */
8712 pop_scope (pushed_scope);
8713
8714 if (TREE_CODE (stmt) == RANGE_FOR_STMT)
8715 finish_range_for_decl (stmt, range_decl, range_expr);
8716 else
8717 /* Convert the range-based for loop into a normal for-statement. */
8718 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8719
8720 return stmt;
8721 }
8722
8723 /* Converts a range-based for-statement into a normal
8724 for-statement, as per the definition.
8725
8726 for (RANGE_DECL : RANGE_EXPR)
8727 BLOCK
8728
8729 should be equivalent to:
8730
8731 {
8732 auto &&__range = RANGE_EXPR;
8733 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8734 __begin != __end;
8735 ++__begin)
8736 {
8737 RANGE_DECL = *__begin;
8738 BLOCK
8739 }
8740 }
8741
8742 If RANGE_EXPR is an array:
8743 BEGIN_EXPR = __range
8744 END_EXPR = __range + ARRAY_SIZE(__range)
8745 Else:
8746 BEGIN_EXPR = begin(__range)
8747 END_EXPR = end(__range);
8748
8749 When calling begin()/end() we must use argument dependent
8750 lookup, but always considering 'std' as an associated namespace. */
8751
8752 tree
8753 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8754 {
8755 tree range_type, range_temp;
8756 tree begin, end;
8757 tree iter_type, begin_expr, end_expr;
8758 tree condition, expression;
8759
8760 /* Find out the type deduced by the declaration
8761 * `auto &&__range = range_expr' */
8762 range_type = cp_build_reference_type (make_auto (), true);
8763 range_type = do_auto_deduction (range_type, range_expr,
8764 type_uses_auto (range_type));
8765
8766 /* Create the __range variable */
8767 range_temp = build_decl (input_location, VAR_DECL,
8768 get_identifier ("__for_range"), range_type);
8769 TREE_USED (range_temp) = 1;
8770 DECL_ARTIFICIAL (range_temp) = 1;
8771 pushdecl (range_temp);
8772 finish_expr_stmt (cp_build_modify_expr (range_temp, INIT_EXPR, range_expr,
8773 tf_warning_or_error));
8774 range_temp = convert_from_reference (range_temp);
8775
8776 if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8777 {
8778 /* If RANGE_TEMP is an array we will use pointer arithmetic */
8779 iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8780 begin_expr = range_temp;
8781 end_expr
8782 = build_binary_op (input_location, PLUS_EXPR,
8783 range_temp,
8784 array_type_nelts_top (TREE_TYPE (range_temp)), 0);
8785 }
8786 else
8787 {
8788 /* If it is not an array, we must call begin(__range)/end__range() */
8789 VEC(tree,gc) *vec;
8790
8791 begin_expr = get_identifier ("begin");
8792 vec = make_tree_vector ();
8793 VEC_safe_push (tree, gc, vec, range_temp);
8794 begin_expr = perform_koenig_lookup (begin_expr, vec,
8795 /*include_std=*/true);
8796 begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8797 tf_warning_or_error);
8798 release_tree_vector (vec);
8799
8800 end_expr = get_identifier ("end");
8801 vec = make_tree_vector ();
8802 VEC_safe_push (tree, gc, vec, range_temp);
8803 end_expr = perform_koenig_lookup (end_expr, vec,
8804 /*include_std=*/true);
8805 end_expr = finish_call_expr (end_expr, &vec, false, true,
8806 tf_warning_or_error);
8807 release_tree_vector (vec);
8808
8809 /* The unqualified type of the __begin and __end temporaries should
8810 * be the same as required by the multiple auto declaration */
8811 iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8812 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8813 error ("inconsistent begin/end types in range-based for: %qT and %qT",
8814 TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8815 }
8816
8817 /* The new for initialization statement */
8818 begin = build_decl (input_location, VAR_DECL,
8819 get_identifier ("__for_begin"), iter_type);
8820 TREE_USED (begin) = 1;
8821 DECL_ARTIFICIAL (begin) = 1;
8822 pushdecl (begin);
8823 finish_expr_stmt (cp_build_modify_expr (begin, INIT_EXPR, begin_expr,
8824 tf_warning_or_error));
8825 end = build_decl (input_location, VAR_DECL,
8826 get_identifier ("__for_end"), iter_type);
8827 TREE_USED (end) = 1;
8828 DECL_ARTIFICIAL (end) = 1;
8829 pushdecl (end);
8830
8831 finish_expr_stmt (cp_build_modify_expr (end, INIT_EXPR, end_expr,
8832 tf_warning_or_error));
8833
8834 finish_for_init_stmt (statement);
8835
8836 /* The new for condition */
8837 condition = build_x_binary_op (NE_EXPR,
8838 begin, ERROR_MARK,
8839 end, ERROR_MARK,
8840 NULL, tf_warning_or_error);
8841 finish_for_cond (condition, statement);
8842
8843 /* The new increment expression */
8844 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8845 finish_for_expr (expression, statement);
8846
8847 /* The declaration is initialized with *__begin inside the loop body */
8848 cp_finish_decl (range_decl,
8849 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8850 /*is_constant_init*/false, NULL_TREE,
8851 LOOKUP_ONLYCONVERTING);
8852
8853 return statement;
8854 }
8855
8856
8857 /* Parse an iteration-statement.
8858
8859 iteration-statement:
8860 while ( condition ) statement
8861 do statement while ( expression ) ;
8862 for ( for-init-statement condition [opt] ; expression [opt] )
8863 statement
8864
8865 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8866
8867 static tree
8868 cp_parser_iteration_statement (cp_parser* parser)
8869 {
8870 cp_token *token;
8871 enum rid keyword;
8872 tree statement;
8873 unsigned char in_statement;
8874
8875 /* Peek at the next token. */
8876 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8877 if (!token)
8878 return error_mark_node;
8879
8880 /* Remember whether or not we are already within an iteration
8881 statement. */
8882 in_statement = parser->in_statement;
8883
8884 /* See what kind of keyword it is. */
8885 keyword = token->keyword;
8886 switch (keyword)
8887 {
8888 case RID_WHILE:
8889 {
8890 tree condition;
8891
8892 /* Begin the while-statement. */
8893 statement = begin_while_stmt ();
8894 /* Look for the `('. */
8895 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8896 /* Parse the condition. */
8897 condition = cp_parser_condition (parser);
8898 finish_while_stmt_cond (condition, statement);
8899 /* Look for the `)'. */
8900 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8901 /* Parse the dependent statement. */
8902 parser->in_statement = IN_ITERATION_STMT;
8903 cp_parser_already_scoped_statement (parser);
8904 parser->in_statement = in_statement;
8905 /* We're done with the while-statement. */
8906 finish_while_stmt (statement);
8907 }
8908 break;
8909
8910 case RID_DO:
8911 {
8912 tree expression;
8913
8914 /* Begin the do-statement. */
8915 statement = begin_do_stmt ();
8916 /* Parse the body of the do-statement. */
8917 parser->in_statement = IN_ITERATION_STMT;
8918 cp_parser_implicitly_scoped_statement (parser, NULL);
8919 parser->in_statement = in_statement;
8920 finish_do_body (statement);
8921 /* Look for the `while' keyword. */
8922 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8923 /* Look for the `('. */
8924 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8925 /* Parse the expression. */
8926 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8927 /* We're done with the do-statement. */
8928 finish_do_stmt (expression, statement);
8929 /* Look for the `)'. */
8930 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8931 /* Look for the `;'. */
8932 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8933 }
8934 break;
8935
8936 case RID_FOR:
8937 {
8938 /* Look for the `('. */
8939 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8940
8941 if (cxx_dialect == cxx0x)
8942 statement = cp_parser_range_for (parser);
8943 else
8944 statement = NULL_TREE;
8945 if (statement == NULL_TREE)
8946 statement = cp_parser_c_for (parser);
8947
8948 /* Look for the `)'. */
8949 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8950
8951 /* Parse the body of the for-statement. */
8952 parser->in_statement = IN_ITERATION_STMT;
8953 cp_parser_already_scoped_statement (parser);
8954 parser->in_statement = in_statement;
8955
8956 /* We're done with the for-statement. */
8957 finish_for_stmt (statement);
8958 }
8959 break;
8960
8961 default:
8962 cp_parser_error (parser, "expected iteration-statement");
8963 statement = error_mark_node;
8964 break;
8965 }
8966
8967 return statement;
8968 }
8969
8970 /* Parse a for-init-statement.
8971
8972 for-init-statement:
8973 expression-statement
8974 simple-declaration */
8975
8976 static void
8977 cp_parser_for_init_statement (cp_parser* parser)
8978 {
8979 /* If the next token is a `;', then we have an empty
8980 expression-statement. Grammatically, this is also a
8981 simple-declaration, but an invalid one, because it does not
8982 declare anything. Therefore, if we did not handle this case
8983 specially, we would issue an error message about an invalid
8984 declaration. */
8985 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8986 {
8987 /* We're going to speculatively look for a declaration, falling back
8988 to an expression, if necessary. */
8989 cp_parser_parse_tentatively (parser);
8990 /* Parse the declaration. */
8991 cp_parser_simple_declaration (parser,
8992 /*function_definition_allowed_p=*/false);
8993 /* If the tentative parse failed, then we shall need to look for an
8994 expression-statement. */
8995 if (cp_parser_parse_definitely (parser))
8996 return;
8997 }
8998
8999 cp_parser_expression_statement (parser, NULL_TREE);
9000 }
9001
9002 /* Parse a jump-statement.
9003
9004 jump-statement:
9005 break ;
9006 continue ;
9007 return expression [opt] ;
9008 return braced-init-list ;
9009 goto identifier ;
9010
9011 GNU extension:
9012
9013 jump-statement:
9014 goto * expression ;
9015
9016 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9017
9018 static tree
9019 cp_parser_jump_statement (cp_parser* parser)
9020 {
9021 tree statement = error_mark_node;
9022 cp_token *token;
9023 enum rid keyword;
9024 unsigned char in_statement;
9025
9026 /* Peek at the next token. */
9027 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9028 if (!token)
9029 return error_mark_node;
9030
9031 /* See what kind of keyword it is. */
9032 keyword = token->keyword;
9033 switch (keyword)
9034 {
9035 case RID_BREAK:
9036 in_statement = parser->in_statement & ~IN_IF_STMT;
9037 switch (in_statement)
9038 {
9039 case 0:
9040 error_at (token->location, "break statement not within loop or switch");
9041 break;
9042 default:
9043 gcc_assert ((in_statement & IN_SWITCH_STMT)
9044 || in_statement == IN_ITERATION_STMT);
9045 statement = finish_break_stmt ();
9046 break;
9047 case IN_OMP_BLOCK:
9048 error_at (token->location, "invalid exit from OpenMP structured block");
9049 break;
9050 case IN_OMP_FOR:
9051 error_at (token->location, "break statement used with OpenMP for loop");
9052 break;
9053 }
9054 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9055 break;
9056
9057 case RID_CONTINUE:
9058 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9059 {
9060 case 0:
9061 error_at (token->location, "continue statement not within a loop");
9062 break;
9063 case IN_ITERATION_STMT:
9064 case IN_OMP_FOR:
9065 statement = finish_continue_stmt ();
9066 break;
9067 case IN_OMP_BLOCK:
9068 error_at (token->location, "invalid exit from OpenMP structured block");
9069 break;
9070 default:
9071 gcc_unreachable ();
9072 }
9073 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9074 break;
9075
9076 case RID_RETURN:
9077 {
9078 tree expr;
9079 bool expr_non_constant_p;
9080
9081 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9082 {
9083 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9084 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9085 }
9086 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9087 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9088 else
9089 /* If the next token is a `;', then there is no
9090 expression. */
9091 expr = NULL_TREE;
9092 /* Build the return-statement. */
9093 statement = finish_return_stmt (expr);
9094 /* Look for the final `;'. */
9095 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9096 }
9097 break;
9098
9099 case RID_GOTO:
9100 /* Create the goto-statement. */
9101 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9102 {
9103 /* Issue a warning about this use of a GNU extension. */
9104 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9105 /* Consume the '*' token. */
9106 cp_lexer_consume_token (parser->lexer);
9107 /* Parse the dependent expression. */
9108 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9109 }
9110 else
9111 finish_goto_stmt (cp_parser_identifier (parser));
9112 /* Look for the final `;'. */
9113 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9114 break;
9115
9116 default:
9117 cp_parser_error (parser, "expected jump-statement");
9118 break;
9119 }
9120
9121 return statement;
9122 }
9123
9124 /* Parse a declaration-statement.
9125
9126 declaration-statement:
9127 block-declaration */
9128
9129 static void
9130 cp_parser_declaration_statement (cp_parser* parser)
9131 {
9132 void *p;
9133
9134 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9135 p = obstack_alloc (&declarator_obstack, 0);
9136
9137 /* Parse the block-declaration. */
9138 cp_parser_block_declaration (parser, /*statement_p=*/true);
9139
9140 /* Free any declarators allocated. */
9141 obstack_free (&declarator_obstack, p);
9142
9143 /* Finish off the statement. */
9144 finish_stmt ();
9145 }
9146
9147 /* Some dependent statements (like `if (cond) statement'), are
9148 implicitly in their own scope. In other words, if the statement is
9149 a single statement (as opposed to a compound-statement), it is
9150 none-the-less treated as if it were enclosed in braces. Any
9151 declarations appearing in the dependent statement are out of scope
9152 after control passes that point. This function parses a statement,
9153 but ensures that is in its own scope, even if it is not a
9154 compound-statement.
9155
9156 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9157 is a (possibly labeled) if statement which is not enclosed in
9158 braces and has an else clause. This is used to implement
9159 -Wparentheses.
9160
9161 Returns the new statement. */
9162
9163 static tree
9164 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9165 {
9166 tree statement;
9167
9168 if (if_p != NULL)
9169 *if_p = false;
9170
9171 /* Mark if () ; with a special NOP_EXPR. */
9172 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9173 {
9174 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9175 cp_lexer_consume_token (parser->lexer);
9176 statement = add_stmt (build_empty_stmt (loc));
9177 }
9178 /* if a compound is opened, we simply parse the statement directly. */
9179 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9180 statement = cp_parser_compound_statement (parser, NULL, false);
9181 /* If the token is not a `{', then we must take special action. */
9182 else
9183 {
9184 /* Create a compound-statement. */
9185 statement = begin_compound_stmt (0);
9186 /* Parse the dependent-statement. */
9187 cp_parser_statement (parser, NULL_TREE, false, if_p);
9188 /* Finish the dummy compound-statement. */
9189 finish_compound_stmt (statement);
9190 }
9191
9192 /* Return the statement. */
9193 return statement;
9194 }
9195
9196 /* For some dependent statements (like `while (cond) statement'), we
9197 have already created a scope. Therefore, even if the dependent
9198 statement is a compound-statement, we do not want to create another
9199 scope. */
9200
9201 static void
9202 cp_parser_already_scoped_statement (cp_parser* parser)
9203 {
9204 /* If the token is a `{', then we must take special action. */
9205 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9206 cp_parser_statement (parser, NULL_TREE, false, NULL);
9207 else
9208 {
9209 /* Avoid calling cp_parser_compound_statement, so that we
9210 don't create a new scope. Do everything else by hand. */
9211 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9212 /* If the next keyword is `__label__' we have a label declaration. */
9213 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9214 cp_parser_label_declaration (parser);
9215 /* Parse an (optional) statement-seq. */
9216 cp_parser_statement_seq_opt (parser, NULL_TREE);
9217 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9218 }
9219 }
9220
9221 /* Declarations [gram.dcl.dcl] */
9222
9223 /* Parse an optional declaration-sequence.
9224
9225 declaration-seq:
9226 declaration
9227 declaration-seq declaration */
9228
9229 static void
9230 cp_parser_declaration_seq_opt (cp_parser* parser)
9231 {
9232 while (true)
9233 {
9234 cp_token *token;
9235
9236 token = cp_lexer_peek_token (parser->lexer);
9237
9238 if (token->type == CPP_CLOSE_BRACE
9239 || token->type == CPP_EOF
9240 || token->type == CPP_PRAGMA_EOL)
9241 break;
9242
9243 if (token->type == CPP_SEMICOLON)
9244 {
9245 /* A declaration consisting of a single semicolon is
9246 invalid. Allow it unless we're being pedantic. */
9247 cp_lexer_consume_token (parser->lexer);
9248 if (!in_system_header)
9249 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9250 continue;
9251 }
9252
9253 /* If we're entering or exiting a region that's implicitly
9254 extern "C", modify the lang context appropriately. */
9255 if (!parser->implicit_extern_c && token->implicit_extern_c)
9256 {
9257 push_lang_context (lang_name_c);
9258 parser->implicit_extern_c = true;
9259 }
9260 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9261 {
9262 pop_lang_context ();
9263 parser->implicit_extern_c = false;
9264 }
9265
9266 if (token->type == CPP_PRAGMA)
9267 {
9268 /* A top-level declaration can consist solely of a #pragma.
9269 A nested declaration cannot, so this is done here and not
9270 in cp_parser_declaration. (A #pragma at block scope is
9271 handled in cp_parser_statement.) */
9272 cp_parser_pragma (parser, pragma_external);
9273 continue;
9274 }
9275
9276 /* Parse the declaration itself. */
9277 cp_parser_declaration (parser);
9278 }
9279 }
9280
9281 /* Parse a declaration.
9282
9283 declaration:
9284 block-declaration
9285 function-definition
9286 template-declaration
9287 explicit-instantiation
9288 explicit-specialization
9289 linkage-specification
9290 namespace-definition
9291
9292 GNU extension:
9293
9294 declaration:
9295 __extension__ declaration */
9296
9297 static void
9298 cp_parser_declaration (cp_parser* parser)
9299 {
9300 cp_token token1;
9301 cp_token token2;
9302 int saved_pedantic;
9303 void *p;
9304 tree attributes = NULL_TREE;
9305
9306 /* Check for the `__extension__' keyword. */
9307 if (cp_parser_extension_opt (parser, &saved_pedantic))
9308 {
9309 /* Parse the qualified declaration. */
9310 cp_parser_declaration (parser);
9311 /* Restore the PEDANTIC flag. */
9312 pedantic = saved_pedantic;
9313
9314 return;
9315 }
9316
9317 /* Try to figure out what kind of declaration is present. */
9318 token1 = *cp_lexer_peek_token (parser->lexer);
9319
9320 if (token1.type != CPP_EOF)
9321 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9322 else
9323 {
9324 token2.type = CPP_EOF;
9325 token2.keyword = RID_MAX;
9326 }
9327
9328 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9329 p = obstack_alloc (&declarator_obstack, 0);
9330
9331 /* If the next token is `extern' and the following token is a string
9332 literal, then we have a linkage specification. */
9333 if (token1.keyword == RID_EXTERN
9334 && cp_parser_is_string_literal (&token2))
9335 cp_parser_linkage_specification (parser);
9336 /* If the next token is `template', then we have either a template
9337 declaration, an explicit instantiation, or an explicit
9338 specialization. */
9339 else if (token1.keyword == RID_TEMPLATE)
9340 {
9341 /* `template <>' indicates a template specialization. */
9342 if (token2.type == CPP_LESS
9343 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9344 cp_parser_explicit_specialization (parser);
9345 /* `template <' indicates a template declaration. */
9346 else if (token2.type == CPP_LESS)
9347 cp_parser_template_declaration (parser, /*member_p=*/false);
9348 /* Anything else must be an explicit instantiation. */
9349 else
9350 cp_parser_explicit_instantiation (parser);
9351 }
9352 /* If the next token is `export', then we have a template
9353 declaration. */
9354 else if (token1.keyword == RID_EXPORT)
9355 cp_parser_template_declaration (parser, /*member_p=*/false);
9356 /* If the next token is `extern', 'static' or 'inline' and the one
9357 after that is `template', we have a GNU extended explicit
9358 instantiation directive. */
9359 else if (cp_parser_allow_gnu_extensions_p (parser)
9360 && (token1.keyword == RID_EXTERN
9361 || token1.keyword == RID_STATIC
9362 || token1.keyword == RID_INLINE)
9363 && token2.keyword == RID_TEMPLATE)
9364 cp_parser_explicit_instantiation (parser);
9365 /* If the next token is `namespace', check for a named or unnamed
9366 namespace definition. */
9367 else if (token1.keyword == RID_NAMESPACE
9368 && (/* A named namespace definition. */
9369 (token2.type == CPP_NAME
9370 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9371 != CPP_EQ))
9372 /* An unnamed namespace definition. */
9373 || token2.type == CPP_OPEN_BRACE
9374 || token2.keyword == RID_ATTRIBUTE))
9375 cp_parser_namespace_definition (parser);
9376 /* An inline (associated) namespace definition. */
9377 else if (token1.keyword == RID_INLINE
9378 && token2.keyword == RID_NAMESPACE)
9379 cp_parser_namespace_definition (parser);
9380 /* Objective-C++ declaration/definition. */
9381 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9382 cp_parser_objc_declaration (parser, NULL_TREE);
9383 else if (c_dialect_objc ()
9384 && token1.keyword == RID_ATTRIBUTE
9385 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9386 cp_parser_objc_declaration (parser, attributes);
9387 /* We must have either a block declaration or a function
9388 definition. */
9389 else
9390 /* Try to parse a block-declaration, or a function-definition. */
9391 cp_parser_block_declaration (parser, /*statement_p=*/false);
9392
9393 /* Free any declarators allocated. */
9394 obstack_free (&declarator_obstack, p);
9395 }
9396
9397 /* Parse a block-declaration.
9398
9399 block-declaration:
9400 simple-declaration
9401 asm-definition
9402 namespace-alias-definition
9403 using-declaration
9404 using-directive
9405
9406 GNU Extension:
9407
9408 block-declaration:
9409 __extension__ block-declaration
9410
9411 C++0x Extension:
9412
9413 block-declaration:
9414 static_assert-declaration
9415
9416 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9417 part of a declaration-statement. */
9418
9419 static void
9420 cp_parser_block_declaration (cp_parser *parser,
9421 bool statement_p)
9422 {
9423 cp_token *token1;
9424 int saved_pedantic;
9425
9426 /* Check for the `__extension__' keyword. */
9427 if (cp_parser_extension_opt (parser, &saved_pedantic))
9428 {
9429 /* Parse the qualified declaration. */
9430 cp_parser_block_declaration (parser, statement_p);
9431 /* Restore the PEDANTIC flag. */
9432 pedantic = saved_pedantic;
9433
9434 return;
9435 }
9436
9437 /* Peek at the next token to figure out which kind of declaration is
9438 present. */
9439 token1 = cp_lexer_peek_token (parser->lexer);
9440
9441 /* If the next keyword is `asm', we have an asm-definition. */
9442 if (token1->keyword == RID_ASM)
9443 {
9444 if (statement_p)
9445 cp_parser_commit_to_tentative_parse (parser);
9446 cp_parser_asm_definition (parser);
9447 }
9448 /* If the next keyword is `namespace', we have a
9449 namespace-alias-definition. */
9450 else if (token1->keyword == RID_NAMESPACE)
9451 cp_parser_namespace_alias_definition (parser);
9452 /* If the next keyword is `using', we have either a
9453 using-declaration or a using-directive. */
9454 else if (token1->keyword == RID_USING)
9455 {
9456 cp_token *token2;
9457
9458 if (statement_p)
9459 cp_parser_commit_to_tentative_parse (parser);
9460 /* If the token after `using' is `namespace', then we have a
9461 using-directive. */
9462 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9463 if (token2->keyword == RID_NAMESPACE)
9464 cp_parser_using_directive (parser);
9465 /* Otherwise, it's a using-declaration. */
9466 else
9467 cp_parser_using_declaration (parser,
9468 /*access_declaration_p=*/false);
9469 }
9470 /* If the next keyword is `__label__' we have a misplaced label
9471 declaration. */
9472 else if (token1->keyword == RID_LABEL)
9473 {
9474 cp_lexer_consume_token (parser->lexer);
9475 error_at (token1->location, "%<__label__%> not at the beginning of a block");
9476 cp_parser_skip_to_end_of_statement (parser);
9477 /* If the next token is now a `;', consume it. */
9478 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9479 cp_lexer_consume_token (parser->lexer);
9480 }
9481 /* If the next token is `static_assert' we have a static assertion. */
9482 else if (token1->keyword == RID_STATIC_ASSERT)
9483 cp_parser_static_assert (parser, /*member_p=*/false);
9484 /* Anything else must be a simple-declaration. */
9485 else
9486 cp_parser_simple_declaration (parser, !statement_p);
9487 }
9488
9489 /* Parse a simple-declaration.
9490
9491 simple-declaration:
9492 decl-specifier-seq [opt] init-declarator-list [opt] ;
9493
9494 init-declarator-list:
9495 init-declarator
9496 init-declarator-list , init-declarator
9497
9498 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9499 function-definition as a simple-declaration. */
9500
9501 static void
9502 cp_parser_simple_declaration (cp_parser* parser,
9503 bool function_definition_allowed_p)
9504 {
9505 cp_decl_specifier_seq decl_specifiers;
9506 int declares_class_or_enum;
9507 bool saw_declarator;
9508
9509 /* Defer access checks until we know what is being declared; the
9510 checks for names appearing in the decl-specifier-seq should be
9511 done as if we were in the scope of the thing being declared. */
9512 push_deferring_access_checks (dk_deferred);
9513
9514 /* Parse the decl-specifier-seq. We have to keep track of whether
9515 or not the decl-specifier-seq declares a named class or
9516 enumeration type, since that is the only case in which the
9517 init-declarator-list is allowed to be empty.
9518
9519 [dcl.dcl]
9520
9521 In a simple-declaration, the optional init-declarator-list can be
9522 omitted only when declaring a class or enumeration, that is when
9523 the decl-specifier-seq contains either a class-specifier, an
9524 elaborated-type-specifier, or an enum-specifier. */
9525 cp_parser_decl_specifier_seq (parser,
9526 CP_PARSER_FLAGS_OPTIONAL,
9527 &decl_specifiers,
9528 &declares_class_or_enum);
9529 /* We no longer need to defer access checks. */
9530 stop_deferring_access_checks ();
9531
9532 /* In a block scope, a valid declaration must always have a
9533 decl-specifier-seq. By not trying to parse declarators, we can
9534 resolve the declaration/expression ambiguity more quickly. */
9535 if (!function_definition_allowed_p
9536 && !decl_specifiers.any_specifiers_p)
9537 {
9538 cp_parser_error (parser, "expected declaration");
9539 goto done;
9540 }
9541
9542 /* If the next two tokens are both identifiers, the code is
9543 erroneous. The usual cause of this situation is code like:
9544
9545 T t;
9546
9547 where "T" should name a type -- but does not. */
9548 if (!decl_specifiers.any_type_specifiers_p
9549 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9550 {
9551 /* If parsing tentatively, we should commit; we really are
9552 looking at a declaration. */
9553 cp_parser_commit_to_tentative_parse (parser);
9554 /* Give up. */
9555 goto done;
9556 }
9557
9558 /* If we have seen at least one decl-specifier, and the next token
9559 is not a parenthesis, then we must be looking at a declaration.
9560 (After "int (" we might be looking at a functional cast.) */
9561 if (decl_specifiers.any_specifiers_p
9562 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9563 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9564 && !cp_parser_error_occurred (parser))
9565 cp_parser_commit_to_tentative_parse (parser);
9566
9567 /* Keep going until we hit the `;' at the end of the simple
9568 declaration. */
9569 saw_declarator = false;
9570 while (cp_lexer_next_token_is_not (parser->lexer,
9571 CPP_SEMICOLON))
9572 {
9573 cp_token *token;
9574 bool function_definition_p;
9575 tree decl;
9576
9577 if (saw_declarator)
9578 {
9579 /* If we are processing next declarator, coma is expected */
9580 token = cp_lexer_peek_token (parser->lexer);
9581 gcc_assert (token->type == CPP_COMMA);
9582 cp_lexer_consume_token (parser->lexer);
9583 }
9584 else
9585 saw_declarator = true;
9586
9587 /* Parse the init-declarator. */
9588 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9589 /*checks=*/NULL,
9590 function_definition_allowed_p,
9591 /*member_p=*/false,
9592 declares_class_or_enum,
9593 &function_definition_p);
9594 /* If an error occurred while parsing tentatively, exit quickly.
9595 (That usually happens when in the body of a function; each
9596 statement is treated as a declaration-statement until proven
9597 otherwise.) */
9598 if (cp_parser_error_occurred (parser))
9599 goto done;
9600 /* Handle function definitions specially. */
9601 if (function_definition_p)
9602 {
9603 /* If the next token is a `,', then we are probably
9604 processing something like:
9605
9606 void f() {}, *p;
9607
9608 which is erroneous. */
9609 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9610 {
9611 cp_token *token = cp_lexer_peek_token (parser->lexer);
9612 error_at (token->location,
9613 "mixing"
9614 " declarations and function-definitions is forbidden");
9615 }
9616 /* Otherwise, we're done with the list of declarators. */
9617 else
9618 {
9619 pop_deferring_access_checks ();
9620 return;
9621 }
9622 }
9623 /* The next token should be either a `,' or a `;'. */
9624 token = cp_lexer_peek_token (parser->lexer);
9625 /* If it's a `,', there are more declarators to come. */
9626 if (token->type == CPP_COMMA)
9627 /* will be consumed next time around */;
9628 /* If it's a `;', we are done. */
9629 else if (token->type == CPP_SEMICOLON)
9630 break;
9631 /* Anything else is an error. */
9632 else
9633 {
9634 /* If we have already issued an error message we don't need
9635 to issue another one. */
9636 if (decl != error_mark_node
9637 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9638 cp_parser_error (parser, "expected %<,%> or %<;%>");
9639 /* Skip tokens until we reach the end of the statement. */
9640 cp_parser_skip_to_end_of_statement (parser);
9641 /* If the next token is now a `;', consume it. */
9642 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9643 cp_lexer_consume_token (parser->lexer);
9644 goto done;
9645 }
9646 /* After the first time around, a function-definition is not
9647 allowed -- even if it was OK at first. For example:
9648
9649 int i, f() {}
9650
9651 is not valid. */
9652 function_definition_allowed_p = false;
9653 }
9654
9655 /* Issue an error message if no declarators are present, and the
9656 decl-specifier-seq does not itself declare a class or
9657 enumeration. */
9658 if (!saw_declarator)
9659 {
9660 if (cp_parser_declares_only_class_p (parser))
9661 shadow_tag (&decl_specifiers);
9662 /* Perform any deferred access checks. */
9663 perform_deferred_access_checks ();
9664 }
9665
9666 /* Consume the `;'. */
9667 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9668
9669 done:
9670 pop_deferring_access_checks ();
9671 }
9672
9673 /* Parse a decl-specifier-seq.
9674
9675 decl-specifier-seq:
9676 decl-specifier-seq [opt] decl-specifier
9677
9678 decl-specifier:
9679 storage-class-specifier
9680 type-specifier
9681 function-specifier
9682 friend
9683 typedef
9684
9685 GNU Extension:
9686
9687 decl-specifier:
9688 attributes
9689
9690 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9691
9692 The parser flags FLAGS is used to control type-specifier parsing.
9693
9694 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9695 flags:
9696
9697 1: one of the decl-specifiers is an elaborated-type-specifier
9698 (i.e., a type declaration)
9699 2: one of the decl-specifiers is an enum-specifier or a
9700 class-specifier (i.e., a type definition)
9701
9702 */
9703
9704 static void
9705 cp_parser_decl_specifier_seq (cp_parser* parser,
9706 cp_parser_flags flags,
9707 cp_decl_specifier_seq *decl_specs,
9708 int* declares_class_or_enum)
9709 {
9710 bool constructor_possible_p = !parser->in_declarator_p;
9711 cp_token *start_token = NULL;
9712
9713 /* Clear DECL_SPECS. */
9714 clear_decl_specs (decl_specs);
9715
9716 /* Assume no class or enumeration type is declared. */
9717 *declares_class_or_enum = 0;
9718
9719 /* Keep reading specifiers until there are no more to read. */
9720 while (true)
9721 {
9722 bool constructor_p;
9723 bool found_decl_spec;
9724 cp_token *token;
9725
9726 /* Peek at the next token. */
9727 token = cp_lexer_peek_token (parser->lexer);
9728
9729 /* Save the first token of the decl spec list for error
9730 reporting. */
9731 if (!start_token)
9732 start_token = token;
9733 /* Handle attributes. */
9734 if (token->keyword == RID_ATTRIBUTE)
9735 {
9736 /* Parse the attributes. */
9737 decl_specs->attributes
9738 = chainon (decl_specs->attributes,
9739 cp_parser_attributes_opt (parser));
9740 continue;
9741 }
9742 /* Assume we will find a decl-specifier keyword. */
9743 found_decl_spec = true;
9744 /* If the next token is an appropriate keyword, we can simply
9745 add it to the list. */
9746 switch (token->keyword)
9747 {
9748 /* decl-specifier:
9749 friend
9750 constexpr */
9751 case RID_FRIEND:
9752 if (!at_class_scope_p ())
9753 {
9754 error_at (token->location, "%<friend%> used outside of class");
9755 cp_lexer_purge_token (parser->lexer);
9756 }
9757 else
9758 {
9759 ++decl_specs->specs[(int) ds_friend];
9760 /* Consume the token. */
9761 cp_lexer_consume_token (parser->lexer);
9762 }
9763 break;
9764
9765 case RID_CONSTEXPR:
9766 ++decl_specs->specs[(int) ds_constexpr];
9767 cp_lexer_consume_token (parser->lexer);
9768 break;
9769
9770 /* function-specifier:
9771 inline
9772 virtual
9773 explicit */
9774 case RID_INLINE:
9775 case RID_VIRTUAL:
9776 case RID_EXPLICIT:
9777 cp_parser_function_specifier_opt (parser, decl_specs);
9778 break;
9779
9780 /* decl-specifier:
9781 typedef */
9782 case RID_TYPEDEF:
9783 ++decl_specs->specs[(int) ds_typedef];
9784 /* Consume the token. */
9785 cp_lexer_consume_token (parser->lexer);
9786 /* A constructor declarator cannot appear in a typedef. */
9787 constructor_possible_p = false;
9788 /* The "typedef" keyword can only occur in a declaration; we
9789 may as well commit at this point. */
9790 cp_parser_commit_to_tentative_parse (parser);
9791
9792 if (decl_specs->storage_class != sc_none)
9793 decl_specs->conflicting_specifiers_p = true;
9794 break;
9795
9796 /* storage-class-specifier:
9797 auto
9798 register
9799 static
9800 extern
9801 mutable
9802
9803 GNU Extension:
9804 thread */
9805 case RID_AUTO:
9806 if (cxx_dialect == cxx98)
9807 {
9808 /* Consume the token. */
9809 cp_lexer_consume_token (parser->lexer);
9810
9811 /* Complain about `auto' as a storage specifier, if
9812 we're complaining about C++0x compatibility. */
9813 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9814 " will change meaning in C++0x; please remove it");
9815
9816 /* Set the storage class anyway. */
9817 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9818 token->location);
9819 }
9820 else
9821 /* C++0x auto type-specifier. */
9822 found_decl_spec = false;
9823 break;
9824
9825 case RID_REGISTER:
9826 case RID_STATIC:
9827 case RID_EXTERN:
9828 case RID_MUTABLE:
9829 /* Consume the token. */
9830 cp_lexer_consume_token (parser->lexer);
9831 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9832 token->location);
9833 break;
9834 case RID_THREAD:
9835 /* Consume the token. */
9836 cp_lexer_consume_token (parser->lexer);
9837 ++decl_specs->specs[(int) ds_thread];
9838 break;
9839
9840 default:
9841 /* We did not yet find a decl-specifier yet. */
9842 found_decl_spec = false;
9843 break;
9844 }
9845
9846 /* Constructors are a special case. The `S' in `S()' is not a
9847 decl-specifier; it is the beginning of the declarator. */
9848 constructor_p
9849 = (!found_decl_spec
9850 && constructor_possible_p
9851 && (cp_parser_constructor_declarator_p
9852 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9853
9854 /* If we don't have a DECL_SPEC yet, then we must be looking at
9855 a type-specifier. */
9856 if (!found_decl_spec && !constructor_p)
9857 {
9858 int decl_spec_declares_class_or_enum;
9859 bool is_cv_qualifier;
9860 tree type_spec;
9861
9862 type_spec
9863 = cp_parser_type_specifier (parser, flags,
9864 decl_specs,
9865 /*is_declaration=*/true,
9866 &decl_spec_declares_class_or_enum,
9867 &is_cv_qualifier);
9868 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9869
9870 /* If this type-specifier referenced a user-defined type
9871 (a typedef, class-name, etc.), then we can't allow any
9872 more such type-specifiers henceforth.
9873
9874 [dcl.spec]
9875
9876 The longest sequence of decl-specifiers that could
9877 possibly be a type name is taken as the
9878 decl-specifier-seq of a declaration. The sequence shall
9879 be self-consistent as described below.
9880
9881 [dcl.type]
9882
9883 As a general rule, at most one type-specifier is allowed
9884 in the complete decl-specifier-seq of a declaration. The
9885 only exceptions are the following:
9886
9887 -- const or volatile can be combined with any other
9888 type-specifier.
9889
9890 -- signed or unsigned can be combined with char, long,
9891 short, or int.
9892
9893 -- ..
9894
9895 Example:
9896
9897 typedef char* Pc;
9898 void g (const int Pc);
9899
9900 Here, Pc is *not* part of the decl-specifier seq; it's
9901 the declarator. Therefore, once we see a type-specifier
9902 (other than a cv-qualifier), we forbid any additional
9903 user-defined types. We *do* still allow things like `int
9904 int' to be considered a decl-specifier-seq, and issue the
9905 error message later. */
9906 if (type_spec && !is_cv_qualifier)
9907 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9908 /* A constructor declarator cannot follow a type-specifier. */
9909 if (type_spec)
9910 {
9911 constructor_possible_p = false;
9912 found_decl_spec = true;
9913 if (!is_cv_qualifier)
9914 decl_specs->any_type_specifiers_p = true;
9915 }
9916 }
9917
9918 /* If we still do not have a DECL_SPEC, then there are no more
9919 decl-specifiers. */
9920 if (!found_decl_spec)
9921 break;
9922
9923 decl_specs->any_specifiers_p = true;
9924 /* After we see one decl-specifier, further decl-specifiers are
9925 always optional. */
9926 flags |= CP_PARSER_FLAGS_OPTIONAL;
9927 }
9928
9929 cp_parser_check_decl_spec (decl_specs, start_token->location);
9930
9931 /* Don't allow a friend specifier with a class definition. */
9932 if (decl_specs->specs[(int) ds_friend] != 0
9933 && (*declares_class_or_enum & 2))
9934 error_at (start_token->location,
9935 "class definition may not be declared a friend");
9936 }
9937
9938 /* Parse an (optional) storage-class-specifier.
9939
9940 storage-class-specifier:
9941 auto
9942 register
9943 static
9944 extern
9945 mutable
9946
9947 GNU Extension:
9948
9949 storage-class-specifier:
9950 thread
9951
9952 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9953
9954 static tree
9955 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9956 {
9957 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9958 {
9959 case RID_AUTO:
9960 if (cxx_dialect != cxx98)
9961 return NULL_TREE;
9962 /* Fall through for C++98. */
9963
9964 case RID_REGISTER:
9965 case RID_STATIC:
9966 case RID_EXTERN:
9967 case RID_MUTABLE:
9968 case RID_THREAD:
9969 /* Consume the token. */
9970 return cp_lexer_consume_token (parser->lexer)->u.value;
9971
9972 default:
9973 return NULL_TREE;
9974 }
9975 }
9976
9977 /* Parse an (optional) function-specifier.
9978
9979 function-specifier:
9980 inline
9981 virtual
9982 explicit
9983
9984 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9985 Updates DECL_SPECS, if it is non-NULL. */
9986
9987 static tree
9988 cp_parser_function_specifier_opt (cp_parser* parser,
9989 cp_decl_specifier_seq *decl_specs)
9990 {
9991 cp_token *token = cp_lexer_peek_token (parser->lexer);
9992 switch (token->keyword)
9993 {
9994 case RID_INLINE:
9995 if (decl_specs)
9996 ++decl_specs->specs[(int) ds_inline];
9997 break;
9998
9999 case RID_VIRTUAL:
10000 /* 14.5.2.3 [temp.mem]
10001
10002 A member function template shall not be virtual. */
10003 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10004 error_at (token->location, "templates may not be %<virtual%>");
10005 else if (decl_specs)
10006 ++decl_specs->specs[(int) ds_virtual];
10007 break;
10008
10009 case RID_EXPLICIT:
10010 if (decl_specs)
10011 ++decl_specs->specs[(int) ds_explicit];
10012 break;
10013
10014 default:
10015 return NULL_TREE;
10016 }
10017
10018 /* Consume the token. */
10019 return cp_lexer_consume_token (parser->lexer)->u.value;
10020 }
10021
10022 /* Parse a linkage-specification.
10023
10024 linkage-specification:
10025 extern string-literal { declaration-seq [opt] }
10026 extern string-literal declaration */
10027
10028 static void
10029 cp_parser_linkage_specification (cp_parser* parser)
10030 {
10031 tree linkage;
10032
10033 /* Look for the `extern' keyword. */
10034 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10035
10036 /* Look for the string-literal. */
10037 linkage = cp_parser_string_literal (parser, false, false);
10038
10039 /* Transform the literal into an identifier. If the literal is a
10040 wide-character string, or contains embedded NULs, then we can't
10041 handle it as the user wants. */
10042 if (strlen (TREE_STRING_POINTER (linkage))
10043 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10044 {
10045 cp_parser_error (parser, "invalid linkage-specification");
10046 /* Assume C++ linkage. */
10047 linkage = lang_name_cplusplus;
10048 }
10049 else
10050 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10051
10052 /* We're now using the new linkage. */
10053 push_lang_context (linkage);
10054
10055 /* If the next token is a `{', then we're using the first
10056 production. */
10057 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10058 {
10059 /* Consume the `{' token. */
10060 cp_lexer_consume_token (parser->lexer);
10061 /* Parse the declarations. */
10062 cp_parser_declaration_seq_opt (parser);
10063 /* Look for the closing `}'. */
10064 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10065 }
10066 /* Otherwise, there's just one declaration. */
10067 else
10068 {
10069 bool saved_in_unbraced_linkage_specification_p;
10070
10071 saved_in_unbraced_linkage_specification_p
10072 = parser->in_unbraced_linkage_specification_p;
10073 parser->in_unbraced_linkage_specification_p = true;
10074 cp_parser_declaration (parser);
10075 parser->in_unbraced_linkage_specification_p
10076 = saved_in_unbraced_linkage_specification_p;
10077 }
10078
10079 /* We're done with the linkage-specification. */
10080 pop_lang_context ();
10081 }
10082
10083 /* Parse a static_assert-declaration.
10084
10085 static_assert-declaration:
10086 static_assert ( constant-expression , string-literal ) ;
10087
10088 If MEMBER_P, this static_assert is a class member. */
10089
10090 static void
10091 cp_parser_static_assert(cp_parser *parser, bool member_p)
10092 {
10093 tree condition;
10094 tree message;
10095 cp_token *token;
10096 location_t saved_loc;
10097
10098 /* Peek at the `static_assert' token so we can keep track of exactly
10099 where the static assertion started. */
10100 token = cp_lexer_peek_token (parser->lexer);
10101 saved_loc = token->location;
10102
10103 /* Look for the `static_assert' keyword. */
10104 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10105 RT_STATIC_ASSERT))
10106 return;
10107
10108 /* We know we are in a static assertion; commit to any tentative
10109 parse. */
10110 if (cp_parser_parsing_tentatively (parser))
10111 cp_parser_commit_to_tentative_parse (parser);
10112
10113 /* Parse the `(' starting the static assertion condition. */
10114 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10115
10116 /* Parse the constant-expression. */
10117 condition =
10118 cp_parser_constant_expression (parser,
10119 /*allow_non_constant_p=*/false,
10120 /*non_constant_p=*/NULL);
10121
10122 /* Parse the separating `,'. */
10123 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10124
10125 /* Parse the string-literal message. */
10126 message = cp_parser_string_literal (parser,
10127 /*translate=*/false,
10128 /*wide_ok=*/true);
10129
10130 /* A `)' completes the static assertion. */
10131 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10132 cp_parser_skip_to_closing_parenthesis (parser,
10133 /*recovering=*/true,
10134 /*or_comma=*/false,
10135 /*consume_paren=*/true);
10136
10137 /* A semicolon terminates the declaration. */
10138 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10139
10140 /* Complete the static assertion, which may mean either processing
10141 the static assert now or saving it for template instantiation. */
10142 finish_static_assert (condition, message, saved_loc, member_p);
10143 }
10144
10145 /* Parse a `decltype' type. Returns the type.
10146
10147 simple-type-specifier:
10148 decltype ( expression ) */
10149
10150 static tree
10151 cp_parser_decltype (cp_parser *parser)
10152 {
10153 tree expr;
10154 bool id_expression_or_member_access_p = false;
10155 const char *saved_message;
10156 bool saved_integral_constant_expression_p;
10157 bool saved_non_integral_constant_expression_p;
10158 cp_token *id_expr_start_token;
10159
10160 /* Look for the `decltype' token. */
10161 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10162 return error_mark_node;
10163
10164 /* Types cannot be defined in a `decltype' expression. Save away the
10165 old message. */
10166 saved_message = parser->type_definition_forbidden_message;
10167
10168 /* And create the new one. */
10169 parser->type_definition_forbidden_message
10170 = G_("types may not be defined in %<decltype%> expressions");
10171
10172 /* The restrictions on constant-expressions do not apply inside
10173 decltype expressions. */
10174 saved_integral_constant_expression_p
10175 = parser->integral_constant_expression_p;
10176 saved_non_integral_constant_expression_p
10177 = parser->non_integral_constant_expression_p;
10178 parser->integral_constant_expression_p = false;
10179
10180 /* Do not actually evaluate the expression. */
10181 ++cp_unevaluated_operand;
10182
10183 /* Do not warn about problems with the expression. */
10184 ++c_inhibit_evaluation_warnings;
10185
10186 /* Parse the opening `('. */
10187 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10188 return error_mark_node;
10189
10190 /* First, try parsing an id-expression. */
10191 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10192 cp_parser_parse_tentatively (parser);
10193 expr = cp_parser_id_expression (parser,
10194 /*template_keyword_p=*/false,
10195 /*check_dependency_p=*/true,
10196 /*template_p=*/NULL,
10197 /*declarator_p=*/false,
10198 /*optional_p=*/false);
10199
10200 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10201 {
10202 bool non_integral_constant_expression_p = false;
10203 tree id_expression = expr;
10204 cp_id_kind idk;
10205 const char *error_msg;
10206
10207 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10208 /* Lookup the name we got back from the id-expression. */
10209 expr = cp_parser_lookup_name (parser, expr,
10210 none_type,
10211 /*is_template=*/false,
10212 /*is_namespace=*/false,
10213 /*check_dependency=*/true,
10214 /*ambiguous_decls=*/NULL,
10215 id_expr_start_token->location);
10216
10217 if (expr
10218 && expr != error_mark_node
10219 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10220 && TREE_CODE (expr) != TYPE_DECL
10221 && (TREE_CODE (expr) != BIT_NOT_EXPR
10222 || !TYPE_P (TREE_OPERAND (expr, 0)))
10223 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10224 {
10225 /* Complete lookup of the id-expression. */
10226 expr = (finish_id_expression
10227 (id_expression, expr, parser->scope, &idk,
10228 /*integral_constant_expression_p=*/false,
10229 /*allow_non_integral_constant_expression_p=*/true,
10230 &non_integral_constant_expression_p,
10231 /*template_p=*/false,
10232 /*done=*/true,
10233 /*address_p=*/false,
10234 /*template_arg_p=*/false,
10235 &error_msg,
10236 id_expr_start_token->location));
10237
10238 if (expr == error_mark_node)
10239 /* We found an id-expression, but it was something that we
10240 should not have found. This is an error, not something
10241 we can recover from, so note that we found an
10242 id-expression and we'll recover as gracefully as
10243 possible. */
10244 id_expression_or_member_access_p = true;
10245 }
10246
10247 if (expr
10248 && expr != error_mark_node
10249 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10250 /* We have an id-expression. */
10251 id_expression_or_member_access_p = true;
10252 }
10253
10254 if (!id_expression_or_member_access_p)
10255 {
10256 /* Abort the id-expression parse. */
10257 cp_parser_abort_tentative_parse (parser);
10258
10259 /* Parsing tentatively, again. */
10260 cp_parser_parse_tentatively (parser);
10261
10262 /* Parse a class member access. */
10263 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10264 /*cast_p=*/false,
10265 /*member_access_only_p=*/true, NULL);
10266
10267 if (expr
10268 && expr != error_mark_node
10269 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10270 /* We have an id-expression. */
10271 id_expression_or_member_access_p = true;
10272 }
10273
10274 if (id_expression_or_member_access_p)
10275 /* We have parsed the complete id-expression or member access. */
10276 cp_parser_parse_definitely (parser);
10277 else
10278 {
10279 bool saved_greater_than_is_operator_p;
10280
10281 /* Abort our attempt to parse an id-expression or member access
10282 expression. */
10283 cp_parser_abort_tentative_parse (parser);
10284
10285 /* Within a parenthesized expression, a `>' token is always
10286 the greater-than operator. */
10287 saved_greater_than_is_operator_p
10288 = parser->greater_than_is_operator_p;
10289 parser->greater_than_is_operator_p = true;
10290
10291 /* Parse a full expression. */
10292 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10293
10294 /* The `>' token might be the end of a template-id or
10295 template-parameter-list now. */
10296 parser->greater_than_is_operator_p
10297 = saved_greater_than_is_operator_p;
10298 }
10299
10300 /* Go back to evaluating expressions. */
10301 --cp_unevaluated_operand;
10302 --c_inhibit_evaluation_warnings;
10303
10304 /* Restore the old message and the integral constant expression
10305 flags. */
10306 parser->type_definition_forbidden_message = saved_message;
10307 parser->integral_constant_expression_p
10308 = saved_integral_constant_expression_p;
10309 parser->non_integral_constant_expression_p
10310 = saved_non_integral_constant_expression_p;
10311
10312 if (expr == error_mark_node)
10313 {
10314 /* Skip everything up to the closing `)'. */
10315 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10316 /*consume_paren=*/true);
10317 return error_mark_node;
10318 }
10319
10320 /* Parse to the closing `)'. */
10321 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10322 {
10323 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10324 /*consume_paren=*/true);
10325 return error_mark_node;
10326 }
10327
10328 return finish_decltype_type (expr, id_expression_or_member_access_p);
10329 }
10330
10331 /* Special member functions [gram.special] */
10332
10333 /* Parse a conversion-function-id.
10334
10335 conversion-function-id:
10336 operator conversion-type-id
10337
10338 Returns an IDENTIFIER_NODE representing the operator. */
10339
10340 static tree
10341 cp_parser_conversion_function_id (cp_parser* parser)
10342 {
10343 tree type;
10344 tree saved_scope;
10345 tree saved_qualifying_scope;
10346 tree saved_object_scope;
10347 tree pushed_scope = NULL_TREE;
10348
10349 /* Look for the `operator' token. */
10350 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10351 return error_mark_node;
10352 /* When we parse the conversion-type-id, the current scope will be
10353 reset. However, we need that information in able to look up the
10354 conversion function later, so we save it here. */
10355 saved_scope = parser->scope;
10356 saved_qualifying_scope = parser->qualifying_scope;
10357 saved_object_scope = parser->object_scope;
10358 /* We must enter the scope of the class so that the names of
10359 entities declared within the class are available in the
10360 conversion-type-id. For example, consider:
10361
10362 struct S {
10363 typedef int I;
10364 operator I();
10365 };
10366
10367 S::operator I() { ... }
10368
10369 In order to see that `I' is a type-name in the definition, we
10370 must be in the scope of `S'. */
10371 if (saved_scope)
10372 pushed_scope = push_scope (saved_scope);
10373 /* Parse the conversion-type-id. */
10374 type = cp_parser_conversion_type_id (parser);
10375 /* Leave the scope of the class, if any. */
10376 if (pushed_scope)
10377 pop_scope (pushed_scope);
10378 /* Restore the saved scope. */
10379 parser->scope = saved_scope;
10380 parser->qualifying_scope = saved_qualifying_scope;
10381 parser->object_scope = saved_object_scope;
10382 /* If the TYPE is invalid, indicate failure. */
10383 if (type == error_mark_node)
10384 return error_mark_node;
10385 return mangle_conv_op_name_for_type (type);
10386 }
10387
10388 /* Parse a conversion-type-id:
10389
10390 conversion-type-id:
10391 type-specifier-seq conversion-declarator [opt]
10392
10393 Returns the TYPE specified. */
10394
10395 static tree
10396 cp_parser_conversion_type_id (cp_parser* parser)
10397 {
10398 tree attributes;
10399 cp_decl_specifier_seq type_specifiers;
10400 cp_declarator *declarator;
10401 tree type_specified;
10402
10403 /* Parse the attributes. */
10404 attributes = cp_parser_attributes_opt (parser);
10405 /* Parse the type-specifiers. */
10406 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10407 /*is_trailing_return=*/false,
10408 &type_specifiers);
10409 /* If that didn't work, stop. */
10410 if (type_specifiers.type == error_mark_node)
10411 return error_mark_node;
10412 /* Parse the conversion-declarator. */
10413 declarator = cp_parser_conversion_declarator_opt (parser);
10414
10415 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10416 /*initialized=*/0, &attributes);
10417 if (attributes)
10418 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10419
10420 /* Don't give this error when parsing tentatively. This happens to
10421 work because we always parse this definitively once. */
10422 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10423 && type_uses_auto (type_specified))
10424 {
10425 error ("invalid use of %<auto%> in conversion operator");
10426 return error_mark_node;
10427 }
10428
10429 return type_specified;
10430 }
10431
10432 /* Parse an (optional) conversion-declarator.
10433
10434 conversion-declarator:
10435 ptr-operator conversion-declarator [opt]
10436
10437 */
10438
10439 static cp_declarator *
10440 cp_parser_conversion_declarator_opt (cp_parser* parser)
10441 {
10442 enum tree_code code;
10443 tree class_type;
10444 cp_cv_quals cv_quals;
10445
10446 /* We don't know if there's a ptr-operator next, or not. */
10447 cp_parser_parse_tentatively (parser);
10448 /* Try the ptr-operator. */
10449 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10450 /* If it worked, look for more conversion-declarators. */
10451 if (cp_parser_parse_definitely (parser))
10452 {
10453 cp_declarator *declarator;
10454
10455 /* Parse another optional declarator. */
10456 declarator = cp_parser_conversion_declarator_opt (parser);
10457
10458 return cp_parser_make_indirect_declarator
10459 (code, class_type, cv_quals, declarator);
10460 }
10461
10462 return NULL;
10463 }
10464
10465 /* Parse an (optional) ctor-initializer.
10466
10467 ctor-initializer:
10468 : mem-initializer-list
10469
10470 Returns TRUE iff the ctor-initializer was actually present. */
10471
10472 static bool
10473 cp_parser_ctor_initializer_opt (cp_parser* parser)
10474 {
10475 /* If the next token is not a `:', then there is no
10476 ctor-initializer. */
10477 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10478 {
10479 /* Do default initialization of any bases and members. */
10480 if (DECL_CONSTRUCTOR_P (current_function_decl))
10481 finish_mem_initializers (NULL_TREE);
10482
10483 return false;
10484 }
10485
10486 /* Consume the `:' token. */
10487 cp_lexer_consume_token (parser->lexer);
10488 /* And the mem-initializer-list. */
10489 cp_parser_mem_initializer_list (parser);
10490
10491 return true;
10492 }
10493
10494 /* Parse a mem-initializer-list.
10495
10496 mem-initializer-list:
10497 mem-initializer ... [opt]
10498 mem-initializer ... [opt] , mem-initializer-list */
10499
10500 static void
10501 cp_parser_mem_initializer_list (cp_parser* parser)
10502 {
10503 tree mem_initializer_list = NULL_TREE;
10504 cp_token *token = cp_lexer_peek_token (parser->lexer);
10505
10506 /* Let the semantic analysis code know that we are starting the
10507 mem-initializer-list. */
10508 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10509 error_at (token->location,
10510 "only constructors take member initializers");
10511
10512 /* Loop through the list. */
10513 while (true)
10514 {
10515 tree mem_initializer;
10516
10517 token = cp_lexer_peek_token (parser->lexer);
10518 /* Parse the mem-initializer. */
10519 mem_initializer = cp_parser_mem_initializer (parser);
10520 /* If the next token is a `...', we're expanding member initializers. */
10521 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10522 {
10523 /* Consume the `...'. */
10524 cp_lexer_consume_token (parser->lexer);
10525
10526 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10527 can be expanded but members cannot. */
10528 if (mem_initializer != error_mark_node
10529 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10530 {
10531 error_at (token->location,
10532 "cannot expand initializer for member %<%D%>",
10533 TREE_PURPOSE (mem_initializer));
10534 mem_initializer = error_mark_node;
10535 }
10536
10537 /* Construct the pack expansion type. */
10538 if (mem_initializer != error_mark_node)
10539 mem_initializer = make_pack_expansion (mem_initializer);
10540 }
10541 /* Add it to the list, unless it was erroneous. */
10542 if (mem_initializer != error_mark_node)
10543 {
10544 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10545 mem_initializer_list = mem_initializer;
10546 }
10547 /* If the next token is not a `,', we're done. */
10548 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10549 break;
10550 /* Consume the `,' token. */
10551 cp_lexer_consume_token (parser->lexer);
10552 }
10553
10554 /* Perform semantic analysis. */
10555 if (DECL_CONSTRUCTOR_P (current_function_decl))
10556 finish_mem_initializers (mem_initializer_list);
10557 }
10558
10559 /* Parse a mem-initializer.
10560
10561 mem-initializer:
10562 mem-initializer-id ( expression-list [opt] )
10563 mem-initializer-id braced-init-list
10564
10565 GNU extension:
10566
10567 mem-initializer:
10568 ( expression-list [opt] )
10569
10570 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10571 class) or FIELD_DECL (for a non-static data member) to initialize;
10572 the TREE_VALUE is the expression-list. An empty initialization
10573 list is represented by void_list_node. */
10574
10575 static tree
10576 cp_parser_mem_initializer (cp_parser* parser)
10577 {
10578 tree mem_initializer_id;
10579 tree expression_list;
10580 tree member;
10581 cp_token *token = cp_lexer_peek_token (parser->lexer);
10582
10583 /* Find out what is being initialized. */
10584 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10585 {
10586 permerror (token->location,
10587 "anachronistic old-style base class initializer");
10588 mem_initializer_id = NULL_TREE;
10589 }
10590 else
10591 {
10592 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10593 if (mem_initializer_id == error_mark_node)
10594 return mem_initializer_id;
10595 }
10596 member = expand_member_init (mem_initializer_id);
10597 if (member && !DECL_P (member))
10598 in_base_initializer = 1;
10599
10600 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10601 {
10602 bool expr_non_constant_p;
10603 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10604 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10605 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10606 expression_list = build_tree_list (NULL_TREE, expression_list);
10607 }
10608 else
10609 {
10610 VEC(tree,gc)* vec;
10611 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10612 /*cast_p=*/false,
10613 /*allow_expansion_p=*/true,
10614 /*non_constant_p=*/NULL);
10615 if (vec == NULL)
10616 return error_mark_node;
10617 expression_list = build_tree_list_vec (vec);
10618 release_tree_vector (vec);
10619 }
10620
10621 if (expression_list == error_mark_node)
10622 return error_mark_node;
10623 if (!expression_list)
10624 expression_list = void_type_node;
10625
10626 in_base_initializer = 0;
10627
10628 return member ? build_tree_list (member, expression_list) : error_mark_node;
10629 }
10630
10631 /* Parse a mem-initializer-id.
10632
10633 mem-initializer-id:
10634 :: [opt] nested-name-specifier [opt] class-name
10635 identifier
10636
10637 Returns a TYPE indicating the class to be initializer for the first
10638 production. Returns an IDENTIFIER_NODE indicating the data member
10639 to be initialized for the second production. */
10640
10641 static tree
10642 cp_parser_mem_initializer_id (cp_parser* parser)
10643 {
10644 bool global_scope_p;
10645 bool nested_name_specifier_p;
10646 bool template_p = false;
10647 tree id;
10648
10649 cp_token *token = cp_lexer_peek_token (parser->lexer);
10650
10651 /* `typename' is not allowed in this context ([temp.res]). */
10652 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10653 {
10654 error_at (token->location,
10655 "keyword %<typename%> not allowed in this context (a qualified "
10656 "member initializer is implicitly a type)");
10657 cp_lexer_consume_token (parser->lexer);
10658 }
10659 /* Look for the optional `::' operator. */
10660 global_scope_p
10661 = (cp_parser_global_scope_opt (parser,
10662 /*current_scope_valid_p=*/false)
10663 != NULL_TREE);
10664 /* Look for the optional nested-name-specifier. The simplest way to
10665 implement:
10666
10667 [temp.res]
10668
10669 The keyword `typename' is not permitted in a base-specifier or
10670 mem-initializer; in these contexts a qualified name that
10671 depends on a template-parameter is implicitly assumed to be a
10672 type name.
10673
10674 is to assume that we have seen the `typename' keyword at this
10675 point. */
10676 nested_name_specifier_p
10677 = (cp_parser_nested_name_specifier_opt (parser,
10678 /*typename_keyword_p=*/true,
10679 /*check_dependency_p=*/true,
10680 /*type_p=*/true,
10681 /*is_declaration=*/true)
10682 != NULL_TREE);
10683 if (nested_name_specifier_p)
10684 template_p = cp_parser_optional_template_keyword (parser);
10685 /* If there is a `::' operator or a nested-name-specifier, then we
10686 are definitely looking for a class-name. */
10687 if (global_scope_p || nested_name_specifier_p)
10688 return cp_parser_class_name (parser,
10689 /*typename_keyword_p=*/true,
10690 /*template_keyword_p=*/template_p,
10691 typename_type,
10692 /*check_dependency_p=*/true,
10693 /*class_head_p=*/false,
10694 /*is_declaration=*/true);
10695 /* Otherwise, we could also be looking for an ordinary identifier. */
10696 cp_parser_parse_tentatively (parser);
10697 /* Try a class-name. */
10698 id = cp_parser_class_name (parser,
10699 /*typename_keyword_p=*/true,
10700 /*template_keyword_p=*/false,
10701 none_type,
10702 /*check_dependency_p=*/true,
10703 /*class_head_p=*/false,
10704 /*is_declaration=*/true);
10705 /* If we found one, we're done. */
10706 if (cp_parser_parse_definitely (parser))
10707 return id;
10708 /* Otherwise, look for an ordinary identifier. */
10709 return cp_parser_identifier (parser);
10710 }
10711
10712 /* Overloading [gram.over] */
10713
10714 /* Parse an operator-function-id.
10715
10716 operator-function-id:
10717 operator operator
10718
10719 Returns an IDENTIFIER_NODE for the operator which is a
10720 human-readable spelling of the identifier, e.g., `operator +'. */
10721
10722 static tree
10723 cp_parser_operator_function_id (cp_parser* parser)
10724 {
10725 /* Look for the `operator' keyword. */
10726 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10727 return error_mark_node;
10728 /* And then the name of the operator itself. */
10729 return cp_parser_operator (parser);
10730 }
10731
10732 /* Parse an operator.
10733
10734 operator:
10735 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10736 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10737 || ++ -- , ->* -> () []
10738
10739 GNU Extensions:
10740
10741 operator:
10742 <? >? <?= >?=
10743
10744 Returns an IDENTIFIER_NODE for the operator which is a
10745 human-readable spelling of the identifier, e.g., `operator +'. */
10746
10747 static tree
10748 cp_parser_operator (cp_parser* parser)
10749 {
10750 tree id = NULL_TREE;
10751 cp_token *token;
10752
10753 /* Peek at the next token. */
10754 token = cp_lexer_peek_token (parser->lexer);
10755 /* Figure out which operator we have. */
10756 switch (token->type)
10757 {
10758 case CPP_KEYWORD:
10759 {
10760 enum tree_code op;
10761
10762 /* The keyword should be either `new' or `delete'. */
10763 if (token->keyword == RID_NEW)
10764 op = NEW_EXPR;
10765 else if (token->keyword == RID_DELETE)
10766 op = DELETE_EXPR;
10767 else
10768 break;
10769
10770 /* Consume the `new' or `delete' token. */
10771 cp_lexer_consume_token (parser->lexer);
10772
10773 /* Peek at the next token. */
10774 token = cp_lexer_peek_token (parser->lexer);
10775 /* If it's a `[' token then this is the array variant of the
10776 operator. */
10777 if (token->type == CPP_OPEN_SQUARE)
10778 {
10779 /* Consume the `[' token. */
10780 cp_lexer_consume_token (parser->lexer);
10781 /* Look for the `]' token. */
10782 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10783 id = ansi_opname (op == NEW_EXPR
10784 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10785 }
10786 /* Otherwise, we have the non-array variant. */
10787 else
10788 id = ansi_opname (op);
10789
10790 return id;
10791 }
10792
10793 case CPP_PLUS:
10794 id = ansi_opname (PLUS_EXPR);
10795 break;
10796
10797 case CPP_MINUS:
10798 id = ansi_opname (MINUS_EXPR);
10799 break;
10800
10801 case CPP_MULT:
10802 id = ansi_opname (MULT_EXPR);
10803 break;
10804
10805 case CPP_DIV:
10806 id = ansi_opname (TRUNC_DIV_EXPR);
10807 break;
10808
10809 case CPP_MOD:
10810 id = ansi_opname (TRUNC_MOD_EXPR);
10811 break;
10812
10813 case CPP_XOR:
10814 id = ansi_opname (BIT_XOR_EXPR);
10815 break;
10816
10817 case CPP_AND:
10818 id = ansi_opname (BIT_AND_EXPR);
10819 break;
10820
10821 case CPP_OR:
10822 id = ansi_opname (BIT_IOR_EXPR);
10823 break;
10824
10825 case CPP_COMPL:
10826 id = ansi_opname (BIT_NOT_EXPR);
10827 break;
10828
10829 case CPP_NOT:
10830 id = ansi_opname (TRUTH_NOT_EXPR);
10831 break;
10832
10833 case CPP_EQ:
10834 id = ansi_assopname (NOP_EXPR);
10835 break;
10836
10837 case CPP_LESS:
10838 id = ansi_opname (LT_EXPR);
10839 break;
10840
10841 case CPP_GREATER:
10842 id = ansi_opname (GT_EXPR);
10843 break;
10844
10845 case CPP_PLUS_EQ:
10846 id = ansi_assopname (PLUS_EXPR);
10847 break;
10848
10849 case CPP_MINUS_EQ:
10850 id = ansi_assopname (MINUS_EXPR);
10851 break;
10852
10853 case CPP_MULT_EQ:
10854 id = ansi_assopname (MULT_EXPR);
10855 break;
10856
10857 case CPP_DIV_EQ:
10858 id = ansi_assopname (TRUNC_DIV_EXPR);
10859 break;
10860
10861 case CPP_MOD_EQ:
10862 id = ansi_assopname (TRUNC_MOD_EXPR);
10863 break;
10864
10865 case CPP_XOR_EQ:
10866 id = ansi_assopname (BIT_XOR_EXPR);
10867 break;
10868
10869 case CPP_AND_EQ:
10870 id = ansi_assopname (BIT_AND_EXPR);
10871 break;
10872
10873 case CPP_OR_EQ:
10874 id = ansi_assopname (BIT_IOR_EXPR);
10875 break;
10876
10877 case CPP_LSHIFT:
10878 id = ansi_opname (LSHIFT_EXPR);
10879 break;
10880
10881 case CPP_RSHIFT:
10882 id = ansi_opname (RSHIFT_EXPR);
10883 break;
10884
10885 case CPP_LSHIFT_EQ:
10886 id = ansi_assopname (LSHIFT_EXPR);
10887 break;
10888
10889 case CPP_RSHIFT_EQ:
10890 id = ansi_assopname (RSHIFT_EXPR);
10891 break;
10892
10893 case CPP_EQ_EQ:
10894 id = ansi_opname (EQ_EXPR);
10895 break;
10896
10897 case CPP_NOT_EQ:
10898 id = ansi_opname (NE_EXPR);
10899 break;
10900
10901 case CPP_LESS_EQ:
10902 id = ansi_opname (LE_EXPR);
10903 break;
10904
10905 case CPP_GREATER_EQ:
10906 id = ansi_opname (GE_EXPR);
10907 break;
10908
10909 case CPP_AND_AND:
10910 id = ansi_opname (TRUTH_ANDIF_EXPR);
10911 break;
10912
10913 case CPP_OR_OR:
10914 id = ansi_opname (TRUTH_ORIF_EXPR);
10915 break;
10916
10917 case CPP_PLUS_PLUS:
10918 id = ansi_opname (POSTINCREMENT_EXPR);
10919 break;
10920
10921 case CPP_MINUS_MINUS:
10922 id = ansi_opname (PREDECREMENT_EXPR);
10923 break;
10924
10925 case CPP_COMMA:
10926 id = ansi_opname (COMPOUND_EXPR);
10927 break;
10928
10929 case CPP_DEREF_STAR:
10930 id = ansi_opname (MEMBER_REF);
10931 break;
10932
10933 case CPP_DEREF:
10934 id = ansi_opname (COMPONENT_REF);
10935 break;
10936
10937 case CPP_OPEN_PAREN:
10938 /* Consume the `('. */
10939 cp_lexer_consume_token (parser->lexer);
10940 /* Look for the matching `)'. */
10941 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10942 return ansi_opname (CALL_EXPR);
10943
10944 case CPP_OPEN_SQUARE:
10945 /* Consume the `['. */
10946 cp_lexer_consume_token (parser->lexer);
10947 /* Look for the matching `]'. */
10948 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10949 return ansi_opname (ARRAY_REF);
10950
10951 default:
10952 /* Anything else is an error. */
10953 break;
10954 }
10955
10956 /* If we have selected an identifier, we need to consume the
10957 operator token. */
10958 if (id)
10959 cp_lexer_consume_token (parser->lexer);
10960 /* Otherwise, no valid operator name was present. */
10961 else
10962 {
10963 cp_parser_error (parser, "expected operator");
10964 id = error_mark_node;
10965 }
10966
10967 return id;
10968 }
10969
10970 /* Parse a template-declaration.
10971
10972 template-declaration:
10973 export [opt] template < template-parameter-list > declaration
10974
10975 If MEMBER_P is TRUE, this template-declaration occurs within a
10976 class-specifier.
10977
10978 The grammar rule given by the standard isn't correct. What
10979 is really meant is:
10980
10981 template-declaration:
10982 export [opt] template-parameter-list-seq
10983 decl-specifier-seq [opt] init-declarator [opt] ;
10984 export [opt] template-parameter-list-seq
10985 function-definition
10986
10987 template-parameter-list-seq:
10988 template-parameter-list-seq [opt]
10989 template < template-parameter-list > */
10990
10991 static void
10992 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10993 {
10994 /* Check for `export'. */
10995 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10996 {
10997 /* Consume the `export' token. */
10998 cp_lexer_consume_token (parser->lexer);
10999 /* Warn that we do not support `export'. */
11000 warning (0, "keyword %<export%> not implemented, and will be ignored");
11001 }
11002
11003 cp_parser_template_declaration_after_export (parser, member_p);
11004 }
11005
11006 /* Parse a template-parameter-list.
11007
11008 template-parameter-list:
11009 template-parameter
11010 template-parameter-list , template-parameter
11011
11012 Returns a TREE_LIST. Each node represents a template parameter.
11013 The nodes are connected via their TREE_CHAINs. */
11014
11015 static tree
11016 cp_parser_template_parameter_list (cp_parser* parser)
11017 {
11018 tree parameter_list = NULL_TREE;
11019
11020 begin_template_parm_list ();
11021 while (true)
11022 {
11023 tree parameter;
11024 bool is_non_type;
11025 bool is_parameter_pack;
11026 location_t parm_loc;
11027
11028 /* Parse the template-parameter. */
11029 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11030 parameter = cp_parser_template_parameter (parser,
11031 &is_non_type,
11032 &is_parameter_pack);
11033 /* Add it to the list. */
11034 if (parameter != error_mark_node)
11035 parameter_list = process_template_parm (parameter_list,
11036 parm_loc,
11037 parameter,
11038 is_non_type,
11039 is_parameter_pack);
11040 else
11041 {
11042 tree err_parm = build_tree_list (parameter, parameter);
11043 TREE_VALUE (err_parm) = error_mark_node;
11044 parameter_list = chainon (parameter_list, err_parm);
11045 }
11046
11047 /* If the next token is not a `,', we're done. */
11048 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11049 break;
11050 /* Otherwise, consume the `,' token. */
11051 cp_lexer_consume_token (parser->lexer);
11052 }
11053
11054 return end_template_parm_list (parameter_list);
11055 }
11056
11057 /* Parse a template-parameter.
11058
11059 template-parameter:
11060 type-parameter
11061 parameter-declaration
11062
11063 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11064 the parameter. The TREE_PURPOSE is the default value, if any.
11065 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11066 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11067 set to true iff this parameter is a parameter pack. */
11068
11069 static tree
11070 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11071 bool *is_parameter_pack)
11072 {
11073 cp_token *token;
11074 cp_parameter_declarator *parameter_declarator;
11075 cp_declarator *id_declarator;
11076 tree parm;
11077
11078 /* Assume it is a type parameter or a template parameter. */
11079 *is_non_type = false;
11080 /* Assume it not a parameter pack. */
11081 *is_parameter_pack = false;
11082 /* Peek at the next token. */
11083 token = cp_lexer_peek_token (parser->lexer);
11084 /* If it is `class' or `template', we have a type-parameter. */
11085 if (token->keyword == RID_TEMPLATE)
11086 return cp_parser_type_parameter (parser, is_parameter_pack);
11087 /* If it is `class' or `typename' we do not know yet whether it is a
11088 type parameter or a non-type parameter. Consider:
11089
11090 template <typename T, typename T::X X> ...
11091
11092 or:
11093
11094 template <class C, class D*> ...
11095
11096 Here, the first parameter is a type parameter, and the second is
11097 a non-type parameter. We can tell by looking at the token after
11098 the identifier -- if it is a `,', `=', or `>' then we have a type
11099 parameter. */
11100 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11101 {
11102 /* Peek at the token after `class' or `typename'. */
11103 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11104 /* If it's an ellipsis, we have a template type parameter
11105 pack. */
11106 if (token->type == CPP_ELLIPSIS)
11107 return cp_parser_type_parameter (parser, is_parameter_pack);
11108 /* If it's an identifier, skip it. */
11109 if (token->type == CPP_NAME)
11110 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11111 /* Now, see if the token looks like the end of a template
11112 parameter. */
11113 if (token->type == CPP_COMMA
11114 || token->type == CPP_EQ
11115 || token->type == CPP_GREATER)
11116 return cp_parser_type_parameter (parser, is_parameter_pack);
11117 }
11118
11119 /* Otherwise, it is a non-type parameter.
11120
11121 [temp.param]
11122
11123 When parsing a default template-argument for a non-type
11124 template-parameter, the first non-nested `>' is taken as the end
11125 of the template parameter-list rather than a greater-than
11126 operator. */
11127 *is_non_type = true;
11128 parameter_declarator
11129 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11130 /*parenthesized_p=*/NULL);
11131
11132 /* If the parameter declaration is marked as a parameter pack, set
11133 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11134 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11135 grokdeclarator. */
11136 if (parameter_declarator
11137 && parameter_declarator->declarator
11138 && parameter_declarator->declarator->parameter_pack_p)
11139 {
11140 *is_parameter_pack = true;
11141 parameter_declarator->declarator->parameter_pack_p = false;
11142 }
11143
11144 /* If the next token is an ellipsis, and we don't already have it
11145 marked as a parameter pack, then we have a parameter pack (that
11146 has no declarator). */
11147 if (!*is_parameter_pack
11148 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11149 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11150 {
11151 /* Consume the `...'. */
11152 cp_lexer_consume_token (parser->lexer);
11153 maybe_warn_variadic_templates ();
11154
11155 *is_parameter_pack = true;
11156 }
11157 /* We might end up with a pack expansion as the type of the non-type
11158 template parameter, in which case this is a non-type template
11159 parameter pack. */
11160 else if (parameter_declarator
11161 && parameter_declarator->decl_specifiers.type
11162 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11163 {
11164 *is_parameter_pack = true;
11165 parameter_declarator->decl_specifiers.type =
11166 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11167 }
11168
11169 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11170 {
11171 /* Parameter packs cannot have default arguments. However, a
11172 user may try to do so, so we'll parse them and give an
11173 appropriate diagnostic here. */
11174
11175 /* Consume the `='. */
11176 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11177 cp_lexer_consume_token (parser->lexer);
11178
11179 /* Find the name of the parameter pack. */
11180 id_declarator = parameter_declarator->declarator;
11181 while (id_declarator && id_declarator->kind != cdk_id)
11182 id_declarator = id_declarator->declarator;
11183
11184 if (id_declarator && id_declarator->kind == cdk_id)
11185 error_at (start_token->location,
11186 "template parameter pack %qD cannot have a default argument",
11187 id_declarator->u.id.unqualified_name);
11188 else
11189 error_at (start_token->location,
11190 "template parameter pack cannot have a default argument");
11191
11192 /* Parse the default argument, but throw away the result. */
11193 cp_parser_default_argument (parser, /*template_parm_p=*/true);
11194 }
11195
11196 parm = grokdeclarator (parameter_declarator->declarator,
11197 &parameter_declarator->decl_specifiers,
11198 TPARM, /*initialized=*/0,
11199 /*attrlist=*/NULL);
11200 if (parm == error_mark_node)
11201 return error_mark_node;
11202
11203 return build_tree_list (parameter_declarator->default_argument, parm);
11204 }
11205
11206 /* Parse a type-parameter.
11207
11208 type-parameter:
11209 class identifier [opt]
11210 class identifier [opt] = type-id
11211 typename identifier [opt]
11212 typename identifier [opt] = type-id
11213 template < template-parameter-list > class identifier [opt]
11214 template < template-parameter-list > class identifier [opt]
11215 = id-expression
11216
11217 GNU Extension (variadic templates):
11218
11219 type-parameter:
11220 class ... identifier [opt]
11221 typename ... identifier [opt]
11222
11223 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11224 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11225 the declaration of the parameter.
11226
11227 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11228
11229 static tree
11230 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11231 {
11232 cp_token *token;
11233 tree parameter;
11234
11235 /* Look for a keyword to tell us what kind of parameter this is. */
11236 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11237 if (!token)
11238 return error_mark_node;
11239
11240 switch (token->keyword)
11241 {
11242 case RID_CLASS:
11243 case RID_TYPENAME:
11244 {
11245 tree identifier;
11246 tree default_argument;
11247
11248 /* If the next token is an ellipsis, we have a template
11249 argument pack. */
11250 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11251 {
11252 /* Consume the `...' token. */
11253 cp_lexer_consume_token (parser->lexer);
11254 maybe_warn_variadic_templates ();
11255
11256 *is_parameter_pack = true;
11257 }
11258
11259 /* If the next token is an identifier, then it names the
11260 parameter. */
11261 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11262 identifier = cp_parser_identifier (parser);
11263 else
11264 identifier = NULL_TREE;
11265
11266 /* Create the parameter. */
11267 parameter = finish_template_type_parm (class_type_node, identifier);
11268
11269 /* If the next token is an `=', we have a default argument. */
11270 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11271 {
11272 /* Consume the `=' token. */
11273 cp_lexer_consume_token (parser->lexer);
11274 /* Parse the default-argument. */
11275 push_deferring_access_checks (dk_no_deferred);
11276 default_argument = cp_parser_type_id (parser);
11277
11278 /* Template parameter packs cannot have default
11279 arguments. */
11280 if (*is_parameter_pack)
11281 {
11282 if (identifier)
11283 error_at (token->location,
11284 "template parameter pack %qD cannot have a "
11285 "default argument", identifier);
11286 else
11287 error_at (token->location,
11288 "template parameter packs cannot have "
11289 "default arguments");
11290 default_argument = NULL_TREE;
11291 }
11292 pop_deferring_access_checks ();
11293 }
11294 else
11295 default_argument = NULL_TREE;
11296
11297 /* Create the combined representation of the parameter and the
11298 default argument. */
11299 parameter = build_tree_list (default_argument, parameter);
11300 }
11301 break;
11302
11303 case RID_TEMPLATE:
11304 {
11305 tree identifier;
11306 tree default_argument;
11307
11308 /* Look for the `<'. */
11309 cp_parser_require (parser, CPP_LESS, RT_LESS);
11310 /* Parse the template-parameter-list. */
11311 cp_parser_template_parameter_list (parser);
11312 /* Look for the `>'. */
11313 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11314 /* Look for the `class' keyword. */
11315 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11316 /* If the next token is an ellipsis, we have a template
11317 argument pack. */
11318 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11319 {
11320 /* Consume the `...' token. */
11321 cp_lexer_consume_token (parser->lexer);
11322 maybe_warn_variadic_templates ();
11323
11324 *is_parameter_pack = true;
11325 }
11326 /* If the next token is an `=', then there is a
11327 default-argument. If the next token is a `>', we are at
11328 the end of the parameter-list. If the next token is a `,',
11329 then we are at the end of this parameter. */
11330 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11331 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11332 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11333 {
11334 identifier = cp_parser_identifier (parser);
11335 /* Treat invalid names as if the parameter were nameless. */
11336 if (identifier == error_mark_node)
11337 identifier = NULL_TREE;
11338 }
11339 else
11340 identifier = NULL_TREE;
11341
11342 /* Create the template parameter. */
11343 parameter = finish_template_template_parm (class_type_node,
11344 identifier);
11345
11346 /* If the next token is an `=', then there is a
11347 default-argument. */
11348 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11349 {
11350 bool is_template;
11351
11352 /* Consume the `='. */
11353 cp_lexer_consume_token (parser->lexer);
11354 /* Parse the id-expression. */
11355 push_deferring_access_checks (dk_no_deferred);
11356 /* save token before parsing the id-expression, for error
11357 reporting */
11358 token = cp_lexer_peek_token (parser->lexer);
11359 default_argument
11360 = cp_parser_id_expression (parser,
11361 /*template_keyword_p=*/false,
11362 /*check_dependency_p=*/true,
11363 /*template_p=*/&is_template,
11364 /*declarator_p=*/false,
11365 /*optional_p=*/false);
11366 if (TREE_CODE (default_argument) == TYPE_DECL)
11367 /* If the id-expression was a template-id that refers to
11368 a template-class, we already have the declaration here,
11369 so no further lookup is needed. */
11370 ;
11371 else
11372 /* Look up the name. */
11373 default_argument
11374 = cp_parser_lookup_name (parser, default_argument,
11375 none_type,
11376 /*is_template=*/is_template,
11377 /*is_namespace=*/false,
11378 /*check_dependency=*/true,
11379 /*ambiguous_decls=*/NULL,
11380 token->location);
11381 /* See if the default argument is valid. */
11382 default_argument
11383 = check_template_template_default_arg (default_argument);
11384
11385 /* Template parameter packs cannot have default
11386 arguments. */
11387 if (*is_parameter_pack)
11388 {
11389 if (identifier)
11390 error_at (token->location,
11391 "template parameter pack %qD cannot "
11392 "have a default argument",
11393 identifier);
11394 else
11395 error_at (token->location, "template parameter packs cannot "
11396 "have default arguments");
11397 default_argument = NULL_TREE;
11398 }
11399 pop_deferring_access_checks ();
11400 }
11401 else
11402 default_argument = NULL_TREE;
11403
11404 /* Create the combined representation of the parameter and the
11405 default argument. */
11406 parameter = build_tree_list (default_argument, parameter);
11407 }
11408 break;
11409
11410 default:
11411 gcc_unreachable ();
11412 break;
11413 }
11414
11415 return parameter;
11416 }
11417
11418 /* Parse a template-id.
11419
11420 template-id:
11421 template-name < template-argument-list [opt] >
11422
11423 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11424 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11425 returned. Otherwise, if the template-name names a function, or set
11426 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11427 names a class, returns a TYPE_DECL for the specialization.
11428
11429 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11430 uninstantiated templates. */
11431
11432 static tree
11433 cp_parser_template_id (cp_parser *parser,
11434 bool template_keyword_p,
11435 bool check_dependency_p,
11436 bool is_declaration)
11437 {
11438 int i;
11439 tree templ;
11440 tree arguments;
11441 tree template_id;
11442 cp_token_position start_of_id = 0;
11443 deferred_access_check *chk;
11444 VEC (deferred_access_check,gc) *access_check;
11445 cp_token *next_token = NULL, *next_token_2 = NULL;
11446 bool is_identifier;
11447
11448 /* If the next token corresponds to a template-id, there is no need
11449 to reparse it. */
11450 next_token = cp_lexer_peek_token (parser->lexer);
11451 if (next_token->type == CPP_TEMPLATE_ID)
11452 {
11453 struct tree_check *check_value;
11454
11455 /* Get the stored value. */
11456 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11457 /* Perform any access checks that were deferred. */
11458 access_check = check_value->checks;
11459 if (access_check)
11460 {
11461 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11462 perform_or_defer_access_check (chk->binfo,
11463 chk->decl,
11464 chk->diag_decl);
11465 }
11466 /* Return the stored value. */
11467 return check_value->value;
11468 }
11469
11470 /* Avoid performing name lookup if there is no possibility of
11471 finding a template-id. */
11472 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11473 || (next_token->type == CPP_NAME
11474 && !cp_parser_nth_token_starts_template_argument_list_p
11475 (parser, 2)))
11476 {
11477 cp_parser_error (parser, "expected template-id");
11478 return error_mark_node;
11479 }
11480
11481 /* Remember where the template-id starts. */
11482 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11483 start_of_id = cp_lexer_token_position (parser->lexer, false);
11484
11485 push_deferring_access_checks (dk_deferred);
11486
11487 /* Parse the template-name. */
11488 is_identifier = false;
11489 templ = cp_parser_template_name (parser, template_keyword_p,
11490 check_dependency_p,
11491 is_declaration,
11492 &is_identifier);
11493 if (templ == error_mark_node || is_identifier)
11494 {
11495 pop_deferring_access_checks ();
11496 return templ;
11497 }
11498
11499 /* If we find the sequence `[:' after a template-name, it's probably
11500 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11501 parse correctly the argument list. */
11502 next_token = cp_lexer_peek_token (parser->lexer);
11503 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11504 if (next_token->type == CPP_OPEN_SQUARE
11505 && next_token->flags & DIGRAPH
11506 && next_token_2->type == CPP_COLON
11507 && !(next_token_2->flags & PREV_WHITE))
11508 {
11509 cp_parser_parse_tentatively (parser);
11510 /* Change `:' into `::'. */
11511 next_token_2->type = CPP_SCOPE;
11512 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11513 CPP_LESS. */
11514 cp_lexer_consume_token (parser->lexer);
11515
11516 /* Parse the arguments. */
11517 arguments = cp_parser_enclosed_template_argument_list (parser);
11518 if (!cp_parser_parse_definitely (parser))
11519 {
11520 /* If we couldn't parse an argument list, then we revert our changes
11521 and return simply an error. Maybe this is not a template-id
11522 after all. */
11523 next_token_2->type = CPP_COLON;
11524 cp_parser_error (parser, "expected %<<%>");
11525 pop_deferring_access_checks ();
11526 return error_mark_node;
11527 }
11528 /* Otherwise, emit an error about the invalid digraph, but continue
11529 parsing because we got our argument list. */
11530 if (permerror (next_token->location,
11531 "%<<::%> cannot begin a template-argument list"))
11532 {
11533 static bool hint = false;
11534 inform (next_token->location,
11535 "%<<:%> is an alternate spelling for %<[%>."
11536 " Insert whitespace between %<<%> and %<::%>");
11537 if (!hint && !flag_permissive)
11538 {
11539 inform (next_token->location, "(if you use %<-fpermissive%>"
11540 " G++ will accept your code)");
11541 hint = true;
11542 }
11543 }
11544 }
11545 else
11546 {
11547 /* Look for the `<' that starts the template-argument-list. */
11548 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11549 {
11550 pop_deferring_access_checks ();
11551 return error_mark_node;
11552 }
11553 /* Parse the arguments. */
11554 arguments = cp_parser_enclosed_template_argument_list (parser);
11555 }
11556
11557 /* Build a representation of the specialization. */
11558 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11559 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11560 else if (DECL_CLASS_TEMPLATE_P (templ)
11561 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11562 {
11563 bool entering_scope;
11564 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11565 template (rather than some instantiation thereof) only if
11566 is not nested within some other construct. For example, in
11567 "template <typename T> void f(T) { A<T>::", A<T> is just an
11568 instantiation of A. */
11569 entering_scope = (template_parm_scope_p ()
11570 && cp_lexer_next_token_is (parser->lexer,
11571 CPP_SCOPE));
11572 template_id
11573 = finish_template_type (templ, arguments, entering_scope);
11574 }
11575 else
11576 {
11577 /* If it's not a class-template or a template-template, it should be
11578 a function-template. */
11579 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11580 || TREE_CODE (templ) == OVERLOAD
11581 || BASELINK_P (templ)));
11582
11583 template_id = lookup_template_function (templ, arguments);
11584 }
11585
11586 /* If parsing tentatively, replace the sequence of tokens that makes
11587 up the template-id with a CPP_TEMPLATE_ID token. That way,
11588 should we re-parse the token stream, we will not have to repeat
11589 the effort required to do the parse, nor will we issue duplicate
11590 error messages about problems during instantiation of the
11591 template. */
11592 if (start_of_id)
11593 {
11594 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11595
11596 /* Reset the contents of the START_OF_ID token. */
11597 token->type = CPP_TEMPLATE_ID;
11598 /* Retrieve any deferred checks. Do not pop this access checks yet
11599 so the memory will not be reclaimed during token replacing below. */
11600 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11601 token->u.tree_check_value->value = template_id;
11602 token->u.tree_check_value->checks = get_deferred_access_checks ();
11603 token->keyword = RID_MAX;
11604
11605 /* Purge all subsequent tokens. */
11606 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11607
11608 /* ??? Can we actually assume that, if template_id ==
11609 error_mark_node, we will have issued a diagnostic to the
11610 user, as opposed to simply marking the tentative parse as
11611 failed? */
11612 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11613 error_at (token->location, "parse error in template argument list");
11614 }
11615
11616 pop_deferring_access_checks ();
11617 return template_id;
11618 }
11619
11620 /* Parse a template-name.
11621
11622 template-name:
11623 identifier
11624
11625 The standard should actually say:
11626
11627 template-name:
11628 identifier
11629 operator-function-id
11630
11631 A defect report has been filed about this issue.
11632
11633 A conversion-function-id cannot be a template name because they cannot
11634 be part of a template-id. In fact, looking at this code:
11635
11636 a.operator K<int>()
11637
11638 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11639 It is impossible to call a templated conversion-function-id with an
11640 explicit argument list, since the only allowed template parameter is
11641 the type to which it is converting.
11642
11643 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11644 `template' keyword, in a construction like:
11645
11646 T::template f<3>()
11647
11648 In that case `f' is taken to be a template-name, even though there
11649 is no way of knowing for sure.
11650
11651 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11652 name refers to a set of overloaded functions, at least one of which
11653 is a template, or an IDENTIFIER_NODE with the name of the template,
11654 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11655 names are looked up inside uninstantiated templates. */
11656
11657 static tree
11658 cp_parser_template_name (cp_parser* parser,
11659 bool template_keyword_p,
11660 bool check_dependency_p,
11661 bool is_declaration,
11662 bool *is_identifier)
11663 {
11664 tree identifier;
11665 tree decl;
11666 tree fns;
11667 cp_token *token = cp_lexer_peek_token (parser->lexer);
11668
11669 /* If the next token is `operator', then we have either an
11670 operator-function-id or a conversion-function-id. */
11671 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11672 {
11673 /* We don't know whether we're looking at an
11674 operator-function-id or a conversion-function-id. */
11675 cp_parser_parse_tentatively (parser);
11676 /* Try an operator-function-id. */
11677 identifier = cp_parser_operator_function_id (parser);
11678 /* If that didn't work, try a conversion-function-id. */
11679 if (!cp_parser_parse_definitely (parser))
11680 {
11681 cp_parser_error (parser, "expected template-name");
11682 return error_mark_node;
11683 }
11684 }
11685 /* Look for the identifier. */
11686 else
11687 identifier = cp_parser_identifier (parser);
11688
11689 /* If we didn't find an identifier, we don't have a template-id. */
11690 if (identifier == error_mark_node)
11691 return error_mark_node;
11692
11693 /* If the name immediately followed the `template' keyword, then it
11694 is a template-name. However, if the next token is not `<', then
11695 we do not treat it as a template-name, since it is not being used
11696 as part of a template-id. This enables us to handle constructs
11697 like:
11698
11699 template <typename T> struct S { S(); };
11700 template <typename T> S<T>::S();
11701
11702 correctly. We would treat `S' as a template -- if it were `S<T>'
11703 -- but we do not if there is no `<'. */
11704
11705 if (processing_template_decl
11706 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11707 {
11708 /* In a declaration, in a dependent context, we pretend that the
11709 "template" keyword was present in order to improve error
11710 recovery. For example, given:
11711
11712 template <typename T> void f(T::X<int>);
11713
11714 we want to treat "X<int>" as a template-id. */
11715 if (is_declaration
11716 && !template_keyword_p
11717 && parser->scope && TYPE_P (parser->scope)
11718 && check_dependency_p
11719 && dependent_scope_p (parser->scope)
11720 /* Do not do this for dtors (or ctors), since they never
11721 need the template keyword before their name. */
11722 && !constructor_name_p (identifier, parser->scope))
11723 {
11724 cp_token_position start = 0;
11725
11726 /* Explain what went wrong. */
11727 error_at (token->location, "non-template %qD used as template",
11728 identifier);
11729 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11730 parser->scope, identifier);
11731 /* If parsing tentatively, find the location of the "<" token. */
11732 if (cp_parser_simulate_error (parser))
11733 start = cp_lexer_token_position (parser->lexer, true);
11734 /* Parse the template arguments so that we can issue error
11735 messages about them. */
11736 cp_lexer_consume_token (parser->lexer);
11737 cp_parser_enclosed_template_argument_list (parser);
11738 /* Skip tokens until we find a good place from which to
11739 continue parsing. */
11740 cp_parser_skip_to_closing_parenthesis (parser,
11741 /*recovering=*/true,
11742 /*or_comma=*/true,
11743 /*consume_paren=*/false);
11744 /* If parsing tentatively, permanently remove the
11745 template argument list. That will prevent duplicate
11746 error messages from being issued about the missing
11747 "template" keyword. */
11748 if (start)
11749 cp_lexer_purge_tokens_after (parser->lexer, start);
11750 if (is_identifier)
11751 *is_identifier = true;
11752 return identifier;
11753 }
11754
11755 /* If the "template" keyword is present, then there is generally
11756 no point in doing name-lookup, so we just return IDENTIFIER.
11757 But, if the qualifying scope is non-dependent then we can
11758 (and must) do name-lookup normally. */
11759 if (template_keyword_p
11760 && (!parser->scope
11761 || (TYPE_P (parser->scope)
11762 && dependent_type_p (parser->scope))))
11763 return identifier;
11764 }
11765
11766 /* Look up the name. */
11767 decl = cp_parser_lookup_name (parser, identifier,
11768 none_type,
11769 /*is_template=*/true,
11770 /*is_namespace=*/false,
11771 check_dependency_p,
11772 /*ambiguous_decls=*/NULL,
11773 token->location);
11774
11775 /* If DECL is a template, then the name was a template-name. */
11776 if (TREE_CODE (decl) == TEMPLATE_DECL)
11777 ;
11778 else
11779 {
11780 tree fn = NULL_TREE;
11781
11782 /* The standard does not explicitly indicate whether a name that
11783 names a set of overloaded declarations, some of which are
11784 templates, is a template-name. However, such a name should
11785 be a template-name; otherwise, there is no way to form a
11786 template-id for the overloaded templates. */
11787 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11788 if (TREE_CODE (fns) == OVERLOAD)
11789 for (fn = fns; fn; fn = OVL_NEXT (fn))
11790 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11791 break;
11792
11793 if (!fn)
11794 {
11795 /* The name does not name a template. */
11796 cp_parser_error (parser, "expected template-name");
11797 return error_mark_node;
11798 }
11799 }
11800
11801 /* If DECL is dependent, and refers to a function, then just return
11802 its name; we will look it up again during template instantiation. */
11803 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11804 {
11805 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11806 if (TYPE_P (scope) && dependent_type_p (scope))
11807 return identifier;
11808 }
11809
11810 return decl;
11811 }
11812
11813 /* Parse a template-argument-list.
11814
11815 template-argument-list:
11816 template-argument ... [opt]
11817 template-argument-list , template-argument ... [opt]
11818
11819 Returns a TREE_VEC containing the arguments. */
11820
11821 static tree
11822 cp_parser_template_argument_list (cp_parser* parser)
11823 {
11824 tree fixed_args[10];
11825 unsigned n_args = 0;
11826 unsigned alloced = 10;
11827 tree *arg_ary = fixed_args;
11828 tree vec;
11829 bool saved_in_template_argument_list_p;
11830 bool saved_ice_p;
11831 bool saved_non_ice_p;
11832
11833 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11834 parser->in_template_argument_list_p = true;
11835 /* Even if the template-id appears in an integral
11836 constant-expression, the contents of the argument list do
11837 not. */
11838 saved_ice_p = parser->integral_constant_expression_p;
11839 parser->integral_constant_expression_p = false;
11840 saved_non_ice_p = parser->non_integral_constant_expression_p;
11841 parser->non_integral_constant_expression_p = false;
11842 /* Parse the arguments. */
11843 do
11844 {
11845 tree argument;
11846
11847 if (n_args)
11848 /* Consume the comma. */
11849 cp_lexer_consume_token (parser->lexer);
11850
11851 /* Parse the template-argument. */
11852 argument = cp_parser_template_argument (parser);
11853
11854 /* If the next token is an ellipsis, we're expanding a template
11855 argument pack. */
11856 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11857 {
11858 if (argument == error_mark_node)
11859 {
11860 cp_token *token = cp_lexer_peek_token (parser->lexer);
11861 error_at (token->location,
11862 "expected parameter pack before %<...%>");
11863 }
11864 /* Consume the `...' token. */
11865 cp_lexer_consume_token (parser->lexer);
11866
11867 /* Make the argument into a TYPE_PACK_EXPANSION or
11868 EXPR_PACK_EXPANSION. */
11869 argument = make_pack_expansion (argument);
11870 }
11871
11872 if (n_args == alloced)
11873 {
11874 alloced *= 2;
11875
11876 if (arg_ary == fixed_args)
11877 {
11878 arg_ary = XNEWVEC (tree, alloced);
11879 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11880 }
11881 else
11882 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11883 }
11884 arg_ary[n_args++] = argument;
11885 }
11886 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11887
11888 vec = make_tree_vec (n_args);
11889
11890 while (n_args--)
11891 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11892
11893 if (arg_ary != fixed_args)
11894 free (arg_ary);
11895 parser->non_integral_constant_expression_p = saved_non_ice_p;
11896 parser->integral_constant_expression_p = saved_ice_p;
11897 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11898 #ifdef ENABLE_CHECKING
11899 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11900 #endif
11901 return vec;
11902 }
11903
11904 /* Parse a template-argument.
11905
11906 template-argument:
11907 assignment-expression
11908 type-id
11909 id-expression
11910
11911 The representation is that of an assignment-expression, type-id, or
11912 id-expression -- except that the qualified id-expression is
11913 evaluated, so that the value returned is either a DECL or an
11914 OVERLOAD.
11915
11916 Although the standard says "assignment-expression", it forbids
11917 throw-expressions or assignments in the template argument.
11918 Therefore, we use "conditional-expression" instead. */
11919
11920 static tree
11921 cp_parser_template_argument (cp_parser* parser)
11922 {
11923 tree argument;
11924 bool template_p;
11925 bool address_p;
11926 bool maybe_type_id = false;
11927 cp_token *token = NULL, *argument_start_token = NULL;
11928 cp_id_kind idk;
11929
11930 /* There's really no way to know what we're looking at, so we just
11931 try each alternative in order.
11932
11933 [temp.arg]
11934
11935 In a template-argument, an ambiguity between a type-id and an
11936 expression is resolved to a type-id, regardless of the form of
11937 the corresponding template-parameter.
11938
11939 Therefore, we try a type-id first. */
11940 cp_parser_parse_tentatively (parser);
11941 argument = cp_parser_template_type_arg (parser);
11942 /* If there was no error parsing the type-id but the next token is a
11943 '>>', our behavior depends on which dialect of C++ we're
11944 parsing. In C++98, we probably found a typo for '> >'. But there
11945 are type-id which are also valid expressions. For instance:
11946
11947 struct X { int operator >> (int); };
11948 template <int V> struct Foo {};
11949 Foo<X () >> 5> r;
11950
11951 Here 'X()' is a valid type-id of a function type, but the user just
11952 wanted to write the expression "X() >> 5". Thus, we remember that we
11953 found a valid type-id, but we still try to parse the argument as an
11954 expression to see what happens.
11955
11956 In C++0x, the '>>' will be considered two separate '>'
11957 tokens. */
11958 if (!cp_parser_error_occurred (parser)
11959 && cxx_dialect == cxx98
11960 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11961 {
11962 maybe_type_id = true;
11963 cp_parser_abort_tentative_parse (parser);
11964 }
11965 else
11966 {
11967 /* If the next token isn't a `,' or a `>', then this argument wasn't
11968 really finished. This means that the argument is not a valid
11969 type-id. */
11970 if (!cp_parser_next_token_ends_template_argument_p (parser))
11971 cp_parser_error (parser, "expected template-argument");
11972 /* If that worked, we're done. */
11973 if (cp_parser_parse_definitely (parser))
11974 return argument;
11975 }
11976 /* We're still not sure what the argument will be. */
11977 cp_parser_parse_tentatively (parser);
11978 /* Try a template. */
11979 argument_start_token = cp_lexer_peek_token (parser->lexer);
11980 argument = cp_parser_id_expression (parser,
11981 /*template_keyword_p=*/false,
11982 /*check_dependency_p=*/true,
11983 &template_p,
11984 /*declarator_p=*/false,
11985 /*optional_p=*/false);
11986 /* If the next token isn't a `,' or a `>', then this argument wasn't
11987 really finished. */
11988 if (!cp_parser_next_token_ends_template_argument_p (parser))
11989 cp_parser_error (parser, "expected template-argument");
11990 if (!cp_parser_error_occurred (parser))
11991 {
11992 /* Figure out what is being referred to. If the id-expression
11993 was for a class template specialization, then we will have a
11994 TYPE_DECL at this point. There is no need to do name lookup
11995 at this point in that case. */
11996 if (TREE_CODE (argument) != TYPE_DECL)
11997 argument = cp_parser_lookup_name (parser, argument,
11998 none_type,
11999 /*is_template=*/template_p,
12000 /*is_namespace=*/false,
12001 /*check_dependency=*/true,
12002 /*ambiguous_decls=*/NULL,
12003 argument_start_token->location);
12004 if (TREE_CODE (argument) != TEMPLATE_DECL
12005 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12006 cp_parser_error (parser, "expected template-name");
12007 }
12008 if (cp_parser_parse_definitely (parser))
12009 return argument;
12010 /* It must be a non-type argument. There permitted cases are given
12011 in [temp.arg.nontype]:
12012
12013 -- an integral constant-expression of integral or enumeration
12014 type; or
12015
12016 -- the name of a non-type template-parameter; or
12017
12018 -- the name of an object or function with external linkage...
12019
12020 -- the address of an object or function with external linkage...
12021
12022 -- a pointer to member... */
12023 /* Look for a non-type template parameter. */
12024 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12025 {
12026 cp_parser_parse_tentatively (parser);
12027 argument = cp_parser_primary_expression (parser,
12028 /*address_p=*/false,
12029 /*cast_p=*/false,
12030 /*template_arg_p=*/true,
12031 &idk);
12032 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12033 || !cp_parser_next_token_ends_template_argument_p (parser))
12034 cp_parser_simulate_error (parser);
12035 if (cp_parser_parse_definitely (parser))
12036 return argument;
12037 }
12038
12039 /* If the next token is "&", the argument must be the address of an
12040 object or function with external linkage. */
12041 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12042 if (address_p)
12043 cp_lexer_consume_token (parser->lexer);
12044 /* See if we might have an id-expression. */
12045 token = cp_lexer_peek_token (parser->lexer);
12046 if (token->type == CPP_NAME
12047 || token->keyword == RID_OPERATOR
12048 || token->type == CPP_SCOPE
12049 || token->type == CPP_TEMPLATE_ID
12050 || token->type == CPP_NESTED_NAME_SPECIFIER)
12051 {
12052 cp_parser_parse_tentatively (parser);
12053 argument = cp_parser_primary_expression (parser,
12054 address_p,
12055 /*cast_p=*/false,
12056 /*template_arg_p=*/true,
12057 &idk);
12058 if (cp_parser_error_occurred (parser)
12059 || !cp_parser_next_token_ends_template_argument_p (parser))
12060 cp_parser_abort_tentative_parse (parser);
12061 else
12062 {
12063 tree probe;
12064
12065 if (TREE_CODE (argument) == INDIRECT_REF)
12066 {
12067 gcc_assert (REFERENCE_REF_P (argument));
12068 argument = TREE_OPERAND (argument, 0);
12069 }
12070
12071 /* If we're in a template, we represent a qualified-id referring
12072 to a static data member as a SCOPE_REF even if the scope isn't
12073 dependent so that we can check access control later. */
12074 probe = argument;
12075 if (TREE_CODE (probe) == SCOPE_REF)
12076 probe = TREE_OPERAND (probe, 1);
12077 if (TREE_CODE (probe) == VAR_DECL)
12078 {
12079 /* A variable without external linkage might still be a
12080 valid constant-expression, so no error is issued here
12081 if the external-linkage check fails. */
12082 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12083 cp_parser_simulate_error (parser);
12084 }
12085 else if (is_overloaded_fn (argument))
12086 /* All overloaded functions are allowed; if the external
12087 linkage test does not pass, an error will be issued
12088 later. */
12089 ;
12090 else if (address_p
12091 && (TREE_CODE (argument) == OFFSET_REF
12092 || TREE_CODE (argument) == SCOPE_REF))
12093 /* A pointer-to-member. */
12094 ;
12095 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12096 ;
12097 else
12098 cp_parser_simulate_error (parser);
12099
12100 if (cp_parser_parse_definitely (parser))
12101 {
12102 if (address_p)
12103 argument = build_x_unary_op (ADDR_EXPR, argument,
12104 tf_warning_or_error);
12105 return argument;
12106 }
12107 }
12108 }
12109 /* If the argument started with "&", there are no other valid
12110 alternatives at this point. */
12111 if (address_p)
12112 {
12113 cp_parser_error (parser, "invalid non-type template argument");
12114 return error_mark_node;
12115 }
12116
12117 /* If the argument wasn't successfully parsed as a type-id followed
12118 by '>>', the argument can only be a constant expression now.
12119 Otherwise, we try parsing the constant-expression tentatively,
12120 because the argument could really be a type-id. */
12121 if (maybe_type_id)
12122 cp_parser_parse_tentatively (parser);
12123 argument = cp_parser_constant_expression (parser,
12124 /*allow_non_constant_p=*/false,
12125 /*non_constant_p=*/NULL);
12126 argument = fold_non_dependent_expr (argument);
12127 if (!maybe_type_id)
12128 return argument;
12129 if (!cp_parser_next_token_ends_template_argument_p (parser))
12130 cp_parser_error (parser, "expected template-argument");
12131 if (cp_parser_parse_definitely (parser))
12132 return argument;
12133 /* We did our best to parse the argument as a non type-id, but that
12134 was the only alternative that matched (albeit with a '>' after
12135 it). We can assume it's just a typo from the user, and a
12136 diagnostic will then be issued. */
12137 return cp_parser_template_type_arg (parser);
12138 }
12139
12140 /* Parse an explicit-instantiation.
12141
12142 explicit-instantiation:
12143 template declaration
12144
12145 Although the standard says `declaration', what it really means is:
12146
12147 explicit-instantiation:
12148 template decl-specifier-seq [opt] declarator [opt] ;
12149
12150 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12151 supposed to be allowed. A defect report has been filed about this
12152 issue.
12153
12154 GNU Extension:
12155
12156 explicit-instantiation:
12157 storage-class-specifier template
12158 decl-specifier-seq [opt] declarator [opt] ;
12159 function-specifier template
12160 decl-specifier-seq [opt] declarator [opt] ; */
12161
12162 static void
12163 cp_parser_explicit_instantiation (cp_parser* parser)
12164 {
12165 int declares_class_or_enum;
12166 cp_decl_specifier_seq decl_specifiers;
12167 tree extension_specifier = NULL_TREE;
12168
12169 /* Look for an (optional) storage-class-specifier or
12170 function-specifier. */
12171 if (cp_parser_allow_gnu_extensions_p (parser))
12172 {
12173 extension_specifier
12174 = cp_parser_storage_class_specifier_opt (parser);
12175 if (!extension_specifier)
12176 extension_specifier
12177 = cp_parser_function_specifier_opt (parser,
12178 /*decl_specs=*/NULL);
12179 }
12180
12181 /* Look for the `template' keyword. */
12182 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12183 /* Let the front end know that we are processing an explicit
12184 instantiation. */
12185 begin_explicit_instantiation ();
12186 /* [temp.explicit] says that we are supposed to ignore access
12187 control while processing explicit instantiation directives. */
12188 push_deferring_access_checks (dk_no_check);
12189 /* Parse a decl-specifier-seq. */
12190 cp_parser_decl_specifier_seq (parser,
12191 CP_PARSER_FLAGS_OPTIONAL,
12192 &decl_specifiers,
12193 &declares_class_or_enum);
12194 /* If there was exactly one decl-specifier, and it declared a class,
12195 and there's no declarator, then we have an explicit type
12196 instantiation. */
12197 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12198 {
12199 tree type;
12200
12201 type = check_tag_decl (&decl_specifiers);
12202 /* Turn access control back on for names used during
12203 template instantiation. */
12204 pop_deferring_access_checks ();
12205 if (type)
12206 do_type_instantiation (type, extension_specifier,
12207 /*complain=*/tf_error);
12208 }
12209 else
12210 {
12211 cp_declarator *declarator;
12212 tree decl;
12213
12214 /* Parse the declarator. */
12215 declarator
12216 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12217 /*ctor_dtor_or_conv_p=*/NULL,
12218 /*parenthesized_p=*/NULL,
12219 /*member_p=*/false);
12220 if (declares_class_or_enum & 2)
12221 cp_parser_check_for_definition_in_return_type (declarator,
12222 decl_specifiers.type,
12223 decl_specifiers.type_location);
12224 if (declarator != cp_error_declarator)
12225 {
12226 decl = grokdeclarator (declarator, &decl_specifiers,
12227 NORMAL, 0, &decl_specifiers.attributes);
12228 /* Turn access control back on for names used during
12229 template instantiation. */
12230 pop_deferring_access_checks ();
12231 /* Do the explicit instantiation. */
12232 do_decl_instantiation (decl, extension_specifier);
12233 }
12234 else
12235 {
12236 pop_deferring_access_checks ();
12237 /* Skip the body of the explicit instantiation. */
12238 cp_parser_skip_to_end_of_statement (parser);
12239 }
12240 }
12241 /* We're done with the instantiation. */
12242 end_explicit_instantiation ();
12243
12244 cp_parser_consume_semicolon_at_end_of_statement (parser);
12245 }
12246
12247 /* Parse an explicit-specialization.
12248
12249 explicit-specialization:
12250 template < > declaration
12251
12252 Although the standard says `declaration', what it really means is:
12253
12254 explicit-specialization:
12255 template <> decl-specifier [opt] init-declarator [opt] ;
12256 template <> function-definition
12257 template <> explicit-specialization
12258 template <> template-declaration */
12259
12260 static void
12261 cp_parser_explicit_specialization (cp_parser* parser)
12262 {
12263 bool need_lang_pop;
12264 cp_token *token = cp_lexer_peek_token (parser->lexer);
12265
12266 /* Look for the `template' keyword. */
12267 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12268 /* Look for the `<'. */
12269 cp_parser_require (parser, CPP_LESS, RT_LESS);
12270 /* Look for the `>'. */
12271 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12272 /* We have processed another parameter list. */
12273 ++parser->num_template_parameter_lists;
12274 /* [temp]
12275
12276 A template ... explicit specialization ... shall not have C
12277 linkage. */
12278 if (current_lang_name == lang_name_c)
12279 {
12280 error_at (token->location, "template specialization with C linkage");
12281 /* Give it C++ linkage to avoid confusing other parts of the
12282 front end. */
12283 push_lang_context (lang_name_cplusplus);
12284 need_lang_pop = true;
12285 }
12286 else
12287 need_lang_pop = false;
12288 /* Let the front end know that we are beginning a specialization. */
12289 if (!begin_specialization ())
12290 {
12291 end_specialization ();
12292 return;
12293 }
12294
12295 /* If the next keyword is `template', we need to figure out whether
12296 or not we're looking a template-declaration. */
12297 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12298 {
12299 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12300 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12301 cp_parser_template_declaration_after_export (parser,
12302 /*member_p=*/false);
12303 else
12304 cp_parser_explicit_specialization (parser);
12305 }
12306 else
12307 /* Parse the dependent declaration. */
12308 cp_parser_single_declaration (parser,
12309 /*checks=*/NULL,
12310 /*member_p=*/false,
12311 /*explicit_specialization_p=*/true,
12312 /*friend_p=*/NULL);
12313 /* We're done with the specialization. */
12314 end_specialization ();
12315 /* For the erroneous case of a template with C linkage, we pushed an
12316 implicit C++ linkage scope; exit that scope now. */
12317 if (need_lang_pop)
12318 pop_lang_context ();
12319 /* We're done with this parameter list. */
12320 --parser->num_template_parameter_lists;
12321 }
12322
12323 /* Parse a type-specifier.
12324
12325 type-specifier:
12326 simple-type-specifier
12327 class-specifier
12328 enum-specifier
12329 elaborated-type-specifier
12330 cv-qualifier
12331
12332 GNU Extension:
12333
12334 type-specifier:
12335 __complex__
12336
12337 Returns a representation of the type-specifier. For a
12338 class-specifier, enum-specifier, or elaborated-type-specifier, a
12339 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12340
12341 The parser flags FLAGS is used to control type-specifier parsing.
12342
12343 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12344 in a decl-specifier-seq.
12345
12346 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12347 class-specifier, enum-specifier, or elaborated-type-specifier, then
12348 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12349 if a type is declared; 2 if it is defined. Otherwise, it is set to
12350 zero.
12351
12352 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12353 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12354 is set to FALSE. */
12355
12356 static tree
12357 cp_parser_type_specifier (cp_parser* parser,
12358 cp_parser_flags flags,
12359 cp_decl_specifier_seq *decl_specs,
12360 bool is_declaration,
12361 int* declares_class_or_enum,
12362 bool* is_cv_qualifier)
12363 {
12364 tree type_spec = NULL_TREE;
12365 cp_token *token;
12366 enum rid keyword;
12367 cp_decl_spec ds = ds_last;
12368
12369 /* Assume this type-specifier does not declare a new type. */
12370 if (declares_class_or_enum)
12371 *declares_class_or_enum = 0;
12372 /* And that it does not specify a cv-qualifier. */
12373 if (is_cv_qualifier)
12374 *is_cv_qualifier = false;
12375 /* Peek at the next token. */
12376 token = cp_lexer_peek_token (parser->lexer);
12377
12378 /* If we're looking at a keyword, we can use that to guide the
12379 production we choose. */
12380 keyword = token->keyword;
12381 switch (keyword)
12382 {
12383 case RID_ENUM:
12384 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12385 goto elaborated_type_specifier;
12386
12387 /* Look for the enum-specifier. */
12388 type_spec = cp_parser_enum_specifier (parser);
12389 /* If that worked, we're done. */
12390 if (type_spec)
12391 {
12392 if (declares_class_or_enum)
12393 *declares_class_or_enum = 2;
12394 if (decl_specs)
12395 cp_parser_set_decl_spec_type (decl_specs,
12396 type_spec,
12397 token->location,
12398 /*user_defined_p=*/true);
12399 return type_spec;
12400 }
12401 else
12402 goto elaborated_type_specifier;
12403
12404 /* Any of these indicate either a class-specifier, or an
12405 elaborated-type-specifier. */
12406 case RID_CLASS:
12407 case RID_STRUCT:
12408 case RID_UNION:
12409 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12410 goto elaborated_type_specifier;
12411
12412 /* Parse tentatively so that we can back up if we don't find a
12413 class-specifier. */
12414 cp_parser_parse_tentatively (parser);
12415 /* Look for the class-specifier. */
12416 type_spec = cp_parser_class_specifier (parser);
12417 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12418 /* If that worked, we're done. */
12419 if (cp_parser_parse_definitely (parser))
12420 {
12421 if (declares_class_or_enum)
12422 *declares_class_or_enum = 2;
12423 if (decl_specs)
12424 cp_parser_set_decl_spec_type (decl_specs,
12425 type_spec,
12426 token->location,
12427 /*user_defined_p=*/true);
12428 return type_spec;
12429 }
12430
12431 /* Fall through. */
12432 elaborated_type_specifier:
12433 /* We're declaring (not defining) a class or enum. */
12434 if (declares_class_or_enum)
12435 *declares_class_or_enum = 1;
12436
12437 /* Fall through. */
12438 case RID_TYPENAME:
12439 /* Look for an elaborated-type-specifier. */
12440 type_spec
12441 = (cp_parser_elaborated_type_specifier
12442 (parser,
12443 decl_specs && decl_specs->specs[(int) ds_friend],
12444 is_declaration));
12445 if (decl_specs)
12446 cp_parser_set_decl_spec_type (decl_specs,
12447 type_spec,
12448 token->location,
12449 /*user_defined_p=*/true);
12450 return type_spec;
12451
12452 case RID_CONST:
12453 ds = ds_const;
12454 if (is_cv_qualifier)
12455 *is_cv_qualifier = true;
12456 break;
12457
12458 case RID_VOLATILE:
12459 ds = ds_volatile;
12460 if (is_cv_qualifier)
12461 *is_cv_qualifier = true;
12462 break;
12463
12464 case RID_RESTRICT:
12465 ds = ds_restrict;
12466 if (is_cv_qualifier)
12467 *is_cv_qualifier = true;
12468 break;
12469
12470 case RID_COMPLEX:
12471 /* The `__complex__' keyword is a GNU extension. */
12472 ds = ds_complex;
12473 break;
12474
12475 default:
12476 break;
12477 }
12478
12479 /* Handle simple keywords. */
12480 if (ds != ds_last)
12481 {
12482 if (decl_specs)
12483 {
12484 ++decl_specs->specs[(int)ds];
12485 decl_specs->any_specifiers_p = true;
12486 }
12487 return cp_lexer_consume_token (parser->lexer)->u.value;
12488 }
12489
12490 /* If we do not already have a type-specifier, assume we are looking
12491 at a simple-type-specifier. */
12492 type_spec = cp_parser_simple_type_specifier (parser,
12493 decl_specs,
12494 flags);
12495
12496 /* If we didn't find a type-specifier, and a type-specifier was not
12497 optional in this context, issue an error message. */
12498 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12499 {
12500 cp_parser_error (parser, "expected type specifier");
12501 return error_mark_node;
12502 }
12503
12504 return type_spec;
12505 }
12506
12507 /* Parse a simple-type-specifier.
12508
12509 simple-type-specifier:
12510 :: [opt] nested-name-specifier [opt] type-name
12511 :: [opt] nested-name-specifier template template-id
12512 char
12513 wchar_t
12514 bool
12515 short
12516 int
12517 long
12518 signed
12519 unsigned
12520 float
12521 double
12522 void
12523
12524 C++0x Extension:
12525
12526 simple-type-specifier:
12527 auto
12528 decltype ( expression )
12529 char16_t
12530 char32_t
12531
12532 GNU Extension:
12533
12534 simple-type-specifier:
12535 __int128
12536 __typeof__ unary-expression
12537 __typeof__ ( type-id )
12538
12539 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12540 appropriately updated. */
12541
12542 static tree
12543 cp_parser_simple_type_specifier (cp_parser* parser,
12544 cp_decl_specifier_seq *decl_specs,
12545 cp_parser_flags flags)
12546 {
12547 tree type = NULL_TREE;
12548 cp_token *token;
12549
12550 /* Peek at the next token. */
12551 token = cp_lexer_peek_token (parser->lexer);
12552
12553 /* If we're looking at a keyword, things are easy. */
12554 switch (token->keyword)
12555 {
12556 case RID_CHAR:
12557 if (decl_specs)
12558 decl_specs->explicit_char_p = true;
12559 type = char_type_node;
12560 break;
12561 case RID_CHAR16:
12562 type = char16_type_node;
12563 break;
12564 case RID_CHAR32:
12565 type = char32_type_node;
12566 break;
12567 case RID_WCHAR:
12568 type = wchar_type_node;
12569 break;
12570 case RID_BOOL:
12571 type = boolean_type_node;
12572 break;
12573 case RID_SHORT:
12574 if (decl_specs)
12575 ++decl_specs->specs[(int) ds_short];
12576 type = short_integer_type_node;
12577 break;
12578 case RID_INT:
12579 if (decl_specs)
12580 decl_specs->explicit_int_p = true;
12581 type = integer_type_node;
12582 break;
12583 case RID_INT128:
12584 if (!int128_integer_type_node)
12585 break;
12586 if (decl_specs)
12587 decl_specs->explicit_int128_p = true;
12588 type = int128_integer_type_node;
12589 break;
12590 case RID_LONG:
12591 if (decl_specs)
12592 ++decl_specs->specs[(int) ds_long];
12593 type = long_integer_type_node;
12594 break;
12595 case RID_SIGNED:
12596 if (decl_specs)
12597 ++decl_specs->specs[(int) ds_signed];
12598 type = integer_type_node;
12599 break;
12600 case RID_UNSIGNED:
12601 if (decl_specs)
12602 ++decl_specs->specs[(int) ds_unsigned];
12603 type = unsigned_type_node;
12604 break;
12605 case RID_FLOAT:
12606 type = float_type_node;
12607 break;
12608 case RID_DOUBLE:
12609 type = double_type_node;
12610 break;
12611 case RID_VOID:
12612 type = void_type_node;
12613 break;
12614
12615 case RID_AUTO:
12616 maybe_warn_cpp0x (CPP0X_AUTO);
12617 type = make_auto ();
12618 break;
12619
12620 case RID_DECLTYPE:
12621 /* Parse the `decltype' type. */
12622 type = cp_parser_decltype (parser);
12623
12624 if (decl_specs)
12625 cp_parser_set_decl_spec_type (decl_specs, type,
12626 token->location,
12627 /*user_defined_p=*/true);
12628
12629 return type;
12630
12631 case RID_TYPEOF:
12632 /* Consume the `typeof' token. */
12633 cp_lexer_consume_token (parser->lexer);
12634 /* Parse the operand to `typeof'. */
12635 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12636 /* If it is not already a TYPE, take its type. */
12637 if (!TYPE_P (type))
12638 type = finish_typeof (type);
12639
12640 if (decl_specs)
12641 cp_parser_set_decl_spec_type (decl_specs, type,
12642 token->location,
12643 /*user_defined_p=*/true);
12644
12645 return type;
12646
12647 default:
12648 break;
12649 }
12650
12651 /* If the type-specifier was for a built-in type, we're done. */
12652 if (type)
12653 {
12654 /* Record the type. */
12655 if (decl_specs
12656 && (token->keyword != RID_SIGNED
12657 && token->keyword != RID_UNSIGNED
12658 && token->keyword != RID_SHORT
12659 && token->keyword != RID_LONG))
12660 cp_parser_set_decl_spec_type (decl_specs,
12661 type,
12662 token->location,
12663 /*user_defined=*/false);
12664 if (decl_specs)
12665 decl_specs->any_specifiers_p = true;
12666
12667 /* Consume the token. */
12668 cp_lexer_consume_token (parser->lexer);
12669
12670 /* There is no valid C++ program where a non-template type is
12671 followed by a "<". That usually indicates that the user thought
12672 that the type was a template. */
12673 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12674
12675 return TYPE_NAME (type);
12676 }
12677
12678 /* The type-specifier must be a user-defined type. */
12679 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12680 {
12681 bool qualified_p;
12682 bool global_p;
12683
12684 /* Don't gobble tokens or issue error messages if this is an
12685 optional type-specifier. */
12686 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12687 cp_parser_parse_tentatively (parser);
12688
12689 /* Look for the optional `::' operator. */
12690 global_p
12691 = (cp_parser_global_scope_opt (parser,
12692 /*current_scope_valid_p=*/false)
12693 != NULL_TREE);
12694 /* Look for the nested-name specifier. */
12695 qualified_p
12696 = (cp_parser_nested_name_specifier_opt (parser,
12697 /*typename_keyword_p=*/false,
12698 /*check_dependency_p=*/true,
12699 /*type_p=*/false,
12700 /*is_declaration=*/false)
12701 != NULL_TREE);
12702 token = cp_lexer_peek_token (parser->lexer);
12703 /* If we have seen a nested-name-specifier, and the next token
12704 is `template', then we are using the template-id production. */
12705 if (parser->scope
12706 && cp_parser_optional_template_keyword (parser))
12707 {
12708 /* Look for the template-id. */
12709 type = cp_parser_template_id (parser,
12710 /*template_keyword_p=*/true,
12711 /*check_dependency_p=*/true,
12712 /*is_declaration=*/false);
12713 /* If the template-id did not name a type, we are out of
12714 luck. */
12715 if (TREE_CODE (type) != TYPE_DECL)
12716 {
12717 cp_parser_error (parser, "expected template-id for type");
12718 type = NULL_TREE;
12719 }
12720 }
12721 /* Otherwise, look for a type-name. */
12722 else
12723 type = cp_parser_type_name (parser);
12724 /* Keep track of all name-lookups performed in class scopes. */
12725 if (type
12726 && !global_p
12727 && !qualified_p
12728 && TREE_CODE (type) == TYPE_DECL
12729 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12730 maybe_note_name_used_in_class (DECL_NAME (type), type);
12731 /* If it didn't work out, we don't have a TYPE. */
12732 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12733 && !cp_parser_parse_definitely (parser))
12734 type = NULL_TREE;
12735 if (type && decl_specs)
12736 cp_parser_set_decl_spec_type (decl_specs, type,
12737 token->location,
12738 /*user_defined=*/true);
12739 }
12740
12741 /* If we didn't get a type-name, issue an error message. */
12742 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12743 {
12744 cp_parser_error (parser, "expected type-name");
12745 return error_mark_node;
12746 }
12747
12748 /* There is no valid C++ program where a non-template type is
12749 followed by a "<". That usually indicates that the user thought
12750 that the type was a template. */
12751 if (type && type != error_mark_node)
12752 {
12753 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12754 If it is, then the '<'...'>' enclose protocol names rather than
12755 template arguments, and so everything is fine. */
12756 if (c_dialect_objc () && !parser->scope
12757 && (objc_is_id (type) || objc_is_class_name (type)))
12758 {
12759 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12760 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12761
12762 /* Clobber the "unqualified" type previously entered into
12763 DECL_SPECS with the new, improved protocol-qualified version. */
12764 if (decl_specs)
12765 decl_specs->type = qual_type;
12766
12767 return qual_type;
12768 }
12769
12770 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12771 token->location);
12772 }
12773
12774 return type;
12775 }
12776
12777 /* Parse a type-name.
12778
12779 type-name:
12780 class-name
12781 enum-name
12782 typedef-name
12783
12784 enum-name:
12785 identifier
12786
12787 typedef-name:
12788 identifier
12789
12790 Returns a TYPE_DECL for the type. */
12791
12792 static tree
12793 cp_parser_type_name (cp_parser* parser)
12794 {
12795 tree type_decl;
12796
12797 /* We can't know yet whether it is a class-name or not. */
12798 cp_parser_parse_tentatively (parser);
12799 /* Try a class-name. */
12800 type_decl = cp_parser_class_name (parser,
12801 /*typename_keyword_p=*/false,
12802 /*template_keyword_p=*/false,
12803 none_type,
12804 /*check_dependency_p=*/true,
12805 /*class_head_p=*/false,
12806 /*is_declaration=*/false);
12807 /* If it's not a class-name, keep looking. */
12808 if (!cp_parser_parse_definitely (parser))
12809 {
12810 /* It must be a typedef-name or an enum-name. */
12811 return cp_parser_nonclass_name (parser);
12812 }
12813
12814 return type_decl;
12815 }
12816
12817 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12818
12819 enum-name:
12820 identifier
12821
12822 typedef-name:
12823 identifier
12824
12825 Returns a TYPE_DECL for the type. */
12826
12827 static tree
12828 cp_parser_nonclass_name (cp_parser* parser)
12829 {
12830 tree type_decl;
12831 tree identifier;
12832
12833 cp_token *token = cp_lexer_peek_token (parser->lexer);
12834 identifier = cp_parser_identifier (parser);
12835 if (identifier == error_mark_node)
12836 return error_mark_node;
12837
12838 /* Look up the type-name. */
12839 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12840
12841 if (TREE_CODE (type_decl) != TYPE_DECL
12842 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12843 {
12844 /* See if this is an Objective-C type. */
12845 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12846 tree type = objc_get_protocol_qualified_type (identifier, protos);
12847 if (type)
12848 type_decl = TYPE_NAME (type);
12849 }
12850
12851 /* Issue an error if we did not find a type-name. */
12852 if (TREE_CODE (type_decl) != TYPE_DECL)
12853 {
12854 if (!cp_parser_simulate_error (parser))
12855 cp_parser_name_lookup_error (parser, identifier, type_decl,
12856 NLE_TYPE, token->location);
12857 return error_mark_node;
12858 }
12859 /* Remember that the name was used in the definition of the
12860 current class so that we can check later to see if the
12861 meaning would have been different after the class was
12862 entirely defined. */
12863 else if (type_decl != error_mark_node
12864 && !parser->scope)
12865 maybe_note_name_used_in_class (identifier, type_decl);
12866
12867 return type_decl;
12868 }
12869
12870 /* Parse an elaborated-type-specifier. Note that the grammar given
12871 here incorporates the resolution to DR68.
12872
12873 elaborated-type-specifier:
12874 class-key :: [opt] nested-name-specifier [opt] identifier
12875 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12876 enum-key :: [opt] nested-name-specifier [opt] identifier
12877 typename :: [opt] nested-name-specifier identifier
12878 typename :: [opt] nested-name-specifier template [opt]
12879 template-id
12880
12881 GNU extension:
12882
12883 elaborated-type-specifier:
12884 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12885 class-key attributes :: [opt] nested-name-specifier [opt]
12886 template [opt] template-id
12887 enum attributes :: [opt] nested-name-specifier [opt] identifier
12888
12889 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12890 declared `friend'. If IS_DECLARATION is TRUE, then this
12891 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12892 something is being declared.
12893
12894 Returns the TYPE specified. */
12895
12896 static tree
12897 cp_parser_elaborated_type_specifier (cp_parser* parser,
12898 bool is_friend,
12899 bool is_declaration)
12900 {
12901 enum tag_types tag_type;
12902 tree identifier;
12903 tree type = NULL_TREE;
12904 tree attributes = NULL_TREE;
12905 tree globalscope;
12906 cp_token *token = NULL;
12907
12908 /* See if we're looking at the `enum' keyword. */
12909 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12910 {
12911 /* Consume the `enum' token. */
12912 cp_lexer_consume_token (parser->lexer);
12913 /* Remember that it's an enumeration type. */
12914 tag_type = enum_type;
12915 /* Parse the optional `struct' or `class' key (for C++0x scoped
12916 enums). */
12917 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12918 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12919 {
12920 if (cxx_dialect == cxx98)
12921 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12922
12923 /* Consume the `struct' or `class'. */
12924 cp_lexer_consume_token (parser->lexer);
12925 }
12926 /* Parse the attributes. */
12927 attributes = cp_parser_attributes_opt (parser);
12928 }
12929 /* Or, it might be `typename'. */
12930 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12931 RID_TYPENAME))
12932 {
12933 /* Consume the `typename' token. */
12934 cp_lexer_consume_token (parser->lexer);
12935 /* Remember that it's a `typename' type. */
12936 tag_type = typename_type;
12937 }
12938 /* Otherwise it must be a class-key. */
12939 else
12940 {
12941 tag_type = cp_parser_class_key (parser);
12942 if (tag_type == none_type)
12943 return error_mark_node;
12944 /* Parse the attributes. */
12945 attributes = cp_parser_attributes_opt (parser);
12946 }
12947
12948 /* Look for the `::' operator. */
12949 globalscope = cp_parser_global_scope_opt (parser,
12950 /*current_scope_valid_p=*/false);
12951 /* Look for the nested-name-specifier. */
12952 if (tag_type == typename_type && !globalscope)
12953 {
12954 if (!cp_parser_nested_name_specifier (parser,
12955 /*typename_keyword_p=*/true,
12956 /*check_dependency_p=*/true,
12957 /*type_p=*/true,
12958 is_declaration))
12959 return error_mark_node;
12960 }
12961 else
12962 /* Even though `typename' is not present, the proposed resolution
12963 to Core Issue 180 says that in `class A<T>::B', `B' should be
12964 considered a type-name, even if `A<T>' is dependent. */
12965 cp_parser_nested_name_specifier_opt (parser,
12966 /*typename_keyword_p=*/true,
12967 /*check_dependency_p=*/true,
12968 /*type_p=*/true,
12969 is_declaration);
12970 /* For everything but enumeration types, consider a template-id.
12971 For an enumeration type, consider only a plain identifier. */
12972 if (tag_type != enum_type)
12973 {
12974 bool template_p = false;
12975 tree decl;
12976
12977 /* Allow the `template' keyword. */
12978 template_p = cp_parser_optional_template_keyword (parser);
12979 /* If we didn't see `template', we don't know if there's a
12980 template-id or not. */
12981 if (!template_p)
12982 cp_parser_parse_tentatively (parser);
12983 /* Parse the template-id. */
12984 token = cp_lexer_peek_token (parser->lexer);
12985 decl = cp_parser_template_id (parser, template_p,
12986 /*check_dependency_p=*/true,
12987 is_declaration);
12988 /* If we didn't find a template-id, look for an ordinary
12989 identifier. */
12990 if (!template_p && !cp_parser_parse_definitely (parser))
12991 ;
12992 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12993 in effect, then we must assume that, upon instantiation, the
12994 template will correspond to a class. */
12995 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12996 && tag_type == typename_type)
12997 type = make_typename_type (parser->scope, decl,
12998 typename_type,
12999 /*complain=*/tf_error);
13000 /* If the `typename' keyword is in effect and DECL is not a type
13001 decl. Then type is non existant. */
13002 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13003 type = NULL_TREE;
13004 else
13005 type = TREE_TYPE (decl);
13006 }
13007
13008 if (!type)
13009 {
13010 token = cp_lexer_peek_token (parser->lexer);
13011 identifier = cp_parser_identifier (parser);
13012
13013 if (identifier == error_mark_node)
13014 {
13015 parser->scope = NULL_TREE;
13016 return error_mark_node;
13017 }
13018
13019 /* For a `typename', we needn't call xref_tag. */
13020 if (tag_type == typename_type
13021 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13022 return cp_parser_make_typename_type (parser, parser->scope,
13023 identifier,
13024 token->location);
13025 /* Look up a qualified name in the usual way. */
13026 if (parser->scope)
13027 {
13028 tree decl;
13029 tree ambiguous_decls;
13030
13031 decl = cp_parser_lookup_name (parser, identifier,
13032 tag_type,
13033 /*is_template=*/false,
13034 /*is_namespace=*/false,
13035 /*check_dependency=*/true,
13036 &ambiguous_decls,
13037 token->location);
13038
13039 /* If the lookup was ambiguous, an error will already have been
13040 issued. */
13041 if (ambiguous_decls)
13042 return error_mark_node;
13043
13044 /* If we are parsing friend declaration, DECL may be a
13045 TEMPLATE_DECL tree node here. However, we need to check
13046 whether this TEMPLATE_DECL results in valid code. Consider
13047 the following example:
13048
13049 namespace N {
13050 template <class T> class C {};
13051 }
13052 class X {
13053 template <class T> friend class N::C; // #1, valid code
13054 };
13055 template <class T> class Y {
13056 friend class N::C; // #2, invalid code
13057 };
13058
13059 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13060 name lookup of `N::C'. We see that friend declaration must
13061 be template for the code to be valid. Note that
13062 processing_template_decl does not work here since it is
13063 always 1 for the above two cases. */
13064
13065 decl = (cp_parser_maybe_treat_template_as_class
13066 (decl, /*tag_name_p=*/is_friend
13067 && parser->num_template_parameter_lists));
13068
13069 if (TREE_CODE (decl) != TYPE_DECL)
13070 {
13071 cp_parser_diagnose_invalid_type_name (parser,
13072 parser->scope,
13073 identifier,
13074 token->location);
13075 return error_mark_node;
13076 }
13077
13078 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13079 {
13080 bool allow_template = (parser->num_template_parameter_lists
13081 || DECL_SELF_REFERENCE_P (decl));
13082 type = check_elaborated_type_specifier (tag_type, decl,
13083 allow_template);
13084
13085 if (type == error_mark_node)
13086 return error_mark_node;
13087 }
13088
13089 /* Forward declarations of nested types, such as
13090
13091 class C1::C2;
13092 class C1::C2::C3;
13093
13094 are invalid unless all components preceding the final '::'
13095 are complete. If all enclosing types are complete, these
13096 declarations become merely pointless.
13097
13098 Invalid forward declarations of nested types are errors
13099 caught elsewhere in parsing. Those that are pointless arrive
13100 here. */
13101
13102 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13103 && !is_friend && !processing_explicit_instantiation)
13104 warning (0, "declaration %qD does not declare anything", decl);
13105
13106 type = TREE_TYPE (decl);
13107 }
13108 else
13109 {
13110 /* An elaborated-type-specifier sometimes introduces a new type and
13111 sometimes names an existing type. Normally, the rule is that it
13112 introduces a new type only if there is not an existing type of
13113 the same name already in scope. For example, given:
13114
13115 struct S {};
13116 void f() { struct S s; }
13117
13118 the `struct S' in the body of `f' is the same `struct S' as in
13119 the global scope; the existing definition is used. However, if
13120 there were no global declaration, this would introduce a new
13121 local class named `S'.
13122
13123 An exception to this rule applies to the following code:
13124
13125 namespace N { struct S; }
13126
13127 Here, the elaborated-type-specifier names a new type
13128 unconditionally; even if there is already an `S' in the
13129 containing scope this declaration names a new type.
13130 This exception only applies if the elaborated-type-specifier
13131 forms the complete declaration:
13132
13133 [class.name]
13134
13135 A declaration consisting solely of `class-key identifier ;' is
13136 either a redeclaration of the name in the current scope or a
13137 forward declaration of the identifier as a class name. It
13138 introduces the name into the current scope.
13139
13140 We are in this situation precisely when the next token is a `;'.
13141
13142 An exception to the exception is that a `friend' declaration does
13143 *not* name a new type; i.e., given:
13144
13145 struct S { friend struct T; };
13146
13147 `T' is not a new type in the scope of `S'.
13148
13149 Also, `new struct S' or `sizeof (struct S)' never results in the
13150 definition of a new type; a new type can only be declared in a
13151 declaration context. */
13152
13153 tag_scope ts;
13154 bool template_p;
13155
13156 if (is_friend)
13157 /* Friends have special name lookup rules. */
13158 ts = ts_within_enclosing_non_class;
13159 else if (is_declaration
13160 && cp_lexer_next_token_is (parser->lexer,
13161 CPP_SEMICOLON))
13162 /* This is a `class-key identifier ;' */
13163 ts = ts_current;
13164 else
13165 ts = ts_global;
13166
13167 template_p =
13168 (parser->num_template_parameter_lists
13169 && (cp_parser_next_token_starts_class_definition_p (parser)
13170 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13171 /* An unqualified name was used to reference this type, so
13172 there were no qualifying templates. */
13173 if (!cp_parser_check_template_parameters (parser,
13174 /*num_templates=*/0,
13175 token->location,
13176 /*declarator=*/NULL))
13177 return error_mark_node;
13178 type = xref_tag (tag_type, identifier, ts, template_p);
13179 }
13180 }
13181
13182 if (type == error_mark_node)
13183 return error_mark_node;
13184
13185 /* Allow attributes on forward declarations of classes. */
13186 if (attributes)
13187 {
13188 if (TREE_CODE (type) == TYPENAME_TYPE)
13189 warning (OPT_Wattributes,
13190 "attributes ignored on uninstantiated type");
13191 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13192 && ! processing_explicit_instantiation)
13193 warning (OPT_Wattributes,
13194 "attributes ignored on template instantiation");
13195 else if (is_declaration && cp_parser_declares_only_class_p (parser))
13196 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13197 else
13198 warning (OPT_Wattributes,
13199 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13200 }
13201
13202 if (tag_type != enum_type)
13203 cp_parser_check_class_key (tag_type, type);
13204
13205 /* A "<" cannot follow an elaborated type specifier. If that
13206 happens, the user was probably trying to form a template-id. */
13207 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13208
13209 return type;
13210 }
13211
13212 /* Parse an enum-specifier.
13213
13214 enum-specifier:
13215 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
13216
13217 enum-key:
13218 enum
13219 enum class [C++0x]
13220 enum struct [C++0x]
13221
13222 enum-base: [C++0x]
13223 : type-specifier-seq
13224
13225 GNU Extensions:
13226 enum-key attributes[opt] identifier [opt] enum-base [opt]
13227 { enumerator-list [opt] }attributes[opt]
13228
13229 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13230 if the token stream isn't an enum-specifier after all. */
13231
13232 static tree
13233 cp_parser_enum_specifier (cp_parser* parser)
13234 {
13235 tree identifier;
13236 tree type;
13237 tree attributes;
13238 bool scoped_enum_p = false;
13239 bool has_underlying_type = false;
13240 tree underlying_type = NULL_TREE;
13241
13242 /* Parse tentatively so that we can back up if we don't find a
13243 enum-specifier. */
13244 cp_parser_parse_tentatively (parser);
13245
13246 /* Caller guarantees that the current token is 'enum', an identifier
13247 possibly follows, and the token after that is an opening brace.
13248 If we don't have an identifier, fabricate an anonymous name for
13249 the enumeration being defined. */
13250 cp_lexer_consume_token (parser->lexer);
13251
13252 /* Parse the "class" or "struct", which indicates a scoped
13253 enumeration type in C++0x. */
13254 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13255 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13256 {
13257 if (cxx_dialect == cxx98)
13258 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13259
13260 /* Consume the `struct' or `class' token. */
13261 cp_lexer_consume_token (parser->lexer);
13262
13263 scoped_enum_p = true;
13264 }
13265
13266 attributes = cp_parser_attributes_opt (parser);
13267
13268 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13269 identifier = cp_parser_identifier (parser);
13270 else
13271 identifier = make_anon_name ();
13272
13273 /* Check for the `:' that denotes a specified underlying type in C++0x.
13274 Note that a ':' could also indicate a bitfield width, however. */
13275 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13276 {
13277 cp_decl_specifier_seq type_specifiers;
13278
13279 /* Consume the `:'. */
13280 cp_lexer_consume_token (parser->lexer);
13281
13282 /* Parse the type-specifier-seq. */
13283 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13284 /*is_trailing_return=*/false,
13285 &type_specifiers);
13286
13287 /* At this point this is surely not elaborated type specifier. */
13288 if (!cp_parser_parse_definitely (parser))
13289 return NULL_TREE;
13290
13291 if (cxx_dialect == cxx98)
13292 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13293
13294 has_underlying_type = true;
13295
13296 /* If that didn't work, stop. */
13297 if (type_specifiers.type != error_mark_node)
13298 {
13299 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13300 /*initialized=*/0, NULL);
13301 if (underlying_type == error_mark_node)
13302 underlying_type = NULL_TREE;
13303 }
13304 }
13305
13306 /* Look for the `{' but don't consume it yet. */
13307 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13308 {
13309 cp_parser_error (parser, "expected %<{%>");
13310 if (has_underlying_type)
13311 return NULL_TREE;
13312 }
13313
13314 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13315 return NULL_TREE;
13316
13317 /* Issue an error message if type-definitions are forbidden here. */
13318 if (!cp_parser_check_type_definition (parser))
13319 type = error_mark_node;
13320 else
13321 /* Create the new type. We do this before consuming the opening
13322 brace so the enum will be recorded as being on the line of its
13323 tag (or the 'enum' keyword, if there is no tag). */
13324 type = start_enum (identifier, underlying_type, scoped_enum_p);
13325
13326 /* Consume the opening brace. */
13327 cp_lexer_consume_token (parser->lexer);
13328
13329 if (type == error_mark_node)
13330 {
13331 cp_parser_skip_to_end_of_block_or_statement (parser);
13332 return error_mark_node;
13333 }
13334
13335 /* If the next token is not '}', then there are some enumerators. */
13336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13337 cp_parser_enumerator_list (parser, type);
13338
13339 /* Consume the final '}'. */
13340 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13341
13342 /* Look for trailing attributes to apply to this enumeration, and
13343 apply them if appropriate. */
13344 if (cp_parser_allow_gnu_extensions_p (parser))
13345 {
13346 tree trailing_attr = cp_parser_attributes_opt (parser);
13347 trailing_attr = chainon (trailing_attr, attributes);
13348 cplus_decl_attributes (&type,
13349 trailing_attr,
13350 (int) ATTR_FLAG_TYPE_IN_PLACE);
13351 }
13352
13353 /* Finish up the enumeration. */
13354 finish_enum (type);
13355
13356 return type;
13357 }
13358
13359 /* Parse an enumerator-list. The enumerators all have the indicated
13360 TYPE.
13361
13362 enumerator-list:
13363 enumerator-definition
13364 enumerator-list , enumerator-definition */
13365
13366 static void
13367 cp_parser_enumerator_list (cp_parser* parser, tree type)
13368 {
13369 while (true)
13370 {
13371 /* Parse an enumerator-definition. */
13372 cp_parser_enumerator_definition (parser, type);
13373
13374 /* If the next token is not a ',', we've reached the end of
13375 the list. */
13376 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13377 break;
13378 /* Otherwise, consume the `,' and keep going. */
13379 cp_lexer_consume_token (parser->lexer);
13380 /* If the next token is a `}', there is a trailing comma. */
13381 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13382 {
13383 if (!in_system_header)
13384 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13385 break;
13386 }
13387 }
13388 }
13389
13390 /* Parse an enumerator-definition. The enumerator has the indicated
13391 TYPE.
13392
13393 enumerator-definition:
13394 enumerator
13395 enumerator = constant-expression
13396
13397 enumerator:
13398 identifier */
13399
13400 static void
13401 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13402 {
13403 tree identifier;
13404 tree value;
13405 location_t loc;
13406
13407 /* Save the input location because we are interested in the location
13408 of the identifier and not the location of the explicit value. */
13409 loc = cp_lexer_peek_token (parser->lexer)->location;
13410
13411 /* Look for the identifier. */
13412 identifier = cp_parser_identifier (parser);
13413 if (identifier == error_mark_node)
13414 return;
13415
13416 /* If the next token is an '=', then there is an explicit value. */
13417 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13418 {
13419 /* Consume the `=' token. */
13420 cp_lexer_consume_token (parser->lexer);
13421 /* Parse the value. */
13422 value = cp_parser_constant_expression (parser,
13423 /*allow_non_constant_p=*/false,
13424 NULL);
13425 }
13426 else
13427 value = NULL_TREE;
13428
13429 /* If we are processing a template, make sure the initializer of the
13430 enumerator doesn't contain any bare template parameter pack. */
13431 if (check_for_bare_parameter_packs (value))
13432 value = error_mark_node;
13433
13434 /* Create the enumerator. */
13435 build_enumerator (identifier, value, type, loc);
13436 }
13437
13438 /* Parse a namespace-name.
13439
13440 namespace-name:
13441 original-namespace-name
13442 namespace-alias
13443
13444 Returns the NAMESPACE_DECL for the namespace. */
13445
13446 static tree
13447 cp_parser_namespace_name (cp_parser* parser)
13448 {
13449 tree identifier;
13450 tree namespace_decl;
13451
13452 cp_token *token = cp_lexer_peek_token (parser->lexer);
13453
13454 /* Get the name of the namespace. */
13455 identifier = cp_parser_identifier (parser);
13456 if (identifier == error_mark_node)
13457 return error_mark_node;
13458
13459 /* Look up the identifier in the currently active scope. Look only
13460 for namespaces, due to:
13461
13462 [basic.lookup.udir]
13463
13464 When looking up a namespace-name in a using-directive or alias
13465 definition, only namespace names are considered.
13466
13467 And:
13468
13469 [basic.lookup.qual]
13470
13471 During the lookup of a name preceding the :: scope resolution
13472 operator, object, function, and enumerator names are ignored.
13473
13474 (Note that cp_parser_qualifying_entity only calls this
13475 function if the token after the name is the scope resolution
13476 operator.) */
13477 namespace_decl = cp_parser_lookup_name (parser, identifier,
13478 none_type,
13479 /*is_template=*/false,
13480 /*is_namespace=*/true,
13481 /*check_dependency=*/true,
13482 /*ambiguous_decls=*/NULL,
13483 token->location);
13484 /* If it's not a namespace, issue an error. */
13485 if (namespace_decl == error_mark_node
13486 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13487 {
13488 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13489 error_at (token->location, "%qD is not a namespace-name", identifier);
13490 cp_parser_error (parser, "expected namespace-name");
13491 namespace_decl = error_mark_node;
13492 }
13493
13494 return namespace_decl;
13495 }
13496
13497 /* Parse a namespace-definition.
13498
13499 namespace-definition:
13500 named-namespace-definition
13501 unnamed-namespace-definition
13502
13503 named-namespace-definition:
13504 original-namespace-definition
13505 extension-namespace-definition
13506
13507 original-namespace-definition:
13508 namespace identifier { namespace-body }
13509
13510 extension-namespace-definition:
13511 namespace original-namespace-name { namespace-body }
13512
13513 unnamed-namespace-definition:
13514 namespace { namespace-body } */
13515
13516 static void
13517 cp_parser_namespace_definition (cp_parser* parser)
13518 {
13519 tree identifier, attribs;
13520 bool has_visibility;
13521 bool is_inline;
13522
13523 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13524 {
13525 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13526 is_inline = true;
13527 cp_lexer_consume_token (parser->lexer);
13528 }
13529 else
13530 is_inline = false;
13531
13532 /* Look for the `namespace' keyword. */
13533 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13534
13535 /* Get the name of the namespace. We do not attempt to distinguish
13536 between an original-namespace-definition and an
13537 extension-namespace-definition at this point. The semantic
13538 analysis routines are responsible for that. */
13539 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13540 identifier = cp_parser_identifier (parser);
13541 else
13542 identifier = NULL_TREE;
13543
13544 /* Parse any specified attributes. */
13545 attribs = cp_parser_attributes_opt (parser);
13546
13547 /* Look for the `{' to start the namespace. */
13548 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13549 /* Start the namespace. */
13550 push_namespace (identifier);
13551
13552 /* "inline namespace" is equivalent to a stub namespace definition
13553 followed by a strong using directive. */
13554 if (is_inline)
13555 {
13556 tree name_space = current_namespace;
13557 /* Set up namespace association. */
13558 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13559 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13560 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13561 /* Import the contents of the inline namespace. */
13562 pop_namespace ();
13563 do_using_directive (name_space);
13564 push_namespace (identifier);
13565 }
13566
13567 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13568
13569 /* Parse the body of the namespace. */
13570 cp_parser_namespace_body (parser);
13571
13572 #ifdef HANDLE_PRAGMA_VISIBILITY
13573 if (has_visibility)
13574 pop_visibility (1);
13575 #endif
13576
13577 /* Finish the namespace. */
13578 pop_namespace ();
13579 /* Look for the final `}'. */
13580 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13581 }
13582
13583 /* Parse a namespace-body.
13584
13585 namespace-body:
13586 declaration-seq [opt] */
13587
13588 static void
13589 cp_parser_namespace_body (cp_parser* parser)
13590 {
13591 cp_parser_declaration_seq_opt (parser);
13592 }
13593
13594 /* Parse a namespace-alias-definition.
13595
13596 namespace-alias-definition:
13597 namespace identifier = qualified-namespace-specifier ; */
13598
13599 static void
13600 cp_parser_namespace_alias_definition (cp_parser* parser)
13601 {
13602 tree identifier;
13603 tree namespace_specifier;
13604
13605 cp_token *token = cp_lexer_peek_token (parser->lexer);
13606
13607 /* Look for the `namespace' keyword. */
13608 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13609 /* Look for the identifier. */
13610 identifier = cp_parser_identifier (parser);
13611 if (identifier == error_mark_node)
13612 return;
13613 /* Look for the `=' token. */
13614 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13615 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13616 {
13617 error_at (token->location, "%<namespace%> definition is not allowed here");
13618 /* Skip the definition. */
13619 cp_lexer_consume_token (parser->lexer);
13620 if (cp_parser_skip_to_closing_brace (parser))
13621 cp_lexer_consume_token (parser->lexer);
13622 return;
13623 }
13624 cp_parser_require (parser, CPP_EQ, RT_EQ);
13625 /* Look for the qualified-namespace-specifier. */
13626 namespace_specifier
13627 = cp_parser_qualified_namespace_specifier (parser);
13628 /* Look for the `;' token. */
13629 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13630
13631 /* Register the alias in the symbol table. */
13632 do_namespace_alias (identifier, namespace_specifier);
13633 }
13634
13635 /* Parse a qualified-namespace-specifier.
13636
13637 qualified-namespace-specifier:
13638 :: [opt] nested-name-specifier [opt] namespace-name
13639
13640 Returns a NAMESPACE_DECL corresponding to the specified
13641 namespace. */
13642
13643 static tree
13644 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13645 {
13646 /* Look for the optional `::'. */
13647 cp_parser_global_scope_opt (parser,
13648 /*current_scope_valid_p=*/false);
13649
13650 /* Look for the optional nested-name-specifier. */
13651 cp_parser_nested_name_specifier_opt (parser,
13652 /*typename_keyword_p=*/false,
13653 /*check_dependency_p=*/true,
13654 /*type_p=*/false,
13655 /*is_declaration=*/true);
13656
13657 return cp_parser_namespace_name (parser);
13658 }
13659
13660 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13661 access declaration.
13662
13663 using-declaration:
13664 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13665 using :: unqualified-id ;
13666
13667 access-declaration:
13668 qualified-id ;
13669
13670 */
13671
13672 static bool
13673 cp_parser_using_declaration (cp_parser* parser,
13674 bool access_declaration_p)
13675 {
13676 cp_token *token;
13677 bool typename_p = false;
13678 bool global_scope_p;
13679 tree decl;
13680 tree identifier;
13681 tree qscope;
13682
13683 if (access_declaration_p)
13684 cp_parser_parse_tentatively (parser);
13685 else
13686 {
13687 /* Look for the `using' keyword. */
13688 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13689
13690 /* Peek at the next token. */
13691 token = cp_lexer_peek_token (parser->lexer);
13692 /* See if it's `typename'. */
13693 if (token->keyword == RID_TYPENAME)
13694 {
13695 /* Remember that we've seen it. */
13696 typename_p = true;
13697 /* Consume the `typename' token. */
13698 cp_lexer_consume_token (parser->lexer);
13699 }
13700 }
13701
13702 /* Look for the optional global scope qualification. */
13703 global_scope_p
13704 = (cp_parser_global_scope_opt (parser,
13705 /*current_scope_valid_p=*/false)
13706 != NULL_TREE);
13707
13708 /* If we saw `typename', or didn't see `::', then there must be a
13709 nested-name-specifier present. */
13710 if (typename_p || !global_scope_p)
13711 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13712 /*check_dependency_p=*/true,
13713 /*type_p=*/false,
13714 /*is_declaration=*/true);
13715 /* Otherwise, we could be in either of the two productions. In that
13716 case, treat the nested-name-specifier as optional. */
13717 else
13718 qscope = cp_parser_nested_name_specifier_opt (parser,
13719 /*typename_keyword_p=*/false,
13720 /*check_dependency_p=*/true,
13721 /*type_p=*/false,
13722 /*is_declaration=*/true);
13723 if (!qscope)
13724 qscope = global_namespace;
13725
13726 if (access_declaration_p && cp_parser_error_occurred (parser))
13727 /* Something has already gone wrong; there's no need to parse
13728 further. Since an error has occurred, the return value of
13729 cp_parser_parse_definitely will be false, as required. */
13730 return cp_parser_parse_definitely (parser);
13731
13732 token = cp_lexer_peek_token (parser->lexer);
13733 /* Parse the unqualified-id. */
13734 identifier = cp_parser_unqualified_id (parser,
13735 /*template_keyword_p=*/false,
13736 /*check_dependency_p=*/true,
13737 /*declarator_p=*/true,
13738 /*optional_p=*/false);
13739
13740 if (access_declaration_p)
13741 {
13742 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13743 cp_parser_simulate_error (parser);
13744 if (!cp_parser_parse_definitely (parser))
13745 return false;
13746 }
13747
13748 /* The function we call to handle a using-declaration is different
13749 depending on what scope we are in. */
13750 if (qscope == error_mark_node || identifier == error_mark_node)
13751 ;
13752 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13753 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13754 /* [namespace.udecl]
13755
13756 A using declaration shall not name a template-id. */
13757 error_at (token->location,
13758 "a template-id may not appear in a using-declaration");
13759 else
13760 {
13761 if (at_class_scope_p ())
13762 {
13763 /* Create the USING_DECL. */
13764 decl = do_class_using_decl (parser->scope, identifier);
13765
13766 if (check_for_bare_parameter_packs (decl))
13767 return false;
13768 else
13769 /* Add it to the list of members in this class. */
13770 finish_member_declaration (decl);
13771 }
13772 else
13773 {
13774 decl = cp_parser_lookup_name_simple (parser,
13775 identifier,
13776 token->location);
13777 if (decl == error_mark_node)
13778 cp_parser_name_lookup_error (parser, identifier,
13779 decl, NLE_NULL,
13780 token->location);
13781 else if (check_for_bare_parameter_packs (decl))
13782 return false;
13783 else if (!at_namespace_scope_p ())
13784 do_local_using_decl (decl, qscope, identifier);
13785 else
13786 do_toplevel_using_decl (decl, qscope, identifier);
13787 }
13788 }
13789
13790 /* Look for the final `;'. */
13791 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13792
13793 return true;
13794 }
13795
13796 /* Parse a using-directive.
13797
13798 using-directive:
13799 using namespace :: [opt] nested-name-specifier [opt]
13800 namespace-name ; */
13801
13802 static void
13803 cp_parser_using_directive (cp_parser* parser)
13804 {
13805 tree namespace_decl;
13806 tree attribs;
13807
13808 /* Look for the `using' keyword. */
13809 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13810 /* And the `namespace' keyword. */
13811 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13812 /* Look for the optional `::' operator. */
13813 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13814 /* And the optional nested-name-specifier. */
13815 cp_parser_nested_name_specifier_opt (parser,
13816 /*typename_keyword_p=*/false,
13817 /*check_dependency_p=*/true,
13818 /*type_p=*/false,
13819 /*is_declaration=*/true);
13820 /* Get the namespace being used. */
13821 namespace_decl = cp_parser_namespace_name (parser);
13822 /* And any specified attributes. */
13823 attribs = cp_parser_attributes_opt (parser);
13824 /* Update the symbol table. */
13825 parse_using_directive (namespace_decl, attribs);
13826 /* Look for the final `;'. */
13827 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13828 }
13829
13830 /* Parse an asm-definition.
13831
13832 asm-definition:
13833 asm ( string-literal ) ;
13834
13835 GNU Extension:
13836
13837 asm-definition:
13838 asm volatile [opt] ( string-literal ) ;
13839 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13840 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13841 : asm-operand-list [opt] ) ;
13842 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13843 : asm-operand-list [opt]
13844 : asm-clobber-list [opt] ) ;
13845 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13846 : asm-clobber-list [opt]
13847 : asm-goto-list ) ; */
13848
13849 static void
13850 cp_parser_asm_definition (cp_parser* parser)
13851 {
13852 tree string;
13853 tree outputs = NULL_TREE;
13854 tree inputs = NULL_TREE;
13855 tree clobbers = NULL_TREE;
13856 tree labels = NULL_TREE;
13857 tree asm_stmt;
13858 bool volatile_p = false;
13859 bool extended_p = false;
13860 bool invalid_inputs_p = false;
13861 bool invalid_outputs_p = false;
13862 bool goto_p = false;
13863 required_token missing = RT_NONE;
13864
13865 /* Look for the `asm' keyword. */
13866 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
13867 /* See if the next token is `volatile'. */
13868 if (cp_parser_allow_gnu_extensions_p (parser)
13869 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13870 {
13871 /* Remember that we saw the `volatile' keyword. */
13872 volatile_p = true;
13873 /* Consume the token. */
13874 cp_lexer_consume_token (parser->lexer);
13875 }
13876 if (cp_parser_allow_gnu_extensions_p (parser)
13877 && parser->in_function_body
13878 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13879 {
13880 /* Remember that we saw the `goto' keyword. */
13881 goto_p = true;
13882 /* Consume the token. */
13883 cp_lexer_consume_token (parser->lexer);
13884 }
13885 /* Look for the opening `('. */
13886 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13887 return;
13888 /* Look for the string. */
13889 string = cp_parser_string_literal (parser, false, false);
13890 if (string == error_mark_node)
13891 {
13892 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13893 /*consume_paren=*/true);
13894 return;
13895 }
13896
13897 /* If we're allowing GNU extensions, check for the extended assembly
13898 syntax. Unfortunately, the `:' tokens need not be separated by
13899 a space in C, and so, for compatibility, we tolerate that here
13900 too. Doing that means that we have to treat the `::' operator as
13901 two `:' tokens. */
13902 if (cp_parser_allow_gnu_extensions_p (parser)
13903 && parser->in_function_body
13904 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13905 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13906 {
13907 bool inputs_p = false;
13908 bool clobbers_p = false;
13909 bool labels_p = false;
13910
13911 /* The extended syntax was used. */
13912 extended_p = true;
13913
13914 /* Look for outputs. */
13915 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13916 {
13917 /* Consume the `:'. */
13918 cp_lexer_consume_token (parser->lexer);
13919 /* Parse the output-operands. */
13920 if (cp_lexer_next_token_is_not (parser->lexer,
13921 CPP_COLON)
13922 && cp_lexer_next_token_is_not (parser->lexer,
13923 CPP_SCOPE)
13924 && cp_lexer_next_token_is_not (parser->lexer,
13925 CPP_CLOSE_PAREN)
13926 && !goto_p)
13927 outputs = cp_parser_asm_operand_list (parser);
13928
13929 if (outputs == error_mark_node)
13930 invalid_outputs_p = true;
13931 }
13932 /* If the next token is `::', there are no outputs, and the
13933 next token is the beginning of the inputs. */
13934 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13935 /* The inputs are coming next. */
13936 inputs_p = true;
13937
13938 /* Look for inputs. */
13939 if (inputs_p
13940 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13941 {
13942 /* Consume the `:' or `::'. */
13943 cp_lexer_consume_token (parser->lexer);
13944 /* Parse the output-operands. */
13945 if (cp_lexer_next_token_is_not (parser->lexer,
13946 CPP_COLON)
13947 && cp_lexer_next_token_is_not (parser->lexer,
13948 CPP_SCOPE)
13949 && cp_lexer_next_token_is_not (parser->lexer,
13950 CPP_CLOSE_PAREN))
13951 inputs = cp_parser_asm_operand_list (parser);
13952
13953 if (inputs == error_mark_node)
13954 invalid_inputs_p = true;
13955 }
13956 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13957 /* The clobbers are coming next. */
13958 clobbers_p = true;
13959
13960 /* Look for clobbers. */
13961 if (clobbers_p
13962 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13963 {
13964 clobbers_p = true;
13965 /* Consume the `:' or `::'. */
13966 cp_lexer_consume_token (parser->lexer);
13967 /* Parse the clobbers. */
13968 if (cp_lexer_next_token_is_not (parser->lexer,
13969 CPP_COLON)
13970 && cp_lexer_next_token_is_not (parser->lexer,
13971 CPP_CLOSE_PAREN))
13972 clobbers = cp_parser_asm_clobber_list (parser);
13973 }
13974 else if (goto_p
13975 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13976 /* The labels are coming next. */
13977 labels_p = true;
13978
13979 /* Look for labels. */
13980 if (labels_p
13981 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13982 {
13983 labels_p = true;
13984 /* Consume the `:' or `::'. */
13985 cp_lexer_consume_token (parser->lexer);
13986 /* Parse the labels. */
13987 labels = cp_parser_asm_label_list (parser);
13988 }
13989
13990 if (goto_p && !labels_p)
13991 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
13992 }
13993 else if (goto_p)
13994 missing = RT_COLON_SCOPE;
13995
13996 /* Look for the closing `)'. */
13997 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13998 missing ? missing : RT_CLOSE_PAREN))
13999 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14000 /*consume_paren=*/true);
14001 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14002
14003 if (!invalid_inputs_p && !invalid_outputs_p)
14004 {
14005 /* Create the ASM_EXPR. */
14006 if (parser->in_function_body)
14007 {
14008 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14009 inputs, clobbers, labels);
14010 /* If the extended syntax was not used, mark the ASM_EXPR. */
14011 if (!extended_p)
14012 {
14013 tree temp = asm_stmt;
14014 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14015 temp = TREE_OPERAND (temp, 0);
14016
14017 ASM_INPUT_P (temp) = 1;
14018 }
14019 }
14020 else
14021 cgraph_add_asm_node (string);
14022 }
14023 }
14024
14025 /* Declarators [gram.dcl.decl] */
14026
14027 /* Parse an init-declarator.
14028
14029 init-declarator:
14030 declarator initializer [opt]
14031
14032 GNU Extension:
14033
14034 init-declarator:
14035 declarator asm-specification [opt] attributes [opt] initializer [opt]
14036
14037 function-definition:
14038 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14039 function-body
14040 decl-specifier-seq [opt] declarator function-try-block
14041
14042 GNU Extension:
14043
14044 function-definition:
14045 __extension__ function-definition
14046
14047 The DECL_SPECIFIERS apply to this declarator. Returns a
14048 representation of the entity declared. If MEMBER_P is TRUE, then
14049 this declarator appears in a class scope. The new DECL created by
14050 this declarator is returned.
14051
14052 The CHECKS are access checks that should be performed once we know
14053 what entity is being declared (and, therefore, what classes have
14054 befriended it).
14055
14056 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14057 for a function-definition here as well. If the declarator is a
14058 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14059 be TRUE upon return. By that point, the function-definition will
14060 have been completely parsed.
14061
14062 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14063 is FALSE. */
14064
14065 static tree
14066 cp_parser_init_declarator (cp_parser* parser,
14067 cp_decl_specifier_seq *decl_specifiers,
14068 VEC (deferred_access_check,gc)* checks,
14069 bool function_definition_allowed_p,
14070 bool member_p,
14071 int declares_class_or_enum,
14072 bool* function_definition_p)
14073 {
14074 cp_token *token = NULL, *asm_spec_start_token = NULL,
14075 *attributes_start_token = NULL;
14076 cp_declarator *declarator;
14077 tree prefix_attributes;
14078 tree attributes;
14079 tree asm_specification;
14080 tree initializer;
14081 tree decl = NULL_TREE;
14082 tree scope;
14083 int is_initialized;
14084 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14085 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14086 "(...)". */
14087 enum cpp_ttype initialization_kind;
14088 bool is_direct_init = false;
14089 bool is_non_constant_init;
14090 int ctor_dtor_or_conv_p;
14091 bool friend_p;
14092 tree pushed_scope = NULL;
14093
14094 /* Gather the attributes that were provided with the
14095 decl-specifiers. */
14096 prefix_attributes = decl_specifiers->attributes;
14097
14098 /* Assume that this is not the declarator for a function
14099 definition. */
14100 if (function_definition_p)
14101 *function_definition_p = false;
14102
14103 /* Defer access checks while parsing the declarator; we cannot know
14104 what names are accessible until we know what is being
14105 declared. */
14106 resume_deferring_access_checks ();
14107
14108 /* Parse the declarator. */
14109 token = cp_lexer_peek_token (parser->lexer);
14110 declarator
14111 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14112 &ctor_dtor_or_conv_p,
14113 /*parenthesized_p=*/NULL,
14114 /*member_p=*/false);
14115 /* Gather up the deferred checks. */
14116 stop_deferring_access_checks ();
14117
14118 /* If the DECLARATOR was erroneous, there's no need to go
14119 further. */
14120 if (declarator == cp_error_declarator)
14121 return error_mark_node;
14122
14123 /* Check that the number of template-parameter-lists is OK. */
14124 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14125 token->location))
14126 return error_mark_node;
14127
14128 if (declares_class_or_enum & 2)
14129 cp_parser_check_for_definition_in_return_type (declarator,
14130 decl_specifiers->type,
14131 decl_specifiers->type_location);
14132
14133 /* Figure out what scope the entity declared by the DECLARATOR is
14134 located in. `grokdeclarator' sometimes changes the scope, so
14135 we compute it now. */
14136 scope = get_scope_of_declarator (declarator);
14137
14138 /* Perform any lookups in the declared type which were thought to be
14139 dependent, but are not in the scope of the declarator. */
14140 decl_specifiers->type
14141 = maybe_update_decl_type (decl_specifiers->type, scope);
14142
14143 /* If we're allowing GNU extensions, look for an asm-specification
14144 and attributes. */
14145 if (cp_parser_allow_gnu_extensions_p (parser))
14146 {
14147 /* Look for an asm-specification. */
14148 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14149 asm_specification = cp_parser_asm_specification_opt (parser);
14150 /* And attributes. */
14151 attributes_start_token = cp_lexer_peek_token (parser->lexer);
14152 attributes = cp_parser_attributes_opt (parser);
14153 }
14154 else
14155 {
14156 asm_specification = NULL_TREE;
14157 attributes = NULL_TREE;
14158 }
14159
14160 /* Peek at the next token. */
14161 token = cp_lexer_peek_token (parser->lexer);
14162 /* Check to see if the token indicates the start of a
14163 function-definition. */
14164 if (function_declarator_p (declarator)
14165 && cp_parser_token_starts_function_definition_p (token))
14166 {
14167 if (!function_definition_allowed_p)
14168 {
14169 /* If a function-definition should not appear here, issue an
14170 error message. */
14171 cp_parser_error (parser,
14172 "a function-definition is not allowed here");
14173 return error_mark_node;
14174 }
14175 else
14176 {
14177 location_t func_brace_location
14178 = cp_lexer_peek_token (parser->lexer)->location;
14179
14180 /* Neither attributes nor an asm-specification are allowed
14181 on a function-definition. */
14182 if (asm_specification)
14183 error_at (asm_spec_start_token->location,
14184 "an asm-specification is not allowed "
14185 "on a function-definition");
14186 if (attributes)
14187 error_at (attributes_start_token->location,
14188 "attributes are not allowed on a function-definition");
14189 /* This is a function-definition. */
14190 *function_definition_p = true;
14191
14192 /* Parse the function definition. */
14193 if (member_p)
14194 decl = cp_parser_save_member_function_body (parser,
14195 decl_specifiers,
14196 declarator,
14197 prefix_attributes);
14198 else
14199 decl
14200 = (cp_parser_function_definition_from_specifiers_and_declarator
14201 (parser, decl_specifiers, prefix_attributes, declarator));
14202
14203 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14204 {
14205 /* This is where the prologue starts... */
14206 DECL_STRUCT_FUNCTION (decl)->function_start_locus
14207 = func_brace_location;
14208 }
14209
14210 return decl;
14211 }
14212 }
14213
14214 /* [dcl.dcl]
14215
14216 Only in function declarations for constructors, destructors, and
14217 type conversions can the decl-specifier-seq be omitted.
14218
14219 We explicitly postpone this check past the point where we handle
14220 function-definitions because we tolerate function-definitions
14221 that are missing their return types in some modes. */
14222 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14223 {
14224 cp_parser_error (parser,
14225 "expected constructor, destructor, or type conversion");
14226 return error_mark_node;
14227 }
14228
14229 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14230 if (token->type == CPP_EQ
14231 || token->type == CPP_OPEN_PAREN
14232 || token->type == CPP_OPEN_BRACE)
14233 {
14234 is_initialized = SD_INITIALIZED;
14235 initialization_kind = token->type;
14236
14237 if (token->type == CPP_EQ
14238 && function_declarator_p (declarator))
14239 {
14240 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14241 if (t2->keyword == RID_DEFAULT)
14242 is_initialized = SD_DEFAULTED;
14243 else if (t2->keyword == RID_DELETE)
14244 is_initialized = SD_DELETED;
14245 }
14246 }
14247 else
14248 {
14249 /* If the init-declarator isn't initialized and isn't followed by a
14250 `,' or `;', it's not a valid init-declarator. */
14251 if (token->type != CPP_COMMA
14252 && token->type != CPP_SEMICOLON)
14253 {
14254 cp_parser_error (parser, "expected initializer");
14255 return error_mark_node;
14256 }
14257 is_initialized = SD_UNINITIALIZED;
14258 initialization_kind = CPP_EOF;
14259 }
14260
14261 /* Because start_decl has side-effects, we should only call it if we
14262 know we're going ahead. By this point, we know that we cannot
14263 possibly be looking at any other construct. */
14264 cp_parser_commit_to_tentative_parse (parser);
14265
14266 /* If the decl specifiers were bad, issue an error now that we're
14267 sure this was intended to be a declarator. Then continue
14268 declaring the variable(s), as int, to try to cut down on further
14269 errors. */
14270 if (decl_specifiers->any_specifiers_p
14271 && decl_specifiers->type == error_mark_node)
14272 {
14273 cp_parser_error (parser, "invalid type in declaration");
14274 decl_specifiers->type = integer_type_node;
14275 }
14276
14277 /* Check to see whether or not this declaration is a friend. */
14278 friend_p = cp_parser_friend_p (decl_specifiers);
14279
14280 /* Enter the newly declared entry in the symbol table. If we're
14281 processing a declaration in a class-specifier, we wait until
14282 after processing the initializer. */
14283 if (!member_p)
14284 {
14285 if (parser->in_unbraced_linkage_specification_p)
14286 decl_specifiers->storage_class = sc_extern;
14287 decl = start_decl (declarator, decl_specifiers,
14288 is_initialized, attributes, prefix_attributes,
14289 &pushed_scope);
14290 /* Adjust location of decl if declarator->id_loc is more appropriate:
14291 set, and decl wasn't merged with another decl, in which case its
14292 location would be different from input_location, and more accurate. */
14293 if (DECL_P (decl)
14294 && declarator->id_loc != UNKNOWN_LOCATION
14295 && DECL_SOURCE_LOCATION (decl) == input_location)
14296 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14297 }
14298 else if (scope)
14299 /* Enter the SCOPE. That way unqualified names appearing in the
14300 initializer will be looked up in SCOPE. */
14301 pushed_scope = push_scope (scope);
14302
14303 /* Perform deferred access control checks, now that we know in which
14304 SCOPE the declared entity resides. */
14305 if (!member_p && decl)
14306 {
14307 tree saved_current_function_decl = NULL_TREE;
14308
14309 /* If the entity being declared is a function, pretend that we
14310 are in its scope. If it is a `friend', it may have access to
14311 things that would not otherwise be accessible. */
14312 if (TREE_CODE (decl) == FUNCTION_DECL)
14313 {
14314 saved_current_function_decl = current_function_decl;
14315 current_function_decl = decl;
14316 }
14317
14318 /* Perform access checks for template parameters. */
14319 cp_parser_perform_template_parameter_access_checks (checks);
14320
14321 /* Perform the access control checks for the declarator and the
14322 decl-specifiers. */
14323 perform_deferred_access_checks ();
14324
14325 /* Restore the saved value. */
14326 if (TREE_CODE (decl) == FUNCTION_DECL)
14327 current_function_decl = saved_current_function_decl;
14328 }
14329
14330 /* Parse the initializer. */
14331 initializer = NULL_TREE;
14332 is_direct_init = false;
14333 is_non_constant_init = true;
14334 if (is_initialized)
14335 {
14336 if (function_declarator_p (declarator))
14337 {
14338 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14339 if (initialization_kind == CPP_EQ)
14340 initializer = cp_parser_pure_specifier (parser);
14341 else
14342 {
14343 /* If the declaration was erroneous, we don't really
14344 know what the user intended, so just silently
14345 consume the initializer. */
14346 if (decl != error_mark_node)
14347 error_at (initializer_start_token->location,
14348 "initializer provided for function");
14349 cp_parser_skip_to_closing_parenthesis (parser,
14350 /*recovering=*/true,
14351 /*or_comma=*/false,
14352 /*consume_paren=*/true);
14353 }
14354 }
14355 else
14356 {
14357 /* We want to record the extra mangling scope for in-class
14358 initializers of class members and initializers of static data
14359 member templates. The former is a C++0x feature which isn't
14360 implemented yet, and I expect it will involve deferring
14361 parsing of the initializer until end of class as with default
14362 arguments. So right here we only handle the latter. */
14363 if (!member_p && processing_template_decl)
14364 start_lambda_scope (decl);
14365 initializer = cp_parser_initializer (parser,
14366 &is_direct_init,
14367 &is_non_constant_init);
14368 if (!member_p && processing_template_decl)
14369 finish_lambda_scope ();
14370 }
14371 }
14372
14373 /* The old parser allows attributes to appear after a parenthesized
14374 initializer. Mark Mitchell proposed removing this functionality
14375 on the GCC mailing lists on 2002-08-13. This parser accepts the
14376 attributes -- but ignores them. */
14377 if (cp_parser_allow_gnu_extensions_p (parser)
14378 && initialization_kind == CPP_OPEN_PAREN)
14379 if (cp_parser_attributes_opt (parser))
14380 warning (OPT_Wattributes,
14381 "attributes after parenthesized initializer ignored");
14382
14383 /* For an in-class declaration, use `grokfield' to create the
14384 declaration. */
14385 if (member_p)
14386 {
14387 if (pushed_scope)
14388 {
14389 pop_scope (pushed_scope);
14390 pushed_scope = false;
14391 }
14392 decl = grokfield (declarator, decl_specifiers,
14393 initializer, !is_non_constant_init,
14394 /*asmspec=*/NULL_TREE,
14395 prefix_attributes);
14396 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14397 cp_parser_save_default_args (parser, decl);
14398 }
14399
14400 /* Finish processing the declaration. But, skip friend
14401 declarations. */
14402 if (!friend_p && decl && decl != error_mark_node)
14403 {
14404 cp_finish_decl (decl,
14405 initializer, !is_non_constant_init,
14406 asm_specification,
14407 /* If the initializer is in parentheses, then this is
14408 a direct-initialization, which means that an
14409 `explicit' constructor is OK. Otherwise, an
14410 `explicit' constructor cannot be used. */
14411 ((is_direct_init || !is_initialized)
14412 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14413 }
14414 else if ((cxx_dialect != cxx98) && friend_p
14415 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14416 /* Core issue #226 (C++0x only): A default template-argument
14417 shall not be specified in a friend class template
14418 declaration. */
14419 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14420 /*is_partial=*/0, /*is_friend_decl=*/1);
14421
14422 if (!friend_p && pushed_scope)
14423 pop_scope (pushed_scope);
14424
14425 return decl;
14426 }
14427
14428 /* Parse a declarator.
14429
14430 declarator:
14431 direct-declarator
14432 ptr-operator declarator
14433
14434 abstract-declarator:
14435 ptr-operator abstract-declarator [opt]
14436 direct-abstract-declarator
14437
14438 GNU Extensions:
14439
14440 declarator:
14441 attributes [opt] direct-declarator
14442 attributes [opt] ptr-operator declarator
14443
14444 abstract-declarator:
14445 attributes [opt] ptr-operator abstract-declarator [opt]
14446 attributes [opt] direct-abstract-declarator
14447
14448 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14449 detect constructor, destructor or conversion operators. It is set
14450 to -1 if the declarator is a name, and +1 if it is a
14451 function. Otherwise it is set to zero. Usually you just want to
14452 test for >0, but internally the negative value is used.
14453
14454 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14455 a decl-specifier-seq unless it declares a constructor, destructor,
14456 or conversion. It might seem that we could check this condition in
14457 semantic analysis, rather than parsing, but that makes it difficult
14458 to handle something like `f()'. We want to notice that there are
14459 no decl-specifiers, and therefore realize that this is an
14460 expression, not a declaration.)
14461
14462 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14463 the declarator is a direct-declarator of the form "(...)".
14464
14465 MEMBER_P is true iff this declarator is a member-declarator. */
14466
14467 static cp_declarator *
14468 cp_parser_declarator (cp_parser* parser,
14469 cp_parser_declarator_kind dcl_kind,
14470 int* ctor_dtor_or_conv_p,
14471 bool* parenthesized_p,
14472 bool member_p)
14473 {
14474 cp_declarator *declarator;
14475 enum tree_code code;
14476 cp_cv_quals cv_quals;
14477 tree class_type;
14478 tree attributes = NULL_TREE;
14479
14480 /* Assume this is not a constructor, destructor, or type-conversion
14481 operator. */
14482 if (ctor_dtor_or_conv_p)
14483 *ctor_dtor_or_conv_p = 0;
14484
14485 if (cp_parser_allow_gnu_extensions_p (parser))
14486 attributes = cp_parser_attributes_opt (parser);
14487
14488 /* Check for the ptr-operator production. */
14489 cp_parser_parse_tentatively (parser);
14490 /* Parse the ptr-operator. */
14491 code = cp_parser_ptr_operator (parser,
14492 &class_type,
14493 &cv_quals);
14494 /* If that worked, then we have a ptr-operator. */
14495 if (cp_parser_parse_definitely (parser))
14496 {
14497 /* If a ptr-operator was found, then this declarator was not
14498 parenthesized. */
14499 if (parenthesized_p)
14500 *parenthesized_p = true;
14501 /* The dependent declarator is optional if we are parsing an
14502 abstract-declarator. */
14503 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14504 cp_parser_parse_tentatively (parser);
14505
14506 /* Parse the dependent declarator. */
14507 declarator = cp_parser_declarator (parser, dcl_kind,
14508 /*ctor_dtor_or_conv_p=*/NULL,
14509 /*parenthesized_p=*/NULL,
14510 /*member_p=*/false);
14511
14512 /* If we are parsing an abstract-declarator, we must handle the
14513 case where the dependent declarator is absent. */
14514 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14515 && !cp_parser_parse_definitely (parser))
14516 declarator = NULL;
14517
14518 declarator = cp_parser_make_indirect_declarator
14519 (code, class_type, cv_quals, declarator);
14520 }
14521 /* Everything else is a direct-declarator. */
14522 else
14523 {
14524 if (parenthesized_p)
14525 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14526 CPP_OPEN_PAREN);
14527 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14528 ctor_dtor_or_conv_p,
14529 member_p);
14530 }
14531
14532 if (attributes && declarator && declarator != cp_error_declarator)
14533 declarator->attributes = attributes;
14534
14535 return declarator;
14536 }
14537
14538 /* Parse a direct-declarator or direct-abstract-declarator.
14539
14540 direct-declarator:
14541 declarator-id
14542 direct-declarator ( parameter-declaration-clause )
14543 cv-qualifier-seq [opt]
14544 exception-specification [opt]
14545 direct-declarator [ constant-expression [opt] ]
14546 ( declarator )
14547
14548 direct-abstract-declarator:
14549 direct-abstract-declarator [opt]
14550 ( parameter-declaration-clause )
14551 cv-qualifier-seq [opt]
14552 exception-specification [opt]
14553 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14554 ( abstract-declarator )
14555
14556 Returns a representation of the declarator. DCL_KIND is
14557 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14558 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14559 we are parsing a direct-declarator. It is
14560 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14561 of ambiguity we prefer an abstract declarator, as per
14562 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14563 cp_parser_declarator. */
14564
14565 static cp_declarator *
14566 cp_parser_direct_declarator (cp_parser* parser,
14567 cp_parser_declarator_kind dcl_kind,
14568 int* ctor_dtor_or_conv_p,
14569 bool member_p)
14570 {
14571 cp_token *token;
14572 cp_declarator *declarator = NULL;
14573 tree scope = NULL_TREE;
14574 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14575 bool saved_in_declarator_p = parser->in_declarator_p;
14576 bool first = true;
14577 tree pushed_scope = NULL_TREE;
14578
14579 while (true)
14580 {
14581 /* Peek at the next token. */
14582 token = cp_lexer_peek_token (parser->lexer);
14583 if (token->type == CPP_OPEN_PAREN)
14584 {
14585 /* This is either a parameter-declaration-clause, or a
14586 parenthesized declarator. When we know we are parsing a
14587 named declarator, it must be a parenthesized declarator
14588 if FIRST is true. For instance, `(int)' is a
14589 parameter-declaration-clause, with an omitted
14590 direct-abstract-declarator. But `((*))', is a
14591 parenthesized abstract declarator. Finally, when T is a
14592 template parameter `(T)' is a
14593 parameter-declaration-clause, and not a parenthesized
14594 named declarator.
14595
14596 We first try and parse a parameter-declaration-clause,
14597 and then try a nested declarator (if FIRST is true).
14598
14599 It is not an error for it not to be a
14600 parameter-declaration-clause, even when FIRST is
14601 false. Consider,
14602
14603 int i (int);
14604 int i (3);
14605
14606 The first is the declaration of a function while the
14607 second is the definition of a variable, including its
14608 initializer.
14609
14610 Having seen only the parenthesis, we cannot know which of
14611 these two alternatives should be selected. Even more
14612 complex are examples like:
14613
14614 int i (int (a));
14615 int i (int (3));
14616
14617 The former is a function-declaration; the latter is a
14618 variable initialization.
14619
14620 Thus again, we try a parameter-declaration-clause, and if
14621 that fails, we back out and return. */
14622
14623 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14624 {
14625 tree params;
14626 unsigned saved_num_template_parameter_lists;
14627 bool is_declarator = false;
14628 tree t;
14629
14630 /* In a member-declarator, the only valid interpretation
14631 of a parenthesis is the start of a
14632 parameter-declaration-clause. (It is invalid to
14633 initialize a static data member with a parenthesized
14634 initializer; only the "=" form of initialization is
14635 permitted.) */
14636 if (!member_p)
14637 cp_parser_parse_tentatively (parser);
14638
14639 /* Consume the `('. */
14640 cp_lexer_consume_token (parser->lexer);
14641 if (first)
14642 {
14643 /* If this is going to be an abstract declarator, we're
14644 in a declarator and we can't have default args. */
14645 parser->default_arg_ok_p = false;
14646 parser->in_declarator_p = true;
14647 }
14648
14649 /* Inside the function parameter list, surrounding
14650 template-parameter-lists do not apply. */
14651 saved_num_template_parameter_lists
14652 = parser->num_template_parameter_lists;
14653 parser->num_template_parameter_lists = 0;
14654
14655 begin_scope (sk_function_parms, NULL_TREE);
14656
14657 /* Parse the parameter-declaration-clause. */
14658 params = cp_parser_parameter_declaration_clause (parser);
14659
14660 parser->num_template_parameter_lists
14661 = saved_num_template_parameter_lists;
14662
14663 /* If all went well, parse the cv-qualifier-seq and the
14664 exception-specification. */
14665 if (member_p || cp_parser_parse_definitely (parser))
14666 {
14667 cp_cv_quals cv_quals;
14668 tree exception_specification;
14669 tree late_return;
14670
14671 is_declarator = true;
14672
14673 if (ctor_dtor_or_conv_p)
14674 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14675 first = false;
14676 /* Consume the `)'. */
14677 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14678
14679 /* Parse the cv-qualifier-seq. */
14680 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14681 /* And the exception-specification. */
14682 exception_specification
14683 = cp_parser_exception_specification_opt (parser);
14684
14685 late_return
14686 = cp_parser_late_return_type_opt (parser);
14687
14688 /* Create the function-declarator. */
14689 declarator = make_call_declarator (declarator,
14690 params,
14691 cv_quals,
14692 exception_specification,
14693 late_return);
14694 /* Any subsequent parameter lists are to do with
14695 return type, so are not those of the declared
14696 function. */
14697 parser->default_arg_ok_p = false;
14698 }
14699
14700 /* Remove the function parms from scope. */
14701 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14702 pop_binding (DECL_NAME (t), t);
14703 leave_scope();
14704
14705 if (is_declarator)
14706 /* Repeat the main loop. */
14707 continue;
14708 }
14709
14710 /* If this is the first, we can try a parenthesized
14711 declarator. */
14712 if (first)
14713 {
14714 bool saved_in_type_id_in_expr_p;
14715
14716 parser->default_arg_ok_p = saved_default_arg_ok_p;
14717 parser->in_declarator_p = saved_in_declarator_p;
14718
14719 /* Consume the `('. */
14720 cp_lexer_consume_token (parser->lexer);
14721 /* Parse the nested declarator. */
14722 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14723 parser->in_type_id_in_expr_p = true;
14724 declarator
14725 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14726 /*parenthesized_p=*/NULL,
14727 member_p);
14728 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14729 first = false;
14730 /* Expect a `)'. */
14731 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14732 declarator = cp_error_declarator;
14733 if (declarator == cp_error_declarator)
14734 break;
14735
14736 goto handle_declarator;
14737 }
14738 /* Otherwise, we must be done. */
14739 else
14740 break;
14741 }
14742 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14743 && token->type == CPP_OPEN_SQUARE)
14744 {
14745 /* Parse an array-declarator. */
14746 tree bounds;
14747
14748 if (ctor_dtor_or_conv_p)
14749 *ctor_dtor_or_conv_p = 0;
14750
14751 first = false;
14752 parser->default_arg_ok_p = false;
14753 parser->in_declarator_p = true;
14754 /* Consume the `['. */
14755 cp_lexer_consume_token (parser->lexer);
14756 /* Peek at the next token. */
14757 token = cp_lexer_peek_token (parser->lexer);
14758 /* If the next token is `]', then there is no
14759 constant-expression. */
14760 if (token->type != CPP_CLOSE_SQUARE)
14761 {
14762 bool non_constant_p;
14763
14764 bounds
14765 = cp_parser_constant_expression (parser,
14766 /*allow_non_constant=*/true,
14767 &non_constant_p);
14768 if (!non_constant_p)
14769 bounds = fold_non_dependent_expr (bounds);
14770 /* Normally, the array bound must be an integral constant
14771 expression. However, as an extension, we allow VLAs
14772 in function scopes as long as they aren't part of a
14773 parameter declaration. */
14774 else if (!parser->in_function_body
14775 || current_binding_level->kind == sk_function_parms)
14776 {
14777 cp_parser_error (parser,
14778 "array bound is not an integer constant");
14779 bounds = error_mark_node;
14780 }
14781 else if (processing_template_decl && !error_operand_p (bounds))
14782 {
14783 /* Remember this wasn't a constant-expression. */
14784 bounds = build_nop (TREE_TYPE (bounds), bounds);
14785 TREE_SIDE_EFFECTS (bounds) = 1;
14786 }
14787 }
14788 else
14789 bounds = NULL_TREE;
14790 /* Look for the closing `]'. */
14791 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14792 {
14793 declarator = cp_error_declarator;
14794 break;
14795 }
14796
14797 declarator = make_array_declarator (declarator, bounds);
14798 }
14799 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14800 {
14801 {
14802 tree qualifying_scope;
14803 tree unqualified_name;
14804 special_function_kind sfk;
14805 bool abstract_ok;
14806 bool pack_expansion_p = false;
14807 cp_token *declarator_id_start_token;
14808
14809 /* Parse a declarator-id */
14810 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14811 if (abstract_ok)
14812 {
14813 cp_parser_parse_tentatively (parser);
14814
14815 /* If we see an ellipsis, we should be looking at a
14816 parameter pack. */
14817 if (token->type == CPP_ELLIPSIS)
14818 {
14819 /* Consume the `...' */
14820 cp_lexer_consume_token (parser->lexer);
14821
14822 pack_expansion_p = true;
14823 }
14824 }
14825
14826 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14827 unqualified_name
14828 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14829 qualifying_scope = parser->scope;
14830 if (abstract_ok)
14831 {
14832 bool okay = false;
14833
14834 if (!unqualified_name && pack_expansion_p)
14835 {
14836 /* Check whether an error occurred. */
14837 okay = !cp_parser_error_occurred (parser);
14838
14839 /* We already consumed the ellipsis to mark a
14840 parameter pack, but we have no way to report it,
14841 so abort the tentative parse. We will be exiting
14842 immediately anyway. */
14843 cp_parser_abort_tentative_parse (parser);
14844 }
14845 else
14846 okay = cp_parser_parse_definitely (parser);
14847
14848 if (!okay)
14849 unqualified_name = error_mark_node;
14850 else if (unqualified_name
14851 && (qualifying_scope
14852 || (TREE_CODE (unqualified_name)
14853 != IDENTIFIER_NODE)))
14854 {
14855 cp_parser_error (parser, "expected unqualified-id");
14856 unqualified_name = error_mark_node;
14857 }
14858 }
14859
14860 if (!unqualified_name)
14861 return NULL;
14862 if (unqualified_name == error_mark_node)
14863 {
14864 declarator = cp_error_declarator;
14865 pack_expansion_p = false;
14866 declarator->parameter_pack_p = false;
14867 break;
14868 }
14869
14870 if (qualifying_scope && at_namespace_scope_p ()
14871 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14872 {
14873 /* In the declaration of a member of a template class
14874 outside of the class itself, the SCOPE will sometimes
14875 be a TYPENAME_TYPE. For example, given:
14876
14877 template <typename T>
14878 int S<T>::R::i = 3;
14879
14880 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14881 this context, we must resolve S<T>::R to an ordinary
14882 type, rather than a typename type.
14883
14884 The reason we normally avoid resolving TYPENAME_TYPEs
14885 is that a specialization of `S' might render
14886 `S<T>::R' not a type. However, if `S' is
14887 specialized, then this `i' will not be used, so there
14888 is no harm in resolving the types here. */
14889 tree type;
14890
14891 /* Resolve the TYPENAME_TYPE. */
14892 type = resolve_typename_type (qualifying_scope,
14893 /*only_current_p=*/false);
14894 /* If that failed, the declarator is invalid. */
14895 if (TREE_CODE (type) == TYPENAME_TYPE)
14896 {
14897 if (typedef_variant_p (type))
14898 error_at (declarator_id_start_token->location,
14899 "cannot define member of dependent typedef "
14900 "%qT", type);
14901 else
14902 error_at (declarator_id_start_token->location,
14903 "%<%T::%E%> is not a type",
14904 TYPE_CONTEXT (qualifying_scope),
14905 TYPE_IDENTIFIER (qualifying_scope));
14906 }
14907 qualifying_scope = type;
14908 }
14909
14910 sfk = sfk_none;
14911
14912 if (unqualified_name)
14913 {
14914 tree class_type;
14915
14916 if (qualifying_scope
14917 && CLASS_TYPE_P (qualifying_scope))
14918 class_type = qualifying_scope;
14919 else
14920 class_type = current_class_type;
14921
14922 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14923 {
14924 tree name_type = TREE_TYPE (unqualified_name);
14925 if (class_type && same_type_p (name_type, class_type))
14926 {
14927 if (qualifying_scope
14928 && CLASSTYPE_USE_TEMPLATE (name_type))
14929 {
14930 error_at (declarator_id_start_token->location,
14931 "invalid use of constructor as a template");
14932 inform (declarator_id_start_token->location,
14933 "use %<%T::%D%> instead of %<%T::%D%> to "
14934 "name the constructor in a qualified name",
14935 class_type,
14936 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14937 class_type, name_type);
14938 declarator = cp_error_declarator;
14939 break;
14940 }
14941 else
14942 unqualified_name = constructor_name (class_type);
14943 }
14944 else
14945 {
14946 /* We do not attempt to print the declarator
14947 here because we do not have enough
14948 information about its original syntactic
14949 form. */
14950 cp_parser_error (parser, "invalid declarator");
14951 declarator = cp_error_declarator;
14952 break;
14953 }
14954 }
14955
14956 if (class_type)
14957 {
14958 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14959 sfk = sfk_destructor;
14960 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14961 sfk = sfk_conversion;
14962 else if (/* There's no way to declare a constructor
14963 for an anonymous type, even if the type
14964 got a name for linkage purposes. */
14965 !TYPE_WAS_ANONYMOUS (class_type)
14966 && constructor_name_p (unqualified_name,
14967 class_type))
14968 {
14969 unqualified_name = constructor_name (class_type);
14970 sfk = sfk_constructor;
14971 }
14972 else if (is_overloaded_fn (unqualified_name)
14973 && DECL_CONSTRUCTOR_P (get_first_fn
14974 (unqualified_name)))
14975 sfk = sfk_constructor;
14976
14977 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14978 *ctor_dtor_or_conv_p = -1;
14979 }
14980 }
14981 declarator = make_id_declarator (qualifying_scope,
14982 unqualified_name,
14983 sfk);
14984 declarator->id_loc = token->location;
14985 declarator->parameter_pack_p = pack_expansion_p;
14986
14987 if (pack_expansion_p)
14988 maybe_warn_variadic_templates ();
14989 }
14990
14991 handle_declarator:;
14992 scope = get_scope_of_declarator (declarator);
14993 if (scope)
14994 /* Any names that appear after the declarator-id for a
14995 member are looked up in the containing scope. */
14996 pushed_scope = push_scope (scope);
14997 parser->in_declarator_p = true;
14998 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14999 || (declarator && declarator->kind == cdk_id))
15000 /* Default args are only allowed on function
15001 declarations. */
15002 parser->default_arg_ok_p = saved_default_arg_ok_p;
15003 else
15004 parser->default_arg_ok_p = false;
15005
15006 first = false;
15007 }
15008 /* We're done. */
15009 else
15010 break;
15011 }
15012
15013 /* For an abstract declarator, we might wind up with nothing at this
15014 point. That's an error; the declarator is not optional. */
15015 if (!declarator)
15016 cp_parser_error (parser, "expected declarator");
15017
15018 /* If we entered a scope, we must exit it now. */
15019 if (pushed_scope)
15020 pop_scope (pushed_scope);
15021
15022 parser->default_arg_ok_p = saved_default_arg_ok_p;
15023 parser->in_declarator_p = saved_in_declarator_p;
15024
15025 return declarator;
15026 }
15027
15028 /* Parse a ptr-operator.
15029
15030 ptr-operator:
15031 * cv-qualifier-seq [opt]
15032 &
15033 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15034
15035 GNU Extension:
15036
15037 ptr-operator:
15038 & cv-qualifier-seq [opt]
15039
15040 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15041 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15042 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15043 filled in with the TYPE containing the member. *CV_QUALS is
15044 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15045 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15046 Note that the tree codes returned by this function have nothing
15047 to do with the types of trees that will be eventually be created
15048 to represent the pointer or reference type being parsed. They are
15049 just constants with suggestive names. */
15050 static enum tree_code
15051 cp_parser_ptr_operator (cp_parser* parser,
15052 tree* type,
15053 cp_cv_quals *cv_quals)
15054 {
15055 enum tree_code code = ERROR_MARK;
15056 cp_token *token;
15057
15058 /* Assume that it's not a pointer-to-member. */
15059 *type = NULL_TREE;
15060 /* And that there are no cv-qualifiers. */
15061 *cv_quals = TYPE_UNQUALIFIED;
15062
15063 /* Peek at the next token. */
15064 token = cp_lexer_peek_token (parser->lexer);
15065
15066 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15067 if (token->type == CPP_MULT)
15068 code = INDIRECT_REF;
15069 else if (token->type == CPP_AND)
15070 code = ADDR_EXPR;
15071 else if ((cxx_dialect != cxx98) &&
15072 token->type == CPP_AND_AND) /* C++0x only */
15073 code = NON_LVALUE_EXPR;
15074
15075 if (code != ERROR_MARK)
15076 {
15077 /* Consume the `*', `&' or `&&'. */
15078 cp_lexer_consume_token (parser->lexer);
15079
15080 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15081 `&', if we are allowing GNU extensions. (The only qualifier
15082 that can legally appear after `&' is `restrict', but that is
15083 enforced during semantic analysis. */
15084 if (code == INDIRECT_REF
15085 || cp_parser_allow_gnu_extensions_p (parser))
15086 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15087 }
15088 else
15089 {
15090 /* Try the pointer-to-member case. */
15091 cp_parser_parse_tentatively (parser);
15092 /* Look for the optional `::' operator. */
15093 cp_parser_global_scope_opt (parser,
15094 /*current_scope_valid_p=*/false);
15095 /* Look for the nested-name specifier. */
15096 token = cp_lexer_peek_token (parser->lexer);
15097 cp_parser_nested_name_specifier (parser,
15098 /*typename_keyword_p=*/false,
15099 /*check_dependency_p=*/true,
15100 /*type_p=*/false,
15101 /*is_declaration=*/false);
15102 /* If we found it, and the next token is a `*', then we are
15103 indeed looking at a pointer-to-member operator. */
15104 if (!cp_parser_error_occurred (parser)
15105 && cp_parser_require (parser, CPP_MULT, RT_MULT))
15106 {
15107 /* Indicate that the `*' operator was used. */
15108 code = INDIRECT_REF;
15109
15110 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15111 error_at (token->location, "%qD is a namespace", parser->scope);
15112 else
15113 {
15114 /* The type of which the member is a member is given by the
15115 current SCOPE. */
15116 *type = parser->scope;
15117 /* The next name will not be qualified. */
15118 parser->scope = NULL_TREE;
15119 parser->qualifying_scope = NULL_TREE;
15120 parser->object_scope = NULL_TREE;
15121 /* Look for the optional cv-qualifier-seq. */
15122 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15123 }
15124 }
15125 /* If that didn't work we don't have a ptr-operator. */
15126 if (!cp_parser_parse_definitely (parser))
15127 cp_parser_error (parser, "expected ptr-operator");
15128 }
15129
15130 return code;
15131 }
15132
15133 /* Parse an (optional) cv-qualifier-seq.
15134
15135 cv-qualifier-seq:
15136 cv-qualifier cv-qualifier-seq [opt]
15137
15138 cv-qualifier:
15139 const
15140 volatile
15141
15142 GNU Extension:
15143
15144 cv-qualifier:
15145 __restrict__
15146
15147 Returns a bitmask representing the cv-qualifiers. */
15148
15149 static cp_cv_quals
15150 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15151 {
15152 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15153
15154 while (true)
15155 {
15156 cp_token *token;
15157 cp_cv_quals cv_qualifier;
15158
15159 /* Peek at the next token. */
15160 token = cp_lexer_peek_token (parser->lexer);
15161 /* See if it's a cv-qualifier. */
15162 switch (token->keyword)
15163 {
15164 case RID_CONST:
15165 cv_qualifier = TYPE_QUAL_CONST;
15166 break;
15167
15168 case RID_VOLATILE:
15169 cv_qualifier = TYPE_QUAL_VOLATILE;
15170 break;
15171
15172 case RID_RESTRICT:
15173 cv_qualifier = TYPE_QUAL_RESTRICT;
15174 break;
15175
15176 default:
15177 cv_qualifier = TYPE_UNQUALIFIED;
15178 break;
15179 }
15180
15181 if (!cv_qualifier)
15182 break;
15183
15184 if (cv_quals & cv_qualifier)
15185 {
15186 error_at (token->location, "duplicate cv-qualifier");
15187 cp_lexer_purge_token (parser->lexer);
15188 }
15189 else
15190 {
15191 cp_lexer_consume_token (parser->lexer);
15192 cv_quals |= cv_qualifier;
15193 }
15194 }
15195
15196 return cv_quals;
15197 }
15198
15199 /* Parse a late-specified return type, if any. This is not a separate
15200 non-terminal, but part of a function declarator, which looks like
15201
15202 -> trailing-type-specifier-seq abstract-declarator(opt)
15203
15204 Returns the type indicated by the type-id. */
15205
15206 static tree
15207 cp_parser_late_return_type_opt (cp_parser* parser)
15208 {
15209 cp_token *token;
15210
15211 /* Peek at the next token. */
15212 token = cp_lexer_peek_token (parser->lexer);
15213 /* A late-specified return type is indicated by an initial '->'. */
15214 if (token->type != CPP_DEREF)
15215 return NULL_TREE;
15216
15217 /* Consume the ->. */
15218 cp_lexer_consume_token (parser->lexer);
15219
15220 return cp_parser_trailing_type_id (parser);
15221 }
15222
15223 /* Parse a declarator-id.
15224
15225 declarator-id:
15226 id-expression
15227 :: [opt] nested-name-specifier [opt] type-name
15228
15229 In the `id-expression' case, the value returned is as for
15230 cp_parser_id_expression if the id-expression was an unqualified-id.
15231 If the id-expression was a qualified-id, then a SCOPE_REF is
15232 returned. The first operand is the scope (either a NAMESPACE_DECL
15233 or TREE_TYPE), but the second is still just a representation of an
15234 unqualified-id. */
15235
15236 static tree
15237 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15238 {
15239 tree id;
15240 /* The expression must be an id-expression. Assume that qualified
15241 names are the names of types so that:
15242
15243 template <class T>
15244 int S<T>::R::i = 3;
15245
15246 will work; we must treat `S<T>::R' as the name of a type.
15247 Similarly, assume that qualified names are templates, where
15248 required, so that:
15249
15250 template <class T>
15251 int S<T>::R<T>::i = 3;
15252
15253 will work, too. */
15254 id = cp_parser_id_expression (parser,
15255 /*template_keyword_p=*/false,
15256 /*check_dependency_p=*/false,
15257 /*template_p=*/NULL,
15258 /*declarator_p=*/true,
15259 optional_p);
15260 if (id && BASELINK_P (id))
15261 id = BASELINK_FUNCTIONS (id);
15262 return id;
15263 }
15264
15265 /* Parse a type-id.
15266
15267 type-id:
15268 type-specifier-seq abstract-declarator [opt]
15269
15270 Returns the TYPE specified. */
15271
15272 static tree
15273 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15274 bool is_trailing_return)
15275 {
15276 cp_decl_specifier_seq type_specifier_seq;
15277 cp_declarator *abstract_declarator;
15278
15279 /* Parse the type-specifier-seq. */
15280 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15281 is_trailing_return,
15282 &type_specifier_seq);
15283 if (type_specifier_seq.type == error_mark_node)
15284 return error_mark_node;
15285
15286 /* There might or might not be an abstract declarator. */
15287 cp_parser_parse_tentatively (parser);
15288 /* Look for the declarator. */
15289 abstract_declarator
15290 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15291 /*parenthesized_p=*/NULL,
15292 /*member_p=*/false);
15293 /* Check to see if there really was a declarator. */
15294 if (!cp_parser_parse_definitely (parser))
15295 abstract_declarator = NULL;
15296
15297 if (type_specifier_seq.type
15298 && type_uses_auto (type_specifier_seq.type))
15299 {
15300 /* A type-id with type 'auto' is only ok if the abstract declarator
15301 is a function declarator with a late-specified return type. */
15302 if (abstract_declarator
15303 && abstract_declarator->kind == cdk_function
15304 && abstract_declarator->u.function.late_return_type)
15305 /* OK */;
15306 else
15307 {
15308 error ("invalid use of %<auto%>");
15309 return error_mark_node;
15310 }
15311 }
15312
15313 return groktypename (&type_specifier_seq, abstract_declarator,
15314 is_template_arg);
15315 }
15316
15317 static tree cp_parser_type_id (cp_parser *parser)
15318 {
15319 return cp_parser_type_id_1 (parser, false, false);
15320 }
15321
15322 static tree cp_parser_template_type_arg (cp_parser *parser)
15323 {
15324 return cp_parser_type_id_1 (parser, true, false);
15325 }
15326
15327 static tree cp_parser_trailing_type_id (cp_parser *parser)
15328 {
15329 return cp_parser_type_id_1 (parser, false, true);
15330 }
15331
15332 /* Parse a type-specifier-seq.
15333
15334 type-specifier-seq:
15335 type-specifier type-specifier-seq [opt]
15336
15337 GNU extension:
15338
15339 type-specifier-seq:
15340 attributes type-specifier-seq [opt]
15341
15342 If IS_DECLARATION is true, we are at the start of a "condition" or
15343 exception-declaration, so we might be followed by a declarator-id.
15344
15345 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15346 i.e. we've just seen "->".
15347
15348 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15349
15350 static void
15351 cp_parser_type_specifier_seq (cp_parser* parser,
15352 bool is_declaration,
15353 bool is_trailing_return,
15354 cp_decl_specifier_seq *type_specifier_seq)
15355 {
15356 bool seen_type_specifier = false;
15357 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15358 cp_token *start_token = NULL;
15359
15360 /* Clear the TYPE_SPECIFIER_SEQ. */
15361 clear_decl_specs (type_specifier_seq);
15362
15363 /* In the context of a trailing return type, enum E { } is an
15364 elaborated-type-specifier followed by a function-body, not an
15365 enum-specifier. */
15366 if (is_trailing_return)
15367 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15368
15369 /* Parse the type-specifiers and attributes. */
15370 while (true)
15371 {
15372 tree type_specifier;
15373 bool is_cv_qualifier;
15374
15375 /* Check for attributes first. */
15376 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15377 {
15378 type_specifier_seq->attributes =
15379 chainon (type_specifier_seq->attributes,
15380 cp_parser_attributes_opt (parser));
15381 continue;
15382 }
15383
15384 /* record the token of the beginning of the type specifier seq,
15385 for error reporting purposes*/
15386 if (!start_token)
15387 start_token = cp_lexer_peek_token (parser->lexer);
15388
15389 /* Look for the type-specifier. */
15390 type_specifier = cp_parser_type_specifier (parser,
15391 flags,
15392 type_specifier_seq,
15393 /*is_declaration=*/false,
15394 NULL,
15395 &is_cv_qualifier);
15396 if (!type_specifier)
15397 {
15398 /* If the first type-specifier could not be found, this is not a
15399 type-specifier-seq at all. */
15400 if (!seen_type_specifier)
15401 {
15402 cp_parser_error (parser, "expected type-specifier");
15403 type_specifier_seq->type = error_mark_node;
15404 return;
15405 }
15406 /* If subsequent type-specifiers could not be found, the
15407 type-specifier-seq is complete. */
15408 break;
15409 }
15410
15411 seen_type_specifier = true;
15412 /* The standard says that a condition can be:
15413
15414 type-specifier-seq declarator = assignment-expression
15415
15416 However, given:
15417
15418 struct S {};
15419 if (int S = ...)
15420
15421 we should treat the "S" as a declarator, not as a
15422 type-specifier. The standard doesn't say that explicitly for
15423 type-specifier-seq, but it does say that for
15424 decl-specifier-seq in an ordinary declaration. Perhaps it
15425 would be clearer just to allow a decl-specifier-seq here, and
15426 then add a semantic restriction that if any decl-specifiers
15427 that are not type-specifiers appear, the program is invalid. */
15428 if (is_declaration && !is_cv_qualifier)
15429 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15430 }
15431
15432 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15433 }
15434
15435 /* Parse a parameter-declaration-clause.
15436
15437 parameter-declaration-clause:
15438 parameter-declaration-list [opt] ... [opt]
15439 parameter-declaration-list , ...
15440
15441 Returns a representation for the parameter declarations. A return
15442 value of NULL indicates a parameter-declaration-clause consisting
15443 only of an ellipsis. */
15444
15445 static tree
15446 cp_parser_parameter_declaration_clause (cp_parser* parser)
15447 {
15448 tree parameters;
15449 cp_token *token;
15450 bool ellipsis_p;
15451 bool is_error;
15452
15453 /* Peek at the next token. */
15454 token = cp_lexer_peek_token (parser->lexer);
15455 /* Check for trivial parameter-declaration-clauses. */
15456 if (token->type == CPP_ELLIPSIS)
15457 {
15458 /* Consume the `...' token. */
15459 cp_lexer_consume_token (parser->lexer);
15460 return NULL_TREE;
15461 }
15462 else if (token->type == CPP_CLOSE_PAREN)
15463 /* There are no parameters. */
15464 {
15465 #ifndef NO_IMPLICIT_EXTERN_C
15466 if (in_system_header && current_class_type == NULL
15467 && current_lang_name == lang_name_c)
15468 return NULL_TREE;
15469 else
15470 #endif
15471 return void_list_node;
15472 }
15473 /* Check for `(void)', too, which is a special case. */
15474 else if (token->keyword == RID_VOID
15475 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15476 == CPP_CLOSE_PAREN))
15477 {
15478 /* Consume the `void' token. */
15479 cp_lexer_consume_token (parser->lexer);
15480 /* There are no parameters. */
15481 return void_list_node;
15482 }
15483
15484 /* Parse the parameter-declaration-list. */
15485 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15486 /* If a parse error occurred while parsing the
15487 parameter-declaration-list, then the entire
15488 parameter-declaration-clause is erroneous. */
15489 if (is_error)
15490 return NULL;
15491
15492 /* Peek at the next token. */
15493 token = cp_lexer_peek_token (parser->lexer);
15494 /* If it's a `,', the clause should terminate with an ellipsis. */
15495 if (token->type == CPP_COMMA)
15496 {
15497 /* Consume the `,'. */
15498 cp_lexer_consume_token (parser->lexer);
15499 /* Expect an ellipsis. */
15500 ellipsis_p
15501 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15502 }
15503 /* It might also be `...' if the optional trailing `,' was
15504 omitted. */
15505 else if (token->type == CPP_ELLIPSIS)
15506 {
15507 /* Consume the `...' token. */
15508 cp_lexer_consume_token (parser->lexer);
15509 /* And remember that we saw it. */
15510 ellipsis_p = true;
15511 }
15512 else
15513 ellipsis_p = false;
15514
15515 /* Finish the parameter list. */
15516 if (!ellipsis_p)
15517 parameters = chainon (parameters, void_list_node);
15518
15519 return parameters;
15520 }
15521
15522 /* Parse a parameter-declaration-list.
15523
15524 parameter-declaration-list:
15525 parameter-declaration
15526 parameter-declaration-list , parameter-declaration
15527
15528 Returns a representation of the parameter-declaration-list, as for
15529 cp_parser_parameter_declaration_clause. However, the
15530 `void_list_node' is never appended to the list. Upon return,
15531 *IS_ERROR will be true iff an error occurred. */
15532
15533 static tree
15534 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15535 {
15536 tree parameters = NULL_TREE;
15537 tree *tail = &parameters;
15538 bool saved_in_unbraced_linkage_specification_p;
15539 int index = 0;
15540
15541 /* Assume all will go well. */
15542 *is_error = false;
15543 /* The special considerations that apply to a function within an
15544 unbraced linkage specifications do not apply to the parameters
15545 to the function. */
15546 saved_in_unbraced_linkage_specification_p
15547 = parser->in_unbraced_linkage_specification_p;
15548 parser->in_unbraced_linkage_specification_p = false;
15549
15550 /* Look for more parameters. */
15551 while (true)
15552 {
15553 cp_parameter_declarator *parameter;
15554 tree decl = error_mark_node;
15555 bool parenthesized_p;
15556 /* Parse the parameter. */
15557 parameter
15558 = cp_parser_parameter_declaration (parser,
15559 /*template_parm_p=*/false,
15560 &parenthesized_p);
15561
15562 /* We don't know yet if the enclosing context is deprecated, so wait
15563 and warn in grokparms if appropriate. */
15564 deprecated_state = DEPRECATED_SUPPRESS;
15565
15566 if (parameter)
15567 decl = grokdeclarator (parameter->declarator,
15568 &parameter->decl_specifiers,
15569 PARM,
15570 parameter->default_argument != NULL_TREE,
15571 &parameter->decl_specifiers.attributes);
15572
15573 deprecated_state = DEPRECATED_NORMAL;
15574
15575 /* If a parse error occurred parsing the parameter declaration,
15576 then the entire parameter-declaration-list is erroneous. */
15577 if (decl == error_mark_node)
15578 {
15579 *is_error = true;
15580 parameters = error_mark_node;
15581 break;
15582 }
15583
15584 if (parameter->decl_specifiers.attributes)
15585 cplus_decl_attributes (&decl,
15586 parameter->decl_specifiers.attributes,
15587 0);
15588 if (DECL_NAME (decl))
15589 decl = pushdecl (decl);
15590
15591 if (decl != error_mark_node)
15592 {
15593 retrofit_lang_decl (decl);
15594 DECL_PARM_INDEX (decl) = ++index;
15595 }
15596
15597 /* Add the new parameter to the list. */
15598 *tail = build_tree_list (parameter->default_argument, decl);
15599 tail = &TREE_CHAIN (*tail);
15600
15601 /* Peek at the next token. */
15602 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15603 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15604 /* These are for Objective-C++ */
15605 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15606 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15607 /* The parameter-declaration-list is complete. */
15608 break;
15609 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15610 {
15611 cp_token *token;
15612
15613 /* Peek at the next token. */
15614 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15615 /* If it's an ellipsis, then the list is complete. */
15616 if (token->type == CPP_ELLIPSIS)
15617 break;
15618 /* Otherwise, there must be more parameters. Consume the
15619 `,'. */
15620 cp_lexer_consume_token (parser->lexer);
15621 /* When parsing something like:
15622
15623 int i(float f, double d)
15624
15625 we can tell after seeing the declaration for "f" that we
15626 are not looking at an initialization of a variable "i",
15627 but rather at the declaration of a function "i".
15628
15629 Due to the fact that the parsing of template arguments
15630 (as specified to a template-id) requires backtracking we
15631 cannot use this technique when inside a template argument
15632 list. */
15633 if (!parser->in_template_argument_list_p
15634 && !parser->in_type_id_in_expr_p
15635 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15636 /* However, a parameter-declaration of the form
15637 "foat(f)" (which is a valid declaration of a
15638 parameter "f") can also be interpreted as an
15639 expression (the conversion of "f" to "float"). */
15640 && !parenthesized_p)
15641 cp_parser_commit_to_tentative_parse (parser);
15642 }
15643 else
15644 {
15645 cp_parser_error (parser, "expected %<,%> or %<...%>");
15646 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15647 cp_parser_skip_to_closing_parenthesis (parser,
15648 /*recovering=*/true,
15649 /*or_comma=*/false,
15650 /*consume_paren=*/false);
15651 break;
15652 }
15653 }
15654
15655 parser->in_unbraced_linkage_specification_p
15656 = saved_in_unbraced_linkage_specification_p;
15657
15658 return parameters;
15659 }
15660
15661 /* Parse a parameter declaration.
15662
15663 parameter-declaration:
15664 decl-specifier-seq ... [opt] declarator
15665 decl-specifier-seq declarator = assignment-expression
15666 decl-specifier-seq ... [opt] abstract-declarator [opt]
15667 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15668
15669 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15670 declares a template parameter. (In that case, a non-nested `>'
15671 token encountered during the parsing of the assignment-expression
15672 is not interpreted as a greater-than operator.)
15673
15674 Returns a representation of the parameter, or NULL if an error
15675 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15676 true iff the declarator is of the form "(p)". */
15677
15678 static cp_parameter_declarator *
15679 cp_parser_parameter_declaration (cp_parser *parser,
15680 bool template_parm_p,
15681 bool *parenthesized_p)
15682 {
15683 int declares_class_or_enum;
15684 cp_decl_specifier_seq decl_specifiers;
15685 cp_declarator *declarator;
15686 tree default_argument;
15687 cp_token *token = NULL, *declarator_token_start = NULL;
15688 const char *saved_message;
15689
15690 /* In a template parameter, `>' is not an operator.
15691
15692 [temp.param]
15693
15694 When parsing a default template-argument for a non-type
15695 template-parameter, the first non-nested `>' is taken as the end
15696 of the template parameter-list rather than a greater-than
15697 operator. */
15698
15699 /* Type definitions may not appear in parameter types. */
15700 saved_message = parser->type_definition_forbidden_message;
15701 parser->type_definition_forbidden_message
15702 = G_("types may not be defined in parameter types");
15703
15704 /* Parse the declaration-specifiers. */
15705 cp_parser_decl_specifier_seq (parser,
15706 CP_PARSER_FLAGS_NONE,
15707 &decl_specifiers,
15708 &declares_class_or_enum);
15709
15710 /* Complain about missing 'typename' or other invalid type names. */
15711 if (!decl_specifiers.any_type_specifiers_p)
15712 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15713
15714 /* If an error occurred, there's no reason to attempt to parse the
15715 rest of the declaration. */
15716 if (cp_parser_error_occurred (parser))
15717 {
15718 parser->type_definition_forbidden_message = saved_message;
15719 return NULL;
15720 }
15721
15722 /* Peek at the next token. */
15723 token = cp_lexer_peek_token (parser->lexer);
15724
15725 /* If the next token is a `)', `,', `=', `>', or `...', then there
15726 is no declarator. However, when variadic templates are enabled,
15727 there may be a declarator following `...'. */
15728 if (token->type == CPP_CLOSE_PAREN
15729 || token->type == CPP_COMMA
15730 || token->type == CPP_EQ
15731 || token->type == CPP_GREATER)
15732 {
15733 declarator = NULL;
15734 if (parenthesized_p)
15735 *parenthesized_p = false;
15736 }
15737 /* Otherwise, there should be a declarator. */
15738 else
15739 {
15740 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15741 parser->default_arg_ok_p = false;
15742
15743 /* After seeing a decl-specifier-seq, if the next token is not a
15744 "(", there is no possibility that the code is a valid
15745 expression. Therefore, if parsing tentatively, we commit at
15746 this point. */
15747 if (!parser->in_template_argument_list_p
15748 /* In an expression context, having seen:
15749
15750 (int((char ...
15751
15752 we cannot be sure whether we are looking at a
15753 function-type (taking a "char" as a parameter) or a cast
15754 of some object of type "char" to "int". */
15755 && !parser->in_type_id_in_expr_p
15756 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15757 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15758 cp_parser_commit_to_tentative_parse (parser);
15759 /* Parse the declarator. */
15760 declarator_token_start = token;
15761 declarator = cp_parser_declarator (parser,
15762 CP_PARSER_DECLARATOR_EITHER,
15763 /*ctor_dtor_or_conv_p=*/NULL,
15764 parenthesized_p,
15765 /*member_p=*/false);
15766 parser->default_arg_ok_p = saved_default_arg_ok_p;
15767 /* After the declarator, allow more attributes. */
15768 decl_specifiers.attributes
15769 = chainon (decl_specifiers.attributes,
15770 cp_parser_attributes_opt (parser));
15771 }
15772
15773 /* If the next token is an ellipsis, and we have not seen a
15774 declarator name, and the type of the declarator contains parameter
15775 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15776 a parameter pack expansion expression. Otherwise, leave the
15777 ellipsis for a C-style variadic function. */
15778 token = cp_lexer_peek_token (parser->lexer);
15779 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15780 {
15781 tree type = decl_specifiers.type;
15782
15783 if (type && DECL_P (type))
15784 type = TREE_TYPE (type);
15785
15786 if (type
15787 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15788 && declarator_can_be_parameter_pack (declarator)
15789 && (!declarator || !declarator->parameter_pack_p)
15790 && uses_parameter_packs (type))
15791 {
15792 /* Consume the `...'. */
15793 cp_lexer_consume_token (parser->lexer);
15794 maybe_warn_variadic_templates ();
15795
15796 /* Build a pack expansion type */
15797 if (declarator)
15798 declarator->parameter_pack_p = true;
15799 else
15800 decl_specifiers.type = make_pack_expansion (type);
15801 }
15802 }
15803
15804 /* The restriction on defining new types applies only to the type
15805 of the parameter, not to the default argument. */
15806 parser->type_definition_forbidden_message = saved_message;
15807
15808 /* If the next token is `=', then process a default argument. */
15809 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15810 {
15811 /* Consume the `='. */
15812 cp_lexer_consume_token (parser->lexer);
15813
15814 /* If we are defining a class, then the tokens that make up the
15815 default argument must be saved and processed later. */
15816 if (!template_parm_p && at_class_scope_p ()
15817 && TYPE_BEING_DEFINED (current_class_type)
15818 && !LAMBDA_TYPE_P (current_class_type))
15819 {
15820 unsigned depth = 0;
15821 int maybe_template_id = 0;
15822 cp_token *first_token;
15823 cp_token *token;
15824
15825 /* Add tokens until we have processed the entire default
15826 argument. We add the range [first_token, token). */
15827 first_token = cp_lexer_peek_token (parser->lexer);
15828 while (true)
15829 {
15830 bool done = false;
15831
15832 /* Peek at the next token. */
15833 token = cp_lexer_peek_token (parser->lexer);
15834 /* What we do depends on what token we have. */
15835 switch (token->type)
15836 {
15837 /* In valid code, a default argument must be
15838 immediately followed by a `,' `)', or `...'. */
15839 case CPP_COMMA:
15840 if (depth == 0 && maybe_template_id)
15841 {
15842 /* If we've seen a '<', we might be in a
15843 template-argument-list. Until Core issue 325 is
15844 resolved, we don't know how this situation ought
15845 to be handled, so try to DTRT. We check whether
15846 what comes after the comma is a valid parameter
15847 declaration list. If it is, then the comma ends
15848 the default argument; otherwise the default
15849 argument continues. */
15850 bool error = false;
15851 tree t;
15852
15853 /* Set ITALP so cp_parser_parameter_declaration_list
15854 doesn't decide to commit to this parse. */
15855 bool saved_italp = parser->in_template_argument_list_p;
15856 parser->in_template_argument_list_p = true;
15857
15858 cp_parser_parse_tentatively (parser);
15859 cp_lexer_consume_token (parser->lexer);
15860 begin_scope (sk_function_parms, NULL_TREE);
15861 cp_parser_parameter_declaration_list (parser, &error);
15862 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15863 pop_binding (DECL_NAME (t), t);
15864 leave_scope ();
15865 if (!cp_parser_error_occurred (parser) && !error)
15866 done = true;
15867 cp_parser_abort_tentative_parse (parser);
15868
15869 parser->in_template_argument_list_p = saved_italp;
15870 break;
15871 }
15872 case CPP_CLOSE_PAREN:
15873 case CPP_ELLIPSIS:
15874 /* If we run into a non-nested `;', `}', or `]',
15875 then the code is invalid -- but the default
15876 argument is certainly over. */
15877 case CPP_SEMICOLON:
15878 case CPP_CLOSE_BRACE:
15879 case CPP_CLOSE_SQUARE:
15880 if (depth == 0)
15881 done = true;
15882 /* Update DEPTH, if necessary. */
15883 else if (token->type == CPP_CLOSE_PAREN
15884 || token->type == CPP_CLOSE_BRACE
15885 || token->type == CPP_CLOSE_SQUARE)
15886 --depth;
15887 break;
15888
15889 case CPP_OPEN_PAREN:
15890 case CPP_OPEN_SQUARE:
15891 case CPP_OPEN_BRACE:
15892 ++depth;
15893 break;
15894
15895 case CPP_LESS:
15896 if (depth == 0)
15897 /* This might be the comparison operator, or it might
15898 start a template argument list. */
15899 ++maybe_template_id;
15900 break;
15901
15902 case CPP_RSHIFT:
15903 if (cxx_dialect == cxx98)
15904 break;
15905 /* Fall through for C++0x, which treats the `>>'
15906 operator like two `>' tokens in certain
15907 cases. */
15908
15909 case CPP_GREATER:
15910 if (depth == 0)
15911 {
15912 /* This might be an operator, or it might close a
15913 template argument list. But if a previous '<'
15914 started a template argument list, this will have
15915 closed it, so we can't be in one anymore. */
15916 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15917 if (maybe_template_id < 0)
15918 maybe_template_id = 0;
15919 }
15920 break;
15921
15922 /* If we run out of tokens, issue an error message. */
15923 case CPP_EOF:
15924 case CPP_PRAGMA_EOL:
15925 error_at (token->location, "file ends in default argument");
15926 done = true;
15927 break;
15928
15929 case CPP_NAME:
15930 case CPP_SCOPE:
15931 /* In these cases, we should look for template-ids.
15932 For example, if the default argument is
15933 `X<int, double>()', we need to do name lookup to
15934 figure out whether or not `X' is a template; if
15935 so, the `,' does not end the default argument.
15936
15937 That is not yet done. */
15938 break;
15939
15940 default:
15941 break;
15942 }
15943
15944 /* If we've reached the end, stop. */
15945 if (done)
15946 break;
15947
15948 /* Add the token to the token block. */
15949 token = cp_lexer_consume_token (parser->lexer);
15950 }
15951
15952 /* Create a DEFAULT_ARG to represent the unparsed default
15953 argument. */
15954 default_argument = make_node (DEFAULT_ARG);
15955 DEFARG_TOKENS (default_argument)
15956 = cp_token_cache_new (first_token, token);
15957 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15958 }
15959 /* Outside of a class definition, we can just parse the
15960 assignment-expression. */
15961 else
15962 {
15963 token = cp_lexer_peek_token (parser->lexer);
15964 default_argument
15965 = cp_parser_default_argument (parser, template_parm_p);
15966 }
15967
15968 if (!parser->default_arg_ok_p)
15969 {
15970 if (flag_permissive)
15971 warning (0, "deprecated use of default argument for parameter of non-function");
15972 else
15973 {
15974 error_at (token->location,
15975 "default arguments are only "
15976 "permitted for function parameters");
15977 default_argument = NULL_TREE;
15978 }
15979 }
15980 else if ((declarator && declarator->parameter_pack_p)
15981 || (decl_specifiers.type
15982 && PACK_EXPANSION_P (decl_specifiers.type)))
15983 {
15984 /* Find the name of the parameter pack. */
15985 cp_declarator *id_declarator = declarator;
15986 while (id_declarator && id_declarator->kind != cdk_id)
15987 id_declarator = id_declarator->declarator;
15988
15989 if (id_declarator && id_declarator->kind == cdk_id)
15990 error_at (declarator_token_start->location,
15991 template_parm_p
15992 ? "template parameter pack %qD"
15993 " cannot have a default argument"
15994 : "parameter pack %qD cannot have a default argument",
15995 id_declarator->u.id.unqualified_name);
15996 else
15997 error_at (declarator_token_start->location,
15998 template_parm_p
15999 ? "template parameter pack cannot have a default argument"
16000 : "parameter pack cannot have a default argument");
16001
16002 default_argument = NULL_TREE;
16003 }
16004 }
16005 else
16006 default_argument = NULL_TREE;
16007
16008 return make_parameter_declarator (&decl_specifiers,
16009 declarator,
16010 default_argument);
16011 }
16012
16013 /* Parse a default argument and return it.
16014
16015 TEMPLATE_PARM_P is true if this is a default argument for a
16016 non-type template parameter. */
16017 static tree
16018 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16019 {
16020 tree default_argument = NULL_TREE;
16021 bool saved_greater_than_is_operator_p;
16022 bool saved_local_variables_forbidden_p;
16023
16024 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16025 set correctly. */
16026 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16027 parser->greater_than_is_operator_p = !template_parm_p;
16028 /* Local variable names (and the `this' keyword) may not
16029 appear in a default argument. */
16030 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16031 parser->local_variables_forbidden_p = true;
16032 /* Parse the assignment-expression. */
16033 if (template_parm_p)
16034 push_deferring_access_checks (dk_no_deferred);
16035 default_argument
16036 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16037 if (template_parm_p)
16038 pop_deferring_access_checks ();
16039 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16040 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16041
16042 return default_argument;
16043 }
16044
16045 /* Parse a function-body.
16046
16047 function-body:
16048 compound_statement */
16049
16050 static void
16051 cp_parser_function_body (cp_parser *parser)
16052 {
16053 cp_parser_compound_statement (parser, NULL, false);
16054 }
16055
16056 /* Parse a ctor-initializer-opt followed by a function-body. Return
16057 true if a ctor-initializer was present. */
16058
16059 static bool
16060 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16061 {
16062 tree body;
16063 bool ctor_initializer_p;
16064
16065 /* Begin the function body. */
16066 body = begin_function_body ();
16067 /* Parse the optional ctor-initializer. */
16068 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16069 /* Parse the function-body. */
16070 cp_parser_function_body (parser);
16071 /* Finish the function body. */
16072 finish_function_body (body);
16073
16074 return ctor_initializer_p;
16075 }
16076
16077 /* Parse an initializer.
16078
16079 initializer:
16080 = initializer-clause
16081 ( expression-list )
16082
16083 Returns an expression representing the initializer. If no
16084 initializer is present, NULL_TREE is returned.
16085
16086 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16087 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16088 set to TRUE if there is no initializer present. If there is an
16089 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16090 is set to true; otherwise it is set to false. */
16091
16092 static tree
16093 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16094 bool* non_constant_p)
16095 {
16096 cp_token *token;
16097 tree init;
16098
16099 /* Peek at the next token. */
16100 token = cp_lexer_peek_token (parser->lexer);
16101
16102 /* Let our caller know whether or not this initializer was
16103 parenthesized. */
16104 *is_direct_init = (token->type != CPP_EQ);
16105 /* Assume that the initializer is constant. */
16106 *non_constant_p = false;
16107
16108 if (token->type == CPP_EQ)
16109 {
16110 /* Consume the `='. */
16111 cp_lexer_consume_token (parser->lexer);
16112 /* Parse the initializer-clause. */
16113 init = cp_parser_initializer_clause (parser, non_constant_p);
16114 }
16115 else if (token->type == CPP_OPEN_PAREN)
16116 {
16117 VEC(tree,gc) *vec;
16118 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16119 /*cast_p=*/false,
16120 /*allow_expansion_p=*/true,
16121 non_constant_p);
16122 if (vec == NULL)
16123 return error_mark_node;
16124 init = build_tree_list_vec (vec);
16125 release_tree_vector (vec);
16126 }
16127 else if (token->type == CPP_OPEN_BRACE)
16128 {
16129 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16130 init = cp_parser_braced_list (parser, non_constant_p);
16131 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16132 }
16133 else
16134 {
16135 /* Anything else is an error. */
16136 cp_parser_error (parser, "expected initializer");
16137 init = error_mark_node;
16138 }
16139
16140 return init;
16141 }
16142
16143 /* Parse an initializer-clause.
16144
16145 initializer-clause:
16146 assignment-expression
16147 braced-init-list
16148
16149 Returns an expression representing the initializer.
16150
16151 If the `assignment-expression' production is used the value
16152 returned is simply a representation for the expression.
16153
16154 Otherwise, calls cp_parser_braced_list. */
16155
16156 static tree
16157 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16158 {
16159 tree initializer;
16160
16161 /* Assume the expression is constant. */
16162 *non_constant_p = false;
16163
16164 /* If it is not a `{', then we are looking at an
16165 assignment-expression. */
16166 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16167 {
16168 initializer
16169 = cp_parser_constant_expression (parser,
16170 /*allow_non_constant_p=*/true,
16171 non_constant_p);
16172 if (!*non_constant_p)
16173 initializer = fold_non_dependent_expr (initializer);
16174 }
16175 else
16176 initializer = cp_parser_braced_list (parser, non_constant_p);
16177
16178 return initializer;
16179 }
16180
16181 /* Parse a brace-enclosed initializer list.
16182
16183 braced-init-list:
16184 { initializer-list , [opt] }
16185 { }
16186
16187 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16188 the elements of the initializer-list (or NULL, if the last
16189 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16190 NULL_TREE. There is no way to detect whether or not the optional
16191 trailing `,' was provided. NON_CONSTANT_P is as for
16192 cp_parser_initializer. */
16193
16194 static tree
16195 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16196 {
16197 tree initializer;
16198
16199 /* Consume the `{' token. */
16200 cp_lexer_consume_token (parser->lexer);
16201 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16202 initializer = make_node (CONSTRUCTOR);
16203 /* If it's not a `}', then there is a non-trivial initializer. */
16204 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16205 {
16206 /* Parse the initializer list. */
16207 CONSTRUCTOR_ELTS (initializer)
16208 = cp_parser_initializer_list (parser, non_constant_p);
16209 /* A trailing `,' token is allowed. */
16210 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16211 cp_lexer_consume_token (parser->lexer);
16212 }
16213 /* Now, there should be a trailing `}'. */
16214 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16215 TREE_TYPE (initializer) = init_list_type_node;
16216 return initializer;
16217 }
16218
16219 /* Parse an initializer-list.
16220
16221 initializer-list:
16222 initializer-clause ... [opt]
16223 initializer-list , initializer-clause ... [opt]
16224
16225 GNU Extension:
16226
16227 initializer-list:
16228 identifier : initializer-clause
16229 initializer-list, identifier : initializer-clause
16230
16231 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16232 for the initializer. If the INDEX of the elt is non-NULL, it is the
16233 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16234 as for cp_parser_initializer. */
16235
16236 static VEC(constructor_elt,gc) *
16237 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16238 {
16239 VEC(constructor_elt,gc) *v = NULL;
16240
16241 /* Assume all of the expressions are constant. */
16242 *non_constant_p = false;
16243
16244 /* Parse the rest of the list. */
16245 while (true)
16246 {
16247 cp_token *token;
16248 tree identifier;
16249 tree initializer;
16250 bool clause_non_constant_p;
16251
16252 /* If the next token is an identifier and the following one is a
16253 colon, we are looking at the GNU designated-initializer
16254 syntax. */
16255 if (cp_parser_allow_gnu_extensions_p (parser)
16256 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16257 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16258 {
16259 /* Warn the user that they are using an extension. */
16260 pedwarn (input_location, OPT_pedantic,
16261 "ISO C++ does not allow designated initializers");
16262 /* Consume the identifier. */
16263 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16264 /* Consume the `:'. */
16265 cp_lexer_consume_token (parser->lexer);
16266 }
16267 else
16268 identifier = NULL_TREE;
16269
16270 /* Parse the initializer. */
16271 initializer = cp_parser_initializer_clause (parser,
16272 &clause_non_constant_p);
16273 /* If any clause is non-constant, so is the entire initializer. */
16274 if (clause_non_constant_p)
16275 *non_constant_p = true;
16276
16277 /* If we have an ellipsis, this is an initializer pack
16278 expansion. */
16279 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16280 {
16281 /* Consume the `...'. */
16282 cp_lexer_consume_token (parser->lexer);
16283
16284 /* Turn the initializer into an initializer expansion. */
16285 initializer = make_pack_expansion (initializer);
16286 }
16287
16288 /* Add it to the vector. */
16289 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16290
16291 /* If the next token is not a comma, we have reached the end of
16292 the list. */
16293 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16294 break;
16295
16296 /* Peek at the next token. */
16297 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16298 /* If the next token is a `}', then we're still done. An
16299 initializer-clause can have a trailing `,' after the
16300 initializer-list and before the closing `}'. */
16301 if (token->type == CPP_CLOSE_BRACE)
16302 break;
16303
16304 /* Consume the `,' token. */
16305 cp_lexer_consume_token (parser->lexer);
16306 }
16307
16308 return v;
16309 }
16310
16311 /* Classes [gram.class] */
16312
16313 /* Parse a class-name.
16314
16315 class-name:
16316 identifier
16317 template-id
16318
16319 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16320 to indicate that names looked up in dependent types should be
16321 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16322 keyword has been used to indicate that the name that appears next
16323 is a template. TAG_TYPE indicates the explicit tag given before
16324 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16325 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16326 is the class being defined in a class-head.
16327
16328 Returns the TYPE_DECL representing the class. */
16329
16330 static tree
16331 cp_parser_class_name (cp_parser *parser,
16332 bool typename_keyword_p,
16333 bool template_keyword_p,
16334 enum tag_types tag_type,
16335 bool check_dependency_p,
16336 bool class_head_p,
16337 bool is_declaration)
16338 {
16339 tree decl;
16340 tree scope;
16341 bool typename_p;
16342 cp_token *token;
16343 tree identifier = NULL_TREE;
16344
16345 /* All class-names start with an identifier. */
16346 token = cp_lexer_peek_token (parser->lexer);
16347 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16348 {
16349 cp_parser_error (parser, "expected class-name");
16350 return error_mark_node;
16351 }
16352
16353 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16354 to a template-id, so we save it here. */
16355 scope = parser->scope;
16356 if (scope == error_mark_node)
16357 return error_mark_node;
16358
16359 /* Any name names a type if we're following the `typename' keyword
16360 in a qualified name where the enclosing scope is type-dependent. */
16361 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16362 && dependent_type_p (scope));
16363 /* Handle the common case (an identifier, but not a template-id)
16364 efficiently. */
16365 if (token->type == CPP_NAME
16366 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16367 {
16368 cp_token *identifier_token;
16369 bool ambiguous_p;
16370
16371 /* Look for the identifier. */
16372 identifier_token = cp_lexer_peek_token (parser->lexer);
16373 ambiguous_p = identifier_token->ambiguous_p;
16374 identifier = cp_parser_identifier (parser);
16375 /* If the next token isn't an identifier, we are certainly not
16376 looking at a class-name. */
16377 if (identifier == error_mark_node)
16378 decl = error_mark_node;
16379 /* If we know this is a type-name, there's no need to look it
16380 up. */
16381 else if (typename_p)
16382 decl = identifier;
16383 else
16384 {
16385 tree ambiguous_decls;
16386 /* If we already know that this lookup is ambiguous, then
16387 we've already issued an error message; there's no reason
16388 to check again. */
16389 if (ambiguous_p)
16390 {
16391 cp_parser_simulate_error (parser);
16392 return error_mark_node;
16393 }
16394 /* If the next token is a `::', then the name must be a type
16395 name.
16396
16397 [basic.lookup.qual]
16398
16399 During the lookup for a name preceding the :: scope
16400 resolution operator, object, function, and enumerator
16401 names are ignored. */
16402 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16403 tag_type = typename_type;
16404 /* Look up the name. */
16405 decl = cp_parser_lookup_name (parser, identifier,
16406 tag_type,
16407 /*is_template=*/false,
16408 /*is_namespace=*/false,
16409 check_dependency_p,
16410 &ambiguous_decls,
16411 identifier_token->location);
16412 if (ambiguous_decls)
16413 {
16414 if (cp_parser_parsing_tentatively (parser))
16415 cp_parser_simulate_error (parser);
16416 return error_mark_node;
16417 }
16418 }
16419 }
16420 else
16421 {
16422 /* Try a template-id. */
16423 decl = cp_parser_template_id (parser, template_keyword_p,
16424 check_dependency_p,
16425 is_declaration);
16426 if (decl == error_mark_node)
16427 return error_mark_node;
16428 }
16429
16430 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16431
16432 /* If this is a typename, create a TYPENAME_TYPE. */
16433 if (typename_p && decl != error_mark_node)
16434 {
16435 decl = make_typename_type (scope, decl, typename_type,
16436 /*complain=*/tf_error);
16437 if (decl != error_mark_node)
16438 decl = TYPE_NAME (decl);
16439 }
16440
16441 /* Check to see that it is really the name of a class. */
16442 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16443 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16444 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16445 /* Situations like this:
16446
16447 template <typename T> struct A {
16448 typename T::template X<int>::I i;
16449 };
16450
16451 are problematic. Is `T::template X<int>' a class-name? The
16452 standard does not seem to be definitive, but there is no other
16453 valid interpretation of the following `::'. Therefore, those
16454 names are considered class-names. */
16455 {
16456 decl = make_typename_type (scope, decl, tag_type, tf_error);
16457 if (decl != error_mark_node)
16458 decl = TYPE_NAME (decl);
16459 }
16460 else if (TREE_CODE (decl) != TYPE_DECL
16461 || TREE_TYPE (decl) == error_mark_node
16462 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
16463 decl = error_mark_node;
16464
16465 if (decl == error_mark_node)
16466 cp_parser_error (parser, "expected class-name");
16467 else if (identifier && !parser->scope)
16468 maybe_note_name_used_in_class (identifier, decl);
16469
16470 return decl;
16471 }
16472
16473 /* Parse a class-specifier.
16474
16475 class-specifier:
16476 class-head { member-specification [opt] }
16477
16478 Returns the TREE_TYPE representing the class. */
16479
16480 static tree
16481 cp_parser_class_specifier (cp_parser* parser)
16482 {
16483 tree type;
16484 tree attributes = NULL_TREE;
16485 bool nested_name_specifier_p;
16486 unsigned saved_num_template_parameter_lists;
16487 bool saved_in_function_body;
16488 bool saved_in_unbraced_linkage_specification_p;
16489 tree old_scope = NULL_TREE;
16490 tree scope = NULL_TREE;
16491 tree bases;
16492
16493 push_deferring_access_checks (dk_no_deferred);
16494
16495 /* Parse the class-head. */
16496 type = cp_parser_class_head (parser,
16497 &nested_name_specifier_p,
16498 &attributes,
16499 &bases);
16500 /* If the class-head was a semantic disaster, skip the entire body
16501 of the class. */
16502 if (!type)
16503 {
16504 cp_parser_skip_to_end_of_block_or_statement (parser);
16505 pop_deferring_access_checks ();
16506 return error_mark_node;
16507 }
16508
16509 /* Look for the `{'. */
16510 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16511 {
16512 pop_deferring_access_checks ();
16513 return error_mark_node;
16514 }
16515
16516 /* Process the base classes. If they're invalid, skip the
16517 entire class body. */
16518 if (!xref_basetypes (type, bases))
16519 {
16520 /* Consuming the closing brace yields better error messages
16521 later on. */
16522 if (cp_parser_skip_to_closing_brace (parser))
16523 cp_lexer_consume_token (parser->lexer);
16524 pop_deferring_access_checks ();
16525 return error_mark_node;
16526 }
16527
16528 /* Issue an error message if type-definitions are forbidden here. */
16529 cp_parser_check_type_definition (parser);
16530 /* Remember that we are defining one more class. */
16531 ++parser->num_classes_being_defined;
16532 /* Inside the class, surrounding template-parameter-lists do not
16533 apply. */
16534 saved_num_template_parameter_lists
16535 = parser->num_template_parameter_lists;
16536 parser->num_template_parameter_lists = 0;
16537 /* We are not in a function body. */
16538 saved_in_function_body = parser->in_function_body;
16539 parser->in_function_body = false;
16540 /* We are not immediately inside an extern "lang" block. */
16541 saved_in_unbraced_linkage_specification_p
16542 = parser->in_unbraced_linkage_specification_p;
16543 parser->in_unbraced_linkage_specification_p = false;
16544
16545 /* Start the class. */
16546 if (nested_name_specifier_p)
16547 {
16548 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16549 old_scope = push_inner_scope (scope);
16550 }
16551 type = begin_class_definition (type, attributes);
16552
16553 if (type == error_mark_node)
16554 /* If the type is erroneous, skip the entire body of the class. */
16555 cp_parser_skip_to_closing_brace (parser);
16556 else
16557 /* Parse the member-specification. */
16558 cp_parser_member_specification_opt (parser);
16559
16560 /* Look for the trailing `}'. */
16561 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16562 /* Look for trailing attributes to apply to this class. */
16563 if (cp_parser_allow_gnu_extensions_p (parser))
16564 attributes = cp_parser_attributes_opt (parser);
16565 if (type != error_mark_node)
16566 type = finish_struct (type, attributes);
16567 if (nested_name_specifier_p)
16568 pop_inner_scope (old_scope, scope);
16569 /* If this class is not itself within the scope of another class,
16570 then we need to parse the bodies of all of the queued function
16571 definitions. Note that the queued functions defined in a class
16572 are not always processed immediately following the
16573 class-specifier for that class. Consider:
16574
16575 struct A {
16576 struct B { void f() { sizeof (A); } };
16577 };
16578
16579 If `f' were processed before the processing of `A' were
16580 completed, there would be no way to compute the size of `A'.
16581 Note that the nesting we are interested in here is lexical --
16582 not the semantic nesting given by TYPE_CONTEXT. In particular,
16583 for:
16584
16585 struct A { struct B; };
16586 struct A::B { void f() { } };
16587
16588 there is no need to delay the parsing of `A::B::f'. */
16589 if (--parser->num_classes_being_defined == 0)
16590 {
16591 tree fn;
16592 tree class_type = NULL_TREE;
16593 tree pushed_scope = NULL_TREE;
16594 unsigned ix;
16595 cp_default_arg_entry *e;
16596
16597 /* In a first pass, parse default arguments to the functions.
16598 Then, in a second pass, parse the bodies of the functions.
16599 This two-phased approach handles cases like:
16600
16601 struct S {
16602 void f() { g(); }
16603 void g(int i = 3);
16604 };
16605
16606 */
16607 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
16608 ix, e)
16609 {
16610 fn = e->decl;
16611 /* If there are default arguments that have not yet been processed,
16612 take care of them now. */
16613 if (class_type != e->class_type)
16614 {
16615 if (pushed_scope)
16616 pop_scope (pushed_scope);
16617 class_type = e->class_type;
16618 pushed_scope = push_scope (class_type);
16619 }
16620 /* Make sure that any template parameters are in scope. */
16621 maybe_begin_member_template_processing (fn);
16622 /* Parse the default argument expressions. */
16623 cp_parser_late_parsing_default_args (parser, fn);
16624 /* Remove any template parameters from the symbol table. */
16625 maybe_end_member_template_processing ();
16626 }
16627 if (pushed_scope)
16628 pop_scope (pushed_scope);
16629 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
16630 /* Now parse the body of the functions. */
16631 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
16632 cp_parser_late_parsing_for_member (parser, fn);
16633 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
16634 }
16635
16636 /* Put back any saved access checks. */
16637 pop_deferring_access_checks ();
16638
16639 /* Restore saved state. */
16640 parser->in_function_body = saved_in_function_body;
16641 parser->num_template_parameter_lists
16642 = saved_num_template_parameter_lists;
16643 parser->in_unbraced_linkage_specification_p
16644 = saved_in_unbraced_linkage_specification_p;
16645
16646 return type;
16647 }
16648
16649 /* Parse a class-head.
16650
16651 class-head:
16652 class-key identifier [opt] base-clause [opt]
16653 class-key nested-name-specifier identifier base-clause [opt]
16654 class-key nested-name-specifier [opt] template-id
16655 base-clause [opt]
16656
16657 GNU Extensions:
16658 class-key attributes identifier [opt] base-clause [opt]
16659 class-key attributes nested-name-specifier identifier base-clause [opt]
16660 class-key attributes nested-name-specifier [opt] template-id
16661 base-clause [opt]
16662
16663 Upon return BASES is initialized to the list of base classes (or
16664 NULL, if there are none) in the same form returned by
16665 cp_parser_base_clause.
16666
16667 Returns the TYPE of the indicated class. Sets
16668 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16669 involving a nested-name-specifier was used, and FALSE otherwise.
16670
16671 Returns error_mark_node if this is not a class-head.
16672
16673 Returns NULL_TREE if the class-head is syntactically valid, but
16674 semantically invalid in a way that means we should skip the entire
16675 body of the class. */
16676
16677 static tree
16678 cp_parser_class_head (cp_parser* parser,
16679 bool* nested_name_specifier_p,
16680 tree *attributes_p,
16681 tree *bases)
16682 {
16683 tree nested_name_specifier;
16684 enum tag_types class_key;
16685 tree id = NULL_TREE;
16686 tree type = NULL_TREE;
16687 tree attributes;
16688 bool template_id_p = false;
16689 bool qualified_p = false;
16690 bool invalid_nested_name_p = false;
16691 bool invalid_explicit_specialization_p = false;
16692 tree pushed_scope = NULL_TREE;
16693 unsigned num_templates;
16694 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16695 /* Assume no nested-name-specifier will be present. */
16696 *nested_name_specifier_p = false;
16697 /* Assume no template parameter lists will be used in defining the
16698 type. */
16699 num_templates = 0;
16700
16701 *bases = NULL_TREE;
16702
16703 /* Look for the class-key. */
16704 class_key = cp_parser_class_key (parser);
16705 if (class_key == none_type)
16706 return error_mark_node;
16707
16708 /* Parse the attributes. */
16709 attributes = cp_parser_attributes_opt (parser);
16710
16711 /* If the next token is `::', that is invalid -- but sometimes
16712 people do try to write:
16713
16714 struct ::S {};
16715
16716 Handle this gracefully by accepting the extra qualifier, and then
16717 issuing an error about it later if this really is a
16718 class-head. If it turns out just to be an elaborated type
16719 specifier, remain silent. */
16720 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16721 qualified_p = true;
16722
16723 push_deferring_access_checks (dk_no_check);
16724
16725 /* Determine the name of the class. Begin by looking for an
16726 optional nested-name-specifier. */
16727 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16728 nested_name_specifier
16729 = cp_parser_nested_name_specifier_opt (parser,
16730 /*typename_keyword_p=*/false,
16731 /*check_dependency_p=*/false,
16732 /*type_p=*/false,
16733 /*is_declaration=*/false);
16734 /* If there was a nested-name-specifier, then there *must* be an
16735 identifier. */
16736 if (nested_name_specifier)
16737 {
16738 type_start_token = cp_lexer_peek_token (parser->lexer);
16739 /* Although the grammar says `identifier', it really means
16740 `class-name' or `template-name'. You are only allowed to
16741 define a class that has already been declared with this
16742 syntax.
16743
16744 The proposed resolution for Core Issue 180 says that wherever
16745 you see `class T::X' you should treat `X' as a type-name.
16746
16747 It is OK to define an inaccessible class; for example:
16748
16749 class A { class B; };
16750 class A::B {};
16751
16752 We do not know if we will see a class-name, or a
16753 template-name. We look for a class-name first, in case the
16754 class-name is a template-id; if we looked for the
16755 template-name first we would stop after the template-name. */
16756 cp_parser_parse_tentatively (parser);
16757 type = cp_parser_class_name (parser,
16758 /*typename_keyword_p=*/false,
16759 /*template_keyword_p=*/false,
16760 class_type,
16761 /*check_dependency_p=*/false,
16762 /*class_head_p=*/true,
16763 /*is_declaration=*/false);
16764 /* If that didn't work, ignore the nested-name-specifier. */
16765 if (!cp_parser_parse_definitely (parser))
16766 {
16767 invalid_nested_name_p = true;
16768 type_start_token = cp_lexer_peek_token (parser->lexer);
16769 id = cp_parser_identifier (parser);
16770 if (id == error_mark_node)
16771 id = NULL_TREE;
16772 }
16773 /* If we could not find a corresponding TYPE, treat this
16774 declaration like an unqualified declaration. */
16775 if (type == error_mark_node)
16776 nested_name_specifier = NULL_TREE;
16777 /* Otherwise, count the number of templates used in TYPE and its
16778 containing scopes. */
16779 else
16780 {
16781 tree scope;
16782
16783 for (scope = TREE_TYPE (type);
16784 scope && TREE_CODE (scope) != NAMESPACE_DECL;
16785 scope = (TYPE_P (scope)
16786 ? TYPE_CONTEXT (scope)
16787 : DECL_CONTEXT (scope)))
16788 if (TYPE_P (scope)
16789 && CLASS_TYPE_P (scope)
16790 && CLASSTYPE_TEMPLATE_INFO (scope)
16791 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16792 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16793 ++num_templates;
16794 }
16795 }
16796 /* Otherwise, the identifier is optional. */
16797 else
16798 {
16799 /* We don't know whether what comes next is a template-id,
16800 an identifier, or nothing at all. */
16801 cp_parser_parse_tentatively (parser);
16802 /* Check for a template-id. */
16803 type_start_token = cp_lexer_peek_token (parser->lexer);
16804 id = cp_parser_template_id (parser,
16805 /*template_keyword_p=*/false,
16806 /*check_dependency_p=*/true,
16807 /*is_declaration=*/true);
16808 /* If that didn't work, it could still be an identifier. */
16809 if (!cp_parser_parse_definitely (parser))
16810 {
16811 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16812 {
16813 type_start_token = cp_lexer_peek_token (parser->lexer);
16814 id = cp_parser_identifier (parser);
16815 }
16816 else
16817 id = NULL_TREE;
16818 }
16819 else
16820 {
16821 template_id_p = true;
16822 ++num_templates;
16823 }
16824 }
16825
16826 pop_deferring_access_checks ();
16827
16828 if (id)
16829 cp_parser_check_for_invalid_template_id (parser, id,
16830 type_start_token->location);
16831
16832 /* If it's not a `:' or a `{' then we can't really be looking at a
16833 class-head, since a class-head only appears as part of a
16834 class-specifier. We have to detect this situation before calling
16835 xref_tag, since that has irreversible side-effects. */
16836 if (!cp_parser_next_token_starts_class_definition_p (parser))
16837 {
16838 cp_parser_error (parser, "expected %<{%> or %<:%>");
16839 return error_mark_node;
16840 }
16841
16842 /* At this point, we're going ahead with the class-specifier, even
16843 if some other problem occurs. */
16844 cp_parser_commit_to_tentative_parse (parser);
16845 /* Issue the error about the overly-qualified name now. */
16846 if (qualified_p)
16847 {
16848 cp_parser_error (parser,
16849 "global qualification of class name is invalid");
16850 return error_mark_node;
16851 }
16852 else if (invalid_nested_name_p)
16853 {
16854 cp_parser_error (parser,
16855 "qualified name does not name a class");
16856 return error_mark_node;
16857 }
16858 else if (nested_name_specifier)
16859 {
16860 tree scope;
16861
16862 /* Reject typedef-names in class heads. */
16863 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16864 {
16865 error_at (type_start_token->location,
16866 "invalid class name in declaration of %qD",
16867 type);
16868 type = NULL_TREE;
16869 goto done;
16870 }
16871
16872 /* Figure out in what scope the declaration is being placed. */
16873 scope = current_scope ();
16874 /* If that scope does not contain the scope in which the
16875 class was originally declared, the program is invalid. */
16876 if (scope && !is_ancestor (scope, nested_name_specifier))
16877 {
16878 if (at_namespace_scope_p ())
16879 error_at (type_start_token->location,
16880 "declaration of %qD in namespace %qD which does not "
16881 "enclose %qD",
16882 type, scope, nested_name_specifier);
16883 else
16884 error_at (type_start_token->location,
16885 "declaration of %qD in %qD which does not enclose %qD",
16886 type, scope, nested_name_specifier);
16887 type = NULL_TREE;
16888 goto done;
16889 }
16890 /* [dcl.meaning]
16891
16892 A declarator-id shall not be qualified except for the
16893 definition of a ... nested class outside of its class
16894 ... [or] the definition or explicit instantiation of a
16895 class member of a namespace outside of its namespace. */
16896 if (scope == nested_name_specifier)
16897 {
16898 permerror (nested_name_specifier_token_start->location,
16899 "extra qualification not allowed");
16900 nested_name_specifier = NULL_TREE;
16901 num_templates = 0;
16902 }
16903 }
16904 /* An explicit-specialization must be preceded by "template <>". If
16905 it is not, try to recover gracefully. */
16906 if (at_namespace_scope_p ()
16907 && parser->num_template_parameter_lists == 0
16908 && template_id_p)
16909 {
16910 error_at (type_start_token->location,
16911 "an explicit specialization must be preceded by %<template <>%>");
16912 invalid_explicit_specialization_p = true;
16913 /* Take the same action that would have been taken by
16914 cp_parser_explicit_specialization. */
16915 ++parser->num_template_parameter_lists;
16916 begin_specialization ();
16917 }
16918 /* There must be no "return" statements between this point and the
16919 end of this function; set "type "to the correct return value and
16920 use "goto done;" to return. */
16921 /* Make sure that the right number of template parameters were
16922 present. */
16923 if (!cp_parser_check_template_parameters (parser, num_templates,
16924 type_start_token->location,
16925 /*declarator=*/NULL))
16926 {
16927 /* If something went wrong, there is no point in even trying to
16928 process the class-definition. */
16929 type = NULL_TREE;
16930 goto done;
16931 }
16932
16933 /* Look up the type. */
16934 if (template_id_p)
16935 {
16936 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16937 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16938 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16939 {
16940 error_at (type_start_token->location,
16941 "function template %qD redeclared as a class template", id);
16942 type = error_mark_node;
16943 }
16944 else
16945 {
16946 type = TREE_TYPE (id);
16947 type = maybe_process_partial_specialization (type);
16948 }
16949 if (nested_name_specifier)
16950 pushed_scope = push_scope (nested_name_specifier);
16951 }
16952 else if (nested_name_specifier)
16953 {
16954 tree class_type;
16955
16956 /* Given:
16957
16958 template <typename T> struct S { struct T };
16959 template <typename T> struct S<T>::T { };
16960
16961 we will get a TYPENAME_TYPE when processing the definition of
16962 `S::T'. We need to resolve it to the actual type before we
16963 try to define it. */
16964 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16965 {
16966 class_type = resolve_typename_type (TREE_TYPE (type),
16967 /*only_current_p=*/false);
16968 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16969 type = TYPE_NAME (class_type);
16970 else
16971 {
16972 cp_parser_error (parser, "could not resolve typename type");
16973 type = error_mark_node;
16974 }
16975 }
16976
16977 if (maybe_process_partial_specialization (TREE_TYPE (type))
16978 == error_mark_node)
16979 {
16980 type = NULL_TREE;
16981 goto done;
16982 }
16983
16984 class_type = current_class_type;
16985 /* Enter the scope indicated by the nested-name-specifier. */
16986 pushed_scope = push_scope (nested_name_specifier);
16987 /* Get the canonical version of this type. */
16988 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16989 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16990 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16991 {
16992 type = push_template_decl (type);
16993 if (type == error_mark_node)
16994 {
16995 type = NULL_TREE;
16996 goto done;
16997 }
16998 }
16999
17000 type = TREE_TYPE (type);
17001 *nested_name_specifier_p = true;
17002 }
17003 else /* The name is not a nested name. */
17004 {
17005 /* If the class was unnamed, create a dummy name. */
17006 if (!id)
17007 id = make_anon_name ();
17008 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17009 parser->num_template_parameter_lists);
17010 }
17011
17012 /* Indicate whether this class was declared as a `class' or as a
17013 `struct'. */
17014 if (TREE_CODE (type) == RECORD_TYPE)
17015 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17016 cp_parser_check_class_key (class_key, type);
17017
17018 /* If this type was already complete, and we see another definition,
17019 that's an error. */
17020 if (type != error_mark_node && COMPLETE_TYPE_P (type))
17021 {
17022 error_at (type_start_token->location, "redefinition of %q#T",
17023 type);
17024 error_at (type_start_token->location, "previous definition of %q+#T",
17025 type);
17026 type = NULL_TREE;
17027 goto done;
17028 }
17029 else if (type == error_mark_node)
17030 type = NULL_TREE;
17031
17032 /* We will have entered the scope containing the class; the names of
17033 base classes should be looked up in that context. For example:
17034
17035 struct A { struct B {}; struct C; };
17036 struct A::C : B {};
17037
17038 is valid. */
17039
17040 /* Get the list of base-classes, if there is one. */
17041 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17042 *bases = cp_parser_base_clause (parser);
17043
17044 done:
17045 /* Leave the scope given by the nested-name-specifier. We will
17046 enter the class scope itself while processing the members. */
17047 if (pushed_scope)
17048 pop_scope (pushed_scope);
17049
17050 if (invalid_explicit_specialization_p)
17051 {
17052 end_specialization ();
17053 --parser->num_template_parameter_lists;
17054 }
17055
17056 if (type)
17057 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17058 *attributes_p = attributes;
17059 return type;
17060 }
17061
17062 /* Parse a class-key.
17063
17064 class-key:
17065 class
17066 struct
17067 union
17068
17069 Returns the kind of class-key specified, or none_type to indicate
17070 error. */
17071
17072 static enum tag_types
17073 cp_parser_class_key (cp_parser* parser)
17074 {
17075 cp_token *token;
17076 enum tag_types tag_type;
17077
17078 /* Look for the class-key. */
17079 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17080 if (!token)
17081 return none_type;
17082
17083 /* Check to see if the TOKEN is a class-key. */
17084 tag_type = cp_parser_token_is_class_key (token);
17085 if (!tag_type)
17086 cp_parser_error (parser, "expected class-key");
17087 return tag_type;
17088 }
17089
17090 /* Parse an (optional) member-specification.
17091
17092 member-specification:
17093 member-declaration member-specification [opt]
17094 access-specifier : member-specification [opt] */
17095
17096 static void
17097 cp_parser_member_specification_opt (cp_parser* parser)
17098 {
17099 while (true)
17100 {
17101 cp_token *token;
17102 enum rid keyword;
17103
17104 /* Peek at the next token. */
17105 token = cp_lexer_peek_token (parser->lexer);
17106 /* If it's a `}', or EOF then we've seen all the members. */
17107 if (token->type == CPP_CLOSE_BRACE
17108 || token->type == CPP_EOF
17109 || token->type == CPP_PRAGMA_EOL)
17110 break;
17111
17112 /* See if this token is a keyword. */
17113 keyword = token->keyword;
17114 switch (keyword)
17115 {
17116 case RID_PUBLIC:
17117 case RID_PROTECTED:
17118 case RID_PRIVATE:
17119 /* Consume the access-specifier. */
17120 cp_lexer_consume_token (parser->lexer);
17121 /* Remember which access-specifier is active. */
17122 current_access_specifier = token->u.value;
17123 /* Look for the `:'. */
17124 cp_parser_require (parser, CPP_COLON, RT_COLON);
17125 break;
17126
17127 default:
17128 /* Accept #pragmas at class scope. */
17129 if (token->type == CPP_PRAGMA)
17130 {
17131 cp_parser_pragma (parser, pragma_external);
17132 break;
17133 }
17134
17135 /* Otherwise, the next construction must be a
17136 member-declaration. */
17137 cp_parser_member_declaration (parser);
17138 }
17139 }
17140 }
17141
17142 /* Parse a member-declaration.
17143
17144 member-declaration:
17145 decl-specifier-seq [opt] member-declarator-list [opt] ;
17146 function-definition ; [opt]
17147 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17148 using-declaration
17149 template-declaration
17150
17151 member-declarator-list:
17152 member-declarator
17153 member-declarator-list , member-declarator
17154
17155 member-declarator:
17156 declarator pure-specifier [opt]
17157 declarator constant-initializer [opt]
17158 identifier [opt] : constant-expression
17159
17160 GNU Extensions:
17161
17162 member-declaration:
17163 __extension__ member-declaration
17164
17165 member-declarator:
17166 declarator attributes [opt] pure-specifier [opt]
17167 declarator attributes [opt] constant-initializer [opt]
17168 identifier [opt] attributes [opt] : constant-expression
17169
17170 C++0x Extensions:
17171
17172 member-declaration:
17173 static_assert-declaration */
17174
17175 static void
17176 cp_parser_member_declaration (cp_parser* parser)
17177 {
17178 cp_decl_specifier_seq decl_specifiers;
17179 tree prefix_attributes;
17180 tree decl;
17181 int declares_class_or_enum;
17182 bool friend_p;
17183 cp_token *token = NULL;
17184 cp_token *decl_spec_token_start = NULL;
17185 cp_token *initializer_token_start = NULL;
17186 int saved_pedantic;
17187
17188 /* Check for the `__extension__' keyword. */
17189 if (cp_parser_extension_opt (parser, &saved_pedantic))
17190 {
17191 /* Recurse. */
17192 cp_parser_member_declaration (parser);
17193 /* Restore the old value of the PEDANTIC flag. */
17194 pedantic = saved_pedantic;
17195
17196 return;
17197 }
17198
17199 /* Check for a template-declaration. */
17200 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17201 {
17202 /* An explicit specialization here is an error condition, and we
17203 expect the specialization handler to detect and report this. */
17204 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17205 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17206 cp_parser_explicit_specialization (parser);
17207 else
17208 cp_parser_template_declaration (parser, /*member_p=*/true);
17209
17210 return;
17211 }
17212
17213 /* Check for a using-declaration. */
17214 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17215 {
17216 /* Parse the using-declaration. */
17217 cp_parser_using_declaration (parser,
17218 /*access_declaration_p=*/false);
17219 return;
17220 }
17221
17222 /* Check for @defs. */
17223 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17224 {
17225 tree ivar, member;
17226 tree ivar_chains = cp_parser_objc_defs_expression (parser);
17227 ivar = ivar_chains;
17228 while (ivar)
17229 {
17230 member = ivar;
17231 ivar = TREE_CHAIN (member);
17232 TREE_CHAIN (member) = NULL_TREE;
17233 finish_member_declaration (member);
17234 }
17235 return;
17236 }
17237
17238 /* If the next token is `static_assert' we have a static assertion. */
17239 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17240 {
17241 cp_parser_static_assert (parser, /*member_p=*/true);
17242 return;
17243 }
17244
17245 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17246 return;
17247
17248 /* Parse the decl-specifier-seq. */
17249 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17250 cp_parser_decl_specifier_seq (parser,
17251 CP_PARSER_FLAGS_OPTIONAL,
17252 &decl_specifiers,
17253 &declares_class_or_enum);
17254 prefix_attributes = decl_specifiers.attributes;
17255 decl_specifiers.attributes = NULL_TREE;
17256 /* Check for an invalid type-name. */
17257 if (!decl_specifiers.any_type_specifiers_p
17258 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17259 return;
17260 /* If there is no declarator, then the decl-specifier-seq should
17261 specify a type. */
17262 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17263 {
17264 /* If there was no decl-specifier-seq, and the next token is a
17265 `;', then we have something like:
17266
17267 struct S { ; };
17268
17269 [class.mem]
17270
17271 Each member-declaration shall declare at least one member
17272 name of the class. */
17273 if (!decl_specifiers.any_specifiers_p)
17274 {
17275 cp_token *token = cp_lexer_peek_token (parser->lexer);
17276 if (!in_system_header_at (token->location))
17277 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17278 }
17279 else
17280 {
17281 tree type;
17282
17283 /* See if this declaration is a friend. */
17284 friend_p = cp_parser_friend_p (&decl_specifiers);
17285 /* If there were decl-specifiers, check to see if there was
17286 a class-declaration. */
17287 type = check_tag_decl (&decl_specifiers);
17288 /* Nested classes have already been added to the class, but
17289 a `friend' needs to be explicitly registered. */
17290 if (friend_p)
17291 {
17292 /* If the `friend' keyword was present, the friend must
17293 be introduced with a class-key. */
17294 if (!declares_class_or_enum)
17295 error_at (decl_spec_token_start->location,
17296 "a class-key must be used when declaring a friend");
17297 /* In this case:
17298
17299 template <typename T> struct A {
17300 friend struct A<T>::B;
17301 };
17302
17303 A<T>::B will be represented by a TYPENAME_TYPE, and
17304 therefore not recognized by check_tag_decl. */
17305 if (!type
17306 && decl_specifiers.type
17307 && TYPE_P (decl_specifiers.type))
17308 type = decl_specifiers.type;
17309 if (!type || !TYPE_P (type))
17310 error_at (decl_spec_token_start->location,
17311 "friend declaration does not name a class or "
17312 "function");
17313 else
17314 make_friend_class (current_class_type, type,
17315 /*complain=*/true);
17316 }
17317 /* If there is no TYPE, an error message will already have
17318 been issued. */
17319 else if (!type || type == error_mark_node)
17320 ;
17321 /* An anonymous aggregate has to be handled specially; such
17322 a declaration really declares a data member (with a
17323 particular type), as opposed to a nested class. */
17324 else if (ANON_AGGR_TYPE_P (type))
17325 {
17326 /* Remove constructors and such from TYPE, now that we
17327 know it is an anonymous aggregate. */
17328 fixup_anonymous_aggr (type);
17329 /* And make the corresponding data member. */
17330 decl = build_decl (decl_spec_token_start->location,
17331 FIELD_DECL, NULL_TREE, type);
17332 /* Add it to the class. */
17333 finish_member_declaration (decl);
17334 }
17335 else
17336 cp_parser_check_access_in_redeclaration
17337 (TYPE_NAME (type),
17338 decl_spec_token_start->location);
17339 }
17340 }
17341 else
17342 {
17343 /* See if these declarations will be friends. */
17344 friend_p = cp_parser_friend_p (&decl_specifiers);
17345
17346 /* Keep going until we hit the `;' at the end of the
17347 declaration. */
17348 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17349 {
17350 tree attributes = NULL_TREE;
17351 tree first_attribute;
17352
17353 /* Peek at the next token. */
17354 token = cp_lexer_peek_token (parser->lexer);
17355
17356 /* Check for a bitfield declaration. */
17357 if (token->type == CPP_COLON
17358 || (token->type == CPP_NAME
17359 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17360 == CPP_COLON))
17361 {
17362 tree identifier;
17363 tree width;
17364
17365 /* Get the name of the bitfield. Note that we cannot just
17366 check TOKEN here because it may have been invalidated by
17367 the call to cp_lexer_peek_nth_token above. */
17368 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17369 identifier = cp_parser_identifier (parser);
17370 else
17371 identifier = NULL_TREE;
17372
17373 /* Consume the `:' token. */
17374 cp_lexer_consume_token (parser->lexer);
17375 /* Get the width of the bitfield. */
17376 width
17377 = cp_parser_constant_expression (parser,
17378 /*allow_non_constant=*/false,
17379 NULL);
17380
17381 /* Look for attributes that apply to the bitfield. */
17382 attributes = cp_parser_attributes_opt (parser);
17383 /* Remember which attributes are prefix attributes and
17384 which are not. */
17385 first_attribute = attributes;
17386 /* Combine the attributes. */
17387 attributes = chainon (prefix_attributes, attributes);
17388
17389 /* Create the bitfield declaration. */
17390 decl = grokbitfield (identifier
17391 ? make_id_declarator (NULL_TREE,
17392 identifier,
17393 sfk_none)
17394 : NULL,
17395 &decl_specifiers,
17396 width,
17397 attributes);
17398 }
17399 else
17400 {
17401 cp_declarator *declarator;
17402 tree initializer;
17403 tree asm_specification;
17404 int ctor_dtor_or_conv_p;
17405
17406 /* Parse the declarator. */
17407 declarator
17408 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17409 &ctor_dtor_or_conv_p,
17410 /*parenthesized_p=*/NULL,
17411 /*member_p=*/true);
17412
17413 /* If something went wrong parsing the declarator, make sure
17414 that we at least consume some tokens. */
17415 if (declarator == cp_error_declarator)
17416 {
17417 /* Skip to the end of the statement. */
17418 cp_parser_skip_to_end_of_statement (parser);
17419 /* If the next token is not a semicolon, that is
17420 probably because we just skipped over the body of
17421 a function. So, we consume a semicolon if
17422 present, but do not issue an error message if it
17423 is not present. */
17424 if (cp_lexer_next_token_is (parser->lexer,
17425 CPP_SEMICOLON))
17426 cp_lexer_consume_token (parser->lexer);
17427 return;
17428 }
17429
17430 if (declares_class_or_enum & 2)
17431 cp_parser_check_for_definition_in_return_type
17432 (declarator, decl_specifiers.type,
17433 decl_specifiers.type_location);
17434
17435 /* Look for an asm-specification. */
17436 asm_specification = cp_parser_asm_specification_opt (parser);
17437 /* Look for attributes that apply to the declaration. */
17438 attributes = cp_parser_attributes_opt (parser);
17439 /* Remember which attributes are prefix attributes and
17440 which are not. */
17441 first_attribute = attributes;
17442 /* Combine the attributes. */
17443 attributes = chainon (prefix_attributes, attributes);
17444
17445 /* If it's an `=', then we have a constant-initializer or a
17446 pure-specifier. It is not correct to parse the
17447 initializer before registering the member declaration
17448 since the member declaration should be in scope while
17449 its initializer is processed. However, the rest of the
17450 front end does not yet provide an interface that allows
17451 us to handle this correctly. */
17452 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17453 {
17454 /* In [class.mem]:
17455
17456 A pure-specifier shall be used only in the declaration of
17457 a virtual function.
17458
17459 A member-declarator can contain a constant-initializer
17460 only if it declares a static member of integral or
17461 enumeration type.
17462
17463 Therefore, if the DECLARATOR is for a function, we look
17464 for a pure-specifier; otherwise, we look for a
17465 constant-initializer. When we call `grokfield', it will
17466 perform more stringent semantics checks. */
17467 initializer_token_start = cp_lexer_peek_token (parser->lexer);
17468 if (function_declarator_p (declarator))
17469 initializer = cp_parser_pure_specifier (parser);
17470 else
17471 /* Parse the initializer. */
17472 initializer = cp_parser_constant_initializer (parser);
17473 }
17474 /* Otherwise, there is no initializer. */
17475 else
17476 initializer = NULL_TREE;
17477
17478 /* See if we are probably looking at a function
17479 definition. We are certainly not looking at a
17480 member-declarator. Calling `grokfield' has
17481 side-effects, so we must not do it unless we are sure
17482 that we are looking at a member-declarator. */
17483 if (cp_parser_token_starts_function_definition_p
17484 (cp_lexer_peek_token (parser->lexer)))
17485 {
17486 /* The grammar does not allow a pure-specifier to be
17487 used when a member function is defined. (It is
17488 possible that this fact is an oversight in the
17489 standard, since a pure function may be defined
17490 outside of the class-specifier. */
17491 if (initializer)
17492 error_at (initializer_token_start->location,
17493 "pure-specifier on function-definition");
17494 decl = cp_parser_save_member_function_body (parser,
17495 &decl_specifiers,
17496 declarator,
17497 attributes);
17498 /* If the member was not a friend, declare it here. */
17499 if (!friend_p)
17500 finish_member_declaration (decl);
17501 /* Peek at the next token. */
17502 token = cp_lexer_peek_token (parser->lexer);
17503 /* If the next token is a semicolon, consume it. */
17504 if (token->type == CPP_SEMICOLON)
17505 cp_lexer_consume_token (parser->lexer);
17506 return;
17507 }
17508 else
17509 if (declarator->kind == cdk_function)
17510 declarator->id_loc = token->location;
17511 /* Create the declaration. */
17512 decl = grokfield (declarator, &decl_specifiers,
17513 initializer, /*init_const_expr_p=*/true,
17514 asm_specification,
17515 attributes);
17516 }
17517
17518 /* Reset PREFIX_ATTRIBUTES. */
17519 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17520 attributes = TREE_CHAIN (attributes);
17521 if (attributes)
17522 TREE_CHAIN (attributes) = NULL_TREE;
17523
17524 /* If there is any qualification still in effect, clear it
17525 now; we will be starting fresh with the next declarator. */
17526 parser->scope = NULL_TREE;
17527 parser->qualifying_scope = NULL_TREE;
17528 parser->object_scope = NULL_TREE;
17529 /* If it's a `,', then there are more declarators. */
17530 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17531 cp_lexer_consume_token (parser->lexer);
17532 /* If the next token isn't a `;', then we have a parse error. */
17533 else if (cp_lexer_next_token_is_not (parser->lexer,
17534 CPP_SEMICOLON))
17535 {
17536 cp_parser_error (parser, "expected %<;%>");
17537 /* Skip tokens until we find a `;'. */
17538 cp_parser_skip_to_end_of_statement (parser);
17539
17540 break;
17541 }
17542
17543 if (decl)
17544 {
17545 /* Add DECL to the list of members. */
17546 if (!friend_p)
17547 finish_member_declaration (decl);
17548
17549 if (TREE_CODE (decl) == FUNCTION_DECL)
17550 cp_parser_save_default_args (parser, decl);
17551 }
17552 }
17553 }
17554
17555 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17556 }
17557
17558 /* Parse a pure-specifier.
17559
17560 pure-specifier:
17561 = 0
17562
17563 Returns INTEGER_ZERO_NODE if a pure specifier is found.
17564 Otherwise, ERROR_MARK_NODE is returned. */
17565
17566 static tree
17567 cp_parser_pure_specifier (cp_parser* parser)
17568 {
17569 cp_token *token;
17570
17571 /* Look for the `=' token. */
17572 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17573 return error_mark_node;
17574 /* Look for the `0' token. */
17575 token = cp_lexer_peek_token (parser->lexer);
17576
17577 if (token->type == CPP_EOF
17578 || token->type == CPP_PRAGMA_EOL)
17579 return error_mark_node;
17580
17581 cp_lexer_consume_token (parser->lexer);
17582
17583 /* Accept = default or = delete in c++0x mode. */
17584 if (token->keyword == RID_DEFAULT
17585 || token->keyword == RID_DELETE)
17586 {
17587 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17588 return token->u.value;
17589 }
17590
17591 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
17592 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
17593 {
17594 cp_parser_error (parser,
17595 "invalid pure specifier (only %<= 0%> is allowed)");
17596 cp_parser_skip_to_end_of_statement (parser);
17597 return error_mark_node;
17598 }
17599 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17600 {
17601 error_at (token->location, "templates may not be %<virtual%>");
17602 return error_mark_node;
17603 }
17604
17605 return integer_zero_node;
17606 }
17607
17608 /* Parse a constant-initializer.
17609
17610 constant-initializer:
17611 = constant-expression
17612
17613 Returns a representation of the constant-expression. */
17614
17615 static tree
17616 cp_parser_constant_initializer (cp_parser* parser)
17617 {
17618 /* Look for the `=' token. */
17619 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17620 return error_mark_node;
17621
17622 /* It is invalid to write:
17623
17624 struct S { static const int i = { 7 }; };
17625
17626 */
17627 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17628 {
17629 cp_parser_error (parser,
17630 "a brace-enclosed initializer is not allowed here");
17631 /* Consume the opening brace. */
17632 cp_lexer_consume_token (parser->lexer);
17633 /* Skip the initializer. */
17634 cp_parser_skip_to_closing_brace (parser);
17635 /* Look for the trailing `}'. */
17636 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17637
17638 return error_mark_node;
17639 }
17640
17641 return cp_parser_constant_expression (parser,
17642 /*allow_non_constant=*/false,
17643 NULL);
17644 }
17645
17646 /* Derived classes [gram.class.derived] */
17647
17648 /* Parse a base-clause.
17649
17650 base-clause:
17651 : base-specifier-list
17652
17653 base-specifier-list:
17654 base-specifier ... [opt]
17655 base-specifier-list , base-specifier ... [opt]
17656
17657 Returns a TREE_LIST representing the base-classes, in the order in
17658 which they were declared. The representation of each node is as
17659 described by cp_parser_base_specifier.
17660
17661 In the case that no bases are specified, this function will return
17662 NULL_TREE, not ERROR_MARK_NODE. */
17663
17664 static tree
17665 cp_parser_base_clause (cp_parser* parser)
17666 {
17667 tree bases = NULL_TREE;
17668
17669 /* Look for the `:' that begins the list. */
17670 cp_parser_require (parser, CPP_COLON, RT_COLON);
17671
17672 /* Scan the base-specifier-list. */
17673 while (true)
17674 {
17675 cp_token *token;
17676 tree base;
17677 bool pack_expansion_p = false;
17678
17679 /* Look for the base-specifier. */
17680 base = cp_parser_base_specifier (parser);
17681 /* Look for the (optional) ellipsis. */
17682 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17683 {
17684 /* Consume the `...'. */
17685 cp_lexer_consume_token (parser->lexer);
17686
17687 pack_expansion_p = true;
17688 }
17689
17690 /* Add BASE to the front of the list. */
17691 if (base != error_mark_node)
17692 {
17693 if (pack_expansion_p)
17694 /* Make this a pack expansion type. */
17695 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17696
17697
17698 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17699 {
17700 TREE_CHAIN (base) = bases;
17701 bases = base;
17702 }
17703 }
17704 /* Peek at the next token. */
17705 token = cp_lexer_peek_token (parser->lexer);
17706 /* If it's not a comma, then the list is complete. */
17707 if (token->type != CPP_COMMA)
17708 break;
17709 /* Consume the `,'. */
17710 cp_lexer_consume_token (parser->lexer);
17711 }
17712
17713 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17714 base class had a qualified name. However, the next name that
17715 appears is certainly not qualified. */
17716 parser->scope = NULL_TREE;
17717 parser->qualifying_scope = NULL_TREE;
17718 parser->object_scope = NULL_TREE;
17719
17720 return nreverse (bases);
17721 }
17722
17723 /* Parse a base-specifier.
17724
17725 base-specifier:
17726 :: [opt] nested-name-specifier [opt] class-name
17727 virtual access-specifier [opt] :: [opt] nested-name-specifier
17728 [opt] class-name
17729 access-specifier virtual [opt] :: [opt] nested-name-specifier
17730 [opt] class-name
17731
17732 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17733 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17734 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17735 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17736
17737 static tree
17738 cp_parser_base_specifier (cp_parser* parser)
17739 {
17740 cp_token *token;
17741 bool done = false;
17742 bool virtual_p = false;
17743 bool duplicate_virtual_error_issued_p = false;
17744 bool duplicate_access_error_issued_p = false;
17745 bool class_scope_p, template_p;
17746 tree access = access_default_node;
17747 tree type;
17748
17749 /* Process the optional `virtual' and `access-specifier'. */
17750 while (!done)
17751 {
17752 /* Peek at the next token. */
17753 token = cp_lexer_peek_token (parser->lexer);
17754 /* Process `virtual'. */
17755 switch (token->keyword)
17756 {
17757 case RID_VIRTUAL:
17758 /* If `virtual' appears more than once, issue an error. */
17759 if (virtual_p && !duplicate_virtual_error_issued_p)
17760 {
17761 cp_parser_error (parser,
17762 "%<virtual%> specified more than once in base-specified");
17763 duplicate_virtual_error_issued_p = true;
17764 }
17765
17766 virtual_p = true;
17767
17768 /* Consume the `virtual' token. */
17769 cp_lexer_consume_token (parser->lexer);
17770
17771 break;
17772
17773 case RID_PUBLIC:
17774 case RID_PROTECTED:
17775 case RID_PRIVATE:
17776 /* If more than one access specifier appears, issue an
17777 error. */
17778 if (access != access_default_node
17779 && !duplicate_access_error_issued_p)
17780 {
17781 cp_parser_error (parser,
17782 "more than one access specifier in base-specified");
17783 duplicate_access_error_issued_p = true;
17784 }
17785
17786 access = ridpointers[(int) token->keyword];
17787
17788 /* Consume the access-specifier. */
17789 cp_lexer_consume_token (parser->lexer);
17790
17791 break;
17792
17793 default:
17794 done = true;
17795 break;
17796 }
17797 }
17798 /* It is not uncommon to see programs mechanically, erroneously, use
17799 the 'typename' keyword to denote (dependent) qualified types
17800 as base classes. */
17801 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17802 {
17803 token = cp_lexer_peek_token (parser->lexer);
17804 if (!processing_template_decl)
17805 error_at (token->location,
17806 "keyword %<typename%> not allowed outside of templates");
17807 else
17808 error_at (token->location,
17809 "keyword %<typename%> not allowed in this context "
17810 "(the base class is implicitly a type)");
17811 cp_lexer_consume_token (parser->lexer);
17812 }
17813
17814 /* Look for the optional `::' operator. */
17815 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17816 /* Look for the nested-name-specifier. The simplest way to
17817 implement:
17818
17819 [temp.res]
17820
17821 The keyword `typename' is not permitted in a base-specifier or
17822 mem-initializer; in these contexts a qualified name that
17823 depends on a template-parameter is implicitly assumed to be a
17824 type name.
17825
17826 is to pretend that we have seen the `typename' keyword at this
17827 point. */
17828 cp_parser_nested_name_specifier_opt (parser,
17829 /*typename_keyword_p=*/true,
17830 /*check_dependency_p=*/true,
17831 typename_type,
17832 /*is_declaration=*/true);
17833 /* If the base class is given by a qualified name, assume that names
17834 we see are type names or templates, as appropriate. */
17835 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17836 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17837
17838 /* Finally, look for the class-name. */
17839 type = cp_parser_class_name (parser,
17840 class_scope_p,
17841 template_p,
17842 typename_type,
17843 /*check_dependency_p=*/true,
17844 /*class_head_p=*/false,
17845 /*is_declaration=*/true);
17846
17847 if (type == error_mark_node)
17848 return error_mark_node;
17849
17850 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17851 }
17852
17853 /* Exception handling [gram.exception] */
17854
17855 /* Parse an (optional) exception-specification.
17856
17857 exception-specification:
17858 throw ( type-id-list [opt] )
17859
17860 Returns a TREE_LIST representing the exception-specification. The
17861 TREE_VALUE of each node is a type. */
17862
17863 static tree
17864 cp_parser_exception_specification_opt (cp_parser* parser)
17865 {
17866 cp_token *token;
17867 tree type_id_list;
17868 const char *saved_message;
17869
17870 /* Peek at the next token. */
17871 token = cp_lexer_peek_token (parser->lexer);
17872
17873 /* Is it a noexcept-specification? */
17874 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
17875 {
17876 tree expr;
17877 cp_lexer_consume_token (parser->lexer);
17878
17879 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
17880 {
17881 cp_lexer_consume_token (parser->lexer);
17882
17883 /* Types may not be defined in an exception-specification. */
17884 saved_message = parser->type_definition_forbidden_message;
17885 parser->type_definition_forbidden_message
17886 = G_("types may not be defined in an exception-specification");
17887
17888 expr = cp_parser_constant_expression (parser, false, NULL);
17889
17890 /* Restore the saved message. */
17891 parser->type_definition_forbidden_message = saved_message;
17892
17893 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17894 }
17895 else
17896 expr = boolean_true_node;
17897
17898 return build_noexcept_spec (expr, tf_warning_or_error);
17899 }
17900
17901 /* If it's not `throw', then there's no exception-specification. */
17902 if (!cp_parser_is_keyword (token, RID_THROW))
17903 return NULL_TREE;
17904
17905 #if 0
17906 /* Enable this once a lot of code has transitioned to noexcept? */
17907 if (cxx_dialect == cxx0x && !in_system_header)
17908 warning (OPT_Wdeprecated, "dynamic exception specifications are "
17909 "deprecated in C++0x; use %<noexcept%> instead.");
17910 #endif
17911
17912 /* Consume the `throw'. */
17913 cp_lexer_consume_token (parser->lexer);
17914
17915 /* Look for the `('. */
17916 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17917
17918 /* Peek at the next token. */
17919 token = cp_lexer_peek_token (parser->lexer);
17920 /* If it's not a `)', then there is a type-id-list. */
17921 if (token->type != CPP_CLOSE_PAREN)
17922 {
17923 /* Types may not be defined in an exception-specification. */
17924 saved_message = parser->type_definition_forbidden_message;
17925 parser->type_definition_forbidden_message
17926 = G_("types may not be defined in an exception-specification");
17927 /* Parse the type-id-list. */
17928 type_id_list = cp_parser_type_id_list (parser);
17929 /* Restore the saved message. */
17930 parser->type_definition_forbidden_message = saved_message;
17931 }
17932 else
17933 type_id_list = empty_except_spec;
17934
17935 /* Look for the `)'. */
17936 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17937
17938 return type_id_list;
17939 }
17940
17941 /* Parse an (optional) type-id-list.
17942
17943 type-id-list:
17944 type-id ... [opt]
17945 type-id-list , type-id ... [opt]
17946
17947 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17948 in the order that the types were presented. */
17949
17950 static tree
17951 cp_parser_type_id_list (cp_parser* parser)
17952 {
17953 tree types = NULL_TREE;
17954
17955 while (true)
17956 {
17957 cp_token *token;
17958 tree type;
17959
17960 /* Get the next type-id. */
17961 type = cp_parser_type_id (parser);
17962 /* Parse the optional ellipsis. */
17963 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17964 {
17965 /* Consume the `...'. */
17966 cp_lexer_consume_token (parser->lexer);
17967
17968 /* Turn the type into a pack expansion expression. */
17969 type = make_pack_expansion (type);
17970 }
17971 /* Add it to the list. */
17972 types = add_exception_specifier (types, type, /*complain=*/1);
17973 /* Peek at the next token. */
17974 token = cp_lexer_peek_token (parser->lexer);
17975 /* If it is not a `,', we are done. */
17976 if (token->type != CPP_COMMA)
17977 break;
17978 /* Consume the `,'. */
17979 cp_lexer_consume_token (parser->lexer);
17980 }
17981
17982 return nreverse (types);
17983 }
17984
17985 /* Parse a try-block.
17986
17987 try-block:
17988 try compound-statement handler-seq */
17989
17990 static tree
17991 cp_parser_try_block (cp_parser* parser)
17992 {
17993 tree try_block;
17994
17995 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
17996 try_block = begin_try_block ();
17997 cp_parser_compound_statement (parser, NULL, true);
17998 finish_try_block (try_block);
17999 cp_parser_handler_seq (parser);
18000 finish_handler_sequence (try_block);
18001
18002 return try_block;
18003 }
18004
18005 /* Parse a function-try-block.
18006
18007 function-try-block:
18008 try ctor-initializer [opt] function-body handler-seq */
18009
18010 static bool
18011 cp_parser_function_try_block (cp_parser* parser)
18012 {
18013 tree compound_stmt;
18014 tree try_block;
18015 bool ctor_initializer_p;
18016
18017 /* Look for the `try' keyword. */
18018 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18019 return false;
18020 /* Let the rest of the front end know where we are. */
18021 try_block = begin_function_try_block (&compound_stmt);
18022 /* Parse the function-body. */
18023 ctor_initializer_p
18024 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18025 /* We're done with the `try' part. */
18026 finish_function_try_block (try_block);
18027 /* Parse the handlers. */
18028 cp_parser_handler_seq (parser);
18029 /* We're done with the handlers. */
18030 finish_function_handler_sequence (try_block, compound_stmt);
18031
18032 return ctor_initializer_p;
18033 }
18034
18035 /* Parse a handler-seq.
18036
18037 handler-seq:
18038 handler handler-seq [opt] */
18039
18040 static void
18041 cp_parser_handler_seq (cp_parser* parser)
18042 {
18043 while (true)
18044 {
18045 cp_token *token;
18046
18047 /* Parse the handler. */
18048 cp_parser_handler (parser);
18049 /* Peek at the next token. */
18050 token = cp_lexer_peek_token (parser->lexer);
18051 /* If it's not `catch' then there are no more handlers. */
18052 if (!cp_parser_is_keyword (token, RID_CATCH))
18053 break;
18054 }
18055 }
18056
18057 /* Parse a handler.
18058
18059 handler:
18060 catch ( exception-declaration ) compound-statement */
18061
18062 static void
18063 cp_parser_handler (cp_parser* parser)
18064 {
18065 tree handler;
18066 tree declaration;
18067
18068 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18069 handler = begin_handler ();
18070 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18071 declaration = cp_parser_exception_declaration (parser);
18072 finish_handler_parms (declaration, handler);
18073 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18074 cp_parser_compound_statement (parser, NULL, false);
18075 finish_handler (handler);
18076 }
18077
18078 /* Parse an exception-declaration.
18079
18080 exception-declaration:
18081 type-specifier-seq declarator
18082 type-specifier-seq abstract-declarator
18083 type-specifier-seq
18084 ...
18085
18086 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18087 ellipsis variant is used. */
18088
18089 static tree
18090 cp_parser_exception_declaration (cp_parser* parser)
18091 {
18092 cp_decl_specifier_seq type_specifiers;
18093 cp_declarator *declarator;
18094 const char *saved_message;
18095
18096 /* If it's an ellipsis, it's easy to handle. */
18097 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18098 {
18099 /* Consume the `...' token. */
18100 cp_lexer_consume_token (parser->lexer);
18101 return NULL_TREE;
18102 }
18103
18104 /* Types may not be defined in exception-declarations. */
18105 saved_message = parser->type_definition_forbidden_message;
18106 parser->type_definition_forbidden_message
18107 = G_("types may not be defined in exception-declarations");
18108
18109 /* Parse the type-specifier-seq. */
18110 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18111 /*is_trailing_return=*/false,
18112 &type_specifiers);
18113 /* If it's a `)', then there is no declarator. */
18114 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18115 declarator = NULL;
18116 else
18117 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18118 /*ctor_dtor_or_conv_p=*/NULL,
18119 /*parenthesized_p=*/NULL,
18120 /*member_p=*/false);
18121
18122 /* Restore the saved message. */
18123 parser->type_definition_forbidden_message = saved_message;
18124
18125 if (!type_specifiers.any_specifiers_p)
18126 return error_mark_node;
18127
18128 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18129 }
18130
18131 /* Parse a throw-expression.
18132
18133 throw-expression:
18134 throw assignment-expression [opt]
18135
18136 Returns a THROW_EXPR representing the throw-expression. */
18137
18138 static tree
18139 cp_parser_throw_expression (cp_parser* parser)
18140 {
18141 tree expression;
18142 cp_token* token;
18143
18144 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18145 token = cp_lexer_peek_token (parser->lexer);
18146 /* Figure out whether or not there is an assignment-expression
18147 following the "throw" keyword. */
18148 if (token->type == CPP_COMMA
18149 || token->type == CPP_SEMICOLON
18150 || token->type == CPP_CLOSE_PAREN
18151 || token->type == CPP_CLOSE_SQUARE
18152 || token->type == CPP_CLOSE_BRACE
18153 || token->type == CPP_COLON)
18154 expression = NULL_TREE;
18155 else
18156 expression = cp_parser_assignment_expression (parser,
18157 /*cast_p=*/false, NULL);
18158
18159 return build_throw (expression);
18160 }
18161
18162 /* GNU Extensions */
18163
18164 /* Parse an (optional) asm-specification.
18165
18166 asm-specification:
18167 asm ( string-literal )
18168
18169 If the asm-specification is present, returns a STRING_CST
18170 corresponding to the string-literal. Otherwise, returns
18171 NULL_TREE. */
18172
18173 static tree
18174 cp_parser_asm_specification_opt (cp_parser* parser)
18175 {
18176 cp_token *token;
18177 tree asm_specification;
18178
18179 /* Peek at the next token. */
18180 token = cp_lexer_peek_token (parser->lexer);
18181 /* If the next token isn't the `asm' keyword, then there's no
18182 asm-specification. */
18183 if (!cp_parser_is_keyword (token, RID_ASM))
18184 return NULL_TREE;
18185
18186 /* Consume the `asm' token. */
18187 cp_lexer_consume_token (parser->lexer);
18188 /* Look for the `('. */
18189 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18190
18191 /* Look for the string-literal. */
18192 asm_specification = cp_parser_string_literal (parser, false, false);
18193
18194 /* Look for the `)'. */
18195 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18196
18197 return asm_specification;
18198 }
18199
18200 /* Parse an asm-operand-list.
18201
18202 asm-operand-list:
18203 asm-operand
18204 asm-operand-list , asm-operand
18205
18206 asm-operand:
18207 string-literal ( expression )
18208 [ string-literal ] string-literal ( expression )
18209
18210 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18211 each node is the expression. The TREE_PURPOSE is itself a
18212 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18213 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18214 is a STRING_CST for the string literal before the parenthesis. Returns
18215 ERROR_MARK_NODE if any of the operands are invalid. */
18216
18217 static tree
18218 cp_parser_asm_operand_list (cp_parser* parser)
18219 {
18220 tree asm_operands = NULL_TREE;
18221 bool invalid_operands = false;
18222
18223 while (true)
18224 {
18225 tree string_literal;
18226 tree expression;
18227 tree name;
18228
18229 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18230 {
18231 /* Consume the `[' token. */
18232 cp_lexer_consume_token (parser->lexer);
18233 /* Read the operand name. */
18234 name = cp_parser_identifier (parser);
18235 if (name != error_mark_node)
18236 name = build_string (IDENTIFIER_LENGTH (name),
18237 IDENTIFIER_POINTER (name));
18238 /* Look for the closing `]'. */
18239 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18240 }
18241 else
18242 name = NULL_TREE;
18243 /* Look for the string-literal. */
18244 string_literal = cp_parser_string_literal (parser, false, false);
18245
18246 /* Look for the `('. */
18247 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18248 /* Parse the expression. */
18249 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18250 /* Look for the `)'. */
18251 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18252
18253 if (name == error_mark_node
18254 || string_literal == error_mark_node
18255 || expression == error_mark_node)
18256 invalid_operands = true;
18257
18258 /* Add this operand to the list. */
18259 asm_operands = tree_cons (build_tree_list (name, string_literal),
18260 expression,
18261 asm_operands);
18262 /* If the next token is not a `,', there are no more
18263 operands. */
18264 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18265 break;
18266 /* Consume the `,'. */
18267 cp_lexer_consume_token (parser->lexer);
18268 }
18269
18270 return invalid_operands ? error_mark_node : nreverse (asm_operands);
18271 }
18272
18273 /* Parse an asm-clobber-list.
18274
18275 asm-clobber-list:
18276 string-literal
18277 asm-clobber-list , string-literal
18278
18279 Returns a TREE_LIST, indicating the clobbers in the order that they
18280 appeared. The TREE_VALUE of each node is a STRING_CST. */
18281
18282 static tree
18283 cp_parser_asm_clobber_list (cp_parser* parser)
18284 {
18285 tree clobbers = NULL_TREE;
18286
18287 while (true)
18288 {
18289 tree string_literal;
18290
18291 /* Look for the string literal. */
18292 string_literal = cp_parser_string_literal (parser, false, false);
18293 /* Add it to the list. */
18294 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18295 /* If the next token is not a `,', then the list is
18296 complete. */
18297 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18298 break;
18299 /* Consume the `,' token. */
18300 cp_lexer_consume_token (parser->lexer);
18301 }
18302
18303 return clobbers;
18304 }
18305
18306 /* Parse an asm-label-list.
18307
18308 asm-label-list:
18309 identifier
18310 asm-label-list , identifier
18311
18312 Returns a TREE_LIST, indicating the labels in the order that they
18313 appeared. The TREE_VALUE of each node is a label. */
18314
18315 static tree
18316 cp_parser_asm_label_list (cp_parser* parser)
18317 {
18318 tree labels = NULL_TREE;
18319
18320 while (true)
18321 {
18322 tree identifier, label, name;
18323
18324 /* Look for the identifier. */
18325 identifier = cp_parser_identifier (parser);
18326 if (!error_operand_p (identifier))
18327 {
18328 label = lookup_label (identifier);
18329 if (TREE_CODE (label) == LABEL_DECL)
18330 {
18331 TREE_USED (label) = 1;
18332 check_goto (label);
18333 name = build_string (IDENTIFIER_LENGTH (identifier),
18334 IDENTIFIER_POINTER (identifier));
18335 labels = tree_cons (name, label, labels);
18336 }
18337 }
18338 /* If the next token is not a `,', then the list is
18339 complete. */
18340 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18341 break;
18342 /* Consume the `,' token. */
18343 cp_lexer_consume_token (parser->lexer);
18344 }
18345
18346 return nreverse (labels);
18347 }
18348
18349 /* Parse an (optional) series of attributes.
18350
18351 attributes:
18352 attributes attribute
18353
18354 attribute:
18355 __attribute__ (( attribute-list [opt] ))
18356
18357 The return value is as for cp_parser_attribute_list. */
18358
18359 static tree
18360 cp_parser_attributes_opt (cp_parser* parser)
18361 {
18362 tree attributes = NULL_TREE;
18363
18364 while (true)
18365 {
18366 cp_token *token;
18367 tree attribute_list;
18368
18369 /* Peek at the next token. */
18370 token = cp_lexer_peek_token (parser->lexer);
18371 /* If it's not `__attribute__', then we're done. */
18372 if (token->keyword != RID_ATTRIBUTE)
18373 break;
18374
18375 /* Consume the `__attribute__' keyword. */
18376 cp_lexer_consume_token (parser->lexer);
18377 /* Look for the two `(' tokens. */
18378 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18379 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18380
18381 /* Peek at the next token. */
18382 token = cp_lexer_peek_token (parser->lexer);
18383 if (token->type != CPP_CLOSE_PAREN)
18384 /* Parse the attribute-list. */
18385 attribute_list = cp_parser_attribute_list (parser);
18386 else
18387 /* If the next token is a `)', then there is no attribute
18388 list. */
18389 attribute_list = NULL;
18390
18391 /* Look for the two `)' tokens. */
18392 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18393 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18394
18395 /* Add these new attributes to the list. */
18396 attributes = chainon (attributes, attribute_list);
18397 }
18398
18399 return attributes;
18400 }
18401
18402 /* Parse an attribute-list.
18403
18404 attribute-list:
18405 attribute
18406 attribute-list , attribute
18407
18408 attribute:
18409 identifier
18410 identifier ( identifier )
18411 identifier ( identifier , expression-list )
18412 identifier ( expression-list )
18413
18414 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18415 to an attribute. The TREE_PURPOSE of each node is the identifier
18416 indicating which attribute is in use. The TREE_VALUE represents
18417 the arguments, if any. */
18418
18419 static tree
18420 cp_parser_attribute_list (cp_parser* parser)
18421 {
18422 tree attribute_list = NULL_TREE;
18423 bool save_translate_strings_p = parser->translate_strings_p;
18424
18425 parser->translate_strings_p = false;
18426 while (true)
18427 {
18428 cp_token *token;
18429 tree identifier;
18430 tree attribute;
18431
18432 /* Look for the identifier. We also allow keywords here; for
18433 example `__attribute__ ((const))' is legal. */
18434 token = cp_lexer_peek_token (parser->lexer);
18435 if (token->type == CPP_NAME
18436 || token->type == CPP_KEYWORD)
18437 {
18438 tree arguments = NULL_TREE;
18439
18440 /* Consume the token. */
18441 token = cp_lexer_consume_token (parser->lexer);
18442
18443 /* Save away the identifier that indicates which attribute
18444 this is. */
18445 identifier = (token->type == CPP_KEYWORD)
18446 /* For keywords, use the canonical spelling, not the
18447 parsed identifier. */
18448 ? ridpointers[(int) token->keyword]
18449 : token->u.value;
18450
18451 attribute = build_tree_list (identifier, NULL_TREE);
18452
18453 /* Peek at the next token. */
18454 token = cp_lexer_peek_token (parser->lexer);
18455 /* If it's an `(', then parse the attribute arguments. */
18456 if (token->type == CPP_OPEN_PAREN)
18457 {
18458 VEC(tree,gc) *vec;
18459 int attr_flag = (attribute_takes_identifier_p (identifier)
18460 ? id_attr : normal_attr);
18461 vec = cp_parser_parenthesized_expression_list
18462 (parser, attr_flag, /*cast_p=*/false,
18463 /*allow_expansion_p=*/false,
18464 /*non_constant_p=*/NULL);
18465 if (vec == NULL)
18466 arguments = error_mark_node;
18467 else
18468 {
18469 arguments = build_tree_list_vec (vec);
18470 release_tree_vector (vec);
18471 }
18472 /* Save the arguments away. */
18473 TREE_VALUE (attribute) = arguments;
18474 }
18475
18476 if (arguments != error_mark_node)
18477 {
18478 /* Add this attribute to the list. */
18479 TREE_CHAIN (attribute) = attribute_list;
18480 attribute_list = attribute;
18481 }
18482
18483 token = cp_lexer_peek_token (parser->lexer);
18484 }
18485 /* Now, look for more attributes. If the next token isn't a
18486 `,', we're done. */
18487 if (token->type != CPP_COMMA)
18488 break;
18489
18490 /* Consume the comma and keep going. */
18491 cp_lexer_consume_token (parser->lexer);
18492 }
18493 parser->translate_strings_p = save_translate_strings_p;
18494
18495 /* We built up the list in reverse order. */
18496 return nreverse (attribute_list);
18497 }
18498
18499 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18500 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18501 current value of the PEDANTIC flag, regardless of whether or not
18502 the `__extension__' keyword is present. The caller is responsible
18503 for restoring the value of the PEDANTIC flag. */
18504
18505 static bool
18506 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18507 {
18508 /* Save the old value of the PEDANTIC flag. */
18509 *saved_pedantic = pedantic;
18510
18511 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18512 {
18513 /* Consume the `__extension__' token. */
18514 cp_lexer_consume_token (parser->lexer);
18515 /* We're not being pedantic while the `__extension__' keyword is
18516 in effect. */
18517 pedantic = 0;
18518
18519 return true;
18520 }
18521
18522 return false;
18523 }
18524
18525 /* Parse a label declaration.
18526
18527 label-declaration:
18528 __label__ label-declarator-seq ;
18529
18530 label-declarator-seq:
18531 identifier , label-declarator-seq
18532 identifier */
18533
18534 static void
18535 cp_parser_label_declaration (cp_parser* parser)
18536 {
18537 /* Look for the `__label__' keyword. */
18538 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18539
18540 while (true)
18541 {
18542 tree identifier;
18543
18544 /* Look for an identifier. */
18545 identifier = cp_parser_identifier (parser);
18546 /* If we failed, stop. */
18547 if (identifier == error_mark_node)
18548 break;
18549 /* Declare it as a label. */
18550 finish_label_decl (identifier);
18551 /* If the next token is a `;', stop. */
18552 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18553 break;
18554 /* Look for the `,' separating the label declarations. */
18555 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18556 }
18557
18558 /* Look for the final `;'. */
18559 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18560 }
18561
18562 /* Support Functions */
18563
18564 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18565 NAME should have one of the representations used for an
18566 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18567 is returned. If PARSER->SCOPE is a dependent type, then a
18568 SCOPE_REF is returned.
18569
18570 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18571 returned; the name was already resolved when the TEMPLATE_ID_EXPR
18572 was formed. Abstractly, such entities should not be passed to this
18573 function, because they do not need to be looked up, but it is
18574 simpler to check for this special case here, rather than at the
18575 call-sites.
18576
18577 In cases not explicitly covered above, this function returns a
18578 DECL, OVERLOAD, or baselink representing the result of the lookup.
18579 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18580 is returned.
18581
18582 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18583 (e.g., "struct") that was used. In that case bindings that do not
18584 refer to types are ignored.
18585
18586 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18587 ignored.
18588
18589 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18590 are ignored.
18591
18592 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18593 types.
18594
18595 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18596 TREE_LIST of candidates if name-lookup results in an ambiguity, and
18597 NULL_TREE otherwise. */
18598
18599 static tree
18600 cp_parser_lookup_name (cp_parser *parser, tree name,
18601 enum tag_types tag_type,
18602 bool is_template,
18603 bool is_namespace,
18604 bool check_dependency,
18605 tree *ambiguous_decls,
18606 location_t name_location)
18607 {
18608 int flags = 0;
18609 tree decl;
18610 tree object_type = parser->context->object_type;
18611
18612 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18613 flags |= LOOKUP_COMPLAIN;
18614
18615 /* Assume that the lookup will be unambiguous. */
18616 if (ambiguous_decls)
18617 *ambiguous_decls = NULL_TREE;
18618
18619 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
18620 no longer valid. Note that if we are parsing tentatively, and
18621 the parse fails, OBJECT_TYPE will be automatically restored. */
18622 parser->context->object_type = NULL_TREE;
18623
18624 if (name == error_mark_node)
18625 return error_mark_node;
18626
18627 /* A template-id has already been resolved; there is no lookup to
18628 do. */
18629 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
18630 return name;
18631 if (BASELINK_P (name))
18632 {
18633 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
18634 == TEMPLATE_ID_EXPR);
18635 return name;
18636 }
18637
18638 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
18639 it should already have been checked to make sure that the name
18640 used matches the type being destroyed. */
18641 if (TREE_CODE (name) == BIT_NOT_EXPR)
18642 {
18643 tree type;
18644
18645 /* Figure out to which type this destructor applies. */
18646 if (parser->scope)
18647 type = parser->scope;
18648 else if (object_type)
18649 type = object_type;
18650 else
18651 type = current_class_type;
18652 /* If that's not a class type, there is no destructor. */
18653 if (!type || !CLASS_TYPE_P (type))
18654 return error_mark_node;
18655 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
18656 lazily_declare_fn (sfk_destructor, type);
18657 if (!CLASSTYPE_DESTRUCTORS (type))
18658 return error_mark_node;
18659 /* If it was a class type, return the destructor. */
18660 return CLASSTYPE_DESTRUCTORS (type);
18661 }
18662
18663 /* By this point, the NAME should be an ordinary identifier. If
18664 the id-expression was a qualified name, the qualifying scope is
18665 stored in PARSER->SCOPE at this point. */
18666 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
18667
18668 /* Perform the lookup. */
18669 if (parser->scope)
18670 {
18671 bool dependent_p;
18672
18673 if (parser->scope == error_mark_node)
18674 return error_mark_node;
18675
18676 /* If the SCOPE is dependent, the lookup must be deferred until
18677 the template is instantiated -- unless we are explicitly
18678 looking up names in uninstantiated templates. Even then, we
18679 cannot look up the name if the scope is not a class type; it
18680 might, for example, be a template type parameter. */
18681 dependent_p = (TYPE_P (parser->scope)
18682 && dependent_scope_p (parser->scope));
18683 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
18684 && dependent_p)
18685 /* Defer lookup. */
18686 decl = error_mark_node;
18687 else
18688 {
18689 tree pushed_scope = NULL_TREE;
18690
18691 /* If PARSER->SCOPE is a dependent type, then it must be a
18692 class type, and we must not be checking dependencies;
18693 otherwise, we would have processed this lookup above. So
18694 that PARSER->SCOPE is not considered a dependent base by
18695 lookup_member, we must enter the scope here. */
18696 if (dependent_p)
18697 pushed_scope = push_scope (parser->scope);
18698
18699 /* If the PARSER->SCOPE is a template specialization, it
18700 may be instantiated during name lookup. In that case,
18701 errors may be issued. Even if we rollback the current
18702 tentative parse, those errors are valid. */
18703 decl = lookup_qualified_name (parser->scope, name,
18704 tag_type != none_type,
18705 /*complain=*/true);
18706
18707 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18708 lookup result and the nested-name-specifier nominates a class C:
18709 * if the name specified after the nested-name-specifier, when
18710 looked up in C, is the injected-class-name of C (Clause 9), or
18711 * if the name specified after the nested-name-specifier is the
18712 same as the identifier or the simple-template-id's template-
18713 name in the last component of the nested-name-specifier,
18714 the name is instead considered to name the constructor of
18715 class C. [ Note: for example, the constructor is not an
18716 acceptable lookup result in an elaborated-type-specifier so
18717 the constructor would not be used in place of the
18718 injected-class-name. --end note ] Such a constructor name
18719 shall be used only in the declarator-id of a declaration that
18720 names a constructor or in a using-declaration. */
18721 if (tag_type == none_type
18722 && DECL_SELF_REFERENCE_P (decl)
18723 && same_type_p (DECL_CONTEXT (decl), parser->scope))
18724 decl = lookup_qualified_name (parser->scope, ctor_identifier,
18725 tag_type != none_type,
18726 /*complain=*/true);
18727
18728 /* If we have a single function from a using decl, pull it out. */
18729 if (TREE_CODE (decl) == OVERLOAD
18730 && !really_overloaded_fn (decl))
18731 decl = OVL_FUNCTION (decl);
18732
18733 if (pushed_scope)
18734 pop_scope (pushed_scope);
18735 }
18736
18737 /* If the scope is a dependent type and either we deferred lookup or
18738 we did lookup but didn't find the name, rememeber the name. */
18739 if (decl == error_mark_node && TYPE_P (parser->scope)
18740 && dependent_type_p (parser->scope))
18741 {
18742 if (tag_type)
18743 {
18744 tree type;
18745
18746 /* The resolution to Core Issue 180 says that `struct
18747 A::B' should be considered a type-name, even if `A'
18748 is dependent. */
18749 type = make_typename_type (parser->scope, name, tag_type,
18750 /*complain=*/tf_error);
18751 decl = TYPE_NAME (type);
18752 }
18753 else if (is_template
18754 && (cp_parser_next_token_ends_template_argument_p (parser)
18755 || cp_lexer_next_token_is (parser->lexer,
18756 CPP_CLOSE_PAREN)))
18757 decl = make_unbound_class_template (parser->scope,
18758 name, NULL_TREE,
18759 /*complain=*/tf_error);
18760 else
18761 decl = build_qualified_name (/*type=*/NULL_TREE,
18762 parser->scope, name,
18763 is_template);
18764 }
18765 parser->qualifying_scope = parser->scope;
18766 parser->object_scope = NULL_TREE;
18767 }
18768 else if (object_type)
18769 {
18770 tree object_decl = NULL_TREE;
18771 /* Look up the name in the scope of the OBJECT_TYPE, unless the
18772 OBJECT_TYPE is not a class. */
18773 if (CLASS_TYPE_P (object_type))
18774 /* If the OBJECT_TYPE is a template specialization, it may
18775 be instantiated during name lookup. In that case, errors
18776 may be issued. Even if we rollback the current tentative
18777 parse, those errors are valid. */
18778 object_decl = lookup_member (object_type,
18779 name,
18780 /*protect=*/0,
18781 tag_type != none_type);
18782 /* Look it up in the enclosing context, too. */
18783 decl = lookup_name_real (name, tag_type != none_type,
18784 /*nonclass=*/0,
18785 /*block_p=*/true, is_namespace, flags);
18786 parser->object_scope = object_type;
18787 parser->qualifying_scope = NULL_TREE;
18788 if (object_decl)
18789 decl = object_decl;
18790 }
18791 else
18792 {
18793 decl = lookup_name_real (name, tag_type != none_type,
18794 /*nonclass=*/0,
18795 /*block_p=*/true, is_namespace, flags);
18796 parser->qualifying_scope = NULL_TREE;
18797 parser->object_scope = NULL_TREE;
18798 }
18799
18800 /* If the lookup failed, let our caller know. */
18801 if (!decl || decl == error_mark_node)
18802 return error_mark_node;
18803
18804 /* Pull out the template from an injected-class-name (or multiple). */
18805 if (is_template)
18806 decl = maybe_get_template_decl_from_type_decl (decl);
18807
18808 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18809 if (TREE_CODE (decl) == TREE_LIST)
18810 {
18811 if (ambiguous_decls)
18812 *ambiguous_decls = decl;
18813 /* The error message we have to print is too complicated for
18814 cp_parser_error, so we incorporate its actions directly. */
18815 if (!cp_parser_simulate_error (parser))
18816 {
18817 error_at (name_location, "reference to %qD is ambiguous",
18818 name);
18819 print_candidates (decl);
18820 }
18821 return error_mark_node;
18822 }
18823
18824 gcc_assert (DECL_P (decl)
18825 || TREE_CODE (decl) == OVERLOAD
18826 || TREE_CODE (decl) == SCOPE_REF
18827 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18828 || BASELINK_P (decl));
18829
18830 /* If we have resolved the name of a member declaration, check to
18831 see if the declaration is accessible. When the name resolves to
18832 set of overloaded functions, accessibility is checked when
18833 overload resolution is done.
18834
18835 During an explicit instantiation, access is not checked at all,
18836 as per [temp.explicit]. */
18837 if (DECL_P (decl))
18838 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18839
18840 return decl;
18841 }
18842
18843 /* Like cp_parser_lookup_name, but for use in the typical case where
18844 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18845 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18846
18847 static tree
18848 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18849 {
18850 return cp_parser_lookup_name (parser, name,
18851 none_type,
18852 /*is_template=*/false,
18853 /*is_namespace=*/false,
18854 /*check_dependency=*/true,
18855 /*ambiguous_decls=*/NULL,
18856 location);
18857 }
18858
18859 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18860 the current context, return the TYPE_DECL. If TAG_NAME_P is
18861 true, the DECL indicates the class being defined in a class-head,
18862 or declared in an elaborated-type-specifier.
18863
18864 Otherwise, return DECL. */
18865
18866 static tree
18867 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18868 {
18869 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18870 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18871
18872 struct A {
18873 template <typename T> struct B;
18874 };
18875
18876 template <typename T> struct A::B {};
18877
18878 Similarly, in an elaborated-type-specifier:
18879
18880 namespace N { struct X{}; }
18881
18882 struct A {
18883 template <typename T> friend struct N::X;
18884 };
18885
18886 However, if the DECL refers to a class type, and we are in
18887 the scope of the class, then the name lookup automatically
18888 finds the TYPE_DECL created by build_self_reference rather
18889 than a TEMPLATE_DECL. For example, in:
18890
18891 template <class T> struct S {
18892 S s;
18893 };
18894
18895 there is no need to handle such case. */
18896
18897 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18898 return DECL_TEMPLATE_RESULT (decl);
18899
18900 return decl;
18901 }
18902
18903 /* If too many, or too few, template-parameter lists apply to the
18904 declarator, issue an error message. Returns TRUE if all went well,
18905 and FALSE otherwise. */
18906
18907 static bool
18908 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18909 cp_declarator *declarator,
18910 location_t declarator_location)
18911 {
18912 unsigned num_templates;
18913
18914 /* We haven't seen any classes that involve template parameters yet. */
18915 num_templates = 0;
18916
18917 switch (declarator->kind)
18918 {
18919 case cdk_id:
18920 if (declarator->u.id.qualifying_scope)
18921 {
18922 tree scope;
18923
18924 scope = declarator->u.id.qualifying_scope;
18925
18926 while (scope && CLASS_TYPE_P (scope))
18927 {
18928 /* You're supposed to have one `template <...>'
18929 for every template class, but you don't need one
18930 for a full specialization. For example:
18931
18932 template <class T> struct S{};
18933 template <> struct S<int> { void f(); };
18934 void S<int>::f () {}
18935
18936 is correct; there shouldn't be a `template <>' for
18937 the definition of `S<int>::f'. */
18938 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18939 /* If SCOPE does not have template information of any
18940 kind, then it is not a template, nor is it nested
18941 within a template. */
18942 break;
18943 if (explicit_class_specialization_p (scope))
18944 break;
18945 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18946 ++num_templates;
18947
18948 scope = TYPE_CONTEXT (scope);
18949 }
18950 }
18951 else if (TREE_CODE (declarator->u.id.unqualified_name)
18952 == TEMPLATE_ID_EXPR)
18953 /* If the DECLARATOR has the form `X<y>' then it uses one
18954 additional level of template parameters. */
18955 ++num_templates;
18956
18957 return cp_parser_check_template_parameters
18958 (parser, num_templates, declarator_location, declarator);
18959
18960
18961 case cdk_function:
18962 case cdk_array:
18963 case cdk_pointer:
18964 case cdk_reference:
18965 case cdk_ptrmem:
18966 return (cp_parser_check_declarator_template_parameters
18967 (parser, declarator->declarator, declarator_location));
18968
18969 case cdk_error:
18970 return true;
18971
18972 default:
18973 gcc_unreachable ();
18974 }
18975 return false;
18976 }
18977
18978 /* NUM_TEMPLATES were used in the current declaration. If that is
18979 invalid, return FALSE and issue an error messages. Otherwise,
18980 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18981 declarator and we can print more accurate diagnostics. */
18982
18983 static bool
18984 cp_parser_check_template_parameters (cp_parser* parser,
18985 unsigned num_templates,
18986 location_t location,
18987 cp_declarator *declarator)
18988 {
18989 /* If there are the same number of template classes and parameter
18990 lists, that's OK. */
18991 if (parser->num_template_parameter_lists == num_templates)
18992 return true;
18993 /* If there are more, but only one more, then we are referring to a
18994 member template. That's OK too. */
18995 if (parser->num_template_parameter_lists == num_templates + 1)
18996 return true;
18997 /* If there are more template classes than parameter lists, we have
18998 something like:
18999
19000 template <class T> void S<T>::R<T>::f (); */
19001 if (parser->num_template_parameter_lists < num_templates)
19002 {
19003 if (declarator && !current_function_decl)
19004 error_at (location, "specializing member %<%T::%E%> "
19005 "requires %<template<>%> syntax",
19006 declarator->u.id.qualifying_scope,
19007 declarator->u.id.unqualified_name);
19008 else if (declarator)
19009 error_at (location, "invalid declaration of %<%T::%E%>",
19010 declarator->u.id.qualifying_scope,
19011 declarator->u.id.unqualified_name);
19012 else
19013 error_at (location, "too few template-parameter-lists");
19014 return false;
19015 }
19016 /* Otherwise, there are too many template parameter lists. We have
19017 something like:
19018
19019 template <class T> template <class U> void S::f(); */
19020 error_at (location, "too many template-parameter-lists");
19021 return false;
19022 }
19023
19024 /* Parse an optional `::' token indicating that the following name is
19025 from the global namespace. If so, PARSER->SCOPE is set to the
19026 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19027 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19028 Returns the new value of PARSER->SCOPE, if the `::' token is
19029 present, and NULL_TREE otherwise. */
19030
19031 static tree
19032 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19033 {
19034 cp_token *token;
19035
19036 /* Peek at the next token. */
19037 token = cp_lexer_peek_token (parser->lexer);
19038 /* If we're looking at a `::' token then we're starting from the
19039 global namespace, not our current location. */
19040 if (token->type == CPP_SCOPE)
19041 {
19042 /* Consume the `::' token. */
19043 cp_lexer_consume_token (parser->lexer);
19044 /* Set the SCOPE so that we know where to start the lookup. */
19045 parser->scope = global_namespace;
19046 parser->qualifying_scope = global_namespace;
19047 parser->object_scope = NULL_TREE;
19048
19049 return parser->scope;
19050 }
19051 else if (!current_scope_valid_p)
19052 {
19053 parser->scope = NULL_TREE;
19054 parser->qualifying_scope = NULL_TREE;
19055 parser->object_scope = NULL_TREE;
19056 }
19057
19058 return NULL_TREE;
19059 }
19060
19061 /* Returns TRUE if the upcoming token sequence is the start of a
19062 constructor declarator. If FRIEND_P is true, the declarator is
19063 preceded by the `friend' specifier. */
19064
19065 static bool
19066 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19067 {
19068 bool constructor_p;
19069 tree nested_name_specifier;
19070 cp_token *next_token;
19071
19072 /* The common case is that this is not a constructor declarator, so
19073 try to avoid doing lots of work if at all possible. It's not
19074 valid declare a constructor at function scope. */
19075 if (parser->in_function_body)
19076 return false;
19077 /* And only certain tokens can begin a constructor declarator. */
19078 next_token = cp_lexer_peek_token (parser->lexer);
19079 if (next_token->type != CPP_NAME
19080 && next_token->type != CPP_SCOPE
19081 && next_token->type != CPP_NESTED_NAME_SPECIFIER
19082 && next_token->type != CPP_TEMPLATE_ID)
19083 return false;
19084
19085 /* Parse tentatively; we are going to roll back all of the tokens
19086 consumed here. */
19087 cp_parser_parse_tentatively (parser);
19088 /* Assume that we are looking at a constructor declarator. */
19089 constructor_p = true;
19090
19091 /* Look for the optional `::' operator. */
19092 cp_parser_global_scope_opt (parser,
19093 /*current_scope_valid_p=*/false);
19094 /* Look for the nested-name-specifier. */
19095 nested_name_specifier
19096 = (cp_parser_nested_name_specifier_opt (parser,
19097 /*typename_keyword_p=*/false,
19098 /*check_dependency_p=*/false,
19099 /*type_p=*/false,
19100 /*is_declaration=*/false));
19101 /* Outside of a class-specifier, there must be a
19102 nested-name-specifier. */
19103 if (!nested_name_specifier &&
19104 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19105 || friend_p))
19106 constructor_p = false;
19107 else if (nested_name_specifier == error_mark_node)
19108 constructor_p = false;
19109
19110 /* If we have a class scope, this is easy; DR 147 says that S::S always
19111 names the constructor, and no other qualified name could. */
19112 if (constructor_p && nested_name_specifier
19113 && TYPE_P (nested_name_specifier))
19114 {
19115 tree id = cp_parser_unqualified_id (parser,
19116 /*template_keyword_p=*/false,
19117 /*check_dependency_p=*/false,
19118 /*declarator_p=*/true,
19119 /*optional_p=*/false);
19120 if (is_overloaded_fn (id))
19121 id = DECL_NAME (get_first_fn (id));
19122 if (!constructor_name_p (id, nested_name_specifier))
19123 constructor_p = false;
19124 }
19125 /* If we still think that this might be a constructor-declarator,
19126 look for a class-name. */
19127 else if (constructor_p)
19128 {
19129 /* If we have:
19130
19131 template <typename T> struct S {
19132 S();
19133 };
19134
19135 we must recognize that the nested `S' names a class. */
19136 tree type_decl;
19137 type_decl = cp_parser_class_name (parser,
19138 /*typename_keyword_p=*/false,
19139 /*template_keyword_p=*/false,
19140 none_type,
19141 /*check_dependency_p=*/false,
19142 /*class_head_p=*/false,
19143 /*is_declaration=*/false);
19144 /* If there was no class-name, then this is not a constructor. */
19145 constructor_p = !cp_parser_error_occurred (parser);
19146
19147 /* If we're still considering a constructor, we have to see a `(',
19148 to begin the parameter-declaration-clause, followed by either a
19149 `)', an `...', or a decl-specifier. We need to check for a
19150 type-specifier to avoid being fooled into thinking that:
19151
19152 S (f) (int);
19153
19154 is a constructor. (It is actually a function named `f' that
19155 takes one parameter (of type `int') and returns a value of type
19156 `S'. */
19157 if (constructor_p
19158 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19159 constructor_p = false;
19160
19161 if (constructor_p
19162 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19163 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19164 /* A parameter declaration begins with a decl-specifier,
19165 which is either the "attribute" keyword, a storage class
19166 specifier, or (usually) a type-specifier. */
19167 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19168 {
19169 tree type;
19170 tree pushed_scope = NULL_TREE;
19171 unsigned saved_num_template_parameter_lists;
19172
19173 /* Names appearing in the type-specifier should be looked up
19174 in the scope of the class. */
19175 if (current_class_type)
19176 type = NULL_TREE;
19177 else
19178 {
19179 type = TREE_TYPE (type_decl);
19180 if (TREE_CODE (type) == TYPENAME_TYPE)
19181 {
19182 type = resolve_typename_type (type,
19183 /*only_current_p=*/false);
19184 if (TREE_CODE (type) == TYPENAME_TYPE)
19185 {
19186 cp_parser_abort_tentative_parse (parser);
19187 return false;
19188 }
19189 }
19190 pushed_scope = push_scope (type);
19191 }
19192
19193 /* Inside the constructor parameter list, surrounding
19194 template-parameter-lists do not apply. */
19195 saved_num_template_parameter_lists
19196 = parser->num_template_parameter_lists;
19197 parser->num_template_parameter_lists = 0;
19198
19199 /* Look for the type-specifier. */
19200 cp_parser_type_specifier (parser,
19201 CP_PARSER_FLAGS_NONE,
19202 /*decl_specs=*/NULL,
19203 /*is_declarator=*/true,
19204 /*declares_class_or_enum=*/NULL,
19205 /*is_cv_qualifier=*/NULL);
19206
19207 parser->num_template_parameter_lists
19208 = saved_num_template_parameter_lists;
19209
19210 /* Leave the scope of the class. */
19211 if (pushed_scope)
19212 pop_scope (pushed_scope);
19213
19214 constructor_p = !cp_parser_error_occurred (parser);
19215 }
19216 }
19217
19218 /* We did not really want to consume any tokens. */
19219 cp_parser_abort_tentative_parse (parser);
19220
19221 return constructor_p;
19222 }
19223
19224 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19225 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19226 they must be performed once we are in the scope of the function.
19227
19228 Returns the function defined. */
19229
19230 static tree
19231 cp_parser_function_definition_from_specifiers_and_declarator
19232 (cp_parser* parser,
19233 cp_decl_specifier_seq *decl_specifiers,
19234 tree attributes,
19235 const cp_declarator *declarator)
19236 {
19237 tree fn;
19238 bool success_p;
19239
19240 /* Begin the function-definition. */
19241 success_p = start_function (decl_specifiers, declarator, attributes);
19242
19243 /* The things we're about to see are not directly qualified by any
19244 template headers we've seen thus far. */
19245 reset_specialization ();
19246
19247 /* If there were names looked up in the decl-specifier-seq that we
19248 did not check, check them now. We must wait until we are in the
19249 scope of the function to perform the checks, since the function
19250 might be a friend. */
19251 perform_deferred_access_checks ();
19252
19253 if (!success_p)
19254 {
19255 /* Skip the entire function. */
19256 cp_parser_skip_to_end_of_block_or_statement (parser);
19257 fn = error_mark_node;
19258 }
19259 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19260 {
19261 /* Seen already, skip it. An error message has already been output. */
19262 cp_parser_skip_to_end_of_block_or_statement (parser);
19263 fn = current_function_decl;
19264 current_function_decl = NULL_TREE;
19265 /* If this is a function from a class, pop the nested class. */
19266 if (current_class_name)
19267 pop_nested_class ();
19268 }
19269 else
19270 fn = cp_parser_function_definition_after_declarator (parser,
19271 /*inline_p=*/false);
19272
19273 return fn;
19274 }
19275
19276 /* Parse the part of a function-definition that follows the
19277 declarator. INLINE_P is TRUE iff this function is an inline
19278 function defined within a class-specifier.
19279
19280 Returns the function defined. */
19281
19282 static tree
19283 cp_parser_function_definition_after_declarator (cp_parser* parser,
19284 bool inline_p)
19285 {
19286 tree fn;
19287 bool ctor_initializer_p = false;
19288 bool saved_in_unbraced_linkage_specification_p;
19289 bool saved_in_function_body;
19290 unsigned saved_num_template_parameter_lists;
19291 cp_token *token;
19292
19293 saved_in_function_body = parser->in_function_body;
19294 parser->in_function_body = true;
19295 /* If the next token is `return', then the code may be trying to
19296 make use of the "named return value" extension that G++ used to
19297 support. */
19298 token = cp_lexer_peek_token (parser->lexer);
19299 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19300 {
19301 /* Consume the `return' keyword. */
19302 cp_lexer_consume_token (parser->lexer);
19303 /* Look for the identifier that indicates what value is to be
19304 returned. */
19305 cp_parser_identifier (parser);
19306 /* Issue an error message. */
19307 error_at (token->location,
19308 "named return values are no longer supported");
19309 /* Skip tokens until we reach the start of the function body. */
19310 while (true)
19311 {
19312 cp_token *token = cp_lexer_peek_token (parser->lexer);
19313 if (token->type == CPP_OPEN_BRACE
19314 || token->type == CPP_EOF
19315 || token->type == CPP_PRAGMA_EOL)
19316 break;
19317 cp_lexer_consume_token (parser->lexer);
19318 }
19319 }
19320 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19321 anything declared inside `f'. */
19322 saved_in_unbraced_linkage_specification_p
19323 = parser->in_unbraced_linkage_specification_p;
19324 parser->in_unbraced_linkage_specification_p = false;
19325 /* Inside the function, surrounding template-parameter-lists do not
19326 apply. */
19327 saved_num_template_parameter_lists
19328 = parser->num_template_parameter_lists;
19329 parser->num_template_parameter_lists = 0;
19330
19331 start_lambda_scope (current_function_decl);
19332
19333 /* If the next token is `try', then we are looking at a
19334 function-try-block. */
19335 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19336 ctor_initializer_p = cp_parser_function_try_block (parser);
19337 /* A function-try-block includes the function-body, so we only do
19338 this next part if we're not processing a function-try-block. */
19339 else
19340 ctor_initializer_p
19341 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19342
19343 finish_lambda_scope ();
19344
19345 /* Finish the function. */
19346 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19347 (inline_p ? 2 : 0));
19348 /* Generate code for it, if necessary. */
19349 expand_or_defer_fn (fn);
19350 /* Restore the saved values. */
19351 parser->in_unbraced_linkage_specification_p
19352 = saved_in_unbraced_linkage_specification_p;
19353 parser->num_template_parameter_lists
19354 = saved_num_template_parameter_lists;
19355 parser->in_function_body = saved_in_function_body;
19356
19357 return fn;
19358 }
19359
19360 /* Parse a template-declaration, assuming that the `export' (and
19361 `extern') keywords, if present, has already been scanned. MEMBER_P
19362 is as for cp_parser_template_declaration. */
19363
19364 static void
19365 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19366 {
19367 tree decl = NULL_TREE;
19368 VEC (deferred_access_check,gc) *checks;
19369 tree parameter_list;
19370 bool friend_p = false;
19371 bool need_lang_pop;
19372 cp_token *token;
19373
19374 /* Look for the `template' keyword. */
19375 token = cp_lexer_peek_token (parser->lexer);
19376 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19377 return;
19378
19379 /* And the `<'. */
19380 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19381 return;
19382 if (at_class_scope_p () && current_function_decl)
19383 {
19384 /* 14.5.2.2 [temp.mem]
19385
19386 A local class shall not have member templates. */
19387 error_at (token->location,
19388 "invalid declaration of member template in local class");
19389 cp_parser_skip_to_end_of_block_or_statement (parser);
19390 return;
19391 }
19392 /* [temp]
19393
19394 A template ... shall not have C linkage. */
19395 if (current_lang_name == lang_name_c)
19396 {
19397 error_at (token->location, "template with C linkage");
19398 /* Give it C++ linkage to avoid confusing other parts of the
19399 front end. */
19400 push_lang_context (lang_name_cplusplus);
19401 need_lang_pop = true;
19402 }
19403 else
19404 need_lang_pop = false;
19405
19406 /* We cannot perform access checks on the template parameter
19407 declarations until we know what is being declared, just as we
19408 cannot check the decl-specifier list. */
19409 push_deferring_access_checks (dk_deferred);
19410
19411 /* If the next token is `>', then we have an invalid
19412 specialization. Rather than complain about an invalid template
19413 parameter, issue an error message here. */
19414 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19415 {
19416 cp_parser_error (parser, "invalid explicit specialization");
19417 begin_specialization ();
19418 parameter_list = NULL_TREE;
19419 }
19420 else
19421 /* Parse the template parameters. */
19422 parameter_list = cp_parser_template_parameter_list (parser);
19423
19424 /* Get the deferred access checks from the parameter list. These
19425 will be checked once we know what is being declared, as for a
19426 member template the checks must be performed in the scope of the
19427 class containing the member. */
19428 checks = get_deferred_access_checks ();
19429
19430 /* Look for the `>'. */
19431 cp_parser_skip_to_end_of_template_parameter_list (parser);
19432 /* We just processed one more parameter list. */
19433 ++parser->num_template_parameter_lists;
19434 /* If the next token is `template', there are more template
19435 parameters. */
19436 if (cp_lexer_next_token_is_keyword (parser->lexer,
19437 RID_TEMPLATE))
19438 cp_parser_template_declaration_after_export (parser, member_p);
19439 else
19440 {
19441 /* There are no access checks when parsing a template, as we do not
19442 know if a specialization will be a friend. */
19443 push_deferring_access_checks (dk_no_check);
19444 token = cp_lexer_peek_token (parser->lexer);
19445 decl = cp_parser_single_declaration (parser,
19446 checks,
19447 member_p,
19448 /*explicit_specialization_p=*/false,
19449 &friend_p);
19450 pop_deferring_access_checks ();
19451
19452 /* If this is a member template declaration, let the front
19453 end know. */
19454 if (member_p && !friend_p && decl)
19455 {
19456 if (TREE_CODE (decl) == TYPE_DECL)
19457 cp_parser_check_access_in_redeclaration (decl, token->location);
19458
19459 decl = finish_member_template_decl (decl);
19460 }
19461 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19462 make_friend_class (current_class_type, TREE_TYPE (decl),
19463 /*complain=*/true);
19464 }
19465 /* We are done with the current parameter list. */
19466 --parser->num_template_parameter_lists;
19467
19468 pop_deferring_access_checks ();
19469
19470 /* Finish up. */
19471 finish_template_decl (parameter_list);
19472
19473 /* Register member declarations. */
19474 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19475 finish_member_declaration (decl);
19476 /* For the erroneous case of a template with C linkage, we pushed an
19477 implicit C++ linkage scope; exit that scope now. */
19478 if (need_lang_pop)
19479 pop_lang_context ();
19480 /* If DECL is a function template, we must return to parse it later.
19481 (Even though there is no definition, there might be default
19482 arguments that need handling.) */
19483 if (member_p && decl
19484 && (TREE_CODE (decl) == FUNCTION_DECL
19485 || DECL_FUNCTION_TEMPLATE_P (decl)))
19486 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19487 }
19488
19489 /* Perform the deferred access checks from a template-parameter-list.
19490 CHECKS is a TREE_LIST of access checks, as returned by
19491 get_deferred_access_checks. */
19492
19493 static void
19494 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19495 {
19496 ++processing_template_parmlist;
19497 perform_access_checks (checks);
19498 --processing_template_parmlist;
19499 }
19500
19501 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19502 `function-definition' sequence. MEMBER_P is true, this declaration
19503 appears in a class scope.
19504
19505 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19506 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19507
19508 static tree
19509 cp_parser_single_declaration (cp_parser* parser,
19510 VEC (deferred_access_check,gc)* checks,
19511 bool member_p,
19512 bool explicit_specialization_p,
19513 bool* friend_p)
19514 {
19515 int declares_class_or_enum;
19516 tree decl = NULL_TREE;
19517 cp_decl_specifier_seq decl_specifiers;
19518 bool function_definition_p = false;
19519 cp_token *decl_spec_token_start;
19520
19521 /* This function is only used when processing a template
19522 declaration. */
19523 gcc_assert (innermost_scope_kind () == sk_template_parms
19524 || innermost_scope_kind () == sk_template_spec);
19525
19526 /* Defer access checks until we know what is being declared. */
19527 push_deferring_access_checks (dk_deferred);
19528
19529 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19530 alternative. */
19531 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19532 cp_parser_decl_specifier_seq (parser,
19533 CP_PARSER_FLAGS_OPTIONAL,
19534 &decl_specifiers,
19535 &declares_class_or_enum);
19536 if (friend_p)
19537 *friend_p = cp_parser_friend_p (&decl_specifiers);
19538
19539 /* There are no template typedefs. */
19540 if (decl_specifiers.specs[(int) ds_typedef])
19541 {
19542 error_at (decl_spec_token_start->location,
19543 "template declaration of %<typedef%>");
19544 decl = error_mark_node;
19545 }
19546
19547 /* Gather up the access checks that occurred the
19548 decl-specifier-seq. */
19549 stop_deferring_access_checks ();
19550
19551 /* Check for the declaration of a template class. */
19552 if (declares_class_or_enum)
19553 {
19554 if (cp_parser_declares_only_class_p (parser))
19555 {
19556 decl = shadow_tag (&decl_specifiers);
19557
19558 /* In this case:
19559
19560 struct C {
19561 friend template <typename T> struct A<T>::B;
19562 };
19563
19564 A<T>::B will be represented by a TYPENAME_TYPE, and
19565 therefore not recognized by shadow_tag. */
19566 if (friend_p && *friend_p
19567 && !decl
19568 && decl_specifiers.type
19569 && TYPE_P (decl_specifiers.type))
19570 decl = decl_specifiers.type;
19571
19572 if (decl && decl != error_mark_node)
19573 decl = TYPE_NAME (decl);
19574 else
19575 decl = error_mark_node;
19576
19577 /* Perform access checks for template parameters. */
19578 cp_parser_perform_template_parameter_access_checks (checks);
19579 }
19580 }
19581
19582 /* Complain about missing 'typename' or other invalid type names. */
19583 if (!decl_specifiers.any_type_specifiers_p)
19584 cp_parser_parse_and_diagnose_invalid_type_name (parser);
19585
19586 /* If it's not a template class, try for a template function. If
19587 the next token is a `;', then this declaration does not declare
19588 anything. But, if there were errors in the decl-specifiers, then
19589 the error might well have come from an attempted class-specifier.
19590 In that case, there's no need to warn about a missing declarator. */
19591 if (!decl
19592 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
19593 || decl_specifiers.type != error_mark_node))
19594 {
19595 decl = cp_parser_init_declarator (parser,
19596 &decl_specifiers,
19597 checks,
19598 /*function_definition_allowed_p=*/true,
19599 member_p,
19600 declares_class_or_enum,
19601 &function_definition_p);
19602
19603 /* 7.1.1-1 [dcl.stc]
19604
19605 A storage-class-specifier shall not be specified in an explicit
19606 specialization... */
19607 if (decl
19608 && explicit_specialization_p
19609 && decl_specifiers.storage_class != sc_none)
19610 {
19611 error_at (decl_spec_token_start->location,
19612 "explicit template specialization cannot have a storage class");
19613 decl = error_mark_node;
19614 }
19615 }
19616
19617 pop_deferring_access_checks ();
19618
19619 /* Clear any current qualification; whatever comes next is the start
19620 of something new. */
19621 parser->scope = NULL_TREE;
19622 parser->qualifying_scope = NULL_TREE;
19623 parser->object_scope = NULL_TREE;
19624 /* Look for a trailing `;' after the declaration. */
19625 if (!function_definition_p
19626 && (decl == error_mark_node
19627 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
19628 cp_parser_skip_to_end_of_block_or_statement (parser);
19629
19630 return decl;
19631 }
19632
19633 /* Parse a cast-expression that is not the operand of a unary "&". */
19634
19635 static tree
19636 cp_parser_simple_cast_expression (cp_parser *parser)
19637 {
19638 return cp_parser_cast_expression (parser, /*address_p=*/false,
19639 /*cast_p=*/false, NULL);
19640 }
19641
19642 /* Parse a functional cast to TYPE. Returns an expression
19643 representing the cast. */
19644
19645 static tree
19646 cp_parser_functional_cast (cp_parser* parser, tree type)
19647 {
19648 VEC(tree,gc) *vec;
19649 tree expression_list;
19650 tree cast;
19651 bool nonconst_p;
19652
19653 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19654 {
19655 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19656 expression_list = cp_parser_braced_list (parser, &nonconst_p);
19657 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
19658 if (TREE_CODE (type) == TYPE_DECL)
19659 type = TREE_TYPE (type);
19660 return finish_compound_literal (type, expression_list);
19661 }
19662
19663
19664 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19665 /*cast_p=*/true,
19666 /*allow_expansion_p=*/true,
19667 /*non_constant_p=*/NULL);
19668 if (vec == NULL)
19669 expression_list = error_mark_node;
19670 else
19671 {
19672 expression_list = build_tree_list_vec (vec);
19673 release_tree_vector (vec);
19674 }
19675
19676 cast = build_functional_cast (type, expression_list,
19677 tf_warning_or_error);
19678 /* [expr.const]/1: In an integral constant expression "only type
19679 conversions to integral or enumeration type can be used". */
19680 if (TREE_CODE (type) == TYPE_DECL)
19681 type = TREE_TYPE (type);
19682 if (cast != error_mark_node
19683 && !cast_valid_in_integral_constant_expression_p (type)
19684 && cp_parser_non_integral_constant_expression (parser,
19685 NIC_CONSTRUCTOR))
19686 return error_mark_node;
19687 return cast;
19688 }
19689
19690 /* Save the tokens that make up the body of a member function defined
19691 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
19692 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
19693 specifiers applied to the declaration. Returns the FUNCTION_DECL
19694 for the member function. */
19695
19696 static tree
19697 cp_parser_save_member_function_body (cp_parser* parser,
19698 cp_decl_specifier_seq *decl_specifiers,
19699 cp_declarator *declarator,
19700 tree attributes)
19701 {
19702 cp_token *first;
19703 cp_token *last;
19704 tree fn;
19705
19706 /* Create the FUNCTION_DECL. */
19707 fn = grokmethod (decl_specifiers, declarator, attributes);
19708 /* If something went badly wrong, bail out now. */
19709 if (fn == error_mark_node)
19710 {
19711 /* If there's a function-body, skip it. */
19712 if (cp_parser_token_starts_function_definition_p
19713 (cp_lexer_peek_token (parser->lexer)))
19714 cp_parser_skip_to_end_of_block_or_statement (parser);
19715 return error_mark_node;
19716 }
19717
19718 /* Remember it, if there default args to post process. */
19719 cp_parser_save_default_args (parser, fn);
19720
19721 /* Save away the tokens that make up the body of the
19722 function. */
19723 first = parser->lexer->next_token;
19724 /* We can have braced-init-list mem-initializers before the fn body. */
19725 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19726 {
19727 cp_lexer_consume_token (parser->lexer);
19728 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19729 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19730 {
19731 /* cache_group will stop after an un-nested { } pair, too. */
19732 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19733 break;
19734
19735 /* variadic mem-inits have ... after the ')'. */
19736 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19737 cp_lexer_consume_token (parser->lexer);
19738 }
19739 }
19740 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19741 /* Handle function try blocks. */
19742 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19743 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19744 last = parser->lexer->next_token;
19745
19746 /* Save away the inline definition; we will process it when the
19747 class is complete. */
19748 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19749 DECL_PENDING_INLINE_P (fn) = 1;
19750
19751 /* We need to know that this was defined in the class, so that
19752 friend templates are handled correctly. */
19753 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19754
19755 /* Add FN to the queue of functions to be parsed later. */
19756 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
19757
19758 return fn;
19759 }
19760
19761 /* Parse a template-argument-list, as well as the trailing ">" (but
19762 not the opening ">"). See cp_parser_template_argument_list for the
19763 return value. */
19764
19765 static tree
19766 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19767 {
19768 tree arguments;
19769 tree saved_scope;
19770 tree saved_qualifying_scope;
19771 tree saved_object_scope;
19772 bool saved_greater_than_is_operator_p;
19773 int saved_unevaluated_operand;
19774 int saved_inhibit_evaluation_warnings;
19775
19776 /* [temp.names]
19777
19778 When parsing a template-id, the first non-nested `>' is taken as
19779 the end of the template-argument-list rather than a greater-than
19780 operator. */
19781 saved_greater_than_is_operator_p
19782 = parser->greater_than_is_operator_p;
19783 parser->greater_than_is_operator_p = false;
19784 /* Parsing the argument list may modify SCOPE, so we save it
19785 here. */
19786 saved_scope = parser->scope;
19787 saved_qualifying_scope = parser->qualifying_scope;
19788 saved_object_scope = parser->object_scope;
19789 /* We need to evaluate the template arguments, even though this
19790 template-id may be nested within a "sizeof". */
19791 saved_unevaluated_operand = cp_unevaluated_operand;
19792 cp_unevaluated_operand = 0;
19793 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19794 c_inhibit_evaluation_warnings = 0;
19795 /* Parse the template-argument-list itself. */
19796 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19797 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19798 arguments = NULL_TREE;
19799 else
19800 arguments = cp_parser_template_argument_list (parser);
19801 /* Look for the `>' that ends the template-argument-list. If we find
19802 a '>>' instead, it's probably just a typo. */
19803 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19804 {
19805 if (cxx_dialect != cxx98)
19806 {
19807 /* In C++0x, a `>>' in a template argument list or cast
19808 expression is considered to be two separate `>'
19809 tokens. So, change the current token to a `>', but don't
19810 consume it: it will be consumed later when the outer
19811 template argument list (or cast expression) is parsed.
19812 Note that this replacement of `>' for `>>' is necessary
19813 even if we are parsing tentatively: in the tentative
19814 case, after calling
19815 cp_parser_enclosed_template_argument_list we will always
19816 throw away all of the template arguments and the first
19817 closing `>', either because the template argument list
19818 was erroneous or because we are replacing those tokens
19819 with a CPP_TEMPLATE_ID token. The second `>' (which will
19820 not have been thrown away) is needed either to close an
19821 outer template argument list or to complete a new-style
19822 cast. */
19823 cp_token *token = cp_lexer_peek_token (parser->lexer);
19824 token->type = CPP_GREATER;
19825 }
19826 else if (!saved_greater_than_is_operator_p)
19827 {
19828 /* If we're in a nested template argument list, the '>>' has
19829 to be a typo for '> >'. We emit the error message, but we
19830 continue parsing and we push a '>' as next token, so that
19831 the argument list will be parsed correctly. Note that the
19832 global source location is still on the token before the
19833 '>>', so we need to say explicitly where we want it. */
19834 cp_token *token = cp_lexer_peek_token (parser->lexer);
19835 error_at (token->location, "%<>>%> should be %<> >%> "
19836 "within a nested template argument list");
19837
19838 token->type = CPP_GREATER;
19839 }
19840 else
19841 {
19842 /* If this is not a nested template argument list, the '>>'
19843 is a typo for '>'. Emit an error message and continue.
19844 Same deal about the token location, but here we can get it
19845 right by consuming the '>>' before issuing the diagnostic. */
19846 cp_token *token = cp_lexer_consume_token (parser->lexer);
19847 error_at (token->location,
19848 "spurious %<>>%>, use %<>%> to terminate "
19849 "a template argument list");
19850 }
19851 }
19852 else
19853 cp_parser_skip_to_end_of_template_parameter_list (parser);
19854 /* The `>' token might be a greater-than operator again now. */
19855 parser->greater_than_is_operator_p
19856 = saved_greater_than_is_operator_p;
19857 /* Restore the SAVED_SCOPE. */
19858 parser->scope = saved_scope;
19859 parser->qualifying_scope = saved_qualifying_scope;
19860 parser->object_scope = saved_object_scope;
19861 cp_unevaluated_operand = saved_unevaluated_operand;
19862 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19863
19864 return arguments;
19865 }
19866
19867 /* MEMBER_FUNCTION is a member function, or a friend. If default
19868 arguments, or the body of the function have not yet been parsed,
19869 parse them now. */
19870
19871 static void
19872 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19873 {
19874 /* If this member is a template, get the underlying
19875 FUNCTION_DECL. */
19876 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19877 member_function = DECL_TEMPLATE_RESULT (member_function);
19878
19879 /* There should not be any class definitions in progress at this
19880 point; the bodies of members are only parsed outside of all class
19881 definitions. */
19882 gcc_assert (parser->num_classes_being_defined == 0);
19883 /* While we're parsing the member functions we might encounter more
19884 classes. We want to handle them right away, but we don't want
19885 them getting mixed up with functions that are currently in the
19886 queue. */
19887 push_unparsed_function_queues (parser);
19888
19889 /* Make sure that any template parameters are in scope. */
19890 maybe_begin_member_template_processing (member_function);
19891
19892 /* If the body of the function has not yet been parsed, parse it
19893 now. */
19894 if (DECL_PENDING_INLINE_P (member_function))
19895 {
19896 tree function_scope;
19897 cp_token_cache *tokens;
19898
19899 /* The function is no longer pending; we are processing it. */
19900 tokens = DECL_PENDING_INLINE_INFO (member_function);
19901 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19902 DECL_PENDING_INLINE_P (member_function) = 0;
19903
19904 /* If this is a local class, enter the scope of the containing
19905 function. */
19906 function_scope = current_function_decl;
19907 if (function_scope)
19908 push_function_context ();
19909
19910 /* Push the body of the function onto the lexer stack. */
19911 cp_parser_push_lexer_for_tokens (parser, tokens);
19912
19913 /* Let the front end know that we going to be defining this
19914 function. */
19915 start_preparsed_function (member_function, NULL_TREE,
19916 SF_PRE_PARSED | SF_INCLASS_INLINE);
19917
19918 /* Don't do access checking if it is a templated function. */
19919 if (processing_template_decl)
19920 push_deferring_access_checks (dk_no_check);
19921
19922 /* Now, parse the body of the function. */
19923 cp_parser_function_definition_after_declarator (parser,
19924 /*inline_p=*/true);
19925
19926 if (processing_template_decl)
19927 pop_deferring_access_checks ();
19928
19929 /* Leave the scope of the containing function. */
19930 if (function_scope)
19931 pop_function_context ();
19932 cp_parser_pop_lexer (parser);
19933 }
19934
19935 /* Remove any template parameters from the symbol table. */
19936 maybe_end_member_template_processing ();
19937
19938 /* Restore the queue. */
19939 pop_unparsed_function_queues (parser);
19940 }
19941
19942 /* If DECL contains any default args, remember it on the unparsed
19943 functions queue. */
19944
19945 static void
19946 cp_parser_save_default_args (cp_parser* parser, tree decl)
19947 {
19948 tree probe;
19949
19950 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19951 probe;
19952 probe = TREE_CHAIN (probe))
19953 if (TREE_PURPOSE (probe))
19954 {
19955 cp_default_arg_entry *entry
19956 = VEC_safe_push (cp_default_arg_entry, gc,
19957 unparsed_funs_with_default_args, NULL);
19958 entry->class_type = current_class_type;
19959 entry->decl = decl;
19960 break;
19961 }
19962 }
19963
19964 /* FN is a FUNCTION_DECL which may contains a parameter with an
19965 unparsed DEFAULT_ARG. Parse the default args now. This function
19966 assumes that the current scope is the scope in which the default
19967 argument should be processed. */
19968
19969 static void
19970 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19971 {
19972 bool saved_local_variables_forbidden_p;
19973 tree parm, parmdecl;
19974
19975 /* While we're parsing the default args, we might (due to the
19976 statement expression extension) encounter more classes. We want
19977 to handle them right away, but we don't want them getting mixed
19978 up with default args that are currently in the queue. */
19979 push_unparsed_function_queues (parser);
19980
19981 /* Local variable names (and the `this' keyword) may not appear
19982 in a default argument. */
19983 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19984 parser->local_variables_forbidden_p = true;
19985
19986 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19987 parmdecl = DECL_ARGUMENTS (fn);
19988 parm && parm != void_list_node;
19989 parm = TREE_CHAIN (parm),
19990 parmdecl = DECL_CHAIN (parmdecl))
19991 {
19992 cp_token_cache *tokens;
19993 tree default_arg = TREE_PURPOSE (parm);
19994 tree parsed_arg;
19995 VEC(tree,gc) *insts;
19996 tree copy;
19997 unsigned ix;
19998
19999 if (!default_arg)
20000 continue;
20001
20002 if (TREE_CODE (default_arg) != DEFAULT_ARG)
20003 /* This can happen for a friend declaration for a function
20004 already declared with default arguments. */
20005 continue;
20006
20007 /* Push the saved tokens for the default argument onto the parser's
20008 lexer stack. */
20009 tokens = DEFARG_TOKENS (default_arg);
20010 cp_parser_push_lexer_for_tokens (parser, tokens);
20011
20012 start_lambda_scope (parmdecl);
20013
20014 /* Parse the assignment-expression. */
20015 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20016 if (parsed_arg == error_mark_node)
20017 {
20018 cp_parser_pop_lexer (parser);
20019 continue;
20020 }
20021
20022 if (!processing_template_decl)
20023 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20024
20025 TREE_PURPOSE (parm) = parsed_arg;
20026
20027 /* Update any instantiations we've already created. */
20028 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20029 VEC_iterate (tree, insts, ix, copy); ix++)
20030 TREE_PURPOSE (copy) = parsed_arg;
20031
20032 finish_lambda_scope ();
20033
20034 /* If the token stream has not been completely used up, then
20035 there was extra junk after the end of the default
20036 argument. */
20037 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20038 cp_parser_error (parser, "expected %<,%>");
20039
20040 /* Revert to the main lexer. */
20041 cp_parser_pop_lexer (parser);
20042 }
20043
20044 /* Make sure no default arg is missing. */
20045 check_default_args (fn);
20046
20047 /* Restore the state of local_variables_forbidden_p. */
20048 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20049
20050 /* Restore the queue. */
20051 pop_unparsed_function_queues (parser);
20052 }
20053
20054 /* Parse the operand of `sizeof' (or a similar operator). Returns
20055 either a TYPE or an expression, depending on the form of the
20056 input. The KEYWORD indicates which kind of expression we have
20057 encountered. */
20058
20059 static tree
20060 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20061 {
20062 tree expr = NULL_TREE;
20063 const char *saved_message;
20064 char *tmp;
20065 bool saved_integral_constant_expression_p;
20066 bool saved_non_integral_constant_expression_p;
20067 bool pack_expansion_p = false;
20068
20069 /* Types cannot be defined in a `sizeof' expression. Save away the
20070 old message. */
20071 saved_message = parser->type_definition_forbidden_message;
20072 /* And create the new one. */
20073 tmp = concat ("types may not be defined in %<",
20074 IDENTIFIER_POINTER (ridpointers[keyword]),
20075 "%> expressions", NULL);
20076 parser->type_definition_forbidden_message = tmp;
20077
20078 /* The restrictions on constant-expressions do not apply inside
20079 sizeof expressions. */
20080 saved_integral_constant_expression_p
20081 = parser->integral_constant_expression_p;
20082 saved_non_integral_constant_expression_p
20083 = parser->non_integral_constant_expression_p;
20084 parser->integral_constant_expression_p = false;
20085
20086 /* If it's a `...', then we are computing the length of a parameter
20087 pack. */
20088 if (keyword == RID_SIZEOF
20089 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20090 {
20091 /* Consume the `...'. */
20092 cp_lexer_consume_token (parser->lexer);
20093 maybe_warn_variadic_templates ();
20094
20095 /* Note that this is an expansion. */
20096 pack_expansion_p = true;
20097 }
20098
20099 /* Do not actually evaluate the expression. */
20100 ++cp_unevaluated_operand;
20101 ++c_inhibit_evaluation_warnings;
20102 /* If it's a `(', then we might be looking at the type-id
20103 construction. */
20104 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20105 {
20106 tree type;
20107 bool saved_in_type_id_in_expr_p;
20108
20109 /* We can't be sure yet whether we're looking at a type-id or an
20110 expression. */
20111 cp_parser_parse_tentatively (parser);
20112 /* Consume the `('. */
20113 cp_lexer_consume_token (parser->lexer);
20114 /* Parse the type-id. */
20115 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20116 parser->in_type_id_in_expr_p = true;
20117 type = cp_parser_type_id (parser);
20118 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20119 /* Now, look for the trailing `)'. */
20120 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20121 /* If all went well, then we're done. */
20122 if (cp_parser_parse_definitely (parser))
20123 {
20124 cp_decl_specifier_seq decl_specs;
20125
20126 /* Build a trivial decl-specifier-seq. */
20127 clear_decl_specs (&decl_specs);
20128 decl_specs.type = type;
20129
20130 /* Call grokdeclarator to figure out what type this is. */
20131 expr = grokdeclarator (NULL,
20132 &decl_specs,
20133 TYPENAME,
20134 /*initialized=*/0,
20135 /*attrlist=*/NULL);
20136 }
20137 }
20138
20139 /* If the type-id production did not work out, then we must be
20140 looking at the unary-expression production. */
20141 if (!expr)
20142 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20143 /*cast_p=*/false, NULL);
20144
20145 if (pack_expansion_p)
20146 /* Build a pack expansion. */
20147 expr = make_pack_expansion (expr);
20148
20149 /* Go back to evaluating expressions. */
20150 --cp_unevaluated_operand;
20151 --c_inhibit_evaluation_warnings;
20152
20153 /* Free the message we created. */
20154 free (tmp);
20155 /* And restore the old one. */
20156 parser->type_definition_forbidden_message = saved_message;
20157 parser->integral_constant_expression_p
20158 = saved_integral_constant_expression_p;
20159 parser->non_integral_constant_expression_p
20160 = saved_non_integral_constant_expression_p;
20161
20162 return expr;
20163 }
20164
20165 /* If the current declaration has no declarator, return true. */
20166
20167 static bool
20168 cp_parser_declares_only_class_p (cp_parser *parser)
20169 {
20170 /* If the next token is a `;' or a `,' then there is no
20171 declarator. */
20172 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20173 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20174 }
20175
20176 /* Update the DECL_SPECS to reflect the storage class indicated by
20177 KEYWORD. */
20178
20179 static void
20180 cp_parser_set_storage_class (cp_parser *parser,
20181 cp_decl_specifier_seq *decl_specs,
20182 enum rid keyword,
20183 location_t location)
20184 {
20185 cp_storage_class storage_class;
20186
20187 if (parser->in_unbraced_linkage_specification_p)
20188 {
20189 error_at (location, "invalid use of %qD in linkage specification",
20190 ridpointers[keyword]);
20191 return;
20192 }
20193 else if (decl_specs->storage_class != sc_none)
20194 {
20195 decl_specs->conflicting_specifiers_p = true;
20196 return;
20197 }
20198
20199 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20200 && decl_specs->specs[(int) ds_thread])
20201 {
20202 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20203 decl_specs->specs[(int) ds_thread] = 0;
20204 }
20205
20206 switch (keyword)
20207 {
20208 case RID_AUTO:
20209 storage_class = sc_auto;
20210 break;
20211 case RID_REGISTER:
20212 storage_class = sc_register;
20213 break;
20214 case RID_STATIC:
20215 storage_class = sc_static;
20216 break;
20217 case RID_EXTERN:
20218 storage_class = sc_extern;
20219 break;
20220 case RID_MUTABLE:
20221 storage_class = sc_mutable;
20222 break;
20223 default:
20224 gcc_unreachable ();
20225 }
20226 decl_specs->storage_class = storage_class;
20227
20228 /* A storage class specifier cannot be applied alongside a typedef
20229 specifier. If there is a typedef specifier present then set
20230 conflicting_specifiers_p which will trigger an error later
20231 on in grokdeclarator. */
20232 if (decl_specs->specs[(int)ds_typedef])
20233 decl_specs->conflicting_specifiers_p = true;
20234 }
20235
20236 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20237 is true, the type is a user-defined type; otherwise it is a
20238 built-in type specified by a keyword. */
20239
20240 static void
20241 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20242 tree type_spec,
20243 location_t location,
20244 bool user_defined_p)
20245 {
20246 decl_specs->any_specifiers_p = true;
20247
20248 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20249 (with, for example, in "typedef int wchar_t;") we remember that
20250 this is what happened. In system headers, we ignore these
20251 declarations so that G++ can work with system headers that are not
20252 C++-safe. */
20253 if (decl_specs->specs[(int) ds_typedef]
20254 && !user_defined_p
20255 && (type_spec == boolean_type_node
20256 || type_spec == char16_type_node
20257 || type_spec == char32_type_node
20258 || type_spec == wchar_type_node)
20259 && (decl_specs->type
20260 || decl_specs->specs[(int) ds_long]
20261 || decl_specs->specs[(int) ds_short]
20262 || decl_specs->specs[(int) ds_unsigned]
20263 || decl_specs->specs[(int) ds_signed]))
20264 {
20265 decl_specs->redefined_builtin_type = type_spec;
20266 if (!decl_specs->type)
20267 {
20268 decl_specs->type = type_spec;
20269 decl_specs->user_defined_type_p = false;
20270 decl_specs->type_location = location;
20271 }
20272 }
20273 else if (decl_specs->type)
20274 decl_specs->multiple_types_p = true;
20275 else
20276 {
20277 decl_specs->type = type_spec;
20278 decl_specs->user_defined_type_p = user_defined_p;
20279 decl_specs->redefined_builtin_type = NULL_TREE;
20280 decl_specs->type_location = location;
20281 }
20282 }
20283
20284 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20285 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20286
20287 static bool
20288 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20289 {
20290 return decl_specifiers->specs[(int) ds_friend] != 0;
20291 }
20292
20293 /* Issue an error message indicating that TOKEN_DESC was expected.
20294 If KEYWORD is true, it indicated this function is called by
20295 cp_parser_require_keword and the required token can only be
20296 a indicated keyword. */
20297
20298 static void
20299 cp_parser_required_error (cp_parser *parser,
20300 required_token token_desc,
20301 bool keyword)
20302 {
20303 switch (token_desc)
20304 {
20305 case RT_NEW:
20306 cp_parser_error (parser, "expected %<new%>");
20307 return;
20308 case RT_DELETE:
20309 cp_parser_error (parser, "expected %<delete%>");
20310 return;
20311 case RT_RETURN:
20312 cp_parser_error (parser, "expected %<return%>");
20313 return;
20314 case RT_WHILE:
20315 cp_parser_error (parser, "expected %<while%>");
20316 return;
20317 case RT_EXTERN:
20318 cp_parser_error (parser, "expected %<extern%>");
20319 return;
20320 case RT_STATIC_ASSERT:
20321 cp_parser_error (parser, "expected %<static_assert%>");
20322 return;
20323 case RT_DECLTYPE:
20324 cp_parser_error (parser, "expected %<decltype%>");
20325 return;
20326 case RT_OPERATOR:
20327 cp_parser_error (parser, "expected %<operator%>");
20328 return;
20329 case RT_CLASS:
20330 cp_parser_error (parser, "expected %<class%>");
20331 return;
20332 case RT_TEMPLATE:
20333 cp_parser_error (parser, "expected %<template%>");
20334 return;
20335 case RT_NAMESPACE:
20336 cp_parser_error (parser, "expected %<namespace%>");
20337 return;
20338 case RT_USING:
20339 cp_parser_error (parser, "expected %<using%>");
20340 return;
20341 case RT_ASM:
20342 cp_parser_error (parser, "expected %<asm%>");
20343 return;
20344 case RT_TRY:
20345 cp_parser_error (parser, "expected %<try%>");
20346 return;
20347 case RT_CATCH:
20348 cp_parser_error (parser, "expected %<catch%>");
20349 return;
20350 case RT_THROW:
20351 cp_parser_error (parser, "expected %<throw%>");
20352 return;
20353 case RT_LABEL:
20354 cp_parser_error (parser, "expected %<__label__%>");
20355 return;
20356 case RT_AT_TRY:
20357 cp_parser_error (parser, "expected %<@try%>");
20358 return;
20359 case RT_AT_SYNCHRONIZED:
20360 cp_parser_error (parser, "expected %<@synchronized%>");
20361 return;
20362 case RT_AT_THROW:
20363 cp_parser_error (parser, "expected %<@throw%>");
20364 return;
20365 default:
20366 break;
20367 }
20368 if (!keyword)
20369 {
20370 switch (token_desc)
20371 {
20372 case RT_SEMICOLON:
20373 cp_parser_error (parser, "expected %<;%>");
20374 return;
20375 case RT_OPEN_PAREN:
20376 cp_parser_error (parser, "expected %<(%>");
20377 return;
20378 case RT_CLOSE_BRACE:
20379 cp_parser_error (parser, "expected %<}%>");
20380 return;
20381 case RT_OPEN_BRACE:
20382 cp_parser_error (parser, "expected %<{%>");
20383 return;
20384 case RT_CLOSE_SQUARE:
20385 cp_parser_error (parser, "expected %<]%>");
20386 return;
20387 case RT_OPEN_SQUARE:
20388 cp_parser_error (parser, "expected %<[%>");
20389 return;
20390 case RT_COMMA:
20391 cp_parser_error (parser, "expected %<,%>");
20392 return;
20393 case RT_SCOPE:
20394 cp_parser_error (parser, "expected %<::%>");
20395 return;
20396 case RT_LESS:
20397 cp_parser_error (parser, "expected %<<%>");
20398 return;
20399 case RT_GREATER:
20400 cp_parser_error (parser, "expected %<>%>");
20401 return;
20402 case RT_EQ:
20403 cp_parser_error (parser, "expected %<=%>");
20404 return;
20405 case RT_ELLIPSIS:
20406 cp_parser_error (parser, "expected %<...%>");
20407 return;
20408 case RT_MULT:
20409 cp_parser_error (parser, "expected %<*%>");
20410 return;
20411 case RT_COMPL:
20412 cp_parser_error (parser, "expected %<~%>");
20413 return;
20414 case RT_COLON:
20415 cp_parser_error (parser, "expected %<:%>");
20416 return;
20417 case RT_COLON_SCOPE:
20418 cp_parser_error (parser, "expected %<:%> or %<::%>");
20419 return;
20420 case RT_CLOSE_PAREN:
20421 cp_parser_error (parser, "expected %<)%>");
20422 return;
20423 case RT_COMMA_CLOSE_PAREN:
20424 cp_parser_error (parser, "expected %<,%> or %<)%>");
20425 return;
20426 case RT_PRAGMA_EOL:
20427 cp_parser_error (parser, "expected end of line");
20428 return;
20429 case RT_NAME:
20430 cp_parser_error (parser, "expected identifier");
20431 return;
20432 case RT_SELECT:
20433 cp_parser_error (parser, "expected selection-statement");
20434 return;
20435 case RT_INTERATION:
20436 cp_parser_error (parser, "expected iteration-statement");
20437 return;
20438 case RT_JUMP:
20439 cp_parser_error (parser, "expected jump-statement");
20440 return;
20441 case RT_CLASS_KEY:
20442 cp_parser_error (parser, "expected class-key");
20443 return;
20444 case RT_CLASS_TYPENAME_TEMPLATE:
20445 cp_parser_error (parser,
20446 "expected %<class%>, %<typename%>, or %<template%>");
20447 return;
20448 default:
20449 gcc_unreachable ();
20450 }
20451 }
20452 else
20453 gcc_unreachable ();
20454 }
20455
20456
20457
20458 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20459 issue an error message indicating that TOKEN_DESC was expected.
20460
20461 Returns the token consumed, if the token had the appropriate type.
20462 Otherwise, returns NULL. */
20463
20464 static cp_token *
20465 cp_parser_require (cp_parser* parser,
20466 enum cpp_ttype type,
20467 required_token token_desc)
20468 {
20469 if (cp_lexer_next_token_is (parser->lexer, type))
20470 return cp_lexer_consume_token (parser->lexer);
20471 else
20472 {
20473 /* Output the MESSAGE -- unless we're parsing tentatively. */
20474 if (!cp_parser_simulate_error (parser))
20475 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20476 return NULL;
20477 }
20478 }
20479
20480 /* An error message is produced if the next token is not '>'.
20481 All further tokens are skipped until the desired token is
20482 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20483
20484 static void
20485 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20486 {
20487 /* Current level of '< ... >'. */
20488 unsigned level = 0;
20489 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20490 unsigned nesting_depth = 0;
20491
20492 /* Are we ready, yet? If not, issue error message. */
20493 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20494 return;
20495
20496 /* Skip tokens until the desired token is found. */
20497 while (true)
20498 {
20499 /* Peek at the next token. */
20500 switch (cp_lexer_peek_token (parser->lexer)->type)
20501 {
20502 case CPP_LESS:
20503 if (!nesting_depth)
20504 ++level;
20505 break;
20506
20507 case CPP_RSHIFT:
20508 if (cxx_dialect == cxx98)
20509 /* C++0x views the `>>' operator as two `>' tokens, but
20510 C++98 does not. */
20511 break;
20512 else if (!nesting_depth && level-- == 0)
20513 {
20514 /* We've hit a `>>' where the first `>' closes the
20515 template argument list, and the second `>' is
20516 spurious. Just consume the `>>' and stop; we've
20517 already produced at least one error. */
20518 cp_lexer_consume_token (parser->lexer);
20519 return;
20520 }
20521 /* Fall through for C++0x, so we handle the second `>' in
20522 the `>>'. */
20523
20524 case CPP_GREATER:
20525 if (!nesting_depth && level-- == 0)
20526 {
20527 /* We've reached the token we want, consume it and stop. */
20528 cp_lexer_consume_token (parser->lexer);
20529 return;
20530 }
20531 break;
20532
20533 case CPP_OPEN_PAREN:
20534 case CPP_OPEN_SQUARE:
20535 ++nesting_depth;
20536 break;
20537
20538 case CPP_CLOSE_PAREN:
20539 case CPP_CLOSE_SQUARE:
20540 if (nesting_depth-- == 0)
20541 return;
20542 break;
20543
20544 case CPP_EOF:
20545 case CPP_PRAGMA_EOL:
20546 case CPP_SEMICOLON:
20547 case CPP_OPEN_BRACE:
20548 case CPP_CLOSE_BRACE:
20549 /* The '>' was probably forgotten, don't look further. */
20550 return;
20551
20552 default:
20553 break;
20554 }
20555
20556 /* Consume this token. */
20557 cp_lexer_consume_token (parser->lexer);
20558 }
20559 }
20560
20561 /* If the next token is the indicated keyword, consume it. Otherwise,
20562 issue an error message indicating that TOKEN_DESC was expected.
20563
20564 Returns the token consumed, if the token had the appropriate type.
20565 Otherwise, returns NULL. */
20566
20567 static cp_token *
20568 cp_parser_require_keyword (cp_parser* parser,
20569 enum rid keyword,
20570 required_token token_desc)
20571 {
20572 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20573
20574 if (token && token->keyword != keyword)
20575 {
20576 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
20577 return NULL;
20578 }
20579
20580 return token;
20581 }
20582
20583 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20584 function-definition. */
20585
20586 static bool
20587 cp_parser_token_starts_function_definition_p (cp_token* token)
20588 {
20589 return (/* An ordinary function-body begins with an `{'. */
20590 token->type == CPP_OPEN_BRACE
20591 /* A ctor-initializer begins with a `:'. */
20592 || token->type == CPP_COLON
20593 /* A function-try-block begins with `try'. */
20594 || token->keyword == RID_TRY
20595 /* The named return value extension begins with `return'. */
20596 || token->keyword == RID_RETURN);
20597 }
20598
20599 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
20600 definition. */
20601
20602 static bool
20603 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
20604 {
20605 cp_token *token;
20606
20607 token = cp_lexer_peek_token (parser->lexer);
20608 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
20609 }
20610
20611 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
20612 C++0x) ending a template-argument. */
20613
20614 static bool
20615 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
20616 {
20617 cp_token *token;
20618
20619 token = cp_lexer_peek_token (parser->lexer);
20620 return (token->type == CPP_COMMA
20621 || token->type == CPP_GREATER
20622 || token->type == CPP_ELLIPSIS
20623 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
20624 }
20625
20626 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
20627 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
20628
20629 static bool
20630 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
20631 size_t n)
20632 {
20633 cp_token *token;
20634
20635 token = cp_lexer_peek_nth_token (parser->lexer, n);
20636 if (token->type == CPP_LESS)
20637 return true;
20638 /* Check for the sequence `<::' in the original code. It would be lexed as
20639 `[:', where `[' is a digraph, and there is no whitespace before
20640 `:'. */
20641 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
20642 {
20643 cp_token *token2;
20644 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
20645 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
20646 return true;
20647 }
20648 return false;
20649 }
20650
20651 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
20652 or none_type otherwise. */
20653
20654 static enum tag_types
20655 cp_parser_token_is_class_key (cp_token* token)
20656 {
20657 switch (token->keyword)
20658 {
20659 case RID_CLASS:
20660 return class_type;
20661 case RID_STRUCT:
20662 return record_type;
20663 case RID_UNION:
20664 return union_type;
20665
20666 default:
20667 return none_type;
20668 }
20669 }
20670
20671 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
20672
20673 static void
20674 cp_parser_check_class_key (enum tag_types class_key, tree type)
20675 {
20676 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
20677 permerror (input_location, "%qs tag used in naming %q#T",
20678 class_key == union_type ? "union"
20679 : class_key == record_type ? "struct" : "class",
20680 type);
20681 }
20682
20683 /* Issue an error message if DECL is redeclared with different
20684 access than its original declaration [class.access.spec/3].
20685 This applies to nested classes and nested class templates.
20686 [class.mem/1]. */
20687
20688 static void
20689 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
20690 {
20691 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
20692 return;
20693
20694 if ((TREE_PRIVATE (decl)
20695 != (current_access_specifier == access_private_node))
20696 || (TREE_PROTECTED (decl)
20697 != (current_access_specifier == access_protected_node)))
20698 error_at (location, "%qD redeclared with different access", decl);
20699 }
20700
20701 /* Look for the `template' keyword, as a syntactic disambiguator.
20702 Return TRUE iff it is present, in which case it will be
20703 consumed. */
20704
20705 static bool
20706 cp_parser_optional_template_keyword (cp_parser *parser)
20707 {
20708 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20709 {
20710 /* The `template' keyword can only be used within templates;
20711 outside templates the parser can always figure out what is a
20712 template and what is not. */
20713 if (!processing_template_decl)
20714 {
20715 cp_token *token = cp_lexer_peek_token (parser->lexer);
20716 error_at (token->location,
20717 "%<template%> (as a disambiguator) is only allowed "
20718 "within templates");
20719 /* If this part of the token stream is rescanned, the same
20720 error message would be generated. So, we purge the token
20721 from the stream. */
20722 cp_lexer_purge_token (parser->lexer);
20723 return false;
20724 }
20725 else
20726 {
20727 /* Consume the `template' keyword. */
20728 cp_lexer_consume_token (parser->lexer);
20729 return true;
20730 }
20731 }
20732
20733 return false;
20734 }
20735
20736 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
20737 set PARSER->SCOPE, and perform other related actions. */
20738
20739 static void
20740 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
20741 {
20742 int i;
20743 struct tree_check *check_value;
20744 deferred_access_check *chk;
20745 VEC (deferred_access_check,gc) *checks;
20746
20747 /* Get the stored value. */
20748 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
20749 /* Perform any access checks that were deferred. */
20750 checks = check_value->checks;
20751 if (checks)
20752 {
20753 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
20754 perform_or_defer_access_check (chk->binfo,
20755 chk->decl,
20756 chk->diag_decl);
20757 }
20758 /* Set the scope from the stored value. */
20759 parser->scope = check_value->value;
20760 parser->qualifying_scope = check_value->qualifying_scope;
20761 parser->object_scope = NULL_TREE;
20762 }
20763
20764 /* Consume tokens up through a non-nested END token. Returns TRUE if we
20765 encounter the end of a block before what we were looking for. */
20766
20767 static bool
20768 cp_parser_cache_group (cp_parser *parser,
20769 enum cpp_ttype end,
20770 unsigned depth)
20771 {
20772 while (true)
20773 {
20774 cp_token *token = cp_lexer_peek_token (parser->lexer);
20775
20776 /* Abort a parenthesized expression if we encounter a semicolon. */
20777 if ((end == CPP_CLOSE_PAREN || depth == 0)
20778 && token->type == CPP_SEMICOLON)
20779 return true;
20780 /* If we've reached the end of the file, stop. */
20781 if (token->type == CPP_EOF
20782 || (end != CPP_PRAGMA_EOL
20783 && token->type == CPP_PRAGMA_EOL))
20784 return true;
20785 if (token->type == CPP_CLOSE_BRACE && depth == 0)
20786 /* We've hit the end of an enclosing block, so there's been some
20787 kind of syntax error. */
20788 return true;
20789
20790 /* Consume the token. */
20791 cp_lexer_consume_token (parser->lexer);
20792 /* See if it starts a new group. */
20793 if (token->type == CPP_OPEN_BRACE)
20794 {
20795 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
20796 /* In theory this should probably check end == '}', but
20797 cp_parser_save_member_function_body needs it to exit
20798 after either '}' or ')' when called with ')'. */
20799 if (depth == 0)
20800 return false;
20801 }
20802 else if (token->type == CPP_OPEN_PAREN)
20803 {
20804 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
20805 if (depth == 0 && end == CPP_CLOSE_PAREN)
20806 return false;
20807 }
20808 else if (token->type == CPP_PRAGMA)
20809 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
20810 else if (token->type == end)
20811 return false;
20812 }
20813 }
20814
20815 /* Begin parsing tentatively. We always save tokens while parsing
20816 tentatively so that if the tentative parsing fails we can restore the
20817 tokens. */
20818
20819 static void
20820 cp_parser_parse_tentatively (cp_parser* parser)
20821 {
20822 /* Enter a new parsing context. */
20823 parser->context = cp_parser_context_new (parser->context);
20824 /* Begin saving tokens. */
20825 cp_lexer_save_tokens (parser->lexer);
20826 /* In order to avoid repetitive access control error messages,
20827 access checks are queued up until we are no longer parsing
20828 tentatively. */
20829 push_deferring_access_checks (dk_deferred);
20830 }
20831
20832 /* Commit to the currently active tentative parse. */
20833
20834 static void
20835 cp_parser_commit_to_tentative_parse (cp_parser* parser)
20836 {
20837 cp_parser_context *context;
20838 cp_lexer *lexer;
20839
20840 /* Mark all of the levels as committed. */
20841 lexer = parser->lexer;
20842 for (context = parser->context; context->next; context = context->next)
20843 {
20844 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
20845 break;
20846 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
20847 while (!cp_lexer_saving_tokens (lexer))
20848 lexer = lexer->next;
20849 cp_lexer_commit_tokens (lexer);
20850 }
20851 }
20852
20853 /* Abort the currently active tentative parse. All consumed tokens
20854 will be rolled back, and no diagnostics will be issued. */
20855
20856 static void
20857 cp_parser_abort_tentative_parse (cp_parser* parser)
20858 {
20859 cp_parser_simulate_error (parser);
20860 /* Now, pretend that we want to see if the construct was
20861 successfully parsed. */
20862 cp_parser_parse_definitely (parser);
20863 }
20864
20865 /* Stop parsing tentatively. If a parse error has occurred, restore the
20866 token stream. Otherwise, commit to the tokens we have consumed.
20867 Returns true if no error occurred; false otherwise. */
20868
20869 static bool
20870 cp_parser_parse_definitely (cp_parser* parser)
20871 {
20872 bool error_occurred;
20873 cp_parser_context *context;
20874
20875 /* Remember whether or not an error occurred, since we are about to
20876 destroy that information. */
20877 error_occurred = cp_parser_error_occurred (parser);
20878 /* Remove the topmost context from the stack. */
20879 context = parser->context;
20880 parser->context = context->next;
20881 /* If no parse errors occurred, commit to the tentative parse. */
20882 if (!error_occurred)
20883 {
20884 /* Commit to the tokens read tentatively, unless that was
20885 already done. */
20886 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20887 cp_lexer_commit_tokens (parser->lexer);
20888
20889 pop_to_parent_deferring_access_checks ();
20890 }
20891 /* Otherwise, if errors occurred, roll back our state so that things
20892 are just as they were before we began the tentative parse. */
20893 else
20894 {
20895 cp_lexer_rollback_tokens (parser->lexer);
20896 pop_deferring_access_checks ();
20897 }
20898 /* Add the context to the front of the free list. */
20899 context->next = cp_parser_context_free_list;
20900 cp_parser_context_free_list = context;
20901
20902 return !error_occurred;
20903 }
20904
20905 /* Returns true if we are parsing tentatively and are not committed to
20906 this tentative parse. */
20907
20908 static bool
20909 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20910 {
20911 return (cp_parser_parsing_tentatively (parser)
20912 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20913 }
20914
20915 /* Returns nonzero iff an error has occurred during the most recent
20916 tentative parse. */
20917
20918 static bool
20919 cp_parser_error_occurred (cp_parser* parser)
20920 {
20921 return (cp_parser_parsing_tentatively (parser)
20922 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20923 }
20924
20925 /* Returns nonzero if GNU extensions are allowed. */
20926
20927 static bool
20928 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20929 {
20930 return parser->allow_gnu_extensions_p;
20931 }
20932 \f
20933 /* Objective-C++ Productions */
20934
20935
20936 /* Parse an Objective-C expression, which feeds into a primary-expression
20937 above.
20938
20939 objc-expression:
20940 objc-message-expression
20941 objc-string-literal
20942 objc-encode-expression
20943 objc-protocol-expression
20944 objc-selector-expression
20945
20946 Returns a tree representation of the expression. */
20947
20948 static tree
20949 cp_parser_objc_expression (cp_parser* parser)
20950 {
20951 /* Try to figure out what kind of declaration is present. */
20952 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20953
20954 switch (kwd->type)
20955 {
20956 case CPP_OPEN_SQUARE:
20957 return cp_parser_objc_message_expression (parser);
20958
20959 case CPP_OBJC_STRING:
20960 kwd = cp_lexer_consume_token (parser->lexer);
20961 return objc_build_string_object (kwd->u.value);
20962
20963 case CPP_KEYWORD:
20964 switch (kwd->keyword)
20965 {
20966 case RID_AT_ENCODE:
20967 return cp_parser_objc_encode_expression (parser);
20968
20969 case RID_AT_PROTOCOL:
20970 return cp_parser_objc_protocol_expression (parser);
20971
20972 case RID_AT_SELECTOR:
20973 return cp_parser_objc_selector_expression (parser);
20974
20975 default:
20976 break;
20977 }
20978 default:
20979 error_at (kwd->location,
20980 "misplaced %<@%D%> Objective-C++ construct",
20981 kwd->u.value);
20982 cp_parser_skip_to_end_of_block_or_statement (parser);
20983 }
20984
20985 return error_mark_node;
20986 }
20987
20988 /* Parse an Objective-C message expression.
20989
20990 objc-message-expression:
20991 [ objc-message-receiver objc-message-args ]
20992
20993 Returns a representation of an Objective-C message. */
20994
20995 static tree
20996 cp_parser_objc_message_expression (cp_parser* parser)
20997 {
20998 tree receiver, messageargs;
20999
21000 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
21001 receiver = cp_parser_objc_message_receiver (parser);
21002 messageargs = cp_parser_objc_message_args (parser);
21003 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21004
21005 return objc_build_message_expr (build_tree_list (receiver, messageargs));
21006 }
21007
21008 /* Parse an objc-message-receiver.
21009
21010 objc-message-receiver:
21011 expression
21012 simple-type-specifier
21013
21014 Returns a representation of the type or expression. */
21015
21016 static tree
21017 cp_parser_objc_message_receiver (cp_parser* parser)
21018 {
21019 tree rcv;
21020
21021 /* An Objective-C message receiver may be either (1) a type
21022 or (2) an expression. */
21023 cp_parser_parse_tentatively (parser);
21024 rcv = cp_parser_expression (parser, false, NULL);
21025
21026 if (cp_parser_parse_definitely (parser))
21027 return rcv;
21028
21029 rcv = cp_parser_simple_type_specifier (parser,
21030 /*decl_specs=*/NULL,
21031 CP_PARSER_FLAGS_NONE);
21032
21033 return objc_get_class_reference (rcv);
21034 }
21035
21036 /* Parse the arguments and selectors comprising an Objective-C message.
21037
21038 objc-message-args:
21039 objc-selector
21040 objc-selector-args
21041 objc-selector-args , objc-comma-args
21042
21043 objc-selector-args:
21044 objc-selector [opt] : assignment-expression
21045 objc-selector-args objc-selector [opt] : assignment-expression
21046
21047 objc-comma-args:
21048 assignment-expression
21049 objc-comma-args , assignment-expression
21050
21051 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21052 selector arguments and TREE_VALUE containing a list of comma
21053 arguments. */
21054
21055 static tree
21056 cp_parser_objc_message_args (cp_parser* parser)
21057 {
21058 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21059 bool maybe_unary_selector_p = true;
21060 cp_token *token = cp_lexer_peek_token (parser->lexer);
21061
21062 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21063 {
21064 tree selector = NULL_TREE, arg;
21065
21066 if (token->type != CPP_COLON)
21067 selector = cp_parser_objc_selector (parser);
21068
21069 /* Detect if we have a unary selector. */
21070 if (maybe_unary_selector_p
21071 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21072 return build_tree_list (selector, NULL_TREE);
21073
21074 maybe_unary_selector_p = false;
21075 cp_parser_require (parser, CPP_COLON, RT_COLON);
21076 arg = cp_parser_assignment_expression (parser, false, NULL);
21077
21078 sel_args
21079 = chainon (sel_args,
21080 build_tree_list (selector, arg));
21081
21082 token = cp_lexer_peek_token (parser->lexer);
21083 }
21084
21085 /* Handle non-selector arguments, if any. */
21086 while (token->type == CPP_COMMA)
21087 {
21088 tree arg;
21089
21090 cp_lexer_consume_token (parser->lexer);
21091 arg = cp_parser_assignment_expression (parser, false, NULL);
21092
21093 addl_args
21094 = chainon (addl_args,
21095 build_tree_list (NULL_TREE, arg));
21096
21097 token = cp_lexer_peek_token (parser->lexer);
21098 }
21099
21100 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21101 {
21102 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21103 return build_tree_list (error_mark_node, error_mark_node);
21104 }
21105
21106 return build_tree_list (sel_args, addl_args);
21107 }
21108
21109 /* Parse an Objective-C encode expression.
21110
21111 objc-encode-expression:
21112 @encode objc-typename
21113
21114 Returns an encoded representation of the type argument. */
21115
21116 static tree
21117 cp_parser_objc_encode_expression (cp_parser* parser)
21118 {
21119 tree type;
21120 cp_token *token;
21121
21122 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
21123 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21124 token = cp_lexer_peek_token (parser->lexer);
21125 type = complete_type (cp_parser_type_id (parser));
21126 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21127
21128 if (!type)
21129 {
21130 error_at (token->location,
21131 "%<@encode%> must specify a type as an argument");
21132 return error_mark_node;
21133 }
21134
21135 /* This happens if we find @encode(T) (where T is a template
21136 typename or something dependent on a template typename) when
21137 parsing a template. In that case, we can't compile it
21138 immediately, but we rather create an AT_ENCODE_EXPR which will
21139 need to be instantiated when the template is used.
21140 */
21141 if (dependent_type_p (type))
21142 {
21143 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21144 TREE_READONLY (value) = 1;
21145 return value;
21146 }
21147
21148 return objc_build_encode_expr (type);
21149 }
21150
21151 /* Parse an Objective-C @defs expression. */
21152
21153 static tree
21154 cp_parser_objc_defs_expression (cp_parser *parser)
21155 {
21156 tree name;
21157
21158 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
21159 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21160 name = cp_parser_identifier (parser);
21161 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21162
21163 return objc_get_class_ivars (name);
21164 }
21165
21166 /* Parse an Objective-C protocol expression.
21167
21168 objc-protocol-expression:
21169 @protocol ( identifier )
21170
21171 Returns a representation of the protocol expression. */
21172
21173 static tree
21174 cp_parser_objc_protocol_expression (cp_parser* parser)
21175 {
21176 tree proto;
21177
21178 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21179 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21180 proto = cp_parser_identifier (parser);
21181 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21182
21183 return objc_build_protocol_expr (proto);
21184 }
21185
21186 /* Parse an Objective-C selector expression.
21187
21188 objc-selector-expression:
21189 @selector ( objc-method-signature )
21190
21191 objc-method-signature:
21192 objc-selector
21193 objc-selector-seq
21194
21195 objc-selector-seq:
21196 objc-selector :
21197 objc-selector-seq objc-selector :
21198
21199 Returns a representation of the method selector. */
21200
21201 static tree
21202 cp_parser_objc_selector_expression (cp_parser* parser)
21203 {
21204 tree sel_seq = NULL_TREE;
21205 bool maybe_unary_selector_p = true;
21206 cp_token *token;
21207 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21208
21209 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
21210 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21211 token = cp_lexer_peek_token (parser->lexer);
21212
21213 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21214 || token->type == CPP_SCOPE)
21215 {
21216 tree selector = NULL_TREE;
21217
21218 if (token->type != CPP_COLON
21219 || token->type == CPP_SCOPE)
21220 selector = cp_parser_objc_selector (parser);
21221
21222 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21223 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21224 {
21225 /* Detect if we have a unary selector. */
21226 if (maybe_unary_selector_p)
21227 {
21228 sel_seq = selector;
21229 goto finish_selector;
21230 }
21231 else
21232 {
21233 cp_parser_error (parser, "expected %<:%>");
21234 }
21235 }
21236 maybe_unary_selector_p = false;
21237 token = cp_lexer_consume_token (parser->lexer);
21238
21239 if (token->type == CPP_SCOPE)
21240 {
21241 sel_seq
21242 = chainon (sel_seq,
21243 build_tree_list (selector, NULL_TREE));
21244 sel_seq
21245 = chainon (sel_seq,
21246 build_tree_list (NULL_TREE, NULL_TREE));
21247 }
21248 else
21249 sel_seq
21250 = chainon (sel_seq,
21251 build_tree_list (selector, NULL_TREE));
21252
21253 token = cp_lexer_peek_token (parser->lexer);
21254 }
21255
21256 finish_selector:
21257 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21258
21259 return objc_build_selector_expr (loc, sel_seq);
21260 }
21261
21262 /* Parse a list of identifiers.
21263
21264 objc-identifier-list:
21265 identifier
21266 objc-identifier-list , identifier
21267
21268 Returns a TREE_LIST of identifier nodes. */
21269
21270 static tree
21271 cp_parser_objc_identifier_list (cp_parser* parser)
21272 {
21273 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
21274 cp_token *sep = cp_lexer_peek_token (parser->lexer);
21275
21276 while (sep->type == CPP_COMMA)
21277 {
21278 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21279 list = chainon (list,
21280 build_tree_list (NULL_TREE,
21281 cp_parser_identifier (parser)));
21282 sep = cp_lexer_peek_token (parser->lexer);
21283 }
21284
21285 return list;
21286 }
21287
21288 /* Parse an Objective-C alias declaration.
21289
21290 objc-alias-declaration:
21291 @compatibility_alias identifier identifier ;
21292
21293 This function registers the alias mapping with the Objective-C front end.
21294 It returns nothing. */
21295
21296 static void
21297 cp_parser_objc_alias_declaration (cp_parser* parser)
21298 {
21299 tree alias, orig;
21300
21301 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
21302 alias = cp_parser_identifier (parser);
21303 orig = cp_parser_identifier (parser);
21304 objc_declare_alias (alias, orig);
21305 cp_parser_consume_semicolon_at_end_of_statement (parser);
21306 }
21307
21308 /* Parse an Objective-C class forward-declaration.
21309
21310 objc-class-declaration:
21311 @class objc-identifier-list ;
21312
21313 The function registers the forward declarations with the Objective-C
21314 front end. It returns nothing. */
21315
21316 static void
21317 cp_parser_objc_class_declaration (cp_parser* parser)
21318 {
21319 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
21320 objc_declare_class (cp_parser_objc_identifier_list (parser));
21321 cp_parser_consume_semicolon_at_end_of_statement (parser);
21322 }
21323
21324 /* Parse a list of Objective-C protocol references.
21325
21326 objc-protocol-refs-opt:
21327 objc-protocol-refs [opt]
21328
21329 objc-protocol-refs:
21330 < objc-identifier-list >
21331
21332 Returns a TREE_LIST of identifiers, if any. */
21333
21334 static tree
21335 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21336 {
21337 tree protorefs = NULL_TREE;
21338
21339 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21340 {
21341 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
21342 protorefs = cp_parser_objc_identifier_list (parser);
21343 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21344 }
21345
21346 return protorefs;
21347 }
21348
21349 /* Parse a Objective-C visibility specification. */
21350
21351 static void
21352 cp_parser_objc_visibility_spec (cp_parser* parser)
21353 {
21354 cp_token *vis = cp_lexer_peek_token (parser->lexer);
21355
21356 switch (vis->keyword)
21357 {
21358 case RID_AT_PRIVATE:
21359 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21360 break;
21361 case RID_AT_PROTECTED:
21362 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21363 break;
21364 case RID_AT_PUBLIC:
21365 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21366 break;
21367 case RID_AT_PACKAGE:
21368 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21369 break;
21370 default:
21371 return;
21372 }
21373
21374 /* Eat '@private'/'@protected'/'@public'. */
21375 cp_lexer_consume_token (parser->lexer);
21376 }
21377
21378 /* Parse an Objective-C method type. */
21379
21380 static void
21381 cp_parser_objc_method_type (cp_parser* parser)
21382 {
21383 objc_set_method_type
21384 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
21385 ? PLUS_EXPR
21386 : MINUS_EXPR);
21387 }
21388
21389 /* Parse an Objective-C protocol qualifier. */
21390
21391 static tree
21392 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21393 {
21394 tree quals = NULL_TREE, node;
21395 cp_token *token = cp_lexer_peek_token (parser->lexer);
21396
21397 node = token->u.value;
21398
21399 while (node && TREE_CODE (node) == IDENTIFIER_NODE
21400 && (node == ridpointers [(int) RID_IN]
21401 || node == ridpointers [(int) RID_OUT]
21402 || node == ridpointers [(int) RID_INOUT]
21403 || node == ridpointers [(int) RID_BYCOPY]
21404 || node == ridpointers [(int) RID_BYREF]
21405 || node == ridpointers [(int) RID_ONEWAY]))
21406 {
21407 quals = tree_cons (NULL_TREE, node, quals);
21408 cp_lexer_consume_token (parser->lexer);
21409 token = cp_lexer_peek_token (parser->lexer);
21410 node = token->u.value;
21411 }
21412
21413 return quals;
21414 }
21415
21416 /* Parse an Objective-C typename. */
21417
21418 static tree
21419 cp_parser_objc_typename (cp_parser* parser)
21420 {
21421 tree type_name = NULL_TREE;
21422
21423 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21424 {
21425 tree proto_quals, cp_type = NULL_TREE;
21426
21427 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21428 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21429
21430 /* An ObjC type name may consist of just protocol qualifiers, in which
21431 case the type shall default to 'id'. */
21432 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21433 cp_type = cp_parser_type_id (parser);
21434
21435 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21436 type_name = build_tree_list (proto_quals, cp_type);
21437 }
21438
21439 return type_name;
21440 }
21441
21442 /* Check to see if TYPE refers to an Objective-C selector name. */
21443
21444 static bool
21445 cp_parser_objc_selector_p (enum cpp_ttype type)
21446 {
21447 return (type == CPP_NAME || type == CPP_KEYWORD
21448 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21449 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21450 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21451 || type == CPP_XOR || type == CPP_XOR_EQ);
21452 }
21453
21454 /* Parse an Objective-C selector. */
21455
21456 static tree
21457 cp_parser_objc_selector (cp_parser* parser)
21458 {
21459 cp_token *token = cp_lexer_consume_token (parser->lexer);
21460
21461 if (!cp_parser_objc_selector_p (token->type))
21462 {
21463 error_at (token->location, "invalid Objective-C++ selector name");
21464 return error_mark_node;
21465 }
21466
21467 /* C++ operator names are allowed to appear in ObjC selectors. */
21468 switch (token->type)
21469 {
21470 case CPP_AND_AND: return get_identifier ("and");
21471 case CPP_AND_EQ: return get_identifier ("and_eq");
21472 case CPP_AND: return get_identifier ("bitand");
21473 case CPP_OR: return get_identifier ("bitor");
21474 case CPP_COMPL: return get_identifier ("compl");
21475 case CPP_NOT: return get_identifier ("not");
21476 case CPP_NOT_EQ: return get_identifier ("not_eq");
21477 case CPP_OR_OR: return get_identifier ("or");
21478 case CPP_OR_EQ: return get_identifier ("or_eq");
21479 case CPP_XOR: return get_identifier ("xor");
21480 case CPP_XOR_EQ: return get_identifier ("xor_eq");
21481 default: return token->u.value;
21482 }
21483 }
21484
21485 /* Parse an Objective-C params list. */
21486
21487 static tree
21488 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21489 {
21490 tree params = NULL_TREE;
21491 bool maybe_unary_selector_p = true;
21492 cp_token *token = cp_lexer_peek_token (parser->lexer);
21493
21494 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21495 {
21496 tree selector = NULL_TREE, type_name, identifier;
21497 tree parm_attr = NULL_TREE;
21498
21499 if (token->keyword == RID_ATTRIBUTE)
21500 break;
21501
21502 if (token->type != CPP_COLON)
21503 selector = cp_parser_objc_selector (parser);
21504
21505 /* Detect if we have a unary selector. */
21506 if (maybe_unary_selector_p
21507 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21508 {
21509 params = selector; /* Might be followed by attributes. */
21510 break;
21511 }
21512
21513 maybe_unary_selector_p = false;
21514 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
21515 {
21516 /* Something went quite wrong. There should be a colon
21517 here, but there is not. Stop parsing parameters. */
21518 break;
21519 }
21520 type_name = cp_parser_objc_typename (parser);
21521 /* New ObjC allows attributes on parameters too. */
21522 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
21523 parm_attr = cp_parser_attributes_opt (parser);
21524 identifier = cp_parser_identifier (parser);
21525
21526 params
21527 = chainon (params,
21528 objc_build_keyword_decl (selector,
21529 type_name,
21530 identifier,
21531 parm_attr));
21532
21533 token = cp_lexer_peek_token (parser->lexer);
21534 }
21535
21536 if (params == NULL_TREE)
21537 {
21538 cp_parser_error (parser, "objective-c++ method declaration is expected");
21539 return error_mark_node;
21540 }
21541
21542 /* We allow tail attributes for the method. */
21543 if (token->keyword == RID_ATTRIBUTE)
21544 {
21545 *attributes = cp_parser_attributes_opt (parser);
21546 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21547 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21548 return params;
21549 cp_parser_error (parser,
21550 "method attributes must be specified at the end");
21551 return error_mark_node;
21552 }
21553
21554 if (params == NULL_TREE)
21555 {
21556 cp_parser_error (parser, "objective-c++ method declaration is expected");
21557 return error_mark_node;
21558 }
21559 return params;
21560 }
21561
21562 /* Parse the non-keyword Objective-C params. */
21563
21564 static tree
21565 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
21566 tree* attributes)
21567 {
21568 tree params = make_node (TREE_LIST);
21569 cp_token *token = cp_lexer_peek_token (parser->lexer);
21570 *ellipsisp = false; /* Initially, assume no ellipsis. */
21571
21572 while (token->type == CPP_COMMA)
21573 {
21574 cp_parameter_declarator *parmdecl;
21575 tree parm;
21576
21577 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21578 token = cp_lexer_peek_token (parser->lexer);
21579
21580 if (token->type == CPP_ELLIPSIS)
21581 {
21582 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
21583 *ellipsisp = true;
21584 token = cp_lexer_peek_token (parser->lexer);
21585 break;
21586 }
21587
21588 /* TODO: parse attributes for tail parameters. */
21589 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21590 parm = grokdeclarator (parmdecl->declarator,
21591 &parmdecl->decl_specifiers,
21592 PARM, /*initialized=*/0,
21593 /*attrlist=*/NULL);
21594
21595 chainon (params, build_tree_list (NULL_TREE, parm));
21596 token = cp_lexer_peek_token (parser->lexer);
21597 }
21598
21599 /* We allow tail attributes for the method. */
21600 if (token->keyword == RID_ATTRIBUTE)
21601 {
21602 if (*attributes == NULL_TREE)
21603 {
21604 *attributes = cp_parser_attributes_opt (parser);
21605 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21606 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21607 return params;
21608 }
21609 else
21610 /* We have an error, but parse the attributes, so that we can
21611 carry on. */
21612 *attributes = cp_parser_attributes_opt (parser);
21613
21614 cp_parser_error (parser,
21615 "method attributes must be specified at the end");
21616 return error_mark_node;
21617 }
21618
21619 return params;
21620 }
21621
21622 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
21623
21624 static void
21625 cp_parser_objc_interstitial_code (cp_parser* parser)
21626 {
21627 cp_token *token = cp_lexer_peek_token (parser->lexer);
21628
21629 /* If the next token is `extern' and the following token is a string
21630 literal, then we have a linkage specification. */
21631 if (token->keyword == RID_EXTERN
21632 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
21633 cp_parser_linkage_specification (parser);
21634 /* Handle #pragma, if any. */
21635 else if (token->type == CPP_PRAGMA)
21636 cp_parser_pragma (parser, pragma_external);
21637 /* Allow stray semicolons. */
21638 else if (token->type == CPP_SEMICOLON)
21639 cp_lexer_consume_token (parser->lexer);
21640 /* Mark methods as optional or required, when building protocols. */
21641 else if (token->keyword == RID_AT_OPTIONAL)
21642 {
21643 cp_lexer_consume_token (parser->lexer);
21644 objc_set_method_opt (true);
21645 }
21646 else if (token->keyword == RID_AT_REQUIRED)
21647 {
21648 cp_lexer_consume_token (parser->lexer);
21649 objc_set_method_opt (false);
21650 }
21651 else if (token->keyword == RID_NAMESPACE)
21652 cp_parser_namespace_definition (parser);
21653 /* Other stray characters must generate errors. */
21654 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
21655 {
21656 cp_lexer_consume_token (parser->lexer);
21657 error ("stray `%s' between Objective-C++ methods",
21658 token->type == CPP_OPEN_BRACE ? "{" : "}");
21659 }
21660 /* Finally, try to parse a block-declaration, or a function-definition. */
21661 else
21662 cp_parser_block_declaration (parser, /*statement_p=*/false);
21663 }
21664
21665 /* Parse a method signature. */
21666
21667 static tree
21668 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
21669 {
21670 tree rettype, kwdparms, optparms;
21671 bool ellipsis = false;
21672
21673 cp_parser_objc_method_type (parser);
21674 rettype = cp_parser_objc_typename (parser);
21675 *attributes = NULL_TREE;
21676 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
21677 if (kwdparms == error_mark_node)
21678 return error_mark_node;
21679 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
21680 if (optparms == error_mark_node)
21681 return error_mark_node;
21682
21683 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
21684 }
21685
21686 static bool
21687 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
21688 {
21689 tree tattr;
21690 cp_lexer_save_tokens (parser->lexer);
21691 tattr = cp_parser_attributes_opt (parser);
21692 gcc_assert (tattr) ;
21693
21694 /* If the attributes are followed by a method introducer, this is not allowed.
21695 Dump the attributes and flag the situation. */
21696 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
21697 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
21698 return true;
21699
21700 /* Otherwise, the attributes introduce some interstitial code, possibly so
21701 rewind to allow that check. */
21702 cp_lexer_rollback_tokens (parser->lexer);
21703 return false;
21704 }
21705
21706 /* Parse an Objective-C method prototype list. */
21707
21708 static void
21709 cp_parser_objc_method_prototype_list (cp_parser* parser)
21710 {
21711 cp_token *token = cp_lexer_peek_token (parser->lexer);
21712
21713 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
21714 {
21715 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21716 {
21717 tree attributes, sig;
21718 sig = cp_parser_objc_method_signature (parser, &attributes);
21719 if (sig == error_mark_node)
21720 {
21721 cp_parser_skip_to_end_of_block_or_statement (parser);
21722 token = cp_lexer_peek_token (parser->lexer);
21723 continue;
21724 }
21725 objc_add_method_declaration (sig, attributes);
21726 cp_parser_consume_semicolon_at_end_of_statement (parser);
21727 }
21728 else if (token->keyword == RID_AT_PROPERTY)
21729 cp_parser_objc_at_property (parser);
21730 else if (token->keyword == RID_ATTRIBUTE
21731 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
21732 warning_at (cp_lexer_peek_token (parser->lexer)->location,
21733 OPT_Wattributes,
21734 "prefix attributes are ignored for methods");
21735 else
21736 /* Allow for interspersed non-ObjC++ code. */
21737 cp_parser_objc_interstitial_code (parser);
21738
21739 token = cp_lexer_peek_token (parser->lexer);
21740 }
21741
21742 if (token->type != CPP_EOF)
21743 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21744 else
21745 cp_parser_error (parser, "expected %<@end%>");
21746
21747 objc_finish_interface ();
21748 }
21749
21750 /* Parse an Objective-C method definition list. */
21751
21752 static void
21753 cp_parser_objc_method_definition_list (cp_parser* parser)
21754 {
21755 cp_token *token = cp_lexer_peek_token (parser->lexer);
21756
21757 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
21758 {
21759 tree meth;
21760
21761 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21762 {
21763 cp_token *ptk;
21764 tree sig, attribute;
21765 push_deferring_access_checks (dk_deferred);
21766 sig = cp_parser_objc_method_signature (parser, &attribute);
21767 if (sig == error_mark_node)
21768 {
21769 cp_parser_skip_to_end_of_block_or_statement (parser);
21770 token = cp_lexer_peek_token (parser->lexer);
21771 continue;
21772 }
21773 objc_start_method_definition (sig, attribute);
21774
21775 /* For historical reasons, we accept an optional semicolon. */
21776 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21777 cp_lexer_consume_token (parser->lexer);
21778
21779 ptk = cp_lexer_peek_token (parser->lexer);
21780 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
21781 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
21782 {
21783 perform_deferred_access_checks ();
21784 stop_deferring_access_checks ();
21785 meth = cp_parser_function_definition_after_declarator (parser,
21786 false);
21787 pop_deferring_access_checks ();
21788 objc_finish_method_definition (meth);
21789 }
21790 }
21791 else if (token->keyword == RID_AT_PROPERTY)
21792 cp_parser_objc_at_property (parser);
21793 else if (token->keyword == RID_ATTRIBUTE
21794 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
21795 warning_at (token->location, OPT_Wattributes,
21796 "prefix attributes are ignored for methods");
21797 else
21798 /* Allow for interspersed non-ObjC++ code. */
21799 cp_parser_objc_interstitial_code (parser);
21800
21801 token = cp_lexer_peek_token (parser->lexer);
21802 }
21803
21804 if (token->type != CPP_EOF)
21805 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21806 else
21807 cp_parser_error (parser, "expected %<@end%>");
21808
21809 objc_finish_implementation ();
21810 }
21811
21812 /* Parse Objective-C ivars. */
21813
21814 static void
21815 cp_parser_objc_class_ivars (cp_parser* parser)
21816 {
21817 cp_token *token = cp_lexer_peek_token (parser->lexer);
21818
21819 if (token->type != CPP_OPEN_BRACE)
21820 return; /* No ivars specified. */
21821
21822 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
21823 token = cp_lexer_peek_token (parser->lexer);
21824
21825 while (token->type != CPP_CLOSE_BRACE
21826 && token->keyword != RID_AT_END && token->type != CPP_EOF)
21827 {
21828 cp_decl_specifier_seq declspecs;
21829 int decl_class_or_enum_p;
21830 tree prefix_attributes;
21831
21832 cp_parser_objc_visibility_spec (parser);
21833
21834 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21835 break;
21836
21837 cp_parser_decl_specifier_seq (parser,
21838 CP_PARSER_FLAGS_OPTIONAL,
21839 &declspecs,
21840 &decl_class_or_enum_p);
21841 prefix_attributes = declspecs.attributes;
21842 declspecs.attributes = NULL_TREE;
21843
21844 /* Keep going until we hit the `;' at the end of the
21845 declaration. */
21846 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21847 {
21848 tree width = NULL_TREE, attributes, first_attribute, decl;
21849 cp_declarator *declarator = NULL;
21850 int ctor_dtor_or_conv_p;
21851
21852 /* Check for a (possibly unnamed) bitfield declaration. */
21853 token = cp_lexer_peek_token (parser->lexer);
21854 if (token->type == CPP_COLON)
21855 goto eat_colon;
21856
21857 if (token->type == CPP_NAME
21858 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21859 == CPP_COLON))
21860 {
21861 /* Get the name of the bitfield. */
21862 declarator = make_id_declarator (NULL_TREE,
21863 cp_parser_identifier (parser),
21864 sfk_none);
21865
21866 eat_colon:
21867 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
21868 /* Get the width of the bitfield. */
21869 width
21870 = cp_parser_constant_expression (parser,
21871 /*allow_non_constant=*/false,
21872 NULL);
21873 }
21874 else
21875 {
21876 /* Parse the declarator. */
21877 declarator
21878 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21879 &ctor_dtor_or_conv_p,
21880 /*parenthesized_p=*/NULL,
21881 /*member_p=*/false);
21882 }
21883
21884 /* Look for attributes that apply to the ivar. */
21885 attributes = cp_parser_attributes_opt (parser);
21886 /* Remember which attributes are prefix attributes and
21887 which are not. */
21888 first_attribute = attributes;
21889 /* Combine the attributes. */
21890 attributes = chainon (prefix_attributes, attributes);
21891
21892 if (width)
21893 /* Create the bitfield declaration. */
21894 decl = grokbitfield (declarator, &declspecs,
21895 width,
21896 attributes);
21897 else
21898 decl = grokfield (declarator, &declspecs,
21899 NULL_TREE, /*init_const_expr_p=*/false,
21900 NULL_TREE, attributes);
21901
21902 /* Add the instance variable. */
21903 objc_add_instance_variable (decl);
21904
21905 /* Reset PREFIX_ATTRIBUTES. */
21906 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21907 attributes = TREE_CHAIN (attributes);
21908 if (attributes)
21909 TREE_CHAIN (attributes) = NULL_TREE;
21910
21911 token = cp_lexer_peek_token (parser->lexer);
21912
21913 if (token->type == CPP_COMMA)
21914 {
21915 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21916 continue;
21917 }
21918 break;
21919 }
21920
21921 cp_parser_consume_semicolon_at_end_of_statement (parser);
21922 token = cp_lexer_peek_token (parser->lexer);
21923 }
21924
21925 if (token->keyword == RID_AT_END)
21926 cp_parser_error (parser, "expected %<}%>");
21927
21928 /* Do not consume the RID_AT_END, so it will be read again as terminating
21929 the @interface of @implementation. */
21930 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
21931 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
21932
21933 /* For historical reasons, we accept an optional semicolon. */
21934 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21935 cp_lexer_consume_token (parser->lexer);
21936 }
21937
21938 /* Parse an Objective-C protocol declaration. */
21939
21940 static void
21941 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
21942 {
21943 tree proto, protorefs;
21944 cp_token *tok;
21945
21946 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21947 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
21948 {
21949 tok = cp_lexer_peek_token (parser->lexer);
21950 error_at (tok->location, "identifier expected after %<@protocol%>");
21951 goto finish;
21952 }
21953
21954 /* See if we have a forward declaration or a definition. */
21955 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
21956
21957 /* Try a forward declaration first. */
21958 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
21959 {
21960 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
21961 finish:
21962 cp_parser_consume_semicolon_at_end_of_statement (parser);
21963 }
21964
21965 /* Ok, we got a full-fledged definition (or at least should). */
21966 else
21967 {
21968 proto = cp_parser_identifier (parser);
21969 protorefs = cp_parser_objc_protocol_refs_opt (parser);
21970 objc_start_protocol (proto, protorefs, attributes);
21971 cp_parser_objc_method_prototype_list (parser);
21972 }
21973 }
21974
21975 /* Parse an Objective-C superclass or category. */
21976
21977 static void
21978 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
21979 tree *categ)
21980 {
21981 cp_token *next = cp_lexer_peek_token (parser->lexer);
21982
21983 *super = *categ = NULL_TREE;
21984 if (next->type == CPP_COLON)
21985 {
21986 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
21987 *super = cp_parser_identifier (parser);
21988 }
21989 else if (next->type == CPP_OPEN_PAREN)
21990 {
21991 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21992 *categ = cp_parser_identifier (parser);
21993 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21994 }
21995 }
21996
21997 /* Parse an Objective-C class interface. */
21998
21999 static void
22000 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22001 {
22002 tree name, super, categ, protos;
22003
22004 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
22005 name = cp_parser_identifier (parser);
22006 if (name == error_mark_node)
22007 {
22008 /* It's hard to recover because even if valid @interface stuff
22009 is to follow, we can't compile it (or validate it) if we
22010 don't even know which class it refers to. Let's assume this
22011 was a stray '@interface' token in the stream and skip it.
22012 */
22013 return;
22014 }
22015 cp_parser_objc_superclass_or_category (parser, &super, &categ);
22016 protos = cp_parser_objc_protocol_refs_opt (parser);
22017
22018 /* We have either a class or a category on our hands. */
22019 if (categ)
22020 objc_start_category_interface (name, categ, protos, attributes);
22021 else
22022 {
22023 objc_start_class_interface (name, super, protos, attributes);
22024 /* Handle instance variable declarations, if any. */
22025 cp_parser_objc_class_ivars (parser);
22026 objc_continue_interface ();
22027 }
22028
22029 cp_parser_objc_method_prototype_list (parser);
22030 }
22031
22032 /* Parse an Objective-C class implementation. */
22033
22034 static void
22035 cp_parser_objc_class_implementation (cp_parser* parser)
22036 {
22037 tree name, super, categ;
22038
22039 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
22040 name = cp_parser_identifier (parser);
22041 if (name == error_mark_node)
22042 {
22043 /* It's hard to recover because even if valid @implementation
22044 stuff is to follow, we can't compile it (or validate it) if
22045 we don't even know which class it refers to. Let's assume
22046 this was a stray '@implementation' token in the stream and
22047 skip it.
22048 */
22049 return;
22050 }
22051 cp_parser_objc_superclass_or_category (parser, &super, &categ);
22052
22053 /* We have either a class or a category on our hands. */
22054 if (categ)
22055 objc_start_category_implementation (name, categ);
22056 else
22057 {
22058 objc_start_class_implementation (name, super);
22059 /* Handle instance variable declarations, if any. */
22060 cp_parser_objc_class_ivars (parser);
22061 objc_continue_implementation ();
22062 }
22063
22064 cp_parser_objc_method_definition_list (parser);
22065 }
22066
22067 /* Consume the @end token and finish off the implementation. */
22068
22069 static void
22070 cp_parser_objc_end_implementation (cp_parser* parser)
22071 {
22072 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22073 objc_finish_implementation ();
22074 }
22075
22076 /* Parse an Objective-C declaration. */
22077
22078 static void
22079 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22080 {
22081 /* Try to figure out what kind of declaration is present. */
22082 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22083
22084 if (attributes)
22085 switch (kwd->keyword)
22086 {
22087 case RID_AT_ALIAS:
22088 case RID_AT_CLASS:
22089 case RID_AT_END:
22090 error_at (kwd->location, "attributes may not be specified before"
22091 " the %<@%D%> Objective-C++ keyword",
22092 kwd->u.value);
22093 attributes = NULL;
22094 break;
22095 case RID_AT_IMPLEMENTATION:
22096 warning_at (kwd->location, OPT_Wattributes,
22097 "prefix attributes are ignored before %<@%D%>",
22098 kwd->u.value);
22099 attributes = NULL;
22100 default:
22101 break;
22102 }
22103
22104 switch (kwd->keyword)
22105 {
22106 case RID_AT_ALIAS:
22107 cp_parser_objc_alias_declaration (parser);
22108 break;
22109 case RID_AT_CLASS:
22110 cp_parser_objc_class_declaration (parser);
22111 break;
22112 case RID_AT_PROTOCOL:
22113 cp_parser_objc_protocol_declaration (parser, attributes);
22114 break;
22115 case RID_AT_INTERFACE:
22116 cp_parser_objc_class_interface (parser, attributes);
22117 break;
22118 case RID_AT_IMPLEMENTATION:
22119 cp_parser_objc_class_implementation (parser);
22120 break;
22121 case RID_AT_END:
22122 cp_parser_objc_end_implementation (parser);
22123 break;
22124 default:
22125 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22126 kwd->u.value);
22127 cp_parser_skip_to_end_of_block_or_statement (parser);
22128 }
22129 }
22130
22131 /* Parse an Objective-C try-catch-finally statement.
22132
22133 objc-try-catch-finally-stmt:
22134 @try compound-statement objc-catch-clause-seq [opt]
22135 objc-finally-clause [opt]
22136
22137 objc-catch-clause-seq:
22138 objc-catch-clause objc-catch-clause-seq [opt]
22139
22140 objc-catch-clause:
22141 @catch ( exception-declaration ) compound-statement
22142
22143 objc-finally-clause
22144 @finally compound-statement
22145
22146 Returns NULL_TREE. */
22147
22148 static tree
22149 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
22150 location_t location;
22151 tree stmt;
22152
22153 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22154 location = cp_lexer_peek_token (parser->lexer)->location;
22155 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22156 node, lest it get absorbed into the surrounding block. */
22157 stmt = push_stmt_list ();
22158 cp_parser_compound_statement (parser, NULL, false);
22159 objc_begin_try_stmt (location, pop_stmt_list (stmt));
22160
22161 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22162 {
22163 cp_parameter_declarator *parmdecl;
22164 tree parm;
22165
22166 cp_lexer_consume_token (parser->lexer);
22167 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22168 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22169 parm = grokdeclarator (parmdecl->declarator,
22170 &parmdecl->decl_specifiers,
22171 PARM, /*initialized=*/0,
22172 /*attrlist=*/NULL);
22173 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22174 objc_begin_catch_clause (parm);
22175 cp_parser_compound_statement (parser, NULL, false);
22176 objc_finish_catch_clause ();
22177 }
22178
22179 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22180 {
22181 cp_lexer_consume_token (parser->lexer);
22182 location = cp_lexer_peek_token (parser->lexer)->location;
22183 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22184 node, lest it get absorbed into the surrounding block. */
22185 stmt = push_stmt_list ();
22186 cp_parser_compound_statement (parser, NULL, false);
22187 objc_build_finally_clause (location, pop_stmt_list (stmt));
22188 }
22189
22190 return objc_finish_try_stmt ();
22191 }
22192
22193 /* Parse an Objective-C synchronized statement.
22194
22195 objc-synchronized-stmt:
22196 @synchronized ( expression ) compound-statement
22197
22198 Returns NULL_TREE. */
22199
22200 static tree
22201 cp_parser_objc_synchronized_statement (cp_parser *parser) {
22202 location_t location;
22203 tree lock, stmt;
22204
22205 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22206
22207 location = cp_lexer_peek_token (parser->lexer)->location;
22208 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22209 lock = cp_parser_expression (parser, false, NULL);
22210 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22211
22212 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22213 node, lest it get absorbed into the surrounding block. */
22214 stmt = push_stmt_list ();
22215 cp_parser_compound_statement (parser, NULL, false);
22216
22217 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22218 }
22219
22220 /* Parse an Objective-C throw statement.
22221
22222 objc-throw-stmt:
22223 @throw assignment-expression [opt] ;
22224
22225 Returns a constructed '@throw' statement. */
22226
22227 static tree
22228 cp_parser_objc_throw_statement (cp_parser *parser) {
22229 tree expr = NULL_TREE;
22230 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22231
22232 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22233
22234 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22235 expr = cp_parser_assignment_expression (parser, false, NULL);
22236
22237 cp_parser_consume_semicolon_at_end_of_statement (parser);
22238
22239 return objc_build_throw_stmt (loc, expr);
22240 }
22241
22242 /* Parse an Objective-C statement. */
22243
22244 static tree
22245 cp_parser_objc_statement (cp_parser * parser) {
22246 /* Try to figure out what kind of declaration is present. */
22247 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22248
22249 switch (kwd->keyword)
22250 {
22251 case RID_AT_TRY:
22252 return cp_parser_objc_try_catch_finally_statement (parser);
22253 case RID_AT_SYNCHRONIZED:
22254 return cp_parser_objc_synchronized_statement (parser);
22255 case RID_AT_THROW:
22256 return cp_parser_objc_throw_statement (parser);
22257 default:
22258 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22259 kwd->u.value);
22260 cp_parser_skip_to_end_of_block_or_statement (parser);
22261 }
22262
22263 return error_mark_node;
22264 }
22265
22266 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22267 look ahead to see if an objc keyword follows the attributes. This
22268 is to detect the use of prefix attributes on ObjC @interface and
22269 @protocol. */
22270
22271 static bool
22272 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22273 {
22274 cp_lexer_save_tokens (parser->lexer);
22275 *attrib = cp_parser_attributes_opt (parser);
22276 gcc_assert (*attrib);
22277 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22278 {
22279 cp_lexer_commit_tokens (parser->lexer);
22280 return true;
22281 }
22282 cp_lexer_rollback_tokens (parser->lexer);
22283 return false;
22284 }
22285
22286 /* This routine parses the propery declarations. */
22287
22288 static void
22289 cp_parser_objc_property_decl (cp_parser *parser)
22290 {
22291 int declares_class_or_enum;
22292 cp_decl_specifier_seq declspecs;
22293
22294 cp_parser_decl_specifier_seq (parser,
22295 CP_PARSER_FLAGS_NONE,
22296 &declspecs,
22297 &declares_class_or_enum);
22298 /* Keep going until we hit the `;' at the end of the declaration. */
22299 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22300 {
22301 tree property;
22302 cp_token *token;
22303 cp_declarator *declarator
22304 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22305 NULL, NULL, false);
22306 property = grokdeclarator (declarator, &declspecs, NORMAL,0, NULL);
22307 /* Recover from any kind of error in property declaration. */
22308 if (property == error_mark_node || property == NULL_TREE)
22309 return;
22310
22311 /* Add to property list. */
22312 objc_add_property_variable (copy_node (property));
22313 token = cp_lexer_peek_token (parser->lexer);
22314 if (token->type == CPP_COMMA)
22315 {
22316 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22317 continue;
22318 }
22319 else if (token->type == CPP_EOF)
22320 break;
22321 }
22322 /* Eat ';' if present, or issue an error. */
22323 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22324 }
22325
22326 /* ObjC @property. */
22327 /* Parse a comma-separated list of property attributes.
22328 The lexer does not recognize */
22329
22330 static void
22331 cp_parser_objc_property_attrlist (cp_parser *parser)
22332 {
22333 cp_token *token;
22334 /* Initialize to an empty list. */
22335 objc_set_property_attr (cp_lexer_peek_token (parser->lexer)->location,
22336 OBJC_PATTR_INIT, NULL_TREE);
22337
22338 /* The list is optional. */
22339 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
22340 return;
22341
22342 /* Eat the '('. */
22343 cp_lexer_consume_token (parser->lexer);
22344
22345 token = cp_lexer_peek_token (parser->lexer);
22346 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF)
22347 {
22348 location_t loc = token->location;
22349 tree node = cp_parser_identifier (parser);
22350 if (node == ridpointers [(int) RID_READONLY])
22351 objc_set_property_attr (loc, OBJC_PATTR_READONLY, NULL_TREE);
22352 else if (node == ridpointers [(int) RID_GETTER]
22353 || node == ridpointers [(int) RID_SETTER]
22354 || node == ridpointers [(int) RID_IVAR])
22355 {
22356 /* Do the getter/setter/ivar attribute. */
22357 token = cp_lexer_consume_token (parser->lexer);
22358 if (token->type == CPP_EQ)
22359 {
22360 tree attr_ident = cp_parser_identifier (parser);
22361 objc_property_attribute_kind pkind;
22362 if (node == ridpointers [(int) RID_GETTER])
22363 pkind = OBJC_PATTR_GETTER;
22364 else if (node == ridpointers [(int) RID_SETTER])
22365 {
22366 pkind = OBJC_PATTR_SETTER;
22367 /* Consume the ':' which must always follow the setter name. */
22368 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22369 cp_lexer_consume_token (parser->lexer);
22370 else
22371 {
22372 error_at (token->location,
22373 "setter name must be followed by %<:%>");
22374 break;
22375 }
22376 }
22377 else
22378 pkind = OBJC_PATTR_IVAR;
22379 objc_set_property_attr (loc, pkind, attr_ident);
22380 }
22381 else
22382 {
22383 error_at (token->location,
22384 "getter/setter/ivar attribute must be followed by %<=%>");
22385 break;
22386 }
22387 }
22388 else if (node == ridpointers [(int) RID_COPIES])
22389 objc_set_property_attr (loc, OBJC_PATTR_COPIES, NULL_TREE);
22390 else
22391 {
22392 error_at (token->location,"unknown property attribute");
22393 break;
22394 }
22395 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22396 cp_lexer_consume_token (parser->lexer);
22397 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22398 warning_at (token->location, 0,
22399 "property attributes should be separated by a %<,%>");
22400 token = cp_lexer_peek_token (parser->lexer);
22401 }
22402
22403 if (token->type != CPP_CLOSE_PAREN)
22404 error_at (token->location,
22405 "syntax error in @property's attribute declaration");
22406 else
22407 /* Consume ')' */
22408 cp_lexer_consume_token (parser->lexer);
22409 }
22410
22411 /* This function parses a @property declaration inside an objective class
22412 or its implementation. */
22413
22414 static void
22415 cp_parser_objc_at_property (cp_parser *parser)
22416 {
22417 /* Consume @property */
22418 cp_lexer_consume_token (parser->lexer);
22419
22420 /* Parse optional attributes list... */
22421 cp_parser_objc_property_attrlist (parser);
22422 /* ... and the property declaration(s). */
22423 cp_parser_objc_property_decl (parser);
22424 }
22425 \f
22426 /* OpenMP 2.5 parsing routines. */
22427
22428 /* Returns name of the next clause.
22429 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
22430 the token is not consumed. Otherwise appropriate pragma_omp_clause is
22431 returned and the token is consumed. */
22432
22433 static pragma_omp_clause
22434 cp_parser_omp_clause_name (cp_parser *parser)
22435 {
22436 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
22437
22438 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
22439 result = PRAGMA_OMP_CLAUSE_IF;
22440 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
22441 result = PRAGMA_OMP_CLAUSE_DEFAULT;
22442 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
22443 result = PRAGMA_OMP_CLAUSE_PRIVATE;
22444 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22445 {
22446 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22447 const char *p = IDENTIFIER_POINTER (id);
22448
22449 switch (p[0])
22450 {
22451 case 'c':
22452 if (!strcmp ("collapse", p))
22453 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
22454 else if (!strcmp ("copyin", p))
22455 result = PRAGMA_OMP_CLAUSE_COPYIN;
22456 else if (!strcmp ("copyprivate", p))
22457 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
22458 break;
22459 case 'f':
22460 if (!strcmp ("firstprivate", p))
22461 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
22462 break;
22463 case 'l':
22464 if (!strcmp ("lastprivate", p))
22465 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
22466 break;
22467 case 'n':
22468 if (!strcmp ("nowait", p))
22469 result = PRAGMA_OMP_CLAUSE_NOWAIT;
22470 else if (!strcmp ("num_threads", p))
22471 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
22472 break;
22473 case 'o':
22474 if (!strcmp ("ordered", p))
22475 result = PRAGMA_OMP_CLAUSE_ORDERED;
22476 break;
22477 case 'r':
22478 if (!strcmp ("reduction", p))
22479 result = PRAGMA_OMP_CLAUSE_REDUCTION;
22480 break;
22481 case 's':
22482 if (!strcmp ("schedule", p))
22483 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
22484 else if (!strcmp ("shared", p))
22485 result = PRAGMA_OMP_CLAUSE_SHARED;
22486 break;
22487 case 'u':
22488 if (!strcmp ("untied", p))
22489 result = PRAGMA_OMP_CLAUSE_UNTIED;
22490 break;
22491 }
22492 }
22493
22494 if (result != PRAGMA_OMP_CLAUSE_NONE)
22495 cp_lexer_consume_token (parser->lexer);
22496
22497 return result;
22498 }
22499
22500 /* Validate that a clause of the given type does not already exist. */
22501
22502 static void
22503 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
22504 const char *name, location_t location)
22505 {
22506 tree c;
22507
22508 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22509 if (OMP_CLAUSE_CODE (c) == code)
22510 {
22511 error_at (location, "too many %qs clauses", name);
22512 break;
22513 }
22514 }
22515
22516 /* OpenMP 2.5:
22517 variable-list:
22518 identifier
22519 variable-list , identifier
22520
22521 In addition, we match a closing parenthesis. An opening parenthesis
22522 will have been consumed by the caller.
22523
22524 If KIND is nonzero, create the appropriate node and install the decl
22525 in OMP_CLAUSE_DECL and add the node to the head of the list.
22526
22527 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
22528 return the list created. */
22529
22530 static tree
22531 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
22532 tree list)
22533 {
22534 cp_token *token;
22535 while (1)
22536 {
22537 tree name, decl;
22538
22539 token = cp_lexer_peek_token (parser->lexer);
22540 name = cp_parser_id_expression (parser, /*template_p=*/false,
22541 /*check_dependency_p=*/true,
22542 /*template_p=*/NULL,
22543 /*declarator_p=*/false,
22544 /*optional_p=*/false);
22545 if (name == error_mark_node)
22546 goto skip_comma;
22547
22548 decl = cp_parser_lookup_name_simple (parser, name, token->location);
22549 if (decl == error_mark_node)
22550 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
22551 token->location);
22552 else if (kind != 0)
22553 {
22554 tree u = build_omp_clause (token->location, kind);
22555 OMP_CLAUSE_DECL (u) = decl;
22556 OMP_CLAUSE_CHAIN (u) = list;
22557 list = u;
22558 }
22559 else
22560 list = tree_cons (decl, NULL_TREE, list);
22561
22562 get_comma:
22563 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22564 break;
22565 cp_lexer_consume_token (parser->lexer);
22566 }
22567
22568 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22569 {
22570 int ending;
22571
22572 /* Try to resync to an unnested comma. Copied from
22573 cp_parser_parenthesized_expression_list. */
22574 skip_comma:
22575 ending = cp_parser_skip_to_closing_parenthesis (parser,
22576 /*recovering=*/true,
22577 /*or_comma=*/true,
22578 /*consume_paren=*/true);
22579 if (ending < 0)
22580 goto get_comma;
22581 }
22582
22583 return list;
22584 }
22585
22586 /* Similarly, but expect leading and trailing parenthesis. This is a very
22587 common case for omp clauses. */
22588
22589 static tree
22590 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
22591 {
22592 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22593 return cp_parser_omp_var_list_no_open (parser, kind, list);
22594 return list;
22595 }
22596
22597 /* OpenMP 3.0:
22598 collapse ( constant-expression ) */
22599
22600 static tree
22601 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
22602 {
22603 tree c, num;
22604 location_t loc;
22605 HOST_WIDE_INT n;
22606
22607 loc = cp_lexer_peek_token (parser->lexer)->location;
22608 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22609 return list;
22610
22611 num = cp_parser_constant_expression (parser, false, NULL);
22612
22613 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22614 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22615 /*or_comma=*/false,
22616 /*consume_paren=*/true);
22617
22618 if (num == error_mark_node)
22619 return list;
22620 num = fold_non_dependent_expr (num);
22621 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
22622 || !host_integerp (num, 0)
22623 || (n = tree_low_cst (num, 0)) <= 0
22624 || (int) n != n)
22625 {
22626 error_at (loc, "collapse argument needs positive constant integer expression");
22627 return list;
22628 }
22629
22630 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
22631 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
22632 OMP_CLAUSE_CHAIN (c) = list;
22633 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
22634
22635 return c;
22636 }
22637
22638 /* OpenMP 2.5:
22639 default ( shared | none ) */
22640
22641 static tree
22642 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
22643 {
22644 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
22645 tree c;
22646
22647 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22648 return list;
22649 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22650 {
22651 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22652 const char *p = IDENTIFIER_POINTER (id);
22653
22654 switch (p[0])
22655 {
22656 case 'n':
22657 if (strcmp ("none", p) != 0)
22658 goto invalid_kind;
22659 kind = OMP_CLAUSE_DEFAULT_NONE;
22660 break;
22661
22662 case 's':
22663 if (strcmp ("shared", p) != 0)
22664 goto invalid_kind;
22665 kind = OMP_CLAUSE_DEFAULT_SHARED;
22666 break;
22667
22668 default:
22669 goto invalid_kind;
22670 }
22671
22672 cp_lexer_consume_token (parser->lexer);
22673 }
22674 else
22675 {
22676 invalid_kind:
22677 cp_parser_error (parser, "expected %<none%> or %<shared%>");
22678 }
22679
22680 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22681 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22682 /*or_comma=*/false,
22683 /*consume_paren=*/true);
22684
22685 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
22686 return list;
22687
22688 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
22689 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
22690 OMP_CLAUSE_CHAIN (c) = list;
22691 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
22692
22693 return c;
22694 }
22695
22696 /* OpenMP 2.5:
22697 if ( expression ) */
22698
22699 static tree
22700 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
22701 {
22702 tree t, c;
22703
22704 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22705 return list;
22706
22707 t = cp_parser_condition (parser);
22708
22709 if (t == error_mark_node
22710 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22711 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22712 /*or_comma=*/false,
22713 /*consume_paren=*/true);
22714
22715 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
22716
22717 c = build_omp_clause (location, OMP_CLAUSE_IF);
22718 OMP_CLAUSE_IF_EXPR (c) = t;
22719 OMP_CLAUSE_CHAIN (c) = list;
22720
22721 return c;
22722 }
22723
22724 /* OpenMP 2.5:
22725 nowait */
22726
22727 static tree
22728 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
22729 tree list, location_t location)
22730 {
22731 tree c;
22732
22733 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
22734
22735 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
22736 OMP_CLAUSE_CHAIN (c) = list;
22737 return c;
22738 }
22739
22740 /* OpenMP 2.5:
22741 num_threads ( expression ) */
22742
22743 static tree
22744 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
22745 location_t location)
22746 {
22747 tree t, c;
22748
22749 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22750 return list;
22751
22752 t = cp_parser_expression (parser, false, NULL);
22753
22754 if (t == error_mark_node
22755 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22756 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22757 /*or_comma=*/false,
22758 /*consume_paren=*/true);
22759
22760 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
22761 "num_threads", location);
22762
22763 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
22764 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
22765 OMP_CLAUSE_CHAIN (c) = list;
22766
22767 return c;
22768 }
22769
22770 /* OpenMP 2.5:
22771 ordered */
22772
22773 static tree
22774 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
22775 tree list, location_t location)
22776 {
22777 tree c;
22778
22779 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
22780 "ordered", location);
22781
22782 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
22783 OMP_CLAUSE_CHAIN (c) = list;
22784 return c;
22785 }
22786
22787 /* OpenMP 2.5:
22788 reduction ( reduction-operator : variable-list )
22789
22790 reduction-operator:
22791 One of: + * - & ^ | && || */
22792
22793 static tree
22794 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
22795 {
22796 enum tree_code code;
22797 tree nlist, c;
22798
22799 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22800 return list;
22801
22802 switch (cp_lexer_peek_token (parser->lexer)->type)
22803 {
22804 case CPP_PLUS:
22805 code = PLUS_EXPR;
22806 break;
22807 case CPP_MULT:
22808 code = MULT_EXPR;
22809 break;
22810 case CPP_MINUS:
22811 code = MINUS_EXPR;
22812 break;
22813 case CPP_AND:
22814 code = BIT_AND_EXPR;
22815 break;
22816 case CPP_XOR:
22817 code = BIT_XOR_EXPR;
22818 break;
22819 case CPP_OR:
22820 code = BIT_IOR_EXPR;
22821 break;
22822 case CPP_AND_AND:
22823 code = TRUTH_ANDIF_EXPR;
22824 break;
22825 case CPP_OR_OR:
22826 code = TRUTH_ORIF_EXPR;
22827 break;
22828 default:
22829 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
22830 "%<|%>, %<&&%>, or %<||%>");
22831 resync_fail:
22832 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22833 /*or_comma=*/false,
22834 /*consume_paren=*/true);
22835 return list;
22836 }
22837 cp_lexer_consume_token (parser->lexer);
22838
22839 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22840 goto resync_fail;
22841
22842 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
22843 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
22844 OMP_CLAUSE_REDUCTION_CODE (c) = code;
22845
22846 return nlist;
22847 }
22848
22849 /* OpenMP 2.5:
22850 schedule ( schedule-kind )
22851 schedule ( schedule-kind , expression )
22852
22853 schedule-kind:
22854 static | dynamic | guided | runtime | auto */
22855
22856 static tree
22857 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
22858 {
22859 tree c, t;
22860
22861 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22862 return list;
22863
22864 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
22865
22866 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22867 {
22868 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22869 const char *p = IDENTIFIER_POINTER (id);
22870
22871 switch (p[0])
22872 {
22873 case 'd':
22874 if (strcmp ("dynamic", p) != 0)
22875 goto invalid_kind;
22876 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
22877 break;
22878
22879 case 'g':
22880 if (strcmp ("guided", p) != 0)
22881 goto invalid_kind;
22882 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
22883 break;
22884
22885 case 'r':
22886 if (strcmp ("runtime", p) != 0)
22887 goto invalid_kind;
22888 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
22889 break;
22890
22891 default:
22892 goto invalid_kind;
22893 }
22894 }
22895 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
22896 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
22897 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
22898 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
22899 else
22900 goto invalid_kind;
22901 cp_lexer_consume_token (parser->lexer);
22902
22903 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22904 {
22905 cp_token *token;
22906 cp_lexer_consume_token (parser->lexer);
22907
22908 token = cp_lexer_peek_token (parser->lexer);
22909 t = cp_parser_assignment_expression (parser, false, NULL);
22910
22911 if (t == error_mark_node)
22912 goto resync_fail;
22913 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
22914 error_at (token->location, "schedule %<runtime%> does not take "
22915 "a %<chunk_size%> parameter");
22916 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
22917 error_at (token->location, "schedule %<auto%> does not take "
22918 "a %<chunk_size%> parameter");
22919 else
22920 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
22921
22922 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22923 goto resync_fail;
22924 }
22925 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
22926 goto resync_fail;
22927
22928 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
22929 OMP_CLAUSE_CHAIN (c) = list;
22930 return c;
22931
22932 invalid_kind:
22933 cp_parser_error (parser, "invalid schedule kind");
22934 resync_fail:
22935 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22936 /*or_comma=*/false,
22937 /*consume_paren=*/true);
22938 return list;
22939 }
22940
22941 /* OpenMP 3.0:
22942 untied */
22943
22944 static tree
22945 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
22946 tree list, location_t location)
22947 {
22948 tree c;
22949
22950 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
22951
22952 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
22953 OMP_CLAUSE_CHAIN (c) = list;
22954 return c;
22955 }
22956
22957 /* Parse all OpenMP clauses. The set clauses allowed by the directive
22958 is a bitmask in MASK. Return the list of clauses found; the result
22959 of clause default goes in *pdefault. */
22960
22961 static tree
22962 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
22963 const char *where, cp_token *pragma_tok)
22964 {
22965 tree clauses = NULL;
22966 bool first = true;
22967 cp_token *token = NULL;
22968
22969 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
22970 {
22971 pragma_omp_clause c_kind;
22972 const char *c_name;
22973 tree prev = clauses;
22974
22975 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22976 cp_lexer_consume_token (parser->lexer);
22977
22978 token = cp_lexer_peek_token (parser->lexer);
22979 c_kind = cp_parser_omp_clause_name (parser);
22980 first = false;
22981
22982 switch (c_kind)
22983 {
22984 case PRAGMA_OMP_CLAUSE_COLLAPSE:
22985 clauses = cp_parser_omp_clause_collapse (parser, clauses,
22986 token->location);
22987 c_name = "collapse";
22988 break;
22989 case PRAGMA_OMP_CLAUSE_COPYIN:
22990 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
22991 c_name = "copyin";
22992 break;
22993 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
22994 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
22995 clauses);
22996 c_name = "copyprivate";
22997 break;
22998 case PRAGMA_OMP_CLAUSE_DEFAULT:
22999 clauses = cp_parser_omp_clause_default (parser, clauses,
23000 token->location);
23001 c_name = "default";
23002 break;
23003 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23004 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23005 clauses);
23006 c_name = "firstprivate";
23007 break;
23008 case PRAGMA_OMP_CLAUSE_IF:
23009 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23010 c_name = "if";
23011 break;
23012 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23013 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23014 clauses);
23015 c_name = "lastprivate";
23016 break;
23017 case PRAGMA_OMP_CLAUSE_NOWAIT:
23018 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23019 c_name = "nowait";
23020 break;
23021 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23022 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23023 token->location);
23024 c_name = "num_threads";
23025 break;
23026 case PRAGMA_OMP_CLAUSE_ORDERED:
23027 clauses = cp_parser_omp_clause_ordered (parser, clauses,
23028 token->location);
23029 c_name = "ordered";
23030 break;
23031 case PRAGMA_OMP_CLAUSE_PRIVATE:
23032 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23033 clauses);
23034 c_name = "private";
23035 break;
23036 case PRAGMA_OMP_CLAUSE_REDUCTION:
23037 clauses = cp_parser_omp_clause_reduction (parser, clauses);
23038 c_name = "reduction";
23039 break;
23040 case PRAGMA_OMP_CLAUSE_SCHEDULE:
23041 clauses = cp_parser_omp_clause_schedule (parser, clauses,
23042 token->location);
23043 c_name = "schedule";
23044 break;
23045 case PRAGMA_OMP_CLAUSE_SHARED:
23046 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23047 clauses);
23048 c_name = "shared";
23049 break;
23050 case PRAGMA_OMP_CLAUSE_UNTIED:
23051 clauses = cp_parser_omp_clause_untied (parser, clauses,
23052 token->location);
23053 c_name = "nowait";
23054 break;
23055 default:
23056 cp_parser_error (parser, "expected %<#pragma omp%> clause");
23057 goto saw_error;
23058 }
23059
23060 if (((mask >> c_kind) & 1) == 0)
23061 {
23062 /* Remove the invalid clause(s) from the list to avoid
23063 confusing the rest of the compiler. */
23064 clauses = prev;
23065 error_at (token->location, "%qs is not valid for %qs", c_name, where);
23066 }
23067 }
23068 saw_error:
23069 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23070 return finish_omp_clauses (clauses);
23071 }
23072
23073 /* OpenMP 2.5:
23074 structured-block:
23075 statement
23076
23077 In practice, we're also interested in adding the statement to an
23078 outer node. So it is convenient if we work around the fact that
23079 cp_parser_statement calls add_stmt. */
23080
23081 static unsigned
23082 cp_parser_begin_omp_structured_block (cp_parser *parser)
23083 {
23084 unsigned save = parser->in_statement;
23085
23086 /* Only move the values to IN_OMP_BLOCK if they weren't false.
23087 This preserves the "not within loop or switch" style error messages
23088 for nonsense cases like
23089 void foo() {
23090 #pragma omp single
23091 break;
23092 }
23093 */
23094 if (parser->in_statement)
23095 parser->in_statement = IN_OMP_BLOCK;
23096
23097 return save;
23098 }
23099
23100 static void
23101 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
23102 {
23103 parser->in_statement = save;
23104 }
23105
23106 static tree
23107 cp_parser_omp_structured_block (cp_parser *parser)
23108 {
23109 tree stmt = begin_omp_structured_block ();
23110 unsigned int save = cp_parser_begin_omp_structured_block (parser);
23111
23112 cp_parser_statement (parser, NULL_TREE, false, NULL);
23113
23114 cp_parser_end_omp_structured_block (parser, save);
23115 return finish_omp_structured_block (stmt);
23116 }
23117
23118 /* OpenMP 2.5:
23119 # pragma omp atomic new-line
23120 expression-stmt
23121
23122 expression-stmt:
23123 x binop= expr | x++ | ++x | x-- | --x
23124 binop:
23125 +, *, -, /, &, ^, |, <<, >>
23126
23127 where x is an lvalue expression with scalar type. */
23128
23129 static void
23130 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
23131 {
23132 tree lhs, rhs;
23133 enum tree_code code;
23134
23135 cp_parser_require_pragma_eol (parser, pragma_tok);
23136
23137 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
23138 /*cast_p=*/false, NULL);
23139 switch (TREE_CODE (lhs))
23140 {
23141 case ERROR_MARK:
23142 goto saw_error;
23143
23144 case PREINCREMENT_EXPR:
23145 case POSTINCREMENT_EXPR:
23146 lhs = TREE_OPERAND (lhs, 0);
23147 code = PLUS_EXPR;
23148 rhs = integer_one_node;
23149 break;
23150
23151 case PREDECREMENT_EXPR:
23152 case POSTDECREMENT_EXPR:
23153 lhs = TREE_OPERAND (lhs, 0);
23154 code = MINUS_EXPR;
23155 rhs = integer_one_node;
23156 break;
23157
23158 case COMPOUND_EXPR:
23159 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
23160 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
23161 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
23162 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
23163 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23164 (TREE_OPERAND (lhs, 1), 0), 0)))
23165 == BOOLEAN_TYPE)
23166 /* Undo effects of boolean_increment for post {in,de}crement. */
23167 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
23168 /* FALLTHRU */
23169 case MODIFY_EXPR:
23170 if (TREE_CODE (lhs) == MODIFY_EXPR
23171 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
23172 {
23173 /* Undo effects of boolean_increment. */
23174 if (integer_onep (TREE_OPERAND (lhs, 1)))
23175 {
23176 /* This is pre or post increment. */
23177 rhs = TREE_OPERAND (lhs, 1);
23178 lhs = TREE_OPERAND (lhs, 0);
23179 code = NOP_EXPR;
23180 break;
23181 }
23182 }
23183 /* FALLTHRU */
23184 default:
23185 switch (cp_lexer_peek_token (parser->lexer)->type)
23186 {
23187 case CPP_MULT_EQ:
23188 code = MULT_EXPR;
23189 break;
23190 case CPP_DIV_EQ:
23191 code = TRUNC_DIV_EXPR;
23192 break;
23193 case CPP_PLUS_EQ:
23194 code = PLUS_EXPR;
23195 break;
23196 case CPP_MINUS_EQ:
23197 code = MINUS_EXPR;
23198 break;
23199 case CPP_LSHIFT_EQ:
23200 code = LSHIFT_EXPR;
23201 break;
23202 case CPP_RSHIFT_EQ:
23203 code = RSHIFT_EXPR;
23204 break;
23205 case CPP_AND_EQ:
23206 code = BIT_AND_EXPR;
23207 break;
23208 case CPP_OR_EQ:
23209 code = BIT_IOR_EXPR;
23210 break;
23211 case CPP_XOR_EQ:
23212 code = BIT_XOR_EXPR;
23213 break;
23214 default:
23215 cp_parser_error (parser,
23216 "invalid operator for %<#pragma omp atomic%>");
23217 goto saw_error;
23218 }
23219 cp_lexer_consume_token (parser->lexer);
23220
23221 rhs = cp_parser_expression (parser, false, NULL);
23222 if (rhs == error_mark_node)
23223 goto saw_error;
23224 break;
23225 }
23226 finish_omp_atomic (code, lhs, rhs);
23227 cp_parser_consume_semicolon_at_end_of_statement (parser);
23228 return;
23229
23230 saw_error:
23231 cp_parser_skip_to_end_of_block_or_statement (parser);
23232 }
23233
23234
23235 /* OpenMP 2.5:
23236 # pragma omp barrier new-line */
23237
23238 static void
23239 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
23240 {
23241 cp_parser_require_pragma_eol (parser, pragma_tok);
23242 finish_omp_barrier ();
23243 }
23244
23245 /* OpenMP 2.5:
23246 # pragma omp critical [(name)] new-line
23247 structured-block */
23248
23249 static tree
23250 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
23251 {
23252 tree stmt, name = NULL;
23253
23254 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23255 {
23256 cp_lexer_consume_token (parser->lexer);
23257
23258 name = cp_parser_identifier (parser);
23259
23260 if (name == error_mark_node
23261 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23262 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23263 /*or_comma=*/false,
23264 /*consume_paren=*/true);
23265 if (name == error_mark_node)
23266 name = NULL;
23267 }
23268 cp_parser_require_pragma_eol (parser, pragma_tok);
23269
23270 stmt = cp_parser_omp_structured_block (parser);
23271 return c_finish_omp_critical (input_location, stmt, name);
23272 }
23273
23274 /* OpenMP 2.5:
23275 # pragma omp flush flush-vars[opt] new-line
23276
23277 flush-vars:
23278 ( variable-list ) */
23279
23280 static void
23281 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
23282 {
23283 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23284 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23285 cp_parser_require_pragma_eol (parser, pragma_tok);
23286
23287 finish_omp_flush ();
23288 }
23289
23290 /* Helper function, to parse omp for increment expression. */
23291
23292 static tree
23293 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
23294 {
23295 tree cond = cp_parser_binary_expression (parser, false, true,
23296 PREC_NOT_OPERATOR, NULL);
23297 bool overloaded_p;
23298
23299 if (cond == error_mark_node
23300 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23301 {
23302 cp_parser_skip_to_end_of_statement (parser);
23303 return error_mark_node;
23304 }
23305
23306 switch (TREE_CODE (cond))
23307 {
23308 case GT_EXPR:
23309 case GE_EXPR:
23310 case LT_EXPR:
23311 case LE_EXPR:
23312 break;
23313 default:
23314 return error_mark_node;
23315 }
23316
23317 /* If decl is an iterator, preserve LHS and RHS of the relational
23318 expr until finish_omp_for. */
23319 if (decl
23320 && (type_dependent_expression_p (decl)
23321 || CLASS_TYPE_P (TREE_TYPE (decl))))
23322 return cond;
23323
23324 return build_x_binary_op (TREE_CODE (cond),
23325 TREE_OPERAND (cond, 0), ERROR_MARK,
23326 TREE_OPERAND (cond, 1), ERROR_MARK,
23327 &overloaded_p, tf_warning_or_error);
23328 }
23329
23330 /* Helper function, to parse omp for increment expression. */
23331
23332 static tree
23333 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
23334 {
23335 cp_token *token = cp_lexer_peek_token (parser->lexer);
23336 enum tree_code op;
23337 tree lhs, rhs;
23338 cp_id_kind idk;
23339 bool decl_first;
23340
23341 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23342 {
23343 op = (token->type == CPP_PLUS_PLUS
23344 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
23345 cp_lexer_consume_token (parser->lexer);
23346 lhs = cp_parser_cast_expression (parser, false, false, NULL);
23347 if (lhs != decl)
23348 return error_mark_node;
23349 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23350 }
23351
23352 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
23353 if (lhs != decl)
23354 return error_mark_node;
23355
23356 token = cp_lexer_peek_token (parser->lexer);
23357 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23358 {
23359 op = (token->type == CPP_PLUS_PLUS
23360 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
23361 cp_lexer_consume_token (parser->lexer);
23362 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23363 }
23364
23365 op = cp_parser_assignment_operator_opt (parser);
23366 if (op == ERROR_MARK)
23367 return error_mark_node;
23368
23369 if (op != NOP_EXPR)
23370 {
23371 rhs = cp_parser_assignment_expression (parser, false, NULL);
23372 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
23373 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
23374 }
23375
23376 lhs = cp_parser_binary_expression (parser, false, false,
23377 PREC_ADDITIVE_EXPRESSION, NULL);
23378 token = cp_lexer_peek_token (parser->lexer);
23379 decl_first = lhs == decl;
23380 if (decl_first)
23381 lhs = NULL_TREE;
23382 if (token->type != CPP_PLUS
23383 && token->type != CPP_MINUS)
23384 return error_mark_node;
23385
23386 do
23387 {
23388 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
23389 cp_lexer_consume_token (parser->lexer);
23390 rhs = cp_parser_binary_expression (parser, false, false,
23391 PREC_ADDITIVE_EXPRESSION, NULL);
23392 token = cp_lexer_peek_token (parser->lexer);
23393 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
23394 {
23395 if (lhs == NULL_TREE)
23396 {
23397 if (op == PLUS_EXPR)
23398 lhs = rhs;
23399 else
23400 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
23401 }
23402 else
23403 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
23404 NULL, tf_warning_or_error);
23405 }
23406 }
23407 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
23408
23409 if (!decl_first)
23410 {
23411 if (rhs != decl || op == MINUS_EXPR)
23412 return error_mark_node;
23413 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
23414 }
23415 else
23416 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
23417
23418 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
23419 }
23420
23421 /* Parse the restricted form of the for statement allowed by OpenMP. */
23422
23423 static tree
23424 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
23425 {
23426 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
23427 tree real_decl, initv, condv, incrv, declv;
23428 tree this_pre_body, cl;
23429 location_t loc_first;
23430 bool collapse_err = false;
23431 int i, collapse = 1, nbraces = 0;
23432 VEC(tree,gc) *for_block = make_tree_vector ();
23433
23434 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
23435 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
23436 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
23437
23438 gcc_assert (collapse >= 1);
23439
23440 declv = make_tree_vec (collapse);
23441 initv = make_tree_vec (collapse);
23442 condv = make_tree_vec (collapse);
23443 incrv = make_tree_vec (collapse);
23444
23445 loc_first = cp_lexer_peek_token (parser->lexer)->location;
23446
23447 for (i = 0; i < collapse; i++)
23448 {
23449 int bracecount = 0;
23450 bool add_private_clause = false;
23451 location_t loc;
23452
23453 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23454 {
23455 cp_parser_error (parser, "for statement expected");
23456 return NULL;
23457 }
23458 loc = cp_lexer_consume_token (parser->lexer)->location;
23459
23460 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23461 return NULL;
23462
23463 init = decl = real_decl = NULL;
23464 this_pre_body = push_stmt_list ();
23465 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23466 {
23467 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
23468
23469 init-expr:
23470 var = lb
23471 integer-type var = lb
23472 random-access-iterator-type var = lb
23473 pointer-type var = lb
23474 */
23475 cp_decl_specifier_seq type_specifiers;
23476
23477 /* First, try to parse as an initialized declaration. See
23478 cp_parser_condition, from whence the bulk of this is copied. */
23479
23480 cp_parser_parse_tentatively (parser);
23481 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
23482 /*is_trailing_return=*/false,
23483 &type_specifiers);
23484 if (cp_parser_parse_definitely (parser))
23485 {
23486 /* If parsing a type specifier seq succeeded, then this
23487 MUST be a initialized declaration. */
23488 tree asm_specification, attributes;
23489 cp_declarator *declarator;
23490
23491 declarator = cp_parser_declarator (parser,
23492 CP_PARSER_DECLARATOR_NAMED,
23493 /*ctor_dtor_or_conv_p=*/NULL,
23494 /*parenthesized_p=*/NULL,
23495 /*member_p=*/false);
23496 attributes = cp_parser_attributes_opt (parser);
23497 asm_specification = cp_parser_asm_specification_opt (parser);
23498
23499 if (declarator == cp_error_declarator)
23500 cp_parser_skip_to_end_of_statement (parser);
23501
23502 else
23503 {
23504 tree pushed_scope, auto_node;
23505
23506 decl = start_decl (declarator, &type_specifiers,
23507 SD_INITIALIZED, attributes,
23508 /*prefix_attributes=*/NULL_TREE,
23509 &pushed_scope);
23510
23511 auto_node = type_uses_auto (TREE_TYPE (decl));
23512 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23513 {
23514 if (cp_lexer_next_token_is (parser->lexer,
23515 CPP_OPEN_PAREN))
23516 error ("parenthesized initialization is not allowed in "
23517 "OpenMP %<for%> loop");
23518 else
23519 /* Trigger an error. */
23520 cp_parser_require (parser, CPP_EQ, RT_EQ);
23521
23522 init = error_mark_node;
23523 cp_parser_skip_to_end_of_statement (parser);
23524 }
23525 else if (CLASS_TYPE_P (TREE_TYPE (decl))
23526 || type_dependent_expression_p (decl)
23527 || auto_node)
23528 {
23529 bool is_direct_init, is_non_constant_init;
23530
23531 init = cp_parser_initializer (parser,
23532 &is_direct_init,
23533 &is_non_constant_init);
23534
23535 if (auto_node && describable_type (init))
23536 {
23537 TREE_TYPE (decl)
23538 = do_auto_deduction (TREE_TYPE (decl), init,
23539 auto_node);
23540
23541 if (!CLASS_TYPE_P (TREE_TYPE (decl))
23542 && !type_dependent_expression_p (decl))
23543 goto non_class;
23544 }
23545
23546 cp_finish_decl (decl, init, !is_non_constant_init,
23547 asm_specification,
23548 LOOKUP_ONLYCONVERTING);
23549 if (CLASS_TYPE_P (TREE_TYPE (decl)))
23550 {
23551 VEC_safe_push (tree, gc, for_block, this_pre_body);
23552 init = NULL_TREE;
23553 }
23554 else
23555 init = pop_stmt_list (this_pre_body);
23556 this_pre_body = NULL_TREE;
23557 }
23558 else
23559 {
23560 /* Consume '='. */
23561 cp_lexer_consume_token (parser->lexer);
23562 init = cp_parser_assignment_expression (parser, false, NULL);
23563
23564 non_class:
23565 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
23566 init = error_mark_node;
23567 else
23568 cp_finish_decl (decl, NULL_TREE,
23569 /*init_const_expr_p=*/false,
23570 asm_specification,
23571 LOOKUP_ONLYCONVERTING);
23572 }
23573
23574 if (pushed_scope)
23575 pop_scope (pushed_scope);
23576 }
23577 }
23578 else
23579 {
23580 cp_id_kind idk;
23581 /* If parsing a type specifier sequence failed, then
23582 this MUST be a simple expression. */
23583 cp_parser_parse_tentatively (parser);
23584 decl = cp_parser_primary_expression (parser, false, false,
23585 false, &idk);
23586 if (!cp_parser_error_occurred (parser)
23587 && decl
23588 && DECL_P (decl)
23589 && CLASS_TYPE_P (TREE_TYPE (decl)))
23590 {
23591 tree rhs;
23592
23593 cp_parser_parse_definitely (parser);
23594 cp_parser_require (parser, CPP_EQ, RT_EQ);
23595 rhs = cp_parser_assignment_expression (parser, false, NULL);
23596 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
23597 rhs,
23598 tf_warning_or_error));
23599 add_private_clause = true;
23600 }
23601 else
23602 {
23603 decl = NULL;
23604 cp_parser_abort_tentative_parse (parser);
23605 init = cp_parser_expression (parser, false, NULL);
23606 if (init)
23607 {
23608 if (TREE_CODE (init) == MODIFY_EXPR
23609 || TREE_CODE (init) == MODOP_EXPR)
23610 real_decl = TREE_OPERAND (init, 0);
23611 }
23612 }
23613 }
23614 }
23615 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23616 if (this_pre_body)
23617 {
23618 this_pre_body = pop_stmt_list (this_pre_body);
23619 if (pre_body)
23620 {
23621 tree t = pre_body;
23622 pre_body = push_stmt_list ();
23623 add_stmt (t);
23624 add_stmt (this_pre_body);
23625 pre_body = pop_stmt_list (pre_body);
23626 }
23627 else
23628 pre_body = this_pre_body;
23629 }
23630
23631 if (decl)
23632 real_decl = decl;
23633 if (par_clauses != NULL && real_decl != NULL_TREE)
23634 {
23635 tree *c;
23636 for (c = par_clauses; *c ; )
23637 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
23638 && OMP_CLAUSE_DECL (*c) == real_decl)
23639 {
23640 error_at (loc, "iteration variable %qD"
23641 " should not be firstprivate", real_decl);
23642 *c = OMP_CLAUSE_CHAIN (*c);
23643 }
23644 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
23645 && OMP_CLAUSE_DECL (*c) == real_decl)
23646 {
23647 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
23648 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
23649 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
23650 OMP_CLAUSE_DECL (l) = real_decl;
23651 OMP_CLAUSE_CHAIN (l) = clauses;
23652 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
23653 clauses = l;
23654 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
23655 CP_OMP_CLAUSE_INFO (*c) = NULL;
23656 add_private_clause = false;
23657 }
23658 else
23659 {
23660 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
23661 && OMP_CLAUSE_DECL (*c) == real_decl)
23662 add_private_clause = false;
23663 c = &OMP_CLAUSE_CHAIN (*c);
23664 }
23665 }
23666
23667 if (add_private_clause)
23668 {
23669 tree c;
23670 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23671 {
23672 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
23673 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
23674 && OMP_CLAUSE_DECL (c) == decl)
23675 break;
23676 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
23677 && OMP_CLAUSE_DECL (c) == decl)
23678 error_at (loc, "iteration variable %qD "
23679 "should not be firstprivate",
23680 decl);
23681 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
23682 && OMP_CLAUSE_DECL (c) == decl)
23683 error_at (loc, "iteration variable %qD should not be reduction",
23684 decl);
23685 }
23686 if (c == NULL)
23687 {
23688 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
23689 OMP_CLAUSE_DECL (c) = decl;
23690 c = finish_omp_clauses (c);
23691 if (c)
23692 {
23693 OMP_CLAUSE_CHAIN (c) = clauses;
23694 clauses = c;
23695 }
23696 }
23697 }
23698
23699 cond = NULL;
23700 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23701 cond = cp_parser_omp_for_cond (parser, decl);
23702 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23703
23704 incr = NULL;
23705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23706 {
23707 /* If decl is an iterator, preserve the operator on decl
23708 until finish_omp_for. */
23709 if (decl
23710 && (type_dependent_expression_p (decl)
23711 || CLASS_TYPE_P (TREE_TYPE (decl))))
23712 incr = cp_parser_omp_for_incr (parser, decl);
23713 else
23714 incr = cp_parser_expression (parser, false, NULL);
23715 }
23716
23717 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23718 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23719 /*or_comma=*/false,
23720 /*consume_paren=*/true);
23721
23722 TREE_VEC_ELT (declv, i) = decl;
23723 TREE_VEC_ELT (initv, i) = init;
23724 TREE_VEC_ELT (condv, i) = cond;
23725 TREE_VEC_ELT (incrv, i) = incr;
23726
23727 if (i == collapse - 1)
23728 break;
23729
23730 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
23731 in between the collapsed for loops to be still considered perfectly
23732 nested. Hopefully the final version clarifies this.
23733 For now handle (multiple) {'s and empty statements. */
23734 cp_parser_parse_tentatively (parser);
23735 do
23736 {
23737 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23738 break;
23739 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23740 {
23741 cp_lexer_consume_token (parser->lexer);
23742 bracecount++;
23743 }
23744 else if (bracecount
23745 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23746 cp_lexer_consume_token (parser->lexer);
23747 else
23748 {
23749 loc = cp_lexer_peek_token (parser->lexer)->location;
23750 error_at (loc, "not enough collapsed for loops");
23751 collapse_err = true;
23752 cp_parser_abort_tentative_parse (parser);
23753 declv = NULL_TREE;
23754 break;
23755 }
23756 }
23757 while (1);
23758
23759 if (declv)
23760 {
23761 cp_parser_parse_definitely (parser);
23762 nbraces += bracecount;
23763 }
23764 }
23765
23766 /* Note that we saved the original contents of this flag when we entered
23767 the structured block, and so we don't need to re-save it here. */
23768 parser->in_statement = IN_OMP_FOR;
23769
23770 /* Note that the grammar doesn't call for a structured block here,
23771 though the loop as a whole is a structured block. */
23772 body = push_stmt_list ();
23773 cp_parser_statement (parser, NULL_TREE, false, NULL);
23774 body = pop_stmt_list (body);
23775
23776 if (declv == NULL_TREE)
23777 ret = NULL_TREE;
23778 else
23779 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
23780 pre_body, clauses);
23781
23782 while (nbraces)
23783 {
23784 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23785 {
23786 cp_lexer_consume_token (parser->lexer);
23787 nbraces--;
23788 }
23789 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23790 cp_lexer_consume_token (parser->lexer);
23791 else
23792 {
23793 if (!collapse_err)
23794 {
23795 error_at (cp_lexer_peek_token (parser->lexer)->location,
23796 "collapsed loops not perfectly nested");
23797 }
23798 collapse_err = true;
23799 cp_parser_statement_seq_opt (parser, NULL);
23800 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23801 break;
23802 }
23803 }
23804
23805 while (!VEC_empty (tree, for_block))
23806 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
23807 release_tree_vector (for_block);
23808
23809 return ret;
23810 }
23811
23812 /* OpenMP 2.5:
23813 #pragma omp for for-clause[optseq] new-line
23814 for-loop */
23815
23816 #define OMP_FOR_CLAUSE_MASK \
23817 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23818 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23819 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
23820 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23821 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
23822 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
23823 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
23824 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
23825
23826 static tree
23827 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
23828 {
23829 tree clauses, sb, ret;
23830 unsigned int save;
23831
23832 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
23833 "#pragma omp for", pragma_tok);
23834
23835 sb = begin_omp_structured_block ();
23836 save = cp_parser_begin_omp_structured_block (parser);
23837
23838 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
23839
23840 cp_parser_end_omp_structured_block (parser, save);
23841 add_stmt (finish_omp_structured_block (sb));
23842
23843 return ret;
23844 }
23845
23846 /* OpenMP 2.5:
23847 # pragma omp master new-line
23848 structured-block */
23849
23850 static tree
23851 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
23852 {
23853 cp_parser_require_pragma_eol (parser, pragma_tok);
23854 return c_finish_omp_master (input_location,
23855 cp_parser_omp_structured_block (parser));
23856 }
23857
23858 /* OpenMP 2.5:
23859 # pragma omp ordered new-line
23860 structured-block */
23861
23862 static tree
23863 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
23864 {
23865 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23866 cp_parser_require_pragma_eol (parser, pragma_tok);
23867 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
23868 }
23869
23870 /* OpenMP 2.5:
23871
23872 section-scope:
23873 { section-sequence }
23874
23875 section-sequence:
23876 section-directive[opt] structured-block
23877 section-sequence section-directive structured-block */
23878
23879 static tree
23880 cp_parser_omp_sections_scope (cp_parser *parser)
23881 {
23882 tree stmt, substmt;
23883 bool error_suppress = false;
23884 cp_token *tok;
23885
23886 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
23887 return NULL_TREE;
23888
23889 stmt = push_stmt_list ();
23890
23891 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
23892 {
23893 unsigned save;
23894
23895 substmt = begin_omp_structured_block ();
23896 save = cp_parser_begin_omp_structured_block (parser);
23897
23898 while (1)
23899 {
23900 cp_parser_statement (parser, NULL_TREE, false, NULL);
23901
23902 tok = cp_lexer_peek_token (parser->lexer);
23903 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
23904 break;
23905 if (tok->type == CPP_CLOSE_BRACE)
23906 break;
23907 if (tok->type == CPP_EOF)
23908 break;
23909 }
23910
23911 cp_parser_end_omp_structured_block (parser, save);
23912 substmt = finish_omp_structured_block (substmt);
23913 substmt = build1 (OMP_SECTION, void_type_node, substmt);
23914 add_stmt (substmt);
23915 }
23916
23917 while (1)
23918 {
23919 tok = cp_lexer_peek_token (parser->lexer);
23920 if (tok->type == CPP_CLOSE_BRACE)
23921 break;
23922 if (tok->type == CPP_EOF)
23923 break;
23924
23925 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
23926 {
23927 cp_lexer_consume_token (parser->lexer);
23928 cp_parser_require_pragma_eol (parser, tok);
23929 error_suppress = false;
23930 }
23931 else if (!error_suppress)
23932 {
23933 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
23934 error_suppress = true;
23935 }
23936
23937 substmt = cp_parser_omp_structured_block (parser);
23938 substmt = build1 (OMP_SECTION, void_type_node, substmt);
23939 add_stmt (substmt);
23940 }
23941 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23942
23943 substmt = pop_stmt_list (stmt);
23944
23945 stmt = make_node (OMP_SECTIONS);
23946 TREE_TYPE (stmt) = void_type_node;
23947 OMP_SECTIONS_BODY (stmt) = substmt;
23948
23949 add_stmt (stmt);
23950 return stmt;
23951 }
23952
23953 /* OpenMP 2.5:
23954 # pragma omp sections sections-clause[optseq] newline
23955 sections-scope */
23956
23957 #define OMP_SECTIONS_CLAUSE_MASK \
23958 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23959 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23960 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
23961 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23962 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
23963
23964 static tree
23965 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
23966 {
23967 tree clauses, ret;
23968
23969 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
23970 "#pragma omp sections", pragma_tok);
23971
23972 ret = cp_parser_omp_sections_scope (parser);
23973 if (ret)
23974 OMP_SECTIONS_CLAUSES (ret) = clauses;
23975
23976 return ret;
23977 }
23978
23979 /* OpenMP 2.5:
23980 # pragma parallel parallel-clause new-line
23981 # pragma parallel for parallel-for-clause new-line
23982 # pragma parallel sections parallel-sections-clause new-line */
23983
23984 #define OMP_PARALLEL_CLAUSE_MASK \
23985 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
23986 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23987 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23988 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
23989 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
23990 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
23991 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23992 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
23993
23994 static tree
23995 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
23996 {
23997 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
23998 const char *p_name = "#pragma omp parallel";
23999 tree stmt, clauses, par_clause, ws_clause, block;
24000 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24001 unsigned int save;
24002 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24003
24004 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24005 {
24006 cp_lexer_consume_token (parser->lexer);
24007 p_kind = PRAGMA_OMP_PARALLEL_FOR;
24008 p_name = "#pragma omp parallel for";
24009 mask |= OMP_FOR_CLAUSE_MASK;
24010 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24011 }
24012 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24013 {
24014 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24015 const char *p = IDENTIFIER_POINTER (id);
24016 if (strcmp (p, "sections") == 0)
24017 {
24018 cp_lexer_consume_token (parser->lexer);
24019 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24020 p_name = "#pragma omp parallel sections";
24021 mask |= OMP_SECTIONS_CLAUSE_MASK;
24022 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24023 }
24024 }
24025
24026 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24027 block = begin_omp_parallel ();
24028 save = cp_parser_begin_omp_structured_block (parser);
24029
24030 switch (p_kind)
24031 {
24032 case PRAGMA_OMP_PARALLEL:
24033 cp_parser_statement (parser, NULL_TREE, false, NULL);
24034 par_clause = clauses;
24035 break;
24036
24037 case PRAGMA_OMP_PARALLEL_FOR:
24038 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24039 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24040 break;
24041
24042 case PRAGMA_OMP_PARALLEL_SECTIONS:
24043 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24044 stmt = cp_parser_omp_sections_scope (parser);
24045 if (stmt)
24046 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24047 break;
24048
24049 default:
24050 gcc_unreachable ();
24051 }
24052
24053 cp_parser_end_omp_structured_block (parser, save);
24054 stmt = finish_omp_parallel (par_clause, block);
24055 if (p_kind != PRAGMA_OMP_PARALLEL)
24056 OMP_PARALLEL_COMBINED (stmt) = 1;
24057 return stmt;
24058 }
24059
24060 /* OpenMP 2.5:
24061 # pragma omp single single-clause[optseq] new-line
24062 structured-block */
24063
24064 #define OMP_SINGLE_CLAUSE_MASK \
24065 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24066 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24067 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
24068 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24069
24070 static tree
24071 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24072 {
24073 tree stmt = make_node (OMP_SINGLE);
24074 TREE_TYPE (stmt) = void_type_node;
24075
24076 OMP_SINGLE_CLAUSES (stmt)
24077 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24078 "#pragma omp single", pragma_tok);
24079 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24080
24081 return add_stmt (stmt);
24082 }
24083
24084 /* OpenMP 3.0:
24085 # pragma omp task task-clause[optseq] new-line
24086 structured-block */
24087
24088 #define OMP_TASK_CLAUSE_MASK \
24089 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24090 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
24091 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24092 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24093 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24094 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24095
24096 static tree
24097 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
24098 {
24099 tree clauses, block;
24100 unsigned int save;
24101
24102 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
24103 "#pragma omp task", pragma_tok);
24104 block = begin_omp_task ();
24105 save = cp_parser_begin_omp_structured_block (parser);
24106 cp_parser_statement (parser, NULL_TREE, false, NULL);
24107 cp_parser_end_omp_structured_block (parser, save);
24108 return finish_omp_task (clauses, block);
24109 }
24110
24111 /* OpenMP 3.0:
24112 # pragma omp taskwait new-line */
24113
24114 static void
24115 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
24116 {
24117 cp_parser_require_pragma_eol (parser, pragma_tok);
24118 finish_omp_taskwait ();
24119 }
24120
24121 /* OpenMP 2.5:
24122 # pragma omp threadprivate (variable-list) */
24123
24124 static void
24125 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
24126 {
24127 tree vars;
24128
24129 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24130 cp_parser_require_pragma_eol (parser, pragma_tok);
24131
24132 finish_omp_threadprivate (vars);
24133 }
24134
24135 /* Main entry point to OpenMP statement pragmas. */
24136
24137 static void
24138 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
24139 {
24140 tree stmt;
24141
24142 switch (pragma_tok->pragma_kind)
24143 {
24144 case PRAGMA_OMP_ATOMIC:
24145 cp_parser_omp_atomic (parser, pragma_tok);
24146 return;
24147 case PRAGMA_OMP_CRITICAL:
24148 stmt = cp_parser_omp_critical (parser, pragma_tok);
24149 break;
24150 case PRAGMA_OMP_FOR:
24151 stmt = cp_parser_omp_for (parser, pragma_tok);
24152 break;
24153 case PRAGMA_OMP_MASTER:
24154 stmt = cp_parser_omp_master (parser, pragma_tok);
24155 break;
24156 case PRAGMA_OMP_ORDERED:
24157 stmt = cp_parser_omp_ordered (parser, pragma_tok);
24158 break;
24159 case PRAGMA_OMP_PARALLEL:
24160 stmt = cp_parser_omp_parallel (parser, pragma_tok);
24161 break;
24162 case PRAGMA_OMP_SECTIONS:
24163 stmt = cp_parser_omp_sections (parser, pragma_tok);
24164 break;
24165 case PRAGMA_OMP_SINGLE:
24166 stmt = cp_parser_omp_single (parser, pragma_tok);
24167 break;
24168 case PRAGMA_OMP_TASK:
24169 stmt = cp_parser_omp_task (parser, pragma_tok);
24170 break;
24171 default:
24172 gcc_unreachable ();
24173 }
24174
24175 if (stmt)
24176 SET_EXPR_LOCATION (stmt, pragma_tok->location);
24177 }
24178 \f
24179 /* The parser. */
24180
24181 static GTY (()) cp_parser *the_parser;
24182
24183 \f
24184 /* Special handling for the first token or line in the file. The first
24185 thing in the file might be #pragma GCC pch_preprocess, which loads a
24186 PCH file, which is a GC collection point. So we need to handle this
24187 first pragma without benefit of an existing lexer structure.
24188
24189 Always returns one token to the caller in *FIRST_TOKEN. This is
24190 either the true first token of the file, or the first token after
24191 the initial pragma. */
24192
24193 static void
24194 cp_parser_initial_pragma (cp_token *first_token)
24195 {
24196 tree name = NULL;
24197
24198 cp_lexer_get_preprocessor_token (NULL, first_token);
24199 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
24200 return;
24201
24202 cp_lexer_get_preprocessor_token (NULL, first_token);
24203 if (first_token->type == CPP_STRING)
24204 {
24205 name = first_token->u.value;
24206
24207 cp_lexer_get_preprocessor_token (NULL, first_token);
24208 if (first_token->type != CPP_PRAGMA_EOL)
24209 error_at (first_token->location,
24210 "junk at end of %<#pragma GCC pch_preprocess%>");
24211 }
24212 else
24213 error_at (first_token->location, "expected string literal");
24214
24215 /* Skip to the end of the pragma. */
24216 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
24217 cp_lexer_get_preprocessor_token (NULL, first_token);
24218
24219 /* Now actually load the PCH file. */
24220 if (name)
24221 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
24222
24223 /* Read one more token to return to our caller. We have to do this
24224 after reading the PCH file in, since its pointers have to be
24225 live. */
24226 cp_lexer_get_preprocessor_token (NULL, first_token);
24227 }
24228
24229 /* Normal parsing of a pragma token. Here we can (and must) use the
24230 regular lexer. */
24231
24232 static bool
24233 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
24234 {
24235 cp_token *pragma_tok;
24236 unsigned int id;
24237
24238 pragma_tok = cp_lexer_consume_token (parser->lexer);
24239 gcc_assert (pragma_tok->type == CPP_PRAGMA);
24240 parser->lexer->in_pragma = true;
24241
24242 id = pragma_tok->pragma_kind;
24243 switch (id)
24244 {
24245 case PRAGMA_GCC_PCH_PREPROCESS:
24246 error_at (pragma_tok->location,
24247 "%<#pragma GCC pch_preprocess%> must be first");
24248 break;
24249
24250 case PRAGMA_OMP_BARRIER:
24251 switch (context)
24252 {
24253 case pragma_compound:
24254 cp_parser_omp_barrier (parser, pragma_tok);
24255 return false;
24256 case pragma_stmt:
24257 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
24258 "used in compound statements");
24259 break;
24260 default:
24261 goto bad_stmt;
24262 }
24263 break;
24264
24265 case PRAGMA_OMP_FLUSH:
24266 switch (context)
24267 {
24268 case pragma_compound:
24269 cp_parser_omp_flush (parser, pragma_tok);
24270 return false;
24271 case pragma_stmt:
24272 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
24273 "used in compound statements");
24274 break;
24275 default:
24276 goto bad_stmt;
24277 }
24278 break;
24279
24280 case PRAGMA_OMP_TASKWAIT:
24281 switch (context)
24282 {
24283 case pragma_compound:
24284 cp_parser_omp_taskwait (parser, pragma_tok);
24285 return false;
24286 case pragma_stmt:
24287 error_at (pragma_tok->location,
24288 "%<#pragma omp taskwait%> may only be "
24289 "used in compound statements");
24290 break;
24291 default:
24292 goto bad_stmt;
24293 }
24294 break;
24295
24296 case PRAGMA_OMP_THREADPRIVATE:
24297 cp_parser_omp_threadprivate (parser, pragma_tok);
24298 return false;
24299
24300 case PRAGMA_OMP_ATOMIC:
24301 case PRAGMA_OMP_CRITICAL:
24302 case PRAGMA_OMP_FOR:
24303 case PRAGMA_OMP_MASTER:
24304 case PRAGMA_OMP_ORDERED:
24305 case PRAGMA_OMP_PARALLEL:
24306 case PRAGMA_OMP_SECTIONS:
24307 case PRAGMA_OMP_SINGLE:
24308 case PRAGMA_OMP_TASK:
24309 if (context == pragma_external)
24310 goto bad_stmt;
24311 cp_parser_omp_construct (parser, pragma_tok);
24312 return true;
24313
24314 case PRAGMA_OMP_SECTION:
24315 error_at (pragma_tok->location,
24316 "%<#pragma omp section%> may only be used in "
24317 "%<#pragma omp sections%> construct");
24318 break;
24319
24320 default:
24321 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
24322 c_invoke_pragma_handler (id);
24323 break;
24324
24325 bad_stmt:
24326 cp_parser_error (parser, "expected declaration specifiers");
24327 break;
24328 }
24329
24330 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
24331 return false;
24332 }
24333
24334 /* The interface the pragma parsers have to the lexer. */
24335
24336 enum cpp_ttype
24337 pragma_lex (tree *value)
24338 {
24339 cp_token *tok;
24340 enum cpp_ttype ret;
24341
24342 tok = cp_lexer_peek_token (the_parser->lexer);
24343
24344 ret = tok->type;
24345 *value = tok->u.value;
24346
24347 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
24348 ret = CPP_EOF;
24349 else if (ret == CPP_STRING)
24350 *value = cp_parser_string_literal (the_parser, false, false);
24351 else
24352 {
24353 cp_lexer_consume_token (the_parser->lexer);
24354 if (ret == CPP_KEYWORD)
24355 ret = CPP_NAME;
24356 }
24357
24358 return ret;
24359 }
24360
24361 \f
24362 /* External interface. */
24363
24364 /* Parse one entire translation unit. */
24365
24366 void
24367 c_parse_file (void)
24368 {
24369 static bool already_called = false;
24370
24371 if (already_called)
24372 {
24373 sorry ("inter-module optimizations not implemented for C++");
24374 return;
24375 }
24376 already_called = true;
24377
24378 the_parser = cp_parser_new ();
24379 push_deferring_access_checks (flag_access_control
24380 ? dk_no_deferred : dk_no_check);
24381 cp_parser_translation_unit (the_parser);
24382 the_parser = NULL;
24383 }
24384
24385 #include "gt-cp-parser.h"