]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
cfgloop.c (verify_loop_structure): Use %' in diagnostics.
[thirdparty/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 static inline cp_token *
506 cp_lexer_previous_token (cp_lexer *lexer)
507 {
508 cp_token_position tp;
509
510 if (lexer->next_token == &eof_token)
511 tp = lexer->last_token - 1;
512 else
513 tp = cp_lexer_token_position (lexer, true);
514
515 return cp_lexer_token_at (lexer, tp);
516 }
517
518 /* nonzero if we are presently saving tokens. */
519
520 static inline int
521 cp_lexer_saving_tokens (const cp_lexer* lexer)
522 {
523 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
524 }
525
526 /* Store the next token from the preprocessor in *TOKEN. Return true
527 if we reach EOF. If LEXER is NULL, assume we are handling an
528 initial #pragma pch_preprocess, and thus want the lexer to return
529 processed strings. */
530
531 static void
532 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
533 {
534 static int is_extern_c = 0;
535
536 /* Get a new token from the preprocessor. */
537 token->type
538 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
539 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
540 token->keyword = RID_MAX;
541 token->pragma_kind = PRAGMA_NONE;
542
543 /* On some systems, some header files are surrounded by an
544 implicit extern "C" block. Set a flag in the token if it
545 comes from such a header. */
546 is_extern_c += pending_lang_change;
547 pending_lang_change = 0;
548 token->implicit_extern_c = is_extern_c > 0;
549
550 /* Check to see if this token is a keyword. */
551 if (token->type == CPP_NAME)
552 {
553 if (C_IS_RESERVED_WORD (token->u.value))
554 {
555 /* Mark this token as a keyword. */
556 token->type = CPP_KEYWORD;
557 /* Record which keyword. */
558 token->keyword = C_RID_CODE (token->u.value);
559 }
560 else
561 {
562 if (warn_cxx0x_compat
563 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
564 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
565 {
566 /* Warn about the C++0x keyword (but still treat it as
567 an identifier). */
568 warning (OPT_Wc__0x_compat,
569 "identifier %qE will become a keyword in C++0x",
570 token->u.value);
571
572 /* Clear out the C_RID_CODE so we don't warn about this
573 particular identifier-turned-keyword again. */
574 C_SET_RID_CODE (token->u.value, RID_MAX);
575 }
576
577 token->ambiguous_p = false;
578 token->keyword = RID_MAX;
579 }
580 }
581 else if (token->type == CPP_AT_NAME)
582 {
583 /* This only happens in Objective-C++; it must be a keyword. */
584 token->type = CPP_KEYWORD;
585 switch (C_RID_CODE (token->u.value))
586 {
587 /* Replace 'class' with '@class', 'private' with '@private',
588 etc. This prevents confusion with the C++ keyword
589 'class', and makes the tokens consistent with other
590 Objective-C 'AT' keywords. For example '@class' is
591 reported as RID_AT_CLASS which is consistent with
592 '@synchronized', which is reported as
593 RID_AT_SYNCHRONIZED.
594 */
595 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
596 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
597 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
598 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
599 case RID_THROW: token->keyword = RID_AT_THROW; break;
600 case RID_TRY: token->keyword = RID_AT_TRY; break;
601 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
602 default: token->keyword = C_RID_CODE (token->u.value);
603 }
604 }
605 else if (token->type == CPP_PRAGMA)
606 {
607 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
608 token->pragma_kind = ((enum pragma_kind)
609 TREE_INT_CST_LOW (token->u.value));
610 token->u.value = NULL_TREE;
611 }
612 }
613
614 /* Update the globals input_location and the input file stack from TOKEN. */
615 static inline void
616 cp_lexer_set_source_position_from_token (cp_token *token)
617 {
618 if (token->type != CPP_EOF)
619 {
620 input_location = token->location;
621 }
622 }
623
624 /* Return a pointer to the next token in the token stream, but do not
625 consume it. */
626
627 static inline cp_token *
628 cp_lexer_peek_token (cp_lexer *lexer)
629 {
630 if (cp_lexer_debugging_p (lexer))
631 {
632 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
633 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
634 putc ('\n', cp_lexer_debug_stream);
635 }
636 return lexer->next_token;
637 }
638
639 /* Return true if the next token has the indicated TYPE. */
640
641 static inline bool
642 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
643 {
644 return cp_lexer_peek_token (lexer)->type == type;
645 }
646
647 /* Return true if the next token does not have the indicated TYPE. */
648
649 static inline bool
650 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
651 {
652 return !cp_lexer_next_token_is (lexer, type);
653 }
654
655 /* Return true if the next token is the indicated KEYWORD. */
656
657 static inline bool
658 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
659 {
660 return cp_lexer_peek_token (lexer)->keyword == keyword;
661 }
662
663 /* Return true if the next token is not the indicated KEYWORD. */
664
665 static inline bool
666 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
667 {
668 return cp_lexer_peek_token (lexer)->keyword != keyword;
669 }
670
671 /* Return true if the next token is a keyword for a decl-specifier. */
672
673 static bool
674 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
675 {
676 cp_token *token;
677
678 token = cp_lexer_peek_token (lexer);
679 switch (token->keyword)
680 {
681 /* auto specifier: storage-class-specifier in C++,
682 simple-type-specifier in C++0x. */
683 case RID_AUTO:
684 /* Storage classes. */
685 case RID_REGISTER:
686 case RID_STATIC:
687 case RID_EXTERN:
688 case RID_MUTABLE:
689 case RID_THREAD:
690 /* Elaborated type specifiers. */
691 case RID_ENUM:
692 case RID_CLASS:
693 case RID_STRUCT:
694 case RID_UNION:
695 case RID_TYPENAME:
696 /* Simple type specifiers. */
697 case RID_CHAR:
698 case RID_CHAR16:
699 case RID_CHAR32:
700 case RID_WCHAR:
701 case RID_BOOL:
702 case RID_SHORT:
703 case RID_INT:
704 case RID_LONG:
705 case RID_INT128:
706 case RID_SIGNED:
707 case RID_UNSIGNED:
708 case RID_FLOAT:
709 case RID_DOUBLE:
710 case RID_VOID:
711 /* GNU extensions. */
712 case RID_ATTRIBUTE:
713 case RID_TYPEOF:
714 /* C++0x extensions. */
715 case RID_DECLTYPE:
716 return true;
717
718 default:
719 return false;
720 }
721 }
722
723 /* Return a pointer to the Nth token in the token stream. If N is 1,
724 then this is precisely equivalent to cp_lexer_peek_token (except
725 that it is not inline). One would like to disallow that case, but
726 there is one case (cp_parser_nth_token_starts_template_id) where
727 the caller passes a variable for N and it might be 1. */
728
729 static cp_token *
730 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
731 {
732 cp_token *token;
733
734 /* N is 1-based, not zero-based. */
735 gcc_assert (n > 0);
736
737 if (cp_lexer_debugging_p (lexer))
738 fprintf (cp_lexer_debug_stream,
739 "cp_lexer: peeking ahead %ld at token: ", (long)n);
740
741 --n;
742 token = lexer->next_token;
743 gcc_assert (!n || token != &eof_token);
744 while (n != 0)
745 {
746 ++token;
747 if (token == lexer->last_token)
748 {
749 token = &eof_token;
750 break;
751 }
752
753 if (token->type != CPP_PURGED)
754 --n;
755 }
756
757 if (cp_lexer_debugging_p (lexer))
758 {
759 cp_lexer_print_token (cp_lexer_debug_stream, token);
760 putc ('\n', cp_lexer_debug_stream);
761 }
762
763 return token;
764 }
765
766 /* Return the next token, and advance the lexer's next_token pointer
767 to point to the next non-purged token. */
768
769 static cp_token *
770 cp_lexer_consume_token (cp_lexer* lexer)
771 {
772 cp_token *token = lexer->next_token;
773
774 gcc_assert (token != &eof_token);
775 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
776
777 do
778 {
779 lexer->next_token++;
780 if (lexer->next_token == lexer->last_token)
781 {
782 lexer->next_token = &eof_token;
783 break;
784 }
785
786 }
787 while (lexer->next_token->type == CPP_PURGED);
788
789 cp_lexer_set_source_position_from_token (token);
790
791 /* Provide debugging output. */
792 if (cp_lexer_debugging_p (lexer))
793 {
794 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
795 cp_lexer_print_token (cp_lexer_debug_stream, token);
796 putc ('\n', cp_lexer_debug_stream);
797 }
798
799 return token;
800 }
801
802 /* Permanently remove the next token from the token stream, and
803 advance the next_token pointer to refer to the next non-purged
804 token. */
805
806 static void
807 cp_lexer_purge_token (cp_lexer *lexer)
808 {
809 cp_token *tok = lexer->next_token;
810
811 gcc_assert (tok != &eof_token);
812 tok->type = CPP_PURGED;
813 tok->location = UNKNOWN_LOCATION;
814 tok->u.value = NULL_TREE;
815 tok->keyword = RID_MAX;
816
817 do
818 {
819 tok++;
820 if (tok == lexer->last_token)
821 {
822 tok = &eof_token;
823 break;
824 }
825 }
826 while (tok->type == CPP_PURGED);
827 lexer->next_token = tok;
828 }
829
830 /* Permanently remove all tokens after TOK, up to, but not
831 including, the token that will be returned next by
832 cp_lexer_peek_token. */
833
834 static void
835 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
836 {
837 cp_token *peek = lexer->next_token;
838
839 if (peek == &eof_token)
840 peek = lexer->last_token;
841
842 gcc_assert (tok < peek);
843
844 for ( tok += 1; tok != peek; tok += 1)
845 {
846 tok->type = CPP_PURGED;
847 tok->location = UNKNOWN_LOCATION;
848 tok->u.value = NULL_TREE;
849 tok->keyword = RID_MAX;
850 }
851 }
852
853 /* Begin saving tokens. All tokens consumed after this point will be
854 preserved. */
855
856 static void
857 cp_lexer_save_tokens (cp_lexer* lexer)
858 {
859 /* Provide debugging output. */
860 if (cp_lexer_debugging_p (lexer))
861 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
862
863 VEC_safe_push (cp_token_position, heap,
864 lexer->saved_tokens, lexer->next_token);
865 }
866
867 /* Commit to the portion of the token stream most recently saved. */
868
869 static void
870 cp_lexer_commit_tokens (cp_lexer* lexer)
871 {
872 /* Provide debugging output. */
873 if (cp_lexer_debugging_p (lexer))
874 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
875
876 VEC_pop (cp_token_position, lexer->saved_tokens);
877 }
878
879 /* Return all tokens saved since the last call to cp_lexer_save_tokens
880 to the token stream. Stop saving tokens. */
881
882 static void
883 cp_lexer_rollback_tokens (cp_lexer* lexer)
884 {
885 /* Provide debugging output. */
886 if (cp_lexer_debugging_p (lexer))
887 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
888
889 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
890 }
891
892 /* Print a representation of the TOKEN on the STREAM. */
893
894 #ifdef ENABLE_CHECKING
895
896 static void
897 cp_lexer_print_token (FILE * stream, cp_token *token)
898 {
899 /* We don't use cpp_type2name here because the parser defines
900 a few tokens of its own. */
901 static const char *const token_names[] = {
902 /* cpplib-defined token types */
903 #define OP(e, s) #e,
904 #define TK(e, s) #e,
905 TTYPE_TABLE
906 #undef OP
907 #undef TK
908 /* C++ parser token types - see "Manifest constants", above. */
909 "KEYWORD",
910 "TEMPLATE_ID",
911 "NESTED_NAME_SPECIFIER",
912 "PURGED"
913 };
914
915 /* If we have a name for the token, print it out. Otherwise, we
916 simply give the numeric code. */
917 gcc_assert (token->type < ARRAY_SIZE(token_names));
918 fputs (token_names[token->type], stream);
919
920 /* For some tokens, print the associated data. */
921 switch (token->type)
922 {
923 case CPP_KEYWORD:
924 /* Some keywords have a value that is not an IDENTIFIER_NODE.
925 For example, `struct' is mapped to an INTEGER_CST. */
926 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
927 break;
928 /* else fall through */
929 case CPP_NAME:
930 fputs (IDENTIFIER_POINTER (token->u.value), stream);
931 break;
932
933 case CPP_STRING:
934 case CPP_STRING16:
935 case CPP_STRING32:
936 case CPP_WSTRING:
937 case CPP_UTF8STRING:
938 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
939 break;
940
941 default:
942 break;
943 }
944 }
945
946 /* Start emitting debugging information. */
947
948 static void
949 cp_lexer_start_debugging (cp_lexer* lexer)
950 {
951 lexer->debugging_p = true;
952 }
953
954 /* Stop emitting debugging information. */
955
956 static void
957 cp_lexer_stop_debugging (cp_lexer* lexer)
958 {
959 lexer->debugging_p = false;
960 }
961
962 #endif /* ENABLE_CHECKING */
963
964 /* Create a new cp_token_cache, representing a range of tokens. */
965
966 static cp_token_cache *
967 cp_token_cache_new (cp_token *first, cp_token *last)
968 {
969 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
970 cache->first = first;
971 cache->last = last;
972 return cache;
973 }
974
975 \f
976 /* Decl-specifiers. */
977
978 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
979
980 static void
981 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
982 {
983 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
984 }
985
986 /* Declarators. */
987
988 /* Nothing other than the parser should be creating declarators;
989 declarators are a semi-syntactic representation of C++ entities.
990 Other parts of the front end that need to create entities (like
991 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
992
993 static cp_declarator *make_call_declarator
994 (cp_declarator *, tree, cp_cv_quals, tree, tree);
995 static cp_declarator *make_array_declarator
996 (cp_declarator *, tree);
997 static cp_declarator *make_pointer_declarator
998 (cp_cv_quals, cp_declarator *);
999 static cp_declarator *make_reference_declarator
1000 (cp_cv_quals, cp_declarator *, bool);
1001 static cp_parameter_declarator *make_parameter_declarator
1002 (cp_decl_specifier_seq *, cp_declarator *, tree);
1003 static cp_declarator *make_ptrmem_declarator
1004 (cp_cv_quals, tree, cp_declarator *);
1005
1006 /* An erroneous declarator. */
1007 static cp_declarator *cp_error_declarator;
1008
1009 /* The obstack on which declarators and related data structures are
1010 allocated. */
1011 static struct obstack declarator_obstack;
1012
1013 /* Alloc BYTES from the declarator memory pool. */
1014
1015 static inline void *
1016 alloc_declarator (size_t bytes)
1017 {
1018 return obstack_alloc (&declarator_obstack, bytes);
1019 }
1020
1021 /* Allocate a declarator of the indicated KIND. Clear fields that are
1022 common to all declarators. */
1023
1024 static cp_declarator *
1025 make_declarator (cp_declarator_kind kind)
1026 {
1027 cp_declarator *declarator;
1028
1029 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1030 declarator->kind = kind;
1031 declarator->attributes = NULL_TREE;
1032 declarator->declarator = NULL;
1033 declarator->parameter_pack_p = false;
1034 declarator->id_loc = UNKNOWN_LOCATION;
1035
1036 return declarator;
1037 }
1038
1039 /* Make a declarator for a generalized identifier. If
1040 QUALIFYING_SCOPE is non-NULL, the identifier is
1041 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1042 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1043 is, if any. */
1044
1045 static cp_declarator *
1046 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1047 special_function_kind sfk)
1048 {
1049 cp_declarator *declarator;
1050
1051 /* It is valid to write:
1052
1053 class C { void f(); };
1054 typedef C D;
1055 void D::f();
1056
1057 The standard is not clear about whether `typedef const C D' is
1058 legal; as of 2002-09-15 the committee is considering that
1059 question. EDG 3.0 allows that syntax. Therefore, we do as
1060 well. */
1061 if (qualifying_scope && TYPE_P (qualifying_scope))
1062 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1063
1064 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1065 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1066 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1067
1068 declarator = make_declarator (cdk_id);
1069 declarator->u.id.qualifying_scope = qualifying_scope;
1070 declarator->u.id.unqualified_name = unqualified_name;
1071 declarator->u.id.sfk = sfk;
1072
1073 return declarator;
1074 }
1075
1076 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1077 of modifiers such as const or volatile to apply to the pointer
1078 type, represented as identifiers. */
1079
1080 cp_declarator *
1081 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1082 {
1083 cp_declarator *declarator;
1084
1085 declarator = make_declarator (cdk_pointer);
1086 declarator->declarator = target;
1087 declarator->u.pointer.qualifiers = cv_qualifiers;
1088 declarator->u.pointer.class_type = NULL_TREE;
1089 if (target)
1090 {
1091 declarator->id_loc = target->id_loc;
1092 declarator->parameter_pack_p = target->parameter_pack_p;
1093 target->parameter_pack_p = false;
1094 }
1095 else
1096 declarator->parameter_pack_p = false;
1097
1098 return declarator;
1099 }
1100
1101 /* Like make_pointer_declarator -- but for references. */
1102
1103 cp_declarator *
1104 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1105 bool rvalue_ref)
1106 {
1107 cp_declarator *declarator;
1108
1109 declarator = make_declarator (cdk_reference);
1110 declarator->declarator = target;
1111 declarator->u.reference.qualifiers = cv_qualifiers;
1112 declarator->u.reference.rvalue_ref = rvalue_ref;
1113 if (target)
1114 {
1115 declarator->id_loc = target->id_loc;
1116 declarator->parameter_pack_p = target->parameter_pack_p;
1117 target->parameter_pack_p = false;
1118 }
1119 else
1120 declarator->parameter_pack_p = false;
1121
1122 return declarator;
1123 }
1124
1125 /* Like make_pointer_declarator -- but for a pointer to a non-static
1126 member of CLASS_TYPE. */
1127
1128 cp_declarator *
1129 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1130 cp_declarator *pointee)
1131 {
1132 cp_declarator *declarator;
1133
1134 declarator = make_declarator (cdk_ptrmem);
1135 declarator->declarator = pointee;
1136 declarator->u.pointer.qualifiers = cv_qualifiers;
1137 declarator->u.pointer.class_type = class_type;
1138
1139 if (pointee)
1140 {
1141 declarator->parameter_pack_p = pointee->parameter_pack_p;
1142 pointee->parameter_pack_p = false;
1143 }
1144 else
1145 declarator->parameter_pack_p = false;
1146
1147 return declarator;
1148 }
1149
1150 /* Make a declarator for the function given by TARGET, with the
1151 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1152 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1153 indicates what exceptions can be thrown. */
1154
1155 cp_declarator *
1156 make_call_declarator (cp_declarator *target,
1157 tree parms,
1158 cp_cv_quals cv_qualifiers,
1159 tree exception_specification,
1160 tree late_return_type)
1161 {
1162 cp_declarator *declarator;
1163
1164 declarator = make_declarator (cdk_function);
1165 declarator->declarator = target;
1166 declarator->u.function.parameters = parms;
1167 declarator->u.function.qualifiers = cv_qualifiers;
1168 declarator->u.function.exception_specification = exception_specification;
1169 declarator->u.function.late_return_type = late_return_type;
1170 if (target)
1171 {
1172 declarator->id_loc = target->id_loc;
1173 declarator->parameter_pack_p = target->parameter_pack_p;
1174 target->parameter_pack_p = false;
1175 }
1176 else
1177 declarator->parameter_pack_p = false;
1178
1179 return declarator;
1180 }
1181
1182 /* Make a declarator for an array of BOUNDS elements, each of which is
1183 defined by ELEMENT. */
1184
1185 cp_declarator *
1186 make_array_declarator (cp_declarator *element, tree bounds)
1187 {
1188 cp_declarator *declarator;
1189
1190 declarator = make_declarator (cdk_array);
1191 declarator->declarator = element;
1192 declarator->u.array.bounds = bounds;
1193 if (element)
1194 {
1195 declarator->id_loc = element->id_loc;
1196 declarator->parameter_pack_p = element->parameter_pack_p;
1197 element->parameter_pack_p = false;
1198 }
1199 else
1200 declarator->parameter_pack_p = false;
1201
1202 return declarator;
1203 }
1204
1205 /* Determine whether the declarator we've seen so far can be a
1206 parameter pack, when followed by an ellipsis. */
1207 static bool
1208 declarator_can_be_parameter_pack (cp_declarator *declarator)
1209 {
1210 /* Search for a declarator name, or any other declarator that goes
1211 after the point where the ellipsis could appear in a parameter
1212 pack. If we find any of these, then this declarator can not be
1213 made into a parameter pack. */
1214 bool found = false;
1215 while (declarator && !found)
1216 {
1217 switch ((int)declarator->kind)
1218 {
1219 case cdk_id:
1220 case cdk_array:
1221 found = true;
1222 break;
1223
1224 case cdk_error:
1225 return true;
1226
1227 default:
1228 declarator = declarator->declarator;
1229 break;
1230 }
1231 }
1232
1233 return !found;
1234 }
1235
1236 cp_parameter_declarator *no_parameters;
1237
1238 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1239 DECLARATOR and DEFAULT_ARGUMENT. */
1240
1241 cp_parameter_declarator *
1242 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1243 cp_declarator *declarator,
1244 tree default_argument)
1245 {
1246 cp_parameter_declarator *parameter;
1247
1248 parameter = ((cp_parameter_declarator *)
1249 alloc_declarator (sizeof (cp_parameter_declarator)));
1250 parameter->next = NULL;
1251 if (decl_specifiers)
1252 parameter->decl_specifiers = *decl_specifiers;
1253 else
1254 clear_decl_specs (&parameter->decl_specifiers);
1255 parameter->declarator = declarator;
1256 parameter->default_argument = default_argument;
1257 parameter->ellipsis_p = false;
1258
1259 return parameter;
1260 }
1261
1262 /* Returns true iff DECLARATOR is a declaration for a function. */
1263
1264 static bool
1265 function_declarator_p (const cp_declarator *declarator)
1266 {
1267 while (declarator)
1268 {
1269 if (declarator->kind == cdk_function
1270 && declarator->declarator->kind == cdk_id)
1271 return true;
1272 if (declarator->kind == cdk_id
1273 || declarator->kind == cdk_error)
1274 return false;
1275 declarator = declarator->declarator;
1276 }
1277 return false;
1278 }
1279
1280 /* The parser. */
1281
1282 /* Overview
1283 --------
1284
1285 A cp_parser parses the token stream as specified by the C++
1286 grammar. Its job is purely parsing, not semantic analysis. For
1287 example, the parser breaks the token stream into declarators,
1288 expressions, statements, and other similar syntactic constructs.
1289 It does not check that the types of the expressions on either side
1290 of an assignment-statement are compatible, or that a function is
1291 not declared with a parameter of type `void'.
1292
1293 The parser invokes routines elsewhere in the compiler to perform
1294 semantic analysis and to build up the abstract syntax tree for the
1295 code processed.
1296
1297 The parser (and the template instantiation code, which is, in a
1298 way, a close relative of parsing) are the only parts of the
1299 compiler that should be calling push_scope and pop_scope, or
1300 related functions. The parser (and template instantiation code)
1301 keeps track of what scope is presently active; everything else
1302 should simply honor that. (The code that generates static
1303 initializers may also need to set the scope, in order to check
1304 access control correctly when emitting the initializers.)
1305
1306 Methodology
1307 -----------
1308
1309 The parser is of the standard recursive-descent variety. Upcoming
1310 tokens in the token stream are examined in order to determine which
1311 production to use when parsing a non-terminal. Some C++ constructs
1312 require arbitrary look ahead to disambiguate. For example, it is
1313 impossible, in the general case, to tell whether a statement is an
1314 expression or declaration without scanning the entire statement.
1315 Therefore, the parser is capable of "parsing tentatively." When the
1316 parser is not sure what construct comes next, it enters this mode.
1317 Then, while we attempt to parse the construct, the parser queues up
1318 error messages, rather than issuing them immediately, and saves the
1319 tokens it consumes. If the construct is parsed successfully, the
1320 parser "commits", i.e., it issues any queued error messages and
1321 the tokens that were being preserved are permanently discarded.
1322 If, however, the construct is not parsed successfully, the parser
1323 rolls back its state completely so that it can resume parsing using
1324 a different alternative.
1325
1326 Future Improvements
1327 -------------------
1328
1329 The performance of the parser could probably be improved substantially.
1330 We could often eliminate the need to parse tentatively by looking ahead
1331 a little bit. In some places, this approach might not entirely eliminate
1332 the need to parse tentatively, but it might still speed up the average
1333 case. */
1334
1335 /* Flags that are passed to some parsing functions. These values can
1336 be bitwise-ored together. */
1337
1338 enum
1339 {
1340 /* No flags. */
1341 CP_PARSER_FLAGS_NONE = 0x0,
1342 /* The construct is optional. If it is not present, then no error
1343 should be issued. */
1344 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1345 /* When parsing a type-specifier, treat user-defined type-names
1346 as non-type identifiers. */
1347 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1348 /* When parsing a type-specifier, do not try to parse a class-specifier
1349 or enum-specifier. */
1350 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1351 /* When parsing a decl-specifier-seq, only allow type-specifier or
1352 constexpr. */
1353 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1354 };
1355
1356 /* This type is used for parameters and variables which hold
1357 combinations of the above flags. */
1358 typedef int cp_parser_flags;
1359
1360 /* The different kinds of declarators we want to parse. */
1361
1362 typedef enum cp_parser_declarator_kind
1363 {
1364 /* We want an abstract declarator. */
1365 CP_PARSER_DECLARATOR_ABSTRACT,
1366 /* We want a named declarator. */
1367 CP_PARSER_DECLARATOR_NAMED,
1368 /* We don't mind, but the name must be an unqualified-id. */
1369 CP_PARSER_DECLARATOR_EITHER
1370 } cp_parser_declarator_kind;
1371
1372 /* The precedence values used to parse binary expressions. The minimum value
1373 of PREC must be 1, because zero is reserved to quickly discriminate
1374 binary operators from other tokens. */
1375
1376 enum cp_parser_prec
1377 {
1378 PREC_NOT_OPERATOR,
1379 PREC_LOGICAL_OR_EXPRESSION,
1380 PREC_LOGICAL_AND_EXPRESSION,
1381 PREC_INCLUSIVE_OR_EXPRESSION,
1382 PREC_EXCLUSIVE_OR_EXPRESSION,
1383 PREC_AND_EXPRESSION,
1384 PREC_EQUALITY_EXPRESSION,
1385 PREC_RELATIONAL_EXPRESSION,
1386 PREC_SHIFT_EXPRESSION,
1387 PREC_ADDITIVE_EXPRESSION,
1388 PREC_MULTIPLICATIVE_EXPRESSION,
1389 PREC_PM_EXPRESSION,
1390 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1391 };
1392
1393 /* A mapping from a token type to a corresponding tree node type, with a
1394 precedence value. */
1395
1396 typedef struct cp_parser_binary_operations_map_node
1397 {
1398 /* The token type. */
1399 enum cpp_ttype token_type;
1400 /* The corresponding tree code. */
1401 enum tree_code tree_type;
1402 /* The precedence of this operator. */
1403 enum cp_parser_prec prec;
1404 } cp_parser_binary_operations_map_node;
1405
1406 /* The status of a tentative parse. */
1407
1408 typedef enum cp_parser_status_kind
1409 {
1410 /* No errors have occurred. */
1411 CP_PARSER_STATUS_KIND_NO_ERROR,
1412 /* An error has occurred. */
1413 CP_PARSER_STATUS_KIND_ERROR,
1414 /* We are committed to this tentative parse, whether or not an error
1415 has occurred. */
1416 CP_PARSER_STATUS_KIND_COMMITTED
1417 } cp_parser_status_kind;
1418
1419 typedef struct cp_parser_expression_stack_entry
1420 {
1421 /* Left hand side of the binary operation we are currently
1422 parsing. */
1423 tree lhs;
1424 /* Original tree code for left hand side, if it was a binary
1425 expression itself (used for -Wparentheses). */
1426 enum tree_code lhs_type;
1427 /* Tree code for the binary operation we are parsing. */
1428 enum tree_code tree_type;
1429 /* Precedence of the binary operation we are parsing. */
1430 enum cp_parser_prec prec;
1431 } cp_parser_expression_stack_entry;
1432
1433 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1434 entries because precedence levels on the stack are monotonically
1435 increasing. */
1436 typedef struct cp_parser_expression_stack_entry
1437 cp_parser_expression_stack[NUM_PREC_VALUES];
1438
1439 /* Context that is saved and restored when parsing tentatively. */
1440 typedef struct GTY (()) cp_parser_context {
1441 /* If this is a tentative parsing context, the status of the
1442 tentative parse. */
1443 enum cp_parser_status_kind status;
1444 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1445 that are looked up in this context must be looked up both in the
1446 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1447 the context of the containing expression. */
1448 tree object_type;
1449
1450 /* The next parsing context in the stack. */
1451 struct cp_parser_context *next;
1452 } cp_parser_context;
1453
1454 /* Prototypes. */
1455
1456 /* Constructors and destructors. */
1457
1458 static cp_parser_context *cp_parser_context_new
1459 (cp_parser_context *);
1460
1461 /* Class variables. */
1462
1463 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1464
1465 /* The operator-precedence table used by cp_parser_binary_expression.
1466 Transformed into an associative array (binops_by_token) by
1467 cp_parser_new. */
1468
1469 static const cp_parser_binary_operations_map_node binops[] = {
1470 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1471 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1472
1473 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1474 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1475 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1476
1477 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1478 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1479
1480 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1481 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1482
1483 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1484 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1485 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1486 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1487
1488 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1489 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1490
1491 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1492
1493 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1494
1495 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1496
1497 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1498
1499 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1500 };
1501
1502 /* The same as binops, but initialized by cp_parser_new so that
1503 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1504 for speed. */
1505 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1506
1507 /* Constructors and destructors. */
1508
1509 /* Construct a new context. The context below this one on the stack
1510 is given by NEXT. */
1511
1512 static cp_parser_context *
1513 cp_parser_context_new (cp_parser_context* next)
1514 {
1515 cp_parser_context *context;
1516
1517 /* Allocate the storage. */
1518 if (cp_parser_context_free_list != NULL)
1519 {
1520 /* Pull the first entry from the free list. */
1521 context = cp_parser_context_free_list;
1522 cp_parser_context_free_list = context->next;
1523 memset (context, 0, sizeof (*context));
1524 }
1525 else
1526 context = ggc_alloc_cleared_cp_parser_context ();
1527
1528 /* No errors have occurred yet in this context. */
1529 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1530 /* If this is not the bottommost context, copy information that we
1531 need from the previous context. */
1532 if (next)
1533 {
1534 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1535 expression, then we are parsing one in this context, too. */
1536 context->object_type = next->object_type;
1537 /* Thread the stack. */
1538 context->next = next;
1539 }
1540
1541 return context;
1542 }
1543
1544 /* An entry in a queue of function arguments that require post-processing. */
1545
1546 typedef struct GTY(()) cp_default_arg_entry_d {
1547 /* The current_class_type when we parsed this arg. */
1548 tree class_type;
1549
1550 /* The function decl itself. */
1551 tree decl;
1552 } cp_default_arg_entry;
1553
1554 DEF_VEC_O(cp_default_arg_entry);
1555 DEF_VEC_ALLOC_O(cp_default_arg_entry,gc);
1556
1557 /* An entry in a stack for member functions of local classes. */
1558
1559 typedef struct GTY(()) cp_unparsed_functions_entry_d {
1560 /* Functions with default arguments that require post-processing.
1561 Functions appear in this list in declaration order. */
1562 VEC(cp_default_arg_entry,gc) *funs_with_default_args;
1563
1564 /* Functions with defintions that require post-processing. Functions
1565 appear in this list in declaration order. */
1566 VEC(tree,gc) *funs_with_definitions;
1567 } cp_unparsed_functions_entry;
1568
1569 DEF_VEC_O(cp_unparsed_functions_entry);
1570 DEF_VEC_ALLOC_O(cp_unparsed_functions_entry,gc);
1571
1572 /* The cp_parser structure represents the C++ parser. */
1573
1574 typedef struct GTY(()) cp_parser {
1575 /* The lexer from which we are obtaining tokens. */
1576 cp_lexer *lexer;
1577
1578 /* The scope in which names should be looked up. If NULL_TREE, then
1579 we look up names in the scope that is currently open in the
1580 source program. If non-NULL, this is either a TYPE or
1581 NAMESPACE_DECL for the scope in which we should look. It can
1582 also be ERROR_MARK, when we've parsed a bogus scope.
1583
1584 This value is not cleared automatically after a name is looked
1585 up, so we must be careful to clear it before starting a new look
1586 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1587 will look up `Z' in the scope of `X', rather than the current
1588 scope.) Unfortunately, it is difficult to tell when name lookup
1589 is complete, because we sometimes peek at a token, look it up,
1590 and then decide not to consume it. */
1591 tree scope;
1592
1593 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1594 last lookup took place. OBJECT_SCOPE is used if an expression
1595 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1596 respectively. QUALIFYING_SCOPE is used for an expression of the
1597 form "X::Y"; it refers to X. */
1598 tree object_scope;
1599 tree qualifying_scope;
1600
1601 /* A stack of parsing contexts. All but the bottom entry on the
1602 stack will be tentative contexts.
1603
1604 We parse tentatively in order to determine which construct is in
1605 use in some situations. For example, in order to determine
1606 whether a statement is an expression-statement or a
1607 declaration-statement we parse it tentatively as a
1608 declaration-statement. If that fails, we then reparse the same
1609 token stream as an expression-statement. */
1610 cp_parser_context *context;
1611
1612 /* True if we are parsing GNU C++. If this flag is not set, then
1613 GNU extensions are not recognized. */
1614 bool allow_gnu_extensions_p;
1615
1616 /* TRUE if the `>' token should be interpreted as the greater-than
1617 operator. FALSE if it is the end of a template-id or
1618 template-parameter-list. In C++0x mode, this flag also applies to
1619 `>>' tokens, which are viewed as two consecutive `>' tokens when
1620 this flag is FALSE. */
1621 bool greater_than_is_operator_p;
1622
1623 /* TRUE if default arguments are allowed within a parameter list
1624 that starts at this point. FALSE if only a gnu extension makes
1625 them permissible. */
1626 bool default_arg_ok_p;
1627
1628 /* TRUE if we are parsing an integral constant-expression. See
1629 [expr.const] for a precise definition. */
1630 bool integral_constant_expression_p;
1631
1632 /* TRUE if we are parsing an integral constant-expression -- but a
1633 non-constant expression should be permitted as well. This flag
1634 is used when parsing an array bound so that GNU variable-length
1635 arrays are tolerated. */
1636 bool allow_non_integral_constant_expression_p;
1637
1638 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1639 been seen that makes the expression non-constant. */
1640 bool non_integral_constant_expression_p;
1641
1642 /* TRUE if local variable names and `this' are forbidden in the
1643 current context. */
1644 bool local_variables_forbidden_p;
1645
1646 /* TRUE if the declaration we are parsing is part of a
1647 linkage-specification of the form `extern string-literal
1648 declaration'. */
1649 bool in_unbraced_linkage_specification_p;
1650
1651 /* TRUE if we are presently parsing a declarator, after the
1652 direct-declarator. */
1653 bool in_declarator_p;
1654
1655 /* TRUE if we are presently parsing a template-argument-list. */
1656 bool in_template_argument_list_p;
1657
1658 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1659 to IN_OMP_BLOCK if parsing OpenMP structured block and
1660 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1661 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1662 iteration-statement, OpenMP block or loop within that switch. */
1663 #define IN_SWITCH_STMT 1
1664 #define IN_ITERATION_STMT 2
1665 #define IN_OMP_BLOCK 4
1666 #define IN_OMP_FOR 8
1667 #define IN_IF_STMT 16
1668 unsigned char in_statement;
1669
1670 /* TRUE if we are presently parsing the body of a switch statement.
1671 Note that this doesn't quite overlap with in_statement above.
1672 The difference relates to giving the right sets of error messages:
1673 "case not in switch" vs "break statement used with OpenMP...". */
1674 bool in_switch_statement_p;
1675
1676 /* TRUE if we are parsing a type-id in an expression context. In
1677 such a situation, both "type (expr)" and "type (type)" are valid
1678 alternatives. */
1679 bool in_type_id_in_expr_p;
1680
1681 /* TRUE if we are currently in a header file where declarations are
1682 implicitly extern "C". */
1683 bool implicit_extern_c;
1684
1685 /* TRUE if strings in expressions should be translated to the execution
1686 character set. */
1687 bool translate_strings_p;
1688
1689 /* TRUE if we are presently parsing the body of a function, but not
1690 a local class. */
1691 bool in_function_body;
1692
1693 /* If non-NULL, then we are parsing a construct where new type
1694 definitions are not permitted. The string stored here will be
1695 issued as an error message if a type is defined. */
1696 const char *type_definition_forbidden_message;
1697
1698 /* A stack used for member functions of local classes. The lists
1699 contained in an individual entry can only be processed once the
1700 outermost class being defined is complete. */
1701 VEC(cp_unparsed_functions_entry,gc) *unparsed_queues;
1702
1703 /* The number of classes whose definitions are currently in
1704 progress. */
1705 unsigned num_classes_being_defined;
1706
1707 /* The number of template parameter lists that apply directly to the
1708 current declaration. */
1709 unsigned num_template_parameter_lists;
1710 } cp_parser;
1711
1712 /* Managing the unparsed function queues. */
1713
1714 #define unparsed_funs_with_default_args \
1715 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1716 #define unparsed_funs_with_definitions \
1717 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1718
1719 static void
1720 push_unparsed_function_queues (cp_parser *parser)
1721 {
1722 VEC_safe_push (cp_unparsed_functions_entry, gc,
1723 parser->unparsed_queues, NULL);
1724 unparsed_funs_with_default_args = NULL;
1725 unparsed_funs_with_definitions = make_tree_vector ();
1726 }
1727
1728 static void
1729 pop_unparsed_function_queues (cp_parser *parser)
1730 {
1731 release_tree_vector (unparsed_funs_with_definitions);
1732 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1733 }
1734
1735 /* Prototypes. */
1736
1737 /* Constructors and destructors. */
1738
1739 static cp_parser *cp_parser_new
1740 (void);
1741
1742 /* Routines to parse various constructs.
1743
1744 Those that return `tree' will return the error_mark_node (rather
1745 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1746 Sometimes, they will return an ordinary node if error-recovery was
1747 attempted, even though a parse error occurred. So, to check
1748 whether or not a parse error occurred, you should always use
1749 cp_parser_error_occurred. If the construct is optional (indicated
1750 either by an `_opt' in the name of the function that does the
1751 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1752 the construct is not present. */
1753
1754 /* Lexical conventions [gram.lex] */
1755
1756 static tree cp_parser_identifier
1757 (cp_parser *);
1758 static tree cp_parser_string_literal
1759 (cp_parser *, bool, bool);
1760
1761 /* Basic concepts [gram.basic] */
1762
1763 static bool cp_parser_translation_unit
1764 (cp_parser *);
1765
1766 /* Expressions [gram.expr] */
1767
1768 static tree cp_parser_primary_expression
1769 (cp_parser *, bool, bool, bool, cp_id_kind *);
1770 static tree cp_parser_id_expression
1771 (cp_parser *, bool, bool, bool *, bool, bool);
1772 static tree cp_parser_unqualified_id
1773 (cp_parser *, bool, bool, bool, bool);
1774 static tree cp_parser_nested_name_specifier_opt
1775 (cp_parser *, bool, bool, bool, bool);
1776 static tree cp_parser_nested_name_specifier
1777 (cp_parser *, bool, bool, bool, bool);
1778 static tree cp_parser_qualifying_entity
1779 (cp_parser *, bool, bool, bool, bool, bool);
1780 static tree cp_parser_postfix_expression
1781 (cp_parser *, bool, bool, bool, cp_id_kind *);
1782 static tree cp_parser_postfix_open_square_expression
1783 (cp_parser *, tree, bool);
1784 static tree cp_parser_postfix_dot_deref_expression
1785 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1786 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1787 (cp_parser *, int, bool, bool, bool *);
1788 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1789 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1790 static void cp_parser_pseudo_destructor_name
1791 (cp_parser *, tree *, tree *);
1792 static tree cp_parser_unary_expression
1793 (cp_parser *, bool, bool, cp_id_kind *);
1794 static enum tree_code cp_parser_unary_operator
1795 (cp_token *);
1796 static tree cp_parser_new_expression
1797 (cp_parser *);
1798 static VEC(tree,gc) *cp_parser_new_placement
1799 (cp_parser *);
1800 static tree cp_parser_new_type_id
1801 (cp_parser *, tree *);
1802 static cp_declarator *cp_parser_new_declarator_opt
1803 (cp_parser *);
1804 static cp_declarator *cp_parser_direct_new_declarator
1805 (cp_parser *);
1806 static VEC(tree,gc) *cp_parser_new_initializer
1807 (cp_parser *);
1808 static tree cp_parser_delete_expression
1809 (cp_parser *);
1810 static tree cp_parser_cast_expression
1811 (cp_parser *, bool, bool, cp_id_kind *);
1812 static tree cp_parser_binary_expression
1813 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1814 static tree cp_parser_question_colon_clause
1815 (cp_parser *, tree);
1816 static tree cp_parser_assignment_expression
1817 (cp_parser *, bool, cp_id_kind *);
1818 static enum tree_code cp_parser_assignment_operator_opt
1819 (cp_parser *);
1820 static tree cp_parser_expression
1821 (cp_parser *, bool, cp_id_kind *);
1822 static tree cp_parser_constant_expression
1823 (cp_parser *, bool, bool *);
1824 static tree cp_parser_builtin_offsetof
1825 (cp_parser *);
1826 static tree cp_parser_lambda_expression
1827 (cp_parser *);
1828 static void cp_parser_lambda_introducer
1829 (cp_parser *, tree);
1830 static void cp_parser_lambda_declarator_opt
1831 (cp_parser *, tree);
1832 static void cp_parser_lambda_body
1833 (cp_parser *, tree);
1834
1835 /* Statements [gram.stmt.stmt] */
1836
1837 static void cp_parser_statement
1838 (cp_parser *, tree, bool, bool *);
1839 static void cp_parser_label_for_labeled_statement
1840 (cp_parser *);
1841 static tree cp_parser_expression_statement
1842 (cp_parser *, tree);
1843 static tree cp_parser_compound_statement
1844 (cp_parser *, tree, bool);
1845 static void cp_parser_statement_seq_opt
1846 (cp_parser *, tree);
1847 static tree cp_parser_selection_statement
1848 (cp_parser *, bool *);
1849 static tree cp_parser_condition
1850 (cp_parser *);
1851 static tree cp_parser_iteration_statement
1852 (cp_parser *);
1853 static void cp_parser_for_init_statement
1854 (cp_parser *);
1855 static tree cp_parser_c_for
1856 (cp_parser *);
1857 static tree cp_parser_range_for
1858 (cp_parser *);
1859 static tree cp_parser_jump_statement
1860 (cp_parser *);
1861 static void cp_parser_declaration_statement
1862 (cp_parser *);
1863
1864 static tree cp_parser_implicitly_scoped_statement
1865 (cp_parser *, bool *);
1866 static void cp_parser_already_scoped_statement
1867 (cp_parser *);
1868
1869 /* Declarations [gram.dcl.dcl] */
1870
1871 static void cp_parser_declaration_seq_opt
1872 (cp_parser *);
1873 static void cp_parser_declaration
1874 (cp_parser *);
1875 static void cp_parser_block_declaration
1876 (cp_parser *, bool);
1877 static void cp_parser_simple_declaration
1878 (cp_parser *, bool);
1879 static void cp_parser_decl_specifier_seq
1880 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1881 static tree cp_parser_storage_class_specifier_opt
1882 (cp_parser *);
1883 static tree cp_parser_function_specifier_opt
1884 (cp_parser *, cp_decl_specifier_seq *);
1885 static tree cp_parser_type_specifier
1886 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1887 int *, bool *);
1888 static tree cp_parser_simple_type_specifier
1889 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1890 static tree cp_parser_type_name
1891 (cp_parser *);
1892 static tree cp_parser_nonclass_name
1893 (cp_parser* parser);
1894 static tree cp_parser_elaborated_type_specifier
1895 (cp_parser *, bool, bool);
1896 static tree cp_parser_enum_specifier
1897 (cp_parser *);
1898 static void cp_parser_enumerator_list
1899 (cp_parser *, tree);
1900 static void cp_parser_enumerator_definition
1901 (cp_parser *, tree);
1902 static tree cp_parser_namespace_name
1903 (cp_parser *);
1904 static void cp_parser_namespace_definition
1905 (cp_parser *);
1906 static void cp_parser_namespace_body
1907 (cp_parser *);
1908 static tree cp_parser_qualified_namespace_specifier
1909 (cp_parser *);
1910 static void cp_parser_namespace_alias_definition
1911 (cp_parser *);
1912 static bool cp_parser_using_declaration
1913 (cp_parser *, bool);
1914 static void cp_parser_using_directive
1915 (cp_parser *);
1916 static void cp_parser_asm_definition
1917 (cp_parser *);
1918 static void cp_parser_linkage_specification
1919 (cp_parser *);
1920 static void cp_parser_static_assert
1921 (cp_parser *, bool);
1922 static tree cp_parser_decltype
1923 (cp_parser *);
1924
1925 /* Declarators [gram.dcl.decl] */
1926
1927 static tree cp_parser_init_declarator
1928 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1929 static cp_declarator *cp_parser_declarator
1930 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1931 static cp_declarator *cp_parser_direct_declarator
1932 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1933 static enum tree_code cp_parser_ptr_operator
1934 (cp_parser *, tree *, cp_cv_quals *);
1935 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1936 (cp_parser *);
1937 static tree cp_parser_late_return_type_opt
1938 (cp_parser *);
1939 static tree cp_parser_declarator_id
1940 (cp_parser *, bool);
1941 static tree cp_parser_type_id
1942 (cp_parser *);
1943 static tree cp_parser_template_type_arg
1944 (cp_parser *);
1945 static tree cp_parser_trailing_type_id (cp_parser *);
1946 static tree cp_parser_type_id_1
1947 (cp_parser *, bool, bool);
1948 static void cp_parser_type_specifier_seq
1949 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1950 static tree cp_parser_parameter_declaration_clause
1951 (cp_parser *);
1952 static tree cp_parser_parameter_declaration_list
1953 (cp_parser *, bool *);
1954 static cp_parameter_declarator *cp_parser_parameter_declaration
1955 (cp_parser *, bool, bool *);
1956 static tree cp_parser_default_argument
1957 (cp_parser *, bool);
1958 static void cp_parser_function_body
1959 (cp_parser *);
1960 static tree cp_parser_initializer
1961 (cp_parser *, bool *, bool *);
1962 static tree cp_parser_initializer_clause
1963 (cp_parser *, bool *);
1964 static tree cp_parser_braced_list
1965 (cp_parser*, bool*);
1966 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1967 (cp_parser *, bool *);
1968
1969 static bool cp_parser_ctor_initializer_opt_and_function_body
1970 (cp_parser *);
1971
1972 /* Classes [gram.class] */
1973
1974 static tree cp_parser_class_name
1975 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1976 static tree cp_parser_class_specifier
1977 (cp_parser *);
1978 static tree cp_parser_class_head
1979 (cp_parser *, bool *, tree *, tree *);
1980 static enum tag_types cp_parser_class_key
1981 (cp_parser *);
1982 static void cp_parser_member_specification_opt
1983 (cp_parser *);
1984 static void cp_parser_member_declaration
1985 (cp_parser *);
1986 static tree cp_parser_pure_specifier
1987 (cp_parser *);
1988 static tree cp_parser_constant_initializer
1989 (cp_parser *);
1990
1991 /* Derived classes [gram.class.derived] */
1992
1993 static tree cp_parser_base_clause
1994 (cp_parser *);
1995 static tree cp_parser_base_specifier
1996 (cp_parser *);
1997
1998 /* Special member functions [gram.special] */
1999
2000 static tree cp_parser_conversion_function_id
2001 (cp_parser *);
2002 static tree cp_parser_conversion_type_id
2003 (cp_parser *);
2004 static cp_declarator *cp_parser_conversion_declarator_opt
2005 (cp_parser *);
2006 static bool cp_parser_ctor_initializer_opt
2007 (cp_parser *);
2008 static void cp_parser_mem_initializer_list
2009 (cp_parser *);
2010 static tree cp_parser_mem_initializer
2011 (cp_parser *);
2012 static tree cp_parser_mem_initializer_id
2013 (cp_parser *);
2014
2015 /* Overloading [gram.over] */
2016
2017 static tree cp_parser_operator_function_id
2018 (cp_parser *);
2019 static tree cp_parser_operator
2020 (cp_parser *);
2021
2022 /* Templates [gram.temp] */
2023
2024 static void cp_parser_template_declaration
2025 (cp_parser *, bool);
2026 static tree cp_parser_template_parameter_list
2027 (cp_parser *);
2028 static tree cp_parser_template_parameter
2029 (cp_parser *, bool *, bool *);
2030 static tree cp_parser_type_parameter
2031 (cp_parser *, bool *);
2032 static tree cp_parser_template_id
2033 (cp_parser *, bool, bool, bool);
2034 static tree cp_parser_template_name
2035 (cp_parser *, bool, bool, bool, bool *);
2036 static tree cp_parser_template_argument_list
2037 (cp_parser *);
2038 static tree cp_parser_template_argument
2039 (cp_parser *);
2040 static void cp_parser_explicit_instantiation
2041 (cp_parser *);
2042 static void cp_parser_explicit_specialization
2043 (cp_parser *);
2044
2045 /* Exception handling [gram.exception] */
2046
2047 static tree cp_parser_try_block
2048 (cp_parser *);
2049 static bool cp_parser_function_try_block
2050 (cp_parser *);
2051 static void cp_parser_handler_seq
2052 (cp_parser *);
2053 static void cp_parser_handler
2054 (cp_parser *);
2055 static tree cp_parser_exception_declaration
2056 (cp_parser *);
2057 static tree cp_parser_throw_expression
2058 (cp_parser *);
2059 static tree cp_parser_exception_specification_opt
2060 (cp_parser *);
2061 static tree cp_parser_type_id_list
2062 (cp_parser *);
2063
2064 /* GNU Extensions */
2065
2066 static tree cp_parser_asm_specification_opt
2067 (cp_parser *);
2068 static tree cp_parser_asm_operand_list
2069 (cp_parser *);
2070 static tree cp_parser_asm_clobber_list
2071 (cp_parser *);
2072 static tree cp_parser_asm_label_list
2073 (cp_parser *);
2074 static tree cp_parser_attributes_opt
2075 (cp_parser *);
2076 static tree cp_parser_attribute_list
2077 (cp_parser *);
2078 static bool cp_parser_extension_opt
2079 (cp_parser *, int *);
2080 static void cp_parser_label_declaration
2081 (cp_parser *);
2082
2083 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2084 static bool cp_parser_pragma
2085 (cp_parser *, enum pragma_context);
2086
2087 /* Objective-C++ Productions */
2088
2089 static tree cp_parser_objc_message_receiver
2090 (cp_parser *);
2091 static tree cp_parser_objc_message_args
2092 (cp_parser *);
2093 static tree cp_parser_objc_message_expression
2094 (cp_parser *);
2095 static tree cp_parser_objc_encode_expression
2096 (cp_parser *);
2097 static tree cp_parser_objc_defs_expression
2098 (cp_parser *);
2099 static tree cp_parser_objc_protocol_expression
2100 (cp_parser *);
2101 static tree cp_parser_objc_selector_expression
2102 (cp_parser *);
2103 static tree cp_parser_objc_expression
2104 (cp_parser *);
2105 static bool cp_parser_objc_selector_p
2106 (enum cpp_ttype);
2107 static tree cp_parser_objc_selector
2108 (cp_parser *);
2109 static tree cp_parser_objc_protocol_refs_opt
2110 (cp_parser *);
2111 static void cp_parser_objc_declaration
2112 (cp_parser *, tree);
2113 static tree cp_parser_objc_statement
2114 (cp_parser *);
2115 static bool cp_parser_objc_valid_prefix_attributes
2116 (cp_parser *, tree *);
2117 static void cp_parser_objc_at_property_declaration
2118 (cp_parser *) ;
2119 static void cp_parser_objc_at_synthesize_declaration
2120 (cp_parser *) ;
2121 static void cp_parser_objc_at_dynamic_declaration
2122 (cp_parser *) ;
2123 static tree cp_parser_objc_struct_declaration
2124 (cp_parser *) ;
2125
2126 /* Utility Routines */
2127
2128 static tree cp_parser_lookup_name
2129 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2130 static tree cp_parser_lookup_name_simple
2131 (cp_parser *, tree, location_t);
2132 static tree cp_parser_maybe_treat_template_as_class
2133 (tree, bool);
2134 static bool cp_parser_check_declarator_template_parameters
2135 (cp_parser *, cp_declarator *, location_t);
2136 static bool cp_parser_check_template_parameters
2137 (cp_parser *, unsigned, location_t, cp_declarator *);
2138 static tree cp_parser_simple_cast_expression
2139 (cp_parser *);
2140 static tree cp_parser_global_scope_opt
2141 (cp_parser *, bool);
2142 static bool cp_parser_constructor_declarator_p
2143 (cp_parser *, bool);
2144 static tree cp_parser_function_definition_from_specifiers_and_declarator
2145 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2146 static tree cp_parser_function_definition_after_declarator
2147 (cp_parser *, bool);
2148 static void cp_parser_template_declaration_after_export
2149 (cp_parser *, bool);
2150 static void cp_parser_perform_template_parameter_access_checks
2151 (VEC (deferred_access_check,gc)*);
2152 static tree cp_parser_single_declaration
2153 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2154 static tree cp_parser_functional_cast
2155 (cp_parser *, tree);
2156 static tree cp_parser_save_member_function_body
2157 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2158 static tree cp_parser_enclosed_template_argument_list
2159 (cp_parser *);
2160 static void cp_parser_save_default_args
2161 (cp_parser *, tree);
2162 static void cp_parser_late_parsing_for_member
2163 (cp_parser *, tree);
2164 static void cp_parser_late_parsing_default_args
2165 (cp_parser *, tree);
2166 static tree cp_parser_sizeof_operand
2167 (cp_parser *, enum rid);
2168 static tree cp_parser_trait_expr
2169 (cp_parser *, enum rid);
2170 static bool cp_parser_declares_only_class_p
2171 (cp_parser *);
2172 static void cp_parser_set_storage_class
2173 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2174 static void cp_parser_set_decl_spec_type
2175 (cp_decl_specifier_seq *, tree, location_t, bool);
2176 static bool cp_parser_friend_p
2177 (const cp_decl_specifier_seq *);
2178 static void cp_parser_required_error
2179 (cp_parser *, required_token, bool);
2180 static cp_token *cp_parser_require
2181 (cp_parser *, enum cpp_ttype, required_token);
2182 static cp_token *cp_parser_require_keyword
2183 (cp_parser *, enum rid, required_token);
2184 static bool cp_parser_token_starts_function_definition_p
2185 (cp_token *);
2186 static bool cp_parser_next_token_starts_class_definition_p
2187 (cp_parser *);
2188 static bool cp_parser_next_token_ends_template_argument_p
2189 (cp_parser *);
2190 static bool cp_parser_nth_token_starts_template_argument_list_p
2191 (cp_parser *, size_t);
2192 static enum tag_types cp_parser_token_is_class_key
2193 (cp_token *);
2194 static void cp_parser_check_class_key
2195 (enum tag_types, tree type);
2196 static void cp_parser_check_access_in_redeclaration
2197 (tree type, location_t location);
2198 static bool cp_parser_optional_template_keyword
2199 (cp_parser *);
2200 static void cp_parser_pre_parsed_nested_name_specifier
2201 (cp_parser *);
2202 static bool cp_parser_cache_group
2203 (cp_parser *, enum cpp_ttype, unsigned);
2204 static void cp_parser_parse_tentatively
2205 (cp_parser *);
2206 static void cp_parser_commit_to_tentative_parse
2207 (cp_parser *);
2208 static void cp_parser_abort_tentative_parse
2209 (cp_parser *);
2210 static bool cp_parser_parse_definitely
2211 (cp_parser *);
2212 static inline bool cp_parser_parsing_tentatively
2213 (cp_parser *);
2214 static bool cp_parser_uncommitted_to_tentative_parse_p
2215 (cp_parser *);
2216 static void cp_parser_error
2217 (cp_parser *, const char *);
2218 static void cp_parser_name_lookup_error
2219 (cp_parser *, tree, tree, name_lookup_error, location_t);
2220 static bool cp_parser_simulate_error
2221 (cp_parser *);
2222 static bool cp_parser_check_type_definition
2223 (cp_parser *);
2224 static void cp_parser_check_for_definition_in_return_type
2225 (cp_declarator *, tree, location_t type_location);
2226 static void cp_parser_check_for_invalid_template_id
2227 (cp_parser *, tree, location_t location);
2228 static bool cp_parser_non_integral_constant_expression
2229 (cp_parser *, non_integral_constant);
2230 static void cp_parser_diagnose_invalid_type_name
2231 (cp_parser *, tree, tree, location_t);
2232 static bool cp_parser_parse_and_diagnose_invalid_type_name
2233 (cp_parser *);
2234 static int cp_parser_skip_to_closing_parenthesis
2235 (cp_parser *, bool, bool, bool);
2236 static void cp_parser_skip_to_end_of_statement
2237 (cp_parser *);
2238 static void cp_parser_consume_semicolon_at_end_of_statement
2239 (cp_parser *);
2240 static void cp_parser_skip_to_end_of_block_or_statement
2241 (cp_parser *);
2242 static bool cp_parser_skip_to_closing_brace
2243 (cp_parser *);
2244 static void cp_parser_skip_to_end_of_template_parameter_list
2245 (cp_parser *);
2246 static void cp_parser_skip_to_pragma_eol
2247 (cp_parser*, cp_token *);
2248 static bool cp_parser_error_occurred
2249 (cp_parser *);
2250 static bool cp_parser_allow_gnu_extensions_p
2251 (cp_parser *);
2252 static bool cp_parser_is_string_literal
2253 (cp_token *);
2254 static bool cp_parser_is_keyword
2255 (cp_token *, enum rid);
2256 static tree cp_parser_make_typename_type
2257 (cp_parser *, tree, tree, location_t location);
2258 static cp_declarator * cp_parser_make_indirect_declarator
2259 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2260
2261 /* Returns nonzero if we are parsing tentatively. */
2262
2263 static inline bool
2264 cp_parser_parsing_tentatively (cp_parser* parser)
2265 {
2266 return parser->context->next != NULL;
2267 }
2268
2269 /* Returns nonzero if TOKEN is a string literal. */
2270
2271 static bool
2272 cp_parser_is_string_literal (cp_token* token)
2273 {
2274 return (token->type == CPP_STRING ||
2275 token->type == CPP_STRING16 ||
2276 token->type == CPP_STRING32 ||
2277 token->type == CPP_WSTRING ||
2278 token->type == CPP_UTF8STRING);
2279 }
2280
2281 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2282
2283 static bool
2284 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2285 {
2286 return token->keyword == keyword;
2287 }
2288
2289 /* If not parsing tentatively, issue a diagnostic of the form
2290 FILE:LINE: MESSAGE before TOKEN
2291 where TOKEN is the next token in the input stream. MESSAGE
2292 (specified by the caller) is usually of the form "expected
2293 OTHER-TOKEN". */
2294
2295 static void
2296 cp_parser_error (cp_parser* parser, const char* gmsgid)
2297 {
2298 if (!cp_parser_simulate_error (parser))
2299 {
2300 cp_token *token = cp_lexer_peek_token (parser->lexer);
2301 /* This diagnostic makes more sense if it is tagged to the line
2302 of the token we just peeked at. */
2303 cp_lexer_set_source_position_from_token (token);
2304
2305 if (token->type == CPP_PRAGMA)
2306 {
2307 error_at (token->location,
2308 "%<#pragma%> is not allowed here");
2309 cp_parser_skip_to_pragma_eol (parser, token);
2310 return;
2311 }
2312
2313 c_parse_error (gmsgid,
2314 /* Because c_parser_error does not understand
2315 CPP_KEYWORD, keywords are treated like
2316 identifiers. */
2317 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2318 token->u.value, token->flags);
2319 }
2320 }
2321
2322 /* Issue an error about name-lookup failing. NAME is the
2323 IDENTIFIER_NODE DECL is the result of
2324 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2325 the thing that we hoped to find. */
2326
2327 static void
2328 cp_parser_name_lookup_error (cp_parser* parser,
2329 tree name,
2330 tree decl,
2331 name_lookup_error desired,
2332 location_t location)
2333 {
2334 /* If name lookup completely failed, tell the user that NAME was not
2335 declared. */
2336 if (decl == error_mark_node)
2337 {
2338 if (parser->scope && parser->scope != global_namespace)
2339 error_at (location, "%<%E::%E%> has not been declared",
2340 parser->scope, name);
2341 else if (parser->scope == global_namespace)
2342 error_at (location, "%<::%E%> has not been declared", name);
2343 else if (parser->object_scope
2344 && !CLASS_TYPE_P (parser->object_scope))
2345 error_at (location, "request for member %qE in non-class type %qT",
2346 name, parser->object_scope);
2347 else if (parser->object_scope)
2348 error_at (location, "%<%T::%E%> has not been declared",
2349 parser->object_scope, name);
2350 else
2351 error_at (location, "%qE has not been declared", name);
2352 }
2353 else if (parser->scope && parser->scope != global_namespace)
2354 {
2355 switch (desired)
2356 {
2357 case NLE_TYPE:
2358 error_at (location, "%<%E::%E%> is not a type",
2359 parser->scope, name);
2360 break;
2361 case NLE_CXX98:
2362 error_at (location, "%<%E::%E%> is not a class or namespace",
2363 parser->scope, name);
2364 break;
2365 case NLE_NOT_CXX98:
2366 error_at (location,
2367 "%<%E::%E%> is not a class, namespace, or enumeration",
2368 parser->scope, name);
2369 break;
2370 default:
2371 gcc_unreachable ();
2372
2373 }
2374 }
2375 else if (parser->scope == global_namespace)
2376 {
2377 switch (desired)
2378 {
2379 case NLE_TYPE:
2380 error_at (location, "%<::%E%> is not a type", name);
2381 break;
2382 case NLE_CXX98:
2383 error_at (location, "%<::%E%> is not a class or namespace", name);
2384 break;
2385 case NLE_NOT_CXX98:
2386 error_at (location,
2387 "%<::%E%> is not a class, namespace, or enumeration",
2388 name);
2389 break;
2390 default:
2391 gcc_unreachable ();
2392 }
2393 }
2394 else
2395 {
2396 switch (desired)
2397 {
2398 case NLE_TYPE:
2399 error_at (location, "%qE is not a type", name);
2400 break;
2401 case NLE_CXX98:
2402 error_at (location, "%qE is not a class or namespace", name);
2403 break;
2404 case NLE_NOT_CXX98:
2405 error_at (location,
2406 "%qE is not a class, namespace, or enumeration", name);
2407 break;
2408 default:
2409 gcc_unreachable ();
2410 }
2411 }
2412 }
2413
2414 /* If we are parsing tentatively, remember that an error has occurred
2415 during this tentative parse. Returns true if the error was
2416 simulated; false if a message should be issued by the caller. */
2417
2418 static bool
2419 cp_parser_simulate_error (cp_parser* parser)
2420 {
2421 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2422 {
2423 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2424 return true;
2425 }
2426 return false;
2427 }
2428
2429 /* Check for repeated decl-specifiers. */
2430
2431 static void
2432 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2433 location_t location)
2434 {
2435 int ds;
2436
2437 for (ds = ds_first; ds != ds_last; ++ds)
2438 {
2439 unsigned count = decl_specs->specs[ds];
2440 if (count < 2)
2441 continue;
2442 /* The "long" specifier is a special case because of "long long". */
2443 if (ds == ds_long)
2444 {
2445 if (count > 2)
2446 error_at (location, "%<long long long%> is too long for GCC");
2447 else
2448 pedwarn_cxx98 (location, OPT_Wlong_long,
2449 "ISO C++ 1998 does not support %<long long%>");
2450 }
2451 else if (count > 1)
2452 {
2453 static const char *const decl_spec_names[] = {
2454 "signed",
2455 "unsigned",
2456 "short",
2457 "long",
2458 "const",
2459 "volatile",
2460 "restrict",
2461 "inline",
2462 "virtual",
2463 "explicit",
2464 "friend",
2465 "typedef",
2466 "constexpr",
2467 "__complex",
2468 "__thread"
2469 };
2470 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2471 }
2472 }
2473 }
2474
2475 /* This function is called when a type is defined. If type
2476 definitions are forbidden at this point, an error message is
2477 issued. */
2478
2479 static bool
2480 cp_parser_check_type_definition (cp_parser* parser)
2481 {
2482 /* If types are forbidden here, issue a message. */
2483 if (parser->type_definition_forbidden_message)
2484 {
2485 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2486 in the message need to be interpreted. */
2487 error (parser->type_definition_forbidden_message);
2488 return false;
2489 }
2490 return true;
2491 }
2492
2493 /* This function is called when the DECLARATOR is processed. The TYPE
2494 was a type defined in the decl-specifiers. If it is invalid to
2495 define a type in the decl-specifiers for DECLARATOR, an error is
2496 issued. TYPE_LOCATION is the location of TYPE and is used
2497 for error reporting. */
2498
2499 static void
2500 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2501 tree type, location_t type_location)
2502 {
2503 /* [dcl.fct] forbids type definitions in return types.
2504 Unfortunately, it's not easy to know whether or not we are
2505 processing a return type until after the fact. */
2506 while (declarator
2507 && (declarator->kind == cdk_pointer
2508 || declarator->kind == cdk_reference
2509 || declarator->kind == cdk_ptrmem))
2510 declarator = declarator->declarator;
2511 if (declarator
2512 && declarator->kind == cdk_function)
2513 {
2514 error_at (type_location,
2515 "new types may not be defined in a return type");
2516 inform (type_location,
2517 "(perhaps a semicolon is missing after the definition of %qT)",
2518 type);
2519 }
2520 }
2521
2522 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2523 "<" in any valid C++ program. If the next token is indeed "<",
2524 issue a message warning the user about what appears to be an
2525 invalid attempt to form a template-id. LOCATION is the location
2526 of the type-specifier (TYPE) */
2527
2528 static void
2529 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2530 tree type, location_t location)
2531 {
2532 cp_token_position start = 0;
2533
2534 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2535 {
2536 if (TYPE_P (type))
2537 error_at (location, "%qT is not a template", type);
2538 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2539 error_at (location, "%qE is not a template", type);
2540 else
2541 error_at (location, "invalid template-id");
2542 /* Remember the location of the invalid "<". */
2543 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2544 start = cp_lexer_token_position (parser->lexer, true);
2545 /* Consume the "<". */
2546 cp_lexer_consume_token (parser->lexer);
2547 /* Parse the template arguments. */
2548 cp_parser_enclosed_template_argument_list (parser);
2549 /* Permanently remove the invalid template arguments so that
2550 this error message is not issued again. */
2551 if (start)
2552 cp_lexer_purge_tokens_after (parser->lexer, start);
2553 }
2554 }
2555
2556 /* If parsing an integral constant-expression, issue an error message
2557 about the fact that THING appeared and return true. Otherwise,
2558 return false. In either case, set
2559 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2560
2561 static bool
2562 cp_parser_non_integral_constant_expression (cp_parser *parser,
2563 non_integral_constant thing)
2564 {
2565 parser->non_integral_constant_expression_p = true;
2566 if (parser->integral_constant_expression_p)
2567 {
2568 if (!parser->allow_non_integral_constant_expression_p)
2569 {
2570 const char *msg = NULL;
2571 switch (thing)
2572 {
2573 case NIC_FLOAT:
2574 error ("floating-point literal "
2575 "cannot appear in a constant-expression");
2576 return true;
2577 case NIC_CAST:
2578 error ("a cast to a type other than an integral or "
2579 "enumeration type cannot appear in a "
2580 "constant-expression");
2581 return true;
2582 case NIC_TYPEID:
2583 error ("%<typeid%> operator "
2584 "cannot appear in a constant-expression");
2585 return true;
2586 case NIC_NCC:
2587 error ("non-constant compound literals "
2588 "cannot appear in a constant-expression");
2589 return true;
2590 case NIC_FUNC_CALL:
2591 error ("a function call "
2592 "cannot appear in a constant-expression");
2593 return true;
2594 case NIC_INC:
2595 error ("an increment "
2596 "cannot appear in a constant-expression");
2597 return true;
2598 case NIC_DEC:
2599 error ("an decrement "
2600 "cannot appear in a constant-expression");
2601 return true;
2602 case NIC_ARRAY_REF:
2603 error ("an array reference "
2604 "cannot appear in a constant-expression");
2605 return true;
2606 case NIC_ADDR_LABEL:
2607 error ("the address of a label "
2608 "cannot appear in a constant-expression");
2609 return true;
2610 case NIC_OVERLOADED:
2611 error ("calls to overloaded operators "
2612 "cannot appear in a constant-expression");
2613 return true;
2614 case NIC_ASSIGNMENT:
2615 error ("an assignment cannot appear in a constant-expression");
2616 return true;
2617 case NIC_COMMA:
2618 error ("a comma operator "
2619 "cannot appear in a constant-expression");
2620 return true;
2621 case NIC_CONSTRUCTOR:
2622 error ("a call to a constructor "
2623 "cannot appear in a constant-expression");
2624 return true;
2625 case NIC_THIS:
2626 msg = "this";
2627 break;
2628 case NIC_FUNC_NAME:
2629 msg = "__FUNCTION__";
2630 break;
2631 case NIC_PRETTY_FUNC:
2632 msg = "__PRETTY_FUNCTION__";
2633 break;
2634 case NIC_C99_FUNC:
2635 msg = "__func__";
2636 break;
2637 case NIC_VA_ARG:
2638 msg = "va_arg";
2639 break;
2640 case NIC_ARROW:
2641 msg = "->";
2642 break;
2643 case NIC_POINT:
2644 msg = ".";
2645 break;
2646 case NIC_STAR:
2647 msg = "*";
2648 break;
2649 case NIC_ADDR:
2650 msg = "&";
2651 break;
2652 case NIC_PREINCREMENT:
2653 msg = "++";
2654 break;
2655 case NIC_PREDECREMENT:
2656 msg = "--";
2657 break;
2658 case NIC_NEW:
2659 msg = "new";
2660 break;
2661 case NIC_DEL:
2662 msg = "delete";
2663 break;
2664 default:
2665 gcc_unreachable ();
2666 }
2667 if (msg)
2668 error ("%qs cannot appear in a constant-expression", msg);
2669 return true;
2670 }
2671 }
2672 return false;
2673 }
2674
2675 /* Emit a diagnostic for an invalid type name. SCOPE is the
2676 qualifying scope (or NULL, if none) for ID. This function commits
2677 to the current active tentative parse, if any. (Otherwise, the
2678 problematic construct might be encountered again later, resulting
2679 in duplicate error messages.) LOCATION is the location of ID. */
2680
2681 static void
2682 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2683 tree scope, tree id,
2684 location_t location)
2685 {
2686 tree decl, old_scope;
2687 /* Try to lookup the identifier. */
2688 old_scope = parser->scope;
2689 parser->scope = scope;
2690 decl = cp_parser_lookup_name_simple (parser, id, location);
2691 parser->scope = old_scope;
2692 /* If the lookup found a template-name, it means that the user forgot
2693 to specify an argument list. Emit a useful error message. */
2694 if (TREE_CODE (decl) == TEMPLATE_DECL)
2695 error_at (location,
2696 "invalid use of template-name %qE without an argument list",
2697 decl);
2698 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2699 error_at (location, "invalid use of destructor %qD as a type", id);
2700 else if (TREE_CODE (decl) == TYPE_DECL)
2701 /* Something like 'unsigned A a;' */
2702 error_at (location, "invalid combination of multiple type-specifiers");
2703 else if (!parser->scope)
2704 {
2705 /* Issue an error message. */
2706 error_at (location, "%qE does not name a type", id);
2707 /* If we're in a template class, it's possible that the user was
2708 referring to a type from a base class. For example:
2709
2710 template <typename T> struct A { typedef T X; };
2711 template <typename T> struct B : public A<T> { X x; };
2712
2713 The user should have said "typename A<T>::X". */
2714 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2715 inform (location, "C++0x %<constexpr%> only available with "
2716 "-std=c++0x or -std=gnu++0x");
2717 else if (processing_template_decl && current_class_type
2718 && TYPE_BINFO (current_class_type))
2719 {
2720 tree b;
2721
2722 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2723 b;
2724 b = TREE_CHAIN (b))
2725 {
2726 tree base_type = BINFO_TYPE (b);
2727 if (CLASS_TYPE_P (base_type)
2728 && dependent_type_p (base_type))
2729 {
2730 tree field;
2731 /* Go from a particular instantiation of the
2732 template (which will have an empty TYPE_FIELDs),
2733 to the main version. */
2734 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2735 for (field = TYPE_FIELDS (base_type);
2736 field;
2737 field = DECL_CHAIN (field))
2738 if (TREE_CODE (field) == TYPE_DECL
2739 && DECL_NAME (field) == id)
2740 {
2741 inform (location,
2742 "(perhaps %<typename %T::%E%> was intended)",
2743 BINFO_TYPE (b), id);
2744 break;
2745 }
2746 if (field)
2747 break;
2748 }
2749 }
2750 }
2751 }
2752 /* Here we diagnose qualified-ids where the scope is actually correct,
2753 but the identifier does not resolve to a valid type name. */
2754 else if (parser->scope != error_mark_node)
2755 {
2756 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2757 error_at (location, "%qE in namespace %qE does not name a type",
2758 id, parser->scope);
2759 else if (CLASS_TYPE_P (parser->scope)
2760 && constructor_name_p (id, parser->scope))
2761 {
2762 /* A<T>::A<T>() */
2763 error_at (location, "%<%T::%E%> names the constructor, not"
2764 " the type", parser->scope, id);
2765 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2766 error_at (location, "and %qT has no template constructors",
2767 parser->scope);
2768 }
2769 else if (TYPE_P (parser->scope)
2770 && dependent_scope_p (parser->scope))
2771 error_at (location, "need %<typename%> before %<%T::%E%> because "
2772 "%qT is a dependent scope",
2773 parser->scope, id, parser->scope);
2774 else if (TYPE_P (parser->scope))
2775 error_at (location, "%qE in class %qT does not name a type",
2776 id, parser->scope);
2777 else
2778 gcc_unreachable ();
2779 }
2780 cp_parser_commit_to_tentative_parse (parser);
2781 }
2782
2783 /* Check for a common situation where a type-name should be present,
2784 but is not, and issue a sensible error message. Returns true if an
2785 invalid type-name was detected.
2786
2787 The situation handled by this function are variable declarations of the
2788 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2789 Usually, `ID' should name a type, but if we got here it means that it
2790 does not. We try to emit the best possible error message depending on
2791 how exactly the id-expression looks like. */
2792
2793 static bool
2794 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2795 {
2796 tree id;
2797 cp_token *token = cp_lexer_peek_token (parser->lexer);
2798
2799 /* Avoid duplicate error about ambiguous lookup. */
2800 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2801 {
2802 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2803 if (next->type == CPP_NAME && next->ambiguous_p)
2804 goto out;
2805 }
2806
2807 cp_parser_parse_tentatively (parser);
2808 id = cp_parser_id_expression (parser,
2809 /*template_keyword_p=*/false,
2810 /*check_dependency_p=*/true,
2811 /*template_p=*/NULL,
2812 /*declarator_p=*/true,
2813 /*optional_p=*/false);
2814 /* If the next token is a (, this is a function with no explicit return
2815 type, i.e. constructor, destructor or conversion op. */
2816 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2817 || TREE_CODE (id) == TYPE_DECL)
2818 {
2819 cp_parser_abort_tentative_parse (parser);
2820 return false;
2821 }
2822 if (!cp_parser_parse_definitely (parser))
2823 return false;
2824
2825 /* Emit a diagnostic for the invalid type. */
2826 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2827 id, token->location);
2828 out:
2829 /* If we aren't in the middle of a declarator (i.e. in a
2830 parameter-declaration-clause), skip to the end of the declaration;
2831 there's no point in trying to process it. */
2832 if (!parser->in_declarator_p)
2833 cp_parser_skip_to_end_of_block_or_statement (parser);
2834 return true;
2835 }
2836
2837 /* Consume tokens up to, and including, the next non-nested closing `)'.
2838 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2839 are doing error recovery. Returns -1 if OR_COMMA is true and we
2840 found an unnested comma. */
2841
2842 static int
2843 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2844 bool recovering,
2845 bool or_comma,
2846 bool consume_paren)
2847 {
2848 unsigned paren_depth = 0;
2849 unsigned brace_depth = 0;
2850 unsigned square_depth = 0;
2851
2852 if (recovering && !or_comma
2853 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2854 return 0;
2855
2856 while (true)
2857 {
2858 cp_token * token = cp_lexer_peek_token (parser->lexer);
2859
2860 switch (token->type)
2861 {
2862 case CPP_EOF:
2863 case CPP_PRAGMA_EOL:
2864 /* If we've run out of tokens, then there is no closing `)'. */
2865 return 0;
2866
2867 /* This is good for lambda expression capture-lists. */
2868 case CPP_OPEN_SQUARE:
2869 ++square_depth;
2870 break;
2871 case CPP_CLOSE_SQUARE:
2872 if (!square_depth--)
2873 return 0;
2874 break;
2875
2876 case CPP_SEMICOLON:
2877 /* This matches the processing in skip_to_end_of_statement. */
2878 if (!brace_depth)
2879 return 0;
2880 break;
2881
2882 case CPP_OPEN_BRACE:
2883 ++brace_depth;
2884 break;
2885 case CPP_CLOSE_BRACE:
2886 if (!brace_depth--)
2887 return 0;
2888 break;
2889
2890 case CPP_COMMA:
2891 if (recovering && or_comma && !brace_depth && !paren_depth
2892 && !square_depth)
2893 return -1;
2894 break;
2895
2896 case CPP_OPEN_PAREN:
2897 if (!brace_depth)
2898 ++paren_depth;
2899 break;
2900
2901 case CPP_CLOSE_PAREN:
2902 if (!brace_depth && !paren_depth--)
2903 {
2904 if (consume_paren)
2905 cp_lexer_consume_token (parser->lexer);
2906 return 1;
2907 }
2908 break;
2909
2910 default:
2911 break;
2912 }
2913
2914 /* Consume the token. */
2915 cp_lexer_consume_token (parser->lexer);
2916 }
2917 }
2918
2919 /* Consume tokens until we reach the end of the current statement.
2920 Normally, that will be just before consuming a `;'. However, if a
2921 non-nested `}' comes first, then we stop before consuming that. */
2922
2923 static void
2924 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2925 {
2926 unsigned nesting_depth = 0;
2927
2928 while (true)
2929 {
2930 cp_token *token = cp_lexer_peek_token (parser->lexer);
2931
2932 switch (token->type)
2933 {
2934 case CPP_EOF:
2935 case CPP_PRAGMA_EOL:
2936 /* If we've run out of tokens, stop. */
2937 return;
2938
2939 case CPP_SEMICOLON:
2940 /* If the next token is a `;', we have reached the end of the
2941 statement. */
2942 if (!nesting_depth)
2943 return;
2944 break;
2945
2946 case CPP_CLOSE_BRACE:
2947 /* If this is a non-nested '}', stop before consuming it.
2948 That way, when confronted with something like:
2949
2950 { 3 + }
2951
2952 we stop before consuming the closing '}', even though we
2953 have not yet reached a `;'. */
2954 if (nesting_depth == 0)
2955 return;
2956
2957 /* If it is the closing '}' for a block that we have
2958 scanned, stop -- but only after consuming the token.
2959 That way given:
2960
2961 void f g () { ... }
2962 typedef int I;
2963
2964 we will stop after the body of the erroneously declared
2965 function, but before consuming the following `typedef'
2966 declaration. */
2967 if (--nesting_depth == 0)
2968 {
2969 cp_lexer_consume_token (parser->lexer);
2970 return;
2971 }
2972
2973 case CPP_OPEN_BRACE:
2974 ++nesting_depth;
2975 break;
2976
2977 default:
2978 break;
2979 }
2980
2981 /* Consume the token. */
2982 cp_lexer_consume_token (parser->lexer);
2983 }
2984 }
2985
2986 /* This function is called at the end of a statement or declaration.
2987 If the next token is a semicolon, it is consumed; otherwise, error
2988 recovery is attempted. */
2989
2990 static void
2991 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2992 {
2993 /* Look for the trailing `;'. */
2994 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2995 {
2996 /* If there is additional (erroneous) input, skip to the end of
2997 the statement. */
2998 cp_parser_skip_to_end_of_statement (parser);
2999 /* If the next token is now a `;', consume it. */
3000 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3001 cp_lexer_consume_token (parser->lexer);
3002 }
3003 }
3004
3005 /* Skip tokens until we have consumed an entire block, or until we
3006 have consumed a non-nested `;'. */
3007
3008 static void
3009 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3010 {
3011 int nesting_depth = 0;
3012
3013 while (nesting_depth >= 0)
3014 {
3015 cp_token *token = cp_lexer_peek_token (parser->lexer);
3016
3017 switch (token->type)
3018 {
3019 case CPP_EOF:
3020 case CPP_PRAGMA_EOL:
3021 /* If we've run out of tokens, stop. */
3022 return;
3023
3024 case CPP_SEMICOLON:
3025 /* Stop if this is an unnested ';'. */
3026 if (!nesting_depth)
3027 nesting_depth = -1;
3028 break;
3029
3030 case CPP_CLOSE_BRACE:
3031 /* Stop if this is an unnested '}', or closes the outermost
3032 nesting level. */
3033 nesting_depth--;
3034 if (nesting_depth < 0)
3035 return;
3036 if (!nesting_depth)
3037 nesting_depth = -1;
3038 break;
3039
3040 case CPP_OPEN_BRACE:
3041 /* Nest. */
3042 nesting_depth++;
3043 break;
3044
3045 default:
3046 break;
3047 }
3048
3049 /* Consume the token. */
3050 cp_lexer_consume_token (parser->lexer);
3051 }
3052 }
3053
3054 /* Skip tokens until a non-nested closing curly brace is the next
3055 token, or there are no more tokens. Return true in the first case,
3056 false otherwise. */
3057
3058 static bool
3059 cp_parser_skip_to_closing_brace (cp_parser *parser)
3060 {
3061 unsigned nesting_depth = 0;
3062
3063 while (true)
3064 {
3065 cp_token *token = cp_lexer_peek_token (parser->lexer);
3066
3067 switch (token->type)
3068 {
3069 case CPP_EOF:
3070 case CPP_PRAGMA_EOL:
3071 /* If we've run out of tokens, stop. */
3072 return false;
3073
3074 case CPP_CLOSE_BRACE:
3075 /* If the next token is a non-nested `}', then we have reached
3076 the end of the current block. */
3077 if (nesting_depth-- == 0)
3078 return true;
3079 break;
3080
3081 case CPP_OPEN_BRACE:
3082 /* If it the next token is a `{', then we are entering a new
3083 block. Consume the entire block. */
3084 ++nesting_depth;
3085 break;
3086
3087 default:
3088 break;
3089 }
3090
3091 /* Consume the token. */
3092 cp_lexer_consume_token (parser->lexer);
3093 }
3094 }
3095
3096 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3097 parameter is the PRAGMA token, allowing us to purge the entire pragma
3098 sequence. */
3099
3100 static void
3101 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3102 {
3103 cp_token *token;
3104
3105 parser->lexer->in_pragma = false;
3106
3107 do
3108 token = cp_lexer_consume_token (parser->lexer);
3109 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3110
3111 /* Ensure that the pragma is not parsed again. */
3112 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3113 }
3114
3115 /* Require pragma end of line, resyncing with it as necessary. The
3116 arguments are as for cp_parser_skip_to_pragma_eol. */
3117
3118 static void
3119 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3120 {
3121 parser->lexer->in_pragma = false;
3122 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3123 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3124 }
3125
3126 /* This is a simple wrapper around make_typename_type. When the id is
3127 an unresolved identifier node, we can provide a superior diagnostic
3128 using cp_parser_diagnose_invalid_type_name. */
3129
3130 static tree
3131 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3132 tree id, location_t id_location)
3133 {
3134 tree result;
3135 if (TREE_CODE (id) == IDENTIFIER_NODE)
3136 {
3137 result = make_typename_type (scope, id, typename_type,
3138 /*complain=*/tf_none);
3139 if (result == error_mark_node)
3140 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3141 return result;
3142 }
3143 return make_typename_type (scope, id, typename_type, tf_error);
3144 }
3145
3146 /* This is a wrapper around the
3147 make_{pointer,ptrmem,reference}_declarator functions that decides
3148 which one to call based on the CODE and CLASS_TYPE arguments. The
3149 CODE argument should be one of the values returned by
3150 cp_parser_ptr_operator. */
3151 static cp_declarator *
3152 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3153 cp_cv_quals cv_qualifiers,
3154 cp_declarator *target)
3155 {
3156 if (code == ERROR_MARK)
3157 return cp_error_declarator;
3158
3159 if (code == INDIRECT_REF)
3160 if (class_type == NULL_TREE)
3161 return make_pointer_declarator (cv_qualifiers, target);
3162 else
3163 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3164 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3165 return make_reference_declarator (cv_qualifiers, target, false);
3166 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3167 return make_reference_declarator (cv_qualifiers, target, true);
3168 gcc_unreachable ();
3169 }
3170
3171 /* Create a new C++ parser. */
3172
3173 static cp_parser *
3174 cp_parser_new (void)
3175 {
3176 cp_parser *parser;
3177 cp_lexer *lexer;
3178 unsigned i;
3179
3180 /* cp_lexer_new_main is called before doing GC allocation because
3181 cp_lexer_new_main might load a PCH file. */
3182 lexer = cp_lexer_new_main ();
3183
3184 /* Initialize the binops_by_token so that we can get the tree
3185 directly from the token. */
3186 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3187 binops_by_token[binops[i].token_type] = binops[i];
3188
3189 parser = ggc_alloc_cleared_cp_parser ();
3190 parser->lexer = lexer;
3191 parser->context = cp_parser_context_new (NULL);
3192
3193 /* For now, we always accept GNU extensions. */
3194 parser->allow_gnu_extensions_p = 1;
3195
3196 /* The `>' token is a greater-than operator, not the end of a
3197 template-id. */
3198 parser->greater_than_is_operator_p = true;
3199
3200 parser->default_arg_ok_p = true;
3201
3202 /* We are not parsing a constant-expression. */
3203 parser->integral_constant_expression_p = false;
3204 parser->allow_non_integral_constant_expression_p = false;
3205 parser->non_integral_constant_expression_p = false;
3206
3207 /* Local variable names are not forbidden. */
3208 parser->local_variables_forbidden_p = false;
3209
3210 /* We are not processing an `extern "C"' declaration. */
3211 parser->in_unbraced_linkage_specification_p = false;
3212
3213 /* We are not processing a declarator. */
3214 parser->in_declarator_p = false;
3215
3216 /* We are not processing a template-argument-list. */
3217 parser->in_template_argument_list_p = false;
3218
3219 /* We are not in an iteration statement. */
3220 parser->in_statement = 0;
3221
3222 /* We are not in a switch statement. */
3223 parser->in_switch_statement_p = false;
3224
3225 /* We are not parsing a type-id inside an expression. */
3226 parser->in_type_id_in_expr_p = false;
3227
3228 /* Declarations aren't implicitly extern "C". */
3229 parser->implicit_extern_c = false;
3230
3231 /* String literals should be translated to the execution character set. */
3232 parser->translate_strings_p = true;
3233
3234 /* We are not parsing a function body. */
3235 parser->in_function_body = false;
3236
3237 /* The unparsed function queue is empty. */
3238 push_unparsed_function_queues (parser);
3239
3240 /* There are no classes being defined. */
3241 parser->num_classes_being_defined = 0;
3242
3243 /* No template parameters apply. */
3244 parser->num_template_parameter_lists = 0;
3245
3246 return parser;
3247 }
3248
3249 /* Create a cp_lexer structure which will emit the tokens in CACHE
3250 and push it onto the parser's lexer stack. This is used for delayed
3251 parsing of in-class method bodies and default arguments, and should
3252 not be confused with tentative parsing. */
3253 static void
3254 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3255 {
3256 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3257 lexer->next = parser->lexer;
3258 parser->lexer = lexer;
3259
3260 /* Move the current source position to that of the first token in the
3261 new lexer. */
3262 cp_lexer_set_source_position_from_token (lexer->next_token);
3263 }
3264
3265 /* Pop the top lexer off the parser stack. This is never used for the
3266 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3267 static void
3268 cp_parser_pop_lexer (cp_parser *parser)
3269 {
3270 cp_lexer *lexer = parser->lexer;
3271 parser->lexer = lexer->next;
3272 cp_lexer_destroy (lexer);
3273
3274 /* Put the current source position back where it was before this
3275 lexer was pushed. */
3276 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3277 }
3278
3279 /* Lexical conventions [gram.lex] */
3280
3281 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3282 identifier. */
3283
3284 static tree
3285 cp_parser_identifier (cp_parser* parser)
3286 {
3287 cp_token *token;
3288
3289 /* Look for the identifier. */
3290 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3291 /* Return the value. */
3292 return token ? token->u.value : error_mark_node;
3293 }
3294
3295 /* Parse a sequence of adjacent string constants. Returns a
3296 TREE_STRING representing the combined, nul-terminated string
3297 constant. If TRANSLATE is true, translate the string to the
3298 execution character set. If WIDE_OK is true, a wide string is
3299 invalid here.
3300
3301 C++98 [lex.string] says that if a narrow string literal token is
3302 adjacent to a wide string literal token, the behavior is undefined.
3303 However, C99 6.4.5p4 says that this results in a wide string literal.
3304 We follow C99 here, for consistency with the C front end.
3305
3306 This code is largely lifted from lex_string() in c-lex.c.
3307
3308 FUTURE: ObjC++ will need to handle @-strings here. */
3309 static tree
3310 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3311 {
3312 tree value;
3313 size_t count;
3314 struct obstack str_ob;
3315 cpp_string str, istr, *strs;
3316 cp_token *tok;
3317 enum cpp_ttype type;
3318
3319 tok = cp_lexer_peek_token (parser->lexer);
3320 if (!cp_parser_is_string_literal (tok))
3321 {
3322 cp_parser_error (parser, "expected string-literal");
3323 return error_mark_node;
3324 }
3325
3326 type = tok->type;
3327
3328 /* Try to avoid the overhead of creating and destroying an obstack
3329 for the common case of just one string. */
3330 if (!cp_parser_is_string_literal
3331 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3332 {
3333 cp_lexer_consume_token (parser->lexer);
3334
3335 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3336 str.len = TREE_STRING_LENGTH (tok->u.value);
3337 count = 1;
3338
3339 strs = &str;
3340 }
3341 else
3342 {
3343 gcc_obstack_init (&str_ob);
3344 count = 0;
3345
3346 do
3347 {
3348 cp_lexer_consume_token (parser->lexer);
3349 count++;
3350 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3351 str.len = TREE_STRING_LENGTH (tok->u.value);
3352
3353 if (type != tok->type)
3354 {
3355 if (type == CPP_STRING)
3356 type = tok->type;
3357 else if (tok->type != CPP_STRING)
3358 error_at (tok->location,
3359 "unsupported non-standard concatenation "
3360 "of string literals");
3361 }
3362
3363 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3364
3365 tok = cp_lexer_peek_token (parser->lexer);
3366 }
3367 while (cp_parser_is_string_literal (tok));
3368
3369 strs = (cpp_string *) obstack_finish (&str_ob);
3370 }
3371
3372 if (type != CPP_STRING && !wide_ok)
3373 {
3374 cp_parser_error (parser, "a wide string is invalid in this context");
3375 type = CPP_STRING;
3376 }
3377
3378 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3379 (parse_in, strs, count, &istr, type))
3380 {
3381 value = build_string (istr.len, (const char *)istr.text);
3382 free (CONST_CAST (unsigned char *, istr.text));
3383
3384 switch (type)
3385 {
3386 default:
3387 case CPP_STRING:
3388 case CPP_UTF8STRING:
3389 TREE_TYPE (value) = char_array_type_node;
3390 break;
3391 case CPP_STRING16:
3392 TREE_TYPE (value) = char16_array_type_node;
3393 break;
3394 case CPP_STRING32:
3395 TREE_TYPE (value) = char32_array_type_node;
3396 break;
3397 case CPP_WSTRING:
3398 TREE_TYPE (value) = wchar_array_type_node;
3399 break;
3400 }
3401
3402 value = fix_string_type (value);
3403 }
3404 else
3405 /* cpp_interpret_string has issued an error. */
3406 value = error_mark_node;
3407
3408 if (count > 1)
3409 obstack_free (&str_ob, 0);
3410
3411 return value;
3412 }
3413
3414
3415 /* Basic concepts [gram.basic] */
3416
3417 /* Parse a translation-unit.
3418
3419 translation-unit:
3420 declaration-seq [opt]
3421
3422 Returns TRUE if all went well. */
3423
3424 static bool
3425 cp_parser_translation_unit (cp_parser* parser)
3426 {
3427 /* The address of the first non-permanent object on the declarator
3428 obstack. */
3429 static void *declarator_obstack_base;
3430
3431 bool success;
3432
3433 /* Create the declarator obstack, if necessary. */
3434 if (!cp_error_declarator)
3435 {
3436 gcc_obstack_init (&declarator_obstack);
3437 /* Create the error declarator. */
3438 cp_error_declarator = make_declarator (cdk_error);
3439 /* Create the empty parameter list. */
3440 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3441 /* Remember where the base of the declarator obstack lies. */
3442 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3443 }
3444
3445 cp_parser_declaration_seq_opt (parser);
3446
3447 /* If there are no tokens left then all went well. */
3448 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3449 {
3450 /* Get rid of the token array; we don't need it any more. */
3451 cp_lexer_destroy (parser->lexer);
3452 parser->lexer = NULL;
3453
3454 /* This file might have been a context that's implicitly extern
3455 "C". If so, pop the lang context. (Only relevant for PCH.) */
3456 if (parser->implicit_extern_c)
3457 {
3458 pop_lang_context ();
3459 parser->implicit_extern_c = false;
3460 }
3461
3462 /* Finish up. */
3463 finish_translation_unit ();
3464
3465 success = true;
3466 }
3467 else
3468 {
3469 cp_parser_error (parser, "expected declaration");
3470 success = false;
3471 }
3472
3473 /* Make sure the declarator obstack was fully cleaned up. */
3474 gcc_assert (obstack_next_free (&declarator_obstack)
3475 == declarator_obstack_base);
3476
3477 /* All went well. */
3478 return success;
3479 }
3480
3481 /* Expressions [gram.expr] */
3482
3483 /* Parse a primary-expression.
3484
3485 primary-expression:
3486 literal
3487 this
3488 ( expression )
3489 id-expression
3490
3491 GNU Extensions:
3492
3493 primary-expression:
3494 ( compound-statement )
3495 __builtin_va_arg ( assignment-expression , type-id )
3496 __builtin_offsetof ( type-id , offsetof-expression )
3497
3498 C++ Extensions:
3499 __has_nothrow_assign ( type-id )
3500 __has_nothrow_constructor ( type-id )
3501 __has_nothrow_copy ( type-id )
3502 __has_trivial_assign ( type-id )
3503 __has_trivial_constructor ( type-id )
3504 __has_trivial_copy ( type-id )
3505 __has_trivial_destructor ( type-id )
3506 __has_virtual_destructor ( type-id )
3507 __is_abstract ( type-id )
3508 __is_base_of ( type-id , type-id )
3509 __is_class ( type-id )
3510 __is_convertible_to ( type-id , type-id )
3511 __is_empty ( type-id )
3512 __is_enum ( type-id )
3513 __is_pod ( type-id )
3514 __is_polymorphic ( type-id )
3515 __is_union ( type-id )
3516
3517 Objective-C++ Extension:
3518
3519 primary-expression:
3520 objc-expression
3521
3522 literal:
3523 __null
3524
3525 ADDRESS_P is true iff this expression was immediately preceded by
3526 "&" and therefore might denote a pointer-to-member. CAST_P is true
3527 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3528 true iff this expression is a template argument.
3529
3530 Returns a representation of the expression. Upon return, *IDK
3531 indicates what kind of id-expression (if any) was present. */
3532
3533 static tree
3534 cp_parser_primary_expression (cp_parser *parser,
3535 bool address_p,
3536 bool cast_p,
3537 bool template_arg_p,
3538 cp_id_kind *idk)
3539 {
3540 cp_token *token = NULL;
3541
3542 /* Assume the primary expression is not an id-expression. */
3543 *idk = CP_ID_KIND_NONE;
3544
3545 /* Peek at the next token. */
3546 token = cp_lexer_peek_token (parser->lexer);
3547 switch (token->type)
3548 {
3549 /* literal:
3550 integer-literal
3551 character-literal
3552 floating-literal
3553 string-literal
3554 boolean-literal */
3555 case CPP_CHAR:
3556 case CPP_CHAR16:
3557 case CPP_CHAR32:
3558 case CPP_WCHAR:
3559 case CPP_NUMBER:
3560 token = cp_lexer_consume_token (parser->lexer);
3561 if (TREE_CODE (token->u.value) == FIXED_CST)
3562 {
3563 error_at (token->location,
3564 "fixed-point types not supported in C++");
3565 return error_mark_node;
3566 }
3567 /* Floating-point literals are only allowed in an integral
3568 constant expression if they are cast to an integral or
3569 enumeration type. */
3570 if (TREE_CODE (token->u.value) == REAL_CST
3571 && parser->integral_constant_expression_p
3572 && pedantic)
3573 {
3574 /* CAST_P will be set even in invalid code like "int(2.7 +
3575 ...)". Therefore, we have to check that the next token
3576 is sure to end the cast. */
3577 if (cast_p)
3578 {
3579 cp_token *next_token;
3580
3581 next_token = cp_lexer_peek_token (parser->lexer);
3582 if (/* The comma at the end of an
3583 enumerator-definition. */
3584 next_token->type != CPP_COMMA
3585 /* The curly brace at the end of an enum-specifier. */
3586 && next_token->type != CPP_CLOSE_BRACE
3587 /* The end of a statement. */
3588 && next_token->type != CPP_SEMICOLON
3589 /* The end of the cast-expression. */
3590 && next_token->type != CPP_CLOSE_PAREN
3591 /* The end of an array bound. */
3592 && next_token->type != CPP_CLOSE_SQUARE
3593 /* The closing ">" in a template-argument-list. */
3594 && (next_token->type != CPP_GREATER
3595 || parser->greater_than_is_operator_p)
3596 /* C++0x only: A ">>" treated like two ">" tokens,
3597 in a template-argument-list. */
3598 && (next_token->type != CPP_RSHIFT
3599 || (cxx_dialect == cxx98)
3600 || parser->greater_than_is_operator_p))
3601 cast_p = false;
3602 }
3603
3604 /* If we are within a cast, then the constraint that the
3605 cast is to an integral or enumeration type will be
3606 checked at that point. If we are not within a cast, then
3607 this code is invalid. */
3608 if (!cast_p)
3609 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3610 }
3611 return token->u.value;
3612
3613 case CPP_STRING:
3614 case CPP_STRING16:
3615 case CPP_STRING32:
3616 case CPP_WSTRING:
3617 case CPP_UTF8STRING:
3618 /* ??? Should wide strings be allowed when parser->translate_strings_p
3619 is false (i.e. in attributes)? If not, we can kill the third
3620 argument to cp_parser_string_literal. */
3621 return cp_parser_string_literal (parser,
3622 parser->translate_strings_p,
3623 true);
3624
3625 case CPP_OPEN_PAREN:
3626 {
3627 tree expr;
3628 bool saved_greater_than_is_operator_p;
3629
3630 /* Consume the `('. */
3631 cp_lexer_consume_token (parser->lexer);
3632 /* Within a parenthesized expression, a `>' token is always
3633 the greater-than operator. */
3634 saved_greater_than_is_operator_p
3635 = parser->greater_than_is_operator_p;
3636 parser->greater_than_is_operator_p = true;
3637 /* If we see `( { ' then we are looking at the beginning of
3638 a GNU statement-expression. */
3639 if (cp_parser_allow_gnu_extensions_p (parser)
3640 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3641 {
3642 /* Statement-expressions are not allowed by the standard. */
3643 pedwarn (token->location, OPT_pedantic,
3644 "ISO C++ forbids braced-groups within expressions");
3645
3646 /* And they're not allowed outside of a function-body; you
3647 cannot, for example, write:
3648
3649 int i = ({ int j = 3; j + 1; });
3650
3651 at class or namespace scope. */
3652 if (!parser->in_function_body
3653 || parser->in_template_argument_list_p)
3654 {
3655 error_at (token->location,
3656 "statement-expressions are not allowed outside "
3657 "functions nor in template-argument lists");
3658 cp_parser_skip_to_end_of_block_or_statement (parser);
3659 expr = error_mark_node;
3660 }
3661 else
3662 {
3663 /* Start the statement-expression. */
3664 expr = begin_stmt_expr ();
3665 /* Parse the compound-statement. */
3666 cp_parser_compound_statement (parser, expr, false);
3667 /* Finish up. */
3668 expr = finish_stmt_expr (expr, false);
3669 }
3670 }
3671 else
3672 {
3673 /* Parse the parenthesized expression. */
3674 expr = cp_parser_expression (parser, cast_p, idk);
3675 /* Let the front end know that this expression was
3676 enclosed in parentheses. This matters in case, for
3677 example, the expression is of the form `A::B', since
3678 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3679 not. */
3680 finish_parenthesized_expr (expr);
3681 }
3682 /* The `>' token might be the end of a template-id or
3683 template-parameter-list now. */
3684 parser->greater_than_is_operator_p
3685 = saved_greater_than_is_operator_p;
3686 /* Consume the `)'. */
3687 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3688 cp_parser_skip_to_end_of_statement (parser);
3689
3690 return expr;
3691 }
3692
3693 case CPP_OPEN_SQUARE:
3694 if (c_dialect_objc ())
3695 /* We have an Objective-C++ message. */
3696 return cp_parser_objc_expression (parser);
3697 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3698 return cp_parser_lambda_expression (parser);
3699
3700 case CPP_OBJC_STRING:
3701 if (c_dialect_objc ())
3702 /* We have an Objective-C++ string literal. */
3703 return cp_parser_objc_expression (parser);
3704 cp_parser_error (parser, "expected primary-expression");
3705 return error_mark_node;
3706
3707 case CPP_KEYWORD:
3708 switch (token->keyword)
3709 {
3710 /* These two are the boolean literals. */
3711 case RID_TRUE:
3712 cp_lexer_consume_token (parser->lexer);
3713 return boolean_true_node;
3714 case RID_FALSE:
3715 cp_lexer_consume_token (parser->lexer);
3716 return boolean_false_node;
3717
3718 /* The `__null' literal. */
3719 case RID_NULL:
3720 cp_lexer_consume_token (parser->lexer);
3721 return null_node;
3722
3723 /* The `nullptr' literal. */
3724 case RID_NULLPTR:
3725 cp_lexer_consume_token (parser->lexer);
3726 return nullptr_node;
3727
3728 /* Recognize the `this' keyword. */
3729 case RID_THIS:
3730 cp_lexer_consume_token (parser->lexer);
3731 if (parser->local_variables_forbidden_p)
3732 {
3733 error_at (token->location,
3734 "%<this%> may not be used in this context");
3735 return error_mark_node;
3736 }
3737 /* Pointers cannot appear in constant-expressions. */
3738 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3739 return error_mark_node;
3740 return finish_this_expr ();
3741
3742 /* The `operator' keyword can be the beginning of an
3743 id-expression. */
3744 case RID_OPERATOR:
3745 goto id_expression;
3746
3747 case RID_FUNCTION_NAME:
3748 case RID_PRETTY_FUNCTION_NAME:
3749 case RID_C99_FUNCTION_NAME:
3750 {
3751 non_integral_constant name;
3752
3753 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3754 __func__ are the names of variables -- but they are
3755 treated specially. Therefore, they are handled here,
3756 rather than relying on the generic id-expression logic
3757 below. Grammatically, these names are id-expressions.
3758
3759 Consume the token. */
3760 token = cp_lexer_consume_token (parser->lexer);
3761
3762 switch (token->keyword)
3763 {
3764 case RID_FUNCTION_NAME:
3765 name = NIC_FUNC_NAME;
3766 break;
3767 case RID_PRETTY_FUNCTION_NAME:
3768 name = NIC_PRETTY_FUNC;
3769 break;
3770 case RID_C99_FUNCTION_NAME:
3771 name = NIC_C99_FUNC;
3772 break;
3773 default:
3774 gcc_unreachable ();
3775 }
3776
3777 if (cp_parser_non_integral_constant_expression (parser, name))
3778 return error_mark_node;
3779
3780 /* Look up the name. */
3781 return finish_fname (token->u.value);
3782 }
3783
3784 case RID_VA_ARG:
3785 {
3786 tree expression;
3787 tree type;
3788
3789 /* The `__builtin_va_arg' construct is used to handle
3790 `va_arg'. Consume the `__builtin_va_arg' token. */
3791 cp_lexer_consume_token (parser->lexer);
3792 /* Look for the opening `('. */
3793 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3794 /* Now, parse the assignment-expression. */
3795 expression = cp_parser_assignment_expression (parser,
3796 /*cast_p=*/false, NULL);
3797 /* Look for the `,'. */
3798 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3799 /* Parse the type-id. */
3800 type = cp_parser_type_id (parser);
3801 /* Look for the closing `)'. */
3802 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3803 /* Using `va_arg' in a constant-expression is not
3804 allowed. */
3805 if (cp_parser_non_integral_constant_expression (parser,
3806 NIC_VA_ARG))
3807 return error_mark_node;
3808 return build_x_va_arg (expression, type);
3809 }
3810
3811 case RID_OFFSETOF:
3812 return cp_parser_builtin_offsetof (parser);
3813
3814 case RID_HAS_NOTHROW_ASSIGN:
3815 case RID_HAS_NOTHROW_CONSTRUCTOR:
3816 case RID_HAS_NOTHROW_COPY:
3817 case RID_HAS_TRIVIAL_ASSIGN:
3818 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3819 case RID_HAS_TRIVIAL_COPY:
3820 case RID_HAS_TRIVIAL_DESTRUCTOR:
3821 case RID_HAS_VIRTUAL_DESTRUCTOR:
3822 case RID_IS_ABSTRACT:
3823 case RID_IS_BASE_OF:
3824 case RID_IS_CLASS:
3825 case RID_IS_CONVERTIBLE_TO:
3826 case RID_IS_EMPTY:
3827 case RID_IS_ENUM:
3828 case RID_IS_POD:
3829 case RID_IS_POLYMORPHIC:
3830 case RID_IS_STD_LAYOUT:
3831 case RID_IS_TRIVIAL:
3832 case RID_IS_UNION:
3833 case RID_IS_LITERAL_TYPE:
3834 return cp_parser_trait_expr (parser, token->keyword);
3835
3836 /* Objective-C++ expressions. */
3837 case RID_AT_ENCODE:
3838 case RID_AT_PROTOCOL:
3839 case RID_AT_SELECTOR:
3840 return cp_parser_objc_expression (parser);
3841
3842 case RID_TEMPLATE:
3843 if (parser->in_function_body
3844 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3845 == CPP_LESS))
3846 {
3847 error_at (token->location,
3848 "a template declaration cannot appear at block scope");
3849 cp_parser_skip_to_end_of_block_or_statement (parser);
3850 return error_mark_node;
3851 }
3852 default:
3853 cp_parser_error (parser, "expected primary-expression");
3854 return error_mark_node;
3855 }
3856
3857 /* An id-expression can start with either an identifier, a
3858 `::' as the beginning of a qualified-id, or the "operator"
3859 keyword. */
3860 case CPP_NAME:
3861 case CPP_SCOPE:
3862 case CPP_TEMPLATE_ID:
3863 case CPP_NESTED_NAME_SPECIFIER:
3864 {
3865 tree id_expression;
3866 tree decl;
3867 const char *error_msg;
3868 bool template_p;
3869 bool done;
3870 cp_token *id_expr_token;
3871
3872 id_expression:
3873 /* Parse the id-expression. */
3874 id_expression
3875 = cp_parser_id_expression (parser,
3876 /*template_keyword_p=*/false,
3877 /*check_dependency_p=*/true,
3878 &template_p,
3879 /*declarator_p=*/false,
3880 /*optional_p=*/false);
3881 if (id_expression == error_mark_node)
3882 return error_mark_node;
3883 id_expr_token = token;
3884 token = cp_lexer_peek_token (parser->lexer);
3885 done = (token->type != CPP_OPEN_SQUARE
3886 && token->type != CPP_OPEN_PAREN
3887 && token->type != CPP_DOT
3888 && token->type != CPP_DEREF
3889 && token->type != CPP_PLUS_PLUS
3890 && token->type != CPP_MINUS_MINUS);
3891 /* If we have a template-id, then no further lookup is
3892 required. If the template-id was for a template-class, we
3893 will sometimes have a TYPE_DECL at this point. */
3894 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3895 || TREE_CODE (id_expression) == TYPE_DECL)
3896 decl = id_expression;
3897 /* Look up the name. */
3898 else
3899 {
3900 tree ambiguous_decls;
3901
3902 /* If we already know that this lookup is ambiguous, then
3903 we've already issued an error message; there's no reason
3904 to check again. */
3905 if (id_expr_token->type == CPP_NAME
3906 && id_expr_token->ambiguous_p)
3907 {
3908 cp_parser_simulate_error (parser);
3909 return error_mark_node;
3910 }
3911
3912 decl = cp_parser_lookup_name (parser, id_expression,
3913 none_type,
3914 template_p,
3915 /*is_namespace=*/false,
3916 /*check_dependency=*/true,
3917 &ambiguous_decls,
3918 id_expr_token->location);
3919 /* If the lookup was ambiguous, an error will already have
3920 been issued. */
3921 if (ambiguous_decls)
3922 return error_mark_node;
3923
3924 /* In Objective-C++, we may have an Objective-C 2.0
3925 dot-syntax for classes here. */
3926 if (c_dialect_objc ()
3927 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3928 && TREE_CODE (decl) == TYPE_DECL
3929 && objc_is_class_name (decl))
3930 {
3931 tree component;
3932 cp_lexer_consume_token (parser->lexer);
3933 component = cp_parser_identifier (parser);
3934 if (component == error_mark_node)
3935 return error_mark_node;
3936
3937 return objc_build_class_component_ref (id_expression, component);
3938 }
3939
3940 /* In Objective-C++, an instance variable (ivar) may be preferred
3941 to whatever cp_parser_lookup_name() found. */
3942 decl = objc_lookup_ivar (decl, id_expression);
3943
3944 /* If name lookup gives us a SCOPE_REF, then the
3945 qualifying scope was dependent. */
3946 if (TREE_CODE (decl) == SCOPE_REF)
3947 {
3948 /* At this point, we do not know if DECL is a valid
3949 integral constant expression. We assume that it is
3950 in fact such an expression, so that code like:
3951
3952 template <int N> struct A {
3953 int a[B<N>::i];
3954 };
3955
3956 is accepted. At template-instantiation time, we
3957 will check that B<N>::i is actually a constant. */
3958 return decl;
3959 }
3960 /* Check to see if DECL is a local variable in a context
3961 where that is forbidden. */
3962 if (parser->local_variables_forbidden_p
3963 && local_variable_p (decl))
3964 {
3965 /* It might be that we only found DECL because we are
3966 trying to be generous with pre-ISO scoping rules.
3967 For example, consider:
3968
3969 int i;
3970 void g() {
3971 for (int i = 0; i < 10; ++i) {}
3972 extern void f(int j = i);
3973 }
3974
3975 Here, name look up will originally find the out
3976 of scope `i'. We need to issue a warning message,
3977 but then use the global `i'. */
3978 decl = check_for_out_of_scope_variable (decl);
3979 if (local_variable_p (decl))
3980 {
3981 error_at (id_expr_token->location,
3982 "local variable %qD may not appear in this context",
3983 decl);
3984 return error_mark_node;
3985 }
3986 }
3987 }
3988
3989 decl = (finish_id_expression
3990 (id_expression, decl, parser->scope,
3991 idk,
3992 parser->integral_constant_expression_p,
3993 parser->allow_non_integral_constant_expression_p,
3994 &parser->non_integral_constant_expression_p,
3995 template_p, done, address_p,
3996 template_arg_p,
3997 &error_msg,
3998 id_expr_token->location));
3999 if (error_msg)
4000 cp_parser_error (parser, error_msg);
4001 return decl;
4002 }
4003
4004 /* Anything else is an error. */
4005 default:
4006 cp_parser_error (parser, "expected primary-expression");
4007 return error_mark_node;
4008 }
4009 }
4010
4011 /* Parse an id-expression.
4012
4013 id-expression:
4014 unqualified-id
4015 qualified-id
4016
4017 qualified-id:
4018 :: [opt] nested-name-specifier template [opt] unqualified-id
4019 :: identifier
4020 :: operator-function-id
4021 :: template-id
4022
4023 Return a representation of the unqualified portion of the
4024 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4025 a `::' or nested-name-specifier.
4026
4027 Often, if the id-expression was a qualified-id, the caller will
4028 want to make a SCOPE_REF to represent the qualified-id. This
4029 function does not do this in order to avoid wastefully creating
4030 SCOPE_REFs when they are not required.
4031
4032 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4033 `template' keyword.
4034
4035 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4036 uninstantiated templates.
4037
4038 If *TEMPLATE_P is non-NULL, it is set to true iff the
4039 `template' keyword is used to explicitly indicate that the entity
4040 named is a template.
4041
4042 If DECLARATOR_P is true, the id-expression is appearing as part of
4043 a declarator, rather than as part of an expression. */
4044
4045 static tree
4046 cp_parser_id_expression (cp_parser *parser,
4047 bool template_keyword_p,
4048 bool check_dependency_p,
4049 bool *template_p,
4050 bool declarator_p,
4051 bool optional_p)
4052 {
4053 bool global_scope_p;
4054 bool nested_name_specifier_p;
4055
4056 /* Assume the `template' keyword was not used. */
4057 if (template_p)
4058 *template_p = template_keyword_p;
4059
4060 /* Look for the optional `::' operator. */
4061 global_scope_p
4062 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4063 != NULL_TREE);
4064 /* Look for the optional nested-name-specifier. */
4065 nested_name_specifier_p
4066 = (cp_parser_nested_name_specifier_opt (parser,
4067 /*typename_keyword_p=*/false,
4068 check_dependency_p,
4069 /*type_p=*/false,
4070 declarator_p)
4071 != NULL_TREE);
4072 /* If there is a nested-name-specifier, then we are looking at
4073 the first qualified-id production. */
4074 if (nested_name_specifier_p)
4075 {
4076 tree saved_scope;
4077 tree saved_object_scope;
4078 tree saved_qualifying_scope;
4079 tree unqualified_id;
4080 bool is_template;
4081
4082 /* See if the next token is the `template' keyword. */
4083 if (!template_p)
4084 template_p = &is_template;
4085 *template_p = cp_parser_optional_template_keyword (parser);
4086 /* Name lookup we do during the processing of the
4087 unqualified-id might obliterate SCOPE. */
4088 saved_scope = parser->scope;
4089 saved_object_scope = parser->object_scope;
4090 saved_qualifying_scope = parser->qualifying_scope;
4091 /* Process the final unqualified-id. */
4092 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4093 check_dependency_p,
4094 declarator_p,
4095 /*optional_p=*/false);
4096 /* Restore the SAVED_SCOPE for our caller. */
4097 parser->scope = saved_scope;
4098 parser->object_scope = saved_object_scope;
4099 parser->qualifying_scope = saved_qualifying_scope;
4100
4101 return unqualified_id;
4102 }
4103 /* Otherwise, if we are in global scope, then we are looking at one
4104 of the other qualified-id productions. */
4105 else if (global_scope_p)
4106 {
4107 cp_token *token;
4108 tree id;
4109
4110 /* Peek at the next token. */
4111 token = cp_lexer_peek_token (parser->lexer);
4112
4113 /* If it's an identifier, and the next token is not a "<", then
4114 we can avoid the template-id case. This is an optimization
4115 for this common case. */
4116 if (token->type == CPP_NAME
4117 && !cp_parser_nth_token_starts_template_argument_list_p
4118 (parser, 2))
4119 return cp_parser_identifier (parser);
4120
4121 cp_parser_parse_tentatively (parser);
4122 /* Try a template-id. */
4123 id = cp_parser_template_id (parser,
4124 /*template_keyword_p=*/false,
4125 /*check_dependency_p=*/true,
4126 declarator_p);
4127 /* If that worked, we're done. */
4128 if (cp_parser_parse_definitely (parser))
4129 return id;
4130
4131 /* Peek at the next token. (Changes in the token buffer may
4132 have invalidated the pointer obtained above.) */
4133 token = cp_lexer_peek_token (parser->lexer);
4134
4135 switch (token->type)
4136 {
4137 case CPP_NAME:
4138 return cp_parser_identifier (parser);
4139
4140 case CPP_KEYWORD:
4141 if (token->keyword == RID_OPERATOR)
4142 return cp_parser_operator_function_id (parser);
4143 /* Fall through. */
4144
4145 default:
4146 cp_parser_error (parser, "expected id-expression");
4147 return error_mark_node;
4148 }
4149 }
4150 else
4151 return cp_parser_unqualified_id (parser, template_keyword_p,
4152 /*check_dependency_p=*/true,
4153 declarator_p,
4154 optional_p);
4155 }
4156
4157 /* Parse an unqualified-id.
4158
4159 unqualified-id:
4160 identifier
4161 operator-function-id
4162 conversion-function-id
4163 ~ class-name
4164 template-id
4165
4166 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4167 keyword, in a construct like `A::template ...'.
4168
4169 Returns a representation of unqualified-id. For the `identifier'
4170 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4171 production a BIT_NOT_EXPR is returned; the operand of the
4172 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4173 other productions, see the documentation accompanying the
4174 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4175 names are looked up in uninstantiated templates. If DECLARATOR_P
4176 is true, the unqualified-id is appearing as part of a declarator,
4177 rather than as part of an expression. */
4178
4179 static tree
4180 cp_parser_unqualified_id (cp_parser* parser,
4181 bool template_keyword_p,
4182 bool check_dependency_p,
4183 bool declarator_p,
4184 bool optional_p)
4185 {
4186 cp_token *token;
4187
4188 /* Peek at the next token. */
4189 token = cp_lexer_peek_token (parser->lexer);
4190
4191 switch (token->type)
4192 {
4193 case CPP_NAME:
4194 {
4195 tree id;
4196
4197 /* We don't know yet whether or not this will be a
4198 template-id. */
4199 cp_parser_parse_tentatively (parser);
4200 /* Try a template-id. */
4201 id = cp_parser_template_id (parser, template_keyword_p,
4202 check_dependency_p,
4203 declarator_p);
4204 /* If it worked, we're done. */
4205 if (cp_parser_parse_definitely (parser))
4206 return id;
4207 /* Otherwise, it's an ordinary identifier. */
4208 return cp_parser_identifier (parser);
4209 }
4210
4211 case CPP_TEMPLATE_ID:
4212 return cp_parser_template_id (parser, template_keyword_p,
4213 check_dependency_p,
4214 declarator_p);
4215
4216 case CPP_COMPL:
4217 {
4218 tree type_decl;
4219 tree qualifying_scope;
4220 tree object_scope;
4221 tree scope;
4222 bool done;
4223
4224 /* Consume the `~' token. */
4225 cp_lexer_consume_token (parser->lexer);
4226 /* Parse the class-name. The standard, as written, seems to
4227 say that:
4228
4229 template <typename T> struct S { ~S (); };
4230 template <typename T> S<T>::~S() {}
4231
4232 is invalid, since `~' must be followed by a class-name, but
4233 `S<T>' is dependent, and so not known to be a class.
4234 That's not right; we need to look in uninstantiated
4235 templates. A further complication arises from:
4236
4237 template <typename T> void f(T t) {
4238 t.T::~T();
4239 }
4240
4241 Here, it is not possible to look up `T' in the scope of `T'
4242 itself. We must look in both the current scope, and the
4243 scope of the containing complete expression.
4244
4245 Yet another issue is:
4246
4247 struct S {
4248 int S;
4249 ~S();
4250 };
4251
4252 S::~S() {}
4253
4254 The standard does not seem to say that the `S' in `~S'
4255 should refer to the type `S' and not the data member
4256 `S::S'. */
4257
4258 /* DR 244 says that we look up the name after the "~" in the
4259 same scope as we looked up the qualifying name. That idea
4260 isn't fully worked out; it's more complicated than that. */
4261 scope = parser->scope;
4262 object_scope = parser->object_scope;
4263 qualifying_scope = parser->qualifying_scope;
4264
4265 /* Check for invalid scopes. */
4266 if (scope == error_mark_node)
4267 {
4268 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4269 cp_lexer_consume_token (parser->lexer);
4270 return error_mark_node;
4271 }
4272 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4273 {
4274 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4275 error_at (token->location,
4276 "scope %qT before %<~%> is not a class-name",
4277 scope);
4278 cp_parser_simulate_error (parser);
4279 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4280 cp_lexer_consume_token (parser->lexer);
4281 return error_mark_node;
4282 }
4283 gcc_assert (!scope || TYPE_P (scope));
4284
4285 /* If the name is of the form "X::~X" it's OK even if X is a
4286 typedef. */
4287 token = cp_lexer_peek_token (parser->lexer);
4288 if (scope
4289 && token->type == CPP_NAME
4290 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4291 != CPP_LESS)
4292 && (token->u.value == TYPE_IDENTIFIER (scope)
4293 || constructor_name_p (token->u.value, scope)))
4294 {
4295 cp_lexer_consume_token (parser->lexer);
4296 return build_nt (BIT_NOT_EXPR, scope);
4297 }
4298
4299 /* If there was an explicit qualification (S::~T), first look
4300 in the scope given by the qualification (i.e., S).
4301
4302 Note: in the calls to cp_parser_class_name below we pass
4303 typename_type so that lookup finds the injected-class-name
4304 rather than the constructor. */
4305 done = false;
4306 type_decl = NULL_TREE;
4307 if (scope)
4308 {
4309 cp_parser_parse_tentatively (parser);
4310 type_decl = cp_parser_class_name (parser,
4311 /*typename_keyword_p=*/false,
4312 /*template_keyword_p=*/false,
4313 typename_type,
4314 /*check_dependency=*/false,
4315 /*class_head_p=*/false,
4316 declarator_p);
4317 if (cp_parser_parse_definitely (parser))
4318 done = true;
4319 }
4320 /* In "N::S::~S", look in "N" as well. */
4321 if (!done && scope && qualifying_scope)
4322 {
4323 cp_parser_parse_tentatively (parser);
4324 parser->scope = qualifying_scope;
4325 parser->object_scope = NULL_TREE;
4326 parser->qualifying_scope = NULL_TREE;
4327 type_decl
4328 = cp_parser_class_name (parser,
4329 /*typename_keyword_p=*/false,
4330 /*template_keyword_p=*/false,
4331 typename_type,
4332 /*check_dependency=*/false,
4333 /*class_head_p=*/false,
4334 declarator_p);
4335 if (cp_parser_parse_definitely (parser))
4336 done = true;
4337 }
4338 /* In "p->S::~T", look in the scope given by "*p" as well. */
4339 else if (!done && object_scope)
4340 {
4341 cp_parser_parse_tentatively (parser);
4342 parser->scope = object_scope;
4343 parser->object_scope = NULL_TREE;
4344 parser->qualifying_scope = NULL_TREE;
4345 type_decl
4346 = cp_parser_class_name (parser,
4347 /*typename_keyword_p=*/false,
4348 /*template_keyword_p=*/false,
4349 typename_type,
4350 /*check_dependency=*/false,
4351 /*class_head_p=*/false,
4352 declarator_p);
4353 if (cp_parser_parse_definitely (parser))
4354 done = true;
4355 }
4356 /* Look in the surrounding context. */
4357 if (!done)
4358 {
4359 parser->scope = NULL_TREE;
4360 parser->object_scope = NULL_TREE;
4361 parser->qualifying_scope = NULL_TREE;
4362 if (processing_template_decl)
4363 cp_parser_parse_tentatively (parser);
4364 type_decl
4365 = cp_parser_class_name (parser,
4366 /*typename_keyword_p=*/false,
4367 /*template_keyword_p=*/false,
4368 typename_type,
4369 /*check_dependency=*/false,
4370 /*class_head_p=*/false,
4371 declarator_p);
4372 if (processing_template_decl
4373 && ! cp_parser_parse_definitely (parser))
4374 {
4375 /* We couldn't find a type with this name, so just accept
4376 it and check for a match at instantiation time. */
4377 type_decl = cp_parser_identifier (parser);
4378 if (type_decl != error_mark_node)
4379 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4380 return type_decl;
4381 }
4382 }
4383 /* If an error occurred, assume that the name of the
4384 destructor is the same as the name of the qualifying
4385 class. That allows us to keep parsing after running
4386 into ill-formed destructor names. */
4387 if (type_decl == error_mark_node && scope)
4388 return build_nt (BIT_NOT_EXPR, scope);
4389 else if (type_decl == error_mark_node)
4390 return error_mark_node;
4391
4392 /* Check that destructor name and scope match. */
4393 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4394 {
4395 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4396 error_at (token->location,
4397 "declaration of %<~%T%> as member of %qT",
4398 type_decl, scope);
4399 cp_parser_simulate_error (parser);
4400 return error_mark_node;
4401 }
4402
4403 /* [class.dtor]
4404
4405 A typedef-name that names a class shall not be used as the
4406 identifier in the declarator for a destructor declaration. */
4407 if (declarator_p
4408 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4409 && !DECL_SELF_REFERENCE_P (type_decl)
4410 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4411 error_at (token->location,
4412 "typedef-name %qD used as destructor declarator",
4413 type_decl);
4414
4415 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4416 }
4417
4418 case CPP_KEYWORD:
4419 if (token->keyword == RID_OPERATOR)
4420 {
4421 tree id;
4422
4423 /* This could be a template-id, so we try that first. */
4424 cp_parser_parse_tentatively (parser);
4425 /* Try a template-id. */
4426 id = cp_parser_template_id (parser, template_keyword_p,
4427 /*check_dependency_p=*/true,
4428 declarator_p);
4429 /* If that worked, we're done. */
4430 if (cp_parser_parse_definitely (parser))
4431 return id;
4432 /* We still don't know whether we're looking at an
4433 operator-function-id or a conversion-function-id. */
4434 cp_parser_parse_tentatively (parser);
4435 /* Try an operator-function-id. */
4436 id = cp_parser_operator_function_id (parser);
4437 /* If that didn't work, try a conversion-function-id. */
4438 if (!cp_parser_parse_definitely (parser))
4439 id = cp_parser_conversion_function_id (parser);
4440
4441 return id;
4442 }
4443 /* Fall through. */
4444
4445 default:
4446 if (optional_p)
4447 return NULL_TREE;
4448 cp_parser_error (parser, "expected unqualified-id");
4449 return error_mark_node;
4450 }
4451 }
4452
4453 /* Parse an (optional) nested-name-specifier.
4454
4455 nested-name-specifier: [C++98]
4456 class-or-namespace-name :: nested-name-specifier [opt]
4457 class-or-namespace-name :: template nested-name-specifier [opt]
4458
4459 nested-name-specifier: [C++0x]
4460 type-name ::
4461 namespace-name ::
4462 nested-name-specifier identifier ::
4463 nested-name-specifier template [opt] simple-template-id ::
4464
4465 PARSER->SCOPE should be set appropriately before this function is
4466 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4467 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4468 in name lookups.
4469
4470 Sets PARSER->SCOPE to the class (TYPE) or namespace
4471 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4472 it unchanged if there is no nested-name-specifier. Returns the new
4473 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4474
4475 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4476 part of a declaration and/or decl-specifier. */
4477
4478 static tree
4479 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4480 bool typename_keyword_p,
4481 bool check_dependency_p,
4482 bool type_p,
4483 bool is_declaration)
4484 {
4485 bool success = false;
4486 cp_token_position start = 0;
4487 cp_token *token;
4488
4489 /* Remember where the nested-name-specifier starts. */
4490 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4491 {
4492 start = cp_lexer_token_position (parser->lexer, false);
4493 push_deferring_access_checks (dk_deferred);
4494 }
4495
4496 while (true)
4497 {
4498 tree new_scope;
4499 tree old_scope;
4500 tree saved_qualifying_scope;
4501 bool template_keyword_p;
4502
4503 /* Spot cases that cannot be the beginning of a
4504 nested-name-specifier. */
4505 token = cp_lexer_peek_token (parser->lexer);
4506
4507 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4508 the already parsed nested-name-specifier. */
4509 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4510 {
4511 /* Grab the nested-name-specifier and continue the loop. */
4512 cp_parser_pre_parsed_nested_name_specifier (parser);
4513 /* If we originally encountered this nested-name-specifier
4514 with IS_DECLARATION set to false, we will not have
4515 resolved TYPENAME_TYPEs, so we must do so here. */
4516 if (is_declaration
4517 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4518 {
4519 new_scope = resolve_typename_type (parser->scope,
4520 /*only_current_p=*/false);
4521 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4522 parser->scope = new_scope;
4523 }
4524 success = true;
4525 continue;
4526 }
4527
4528 /* Spot cases that cannot be the beginning of a
4529 nested-name-specifier. On the second and subsequent times
4530 through the loop, we look for the `template' keyword. */
4531 if (success && token->keyword == RID_TEMPLATE)
4532 ;
4533 /* A template-id can start a nested-name-specifier. */
4534 else if (token->type == CPP_TEMPLATE_ID)
4535 ;
4536 else
4537 {
4538 /* If the next token is not an identifier, then it is
4539 definitely not a type-name or namespace-name. */
4540 if (token->type != CPP_NAME)
4541 break;
4542 /* If the following token is neither a `<' (to begin a
4543 template-id), nor a `::', then we are not looking at a
4544 nested-name-specifier. */
4545 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4546 if (token->type != CPP_SCOPE
4547 && !cp_parser_nth_token_starts_template_argument_list_p
4548 (parser, 2))
4549 break;
4550 }
4551
4552 /* The nested-name-specifier is optional, so we parse
4553 tentatively. */
4554 cp_parser_parse_tentatively (parser);
4555
4556 /* Look for the optional `template' keyword, if this isn't the
4557 first time through the loop. */
4558 if (success)
4559 template_keyword_p = cp_parser_optional_template_keyword (parser);
4560 else
4561 template_keyword_p = false;
4562
4563 /* Save the old scope since the name lookup we are about to do
4564 might destroy it. */
4565 old_scope = parser->scope;
4566 saved_qualifying_scope = parser->qualifying_scope;
4567 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4568 look up names in "X<T>::I" in order to determine that "Y" is
4569 a template. So, if we have a typename at this point, we make
4570 an effort to look through it. */
4571 if (is_declaration
4572 && !typename_keyword_p
4573 && parser->scope
4574 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4575 parser->scope = resolve_typename_type (parser->scope,
4576 /*only_current_p=*/false);
4577 /* Parse the qualifying entity. */
4578 new_scope
4579 = cp_parser_qualifying_entity (parser,
4580 typename_keyword_p,
4581 template_keyword_p,
4582 check_dependency_p,
4583 type_p,
4584 is_declaration);
4585 /* Look for the `::' token. */
4586 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4587
4588 /* If we found what we wanted, we keep going; otherwise, we're
4589 done. */
4590 if (!cp_parser_parse_definitely (parser))
4591 {
4592 bool error_p = false;
4593
4594 /* Restore the OLD_SCOPE since it was valid before the
4595 failed attempt at finding the last
4596 class-or-namespace-name. */
4597 parser->scope = old_scope;
4598 parser->qualifying_scope = saved_qualifying_scope;
4599 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4600 break;
4601 /* If the next token is an identifier, and the one after
4602 that is a `::', then any valid interpretation would have
4603 found a class-or-namespace-name. */
4604 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4605 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4606 == CPP_SCOPE)
4607 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4608 != CPP_COMPL))
4609 {
4610 token = cp_lexer_consume_token (parser->lexer);
4611 if (!error_p)
4612 {
4613 if (!token->ambiguous_p)
4614 {
4615 tree decl;
4616 tree ambiguous_decls;
4617
4618 decl = cp_parser_lookup_name (parser, token->u.value,
4619 none_type,
4620 /*is_template=*/false,
4621 /*is_namespace=*/false,
4622 /*check_dependency=*/true,
4623 &ambiguous_decls,
4624 token->location);
4625 if (TREE_CODE (decl) == TEMPLATE_DECL)
4626 error_at (token->location,
4627 "%qD used without template parameters",
4628 decl);
4629 else if (ambiguous_decls)
4630 {
4631 error_at (token->location,
4632 "reference to %qD is ambiguous",
4633 token->u.value);
4634 print_candidates (ambiguous_decls);
4635 decl = error_mark_node;
4636 }
4637 else
4638 {
4639 if (cxx_dialect != cxx98)
4640 cp_parser_name_lookup_error
4641 (parser, token->u.value, decl, NLE_NOT_CXX98,
4642 token->location);
4643 else
4644 cp_parser_name_lookup_error
4645 (parser, token->u.value, decl, NLE_CXX98,
4646 token->location);
4647 }
4648 }
4649 parser->scope = error_mark_node;
4650 error_p = true;
4651 /* Treat this as a successful nested-name-specifier
4652 due to:
4653
4654 [basic.lookup.qual]
4655
4656 If the name found is not a class-name (clause
4657 _class_) or namespace-name (_namespace.def_), the
4658 program is ill-formed. */
4659 success = true;
4660 }
4661 cp_lexer_consume_token (parser->lexer);
4662 }
4663 break;
4664 }
4665 /* We've found one valid nested-name-specifier. */
4666 success = true;
4667 /* Name lookup always gives us a DECL. */
4668 if (TREE_CODE (new_scope) == TYPE_DECL)
4669 new_scope = TREE_TYPE (new_scope);
4670 /* Uses of "template" must be followed by actual templates. */
4671 if (template_keyword_p
4672 && !(CLASS_TYPE_P (new_scope)
4673 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4674 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4675 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4676 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4677 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4678 == TEMPLATE_ID_EXPR)))
4679 permerror (input_location, TYPE_P (new_scope)
4680 ? "%qT is not a template"
4681 : "%qD is not a template",
4682 new_scope);
4683 /* If it is a class scope, try to complete it; we are about to
4684 be looking up names inside the class. */
4685 if (TYPE_P (new_scope)
4686 /* Since checking types for dependency can be expensive,
4687 avoid doing it if the type is already complete. */
4688 && !COMPLETE_TYPE_P (new_scope)
4689 /* Do not try to complete dependent types. */
4690 && !dependent_type_p (new_scope))
4691 {
4692 new_scope = complete_type (new_scope);
4693 /* If it is a typedef to current class, use the current
4694 class instead, as the typedef won't have any names inside
4695 it yet. */
4696 if (!COMPLETE_TYPE_P (new_scope)
4697 && currently_open_class (new_scope))
4698 new_scope = TYPE_MAIN_VARIANT (new_scope);
4699 }
4700 /* Make sure we look in the right scope the next time through
4701 the loop. */
4702 parser->scope = new_scope;
4703 }
4704
4705 /* If parsing tentatively, replace the sequence of tokens that makes
4706 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4707 token. That way, should we re-parse the token stream, we will
4708 not have to repeat the effort required to do the parse, nor will
4709 we issue duplicate error messages. */
4710 if (success && start)
4711 {
4712 cp_token *token;
4713
4714 token = cp_lexer_token_at (parser->lexer, start);
4715 /* Reset the contents of the START token. */
4716 token->type = CPP_NESTED_NAME_SPECIFIER;
4717 /* Retrieve any deferred checks. Do not pop this access checks yet
4718 so the memory will not be reclaimed during token replacing below. */
4719 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4720 token->u.tree_check_value->value = parser->scope;
4721 token->u.tree_check_value->checks = get_deferred_access_checks ();
4722 token->u.tree_check_value->qualifying_scope =
4723 parser->qualifying_scope;
4724 token->keyword = RID_MAX;
4725
4726 /* Purge all subsequent tokens. */
4727 cp_lexer_purge_tokens_after (parser->lexer, start);
4728 }
4729
4730 if (start)
4731 pop_to_parent_deferring_access_checks ();
4732
4733 return success ? parser->scope : NULL_TREE;
4734 }
4735
4736 /* Parse a nested-name-specifier. See
4737 cp_parser_nested_name_specifier_opt for details. This function
4738 behaves identically, except that it will an issue an error if no
4739 nested-name-specifier is present. */
4740
4741 static tree
4742 cp_parser_nested_name_specifier (cp_parser *parser,
4743 bool typename_keyword_p,
4744 bool check_dependency_p,
4745 bool type_p,
4746 bool is_declaration)
4747 {
4748 tree scope;
4749
4750 /* Look for the nested-name-specifier. */
4751 scope = cp_parser_nested_name_specifier_opt (parser,
4752 typename_keyword_p,
4753 check_dependency_p,
4754 type_p,
4755 is_declaration);
4756 /* If it was not present, issue an error message. */
4757 if (!scope)
4758 {
4759 cp_parser_error (parser, "expected nested-name-specifier");
4760 parser->scope = NULL_TREE;
4761 }
4762
4763 return scope;
4764 }
4765
4766 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4767 this is either a class-name or a namespace-name (which corresponds
4768 to the class-or-namespace-name production in the grammar). For
4769 C++0x, it can also be a type-name that refers to an enumeration
4770 type.
4771
4772 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4773 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4774 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4775 TYPE_P is TRUE iff the next name should be taken as a class-name,
4776 even the same name is declared to be another entity in the same
4777 scope.
4778
4779 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4780 specified by the class-or-namespace-name. If neither is found the
4781 ERROR_MARK_NODE is returned. */
4782
4783 static tree
4784 cp_parser_qualifying_entity (cp_parser *parser,
4785 bool typename_keyword_p,
4786 bool template_keyword_p,
4787 bool check_dependency_p,
4788 bool type_p,
4789 bool is_declaration)
4790 {
4791 tree saved_scope;
4792 tree saved_qualifying_scope;
4793 tree saved_object_scope;
4794 tree scope;
4795 bool only_class_p;
4796 bool successful_parse_p;
4797
4798 /* Before we try to parse the class-name, we must save away the
4799 current PARSER->SCOPE since cp_parser_class_name will destroy
4800 it. */
4801 saved_scope = parser->scope;
4802 saved_qualifying_scope = parser->qualifying_scope;
4803 saved_object_scope = parser->object_scope;
4804 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4805 there is no need to look for a namespace-name. */
4806 only_class_p = template_keyword_p
4807 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4808 if (!only_class_p)
4809 cp_parser_parse_tentatively (parser);
4810 scope = cp_parser_class_name (parser,
4811 typename_keyword_p,
4812 template_keyword_p,
4813 type_p ? class_type : none_type,
4814 check_dependency_p,
4815 /*class_head_p=*/false,
4816 is_declaration);
4817 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4818 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4819 if (!only_class_p
4820 && cxx_dialect != cxx98
4821 && !successful_parse_p)
4822 {
4823 /* Restore the saved scope. */
4824 parser->scope = saved_scope;
4825 parser->qualifying_scope = saved_qualifying_scope;
4826 parser->object_scope = saved_object_scope;
4827
4828 /* Parse tentatively. */
4829 cp_parser_parse_tentatively (parser);
4830
4831 /* Parse a typedef-name or enum-name. */
4832 scope = cp_parser_nonclass_name (parser);
4833
4834 /* "If the name found does not designate a namespace or a class,
4835 enumeration, or dependent type, the program is ill-formed."
4836
4837 We cover classes and dependent types above and namespaces below,
4838 so this code is only looking for enums. */
4839 if (!scope || TREE_CODE (scope) != TYPE_DECL
4840 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4841 cp_parser_simulate_error (parser);
4842
4843 successful_parse_p = cp_parser_parse_definitely (parser);
4844 }
4845 /* If that didn't work, try for a namespace-name. */
4846 if (!only_class_p && !successful_parse_p)
4847 {
4848 /* Restore the saved scope. */
4849 parser->scope = saved_scope;
4850 parser->qualifying_scope = saved_qualifying_scope;
4851 parser->object_scope = saved_object_scope;
4852 /* If we are not looking at an identifier followed by the scope
4853 resolution operator, then this is not part of a
4854 nested-name-specifier. (Note that this function is only used
4855 to parse the components of a nested-name-specifier.) */
4856 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4857 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4858 return error_mark_node;
4859 scope = cp_parser_namespace_name (parser);
4860 }
4861
4862 return scope;
4863 }
4864
4865 /* Parse a postfix-expression.
4866
4867 postfix-expression:
4868 primary-expression
4869 postfix-expression [ expression ]
4870 postfix-expression ( expression-list [opt] )
4871 simple-type-specifier ( expression-list [opt] )
4872 typename :: [opt] nested-name-specifier identifier
4873 ( expression-list [opt] )
4874 typename :: [opt] nested-name-specifier template [opt] template-id
4875 ( expression-list [opt] )
4876 postfix-expression . template [opt] id-expression
4877 postfix-expression -> template [opt] id-expression
4878 postfix-expression . pseudo-destructor-name
4879 postfix-expression -> pseudo-destructor-name
4880 postfix-expression ++
4881 postfix-expression --
4882 dynamic_cast < type-id > ( expression )
4883 static_cast < type-id > ( expression )
4884 reinterpret_cast < type-id > ( expression )
4885 const_cast < type-id > ( expression )
4886 typeid ( expression )
4887 typeid ( type-id )
4888
4889 GNU Extension:
4890
4891 postfix-expression:
4892 ( type-id ) { initializer-list , [opt] }
4893
4894 This extension is a GNU version of the C99 compound-literal
4895 construct. (The C99 grammar uses `type-name' instead of `type-id',
4896 but they are essentially the same concept.)
4897
4898 If ADDRESS_P is true, the postfix expression is the operand of the
4899 `&' operator. CAST_P is true if this expression is the target of a
4900 cast.
4901
4902 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4903 class member access expressions [expr.ref].
4904
4905 Returns a representation of the expression. */
4906
4907 static tree
4908 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4909 bool member_access_only_p,
4910 cp_id_kind * pidk_return)
4911 {
4912 cp_token *token;
4913 enum rid keyword;
4914 cp_id_kind idk = CP_ID_KIND_NONE;
4915 tree postfix_expression = NULL_TREE;
4916 bool is_member_access = false;
4917
4918 /* Peek at the next token. */
4919 token = cp_lexer_peek_token (parser->lexer);
4920 /* Some of the productions are determined by keywords. */
4921 keyword = token->keyword;
4922 switch (keyword)
4923 {
4924 case RID_DYNCAST:
4925 case RID_STATCAST:
4926 case RID_REINTCAST:
4927 case RID_CONSTCAST:
4928 {
4929 tree type;
4930 tree expression;
4931 const char *saved_message;
4932
4933 /* All of these can be handled in the same way from the point
4934 of view of parsing. Begin by consuming the token
4935 identifying the cast. */
4936 cp_lexer_consume_token (parser->lexer);
4937
4938 /* New types cannot be defined in the cast. */
4939 saved_message = parser->type_definition_forbidden_message;
4940 parser->type_definition_forbidden_message
4941 = G_("types may not be defined in casts");
4942
4943 /* Look for the opening `<'. */
4944 cp_parser_require (parser, CPP_LESS, RT_LESS);
4945 /* Parse the type to which we are casting. */
4946 type = cp_parser_type_id (parser);
4947 /* Look for the closing `>'. */
4948 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4949 /* Restore the old message. */
4950 parser->type_definition_forbidden_message = saved_message;
4951
4952 /* And the expression which is being cast. */
4953 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4954 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4955 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4956
4957 /* Only type conversions to integral or enumeration types
4958 can be used in constant-expressions. */
4959 if (!cast_valid_in_integral_constant_expression_p (type)
4960 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4961 return error_mark_node;
4962
4963 switch (keyword)
4964 {
4965 case RID_DYNCAST:
4966 postfix_expression
4967 = build_dynamic_cast (type, expression, tf_warning_or_error);
4968 break;
4969 case RID_STATCAST:
4970 postfix_expression
4971 = build_static_cast (type, expression, tf_warning_or_error);
4972 break;
4973 case RID_REINTCAST:
4974 postfix_expression
4975 = build_reinterpret_cast (type, expression,
4976 tf_warning_or_error);
4977 break;
4978 case RID_CONSTCAST:
4979 postfix_expression
4980 = build_const_cast (type, expression, tf_warning_or_error);
4981 break;
4982 default:
4983 gcc_unreachable ();
4984 }
4985 }
4986 break;
4987
4988 case RID_TYPEID:
4989 {
4990 tree type;
4991 const char *saved_message;
4992 bool saved_in_type_id_in_expr_p;
4993
4994 /* Consume the `typeid' token. */
4995 cp_lexer_consume_token (parser->lexer);
4996 /* Look for the `(' token. */
4997 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4998 /* Types cannot be defined in a `typeid' expression. */
4999 saved_message = parser->type_definition_forbidden_message;
5000 parser->type_definition_forbidden_message
5001 = G_("types may not be defined in a %<typeid%> expression");
5002 /* We can't be sure yet whether we're looking at a type-id or an
5003 expression. */
5004 cp_parser_parse_tentatively (parser);
5005 /* Try a type-id first. */
5006 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5007 parser->in_type_id_in_expr_p = true;
5008 type = cp_parser_type_id (parser);
5009 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5010 /* Look for the `)' token. Otherwise, we can't be sure that
5011 we're not looking at an expression: consider `typeid (int
5012 (3))', for example. */
5013 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5014 /* If all went well, simply lookup the type-id. */
5015 if (cp_parser_parse_definitely (parser))
5016 postfix_expression = get_typeid (type);
5017 /* Otherwise, fall back to the expression variant. */
5018 else
5019 {
5020 tree expression;
5021
5022 /* Look for an expression. */
5023 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5024 /* Compute its typeid. */
5025 postfix_expression = build_typeid (expression);
5026 /* Look for the `)' token. */
5027 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5028 }
5029 /* Restore the saved message. */
5030 parser->type_definition_forbidden_message = saved_message;
5031 /* `typeid' may not appear in an integral constant expression. */
5032 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
5033 return error_mark_node;
5034 }
5035 break;
5036
5037 case RID_TYPENAME:
5038 {
5039 tree type;
5040 /* The syntax permitted here is the same permitted for an
5041 elaborated-type-specifier. */
5042 type = cp_parser_elaborated_type_specifier (parser,
5043 /*is_friend=*/false,
5044 /*is_declaration=*/false);
5045 postfix_expression = cp_parser_functional_cast (parser, type);
5046 }
5047 break;
5048
5049 default:
5050 {
5051 tree type;
5052
5053 /* If the next thing is a simple-type-specifier, we may be
5054 looking at a functional cast. We could also be looking at
5055 an id-expression. So, we try the functional cast, and if
5056 that doesn't work we fall back to the primary-expression. */
5057 cp_parser_parse_tentatively (parser);
5058 /* Look for the simple-type-specifier. */
5059 type = cp_parser_simple_type_specifier (parser,
5060 /*decl_specs=*/NULL,
5061 CP_PARSER_FLAGS_NONE);
5062 /* Parse the cast itself. */
5063 if (!cp_parser_error_occurred (parser))
5064 postfix_expression
5065 = cp_parser_functional_cast (parser, type);
5066 /* If that worked, we're done. */
5067 if (cp_parser_parse_definitely (parser))
5068 break;
5069
5070 /* If the functional-cast didn't work out, try a
5071 compound-literal. */
5072 if (cp_parser_allow_gnu_extensions_p (parser)
5073 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5074 {
5075 VEC(constructor_elt,gc) *initializer_list = NULL;
5076 bool saved_in_type_id_in_expr_p;
5077
5078 cp_parser_parse_tentatively (parser);
5079 /* Consume the `('. */
5080 cp_lexer_consume_token (parser->lexer);
5081 /* Parse the type. */
5082 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5083 parser->in_type_id_in_expr_p = true;
5084 type = cp_parser_type_id (parser);
5085 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5086 /* Look for the `)'. */
5087 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5088 /* Look for the `{'. */
5089 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5090 /* If things aren't going well, there's no need to
5091 keep going. */
5092 if (!cp_parser_error_occurred (parser))
5093 {
5094 bool non_constant_p;
5095 /* Parse the initializer-list. */
5096 initializer_list
5097 = cp_parser_initializer_list (parser, &non_constant_p);
5098 /* Allow a trailing `,'. */
5099 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5100 cp_lexer_consume_token (parser->lexer);
5101 /* Look for the final `}'. */
5102 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5103 }
5104 /* If that worked, we're definitely looking at a
5105 compound-literal expression. */
5106 if (cp_parser_parse_definitely (parser))
5107 {
5108 /* Warn the user that a compound literal is not
5109 allowed in standard C++. */
5110 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5111 /* For simplicity, we disallow compound literals in
5112 constant-expressions. We could
5113 allow compound literals of integer type, whose
5114 initializer was a constant, in constant
5115 expressions. Permitting that usage, as a further
5116 extension, would not change the meaning of any
5117 currently accepted programs. (Of course, as
5118 compound literals are not part of ISO C++, the
5119 standard has nothing to say.) */
5120 if (cp_parser_non_integral_constant_expression (parser,
5121 NIC_NCC))
5122 {
5123 postfix_expression = error_mark_node;
5124 break;
5125 }
5126 /* Form the representation of the compound-literal. */
5127 postfix_expression
5128 = (finish_compound_literal
5129 (type, build_constructor (init_list_type_node,
5130 initializer_list)));
5131 break;
5132 }
5133 }
5134
5135 /* It must be a primary-expression. */
5136 postfix_expression
5137 = cp_parser_primary_expression (parser, address_p, cast_p,
5138 /*template_arg_p=*/false,
5139 &idk);
5140 }
5141 break;
5142 }
5143
5144 /* Keep looping until the postfix-expression is complete. */
5145 while (true)
5146 {
5147 if (idk == CP_ID_KIND_UNQUALIFIED
5148 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5149 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5150 /* It is not a Koenig lookup function call. */
5151 postfix_expression
5152 = unqualified_name_lookup_error (postfix_expression);
5153
5154 /* Peek at the next token. */
5155 token = cp_lexer_peek_token (parser->lexer);
5156
5157 switch (token->type)
5158 {
5159 case CPP_OPEN_SQUARE:
5160 postfix_expression
5161 = cp_parser_postfix_open_square_expression (parser,
5162 postfix_expression,
5163 false);
5164 idk = CP_ID_KIND_NONE;
5165 is_member_access = false;
5166 break;
5167
5168 case CPP_OPEN_PAREN:
5169 /* postfix-expression ( expression-list [opt] ) */
5170 {
5171 bool koenig_p;
5172 bool is_builtin_constant_p;
5173 bool saved_integral_constant_expression_p = false;
5174 bool saved_non_integral_constant_expression_p = false;
5175 VEC(tree,gc) *args;
5176
5177 is_member_access = false;
5178
5179 is_builtin_constant_p
5180 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5181 if (is_builtin_constant_p)
5182 {
5183 /* The whole point of __builtin_constant_p is to allow
5184 non-constant expressions to appear as arguments. */
5185 saved_integral_constant_expression_p
5186 = parser->integral_constant_expression_p;
5187 saved_non_integral_constant_expression_p
5188 = parser->non_integral_constant_expression_p;
5189 parser->integral_constant_expression_p = false;
5190 }
5191 args = (cp_parser_parenthesized_expression_list
5192 (parser, non_attr,
5193 /*cast_p=*/false, /*allow_expansion_p=*/true,
5194 /*non_constant_p=*/NULL));
5195 if (is_builtin_constant_p)
5196 {
5197 parser->integral_constant_expression_p
5198 = saved_integral_constant_expression_p;
5199 parser->non_integral_constant_expression_p
5200 = saved_non_integral_constant_expression_p;
5201 }
5202
5203 if (args == NULL)
5204 {
5205 postfix_expression = error_mark_node;
5206 break;
5207 }
5208
5209 /* Function calls are not permitted in
5210 constant-expressions. */
5211 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5212 && cp_parser_non_integral_constant_expression (parser,
5213 NIC_FUNC_CALL))
5214 {
5215 postfix_expression = error_mark_node;
5216 release_tree_vector (args);
5217 break;
5218 }
5219
5220 koenig_p = false;
5221 if (idk == CP_ID_KIND_UNQUALIFIED
5222 || idk == CP_ID_KIND_TEMPLATE_ID)
5223 {
5224 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5225 {
5226 if (!VEC_empty (tree, args))
5227 {
5228 koenig_p = true;
5229 if (!any_type_dependent_arguments_p (args))
5230 postfix_expression
5231 = perform_koenig_lookup (postfix_expression, args,
5232 /*include_std=*/false);
5233 }
5234 else
5235 postfix_expression
5236 = unqualified_fn_lookup_error (postfix_expression);
5237 }
5238 /* We do not perform argument-dependent lookup if
5239 normal lookup finds a non-function, in accordance
5240 with the expected resolution of DR 218. */
5241 else if (!VEC_empty (tree, args)
5242 && is_overloaded_fn (postfix_expression))
5243 {
5244 tree fn = get_first_fn (postfix_expression);
5245 fn = STRIP_TEMPLATE (fn);
5246
5247 /* Do not do argument dependent lookup if regular
5248 lookup finds a member function or a block-scope
5249 function declaration. [basic.lookup.argdep]/3 */
5250 if (!DECL_FUNCTION_MEMBER_P (fn)
5251 && !DECL_LOCAL_FUNCTION_P (fn))
5252 {
5253 koenig_p = true;
5254 if (!any_type_dependent_arguments_p (args))
5255 postfix_expression
5256 = perform_koenig_lookup (postfix_expression, args,
5257 /*include_std=*/false);
5258 }
5259 }
5260 }
5261
5262 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5263 {
5264 tree instance = TREE_OPERAND (postfix_expression, 0);
5265 tree fn = TREE_OPERAND (postfix_expression, 1);
5266
5267 if (processing_template_decl
5268 && (type_dependent_expression_p (instance)
5269 || (!BASELINK_P (fn)
5270 && TREE_CODE (fn) != FIELD_DECL)
5271 || type_dependent_expression_p (fn)
5272 || any_type_dependent_arguments_p (args)))
5273 {
5274 postfix_expression
5275 = build_nt_call_vec (postfix_expression, args);
5276 release_tree_vector (args);
5277 break;
5278 }
5279
5280 if (BASELINK_P (fn))
5281 {
5282 postfix_expression
5283 = (build_new_method_call
5284 (instance, fn, &args, NULL_TREE,
5285 (idk == CP_ID_KIND_QUALIFIED
5286 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5287 /*fn_p=*/NULL,
5288 tf_warning_or_error));
5289 }
5290 else
5291 postfix_expression
5292 = finish_call_expr (postfix_expression, &args,
5293 /*disallow_virtual=*/false,
5294 /*koenig_p=*/false,
5295 tf_warning_or_error);
5296 }
5297 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5298 || TREE_CODE (postfix_expression) == MEMBER_REF
5299 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5300 postfix_expression = (build_offset_ref_call_from_tree
5301 (postfix_expression, &args));
5302 else if (idk == CP_ID_KIND_QUALIFIED)
5303 /* A call to a static class member, or a namespace-scope
5304 function. */
5305 postfix_expression
5306 = finish_call_expr (postfix_expression, &args,
5307 /*disallow_virtual=*/true,
5308 koenig_p,
5309 tf_warning_or_error);
5310 else
5311 /* All other function calls. */
5312 postfix_expression
5313 = finish_call_expr (postfix_expression, &args,
5314 /*disallow_virtual=*/false,
5315 koenig_p,
5316 tf_warning_or_error);
5317
5318 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5319 idk = CP_ID_KIND_NONE;
5320
5321 release_tree_vector (args);
5322 }
5323 break;
5324
5325 case CPP_DOT:
5326 case CPP_DEREF:
5327 /* postfix-expression . template [opt] id-expression
5328 postfix-expression . pseudo-destructor-name
5329 postfix-expression -> template [opt] id-expression
5330 postfix-expression -> pseudo-destructor-name */
5331
5332 /* Consume the `.' or `->' operator. */
5333 cp_lexer_consume_token (parser->lexer);
5334
5335 postfix_expression
5336 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5337 postfix_expression,
5338 false, &idk,
5339 token->location);
5340
5341 is_member_access = true;
5342 break;
5343
5344 case CPP_PLUS_PLUS:
5345 /* postfix-expression ++ */
5346 /* Consume the `++' token. */
5347 cp_lexer_consume_token (parser->lexer);
5348 /* Generate a representation for the complete expression. */
5349 postfix_expression
5350 = finish_increment_expr (postfix_expression,
5351 POSTINCREMENT_EXPR);
5352 /* Increments may not appear in constant-expressions. */
5353 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5354 postfix_expression = error_mark_node;
5355 idk = CP_ID_KIND_NONE;
5356 is_member_access = false;
5357 break;
5358
5359 case CPP_MINUS_MINUS:
5360 /* postfix-expression -- */
5361 /* Consume the `--' token. */
5362 cp_lexer_consume_token (parser->lexer);
5363 /* Generate a representation for the complete expression. */
5364 postfix_expression
5365 = finish_increment_expr (postfix_expression,
5366 POSTDECREMENT_EXPR);
5367 /* Decrements may not appear in constant-expressions. */
5368 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5369 postfix_expression = error_mark_node;
5370 idk = CP_ID_KIND_NONE;
5371 is_member_access = false;
5372 break;
5373
5374 default:
5375 if (pidk_return != NULL)
5376 * pidk_return = idk;
5377 if (member_access_only_p)
5378 return is_member_access? postfix_expression : error_mark_node;
5379 else
5380 return postfix_expression;
5381 }
5382 }
5383
5384 /* We should never get here. */
5385 gcc_unreachable ();
5386 return error_mark_node;
5387 }
5388
5389 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5390 by cp_parser_builtin_offsetof. We're looking for
5391
5392 postfix-expression [ expression ]
5393
5394 FOR_OFFSETOF is set if we're being called in that context, which
5395 changes how we deal with integer constant expressions. */
5396
5397 static tree
5398 cp_parser_postfix_open_square_expression (cp_parser *parser,
5399 tree postfix_expression,
5400 bool for_offsetof)
5401 {
5402 tree index;
5403
5404 /* Consume the `[' token. */
5405 cp_lexer_consume_token (parser->lexer);
5406
5407 /* Parse the index expression. */
5408 /* ??? For offsetof, there is a question of what to allow here. If
5409 offsetof is not being used in an integral constant expression context,
5410 then we *could* get the right answer by computing the value at runtime.
5411 If we are in an integral constant expression context, then we might
5412 could accept any constant expression; hard to say without analysis.
5413 Rather than open the barn door too wide right away, allow only integer
5414 constant expressions here. */
5415 if (for_offsetof)
5416 index = cp_parser_constant_expression (parser, false, NULL);
5417 else
5418 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5419
5420 /* Look for the closing `]'. */
5421 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5422
5423 /* Build the ARRAY_REF. */
5424 postfix_expression = grok_array_decl (postfix_expression, index);
5425
5426 /* When not doing offsetof, array references are not permitted in
5427 constant-expressions. */
5428 if (!for_offsetof
5429 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5430 postfix_expression = error_mark_node;
5431
5432 return postfix_expression;
5433 }
5434
5435 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5436 by cp_parser_builtin_offsetof. We're looking for
5437
5438 postfix-expression . template [opt] id-expression
5439 postfix-expression . pseudo-destructor-name
5440 postfix-expression -> template [opt] id-expression
5441 postfix-expression -> pseudo-destructor-name
5442
5443 FOR_OFFSETOF is set if we're being called in that context. That sorta
5444 limits what of the above we'll actually accept, but nevermind.
5445 TOKEN_TYPE is the "." or "->" token, which will already have been
5446 removed from the stream. */
5447
5448 static tree
5449 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5450 enum cpp_ttype token_type,
5451 tree postfix_expression,
5452 bool for_offsetof, cp_id_kind *idk,
5453 location_t location)
5454 {
5455 tree name;
5456 bool dependent_p;
5457 bool pseudo_destructor_p;
5458 tree scope = NULL_TREE;
5459
5460 /* If this is a `->' operator, dereference the pointer. */
5461 if (token_type == CPP_DEREF)
5462 postfix_expression = build_x_arrow (postfix_expression);
5463 /* Check to see whether or not the expression is type-dependent. */
5464 dependent_p = type_dependent_expression_p (postfix_expression);
5465 /* The identifier following the `->' or `.' is not qualified. */
5466 parser->scope = NULL_TREE;
5467 parser->qualifying_scope = NULL_TREE;
5468 parser->object_scope = NULL_TREE;
5469 *idk = CP_ID_KIND_NONE;
5470
5471 /* Enter the scope corresponding to the type of the object
5472 given by the POSTFIX_EXPRESSION. */
5473 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5474 {
5475 scope = TREE_TYPE (postfix_expression);
5476 /* According to the standard, no expression should ever have
5477 reference type. Unfortunately, we do not currently match
5478 the standard in this respect in that our internal representation
5479 of an expression may have reference type even when the standard
5480 says it does not. Therefore, we have to manually obtain the
5481 underlying type here. */
5482 scope = non_reference (scope);
5483 /* The type of the POSTFIX_EXPRESSION must be complete. */
5484 if (scope == unknown_type_node)
5485 {
5486 error_at (location, "%qE does not have class type",
5487 postfix_expression);
5488 scope = NULL_TREE;
5489 }
5490 else
5491 scope = complete_type_or_else (scope, NULL_TREE);
5492 /* Let the name lookup machinery know that we are processing a
5493 class member access expression. */
5494 parser->context->object_type = scope;
5495 /* If something went wrong, we want to be able to discern that case,
5496 as opposed to the case where there was no SCOPE due to the type
5497 of expression being dependent. */
5498 if (!scope)
5499 scope = error_mark_node;
5500 /* If the SCOPE was erroneous, make the various semantic analysis
5501 functions exit quickly -- and without issuing additional error
5502 messages. */
5503 if (scope == error_mark_node)
5504 postfix_expression = error_mark_node;
5505 }
5506
5507 /* Assume this expression is not a pseudo-destructor access. */
5508 pseudo_destructor_p = false;
5509
5510 /* If the SCOPE is a scalar type, then, if this is a valid program,
5511 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5512 is type dependent, it can be pseudo-destructor-name or something else.
5513 Try to parse it as pseudo-destructor-name first. */
5514 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5515 {
5516 tree s;
5517 tree type;
5518
5519 cp_parser_parse_tentatively (parser);
5520 /* Parse the pseudo-destructor-name. */
5521 s = NULL_TREE;
5522 cp_parser_pseudo_destructor_name (parser, &s, &type);
5523 if (dependent_p
5524 && (cp_parser_error_occurred (parser)
5525 || TREE_CODE (type) != TYPE_DECL
5526 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5527 cp_parser_abort_tentative_parse (parser);
5528 else if (cp_parser_parse_definitely (parser))
5529 {
5530 pseudo_destructor_p = true;
5531 postfix_expression
5532 = finish_pseudo_destructor_expr (postfix_expression,
5533 s, TREE_TYPE (type));
5534 }
5535 }
5536
5537 if (!pseudo_destructor_p)
5538 {
5539 /* If the SCOPE is not a scalar type, we are looking at an
5540 ordinary class member access expression, rather than a
5541 pseudo-destructor-name. */
5542 bool template_p;
5543 cp_token *token = cp_lexer_peek_token (parser->lexer);
5544 /* Parse the id-expression. */
5545 name = (cp_parser_id_expression
5546 (parser,
5547 cp_parser_optional_template_keyword (parser),
5548 /*check_dependency_p=*/true,
5549 &template_p,
5550 /*declarator_p=*/false,
5551 /*optional_p=*/false));
5552 /* In general, build a SCOPE_REF if the member name is qualified.
5553 However, if the name was not dependent and has already been
5554 resolved; there is no need to build the SCOPE_REF. For example;
5555
5556 struct X { void f(); };
5557 template <typename T> void f(T* t) { t->X::f(); }
5558
5559 Even though "t" is dependent, "X::f" is not and has been resolved
5560 to a BASELINK; there is no need to include scope information. */
5561
5562 /* But we do need to remember that there was an explicit scope for
5563 virtual function calls. */
5564 if (parser->scope)
5565 *idk = CP_ID_KIND_QUALIFIED;
5566
5567 /* If the name is a template-id that names a type, we will get a
5568 TYPE_DECL here. That is invalid code. */
5569 if (TREE_CODE (name) == TYPE_DECL)
5570 {
5571 error_at (token->location, "invalid use of %qD", name);
5572 postfix_expression = error_mark_node;
5573 }
5574 else
5575 {
5576 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5577 {
5578 name = build_qualified_name (/*type=*/NULL_TREE,
5579 parser->scope,
5580 name,
5581 template_p);
5582 parser->scope = NULL_TREE;
5583 parser->qualifying_scope = NULL_TREE;
5584 parser->object_scope = NULL_TREE;
5585 }
5586 if (scope && name && BASELINK_P (name))
5587 adjust_result_of_qualified_name_lookup
5588 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5589 postfix_expression
5590 = finish_class_member_access_expr (postfix_expression, name,
5591 template_p,
5592 tf_warning_or_error);
5593 }
5594 }
5595
5596 /* We no longer need to look up names in the scope of the object on
5597 the left-hand side of the `.' or `->' operator. */
5598 parser->context->object_type = NULL_TREE;
5599
5600 /* Outside of offsetof, these operators may not appear in
5601 constant-expressions. */
5602 if (!for_offsetof
5603 && (cp_parser_non_integral_constant_expression
5604 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5605 postfix_expression = error_mark_node;
5606
5607 return postfix_expression;
5608 }
5609
5610 /* Parse a parenthesized expression-list.
5611
5612 expression-list:
5613 assignment-expression
5614 expression-list, assignment-expression
5615
5616 attribute-list:
5617 expression-list
5618 identifier
5619 identifier, expression-list
5620
5621 CAST_P is true if this expression is the target of a cast.
5622
5623 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5624 argument pack.
5625
5626 Returns a vector of trees. Each element is a representation of an
5627 assignment-expression. NULL is returned if the ( and or ) are
5628 missing. An empty, but allocated, vector is returned on no
5629 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5630 if we are parsing an attribute list for an attribute that wants a
5631 plain identifier argument, normal_attr for an attribute that wants
5632 an expression, or non_attr if we aren't parsing an attribute list. If
5633 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5634 not all of the expressions in the list were constant. */
5635
5636 static VEC(tree,gc) *
5637 cp_parser_parenthesized_expression_list (cp_parser* parser,
5638 int is_attribute_list,
5639 bool cast_p,
5640 bool allow_expansion_p,
5641 bool *non_constant_p)
5642 {
5643 VEC(tree,gc) *expression_list;
5644 bool fold_expr_p = is_attribute_list != non_attr;
5645 tree identifier = NULL_TREE;
5646 bool saved_greater_than_is_operator_p;
5647
5648 /* Assume all the expressions will be constant. */
5649 if (non_constant_p)
5650 *non_constant_p = false;
5651
5652 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5653 return NULL;
5654
5655 expression_list = make_tree_vector ();
5656
5657 /* Within a parenthesized expression, a `>' token is always
5658 the greater-than operator. */
5659 saved_greater_than_is_operator_p
5660 = parser->greater_than_is_operator_p;
5661 parser->greater_than_is_operator_p = true;
5662
5663 /* Consume expressions until there are no more. */
5664 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5665 while (true)
5666 {
5667 tree expr;
5668
5669 /* At the beginning of attribute lists, check to see if the
5670 next token is an identifier. */
5671 if (is_attribute_list == id_attr
5672 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5673 {
5674 cp_token *token;
5675
5676 /* Consume the identifier. */
5677 token = cp_lexer_consume_token (parser->lexer);
5678 /* Save the identifier. */
5679 identifier = token->u.value;
5680 }
5681 else
5682 {
5683 bool expr_non_constant_p;
5684
5685 /* Parse the next assignment-expression. */
5686 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5687 {
5688 /* A braced-init-list. */
5689 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5690 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5691 if (non_constant_p && expr_non_constant_p)
5692 *non_constant_p = true;
5693 }
5694 else if (non_constant_p)
5695 {
5696 expr = (cp_parser_constant_expression
5697 (parser, /*allow_non_constant_p=*/true,
5698 &expr_non_constant_p));
5699 if (expr_non_constant_p)
5700 *non_constant_p = true;
5701 }
5702 else
5703 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5704
5705 if (fold_expr_p)
5706 expr = fold_non_dependent_expr (expr);
5707
5708 /* If we have an ellipsis, then this is an expression
5709 expansion. */
5710 if (allow_expansion_p
5711 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5712 {
5713 /* Consume the `...'. */
5714 cp_lexer_consume_token (parser->lexer);
5715
5716 /* Build the argument pack. */
5717 expr = make_pack_expansion (expr);
5718 }
5719
5720 /* Add it to the list. We add error_mark_node
5721 expressions to the list, so that we can still tell if
5722 the correct form for a parenthesized expression-list
5723 is found. That gives better errors. */
5724 VEC_safe_push (tree, gc, expression_list, expr);
5725
5726 if (expr == error_mark_node)
5727 goto skip_comma;
5728 }
5729
5730 /* After the first item, attribute lists look the same as
5731 expression lists. */
5732 is_attribute_list = non_attr;
5733
5734 get_comma:;
5735 /* If the next token isn't a `,', then we are done. */
5736 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5737 break;
5738
5739 /* Otherwise, consume the `,' and keep going. */
5740 cp_lexer_consume_token (parser->lexer);
5741 }
5742
5743 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5744 {
5745 int ending;
5746
5747 skip_comma:;
5748 /* We try and resync to an unnested comma, as that will give the
5749 user better diagnostics. */
5750 ending = cp_parser_skip_to_closing_parenthesis (parser,
5751 /*recovering=*/true,
5752 /*or_comma=*/true,
5753 /*consume_paren=*/true);
5754 if (ending < 0)
5755 goto get_comma;
5756 if (!ending)
5757 {
5758 parser->greater_than_is_operator_p
5759 = saved_greater_than_is_operator_p;
5760 return NULL;
5761 }
5762 }
5763
5764 parser->greater_than_is_operator_p
5765 = saved_greater_than_is_operator_p;
5766
5767 if (identifier)
5768 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5769
5770 return expression_list;
5771 }
5772
5773 /* Parse a pseudo-destructor-name.
5774
5775 pseudo-destructor-name:
5776 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5777 :: [opt] nested-name-specifier template template-id :: ~ type-name
5778 :: [opt] nested-name-specifier [opt] ~ type-name
5779
5780 If either of the first two productions is used, sets *SCOPE to the
5781 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5782 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5783 or ERROR_MARK_NODE if the parse fails. */
5784
5785 static void
5786 cp_parser_pseudo_destructor_name (cp_parser* parser,
5787 tree* scope,
5788 tree* type)
5789 {
5790 bool nested_name_specifier_p;
5791
5792 /* Assume that things will not work out. */
5793 *type = error_mark_node;
5794
5795 /* Look for the optional `::' operator. */
5796 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5797 /* Look for the optional nested-name-specifier. */
5798 nested_name_specifier_p
5799 = (cp_parser_nested_name_specifier_opt (parser,
5800 /*typename_keyword_p=*/false,
5801 /*check_dependency_p=*/true,
5802 /*type_p=*/false,
5803 /*is_declaration=*/false)
5804 != NULL_TREE);
5805 /* Now, if we saw a nested-name-specifier, we might be doing the
5806 second production. */
5807 if (nested_name_specifier_p
5808 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5809 {
5810 /* Consume the `template' keyword. */
5811 cp_lexer_consume_token (parser->lexer);
5812 /* Parse the template-id. */
5813 cp_parser_template_id (parser,
5814 /*template_keyword_p=*/true,
5815 /*check_dependency_p=*/false,
5816 /*is_declaration=*/true);
5817 /* Look for the `::' token. */
5818 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5819 }
5820 /* If the next token is not a `~', then there might be some
5821 additional qualification. */
5822 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5823 {
5824 /* At this point, we're looking for "type-name :: ~". The type-name
5825 must not be a class-name, since this is a pseudo-destructor. So,
5826 it must be either an enum-name, or a typedef-name -- both of which
5827 are just identifiers. So, we peek ahead to check that the "::"
5828 and "~" tokens are present; if they are not, then we can avoid
5829 calling type_name. */
5830 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5831 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5832 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5833 {
5834 cp_parser_error (parser, "non-scalar type");
5835 return;
5836 }
5837
5838 /* Look for the type-name. */
5839 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5840 if (*scope == error_mark_node)
5841 return;
5842
5843 /* Look for the `::' token. */
5844 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5845 }
5846 else
5847 *scope = NULL_TREE;
5848
5849 /* Look for the `~'. */
5850 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5851 /* Look for the type-name again. We are not responsible for
5852 checking that it matches the first type-name. */
5853 *type = cp_parser_nonclass_name (parser);
5854 }
5855
5856 /* Parse a unary-expression.
5857
5858 unary-expression:
5859 postfix-expression
5860 ++ cast-expression
5861 -- cast-expression
5862 unary-operator cast-expression
5863 sizeof unary-expression
5864 sizeof ( type-id )
5865 new-expression
5866 delete-expression
5867
5868 GNU Extensions:
5869
5870 unary-expression:
5871 __extension__ cast-expression
5872 __alignof__ unary-expression
5873 __alignof__ ( type-id )
5874 __real__ cast-expression
5875 __imag__ cast-expression
5876 && identifier
5877
5878 ADDRESS_P is true iff the unary-expression is appearing as the
5879 operand of the `&' operator. CAST_P is true if this expression is
5880 the target of a cast.
5881
5882 Returns a representation of the expression. */
5883
5884 static tree
5885 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5886 cp_id_kind * pidk)
5887 {
5888 cp_token *token;
5889 enum tree_code unary_operator;
5890
5891 /* Peek at the next token. */
5892 token = cp_lexer_peek_token (parser->lexer);
5893 /* Some keywords give away the kind of expression. */
5894 if (token->type == CPP_KEYWORD)
5895 {
5896 enum rid keyword = token->keyword;
5897
5898 switch (keyword)
5899 {
5900 case RID_ALIGNOF:
5901 case RID_SIZEOF:
5902 {
5903 tree operand;
5904 enum tree_code op;
5905
5906 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5907 /* Consume the token. */
5908 cp_lexer_consume_token (parser->lexer);
5909 /* Parse the operand. */
5910 operand = cp_parser_sizeof_operand (parser, keyword);
5911
5912 if (TYPE_P (operand))
5913 return cxx_sizeof_or_alignof_type (operand, op, true);
5914 else
5915 return cxx_sizeof_or_alignof_expr (operand, op, true);
5916 }
5917
5918 case RID_NEW:
5919 return cp_parser_new_expression (parser);
5920
5921 case RID_DELETE:
5922 return cp_parser_delete_expression (parser);
5923
5924 case RID_EXTENSION:
5925 {
5926 /* The saved value of the PEDANTIC flag. */
5927 int saved_pedantic;
5928 tree expr;
5929
5930 /* Save away the PEDANTIC flag. */
5931 cp_parser_extension_opt (parser, &saved_pedantic);
5932 /* Parse the cast-expression. */
5933 expr = cp_parser_simple_cast_expression (parser);
5934 /* Restore the PEDANTIC flag. */
5935 pedantic = saved_pedantic;
5936
5937 return expr;
5938 }
5939
5940 case RID_REALPART:
5941 case RID_IMAGPART:
5942 {
5943 tree expression;
5944
5945 /* Consume the `__real__' or `__imag__' token. */
5946 cp_lexer_consume_token (parser->lexer);
5947 /* Parse the cast-expression. */
5948 expression = cp_parser_simple_cast_expression (parser);
5949 /* Create the complete representation. */
5950 return build_x_unary_op ((keyword == RID_REALPART
5951 ? REALPART_EXPR : IMAGPART_EXPR),
5952 expression,
5953 tf_warning_or_error);
5954 }
5955 break;
5956
5957 case RID_NOEXCEPT:
5958 {
5959 tree expr;
5960 const char *saved_message;
5961 bool saved_integral_constant_expression_p;
5962 bool saved_non_integral_constant_expression_p;
5963 bool saved_greater_than_is_operator_p;
5964
5965 cp_lexer_consume_token (parser->lexer);
5966 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5967
5968 saved_message = parser->type_definition_forbidden_message;
5969 parser->type_definition_forbidden_message
5970 = G_("types may not be defined in %<noexcept%> expressions");
5971
5972 saved_integral_constant_expression_p
5973 = parser->integral_constant_expression_p;
5974 saved_non_integral_constant_expression_p
5975 = parser->non_integral_constant_expression_p;
5976 parser->integral_constant_expression_p = false;
5977
5978 saved_greater_than_is_operator_p
5979 = parser->greater_than_is_operator_p;
5980 parser->greater_than_is_operator_p = true;
5981
5982 ++cp_unevaluated_operand;
5983 ++c_inhibit_evaluation_warnings;
5984 expr = cp_parser_expression (parser, false, NULL);
5985 --c_inhibit_evaluation_warnings;
5986 --cp_unevaluated_operand;
5987
5988 parser->greater_than_is_operator_p
5989 = saved_greater_than_is_operator_p;
5990
5991 parser->integral_constant_expression_p
5992 = saved_integral_constant_expression_p;
5993 parser->non_integral_constant_expression_p
5994 = saved_non_integral_constant_expression_p;
5995
5996 parser->type_definition_forbidden_message = saved_message;
5997
5998 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5999 return finish_noexcept_expr (expr, tf_warning_or_error);
6000 }
6001
6002 default:
6003 break;
6004 }
6005 }
6006
6007 /* Look for the `:: new' and `:: delete', which also signal the
6008 beginning of a new-expression, or delete-expression,
6009 respectively. If the next token is `::', then it might be one of
6010 these. */
6011 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6012 {
6013 enum rid keyword;
6014
6015 /* See if the token after the `::' is one of the keywords in
6016 which we're interested. */
6017 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6018 /* If it's `new', we have a new-expression. */
6019 if (keyword == RID_NEW)
6020 return cp_parser_new_expression (parser);
6021 /* Similarly, for `delete'. */
6022 else if (keyword == RID_DELETE)
6023 return cp_parser_delete_expression (parser);
6024 }
6025
6026 /* Look for a unary operator. */
6027 unary_operator = cp_parser_unary_operator (token);
6028 /* The `++' and `--' operators can be handled similarly, even though
6029 they are not technically unary-operators in the grammar. */
6030 if (unary_operator == ERROR_MARK)
6031 {
6032 if (token->type == CPP_PLUS_PLUS)
6033 unary_operator = PREINCREMENT_EXPR;
6034 else if (token->type == CPP_MINUS_MINUS)
6035 unary_operator = PREDECREMENT_EXPR;
6036 /* Handle the GNU address-of-label extension. */
6037 else if (cp_parser_allow_gnu_extensions_p (parser)
6038 && token->type == CPP_AND_AND)
6039 {
6040 tree identifier;
6041 tree expression;
6042 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6043
6044 /* Consume the '&&' token. */
6045 cp_lexer_consume_token (parser->lexer);
6046 /* Look for the identifier. */
6047 identifier = cp_parser_identifier (parser);
6048 /* Create an expression representing the address. */
6049 expression = finish_label_address_expr (identifier, loc);
6050 if (cp_parser_non_integral_constant_expression (parser,
6051 NIC_ADDR_LABEL))
6052 expression = error_mark_node;
6053 return expression;
6054 }
6055 }
6056 if (unary_operator != ERROR_MARK)
6057 {
6058 tree cast_expression;
6059 tree expression = error_mark_node;
6060 non_integral_constant non_constant_p = NIC_NONE;
6061
6062 /* Consume the operator token. */
6063 token = cp_lexer_consume_token (parser->lexer);
6064 /* Parse the cast-expression. */
6065 cast_expression
6066 = cp_parser_cast_expression (parser,
6067 unary_operator == ADDR_EXPR,
6068 /*cast_p=*/false, pidk);
6069 /* Now, build an appropriate representation. */
6070 switch (unary_operator)
6071 {
6072 case INDIRECT_REF:
6073 non_constant_p = NIC_STAR;
6074 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6075 tf_warning_or_error);
6076 break;
6077
6078 case ADDR_EXPR:
6079 non_constant_p = NIC_ADDR;
6080 /* Fall through. */
6081 case BIT_NOT_EXPR:
6082 expression = build_x_unary_op (unary_operator, cast_expression,
6083 tf_warning_or_error);
6084 break;
6085
6086 case PREINCREMENT_EXPR:
6087 case PREDECREMENT_EXPR:
6088 non_constant_p = unary_operator == PREINCREMENT_EXPR
6089 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6090 /* Fall through. */
6091 case UNARY_PLUS_EXPR:
6092 case NEGATE_EXPR:
6093 case TRUTH_NOT_EXPR:
6094 expression = finish_unary_op_expr (unary_operator, cast_expression);
6095 break;
6096
6097 default:
6098 gcc_unreachable ();
6099 }
6100
6101 if (non_constant_p != NIC_NONE
6102 && cp_parser_non_integral_constant_expression (parser,
6103 non_constant_p))
6104 expression = error_mark_node;
6105
6106 return expression;
6107 }
6108
6109 return cp_parser_postfix_expression (parser, address_p, cast_p,
6110 /*member_access_only_p=*/false,
6111 pidk);
6112 }
6113
6114 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6115 unary-operator, the corresponding tree code is returned. */
6116
6117 static enum tree_code
6118 cp_parser_unary_operator (cp_token* token)
6119 {
6120 switch (token->type)
6121 {
6122 case CPP_MULT:
6123 return INDIRECT_REF;
6124
6125 case CPP_AND:
6126 return ADDR_EXPR;
6127
6128 case CPP_PLUS:
6129 return UNARY_PLUS_EXPR;
6130
6131 case CPP_MINUS:
6132 return NEGATE_EXPR;
6133
6134 case CPP_NOT:
6135 return TRUTH_NOT_EXPR;
6136
6137 case CPP_COMPL:
6138 return BIT_NOT_EXPR;
6139
6140 default:
6141 return ERROR_MARK;
6142 }
6143 }
6144
6145 /* Parse a new-expression.
6146
6147 new-expression:
6148 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6149 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6150
6151 Returns a representation of the expression. */
6152
6153 static tree
6154 cp_parser_new_expression (cp_parser* parser)
6155 {
6156 bool global_scope_p;
6157 VEC(tree,gc) *placement;
6158 tree type;
6159 VEC(tree,gc) *initializer;
6160 tree nelts;
6161 tree ret;
6162
6163 /* Look for the optional `::' operator. */
6164 global_scope_p
6165 = (cp_parser_global_scope_opt (parser,
6166 /*current_scope_valid_p=*/false)
6167 != NULL_TREE);
6168 /* Look for the `new' operator. */
6169 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6170 /* There's no easy way to tell a new-placement from the
6171 `( type-id )' construct. */
6172 cp_parser_parse_tentatively (parser);
6173 /* Look for a new-placement. */
6174 placement = cp_parser_new_placement (parser);
6175 /* If that didn't work out, there's no new-placement. */
6176 if (!cp_parser_parse_definitely (parser))
6177 {
6178 if (placement != NULL)
6179 release_tree_vector (placement);
6180 placement = NULL;
6181 }
6182
6183 /* If the next token is a `(', then we have a parenthesized
6184 type-id. */
6185 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6186 {
6187 cp_token *token;
6188 /* Consume the `('. */
6189 cp_lexer_consume_token (parser->lexer);
6190 /* Parse the type-id. */
6191 type = cp_parser_type_id (parser);
6192 /* Look for the closing `)'. */
6193 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6194 token = cp_lexer_peek_token (parser->lexer);
6195 /* There should not be a direct-new-declarator in this production,
6196 but GCC used to allowed this, so we check and emit a sensible error
6197 message for this case. */
6198 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6199 {
6200 error_at (token->location,
6201 "array bound forbidden after parenthesized type-id");
6202 inform (token->location,
6203 "try removing the parentheses around the type-id");
6204 cp_parser_direct_new_declarator (parser);
6205 }
6206 nelts = NULL_TREE;
6207 }
6208 /* Otherwise, there must be a new-type-id. */
6209 else
6210 type = cp_parser_new_type_id (parser, &nelts);
6211
6212 /* If the next token is a `(' or '{', then we have a new-initializer. */
6213 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6214 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6215 initializer = cp_parser_new_initializer (parser);
6216 else
6217 initializer = NULL;
6218
6219 /* A new-expression may not appear in an integral constant
6220 expression. */
6221 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6222 ret = error_mark_node;
6223 else
6224 {
6225 /* Create a representation of the new-expression. */
6226 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6227 tf_warning_or_error);
6228 }
6229
6230 if (placement != NULL)
6231 release_tree_vector (placement);
6232 if (initializer != NULL)
6233 release_tree_vector (initializer);
6234
6235 return ret;
6236 }
6237
6238 /* Parse a new-placement.
6239
6240 new-placement:
6241 ( expression-list )
6242
6243 Returns the same representation as for an expression-list. */
6244
6245 static VEC(tree,gc) *
6246 cp_parser_new_placement (cp_parser* parser)
6247 {
6248 VEC(tree,gc) *expression_list;
6249
6250 /* Parse the expression-list. */
6251 expression_list = (cp_parser_parenthesized_expression_list
6252 (parser, non_attr, /*cast_p=*/false,
6253 /*allow_expansion_p=*/true,
6254 /*non_constant_p=*/NULL));
6255
6256 return expression_list;
6257 }
6258
6259 /* Parse a new-type-id.
6260
6261 new-type-id:
6262 type-specifier-seq new-declarator [opt]
6263
6264 Returns the TYPE allocated. If the new-type-id indicates an array
6265 type, *NELTS is set to the number of elements in the last array
6266 bound; the TYPE will not include the last array bound. */
6267
6268 static tree
6269 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6270 {
6271 cp_decl_specifier_seq type_specifier_seq;
6272 cp_declarator *new_declarator;
6273 cp_declarator *declarator;
6274 cp_declarator *outer_declarator;
6275 const char *saved_message;
6276 tree type;
6277
6278 /* The type-specifier sequence must not contain type definitions.
6279 (It cannot contain declarations of new types either, but if they
6280 are not definitions we will catch that because they are not
6281 complete.) */
6282 saved_message = parser->type_definition_forbidden_message;
6283 parser->type_definition_forbidden_message
6284 = G_("types may not be defined in a new-type-id");
6285 /* Parse the type-specifier-seq. */
6286 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6287 /*is_trailing_return=*/false,
6288 &type_specifier_seq);
6289 /* Restore the old message. */
6290 parser->type_definition_forbidden_message = saved_message;
6291 /* Parse the new-declarator. */
6292 new_declarator = cp_parser_new_declarator_opt (parser);
6293
6294 /* Determine the number of elements in the last array dimension, if
6295 any. */
6296 *nelts = NULL_TREE;
6297 /* Skip down to the last array dimension. */
6298 declarator = new_declarator;
6299 outer_declarator = NULL;
6300 while (declarator && (declarator->kind == cdk_pointer
6301 || declarator->kind == cdk_ptrmem))
6302 {
6303 outer_declarator = declarator;
6304 declarator = declarator->declarator;
6305 }
6306 while (declarator
6307 && declarator->kind == cdk_array
6308 && declarator->declarator
6309 && declarator->declarator->kind == cdk_array)
6310 {
6311 outer_declarator = declarator;
6312 declarator = declarator->declarator;
6313 }
6314
6315 if (declarator && declarator->kind == cdk_array)
6316 {
6317 *nelts = declarator->u.array.bounds;
6318 if (*nelts == error_mark_node)
6319 *nelts = integer_one_node;
6320
6321 if (outer_declarator)
6322 outer_declarator->declarator = declarator->declarator;
6323 else
6324 new_declarator = NULL;
6325 }
6326
6327 type = groktypename (&type_specifier_seq, new_declarator, false);
6328 return type;
6329 }
6330
6331 /* Parse an (optional) new-declarator.
6332
6333 new-declarator:
6334 ptr-operator new-declarator [opt]
6335 direct-new-declarator
6336
6337 Returns the declarator. */
6338
6339 static cp_declarator *
6340 cp_parser_new_declarator_opt (cp_parser* parser)
6341 {
6342 enum tree_code code;
6343 tree type;
6344 cp_cv_quals cv_quals;
6345
6346 /* We don't know if there's a ptr-operator next, or not. */
6347 cp_parser_parse_tentatively (parser);
6348 /* Look for a ptr-operator. */
6349 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6350 /* If that worked, look for more new-declarators. */
6351 if (cp_parser_parse_definitely (parser))
6352 {
6353 cp_declarator *declarator;
6354
6355 /* Parse another optional declarator. */
6356 declarator = cp_parser_new_declarator_opt (parser);
6357
6358 return cp_parser_make_indirect_declarator
6359 (code, type, cv_quals, declarator);
6360 }
6361
6362 /* If the next token is a `[', there is a direct-new-declarator. */
6363 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6364 return cp_parser_direct_new_declarator (parser);
6365
6366 return NULL;
6367 }
6368
6369 /* Parse a direct-new-declarator.
6370
6371 direct-new-declarator:
6372 [ expression ]
6373 direct-new-declarator [constant-expression]
6374
6375 */
6376
6377 static cp_declarator *
6378 cp_parser_direct_new_declarator (cp_parser* parser)
6379 {
6380 cp_declarator *declarator = NULL;
6381
6382 while (true)
6383 {
6384 tree expression;
6385
6386 /* Look for the opening `['. */
6387 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6388 /* The first expression is not required to be constant. */
6389 if (!declarator)
6390 {
6391 cp_token *token = cp_lexer_peek_token (parser->lexer);
6392 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6393 /* The standard requires that the expression have integral
6394 type. DR 74 adds enumeration types. We believe that the
6395 real intent is that these expressions be handled like the
6396 expression in a `switch' condition, which also allows
6397 classes with a single conversion to integral or
6398 enumeration type. */
6399 if (!processing_template_decl)
6400 {
6401 expression
6402 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6403 expression,
6404 /*complain=*/true);
6405 if (!expression)
6406 {
6407 error_at (token->location,
6408 "expression in new-declarator must have integral "
6409 "or enumeration type");
6410 expression = error_mark_node;
6411 }
6412 }
6413 }
6414 /* But all the other expressions must be. */
6415 else
6416 expression
6417 = cp_parser_constant_expression (parser,
6418 /*allow_non_constant=*/false,
6419 NULL);
6420 /* Look for the closing `]'. */
6421 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6422
6423 /* Add this bound to the declarator. */
6424 declarator = make_array_declarator (declarator, expression);
6425
6426 /* If the next token is not a `[', then there are no more
6427 bounds. */
6428 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6429 break;
6430 }
6431
6432 return declarator;
6433 }
6434
6435 /* Parse a new-initializer.
6436
6437 new-initializer:
6438 ( expression-list [opt] )
6439 braced-init-list
6440
6441 Returns a representation of the expression-list. */
6442
6443 static VEC(tree,gc) *
6444 cp_parser_new_initializer (cp_parser* parser)
6445 {
6446 VEC(tree,gc) *expression_list;
6447
6448 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6449 {
6450 tree t;
6451 bool expr_non_constant_p;
6452 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6453 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6454 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6455 expression_list = make_tree_vector_single (t);
6456 }
6457 else
6458 expression_list = (cp_parser_parenthesized_expression_list
6459 (parser, non_attr, /*cast_p=*/false,
6460 /*allow_expansion_p=*/true,
6461 /*non_constant_p=*/NULL));
6462
6463 return expression_list;
6464 }
6465
6466 /* Parse a delete-expression.
6467
6468 delete-expression:
6469 :: [opt] delete cast-expression
6470 :: [opt] delete [ ] cast-expression
6471
6472 Returns a representation of the expression. */
6473
6474 static tree
6475 cp_parser_delete_expression (cp_parser* parser)
6476 {
6477 bool global_scope_p;
6478 bool array_p;
6479 tree expression;
6480
6481 /* Look for the optional `::' operator. */
6482 global_scope_p
6483 = (cp_parser_global_scope_opt (parser,
6484 /*current_scope_valid_p=*/false)
6485 != NULL_TREE);
6486 /* Look for the `delete' keyword. */
6487 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6488 /* See if the array syntax is in use. */
6489 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6490 {
6491 /* Consume the `[' token. */
6492 cp_lexer_consume_token (parser->lexer);
6493 /* Look for the `]' token. */
6494 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6495 /* Remember that this is the `[]' construct. */
6496 array_p = true;
6497 }
6498 else
6499 array_p = false;
6500
6501 /* Parse the cast-expression. */
6502 expression = cp_parser_simple_cast_expression (parser);
6503
6504 /* A delete-expression may not appear in an integral constant
6505 expression. */
6506 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6507 return error_mark_node;
6508
6509 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6510 }
6511
6512 /* Returns true if TOKEN may start a cast-expression and false
6513 otherwise. */
6514
6515 static bool
6516 cp_parser_token_starts_cast_expression (cp_token *token)
6517 {
6518 switch (token->type)
6519 {
6520 case CPP_COMMA:
6521 case CPP_SEMICOLON:
6522 case CPP_QUERY:
6523 case CPP_COLON:
6524 case CPP_CLOSE_SQUARE:
6525 case CPP_CLOSE_PAREN:
6526 case CPP_CLOSE_BRACE:
6527 case CPP_DOT:
6528 case CPP_DOT_STAR:
6529 case CPP_DEREF:
6530 case CPP_DEREF_STAR:
6531 case CPP_DIV:
6532 case CPP_MOD:
6533 case CPP_LSHIFT:
6534 case CPP_RSHIFT:
6535 case CPP_LESS:
6536 case CPP_GREATER:
6537 case CPP_LESS_EQ:
6538 case CPP_GREATER_EQ:
6539 case CPP_EQ_EQ:
6540 case CPP_NOT_EQ:
6541 case CPP_EQ:
6542 case CPP_MULT_EQ:
6543 case CPP_DIV_EQ:
6544 case CPP_MOD_EQ:
6545 case CPP_PLUS_EQ:
6546 case CPP_MINUS_EQ:
6547 case CPP_RSHIFT_EQ:
6548 case CPP_LSHIFT_EQ:
6549 case CPP_AND_EQ:
6550 case CPP_XOR_EQ:
6551 case CPP_OR_EQ:
6552 case CPP_XOR:
6553 case CPP_OR:
6554 case CPP_OR_OR:
6555 case CPP_EOF:
6556 return false;
6557
6558 /* '[' may start a primary-expression in obj-c++. */
6559 case CPP_OPEN_SQUARE:
6560 return c_dialect_objc ();
6561
6562 default:
6563 return true;
6564 }
6565 }
6566
6567 /* Parse a cast-expression.
6568
6569 cast-expression:
6570 unary-expression
6571 ( type-id ) cast-expression
6572
6573 ADDRESS_P is true iff the unary-expression is appearing as the
6574 operand of the `&' operator. CAST_P is true if this expression is
6575 the target of a cast.
6576
6577 Returns a representation of the expression. */
6578
6579 static tree
6580 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6581 cp_id_kind * pidk)
6582 {
6583 /* If it's a `(', then we might be looking at a cast. */
6584 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6585 {
6586 tree type = NULL_TREE;
6587 tree expr = NULL_TREE;
6588 bool compound_literal_p;
6589 const char *saved_message;
6590
6591 /* There's no way to know yet whether or not this is a cast.
6592 For example, `(int (3))' is a unary-expression, while `(int)
6593 3' is a cast. So, we resort to parsing tentatively. */
6594 cp_parser_parse_tentatively (parser);
6595 /* Types may not be defined in a cast. */
6596 saved_message = parser->type_definition_forbidden_message;
6597 parser->type_definition_forbidden_message
6598 = G_("types may not be defined in casts");
6599 /* Consume the `('. */
6600 cp_lexer_consume_token (parser->lexer);
6601 /* A very tricky bit is that `(struct S) { 3 }' is a
6602 compound-literal (which we permit in C++ as an extension).
6603 But, that construct is not a cast-expression -- it is a
6604 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6605 is legal; if the compound-literal were a cast-expression,
6606 you'd need an extra set of parentheses.) But, if we parse
6607 the type-id, and it happens to be a class-specifier, then we
6608 will commit to the parse at that point, because we cannot
6609 undo the action that is done when creating a new class. So,
6610 then we cannot back up and do a postfix-expression.
6611
6612 Therefore, we scan ahead to the closing `)', and check to see
6613 if the token after the `)' is a `{'. If so, we are not
6614 looking at a cast-expression.
6615
6616 Save tokens so that we can put them back. */
6617 cp_lexer_save_tokens (parser->lexer);
6618 /* Skip tokens until the next token is a closing parenthesis.
6619 If we find the closing `)', and the next token is a `{', then
6620 we are looking at a compound-literal. */
6621 compound_literal_p
6622 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6623 /*consume_paren=*/true)
6624 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6625 /* Roll back the tokens we skipped. */
6626 cp_lexer_rollback_tokens (parser->lexer);
6627 /* If we were looking at a compound-literal, simulate an error
6628 so that the call to cp_parser_parse_definitely below will
6629 fail. */
6630 if (compound_literal_p)
6631 cp_parser_simulate_error (parser);
6632 else
6633 {
6634 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6635 parser->in_type_id_in_expr_p = true;
6636 /* Look for the type-id. */
6637 type = cp_parser_type_id (parser);
6638 /* Look for the closing `)'. */
6639 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6640 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6641 }
6642
6643 /* Restore the saved message. */
6644 parser->type_definition_forbidden_message = saved_message;
6645
6646 /* At this point this can only be either a cast or a
6647 parenthesized ctor such as `(T ())' that looks like a cast to
6648 function returning T. */
6649 if (!cp_parser_error_occurred (parser)
6650 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6651 (parser->lexer)))
6652 {
6653 cp_parser_parse_definitely (parser);
6654 expr = cp_parser_cast_expression (parser,
6655 /*address_p=*/false,
6656 /*cast_p=*/true, pidk);
6657
6658 /* Warn about old-style casts, if so requested. */
6659 if (warn_old_style_cast
6660 && !in_system_header
6661 && !VOID_TYPE_P (type)
6662 && current_lang_name != lang_name_c)
6663 warning (OPT_Wold_style_cast, "use of old-style cast");
6664
6665 /* Only type conversions to integral or enumeration types
6666 can be used in constant-expressions. */
6667 if (!cast_valid_in_integral_constant_expression_p (type)
6668 && cp_parser_non_integral_constant_expression (parser,
6669 NIC_CAST))
6670 return error_mark_node;
6671
6672 /* Perform the cast. */
6673 expr = build_c_cast (input_location, type, expr);
6674 return expr;
6675 }
6676 else
6677 cp_parser_abort_tentative_parse (parser);
6678 }
6679
6680 /* If we get here, then it's not a cast, so it must be a
6681 unary-expression. */
6682 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6683 }
6684
6685 /* Parse a binary expression of the general form:
6686
6687 pm-expression:
6688 cast-expression
6689 pm-expression .* cast-expression
6690 pm-expression ->* cast-expression
6691
6692 multiplicative-expression:
6693 pm-expression
6694 multiplicative-expression * pm-expression
6695 multiplicative-expression / pm-expression
6696 multiplicative-expression % pm-expression
6697
6698 additive-expression:
6699 multiplicative-expression
6700 additive-expression + multiplicative-expression
6701 additive-expression - multiplicative-expression
6702
6703 shift-expression:
6704 additive-expression
6705 shift-expression << additive-expression
6706 shift-expression >> additive-expression
6707
6708 relational-expression:
6709 shift-expression
6710 relational-expression < shift-expression
6711 relational-expression > shift-expression
6712 relational-expression <= shift-expression
6713 relational-expression >= shift-expression
6714
6715 GNU Extension:
6716
6717 relational-expression:
6718 relational-expression <? shift-expression
6719 relational-expression >? shift-expression
6720
6721 equality-expression:
6722 relational-expression
6723 equality-expression == relational-expression
6724 equality-expression != relational-expression
6725
6726 and-expression:
6727 equality-expression
6728 and-expression & equality-expression
6729
6730 exclusive-or-expression:
6731 and-expression
6732 exclusive-or-expression ^ and-expression
6733
6734 inclusive-or-expression:
6735 exclusive-or-expression
6736 inclusive-or-expression | exclusive-or-expression
6737
6738 logical-and-expression:
6739 inclusive-or-expression
6740 logical-and-expression && inclusive-or-expression
6741
6742 logical-or-expression:
6743 logical-and-expression
6744 logical-or-expression || logical-and-expression
6745
6746 All these are implemented with a single function like:
6747
6748 binary-expression:
6749 simple-cast-expression
6750 binary-expression <token> binary-expression
6751
6752 CAST_P is true if this expression is the target of a cast.
6753
6754 The binops_by_token map is used to get the tree codes for each <token> type.
6755 binary-expressions are associated according to a precedence table. */
6756
6757 #define TOKEN_PRECEDENCE(token) \
6758 (((token->type == CPP_GREATER \
6759 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6760 && !parser->greater_than_is_operator_p) \
6761 ? PREC_NOT_OPERATOR \
6762 : binops_by_token[token->type].prec)
6763
6764 static tree
6765 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6766 bool no_toplevel_fold_p,
6767 enum cp_parser_prec prec,
6768 cp_id_kind * pidk)
6769 {
6770 cp_parser_expression_stack stack;
6771 cp_parser_expression_stack_entry *sp = &stack[0];
6772 tree lhs, rhs;
6773 cp_token *token;
6774 enum tree_code tree_type, lhs_type, rhs_type;
6775 enum cp_parser_prec new_prec, lookahead_prec;
6776 bool overloaded_p;
6777
6778 /* Parse the first expression. */
6779 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6780 lhs_type = ERROR_MARK;
6781
6782 for (;;)
6783 {
6784 /* Get an operator token. */
6785 token = cp_lexer_peek_token (parser->lexer);
6786
6787 if (warn_cxx0x_compat
6788 && token->type == CPP_RSHIFT
6789 && !parser->greater_than_is_operator_p)
6790 {
6791 if (warning_at (token->location, OPT_Wc__0x_compat,
6792 "%<>>%> operator will be treated as"
6793 " two right angle brackets in C++0x"))
6794 inform (token->location,
6795 "suggest parentheses around %<>>%> expression");
6796 }
6797
6798 new_prec = TOKEN_PRECEDENCE (token);
6799
6800 /* Popping an entry off the stack means we completed a subexpression:
6801 - either we found a token which is not an operator (`>' where it is not
6802 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6803 will happen repeatedly;
6804 - or, we found an operator which has lower priority. This is the case
6805 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6806 parsing `3 * 4'. */
6807 if (new_prec <= prec)
6808 {
6809 if (sp == stack)
6810 break;
6811 else
6812 goto pop;
6813 }
6814
6815 get_rhs:
6816 tree_type = binops_by_token[token->type].tree_type;
6817
6818 /* We used the operator token. */
6819 cp_lexer_consume_token (parser->lexer);
6820
6821 /* For "false && x" or "true || x", x will never be executed;
6822 disable warnings while evaluating it. */
6823 if (tree_type == TRUTH_ANDIF_EXPR)
6824 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6825 else if (tree_type == TRUTH_ORIF_EXPR)
6826 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6827
6828 /* Extract another operand. It may be the RHS of this expression
6829 or the LHS of a new, higher priority expression. */
6830 rhs = cp_parser_simple_cast_expression (parser);
6831 rhs_type = ERROR_MARK;
6832
6833 /* Get another operator token. Look up its precedence to avoid
6834 building a useless (immediately popped) stack entry for common
6835 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6836 token = cp_lexer_peek_token (parser->lexer);
6837 lookahead_prec = TOKEN_PRECEDENCE (token);
6838 if (lookahead_prec > new_prec)
6839 {
6840 /* ... and prepare to parse the RHS of the new, higher priority
6841 expression. Since precedence levels on the stack are
6842 monotonically increasing, we do not have to care about
6843 stack overflows. */
6844 sp->prec = prec;
6845 sp->tree_type = tree_type;
6846 sp->lhs = lhs;
6847 sp->lhs_type = lhs_type;
6848 sp++;
6849 lhs = rhs;
6850 lhs_type = rhs_type;
6851 prec = new_prec;
6852 new_prec = lookahead_prec;
6853 goto get_rhs;
6854
6855 pop:
6856 lookahead_prec = new_prec;
6857 /* If the stack is not empty, we have parsed into LHS the right side
6858 (`4' in the example above) of an expression we had suspended.
6859 We can use the information on the stack to recover the LHS (`3')
6860 from the stack together with the tree code (`MULT_EXPR'), and
6861 the precedence of the higher level subexpression
6862 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6863 which will be used to actually build the additive expression. */
6864 --sp;
6865 prec = sp->prec;
6866 tree_type = sp->tree_type;
6867 rhs = lhs;
6868 rhs_type = lhs_type;
6869 lhs = sp->lhs;
6870 lhs_type = sp->lhs_type;
6871 }
6872
6873 /* Undo the disabling of warnings done above. */
6874 if (tree_type == TRUTH_ANDIF_EXPR)
6875 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6876 else if (tree_type == TRUTH_ORIF_EXPR)
6877 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6878
6879 overloaded_p = false;
6880 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6881 ERROR_MARK for everything that is not a binary expression.
6882 This makes warn_about_parentheses miss some warnings that
6883 involve unary operators. For unary expressions we should
6884 pass the correct tree_code unless the unary expression was
6885 surrounded by parentheses.
6886 */
6887 if (no_toplevel_fold_p
6888 && lookahead_prec <= prec
6889 && sp == stack
6890 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6891 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6892 else
6893 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6894 &overloaded_p, tf_warning_or_error);
6895 lhs_type = tree_type;
6896
6897 /* If the binary operator required the use of an overloaded operator,
6898 then this expression cannot be an integral constant-expression.
6899 An overloaded operator can be used even if both operands are
6900 otherwise permissible in an integral constant-expression if at
6901 least one of the operands is of enumeration type. */
6902
6903 if (overloaded_p
6904 && cp_parser_non_integral_constant_expression (parser,
6905 NIC_OVERLOADED))
6906 return error_mark_node;
6907 }
6908
6909 return lhs;
6910 }
6911
6912
6913 /* Parse the `? expression : assignment-expression' part of a
6914 conditional-expression. The LOGICAL_OR_EXPR is the
6915 logical-or-expression that started the conditional-expression.
6916 Returns a representation of the entire conditional-expression.
6917
6918 This routine is used by cp_parser_assignment_expression.
6919
6920 ? expression : assignment-expression
6921
6922 GNU Extensions:
6923
6924 ? : assignment-expression */
6925
6926 static tree
6927 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6928 {
6929 tree expr;
6930 tree assignment_expr;
6931 struct cp_token *token;
6932
6933 /* Consume the `?' token. */
6934 cp_lexer_consume_token (parser->lexer);
6935 token = cp_lexer_peek_token (parser->lexer);
6936 if (cp_parser_allow_gnu_extensions_p (parser)
6937 && token->type == CPP_COLON)
6938 {
6939 pedwarn (token->location, OPT_pedantic,
6940 "ISO C++ does not allow ?: with omitted middle operand");
6941 /* Implicit true clause. */
6942 expr = NULL_TREE;
6943 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6944 warn_for_omitted_condop (token->location, logical_or_expr);
6945 }
6946 else
6947 {
6948 /* Parse the expression. */
6949 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6950 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6951 c_inhibit_evaluation_warnings +=
6952 ((logical_or_expr == truthvalue_true_node)
6953 - (logical_or_expr == truthvalue_false_node));
6954 }
6955
6956 /* The next token should be a `:'. */
6957 cp_parser_require (parser, CPP_COLON, RT_COLON);
6958 /* Parse the assignment-expression. */
6959 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6960 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6961
6962 /* Build the conditional-expression. */
6963 return build_x_conditional_expr (logical_or_expr,
6964 expr,
6965 assignment_expr,
6966 tf_warning_or_error);
6967 }
6968
6969 /* Parse an assignment-expression.
6970
6971 assignment-expression:
6972 conditional-expression
6973 logical-or-expression assignment-operator assignment_expression
6974 throw-expression
6975
6976 CAST_P is true if this expression is the target of a cast.
6977
6978 Returns a representation for the expression. */
6979
6980 static tree
6981 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6982 cp_id_kind * pidk)
6983 {
6984 tree expr;
6985
6986 /* If the next token is the `throw' keyword, then we're looking at
6987 a throw-expression. */
6988 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6989 expr = cp_parser_throw_expression (parser);
6990 /* Otherwise, it must be that we are looking at a
6991 logical-or-expression. */
6992 else
6993 {
6994 /* Parse the binary expressions (logical-or-expression). */
6995 expr = cp_parser_binary_expression (parser, cast_p, false,
6996 PREC_NOT_OPERATOR, pidk);
6997 /* If the next token is a `?' then we're actually looking at a
6998 conditional-expression. */
6999 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7000 return cp_parser_question_colon_clause (parser, expr);
7001 else
7002 {
7003 enum tree_code assignment_operator;
7004
7005 /* If it's an assignment-operator, we're using the second
7006 production. */
7007 assignment_operator
7008 = cp_parser_assignment_operator_opt (parser);
7009 if (assignment_operator != ERROR_MARK)
7010 {
7011 bool non_constant_p;
7012
7013 /* Parse the right-hand side of the assignment. */
7014 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7015
7016 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7017 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7018
7019 /* An assignment may not appear in a
7020 constant-expression. */
7021 if (cp_parser_non_integral_constant_expression (parser,
7022 NIC_ASSIGNMENT))
7023 return error_mark_node;
7024 /* Build the assignment expression. */
7025 expr = build_x_modify_expr (expr,
7026 assignment_operator,
7027 rhs,
7028 tf_warning_or_error);
7029 }
7030 }
7031 }
7032
7033 return expr;
7034 }
7035
7036 /* Parse an (optional) assignment-operator.
7037
7038 assignment-operator: one of
7039 = *= /= %= += -= >>= <<= &= ^= |=
7040
7041 GNU Extension:
7042
7043 assignment-operator: one of
7044 <?= >?=
7045
7046 If the next token is an assignment operator, the corresponding tree
7047 code is returned, and the token is consumed. For example, for
7048 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7049 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7050 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7051 operator, ERROR_MARK is returned. */
7052
7053 static enum tree_code
7054 cp_parser_assignment_operator_opt (cp_parser* parser)
7055 {
7056 enum tree_code op;
7057 cp_token *token;
7058
7059 /* Peek at the next token. */
7060 token = cp_lexer_peek_token (parser->lexer);
7061
7062 switch (token->type)
7063 {
7064 case CPP_EQ:
7065 op = NOP_EXPR;
7066 break;
7067
7068 case CPP_MULT_EQ:
7069 op = MULT_EXPR;
7070 break;
7071
7072 case CPP_DIV_EQ:
7073 op = TRUNC_DIV_EXPR;
7074 break;
7075
7076 case CPP_MOD_EQ:
7077 op = TRUNC_MOD_EXPR;
7078 break;
7079
7080 case CPP_PLUS_EQ:
7081 op = PLUS_EXPR;
7082 break;
7083
7084 case CPP_MINUS_EQ:
7085 op = MINUS_EXPR;
7086 break;
7087
7088 case CPP_RSHIFT_EQ:
7089 op = RSHIFT_EXPR;
7090 break;
7091
7092 case CPP_LSHIFT_EQ:
7093 op = LSHIFT_EXPR;
7094 break;
7095
7096 case CPP_AND_EQ:
7097 op = BIT_AND_EXPR;
7098 break;
7099
7100 case CPP_XOR_EQ:
7101 op = BIT_XOR_EXPR;
7102 break;
7103
7104 case CPP_OR_EQ:
7105 op = BIT_IOR_EXPR;
7106 break;
7107
7108 default:
7109 /* Nothing else is an assignment operator. */
7110 op = ERROR_MARK;
7111 }
7112
7113 /* If it was an assignment operator, consume it. */
7114 if (op != ERROR_MARK)
7115 cp_lexer_consume_token (parser->lexer);
7116
7117 return op;
7118 }
7119
7120 /* Parse an expression.
7121
7122 expression:
7123 assignment-expression
7124 expression , assignment-expression
7125
7126 CAST_P is true if this expression is the target of a cast.
7127
7128 Returns a representation of the expression. */
7129
7130 static tree
7131 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7132 {
7133 tree expression = NULL_TREE;
7134
7135 while (true)
7136 {
7137 tree assignment_expression;
7138
7139 /* Parse the next assignment-expression. */
7140 assignment_expression
7141 = cp_parser_assignment_expression (parser, cast_p, pidk);
7142 /* If this is the first assignment-expression, we can just
7143 save it away. */
7144 if (!expression)
7145 expression = assignment_expression;
7146 else
7147 expression = build_x_compound_expr (expression,
7148 assignment_expression,
7149 tf_warning_or_error);
7150 /* If the next token is not a comma, then we are done with the
7151 expression. */
7152 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7153 break;
7154 /* Consume the `,'. */
7155 cp_lexer_consume_token (parser->lexer);
7156 /* A comma operator cannot appear in a constant-expression. */
7157 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7158 expression = error_mark_node;
7159 }
7160
7161 return expression;
7162 }
7163
7164 /* Parse a constant-expression.
7165
7166 constant-expression:
7167 conditional-expression
7168
7169 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7170 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7171 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7172 is false, NON_CONSTANT_P should be NULL. */
7173
7174 static tree
7175 cp_parser_constant_expression (cp_parser* parser,
7176 bool allow_non_constant_p,
7177 bool *non_constant_p)
7178 {
7179 bool saved_integral_constant_expression_p;
7180 bool saved_allow_non_integral_constant_expression_p;
7181 bool saved_non_integral_constant_expression_p;
7182 tree expression;
7183
7184 /* It might seem that we could simply parse the
7185 conditional-expression, and then check to see if it were
7186 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7187 one that the compiler can figure out is constant, possibly after
7188 doing some simplifications or optimizations. The standard has a
7189 precise definition of constant-expression, and we must honor
7190 that, even though it is somewhat more restrictive.
7191
7192 For example:
7193
7194 int i[(2, 3)];
7195
7196 is not a legal declaration, because `(2, 3)' is not a
7197 constant-expression. The `,' operator is forbidden in a
7198 constant-expression. However, GCC's constant-folding machinery
7199 will fold this operation to an INTEGER_CST for `3'. */
7200
7201 /* Save the old settings. */
7202 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7203 saved_allow_non_integral_constant_expression_p
7204 = parser->allow_non_integral_constant_expression_p;
7205 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7206 /* We are now parsing a constant-expression. */
7207 parser->integral_constant_expression_p = true;
7208 parser->allow_non_integral_constant_expression_p
7209 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7210 parser->non_integral_constant_expression_p = false;
7211 /* Although the grammar says "conditional-expression", we parse an
7212 "assignment-expression", which also permits "throw-expression"
7213 and the use of assignment operators. In the case that
7214 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7215 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7216 actually essential that we look for an assignment-expression.
7217 For example, cp_parser_initializer_clauses uses this function to
7218 determine whether a particular assignment-expression is in fact
7219 constant. */
7220 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7221 /* Restore the old settings. */
7222 parser->integral_constant_expression_p
7223 = saved_integral_constant_expression_p;
7224 parser->allow_non_integral_constant_expression_p
7225 = saved_allow_non_integral_constant_expression_p;
7226 if (allow_non_constant_p)
7227 *non_constant_p = parser->non_integral_constant_expression_p;
7228 else if (parser->non_integral_constant_expression_p
7229 && cxx_dialect < cxx0x)
7230 expression = error_mark_node;
7231 parser->non_integral_constant_expression_p
7232 = saved_non_integral_constant_expression_p;
7233
7234 return expression;
7235 }
7236
7237 /* Parse __builtin_offsetof.
7238
7239 offsetof-expression:
7240 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7241
7242 offsetof-member-designator:
7243 id-expression
7244 | offsetof-member-designator "." id-expression
7245 | offsetof-member-designator "[" expression "]"
7246 | offsetof-member-designator "->" id-expression */
7247
7248 static tree
7249 cp_parser_builtin_offsetof (cp_parser *parser)
7250 {
7251 int save_ice_p, save_non_ice_p;
7252 tree type, expr;
7253 cp_id_kind dummy;
7254 cp_token *token;
7255
7256 /* We're about to accept non-integral-constant things, but will
7257 definitely yield an integral constant expression. Save and
7258 restore these values around our local parsing. */
7259 save_ice_p = parser->integral_constant_expression_p;
7260 save_non_ice_p = parser->non_integral_constant_expression_p;
7261
7262 /* Consume the "__builtin_offsetof" token. */
7263 cp_lexer_consume_token (parser->lexer);
7264 /* Consume the opening `('. */
7265 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7266 /* Parse the type-id. */
7267 type = cp_parser_type_id (parser);
7268 /* Look for the `,'. */
7269 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7270 token = cp_lexer_peek_token (parser->lexer);
7271
7272 /* Build the (type *)null that begins the traditional offsetof macro. */
7273 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7274 tf_warning_or_error);
7275
7276 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7277 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7278 true, &dummy, token->location);
7279 while (true)
7280 {
7281 token = cp_lexer_peek_token (parser->lexer);
7282 switch (token->type)
7283 {
7284 case CPP_OPEN_SQUARE:
7285 /* offsetof-member-designator "[" expression "]" */
7286 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7287 break;
7288
7289 case CPP_DEREF:
7290 /* offsetof-member-designator "->" identifier */
7291 expr = grok_array_decl (expr, integer_zero_node);
7292 /* FALLTHRU */
7293
7294 case CPP_DOT:
7295 /* offsetof-member-designator "." identifier */
7296 cp_lexer_consume_token (parser->lexer);
7297 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7298 expr, true, &dummy,
7299 token->location);
7300 break;
7301
7302 case CPP_CLOSE_PAREN:
7303 /* Consume the ")" token. */
7304 cp_lexer_consume_token (parser->lexer);
7305 goto success;
7306
7307 default:
7308 /* Error. We know the following require will fail, but
7309 that gives the proper error message. */
7310 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7311 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7312 expr = error_mark_node;
7313 goto failure;
7314 }
7315 }
7316
7317 success:
7318 /* If we're processing a template, we can't finish the semantics yet.
7319 Otherwise we can fold the entire expression now. */
7320 if (processing_template_decl)
7321 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7322 else
7323 expr = finish_offsetof (expr);
7324
7325 failure:
7326 parser->integral_constant_expression_p = save_ice_p;
7327 parser->non_integral_constant_expression_p = save_non_ice_p;
7328
7329 return expr;
7330 }
7331
7332 /* Parse a trait expression. */
7333
7334 static tree
7335 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7336 {
7337 cp_trait_kind kind;
7338 tree type1, type2 = NULL_TREE;
7339 bool binary = false;
7340 cp_decl_specifier_seq decl_specs;
7341
7342 switch (keyword)
7343 {
7344 case RID_HAS_NOTHROW_ASSIGN:
7345 kind = CPTK_HAS_NOTHROW_ASSIGN;
7346 break;
7347 case RID_HAS_NOTHROW_CONSTRUCTOR:
7348 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7349 break;
7350 case RID_HAS_NOTHROW_COPY:
7351 kind = CPTK_HAS_NOTHROW_COPY;
7352 break;
7353 case RID_HAS_TRIVIAL_ASSIGN:
7354 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7355 break;
7356 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7357 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7358 break;
7359 case RID_HAS_TRIVIAL_COPY:
7360 kind = CPTK_HAS_TRIVIAL_COPY;
7361 break;
7362 case RID_HAS_TRIVIAL_DESTRUCTOR:
7363 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7364 break;
7365 case RID_HAS_VIRTUAL_DESTRUCTOR:
7366 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7367 break;
7368 case RID_IS_ABSTRACT:
7369 kind = CPTK_IS_ABSTRACT;
7370 break;
7371 case RID_IS_BASE_OF:
7372 kind = CPTK_IS_BASE_OF;
7373 binary = true;
7374 break;
7375 case RID_IS_CLASS:
7376 kind = CPTK_IS_CLASS;
7377 break;
7378 case RID_IS_CONVERTIBLE_TO:
7379 kind = CPTK_IS_CONVERTIBLE_TO;
7380 binary = true;
7381 break;
7382 case RID_IS_EMPTY:
7383 kind = CPTK_IS_EMPTY;
7384 break;
7385 case RID_IS_ENUM:
7386 kind = CPTK_IS_ENUM;
7387 break;
7388 case RID_IS_POD:
7389 kind = CPTK_IS_POD;
7390 break;
7391 case RID_IS_POLYMORPHIC:
7392 kind = CPTK_IS_POLYMORPHIC;
7393 break;
7394 case RID_IS_STD_LAYOUT:
7395 kind = CPTK_IS_STD_LAYOUT;
7396 break;
7397 case RID_IS_TRIVIAL:
7398 kind = CPTK_IS_TRIVIAL;
7399 break;
7400 case RID_IS_UNION:
7401 kind = CPTK_IS_UNION;
7402 break;
7403 case RID_IS_LITERAL_TYPE:
7404 kind = CPTK_IS_LITERAL_TYPE;
7405 break;
7406 default:
7407 gcc_unreachable ();
7408 }
7409
7410 /* Consume the token. */
7411 cp_lexer_consume_token (parser->lexer);
7412
7413 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7414
7415 type1 = cp_parser_type_id (parser);
7416
7417 if (type1 == error_mark_node)
7418 return error_mark_node;
7419
7420 /* Build a trivial decl-specifier-seq. */
7421 clear_decl_specs (&decl_specs);
7422 decl_specs.type = type1;
7423
7424 /* Call grokdeclarator to figure out what type this is. */
7425 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7426 /*initialized=*/0, /*attrlist=*/NULL);
7427
7428 if (binary)
7429 {
7430 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7431
7432 type2 = cp_parser_type_id (parser);
7433
7434 if (type2 == error_mark_node)
7435 return error_mark_node;
7436
7437 /* Build a trivial decl-specifier-seq. */
7438 clear_decl_specs (&decl_specs);
7439 decl_specs.type = type2;
7440
7441 /* Call grokdeclarator to figure out what type this is. */
7442 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7443 /*initialized=*/0, /*attrlist=*/NULL);
7444 }
7445
7446 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7447
7448 /* Complete the trait expression, which may mean either processing
7449 the trait expr now or saving it for template instantiation. */
7450 return finish_trait_expr (kind, type1, type2);
7451 }
7452
7453 /* Lambdas that appear in variable initializer or default argument scope
7454 get that in their mangling, so we need to record it. We might as well
7455 use the count for function and namespace scopes as well. */
7456 static GTY(()) tree lambda_scope;
7457 static GTY(()) int lambda_count;
7458 typedef struct GTY(()) tree_int
7459 {
7460 tree t;
7461 int i;
7462 } tree_int;
7463 DEF_VEC_O(tree_int);
7464 DEF_VEC_ALLOC_O(tree_int,gc);
7465 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7466
7467 static void
7468 start_lambda_scope (tree decl)
7469 {
7470 tree_int ti;
7471 gcc_assert (decl);
7472 /* Once we're inside a function, we ignore other scopes and just push
7473 the function again so that popping works properly. */
7474 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7475 decl = current_function_decl;
7476 ti.t = lambda_scope;
7477 ti.i = lambda_count;
7478 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7479 if (lambda_scope != decl)
7480 {
7481 /* Don't reset the count if we're still in the same function. */
7482 lambda_scope = decl;
7483 lambda_count = 0;
7484 }
7485 }
7486
7487 static void
7488 record_lambda_scope (tree lambda)
7489 {
7490 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7491 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7492 }
7493
7494 static void
7495 finish_lambda_scope (void)
7496 {
7497 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7498 if (lambda_scope != p->t)
7499 {
7500 lambda_scope = p->t;
7501 lambda_count = p->i;
7502 }
7503 VEC_pop (tree_int, lambda_scope_stack);
7504 }
7505
7506 /* Parse a lambda expression.
7507
7508 lambda-expression:
7509 lambda-introducer lambda-declarator [opt] compound-statement
7510
7511 Returns a representation of the expression. */
7512
7513 static tree
7514 cp_parser_lambda_expression (cp_parser* parser)
7515 {
7516 tree lambda_expr = build_lambda_expr ();
7517 tree type;
7518
7519 LAMBDA_EXPR_LOCATION (lambda_expr)
7520 = cp_lexer_peek_token (parser->lexer)->location;
7521
7522 if (cp_unevaluated_operand)
7523 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7524 "lambda-expression in unevaluated context");
7525
7526 /* We may be in the middle of deferred access check. Disable
7527 it now. */
7528 push_deferring_access_checks (dk_no_deferred);
7529
7530 cp_parser_lambda_introducer (parser, lambda_expr);
7531
7532 type = begin_lambda_type (lambda_expr);
7533
7534 record_lambda_scope (lambda_expr);
7535
7536 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7537 determine_visibility (TYPE_NAME (type));
7538
7539 /* Now that we've started the type, add the capture fields for any
7540 explicit captures. */
7541 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7542
7543 {
7544 /* Inside the class, surrounding template-parameter-lists do not apply. */
7545 unsigned int saved_num_template_parameter_lists
7546 = parser->num_template_parameter_lists;
7547
7548 parser->num_template_parameter_lists = 0;
7549
7550 /* By virtue of defining a local class, a lambda expression has access to
7551 the private variables of enclosing classes. */
7552
7553 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7554
7555 cp_parser_lambda_body (parser, lambda_expr);
7556
7557 /* The capture list was built up in reverse order; fix that now. */
7558 {
7559 tree newlist = NULL_TREE;
7560 tree elt, next;
7561
7562 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7563 elt; elt = next)
7564 {
7565 tree field = TREE_PURPOSE (elt);
7566 char *buf;
7567
7568 next = TREE_CHAIN (elt);
7569 TREE_CHAIN (elt) = newlist;
7570 newlist = elt;
7571
7572 /* Also add __ to the beginning of the field name so that code
7573 outside the lambda body can't see the captured name. We could
7574 just remove the name entirely, but this is more useful for
7575 debugging. */
7576 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7577 /* The 'this' capture already starts with __. */
7578 continue;
7579
7580 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7581 buf[1] = buf[0] = '_';
7582 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7583 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7584 DECL_NAME (field) = get_identifier (buf);
7585 }
7586 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7587 }
7588
7589 maybe_add_lambda_conv_op (type);
7590
7591 type = finish_struct (type, /*attributes=*/NULL_TREE);
7592
7593 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7594 }
7595
7596 pop_deferring_access_checks ();
7597
7598 return build_lambda_object (lambda_expr);
7599 }
7600
7601 /* Parse the beginning of a lambda expression.
7602
7603 lambda-introducer:
7604 [ lambda-capture [opt] ]
7605
7606 LAMBDA_EXPR is the current representation of the lambda expression. */
7607
7608 static void
7609 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7610 {
7611 /* Need commas after the first capture. */
7612 bool first = true;
7613
7614 /* Eat the leading `['. */
7615 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7616
7617 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7618 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7619 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7620 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7621 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7622 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7623
7624 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7625 {
7626 cp_lexer_consume_token (parser->lexer);
7627 first = false;
7628 }
7629
7630 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7631 {
7632 cp_token* capture_token;
7633 tree capture_id;
7634 tree capture_init_expr;
7635 cp_id_kind idk = CP_ID_KIND_NONE;
7636 bool explicit_init_p = false;
7637
7638 enum capture_kind_type
7639 {
7640 BY_COPY,
7641 BY_REFERENCE
7642 };
7643 enum capture_kind_type capture_kind = BY_COPY;
7644
7645 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7646 {
7647 error ("expected end of capture-list");
7648 return;
7649 }
7650
7651 if (first)
7652 first = false;
7653 else
7654 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7655
7656 /* Possibly capture `this'. */
7657 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7658 {
7659 cp_lexer_consume_token (parser->lexer);
7660 add_capture (lambda_expr,
7661 /*id=*/get_identifier ("__this"),
7662 /*initializer=*/finish_this_expr(),
7663 /*by_reference_p=*/false,
7664 explicit_init_p);
7665 continue;
7666 }
7667
7668 /* Remember whether we want to capture as a reference or not. */
7669 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7670 {
7671 capture_kind = BY_REFERENCE;
7672 cp_lexer_consume_token (parser->lexer);
7673 }
7674
7675 /* Get the identifier. */
7676 capture_token = cp_lexer_peek_token (parser->lexer);
7677 capture_id = cp_parser_identifier (parser);
7678
7679 if (capture_id == error_mark_node)
7680 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7681 delimiters, but I modified this to stop on unnested ']' as well. It
7682 was already changed to stop on unnested '}', so the
7683 "closing_parenthesis" name is no more misleading with my change. */
7684 {
7685 cp_parser_skip_to_closing_parenthesis (parser,
7686 /*recovering=*/true,
7687 /*or_comma=*/true,
7688 /*consume_paren=*/true);
7689 break;
7690 }
7691
7692 /* Find the initializer for this capture. */
7693 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7694 {
7695 /* An explicit expression exists. */
7696 cp_lexer_consume_token (parser->lexer);
7697 pedwarn (input_location, OPT_pedantic,
7698 "ISO C++ does not allow initializers "
7699 "in lambda expression capture lists");
7700 capture_init_expr = cp_parser_assignment_expression (parser,
7701 /*cast_p=*/true,
7702 &idk);
7703 explicit_init_p = true;
7704 }
7705 else
7706 {
7707 const char* error_msg;
7708
7709 /* Turn the identifier into an id-expression. */
7710 capture_init_expr
7711 = cp_parser_lookup_name
7712 (parser,
7713 capture_id,
7714 none_type,
7715 /*is_template=*/false,
7716 /*is_namespace=*/false,
7717 /*check_dependency=*/true,
7718 /*ambiguous_decls=*/NULL,
7719 capture_token->location);
7720
7721 capture_init_expr
7722 = finish_id_expression
7723 (capture_id,
7724 capture_init_expr,
7725 parser->scope,
7726 &idk,
7727 /*integral_constant_expression_p=*/false,
7728 /*allow_non_integral_constant_expression_p=*/false,
7729 /*non_integral_constant_expression_p=*/NULL,
7730 /*template_p=*/false,
7731 /*done=*/true,
7732 /*address_p=*/false,
7733 /*template_arg_p=*/false,
7734 &error_msg,
7735 capture_token->location);
7736 }
7737
7738 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7739 capture_init_expr
7740 = unqualified_name_lookup_error (capture_init_expr);
7741
7742 add_capture (lambda_expr,
7743 capture_id,
7744 capture_init_expr,
7745 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7746 explicit_init_p);
7747 }
7748
7749 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7750 }
7751
7752 /* Parse the (optional) middle of a lambda expression.
7753
7754 lambda-declarator:
7755 ( parameter-declaration-clause [opt] )
7756 attribute-specifier [opt]
7757 mutable [opt]
7758 exception-specification [opt]
7759 lambda-return-type-clause [opt]
7760
7761 LAMBDA_EXPR is the current representation of the lambda expression. */
7762
7763 static void
7764 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7765 {
7766 /* 5.1.1.4 of the standard says:
7767 If a lambda-expression does not include a lambda-declarator, it is as if
7768 the lambda-declarator were ().
7769 This means an empty parameter list, no attributes, and no exception
7770 specification. */
7771 tree param_list = void_list_node;
7772 tree attributes = NULL_TREE;
7773 tree exception_spec = NULL_TREE;
7774 tree t;
7775
7776 /* The lambda-declarator is optional, but must begin with an opening
7777 parenthesis if present. */
7778 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7779 {
7780 cp_lexer_consume_token (parser->lexer);
7781
7782 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7783
7784 /* Parse parameters. */
7785 param_list = cp_parser_parameter_declaration_clause (parser);
7786
7787 /* Default arguments shall not be specified in the
7788 parameter-declaration-clause of a lambda-declarator. */
7789 for (t = param_list; t; t = TREE_CHAIN (t))
7790 if (TREE_PURPOSE (t))
7791 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7792 "default argument specified for lambda parameter");
7793
7794 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7795
7796 attributes = cp_parser_attributes_opt (parser);
7797
7798 /* Parse optional `mutable' keyword. */
7799 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7800 {
7801 cp_lexer_consume_token (parser->lexer);
7802 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7803 }
7804
7805 /* Parse optional exception specification. */
7806 exception_spec = cp_parser_exception_specification_opt (parser);
7807
7808 /* Parse optional trailing return type. */
7809 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7810 {
7811 cp_lexer_consume_token (parser->lexer);
7812 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7813 }
7814
7815 /* The function parameters must be in scope all the way until after the
7816 trailing-return-type in case of decltype. */
7817 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7818 pop_binding (DECL_NAME (t), t);
7819
7820 leave_scope ();
7821 }
7822
7823 /* Create the function call operator.
7824
7825 Messing with declarators like this is no uglier than building up the
7826 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7827 other code. */
7828 {
7829 cp_decl_specifier_seq return_type_specs;
7830 cp_declarator* declarator;
7831 tree fco;
7832 int quals;
7833 void *p;
7834
7835 clear_decl_specs (&return_type_specs);
7836 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7837 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7838 else
7839 /* Maybe we will deduce the return type later, but we can use void
7840 as a placeholder return type anyways. */
7841 return_type_specs.type = void_type_node;
7842
7843 p = obstack_alloc (&declarator_obstack, 0);
7844
7845 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7846 sfk_none);
7847
7848 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7849 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7850 declarator = make_call_declarator (declarator, param_list, quals,
7851 exception_spec,
7852 /*late_return_type=*/NULL_TREE);
7853 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7854
7855 fco = grokmethod (&return_type_specs,
7856 declarator,
7857 attributes);
7858 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7859 DECL_ARTIFICIAL (fco) = 1;
7860
7861 finish_member_declaration (fco);
7862
7863 obstack_free (&declarator_obstack, p);
7864 }
7865 }
7866
7867 /* Parse the body of a lambda expression, which is simply
7868
7869 compound-statement
7870
7871 but which requires special handling.
7872 LAMBDA_EXPR is the current representation of the lambda expression. */
7873
7874 static void
7875 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7876 {
7877 bool nested = (current_function_decl != NULL_TREE);
7878 if (nested)
7879 push_function_context ();
7880
7881 /* Finish the function call operator
7882 - class_specifier
7883 + late_parsing_for_member
7884 + function_definition_after_declarator
7885 + ctor_initializer_opt_and_function_body */
7886 {
7887 tree fco = lambda_function (lambda_expr);
7888 tree body;
7889 bool done = false;
7890
7891 /* Let the front end know that we are going to be defining this
7892 function. */
7893 start_preparsed_function (fco,
7894 NULL_TREE,
7895 SF_PRE_PARSED | SF_INCLASS_INLINE);
7896
7897 start_lambda_scope (fco);
7898 body = begin_function_body ();
7899
7900 /* 5.1.1.4 of the standard says:
7901 If a lambda-expression does not include a trailing-return-type, it
7902 is as if the trailing-return-type denotes the following type:
7903 * if the compound-statement is of the form
7904 { return attribute-specifier [opt] expression ; }
7905 the type of the returned expression after lvalue-to-rvalue
7906 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7907 (_conv.array_ 4.2), and function-to-pointer conversion
7908 (_conv.func_ 4.3);
7909 * otherwise, void. */
7910
7911 /* In a lambda that has neither a lambda-return-type-clause
7912 nor a deducible form, errors should be reported for return statements
7913 in the body. Since we used void as the placeholder return type, parsing
7914 the body as usual will give such desired behavior. */
7915 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7916 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7917 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7918 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7919 {
7920 tree compound_stmt;
7921 tree expr = NULL_TREE;
7922 cp_id_kind idk = CP_ID_KIND_NONE;
7923
7924 /* Parse tentatively in case there's more after the initial return
7925 statement. */
7926 cp_parser_parse_tentatively (parser);
7927
7928 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7929 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7930
7931 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7932
7933 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7934 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7935
7936 if (cp_parser_parse_definitely (parser))
7937 {
7938 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7939
7940 compound_stmt = begin_compound_stmt (0);
7941 /* Will get error here if type not deduced yet. */
7942 finish_return_stmt (expr);
7943 finish_compound_stmt (compound_stmt);
7944
7945 done = true;
7946 }
7947 }
7948
7949 if (!done)
7950 {
7951 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7952 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7953 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7954 cp_parser_compound_stmt does not pass it. */
7955 cp_parser_function_body (parser);
7956 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7957 }
7958
7959 finish_function_body (body);
7960 finish_lambda_scope ();
7961
7962 /* Finish the function and generate code for it if necessary. */
7963 expand_or_defer_fn (finish_function (/*inline*/2));
7964 }
7965
7966 if (nested)
7967 pop_function_context();
7968 }
7969
7970 /* Statements [gram.stmt.stmt] */
7971
7972 /* Parse a statement.
7973
7974 statement:
7975 labeled-statement
7976 expression-statement
7977 compound-statement
7978 selection-statement
7979 iteration-statement
7980 jump-statement
7981 declaration-statement
7982 try-block
7983
7984 IN_COMPOUND is true when the statement is nested inside a
7985 cp_parser_compound_statement; this matters for certain pragmas.
7986
7987 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7988 is a (possibly labeled) if statement which is not enclosed in braces
7989 and has an else clause. This is used to implement -Wparentheses. */
7990
7991 static void
7992 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7993 bool in_compound, bool *if_p)
7994 {
7995 tree statement;
7996 cp_token *token;
7997 location_t statement_location;
7998
7999 restart:
8000 if (if_p != NULL)
8001 *if_p = false;
8002 /* There is no statement yet. */
8003 statement = NULL_TREE;
8004 /* Peek at the next token. */
8005 token = cp_lexer_peek_token (parser->lexer);
8006 /* Remember the location of the first token in the statement. */
8007 statement_location = token->location;
8008 /* If this is a keyword, then that will often determine what kind of
8009 statement we have. */
8010 if (token->type == CPP_KEYWORD)
8011 {
8012 enum rid keyword = token->keyword;
8013
8014 switch (keyword)
8015 {
8016 case RID_CASE:
8017 case RID_DEFAULT:
8018 /* Looks like a labeled-statement with a case label.
8019 Parse the label, and then use tail recursion to parse
8020 the statement. */
8021 cp_parser_label_for_labeled_statement (parser);
8022 goto restart;
8023
8024 case RID_IF:
8025 case RID_SWITCH:
8026 statement = cp_parser_selection_statement (parser, if_p);
8027 break;
8028
8029 case RID_WHILE:
8030 case RID_DO:
8031 case RID_FOR:
8032 statement = cp_parser_iteration_statement (parser);
8033 break;
8034
8035 case RID_BREAK:
8036 case RID_CONTINUE:
8037 case RID_RETURN:
8038 case RID_GOTO:
8039 statement = cp_parser_jump_statement (parser);
8040 break;
8041
8042 /* Objective-C++ exception-handling constructs. */
8043 case RID_AT_TRY:
8044 case RID_AT_CATCH:
8045 case RID_AT_FINALLY:
8046 case RID_AT_SYNCHRONIZED:
8047 case RID_AT_THROW:
8048 statement = cp_parser_objc_statement (parser);
8049 break;
8050
8051 case RID_TRY:
8052 statement = cp_parser_try_block (parser);
8053 break;
8054
8055 case RID_NAMESPACE:
8056 /* This must be a namespace alias definition. */
8057 cp_parser_declaration_statement (parser);
8058 return;
8059
8060 default:
8061 /* It might be a keyword like `int' that can start a
8062 declaration-statement. */
8063 break;
8064 }
8065 }
8066 else if (token->type == CPP_NAME)
8067 {
8068 /* If the next token is a `:', then we are looking at a
8069 labeled-statement. */
8070 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8071 if (token->type == CPP_COLON)
8072 {
8073 /* Looks like a labeled-statement with an ordinary label.
8074 Parse the label, and then use tail recursion to parse
8075 the statement. */
8076 cp_parser_label_for_labeled_statement (parser);
8077 goto restart;
8078 }
8079 }
8080 /* Anything that starts with a `{' must be a compound-statement. */
8081 else if (token->type == CPP_OPEN_BRACE)
8082 statement = cp_parser_compound_statement (parser, NULL, false);
8083 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8084 a statement all its own. */
8085 else if (token->type == CPP_PRAGMA)
8086 {
8087 /* Only certain OpenMP pragmas are attached to statements, and thus
8088 are considered statements themselves. All others are not. In
8089 the context of a compound, accept the pragma as a "statement" and
8090 return so that we can check for a close brace. Otherwise we
8091 require a real statement and must go back and read one. */
8092 if (in_compound)
8093 cp_parser_pragma (parser, pragma_compound);
8094 else if (!cp_parser_pragma (parser, pragma_stmt))
8095 goto restart;
8096 return;
8097 }
8098 else if (token->type == CPP_EOF)
8099 {
8100 cp_parser_error (parser, "expected statement");
8101 return;
8102 }
8103
8104 /* Everything else must be a declaration-statement or an
8105 expression-statement. Try for the declaration-statement
8106 first, unless we are looking at a `;', in which case we know that
8107 we have an expression-statement. */
8108 if (!statement)
8109 {
8110 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8111 {
8112 cp_parser_parse_tentatively (parser);
8113 /* Try to parse the declaration-statement. */
8114 cp_parser_declaration_statement (parser);
8115 /* If that worked, we're done. */
8116 if (cp_parser_parse_definitely (parser))
8117 return;
8118 }
8119 /* Look for an expression-statement instead. */
8120 statement = cp_parser_expression_statement (parser, in_statement_expr);
8121 }
8122
8123 /* Set the line number for the statement. */
8124 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8125 SET_EXPR_LOCATION (statement, statement_location);
8126 }
8127
8128 /* Parse the label for a labeled-statement, i.e.
8129
8130 identifier :
8131 case constant-expression :
8132 default :
8133
8134 GNU Extension:
8135 case constant-expression ... constant-expression : statement
8136
8137 When a label is parsed without errors, the label is added to the
8138 parse tree by the finish_* functions, so this function doesn't
8139 have to return the label. */
8140
8141 static void
8142 cp_parser_label_for_labeled_statement (cp_parser* parser)
8143 {
8144 cp_token *token;
8145 tree label = NULL_TREE;
8146
8147 /* The next token should be an identifier. */
8148 token = cp_lexer_peek_token (parser->lexer);
8149 if (token->type != CPP_NAME
8150 && token->type != CPP_KEYWORD)
8151 {
8152 cp_parser_error (parser, "expected labeled-statement");
8153 return;
8154 }
8155
8156 switch (token->keyword)
8157 {
8158 case RID_CASE:
8159 {
8160 tree expr, expr_hi;
8161 cp_token *ellipsis;
8162
8163 /* Consume the `case' token. */
8164 cp_lexer_consume_token (parser->lexer);
8165 /* Parse the constant-expression. */
8166 expr = cp_parser_constant_expression (parser,
8167 /*allow_non_constant_p=*/false,
8168 NULL);
8169
8170 ellipsis = cp_lexer_peek_token (parser->lexer);
8171 if (ellipsis->type == CPP_ELLIPSIS)
8172 {
8173 /* Consume the `...' token. */
8174 cp_lexer_consume_token (parser->lexer);
8175 expr_hi =
8176 cp_parser_constant_expression (parser,
8177 /*allow_non_constant_p=*/false,
8178 NULL);
8179 /* We don't need to emit warnings here, as the common code
8180 will do this for us. */
8181 }
8182 else
8183 expr_hi = NULL_TREE;
8184
8185 if (parser->in_switch_statement_p)
8186 finish_case_label (token->location, expr, expr_hi);
8187 else
8188 error_at (token->location,
8189 "case label %qE not within a switch statement",
8190 expr);
8191 }
8192 break;
8193
8194 case RID_DEFAULT:
8195 /* Consume the `default' token. */
8196 cp_lexer_consume_token (parser->lexer);
8197
8198 if (parser->in_switch_statement_p)
8199 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8200 else
8201 error_at (token->location, "case label not within a switch statement");
8202 break;
8203
8204 default:
8205 /* Anything else must be an ordinary label. */
8206 label = finish_label_stmt (cp_parser_identifier (parser));
8207 break;
8208 }
8209
8210 /* Require the `:' token. */
8211 cp_parser_require (parser, CPP_COLON, RT_COLON);
8212
8213 /* An ordinary label may optionally be followed by attributes.
8214 However, this is only permitted if the attributes are then
8215 followed by a semicolon. This is because, for backward
8216 compatibility, when parsing
8217 lab: __attribute__ ((unused)) int i;
8218 we want the attribute to attach to "i", not "lab". */
8219 if (label != NULL_TREE
8220 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8221 {
8222 tree attrs;
8223
8224 cp_parser_parse_tentatively (parser);
8225 attrs = cp_parser_attributes_opt (parser);
8226 if (attrs == NULL_TREE
8227 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8228 cp_parser_abort_tentative_parse (parser);
8229 else if (!cp_parser_parse_definitely (parser))
8230 ;
8231 else
8232 cplus_decl_attributes (&label, attrs, 0);
8233 }
8234 }
8235
8236 /* Parse an expression-statement.
8237
8238 expression-statement:
8239 expression [opt] ;
8240
8241 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8242 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8243 indicates whether this expression-statement is part of an
8244 expression statement. */
8245
8246 static tree
8247 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8248 {
8249 tree statement = NULL_TREE;
8250 cp_token *token = cp_lexer_peek_token (parser->lexer);
8251
8252 /* If the next token is a ';', then there is no expression
8253 statement. */
8254 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8255 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8256
8257 /* Give a helpful message for "A<T>::type t;" and the like. */
8258 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8259 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8260 {
8261 if (TREE_CODE (statement) == SCOPE_REF)
8262 error_at (token->location, "need %<typename%> before %qE because "
8263 "%qT is a dependent scope",
8264 statement, TREE_OPERAND (statement, 0));
8265 else if (is_overloaded_fn (statement)
8266 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8267 {
8268 /* A::A a; */
8269 tree fn = get_first_fn (statement);
8270 error_at (token->location,
8271 "%<%T::%D%> names the constructor, not the type",
8272 DECL_CONTEXT (fn), DECL_NAME (fn));
8273 }
8274 }
8275
8276 /* Consume the final `;'. */
8277 cp_parser_consume_semicolon_at_end_of_statement (parser);
8278
8279 if (in_statement_expr
8280 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8281 /* This is the final expression statement of a statement
8282 expression. */
8283 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8284 else if (statement)
8285 statement = finish_expr_stmt (statement);
8286 else
8287 finish_stmt ();
8288
8289 return statement;
8290 }
8291
8292 /* Parse a compound-statement.
8293
8294 compound-statement:
8295 { statement-seq [opt] }
8296
8297 GNU extension:
8298
8299 compound-statement:
8300 { label-declaration-seq [opt] statement-seq [opt] }
8301
8302 label-declaration-seq:
8303 label-declaration
8304 label-declaration-seq label-declaration
8305
8306 Returns a tree representing the statement. */
8307
8308 static tree
8309 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8310 bool in_try)
8311 {
8312 tree compound_stmt;
8313
8314 /* Consume the `{'. */
8315 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8316 return error_mark_node;
8317 /* Begin the compound-statement. */
8318 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8319 /* If the next keyword is `__label__' we have a label declaration. */
8320 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8321 cp_parser_label_declaration (parser);
8322 /* Parse an (optional) statement-seq. */
8323 cp_parser_statement_seq_opt (parser, in_statement_expr);
8324 /* Finish the compound-statement. */
8325 finish_compound_stmt (compound_stmt);
8326 /* Consume the `}'. */
8327 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8328
8329 return compound_stmt;
8330 }
8331
8332 /* Parse an (optional) statement-seq.
8333
8334 statement-seq:
8335 statement
8336 statement-seq [opt] statement */
8337
8338 static void
8339 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8340 {
8341 /* Scan statements until there aren't any more. */
8342 while (true)
8343 {
8344 cp_token *token = cp_lexer_peek_token (parser->lexer);
8345
8346 /* If we are looking at a `}', then we have run out of
8347 statements; the same is true if we have reached the end
8348 of file, or have stumbled upon a stray '@end'. */
8349 if (token->type == CPP_CLOSE_BRACE
8350 || token->type == CPP_EOF
8351 || token->type == CPP_PRAGMA_EOL
8352 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8353 break;
8354
8355 /* If we are in a compound statement and find 'else' then
8356 something went wrong. */
8357 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8358 {
8359 if (parser->in_statement & IN_IF_STMT)
8360 break;
8361 else
8362 {
8363 token = cp_lexer_consume_token (parser->lexer);
8364 error_at (token->location, "%<else%> without a previous %<if%>");
8365 }
8366 }
8367
8368 /* Parse the statement. */
8369 cp_parser_statement (parser, in_statement_expr, true, NULL);
8370 }
8371 }
8372
8373 /* Parse a selection-statement.
8374
8375 selection-statement:
8376 if ( condition ) statement
8377 if ( condition ) statement else statement
8378 switch ( condition ) statement
8379
8380 Returns the new IF_STMT or SWITCH_STMT.
8381
8382 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8383 is a (possibly labeled) if statement which is not enclosed in
8384 braces and has an else clause. This is used to implement
8385 -Wparentheses. */
8386
8387 static tree
8388 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8389 {
8390 cp_token *token;
8391 enum rid keyword;
8392
8393 if (if_p != NULL)
8394 *if_p = false;
8395
8396 /* Peek at the next token. */
8397 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8398
8399 /* See what kind of keyword it is. */
8400 keyword = token->keyword;
8401 switch (keyword)
8402 {
8403 case RID_IF:
8404 case RID_SWITCH:
8405 {
8406 tree statement;
8407 tree condition;
8408
8409 /* Look for the `('. */
8410 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8411 {
8412 cp_parser_skip_to_end_of_statement (parser);
8413 return error_mark_node;
8414 }
8415
8416 /* Begin the selection-statement. */
8417 if (keyword == RID_IF)
8418 statement = begin_if_stmt ();
8419 else
8420 statement = begin_switch_stmt ();
8421
8422 /* Parse the condition. */
8423 condition = cp_parser_condition (parser);
8424 /* Look for the `)'. */
8425 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8426 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8427 /*consume_paren=*/true);
8428
8429 if (keyword == RID_IF)
8430 {
8431 bool nested_if;
8432 unsigned char in_statement;
8433
8434 /* Add the condition. */
8435 finish_if_stmt_cond (condition, statement);
8436
8437 /* Parse the then-clause. */
8438 in_statement = parser->in_statement;
8439 parser->in_statement |= IN_IF_STMT;
8440 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8441 {
8442 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8443 add_stmt (build_empty_stmt (loc));
8444 cp_lexer_consume_token (parser->lexer);
8445 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8446 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8447 "empty body in an %<if%> statement");
8448 nested_if = false;
8449 }
8450 else
8451 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8452 parser->in_statement = in_statement;
8453
8454 finish_then_clause (statement);
8455
8456 /* If the next token is `else', parse the else-clause. */
8457 if (cp_lexer_next_token_is_keyword (parser->lexer,
8458 RID_ELSE))
8459 {
8460 /* Consume the `else' keyword. */
8461 cp_lexer_consume_token (parser->lexer);
8462 begin_else_clause (statement);
8463 /* Parse the else-clause. */
8464 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8465 {
8466 location_t loc;
8467 loc = cp_lexer_peek_token (parser->lexer)->location;
8468 warning_at (loc,
8469 OPT_Wempty_body, "suggest braces around "
8470 "empty body in an %<else%> statement");
8471 add_stmt (build_empty_stmt (loc));
8472 cp_lexer_consume_token (parser->lexer);
8473 }
8474 else
8475 cp_parser_implicitly_scoped_statement (parser, NULL);
8476
8477 finish_else_clause (statement);
8478
8479 /* If we are currently parsing a then-clause, then
8480 IF_P will not be NULL. We set it to true to
8481 indicate that this if statement has an else clause.
8482 This may trigger the Wparentheses warning below
8483 when we get back up to the parent if statement. */
8484 if (if_p != NULL)
8485 *if_p = true;
8486 }
8487 else
8488 {
8489 /* This if statement does not have an else clause. If
8490 NESTED_IF is true, then the then-clause is an if
8491 statement which does have an else clause. We warn
8492 about the potential ambiguity. */
8493 if (nested_if)
8494 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8495 "suggest explicit braces to avoid ambiguous"
8496 " %<else%>");
8497 }
8498
8499 /* Now we're all done with the if-statement. */
8500 finish_if_stmt (statement);
8501 }
8502 else
8503 {
8504 bool in_switch_statement_p;
8505 unsigned char in_statement;
8506
8507 /* Add the condition. */
8508 finish_switch_cond (condition, statement);
8509
8510 /* Parse the body of the switch-statement. */
8511 in_switch_statement_p = parser->in_switch_statement_p;
8512 in_statement = parser->in_statement;
8513 parser->in_switch_statement_p = true;
8514 parser->in_statement |= IN_SWITCH_STMT;
8515 cp_parser_implicitly_scoped_statement (parser, NULL);
8516 parser->in_switch_statement_p = in_switch_statement_p;
8517 parser->in_statement = in_statement;
8518
8519 /* Now we're all done with the switch-statement. */
8520 finish_switch_stmt (statement);
8521 }
8522
8523 return statement;
8524 }
8525 break;
8526
8527 default:
8528 cp_parser_error (parser, "expected selection-statement");
8529 return error_mark_node;
8530 }
8531 }
8532
8533 /* Parse a condition.
8534
8535 condition:
8536 expression
8537 type-specifier-seq declarator = initializer-clause
8538 type-specifier-seq declarator braced-init-list
8539
8540 GNU Extension:
8541
8542 condition:
8543 type-specifier-seq declarator asm-specification [opt]
8544 attributes [opt] = assignment-expression
8545
8546 Returns the expression that should be tested. */
8547
8548 static tree
8549 cp_parser_condition (cp_parser* parser)
8550 {
8551 cp_decl_specifier_seq type_specifiers;
8552 const char *saved_message;
8553 int declares_class_or_enum;
8554
8555 /* Try the declaration first. */
8556 cp_parser_parse_tentatively (parser);
8557 /* New types are not allowed in the type-specifier-seq for a
8558 condition. */
8559 saved_message = parser->type_definition_forbidden_message;
8560 parser->type_definition_forbidden_message
8561 = G_("types may not be defined in conditions");
8562 /* Parse the type-specifier-seq. */
8563 cp_parser_decl_specifier_seq (parser,
8564 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8565 &type_specifiers,
8566 &declares_class_or_enum);
8567 /* Restore the saved message. */
8568 parser->type_definition_forbidden_message = saved_message;
8569 /* If all is well, we might be looking at a declaration. */
8570 if (!cp_parser_error_occurred (parser))
8571 {
8572 tree decl;
8573 tree asm_specification;
8574 tree attributes;
8575 cp_declarator *declarator;
8576 tree initializer = NULL_TREE;
8577
8578 /* Parse the declarator. */
8579 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8580 /*ctor_dtor_or_conv_p=*/NULL,
8581 /*parenthesized_p=*/NULL,
8582 /*member_p=*/false);
8583 /* Parse the attributes. */
8584 attributes = cp_parser_attributes_opt (parser);
8585 /* Parse the asm-specification. */
8586 asm_specification = cp_parser_asm_specification_opt (parser);
8587 /* If the next token is not an `=' or '{', then we might still be
8588 looking at an expression. For example:
8589
8590 if (A(a).x)
8591
8592 looks like a decl-specifier-seq and a declarator -- but then
8593 there is no `=', so this is an expression. */
8594 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8595 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8596 cp_parser_simulate_error (parser);
8597
8598 /* If we did see an `=' or '{', then we are looking at a declaration
8599 for sure. */
8600 if (cp_parser_parse_definitely (parser))
8601 {
8602 tree pushed_scope;
8603 bool non_constant_p;
8604 bool flags = LOOKUP_ONLYCONVERTING;
8605
8606 /* Create the declaration. */
8607 decl = start_decl (declarator, &type_specifiers,
8608 /*initialized_p=*/true,
8609 attributes, /*prefix_attributes=*/NULL_TREE,
8610 &pushed_scope);
8611
8612 /* Parse the initializer. */
8613 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8614 {
8615 initializer = cp_parser_braced_list (parser, &non_constant_p);
8616 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8617 flags = 0;
8618 }
8619 else
8620 {
8621 /* Consume the `='. */
8622 cp_parser_require (parser, CPP_EQ, RT_EQ);
8623 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8624 }
8625 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8626 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8627
8628 if (!non_constant_p)
8629 initializer = fold_non_dependent_expr (initializer);
8630
8631 /* Process the initializer. */
8632 cp_finish_decl (decl,
8633 initializer, !non_constant_p,
8634 asm_specification,
8635 flags);
8636
8637 if (pushed_scope)
8638 pop_scope (pushed_scope);
8639
8640 return convert_from_reference (decl);
8641 }
8642 }
8643 /* If we didn't even get past the declarator successfully, we are
8644 definitely not looking at a declaration. */
8645 else
8646 cp_parser_abort_tentative_parse (parser);
8647
8648 /* Otherwise, we are looking at an expression. */
8649 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8650 }
8651
8652 /* Parses a traditional for-statement until the closing ')', not included. */
8653
8654 static tree
8655 cp_parser_c_for (cp_parser *parser)
8656 {
8657 /* Normal for loop */
8658 tree stmt;
8659 tree condition = NULL_TREE;
8660 tree expression = NULL_TREE;
8661
8662 /* Begin the for-statement. */
8663 stmt = begin_for_stmt ();
8664
8665 /* Parse the initialization. */
8666 cp_parser_for_init_statement (parser);
8667 finish_for_init_stmt (stmt);
8668
8669 /* If there's a condition, process it. */
8670 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8671 condition = cp_parser_condition (parser);
8672 finish_for_cond (condition, stmt);
8673 /* Look for the `;'. */
8674 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8675
8676 /* If there's an expression, process it. */
8677 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8678 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8679 finish_for_expr (expression, stmt);
8680
8681 return stmt;
8682 }
8683
8684 /* Tries to parse a range-based for-statement:
8685
8686 range-based-for:
8687 type-specifier-seq declarator : expression
8688
8689 If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8690 expression. Note that the *DECL is returned unfinished, so
8691 later you should call cp_finish_decl().
8692
8693 Returns TRUE iff a range-based for is parsed. */
8694
8695 static tree
8696 cp_parser_range_for (cp_parser *parser)
8697 {
8698 tree stmt, range_decl, range_expr;
8699 cp_decl_specifier_seq type_specifiers;
8700 cp_declarator *declarator;
8701 const char *saved_message;
8702 tree attributes, pushed_scope;
8703
8704 cp_parser_parse_tentatively (parser);
8705 /* New types are not allowed in the type-specifier-seq for a
8706 range-based for loop. */
8707 saved_message = parser->type_definition_forbidden_message;
8708 parser->type_definition_forbidden_message
8709 = G_("types may not be defined in range-based for loops");
8710 /* Parse the type-specifier-seq. */
8711 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8712 /*is_trailing_return=*/false,
8713 &type_specifiers);
8714 /* Restore the saved message. */
8715 parser->type_definition_forbidden_message = saved_message;
8716 /* If all is well, we might be looking at a declaration. */
8717 if (cp_parser_error_occurred (parser))
8718 {
8719 cp_parser_abort_tentative_parse (parser);
8720 return NULL_TREE;
8721 }
8722 /* Parse the declarator. */
8723 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8724 /*ctor_dtor_or_conv_p=*/NULL,
8725 /*parenthesized_p=*/NULL,
8726 /*member_p=*/false);
8727 /* Parse the attributes. */
8728 attributes = cp_parser_attributes_opt (parser);
8729 /* The next token should be `:'. */
8730 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8731 cp_parser_simulate_error (parser);
8732
8733 /* Check if it is a range-based for */
8734 if (!cp_parser_parse_definitely (parser))
8735 return NULL_TREE;
8736
8737 cp_parser_require (parser, CPP_COLON, RT_COLON);
8738 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8739 {
8740 bool expr_non_constant_p;
8741 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8742 }
8743 else
8744 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8745
8746 /* If in template, STMT is converted to a normal for-statements
8747 at instantiation. If not, it is done just ahead. */
8748 if (processing_template_decl)
8749 stmt = begin_range_for_stmt ();
8750 else
8751 stmt = begin_for_stmt ();
8752
8753 /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8754 range_decl = start_decl (declarator, &type_specifiers,
8755 /*initialized_p=*/SD_INITIALIZED,
8756 attributes, /*prefix_attributes=*/NULL_TREE,
8757 &pushed_scope);
8758 /* No scope allowed here */
8759 pop_scope (pushed_scope);
8760
8761 if (TREE_CODE (stmt) == RANGE_FOR_STMT)
8762 finish_range_for_decl (stmt, range_decl, range_expr);
8763 else
8764 /* Convert the range-based for loop into a normal for-statement. */
8765 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8766
8767 return stmt;
8768 }
8769
8770 /* Converts a range-based for-statement into a normal
8771 for-statement, as per the definition.
8772
8773 for (RANGE_DECL : RANGE_EXPR)
8774 BLOCK
8775
8776 should be equivalent to:
8777
8778 {
8779 auto &&__range = RANGE_EXPR;
8780 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8781 __begin != __end;
8782 ++__begin)
8783 {
8784 RANGE_DECL = *__begin;
8785 BLOCK
8786 }
8787 }
8788
8789 If RANGE_EXPR is an array:
8790 BEGIN_EXPR = __range
8791 END_EXPR = __range + ARRAY_SIZE(__range)
8792 Else:
8793 BEGIN_EXPR = begin(__range)
8794 END_EXPR = end(__range);
8795
8796 When calling begin()/end() we must use argument dependent
8797 lookup, but always considering 'std' as an associated namespace. */
8798
8799 tree
8800 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8801 {
8802 tree range_type, range_temp;
8803 tree begin, end;
8804 tree iter_type, begin_expr, end_expr;
8805 tree condition, expression;
8806
8807 /* Find out the type deduced by the declaration
8808 * `auto &&__range = range_expr' */
8809 range_type = cp_build_reference_type (make_auto (), true);
8810 range_type = do_auto_deduction (range_type, range_expr,
8811 type_uses_auto (range_type));
8812
8813 /* Create the __range variable */
8814 range_temp = build_decl (input_location, VAR_DECL,
8815 get_identifier ("__for_range"), range_type);
8816 TREE_USED (range_temp) = 1;
8817 DECL_ARTIFICIAL (range_temp) = 1;
8818 pushdecl (range_temp);
8819 cp_finish_decl (range_temp, range_expr,
8820 /*is_constant_init*/false, NULL_TREE,
8821 LOOKUP_ONLYCONVERTING);
8822
8823 range_temp = convert_from_reference (range_temp);
8824
8825 if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8826 {
8827 /* If RANGE_TEMP is an array we will use pointer arithmetic */
8828 iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8829 begin_expr = range_temp;
8830 end_expr
8831 = build_binary_op (input_location, PLUS_EXPR,
8832 range_temp,
8833 array_type_nelts_top (TREE_TYPE (range_temp)), 0);
8834 }
8835 else
8836 {
8837 /* If it is not an array, we must call begin(__range)/end__range() */
8838 VEC(tree,gc) *vec;
8839
8840 begin_expr = get_identifier ("begin");
8841 vec = make_tree_vector ();
8842 VEC_safe_push (tree, gc, vec, range_temp);
8843 begin_expr = perform_koenig_lookup (begin_expr, vec,
8844 /*include_std=*/true);
8845 begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8846 tf_warning_or_error);
8847 release_tree_vector (vec);
8848
8849 end_expr = get_identifier ("end");
8850 vec = make_tree_vector ();
8851 VEC_safe_push (tree, gc, vec, range_temp);
8852 end_expr = perform_koenig_lookup (end_expr, vec,
8853 /*include_std=*/true);
8854 end_expr = finish_call_expr (end_expr, &vec, false, true,
8855 tf_warning_or_error);
8856 release_tree_vector (vec);
8857
8858 /* The unqualified type of the __begin and __end temporaries should
8859 * be the same as required by the multiple auto declaration */
8860 iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8861 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8862 error ("inconsistent begin/end types in range-based for: %qT and %qT",
8863 TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8864 }
8865
8866 /* The new for initialization statement */
8867 begin = build_decl (input_location, VAR_DECL,
8868 get_identifier ("__for_begin"), iter_type);
8869 TREE_USED (begin) = 1;
8870 DECL_ARTIFICIAL (begin) = 1;
8871 pushdecl (begin);
8872 cp_finish_decl (begin, begin_expr,
8873 /*is_constant_init*/false, NULL_TREE,
8874 LOOKUP_ONLYCONVERTING);
8875
8876 end = build_decl (input_location, VAR_DECL,
8877 get_identifier ("__for_end"), iter_type);
8878 TREE_USED (end) = 1;
8879 DECL_ARTIFICIAL (end) = 1;
8880 pushdecl (end);
8881 cp_finish_decl (end, end_expr,
8882 /*is_constant_init*/false, NULL_TREE,
8883 LOOKUP_ONLYCONVERTING);
8884
8885 finish_for_init_stmt (statement);
8886
8887 /* The new for condition */
8888 condition = build_x_binary_op (NE_EXPR,
8889 begin, ERROR_MARK,
8890 end, ERROR_MARK,
8891 NULL, tf_warning_or_error);
8892 finish_for_cond (condition, statement);
8893
8894 /* The new increment expression */
8895 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8896 finish_for_expr (expression, statement);
8897
8898 /* The declaration is initialized with *__begin inside the loop body */
8899 cp_finish_decl (range_decl,
8900 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8901 /*is_constant_init*/false, NULL_TREE,
8902 LOOKUP_ONLYCONVERTING);
8903
8904 return statement;
8905 }
8906
8907
8908 /* Parse an iteration-statement.
8909
8910 iteration-statement:
8911 while ( condition ) statement
8912 do statement while ( expression ) ;
8913 for ( for-init-statement condition [opt] ; expression [opt] )
8914 statement
8915
8916 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8917
8918 static tree
8919 cp_parser_iteration_statement (cp_parser* parser)
8920 {
8921 cp_token *token;
8922 enum rid keyword;
8923 tree statement;
8924 unsigned char in_statement;
8925
8926 /* Peek at the next token. */
8927 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8928 if (!token)
8929 return error_mark_node;
8930
8931 /* Remember whether or not we are already within an iteration
8932 statement. */
8933 in_statement = parser->in_statement;
8934
8935 /* See what kind of keyword it is. */
8936 keyword = token->keyword;
8937 switch (keyword)
8938 {
8939 case RID_WHILE:
8940 {
8941 tree condition;
8942
8943 /* Begin the while-statement. */
8944 statement = begin_while_stmt ();
8945 /* Look for the `('. */
8946 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8947 /* Parse the condition. */
8948 condition = cp_parser_condition (parser);
8949 finish_while_stmt_cond (condition, statement);
8950 /* Look for the `)'. */
8951 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8952 /* Parse the dependent statement. */
8953 parser->in_statement = IN_ITERATION_STMT;
8954 cp_parser_already_scoped_statement (parser);
8955 parser->in_statement = in_statement;
8956 /* We're done with the while-statement. */
8957 finish_while_stmt (statement);
8958 }
8959 break;
8960
8961 case RID_DO:
8962 {
8963 tree expression;
8964
8965 /* Begin the do-statement. */
8966 statement = begin_do_stmt ();
8967 /* Parse the body of the do-statement. */
8968 parser->in_statement = IN_ITERATION_STMT;
8969 cp_parser_implicitly_scoped_statement (parser, NULL);
8970 parser->in_statement = in_statement;
8971 finish_do_body (statement);
8972 /* Look for the `while' keyword. */
8973 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8974 /* Look for the `('. */
8975 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8976 /* Parse the expression. */
8977 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8978 /* We're done with the do-statement. */
8979 finish_do_stmt (expression, statement);
8980 /* Look for the `)'. */
8981 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8982 /* Look for the `;'. */
8983 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8984 }
8985 break;
8986
8987 case RID_FOR:
8988 {
8989 /* Look for the `('. */
8990 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8991
8992 if (cxx_dialect == cxx0x)
8993 statement = cp_parser_range_for (parser);
8994 else
8995 statement = NULL_TREE;
8996 if (statement == NULL_TREE)
8997 statement = cp_parser_c_for (parser);
8998
8999 /* Look for the `)'. */
9000 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9001
9002 /* Parse the body of the for-statement. */
9003 parser->in_statement = IN_ITERATION_STMT;
9004 cp_parser_already_scoped_statement (parser);
9005 parser->in_statement = in_statement;
9006
9007 /* We're done with the for-statement. */
9008 finish_for_stmt (statement);
9009 }
9010 break;
9011
9012 default:
9013 cp_parser_error (parser, "expected iteration-statement");
9014 statement = error_mark_node;
9015 break;
9016 }
9017
9018 return statement;
9019 }
9020
9021 /* Parse a for-init-statement.
9022
9023 for-init-statement:
9024 expression-statement
9025 simple-declaration */
9026
9027 static void
9028 cp_parser_for_init_statement (cp_parser* parser)
9029 {
9030 /* If the next token is a `;', then we have an empty
9031 expression-statement. Grammatically, this is also a
9032 simple-declaration, but an invalid one, because it does not
9033 declare anything. Therefore, if we did not handle this case
9034 specially, we would issue an error message about an invalid
9035 declaration. */
9036 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9037 {
9038 /* We're going to speculatively look for a declaration, falling back
9039 to an expression, if necessary. */
9040 cp_parser_parse_tentatively (parser);
9041 /* Parse the declaration. */
9042 cp_parser_simple_declaration (parser,
9043 /*function_definition_allowed_p=*/false);
9044 /* If the tentative parse failed, then we shall need to look for an
9045 expression-statement. */
9046 if (cp_parser_parse_definitely (parser))
9047 return;
9048 }
9049
9050 cp_parser_expression_statement (parser, NULL_TREE);
9051 }
9052
9053 /* Parse a jump-statement.
9054
9055 jump-statement:
9056 break ;
9057 continue ;
9058 return expression [opt] ;
9059 return braced-init-list ;
9060 goto identifier ;
9061
9062 GNU extension:
9063
9064 jump-statement:
9065 goto * expression ;
9066
9067 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9068
9069 static tree
9070 cp_parser_jump_statement (cp_parser* parser)
9071 {
9072 tree statement = error_mark_node;
9073 cp_token *token;
9074 enum rid keyword;
9075 unsigned char in_statement;
9076
9077 /* Peek at the next token. */
9078 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9079 if (!token)
9080 return error_mark_node;
9081
9082 /* See what kind of keyword it is. */
9083 keyword = token->keyword;
9084 switch (keyword)
9085 {
9086 case RID_BREAK:
9087 in_statement = parser->in_statement & ~IN_IF_STMT;
9088 switch (in_statement)
9089 {
9090 case 0:
9091 error_at (token->location, "break statement not within loop or switch");
9092 break;
9093 default:
9094 gcc_assert ((in_statement & IN_SWITCH_STMT)
9095 || in_statement == IN_ITERATION_STMT);
9096 statement = finish_break_stmt ();
9097 break;
9098 case IN_OMP_BLOCK:
9099 error_at (token->location, "invalid exit from OpenMP structured block");
9100 break;
9101 case IN_OMP_FOR:
9102 error_at (token->location, "break statement used with OpenMP for loop");
9103 break;
9104 }
9105 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9106 break;
9107
9108 case RID_CONTINUE:
9109 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9110 {
9111 case 0:
9112 error_at (token->location, "continue statement not within a loop");
9113 break;
9114 case IN_ITERATION_STMT:
9115 case IN_OMP_FOR:
9116 statement = finish_continue_stmt ();
9117 break;
9118 case IN_OMP_BLOCK:
9119 error_at (token->location, "invalid exit from OpenMP structured block");
9120 break;
9121 default:
9122 gcc_unreachable ();
9123 }
9124 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9125 break;
9126
9127 case RID_RETURN:
9128 {
9129 tree expr;
9130 bool expr_non_constant_p;
9131
9132 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9133 {
9134 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9135 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9136 }
9137 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9138 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9139 else
9140 /* If the next token is a `;', then there is no
9141 expression. */
9142 expr = NULL_TREE;
9143 /* Build the return-statement. */
9144 statement = finish_return_stmt (expr);
9145 /* Look for the final `;'. */
9146 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9147 }
9148 break;
9149
9150 case RID_GOTO:
9151 /* Create the goto-statement. */
9152 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9153 {
9154 /* Issue a warning about this use of a GNU extension. */
9155 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9156 /* Consume the '*' token. */
9157 cp_lexer_consume_token (parser->lexer);
9158 /* Parse the dependent expression. */
9159 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9160 }
9161 else
9162 finish_goto_stmt (cp_parser_identifier (parser));
9163 /* Look for the final `;'. */
9164 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9165 break;
9166
9167 default:
9168 cp_parser_error (parser, "expected jump-statement");
9169 break;
9170 }
9171
9172 return statement;
9173 }
9174
9175 /* Parse a declaration-statement.
9176
9177 declaration-statement:
9178 block-declaration */
9179
9180 static void
9181 cp_parser_declaration_statement (cp_parser* parser)
9182 {
9183 void *p;
9184
9185 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9186 p = obstack_alloc (&declarator_obstack, 0);
9187
9188 /* Parse the block-declaration. */
9189 cp_parser_block_declaration (parser, /*statement_p=*/true);
9190
9191 /* Free any declarators allocated. */
9192 obstack_free (&declarator_obstack, p);
9193
9194 /* Finish off the statement. */
9195 finish_stmt ();
9196 }
9197
9198 /* Some dependent statements (like `if (cond) statement'), are
9199 implicitly in their own scope. In other words, if the statement is
9200 a single statement (as opposed to a compound-statement), it is
9201 none-the-less treated as if it were enclosed in braces. Any
9202 declarations appearing in the dependent statement are out of scope
9203 after control passes that point. This function parses a statement,
9204 but ensures that is in its own scope, even if it is not a
9205 compound-statement.
9206
9207 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9208 is a (possibly labeled) if statement which is not enclosed in
9209 braces and has an else clause. This is used to implement
9210 -Wparentheses.
9211
9212 Returns the new statement. */
9213
9214 static tree
9215 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9216 {
9217 tree statement;
9218
9219 if (if_p != NULL)
9220 *if_p = false;
9221
9222 /* Mark if () ; with a special NOP_EXPR. */
9223 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9224 {
9225 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9226 cp_lexer_consume_token (parser->lexer);
9227 statement = add_stmt (build_empty_stmt (loc));
9228 }
9229 /* if a compound is opened, we simply parse the statement directly. */
9230 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9231 statement = cp_parser_compound_statement (parser, NULL, false);
9232 /* If the token is not a `{', then we must take special action. */
9233 else
9234 {
9235 /* Create a compound-statement. */
9236 statement = begin_compound_stmt (0);
9237 /* Parse the dependent-statement. */
9238 cp_parser_statement (parser, NULL_TREE, false, if_p);
9239 /* Finish the dummy compound-statement. */
9240 finish_compound_stmt (statement);
9241 }
9242
9243 /* Return the statement. */
9244 return statement;
9245 }
9246
9247 /* For some dependent statements (like `while (cond) statement'), we
9248 have already created a scope. Therefore, even if the dependent
9249 statement is a compound-statement, we do not want to create another
9250 scope. */
9251
9252 static void
9253 cp_parser_already_scoped_statement (cp_parser* parser)
9254 {
9255 /* If the token is a `{', then we must take special action. */
9256 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9257 cp_parser_statement (parser, NULL_TREE, false, NULL);
9258 else
9259 {
9260 /* Avoid calling cp_parser_compound_statement, so that we
9261 don't create a new scope. Do everything else by hand. */
9262 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9263 /* If the next keyword is `__label__' we have a label declaration. */
9264 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9265 cp_parser_label_declaration (parser);
9266 /* Parse an (optional) statement-seq. */
9267 cp_parser_statement_seq_opt (parser, NULL_TREE);
9268 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9269 }
9270 }
9271
9272 /* Declarations [gram.dcl.dcl] */
9273
9274 /* Parse an optional declaration-sequence.
9275
9276 declaration-seq:
9277 declaration
9278 declaration-seq declaration */
9279
9280 static void
9281 cp_parser_declaration_seq_opt (cp_parser* parser)
9282 {
9283 while (true)
9284 {
9285 cp_token *token;
9286
9287 token = cp_lexer_peek_token (parser->lexer);
9288
9289 if (token->type == CPP_CLOSE_BRACE
9290 || token->type == CPP_EOF
9291 || token->type == CPP_PRAGMA_EOL)
9292 break;
9293
9294 if (token->type == CPP_SEMICOLON)
9295 {
9296 /* A declaration consisting of a single semicolon is
9297 invalid. Allow it unless we're being pedantic. */
9298 cp_lexer_consume_token (parser->lexer);
9299 if (!in_system_header)
9300 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9301 continue;
9302 }
9303
9304 /* If we're entering or exiting a region that's implicitly
9305 extern "C", modify the lang context appropriately. */
9306 if (!parser->implicit_extern_c && token->implicit_extern_c)
9307 {
9308 push_lang_context (lang_name_c);
9309 parser->implicit_extern_c = true;
9310 }
9311 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9312 {
9313 pop_lang_context ();
9314 parser->implicit_extern_c = false;
9315 }
9316
9317 if (token->type == CPP_PRAGMA)
9318 {
9319 /* A top-level declaration can consist solely of a #pragma.
9320 A nested declaration cannot, so this is done here and not
9321 in cp_parser_declaration. (A #pragma at block scope is
9322 handled in cp_parser_statement.) */
9323 cp_parser_pragma (parser, pragma_external);
9324 continue;
9325 }
9326
9327 /* Parse the declaration itself. */
9328 cp_parser_declaration (parser);
9329 }
9330 }
9331
9332 /* Parse a declaration.
9333
9334 declaration:
9335 block-declaration
9336 function-definition
9337 template-declaration
9338 explicit-instantiation
9339 explicit-specialization
9340 linkage-specification
9341 namespace-definition
9342
9343 GNU extension:
9344
9345 declaration:
9346 __extension__ declaration */
9347
9348 static void
9349 cp_parser_declaration (cp_parser* parser)
9350 {
9351 cp_token token1;
9352 cp_token token2;
9353 int saved_pedantic;
9354 void *p;
9355 tree attributes = NULL_TREE;
9356
9357 /* Check for the `__extension__' keyword. */
9358 if (cp_parser_extension_opt (parser, &saved_pedantic))
9359 {
9360 /* Parse the qualified declaration. */
9361 cp_parser_declaration (parser);
9362 /* Restore the PEDANTIC flag. */
9363 pedantic = saved_pedantic;
9364
9365 return;
9366 }
9367
9368 /* Try to figure out what kind of declaration is present. */
9369 token1 = *cp_lexer_peek_token (parser->lexer);
9370
9371 if (token1.type != CPP_EOF)
9372 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9373 else
9374 {
9375 token2.type = CPP_EOF;
9376 token2.keyword = RID_MAX;
9377 }
9378
9379 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9380 p = obstack_alloc (&declarator_obstack, 0);
9381
9382 /* If the next token is `extern' and the following token is a string
9383 literal, then we have a linkage specification. */
9384 if (token1.keyword == RID_EXTERN
9385 && cp_parser_is_string_literal (&token2))
9386 cp_parser_linkage_specification (parser);
9387 /* If the next token is `template', then we have either a template
9388 declaration, an explicit instantiation, or an explicit
9389 specialization. */
9390 else if (token1.keyword == RID_TEMPLATE)
9391 {
9392 /* `template <>' indicates a template specialization. */
9393 if (token2.type == CPP_LESS
9394 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9395 cp_parser_explicit_specialization (parser);
9396 /* `template <' indicates a template declaration. */
9397 else if (token2.type == CPP_LESS)
9398 cp_parser_template_declaration (parser, /*member_p=*/false);
9399 /* Anything else must be an explicit instantiation. */
9400 else
9401 cp_parser_explicit_instantiation (parser);
9402 }
9403 /* If the next token is `export', then we have a template
9404 declaration. */
9405 else if (token1.keyword == RID_EXPORT)
9406 cp_parser_template_declaration (parser, /*member_p=*/false);
9407 /* If the next token is `extern', 'static' or 'inline' and the one
9408 after that is `template', we have a GNU extended explicit
9409 instantiation directive. */
9410 else if (cp_parser_allow_gnu_extensions_p (parser)
9411 && (token1.keyword == RID_EXTERN
9412 || token1.keyword == RID_STATIC
9413 || token1.keyword == RID_INLINE)
9414 && token2.keyword == RID_TEMPLATE)
9415 cp_parser_explicit_instantiation (parser);
9416 /* If the next token is `namespace', check for a named or unnamed
9417 namespace definition. */
9418 else if (token1.keyword == RID_NAMESPACE
9419 && (/* A named namespace definition. */
9420 (token2.type == CPP_NAME
9421 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9422 != CPP_EQ))
9423 /* An unnamed namespace definition. */
9424 || token2.type == CPP_OPEN_BRACE
9425 || token2.keyword == RID_ATTRIBUTE))
9426 cp_parser_namespace_definition (parser);
9427 /* An inline (associated) namespace definition. */
9428 else if (token1.keyword == RID_INLINE
9429 && token2.keyword == RID_NAMESPACE)
9430 cp_parser_namespace_definition (parser);
9431 /* Objective-C++ declaration/definition. */
9432 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9433 cp_parser_objc_declaration (parser, NULL_TREE);
9434 else if (c_dialect_objc ()
9435 && token1.keyword == RID_ATTRIBUTE
9436 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9437 cp_parser_objc_declaration (parser, attributes);
9438 /* We must have either a block declaration or a function
9439 definition. */
9440 else
9441 /* Try to parse a block-declaration, or a function-definition. */
9442 cp_parser_block_declaration (parser, /*statement_p=*/false);
9443
9444 /* Free any declarators allocated. */
9445 obstack_free (&declarator_obstack, p);
9446 }
9447
9448 /* Parse a block-declaration.
9449
9450 block-declaration:
9451 simple-declaration
9452 asm-definition
9453 namespace-alias-definition
9454 using-declaration
9455 using-directive
9456
9457 GNU Extension:
9458
9459 block-declaration:
9460 __extension__ block-declaration
9461
9462 C++0x Extension:
9463
9464 block-declaration:
9465 static_assert-declaration
9466
9467 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9468 part of a declaration-statement. */
9469
9470 static void
9471 cp_parser_block_declaration (cp_parser *parser,
9472 bool statement_p)
9473 {
9474 cp_token *token1;
9475 int saved_pedantic;
9476
9477 /* Check for the `__extension__' keyword. */
9478 if (cp_parser_extension_opt (parser, &saved_pedantic))
9479 {
9480 /* Parse the qualified declaration. */
9481 cp_parser_block_declaration (parser, statement_p);
9482 /* Restore the PEDANTIC flag. */
9483 pedantic = saved_pedantic;
9484
9485 return;
9486 }
9487
9488 /* Peek at the next token to figure out which kind of declaration is
9489 present. */
9490 token1 = cp_lexer_peek_token (parser->lexer);
9491
9492 /* If the next keyword is `asm', we have an asm-definition. */
9493 if (token1->keyword == RID_ASM)
9494 {
9495 if (statement_p)
9496 cp_parser_commit_to_tentative_parse (parser);
9497 cp_parser_asm_definition (parser);
9498 }
9499 /* If the next keyword is `namespace', we have a
9500 namespace-alias-definition. */
9501 else if (token1->keyword == RID_NAMESPACE)
9502 cp_parser_namespace_alias_definition (parser);
9503 /* If the next keyword is `using', we have either a
9504 using-declaration or a using-directive. */
9505 else if (token1->keyword == RID_USING)
9506 {
9507 cp_token *token2;
9508
9509 if (statement_p)
9510 cp_parser_commit_to_tentative_parse (parser);
9511 /* If the token after `using' is `namespace', then we have a
9512 using-directive. */
9513 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9514 if (token2->keyword == RID_NAMESPACE)
9515 cp_parser_using_directive (parser);
9516 /* Otherwise, it's a using-declaration. */
9517 else
9518 cp_parser_using_declaration (parser,
9519 /*access_declaration_p=*/false);
9520 }
9521 /* If the next keyword is `__label__' we have a misplaced label
9522 declaration. */
9523 else if (token1->keyword == RID_LABEL)
9524 {
9525 cp_lexer_consume_token (parser->lexer);
9526 error_at (token1->location, "%<__label__%> not at the beginning of a block");
9527 cp_parser_skip_to_end_of_statement (parser);
9528 /* If the next token is now a `;', consume it. */
9529 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9530 cp_lexer_consume_token (parser->lexer);
9531 }
9532 /* If the next token is `static_assert' we have a static assertion. */
9533 else if (token1->keyword == RID_STATIC_ASSERT)
9534 cp_parser_static_assert (parser, /*member_p=*/false);
9535 /* Anything else must be a simple-declaration. */
9536 else
9537 cp_parser_simple_declaration (parser, !statement_p);
9538 }
9539
9540 /* Parse a simple-declaration.
9541
9542 simple-declaration:
9543 decl-specifier-seq [opt] init-declarator-list [opt] ;
9544
9545 init-declarator-list:
9546 init-declarator
9547 init-declarator-list , init-declarator
9548
9549 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9550 function-definition as a simple-declaration. */
9551
9552 static void
9553 cp_parser_simple_declaration (cp_parser* parser,
9554 bool function_definition_allowed_p)
9555 {
9556 cp_decl_specifier_seq decl_specifiers;
9557 int declares_class_or_enum;
9558 bool saw_declarator;
9559
9560 /* Defer access checks until we know what is being declared; the
9561 checks for names appearing in the decl-specifier-seq should be
9562 done as if we were in the scope of the thing being declared. */
9563 push_deferring_access_checks (dk_deferred);
9564
9565 /* Parse the decl-specifier-seq. We have to keep track of whether
9566 or not the decl-specifier-seq declares a named class or
9567 enumeration type, since that is the only case in which the
9568 init-declarator-list is allowed to be empty.
9569
9570 [dcl.dcl]
9571
9572 In a simple-declaration, the optional init-declarator-list can be
9573 omitted only when declaring a class or enumeration, that is when
9574 the decl-specifier-seq contains either a class-specifier, an
9575 elaborated-type-specifier, or an enum-specifier. */
9576 cp_parser_decl_specifier_seq (parser,
9577 CP_PARSER_FLAGS_OPTIONAL,
9578 &decl_specifiers,
9579 &declares_class_or_enum);
9580 /* We no longer need to defer access checks. */
9581 stop_deferring_access_checks ();
9582
9583 /* In a block scope, a valid declaration must always have a
9584 decl-specifier-seq. By not trying to parse declarators, we can
9585 resolve the declaration/expression ambiguity more quickly. */
9586 if (!function_definition_allowed_p
9587 && !decl_specifiers.any_specifiers_p)
9588 {
9589 cp_parser_error (parser, "expected declaration");
9590 goto done;
9591 }
9592
9593 /* If the next two tokens are both identifiers, the code is
9594 erroneous. The usual cause of this situation is code like:
9595
9596 T t;
9597
9598 where "T" should name a type -- but does not. */
9599 if (!decl_specifiers.any_type_specifiers_p
9600 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9601 {
9602 /* If parsing tentatively, we should commit; we really are
9603 looking at a declaration. */
9604 cp_parser_commit_to_tentative_parse (parser);
9605 /* Give up. */
9606 goto done;
9607 }
9608
9609 /* If we have seen at least one decl-specifier, and the next token
9610 is not a parenthesis, then we must be looking at a declaration.
9611 (After "int (" we might be looking at a functional cast.) */
9612 if (decl_specifiers.any_specifiers_p
9613 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9614 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9615 && !cp_parser_error_occurred (parser))
9616 cp_parser_commit_to_tentative_parse (parser);
9617
9618 /* Keep going until we hit the `;' at the end of the simple
9619 declaration. */
9620 saw_declarator = false;
9621 while (cp_lexer_next_token_is_not (parser->lexer,
9622 CPP_SEMICOLON))
9623 {
9624 cp_token *token;
9625 bool function_definition_p;
9626 tree decl;
9627
9628 if (saw_declarator)
9629 {
9630 /* If we are processing next declarator, coma is expected */
9631 token = cp_lexer_peek_token (parser->lexer);
9632 gcc_assert (token->type == CPP_COMMA);
9633 cp_lexer_consume_token (parser->lexer);
9634 }
9635 else
9636 saw_declarator = true;
9637
9638 /* Parse the init-declarator. */
9639 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9640 /*checks=*/NULL,
9641 function_definition_allowed_p,
9642 /*member_p=*/false,
9643 declares_class_or_enum,
9644 &function_definition_p);
9645 /* If an error occurred while parsing tentatively, exit quickly.
9646 (That usually happens when in the body of a function; each
9647 statement is treated as a declaration-statement until proven
9648 otherwise.) */
9649 if (cp_parser_error_occurred (parser))
9650 goto done;
9651 /* Handle function definitions specially. */
9652 if (function_definition_p)
9653 {
9654 /* If the next token is a `,', then we are probably
9655 processing something like:
9656
9657 void f() {}, *p;
9658
9659 which is erroneous. */
9660 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9661 {
9662 cp_token *token = cp_lexer_peek_token (parser->lexer);
9663 error_at (token->location,
9664 "mixing"
9665 " declarations and function-definitions is forbidden");
9666 }
9667 /* Otherwise, we're done with the list of declarators. */
9668 else
9669 {
9670 pop_deferring_access_checks ();
9671 return;
9672 }
9673 }
9674 /* The next token should be either a `,' or a `;'. */
9675 token = cp_lexer_peek_token (parser->lexer);
9676 /* If it's a `,', there are more declarators to come. */
9677 if (token->type == CPP_COMMA)
9678 /* will be consumed next time around */;
9679 /* If it's a `;', we are done. */
9680 else if (token->type == CPP_SEMICOLON)
9681 break;
9682 /* Anything else is an error. */
9683 else
9684 {
9685 /* If we have already issued an error message we don't need
9686 to issue another one. */
9687 if (decl != error_mark_node
9688 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9689 cp_parser_error (parser, "expected %<,%> or %<;%>");
9690 /* Skip tokens until we reach the end of the statement. */
9691 cp_parser_skip_to_end_of_statement (parser);
9692 /* If the next token is now a `;', consume it. */
9693 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9694 cp_lexer_consume_token (parser->lexer);
9695 goto done;
9696 }
9697 /* After the first time around, a function-definition is not
9698 allowed -- even if it was OK at first. For example:
9699
9700 int i, f() {}
9701
9702 is not valid. */
9703 function_definition_allowed_p = false;
9704 }
9705
9706 /* Issue an error message if no declarators are present, and the
9707 decl-specifier-seq does not itself declare a class or
9708 enumeration. */
9709 if (!saw_declarator)
9710 {
9711 if (cp_parser_declares_only_class_p (parser))
9712 shadow_tag (&decl_specifiers);
9713 /* Perform any deferred access checks. */
9714 perform_deferred_access_checks ();
9715 }
9716
9717 /* Consume the `;'. */
9718 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9719
9720 done:
9721 pop_deferring_access_checks ();
9722 }
9723
9724 /* Parse a decl-specifier-seq.
9725
9726 decl-specifier-seq:
9727 decl-specifier-seq [opt] decl-specifier
9728
9729 decl-specifier:
9730 storage-class-specifier
9731 type-specifier
9732 function-specifier
9733 friend
9734 typedef
9735
9736 GNU Extension:
9737
9738 decl-specifier:
9739 attributes
9740
9741 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9742
9743 The parser flags FLAGS is used to control type-specifier parsing.
9744
9745 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9746 flags:
9747
9748 1: one of the decl-specifiers is an elaborated-type-specifier
9749 (i.e., a type declaration)
9750 2: one of the decl-specifiers is an enum-specifier or a
9751 class-specifier (i.e., a type definition)
9752
9753 */
9754
9755 static void
9756 cp_parser_decl_specifier_seq (cp_parser* parser,
9757 cp_parser_flags flags,
9758 cp_decl_specifier_seq *decl_specs,
9759 int* declares_class_or_enum)
9760 {
9761 bool constructor_possible_p = !parser->in_declarator_p;
9762 cp_token *start_token = NULL;
9763
9764 /* Clear DECL_SPECS. */
9765 clear_decl_specs (decl_specs);
9766
9767 /* Assume no class or enumeration type is declared. */
9768 *declares_class_or_enum = 0;
9769
9770 /* Keep reading specifiers until there are no more to read. */
9771 while (true)
9772 {
9773 bool constructor_p;
9774 bool found_decl_spec;
9775 cp_token *token;
9776
9777 /* Peek at the next token. */
9778 token = cp_lexer_peek_token (parser->lexer);
9779
9780 /* Save the first token of the decl spec list for error
9781 reporting. */
9782 if (!start_token)
9783 start_token = token;
9784 /* Handle attributes. */
9785 if (token->keyword == RID_ATTRIBUTE)
9786 {
9787 /* Parse the attributes. */
9788 decl_specs->attributes
9789 = chainon (decl_specs->attributes,
9790 cp_parser_attributes_opt (parser));
9791 continue;
9792 }
9793 /* Assume we will find a decl-specifier keyword. */
9794 found_decl_spec = true;
9795 /* If the next token is an appropriate keyword, we can simply
9796 add it to the list. */
9797 switch (token->keyword)
9798 {
9799 /* decl-specifier:
9800 friend
9801 constexpr */
9802 case RID_FRIEND:
9803 if (!at_class_scope_p ())
9804 {
9805 error_at (token->location, "%<friend%> used outside of class");
9806 cp_lexer_purge_token (parser->lexer);
9807 }
9808 else
9809 {
9810 ++decl_specs->specs[(int) ds_friend];
9811 /* Consume the token. */
9812 cp_lexer_consume_token (parser->lexer);
9813 }
9814 break;
9815
9816 case RID_CONSTEXPR:
9817 ++decl_specs->specs[(int) ds_constexpr];
9818 cp_lexer_consume_token (parser->lexer);
9819 break;
9820
9821 /* function-specifier:
9822 inline
9823 virtual
9824 explicit */
9825 case RID_INLINE:
9826 case RID_VIRTUAL:
9827 case RID_EXPLICIT:
9828 cp_parser_function_specifier_opt (parser, decl_specs);
9829 break;
9830
9831 /* decl-specifier:
9832 typedef */
9833 case RID_TYPEDEF:
9834 ++decl_specs->specs[(int) ds_typedef];
9835 /* Consume the token. */
9836 cp_lexer_consume_token (parser->lexer);
9837 /* A constructor declarator cannot appear in a typedef. */
9838 constructor_possible_p = false;
9839 /* The "typedef" keyword can only occur in a declaration; we
9840 may as well commit at this point. */
9841 cp_parser_commit_to_tentative_parse (parser);
9842
9843 if (decl_specs->storage_class != sc_none)
9844 decl_specs->conflicting_specifiers_p = true;
9845 break;
9846
9847 /* storage-class-specifier:
9848 auto
9849 register
9850 static
9851 extern
9852 mutable
9853
9854 GNU Extension:
9855 thread */
9856 case RID_AUTO:
9857 if (cxx_dialect == cxx98)
9858 {
9859 /* Consume the token. */
9860 cp_lexer_consume_token (parser->lexer);
9861
9862 /* Complain about `auto' as a storage specifier, if
9863 we're complaining about C++0x compatibility. */
9864 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9865 " will change meaning in C++0x; please remove it");
9866
9867 /* Set the storage class anyway. */
9868 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9869 token->location);
9870 }
9871 else
9872 /* C++0x auto type-specifier. */
9873 found_decl_spec = false;
9874 break;
9875
9876 case RID_REGISTER:
9877 case RID_STATIC:
9878 case RID_EXTERN:
9879 case RID_MUTABLE:
9880 /* Consume the token. */
9881 cp_lexer_consume_token (parser->lexer);
9882 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9883 token->location);
9884 break;
9885 case RID_THREAD:
9886 /* Consume the token. */
9887 cp_lexer_consume_token (parser->lexer);
9888 ++decl_specs->specs[(int) ds_thread];
9889 break;
9890
9891 default:
9892 /* We did not yet find a decl-specifier yet. */
9893 found_decl_spec = false;
9894 break;
9895 }
9896
9897 if (found_decl_spec
9898 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9899 && token->keyword != RID_CONSTEXPR)
9900 error ("decl-specifier invalid in condition");
9901
9902 /* Constructors are a special case. The `S' in `S()' is not a
9903 decl-specifier; it is the beginning of the declarator. */
9904 constructor_p
9905 = (!found_decl_spec
9906 && constructor_possible_p
9907 && (cp_parser_constructor_declarator_p
9908 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9909
9910 /* If we don't have a DECL_SPEC yet, then we must be looking at
9911 a type-specifier. */
9912 if (!found_decl_spec && !constructor_p)
9913 {
9914 int decl_spec_declares_class_or_enum;
9915 bool is_cv_qualifier;
9916 tree type_spec;
9917
9918 type_spec
9919 = cp_parser_type_specifier (parser, flags,
9920 decl_specs,
9921 /*is_declaration=*/true,
9922 &decl_spec_declares_class_or_enum,
9923 &is_cv_qualifier);
9924 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9925
9926 /* If this type-specifier referenced a user-defined type
9927 (a typedef, class-name, etc.), then we can't allow any
9928 more such type-specifiers henceforth.
9929
9930 [dcl.spec]
9931
9932 The longest sequence of decl-specifiers that could
9933 possibly be a type name is taken as the
9934 decl-specifier-seq of a declaration. The sequence shall
9935 be self-consistent as described below.
9936
9937 [dcl.type]
9938
9939 As a general rule, at most one type-specifier is allowed
9940 in the complete decl-specifier-seq of a declaration. The
9941 only exceptions are the following:
9942
9943 -- const or volatile can be combined with any other
9944 type-specifier.
9945
9946 -- signed or unsigned can be combined with char, long,
9947 short, or int.
9948
9949 -- ..
9950
9951 Example:
9952
9953 typedef char* Pc;
9954 void g (const int Pc);
9955
9956 Here, Pc is *not* part of the decl-specifier seq; it's
9957 the declarator. Therefore, once we see a type-specifier
9958 (other than a cv-qualifier), we forbid any additional
9959 user-defined types. We *do* still allow things like `int
9960 int' to be considered a decl-specifier-seq, and issue the
9961 error message later. */
9962 if (type_spec && !is_cv_qualifier)
9963 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9964 /* A constructor declarator cannot follow a type-specifier. */
9965 if (type_spec)
9966 {
9967 constructor_possible_p = false;
9968 found_decl_spec = true;
9969 if (!is_cv_qualifier)
9970 decl_specs->any_type_specifiers_p = true;
9971 }
9972 }
9973
9974 /* If we still do not have a DECL_SPEC, then there are no more
9975 decl-specifiers. */
9976 if (!found_decl_spec)
9977 break;
9978
9979 decl_specs->any_specifiers_p = true;
9980 /* After we see one decl-specifier, further decl-specifiers are
9981 always optional. */
9982 flags |= CP_PARSER_FLAGS_OPTIONAL;
9983 }
9984
9985 cp_parser_check_decl_spec (decl_specs, start_token->location);
9986
9987 /* Don't allow a friend specifier with a class definition. */
9988 if (decl_specs->specs[(int) ds_friend] != 0
9989 && (*declares_class_or_enum & 2))
9990 error_at (start_token->location,
9991 "class definition may not be declared a friend");
9992 }
9993
9994 /* Parse an (optional) storage-class-specifier.
9995
9996 storage-class-specifier:
9997 auto
9998 register
9999 static
10000 extern
10001 mutable
10002
10003 GNU Extension:
10004
10005 storage-class-specifier:
10006 thread
10007
10008 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10009
10010 static tree
10011 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10012 {
10013 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10014 {
10015 case RID_AUTO:
10016 if (cxx_dialect != cxx98)
10017 return NULL_TREE;
10018 /* Fall through for C++98. */
10019
10020 case RID_REGISTER:
10021 case RID_STATIC:
10022 case RID_EXTERN:
10023 case RID_MUTABLE:
10024 case RID_THREAD:
10025 /* Consume the token. */
10026 return cp_lexer_consume_token (parser->lexer)->u.value;
10027
10028 default:
10029 return NULL_TREE;
10030 }
10031 }
10032
10033 /* Parse an (optional) function-specifier.
10034
10035 function-specifier:
10036 inline
10037 virtual
10038 explicit
10039
10040 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10041 Updates DECL_SPECS, if it is non-NULL. */
10042
10043 static tree
10044 cp_parser_function_specifier_opt (cp_parser* parser,
10045 cp_decl_specifier_seq *decl_specs)
10046 {
10047 cp_token *token = cp_lexer_peek_token (parser->lexer);
10048 switch (token->keyword)
10049 {
10050 case RID_INLINE:
10051 if (decl_specs)
10052 ++decl_specs->specs[(int) ds_inline];
10053 break;
10054
10055 case RID_VIRTUAL:
10056 /* 14.5.2.3 [temp.mem]
10057
10058 A member function template shall not be virtual. */
10059 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10060 error_at (token->location, "templates may not be %<virtual%>");
10061 else if (decl_specs)
10062 ++decl_specs->specs[(int) ds_virtual];
10063 break;
10064
10065 case RID_EXPLICIT:
10066 if (decl_specs)
10067 ++decl_specs->specs[(int) ds_explicit];
10068 break;
10069
10070 default:
10071 return NULL_TREE;
10072 }
10073
10074 /* Consume the token. */
10075 return cp_lexer_consume_token (parser->lexer)->u.value;
10076 }
10077
10078 /* Parse a linkage-specification.
10079
10080 linkage-specification:
10081 extern string-literal { declaration-seq [opt] }
10082 extern string-literal declaration */
10083
10084 static void
10085 cp_parser_linkage_specification (cp_parser* parser)
10086 {
10087 tree linkage;
10088
10089 /* Look for the `extern' keyword. */
10090 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10091
10092 /* Look for the string-literal. */
10093 linkage = cp_parser_string_literal (parser, false, false);
10094
10095 /* Transform the literal into an identifier. If the literal is a
10096 wide-character string, or contains embedded NULs, then we can't
10097 handle it as the user wants. */
10098 if (strlen (TREE_STRING_POINTER (linkage))
10099 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10100 {
10101 cp_parser_error (parser, "invalid linkage-specification");
10102 /* Assume C++ linkage. */
10103 linkage = lang_name_cplusplus;
10104 }
10105 else
10106 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10107
10108 /* We're now using the new linkage. */
10109 push_lang_context (linkage);
10110
10111 /* If the next token is a `{', then we're using the first
10112 production. */
10113 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10114 {
10115 /* Consume the `{' token. */
10116 cp_lexer_consume_token (parser->lexer);
10117 /* Parse the declarations. */
10118 cp_parser_declaration_seq_opt (parser);
10119 /* Look for the closing `}'. */
10120 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10121 }
10122 /* Otherwise, there's just one declaration. */
10123 else
10124 {
10125 bool saved_in_unbraced_linkage_specification_p;
10126
10127 saved_in_unbraced_linkage_specification_p
10128 = parser->in_unbraced_linkage_specification_p;
10129 parser->in_unbraced_linkage_specification_p = true;
10130 cp_parser_declaration (parser);
10131 parser->in_unbraced_linkage_specification_p
10132 = saved_in_unbraced_linkage_specification_p;
10133 }
10134
10135 /* We're done with the linkage-specification. */
10136 pop_lang_context ();
10137 }
10138
10139 /* Parse a static_assert-declaration.
10140
10141 static_assert-declaration:
10142 static_assert ( constant-expression , string-literal ) ;
10143
10144 If MEMBER_P, this static_assert is a class member. */
10145
10146 static void
10147 cp_parser_static_assert(cp_parser *parser, bool member_p)
10148 {
10149 tree condition;
10150 tree message;
10151 cp_token *token;
10152 location_t saved_loc;
10153
10154 /* Peek at the `static_assert' token so we can keep track of exactly
10155 where the static assertion started. */
10156 token = cp_lexer_peek_token (parser->lexer);
10157 saved_loc = token->location;
10158
10159 /* Look for the `static_assert' keyword. */
10160 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10161 RT_STATIC_ASSERT))
10162 return;
10163
10164 /* We know we are in a static assertion; commit to any tentative
10165 parse. */
10166 if (cp_parser_parsing_tentatively (parser))
10167 cp_parser_commit_to_tentative_parse (parser);
10168
10169 /* Parse the `(' starting the static assertion condition. */
10170 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10171
10172 /* Parse the constant-expression. */
10173 condition =
10174 cp_parser_constant_expression (parser,
10175 /*allow_non_constant_p=*/false,
10176 /*non_constant_p=*/NULL);
10177
10178 /* Parse the separating `,'. */
10179 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10180
10181 /* Parse the string-literal message. */
10182 message = cp_parser_string_literal (parser,
10183 /*translate=*/false,
10184 /*wide_ok=*/true);
10185
10186 /* A `)' completes the static assertion. */
10187 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10188 cp_parser_skip_to_closing_parenthesis (parser,
10189 /*recovering=*/true,
10190 /*or_comma=*/false,
10191 /*consume_paren=*/true);
10192
10193 /* A semicolon terminates the declaration. */
10194 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10195
10196 /* Complete the static assertion, which may mean either processing
10197 the static assert now or saving it for template instantiation. */
10198 finish_static_assert (condition, message, saved_loc, member_p);
10199 }
10200
10201 /* Parse a `decltype' type. Returns the type.
10202
10203 simple-type-specifier:
10204 decltype ( expression ) */
10205
10206 static tree
10207 cp_parser_decltype (cp_parser *parser)
10208 {
10209 tree expr;
10210 bool id_expression_or_member_access_p = false;
10211 const char *saved_message;
10212 bool saved_integral_constant_expression_p;
10213 bool saved_non_integral_constant_expression_p;
10214 cp_token *id_expr_start_token;
10215
10216 /* Look for the `decltype' token. */
10217 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10218 return error_mark_node;
10219
10220 /* Types cannot be defined in a `decltype' expression. Save away the
10221 old message. */
10222 saved_message = parser->type_definition_forbidden_message;
10223
10224 /* And create the new one. */
10225 parser->type_definition_forbidden_message
10226 = G_("types may not be defined in %<decltype%> expressions");
10227
10228 /* The restrictions on constant-expressions do not apply inside
10229 decltype expressions. */
10230 saved_integral_constant_expression_p
10231 = parser->integral_constant_expression_p;
10232 saved_non_integral_constant_expression_p
10233 = parser->non_integral_constant_expression_p;
10234 parser->integral_constant_expression_p = false;
10235
10236 /* Do not actually evaluate the expression. */
10237 ++cp_unevaluated_operand;
10238
10239 /* Do not warn about problems with the expression. */
10240 ++c_inhibit_evaluation_warnings;
10241
10242 /* Parse the opening `('. */
10243 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10244 return error_mark_node;
10245
10246 /* First, try parsing an id-expression. */
10247 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10248 cp_parser_parse_tentatively (parser);
10249 expr = cp_parser_id_expression (parser,
10250 /*template_keyword_p=*/false,
10251 /*check_dependency_p=*/true,
10252 /*template_p=*/NULL,
10253 /*declarator_p=*/false,
10254 /*optional_p=*/false);
10255
10256 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10257 {
10258 bool non_integral_constant_expression_p = false;
10259 tree id_expression = expr;
10260 cp_id_kind idk;
10261 const char *error_msg;
10262
10263 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10264 /* Lookup the name we got back from the id-expression. */
10265 expr = cp_parser_lookup_name (parser, expr,
10266 none_type,
10267 /*is_template=*/false,
10268 /*is_namespace=*/false,
10269 /*check_dependency=*/true,
10270 /*ambiguous_decls=*/NULL,
10271 id_expr_start_token->location);
10272
10273 if (expr
10274 && expr != error_mark_node
10275 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10276 && TREE_CODE (expr) != TYPE_DECL
10277 && (TREE_CODE (expr) != BIT_NOT_EXPR
10278 || !TYPE_P (TREE_OPERAND (expr, 0)))
10279 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10280 {
10281 /* Complete lookup of the id-expression. */
10282 expr = (finish_id_expression
10283 (id_expression, expr, parser->scope, &idk,
10284 /*integral_constant_expression_p=*/false,
10285 /*allow_non_integral_constant_expression_p=*/true,
10286 &non_integral_constant_expression_p,
10287 /*template_p=*/false,
10288 /*done=*/true,
10289 /*address_p=*/false,
10290 /*template_arg_p=*/false,
10291 &error_msg,
10292 id_expr_start_token->location));
10293
10294 if (expr == error_mark_node)
10295 /* We found an id-expression, but it was something that we
10296 should not have found. This is an error, not something
10297 we can recover from, so note that we found an
10298 id-expression and we'll recover as gracefully as
10299 possible. */
10300 id_expression_or_member_access_p = true;
10301 }
10302
10303 if (expr
10304 && expr != error_mark_node
10305 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10306 /* We have an id-expression. */
10307 id_expression_or_member_access_p = true;
10308 }
10309
10310 if (!id_expression_or_member_access_p)
10311 {
10312 /* Abort the id-expression parse. */
10313 cp_parser_abort_tentative_parse (parser);
10314
10315 /* Parsing tentatively, again. */
10316 cp_parser_parse_tentatively (parser);
10317
10318 /* Parse a class member access. */
10319 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10320 /*cast_p=*/false,
10321 /*member_access_only_p=*/true, NULL);
10322
10323 if (expr
10324 && expr != error_mark_node
10325 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10326 /* We have an id-expression. */
10327 id_expression_or_member_access_p = true;
10328 }
10329
10330 if (id_expression_or_member_access_p)
10331 /* We have parsed the complete id-expression or member access. */
10332 cp_parser_parse_definitely (parser);
10333 else
10334 {
10335 bool saved_greater_than_is_operator_p;
10336
10337 /* Abort our attempt to parse an id-expression or member access
10338 expression. */
10339 cp_parser_abort_tentative_parse (parser);
10340
10341 /* Within a parenthesized expression, a `>' token is always
10342 the greater-than operator. */
10343 saved_greater_than_is_operator_p
10344 = parser->greater_than_is_operator_p;
10345 parser->greater_than_is_operator_p = true;
10346
10347 /* Parse a full expression. */
10348 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10349
10350 /* The `>' token might be the end of a template-id or
10351 template-parameter-list now. */
10352 parser->greater_than_is_operator_p
10353 = saved_greater_than_is_operator_p;
10354 }
10355
10356 /* Go back to evaluating expressions. */
10357 --cp_unevaluated_operand;
10358 --c_inhibit_evaluation_warnings;
10359
10360 /* Restore the old message and the integral constant expression
10361 flags. */
10362 parser->type_definition_forbidden_message = saved_message;
10363 parser->integral_constant_expression_p
10364 = saved_integral_constant_expression_p;
10365 parser->non_integral_constant_expression_p
10366 = saved_non_integral_constant_expression_p;
10367
10368 if (expr == error_mark_node)
10369 {
10370 /* Skip everything up to the closing `)'. */
10371 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10372 /*consume_paren=*/true);
10373 return error_mark_node;
10374 }
10375
10376 /* Parse to the closing `)'. */
10377 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10378 {
10379 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10380 /*consume_paren=*/true);
10381 return error_mark_node;
10382 }
10383
10384 return finish_decltype_type (expr, id_expression_or_member_access_p);
10385 }
10386
10387 /* Special member functions [gram.special] */
10388
10389 /* Parse a conversion-function-id.
10390
10391 conversion-function-id:
10392 operator conversion-type-id
10393
10394 Returns an IDENTIFIER_NODE representing the operator. */
10395
10396 static tree
10397 cp_parser_conversion_function_id (cp_parser* parser)
10398 {
10399 tree type;
10400 tree saved_scope;
10401 tree saved_qualifying_scope;
10402 tree saved_object_scope;
10403 tree pushed_scope = NULL_TREE;
10404
10405 /* Look for the `operator' token. */
10406 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10407 return error_mark_node;
10408 /* When we parse the conversion-type-id, the current scope will be
10409 reset. However, we need that information in able to look up the
10410 conversion function later, so we save it here. */
10411 saved_scope = parser->scope;
10412 saved_qualifying_scope = parser->qualifying_scope;
10413 saved_object_scope = parser->object_scope;
10414 /* We must enter the scope of the class so that the names of
10415 entities declared within the class are available in the
10416 conversion-type-id. For example, consider:
10417
10418 struct S {
10419 typedef int I;
10420 operator I();
10421 };
10422
10423 S::operator I() { ... }
10424
10425 In order to see that `I' is a type-name in the definition, we
10426 must be in the scope of `S'. */
10427 if (saved_scope)
10428 pushed_scope = push_scope (saved_scope);
10429 /* Parse the conversion-type-id. */
10430 type = cp_parser_conversion_type_id (parser);
10431 /* Leave the scope of the class, if any. */
10432 if (pushed_scope)
10433 pop_scope (pushed_scope);
10434 /* Restore the saved scope. */
10435 parser->scope = saved_scope;
10436 parser->qualifying_scope = saved_qualifying_scope;
10437 parser->object_scope = saved_object_scope;
10438 /* If the TYPE is invalid, indicate failure. */
10439 if (type == error_mark_node)
10440 return error_mark_node;
10441 return mangle_conv_op_name_for_type (type);
10442 }
10443
10444 /* Parse a conversion-type-id:
10445
10446 conversion-type-id:
10447 type-specifier-seq conversion-declarator [opt]
10448
10449 Returns the TYPE specified. */
10450
10451 static tree
10452 cp_parser_conversion_type_id (cp_parser* parser)
10453 {
10454 tree attributes;
10455 cp_decl_specifier_seq type_specifiers;
10456 cp_declarator *declarator;
10457 tree type_specified;
10458
10459 /* Parse the attributes. */
10460 attributes = cp_parser_attributes_opt (parser);
10461 /* Parse the type-specifiers. */
10462 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10463 /*is_trailing_return=*/false,
10464 &type_specifiers);
10465 /* If that didn't work, stop. */
10466 if (type_specifiers.type == error_mark_node)
10467 return error_mark_node;
10468 /* Parse the conversion-declarator. */
10469 declarator = cp_parser_conversion_declarator_opt (parser);
10470
10471 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10472 /*initialized=*/0, &attributes);
10473 if (attributes)
10474 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10475
10476 /* Don't give this error when parsing tentatively. This happens to
10477 work because we always parse this definitively once. */
10478 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10479 && type_uses_auto (type_specified))
10480 {
10481 error ("invalid use of %<auto%> in conversion operator");
10482 return error_mark_node;
10483 }
10484
10485 return type_specified;
10486 }
10487
10488 /* Parse an (optional) conversion-declarator.
10489
10490 conversion-declarator:
10491 ptr-operator conversion-declarator [opt]
10492
10493 */
10494
10495 static cp_declarator *
10496 cp_parser_conversion_declarator_opt (cp_parser* parser)
10497 {
10498 enum tree_code code;
10499 tree class_type;
10500 cp_cv_quals cv_quals;
10501
10502 /* We don't know if there's a ptr-operator next, or not. */
10503 cp_parser_parse_tentatively (parser);
10504 /* Try the ptr-operator. */
10505 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10506 /* If it worked, look for more conversion-declarators. */
10507 if (cp_parser_parse_definitely (parser))
10508 {
10509 cp_declarator *declarator;
10510
10511 /* Parse another optional declarator. */
10512 declarator = cp_parser_conversion_declarator_opt (parser);
10513
10514 return cp_parser_make_indirect_declarator
10515 (code, class_type, cv_quals, declarator);
10516 }
10517
10518 return NULL;
10519 }
10520
10521 /* Parse an (optional) ctor-initializer.
10522
10523 ctor-initializer:
10524 : mem-initializer-list
10525
10526 Returns TRUE iff the ctor-initializer was actually present. */
10527
10528 static bool
10529 cp_parser_ctor_initializer_opt (cp_parser* parser)
10530 {
10531 /* If the next token is not a `:', then there is no
10532 ctor-initializer. */
10533 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10534 {
10535 /* Do default initialization of any bases and members. */
10536 if (DECL_CONSTRUCTOR_P (current_function_decl))
10537 finish_mem_initializers (NULL_TREE);
10538
10539 return false;
10540 }
10541
10542 /* Consume the `:' token. */
10543 cp_lexer_consume_token (parser->lexer);
10544 /* And the mem-initializer-list. */
10545 cp_parser_mem_initializer_list (parser);
10546
10547 return true;
10548 }
10549
10550 /* Parse a mem-initializer-list.
10551
10552 mem-initializer-list:
10553 mem-initializer ... [opt]
10554 mem-initializer ... [opt] , mem-initializer-list */
10555
10556 static void
10557 cp_parser_mem_initializer_list (cp_parser* parser)
10558 {
10559 tree mem_initializer_list = NULL_TREE;
10560 cp_token *token = cp_lexer_peek_token (parser->lexer);
10561
10562 /* Let the semantic analysis code know that we are starting the
10563 mem-initializer-list. */
10564 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10565 error_at (token->location,
10566 "only constructors take member initializers");
10567
10568 /* Loop through the list. */
10569 while (true)
10570 {
10571 tree mem_initializer;
10572
10573 token = cp_lexer_peek_token (parser->lexer);
10574 /* Parse the mem-initializer. */
10575 mem_initializer = cp_parser_mem_initializer (parser);
10576 /* If the next token is a `...', we're expanding member initializers. */
10577 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10578 {
10579 /* Consume the `...'. */
10580 cp_lexer_consume_token (parser->lexer);
10581
10582 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10583 can be expanded but members cannot. */
10584 if (mem_initializer != error_mark_node
10585 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10586 {
10587 error_at (token->location,
10588 "cannot expand initializer for member %<%D%>",
10589 TREE_PURPOSE (mem_initializer));
10590 mem_initializer = error_mark_node;
10591 }
10592
10593 /* Construct the pack expansion type. */
10594 if (mem_initializer != error_mark_node)
10595 mem_initializer = make_pack_expansion (mem_initializer);
10596 }
10597 /* Add it to the list, unless it was erroneous. */
10598 if (mem_initializer != error_mark_node)
10599 {
10600 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10601 mem_initializer_list = mem_initializer;
10602 }
10603 /* If the next token is not a `,', we're done. */
10604 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10605 break;
10606 /* Consume the `,' token. */
10607 cp_lexer_consume_token (parser->lexer);
10608 }
10609
10610 /* Perform semantic analysis. */
10611 if (DECL_CONSTRUCTOR_P (current_function_decl))
10612 finish_mem_initializers (mem_initializer_list);
10613 }
10614
10615 /* Parse a mem-initializer.
10616
10617 mem-initializer:
10618 mem-initializer-id ( expression-list [opt] )
10619 mem-initializer-id braced-init-list
10620
10621 GNU extension:
10622
10623 mem-initializer:
10624 ( expression-list [opt] )
10625
10626 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10627 class) or FIELD_DECL (for a non-static data member) to initialize;
10628 the TREE_VALUE is the expression-list. An empty initialization
10629 list is represented by void_list_node. */
10630
10631 static tree
10632 cp_parser_mem_initializer (cp_parser* parser)
10633 {
10634 tree mem_initializer_id;
10635 tree expression_list;
10636 tree member;
10637 cp_token *token = cp_lexer_peek_token (parser->lexer);
10638
10639 /* Find out what is being initialized. */
10640 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10641 {
10642 permerror (token->location,
10643 "anachronistic old-style base class initializer");
10644 mem_initializer_id = NULL_TREE;
10645 }
10646 else
10647 {
10648 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10649 if (mem_initializer_id == error_mark_node)
10650 return mem_initializer_id;
10651 }
10652 member = expand_member_init (mem_initializer_id);
10653 if (member && !DECL_P (member))
10654 in_base_initializer = 1;
10655
10656 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10657 {
10658 bool expr_non_constant_p;
10659 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10660 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10661 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10662 expression_list = build_tree_list (NULL_TREE, expression_list);
10663 }
10664 else
10665 {
10666 VEC(tree,gc)* vec;
10667 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10668 /*cast_p=*/false,
10669 /*allow_expansion_p=*/true,
10670 /*non_constant_p=*/NULL);
10671 if (vec == NULL)
10672 return error_mark_node;
10673 expression_list = build_tree_list_vec (vec);
10674 release_tree_vector (vec);
10675 }
10676
10677 if (expression_list == error_mark_node)
10678 return error_mark_node;
10679 if (!expression_list)
10680 expression_list = void_type_node;
10681
10682 in_base_initializer = 0;
10683
10684 return member ? build_tree_list (member, expression_list) : error_mark_node;
10685 }
10686
10687 /* Parse a mem-initializer-id.
10688
10689 mem-initializer-id:
10690 :: [opt] nested-name-specifier [opt] class-name
10691 identifier
10692
10693 Returns a TYPE indicating the class to be initializer for the first
10694 production. Returns an IDENTIFIER_NODE indicating the data member
10695 to be initialized for the second production. */
10696
10697 static tree
10698 cp_parser_mem_initializer_id (cp_parser* parser)
10699 {
10700 bool global_scope_p;
10701 bool nested_name_specifier_p;
10702 bool template_p = false;
10703 tree id;
10704
10705 cp_token *token = cp_lexer_peek_token (parser->lexer);
10706
10707 /* `typename' is not allowed in this context ([temp.res]). */
10708 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10709 {
10710 error_at (token->location,
10711 "keyword %<typename%> not allowed in this context (a qualified "
10712 "member initializer is implicitly a type)");
10713 cp_lexer_consume_token (parser->lexer);
10714 }
10715 /* Look for the optional `::' operator. */
10716 global_scope_p
10717 = (cp_parser_global_scope_opt (parser,
10718 /*current_scope_valid_p=*/false)
10719 != NULL_TREE);
10720 /* Look for the optional nested-name-specifier. The simplest way to
10721 implement:
10722
10723 [temp.res]
10724
10725 The keyword `typename' is not permitted in a base-specifier or
10726 mem-initializer; in these contexts a qualified name that
10727 depends on a template-parameter is implicitly assumed to be a
10728 type name.
10729
10730 is to assume that we have seen the `typename' keyword at this
10731 point. */
10732 nested_name_specifier_p
10733 = (cp_parser_nested_name_specifier_opt (parser,
10734 /*typename_keyword_p=*/true,
10735 /*check_dependency_p=*/true,
10736 /*type_p=*/true,
10737 /*is_declaration=*/true)
10738 != NULL_TREE);
10739 if (nested_name_specifier_p)
10740 template_p = cp_parser_optional_template_keyword (parser);
10741 /* If there is a `::' operator or a nested-name-specifier, then we
10742 are definitely looking for a class-name. */
10743 if (global_scope_p || nested_name_specifier_p)
10744 return cp_parser_class_name (parser,
10745 /*typename_keyword_p=*/true,
10746 /*template_keyword_p=*/template_p,
10747 typename_type,
10748 /*check_dependency_p=*/true,
10749 /*class_head_p=*/false,
10750 /*is_declaration=*/true);
10751 /* Otherwise, we could also be looking for an ordinary identifier. */
10752 cp_parser_parse_tentatively (parser);
10753 /* Try a class-name. */
10754 id = cp_parser_class_name (parser,
10755 /*typename_keyword_p=*/true,
10756 /*template_keyword_p=*/false,
10757 none_type,
10758 /*check_dependency_p=*/true,
10759 /*class_head_p=*/false,
10760 /*is_declaration=*/true);
10761 /* If we found one, we're done. */
10762 if (cp_parser_parse_definitely (parser))
10763 return id;
10764 /* Otherwise, look for an ordinary identifier. */
10765 return cp_parser_identifier (parser);
10766 }
10767
10768 /* Overloading [gram.over] */
10769
10770 /* Parse an operator-function-id.
10771
10772 operator-function-id:
10773 operator operator
10774
10775 Returns an IDENTIFIER_NODE for the operator which is a
10776 human-readable spelling of the identifier, e.g., `operator +'. */
10777
10778 static tree
10779 cp_parser_operator_function_id (cp_parser* parser)
10780 {
10781 /* Look for the `operator' keyword. */
10782 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10783 return error_mark_node;
10784 /* And then the name of the operator itself. */
10785 return cp_parser_operator (parser);
10786 }
10787
10788 /* Parse an operator.
10789
10790 operator:
10791 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10792 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10793 || ++ -- , ->* -> () []
10794
10795 GNU Extensions:
10796
10797 operator:
10798 <? >? <?= >?=
10799
10800 Returns an IDENTIFIER_NODE for the operator which is a
10801 human-readable spelling of the identifier, e.g., `operator +'. */
10802
10803 static tree
10804 cp_parser_operator (cp_parser* parser)
10805 {
10806 tree id = NULL_TREE;
10807 cp_token *token;
10808
10809 /* Peek at the next token. */
10810 token = cp_lexer_peek_token (parser->lexer);
10811 /* Figure out which operator we have. */
10812 switch (token->type)
10813 {
10814 case CPP_KEYWORD:
10815 {
10816 enum tree_code op;
10817
10818 /* The keyword should be either `new' or `delete'. */
10819 if (token->keyword == RID_NEW)
10820 op = NEW_EXPR;
10821 else if (token->keyword == RID_DELETE)
10822 op = DELETE_EXPR;
10823 else
10824 break;
10825
10826 /* Consume the `new' or `delete' token. */
10827 cp_lexer_consume_token (parser->lexer);
10828
10829 /* Peek at the next token. */
10830 token = cp_lexer_peek_token (parser->lexer);
10831 /* If it's a `[' token then this is the array variant of the
10832 operator. */
10833 if (token->type == CPP_OPEN_SQUARE)
10834 {
10835 /* Consume the `[' token. */
10836 cp_lexer_consume_token (parser->lexer);
10837 /* Look for the `]' token. */
10838 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10839 id = ansi_opname (op == NEW_EXPR
10840 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10841 }
10842 /* Otherwise, we have the non-array variant. */
10843 else
10844 id = ansi_opname (op);
10845
10846 return id;
10847 }
10848
10849 case CPP_PLUS:
10850 id = ansi_opname (PLUS_EXPR);
10851 break;
10852
10853 case CPP_MINUS:
10854 id = ansi_opname (MINUS_EXPR);
10855 break;
10856
10857 case CPP_MULT:
10858 id = ansi_opname (MULT_EXPR);
10859 break;
10860
10861 case CPP_DIV:
10862 id = ansi_opname (TRUNC_DIV_EXPR);
10863 break;
10864
10865 case CPP_MOD:
10866 id = ansi_opname (TRUNC_MOD_EXPR);
10867 break;
10868
10869 case CPP_XOR:
10870 id = ansi_opname (BIT_XOR_EXPR);
10871 break;
10872
10873 case CPP_AND:
10874 id = ansi_opname (BIT_AND_EXPR);
10875 break;
10876
10877 case CPP_OR:
10878 id = ansi_opname (BIT_IOR_EXPR);
10879 break;
10880
10881 case CPP_COMPL:
10882 id = ansi_opname (BIT_NOT_EXPR);
10883 break;
10884
10885 case CPP_NOT:
10886 id = ansi_opname (TRUTH_NOT_EXPR);
10887 break;
10888
10889 case CPP_EQ:
10890 id = ansi_assopname (NOP_EXPR);
10891 break;
10892
10893 case CPP_LESS:
10894 id = ansi_opname (LT_EXPR);
10895 break;
10896
10897 case CPP_GREATER:
10898 id = ansi_opname (GT_EXPR);
10899 break;
10900
10901 case CPP_PLUS_EQ:
10902 id = ansi_assopname (PLUS_EXPR);
10903 break;
10904
10905 case CPP_MINUS_EQ:
10906 id = ansi_assopname (MINUS_EXPR);
10907 break;
10908
10909 case CPP_MULT_EQ:
10910 id = ansi_assopname (MULT_EXPR);
10911 break;
10912
10913 case CPP_DIV_EQ:
10914 id = ansi_assopname (TRUNC_DIV_EXPR);
10915 break;
10916
10917 case CPP_MOD_EQ:
10918 id = ansi_assopname (TRUNC_MOD_EXPR);
10919 break;
10920
10921 case CPP_XOR_EQ:
10922 id = ansi_assopname (BIT_XOR_EXPR);
10923 break;
10924
10925 case CPP_AND_EQ:
10926 id = ansi_assopname (BIT_AND_EXPR);
10927 break;
10928
10929 case CPP_OR_EQ:
10930 id = ansi_assopname (BIT_IOR_EXPR);
10931 break;
10932
10933 case CPP_LSHIFT:
10934 id = ansi_opname (LSHIFT_EXPR);
10935 break;
10936
10937 case CPP_RSHIFT:
10938 id = ansi_opname (RSHIFT_EXPR);
10939 break;
10940
10941 case CPP_LSHIFT_EQ:
10942 id = ansi_assopname (LSHIFT_EXPR);
10943 break;
10944
10945 case CPP_RSHIFT_EQ:
10946 id = ansi_assopname (RSHIFT_EXPR);
10947 break;
10948
10949 case CPP_EQ_EQ:
10950 id = ansi_opname (EQ_EXPR);
10951 break;
10952
10953 case CPP_NOT_EQ:
10954 id = ansi_opname (NE_EXPR);
10955 break;
10956
10957 case CPP_LESS_EQ:
10958 id = ansi_opname (LE_EXPR);
10959 break;
10960
10961 case CPP_GREATER_EQ:
10962 id = ansi_opname (GE_EXPR);
10963 break;
10964
10965 case CPP_AND_AND:
10966 id = ansi_opname (TRUTH_ANDIF_EXPR);
10967 break;
10968
10969 case CPP_OR_OR:
10970 id = ansi_opname (TRUTH_ORIF_EXPR);
10971 break;
10972
10973 case CPP_PLUS_PLUS:
10974 id = ansi_opname (POSTINCREMENT_EXPR);
10975 break;
10976
10977 case CPP_MINUS_MINUS:
10978 id = ansi_opname (PREDECREMENT_EXPR);
10979 break;
10980
10981 case CPP_COMMA:
10982 id = ansi_opname (COMPOUND_EXPR);
10983 break;
10984
10985 case CPP_DEREF_STAR:
10986 id = ansi_opname (MEMBER_REF);
10987 break;
10988
10989 case CPP_DEREF:
10990 id = ansi_opname (COMPONENT_REF);
10991 break;
10992
10993 case CPP_OPEN_PAREN:
10994 /* Consume the `('. */
10995 cp_lexer_consume_token (parser->lexer);
10996 /* Look for the matching `)'. */
10997 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10998 return ansi_opname (CALL_EXPR);
10999
11000 case CPP_OPEN_SQUARE:
11001 /* Consume the `['. */
11002 cp_lexer_consume_token (parser->lexer);
11003 /* Look for the matching `]'. */
11004 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11005 return ansi_opname (ARRAY_REF);
11006
11007 default:
11008 /* Anything else is an error. */
11009 break;
11010 }
11011
11012 /* If we have selected an identifier, we need to consume the
11013 operator token. */
11014 if (id)
11015 cp_lexer_consume_token (parser->lexer);
11016 /* Otherwise, no valid operator name was present. */
11017 else
11018 {
11019 cp_parser_error (parser, "expected operator");
11020 id = error_mark_node;
11021 }
11022
11023 return id;
11024 }
11025
11026 /* Parse a template-declaration.
11027
11028 template-declaration:
11029 export [opt] template < template-parameter-list > declaration
11030
11031 If MEMBER_P is TRUE, this template-declaration occurs within a
11032 class-specifier.
11033
11034 The grammar rule given by the standard isn't correct. What
11035 is really meant is:
11036
11037 template-declaration:
11038 export [opt] template-parameter-list-seq
11039 decl-specifier-seq [opt] init-declarator [opt] ;
11040 export [opt] template-parameter-list-seq
11041 function-definition
11042
11043 template-parameter-list-seq:
11044 template-parameter-list-seq [opt]
11045 template < template-parameter-list > */
11046
11047 static void
11048 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11049 {
11050 /* Check for `export'. */
11051 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11052 {
11053 /* Consume the `export' token. */
11054 cp_lexer_consume_token (parser->lexer);
11055 /* Warn that we do not support `export'. */
11056 warning (0, "keyword %<export%> not implemented, and will be ignored");
11057 }
11058
11059 cp_parser_template_declaration_after_export (parser, member_p);
11060 }
11061
11062 /* Parse a template-parameter-list.
11063
11064 template-parameter-list:
11065 template-parameter
11066 template-parameter-list , template-parameter
11067
11068 Returns a TREE_LIST. Each node represents a template parameter.
11069 The nodes are connected via their TREE_CHAINs. */
11070
11071 static tree
11072 cp_parser_template_parameter_list (cp_parser* parser)
11073 {
11074 tree parameter_list = NULL_TREE;
11075
11076 begin_template_parm_list ();
11077
11078 /* The loop below parses the template parms. We first need to know
11079 the total number of template parms to be able to compute proper
11080 canonical types of each dependent type. So after the loop, when
11081 we know the total number of template parms,
11082 end_template_parm_list computes the proper canonical types and
11083 fixes up the dependent types accordingly. */
11084 while (true)
11085 {
11086 tree parameter;
11087 bool is_non_type;
11088 bool is_parameter_pack;
11089 location_t parm_loc;
11090
11091 /* Parse the template-parameter. */
11092 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11093 parameter = cp_parser_template_parameter (parser,
11094 &is_non_type,
11095 &is_parameter_pack);
11096 /* Add it to the list. */
11097 if (parameter != error_mark_node)
11098 parameter_list = process_template_parm (parameter_list,
11099 parm_loc,
11100 parameter,
11101 is_non_type,
11102 is_parameter_pack,
11103 0);
11104 else
11105 {
11106 tree err_parm = build_tree_list (parameter, parameter);
11107 parameter_list = chainon (parameter_list, err_parm);
11108 }
11109
11110 /* If the next token is not a `,', we're done. */
11111 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11112 break;
11113 /* Otherwise, consume the `,' token. */
11114 cp_lexer_consume_token (parser->lexer);
11115 }
11116
11117 return end_template_parm_list (parameter_list);
11118 }
11119
11120 /* Parse a template-parameter.
11121
11122 template-parameter:
11123 type-parameter
11124 parameter-declaration
11125
11126 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11127 the parameter. The TREE_PURPOSE is the default value, if any.
11128 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11129 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11130 set to true iff this parameter is a parameter pack. */
11131
11132 static tree
11133 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11134 bool *is_parameter_pack)
11135 {
11136 cp_token *token;
11137 cp_parameter_declarator *parameter_declarator;
11138 cp_declarator *id_declarator;
11139 tree parm;
11140
11141 /* Assume it is a type parameter or a template parameter. */
11142 *is_non_type = false;
11143 /* Assume it not a parameter pack. */
11144 *is_parameter_pack = false;
11145 /* Peek at the next token. */
11146 token = cp_lexer_peek_token (parser->lexer);
11147 /* If it is `class' or `template', we have a type-parameter. */
11148 if (token->keyword == RID_TEMPLATE)
11149 return cp_parser_type_parameter (parser, is_parameter_pack);
11150 /* If it is `class' or `typename' we do not know yet whether it is a
11151 type parameter or a non-type parameter. Consider:
11152
11153 template <typename T, typename T::X X> ...
11154
11155 or:
11156
11157 template <class C, class D*> ...
11158
11159 Here, the first parameter is a type parameter, and the second is
11160 a non-type parameter. We can tell by looking at the token after
11161 the identifier -- if it is a `,', `=', or `>' then we have a type
11162 parameter. */
11163 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11164 {
11165 /* Peek at the token after `class' or `typename'. */
11166 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11167 /* If it's an ellipsis, we have a template type parameter
11168 pack. */
11169 if (token->type == CPP_ELLIPSIS)
11170 return cp_parser_type_parameter (parser, is_parameter_pack);
11171 /* If it's an identifier, skip it. */
11172 if (token->type == CPP_NAME)
11173 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11174 /* Now, see if the token looks like the end of a template
11175 parameter. */
11176 if (token->type == CPP_COMMA
11177 || token->type == CPP_EQ
11178 || token->type == CPP_GREATER)
11179 return cp_parser_type_parameter (parser, is_parameter_pack);
11180 }
11181
11182 /* Otherwise, it is a non-type parameter.
11183
11184 [temp.param]
11185
11186 When parsing a default template-argument for a non-type
11187 template-parameter, the first non-nested `>' is taken as the end
11188 of the template parameter-list rather than a greater-than
11189 operator. */
11190 *is_non_type = true;
11191 parameter_declarator
11192 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11193 /*parenthesized_p=*/NULL);
11194
11195 /* If the parameter declaration is marked as a parameter pack, set
11196 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11197 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11198 grokdeclarator. */
11199 if (parameter_declarator
11200 && parameter_declarator->declarator
11201 && parameter_declarator->declarator->parameter_pack_p)
11202 {
11203 *is_parameter_pack = true;
11204 parameter_declarator->declarator->parameter_pack_p = false;
11205 }
11206
11207 /* If the next token is an ellipsis, and we don't already have it
11208 marked as a parameter pack, then we have a parameter pack (that
11209 has no declarator). */
11210 if (!*is_parameter_pack
11211 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11212 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11213 {
11214 /* Consume the `...'. */
11215 cp_lexer_consume_token (parser->lexer);
11216 maybe_warn_variadic_templates ();
11217
11218 *is_parameter_pack = true;
11219 }
11220 /* We might end up with a pack expansion as the type of the non-type
11221 template parameter, in which case this is a non-type template
11222 parameter pack. */
11223 else if (parameter_declarator
11224 && parameter_declarator->decl_specifiers.type
11225 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11226 {
11227 *is_parameter_pack = true;
11228 parameter_declarator->decl_specifiers.type =
11229 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11230 }
11231
11232 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11233 {
11234 /* Parameter packs cannot have default arguments. However, a
11235 user may try to do so, so we'll parse them and give an
11236 appropriate diagnostic here. */
11237
11238 /* Consume the `='. */
11239 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11240 cp_lexer_consume_token (parser->lexer);
11241
11242 /* Find the name of the parameter pack. */
11243 id_declarator = parameter_declarator->declarator;
11244 while (id_declarator && id_declarator->kind != cdk_id)
11245 id_declarator = id_declarator->declarator;
11246
11247 if (id_declarator && id_declarator->kind == cdk_id)
11248 error_at (start_token->location,
11249 "template parameter pack %qD cannot have a default argument",
11250 id_declarator->u.id.unqualified_name);
11251 else
11252 error_at (start_token->location,
11253 "template parameter pack cannot have a default argument");
11254
11255 /* Parse the default argument, but throw away the result. */
11256 cp_parser_default_argument (parser, /*template_parm_p=*/true);
11257 }
11258
11259 parm = grokdeclarator (parameter_declarator->declarator,
11260 &parameter_declarator->decl_specifiers,
11261 TPARM, /*initialized=*/0,
11262 /*attrlist=*/NULL);
11263 if (parm == error_mark_node)
11264 return error_mark_node;
11265
11266 return build_tree_list (parameter_declarator->default_argument, parm);
11267 }
11268
11269 /* Parse a type-parameter.
11270
11271 type-parameter:
11272 class identifier [opt]
11273 class identifier [opt] = type-id
11274 typename identifier [opt]
11275 typename identifier [opt] = type-id
11276 template < template-parameter-list > class identifier [opt]
11277 template < template-parameter-list > class identifier [opt]
11278 = id-expression
11279
11280 GNU Extension (variadic templates):
11281
11282 type-parameter:
11283 class ... identifier [opt]
11284 typename ... identifier [opt]
11285
11286 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11287 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11288 the declaration of the parameter.
11289
11290 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11291
11292 static tree
11293 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11294 {
11295 cp_token *token;
11296 tree parameter;
11297
11298 /* Look for a keyword to tell us what kind of parameter this is. */
11299 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11300 if (!token)
11301 return error_mark_node;
11302
11303 switch (token->keyword)
11304 {
11305 case RID_CLASS:
11306 case RID_TYPENAME:
11307 {
11308 tree identifier;
11309 tree default_argument;
11310
11311 /* If the next token is an ellipsis, we have a template
11312 argument pack. */
11313 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11314 {
11315 /* Consume the `...' token. */
11316 cp_lexer_consume_token (parser->lexer);
11317 maybe_warn_variadic_templates ();
11318
11319 *is_parameter_pack = true;
11320 }
11321
11322 /* If the next token is an identifier, then it names the
11323 parameter. */
11324 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11325 identifier = cp_parser_identifier (parser);
11326 else
11327 identifier = NULL_TREE;
11328
11329 /* Create the parameter. */
11330 parameter = finish_template_type_parm (class_type_node, identifier);
11331
11332 /* If the next token is an `=', we have a default argument. */
11333 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11334 {
11335 /* Consume the `=' token. */
11336 cp_lexer_consume_token (parser->lexer);
11337 /* Parse the default-argument. */
11338 push_deferring_access_checks (dk_no_deferred);
11339 default_argument = cp_parser_type_id (parser);
11340
11341 /* Template parameter packs cannot have default
11342 arguments. */
11343 if (*is_parameter_pack)
11344 {
11345 if (identifier)
11346 error_at (token->location,
11347 "template parameter pack %qD cannot have a "
11348 "default argument", identifier);
11349 else
11350 error_at (token->location,
11351 "template parameter packs cannot have "
11352 "default arguments");
11353 default_argument = NULL_TREE;
11354 }
11355 pop_deferring_access_checks ();
11356 }
11357 else
11358 default_argument = NULL_TREE;
11359
11360 /* Create the combined representation of the parameter and the
11361 default argument. */
11362 parameter = build_tree_list (default_argument, parameter);
11363 }
11364 break;
11365
11366 case RID_TEMPLATE:
11367 {
11368 tree identifier;
11369 tree default_argument;
11370
11371 /* Look for the `<'. */
11372 cp_parser_require (parser, CPP_LESS, RT_LESS);
11373 /* Parse the template-parameter-list. */
11374 cp_parser_template_parameter_list (parser);
11375 /* Look for the `>'. */
11376 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11377 /* Look for the `class' keyword. */
11378 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11379 /* If the next token is an ellipsis, we have a template
11380 argument pack. */
11381 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11382 {
11383 /* Consume the `...' token. */
11384 cp_lexer_consume_token (parser->lexer);
11385 maybe_warn_variadic_templates ();
11386
11387 *is_parameter_pack = true;
11388 }
11389 /* If the next token is an `=', then there is a
11390 default-argument. If the next token is a `>', we are at
11391 the end of the parameter-list. If the next token is a `,',
11392 then we are at the end of this parameter. */
11393 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11394 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11395 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11396 {
11397 identifier = cp_parser_identifier (parser);
11398 /* Treat invalid names as if the parameter were nameless. */
11399 if (identifier == error_mark_node)
11400 identifier = NULL_TREE;
11401 }
11402 else
11403 identifier = NULL_TREE;
11404
11405 /* Create the template parameter. */
11406 parameter = finish_template_template_parm (class_type_node,
11407 identifier);
11408
11409 /* If the next token is an `=', then there is a
11410 default-argument. */
11411 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11412 {
11413 bool is_template;
11414
11415 /* Consume the `='. */
11416 cp_lexer_consume_token (parser->lexer);
11417 /* Parse the id-expression. */
11418 push_deferring_access_checks (dk_no_deferred);
11419 /* save token before parsing the id-expression, for error
11420 reporting */
11421 token = cp_lexer_peek_token (parser->lexer);
11422 default_argument
11423 = cp_parser_id_expression (parser,
11424 /*template_keyword_p=*/false,
11425 /*check_dependency_p=*/true,
11426 /*template_p=*/&is_template,
11427 /*declarator_p=*/false,
11428 /*optional_p=*/false);
11429 if (TREE_CODE (default_argument) == TYPE_DECL)
11430 /* If the id-expression was a template-id that refers to
11431 a template-class, we already have the declaration here,
11432 so no further lookup is needed. */
11433 ;
11434 else
11435 /* Look up the name. */
11436 default_argument
11437 = cp_parser_lookup_name (parser, default_argument,
11438 none_type,
11439 /*is_template=*/is_template,
11440 /*is_namespace=*/false,
11441 /*check_dependency=*/true,
11442 /*ambiguous_decls=*/NULL,
11443 token->location);
11444 /* See if the default argument is valid. */
11445 default_argument
11446 = check_template_template_default_arg (default_argument);
11447
11448 /* Template parameter packs cannot have default
11449 arguments. */
11450 if (*is_parameter_pack)
11451 {
11452 if (identifier)
11453 error_at (token->location,
11454 "template parameter pack %qD cannot "
11455 "have a default argument",
11456 identifier);
11457 else
11458 error_at (token->location, "template parameter packs cannot "
11459 "have default arguments");
11460 default_argument = NULL_TREE;
11461 }
11462 pop_deferring_access_checks ();
11463 }
11464 else
11465 default_argument = NULL_TREE;
11466
11467 /* Create the combined representation of the parameter and the
11468 default argument. */
11469 parameter = build_tree_list (default_argument, parameter);
11470 }
11471 break;
11472
11473 default:
11474 gcc_unreachable ();
11475 break;
11476 }
11477
11478 return parameter;
11479 }
11480
11481 /* Parse a template-id.
11482
11483 template-id:
11484 template-name < template-argument-list [opt] >
11485
11486 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11487 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11488 returned. Otherwise, if the template-name names a function, or set
11489 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11490 names a class, returns a TYPE_DECL for the specialization.
11491
11492 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11493 uninstantiated templates. */
11494
11495 static tree
11496 cp_parser_template_id (cp_parser *parser,
11497 bool template_keyword_p,
11498 bool check_dependency_p,
11499 bool is_declaration)
11500 {
11501 int i;
11502 tree templ;
11503 tree arguments;
11504 tree template_id;
11505 cp_token_position start_of_id = 0;
11506 deferred_access_check *chk;
11507 VEC (deferred_access_check,gc) *access_check;
11508 cp_token *next_token = NULL, *next_token_2 = NULL;
11509 bool is_identifier;
11510
11511 /* If the next token corresponds to a template-id, there is no need
11512 to reparse it. */
11513 next_token = cp_lexer_peek_token (parser->lexer);
11514 if (next_token->type == CPP_TEMPLATE_ID)
11515 {
11516 struct tree_check *check_value;
11517
11518 /* Get the stored value. */
11519 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11520 /* Perform any access checks that were deferred. */
11521 access_check = check_value->checks;
11522 if (access_check)
11523 {
11524 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11525 perform_or_defer_access_check (chk->binfo,
11526 chk->decl,
11527 chk->diag_decl);
11528 }
11529 /* Return the stored value. */
11530 return check_value->value;
11531 }
11532
11533 /* Avoid performing name lookup if there is no possibility of
11534 finding a template-id. */
11535 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11536 || (next_token->type == CPP_NAME
11537 && !cp_parser_nth_token_starts_template_argument_list_p
11538 (parser, 2)))
11539 {
11540 cp_parser_error (parser, "expected template-id");
11541 return error_mark_node;
11542 }
11543
11544 /* Remember where the template-id starts. */
11545 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11546 start_of_id = cp_lexer_token_position (parser->lexer, false);
11547
11548 push_deferring_access_checks (dk_deferred);
11549
11550 /* Parse the template-name. */
11551 is_identifier = false;
11552 templ = cp_parser_template_name (parser, template_keyword_p,
11553 check_dependency_p,
11554 is_declaration,
11555 &is_identifier);
11556 if (templ == error_mark_node || is_identifier)
11557 {
11558 pop_deferring_access_checks ();
11559 return templ;
11560 }
11561
11562 /* If we find the sequence `[:' after a template-name, it's probably
11563 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11564 parse correctly the argument list. */
11565 next_token = cp_lexer_peek_token (parser->lexer);
11566 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11567 if (next_token->type == CPP_OPEN_SQUARE
11568 && next_token->flags & DIGRAPH
11569 && next_token_2->type == CPP_COLON
11570 && !(next_token_2->flags & PREV_WHITE))
11571 {
11572 cp_parser_parse_tentatively (parser);
11573 /* Change `:' into `::'. */
11574 next_token_2->type = CPP_SCOPE;
11575 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11576 CPP_LESS. */
11577 cp_lexer_consume_token (parser->lexer);
11578
11579 /* Parse the arguments. */
11580 arguments = cp_parser_enclosed_template_argument_list (parser);
11581 if (!cp_parser_parse_definitely (parser))
11582 {
11583 /* If we couldn't parse an argument list, then we revert our changes
11584 and return simply an error. Maybe this is not a template-id
11585 after all. */
11586 next_token_2->type = CPP_COLON;
11587 cp_parser_error (parser, "expected %<<%>");
11588 pop_deferring_access_checks ();
11589 return error_mark_node;
11590 }
11591 /* Otherwise, emit an error about the invalid digraph, but continue
11592 parsing because we got our argument list. */
11593 if (permerror (next_token->location,
11594 "%<<::%> cannot begin a template-argument list"))
11595 {
11596 static bool hint = false;
11597 inform (next_token->location,
11598 "%<<:%> is an alternate spelling for %<[%>."
11599 " Insert whitespace between %<<%> and %<::%>");
11600 if (!hint && !flag_permissive)
11601 {
11602 inform (next_token->location, "(if you use %<-fpermissive%>"
11603 " G++ will accept your code)");
11604 hint = true;
11605 }
11606 }
11607 }
11608 else
11609 {
11610 /* Look for the `<' that starts the template-argument-list. */
11611 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11612 {
11613 pop_deferring_access_checks ();
11614 return error_mark_node;
11615 }
11616 /* Parse the arguments. */
11617 arguments = cp_parser_enclosed_template_argument_list (parser);
11618 }
11619
11620 /* Build a representation of the specialization. */
11621 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11622 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11623 else if (DECL_CLASS_TEMPLATE_P (templ)
11624 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11625 {
11626 bool entering_scope;
11627 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11628 template (rather than some instantiation thereof) only if
11629 is not nested within some other construct. For example, in
11630 "template <typename T> void f(T) { A<T>::", A<T> is just an
11631 instantiation of A. */
11632 entering_scope = (template_parm_scope_p ()
11633 && cp_lexer_next_token_is (parser->lexer,
11634 CPP_SCOPE));
11635 template_id
11636 = finish_template_type (templ, arguments, entering_scope);
11637 }
11638 else
11639 {
11640 /* If it's not a class-template or a template-template, it should be
11641 a function-template. */
11642 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11643 || TREE_CODE (templ) == OVERLOAD
11644 || BASELINK_P (templ)));
11645
11646 template_id = lookup_template_function (templ, arguments);
11647 }
11648
11649 /* If parsing tentatively, replace the sequence of tokens that makes
11650 up the template-id with a CPP_TEMPLATE_ID token. That way,
11651 should we re-parse the token stream, we will not have to repeat
11652 the effort required to do the parse, nor will we issue duplicate
11653 error messages about problems during instantiation of the
11654 template. */
11655 if (start_of_id)
11656 {
11657 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11658
11659 /* Reset the contents of the START_OF_ID token. */
11660 token->type = CPP_TEMPLATE_ID;
11661 /* Retrieve any deferred checks. Do not pop this access checks yet
11662 so the memory will not be reclaimed during token replacing below. */
11663 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11664 token->u.tree_check_value->value = template_id;
11665 token->u.tree_check_value->checks = get_deferred_access_checks ();
11666 token->keyword = RID_MAX;
11667
11668 /* Purge all subsequent tokens. */
11669 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11670
11671 /* ??? Can we actually assume that, if template_id ==
11672 error_mark_node, we will have issued a diagnostic to the
11673 user, as opposed to simply marking the tentative parse as
11674 failed? */
11675 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11676 error_at (token->location, "parse error in template argument list");
11677 }
11678
11679 pop_deferring_access_checks ();
11680 return template_id;
11681 }
11682
11683 /* Parse a template-name.
11684
11685 template-name:
11686 identifier
11687
11688 The standard should actually say:
11689
11690 template-name:
11691 identifier
11692 operator-function-id
11693
11694 A defect report has been filed about this issue.
11695
11696 A conversion-function-id cannot be a template name because they cannot
11697 be part of a template-id. In fact, looking at this code:
11698
11699 a.operator K<int>()
11700
11701 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11702 It is impossible to call a templated conversion-function-id with an
11703 explicit argument list, since the only allowed template parameter is
11704 the type to which it is converting.
11705
11706 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11707 `template' keyword, in a construction like:
11708
11709 T::template f<3>()
11710
11711 In that case `f' is taken to be a template-name, even though there
11712 is no way of knowing for sure.
11713
11714 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11715 name refers to a set of overloaded functions, at least one of which
11716 is a template, or an IDENTIFIER_NODE with the name of the template,
11717 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11718 names are looked up inside uninstantiated templates. */
11719
11720 static tree
11721 cp_parser_template_name (cp_parser* parser,
11722 bool template_keyword_p,
11723 bool check_dependency_p,
11724 bool is_declaration,
11725 bool *is_identifier)
11726 {
11727 tree identifier;
11728 tree decl;
11729 tree fns;
11730 cp_token *token = cp_lexer_peek_token (parser->lexer);
11731
11732 /* If the next token is `operator', then we have either an
11733 operator-function-id or a conversion-function-id. */
11734 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11735 {
11736 /* We don't know whether we're looking at an
11737 operator-function-id or a conversion-function-id. */
11738 cp_parser_parse_tentatively (parser);
11739 /* Try an operator-function-id. */
11740 identifier = cp_parser_operator_function_id (parser);
11741 /* If that didn't work, try a conversion-function-id. */
11742 if (!cp_parser_parse_definitely (parser))
11743 {
11744 cp_parser_error (parser, "expected template-name");
11745 return error_mark_node;
11746 }
11747 }
11748 /* Look for the identifier. */
11749 else
11750 identifier = cp_parser_identifier (parser);
11751
11752 /* If we didn't find an identifier, we don't have a template-id. */
11753 if (identifier == error_mark_node)
11754 return error_mark_node;
11755
11756 /* If the name immediately followed the `template' keyword, then it
11757 is a template-name. However, if the next token is not `<', then
11758 we do not treat it as a template-name, since it is not being used
11759 as part of a template-id. This enables us to handle constructs
11760 like:
11761
11762 template <typename T> struct S { S(); };
11763 template <typename T> S<T>::S();
11764
11765 correctly. We would treat `S' as a template -- if it were `S<T>'
11766 -- but we do not if there is no `<'. */
11767
11768 if (processing_template_decl
11769 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11770 {
11771 /* In a declaration, in a dependent context, we pretend that the
11772 "template" keyword was present in order to improve error
11773 recovery. For example, given:
11774
11775 template <typename T> void f(T::X<int>);
11776
11777 we want to treat "X<int>" as a template-id. */
11778 if (is_declaration
11779 && !template_keyword_p
11780 && parser->scope && TYPE_P (parser->scope)
11781 && check_dependency_p
11782 && dependent_scope_p (parser->scope)
11783 /* Do not do this for dtors (or ctors), since they never
11784 need the template keyword before their name. */
11785 && !constructor_name_p (identifier, parser->scope))
11786 {
11787 cp_token_position start = 0;
11788
11789 /* Explain what went wrong. */
11790 error_at (token->location, "non-template %qD used as template",
11791 identifier);
11792 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11793 parser->scope, identifier);
11794 /* If parsing tentatively, find the location of the "<" token. */
11795 if (cp_parser_simulate_error (parser))
11796 start = cp_lexer_token_position (parser->lexer, true);
11797 /* Parse the template arguments so that we can issue error
11798 messages about them. */
11799 cp_lexer_consume_token (parser->lexer);
11800 cp_parser_enclosed_template_argument_list (parser);
11801 /* Skip tokens until we find a good place from which to
11802 continue parsing. */
11803 cp_parser_skip_to_closing_parenthesis (parser,
11804 /*recovering=*/true,
11805 /*or_comma=*/true,
11806 /*consume_paren=*/false);
11807 /* If parsing tentatively, permanently remove the
11808 template argument list. That will prevent duplicate
11809 error messages from being issued about the missing
11810 "template" keyword. */
11811 if (start)
11812 cp_lexer_purge_tokens_after (parser->lexer, start);
11813 if (is_identifier)
11814 *is_identifier = true;
11815 return identifier;
11816 }
11817
11818 /* If the "template" keyword is present, then there is generally
11819 no point in doing name-lookup, so we just return IDENTIFIER.
11820 But, if the qualifying scope is non-dependent then we can
11821 (and must) do name-lookup normally. */
11822 if (template_keyword_p
11823 && (!parser->scope
11824 || (TYPE_P (parser->scope)
11825 && dependent_type_p (parser->scope))))
11826 return identifier;
11827 }
11828
11829 /* Look up the name. */
11830 decl = cp_parser_lookup_name (parser, identifier,
11831 none_type,
11832 /*is_template=*/true,
11833 /*is_namespace=*/false,
11834 check_dependency_p,
11835 /*ambiguous_decls=*/NULL,
11836 token->location);
11837
11838 /* If DECL is a template, then the name was a template-name. */
11839 if (TREE_CODE (decl) == TEMPLATE_DECL)
11840 ;
11841 else
11842 {
11843 tree fn = NULL_TREE;
11844
11845 /* The standard does not explicitly indicate whether a name that
11846 names a set of overloaded declarations, some of which are
11847 templates, is a template-name. However, such a name should
11848 be a template-name; otherwise, there is no way to form a
11849 template-id for the overloaded templates. */
11850 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11851 if (TREE_CODE (fns) == OVERLOAD)
11852 for (fn = fns; fn; fn = OVL_NEXT (fn))
11853 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11854 break;
11855
11856 if (!fn)
11857 {
11858 /* The name does not name a template. */
11859 cp_parser_error (parser, "expected template-name");
11860 return error_mark_node;
11861 }
11862 }
11863
11864 /* If DECL is dependent, and refers to a function, then just return
11865 its name; we will look it up again during template instantiation. */
11866 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11867 {
11868 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11869 if (TYPE_P (scope) && dependent_type_p (scope))
11870 return identifier;
11871 }
11872
11873 return decl;
11874 }
11875
11876 /* Parse a template-argument-list.
11877
11878 template-argument-list:
11879 template-argument ... [opt]
11880 template-argument-list , template-argument ... [opt]
11881
11882 Returns a TREE_VEC containing the arguments. */
11883
11884 static tree
11885 cp_parser_template_argument_list (cp_parser* parser)
11886 {
11887 tree fixed_args[10];
11888 unsigned n_args = 0;
11889 unsigned alloced = 10;
11890 tree *arg_ary = fixed_args;
11891 tree vec;
11892 bool saved_in_template_argument_list_p;
11893 bool saved_ice_p;
11894 bool saved_non_ice_p;
11895
11896 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11897 parser->in_template_argument_list_p = true;
11898 /* Even if the template-id appears in an integral
11899 constant-expression, the contents of the argument list do
11900 not. */
11901 saved_ice_p = parser->integral_constant_expression_p;
11902 parser->integral_constant_expression_p = false;
11903 saved_non_ice_p = parser->non_integral_constant_expression_p;
11904 parser->non_integral_constant_expression_p = false;
11905 /* Parse the arguments. */
11906 do
11907 {
11908 tree argument;
11909
11910 if (n_args)
11911 /* Consume the comma. */
11912 cp_lexer_consume_token (parser->lexer);
11913
11914 /* Parse the template-argument. */
11915 argument = cp_parser_template_argument (parser);
11916
11917 /* If the next token is an ellipsis, we're expanding a template
11918 argument pack. */
11919 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11920 {
11921 if (argument == error_mark_node)
11922 {
11923 cp_token *token = cp_lexer_peek_token (parser->lexer);
11924 error_at (token->location,
11925 "expected parameter pack before %<...%>");
11926 }
11927 /* Consume the `...' token. */
11928 cp_lexer_consume_token (parser->lexer);
11929
11930 /* Make the argument into a TYPE_PACK_EXPANSION or
11931 EXPR_PACK_EXPANSION. */
11932 argument = make_pack_expansion (argument);
11933 }
11934
11935 if (n_args == alloced)
11936 {
11937 alloced *= 2;
11938
11939 if (arg_ary == fixed_args)
11940 {
11941 arg_ary = XNEWVEC (tree, alloced);
11942 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11943 }
11944 else
11945 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11946 }
11947 arg_ary[n_args++] = argument;
11948 }
11949 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11950
11951 vec = make_tree_vec (n_args);
11952
11953 while (n_args--)
11954 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11955
11956 if (arg_ary != fixed_args)
11957 free (arg_ary);
11958 parser->non_integral_constant_expression_p = saved_non_ice_p;
11959 parser->integral_constant_expression_p = saved_ice_p;
11960 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11961 #ifdef ENABLE_CHECKING
11962 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11963 #endif
11964 return vec;
11965 }
11966
11967 /* Parse a template-argument.
11968
11969 template-argument:
11970 assignment-expression
11971 type-id
11972 id-expression
11973
11974 The representation is that of an assignment-expression, type-id, or
11975 id-expression -- except that the qualified id-expression is
11976 evaluated, so that the value returned is either a DECL or an
11977 OVERLOAD.
11978
11979 Although the standard says "assignment-expression", it forbids
11980 throw-expressions or assignments in the template argument.
11981 Therefore, we use "conditional-expression" instead. */
11982
11983 static tree
11984 cp_parser_template_argument (cp_parser* parser)
11985 {
11986 tree argument;
11987 bool template_p;
11988 bool address_p;
11989 bool maybe_type_id = false;
11990 cp_token *token = NULL, *argument_start_token = NULL;
11991 cp_id_kind idk;
11992
11993 /* There's really no way to know what we're looking at, so we just
11994 try each alternative in order.
11995
11996 [temp.arg]
11997
11998 In a template-argument, an ambiguity between a type-id and an
11999 expression is resolved to a type-id, regardless of the form of
12000 the corresponding template-parameter.
12001
12002 Therefore, we try a type-id first. */
12003 cp_parser_parse_tentatively (parser);
12004 argument = cp_parser_template_type_arg (parser);
12005 /* If there was no error parsing the type-id but the next token is a
12006 '>>', our behavior depends on which dialect of C++ we're
12007 parsing. In C++98, we probably found a typo for '> >'. But there
12008 are type-id which are also valid expressions. For instance:
12009
12010 struct X { int operator >> (int); };
12011 template <int V> struct Foo {};
12012 Foo<X () >> 5> r;
12013
12014 Here 'X()' is a valid type-id of a function type, but the user just
12015 wanted to write the expression "X() >> 5". Thus, we remember that we
12016 found a valid type-id, but we still try to parse the argument as an
12017 expression to see what happens.
12018
12019 In C++0x, the '>>' will be considered two separate '>'
12020 tokens. */
12021 if (!cp_parser_error_occurred (parser)
12022 && cxx_dialect == cxx98
12023 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12024 {
12025 maybe_type_id = true;
12026 cp_parser_abort_tentative_parse (parser);
12027 }
12028 else
12029 {
12030 /* If the next token isn't a `,' or a `>', then this argument wasn't
12031 really finished. This means that the argument is not a valid
12032 type-id. */
12033 if (!cp_parser_next_token_ends_template_argument_p (parser))
12034 cp_parser_error (parser, "expected template-argument");
12035 /* If that worked, we're done. */
12036 if (cp_parser_parse_definitely (parser))
12037 return argument;
12038 }
12039 /* We're still not sure what the argument will be. */
12040 cp_parser_parse_tentatively (parser);
12041 /* Try a template. */
12042 argument_start_token = cp_lexer_peek_token (parser->lexer);
12043 argument = cp_parser_id_expression (parser,
12044 /*template_keyword_p=*/false,
12045 /*check_dependency_p=*/true,
12046 &template_p,
12047 /*declarator_p=*/false,
12048 /*optional_p=*/false);
12049 /* If the next token isn't a `,' or a `>', then this argument wasn't
12050 really finished. */
12051 if (!cp_parser_next_token_ends_template_argument_p (parser))
12052 cp_parser_error (parser, "expected template-argument");
12053 if (!cp_parser_error_occurred (parser))
12054 {
12055 /* Figure out what is being referred to. If the id-expression
12056 was for a class template specialization, then we will have a
12057 TYPE_DECL at this point. There is no need to do name lookup
12058 at this point in that case. */
12059 if (TREE_CODE (argument) != TYPE_DECL)
12060 argument = cp_parser_lookup_name (parser, argument,
12061 none_type,
12062 /*is_template=*/template_p,
12063 /*is_namespace=*/false,
12064 /*check_dependency=*/true,
12065 /*ambiguous_decls=*/NULL,
12066 argument_start_token->location);
12067 if (TREE_CODE (argument) != TEMPLATE_DECL
12068 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12069 cp_parser_error (parser, "expected template-name");
12070 }
12071 if (cp_parser_parse_definitely (parser))
12072 return argument;
12073 /* It must be a non-type argument. There permitted cases are given
12074 in [temp.arg.nontype]:
12075
12076 -- an integral constant-expression of integral or enumeration
12077 type; or
12078
12079 -- the name of a non-type template-parameter; or
12080
12081 -- the name of an object or function with external linkage...
12082
12083 -- the address of an object or function with external linkage...
12084
12085 -- a pointer to member... */
12086 /* Look for a non-type template parameter. */
12087 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12088 {
12089 cp_parser_parse_tentatively (parser);
12090 argument = cp_parser_primary_expression (parser,
12091 /*address_p=*/false,
12092 /*cast_p=*/false,
12093 /*template_arg_p=*/true,
12094 &idk);
12095 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12096 || !cp_parser_next_token_ends_template_argument_p (parser))
12097 cp_parser_simulate_error (parser);
12098 if (cp_parser_parse_definitely (parser))
12099 return argument;
12100 }
12101
12102 /* If the next token is "&", the argument must be the address of an
12103 object or function with external linkage. */
12104 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12105 if (address_p)
12106 cp_lexer_consume_token (parser->lexer);
12107 /* See if we might have an id-expression. */
12108 token = cp_lexer_peek_token (parser->lexer);
12109 if (token->type == CPP_NAME
12110 || token->keyword == RID_OPERATOR
12111 || token->type == CPP_SCOPE
12112 || token->type == CPP_TEMPLATE_ID
12113 || token->type == CPP_NESTED_NAME_SPECIFIER)
12114 {
12115 cp_parser_parse_tentatively (parser);
12116 argument = cp_parser_primary_expression (parser,
12117 address_p,
12118 /*cast_p=*/false,
12119 /*template_arg_p=*/true,
12120 &idk);
12121 if (cp_parser_error_occurred (parser)
12122 || !cp_parser_next_token_ends_template_argument_p (parser))
12123 cp_parser_abort_tentative_parse (parser);
12124 else
12125 {
12126 tree probe;
12127
12128 if (TREE_CODE (argument) == INDIRECT_REF)
12129 {
12130 gcc_assert (REFERENCE_REF_P (argument));
12131 argument = TREE_OPERAND (argument, 0);
12132 }
12133
12134 /* If we're in a template, we represent a qualified-id referring
12135 to a static data member as a SCOPE_REF even if the scope isn't
12136 dependent so that we can check access control later. */
12137 probe = argument;
12138 if (TREE_CODE (probe) == SCOPE_REF)
12139 probe = TREE_OPERAND (probe, 1);
12140 if (TREE_CODE (probe) == VAR_DECL)
12141 {
12142 /* A variable without external linkage might still be a
12143 valid constant-expression, so no error is issued here
12144 if the external-linkage check fails. */
12145 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12146 cp_parser_simulate_error (parser);
12147 }
12148 else if (is_overloaded_fn (argument))
12149 /* All overloaded functions are allowed; if the external
12150 linkage test does not pass, an error will be issued
12151 later. */
12152 ;
12153 else if (address_p
12154 && (TREE_CODE (argument) == OFFSET_REF
12155 || TREE_CODE (argument) == SCOPE_REF))
12156 /* A pointer-to-member. */
12157 ;
12158 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12159 ;
12160 else
12161 cp_parser_simulate_error (parser);
12162
12163 if (cp_parser_parse_definitely (parser))
12164 {
12165 if (address_p)
12166 argument = build_x_unary_op (ADDR_EXPR, argument,
12167 tf_warning_or_error);
12168 return argument;
12169 }
12170 }
12171 }
12172 /* If the argument started with "&", there are no other valid
12173 alternatives at this point. */
12174 if (address_p)
12175 {
12176 cp_parser_error (parser, "invalid non-type template argument");
12177 return error_mark_node;
12178 }
12179
12180 /* If the argument wasn't successfully parsed as a type-id followed
12181 by '>>', the argument can only be a constant expression now.
12182 Otherwise, we try parsing the constant-expression tentatively,
12183 because the argument could really be a type-id. */
12184 if (maybe_type_id)
12185 cp_parser_parse_tentatively (parser);
12186 argument = cp_parser_constant_expression (parser,
12187 /*allow_non_constant_p=*/false,
12188 /*non_constant_p=*/NULL);
12189 argument = fold_non_dependent_expr (argument);
12190 if (!maybe_type_id)
12191 return argument;
12192 if (!cp_parser_next_token_ends_template_argument_p (parser))
12193 cp_parser_error (parser, "expected template-argument");
12194 if (cp_parser_parse_definitely (parser))
12195 return argument;
12196 /* We did our best to parse the argument as a non type-id, but that
12197 was the only alternative that matched (albeit with a '>' after
12198 it). We can assume it's just a typo from the user, and a
12199 diagnostic will then be issued. */
12200 return cp_parser_template_type_arg (parser);
12201 }
12202
12203 /* Parse an explicit-instantiation.
12204
12205 explicit-instantiation:
12206 template declaration
12207
12208 Although the standard says `declaration', what it really means is:
12209
12210 explicit-instantiation:
12211 template decl-specifier-seq [opt] declarator [opt] ;
12212
12213 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12214 supposed to be allowed. A defect report has been filed about this
12215 issue.
12216
12217 GNU Extension:
12218
12219 explicit-instantiation:
12220 storage-class-specifier template
12221 decl-specifier-seq [opt] declarator [opt] ;
12222 function-specifier template
12223 decl-specifier-seq [opt] declarator [opt] ; */
12224
12225 static void
12226 cp_parser_explicit_instantiation (cp_parser* parser)
12227 {
12228 int declares_class_or_enum;
12229 cp_decl_specifier_seq decl_specifiers;
12230 tree extension_specifier = NULL_TREE;
12231
12232 /* Look for an (optional) storage-class-specifier or
12233 function-specifier. */
12234 if (cp_parser_allow_gnu_extensions_p (parser))
12235 {
12236 extension_specifier
12237 = cp_parser_storage_class_specifier_opt (parser);
12238 if (!extension_specifier)
12239 extension_specifier
12240 = cp_parser_function_specifier_opt (parser,
12241 /*decl_specs=*/NULL);
12242 }
12243
12244 /* Look for the `template' keyword. */
12245 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12246 /* Let the front end know that we are processing an explicit
12247 instantiation. */
12248 begin_explicit_instantiation ();
12249 /* [temp.explicit] says that we are supposed to ignore access
12250 control while processing explicit instantiation directives. */
12251 push_deferring_access_checks (dk_no_check);
12252 /* Parse a decl-specifier-seq. */
12253 cp_parser_decl_specifier_seq (parser,
12254 CP_PARSER_FLAGS_OPTIONAL,
12255 &decl_specifiers,
12256 &declares_class_or_enum);
12257 /* If there was exactly one decl-specifier, and it declared a class,
12258 and there's no declarator, then we have an explicit type
12259 instantiation. */
12260 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12261 {
12262 tree type;
12263
12264 type = check_tag_decl (&decl_specifiers);
12265 /* Turn access control back on for names used during
12266 template instantiation. */
12267 pop_deferring_access_checks ();
12268 if (type)
12269 do_type_instantiation (type, extension_specifier,
12270 /*complain=*/tf_error);
12271 }
12272 else
12273 {
12274 cp_declarator *declarator;
12275 tree decl;
12276
12277 /* Parse the declarator. */
12278 declarator
12279 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12280 /*ctor_dtor_or_conv_p=*/NULL,
12281 /*parenthesized_p=*/NULL,
12282 /*member_p=*/false);
12283 if (declares_class_or_enum & 2)
12284 cp_parser_check_for_definition_in_return_type (declarator,
12285 decl_specifiers.type,
12286 decl_specifiers.type_location);
12287 if (declarator != cp_error_declarator)
12288 {
12289 if (decl_specifiers.specs[(int)ds_inline])
12290 permerror (input_location, "explicit instantiation shall not use"
12291 " %<inline%> specifier");
12292 if (decl_specifiers.specs[(int)ds_constexpr])
12293 permerror (input_location, "explicit instantiation shall not use"
12294 " %<constexpr%> specifier");
12295
12296 decl = grokdeclarator (declarator, &decl_specifiers,
12297 NORMAL, 0, &decl_specifiers.attributes);
12298 /* Turn access control back on for names used during
12299 template instantiation. */
12300 pop_deferring_access_checks ();
12301 /* Do the explicit instantiation. */
12302 do_decl_instantiation (decl, extension_specifier);
12303 }
12304 else
12305 {
12306 pop_deferring_access_checks ();
12307 /* Skip the body of the explicit instantiation. */
12308 cp_parser_skip_to_end_of_statement (parser);
12309 }
12310 }
12311 /* We're done with the instantiation. */
12312 end_explicit_instantiation ();
12313
12314 cp_parser_consume_semicolon_at_end_of_statement (parser);
12315 }
12316
12317 /* Parse an explicit-specialization.
12318
12319 explicit-specialization:
12320 template < > declaration
12321
12322 Although the standard says `declaration', what it really means is:
12323
12324 explicit-specialization:
12325 template <> decl-specifier [opt] init-declarator [opt] ;
12326 template <> function-definition
12327 template <> explicit-specialization
12328 template <> template-declaration */
12329
12330 static void
12331 cp_parser_explicit_specialization (cp_parser* parser)
12332 {
12333 bool need_lang_pop;
12334 cp_token *token = cp_lexer_peek_token (parser->lexer);
12335
12336 /* Look for the `template' keyword. */
12337 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12338 /* Look for the `<'. */
12339 cp_parser_require (parser, CPP_LESS, RT_LESS);
12340 /* Look for the `>'. */
12341 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12342 /* We have processed another parameter list. */
12343 ++parser->num_template_parameter_lists;
12344 /* [temp]
12345
12346 A template ... explicit specialization ... shall not have C
12347 linkage. */
12348 if (current_lang_name == lang_name_c)
12349 {
12350 error_at (token->location, "template specialization with C linkage");
12351 /* Give it C++ linkage to avoid confusing other parts of the
12352 front end. */
12353 push_lang_context (lang_name_cplusplus);
12354 need_lang_pop = true;
12355 }
12356 else
12357 need_lang_pop = false;
12358 /* Let the front end know that we are beginning a specialization. */
12359 if (!begin_specialization ())
12360 {
12361 end_specialization ();
12362 return;
12363 }
12364
12365 /* If the next keyword is `template', we need to figure out whether
12366 or not we're looking a template-declaration. */
12367 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12368 {
12369 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12370 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12371 cp_parser_template_declaration_after_export (parser,
12372 /*member_p=*/false);
12373 else
12374 cp_parser_explicit_specialization (parser);
12375 }
12376 else
12377 /* Parse the dependent declaration. */
12378 cp_parser_single_declaration (parser,
12379 /*checks=*/NULL,
12380 /*member_p=*/false,
12381 /*explicit_specialization_p=*/true,
12382 /*friend_p=*/NULL);
12383 /* We're done with the specialization. */
12384 end_specialization ();
12385 /* For the erroneous case of a template with C linkage, we pushed an
12386 implicit C++ linkage scope; exit that scope now. */
12387 if (need_lang_pop)
12388 pop_lang_context ();
12389 /* We're done with this parameter list. */
12390 --parser->num_template_parameter_lists;
12391 }
12392
12393 /* Parse a type-specifier.
12394
12395 type-specifier:
12396 simple-type-specifier
12397 class-specifier
12398 enum-specifier
12399 elaborated-type-specifier
12400 cv-qualifier
12401
12402 GNU Extension:
12403
12404 type-specifier:
12405 __complex__
12406
12407 Returns a representation of the type-specifier. For a
12408 class-specifier, enum-specifier, or elaborated-type-specifier, a
12409 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12410
12411 The parser flags FLAGS is used to control type-specifier parsing.
12412
12413 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12414 in a decl-specifier-seq.
12415
12416 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12417 class-specifier, enum-specifier, or elaborated-type-specifier, then
12418 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12419 if a type is declared; 2 if it is defined. Otherwise, it is set to
12420 zero.
12421
12422 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12423 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12424 is set to FALSE. */
12425
12426 static tree
12427 cp_parser_type_specifier (cp_parser* parser,
12428 cp_parser_flags flags,
12429 cp_decl_specifier_seq *decl_specs,
12430 bool is_declaration,
12431 int* declares_class_or_enum,
12432 bool* is_cv_qualifier)
12433 {
12434 tree type_spec = NULL_TREE;
12435 cp_token *token;
12436 enum rid keyword;
12437 cp_decl_spec ds = ds_last;
12438
12439 /* Assume this type-specifier does not declare a new type. */
12440 if (declares_class_or_enum)
12441 *declares_class_or_enum = 0;
12442 /* And that it does not specify a cv-qualifier. */
12443 if (is_cv_qualifier)
12444 *is_cv_qualifier = false;
12445 /* Peek at the next token. */
12446 token = cp_lexer_peek_token (parser->lexer);
12447
12448 /* If we're looking at a keyword, we can use that to guide the
12449 production we choose. */
12450 keyword = token->keyword;
12451 switch (keyword)
12452 {
12453 case RID_ENUM:
12454 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12455 goto elaborated_type_specifier;
12456
12457 /* Look for the enum-specifier. */
12458 type_spec = cp_parser_enum_specifier (parser);
12459 /* If that worked, we're done. */
12460 if (type_spec)
12461 {
12462 if (declares_class_or_enum)
12463 *declares_class_or_enum = 2;
12464 if (decl_specs)
12465 cp_parser_set_decl_spec_type (decl_specs,
12466 type_spec,
12467 token->location,
12468 /*user_defined_p=*/true);
12469 return type_spec;
12470 }
12471 else
12472 goto elaborated_type_specifier;
12473
12474 /* Any of these indicate either a class-specifier, or an
12475 elaborated-type-specifier. */
12476 case RID_CLASS:
12477 case RID_STRUCT:
12478 case RID_UNION:
12479 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12480 goto elaborated_type_specifier;
12481
12482 /* Parse tentatively so that we can back up if we don't find a
12483 class-specifier. */
12484 cp_parser_parse_tentatively (parser);
12485 /* Look for the class-specifier. */
12486 type_spec = cp_parser_class_specifier (parser);
12487 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12488 /* If that worked, we're done. */
12489 if (cp_parser_parse_definitely (parser))
12490 {
12491 if (declares_class_or_enum)
12492 *declares_class_or_enum = 2;
12493 if (decl_specs)
12494 cp_parser_set_decl_spec_type (decl_specs,
12495 type_spec,
12496 token->location,
12497 /*user_defined_p=*/true);
12498 return type_spec;
12499 }
12500
12501 /* Fall through. */
12502 elaborated_type_specifier:
12503 /* We're declaring (not defining) a class or enum. */
12504 if (declares_class_or_enum)
12505 *declares_class_or_enum = 1;
12506
12507 /* Fall through. */
12508 case RID_TYPENAME:
12509 /* Look for an elaborated-type-specifier. */
12510 type_spec
12511 = (cp_parser_elaborated_type_specifier
12512 (parser,
12513 decl_specs && decl_specs->specs[(int) ds_friend],
12514 is_declaration));
12515 if (decl_specs)
12516 cp_parser_set_decl_spec_type (decl_specs,
12517 type_spec,
12518 token->location,
12519 /*user_defined_p=*/true);
12520 return type_spec;
12521
12522 case RID_CONST:
12523 ds = ds_const;
12524 if (is_cv_qualifier)
12525 *is_cv_qualifier = true;
12526 break;
12527
12528 case RID_VOLATILE:
12529 ds = ds_volatile;
12530 if (is_cv_qualifier)
12531 *is_cv_qualifier = true;
12532 break;
12533
12534 case RID_RESTRICT:
12535 ds = ds_restrict;
12536 if (is_cv_qualifier)
12537 *is_cv_qualifier = true;
12538 break;
12539
12540 case RID_COMPLEX:
12541 /* The `__complex__' keyword is a GNU extension. */
12542 ds = ds_complex;
12543 break;
12544
12545 default:
12546 break;
12547 }
12548
12549 /* Handle simple keywords. */
12550 if (ds != ds_last)
12551 {
12552 if (decl_specs)
12553 {
12554 ++decl_specs->specs[(int)ds];
12555 decl_specs->any_specifiers_p = true;
12556 }
12557 return cp_lexer_consume_token (parser->lexer)->u.value;
12558 }
12559
12560 /* If we do not already have a type-specifier, assume we are looking
12561 at a simple-type-specifier. */
12562 type_spec = cp_parser_simple_type_specifier (parser,
12563 decl_specs,
12564 flags);
12565
12566 /* If we didn't find a type-specifier, and a type-specifier was not
12567 optional in this context, issue an error message. */
12568 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12569 {
12570 cp_parser_error (parser, "expected type specifier");
12571 return error_mark_node;
12572 }
12573
12574 return type_spec;
12575 }
12576
12577 /* Parse a simple-type-specifier.
12578
12579 simple-type-specifier:
12580 :: [opt] nested-name-specifier [opt] type-name
12581 :: [opt] nested-name-specifier template template-id
12582 char
12583 wchar_t
12584 bool
12585 short
12586 int
12587 long
12588 signed
12589 unsigned
12590 float
12591 double
12592 void
12593
12594 C++0x Extension:
12595
12596 simple-type-specifier:
12597 auto
12598 decltype ( expression )
12599 char16_t
12600 char32_t
12601
12602 GNU Extension:
12603
12604 simple-type-specifier:
12605 __int128
12606 __typeof__ unary-expression
12607 __typeof__ ( type-id )
12608
12609 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12610 appropriately updated. */
12611
12612 static tree
12613 cp_parser_simple_type_specifier (cp_parser* parser,
12614 cp_decl_specifier_seq *decl_specs,
12615 cp_parser_flags flags)
12616 {
12617 tree type = NULL_TREE;
12618 cp_token *token;
12619
12620 /* Peek at the next token. */
12621 token = cp_lexer_peek_token (parser->lexer);
12622
12623 /* If we're looking at a keyword, things are easy. */
12624 switch (token->keyword)
12625 {
12626 case RID_CHAR:
12627 if (decl_specs)
12628 decl_specs->explicit_char_p = true;
12629 type = char_type_node;
12630 break;
12631 case RID_CHAR16:
12632 type = char16_type_node;
12633 break;
12634 case RID_CHAR32:
12635 type = char32_type_node;
12636 break;
12637 case RID_WCHAR:
12638 type = wchar_type_node;
12639 break;
12640 case RID_BOOL:
12641 type = boolean_type_node;
12642 break;
12643 case RID_SHORT:
12644 if (decl_specs)
12645 ++decl_specs->specs[(int) ds_short];
12646 type = short_integer_type_node;
12647 break;
12648 case RID_INT:
12649 if (decl_specs)
12650 decl_specs->explicit_int_p = true;
12651 type = integer_type_node;
12652 break;
12653 case RID_INT128:
12654 if (!int128_integer_type_node)
12655 break;
12656 if (decl_specs)
12657 decl_specs->explicit_int128_p = true;
12658 type = int128_integer_type_node;
12659 break;
12660 case RID_LONG:
12661 if (decl_specs)
12662 ++decl_specs->specs[(int) ds_long];
12663 type = long_integer_type_node;
12664 break;
12665 case RID_SIGNED:
12666 if (decl_specs)
12667 ++decl_specs->specs[(int) ds_signed];
12668 type = integer_type_node;
12669 break;
12670 case RID_UNSIGNED:
12671 if (decl_specs)
12672 ++decl_specs->specs[(int) ds_unsigned];
12673 type = unsigned_type_node;
12674 break;
12675 case RID_FLOAT:
12676 type = float_type_node;
12677 break;
12678 case RID_DOUBLE:
12679 type = double_type_node;
12680 break;
12681 case RID_VOID:
12682 type = void_type_node;
12683 break;
12684
12685 case RID_AUTO:
12686 maybe_warn_cpp0x (CPP0X_AUTO);
12687 type = make_auto ();
12688 break;
12689
12690 case RID_DECLTYPE:
12691 /* Parse the `decltype' type. */
12692 type = cp_parser_decltype (parser);
12693
12694 if (decl_specs)
12695 cp_parser_set_decl_spec_type (decl_specs, type,
12696 token->location,
12697 /*user_defined_p=*/true);
12698
12699 return type;
12700
12701 case RID_TYPEOF:
12702 /* Consume the `typeof' token. */
12703 cp_lexer_consume_token (parser->lexer);
12704 /* Parse the operand to `typeof'. */
12705 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12706 /* If it is not already a TYPE, take its type. */
12707 if (!TYPE_P (type))
12708 type = finish_typeof (type);
12709
12710 if (decl_specs)
12711 cp_parser_set_decl_spec_type (decl_specs, type,
12712 token->location,
12713 /*user_defined_p=*/true);
12714
12715 return type;
12716
12717 default:
12718 break;
12719 }
12720
12721 /* If the type-specifier was for a built-in type, we're done. */
12722 if (type)
12723 {
12724 /* Record the type. */
12725 if (decl_specs
12726 && (token->keyword != RID_SIGNED
12727 && token->keyword != RID_UNSIGNED
12728 && token->keyword != RID_SHORT
12729 && token->keyword != RID_LONG))
12730 cp_parser_set_decl_spec_type (decl_specs,
12731 type,
12732 token->location,
12733 /*user_defined=*/false);
12734 if (decl_specs)
12735 decl_specs->any_specifiers_p = true;
12736
12737 /* Consume the token. */
12738 cp_lexer_consume_token (parser->lexer);
12739
12740 /* There is no valid C++ program where a non-template type is
12741 followed by a "<". That usually indicates that the user thought
12742 that the type was a template. */
12743 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12744
12745 return TYPE_NAME (type);
12746 }
12747
12748 /* The type-specifier must be a user-defined type. */
12749 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12750 {
12751 bool qualified_p;
12752 bool global_p;
12753
12754 /* Don't gobble tokens or issue error messages if this is an
12755 optional type-specifier. */
12756 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12757 cp_parser_parse_tentatively (parser);
12758
12759 /* Look for the optional `::' operator. */
12760 global_p
12761 = (cp_parser_global_scope_opt (parser,
12762 /*current_scope_valid_p=*/false)
12763 != NULL_TREE);
12764 /* Look for the nested-name specifier. */
12765 qualified_p
12766 = (cp_parser_nested_name_specifier_opt (parser,
12767 /*typename_keyword_p=*/false,
12768 /*check_dependency_p=*/true,
12769 /*type_p=*/false,
12770 /*is_declaration=*/false)
12771 != NULL_TREE);
12772 token = cp_lexer_peek_token (parser->lexer);
12773 /* If we have seen a nested-name-specifier, and the next token
12774 is `template', then we are using the template-id production. */
12775 if (parser->scope
12776 && cp_parser_optional_template_keyword (parser))
12777 {
12778 /* Look for the template-id. */
12779 type = cp_parser_template_id (parser,
12780 /*template_keyword_p=*/true,
12781 /*check_dependency_p=*/true,
12782 /*is_declaration=*/false);
12783 /* If the template-id did not name a type, we are out of
12784 luck. */
12785 if (TREE_CODE (type) != TYPE_DECL)
12786 {
12787 cp_parser_error (parser, "expected template-id for type");
12788 type = NULL_TREE;
12789 }
12790 }
12791 /* Otherwise, look for a type-name. */
12792 else
12793 type = cp_parser_type_name (parser);
12794 /* Keep track of all name-lookups performed in class scopes. */
12795 if (type
12796 && !global_p
12797 && !qualified_p
12798 && TREE_CODE (type) == TYPE_DECL
12799 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12800 maybe_note_name_used_in_class (DECL_NAME (type), type);
12801 /* If it didn't work out, we don't have a TYPE. */
12802 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12803 && !cp_parser_parse_definitely (parser))
12804 type = NULL_TREE;
12805 if (type && decl_specs)
12806 cp_parser_set_decl_spec_type (decl_specs, type,
12807 token->location,
12808 /*user_defined=*/true);
12809 }
12810
12811 /* If we didn't get a type-name, issue an error message. */
12812 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12813 {
12814 cp_parser_error (parser, "expected type-name");
12815 return error_mark_node;
12816 }
12817
12818 if (type && type != error_mark_node)
12819 {
12820 /* See if TYPE is an Objective-C type, and if so, parse and
12821 accept any protocol references following it. Do this before
12822 the cp_parser_check_for_invalid_template_id() call, because
12823 Objective-C types can be followed by '<...>' which would
12824 enclose protocol names rather than template arguments, and so
12825 everything is fine. */
12826 if (c_dialect_objc () && !parser->scope
12827 && (objc_is_id (type) || objc_is_class_name (type)))
12828 {
12829 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12830 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12831
12832 /* Clobber the "unqualified" type previously entered into
12833 DECL_SPECS with the new, improved protocol-qualified version. */
12834 if (decl_specs)
12835 decl_specs->type = qual_type;
12836
12837 return qual_type;
12838 }
12839
12840 /* There is no valid C++ program where a non-template type is
12841 followed by a "<". That usually indicates that the user
12842 thought that the type was a template. */
12843 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12844 token->location);
12845 }
12846
12847 return type;
12848 }
12849
12850 /* Parse a type-name.
12851
12852 type-name:
12853 class-name
12854 enum-name
12855 typedef-name
12856
12857 enum-name:
12858 identifier
12859
12860 typedef-name:
12861 identifier
12862
12863 Returns a TYPE_DECL for the type. */
12864
12865 static tree
12866 cp_parser_type_name (cp_parser* parser)
12867 {
12868 tree type_decl;
12869
12870 /* We can't know yet whether it is a class-name or not. */
12871 cp_parser_parse_tentatively (parser);
12872 /* Try a class-name. */
12873 type_decl = cp_parser_class_name (parser,
12874 /*typename_keyword_p=*/false,
12875 /*template_keyword_p=*/false,
12876 none_type,
12877 /*check_dependency_p=*/true,
12878 /*class_head_p=*/false,
12879 /*is_declaration=*/false);
12880 /* If it's not a class-name, keep looking. */
12881 if (!cp_parser_parse_definitely (parser))
12882 {
12883 /* It must be a typedef-name or an enum-name. */
12884 return cp_parser_nonclass_name (parser);
12885 }
12886
12887 return type_decl;
12888 }
12889
12890 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12891
12892 enum-name:
12893 identifier
12894
12895 typedef-name:
12896 identifier
12897
12898 Returns a TYPE_DECL for the type. */
12899
12900 static tree
12901 cp_parser_nonclass_name (cp_parser* parser)
12902 {
12903 tree type_decl;
12904 tree identifier;
12905
12906 cp_token *token = cp_lexer_peek_token (parser->lexer);
12907 identifier = cp_parser_identifier (parser);
12908 if (identifier == error_mark_node)
12909 return error_mark_node;
12910
12911 /* Look up the type-name. */
12912 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12913
12914 if (TREE_CODE (type_decl) != TYPE_DECL
12915 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12916 {
12917 /* See if this is an Objective-C type. */
12918 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12919 tree type = objc_get_protocol_qualified_type (identifier, protos);
12920 if (type)
12921 type_decl = TYPE_NAME (type);
12922 }
12923
12924 /* Issue an error if we did not find a type-name. */
12925 if (TREE_CODE (type_decl) != TYPE_DECL
12926 /* In Objective-C, we have the complication that class names are
12927 normally type names and start declarations (eg, the
12928 "NSObject" in "NSObject *object;"), but can be used in an
12929 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12930 is an expression. So, a classname followed by a dot is not a
12931 valid type-name. */
12932 || (objc_is_class_name (TREE_TYPE (type_decl))
12933 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12934 {
12935 if (!cp_parser_simulate_error (parser))
12936 cp_parser_name_lookup_error (parser, identifier, type_decl,
12937 NLE_TYPE, token->location);
12938 return error_mark_node;
12939 }
12940 /* Remember that the name was used in the definition of the
12941 current class so that we can check later to see if the
12942 meaning would have been different after the class was
12943 entirely defined. */
12944 else if (type_decl != error_mark_node
12945 && !parser->scope)
12946 maybe_note_name_used_in_class (identifier, type_decl);
12947
12948 return type_decl;
12949 }
12950
12951 /* Parse an elaborated-type-specifier. Note that the grammar given
12952 here incorporates the resolution to DR68.
12953
12954 elaborated-type-specifier:
12955 class-key :: [opt] nested-name-specifier [opt] identifier
12956 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12957 enum-key :: [opt] nested-name-specifier [opt] identifier
12958 typename :: [opt] nested-name-specifier identifier
12959 typename :: [opt] nested-name-specifier template [opt]
12960 template-id
12961
12962 GNU extension:
12963
12964 elaborated-type-specifier:
12965 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12966 class-key attributes :: [opt] nested-name-specifier [opt]
12967 template [opt] template-id
12968 enum attributes :: [opt] nested-name-specifier [opt] identifier
12969
12970 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12971 declared `friend'. If IS_DECLARATION is TRUE, then this
12972 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12973 something is being declared.
12974
12975 Returns the TYPE specified. */
12976
12977 static tree
12978 cp_parser_elaborated_type_specifier (cp_parser* parser,
12979 bool is_friend,
12980 bool is_declaration)
12981 {
12982 enum tag_types tag_type;
12983 tree identifier;
12984 tree type = NULL_TREE;
12985 tree attributes = NULL_TREE;
12986 tree globalscope;
12987 cp_token *token = NULL;
12988
12989 /* See if we're looking at the `enum' keyword. */
12990 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12991 {
12992 /* Consume the `enum' token. */
12993 cp_lexer_consume_token (parser->lexer);
12994 /* Remember that it's an enumeration type. */
12995 tag_type = enum_type;
12996 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
12997 enums) is used here. */
12998 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12999 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13000 {
13001 pedwarn (input_location, 0, "elaborated-type-specifier "
13002 "for a scoped enum must not use the %<%D%> keyword",
13003 cp_lexer_peek_token (parser->lexer)->u.value);
13004 /* Consume the `struct' or `class' and parse it anyway. */
13005 cp_lexer_consume_token (parser->lexer);
13006 }
13007 /* Parse the attributes. */
13008 attributes = cp_parser_attributes_opt (parser);
13009 }
13010 /* Or, it might be `typename'. */
13011 else if (cp_lexer_next_token_is_keyword (parser->lexer,
13012 RID_TYPENAME))
13013 {
13014 /* Consume the `typename' token. */
13015 cp_lexer_consume_token (parser->lexer);
13016 /* Remember that it's a `typename' type. */
13017 tag_type = typename_type;
13018 }
13019 /* Otherwise it must be a class-key. */
13020 else
13021 {
13022 tag_type = cp_parser_class_key (parser);
13023 if (tag_type == none_type)
13024 return error_mark_node;
13025 /* Parse the attributes. */
13026 attributes = cp_parser_attributes_opt (parser);
13027 }
13028
13029 /* Look for the `::' operator. */
13030 globalscope = cp_parser_global_scope_opt (parser,
13031 /*current_scope_valid_p=*/false);
13032 /* Look for the nested-name-specifier. */
13033 if (tag_type == typename_type && !globalscope)
13034 {
13035 if (!cp_parser_nested_name_specifier (parser,
13036 /*typename_keyword_p=*/true,
13037 /*check_dependency_p=*/true,
13038 /*type_p=*/true,
13039 is_declaration))
13040 return error_mark_node;
13041 }
13042 else
13043 /* Even though `typename' is not present, the proposed resolution
13044 to Core Issue 180 says that in `class A<T>::B', `B' should be
13045 considered a type-name, even if `A<T>' is dependent. */
13046 cp_parser_nested_name_specifier_opt (parser,
13047 /*typename_keyword_p=*/true,
13048 /*check_dependency_p=*/true,
13049 /*type_p=*/true,
13050 is_declaration);
13051 /* For everything but enumeration types, consider a template-id.
13052 For an enumeration type, consider only a plain identifier. */
13053 if (tag_type != enum_type)
13054 {
13055 bool template_p = false;
13056 tree decl;
13057
13058 /* Allow the `template' keyword. */
13059 template_p = cp_parser_optional_template_keyword (parser);
13060 /* If we didn't see `template', we don't know if there's a
13061 template-id or not. */
13062 if (!template_p)
13063 cp_parser_parse_tentatively (parser);
13064 /* Parse the template-id. */
13065 token = cp_lexer_peek_token (parser->lexer);
13066 decl = cp_parser_template_id (parser, template_p,
13067 /*check_dependency_p=*/true,
13068 is_declaration);
13069 /* If we didn't find a template-id, look for an ordinary
13070 identifier. */
13071 if (!template_p && !cp_parser_parse_definitely (parser))
13072 ;
13073 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13074 in effect, then we must assume that, upon instantiation, the
13075 template will correspond to a class. */
13076 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13077 && tag_type == typename_type)
13078 type = make_typename_type (parser->scope, decl,
13079 typename_type,
13080 /*complain=*/tf_error);
13081 /* If the `typename' keyword is in effect and DECL is not a type
13082 decl. Then type is non existant. */
13083 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13084 type = NULL_TREE;
13085 else
13086 type = TREE_TYPE (decl);
13087 }
13088
13089 if (!type)
13090 {
13091 token = cp_lexer_peek_token (parser->lexer);
13092 identifier = cp_parser_identifier (parser);
13093
13094 if (identifier == error_mark_node)
13095 {
13096 parser->scope = NULL_TREE;
13097 return error_mark_node;
13098 }
13099
13100 /* For a `typename', we needn't call xref_tag. */
13101 if (tag_type == typename_type
13102 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13103 return cp_parser_make_typename_type (parser, parser->scope,
13104 identifier,
13105 token->location);
13106 /* Look up a qualified name in the usual way. */
13107 if (parser->scope)
13108 {
13109 tree decl;
13110 tree ambiguous_decls;
13111
13112 decl = cp_parser_lookup_name (parser, identifier,
13113 tag_type,
13114 /*is_template=*/false,
13115 /*is_namespace=*/false,
13116 /*check_dependency=*/true,
13117 &ambiguous_decls,
13118 token->location);
13119
13120 /* If the lookup was ambiguous, an error will already have been
13121 issued. */
13122 if (ambiguous_decls)
13123 return error_mark_node;
13124
13125 /* If we are parsing friend declaration, DECL may be a
13126 TEMPLATE_DECL tree node here. However, we need to check
13127 whether this TEMPLATE_DECL results in valid code. Consider
13128 the following example:
13129
13130 namespace N {
13131 template <class T> class C {};
13132 }
13133 class X {
13134 template <class T> friend class N::C; // #1, valid code
13135 };
13136 template <class T> class Y {
13137 friend class N::C; // #2, invalid code
13138 };
13139
13140 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13141 name lookup of `N::C'. We see that friend declaration must
13142 be template for the code to be valid. Note that
13143 processing_template_decl does not work here since it is
13144 always 1 for the above two cases. */
13145
13146 decl = (cp_parser_maybe_treat_template_as_class
13147 (decl, /*tag_name_p=*/is_friend
13148 && parser->num_template_parameter_lists));
13149
13150 if (TREE_CODE (decl) != TYPE_DECL)
13151 {
13152 cp_parser_diagnose_invalid_type_name (parser,
13153 parser->scope,
13154 identifier,
13155 token->location);
13156 return error_mark_node;
13157 }
13158
13159 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13160 {
13161 bool allow_template = (parser->num_template_parameter_lists
13162 || DECL_SELF_REFERENCE_P (decl));
13163 type = check_elaborated_type_specifier (tag_type, decl,
13164 allow_template);
13165
13166 if (type == error_mark_node)
13167 return error_mark_node;
13168 }
13169
13170 /* Forward declarations of nested types, such as
13171
13172 class C1::C2;
13173 class C1::C2::C3;
13174
13175 are invalid unless all components preceding the final '::'
13176 are complete. If all enclosing types are complete, these
13177 declarations become merely pointless.
13178
13179 Invalid forward declarations of nested types are errors
13180 caught elsewhere in parsing. Those that are pointless arrive
13181 here. */
13182
13183 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13184 && !is_friend && !processing_explicit_instantiation)
13185 warning (0, "declaration %qD does not declare anything", decl);
13186
13187 type = TREE_TYPE (decl);
13188 }
13189 else
13190 {
13191 /* An elaborated-type-specifier sometimes introduces a new type and
13192 sometimes names an existing type. Normally, the rule is that it
13193 introduces a new type only if there is not an existing type of
13194 the same name already in scope. For example, given:
13195
13196 struct S {};
13197 void f() { struct S s; }
13198
13199 the `struct S' in the body of `f' is the same `struct S' as in
13200 the global scope; the existing definition is used. However, if
13201 there were no global declaration, this would introduce a new
13202 local class named `S'.
13203
13204 An exception to this rule applies to the following code:
13205
13206 namespace N { struct S; }
13207
13208 Here, the elaborated-type-specifier names a new type
13209 unconditionally; even if there is already an `S' in the
13210 containing scope this declaration names a new type.
13211 This exception only applies if the elaborated-type-specifier
13212 forms the complete declaration:
13213
13214 [class.name]
13215
13216 A declaration consisting solely of `class-key identifier ;' is
13217 either a redeclaration of the name in the current scope or a
13218 forward declaration of the identifier as a class name. It
13219 introduces the name into the current scope.
13220
13221 We are in this situation precisely when the next token is a `;'.
13222
13223 An exception to the exception is that a `friend' declaration does
13224 *not* name a new type; i.e., given:
13225
13226 struct S { friend struct T; };
13227
13228 `T' is not a new type in the scope of `S'.
13229
13230 Also, `new struct S' or `sizeof (struct S)' never results in the
13231 definition of a new type; a new type can only be declared in a
13232 declaration context. */
13233
13234 tag_scope ts;
13235 bool template_p;
13236
13237 if (is_friend)
13238 /* Friends have special name lookup rules. */
13239 ts = ts_within_enclosing_non_class;
13240 else if (is_declaration
13241 && cp_lexer_next_token_is (parser->lexer,
13242 CPP_SEMICOLON))
13243 /* This is a `class-key identifier ;' */
13244 ts = ts_current;
13245 else
13246 ts = ts_global;
13247
13248 template_p =
13249 (parser->num_template_parameter_lists
13250 && (cp_parser_next_token_starts_class_definition_p (parser)
13251 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13252 /* An unqualified name was used to reference this type, so
13253 there were no qualifying templates. */
13254 if (!cp_parser_check_template_parameters (parser,
13255 /*num_templates=*/0,
13256 token->location,
13257 /*declarator=*/NULL))
13258 return error_mark_node;
13259 type = xref_tag (tag_type, identifier, ts, template_p);
13260 }
13261 }
13262
13263 if (type == error_mark_node)
13264 return error_mark_node;
13265
13266 /* Allow attributes on forward declarations of classes. */
13267 if (attributes)
13268 {
13269 if (TREE_CODE (type) == TYPENAME_TYPE)
13270 warning (OPT_Wattributes,
13271 "attributes ignored on uninstantiated type");
13272 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13273 && ! processing_explicit_instantiation)
13274 warning (OPT_Wattributes,
13275 "attributes ignored on template instantiation");
13276 else if (is_declaration && cp_parser_declares_only_class_p (parser))
13277 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13278 else
13279 warning (OPT_Wattributes,
13280 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13281 }
13282
13283 if (tag_type != enum_type)
13284 cp_parser_check_class_key (tag_type, type);
13285
13286 /* A "<" cannot follow an elaborated type specifier. If that
13287 happens, the user was probably trying to form a template-id. */
13288 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13289
13290 return type;
13291 }
13292
13293 /* Parse an enum-specifier.
13294
13295 enum-specifier:
13296 enum-head { enumerator-list [opt] }
13297
13298 enum-head:
13299 enum-key identifier [opt] enum-base [opt]
13300 enum-key nested-name-specifier identifier enum-base [opt]
13301
13302 enum-key:
13303 enum
13304 enum class [C++0x]
13305 enum struct [C++0x]
13306
13307 enum-base: [C++0x]
13308 : type-specifier-seq
13309
13310 opaque-enum-specifier:
13311 enum-key identifier enum-base [opt] ;
13312
13313 GNU Extensions:
13314 enum-key attributes[opt] identifier [opt] enum-base [opt]
13315 { enumerator-list [opt] }attributes[opt]
13316
13317 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13318 if the token stream isn't an enum-specifier after all. */
13319
13320 static tree
13321 cp_parser_enum_specifier (cp_parser* parser)
13322 {
13323 tree identifier;
13324 tree type = NULL_TREE;
13325 tree prev_scope;
13326 tree nested_name_specifier = NULL_TREE;
13327 tree attributes;
13328 bool scoped_enum_p = false;
13329 bool has_underlying_type = false;
13330 bool nested_being_defined = false;
13331 bool new_value_list = false;
13332 bool is_new_type = false;
13333 bool is_anonymous = false;
13334 tree underlying_type = NULL_TREE;
13335 cp_token *type_start_token = NULL;
13336
13337 /* Parse tentatively so that we can back up if we don't find a
13338 enum-specifier. */
13339 cp_parser_parse_tentatively (parser);
13340
13341 /* Caller guarantees that the current token is 'enum', an identifier
13342 possibly follows, and the token after that is an opening brace.
13343 If we don't have an identifier, fabricate an anonymous name for
13344 the enumeration being defined. */
13345 cp_lexer_consume_token (parser->lexer);
13346
13347 /* Parse the "class" or "struct", which indicates a scoped
13348 enumeration type in C++0x. */
13349 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13350 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13351 {
13352 if (cxx_dialect < cxx0x)
13353 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13354
13355 /* Consume the `struct' or `class' token. */
13356 cp_lexer_consume_token (parser->lexer);
13357
13358 scoped_enum_p = true;
13359 }
13360
13361 attributes = cp_parser_attributes_opt (parser);
13362
13363 /* Clear the qualification. */
13364 parser->scope = NULL_TREE;
13365 parser->qualifying_scope = NULL_TREE;
13366 parser->object_scope = NULL_TREE;
13367
13368 /* Figure out in what scope the declaration is being placed. */
13369 prev_scope = current_scope ();
13370
13371 type_start_token = cp_lexer_peek_token (parser->lexer);
13372
13373 push_deferring_access_checks (dk_no_check);
13374 nested_name_specifier
13375 = cp_parser_nested_name_specifier_opt (parser,
13376 /*typename_keyword_p=*/true,
13377 /*check_dependency_p=*/false,
13378 /*type_p=*/false,
13379 /*is_declaration=*/false);
13380
13381 if (nested_name_specifier)
13382 {
13383 tree name;
13384
13385 identifier = cp_parser_identifier (parser);
13386 name = cp_parser_lookup_name (parser, identifier,
13387 enum_type,
13388 /*is_template=*/false,
13389 /*is_namespace=*/false,
13390 /*check_dependency=*/true,
13391 /*ambiguous_decls=*/NULL,
13392 input_location);
13393 if (name)
13394 {
13395 type = TREE_TYPE (name);
13396 if (TREE_CODE (type) == TYPENAME_TYPE)
13397 {
13398 /* Are template enums allowed in ISO? */
13399 if (template_parm_scope_p ())
13400 pedwarn (type_start_token->location, OPT_pedantic,
13401 "%qD is an enumeration template", name);
13402 /* ignore a typename reference, for it will be solved by name
13403 in start_enum. */
13404 type = NULL_TREE;
13405 }
13406 }
13407 else
13408 error_at (type_start_token->location,
13409 "%qD is not an enumerator-name", identifier);
13410 }
13411 else
13412 {
13413 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13414 identifier = cp_parser_identifier (parser);
13415 else
13416 {
13417 identifier = make_anon_name ();
13418 is_anonymous = true;
13419 }
13420 }
13421 pop_deferring_access_checks ();
13422
13423 /* Check for the `:' that denotes a specified underlying type in C++0x.
13424 Note that a ':' could also indicate a bitfield width, however. */
13425 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13426 {
13427 cp_decl_specifier_seq type_specifiers;
13428
13429 /* Consume the `:'. */
13430 cp_lexer_consume_token (parser->lexer);
13431
13432 /* Parse the type-specifier-seq. */
13433 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13434 /*is_trailing_return=*/false,
13435 &type_specifiers);
13436
13437 /* At this point this is surely not elaborated type specifier. */
13438 if (!cp_parser_parse_definitely (parser))
13439 return NULL_TREE;
13440
13441 if (cxx_dialect < cxx0x)
13442 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13443
13444 has_underlying_type = true;
13445
13446 /* If that didn't work, stop. */
13447 if (type_specifiers.type != error_mark_node)
13448 {
13449 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13450 /*initialized=*/0, NULL);
13451 if (underlying_type == error_mark_node)
13452 underlying_type = NULL_TREE;
13453 }
13454 }
13455
13456 /* Look for the `{' but don't consume it yet. */
13457 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13458 {
13459 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13460 {
13461 cp_parser_error (parser, "expected %<{%>");
13462 if (has_underlying_type)
13463 return NULL_TREE;
13464 }
13465 /* An opaque-enum-specifier must have a ';' here. */
13466 if ((scoped_enum_p || underlying_type)
13467 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13468 {
13469 cp_parser_error (parser, "expected %<;%> or %<{%>");
13470 if (has_underlying_type)
13471 return NULL_TREE;
13472 }
13473 }
13474
13475 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13476 return NULL_TREE;
13477
13478 if (nested_name_specifier)
13479 {
13480 if (CLASS_TYPE_P (nested_name_specifier))
13481 {
13482 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13483 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13484 push_scope (nested_name_specifier);
13485 }
13486 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13487 {
13488 push_nested_namespace (nested_name_specifier);
13489 }
13490 }
13491
13492 /* Issue an error message if type-definitions are forbidden here. */
13493 if (!cp_parser_check_type_definition (parser))
13494 type = error_mark_node;
13495 else
13496 /* Create the new type. We do this before consuming the opening
13497 brace so the enum will be recorded as being on the line of its
13498 tag (or the 'enum' keyword, if there is no tag). */
13499 type = start_enum (identifier, type, underlying_type,
13500 scoped_enum_p, &is_new_type);
13501
13502 /* If the next token is not '{' it is an opaque-enum-specifier or an
13503 elaborated-type-specifier. */
13504 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13505 {
13506 if (nested_name_specifier)
13507 {
13508 /* The following catches invalid code such as:
13509 enum class S<int>::E { A, B, C }; */
13510 if (!processing_specialization
13511 && CLASS_TYPE_P (nested_name_specifier)
13512 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13513 error_at (type_start_token->location, "cannot add an enumerator "
13514 "list to a template instantiation");
13515
13516 /* If that scope does not contain the scope in which the
13517 class was originally declared, the program is invalid. */
13518 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13519 {
13520 if (at_namespace_scope_p ())
13521 error_at (type_start_token->location,
13522 "declaration of %qD in namespace %qD which does not "
13523 "enclose %qD",
13524 type, prev_scope, nested_name_specifier);
13525 else
13526 error_at (type_start_token->location,
13527 "declaration of %qD in %qD which does not enclose %qD",
13528 type, prev_scope, nested_name_specifier);
13529 type = error_mark_node;
13530 }
13531 }
13532
13533 if (scoped_enum_p)
13534 begin_scope (sk_scoped_enum, type);
13535
13536 /* Consume the opening brace. */
13537 cp_lexer_consume_token (parser->lexer);
13538
13539 if (type == error_mark_node)
13540 ; /* Nothing to add */
13541 else if (OPAQUE_ENUM_P (type)
13542 || (cxx_dialect > cxx98 && processing_specialization))
13543 {
13544 new_value_list = true;
13545 SET_OPAQUE_ENUM_P (type, false);
13546 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13547 }
13548 else
13549 {
13550 error_at (type_start_token->location, "multiple definition of %q#T", type);
13551 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13552 "previous definition here");
13553 type = error_mark_node;
13554 }
13555
13556 if (type == error_mark_node)
13557 cp_parser_skip_to_end_of_block_or_statement (parser);
13558 /* If the next token is not '}', then there are some enumerators. */
13559 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13560 cp_parser_enumerator_list (parser, type);
13561
13562 /* Consume the final '}'. */
13563 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13564
13565 if (scoped_enum_p)
13566 finish_scope ();
13567 }
13568 else
13569 {
13570 /* If a ';' follows, then it is an opaque-enum-specifier
13571 and additional restrictions apply. */
13572 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13573 {
13574 if (is_anonymous)
13575 error_at (type_start_token->location,
13576 "opaque-enum-specifier without name");
13577 else if (nested_name_specifier)
13578 error_at (type_start_token->location,
13579 "opaque-enum-specifier must use a simple identifier");
13580 }
13581 }
13582
13583 /* Look for trailing attributes to apply to this enumeration, and
13584 apply them if appropriate. */
13585 if (cp_parser_allow_gnu_extensions_p (parser))
13586 {
13587 tree trailing_attr = cp_parser_attributes_opt (parser);
13588 trailing_attr = chainon (trailing_attr, attributes);
13589 cplus_decl_attributes (&type,
13590 trailing_attr,
13591 (int) ATTR_FLAG_TYPE_IN_PLACE);
13592 }
13593
13594 /* Finish up the enumeration. */
13595 if (type != error_mark_node)
13596 {
13597 if (new_value_list)
13598 finish_enum_value_list (type);
13599 if (is_new_type)
13600 finish_enum (type);
13601 }
13602
13603 if (nested_name_specifier)
13604 {
13605 if (CLASS_TYPE_P (nested_name_specifier))
13606 {
13607 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13608 pop_scope (nested_name_specifier);
13609 }
13610 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13611 {
13612 pop_nested_namespace (nested_name_specifier);
13613 }
13614 }
13615 return type;
13616 }
13617
13618 /* Parse an enumerator-list. The enumerators all have the indicated
13619 TYPE.
13620
13621 enumerator-list:
13622 enumerator-definition
13623 enumerator-list , enumerator-definition */
13624
13625 static void
13626 cp_parser_enumerator_list (cp_parser* parser, tree type)
13627 {
13628 while (true)
13629 {
13630 /* Parse an enumerator-definition. */
13631 cp_parser_enumerator_definition (parser, type);
13632
13633 /* If the next token is not a ',', we've reached the end of
13634 the list. */
13635 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13636 break;
13637 /* Otherwise, consume the `,' and keep going. */
13638 cp_lexer_consume_token (parser->lexer);
13639 /* If the next token is a `}', there is a trailing comma. */
13640 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13641 {
13642 if (!in_system_header)
13643 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13644 break;
13645 }
13646 }
13647 }
13648
13649 /* Parse an enumerator-definition. The enumerator has the indicated
13650 TYPE.
13651
13652 enumerator-definition:
13653 enumerator
13654 enumerator = constant-expression
13655
13656 enumerator:
13657 identifier */
13658
13659 static void
13660 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13661 {
13662 tree identifier;
13663 tree value;
13664 location_t loc;
13665
13666 /* Save the input location because we are interested in the location
13667 of the identifier and not the location of the explicit value. */
13668 loc = cp_lexer_peek_token (parser->lexer)->location;
13669
13670 /* Look for the identifier. */
13671 identifier = cp_parser_identifier (parser);
13672 if (identifier == error_mark_node)
13673 return;
13674
13675 /* If the next token is an '=', then there is an explicit value. */
13676 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13677 {
13678 /* Consume the `=' token. */
13679 cp_lexer_consume_token (parser->lexer);
13680 /* Parse the value. */
13681 value = cp_parser_constant_expression (parser,
13682 /*allow_non_constant_p=*/false,
13683 NULL);
13684 }
13685 else
13686 value = NULL_TREE;
13687
13688 /* If we are processing a template, make sure the initializer of the
13689 enumerator doesn't contain any bare template parameter pack. */
13690 if (check_for_bare_parameter_packs (value))
13691 value = error_mark_node;
13692
13693 /* Create the enumerator. */
13694 build_enumerator (identifier, value, type, loc);
13695 }
13696
13697 /* Parse a namespace-name.
13698
13699 namespace-name:
13700 original-namespace-name
13701 namespace-alias
13702
13703 Returns the NAMESPACE_DECL for the namespace. */
13704
13705 static tree
13706 cp_parser_namespace_name (cp_parser* parser)
13707 {
13708 tree identifier;
13709 tree namespace_decl;
13710
13711 cp_token *token = cp_lexer_peek_token (parser->lexer);
13712
13713 /* Get the name of the namespace. */
13714 identifier = cp_parser_identifier (parser);
13715 if (identifier == error_mark_node)
13716 return error_mark_node;
13717
13718 /* Look up the identifier in the currently active scope. Look only
13719 for namespaces, due to:
13720
13721 [basic.lookup.udir]
13722
13723 When looking up a namespace-name in a using-directive or alias
13724 definition, only namespace names are considered.
13725
13726 And:
13727
13728 [basic.lookup.qual]
13729
13730 During the lookup of a name preceding the :: scope resolution
13731 operator, object, function, and enumerator names are ignored.
13732
13733 (Note that cp_parser_qualifying_entity only calls this
13734 function if the token after the name is the scope resolution
13735 operator.) */
13736 namespace_decl = cp_parser_lookup_name (parser, identifier,
13737 none_type,
13738 /*is_template=*/false,
13739 /*is_namespace=*/true,
13740 /*check_dependency=*/true,
13741 /*ambiguous_decls=*/NULL,
13742 token->location);
13743 /* If it's not a namespace, issue an error. */
13744 if (namespace_decl == error_mark_node
13745 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13746 {
13747 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13748 error_at (token->location, "%qD is not a namespace-name", identifier);
13749 cp_parser_error (parser, "expected namespace-name");
13750 namespace_decl = error_mark_node;
13751 }
13752
13753 return namespace_decl;
13754 }
13755
13756 /* Parse a namespace-definition.
13757
13758 namespace-definition:
13759 named-namespace-definition
13760 unnamed-namespace-definition
13761
13762 named-namespace-definition:
13763 original-namespace-definition
13764 extension-namespace-definition
13765
13766 original-namespace-definition:
13767 namespace identifier { namespace-body }
13768
13769 extension-namespace-definition:
13770 namespace original-namespace-name { namespace-body }
13771
13772 unnamed-namespace-definition:
13773 namespace { namespace-body } */
13774
13775 static void
13776 cp_parser_namespace_definition (cp_parser* parser)
13777 {
13778 tree identifier, attribs;
13779 bool has_visibility;
13780 bool is_inline;
13781
13782 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13783 {
13784 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13785 is_inline = true;
13786 cp_lexer_consume_token (parser->lexer);
13787 }
13788 else
13789 is_inline = false;
13790
13791 /* Look for the `namespace' keyword. */
13792 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13793
13794 /* Get the name of the namespace. We do not attempt to distinguish
13795 between an original-namespace-definition and an
13796 extension-namespace-definition at this point. The semantic
13797 analysis routines are responsible for that. */
13798 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13799 identifier = cp_parser_identifier (parser);
13800 else
13801 identifier = NULL_TREE;
13802
13803 /* Parse any specified attributes. */
13804 attribs = cp_parser_attributes_opt (parser);
13805
13806 /* Look for the `{' to start the namespace. */
13807 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13808 /* Start the namespace. */
13809 push_namespace (identifier);
13810
13811 /* "inline namespace" is equivalent to a stub namespace definition
13812 followed by a strong using directive. */
13813 if (is_inline)
13814 {
13815 tree name_space = current_namespace;
13816 /* Set up namespace association. */
13817 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13818 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13819 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13820 /* Import the contents of the inline namespace. */
13821 pop_namespace ();
13822 do_using_directive (name_space);
13823 push_namespace (identifier);
13824 }
13825
13826 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13827
13828 /* Parse the body of the namespace. */
13829 cp_parser_namespace_body (parser);
13830
13831 #ifdef HANDLE_PRAGMA_VISIBILITY
13832 if (has_visibility)
13833 pop_visibility (1);
13834 #endif
13835
13836 /* Finish the namespace. */
13837 pop_namespace ();
13838 /* Look for the final `}'. */
13839 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13840 }
13841
13842 /* Parse a namespace-body.
13843
13844 namespace-body:
13845 declaration-seq [opt] */
13846
13847 static void
13848 cp_parser_namespace_body (cp_parser* parser)
13849 {
13850 cp_parser_declaration_seq_opt (parser);
13851 }
13852
13853 /* Parse a namespace-alias-definition.
13854
13855 namespace-alias-definition:
13856 namespace identifier = qualified-namespace-specifier ; */
13857
13858 static void
13859 cp_parser_namespace_alias_definition (cp_parser* parser)
13860 {
13861 tree identifier;
13862 tree namespace_specifier;
13863
13864 cp_token *token = cp_lexer_peek_token (parser->lexer);
13865
13866 /* Look for the `namespace' keyword. */
13867 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13868 /* Look for the identifier. */
13869 identifier = cp_parser_identifier (parser);
13870 if (identifier == error_mark_node)
13871 return;
13872 /* Look for the `=' token. */
13873 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13874 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13875 {
13876 error_at (token->location, "%<namespace%> definition is not allowed here");
13877 /* Skip the definition. */
13878 cp_lexer_consume_token (parser->lexer);
13879 if (cp_parser_skip_to_closing_brace (parser))
13880 cp_lexer_consume_token (parser->lexer);
13881 return;
13882 }
13883 cp_parser_require (parser, CPP_EQ, RT_EQ);
13884 /* Look for the qualified-namespace-specifier. */
13885 namespace_specifier
13886 = cp_parser_qualified_namespace_specifier (parser);
13887 /* Look for the `;' token. */
13888 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13889
13890 /* Register the alias in the symbol table. */
13891 do_namespace_alias (identifier, namespace_specifier);
13892 }
13893
13894 /* Parse a qualified-namespace-specifier.
13895
13896 qualified-namespace-specifier:
13897 :: [opt] nested-name-specifier [opt] namespace-name
13898
13899 Returns a NAMESPACE_DECL corresponding to the specified
13900 namespace. */
13901
13902 static tree
13903 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13904 {
13905 /* Look for the optional `::'. */
13906 cp_parser_global_scope_opt (parser,
13907 /*current_scope_valid_p=*/false);
13908
13909 /* Look for the optional nested-name-specifier. */
13910 cp_parser_nested_name_specifier_opt (parser,
13911 /*typename_keyword_p=*/false,
13912 /*check_dependency_p=*/true,
13913 /*type_p=*/false,
13914 /*is_declaration=*/true);
13915
13916 return cp_parser_namespace_name (parser);
13917 }
13918
13919 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13920 access declaration.
13921
13922 using-declaration:
13923 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13924 using :: unqualified-id ;
13925
13926 access-declaration:
13927 qualified-id ;
13928
13929 */
13930
13931 static bool
13932 cp_parser_using_declaration (cp_parser* parser,
13933 bool access_declaration_p)
13934 {
13935 cp_token *token;
13936 bool typename_p = false;
13937 bool global_scope_p;
13938 tree decl;
13939 tree identifier;
13940 tree qscope;
13941
13942 if (access_declaration_p)
13943 cp_parser_parse_tentatively (parser);
13944 else
13945 {
13946 /* Look for the `using' keyword. */
13947 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13948
13949 /* Peek at the next token. */
13950 token = cp_lexer_peek_token (parser->lexer);
13951 /* See if it's `typename'. */
13952 if (token->keyword == RID_TYPENAME)
13953 {
13954 /* Remember that we've seen it. */
13955 typename_p = true;
13956 /* Consume the `typename' token. */
13957 cp_lexer_consume_token (parser->lexer);
13958 }
13959 }
13960
13961 /* Look for the optional global scope qualification. */
13962 global_scope_p
13963 = (cp_parser_global_scope_opt (parser,
13964 /*current_scope_valid_p=*/false)
13965 != NULL_TREE);
13966
13967 /* If we saw `typename', or didn't see `::', then there must be a
13968 nested-name-specifier present. */
13969 if (typename_p || !global_scope_p)
13970 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13971 /*check_dependency_p=*/true,
13972 /*type_p=*/false,
13973 /*is_declaration=*/true);
13974 /* Otherwise, we could be in either of the two productions. In that
13975 case, treat the nested-name-specifier as optional. */
13976 else
13977 qscope = cp_parser_nested_name_specifier_opt (parser,
13978 /*typename_keyword_p=*/false,
13979 /*check_dependency_p=*/true,
13980 /*type_p=*/false,
13981 /*is_declaration=*/true);
13982 if (!qscope)
13983 qscope = global_namespace;
13984
13985 if (access_declaration_p && cp_parser_error_occurred (parser))
13986 /* Something has already gone wrong; there's no need to parse
13987 further. Since an error has occurred, the return value of
13988 cp_parser_parse_definitely will be false, as required. */
13989 return cp_parser_parse_definitely (parser);
13990
13991 token = cp_lexer_peek_token (parser->lexer);
13992 /* Parse the unqualified-id. */
13993 identifier = cp_parser_unqualified_id (parser,
13994 /*template_keyword_p=*/false,
13995 /*check_dependency_p=*/true,
13996 /*declarator_p=*/true,
13997 /*optional_p=*/false);
13998
13999 if (access_declaration_p)
14000 {
14001 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14002 cp_parser_simulate_error (parser);
14003 if (!cp_parser_parse_definitely (parser))
14004 return false;
14005 }
14006
14007 /* The function we call to handle a using-declaration is different
14008 depending on what scope we are in. */
14009 if (qscope == error_mark_node || identifier == error_mark_node)
14010 ;
14011 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14012 && TREE_CODE (identifier) != BIT_NOT_EXPR)
14013 /* [namespace.udecl]
14014
14015 A using declaration shall not name a template-id. */
14016 error_at (token->location,
14017 "a template-id may not appear in a using-declaration");
14018 else
14019 {
14020 if (at_class_scope_p ())
14021 {
14022 /* Create the USING_DECL. */
14023 decl = do_class_using_decl (parser->scope, identifier);
14024
14025 if (check_for_bare_parameter_packs (decl))
14026 return false;
14027 else
14028 /* Add it to the list of members in this class. */
14029 finish_member_declaration (decl);
14030 }
14031 else
14032 {
14033 decl = cp_parser_lookup_name_simple (parser,
14034 identifier,
14035 token->location);
14036 if (decl == error_mark_node)
14037 cp_parser_name_lookup_error (parser, identifier,
14038 decl, NLE_NULL,
14039 token->location);
14040 else if (check_for_bare_parameter_packs (decl))
14041 return false;
14042 else if (!at_namespace_scope_p ())
14043 do_local_using_decl (decl, qscope, identifier);
14044 else
14045 do_toplevel_using_decl (decl, qscope, identifier);
14046 }
14047 }
14048
14049 /* Look for the final `;'. */
14050 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14051
14052 return true;
14053 }
14054
14055 /* Parse a using-directive.
14056
14057 using-directive:
14058 using namespace :: [opt] nested-name-specifier [opt]
14059 namespace-name ; */
14060
14061 static void
14062 cp_parser_using_directive (cp_parser* parser)
14063 {
14064 tree namespace_decl;
14065 tree attribs;
14066
14067 /* Look for the `using' keyword. */
14068 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14069 /* And the `namespace' keyword. */
14070 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14071 /* Look for the optional `::' operator. */
14072 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14073 /* And the optional nested-name-specifier. */
14074 cp_parser_nested_name_specifier_opt (parser,
14075 /*typename_keyword_p=*/false,
14076 /*check_dependency_p=*/true,
14077 /*type_p=*/false,
14078 /*is_declaration=*/true);
14079 /* Get the namespace being used. */
14080 namespace_decl = cp_parser_namespace_name (parser);
14081 /* And any specified attributes. */
14082 attribs = cp_parser_attributes_opt (parser);
14083 /* Update the symbol table. */
14084 parse_using_directive (namespace_decl, attribs);
14085 /* Look for the final `;'. */
14086 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14087 }
14088
14089 /* Parse an asm-definition.
14090
14091 asm-definition:
14092 asm ( string-literal ) ;
14093
14094 GNU Extension:
14095
14096 asm-definition:
14097 asm volatile [opt] ( string-literal ) ;
14098 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14099 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14100 : asm-operand-list [opt] ) ;
14101 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14102 : asm-operand-list [opt]
14103 : asm-clobber-list [opt] ) ;
14104 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14105 : asm-clobber-list [opt]
14106 : asm-goto-list ) ; */
14107
14108 static void
14109 cp_parser_asm_definition (cp_parser* parser)
14110 {
14111 tree string;
14112 tree outputs = NULL_TREE;
14113 tree inputs = NULL_TREE;
14114 tree clobbers = NULL_TREE;
14115 tree labels = NULL_TREE;
14116 tree asm_stmt;
14117 bool volatile_p = false;
14118 bool extended_p = false;
14119 bool invalid_inputs_p = false;
14120 bool invalid_outputs_p = false;
14121 bool goto_p = false;
14122 required_token missing = RT_NONE;
14123
14124 /* Look for the `asm' keyword. */
14125 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14126 /* See if the next token is `volatile'. */
14127 if (cp_parser_allow_gnu_extensions_p (parser)
14128 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14129 {
14130 /* Remember that we saw the `volatile' keyword. */
14131 volatile_p = true;
14132 /* Consume the token. */
14133 cp_lexer_consume_token (parser->lexer);
14134 }
14135 if (cp_parser_allow_gnu_extensions_p (parser)
14136 && parser->in_function_body
14137 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14138 {
14139 /* Remember that we saw the `goto' keyword. */
14140 goto_p = true;
14141 /* Consume the token. */
14142 cp_lexer_consume_token (parser->lexer);
14143 }
14144 /* Look for the opening `('. */
14145 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14146 return;
14147 /* Look for the string. */
14148 string = cp_parser_string_literal (parser, false, false);
14149 if (string == error_mark_node)
14150 {
14151 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14152 /*consume_paren=*/true);
14153 return;
14154 }
14155
14156 /* If we're allowing GNU extensions, check for the extended assembly
14157 syntax. Unfortunately, the `:' tokens need not be separated by
14158 a space in C, and so, for compatibility, we tolerate that here
14159 too. Doing that means that we have to treat the `::' operator as
14160 two `:' tokens. */
14161 if (cp_parser_allow_gnu_extensions_p (parser)
14162 && parser->in_function_body
14163 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14164 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14165 {
14166 bool inputs_p = false;
14167 bool clobbers_p = false;
14168 bool labels_p = false;
14169
14170 /* The extended syntax was used. */
14171 extended_p = true;
14172
14173 /* Look for outputs. */
14174 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14175 {
14176 /* Consume the `:'. */
14177 cp_lexer_consume_token (parser->lexer);
14178 /* Parse the output-operands. */
14179 if (cp_lexer_next_token_is_not (parser->lexer,
14180 CPP_COLON)
14181 && cp_lexer_next_token_is_not (parser->lexer,
14182 CPP_SCOPE)
14183 && cp_lexer_next_token_is_not (parser->lexer,
14184 CPP_CLOSE_PAREN)
14185 && !goto_p)
14186 outputs = cp_parser_asm_operand_list (parser);
14187
14188 if (outputs == error_mark_node)
14189 invalid_outputs_p = true;
14190 }
14191 /* If the next token is `::', there are no outputs, and the
14192 next token is the beginning of the inputs. */
14193 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14194 /* The inputs are coming next. */
14195 inputs_p = true;
14196
14197 /* Look for inputs. */
14198 if (inputs_p
14199 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14200 {
14201 /* Consume the `:' or `::'. */
14202 cp_lexer_consume_token (parser->lexer);
14203 /* Parse the output-operands. */
14204 if (cp_lexer_next_token_is_not (parser->lexer,
14205 CPP_COLON)
14206 && cp_lexer_next_token_is_not (parser->lexer,
14207 CPP_SCOPE)
14208 && cp_lexer_next_token_is_not (parser->lexer,
14209 CPP_CLOSE_PAREN))
14210 inputs = cp_parser_asm_operand_list (parser);
14211
14212 if (inputs == error_mark_node)
14213 invalid_inputs_p = true;
14214 }
14215 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14216 /* The clobbers are coming next. */
14217 clobbers_p = true;
14218
14219 /* Look for clobbers. */
14220 if (clobbers_p
14221 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14222 {
14223 clobbers_p = true;
14224 /* Consume the `:' or `::'. */
14225 cp_lexer_consume_token (parser->lexer);
14226 /* Parse the clobbers. */
14227 if (cp_lexer_next_token_is_not (parser->lexer,
14228 CPP_COLON)
14229 && cp_lexer_next_token_is_not (parser->lexer,
14230 CPP_CLOSE_PAREN))
14231 clobbers = cp_parser_asm_clobber_list (parser);
14232 }
14233 else if (goto_p
14234 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14235 /* The labels are coming next. */
14236 labels_p = true;
14237
14238 /* Look for labels. */
14239 if (labels_p
14240 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14241 {
14242 labels_p = true;
14243 /* Consume the `:' or `::'. */
14244 cp_lexer_consume_token (parser->lexer);
14245 /* Parse the labels. */
14246 labels = cp_parser_asm_label_list (parser);
14247 }
14248
14249 if (goto_p && !labels_p)
14250 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14251 }
14252 else if (goto_p)
14253 missing = RT_COLON_SCOPE;
14254
14255 /* Look for the closing `)'. */
14256 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14257 missing ? missing : RT_CLOSE_PAREN))
14258 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14259 /*consume_paren=*/true);
14260 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14261
14262 if (!invalid_inputs_p && !invalid_outputs_p)
14263 {
14264 /* Create the ASM_EXPR. */
14265 if (parser->in_function_body)
14266 {
14267 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14268 inputs, clobbers, labels);
14269 /* If the extended syntax was not used, mark the ASM_EXPR. */
14270 if (!extended_p)
14271 {
14272 tree temp = asm_stmt;
14273 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14274 temp = TREE_OPERAND (temp, 0);
14275
14276 ASM_INPUT_P (temp) = 1;
14277 }
14278 }
14279 else
14280 cgraph_add_asm_node (string);
14281 }
14282 }
14283
14284 /* Declarators [gram.dcl.decl] */
14285
14286 /* Parse an init-declarator.
14287
14288 init-declarator:
14289 declarator initializer [opt]
14290
14291 GNU Extension:
14292
14293 init-declarator:
14294 declarator asm-specification [opt] attributes [opt] initializer [opt]
14295
14296 function-definition:
14297 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14298 function-body
14299 decl-specifier-seq [opt] declarator function-try-block
14300
14301 GNU Extension:
14302
14303 function-definition:
14304 __extension__ function-definition
14305
14306 The DECL_SPECIFIERS apply to this declarator. Returns a
14307 representation of the entity declared. If MEMBER_P is TRUE, then
14308 this declarator appears in a class scope. The new DECL created by
14309 this declarator is returned.
14310
14311 The CHECKS are access checks that should be performed once we know
14312 what entity is being declared (and, therefore, what classes have
14313 befriended it).
14314
14315 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14316 for a function-definition here as well. If the declarator is a
14317 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14318 be TRUE upon return. By that point, the function-definition will
14319 have been completely parsed.
14320
14321 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14322 is FALSE. */
14323
14324 static tree
14325 cp_parser_init_declarator (cp_parser* parser,
14326 cp_decl_specifier_seq *decl_specifiers,
14327 VEC (deferred_access_check,gc)* checks,
14328 bool function_definition_allowed_p,
14329 bool member_p,
14330 int declares_class_or_enum,
14331 bool* function_definition_p)
14332 {
14333 cp_token *token = NULL, *asm_spec_start_token = NULL,
14334 *attributes_start_token = NULL;
14335 cp_declarator *declarator;
14336 tree prefix_attributes;
14337 tree attributes;
14338 tree asm_specification;
14339 tree initializer;
14340 tree decl = NULL_TREE;
14341 tree scope;
14342 int is_initialized;
14343 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14344 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14345 "(...)". */
14346 enum cpp_ttype initialization_kind;
14347 bool is_direct_init = false;
14348 bool is_non_constant_init;
14349 int ctor_dtor_or_conv_p;
14350 bool friend_p;
14351 tree pushed_scope = NULL;
14352
14353 /* Gather the attributes that were provided with the
14354 decl-specifiers. */
14355 prefix_attributes = decl_specifiers->attributes;
14356
14357 /* Assume that this is not the declarator for a function
14358 definition. */
14359 if (function_definition_p)
14360 *function_definition_p = false;
14361
14362 /* Defer access checks while parsing the declarator; we cannot know
14363 what names are accessible until we know what is being
14364 declared. */
14365 resume_deferring_access_checks ();
14366
14367 /* Parse the declarator. */
14368 token = cp_lexer_peek_token (parser->lexer);
14369 declarator
14370 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14371 &ctor_dtor_or_conv_p,
14372 /*parenthesized_p=*/NULL,
14373 /*member_p=*/false);
14374 /* Gather up the deferred checks. */
14375 stop_deferring_access_checks ();
14376
14377 /* If the DECLARATOR was erroneous, there's no need to go
14378 further. */
14379 if (declarator == cp_error_declarator)
14380 return error_mark_node;
14381
14382 /* Check that the number of template-parameter-lists is OK. */
14383 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14384 token->location))
14385 return error_mark_node;
14386
14387 if (declares_class_or_enum & 2)
14388 cp_parser_check_for_definition_in_return_type (declarator,
14389 decl_specifiers->type,
14390 decl_specifiers->type_location);
14391
14392 /* Figure out what scope the entity declared by the DECLARATOR is
14393 located in. `grokdeclarator' sometimes changes the scope, so
14394 we compute it now. */
14395 scope = get_scope_of_declarator (declarator);
14396
14397 /* Perform any lookups in the declared type which were thought to be
14398 dependent, but are not in the scope of the declarator. */
14399 decl_specifiers->type
14400 = maybe_update_decl_type (decl_specifiers->type, scope);
14401
14402 /* If we're allowing GNU extensions, look for an asm-specification
14403 and attributes. */
14404 if (cp_parser_allow_gnu_extensions_p (parser))
14405 {
14406 /* Look for an asm-specification. */
14407 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14408 asm_specification = cp_parser_asm_specification_opt (parser);
14409 /* And attributes. */
14410 attributes_start_token = cp_lexer_peek_token (parser->lexer);
14411 attributes = cp_parser_attributes_opt (parser);
14412 }
14413 else
14414 {
14415 asm_specification = NULL_TREE;
14416 attributes = NULL_TREE;
14417 }
14418
14419 /* Peek at the next token. */
14420 token = cp_lexer_peek_token (parser->lexer);
14421 /* Check to see if the token indicates the start of a
14422 function-definition. */
14423 if (function_declarator_p (declarator)
14424 && cp_parser_token_starts_function_definition_p (token))
14425 {
14426 if (!function_definition_allowed_p)
14427 {
14428 /* If a function-definition should not appear here, issue an
14429 error message. */
14430 cp_parser_error (parser,
14431 "a function-definition is not allowed here");
14432 return error_mark_node;
14433 }
14434 else
14435 {
14436 location_t func_brace_location
14437 = cp_lexer_peek_token (parser->lexer)->location;
14438
14439 /* Neither attributes nor an asm-specification are allowed
14440 on a function-definition. */
14441 if (asm_specification)
14442 error_at (asm_spec_start_token->location,
14443 "an asm-specification is not allowed "
14444 "on a function-definition");
14445 if (attributes)
14446 error_at (attributes_start_token->location,
14447 "attributes are not allowed on a function-definition");
14448 /* This is a function-definition. */
14449 *function_definition_p = true;
14450
14451 /* Parse the function definition. */
14452 if (member_p)
14453 decl = cp_parser_save_member_function_body (parser,
14454 decl_specifiers,
14455 declarator,
14456 prefix_attributes);
14457 else
14458 decl
14459 = (cp_parser_function_definition_from_specifiers_and_declarator
14460 (parser, decl_specifiers, prefix_attributes, declarator));
14461
14462 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14463 {
14464 /* This is where the prologue starts... */
14465 DECL_STRUCT_FUNCTION (decl)->function_start_locus
14466 = func_brace_location;
14467 }
14468
14469 return decl;
14470 }
14471 }
14472
14473 /* [dcl.dcl]
14474
14475 Only in function declarations for constructors, destructors, and
14476 type conversions can the decl-specifier-seq be omitted.
14477
14478 We explicitly postpone this check past the point where we handle
14479 function-definitions because we tolerate function-definitions
14480 that are missing their return types in some modes. */
14481 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14482 {
14483 cp_parser_error (parser,
14484 "expected constructor, destructor, or type conversion");
14485 return error_mark_node;
14486 }
14487
14488 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14489 if (token->type == CPP_EQ
14490 || token->type == CPP_OPEN_PAREN
14491 || token->type == CPP_OPEN_BRACE)
14492 {
14493 is_initialized = SD_INITIALIZED;
14494 initialization_kind = token->type;
14495
14496 if (token->type == CPP_EQ
14497 && function_declarator_p (declarator))
14498 {
14499 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14500 if (t2->keyword == RID_DEFAULT)
14501 is_initialized = SD_DEFAULTED;
14502 else if (t2->keyword == RID_DELETE)
14503 is_initialized = SD_DELETED;
14504 }
14505 }
14506 else
14507 {
14508 /* If the init-declarator isn't initialized and isn't followed by a
14509 `,' or `;', it's not a valid init-declarator. */
14510 if (token->type != CPP_COMMA
14511 && token->type != CPP_SEMICOLON)
14512 {
14513 cp_parser_error (parser, "expected initializer");
14514 return error_mark_node;
14515 }
14516 is_initialized = SD_UNINITIALIZED;
14517 initialization_kind = CPP_EOF;
14518 }
14519
14520 /* Because start_decl has side-effects, we should only call it if we
14521 know we're going ahead. By this point, we know that we cannot
14522 possibly be looking at any other construct. */
14523 cp_parser_commit_to_tentative_parse (parser);
14524
14525 /* If the decl specifiers were bad, issue an error now that we're
14526 sure this was intended to be a declarator. Then continue
14527 declaring the variable(s), as int, to try to cut down on further
14528 errors. */
14529 if (decl_specifiers->any_specifiers_p
14530 && decl_specifiers->type == error_mark_node)
14531 {
14532 cp_parser_error (parser, "invalid type in declaration");
14533 decl_specifiers->type = integer_type_node;
14534 }
14535
14536 /* Check to see whether or not this declaration is a friend. */
14537 friend_p = cp_parser_friend_p (decl_specifiers);
14538
14539 /* Enter the newly declared entry in the symbol table. If we're
14540 processing a declaration in a class-specifier, we wait until
14541 after processing the initializer. */
14542 if (!member_p)
14543 {
14544 if (parser->in_unbraced_linkage_specification_p)
14545 decl_specifiers->storage_class = sc_extern;
14546 decl = start_decl (declarator, decl_specifiers,
14547 is_initialized, attributes, prefix_attributes,
14548 &pushed_scope);
14549 /* Adjust location of decl if declarator->id_loc is more appropriate:
14550 set, and decl wasn't merged with another decl, in which case its
14551 location would be different from input_location, and more accurate. */
14552 if (DECL_P (decl)
14553 && declarator->id_loc != UNKNOWN_LOCATION
14554 && DECL_SOURCE_LOCATION (decl) == input_location)
14555 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14556 }
14557 else if (scope)
14558 /* Enter the SCOPE. That way unqualified names appearing in the
14559 initializer will be looked up in SCOPE. */
14560 pushed_scope = push_scope (scope);
14561
14562 /* Perform deferred access control checks, now that we know in which
14563 SCOPE the declared entity resides. */
14564 if (!member_p && decl)
14565 {
14566 tree saved_current_function_decl = NULL_TREE;
14567
14568 /* If the entity being declared is a function, pretend that we
14569 are in its scope. If it is a `friend', it may have access to
14570 things that would not otherwise be accessible. */
14571 if (TREE_CODE (decl) == FUNCTION_DECL)
14572 {
14573 saved_current_function_decl = current_function_decl;
14574 current_function_decl = decl;
14575 }
14576
14577 /* Perform access checks for template parameters. */
14578 cp_parser_perform_template_parameter_access_checks (checks);
14579
14580 /* Perform the access control checks for the declarator and the
14581 decl-specifiers. */
14582 perform_deferred_access_checks ();
14583
14584 /* Restore the saved value. */
14585 if (TREE_CODE (decl) == FUNCTION_DECL)
14586 current_function_decl = saved_current_function_decl;
14587 }
14588
14589 /* Parse the initializer. */
14590 initializer = NULL_TREE;
14591 is_direct_init = false;
14592 is_non_constant_init = true;
14593 if (is_initialized)
14594 {
14595 if (function_declarator_p (declarator))
14596 {
14597 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14598 if (initialization_kind == CPP_EQ)
14599 initializer = cp_parser_pure_specifier (parser);
14600 else
14601 {
14602 /* If the declaration was erroneous, we don't really
14603 know what the user intended, so just silently
14604 consume the initializer. */
14605 if (decl != error_mark_node)
14606 error_at (initializer_start_token->location,
14607 "initializer provided for function");
14608 cp_parser_skip_to_closing_parenthesis (parser,
14609 /*recovering=*/true,
14610 /*or_comma=*/false,
14611 /*consume_paren=*/true);
14612 }
14613 }
14614 else
14615 {
14616 /* We want to record the extra mangling scope for in-class
14617 initializers of class members and initializers of static data
14618 member templates. The former is a C++0x feature which isn't
14619 implemented yet, and I expect it will involve deferring
14620 parsing of the initializer until end of class as with default
14621 arguments. So right here we only handle the latter. */
14622 if (!member_p && processing_template_decl)
14623 start_lambda_scope (decl);
14624 initializer = cp_parser_initializer (parser,
14625 &is_direct_init,
14626 &is_non_constant_init);
14627 if (!member_p && processing_template_decl)
14628 finish_lambda_scope ();
14629 }
14630 }
14631
14632 /* The old parser allows attributes to appear after a parenthesized
14633 initializer. Mark Mitchell proposed removing this functionality
14634 on the GCC mailing lists on 2002-08-13. This parser accepts the
14635 attributes -- but ignores them. */
14636 if (cp_parser_allow_gnu_extensions_p (parser)
14637 && initialization_kind == CPP_OPEN_PAREN)
14638 if (cp_parser_attributes_opt (parser))
14639 warning (OPT_Wattributes,
14640 "attributes after parenthesized initializer ignored");
14641
14642 /* For an in-class declaration, use `grokfield' to create the
14643 declaration. */
14644 if (member_p)
14645 {
14646 if (pushed_scope)
14647 {
14648 pop_scope (pushed_scope);
14649 pushed_scope = false;
14650 }
14651 decl = grokfield (declarator, decl_specifiers,
14652 initializer, !is_non_constant_init,
14653 /*asmspec=*/NULL_TREE,
14654 prefix_attributes);
14655 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14656 cp_parser_save_default_args (parser, decl);
14657 }
14658
14659 /* Finish processing the declaration. But, skip friend
14660 declarations. */
14661 if (!friend_p && decl && decl != error_mark_node)
14662 {
14663 cp_finish_decl (decl,
14664 initializer, !is_non_constant_init,
14665 asm_specification,
14666 /* If the initializer is in parentheses, then this is
14667 a direct-initialization, which means that an
14668 `explicit' constructor is OK. Otherwise, an
14669 `explicit' constructor cannot be used. */
14670 ((is_direct_init || !is_initialized)
14671 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14672 }
14673 else if ((cxx_dialect != cxx98) && friend_p
14674 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14675 /* Core issue #226 (C++0x only): A default template-argument
14676 shall not be specified in a friend class template
14677 declaration. */
14678 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14679 /*is_partial=*/0, /*is_friend_decl=*/1);
14680
14681 if (!friend_p && pushed_scope)
14682 pop_scope (pushed_scope);
14683
14684 return decl;
14685 }
14686
14687 /* Parse a declarator.
14688
14689 declarator:
14690 direct-declarator
14691 ptr-operator declarator
14692
14693 abstract-declarator:
14694 ptr-operator abstract-declarator [opt]
14695 direct-abstract-declarator
14696
14697 GNU Extensions:
14698
14699 declarator:
14700 attributes [opt] direct-declarator
14701 attributes [opt] ptr-operator declarator
14702
14703 abstract-declarator:
14704 attributes [opt] ptr-operator abstract-declarator [opt]
14705 attributes [opt] direct-abstract-declarator
14706
14707 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14708 detect constructor, destructor or conversion operators. It is set
14709 to -1 if the declarator is a name, and +1 if it is a
14710 function. Otherwise it is set to zero. Usually you just want to
14711 test for >0, but internally the negative value is used.
14712
14713 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14714 a decl-specifier-seq unless it declares a constructor, destructor,
14715 or conversion. It might seem that we could check this condition in
14716 semantic analysis, rather than parsing, but that makes it difficult
14717 to handle something like `f()'. We want to notice that there are
14718 no decl-specifiers, and therefore realize that this is an
14719 expression, not a declaration.)
14720
14721 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14722 the declarator is a direct-declarator of the form "(...)".
14723
14724 MEMBER_P is true iff this declarator is a member-declarator. */
14725
14726 static cp_declarator *
14727 cp_parser_declarator (cp_parser* parser,
14728 cp_parser_declarator_kind dcl_kind,
14729 int* ctor_dtor_or_conv_p,
14730 bool* parenthesized_p,
14731 bool member_p)
14732 {
14733 cp_declarator *declarator;
14734 enum tree_code code;
14735 cp_cv_quals cv_quals;
14736 tree class_type;
14737 tree attributes = NULL_TREE;
14738
14739 /* Assume this is not a constructor, destructor, or type-conversion
14740 operator. */
14741 if (ctor_dtor_or_conv_p)
14742 *ctor_dtor_or_conv_p = 0;
14743
14744 if (cp_parser_allow_gnu_extensions_p (parser))
14745 attributes = cp_parser_attributes_opt (parser);
14746
14747 /* Check for the ptr-operator production. */
14748 cp_parser_parse_tentatively (parser);
14749 /* Parse the ptr-operator. */
14750 code = cp_parser_ptr_operator (parser,
14751 &class_type,
14752 &cv_quals);
14753 /* If that worked, then we have a ptr-operator. */
14754 if (cp_parser_parse_definitely (parser))
14755 {
14756 /* If a ptr-operator was found, then this declarator was not
14757 parenthesized. */
14758 if (parenthesized_p)
14759 *parenthesized_p = true;
14760 /* The dependent declarator is optional if we are parsing an
14761 abstract-declarator. */
14762 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14763 cp_parser_parse_tentatively (parser);
14764
14765 /* Parse the dependent declarator. */
14766 declarator = cp_parser_declarator (parser, dcl_kind,
14767 /*ctor_dtor_or_conv_p=*/NULL,
14768 /*parenthesized_p=*/NULL,
14769 /*member_p=*/false);
14770
14771 /* If we are parsing an abstract-declarator, we must handle the
14772 case where the dependent declarator is absent. */
14773 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14774 && !cp_parser_parse_definitely (parser))
14775 declarator = NULL;
14776
14777 declarator = cp_parser_make_indirect_declarator
14778 (code, class_type, cv_quals, declarator);
14779 }
14780 /* Everything else is a direct-declarator. */
14781 else
14782 {
14783 if (parenthesized_p)
14784 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14785 CPP_OPEN_PAREN);
14786 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14787 ctor_dtor_or_conv_p,
14788 member_p);
14789 }
14790
14791 if (attributes && declarator && declarator != cp_error_declarator)
14792 declarator->attributes = attributes;
14793
14794 return declarator;
14795 }
14796
14797 /* Parse a direct-declarator or direct-abstract-declarator.
14798
14799 direct-declarator:
14800 declarator-id
14801 direct-declarator ( parameter-declaration-clause )
14802 cv-qualifier-seq [opt]
14803 exception-specification [opt]
14804 direct-declarator [ constant-expression [opt] ]
14805 ( declarator )
14806
14807 direct-abstract-declarator:
14808 direct-abstract-declarator [opt]
14809 ( parameter-declaration-clause )
14810 cv-qualifier-seq [opt]
14811 exception-specification [opt]
14812 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14813 ( abstract-declarator )
14814
14815 Returns a representation of the declarator. DCL_KIND is
14816 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14817 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14818 we are parsing a direct-declarator. It is
14819 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14820 of ambiguity we prefer an abstract declarator, as per
14821 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14822 cp_parser_declarator. */
14823
14824 static cp_declarator *
14825 cp_parser_direct_declarator (cp_parser* parser,
14826 cp_parser_declarator_kind dcl_kind,
14827 int* ctor_dtor_or_conv_p,
14828 bool member_p)
14829 {
14830 cp_token *token;
14831 cp_declarator *declarator = NULL;
14832 tree scope = NULL_TREE;
14833 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14834 bool saved_in_declarator_p = parser->in_declarator_p;
14835 bool first = true;
14836 tree pushed_scope = NULL_TREE;
14837
14838 while (true)
14839 {
14840 /* Peek at the next token. */
14841 token = cp_lexer_peek_token (parser->lexer);
14842 if (token->type == CPP_OPEN_PAREN)
14843 {
14844 /* This is either a parameter-declaration-clause, or a
14845 parenthesized declarator. When we know we are parsing a
14846 named declarator, it must be a parenthesized declarator
14847 if FIRST is true. For instance, `(int)' is a
14848 parameter-declaration-clause, with an omitted
14849 direct-abstract-declarator. But `((*))', is a
14850 parenthesized abstract declarator. Finally, when T is a
14851 template parameter `(T)' is a
14852 parameter-declaration-clause, and not a parenthesized
14853 named declarator.
14854
14855 We first try and parse a parameter-declaration-clause,
14856 and then try a nested declarator (if FIRST is true).
14857
14858 It is not an error for it not to be a
14859 parameter-declaration-clause, even when FIRST is
14860 false. Consider,
14861
14862 int i (int);
14863 int i (3);
14864
14865 The first is the declaration of a function while the
14866 second is the definition of a variable, including its
14867 initializer.
14868
14869 Having seen only the parenthesis, we cannot know which of
14870 these two alternatives should be selected. Even more
14871 complex are examples like:
14872
14873 int i (int (a));
14874 int i (int (3));
14875
14876 The former is a function-declaration; the latter is a
14877 variable initialization.
14878
14879 Thus again, we try a parameter-declaration-clause, and if
14880 that fails, we back out and return. */
14881
14882 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14883 {
14884 tree params;
14885 unsigned saved_num_template_parameter_lists;
14886 bool is_declarator = false;
14887 tree t;
14888
14889 /* In a member-declarator, the only valid interpretation
14890 of a parenthesis is the start of a
14891 parameter-declaration-clause. (It is invalid to
14892 initialize a static data member with a parenthesized
14893 initializer; only the "=" form of initialization is
14894 permitted.) */
14895 if (!member_p)
14896 cp_parser_parse_tentatively (parser);
14897
14898 /* Consume the `('. */
14899 cp_lexer_consume_token (parser->lexer);
14900 if (first)
14901 {
14902 /* If this is going to be an abstract declarator, we're
14903 in a declarator and we can't have default args. */
14904 parser->default_arg_ok_p = false;
14905 parser->in_declarator_p = true;
14906 }
14907
14908 /* Inside the function parameter list, surrounding
14909 template-parameter-lists do not apply. */
14910 saved_num_template_parameter_lists
14911 = parser->num_template_parameter_lists;
14912 parser->num_template_parameter_lists = 0;
14913
14914 begin_scope (sk_function_parms, NULL_TREE);
14915
14916 /* Parse the parameter-declaration-clause. */
14917 params = cp_parser_parameter_declaration_clause (parser);
14918
14919 parser->num_template_parameter_lists
14920 = saved_num_template_parameter_lists;
14921
14922 /* If all went well, parse the cv-qualifier-seq and the
14923 exception-specification. */
14924 if (member_p || cp_parser_parse_definitely (parser))
14925 {
14926 cp_cv_quals cv_quals;
14927 tree exception_specification;
14928 tree late_return;
14929
14930 is_declarator = true;
14931
14932 if (ctor_dtor_or_conv_p)
14933 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14934 first = false;
14935 /* Consume the `)'. */
14936 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14937
14938 /* Parse the cv-qualifier-seq. */
14939 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14940 /* And the exception-specification. */
14941 exception_specification
14942 = cp_parser_exception_specification_opt (parser);
14943
14944 late_return
14945 = cp_parser_late_return_type_opt (parser);
14946
14947 /* Create the function-declarator. */
14948 declarator = make_call_declarator (declarator,
14949 params,
14950 cv_quals,
14951 exception_specification,
14952 late_return);
14953 /* Any subsequent parameter lists are to do with
14954 return type, so are not those of the declared
14955 function. */
14956 parser->default_arg_ok_p = false;
14957 }
14958
14959 /* Remove the function parms from scope. */
14960 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14961 pop_binding (DECL_NAME (t), t);
14962 leave_scope();
14963
14964 if (is_declarator)
14965 /* Repeat the main loop. */
14966 continue;
14967 }
14968
14969 /* If this is the first, we can try a parenthesized
14970 declarator. */
14971 if (first)
14972 {
14973 bool saved_in_type_id_in_expr_p;
14974
14975 parser->default_arg_ok_p = saved_default_arg_ok_p;
14976 parser->in_declarator_p = saved_in_declarator_p;
14977
14978 /* Consume the `('. */
14979 cp_lexer_consume_token (parser->lexer);
14980 /* Parse the nested declarator. */
14981 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14982 parser->in_type_id_in_expr_p = true;
14983 declarator
14984 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14985 /*parenthesized_p=*/NULL,
14986 member_p);
14987 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14988 first = false;
14989 /* Expect a `)'. */
14990 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14991 declarator = cp_error_declarator;
14992 if (declarator == cp_error_declarator)
14993 break;
14994
14995 goto handle_declarator;
14996 }
14997 /* Otherwise, we must be done. */
14998 else
14999 break;
15000 }
15001 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15002 && token->type == CPP_OPEN_SQUARE)
15003 {
15004 /* Parse an array-declarator. */
15005 tree bounds;
15006
15007 if (ctor_dtor_or_conv_p)
15008 *ctor_dtor_or_conv_p = 0;
15009
15010 first = false;
15011 parser->default_arg_ok_p = false;
15012 parser->in_declarator_p = true;
15013 /* Consume the `['. */
15014 cp_lexer_consume_token (parser->lexer);
15015 /* Peek at the next token. */
15016 token = cp_lexer_peek_token (parser->lexer);
15017 /* If the next token is `]', then there is no
15018 constant-expression. */
15019 if (token->type != CPP_CLOSE_SQUARE)
15020 {
15021 bool non_constant_p;
15022
15023 bounds
15024 = cp_parser_constant_expression (parser,
15025 /*allow_non_constant=*/true,
15026 &non_constant_p);
15027 if (!non_constant_p || cxx_dialect >= cxx0x)
15028 /* OK */;
15029 /* Normally, the array bound must be an integral constant
15030 expression. However, as an extension, we allow VLAs
15031 in function scopes as long as they aren't part of a
15032 parameter declaration. */
15033 else if (!parser->in_function_body
15034 || current_binding_level->kind == sk_function_parms)
15035 {
15036 cp_parser_error (parser,
15037 "array bound is not an integer constant");
15038 bounds = error_mark_node;
15039 }
15040 else if (processing_template_decl && !error_operand_p (bounds))
15041 {
15042 /* Remember this wasn't a constant-expression. */
15043 bounds = build_nop (TREE_TYPE (bounds), bounds);
15044 TREE_SIDE_EFFECTS (bounds) = 1;
15045 }
15046 }
15047 else
15048 bounds = NULL_TREE;
15049 /* Look for the closing `]'. */
15050 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15051 {
15052 declarator = cp_error_declarator;
15053 break;
15054 }
15055
15056 declarator = make_array_declarator (declarator, bounds);
15057 }
15058 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15059 {
15060 {
15061 tree qualifying_scope;
15062 tree unqualified_name;
15063 special_function_kind sfk;
15064 bool abstract_ok;
15065 bool pack_expansion_p = false;
15066 cp_token *declarator_id_start_token;
15067
15068 /* Parse a declarator-id */
15069 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15070 if (abstract_ok)
15071 {
15072 cp_parser_parse_tentatively (parser);
15073
15074 /* If we see an ellipsis, we should be looking at a
15075 parameter pack. */
15076 if (token->type == CPP_ELLIPSIS)
15077 {
15078 /* Consume the `...' */
15079 cp_lexer_consume_token (parser->lexer);
15080
15081 pack_expansion_p = true;
15082 }
15083 }
15084
15085 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15086 unqualified_name
15087 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15088 qualifying_scope = parser->scope;
15089 if (abstract_ok)
15090 {
15091 bool okay = false;
15092
15093 if (!unqualified_name && pack_expansion_p)
15094 {
15095 /* Check whether an error occurred. */
15096 okay = !cp_parser_error_occurred (parser);
15097
15098 /* We already consumed the ellipsis to mark a
15099 parameter pack, but we have no way to report it,
15100 so abort the tentative parse. We will be exiting
15101 immediately anyway. */
15102 cp_parser_abort_tentative_parse (parser);
15103 }
15104 else
15105 okay = cp_parser_parse_definitely (parser);
15106
15107 if (!okay)
15108 unqualified_name = error_mark_node;
15109 else if (unqualified_name
15110 && (qualifying_scope
15111 || (TREE_CODE (unqualified_name)
15112 != IDENTIFIER_NODE)))
15113 {
15114 cp_parser_error (parser, "expected unqualified-id");
15115 unqualified_name = error_mark_node;
15116 }
15117 }
15118
15119 if (!unqualified_name)
15120 return NULL;
15121 if (unqualified_name == error_mark_node)
15122 {
15123 declarator = cp_error_declarator;
15124 pack_expansion_p = false;
15125 declarator->parameter_pack_p = false;
15126 break;
15127 }
15128
15129 if (qualifying_scope && at_namespace_scope_p ()
15130 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15131 {
15132 /* In the declaration of a member of a template class
15133 outside of the class itself, the SCOPE will sometimes
15134 be a TYPENAME_TYPE. For example, given:
15135
15136 template <typename T>
15137 int S<T>::R::i = 3;
15138
15139 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
15140 this context, we must resolve S<T>::R to an ordinary
15141 type, rather than a typename type.
15142
15143 The reason we normally avoid resolving TYPENAME_TYPEs
15144 is that a specialization of `S' might render
15145 `S<T>::R' not a type. However, if `S' is
15146 specialized, then this `i' will not be used, so there
15147 is no harm in resolving the types here. */
15148 tree type;
15149
15150 /* Resolve the TYPENAME_TYPE. */
15151 type = resolve_typename_type (qualifying_scope,
15152 /*only_current_p=*/false);
15153 /* If that failed, the declarator is invalid. */
15154 if (TREE_CODE (type) == TYPENAME_TYPE)
15155 {
15156 if (typedef_variant_p (type))
15157 error_at (declarator_id_start_token->location,
15158 "cannot define member of dependent typedef "
15159 "%qT", type);
15160 else
15161 error_at (declarator_id_start_token->location,
15162 "%<%T::%E%> is not a type",
15163 TYPE_CONTEXT (qualifying_scope),
15164 TYPE_IDENTIFIER (qualifying_scope));
15165 }
15166 qualifying_scope = type;
15167 }
15168
15169 sfk = sfk_none;
15170
15171 if (unqualified_name)
15172 {
15173 tree class_type;
15174
15175 if (qualifying_scope
15176 && CLASS_TYPE_P (qualifying_scope))
15177 class_type = qualifying_scope;
15178 else
15179 class_type = current_class_type;
15180
15181 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15182 {
15183 tree name_type = TREE_TYPE (unqualified_name);
15184 if (class_type && same_type_p (name_type, class_type))
15185 {
15186 if (qualifying_scope
15187 && CLASSTYPE_USE_TEMPLATE (name_type))
15188 {
15189 error_at (declarator_id_start_token->location,
15190 "invalid use of constructor as a template");
15191 inform (declarator_id_start_token->location,
15192 "use %<%T::%D%> instead of %<%T::%D%> to "
15193 "name the constructor in a qualified name",
15194 class_type,
15195 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15196 class_type, name_type);
15197 declarator = cp_error_declarator;
15198 break;
15199 }
15200 else
15201 unqualified_name = constructor_name (class_type);
15202 }
15203 else
15204 {
15205 /* We do not attempt to print the declarator
15206 here because we do not have enough
15207 information about its original syntactic
15208 form. */
15209 cp_parser_error (parser, "invalid declarator");
15210 declarator = cp_error_declarator;
15211 break;
15212 }
15213 }
15214
15215 if (class_type)
15216 {
15217 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15218 sfk = sfk_destructor;
15219 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15220 sfk = sfk_conversion;
15221 else if (/* There's no way to declare a constructor
15222 for an anonymous type, even if the type
15223 got a name for linkage purposes. */
15224 !TYPE_WAS_ANONYMOUS (class_type)
15225 && constructor_name_p (unqualified_name,
15226 class_type))
15227 {
15228 unqualified_name = constructor_name (class_type);
15229 sfk = sfk_constructor;
15230 }
15231 else if (is_overloaded_fn (unqualified_name)
15232 && DECL_CONSTRUCTOR_P (get_first_fn
15233 (unqualified_name)))
15234 sfk = sfk_constructor;
15235
15236 if (ctor_dtor_or_conv_p && sfk != sfk_none)
15237 *ctor_dtor_or_conv_p = -1;
15238 }
15239 }
15240 declarator = make_id_declarator (qualifying_scope,
15241 unqualified_name,
15242 sfk);
15243 declarator->id_loc = token->location;
15244 declarator->parameter_pack_p = pack_expansion_p;
15245
15246 if (pack_expansion_p)
15247 maybe_warn_variadic_templates ();
15248 }
15249
15250 handle_declarator:;
15251 scope = get_scope_of_declarator (declarator);
15252 if (scope)
15253 /* Any names that appear after the declarator-id for a
15254 member are looked up in the containing scope. */
15255 pushed_scope = push_scope (scope);
15256 parser->in_declarator_p = true;
15257 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15258 || (declarator && declarator->kind == cdk_id))
15259 /* Default args are only allowed on function
15260 declarations. */
15261 parser->default_arg_ok_p = saved_default_arg_ok_p;
15262 else
15263 parser->default_arg_ok_p = false;
15264
15265 first = false;
15266 }
15267 /* We're done. */
15268 else
15269 break;
15270 }
15271
15272 /* For an abstract declarator, we might wind up with nothing at this
15273 point. That's an error; the declarator is not optional. */
15274 if (!declarator)
15275 cp_parser_error (parser, "expected declarator");
15276
15277 /* If we entered a scope, we must exit it now. */
15278 if (pushed_scope)
15279 pop_scope (pushed_scope);
15280
15281 parser->default_arg_ok_p = saved_default_arg_ok_p;
15282 parser->in_declarator_p = saved_in_declarator_p;
15283
15284 return declarator;
15285 }
15286
15287 /* Parse a ptr-operator.
15288
15289 ptr-operator:
15290 * cv-qualifier-seq [opt]
15291 &
15292 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15293
15294 GNU Extension:
15295
15296 ptr-operator:
15297 & cv-qualifier-seq [opt]
15298
15299 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15300 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15301 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15302 filled in with the TYPE containing the member. *CV_QUALS is
15303 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15304 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15305 Note that the tree codes returned by this function have nothing
15306 to do with the types of trees that will be eventually be created
15307 to represent the pointer or reference type being parsed. They are
15308 just constants with suggestive names. */
15309 static enum tree_code
15310 cp_parser_ptr_operator (cp_parser* parser,
15311 tree* type,
15312 cp_cv_quals *cv_quals)
15313 {
15314 enum tree_code code = ERROR_MARK;
15315 cp_token *token;
15316
15317 /* Assume that it's not a pointer-to-member. */
15318 *type = NULL_TREE;
15319 /* And that there are no cv-qualifiers. */
15320 *cv_quals = TYPE_UNQUALIFIED;
15321
15322 /* Peek at the next token. */
15323 token = cp_lexer_peek_token (parser->lexer);
15324
15325 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15326 if (token->type == CPP_MULT)
15327 code = INDIRECT_REF;
15328 else if (token->type == CPP_AND)
15329 code = ADDR_EXPR;
15330 else if ((cxx_dialect != cxx98) &&
15331 token->type == CPP_AND_AND) /* C++0x only */
15332 code = NON_LVALUE_EXPR;
15333
15334 if (code != ERROR_MARK)
15335 {
15336 /* Consume the `*', `&' or `&&'. */
15337 cp_lexer_consume_token (parser->lexer);
15338
15339 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15340 `&', if we are allowing GNU extensions. (The only qualifier
15341 that can legally appear after `&' is `restrict', but that is
15342 enforced during semantic analysis. */
15343 if (code == INDIRECT_REF
15344 || cp_parser_allow_gnu_extensions_p (parser))
15345 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15346 }
15347 else
15348 {
15349 /* Try the pointer-to-member case. */
15350 cp_parser_parse_tentatively (parser);
15351 /* Look for the optional `::' operator. */
15352 cp_parser_global_scope_opt (parser,
15353 /*current_scope_valid_p=*/false);
15354 /* Look for the nested-name specifier. */
15355 token = cp_lexer_peek_token (parser->lexer);
15356 cp_parser_nested_name_specifier (parser,
15357 /*typename_keyword_p=*/false,
15358 /*check_dependency_p=*/true,
15359 /*type_p=*/false,
15360 /*is_declaration=*/false);
15361 /* If we found it, and the next token is a `*', then we are
15362 indeed looking at a pointer-to-member operator. */
15363 if (!cp_parser_error_occurred (parser)
15364 && cp_parser_require (parser, CPP_MULT, RT_MULT))
15365 {
15366 /* Indicate that the `*' operator was used. */
15367 code = INDIRECT_REF;
15368
15369 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15370 error_at (token->location, "%qD is a namespace", parser->scope);
15371 else
15372 {
15373 /* The type of which the member is a member is given by the
15374 current SCOPE. */
15375 *type = parser->scope;
15376 /* The next name will not be qualified. */
15377 parser->scope = NULL_TREE;
15378 parser->qualifying_scope = NULL_TREE;
15379 parser->object_scope = NULL_TREE;
15380 /* Look for the optional cv-qualifier-seq. */
15381 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15382 }
15383 }
15384 /* If that didn't work we don't have a ptr-operator. */
15385 if (!cp_parser_parse_definitely (parser))
15386 cp_parser_error (parser, "expected ptr-operator");
15387 }
15388
15389 return code;
15390 }
15391
15392 /* Parse an (optional) cv-qualifier-seq.
15393
15394 cv-qualifier-seq:
15395 cv-qualifier cv-qualifier-seq [opt]
15396
15397 cv-qualifier:
15398 const
15399 volatile
15400
15401 GNU Extension:
15402
15403 cv-qualifier:
15404 __restrict__
15405
15406 Returns a bitmask representing the cv-qualifiers. */
15407
15408 static cp_cv_quals
15409 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15410 {
15411 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15412
15413 while (true)
15414 {
15415 cp_token *token;
15416 cp_cv_quals cv_qualifier;
15417
15418 /* Peek at the next token. */
15419 token = cp_lexer_peek_token (parser->lexer);
15420 /* See if it's a cv-qualifier. */
15421 switch (token->keyword)
15422 {
15423 case RID_CONST:
15424 cv_qualifier = TYPE_QUAL_CONST;
15425 break;
15426
15427 case RID_VOLATILE:
15428 cv_qualifier = TYPE_QUAL_VOLATILE;
15429 break;
15430
15431 case RID_RESTRICT:
15432 cv_qualifier = TYPE_QUAL_RESTRICT;
15433 break;
15434
15435 default:
15436 cv_qualifier = TYPE_UNQUALIFIED;
15437 break;
15438 }
15439
15440 if (!cv_qualifier)
15441 break;
15442
15443 if (cv_quals & cv_qualifier)
15444 {
15445 error_at (token->location, "duplicate cv-qualifier");
15446 cp_lexer_purge_token (parser->lexer);
15447 }
15448 else
15449 {
15450 cp_lexer_consume_token (parser->lexer);
15451 cv_quals |= cv_qualifier;
15452 }
15453 }
15454
15455 return cv_quals;
15456 }
15457
15458 /* Parse a late-specified return type, if any. This is not a separate
15459 non-terminal, but part of a function declarator, which looks like
15460
15461 -> trailing-type-specifier-seq abstract-declarator(opt)
15462
15463 Returns the type indicated by the type-id. */
15464
15465 static tree
15466 cp_parser_late_return_type_opt (cp_parser* parser)
15467 {
15468 cp_token *token;
15469
15470 /* Peek at the next token. */
15471 token = cp_lexer_peek_token (parser->lexer);
15472 /* A late-specified return type is indicated by an initial '->'. */
15473 if (token->type != CPP_DEREF)
15474 return NULL_TREE;
15475
15476 /* Consume the ->. */
15477 cp_lexer_consume_token (parser->lexer);
15478
15479 return cp_parser_trailing_type_id (parser);
15480 }
15481
15482 /* Parse a declarator-id.
15483
15484 declarator-id:
15485 id-expression
15486 :: [opt] nested-name-specifier [opt] type-name
15487
15488 In the `id-expression' case, the value returned is as for
15489 cp_parser_id_expression if the id-expression was an unqualified-id.
15490 If the id-expression was a qualified-id, then a SCOPE_REF is
15491 returned. The first operand is the scope (either a NAMESPACE_DECL
15492 or TREE_TYPE), but the second is still just a representation of an
15493 unqualified-id. */
15494
15495 static tree
15496 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15497 {
15498 tree id;
15499 /* The expression must be an id-expression. Assume that qualified
15500 names are the names of types so that:
15501
15502 template <class T>
15503 int S<T>::R::i = 3;
15504
15505 will work; we must treat `S<T>::R' as the name of a type.
15506 Similarly, assume that qualified names are templates, where
15507 required, so that:
15508
15509 template <class T>
15510 int S<T>::R<T>::i = 3;
15511
15512 will work, too. */
15513 id = cp_parser_id_expression (parser,
15514 /*template_keyword_p=*/false,
15515 /*check_dependency_p=*/false,
15516 /*template_p=*/NULL,
15517 /*declarator_p=*/true,
15518 optional_p);
15519 if (id && BASELINK_P (id))
15520 id = BASELINK_FUNCTIONS (id);
15521 return id;
15522 }
15523
15524 /* Parse a type-id.
15525
15526 type-id:
15527 type-specifier-seq abstract-declarator [opt]
15528
15529 Returns the TYPE specified. */
15530
15531 static tree
15532 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15533 bool is_trailing_return)
15534 {
15535 cp_decl_specifier_seq type_specifier_seq;
15536 cp_declarator *abstract_declarator;
15537
15538 /* Parse the type-specifier-seq. */
15539 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15540 is_trailing_return,
15541 &type_specifier_seq);
15542 if (type_specifier_seq.type == error_mark_node)
15543 return error_mark_node;
15544
15545 /* There might or might not be an abstract declarator. */
15546 cp_parser_parse_tentatively (parser);
15547 /* Look for the declarator. */
15548 abstract_declarator
15549 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15550 /*parenthesized_p=*/NULL,
15551 /*member_p=*/false);
15552 /* Check to see if there really was a declarator. */
15553 if (!cp_parser_parse_definitely (parser))
15554 abstract_declarator = NULL;
15555
15556 if (type_specifier_seq.type
15557 && type_uses_auto (type_specifier_seq.type))
15558 {
15559 /* A type-id with type 'auto' is only ok if the abstract declarator
15560 is a function declarator with a late-specified return type. */
15561 if (abstract_declarator
15562 && abstract_declarator->kind == cdk_function
15563 && abstract_declarator->u.function.late_return_type)
15564 /* OK */;
15565 else
15566 {
15567 error ("invalid use of %<auto%>");
15568 return error_mark_node;
15569 }
15570 }
15571
15572 return groktypename (&type_specifier_seq, abstract_declarator,
15573 is_template_arg);
15574 }
15575
15576 static tree cp_parser_type_id (cp_parser *parser)
15577 {
15578 return cp_parser_type_id_1 (parser, false, false);
15579 }
15580
15581 static tree cp_parser_template_type_arg (cp_parser *parser)
15582 {
15583 return cp_parser_type_id_1 (parser, true, false);
15584 }
15585
15586 static tree cp_parser_trailing_type_id (cp_parser *parser)
15587 {
15588 return cp_parser_type_id_1 (parser, false, true);
15589 }
15590
15591 /* Parse a type-specifier-seq.
15592
15593 type-specifier-seq:
15594 type-specifier type-specifier-seq [opt]
15595
15596 GNU extension:
15597
15598 type-specifier-seq:
15599 attributes type-specifier-seq [opt]
15600
15601 If IS_DECLARATION is true, we are at the start of a "condition" or
15602 exception-declaration, so we might be followed by a declarator-id.
15603
15604 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15605 i.e. we've just seen "->".
15606
15607 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15608
15609 static void
15610 cp_parser_type_specifier_seq (cp_parser* parser,
15611 bool is_declaration,
15612 bool is_trailing_return,
15613 cp_decl_specifier_seq *type_specifier_seq)
15614 {
15615 bool seen_type_specifier = false;
15616 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15617 cp_token *start_token = NULL;
15618
15619 /* Clear the TYPE_SPECIFIER_SEQ. */
15620 clear_decl_specs (type_specifier_seq);
15621
15622 /* In the context of a trailing return type, enum E { } is an
15623 elaborated-type-specifier followed by a function-body, not an
15624 enum-specifier. */
15625 if (is_trailing_return)
15626 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15627
15628 /* Parse the type-specifiers and attributes. */
15629 while (true)
15630 {
15631 tree type_specifier;
15632 bool is_cv_qualifier;
15633
15634 /* Check for attributes first. */
15635 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15636 {
15637 type_specifier_seq->attributes =
15638 chainon (type_specifier_seq->attributes,
15639 cp_parser_attributes_opt (parser));
15640 continue;
15641 }
15642
15643 /* record the token of the beginning of the type specifier seq,
15644 for error reporting purposes*/
15645 if (!start_token)
15646 start_token = cp_lexer_peek_token (parser->lexer);
15647
15648 /* Look for the type-specifier. */
15649 type_specifier = cp_parser_type_specifier (parser,
15650 flags,
15651 type_specifier_seq,
15652 /*is_declaration=*/false,
15653 NULL,
15654 &is_cv_qualifier);
15655 if (!type_specifier)
15656 {
15657 /* If the first type-specifier could not be found, this is not a
15658 type-specifier-seq at all. */
15659 if (!seen_type_specifier)
15660 {
15661 cp_parser_error (parser, "expected type-specifier");
15662 type_specifier_seq->type = error_mark_node;
15663 return;
15664 }
15665 /* If subsequent type-specifiers could not be found, the
15666 type-specifier-seq is complete. */
15667 break;
15668 }
15669
15670 seen_type_specifier = true;
15671 /* The standard says that a condition can be:
15672
15673 type-specifier-seq declarator = assignment-expression
15674
15675 However, given:
15676
15677 struct S {};
15678 if (int S = ...)
15679
15680 we should treat the "S" as a declarator, not as a
15681 type-specifier. The standard doesn't say that explicitly for
15682 type-specifier-seq, but it does say that for
15683 decl-specifier-seq in an ordinary declaration. Perhaps it
15684 would be clearer just to allow a decl-specifier-seq here, and
15685 then add a semantic restriction that if any decl-specifiers
15686 that are not type-specifiers appear, the program is invalid. */
15687 if (is_declaration && !is_cv_qualifier)
15688 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15689 }
15690
15691 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15692 }
15693
15694 /* Parse a parameter-declaration-clause.
15695
15696 parameter-declaration-clause:
15697 parameter-declaration-list [opt] ... [opt]
15698 parameter-declaration-list , ...
15699
15700 Returns a representation for the parameter declarations. A return
15701 value of NULL indicates a parameter-declaration-clause consisting
15702 only of an ellipsis. */
15703
15704 static tree
15705 cp_parser_parameter_declaration_clause (cp_parser* parser)
15706 {
15707 tree parameters;
15708 cp_token *token;
15709 bool ellipsis_p;
15710 bool is_error;
15711
15712 /* Peek at the next token. */
15713 token = cp_lexer_peek_token (parser->lexer);
15714 /* Check for trivial parameter-declaration-clauses. */
15715 if (token->type == CPP_ELLIPSIS)
15716 {
15717 /* Consume the `...' token. */
15718 cp_lexer_consume_token (parser->lexer);
15719 return NULL_TREE;
15720 }
15721 else if (token->type == CPP_CLOSE_PAREN)
15722 /* There are no parameters. */
15723 {
15724 #ifndef NO_IMPLICIT_EXTERN_C
15725 if (in_system_header && current_class_type == NULL
15726 && current_lang_name == lang_name_c)
15727 return NULL_TREE;
15728 else
15729 #endif
15730 return void_list_node;
15731 }
15732 /* Check for `(void)', too, which is a special case. */
15733 else if (token->keyword == RID_VOID
15734 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15735 == CPP_CLOSE_PAREN))
15736 {
15737 /* Consume the `void' token. */
15738 cp_lexer_consume_token (parser->lexer);
15739 /* There are no parameters. */
15740 return void_list_node;
15741 }
15742
15743 /* Parse the parameter-declaration-list. */
15744 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15745 /* If a parse error occurred while parsing the
15746 parameter-declaration-list, then the entire
15747 parameter-declaration-clause is erroneous. */
15748 if (is_error)
15749 return NULL;
15750
15751 /* Peek at the next token. */
15752 token = cp_lexer_peek_token (parser->lexer);
15753 /* If it's a `,', the clause should terminate with an ellipsis. */
15754 if (token->type == CPP_COMMA)
15755 {
15756 /* Consume the `,'. */
15757 cp_lexer_consume_token (parser->lexer);
15758 /* Expect an ellipsis. */
15759 ellipsis_p
15760 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15761 }
15762 /* It might also be `...' if the optional trailing `,' was
15763 omitted. */
15764 else if (token->type == CPP_ELLIPSIS)
15765 {
15766 /* Consume the `...' token. */
15767 cp_lexer_consume_token (parser->lexer);
15768 /* And remember that we saw it. */
15769 ellipsis_p = true;
15770 }
15771 else
15772 ellipsis_p = false;
15773
15774 /* Finish the parameter list. */
15775 if (!ellipsis_p)
15776 parameters = chainon (parameters, void_list_node);
15777
15778 return parameters;
15779 }
15780
15781 /* Parse a parameter-declaration-list.
15782
15783 parameter-declaration-list:
15784 parameter-declaration
15785 parameter-declaration-list , parameter-declaration
15786
15787 Returns a representation of the parameter-declaration-list, as for
15788 cp_parser_parameter_declaration_clause. However, the
15789 `void_list_node' is never appended to the list. Upon return,
15790 *IS_ERROR will be true iff an error occurred. */
15791
15792 static tree
15793 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15794 {
15795 tree parameters = NULL_TREE;
15796 tree *tail = &parameters;
15797 bool saved_in_unbraced_linkage_specification_p;
15798 int index = 0;
15799
15800 /* Assume all will go well. */
15801 *is_error = false;
15802 /* The special considerations that apply to a function within an
15803 unbraced linkage specifications do not apply to the parameters
15804 to the function. */
15805 saved_in_unbraced_linkage_specification_p
15806 = parser->in_unbraced_linkage_specification_p;
15807 parser->in_unbraced_linkage_specification_p = false;
15808
15809 /* Look for more parameters. */
15810 while (true)
15811 {
15812 cp_parameter_declarator *parameter;
15813 tree decl = error_mark_node;
15814 bool parenthesized_p;
15815 /* Parse the parameter. */
15816 parameter
15817 = cp_parser_parameter_declaration (parser,
15818 /*template_parm_p=*/false,
15819 &parenthesized_p);
15820
15821 /* We don't know yet if the enclosing context is deprecated, so wait
15822 and warn in grokparms if appropriate. */
15823 deprecated_state = DEPRECATED_SUPPRESS;
15824
15825 if (parameter)
15826 decl = grokdeclarator (parameter->declarator,
15827 &parameter->decl_specifiers,
15828 PARM,
15829 parameter->default_argument != NULL_TREE,
15830 &parameter->decl_specifiers.attributes);
15831
15832 deprecated_state = DEPRECATED_NORMAL;
15833
15834 /* If a parse error occurred parsing the parameter declaration,
15835 then the entire parameter-declaration-list is erroneous. */
15836 if (decl == error_mark_node)
15837 {
15838 *is_error = true;
15839 parameters = error_mark_node;
15840 break;
15841 }
15842
15843 if (parameter->decl_specifiers.attributes)
15844 cplus_decl_attributes (&decl,
15845 parameter->decl_specifiers.attributes,
15846 0);
15847 if (DECL_NAME (decl))
15848 decl = pushdecl (decl);
15849
15850 if (decl != error_mark_node)
15851 {
15852 retrofit_lang_decl (decl);
15853 DECL_PARM_INDEX (decl) = ++index;
15854 }
15855
15856 /* Add the new parameter to the list. */
15857 *tail = build_tree_list (parameter->default_argument, decl);
15858 tail = &TREE_CHAIN (*tail);
15859
15860 /* Peek at the next token. */
15861 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15862 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15863 /* These are for Objective-C++ */
15864 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15865 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15866 /* The parameter-declaration-list is complete. */
15867 break;
15868 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15869 {
15870 cp_token *token;
15871
15872 /* Peek at the next token. */
15873 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15874 /* If it's an ellipsis, then the list is complete. */
15875 if (token->type == CPP_ELLIPSIS)
15876 break;
15877 /* Otherwise, there must be more parameters. Consume the
15878 `,'. */
15879 cp_lexer_consume_token (parser->lexer);
15880 /* When parsing something like:
15881
15882 int i(float f, double d)
15883
15884 we can tell after seeing the declaration for "f" that we
15885 are not looking at an initialization of a variable "i",
15886 but rather at the declaration of a function "i".
15887
15888 Due to the fact that the parsing of template arguments
15889 (as specified to a template-id) requires backtracking we
15890 cannot use this technique when inside a template argument
15891 list. */
15892 if (!parser->in_template_argument_list_p
15893 && !parser->in_type_id_in_expr_p
15894 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15895 /* However, a parameter-declaration of the form
15896 "foat(f)" (which is a valid declaration of a
15897 parameter "f") can also be interpreted as an
15898 expression (the conversion of "f" to "float"). */
15899 && !parenthesized_p)
15900 cp_parser_commit_to_tentative_parse (parser);
15901 }
15902 else
15903 {
15904 cp_parser_error (parser, "expected %<,%> or %<...%>");
15905 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15906 cp_parser_skip_to_closing_parenthesis (parser,
15907 /*recovering=*/true,
15908 /*or_comma=*/false,
15909 /*consume_paren=*/false);
15910 break;
15911 }
15912 }
15913
15914 parser->in_unbraced_linkage_specification_p
15915 = saved_in_unbraced_linkage_specification_p;
15916
15917 return parameters;
15918 }
15919
15920 /* Parse a parameter declaration.
15921
15922 parameter-declaration:
15923 decl-specifier-seq ... [opt] declarator
15924 decl-specifier-seq declarator = assignment-expression
15925 decl-specifier-seq ... [opt] abstract-declarator [opt]
15926 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15927
15928 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15929 declares a template parameter. (In that case, a non-nested `>'
15930 token encountered during the parsing of the assignment-expression
15931 is not interpreted as a greater-than operator.)
15932
15933 Returns a representation of the parameter, or NULL if an error
15934 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15935 true iff the declarator is of the form "(p)". */
15936
15937 static cp_parameter_declarator *
15938 cp_parser_parameter_declaration (cp_parser *parser,
15939 bool template_parm_p,
15940 bool *parenthesized_p)
15941 {
15942 int declares_class_or_enum;
15943 cp_decl_specifier_seq decl_specifiers;
15944 cp_declarator *declarator;
15945 tree default_argument;
15946 cp_token *token = NULL, *declarator_token_start = NULL;
15947 const char *saved_message;
15948
15949 /* In a template parameter, `>' is not an operator.
15950
15951 [temp.param]
15952
15953 When parsing a default template-argument for a non-type
15954 template-parameter, the first non-nested `>' is taken as the end
15955 of the template parameter-list rather than a greater-than
15956 operator. */
15957
15958 /* Type definitions may not appear in parameter types. */
15959 saved_message = parser->type_definition_forbidden_message;
15960 parser->type_definition_forbidden_message
15961 = G_("types may not be defined in parameter types");
15962
15963 /* Parse the declaration-specifiers. */
15964 cp_parser_decl_specifier_seq (parser,
15965 CP_PARSER_FLAGS_NONE,
15966 &decl_specifiers,
15967 &declares_class_or_enum);
15968
15969 /* Complain about missing 'typename' or other invalid type names. */
15970 if (!decl_specifiers.any_type_specifiers_p)
15971 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15972
15973 /* If an error occurred, there's no reason to attempt to parse the
15974 rest of the declaration. */
15975 if (cp_parser_error_occurred (parser))
15976 {
15977 parser->type_definition_forbidden_message = saved_message;
15978 return NULL;
15979 }
15980
15981 /* Peek at the next token. */
15982 token = cp_lexer_peek_token (parser->lexer);
15983
15984 /* If the next token is a `)', `,', `=', `>', or `...', then there
15985 is no declarator. However, when variadic templates are enabled,
15986 there may be a declarator following `...'. */
15987 if (token->type == CPP_CLOSE_PAREN
15988 || token->type == CPP_COMMA
15989 || token->type == CPP_EQ
15990 || token->type == CPP_GREATER)
15991 {
15992 declarator = NULL;
15993 if (parenthesized_p)
15994 *parenthesized_p = false;
15995 }
15996 /* Otherwise, there should be a declarator. */
15997 else
15998 {
15999 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16000 parser->default_arg_ok_p = false;
16001
16002 /* After seeing a decl-specifier-seq, if the next token is not a
16003 "(", there is no possibility that the code is a valid
16004 expression. Therefore, if parsing tentatively, we commit at
16005 this point. */
16006 if (!parser->in_template_argument_list_p
16007 /* In an expression context, having seen:
16008
16009 (int((char ...
16010
16011 we cannot be sure whether we are looking at a
16012 function-type (taking a "char" as a parameter) or a cast
16013 of some object of type "char" to "int". */
16014 && !parser->in_type_id_in_expr_p
16015 && cp_parser_uncommitted_to_tentative_parse_p (parser)
16016 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16017 cp_parser_commit_to_tentative_parse (parser);
16018 /* Parse the declarator. */
16019 declarator_token_start = token;
16020 declarator = cp_parser_declarator (parser,
16021 CP_PARSER_DECLARATOR_EITHER,
16022 /*ctor_dtor_or_conv_p=*/NULL,
16023 parenthesized_p,
16024 /*member_p=*/false);
16025 parser->default_arg_ok_p = saved_default_arg_ok_p;
16026 /* After the declarator, allow more attributes. */
16027 decl_specifiers.attributes
16028 = chainon (decl_specifiers.attributes,
16029 cp_parser_attributes_opt (parser));
16030 }
16031
16032 /* If the next token is an ellipsis, and we have not seen a
16033 declarator name, and the type of the declarator contains parameter
16034 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16035 a parameter pack expansion expression. Otherwise, leave the
16036 ellipsis for a C-style variadic function. */
16037 token = cp_lexer_peek_token (parser->lexer);
16038 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16039 {
16040 tree type = decl_specifiers.type;
16041
16042 if (type && DECL_P (type))
16043 type = TREE_TYPE (type);
16044
16045 if (type
16046 && TREE_CODE (type) != TYPE_PACK_EXPANSION
16047 && declarator_can_be_parameter_pack (declarator)
16048 && (!declarator || !declarator->parameter_pack_p)
16049 && uses_parameter_packs (type))
16050 {
16051 /* Consume the `...'. */
16052 cp_lexer_consume_token (parser->lexer);
16053 maybe_warn_variadic_templates ();
16054
16055 /* Build a pack expansion type */
16056 if (declarator)
16057 declarator->parameter_pack_p = true;
16058 else
16059 decl_specifiers.type = make_pack_expansion (type);
16060 }
16061 }
16062
16063 /* The restriction on defining new types applies only to the type
16064 of the parameter, not to the default argument. */
16065 parser->type_definition_forbidden_message = saved_message;
16066
16067 /* If the next token is `=', then process a default argument. */
16068 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16069 {
16070 /* Consume the `='. */
16071 cp_lexer_consume_token (parser->lexer);
16072
16073 /* If we are defining a class, then the tokens that make up the
16074 default argument must be saved and processed later. */
16075 if (!template_parm_p && at_class_scope_p ()
16076 && TYPE_BEING_DEFINED (current_class_type)
16077 && !LAMBDA_TYPE_P (current_class_type))
16078 {
16079 unsigned depth = 0;
16080 int maybe_template_id = 0;
16081 cp_token *first_token;
16082 cp_token *token;
16083
16084 /* Add tokens until we have processed the entire default
16085 argument. We add the range [first_token, token). */
16086 first_token = cp_lexer_peek_token (parser->lexer);
16087 while (true)
16088 {
16089 bool done = false;
16090
16091 /* Peek at the next token. */
16092 token = cp_lexer_peek_token (parser->lexer);
16093 /* What we do depends on what token we have. */
16094 switch (token->type)
16095 {
16096 /* In valid code, a default argument must be
16097 immediately followed by a `,' `)', or `...'. */
16098 case CPP_COMMA:
16099 if (depth == 0 && maybe_template_id)
16100 {
16101 /* If we've seen a '<', we might be in a
16102 template-argument-list. Until Core issue 325 is
16103 resolved, we don't know how this situation ought
16104 to be handled, so try to DTRT. We check whether
16105 what comes after the comma is a valid parameter
16106 declaration list. If it is, then the comma ends
16107 the default argument; otherwise the default
16108 argument continues. */
16109 bool error = false;
16110 tree t;
16111
16112 /* Set ITALP so cp_parser_parameter_declaration_list
16113 doesn't decide to commit to this parse. */
16114 bool saved_italp = parser->in_template_argument_list_p;
16115 parser->in_template_argument_list_p = true;
16116
16117 cp_parser_parse_tentatively (parser);
16118 cp_lexer_consume_token (parser->lexer);
16119 begin_scope (sk_function_parms, NULL_TREE);
16120 cp_parser_parameter_declaration_list (parser, &error);
16121 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16122 pop_binding (DECL_NAME (t), t);
16123 leave_scope ();
16124 if (!cp_parser_error_occurred (parser) && !error)
16125 done = true;
16126 cp_parser_abort_tentative_parse (parser);
16127
16128 parser->in_template_argument_list_p = saved_italp;
16129 break;
16130 }
16131 case CPP_CLOSE_PAREN:
16132 case CPP_ELLIPSIS:
16133 /* If we run into a non-nested `;', `}', or `]',
16134 then the code is invalid -- but the default
16135 argument is certainly over. */
16136 case CPP_SEMICOLON:
16137 case CPP_CLOSE_BRACE:
16138 case CPP_CLOSE_SQUARE:
16139 if (depth == 0)
16140 done = true;
16141 /* Update DEPTH, if necessary. */
16142 else if (token->type == CPP_CLOSE_PAREN
16143 || token->type == CPP_CLOSE_BRACE
16144 || token->type == CPP_CLOSE_SQUARE)
16145 --depth;
16146 break;
16147
16148 case CPP_OPEN_PAREN:
16149 case CPP_OPEN_SQUARE:
16150 case CPP_OPEN_BRACE:
16151 ++depth;
16152 break;
16153
16154 case CPP_LESS:
16155 if (depth == 0)
16156 /* This might be the comparison operator, or it might
16157 start a template argument list. */
16158 ++maybe_template_id;
16159 break;
16160
16161 case CPP_RSHIFT:
16162 if (cxx_dialect == cxx98)
16163 break;
16164 /* Fall through for C++0x, which treats the `>>'
16165 operator like two `>' tokens in certain
16166 cases. */
16167
16168 case CPP_GREATER:
16169 if (depth == 0)
16170 {
16171 /* This might be an operator, or it might close a
16172 template argument list. But if a previous '<'
16173 started a template argument list, this will have
16174 closed it, so we can't be in one anymore. */
16175 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16176 if (maybe_template_id < 0)
16177 maybe_template_id = 0;
16178 }
16179 break;
16180
16181 /* If we run out of tokens, issue an error message. */
16182 case CPP_EOF:
16183 case CPP_PRAGMA_EOL:
16184 error_at (token->location, "file ends in default argument");
16185 done = true;
16186 break;
16187
16188 case CPP_NAME:
16189 case CPP_SCOPE:
16190 /* In these cases, we should look for template-ids.
16191 For example, if the default argument is
16192 `X<int, double>()', we need to do name lookup to
16193 figure out whether or not `X' is a template; if
16194 so, the `,' does not end the default argument.
16195
16196 That is not yet done. */
16197 break;
16198
16199 default:
16200 break;
16201 }
16202
16203 /* If we've reached the end, stop. */
16204 if (done)
16205 break;
16206
16207 /* Add the token to the token block. */
16208 token = cp_lexer_consume_token (parser->lexer);
16209 }
16210
16211 /* Create a DEFAULT_ARG to represent the unparsed default
16212 argument. */
16213 default_argument = make_node (DEFAULT_ARG);
16214 DEFARG_TOKENS (default_argument)
16215 = cp_token_cache_new (first_token, token);
16216 DEFARG_INSTANTIATIONS (default_argument) = NULL;
16217 }
16218 /* Outside of a class definition, we can just parse the
16219 assignment-expression. */
16220 else
16221 {
16222 token = cp_lexer_peek_token (parser->lexer);
16223 default_argument
16224 = cp_parser_default_argument (parser, template_parm_p);
16225 }
16226
16227 if (!parser->default_arg_ok_p)
16228 {
16229 if (flag_permissive)
16230 warning (0, "deprecated use of default argument for parameter of non-function");
16231 else
16232 {
16233 error_at (token->location,
16234 "default arguments are only "
16235 "permitted for function parameters");
16236 default_argument = NULL_TREE;
16237 }
16238 }
16239 else if ((declarator && declarator->parameter_pack_p)
16240 || (decl_specifiers.type
16241 && PACK_EXPANSION_P (decl_specifiers.type)))
16242 {
16243 /* Find the name of the parameter pack. */
16244 cp_declarator *id_declarator = declarator;
16245 while (id_declarator && id_declarator->kind != cdk_id)
16246 id_declarator = id_declarator->declarator;
16247
16248 if (id_declarator && id_declarator->kind == cdk_id)
16249 error_at (declarator_token_start->location,
16250 template_parm_p
16251 ? "template parameter pack %qD"
16252 " cannot have a default argument"
16253 : "parameter pack %qD cannot have a default argument",
16254 id_declarator->u.id.unqualified_name);
16255 else
16256 error_at (declarator_token_start->location,
16257 template_parm_p
16258 ? "template parameter pack cannot have a default argument"
16259 : "parameter pack cannot have a default argument");
16260
16261 default_argument = NULL_TREE;
16262 }
16263 }
16264 else
16265 default_argument = NULL_TREE;
16266
16267 return make_parameter_declarator (&decl_specifiers,
16268 declarator,
16269 default_argument);
16270 }
16271
16272 /* Parse a default argument and return it.
16273
16274 TEMPLATE_PARM_P is true if this is a default argument for a
16275 non-type template parameter. */
16276 static tree
16277 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16278 {
16279 tree default_argument = NULL_TREE;
16280 bool saved_greater_than_is_operator_p;
16281 bool saved_local_variables_forbidden_p;
16282
16283 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16284 set correctly. */
16285 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16286 parser->greater_than_is_operator_p = !template_parm_p;
16287 /* Local variable names (and the `this' keyword) may not
16288 appear in a default argument. */
16289 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16290 parser->local_variables_forbidden_p = true;
16291 /* Parse the assignment-expression. */
16292 if (template_parm_p)
16293 push_deferring_access_checks (dk_no_deferred);
16294 default_argument
16295 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16296 if (template_parm_p)
16297 pop_deferring_access_checks ();
16298 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16299 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16300
16301 return default_argument;
16302 }
16303
16304 /* Parse a function-body.
16305
16306 function-body:
16307 compound_statement */
16308
16309 static void
16310 cp_parser_function_body (cp_parser *parser)
16311 {
16312 cp_parser_compound_statement (parser, NULL, false);
16313 }
16314
16315 /* Parse a ctor-initializer-opt followed by a function-body. Return
16316 true if a ctor-initializer was present. */
16317
16318 static bool
16319 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16320 {
16321 tree body, list;
16322 bool ctor_initializer_p;
16323 const bool check_body_p =
16324 DECL_CONSTRUCTOR_P (current_function_decl)
16325 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16326 tree last = NULL;
16327
16328 /* Begin the function body. */
16329 body = begin_function_body ();
16330 /* Parse the optional ctor-initializer. */
16331 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16332
16333 /* If we're parsing a constexpr constructor definition, we need
16334 to check that the constructor body is indeed empty. However,
16335 before we get to cp_parser_function_body lot of junk has been
16336 generated, so we can't just check that we have an empty block.
16337 Rather we take a snapshot of the outermost block, and check whether
16338 cp_parser_function_body changed its state. */
16339 if (check_body_p)
16340 {
16341 list = body;
16342 if (TREE_CODE (list) == BIND_EXPR)
16343 list = BIND_EXPR_BODY (list);
16344 if (TREE_CODE (list) == STATEMENT_LIST
16345 && STATEMENT_LIST_TAIL (list) != NULL)
16346 last = STATEMENT_LIST_TAIL (list)->stmt;
16347 }
16348 /* Parse the function-body. */
16349 cp_parser_function_body (parser);
16350 if (check_body_p)
16351 check_constexpr_ctor_body (last, list);
16352 /* Finish the function body. */
16353 finish_function_body (body);
16354
16355 return ctor_initializer_p;
16356 }
16357
16358 /* Parse an initializer.
16359
16360 initializer:
16361 = initializer-clause
16362 ( expression-list )
16363
16364 Returns an expression representing the initializer. If no
16365 initializer is present, NULL_TREE is returned.
16366
16367 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16368 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16369 set to TRUE if there is no initializer present. If there is an
16370 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16371 is set to true; otherwise it is set to false. */
16372
16373 static tree
16374 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16375 bool* non_constant_p)
16376 {
16377 cp_token *token;
16378 tree init;
16379
16380 /* Peek at the next token. */
16381 token = cp_lexer_peek_token (parser->lexer);
16382
16383 /* Let our caller know whether or not this initializer was
16384 parenthesized. */
16385 *is_direct_init = (token->type != CPP_EQ);
16386 /* Assume that the initializer is constant. */
16387 *non_constant_p = false;
16388
16389 if (token->type == CPP_EQ)
16390 {
16391 /* Consume the `='. */
16392 cp_lexer_consume_token (parser->lexer);
16393 /* Parse the initializer-clause. */
16394 init = cp_parser_initializer_clause (parser, non_constant_p);
16395 }
16396 else if (token->type == CPP_OPEN_PAREN)
16397 {
16398 VEC(tree,gc) *vec;
16399 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16400 /*cast_p=*/false,
16401 /*allow_expansion_p=*/true,
16402 non_constant_p);
16403 if (vec == NULL)
16404 return error_mark_node;
16405 init = build_tree_list_vec (vec);
16406 release_tree_vector (vec);
16407 }
16408 else if (token->type == CPP_OPEN_BRACE)
16409 {
16410 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16411 init = cp_parser_braced_list (parser, non_constant_p);
16412 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16413 }
16414 else
16415 {
16416 /* Anything else is an error. */
16417 cp_parser_error (parser, "expected initializer");
16418 init = error_mark_node;
16419 }
16420
16421 return init;
16422 }
16423
16424 /* Parse an initializer-clause.
16425
16426 initializer-clause:
16427 assignment-expression
16428 braced-init-list
16429
16430 Returns an expression representing the initializer.
16431
16432 If the `assignment-expression' production is used the value
16433 returned is simply a representation for the expression.
16434
16435 Otherwise, calls cp_parser_braced_list. */
16436
16437 static tree
16438 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16439 {
16440 tree initializer;
16441
16442 /* Assume the expression is constant. */
16443 *non_constant_p = false;
16444
16445 /* If it is not a `{', then we are looking at an
16446 assignment-expression. */
16447 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16448 {
16449 initializer
16450 = cp_parser_constant_expression (parser,
16451 /*allow_non_constant_p=*/true,
16452 non_constant_p);
16453 if (!*non_constant_p)
16454 {
16455 /* We only want to fold if this is really a constant
16456 expression. FIXME Actually, we don't want to fold here, but in
16457 cp_finish_decl. */
16458 tree folded = fold_non_dependent_expr (initializer);
16459 folded = maybe_constant_value (folded);
16460 if (TREE_CONSTANT (folded))
16461 initializer = folded;
16462 }
16463 }
16464 else
16465 initializer = cp_parser_braced_list (parser, non_constant_p);
16466
16467 return initializer;
16468 }
16469
16470 /* Parse a brace-enclosed initializer list.
16471
16472 braced-init-list:
16473 { initializer-list , [opt] }
16474 { }
16475
16476 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16477 the elements of the initializer-list (or NULL, if the last
16478 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16479 NULL_TREE. There is no way to detect whether or not the optional
16480 trailing `,' was provided. NON_CONSTANT_P is as for
16481 cp_parser_initializer. */
16482
16483 static tree
16484 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16485 {
16486 tree initializer;
16487
16488 /* Consume the `{' token. */
16489 cp_lexer_consume_token (parser->lexer);
16490 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16491 initializer = make_node (CONSTRUCTOR);
16492 /* If it's not a `}', then there is a non-trivial initializer. */
16493 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16494 {
16495 /* Parse the initializer list. */
16496 CONSTRUCTOR_ELTS (initializer)
16497 = cp_parser_initializer_list (parser, non_constant_p);
16498 /* A trailing `,' token is allowed. */
16499 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16500 cp_lexer_consume_token (parser->lexer);
16501 }
16502 /* Now, there should be a trailing `}'. */
16503 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16504 TREE_TYPE (initializer) = init_list_type_node;
16505 return initializer;
16506 }
16507
16508 /* Parse an initializer-list.
16509
16510 initializer-list:
16511 initializer-clause ... [opt]
16512 initializer-list , initializer-clause ... [opt]
16513
16514 GNU Extension:
16515
16516 initializer-list:
16517 identifier : initializer-clause
16518 initializer-list, identifier : initializer-clause
16519
16520 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16521 for the initializer. If the INDEX of the elt is non-NULL, it is the
16522 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16523 as for cp_parser_initializer. */
16524
16525 static VEC(constructor_elt,gc) *
16526 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16527 {
16528 VEC(constructor_elt,gc) *v = NULL;
16529
16530 /* Assume all of the expressions are constant. */
16531 *non_constant_p = false;
16532
16533 /* Parse the rest of the list. */
16534 while (true)
16535 {
16536 cp_token *token;
16537 tree identifier;
16538 tree initializer;
16539 bool clause_non_constant_p;
16540
16541 /* If the next token is an identifier and the following one is a
16542 colon, we are looking at the GNU designated-initializer
16543 syntax. */
16544 if (cp_parser_allow_gnu_extensions_p (parser)
16545 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16546 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16547 {
16548 /* Warn the user that they are using an extension. */
16549 pedwarn (input_location, OPT_pedantic,
16550 "ISO C++ does not allow designated initializers");
16551 /* Consume the identifier. */
16552 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16553 /* Consume the `:'. */
16554 cp_lexer_consume_token (parser->lexer);
16555 }
16556 else
16557 identifier = NULL_TREE;
16558
16559 /* Parse the initializer. */
16560 initializer = cp_parser_initializer_clause (parser,
16561 &clause_non_constant_p);
16562 /* If any clause is non-constant, so is the entire initializer. */
16563 if (clause_non_constant_p)
16564 *non_constant_p = true;
16565
16566 /* If we have an ellipsis, this is an initializer pack
16567 expansion. */
16568 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16569 {
16570 /* Consume the `...'. */
16571 cp_lexer_consume_token (parser->lexer);
16572
16573 /* Turn the initializer into an initializer expansion. */
16574 initializer = make_pack_expansion (initializer);
16575 }
16576
16577 /* Add it to the vector. */
16578 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16579
16580 /* If the next token is not a comma, we have reached the end of
16581 the list. */
16582 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16583 break;
16584
16585 /* Peek at the next token. */
16586 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16587 /* If the next token is a `}', then we're still done. An
16588 initializer-clause can have a trailing `,' after the
16589 initializer-list and before the closing `}'. */
16590 if (token->type == CPP_CLOSE_BRACE)
16591 break;
16592
16593 /* Consume the `,' token. */
16594 cp_lexer_consume_token (parser->lexer);
16595 }
16596
16597 return v;
16598 }
16599
16600 /* Classes [gram.class] */
16601
16602 /* Parse a class-name.
16603
16604 class-name:
16605 identifier
16606 template-id
16607
16608 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16609 to indicate that names looked up in dependent types should be
16610 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16611 keyword has been used to indicate that the name that appears next
16612 is a template. TAG_TYPE indicates the explicit tag given before
16613 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16614 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16615 is the class being defined in a class-head.
16616
16617 Returns the TYPE_DECL representing the class. */
16618
16619 static tree
16620 cp_parser_class_name (cp_parser *parser,
16621 bool typename_keyword_p,
16622 bool template_keyword_p,
16623 enum tag_types tag_type,
16624 bool check_dependency_p,
16625 bool class_head_p,
16626 bool is_declaration)
16627 {
16628 tree decl;
16629 tree scope;
16630 bool typename_p;
16631 cp_token *token;
16632 tree identifier = NULL_TREE;
16633
16634 /* All class-names start with an identifier. */
16635 token = cp_lexer_peek_token (parser->lexer);
16636 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16637 {
16638 cp_parser_error (parser, "expected class-name");
16639 return error_mark_node;
16640 }
16641
16642 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16643 to a template-id, so we save it here. */
16644 scope = parser->scope;
16645 if (scope == error_mark_node)
16646 return error_mark_node;
16647
16648 /* Any name names a type if we're following the `typename' keyword
16649 in a qualified name where the enclosing scope is type-dependent. */
16650 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16651 && dependent_type_p (scope));
16652 /* Handle the common case (an identifier, but not a template-id)
16653 efficiently. */
16654 if (token->type == CPP_NAME
16655 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16656 {
16657 cp_token *identifier_token;
16658 bool ambiguous_p;
16659
16660 /* Look for the identifier. */
16661 identifier_token = cp_lexer_peek_token (parser->lexer);
16662 ambiguous_p = identifier_token->ambiguous_p;
16663 identifier = cp_parser_identifier (parser);
16664 /* If the next token isn't an identifier, we are certainly not
16665 looking at a class-name. */
16666 if (identifier == error_mark_node)
16667 decl = error_mark_node;
16668 /* If we know this is a type-name, there's no need to look it
16669 up. */
16670 else if (typename_p)
16671 decl = identifier;
16672 else
16673 {
16674 tree ambiguous_decls;
16675 /* If we already know that this lookup is ambiguous, then
16676 we've already issued an error message; there's no reason
16677 to check again. */
16678 if (ambiguous_p)
16679 {
16680 cp_parser_simulate_error (parser);
16681 return error_mark_node;
16682 }
16683 /* If the next token is a `::', then the name must be a type
16684 name.
16685
16686 [basic.lookup.qual]
16687
16688 During the lookup for a name preceding the :: scope
16689 resolution operator, object, function, and enumerator
16690 names are ignored. */
16691 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16692 tag_type = typename_type;
16693 /* Look up the name. */
16694 decl = cp_parser_lookup_name (parser, identifier,
16695 tag_type,
16696 /*is_template=*/false,
16697 /*is_namespace=*/false,
16698 check_dependency_p,
16699 &ambiguous_decls,
16700 identifier_token->location);
16701 if (ambiguous_decls)
16702 {
16703 if (cp_parser_parsing_tentatively (parser))
16704 cp_parser_simulate_error (parser);
16705 return error_mark_node;
16706 }
16707 }
16708 }
16709 else
16710 {
16711 /* Try a template-id. */
16712 decl = cp_parser_template_id (parser, template_keyword_p,
16713 check_dependency_p,
16714 is_declaration);
16715 if (decl == error_mark_node)
16716 return error_mark_node;
16717 }
16718
16719 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16720
16721 /* If this is a typename, create a TYPENAME_TYPE. */
16722 if (typename_p && decl != error_mark_node)
16723 {
16724 decl = make_typename_type (scope, decl, typename_type,
16725 /*complain=*/tf_error);
16726 if (decl != error_mark_node)
16727 decl = TYPE_NAME (decl);
16728 }
16729
16730 /* Check to see that it is really the name of a class. */
16731 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16732 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16733 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16734 /* Situations like this:
16735
16736 template <typename T> struct A {
16737 typename T::template X<int>::I i;
16738 };
16739
16740 are problematic. Is `T::template X<int>' a class-name? The
16741 standard does not seem to be definitive, but there is no other
16742 valid interpretation of the following `::'. Therefore, those
16743 names are considered class-names. */
16744 {
16745 decl = make_typename_type (scope, decl, tag_type, tf_error);
16746 if (decl != error_mark_node)
16747 decl = TYPE_NAME (decl);
16748 }
16749 else if (TREE_CODE (decl) != TYPE_DECL
16750 || TREE_TYPE (decl) == error_mark_node
16751 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16752 /* In Objective-C 2.0, a classname followed by '.' starts a
16753 dot-syntax expression, and it's not a type-name. */
16754 || (c_dialect_objc ()
16755 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
16756 && objc_is_class_name (decl)))
16757 decl = error_mark_node;
16758
16759 if (decl == error_mark_node)
16760 cp_parser_error (parser, "expected class-name");
16761 else if (identifier && !parser->scope)
16762 maybe_note_name_used_in_class (identifier, decl);
16763
16764 return decl;
16765 }
16766
16767 /* Parse a class-specifier.
16768
16769 class-specifier:
16770 class-head { member-specification [opt] }
16771
16772 Returns the TREE_TYPE representing the class. */
16773
16774 static tree
16775 cp_parser_class_specifier (cp_parser* parser)
16776 {
16777 tree type;
16778 tree attributes = NULL_TREE;
16779 bool nested_name_specifier_p;
16780 unsigned saved_num_template_parameter_lists;
16781 bool saved_in_function_body;
16782 bool saved_in_unbraced_linkage_specification_p;
16783 tree old_scope = NULL_TREE;
16784 tree scope = NULL_TREE;
16785 tree bases;
16786
16787 push_deferring_access_checks (dk_no_deferred);
16788
16789 /* Parse the class-head. */
16790 type = cp_parser_class_head (parser,
16791 &nested_name_specifier_p,
16792 &attributes,
16793 &bases);
16794 /* If the class-head was a semantic disaster, skip the entire body
16795 of the class. */
16796 if (!type)
16797 {
16798 cp_parser_skip_to_end_of_block_or_statement (parser);
16799 pop_deferring_access_checks ();
16800 return error_mark_node;
16801 }
16802
16803 /* Look for the `{'. */
16804 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16805 {
16806 pop_deferring_access_checks ();
16807 return error_mark_node;
16808 }
16809
16810 /* Process the base classes. If they're invalid, skip the
16811 entire class body. */
16812 if (!xref_basetypes (type, bases))
16813 {
16814 /* Consuming the closing brace yields better error messages
16815 later on. */
16816 if (cp_parser_skip_to_closing_brace (parser))
16817 cp_lexer_consume_token (parser->lexer);
16818 pop_deferring_access_checks ();
16819 return error_mark_node;
16820 }
16821
16822 /* Issue an error message if type-definitions are forbidden here. */
16823 cp_parser_check_type_definition (parser);
16824 /* Remember that we are defining one more class. */
16825 ++parser->num_classes_being_defined;
16826 /* Inside the class, surrounding template-parameter-lists do not
16827 apply. */
16828 saved_num_template_parameter_lists
16829 = parser->num_template_parameter_lists;
16830 parser->num_template_parameter_lists = 0;
16831 /* We are not in a function body. */
16832 saved_in_function_body = parser->in_function_body;
16833 parser->in_function_body = false;
16834 /* We are not immediately inside an extern "lang" block. */
16835 saved_in_unbraced_linkage_specification_p
16836 = parser->in_unbraced_linkage_specification_p;
16837 parser->in_unbraced_linkage_specification_p = false;
16838
16839 /* Start the class. */
16840 if (nested_name_specifier_p)
16841 {
16842 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16843 old_scope = push_inner_scope (scope);
16844 }
16845 type = begin_class_definition (type, attributes);
16846
16847 if (type == error_mark_node)
16848 /* If the type is erroneous, skip the entire body of the class. */
16849 cp_parser_skip_to_closing_brace (parser);
16850 else
16851 /* Parse the member-specification. */
16852 cp_parser_member_specification_opt (parser);
16853
16854 /* Look for the trailing `}'. */
16855 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16856 /* Look for trailing attributes to apply to this class. */
16857 if (cp_parser_allow_gnu_extensions_p (parser))
16858 attributes = cp_parser_attributes_opt (parser);
16859 if (type != error_mark_node)
16860 type = finish_struct (type, attributes);
16861 if (nested_name_specifier_p)
16862 pop_inner_scope (old_scope, scope);
16863 /* If this class is not itself within the scope of another class,
16864 then we need to parse the bodies of all of the queued function
16865 definitions. Note that the queued functions defined in a class
16866 are not always processed immediately following the
16867 class-specifier for that class. Consider:
16868
16869 struct A {
16870 struct B { void f() { sizeof (A); } };
16871 };
16872
16873 If `f' were processed before the processing of `A' were
16874 completed, there would be no way to compute the size of `A'.
16875 Note that the nesting we are interested in here is lexical --
16876 not the semantic nesting given by TYPE_CONTEXT. In particular,
16877 for:
16878
16879 struct A { struct B; };
16880 struct A::B { void f() { } };
16881
16882 there is no need to delay the parsing of `A::B::f'. */
16883 if (--parser->num_classes_being_defined == 0)
16884 {
16885 tree fn;
16886 tree class_type = NULL_TREE;
16887 tree pushed_scope = NULL_TREE;
16888 unsigned ix;
16889 cp_default_arg_entry *e;
16890
16891 /* In a first pass, parse default arguments to the functions.
16892 Then, in a second pass, parse the bodies of the functions.
16893 This two-phased approach handles cases like:
16894
16895 struct S {
16896 void f() { g(); }
16897 void g(int i = 3);
16898 };
16899
16900 */
16901 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
16902 ix, e)
16903 {
16904 fn = e->decl;
16905 /* If there are default arguments that have not yet been processed,
16906 take care of them now. */
16907 if (class_type != e->class_type)
16908 {
16909 if (pushed_scope)
16910 pop_scope (pushed_scope);
16911 class_type = e->class_type;
16912 pushed_scope = push_scope (class_type);
16913 }
16914 /* Make sure that any template parameters are in scope. */
16915 maybe_begin_member_template_processing (fn);
16916 /* Parse the default argument expressions. */
16917 cp_parser_late_parsing_default_args (parser, fn);
16918 /* Remove any template parameters from the symbol table. */
16919 maybe_end_member_template_processing ();
16920 }
16921 if (pushed_scope)
16922 pop_scope (pushed_scope);
16923 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
16924 /* Now parse the body of the functions. */
16925 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
16926 cp_parser_late_parsing_for_member (parser, fn);
16927 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
16928 }
16929
16930 /* Put back any saved access checks. */
16931 pop_deferring_access_checks ();
16932
16933 /* Restore saved state. */
16934 parser->in_function_body = saved_in_function_body;
16935 parser->num_template_parameter_lists
16936 = saved_num_template_parameter_lists;
16937 parser->in_unbraced_linkage_specification_p
16938 = saved_in_unbraced_linkage_specification_p;
16939
16940 return type;
16941 }
16942
16943 /* Parse a class-head.
16944
16945 class-head:
16946 class-key identifier [opt] base-clause [opt]
16947 class-key nested-name-specifier identifier base-clause [opt]
16948 class-key nested-name-specifier [opt] template-id
16949 base-clause [opt]
16950
16951 GNU Extensions:
16952 class-key attributes identifier [opt] base-clause [opt]
16953 class-key attributes nested-name-specifier identifier base-clause [opt]
16954 class-key attributes nested-name-specifier [opt] template-id
16955 base-clause [opt]
16956
16957 Upon return BASES is initialized to the list of base classes (or
16958 NULL, if there are none) in the same form returned by
16959 cp_parser_base_clause.
16960
16961 Returns the TYPE of the indicated class. Sets
16962 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16963 involving a nested-name-specifier was used, and FALSE otherwise.
16964
16965 Returns error_mark_node if this is not a class-head.
16966
16967 Returns NULL_TREE if the class-head is syntactically valid, but
16968 semantically invalid in a way that means we should skip the entire
16969 body of the class. */
16970
16971 static tree
16972 cp_parser_class_head (cp_parser* parser,
16973 bool* nested_name_specifier_p,
16974 tree *attributes_p,
16975 tree *bases)
16976 {
16977 tree nested_name_specifier;
16978 enum tag_types class_key;
16979 tree id = NULL_TREE;
16980 tree type = NULL_TREE;
16981 tree attributes;
16982 bool template_id_p = false;
16983 bool qualified_p = false;
16984 bool invalid_nested_name_p = false;
16985 bool invalid_explicit_specialization_p = false;
16986 tree pushed_scope = NULL_TREE;
16987 unsigned num_templates;
16988 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16989 /* Assume no nested-name-specifier will be present. */
16990 *nested_name_specifier_p = false;
16991 /* Assume no template parameter lists will be used in defining the
16992 type. */
16993 num_templates = 0;
16994
16995 *bases = NULL_TREE;
16996
16997 /* Look for the class-key. */
16998 class_key = cp_parser_class_key (parser);
16999 if (class_key == none_type)
17000 return error_mark_node;
17001
17002 /* Parse the attributes. */
17003 attributes = cp_parser_attributes_opt (parser);
17004
17005 /* If the next token is `::', that is invalid -- but sometimes
17006 people do try to write:
17007
17008 struct ::S {};
17009
17010 Handle this gracefully by accepting the extra qualifier, and then
17011 issuing an error about it later if this really is a
17012 class-head. If it turns out just to be an elaborated type
17013 specifier, remain silent. */
17014 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17015 qualified_p = true;
17016
17017 push_deferring_access_checks (dk_no_check);
17018
17019 /* Determine the name of the class. Begin by looking for an
17020 optional nested-name-specifier. */
17021 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17022 nested_name_specifier
17023 = cp_parser_nested_name_specifier_opt (parser,
17024 /*typename_keyword_p=*/false,
17025 /*check_dependency_p=*/false,
17026 /*type_p=*/false,
17027 /*is_declaration=*/false);
17028 /* If there was a nested-name-specifier, then there *must* be an
17029 identifier. */
17030 if (nested_name_specifier)
17031 {
17032 type_start_token = cp_lexer_peek_token (parser->lexer);
17033 /* Although the grammar says `identifier', it really means
17034 `class-name' or `template-name'. You are only allowed to
17035 define a class that has already been declared with this
17036 syntax.
17037
17038 The proposed resolution for Core Issue 180 says that wherever
17039 you see `class T::X' you should treat `X' as a type-name.
17040
17041 It is OK to define an inaccessible class; for example:
17042
17043 class A { class B; };
17044 class A::B {};
17045
17046 We do not know if we will see a class-name, or a
17047 template-name. We look for a class-name first, in case the
17048 class-name is a template-id; if we looked for the
17049 template-name first we would stop after the template-name. */
17050 cp_parser_parse_tentatively (parser);
17051 type = cp_parser_class_name (parser,
17052 /*typename_keyword_p=*/false,
17053 /*template_keyword_p=*/false,
17054 class_type,
17055 /*check_dependency_p=*/false,
17056 /*class_head_p=*/true,
17057 /*is_declaration=*/false);
17058 /* If that didn't work, ignore the nested-name-specifier. */
17059 if (!cp_parser_parse_definitely (parser))
17060 {
17061 invalid_nested_name_p = true;
17062 type_start_token = cp_lexer_peek_token (parser->lexer);
17063 id = cp_parser_identifier (parser);
17064 if (id == error_mark_node)
17065 id = NULL_TREE;
17066 }
17067 /* If we could not find a corresponding TYPE, treat this
17068 declaration like an unqualified declaration. */
17069 if (type == error_mark_node)
17070 nested_name_specifier = NULL_TREE;
17071 /* Otherwise, count the number of templates used in TYPE and its
17072 containing scopes. */
17073 else
17074 {
17075 tree scope;
17076
17077 for (scope = TREE_TYPE (type);
17078 scope && TREE_CODE (scope) != NAMESPACE_DECL;
17079 scope = (TYPE_P (scope)
17080 ? TYPE_CONTEXT (scope)
17081 : DECL_CONTEXT (scope)))
17082 if (TYPE_P (scope)
17083 && CLASS_TYPE_P (scope)
17084 && CLASSTYPE_TEMPLATE_INFO (scope)
17085 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17086 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17087 ++num_templates;
17088 }
17089 }
17090 /* Otherwise, the identifier is optional. */
17091 else
17092 {
17093 /* We don't know whether what comes next is a template-id,
17094 an identifier, or nothing at all. */
17095 cp_parser_parse_tentatively (parser);
17096 /* Check for a template-id. */
17097 type_start_token = cp_lexer_peek_token (parser->lexer);
17098 id = cp_parser_template_id (parser,
17099 /*template_keyword_p=*/false,
17100 /*check_dependency_p=*/true,
17101 /*is_declaration=*/true);
17102 /* If that didn't work, it could still be an identifier. */
17103 if (!cp_parser_parse_definitely (parser))
17104 {
17105 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17106 {
17107 type_start_token = cp_lexer_peek_token (parser->lexer);
17108 id = cp_parser_identifier (parser);
17109 }
17110 else
17111 id = NULL_TREE;
17112 }
17113 else
17114 {
17115 template_id_p = true;
17116 ++num_templates;
17117 }
17118 }
17119
17120 pop_deferring_access_checks ();
17121
17122 if (id)
17123 cp_parser_check_for_invalid_template_id (parser, id,
17124 type_start_token->location);
17125
17126 /* If it's not a `:' or a `{' then we can't really be looking at a
17127 class-head, since a class-head only appears as part of a
17128 class-specifier. We have to detect this situation before calling
17129 xref_tag, since that has irreversible side-effects. */
17130 if (!cp_parser_next_token_starts_class_definition_p (parser))
17131 {
17132 cp_parser_error (parser, "expected %<{%> or %<:%>");
17133 return error_mark_node;
17134 }
17135
17136 /* At this point, we're going ahead with the class-specifier, even
17137 if some other problem occurs. */
17138 cp_parser_commit_to_tentative_parse (parser);
17139 /* Issue the error about the overly-qualified name now. */
17140 if (qualified_p)
17141 {
17142 cp_parser_error (parser,
17143 "global qualification of class name is invalid");
17144 return error_mark_node;
17145 }
17146 else if (invalid_nested_name_p)
17147 {
17148 cp_parser_error (parser,
17149 "qualified name does not name a class");
17150 return error_mark_node;
17151 }
17152 else if (nested_name_specifier)
17153 {
17154 tree scope;
17155
17156 /* Reject typedef-names in class heads. */
17157 if (!DECL_IMPLICIT_TYPEDEF_P (type))
17158 {
17159 error_at (type_start_token->location,
17160 "invalid class name in declaration of %qD",
17161 type);
17162 type = NULL_TREE;
17163 goto done;
17164 }
17165
17166 /* Figure out in what scope the declaration is being placed. */
17167 scope = current_scope ();
17168 /* If that scope does not contain the scope in which the
17169 class was originally declared, the program is invalid. */
17170 if (scope && !is_ancestor (scope, nested_name_specifier))
17171 {
17172 if (at_namespace_scope_p ())
17173 error_at (type_start_token->location,
17174 "declaration of %qD in namespace %qD which does not "
17175 "enclose %qD",
17176 type, scope, nested_name_specifier);
17177 else
17178 error_at (type_start_token->location,
17179 "declaration of %qD in %qD which does not enclose %qD",
17180 type, scope, nested_name_specifier);
17181 type = NULL_TREE;
17182 goto done;
17183 }
17184 /* [dcl.meaning]
17185
17186 A declarator-id shall not be qualified except for the
17187 definition of a ... nested class outside of its class
17188 ... [or] the definition or explicit instantiation of a
17189 class member of a namespace outside of its namespace. */
17190 if (scope == nested_name_specifier)
17191 {
17192 permerror (nested_name_specifier_token_start->location,
17193 "extra qualification not allowed");
17194 nested_name_specifier = NULL_TREE;
17195 num_templates = 0;
17196 }
17197 }
17198 /* An explicit-specialization must be preceded by "template <>". If
17199 it is not, try to recover gracefully. */
17200 if (at_namespace_scope_p ()
17201 && parser->num_template_parameter_lists == 0
17202 && template_id_p)
17203 {
17204 error_at (type_start_token->location,
17205 "an explicit specialization must be preceded by %<template <>%>");
17206 invalid_explicit_specialization_p = true;
17207 /* Take the same action that would have been taken by
17208 cp_parser_explicit_specialization. */
17209 ++parser->num_template_parameter_lists;
17210 begin_specialization ();
17211 }
17212 /* There must be no "return" statements between this point and the
17213 end of this function; set "type "to the correct return value and
17214 use "goto done;" to return. */
17215 /* Make sure that the right number of template parameters were
17216 present. */
17217 if (!cp_parser_check_template_parameters (parser, num_templates,
17218 type_start_token->location,
17219 /*declarator=*/NULL))
17220 {
17221 /* If something went wrong, there is no point in even trying to
17222 process the class-definition. */
17223 type = NULL_TREE;
17224 goto done;
17225 }
17226
17227 /* Look up the type. */
17228 if (template_id_p)
17229 {
17230 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17231 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17232 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17233 {
17234 error_at (type_start_token->location,
17235 "function template %qD redeclared as a class template", id);
17236 type = error_mark_node;
17237 }
17238 else
17239 {
17240 type = TREE_TYPE (id);
17241 type = maybe_process_partial_specialization (type);
17242 }
17243 if (nested_name_specifier)
17244 pushed_scope = push_scope (nested_name_specifier);
17245 }
17246 else if (nested_name_specifier)
17247 {
17248 tree class_type;
17249
17250 /* Given:
17251
17252 template <typename T> struct S { struct T };
17253 template <typename T> struct S<T>::T { };
17254
17255 we will get a TYPENAME_TYPE when processing the definition of
17256 `S::T'. We need to resolve it to the actual type before we
17257 try to define it. */
17258 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17259 {
17260 class_type = resolve_typename_type (TREE_TYPE (type),
17261 /*only_current_p=*/false);
17262 if (TREE_CODE (class_type) != TYPENAME_TYPE)
17263 type = TYPE_NAME (class_type);
17264 else
17265 {
17266 cp_parser_error (parser, "could not resolve typename type");
17267 type = error_mark_node;
17268 }
17269 }
17270
17271 if (maybe_process_partial_specialization (TREE_TYPE (type))
17272 == error_mark_node)
17273 {
17274 type = NULL_TREE;
17275 goto done;
17276 }
17277
17278 class_type = current_class_type;
17279 /* Enter the scope indicated by the nested-name-specifier. */
17280 pushed_scope = push_scope (nested_name_specifier);
17281 /* Get the canonical version of this type. */
17282 type = TYPE_MAIN_DECL (TREE_TYPE (type));
17283 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17284 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17285 {
17286 type = push_template_decl (type);
17287 if (type == error_mark_node)
17288 {
17289 type = NULL_TREE;
17290 goto done;
17291 }
17292 }
17293
17294 type = TREE_TYPE (type);
17295 *nested_name_specifier_p = true;
17296 }
17297 else /* The name is not a nested name. */
17298 {
17299 /* If the class was unnamed, create a dummy name. */
17300 if (!id)
17301 id = make_anon_name ();
17302 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17303 parser->num_template_parameter_lists);
17304 }
17305
17306 /* Indicate whether this class was declared as a `class' or as a
17307 `struct'. */
17308 if (TREE_CODE (type) == RECORD_TYPE)
17309 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17310 cp_parser_check_class_key (class_key, type);
17311
17312 /* If this type was already complete, and we see another definition,
17313 that's an error. */
17314 if (type != error_mark_node && COMPLETE_TYPE_P (type))
17315 {
17316 error_at (type_start_token->location, "redefinition of %q#T",
17317 type);
17318 error_at (type_start_token->location, "previous definition of %q+#T",
17319 type);
17320 type = NULL_TREE;
17321 goto done;
17322 }
17323 else if (type == error_mark_node)
17324 type = NULL_TREE;
17325
17326 /* We will have entered the scope containing the class; the names of
17327 base classes should be looked up in that context. For example:
17328
17329 struct A { struct B {}; struct C; };
17330 struct A::C : B {};
17331
17332 is valid. */
17333
17334 /* Get the list of base-classes, if there is one. */
17335 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17336 *bases = cp_parser_base_clause (parser);
17337
17338 done:
17339 /* Leave the scope given by the nested-name-specifier. We will
17340 enter the class scope itself while processing the members. */
17341 if (pushed_scope)
17342 pop_scope (pushed_scope);
17343
17344 if (invalid_explicit_specialization_p)
17345 {
17346 end_specialization ();
17347 --parser->num_template_parameter_lists;
17348 }
17349
17350 if (type)
17351 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17352 *attributes_p = attributes;
17353 return type;
17354 }
17355
17356 /* Parse a class-key.
17357
17358 class-key:
17359 class
17360 struct
17361 union
17362
17363 Returns the kind of class-key specified, or none_type to indicate
17364 error. */
17365
17366 static enum tag_types
17367 cp_parser_class_key (cp_parser* parser)
17368 {
17369 cp_token *token;
17370 enum tag_types tag_type;
17371
17372 /* Look for the class-key. */
17373 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17374 if (!token)
17375 return none_type;
17376
17377 /* Check to see if the TOKEN is a class-key. */
17378 tag_type = cp_parser_token_is_class_key (token);
17379 if (!tag_type)
17380 cp_parser_error (parser, "expected class-key");
17381 return tag_type;
17382 }
17383
17384 /* Parse an (optional) member-specification.
17385
17386 member-specification:
17387 member-declaration member-specification [opt]
17388 access-specifier : member-specification [opt] */
17389
17390 static void
17391 cp_parser_member_specification_opt (cp_parser* parser)
17392 {
17393 while (true)
17394 {
17395 cp_token *token;
17396 enum rid keyword;
17397
17398 /* Peek at the next token. */
17399 token = cp_lexer_peek_token (parser->lexer);
17400 /* If it's a `}', or EOF then we've seen all the members. */
17401 if (token->type == CPP_CLOSE_BRACE
17402 || token->type == CPP_EOF
17403 || token->type == CPP_PRAGMA_EOL)
17404 break;
17405
17406 /* See if this token is a keyword. */
17407 keyword = token->keyword;
17408 switch (keyword)
17409 {
17410 case RID_PUBLIC:
17411 case RID_PROTECTED:
17412 case RID_PRIVATE:
17413 /* Consume the access-specifier. */
17414 cp_lexer_consume_token (parser->lexer);
17415 /* Remember which access-specifier is active. */
17416 current_access_specifier = token->u.value;
17417 /* Look for the `:'. */
17418 cp_parser_require (parser, CPP_COLON, RT_COLON);
17419 break;
17420
17421 default:
17422 /* Accept #pragmas at class scope. */
17423 if (token->type == CPP_PRAGMA)
17424 {
17425 cp_parser_pragma (parser, pragma_external);
17426 break;
17427 }
17428
17429 /* Otherwise, the next construction must be a
17430 member-declaration. */
17431 cp_parser_member_declaration (parser);
17432 }
17433 }
17434 }
17435
17436 /* Parse a member-declaration.
17437
17438 member-declaration:
17439 decl-specifier-seq [opt] member-declarator-list [opt] ;
17440 function-definition ; [opt]
17441 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17442 using-declaration
17443 template-declaration
17444
17445 member-declarator-list:
17446 member-declarator
17447 member-declarator-list , member-declarator
17448
17449 member-declarator:
17450 declarator pure-specifier [opt]
17451 declarator constant-initializer [opt]
17452 identifier [opt] : constant-expression
17453
17454 GNU Extensions:
17455
17456 member-declaration:
17457 __extension__ member-declaration
17458
17459 member-declarator:
17460 declarator attributes [opt] pure-specifier [opt]
17461 declarator attributes [opt] constant-initializer [opt]
17462 identifier [opt] attributes [opt] : constant-expression
17463
17464 C++0x Extensions:
17465
17466 member-declaration:
17467 static_assert-declaration */
17468
17469 static void
17470 cp_parser_member_declaration (cp_parser* parser)
17471 {
17472 cp_decl_specifier_seq decl_specifiers;
17473 tree prefix_attributes;
17474 tree decl;
17475 int declares_class_or_enum;
17476 bool friend_p;
17477 cp_token *token = NULL;
17478 cp_token *decl_spec_token_start = NULL;
17479 cp_token *initializer_token_start = NULL;
17480 int saved_pedantic;
17481
17482 /* Check for the `__extension__' keyword. */
17483 if (cp_parser_extension_opt (parser, &saved_pedantic))
17484 {
17485 /* Recurse. */
17486 cp_parser_member_declaration (parser);
17487 /* Restore the old value of the PEDANTIC flag. */
17488 pedantic = saved_pedantic;
17489
17490 return;
17491 }
17492
17493 /* Check for a template-declaration. */
17494 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17495 {
17496 /* An explicit specialization here is an error condition, and we
17497 expect the specialization handler to detect and report this. */
17498 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17499 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17500 cp_parser_explicit_specialization (parser);
17501 else
17502 cp_parser_template_declaration (parser, /*member_p=*/true);
17503
17504 return;
17505 }
17506
17507 /* Check for a using-declaration. */
17508 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17509 {
17510 /* Parse the using-declaration. */
17511 cp_parser_using_declaration (parser,
17512 /*access_declaration_p=*/false);
17513 return;
17514 }
17515
17516 /* Check for @defs. */
17517 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17518 {
17519 tree ivar, member;
17520 tree ivar_chains = cp_parser_objc_defs_expression (parser);
17521 ivar = ivar_chains;
17522 while (ivar)
17523 {
17524 member = ivar;
17525 ivar = TREE_CHAIN (member);
17526 TREE_CHAIN (member) = NULL_TREE;
17527 finish_member_declaration (member);
17528 }
17529 return;
17530 }
17531
17532 /* If the next token is `static_assert' we have a static assertion. */
17533 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17534 {
17535 cp_parser_static_assert (parser, /*member_p=*/true);
17536 return;
17537 }
17538
17539 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17540 return;
17541
17542 /* Parse the decl-specifier-seq. */
17543 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17544 cp_parser_decl_specifier_seq (parser,
17545 CP_PARSER_FLAGS_OPTIONAL,
17546 &decl_specifiers,
17547 &declares_class_or_enum);
17548 prefix_attributes = decl_specifiers.attributes;
17549 decl_specifiers.attributes = NULL_TREE;
17550 /* Check for an invalid type-name. */
17551 if (!decl_specifiers.any_type_specifiers_p
17552 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17553 return;
17554 /* If there is no declarator, then the decl-specifier-seq should
17555 specify a type. */
17556 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17557 {
17558 /* If there was no decl-specifier-seq, and the next token is a
17559 `;', then we have something like:
17560
17561 struct S { ; };
17562
17563 [class.mem]
17564
17565 Each member-declaration shall declare at least one member
17566 name of the class. */
17567 if (!decl_specifiers.any_specifiers_p)
17568 {
17569 cp_token *token = cp_lexer_peek_token (parser->lexer);
17570 if (!in_system_header_at (token->location))
17571 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17572 }
17573 else
17574 {
17575 tree type;
17576
17577 /* See if this declaration is a friend. */
17578 friend_p = cp_parser_friend_p (&decl_specifiers);
17579 /* If there were decl-specifiers, check to see if there was
17580 a class-declaration. */
17581 type = check_tag_decl (&decl_specifiers);
17582 /* Nested classes have already been added to the class, but
17583 a `friend' needs to be explicitly registered. */
17584 if (friend_p)
17585 {
17586 /* If the `friend' keyword was present, the friend must
17587 be introduced with a class-key. */
17588 if (!declares_class_or_enum)
17589 error_at (decl_spec_token_start->location,
17590 "a class-key must be used when declaring a friend");
17591 /* In this case:
17592
17593 template <typename T> struct A {
17594 friend struct A<T>::B;
17595 };
17596
17597 A<T>::B will be represented by a TYPENAME_TYPE, and
17598 therefore not recognized by check_tag_decl. */
17599 if (!type
17600 && decl_specifiers.type
17601 && TYPE_P (decl_specifiers.type))
17602 type = decl_specifiers.type;
17603 if (!type || !TYPE_P (type))
17604 error_at (decl_spec_token_start->location,
17605 "friend declaration does not name a class or "
17606 "function");
17607 else
17608 make_friend_class (current_class_type, type,
17609 /*complain=*/true);
17610 }
17611 /* If there is no TYPE, an error message will already have
17612 been issued. */
17613 else if (!type || type == error_mark_node)
17614 ;
17615 /* An anonymous aggregate has to be handled specially; such
17616 a declaration really declares a data member (with a
17617 particular type), as opposed to a nested class. */
17618 else if (ANON_AGGR_TYPE_P (type))
17619 {
17620 /* Remove constructors and such from TYPE, now that we
17621 know it is an anonymous aggregate. */
17622 fixup_anonymous_aggr (type);
17623 /* And make the corresponding data member. */
17624 decl = build_decl (decl_spec_token_start->location,
17625 FIELD_DECL, NULL_TREE, type);
17626 /* Add it to the class. */
17627 finish_member_declaration (decl);
17628 }
17629 else
17630 cp_parser_check_access_in_redeclaration
17631 (TYPE_NAME (type),
17632 decl_spec_token_start->location);
17633 }
17634 }
17635 else
17636 {
17637 bool assume_semicolon = false;
17638
17639 /* See if these declarations will be friends. */
17640 friend_p = cp_parser_friend_p (&decl_specifiers);
17641
17642 /* Keep going until we hit the `;' at the end of the
17643 declaration. */
17644 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17645 {
17646 tree attributes = NULL_TREE;
17647 tree first_attribute;
17648
17649 /* Peek at the next token. */
17650 token = cp_lexer_peek_token (parser->lexer);
17651
17652 /* Check for a bitfield declaration. */
17653 if (token->type == CPP_COLON
17654 || (token->type == CPP_NAME
17655 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17656 == CPP_COLON))
17657 {
17658 tree identifier;
17659 tree width;
17660
17661 /* Get the name of the bitfield. Note that we cannot just
17662 check TOKEN here because it may have been invalidated by
17663 the call to cp_lexer_peek_nth_token above. */
17664 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17665 identifier = cp_parser_identifier (parser);
17666 else
17667 identifier = NULL_TREE;
17668
17669 /* Consume the `:' token. */
17670 cp_lexer_consume_token (parser->lexer);
17671 /* Get the width of the bitfield. */
17672 width
17673 = cp_parser_constant_expression (parser,
17674 /*allow_non_constant=*/false,
17675 NULL);
17676
17677 /* Look for attributes that apply to the bitfield. */
17678 attributes = cp_parser_attributes_opt (parser);
17679 /* Remember which attributes are prefix attributes and
17680 which are not. */
17681 first_attribute = attributes;
17682 /* Combine the attributes. */
17683 attributes = chainon (prefix_attributes, attributes);
17684
17685 /* Create the bitfield declaration. */
17686 decl = grokbitfield (identifier
17687 ? make_id_declarator (NULL_TREE,
17688 identifier,
17689 sfk_none)
17690 : NULL,
17691 &decl_specifiers,
17692 width,
17693 attributes);
17694 }
17695 else
17696 {
17697 cp_declarator *declarator;
17698 tree initializer;
17699 tree asm_specification;
17700 int ctor_dtor_or_conv_p;
17701
17702 /* Parse the declarator. */
17703 declarator
17704 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17705 &ctor_dtor_or_conv_p,
17706 /*parenthesized_p=*/NULL,
17707 /*member_p=*/true);
17708
17709 /* If something went wrong parsing the declarator, make sure
17710 that we at least consume some tokens. */
17711 if (declarator == cp_error_declarator)
17712 {
17713 /* Skip to the end of the statement. */
17714 cp_parser_skip_to_end_of_statement (parser);
17715 /* If the next token is not a semicolon, that is
17716 probably because we just skipped over the body of
17717 a function. So, we consume a semicolon if
17718 present, but do not issue an error message if it
17719 is not present. */
17720 if (cp_lexer_next_token_is (parser->lexer,
17721 CPP_SEMICOLON))
17722 cp_lexer_consume_token (parser->lexer);
17723 return;
17724 }
17725
17726 if (declares_class_or_enum & 2)
17727 cp_parser_check_for_definition_in_return_type
17728 (declarator, decl_specifiers.type,
17729 decl_specifiers.type_location);
17730
17731 /* Look for an asm-specification. */
17732 asm_specification = cp_parser_asm_specification_opt (parser);
17733 /* Look for attributes that apply to the declaration. */
17734 attributes = cp_parser_attributes_opt (parser);
17735 /* Remember which attributes are prefix attributes and
17736 which are not. */
17737 first_attribute = attributes;
17738 /* Combine the attributes. */
17739 attributes = chainon (prefix_attributes, attributes);
17740
17741 /* If it's an `=', then we have a constant-initializer or a
17742 pure-specifier. It is not correct to parse the
17743 initializer before registering the member declaration
17744 since the member declaration should be in scope while
17745 its initializer is processed. However, the rest of the
17746 front end does not yet provide an interface that allows
17747 us to handle this correctly. */
17748 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17749 {
17750 /* In [class.mem]:
17751
17752 A pure-specifier shall be used only in the declaration of
17753 a virtual function.
17754
17755 A member-declarator can contain a constant-initializer
17756 only if it declares a static member of integral or
17757 enumeration type.
17758
17759 Therefore, if the DECLARATOR is for a function, we look
17760 for a pure-specifier; otherwise, we look for a
17761 constant-initializer. When we call `grokfield', it will
17762 perform more stringent semantics checks. */
17763 initializer_token_start = cp_lexer_peek_token (parser->lexer);
17764 if (function_declarator_p (declarator))
17765 initializer = cp_parser_pure_specifier (parser);
17766 else
17767 /* Parse the initializer. */
17768 initializer = cp_parser_constant_initializer (parser);
17769 }
17770 /* Otherwise, there is no initializer. */
17771 else
17772 initializer = NULL_TREE;
17773
17774 /* See if we are probably looking at a function
17775 definition. We are certainly not looking at a
17776 member-declarator. Calling `grokfield' has
17777 side-effects, so we must not do it unless we are sure
17778 that we are looking at a member-declarator. */
17779 if (cp_parser_token_starts_function_definition_p
17780 (cp_lexer_peek_token (parser->lexer)))
17781 {
17782 /* The grammar does not allow a pure-specifier to be
17783 used when a member function is defined. (It is
17784 possible that this fact is an oversight in the
17785 standard, since a pure function may be defined
17786 outside of the class-specifier. */
17787 if (initializer)
17788 error_at (initializer_token_start->location,
17789 "pure-specifier on function-definition");
17790 decl = cp_parser_save_member_function_body (parser,
17791 &decl_specifiers,
17792 declarator,
17793 attributes);
17794 /* If the member was not a friend, declare it here. */
17795 if (!friend_p)
17796 finish_member_declaration (decl);
17797 /* Peek at the next token. */
17798 token = cp_lexer_peek_token (parser->lexer);
17799 /* If the next token is a semicolon, consume it. */
17800 if (token->type == CPP_SEMICOLON)
17801 cp_lexer_consume_token (parser->lexer);
17802 return;
17803 }
17804 else
17805 if (declarator->kind == cdk_function)
17806 declarator->id_loc = token->location;
17807 /* Create the declaration. */
17808 decl = grokfield (declarator, &decl_specifiers,
17809 initializer, /*init_const_expr_p=*/true,
17810 asm_specification,
17811 attributes);
17812 }
17813
17814 /* Reset PREFIX_ATTRIBUTES. */
17815 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17816 attributes = TREE_CHAIN (attributes);
17817 if (attributes)
17818 TREE_CHAIN (attributes) = NULL_TREE;
17819
17820 /* If there is any qualification still in effect, clear it
17821 now; we will be starting fresh with the next declarator. */
17822 parser->scope = NULL_TREE;
17823 parser->qualifying_scope = NULL_TREE;
17824 parser->object_scope = NULL_TREE;
17825 /* If it's a `,', then there are more declarators. */
17826 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17827 cp_lexer_consume_token (parser->lexer);
17828 /* If the next token isn't a `;', then we have a parse error. */
17829 else if (cp_lexer_next_token_is_not (parser->lexer,
17830 CPP_SEMICOLON))
17831 {
17832 /* The next token might be a ways away from where the
17833 actual semicolon is missing. Find the previous token
17834 and use that for our error position. */
17835 cp_token *token = cp_lexer_previous_token (parser->lexer);
17836 error_at (token->location,
17837 "expected %<;%> at end of member declaration");
17838
17839 /* Assume that the user meant to provide a semicolon. If
17840 we were to cp_parser_skip_to_end_of_statement, we might
17841 skip to a semicolon inside a member function definition
17842 and issue nonsensical error messages. */
17843 assume_semicolon = true;
17844 }
17845
17846 if (decl)
17847 {
17848 /* Add DECL to the list of members. */
17849 if (!friend_p)
17850 finish_member_declaration (decl);
17851
17852 if (TREE_CODE (decl) == FUNCTION_DECL)
17853 cp_parser_save_default_args (parser, decl);
17854 }
17855
17856 if (assume_semicolon)
17857 return;
17858 }
17859 }
17860
17861 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17862 }
17863
17864 /* Parse a pure-specifier.
17865
17866 pure-specifier:
17867 = 0
17868
17869 Returns INTEGER_ZERO_NODE if a pure specifier is found.
17870 Otherwise, ERROR_MARK_NODE is returned. */
17871
17872 static tree
17873 cp_parser_pure_specifier (cp_parser* parser)
17874 {
17875 cp_token *token;
17876
17877 /* Look for the `=' token. */
17878 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17879 return error_mark_node;
17880 /* Look for the `0' token. */
17881 token = cp_lexer_peek_token (parser->lexer);
17882
17883 if (token->type == CPP_EOF
17884 || token->type == CPP_PRAGMA_EOL)
17885 return error_mark_node;
17886
17887 cp_lexer_consume_token (parser->lexer);
17888
17889 /* Accept = default or = delete in c++0x mode. */
17890 if (token->keyword == RID_DEFAULT
17891 || token->keyword == RID_DELETE)
17892 {
17893 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17894 return token->u.value;
17895 }
17896
17897 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
17898 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
17899 {
17900 cp_parser_error (parser,
17901 "invalid pure specifier (only %<= 0%> is allowed)");
17902 cp_parser_skip_to_end_of_statement (parser);
17903 return error_mark_node;
17904 }
17905 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17906 {
17907 error_at (token->location, "templates may not be %<virtual%>");
17908 return error_mark_node;
17909 }
17910
17911 return integer_zero_node;
17912 }
17913
17914 /* Parse a constant-initializer.
17915
17916 constant-initializer:
17917 = constant-expression
17918
17919 Returns a representation of the constant-expression. */
17920
17921 static tree
17922 cp_parser_constant_initializer (cp_parser* parser)
17923 {
17924 /* Look for the `=' token. */
17925 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17926 return error_mark_node;
17927
17928 /* It is invalid to write:
17929
17930 struct S { static const int i = { 7 }; };
17931
17932 */
17933 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17934 {
17935 cp_parser_error (parser,
17936 "a brace-enclosed initializer is not allowed here");
17937 /* Consume the opening brace. */
17938 cp_lexer_consume_token (parser->lexer);
17939 /* Skip the initializer. */
17940 cp_parser_skip_to_closing_brace (parser);
17941 /* Look for the trailing `}'. */
17942 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17943
17944 return error_mark_node;
17945 }
17946
17947 return cp_parser_constant_expression (parser,
17948 /*allow_non_constant=*/false,
17949 NULL);
17950 }
17951
17952 /* Derived classes [gram.class.derived] */
17953
17954 /* Parse a base-clause.
17955
17956 base-clause:
17957 : base-specifier-list
17958
17959 base-specifier-list:
17960 base-specifier ... [opt]
17961 base-specifier-list , base-specifier ... [opt]
17962
17963 Returns a TREE_LIST representing the base-classes, in the order in
17964 which they were declared. The representation of each node is as
17965 described by cp_parser_base_specifier.
17966
17967 In the case that no bases are specified, this function will return
17968 NULL_TREE, not ERROR_MARK_NODE. */
17969
17970 static tree
17971 cp_parser_base_clause (cp_parser* parser)
17972 {
17973 tree bases = NULL_TREE;
17974
17975 /* Look for the `:' that begins the list. */
17976 cp_parser_require (parser, CPP_COLON, RT_COLON);
17977
17978 /* Scan the base-specifier-list. */
17979 while (true)
17980 {
17981 cp_token *token;
17982 tree base;
17983 bool pack_expansion_p = false;
17984
17985 /* Look for the base-specifier. */
17986 base = cp_parser_base_specifier (parser);
17987 /* Look for the (optional) ellipsis. */
17988 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17989 {
17990 /* Consume the `...'. */
17991 cp_lexer_consume_token (parser->lexer);
17992
17993 pack_expansion_p = true;
17994 }
17995
17996 /* Add BASE to the front of the list. */
17997 if (base != error_mark_node)
17998 {
17999 if (pack_expansion_p)
18000 /* Make this a pack expansion type. */
18001 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18002
18003
18004 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18005 {
18006 TREE_CHAIN (base) = bases;
18007 bases = base;
18008 }
18009 }
18010 /* Peek at the next token. */
18011 token = cp_lexer_peek_token (parser->lexer);
18012 /* If it's not a comma, then the list is complete. */
18013 if (token->type != CPP_COMMA)
18014 break;
18015 /* Consume the `,'. */
18016 cp_lexer_consume_token (parser->lexer);
18017 }
18018
18019 /* PARSER->SCOPE may still be non-NULL at this point, if the last
18020 base class had a qualified name. However, the next name that
18021 appears is certainly not qualified. */
18022 parser->scope = NULL_TREE;
18023 parser->qualifying_scope = NULL_TREE;
18024 parser->object_scope = NULL_TREE;
18025
18026 return nreverse (bases);
18027 }
18028
18029 /* Parse a base-specifier.
18030
18031 base-specifier:
18032 :: [opt] nested-name-specifier [opt] class-name
18033 virtual access-specifier [opt] :: [opt] nested-name-specifier
18034 [opt] class-name
18035 access-specifier virtual [opt] :: [opt] nested-name-specifier
18036 [opt] class-name
18037
18038 Returns a TREE_LIST. The TREE_PURPOSE will be one of
18039 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18040 indicate the specifiers provided. The TREE_VALUE will be a TYPE
18041 (or the ERROR_MARK_NODE) indicating the type that was specified. */
18042
18043 static tree
18044 cp_parser_base_specifier (cp_parser* parser)
18045 {
18046 cp_token *token;
18047 bool done = false;
18048 bool virtual_p = false;
18049 bool duplicate_virtual_error_issued_p = false;
18050 bool duplicate_access_error_issued_p = false;
18051 bool class_scope_p, template_p;
18052 tree access = access_default_node;
18053 tree type;
18054
18055 /* Process the optional `virtual' and `access-specifier'. */
18056 while (!done)
18057 {
18058 /* Peek at the next token. */
18059 token = cp_lexer_peek_token (parser->lexer);
18060 /* Process `virtual'. */
18061 switch (token->keyword)
18062 {
18063 case RID_VIRTUAL:
18064 /* If `virtual' appears more than once, issue an error. */
18065 if (virtual_p && !duplicate_virtual_error_issued_p)
18066 {
18067 cp_parser_error (parser,
18068 "%<virtual%> specified more than once in base-specified");
18069 duplicate_virtual_error_issued_p = true;
18070 }
18071
18072 virtual_p = true;
18073
18074 /* Consume the `virtual' token. */
18075 cp_lexer_consume_token (parser->lexer);
18076
18077 break;
18078
18079 case RID_PUBLIC:
18080 case RID_PROTECTED:
18081 case RID_PRIVATE:
18082 /* If more than one access specifier appears, issue an
18083 error. */
18084 if (access != access_default_node
18085 && !duplicate_access_error_issued_p)
18086 {
18087 cp_parser_error (parser,
18088 "more than one access specifier in base-specified");
18089 duplicate_access_error_issued_p = true;
18090 }
18091
18092 access = ridpointers[(int) token->keyword];
18093
18094 /* Consume the access-specifier. */
18095 cp_lexer_consume_token (parser->lexer);
18096
18097 break;
18098
18099 default:
18100 done = true;
18101 break;
18102 }
18103 }
18104 /* It is not uncommon to see programs mechanically, erroneously, use
18105 the 'typename' keyword to denote (dependent) qualified types
18106 as base classes. */
18107 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18108 {
18109 token = cp_lexer_peek_token (parser->lexer);
18110 if (!processing_template_decl)
18111 error_at (token->location,
18112 "keyword %<typename%> not allowed outside of templates");
18113 else
18114 error_at (token->location,
18115 "keyword %<typename%> not allowed in this context "
18116 "(the base class is implicitly a type)");
18117 cp_lexer_consume_token (parser->lexer);
18118 }
18119
18120 /* Look for the optional `::' operator. */
18121 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18122 /* Look for the nested-name-specifier. The simplest way to
18123 implement:
18124
18125 [temp.res]
18126
18127 The keyword `typename' is not permitted in a base-specifier or
18128 mem-initializer; in these contexts a qualified name that
18129 depends on a template-parameter is implicitly assumed to be a
18130 type name.
18131
18132 is to pretend that we have seen the `typename' keyword at this
18133 point. */
18134 cp_parser_nested_name_specifier_opt (parser,
18135 /*typename_keyword_p=*/true,
18136 /*check_dependency_p=*/true,
18137 typename_type,
18138 /*is_declaration=*/true);
18139 /* If the base class is given by a qualified name, assume that names
18140 we see are type names or templates, as appropriate. */
18141 class_scope_p = (parser->scope && TYPE_P (parser->scope));
18142 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18143
18144 /* Finally, look for the class-name. */
18145 type = cp_parser_class_name (parser,
18146 class_scope_p,
18147 template_p,
18148 typename_type,
18149 /*check_dependency_p=*/true,
18150 /*class_head_p=*/false,
18151 /*is_declaration=*/true);
18152
18153 if (type == error_mark_node)
18154 return error_mark_node;
18155
18156 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18157 }
18158
18159 /* Exception handling [gram.exception] */
18160
18161 /* Parse an (optional) exception-specification.
18162
18163 exception-specification:
18164 throw ( type-id-list [opt] )
18165
18166 Returns a TREE_LIST representing the exception-specification. The
18167 TREE_VALUE of each node is a type. */
18168
18169 static tree
18170 cp_parser_exception_specification_opt (cp_parser* parser)
18171 {
18172 cp_token *token;
18173 tree type_id_list;
18174 const char *saved_message;
18175
18176 /* Peek at the next token. */
18177 token = cp_lexer_peek_token (parser->lexer);
18178
18179 /* Is it a noexcept-specification? */
18180 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18181 {
18182 tree expr;
18183 cp_lexer_consume_token (parser->lexer);
18184
18185 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18186 {
18187 cp_lexer_consume_token (parser->lexer);
18188
18189 /* Types may not be defined in an exception-specification. */
18190 saved_message = parser->type_definition_forbidden_message;
18191 parser->type_definition_forbidden_message
18192 = G_("types may not be defined in an exception-specification");
18193
18194 expr = cp_parser_constant_expression (parser, false, NULL);
18195
18196 /* Restore the saved message. */
18197 parser->type_definition_forbidden_message = saved_message;
18198
18199 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18200 }
18201 else
18202 expr = boolean_true_node;
18203
18204 return build_noexcept_spec (expr, tf_warning_or_error);
18205 }
18206
18207 /* If it's not `throw', then there's no exception-specification. */
18208 if (!cp_parser_is_keyword (token, RID_THROW))
18209 return NULL_TREE;
18210
18211 #if 0
18212 /* Enable this once a lot of code has transitioned to noexcept? */
18213 if (cxx_dialect == cxx0x && !in_system_header)
18214 warning (OPT_Wdeprecated, "dynamic exception specifications are "
18215 "deprecated in C++0x; use %<noexcept%> instead");
18216 #endif
18217
18218 /* Consume the `throw'. */
18219 cp_lexer_consume_token (parser->lexer);
18220
18221 /* Look for the `('. */
18222 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18223
18224 /* Peek at the next token. */
18225 token = cp_lexer_peek_token (parser->lexer);
18226 /* If it's not a `)', then there is a type-id-list. */
18227 if (token->type != CPP_CLOSE_PAREN)
18228 {
18229 /* Types may not be defined in an exception-specification. */
18230 saved_message = parser->type_definition_forbidden_message;
18231 parser->type_definition_forbidden_message
18232 = G_("types may not be defined in an exception-specification");
18233 /* Parse the type-id-list. */
18234 type_id_list = cp_parser_type_id_list (parser);
18235 /* Restore the saved message. */
18236 parser->type_definition_forbidden_message = saved_message;
18237 }
18238 else
18239 type_id_list = empty_except_spec;
18240
18241 /* Look for the `)'. */
18242 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18243
18244 return type_id_list;
18245 }
18246
18247 /* Parse an (optional) type-id-list.
18248
18249 type-id-list:
18250 type-id ... [opt]
18251 type-id-list , type-id ... [opt]
18252
18253 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
18254 in the order that the types were presented. */
18255
18256 static tree
18257 cp_parser_type_id_list (cp_parser* parser)
18258 {
18259 tree types = NULL_TREE;
18260
18261 while (true)
18262 {
18263 cp_token *token;
18264 tree type;
18265
18266 /* Get the next type-id. */
18267 type = cp_parser_type_id (parser);
18268 /* Parse the optional ellipsis. */
18269 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18270 {
18271 /* Consume the `...'. */
18272 cp_lexer_consume_token (parser->lexer);
18273
18274 /* Turn the type into a pack expansion expression. */
18275 type = make_pack_expansion (type);
18276 }
18277 /* Add it to the list. */
18278 types = add_exception_specifier (types, type, /*complain=*/1);
18279 /* Peek at the next token. */
18280 token = cp_lexer_peek_token (parser->lexer);
18281 /* If it is not a `,', we are done. */
18282 if (token->type != CPP_COMMA)
18283 break;
18284 /* Consume the `,'. */
18285 cp_lexer_consume_token (parser->lexer);
18286 }
18287
18288 return nreverse (types);
18289 }
18290
18291 /* Parse a try-block.
18292
18293 try-block:
18294 try compound-statement handler-seq */
18295
18296 static tree
18297 cp_parser_try_block (cp_parser* parser)
18298 {
18299 tree try_block;
18300
18301 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18302 try_block = begin_try_block ();
18303 cp_parser_compound_statement (parser, NULL, true);
18304 finish_try_block (try_block);
18305 cp_parser_handler_seq (parser);
18306 finish_handler_sequence (try_block);
18307
18308 return try_block;
18309 }
18310
18311 /* Parse a function-try-block.
18312
18313 function-try-block:
18314 try ctor-initializer [opt] function-body handler-seq */
18315
18316 static bool
18317 cp_parser_function_try_block (cp_parser* parser)
18318 {
18319 tree compound_stmt;
18320 tree try_block;
18321 bool ctor_initializer_p;
18322
18323 /* Look for the `try' keyword. */
18324 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18325 return false;
18326 /* Let the rest of the front end know where we are. */
18327 try_block = begin_function_try_block (&compound_stmt);
18328 /* Parse the function-body. */
18329 ctor_initializer_p
18330 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18331 /* We're done with the `try' part. */
18332 finish_function_try_block (try_block);
18333 /* Parse the handlers. */
18334 cp_parser_handler_seq (parser);
18335 /* We're done with the handlers. */
18336 finish_function_handler_sequence (try_block, compound_stmt);
18337
18338 return ctor_initializer_p;
18339 }
18340
18341 /* Parse a handler-seq.
18342
18343 handler-seq:
18344 handler handler-seq [opt] */
18345
18346 static void
18347 cp_parser_handler_seq (cp_parser* parser)
18348 {
18349 while (true)
18350 {
18351 cp_token *token;
18352
18353 /* Parse the handler. */
18354 cp_parser_handler (parser);
18355 /* Peek at the next token. */
18356 token = cp_lexer_peek_token (parser->lexer);
18357 /* If it's not `catch' then there are no more handlers. */
18358 if (!cp_parser_is_keyword (token, RID_CATCH))
18359 break;
18360 }
18361 }
18362
18363 /* Parse a handler.
18364
18365 handler:
18366 catch ( exception-declaration ) compound-statement */
18367
18368 static void
18369 cp_parser_handler (cp_parser* parser)
18370 {
18371 tree handler;
18372 tree declaration;
18373
18374 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18375 handler = begin_handler ();
18376 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18377 declaration = cp_parser_exception_declaration (parser);
18378 finish_handler_parms (declaration, handler);
18379 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18380 cp_parser_compound_statement (parser, NULL, false);
18381 finish_handler (handler);
18382 }
18383
18384 /* Parse an exception-declaration.
18385
18386 exception-declaration:
18387 type-specifier-seq declarator
18388 type-specifier-seq abstract-declarator
18389 type-specifier-seq
18390 ...
18391
18392 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18393 ellipsis variant is used. */
18394
18395 static tree
18396 cp_parser_exception_declaration (cp_parser* parser)
18397 {
18398 cp_decl_specifier_seq type_specifiers;
18399 cp_declarator *declarator;
18400 const char *saved_message;
18401
18402 /* If it's an ellipsis, it's easy to handle. */
18403 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18404 {
18405 /* Consume the `...' token. */
18406 cp_lexer_consume_token (parser->lexer);
18407 return NULL_TREE;
18408 }
18409
18410 /* Types may not be defined in exception-declarations. */
18411 saved_message = parser->type_definition_forbidden_message;
18412 parser->type_definition_forbidden_message
18413 = G_("types may not be defined in exception-declarations");
18414
18415 /* Parse the type-specifier-seq. */
18416 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18417 /*is_trailing_return=*/false,
18418 &type_specifiers);
18419 /* If it's a `)', then there is no declarator. */
18420 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18421 declarator = NULL;
18422 else
18423 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18424 /*ctor_dtor_or_conv_p=*/NULL,
18425 /*parenthesized_p=*/NULL,
18426 /*member_p=*/false);
18427
18428 /* Restore the saved message. */
18429 parser->type_definition_forbidden_message = saved_message;
18430
18431 if (!type_specifiers.any_specifiers_p)
18432 return error_mark_node;
18433
18434 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18435 }
18436
18437 /* Parse a throw-expression.
18438
18439 throw-expression:
18440 throw assignment-expression [opt]
18441
18442 Returns a THROW_EXPR representing the throw-expression. */
18443
18444 static tree
18445 cp_parser_throw_expression (cp_parser* parser)
18446 {
18447 tree expression;
18448 cp_token* token;
18449
18450 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18451 token = cp_lexer_peek_token (parser->lexer);
18452 /* Figure out whether or not there is an assignment-expression
18453 following the "throw" keyword. */
18454 if (token->type == CPP_COMMA
18455 || token->type == CPP_SEMICOLON
18456 || token->type == CPP_CLOSE_PAREN
18457 || token->type == CPP_CLOSE_SQUARE
18458 || token->type == CPP_CLOSE_BRACE
18459 || token->type == CPP_COLON)
18460 expression = NULL_TREE;
18461 else
18462 expression = cp_parser_assignment_expression (parser,
18463 /*cast_p=*/false, NULL);
18464
18465 return build_throw (expression);
18466 }
18467
18468 /* GNU Extensions */
18469
18470 /* Parse an (optional) asm-specification.
18471
18472 asm-specification:
18473 asm ( string-literal )
18474
18475 If the asm-specification is present, returns a STRING_CST
18476 corresponding to the string-literal. Otherwise, returns
18477 NULL_TREE. */
18478
18479 static tree
18480 cp_parser_asm_specification_opt (cp_parser* parser)
18481 {
18482 cp_token *token;
18483 tree asm_specification;
18484
18485 /* Peek at the next token. */
18486 token = cp_lexer_peek_token (parser->lexer);
18487 /* If the next token isn't the `asm' keyword, then there's no
18488 asm-specification. */
18489 if (!cp_parser_is_keyword (token, RID_ASM))
18490 return NULL_TREE;
18491
18492 /* Consume the `asm' token. */
18493 cp_lexer_consume_token (parser->lexer);
18494 /* Look for the `('. */
18495 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18496
18497 /* Look for the string-literal. */
18498 asm_specification = cp_parser_string_literal (parser, false, false);
18499
18500 /* Look for the `)'. */
18501 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18502
18503 return asm_specification;
18504 }
18505
18506 /* Parse an asm-operand-list.
18507
18508 asm-operand-list:
18509 asm-operand
18510 asm-operand-list , asm-operand
18511
18512 asm-operand:
18513 string-literal ( expression )
18514 [ string-literal ] string-literal ( expression )
18515
18516 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18517 each node is the expression. The TREE_PURPOSE is itself a
18518 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18519 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18520 is a STRING_CST for the string literal before the parenthesis. Returns
18521 ERROR_MARK_NODE if any of the operands are invalid. */
18522
18523 static tree
18524 cp_parser_asm_operand_list (cp_parser* parser)
18525 {
18526 tree asm_operands = NULL_TREE;
18527 bool invalid_operands = false;
18528
18529 while (true)
18530 {
18531 tree string_literal;
18532 tree expression;
18533 tree name;
18534
18535 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18536 {
18537 /* Consume the `[' token. */
18538 cp_lexer_consume_token (parser->lexer);
18539 /* Read the operand name. */
18540 name = cp_parser_identifier (parser);
18541 if (name != error_mark_node)
18542 name = build_string (IDENTIFIER_LENGTH (name),
18543 IDENTIFIER_POINTER (name));
18544 /* Look for the closing `]'. */
18545 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18546 }
18547 else
18548 name = NULL_TREE;
18549 /* Look for the string-literal. */
18550 string_literal = cp_parser_string_literal (parser, false, false);
18551
18552 /* Look for the `('. */
18553 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18554 /* Parse the expression. */
18555 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18556 /* Look for the `)'. */
18557 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18558
18559 if (name == error_mark_node
18560 || string_literal == error_mark_node
18561 || expression == error_mark_node)
18562 invalid_operands = true;
18563
18564 /* Add this operand to the list. */
18565 asm_operands = tree_cons (build_tree_list (name, string_literal),
18566 expression,
18567 asm_operands);
18568 /* If the next token is not a `,', there are no more
18569 operands. */
18570 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18571 break;
18572 /* Consume the `,'. */
18573 cp_lexer_consume_token (parser->lexer);
18574 }
18575
18576 return invalid_operands ? error_mark_node : nreverse (asm_operands);
18577 }
18578
18579 /* Parse an asm-clobber-list.
18580
18581 asm-clobber-list:
18582 string-literal
18583 asm-clobber-list , string-literal
18584
18585 Returns a TREE_LIST, indicating the clobbers in the order that they
18586 appeared. The TREE_VALUE of each node is a STRING_CST. */
18587
18588 static tree
18589 cp_parser_asm_clobber_list (cp_parser* parser)
18590 {
18591 tree clobbers = NULL_TREE;
18592
18593 while (true)
18594 {
18595 tree string_literal;
18596
18597 /* Look for the string literal. */
18598 string_literal = cp_parser_string_literal (parser, false, false);
18599 /* Add it to the list. */
18600 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18601 /* If the next token is not a `,', then the list is
18602 complete. */
18603 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18604 break;
18605 /* Consume the `,' token. */
18606 cp_lexer_consume_token (parser->lexer);
18607 }
18608
18609 return clobbers;
18610 }
18611
18612 /* Parse an asm-label-list.
18613
18614 asm-label-list:
18615 identifier
18616 asm-label-list , identifier
18617
18618 Returns a TREE_LIST, indicating the labels in the order that they
18619 appeared. The TREE_VALUE of each node is a label. */
18620
18621 static tree
18622 cp_parser_asm_label_list (cp_parser* parser)
18623 {
18624 tree labels = NULL_TREE;
18625
18626 while (true)
18627 {
18628 tree identifier, label, name;
18629
18630 /* Look for the identifier. */
18631 identifier = cp_parser_identifier (parser);
18632 if (!error_operand_p (identifier))
18633 {
18634 label = lookup_label (identifier);
18635 if (TREE_CODE (label) == LABEL_DECL)
18636 {
18637 TREE_USED (label) = 1;
18638 check_goto (label);
18639 name = build_string (IDENTIFIER_LENGTH (identifier),
18640 IDENTIFIER_POINTER (identifier));
18641 labels = tree_cons (name, label, labels);
18642 }
18643 }
18644 /* If the next token is not a `,', then the list is
18645 complete. */
18646 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18647 break;
18648 /* Consume the `,' token. */
18649 cp_lexer_consume_token (parser->lexer);
18650 }
18651
18652 return nreverse (labels);
18653 }
18654
18655 /* Parse an (optional) series of attributes.
18656
18657 attributes:
18658 attributes attribute
18659
18660 attribute:
18661 __attribute__ (( attribute-list [opt] ))
18662
18663 The return value is as for cp_parser_attribute_list. */
18664
18665 static tree
18666 cp_parser_attributes_opt (cp_parser* parser)
18667 {
18668 tree attributes = NULL_TREE;
18669
18670 while (true)
18671 {
18672 cp_token *token;
18673 tree attribute_list;
18674
18675 /* Peek at the next token. */
18676 token = cp_lexer_peek_token (parser->lexer);
18677 /* If it's not `__attribute__', then we're done. */
18678 if (token->keyword != RID_ATTRIBUTE)
18679 break;
18680
18681 /* Consume the `__attribute__' keyword. */
18682 cp_lexer_consume_token (parser->lexer);
18683 /* Look for the two `(' tokens. */
18684 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18685 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18686
18687 /* Peek at the next token. */
18688 token = cp_lexer_peek_token (parser->lexer);
18689 if (token->type != CPP_CLOSE_PAREN)
18690 /* Parse the attribute-list. */
18691 attribute_list = cp_parser_attribute_list (parser);
18692 else
18693 /* If the next token is a `)', then there is no attribute
18694 list. */
18695 attribute_list = NULL;
18696
18697 /* Look for the two `)' tokens. */
18698 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18699 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18700
18701 /* Add these new attributes to the list. */
18702 attributes = chainon (attributes, attribute_list);
18703 }
18704
18705 return attributes;
18706 }
18707
18708 /* Parse an attribute-list.
18709
18710 attribute-list:
18711 attribute
18712 attribute-list , attribute
18713
18714 attribute:
18715 identifier
18716 identifier ( identifier )
18717 identifier ( identifier , expression-list )
18718 identifier ( expression-list )
18719
18720 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18721 to an attribute. The TREE_PURPOSE of each node is the identifier
18722 indicating which attribute is in use. The TREE_VALUE represents
18723 the arguments, if any. */
18724
18725 static tree
18726 cp_parser_attribute_list (cp_parser* parser)
18727 {
18728 tree attribute_list = NULL_TREE;
18729 bool save_translate_strings_p = parser->translate_strings_p;
18730
18731 parser->translate_strings_p = false;
18732 while (true)
18733 {
18734 cp_token *token;
18735 tree identifier;
18736 tree attribute;
18737
18738 /* Look for the identifier. We also allow keywords here; for
18739 example `__attribute__ ((const))' is legal. */
18740 token = cp_lexer_peek_token (parser->lexer);
18741 if (token->type == CPP_NAME
18742 || token->type == CPP_KEYWORD)
18743 {
18744 tree arguments = NULL_TREE;
18745
18746 /* Consume the token. */
18747 token = cp_lexer_consume_token (parser->lexer);
18748
18749 /* Save away the identifier that indicates which attribute
18750 this is. */
18751 identifier = (token->type == CPP_KEYWORD)
18752 /* For keywords, use the canonical spelling, not the
18753 parsed identifier. */
18754 ? ridpointers[(int) token->keyword]
18755 : token->u.value;
18756
18757 attribute = build_tree_list (identifier, NULL_TREE);
18758
18759 /* Peek at the next token. */
18760 token = cp_lexer_peek_token (parser->lexer);
18761 /* If it's an `(', then parse the attribute arguments. */
18762 if (token->type == CPP_OPEN_PAREN)
18763 {
18764 VEC(tree,gc) *vec;
18765 int attr_flag = (attribute_takes_identifier_p (identifier)
18766 ? id_attr : normal_attr);
18767 vec = cp_parser_parenthesized_expression_list
18768 (parser, attr_flag, /*cast_p=*/false,
18769 /*allow_expansion_p=*/false,
18770 /*non_constant_p=*/NULL);
18771 if (vec == NULL)
18772 arguments = error_mark_node;
18773 else
18774 {
18775 arguments = build_tree_list_vec (vec);
18776 release_tree_vector (vec);
18777 }
18778 /* Save the arguments away. */
18779 TREE_VALUE (attribute) = arguments;
18780 }
18781
18782 if (arguments != error_mark_node)
18783 {
18784 /* Add this attribute to the list. */
18785 TREE_CHAIN (attribute) = attribute_list;
18786 attribute_list = attribute;
18787 }
18788
18789 token = cp_lexer_peek_token (parser->lexer);
18790 }
18791 /* Now, look for more attributes. If the next token isn't a
18792 `,', we're done. */
18793 if (token->type != CPP_COMMA)
18794 break;
18795
18796 /* Consume the comma and keep going. */
18797 cp_lexer_consume_token (parser->lexer);
18798 }
18799 parser->translate_strings_p = save_translate_strings_p;
18800
18801 /* We built up the list in reverse order. */
18802 return nreverse (attribute_list);
18803 }
18804
18805 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18806 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18807 current value of the PEDANTIC flag, regardless of whether or not
18808 the `__extension__' keyword is present. The caller is responsible
18809 for restoring the value of the PEDANTIC flag. */
18810
18811 static bool
18812 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18813 {
18814 /* Save the old value of the PEDANTIC flag. */
18815 *saved_pedantic = pedantic;
18816
18817 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18818 {
18819 /* Consume the `__extension__' token. */
18820 cp_lexer_consume_token (parser->lexer);
18821 /* We're not being pedantic while the `__extension__' keyword is
18822 in effect. */
18823 pedantic = 0;
18824
18825 return true;
18826 }
18827
18828 return false;
18829 }
18830
18831 /* Parse a label declaration.
18832
18833 label-declaration:
18834 __label__ label-declarator-seq ;
18835
18836 label-declarator-seq:
18837 identifier , label-declarator-seq
18838 identifier */
18839
18840 static void
18841 cp_parser_label_declaration (cp_parser* parser)
18842 {
18843 /* Look for the `__label__' keyword. */
18844 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18845
18846 while (true)
18847 {
18848 tree identifier;
18849
18850 /* Look for an identifier. */
18851 identifier = cp_parser_identifier (parser);
18852 /* If we failed, stop. */
18853 if (identifier == error_mark_node)
18854 break;
18855 /* Declare it as a label. */
18856 finish_label_decl (identifier);
18857 /* If the next token is a `;', stop. */
18858 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18859 break;
18860 /* Look for the `,' separating the label declarations. */
18861 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18862 }
18863
18864 /* Look for the final `;'. */
18865 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18866 }
18867
18868 /* Support Functions */
18869
18870 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18871 NAME should have one of the representations used for an
18872 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18873 is returned. If PARSER->SCOPE is a dependent type, then a
18874 SCOPE_REF is returned.
18875
18876 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18877 returned; the name was already resolved when the TEMPLATE_ID_EXPR
18878 was formed. Abstractly, such entities should not be passed to this
18879 function, because they do not need to be looked up, but it is
18880 simpler to check for this special case here, rather than at the
18881 call-sites.
18882
18883 In cases not explicitly covered above, this function returns a
18884 DECL, OVERLOAD, or baselink representing the result of the lookup.
18885 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18886 is returned.
18887
18888 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18889 (e.g., "struct") that was used. In that case bindings that do not
18890 refer to types are ignored.
18891
18892 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18893 ignored.
18894
18895 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18896 are ignored.
18897
18898 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18899 types.
18900
18901 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18902 TREE_LIST of candidates if name-lookup results in an ambiguity, and
18903 NULL_TREE otherwise. */
18904
18905 static tree
18906 cp_parser_lookup_name (cp_parser *parser, tree name,
18907 enum tag_types tag_type,
18908 bool is_template,
18909 bool is_namespace,
18910 bool check_dependency,
18911 tree *ambiguous_decls,
18912 location_t name_location)
18913 {
18914 int flags = 0;
18915 tree decl;
18916 tree object_type = parser->context->object_type;
18917
18918 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18919 flags |= LOOKUP_COMPLAIN;
18920
18921 /* Assume that the lookup will be unambiguous. */
18922 if (ambiguous_decls)
18923 *ambiguous_decls = NULL_TREE;
18924
18925 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
18926 no longer valid. Note that if we are parsing tentatively, and
18927 the parse fails, OBJECT_TYPE will be automatically restored. */
18928 parser->context->object_type = NULL_TREE;
18929
18930 if (name == error_mark_node)
18931 return error_mark_node;
18932
18933 /* A template-id has already been resolved; there is no lookup to
18934 do. */
18935 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
18936 return name;
18937 if (BASELINK_P (name))
18938 {
18939 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
18940 == TEMPLATE_ID_EXPR);
18941 return name;
18942 }
18943
18944 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
18945 it should already have been checked to make sure that the name
18946 used matches the type being destroyed. */
18947 if (TREE_CODE (name) == BIT_NOT_EXPR)
18948 {
18949 tree type;
18950
18951 /* Figure out to which type this destructor applies. */
18952 if (parser->scope)
18953 type = parser->scope;
18954 else if (object_type)
18955 type = object_type;
18956 else
18957 type = current_class_type;
18958 /* If that's not a class type, there is no destructor. */
18959 if (!type || !CLASS_TYPE_P (type))
18960 return error_mark_node;
18961 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
18962 lazily_declare_fn (sfk_destructor, type);
18963 if (!CLASSTYPE_DESTRUCTORS (type))
18964 return error_mark_node;
18965 /* If it was a class type, return the destructor. */
18966 return CLASSTYPE_DESTRUCTORS (type);
18967 }
18968
18969 /* By this point, the NAME should be an ordinary identifier. If
18970 the id-expression was a qualified name, the qualifying scope is
18971 stored in PARSER->SCOPE at this point. */
18972 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
18973
18974 /* Perform the lookup. */
18975 if (parser->scope)
18976 {
18977 bool dependent_p;
18978
18979 if (parser->scope == error_mark_node)
18980 return error_mark_node;
18981
18982 /* If the SCOPE is dependent, the lookup must be deferred until
18983 the template is instantiated -- unless we are explicitly
18984 looking up names in uninstantiated templates. Even then, we
18985 cannot look up the name if the scope is not a class type; it
18986 might, for example, be a template type parameter. */
18987 dependent_p = (TYPE_P (parser->scope)
18988 && dependent_scope_p (parser->scope));
18989 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
18990 && dependent_p)
18991 /* Defer lookup. */
18992 decl = error_mark_node;
18993 else
18994 {
18995 tree pushed_scope = NULL_TREE;
18996
18997 /* If PARSER->SCOPE is a dependent type, then it must be a
18998 class type, and we must not be checking dependencies;
18999 otherwise, we would have processed this lookup above. So
19000 that PARSER->SCOPE is not considered a dependent base by
19001 lookup_member, we must enter the scope here. */
19002 if (dependent_p)
19003 pushed_scope = push_scope (parser->scope);
19004
19005 /* If the PARSER->SCOPE is a template specialization, it
19006 may be instantiated during name lookup. In that case,
19007 errors may be issued. Even if we rollback the current
19008 tentative parse, those errors are valid. */
19009 decl = lookup_qualified_name (parser->scope, name,
19010 tag_type != none_type,
19011 /*complain=*/true);
19012
19013 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19014 lookup result and the nested-name-specifier nominates a class C:
19015 * if the name specified after the nested-name-specifier, when
19016 looked up in C, is the injected-class-name of C (Clause 9), or
19017 * if the name specified after the nested-name-specifier is the
19018 same as the identifier or the simple-template-id's template-
19019 name in the last component of the nested-name-specifier,
19020 the name is instead considered to name the constructor of
19021 class C. [ Note: for example, the constructor is not an
19022 acceptable lookup result in an elaborated-type-specifier so
19023 the constructor would not be used in place of the
19024 injected-class-name. --end note ] Such a constructor name
19025 shall be used only in the declarator-id of a declaration that
19026 names a constructor or in a using-declaration. */
19027 if (tag_type == none_type
19028 && DECL_SELF_REFERENCE_P (decl)
19029 && same_type_p (DECL_CONTEXT (decl), parser->scope))
19030 decl = lookup_qualified_name (parser->scope, ctor_identifier,
19031 tag_type != none_type,
19032 /*complain=*/true);
19033
19034 /* If we have a single function from a using decl, pull it out. */
19035 if (TREE_CODE (decl) == OVERLOAD
19036 && !really_overloaded_fn (decl))
19037 decl = OVL_FUNCTION (decl);
19038
19039 if (pushed_scope)
19040 pop_scope (pushed_scope);
19041 }
19042
19043 /* If the scope is a dependent type and either we deferred lookup or
19044 we did lookup but didn't find the name, rememeber the name. */
19045 if (decl == error_mark_node && TYPE_P (parser->scope)
19046 && dependent_type_p (parser->scope))
19047 {
19048 if (tag_type)
19049 {
19050 tree type;
19051
19052 /* The resolution to Core Issue 180 says that `struct
19053 A::B' should be considered a type-name, even if `A'
19054 is dependent. */
19055 type = make_typename_type (parser->scope, name, tag_type,
19056 /*complain=*/tf_error);
19057 decl = TYPE_NAME (type);
19058 }
19059 else if (is_template
19060 && (cp_parser_next_token_ends_template_argument_p (parser)
19061 || cp_lexer_next_token_is (parser->lexer,
19062 CPP_CLOSE_PAREN)))
19063 decl = make_unbound_class_template (parser->scope,
19064 name, NULL_TREE,
19065 /*complain=*/tf_error);
19066 else
19067 decl = build_qualified_name (/*type=*/NULL_TREE,
19068 parser->scope, name,
19069 is_template);
19070 }
19071 parser->qualifying_scope = parser->scope;
19072 parser->object_scope = NULL_TREE;
19073 }
19074 else if (object_type)
19075 {
19076 tree object_decl = NULL_TREE;
19077 /* Look up the name in the scope of the OBJECT_TYPE, unless the
19078 OBJECT_TYPE is not a class. */
19079 if (CLASS_TYPE_P (object_type))
19080 /* If the OBJECT_TYPE is a template specialization, it may
19081 be instantiated during name lookup. In that case, errors
19082 may be issued. Even if we rollback the current tentative
19083 parse, those errors are valid. */
19084 object_decl = lookup_member (object_type,
19085 name,
19086 /*protect=*/0,
19087 tag_type != none_type);
19088 /* Look it up in the enclosing context, too. */
19089 decl = lookup_name_real (name, tag_type != none_type,
19090 /*nonclass=*/0,
19091 /*block_p=*/true, is_namespace, flags);
19092 parser->object_scope = object_type;
19093 parser->qualifying_scope = NULL_TREE;
19094 if (object_decl)
19095 decl = object_decl;
19096 }
19097 else
19098 {
19099 decl = lookup_name_real (name, tag_type != none_type,
19100 /*nonclass=*/0,
19101 /*block_p=*/true, is_namespace, flags);
19102 parser->qualifying_scope = NULL_TREE;
19103 parser->object_scope = NULL_TREE;
19104 }
19105
19106 /* If the lookup failed, let our caller know. */
19107 if (!decl || decl == error_mark_node)
19108 return error_mark_node;
19109
19110 /* Pull out the template from an injected-class-name (or multiple). */
19111 if (is_template)
19112 decl = maybe_get_template_decl_from_type_decl (decl);
19113
19114 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
19115 if (TREE_CODE (decl) == TREE_LIST)
19116 {
19117 if (ambiguous_decls)
19118 *ambiguous_decls = decl;
19119 /* The error message we have to print is too complicated for
19120 cp_parser_error, so we incorporate its actions directly. */
19121 if (!cp_parser_simulate_error (parser))
19122 {
19123 error_at (name_location, "reference to %qD is ambiguous",
19124 name);
19125 print_candidates (decl);
19126 }
19127 return error_mark_node;
19128 }
19129
19130 gcc_assert (DECL_P (decl)
19131 || TREE_CODE (decl) == OVERLOAD
19132 || TREE_CODE (decl) == SCOPE_REF
19133 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19134 || BASELINK_P (decl));
19135
19136 /* If we have resolved the name of a member declaration, check to
19137 see if the declaration is accessible. When the name resolves to
19138 set of overloaded functions, accessibility is checked when
19139 overload resolution is done.
19140
19141 During an explicit instantiation, access is not checked at all,
19142 as per [temp.explicit]. */
19143 if (DECL_P (decl))
19144 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19145
19146 return decl;
19147 }
19148
19149 /* Like cp_parser_lookup_name, but for use in the typical case where
19150 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19151 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
19152
19153 static tree
19154 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19155 {
19156 return cp_parser_lookup_name (parser, name,
19157 none_type,
19158 /*is_template=*/false,
19159 /*is_namespace=*/false,
19160 /*check_dependency=*/true,
19161 /*ambiguous_decls=*/NULL,
19162 location);
19163 }
19164
19165 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19166 the current context, return the TYPE_DECL. If TAG_NAME_P is
19167 true, the DECL indicates the class being defined in a class-head,
19168 or declared in an elaborated-type-specifier.
19169
19170 Otherwise, return DECL. */
19171
19172 static tree
19173 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19174 {
19175 /* If the TEMPLATE_DECL is being declared as part of a class-head,
19176 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19177
19178 struct A {
19179 template <typename T> struct B;
19180 };
19181
19182 template <typename T> struct A::B {};
19183
19184 Similarly, in an elaborated-type-specifier:
19185
19186 namespace N { struct X{}; }
19187
19188 struct A {
19189 template <typename T> friend struct N::X;
19190 };
19191
19192 However, if the DECL refers to a class type, and we are in
19193 the scope of the class, then the name lookup automatically
19194 finds the TYPE_DECL created by build_self_reference rather
19195 than a TEMPLATE_DECL. For example, in:
19196
19197 template <class T> struct S {
19198 S s;
19199 };
19200
19201 there is no need to handle such case. */
19202
19203 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19204 return DECL_TEMPLATE_RESULT (decl);
19205
19206 return decl;
19207 }
19208
19209 /* If too many, or too few, template-parameter lists apply to the
19210 declarator, issue an error message. Returns TRUE if all went well,
19211 and FALSE otherwise. */
19212
19213 static bool
19214 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19215 cp_declarator *declarator,
19216 location_t declarator_location)
19217 {
19218 unsigned num_templates;
19219
19220 /* We haven't seen any classes that involve template parameters yet. */
19221 num_templates = 0;
19222
19223 switch (declarator->kind)
19224 {
19225 case cdk_id:
19226 if (declarator->u.id.qualifying_scope)
19227 {
19228 tree scope;
19229
19230 scope = declarator->u.id.qualifying_scope;
19231
19232 while (scope && CLASS_TYPE_P (scope))
19233 {
19234 /* You're supposed to have one `template <...>'
19235 for every template class, but you don't need one
19236 for a full specialization. For example:
19237
19238 template <class T> struct S{};
19239 template <> struct S<int> { void f(); };
19240 void S<int>::f () {}
19241
19242 is correct; there shouldn't be a `template <>' for
19243 the definition of `S<int>::f'. */
19244 if (!CLASSTYPE_TEMPLATE_INFO (scope))
19245 /* If SCOPE does not have template information of any
19246 kind, then it is not a template, nor is it nested
19247 within a template. */
19248 break;
19249 if (explicit_class_specialization_p (scope))
19250 break;
19251 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19252 ++num_templates;
19253
19254 scope = TYPE_CONTEXT (scope);
19255 }
19256 }
19257 else if (TREE_CODE (declarator->u.id.unqualified_name)
19258 == TEMPLATE_ID_EXPR)
19259 /* If the DECLARATOR has the form `X<y>' then it uses one
19260 additional level of template parameters. */
19261 ++num_templates;
19262
19263 return cp_parser_check_template_parameters
19264 (parser, num_templates, declarator_location, declarator);
19265
19266
19267 case cdk_function:
19268 case cdk_array:
19269 case cdk_pointer:
19270 case cdk_reference:
19271 case cdk_ptrmem:
19272 return (cp_parser_check_declarator_template_parameters
19273 (parser, declarator->declarator, declarator_location));
19274
19275 case cdk_error:
19276 return true;
19277
19278 default:
19279 gcc_unreachable ();
19280 }
19281 return false;
19282 }
19283
19284 /* NUM_TEMPLATES were used in the current declaration. If that is
19285 invalid, return FALSE and issue an error messages. Otherwise,
19286 return TRUE. If DECLARATOR is non-NULL, then we are checking a
19287 declarator and we can print more accurate diagnostics. */
19288
19289 static bool
19290 cp_parser_check_template_parameters (cp_parser* parser,
19291 unsigned num_templates,
19292 location_t location,
19293 cp_declarator *declarator)
19294 {
19295 /* If there are the same number of template classes and parameter
19296 lists, that's OK. */
19297 if (parser->num_template_parameter_lists == num_templates)
19298 return true;
19299 /* If there are more, but only one more, then we are referring to a
19300 member template. That's OK too. */
19301 if (parser->num_template_parameter_lists == num_templates + 1)
19302 return true;
19303 /* If there are more template classes than parameter lists, we have
19304 something like:
19305
19306 template <class T> void S<T>::R<T>::f (); */
19307 if (parser->num_template_parameter_lists < num_templates)
19308 {
19309 if (declarator && !current_function_decl)
19310 error_at (location, "specializing member %<%T::%E%> "
19311 "requires %<template<>%> syntax",
19312 declarator->u.id.qualifying_scope,
19313 declarator->u.id.unqualified_name);
19314 else if (declarator)
19315 error_at (location, "invalid declaration of %<%T::%E%>",
19316 declarator->u.id.qualifying_scope,
19317 declarator->u.id.unqualified_name);
19318 else
19319 error_at (location, "too few template-parameter-lists");
19320 return false;
19321 }
19322 /* Otherwise, there are too many template parameter lists. We have
19323 something like:
19324
19325 template <class T> template <class U> void S::f(); */
19326 error_at (location, "too many template-parameter-lists");
19327 return false;
19328 }
19329
19330 /* Parse an optional `::' token indicating that the following name is
19331 from the global namespace. If so, PARSER->SCOPE is set to the
19332 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19333 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19334 Returns the new value of PARSER->SCOPE, if the `::' token is
19335 present, and NULL_TREE otherwise. */
19336
19337 static tree
19338 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19339 {
19340 cp_token *token;
19341
19342 /* Peek at the next token. */
19343 token = cp_lexer_peek_token (parser->lexer);
19344 /* If we're looking at a `::' token then we're starting from the
19345 global namespace, not our current location. */
19346 if (token->type == CPP_SCOPE)
19347 {
19348 /* Consume the `::' token. */
19349 cp_lexer_consume_token (parser->lexer);
19350 /* Set the SCOPE so that we know where to start the lookup. */
19351 parser->scope = global_namespace;
19352 parser->qualifying_scope = global_namespace;
19353 parser->object_scope = NULL_TREE;
19354
19355 return parser->scope;
19356 }
19357 else if (!current_scope_valid_p)
19358 {
19359 parser->scope = NULL_TREE;
19360 parser->qualifying_scope = NULL_TREE;
19361 parser->object_scope = NULL_TREE;
19362 }
19363
19364 return NULL_TREE;
19365 }
19366
19367 /* Returns TRUE if the upcoming token sequence is the start of a
19368 constructor declarator. If FRIEND_P is true, the declarator is
19369 preceded by the `friend' specifier. */
19370
19371 static bool
19372 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19373 {
19374 bool constructor_p;
19375 tree nested_name_specifier;
19376 cp_token *next_token;
19377
19378 /* The common case is that this is not a constructor declarator, so
19379 try to avoid doing lots of work if at all possible. It's not
19380 valid declare a constructor at function scope. */
19381 if (parser->in_function_body)
19382 return false;
19383 /* And only certain tokens can begin a constructor declarator. */
19384 next_token = cp_lexer_peek_token (parser->lexer);
19385 if (next_token->type != CPP_NAME
19386 && next_token->type != CPP_SCOPE
19387 && next_token->type != CPP_NESTED_NAME_SPECIFIER
19388 && next_token->type != CPP_TEMPLATE_ID)
19389 return false;
19390
19391 /* Parse tentatively; we are going to roll back all of the tokens
19392 consumed here. */
19393 cp_parser_parse_tentatively (parser);
19394 /* Assume that we are looking at a constructor declarator. */
19395 constructor_p = true;
19396
19397 /* Look for the optional `::' operator. */
19398 cp_parser_global_scope_opt (parser,
19399 /*current_scope_valid_p=*/false);
19400 /* Look for the nested-name-specifier. */
19401 nested_name_specifier
19402 = (cp_parser_nested_name_specifier_opt (parser,
19403 /*typename_keyword_p=*/false,
19404 /*check_dependency_p=*/false,
19405 /*type_p=*/false,
19406 /*is_declaration=*/false));
19407 /* Outside of a class-specifier, there must be a
19408 nested-name-specifier. */
19409 if (!nested_name_specifier &&
19410 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19411 || friend_p))
19412 constructor_p = false;
19413 else if (nested_name_specifier == error_mark_node)
19414 constructor_p = false;
19415
19416 /* If we have a class scope, this is easy; DR 147 says that S::S always
19417 names the constructor, and no other qualified name could. */
19418 if (constructor_p && nested_name_specifier
19419 && TYPE_P (nested_name_specifier))
19420 {
19421 tree id = cp_parser_unqualified_id (parser,
19422 /*template_keyword_p=*/false,
19423 /*check_dependency_p=*/false,
19424 /*declarator_p=*/true,
19425 /*optional_p=*/false);
19426 if (is_overloaded_fn (id))
19427 id = DECL_NAME (get_first_fn (id));
19428 if (!constructor_name_p (id, nested_name_specifier))
19429 constructor_p = false;
19430 }
19431 /* If we still think that this might be a constructor-declarator,
19432 look for a class-name. */
19433 else if (constructor_p)
19434 {
19435 /* If we have:
19436
19437 template <typename T> struct S {
19438 S();
19439 };
19440
19441 we must recognize that the nested `S' names a class. */
19442 tree type_decl;
19443 type_decl = cp_parser_class_name (parser,
19444 /*typename_keyword_p=*/false,
19445 /*template_keyword_p=*/false,
19446 none_type,
19447 /*check_dependency_p=*/false,
19448 /*class_head_p=*/false,
19449 /*is_declaration=*/false);
19450 /* If there was no class-name, then this is not a constructor. */
19451 constructor_p = !cp_parser_error_occurred (parser);
19452
19453 /* If we're still considering a constructor, we have to see a `(',
19454 to begin the parameter-declaration-clause, followed by either a
19455 `)', an `...', or a decl-specifier. We need to check for a
19456 type-specifier to avoid being fooled into thinking that:
19457
19458 S (f) (int);
19459
19460 is a constructor. (It is actually a function named `f' that
19461 takes one parameter (of type `int') and returns a value of type
19462 `S'. */
19463 if (constructor_p
19464 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19465 constructor_p = false;
19466
19467 if (constructor_p
19468 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19469 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19470 /* A parameter declaration begins with a decl-specifier,
19471 which is either the "attribute" keyword, a storage class
19472 specifier, or (usually) a type-specifier. */
19473 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19474 {
19475 tree type;
19476 tree pushed_scope = NULL_TREE;
19477 unsigned saved_num_template_parameter_lists;
19478
19479 /* Names appearing in the type-specifier should be looked up
19480 in the scope of the class. */
19481 if (current_class_type)
19482 type = NULL_TREE;
19483 else
19484 {
19485 type = TREE_TYPE (type_decl);
19486 if (TREE_CODE (type) == TYPENAME_TYPE)
19487 {
19488 type = resolve_typename_type (type,
19489 /*only_current_p=*/false);
19490 if (TREE_CODE (type) == TYPENAME_TYPE)
19491 {
19492 cp_parser_abort_tentative_parse (parser);
19493 return false;
19494 }
19495 }
19496 pushed_scope = push_scope (type);
19497 }
19498
19499 /* Inside the constructor parameter list, surrounding
19500 template-parameter-lists do not apply. */
19501 saved_num_template_parameter_lists
19502 = parser->num_template_parameter_lists;
19503 parser->num_template_parameter_lists = 0;
19504
19505 /* Look for the type-specifier. */
19506 cp_parser_type_specifier (parser,
19507 CP_PARSER_FLAGS_NONE,
19508 /*decl_specs=*/NULL,
19509 /*is_declarator=*/true,
19510 /*declares_class_or_enum=*/NULL,
19511 /*is_cv_qualifier=*/NULL);
19512
19513 parser->num_template_parameter_lists
19514 = saved_num_template_parameter_lists;
19515
19516 /* Leave the scope of the class. */
19517 if (pushed_scope)
19518 pop_scope (pushed_scope);
19519
19520 constructor_p = !cp_parser_error_occurred (parser);
19521 }
19522 }
19523
19524 /* We did not really want to consume any tokens. */
19525 cp_parser_abort_tentative_parse (parser);
19526
19527 return constructor_p;
19528 }
19529
19530 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19531 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19532 they must be performed once we are in the scope of the function.
19533
19534 Returns the function defined. */
19535
19536 static tree
19537 cp_parser_function_definition_from_specifiers_and_declarator
19538 (cp_parser* parser,
19539 cp_decl_specifier_seq *decl_specifiers,
19540 tree attributes,
19541 const cp_declarator *declarator)
19542 {
19543 tree fn;
19544 bool success_p;
19545
19546 /* Begin the function-definition. */
19547 success_p = start_function (decl_specifiers, declarator, attributes);
19548
19549 /* The things we're about to see are not directly qualified by any
19550 template headers we've seen thus far. */
19551 reset_specialization ();
19552
19553 /* If there were names looked up in the decl-specifier-seq that we
19554 did not check, check them now. We must wait until we are in the
19555 scope of the function to perform the checks, since the function
19556 might be a friend. */
19557 perform_deferred_access_checks ();
19558
19559 if (!success_p)
19560 {
19561 /* Skip the entire function. */
19562 cp_parser_skip_to_end_of_block_or_statement (parser);
19563 fn = error_mark_node;
19564 }
19565 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19566 {
19567 /* Seen already, skip it. An error message has already been output. */
19568 cp_parser_skip_to_end_of_block_or_statement (parser);
19569 fn = current_function_decl;
19570 current_function_decl = NULL_TREE;
19571 /* If this is a function from a class, pop the nested class. */
19572 if (current_class_name)
19573 pop_nested_class ();
19574 }
19575 else
19576 fn = cp_parser_function_definition_after_declarator (parser,
19577 /*inline_p=*/false);
19578
19579 return fn;
19580 }
19581
19582 /* Parse the part of a function-definition that follows the
19583 declarator. INLINE_P is TRUE iff this function is an inline
19584 function defined within a class-specifier.
19585
19586 Returns the function defined. */
19587
19588 static tree
19589 cp_parser_function_definition_after_declarator (cp_parser* parser,
19590 bool inline_p)
19591 {
19592 tree fn;
19593 bool ctor_initializer_p = false;
19594 bool saved_in_unbraced_linkage_specification_p;
19595 bool saved_in_function_body;
19596 unsigned saved_num_template_parameter_lists;
19597 cp_token *token;
19598
19599 saved_in_function_body = parser->in_function_body;
19600 parser->in_function_body = true;
19601 /* If the next token is `return', then the code may be trying to
19602 make use of the "named return value" extension that G++ used to
19603 support. */
19604 token = cp_lexer_peek_token (parser->lexer);
19605 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19606 {
19607 /* Consume the `return' keyword. */
19608 cp_lexer_consume_token (parser->lexer);
19609 /* Look for the identifier that indicates what value is to be
19610 returned. */
19611 cp_parser_identifier (parser);
19612 /* Issue an error message. */
19613 error_at (token->location,
19614 "named return values are no longer supported");
19615 /* Skip tokens until we reach the start of the function body. */
19616 while (true)
19617 {
19618 cp_token *token = cp_lexer_peek_token (parser->lexer);
19619 if (token->type == CPP_OPEN_BRACE
19620 || token->type == CPP_EOF
19621 || token->type == CPP_PRAGMA_EOL)
19622 break;
19623 cp_lexer_consume_token (parser->lexer);
19624 }
19625 }
19626 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19627 anything declared inside `f'. */
19628 saved_in_unbraced_linkage_specification_p
19629 = parser->in_unbraced_linkage_specification_p;
19630 parser->in_unbraced_linkage_specification_p = false;
19631 /* Inside the function, surrounding template-parameter-lists do not
19632 apply. */
19633 saved_num_template_parameter_lists
19634 = parser->num_template_parameter_lists;
19635 parser->num_template_parameter_lists = 0;
19636
19637 start_lambda_scope (current_function_decl);
19638
19639 /* If the next token is `try', then we are looking at a
19640 function-try-block. */
19641 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19642 ctor_initializer_p = cp_parser_function_try_block (parser);
19643 /* A function-try-block includes the function-body, so we only do
19644 this next part if we're not processing a function-try-block. */
19645 else
19646 ctor_initializer_p
19647 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19648
19649 finish_lambda_scope ();
19650
19651 /* Finish the function. */
19652 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19653 (inline_p ? 2 : 0));
19654 /* Generate code for it, if necessary. */
19655 expand_or_defer_fn (fn);
19656 /* Restore the saved values. */
19657 parser->in_unbraced_linkage_specification_p
19658 = saved_in_unbraced_linkage_specification_p;
19659 parser->num_template_parameter_lists
19660 = saved_num_template_parameter_lists;
19661 parser->in_function_body = saved_in_function_body;
19662
19663 return fn;
19664 }
19665
19666 /* Parse a template-declaration, assuming that the `export' (and
19667 `extern') keywords, if present, has already been scanned. MEMBER_P
19668 is as for cp_parser_template_declaration. */
19669
19670 static void
19671 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19672 {
19673 tree decl = NULL_TREE;
19674 VEC (deferred_access_check,gc) *checks;
19675 tree parameter_list;
19676 bool friend_p = false;
19677 bool need_lang_pop;
19678 cp_token *token;
19679
19680 /* Look for the `template' keyword. */
19681 token = cp_lexer_peek_token (parser->lexer);
19682 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19683 return;
19684
19685 /* And the `<'. */
19686 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19687 return;
19688 if (at_class_scope_p () && current_function_decl)
19689 {
19690 /* 14.5.2.2 [temp.mem]
19691
19692 A local class shall not have member templates. */
19693 error_at (token->location,
19694 "invalid declaration of member template in local class");
19695 cp_parser_skip_to_end_of_block_or_statement (parser);
19696 return;
19697 }
19698 /* [temp]
19699
19700 A template ... shall not have C linkage. */
19701 if (current_lang_name == lang_name_c)
19702 {
19703 error_at (token->location, "template with C linkage");
19704 /* Give it C++ linkage to avoid confusing other parts of the
19705 front end. */
19706 push_lang_context (lang_name_cplusplus);
19707 need_lang_pop = true;
19708 }
19709 else
19710 need_lang_pop = false;
19711
19712 /* We cannot perform access checks on the template parameter
19713 declarations until we know what is being declared, just as we
19714 cannot check the decl-specifier list. */
19715 push_deferring_access_checks (dk_deferred);
19716
19717 /* If the next token is `>', then we have an invalid
19718 specialization. Rather than complain about an invalid template
19719 parameter, issue an error message here. */
19720 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19721 {
19722 cp_parser_error (parser, "invalid explicit specialization");
19723 begin_specialization ();
19724 parameter_list = NULL_TREE;
19725 }
19726 else
19727 /* Parse the template parameters. */
19728 parameter_list = cp_parser_template_parameter_list (parser);
19729
19730 /* Get the deferred access checks from the parameter list. These
19731 will be checked once we know what is being declared, as for a
19732 member template the checks must be performed in the scope of the
19733 class containing the member. */
19734 checks = get_deferred_access_checks ();
19735
19736 /* Look for the `>'. */
19737 cp_parser_skip_to_end_of_template_parameter_list (parser);
19738 /* We just processed one more parameter list. */
19739 ++parser->num_template_parameter_lists;
19740 /* If the next token is `template', there are more template
19741 parameters. */
19742 if (cp_lexer_next_token_is_keyword (parser->lexer,
19743 RID_TEMPLATE))
19744 cp_parser_template_declaration_after_export (parser, member_p);
19745 else
19746 {
19747 /* There are no access checks when parsing a template, as we do not
19748 know if a specialization will be a friend. */
19749 push_deferring_access_checks (dk_no_check);
19750 token = cp_lexer_peek_token (parser->lexer);
19751 decl = cp_parser_single_declaration (parser,
19752 checks,
19753 member_p,
19754 /*explicit_specialization_p=*/false,
19755 &friend_p);
19756 pop_deferring_access_checks ();
19757
19758 /* If this is a member template declaration, let the front
19759 end know. */
19760 if (member_p && !friend_p && decl)
19761 {
19762 if (TREE_CODE (decl) == TYPE_DECL)
19763 cp_parser_check_access_in_redeclaration (decl, token->location);
19764
19765 decl = finish_member_template_decl (decl);
19766 }
19767 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19768 make_friend_class (current_class_type, TREE_TYPE (decl),
19769 /*complain=*/true);
19770 }
19771 /* We are done with the current parameter list. */
19772 --parser->num_template_parameter_lists;
19773
19774 pop_deferring_access_checks ();
19775
19776 /* Finish up. */
19777 finish_template_decl (parameter_list);
19778
19779 /* Register member declarations. */
19780 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19781 finish_member_declaration (decl);
19782 /* For the erroneous case of a template with C linkage, we pushed an
19783 implicit C++ linkage scope; exit that scope now. */
19784 if (need_lang_pop)
19785 pop_lang_context ();
19786 /* If DECL is a function template, we must return to parse it later.
19787 (Even though there is no definition, there might be default
19788 arguments that need handling.) */
19789 if (member_p && decl
19790 && (TREE_CODE (decl) == FUNCTION_DECL
19791 || DECL_FUNCTION_TEMPLATE_P (decl)))
19792 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19793 }
19794
19795 /* Perform the deferred access checks from a template-parameter-list.
19796 CHECKS is a TREE_LIST of access checks, as returned by
19797 get_deferred_access_checks. */
19798
19799 static void
19800 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19801 {
19802 ++processing_template_parmlist;
19803 perform_access_checks (checks);
19804 --processing_template_parmlist;
19805 }
19806
19807 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19808 `function-definition' sequence. MEMBER_P is true, this declaration
19809 appears in a class scope.
19810
19811 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19812 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19813
19814 static tree
19815 cp_parser_single_declaration (cp_parser* parser,
19816 VEC (deferred_access_check,gc)* checks,
19817 bool member_p,
19818 bool explicit_specialization_p,
19819 bool* friend_p)
19820 {
19821 int declares_class_or_enum;
19822 tree decl = NULL_TREE;
19823 cp_decl_specifier_seq decl_specifiers;
19824 bool function_definition_p = false;
19825 cp_token *decl_spec_token_start;
19826
19827 /* This function is only used when processing a template
19828 declaration. */
19829 gcc_assert (innermost_scope_kind () == sk_template_parms
19830 || innermost_scope_kind () == sk_template_spec);
19831
19832 /* Defer access checks until we know what is being declared. */
19833 push_deferring_access_checks (dk_deferred);
19834
19835 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19836 alternative. */
19837 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19838 cp_parser_decl_specifier_seq (parser,
19839 CP_PARSER_FLAGS_OPTIONAL,
19840 &decl_specifiers,
19841 &declares_class_or_enum);
19842 if (friend_p)
19843 *friend_p = cp_parser_friend_p (&decl_specifiers);
19844
19845 /* There are no template typedefs. */
19846 if (decl_specifiers.specs[(int) ds_typedef])
19847 {
19848 error_at (decl_spec_token_start->location,
19849 "template declaration of %<typedef%>");
19850 decl = error_mark_node;
19851 }
19852
19853 /* Gather up the access checks that occurred the
19854 decl-specifier-seq. */
19855 stop_deferring_access_checks ();
19856
19857 /* Check for the declaration of a template class. */
19858 if (declares_class_or_enum)
19859 {
19860 if (cp_parser_declares_only_class_p (parser))
19861 {
19862 decl = shadow_tag (&decl_specifiers);
19863
19864 /* In this case:
19865
19866 struct C {
19867 friend template <typename T> struct A<T>::B;
19868 };
19869
19870 A<T>::B will be represented by a TYPENAME_TYPE, and
19871 therefore not recognized by shadow_tag. */
19872 if (friend_p && *friend_p
19873 && !decl
19874 && decl_specifiers.type
19875 && TYPE_P (decl_specifiers.type))
19876 decl = decl_specifiers.type;
19877
19878 if (decl && decl != error_mark_node)
19879 decl = TYPE_NAME (decl);
19880 else
19881 decl = error_mark_node;
19882
19883 /* Perform access checks for template parameters. */
19884 cp_parser_perform_template_parameter_access_checks (checks);
19885 }
19886 }
19887
19888 /* Complain about missing 'typename' or other invalid type names. */
19889 if (!decl_specifiers.any_type_specifiers_p)
19890 cp_parser_parse_and_diagnose_invalid_type_name (parser);
19891
19892 /* If it's not a template class, try for a template function. If
19893 the next token is a `;', then this declaration does not declare
19894 anything. But, if there were errors in the decl-specifiers, then
19895 the error might well have come from an attempted class-specifier.
19896 In that case, there's no need to warn about a missing declarator. */
19897 if (!decl
19898 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
19899 || decl_specifiers.type != error_mark_node))
19900 {
19901 decl = cp_parser_init_declarator (parser,
19902 &decl_specifiers,
19903 checks,
19904 /*function_definition_allowed_p=*/true,
19905 member_p,
19906 declares_class_or_enum,
19907 &function_definition_p);
19908
19909 /* 7.1.1-1 [dcl.stc]
19910
19911 A storage-class-specifier shall not be specified in an explicit
19912 specialization... */
19913 if (decl
19914 && explicit_specialization_p
19915 && decl_specifiers.storage_class != sc_none)
19916 {
19917 error_at (decl_spec_token_start->location,
19918 "explicit template specialization cannot have a storage class");
19919 decl = error_mark_node;
19920 }
19921 }
19922
19923 pop_deferring_access_checks ();
19924
19925 /* Clear any current qualification; whatever comes next is the start
19926 of something new. */
19927 parser->scope = NULL_TREE;
19928 parser->qualifying_scope = NULL_TREE;
19929 parser->object_scope = NULL_TREE;
19930 /* Look for a trailing `;' after the declaration. */
19931 if (!function_definition_p
19932 && (decl == error_mark_node
19933 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
19934 cp_parser_skip_to_end_of_block_or_statement (parser);
19935
19936 return decl;
19937 }
19938
19939 /* Parse a cast-expression that is not the operand of a unary "&". */
19940
19941 static tree
19942 cp_parser_simple_cast_expression (cp_parser *parser)
19943 {
19944 return cp_parser_cast_expression (parser, /*address_p=*/false,
19945 /*cast_p=*/false, NULL);
19946 }
19947
19948 /* Parse a functional cast to TYPE. Returns an expression
19949 representing the cast. */
19950
19951 static tree
19952 cp_parser_functional_cast (cp_parser* parser, tree type)
19953 {
19954 VEC(tree,gc) *vec;
19955 tree expression_list;
19956 tree cast;
19957 bool nonconst_p;
19958
19959 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19960 {
19961 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19962 expression_list = cp_parser_braced_list (parser, &nonconst_p);
19963 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
19964 if (TREE_CODE (type) == TYPE_DECL)
19965 type = TREE_TYPE (type);
19966 return finish_compound_literal (type, expression_list);
19967 }
19968
19969
19970 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19971 /*cast_p=*/true,
19972 /*allow_expansion_p=*/true,
19973 /*non_constant_p=*/NULL);
19974 if (vec == NULL)
19975 expression_list = error_mark_node;
19976 else
19977 {
19978 expression_list = build_tree_list_vec (vec);
19979 release_tree_vector (vec);
19980 }
19981
19982 cast = build_functional_cast (type, expression_list,
19983 tf_warning_or_error);
19984 /* [expr.const]/1: In an integral constant expression "only type
19985 conversions to integral or enumeration type can be used". */
19986 if (TREE_CODE (type) == TYPE_DECL)
19987 type = TREE_TYPE (type);
19988 if (cast != error_mark_node
19989 && !cast_valid_in_integral_constant_expression_p (type)
19990 && cp_parser_non_integral_constant_expression (parser,
19991 NIC_CONSTRUCTOR))
19992 return error_mark_node;
19993 return cast;
19994 }
19995
19996 /* Save the tokens that make up the body of a member function defined
19997 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
19998 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
19999 specifiers applied to the declaration. Returns the FUNCTION_DECL
20000 for the member function. */
20001
20002 static tree
20003 cp_parser_save_member_function_body (cp_parser* parser,
20004 cp_decl_specifier_seq *decl_specifiers,
20005 cp_declarator *declarator,
20006 tree attributes)
20007 {
20008 cp_token *first;
20009 cp_token *last;
20010 tree fn;
20011
20012 /* Create the FUNCTION_DECL. */
20013 fn = grokmethod (decl_specifiers, declarator, attributes);
20014 /* If something went badly wrong, bail out now. */
20015 if (fn == error_mark_node)
20016 {
20017 /* If there's a function-body, skip it. */
20018 if (cp_parser_token_starts_function_definition_p
20019 (cp_lexer_peek_token (parser->lexer)))
20020 cp_parser_skip_to_end_of_block_or_statement (parser);
20021 return error_mark_node;
20022 }
20023
20024 /* Remember it, if there default args to post process. */
20025 cp_parser_save_default_args (parser, fn);
20026
20027 /* Save away the tokens that make up the body of the
20028 function. */
20029 first = parser->lexer->next_token;
20030 /* We can have braced-init-list mem-initializers before the fn body. */
20031 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20032 {
20033 cp_lexer_consume_token (parser->lexer);
20034 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20035 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20036 {
20037 /* cache_group will stop after an un-nested { } pair, too. */
20038 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20039 break;
20040
20041 /* variadic mem-inits have ... after the ')'. */
20042 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20043 cp_lexer_consume_token (parser->lexer);
20044 }
20045 }
20046 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20047 /* Handle function try blocks. */
20048 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20049 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20050 last = parser->lexer->next_token;
20051
20052 /* Save away the inline definition; we will process it when the
20053 class is complete. */
20054 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20055 DECL_PENDING_INLINE_P (fn) = 1;
20056
20057 /* We need to know that this was defined in the class, so that
20058 friend templates are handled correctly. */
20059 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20060
20061 /* Add FN to the queue of functions to be parsed later. */
20062 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20063
20064 return fn;
20065 }
20066
20067 /* Parse a template-argument-list, as well as the trailing ">" (but
20068 not the opening ">"). See cp_parser_template_argument_list for the
20069 return value. */
20070
20071 static tree
20072 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20073 {
20074 tree arguments;
20075 tree saved_scope;
20076 tree saved_qualifying_scope;
20077 tree saved_object_scope;
20078 bool saved_greater_than_is_operator_p;
20079 int saved_unevaluated_operand;
20080 int saved_inhibit_evaluation_warnings;
20081
20082 /* [temp.names]
20083
20084 When parsing a template-id, the first non-nested `>' is taken as
20085 the end of the template-argument-list rather than a greater-than
20086 operator. */
20087 saved_greater_than_is_operator_p
20088 = parser->greater_than_is_operator_p;
20089 parser->greater_than_is_operator_p = false;
20090 /* Parsing the argument list may modify SCOPE, so we save it
20091 here. */
20092 saved_scope = parser->scope;
20093 saved_qualifying_scope = parser->qualifying_scope;
20094 saved_object_scope = parser->object_scope;
20095 /* We need to evaluate the template arguments, even though this
20096 template-id may be nested within a "sizeof". */
20097 saved_unevaluated_operand = cp_unevaluated_operand;
20098 cp_unevaluated_operand = 0;
20099 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20100 c_inhibit_evaluation_warnings = 0;
20101 /* Parse the template-argument-list itself. */
20102 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20103 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20104 arguments = NULL_TREE;
20105 else
20106 arguments = cp_parser_template_argument_list (parser);
20107 /* Look for the `>' that ends the template-argument-list. If we find
20108 a '>>' instead, it's probably just a typo. */
20109 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20110 {
20111 if (cxx_dialect != cxx98)
20112 {
20113 /* In C++0x, a `>>' in a template argument list or cast
20114 expression is considered to be two separate `>'
20115 tokens. So, change the current token to a `>', but don't
20116 consume it: it will be consumed later when the outer
20117 template argument list (or cast expression) is parsed.
20118 Note that this replacement of `>' for `>>' is necessary
20119 even if we are parsing tentatively: in the tentative
20120 case, after calling
20121 cp_parser_enclosed_template_argument_list we will always
20122 throw away all of the template arguments and the first
20123 closing `>', either because the template argument list
20124 was erroneous or because we are replacing those tokens
20125 with a CPP_TEMPLATE_ID token. The second `>' (which will
20126 not have been thrown away) is needed either to close an
20127 outer template argument list or to complete a new-style
20128 cast. */
20129 cp_token *token = cp_lexer_peek_token (parser->lexer);
20130 token->type = CPP_GREATER;
20131 }
20132 else if (!saved_greater_than_is_operator_p)
20133 {
20134 /* If we're in a nested template argument list, the '>>' has
20135 to be a typo for '> >'. We emit the error message, but we
20136 continue parsing and we push a '>' as next token, so that
20137 the argument list will be parsed correctly. Note that the
20138 global source location is still on the token before the
20139 '>>', so we need to say explicitly where we want it. */
20140 cp_token *token = cp_lexer_peek_token (parser->lexer);
20141 error_at (token->location, "%<>>%> should be %<> >%> "
20142 "within a nested template argument list");
20143
20144 token->type = CPP_GREATER;
20145 }
20146 else
20147 {
20148 /* If this is not a nested template argument list, the '>>'
20149 is a typo for '>'. Emit an error message and continue.
20150 Same deal about the token location, but here we can get it
20151 right by consuming the '>>' before issuing the diagnostic. */
20152 cp_token *token = cp_lexer_consume_token (parser->lexer);
20153 error_at (token->location,
20154 "spurious %<>>%>, use %<>%> to terminate "
20155 "a template argument list");
20156 }
20157 }
20158 else
20159 cp_parser_skip_to_end_of_template_parameter_list (parser);
20160 /* The `>' token might be a greater-than operator again now. */
20161 parser->greater_than_is_operator_p
20162 = saved_greater_than_is_operator_p;
20163 /* Restore the SAVED_SCOPE. */
20164 parser->scope = saved_scope;
20165 parser->qualifying_scope = saved_qualifying_scope;
20166 parser->object_scope = saved_object_scope;
20167 cp_unevaluated_operand = saved_unevaluated_operand;
20168 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20169
20170 return arguments;
20171 }
20172
20173 /* MEMBER_FUNCTION is a member function, or a friend. If default
20174 arguments, or the body of the function have not yet been parsed,
20175 parse them now. */
20176
20177 static void
20178 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20179 {
20180 /* If this member is a template, get the underlying
20181 FUNCTION_DECL. */
20182 if (DECL_FUNCTION_TEMPLATE_P (member_function))
20183 member_function = DECL_TEMPLATE_RESULT (member_function);
20184
20185 /* There should not be any class definitions in progress at this
20186 point; the bodies of members are only parsed outside of all class
20187 definitions. */
20188 gcc_assert (parser->num_classes_being_defined == 0);
20189 /* While we're parsing the member functions we might encounter more
20190 classes. We want to handle them right away, but we don't want
20191 them getting mixed up with functions that are currently in the
20192 queue. */
20193 push_unparsed_function_queues (parser);
20194
20195 /* Make sure that any template parameters are in scope. */
20196 maybe_begin_member_template_processing (member_function);
20197
20198 /* If the body of the function has not yet been parsed, parse it
20199 now. */
20200 if (DECL_PENDING_INLINE_P (member_function))
20201 {
20202 tree function_scope;
20203 cp_token_cache *tokens;
20204
20205 /* The function is no longer pending; we are processing it. */
20206 tokens = DECL_PENDING_INLINE_INFO (member_function);
20207 DECL_PENDING_INLINE_INFO (member_function) = NULL;
20208 DECL_PENDING_INLINE_P (member_function) = 0;
20209
20210 /* If this is a local class, enter the scope of the containing
20211 function. */
20212 function_scope = current_function_decl;
20213 if (function_scope)
20214 push_function_context ();
20215
20216 /* Push the body of the function onto the lexer stack. */
20217 cp_parser_push_lexer_for_tokens (parser, tokens);
20218
20219 /* Let the front end know that we going to be defining this
20220 function. */
20221 start_preparsed_function (member_function, NULL_TREE,
20222 SF_PRE_PARSED | SF_INCLASS_INLINE);
20223
20224 /* Don't do access checking if it is a templated function. */
20225 if (processing_template_decl)
20226 push_deferring_access_checks (dk_no_check);
20227
20228 /* Now, parse the body of the function. */
20229 cp_parser_function_definition_after_declarator (parser,
20230 /*inline_p=*/true);
20231
20232 if (processing_template_decl)
20233 pop_deferring_access_checks ();
20234
20235 /* Leave the scope of the containing function. */
20236 if (function_scope)
20237 pop_function_context ();
20238 cp_parser_pop_lexer (parser);
20239 }
20240
20241 /* Remove any template parameters from the symbol table. */
20242 maybe_end_member_template_processing ();
20243
20244 /* Restore the queue. */
20245 pop_unparsed_function_queues (parser);
20246 }
20247
20248 /* If DECL contains any default args, remember it on the unparsed
20249 functions queue. */
20250
20251 static void
20252 cp_parser_save_default_args (cp_parser* parser, tree decl)
20253 {
20254 tree probe;
20255
20256 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20257 probe;
20258 probe = TREE_CHAIN (probe))
20259 if (TREE_PURPOSE (probe))
20260 {
20261 cp_default_arg_entry *entry
20262 = VEC_safe_push (cp_default_arg_entry, gc,
20263 unparsed_funs_with_default_args, NULL);
20264 entry->class_type = current_class_type;
20265 entry->decl = decl;
20266 break;
20267 }
20268 }
20269
20270 /* FN is a FUNCTION_DECL which may contains a parameter with an
20271 unparsed DEFAULT_ARG. Parse the default args now. This function
20272 assumes that the current scope is the scope in which the default
20273 argument should be processed. */
20274
20275 static void
20276 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20277 {
20278 bool saved_local_variables_forbidden_p;
20279 tree parm, parmdecl;
20280
20281 /* While we're parsing the default args, we might (due to the
20282 statement expression extension) encounter more classes. We want
20283 to handle them right away, but we don't want them getting mixed
20284 up with default args that are currently in the queue. */
20285 push_unparsed_function_queues (parser);
20286
20287 /* Local variable names (and the `this' keyword) may not appear
20288 in a default argument. */
20289 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20290 parser->local_variables_forbidden_p = true;
20291
20292 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20293 parmdecl = DECL_ARGUMENTS (fn);
20294 parm && parm != void_list_node;
20295 parm = TREE_CHAIN (parm),
20296 parmdecl = DECL_CHAIN (parmdecl))
20297 {
20298 cp_token_cache *tokens;
20299 tree default_arg = TREE_PURPOSE (parm);
20300 tree parsed_arg;
20301 VEC(tree,gc) *insts;
20302 tree copy;
20303 unsigned ix;
20304
20305 if (!default_arg)
20306 continue;
20307
20308 if (TREE_CODE (default_arg) != DEFAULT_ARG)
20309 /* This can happen for a friend declaration for a function
20310 already declared with default arguments. */
20311 continue;
20312
20313 /* Push the saved tokens for the default argument onto the parser's
20314 lexer stack. */
20315 tokens = DEFARG_TOKENS (default_arg);
20316 cp_parser_push_lexer_for_tokens (parser, tokens);
20317
20318 start_lambda_scope (parmdecl);
20319
20320 /* Parse the assignment-expression. */
20321 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20322 if (parsed_arg == error_mark_node)
20323 {
20324 cp_parser_pop_lexer (parser);
20325 continue;
20326 }
20327
20328 if (!processing_template_decl)
20329 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20330
20331 TREE_PURPOSE (parm) = parsed_arg;
20332
20333 /* Update any instantiations we've already created. */
20334 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20335 VEC_iterate (tree, insts, ix, copy); ix++)
20336 TREE_PURPOSE (copy) = parsed_arg;
20337
20338 finish_lambda_scope ();
20339
20340 /* If the token stream has not been completely used up, then
20341 there was extra junk after the end of the default
20342 argument. */
20343 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20344 cp_parser_error (parser, "expected %<,%>");
20345
20346 /* Revert to the main lexer. */
20347 cp_parser_pop_lexer (parser);
20348 }
20349
20350 /* Make sure no default arg is missing. */
20351 check_default_args (fn);
20352
20353 /* Restore the state of local_variables_forbidden_p. */
20354 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20355
20356 /* Restore the queue. */
20357 pop_unparsed_function_queues (parser);
20358 }
20359
20360 /* Parse the operand of `sizeof' (or a similar operator). Returns
20361 either a TYPE or an expression, depending on the form of the
20362 input. The KEYWORD indicates which kind of expression we have
20363 encountered. */
20364
20365 static tree
20366 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20367 {
20368 tree expr = NULL_TREE;
20369 const char *saved_message;
20370 char *tmp;
20371 bool saved_integral_constant_expression_p;
20372 bool saved_non_integral_constant_expression_p;
20373 bool pack_expansion_p = false;
20374
20375 /* Types cannot be defined in a `sizeof' expression. Save away the
20376 old message. */
20377 saved_message = parser->type_definition_forbidden_message;
20378 /* And create the new one. */
20379 tmp = concat ("types may not be defined in %<",
20380 IDENTIFIER_POINTER (ridpointers[keyword]),
20381 "%> expressions", NULL);
20382 parser->type_definition_forbidden_message = tmp;
20383
20384 /* The restrictions on constant-expressions do not apply inside
20385 sizeof expressions. */
20386 saved_integral_constant_expression_p
20387 = parser->integral_constant_expression_p;
20388 saved_non_integral_constant_expression_p
20389 = parser->non_integral_constant_expression_p;
20390 parser->integral_constant_expression_p = false;
20391
20392 /* If it's a `...', then we are computing the length of a parameter
20393 pack. */
20394 if (keyword == RID_SIZEOF
20395 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20396 {
20397 /* Consume the `...'. */
20398 cp_lexer_consume_token (parser->lexer);
20399 maybe_warn_variadic_templates ();
20400
20401 /* Note that this is an expansion. */
20402 pack_expansion_p = true;
20403 }
20404
20405 /* Do not actually evaluate the expression. */
20406 ++cp_unevaluated_operand;
20407 ++c_inhibit_evaluation_warnings;
20408 /* If it's a `(', then we might be looking at the type-id
20409 construction. */
20410 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20411 {
20412 tree type;
20413 bool saved_in_type_id_in_expr_p;
20414
20415 /* We can't be sure yet whether we're looking at a type-id or an
20416 expression. */
20417 cp_parser_parse_tentatively (parser);
20418 /* Consume the `('. */
20419 cp_lexer_consume_token (parser->lexer);
20420 /* Parse the type-id. */
20421 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20422 parser->in_type_id_in_expr_p = true;
20423 type = cp_parser_type_id (parser);
20424 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20425 /* Now, look for the trailing `)'. */
20426 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20427 /* If all went well, then we're done. */
20428 if (cp_parser_parse_definitely (parser))
20429 {
20430 cp_decl_specifier_seq decl_specs;
20431
20432 /* Build a trivial decl-specifier-seq. */
20433 clear_decl_specs (&decl_specs);
20434 decl_specs.type = type;
20435
20436 /* Call grokdeclarator to figure out what type this is. */
20437 expr = grokdeclarator (NULL,
20438 &decl_specs,
20439 TYPENAME,
20440 /*initialized=*/0,
20441 /*attrlist=*/NULL);
20442 }
20443 }
20444
20445 /* If the type-id production did not work out, then we must be
20446 looking at the unary-expression production. */
20447 if (!expr)
20448 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20449 /*cast_p=*/false, NULL);
20450
20451 if (pack_expansion_p)
20452 /* Build a pack expansion. */
20453 expr = make_pack_expansion (expr);
20454
20455 /* Go back to evaluating expressions. */
20456 --cp_unevaluated_operand;
20457 --c_inhibit_evaluation_warnings;
20458
20459 /* Free the message we created. */
20460 free (tmp);
20461 /* And restore the old one. */
20462 parser->type_definition_forbidden_message = saved_message;
20463 parser->integral_constant_expression_p
20464 = saved_integral_constant_expression_p;
20465 parser->non_integral_constant_expression_p
20466 = saved_non_integral_constant_expression_p;
20467
20468 return expr;
20469 }
20470
20471 /* If the current declaration has no declarator, return true. */
20472
20473 static bool
20474 cp_parser_declares_only_class_p (cp_parser *parser)
20475 {
20476 /* If the next token is a `;' or a `,' then there is no
20477 declarator. */
20478 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20479 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20480 }
20481
20482 /* Update the DECL_SPECS to reflect the storage class indicated by
20483 KEYWORD. */
20484
20485 static void
20486 cp_parser_set_storage_class (cp_parser *parser,
20487 cp_decl_specifier_seq *decl_specs,
20488 enum rid keyword,
20489 location_t location)
20490 {
20491 cp_storage_class storage_class;
20492
20493 if (parser->in_unbraced_linkage_specification_p)
20494 {
20495 error_at (location, "invalid use of %qD in linkage specification",
20496 ridpointers[keyword]);
20497 return;
20498 }
20499 else if (decl_specs->storage_class != sc_none)
20500 {
20501 decl_specs->conflicting_specifiers_p = true;
20502 return;
20503 }
20504
20505 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20506 && decl_specs->specs[(int) ds_thread])
20507 {
20508 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20509 decl_specs->specs[(int) ds_thread] = 0;
20510 }
20511
20512 switch (keyword)
20513 {
20514 case RID_AUTO:
20515 storage_class = sc_auto;
20516 break;
20517 case RID_REGISTER:
20518 storage_class = sc_register;
20519 break;
20520 case RID_STATIC:
20521 storage_class = sc_static;
20522 break;
20523 case RID_EXTERN:
20524 storage_class = sc_extern;
20525 break;
20526 case RID_MUTABLE:
20527 storage_class = sc_mutable;
20528 break;
20529 default:
20530 gcc_unreachable ();
20531 }
20532 decl_specs->storage_class = storage_class;
20533
20534 /* A storage class specifier cannot be applied alongside a typedef
20535 specifier. If there is a typedef specifier present then set
20536 conflicting_specifiers_p which will trigger an error later
20537 on in grokdeclarator. */
20538 if (decl_specs->specs[(int)ds_typedef])
20539 decl_specs->conflicting_specifiers_p = true;
20540 }
20541
20542 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20543 is true, the type is a user-defined type; otherwise it is a
20544 built-in type specified by a keyword. */
20545
20546 static void
20547 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20548 tree type_spec,
20549 location_t location,
20550 bool user_defined_p)
20551 {
20552 decl_specs->any_specifiers_p = true;
20553
20554 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20555 (with, for example, in "typedef int wchar_t;") we remember that
20556 this is what happened. In system headers, we ignore these
20557 declarations so that G++ can work with system headers that are not
20558 C++-safe. */
20559 if (decl_specs->specs[(int) ds_typedef]
20560 && !user_defined_p
20561 && (type_spec == boolean_type_node
20562 || type_spec == char16_type_node
20563 || type_spec == char32_type_node
20564 || type_spec == wchar_type_node)
20565 && (decl_specs->type
20566 || decl_specs->specs[(int) ds_long]
20567 || decl_specs->specs[(int) ds_short]
20568 || decl_specs->specs[(int) ds_unsigned]
20569 || decl_specs->specs[(int) ds_signed]))
20570 {
20571 decl_specs->redefined_builtin_type = type_spec;
20572 if (!decl_specs->type)
20573 {
20574 decl_specs->type = type_spec;
20575 decl_specs->user_defined_type_p = false;
20576 decl_specs->type_location = location;
20577 }
20578 }
20579 else if (decl_specs->type)
20580 decl_specs->multiple_types_p = true;
20581 else
20582 {
20583 decl_specs->type = type_spec;
20584 decl_specs->user_defined_type_p = user_defined_p;
20585 decl_specs->redefined_builtin_type = NULL_TREE;
20586 decl_specs->type_location = location;
20587 }
20588 }
20589
20590 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20591 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20592
20593 static bool
20594 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20595 {
20596 return decl_specifiers->specs[(int) ds_friend] != 0;
20597 }
20598
20599 /* Issue an error message indicating that TOKEN_DESC was expected.
20600 If KEYWORD is true, it indicated this function is called by
20601 cp_parser_require_keword and the required token can only be
20602 a indicated keyword. */
20603
20604 static void
20605 cp_parser_required_error (cp_parser *parser,
20606 required_token token_desc,
20607 bool keyword)
20608 {
20609 switch (token_desc)
20610 {
20611 case RT_NEW:
20612 cp_parser_error (parser, "expected %<new%>");
20613 return;
20614 case RT_DELETE:
20615 cp_parser_error (parser, "expected %<delete%>");
20616 return;
20617 case RT_RETURN:
20618 cp_parser_error (parser, "expected %<return%>");
20619 return;
20620 case RT_WHILE:
20621 cp_parser_error (parser, "expected %<while%>");
20622 return;
20623 case RT_EXTERN:
20624 cp_parser_error (parser, "expected %<extern%>");
20625 return;
20626 case RT_STATIC_ASSERT:
20627 cp_parser_error (parser, "expected %<static_assert%>");
20628 return;
20629 case RT_DECLTYPE:
20630 cp_parser_error (parser, "expected %<decltype%>");
20631 return;
20632 case RT_OPERATOR:
20633 cp_parser_error (parser, "expected %<operator%>");
20634 return;
20635 case RT_CLASS:
20636 cp_parser_error (parser, "expected %<class%>");
20637 return;
20638 case RT_TEMPLATE:
20639 cp_parser_error (parser, "expected %<template%>");
20640 return;
20641 case RT_NAMESPACE:
20642 cp_parser_error (parser, "expected %<namespace%>");
20643 return;
20644 case RT_USING:
20645 cp_parser_error (parser, "expected %<using%>");
20646 return;
20647 case RT_ASM:
20648 cp_parser_error (parser, "expected %<asm%>");
20649 return;
20650 case RT_TRY:
20651 cp_parser_error (parser, "expected %<try%>");
20652 return;
20653 case RT_CATCH:
20654 cp_parser_error (parser, "expected %<catch%>");
20655 return;
20656 case RT_THROW:
20657 cp_parser_error (parser, "expected %<throw%>");
20658 return;
20659 case RT_LABEL:
20660 cp_parser_error (parser, "expected %<__label__%>");
20661 return;
20662 case RT_AT_TRY:
20663 cp_parser_error (parser, "expected %<@try%>");
20664 return;
20665 case RT_AT_SYNCHRONIZED:
20666 cp_parser_error (parser, "expected %<@synchronized%>");
20667 return;
20668 case RT_AT_THROW:
20669 cp_parser_error (parser, "expected %<@throw%>");
20670 return;
20671 default:
20672 break;
20673 }
20674 if (!keyword)
20675 {
20676 switch (token_desc)
20677 {
20678 case RT_SEMICOLON:
20679 cp_parser_error (parser, "expected %<;%>");
20680 return;
20681 case RT_OPEN_PAREN:
20682 cp_parser_error (parser, "expected %<(%>");
20683 return;
20684 case RT_CLOSE_BRACE:
20685 cp_parser_error (parser, "expected %<}%>");
20686 return;
20687 case RT_OPEN_BRACE:
20688 cp_parser_error (parser, "expected %<{%>");
20689 return;
20690 case RT_CLOSE_SQUARE:
20691 cp_parser_error (parser, "expected %<]%>");
20692 return;
20693 case RT_OPEN_SQUARE:
20694 cp_parser_error (parser, "expected %<[%>");
20695 return;
20696 case RT_COMMA:
20697 cp_parser_error (parser, "expected %<,%>");
20698 return;
20699 case RT_SCOPE:
20700 cp_parser_error (parser, "expected %<::%>");
20701 return;
20702 case RT_LESS:
20703 cp_parser_error (parser, "expected %<<%>");
20704 return;
20705 case RT_GREATER:
20706 cp_parser_error (parser, "expected %<>%>");
20707 return;
20708 case RT_EQ:
20709 cp_parser_error (parser, "expected %<=%>");
20710 return;
20711 case RT_ELLIPSIS:
20712 cp_parser_error (parser, "expected %<...%>");
20713 return;
20714 case RT_MULT:
20715 cp_parser_error (parser, "expected %<*%>");
20716 return;
20717 case RT_COMPL:
20718 cp_parser_error (parser, "expected %<~%>");
20719 return;
20720 case RT_COLON:
20721 cp_parser_error (parser, "expected %<:%>");
20722 return;
20723 case RT_COLON_SCOPE:
20724 cp_parser_error (parser, "expected %<:%> or %<::%>");
20725 return;
20726 case RT_CLOSE_PAREN:
20727 cp_parser_error (parser, "expected %<)%>");
20728 return;
20729 case RT_COMMA_CLOSE_PAREN:
20730 cp_parser_error (parser, "expected %<,%> or %<)%>");
20731 return;
20732 case RT_PRAGMA_EOL:
20733 cp_parser_error (parser, "expected end of line");
20734 return;
20735 case RT_NAME:
20736 cp_parser_error (parser, "expected identifier");
20737 return;
20738 case RT_SELECT:
20739 cp_parser_error (parser, "expected selection-statement");
20740 return;
20741 case RT_INTERATION:
20742 cp_parser_error (parser, "expected iteration-statement");
20743 return;
20744 case RT_JUMP:
20745 cp_parser_error (parser, "expected jump-statement");
20746 return;
20747 case RT_CLASS_KEY:
20748 cp_parser_error (parser, "expected class-key");
20749 return;
20750 case RT_CLASS_TYPENAME_TEMPLATE:
20751 cp_parser_error (parser,
20752 "expected %<class%>, %<typename%>, or %<template%>");
20753 return;
20754 default:
20755 gcc_unreachable ();
20756 }
20757 }
20758 else
20759 gcc_unreachable ();
20760 }
20761
20762
20763
20764 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20765 issue an error message indicating that TOKEN_DESC was expected.
20766
20767 Returns the token consumed, if the token had the appropriate type.
20768 Otherwise, returns NULL. */
20769
20770 static cp_token *
20771 cp_parser_require (cp_parser* parser,
20772 enum cpp_ttype type,
20773 required_token token_desc)
20774 {
20775 if (cp_lexer_next_token_is (parser->lexer, type))
20776 return cp_lexer_consume_token (parser->lexer);
20777 else
20778 {
20779 /* Output the MESSAGE -- unless we're parsing tentatively. */
20780 if (!cp_parser_simulate_error (parser))
20781 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20782 return NULL;
20783 }
20784 }
20785
20786 /* An error message is produced if the next token is not '>'.
20787 All further tokens are skipped until the desired token is
20788 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20789
20790 static void
20791 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20792 {
20793 /* Current level of '< ... >'. */
20794 unsigned level = 0;
20795 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20796 unsigned nesting_depth = 0;
20797
20798 /* Are we ready, yet? If not, issue error message. */
20799 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20800 return;
20801
20802 /* Skip tokens until the desired token is found. */
20803 while (true)
20804 {
20805 /* Peek at the next token. */
20806 switch (cp_lexer_peek_token (parser->lexer)->type)
20807 {
20808 case CPP_LESS:
20809 if (!nesting_depth)
20810 ++level;
20811 break;
20812
20813 case CPP_RSHIFT:
20814 if (cxx_dialect == cxx98)
20815 /* C++0x views the `>>' operator as two `>' tokens, but
20816 C++98 does not. */
20817 break;
20818 else if (!nesting_depth && level-- == 0)
20819 {
20820 /* We've hit a `>>' where the first `>' closes the
20821 template argument list, and the second `>' is
20822 spurious. Just consume the `>>' and stop; we've
20823 already produced at least one error. */
20824 cp_lexer_consume_token (parser->lexer);
20825 return;
20826 }
20827 /* Fall through for C++0x, so we handle the second `>' in
20828 the `>>'. */
20829
20830 case CPP_GREATER:
20831 if (!nesting_depth && level-- == 0)
20832 {
20833 /* We've reached the token we want, consume it and stop. */
20834 cp_lexer_consume_token (parser->lexer);
20835 return;
20836 }
20837 break;
20838
20839 case CPP_OPEN_PAREN:
20840 case CPP_OPEN_SQUARE:
20841 ++nesting_depth;
20842 break;
20843
20844 case CPP_CLOSE_PAREN:
20845 case CPP_CLOSE_SQUARE:
20846 if (nesting_depth-- == 0)
20847 return;
20848 break;
20849
20850 case CPP_EOF:
20851 case CPP_PRAGMA_EOL:
20852 case CPP_SEMICOLON:
20853 case CPP_OPEN_BRACE:
20854 case CPP_CLOSE_BRACE:
20855 /* The '>' was probably forgotten, don't look further. */
20856 return;
20857
20858 default:
20859 break;
20860 }
20861
20862 /* Consume this token. */
20863 cp_lexer_consume_token (parser->lexer);
20864 }
20865 }
20866
20867 /* If the next token is the indicated keyword, consume it. Otherwise,
20868 issue an error message indicating that TOKEN_DESC was expected.
20869
20870 Returns the token consumed, if the token had the appropriate type.
20871 Otherwise, returns NULL. */
20872
20873 static cp_token *
20874 cp_parser_require_keyword (cp_parser* parser,
20875 enum rid keyword,
20876 required_token token_desc)
20877 {
20878 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20879
20880 if (token && token->keyword != keyword)
20881 {
20882 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
20883 return NULL;
20884 }
20885
20886 return token;
20887 }
20888
20889 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20890 function-definition. */
20891
20892 static bool
20893 cp_parser_token_starts_function_definition_p (cp_token* token)
20894 {
20895 return (/* An ordinary function-body begins with an `{'. */
20896 token->type == CPP_OPEN_BRACE
20897 /* A ctor-initializer begins with a `:'. */
20898 || token->type == CPP_COLON
20899 /* A function-try-block begins with `try'. */
20900 || token->keyword == RID_TRY
20901 /* The named return value extension begins with `return'. */
20902 || token->keyword == RID_RETURN);
20903 }
20904
20905 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
20906 definition. */
20907
20908 static bool
20909 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
20910 {
20911 cp_token *token;
20912
20913 token = cp_lexer_peek_token (parser->lexer);
20914 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
20915 }
20916
20917 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
20918 C++0x) ending a template-argument. */
20919
20920 static bool
20921 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
20922 {
20923 cp_token *token;
20924
20925 token = cp_lexer_peek_token (parser->lexer);
20926 return (token->type == CPP_COMMA
20927 || token->type == CPP_GREATER
20928 || token->type == CPP_ELLIPSIS
20929 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
20930 }
20931
20932 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
20933 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
20934
20935 static bool
20936 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
20937 size_t n)
20938 {
20939 cp_token *token;
20940
20941 token = cp_lexer_peek_nth_token (parser->lexer, n);
20942 if (token->type == CPP_LESS)
20943 return true;
20944 /* Check for the sequence `<::' in the original code. It would be lexed as
20945 `[:', where `[' is a digraph, and there is no whitespace before
20946 `:'. */
20947 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
20948 {
20949 cp_token *token2;
20950 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
20951 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
20952 return true;
20953 }
20954 return false;
20955 }
20956
20957 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
20958 or none_type otherwise. */
20959
20960 static enum tag_types
20961 cp_parser_token_is_class_key (cp_token* token)
20962 {
20963 switch (token->keyword)
20964 {
20965 case RID_CLASS:
20966 return class_type;
20967 case RID_STRUCT:
20968 return record_type;
20969 case RID_UNION:
20970 return union_type;
20971
20972 default:
20973 return none_type;
20974 }
20975 }
20976
20977 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
20978
20979 static void
20980 cp_parser_check_class_key (enum tag_types class_key, tree type)
20981 {
20982 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
20983 permerror (input_location, "%qs tag used in naming %q#T",
20984 class_key == union_type ? "union"
20985 : class_key == record_type ? "struct" : "class",
20986 type);
20987 }
20988
20989 /* Issue an error message if DECL is redeclared with different
20990 access than its original declaration [class.access.spec/3].
20991 This applies to nested classes and nested class templates.
20992 [class.mem/1]. */
20993
20994 static void
20995 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
20996 {
20997 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
20998 return;
20999
21000 if ((TREE_PRIVATE (decl)
21001 != (current_access_specifier == access_private_node))
21002 || (TREE_PROTECTED (decl)
21003 != (current_access_specifier == access_protected_node)))
21004 error_at (location, "%qD redeclared with different access", decl);
21005 }
21006
21007 /* Look for the `template' keyword, as a syntactic disambiguator.
21008 Return TRUE iff it is present, in which case it will be
21009 consumed. */
21010
21011 static bool
21012 cp_parser_optional_template_keyword (cp_parser *parser)
21013 {
21014 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21015 {
21016 /* The `template' keyword can only be used within templates;
21017 outside templates the parser can always figure out what is a
21018 template and what is not. */
21019 if (!processing_template_decl)
21020 {
21021 cp_token *token = cp_lexer_peek_token (parser->lexer);
21022 error_at (token->location,
21023 "%<template%> (as a disambiguator) is only allowed "
21024 "within templates");
21025 /* If this part of the token stream is rescanned, the same
21026 error message would be generated. So, we purge the token
21027 from the stream. */
21028 cp_lexer_purge_token (parser->lexer);
21029 return false;
21030 }
21031 else
21032 {
21033 /* Consume the `template' keyword. */
21034 cp_lexer_consume_token (parser->lexer);
21035 return true;
21036 }
21037 }
21038
21039 return false;
21040 }
21041
21042 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
21043 set PARSER->SCOPE, and perform other related actions. */
21044
21045 static void
21046 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21047 {
21048 int i;
21049 struct tree_check *check_value;
21050 deferred_access_check *chk;
21051 VEC (deferred_access_check,gc) *checks;
21052
21053 /* Get the stored value. */
21054 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21055 /* Perform any access checks that were deferred. */
21056 checks = check_value->checks;
21057 if (checks)
21058 {
21059 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21060 perform_or_defer_access_check (chk->binfo,
21061 chk->decl,
21062 chk->diag_decl);
21063 }
21064 /* Set the scope from the stored value. */
21065 parser->scope = check_value->value;
21066 parser->qualifying_scope = check_value->qualifying_scope;
21067 parser->object_scope = NULL_TREE;
21068 }
21069
21070 /* Consume tokens up through a non-nested END token. Returns TRUE if we
21071 encounter the end of a block before what we were looking for. */
21072
21073 static bool
21074 cp_parser_cache_group (cp_parser *parser,
21075 enum cpp_ttype end,
21076 unsigned depth)
21077 {
21078 while (true)
21079 {
21080 cp_token *token = cp_lexer_peek_token (parser->lexer);
21081
21082 /* Abort a parenthesized expression if we encounter a semicolon. */
21083 if ((end == CPP_CLOSE_PAREN || depth == 0)
21084 && token->type == CPP_SEMICOLON)
21085 return true;
21086 /* If we've reached the end of the file, stop. */
21087 if (token->type == CPP_EOF
21088 || (end != CPP_PRAGMA_EOL
21089 && token->type == CPP_PRAGMA_EOL))
21090 return true;
21091 if (token->type == CPP_CLOSE_BRACE && depth == 0)
21092 /* We've hit the end of an enclosing block, so there's been some
21093 kind of syntax error. */
21094 return true;
21095
21096 /* Consume the token. */
21097 cp_lexer_consume_token (parser->lexer);
21098 /* See if it starts a new group. */
21099 if (token->type == CPP_OPEN_BRACE)
21100 {
21101 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21102 /* In theory this should probably check end == '}', but
21103 cp_parser_save_member_function_body needs it to exit
21104 after either '}' or ')' when called with ')'. */
21105 if (depth == 0)
21106 return false;
21107 }
21108 else if (token->type == CPP_OPEN_PAREN)
21109 {
21110 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21111 if (depth == 0 && end == CPP_CLOSE_PAREN)
21112 return false;
21113 }
21114 else if (token->type == CPP_PRAGMA)
21115 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21116 else if (token->type == end)
21117 return false;
21118 }
21119 }
21120
21121 /* Begin parsing tentatively. We always save tokens while parsing
21122 tentatively so that if the tentative parsing fails we can restore the
21123 tokens. */
21124
21125 static void
21126 cp_parser_parse_tentatively (cp_parser* parser)
21127 {
21128 /* Enter a new parsing context. */
21129 parser->context = cp_parser_context_new (parser->context);
21130 /* Begin saving tokens. */
21131 cp_lexer_save_tokens (parser->lexer);
21132 /* In order to avoid repetitive access control error messages,
21133 access checks are queued up until we are no longer parsing
21134 tentatively. */
21135 push_deferring_access_checks (dk_deferred);
21136 }
21137
21138 /* Commit to the currently active tentative parse. */
21139
21140 static void
21141 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21142 {
21143 cp_parser_context *context;
21144 cp_lexer *lexer;
21145
21146 /* Mark all of the levels as committed. */
21147 lexer = parser->lexer;
21148 for (context = parser->context; context->next; context = context->next)
21149 {
21150 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21151 break;
21152 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21153 while (!cp_lexer_saving_tokens (lexer))
21154 lexer = lexer->next;
21155 cp_lexer_commit_tokens (lexer);
21156 }
21157 }
21158
21159 /* Abort the currently active tentative parse. All consumed tokens
21160 will be rolled back, and no diagnostics will be issued. */
21161
21162 static void
21163 cp_parser_abort_tentative_parse (cp_parser* parser)
21164 {
21165 cp_parser_simulate_error (parser);
21166 /* Now, pretend that we want to see if the construct was
21167 successfully parsed. */
21168 cp_parser_parse_definitely (parser);
21169 }
21170
21171 /* Stop parsing tentatively. If a parse error has occurred, restore the
21172 token stream. Otherwise, commit to the tokens we have consumed.
21173 Returns true if no error occurred; false otherwise. */
21174
21175 static bool
21176 cp_parser_parse_definitely (cp_parser* parser)
21177 {
21178 bool error_occurred;
21179 cp_parser_context *context;
21180
21181 /* Remember whether or not an error occurred, since we are about to
21182 destroy that information. */
21183 error_occurred = cp_parser_error_occurred (parser);
21184 /* Remove the topmost context from the stack. */
21185 context = parser->context;
21186 parser->context = context->next;
21187 /* If no parse errors occurred, commit to the tentative parse. */
21188 if (!error_occurred)
21189 {
21190 /* Commit to the tokens read tentatively, unless that was
21191 already done. */
21192 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21193 cp_lexer_commit_tokens (parser->lexer);
21194
21195 pop_to_parent_deferring_access_checks ();
21196 }
21197 /* Otherwise, if errors occurred, roll back our state so that things
21198 are just as they were before we began the tentative parse. */
21199 else
21200 {
21201 cp_lexer_rollback_tokens (parser->lexer);
21202 pop_deferring_access_checks ();
21203 }
21204 /* Add the context to the front of the free list. */
21205 context->next = cp_parser_context_free_list;
21206 cp_parser_context_free_list = context;
21207
21208 return !error_occurred;
21209 }
21210
21211 /* Returns true if we are parsing tentatively and are not committed to
21212 this tentative parse. */
21213
21214 static bool
21215 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21216 {
21217 return (cp_parser_parsing_tentatively (parser)
21218 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21219 }
21220
21221 /* Returns nonzero iff an error has occurred during the most recent
21222 tentative parse. */
21223
21224 static bool
21225 cp_parser_error_occurred (cp_parser* parser)
21226 {
21227 return (cp_parser_parsing_tentatively (parser)
21228 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21229 }
21230
21231 /* Returns nonzero if GNU extensions are allowed. */
21232
21233 static bool
21234 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21235 {
21236 return parser->allow_gnu_extensions_p;
21237 }
21238 \f
21239 /* Objective-C++ Productions */
21240
21241
21242 /* Parse an Objective-C expression, which feeds into a primary-expression
21243 above.
21244
21245 objc-expression:
21246 objc-message-expression
21247 objc-string-literal
21248 objc-encode-expression
21249 objc-protocol-expression
21250 objc-selector-expression
21251
21252 Returns a tree representation of the expression. */
21253
21254 static tree
21255 cp_parser_objc_expression (cp_parser* parser)
21256 {
21257 /* Try to figure out what kind of declaration is present. */
21258 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21259
21260 switch (kwd->type)
21261 {
21262 case CPP_OPEN_SQUARE:
21263 return cp_parser_objc_message_expression (parser);
21264
21265 case CPP_OBJC_STRING:
21266 kwd = cp_lexer_consume_token (parser->lexer);
21267 return objc_build_string_object (kwd->u.value);
21268
21269 case CPP_KEYWORD:
21270 switch (kwd->keyword)
21271 {
21272 case RID_AT_ENCODE:
21273 return cp_parser_objc_encode_expression (parser);
21274
21275 case RID_AT_PROTOCOL:
21276 return cp_parser_objc_protocol_expression (parser);
21277
21278 case RID_AT_SELECTOR:
21279 return cp_parser_objc_selector_expression (parser);
21280
21281 default:
21282 break;
21283 }
21284 default:
21285 error_at (kwd->location,
21286 "misplaced %<@%D%> Objective-C++ construct",
21287 kwd->u.value);
21288 cp_parser_skip_to_end_of_block_or_statement (parser);
21289 }
21290
21291 return error_mark_node;
21292 }
21293
21294 /* Parse an Objective-C message expression.
21295
21296 objc-message-expression:
21297 [ objc-message-receiver objc-message-args ]
21298
21299 Returns a representation of an Objective-C message. */
21300
21301 static tree
21302 cp_parser_objc_message_expression (cp_parser* parser)
21303 {
21304 tree receiver, messageargs;
21305
21306 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
21307 receiver = cp_parser_objc_message_receiver (parser);
21308 messageargs = cp_parser_objc_message_args (parser);
21309 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21310
21311 return objc_build_message_expr (build_tree_list (receiver, messageargs));
21312 }
21313
21314 /* Parse an objc-message-receiver.
21315
21316 objc-message-receiver:
21317 expression
21318 simple-type-specifier
21319
21320 Returns a representation of the type or expression. */
21321
21322 static tree
21323 cp_parser_objc_message_receiver (cp_parser* parser)
21324 {
21325 tree rcv;
21326
21327 /* An Objective-C message receiver may be either (1) a type
21328 or (2) an expression. */
21329 cp_parser_parse_tentatively (parser);
21330 rcv = cp_parser_expression (parser, false, NULL);
21331
21332 if (cp_parser_parse_definitely (parser))
21333 return rcv;
21334
21335 rcv = cp_parser_simple_type_specifier (parser,
21336 /*decl_specs=*/NULL,
21337 CP_PARSER_FLAGS_NONE);
21338
21339 return objc_get_class_reference (rcv);
21340 }
21341
21342 /* Parse the arguments and selectors comprising an Objective-C message.
21343
21344 objc-message-args:
21345 objc-selector
21346 objc-selector-args
21347 objc-selector-args , objc-comma-args
21348
21349 objc-selector-args:
21350 objc-selector [opt] : assignment-expression
21351 objc-selector-args objc-selector [opt] : assignment-expression
21352
21353 objc-comma-args:
21354 assignment-expression
21355 objc-comma-args , assignment-expression
21356
21357 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21358 selector arguments and TREE_VALUE containing a list of comma
21359 arguments. */
21360
21361 static tree
21362 cp_parser_objc_message_args (cp_parser* parser)
21363 {
21364 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21365 bool maybe_unary_selector_p = true;
21366 cp_token *token = cp_lexer_peek_token (parser->lexer);
21367
21368 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21369 {
21370 tree selector = NULL_TREE, arg;
21371
21372 if (token->type != CPP_COLON)
21373 selector = cp_parser_objc_selector (parser);
21374
21375 /* Detect if we have a unary selector. */
21376 if (maybe_unary_selector_p
21377 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21378 return build_tree_list (selector, NULL_TREE);
21379
21380 maybe_unary_selector_p = false;
21381 cp_parser_require (parser, CPP_COLON, RT_COLON);
21382 arg = cp_parser_assignment_expression (parser, false, NULL);
21383
21384 sel_args
21385 = chainon (sel_args,
21386 build_tree_list (selector, arg));
21387
21388 token = cp_lexer_peek_token (parser->lexer);
21389 }
21390
21391 /* Handle non-selector arguments, if any. */
21392 while (token->type == CPP_COMMA)
21393 {
21394 tree arg;
21395
21396 cp_lexer_consume_token (parser->lexer);
21397 arg = cp_parser_assignment_expression (parser, false, NULL);
21398
21399 addl_args
21400 = chainon (addl_args,
21401 build_tree_list (NULL_TREE, arg));
21402
21403 token = cp_lexer_peek_token (parser->lexer);
21404 }
21405
21406 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21407 {
21408 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21409 return build_tree_list (error_mark_node, error_mark_node);
21410 }
21411
21412 return build_tree_list (sel_args, addl_args);
21413 }
21414
21415 /* Parse an Objective-C encode expression.
21416
21417 objc-encode-expression:
21418 @encode objc-typename
21419
21420 Returns an encoded representation of the type argument. */
21421
21422 static tree
21423 cp_parser_objc_encode_expression (cp_parser* parser)
21424 {
21425 tree type;
21426 cp_token *token;
21427
21428 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
21429 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21430 token = cp_lexer_peek_token (parser->lexer);
21431 type = complete_type (cp_parser_type_id (parser));
21432 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21433
21434 if (!type)
21435 {
21436 error_at (token->location,
21437 "%<@encode%> must specify a type as an argument");
21438 return error_mark_node;
21439 }
21440
21441 /* This happens if we find @encode(T) (where T is a template
21442 typename or something dependent on a template typename) when
21443 parsing a template. In that case, we can't compile it
21444 immediately, but we rather create an AT_ENCODE_EXPR which will
21445 need to be instantiated when the template is used.
21446 */
21447 if (dependent_type_p (type))
21448 {
21449 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21450 TREE_READONLY (value) = 1;
21451 return value;
21452 }
21453
21454 return objc_build_encode_expr (type);
21455 }
21456
21457 /* Parse an Objective-C @defs expression. */
21458
21459 static tree
21460 cp_parser_objc_defs_expression (cp_parser *parser)
21461 {
21462 tree name;
21463
21464 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
21465 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21466 name = cp_parser_identifier (parser);
21467 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21468
21469 return objc_get_class_ivars (name);
21470 }
21471
21472 /* Parse an Objective-C protocol expression.
21473
21474 objc-protocol-expression:
21475 @protocol ( identifier )
21476
21477 Returns a representation of the protocol expression. */
21478
21479 static tree
21480 cp_parser_objc_protocol_expression (cp_parser* parser)
21481 {
21482 tree proto;
21483
21484 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21485 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21486 proto = cp_parser_identifier (parser);
21487 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21488
21489 return objc_build_protocol_expr (proto);
21490 }
21491
21492 /* Parse an Objective-C selector expression.
21493
21494 objc-selector-expression:
21495 @selector ( objc-method-signature )
21496
21497 objc-method-signature:
21498 objc-selector
21499 objc-selector-seq
21500
21501 objc-selector-seq:
21502 objc-selector :
21503 objc-selector-seq objc-selector :
21504
21505 Returns a representation of the method selector. */
21506
21507 static tree
21508 cp_parser_objc_selector_expression (cp_parser* parser)
21509 {
21510 tree sel_seq = NULL_TREE;
21511 bool maybe_unary_selector_p = true;
21512 cp_token *token;
21513 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21514
21515 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
21516 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21517 token = cp_lexer_peek_token (parser->lexer);
21518
21519 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21520 || token->type == CPP_SCOPE)
21521 {
21522 tree selector = NULL_TREE;
21523
21524 if (token->type != CPP_COLON
21525 || token->type == CPP_SCOPE)
21526 selector = cp_parser_objc_selector (parser);
21527
21528 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21529 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21530 {
21531 /* Detect if we have a unary selector. */
21532 if (maybe_unary_selector_p)
21533 {
21534 sel_seq = selector;
21535 goto finish_selector;
21536 }
21537 else
21538 {
21539 cp_parser_error (parser, "expected %<:%>");
21540 }
21541 }
21542 maybe_unary_selector_p = false;
21543 token = cp_lexer_consume_token (parser->lexer);
21544
21545 if (token->type == CPP_SCOPE)
21546 {
21547 sel_seq
21548 = chainon (sel_seq,
21549 build_tree_list (selector, NULL_TREE));
21550 sel_seq
21551 = chainon (sel_seq,
21552 build_tree_list (NULL_TREE, NULL_TREE));
21553 }
21554 else
21555 sel_seq
21556 = chainon (sel_seq,
21557 build_tree_list (selector, NULL_TREE));
21558
21559 token = cp_lexer_peek_token (parser->lexer);
21560 }
21561
21562 finish_selector:
21563 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21564
21565 return objc_build_selector_expr (loc, sel_seq);
21566 }
21567
21568 /* Parse a list of identifiers.
21569
21570 objc-identifier-list:
21571 identifier
21572 objc-identifier-list , identifier
21573
21574 Returns a TREE_LIST of identifier nodes. */
21575
21576 static tree
21577 cp_parser_objc_identifier_list (cp_parser* parser)
21578 {
21579 tree identifier;
21580 tree list;
21581 cp_token *sep;
21582
21583 identifier = cp_parser_identifier (parser);
21584 if (identifier == error_mark_node)
21585 return error_mark_node;
21586
21587 list = build_tree_list (NULL_TREE, identifier);
21588 sep = cp_lexer_peek_token (parser->lexer);
21589
21590 while (sep->type == CPP_COMMA)
21591 {
21592 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21593 identifier = cp_parser_identifier (parser);
21594 if (identifier == error_mark_node)
21595 return list;
21596
21597 list = chainon (list, build_tree_list (NULL_TREE,
21598 identifier));
21599 sep = cp_lexer_peek_token (parser->lexer);
21600 }
21601
21602 return list;
21603 }
21604
21605 /* Parse an Objective-C alias declaration.
21606
21607 objc-alias-declaration:
21608 @compatibility_alias identifier identifier ;
21609
21610 This function registers the alias mapping with the Objective-C front end.
21611 It returns nothing. */
21612
21613 static void
21614 cp_parser_objc_alias_declaration (cp_parser* parser)
21615 {
21616 tree alias, orig;
21617
21618 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
21619 alias = cp_parser_identifier (parser);
21620 orig = cp_parser_identifier (parser);
21621 objc_declare_alias (alias, orig);
21622 cp_parser_consume_semicolon_at_end_of_statement (parser);
21623 }
21624
21625 /* Parse an Objective-C class forward-declaration.
21626
21627 objc-class-declaration:
21628 @class objc-identifier-list ;
21629
21630 The function registers the forward declarations with the Objective-C
21631 front end. It returns nothing. */
21632
21633 static void
21634 cp_parser_objc_class_declaration (cp_parser* parser)
21635 {
21636 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
21637 objc_declare_class (cp_parser_objc_identifier_list (parser));
21638 cp_parser_consume_semicolon_at_end_of_statement (parser);
21639 }
21640
21641 /* Parse a list of Objective-C protocol references.
21642
21643 objc-protocol-refs-opt:
21644 objc-protocol-refs [opt]
21645
21646 objc-protocol-refs:
21647 < objc-identifier-list >
21648
21649 Returns a TREE_LIST of identifiers, if any. */
21650
21651 static tree
21652 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21653 {
21654 tree protorefs = NULL_TREE;
21655
21656 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21657 {
21658 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
21659 protorefs = cp_parser_objc_identifier_list (parser);
21660 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21661 }
21662
21663 return protorefs;
21664 }
21665
21666 /* Parse a Objective-C visibility specification. */
21667
21668 static void
21669 cp_parser_objc_visibility_spec (cp_parser* parser)
21670 {
21671 cp_token *vis = cp_lexer_peek_token (parser->lexer);
21672
21673 switch (vis->keyword)
21674 {
21675 case RID_AT_PRIVATE:
21676 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21677 break;
21678 case RID_AT_PROTECTED:
21679 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21680 break;
21681 case RID_AT_PUBLIC:
21682 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21683 break;
21684 case RID_AT_PACKAGE:
21685 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21686 break;
21687 default:
21688 return;
21689 }
21690
21691 /* Eat '@private'/'@protected'/'@public'. */
21692 cp_lexer_consume_token (parser->lexer);
21693 }
21694
21695 /* Parse an Objective-C method type. Return 'true' if it is a class
21696 (+) method, and 'false' if it is an instance (-) method. */
21697
21698 static inline bool
21699 cp_parser_objc_method_type (cp_parser* parser)
21700 {
21701 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21702 return true;
21703 else
21704 return false;
21705 }
21706
21707 /* Parse an Objective-C protocol qualifier. */
21708
21709 static tree
21710 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21711 {
21712 tree quals = NULL_TREE, node;
21713 cp_token *token = cp_lexer_peek_token (parser->lexer);
21714
21715 node = token->u.value;
21716
21717 while (node && TREE_CODE (node) == IDENTIFIER_NODE
21718 && (node == ridpointers [(int) RID_IN]
21719 || node == ridpointers [(int) RID_OUT]
21720 || node == ridpointers [(int) RID_INOUT]
21721 || node == ridpointers [(int) RID_BYCOPY]
21722 || node == ridpointers [(int) RID_BYREF]
21723 || node == ridpointers [(int) RID_ONEWAY]))
21724 {
21725 quals = tree_cons (NULL_TREE, node, quals);
21726 cp_lexer_consume_token (parser->lexer);
21727 token = cp_lexer_peek_token (parser->lexer);
21728 node = token->u.value;
21729 }
21730
21731 return quals;
21732 }
21733
21734 /* Parse an Objective-C typename. */
21735
21736 static tree
21737 cp_parser_objc_typename (cp_parser* parser)
21738 {
21739 tree type_name = NULL_TREE;
21740
21741 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21742 {
21743 tree proto_quals, cp_type = NULL_TREE;
21744
21745 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21746 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21747
21748 /* An ObjC type name may consist of just protocol qualifiers, in which
21749 case the type shall default to 'id'. */
21750 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21751 cp_type = cp_parser_type_id (parser);
21752
21753 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21754 type_name = build_tree_list (proto_quals, cp_type);
21755 }
21756
21757 return type_name;
21758 }
21759
21760 /* Check to see if TYPE refers to an Objective-C selector name. */
21761
21762 static bool
21763 cp_parser_objc_selector_p (enum cpp_ttype type)
21764 {
21765 return (type == CPP_NAME || type == CPP_KEYWORD
21766 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21767 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21768 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21769 || type == CPP_XOR || type == CPP_XOR_EQ);
21770 }
21771
21772 /* Parse an Objective-C selector. */
21773
21774 static tree
21775 cp_parser_objc_selector (cp_parser* parser)
21776 {
21777 cp_token *token = cp_lexer_consume_token (parser->lexer);
21778
21779 if (!cp_parser_objc_selector_p (token->type))
21780 {
21781 error_at (token->location, "invalid Objective-C++ selector name");
21782 return error_mark_node;
21783 }
21784
21785 /* C++ operator names are allowed to appear in ObjC selectors. */
21786 switch (token->type)
21787 {
21788 case CPP_AND_AND: return get_identifier ("and");
21789 case CPP_AND_EQ: return get_identifier ("and_eq");
21790 case CPP_AND: return get_identifier ("bitand");
21791 case CPP_OR: return get_identifier ("bitor");
21792 case CPP_COMPL: return get_identifier ("compl");
21793 case CPP_NOT: return get_identifier ("not");
21794 case CPP_NOT_EQ: return get_identifier ("not_eq");
21795 case CPP_OR_OR: return get_identifier ("or");
21796 case CPP_OR_EQ: return get_identifier ("or_eq");
21797 case CPP_XOR: return get_identifier ("xor");
21798 case CPP_XOR_EQ: return get_identifier ("xor_eq");
21799 default: return token->u.value;
21800 }
21801 }
21802
21803 /* Parse an Objective-C params list. */
21804
21805 static tree
21806 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21807 {
21808 tree params = NULL_TREE;
21809 bool maybe_unary_selector_p = true;
21810 cp_token *token = cp_lexer_peek_token (parser->lexer);
21811
21812 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21813 {
21814 tree selector = NULL_TREE, type_name, identifier;
21815 tree parm_attr = NULL_TREE;
21816
21817 if (token->keyword == RID_ATTRIBUTE)
21818 break;
21819
21820 if (token->type != CPP_COLON)
21821 selector = cp_parser_objc_selector (parser);
21822
21823 /* Detect if we have a unary selector. */
21824 if (maybe_unary_selector_p
21825 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21826 {
21827 params = selector; /* Might be followed by attributes. */
21828 break;
21829 }
21830
21831 maybe_unary_selector_p = false;
21832 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
21833 {
21834 /* Something went quite wrong. There should be a colon
21835 here, but there is not. Stop parsing parameters. */
21836 break;
21837 }
21838 type_name = cp_parser_objc_typename (parser);
21839 /* New ObjC allows attributes on parameters too. */
21840 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
21841 parm_attr = cp_parser_attributes_opt (parser);
21842 identifier = cp_parser_identifier (parser);
21843
21844 params
21845 = chainon (params,
21846 objc_build_keyword_decl (selector,
21847 type_name,
21848 identifier,
21849 parm_attr));
21850
21851 token = cp_lexer_peek_token (parser->lexer);
21852 }
21853
21854 if (params == NULL_TREE)
21855 {
21856 cp_parser_error (parser, "objective-c++ method declaration is expected");
21857 return error_mark_node;
21858 }
21859
21860 /* We allow tail attributes for the method. */
21861 if (token->keyword == RID_ATTRIBUTE)
21862 {
21863 *attributes = cp_parser_attributes_opt (parser);
21864 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21865 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21866 return params;
21867 cp_parser_error (parser,
21868 "method attributes must be specified at the end");
21869 return error_mark_node;
21870 }
21871
21872 if (params == NULL_TREE)
21873 {
21874 cp_parser_error (parser, "objective-c++ method declaration is expected");
21875 return error_mark_node;
21876 }
21877 return params;
21878 }
21879
21880 /* Parse the non-keyword Objective-C params. */
21881
21882 static tree
21883 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
21884 tree* attributes)
21885 {
21886 tree params = make_node (TREE_LIST);
21887 cp_token *token = cp_lexer_peek_token (parser->lexer);
21888 *ellipsisp = false; /* Initially, assume no ellipsis. */
21889
21890 while (token->type == CPP_COMMA)
21891 {
21892 cp_parameter_declarator *parmdecl;
21893 tree parm;
21894
21895 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21896 token = cp_lexer_peek_token (parser->lexer);
21897
21898 if (token->type == CPP_ELLIPSIS)
21899 {
21900 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
21901 *ellipsisp = true;
21902 token = cp_lexer_peek_token (parser->lexer);
21903 break;
21904 }
21905
21906 /* TODO: parse attributes for tail parameters. */
21907 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21908 parm = grokdeclarator (parmdecl->declarator,
21909 &parmdecl->decl_specifiers,
21910 PARM, /*initialized=*/0,
21911 /*attrlist=*/NULL);
21912
21913 chainon (params, build_tree_list (NULL_TREE, parm));
21914 token = cp_lexer_peek_token (parser->lexer);
21915 }
21916
21917 /* We allow tail attributes for the method. */
21918 if (token->keyword == RID_ATTRIBUTE)
21919 {
21920 if (*attributes == NULL_TREE)
21921 {
21922 *attributes = cp_parser_attributes_opt (parser);
21923 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21924 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21925 return params;
21926 }
21927 else
21928 /* We have an error, but parse the attributes, so that we can
21929 carry on. */
21930 *attributes = cp_parser_attributes_opt (parser);
21931
21932 cp_parser_error (parser,
21933 "method attributes must be specified at the end");
21934 return error_mark_node;
21935 }
21936
21937 return params;
21938 }
21939
21940 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
21941
21942 static void
21943 cp_parser_objc_interstitial_code (cp_parser* parser)
21944 {
21945 cp_token *token = cp_lexer_peek_token (parser->lexer);
21946
21947 /* If the next token is `extern' and the following token is a string
21948 literal, then we have a linkage specification. */
21949 if (token->keyword == RID_EXTERN
21950 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
21951 cp_parser_linkage_specification (parser);
21952 /* Handle #pragma, if any. */
21953 else if (token->type == CPP_PRAGMA)
21954 cp_parser_pragma (parser, pragma_external);
21955 /* Allow stray semicolons. */
21956 else if (token->type == CPP_SEMICOLON)
21957 cp_lexer_consume_token (parser->lexer);
21958 /* Mark methods as optional or required, when building protocols. */
21959 else if (token->keyword == RID_AT_OPTIONAL)
21960 {
21961 cp_lexer_consume_token (parser->lexer);
21962 objc_set_method_opt (true);
21963 }
21964 else if (token->keyword == RID_AT_REQUIRED)
21965 {
21966 cp_lexer_consume_token (parser->lexer);
21967 objc_set_method_opt (false);
21968 }
21969 else if (token->keyword == RID_NAMESPACE)
21970 cp_parser_namespace_definition (parser);
21971 /* Other stray characters must generate errors. */
21972 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
21973 {
21974 cp_lexer_consume_token (parser->lexer);
21975 error ("stray %qs between Objective-C++ methods",
21976 token->type == CPP_OPEN_BRACE ? "{" : "}");
21977 }
21978 /* Finally, try to parse a block-declaration, or a function-definition. */
21979 else
21980 cp_parser_block_declaration (parser, /*statement_p=*/false);
21981 }
21982
21983 /* Parse a method signature. */
21984
21985 static tree
21986 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
21987 {
21988 tree rettype, kwdparms, optparms;
21989 bool ellipsis = false;
21990 bool is_class_method;
21991
21992 is_class_method = cp_parser_objc_method_type (parser);
21993 rettype = cp_parser_objc_typename (parser);
21994 *attributes = NULL_TREE;
21995 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
21996 if (kwdparms == error_mark_node)
21997 return error_mark_node;
21998 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
21999 if (optparms == error_mark_node)
22000 return error_mark_node;
22001
22002 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22003 }
22004
22005 static bool
22006 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22007 {
22008 tree tattr;
22009 cp_lexer_save_tokens (parser->lexer);
22010 tattr = cp_parser_attributes_opt (parser);
22011 gcc_assert (tattr) ;
22012
22013 /* If the attributes are followed by a method introducer, this is not allowed.
22014 Dump the attributes and flag the situation. */
22015 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22016 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22017 return true;
22018
22019 /* Otherwise, the attributes introduce some interstitial code, possibly so
22020 rewind to allow that check. */
22021 cp_lexer_rollback_tokens (parser->lexer);
22022 return false;
22023 }
22024
22025 /* Parse an Objective-C method prototype list. */
22026
22027 static void
22028 cp_parser_objc_method_prototype_list (cp_parser* parser)
22029 {
22030 cp_token *token = cp_lexer_peek_token (parser->lexer);
22031
22032 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22033 {
22034 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22035 {
22036 tree attributes, sig;
22037 bool is_class_method;
22038 if (token->type == CPP_PLUS)
22039 is_class_method = true;
22040 else
22041 is_class_method = false;
22042 sig = cp_parser_objc_method_signature (parser, &attributes);
22043 if (sig == error_mark_node)
22044 {
22045 cp_parser_skip_to_end_of_block_or_statement (parser);
22046 token = cp_lexer_peek_token (parser->lexer);
22047 continue;
22048 }
22049 objc_add_method_declaration (is_class_method, sig, attributes);
22050 cp_parser_consume_semicolon_at_end_of_statement (parser);
22051 }
22052 else if (token->keyword == RID_AT_PROPERTY)
22053 cp_parser_objc_at_property_declaration (parser);
22054 else if (token->keyword == RID_ATTRIBUTE
22055 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22056 warning_at (cp_lexer_peek_token (parser->lexer)->location,
22057 OPT_Wattributes,
22058 "prefix attributes are ignored for methods");
22059 else
22060 /* Allow for interspersed non-ObjC++ code. */
22061 cp_parser_objc_interstitial_code (parser);
22062
22063 token = cp_lexer_peek_token (parser->lexer);
22064 }
22065
22066 if (token->type != CPP_EOF)
22067 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22068 else
22069 cp_parser_error (parser, "expected %<@end%>");
22070
22071 objc_finish_interface ();
22072 }
22073
22074 /* Parse an Objective-C method definition list. */
22075
22076 static void
22077 cp_parser_objc_method_definition_list (cp_parser* parser)
22078 {
22079 cp_token *token = cp_lexer_peek_token (parser->lexer);
22080
22081 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22082 {
22083 tree meth;
22084
22085 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22086 {
22087 cp_token *ptk;
22088 tree sig, attribute;
22089 bool is_class_method;
22090 if (token->type == CPP_PLUS)
22091 is_class_method = true;
22092 else
22093 is_class_method = false;
22094 push_deferring_access_checks (dk_deferred);
22095 sig = cp_parser_objc_method_signature (parser, &attribute);
22096 if (sig == error_mark_node)
22097 {
22098 cp_parser_skip_to_end_of_block_or_statement (parser);
22099 token = cp_lexer_peek_token (parser->lexer);
22100 continue;
22101 }
22102 objc_start_method_definition (is_class_method, sig, attribute);
22103
22104 /* For historical reasons, we accept an optional semicolon. */
22105 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22106 cp_lexer_consume_token (parser->lexer);
22107
22108 ptk = cp_lexer_peek_token (parser->lexer);
22109 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
22110 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22111 {
22112 perform_deferred_access_checks ();
22113 stop_deferring_access_checks ();
22114 meth = cp_parser_function_definition_after_declarator (parser,
22115 false);
22116 pop_deferring_access_checks ();
22117 objc_finish_method_definition (meth);
22118 }
22119 }
22120 /* The following case will be removed once @synthesize is
22121 completely implemented. */
22122 else if (token->keyword == RID_AT_PROPERTY)
22123 cp_parser_objc_at_property_declaration (parser);
22124 else if (token->keyword == RID_AT_SYNTHESIZE)
22125 cp_parser_objc_at_synthesize_declaration (parser);
22126 else if (token->keyword == RID_AT_DYNAMIC)
22127 cp_parser_objc_at_dynamic_declaration (parser);
22128 else if (token->keyword == RID_ATTRIBUTE
22129 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22130 warning_at (token->location, OPT_Wattributes,
22131 "prefix attributes are ignored for methods");
22132 else
22133 /* Allow for interspersed non-ObjC++ code. */
22134 cp_parser_objc_interstitial_code (parser);
22135
22136 token = cp_lexer_peek_token (parser->lexer);
22137 }
22138
22139 if (token->type != CPP_EOF)
22140 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22141 else
22142 cp_parser_error (parser, "expected %<@end%>");
22143
22144 objc_finish_implementation ();
22145 }
22146
22147 /* Parse Objective-C ivars. */
22148
22149 static void
22150 cp_parser_objc_class_ivars (cp_parser* parser)
22151 {
22152 cp_token *token = cp_lexer_peek_token (parser->lexer);
22153
22154 if (token->type != CPP_OPEN_BRACE)
22155 return; /* No ivars specified. */
22156
22157 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
22158 token = cp_lexer_peek_token (parser->lexer);
22159
22160 while (token->type != CPP_CLOSE_BRACE
22161 && token->keyword != RID_AT_END && token->type != CPP_EOF)
22162 {
22163 cp_decl_specifier_seq declspecs;
22164 int decl_class_or_enum_p;
22165 tree prefix_attributes;
22166
22167 cp_parser_objc_visibility_spec (parser);
22168
22169 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22170 break;
22171
22172 cp_parser_decl_specifier_seq (parser,
22173 CP_PARSER_FLAGS_OPTIONAL,
22174 &declspecs,
22175 &decl_class_or_enum_p);
22176
22177 /* auto, register, static, extern, mutable. */
22178 if (declspecs.storage_class != sc_none)
22179 {
22180 cp_parser_error (parser, "invalid type for instance variable");
22181 declspecs.storage_class = sc_none;
22182 }
22183
22184 /* __thread. */
22185 if (declspecs.specs[(int) ds_thread])
22186 {
22187 cp_parser_error (parser, "invalid type for instance variable");
22188 declspecs.specs[(int) ds_thread] = 0;
22189 }
22190
22191 /* typedef. */
22192 if (declspecs.specs[(int) ds_typedef])
22193 {
22194 cp_parser_error (parser, "invalid type for instance variable");
22195 declspecs.specs[(int) ds_typedef] = 0;
22196 }
22197
22198 prefix_attributes = declspecs.attributes;
22199 declspecs.attributes = NULL_TREE;
22200
22201 /* Keep going until we hit the `;' at the end of the
22202 declaration. */
22203 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22204 {
22205 tree width = NULL_TREE, attributes, first_attribute, decl;
22206 cp_declarator *declarator = NULL;
22207 int ctor_dtor_or_conv_p;
22208
22209 /* Check for a (possibly unnamed) bitfield declaration. */
22210 token = cp_lexer_peek_token (parser->lexer);
22211 if (token->type == CPP_COLON)
22212 goto eat_colon;
22213
22214 if (token->type == CPP_NAME
22215 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22216 == CPP_COLON))
22217 {
22218 /* Get the name of the bitfield. */
22219 declarator = make_id_declarator (NULL_TREE,
22220 cp_parser_identifier (parser),
22221 sfk_none);
22222
22223 eat_colon:
22224 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22225 /* Get the width of the bitfield. */
22226 width
22227 = cp_parser_constant_expression (parser,
22228 /*allow_non_constant=*/false,
22229 NULL);
22230 }
22231 else
22232 {
22233 /* Parse the declarator. */
22234 declarator
22235 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22236 &ctor_dtor_or_conv_p,
22237 /*parenthesized_p=*/NULL,
22238 /*member_p=*/false);
22239 }
22240
22241 /* Look for attributes that apply to the ivar. */
22242 attributes = cp_parser_attributes_opt (parser);
22243 /* Remember which attributes are prefix attributes and
22244 which are not. */
22245 first_attribute = attributes;
22246 /* Combine the attributes. */
22247 attributes = chainon (prefix_attributes, attributes);
22248
22249 if (width)
22250 /* Create the bitfield declaration. */
22251 decl = grokbitfield (declarator, &declspecs,
22252 width,
22253 attributes);
22254 else
22255 decl = grokfield (declarator, &declspecs,
22256 NULL_TREE, /*init_const_expr_p=*/false,
22257 NULL_TREE, attributes);
22258
22259 /* Add the instance variable. */
22260 objc_add_instance_variable (decl);
22261
22262 /* Reset PREFIX_ATTRIBUTES. */
22263 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22264 attributes = TREE_CHAIN (attributes);
22265 if (attributes)
22266 TREE_CHAIN (attributes) = NULL_TREE;
22267
22268 token = cp_lexer_peek_token (parser->lexer);
22269
22270 if (token->type == CPP_COMMA)
22271 {
22272 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22273 continue;
22274 }
22275 break;
22276 }
22277
22278 cp_parser_consume_semicolon_at_end_of_statement (parser);
22279 token = cp_lexer_peek_token (parser->lexer);
22280 }
22281
22282 if (token->keyword == RID_AT_END)
22283 cp_parser_error (parser, "expected %<}%>");
22284
22285 /* Do not consume the RID_AT_END, so it will be read again as terminating
22286 the @interface of @implementation. */
22287 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22288 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
22289
22290 /* For historical reasons, we accept an optional semicolon. */
22291 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22292 cp_lexer_consume_token (parser->lexer);
22293 }
22294
22295 /* Parse an Objective-C protocol declaration. */
22296
22297 static void
22298 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22299 {
22300 tree proto, protorefs;
22301 cp_token *tok;
22302
22303 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
22304 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22305 {
22306 tok = cp_lexer_peek_token (parser->lexer);
22307 error_at (tok->location, "identifier expected after %<@protocol%>");
22308 goto finish;
22309 }
22310
22311 /* See if we have a forward declaration or a definition. */
22312 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22313
22314 /* Try a forward declaration first. */
22315 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22316 {
22317 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
22318 finish:
22319 cp_parser_consume_semicolon_at_end_of_statement (parser);
22320 }
22321
22322 /* Ok, we got a full-fledged definition (or at least should). */
22323 else
22324 {
22325 proto = cp_parser_identifier (parser);
22326 protorefs = cp_parser_objc_protocol_refs_opt (parser);
22327 objc_start_protocol (proto, protorefs, attributes);
22328 cp_parser_objc_method_prototype_list (parser);
22329 }
22330 }
22331
22332 /* Parse an Objective-C superclass or category. */
22333
22334 static void
22335 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
22336 tree *categ)
22337 {
22338 cp_token *next = cp_lexer_peek_token (parser->lexer);
22339
22340 *super = *categ = NULL_TREE;
22341 if (next->type == CPP_COLON)
22342 {
22343 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22344 *super = cp_parser_identifier (parser);
22345 }
22346 else if (next->type == CPP_OPEN_PAREN)
22347 {
22348 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
22349 *categ = cp_parser_identifier (parser);
22350 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22351 }
22352 }
22353
22354 /* Parse an Objective-C class interface. */
22355
22356 static void
22357 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22358 {
22359 tree name, super, categ, protos;
22360
22361 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
22362 name = cp_parser_identifier (parser);
22363 if (name == error_mark_node)
22364 {
22365 /* It's hard to recover because even if valid @interface stuff
22366 is to follow, we can't compile it (or validate it) if we
22367 don't even know which class it refers to. Let's assume this
22368 was a stray '@interface' token in the stream and skip it.
22369 */
22370 return;
22371 }
22372 cp_parser_objc_superclass_or_category (parser, &super, &categ);
22373 protos = cp_parser_objc_protocol_refs_opt (parser);
22374
22375 /* We have either a class or a category on our hands. */
22376 if (categ)
22377 objc_start_category_interface (name, categ, protos, attributes);
22378 else
22379 {
22380 objc_start_class_interface (name, super, protos, attributes);
22381 /* Handle instance variable declarations, if any. */
22382 cp_parser_objc_class_ivars (parser);
22383 objc_continue_interface ();
22384 }
22385
22386 cp_parser_objc_method_prototype_list (parser);
22387 }
22388
22389 /* Parse an Objective-C class implementation. */
22390
22391 static void
22392 cp_parser_objc_class_implementation (cp_parser* parser)
22393 {
22394 tree name, super, categ;
22395
22396 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
22397 name = cp_parser_identifier (parser);
22398 if (name == error_mark_node)
22399 {
22400 /* It's hard to recover because even if valid @implementation
22401 stuff is to follow, we can't compile it (or validate it) if
22402 we don't even know which class it refers to. Let's assume
22403 this was a stray '@implementation' token in the stream and
22404 skip it.
22405 */
22406 return;
22407 }
22408 cp_parser_objc_superclass_or_category (parser, &super, &categ);
22409
22410 /* We have either a class or a category on our hands. */
22411 if (categ)
22412 objc_start_category_implementation (name, categ);
22413 else
22414 {
22415 objc_start_class_implementation (name, super);
22416 /* Handle instance variable declarations, if any. */
22417 cp_parser_objc_class_ivars (parser);
22418 objc_continue_implementation ();
22419 }
22420
22421 cp_parser_objc_method_definition_list (parser);
22422 }
22423
22424 /* Consume the @end token and finish off the implementation. */
22425
22426 static void
22427 cp_parser_objc_end_implementation (cp_parser* parser)
22428 {
22429 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22430 objc_finish_implementation ();
22431 }
22432
22433 /* Parse an Objective-C declaration. */
22434
22435 static void
22436 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22437 {
22438 /* Try to figure out what kind of declaration is present. */
22439 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22440
22441 if (attributes)
22442 switch (kwd->keyword)
22443 {
22444 case RID_AT_ALIAS:
22445 case RID_AT_CLASS:
22446 case RID_AT_END:
22447 error_at (kwd->location, "attributes may not be specified before"
22448 " the %<@%D%> Objective-C++ keyword",
22449 kwd->u.value);
22450 attributes = NULL;
22451 break;
22452 case RID_AT_IMPLEMENTATION:
22453 warning_at (kwd->location, OPT_Wattributes,
22454 "prefix attributes are ignored before %<@%D%>",
22455 kwd->u.value);
22456 attributes = NULL;
22457 default:
22458 break;
22459 }
22460
22461 switch (kwd->keyword)
22462 {
22463 case RID_AT_ALIAS:
22464 cp_parser_objc_alias_declaration (parser);
22465 break;
22466 case RID_AT_CLASS:
22467 cp_parser_objc_class_declaration (parser);
22468 break;
22469 case RID_AT_PROTOCOL:
22470 cp_parser_objc_protocol_declaration (parser, attributes);
22471 break;
22472 case RID_AT_INTERFACE:
22473 cp_parser_objc_class_interface (parser, attributes);
22474 break;
22475 case RID_AT_IMPLEMENTATION:
22476 cp_parser_objc_class_implementation (parser);
22477 break;
22478 case RID_AT_END:
22479 cp_parser_objc_end_implementation (parser);
22480 break;
22481 default:
22482 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22483 kwd->u.value);
22484 cp_parser_skip_to_end_of_block_or_statement (parser);
22485 }
22486 }
22487
22488 /* Parse an Objective-C try-catch-finally statement.
22489
22490 objc-try-catch-finally-stmt:
22491 @try compound-statement objc-catch-clause-seq [opt]
22492 objc-finally-clause [opt]
22493
22494 objc-catch-clause-seq:
22495 objc-catch-clause objc-catch-clause-seq [opt]
22496
22497 objc-catch-clause:
22498 @catch ( exception-declaration ) compound-statement
22499
22500 objc-finally-clause
22501 @finally compound-statement
22502
22503 Returns NULL_TREE. */
22504
22505 static tree
22506 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
22507 location_t location;
22508 tree stmt;
22509
22510 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22511 location = cp_lexer_peek_token (parser->lexer)->location;
22512 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22513 node, lest it get absorbed into the surrounding block. */
22514 stmt = push_stmt_list ();
22515 cp_parser_compound_statement (parser, NULL, false);
22516 objc_begin_try_stmt (location, pop_stmt_list (stmt));
22517
22518 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22519 {
22520 cp_parameter_declarator *parmdecl;
22521 tree parm;
22522
22523 cp_lexer_consume_token (parser->lexer);
22524 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22525 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22526 parm = grokdeclarator (parmdecl->declarator,
22527 &parmdecl->decl_specifiers,
22528 PARM, /*initialized=*/0,
22529 /*attrlist=*/NULL);
22530 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22531 objc_begin_catch_clause (parm);
22532 cp_parser_compound_statement (parser, NULL, false);
22533 objc_finish_catch_clause ();
22534 }
22535
22536 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22537 {
22538 cp_lexer_consume_token (parser->lexer);
22539 location = cp_lexer_peek_token (parser->lexer)->location;
22540 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22541 node, lest it get absorbed into the surrounding block. */
22542 stmt = push_stmt_list ();
22543 cp_parser_compound_statement (parser, NULL, false);
22544 objc_build_finally_clause (location, pop_stmt_list (stmt));
22545 }
22546
22547 return objc_finish_try_stmt ();
22548 }
22549
22550 /* Parse an Objective-C synchronized statement.
22551
22552 objc-synchronized-stmt:
22553 @synchronized ( expression ) compound-statement
22554
22555 Returns NULL_TREE. */
22556
22557 static tree
22558 cp_parser_objc_synchronized_statement (cp_parser *parser) {
22559 location_t location;
22560 tree lock, stmt;
22561
22562 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22563
22564 location = cp_lexer_peek_token (parser->lexer)->location;
22565 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22566 lock = cp_parser_expression (parser, false, NULL);
22567 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22568
22569 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22570 node, lest it get absorbed into the surrounding block. */
22571 stmt = push_stmt_list ();
22572 cp_parser_compound_statement (parser, NULL, false);
22573
22574 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22575 }
22576
22577 /* Parse an Objective-C throw statement.
22578
22579 objc-throw-stmt:
22580 @throw assignment-expression [opt] ;
22581
22582 Returns a constructed '@throw' statement. */
22583
22584 static tree
22585 cp_parser_objc_throw_statement (cp_parser *parser) {
22586 tree expr = NULL_TREE;
22587 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22588
22589 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22590
22591 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22592 expr = cp_parser_assignment_expression (parser, false, NULL);
22593
22594 cp_parser_consume_semicolon_at_end_of_statement (parser);
22595
22596 return objc_build_throw_stmt (loc, expr);
22597 }
22598
22599 /* Parse an Objective-C statement. */
22600
22601 static tree
22602 cp_parser_objc_statement (cp_parser * parser) {
22603 /* Try to figure out what kind of declaration is present. */
22604 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22605
22606 switch (kwd->keyword)
22607 {
22608 case RID_AT_TRY:
22609 return cp_parser_objc_try_catch_finally_statement (parser);
22610 case RID_AT_SYNCHRONIZED:
22611 return cp_parser_objc_synchronized_statement (parser);
22612 case RID_AT_THROW:
22613 return cp_parser_objc_throw_statement (parser);
22614 default:
22615 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22616 kwd->u.value);
22617 cp_parser_skip_to_end_of_block_or_statement (parser);
22618 }
22619
22620 return error_mark_node;
22621 }
22622
22623 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22624 look ahead to see if an objc keyword follows the attributes. This
22625 is to detect the use of prefix attributes on ObjC @interface and
22626 @protocol. */
22627
22628 static bool
22629 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22630 {
22631 cp_lexer_save_tokens (parser->lexer);
22632 *attrib = cp_parser_attributes_opt (parser);
22633 gcc_assert (*attrib);
22634 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22635 {
22636 cp_lexer_commit_tokens (parser->lexer);
22637 return true;
22638 }
22639 cp_lexer_rollback_tokens (parser->lexer);
22640 return false;
22641 }
22642
22643 /* This routine is a minimal replacement for
22644 c_parser_struct_declaration () used when parsing the list of
22645 types/names or ObjC++ properties. For example, when parsing the
22646 code
22647
22648 @property (readonly) int a, b, c;
22649
22650 this function is responsible for parsing "int a, int b, int c" and
22651 returning the declarations as CHAIN of DECLs.
22652
22653 TODO: Share this code with cp_parser_objc_class_ivars. It's very
22654 similar parsing. */
22655 static tree
22656 cp_parser_objc_struct_declaration (cp_parser *parser)
22657 {
22658 tree decls = NULL_TREE;
22659 cp_decl_specifier_seq declspecs;
22660 int decl_class_or_enum_p;
22661 tree prefix_attributes;
22662
22663 cp_parser_decl_specifier_seq (parser,
22664 CP_PARSER_FLAGS_NONE,
22665 &declspecs,
22666 &decl_class_or_enum_p);
22667
22668 if (declspecs.type == error_mark_node)
22669 return error_mark_node;
22670
22671 /* auto, register, static, extern, mutable. */
22672 if (declspecs.storage_class != sc_none)
22673 {
22674 cp_parser_error (parser, "invalid type for property");
22675 declspecs.storage_class = sc_none;
22676 }
22677
22678 /* __thread. */
22679 if (declspecs.specs[(int) ds_thread])
22680 {
22681 cp_parser_error (parser, "invalid type for property");
22682 declspecs.specs[(int) ds_thread] = 0;
22683 }
22684
22685 /* typedef. */
22686 if (declspecs.specs[(int) ds_typedef])
22687 {
22688 cp_parser_error (parser, "invalid type for property");
22689 declspecs.specs[(int) ds_typedef] = 0;
22690 }
22691
22692 prefix_attributes = declspecs.attributes;
22693 declspecs.attributes = NULL_TREE;
22694
22695 /* Keep going until we hit the `;' at the end of the declaration. */
22696 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22697 {
22698 tree attributes, first_attribute, decl;
22699 cp_declarator *declarator;
22700 cp_token *token;
22701
22702 /* Parse the declarator. */
22703 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22704 NULL, NULL, false);
22705
22706 /* Look for attributes that apply to the ivar. */
22707 attributes = cp_parser_attributes_opt (parser);
22708 /* Remember which attributes are prefix attributes and
22709 which are not. */
22710 first_attribute = attributes;
22711 /* Combine the attributes. */
22712 attributes = chainon (prefix_attributes, attributes);
22713
22714 decl = grokfield (declarator, &declspecs,
22715 NULL_TREE, /*init_const_expr_p=*/false,
22716 NULL_TREE, attributes);
22717
22718 if (decl == error_mark_node || decl == NULL_TREE)
22719 return error_mark_node;
22720
22721 /* Reset PREFIX_ATTRIBUTES. */
22722 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22723 attributes = TREE_CHAIN (attributes);
22724 if (attributes)
22725 TREE_CHAIN (attributes) = NULL_TREE;
22726
22727 DECL_CHAIN (decl) = decls;
22728 decls = decl;
22729
22730 token = cp_lexer_peek_token (parser->lexer);
22731 if (token->type == CPP_COMMA)
22732 {
22733 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22734 continue;
22735 }
22736 else
22737 break;
22738 }
22739 return decls;
22740 }
22741
22742 /* Parse an Objective-C @property declaration. The syntax is:
22743
22744 objc-property-declaration:
22745 '@property' objc-property-attributes[opt] struct-declaration ;
22746
22747 objc-property-attributes:
22748 '(' objc-property-attribute-list ')'
22749
22750 objc-property-attribute-list:
22751 objc-property-attribute
22752 objc-property-attribute-list, objc-property-attribute
22753
22754 objc-property-attribute
22755 'getter' = identifier
22756 'setter' = identifier
22757 'readonly'
22758 'readwrite'
22759 'assign'
22760 'retain'
22761 'copy'
22762 'nonatomic'
22763
22764 For example:
22765 @property NSString *name;
22766 @property (readonly) id object;
22767 @property (retain, nonatomic, getter=getTheName) id name;
22768 @property int a, b, c;
22769
22770 PS: This function is identical to
22771 c_parser_objc_at_property_declaration for C. Keep them in sync. */
22772 static void
22773 cp_parser_objc_at_property_declaration (cp_parser *parser)
22774 {
22775 /* The following variables hold the attributes of the properties as
22776 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
22777 seen. When we see an attribute, we set them to 'true' (if they
22778 are boolean properties) or to the identifier (if they have an
22779 argument, ie, for getter and setter). Note that here we only
22780 parse the list of attributes, check the syntax and accumulate the
22781 attributes that we find. objc_add_property_declaration() will
22782 then process the information. */
22783 bool property_assign = false;
22784 bool property_copy = false;
22785 tree property_getter_ident = NULL_TREE;
22786 bool property_nonatomic = false;
22787 bool property_readonly = false;
22788 bool property_readwrite = false;
22789 bool property_retain = false;
22790 tree property_setter_ident = NULL_TREE;
22791
22792 /* 'properties' is the list of properties that we read. Usually a
22793 single one, but maybe more (eg, in "@property int a, b, c;" there
22794 are three). */
22795 tree properties;
22796 location_t loc;
22797
22798 loc = cp_lexer_peek_token (parser->lexer)->location;
22799
22800 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
22801
22802 /* Parse the optional attribute list... */
22803 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22804 {
22805 /* Eat the '('. */
22806 cp_lexer_consume_token (parser->lexer);
22807
22808 while (true)
22809 {
22810 bool syntax_error = false;
22811 cp_token *token = cp_lexer_peek_token (parser->lexer);
22812 enum rid keyword;
22813
22814 if (token->type != CPP_NAME)
22815 {
22816 cp_parser_error (parser, "expected identifier");
22817 break;
22818 }
22819 keyword = C_RID_CODE (token->u.value);
22820 cp_lexer_consume_token (parser->lexer);
22821 switch (keyword)
22822 {
22823 case RID_ASSIGN: property_assign = true; break;
22824 case RID_COPY: property_copy = true; break;
22825 case RID_NONATOMIC: property_nonatomic = true; break;
22826 case RID_READONLY: property_readonly = true; break;
22827 case RID_READWRITE: property_readwrite = true; break;
22828 case RID_RETAIN: property_retain = true; break;
22829
22830 case RID_GETTER:
22831 case RID_SETTER:
22832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22833 {
22834 cp_parser_error (parser,
22835 "getter/setter/ivar attribute must be followed by %<=%>");
22836 syntax_error = true;
22837 break;
22838 }
22839 cp_lexer_consume_token (parser->lexer); /* eat the = */
22840 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22841 {
22842 cp_parser_error (parser, "expected identifier");
22843 syntax_error = true;
22844 break;
22845 }
22846 if (keyword == RID_SETTER)
22847 {
22848 if (property_setter_ident != NULL_TREE)
22849 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
22850 else
22851 property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22852 cp_lexer_consume_token (parser->lexer);
22853 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22854 cp_parser_error (parser, "setter name must terminate with %<:%>");
22855 else
22856 cp_lexer_consume_token (parser->lexer);
22857 }
22858 else
22859 {
22860 if (property_getter_ident != NULL_TREE)
22861 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
22862 else
22863 property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22864 cp_lexer_consume_token (parser->lexer);
22865 }
22866 break;
22867 default:
22868 cp_parser_error (parser, "unknown property attribute");
22869 syntax_error = true;
22870 break;
22871 }
22872
22873 if (syntax_error)
22874 break;
22875
22876 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22877 cp_lexer_consume_token (parser->lexer);
22878 else
22879 break;
22880 }
22881
22882 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22883 {
22884 cp_parser_skip_to_closing_parenthesis (parser,
22885 /*recovering=*/true,
22886 /*or_comma=*/false,
22887 /*consume_paren=*/true);
22888 }
22889 }
22890
22891 /* ... and the property declaration(s). */
22892 properties = cp_parser_objc_struct_declaration (parser);
22893
22894 if (properties == error_mark_node)
22895 {
22896 cp_parser_skip_to_end_of_statement (parser);
22897 /* If the next token is now a `;', consume it. */
22898 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22899 cp_lexer_consume_token (parser->lexer);
22900 return;
22901 }
22902
22903 if (properties == NULL_TREE)
22904 cp_parser_error (parser, "expected identifier");
22905 else
22906 {
22907 /* Comma-separated properties are chained together in
22908 reverse order; add them one by one. */
22909 properties = nreverse (properties);
22910
22911 for (; properties; properties = TREE_CHAIN (properties))
22912 objc_add_property_declaration (loc, copy_node (properties),
22913 property_readonly, property_readwrite,
22914 property_assign, property_retain,
22915 property_copy, property_nonatomic,
22916 property_getter_ident, property_setter_ident);
22917 }
22918
22919 cp_parser_consume_semicolon_at_end_of_statement (parser);
22920 }
22921
22922 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
22923
22924 objc-synthesize-declaration:
22925 @synthesize objc-synthesize-identifier-list ;
22926
22927 objc-synthesize-identifier-list:
22928 objc-synthesize-identifier
22929 objc-synthesize-identifier-list, objc-synthesize-identifier
22930
22931 objc-synthesize-identifier
22932 identifier
22933 identifier = identifier
22934
22935 For example:
22936 @synthesize MyProperty;
22937 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
22938
22939 PS: This function is identical to c_parser_objc_at_synthesize_declaration
22940 for C. Keep them in sync.
22941 */
22942 static void
22943 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
22944 {
22945 tree list = NULL_TREE;
22946 location_t loc;
22947 loc = cp_lexer_peek_token (parser->lexer)->location;
22948
22949 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
22950 while (true)
22951 {
22952 tree property, ivar;
22953 property = cp_parser_identifier (parser);
22954 if (property == error_mark_node)
22955 {
22956 cp_parser_consume_semicolon_at_end_of_statement (parser);
22957 return;
22958 }
22959 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22960 {
22961 cp_lexer_consume_token (parser->lexer);
22962 ivar = cp_parser_identifier (parser);
22963 if (ivar == error_mark_node)
22964 {
22965 cp_parser_consume_semicolon_at_end_of_statement (parser);
22966 return;
22967 }
22968 }
22969 else
22970 ivar = NULL_TREE;
22971 list = chainon (list, build_tree_list (ivar, property));
22972 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22973 cp_lexer_consume_token (parser->lexer);
22974 else
22975 break;
22976 }
22977 cp_parser_consume_semicolon_at_end_of_statement (parser);
22978 objc_add_synthesize_declaration (loc, list);
22979 }
22980
22981 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
22982
22983 objc-dynamic-declaration:
22984 @dynamic identifier-list ;
22985
22986 For example:
22987 @dynamic MyProperty;
22988 @dynamic MyProperty, AnotherProperty;
22989
22990 PS: This function is identical to c_parser_objc_at_dynamic_declaration
22991 for C. Keep them in sync.
22992 */
22993 static void
22994 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
22995 {
22996 tree list = NULL_TREE;
22997 location_t loc;
22998 loc = cp_lexer_peek_token (parser->lexer)->location;
22999
23000 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
23001 while (true)
23002 {
23003 tree property;
23004 property = cp_parser_identifier (parser);
23005 if (property == error_mark_node)
23006 {
23007 cp_parser_consume_semicolon_at_end_of_statement (parser);
23008 return;
23009 }
23010 list = chainon (list, build_tree_list (NULL, property));
23011 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23012 cp_lexer_consume_token (parser->lexer);
23013 else
23014 break;
23015 }
23016 cp_parser_consume_semicolon_at_end_of_statement (parser);
23017 objc_add_dynamic_declaration (loc, list);
23018 }
23019
23020 \f
23021 /* OpenMP 2.5 parsing routines. */
23022
23023 /* Returns name of the next clause.
23024 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23025 the token is not consumed. Otherwise appropriate pragma_omp_clause is
23026 returned and the token is consumed. */
23027
23028 static pragma_omp_clause
23029 cp_parser_omp_clause_name (cp_parser *parser)
23030 {
23031 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23032
23033 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23034 result = PRAGMA_OMP_CLAUSE_IF;
23035 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23036 result = PRAGMA_OMP_CLAUSE_DEFAULT;
23037 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23038 result = PRAGMA_OMP_CLAUSE_PRIVATE;
23039 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23040 {
23041 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23042 const char *p = IDENTIFIER_POINTER (id);
23043
23044 switch (p[0])
23045 {
23046 case 'c':
23047 if (!strcmp ("collapse", p))
23048 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23049 else if (!strcmp ("copyin", p))
23050 result = PRAGMA_OMP_CLAUSE_COPYIN;
23051 else if (!strcmp ("copyprivate", p))
23052 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23053 break;
23054 case 'f':
23055 if (!strcmp ("firstprivate", p))
23056 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23057 break;
23058 case 'l':
23059 if (!strcmp ("lastprivate", p))
23060 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23061 break;
23062 case 'n':
23063 if (!strcmp ("nowait", p))
23064 result = PRAGMA_OMP_CLAUSE_NOWAIT;
23065 else if (!strcmp ("num_threads", p))
23066 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23067 break;
23068 case 'o':
23069 if (!strcmp ("ordered", p))
23070 result = PRAGMA_OMP_CLAUSE_ORDERED;
23071 break;
23072 case 'r':
23073 if (!strcmp ("reduction", p))
23074 result = PRAGMA_OMP_CLAUSE_REDUCTION;
23075 break;
23076 case 's':
23077 if (!strcmp ("schedule", p))
23078 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23079 else if (!strcmp ("shared", p))
23080 result = PRAGMA_OMP_CLAUSE_SHARED;
23081 break;
23082 case 'u':
23083 if (!strcmp ("untied", p))
23084 result = PRAGMA_OMP_CLAUSE_UNTIED;
23085 break;
23086 }
23087 }
23088
23089 if (result != PRAGMA_OMP_CLAUSE_NONE)
23090 cp_lexer_consume_token (parser->lexer);
23091
23092 return result;
23093 }
23094
23095 /* Validate that a clause of the given type does not already exist. */
23096
23097 static void
23098 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23099 const char *name, location_t location)
23100 {
23101 tree c;
23102
23103 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23104 if (OMP_CLAUSE_CODE (c) == code)
23105 {
23106 error_at (location, "too many %qs clauses", name);
23107 break;
23108 }
23109 }
23110
23111 /* OpenMP 2.5:
23112 variable-list:
23113 identifier
23114 variable-list , identifier
23115
23116 In addition, we match a closing parenthesis. An opening parenthesis
23117 will have been consumed by the caller.
23118
23119 If KIND is nonzero, create the appropriate node and install the decl
23120 in OMP_CLAUSE_DECL and add the node to the head of the list.
23121
23122 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23123 return the list created. */
23124
23125 static tree
23126 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23127 tree list)
23128 {
23129 cp_token *token;
23130 while (1)
23131 {
23132 tree name, decl;
23133
23134 token = cp_lexer_peek_token (parser->lexer);
23135 name = cp_parser_id_expression (parser, /*template_p=*/false,
23136 /*check_dependency_p=*/true,
23137 /*template_p=*/NULL,
23138 /*declarator_p=*/false,
23139 /*optional_p=*/false);
23140 if (name == error_mark_node)
23141 goto skip_comma;
23142
23143 decl = cp_parser_lookup_name_simple (parser, name, token->location);
23144 if (decl == error_mark_node)
23145 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23146 token->location);
23147 else if (kind != 0)
23148 {
23149 tree u = build_omp_clause (token->location, kind);
23150 OMP_CLAUSE_DECL (u) = decl;
23151 OMP_CLAUSE_CHAIN (u) = list;
23152 list = u;
23153 }
23154 else
23155 list = tree_cons (decl, NULL_TREE, list);
23156
23157 get_comma:
23158 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23159 break;
23160 cp_lexer_consume_token (parser->lexer);
23161 }
23162
23163 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23164 {
23165 int ending;
23166
23167 /* Try to resync to an unnested comma. Copied from
23168 cp_parser_parenthesized_expression_list. */
23169 skip_comma:
23170 ending = cp_parser_skip_to_closing_parenthesis (parser,
23171 /*recovering=*/true,
23172 /*or_comma=*/true,
23173 /*consume_paren=*/true);
23174 if (ending < 0)
23175 goto get_comma;
23176 }
23177
23178 return list;
23179 }
23180
23181 /* Similarly, but expect leading and trailing parenthesis. This is a very
23182 common case for omp clauses. */
23183
23184 static tree
23185 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23186 {
23187 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23188 return cp_parser_omp_var_list_no_open (parser, kind, list);
23189 return list;
23190 }
23191
23192 /* OpenMP 3.0:
23193 collapse ( constant-expression ) */
23194
23195 static tree
23196 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23197 {
23198 tree c, num;
23199 location_t loc;
23200 HOST_WIDE_INT n;
23201
23202 loc = cp_lexer_peek_token (parser->lexer)->location;
23203 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23204 return list;
23205
23206 num = cp_parser_constant_expression (parser, false, NULL);
23207
23208 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23209 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23210 /*or_comma=*/false,
23211 /*consume_paren=*/true);
23212
23213 if (num == error_mark_node)
23214 return list;
23215 num = fold_non_dependent_expr (num);
23216 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23217 || !host_integerp (num, 0)
23218 || (n = tree_low_cst (num, 0)) <= 0
23219 || (int) n != n)
23220 {
23221 error_at (loc, "collapse argument needs positive constant integer expression");
23222 return list;
23223 }
23224
23225 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23226 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23227 OMP_CLAUSE_CHAIN (c) = list;
23228 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23229
23230 return c;
23231 }
23232
23233 /* OpenMP 2.5:
23234 default ( shared | none ) */
23235
23236 static tree
23237 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23238 {
23239 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23240 tree c;
23241
23242 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23243 return list;
23244 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23245 {
23246 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23247 const char *p = IDENTIFIER_POINTER (id);
23248
23249 switch (p[0])
23250 {
23251 case 'n':
23252 if (strcmp ("none", p) != 0)
23253 goto invalid_kind;
23254 kind = OMP_CLAUSE_DEFAULT_NONE;
23255 break;
23256
23257 case 's':
23258 if (strcmp ("shared", p) != 0)
23259 goto invalid_kind;
23260 kind = OMP_CLAUSE_DEFAULT_SHARED;
23261 break;
23262
23263 default:
23264 goto invalid_kind;
23265 }
23266
23267 cp_lexer_consume_token (parser->lexer);
23268 }
23269 else
23270 {
23271 invalid_kind:
23272 cp_parser_error (parser, "expected %<none%> or %<shared%>");
23273 }
23274
23275 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23276 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23277 /*or_comma=*/false,
23278 /*consume_paren=*/true);
23279
23280 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23281 return list;
23282
23283 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23284 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23285 OMP_CLAUSE_CHAIN (c) = list;
23286 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23287
23288 return c;
23289 }
23290
23291 /* OpenMP 2.5:
23292 if ( expression ) */
23293
23294 static tree
23295 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23296 {
23297 tree t, c;
23298
23299 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23300 return list;
23301
23302 t = cp_parser_condition (parser);
23303
23304 if (t == error_mark_node
23305 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23306 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23307 /*or_comma=*/false,
23308 /*consume_paren=*/true);
23309
23310 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23311
23312 c = build_omp_clause (location, OMP_CLAUSE_IF);
23313 OMP_CLAUSE_IF_EXPR (c) = t;
23314 OMP_CLAUSE_CHAIN (c) = list;
23315
23316 return c;
23317 }
23318
23319 /* OpenMP 2.5:
23320 nowait */
23321
23322 static tree
23323 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23324 tree list, location_t location)
23325 {
23326 tree c;
23327
23328 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23329
23330 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23331 OMP_CLAUSE_CHAIN (c) = list;
23332 return c;
23333 }
23334
23335 /* OpenMP 2.5:
23336 num_threads ( expression ) */
23337
23338 static tree
23339 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23340 location_t location)
23341 {
23342 tree t, c;
23343
23344 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23345 return list;
23346
23347 t = cp_parser_expression (parser, false, NULL);
23348
23349 if (t == error_mark_node
23350 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23351 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23352 /*or_comma=*/false,
23353 /*consume_paren=*/true);
23354
23355 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23356 "num_threads", location);
23357
23358 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23359 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23360 OMP_CLAUSE_CHAIN (c) = list;
23361
23362 return c;
23363 }
23364
23365 /* OpenMP 2.5:
23366 ordered */
23367
23368 static tree
23369 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23370 tree list, location_t location)
23371 {
23372 tree c;
23373
23374 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23375 "ordered", location);
23376
23377 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23378 OMP_CLAUSE_CHAIN (c) = list;
23379 return c;
23380 }
23381
23382 /* OpenMP 2.5:
23383 reduction ( reduction-operator : variable-list )
23384
23385 reduction-operator:
23386 One of: + * - & ^ | && || */
23387
23388 static tree
23389 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23390 {
23391 enum tree_code code;
23392 tree nlist, c;
23393
23394 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23395 return list;
23396
23397 switch (cp_lexer_peek_token (parser->lexer)->type)
23398 {
23399 case CPP_PLUS:
23400 code = PLUS_EXPR;
23401 break;
23402 case CPP_MULT:
23403 code = MULT_EXPR;
23404 break;
23405 case CPP_MINUS:
23406 code = MINUS_EXPR;
23407 break;
23408 case CPP_AND:
23409 code = BIT_AND_EXPR;
23410 break;
23411 case CPP_XOR:
23412 code = BIT_XOR_EXPR;
23413 break;
23414 case CPP_OR:
23415 code = BIT_IOR_EXPR;
23416 break;
23417 case CPP_AND_AND:
23418 code = TRUTH_ANDIF_EXPR;
23419 break;
23420 case CPP_OR_OR:
23421 code = TRUTH_ORIF_EXPR;
23422 break;
23423 default:
23424 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23425 "%<|%>, %<&&%>, or %<||%>");
23426 resync_fail:
23427 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23428 /*or_comma=*/false,
23429 /*consume_paren=*/true);
23430 return list;
23431 }
23432 cp_lexer_consume_token (parser->lexer);
23433
23434 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23435 goto resync_fail;
23436
23437 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23438 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23439 OMP_CLAUSE_REDUCTION_CODE (c) = code;
23440
23441 return nlist;
23442 }
23443
23444 /* OpenMP 2.5:
23445 schedule ( schedule-kind )
23446 schedule ( schedule-kind , expression )
23447
23448 schedule-kind:
23449 static | dynamic | guided | runtime | auto */
23450
23451 static tree
23452 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23453 {
23454 tree c, t;
23455
23456 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23457 return list;
23458
23459 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23460
23461 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23462 {
23463 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23464 const char *p = IDENTIFIER_POINTER (id);
23465
23466 switch (p[0])
23467 {
23468 case 'd':
23469 if (strcmp ("dynamic", p) != 0)
23470 goto invalid_kind;
23471 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23472 break;
23473
23474 case 'g':
23475 if (strcmp ("guided", p) != 0)
23476 goto invalid_kind;
23477 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23478 break;
23479
23480 case 'r':
23481 if (strcmp ("runtime", p) != 0)
23482 goto invalid_kind;
23483 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23484 break;
23485
23486 default:
23487 goto invalid_kind;
23488 }
23489 }
23490 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23491 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23492 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23493 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23494 else
23495 goto invalid_kind;
23496 cp_lexer_consume_token (parser->lexer);
23497
23498 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23499 {
23500 cp_token *token;
23501 cp_lexer_consume_token (parser->lexer);
23502
23503 token = cp_lexer_peek_token (parser->lexer);
23504 t = cp_parser_assignment_expression (parser, false, NULL);
23505
23506 if (t == error_mark_node)
23507 goto resync_fail;
23508 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23509 error_at (token->location, "schedule %<runtime%> does not take "
23510 "a %<chunk_size%> parameter");
23511 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23512 error_at (token->location, "schedule %<auto%> does not take "
23513 "a %<chunk_size%> parameter");
23514 else
23515 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23516
23517 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23518 goto resync_fail;
23519 }
23520 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23521 goto resync_fail;
23522
23523 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23524 OMP_CLAUSE_CHAIN (c) = list;
23525 return c;
23526
23527 invalid_kind:
23528 cp_parser_error (parser, "invalid schedule kind");
23529 resync_fail:
23530 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23531 /*or_comma=*/false,
23532 /*consume_paren=*/true);
23533 return list;
23534 }
23535
23536 /* OpenMP 3.0:
23537 untied */
23538
23539 static tree
23540 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23541 tree list, location_t location)
23542 {
23543 tree c;
23544
23545 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23546
23547 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23548 OMP_CLAUSE_CHAIN (c) = list;
23549 return c;
23550 }
23551
23552 /* Parse all OpenMP clauses. The set clauses allowed by the directive
23553 is a bitmask in MASK. Return the list of clauses found; the result
23554 of clause default goes in *pdefault. */
23555
23556 static tree
23557 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23558 const char *where, cp_token *pragma_tok)
23559 {
23560 tree clauses = NULL;
23561 bool first = true;
23562 cp_token *token = NULL;
23563
23564 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23565 {
23566 pragma_omp_clause c_kind;
23567 const char *c_name;
23568 tree prev = clauses;
23569
23570 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23571 cp_lexer_consume_token (parser->lexer);
23572
23573 token = cp_lexer_peek_token (parser->lexer);
23574 c_kind = cp_parser_omp_clause_name (parser);
23575 first = false;
23576
23577 switch (c_kind)
23578 {
23579 case PRAGMA_OMP_CLAUSE_COLLAPSE:
23580 clauses = cp_parser_omp_clause_collapse (parser, clauses,
23581 token->location);
23582 c_name = "collapse";
23583 break;
23584 case PRAGMA_OMP_CLAUSE_COPYIN:
23585 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23586 c_name = "copyin";
23587 break;
23588 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23589 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23590 clauses);
23591 c_name = "copyprivate";
23592 break;
23593 case PRAGMA_OMP_CLAUSE_DEFAULT:
23594 clauses = cp_parser_omp_clause_default (parser, clauses,
23595 token->location);
23596 c_name = "default";
23597 break;
23598 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23599 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23600 clauses);
23601 c_name = "firstprivate";
23602 break;
23603 case PRAGMA_OMP_CLAUSE_IF:
23604 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23605 c_name = "if";
23606 break;
23607 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23608 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23609 clauses);
23610 c_name = "lastprivate";
23611 break;
23612 case PRAGMA_OMP_CLAUSE_NOWAIT:
23613 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23614 c_name = "nowait";
23615 break;
23616 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23617 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23618 token->location);
23619 c_name = "num_threads";
23620 break;
23621 case PRAGMA_OMP_CLAUSE_ORDERED:
23622 clauses = cp_parser_omp_clause_ordered (parser, clauses,
23623 token->location);
23624 c_name = "ordered";
23625 break;
23626 case PRAGMA_OMP_CLAUSE_PRIVATE:
23627 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23628 clauses);
23629 c_name = "private";
23630 break;
23631 case PRAGMA_OMP_CLAUSE_REDUCTION:
23632 clauses = cp_parser_omp_clause_reduction (parser, clauses);
23633 c_name = "reduction";
23634 break;
23635 case PRAGMA_OMP_CLAUSE_SCHEDULE:
23636 clauses = cp_parser_omp_clause_schedule (parser, clauses,
23637 token->location);
23638 c_name = "schedule";
23639 break;
23640 case PRAGMA_OMP_CLAUSE_SHARED:
23641 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23642 clauses);
23643 c_name = "shared";
23644 break;
23645 case PRAGMA_OMP_CLAUSE_UNTIED:
23646 clauses = cp_parser_omp_clause_untied (parser, clauses,
23647 token->location);
23648 c_name = "nowait";
23649 break;
23650 default:
23651 cp_parser_error (parser, "expected %<#pragma omp%> clause");
23652 goto saw_error;
23653 }
23654
23655 if (((mask >> c_kind) & 1) == 0)
23656 {
23657 /* Remove the invalid clause(s) from the list to avoid
23658 confusing the rest of the compiler. */
23659 clauses = prev;
23660 error_at (token->location, "%qs is not valid for %qs", c_name, where);
23661 }
23662 }
23663 saw_error:
23664 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23665 return finish_omp_clauses (clauses);
23666 }
23667
23668 /* OpenMP 2.5:
23669 structured-block:
23670 statement
23671
23672 In practice, we're also interested in adding the statement to an
23673 outer node. So it is convenient if we work around the fact that
23674 cp_parser_statement calls add_stmt. */
23675
23676 static unsigned
23677 cp_parser_begin_omp_structured_block (cp_parser *parser)
23678 {
23679 unsigned save = parser->in_statement;
23680
23681 /* Only move the values to IN_OMP_BLOCK if they weren't false.
23682 This preserves the "not within loop or switch" style error messages
23683 for nonsense cases like
23684 void foo() {
23685 #pragma omp single
23686 break;
23687 }
23688 */
23689 if (parser->in_statement)
23690 parser->in_statement = IN_OMP_BLOCK;
23691
23692 return save;
23693 }
23694
23695 static void
23696 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
23697 {
23698 parser->in_statement = save;
23699 }
23700
23701 static tree
23702 cp_parser_omp_structured_block (cp_parser *parser)
23703 {
23704 tree stmt = begin_omp_structured_block ();
23705 unsigned int save = cp_parser_begin_omp_structured_block (parser);
23706
23707 cp_parser_statement (parser, NULL_TREE, false, NULL);
23708
23709 cp_parser_end_omp_structured_block (parser, save);
23710 return finish_omp_structured_block (stmt);
23711 }
23712
23713 /* OpenMP 2.5:
23714 # pragma omp atomic new-line
23715 expression-stmt
23716
23717 expression-stmt:
23718 x binop= expr | x++ | ++x | x-- | --x
23719 binop:
23720 +, *, -, /, &, ^, |, <<, >>
23721
23722 where x is an lvalue expression with scalar type. */
23723
23724 static void
23725 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
23726 {
23727 tree lhs, rhs;
23728 enum tree_code code;
23729
23730 cp_parser_require_pragma_eol (parser, pragma_tok);
23731
23732 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
23733 /*cast_p=*/false, NULL);
23734 switch (TREE_CODE (lhs))
23735 {
23736 case ERROR_MARK:
23737 goto saw_error;
23738
23739 case PREINCREMENT_EXPR:
23740 case POSTINCREMENT_EXPR:
23741 lhs = TREE_OPERAND (lhs, 0);
23742 code = PLUS_EXPR;
23743 rhs = integer_one_node;
23744 break;
23745
23746 case PREDECREMENT_EXPR:
23747 case POSTDECREMENT_EXPR:
23748 lhs = TREE_OPERAND (lhs, 0);
23749 code = MINUS_EXPR;
23750 rhs = integer_one_node;
23751 break;
23752
23753 case COMPOUND_EXPR:
23754 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
23755 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
23756 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
23757 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
23758 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23759 (TREE_OPERAND (lhs, 1), 0), 0)))
23760 == BOOLEAN_TYPE)
23761 /* Undo effects of boolean_increment for post {in,de}crement. */
23762 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
23763 /* FALLTHRU */
23764 case MODIFY_EXPR:
23765 if (TREE_CODE (lhs) == MODIFY_EXPR
23766 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
23767 {
23768 /* Undo effects of boolean_increment. */
23769 if (integer_onep (TREE_OPERAND (lhs, 1)))
23770 {
23771 /* This is pre or post increment. */
23772 rhs = TREE_OPERAND (lhs, 1);
23773 lhs = TREE_OPERAND (lhs, 0);
23774 code = NOP_EXPR;
23775 break;
23776 }
23777 }
23778 /* FALLTHRU */
23779 default:
23780 switch (cp_lexer_peek_token (parser->lexer)->type)
23781 {
23782 case CPP_MULT_EQ:
23783 code = MULT_EXPR;
23784 break;
23785 case CPP_DIV_EQ:
23786 code = TRUNC_DIV_EXPR;
23787 break;
23788 case CPP_PLUS_EQ:
23789 code = PLUS_EXPR;
23790 break;
23791 case CPP_MINUS_EQ:
23792 code = MINUS_EXPR;
23793 break;
23794 case CPP_LSHIFT_EQ:
23795 code = LSHIFT_EXPR;
23796 break;
23797 case CPP_RSHIFT_EQ:
23798 code = RSHIFT_EXPR;
23799 break;
23800 case CPP_AND_EQ:
23801 code = BIT_AND_EXPR;
23802 break;
23803 case CPP_OR_EQ:
23804 code = BIT_IOR_EXPR;
23805 break;
23806 case CPP_XOR_EQ:
23807 code = BIT_XOR_EXPR;
23808 break;
23809 default:
23810 cp_parser_error (parser,
23811 "invalid operator for %<#pragma omp atomic%>");
23812 goto saw_error;
23813 }
23814 cp_lexer_consume_token (parser->lexer);
23815
23816 rhs = cp_parser_expression (parser, false, NULL);
23817 if (rhs == error_mark_node)
23818 goto saw_error;
23819 break;
23820 }
23821 finish_omp_atomic (code, lhs, rhs);
23822 cp_parser_consume_semicolon_at_end_of_statement (parser);
23823 return;
23824
23825 saw_error:
23826 cp_parser_skip_to_end_of_block_or_statement (parser);
23827 }
23828
23829
23830 /* OpenMP 2.5:
23831 # pragma omp barrier new-line */
23832
23833 static void
23834 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
23835 {
23836 cp_parser_require_pragma_eol (parser, pragma_tok);
23837 finish_omp_barrier ();
23838 }
23839
23840 /* OpenMP 2.5:
23841 # pragma omp critical [(name)] new-line
23842 structured-block */
23843
23844 static tree
23845 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
23846 {
23847 tree stmt, name = NULL;
23848
23849 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23850 {
23851 cp_lexer_consume_token (parser->lexer);
23852
23853 name = cp_parser_identifier (parser);
23854
23855 if (name == error_mark_node
23856 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23857 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23858 /*or_comma=*/false,
23859 /*consume_paren=*/true);
23860 if (name == error_mark_node)
23861 name = NULL;
23862 }
23863 cp_parser_require_pragma_eol (parser, pragma_tok);
23864
23865 stmt = cp_parser_omp_structured_block (parser);
23866 return c_finish_omp_critical (input_location, stmt, name);
23867 }
23868
23869 /* OpenMP 2.5:
23870 # pragma omp flush flush-vars[opt] new-line
23871
23872 flush-vars:
23873 ( variable-list ) */
23874
23875 static void
23876 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
23877 {
23878 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23879 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23880 cp_parser_require_pragma_eol (parser, pragma_tok);
23881
23882 finish_omp_flush ();
23883 }
23884
23885 /* Helper function, to parse omp for increment expression. */
23886
23887 static tree
23888 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
23889 {
23890 tree cond = cp_parser_binary_expression (parser, false, true,
23891 PREC_NOT_OPERATOR, NULL);
23892 bool overloaded_p;
23893
23894 if (cond == error_mark_node
23895 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23896 {
23897 cp_parser_skip_to_end_of_statement (parser);
23898 return error_mark_node;
23899 }
23900
23901 switch (TREE_CODE (cond))
23902 {
23903 case GT_EXPR:
23904 case GE_EXPR:
23905 case LT_EXPR:
23906 case LE_EXPR:
23907 break;
23908 default:
23909 return error_mark_node;
23910 }
23911
23912 /* If decl is an iterator, preserve LHS and RHS of the relational
23913 expr until finish_omp_for. */
23914 if (decl
23915 && (type_dependent_expression_p (decl)
23916 || CLASS_TYPE_P (TREE_TYPE (decl))))
23917 return cond;
23918
23919 return build_x_binary_op (TREE_CODE (cond),
23920 TREE_OPERAND (cond, 0), ERROR_MARK,
23921 TREE_OPERAND (cond, 1), ERROR_MARK,
23922 &overloaded_p, tf_warning_or_error);
23923 }
23924
23925 /* Helper function, to parse omp for increment expression. */
23926
23927 static tree
23928 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
23929 {
23930 cp_token *token = cp_lexer_peek_token (parser->lexer);
23931 enum tree_code op;
23932 tree lhs, rhs;
23933 cp_id_kind idk;
23934 bool decl_first;
23935
23936 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23937 {
23938 op = (token->type == CPP_PLUS_PLUS
23939 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
23940 cp_lexer_consume_token (parser->lexer);
23941 lhs = cp_parser_cast_expression (parser, false, false, NULL);
23942 if (lhs != decl)
23943 return error_mark_node;
23944 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23945 }
23946
23947 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
23948 if (lhs != decl)
23949 return error_mark_node;
23950
23951 token = cp_lexer_peek_token (parser->lexer);
23952 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23953 {
23954 op = (token->type == CPP_PLUS_PLUS
23955 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
23956 cp_lexer_consume_token (parser->lexer);
23957 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23958 }
23959
23960 op = cp_parser_assignment_operator_opt (parser);
23961 if (op == ERROR_MARK)
23962 return error_mark_node;
23963
23964 if (op != NOP_EXPR)
23965 {
23966 rhs = cp_parser_assignment_expression (parser, false, NULL);
23967 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
23968 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
23969 }
23970
23971 lhs = cp_parser_binary_expression (parser, false, false,
23972 PREC_ADDITIVE_EXPRESSION, NULL);
23973 token = cp_lexer_peek_token (parser->lexer);
23974 decl_first = lhs == decl;
23975 if (decl_first)
23976 lhs = NULL_TREE;
23977 if (token->type != CPP_PLUS
23978 && token->type != CPP_MINUS)
23979 return error_mark_node;
23980
23981 do
23982 {
23983 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
23984 cp_lexer_consume_token (parser->lexer);
23985 rhs = cp_parser_binary_expression (parser, false, false,
23986 PREC_ADDITIVE_EXPRESSION, NULL);
23987 token = cp_lexer_peek_token (parser->lexer);
23988 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
23989 {
23990 if (lhs == NULL_TREE)
23991 {
23992 if (op == PLUS_EXPR)
23993 lhs = rhs;
23994 else
23995 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
23996 }
23997 else
23998 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
23999 NULL, tf_warning_or_error);
24000 }
24001 }
24002 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24003
24004 if (!decl_first)
24005 {
24006 if (rhs != decl || op == MINUS_EXPR)
24007 return error_mark_node;
24008 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24009 }
24010 else
24011 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24012
24013 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24014 }
24015
24016 /* Parse the restricted form of the for statement allowed by OpenMP. */
24017
24018 static tree
24019 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24020 {
24021 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24022 tree real_decl, initv, condv, incrv, declv;
24023 tree this_pre_body, cl;
24024 location_t loc_first;
24025 bool collapse_err = false;
24026 int i, collapse = 1, nbraces = 0;
24027 VEC(tree,gc) *for_block = make_tree_vector ();
24028
24029 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24030 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24031 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24032
24033 gcc_assert (collapse >= 1);
24034
24035 declv = make_tree_vec (collapse);
24036 initv = make_tree_vec (collapse);
24037 condv = make_tree_vec (collapse);
24038 incrv = make_tree_vec (collapse);
24039
24040 loc_first = cp_lexer_peek_token (parser->lexer)->location;
24041
24042 for (i = 0; i < collapse; i++)
24043 {
24044 int bracecount = 0;
24045 bool add_private_clause = false;
24046 location_t loc;
24047
24048 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24049 {
24050 cp_parser_error (parser, "for statement expected");
24051 return NULL;
24052 }
24053 loc = cp_lexer_consume_token (parser->lexer)->location;
24054
24055 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24056 return NULL;
24057
24058 init = decl = real_decl = NULL;
24059 this_pre_body = push_stmt_list ();
24060 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24061 {
24062 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24063
24064 init-expr:
24065 var = lb
24066 integer-type var = lb
24067 random-access-iterator-type var = lb
24068 pointer-type var = lb
24069 */
24070 cp_decl_specifier_seq type_specifiers;
24071
24072 /* First, try to parse as an initialized declaration. See
24073 cp_parser_condition, from whence the bulk of this is copied. */
24074
24075 cp_parser_parse_tentatively (parser);
24076 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24077 /*is_trailing_return=*/false,
24078 &type_specifiers);
24079 if (cp_parser_parse_definitely (parser))
24080 {
24081 /* If parsing a type specifier seq succeeded, then this
24082 MUST be a initialized declaration. */
24083 tree asm_specification, attributes;
24084 cp_declarator *declarator;
24085
24086 declarator = cp_parser_declarator (parser,
24087 CP_PARSER_DECLARATOR_NAMED,
24088 /*ctor_dtor_or_conv_p=*/NULL,
24089 /*parenthesized_p=*/NULL,
24090 /*member_p=*/false);
24091 attributes = cp_parser_attributes_opt (parser);
24092 asm_specification = cp_parser_asm_specification_opt (parser);
24093
24094 if (declarator == cp_error_declarator)
24095 cp_parser_skip_to_end_of_statement (parser);
24096
24097 else
24098 {
24099 tree pushed_scope, auto_node;
24100
24101 decl = start_decl (declarator, &type_specifiers,
24102 SD_INITIALIZED, attributes,
24103 /*prefix_attributes=*/NULL_TREE,
24104 &pushed_scope);
24105
24106 auto_node = type_uses_auto (TREE_TYPE (decl));
24107 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24108 {
24109 if (cp_lexer_next_token_is (parser->lexer,
24110 CPP_OPEN_PAREN))
24111 error ("parenthesized initialization is not allowed in "
24112 "OpenMP %<for%> loop");
24113 else
24114 /* Trigger an error. */
24115 cp_parser_require (parser, CPP_EQ, RT_EQ);
24116
24117 init = error_mark_node;
24118 cp_parser_skip_to_end_of_statement (parser);
24119 }
24120 else if (CLASS_TYPE_P (TREE_TYPE (decl))
24121 || type_dependent_expression_p (decl)
24122 || auto_node)
24123 {
24124 bool is_direct_init, is_non_constant_init;
24125
24126 init = cp_parser_initializer (parser,
24127 &is_direct_init,
24128 &is_non_constant_init);
24129
24130 if (auto_node && describable_type (init))
24131 {
24132 TREE_TYPE (decl)
24133 = do_auto_deduction (TREE_TYPE (decl), init,
24134 auto_node);
24135
24136 if (!CLASS_TYPE_P (TREE_TYPE (decl))
24137 && !type_dependent_expression_p (decl))
24138 goto non_class;
24139 }
24140
24141 cp_finish_decl (decl, init, !is_non_constant_init,
24142 asm_specification,
24143 LOOKUP_ONLYCONVERTING);
24144 if (CLASS_TYPE_P (TREE_TYPE (decl)))
24145 {
24146 VEC_safe_push (tree, gc, for_block, this_pre_body);
24147 init = NULL_TREE;
24148 }
24149 else
24150 init = pop_stmt_list (this_pre_body);
24151 this_pre_body = NULL_TREE;
24152 }
24153 else
24154 {
24155 /* Consume '='. */
24156 cp_lexer_consume_token (parser->lexer);
24157 init = cp_parser_assignment_expression (parser, false, NULL);
24158
24159 non_class:
24160 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24161 init = error_mark_node;
24162 else
24163 cp_finish_decl (decl, NULL_TREE,
24164 /*init_const_expr_p=*/false,
24165 asm_specification,
24166 LOOKUP_ONLYCONVERTING);
24167 }
24168
24169 if (pushed_scope)
24170 pop_scope (pushed_scope);
24171 }
24172 }
24173 else
24174 {
24175 cp_id_kind idk;
24176 /* If parsing a type specifier sequence failed, then
24177 this MUST be a simple expression. */
24178 cp_parser_parse_tentatively (parser);
24179 decl = cp_parser_primary_expression (parser, false, false,
24180 false, &idk);
24181 if (!cp_parser_error_occurred (parser)
24182 && decl
24183 && DECL_P (decl)
24184 && CLASS_TYPE_P (TREE_TYPE (decl)))
24185 {
24186 tree rhs;
24187
24188 cp_parser_parse_definitely (parser);
24189 cp_parser_require (parser, CPP_EQ, RT_EQ);
24190 rhs = cp_parser_assignment_expression (parser, false, NULL);
24191 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24192 rhs,
24193 tf_warning_or_error));
24194 add_private_clause = true;
24195 }
24196 else
24197 {
24198 decl = NULL;
24199 cp_parser_abort_tentative_parse (parser);
24200 init = cp_parser_expression (parser, false, NULL);
24201 if (init)
24202 {
24203 if (TREE_CODE (init) == MODIFY_EXPR
24204 || TREE_CODE (init) == MODOP_EXPR)
24205 real_decl = TREE_OPERAND (init, 0);
24206 }
24207 }
24208 }
24209 }
24210 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24211 if (this_pre_body)
24212 {
24213 this_pre_body = pop_stmt_list (this_pre_body);
24214 if (pre_body)
24215 {
24216 tree t = pre_body;
24217 pre_body = push_stmt_list ();
24218 add_stmt (t);
24219 add_stmt (this_pre_body);
24220 pre_body = pop_stmt_list (pre_body);
24221 }
24222 else
24223 pre_body = this_pre_body;
24224 }
24225
24226 if (decl)
24227 real_decl = decl;
24228 if (par_clauses != NULL && real_decl != NULL_TREE)
24229 {
24230 tree *c;
24231 for (c = par_clauses; *c ; )
24232 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24233 && OMP_CLAUSE_DECL (*c) == real_decl)
24234 {
24235 error_at (loc, "iteration variable %qD"
24236 " should not be firstprivate", real_decl);
24237 *c = OMP_CLAUSE_CHAIN (*c);
24238 }
24239 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24240 && OMP_CLAUSE_DECL (*c) == real_decl)
24241 {
24242 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24243 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
24244 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24245 OMP_CLAUSE_DECL (l) = real_decl;
24246 OMP_CLAUSE_CHAIN (l) = clauses;
24247 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24248 clauses = l;
24249 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24250 CP_OMP_CLAUSE_INFO (*c) = NULL;
24251 add_private_clause = false;
24252 }
24253 else
24254 {
24255 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24256 && OMP_CLAUSE_DECL (*c) == real_decl)
24257 add_private_clause = false;
24258 c = &OMP_CLAUSE_CHAIN (*c);
24259 }
24260 }
24261
24262 if (add_private_clause)
24263 {
24264 tree c;
24265 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24266 {
24267 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24268 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24269 && OMP_CLAUSE_DECL (c) == decl)
24270 break;
24271 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24272 && OMP_CLAUSE_DECL (c) == decl)
24273 error_at (loc, "iteration variable %qD "
24274 "should not be firstprivate",
24275 decl);
24276 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24277 && OMP_CLAUSE_DECL (c) == decl)
24278 error_at (loc, "iteration variable %qD should not be reduction",
24279 decl);
24280 }
24281 if (c == NULL)
24282 {
24283 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24284 OMP_CLAUSE_DECL (c) = decl;
24285 c = finish_omp_clauses (c);
24286 if (c)
24287 {
24288 OMP_CLAUSE_CHAIN (c) = clauses;
24289 clauses = c;
24290 }
24291 }
24292 }
24293
24294 cond = NULL;
24295 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24296 cond = cp_parser_omp_for_cond (parser, decl);
24297 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24298
24299 incr = NULL;
24300 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24301 {
24302 /* If decl is an iterator, preserve the operator on decl
24303 until finish_omp_for. */
24304 if (decl
24305 && (type_dependent_expression_p (decl)
24306 || CLASS_TYPE_P (TREE_TYPE (decl))))
24307 incr = cp_parser_omp_for_incr (parser, decl);
24308 else
24309 incr = cp_parser_expression (parser, false, NULL);
24310 }
24311
24312 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24313 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24314 /*or_comma=*/false,
24315 /*consume_paren=*/true);
24316
24317 TREE_VEC_ELT (declv, i) = decl;
24318 TREE_VEC_ELT (initv, i) = init;
24319 TREE_VEC_ELT (condv, i) = cond;
24320 TREE_VEC_ELT (incrv, i) = incr;
24321
24322 if (i == collapse - 1)
24323 break;
24324
24325 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24326 in between the collapsed for loops to be still considered perfectly
24327 nested. Hopefully the final version clarifies this.
24328 For now handle (multiple) {'s and empty statements. */
24329 cp_parser_parse_tentatively (parser);
24330 do
24331 {
24332 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24333 break;
24334 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24335 {
24336 cp_lexer_consume_token (parser->lexer);
24337 bracecount++;
24338 }
24339 else if (bracecount
24340 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24341 cp_lexer_consume_token (parser->lexer);
24342 else
24343 {
24344 loc = cp_lexer_peek_token (parser->lexer)->location;
24345 error_at (loc, "not enough collapsed for loops");
24346 collapse_err = true;
24347 cp_parser_abort_tentative_parse (parser);
24348 declv = NULL_TREE;
24349 break;
24350 }
24351 }
24352 while (1);
24353
24354 if (declv)
24355 {
24356 cp_parser_parse_definitely (parser);
24357 nbraces += bracecount;
24358 }
24359 }
24360
24361 /* Note that we saved the original contents of this flag when we entered
24362 the structured block, and so we don't need to re-save it here. */
24363 parser->in_statement = IN_OMP_FOR;
24364
24365 /* Note that the grammar doesn't call for a structured block here,
24366 though the loop as a whole is a structured block. */
24367 body = push_stmt_list ();
24368 cp_parser_statement (parser, NULL_TREE, false, NULL);
24369 body = pop_stmt_list (body);
24370
24371 if (declv == NULL_TREE)
24372 ret = NULL_TREE;
24373 else
24374 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24375 pre_body, clauses);
24376
24377 while (nbraces)
24378 {
24379 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24380 {
24381 cp_lexer_consume_token (parser->lexer);
24382 nbraces--;
24383 }
24384 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24385 cp_lexer_consume_token (parser->lexer);
24386 else
24387 {
24388 if (!collapse_err)
24389 {
24390 error_at (cp_lexer_peek_token (parser->lexer)->location,
24391 "collapsed loops not perfectly nested");
24392 }
24393 collapse_err = true;
24394 cp_parser_statement_seq_opt (parser, NULL);
24395 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24396 break;
24397 }
24398 }
24399
24400 while (!VEC_empty (tree, for_block))
24401 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24402 release_tree_vector (for_block);
24403
24404 return ret;
24405 }
24406
24407 /* OpenMP 2.5:
24408 #pragma omp for for-clause[optseq] new-line
24409 for-loop */
24410
24411 #define OMP_FOR_CLAUSE_MASK \
24412 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24413 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24414 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24415 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24416 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
24417 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
24418 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
24419 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24420
24421 static tree
24422 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24423 {
24424 tree clauses, sb, ret;
24425 unsigned int save;
24426
24427 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24428 "#pragma omp for", pragma_tok);
24429
24430 sb = begin_omp_structured_block ();
24431 save = cp_parser_begin_omp_structured_block (parser);
24432
24433 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24434
24435 cp_parser_end_omp_structured_block (parser, save);
24436 add_stmt (finish_omp_structured_block (sb));
24437
24438 return ret;
24439 }
24440
24441 /* OpenMP 2.5:
24442 # pragma omp master new-line
24443 structured-block */
24444
24445 static tree
24446 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24447 {
24448 cp_parser_require_pragma_eol (parser, pragma_tok);
24449 return c_finish_omp_master (input_location,
24450 cp_parser_omp_structured_block (parser));
24451 }
24452
24453 /* OpenMP 2.5:
24454 # pragma omp ordered new-line
24455 structured-block */
24456
24457 static tree
24458 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24459 {
24460 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24461 cp_parser_require_pragma_eol (parser, pragma_tok);
24462 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24463 }
24464
24465 /* OpenMP 2.5:
24466
24467 section-scope:
24468 { section-sequence }
24469
24470 section-sequence:
24471 section-directive[opt] structured-block
24472 section-sequence section-directive structured-block */
24473
24474 static tree
24475 cp_parser_omp_sections_scope (cp_parser *parser)
24476 {
24477 tree stmt, substmt;
24478 bool error_suppress = false;
24479 cp_token *tok;
24480
24481 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24482 return NULL_TREE;
24483
24484 stmt = push_stmt_list ();
24485
24486 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24487 {
24488 unsigned save;
24489
24490 substmt = begin_omp_structured_block ();
24491 save = cp_parser_begin_omp_structured_block (parser);
24492
24493 while (1)
24494 {
24495 cp_parser_statement (parser, NULL_TREE, false, NULL);
24496
24497 tok = cp_lexer_peek_token (parser->lexer);
24498 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24499 break;
24500 if (tok->type == CPP_CLOSE_BRACE)
24501 break;
24502 if (tok->type == CPP_EOF)
24503 break;
24504 }
24505
24506 cp_parser_end_omp_structured_block (parser, save);
24507 substmt = finish_omp_structured_block (substmt);
24508 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24509 add_stmt (substmt);
24510 }
24511
24512 while (1)
24513 {
24514 tok = cp_lexer_peek_token (parser->lexer);
24515 if (tok->type == CPP_CLOSE_BRACE)
24516 break;
24517 if (tok->type == CPP_EOF)
24518 break;
24519
24520 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24521 {
24522 cp_lexer_consume_token (parser->lexer);
24523 cp_parser_require_pragma_eol (parser, tok);
24524 error_suppress = false;
24525 }
24526 else if (!error_suppress)
24527 {
24528 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24529 error_suppress = true;
24530 }
24531
24532 substmt = cp_parser_omp_structured_block (parser);
24533 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24534 add_stmt (substmt);
24535 }
24536 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24537
24538 substmt = pop_stmt_list (stmt);
24539
24540 stmt = make_node (OMP_SECTIONS);
24541 TREE_TYPE (stmt) = void_type_node;
24542 OMP_SECTIONS_BODY (stmt) = substmt;
24543
24544 add_stmt (stmt);
24545 return stmt;
24546 }
24547
24548 /* OpenMP 2.5:
24549 # pragma omp sections sections-clause[optseq] newline
24550 sections-scope */
24551
24552 #define OMP_SECTIONS_CLAUSE_MASK \
24553 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24554 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24555 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24556 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24557 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24558
24559 static tree
24560 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24561 {
24562 tree clauses, ret;
24563
24564 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24565 "#pragma omp sections", pragma_tok);
24566
24567 ret = cp_parser_omp_sections_scope (parser);
24568 if (ret)
24569 OMP_SECTIONS_CLAUSES (ret) = clauses;
24570
24571 return ret;
24572 }
24573
24574 /* OpenMP 2.5:
24575 # pragma parallel parallel-clause new-line
24576 # pragma parallel for parallel-for-clause new-line
24577 # pragma parallel sections parallel-sections-clause new-line */
24578
24579 #define OMP_PARALLEL_CLAUSE_MASK \
24580 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24581 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24582 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24583 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24584 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
24585 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
24586 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24587 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24588
24589 static tree
24590 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24591 {
24592 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24593 const char *p_name = "#pragma omp parallel";
24594 tree stmt, clauses, par_clause, ws_clause, block;
24595 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24596 unsigned int save;
24597 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24598
24599 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24600 {
24601 cp_lexer_consume_token (parser->lexer);
24602 p_kind = PRAGMA_OMP_PARALLEL_FOR;
24603 p_name = "#pragma omp parallel for";
24604 mask |= OMP_FOR_CLAUSE_MASK;
24605 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24606 }
24607 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24608 {
24609 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24610 const char *p = IDENTIFIER_POINTER (id);
24611 if (strcmp (p, "sections") == 0)
24612 {
24613 cp_lexer_consume_token (parser->lexer);
24614 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24615 p_name = "#pragma omp parallel sections";
24616 mask |= OMP_SECTIONS_CLAUSE_MASK;
24617 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24618 }
24619 }
24620
24621 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24622 block = begin_omp_parallel ();
24623 save = cp_parser_begin_omp_structured_block (parser);
24624
24625 switch (p_kind)
24626 {
24627 case PRAGMA_OMP_PARALLEL:
24628 cp_parser_statement (parser, NULL_TREE, false, NULL);
24629 par_clause = clauses;
24630 break;
24631
24632 case PRAGMA_OMP_PARALLEL_FOR:
24633 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24634 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24635 break;
24636
24637 case PRAGMA_OMP_PARALLEL_SECTIONS:
24638 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24639 stmt = cp_parser_omp_sections_scope (parser);
24640 if (stmt)
24641 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24642 break;
24643
24644 default:
24645 gcc_unreachable ();
24646 }
24647
24648 cp_parser_end_omp_structured_block (parser, save);
24649 stmt = finish_omp_parallel (par_clause, block);
24650 if (p_kind != PRAGMA_OMP_PARALLEL)
24651 OMP_PARALLEL_COMBINED (stmt) = 1;
24652 return stmt;
24653 }
24654
24655 /* OpenMP 2.5:
24656 # pragma omp single single-clause[optseq] new-line
24657 structured-block */
24658
24659 #define OMP_SINGLE_CLAUSE_MASK \
24660 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24661 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24662 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
24663 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24664
24665 static tree
24666 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24667 {
24668 tree stmt = make_node (OMP_SINGLE);
24669 TREE_TYPE (stmt) = void_type_node;
24670
24671 OMP_SINGLE_CLAUSES (stmt)
24672 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24673 "#pragma omp single", pragma_tok);
24674 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24675
24676 return add_stmt (stmt);
24677 }
24678
24679 /* OpenMP 3.0:
24680 # pragma omp task task-clause[optseq] new-line
24681 structured-block */
24682
24683 #define OMP_TASK_CLAUSE_MASK \
24684 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24685 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
24686 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24687 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24688 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24689 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24690
24691 static tree
24692 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
24693 {
24694 tree clauses, block;
24695 unsigned int save;
24696
24697 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
24698 "#pragma omp task", pragma_tok);
24699 block = begin_omp_task ();
24700 save = cp_parser_begin_omp_structured_block (parser);
24701 cp_parser_statement (parser, NULL_TREE, false, NULL);
24702 cp_parser_end_omp_structured_block (parser, save);
24703 return finish_omp_task (clauses, block);
24704 }
24705
24706 /* OpenMP 3.0:
24707 # pragma omp taskwait new-line */
24708
24709 static void
24710 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
24711 {
24712 cp_parser_require_pragma_eol (parser, pragma_tok);
24713 finish_omp_taskwait ();
24714 }
24715
24716 /* OpenMP 2.5:
24717 # pragma omp threadprivate (variable-list) */
24718
24719 static void
24720 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
24721 {
24722 tree vars;
24723
24724 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24725 cp_parser_require_pragma_eol (parser, pragma_tok);
24726
24727 finish_omp_threadprivate (vars);
24728 }
24729
24730 /* Main entry point to OpenMP statement pragmas. */
24731
24732 static void
24733 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
24734 {
24735 tree stmt;
24736
24737 switch (pragma_tok->pragma_kind)
24738 {
24739 case PRAGMA_OMP_ATOMIC:
24740 cp_parser_omp_atomic (parser, pragma_tok);
24741 return;
24742 case PRAGMA_OMP_CRITICAL:
24743 stmt = cp_parser_omp_critical (parser, pragma_tok);
24744 break;
24745 case PRAGMA_OMP_FOR:
24746 stmt = cp_parser_omp_for (parser, pragma_tok);
24747 break;
24748 case PRAGMA_OMP_MASTER:
24749 stmt = cp_parser_omp_master (parser, pragma_tok);
24750 break;
24751 case PRAGMA_OMP_ORDERED:
24752 stmt = cp_parser_omp_ordered (parser, pragma_tok);
24753 break;
24754 case PRAGMA_OMP_PARALLEL:
24755 stmt = cp_parser_omp_parallel (parser, pragma_tok);
24756 break;
24757 case PRAGMA_OMP_SECTIONS:
24758 stmt = cp_parser_omp_sections (parser, pragma_tok);
24759 break;
24760 case PRAGMA_OMP_SINGLE:
24761 stmt = cp_parser_omp_single (parser, pragma_tok);
24762 break;
24763 case PRAGMA_OMP_TASK:
24764 stmt = cp_parser_omp_task (parser, pragma_tok);
24765 break;
24766 default:
24767 gcc_unreachable ();
24768 }
24769
24770 if (stmt)
24771 SET_EXPR_LOCATION (stmt, pragma_tok->location);
24772 }
24773 \f
24774 /* The parser. */
24775
24776 static GTY (()) cp_parser *the_parser;
24777
24778 \f
24779 /* Special handling for the first token or line in the file. The first
24780 thing in the file might be #pragma GCC pch_preprocess, which loads a
24781 PCH file, which is a GC collection point. So we need to handle this
24782 first pragma without benefit of an existing lexer structure.
24783
24784 Always returns one token to the caller in *FIRST_TOKEN. This is
24785 either the true first token of the file, or the first token after
24786 the initial pragma. */
24787
24788 static void
24789 cp_parser_initial_pragma (cp_token *first_token)
24790 {
24791 tree name = NULL;
24792
24793 cp_lexer_get_preprocessor_token (NULL, first_token);
24794 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
24795 return;
24796
24797 cp_lexer_get_preprocessor_token (NULL, first_token);
24798 if (first_token->type == CPP_STRING)
24799 {
24800 name = first_token->u.value;
24801
24802 cp_lexer_get_preprocessor_token (NULL, first_token);
24803 if (first_token->type != CPP_PRAGMA_EOL)
24804 error_at (first_token->location,
24805 "junk at end of %<#pragma GCC pch_preprocess%>");
24806 }
24807 else
24808 error_at (first_token->location, "expected string literal");
24809
24810 /* Skip to the end of the pragma. */
24811 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
24812 cp_lexer_get_preprocessor_token (NULL, first_token);
24813
24814 /* Now actually load the PCH file. */
24815 if (name)
24816 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
24817
24818 /* Read one more token to return to our caller. We have to do this
24819 after reading the PCH file in, since its pointers have to be
24820 live. */
24821 cp_lexer_get_preprocessor_token (NULL, first_token);
24822 }
24823
24824 /* Normal parsing of a pragma token. Here we can (and must) use the
24825 regular lexer. */
24826
24827 static bool
24828 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
24829 {
24830 cp_token *pragma_tok;
24831 unsigned int id;
24832
24833 pragma_tok = cp_lexer_consume_token (parser->lexer);
24834 gcc_assert (pragma_tok->type == CPP_PRAGMA);
24835 parser->lexer->in_pragma = true;
24836
24837 id = pragma_tok->pragma_kind;
24838 switch (id)
24839 {
24840 case PRAGMA_GCC_PCH_PREPROCESS:
24841 error_at (pragma_tok->location,
24842 "%<#pragma GCC pch_preprocess%> must be first");
24843 break;
24844
24845 case PRAGMA_OMP_BARRIER:
24846 switch (context)
24847 {
24848 case pragma_compound:
24849 cp_parser_omp_barrier (parser, pragma_tok);
24850 return false;
24851 case pragma_stmt:
24852 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
24853 "used in compound statements");
24854 break;
24855 default:
24856 goto bad_stmt;
24857 }
24858 break;
24859
24860 case PRAGMA_OMP_FLUSH:
24861 switch (context)
24862 {
24863 case pragma_compound:
24864 cp_parser_omp_flush (parser, pragma_tok);
24865 return false;
24866 case pragma_stmt:
24867 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
24868 "used in compound statements");
24869 break;
24870 default:
24871 goto bad_stmt;
24872 }
24873 break;
24874
24875 case PRAGMA_OMP_TASKWAIT:
24876 switch (context)
24877 {
24878 case pragma_compound:
24879 cp_parser_omp_taskwait (parser, pragma_tok);
24880 return false;
24881 case pragma_stmt:
24882 error_at (pragma_tok->location,
24883 "%<#pragma omp taskwait%> may only be "
24884 "used in compound statements");
24885 break;
24886 default:
24887 goto bad_stmt;
24888 }
24889 break;
24890
24891 case PRAGMA_OMP_THREADPRIVATE:
24892 cp_parser_omp_threadprivate (parser, pragma_tok);
24893 return false;
24894
24895 case PRAGMA_OMP_ATOMIC:
24896 case PRAGMA_OMP_CRITICAL:
24897 case PRAGMA_OMP_FOR:
24898 case PRAGMA_OMP_MASTER:
24899 case PRAGMA_OMP_ORDERED:
24900 case PRAGMA_OMP_PARALLEL:
24901 case PRAGMA_OMP_SECTIONS:
24902 case PRAGMA_OMP_SINGLE:
24903 case PRAGMA_OMP_TASK:
24904 if (context == pragma_external)
24905 goto bad_stmt;
24906 cp_parser_omp_construct (parser, pragma_tok);
24907 return true;
24908
24909 case PRAGMA_OMP_SECTION:
24910 error_at (pragma_tok->location,
24911 "%<#pragma omp section%> may only be used in "
24912 "%<#pragma omp sections%> construct");
24913 break;
24914
24915 default:
24916 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
24917 c_invoke_pragma_handler (id);
24918 break;
24919
24920 bad_stmt:
24921 cp_parser_error (parser, "expected declaration specifiers");
24922 break;
24923 }
24924
24925 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
24926 return false;
24927 }
24928
24929 /* The interface the pragma parsers have to the lexer. */
24930
24931 enum cpp_ttype
24932 pragma_lex (tree *value)
24933 {
24934 cp_token *tok;
24935 enum cpp_ttype ret;
24936
24937 tok = cp_lexer_peek_token (the_parser->lexer);
24938
24939 ret = tok->type;
24940 *value = tok->u.value;
24941
24942 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
24943 ret = CPP_EOF;
24944 else if (ret == CPP_STRING)
24945 *value = cp_parser_string_literal (the_parser, false, false);
24946 else
24947 {
24948 cp_lexer_consume_token (the_parser->lexer);
24949 if (ret == CPP_KEYWORD)
24950 ret = CPP_NAME;
24951 }
24952
24953 return ret;
24954 }
24955
24956 \f
24957 /* External interface. */
24958
24959 /* Parse one entire translation unit. */
24960
24961 void
24962 c_parse_file (void)
24963 {
24964 static bool already_called = false;
24965
24966 if (already_called)
24967 {
24968 sorry ("inter-module optimizations not implemented for C++");
24969 return;
24970 }
24971 already_called = true;
24972
24973 the_parser = cp_parser_new ();
24974 push_deferring_access_checks (flag_access_control
24975 ? dk_no_deferred : dk_no_check);
24976 cp_parser_translation_unit (the_parser);
24977 the_parser = NULL;
24978 }
24979
24980 #include "gt-cp-parser.h"