OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.5.1/] [gcc/] [cp/] [parser.c] - Blame information for rev 307

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 283 jeremybenn
/* C++ Parser.
2
   Copyright (C) 2000, 2001, 2002, 2003, 2004,
3
   2005, 2007, 2008, 2009  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 "dyn-string.h"
27
#include "varray.h"
28
#include "cpplib.h"
29
#include "tree.h"
30
#include "cp-tree.h"
31
#include "intl.h"
32
#include "c-pragma.h"
33
#include "decl.h"
34
#include "flags.h"
35
#include "diagnostic.h"
36
#include "toplev.h"
37
#include "output.h"
38
#include "target.h"
39
#include "cgraph.h"
40
#include "c-common.h"
41
#include "plugin.h"
42
 
43
 
44
/* The lexer.  */
45
 
46
/* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47
   and c-lex.c) and the C++ parser.  */
48
 
49
/* A token's value and its associated deferred access checks and
50
   qualifying scope.  */
51
 
52
struct GTY(()) tree_check {
53
  /* The value associated with the token.  */
54
  tree value;
55
  /* The checks that have been associated with value.  */
56
  VEC (deferred_access_check, gc)* checks;
57
  /* The token's qualifying scope (used when it is a
58
     CPP_NESTED_NAME_SPECIFIER).  */
59
  tree qualifying_scope;
60
};
61
 
62
/* A C++ token.  */
63
 
64
typedef struct GTY (()) cp_token {
65
  /* The kind of token.  */
66
  ENUM_BITFIELD (cpp_ttype) type : 8;
67
  /* If this token is a keyword, this value indicates which keyword.
68
     Otherwise, this value is RID_MAX.  */
69
  ENUM_BITFIELD (rid) keyword : 8;
70
  /* Token flags.  */
71
  unsigned char flags;
72
  /* Identifier for the pragma.  */
73
  ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74
  /* True if this token is from a context where it is implicitly extern "C" */
75
  BOOL_BITFIELD implicit_extern_c : 1;
76
  /* True for a CPP_NAME token that is not a keyword (i.e., for which
77
     KEYWORD is RID_MAX) iff this name was looked up and found to be
78
     ambiguous.  An error has already been reported.  */
79
  BOOL_BITFIELD ambiguous_p : 1;
80
  /* The location at which this token was found.  */
81
  location_t location;
82
  /* The value associated with this token, if any.  */
83
  union cp_token_value {
84
    /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
85
    struct tree_check* GTY((tag ("1"))) tree_check_value;
86
    /* Use for all other tokens.  */
87
    tree GTY((tag ("0"))) value;
88
  } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89
} cp_token;
90
 
91
/* We use a stack of token pointer for saving token sets.  */
92
typedef struct cp_token *cp_token_position;
93
DEF_VEC_P (cp_token_position);
94
DEF_VEC_ALLOC_P (cp_token_position,heap);
95
 
96
static cp_token eof_token =
97
{
98
  CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
99
};
100
 
101
/* The cp_lexer structure represents the C++ lexer.  It is responsible
102
   for managing the token stream from the preprocessor and supplying
103
   it to the parser.  Tokens are never added to the cp_lexer after
104
   it is created.  */
105
 
106
typedef struct GTY (()) cp_lexer {
107
  /* The memory allocated for the buffer.  NULL if this lexer does not
108
     own the token buffer.  */
109
  cp_token * GTY ((length ("%h.buffer_length"))) buffer;
110
  /* If the lexer owns the buffer, this is the number of tokens in the
111
     buffer.  */
112
  size_t buffer_length;
113
 
114
  /* A pointer just past the last available token.  The tokens
115
     in this lexer are [buffer, last_token).  */
116
  cp_token_position GTY ((skip)) last_token;
117
 
118
  /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
119
     no more available tokens.  */
120
  cp_token_position GTY ((skip)) next_token;
121
 
122
  /* A stack indicating positions at which cp_lexer_save_tokens was
123
     called.  The top entry is the most recent position at which we
124
     began saving tokens.  If the stack is non-empty, we are saving
125
     tokens.  */
126
  VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
127
 
128
  /* The next lexer in a linked list of lexers.  */
129
  struct cp_lexer *next;
130
 
131
  /* True if we should output debugging information.  */
132
  bool debugging_p;
133
 
134
  /* True if we're in the context of parsing a pragma, and should not
135
     increment past the end-of-line marker.  */
136
  bool in_pragma;
137
} cp_lexer;
138
 
139
/* cp_token_cache is a range of tokens.  There is no need to represent
140
   allocate heap memory for it, since tokens are never removed from the
141
   lexer's array.  There is also no need for the GC to walk through
142
   a cp_token_cache, since everything in here is referenced through
143
   a lexer.  */
144
 
145
typedef struct GTY(()) cp_token_cache {
146
  /* The beginning of the token range.  */
147
  cp_token * GTY((skip)) first;
148
 
149
  /* Points immediately after the last token in the range.  */
150
  cp_token * GTY ((skip)) last;
151
} cp_token_cache;
152
 
153
/* Prototypes.  */
154
 
155
static cp_lexer *cp_lexer_new_main
156
  (void);
157
static cp_lexer *cp_lexer_new_from_tokens
158
  (cp_token_cache *tokens);
159
static void cp_lexer_destroy
160
  (cp_lexer *);
161
static int cp_lexer_saving_tokens
162
  (const cp_lexer *);
163
static cp_token_position cp_lexer_token_position
164
  (cp_lexer *, bool);
165
static cp_token *cp_lexer_token_at
166
  (cp_lexer *, cp_token_position);
167
static void cp_lexer_get_preprocessor_token
168
  (cp_lexer *, cp_token *);
169
static inline cp_token *cp_lexer_peek_token
170
  (cp_lexer *);
171
static cp_token *cp_lexer_peek_nth_token
172
  (cp_lexer *, size_t);
173
static inline bool cp_lexer_next_token_is
174
  (cp_lexer *, enum cpp_ttype);
175
static bool cp_lexer_next_token_is_not
176
  (cp_lexer *, enum cpp_ttype);
177
static bool cp_lexer_next_token_is_keyword
178
  (cp_lexer *, enum rid);
179
static cp_token *cp_lexer_consume_token
180
  (cp_lexer *);
181
static void cp_lexer_purge_token
182
  (cp_lexer *);
183
static void cp_lexer_purge_tokens_after
184
  (cp_lexer *, cp_token_position);
185
static void cp_lexer_save_tokens
186
  (cp_lexer *);
187
static void cp_lexer_commit_tokens
188
  (cp_lexer *);
189
static void cp_lexer_rollback_tokens
190
  (cp_lexer *);
191
#ifdef ENABLE_CHECKING
192
static void cp_lexer_print_token
193
  (FILE *, cp_token *);
194
static inline bool cp_lexer_debugging_p
195
  (cp_lexer *);
196
static void cp_lexer_start_debugging
197
  (cp_lexer *) ATTRIBUTE_UNUSED;
198
static void cp_lexer_stop_debugging
199
  (cp_lexer *) ATTRIBUTE_UNUSED;
200
#else
201
/* If we define cp_lexer_debug_stream to NULL it will provoke warnings
202
   about passing NULL to functions that require non-NULL arguments
203
   (fputs, fprintf).  It will never be used, so all we need is a value
204
   of the right type that's guaranteed not to be NULL.  */
205
#define cp_lexer_debug_stream stdout
206
#define cp_lexer_print_token(str, tok) (void) 0
207
#define cp_lexer_debugging_p(lexer) 0
208
#endif /* ENABLE_CHECKING */
209
 
210
static cp_token_cache *cp_token_cache_new
211
  (cp_token *, cp_token *);
212
 
213
static void cp_parser_initial_pragma
214
  (cp_token *);
215
 
216
/* Manifest constants.  */
217
#define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
218
#define CP_SAVED_TOKEN_STACK 5
219
 
220
/* A token type for keywords, as opposed to ordinary identifiers.  */
221
#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
222
 
223
/* A token type for template-ids.  If a template-id is processed while
224
   parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
225
   the value of the CPP_TEMPLATE_ID is whatever was returned by
226
   cp_parser_template_id.  */
227
#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
228
 
229
/* A token type for nested-name-specifiers.  If a
230
   nested-name-specifier is processed while parsing tentatively, it is
231
   replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
232
   CPP_NESTED_NAME_SPECIFIER is whatever was returned by
233
   cp_parser_nested_name_specifier_opt.  */
234
#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
235
 
236
/* A token type for tokens that are not tokens at all; these are used
237
   to represent slots in the array where there used to be a token
238
   that has now been deleted.  */
239
#define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
240
 
241
/* The number of token types, including C++-specific ones.  */
242
#define N_CP_TTYPES ((int) (CPP_PURGED + 1))
243
 
244
/* Variables.  */
245
 
246
#ifdef ENABLE_CHECKING
247
/* The stream to which debugging output should be written.  */
248
static FILE *cp_lexer_debug_stream;
249
#endif /* ENABLE_CHECKING */
250
 
251
/* Nonzero if we are parsing an unevaluated operand: an operand to
252
   sizeof, typeof, or alignof.  */
253
int cp_unevaluated_operand;
254
 
255
/* Create a new main C++ lexer, the lexer that gets tokens from the
256
   preprocessor.  */
257
 
258
static cp_lexer *
259
cp_lexer_new_main (void)
260
{
261
  cp_token first_token;
262
  cp_lexer *lexer;
263
  cp_token *pos;
264
  size_t alloc;
265
  size_t space;
266
  cp_token *buffer;
267
 
268
  /* It's possible that parsing the first pragma will load a PCH file,
269
     which is a GC collection point.  So we have to do that before
270
     allocating any memory.  */
271
  cp_parser_initial_pragma (&first_token);
272
 
273
  c_common_no_more_pch ();
274
 
275
  /* Allocate the memory.  */
276
  lexer = GGC_CNEW (cp_lexer);
277
 
278
#ifdef ENABLE_CHECKING
279
  /* Initially we are not debugging.  */
280
  lexer->debugging_p = false;
281
#endif /* ENABLE_CHECKING */
282
  lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
283
                                   CP_SAVED_TOKEN_STACK);
284
 
285
  /* Create the buffer.  */
286
  alloc = CP_LEXER_BUFFER_SIZE;
287
  buffer = GGC_NEWVEC (cp_token, alloc);
288
 
289
  /* Put the first token in the buffer.  */
290
  space = alloc;
291
  pos = buffer;
292
  *pos = first_token;
293
 
294
  /* Get the remaining tokens from the preprocessor.  */
295
  while (pos->type != CPP_EOF)
296
    {
297
      pos++;
298
      if (!--space)
299
        {
300
          space = alloc;
301
          alloc *= 2;
302
          buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
303
          pos = buffer + space;
304
        }
305
      cp_lexer_get_preprocessor_token (lexer, pos);
306
    }
307
  lexer->buffer = buffer;
308
  lexer->buffer_length = alloc - space;
309
  lexer->last_token = pos;
310
  lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
311
 
312
  /* Subsequent preprocessor diagnostics should use compiler
313
     diagnostic functions to get the compiler source location.  */
314
  done_lexing = true;
315
 
316
  gcc_assert (lexer->next_token->type != CPP_PURGED);
317
  return lexer;
318
}
319
 
320
/* Create a new lexer whose token stream is primed with the tokens in
321
   CACHE.  When these tokens are exhausted, no new tokens will be read.  */
322
 
323
static cp_lexer *
324
cp_lexer_new_from_tokens (cp_token_cache *cache)
325
{
326
  cp_token *first = cache->first;
327
  cp_token *last = cache->last;
328
  cp_lexer *lexer = GGC_CNEW (cp_lexer);
329
 
330
  /* We do not own the buffer.  */
331
  lexer->buffer = NULL;
332
  lexer->buffer_length = 0;
333
  lexer->next_token = first == last ? &eof_token : first;
334
  lexer->last_token = last;
335
 
336
  lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337
                                   CP_SAVED_TOKEN_STACK);
338
 
339
#ifdef ENABLE_CHECKING
340
  /* Initially we are not debugging.  */
341
  lexer->debugging_p = false;
342
#endif
343
 
344
  gcc_assert (lexer->next_token->type != CPP_PURGED);
345
  return lexer;
346
}
347
 
348
/* Frees all resources associated with LEXER.  */
349
 
350
static void
351
cp_lexer_destroy (cp_lexer *lexer)
352
{
353
  if (lexer->buffer)
354
    ggc_free (lexer->buffer);
355
  VEC_free (cp_token_position, heap, lexer->saved_tokens);
356
  ggc_free (lexer);
357
}
358
 
359
/* Returns nonzero if debugging information should be output.  */
360
 
361
#ifdef ENABLE_CHECKING
362
 
363
static inline bool
364
cp_lexer_debugging_p (cp_lexer *lexer)
365
{
366
  return lexer->debugging_p;
367
}
368
 
369
#endif /* ENABLE_CHECKING */
370
 
371
static inline cp_token_position
372
cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373
{
374
  gcc_assert (!previous_p || lexer->next_token != &eof_token);
375
 
376
  return lexer->next_token - previous_p;
377
}
378
 
379
static inline cp_token *
380
cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
381
{
382
  return pos;
383
}
384
 
385
/* nonzero if we are presently saving tokens.  */
386
 
387
static inline int
388
cp_lexer_saving_tokens (const cp_lexer* lexer)
389
{
390
  return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
391
}
392
 
393
/* Store the next token from the preprocessor in *TOKEN.  Return true
394
   if we reach EOF.  If LEXER is NULL, assume we are handling an
395
   initial #pragma pch_preprocess, and thus want the lexer to return
396
   processed strings.  */
397
 
398
static void
399
cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400
{
401
  static int is_extern_c = 0;
402
 
403
   /* Get a new token from the preprocessor.  */
404
  token->type
405
    = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406
                        lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
407
  token->keyword = RID_MAX;
408
  token->pragma_kind = PRAGMA_NONE;
409
 
410
  /* On some systems, some header files are surrounded by an
411
     implicit extern "C" block.  Set a flag in the token if it
412
     comes from such a header.  */
413
  is_extern_c += pending_lang_change;
414
  pending_lang_change = 0;
415
  token->implicit_extern_c = is_extern_c > 0;
416
 
417
  /* Check to see if this token is a keyword.  */
418
  if (token->type == CPP_NAME)
419
    {
420
      if (C_IS_RESERVED_WORD (token->u.value))
421
        {
422
          /* Mark this token as a keyword.  */
423
          token->type = CPP_KEYWORD;
424
          /* Record which keyword.  */
425
          token->keyword = C_RID_CODE (token->u.value);
426
        }
427
      else
428
        {
429
          if (warn_cxx0x_compat
430
              && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
431
              && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
432
            {
433
              /* Warn about the C++0x keyword (but still treat it as
434
                 an identifier).  */
435
              warning (OPT_Wc__0x_compat,
436
                       "identifier %qE will become a keyword in C++0x",
437
                       token->u.value);
438
 
439
              /* Clear out the C_RID_CODE so we don't warn about this
440
                 particular identifier-turned-keyword again.  */
441
              C_SET_RID_CODE (token->u.value, RID_MAX);
442
            }
443
 
444
          token->ambiguous_p = false;
445
          token->keyword = RID_MAX;
446
        }
447
    }
448
  /* Handle Objective-C++ keywords.  */
449
  else if (token->type == CPP_AT_NAME)
450
    {
451
      token->type = CPP_KEYWORD;
452
      switch (C_RID_CODE (token->u.value))
453
        {
454
        /* Map 'class' to '@class', 'private' to '@private', etc.  */
455
        case RID_CLASS: token->keyword = RID_AT_CLASS; break;
456
        case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
457
        case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
458
        case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
459
        case RID_THROW: token->keyword = RID_AT_THROW; break;
460
        case RID_TRY: token->keyword = RID_AT_TRY; break;
461
        case RID_CATCH: token->keyword = RID_AT_CATCH; break;
462
        default: token->keyword = C_RID_CODE (token->u.value);
463
        }
464
    }
465
  else if (token->type == CPP_PRAGMA)
466
    {
467
      /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
468
      token->pragma_kind = ((enum pragma_kind)
469
                            TREE_INT_CST_LOW (token->u.value));
470
      token->u.value = NULL_TREE;
471
    }
472
}
473
 
474
/* Update the globals input_location and the input file stack from TOKEN.  */
475
static inline void
476
cp_lexer_set_source_position_from_token (cp_token *token)
477
{
478
  if (token->type != CPP_EOF)
479
    {
480
      input_location = token->location;
481
    }
482
}
483
 
484
/* Return a pointer to the next token in the token stream, but do not
485
   consume it.  */
486
 
487
static inline cp_token *
488
cp_lexer_peek_token (cp_lexer *lexer)
489
{
490
  if (cp_lexer_debugging_p (lexer))
491
    {
492
      fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
493
      cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
494
      putc ('\n', cp_lexer_debug_stream);
495
    }
496
  return lexer->next_token;
497
}
498
 
499
/* Return true if the next token has the indicated TYPE.  */
500
 
501
static inline bool
502
cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
503
{
504
  return cp_lexer_peek_token (lexer)->type == type;
505
}
506
 
507
/* Return true if the next token does not have the indicated TYPE.  */
508
 
509
static inline bool
510
cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
511
{
512
  return !cp_lexer_next_token_is (lexer, type);
513
}
514
 
515
/* Return true if the next token is the indicated KEYWORD.  */
516
 
517
static inline bool
518
cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
519
{
520
  return cp_lexer_peek_token (lexer)->keyword == keyword;
521
}
522
 
523
/* Return true if the next token is not the indicated KEYWORD.  */
524
 
525
static inline bool
526
cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
527
{
528
  return cp_lexer_peek_token (lexer)->keyword != keyword;
529
}
530
 
531
/* Return true if the next token is a keyword for a decl-specifier.  */
532
 
533
static bool
534
cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
535
{
536
  cp_token *token;
537
 
538
  token = cp_lexer_peek_token (lexer);
539
  switch (token->keyword)
540
    {
541
      /* auto specifier: storage-class-specifier in C++,
542
         simple-type-specifier in C++0x.  */
543
    case RID_AUTO:
544
      /* Storage classes.  */
545
    case RID_REGISTER:
546
    case RID_STATIC:
547
    case RID_EXTERN:
548
    case RID_MUTABLE:
549
    case RID_THREAD:
550
      /* Elaborated type specifiers.  */
551
    case RID_ENUM:
552
    case RID_CLASS:
553
    case RID_STRUCT:
554
    case RID_UNION:
555
    case RID_TYPENAME:
556
      /* Simple type specifiers.  */
557
    case RID_CHAR:
558
    case RID_CHAR16:
559
    case RID_CHAR32:
560
    case RID_WCHAR:
561
    case RID_BOOL:
562
    case RID_SHORT:
563
    case RID_INT:
564
    case RID_LONG:
565
    case RID_SIGNED:
566
    case RID_UNSIGNED:
567
    case RID_FLOAT:
568
    case RID_DOUBLE:
569
    case RID_VOID:
570
      /* GNU extensions.  */
571
    case RID_ATTRIBUTE:
572
    case RID_TYPEOF:
573
      /* C++0x extensions.  */
574
    case RID_DECLTYPE:
575
      return true;
576
 
577
    default:
578
      return false;
579
    }
580
}
581
 
582
/* Return a pointer to the Nth token in the token stream.  If N is 1,
583
   then this is precisely equivalent to cp_lexer_peek_token (except
584
   that it is not inline).  One would like to disallow that case, but
585
   there is one case (cp_parser_nth_token_starts_template_id) where
586
   the caller passes a variable for N and it might be 1.  */
587
 
588
static cp_token *
589
cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
590
{
591
  cp_token *token;
592
 
593
  /* N is 1-based, not zero-based.  */
594
  gcc_assert (n > 0);
595
 
596
  if (cp_lexer_debugging_p (lexer))
597
    fprintf (cp_lexer_debug_stream,
598
             "cp_lexer: peeking ahead %ld at token: ", (long)n);
599
 
600
  --n;
601
  token = lexer->next_token;
602
  gcc_assert (!n || token != &eof_token);
603
  while (n != 0)
604
    {
605
      ++token;
606
      if (token == lexer->last_token)
607
        {
608
          token = &eof_token;
609
          break;
610
        }
611
 
612
      if (token->type != CPP_PURGED)
613
        --n;
614
    }
615
 
616
  if (cp_lexer_debugging_p (lexer))
617
    {
618
      cp_lexer_print_token (cp_lexer_debug_stream, token);
619
      putc ('\n', cp_lexer_debug_stream);
620
    }
621
 
622
  return token;
623
}
624
 
625
/* Return the next token, and advance the lexer's next_token pointer
626
   to point to the next non-purged token.  */
627
 
628
static cp_token *
629
cp_lexer_consume_token (cp_lexer* lexer)
630
{
631
  cp_token *token = lexer->next_token;
632
 
633
  gcc_assert (token != &eof_token);
634
  gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
635
 
636
  do
637
    {
638
      lexer->next_token++;
639
      if (lexer->next_token == lexer->last_token)
640
        {
641
          lexer->next_token = &eof_token;
642
          break;
643
        }
644
 
645
    }
646
  while (lexer->next_token->type == CPP_PURGED);
647
 
648
  cp_lexer_set_source_position_from_token (token);
649
 
650
  /* Provide debugging output.  */
651
  if (cp_lexer_debugging_p (lexer))
652
    {
653
      fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
654
      cp_lexer_print_token (cp_lexer_debug_stream, token);
655
      putc ('\n', cp_lexer_debug_stream);
656
    }
657
 
658
  return token;
659
}
660
 
661
/* Permanently remove the next token from the token stream, and
662
   advance the next_token pointer to refer to the next non-purged
663
   token.  */
664
 
665
static void
666
cp_lexer_purge_token (cp_lexer *lexer)
667
{
668
  cp_token *tok = lexer->next_token;
669
 
670
  gcc_assert (tok != &eof_token);
671
  tok->type = CPP_PURGED;
672
  tok->location = UNKNOWN_LOCATION;
673
  tok->u.value = NULL_TREE;
674
  tok->keyword = RID_MAX;
675
 
676
  do
677
    {
678
      tok++;
679
      if (tok == lexer->last_token)
680
        {
681
          tok = &eof_token;
682
          break;
683
        }
684
    }
685
  while (tok->type == CPP_PURGED);
686
  lexer->next_token = tok;
687
}
688
 
689
/* Permanently remove all tokens after TOK, up to, but not
690
   including, the token that will be returned next by
691
   cp_lexer_peek_token.  */
692
 
693
static void
694
cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
695
{
696
  cp_token *peek = lexer->next_token;
697
 
698
  if (peek == &eof_token)
699
    peek = lexer->last_token;
700
 
701
  gcc_assert (tok < peek);
702
 
703
  for ( tok += 1; tok != peek; tok += 1)
704
    {
705
      tok->type = CPP_PURGED;
706
      tok->location = UNKNOWN_LOCATION;
707
      tok->u.value = NULL_TREE;
708
      tok->keyword = RID_MAX;
709
    }
710
}
711
 
712
/* Begin saving tokens.  All tokens consumed after this point will be
713
   preserved.  */
714
 
715
static void
716
cp_lexer_save_tokens (cp_lexer* lexer)
717
{
718
  /* Provide debugging output.  */
719
  if (cp_lexer_debugging_p (lexer))
720
    fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
721
 
722
  VEC_safe_push (cp_token_position, heap,
723
                 lexer->saved_tokens, lexer->next_token);
724
}
725
 
726
/* Commit to the portion of the token stream most recently saved.  */
727
 
728
static void
729
cp_lexer_commit_tokens (cp_lexer* lexer)
730
{
731
  /* Provide debugging output.  */
732
  if (cp_lexer_debugging_p (lexer))
733
    fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
734
 
735
  VEC_pop (cp_token_position, lexer->saved_tokens);
736
}
737
 
738
/* Return all tokens saved since the last call to cp_lexer_save_tokens
739
   to the token stream.  Stop saving tokens.  */
740
 
741
static void
742
cp_lexer_rollback_tokens (cp_lexer* lexer)
743
{
744
  /* Provide debugging output.  */
745
  if (cp_lexer_debugging_p (lexer))
746
    fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
747
 
748
  lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
749
}
750
 
751
/* Print a representation of the TOKEN on the STREAM.  */
752
 
753
#ifdef ENABLE_CHECKING
754
 
755
static void
756
cp_lexer_print_token (FILE * stream, cp_token *token)
757
{
758
  /* We don't use cpp_type2name here because the parser defines
759
     a few tokens of its own.  */
760
  static const char *const token_names[] = {
761
    /* cpplib-defined token types */
762
#define OP(e, s) #e,
763
#define TK(e, s) #e,
764
    TTYPE_TABLE
765
#undef OP
766
#undef TK
767
    /* C++ parser token types - see "Manifest constants", above.  */
768
    "KEYWORD",
769
    "TEMPLATE_ID",
770
    "NESTED_NAME_SPECIFIER",
771
    "PURGED"
772
  };
773
 
774
  /* If we have a name for the token, print it out.  Otherwise, we
775
     simply give the numeric code.  */
776
  gcc_assert (token->type < ARRAY_SIZE(token_names));
777
  fputs (token_names[token->type], stream);
778
 
779
  /* For some tokens, print the associated data.  */
780
  switch (token->type)
781
    {
782
    case CPP_KEYWORD:
783
      /* Some keywords have a value that is not an IDENTIFIER_NODE.
784
         For example, `struct' is mapped to an INTEGER_CST.  */
785
      if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
786
        break;
787
      /* else fall through */
788
    case CPP_NAME:
789
      fputs (IDENTIFIER_POINTER (token->u.value), stream);
790
      break;
791
 
792
    case CPP_STRING:
793
    case CPP_STRING16:
794
    case CPP_STRING32:
795
    case CPP_WSTRING:
796
    case CPP_UTF8STRING:
797
      fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
798
      break;
799
 
800
    default:
801
      break;
802
    }
803
}
804
 
805
/* Start emitting debugging information.  */
806
 
807
static void
808
cp_lexer_start_debugging (cp_lexer* lexer)
809
{
810
  lexer->debugging_p = true;
811
}
812
 
813
/* Stop emitting debugging information.  */
814
 
815
static void
816
cp_lexer_stop_debugging (cp_lexer* lexer)
817
{
818
  lexer->debugging_p = false;
819
}
820
 
821
#endif /* ENABLE_CHECKING */
822
 
823
/* Create a new cp_token_cache, representing a range of tokens.  */
824
 
825
static cp_token_cache *
826
cp_token_cache_new (cp_token *first, cp_token *last)
827
{
828
  cp_token_cache *cache = GGC_NEW (cp_token_cache);
829
  cache->first = first;
830
  cache->last = last;
831
  return cache;
832
}
833
 
834
 
835
/* Decl-specifiers.  */
836
 
837
/* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
838
 
839
static void
840
clear_decl_specs (cp_decl_specifier_seq *decl_specs)
841
{
842
  memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
843
}
844
 
845
/* Declarators.  */
846
 
847
/* Nothing other than the parser should be creating declarators;
848
   declarators are a semi-syntactic representation of C++ entities.
849
   Other parts of the front end that need to create entities (like
850
   VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
851
 
852
static cp_declarator *make_call_declarator
853
  (cp_declarator *, tree, cp_cv_quals, tree, tree);
854
static cp_declarator *make_array_declarator
855
  (cp_declarator *, tree);
856
static cp_declarator *make_pointer_declarator
857
  (cp_cv_quals, cp_declarator *);
858
static cp_declarator *make_reference_declarator
859
  (cp_cv_quals, cp_declarator *, bool);
860
static cp_parameter_declarator *make_parameter_declarator
861
  (cp_decl_specifier_seq *, cp_declarator *, tree);
862
static cp_declarator *make_ptrmem_declarator
863
  (cp_cv_quals, tree, cp_declarator *);
864
 
865
/* An erroneous declarator.  */
866
static cp_declarator *cp_error_declarator;
867
 
868
/* The obstack on which declarators and related data structures are
869
   allocated.  */
870
static struct obstack declarator_obstack;
871
 
872
/* Alloc BYTES from the declarator memory pool.  */
873
 
874
static inline void *
875
alloc_declarator (size_t bytes)
876
{
877
  return obstack_alloc (&declarator_obstack, bytes);
878
}
879
 
880
/* Allocate a declarator of the indicated KIND.  Clear fields that are
881
   common to all declarators.  */
882
 
883
static cp_declarator *
884
make_declarator (cp_declarator_kind kind)
885
{
886
  cp_declarator *declarator;
887
 
888
  declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
889
  declarator->kind = kind;
890
  declarator->attributes = NULL_TREE;
891
  declarator->declarator = NULL;
892
  declarator->parameter_pack_p = false;
893
  declarator->id_loc = UNKNOWN_LOCATION;
894
 
895
  return declarator;
896
}
897
 
898
/* Make a declarator for a generalized identifier.  If
899
   QUALIFYING_SCOPE is non-NULL, the identifier is
900
   QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
901
   UNQUALIFIED_NAME.  SFK indicates the kind of special function this
902
   is, if any.   */
903
 
904
static cp_declarator *
905
make_id_declarator (tree qualifying_scope, tree unqualified_name,
906
                    special_function_kind sfk)
907
{
908
  cp_declarator *declarator;
909
 
910
  /* It is valid to write:
911
 
912
       class C { void f(); };
913
       typedef C D;
914
       void D::f();
915
 
916
     The standard is not clear about whether `typedef const C D' is
917
     legal; as of 2002-09-15 the committee is considering that
918
     question.  EDG 3.0 allows that syntax.  Therefore, we do as
919
     well.  */
920
  if (qualifying_scope && TYPE_P (qualifying_scope))
921
    qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
922
 
923
  gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
924
              || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
925
              || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
926
 
927
  declarator = make_declarator (cdk_id);
928
  declarator->u.id.qualifying_scope = qualifying_scope;
929
  declarator->u.id.unqualified_name = unqualified_name;
930
  declarator->u.id.sfk = sfk;
931
 
932
  return declarator;
933
}
934
 
935
/* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
936
   of modifiers such as const or volatile to apply to the pointer
937
   type, represented as identifiers.  */
938
 
939
cp_declarator *
940
make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
941
{
942
  cp_declarator *declarator;
943
 
944
  declarator = make_declarator (cdk_pointer);
945
  declarator->declarator = target;
946
  declarator->u.pointer.qualifiers = cv_qualifiers;
947
  declarator->u.pointer.class_type = NULL_TREE;
948
  if (target)
949
    {
950
      declarator->parameter_pack_p = target->parameter_pack_p;
951
      target->parameter_pack_p = false;
952
    }
953
  else
954
    declarator->parameter_pack_p = false;
955
 
956
  return declarator;
957
}
958
 
959
/* Like make_pointer_declarator -- but for references.  */
960
 
961
cp_declarator *
962
make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
963
                           bool rvalue_ref)
964
{
965
  cp_declarator *declarator;
966
 
967
  declarator = make_declarator (cdk_reference);
968
  declarator->declarator = target;
969
  declarator->u.reference.qualifiers = cv_qualifiers;
970
  declarator->u.reference.rvalue_ref = rvalue_ref;
971
  if (target)
972
    {
973
      declarator->parameter_pack_p = target->parameter_pack_p;
974
      target->parameter_pack_p = false;
975
    }
976
  else
977
    declarator->parameter_pack_p = false;
978
 
979
  return declarator;
980
}
981
 
982
/* Like make_pointer_declarator -- but for a pointer to a non-static
983
   member of CLASS_TYPE.  */
984
 
985
cp_declarator *
986
make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
987
                        cp_declarator *pointee)
988
{
989
  cp_declarator *declarator;
990
 
991
  declarator = make_declarator (cdk_ptrmem);
992
  declarator->declarator = pointee;
993
  declarator->u.pointer.qualifiers = cv_qualifiers;
994
  declarator->u.pointer.class_type = class_type;
995
 
996
  if (pointee)
997
    {
998
      declarator->parameter_pack_p = pointee->parameter_pack_p;
999
      pointee->parameter_pack_p = false;
1000
    }
1001
  else
1002
    declarator->parameter_pack_p = false;
1003
 
1004
  return declarator;
1005
}
1006
 
1007
/* Make a declarator for the function given by TARGET, with the
1008
   indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1009
   "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1010
   indicates what exceptions can be thrown.  */
1011
 
1012
cp_declarator *
1013
make_call_declarator (cp_declarator *target,
1014
                      tree parms,
1015
                      cp_cv_quals cv_qualifiers,
1016
                      tree exception_specification,
1017
                      tree late_return_type)
1018
{
1019
  cp_declarator *declarator;
1020
 
1021
  declarator = make_declarator (cdk_function);
1022
  declarator->declarator = target;
1023
  declarator->u.function.parameters = parms;
1024
  declarator->u.function.qualifiers = cv_qualifiers;
1025
  declarator->u.function.exception_specification = exception_specification;
1026
  declarator->u.function.late_return_type = late_return_type;
1027
  if (target)
1028
    {
1029
      declarator->parameter_pack_p = target->parameter_pack_p;
1030
      target->parameter_pack_p = false;
1031
    }
1032
  else
1033
    declarator->parameter_pack_p = false;
1034
 
1035
  return declarator;
1036
}
1037
 
1038
/* Make a declarator for an array of BOUNDS elements, each of which is
1039
   defined by ELEMENT.  */
1040
 
1041
cp_declarator *
1042
make_array_declarator (cp_declarator *element, tree bounds)
1043
{
1044
  cp_declarator *declarator;
1045
 
1046
  declarator = make_declarator (cdk_array);
1047
  declarator->declarator = element;
1048
  declarator->u.array.bounds = bounds;
1049
  if (element)
1050
    {
1051
      declarator->parameter_pack_p = element->parameter_pack_p;
1052
      element->parameter_pack_p = false;
1053
    }
1054
  else
1055
    declarator->parameter_pack_p = false;
1056
 
1057
  return declarator;
1058
}
1059
 
1060
/* Determine whether the declarator we've seen so far can be a
1061
   parameter pack, when followed by an ellipsis.  */
1062
static bool
1063
declarator_can_be_parameter_pack (cp_declarator *declarator)
1064
{
1065
  /* Search for a declarator name, or any other declarator that goes
1066
     after the point where the ellipsis could appear in a parameter
1067
     pack. If we find any of these, then this declarator can not be
1068
     made into a parameter pack.  */
1069
  bool found = false;
1070
  while (declarator && !found)
1071
    {
1072
      switch ((int)declarator->kind)
1073
        {
1074
        case cdk_id:
1075
        case cdk_array:
1076
          found = true;
1077
          break;
1078
 
1079
        case cdk_error:
1080
          return true;
1081
 
1082
        default:
1083
          declarator = declarator->declarator;
1084
          break;
1085
        }
1086
    }
1087
 
1088
  return !found;
1089
}
1090
 
1091
cp_parameter_declarator *no_parameters;
1092
 
1093
/* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1094
   DECLARATOR and DEFAULT_ARGUMENT.  */
1095
 
1096
cp_parameter_declarator *
1097
make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1098
                           cp_declarator *declarator,
1099
                           tree default_argument)
1100
{
1101
  cp_parameter_declarator *parameter;
1102
 
1103
  parameter = ((cp_parameter_declarator *)
1104
               alloc_declarator (sizeof (cp_parameter_declarator)));
1105
  parameter->next = NULL;
1106
  if (decl_specifiers)
1107
    parameter->decl_specifiers = *decl_specifiers;
1108
  else
1109
    clear_decl_specs (&parameter->decl_specifiers);
1110
  parameter->declarator = declarator;
1111
  parameter->default_argument = default_argument;
1112
  parameter->ellipsis_p = false;
1113
 
1114
  return parameter;
1115
}
1116
 
1117
/* Returns true iff DECLARATOR  is a declaration for a function.  */
1118
 
1119
static bool
1120
function_declarator_p (const cp_declarator *declarator)
1121
{
1122
  while (declarator)
1123
    {
1124
      if (declarator->kind == cdk_function
1125
          && declarator->declarator->kind == cdk_id)
1126
        return true;
1127
      if (declarator->kind == cdk_id
1128
          || declarator->kind == cdk_error)
1129
        return false;
1130
      declarator = declarator->declarator;
1131
    }
1132
  return false;
1133
}
1134
 
1135
/* The parser.  */
1136
 
1137
/* Overview
1138
   --------
1139
 
1140
   A cp_parser parses the token stream as specified by the C++
1141
   grammar.  Its job is purely parsing, not semantic analysis.  For
1142
   example, the parser breaks the token stream into declarators,
1143
   expressions, statements, and other similar syntactic constructs.
1144
   It does not check that the types of the expressions on either side
1145
   of an assignment-statement are compatible, or that a function is
1146
   not declared with a parameter of type `void'.
1147
 
1148
   The parser invokes routines elsewhere in the compiler to perform
1149
   semantic analysis and to build up the abstract syntax tree for the
1150
   code processed.
1151
 
1152
   The parser (and the template instantiation code, which is, in a
1153
   way, a close relative of parsing) are the only parts of the
1154
   compiler that should be calling push_scope and pop_scope, or
1155
   related functions.  The parser (and template instantiation code)
1156
   keeps track of what scope is presently active; everything else
1157
   should simply honor that.  (The code that generates static
1158
   initializers may also need to set the scope, in order to check
1159
   access control correctly when emitting the initializers.)
1160
 
1161
   Methodology
1162
   -----------
1163
 
1164
   The parser is of the standard recursive-descent variety.  Upcoming
1165
   tokens in the token stream are examined in order to determine which
1166
   production to use when parsing a non-terminal.  Some C++ constructs
1167
   require arbitrary look ahead to disambiguate.  For example, it is
1168
   impossible, in the general case, to tell whether a statement is an
1169
   expression or declaration without scanning the entire statement.
1170
   Therefore, the parser is capable of "parsing tentatively."  When the
1171
   parser is not sure what construct comes next, it enters this mode.
1172
   Then, while we attempt to parse the construct, the parser queues up
1173
   error messages, rather than issuing them immediately, and saves the
1174
   tokens it consumes.  If the construct is parsed successfully, the
1175
   parser "commits", i.e., it issues any queued error messages and
1176
   the tokens that were being preserved are permanently discarded.
1177
   If, however, the construct is not parsed successfully, the parser
1178
   rolls back its state completely so that it can resume parsing using
1179
   a different alternative.
1180
 
1181
   Future Improvements
1182
   -------------------
1183
 
1184
   The performance of the parser could probably be improved substantially.
1185
   We could often eliminate the need to parse tentatively by looking ahead
1186
   a little bit.  In some places, this approach might not entirely eliminate
1187
   the need to parse tentatively, but it might still speed up the average
1188
   case.  */
1189
 
1190
/* Flags that are passed to some parsing functions.  These values can
1191
   be bitwise-ored together.  */
1192
 
1193
enum
1194
{
1195
  /* No flags.  */
1196
  CP_PARSER_FLAGS_NONE = 0x0,
1197
  /* The construct is optional.  If it is not present, then no error
1198
     should be issued.  */
1199
  CP_PARSER_FLAGS_OPTIONAL = 0x1,
1200
  /* When parsing a type-specifier, treat user-defined type-names
1201
     as non-type identifiers.  */
1202
  CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1203
  /* When parsing a type-specifier, do not try to parse a class-specifier
1204
     or enum-specifier.  */
1205
  CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1206
};
1207
 
1208
/* This type is used for parameters and variables which hold
1209
   combinations of the above flags.  */
1210
typedef int cp_parser_flags;
1211
 
1212
/* The different kinds of declarators we want to parse.  */
1213
 
1214
typedef enum cp_parser_declarator_kind
1215
{
1216
  /* We want an abstract declarator.  */
1217
  CP_PARSER_DECLARATOR_ABSTRACT,
1218
  /* We want a named declarator.  */
1219
  CP_PARSER_DECLARATOR_NAMED,
1220
  /* We don't mind, but the name must be an unqualified-id.  */
1221
  CP_PARSER_DECLARATOR_EITHER
1222
} cp_parser_declarator_kind;
1223
 
1224
/* The precedence values used to parse binary expressions.  The minimum value
1225
   of PREC must be 1, because zero is reserved to quickly discriminate
1226
   binary operators from other tokens.  */
1227
 
1228
enum cp_parser_prec
1229
{
1230
  PREC_NOT_OPERATOR,
1231
  PREC_LOGICAL_OR_EXPRESSION,
1232
  PREC_LOGICAL_AND_EXPRESSION,
1233
  PREC_INCLUSIVE_OR_EXPRESSION,
1234
  PREC_EXCLUSIVE_OR_EXPRESSION,
1235
  PREC_AND_EXPRESSION,
1236
  PREC_EQUALITY_EXPRESSION,
1237
  PREC_RELATIONAL_EXPRESSION,
1238
  PREC_SHIFT_EXPRESSION,
1239
  PREC_ADDITIVE_EXPRESSION,
1240
  PREC_MULTIPLICATIVE_EXPRESSION,
1241
  PREC_PM_EXPRESSION,
1242
  NUM_PREC_VALUES = PREC_PM_EXPRESSION
1243
};
1244
 
1245
/* A mapping from a token type to a corresponding tree node type, with a
1246
   precedence value.  */
1247
 
1248
typedef struct cp_parser_binary_operations_map_node
1249
{
1250
  /* The token type.  */
1251
  enum cpp_ttype token_type;
1252
  /* The corresponding tree code.  */
1253
  enum tree_code tree_type;
1254
  /* The precedence of this operator.  */
1255
  enum cp_parser_prec prec;
1256
} cp_parser_binary_operations_map_node;
1257
 
1258
/* The status of a tentative parse.  */
1259
 
1260
typedef enum cp_parser_status_kind
1261
{
1262
  /* No errors have occurred.  */
1263
  CP_PARSER_STATUS_KIND_NO_ERROR,
1264
  /* An error has occurred.  */
1265
  CP_PARSER_STATUS_KIND_ERROR,
1266
  /* We are committed to this tentative parse, whether or not an error
1267
     has occurred.  */
1268
  CP_PARSER_STATUS_KIND_COMMITTED
1269
} cp_parser_status_kind;
1270
 
1271
typedef struct cp_parser_expression_stack_entry
1272
{
1273
  /* Left hand side of the binary operation we are currently
1274
     parsing.  */
1275
  tree lhs;
1276
  /* Original tree code for left hand side, if it was a binary
1277
     expression itself (used for -Wparentheses).  */
1278
  enum tree_code lhs_type;
1279
  /* Tree code for the binary operation we are parsing.  */
1280
  enum tree_code tree_type;
1281
  /* Precedence of the binary operation we are parsing.  */
1282
  enum cp_parser_prec prec;
1283
} cp_parser_expression_stack_entry;
1284
 
1285
/* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1286
   entries because precedence levels on the stack are monotonically
1287
   increasing.  */
1288
typedef struct cp_parser_expression_stack_entry
1289
  cp_parser_expression_stack[NUM_PREC_VALUES];
1290
 
1291
/* Context that is saved and restored when parsing tentatively.  */
1292
typedef struct GTY (()) cp_parser_context {
1293
  /* If this is a tentative parsing context, the status of the
1294
     tentative parse.  */
1295
  enum cp_parser_status_kind status;
1296
  /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1297
     that are looked up in this context must be looked up both in the
1298
     scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1299
     the context of the containing expression.  */
1300
  tree object_type;
1301
 
1302
  /* The next parsing context in the stack.  */
1303
  struct cp_parser_context *next;
1304
} cp_parser_context;
1305
 
1306
/* Prototypes.  */
1307
 
1308
/* Constructors and destructors.  */
1309
 
1310
static cp_parser_context *cp_parser_context_new
1311
  (cp_parser_context *);
1312
 
1313
/* Class variables.  */
1314
 
1315
static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1316
 
1317
/* The operator-precedence table used by cp_parser_binary_expression.
1318
   Transformed into an associative array (binops_by_token) by
1319
   cp_parser_new.  */
1320
 
1321
static const cp_parser_binary_operations_map_node binops[] = {
1322
  { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1323
  { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1324
 
1325
  { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1326
  { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1327
  { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1328
 
1329
  { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1330
  { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1331
 
1332
  { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1333
  { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1334
 
1335
  { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1336
  { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1337
  { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1338
  { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1339
 
1340
  { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1341
  { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1342
 
1343
  { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1344
 
1345
  { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1346
 
1347
  { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1348
 
1349
  { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1350
 
1351
  { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1352
};
1353
 
1354
/* The same as binops, but initialized by cp_parser_new so that
1355
   binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1356
   for speed.  */
1357
static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1358
 
1359
/* Constructors and destructors.  */
1360
 
1361
/* Construct a new context.  The context below this one on the stack
1362
   is given by NEXT.  */
1363
 
1364
static cp_parser_context *
1365
cp_parser_context_new (cp_parser_context* next)
1366
{
1367
  cp_parser_context *context;
1368
 
1369
  /* Allocate the storage.  */
1370
  if (cp_parser_context_free_list != NULL)
1371
    {
1372
      /* Pull the first entry from the free list.  */
1373
      context = cp_parser_context_free_list;
1374
      cp_parser_context_free_list = context->next;
1375
      memset (context, 0, sizeof (*context));
1376
    }
1377
  else
1378
    context = GGC_CNEW (cp_parser_context);
1379
 
1380
  /* No errors have occurred yet in this context.  */
1381
  context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1382
  /* If this is not the bottommost context, copy information that we
1383
     need from the previous context.  */
1384
  if (next)
1385
    {
1386
      /* If, in the NEXT context, we are parsing an `x->' or `x.'
1387
         expression, then we are parsing one in this context, too.  */
1388
      context->object_type = next->object_type;
1389
      /* Thread the stack.  */
1390
      context->next = next;
1391
    }
1392
 
1393
  return context;
1394
}
1395
 
1396
/* The cp_parser structure represents the C++ parser.  */
1397
 
1398
typedef struct GTY(()) cp_parser {
1399
  /* The lexer from which we are obtaining tokens.  */
1400
  cp_lexer *lexer;
1401
 
1402
  /* The scope in which names should be looked up.  If NULL_TREE, then
1403
     we look up names in the scope that is currently open in the
1404
     source program.  If non-NULL, this is either a TYPE or
1405
     NAMESPACE_DECL for the scope in which we should look.  It can
1406
     also be ERROR_MARK, when we've parsed a bogus scope.
1407
 
1408
     This value is not cleared automatically after a name is looked
1409
     up, so we must be careful to clear it before starting a new look
1410
     up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1411
     will look up `Z' in the scope of `X', rather than the current
1412
     scope.)  Unfortunately, it is difficult to tell when name lookup
1413
     is complete, because we sometimes peek at a token, look it up,
1414
     and then decide not to consume it.   */
1415
  tree scope;
1416
 
1417
  /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1418
     last lookup took place.  OBJECT_SCOPE is used if an expression
1419
     like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1420
     respectively.  QUALIFYING_SCOPE is used for an expression of the
1421
     form "X::Y"; it refers to X.  */
1422
  tree object_scope;
1423
  tree qualifying_scope;
1424
 
1425
  /* A stack of parsing contexts.  All but the bottom entry on the
1426
     stack will be tentative contexts.
1427
 
1428
     We parse tentatively in order to determine which construct is in
1429
     use in some situations.  For example, in order to determine
1430
     whether a statement is an expression-statement or a
1431
     declaration-statement we parse it tentatively as a
1432
     declaration-statement.  If that fails, we then reparse the same
1433
     token stream as an expression-statement.  */
1434
  cp_parser_context *context;
1435
 
1436
  /* True if we are parsing GNU C++.  If this flag is not set, then
1437
     GNU extensions are not recognized.  */
1438
  bool allow_gnu_extensions_p;
1439
 
1440
  /* TRUE if the `>' token should be interpreted as the greater-than
1441
     operator.  FALSE if it is the end of a template-id or
1442
     template-parameter-list. In C++0x mode, this flag also applies to
1443
     `>>' tokens, which are viewed as two consecutive `>' tokens when
1444
     this flag is FALSE.  */
1445
  bool greater_than_is_operator_p;
1446
 
1447
  /* TRUE if default arguments are allowed within a parameter list
1448
     that starts at this point. FALSE if only a gnu extension makes
1449
     them permissible.  */
1450
  bool default_arg_ok_p;
1451
 
1452
  /* TRUE if we are parsing an integral constant-expression.  See
1453
     [expr.const] for a precise definition.  */
1454
  bool integral_constant_expression_p;
1455
 
1456
  /* TRUE if we are parsing an integral constant-expression -- but a
1457
     non-constant expression should be permitted as well.  This flag
1458
     is used when parsing an array bound so that GNU variable-length
1459
     arrays are tolerated.  */
1460
  bool allow_non_integral_constant_expression_p;
1461
 
1462
  /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1463
     been seen that makes the expression non-constant.  */
1464
  bool non_integral_constant_expression_p;
1465
 
1466
  /* TRUE if local variable names and `this' are forbidden in the
1467
     current context.  */
1468
  bool local_variables_forbidden_p;
1469
 
1470
  /* TRUE if the declaration we are parsing is part of a
1471
     linkage-specification of the form `extern string-literal
1472
     declaration'.  */
1473
  bool in_unbraced_linkage_specification_p;
1474
 
1475
  /* TRUE if we are presently parsing a declarator, after the
1476
     direct-declarator.  */
1477
  bool in_declarator_p;
1478
 
1479
  /* TRUE if we are presently parsing a template-argument-list.  */
1480
  bool in_template_argument_list_p;
1481
 
1482
  /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1483
     to IN_OMP_BLOCK if parsing OpenMP structured block and
1484
     IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1485
     this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1486
     iteration-statement, OpenMP block or loop within that switch.  */
1487
#define IN_SWITCH_STMT          1
1488
#define IN_ITERATION_STMT       2
1489
#define IN_OMP_BLOCK            4
1490
#define IN_OMP_FOR              8
1491
#define IN_IF_STMT             16
1492
  unsigned char in_statement;
1493
 
1494
  /* TRUE if we are presently parsing the body of a switch statement.
1495
     Note that this doesn't quite overlap with in_statement above.
1496
     The difference relates to giving the right sets of error messages:
1497
     "case not in switch" vs "break statement used with OpenMP...".  */
1498
  bool in_switch_statement_p;
1499
 
1500
  /* TRUE if we are parsing a type-id in an expression context.  In
1501
     such a situation, both "type (expr)" and "type (type)" are valid
1502
     alternatives.  */
1503
  bool in_type_id_in_expr_p;
1504
 
1505
  /* TRUE if we are currently in a header file where declarations are
1506
     implicitly extern "C".  */
1507
  bool implicit_extern_c;
1508
 
1509
  /* TRUE if strings in expressions should be translated to the execution
1510
     character set.  */
1511
  bool translate_strings_p;
1512
 
1513
  /* TRUE if we are presently parsing the body of a function, but not
1514
     a local class.  */
1515
  bool in_function_body;
1516
 
1517
  /* If non-NULL, then we are parsing a construct where new type
1518
     definitions are not permitted.  The string stored here will be
1519
     issued as an error message if a type is defined.  */
1520
  const char *type_definition_forbidden_message;
1521
 
1522
  /* A list of lists. The outer list is a stack, used for member
1523
     functions of local classes. At each level there are two sub-list,
1524
     one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1525
     sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1526
     TREE_VALUE's. The functions are chained in reverse declaration
1527
     order.
1528
 
1529
     The TREE_PURPOSE sublist contains those functions with default
1530
     arguments that need post processing, and the TREE_VALUE sublist
1531
     contains those functions with definitions that need post
1532
     processing.
1533
 
1534
     These lists can only be processed once the outermost class being
1535
     defined is complete.  */
1536
  tree unparsed_functions_queues;
1537
 
1538
  /* The number of classes whose definitions are currently in
1539
     progress.  */
1540
  unsigned num_classes_being_defined;
1541
 
1542
  /* The number of template parameter lists that apply directly to the
1543
     current declaration.  */
1544
  unsigned num_template_parameter_lists;
1545
} cp_parser;
1546
 
1547
/* Prototypes.  */
1548
 
1549
/* Constructors and destructors.  */
1550
 
1551
static cp_parser *cp_parser_new
1552
  (void);
1553
 
1554
/* Routines to parse various constructs.
1555
 
1556
   Those that return `tree' will return the error_mark_node (rather
1557
   than NULL_TREE) if a parse error occurs, unless otherwise noted.
1558
   Sometimes, they will return an ordinary node if error-recovery was
1559
   attempted, even though a parse error occurred.  So, to check
1560
   whether or not a parse error occurred, you should always use
1561
   cp_parser_error_occurred.  If the construct is optional (indicated
1562
   either by an `_opt' in the name of the function that does the
1563
   parsing or via a FLAGS parameter), then NULL_TREE is returned if
1564
   the construct is not present.  */
1565
 
1566
/* Lexical conventions [gram.lex]  */
1567
 
1568
static tree cp_parser_identifier
1569
  (cp_parser *);
1570
static tree cp_parser_string_literal
1571
  (cp_parser *, bool, bool);
1572
 
1573
/* Basic concepts [gram.basic]  */
1574
 
1575
static bool cp_parser_translation_unit
1576
  (cp_parser *);
1577
 
1578
/* Expressions [gram.expr]  */
1579
 
1580
static tree cp_parser_primary_expression
1581
  (cp_parser *, bool, bool, bool, cp_id_kind *);
1582
static tree cp_parser_id_expression
1583
  (cp_parser *, bool, bool, bool *, bool, bool);
1584
static tree cp_parser_unqualified_id
1585
  (cp_parser *, bool, bool, bool, bool);
1586
static tree cp_parser_nested_name_specifier_opt
1587
  (cp_parser *, bool, bool, bool, bool);
1588
static tree cp_parser_nested_name_specifier
1589
  (cp_parser *, bool, bool, bool, bool);
1590
static tree cp_parser_qualifying_entity
1591
  (cp_parser *, bool, bool, bool, bool, bool);
1592
static tree cp_parser_postfix_expression
1593
  (cp_parser *, bool, bool, bool, cp_id_kind *);
1594
static tree cp_parser_postfix_open_square_expression
1595
  (cp_parser *, tree, bool);
1596
static tree cp_parser_postfix_dot_deref_expression
1597
  (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1598
static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1599
  (cp_parser *, bool, bool, bool, bool *);
1600
static void cp_parser_pseudo_destructor_name
1601
  (cp_parser *, tree *, tree *);
1602
static tree cp_parser_unary_expression
1603
  (cp_parser *, bool, bool, cp_id_kind *);
1604
static enum tree_code cp_parser_unary_operator
1605
  (cp_token *);
1606
static tree cp_parser_new_expression
1607
  (cp_parser *);
1608
static VEC(tree,gc) *cp_parser_new_placement
1609
  (cp_parser *);
1610
static tree cp_parser_new_type_id
1611
  (cp_parser *, tree *);
1612
static cp_declarator *cp_parser_new_declarator_opt
1613
  (cp_parser *);
1614
static cp_declarator *cp_parser_direct_new_declarator
1615
  (cp_parser *);
1616
static VEC(tree,gc) *cp_parser_new_initializer
1617
  (cp_parser *);
1618
static tree cp_parser_delete_expression
1619
  (cp_parser *);
1620
static tree cp_parser_cast_expression
1621
  (cp_parser *, bool, bool, cp_id_kind *);
1622
static tree cp_parser_binary_expression
1623
  (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1624
static tree cp_parser_question_colon_clause
1625
  (cp_parser *, tree);
1626
static tree cp_parser_assignment_expression
1627
  (cp_parser *, bool, cp_id_kind *);
1628
static enum tree_code cp_parser_assignment_operator_opt
1629
  (cp_parser *);
1630
static tree cp_parser_expression
1631
  (cp_parser *, bool, cp_id_kind *);
1632
static tree cp_parser_constant_expression
1633
  (cp_parser *, bool, bool *);
1634
static tree cp_parser_builtin_offsetof
1635
  (cp_parser *);
1636
static tree cp_parser_lambda_expression
1637
  (cp_parser *);
1638
static void cp_parser_lambda_introducer
1639
  (cp_parser *, tree);
1640
static void cp_parser_lambda_declarator_opt
1641
  (cp_parser *, tree);
1642
static void cp_parser_lambda_body
1643
  (cp_parser *, tree);
1644
 
1645
/* Statements [gram.stmt.stmt]  */
1646
 
1647
static void cp_parser_statement
1648
  (cp_parser *, tree, bool, bool *);
1649
static void cp_parser_label_for_labeled_statement
1650
  (cp_parser *);
1651
static tree cp_parser_expression_statement
1652
  (cp_parser *, tree);
1653
static tree cp_parser_compound_statement
1654
  (cp_parser *, tree, bool);
1655
static void cp_parser_statement_seq_opt
1656
  (cp_parser *, tree);
1657
static tree cp_parser_selection_statement
1658
  (cp_parser *, bool *);
1659
static tree cp_parser_condition
1660
  (cp_parser *);
1661
static tree cp_parser_iteration_statement
1662
  (cp_parser *);
1663
static void cp_parser_for_init_statement
1664
  (cp_parser *);
1665
static tree cp_parser_jump_statement
1666
  (cp_parser *);
1667
static void cp_parser_declaration_statement
1668
  (cp_parser *);
1669
 
1670
static tree cp_parser_implicitly_scoped_statement
1671
  (cp_parser *, bool *);
1672
static void cp_parser_already_scoped_statement
1673
  (cp_parser *);
1674
 
1675
/* Declarations [gram.dcl.dcl] */
1676
 
1677
static void cp_parser_declaration_seq_opt
1678
  (cp_parser *);
1679
static void cp_parser_declaration
1680
  (cp_parser *);
1681
static void cp_parser_block_declaration
1682
  (cp_parser *, bool);
1683
static void cp_parser_simple_declaration
1684
  (cp_parser *, bool);
1685
static void cp_parser_decl_specifier_seq
1686
  (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1687
static tree cp_parser_storage_class_specifier_opt
1688
  (cp_parser *);
1689
static tree cp_parser_function_specifier_opt
1690
  (cp_parser *, cp_decl_specifier_seq *);
1691
static tree cp_parser_type_specifier
1692
  (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1693
   int *, bool *);
1694
static tree cp_parser_simple_type_specifier
1695
  (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1696
static tree cp_parser_type_name
1697
  (cp_parser *);
1698
static tree cp_parser_nonclass_name
1699
  (cp_parser* parser);
1700
static tree cp_parser_elaborated_type_specifier
1701
  (cp_parser *, bool, bool);
1702
static tree cp_parser_enum_specifier
1703
  (cp_parser *);
1704
static void cp_parser_enumerator_list
1705
  (cp_parser *, tree);
1706
static void cp_parser_enumerator_definition
1707
  (cp_parser *, tree);
1708
static tree cp_parser_namespace_name
1709
  (cp_parser *);
1710
static void cp_parser_namespace_definition
1711
  (cp_parser *);
1712
static void cp_parser_namespace_body
1713
  (cp_parser *);
1714
static tree cp_parser_qualified_namespace_specifier
1715
  (cp_parser *);
1716
static void cp_parser_namespace_alias_definition
1717
  (cp_parser *);
1718
static bool cp_parser_using_declaration
1719
  (cp_parser *, bool);
1720
static void cp_parser_using_directive
1721
  (cp_parser *);
1722
static void cp_parser_asm_definition
1723
  (cp_parser *);
1724
static void cp_parser_linkage_specification
1725
  (cp_parser *);
1726
static void cp_parser_static_assert
1727
  (cp_parser *, bool);
1728
static tree cp_parser_decltype
1729
  (cp_parser *);
1730
 
1731
/* Declarators [gram.dcl.decl] */
1732
 
1733
static tree cp_parser_init_declarator
1734
  (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1735
static cp_declarator *cp_parser_declarator
1736
  (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1737
static cp_declarator *cp_parser_direct_declarator
1738
  (cp_parser *, cp_parser_declarator_kind, int *, bool);
1739
static enum tree_code cp_parser_ptr_operator
1740
  (cp_parser *, tree *, cp_cv_quals *);
1741
static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1742
  (cp_parser *);
1743
static tree cp_parser_late_return_type_opt
1744
  (cp_parser *);
1745
static tree cp_parser_declarator_id
1746
  (cp_parser *, bool);
1747
static tree cp_parser_type_id
1748
  (cp_parser *);
1749
static tree cp_parser_template_type_arg
1750
  (cp_parser *);
1751
static tree cp_parser_trailing_type_id (cp_parser *);
1752
static tree cp_parser_type_id_1
1753
  (cp_parser *, bool, bool);
1754
static void cp_parser_type_specifier_seq
1755
  (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1756
static tree cp_parser_parameter_declaration_clause
1757
  (cp_parser *);
1758
static tree cp_parser_parameter_declaration_list
1759
  (cp_parser *, bool *);
1760
static cp_parameter_declarator *cp_parser_parameter_declaration
1761
  (cp_parser *, bool, bool *);
1762
static tree cp_parser_default_argument
1763
  (cp_parser *, bool);
1764
static void cp_parser_function_body
1765
  (cp_parser *);
1766
static tree cp_parser_initializer
1767
  (cp_parser *, bool *, bool *);
1768
static tree cp_parser_initializer_clause
1769
  (cp_parser *, bool *);
1770
static tree cp_parser_braced_list
1771
  (cp_parser*, bool*);
1772
static VEC(constructor_elt,gc) *cp_parser_initializer_list
1773
  (cp_parser *, bool *);
1774
 
1775
static bool cp_parser_ctor_initializer_opt_and_function_body
1776
  (cp_parser *);
1777
 
1778
/* Classes [gram.class] */
1779
 
1780
static tree cp_parser_class_name
1781
  (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1782
static tree cp_parser_class_specifier
1783
  (cp_parser *);
1784
static tree cp_parser_class_head
1785
  (cp_parser *, bool *, tree *, tree *);
1786
static enum tag_types cp_parser_class_key
1787
  (cp_parser *);
1788
static void cp_parser_member_specification_opt
1789
  (cp_parser *);
1790
static void cp_parser_member_declaration
1791
  (cp_parser *);
1792
static tree cp_parser_pure_specifier
1793
  (cp_parser *);
1794
static tree cp_parser_constant_initializer
1795
  (cp_parser *);
1796
 
1797
/* Derived classes [gram.class.derived] */
1798
 
1799
static tree cp_parser_base_clause
1800
  (cp_parser *);
1801
static tree cp_parser_base_specifier
1802
  (cp_parser *);
1803
 
1804
/* Special member functions [gram.special] */
1805
 
1806
static tree cp_parser_conversion_function_id
1807
  (cp_parser *);
1808
static tree cp_parser_conversion_type_id
1809
  (cp_parser *);
1810
static cp_declarator *cp_parser_conversion_declarator_opt
1811
  (cp_parser *);
1812
static bool cp_parser_ctor_initializer_opt
1813
  (cp_parser *);
1814
static void cp_parser_mem_initializer_list
1815
  (cp_parser *);
1816
static tree cp_parser_mem_initializer
1817
  (cp_parser *);
1818
static tree cp_parser_mem_initializer_id
1819
  (cp_parser *);
1820
 
1821
/* Overloading [gram.over] */
1822
 
1823
static tree cp_parser_operator_function_id
1824
  (cp_parser *);
1825
static tree cp_parser_operator
1826
  (cp_parser *);
1827
 
1828
/* Templates [gram.temp] */
1829
 
1830
static void cp_parser_template_declaration
1831
  (cp_parser *, bool);
1832
static tree cp_parser_template_parameter_list
1833
  (cp_parser *);
1834
static tree cp_parser_template_parameter
1835
  (cp_parser *, bool *, bool *);
1836
static tree cp_parser_type_parameter
1837
  (cp_parser *, bool *);
1838
static tree cp_parser_template_id
1839
  (cp_parser *, bool, bool, bool);
1840
static tree cp_parser_template_name
1841
  (cp_parser *, bool, bool, bool, bool *);
1842
static tree cp_parser_template_argument_list
1843
  (cp_parser *);
1844
static tree cp_parser_template_argument
1845
  (cp_parser *);
1846
static void cp_parser_explicit_instantiation
1847
  (cp_parser *);
1848
static void cp_parser_explicit_specialization
1849
  (cp_parser *);
1850
 
1851
/* Exception handling [gram.exception] */
1852
 
1853
static tree cp_parser_try_block
1854
  (cp_parser *);
1855
static bool cp_parser_function_try_block
1856
  (cp_parser *);
1857
static void cp_parser_handler_seq
1858
  (cp_parser *);
1859
static void cp_parser_handler
1860
  (cp_parser *);
1861
static tree cp_parser_exception_declaration
1862
  (cp_parser *);
1863
static tree cp_parser_throw_expression
1864
  (cp_parser *);
1865
static tree cp_parser_exception_specification_opt
1866
  (cp_parser *);
1867
static tree cp_parser_type_id_list
1868
  (cp_parser *);
1869
 
1870
/* GNU Extensions */
1871
 
1872
static tree cp_parser_asm_specification_opt
1873
  (cp_parser *);
1874
static tree cp_parser_asm_operand_list
1875
  (cp_parser *);
1876
static tree cp_parser_asm_clobber_list
1877
  (cp_parser *);
1878
static tree cp_parser_asm_label_list
1879
  (cp_parser *);
1880
static tree cp_parser_attributes_opt
1881
  (cp_parser *);
1882
static tree cp_parser_attribute_list
1883
  (cp_parser *);
1884
static bool cp_parser_extension_opt
1885
  (cp_parser *, int *);
1886
static void cp_parser_label_declaration
1887
  (cp_parser *);
1888
 
1889
enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1890
static bool cp_parser_pragma
1891
  (cp_parser *, enum pragma_context);
1892
 
1893
/* Objective-C++ Productions */
1894
 
1895
static tree cp_parser_objc_message_receiver
1896
  (cp_parser *);
1897
static tree cp_parser_objc_message_args
1898
  (cp_parser *);
1899
static tree cp_parser_objc_message_expression
1900
  (cp_parser *);
1901
static tree cp_parser_objc_encode_expression
1902
  (cp_parser *);
1903
static tree cp_parser_objc_defs_expression
1904
  (cp_parser *);
1905
static tree cp_parser_objc_protocol_expression
1906
  (cp_parser *);
1907
static tree cp_parser_objc_selector_expression
1908
  (cp_parser *);
1909
static tree cp_parser_objc_expression
1910
  (cp_parser *);
1911
static bool cp_parser_objc_selector_p
1912
  (enum cpp_ttype);
1913
static tree cp_parser_objc_selector
1914
  (cp_parser *);
1915
static tree cp_parser_objc_protocol_refs_opt
1916
  (cp_parser *);
1917
static void cp_parser_objc_declaration
1918
  (cp_parser *);
1919
static tree cp_parser_objc_statement
1920
  (cp_parser *);
1921
 
1922
/* Utility Routines */
1923
 
1924
static tree cp_parser_lookup_name
1925
  (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1926
static tree cp_parser_lookup_name_simple
1927
  (cp_parser *, tree, location_t);
1928
static tree cp_parser_maybe_treat_template_as_class
1929
  (tree, bool);
1930
static bool cp_parser_check_declarator_template_parameters
1931
  (cp_parser *, cp_declarator *, location_t);
1932
static bool cp_parser_check_template_parameters
1933
  (cp_parser *, unsigned, location_t, cp_declarator *);
1934
static tree cp_parser_simple_cast_expression
1935
  (cp_parser *);
1936
static tree cp_parser_global_scope_opt
1937
  (cp_parser *, bool);
1938
static bool cp_parser_constructor_declarator_p
1939
  (cp_parser *, bool);
1940
static tree cp_parser_function_definition_from_specifiers_and_declarator
1941
  (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1942
static tree cp_parser_function_definition_after_declarator
1943
  (cp_parser *, bool);
1944
static void cp_parser_template_declaration_after_export
1945
  (cp_parser *, bool);
1946
static void cp_parser_perform_template_parameter_access_checks
1947
  (VEC (deferred_access_check,gc)*);
1948
static tree cp_parser_single_declaration
1949
  (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1950
static tree cp_parser_functional_cast
1951
  (cp_parser *, tree);
1952
static tree cp_parser_save_member_function_body
1953
  (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1954
static tree cp_parser_enclosed_template_argument_list
1955
  (cp_parser *);
1956
static void cp_parser_save_default_args
1957
  (cp_parser *, tree);
1958
static void cp_parser_late_parsing_for_member
1959
  (cp_parser *, tree);
1960
static void cp_parser_late_parsing_default_args
1961
  (cp_parser *, tree);
1962
static tree cp_parser_sizeof_operand
1963
  (cp_parser *, enum rid);
1964
static tree cp_parser_trait_expr
1965
  (cp_parser *, enum rid);
1966
static bool cp_parser_declares_only_class_p
1967
  (cp_parser *);
1968
static void cp_parser_set_storage_class
1969
  (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1970
static void cp_parser_set_decl_spec_type
1971
  (cp_decl_specifier_seq *, tree, location_t, bool);
1972
static bool cp_parser_friend_p
1973
  (const cp_decl_specifier_seq *);
1974
static cp_token *cp_parser_require
1975
  (cp_parser *, enum cpp_ttype, const char *);
1976
static cp_token *cp_parser_require_keyword
1977
  (cp_parser *, enum rid, const char *);
1978
static bool cp_parser_token_starts_function_definition_p
1979
  (cp_token *);
1980
static bool cp_parser_next_token_starts_class_definition_p
1981
  (cp_parser *);
1982
static bool cp_parser_next_token_ends_template_argument_p
1983
  (cp_parser *);
1984
static bool cp_parser_nth_token_starts_template_argument_list_p
1985
  (cp_parser *, size_t);
1986
static enum tag_types cp_parser_token_is_class_key
1987
  (cp_token *);
1988
static void cp_parser_check_class_key
1989
  (enum tag_types, tree type);
1990
static void cp_parser_check_access_in_redeclaration
1991
  (tree type, location_t location);
1992
static bool cp_parser_optional_template_keyword
1993
  (cp_parser *);
1994
static void cp_parser_pre_parsed_nested_name_specifier
1995
  (cp_parser *);
1996
static bool cp_parser_cache_group
1997
  (cp_parser *, enum cpp_ttype, unsigned);
1998
static void cp_parser_parse_tentatively
1999
  (cp_parser *);
2000
static void cp_parser_commit_to_tentative_parse
2001
  (cp_parser *);
2002
static void cp_parser_abort_tentative_parse
2003
  (cp_parser *);
2004
static bool cp_parser_parse_definitely
2005
  (cp_parser *);
2006
static inline bool cp_parser_parsing_tentatively
2007
  (cp_parser *);
2008
static bool cp_parser_uncommitted_to_tentative_parse_p
2009
  (cp_parser *);
2010
static void cp_parser_error
2011
  (cp_parser *, const char *);
2012
static void cp_parser_name_lookup_error
2013
  (cp_parser *, tree, tree, const char *, location_t);
2014
static bool cp_parser_simulate_error
2015
  (cp_parser *);
2016
static bool cp_parser_check_type_definition
2017
  (cp_parser *);
2018
static void cp_parser_check_for_definition_in_return_type
2019
  (cp_declarator *, tree, location_t type_location);
2020
static void cp_parser_check_for_invalid_template_id
2021
  (cp_parser *, tree, location_t location);
2022
static bool cp_parser_non_integral_constant_expression
2023
  (cp_parser *, const char *);
2024
static void cp_parser_diagnose_invalid_type_name
2025
  (cp_parser *, tree, tree, location_t);
2026
static bool cp_parser_parse_and_diagnose_invalid_type_name
2027
  (cp_parser *);
2028
static int cp_parser_skip_to_closing_parenthesis
2029
  (cp_parser *, bool, bool, bool);
2030
static void cp_parser_skip_to_end_of_statement
2031
  (cp_parser *);
2032
static void cp_parser_consume_semicolon_at_end_of_statement
2033
  (cp_parser *);
2034
static void cp_parser_skip_to_end_of_block_or_statement
2035
  (cp_parser *);
2036
static bool cp_parser_skip_to_closing_brace
2037
  (cp_parser *);
2038
static void cp_parser_skip_to_end_of_template_parameter_list
2039
  (cp_parser *);
2040
static void cp_parser_skip_to_pragma_eol
2041
  (cp_parser*, cp_token *);
2042
static bool cp_parser_error_occurred
2043
  (cp_parser *);
2044
static bool cp_parser_allow_gnu_extensions_p
2045
  (cp_parser *);
2046
static bool cp_parser_is_string_literal
2047
  (cp_token *);
2048
static bool cp_parser_is_keyword
2049
  (cp_token *, enum rid);
2050
static tree cp_parser_make_typename_type
2051
  (cp_parser *, tree, tree, location_t location);
2052
static cp_declarator * cp_parser_make_indirect_declarator
2053
  (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2054
 
2055
/* Returns nonzero if we are parsing tentatively.  */
2056
 
2057
static inline bool
2058
cp_parser_parsing_tentatively (cp_parser* parser)
2059
{
2060
  return parser->context->next != NULL;
2061
}
2062
 
2063
/* Returns nonzero if TOKEN is a string literal.  */
2064
 
2065
static bool
2066
cp_parser_is_string_literal (cp_token* token)
2067
{
2068
  return (token->type == CPP_STRING ||
2069
          token->type == CPP_STRING16 ||
2070
          token->type == CPP_STRING32 ||
2071
          token->type == CPP_WSTRING ||
2072
          token->type == CPP_UTF8STRING);
2073
}
2074
 
2075
/* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2076
 
2077
static bool
2078
cp_parser_is_keyword (cp_token* token, enum rid keyword)
2079
{
2080
  return token->keyword == keyword;
2081
}
2082
 
2083
/* If not parsing tentatively, issue a diagnostic of the form
2084
      FILE:LINE: MESSAGE before TOKEN
2085
   where TOKEN is the next token in the input stream.  MESSAGE
2086
   (specified by the caller) is usually of the form "expected
2087
   OTHER-TOKEN".  */
2088
 
2089
static void
2090
cp_parser_error (cp_parser* parser, const char* message)
2091
{
2092
  if (!cp_parser_simulate_error (parser))
2093
    {
2094
      cp_token *token = cp_lexer_peek_token (parser->lexer);
2095
      /* This diagnostic makes more sense if it is tagged to the line
2096
         of the token we just peeked at.  */
2097
      cp_lexer_set_source_position_from_token (token);
2098
 
2099
      if (token->type == CPP_PRAGMA)
2100
        {
2101
          error_at (token->location,
2102
                    "%<#pragma%> is not allowed here");
2103
          cp_parser_skip_to_pragma_eol (parser, token);
2104
          return;
2105
        }
2106
 
2107
      c_parse_error (message,
2108
                     /* Because c_parser_error does not understand
2109
                        CPP_KEYWORD, keywords are treated like
2110
                        identifiers.  */
2111
                     (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2112
                     token->u.value, token->flags);
2113
    }
2114
}
2115
 
2116
/* Issue an error about name-lookup failing.  NAME is the
2117
   IDENTIFIER_NODE DECL is the result of
2118
   the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2119
   the thing that we hoped to find.  */
2120
 
2121
static void
2122
cp_parser_name_lookup_error (cp_parser* parser,
2123
                             tree name,
2124
                             tree decl,
2125
                             const char* desired,
2126
                             location_t location)
2127
{
2128
  /* If name lookup completely failed, tell the user that NAME was not
2129
     declared.  */
2130
  if (decl == error_mark_node)
2131
    {
2132
      if (parser->scope && parser->scope != global_namespace)
2133
        error_at (location, "%<%E::%E%> has not been declared",
2134
                  parser->scope, name);
2135
      else if (parser->scope == global_namespace)
2136
        error_at (location, "%<::%E%> has not been declared", name);
2137
      else if (parser->object_scope
2138
               && !CLASS_TYPE_P (parser->object_scope))
2139
        error_at (location, "request for member %qE in non-class type %qT",
2140
                  name, parser->object_scope);
2141
      else if (parser->object_scope)
2142
        error_at (location, "%<%T::%E%> has not been declared",
2143
                  parser->object_scope, name);
2144
      else
2145
        error_at (location, "%qE has not been declared", name);
2146
    }
2147
  else if (parser->scope && parser->scope != global_namespace)
2148
    error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2149
  else if (parser->scope == global_namespace)
2150
    error_at (location, "%<::%E%> %s", name, desired);
2151
  else
2152
    error_at (location, "%qE %s", name, desired);
2153
}
2154
 
2155
/* If we are parsing tentatively, remember that an error has occurred
2156
   during this tentative parse.  Returns true if the error was
2157
   simulated; false if a message should be issued by the caller.  */
2158
 
2159
static bool
2160
cp_parser_simulate_error (cp_parser* parser)
2161
{
2162
  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2163
    {
2164
      parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2165
      return true;
2166
    }
2167
  return false;
2168
}
2169
 
2170
/* Check for repeated decl-specifiers.  */
2171
 
2172
static void
2173
cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2174
                           location_t location)
2175
{
2176
  int ds;
2177
 
2178
  for (ds = ds_first; ds != ds_last; ++ds)
2179
    {
2180
      unsigned count = decl_specs->specs[ds];
2181
      if (count < 2)
2182
        continue;
2183
      /* The "long" specifier is a special case because of "long long".  */
2184
      if (ds == ds_long)
2185
        {
2186
          if (count > 2)
2187
            error_at (location, "%<long long long%> is too long for GCC");
2188
          else
2189
            pedwarn_cxx98 (location, OPT_Wlong_long,
2190
                           "ISO C++ 1998 does not support %<long long%>");
2191
        }
2192
      else if (count > 1)
2193
        {
2194
          static const char *const decl_spec_names[] = {
2195
            "signed",
2196
            "unsigned",
2197
            "short",
2198
            "long",
2199
            "const",
2200
            "volatile",
2201
            "restrict",
2202
            "inline",
2203
            "virtual",
2204
            "explicit",
2205
            "friend",
2206
            "typedef",
2207
            "constexpr",
2208
            "__complex",
2209
            "__thread"
2210
          };
2211
          error_at (location, "duplicate %qs", decl_spec_names[ds]);
2212
        }
2213
    }
2214
}
2215
 
2216
/* This function is called when a type is defined.  If type
2217
   definitions are forbidden at this point, an error message is
2218
   issued.  */
2219
 
2220
static bool
2221
cp_parser_check_type_definition (cp_parser* parser)
2222
{
2223
  /* If types are forbidden here, issue a message.  */
2224
  if (parser->type_definition_forbidden_message)
2225
    {
2226
      /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2227
         in the message need to be interpreted.  */
2228
      error (parser->type_definition_forbidden_message);
2229
      return false;
2230
    }
2231
  return true;
2232
}
2233
 
2234
/* This function is called when the DECLARATOR is processed.  The TYPE
2235
   was a type defined in the decl-specifiers.  If it is invalid to
2236
   define a type in the decl-specifiers for DECLARATOR, an error is
2237
   issued. TYPE_LOCATION is the location of TYPE and is used
2238
   for error reporting.  */
2239
 
2240
static void
2241
cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2242
                                               tree type, location_t type_location)
2243
{
2244
  /* [dcl.fct] forbids type definitions in return types.
2245
     Unfortunately, it's not easy to know whether or not we are
2246
     processing a return type until after the fact.  */
2247
  while (declarator
2248
         && (declarator->kind == cdk_pointer
2249
             || declarator->kind == cdk_reference
2250
             || declarator->kind == cdk_ptrmem))
2251
    declarator = declarator->declarator;
2252
  if (declarator
2253
      && declarator->kind == cdk_function)
2254
    {
2255
      error_at (type_location,
2256
                "new types may not be defined in a return type");
2257
      inform (type_location,
2258
              "(perhaps a semicolon is missing after the definition of %qT)",
2259
              type);
2260
    }
2261
}
2262
 
2263
/* A type-specifier (TYPE) has been parsed which cannot be followed by
2264
   "<" in any valid C++ program.  If the next token is indeed "<",
2265
   issue a message warning the user about what appears to be an
2266
   invalid attempt to form a template-id. LOCATION is the location
2267
   of the type-specifier (TYPE) */
2268
 
2269
static void
2270
cp_parser_check_for_invalid_template_id (cp_parser* parser,
2271
                                         tree type, location_t location)
2272
{
2273
  cp_token_position start = 0;
2274
 
2275
  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2276
    {
2277
      if (TYPE_P (type))
2278
        error_at (location, "%qT is not a template", type);
2279
      else if (TREE_CODE (type) == IDENTIFIER_NODE)
2280
        error_at (location, "%qE is not a template", type);
2281
      else
2282
        error_at (location, "invalid template-id");
2283
      /* Remember the location of the invalid "<".  */
2284
      if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2285
        start = cp_lexer_token_position (parser->lexer, true);
2286
      /* Consume the "<".  */
2287
      cp_lexer_consume_token (parser->lexer);
2288
      /* Parse the template arguments.  */
2289
      cp_parser_enclosed_template_argument_list (parser);
2290
      /* Permanently remove the invalid template arguments so that
2291
         this error message is not issued again.  */
2292
      if (start)
2293
        cp_lexer_purge_tokens_after (parser->lexer, start);
2294
    }
2295
}
2296
 
2297
/* If parsing an integral constant-expression, issue an error message
2298
   about the fact that THING appeared and return true.  Otherwise,
2299
   return false.  In either case, set
2300
   PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2301
 
2302
static bool
2303
cp_parser_non_integral_constant_expression (cp_parser  *parser,
2304
                                            const char *thing)
2305
{
2306
  parser->non_integral_constant_expression_p = true;
2307
  if (parser->integral_constant_expression_p)
2308
    {
2309
      if (!parser->allow_non_integral_constant_expression_p)
2310
        {
2311
          /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2312
             in the message need to be interpreted.  */
2313
          char *message = concat (thing,
2314
                                  " cannot appear in a constant-expression",
2315
                                  NULL);
2316
          error (message);
2317
          free (message);
2318
          return true;
2319
        }
2320
    }
2321
  return false;
2322
}
2323
 
2324
/* Emit a diagnostic for an invalid type name.  SCOPE is the
2325
   qualifying scope (or NULL, if none) for ID.  This function commits
2326
   to the current active tentative parse, if any.  (Otherwise, the
2327
   problematic construct might be encountered again later, resulting
2328
   in duplicate error messages.) LOCATION is the location of ID.  */
2329
 
2330
static void
2331
cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2332
                                      tree scope, tree id,
2333
                                      location_t location)
2334
{
2335
  tree decl, old_scope;
2336
  /* Try to lookup the identifier.  */
2337
  old_scope = parser->scope;
2338
  parser->scope = scope;
2339
  decl = cp_parser_lookup_name_simple (parser, id, location);
2340
  parser->scope = old_scope;
2341
  /* If the lookup found a template-name, it means that the user forgot
2342
  to specify an argument list. Emit a useful error message.  */
2343
  if (TREE_CODE (decl) == TEMPLATE_DECL)
2344
    error_at (location,
2345
              "invalid use of template-name %qE without an argument list",
2346
              decl);
2347
  else if (TREE_CODE (id) == BIT_NOT_EXPR)
2348
    error_at (location, "invalid use of destructor %qD as a type", id);
2349
  else if (TREE_CODE (decl) == TYPE_DECL)
2350
    /* Something like 'unsigned A a;'  */
2351
    error_at (location, "invalid combination of multiple type-specifiers");
2352
  else if (!parser->scope)
2353
    {
2354
      /* Issue an error message.  */
2355
      error_at (location, "%qE does not name a type", id);
2356
      /* If we're in a template class, it's possible that the user was
2357
         referring to a type from a base class.  For example:
2358
 
2359
           template <typename T> struct A { typedef T X; };
2360
           template <typename T> struct B : public A<T> { X x; };
2361
 
2362
         The user should have said "typename A<T>::X".  */
2363
      if (processing_template_decl && current_class_type
2364
          && TYPE_BINFO (current_class_type))
2365
        {
2366
          tree b;
2367
 
2368
          for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2369
               b;
2370
               b = TREE_CHAIN (b))
2371
            {
2372
              tree base_type = BINFO_TYPE (b);
2373
              if (CLASS_TYPE_P (base_type)
2374
                  && dependent_type_p (base_type))
2375
                {
2376
                  tree field;
2377
                  /* Go from a particular instantiation of the
2378
                     template (which will have an empty TYPE_FIELDs),
2379
                     to the main version.  */
2380
                  base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2381
                  for (field = TYPE_FIELDS (base_type);
2382
                       field;
2383
                       field = TREE_CHAIN (field))
2384
                    if (TREE_CODE (field) == TYPE_DECL
2385
                        && DECL_NAME (field) == id)
2386
                      {
2387
                        inform (location,
2388
                                "(perhaps %<typename %T::%E%> was intended)",
2389
                                BINFO_TYPE (b), id);
2390
                        break;
2391
                      }
2392
                  if (field)
2393
                    break;
2394
                }
2395
            }
2396
        }
2397
    }
2398
  /* Here we diagnose qualified-ids where the scope is actually correct,
2399
     but the identifier does not resolve to a valid type name.  */
2400
  else if (parser->scope != error_mark_node)
2401
    {
2402
      if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2403
        error_at (location, "%qE in namespace %qE does not name a type",
2404
                  id, parser->scope);
2405
      else if (CLASS_TYPE_P (parser->scope)
2406
               && constructor_name_p (id, parser->scope))
2407
        {
2408
          /* A<T>::A<T>() */
2409
          error_at (location, "%<%T::%E%> names the constructor, not"
2410
                    " the type", parser->scope, id);
2411
          if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2412
            error_at (location, "and %qT has no template constructors",
2413
                      parser->scope);
2414
        }
2415
      else if (TYPE_P (parser->scope)
2416
               && dependent_scope_p (parser->scope))
2417
        error_at (location, "need %<typename%> before %<%T::%E%> because "
2418
                  "%qT is a dependent scope",
2419
                  parser->scope, id, parser->scope);
2420
      else if (TYPE_P (parser->scope))
2421
        error_at (location, "%qE in class %qT does not name a type",
2422
                  id, parser->scope);
2423
      else
2424
        gcc_unreachable ();
2425
    }
2426
  cp_parser_commit_to_tentative_parse (parser);
2427
}
2428
 
2429
/* Check for a common situation where a type-name should be present,
2430
   but is not, and issue a sensible error message.  Returns true if an
2431
   invalid type-name was detected.
2432
 
2433
   The situation handled by this function are variable declarations of the
2434
   form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2435
   Usually, `ID' should name a type, but if we got here it means that it
2436
   does not. We try to emit the best possible error message depending on
2437
   how exactly the id-expression looks like.  */
2438
 
2439
static bool
2440
cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2441
{
2442
  tree id;
2443
  cp_token *token = cp_lexer_peek_token (parser->lexer);
2444
 
2445
  /* Avoid duplicate error about ambiguous lookup.  */
2446
  if (token->type == CPP_NESTED_NAME_SPECIFIER)
2447
    {
2448
      cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2449
      if (next->type == CPP_NAME && next->ambiguous_p)
2450
        goto out;
2451
    }
2452
 
2453
  cp_parser_parse_tentatively (parser);
2454
  id = cp_parser_id_expression (parser,
2455
                                /*template_keyword_p=*/false,
2456
                                /*check_dependency_p=*/true,
2457
                                /*template_p=*/NULL,
2458
                                /*declarator_p=*/true,
2459
                                /*optional_p=*/false);
2460
  /* If the next token is a (, this is a function with no explicit return
2461
     type, i.e. constructor, destructor or conversion op.  */
2462
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2463
      || TREE_CODE (id) == TYPE_DECL)
2464
    {
2465
      cp_parser_abort_tentative_parse (parser);
2466
      return false;
2467
    }
2468
  if (!cp_parser_parse_definitely (parser))
2469
    return false;
2470
 
2471
  /* Emit a diagnostic for the invalid type.  */
2472
  cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2473
                                        id, token->location);
2474
 out:
2475
  /* If we aren't in the middle of a declarator (i.e. in a
2476
     parameter-declaration-clause), skip to the end of the declaration;
2477
     there's no point in trying to process it.  */
2478
  if (!parser->in_declarator_p)
2479
    cp_parser_skip_to_end_of_block_or_statement (parser);
2480
  return true;
2481
}
2482
 
2483
/* Consume tokens up to, and including, the next non-nested closing `)'.
2484
   Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2485
   are doing error recovery. Returns -1 if OR_COMMA is true and we
2486
   found an unnested comma.  */
2487
 
2488
static int
2489
cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2490
                                       bool recovering,
2491
                                       bool or_comma,
2492
                                       bool consume_paren)
2493
{
2494
  unsigned paren_depth = 0;
2495
  unsigned brace_depth = 0;
2496
  unsigned square_depth = 0;
2497
 
2498
  if (recovering && !or_comma
2499
      && cp_parser_uncommitted_to_tentative_parse_p (parser))
2500
    return 0;
2501
 
2502
  while (true)
2503
    {
2504
      cp_token * token = cp_lexer_peek_token (parser->lexer);
2505
 
2506
      switch (token->type)
2507
        {
2508
        case CPP_EOF:
2509
        case CPP_PRAGMA_EOL:
2510
          /* If we've run out of tokens, then there is no closing `)'.  */
2511
          return 0;
2512
 
2513
        /* This is good for lambda expression capture-lists.  */
2514
        case CPP_OPEN_SQUARE:
2515
          ++square_depth;
2516
          break;
2517
        case CPP_CLOSE_SQUARE:
2518
          if (!square_depth--)
2519
            return 0;
2520
          break;
2521
 
2522
        case CPP_SEMICOLON:
2523
          /* This matches the processing in skip_to_end_of_statement.  */
2524
          if (!brace_depth)
2525
            return 0;
2526
          break;
2527
 
2528
        case CPP_OPEN_BRACE:
2529
          ++brace_depth;
2530
          break;
2531
        case CPP_CLOSE_BRACE:
2532
          if (!brace_depth--)
2533
            return 0;
2534
          break;
2535
 
2536
        case CPP_COMMA:
2537
          if (recovering && or_comma && !brace_depth && !paren_depth
2538
              && !square_depth)
2539
            return -1;
2540
          break;
2541
 
2542
        case CPP_OPEN_PAREN:
2543
          if (!brace_depth)
2544
            ++paren_depth;
2545
          break;
2546
 
2547
        case CPP_CLOSE_PAREN:
2548
          if (!brace_depth && !paren_depth--)
2549
            {
2550
              if (consume_paren)
2551
                cp_lexer_consume_token (parser->lexer);
2552
              return 1;
2553
            }
2554
          break;
2555
 
2556
        default:
2557
          break;
2558
        }
2559
 
2560
      /* Consume the token.  */
2561
      cp_lexer_consume_token (parser->lexer);
2562
    }
2563
}
2564
 
2565
/* Consume tokens until we reach the end of the current statement.
2566
   Normally, that will be just before consuming a `;'.  However, if a
2567
   non-nested `}' comes first, then we stop before consuming that.  */
2568
 
2569
static void
2570
cp_parser_skip_to_end_of_statement (cp_parser* parser)
2571
{
2572
  unsigned nesting_depth = 0;
2573
 
2574
  while (true)
2575
    {
2576
      cp_token *token = cp_lexer_peek_token (parser->lexer);
2577
 
2578
      switch (token->type)
2579
        {
2580
        case CPP_EOF:
2581
        case CPP_PRAGMA_EOL:
2582
          /* If we've run out of tokens, stop.  */
2583
          return;
2584
 
2585
        case CPP_SEMICOLON:
2586
          /* If the next token is a `;', we have reached the end of the
2587
             statement.  */
2588
          if (!nesting_depth)
2589
            return;
2590
          break;
2591
 
2592
        case CPP_CLOSE_BRACE:
2593
          /* If this is a non-nested '}', stop before consuming it.
2594
             That way, when confronted with something like:
2595
 
2596
               { 3 + }
2597
 
2598
             we stop before consuming the closing '}', even though we
2599
             have not yet reached a `;'.  */
2600
          if (nesting_depth == 0)
2601
            return;
2602
 
2603
          /* If it is the closing '}' for a block that we have
2604
             scanned, stop -- but only after consuming the token.
2605
             That way given:
2606
 
2607
                void f g () { ... }
2608
                typedef int I;
2609
 
2610
             we will stop after the body of the erroneously declared
2611
             function, but before consuming the following `typedef'
2612
             declaration.  */
2613
          if (--nesting_depth == 0)
2614
            {
2615
              cp_lexer_consume_token (parser->lexer);
2616
              return;
2617
            }
2618
 
2619
        case CPP_OPEN_BRACE:
2620
          ++nesting_depth;
2621
          break;
2622
 
2623
        default:
2624
          break;
2625
        }
2626
 
2627
      /* Consume the token.  */
2628
      cp_lexer_consume_token (parser->lexer);
2629
    }
2630
}
2631
 
2632
/* This function is called at the end of a statement or declaration.
2633
   If the next token is a semicolon, it is consumed; otherwise, error
2634
   recovery is attempted.  */
2635
 
2636
static void
2637
cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2638
{
2639
  /* Look for the trailing `;'.  */
2640
  if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2641
    {
2642
      /* If there is additional (erroneous) input, skip to the end of
2643
         the statement.  */
2644
      cp_parser_skip_to_end_of_statement (parser);
2645
      /* If the next token is now a `;', consume it.  */
2646
      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2647
        cp_lexer_consume_token (parser->lexer);
2648
    }
2649
}
2650
 
2651
/* Skip tokens until we have consumed an entire block, or until we
2652
   have consumed a non-nested `;'.  */
2653
 
2654
static void
2655
cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2656
{
2657
  int nesting_depth = 0;
2658
 
2659
  while (nesting_depth >= 0)
2660
    {
2661
      cp_token *token = cp_lexer_peek_token (parser->lexer);
2662
 
2663
      switch (token->type)
2664
        {
2665
        case CPP_EOF:
2666
        case CPP_PRAGMA_EOL:
2667
          /* If we've run out of tokens, stop.  */
2668
          return;
2669
 
2670
        case CPP_SEMICOLON:
2671
          /* Stop if this is an unnested ';'. */
2672
          if (!nesting_depth)
2673
            nesting_depth = -1;
2674
          break;
2675
 
2676
        case CPP_CLOSE_BRACE:
2677
          /* Stop if this is an unnested '}', or closes the outermost
2678
             nesting level.  */
2679
          nesting_depth--;
2680
          if (nesting_depth < 0)
2681
            return;
2682
          if (!nesting_depth)
2683
            nesting_depth = -1;
2684
          break;
2685
 
2686
        case CPP_OPEN_BRACE:
2687
          /* Nest. */
2688
          nesting_depth++;
2689
          break;
2690
 
2691
        default:
2692
          break;
2693
        }
2694
 
2695
      /* Consume the token.  */
2696
      cp_lexer_consume_token (parser->lexer);
2697
    }
2698
}
2699
 
2700
/* Skip tokens until a non-nested closing curly brace is the next
2701
   token, or there are no more tokens. Return true in the first case,
2702
   false otherwise.  */
2703
 
2704
static bool
2705
cp_parser_skip_to_closing_brace (cp_parser *parser)
2706
{
2707
  unsigned nesting_depth = 0;
2708
 
2709
  while (true)
2710
    {
2711
      cp_token *token = cp_lexer_peek_token (parser->lexer);
2712
 
2713
      switch (token->type)
2714
        {
2715
        case CPP_EOF:
2716
        case CPP_PRAGMA_EOL:
2717
          /* If we've run out of tokens, stop.  */
2718
          return false;
2719
 
2720
        case CPP_CLOSE_BRACE:
2721
          /* If the next token is a non-nested `}', then we have reached
2722
             the end of the current block.  */
2723
          if (nesting_depth-- == 0)
2724
            return true;
2725
          break;
2726
 
2727
        case CPP_OPEN_BRACE:
2728
          /* If it the next token is a `{', then we are entering a new
2729
             block.  Consume the entire block.  */
2730
          ++nesting_depth;
2731
          break;
2732
 
2733
        default:
2734
          break;
2735
        }
2736
 
2737
      /* Consume the token.  */
2738
      cp_lexer_consume_token (parser->lexer);
2739
    }
2740
}
2741
 
2742
/* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2743
   parameter is the PRAGMA token, allowing us to purge the entire pragma
2744
   sequence.  */
2745
 
2746
static void
2747
cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2748
{
2749
  cp_token *token;
2750
 
2751
  parser->lexer->in_pragma = false;
2752
 
2753
  do
2754
    token = cp_lexer_consume_token (parser->lexer);
2755
  while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2756
 
2757
  /* Ensure that the pragma is not parsed again.  */
2758
  cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2759
}
2760
 
2761
/* Require pragma end of line, resyncing with it as necessary.  The
2762
   arguments are as for cp_parser_skip_to_pragma_eol.  */
2763
 
2764
static void
2765
cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2766
{
2767
  parser->lexer->in_pragma = false;
2768
  if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2769
    cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2770
}
2771
 
2772
/* This is a simple wrapper around make_typename_type. When the id is
2773
   an unresolved identifier node, we can provide a superior diagnostic
2774
   using cp_parser_diagnose_invalid_type_name.  */
2775
 
2776
static tree
2777
cp_parser_make_typename_type (cp_parser *parser, tree scope,
2778
                              tree id, location_t id_location)
2779
{
2780
  tree result;
2781
  if (TREE_CODE (id) == IDENTIFIER_NODE)
2782
    {
2783
      result = make_typename_type (scope, id, typename_type,
2784
                                   /*complain=*/tf_none);
2785
      if (result == error_mark_node)
2786
        cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2787
      return result;
2788
    }
2789
  return make_typename_type (scope, id, typename_type, tf_error);
2790
}
2791
 
2792
/* This is a wrapper around the
2793
   make_{pointer,ptrmem,reference}_declarator functions that decides
2794
   which one to call based on the CODE and CLASS_TYPE arguments. The
2795
   CODE argument should be one of the values returned by
2796
   cp_parser_ptr_operator. */
2797
static cp_declarator *
2798
cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2799
                                    cp_cv_quals cv_qualifiers,
2800
                                    cp_declarator *target)
2801
{
2802
  if (code == ERROR_MARK)
2803
    return cp_error_declarator;
2804
 
2805
  if (code == INDIRECT_REF)
2806
    if (class_type == NULL_TREE)
2807
      return make_pointer_declarator (cv_qualifiers, target);
2808
    else
2809
      return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2810
  else if (code == ADDR_EXPR && class_type == NULL_TREE)
2811
    return make_reference_declarator (cv_qualifiers, target, false);
2812
  else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2813
    return make_reference_declarator (cv_qualifiers, target, true);
2814
  gcc_unreachable ();
2815
}
2816
 
2817
/* Create a new C++ parser.  */
2818
 
2819
static cp_parser *
2820
cp_parser_new (void)
2821
{
2822
  cp_parser *parser;
2823
  cp_lexer *lexer;
2824
  unsigned i;
2825
 
2826
  /* cp_lexer_new_main is called before calling ggc_alloc because
2827
     cp_lexer_new_main might load a PCH file.  */
2828
  lexer = cp_lexer_new_main ();
2829
 
2830
  /* Initialize the binops_by_token so that we can get the tree
2831
     directly from the token.  */
2832
  for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2833
    binops_by_token[binops[i].token_type] = binops[i];
2834
 
2835
  parser = GGC_CNEW (cp_parser);
2836
  parser->lexer = lexer;
2837
  parser->context = cp_parser_context_new (NULL);
2838
 
2839
  /* For now, we always accept GNU extensions.  */
2840
  parser->allow_gnu_extensions_p = 1;
2841
 
2842
  /* The `>' token is a greater-than operator, not the end of a
2843
     template-id.  */
2844
  parser->greater_than_is_operator_p = true;
2845
 
2846
  parser->default_arg_ok_p = true;
2847
 
2848
  /* We are not parsing a constant-expression.  */
2849
  parser->integral_constant_expression_p = false;
2850
  parser->allow_non_integral_constant_expression_p = false;
2851
  parser->non_integral_constant_expression_p = false;
2852
 
2853
  /* Local variable names are not forbidden.  */
2854
  parser->local_variables_forbidden_p = false;
2855
 
2856
  /* We are not processing an `extern "C"' declaration.  */
2857
  parser->in_unbraced_linkage_specification_p = false;
2858
 
2859
  /* We are not processing a declarator.  */
2860
  parser->in_declarator_p = false;
2861
 
2862
  /* We are not processing a template-argument-list.  */
2863
  parser->in_template_argument_list_p = false;
2864
 
2865
  /* We are not in an iteration statement.  */
2866
  parser->in_statement = 0;
2867
 
2868
  /* We are not in a switch statement.  */
2869
  parser->in_switch_statement_p = false;
2870
 
2871
  /* We are not parsing a type-id inside an expression.  */
2872
  parser->in_type_id_in_expr_p = false;
2873
 
2874
  /* Declarations aren't implicitly extern "C".  */
2875
  parser->implicit_extern_c = false;
2876
 
2877
  /* String literals should be translated to the execution character set.  */
2878
  parser->translate_strings_p = true;
2879
 
2880
  /* We are not parsing a function body.  */
2881
  parser->in_function_body = false;
2882
 
2883
  /* The unparsed function queue is empty.  */
2884
  parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2885
 
2886
  /* There are no classes being defined.  */
2887
  parser->num_classes_being_defined = 0;
2888
 
2889
  /* No template parameters apply.  */
2890
  parser->num_template_parameter_lists = 0;
2891
 
2892
  return parser;
2893
}
2894
 
2895
/* Create a cp_lexer structure which will emit the tokens in CACHE
2896
   and push it onto the parser's lexer stack.  This is used for delayed
2897
   parsing of in-class method bodies and default arguments, and should
2898
   not be confused with tentative parsing.  */
2899
static void
2900
cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2901
{
2902
  cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2903
  lexer->next = parser->lexer;
2904
  parser->lexer = lexer;
2905
 
2906
  /* Move the current source position to that of the first token in the
2907
     new lexer.  */
2908
  cp_lexer_set_source_position_from_token (lexer->next_token);
2909
}
2910
 
2911
/* Pop the top lexer off the parser stack.  This is never used for the
2912
   "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2913
static void
2914
cp_parser_pop_lexer (cp_parser *parser)
2915
{
2916
  cp_lexer *lexer = parser->lexer;
2917
  parser->lexer = lexer->next;
2918
  cp_lexer_destroy (lexer);
2919
 
2920
  /* Put the current source position back where it was before this
2921
     lexer was pushed.  */
2922
  cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2923
}
2924
 
2925
/* Lexical conventions [gram.lex]  */
2926
 
2927
/* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2928
   identifier.  */
2929
 
2930
static tree
2931
cp_parser_identifier (cp_parser* parser)
2932
{
2933
  cp_token *token;
2934
 
2935
  /* Look for the identifier.  */
2936
  token = cp_parser_require (parser, CPP_NAME, "identifier");
2937
  /* Return the value.  */
2938
  return token ? token->u.value : error_mark_node;
2939
}
2940
 
2941
/* Parse a sequence of adjacent string constants.  Returns a
2942
   TREE_STRING representing the combined, nul-terminated string
2943
   constant.  If TRANSLATE is true, translate the string to the
2944
   execution character set.  If WIDE_OK is true, a wide string is
2945
   invalid here.
2946
 
2947
   C++98 [lex.string] says that if a narrow string literal token is
2948
   adjacent to a wide string literal token, the behavior is undefined.
2949
   However, C99 6.4.5p4 says that this results in a wide string literal.
2950
   We follow C99 here, for consistency with the C front end.
2951
 
2952
   This code is largely lifted from lex_string() in c-lex.c.
2953
 
2954
   FUTURE: ObjC++ will need to handle @-strings here.  */
2955
static tree
2956
cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2957
{
2958
  tree value;
2959
  size_t count;
2960
  struct obstack str_ob;
2961
  cpp_string str, istr, *strs;
2962
  cp_token *tok;
2963
  enum cpp_ttype type;
2964
 
2965
  tok = cp_lexer_peek_token (parser->lexer);
2966
  if (!cp_parser_is_string_literal (tok))
2967
    {
2968
      cp_parser_error (parser, "expected string-literal");
2969
      return error_mark_node;
2970
    }
2971
 
2972
  type = tok->type;
2973
 
2974
  /* Try to avoid the overhead of creating and destroying an obstack
2975
     for the common case of just one string.  */
2976
  if (!cp_parser_is_string_literal
2977
      (cp_lexer_peek_nth_token (parser->lexer, 2)))
2978
    {
2979
      cp_lexer_consume_token (parser->lexer);
2980
 
2981
      str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2982
      str.len = TREE_STRING_LENGTH (tok->u.value);
2983
      count = 1;
2984
 
2985
      strs = &str;
2986
    }
2987
  else
2988
    {
2989
      gcc_obstack_init (&str_ob);
2990
      count = 0;
2991
 
2992
      do
2993
        {
2994
          cp_lexer_consume_token (parser->lexer);
2995
          count++;
2996
          str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2997
          str.len = TREE_STRING_LENGTH (tok->u.value);
2998
 
2999
          if (type != tok->type)
3000
            {
3001
              if (type == CPP_STRING)
3002
                type = tok->type;
3003
              else if (tok->type != CPP_STRING)
3004
                error_at (tok->location,
3005
                          "unsupported non-standard concatenation "
3006
                          "of string literals");
3007
            }
3008
 
3009
          obstack_grow (&str_ob, &str, sizeof (cpp_string));
3010
 
3011
          tok = cp_lexer_peek_token (parser->lexer);
3012
        }
3013
      while (cp_parser_is_string_literal (tok));
3014
 
3015
      strs = (cpp_string *) obstack_finish (&str_ob);
3016
    }
3017
 
3018
  if (type != CPP_STRING && !wide_ok)
3019
    {
3020
      cp_parser_error (parser, "a wide string is invalid in this context");
3021
      type = CPP_STRING;
3022
    }
3023
 
3024
  if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3025
      (parse_in, strs, count, &istr, type))
3026
    {
3027
      value = build_string (istr.len, (const char *)istr.text);
3028
      free (CONST_CAST (unsigned char *, istr.text));
3029
 
3030
      switch (type)
3031
        {
3032
        default:
3033
        case CPP_STRING:
3034
        case CPP_UTF8STRING:
3035
          TREE_TYPE (value) = char_array_type_node;
3036
          break;
3037
        case CPP_STRING16:
3038
          TREE_TYPE (value) = char16_array_type_node;
3039
          break;
3040
        case CPP_STRING32:
3041
          TREE_TYPE (value) = char32_array_type_node;
3042
          break;
3043
        case CPP_WSTRING:
3044
          TREE_TYPE (value) = wchar_array_type_node;
3045
          break;
3046
        }
3047
 
3048
      value = fix_string_type (value);
3049
    }
3050
  else
3051
    /* cpp_interpret_string has issued an error.  */
3052
    value = error_mark_node;
3053
 
3054
  if (count > 1)
3055
    obstack_free (&str_ob, 0);
3056
 
3057
  return value;
3058
}
3059
 
3060
 
3061
/* Basic concepts [gram.basic]  */
3062
 
3063
/* Parse a translation-unit.
3064
 
3065
   translation-unit:
3066
     declaration-seq [opt]
3067
 
3068
   Returns TRUE if all went well.  */
3069
 
3070
static bool
3071
cp_parser_translation_unit (cp_parser* parser)
3072
{
3073
  /* The address of the first non-permanent object on the declarator
3074
     obstack.  */
3075
  static void *declarator_obstack_base;
3076
 
3077
  bool success;
3078
 
3079
  /* Create the declarator obstack, if necessary.  */
3080
  if (!cp_error_declarator)
3081
    {
3082
      gcc_obstack_init (&declarator_obstack);
3083
      /* Create the error declarator.  */
3084
      cp_error_declarator = make_declarator (cdk_error);
3085
      /* Create the empty parameter list.  */
3086
      no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3087
      /* Remember where the base of the declarator obstack lies.  */
3088
      declarator_obstack_base = obstack_next_free (&declarator_obstack);
3089
    }
3090
 
3091
  cp_parser_declaration_seq_opt (parser);
3092
 
3093
  /* If there are no tokens left then all went well.  */
3094
  if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3095
    {
3096
      /* Get rid of the token array; we don't need it any more.  */
3097
      cp_lexer_destroy (parser->lexer);
3098
      parser->lexer = NULL;
3099
 
3100
      /* This file might have been a context that's implicitly extern
3101
         "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3102
      if (parser->implicit_extern_c)
3103
        {
3104
          pop_lang_context ();
3105
          parser->implicit_extern_c = false;
3106
        }
3107
 
3108
      /* Finish up.  */
3109
      finish_translation_unit ();
3110
 
3111
      success = true;
3112
    }
3113
  else
3114
    {
3115
      cp_parser_error (parser, "expected declaration");
3116
      success = false;
3117
    }
3118
 
3119
  /* Make sure the declarator obstack was fully cleaned up.  */
3120
  gcc_assert (obstack_next_free (&declarator_obstack)
3121
              == declarator_obstack_base);
3122
 
3123
  /* All went well.  */
3124
  return success;
3125
}
3126
 
3127
/* Expressions [gram.expr] */
3128
 
3129
/* Parse a primary-expression.
3130
 
3131
   primary-expression:
3132
     literal
3133
     this
3134
     ( expression )
3135
     id-expression
3136
 
3137
   GNU Extensions:
3138
 
3139
   primary-expression:
3140
     ( compound-statement )
3141
     __builtin_va_arg ( assignment-expression , type-id )
3142
     __builtin_offsetof ( type-id , offsetof-expression )
3143
 
3144
   C++ Extensions:
3145
     __has_nothrow_assign ( type-id )
3146
     __has_nothrow_constructor ( type-id )
3147
     __has_nothrow_copy ( type-id )
3148
     __has_trivial_assign ( type-id )
3149
     __has_trivial_constructor ( type-id )
3150
     __has_trivial_copy ( type-id )
3151
     __has_trivial_destructor ( type-id )
3152
     __has_virtual_destructor ( type-id )
3153
     __is_abstract ( type-id )
3154
     __is_base_of ( type-id , type-id )
3155
     __is_class ( type-id )
3156
     __is_convertible_to ( type-id , type-id )
3157
     __is_empty ( type-id )
3158
     __is_enum ( type-id )
3159
     __is_pod ( type-id )
3160
     __is_polymorphic ( type-id )
3161
     __is_union ( type-id )
3162
 
3163
   Objective-C++ Extension:
3164
 
3165
   primary-expression:
3166
     objc-expression
3167
 
3168
   literal:
3169
     __null
3170
 
3171
   ADDRESS_P is true iff this expression was immediately preceded by
3172
   "&" and therefore might denote a pointer-to-member.  CAST_P is true
3173
   iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3174
   true iff this expression is a template argument.
3175
 
3176
   Returns a representation of the expression.  Upon return, *IDK
3177
   indicates what kind of id-expression (if any) was present.  */
3178
 
3179
static tree
3180
cp_parser_primary_expression (cp_parser *parser,
3181
                              bool address_p,
3182
                              bool cast_p,
3183
                              bool template_arg_p,
3184
                              cp_id_kind *idk)
3185
{
3186
  cp_token *token = NULL;
3187
 
3188
  /* Assume the primary expression is not an id-expression.  */
3189
  *idk = CP_ID_KIND_NONE;
3190
 
3191
  /* Peek at the next token.  */
3192
  token = cp_lexer_peek_token (parser->lexer);
3193
  switch (token->type)
3194
    {
3195
      /* literal:
3196
           integer-literal
3197
           character-literal
3198
           floating-literal
3199
           string-literal
3200
           boolean-literal  */
3201
    case CPP_CHAR:
3202
    case CPP_CHAR16:
3203
    case CPP_CHAR32:
3204
    case CPP_WCHAR:
3205
    case CPP_NUMBER:
3206
      token = cp_lexer_consume_token (parser->lexer);
3207
      if (TREE_CODE (token->u.value) == FIXED_CST)
3208
        {
3209
          error_at (token->location,
3210
                    "fixed-point types not supported in C++");
3211
          return error_mark_node;
3212
        }
3213
      /* Floating-point literals are only allowed in an integral
3214
         constant expression if they are cast to an integral or
3215
         enumeration type.  */
3216
      if (TREE_CODE (token->u.value) == REAL_CST
3217
          && parser->integral_constant_expression_p
3218
          && pedantic)
3219
        {
3220
          /* CAST_P will be set even in invalid code like "int(2.7 +
3221
             ...)".   Therefore, we have to check that the next token
3222
             is sure to end the cast.  */
3223
          if (cast_p)
3224
            {
3225
              cp_token *next_token;
3226
 
3227
              next_token = cp_lexer_peek_token (parser->lexer);
3228
              if (/* The comma at the end of an
3229
                     enumerator-definition.  */
3230
                  next_token->type != CPP_COMMA
3231
                  /* The curly brace at the end of an enum-specifier.  */
3232
                  && next_token->type != CPP_CLOSE_BRACE
3233
                  /* The end of a statement.  */
3234
                  && next_token->type != CPP_SEMICOLON
3235
                  /* The end of the cast-expression.  */
3236
                  && next_token->type != CPP_CLOSE_PAREN
3237
                  /* The end of an array bound.  */
3238
                  && next_token->type != CPP_CLOSE_SQUARE
3239
                  /* The closing ">" in a template-argument-list.  */
3240
                  && (next_token->type != CPP_GREATER
3241
                      || parser->greater_than_is_operator_p)
3242
                  /* C++0x only: A ">>" treated like two ">" tokens,
3243
                     in a template-argument-list.  */
3244
                  && (next_token->type != CPP_RSHIFT
3245
                      || (cxx_dialect == cxx98)
3246
                      || parser->greater_than_is_operator_p))
3247
                cast_p = false;
3248
            }
3249
 
3250
          /* If we are within a cast, then the constraint that the
3251
             cast is to an integral or enumeration type will be
3252
             checked at that point.  If we are not within a cast, then
3253
             this code is invalid.  */
3254
          if (!cast_p)
3255
            cp_parser_non_integral_constant_expression
3256
              (parser, "floating-point literal");
3257
        }
3258
      return token->u.value;
3259
 
3260
    case CPP_STRING:
3261
    case CPP_STRING16:
3262
    case CPP_STRING32:
3263
    case CPP_WSTRING:
3264
    case CPP_UTF8STRING:
3265
      /* ??? Should wide strings be allowed when parser->translate_strings_p
3266
         is false (i.e. in attributes)?  If not, we can kill the third
3267
         argument to cp_parser_string_literal.  */
3268
      return cp_parser_string_literal (parser,
3269
                                       parser->translate_strings_p,
3270
                                       true);
3271
 
3272
    case CPP_OPEN_PAREN:
3273
      {
3274
        tree expr;
3275
        bool saved_greater_than_is_operator_p;
3276
 
3277
        /* Consume the `('.  */
3278
        cp_lexer_consume_token (parser->lexer);
3279
        /* Within a parenthesized expression, a `>' token is always
3280
           the greater-than operator.  */
3281
        saved_greater_than_is_operator_p
3282
          = parser->greater_than_is_operator_p;
3283
        parser->greater_than_is_operator_p = true;
3284
        /* If we see `( { ' then we are looking at the beginning of
3285
           a GNU statement-expression.  */
3286
        if (cp_parser_allow_gnu_extensions_p (parser)
3287
            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3288
          {
3289
            /* Statement-expressions are not allowed by the standard.  */
3290
            pedwarn (token->location, OPT_pedantic,
3291
                     "ISO C++ forbids braced-groups within expressions");
3292
 
3293
            /* And they're not allowed outside of a function-body; you
3294
               cannot, for example, write:
3295
 
3296
                 int i = ({ int j = 3; j + 1; });
3297
 
3298
               at class or namespace scope.  */
3299
            if (!parser->in_function_body
3300
                || parser->in_template_argument_list_p)
3301
              {
3302
                error_at (token->location,
3303
                          "statement-expressions are not allowed outside "
3304
                          "functions nor in template-argument lists");
3305
                cp_parser_skip_to_end_of_block_or_statement (parser);
3306
                expr = error_mark_node;
3307
              }
3308
            else
3309
              {
3310
                /* Start the statement-expression.  */
3311
                expr = begin_stmt_expr ();
3312
                /* Parse the compound-statement.  */
3313
                cp_parser_compound_statement (parser, expr, false);
3314
                /* Finish up.  */
3315
                expr = finish_stmt_expr (expr, false);
3316
              }
3317
          }
3318
        else
3319
          {
3320
            /* Parse the parenthesized expression.  */
3321
            expr = cp_parser_expression (parser, cast_p, idk);
3322
            /* Let the front end know that this expression was
3323
               enclosed in parentheses. This matters in case, for
3324
               example, the expression is of the form `A::B', since
3325
               `&A::B' might be a pointer-to-member, but `&(A::B)' is
3326
               not.  */
3327
            finish_parenthesized_expr (expr);
3328
          }
3329
        /* The `>' token might be the end of a template-id or
3330
           template-parameter-list now.  */
3331
        parser->greater_than_is_operator_p
3332
          = saved_greater_than_is_operator_p;
3333
        /* Consume the `)'.  */
3334
        if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3335
          cp_parser_skip_to_end_of_statement (parser);
3336
 
3337
        return expr;
3338
      }
3339
 
3340
    case CPP_OPEN_SQUARE:
3341
      if (c_dialect_objc ())
3342
        /* We have an Objective-C++ message. */
3343
        return cp_parser_objc_expression (parser);
3344
      maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3345
      return cp_parser_lambda_expression (parser);
3346
 
3347
    case CPP_OBJC_STRING:
3348
      if (c_dialect_objc ())
3349
        /* We have an Objective-C++ string literal. */
3350
        return cp_parser_objc_expression (parser);
3351
      cp_parser_error (parser, "expected primary-expression");
3352
      return error_mark_node;
3353
 
3354
    case CPP_KEYWORD:
3355
      switch (token->keyword)
3356
        {
3357
          /* These two are the boolean literals.  */
3358
        case RID_TRUE:
3359
          cp_lexer_consume_token (parser->lexer);
3360
          return boolean_true_node;
3361
        case RID_FALSE:
3362
          cp_lexer_consume_token (parser->lexer);
3363
          return boolean_false_node;
3364
 
3365
          /* The `__null' literal.  */
3366
        case RID_NULL:
3367
          cp_lexer_consume_token (parser->lexer);
3368
          return null_node;
3369
 
3370
          /* Recognize the `this' keyword.  */
3371
        case RID_THIS:
3372
          cp_lexer_consume_token (parser->lexer);
3373
          if (parser->local_variables_forbidden_p)
3374
            {
3375
              error_at (token->location,
3376
                        "%<this%> may not be used in this context");
3377
              return error_mark_node;
3378
            }
3379
          /* Pointers cannot appear in constant-expressions.  */
3380
          if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3381
            return error_mark_node;
3382
          return finish_this_expr ();
3383
 
3384
          /* The `operator' keyword can be the beginning of an
3385
             id-expression.  */
3386
        case RID_OPERATOR:
3387
          goto id_expression;
3388
 
3389
        case RID_FUNCTION_NAME:
3390
        case RID_PRETTY_FUNCTION_NAME:
3391
        case RID_C99_FUNCTION_NAME:
3392
          {
3393
            const char *name;
3394
 
3395
            /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3396
               __func__ are the names of variables -- but they are
3397
               treated specially.  Therefore, they are handled here,
3398
               rather than relying on the generic id-expression logic
3399
               below.  Grammatically, these names are id-expressions.
3400
 
3401
               Consume the token.  */
3402
            token = cp_lexer_consume_token (parser->lexer);
3403
 
3404
            switch (token->keyword)
3405
              {
3406
              case RID_FUNCTION_NAME:
3407
                name = "%<__FUNCTION__%>";
3408
                break;
3409
              case RID_PRETTY_FUNCTION_NAME:
3410
                name = "%<__PRETTY_FUNCTION__%>";
3411
                break;
3412
              case RID_C99_FUNCTION_NAME:
3413
                name = "%<__func__%>";
3414
                break;
3415
              default:
3416
                gcc_unreachable ();
3417
              }
3418
 
3419
            if (cp_parser_non_integral_constant_expression (parser, name))
3420
              return error_mark_node;
3421
 
3422
            /* Look up the name.  */
3423
            return finish_fname (token->u.value);
3424
          }
3425
 
3426
        case RID_VA_ARG:
3427
          {
3428
            tree expression;
3429
            tree type;
3430
 
3431
            /* The `__builtin_va_arg' construct is used to handle
3432
               `va_arg'.  Consume the `__builtin_va_arg' token.  */
3433
            cp_lexer_consume_token (parser->lexer);
3434
            /* Look for the opening `('.  */
3435
            cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3436
            /* Now, parse the assignment-expression.  */
3437
            expression = cp_parser_assignment_expression (parser,
3438
                                                          /*cast_p=*/false, NULL);
3439
            /* Look for the `,'.  */
3440
            cp_parser_require (parser, CPP_COMMA, "%<,%>");
3441
            /* Parse the type-id.  */
3442
            type = cp_parser_type_id (parser);
3443
            /* Look for the closing `)'.  */
3444
            cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3445
            /* Using `va_arg' in a constant-expression is not
3446
               allowed.  */
3447
            if (cp_parser_non_integral_constant_expression (parser,
3448
                                                            "%<va_arg%>"))
3449
              return error_mark_node;
3450
            return build_x_va_arg (expression, type);
3451
          }
3452
 
3453
        case RID_OFFSETOF:
3454
          return cp_parser_builtin_offsetof (parser);
3455
 
3456
        case RID_HAS_NOTHROW_ASSIGN:
3457
        case RID_HAS_NOTHROW_CONSTRUCTOR:
3458
        case RID_HAS_NOTHROW_COPY:
3459
        case RID_HAS_TRIVIAL_ASSIGN:
3460
        case RID_HAS_TRIVIAL_CONSTRUCTOR:
3461
        case RID_HAS_TRIVIAL_COPY:
3462
        case RID_HAS_TRIVIAL_DESTRUCTOR:
3463
        case RID_HAS_VIRTUAL_DESTRUCTOR:
3464
        case RID_IS_ABSTRACT:
3465
        case RID_IS_BASE_OF:
3466
        case RID_IS_CLASS:
3467
        case RID_IS_CONVERTIBLE_TO:
3468
        case RID_IS_EMPTY:
3469
        case RID_IS_ENUM:
3470
        case RID_IS_POD:
3471
        case RID_IS_POLYMORPHIC:
3472
        case RID_IS_STD_LAYOUT:
3473
        case RID_IS_TRIVIAL:
3474
        case RID_IS_UNION:
3475
          return cp_parser_trait_expr (parser, token->keyword);
3476
 
3477
        /* Objective-C++ expressions.  */
3478
        case RID_AT_ENCODE:
3479
        case RID_AT_PROTOCOL:
3480
        case RID_AT_SELECTOR:
3481
          return cp_parser_objc_expression (parser);
3482
 
3483
        default:
3484
          cp_parser_error (parser, "expected primary-expression");
3485
          return error_mark_node;
3486
        }
3487
 
3488
      /* An id-expression can start with either an identifier, a
3489
         `::' as the beginning of a qualified-id, or the "operator"
3490
         keyword.  */
3491
    case CPP_NAME:
3492
    case CPP_SCOPE:
3493
    case CPP_TEMPLATE_ID:
3494
    case CPP_NESTED_NAME_SPECIFIER:
3495
      {
3496
        tree id_expression;
3497
        tree decl;
3498
        const char *error_msg;
3499
        bool template_p;
3500
        bool done;
3501
        cp_token *id_expr_token;
3502
 
3503
      id_expression:
3504
        /* Parse the id-expression.  */
3505
        id_expression
3506
          = cp_parser_id_expression (parser,
3507
                                     /*template_keyword_p=*/false,
3508
                                     /*check_dependency_p=*/true,
3509
                                     &template_p,
3510
                                     /*declarator_p=*/false,
3511
                                     /*optional_p=*/false);
3512
        if (id_expression == error_mark_node)
3513
          return error_mark_node;
3514
        id_expr_token = token;
3515
        token = cp_lexer_peek_token (parser->lexer);
3516
        done = (token->type != CPP_OPEN_SQUARE
3517
                && token->type != CPP_OPEN_PAREN
3518
                && token->type != CPP_DOT
3519
                && token->type != CPP_DEREF
3520
                && token->type != CPP_PLUS_PLUS
3521
                && token->type != CPP_MINUS_MINUS);
3522
        /* If we have a template-id, then no further lookup is
3523
           required.  If the template-id was for a template-class, we
3524
           will sometimes have a TYPE_DECL at this point.  */
3525
        if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3526
                 || TREE_CODE (id_expression) == TYPE_DECL)
3527
          decl = id_expression;
3528
        /* Look up the name.  */
3529
        else
3530
          {
3531
            tree ambiguous_decls;
3532
 
3533
            /* If we already know that this lookup is ambiguous, then
3534
               we've already issued an error message; there's no reason
3535
               to check again.  */
3536
            if (id_expr_token->type == CPP_NAME
3537
                && id_expr_token->ambiguous_p)
3538
              {
3539
                cp_parser_simulate_error (parser);
3540
                return error_mark_node;
3541
              }
3542
 
3543
            decl = cp_parser_lookup_name (parser, id_expression,
3544
                                          none_type,
3545
                                          template_p,
3546
                                          /*is_namespace=*/false,
3547
                                          /*check_dependency=*/true,
3548
                                          &ambiguous_decls,
3549
                                          id_expr_token->location);
3550
            /* If the lookup was ambiguous, an error will already have
3551
               been issued.  */
3552
            if (ambiguous_decls)
3553
              return error_mark_node;
3554
 
3555
            /* In Objective-C++, an instance variable (ivar) may be preferred
3556
               to whatever cp_parser_lookup_name() found.  */
3557
            decl = objc_lookup_ivar (decl, id_expression);
3558
 
3559
            /* If name lookup gives us a SCOPE_REF, then the
3560
               qualifying scope was dependent.  */
3561
            if (TREE_CODE (decl) == SCOPE_REF)
3562
              {
3563
                /* At this point, we do not know if DECL is a valid
3564
                   integral constant expression.  We assume that it is
3565
                   in fact such an expression, so that code like:
3566
 
3567
                      template <int N> struct A {
3568
                        int a[B<N>::i];
3569
                      };
3570
 
3571
                   is accepted.  At template-instantiation time, we
3572
                   will check that B<N>::i is actually a constant.  */
3573
                return decl;
3574
              }
3575
            /* Check to see if DECL is a local variable in a context
3576
               where that is forbidden.  */
3577
            if (parser->local_variables_forbidden_p
3578
                && local_variable_p (decl))
3579
              {
3580
                /* It might be that we only found DECL because we are
3581
                   trying to be generous with pre-ISO scoping rules.
3582
                   For example, consider:
3583
 
3584
                     int i;
3585
                     void g() {
3586
                       for (int i = 0; i < 10; ++i) {}
3587
                       extern void f(int j = i);
3588
                     }
3589
 
3590
                   Here, name look up will originally find the out
3591
                   of scope `i'.  We need to issue a warning message,
3592
                   but then use the global `i'.  */
3593
                decl = check_for_out_of_scope_variable (decl);
3594
                if (local_variable_p (decl))
3595
                  {
3596
                    error_at (id_expr_token->location,
3597
                              "local variable %qD may not appear in this context",
3598
                              decl);
3599
                    return error_mark_node;
3600
                  }
3601
              }
3602
          }
3603
 
3604
        decl = (finish_id_expression
3605
                (id_expression, decl, parser->scope,
3606
                 idk,
3607
                 parser->integral_constant_expression_p,
3608
                 parser->allow_non_integral_constant_expression_p,
3609
                 &parser->non_integral_constant_expression_p,
3610
                 template_p, done, address_p,
3611
                 template_arg_p,
3612
                 &error_msg,
3613
                 id_expr_token->location));
3614
        if (error_msg)
3615
          cp_parser_error (parser, error_msg);
3616
        return decl;
3617
      }
3618
 
3619
      /* Anything else is an error.  */
3620
    default:
3621
      cp_parser_error (parser, "expected primary-expression");
3622
      return error_mark_node;
3623
    }
3624
}
3625
 
3626
/* Parse an id-expression.
3627
 
3628
   id-expression:
3629
     unqualified-id
3630
     qualified-id
3631
 
3632
   qualified-id:
3633
     :: [opt] nested-name-specifier template [opt] unqualified-id
3634
     :: identifier
3635
     :: operator-function-id
3636
     :: template-id
3637
 
3638
   Return a representation of the unqualified portion of the
3639
   identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3640
   a `::' or nested-name-specifier.
3641
 
3642
   Often, if the id-expression was a qualified-id, the caller will
3643
   want to make a SCOPE_REF to represent the qualified-id.  This
3644
   function does not do this in order to avoid wastefully creating
3645
   SCOPE_REFs when they are not required.
3646
 
3647
   If TEMPLATE_KEYWORD_P is true, then we have just seen the
3648
   `template' keyword.
3649
 
3650
   If CHECK_DEPENDENCY_P is false, then names are looked up inside
3651
   uninstantiated templates.
3652
 
3653
   If *TEMPLATE_P is non-NULL, it is set to true iff the
3654
   `template' keyword is used to explicitly indicate that the entity
3655
   named is a template.
3656
 
3657
   If DECLARATOR_P is true, the id-expression is appearing as part of
3658
   a declarator, rather than as part of an expression.  */
3659
 
3660
static tree
3661
cp_parser_id_expression (cp_parser *parser,
3662
                         bool template_keyword_p,
3663
                         bool check_dependency_p,
3664
                         bool *template_p,
3665
                         bool declarator_p,
3666
                         bool optional_p)
3667
{
3668
  bool global_scope_p;
3669
  bool nested_name_specifier_p;
3670
 
3671
  /* Assume the `template' keyword was not used.  */
3672
  if (template_p)
3673
    *template_p = template_keyword_p;
3674
 
3675
  /* Look for the optional `::' operator.  */
3676
  global_scope_p
3677
    = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3678
       != NULL_TREE);
3679
  /* Look for the optional nested-name-specifier.  */
3680
  nested_name_specifier_p
3681
    = (cp_parser_nested_name_specifier_opt (parser,
3682
                                            /*typename_keyword_p=*/false,
3683
                                            check_dependency_p,
3684
                                            /*type_p=*/false,
3685
                                            declarator_p)
3686
       != NULL_TREE);
3687
  /* If there is a nested-name-specifier, then we are looking at
3688
     the first qualified-id production.  */
3689
  if (nested_name_specifier_p)
3690
    {
3691
      tree saved_scope;
3692
      tree saved_object_scope;
3693
      tree saved_qualifying_scope;
3694
      tree unqualified_id;
3695
      bool is_template;
3696
 
3697
      /* See if the next token is the `template' keyword.  */
3698
      if (!template_p)
3699
        template_p = &is_template;
3700
      *template_p = cp_parser_optional_template_keyword (parser);
3701
      /* Name lookup we do during the processing of the
3702
         unqualified-id might obliterate SCOPE.  */
3703
      saved_scope = parser->scope;
3704
      saved_object_scope = parser->object_scope;
3705
      saved_qualifying_scope = parser->qualifying_scope;
3706
      /* Process the final unqualified-id.  */
3707
      unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3708
                                                 check_dependency_p,
3709
                                                 declarator_p,
3710
                                                 /*optional_p=*/false);
3711
      /* Restore the SAVED_SCOPE for our caller.  */
3712
      parser->scope = saved_scope;
3713
      parser->object_scope = saved_object_scope;
3714
      parser->qualifying_scope = saved_qualifying_scope;
3715
 
3716
      return unqualified_id;
3717
    }
3718
  /* Otherwise, if we are in global scope, then we are looking at one
3719
     of the other qualified-id productions.  */
3720
  else if (global_scope_p)
3721
    {
3722
      cp_token *token;
3723
      tree id;
3724
 
3725
      /* Peek at the next token.  */
3726
      token = cp_lexer_peek_token (parser->lexer);
3727
 
3728
      /* If it's an identifier, and the next token is not a "<", then
3729
         we can avoid the template-id case.  This is an optimization
3730
         for this common case.  */
3731
      if (token->type == CPP_NAME
3732
          && !cp_parser_nth_token_starts_template_argument_list_p
3733
               (parser, 2))
3734
        return cp_parser_identifier (parser);
3735
 
3736
      cp_parser_parse_tentatively (parser);
3737
      /* Try a template-id.  */
3738
      id = cp_parser_template_id (parser,
3739
                                  /*template_keyword_p=*/false,
3740
                                  /*check_dependency_p=*/true,
3741
                                  declarator_p);
3742
      /* If that worked, we're done.  */
3743
      if (cp_parser_parse_definitely (parser))
3744
        return id;
3745
 
3746
      /* Peek at the next token.  (Changes in the token buffer may
3747
         have invalidated the pointer obtained above.)  */
3748
      token = cp_lexer_peek_token (parser->lexer);
3749
 
3750
      switch (token->type)
3751
        {
3752
        case CPP_NAME:
3753
          return cp_parser_identifier (parser);
3754
 
3755
        case CPP_KEYWORD:
3756
          if (token->keyword == RID_OPERATOR)
3757
            return cp_parser_operator_function_id (parser);
3758
          /* Fall through.  */
3759
 
3760
        default:
3761
          cp_parser_error (parser, "expected id-expression");
3762
          return error_mark_node;
3763
        }
3764
    }
3765
  else
3766
    return cp_parser_unqualified_id (parser, template_keyword_p,
3767
                                     /*check_dependency_p=*/true,
3768
                                     declarator_p,
3769
                                     optional_p);
3770
}
3771
 
3772
/* Parse an unqualified-id.
3773
 
3774
   unqualified-id:
3775
     identifier
3776
     operator-function-id
3777
     conversion-function-id
3778
     ~ class-name
3779
     template-id
3780
 
3781
   If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3782
   keyword, in a construct like `A::template ...'.
3783
 
3784
   Returns a representation of unqualified-id.  For the `identifier'
3785
   production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3786
   production a BIT_NOT_EXPR is returned; the operand of the
3787
   BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3788
   other productions, see the documentation accompanying the
3789
   corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3790
   names are looked up in uninstantiated templates.  If DECLARATOR_P
3791
   is true, the unqualified-id is appearing as part of a declarator,
3792
   rather than as part of an expression.  */
3793
 
3794
static tree
3795
cp_parser_unqualified_id (cp_parser* parser,
3796
                          bool template_keyword_p,
3797
                          bool check_dependency_p,
3798
                          bool declarator_p,
3799
                          bool optional_p)
3800
{
3801
  cp_token *token;
3802
 
3803
  /* Peek at the next token.  */
3804
  token = cp_lexer_peek_token (parser->lexer);
3805
 
3806
  switch (token->type)
3807
    {
3808
    case CPP_NAME:
3809
      {
3810
        tree id;
3811
 
3812
        /* We don't know yet whether or not this will be a
3813
           template-id.  */
3814
        cp_parser_parse_tentatively (parser);
3815
        /* Try a template-id.  */
3816
        id = cp_parser_template_id (parser, template_keyword_p,
3817
                                    check_dependency_p,
3818
                                    declarator_p);
3819
        /* If it worked, we're done.  */
3820
        if (cp_parser_parse_definitely (parser))
3821
          return id;
3822
        /* Otherwise, it's an ordinary identifier.  */
3823
        return cp_parser_identifier (parser);
3824
      }
3825
 
3826
    case CPP_TEMPLATE_ID:
3827
      return cp_parser_template_id (parser, template_keyword_p,
3828
                                    check_dependency_p,
3829
                                    declarator_p);
3830
 
3831
    case CPP_COMPL:
3832
      {
3833
        tree type_decl;
3834
        tree qualifying_scope;
3835
        tree object_scope;
3836
        tree scope;
3837
        bool done;
3838
 
3839
        /* Consume the `~' token.  */
3840
        cp_lexer_consume_token (parser->lexer);
3841
        /* Parse the class-name.  The standard, as written, seems to
3842
           say that:
3843
 
3844
             template <typename T> struct S { ~S (); };
3845
             template <typename T> S<T>::~S() {}
3846
 
3847
           is invalid, since `~' must be followed by a class-name, but
3848
           `S<T>' is dependent, and so not known to be a class.
3849
           That's not right; we need to look in uninstantiated
3850
           templates.  A further complication arises from:
3851
 
3852
             template <typename T> void f(T t) {
3853
               t.T::~T();
3854
             }
3855
 
3856
           Here, it is not possible to look up `T' in the scope of `T'
3857
           itself.  We must look in both the current scope, and the
3858
           scope of the containing complete expression.
3859
 
3860
           Yet another issue is:
3861
 
3862
             struct S {
3863
               int S;
3864
               ~S();
3865
             };
3866
 
3867
             S::~S() {}
3868
 
3869
           The standard does not seem to say that the `S' in `~S'
3870
           should refer to the type `S' and not the data member
3871
           `S::S'.  */
3872
 
3873
        /* DR 244 says that we look up the name after the "~" in the
3874
           same scope as we looked up the qualifying name.  That idea
3875
           isn't fully worked out; it's more complicated than that.  */
3876
        scope = parser->scope;
3877
        object_scope = parser->object_scope;
3878
        qualifying_scope = parser->qualifying_scope;
3879
 
3880
        /* Check for invalid scopes.  */
3881
        if (scope == error_mark_node)
3882
          {
3883
            if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3884
              cp_lexer_consume_token (parser->lexer);
3885
            return error_mark_node;
3886
          }
3887
        if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3888
          {
3889
            if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3890
              error_at (token->location,
3891
                        "scope %qT before %<~%> is not a class-name",
3892
                        scope);
3893
            cp_parser_simulate_error (parser);
3894
            if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3895
              cp_lexer_consume_token (parser->lexer);
3896
            return error_mark_node;
3897
          }
3898
        gcc_assert (!scope || TYPE_P (scope));
3899
 
3900
        /* If the name is of the form "X::~X" it's OK even if X is a
3901
           typedef.  */
3902
        token = cp_lexer_peek_token (parser->lexer);
3903
        if (scope
3904
            && token->type == CPP_NAME
3905
            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3906
                != CPP_LESS)
3907
            && (token->u.value == TYPE_IDENTIFIER (scope)
3908
                || constructor_name_p (token->u.value, scope)))
3909
          {
3910
            cp_lexer_consume_token (parser->lexer);
3911
            return build_nt (BIT_NOT_EXPR, scope);
3912
          }
3913
 
3914
        /* If there was an explicit qualification (S::~T), first look
3915
           in the scope given by the qualification (i.e., S).
3916
 
3917
           Note: in the calls to cp_parser_class_name below we pass
3918
           typename_type so that lookup finds the injected-class-name
3919
           rather than the constructor.  */
3920
        done = false;
3921
        type_decl = NULL_TREE;
3922
        if (scope)
3923
          {
3924
            cp_parser_parse_tentatively (parser);
3925
            type_decl = cp_parser_class_name (parser,
3926
                                              /*typename_keyword_p=*/false,
3927
                                              /*template_keyword_p=*/false,
3928
                                              typename_type,
3929
                                              /*check_dependency=*/false,
3930
                                              /*class_head_p=*/false,
3931
                                              declarator_p);
3932
            if (cp_parser_parse_definitely (parser))
3933
              done = true;
3934
          }
3935
        /* In "N::S::~S", look in "N" as well.  */
3936
        if (!done && scope && qualifying_scope)
3937
          {
3938
            cp_parser_parse_tentatively (parser);
3939
            parser->scope = qualifying_scope;
3940
            parser->object_scope = NULL_TREE;
3941
            parser->qualifying_scope = NULL_TREE;
3942
            type_decl
3943
              = cp_parser_class_name (parser,
3944
                                      /*typename_keyword_p=*/false,
3945
                                      /*template_keyword_p=*/false,
3946
                                      typename_type,
3947
                                      /*check_dependency=*/false,
3948
                                      /*class_head_p=*/false,
3949
                                      declarator_p);
3950
            if (cp_parser_parse_definitely (parser))
3951
              done = true;
3952
          }
3953
        /* In "p->S::~T", look in the scope given by "*p" as well.  */
3954
        else if (!done && object_scope)
3955
          {
3956
            cp_parser_parse_tentatively (parser);
3957
            parser->scope = object_scope;
3958
            parser->object_scope = NULL_TREE;
3959
            parser->qualifying_scope = NULL_TREE;
3960
            type_decl
3961
              = cp_parser_class_name (parser,
3962
                                      /*typename_keyword_p=*/false,
3963
                                      /*template_keyword_p=*/false,
3964
                                      typename_type,
3965
                                      /*check_dependency=*/false,
3966
                                      /*class_head_p=*/false,
3967
                                      declarator_p);
3968
            if (cp_parser_parse_definitely (parser))
3969
              done = true;
3970
          }
3971
        /* Look in the surrounding context.  */
3972
        if (!done)
3973
          {
3974
            parser->scope = NULL_TREE;
3975
            parser->object_scope = NULL_TREE;
3976
            parser->qualifying_scope = NULL_TREE;
3977
            if (processing_template_decl)
3978
              cp_parser_parse_tentatively (parser);
3979
            type_decl
3980
              = cp_parser_class_name (parser,
3981
                                      /*typename_keyword_p=*/false,
3982
                                      /*template_keyword_p=*/false,
3983
                                      typename_type,
3984
                                      /*check_dependency=*/false,
3985
                                      /*class_head_p=*/false,
3986
                                      declarator_p);
3987
            if (processing_template_decl
3988
                && ! cp_parser_parse_definitely (parser))
3989
              {
3990
                /* We couldn't find a type with this name, so just accept
3991
                   it and check for a match at instantiation time.  */
3992
                type_decl = cp_parser_identifier (parser);
3993
                if (type_decl != error_mark_node)
3994
                  type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3995
                return type_decl;
3996
              }
3997
          }
3998
        /* If an error occurred, assume that the name of the
3999
           destructor is the same as the name of the qualifying
4000
           class.  That allows us to keep parsing after running
4001
           into ill-formed destructor names.  */
4002
        if (type_decl == error_mark_node && scope)
4003
          return build_nt (BIT_NOT_EXPR, scope);
4004
        else if (type_decl == error_mark_node)
4005
          return error_mark_node;
4006
 
4007
        /* Check that destructor name and scope match.  */
4008
        if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4009
          {
4010
            if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4011
              error_at (token->location,
4012
                        "declaration of %<~%T%> as member of %qT",
4013
                        type_decl, scope);
4014
            cp_parser_simulate_error (parser);
4015
            return error_mark_node;
4016
          }
4017
 
4018
        /* [class.dtor]
4019
 
4020
           A typedef-name that names a class shall not be used as the
4021
           identifier in the declarator for a destructor declaration.  */
4022
        if (declarator_p
4023
            && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4024
            && !DECL_SELF_REFERENCE_P (type_decl)
4025
            && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4026
          error_at (token->location,
4027
                    "typedef-name %qD used as destructor declarator",
4028
                    type_decl);
4029
 
4030
        return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4031
      }
4032
 
4033
    case CPP_KEYWORD:
4034
      if (token->keyword == RID_OPERATOR)
4035
        {
4036
          tree id;
4037
 
4038
          /* This could be a template-id, so we try that first.  */
4039
          cp_parser_parse_tentatively (parser);
4040
          /* Try a template-id.  */
4041
          id = cp_parser_template_id (parser, template_keyword_p,
4042
                                      /*check_dependency_p=*/true,
4043
                                      declarator_p);
4044
          /* If that worked, we're done.  */
4045
          if (cp_parser_parse_definitely (parser))
4046
            return id;
4047
          /* We still don't know whether we're looking at an
4048
             operator-function-id or a conversion-function-id.  */
4049
          cp_parser_parse_tentatively (parser);
4050
          /* Try an operator-function-id.  */
4051
          id = cp_parser_operator_function_id (parser);
4052
          /* If that didn't work, try a conversion-function-id.  */
4053
          if (!cp_parser_parse_definitely (parser))
4054
            id = cp_parser_conversion_function_id (parser);
4055
 
4056
          return id;
4057
        }
4058
      /* Fall through.  */
4059
 
4060
    default:
4061
      if (optional_p)
4062
        return NULL_TREE;
4063
      cp_parser_error (parser, "expected unqualified-id");
4064
      return error_mark_node;
4065
    }
4066
}
4067
 
4068
/* Parse an (optional) nested-name-specifier.
4069
 
4070
   nested-name-specifier: [C++98]
4071
     class-or-namespace-name :: nested-name-specifier [opt]
4072
     class-or-namespace-name :: template nested-name-specifier [opt]
4073
 
4074
   nested-name-specifier: [C++0x]
4075
     type-name ::
4076
     namespace-name ::
4077
     nested-name-specifier identifier ::
4078
     nested-name-specifier template [opt] simple-template-id ::
4079
 
4080
   PARSER->SCOPE should be set appropriately before this function is
4081
   called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4082
   effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4083
   in name lookups.
4084
 
4085
   Sets PARSER->SCOPE to the class (TYPE) or namespace
4086
   (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4087
   it unchanged if there is no nested-name-specifier.  Returns the new
4088
   scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4089
 
4090
   If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4091
   part of a declaration and/or decl-specifier.  */
4092
 
4093
static tree
4094
cp_parser_nested_name_specifier_opt (cp_parser *parser,
4095
                                     bool typename_keyword_p,
4096
                                     bool check_dependency_p,
4097
                                     bool type_p,
4098
                                     bool is_declaration)
4099
{
4100
  bool success = false;
4101
  cp_token_position start = 0;
4102
  cp_token *token;
4103
 
4104
  /* Remember where the nested-name-specifier starts.  */
4105
  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4106
    {
4107
      start = cp_lexer_token_position (parser->lexer, false);
4108
      push_deferring_access_checks (dk_deferred);
4109
    }
4110
 
4111
  while (true)
4112
    {
4113
      tree new_scope;
4114
      tree old_scope;
4115
      tree saved_qualifying_scope;
4116
      bool template_keyword_p;
4117
 
4118
      /* Spot cases that cannot be the beginning of a
4119
         nested-name-specifier.  */
4120
      token = cp_lexer_peek_token (parser->lexer);
4121
 
4122
      /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4123
         the already parsed nested-name-specifier.  */
4124
      if (token->type == CPP_NESTED_NAME_SPECIFIER)
4125
        {
4126
          /* Grab the nested-name-specifier and continue the loop.  */
4127
          cp_parser_pre_parsed_nested_name_specifier (parser);
4128
          /* If we originally encountered this nested-name-specifier
4129
             with IS_DECLARATION set to false, we will not have
4130
             resolved TYPENAME_TYPEs, so we must do so here.  */
4131
          if (is_declaration
4132
              && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4133
            {
4134
              new_scope = resolve_typename_type (parser->scope,
4135
                                                 /*only_current_p=*/false);
4136
              if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4137
                parser->scope = new_scope;
4138
            }
4139
          success = true;
4140
          continue;
4141
        }
4142
 
4143
      /* Spot cases that cannot be the beginning of a
4144
         nested-name-specifier.  On the second and subsequent times
4145
         through the loop, we look for the `template' keyword.  */
4146
      if (success && token->keyword == RID_TEMPLATE)
4147
        ;
4148
      /* A template-id can start a nested-name-specifier.  */
4149
      else if (token->type == CPP_TEMPLATE_ID)
4150
        ;
4151
      else
4152
        {
4153
          /* If the next token is not an identifier, then it is
4154
             definitely not a type-name or namespace-name.  */
4155
          if (token->type != CPP_NAME)
4156
            break;
4157
          /* If the following token is neither a `<' (to begin a
4158
             template-id), nor a `::', then we are not looking at a
4159
             nested-name-specifier.  */
4160
          token = cp_lexer_peek_nth_token (parser->lexer, 2);
4161
          if (token->type != CPP_SCOPE
4162
              && !cp_parser_nth_token_starts_template_argument_list_p
4163
                  (parser, 2))
4164
            break;
4165
        }
4166
 
4167
      /* The nested-name-specifier is optional, so we parse
4168
         tentatively.  */
4169
      cp_parser_parse_tentatively (parser);
4170
 
4171
      /* Look for the optional `template' keyword, if this isn't the
4172
         first time through the loop.  */
4173
      if (success)
4174
        template_keyword_p = cp_parser_optional_template_keyword (parser);
4175
      else
4176
        template_keyword_p = false;
4177
 
4178
      /* Save the old scope since the name lookup we are about to do
4179
         might destroy it.  */
4180
      old_scope = parser->scope;
4181
      saved_qualifying_scope = parser->qualifying_scope;
4182
      /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4183
         look up names in "X<T>::I" in order to determine that "Y" is
4184
         a template.  So, if we have a typename at this point, we make
4185
         an effort to look through it.  */
4186
      if (is_declaration
4187
          && !typename_keyword_p
4188
          && parser->scope
4189
          && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4190
        parser->scope = resolve_typename_type (parser->scope,
4191
                                               /*only_current_p=*/false);
4192
      /* Parse the qualifying entity.  */
4193
      new_scope
4194
        = cp_parser_qualifying_entity (parser,
4195
                                       typename_keyword_p,
4196
                                       template_keyword_p,
4197
                                       check_dependency_p,
4198
                                       type_p,
4199
                                       is_declaration);
4200
      /* Look for the `::' token.  */
4201
      cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4202
 
4203
      /* If we found what we wanted, we keep going; otherwise, we're
4204
         done.  */
4205
      if (!cp_parser_parse_definitely (parser))
4206
        {
4207
          bool error_p = false;
4208
 
4209
          /* Restore the OLD_SCOPE since it was valid before the
4210
             failed attempt at finding the last
4211
             class-or-namespace-name.  */
4212
          parser->scope = old_scope;
4213
          parser->qualifying_scope = saved_qualifying_scope;
4214
          if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4215
            break;
4216
          /* If the next token is an identifier, and the one after
4217
             that is a `::', then any valid interpretation would have
4218
             found a class-or-namespace-name.  */
4219
          while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4220
                 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4221
                     == CPP_SCOPE)
4222
                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4223
                     != CPP_COMPL))
4224
            {
4225
              token = cp_lexer_consume_token (parser->lexer);
4226
              if (!error_p)
4227
                {
4228
                  if (!token->ambiguous_p)
4229
                    {
4230
                      tree decl;
4231
                      tree ambiguous_decls;
4232
 
4233
                      decl = cp_parser_lookup_name (parser, token->u.value,
4234
                                                    none_type,
4235
                                                    /*is_template=*/false,
4236
                                                    /*is_namespace=*/false,
4237
                                                    /*check_dependency=*/true,
4238
                                                    &ambiguous_decls,
4239
                                                    token->location);
4240
                      if (TREE_CODE (decl) == TEMPLATE_DECL)
4241
                        error_at (token->location,
4242
                                  "%qD used without template parameters",
4243
                                  decl);
4244
                      else if (ambiguous_decls)
4245
                        {
4246
                          error_at (token->location,
4247
                                    "reference to %qD is ambiguous",
4248
                                    token->u.value);
4249
                          print_candidates (ambiguous_decls);
4250
                          decl = error_mark_node;
4251
                        }
4252
                      else
4253
                        {
4254
                          const char* msg = "is not a class or namespace";
4255
                          if (cxx_dialect != cxx98)
4256
                            msg = "is not a class, namespace, or enumeration";
4257
                          cp_parser_name_lookup_error
4258
                            (parser, token->u.value, decl, msg,
4259
                             token->location);
4260
                        }
4261
                    }
4262
                  parser->scope = error_mark_node;
4263
                  error_p = true;
4264
                  /* Treat this as a successful nested-name-specifier
4265
                     due to:
4266
 
4267
                     [basic.lookup.qual]
4268
 
4269
                     If the name found is not a class-name (clause
4270
                     _class_) or namespace-name (_namespace.def_), the
4271
                     program is ill-formed.  */
4272
                  success = true;
4273
                }
4274
              cp_lexer_consume_token (parser->lexer);
4275
            }
4276
          break;
4277
        }
4278
      /* We've found one valid nested-name-specifier.  */
4279
      success = true;
4280
      /* Name lookup always gives us a DECL.  */
4281
      if (TREE_CODE (new_scope) == TYPE_DECL)
4282
        new_scope = TREE_TYPE (new_scope);
4283
      /* Uses of "template" must be followed by actual templates.  */
4284
      if (template_keyword_p
4285
          && !(CLASS_TYPE_P (new_scope)
4286
               && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4287
                    && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4288
                   || CLASSTYPE_IS_TEMPLATE (new_scope)))
4289
          && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4290
               && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4291
                   == TEMPLATE_ID_EXPR)))
4292
        permerror (input_location, TYPE_P (new_scope)
4293
                   ? "%qT is not a template"
4294
                   : "%qD is not a template",
4295
                   new_scope);
4296
      /* If it is a class scope, try to complete it; we are about to
4297
         be looking up names inside the class.  */
4298
      if (TYPE_P (new_scope)
4299
          /* Since checking types for dependency can be expensive,
4300
             avoid doing it if the type is already complete.  */
4301
          && !COMPLETE_TYPE_P (new_scope)
4302
          /* Do not try to complete dependent types.  */
4303
          && !dependent_type_p (new_scope))
4304
        {
4305
          new_scope = complete_type (new_scope);
4306
          /* If it is a typedef to current class, use the current
4307
             class instead, as the typedef won't have any names inside
4308
             it yet.  */
4309
          if (!COMPLETE_TYPE_P (new_scope)
4310
              && currently_open_class (new_scope))
4311
            new_scope = TYPE_MAIN_VARIANT (new_scope);
4312
        }
4313
      /* Make sure we look in the right scope the next time through
4314
         the loop.  */
4315
      parser->scope = new_scope;
4316
    }
4317
 
4318
  /* If parsing tentatively, replace the sequence of tokens that makes
4319
     up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4320
     token.  That way, should we re-parse the token stream, we will
4321
     not have to repeat the effort required to do the parse, nor will
4322
     we issue duplicate error messages.  */
4323
  if (success && start)
4324
    {
4325
      cp_token *token;
4326
 
4327
      token = cp_lexer_token_at (parser->lexer, start);
4328
      /* Reset the contents of the START token.  */
4329
      token->type = CPP_NESTED_NAME_SPECIFIER;
4330
      /* Retrieve any deferred checks.  Do not pop this access checks yet
4331
         so the memory will not be reclaimed during token replacing below.  */
4332
      token->u.tree_check_value = GGC_CNEW (struct tree_check);
4333
      token->u.tree_check_value->value = parser->scope;
4334
      token->u.tree_check_value->checks = get_deferred_access_checks ();
4335
      token->u.tree_check_value->qualifying_scope =
4336
        parser->qualifying_scope;
4337
      token->keyword = RID_MAX;
4338
 
4339
      /* Purge all subsequent tokens.  */
4340
      cp_lexer_purge_tokens_after (parser->lexer, start);
4341
    }
4342
 
4343
  if (start)
4344
    pop_to_parent_deferring_access_checks ();
4345
 
4346
  return success ? parser->scope : NULL_TREE;
4347
}
4348
 
4349
/* Parse a nested-name-specifier.  See
4350
   cp_parser_nested_name_specifier_opt for details.  This function
4351
   behaves identically, except that it will an issue an error if no
4352
   nested-name-specifier is present.  */
4353
 
4354
static tree
4355
cp_parser_nested_name_specifier (cp_parser *parser,
4356
                                 bool typename_keyword_p,
4357
                                 bool check_dependency_p,
4358
                                 bool type_p,
4359
                                 bool is_declaration)
4360
{
4361
  tree scope;
4362
 
4363
  /* Look for the nested-name-specifier.  */
4364
  scope = cp_parser_nested_name_specifier_opt (parser,
4365
                                               typename_keyword_p,
4366
                                               check_dependency_p,
4367
                                               type_p,
4368
                                               is_declaration);
4369
  /* If it was not present, issue an error message.  */
4370
  if (!scope)
4371
    {
4372
      cp_parser_error (parser, "expected nested-name-specifier");
4373
      parser->scope = NULL_TREE;
4374
    }
4375
 
4376
  return scope;
4377
}
4378
 
4379
/* Parse the qualifying entity in a nested-name-specifier. For C++98,
4380
   this is either a class-name or a namespace-name (which corresponds
4381
   to the class-or-namespace-name production in the grammar). For
4382
   C++0x, it can also be a type-name that refers to an enumeration
4383
   type.
4384
 
4385
   TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4386
   TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4387
   CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4388
   TYPE_P is TRUE iff the next name should be taken as a class-name,
4389
   even the same name is declared to be another entity in the same
4390
   scope.
4391
 
4392
   Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4393
   specified by the class-or-namespace-name.  If neither is found the
4394
   ERROR_MARK_NODE is returned.  */
4395
 
4396
static tree
4397
cp_parser_qualifying_entity (cp_parser *parser,
4398
                             bool typename_keyword_p,
4399
                             bool template_keyword_p,
4400
                             bool check_dependency_p,
4401
                             bool type_p,
4402
                             bool is_declaration)
4403
{
4404
  tree saved_scope;
4405
  tree saved_qualifying_scope;
4406
  tree saved_object_scope;
4407
  tree scope;
4408
  bool only_class_p;
4409
  bool successful_parse_p;
4410
 
4411
  /* Before we try to parse the class-name, we must save away the
4412
     current PARSER->SCOPE since cp_parser_class_name will destroy
4413
     it.  */
4414
  saved_scope = parser->scope;
4415
  saved_qualifying_scope = parser->qualifying_scope;
4416
  saved_object_scope = parser->object_scope;
4417
  /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4418
     there is no need to look for a namespace-name.  */
4419
  only_class_p = template_keyword_p
4420
    || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4421
  if (!only_class_p)
4422
    cp_parser_parse_tentatively (parser);
4423
  scope = cp_parser_class_name (parser,
4424
                                typename_keyword_p,
4425
                                template_keyword_p,
4426
                                type_p ? class_type : none_type,
4427
                                check_dependency_p,
4428
                                /*class_head_p=*/false,
4429
                                is_declaration);
4430
  successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4431
  /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4432
  if (!only_class_p
4433
      && cxx_dialect != cxx98
4434
      && !successful_parse_p)
4435
    {
4436
      /* Restore the saved scope.  */
4437
      parser->scope = saved_scope;
4438
      parser->qualifying_scope = saved_qualifying_scope;
4439
      parser->object_scope = saved_object_scope;
4440
 
4441
      /* Parse tentatively.  */
4442
      cp_parser_parse_tentatively (parser);
4443
 
4444
      /* Parse a typedef-name or enum-name.  */
4445
      scope = cp_parser_nonclass_name (parser);
4446
 
4447
      /* "If the name found does not designate a namespace or a class,
4448
         enumeration, or dependent type, the program is ill-formed."
4449
 
4450
         We cover classes and dependent types above and namespaces below,
4451
         so this code is only looking for enums.  */
4452
      if (!scope || TREE_CODE (scope) != TYPE_DECL
4453
          || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4454
        cp_parser_simulate_error (parser);
4455
 
4456
      successful_parse_p = cp_parser_parse_definitely (parser);
4457
    }
4458
  /* If that didn't work, try for a namespace-name.  */
4459
  if (!only_class_p && !successful_parse_p)
4460
    {
4461
      /* Restore the saved scope.  */
4462
      parser->scope = saved_scope;
4463
      parser->qualifying_scope = saved_qualifying_scope;
4464
      parser->object_scope = saved_object_scope;
4465
      /* If we are not looking at an identifier followed by the scope
4466
         resolution operator, then this is not part of a
4467
         nested-name-specifier.  (Note that this function is only used
4468
         to parse the components of a nested-name-specifier.)  */
4469
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4470
          || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4471
        return error_mark_node;
4472
      scope = cp_parser_namespace_name (parser);
4473
    }
4474
 
4475
  return scope;
4476
}
4477
 
4478
/* Parse a postfix-expression.
4479
 
4480
   postfix-expression:
4481
     primary-expression
4482
     postfix-expression [ expression ]
4483
     postfix-expression ( expression-list [opt] )
4484
     simple-type-specifier ( expression-list [opt] )
4485
     typename :: [opt] nested-name-specifier identifier
4486
       ( expression-list [opt] )
4487
     typename :: [opt] nested-name-specifier template [opt] template-id
4488
       ( expression-list [opt] )
4489
     postfix-expression . template [opt] id-expression
4490
     postfix-expression -> template [opt] id-expression
4491
     postfix-expression . pseudo-destructor-name
4492
     postfix-expression -> pseudo-destructor-name
4493
     postfix-expression ++
4494
     postfix-expression --
4495
     dynamic_cast < type-id > ( expression )
4496
     static_cast < type-id > ( expression )
4497
     reinterpret_cast < type-id > ( expression )
4498
     const_cast < type-id > ( expression )
4499
     typeid ( expression )
4500
     typeid ( type-id )
4501
 
4502
   GNU Extension:
4503
 
4504
   postfix-expression:
4505
     ( type-id ) { initializer-list , [opt] }
4506
 
4507
   This extension is a GNU version of the C99 compound-literal
4508
   construct.  (The C99 grammar uses `type-name' instead of `type-id',
4509
   but they are essentially the same concept.)
4510
 
4511
   If ADDRESS_P is true, the postfix expression is the operand of the
4512
   `&' operator.  CAST_P is true if this expression is the target of a
4513
   cast.
4514
 
4515
   If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4516
   class member access expressions [expr.ref].
4517
 
4518
   Returns a representation of the expression.  */
4519
 
4520
static tree
4521
cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4522
                              bool member_access_only_p,
4523
                              cp_id_kind * pidk_return)
4524
{
4525
  cp_token *token;
4526
  enum rid keyword;
4527
  cp_id_kind idk = CP_ID_KIND_NONE;
4528
  tree postfix_expression = NULL_TREE;
4529
  bool is_member_access = false;
4530
 
4531
  /* Peek at the next token.  */
4532
  token = cp_lexer_peek_token (parser->lexer);
4533
  /* Some of the productions are determined by keywords.  */
4534
  keyword = token->keyword;
4535
  switch (keyword)
4536
    {
4537
    case RID_DYNCAST:
4538
    case RID_STATCAST:
4539
    case RID_REINTCAST:
4540
    case RID_CONSTCAST:
4541
      {
4542
        tree type;
4543
        tree expression;
4544
        const char *saved_message;
4545
 
4546
        /* All of these can be handled in the same way from the point
4547
           of view of parsing.  Begin by consuming the token
4548
           identifying the cast.  */
4549
        cp_lexer_consume_token (parser->lexer);
4550
 
4551
        /* New types cannot be defined in the cast.  */
4552
        saved_message = parser->type_definition_forbidden_message;
4553
        parser->type_definition_forbidden_message
4554
          = G_("types may not be defined in casts");
4555
 
4556
        /* Look for the opening `<'.  */
4557
        cp_parser_require (parser, CPP_LESS, "%<<%>");
4558
        /* Parse the type to which we are casting.  */
4559
        type = cp_parser_type_id (parser);
4560
        /* Look for the closing `>'.  */
4561
        cp_parser_require (parser, CPP_GREATER, "%<>%>");
4562
        /* Restore the old message.  */
4563
        parser->type_definition_forbidden_message = saved_message;
4564
 
4565
        /* And the expression which is being cast.  */
4566
        cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4567
        expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4568
        cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4569
 
4570
        /* Only type conversions to integral or enumeration types
4571
           can be used in constant-expressions.  */
4572
        if (!cast_valid_in_integral_constant_expression_p (type)
4573
            && (cp_parser_non_integral_constant_expression
4574
                (parser,
4575
                 "a cast to a type other than an integral or "
4576
                 "enumeration type")))
4577
          return error_mark_node;
4578
 
4579
        switch (keyword)
4580
          {
4581
          case RID_DYNCAST:
4582
            postfix_expression
4583
              = build_dynamic_cast (type, expression, tf_warning_or_error);
4584
            break;
4585
          case RID_STATCAST:
4586
            postfix_expression
4587
              = build_static_cast (type, expression, tf_warning_or_error);
4588
            break;
4589
          case RID_REINTCAST:
4590
            postfix_expression
4591
              = build_reinterpret_cast (type, expression,
4592
                                        tf_warning_or_error);
4593
            break;
4594
          case RID_CONSTCAST:
4595
            postfix_expression
4596
              = build_const_cast (type, expression, tf_warning_or_error);
4597
            break;
4598
          default:
4599
            gcc_unreachable ();
4600
          }
4601
      }
4602
      break;
4603
 
4604
    case RID_TYPEID:
4605
      {
4606
        tree type;
4607
        const char *saved_message;
4608
        bool saved_in_type_id_in_expr_p;
4609
 
4610
        /* Consume the `typeid' token.  */
4611
        cp_lexer_consume_token (parser->lexer);
4612
        /* Look for the `(' token.  */
4613
        cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4614
        /* Types cannot be defined in a `typeid' expression.  */
4615
        saved_message = parser->type_definition_forbidden_message;
4616
        parser->type_definition_forbidden_message
4617
          = G_("types may not be defined in a %<typeid%> expression");
4618
        /* We can't be sure yet whether we're looking at a type-id or an
4619
           expression.  */
4620
        cp_parser_parse_tentatively (parser);
4621
        /* Try a type-id first.  */
4622
        saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4623
        parser->in_type_id_in_expr_p = true;
4624
        type = cp_parser_type_id (parser);
4625
        parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4626
        /* Look for the `)' token.  Otherwise, we can't be sure that
4627
           we're not looking at an expression: consider `typeid (int
4628
           (3))', for example.  */
4629
        cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4630
        /* If all went well, simply lookup the type-id.  */
4631
        if (cp_parser_parse_definitely (parser))
4632
          postfix_expression = get_typeid (type);
4633
        /* Otherwise, fall back to the expression variant.  */
4634
        else
4635
          {
4636
            tree expression;
4637
 
4638
            /* Look for an expression.  */
4639
            expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4640
            /* Compute its typeid.  */
4641
            postfix_expression = build_typeid (expression);
4642
            /* Look for the `)' token.  */
4643
            cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4644
          }
4645
        /* Restore the saved message.  */
4646
        parser->type_definition_forbidden_message = saved_message;
4647
        /* `typeid' may not appear in an integral constant expression.  */
4648
        if (cp_parser_non_integral_constant_expression(parser,
4649
                                                       "%<typeid%> operator"))
4650
          return error_mark_node;
4651
      }
4652
      break;
4653
 
4654
    case RID_TYPENAME:
4655
      {
4656
        tree type;
4657
        /* The syntax permitted here is the same permitted for an
4658
           elaborated-type-specifier.  */
4659
        type = cp_parser_elaborated_type_specifier (parser,
4660
                                                    /*is_friend=*/false,
4661
                                                    /*is_declaration=*/false);
4662
        postfix_expression = cp_parser_functional_cast (parser, type);
4663
      }
4664
      break;
4665
 
4666
    default:
4667
      {
4668
        tree type;
4669
 
4670
        /* If the next thing is a simple-type-specifier, we may be
4671
           looking at a functional cast.  We could also be looking at
4672
           an id-expression.  So, we try the functional cast, and if
4673
           that doesn't work we fall back to the primary-expression.  */
4674
        cp_parser_parse_tentatively (parser);
4675
        /* Look for the simple-type-specifier.  */
4676
        type = cp_parser_simple_type_specifier (parser,
4677
                                                /*decl_specs=*/NULL,
4678
                                                CP_PARSER_FLAGS_NONE);
4679
        /* Parse the cast itself.  */
4680
        if (!cp_parser_error_occurred (parser))
4681
          postfix_expression
4682
            = cp_parser_functional_cast (parser, type);
4683
        /* If that worked, we're done.  */
4684
        if (cp_parser_parse_definitely (parser))
4685
          break;
4686
 
4687
        /* If the functional-cast didn't work out, try a
4688
           compound-literal.  */
4689
        if (cp_parser_allow_gnu_extensions_p (parser)
4690
            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4691
          {
4692
            VEC(constructor_elt,gc) *initializer_list = NULL;
4693
            bool saved_in_type_id_in_expr_p;
4694
 
4695
            cp_parser_parse_tentatively (parser);
4696
            /* Consume the `('.  */
4697
            cp_lexer_consume_token (parser->lexer);
4698
            /* Parse the type.  */
4699
            saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4700
            parser->in_type_id_in_expr_p = true;
4701
            type = cp_parser_type_id (parser);
4702
            parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4703
            /* Look for the `)'.  */
4704
            cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4705
            /* Look for the `{'.  */
4706
            cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4707
            /* If things aren't going well, there's no need to
4708
               keep going.  */
4709
            if (!cp_parser_error_occurred (parser))
4710
              {
4711
                bool non_constant_p;
4712
                /* Parse the initializer-list.  */
4713
                initializer_list
4714
                  = cp_parser_initializer_list (parser, &non_constant_p);
4715
                /* Allow a trailing `,'.  */
4716
                if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4717
                  cp_lexer_consume_token (parser->lexer);
4718
                /* Look for the final `}'.  */
4719
                cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4720
              }
4721
            /* If that worked, we're definitely looking at a
4722
               compound-literal expression.  */
4723
            if (cp_parser_parse_definitely (parser))
4724
              {
4725
                /* Warn the user that a compound literal is not
4726
                   allowed in standard C++.  */
4727
                pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4728
                /* For simplicity, we disallow compound literals in
4729
                   constant-expressions.  We could
4730
                   allow compound literals of integer type, whose
4731
                   initializer was a constant, in constant
4732
                   expressions.  Permitting that usage, as a further
4733
                   extension, would not change the meaning of any
4734
                   currently accepted programs.  (Of course, as
4735
                   compound literals are not part of ISO C++, the
4736
                   standard has nothing to say.)  */
4737
                if (cp_parser_non_integral_constant_expression
4738
                    (parser, "non-constant compound literals"))
4739
                  {
4740
                    postfix_expression = error_mark_node;
4741
                    break;
4742
                  }
4743
                /* Form the representation of the compound-literal.  */
4744
                postfix_expression
4745
                  = (finish_compound_literal
4746
                     (type, build_constructor (init_list_type_node,
4747
                                               initializer_list)));
4748
                break;
4749
              }
4750
          }
4751
 
4752
        /* It must be a primary-expression.  */
4753
        postfix_expression
4754
          = cp_parser_primary_expression (parser, address_p, cast_p,
4755
                                          /*template_arg_p=*/false,
4756
                                          &idk);
4757
      }
4758
      break;
4759
    }
4760
 
4761
  /* Keep looping until the postfix-expression is complete.  */
4762
  while (true)
4763
    {
4764
      if (idk == CP_ID_KIND_UNQUALIFIED
4765
          && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4766
          && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4767
        /* It is not a Koenig lookup function call.  */
4768
        postfix_expression
4769
          = unqualified_name_lookup_error (postfix_expression);
4770
 
4771
      /* Peek at the next token.  */
4772
      token = cp_lexer_peek_token (parser->lexer);
4773
 
4774
      switch (token->type)
4775
        {
4776
        case CPP_OPEN_SQUARE:
4777
          postfix_expression
4778
            = cp_parser_postfix_open_square_expression (parser,
4779
                                                        postfix_expression,
4780
                                                        false);
4781
          idk = CP_ID_KIND_NONE;
4782
          is_member_access = false;
4783
          break;
4784
 
4785
        case CPP_OPEN_PAREN:
4786
          /* postfix-expression ( expression-list [opt] ) */
4787
          {
4788
            bool koenig_p;
4789
            bool is_builtin_constant_p;
4790
            bool saved_integral_constant_expression_p = false;
4791
            bool saved_non_integral_constant_expression_p = false;
4792
            VEC(tree,gc) *args;
4793
 
4794
            is_member_access = false;
4795
 
4796
            is_builtin_constant_p
4797
              = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4798
            if (is_builtin_constant_p)
4799
              {
4800
                /* The whole point of __builtin_constant_p is to allow
4801
                   non-constant expressions to appear as arguments.  */
4802
                saved_integral_constant_expression_p
4803
                  = parser->integral_constant_expression_p;
4804
                saved_non_integral_constant_expression_p
4805
                  = parser->non_integral_constant_expression_p;
4806
                parser->integral_constant_expression_p = false;
4807
              }
4808
            args = (cp_parser_parenthesized_expression_list
4809
                    (parser, /*is_attribute_list=*/false,
4810
                     /*cast_p=*/false, /*allow_expansion_p=*/true,
4811
                     /*non_constant_p=*/NULL));
4812
            if (is_builtin_constant_p)
4813
              {
4814
                parser->integral_constant_expression_p
4815
                  = saved_integral_constant_expression_p;
4816
                parser->non_integral_constant_expression_p
4817
                  = saved_non_integral_constant_expression_p;
4818
              }
4819
 
4820
            if (args == NULL)
4821
              {
4822
                postfix_expression = error_mark_node;
4823
                break;
4824
              }
4825
 
4826
            /* Function calls are not permitted in
4827
               constant-expressions.  */
4828
            if (! builtin_valid_in_constant_expr_p (postfix_expression)
4829
                && cp_parser_non_integral_constant_expression (parser,
4830
                                                               "a function call"))
4831
              {
4832
                postfix_expression = error_mark_node;
4833
                release_tree_vector (args);
4834
                break;
4835
              }
4836
 
4837
            koenig_p = false;
4838
            if (idk == CP_ID_KIND_UNQUALIFIED
4839
                || idk == CP_ID_KIND_TEMPLATE_ID)
4840
              {
4841
                if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4842
                  {
4843
                    if (!VEC_empty (tree, args))
4844
                      {
4845
                        koenig_p = true;
4846
                        if (!any_type_dependent_arguments_p (args))
4847
                          postfix_expression
4848
                            = perform_koenig_lookup (postfix_expression, args);
4849
                      }
4850
                    else
4851
                      postfix_expression
4852
                        = unqualified_fn_lookup_error (postfix_expression);
4853
                  }
4854
                /* We do not perform argument-dependent lookup if
4855
                   normal lookup finds a non-function, in accordance
4856
                   with the expected resolution of DR 218.  */
4857
                else if (!VEC_empty (tree, args)
4858
                         && is_overloaded_fn (postfix_expression))
4859
                  {
4860
                    tree fn = get_first_fn (postfix_expression);
4861
                    fn = STRIP_TEMPLATE (fn);
4862
 
4863
                    /* Do not do argument dependent lookup if regular
4864
                       lookup finds a member function or a block-scope
4865
                       function declaration.  [basic.lookup.argdep]/3  */
4866
                    if (!DECL_FUNCTION_MEMBER_P (fn)
4867
                        && !DECL_LOCAL_FUNCTION_P (fn))
4868
                      {
4869
                        koenig_p = true;
4870
                        if (!any_type_dependent_arguments_p (args))
4871
                          postfix_expression
4872
                            = perform_koenig_lookup (postfix_expression, args);
4873
                      }
4874
                  }
4875
              }
4876
 
4877
            if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4878
              {
4879
                tree instance = TREE_OPERAND (postfix_expression, 0);
4880
                tree fn = TREE_OPERAND (postfix_expression, 1);
4881
 
4882
                if (processing_template_decl
4883
                    && (type_dependent_expression_p (instance)
4884
                        || (!BASELINK_P (fn)
4885
                            && TREE_CODE (fn) != FIELD_DECL)
4886
                        || type_dependent_expression_p (fn)
4887
                        || any_type_dependent_arguments_p (args)))
4888
                  {
4889
                    postfix_expression
4890
                      = build_nt_call_vec (postfix_expression, args);
4891
                    release_tree_vector (args);
4892
                    break;
4893
                  }
4894
 
4895
                if (BASELINK_P (fn))
4896
                  {
4897
                  postfix_expression
4898
                    = (build_new_method_call
4899
                       (instance, fn, &args, NULL_TREE,
4900
                        (idk == CP_ID_KIND_QUALIFIED
4901
                         ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4902
                        /*fn_p=*/NULL,
4903
                        tf_warning_or_error));
4904
                  }
4905
                else
4906
                  postfix_expression
4907
                    = finish_call_expr (postfix_expression, &args,
4908
                                        /*disallow_virtual=*/false,
4909
                                        /*koenig_p=*/false,
4910
                                        tf_warning_or_error);
4911
              }
4912
            else if (TREE_CODE (postfix_expression) == OFFSET_REF
4913
                     || TREE_CODE (postfix_expression) == MEMBER_REF
4914
                     || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4915
              postfix_expression = (build_offset_ref_call_from_tree
4916
                                    (postfix_expression, &args));
4917
            else if (idk == CP_ID_KIND_QUALIFIED)
4918
              /* A call to a static class member, or a namespace-scope
4919
                 function.  */
4920
              postfix_expression
4921
                = finish_call_expr (postfix_expression, &args,
4922
                                    /*disallow_virtual=*/true,
4923
                                    koenig_p,
4924
                                    tf_warning_or_error);
4925
            else
4926
              /* All other function calls.  */
4927
              postfix_expression
4928
                = finish_call_expr (postfix_expression, &args,
4929
                                    /*disallow_virtual=*/false,
4930
                                    koenig_p,
4931
                                    tf_warning_or_error);
4932
 
4933
            /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4934
            idk = CP_ID_KIND_NONE;
4935
 
4936
            release_tree_vector (args);
4937
          }
4938
          break;
4939
 
4940
        case CPP_DOT:
4941
        case CPP_DEREF:
4942
          /* postfix-expression . template [opt] id-expression
4943
             postfix-expression . pseudo-destructor-name
4944
             postfix-expression -> template [opt] id-expression
4945
             postfix-expression -> pseudo-destructor-name */
4946
 
4947
          /* Consume the `.' or `->' operator.  */
4948
          cp_lexer_consume_token (parser->lexer);
4949
 
4950
          postfix_expression
4951
            = cp_parser_postfix_dot_deref_expression (parser, token->type,
4952
                                                      postfix_expression,
4953
                                                      false, &idk,
4954
                                                      token->location);
4955
 
4956
          is_member_access = true;
4957
          break;
4958
 
4959
        case CPP_PLUS_PLUS:
4960
          /* postfix-expression ++  */
4961
          /* Consume the `++' token.  */
4962
          cp_lexer_consume_token (parser->lexer);
4963
          /* Generate a representation for the complete expression.  */
4964
          postfix_expression
4965
            = finish_increment_expr (postfix_expression,
4966
                                     POSTINCREMENT_EXPR);
4967
          /* Increments may not appear in constant-expressions.  */
4968
          if (cp_parser_non_integral_constant_expression (parser,
4969
                                                          "an increment"))
4970
            postfix_expression = error_mark_node;
4971
          idk = CP_ID_KIND_NONE;
4972
          is_member_access = false;
4973
          break;
4974
 
4975
        case CPP_MINUS_MINUS:
4976
          /* postfix-expression -- */
4977
          /* Consume the `--' token.  */
4978
          cp_lexer_consume_token (parser->lexer);
4979
          /* Generate a representation for the complete expression.  */
4980
          postfix_expression
4981
            = finish_increment_expr (postfix_expression,
4982
                                     POSTDECREMENT_EXPR);
4983
          /* Decrements may not appear in constant-expressions.  */
4984
          if (cp_parser_non_integral_constant_expression (parser,
4985
                                                          "a decrement"))
4986
            postfix_expression = error_mark_node;
4987
          idk = CP_ID_KIND_NONE;
4988
          is_member_access = false;
4989
          break;
4990
 
4991
        default:
4992
          if (pidk_return != NULL)
4993
            * pidk_return = idk;
4994
          if (member_access_only_p)
4995
            return is_member_access? postfix_expression : error_mark_node;
4996
          else
4997
            return postfix_expression;
4998
        }
4999
    }
5000
 
5001
  /* We should never get here.  */
5002
  gcc_unreachable ();
5003
  return error_mark_node;
5004
}
5005
 
5006
/* A subroutine of cp_parser_postfix_expression that also gets hijacked
5007
   by cp_parser_builtin_offsetof.  We're looking for
5008
 
5009
     postfix-expression [ expression ]
5010
 
5011
   FOR_OFFSETOF is set if we're being called in that context, which
5012
   changes how we deal with integer constant expressions.  */
5013
 
5014
static tree
5015
cp_parser_postfix_open_square_expression (cp_parser *parser,
5016
                                          tree postfix_expression,
5017
                                          bool for_offsetof)
5018
{
5019
  tree index;
5020
 
5021
  /* Consume the `[' token.  */
5022
  cp_lexer_consume_token (parser->lexer);
5023
 
5024
  /* Parse the index expression.  */
5025
  /* ??? For offsetof, there is a question of what to allow here.  If
5026
     offsetof is not being used in an integral constant expression context,
5027
     then we *could* get the right answer by computing the value at runtime.
5028
     If we are in an integral constant expression context, then we might
5029
     could accept any constant expression; hard to say without analysis.
5030
     Rather than open the barn door too wide right away, allow only integer
5031
     constant expressions here.  */
5032
  if (for_offsetof)
5033
    index = cp_parser_constant_expression (parser, false, NULL);
5034
  else
5035
    index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5036
 
5037
  /* Look for the closing `]'.  */
5038
  cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5039
 
5040
  /* Build the ARRAY_REF.  */
5041
  postfix_expression = grok_array_decl (postfix_expression, index);
5042
 
5043
  /* When not doing offsetof, array references are not permitted in
5044
     constant-expressions.  */
5045
  if (!for_offsetof
5046
      && (cp_parser_non_integral_constant_expression
5047
          (parser, "an array reference")))
5048
    postfix_expression = error_mark_node;
5049
 
5050
  return postfix_expression;
5051
}
5052
 
5053
/* A subroutine of cp_parser_postfix_expression that also gets hijacked
5054
   by cp_parser_builtin_offsetof.  We're looking for
5055
 
5056
     postfix-expression . template [opt] id-expression
5057
     postfix-expression . pseudo-destructor-name
5058
     postfix-expression -> template [opt] id-expression
5059
     postfix-expression -> pseudo-destructor-name
5060
 
5061
   FOR_OFFSETOF is set if we're being called in that context.  That sorta
5062
   limits what of the above we'll actually accept, but nevermind.
5063
   TOKEN_TYPE is the "." or "->" token, which will already have been
5064
   removed from the stream.  */
5065
 
5066
static tree
5067
cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5068
                                        enum cpp_ttype token_type,
5069
                                        tree postfix_expression,
5070
                                        bool for_offsetof, cp_id_kind *idk,
5071
                                        location_t location)
5072
{
5073
  tree name;
5074
  bool dependent_p;
5075
  bool pseudo_destructor_p;
5076
  tree scope = NULL_TREE;
5077
 
5078
  /* If this is a `->' operator, dereference the pointer.  */
5079
  if (token_type == CPP_DEREF)
5080
    postfix_expression = build_x_arrow (postfix_expression);
5081
  /* Check to see whether or not the expression is type-dependent.  */
5082
  dependent_p = type_dependent_expression_p (postfix_expression);
5083
  /* The identifier following the `->' or `.' is not qualified.  */
5084
  parser->scope = NULL_TREE;
5085
  parser->qualifying_scope = NULL_TREE;
5086
  parser->object_scope = NULL_TREE;
5087
  *idk = CP_ID_KIND_NONE;
5088
 
5089
  /* Enter the scope corresponding to the type of the object
5090
     given by the POSTFIX_EXPRESSION.  */
5091
  if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5092
    {
5093
      scope = TREE_TYPE (postfix_expression);
5094
      /* According to the standard, no expression should ever have
5095
         reference type.  Unfortunately, we do not currently match
5096
         the standard in this respect in that our internal representation
5097
         of an expression may have reference type even when the standard
5098
         says it does not.  Therefore, we have to manually obtain the
5099
         underlying type here.  */
5100
      scope = non_reference (scope);
5101
      /* The type of the POSTFIX_EXPRESSION must be complete.  */
5102
      if (scope == unknown_type_node)
5103
        {
5104
          error_at (location, "%qE does not have class type",
5105
                    postfix_expression);
5106
          scope = NULL_TREE;
5107
        }
5108
      else
5109
        scope = complete_type_or_else (scope, NULL_TREE);
5110
      /* Let the name lookup machinery know that we are processing a
5111
         class member access expression.  */
5112
      parser->context->object_type = scope;
5113
      /* If something went wrong, we want to be able to discern that case,
5114
         as opposed to the case where there was no SCOPE due to the type
5115
         of expression being dependent.  */
5116
      if (!scope)
5117
        scope = error_mark_node;
5118
      /* If the SCOPE was erroneous, make the various semantic analysis
5119
         functions exit quickly -- and without issuing additional error
5120
         messages.  */
5121
      if (scope == error_mark_node)
5122
        postfix_expression = error_mark_node;
5123
    }
5124
 
5125
  /* Assume this expression is not a pseudo-destructor access.  */
5126
  pseudo_destructor_p = false;
5127
 
5128
  /* If the SCOPE is a scalar type, then, if this is a valid program,
5129
     we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5130
     is type dependent, it can be pseudo-destructor-name or something else.
5131
     Try to parse it as pseudo-destructor-name first.  */
5132
  if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5133
    {
5134
      tree s;
5135
      tree type;
5136
 
5137
      cp_parser_parse_tentatively (parser);
5138
      /* Parse the pseudo-destructor-name.  */
5139
      s = NULL_TREE;
5140
      cp_parser_pseudo_destructor_name (parser, &s, &type);
5141
      if (dependent_p
5142
          && (cp_parser_error_occurred (parser)
5143
              || TREE_CODE (type) != TYPE_DECL
5144
              || !SCALAR_TYPE_P (TREE_TYPE (type))))
5145
        cp_parser_abort_tentative_parse (parser);
5146
      else if (cp_parser_parse_definitely (parser))
5147
        {
5148
          pseudo_destructor_p = true;
5149
          postfix_expression
5150
            = finish_pseudo_destructor_expr (postfix_expression,
5151
                                             s, TREE_TYPE (type));
5152
        }
5153
    }
5154
 
5155
  if (!pseudo_destructor_p)
5156
    {
5157
      /* If the SCOPE is not a scalar type, we are looking at an
5158
         ordinary class member access expression, rather than a
5159
         pseudo-destructor-name.  */
5160
      bool template_p;
5161
      cp_token *token = cp_lexer_peek_token (parser->lexer);
5162
      /* Parse the id-expression.  */
5163
      name = (cp_parser_id_expression
5164
              (parser,
5165
               cp_parser_optional_template_keyword (parser),
5166
               /*check_dependency_p=*/true,
5167
               &template_p,
5168
               /*declarator_p=*/false,
5169
               /*optional_p=*/false));
5170
      /* In general, build a SCOPE_REF if the member name is qualified.
5171
         However, if the name was not dependent and has already been
5172
         resolved; there is no need to build the SCOPE_REF.  For example;
5173
 
5174
             struct X { void f(); };
5175
             template <typename T> void f(T* t) { t->X::f(); }
5176
 
5177
         Even though "t" is dependent, "X::f" is not and has been resolved
5178
         to a BASELINK; there is no need to include scope information.  */
5179
 
5180
      /* But we do need to remember that there was an explicit scope for
5181
         virtual function calls.  */
5182
      if (parser->scope)
5183
        *idk = CP_ID_KIND_QUALIFIED;
5184
 
5185
      /* If the name is a template-id that names a type, we will get a
5186
         TYPE_DECL here.  That is invalid code.  */
5187
      if (TREE_CODE (name) == TYPE_DECL)
5188
        {
5189
          error_at (token->location, "invalid use of %qD", name);
5190
          postfix_expression = error_mark_node;
5191
        }
5192
      else
5193
        {
5194
          if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5195
            {
5196
              name = build_qualified_name (/*type=*/NULL_TREE,
5197
                                           parser->scope,
5198
                                           name,
5199
                                           template_p);
5200
              parser->scope = NULL_TREE;
5201
              parser->qualifying_scope = NULL_TREE;
5202
              parser->object_scope = NULL_TREE;
5203
            }
5204
          if (scope && name && BASELINK_P (name))
5205
            adjust_result_of_qualified_name_lookup
5206
              (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5207
          postfix_expression
5208
            = finish_class_member_access_expr (postfix_expression, name,
5209
                                               template_p,
5210
                                               tf_warning_or_error);
5211
        }
5212
    }
5213
 
5214
  /* We no longer need to look up names in the scope of the object on
5215
     the left-hand side of the `.' or `->' operator.  */
5216
  parser->context->object_type = NULL_TREE;
5217
 
5218
  /* Outside of offsetof, these operators may not appear in
5219
     constant-expressions.  */
5220
  if (!for_offsetof
5221
      && (cp_parser_non_integral_constant_expression
5222
          (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5223
    postfix_expression = error_mark_node;
5224
 
5225
  return postfix_expression;
5226
}
5227
 
5228
/* Parse a parenthesized expression-list.
5229
 
5230
   expression-list:
5231
     assignment-expression
5232
     expression-list, assignment-expression
5233
 
5234
   attribute-list:
5235
     expression-list
5236
     identifier
5237
     identifier, expression-list
5238
 
5239
   CAST_P is true if this expression is the target of a cast.
5240
 
5241
   ALLOW_EXPANSION_P is true if this expression allows expansion of an
5242
   argument pack.
5243
 
5244
   Returns a vector of trees.  Each element is a representation of an
5245
   assignment-expression.  NULL is returned if the ( and or ) are
5246
   missing.  An empty, but allocated, vector is returned on no
5247
   expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is true
5248
   if this is really an attribute list being parsed.  If
5249
   NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5250
   not all of the expressions in the list were constant.  */
5251
 
5252
static VEC(tree,gc) *
5253
cp_parser_parenthesized_expression_list (cp_parser* parser,
5254
                                         bool is_attribute_list,
5255
                                         bool cast_p,
5256
                                         bool allow_expansion_p,
5257
                                         bool *non_constant_p)
5258
{
5259
  VEC(tree,gc) *expression_list;
5260
  bool fold_expr_p = is_attribute_list;
5261
  tree identifier = NULL_TREE;
5262
  bool saved_greater_than_is_operator_p;
5263
 
5264
  /* Assume all the expressions will be constant.  */
5265
  if (non_constant_p)
5266
    *non_constant_p = false;
5267
 
5268
  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5269
    return NULL;
5270
 
5271
  expression_list = make_tree_vector ();
5272
 
5273
  /* Within a parenthesized expression, a `>' token is always
5274
     the greater-than operator.  */
5275
  saved_greater_than_is_operator_p
5276
    = parser->greater_than_is_operator_p;
5277
  parser->greater_than_is_operator_p = true;
5278
 
5279
  /* Consume expressions until there are no more.  */
5280
  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5281
    while (true)
5282
      {
5283
        tree expr;
5284
 
5285
        /* At the beginning of attribute lists, check to see if the
5286
           next token is an identifier.  */
5287
        if (is_attribute_list
5288
            && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5289
          {
5290
            cp_token *token;
5291
 
5292
            /* Consume the identifier.  */
5293
            token = cp_lexer_consume_token (parser->lexer);
5294
            /* Save the identifier.  */
5295
            identifier = token->u.value;
5296
          }
5297
        else
5298
          {
5299
            bool expr_non_constant_p;
5300
 
5301
            /* Parse the next assignment-expression.  */
5302
            if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5303
              {
5304
                /* A braced-init-list.  */
5305
                maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5306
                expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5307
                if (non_constant_p && expr_non_constant_p)
5308
                  *non_constant_p = true;
5309
              }
5310
            else if (non_constant_p)
5311
              {
5312
                expr = (cp_parser_constant_expression
5313
                        (parser, /*allow_non_constant_p=*/true,
5314
                         &expr_non_constant_p));
5315
                if (expr_non_constant_p)
5316
                  *non_constant_p = true;
5317
              }
5318
            else
5319
              expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5320
 
5321
            if (fold_expr_p)
5322
              expr = fold_non_dependent_expr (expr);
5323
 
5324
            /* If we have an ellipsis, then this is an expression
5325
               expansion.  */
5326
            if (allow_expansion_p
5327
                && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5328
              {
5329
                /* Consume the `...'.  */
5330
                cp_lexer_consume_token (parser->lexer);
5331
 
5332
                /* Build the argument pack.  */
5333
                expr = make_pack_expansion (expr);
5334
              }
5335
 
5336
             /* Add it to the list.  We add error_mark_node
5337
                expressions to the list, so that we can still tell if
5338
                the correct form for a parenthesized expression-list
5339
                is found. That gives better errors.  */
5340
            VEC_safe_push (tree, gc, expression_list, expr);
5341
 
5342
            if (expr == error_mark_node)
5343
              goto skip_comma;
5344
          }
5345
 
5346
        /* After the first item, attribute lists look the same as
5347
           expression lists.  */
5348
        is_attribute_list = false;
5349
 
5350
      get_comma:;
5351
        /* If the next token isn't a `,', then we are done.  */
5352
        if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5353
          break;
5354
 
5355
        /* Otherwise, consume the `,' and keep going.  */
5356
        cp_lexer_consume_token (parser->lexer);
5357
      }
5358
 
5359
  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5360
    {
5361
      int ending;
5362
 
5363
    skip_comma:;
5364
      /* We try and resync to an unnested comma, as that will give the
5365
         user better diagnostics.  */
5366
      ending = cp_parser_skip_to_closing_parenthesis (parser,
5367
                                                      /*recovering=*/true,
5368
                                                      /*or_comma=*/true,
5369
                                                      /*consume_paren=*/true);
5370
      if (ending < 0)
5371
        goto get_comma;
5372
      if (!ending)
5373
        {
5374
          parser->greater_than_is_operator_p
5375
            = saved_greater_than_is_operator_p;
5376
          return NULL;
5377
        }
5378
    }
5379
 
5380
  parser->greater_than_is_operator_p
5381
    = saved_greater_than_is_operator_p;
5382
 
5383
  if (identifier)
5384
    VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5385
 
5386
  return expression_list;
5387
}
5388
 
5389
/* Parse a pseudo-destructor-name.
5390
 
5391
   pseudo-destructor-name:
5392
     :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5393
     :: [opt] nested-name-specifier template template-id :: ~ type-name
5394
     :: [opt] nested-name-specifier [opt] ~ type-name
5395
 
5396
   If either of the first two productions is used, sets *SCOPE to the
5397
   TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5398
   NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5399
   or ERROR_MARK_NODE if the parse fails.  */
5400
 
5401
static void
5402
cp_parser_pseudo_destructor_name (cp_parser* parser,
5403
                                  tree* scope,
5404
                                  tree* type)
5405
{
5406
  bool nested_name_specifier_p;
5407
 
5408
  /* Assume that things will not work out.  */
5409
  *type = error_mark_node;
5410
 
5411
  /* Look for the optional `::' operator.  */
5412
  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5413
  /* Look for the optional nested-name-specifier.  */
5414
  nested_name_specifier_p
5415
    = (cp_parser_nested_name_specifier_opt (parser,
5416
                                            /*typename_keyword_p=*/false,
5417
                                            /*check_dependency_p=*/true,
5418
                                            /*type_p=*/false,
5419
                                            /*is_declaration=*/false)
5420
       != NULL_TREE);
5421
  /* Now, if we saw a nested-name-specifier, we might be doing the
5422
     second production.  */
5423
  if (nested_name_specifier_p
5424
      && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5425
    {
5426
      /* Consume the `template' keyword.  */
5427
      cp_lexer_consume_token (parser->lexer);
5428
      /* Parse the template-id.  */
5429
      cp_parser_template_id (parser,
5430
                             /*template_keyword_p=*/true,
5431
                             /*check_dependency_p=*/false,
5432
                             /*is_declaration=*/true);
5433
      /* Look for the `::' token.  */
5434
      cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5435
    }
5436
  /* If the next token is not a `~', then there might be some
5437
     additional qualification.  */
5438
  else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5439
    {
5440
      /* At this point, we're looking for "type-name :: ~".  The type-name
5441
         must not be a class-name, since this is a pseudo-destructor.  So,
5442
         it must be either an enum-name, or a typedef-name -- both of which
5443
         are just identifiers.  So, we peek ahead to check that the "::"
5444
         and "~" tokens are present; if they are not, then we can avoid
5445
         calling type_name.  */
5446
      if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5447
          || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5448
          || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5449
        {
5450
          cp_parser_error (parser, "non-scalar type");
5451
          return;
5452
        }
5453
 
5454
      /* Look for the type-name.  */
5455
      *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5456
      if (*scope == error_mark_node)
5457
        return;
5458
 
5459
      /* Look for the `::' token.  */
5460
      cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5461
    }
5462
  else
5463
    *scope = NULL_TREE;
5464
 
5465
  /* Look for the `~'.  */
5466
  cp_parser_require (parser, CPP_COMPL, "%<~%>");
5467
  /* Look for the type-name again.  We are not responsible for
5468
     checking that it matches the first type-name.  */
5469
  *type = cp_parser_nonclass_name (parser);
5470
}
5471
 
5472
/* Parse a unary-expression.
5473
 
5474
   unary-expression:
5475
     postfix-expression
5476
     ++ cast-expression
5477
     -- cast-expression
5478
     unary-operator cast-expression
5479
     sizeof unary-expression
5480
     sizeof ( type-id )
5481
     new-expression
5482
     delete-expression
5483
 
5484
   GNU Extensions:
5485
 
5486
   unary-expression:
5487
     __extension__ cast-expression
5488
     __alignof__ unary-expression
5489
     __alignof__ ( type-id )
5490
     __real__ cast-expression
5491
     __imag__ cast-expression
5492
     && identifier
5493
 
5494
   ADDRESS_P is true iff the unary-expression is appearing as the
5495
   operand of the `&' operator.   CAST_P is true if this expression is
5496
   the target of a cast.
5497
 
5498
   Returns a representation of the expression.  */
5499
 
5500
static tree
5501
cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5502
                            cp_id_kind * pidk)
5503
{
5504
  cp_token *token;
5505
  enum tree_code unary_operator;
5506
 
5507
  /* Peek at the next token.  */
5508
  token = cp_lexer_peek_token (parser->lexer);
5509
  /* Some keywords give away the kind of expression.  */
5510
  if (token->type == CPP_KEYWORD)
5511
    {
5512
      enum rid keyword = token->keyword;
5513
 
5514
      switch (keyword)
5515
        {
5516
        case RID_ALIGNOF:
5517
        case RID_SIZEOF:
5518
          {
5519
            tree operand;
5520
            enum tree_code op;
5521
 
5522
            op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5523
            /* Consume the token.  */
5524
            cp_lexer_consume_token (parser->lexer);
5525
            /* Parse the operand.  */
5526
            operand = cp_parser_sizeof_operand (parser, keyword);
5527
 
5528
            if (TYPE_P (operand))
5529
              return cxx_sizeof_or_alignof_type (operand, op, true);
5530
            else
5531
              return cxx_sizeof_or_alignof_expr (operand, op, true);
5532
          }
5533
 
5534
        case RID_NEW:
5535
          return cp_parser_new_expression (parser);
5536
 
5537
        case RID_DELETE:
5538
          return cp_parser_delete_expression (parser);
5539
 
5540
        case RID_EXTENSION:
5541
          {
5542
            /* The saved value of the PEDANTIC flag.  */
5543
            int saved_pedantic;
5544
            tree expr;
5545
 
5546
            /* Save away the PEDANTIC flag.  */
5547
            cp_parser_extension_opt (parser, &saved_pedantic);
5548
            /* Parse the cast-expression.  */
5549
            expr = cp_parser_simple_cast_expression (parser);
5550
            /* Restore the PEDANTIC flag.  */
5551
            pedantic = saved_pedantic;
5552
 
5553
            return expr;
5554
          }
5555
 
5556
        case RID_REALPART:
5557
        case RID_IMAGPART:
5558
          {
5559
            tree expression;
5560
 
5561
            /* Consume the `__real__' or `__imag__' token.  */
5562
            cp_lexer_consume_token (parser->lexer);
5563
            /* Parse the cast-expression.  */
5564
            expression = cp_parser_simple_cast_expression (parser);
5565
            /* Create the complete representation.  */
5566
            return build_x_unary_op ((keyword == RID_REALPART
5567
                                      ? REALPART_EXPR : IMAGPART_EXPR),
5568
                                     expression,
5569
                                     tf_warning_or_error);
5570
          }
5571
          break;
5572
 
5573
        default:
5574
          break;
5575
        }
5576
    }
5577
 
5578
  /* Look for the `:: new' and `:: delete', which also signal the
5579
     beginning of a new-expression, or delete-expression,
5580
     respectively.  If the next token is `::', then it might be one of
5581
     these.  */
5582
  if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5583
    {
5584
      enum rid keyword;
5585
 
5586
      /* See if the token after the `::' is one of the keywords in
5587
         which we're interested.  */
5588
      keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5589
      /* If it's `new', we have a new-expression.  */
5590
      if (keyword == RID_NEW)
5591
        return cp_parser_new_expression (parser);
5592
      /* Similarly, for `delete'.  */
5593
      else if (keyword == RID_DELETE)
5594
        return cp_parser_delete_expression (parser);
5595
    }
5596
 
5597
  /* Look for a unary operator.  */
5598
  unary_operator = cp_parser_unary_operator (token);
5599
  /* The `++' and `--' operators can be handled similarly, even though
5600
     they are not technically unary-operators in the grammar.  */
5601
  if (unary_operator == ERROR_MARK)
5602
    {
5603
      if (token->type == CPP_PLUS_PLUS)
5604
        unary_operator = PREINCREMENT_EXPR;
5605
      else if (token->type == CPP_MINUS_MINUS)
5606
        unary_operator = PREDECREMENT_EXPR;
5607
      /* Handle the GNU address-of-label extension.  */
5608
      else if (cp_parser_allow_gnu_extensions_p (parser)
5609
               && token->type == CPP_AND_AND)
5610
        {
5611
          tree identifier;
5612
          tree expression;
5613
          location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5614
 
5615
          /* Consume the '&&' token.  */
5616
          cp_lexer_consume_token (parser->lexer);
5617
          /* Look for the identifier.  */
5618
          identifier = cp_parser_identifier (parser);
5619
          /* Create an expression representing the address.  */
5620
          expression = finish_label_address_expr (identifier, loc);
5621
          if (cp_parser_non_integral_constant_expression (parser,
5622
                                                "the address of a label"))
5623
            expression = error_mark_node;
5624
          return expression;
5625
        }
5626
    }
5627
  if (unary_operator != ERROR_MARK)
5628
    {
5629
      tree cast_expression;
5630
      tree expression = error_mark_node;
5631
      const char *non_constant_p = NULL;
5632
 
5633
      /* Consume the operator token.  */
5634
      token = cp_lexer_consume_token (parser->lexer);
5635
      /* Parse the cast-expression.  */
5636
      cast_expression
5637
        = cp_parser_cast_expression (parser,
5638
                                     unary_operator == ADDR_EXPR,
5639
                                     /*cast_p=*/false, pidk);
5640
      /* Now, build an appropriate representation.  */
5641
      switch (unary_operator)
5642
        {
5643
        case INDIRECT_REF:
5644
          non_constant_p = "%<*%>";
5645
          expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5646
                                             tf_warning_or_error);
5647
          break;
5648
 
5649
        case ADDR_EXPR:
5650
          non_constant_p = "%<&%>";
5651
          /* Fall through.  */
5652
        case BIT_NOT_EXPR:
5653
          expression = build_x_unary_op (unary_operator, cast_expression,
5654
                                         tf_warning_or_error);
5655
          break;
5656
 
5657
        case PREINCREMENT_EXPR:
5658
        case PREDECREMENT_EXPR:
5659
          non_constant_p = (unary_operator == PREINCREMENT_EXPR
5660
                            ? "%<++%>" : "%<--%>");
5661
          /* Fall through.  */
5662
        case UNARY_PLUS_EXPR:
5663
        case NEGATE_EXPR:
5664
        case TRUTH_NOT_EXPR:
5665
          expression = finish_unary_op_expr (unary_operator, cast_expression);
5666
          break;
5667
 
5668
        default:
5669
          gcc_unreachable ();
5670
        }
5671
 
5672
      if (non_constant_p
5673
          && cp_parser_non_integral_constant_expression (parser,
5674
                                                         non_constant_p))
5675
        expression = error_mark_node;
5676
 
5677
      return expression;
5678
    }
5679
 
5680
  return cp_parser_postfix_expression (parser, address_p, cast_p,
5681
                                       /*member_access_only_p=*/false,
5682
                                       pidk);
5683
}
5684
 
5685
/* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5686
   unary-operator, the corresponding tree code is returned.  */
5687
 
5688
static enum tree_code
5689
cp_parser_unary_operator (cp_token* token)
5690
{
5691
  switch (token->type)
5692
    {
5693
    case CPP_MULT:
5694
      return INDIRECT_REF;
5695
 
5696
    case CPP_AND:
5697
      return ADDR_EXPR;
5698
 
5699
    case CPP_PLUS:
5700
      return UNARY_PLUS_EXPR;
5701
 
5702
    case CPP_MINUS:
5703
      return NEGATE_EXPR;
5704
 
5705
    case CPP_NOT:
5706
      return TRUTH_NOT_EXPR;
5707
 
5708
    case CPP_COMPL:
5709
      return BIT_NOT_EXPR;
5710
 
5711
    default:
5712
      return ERROR_MARK;
5713
    }
5714
}
5715
 
5716
/* Parse a new-expression.
5717
 
5718
   new-expression:
5719
     :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5720
     :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5721
 
5722
   Returns a representation of the expression.  */
5723
 
5724
static tree
5725
cp_parser_new_expression (cp_parser* parser)
5726
{
5727
  bool global_scope_p;
5728
  VEC(tree,gc) *placement;
5729
  tree type;
5730
  VEC(tree,gc) *initializer;
5731
  tree nelts;
5732
  tree ret;
5733
 
5734
  /* Look for the optional `::' operator.  */
5735
  global_scope_p
5736
    = (cp_parser_global_scope_opt (parser,
5737
                                   /*current_scope_valid_p=*/false)
5738
       != NULL_TREE);
5739
  /* Look for the `new' operator.  */
5740
  cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5741
  /* There's no easy way to tell a new-placement from the
5742
     `( type-id )' construct.  */
5743
  cp_parser_parse_tentatively (parser);
5744
  /* Look for a new-placement.  */
5745
  placement = cp_parser_new_placement (parser);
5746
  /* If that didn't work out, there's no new-placement.  */
5747
  if (!cp_parser_parse_definitely (parser))
5748
    {
5749
      if (placement != NULL)
5750
        release_tree_vector (placement);
5751
      placement = NULL;
5752
    }
5753
 
5754
  /* If the next token is a `(', then we have a parenthesized
5755
     type-id.  */
5756
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5757
    {
5758
      cp_token *token;
5759
      /* Consume the `('.  */
5760
      cp_lexer_consume_token (parser->lexer);
5761
      /* Parse the type-id.  */
5762
      type = cp_parser_type_id (parser);
5763
      /* Look for the closing `)'.  */
5764
      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5765
      token = cp_lexer_peek_token (parser->lexer);
5766
      /* There should not be a direct-new-declarator in this production,
5767
         but GCC used to allowed this, so we check and emit a sensible error
5768
         message for this case.  */
5769
      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5770
        {
5771
          error_at (token->location,
5772
                    "array bound forbidden after parenthesized type-id");
5773
          inform (token->location,
5774
                  "try removing the parentheses around the type-id");
5775
          cp_parser_direct_new_declarator (parser);
5776
        }
5777
      nelts = NULL_TREE;
5778
    }
5779
  /* Otherwise, there must be a new-type-id.  */
5780
  else
5781
    type = cp_parser_new_type_id (parser, &nelts);
5782
 
5783
  /* If the next token is a `(' or '{', then we have a new-initializer.  */
5784
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5785
      || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5786
    initializer = cp_parser_new_initializer (parser);
5787
  else
5788
    initializer = NULL;
5789
 
5790
  /* A new-expression may not appear in an integral constant
5791
     expression.  */
5792
  if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5793
    ret = error_mark_node;
5794
  else
5795
    {
5796
      /* Create a representation of the new-expression.  */
5797
      ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5798
                       tf_warning_or_error);
5799
    }
5800
 
5801
  if (placement != NULL)
5802
    release_tree_vector (placement);
5803
  if (initializer != NULL)
5804
    release_tree_vector (initializer);
5805
 
5806
  return ret;
5807
}
5808
 
5809
/* Parse a new-placement.
5810
 
5811
   new-placement:
5812
     ( expression-list )
5813
 
5814
   Returns the same representation as for an expression-list.  */
5815
 
5816
static VEC(tree,gc) *
5817
cp_parser_new_placement (cp_parser* parser)
5818
{
5819
  VEC(tree,gc) *expression_list;
5820
 
5821
  /* Parse the expression-list.  */
5822
  expression_list = (cp_parser_parenthesized_expression_list
5823
                     (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5824
                      /*non_constant_p=*/NULL));
5825
 
5826
  return expression_list;
5827
}
5828
 
5829
/* Parse a new-type-id.
5830
 
5831
   new-type-id:
5832
     type-specifier-seq new-declarator [opt]
5833
 
5834
   Returns the TYPE allocated.  If the new-type-id indicates an array
5835
   type, *NELTS is set to the number of elements in the last array
5836
   bound; the TYPE will not include the last array bound.  */
5837
 
5838
static tree
5839
cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5840
{
5841
  cp_decl_specifier_seq type_specifier_seq;
5842
  cp_declarator *new_declarator;
5843
  cp_declarator *declarator;
5844
  cp_declarator *outer_declarator;
5845
  const char *saved_message;
5846
  tree type;
5847
 
5848
  /* The type-specifier sequence must not contain type definitions.
5849
     (It cannot contain declarations of new types either, but if they
5850
     are not definitions we will catch that because they are not
5851
     complete.)  */
5852
  saved_message = parser->type_definition_forbidden_message;
5853
  parser->type_definition_forbidden_message
5854
    = G_("types may not be defined in a new-type-id");
5855
  /* Parse the type-specifier-seq.  */
5856
  cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5857
                                /*is_trailing_return=*/false,
5858
                                &type_specifier_seq);
5859
  /* Restore the old message.  */
5860
  parser->type_definition_forbidden_message = saved_message;
5861
  /* Parse the new-declarator.  */
5862
  new_declarator = cp_parser_new_declarator_opt (parser);
5863
 
5864
  /* Determine the number of elements in the last array dimension, if
5865
     any.  */
5866
  *nelts = NULL_TREE;
5867
  /* Skip down to the last array dimension.  */
5868
  declarator = new_declarator;
5869
  outer_declarator = NULL;
5870
  while (declarator && (declarator->kind == cdk_pointer
5871
                        || declarator->kind == cdk_ptrmem))
5872
    {
5873
      outer_declarator = declarator;
5874
      declarator = declarator->declarator;
5875
    }
5876
  while (declarator
5877
         && declarator->kind == cdk_array
5878
         && declarator->declarator
5879
         && declarator->declarator->kind == cdk_array)
5880
    {
5881
      outer_declarator = declarator;
5882
      declarator = declarator->declarator;
5883
    }
5884
 
5885
  if (declarator && declarator->kind == cdk_array)
5886
    {
5887
      *nelts = declarator->u.array.bounds;
5888
      if (*nelts == error_mark_node)
5889
        *nelts = integer_one_node;
5890
 
5891
      if (outer_declarator)
5892
        outer_declarator->declarator = declarator->declarator;
5893
      else
5894
        new_declarator = NULL;
5895
    }
5896
 
5897
  type = groktypename (&type_specifier_seq, new_declarator, false);
5898
  return type;
5899
}
5900
 
5901
/* Parse an (optional) new-declarator.
5902
 
5903
   new-declarator:
5904
     ptr-operator new-declarator [opt]
5905
     direct-new-declarator
5906
 
5907
   Returns the declarator.  */
5908
 
5909
static cp_declarator *
5910
cp_parser_new_declarator_opt (cp_parser* parser)
5911
{
5912
  enum tree_code code;
5913
  tree type;
5914
  cp_cv_quals cv_quals;
5915
 
5916
  /* We don't know if there's a ptr-operator next, or not.  */
5917
  cp_parser_parse_tentatively (parser);
5918
  /* Look for a ptr-operator.  */
5919
  code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5920
  /* If that worked, look for more new-declarators.  */
5921
  if (cp_parser_parse_definitely (parser))
5922
    {
5923
      cp_declarator *declarator;
5924
 
5925
      /* Parse another optional declarator.  */
5926
      declarator = cp_parser_new_declarator_opt (parser);
5927
 
5928
      return cp_parser_make_indirect_declarator
5929
        (code, type, cv_quals, declarator);
5930
    }
5931
 
5932
  /* If the next token is a `[', there is a direct-new-declarator.  */
5933
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5934
    return cp_parser_direct_new_declarator (parser);
5935
 
5936
  return NULL;
5937
}
5938
 
5939
/* Parse a direct-new-declarator.
5940
 
5941
   direct-new-declarator:
5942
     [ expression ]
5943
     direct-new-declarator [constant-expression]
5944
 
5945
   */
5946
 
5947
static cp_declarator *
5948
cp_parser_direct_new_declarator (cp_parser* parser)
5949
{
5950
  cp_declarator *declarator = NULL;
5951
 
5952
  while (true)
5953
    {
5954
      tree expression;
5955
 
5956
      /* Look for the opening `['.  */
5957
      cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5958
      /* The first expression is not required to be constant.  */
5959
      if (!declarator)
5960
        {
5961
          cp_token *token = cp_lexer_peek_token (parser->lexer);
5962
          expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5963
          /* The standard requires that the expression have integral
5964
             type.  DR 74 adds enumeration types.  We believe that the
5965
             real intent is that these expressions be handled like the
5966
             expression in a `switch' condition, which also allows
5967
             classes with a single conversion to integral or
5968
             enumeration type.  */
5969
          if (!processing_template_decl)
5970
            {
5971
              expression
5972
                = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5973
                                              expression,
5974
                                              /*complain=*/true);
5975
              if (!expression)
5976
                {
5977
                  error_at (token->location,
5978
                            "expression in new-declarator must have integral "
5979
                            "or enumeration type");
5980
                  expression = error_mark_node;
5981
                }
5982
            }
5983
        }
5984
      /* But all the other expressions must be.  */
5985
      else
5986
        expression
5987
          = cp_parser_constant_expression (parser,
5988
                                           /*allow_non_constant=*/false,
5989
                                           NULL);
5990
      /* Look for the closing `]'.  */
5991
      cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5992
 
5993
      /* Add this bound to the declarator.  */
5994
      declarator = make_array_declarator (declarator, expression);
5995
 
5996
      /* If the next token is not a `[', then there are no more
5997
         bounds.  */
5998
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5999
        break;
6000
    }
6001
 
6002
  return declarator;
6003
}
6004
 
6005
/* Parse a new-initializer.
6006
 
6007
   new-initializer:
6008
     ( expression-list [opt] )
6009
     braced-init-list
6010
 
6011
   Returns a representation of the expression-list.  */
6012
 
6013
static VEC(tree,gc) *
6014
cp_parser_new_initializer (cp_parser* parser)
6015
{
6016
  VEC(tree,gc) *expression_list;
6017
 
6018
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6019
    {
6020
      tree t;
6021
      bool expr_non_constant_p;
6022
      maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6023
      t = cp_parser_braced_list (parser, &expr_non_constant_p);
6024
      CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6025
      expression_list = make_tree_vector_single (t);
6026
    }
6027
  else
6028
    expression_list = (cp_parser_parenthesized_expression_list
6029
                       (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
6030
                        /*non_constant_p=*/NULL));
6031
 
6032
  return expression_list;
6033
}
6034
 
6035
/* Parse a delete-expression.
6036
 
6037
   delete-expression:
6038
     :: [opt] delete cast-expression
6039
     :: [opt] delete [ ] cast-expression
6040
 
6041
   Returns a representation of the expression.  */
6042
 
6043
static tree
6044
cp_parser_delete_expression (cp_parser* parser)
6045
{
6046
  bool global_scope_p;
6047
  bool array_p;
6048
  tree expression;
6049
 
6050
  /* Look for the optional `::' operator.  */
6051
  global_scope_p
6052
    = (cp_parser_global_scope_opt (parser,
6053
                                   /*current_scope_valid_p=*/false)
6054
       != NULL_TREE);
6055
  /* Look for the `delete' keyword.  */
6056
  cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6057
  /* See if the array syntax is in use.  */
6058
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6059
    {
6060
      /* Consume the `[' token.  */
6061
      cp_lexer_consume_token (parser->lexer);
6062
      /* Look for the `]' token.  */
6063
      cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6064
      /* Remember that this is the `[]' construct.  */
6065
      array_p = true;
6066
    }
6067
  else
6068
    array_p = false;
6069
 
6070
  /* Parse the cast-expression.  */
6071
  expression = cp_parser_simple_cast_expression (parser);
6072
 
6073
  /* A delete-expression may not appear in an integral constant
6074
     expression.  */
6075
  if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6076
    return error_mark_node;
6077
 
6078
  return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6079
}
6080
 
6081
/* Returns true if TOKEN may start a cast-expression and false
6082
   otherwise.  */
6083
 
6084
static bool
6085
cp_parser_token_starts_cast_expression (cp_token *token)
6086
{
6087
  switch (token->type)
6088
    {
6089
    case CPP_COMMA:
6090
    case CPP_SEMICOLON:
6091
    case CPP_QUERY:
6092
    case CPP_COLON:
6093
    case CPP_CLOSE_SQUARE:
6094
    case CPP_CLOSE_PAREN:
6095
    case CPP_CLOSE_BRACE:
6096
    case CPP_DOT:
6097
    case CPP_DOT_STAR:
6098
    case CPP_DEREF:
6099
    case CPP_DEREF_STAR:
6100
    case CPP_DIV:
6101
    case CPP_MOD:
6102
    case CPP_LSHIFT:
6103
    case CPP_RSHIFT:
6104
    case CPP_LESS:
6105
    case CPP_GREATER:
6106
    case CPP_LESS_EQ:
6107
    case CPP_GREATER_EQ:
6108
    case CPP_EQ_EQ:
6109
    case CPP_NOT_EQ:
6110
    case CPP_EQ:
6111
    case CPP_MULT_EQ:
6112
    case CPP_DIV_EQ:
6113
    case CPP_MOD_EQ:
6114
    case CPP_PLUS_EQ:
6115
    case CPP_MINUS_EQ:
6116
    case CPP_RSHIFT_EQ:
6117
    case CPP_LSHIFT_EQ:
6118
    case CPP_AND_EQ:
6119
    case CPP_XOR_EQ:
6120
    case CPP_OR_EQ:
6121
    case CPP_XOR:
6122
    case CPP_OR:
6123
    case CPP_OR_OR:
6124
    case CPP_EOF:
6125
      return false;
6126
 
6127
      /* '[' may start a primary-expression in obj-c++.  */
6128
    case CPP_OPEN_SQUARE:
6129
      return c_dialect_objc ();
6130
 
6131
    default:
6132
      return true;
6133
    }
6134
}
6135
 
6136
/* Parse a cast-expression.
6137
 
6138
   cast-expression:
6139
     unary-expression
6140
     ( type-id ) cast-expression
6141
 
6142
   ADDRESS_P is true iff the unary-expression is appearing as the
6143
   operand of the `&' operator.   CAST_P is true if this expression is
6144
   the target of a cast.
6145
 
6146
   Returns a representation of the expression.  */
6147
 
6148
static tree
6149
cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6150
                           cp_id_kind * pidk)
6151
{
6152
  /* If it's a `(', then we might be looking at a cast.  */
6153
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6154
    {
6155
      tree type = NULL_TREE;
6156
      tree expr = NULL_TREE;
6157
      bool compound_literal_p;
6158
      const char *saved_message;
6159
 
6160
      /* There's no way to know yet whether or not this is a cast.
6161
         For example, `(int (3))' is a unary-expression, while `(int)
6162
         3' is a cast.  So, we resort to parsing tentatively.  */
6163
      cp_parser_parse_tentatively (parser);
6164
      /* Types may not be defined in a cast.  */
6165
      saved_message = parser->type_definition_forbidden_message;
6166
      parser->type_definition_forbidden_message
6167
        = G_("types may not be defined in casts");
6168
      /* Consume the `('.  */
6169
      cp_lexer_consume_token (parser->lexer);
6170
      /* A very tricky bit is that `(struct S) { 3 }' is a
6171
         compound-literal (which we permit in C++ as an extension).
6172
         But, that construct is not a cast-expression -- it is a
6173
         postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6174
         is legal; if the compound-literal were a cast-expression,
6175
         you'd need an extra set of parentheses.)  But, if we parse
6176
         the type-id, and it happens to be a class-specifier, then we
6177
         will commit to the parse at that point, because we cannot
6178
         undo the action that is done when creating a new class.  So,
6179
         then we cannot back up and do a postfix-expression.
6180
 
6181
         Therefore, we scan ahead to the closing `)', and check to see
6182
         if the token after the `)' is a `{'.  If so, we are not
6183
         looking at a cast-expression.
6184
 
6185
         Save tokens so that we can put them back.  */
6186
      cp_lexer_save_tokens (parser->lexer);
6187
      /* Skip tokens until the next token is a closing parenthesis.
6188
         If we find the closing `)', and the next token is a `{', then
6189
         we are looking at a compound-literal.  */
6190
      compound_literal_p
6191
        = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6192
                                                  /*consume_paren=*/true)
6193
           && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6194
      /* Roll back the tokens we skipped.  */
6195
      cp_lexer_rollback_tokens (parser->lexer);
6196
      /* If we were looking at a compound-literal, simulate an error
6197
         so that the call to cp_parser_parse_definitely below will
6198
         fail.  */
6199
      if (compound_literal_p)
6200
        cp_parser_simulate_error (parser);
6201
      else
6202
        {
6203
          bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6204
          parser->in_type_id_in_expr_p = true;
6205
          /* Look for the type-id.  */
6206
          type = cp_parser_type_id (parser);
6207
          /* Look for the closing `)'.  */
6208
          cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6209
          parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6210
        }
6211
 
6212
      /* Restore the saved message.  */
6213
      parser->type_definition_forbidden_message = saved_message;
6214
 
6215
      /* At this point this can only be either a cast or a
6216
         parenthesized ctor such as `(T ())' that looks like a cast to
6217
         function returning T.  */
6218
      if (!cp_parser_error_occurred (parser)
6219
          && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6220
                                                     (parser->lexer)))
6221
        {
6222
          cp_parser_parse_definitely (parser);
6223
          expr = cp_parser_cast_expression (parser,
6224
                                            /*address_p=*/false,
6225
                                            /*cast_p=*/true, pidk);
6226
 
6227
          /* Warn about old-style casts, if so requested.  */
6228
          if (warn_old_style_cast
6229
              && !in_system_header
6230
              && !VOID_TYPE_P (type)
6231
              && current_lang_name != lang_name_c)
6232
            warning (OPT_Wold_style_cast, "use of old-style cast");
6233
 
6234
          /* Only type conversions to integral or enumeration types
6235
             can be used in constant-expressions.  */
6236
          if (!cast_valid_in_integral_constant_expression_p (type)
6237
              && (cp_parser_non_integral_constant_expression
6238
                  (parser,
6239
                   "a cast to a type other than an integral or "
6240
                   "enumeration type")))
6241
            return error_mark_node;
6242
 
6243
          /* Perform the cast.  */
6244
          expr = build_c_cast (input_location, type, expr);
6245
          return expr;
6246
        }
6247
      else
6248
        cp_parser_abort_tentative_parse (parser);
6249
    }
6250
 
6251
  /* If we get here, then it's not a cast, so it must be a
6252
     unary-expression.  */
6253
  return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6254
}
6255
 
6256
/* Parse a binary expression of the general form:
6257
 
6258
   pm-expression:
6259
     cast-expression
6260
     pm-expression .* cast-expression
6261
     pm-expression ->* cast-expression
6262
 
6263
   multiplicative-expression:
6264
     pm-expression
6265
     multiplicative-expression * pm-expression
6266
     multiplicative-expression / pm-expression
6267
     multiplicative-expression % pm-expression
6268
 
6269
   additive-expression:
6270
     multiplicative-expression
6271
     additive-expression + multiplicative-expression
6272
     additive-expression - multiplicative-expression
6273
 
6274
   shift-expression:
6275
     additive-expression
6276
     shift-expression << additive-expression
6277
     shift-expression >> additive-expression
6278
 
6279
   relational-expression:
6280
     shift-expression
6281
     relational-expression < shift-expression
6282
     relational-expression > shift-expression
6283
     relational-expression <= shift-expression
6284
     relational-expression >= shift-expression
6285
 
6286
  GNU Extension:
6287
 
6288
   relational-expression:
6289
     relational-expression <? shift-expression
6290
     relational-expression >? shift-expression
6291
 
6292
   equality-expression:
6293
     relational-expression
6294
     equality-expression == relational-expression
6295
     equality-expression != relational-expression
6296
 
6297
   and-expression:
6298
     equality-expression
6299
     and-expression & equality-expression
6300
 
6301
   exclusive-or-expression:
6302
     and-expression
6303
     exclusive-or-expression ^ and-expression
6304
 
6305
   inclusive-or-expression:
6306
     exclusive-or-expression
6307
     inclusive-or-expression | exclusive-or-expression
6308
 
6309
   logical-and-expression:
6310
     inclusive-or-expression
6311
     logical-and-expression && inclusive-or-expression
6312
 
6313
   logical-or-expression:
6314
     logical-and-expression
6315
     logical-or-expression || logical-and-expression
6316
 
6317
   All these are implemented with a single function like:
6318
 
6319
   binary-expression:
6320
     simple-cast-expression
6321
     binary-expression <token> binary-expression
6322
 
6323
   CAST_P is true if this expression is the target of a cast.
6324
 
6325
   The binops_by_token map is used to get the tree codes for each <token> type.
6326
   binary-expressions are associated according to a precedence table.  */
6327
 
6328
#define TOKEN_PRECEDENCE(token)                              \
6329
(((token->type == CPP_GREATER                                \
6330
   || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6331
  && !parser->greater_than_is_operator_p)                    \
6332
 ? PREC_NOT_OPERATOR                                         \
6333
 : binops_by_token[token->type].prec)
6334
 
6335
static tree
6336
cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6337
                             bool no_toplevel_fold_p,
6338
                             enum cp_parser_prec prec,
6339
                             cp_id_kind * pidk)
6340
{
6341
  cp_parser_expression_stack stack;
6342
  cp_parser_expression_stack_entry *sp = &stack[0];
6343
  tree lhs, rhs;
6344
  cp_token *token;
6345
  enum tree_code tree_type, lhs_type, rhs_type;
6346
  enum cp_parser_prec new_prec, lookahead_prec;
6347
  bool overloaded_p;
6348
 
6349
  /* Parse the first expression.  */
6350
  lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6351
  lhs_type = ERROR_MARK;
6352
 
6353
  for (;;)
6354
    {
6355
      /* Get an operator token.  */
6356
      token = cp_lexer_peek_token (parser->lexer);
6357
 
6358
      if (warn_cxx0x_compat
6359
          && token->type == CPP_RSHIFT
6360
          && !parser->greater_than_is_operator_p)
6361
        {
6362
          if (warning_at (token->location, OPT_Wc__0x_compat,
6363
                          "%<>>%> operator will be treated as"
6364
                          " two right angle brackets in C++0x"))
6365
            inform (token->location,
6366
                    "suggest parentheses around %<>>%> expression");
6367
        }
6368
 
6369
      new_prec = TOKEN_PRECEDENCE (token);
6370
 
6371
      /* Popping an entry off the stack means we completed a subexpression:
6372
         - either we found a token which is not an operator (`>' where it is not
6373
           an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6374
           will happen repeatedly;
6375
         - or, we found an operator which has lower priority.  This is the case
6376
           where the recursive descent *ascends*, as in `3 * 4 + 5' after
6377
           parsing `3 * 4'.  */
6378
      if (new_prec <= prec)
6379
        {
6380
          if (sp == stack)
6381
            break;
6382
          else
6383
            goto pop;
6384
        }
6385
 
6386
     get_rhs:
6387
      tree_type = binops_by_token[token->type].tree_type;
6388
 
6389
      /* We used the operator token.  */
6390
      cp_lexer_consume_token (parser->lexer);
6391
 
6392
      /* For "false && x" or "true || x", x will never be executed;
6393
         disable warnings while evaluating it.  */
6394
      if (tree_type == TRUTH_ANDIF_EXPR)
6395
        c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6396
      else if (tree_type == TRUTH_ORIF_EXPR)
6397
        c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6398
 
6399
      /* Extract another operand.  It may be the RHS of this expression
6400
         or the LHS of a new, higher priority expression.  */
6401
      rhs = cp_parser_simple_cast_expression (parser);
6402
      rhs_type = ERROR_MARK;
6403
 
6404
      /* Get another operator token.  Look up its precedence to avoid
6405
         building a useless (immediately popped) stack entry for common
6406
         cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6407
      token = cp_lexer_peek_token (parser->lexer);
6408
      lookahead_prec = TOKEN_PRECEDENCE (token);
6409
      if (lookahead_prec > new_prec)
6410
        {
6411
          /* ... and prepare to parse the RHS of the new, higher priority
6412
             expression.  Since precedence levels on the stack are
6413
             monotonically increasing, we do not have to care about
6414
             stack overflows.  */
6415
          sp->prec = prec;
6416
          sp->tree_type = tree_type;
6417
          sp->lhs = lhs;
6418
          sp->lhs_type = lhs_type;
6419
          sp++;
6420
          lhs = rhs;
6421
          lhs_type = rhs_type;
6422
          prec = new_prec;
6423
          new_prec = lookahead_prec;
6424
          goto get_rhs;
6425
 
6426
         pop:
6427
          lookahead_prec = new_prec;
6428
          /* If the stack is not empty, we have parsed into LHS the right side
6429
             (`4' in the example above) of an expression we had suspended.
6430
             We can use the information on the stack to recover the LHS (`3')
6431
             from the stack together with the tree code (`MULT_EXPR'), and
6432
             the precedence of the higher level subexpression
6433
             (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6434
             which will be used to actually build the additive expression.  */
6435
          --sp;
6436
          prec = sp->prec;
6437
          tree_type = sp->tree_type;
6438
          rhs = lhs;
6439
          rhs_type = lhs_type;
6440
          lhs = sp->lhs;
6441
          lhs_type = sp->lhs_type;
6442
        }
6443
 
6444
      /* Undo the disabling of warnings done above.  */
6445
      if (tree_type == TRUTH_ANDIF_EXPR)
6446
        c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6447
      else if (tree_type == TRUTH_ORIF_EXPR)
6448
        c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6449
 
6450
      overloaded_p = false;
6451
      /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6452
         ERROR_MARK for everything that is not a binary expression.
6453
         This makes warn_about_parentheses miss some warnings that
6454
         involve unary operators.  For unary expressions we should
6455
         pass the correct tree_code unless the unary expression was
6456
         surrounded by parentheses.
6457
      */
6458
      if (no_toplevel_fold_p
6459
          && lookahead_prec <= prec
6460
          && sp == stack
6461
          && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6462
        lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6463
      else
6464
        lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6465
                                 &overloaded_p, tf_warning_or_error);
6466
      lhs_type = tree_type;
6467
 
6468
      /* If the binary operator required the use of an overloaded operator,
6469
         then this expression cannot be an integral constant-expression.
6470
         An overloaded operator can be used even if both operands are
6471
         otherwise permissible in an integral constant-expression if at
6472
         least one of the operands is of enumeration type.  */
6473
 
6474
      if (overloaded_p
6475
          && (cp_parser_non_integral_constant_expression
6476
              (parser, "calls to overloaded operators")))
6477
        return error_mark_node;
6478
    }
6479
 
6480
  return lhs;
6481
}
6482
 
6483
 
6484
/* Parse the `? expression : assignment-expression' part of a
6485
   conditional-expression.  The LOGICAL_OR_EXPR is the
6486
   logical-or-expression that started the conditional-expression.
6487
   Returns a representation of the entire conditional-expression.
6488
 
6489
   This routine is used by cp_parser_assignment_expression.
6490
 
6491
     ? expression : assignment-expression
6492
 
6493
   GNU Extensions:
6494
 
6495
     ? : assignment-expression */
6496
 
6497
static tree
6498
cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6499
{
6500
  tree expr;
6501
  tree assignment_expr;
6502
 
6503
  /* Consume the `?' token.  */
6504
  cp_lexer_consume_token (parser->lexer);
6505
  if (cp_parser_allow_gnu_extensions_p (parser)
6506
      && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6507
    {
6508
      /* Implicit true clause.  */
6509
      expr = NULL_TREE;
6510
      c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6511
    }
6512
  else
6513
    {
6514
      /* Parse the expression.  */
6515
      c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6516
      expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6517
      c_inhibit_evaluation_warnings +=
6518
        ((logical_or_expr == truthvalue_true_node)
6519
         - (logical_or_expr == truthvalue_false_node));
6520
    }
6521
 
6522
  /* The next token should be a `:'.  */
6523
  cp_parser_require (parser, CPP_COLON, "%<:%>");
6524
  /* Parse the assignment-expression.  */
6525
  assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6526
  c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6527
 
6528
  /* Build the conditional-expression.  */
6529
  return build_x_conditional_expr (logical_or_expr,
6530
                                   expr,
6531
                                   assignment_expr,
6532
                                   tf_warning_or_error);
6533
}
6534
 
6535
/* Parse an assignment-expression.
6536
 
6537
   assignment-expression:
6538
     conditional-expression
6539
     logical-or-expression assignment-operator assignment_expression
6540
     throw-expression
6541
 
6542
   CAST_P is true if this expression is the target of a cast.
6543
 
6544
   Returns a representation for the expression.  */
6545
 
6546
static tree
6547
cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6548
                                 cp_id_kind * pidk)
6549
{
6550
  tree expr;
6551
 
6552
  /* If the next token is the `throw' keyword, then we're looking at
6553
     a throw-expression.  */
6554
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6555
    expr = cp_parser_throw_expression (parser);
6556
  /* Otherwise, it must be that we are looking at a
6557
     logical-or-expression.  */
6558
  else
6559
    {
6560
      /* Parse the binary expressions (logical-or-expression).  */
6561
      expr = cp_parser_binary_expression (parser, cast_p, false,
6562
                                          PREC_NOT_OPERATOR, pidk);
6563
      /* If the next token is a `?' then we're actually looking at a
6564
         conditional-expression.  */
6565
      if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6566
        return cp_parser_question_colon_clause (parser, expr);
6567
      else
6568
        {
6569
          enum tree_code assignment_operator;
6570
 
6571
          /* If it's an assignment-operator, we're using the second
6572
             production.  */
6573
          assignment_operator
6574
            = cp_parser_assignment_operator_opt (parser);
6575
          if (assignment_operator != ERROR_MARK)
6576
            {
6577
              bool non_constant_p;
6578
 
6579
              /* Parse the right-hand side of the assignment.  */
6580
              tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6581
 
6582
              if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6583
                maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6584
 
6585
              /* An assignment may not appear in a
6586
                 constant-expression.  */
6587
              if (cp_parser_non_integral_constant_expression (parser,
6588
                                                              "an assignment"))
6589
                return error_mark_node;
6590
              /* Build the assignment expression.  */
6591
              expr = build_x_modify_expr (expr,
6592
                                          assignment_operator,
6593
                                          rhs,
6594
                                          tf_warning_or_error);
6595
            }
6596
        }
6597
    }
6598
 
6599
  return expr;
6600
}
6601
 
6602
/* Parse an (optional) assignment-operator.
6603
 
6604
   assignment-operator: one of
6605
     = *= /= %= += -= >>= <<= &= ^= |=
6606
 
6607
   GNU Extension:
6608
 
6609
   assignment-operator: one of
6610
     <?= >?=
6611
 
6612
   If the next token is an assignment operator, the corresponding tree
6613
   code is returned, and the token is consumed.  For example, for
6614
   `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6615
   NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6616
   TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6617
   operator, ERROR_MARK is returned.  */
6618
 
6619
static enum tree_code
6620
cp_parser_assignment_operator_opt (cp_parser* parser)
6621
{
6622
  enum tree_code op;
6623
  cp_token *token;
6624
 
6625
  /* Peek at the next token.  */
6626
  token = cp_lexer_peek_token (parser->lexer);
6627
 
6628
  switch (token->type)
6629
    {
6630
    case CPP_EQ:
6631
      op = NOP_EXPR;
6632
      break;
6633
 
6634
    case CPP_MULT_EQ:
6635
      op = MULT_EXPR;
6636
      break;
6637
 
6638
    case CPP_DIV_EQ:
6639
      op = TRUNC_DIV_EXPR;
6640
      break;
6641
 
6642
    case CPP_MOD_EQ:
6643
      op = TRUNC_MOD_EXPR;
6644
      break;
6645
 
6646
    case CPP_PLUS_EQ:
6647
      op = PLUS_EXPR;
6648
      break;
6649
 
6650
    case CPP_MINUS_EQ:
6651
      op = MINUS_EXPR;
6652
      break;
6653
 
6654
    case CPP_RSHIFT_EQ:
6655
      op = RSHIFT_EXPR;
6656
      break;
6657
 
6658
    case CPP_LSHIFT_EQ:
6659
      op = LSHIFT_EXPR;
6660
      break;
6661
 
6662
    case CPP_AND_EQ:
6663
      op = BIT_AND_EXPR;
6664
      break;
6665
 
6666
    case CPP_XOR_EQ:
6667
      op = BIT_XOR_EXPR;
6668
      break;
6669
 
6670
    case CPP_OR_EQ:
6671
      op = BIT_IOR_EXPR;
6672
      break;
6673
 
6674
    default:
6675
      /* Nothing else is an assignment operator.  */
6676
      op = ERROR_MARK;
6677
    }
6678
 
6679
  /* If it was an assignment operator, consume it.  */
6680
  if (op != ERROR_MARK)
6681
    cp_lexer_consume_token (parser->lexer);
6682
 
6683
  return op;
6684
}
6685
 
6686
/* Parse an expression.
6687
 
6688
   expression:
6689
     assignment-expression
6690
     expression , assignment-expression
6691
 
6692
   CAST_P is true if this expression is the target of a cast.
6693
 
6694
   Returns a representation of the expression.  */
6695
 
6696
static tree
6697
cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6698
{
6699
  tree expression = NULL_TREE;
6700
 
6701
  while (true)
6702
    {
6703
      tree assignment_expression;
6704
 
6705
      /* Parse the next assignment-expression.  */
6706
      assignment_expression
6707
        = cp_parser_assignment_expression (parser, cast_p, pidk);
6708
      /* If this is the first assignment-expression, we can just
6709
         save it away.  */
6710
      if (!expression)
6711
        expression = assignment_expression;
6712
      else
6713
        expression = build_x_compound_expr (expression,
6714
                                            assignment_expression,
6715
                                            tf_warning_or_error);
6716
      /* If the next token is not a comma, then we are done with the
6717
         expression.  */
6718
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6719
        break;
6720
      /* Consume the `,'.  */
6721
      cp_lexer_consume_token (parser->lexer);
6722
      /* A comma operator cannot appear in a constant-expression.  */
6723
      if (cp_parser_non_integral_constant_expression (parser,
6724
                                                      "a comma operator"))
6725
        expression = error_mark_node;
6726
    }
6727
 
6728
  return expression;
6729
}
6730
 
6731
/* Parse a constant-expression.
6732
 
6733
   constant-expression:
6734
     conditional-expression
6735
 
6736
  If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6737
  accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6738
  constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6739
  is false, NON_CONSTANT_P should be NULL.  */
6740
 
6741
static tree
6742
cp_parser_constant_expression (cp_parser* parser,
6743
                               bool allow_non_constant_p,
6744
                               bool *non_constant_p)
6745
{
6746
  bool saved_integral_constant_expression_p;
6747
  bool saved_allow_non_integral_constant_expression_p;
6748
  bool saved_non_integral_constant_expression_p;
6749
  tree expression;
6750
 
6751
  /* It might seem that we could simply parse the
6752
     conditional-expression, and then check to see if it were
6753
     TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6754
     one that the compiler can figure out is constant, possibly after
6755
     doing some simplifications or optimizations.  The standard has a
6756
     precise definition of constant-expression, and we must honor
6757
     that, even though it is somewhat more restrictive.
6758
 
6759
     For example:
6760
 
6761
       int i[(2, 3)];
6762
 
6763
     is not a legal declaration, because `(2, 3)' is not a
6764
     constant-expression.  The `,' operator is forbidden in a
6765
     constant-expression.  However, GCC's constant-folding machinery
6766
     will fold this operation to an INTEGER_CST for `3'.  */
6767
 
6768
  /* Save the old settings.  */
6769
  saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6770
  saved_allow_non_integral_constant_expression_p
6771
    = parser->allow_non_integral_constant_expression_p;
6772
  saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6773
  /* We are now parsing a constant-expression.  */
6774
  parser->integral_constant_expression_p = true;
6775
  parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6776
  parser->non_integral_constant_expression_p = false;
6777
  /* Although the grammar says "conditional-expression", we parse an
6778
     "assignment-expression", which also permits "throw-expression"
6779
     and the use of assignment operators.  In the case that
6780
     ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6781
     otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6782
     actually essential that we look for an assignment-expression.
6783
     For example, cp_parser_initializer_clauses uses this function to
6784
     determine whether a particular assignment-expression is in fact
6785
     constant.  */
6786
  expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6787
  /* Restore the old settings.  */
6788
  parser->integral_constant_expression_p
6789
    = saved_integral_constant_expression_p;
6790
  parser->allow_non_integral_constant_expression_p
6791
    = saved_allow_non_integral_constant_expression_p;
6792
  if (allow_non_constant_p)
6793
    *non_constant_p = parser->non_integral_constant_expression_p;
6794
  else if (parser->non_integral_constant_expression_p)
6795
    expression = error_mark_node;
6796
  parser->non_integral_constant_expression_p
6797
    = saved_non_integral_constant_expression_p;
6798
 
6799
  return expression;
6800
}
6801
 
6802
/* Parse __builtin_offsetof.
6803
 
6804
   offsetof-expression:
6805
     "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6806
 
6807
   offsetof-member-designator:
6808
     id-expression
6809
     | offsetof-member-designator "." id-expression
6810
     | offsetof-member-designator "[" expression "]"
6811
     | offsetof-member-designator "->" id-expression  */
6812
 
6813
static tree
6814
cp_parser_builtin_offsetof (cp_parser *parser)
6815
{
6816
  int save_ice_p, save_non_ice_p;
6817
  tree type, expr;
6818
  cp_id_kind dummy;
6819
  cp_token *token;
6820
 
6821
  /* We're about to accept non-integral-constant things, but will
6822
     definitely yield an integral constant expression.  Save and
6823
     restore these values around our local parsing.  */
6824
  save_ice_p = parser->integral_constant_expression_p;
6825
  save_non_ice_p = parser->non_integral_constant_expression_p;
6826
 
6827
  /* Consume the "__builtin_offsetof" token.  */
6828
  cp_lexer_consume_token (parser->lexer);
6829
  /* Consume the opening `('.  */
6830
  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6831
  /* Parse the type-id.  */
6832
  type = cp_parser_type_id (parser);
6833
  /* Look for the `,'.  */
6834
  cp_parser_require (parser, CPP_COMMA, "%<,%>");
6835
  token = cp_lexer_peek_token (parser->lexer);
6836
 
6837
  /* Build the (type *)null that begins the traditional offsetof macro.  */
6838
  expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6839
                            tf_warning_or_error);
6840
 
6841
  /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6842
  expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6843
                                                 true, &dummy, token->location);
6844
  while (true)
6845
    {
6846
      token = cp_lexer_peek_token (parser->lexer);
6847
      switch (token->type)
6848
        {
6849
        case CPP_OPEN_SQUARE:
6850
          /* offsetof-member-designator "[" expression "]" */
6851
          expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6852
          break;
6853
 
6854
        case CPP_DEREF:
6855
          /* offsetof-member-designator "->" identifier */
6856
          expr = grok_array_decl (expr, integer_zero_node);
6857
          /* FALLTHRU */
6858
 
6859
        case CPP_DOT:
6860
          /* offsetof-member-designator "." identifier */
6861
          cp_lexer_consume_token (parser->lexer);
6862
          expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6863
                                                         expr, true, &dummy,
6864
                                                         token->location);
6865
          break;
6866
 
6867
        case CPP_CLOSE_PAREN:
6868
          /* Consume the ")" token.  */
6869
          cp_lexer_consume_token (parser->lexer);
6870
          goto success;
6871
 
6872
        default:
6873
          /* Error.  We know the following require will fail, but
6874
             that gives the proper error message.  */
6875
          cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6876
          cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6877
          expr = error_mark_node;
6878
          goto failure;
6879
        }
6880
    }
6881
 
6882
 success:
6883
  /* If we're processing a template, we can't finish the semantics yet.
6884
     Otherwise we can fold the entire expression now.  */
6885
  if (processing_template_decl)
6886
    expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6887
  else
6888
    expr = finish_offsetof (expr);
6889
 
6890
 failure:
6891
  parser->integral_constant_expression_p = save_ice_p;
6892
  parser->non_integral_constant_expression_p = save_non_ice_p;
6893
 
6894
  return expr;
6895
}
6896
 
6897
/* Parse a trait expression.  */
6898
 
6899
static tree
6900
cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6901
{
6902
  cp_trait_kind kind;
6903
  tree type1, type2 = NULL_TREE;
6904
  bool binary = false;
6905
  cp_decl_specifier_seq decl_specs;
6906
 
6907
  switch (keyword)
6908
    {
6909
    case RID_HAS_NOTHROW_ASSIGN:
6910
      kind = CPTK_HAS_NOTHROW_ASSIGN;
6911
      break;
6912
    case RID_HAS_NOTHROW_CONSTRUCTOR:
6913
      kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6914
      break;
6915
    case RID_HAS_NOTHROW_COPY:
6916
      kind = CPTK_HAS_NOTHROW_COPY;
6917
      break;
6918
    case RID_HAS_TRIVIAL_ASSIGN:
6919
      kind = CPTK_HAS_TRIVIAL_ASSIGN;
6920
      break;
6921
    case RID_HAS_TRIVIAL_CONSTRUCTOR:
6922
      kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6923
      break;
6924
    case RID_HAS_TRIVIAL_COPY:
6925
      kind = CPTK_HAS_TRIVIAL_COPY;
6926
      break;
6927
    case RID_HAS_TRIVIAL_DESTRUCTOR:
6928
      kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6929
      break;
6930
    case RID_HAS_VIRTUAL_DESTRUCTOR:
6931
      kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6932
      break;
6933
    case RID_IS_ABSTRACT:
6934
      kind = CPTK_IS_ABSTRACT;
6935
      break;
6936
    case RID_IS_BASE_OF:
6937
      kind = CPTK_IS_BASE_OF;
6938
      binary = true;
6939
      break;
6940
    case RID_IS_CLASS:
6941
      kind = CPTK_IS_CLASS;
6942
      break;
6943
    case RID_IS_CONVERTIBLE_TO:
6944
      kind = CPTK_IS_CONVERTIBLE_TO;
6945
      binary = true;
6946
      break;
6947
    case RID_IS_EMPTY:
6948
      kind = CPTK_IS_EMPTY;
6949
      break;
6950
    case RID_IS_ENUM:
6951
      kind = CPTK_IS_ENUM;
6952
      break;
6953
    case RID_IS_POD:
6954
      kind = CPTK_IS_POD;
6955
      break;
6956
    case RID_IS_POLYMORPHIC:
6957
      kind = CPTK_IS_POLYMORPHIC;
6958
      break;
6959
    case RID_IS_STD_LAYOUT:
6960
      kind = CPTK_IS_STD_LAYOUT;
6961
      break;
6962
    case RID_IS_TRIVIAL:
6963
      kind = CPTK_IS_TRIVIAL;
6964
      break;
6965
    case RID_IS_UNION:
6966
      kind = CPTK_IS_UNION;
6967
      break;
6968
    default:
6969
      gcc_unreachable ();
6970
    }
6971
 
6972
  /* Consume the token.  */
6973
  cp_lexer_consume_token (parser->lexer);
6974
 
6975
  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6976
 
6977
  type1 = cp_parser_type_id (parser);
6978
 
6979
  if (type1 == error_mark_node)
6980
    return error_mark_node;
6981
 
6982
  /* Build a trivial decl-specifier-seq.  */
6983
  clear_decl_specs (&decl_specs);
6984
  decl_specs.type = type1;
6985
 
6986
  /* Call grokdeclarator to figure out what type this is.  */
6987
  type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6988
                          /*initialized=*/0, /*attrlist=*/NULL);
6989
 
6990
  if (binary)
6991
    {
6992
      cp_parser_require (parser, CPP_COMMA, "%<,%>");
6993
 
6994
      type2 = cp_parser_type_id (parser);
6995
 
6996
      if (type2 == error_mark_node)
6997
        return error_mark_node;
6998
 
6999
      /* Build a trivial decl-specifier-seq.  */
7000
      clear_decl_specs (&decl_specs);
7001
      decl_specs.type = type2;
7002
 
7003
      /* Call grokdeclarator to figure out what type this is.  */
7004
      type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7005
                              /*initialized=*/0, /*attrlist=*/NULL);
7006
    }
7007
 
7008
  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7009
 
7010
  /* Complete the trait expression, which may mean either processing
7011
     the trait expr now or saving it for template instantiation.  */
7012
  return finish_trait_expr (kind, type1, type2);
7013
}
7014
 
7015
/* Lambdas that appear in variable initializer or default argument scope
7016
   get that in their mangling, so we need to record it.  We might as well
7017
   use the count for function and namespace scopes as well.  */
7018
static GTY(()) tree lambda_scope;
7019
static GTY(()) int lambda_count;
7020
typedef struct GTY(()) tree_int
7021
{
7022
  tree t;
7023
  int i;
7024
} tree_int;
7025
DEF_VEC_O(tree_int);
7026
DEF_VEC_ALLOC_O(tree_int,gc);
7027
static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7028
 
7029
static void
7030
start_lambda_scope (tree decl)
7031
{
7032
  tree_int ti;
7033
  gcc_assert (decl);
7034
  /* Once we're inside a function, we ignore other scopes and just push
7035
     the function again so that popping works properly.  */
7036
  if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7037
    decl = current_function_decl;
7038
  ti.t = lambda_scope;
7039
  ti.i = lambda_count;
7040
  VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7041
  if (lambda_scope != decl)
7042
    {
7043
      /* Don't reset the count if we're still in the same function.  */
7044
      lambda_scope = decl;
7045
      lambda_count = 0;
7046
    }
7047
}
7048
 
7049
static void
7050
record_lambda_scope (tree lambda)
7051
{
7052
  LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7053
  LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7054
}
7055
 
7056
static void
7057
finish_lambda_scope (void)
7058
{
7059
  tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7060
  if (lambda_scope != p->t)
7061
    {
7062
      lambda_scope = p->t;
7063
      lambda_count = p->i;
7064
    }
7065
  VEC_pop (tree_int, lambda_scope_stack);
7066
}
7067
 
7068
/* Parse a lambda expression.
7069
 
7070
   lambda-expression:
7071
     lambda-introducer lambda-declarator [opt] compound-statement
7072
 
7073
   Returns a representation of the expression.  */
7074
 
7075
static tree
7076
cp_parser_lambda_expression (cp_parser* parser)
7077
{
7078
  tree lambda_expr = build_lambda_expr ();
7079
  tree type;
7080
 
7081
  LAMBDA_EXPR_LOCATION (lambda_expr)
7082
    = cp_lexer_peek_token (parser->lexer)->location;
7083
 
7084
  if (cp_unevaluated_operand)
7085
    error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7086
              "lambda-expression in unevaluated context");
7087
 
7088
  /* We may be in the middle of deferred access check.  Disable
7089
     it now.  */
7090
  push_deferring_access_checks (dk_no_deferred);
7091
 
7092
  cp_parser_lambda_introducer (parser, lambda_expr);
7093
 
7094
  type = begin_lambda_type (lambda_expr);
7095
 
7096
  record_lambda_scope (lambda_expr);
7097
 
7098
  /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7099
  determine_visibility (TYPE_NAME (type));
7100
 
7101
  /* Now that we've started the type, add the capture fields for any
7102
     explicit captures.  */
7103
  register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7104
 
7105
  {
7106
    /* Inside the class, surrounding template-parameter-lists do not apply.  */
7107
    unsigned int saved_num_template_parameter_lists
7108
        = parser->num_template_parameter_lists;
7109
 
7110
    parser->num_template_parameter_lists = 0;
7111
 
7112
    /* By virtue of defining a local class, a lambda expression has access to
7113
       the private variables of enclosing classes.  */
7114
 
7115
    cp_parser_lambda_declarator_opt (parser, lambda_expr);
7116
 
7117
    cp_parser_lambda_body (parser, lambda_expr);
7118
 
7119
    /* The capture list was built up in reverse order; fix that now.  */
7120
    {
7121
      tree newlist = NULL_TREE;
7122
      tree elt, next;
7123
 
7124
      for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7125
           elt; elt = next)
7126
        {
7127
          tree field = TREE_PURPOSE (elt);
7128
          char *buf;
7129
 
7130
          next = TREE_CHAIN (elt);
7131
          TREE_CHAIN (elt) = newlist;
7132
          newlist = elt;
7133
 
7134
          /* Also add __ to the beginning of the field name so that code
7135
             outside the lambda body can't see the captured name.  We could
7136
             just remove the name entirely, but this is more useful for
7137
             debugging.  */
7138
          if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7139
            /* The 'this' capture already starts with __.  */
7140
            continue;
7141
 
7142
          buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7143
          buf[1] = buf[0] = '_';
7144
          memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7145
                  IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7146
          DECL_NAME (field) = get_identifier (buf);
7147
        }
7148
      LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7149
    }
7150
 
7151
    maybe_add_lambda_conv_op (type);
7152
 
7153
    type = finish_struct (type, /*attributes=*/NULL_TREE);
7154
 
7155
    parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7156
  }
7157
 
7158
  pop_deferring_access_checks ();
7159
 
7160
  return build_lambda_object (lambda_expr);
7161
}
7162
 
7163
/* Parse the beginning of a lambda expression.
7164
 
7165
   lambda-introducer:
7166
     [ lambda-capture [opt] ]
7167
 
7168
   LAMBDA_EXPR is the current representation of the lambda expression.  */
7169
 
7170
static void
7171
cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7172
{
7173
  /* Need commas after the first capture.  */
7174
  bool first = true;
7175
 
7176
  /* Eat the leading `['.  */
7177
  cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7178
 
7179
  /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7180
  if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7181
      && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7182
    LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7183
  else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7184
    LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7185
 
7186
  if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7187
    {
7188
      cp_lexer_consume_token (parser->lexer);
7189
      first = false;
7190
    }
7191
 
7192
  while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7193
    {
7194
      cp_token* capture_token;
7195
      tree capture_id;
7196
      tree capture_init_expr;
7197
      cp_id_kind idk = CP_ID_KIND_NONE;
7198
      bool explicit_init_p = false;
7199
 
7200
      enum capture_kind_type
7201
      {
7202
        BY_COPY,
7203
        BY_REFERENCE
7204
      };
7205
      enum capture_kind_type capture_kind = BY_COPY;
7206
 
7207
      if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7208
        {
7209
          error ("expected end of capture-list");
7210
          return;
7211
        }
7212
 
7213
      if (first)
7214
        first = false;
7215
      else
7216
        cp_parser_require (parser, CPP_COMMA, "%<,%>");
7217
 
7218
      /* Possibly capture `this'.  */
7219
      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7220
        {
7221
          cp_lexer_consume_token (parser->lexer);
7222
          add_capture (lambda_expr,
7223
                       /*id=*/get_identifier ("__this"),
7224
                       /*initializer=*/finish_this_expr(),
7225
                       /*by_reference_p=*/false,
7226
                       explicit_init_p);
7227
          continue;
7228
        }
7229
 
7230
      /* Remember whether we want to capture as a reference or not.  */
7231
      if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7232
        {
7233
          capture_kind = BY_REFERENCE;
7234
          cp_lexer_consume_token (parser->lexer);
7235
        }
7236
 
7237
      /* Get the identifier.  */
7238
      capture_token = cp_lexer_peek_token (parser->lexer);
7239
      capture_id = cp_parser_identifier (parser);
7240
 
7241
      if (capture_id == error_mark_node)
7242
        /* Would be nice to have a cp_parser_skip_to_closing_x for general
7243
           delimiters, but I modified this to stop on unnested ']' as well.  It
7244
           was already changed to stop on unnested '}', so the
7245
           "closing_parenthesis" name is no more misleading with my change.  */
7246
        {
7247
          cp_parser_skip_to_closing_parenthesis (parser,
7248
                                                 /*recovering=*/true,
7249
                                                 /*or_comma=*/true,
7250
                                                 /*consume_paren=*/true);
7251
          break;
7252
        }
7253
 
7254
      /* Find the initializer for this capture.  */
7255
      if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7256
        {
7257
          /* An explicit expression exists.  */
7258
          cp_lexer_consume_token (parser->lexer);
7259
          pedwarn (input_location, OPT_pedantic,
7260
                   "ISO C++ does not allow initializers "
7261
                   "in lambda expression capture lists");
7262
          capture_init_expr = cp_parser_assignment_expression (parser,
7263
                                                               /*cast_p=*/true,
7264
                                                               &idk);
7265
          explicit_init_p = true;
7266
        }
7267
      else
7268
        {
7269
          const char* error_msg;
7270
 
7271
          /* Turn the identifier into an id-expression.  */
7272
          capture_init_expr
7273
            = cp_parser_lookup_name
7274
                (parser,
7275
                 capture_id,
7276
                 none_type,
7277
                 /*is_template=*/false,
7278
                 /*is_namespace=*/false,
7279
                 /*check_dependency=*/true,
7280
                 /*ambiguous_decls=*/NULL,
7281
                 capture_token->location);
7282
 
7283
          capture_init_expr
7284
            = finish_id_expression
7285
                (capture_id,
7286
                 capture_init_expr,
7287
                 parser->scope,
7288
                 &idk,
7289
                 /*integral_constant_expression_p=*/false,
7290
                 /*allow_non_integral_constant_expression_p=*/false,
7291
                 /*non_integral_constant_expression_p=*/NULL,
7292
                 /*template_p=*/false,
7293
                 /*done=*/true,
7294
                 /*address_p=*/false,
7295
                 /*template_arg_p=*/false,
7296
                 &error_msg,
7297
                 capture_token->location);
7298
        }
7299
 
7300
      if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7301
        capture_init_expr
7302
          = unqualified_name_lookup_error (capture_init_expr);
7303
 
7304
      add_capture (lambda_expr,
7305
                   capture_id,
7306
                   capture_init_expr,
7307
                   /*by_reference_p=*/capture_kind == BY_REFERENCE,
7308
                   explicit_init_p);
7309
    }
7310
 
7311
  cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7312
}
7313
 
7314
/* Parse the (optional) middle of a lambda expression.
7315
 
7316
   lambda-declarator:
7317
     ( parameter-declaration-clause [opt] )
7318
       attribute-specifier [opt]
7319
       mutable [opt]
7320
       exception-specification [opt]
7321
       lambda-return-type-clause [opt]
7322
 
7323
   LAMBDA_EXPR is the current representation of the lambda expression.  */
7324
 
7325
static void
7326
cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7327
{
7328
  /* 5.1.1.4 of the standard says:
7329
       If a lambda-expression does not include a lambda-declarator, it is as if
7330
       the lambda-declarator were ().
7331
     This means an empty parameter list, no attributes, and no exception
7332
     specification.  */
7333
  tree param_list = void_list_node;
7334
  tree attributes = NULL_TREE;
7335
  tree exception_spec = NULL_TREE;
7336
  tree t;
7337
 
7338
  /* The lambda-declarator is optional, but must begin with an opening
7339
     parenthesis if present.  */
7340
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7341
    {
7342
      cp_lexer_consume_token (parser->lexer);
7343
 
7344
      begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7345
 
7346
      /* Parse parameters.  */
7347
      param_list = cp_parser_parameter_declaration_clause (parser);
7348
 
7349
      /* Default arguments shall not be specified in the
7350
         parameter-declaration-clause of a lambda-declarator.  */
7351
      for (t = param_list; t; t = TREE_CHAIN (t))
7352
        if (TREE_PURPOSE (t))
7353
          pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7354
                   "default argument specified for lambda parameter");
7355
 
7356
      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7357
 
7358
      attributes = cp_parser_attributes_opt (parser);
7359
 
7360
      /* Parse optional `mutable' keyword.  */
7361
      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7362
        {
7363
          cp_lexer_consume_token (parser->lexer);
7364
          LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7365
        }
7366
 
7367
      /* Parse optional exception specification.  */
7368
      exception_spec = cp_parser_exception_specification_opt (parser);
7369
 
7370
      /* Parse optional trailing return type.  */
7371
      if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7372
        {
7373
          cp_lexer_consume_token (parser->lexer);
7374
          LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7375
        }
7376
 
7377
      /* The function parameters must be in scope all the way until after the
7378
         trailing-return-type in case of decltype.  */
7379
      for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7380
        pop_binding (DECL_NAME (t), t);
7381
 
7382
      leave_scope ();
7383
    }
7384
 
7385
  /* Create the function call operator.
7386
 
7387
     Messing with declarators like this is no uglier than building up the
7388
     FUNCTION_DECL by hand, and this is less likely to get out of sync with
7389
     other code.  */
7390
  {
7391
    cp_decl_specifier_seq return_type_specs;
7392
    cp_declarator* declarator;
7393
    tree fco;
7394
    int quals;
7395
    void *p;
7396
 
7397
    clear_decl_specs (&return_type_specs);
7398
    if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7399
      return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7400
    else
7401
      /* Maybe we will deduce the return type later, but we can use void
7402
         as a placeholder return type anyways.  */
7403
      return_type_specs.type = void_type_node;
7404
 
7405
    p = obstack_alloc (&declarator_obstack, 0);
7406
 
7407
    declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7408
                                     sfk_none);
7409
 
7410
    quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7411
             ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7412
    declarator = make_call_declarator (declarator, param_list, quals,
7413
                                       exception_spec,
7414
                                       /*late_return_type=*/NULL_TREE);
7415
    declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7416
 
7417
    fco = grokmethod (&return_type_specs,
7418
                      declarator,
7419
                      attributes);
7420
    DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7421
    DECL_ARTIFICIAL (fco) = 1;
7422
 
7423
    finish_member_declaration (fco);
7424
 
7425
    obstack_free (&declarator_obstack, p);
7426
  }
7427
}
7428
 
7429
/* Parse the body of a lambda expression, which is simply
7430
 
7431
   compound-statement
7432
 
7433
   but which requires special handling.
7434
   LAMBDA_EXPR is the current representation of the lambda expression.  */
7435
 
7436
static void
7437
cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7438
{
7439
  bool nested = (current_function_decl != NULL_TREE);
7440
  if (nested)
7441
    push_function_context ();
7442
 
7443
  /* Finish the function call operator
7444
     - class_specifier
7445
     + late_parsing_for_member
7446
     + function_definition_after_declarator
7447
     + ctor_initializer_opt_and_function_body  */
7448
  {
7449
    tree fco = lambda_function (lambda_expr);
7450
    tree body;
7451
    bool done = false;
7452
 
7453
    /* Let the front end know that we are going to be defining this
7454
       function.  */
7455
    start_preparsed_function (fco,
7456
                              NULL_TREE,
7457
                              SF_PRE_PARSED | SF_INCLASS_INLINE);
7458
 
7459
    start_lambda_scope (fco);
7460
    body = begin_function_body ();
7461
 
7462
    /* 5.1.1.4 of the standard says:
7463
         If a lambda-expression does not include a trailing-return-type, it
7464
         is as if the trailing-return-type denotes the following type:
7465
          * if the compound-statement is of the form
7466
               { return attribute-specifier [opt] expression ; }
7467
             the type of the returned expression after lvalue-to-rvalue
7468
             conversion (_conv.lval_ 4.1), array-to-pointer conversion
7469
             (_conv.array_ 4.2), and function-to-pointer conversion
7470
             (_conv.func_ 4.3);
7471
          * otherwise, void.  */
7472
 
7473
    /* In a lambda that has neither a lambda-return-type-clause
7474
       nor a deducible form, errors should be reported for return statements
7475
       in the body.  Since we used void as the placeholder return type, parsing
7476
       the body as usual will give such desired behavior.  */
7477
    if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7478
        && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7479
        && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7480
        && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7481
      {
7482
        tree compound_stmt;
7483
        tree expr = NULL_TREE;
7484
        cp_id_kind idk = CP_ID_KIND_NONE;
7485
 
7486
        /* Parse tentatively in case there's more after the initial return
7487
           statement.  */
7488
        cp_parser_parse_tentatively (parser);
7489
 
7490
        cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7491
        cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7492
 
7493
        expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7494
 
7495
        cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7496
        cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7497
 
7498
        if (cp_parser_parse_definitely (parser))
7499
          {
7500
            apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7501
 
7502
            compound_stmt = begin_compound_stmt (0);
7503
            /* Will get error here if type not deduced yet.  */
7504
            finish_return_stmt (expr);
7505
            finish_compound_stmt (compound_stmt);
7506
 
7507
            done = true;
7508
          }
7509
      }
7510
 
7511
    if (!done)
7512
      {
7513
        if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7514
          LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7515
        /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7516
           cp_parser_compound_stmt does not pass it.  */
7517
        cp_parser_function_body (parser);
7518
        LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7519
      }
7520
 
7521
    finish_function_body (body);
7522
    finish_lambda_scope ();
7523
 
7524
    /* Finish the function and generate code for it if necessary.  */
7525
    expand_or_defer_fn (finish_function (/*inline*/2));
7526
  }
7527
 
7528
  if (nested)
7529
    pop_function_context();
7530
}
7531
 
7532
/* Statements [gram.stmt.stmt]  */
7533
 
7534
/* Parse a statement.
7535
 
7536
   statement:
7537
     labeled-statement
7538
     expression-statement
7539
     compound-statement
7540
     selection-statement
7541
     iteration-statement
7542
     jump-statement
7543
     declaration-statement
7544
     try-block
7545
 
7546
  IN_COMPOUND is true when the statement is nested inside a
7547
  cp_parser_compound_statement; this matters for certain pragmas.
7548
 
7549
  If IF_P is not NULL, *IF_P is set to indicate whether the statement
7550
  is a (possibly labeled) if statement which is not enclosed in braces
7551
  and has an else clause.  This is used to implement -Wparentheses.  */
7552
 
7553
static void
7554
cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7555
                     bool in_compound, bool *if_p)
7556
{
7557
  tree statement;
7558
  cp_token *token;
7559
  location_t statement_location;
7560
 
7561
 restart:
7562
  if (if_p != NULL)
7563
    *if_p = false;
7564
  /* There is no statement yet.  */
7565
  statement = NULL_TREE;
7566
  /* Peek at the next token.  */
7567
  token = cp_lexer_peek_token (parser->lexer);
7568
  /* Remember the location of the first token in the statement.  */
7569
  statement_location = token->location;
7570
  /* If this is a keyword, then that will often determine what kind of
7571
     statement we have.  */
7572
  if (token->type == CPP_KEYWORD)
7573
    {
7574
      enum rid keyword = token->keyword;
7575
 
7576
      switch (keyword)
7577
        {
7578
        case RID_CASE:
7579
        case RID_DEFAULT:
7580
          /* Looks like a labeled-statement with a case label.
7581
             Parse the label, and then use tail recursion to parse
7582
             the statement.  */
7583
          cp_parser_label_for_labeled_statement (parser);
7584
          goto restart;
7585
 
7586
        case RID_IF:
7587
        case RID_SWITCH:
7588
          statement = cp_parser_selection_statement (parser, if_p);
7589
          break;
7590
 
7591
        case RID_WHILE:
7592
        case RID_DO:
7593
        case RID_FOR:
7594
          statement = cp_parser_iteration_statement (parser);
7595
          break;
7596
 
7597
        case RID_BREAK:
7598
        case RID_CONTINUE:
7599
        case RID_RETURN:
7600
        case RID_GOTO:
7601
          statement = cp_parser_jump_statement (parser);
7602
          break;
7603
 
7604
          /* Objective-C++ exception-handling constructs.  */
7605
        case RID_AT_TRY:
7606
        case RID_AT_CATCH:
7607
        case RID_AT_FINALLY:
7608
        case RID_AT_SYNCHRONIZED:
7609
        case RID_AT_THROW:
7610
          statement = cp_parser_objc_statement (parser);
7611
          break;
7612
 
7613
        case RID_TRY:
7614
          statement = cp_parser_try_block (parser);
7615
          break;
7616
 
7617
        case RID_NAMESPACE:
7618
          /* This must be a namespace alias definition.  */
7619
          cp_parser_declaration_statement (parser);
7620
          return;
7621
 
7622
        default:
7623
          /* It might be a keyword like `int' that can start a
7624
             declaration-statement.  */
7625
          break;
7626
        }
7627
    }
7628
  else if (token->type == CPP_NAME)
7629
    {
7630
      /* If the next token is a `:', then we are looking at a
7631
         labeled-statement.  */
7632
      token = cp_lexer_peek_nth_token (parser->lexer, 2);
7633
      if (token->type == CPP_COLON)
7634
        {
7635
          /* Looks like a labeled-statement with an ordinary label.
7636
             Parse the label, and then use tail recursion to parse
7637
             the statement.  */
7638
          cp_parser_label_for_labeled_statement (parser);
7639
          goto restart;
7640
        }
7641
    }
7642
  /* Anything that starts with a `{' must be a compound-statement.  */
7643
  else if (token->type == CPP_OPEN_BRACE)
7644
    statement = cp_parser_compound_statement (parser, NULL, false);
7645
  /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7646
     a statement all its own.  */
7647
  else if (token->type == CPP_PRAGMA)
7648
    {
7649
      /* Only certain OpenMP pragmas are attached to statements, and thus
7650
         are considered statements themselves.  All others are not.  In
7651
         the context of a compound, accept the pragma as a "statement" and
7652
         return so that we can check for a close brace.  Otherwise we
7653
         require a real statement and must go back and read one.  */
7654
      if (in_compound)
7655
        cp_parser_pragma (parser, pragma_compound);
7656
      else if (!cp_parser_pragma (parser, pragma_stmt))
7657
        goto restart;
7658
      return;
7659
    }
7660
  else if (token->type == CPP_EOF)
7661
    {
7662
      cp_parser_error (parser, "expected statement");
7663
      return;
7664
    }
7665
 
7666
  /* Everything else must be a declaration-statement or an
7667
     expression-statement.  Try for the declaration-statement
7668
     first, unless we are looking at a `;', in which case we know that
7669
     we have an expression-statement.  */
7670
  if (!statement)
7671
    {
7672
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7673
        {
7674
          cp_parser_parse_tentatively (parser);
7675
          /* Try to parse the declaration-statement.  */
7676
          cp_parser_declaration_statement (parser);
7677
          /* If that worked, we're done.  */
7678
          if (cp_parser_parse_definitely (parser))
7679
            return;
7680
        }
7681
      /* Look for an expression-statement instead.  */
7682
      statement = cp_parser_expression_statement (parser, in_statement_expr);
7683
    }
7684
 
7685
  /* Set the line number for the statement.  */
7686
  if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7687
    SET_EXPR_LOCATION (statement, statement_location);
7688
}
7689
 
7690
/* Parse the label for a labeled-statement, i.e.
7691
 
7692
   identifier :
7693
   case constant-expression :
7694
   default :
7695
 
7696
   GNU Extension:
7697
   case constant-expression ... constant-expression : statement
7698
 
7699
   When a label is parsed without errors, the label is added to the
7700
   parse tree by the finish_* functions, so this function doesn't
7701
   have to return the label.  */
7702
 
7703
static void
7704
cp_parser_label_for_labeled_statement (cp_parser* parser)
7705
{
7706
  cp_token *token;
7707
  tree label = NULL_TREE;
7708
 
7709
  /* The next token should be an identifier.  */
7710
  token = cp_lexer_peek_token (parser->lexer);
7711
  if (token->type != CPP_NAME
7712
      && token->type != CPP_KEYWORD)
7713
    {
7714
      cp_parser_error (parser, "expected labeled-statement");
7715
      return;
7716
    }
7717
 
7718
  switch (token->keyword)
7719
    {
7720
    case RID_CASE:
7721
      {
7722
        tree expr, expr_hi;
7723
        cp_token *ellipsis;
7724
 
7725
        /* Consume the `case' token.  */
7726
        cp_lexer_consume_token (parser->lexer);
7727
        /* Parse the constant-expression.  */
7728
        expr = cp_parser_constant_expression (parser,
7729
                                              /*allow_non_constant_p=*/false,
7730
                                              NULL);
7731
 
7732
        ellipsis = cp_lexer_peek_token (parser->lexer);
7733
        if (ellipsis->type == CPP_ELLIPSIS)
7734
          {
7735
            /* Consume the `...' token.  */
7736
            cp_lexer_consume_token (parser->lexer);
7737
            expr_hi =
7738
              cp_parser_constant_expression (parser,
7739
                                             /*allow_non_constant_p=*/false,
7740
                                             NULL);
7741
            /* We don't need to emit warnings here, as the common code
7742
               will do this for us.  */
7743
          }
7744
        else
7745
          expr_hi = NULL_TREE;
7746
 
7747
        if (parser->in_switch_statement_p)
7748
          finish_case_label (token->location, expr, expr_hi);
7749
        else
7750
          error_at (token->location,
7751
                    "case label %qE not within a switch statement",
7752
                    expr);
7753
      }
7754
      break;
7755
 
7756
    case RID_DEFAULT:
7757
      /* Consume the `default' token.  */
7758
      cp_lexer_consume_token (parser->lexer);
7759
 
7760
      if (parser->in_switch_statement_p)
7761
        finish_case_label (token->location, NULL_TREE, NULL_TREE);
7762
      else
7763
        error_at (token->location, "case label not within a switch statement");
7764
      break;
7765
 
7766
    default:
7767
      /* Anything else must be an ordinary label.  */
7768
      label = finish_label_stmt (cp_parser_identifier (parser));
7769
      break;
7770
    }
7771
 
7772
  /* Require the `:' token.  */
7773
  cp_parser_require (parser, CPP_COLON, "%<:%>");
7774
 
7775
  /* An ordinary label may optionally be followed by attributes.
7776
     However, this is only permitted if the attributes are then
7777
     followed by a semicolon.  This is because, for backward
7778
     compatibility, when parsing
7779
       lab: __attribute__ ((unused)) int i;
7780
     we want the attribute to attach to "i", not "lab".  */
7781
  if (label != NULL_TREE
7782
      && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7783
    {
7784
      tree attrs;
7785
 
7786
      cp_parser_parse_tentatively (parser);
7787
      attrs = cp_parser_attributes_opt (parser);
7788
      if (attrs == NULL_TREE
7789
          || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7790
        cp_parser_abort_tentative_parse (parser);
7791
      else if (!cp_parser_parse_definitely (parser))
7792
        ;
7793
      else
7794
        cplus_decl_attributes (&label, attrs, 0);
7795
    }
7796
}
7797
 
7798
/* Parse an expression-statement.
7799
 
7800
   expression-statement:
7801
     expression [opt] ;
7802
 
7803
   Returns the new EXPR_STMT -- or NULL_TREE if the expression
7804
   statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7805
   indicates whether this expression-statement is part of an
7806
   expression statement.  */
7807
 
7808
static tree
7809
cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7810
{
7811
  tree statement = NULL_TREE;
7812
  cp_token *token = cp_lexer_peek_token (parser->lexer);
7813
 
7814
  /* If the next token is a ';', then there is no expression
7815
     statement.  */
7816
  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7817
    statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7818
 
7819
  /* Give a helpful message for "A<T>::type t;" and the like.  */
7820
  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7821
      && !cp_parser_uncommitted_to_tentative_parse_p (parser))
7822
    {
7823
      if (TREE_CODE (statement) == SCOPE_REF)
7824
        error_at (token->location, "need %<typename%> before %qE because "
7825
                  "%qT is a dependent scope",
7826
                  statement, TREE_OPERAND (statement, 0));
7827
      else if (is_overloaded_fn (statement)
7828
               && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
7829
        {
7830
          /* A::A a; */
7831
          tree fn = get_first_fn (statement);
7832
          error_at (token->location,
7833
                    "%<%T::%D%> names the constructor, not the type",
7834
                    DECL_CONTEXT (fn), DECL_NAME (fn));
7835
        }
7836
    }
7837
 
7838
  /* Consume the final `;'.  */
7839
  cp_parser_consume_semicolon_at_end_of_statement (parser);
7840
 
7841
  if (in_statement_expr
7842
      && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7843
    /* This is the final expression statement of a statement
7844
       expression.  */
7845
    statement = finish_stmt_expr_expr (statement, in_statement_expr);
7846
  else if (statement)
7847
    statement = finish_expr_stmt (statement);
7848
  else
7849
    finish_stmt ();
7850
 
7851
  return statement;
7852
}
7853
 
7854
/* Parse a compound-statement.
7855
 
7856
   compound-statement:
7857
     { statement-seq [opt] }
7858
 
7859
   GNU extension:
7860
 
7861
   compound-statement:
7862
     { label-declaration-seq [opt] statement-seq [opt] }
7863
 
7864
   label-declaration-seq:
7865
     label-declaration
7866
     label-declaration-seq label-declaration
7867
 
7868
   Returns a tree representing the statement.  */
7869
 
7870
static tree
7871
cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7872
                              bool in_try)
7873
{
7874
  tree compound_stmt;
7875
 
7876
  /* Consume the `{'.  */
7877
  if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7878
    return error_mark_node;
7879
  /* Begin the compound-statement.  */
7880
  compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7881
  /* If the next keyword is `__label__' we have a label declaration.  */
7882
  while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7883
    cp_parser_label_declaration (parser);
7884
  /* Parse an (optional) statement-seq.  */
7885
  cp_parser_statement_seq_opt (parser, in_statement_expr);
7886
  /* Finish the compound-statement.  */
7887
  finish_compound_stmt (compound_stmt);
7888
  /* Consume the `}'.  */
7889
  cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7890
 
7891
  return compound_stmt;
7892
}
7893
 
7894
/* Parse an (optional) statement-seq.
7895
 
7896
   statement-seq:
7897
     statement
7898
     statement-seq [opt] statement  */
7899
 
7900
static void
7901
cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7902
{
7903
  /* Scan statements until there aren't any more.  */
7904
  while (true)
7905
    {
7906
      cp_token *token = cp_lexer_peek_token (parser->lexer);
7907
 
7908
      /* If we're looking at a `}', then we've run out of statements.  */
7909
      if (token->type == CPP_CLOSE_BRACE
7910
          || token->type == CPP_EOF
7911
          || token->type == CPP_PRAGMA_EOL)
7912
        break;
7913
 
7914
      /* If we are in a compound statement and find 'else' then
7915
         something went wrong.  */
7916
      else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7917
        {
7918
          if (parser->in_statement & IN_IF_STMT)
7919
            break;
7920
          else
7921
            {
7922
              token = cp_lexer_consume_token (parser->lexer);
7923
              error_at (token->location, "%<else%> without a previous %<if%>");
7924
            }
7925
        }
7926
 
7927
      /* Parse the statement.  */
7928
      cp_parser_statement (parser, in_statement_expr, true, NULL);
7929
    }
7930
}
7931
 
7932
/* Parse a selection-statement.
7933
 
7934
   selection-statement:
7935
     if ( condition ) statement
7936
     if ( condition ) statement else statement
7937
     switch ( condition ) statement
7938
 
7939
   Returns the new IF_STMT or SWITCH_STMT.
7940
 
7941
   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7942
   is a (possibly labeled) if statement which is not enclosed in
7943
   braces and has an else clause.  This is used to implement
7944
   -Wparentheses.  */
7945
 
7946
static tree
7947
cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7948
{
7949
  cp_token *token;
7950
  enum rid keyword;
7951
 
7952
  if (if_p != NULL)
7953
    *if_p = false;
7954
 
7955
  /* Peek at the next token.  */
7956
  token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7957
 
7958
  /* See what kind of keyword it is.  */
7959
  keyword = token->keyword;
7960
  switch (keyword)
7961
    {
7962
    case RID_IF:
7963
    case RID_SWITCH:
7964
      {
7965
        tree statement;
7966
        tree condition;
7967
 
7968
        /* Look for the `('.  */
7969
        if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7970
          {
7971
            cp_parser_skip_to_end_of_statement (parser);
7972
            return error_mark_node;
7973
          }
7974
 
7975
        /* Begin the selection-statement.  */
7976
        if (keyword == RID_IF)
7977
          statement = begin_if_stmt ();
7978
        else
7979
          statement = begin_switch_stmt ();
7980
 
7981
        /* Parse the condition.  */
7982
        condition = cp_parser_condition (parser);
7983
        /* Look for the `)'.  */
7984
        if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7985
          cp_parser_skip_to_closing_parenthesis (parser, true, false,
7986
                                                 /*consume_paren=*/true);
7987
 
7988
        if (keyword == RID_IF)
7989
          {
7990
            bool nested_if;
7991
            unsigned char in_statement;
7992
 
7993
            /* Add the condition.  */
7994
            finish_if_stmt_cond (condition, statement);
7995
 
7996
            /* Parse the then-clause.  */
7997
            in_statement = parser->in_statement;
7998
            parser->in_statement |= IN_IF_STMT;
7999
            if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8000
              {
8001
                location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8002
                add_stmt (build_empty_stmt (loc));
8003
                cp_lexer_consume_token (parser->lexer);
8004
                if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8005
                  warning_at (loc, OPT_Wempty_body, "suggest braces around "
8006
                              "empty body in an %<if%> statement");
8007
                nested_if = false;
8008
              }
8009
            else
8010
              cp_parser_implicitly_scoped_statement (parser, &nested_if);
8011
            parser->in_statement = in_statement;
8012
 
8013
            finish_then_clause (statement);
8014
 
8015
            /* If the next token is `else', parse the else-clause.  */
8016
            if (cp_lexer_next_token_is_keyword (parser->lexer,
8017
                                                RID_ELSE))
8018
              {
8019
                /* Consume the `else' keyword.  */
8020
                cp_lexer_consume_token (parser->lexer);
8021
                begin_else_clause (statement);
8022
                /* Parse the else-clause.  */
8023
                if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8024
                  {
8025
                    location_t loc;
8026
                    loc = cp_lexer_peek_token (parser->lexer)->location;
8027
                    warning_at (loc,
8028
                                OPT_Wempty_body, "suggest braces around "
8029
                                "empty body in an %<else%> statement");
8030
                    add_stmt (build_empty_stmt (loc));
8031
                    cp_lexer_consume_token (parser->lexer);
8032
                  }
8033
                else
8034
                  cp_parser_implicitly_scoped_statement (parser, NULL);
8035
 
8036
                finish_else_clause (statement);
8037
 
8038
                /* If we are currently parsing a then-clause, then
8039
                   IF_P will not be NULL.  We set it to true to
8040
                   indicate that this if statement has an else clause.
8041
                   This may trigger the Wparentheses warning below
8042
                   when we get back up to the parent if statement.  */
8043
                if (if_p != NULL)
8044
                  *if_p = true;
8045
              }
8046
            else
8047
              {
8048
                /* This if statement does not have an else clause.  If
8049
                   NESTED_IF is true, then the then-clause is an if
8050
                   statement which does have an else clause.  We warn
8051
                   about the potential ambiguity.  */
8052
                if (nested_if)
8053
                  warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8054
                              "suggest explicit braces to avoid ambiguous"
8055
                              " %<else%>");
8056
              }
8057
 
8058
            /* Now we're all done with the if-statement.  */
8059
            finish_if_stmt (statement);
8060
          }
8061
        else
8062
          {
8063
            bool in_switch_statement_p;
8064
            unsigned char in_statement;
8065
 
8066
            /* Add the condition.  */
8067
            finish_switch_cond (condition, statement);
8068
 
8069
            /* Parse the body of the switch-statement.  */
8070
            in_switch_statement_p = parser->in_switch_statement_p;
8071
            in_statement = parser->in_statement;
8072
            parser->in_switch_statement_p = true;
8073
            parser->in_statement |= IN_SWITCH_STMT;
8074
            cp_parser_implicitly_scoped_statement (parser, NULL);
8075
            parser->in_switch_statement_p = in_switch_statement_p;
8076
            parser->in_statement = in_statement;
8077
 
8078
            /* Now we're all done with the switch-statement.  */
8079
            finish_switch_stmt (statement);
8080
          }
8081
 
8082
        return statement;
8083
      }
8084
      break;
8085
 
8086
    default:
8087
      cp_parser_error (parser, "expected selection-statement");
8088
      return error_mark_node;
8089
    }
8090
}
8091
 
8092
/* Parse a condition.
8093
 
8094
   condition:
8095
     expression
8096
     type-specifier-seq declarator = initializer-clause
8097
     type-specifier-seq declarator braced-init-list
8098
 
8099
   GNU Extension:
8100
 
8101
   condition:
8102
     type-specifier-seq declarator asm-specification [opt]
8103
       attributes [opt] = assignment-expression
8104
 
8105
   Returns the expression that should be tested.  */
8106
 
8107
static tree
8108
cp_parser_condition (cp_parser* parser)
8109
{
8110
  cp_decl_specifier_seq type_specifiers;
8111
  const char *saved_message;
8112
 
8113
  /* Try the declaration first.  */
8114
  cp_parser_parse_tentatively (parser);
8115
  /* New types are not allowed in the type-specifier-seq for a
8116
     condition.  */
8117
  saved_message = parser->type_definition_forbidden_message;
8118
  parser->type_definition_forbidden_message
8119
    = G_("types may not be defined in conditions");
8120
  /* Parse the type-specifier-seq.  */
8121
  cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8122
                                /*is_trailing_return=*/false,
8123
                                &type_specifiers);
8124
  /* Restore the saved message.  */
8125
  parser->type_definition_forbidden_message = saved_message;
8126
  /* If all is well, we might be looking at a declaration.  */
8127
  if (!cp_parser_error_occurred (parser))
8128
    {
8129
      tree decl;
8130
      tree asm_specification;
8131
      tree attributes;
8132
      cp_declarator *declarator;
8133
      tree initializer = NULL_TREE;
8134
 
8135
      /* Parse the declarator.  */
8136
      declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8137
                                         /*ctor_dtor_or_conv_p=*/NULL,
8138
                                         /*parenthesized_p=*/NULL,
8139
                                         /*member_p=*/false);
8140
      /* Parse the attributes.  */
8141
      attributes = cp_parser_attributes_opt (parser);
8142
      /* Parse the asm-specification.  */
8143
      asm_specification = cp_parser_asm_specification_opt (parser);
8144
      /* If the next token is not an `=' or '{', then we might still be
8145
         looking at an expression.  For example:
8146
 
8147
           if (A(a).x)
8148
 
8149
         looks like a decl-specifier-seq and a declarator -- but then
8150
         there is no `=', so this is an expression.  */
8151
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8152
          && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8153
        cp_parser_simulate_error (parser);
8154
 
8155
      /* If we did see an `=' or '{', then we are looking at a declaration
8156
         for sure.  */
8157
      if (cp_parser_parse_definitely (parser))
8158
        {
8159
          tree pushed_scope;
8160
          bool non_constant_p;
8161
          bool flags = LOOKUP_ONLYCONVERTING;
8162
 
8163
          /* Create the declaration.  */
8164
          decl = start_decl (declarator, &type_specifiers,
8165
                             /*initialized_p=*/true,
8166
                             attributes, /*prefix_attributes=*/NULL_TREE,
8167
                             &pushed_scope);
8168
 
8169
          /* Parse the initializer.  */
8170
          if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8171
            {
8172
              initializer = cp_parser_braced_list (parser, &non_constant_p);
8173
              CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8174
              flags = 0;
8175
            }
8176
          else
8177
            {
8178
              /* Consume the `='.  */
8179
              cp_parser_require (parser, CPP_EQ, "%<=%>");
8180
              initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8181
            }
8182
          if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8183
            maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8184
 
8185
          if (!non_constant_p)
8186
            initializer = fold_non_dependent_expr (initializer);
8187
 
8188
          /* Process the initializer.  */
8189
          cp_finish_decl (decl,
8190
                          initializer, !non_constant_p,
8191
                          asm_specification,
8192
                          flags);
8193
 
8194
          if (pushed_scope)
8195
            pop_scope (pushed_scope);
8196
 
8197
          return convert_from_reference (decl);
8198
        }
8199
    }
8200
  /* If we didn't even get past the declarator successfully, we are
8201
     definitely not looking at a declaration.  */
8202
  else
8203
    cp_parser_abort_tentative_parse (parser);
8204
 
8205
  /* Otherwise, we are looking at an expression.  */
8206
  return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8207
}
8208
 
8209
/* Parse an iteration-statement.
8210
 
8211
   iteration-statement:
8212
     while ( condition ) statement
8213
     do statement while ( expression ) ;
8214
     for ( for-init-statement condition [opt] ; expression [opt] )
8215
       statement
8216
 
8217
   Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
8218
 
8219
static tree
8220
cp_parser_iteration_statement (cp_parser* parser)
8221
{
8222
  cp_token *token;
8223
  enum rid keyword;
8224
  tree statement;
8225
  unsigned char in_statement;
8226
 
8227
  /* Peek at the next token.  */
8228
  token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8229
  if (!token)
8230
    return error_mark_node;
8231
 
8232
  /* Remember whether or not we are already within an iteration
8233
     statement.  */
8234
  in_statement = parser->in_statement;
8235
 
8236
  /* See what kind of keyword it is.  */
8237
  keyword = token->keyword;
8238
  switch (keyword)
8239
    {
8240
    case RID_WHILE:
8241
      {
8242
        tree condition;
8243
 
8244
        /* Begin the while-statement.  */
8245
        statement = begin_while_stmt ();
8246
        /* Look for the `('.  */
8247
        cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8248
        /* Parse the condition.  */
8249
        condition = cp_parser_condition (parser);
8250
        finish_while_stmt_cond (condition, statement);
8251
        /* Look for the `)'.  */
8252
        cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8253
        /* Parse the dependent statement.  */
8254
        parser->in_statement = IN_ITERATION_STMT;
8255
        cp_parser_already_scoped_statement (parser);
8256
        parser->in_statement = in_statement;
8257
        /* We're done with the while-statement.  */
8258
        finish_while_stmt (statement);
8259
      }
8260
      break;
8261
 
8262
    case RID_DO:
8263
      {
8264
        tree expression;
8265
 
8266
        /* Begin the do-statement.  */
8267
        statement = begin_do_stmt ();
8268
        /* Parse the body of the do-statement.  */
8269
        parser->in_statement = IN_ITERATION_STMT;
8270
        cp_parser_implicitly_scoped_statement (parser, NULL);
8271
        parser->in_statement = in_statement;
8272
        finish_do_body (statement);
8273
        /* Look for the `while' keyword.  */
8274
        cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8275
        /* Look for the `('.  */
8276
        cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8277
        /* Parse the expression.  */
8278
        expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8279
        /* We're done with the do-statement.  */
8280
        finish_do_stmt (expression, statement);
8281
        /* Look for the `)'.  */
8282
        cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8283
        /* Look for the `;'.  */
8284
        cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8285
      }
8286
      break;
8287
 
8288
    case RID_FOR:
8289
      {
8290
        tree condition = NULL_TREE;
8291
        tree expression = NULL_TREE;
8292
 
8293
        /* Begin the for-statement.  */
8294
        statement = begin_for_stmt ();
8295
        /* Look for the `('.  */
8296
        cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8297
        /* Parse the initialization.  */
8298
        cp_parser_for_init_statement (parser);
8299
        finish_for_init_stmt (statement);
8300
 
8301
        /* If there's a condition, process it.  */
8302
        if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8303
          condition = cp_parser_condition (parser);
8304
        finish_for_cond (condition, statement);
8305
        /* Look for the `;'.  */
8306
        cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8307
 
8308
        /* If there's an expression, process it.  */
8309
        if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8310
          expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8311
        finish_for_expr (expression, statement);
8312
        /* Look for the `)'.  */
8313
        cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8314
 
8315
        /* Parse the body of the for-statement.  */
8316
        parser->in_statement = IN_ITERATION_STMT;
8317
        cp_parser_already_scoped_statement (parser);
8318
        parser->in_statement = in_statement;
8319
 
8320
        /* We're done with the for-statement.  */
8321
        finish_for_stmt (statement);
8322
      }
8323
      break;
8324
 
8325
    default:
8326
      cp_parser_error (parser, "expected iteration-statement");
8327
      statement = error_mark_node;
8328
      break;
8329
    }
8330
 
8331
  return statement;
8332
}
8333
 
8334
/* Parse a for-init-statement.
8335
 
8336
   for-init-statement:
8337
     expression-statement
8338
     simple-declaration  */
8339
 
8340
static void
8341
cp_parser_for_init_statement (cp_parser* parser)
8342
{
8343
  /* If the next token is a `;', then we have an empty
8344
     expression-statement.  Grammatically, this is also a
8345
     simple-declaration, but an invalid one, because it does not
8346
     declare anything.  Therefore, if we did not handle this case
8347
     specially, we would issue an error message about an invalid
8348
     declaration.  */
8349
  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8350
    {
8351
      /* We're going to speculatively look for a declaration, falling back
8352
         to an expression, if necessary.  */
8353
      cp_parser_parse_tentatively (parser);
8354
      /* Parse the declaration.  */
8355
      cp_parser_simple_declaration (parser,
8356
                                    /*function_definition_allowed_p=*/false);
8357
      /* If the tentative parse failed, then we shall need to look for an
8358
         expression-statement.  */
8359
      if (cp_parser_parse_definitely (parser))
8360
        return;
8361
    }
8362
 
8363
  cp_parser_expression_statement (parser, NULL_TREE);
8364
}
8365
 
8366
/* Parse a jump-statement.
8367
 
8368
   jump-statement:
8369
     break ;
8370
     continue ;
8371
     return expression [opt] ;
8372
     return braced-init-list ;
8373
     goto identifier ;
8374
 
8375
   GNU extension:
8376
 
8377
   jump-statement:
8378
     goto * expression ;
8379
 
8380
   Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
8381
 
8382
static tree
8383
cp_parser_jump_statement (cp_parser* parser)
8384
{
8385
  tree statement = error_mark_node;
8386
  cp_token *token;
8387
  enum rid keyword;
8388
  unsigned char in_statement;
8389
 
8390
  /* Peek at the next token.  */
8391
  token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8392
  if (!token)
8393
    return error_mark_node;
8394
 
8395
  /* See what kind of keyword it is.  */
8396
  keyword = token->keyword;
8397
  switch (keyword)
8398
    {
8399
    case RID_BREAK:
8400
      in_statement = parser->in_statement & ~IN_IF_STMT;
8401
      switch (in_statement)
8402
        {
8403
        case 0:
8404
          error_at (token->location, "break statement not within loop or switch");
8405
          break;
8406
        default:
8407
          gcc_assert ((in_statement & IN_SWITCH_STMT)
8408
                      || in_statement == IN_ITERATION_STMT);
8409
          statement = finish_break_stmt ();
8410
          break;
8411
        case IN_OMP_BLOCK:
8412
          error_at (token->location, "invalid exit from OpenMP structured block");
8413
          break;
8414
        case IN_OMP_FOR:
8415
          error_at (token->location, "break statement used with OpenMP for loop");
8416
          break;
8417
        }
8418
      cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8419
      break;
8420
 
8421
    case RID_CONTINUE:
8422
      switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8423
        {
8424
        case 0:
8425
          error_at (token->location, "continue statement not within a loop");
8426
          break;
8427
        case IN_ITERATION_STMT:
8428
        case IN_OMP_FOR:
8429
          statement = finish_continue_stmt ();
8430
          break;
8431
        case IN_OMP_BLOCK:
8432
          error_at (token->location, "invalid exit from OpenMP structured block");
8433
          break;
8434
        default:
8435
          gcc_unreachable ();
8436
        }
8437
      cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8438
      break;
8439
 
8440
    case RID_RETURN:
8441
      {
8442
        tree expr;
8443
        bool expr_non_constant_p;
8444
 
8445
        if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8446
          {
8447
            maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8448
            expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8449
          }
8450
        else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8451
          expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8452
        else
8453
          /* If the next token is a `;', then there is no
8454
             expression.  */
8455
          expr = NULL_TREE;
8456
        /* Build the return-statement.  */
8457
        statement = finish_return_stmt (expr);
8458
        /* Look for the final `;'.  */
8459
        cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8460
      }
8461
      break;
8462
 
8463
    case RID_GOTO:
8464
      /* Create the goto-statement.  */
8465
      if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8466
        {
8467
          /* Issue a warning about this use of a GNU extension.  */
8468
          pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8469
          /* Consume the '*' token.  */
8470
          cp_lexer_consume_token (parser->lexer);
8471
          /* Parse the dependent expression.  */
8472
          finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8473
        }
8474
      else
8475
        finish_goto_stmt (cp_parser_identifier (parser));
8476
      /* Look for the final `;'.  */
8477
      cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8478
      break;
8479
 
8480
    default:
8481
      cp_parser_error (parser, "expected jump-statement");
8482
      break;
8483
    }
8484
 
8485
  return statement;
8486
}
8487
 
8488
/* Parse a declaration-statement.
8489
 
8490
   declaration-statement:
8491
     block-declaration  */
8492
 
8493
static void
8494
cp_parser_declaration_statement (cp_parser* parser)
8495
{
8496
  void *p;
8497
 
8498
  /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8499
  p = obstack_alloc (&declarator_obstack, 0);
8500
 
8501
 /* Parse the block-declaration.  */
8502
  cp_parser_block_declaration (parser, /*statement_p=*/true);
8503
 
8504
  /* Free any declarators allocated.  */
8505
  obstack_free (&declarator_obstack, p);
8506
 
8507
  /* Finish off the statement.  */
8508
  finish_stmt ();
8509
}
8510
 
8511
/* Some dependent statements (like `if (cond) statement'), are
8512
   implicitly in their own scope.  In other words, if the statement is
8513
   a single statement (as opposed to a compound-statement), it is
8514
   none-the-less treated as if it were enclosed in braces.  Any
8515
   declarations appearing in the dependent statement are out of scope
8516
   after control passes that point.  This function parses a statement,
8517
   but ensures that is in its own scope, even if it is not a
8518
   compound-statement.
8519
 
8520
   If IF_P is not NULL, *IF_P is set to indicate whether the statement
8521
   is a (possibly labeled) if statement which is not enclosed in
8522
   braces and has an else clause.  This is used to implement
8523
   -Wparentheses.
8524
 
8525
   Returns the new statement.  */
8526
 
8527
static tree
8528
cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8529
{
8530
  tree statement;
8531
 
8532
  if (if_p != NULL)
8533
    *if_p = false;
8534
 
8535
  /* Mark if () ; with a special NOP_EXPR.  */
8536
  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8537
    {
8538
      location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8539
      cp_lexer_consume_token (parser->lexer);
8540
      statement = add_stmt (build_empty_stmt (loc));
8541
    }
8542
  /* if a compound is opened, we simply parse the statement directly.  */
8543
  else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8544
    statement = cp_parser_compound_statement (parser, NULL, false);
8545
  /* If the token is not a `{', then we must take special action.  */
8546
  else
8547
    {
8548
      /* Create a compound-statement.  */
8549
      statement = begin_compound_stmt (0);
8550
      /* Parse the dependent-statement.  */
8551
      cp_parser_statement (parser, NULL_TREE, false, if_p);
8552
      /* Finish the dummy compound-statement.  */
8553
      finish_compound_stmt (statement);
8554
    }
8555
 
8556
  /* Return the statement.  */
8557
  return statement;
8558
}
8559
 
8560
/* For some dependent statements (like `while (cond) statement'), we
8561
   have already created a scope.  Therefore, even if the dependent
8562
   statement is a compound-statement, we do not want to create another
8563
   scope.  */
8564
 
8565
static void
8566
cp_parser_already_scoped_statement (cp_parser* parser)
8567
{
8568
  /* If the token is a `{', then we must take special action.  */
8569
  if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8570
    cp_parser_statement (parser, NULL_TREE, false, NULL);
8571
  else
8572
    {
8573
      /* Avoid calling cp_parser_compound_statement, so that we
8574
         don't create a new scope.  Do everything else by hand.  */
8575
      cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8576
      /* If the next keyword is `__label__' we have a label declaration.  */
8577
      while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8578
        cp_parser_label_declaration (parser);
8579
      /* Parse an (optional) statement-seq.  */
8580
      cp_parser_statement_seq_opt (parser, NULL_TREE);
8581
      cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8582
    }
8583
}
8584
 
8585
/* Declarations [gram.dcl.dcl] */
8586
 
8587
/* Parse an optional declaration-sequence.
8588
 
8589
   declaration-seq:
8590
     declaration
8591
     declaration-seq declaration  */
8592
 
8593
static void
8594
cp_parser_declaration_seq_opt (cp_parser* parser)
8595
{
8596
  while (true)
8597
    {
8598
      cp_token *token;
8599
 
8600
      token = cp_lexer_peek_token (parser->lexer);
8601
 
8602
      if (token->type == CPP_CLOSE_BRACE
8603
          || token->type == CPP_EOF
8604
          || token->type == CPP_PRAGMA_EOL)
8605
        break;
8606
 
8607
      if (token->type == CPP_SEMICOLON)
8608
        {
8609
          /* A declaration consisting of a single semicolon is
8610
             invalid.  Allow it unless we're being pedantic.  */
8611
          cp_lexer_consume_token (parser->lexer);
8612
          if (!in_system_header)
8613
            pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8614
          continue;
8615
        }
8616
 
8617
      /* If we're entering or exiting a region that's implicitly
8618
         extern "C", modify the lang context appropriately.  */
8619
      if (!parser->implicit_extern_c && token->implicit_extern_c)
8620
        {
8621
          push_lang_context (lang_name_c);
8622
          parser->implicit_extern_c = true;
8623
        }
8624
      else if (parser->implicit_extern_c && !token->implicit_extern_c)
8625
        {
8626
          pop_lang_context ();
8627
          parser->implicit_extern_c = false;
8628
        }
8629
 
8630
      if (token->type == CPP_PRAGMA)
8631
        {
8632
          /* A top-level declaration can consist solely of a #pragma.
8633
             A nested declaration cannot, so this is done here and not
8634
             in cp_parser_declaration.  (A #pragma at block scope is
8635
             handled in cp_parser_statement.)  */
8636
          cp_parser_pragma (parser, pragma_external);
8637
          continue;
8638
        }
8639
 
8640
      /* Parse the declaration itself.  */
8641
      cp_parser_declaration (parser);
8642
    }
8643
}
8644
 
8645
/* Parse a declaration.
8646
 
8647
   declaration:
8648
     block-declaration
8649
     function-definition
8650
     template-declaration
8651
     explicit-instantiation
8652
     explicit-specialization
8653
     linkage-specification
8654
     namespace-definition
8655
 
8656
   GNU extension:
8657
 
8658
   declaration:
8659
      __extension__ declaration */
8660
 
8661
static void
8662
cp_parser_declaration (cp_parser* parser)
8663
{
8664
  cp_token token1;
8665
  cp_token token2;
8666
  int saved_pedantic;
8667
  void *p;
8668
 
8669
  /* Check for the `__extension__' keyword.  */
8670
  if (cp_parser_extension_opt (parser, &saved_pedantic))
8671
    {
8672
      /* Parse the qualified declaration.  */
8673
      cp_parser_declaration (parser);
8674
      /* Restore the PEDANTIC flag.  */
8675
      pedantic = saved_pedantic;
8676
 
8677
      return;
8678
    }
8679
 
8680
  /* Try to figure out what kind of declaration is present.  */
8681
  token1 = *cp_lexer_peek_token (parser->lexer);
8682
 
8683
  if (token1.type != CPP_EOF)
8684
    token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8685
  else
8686
    {
8687
      token2.type = CPP_EOF;
8688
      token2.keyword = RID_MAX;
8689
    }
8690
 
8691
  /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8692
  p = obstack_alloc (&declarator_obstack, 0);
8693
 
8694
  /* If the next token is `extern' and the following token is a string
8695
     literal, then we have a linkage specification.  */
8696
  if (token1.keyword == RID_EXTERN
8697
      && cp_parser_is_string_literal (&token2))
8698
    cp_parser_linkage_specification (parser);
8699
  /* If the next token is `template', then we have either a template
8700
     declaration, an explicit instantiation, or an explicit
8701
     specialization.  */
8702
  else if (token1.keyword == RID_TEMPLATE)
8703
    {
8704
      /* `template <>' indicates a template specialization.  */
8705
      if (token2.type == CPP_LESS
8706
          && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8707
        cp_parser_explicit_specialization (parser);
8708
      /* `template <' indicates a template declaration.  */
8709
      else if (token2.type == CPP_LESS)
8710
        cp_parser_template_declaration (parser, /*member_p=*/false);
8711
      /* Anything else must be an explicit instantiation.  */
8712
      else
8713
        cp_parser_explicit_instantiation (parser);
8714
    }
8715
  /* If the next token is `export', then we have a template
8716
     declaration.  */
8717
  else if (token1.keyword == RID_EXPORT)
8718
    cp_parser_template_declaration (parser, /*member_p=*/false);
8719
  /* If the next token is `extern', 'static' or 'inline' and the one
8720
     after that is `template', we have a GNU extended explicit
8721
     instantiation directive.  */
8722
  else if (cp_parser_allow_gnu_extensions_p (parser)
8723
           && (token1.keyword == RID_EXTERN
8724
               || token1.keyword == RID_STATIC
8725
               || token1.keyword == RID_INLINE)
8726
           && token2.keyword == RID_TEMPLATE)
8727
    cp_parser_explicit_instantiation (parser);
8728
  /* If the next token is `namespace', check for a named or unnamed
8729
     namespace definition.  */
8730
  else if (token1.keyword == RID_NAMESPACE
8731
           && (/* A named namespace definition.  */
8732
               (token2.type == CPP_NAME
8733
                && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8734
                    != CPP_EQ))
8735
               /* An unnamed namespace definition.  */
8736
               || token2.type == CPP_OPEN_BRACE
8737
               || token2.keyword == RID_ATTRIBUTE))
8738
    cp_parser_namespace_definition (parser);
8739
  /* An inline (associated) namespace definition.  */
8740
  else if (token1.keyword == RID_INLINE
8741
           && token2.keyword == RID_NAMESPACE)
8742
    cp_parser_namespace_definition (parser);
8743
  /* Objective-C++ declaration/definition.  */
8744
  else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8745
    cp_parser_objc_declaration (parser);
8746
  /* We must have either a block declaration or a function
8747
     definition.  */
8748
  else
8749
    /* Try to parse a block-declaration, or a function-definition.  */
8750
    cp_parser_block_declaration (parser, /*statement_p=*/false);
8751
 
8752
  /* Free any declarators allocated.  */
8753
  obstack_free (&declarator_obstack, p);
8754
}
8755
 
8756
/* Parse a block-declaration.
8757
 
8758
   block-declaration:
8759
     simple-declaration
8760
     asm-definition
8761
     namespace-alias-definition
8762
     using-declaration
8763
     using-directive
8764
 
8765
   GNU Extension:
8766
 
8767
   block-declaration:
8768
     __extension__ block-declaration
8769
 
8770
   C++0x Extension:
8771
 
8772
   block-declaration:
8773
     static_assert-declaration
8774
 
8775
   If STATEMENT_P is TRUE, then this block-declaration is occurring as
8776
   part of a declaration-statement.  */
8777
 
8778
static void
8779
cp_parser_block_declaration (cp_parser *parser,
8780
                             bool      statement_p)
8781
{
8782
  cp_token *token1;
8783
  int saved_pedantic;
8784
 
8785
  /* Check for the `__extension__' keyword.  */
8786
  if (cp_parser_extension_opt (parser, &saved_pedantic))
8787
    {
8788
      /* Parse the qualified declaration.  */
8789
      cp_parser_block_declaration (parser, statement_p);
8790
      /* Restore the PEDANTIC flag.  */
8791
      pedantic = saved_pedantic;
8792
 
8793
      return;
8794
    }
8795
 
8796
  /* Peek at the next token to figure out which kind of declaration is
8797
     present.  */
8798
  token1 = cp_lexer_peek_token (parser->lexer);
8799
 
8800
  /* If the next keyword is `asm', we have an asm-definition.  */
8801
  if (token1->keyword == RID_ASM)
8802
    {
8803
      if (statement_p)
8804
        cp_parser_commit_to_tentative_parse (parser);
8805
      cp_parser_asm_definition (parser);
8806
    }
8807
  /* If the next keyword is `namespace', we have a
8808
     namespace-alias-definition.  */
8809
  else if (token1->keyword == RID_NAMESPACE)
8810
    cp_parser_namespace_alias_definition (parser);
8811
  /* If the next keyword is `using', we have either a
8812
     using-declaration or a using-directive.  */
8813
  else if (token1->keyword == RID_USING)
8814
    {
8815
      cp_token *token2;
8816
 
8817
      if (statement_p)
8818
        cp_parser_commit_to_tentative_parse (parser);
8819
      /* If the token after `using' is `namespace', then we have a
8820
         using-directive.  */
8821
      token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8822
      if (token2->keyword == RID_NAMESPACE)
8823
        cp_parser_using_directive (parser);
8824
      /* Otherwise, it's a using-declaration.  */
8825
      else
8826
        cp_parser_using_declaration (parser,
8827
                                     /*access_declaration_p=*/false);
8828
    }
8829
  /* If the next keyword is `__label__' we have a misplaced label
8830
     declaration.  */
8831
  else if (token1->keyword == RID_LABEL)
8832
    {
8833
      cp_lexer_consume_token (parser->lexer);
8834
      error_at (token1->location, "%<__label__%> not at the beginning of a block");
8835
      cp_parser_skip_to_end_of_statement (parser);
8836
      /* If the next token is now a `;', consume it.  */
8837
      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8838
        cp_lexer_consume_token (parser->lexer);
8839
    }
8840
  /* If the next token is `static_assert' we have a static assertion.  */
8841
  else if (token1->keyword == RID_STATIC_ASSERT)
8842
    cp_parser_static_assert (parser, /*member_p=*/false);
8843
  /* Anything else must be a simple-declaration.  */
8844
  else
8845
    cp_parser_simple_declaration (parser, !statement_p);
8846
}
8847
 
8848
/* Parse a simple-declaration.
8849
 
8850
   simple-declaration:
8851
     decl-specifier-seq [opt] init-declarator-list [opt] ;
8852
 
8853
   init-declarator-list:
8854
     init-declarator
8855
     init-declarator-list , init-declarator
8856
 
8857
   If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8858
   function-definition as a simple-declaration.  */
8859
 
8860
static void
8861
cp_parser_simple_declaration (cp_parser* parser,
8862
                              bool function_definition_allowed_p)
8863
{
8864
  cp_decl_specifier_seq decl_specifiers;
8865
  int declares_class_or_enum;
8866
  bool saw_declarator;
8867
 
8868
  /* Defer access checks until we know what is being declared; the
8869
     checks for names appearing in the decl-specifier-seq should be
8870
     done as if we were in the scope of the thing being declared.  */
8871
  push_deferring_access_checks (dk_deferred);
8872
 
8873
  /* Parse the decl-specifier-seq.  We have to keep track of whether
8874
     or not the decl-specifier-seq declares a named class or
8875
     enumeration type, since that is the only case in which the
8876
     init-declarator-list is allowed to be empty.
8877
 
8878
     [dcl.dcl]
8879
 
8880
     In a simple-declaration, the optional init-declarator-list can be
8881
     omitted only when declaring a class or enumeration, that is when
8882
     the decl-specifier-seq contains either a class-specifier, an
8883
     elaborated-type-specifier, or an enum-specifier.  */
8884
  cp_parser_decl_specifier_seq (parser,
8885
                                CP_PARSER_FLAGS_OPTIONAL,
8886
                                &decl_specifiers,
8887
                                &declares_class_or_enum);
8888
  /* We no longer need to defer access checks.  */
8889
  stop_deferring_access_checks ();
8890
 
8891
  /* In a block scope, a valid declaration must always have a
8892
     decl-specifier-seq.  By not trying to parse declarators, we can
8893
     resolve the declaration/expression ambiguity more quickly.  */
8894
  if (!function_definition_allowed_p
8895
      && !decl_specifiers.any_specifiers_p)
8896
    {
8897
      cp_parser_error (parser, "expected declaration");
8898
      goto done;
8899
    }
8900
 
8901
  /* If the next two tokens are both identifiers, the code is
8902
     erroneous. The usual cause of this situation is code like:
8903
 
8904
       T t;
8905
 
8906
     where "T" should name a type -- but does not.  */
8907
  if (!decl_specifiers.any_type_specifiers_p
8908
      && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8909
    {
8910
      /* If parsing tentatively, we should commit; we really are
8911
         looking at a declaration.  */
8912
      cp_parser_commit_to_tentative_parse (parser);
8913
      /* Give up.  */
8914
      goto done;
8915
    }
8916
 
8917
  /* If we have seen at least one decl-specifier, and the next token
8918
     is not a parenthesis, then we must be looking at a declaration.
8919
     (After "int (" we might be looking at a functional cast.)  */
8920
  if (decl_specifiers.any_specifiers_p
8921
      && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8922
      && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8923
      && !cp_parser_error_occurred (parser))
8924
    cp_parser_commit_to_tentative_parse (parser);
8925
 
8926
  /* Keep going until we hit the `;' at the end of the simple
8927
     declaration.  */
8928
  saw_declarator = false;
8929
  while (cp_lexer_next_token_is_not (parser->lexer,
8930
                                     CPP_SEMICOLON))
8931
    {
8932
      cp_token *token;
8933
      bool function_definition_p;
8934
      tree decl;
8935
 
8936
      if (saw_declarator)
8937
        {
8938
          /* If we are processing next declarator, coma is expected */
8939
          token = cp_lexer_peek_token (parser->lexer);
8940
          gcc_assert (token->type == CPP_COMMA);
8941
          cp_lexer_consume_token (parser->lexer);
8942
        }
8943
      else
8944
        saw_declarator = true;
8945
 
8946
      /* Parse the init-declarator.  */
8947
      decl = cp_parser_init_declarator (parser, &decl_specifiers,
8948
                                        /*checks=*/NULL,
8949
                                        function_definition_allowed_p,
8950
                                        /*member_p=*/false,
8951
                                        declares_class_or_enum,
8952
                                        &function_definition_p);
8953
      /* If an error occurred while parsing tentatively, exit quickly.
8954
         (That usually happens when in the body of a function; each
8955
         statement is treated as a declaration-statement until proven
8956
         otherwise.)  */
8957
      if (cp_parser_error_occurred (parser))
8958
        goto done;
8959
      /* Handle function definitions specially.  */
8960
      if (function_definition_p)
8961
        {
8962
          /* If the next token is a `,', then we are probably
8963
             processing something like:
8964
 
8965
               void f() {}, *p;
8966
 
8967
             which is erroneous.  */
8968
          if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8969
            {
8970
              cp_token *token = cp_lexer_peek_token (parser->lexer);
8971
              error_at (token->location,
8972
                        "mixing"
8973
                        " declarations and function-definitions is forbidden");
8974
            }
8975
          /* Otherwise, we're done with the list of declarators.  */
8976
          else
8977
            {
8978
              pop_deferring_access_checks ();
8979
              return;
8980
            }
8981
        }
8982
      /* The next token should be either a `,' or a `;'.  */
8983
      token = cp_lexer_peek_token (parser->lexer);
8984
      /* If it's a `,', there are more declarators to come.  */
8985
      if (token->type == CPP_COMMA)
8986
        /* will be consumed next time around */;
8987
      /* If it's a `;', we are done.  */
8988
      else if (token->type == CPP_SEMICOLON)
8989
        break;
8990
      /* Anything else is an error.  */
8991
      else
8992
        {
8993
          /* If we have already issued an error message we don't need
8994
             to issue another one.  */
8995
          if (decl != error_mark_node
8996
              || cp_parser_uncommitted_to_tentative_parse_p (parser))
8997
            cp_parser_error (parser, "expected %<,%> or %<;%>");
8998
          /* Skip tokens until we reach the end of the statement.  */
8999
          cp_parser_skip_to_end_of_statement (parser);
9000
          /* If the next token is now a `;', consume it.  */
9001
          if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9002
            cp_lexer_consume_token (parser->lexer);
9003
          goto done;
9004
        }
9005
      /* After the first time around, a function-definition is not
9006
         allowed -- even if it was OK at first.  For example:
9007
 
9008
           int i, f() {}
9009
 
9010
         is not valid.  */
9011
      function_definition_allowed_p = false;
9012
    }
9013
 
9014
  /* Issue an error message if no declarators are present, and the
9015
     decl-specifier-seq does not itself declare a class or
9016
     enumeration.  */
9017
  if (!saw_declarator)
9018
    {
9019
      if (cp_parser_declares_only_class_p (parser))
9020
        shadow_tag (&decl_specifiers);
9021
      /* Perform any deferred access checks.  */
9022
      perform_deferred_access_checks ();
9023
    }
9024
 
9025
  /* Consume the `;'.  */
9026
  cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9027
 
9028
 done:
9029
  pop_deferring_access_checks ();
9030
}
9031
 
9032
/* Parse a decl-specifier-seq.
9033
 
9034
   decl-specifier-seq:
9035
     decl-specifier-seq [opt] decl-specifier
9036
 
9037
   decl-specifier:
9038
     storage-class-specifier
9039
     type-specifier
9040
     function-specifier
9041
     friend
9042
     typedef
9043
 
9044
   GNU Extension:
9045
 
9046
   decl-specifier:
9047
     attributes
9048
 
9049
   Set *DECL_SPECS to a representation of the decl-specifier-seq.
9050
 
9051
   The parser flags FLAGS is used to control type-specifier parsing.
9052
 
9053
   *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9054
   flags:
9055
 
9056
     1: one of the decl-specifiers is an elaborated-type-specifier
9057
        (i.e., a type declaration)
9058
     2: one of the decl-specifiers is an enum-specifier or a
9059
        class-specifier (i.e., a type definition)
9060
 
9061
   */
9062
 
9063
static void
9064
cp_parser_decl_specifier_seq (cp_parser* parser,
9065
                              cp_parser_flags flags,
9066
                              cp_decl_specifier_seq *decl_specs,
9067
                              int* declares_class_or_enum)
9068
{
9069
  bool constructor_possible_p = !parser->in_declarator_p;
9070
  cp_token *start_token = NULL;
9071
 
9072
  /* Clear DECL_SPECS.  */
9073
  clear_decl_specs (decl_specs);
9074
 
9075
  /* Assume no class or enumeration type is declared.  */
9076
  *declares_class_or_enum = 0;
9077
 
9078
  /* Keep reading specifiers until there are no more to read.  */
9079
  while (true)
9080
    {
9081
      bool constructor_p;
9082
      bool found_decl_spec;
9083
      cp_token *token;
9084
 
9085
      /* Peek at the next token.  */
9086
      token = cp_lexer_peek_token (parser->lexer);
9087
 
9088
      /* Save the first token of the decl spec list for error
9089
         reporting.  */
9090
      if (!start_token)
9091
        start_token = token;
9092
      /* Handle attributes.  */
9093
      if (token->keyword == RID_ATTRIBUTE)
9094
        {
9095
          /* Parse the attributes.  */
9096
          decl_specs->attributes
9097
            = chainon (decl_specs->attributes,
9098
                       cp_parser_attributes_opt (parser));
9099
          continue;
9100
        }
9101
      /* Assume we will find a decl-specifier keyword.  */
9102
      found_decl_spec = true;
9103
      /* If the next token is an appropriate keyword, we can simply
9104
         add it to the list.  */
9105
      switch (token->keyword)
9106
        {
9107
          /* decl-specifier:
9108
               friend
9109
               constexpr */
9110
        case RID_FRIEND:
9111
          if (!at_class_scope_p ())
9112
            {
9113
              error_at (token->location, "%<friend%> used outside of class");
9114
              cp_lexer_purge_token (parser->lexer);
9115
            }
9116
          else
9117
            {
9118
              ++decl_specs->specs[(int) ds_friend];
9119
              /* Consume the token.  */
9120
              cp_lexer_consume_token (parser->lexer);
9121
            }
9122
          break;
9123
 
9124
        case RID_CONSTEXPR:
9125
          ++decl_specs->specs[(int) ds_constexpr];
9126
          cp_lexer_consume_token (parser->lexer);
9127
          break;
9128
 
9129
          /* function-specifier:
9130
               inline
9131
               virtual
9132
               explicit  */
9133
        case RID_INLINE:
9134
        case RID_VIRTUAL:
9135
        case RID_EXPLICIT:
9136
          cp_parser_function_specifier_opt (parser, decl_specs);
9137
          break;
9138
 
9139
          /* decl-specifier:
9140
               typedef  */
9141
        case RID_TYPEDEF:
9142
          ++decl_specs->specs[(int) ds_typedef];
9143
          /* Consume the token.  */
9144
          cp_lexer_consume_token (parser->lexer);
9145
          /* A constructor declarator cannot appear in a typedef.  */
9146
          constructor_possible_p = false;
9147
          /* The "typedef" keyword can only occur in a declaration; we
9148
             may as well commit at this point.  */
9149
          cp_parser_commit_to_tentative_parse (parser);
9150
 
9151
          if (decl_specs->storage_class != sc_none)
9152
            decl_specs->conflicting_specifiers_p = true;
9153
          break;
9154
 
9155
          /* storage-class-specifier:
9156
               auto
9157
               register
9158
               static
9159
               extern
9160
               mutable
9161
 
9162
             GNU Extension:
9163
               thread  */
9164
        case RID_AUTO:
9165
          if (cxx_dialect == cxx98)
9166
            {
9167
              /* Consume the token.  */
9168
              cp_lexer_consume_token (parser->lexer);
9169
 
9170
              /* Complain about `auto' as a storage specifier, if
9171
                 we're complaining about C++0x compatibility.  */
9172
              warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9173
                          " will change meaning in C++0x; please remove it");
9174
 
9175
              /* Set the storage class anyway.  */
9176
              cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9177
                                           token->location);
9178
            }
9179
          else
9180
            /* C++0x auto type-specifier.  */
9181
            found_decl_spec = false;
9182
          break;
9183
 
9184
        case RID_REGISTER:
9185
        case RID_STATIC:
9186
        case RID_EXTERN:
9187
        case RID_MUTABLE:
9188
          /* Consume the token.  */
9189
          cp_lexer_consume_token (parser->lexer);
9190
          cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9191
                                       token->location);
9192
          break;
9193
        case RID_THREAD:
9194
          /* Consume the token.  */
9195
          cp_lexer_consume_token (parser->lexer);
9196
          ++decl_specs->specs[(int) ds_thread];
9197
          break;
9198
 
9199
        default:
9200
          /* We did not yet find a decl-specifier yet.  */
9201
          found_decl_spec = false;
9202
          break;
9203
        }
9204
 
9205
      /* Constructors are a special case.  The `S' in `S()' is not a
9206
         decl-specifier; it is the beginning of the declarator.  */
9207
      constructor_p
9208
        = (!found_decl_spec
9209
           && constructor_possible_p
9210
           && (cp_parser_constructor_declarator_p
9211
               (parser, decl_specs->specs[(int) ds_friend] != 0)));
9212
 
9213
      /* If we don't have a DECL_SPEC yet, then we must be looking at
9214
         a type-specifier.  */
9215
      if (!found_decl_spec && !constructor_p)
9216
        {
9217
          int decl_spec_declares_class_or_enum;
9218
          bool is_cv_qualifier;
9219
          tree type_spec;
9220
 
9221
          type_spec
9222
            = cp_parser_type_specifier (parser, flags,
9223
                                        decl_specs,
9224
                                        /*is_declaration=*/true,
9225
                                        &decl_spec_declares_class_or_enum,
9226
                                        &is_cv_qualifier);
9227
          *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9228
 
9229
          /* If this type-specifier referenced a user-defined type
9230
             (a typedef, class-name, etc.), then we can't allow any
9231
             more such type-specifiers henceforth.
9232
 
9233
             [dcl.spec]
9234
 
9235
             The longest sequence of decl-specifiers that could
9236
             possibly be a type name is taken as the
9237
             decl-specifier-seq of a declaration.  The sequence shall
9238
             be self-consistent as described below.
9239
 
9240
             [dcl.type]
9241
 
9242
             As a general rule, at most one type-specifier is allowed
9243
             in the complete decl-specifier-seq of a declaration.  The
9244
             only exceptions are the following:
9245
 
9246
             -- const or volatile can be combined with any other
9247
                type-specifier.
9248
 
9249
             -- signed or unsigned can be combined with char, long,
9250
                short, or int.
9251
 
9252
             -- ..
9253
 
9254
             Example:
9255
 
9256
               typedef char* Pc;
9257
               void g (const int Pc);
9258
 
9259
             Here, Pc is *not* part of the decl-specifier seq; it's
9260
             the declarator.  Therefore, once we see a type-specifier
9261
             (other than a cv-qualifier), we forbid any additional
9262
             user-defined types.  We *do* still allow things like `int
9263
             int' to be considered a decl-specifier-seq, and issue the
9264
             error message later.  */
9265
          if (type_spec && !is_cv_qualifier)
9266
            flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9267
          /* A constructor declarator cannot follow a type-specifier.  */
9268
          if (type_spec)
9269
            {
9270
              constructor_possible_p = false;
9271
              found_decl_spec = true;
9272
              if (!is_cv_qualifier)
9273
                decl_specs->any_type_specifiers_p = true;
9274
            }
9275
        }
9276
 
9277
      /* If we still do not have a DECL_SPEC, then there are no more
9278
         decl-specifiers.  */
9279
      if (!found_decl_spec)
9280
        break;
9281
 
9282
      decl_specs->any_specifiers_p = true;
9283
      /* After we see one decl-specifier, further decl-specifiers are
9284
         always optional.  */
9285
      flags |= CP_PARSER_FLAGS_OPTIONAL;
9286
    }
9287
 
9288
  cp_parser_check_decl_spec (decl_specs, start_token->location);
9289
 
9290
  /* Don't allow a friend specifier with a class definition.  */
9291
  if (decl_specs->specs[(int) ds_friend] != 0
9292
      && (*declares_class_or_enum & 2))
9293
    error_at (start_token->location,
9294
              "class definition may not be declared a friend");
9295
}
9296
 
9297
/* Parse an (optional) storage-class-specifier.
9298
 
9299
   storage-class-specifier:
9300
     auto
9301
     register
9302
     static
9303
     extern
9304
     mutable
9305
 
9306
   GNU Extension:
9307
 
9308
   storage-class-specifier:
9309
     thread
9310
 
9311
   Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9312
 
9313
static tree
9314
cp_parser_storage_class_specifier_opt (cp_parser* parser)
9315
{
9316
  switch (cp_lexer_peek_token (parser->lexer)->keyword)
9317
    {
9318
    case RID_AUTO:
9319
      if (cxx_dialect != cxx98)
9320
        return NULL_TREE;
9321
      /* Fall through for C++98.  */
9322
 
9323
    case RID_REGISTER:
9324
    case RID_STATIC:
9325
    case RID_EXTERN:
9326
    case RID_MUTABLE:
9327
    case RID_THREAD:
9328
      /* Consume the token.  */
9329
      return cp_lexer_consume_token (parser->lexer)->u.value;
9330
 
9331
    default:
9332
      return NULL_TREE;
9333
    }
9334
}
9335
 
9336
/* Parse an (optional) function-specifier.
9337
 
9338
   function-specifier:
9339
     inline
9340
     virtual
9341
     explicit
9342
 
9343
   Returns an IDENTIFIER_NODE corresponding to the keyword used.
9344
   Updates DECL_SPECS, if it is non-NULL.  */
9345
 
9346
static tree
9347
cp_parser_function_specifier_opt (cp_parser* parser,
9348
                                  cp_decl_specifier_seq *decl_specs)
9349
{
9350
  cp_token *token = cp_lexer_peek_token (parser->lexer);
9351
  switch (token->keyword)
9352
    {
9353
    case RID_INLINE:
9354
      if (decl_specs)
9355
        ++decl_specs->specs[(int) ds_inline];
9356
      break;
9357
 
9358
    case RID_VIRTUAL:
9359
      /* 14.5.2.3 [temp.mem]
9360
 
9361
         A member function template shall not be virtual.  */
9362
      if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9363
        error_at (token->location, "templates may not be %<virtual%>");
9364
      else if (decl_specs)
9365
        ++decl_specs->specs[(int) ds_virtual];
9366
      break;
9367
 
9368
    case RID_EXPLICIT:
9369
      if (decl_specs)
9370
        ++decl_specs->specs[(int) ds_explicit];
9371
      break;
9372
 
9373
    default:
9374
      return NULL_TREE;
9375
    }
9376
 
9377
  /* Consume the token.  */
9378
  return cp_lexer_consume_token (parser->lexer)->u.value;
9379
}
9380
 
9381
/* Parse a linkage-specification.
9382
 
9383
   linkage-specification:
9384
     extern string-literal { declaration-seq [opt] }
9385
     extern string-literal declaration  */
9386
 
9387
static void
9388
cp_parser_linkage_specification (cp_parser* parser)
9389
{
9390
  tree linkage;
9391
 
9392
  /* Look for the `extern' keyword.  */
9393
  cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9394
 
9395
  /* Look for the string-literal.  */
9396
  linkage = cp_parser_string_literal (parser, false, false);
9397
 
9398
  /* Transform the literal into an identifier.  If the literal is a
9399
     wide-character string, or contains embedded NULs, then we can't
9400
     handle it as the user wants.  */
9401
  if (strlen (TREE_STRING_POINTER (linkage))
9402
      != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9403
    {
9404
      cp_parser_error (parser, "invalid linkage-specification");
9405
      /* Assume C++ linkage.  */
9406
      linkage = lang_name_cplusplus;
9407
    }
9408
  else
9409
    linkage = get_identifier (TREE_STRING_POINTER (linkage));
9410
 
9411
  /* We're now using the new linkage.  */
9412
  push_lang_context (linkage);
9413
 
9414
  /* If the next token is a `{', then we're using the first
9415
     production.  */
9416
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9417
    {
9418
      /* Consume the `{' token.  */
9419
      cp_lexer_consume_token (parser->lexer);
9420
      /* Parse the declarations.  */
9421
      cp_parser_declaration_seq_opt (parser);
9422
      /* Look for the closing `}'.  */
9423
      cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9424
    }
9425
  /* Otherwise, there's just one declaration.  */
9426
  else
9427
    {
9428
      bool saved_in_unbraced_linkage_specification_p;
9429
 
9430
      saved_in_unbraced_linkage_specification_p
9431
        = parser->in_unbraced_linkage_specification_p;
9432
      parser->in_unbraced_linkage_specification_p = true;
9433
      cp_parser_declaration (parser);
9434
      parser->in_unbraced_linkage_specification_p
9435
        = saved_in_unbraced_linkage_specification_p;
9436
    }
9437
 
9438
  /* We're done with the linkage-specification.  */
9439
  pop_lang_context ();
9440
}
9441
 
9442
/* Parse a static_assert-declaration.
9443
 
9444
   static_assert-declaration:
9445
     static_assert ( constant-expression , string-literal ) ;
9446
 
9447
   If MEMBER_P, this static_assert is a class member.  */
9448
 
9449
static void
9450
cp_parser_static_assert(cp_parser *parser, bool member_p)
9451
{
9452
  tree condition;
9453
  tree message;
9454
  cp_token *token;
9455
  location_t saved_loc;
9456
 
9457
  /* Peek at the `static_assert' token so we can keep track of exactly
9458
     where the static assertion started.  */
9459
  token = cp_lexer_peek_token (parser->lexer);
9460
  saved_loc = token->location;
9461
 
9462
  /* Look for the `static_assert' keyword.  */
9463
  if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9464
                                  "%<static_assert%>"))
9465
    return;
9466
 
9467
  /*  We know we are in a static assertion; commit to any tentative
9468
      parse.  */
9469
  if (cp_parser_parsing_tentatively (parser))
9470
    cp_parser_commit_to_tentative_parse (parser);
9471
 
9472
  /* Parse the `(' starting the static assertion condition.  */
9473
  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9474
 
9475
  /* Parse the constant-expression.  */
9476
  condition =
9477
    cp_parser_constant_expression (parser,
9478
                                   /*allow_non_constant_p=*/false,
9479
                                   /*non_constant_p=*/NULL);
9480
 
9481
  /* Parse the separating `,'.  */
9482
  cp_parser_require (parser, CPP_COMMA, "%<,%>");
9483
 
9484
  /* Parse the string-literal message.  */
9485
  message = cp_parser_string_literal (parser,
9486
                                      /*translate=*/false,
9487
                                      /*wide_ok=*/true);
9488
 
9489
  /* A `)' completes the static assertion.  */
9490
  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9491
    cp_parser_skip_to_closing_parenthesis (parser,
9492
                                           /*recovering=*/true,
9493
                                           /*or_comma=*/false,
9494
                                           /*consume_paren=*/true);
9495
 
9496
  /* A semicolon terminates the declaration.  */
9497
  cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9498
 
9499
  /* Complete the static assertion, which may mean either processing
9500
     the static assert now or saving it for template instantiation.  */
9501
  finish_static_assert (condition, message, saved_loc, member_p);
9502
}
9503
 
9504
/* Parse a `decltype' type. Returns the type.
9505
 
9506
   simple-type-specifier:
9507
     decltype ( expression )  */
9508
 
9509
static tree
9510
cp_parser_decltype (cp_parser *parser)
9511
{
9512
  tree expr;
9513
  bool id_expression_or_member_access_p = false;
9514
  const char *saved_message;
9515
  bool saved_integral_constant_expression_p;
9516
  bool saved_non_integral_constant_expression_p;
9517
  cp_token *id_expr_start_token;
9518
 
9519
  /* Look for the `decltype' token.  */
9520
  if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9521
    return error_mark_node;
9522
 
9523
  /* Types cannot be defined in a `decltype' expression.  Save away the
9524
     old message.  */
9525
  saved_message = parser->type_definition_forbidden_message;
9526
 
9527
  /* And create the new one.  */
9528
  parser->type_definition_forbidden_message
9529
    = G_("types may not be defined in %<decltype%> expressions");
9530
 
9531
  /* The restrictions on constant-expressions do not apply inside
9532
     decltype expressions.  */
9533
  saved_integral_constant_expression_p
9534
    = parser->integral_constant_expression_p;
9535
  saved_non_integral_constant_expression_p
9536
    = parser->non_integral_constant_expression_p;
9537
  parser->integral_constant_expression_p = false;
9538
 
9539
  /* Do not actually evaluate the expression.  */
9540
  ++cp_unevaluated_operand;
9541
 
9542
  /* Do not warn about problems with the expression.  */
9543
  ++c_inhibit_evaluation_warnings;
9544
 
9545
  /* Parse the opening `('.  */
9546
  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9547
    return error_mark_node;
9548
 
9549
  /* First, try parsing an id-expression.  */
9550
  id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9551
  cp_parser_parse_tentatively (parser);
9552
  expr = cp_parser_id_expression (parser,
9553
                                  /*template_keyword_p=*/false,
9554
                                  /*check_dependency_p=*/true,
9555
                                  /*template_p=*/NULL,
9556
                                  /*declarator_p=*/false,
9557
                                  /*optional_p=*/false);
9558
 
9559
  if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9560
    {
9561
      bool non_integral_constant_expression_p = false;
9562
      tree id_expression = expr;
9563
      cp_id_kind idk;
9564
      const char *error_msg;
9565
 
9566
      if (TREE_CODE (expr) == IDENTIFIER_NODE)
9567
        /* Lookup the name we got back from the id-expression.  */
9568
        expr = cp_parser_lookup_name (parser, expr,
9569
                                      none_type,
9570
                                      /*is_template=*/false,
9571
                                      /*is_namespace=*/false,
9572
                                      /*check_dependency=*/true,
9573
                                      /*ambiguous_decls=*/NULL,
9574
                                      id_expr_start_token->location);
9575
 
9576
      if (expr
9577
          && expr != error_mark_node
9578
          && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9579
          && TREE_CODE (expr) != TYPE_DECL
9580
          && (TREE_CODE (expr) != BIT_NOT_EXPR
9581
              || !TYPE_P (TREE_OPERAND (expr, 0)))
9582
          && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9583
        {
9584
          /* Complete lookup of the id-expression.  */
9585
          expr = (finish_id_expression
9586
                  (id_expression, expr, parser->scope, &idk,
9587
                   /*integral_constant_expression_p=*/false,
9588
                   /*allow_non_integral_constant_expression_p=*/true,
9589
                   &non_integral_constant_expression_p,
9590
                   /*template_p=*/false,
9591
                   /*done=*/true,
9592
                   /*address_p=*/false,
9593
                   /*template_arg_p=*/false,
9594
                   &error_msg,
9595
                   id_expr_start_token->location));
9596
 
9597
          if (expr == error_mark_node)
9598
            /* We found an id-expression, but it was something that we
9599
               should not have found. This is an error, not something
9600
               we can recover from, so note that we found an
9601
               id-expression and we'll recover as gracefully as
9602
               possible.  */
9603
            id_expression_or_member_access_p = true;
9604
        }
9605
 
9606
      if (expr
9607
          && expr != error_mark_node
9608
          && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9609
        /* We have an id-expression.  */
9610
        id_expression_or_member_access_p = true;
9611
    }
9612
 
9613
  if (!id_expression_or_member_access_p)
9614
    {
9615
      /* Abort the id-expression parse.  */
9616
      cp_parser_abort_tentative_parse (parser);
9617
 
9618
      /* Parsing tentatively, again.  */
9619
      cp_parser_parse_tentatively (parser);
9620
 
9621
      /* Parse a class member access.  */
9622
      expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9623
                                           /*cast_p=*/false,
9624
                                           /*member_access_only_p=*/true, NULL);
9625
 
9626
      if (expr
9627
          && expr != error_mark_node
9628
          && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9629
        /* We have an id-expression.  */
9630
        id_expression_or_member_access_p = true;
9631
    }
9632
 
9633
  if (id_expression_or_member_access_p)
9634
    /* We have parsed the complete id-expression or member access.  */
9635
    cp_parser_parse_definitely (parser);
9636
  else
9637
    {
9638
      bool saved_greater_than_is_operator_p;
9639
 
9640
      /* Abort our attempt to parse an id-expression or member access
9641
         expression.  */
9642
      cp_parser_abort_tentative_parse (parser);
9643
 
9644
      /* Within a parenthesized expression, a `>' token is always
9645
         the greater-than operator.  */
9646
      saved_greater_than_is_operator_p
9647
        = parser->greater_than_is_operator_p;
9648
      parser->greater_than_is_operator_p = true;
9649
 
9650
      /* Parse a full expression.  */
9651
      expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9652
 
9653
      /* The `>' token might be the end of a template-id or
9654
         template-parameter-list now.  */
9655
      parser->greater_than_is_operator_p
9656
        = saved_greater_than_is_operator_p;
9657
    }
9658
 
9659
  /* Go back to evaluating expressions.  */
9660
  --cp_unevaluated_operand;
9661
  --c_inhibit_evaluation_warnings;
9662
 
9663
  /* Restore the old message and the integral constant expression
9664
     flags.  */
9665
  parser->type_definition_forbidden_message = saved_message;
9666
  parser->integral_constant_expression_p
9667
    = saved_integral_constant_expression_p;
9668
  parser->non_integral_constant_expression_p
9669
    = saved_non_integral_constant_expression_p;
9670
 
9671
  if (expr == error_mark_node)
9672
    {
9673
      /* Skip everything up to the closing `)'.  */
9674
      cp_parser_skip_to_closing_parenthesis (parser, true, false,
9675
                                             /*consume_paren=*/true);
9676
      return error_mark_node;
9677
    }
9678
 
9679
  /* Parse to the closing `)'.  */
9680
  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9681
    {
9682
      cp_parser_skip_to_closing_parenthesis (parser, true, false,
9683
                                             /*consume_paren=*/true);
9684
      return error_mark_node;
9685
    }
9686
 
9687
  return finish_decltype_type (expr, id_expression_or_member_access_p);
9688
}
9689
 
9690
/* Special member functions [gram.special] */
9691
 
9692
/* Parse a conversion-function-id.
9693
 
9694
   conversion-function-id:
9695
     operator conversion-type-id
9696
 
9697
   Returns an IDENTIFIER_NODE representing the operator.  */
9698
 
9699
static tree
9700
cp_parser_conversion_function_id (cp_parser* parser)
9701
{
9702
  tree type;
9703
  tree saved_scope;
9704
  tree saved_qualifying_scope;
9705
  tree saved_object_scope;
9706
  tree pushed_scope = NULL_TREE;
9707
 
9708
  /* Look for the `operator' token.  */
9709
  if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9710
    return error_mark_node;
9711
  /* When we parse the conversion-type-id, the current scope will be
9712
     reset.  However, we need that information in able to look up the
9713
     conversion function later, so we save it here.  */
9714
  saved_scope = parser->scope;
9715
  saved_qualifying_scope = parser->qualifying_scope;
9716
  saved_object_scope = parser->object_scope;
9717
  /* We must enter the scope of the class so that the names of
9718
     entities declared within the class are available in the
9719
     conversion-type-id.  For example, consider:
9720
 
9721
       struct S {
9722
         typedef int I;
9723
         operator I();
9724
       };
9725
 
9726
       S::operator I() { ... }
9727
 
9728
     In order to see that `I' is a type-name in the definition, we
9729
     must be in the scope of `S'.  */
9730
  if (saved_scope)
9731
    pushed_scope = push_scope (saved_scope);
9732
  /* Parse the conversion-type-id.  */
9733
  type = cp_parser_conversion_type_id (parser);
9734
  /* Leave the scope of the class, if any.  */
9735
  if (pushed_scope)
9736
    pop_scope (pushed_scope);
9737
  /* Restore the saved scope.  */
9738
  parser->scope = saved_scope;
9739
  parser->qualifying_scope = saved_qualifying_scope;
9740
  parser->object_scope = saved_object_scope;
9741
  /* If the TYPE is invalid, indicate failure.  */
9742
  if (type == error_mark_node)
9743
    return error_mark_node;
9744
  return mangle_conv_op_name_for_type (type);
9745
}
9746
 
9747
/* Parse a conversion-type-id:
9748
 
9749
   conversion-type-id:
9750
     type-specifier-seq conversion-declarator [opt]
9751
 
9752
   Returns the TYPE specified.  */
9753
 
9754
static tree
9755
cp_parser_conversion_type_id (cp_parser* parser)
9756
{
9757
  tree attributes;
9758
  cp_decl_specifier_seq type_specifiers;
9759
  cp_declarator *declarator;
9760
  tree type_specified;
9761
 
9762
  /* Parse the attributes.  */
9763
  attributes = cp_parser_attributes_opt (parser);
9764
  /* Parse the type-specifiers.  */
9765
  cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9766
                                /*is_trailing_return=*/false,
9767
                                &type_specifiers);
9768
  /* If that didn't work, stop.  */
9769
  if (type_specifiers.type == error_mark_node)
9770
    return error_mark_node;
9771
  /* Parse the conversion-declarator.  */
9772
  declarator = cp_parser_conversion_declarator_opt (parser);
9773
 
9774
  type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9775
                                    /*initialized=*/0, &attributes);
9776
  if (attributes)
9777
    cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9778
 
9779
  /* Don't give this error when parsing tentatively.  This happens to
9780
     work because we always parse this definitively once.  */
9781
  if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9782
      && type_uses_auto (type_specified))
9783
    {
9784
      error ("invalid use of %<auto%> in conversion operator");
9785
      return error_mark_node;
9786
    }
9787
 
9788
  return type_specified;
9789
}
9790
 
9791
/* Parse an (optional) conversion-declarator.
9792
 
9793
   conversion-declarator:
9794
     ptr-operator conversion-declarator [opt]
9795
 
9796
   */
9797
 
9798
static cp_declarator *
9799
cp_parser_conversion_declarator_opt (cp_parser* parser)
9800
{
9801
  enum tree_code code;
9802
  tree class_type;
9803
  cp_cv_quals cv_quals;
9804
 
9805
  /* We don't know if there's a ptr-operator next, or not.  */
9806
  cp_parser_parse_tentatively (parser);
9807
  /* Try the ptr-operator.  */
9808
  code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9809
  /* If it worked, look for more conversion-declarators.  */
9810
  if (cp_parser_parse_definitely (parser))
9811
    {
9812
      cp_declarator *declarator;
9813
 
9814
      /* Parse another optional declarator.  */
9815
      declarator = cp_parser_conversion_declarator_opt (parser);
9816
 
9817
      return cp_parser_make_indirect_declarator
9818
        (code, class_type, cv_quals, declarator);
9819
   }
9820
 
9821
  return NULL;
9822
}
9823
 
9824
/* Parse an (optional) ctor-initializer.
9825
 
9826
   ctor-initializer:
9827
     : mem-initializer-list
9828
 
9829
   Returns TRUE iff the ctor-initializer was actually present.  */
9830
 
9831
static bool
9832
cp_parser_ctor_initializer_opt (cp_parser* parser)
9833
{
9834
  /* If the next token is not a `:', then there is no
9835
     ctor-initializer.  */
9836
  if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9837
    {
9838
      /* Do default initialization of any bases and members.  */
9839
      if (DECL_CONSTRUCTOR_P (current_function_decl))
9840
        finish_mem_initializers (NULL_TREE);
9841
 
9842
      return false;
9843
    }
9844
 
9845
  /* Consume the `:' token.  */
9846
  cp_lexer_consume_token (parser->lexer);
9847
  /* And the mem-initializer-list.  */
9848
  cp_parser_mem_initializer_list (parser);
9849
 
9850
  return true;
9851
}
9852
 
9853
/* Parse a mem-initializer-list.
9854
 
9855
   mem-initializer-list:
9856
     mem-initializer ... [opt]
9857
     mem-initializer ... [opt] , mem-initializer-list  */
9858
 
9859
static void
9860
cp_parser_mem_initializer_list (cp_parser* parser)
9861
{
9862
  tree mem_initializer_list = NULL_TREE;
9863
  cp_token *token = cp_lexer_peek_token (parser->lexer);
9864
 
9865
  /* Let the semantic analysis code know that we are starting the
9866
     mem-initializer-list.  */
9867
  if (!DECL_CONSTRUCTOR_P (current_function_decl))
9868
    error_at (token->location,
9869
              "only constructors take base initializers");
9870
 
9871
  /* Loop through the list.  */
9872
  while (true)
9873
    {
9874
      tree mem_initializer;
9875
 
9876
      token = cp_lexer_peek_token (parser->lexer);
9877
      /* Parse the mem-initializer.  */
9878
      mem_initializer = cp_parser_mem_initializer (parser);
9879
      /* If the next token is a `...', we're expanding member initializers. */
9880
      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9881
        {
9882
          /* Consume the `...'. */
9883
          cp_lexer_consume_token (parser->lexer);
9884
 
9885
          /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9886
             can be expanded but members cannot. */
9887
          if (mem_initializer != error_mark_node
9888
              && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9889
            {
9890
              error_at (token->location,
9891
                        "cannot expand initializer for member %<%D%>",
9892
                        TREE_PURPOSE (mem_initializer));
9893
              mem_initializer = error_mark_node;
9894
            }
9895
 
9896
          /* Construct the pack expansion type. */
9897
          if (mem_initializer != error_mark_node)
9898
            mem_initializer = make_pack_expansion (mem_initializer);
9899
        }
9900
      /* Add it to the list, unless it was erroneous.  */
9901
      if (mem_initializer != error_mark_node)
9902
        {
9903
          TREE_CHAIN (mem_initializer) = mem_initializer_list;
9904
          mem_initializer_list = mem_initializer;
9905
        }
9906
      /* If the next token is not a `,', we're done.  */
9907
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9908
        break;
9909
      /* Consume the `,' token.  */
9910
      cp_lexer_consume_token (parser->lexer);
9911
    }
9912
 
9913
  /* Perform semantic analysis.  */
9914
  if (DECL_CONSTRUCTOR_P (current_function_decl))
9915
    finish_mem_initializers (mem_initializer_list);
9916
}
9917
 
9918
/* Parse a mem-initializer.
9919
 
9920
   mem-initializer:
9921
     mem-initializer-id ( expression-list [opt] )
9922
     mem-initializer-id braced-init-list
9923
 
9924
   GNU extension:
9925
 
9926
   mem-initializer:
9927
     ( expression-list [opt] )
9928
 
9929
   Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9930
   class) or FIELD_DECL (for a non-static data member) to initialize;
9931
   the TREE_VALUE is the expression-list.  An empty initialization
9932
   list is represented by void_list_node.  */
9933
 
9934
static tree
9935
cp_parser_mem_initializer (cp_parser* parser)
9936
{
9937
  tree mem_initializer_id;
9938
  tree expression_list;
9939
  tree member;
9940
  cp_token *token = cp_lexer_peek_token (parser->lexer);
9941
 
9942
  /* Find out what is being initialized.  */
9943
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9944
    {
9945
      permerror (token->location,
9946
                 "anachronistic old-style base class initializer");
9947
      mem_initializer_id = NULL_TREE;
9948
    }
9949
  else
9950
    {
9951
      mem_initializer_id = cp_parser_mem_initializer_id (parser);
9952
      if (mem_initializer_id == error_mark_node)
9953
        return mem_initializer_id;
9954
    }
9955
  member = expand_member_init (mem_initializer_id);
9956
  if (member && !DECL_P (member))
9957
    in_base_initializer = 1;
9958
 
9959
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9960
    {
9961
      bool expr_non_constant_p;
9962
      maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9963
      expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9964
      CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9965
      expression_list = build_tree_list (NULL_TREE, expression_list);
9966
    }
9967
  else
9968
    {
9969
      VEC(tree,gc)* vec;
9970
      vec = cp_parser_parenthesized_expression_list (parser, false,
9971
                                                     /*cast_p=*/false,
9972
                                                     /*allow_expansion_p=*/true,
9973
                                                     /*non_constant_p=*/NULL);
9974
      if (vec == NULL)
9975
        return error_mark_node;
9976
      expression_list = build_tree_list_vec (vec);
9977
      release_tree_vector (vec);
9978
    }
9979
 
9980
  if (expression_list == error_mark_node)
9981
    return error_mark_node;
9982
  if (!expression_list)
9983
    expression_list = void_type_node;
9984
 
9985
  in_base_initializer = 0;
9986
 
9987
  return member ? build_tree_list (member, expression_list) : error_mark_node;
9988
}
9989
 
9990
/* Parse a mem-initializer-id.
9991
 
9992
   mem-initializer-id:
9993
     :: [opt] nested-name-specifier [opt] class-name
9994
     identifier
9995
 
9996
   Returns a TYPE indicating the class to be initializer for the first
9997
   production.  Returns an IDENTIFIER_NODE indicating the data member
9998
   to be initialized for the second production.  */
9999
 
10000
static tree
10001
cp_parser_mem_initializer_id (cp_parser* parser)
10002
{
10003
  bool global_scope_p;
10004
  bool nested_name_specifier_p;
10005
  bool template_p = false;
10006
  tree id;
10007
 
10008
  cp_token *token = cp_lexer_peek_token (parser->lexer);
10009
 
10010
  /* `typename' is not allowed in this context ([temp.res]).  */
10011
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10012
    {
10013
      error_at (token->location,
10014
                "keyword %<typename%> not allowed in this context (a qualified "
10015
                "member initializer is implicitly a type)");
10016
      cp_lexer_consume_token (parser->lexer);
10017
    }
10018
  /* Look for the optional `::' operator.  */
10019
  global_scope_p
10020
    = (cp_parser_global_scope_opt (parser,
10021
                                   /*current_scope_valid_p=*/false)
10022
       != NULL_TREE);
10023
  /* Look for the optional nested-name-specifier.  The simplest way to
10024
     implement:
10025
 
10026
       [temp.res]
10027
 
10028
       The keyword `typename' is not permitted in a base-specifier or
10029
       mem-initializer; in these contexts a qualified name that
10030
       depends on a template-parameter is implicitly assumed to be a
10031
       type name.
10032
 
10033
     is to assume that we have seen the `typename' keyword at this
10034
     point.  */
10035
  nested_name_specifier_p
10036
    = (cp_parser_nested_name_specifier_opt (parser,
10037
                                            /*typename_keyword_p=*/true,
10038
                                            /*check_dependency_p=*/true,
10039
                                            /*type_p=*/true,
10040
                                            /*is_declaration=*/true)
10041
       != NULL_TREE);
10042
  if (nested_name_specifier_p)
10043
    template_p = cp_parser_optional_template_keyword (parser);
10044
  /* If there is a `::' operator or a nested-name-specifier, then we
10045
     are definitely looking for a class-name.  */
10046
  if (global_scope_p || nested_name_specifier_p)
10047
    return cp_parser_class_name (parser,
10048
                                 /*typename_keyword_p=*/true,
10049
                                 /*template_keyword_p=*/template_p,
10050
                                 typename_type,
10051
                                 /*check_dependency_p=*/true,
10052
                                 /*class_head_p=*/false,
10053
                                 /*is_declaration=*/true);
10054
  /* Otherwise, we could also be looking for an ordinary identifier.  */
10055
  cp_parser_parse_tentatively (parser);
10056
  /* Try a class-name.  */
10057
  id = cp_parser_class_name (parser,
10058
                             /*typename_keyword_p=*/true,
10059
                             /*template_keyword_p=*/false,
10060
                             none_type,
10061
                             /*check_dependency_p=*/true,
10062
                             /*class_head_p=*/false,
10063
                             /*is_declaration=*/true);
10064
  /* If we found one, we're done.  */
10065
  if (cp_parser_parse_definitely (parser))
10066
    return id;
10067
  /* Otherwise, look for an ordinary identifier.  */
10068
  return cp_parser_identifier (parser);
10069
}
10070
 
10071
/* Overloading [gram.over] */
10072
 
10073
/* Parse an operator-function-id.
10074
 
10075
   operator-function-id:
10076
     operator operator
10077
 
10078
   Returns an IDENTIFIER_NODE for the operator which is a
10079
   human-readable spelling of the identifier, e.g., `operator +'.  */
10080
 
10081
static tree
10082
cp_parser_operator_function_id (cp_parser* parser)
10083
{
10084
  /* Look for the `operator' keyword.  */
10085
  if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10086
    return error_mark_node;
10087
  /* And then the name of the operator itself.  */
10088
  return cp_parser_operator (parser);
10089
}
10090
 
10091
/* Parse an operator.
10092
 
10093
   operator:
10094
     new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10095
     += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10096
     || ++ -- , ->* -> () []
10097
 
10098
   GNU Extensions:
10099
 
10100
   operator:
10101
     <? >? <?= >?=
10102
 
10103
   Returns an IDENTIFIER_NODE for the operator which is a
10104
   human-readable spelling of the identifier, e.g., `operator +'.  */
10105
 
10106
static tree
10107
cp_parser_operator (cp_parser* parser)
10108
{
10109
  tree id = NULL_TREE;
10110
  cp_token *token;
10111
 
10112
  /* Peek at the next token.  */
10113
  token = cp_lexer_peek_token (parser->lexer);
10114
  /* Figure out which operator we have.  */
10115
  switch (token->type)
10116
    {
10117
    case CPP_KEYWORD:
10118
      {
10119
        enum tree_code op;
10120
 
10121
        /* The keyword should be either `new' or `delete'.  */
10122
        if (token->keyword == RID_NEW)
10123
          op = NEW_EXPR;
10124
        else if (token->keyword == RID_DELETE)
10125
          op = DELETE_EXPR;
10126
        else
10127
          break;
10128
 
10129
        /* Consume the `new' or `delete' token.  */
10130
        cp_lexer_consume_token (parser->lexer);
10131
 
10132
        /* Peek at the next token.  */
10133
        token = cp_lexer_peek_token (parser->lexer);
10134
        /* If it's a `[' token then this is the array variant of the
10135
           operator.  */
10136
        if (token->type == CPP_OPEN_SQUARE)
10137
          {
10138
            /* Consume the `[' token.  */
10139
            cp_lexer_consume_token (parser->lexer);
10140
            /* Look for the `]' token.  */
10141
            cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10142
            id = ansi_opname (op == NEW_EXPR
10143
                              ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10144
          }
10145
        /* Otherwise, we have the non-array variant.  */
10146
        else
10147
          id = ansi_opname (op);
10148
 
10149
        return id;
10150
      }
10151
 
10152
    case CPP_PLUS:
10153
      id = ansi_opname (PLUS_EXPR);
10154
      break;
10155
 
10156
    case CPP_MINUS:
10157
      id = ansi_opname (MINUS_EXPR);
10158
      break;
10159
 
10160
    case CPP_MULT:
10161
      id = ansi_opname (MULT_EXPR);
10162
      break;
10163
 
10164
    case CPP_DIV:
10165
      id = ansi_opname (TRUNC_DIV_EXPR);
10166
      break;
10167
 
10168
    case CPP_MOD:
10169
      id = ansi_opname (TRUNC_MOD_EXPR);
10170
      break;
10171
 
10172
    case CPP_XOR:
10173
      id = ansi_opname (BIT_XOR_EXPR);
10174
      break;
10175
 
10176
    case CPP_AND:
10177
      id = ansi_opname (BIT_AND_EXPR);
10178
      break;
10179
 
10180
    case CPP_OR:
10181
      id = ansi_opname (BIT_IOR_EXPR);
10182
      break;
10183
 
10184
    case CPP_COMPL:
10185
      id = ansi_opname (BIT_NOT_EXPR);
10186
      break;
10187
 
10188
    case CPP_NOT:
10189
      id = ansi_opname (TRUTH_NOT_EXPR);
10190
      break;
10191
 
10192
    case CPP_EQ:
10193
      id = ansi_assopname (NOP_EXPR);
10194
      break;
10195
 
10196
    case CPP_LESS:
10197
      id = ansi_opname (LT_EXPR);
10198
      break;
10199
 
10200
    case CPP_GREATER:
10201
      id = ansi_opname (GT_EXPR);
10202
      break;
10203
 
10204
    case CPP_PLUS_EQ:
10205
      id = ansi_assopname (PLUS_EXPR);
10206
      break;
10207
 
10208
    case CPP_MINUS_EQ:
10209
      id = ansi_assopname (MINUS_EXPR);
10210
      break;
10211
 
10212
    case CPP_MULT_EQ:
10213
      id = ansi_assopname (MULT_EXPR);
10214
      break;
10215
 
10216
    case CPP_DIV_EQ:
10217
      id = ansi_assopname (TRUNC_DIV_EXPR);
10218
      break;
10219
 
10220
    case CPP_MOD_EQ:
10221
      id = ansi_assopname (TRUNC_MOD_EXPR);
10222
      break;
10223
 
10224
    case CPP_XOR_EQ:
10225
      id = ansi_assopname (BIT_XOR_EXPR);
10226
      break;
10227
 
10228
    case CPP_AND_EQ:
10229
      id = ansi_assopname (BIT_AND_EXPR);
10230
      break;
10231
 
10232
    case CPP_OR_EQ:
10233
      id = ansi_assopname (BIT_IOR_EXPR);
10234
      break;
10235
 
10236
    case CPP_LSHIFT:
10237
      id = ansi_opname (LSHIFT_EXPR);
10238
      break;
10239
 
10240
    case CPP_RSHIFT:
10241
      id = ansi_opname (RSHIFT_EXPR);
10242
      break;
10243
 
10244
    case CPP_LSHIFT_EQ:
10245
      id = ansi_assopname (LSHIFT_EXPR);
10246
      break;
10247
 
10248
    case CPP_RSHIFT_EQ:
10249
      id = ansi_assopname (RSHIFT_EXPR);
10250
      break;
10251
 
10252
    case CPP_EQ_EQ:
10253
      id = ansi_opname (EQ_EXPR);
10254
      break;
10255
 
10256
    case CPP_NOT_EQ:
10257
      id = ansi_opname (NE_EXPR);
10258
      break;
10259
 
10260
    case CPP_LESS_EQ:
10261
      id = ansi_opname (LE_EXPR);
10262
      break;
10263
 
10264
    case CPP_GREATER_EQ:
10265
      id = ansi_opname (GE_EXPR);
10266
      break;
10267
 
10268
    case CPP_AND_AND:
10269
      id = ansi_opname (TRUTH_ANDIF_EXPR);
10270
      break;
10271
 
10272
    case CPP_OR_OR:
10273
      id = ansi_opname (TRUTH_ORIF_EXPR);
10274
      break;
10275
 
10276
    case CPP_PLUS_PLUS:
10277
      id = ansi_opname (POSTINCREMENT_EXPR);
10278
      break;
10279
 
10280
    case CPP_MINUS_MINUS:
10281
      id = ansi_opname (PREDECREMENT_EXPR);
10282
      break;
10283
 
10284
    case CPP_COMMA:
10285
      id = ansi_opname (COMPOUND_EXPR);
10286
      break;
10287
 
10288
    case CPP_DEREF_STAR:
10289
      id = ansi_opname (MEMBER_REF);
10290
      break;
10291
 
10292
    case CPP_DEREF:
10293
      id = ansi_opname (COMPONENT_REF);
10294
      break;
10295
 
10296
    case CPP_OPEN_PAREN:
10297
      /* Consume the `('.  */
10298
      cp_lexer_consume_token (parser->lexer);
10299
      /* Look for the matching `)'.  */
10300
      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10301
      return ansi_opname (CALL_EXPR);
10302
 
10303
    case CPP_OPEN_SQUARE:
10304
      /* Consume the `['.  */
10305
      cp_lexer_consume_token (parser->lexer);
10306
      /* Look for the matching `]'.  */
10307
      cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10308
      return ansi_opname (ARRAY_REF);
10309
 
10310
    default:
10311
      /* Anything else is an error.  */
10312
      break;
10313
    }
10314
 
10315
  /* If we have selected an identifier, we need to consume the
10316
     operator token.  */
10317
  if (id)
10318
    cp_lexer_consume_token (parser->lexer);
10319
  /* Otherwise, no valid operator name was present.  */
10320
  else
10321
    {
10322
      cp_parser_error (parser, "expected operator");
10323
      id = error_mark_node;
10324
    }
10325
 
10326
  return id;
10327
}
10328
 
10329
/* Parse a template-declaration.
10330
 
10331
   template-declaration:
10332
     export [opt] template < template-parameter-list > declaration
10333
 
10334
   If MEMBER_P is TRUE, this template-declaration occurs within a
10335
   class-specifier.
10336
 
10337
   The grammar rule given by the standard isn't correct.  What
10338
   is really meant is:
10339
 
10340
   template-declaration:
10341
     export [opt] template-parameter-list-seq
10342
       decl-specifier-seq [opt] init-declarator [opt] ;
10343
     export [opt] template-parameter-list-seq
10344
       function-definition
10345
 
10346
   template-parameter-list-seq:
10347
     template-parameter-list-seq [opt]
10348
     template < template-parameter-list >  */
10349
 
10350
static void
10351
cp_parser_template_declaration (cp_parser* parser, bool member_p)
10352
{
10353
  /* Check for `export'.  */
10354
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10355
    {
10356
      /* Consume the `export' token.  */
10357
      cp_lexer_consume_token (parser->lexer);
10358
      /* Warn that we do not support `export'.  */
10359
      warning (0, "keyword %<export%> not implemented, and will be ignored");
10360
    }
10361
 
10362
  cp_parser_template_declaration_after_export (parser, member_p);
10363
}
10364
 
10365
/* Parse a template-parameter-list.
10366
 
10367
   template-parameter-list:
10368
     template-parameter
10369
     template-parameter-list , template-parameter
10370
 
10371
   Returns a TREE_LIST.  Each node represents a template parameter.
10372
   The nodes are connected via their TREE_CHAINs.  */
10373
 
10374
static tree
10375
cp_parser_template_parameter_list (cp_parser* parser)
10376
{
10377
  tree parameter_list = NULL_TREE;
10378
 
10379
  begin_template_parm_list ();
10380
  while (true)
10381
    {
10382
      tree parameter;
10383
      bool is_non_type;
10384
      bool is_parameter_pack;
10385
      location_t parm_loc;
10386
 
10387
      /* Parse the template-parameter.  */
10388
      parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10389
      parameter = cp_parser_template_parameter (parser,
10390
                                                &is_non_type,
10391
                                                &is_parameter_pack);
10392
      /* Add it to the list.  */
10393
      if (parameter != error_mark_node)
10394
        parameter_list = process_template_parm (parameter_list,
10395
                                                parm_loc,
10396
                                                parameter,
10397
                                                is_non_type,
10398
                                                is_parameter_pack);
10399
      else
10400
       {
10401
         tree err_parm = build_tree_list (parameter, parameter);
10402
         TREE_VALUE (err_parm) = error_mark_node;
10403
         parameter_list = chainon (parameter_list, err_parm);
10404
       }
10405
 
10406
      /* If the next token is not a `,', we're done.  */
10407
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10408
        break;
10409
      /* Otherwise, consume the `,' token.  */
10410
      cp_lexer_consume_token (parser->lexer);
10411
    }
10412
 
10413
  return end_template_parm_list (parameter_list);
10414
}
10415
 
10416
/* Parse a template-parameter.
10417
 
10418
   template-parameter:
10419
     type-parameter
10420
     parameter-declaration
10421
 
10422
   If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
10423
   the parameter.  The TREE_PURPOSE is the default value, if any.
10424
   Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
10425
   iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
10426
   set to true iff this parameter is a parameter pack. */
10427
 
10428
static tree
10429
cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10430
                              bool *is_parameter_pack)
10431
{
10432
  cp_token *token;
10433
  cp_parameter_declarator *parameter_declarator;
10434
  cp_declarator *id_declarator;
10435
  tree parm;
10436
 
10437
  /* Assume it is a type parameter or a template parameter.  */
10438
  *is_non_type = false;
10439
  /* Assume it not a parameter pack. */
10440
  *is_parameter_pack = false;
10441
  /* Peek at the next token.  */
10442
  token = cp_lexer_peek_token (parser->lexer);
10443
  /* If it is `class' or `template', we have a type-parameter.  */
10444
  if (token->keyword == RID_TEMPLATE)
10445
    return cp_parser_type_parameter (parser, is_parameter_pack);
10446
  /* If it is `class' or `typename' we do not know yet whether it is a
10447
     type parameter or a non-type parameter.  Consider:
10448
 
10449
       template <typename T, typename T::X X> ...
10450
 
10451
     or:
10452
 
10453
       template <class C, class D*> ...
10454
 
10455
     Here, the first parameter is a type parameter, and the second is
10456
     a non-type parameter.  We can tell by looking at the token after
10457
     the identifier -- if it is a `,', `=', or `>' then we have a type
10458
     parameter.  */
10459
  if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10460
    {
10461
      /* Peek at the token after `class' or `typename'.  */
10462
      token = cp_lexer_peek_nth_token (parser->lexer, 2);
10463
      /* If it's an ellipsis, we have a template type parameter
10464
         pack. */
10465
      if (token->type == CPP_ELLIPSIS)
10466
        return cp_parser_type_parameter (parser, is_parameter_pack);
10467
      /* If it's an identifier, skip it.  */
10468
      if (token->type == CPP_NAME)
10469
        token = cp_lexer_peek_nth_token (parser->lexer, 3);
10470
      /* Now, see if the token looks like the end of a template
10471
         parameter.  */
10472
      if (token->type == CPP_COMMA
10473
          || token->type == CPP_EQ
10474
          || token->type == CPP_GREATER)
10475
        return cp_parser_type_parameter (parser, is_parameter_pack);
10476
    }
10477
 
10478
  /* Otherwise, it is a non-type parameter.
10479
 
10480
     [temp.param]
10481
 
10482
     When parsing a default template-argument for a non-type
10483
     template-parameter, the first non-nested `>' is taken as the end
10484
     of the template parameter-list rather than a greater-than
10485
     operator.  */
10486
  *is_non_type = true;
10487
  parameter_declarator
10488
     = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10489
                                        /*parenthesized_p=*/NULL);
10490
 
10491
  /* If the parameter declaration is marked as a parameter pack, set
10492
     *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10493
     declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10494
     grokdeclarator. */
10495
  if (parameter_declarator
10496
      && parameter_declarator->declarator
10497
      && parameter_declarator->declarator->parameter_pack_p)
10498
    {
10499
      *is_parameter_pack = true;
10500
      parameter_declarator->declarator->parameter_pack_p = false;
10501
    }
10502
 
10503
  /* If the next token is an ellipsis, and we don't already have it
10504
     marked as a parameter pack, then we have a parameter pack (that
10505
     has no declarator).  */
10506
  if (!*is_parameter_pack
10507
      && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10508
      && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10509
    {
10510
      /* Consume the `...'.  */
10511
      cp_lexer_consume_token (parser->lexer);
10512
      maybe_warn_variadic_templates ();
10513
 
10514
      *is_parameter_pack = true;
10515
    }
10516
  /* We might end up with a pack expansion as the type of the non-type
10517
     template parameter, in which case this is a non-type template
10518
     parameter pack.  */
10519
  else if (parameter_declarator
10520
           && parameter_declarator->decl_specifiers.type
10521
           && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10522
    {
10523
      *is_parameter_pack = true;
10524
      parameter_declarator->decl_specifiers.type =
10525
        PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10526
    }
10527
 
10528
  if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10529
    {
10530
      /* Parameter packs cannot have default arguments.  However, a
10531
         user may try to do so, so we'll parse them and give an
10532
         appropriate diagnostic here.  */
10533
 
10534
      /* Consume the `='.  */
10535
      cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10536
      cp_lexer_consume_token (parser->lexer);
10537
 
10538
      /* Find the name of the parameter pack.  */
10539
      id_declarator = parameter_declarator->declarator;
10540
      while (id_declarator && id_declarator->kind != cdk_id)
10541
        id_declarator = id_declarator->declarator;
10542
 
10543
      if (id_declarator && id_declarator->kind == cdk_id)
10544
        error_at (start_token->location,
10545
                  "template parameter pack %qD cannot have a default argument",
10546
                  id_declarator->u.id.unqualified_name);
10547
      else
10548
        error_at (start_token->location,
10549
                  "template parameter pack cannot have a default argument");
10550
 
10551
      /* Parse the default argument, but throw away the result.  */
10552
      cp_parser_default_argument (parser, /*template_parm_p=*/true);
10553
    }
10554
 
10555
  parm = grokdeclarator (parameter_declarator->declarator,
10556
                         &parameter_declarator->decl_specifiers,
10557
                         TPARM, /*initialized=*/0,
10558
                         /*attrlist=*/NULL);
10559
  if (parm == error_mark_node)
10560
    return error_mark_node;
10561
 
10562
  return build_tree_list (parameter_declarator->default_argument, parm);
10563
}
10564
 
10565
/* Parse a type-parameter.
10566
 
10567
   type-parameter:
10568
     class identifier [opt]
10569
     class identifier [opt] = type-id
10570
     typename identifier [opt]
10571
     typename identifier [opt] = type-id
10572
     template < template-parameter-list > class identifier [opt]
10573
     template < template-parameter-list > class identifier [opt]
10574
       = id-expression
10575
 
10576
   GNU Extension (variadic templates):
10577
 
10578
   type-parameter:
10579
     class ... identifier [opt]
10580
     typename ... identifier [opt]
10581
 
10582
   Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
10583
   TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
10584
   the declaration of the parameter.
10585
 
10586
   Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10587
 
10588
static tree
10589
cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10590
{
10591
  cp_token *token;
10592
  tree parameter;
10593
 
10594
  /* Look for a keyword to tell us what kind of parameter this is.  */
10595
  token = cp_parser_require (parser, CPP_KEYWORD,
10596
                             "%<class%>, %<typename%>, or %<template%>");
10597
  if (!token)
10598
    return error_mark_node;
10599
 
10600
  switch (token->keyword)
10601
    {
10602
    case RID_CLASS:
10603
    case RID_TYPENAME:
10604
      {
10605
        tree identifier;
10606
        tree default_argument;
10607
 
10608
        /* If the next token is an ellipsis, we have a template
10609
           argument pack. */
10610
        if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10611
          {
10612
            /* Consume the `...' token. */
10613
            cp_lexer_consume_token (parser->lexer);
10614
            maybe_warn_variadic_templates ();
10615
 
10616
            *is_parameter_pack = true;
10617
          }
10618
 
10619
        /* If the next token is an identifier, then it names the
10620
           parameter.  */
10621
        if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10622
          identifier = cp_parser_identifier (parser);
10623
        else
10624
          identifier = NULL_TREE;
10625
 
10626
        /* Create the parameter.  */
10627
        parameter = finish_template_type_parm (class_type_node, identifier);
10628
 
10629
        /* If the next token is an `=', we have a default argument.  */
10630
        if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10631
          {
10632
            /* Consume the `=' token.  */
10633
            cp_lexer_consume_token (parser->lexer);
10634
            /* Parse the default-argument.  */
10635
            push_deferring_access_checks (dk_no_deferred);
10636
            default_argument = cp_parser_type_id (parser);
10637
 
10638
            /* Template parameter packs cannot have default
10639
               arguments. */
10640
            if (*is_parameter_pack)
10641
              {
10642
                if (identifier)
10643
                  error_at (token->location,
10644
                            "template parameter pack %qD cannot have a "
10645
                            "default argument", identifier);
10646
                else
10647
                  error_at (token->location,
10648
                            "template parameter packs cannot have "
10649
                            "default arguments");
10650
                default_argument = NULL_TREE;
10651
              }
10652
            pop_deferring_access_checks ();
10653
          }
10654
        else
10655
          default_argument = NULL_TREE;
10656
 
10657
        /* Create the combined representation of the parameter and the
10658
           default argument.  */
10659
        parameter = build_tree_list (default_argument, parameter);
10660
      }
10661
      break;
10662
 
10663
    case RID_TEMPLATE:
10664
      {
10665
        tree identifier;
10666
        tree default_argument;
10667
 
10668
        /* Look for the `<'.  */
10669
        cp_parser_require (parser, CPP_LESS, "%<<%>");
10670
        /* Parse the template-parameter-list.  */
10671
        cp_parser_template_parameter_list (parser);
10672
        /* Look for the `>'.  */
10673
        cp_parser_require (parser, CPP_GREATER, "%<>%>");
10674
        /* Look for the `class' keyword.  */
10675
        cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10676
        /* If the next token is an ellipsis, we have a template
10677
           argument pack. */
10678
        if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10679
          {
10680
            /* Consume the `...' token. */
10681
            cp_lexer_consume_token (parser->lexer);
10682
            maybe_warn_variadic_templates ();
10683
 
10684
            *is_parameter_pack = true;
10685
          }
10686
        /* If the next token is an `=', then there is a
10687
           default-argument.  If the next token is a `>', we are at
10688
           the end of the parameter-list.  If the next token is a `,',
10689
           then we are at the end of this parameter.  */
10690
        if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10691
            && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10692
            && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10693
          {
10694
            identifier = cp_parser_identifier (parser);
10695
            /* Treat invalid names as if the parameter were nameless.  */
10696
            if (identifier == error_mark_node)
10697
              identifier = NULL_TREE;
10698
          }
10699
        else
10700
          identifier = NULL_TREE;
10701
 
10702
        /* Create the template parameter.  */
10703
        parameter = finish_template_template_parm (class_type_node,
10704
                                                   identifier);
10705
 
10706
        /* If the next token is an `=', then there is a
10707
           default-argument.  */
10708
        if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10709
          {
10710
            bool is_template;
10711
 
10712
            /* Consume the `='.  */
10713
            cp_lexer_consume_token (parser->lexer);
10714
            /* Parse the id-expression.  */
10715
            push_deferring_access_checks (dk_no_deferred);
10716
            /* save token before parsing the id-expression, for error
10717
               reporting */
10718
            token = cp_lexer_peek_token (parser->lexer);
10719
            default_argument
10720
              = cp_parser_id_expression (parser,
10721
                                         /*template_keyword_p=*/false,
10722
                                         /*check_dependency_p=*/true,
10723
                                         /*template_p=*/&is_template,
10724
                                         /*declarator_p=*/false,
10725
                                         /*optional_p=*/false);
10726
            if (TREE_CODE (default_argument) == TYPE_DECL)
10727
              /* If the id-expression was a template-id that refers to
10728
                 a template-class, we already have the declaration here,
10729
                 so no further lookup is needed.  */
10730
                 ;
10731
            else
10732
              /* Look up the name.  */
10733
              default_argument
10734
                = cp_parser_lookup_name (parser, default_argument,
10735
                                         none_type,
10736
                                         /*is_template=*/is_template,
10737
                                         /*is_namespace=*/false,
10738
                                         /*check_dependency=*/true,
10739
                                         /*ambiguous_decls=*/NULL,
10740
                                         token->location);
10741
            /* See if the default argument is valid.  */
10742
            default_argument
10743
              = check_template_template_default_arg (default_argument);
10744
 
10745
            /* Template parameter packs cannot have default
10746
               arguments. */
10747
            if (*is_parameter_pack)
10748
              {
10749
                if (identifier)
10750
                  error_at (token->location,
10751
                            "template parameter pack %qD cannot "
10752
                            "have a default argument",
10753
                            identifier);
10754
                else
10755
                  error_at (token->location, "template parameter packs cannot "
10756
                            "have default arguments");
10757
                default_argument = NULL_TREE;
10758
              }
10759
            pop_deferring_access_checks ();
10760
          }
10761
        else
10762
          default_argument = NULL_TREE;
10763
 
10764
        /* Create the combined representation of the parameter and the
10765
           default argument.  */
10766
        parameter = build_tree_list (default_argument, parameter);
10767
      }
10768
      break;
10769
 
10770
    default:
10771
      gcc_unreachable ();
10772
      break;
10773
    }
10774
 
10775
  return parameter;
10776
}
10777
 
10778
/* Parse a template-id.
10779
 
10780
   template-id:
10781
     template-name < template-argument-list [opt] >
10782
 
10783
   If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10784
   `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10785
   returned.  Otherwise, if the template-name names a function, or set
10786
   of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10787
   names a class, returns a TYPE_DECL for the specialization.
10788
 
10789
   If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10790
   uninstantiated templates.  */
10791
 
10792
static tree
10793
cp_parser_template_id (cp_parser *parser,
10794
                       bool template_keyword_p,
10795
                       bool check_dependency_p,
10796
                       bool is_declaration)
10797
{
10798
  int i;
10799
  tree templ;
10800
  tree arguments;
10801
  tree template_id;
10802
  cp_token_position start_of_id = 0;
10803
  deferred_access_check *chk;
10804
  VEC (deferred_access_check,gc) *access_check;
10805
  cp_token *next_token = NULL, *next_token_2 = NULL;
10806
  bool is_identifier;
10807
 
10808
  /* If the next token corresponds to a template-id, there is no need
10809
     to reparse it.  */
10810
  next_token = cp_lexer_peek_token (parser->lexer);
10811
  if (next_token->type == CPP_TEMPLATE_ID)
10812
    {
10813
      struct tree_check *check_value;
10814
 
10815
      /* Get the stored value.  */
10816
      check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10817
      /* Perform any access checks that were deferred.  */
10818
      access_check = check_value->checks;
10819
      if (access_check)
10820
        {
10821
          for (i = 0 ;
10822
               VEC_iterate (deferred_access_check, access_check, i, chk) ;
10823
               ++i)
10824
            {
10825
              perform_or_defer_access_check (chk->binfo,
10826
                                             chk->decl,
10827
                                             chk->diag_decl);
10828
            }
10829
        }
10830
      /* Return the stored value.  */
10831
      return check_value->value;
10832
    }
10833
 
10834
  /* Avoid performing name lookup if there is no possibility of
10835
     finding a template-id.  */
10836
  if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10837
      || (next_token->type == CPP_NAME
10838
          && !cp_parser_nth_token_starts_template_argument_list_p
10839
               (parser, 2)))
10840
    {
10841
      cp_parser_error (parser, "expected template-id");
10842
      return error_mark_node;
10843
    }
10844
 
10845
  /* Remember where the template-id starts.  */
10846
  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10847
    start_of_id = cp_lexer_token_position (parser->lexer, false);
10848
 
10849
  push_deferring_access_checks (dk_deferred);
10850
 
10851
  /* Parse the template-name.  */
10852
  is_identifier = false;
10853
  templ = cp_parser_template_name (parser, template_keyword_p,
10854
                                   check_dependency_p,
10855
                                   is_declaration,
10856
                                   &is_identifier);
10857
  if (templ == error_mark_node || is_identifier)
10858
    {
10859
      pop_deferring_access_checks ();
10860
      return templ;
10861
    }
10862
 
10863
  /* If we find the sequence `[:' after a template-name, it's probably
10864
     a digraph-typo for `< ::'. Substitute the tokens and check if we can
10865
     parse correctly the argument list.  */
10866
  next_token = cp_lexer_peek_token (parser->lexer);
10867
  next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10868
  if (next_token->type == CPP_OPEN_SQUARE
10869
      && next_token->flags & DIGRAPH
10870
      && next_token_2->type == CPP_COLON
10871
      && !(next_token_2->flags & PREV_WHITE))
10872
    {
10873
      cp_parser_parse_tentatively (parser);
10874
      /* Change `:' into `::'.  */
10875
      next_token_2->type = CPP_SCOPE;
10876
      /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10877
         CPP_LESS.  */
10878
      cp_lexer_consume_token (parser->lexer);
10879
 
10880
      /* Parse the arguments.  */
10881
      arguments = cp_parser_enclosed_template_argument_list (parser);
10882
      if (!cp_parser_parse_definitely (parser))
10883
        {
10884
          /* If we couldn't parse an argument list, then we revert our changes
10885
             and return simply an error. Maybe this is not a template-id
10886
             after all.  */
10887
          next_token_2->type = CPP_COLON;
10888
          cp_parser_error (parser, "expected %<<%>");
10889
          pop_deferring_access_checks ();
10890
          return error_mark_node;
10891
        }
10892
      /* Otherwise, emit an error about the invalid digraph, but continue
10893
         parsing because we got our argument list.  */
10894
      if (permerror (next_token->location,
10895
                     "%<<::%> cannot begin a template-argument list"))
10896
        {
10897
          static bool hint = false;
10898
          inform (next_token->location,
10899
                  "%<<:%> is an alternate spelling for %<[%>."
10900
                  " Insert whitespace between %<<%> and %<::%>");
10901
          if (!hint && !flag_permissive)
10902
            {
10903
              inform (next_token->location, "(if you use %<-fpermissive%>"
10904
                      " G++ will accept your code)");
10905
              hint = true;
10906
            }
10907
        }
10908
    }
10909
  else
10910
    {
10911
      /* Look for the `<' that starts the template-argument-list.  */
10912
      if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10913
        {
10914
          pop_deferring_access_checks ();
10915
          return error_mark_node;
10916
        }
10917
      /* Parse the arguments.  */
10918
      arguments = cp_parser_enclosed_template_argument_list (parser);
10919
    }
10920
 
10921
  /* Build a representation of the specialization.  */
10922
  if (TREE_CODE (templ) == IDENTIFIER_NODE)
10923
    template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10924
  else if (DECL_CLASS_TEMPLATE_P (templ)
10925
           || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10926
    {
10927
      bool entering_scope;
10928
      /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10929
         template (rather than some instantiation thereof) only if
10930
         is not nested within some other construct.  For example, in
10931
         "template <typename T> void f(T) { A<T>::", A<T> is just an
10932
         instantiation of A.  */
10933
      entering_scope = (template_parm_scope_p ()
10934
                        && cp_lexer_next_token_is (parser->lexer,
10935
                                                   CPP_SCOPE));
10936
      template_id
10937
        = finish_template_type (templ, arguments, entering_scope);
10938
    }
10939
  else
10940
    {
10941
      /* If it's not a class-template or a template-template, it should be
10942
         a function-template.  */
10943
      gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10944
                   || TREE_CODE (templ) == OVERLOAD
10945
                   || BASELINK_P (templ)));
10946
 
10947
      template_id = lookup_template_function (templ, arguments);
10948
    }
10949
 
10950
  /* If parsing tentatively, replace the sequence of tokens that makes
10951
     up the template-id with a CPP_TEMPLATE_ID token.  That way,
10952
     should we re-parse the token stream, we will not have to repeat
10953
     the effort required to do the parse, nor will we issue duplicate
10954
     error messages about problems during instantiation of the
10955
     template.  */
10956
  if (start_of_id)
10957
    {
10958
      cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10959
 
10960
      /* Reset the contents of the START_OF_ID token.  */
10961
      token->type = CPP_TEMPLATE_ID;
10962
      /* Retrieve any deferred checks.  Do not pop this access checks yet
10963
         so the memory will not be reclaimed during token replacing below.  */
10964
      token->u.tree_check_value = GGC_CNEW (struct tree_check);
10965
      token->u.tree_check_value->value = template_id;
10966
      token->u.tree_check_value->checks = get_deferred_access_checks ();
10967
      token->keyword = RID_MAX;
10968
 
10969
      /* Purge all subsequent tokens.  */
10970
      cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10971
 
10972
      /* ??? Can we actually assume that, if template_id ==
10973
         error_mark_node, we will have issued a diagnostic to the
10974
         user, as opposed to simply marking the tentative parse as
10975
         failed?  */
10976
      if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10977
        error_at (token->location, "parse error in template argument list");
10978
    }
10979
 
10980
  pop_deferring_access_checks ();
10981
  return template_id;
10982
}
10983
 
10984
/* Parse a template-name.
10985
 
10986
   template-name:
10987
     identifier
10988
 
10989
   The standard should actually say:
10990
 
10991
   template-name:
10992
     identifier
10993
     operator-function-id
10994
 
10995
   A defect report has been filed about this issue.
10996
 
10997
   A conversion-function-id cannot be a template name because they cannot
10998
   be part of a template-id. In fact, looking at this code:
10999
 
11000
   a.operator K<int>()
11001
 
11002
   the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11003
   It is impossible to call a templated conversion-function-id with an
11004
   explicit argument list, since the only allowed template parameter is
11005
   the type to which it is converting.
11006
 
11007
   If TEMPLATE_KEYWORD_P is true, then we have just seen the
11008
   `template' keyword, in a construction like:
11009
 
11010
     T::template f<3>()
11011
 
11012
   In that case `f' is taken to be a template-name, even though there
11013
   is no way of knowing for sure.
11014
 
11015
   Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11016
   name refers to a set of overloaded functions, at least one of which
11017
   is a template, or an IDENTIFIER_NODE with the name of the template,
11018
   if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11019
   names are looked up inside uninstantiated templates.  */
11020
 
11021
static tree
11022
cp_parser_template_name (cp_parser* parser,
11023
                         bool template_keyword_p,
11024
                         bool check_dependency_p,
11025
                         bool is_declaration,
11026
                         bool *is_identifier)
11027
{
11028
  tree identifier;
11029
  tree decl;
11030
  tree fns;
11031
  cp_token *token = cp_lexer_peek_token (parser->lexer);
11032
 
11033
  /* If the next token is `operator', then we have either an
11034
     operator-function-id or a conversion-function-id.  */
11035
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11036
    {
11037
      /* We don't know whether we're looking at an
11038
         operator-function-id or a conversion-function-id.  */
11039
      cp_parser_parse_tentatively (parser);
11040
      /* Try an operator-function-id.  */
11041
      identifier = cp_parser_operator_function_id (parser);
11042
      /* If that didn't work, try a conversion-function-id.  */
11043
      if (!cp_parser_parse_definitely (parser))
11044
        {
11045
          cp_parser_error (parser, "expected template-name");
11046
          return error_mark_node;
11047
        }
11048
    }
11049
  /* Look for the identifier.  */
11050
  else
11051
    identifier = cp_parser_identifier (parser);
11052
 
11053
  /* If we didn't find an identifier, we don't have a template-id.  */
11054
  if (identifier == error_mark_node)
11055
    return error_mark_node;
11056
 
11057
  /* If the name immediately followed the `template' keyword, then it
11058
     is a template-name.  However, if the next token is not `<', then
11059
     we do not treat it as a template-name, since it is not being used
11060
     as part of a template-id.  This enables us to handle constructs
11061
     like:
11062
 
11063
       template <typename T> struct S { S(); };
11064
       template <typename T> S<T>::S();
11065
 
11066
     correctly.  We would treat `S' as a template -- if it were `S<T>'
11067
     -- but we do not if there is no `<'.  */
11068
 
11069
  if (processing_template_decl
11070
      && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11071
    {
11072
      /* In a declaration, in a dependent context, we pretend that the
11073
         "template" keyword was present in order to improve error
11074
         recovery.  For example, given:
11075
 
11076
           template <typename T> void f(T::X<int>);
11077
 
11078
         we want to treat "X<int>" as a template-id.  */
11079
      if (is_declaration
11080
          && !template_keyword_p
11081
          && parser->scope && TYPE_P (parser->scope)
11082
          && check_dependency_p
11083
          && dependent_scope_p (parser->scope)
11084
          /* Do not do this for dtors (or ctors), since they never
11085
             need the template keyword before their name.  */
11086
          && !constructor_name_p (identifier, parser->scope))
11087
        {
11088
          cp_token_position start = 0;
11089
 
11090
          /* Explain what went wrong.  */
11091
          error_at (token->location, "non-template %qD used as template",
11092
                    identifier);
11093
          inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11094
                  parser->scope, identifier);
11095
          /* If parsing tentatively, find the location of the "<" token.  */
11096
          if (cp_parser_simulate_error (parser))
11097
            start = cp_lexer_token_position (parser->lexer, true);
11098
          /* Parse the template arguments so that we can issue error
11099
             messages about them.  */
11100
          cp_lexer_consume_token (parser->lexer);
11101
          cp_parser_enclosed_template_argument_list (parser);
11102
          /* Skip tokens until we find a good place from which to
11103
             continue parsing.  */
11104
          cp_parser_skip_to_closing_parenthesis (parser,
11105
                                                 /*recovering=*/true,
11106
                                                 /*or_comma=*/true,
11107
                                                 /*consume_paren=*/false);
11108
          /* If parsing tentatively, permanently remove the
11109
             template argument list.  That will prevent duplicate
11110
             error messages from being issued about the missing
11111
             "template" keyword.  */
11112
          if (start)
11113
            cp_lexer_purge_tokens_after (parser->lexer, start);
11114
          if (is_identifier)
11115
            *is_identifier = true;
11116
          return identifier;
11117
        }
11118
 
11119
      /* If the "template" keyword is present, then there is generally
11120
         no point in doing name-lookup, so we just return IDENTIFIER.
11121
         But, if the qualifying scope is non-dependent then we can
11122
         (and must) do name-lookup normally.  */
11123
      if (template_keyword_p
11124
          && (!parser->scope
11125
              || (TYPE_P (parser->scope)
11126
                  && dependent_type_p (parser->scope))))
11127
        return identifier;
11128
    }
11129
 
11130
  /* Look up the name.  */
11131
  decl = cp_parser_lookup_name (parser, identifier,
11132
                                none_type,
11133
                                /*is_template=*/true,
11134
                                /*is_namespace=*/false,
11135
                                check_dependency_p,
11136
                                /*ambiguous_decls=*/NULL,
11137
                                token->location);
11138
 
11139
  /* If DECL is a template, then the name was a template-name.  */
11140
  if (TREE_CODE (decl) == TEMPLATE_DECL)
11141
    ;
11142
  else
11143
    {
11144
      tree fn = NULL_TREE;
11145
 
11146
      /* The standard does not explicitly indicate whether a name that
11147
         names a set of overloaded declarations, some of which are
11148
         templates, is a template-name.  However, such a name should
11149
         be a template-name; otherwise, there is no way to form a
11150
         template-id for the overloaded templates.  */
11151
      fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11152
      if (TREE_CODE (fns) == OVERLOAD)
11153
        for (fn = fns; fn; fn = OVL_NEXT (fn))
11154
          if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11155
            break;
11156
 
11157
      if (!fn)
11158
        {
11159
          /* The name does not name a template.  */
11160
          cp_parser_error (parser, "expected template-name");
11161
          return error_mark_node;
11162
        }
11163
    }
11164
 
11165
  /* If DECL is dependent, and refers to a function, then just return
11166
     its name; we will look it up again during template instantiation.  */
11167
  if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11168
    {
11169
      tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11170
      if (TYPE_P (scope) && dependent_type_p (scope))
11171
        return identifier;
11172
    }
11173
 
11174
  return decl;
11175
}
11176
 
11177
/* Parse a template-argument-list.
11178
 
11179
   template-argument-list:
11180
     template-argument ... [opt]
11181
     template-argument-list , template-argument ... [opt]
11182
 
11183
   Returns a TREE_VEC containing the arguments.  */
11184
 
11185
static tree
11186
cp_parser_template_argument_list (cp_parser* parser)
11187
{
11188
  tree fixed_args[10];
11189
  unsigned n_args = 0;
11190
  unsigned alloced = 10;
11191
  tree *arg_ary = fixed_args;
11192
  tree vec;
11193
  bool saved_in_template_argument_list_p;
11194
  bool saved_ice_p;
11195
  bool saved_non_ice_p;
11196
 
11197
  saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11198
  parser->in_template_argument_list_p = true;
11199
  /* Even if the template-id appears in an integral
11200
     constant-expression, the contents of the argument list do
11201
     not.  */
11202
  saved_ice_p = parser->integral_constant_expression_p;
11203
  parser->integral_constant_expression_p = false;
11204
  saved_non_ice_p = parser->non_integral_constant_expression_p;
11205
  parser->non_integral_constant_expression_p = false;
11206
  /* Parse the arguments.  */
11207
  do
11208
    {
11209
      tree argument;
11210
 
11211
      if (n_args)
11212
        /* Consume the comma.  */
11213
        cp_lexer_consume_token (parser->lexer);
11214
 
11215
      /* Parse the template-argument.  */
11216
      argument = cp_parser_template_argument (parser);
11217
 
11218
      /* If the next token is an ellipsis, we're expanding a template
11219
         argument pack. */
11220
      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11221
        {
11222
          if (argument == error_mark_node)
11223
            {
11224
              cp_token *token = cp_lexer_peek_token (parser->lexer);
11225
              error_at (token->location,
11226
                        "expected parameter pack before %<...%>");
11227
            }
11228
          /* Consume the `...' token. */
11229
          cp_lexer_consume_token (parser->lexer);
11230
 
11231
          /* Make the argument into a TYPE_PACK_EXPANSION or
11232
             EXPR_PACK_EXPANSION. */
11233
          argument = make_pack_expansion (argument);
11234
        }
11235
 
11236
      if (n_args == alloced)
11237
        {
11238
          alloced *= 2;
11239
 
11240
          if (arg_ary == fixed_args)
11241
            {
11242
              arg_ary = XNEWVEC (tree, alloced);
11243
              memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11244
            }
11245
          else
11246
            arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11247
        }
11248
      arg_ary[n_args++] = argument;
11249
    }
11250
  while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11251
 
11252
  vec = make_tree_vec (n_args);
11253
 
11254
  while (n_args--)
11255
    TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11256
 
11257
  if (arg_ary != fixed_args)
11258
    free (arg_ary);
11259
  parser->non_integral_constant_expression_p = saved_non_ice_p;
11260
  parser->integral_constant_expression_p = saved_ice_p;
11261
  parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11262
#ifdef ENABLE_CHECKING
11263
  SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11264
#endif
11265
  return vec;
11266
}
11267
 
11268
/* Parse a template-argument.
11269
 
11270
   template-argument:
11271
     assignment-expression
11272
     type-id
11273
     id-expression
11274
 
11275
   The representation is that of an assignment-expression, type-id, or
11276
   id-expression -- except that the qualified id-expression is
11277
   evaluated, so that the value returned is either a DECL or an
11278
   OVERLOAD.
11279
 
11280
   Although the standard says "assignment-expression", it forbids
11281
   throw-expressions or assignments in the template argument.
11282
   Therefore, we use "conditional-expression" instead.  */
11283
 
11284
static tree
11285
cp_parser_template_argument (cp_parser* parser)
11286
{
11287
  tree argument;
11288
  bool template_p;
11289
  bool address_p;
11290
  bool maybe_type_id = false;
11291
  cp_token *token = NULL, *argument_start_token = NULL;
11292
  cp_id_kind idk;
11293
 
11294
  /* There's really no way to know what we're looking at, so we just
11295
     try each alternative in order.
11296
 
11297
       [temp.arg]
11298
 
11299
       In a template-argument, an ambiguity between a type-id and an
11300
       expression is resolved to a type-id, regardless of the form of
11301
       the corresponding template-parameter.
11302
 
11303
     Therefore, we try a type-id first.  */
11304
  cp_parser_parse_tentatively (parser);
11305
  argument = cp_parser_template_type_arg (parser);
11306
  /* If there was no error parsing the type-id but the next token is a
11307
     '>>', our behavior depends on which dialect of C++ we're
11308
     parsing. In C++98, we probably found a typo for '> >'. But there
11309
     are type-id which are also valid expressions. For instance:
11310
 
11311
     struct X { int operator >> (int); };
11312
     template <int V> struct Foo {};
11313
     Foo<X () >> 5> r;
11314
 
11315
     Here 'X()' is a valid type-id of a function type, but the user just
11316
     wanted to write the expression "X() >> 5". Thus, we remember that we
11317
     found a valid type-id, but we still try to parse the argument as an
11318
     expression to see what happens.
11319
 
11320
     In C++0x, the '>>' will be considered two separate '>'
11321
     tokens.  */
11322
  if (!cp_parser_error_occurred (parser)
11323
      && cxx_dialect == cxx98
11324
      && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11325
    {
11326
      maybe_type_id = true;
11327
      cp_parser_abort_tentative_parse (parser);
11328
    }
11329
  else
11330
    {
11331
      /* If the next token isn't a `,' or a `>', then this argument wasn't
11332
      really finished. This means that the argument is not a valid
11333
      type-id.  */
11334
      if (!cp_parser_next_token_ends_template_argument_p (parser))
11335
        cp_parser_error (parser, "expected template-argument");
11336
      /* If that worked, we're done.  */
11337
      if (cp_parser_parse_definitely (parser))
11338
        return argument;
11339
    }
11340
  /* We're still not sure what the argument will be.  */
11341
  cp_parser_parse_tentatively (parser);
11342
  /* Try a template.  */
11343
  argument_start_token = cp_lexer_peek_token (parser->lexer);
11344
  argument = cp_parser_id_expression (parser,
11345
                                      /*template_keyword_p=*/false,
11346
                                      /*check_dependency_p=*/true,
11347
                                      &template_p,
11348
                                      /*declarator_p=*/false,
11349
                                      /*optional_p=*/false);
11350
  /* If the next token isn't a `,' or a `>', then this argument wasn't
11351
     really finished.  */
11352
  if (!cp_parser_next_token_ends_template_argument_p (parser))
11353
    cp_parser_error (parser, "expected template-argument");
11354
  if (!cp_parser_error_occurred (parser))
11355
    {
11356
      /* Figure out what is being referred to.  If the id-expression
11357
         was for a class template specialization, then we will have a
11358
         TYPE_DECL at this point.  There is no need to do name lookup
11359
         at this point in that case.  */
11360
      if (TREE_CODE (argument) != TYPE_DECL)
11361
        argument = cp_parser_lookup_name (parser, argument,
11362
                                          none_type,
11363
                                          /*is_template=*/template_p,
11364
                                          /*is_namespace=*/false,
11365
                                          /*check_dependency=*/true,
11366
                                          /*ambiguous_decls=*/NULL,
11367
                                          argument_start_token->location);
11368
      if (TREE_CODE (argument) != TEMPLATE_DECL
11369
          && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11370
        cp_parser_error (parser, "expected template-name");
11371
    }
11372
  if (cp_parser_parse_definitely (parser))
11373
    return argument;
11374
  /* It must be a non-type argument.  There permitted cases are given
11375
     in [temp.arg.nontype]:
11376
 
11377
     -- an integral constant-expression of integral or enumeration
11378
        type; or
11379
 
11380
     -- the name of a non-type template-parameter; or
11381
 
11382
     -- the name of an object or function with external linkage...
11383
 
11384
     -- the address of an object or function with external linkage...
11385
 
11386
     -- a pointer to member...  */
11387
  /* Look for a non-type template parameter.  */
11388
  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11389
    {
11390
      cp_parser_parse_tentatively (parser);
11391
      argument = cp_parser_primary_expression (parser,
11392
                                               /*address_p=*/false,
11393
                                               /*cast_p=*/false,
11394
                                               /*template_arg_p=*/true,
11395
                                               &idk);
11396
      if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11397
          || !cp_parser_next_token_ends_template_argument_p (parser))
11398
        cp_parser_simulate_error (parser);
11399
      if (cp_parser_parse_definitely (parser))
11400
        return argument;
11401
    }
11402
 
11403
  /* If the next token is "&", the argument must be the address of an
11404
     object or function with external linkage.  */
11405
  address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11406
  if (address_p)
11407
    cp_lexer_consume_token (parser->lexer);
11408
  /* See if we might have an id-expression.  */
11409
  token = cp_lexer_peek_token (parser->lexer);
11410
  if (token->type == CPP_NAME
11411
      || token->keyword == RID_OPERATOR
11412
      || token->type == CPP_SCOPE
11413
      || token->type == CPP_TEMPLATE_ID
11414
      || token->type == CPP_NESTED_NAME_SPECIFIER)
11415
    {
11416
      cp_parser_parse_tentatively (parser);
11417
      argument = cp_parser_primary_expression (parser,
11418
                                               address_p,
11419
                                               /*cast_p=*/false,
11420
                                               /*template_arg_p=*/true,
11421
                                               &idk);
11422
      if (cp_parser_error_occurred (parser)
11423
          || !cp_parser_next_token_ends_template_argument_p (parser))
11424
        cp_parser_abort_tentative_parse (parser);
11425
      else
11426
        {
11427
          tree probe;
11428
 
11429
          if (TREE_CODE (argument) == INDIRECT_REF)
11430
            {
11431
              gcc_assert (REFERENCE_REF_P (argument));
11432
              argument = TREE_OPERAND (argument, 0);
11433
            }
11434
 
11435
          /* If we're in a template, we represent a qualified-id referring
11436
             to a static data member as a SCOPE_REF even if the scope isn't
11437
             dependent so that we can check access control later.  */
11438
          probe = argument;
11439
          if (TREE_CODE (probe) == SCOPE_REF)
11440
            probe = TREE_OPERAND (probe, 1);
11441
          if (TREE_CODE (probe) == VAR_DECL)
11442
            {
11443
              /* A variable without external linkage might still be a
11444
                 valid constant-expression, so no error is issued here
11445
                 if the external-linkage check fails.  */
11446
              if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11447
                cp_parser_simulate_error (parser);
11448
            }
11449
          else if (is_overloaded_fn (argument))
11450
            /* All overloaded functions are allowed; if the external
11451
               linkage test does not pass, an error will be issued
11452
               later.  */
11453
            ;
11454
          else if (address_p
11455
                   && (TREE_CODE (argument) == OFFSET_REF
11456
                       || TREE_CODE (argument) == SCOPE_REF))
11457
            /* A pointer-to-member.  */
11458
            ;
11459
          else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11460
            ;
11461
          else
11462
            cp_parser_simulate_error (parser);
11463
 
11464
          if (cp_parser_parse_definitely (parser))
11465
            {
11466
              if (address_p)
11467
                argument = build_x_unary_op (ADDR_EXPR, argument,
11468
                                             tf_warning_or_error);
11469
              return argument;
11470
            }
11471
        }
11472
    }
11473
  /* If the argument started with "&", there are no other valid
11474
     alternatives at this point.  */
11475
  if (address_p)
11476
    {
11477
      cp_parser_error (parser, "invalid non-type template argument");
11478
      return error_mark_node;
11479
    }
11480
 
11481
  /* If the argument wasn't successfully parsed as a type-id followed
11482
     by '>>', the argument can only be a constant expression now.
11483
     Otherwise, we try parsing the constant-expression tentatively,
11484
     because the argument could really be a type-id.  */
11485
  if (maybe_type_id)
11486
    cp_parser_parse_tentatively (parser);
11487
  argument = cp_parser_constant_expression (parser,
11488
                                            /*allow_non_constant_p=*/false,
11489
                                            /*non_constant_p=*/NULL);
11490
  argument = fold_non_dependent_expr (argument);
11491
  if (!maybe_type_id)
11492
    return argument;
11493
  if (!cp_parser_next_token_ends_template_argument_p (parser))
11494
    cp_parser_error (parser, "expected template-argument");
11495
  if (cp_parser_parse_definitely (parser))
11496
    return argument;
11497
  /* We did our best to parse the argument as a non type-id, but that
11498
     was the only alternative that matched (albeit with a '>' after
11499
     it). We can assume it's just a typo from the user, and a
11500
     diagnostic will then be issued.  */
11501
  return cp_parser_template_type_arg (parser);
11502
}
11503
 
11504
/* Parse an explicit-instantiation.
11505
 
11506
   explicit-instantiation:
11507
     template declaration
11508
 
11509
   Although the standard says `declaration', what it really means is:
11510
 
11511
   explicit-instantiation:
11512
     template decl-specifier-seq [opt] declarator [opt] ;
11513
 
11514
   Things like `template int S<int>::i = 5, int S<double>::j;' are not
11515
   supposed to be allowed.  A defect report has been filed about this
11516
   issue.
11517
 
11518
   GNU Extension:
11519
 
11520
   explicit-instantiation:
11521
     storage-class-specifier template
11522
       decl-specifier-seq [opt] declarator [opt] ;
11523
     function-specifier template
11524
       decl-specifier-seq [opt] declarator [opt] ;  */
11525
 
11526
static void
11527
cp_parser_explicit_instantiation (cp_parser* parser)
11528
{
11529
  int declares_class_or_enum;
11530
  cp_decl_specifier_seq decl_specifiers;
11531
  tree extension_specifier = NULL_TREE;
11532
 
11533
  /* Look for an (optional) storage-class-specifier or
11534
     function-specifier.  */
11535
  if (cp_parser_allow_gnu_extensions_p (parser))
11536
    {
11537
      extension_specifier
11538
        = cp_parser_storage_class_specifier_opt (parser);
11539
      if (!extension_specifier)
11540
        extension_specifier
11541
          = cp_parser_function_specifier_opt (parser,
11542
                                              /*decl_specs=*/NULL);
11543
    }
11544
 
11545
  /* Look for the `template' keyword.  */
11546
  cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11547
  /* Let the front end know that we are processing an explicit
11548
     instantiation.  */
11549
  begin_explicit_instantiation ();
11550
  /* [temp.explicit] says that we are supposed to ignore access
11551
     control while processing explicit instantiation directives.  */
11552
  push_deferring_access_checks (dk_no_check);
11553
  /* Parse a decl-specifier-seq.  */
11554
  cp_parser_decl_specifier_seq (parser,
11555
                                CP_PARSER_FLAGS_OPTIONAL,
11556
                                &decl_specifiers,
11557
                                &declares_class_or_enum);
11558
  /* If there was exactly one decl-specifier, and it declared a class,
11559
     and there's no declarator, then we have an explicit type
11560
     instantiation.  */
11561
  if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11562
    {
11563
      tree type;
11564
 
11565
      type = check_tag_decl (&decl_specifiers);
11566
      /* Turn access control back on for names used during
11567
         template instantiation.  */
11568
      pop_deferring_access_checks ();
11569
      if (type)
11570
        do_type_instantiation (type, extension_specifier,
11571
                               /*complain=*/tf_error);
11572
    }
11573
  else
11574
    {
11575
      cp_declarator *declarator;
11576
      tree decl;
11577
 
11578
      /* Parse the declarator.  */
11579
      declarator
11580
        = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11581
                                /*ctor_dtor_or_conv_p=*/NULL,
11582
                                /*parenthesized_p=*/NULL,
11583
                                /*member_p=*/false);
11584
      if (declares_class_or_enum & 2)
11585
        cp_parser_check_for_definition_in_return_type (declarator,
11586
                                                       decl_specifiers.type,
11587
                                                       decl_specifiers.type_location);
11588
      if (declarator != cp_error_declarator)
11589
        {
11590
          decl = grokdeclarator (declarator, &decl_specifiers,
11591
                                 NORMAL, 0, &decl_specifiers.attributes);
11592
          /* Turn access control back on for names used during
11593
             template instantiation.  */
11594
          pop_deferring_access_checks ();
11595
          /* Do the explicit instantiation.  */
11596
          do_decl_instantiation (decl, extension_specifier);
11597
        }
11598
      else
11599
        {
11600
          pop_deferring_access_checks ();
11601
          /* Skip the body of the explicit instantiation.  */
11602
          cp_parser_skip_to_end_of_statement (parser);
11603
        }
11604
    }
11605
  /* We're done with the instantiation.  */
11606
  end_explicit_instantiation ();
11607
 
11608
  cp_parser_consume_semicolon_at_end_of_statement (parser);
11609
}
11610
 
11611
/* Parse an explicit-specialization.
11612
 
11613
   explicit-specialization:
11614
     template < > declaration
11615
 
11616
   Although the standard says `declaration', what it really means is:
11617
 
11618
   explicit-specialization:
11619
     template <> decl-specifier [opt] init-declarator [opt] ;
11620
     template <> function-definition
11621
     template <> explicit-specialization
11622
     template <> template-declaration  */
11623
 
11624
static void
11625
cp_parser_explicit_specialization (cp_parser* parser)
11626
{
11627
  bool need_lang_pop;
11628
  cp_token *token = cp_lexer_peek_token (parser->lexer);
11629
 
11630
  /* Look for the `template' keyword.  */
11631
  cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11632
  /* Look for the `<'.  */
11633
  cp_parser_require (parser, CPP_LESS, "%<<%>");
11634
  /* Look for the `>'.  */
11635
  cp_parser_require (parser, CPP_GREATER, "%<>%>");
11636
  /* We have processed another parameter list.  */
11637
  ++parser->num_template_parameter_lists;
11638
  /* [temp]
11639
 
11640
     A template ... explicit specialization ... shall not have C
11641
     linkage.  */
11642
  if (current_lang_name == lang_name_c)
11643
    {
11644
      error_at (token->location, "template specialization with C linkage");
11645
      /* Give it C++ linkage to avoid confusing other parts of the
11646
         front end.  */
11647
      push_lang_context (lang_name_cplusplus);
11648
      need_lang_pop = true;
11649
    }
11650
  else
11651
    need_lang_pop = false;
11652
  /* Let the front end know that we are beginning a specialization.  */
11653
  if (!begin_specialization ())
11654
    {
11655
      end_specialization ();
11656
      return;
11657
    }
11658
 
11659
  /* If the next keyword is `template', we need to figure out whether
11660
     or not we're looking a template-declaration.  */
11661
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11662
    {
11663
      if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11664
          && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11665
        cp_parser_template_declaration_after_export (parser,
11666
                                                     /*member_p=*/false);
11667
      else
11668
        cp_parser_explicit_specialization (parser);
11669
    }
11670
  else
11671
    /* Parse the dependent declaration.  */
11672
    cp_parser_single_declaration (parser,
11673
                                  /*checks=*/NULL,
11674
                                  /*member_p=*/false,
11675
                                  /*explicit_specialization_p=*/true,
11676
                                  /*friend_p=*/NULL);
11677
  /* We're done with the specialization.  */
11678
  end_specialization ();
11679
  /* For the erroneous case of a template with C linkage, we pushed an
11680
     implicit C++ linkage scope; exit that scope now.  */
11681
  if (need_lang_pop)
11682
    pop_lang_context ();
11683
  /* We're done with this parameter list.  */
11684
  --parser->num_template_parameter_lists;
11685
}
11686
 
11687
/* Parse a type-specifier.
11688
 
11689
   type-specifier:
11690
     simple-type-specifier
11691
     class-specifier
11692
     enum-specifier
11693
     elaborated-type-specifier
11694
     cv-qualifier
11695
 
11696
   GNU Extension:
11697
 
11698
   type-specifier:
11699
     __complex__
11700
 
11701
   Returns a representation of the type-specifier.  For a
11702
   class-specifier, enum-specifier, or elaborated-type-specifier, a
11703
   TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11704
 
11705
   The parser flags FLAGS is used to control type-specifier parsing.
11706
 
11707
   If IS_DECLARATION is TRUE, then this type-specifier is appearing
11708
   in a decl-specifier-seq.
11709
 
11710
   If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11711
   class-specifier, enum-specifier, or elaborated-type-specifier, then
11712
   *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
11713
   if a type is declared; 2 if it is defined.  Otherwise, it is set to
11714
   zero.
11715
 
11716
   If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11717
   cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
11718
   is set to FALSE.  */
11719
 
11720
static tree
11721
cp_parser_type_specifier (cp_parser* parser,
11722
                          cp_parser_flags flags,
11723
                          cp_decl_specifier_seq *decl_specs,
11724
                          bool is_declaration,
11725
                          int* declares_class_or_enum,
11726
                          bool* is_cv_qualifier)
11727
{
11728
  tree type_spec = NULL_TREE;
11729
  cp_token *token;
11730
  enum rid keyword;
11731
  cp_decl_spec ds = ds_last;
11732
 
11733
  /* Assume this type-specifier does not declare a new type.  */
11734
  if (declares_class_or_enum)
11735
    *declares_class_or_enum = 0;
11736
  /* And that it does not specify a cv-qualifier.  */
11737
  if (is_cv_qualifier)
11738
    *is_cv_qualifier = false;
11739
  /* Peek at the next token.  */
11740
  token = cp_lexer_peek_token (parser->lexer);
11741
 
11742
  /* If we're looking at a keyword, we can use that to guide the
11743
     production we choose.  */
11744
  keyword = token->keyword;
11745
  switch (keyword)
11746
    {
11747
    case RID_ENUM:
11748
      if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11749
        goto elaborated_type_specifier;
11750
 
11751
      /* Look for the enum-specifier.  */
11752
      type_spec = cp_parser_enum_specifier (parser);
11753
      /* If that worked, we're done.  */
11754
      if (type_spec)
11755
        {
11756
          if (declares_class_or_enum)
11757
            *declares_class_or_enum = 2;
11758
          if (decl_specs)
11759
            cp_parser_set_decl_spec_type (decl_specs,
11760
                                          type_spec,
11761
                                          token->location,
11762
                                          /*user_defined_p=*/true);
11763
          return type_spec;
11764
        }
11765
      else
11766
        goto elaborated_type_specifier;
11767
 
11768
      /* Any of these indicate either a class-specifier, or an
11769
         elaborated-type-specifier.  */
11770
    case RID_CLASS:
11771
    case RID_STRUCT:
11772
    case RID_UNION:
11773
      if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11774
        goto elaborated_type_specifier;
11775
 
11776
      /* Parse tentatively so that we can back up if we don't find a
11777
         class-specifier.  */
11778
      cp_parser_parse_tentatively (parser);
11779
      /* Look for the class-specifier.  */
11780
      type_spec = cp_parser_class_specifier (parser);
11781
      invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11782
      /* If that worked, we're done.  */
11783
      if (cp_parser_parse_definitely (parser))
11784
        {
11785
          if (declares_class_or_enum)
11786
            *declares_class_or_enum = 2;
11787
          if (decl_specs)
11788
            cp_parser_set_decl_spec_type (decl_specs,
11789
                                          type_spec,
11790
                                          token->location,
11791
                                          /*user_defined_p=*/true);
11792
          return type_spec;
11793
        }
11794
 
11795
      /* Fall through.  */
11796
    elaborated_type_specifier:
11797
      /* We're declaring (not defining) a class or enum.  */
11798
      if (declares_class_or_enum)
11799
        *declares_class_or_enum = 1;
11800
 
11801
      /* Fall through.  */
11802
    case RID_TYPENAME:
11803
      /* Look for an elaborated-type-specifier.  */
11804
      type_spec
11805
        = (cp_parser_elaborated_type_specifier
11806
           (parser,
11807
            decl_specs && decl_specs->specs[(int) ds_friend],
11808
            is_declaration));
11809
      if (decl_specs)
11810
        cp_parser_set_decl_spec_type (decl_specs,
11811
                                      type_spec,
11812
                                      token->location,
11813
                                      /*user_defined_p=*/true);
11814
      return type_spec;
11815
 
11816
    case RID_CONST:
11817
      ds = ds_const;
11818
      if (is_cv_qualifier)
11819
        *is_cv_qualifier = true;
11820
      break;
11821
 
11822
    case RID_VOLATILE:
11823
      ds = ds_volatile;
11824
      if (is_cv_qualifier)
11825
        *is_cv_qualifier = true;
11826
      break;
11827
 
11828
    case RID_RESTRICT:
11829
      ds = ds_restrict;
11830
      if (is_cv_qualifier)
11831
        *is_cv_qualifier = true;
11832
      break;
11833
 
11834
    case RID_COMPLEX:
11835
      /* The `__complex__' keyword is a GNU extension.  */
11836
      ds = ds_complex;
11837
      break;
11838
 
11839
    default:
11840
      break;
11841
    }
11842
 
11843
  /* Handle simple keywords.  */
11844
  if (ds != ds_last)
11845
    {
11846
      if (decl_specs)
11847
        {
11848
          ++decl_specs->specs[(int)ds];
11849
          decl_specs->any_specifiers_p = true;
11850
        }
11851
      return cp_lexer_consume_token (parser->lexer)->u.value;
11852
    }
11853
 
11854
  /* If we do not already have a type-specifier, assume we are looking
11855
     at a simple-type-specifier.  */
11856
  type_spec = cp_parser_simple_type_specifier (parser,
11857
                                               decl_specs,
11858
                                               flags);
11859
 
11860
  /* If we didn't find a type-specifier, and a type-specifier was not
11861
     optional in this context, issue an error message.  */
11862
  if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11863
    {
11864
      cp_parser_error (parser, "expected type specifier");
11865
      return error_mark_node;
11866
    }
11867
 
11868
  return type_spec;
11869
}
11870
 
11871
/* Parse a simple-type-specifier.
11872
 
11873
   simple-type-specifier:
11874
     :: [opt] nested-name-specifier [opt] type-name
11875
     :: [opt] nested-name-specifier template template-id
11876
     char
11877
     wchar_t
11878
     bool
11879
     short
11880
     int
11881
     long
11882
     signed
11883
     unsigned
11884
     float
11885
     double
11886
     void
11887
 
11888
   C++0x Extension:
11889
 
11890
   simple-type-specifier:
11891
     auto
11892
     decltype ( expression )
11893
     char16_t
11894
     char32_t
11895
 
11896
   GNU Extension:
11897
 
11898
   simple-type-specifier:
11899
     __typeof__ unary-expression
11900
     __typeof__ ( type-id )
11901
 
11902
   Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11903
   appropriately updated.  */
11904
 
11905
static tree
11906
cp_parser_simple_type_specifier (cp_parser* parser,
11907
                                 cp_decl_specifier_seq *decl_specs,
11908
                                 cp_parser_flags flags)
11909
{
11910
  tree type = NULL_TREE;
11911
  cp_token *token;
11912
 
11913
  /* Peek at the next token.  */
11914
  token = cp_lexer_peek_token (parser->lexer);
11915
 
11916
  /* If we're looking at a keyword, things are easy.  */
11917
  switch (token->keyword)
11918
    {
11919
    case RID_CHAR:
11920
      if (decl_specs)
11921
        decl_specs->explicit_char_p = true;
11922
      type = char_type_node;
11923
      break;
11924
    case RID_CHAR16:
11925
      type = char16_type_node;
11926
      break;
11927
    case RID_CHAR32:
11928
      type = char32_type_node;
11929
      break;
11930
    case RID_WCHAR:
11931
      type = wchar_type_node;
11932
      break;
11933
    case RID_BOOL:
11934
      type = boolean_type_node;
11935
      break;
11936
    case RID_SHORT:
11937
      if (decl_specs)
11938
        ++decl_specs->specs[(int) ds_short];
11939
      type = short_integer_type_node;
11940
      break;
11941
    case RID_INT:
11942
      if (decl_specs)
11943
        decl_specs->explicit_int_p = true;
11944
      type = integer_type_node;
11945
      break;
11946
    case RID_LONG:
11947
      if (decl_specs)
11948
        ++decl_specs->specs[(int) ds_long];
11949
      type = long_integer_type_node;
11950
      break;
11951
    case RID_SIGNED:
11952
      if (decl_specs)
11953
        ++decl_specs->specs[(int) ds_signed];
11954
      type = integer_type_node;
11955
      break;
11956
    case RID_UNSIGNED:
11957
      if (decl_specs)
11958
        ++decl_specs->specs[(int) ds_unsigned];
11959
      type = unsigned_type_node;
11960
      break;
11961
    case RID_FLOAT:
11962
      type = float_type_node;
11963
      break;
11964
    case RID_DOUBLE:
11965
      type = double_type_node;
11966
      break;
11967
    case RID_VOID:
11968
      type = void_type_node;
11969
      break;
11970
 
11971
    case RID_AUTO:
11972
      maybe_warn_cpp0x (CPP0X_AUTO);
11973
      type = make_auto ();
11974
      break;
11975
 
11976
    case RID_DECLTYPE:
11977
      /* Parse the `decltype' type.  */
11978
      type = cp_parser_decltype (parser);
11979
 
11980
      if (decl_specs)
11981
        cp_parser_set_decl_spec_type (decl_specs, type,
11982
                                      token->location,
11983
                                      /*user_defined_p=*/true);
11984
 
11985
      return type;
11986
 
11987
    case RID_TYPEOF:
11988
      /* Consume the `typeof' token.  */
11989
      cp_lexer_consume_token (parser->lexer);
11990
      /* Parse the operand to `typeof'.  */
11991
      type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11992
      /* If it is not already a TYPE, take its type.  */
11993
      if (!TYPE_P (type))
11994
        type = finish_typeof (type);
11995
 
11996
      if (decl_specs)
11997
        cp_parser_set_decl_spec_type (decl_specs, type,
11998
                                      token->location,
11999
                                      /*user_defined_p=*/true);
12000
 
12001
      return type;
12002
 
12003
    default:
12004
      break;
12005
    }
12006
 
12007
  /* If the type-specifier was for a built-in type, we're done.  */
12008
  if (type)
12009
    {
12010
      /* Record the type.  */
12011
      if (decl_specs
12012
          && (token->keyword != RID_SIGNED
12013
              && token->keyword != RID_UNSIGNED
12014
              && token->keyword != RID_SHORT
12015
              && token->keyword != RID_LONG))
12016
        cp_parser_set_decl_spec_type (decl_specs,
12017
                                      type,
12018
                                      token->location,
12019
                                      /*user_defined=*/false);
12020
      if (decl_specs)
12021
        decl_specs->any_specifiers_p = true;
12022
 
12023
      /* Consume the token.  */
12024
      cp_lexer_consume_token (parser->lexer);
12025
 
12026
      /* There is no valid C++ program where a non-template type is
12027
         followed by a "<".  That usually indicates that the user thought
12028
         that the type was a template.  */
12029
      cp_parser_check_for_invalid_template_id (parser, type, token->location);
12030
 
12031
      return TYPE_NAME (type);
12032
    }
12033
 
12034
  /* The type-specifier must be a user-defined type.  */
12035
  if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12036
    {
12037
      bool qualified_p;
12038
      bool global_p;
12039
 
12040
      /* Don't gobble tokens or issue error messages if this is an
12041
         optional type-specifier.  */
12042
      if (flags & CP_PARSER_FLAGS_OPTIONAL)
12043
        cp_parser_parse_tentatively (parser);
12044
 
12045
      /* Look for the optional `::' operator.  */
12046
      global_p
12047
        = (cp_parser_global_scope_opt (parser,
12048
                                       /*current_scope_valid_p=*/false)
12049
           != NULL_TREE);
12050
      /* Look for the nested-name specifier.  */
12051
      qualified_p
12052
        = (cp_parser_nested_name_specifier_opt (parser,
12053
                                                /*typename_keyword_p=*/false,
12054
                                                /*check_dependency_p=*/true,
12055
                                                /*type_p=*/false,
12056
                                                /*is_declaration=*/false)
12057
           != NULL_TREE);
12058
      token = cp_lexer_peek_token (parser->lexer);
12059
      /* If we have seen a nested-name-specifier, and the next token
12060
         is `template', then we are using the template-id production.  */
12061
      if (parser->scope
12062
          && cp_parser_optional_template_keyword (parser))
12063
        {
12064
          /* Look for the template-id.  */
12065
          type = cp_parser_template_id (parser,
12066
                                        /*template_keyword_p=*/true,
12067
                                        /*check_dependency_p=*/true,
12068
                                        /*is_declaration=*/false);
12069
          /* If the template-id did not name a type, we are out of
12070
             luck.  */
12071
          if (TREE_CODE (type) != TYPE_DECL)
12072
            {
12073
              cp_parser_error (parser, "expected template-id for type");
12074
              type = NULL_TREE;
12075
            }
12076
        }
12077
      /* Otherwise, look for a type-name.  */
12078
      else
12079
        type = cp_parser_type_name (parser);
12080
      /* Keep track of all name-lookups performed in class scopes.  */
12081
      if (type
12082
          && !global_p
12083
          && !qualified_p
12084
          && TREE_CODE (type) == TYPE_DECL
12085
          && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12086
        maybe_note_name_used_in_class (DECL_NAME (type), type);
12087
      /* If it didn't work out, we don't have a TYPE.  */
12088
      if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12089
          && !cp_parser_parse_definitely (parser))
12090
        type = NULL_TREE;
12091
      if (type && decl_specs)
12092
        cp_parser_set_decl_spec_type (decl_specs, type,
12093
                                      token->location,
12094
                                      /*user_defined=*/true);
12095
    }
12096
 
12097
  /* If we didn't get a type-name, issue an error message.  */
12098
  if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12099
    {
12100
      cp_parser_error (parser, "expected type-name");
12101
      return error_mark_node;
12102
    }
12103
 
12104
  /* There is no valid C++ program where a non-template type is
12105
     followed by a "<".  That usually indicates that the user thought
12106
     that the type was a template.  */
12107
  if (type && type != error_mark_node)
12108
    {
12109
      /* As a last-ditch effort, see if TYPE is an Objective-C type.
12110
         If it is, then the '<'...'>' enclose protocol names rather than
12111
         template arguments, and so everything is fine.  */
12112
      if (c_dialect_objc ()
12113
          && (objc_is_id (type) || objc_is_class_name (type)))
12114
        {
12115
          tree protos = cp_parser_objc_protocol_refs_opt (parser);
12116
          tree qual_type = objc_get_protocol_qualified_type (type, protos);
12117
 
12118
          /* Clobber the "unqualified" type previously entered into
12119
             DECL_SPECS with the new, improved protocol-qualified version.  */
12120
          if (decl_specs)
12121
            decl_specs->type = qual_type;
12122
 
12123
          return qual_type;
12124
        }
12125
 
12126
      cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12127
                                               token->location);
12128
    }
12129
 
12130
  return type;
12131
}
12132
 
12133
/* Parse a type-name.
12134
 
12135
   type-name:
12136
     class-name
12137
     enum-name
12138
     typedef-name
12139
 
12140
   enum-name:
12141
     identifier
12142
 
12143
   typedef-name:
12144
     identifier
12145
 
12146
   Returns a TYPE_DECL for the type.  */
12147
 
12148
static tree
12149
cp_parser_type_name (cp_parser* parser)
12150
{
12151
  tree type_decl;
12152
 
12153
  /* We can't know yet whether it is a class-name or not.  */
12154
  cp_parser_parse_tentatively (parser);
12155
  /* Try a class-name.  */
12156
  type_decl = cp_parser_class_name (parser,
12157
                                    /*typename_keyword_p=*/false,
12158
                                    /*template_keyword_p=*/false,
12159
                                    none_type,
12160
                                    /*check_dependency_p=*/true,
12161
                                    /*class_head_p=*/false,
12162
                                    /*is_declaration=*/false);
12163
  /* If it's not a class-name, keep looking.  */
12164
  if (!cp_parser_parse_definitely (parser))
12165
    {
12166
      /* It must be a typedef-name or an enum-name.  */
12167
      return cp_parser_nonclass_name (parser);
12168
    }
12169
 
12170
  return type_decl;
12171
}
12172
 
12173
/* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12174
 
12175
   enum-name:
12176
     identifier
12177
 
12178
   typedef-name:
12179
     identifier
12180
 
12181
   Returns a TYPE_DECL for the type.  */
12182
 
12183
static tree
12184
cp_parser_nonclass_name (cp_parser* parser)
12185
{
12186
  tree type_decl;
12187
  tree identifier;
12188
 
12189
  cp_token *token = cp_lexer_peek_token (parser->lexer);
12190
  identifier = cp_parser_identifier (parser);
12191
  if (identifier == error_mark_node)
12192
    return error_mark_node;
12193
 
12194
  /* Look up the type-name.  */
12195
  type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12196
 
12197
  if (TREE_CODE (type_decl) != TYPE_DECL
12198
      && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12199
    {
12200
      /* See if this is an Objective-C type.  */
12201
      tree protos = cp_parser_objc_protocol_refs_opt (parser);
12202
      tree type = objc_get_protocol_qualified_type (identifier, protos);
12203
      if (type)
12204
        type_decl = TYPE_NAME (type);
12205
    }
12206
 
12207
  /* Issue an error if we did not find a type-name.  */
12208
  if (TREE_CODE (type_decl) != TYPE_DECL)
12209
    {
12210
      if (!cp_parser_simulate_error (parser))
12211
        cp_parser_name_lookup_error (parser, identifier, type_decl,
12212
                                     "is not a type", token->location);
12213
      return error_mark_node;
12214
    }
12215
  /* Remember that the name was used in the definition of the
12216
     current class so that we can check later to see if the
12217
     meaning would have been different after the class was
12218
     entirely defined.  */
12219
  else if (type_decl != error_mark_node
12220
           && !parser->scope)
12221
    maybe_note_name_used_in_class (identifier, type_decl);
12222
 
12223
  return type_decl;
12224
}
12225
 
12226
/* Parse an elaborated-type-specifier.  Note that the grammar given
12227
   here incorporates the resolution to DR68.
12228
 
12229
   elaborated-type-specifier:
12230
     class-key :: [opt] nested-name-specifier [opt] identifier
12231
     class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12232
     enum-key :: [opt] nested-name-specifier [opt] identifier
12233
     typename :: [opt] nested-name-specifier identifier
12234
     typename :: [opt] nested-name-specifier template [opt]
12235
       template-id
12236
 
12237
   GNU extension:
12238
 
12239
   elaborated-type-specifier:
12240
     class-key attributes :: [opt] nested-name-specifier [opt] identifier
12241
     class-key attributes :: [opt] nested-name-specifier [opt]
12242
               template [opt] template-id
12243
     enum attributes :: [opt] nested-name-specifier [opt] identifier
12244
 
12245
   If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12246
   declared `friend'.  If IS_DECLARATION is TRUE, then this
12247
   elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12248
   something is being declared.
12249
 
12250
   Returns the TYPE specified.  */
12251
 
12252
static tree
12253
cp_parser_elaborated_type_specifier (cp_parser* parser,
12254
                                     bool is_friend,
12255
                                     bool is_declaration)
12256
{
12257
  enum tag_types tag_type;
12258
  tree identifier;
12259
  tree type = NULL_TREE;
12260
  tree attributes = NULL_TREE;
12261
  tree globalscope;
12262
  cp_token *token = NULL;
12263
 
12264
  /* See if we're looking at the `enum' keyword.  */
12265
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12266
    {
12267
      /* Consume the `enum' token.  */
12268
      cp_lexer_consume_token (parser->lexer);
12269
      /* Remember that it's an enumeration type.  */
12270
      tag_type = enum_type;
12271
      /* Parse the optional `struct' or `class' key (for C++0x scoped
12272
         enums).  */
12273
      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12274
          || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12275
        {
12276
          if (cxx_dialect == cxx98)
12277
            maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12278
 
12279
          /* Consume the `struct' or `class'.  */
12280
          cp_lexer_consume_token (parser->lexer);
12281
        }
12282
      /* Parse the attributes.  */
12283
      attributes = cp_parser_attributes_opt (parser);
12284
    }
12285
  /* Or, it might be `typename'.  */
12286
  else if (cp_lexer_next_token_is_keyword (parser->lexer,
12287
                                           RID_TYPENAME))
12288
    {
12289
      /* Consume the `typename' token.  */
12290
      cp_lexer_consume_token (parser->lexer);
12291
      /* Remember that it's a `typename' type.  */
12292
      tag_type = typename_type;
12293
    }
12294
  /* Otherwise it must be a class-key.  */
12295
  else
12296
    {
12297
      tag_type = cp_parser_class_key (parser);
12298
      if (tag_type == none_type)
12299
        return error_mark_node;
12300
      /* Parse the attributes.  */
12301
      attributes = cp_parser_attributes_opt (parser);
12302
    }
12303
 
12304
  /* Look for the `::' operator.  */
12305
  globalscope =  cp_parser_global_scope_opt (parser,
12306
                                             /*current_scope_valid_p=*/false);
12307
  /* Look for the nested-name-specifier.  */
12308
  if (tag_type == typename_type && !globalscope)
12309
    {
12310
      if (!cp_parser_nested_name_specifier (parser,
12311
                                           /*typename_keyword_p=*/true,
12312
                                           /*check_dependency_p=*/true,
12313
                                           /*type_p=*/true,
12314
                                            is_declaration))
12315
        return error_mark_node;
12316
    }
12317
  else
12318
    /* Even though `typename' is not present, the proposed resolution
12319
       to Core Issue 180 says that in `class A<T>::B', `B' should be
12320
       considered a type-name, even if `A<T>' is dependent.  */
12321
    cp_parser_nested_name_specifier_opt (parser,
12322
                                         /*typename_keyword_p=*/true,
12323
                                         /*check_dependency_p=*/true,
12324
                                         /*type_p=*/true,
12325
                                         is_declaration);
12326
 /* For everything but enumeration types, consider a template-id.
12327
    For an enumeration type, consider only a plain identifier.  */
12328
  if (tag_type != enum_type)
12329
    {
12330
      bool template_p = false;
12331
      tree decl;
12332
 
12333
      /* Allow the `template' keyword.  */
12334
      template_p = cp_parser_optional_template_keyword (parser);
12335
      /* If we didn't see `template', we don't know if there's a
12336
         template-id or not.  */
12337
      if (!template_p)
12338
        cp_parser_parse_tentatively (parser);
12339
      /* Parse the template-id.  */
12340
      token = cp_lexer_peek_token (parser->lexer);
12341
      decl = cp_parser_template_id (parser, template_p,
12342
                                    /*check_dependency_p=*/true,
12343
                                    is_declaration);
12344
      /* If we didn't find a template-id, look for an ordinary
12345
         identifier.  */
12346
      if (!template_p && !cp_parser_parse_definitely (parser))
12347
        ;
12348
      /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12349
         in effect, then we must assume that, upon instantiation, the
12350
         template will correspond to a class.  */
12351
      else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12352
               && tag_type == typename_type)
12353
        type = make_typename_type (parser->scope, decl,
12354
                                   typename_type,
12355
                                   /*complain=*/tf_error);
12356
      /* If the `typename' keyword is in effect and DECL is not a type
12357
         decl. Then type is non existant.   */
12358
      else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12359
        type = NULL_TREE;
12360
      else
12361
        type = TREE_TYPE (decl);
12362
    }
12363
 
12364
  if (!type)
12365
    {
12366
      token = cp_lexer_peek_token (parser->lexer);
12367
      identifier = cp_parser_identifier (parser);
12368
 
12369
      if (identifier == error_mark_node)
12370
        {
12371
          parser->scope = NULL_TREE;
12372
          return error_mark_node;
12373
        }
12374
 
12375
      /* For a `typename', we needn't call xref_tag.  */
12376
      if (tag_type == typename_type
12377
          && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12378
        return cp_parser_make_typename_type (parser, parser->scope,
12379
                                             identifier,
12380
                                             token->location);
12381
      /* Look up a qualified name in the usual way.  */
12382
      if (parser->scope)
12383
        {
12384
          tree decl;
12385
          tree ambiguous_decls;
12386
 
12387
          decl = cp_parser_lookup_name (parser, identifier,
12388
                                        tag_type,
12389
                                        /*is_template=*/false,
12390
                                        /*is_namespace=*/false,
12391
                                        /*check_dependency=*/true,
12392
                                        &ambiguous_decls,
12393
                                        token->location);
12394
 
12395
          /* If the lookup was ambiguous, an error will already have been
12396
             issued.  */
12397
          if (ambiguous_decls)
12398
            return error_mark_node;
12399
 
12400
          /* If we are parsing friend declaration, DECL may be a
12401
             TEMPLATE_DECL tree node here.  However, we need to check
12402
             whether this TEMPLATE_DECL results in valid code.  Consider
12403
             the following example:
12404
 
12405
               namespace N {
12406
                 template <class T> class C {};
12407
               }
12408
               class X {
12409
                 template <class T> friend class N::C; // #1, valid code
12410
               };
12411
               template <class T> class Y {
12412
                 friend class N::C;                    // #2, invalid code
12413
               };
12414
 
12415
             For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12416
             name lookup of `N::C'.  We see that friend declaration must
12417
             be template for the code to be valid.  Note that
12418
             processing_template_decl does not work here since it is
12419
             always 1 for the above two cases.  */
12420
 
12421
          decl = (cp_parser_maybe_treat_template_as_class
12422
                  (decl, /*tag_name_p=*/is_friend
12423
                         && parser->num_template_parameter_lists));
12424
 
12425
          if (TREE_CODE (decl) != TYPE_DECL)
12426
            {
12427
              cp_parser_diagnose_invalid_type_name (parser,
12428
                                                    parser->scope,
12429
                                                    identifier,
12430
                                                    token->location);
12431
              return error_mark_node;
12432
            }
12433
 
12434
          if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12435
            {
12436
              bool allow_template = (parser->num_template_parameter_lists
12437
                                      || DECL_SELF_REFERENCE_P (decl));
12438
              type = check_elaborated_type_specifier (tag_type, decl,
12439
                                                      allow_template);
12440
 
12441
              if (type == error_mark_node)
12442
                return error_mark_node;
12443
            }
12444
 
12445
          /* Forward declarations of nested types, such as
12446
 
12447
               class C1::C2;
12448
               class C1::C2::C3;
12449
 
12450
             are invalid unless all components preceding the final '::'
12451
             are complete.  If all enclosing types are complete, these
12452
             declarations become merely pointless.
12453
 
12454
             Invalid forward declarations of nested types are errors
12455
             caught elsewhere in parsing.  Those that are pointless arrive
12456
             here.  */
12457
 
12458
          if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12459
              && !is_friend && !processing_explicit_instantiation)
12460
            warning (0, "declaration %qD does not declare anything", decl);
12461
 
12462
          type = TREE_TYPE (decl);
12463
        }
12464
      else
12465
        {
12466
          /* An elaborated-type-specifier sometimes introduces a new type and
12467
             sometimes names an existing type.  Normally, the rule is that it
12468
             introduces a new type only if there is not an existing type of
12469
             the same name already in scope.  For example, given:
12470
 
12471
               struct S {};
12472
               void f() { struct S s; }
12473
 
12474
             the `struct S' in the body of `f' is the same `struct S' as in
12475
             the global scope; the existing definition is used.  However, if
12476
             there were no global declaration, this would introduce a new
12477
             local class named `S'.
12478
 
12479
             An exception to this rule applies to the following code:
12480
 
12481
               namespace N { struct S; }
12482
 
12483
             Here, the elaborated-type-specifier names a new type
12484
             unconditionally; even if there is already an `S' in the
12485
             containing scope this declaration names a new type.
12486
             This exception only applies if the elaborated-type-specifier
12487
             forms the complete declaration:
12488
 
12489
               [class.name]
12490
 
12491
               A declaration consisting solely of `class-key identifier ;' is
12492
               either a redeclaration of the name in the current scope or a
12493
               forward declaration of the identifier as a class name.  It
12494
               introduces the name into the current scope.
12495
 
12496
             We are in this situation precisely when the next token is a `;'.
12497
 
12498
             An exception to the exception is that a `friend' declaration does
12499
             *not* name a new type; i.e., given:
12500
 
12501
               struct S { friend struct T; };
12502
 
12503
             `T' is not a new type in the scope of `S'.
12504
 
12505
             Also, `new struct S' or `sizeof (struct S)' never results in the
12506
             definition of a new type; a new type can only be declared in a
12507
             declaration context.  */
12508
 
12509
          tag_scope ts;
12510
          bool template_p;
12511
 
12512
          if (is_friend)
12513
            /* Friends have special name lookup rules.  */
12514
            ts = ts_within_enclosing_non_class;
12515
          else if (is_declaration
12516
                   && cp_lexer_next_token_is (parser->lexer,
12517
                                              CPP_SEMICOLON))
12518
            /* This is a `class-key identifier ;' */
12519
            ts = ts_current;
12520
          else
12521
            ts = ts_global;
12522
 
12523
          template_p =
12524
            (parser->num_template_parameter_lists
12525
             && (cp_parser_next_token_starts_class_definition_p (parser)
12526
                 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12527
          /* An unqualified name was used to reference this type, so
12528
             there were no qualifying templates.  */
12529
          if (!cp_parser_check_template_parameters (parser,
12530
                                                    /*num_templates=*/0,
12531
                                                    token->location,
12532
                                                    /*declarator=*/NULL))
12533
            return error_mark_node;
12534
          type = xref_tag (tag_type, identifier, ts, template_p);
12535
        }
12536
    }
12537
 
12538
  if (type == error_mark_node)
12539
    return error_mark_node;
12540
 
12541
  /* Allow attributes on forward declarations of classes.  */
12542
  if (attributes)
12543
    {
12544
      if (TREE_CODE (type) == TYPENAME_TYPE)
12545
        warning (OPT_Wattributes,
12546
                 "attributes ignored on uninstantiated type");
12547
      else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12548
               && ! processing_explicit_instantiation)
12549
        warning (OPT_Wattributes,
12550
                 "attributes ignored on template instantiation");
12551
      else if (is_declaration && cp_parser_declares_only_class_p (parser))
12552
        cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12553
      else
12554
        warning (OPT_Wattributes,
12555
                 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12556
    }
12557
 
12558
  if (tag_type != enum_type)
12559
    cp_parser_check_class_key (tag_type, type);
12560
 
12561
  /* A "<" cannot follow an elaborated type specifier.  If that
12562
     happens, the user was probably trying to form a template-id.  */
12563
  cp_parser_check_for_invalid_template_id (parser, type, token->location);
12564
 
12565
  return type;
12566
}
12567
 
12568
/* Parse an enum-specifier.
12569
 
12570
   enum-specifier:
12571
     enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12572
 
12573
   enum-key:
12574
     enum
12575
     enum class   [C++0x]
12576
     enum struct  [C++0x]
12577
 
12578
   enum-base:   [C++0x]
12579
     : type-specifier-seq
12580
 
12581
   GNU Extensions:
12582
     enum-key attributes[opt] identifier [opt] enum-base [opt]
12583
       { enumerator-list [opt] }attributes[opt]
12584
 
12585
   Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12586
   if the token stream isn't an enum-specifier after all.  */
12587
 
12588
static tree
12589
cp_parser_enum_specifier (cp_parser* parser)
12590
{
12591
  tree identifier;
12592
  tree type;
12593
  tree attributes;
12594
  bool scoped_enum_p = false;
12595
  bool has_underlying_type = false;
12596
  tree underlying_type = NULL_TREE;
12597
 
12598
  /* Parse tentatively so that we can back up if we don't find a
12599
     enum-specifier.  */
12600
  cp_parser_parse_tentatively (parser);
12601
 
12602
  /* Caller guarantees that the current token is 'enum', an identifier
12603
     possibly follows, and the token after that is an opening brace.
12604
     If we don't have an identifier, fabricate an anonymous name for
12605
     the enumeration being defined.  */
12606
  cp_lexer_consume_token (parser->lexer);
12607
 
12608
  /* Parse the "class" or "struct", which indicates a scoped
12609
     enumeration type in C++0x.  */
12610
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12611
      || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12612
    {
12613
      if (cxx_dialect == cxx98)
12614
        maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12615
 
12616
      /* Consume the `struct' or `class' token.  */
12617
      cp_lexer_consume_token (parser->lexer);
12618
 
12619
      scoped_enum_p = true;
12620
    }
12621
 
12622
  attributes = cp_parser_attributes_opt (parser);
12623
 
12624
  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12625
    identifier = cp_parser_identifier (parser);
12626
  else
12627
    identifier = make_anon_name ();
12628
 
12629
  /* Check for the `:' that denotes a specified underlying type in C++0x.
12630
     Note that a ':' could also indicate a bitfield width, however.  */
12631
  if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12632
    {
12633
      cp_decl_specifier_seq type_specifiers;
12634
 
12635
      /* Consume the `:'.  */
12636
      cp_lexer_consume_token (parser->lexer);
12637
 
12638
      /* Parse the type-specifier-seq.  */
12639
      cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12640
                                    /*is_trailing_return=*/false,
12641
                                    &type_specifiers);
12642
 
12643
      /* At this point this is surely not elaborated type specifier.  */
12644
      if (!cp_parser_parse_definitely (parser))
12645
        return NULL_TREE;
12646
 
12647
      if (cxx_dialect == cxx98)
12648
        maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12649
 
12650
      has_underlying_type = true;
12651
 
12652
      /* If that didn't work, stop.  */
12653
      if (type_specifiers.type != error_mark_node)
12654
        {
12655
          underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12656
                                            /*initialized=*/0, NULL);
12657
          if (underlying_type == error_mark_node)
12658
            underlying_type = NULL_TREE;
12659
        }
12660
    }
12661
 
12662
  /* Look for the `{' but don't consume it yet.  */
12663
  if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12664
    {
12665
      cp_parser_error (parser, "expected %<{%>");
12666
      if (has_underlying_type)
12667
        return NULL_TREE;
12668
    }
12669
 
12670
  if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12671
    return NULL_TREE;
12672
 
12673
  /* Issue an error message if type-definitions are forbidden here.  */
12674
  if (!cp_parser_check_type_definition (parser))
12675
    type = error_mark_node;
12676
  else
12677
    /* Create the new type.  We do this before consuming the opening
12678
       brace so the enum will be recorded as being on the line of its
12679
       tag (or the 'enum' keyword, if there is no tag).  */
12680
    type = start_enum (identifier, underlying_type, scoped_enum_p);
12681
 
12682
  /* Consume the opening brace.  */
12683
  cp_lexer_consume_token (parser->lexer);
12684
 
12685
  if (type == error_mark_node)
12686
    {
12687
      cp_parser_skip_to_end_of_block_or_statement (parser);
12688
      return error_mark_node;
12689
    }
12690
 
12691
  /* If the next token is not '}', then there are some enumerators.  */
12692
  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12693
    cp_parser_enumerator_list (parser, type);
12694
 
12695
  /* Consume the final '}'.  */
12696
  cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12697
 
12698
  /* Look for trailing attributes to apply to this enumeration, and
12699
     apply them if appropriate.  */
12700
  if (cp_parser_allow_gnu_extensions_p (parser))
12701
    {
12702
      tree trailing_attr = cp_parser_attributes_opt (parser);
12703
      trailing_attr = chainon (trailing_attr, attributes);
12704
      cplus_decl_attributes (&type,
12705
                             trailing_attr,
12706
                             (int) ATTR_FLAG_TYPE_IN_PLACE);
12707
    }
12708
 
12709
  /* Finish up the enumeration.  */
12710
  finish_enum (type);
12711
 
12712
  return type;
12713
}
12714
 
12715
/* Parse an enumerator-list.  The enumerators all have the indicated
12716
   TYPE.
12717
 
12718
   enumerator-list:
12719
     enumerator-definition
12720
     enumerator-list , enumerator-definition  */
12721
 
12722
static void
12723
cp_parser_enumerator_list (cp_parser* parser, tree type)
12724
{
12725
  while (true)
12726
    {
12727
      /* Parse an enumerator-definition.  */
12728
      cp_parser_enumerator_definition (parser, type);
12729
 
12730
      /* If the next token is not a ',', we've reached the end of
12731
         the list.  */
12732
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12733
        break;
12734
      /* Otherwise, consume the `,' and keep going.  */
12735
      cp_lexer_consume_token (parser->lexer);
12736
      /* If the next token is a `}', there is a trailing comma.  */
12737
      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12738
        {
12739
          if (!in_system_header)
12740
            pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12741
          break;
12742
        }
12743
    }
12744
}
12745
 
12746
/* Parse an enumerator-definition.  The enumerator has the indicated
12747
   TYPE.
12748
 
12749
   enumerator-definition:
12750
     enumerator
12751
     enumerator = constant-expression
12752
 
12753
   enumerator:
12754
     identifier  */
12755
 
12756
static void
12757
cp_parser_enumerator_definition (cp_parser* parser, tree type)
12758
{
12759
  tree identifier;
12760
  tree value;
12761
 
12762
  /* Look for the identifier.  */
12763
  identifier = cp_parser_identifier (parser);
12764
  if (identifier == error_mark_node)
12765
    return;
12766
 
12767
  /* If the next token is an '=', then there is an explicit value.  */
12768
  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12769
    {
12770
      /* Consume the `=' token.  */
12771
      cp_lexer_consume_token (parser->lexer);
12772
      /* Parse the value.  */
12773
      value = cp_parser_constant_expression (parser,
12774
                                             /*allow_non_constant_p=*/false,
12775
                                             NULL);
12776
    }
12777
  else
12778
    value = NULL_TREE;
12779
 
12780
  /* If we are processing a template, make sure the initializer of the
12781
     enumerator doesn't contain any bare template parameter pack.  */
12782
  if (check_for_bare_parameter_packs (value))
12783
    value = error_mark_node;
12784
 
12785
  /* Create the enumerator.  */
12786
  build_enumerator (identifier, value, type);
12787
}
12788
 
12789
/* Parse a namespace-name.
12790
 
12791
   namespace-name:
12792
     original-namespace-name
12793
     namespace-alias
12794
 
12795
   Returns the NAMESPACE_DECL for the namespace.  */
12796
 
12797
static tree
12798
cp_parser_namespace_name (cp_parser* parser)
12799
{
12800
  tree identifier;
12801
  tree namespace_decl;
12802
 
12803
  cp_token *token = cp_lexer_peek_token (parser->lexer);
12804
 
12805
  /* Get the name of the namespace.  */
12806
  identifier = cp_parser_identifier (parser);
12807
  if (identifier == error_mark_node)
12808
    return error_mark_node;
12809
 
12810
  /* Look up the identifier in the currently active scope.  Look only
12811
     for namespaces, due to:
12812
 
12813
       [basic.lookup.udir]
12814
 
12815
       When looking up a namespace-name in a using-directive or alias
12816
       definition, only namespace names are considered.
12817
 
12818
     And:
12819
 
12820
       [basic.lookup.qual]
12821
 
12822
       During the lookup of a name preceding the :: scope resolution
12823
       operator, object, function, and enumerator names are ignored.
12824
 
12825
     (Note that cp_parser_qualifying_entity only calls this
12826
     function if the token after the name is the scope resolution
12827
     operator.)  */
12828
  namespace_decl = cp_parser_lookup_name (parser, identifier,
12829
                                          none_type,
12830
                                          /*is_template=*/false,
12831
                                          /*is_namespace=*/true,
12832
                                          /*check_dependency=*/true,
12833
                                          /*ambiguous_decls=*/NULL,
12834
                                          token->location);
12835
  /* If it's not a namespace, issue an error.  */
12836
  if (namespace_decl == error_mark_node
12837
      || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12838
    {
12839
      if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12840
        error_at (token->location, "%qD is not a namespace-name", identifier);
12841
      cp_parser_error (parser, "expected namespace-name");
12842
      namespace_decl = error_mark_node;
12843
    }
12844
 
12845
  return namespace_decl;
12846
}
12847
 
12848
/* Parse a namespace-definition.
12849
 
12850
   namespace-definition:
12851
     named-namespace-definition
12852
     unnamed-namespace-definition
12853
 
12854
   named-namespace-definition:
12855
     original-namespace-definition
12856
     extension-namespace-definition
12857
 
12858
   original-namespace-definition:
12859
     namespace identifier { namespace-body }
12860
 
12861
   extension-namespace-definition:
12862
     namespace original-namespace-name { namespace-body }
12863
 
12864
   unnamed-namespace-definition:
12865
     namespace { namespace-body } */
12866
 
12867
static void
12868
cp_parser_namespace_definition (cp_parser* parser)
12869
{
12870
  tree identifier, attribs;
12871
  bool has_visibility;
12872
  bool is_inline;
12873
 
12874
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12875
    {
12876
      is_inline = true;
12877
      cp_lexer_consume_token (parser->lexer);
12878
    }
12879
  else
12880
    is_inline = false;
12881
 
12882
  /* Look for the `namespace' keyword.  */
12883
  cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12884
 
12885
  /* Get the name of the namespace.  We do not attempt to distinguish
12886
     between an original-namespace-definition and an
12887
     extension-namespace-definition at this point.  The semantic
12888
     analysis routines are responsible for that.  */
12889
  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12890
    identifier = cp_parser_identifier (parser);
12891
  else
12892
    identifier = NULL_TREE;
12893
 
12894
  /* Parse any specified attributes.  */
12895
  attribs = cp_parser_attributes_opt (parser);
12896
 
12897
  /* Look for the `{' to start the namespace.  */
12898
  cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12899
  /* Start the namespace.  */
12900
  push_namespace (identifier);
12901
 
12902
  /* "inline namespace" is equivalent to a stub namespace definition
12903
     followed by a strong using directive.  */
12904
  if (is_inline)
12905
    {
12906
      tree name_space = current_namespace;
12907
      /* Set up namespace association.  */
12908
      DECL_NAMESPACE_ASSOCIATIONS (name_space)
12909
        = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12910
                     DECL_NAMESPACE_ASSOCIATIONS (name_space));
12911
      /* Import the contents of the inline namespace.  */
12912
      pop_namespace ();
12913
      do_using_directive (name_space);
12914
      push_namespace (identifier);
12915
    }
12916
 
12917
  has_visibility = handle_namespace_attrs (current_namespace, attribs);
12918
 
12919
  /* Parse the body of the namespace.  */
12920
  cp_parser_namespace_body (parser);
12921
 
12922
#ifdef HANDLE_PRAGMA_VISIBILITY
12923
  if (has_visibility)
12924
    pop_visibility (1);
12925
#endif
12926
 
12927
  /* Finish the namespace.  */
12928
  pop_namespace ();
12929
  /* Look for the final `}'.  */
12930
  cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12931
}
12932
 
12933
/* Parse a namespace-body.
12934
 
12935
   namespace-body:
12936
     declaration-seq [opt]  */
12937
 
12938
static void
12939
cp_parser_namespace_body (cp_parser* parser)
12940
{
12941
  cp_parser_declaration_seq_opt (parser);
12942
}
12943
 
12944
/* Parse a namespace-alias-definition.
12945
 
12946
   namespace-alias-definition:
12947
     namespace identifier = qualified-namespace-specifier ;  */
12948
 
12949
static void
12950
cp_parser_namespace_alias_definition (cp_parser* parser)
12951
{
12952
  tree identifier;
12953
  tree namespace_specifier;
12954
 
12955
  cp_token *token = cp_lexer_peek_token (parser->lexer);
12956
 
12957
  /* Look for the `namespace' keyword.  */
12958
  cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12959
  /* Look for the identifier.  */
12960
  identifier = cp_parser_identifier (parser);
12961
  if (identifier == error_mark_node)
12962
    return;
12963
  /* Look for the `=' token.  */
12964
  if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12965
      && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12966
    {
12967
      error_at (token->location, "%<namespace%> definition is not allowed here");
12968
      /* Skip the definition.  */
12969
      cp_lexer_consume_token (parser->lexer);
12970
      if (cp_parser_skip_to_closing_brace (parser))
12971
        cp_lexer_consume_token (parser->lexer);
12972
      return;
12973
    }
12974
  cp_parser_require (parser, CPP_EQ, "%<=%>");
12975
  /* Look for the qualified-namespace-specifier.  */
12976
  namespace_specifier
12977
    = cp_parser_qualified_namespace_specifier (parser);
12978
  /* Look for the `;' token.  */
12979
  cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12980
 
12981
  /* Register the alias in the symbol table.  */
12982
  do_namespace_alias (identifier, namespace_specifier);
12983
}
12984
 
12985
/* Parse a qualified-namespace-specifier.
12986
 
12987
   qualified-namespace-specifier:
12988
     :: [opt] nested-name-specifier [opt] namespace-name
12989
 
12990
   Returns a NAMESPACE_DECL corresponding to the specified
12991
   namespace.  */
12992
 
12993
static tree
12994
cp_parser_qualified_namespace_specifier (cp_parser* parser)
12995
{
12996
  /* Look for the optional `::'.  */
12997
  cp_parser_global_scope_opt (parser,
12998
                              /*current_scope_valid_p=*/false);
12999
 
13000
  /* Look for the optional nested-name-specifier.  */
13001
  cp_parser_nested_name_specifier_opt (parser,
13002
                                       /*typename_keyword_p=*/false,
13003
                                       /*check_dependency_p=*/true,
13004
                                       /*type_p=*/false,
13005
                                       /*is_declaration=*/true);
13006
 
13007
  return cp_parser_namespace_name (parser);
13008
}
13009
 
13010
/* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13011
   access declaration.
13012
 
13013
   using-declaration:
13014
     using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13015
     using :: unqualified-id ;
13016
 
13017
   access-declaration:
13018
     qualified-id ;
13019
 
13020
   */
13021
 
13022
static bool
13023
cp_parser_using_declaration (cp_parser* parser,
13024
                             bool access_declaration_p)
13025
{
13026
  cp_token *token;
13027
  bool typename_p = false;
13028
  bool global_scope_p;
13029
  tree decl;
13030
  tree identifier;
13031
  tree qscope;
13032
 
13033
  if (access_declaration_p)
13034
    cp_parser_parse_tentatively (parser);
13035
  else
13036
    {
13037
      /* Look for the `using' keyword.  */
13038
      cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13039
 
13040
      /* Peek at the next token.  */
13041
      token = cp_lexer_peek_token (parser->lexer);
13042
      /* See if it's `typename'.  */
13043
      if (token->keyword == RID_TYPENAME)
13044
        {
13045
          /* Remember that we've seen it.  */
13046
          typename_p = true;
13047
          /* Consume the `typename' token.  */
13048
          cp_lexer_consume_token (parser->lexer);
13049
        }
13050
    }
13051
 
13052
  /* Look for the optional global scope qualification.  */
13053
  global_scope_p
13054
    = (cp_parser_global_scope_opt (parser,
13055
                                   /*current_scope_valid_p=*/false)
13056
       != NULL_TREE);
13057
 
13058
  /* If we saw `typename', or didn't see `::', then there must be a
13059
     nested-name-specifier present.  */
13060
  if (typename_p || !global_scope_p)
13061
    qscope = cp_parser_nested_name_specifier (parser, typename_p,
13062
                                              /*check_dependency_p=*/true,
13063
                                              /*type_p=*/false,
13064
                                              /*is_declaration=*/true);
13065
  /* Otherwise, we could be in either of the two productions.  In that
13066
     case, treat the nested-name-specifier as optional.  */
13067
  else
13068
    qscope = cp_parser_nested_name_specifier_opt (parser,
13069
                                                  /*typename_keyword_p=*/false,
13070
                                                  /*check_dependency_p=*/true,
13071
                                                  /*type_p=*/false,
13072
                                                  /*is_declaration=*/true);
13073
  if (!qscope)
13074
    qscope = global_namespace;
13075
 
13076
  if (access_declaration_p && cp_parser_error_occurred (parser))
13077
    /* Something has already gone wrong; there's no need to parse
13078
       further.  Since an error has occurred, the return value of
13079
       cp_parser_parse_definitely will be false, as required.  */
13080
    return cp_parser_parse_definitely (parser);
13081
 
13082
  token = cp_lexer_peek_token (parser->lexer);
13083
  /* Parse the unqualified-id.  */
13084
  identifier = cp_parser_unqualified_id (parser,
13085
                                         /*template_keyword_p=*/false,
13086
                                         /*check_dependency_p=*/true,
13087
                                         /*declarator_p=*/true,
13088
                                         /*optional_p=*/false);
13089
 
13090
  if (access_declaration_p)
13091
    {
13092
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13093
        cp_parser_simulate_error (parser);
13094
      if (!cp_parser_parse_definitely (parser))
13095
        return false;
13096
    }
13097
 
13098
  /* The function we call to handle a using-declaration is different
13099
     depending on what scope we are in.  */
13100
  if (qscope == error_mark_node || identifier == error_mark_node)
13101
    ;
13102
  else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13103
           && TREE_CODE (identifier) != BIT_NOT_EXPR)
13104
    /* [namespace.udecl]
13105
 
13106
       A using declaration shall not name a template-id.  */
13107
    error_at (token->location,
13108
              "a template-id may not appear in a using-declaration");
13109
  else
13110
    {
13111
      if (at_class_scope_p ())
13112
        {
13113
          /* Create the USING_DECL.  */
13114
          decl = do_class_using_decl (parser->scope, identifier);
13115
 
13116
          if (check_for_bare_parameter_packs (decl))
13117
            return false;
13118
          else
13119
            /* Add it to the list of members in this class.  */
13120
            finish_member_declaration (decl);
13121
        }
13122
      else
13123
        {
13124
          decl = cp_parser_lookup_name_simple (parser,
13125
                                               identifier,
13126
                                               token->location);
13127
          if (decl == error_mark_node)
13128
            cp_parser_name_lookup_error (parser, identifier,
13129
                                         decl, NULL,
13130
                                         token->location);
13131
          else if (check_for_bare_parameter_packs (decl))
13132
            return false;
13133
          else if (!at_namespace_scope_p ())
13134
            do_local_using_decl (decl, qscope, identifier);
13135
          else
13136
            do_toplevel_using_decl (decl, qscope, identifier);
13137
        }
13138
    }
13139
 
13140
  /* Look for the final `;'.  */
13141
  cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13142
 
13143
  return true;
13144
}
13145
 
13146
/* Parse a using-directive.
13147
 
13148
   using-directive:
13149
     using namespace :: [opt] nested-name-specifier [opt]
13150
       namespace-name ;  */
13151
 
13152
static void
13153
cp_parser_using_directive (cp_parser* parser)
13154
{
13155
  tree namespace_decl;
13156
  tree attribs;
13157
 
13158
  /* Look for the `using' keyword.  */
13159
  cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13160
  /* And the `namespace' keyword.  */
13161
  cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13162
  /* Look for the optional `::' operator.  */
13163
  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13164
  /* And the optional nested-name-specifier.  */
13165
  cp_parser_nested_name_specifier_opt (parser,
13166
                                       /*typename_keyword_p=*/false,
13167
                                       /*check_dependency_p=*/true,
13168
                                       /*type_p=*/false,
13169
                                       /*is_declaration=*/true);
13170
  /* Get the namespace being used.  */
13171
  namespace_decl = cp_parser_namespace_name (parser);
13172
  /* And any specified attributes.  */
13173
  attribs = cp_parser_attributes_opt (parser);
13174
  /* Update the symbol table.  */
13175
  parse_using_directive (namespace_decl, attribs);
13176
  /* Look for the final `;'.  */
13177
  cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13178
}
13179
 
13180
/* Parse an asm-definition.
13181
 
13182
   asm-definition:
13183
     asm ( string-literal ) ;
13184
 
13185
   GNU Extension:
13186
 
13187
   asm-definition:
13188
     asm volatile [opt] ( string-literal ) ;
13189
     asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13190
     asm volatile [opt] ( string-literal : asm-operand-list [opt]
13191
                          : asm-operand-list [opt] ) ;
13192
     asm volatile [opt] ( string-literal : asm-operand-list [opt]
13193
                          : asm-operand-list [opt]
13194
                          : asm-clobber-list [opt] ) ;
13195
     asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13196
                               : asm-clobber-list [opt]
13197
                               : asm-goto-list ) ;  */
13198
 
13199
static void
13200
cp_parser_asm_definition (cp_parser* parser)
13201
{
13202
  tree string;
13203
  tree outputs = NULL_TREE;
13204
  tree inputs = NULL_TREE;
13205
  tree clobbers = NULL_TREE;
13206
  tree labels = NULL_TREE;
13207
  tree asm_stmt;
13208
  bool volatile_p = false;
13209
  bool extended_p = false;
13210
  bool invalid_inputs_p = false;
13211
  bool invalid_outputs_p = false;
13212
  bool goto_p = false;
13213
  const char *missing = NULL;
13214
 
13215
  /* Look for the `asm' keyword.  */
13216
  cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13217
  /* See if the next token is `volatile'.  */
13218
  if (cp_parser_allow_gnu_extensions_p (parser)
13219
      && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13220
    {
13221
      /* Remember that we saw the `volatile' keyword.  */
13222
      volatile_p = true;
13223
      /* Consume the token.  */
13224
      cp_lexer_consume_token (parser->lexer);
13225
    }
13226
  if (cp_parser_allow_gnu_extensions_p (parser)
13227
      && parser->in_function_body
13228
      && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13229
    {
13230
      /* Remember that we saw the `goto' keyword.  */
13231
      goto_p = true;
13232
      /* Consume the token.  */
13233
      cp_lexer_consume_token (parser->lexer);
13234
    }
13235
  /* Look for the opening `('.  */
13236
  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13237
    return;
13238
  /* Look for the string.  */
13239
  string = cp_parser_string_literal (parser, false, false);
13240
  if (string == error_mark_node)
13241
    {
13242
      cp_parser_skip_to_closing_parenthesis (parser, true, false,
13243
                                             /*consume_paren=*/true);
13244
      return;
13245
    }
13246
 
13247
  /* If we're allowing GNU extensions, check for the extended assembly
13248
     syntax.  Unfortunately, the `:' tokens need not be separated by
13249
     a space in C, and so, for compatibility, we tolerate that here
13250
     too.  Doing that means that we have to treat the `::' operator as
13251
     two `:' tokens.  */
13252
  if (cp_parser_allow_gnu_extensions_p (parser)
13253
      && parser->in_function_body
13254
      && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13255
          || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13256
    {
13257
      bool inputs_p = false;
13258
      bool clobbers_p = false;
13259
      bool labels_p = false;
13260
 
13261
      /* The extended syntax was used.  */
13262
      extended_p = true;
13263
 
13264
      /* Look for outputs.  */
13265
      if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13266
        {
13267
          /* Consume the `:'.  */
13268
          cp_lexer_consume_token (parser->lexer);
13269
          /* Parse the output-operands.  */
13270
          if (cp_lexer_next_token_is_not (parser->lexer,
13271
                                          CPP_COLON)
13272
              && cp_lexer_next_token_is_not (parser->lexer,
13273
                                             CPP_SCOPE)
13274
              && cp_lexer_next_token_is_not (parser->lexer,
13275
                                             CPP_CLOSE_PAREN)
13276
              && !goto_p)
13277
            outputs = cp_parser_asm_operand_list (parser);
13278
 
13279
            if (outputs == error_mark_node)
13280
              invalid_outputs_p = true;
13281
        }
13282
      /* If the next token is `::', there are no outputs, and the
13283
         next token is the beginning of the inputs.  */
13284
      else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13285
        /* The inputs are coming next.  */
13286
        inputs_p = true;
13287
 
13288
      /* Look for inputs.  */
13289
      if (inputs_p
13290
          || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13291
        {
13292
          /* Consume the `:' or `::'.  */
13293
          cp_lexer_consume_token (parser->lexer);
13294
          /* Parse the output-operands.  */
13295
          if (cp_lexer_next_token_is_not (parser->lexer,
13296
                                          CPP_COLON)
13297
              && cp_lexer_next_token_is_not (parser->lexer,
13298
                                             CPP_SCOPE)
13299
              && cp_lexer_next_token_is_not (parser->lexer,
13300
                                             CPP_CLOSE_PAREN))
13301
            inputs = cp_parser_asm_operand_list (parser);
13302
 
13303
            if (inputs == error_mark_node)
13304
              invalid_inputs_p = true;
13305
        }
13306
      else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13307
        /* The clobbers are coming next.  */
13308
        clobbers_p = true;
13309
 
13310
      /* Look for clobbers.  */
13311
      if (clobbers_p
13312
          || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13313
        {
13314
          clobbers_p = true;
13315
          /* Consume the `:' or `::'.  */
13316
          cp_lexer_consume_token (parser->lexer);
13317
          /* Parse the clobbers.  */
13318
          if (cp_lexer_next_token_is_not (parser->lexer,
13319
                                          CPP_COLON)
13320
              && cp_lexer_next_token_is_not (parser->lexer,
13321
                                             CPP_CLOSE_PAREN))
13322
            clobbers = cp_parser_asm_clobber_list (parser);
13323
        }
13324
      else if (goto_p
13325
               && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13326
        /* The labels are coming next.  */
13327
        labels_p = true;
13328
 
13329
      /* Look for labels.  */
13330
      if (labels_p
13331
          || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13332
        {
13333
          labels_p = true;
13334
          /* Consume the `:' or `::'.  */
13335
          cp_lexer_consume_token (parser->lexer);
13336
          /* Parse the labels.  */
13337
          labels = cp_parser_asm_label_list (parser);
13338
        }
13339
 
13340
      if (goto_p && !labels_p)
13341
        missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13342
    }
13343
  else if (goto_p)
13344
    missing = "%<:%> or %<::%>";
13345
 
13346
  /* Look for the closing `)'.  */
13347
  if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13348
                          missing ? missing : "%<)%>"))
13349
    cp_parser_skip_to_closing_parenthesis (parser, true, false,
13350
                                           /*consume_paren=*/true);
13351
  cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13352
 
13353
  if (!invalid_inputs_p && !invalid_outputs_p)
13354
    {
13355
      /* Create the ASM_EXPR.  */
13356
      if (parser->in_function_body)
13357
        {
13358
          asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13359
                                      inputs, clobbers, labels);
13360
          /* If the extended syntax was not used, mark the ASM_EXPR.  */
13361
          if (!extended_p)
13362
            {
13363
              tree temp = asm_stmt;
13364
              if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13365
                temp = TREE_OPERAND (temp, 0);
13366
 
13367
              ASM_INPUT_P (temp) = 1;
13368
            }
13369
        }
13370
      else
13371
        cgraph_add_asm_node (string);
13372
    }
13373
}
13374
 
13375
/* Declarators [gram.dcl.decl] */
13376
 
13377
/* Parse an init-declarator.
13378
 
13379
   init-declarator:
13380
     declarator initializer [opt]
13381
 
13382
   GNU Extension:
13383
 
13384
   init-declarator:
13385
     declarator asm-specification [opt] attributes [opt] initializer [opt]
13386
 
13387
   function-definition:
13388
     decl-specifier-seq [opt] declarator ctor-initializer [opt]
13389
       function-body
13390
     decl-specifier-seq [opt] declarator function-try-block
13391
 
13392
   GNU Extension:
13393
 
13394
   function-definition:
13395
     __extension__ function-definition
13396
 
13397
   The DECL_SPECIFIERS apply to this declarator.  Returns a
13398
   representation of the entity declared.  If MEMBER_P is TRUE, then
13399
   this declarator appears in a class scope.  The new DECL created by
13400
   this declarator is returned.
13401
 
13402
   The CHECKS are access checks that should be performed once we know
13403
   what entity is being declared (and, therefore, what classes have
13404
   befriended it).
13405
 
13406
   If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13407
   for a function-definition here as well.  If the declarator is a
13408
   declarator for a function-definition, *FUNCTION_DEFINITION_P will
13409
   be TRUE upon return.  By that point, the function-definition will
13410
   have been completely parsed.
13411
 
13412
   FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13413
   is FALSE.  */
13414
 
13415
static tree
13416
cp_parser_init_declarator (cp_parser* parser,
13417
                           cp_decl_specifier_seq *decl_specifiers,
13418
                           VEC (deferred_access_check,gc)* checks,
13419
                           bool function_definition_allowed_p,
13420
                           bool member_p,
13421
                           int declares_class_or_enum,
13422
                           bool* function_definition_p)
13423
{
13424
  cp_token *token = NULL, *asm_spec_start_token = NULL,
13425
           *attributes_start_token = NULL;
13426
  cp_declarator *declarator;
13427
  tree prefix_attributes;
13428
  tree attributes;
13429
  tree asm_specification;
13430
  tree initializer;
13431
  tree decl = NULL_TREE;
13432
  tree scope;
13433
  int is_initialized;
13434
  /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
13435
     initialized with "= ..", CPP_OPEN_PAREN if initialized with
13436
     "(...)".  */
13437
  enum cpp_ttype initialization_kind;
13438
  bool is_direct_init = false;
13439
  bool is_non_constant_init;
13440
  int ctor_dtor_or_conv_p;
13441
  bool friend_p;
13442
  tree pushed_scope = NULL;
13443
 
13444
  /* Gather the attributes that were provided with the
13445
     decl-specifiers.  */
13446
  prefix_attributes = decl_specifiers->attributes;
13447
 
13448
  /* Assume that this is not the declarator for a function
13449
     definition.  */
13450
  if (function_definition_p)
13451
    *function_definition_p = false;
13452
 
13453
  /* Defer access checks while parsing the declarator; we cannot know
13454
     what names are accessible until we know what is being
13455
     declared.  */
13456
  resume_deferring_access_checks ();
13457
 
13458
  /* Parse the declarator.  */
13459
  token = cp_lexer_peek_token (parser->lexer);
13460
  declarator
13461
    = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13462
                            &ctor_dtor_or_conv_p,
13463
                            /*parenthesized_p=*/NULL,
13464
                            /*member_p=*/false);
13465
  /* Gather up the deferred checks.  */
13466
  stop_deferring_access_checks ();
13467
 
13468
  /* If the DECLARATOR was erroneous, there's no need to go
13469
     further.  */
13470
  if (declarator == cp_error_declarator)
13471
    return error_mark_node;
13472
 
13473
  /* Check that the number of template-parameter-lists is OK.  */
13474
  if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13475
                                                       token->location))
13476
    return error_mark_node;
13477
 
13478
  if (declares_class_or_enum & 2)
13479
    cp_parser_check_for_definition_in_return_type (declarator,
13480
                                                   decl_specifiers->type,
13481
                                                   decl_specifiers->type_location);
13482
 
13483
  /* Figure out what scope the entity declared by the DECLARATOR is
13484
     located in.  `grokdeclarator' sometimes changes the scope, so
13485
     we compute it now.  */
13486
  scope = get_scope_of_declarator (declarator);
13487
 
13488
  /* Perform any lookups in the declared type which were thought to be
13489
     dependent, but are not in the scope of the declarator.  */
13490
  decl_specifiers->type
13491
    = maybe_update_decl_type (decl_specifiers->type, scope);
13492
 
13493
  /* If we're allowing GNU extensions, look for an asm-specification
13494
     and attributes.  */
13495
  if (cp_parser_allow_gnu_extensions_p (parser))
13496
    {
13497
      /* Look for an asm-specification.  */
13498
      asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13499
      asm_specification = cp_parser_asm_specification_opt (parser);
13500
      /* And attributes.  */
13501
      attributes_start_token = cp_lexer_peek_token (parser->lexer);
13502
      attributes = cp_parser_attributes_opt (parser);
13503
    }
13504
  else
13505
    {
13506
      asm_specification = NULL_TREE;
13507
      attributes = NULL_TREE;
13508
    }
13509
 
13510
  /* Peek at the next token.  */
13511
  token = cp_lexer_peek_token (parser->lexer);
13512
  /* Check to see if the token indicates the start of a
13513
     function-definition.  */
13514
  if (function_declarator_p (declarator)
13515
      && cp_parser_token_starts_function_definition_p (token))
13516
    {
13517
      if (!function_definition_allowed_p)
13518
        {
13519
          /* If a function-definition should not appear here, issue an
13520
             error message.  */
13521
          cp_parser_error (parser,
13522
                           "a function-definition is not allowed here");
13523
          return error_mark_node;
13524
        }
13525
      else
13526
        {
13527
          location_t func_brace_location
13528
            = cp_lexer_peek_token (parser->lexer)->location;
13529
 
13530
          /* Neither attributes nor an asm-specification are allowed
13531
             on a function-definition.  */
13532
          if (asm_specification)
13533
            error_at (asm_spec_start_token->location,
13534
                      "an asm-specification is not allowed "
13535
                      "on a function-definition");
13536
          if (attributes)
13537
            error_at (attributes_start_token->location,
13538
                      "attributes are not allowed on a function-definition");
13539
          /* This is a function-definition.  */
13540
          *function_definition_p = true;
13541
 
13542
          /* Parse the function definition.  */
13543
          if (member_p)
13544
            decl = cp_parser_save_member_function_body (parser,
13545
                                                        decl_specifiers,
13546
                                                        declarator,
13547
                                                        prefix_attributes);
13548
          else
13549
            decl
13550
              = (cp_parser_function_definition_from_specifiers_and_declarator
13551
                 (parser, decl_specifiers, prefix_attributes, declarator));
13552
 
13553
          if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13554
            {
13555
              /* This is where the prologue starts...  */
13556
              DECL_STRUCT_FUNCTION (decl)->function_start_locus
13557
                = func_brace_location;
13558
            }
13559
 
13560
          return decl;
13561
        }
13562
    }
13563
 
13564
  /* [dcl.dcl]
13565
 
13566
     Only in function declarations for constructors, destructors, and
13567
     type conversions can the decl-specifier-seq be omitted.
13568
 
13569
     We explicitly postpone this check past the point where we handle
13570
     function-definitions because we tolerate function-definitions
13571
     that are missing their return types in some modes.  */
13572
  if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13573
    {
13574
      cp_parser_error (parser,
13575
                       "expected constructor, destructor, or type conversion");
13576
      return error_mark_node;
13577
    }
13578
 
13579
  /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
13580
  if (token->type == CPP_EQ
13581
      || token->type == CPP_OPEN_PAREN
13582
      || token->type == CPP_OPEN_BRACE)
13583
    {
13584
      is_initialized = SD_INITIALIZED;
13585
      initialization_kind = token->type;
13586
 
13587
      if (token->type == CPP_EQ
13588
          && function_declarator_p (declarator))
13589
        {
13590
          cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13591
          if (t2->keyword == RID_DEFAULT)
13592
            is_initialized = SD_DEFAULTED;
13593
          else if (t2->keyword == RID_DELETE)
13594
            is_initialized = SD_DELETED;
13595
        }
13596
    }
13597
  else
13598
    {
13599
      /* If the init-declarator isn't initialized and isn't followed by a
13600
         `,' or `;', it's not a valid init-declarator.  */
13601
      if (token->type != CPP_COMMA
13602
          && token->type != CPP_SEMICOLON)
13603
        {
13604
          cp_parser_error (parser, "expected initializer");
13605
          return error_mark_node;
13606
        }
13607
      is_initialized = SD_UNINITIALIZED;
13608
      initialization_kind = CPP_EOF;
13609
    }
13610
 
13611
  /* Because start_decl has side-effects, we should only call it if we
13612
     know we're going ahead.  By this point, we know that we cannot
13613
     possibly be looking at any other construct.  */
13614
  cp_parser_commit_to_tentative_parse (parser);
13615
 
13616
  /* If the decl specifiers were bad, issue an error now that we're
13617
     sure this was intended to be a declarator.  Then continue
13618
     declaring the variable(s), as int, to try to cut down on further
13619
     errors.  */
13620
  if (decl_specifiers->any_specifiers_p
13621
      && decl_specifiers->type == error_mark_node)
13622
    {
13623
      cp_parser_error (parser, "invalid type in declaration");
13624
      decl_specifiers->type = integer_type_node;
13625
    }
13626
 
13627
  /* Check to see whether or not this declaration is a friend.  */
13628
  friend_p = cp_parser_friend_p (decl_specifiers);
13629
 
13630
  /* Enter the newly declared entry in the symbol table.  If we're
13631
     processing a declaration in a class-specifier, we wait until
13632
     after processing the initializer.  */
13633
  if (!member_p)
13634
    {
13635
      if (parser->in_unbraced_linkage_specification_p)
13636
        decl_specifiers->storage_class = sc_extern;
13637
      decl = start_decl (declarator, decl_specifiers,
13638
                         is_initialized, attributes, prefix_attributes,
13639
                         &pushed_scope);
13640
    }
13641
  else if (scope)
13642
    /* Enter the SCOPE.  That way unqualified names appearing in the
13643
       initializer will be looked up in SCOPE.  */
13644
    pushed_scope = push_scope (scope);
13645
 
13646
  /* Perform deferred access control checks, now that we know in which
13647
     SCOPE the declared entity resides.  */
13648
  if (!member_p && decl)
13649
    {
13650
      tree saved_current_function_decl = NULL_TREE;
13651
 
13652
      /* If the entity being declared is a function, pretend that we
13653
         are in its scope.  If it is a `friend', it may have access to
13654
         things that would not otherwise be accessible.  */
13655
      if (TREE_CODE (decl) == FUNCTION_DECL)
13656
        {
13657
          saved_current_function_decl = current_function_decl;
13658
          current_function_decl = decl;
13659
        }
13660
 
13661
      /* Perform access checks for template parameters.  */
13662
      cp_parser_perform_template_parameter_access_checks (checks);
13663
 
13664
      /* Perform the access control checks for the declarator and the
13665
         decl-specifiers.  */
13666
      perform_deferred_access_checks ();
13667
 
13668
      /* Restore the saved value.  */
13669
      if (TREE_CODE (decl) == FUNCTION_DECL)
13670
        current_function_decl = saved_current_function_decl;
13671
    }
13672
 
13673
  /* Parse the initializer.  */
13674
  initializer = NULL_TREE;
13675
  is_direct_init = false;
13676
  is_non_constant_init = true;
13677
  if (is_initialized)
13678
    {
13679
      if (function_declarator_p (declarator))
13680
        {
13681
          cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13682
           if (initialization_kind == CPP_EQ)
13683
             initializer = cp_parser_pure_specifier (parser);
13684
           else
13685
             {
13686
               /* If the declaration was erroneous, we don't really
13687
                  know what the user intended, so just silently
13688
                  consume the initializer.  */
13689
               if (decl != error_mark_node)
13690
                 error_at (initializer_start_token->location,
13691
                           "initializer provided for function");
13692
               cp_parser_skip_to_closing_parenthesis (parser,
13693
                                                      /*recovering=*/true,
13694
                                                      /*or_comma=*/false,
13695
                                                      /*consume_paren=*/true);
13696
             }
13697
        }
13698
      else
13699
        {
13700
          /* We want to record the extra mangling scope for in-class
13701
             initializers of class members and initializers of static data
13702
             member templates.  The former is a C++0x feature which isn't
13703
             implemented yet, and I expect it will involve deferring
13704
             parsing of the initializer until end of class as with default
13705
             arguments.  So right here we only handle the latter.  */
13706
          if (!member_p && processing_template_decl)
13707
            start_lambda_scope (decl);
13708
          initializer = cp_parser_initializer (parser,
13709
                                               &is_direct_init,
13710
                                               &is_non_constant_init);
13711
          if (!member_p && processing_template_decl)
13712
            finish_lambda_scope ();
13713
        }
13714
    }
13715
 
13716
  /* The old parser allows attributes to appear after a parenthesized
13717
     initializer.  Mark Mitchell proposed removing this functionality
13718
     on the GCC mailing lists on 2002-08-13.  This parser accepts the
13719
     attributes -- but ignores them.  */
13720
  if (cp_parser_allow_gnu_extensions_p (parser)
13721
      && initialization_kind == CPP_OPEN_PAREN)
13722
    if (cp_parser_attributes_opt (parser))
13723
      warning (OPT_Wattributes,
13724
               "attributes after parenthesized initializer ignored");
13725
 
13726
  /* For an in-class declaration, use `grokfield' to create the
13727
     declaration.  */
13728
  if (member_p)
13729
    {
13730
      if (pushed_scope)
13731
        {
13732
          pop_scope (pushed_scope);
13733
          pushed_scope = false;
13734
        }
13735
      decl = grokfield (declarator, decl_specifiers,
13736
                        initializer, !is_non_constant_init,
13737
                        /*asmspec=*/NULL_TREE,
13738
                        prefix_attributes);
13739
      if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13740
        cp_parser_save_default_args (parser, decl);
13741
    }
13742
 
13743
  /* Finish processing the declaration.  But, skip friend
13744
     declarations.  */
13745
  if (!friend_p && decl && decl != error_mark_node)
13746
    {
13747
      cp_finish_decl (decl,
13748
                      initializer, !is_non_constant_init,
13749
                      asm_specification,
13750
                      /* If the initializer is in parentheses, then this is
13751
                         a direct-initialization, which means that an
13752
                         `explicit' constructor is OK.  Otherwise, an
13753
                         `explicit' constructor cannot be used.  */
13754
                      ((is_direct_init || !is_initialized)
13755
                       ? 0 : LOOKUP_ONLYCONVERTING));
13756
    }
13757
  else if ((cxx_dialect != cxx98) && friend_p
13758
           && decl && TREE_CODE (decl) == FUNCTION_DECL)
13759
    /* Core issue #226 (C++0x only): A default template-argument
13760
       shall not be specified in a friend class template
13761
       declaration. */
13762
    check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
13763
                             /*is_partial=*/0, /*is_friend_decl=*/1);
13764
 
13765
  if (!friend_p && pushed_scope)
13766
    pop_scope (pushed_scope);
13767
 
13768
  return decl;
13769
}
13770
 
13771
/* Parse a declarator.
13772
 
13773
   declarator:
13774
     direct-declarator
13775
     ptr-operator declarator
13776
 
13777
   abstract-declarator:
13778
     ptr-operator abstract-declarator [opt]
13779
     direct-abstract-declarator
13780
 
13781
   GNU Extensions:
13782
 
13783
   declarator:
13784
     attributes [opt] direct-declarator
13785
     attributes [opt] ptr-operator declarator
13786
 
13787
   abstract-declarator:
13788
     attributes [opt] ptr-operator abstract-declarator [opt]
13789
     attributes [opt] direct-abstract-declarator
13790
 
13791
   If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13792
   detect constructor, destructor or conversion operators. It is set
13793
   to -1 if the declarator is a name, and +1 if it is a
13794
   function. Otherwise it is set to zero. Usually you just want to
13795
   test for >0, but internally the negative value is used.
13796
 
13797
   (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13798
   a decl-specifier-seq unless it declares a constructor, destructor,
13799
   or conversion.  It might seem that we could check this condition in
13800
   semantic analysis, rather than parsing, but that makes it difficult
13801
   to handle something like `f()'.  We want to notice that there are
13802
   no decl-specifiers, and therefore realize that this is an
13803
   expression, not a declaration.)
13804
 
13805
   If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13806
   the declarator is a direct-declarator of the form "(...)".
13807
 
13808
   MEMBER_P is true iff this declarator is a member-declarator.  */
13809
 
13810
static cp_declarator *
13811
cp_parser_declarator (cp_parser* parser,
13812
                      cp_parser_declarator_kind dcl_kind,
13813
                      int* ctor_dtor_or_conv_p,
13814
                      bool* parenthesized_p,
13815
                      bool member_p)
13816
{
13817
  cp_declarator *declarator;
13818
  enum tree_code code;
13819
  cp_cv_quals cv_quals;
13820
  tree class_type;
13821
  tree attributes = NULL_TREE;
13822
 
13823
  /* Assume this is not a constructor, destructor, or type-conversion
13824
     operator.  */
13825
  if (ctor_dtor_or_conv_p)
13826
    *ctor_dtor_or_conv_p = 0;
13827
 
13828
  if (cp_parser_allow_gnu_extensions_p (parser))
13829
    attributes = cp_parser_attributes_opt (parser);
13830
 
13831
  /* Check for the ptr-operator production.  */
13832
  cp_parser_parse_tentatively (parser);
13833
  /* Parse the ptr-operator.  */
13834
  code = cp_parser_ptr_operator (parser,
13835
                                 &class_type,
13836
                                 &cv_quals);
13837
  /* If that worked, then we have a ptr-operator.  */
13838
  if (cp_parser_parse_definitely (parser))
13839
    {
13840
      /* If a ptr-operator was found, then this declarator was not
13841
         parenthesized.  */
13842
      if (parenthesized_p)
13843
        *parenthesized_p = true;
13844
      /* The dependent declarator is optional if we are parsing an
13845
         abstract-declarator.  */
13846
      if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13847
        cp_parser_parse_tentatively (parser);
13848
 
13849
      /* Parse the dependent declarator.  */
13850
      declarator = cp_parser_declarator (parser, dcl_kind,
13851
                                         /*ctor_dtor_or_conv_p=*/NULL,
13852
                                         /*parenthesized_p=*/NULL,
13853
                                         /*member_p=*/false);
13854
 
13855
      /* If we are parsing an abstract-declarator, we must handle the
13856
         case where the dependent declarator is absent.  */
13857
      if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13858
          && !cp_parser_parse_definitely (parser))
13859
        declarator = NULL;
13860
 
13861
      declarator = cp_parser_make_indirect_declarator
13862
        (code, class_type, cv_quals, declarator);
13863
    }
13864
  /* Everything else is a direct-declarator.  */
13865
  else
13866
    {
13867
      if (parenthesized_p)
13868
        *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13869
                                                   CPP_OPEN_PAREN);
13870
      declarator = cp_parser_direct_declarator (parser, dcl_kind,
13871
                                                ctor_dtor_or_conv_p,
13872
                                                member_p);
13873
    }
13874
 
13875
  if (attributes && declarator && declarator != cp_error_declarator)
13876
    declarator->attributes = attributes;
13877
 
13878
  return declarator;
13879
}
13880
 
13881
/* Parse a direct-declarator or direct-abstract-declarator.
13882
 
13883
   direct-declarator:
13884
     declarator-id
13885
     direct-declarator ( parameter-declaration-clause )
13886
       cv-qualifier-seq [opt]
13887
       exception-specification [opt]
13888
     direct-declarator [ constant-expression [opt] ]
13889
     ( declarator )
13890
 
13891
   direct-abstract-declarator:
13892
     direct-abstract-declarator [opt]
13893
       ( parameter-declaration-clause )
13894
       cv-qualifier-seq [opt]
13895
       exception-specification [opt]
13896
     direct-abstract-declarator [opt] [ constant-expression [opt] ]
13897
     ( abstract-declarator )
13898
 
13899
   Returns a representation of the declarator.  DCL_KIND is
13900
   CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13901
   direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13902
   we are parsing a direct-declarator.  It is
13903
   CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13904
   of ambiguity we prefer an abstract declarator, as per
13905
   [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13906
   cp_parser_declarator.  */
13907
 
13908
static cp_declarator *
13909
cp_parser_direct_declarator (cp_parser* parser,
13910
                             cp_parser_declarator_kind dcl_kind,
13911
                             int* ctor_dtor_or_conv_p,
13912
                             bool member_p)
13913
{
13914
  cp_token *token;
13915
  cp_declarator *declarator = NULL;
13916
  tree scope = NULL_TREE;
13917
  bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13918
  bool saved_in_declarator_p = parser->in_declarator_p;
13919
  bool first = true;
13920
  tree pushed_scope = NULL_TREE;
13921
 
13922
  while (true)
13923
    {
13924
      /* Peek at the next token.  */
13925
      token = cp_lexer_peek_token (parser->lexer);
13926
      if (token->type == CPP_OPEN_PAREN)
13927
        {
13928
          /* This is either a parameter-declaration-clause, or a
13929
             parenthesized declarator. When we know we are parsing a
13930
             named declarator, it must be a parenthesized declarator
13931
             if FIRST is true. For instance, `(int)' is a
13932
             parameter-declaration-clause, with an omitted
13933
             direct-abstract-declarator. But `((*))', is a
13934
             parenthesized abstract declarator. Finally, when T is a
13935
             template parameter `(T)' is a
13936
             parameter-declaration-clause, and not a parenthesized
13937
             named declarator.
13938
 
13939
             We first try and parse a parameter-declaration-clause,
13940
             and then try a nested declarator (if FIRST is true).
13941
 
13942
             It is not an error for it not to be a
13943
             parameter-declaration-clause, even when FIRST is
13944
             false. Consider,
13945
 
13946
               int i (int);
13947
               int i (3);
13948
 
13949
             The first is the declaration of a function while the
13950
             second is the definition of a variable, including its
13951
             initializer.
13952
 
13953
             Having seen only the parenthesis, we cannot know which of
13954
             these two alternatives should be selected.  Even more
13955
             complex are examples like:
13956
 
13957
               int i (int (a));
13958
               int i (int (3));
13959
 
13960
             The former is a function-declaration; the latter is a
13961
             variable initialization.
13962
 
13963
             Thus again, we try a parameter-declaration-clause, and if
13964
             that fails, we back out and return.  */
13965
 
13966
          if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13967
            {
13968
              tree params;
13969
              unsigned saved_num_template_parameter_lists;
13970
              bool is_declarator = false;
13971
              tree t;
13972
 
13973
              /* In a member-declarator, the only valid interpretation
13974
                 of a parenthesis is the start of a
13975
                 parameter-declaration-clause.  (It is invalid to
13976
                 initialize a static data member with a parenthesized
13977
                 initializer; only the "=" form of initialization is
13978
                 permitted.)  */
13979
              if (!member_p)
13980
                cp_parser_parse_tentatively (parser);
13981
 
13982
              /* Consume the `('.  */
13983
              cp_lexer_consume_token (parser->lexer);
13984
              if (first)
13985
                {
13986
                  /* If this is going to be an abstract declarator, we're
13987
                     in a declarator and we can't have default args.  */
13988
                  parser->default_arg_ok_p = false;
13989
                  parser->in_declarator_p = true;
13990
                }
13991
 
13992
              /* Inside the function parameter list, surrounding
13993
                 template-parameter-lists do not apply.  */
13994
              saved_num_template_parameter_lists
13995
                = parser->num_template_parameter_lists;
13996
              parser->num_template_parameter_lists = 0;
13997
 
13998
              begin_scope (sk_function_parms, NULL_TREE);
13999
 
14000
              /* Parse the parameter-declaration-clause.  */
14001
              params = cp_parser_parameter_declaration_clause (parser);
14002
 
14003
              parser->num_template_parameter_lists
14004
                = saved_num_template_parameter_lists;
14005
 
14006
              /* If all went well, parse the cv-qualifier-seq and the
14007
                 exception-specification.  */
14008
              if (member_p || cp_parser_parse_definitely (parser))
14009
                {
14010
                  cp_cv_quals cv_quals;
14011
                  tree exception_specification;
14012
                  tree late_return;
14013
 
14014
                  is_declarator = true;
14015
 
14016
                  if (ctor_dtor_or_conv_p)
14017
                    *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14018
                  first = false;
14019
                  /* Consume the `)'.  */
14020
                  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
14021
 
14022
                  /* Parse the cv-qualifier-seq.  */
14023
                  cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14024
                  /* And the exception-specification.  */
14025
                  exception_specification
14026
                    = cp_parser_exception_specification_opt (parser);
14027
 
14028
                  late_return
14029
                    = cp_parser_late_return_type_opt (parser);
14030
 
14031
                  /* Create the function-declarator.  */
14032
                  declarator = make_call_declarator (declarator,
14033
                                                     params,
14034
                                                     cv_quals,
14035
                                                     exception_specification,
14036
                                                     late_return);
14037
                  /* Any subsequent parameter lists are to do with
14038
                     return type, so are not those of the declared
14039
                     function.  */
14040
                  parser->default_arg_ok_p = false;
14041
                }
14042
 
14043
              /* Remove the function parms from scope.  */
14044
              for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14045
                pop_binding (DECL_NAME (t), t);
14046
              leave_scope();
14047
 
14048
              if (is_declarator)
14049
                /* Repeat the main loop.  */
14050
                continue;
14051
            }
14052
 
14053
          /* If this is the first, we can try a parenthesized
14054
             declarator.  */
14055
          if (first)
14056
            {
14057
              bool saved_in_type_id_in_expr_p;
14058
 
14059
              parser->default_arg_ok_p = saved_default_arg_ok_p;
14060
              parser->in_declarator_p = saved_in_declarator_p;
14061
 
14062
              /* Consume the `('.  */
14063
              cp_lexer_consume_token (parser->lexer);
14064
              /* Parse the nested declarator.  */
14065
              saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14066
              parser->in_type_id_in_expr_p = true;
14067
              declarator
14068
                = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14069
                                        /*parenthesized_p=*/NULL,
14070
                                        member_p);
14071
              parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14072
              first = false;
14073
              /* Expect a `)'.  */
14074
              if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14075
                declarator = cp_error_declarator;
14076
              if (declarator == cp_error_declarator)
14077
                break;
14078
 
14079
              goto handle_declarator;
14080
            }
14081
          /* Otherwise, we must be done.  */
14082
          else
14083
            break;
14084
        }
14085
      else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14086
               && token->type == CPP_OPEN_SQUARE)
14087
        {
14088
          /* Parse an array-declarator.  */
14089
          tree bounds;
14090
 
14091
          if (ctor_dtor_or_conv_p)
14092
            *ctor_dtor_or_conv_p = 0;
14093
 
14094
          first = false;
14095
          parser->default_arg_ok_p = false;
14096
          parser->in_declarator_p = true;
14097
          /* Consume the `['.  */
14098
          cp_lexer_consume_token (parser->lexer);
14099
          /* Peek at the next token.  */
14100
          token = cp_lexer_peek_token (parser->lexer);
14101
          /* If the next token is `]', then there is no
14102
             constant-expression.  */
14103
          if (token->type != CPP_CLOSE_SQUARE)
14104
            {
14105
              bool non_constant_p;
14106
 
14107
              bounds
14108
                = cp_parser_constant_expression (parser,
14109
                                                 /*allow_non_constant=*/true,
14110
                                                 &non_constant_p);
14111
              if (!non_constant_p)
14112
                bounds = fold_non_dependent_expr (bounds);
14113
              /* Normally, the array bound must be an integral constant
14114
                 expression.  However, as an extension, we allow VLAs
14115
                 in function scopes as long as they aren't part of a
14116
                 parameter declaration.  */
14117
              else if (!parser->in_function_body
14118
                       || current_binding_level->kind == sk_function_parms)
14119
                {
14120
                  cp_parser_error (parser,
14121
                                   "array bound is not an integer constant");
14122
                  bounds = error_mark_node;
14123
                }
14124
              else if (processing_template_decl && !error_operand_p (bounds))
14125
                {
14126
                  /* Remember this wasn't a constant-expression.  */
14127
                  bounds = build_nop (TREE_TYPE (bounds), bounds);
14128
                  TREE_SIDE_EFFECTS (bounds) = 1;
14129
                }
14130
            }
14131
          else
14132
            bounds = NULL_TREE;
14133
          /* Look for the closing `]'.  */
14134
          if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14135
            {
14136
              declarator = cp_error_declarator;
14137
              break;
14138
            }
14139
 
14140
          declarator = make_array_declarator (declarator, bounds);
14141
        }
14142
      else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14143
        {
14144
          {
14145
            tree qualifying_scope;
14146
            tree unqualified_name;
14147
            special_function_kind sfk;
14148
            bool abstract_ok;
14149
            bool pack_expansion_p = false;
14150
            cp_token *declarator_id_start_token;
14151
 
14152
            /* Parse a declarator-id */
14153
            abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14154
            if (abstract_ok)
14155
              {
14156
                cp_parser_parse_tentatively (parser);
14157
 
14158
                /* If we see an ellipsis, we should be looking at a
14159
                   parameter pack. */
14160
                if (token->type == CPP_ELLIPSIS)
14161
                  {
14162
                    /* Consume the `...' */
14163
                    cp_lexer_consume_token (parser->lexer);
14164
 
14165
                    pack_expansion_p = true;
14166
                  }
14167
              }
14168
 
14169
            declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14170
            unqualified_name
14171
              = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14172
            qualifying_scope = parser->scope;
14173
            if (abstract_ok)
14174
              {
14175
                bool okay = false;
14176
 
14177
                if (!unqualified_name && pack_expansion_p)
14178
                  {
14179
                    /* Check whether an error occurred. */
14180
                    okay = !cp_parser_error_occurred (parser);
14181
 
14182
                    /* We already consumed the ellipsis to mark a
14183
                       parameter pack, but we have no way to report it,
14184
                       so abort the tentative parse. We will be exiting
14185
                       immediately anyway. */
14186
                    cp_parser_abort_tentative_parse (parser);
14187
                  }
14188
                else
14189
                  okay = cp_parser_parse_definitely (parser);
14190
 
14191
                if (!okay)
14192
                  unqualified_name = error_mark_node;
14193
                else if (unqualified_name
14194
                         && (qualifying_scope
14195
                             || (TREE_CODE (unqualified_name)
14196
                                 != IDENTIFIER_NODE)))
14197
                  {
14198
                    cp_parser_error (parser, "expected unqualified-id");
14199
                    unqualified_name = error_mark_node;
14200
                  }
14201
              }
14202
 
14203
            if (!unqualified_name)
14204
              return NULL;
14205
            if (unqualified_name == error_mark_node)
14206
              {
14207
                declarator = cp_error_declarator;
14208
                pack_expansion_p = false;
14209
                declarator->parameter_pack_p = false;
14210
                break;
14211
              }
14212
 
14213
            if (qualifying_scope && at_namespace_scope_p ()
14214
                && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14215
              {
14216
                /* In the declaration of a member of a template class
14217
                   outside of the class itself, the SCOPE will sometimes
14218
                   be a TYPENAME_TYPE.  For example, given:
14219
 
14220
                   template <typename T>
14221
                   int S<T>::R::i = 3;
14222
 
14223
                   the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
14224
                   this context, we must resolve S<T>::R to an ordinary
14225
                   type, rather than a typename type.
14226
 
14227
                   The reason we normally avoid resolving TYPENAME_TYPEs
14228
                   is that a specialization of `S' might render
14229
                   `S<T>::R' not a type.  However, if `S' is
14230
                   specialized, then this `i' will not be used, so there
14231
                   is no harm in resolving the types here.  */
14232
                tree type;
14233
 
14234
                /* Resolve the TYPENAME_TYPE.  */
14235
                type = resolve_typename_type (qualifying_scope,
14236
                                              /*only_current_p=*/false);
14237
                /* If that failed, the declarator is invalid.  */
14238
                if (TREE_CODE (type) == TYPENAME_TYPE)
14239
                  {
14240
                    if (typedef_variant_p (type))
14241
                      error_at (declarator_id_start_token->location,
14242
                                "cannot define member of dependent typedef "
14243
                                "%qT", type);
14244
                    else
14245
                      error_at (declarator_id_start_token->location,
14246
                                "%<%T::%E%> is not a type",
14247
                                TYPE_CONTEXT (qualifying_scope),
14248
                                TYPE_IDENTIFIER (qualifying_scope));
14249
                  }
14250
                qualifying_scope = type;
14251
              }
14252
 
14253
            sfk = sfk_none;
14254
 
14255
            if (unqualified_name)
14256
              {
14257
                tree class_type;
14258
 
14259
                if (qualifying_scope
14260
                    && CLASS_TYPE_P (qualifying_scope))
14261
                  class_type = qualifying_scope;
14262
                else
14263
                  class_type = current_class_type;
14264
 
14265
                if (TREE_CODE (unqualified_name) == TYPE_DECL)
14266
                  {
14267
                    tree name_type = TREE_TYPE (unqualified_name);
14268
                    if (class_type && same_type_p (name_type, class_type))
14269
                      {
14270
                        if (qualifying_scope
14271
                            && CLASSTYPE_USE_TEMPLATE (name_type))
14272
                          {
14273
                            error_at (declarator_id_start_token->location,
14274
                                      "invalid use of constructor as a template");
14275
                            inform (declarator_id_start_token->location,
14276
                                    "use %<%T::%D%> instead of %<%T::%D%> to "
14277
                                    "name the constructor in a qualified name",
14278
                                    class_type,
14279
                                    DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14280
                                    class_type, name_type);
14281
                            declarator = cp_error_declarator;
14282
                            break;
14283
                          }
14284
                        else
14285
                          unqualified_name = constructor_name (class_type);
14286
                      }
14287
                    else
14288
                      {
14289
                        /* We do not attempt to print the declarator
14290
                           here because we do not have enough
14291
                           information about its original syntactic
14292
                           form.  */
14293
                        cp_parser_error (parser, "invalid declarator");
14294
                        declarator = cp_error_declarator;
14295
                        break;
14296
                      }
14297
                  }
14298
 
14299
                if (class_type)
14300
                  {
14301
                    if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14302
                      sfk = sfk_destructor;
14303
                    else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14304
                      sfk = sfk_conversion;
14305
                    else if (/* There's no way to declare a constructor
14306
                                for an anonymous type, even if the type
14307
                                got a name for linkage purposes.  */
14308
                             !TYPE_WAS_ANONYMOUS (class_type)
14309
                             && constructor_name_p (unqualified_name,
14310
                                                    class_type))
14311
                      {
14312
                        unqualified_name = constructor_name (class_type);
14313
                        sfk = sfk_constructor;
14314
                      }
14315
                    else if (is_overloaded_fn (unqualified_name)
14316
                             && DECL_CONSTRUCTOR_P (get_first_fn
14317
                                                    (unqualified_name)))
14318
                      sfk = sfk_constructor;
14319
 
14320
                    if (ctor_dtor_or_conv_p && sfk != sfk_none)
14321
                      *ctor_dtor_or_conv_p = -1;
14322
                  }
14323
              }
14324
            declarator = make_id_declarator (qualifying_scope,
14325
                                             unqualified_name,
14326
                                             sfk);
14327
            declarator->id_loc = token->location;
14328
            declarator->parameter_pack_p = pack_expansion_p;
14329
 
14330
            if (pack_expansion_p)
14331
              maybe_warn_variadic_templates ();
14332
          }
14333
 
14334
        handle_declarator:;
14335
          scope = get_scope_of_declarator (declarator);
14336
          if (scope)
14337
            /* Any names that appear after the declarator-id for a
14338
               member are looked up in the containing scope.  */
14339
            pushed_scope = push_scope (scope);
14340
          parser->in_declarator_p = true;
14341
          if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14342
              || (declarator && declarator->kind == cdk_id))
14343
            /* Default args are only allowed on function
14344
               declarations.  */
14345
            parser->default_arg_ok_p = saved_default_arg_ok_p;
14346
          else
14347
            parser->default_arg_ok_p = false;
14348
 
14349
          first = false;
14350
        }
14351
      /* We're done.  */
14352
      else
14353
        break;
14354
    }
14355
 
14356
  /* For an abstract declarator, we might wind up with nothing at this
14357
     point.  That's an error; the declarator is not optional.  */
14358
  if (!declarator)
14359
    cp_parser_error (parser, "expected declarator");
14360
 
14361
  /* If we entered a scope, we must exit it now.  */
14362
  if (pushed_scope)
14363
    pop_scope (pushed_scope);
14364
 
14365
  parser->default_arg_ok_p = saved_default_arg_ok_p;
14366
  parser->in_declarator_p = saved_in_declarator_p;
14367
 
14368
  return declarator;
14369
}
14370
 
14371
/* Parse a ptr-operator.
14372
 
14373
   ptr-operator:
14374
     * cv-qualifier-seq [opt]
14375
     &
14376
     :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14377
 
14378
   GNU Extension:
14379
 
14380
   ptr-operator:
14381
     & cv-qualifier-seq [opt]
14382
 
14383
   Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14384
   Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14385
   an rvalue reference. In the case of a pointer-to-member, *TYPE is
14386
   filled in with the TYPE containing the member.  *CV_QUALS is
14387
   filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14388
   are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
14389
   Note that the tree codes returned by this function have nothing
14390
   to do with the types of trees that will be eventually be created
14391
   to represent the pointer or reference type being parsed. They are
14392
   just constants with suggestive names. */
14393
static enum tree_code
14394
cp_parser_ptr_operator (cp_parser* parser,
14395
                        tree* type,
14396
                        cp_cv_quals *cv_quals)
14397
{
14398
  enum tree_code code = ERROR_MARK;
14399
  cp_token *token;
14400
 
14401
  /* Assume that it's not a pointer-to-member.  */
14402
  *type = NULL_TREE;
14403
  /* And that there are no cv-qualifiers.  */
14404
  *cv_quals = TYPE_UNQUALIFIED;
14405
 
14406
  /* Peek at the next token.  */
14407
  token = cp_lexer_peek_token (parser->lexer);
14408
 
14409
  /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
14410
  if (token->type == CPP_MULT)
14411
    code = INDIRECT_REF;
14412
  else if (token->type == CPP_AND)
14413
    code = ADDR_EXPR;
14414
  else if ((cxx_dialect != cxx98) &&
14415
           token->type == CPP_AND_AND) /* C++0x only */
14416
    code = NON_LVALUE_EXPR;
14417
 
14418
  if (code != ERROR_MARK)
14419
    {
14420
      /* Consume the `*', `&' or `&&'.  */
14421
      cp_lexer_consume_token (parser->lexer);
14422
 
14423
      /* A `*' can be followed by a cv-qualifier-seq, and so can a
14424
         `&', if we are allowing GNU extensions.  (The only qualifier
14425
         that can legally appear after `&' is `restrict', but that is
14426
         enforced during semantic analysis.  */
14427
      if (code == INDIRECT_REF
14428
          || cp_parser_allow_gnu_extensions_p (parser))
14429
        *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14430
    }
14431
  else
14432
    {
14433
      /* Try the pointer-to-member case.  */
14434
      cp_parser_parse_tentatively (parser);
14435
      /* Look for the optional `::' operator.  */
14436
      cp_parser_global_scope_opt (parser,
14437
                                  /*current_scope_valid_p=*/false);
14438
      /* Look for the nested-name specifier.  */
14439
      token = cp_lexer_peek_token (parser->lexer);
14440
      cp_parser_nested_name_specifier (parser,
14441
                                       /*typename_keyword_p=*/false,
14442
                                       /*check_dependency_p=*/true,
14443
                                       /*type_p=*/false,
14444
                                       /*is_declaration=*/false);
14445
      /* If we found it, and the next token is a `*', then we are
14446
         indeed looking at a pointer-to-member operator.  */
14447
      if (!cp_parser_error_occurred (parser)
14448
          && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14449
        {
14450
          /* Indicate that the `*' operator was used.  */
14451
          code = INDIRECT_REF;
14452
 
14453
          if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14454
            error_at (token->location, "%qD is a namespace", parser->scope);
14455
          else
14456
            {
14457
              /* The type of which the member is a member is given by the
14458
                 current SCOPE.  */
14459
              *type = parser->scope;
14460
              /* The next name will not be qualified.  */
14461
              parser->scope = NULL_TREE;
14462
              parser->qualifying_scope = NULL_TREE;
14463
              parser->object_scope = NULL_TREE;
14464
              /* Look for the optional cv-qualifier-seq.  */
14465
              *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14466
            }
14467
        }
14468
      /* If that didn't work we don't have a ptr-operator.  */
14469
      if (!cp_parser_parse_definitely (parser))
14470
        cp_parser_error (parser, "expected ptr-operator");
14471
    }
14472
 
14473
  return code;
14474
}
14475
 
14476
/* Parse an (optional) cv-qualifier-seq.
14477
 
14478
   cv-qualifier-seq:
14479
     cv-qualifier cv-qualifier-seq [opt]
14480
 
14481
   cv-qualifier:
14482
     const
14483
     volatile
14484
 
14485
   GNU Extension:
14486
 
14487
   cv-qualifier:
14488
     __restrict__
14489
 
14490
   Returns a bitmask representing the cv-qualifiers.  */
14491
 
14492
static cp_cv_quals
14493
cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14494
{
14495
  cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14496
 
14497
  while (true)
14498
    {
14499
      cp_token *token;
14500
      cp_cv_quals cv_qualifier;
14501
 
14502
      /* Peek at the next token.  */
14503
      token = cp_lexer_peek_token (parser->lexer);
14504
      /* See if it's a cv-qualifier.  */
14505
      switch (token->keyword)
14506
        {
14507
        case RID_CONST:
14508
          cv_qualifier = TYPE_QUAL_CONST;
14509
          break;
14510
 
14511
        case RID_VOLATILE:
14512
          cv_qualifier = TYPE_QUAL_VOLATILE;
14513
          break;
14514
 
14515
        case RID_RESTRICT:
14516
          cv_qualifier = TYPE_QUAL_RESTRICT;
14517
          break;
14518
 
14519
        default:
14520
          cv_qualifier = TYPE_UNQUALIFIED;
14521
          break;
14522
        }
14523
 
14524
      if (!cv_qualifier)
14525
        break;
14526
 
14527
      if (cv_quals & cv_qualifier)
14528
        {
14529
          error_at (token->location, "duplicate cv-qualifier");
14530
          cp_lexer_purge_token (parser->lexer);
14531
        }
14532
      else
14533
        {
14534
          cp_lexer_consume_token (parser->lexer);
14535
          cv_quals |= cv_qualifier;
14536
        }
14537
    }
14538
 
14539
  return cv_quals;
14540
}
14541
 
14542
/* Parse a late-specified return type, if any.  This is not a separate
14543
   non-terminal, but part of a function declarator, which looks like
14544
 
14545
   -> trailing-type-specifier-seq abstract-declarator(opt)
14546
 
14547
   Returns the type indicated by the type-id.  */
14548
 
14549
static tree
14550
cp_parser_late_return_type_opt (cp_parser* parser)
14551
{
14552
  cp_token *token;
14553
 
14554
  /* Peek at the next token.  */
14555
  token = cp_lexer_peek_token (parser->lexer);
14556
  /* A late-specified return type is indicated by an initial '->'. */
14557
  if (token->type != CPP_DEREF)
14558
    return NULL_TREE;
14559
 
14560
  /* Consume the ->.  */
14561
  cp_lexer_consume_token (parser->lexer);
14562
 
14563
  return cp_parser_trailing_type_id (parser);
14564
}
14565
 
14566
/* Parse a declarator-id.
14567
 
14568
   declarator-id:
14569
     id-expression
14570
     :: [opt] nested-name-specifier [opt] type-name
14571
 
14572
   In the `id-expression' case, the value returned is as for
14573
   cp_parser_id_expression if the id-expression was an unqualified-id.
14574
   If the id-expression was a qualified-id, then a SCOPE_REF is
14575
   returned.  The first operand is the scope (either a NAMESPACE_DECL
14576
   or TREE_TYPE), but the second is still just a representation of an
14577
   unqualified-id.  */
14578
 
14579
static tree
14580
cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14581
{
14582
  tree id;
14583
  /* The expression must be an id-expression.  Assume that qualified
14584
     names are the names of types so that:
14585
 
14586
       template <class T>
14587
       int S<T>::R::i = 3;
14588
 
14589
     will work; we must treat `S<T>::R' as the name of a type.
14590
     Similarly, assume that qualified names are templates, where
14591
     required, so that:
14592
 
14593
       template <class T>
14594
       int S<T>::R<T>::i = 3;
14595
 
14596
     will work, too.  */
14597
  id = cp_parser_id_expression (parser,
14598
                                /*template_keyword_p=*/false,
14599
                                /*check_dependency_p=*/false,
14600
                                /*template_p=*/NULL,
14601
                                /*declarator_p=*/true,
14602
                                optional_p);
14603
  if (id && BASELINK_P (id))
14604
    id = BASELINK_FUNCTIONS (id);
14605
  return id;
14606
}
14607
 
14608
/* Parse a type-id.
14609
 
14610
   type-id:
14611
     type-specifier-seq abstract-declarator [opt]
14612
 
14613
   Returns the TYPE specified.  */
14614
 
14615
static tree
14616
cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14617
                     bool is_trailing_return)
14618
{
14619
  cp_decl_specifier_seq type_specifier_seq;
14620
  cp_declarator *abstract_declarator;
14621
 
14622
  /* Parse the type-specifier-seq.  */
14623
  cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14624
                                is_trailing_return,
14625
                                &type_specifier_seq);
14626
  if (type_specifier_seq.type == error_mark_node)
14627
    return error_mark_node;
14628
 
14629
  /* There might or might not be an abstract declarator.  */
14630
  cp_parser_parse_tentatively (parser);
14631
  /* Look for the declarator.  */
14632
  abstract_declarator
14633
    = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14634
                            /*parenthesized_p=*/NULL,
14635
                            /*member_p=*/false);
14636
  /* Check to see if there really was a declarator.  */
14637
  if (!cp_parser_parse_definitely (parser))
14638
    abstract_declarator = NULL;
14639
 
14640
  if (type_specifier_seq.type
14641
      && type_uses_auto (type_specifier_seq.type))
14642
    {
14643
      /* A type-id with type 'auto' is only ok if the abstract declarator
14644
         is a function declarator with a late-specified return type.  */
14645
      if (abstract_declarator
14646
          && abstract_declarator->kind == cdk_function
14647
          && abstract_declarator->u.function.late_return_type)
14648
        /* OK */;
14649
      else
14650
        {
14651
          error ("invalid use of %<auto%>");
14652
          return error_mark_node;
14653
        }
14654
    }
14655
 
14656
  return groktypename (&type_specifier_seq, abstract_declarator,
14657
                       is_template_arg);
14658
}
14659
 
14660
static tree cp_parser_type_id (cp_parser *parser)
14661
{
14662
  return cp_parser_type_id_1 (parser, false, false);
14663
}
14664
 
14665
static tree cp_parser_template_type_arg (cp_parser *parser)
14666
{
14667
  return cp_parser_type_id_1 (parser, true, false);
14668
}
14669
 
14670
static tree cp_parser_trailing_type_id (cp_parser *parser)
14671
{
14672
  return cp_parser_type_id_1 (parser, false, true);
14673
}
14674
 
14675
/* Parse a type-specifier-seq.
14676
 
14677
   type-specifier-seq:
14678
     type-specifier type-specifier-seq [opt]
14679
 
14680
   GNU extension:
14681
 
14682
   type-specifier-seq:
14683
     attributes type-specifier-seq [opt]
14684
 
14685
   If IS_DECLARATION is true, we are at the start of a "condition" or
14686
   exception-declaration, so we might be followed by a declarator-id.
14687
 
14688
   If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14689
   i.e. we've just seen "->".
14690
 
14691
   Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
14692
 
14693
static void
14694
cp_parser_type_specifier_seq (cp_parser* parser,
14695
                              bool is_declaration,
14696
                              bool is_trailing_return,
14697
                              cp_decl_specifier_seq *type_specifier_seq)
14698
{
14699
  bool seen_type_specifier = false;
14700
  cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14701
  cp_token *start_token = NULL;
14702
 
14703
  /* Clear the TYPE_SPECIFIER_SEQ.  */
14704
  clear_decl_specs (type_specifier_seq);
14705
 
14706
  /* In the context of a trailing return type, enum E { } is an
14707
     elaborated-type-specifier followed by a function-body, not an
14708
     enum-specifier.  */
14709
  if (is_trailing_return)
14710
    flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14711
 
14712
  /* Parse the type-specifiers and attributes.  */
14713
  while (true)
14714
    {
14715
      tree type_specifier;
14716
      bool is_cv_qualifier;
14717
 
14718
      /* Check for attributes first.  */
14719
      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14720
        {
14721
          type_specifier_seq->attributes =
14722
            chainon (type_specifier_seq->attributes,
14723
                     cp_parser_attributes_opt (parser));
14724
          continue;
14725
        }
14726
 
14727
      /* record the token of the beginning of the type specifier seq,
14728
         for error reporting purposes*/
14729
     if (!start_token)
14730
       start_token = cp_lexer_peek_token (parser->lexer);
14731
 
14732
      /* Look for the type-specifier.  */
14733
      type_specifier = cp_parser_type_specifier (parser,
14734
                                                 flags,
14735
                                                 type_specifier_seq,
14736
                                                 /*is_declaration=*/false,
14737
                                                 NULL,
14738
                                                 &is_cv_qualifier);
14739
      if (!type_specifier)
14740
        {
14741
          /* If the first type-specifier could not be found, this is not a
14742
             type-specifier-seq at all.  */
14743
          if (!seen_type_specifier)
14744
            {
14745
              cp_parser_error (parser, "expected type-specifier");
14746
              type_specifier_seq->type = error_mark_node;
14747
              return;
14748
            }
14749
          /* If subsequent type-specifiers could not be found, the
14750
             type-specifier-seq is complete.  */
14751
          break;
14752
        }
14753
 
14754
      seen_type_specifier = true;
14755
      /* The standard says that a condition can be:
14756
 
14757
            type-specifier-seq declarator = assignment-expression
14758
 
14759
         However, given:
14760
 
14761
           struct S {};
14762
           if (int S = ...)
14763
 
14764
         we should treat the "S" as a declarator, not as a
14765
         type-specifier.  The standard doesn't say that explicitly for
14766
         type-specifier-seq, but it does say that for
14767
         decl-specifier-seq in an ordinary declaration.  Perhaps it
14768
         would be clearer just to allow a decl-specifier-seq here, and
14769
         then add a semantic restriction that if any decl-specifiers
14770
         that are not type-specifiers appear, the program is invalid.  */
14771
      if (is_declaration && !is_cv_qualifier)
14772
        flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14773
    }
14774
 
14775
  cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14776
}
14777
 
14778
/* Parse a parameter-declaration-clause.
14779
 
14780
   parameter-declaration-clause:
14781
     parameter-declaration-list [opt] ... [opt]
14782
     parameter-declaration-list , ...
14783
 
14784
   Returns a representation for the parameter declarations.  A return
14785
   value of NULL indicates a parameter-declaration-clause consisting
14786
   only of an ellipsis.  */
14787
 
14788
static tree
14789
cp_parser_parameter_declaration_clause (cp_parser* parser)
14790
{
14791
  tree parameters;
14792
  cp_token *token;
14793
  bool ellipsis_p;
14794
  bool is_error;
14795
 
14796
  /* Peek at the next token.  */
14797
  token = cp_lexer_peek_token (parser->lexer);
14798
  /* Check for trivial parameter-declaration-clauses.  */
14799
  if (token->type == CPP_ELLIPSIS)
14800
    {
14801
      /* Consume the `...' token.  */
14802
      cp_lexer_consume_token (parser->lexer);
14803
      return NULL_TREE;
14804
    }
14805
  else if (token->type == CPP_CLOSE_PAREN)
14806
    /* There are no parameters.  */
14807
    {
14808
#ifndef NO_IMPLICIT_EXTERN_C
14809
      if (in_system_header && current_class_type == NULL
14810
          && current_lang_name == lang_name_c)
14811
        return NULL_TREE;
14812
      else
14813
#endif
14814
        return void_list_node;
14815
    }
14816
  /* Check for `(void)', too, which is a special case.  */
14817
  else if (token->keyword == RID_VOID
14818
           && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14819
               == CPP_CLOSE_PAREN))
14820
    {
14821
      /* Consume the `void' token.  */
14822
      cp_lexer_consume_token (parser->lexer);
14823
      /* There are no parameters.  */
14824
      return void_list_node;
14825
    }
14826
 
14827
  /* Parse the parameter-declaration-list.  */
14828
  parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14829
  /* If a parse error occurred while parsing the
14830
     parameter-declaration-list, then the entire
14831
     parameter-declaration-clause is erroneous.  */
14832
  if (is_error)
14833
    return NULL;
14834
 
14835
  /* Peek at the next token.  */
14836
  token = cp_lexer_peek_token (parser->lexer);
14837
  /* If it's a `,', the clause should terminate with an ellipsis.  */
14838
  if (token->type == CPP_COMMA)
14839
    {
14840
      /* Consume the `,'.  */
14841
      cp_lexer_consume_token (parser->lexer);
14842
      /* Expect an ellipsis.  */
14843
      ellipsis_p
14844
        = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14845
    }
14846
  /* It might also be `...' if the optional trailing `,' was
14847
     omitted.  */
14848
  else if (token->type == CPP_ELLIPSIS)
14849
    {
14850
      /* Consume the `...' token.  */
14851
      cp_lexer_consume_token (parser->lexer);
14852
      /* And remember that we saw it.  */
14853
      ellipsis_p = true;
14854
    }
14855
  else
14856
    ellipsis_p = false;
14857
 
14858
  /* Finish the parameter list.  */
14859
  if (!ellipsis_p)
14860
    parameters = chainon (parameters, void_list_node);
14861
 
14862
  return parameters;
14863
}
14864
 
14865
/* Parse a parameter-declaration-list.
14866
 
14867
   parameter-declaration-list:
14868
     parameter-declaration
14869
     parameter-declaration-list , parameter-declaration
14870
 
14871
   Returns a representation of the parameter-declaration-list, as for
14872
   cp_parser_parameter_declaration_clause.  However, the
14873
   `void_list_node' is never appended to the list.  Upon return,
14874
   *IS_ERROR will be true iff an error occurred.  */
14875
 
14876
static tree
14877
cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14878
{
14879
  tree parameters = NULL_TREE;
14880
  tree *tail = &parameters;
14881
  bool saved_in_unbraced_linkage_specification_p;
14882
  int index = 0;
14883
 
14884
  /* Assume all will go well.  */
14885
  *is_error = false;
14886
  /* The special considerations that apply to a function within an
14887
     unbraced linkage specifications do not apply to the parameters
14888
     to the function.  */
14889
  saved_in_unbraced_linkage_specification_p
14890
    = parser->in_unbraced_linkage_specification_p;
14891
  parser->in_unbraced_linkage_specification_p = false;
14892
 
14893
  /* Look for more parameters.  */
14894
  while (true)
14895
    {
14896
      cp_parameter_declarator *parameter;
14897
      tree decl = error_mark_node;
14898
      bool parenthesized_p;
14899
      /* Parse the parameter.  */
14900
      parameter
14901
        = cp_parser_parameter_declaration (parser,
14902
                                           /*template_parm_p=*/false,
14903
                                           &parenthesized_p);
14904
 
14905
      /* We don't know yet if the enclosing context is deprecated, so wait
14906
         and warn in grokparms if appropriate.  */
14907
      deprecated_state = DEPRECATED_SUPPRESS;
14908
 
14909
      if (parameter)
14910
        decl = grokdeclarator (parameter->declarator,
14911
                               &parameter->decl_specifiers,
14912
                               PARM,
14913
                               parameter->default_argument != NULL_TREE,
14914
                               &parameter->decl_specifiers.attributes);
14915
 
14916
      deprecated_state = DEPRECATED_NORMAL;
14917
 
14918
      /* If a parse error occurred parsing the parameter declaration,
14919
         then the entire parameter-declaration-list is erroneous.  */
14920
      if (decl == error_mark_node)
14921
        {
14922
          *is_error = true;
14923
          parameters = error_mark_node;
14924
          break;
14925
        }
14926
 
14927
      if (parameter->decl_specifiers.attributes)
14928
        cplus_decl_attributes (&decl,
14929
                               parameter->decl_specifiers.attributes,
14930
                               0);
14931
      if (DECL_NAME (decl))
14932
        decl = pushdecl (decl);
14933
 
14934
      if (decl != error_mark_node)
14935
        {
14936
          retrofit_lang_decl (decl);
14937
          DECL_PARM_INDEX (decl) = ++index;
14938
        }
14939
 
14940
      /* Add the new parameter to the list.  */
14941
      *tail = build_tree_list (parameter->default_argument, decl);
14942
      tail = &TREE_CHAIN (*tail);
14943
 
14944
      /* Peek at the next token.  */
14945
      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14946
          || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14947
          /* These are for Objective-C++ */
14948
          || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14949
          || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14950
        /* The parameter-declaration-list is complete.  */
14951
        break;
14952
      else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14953
        {
14954
          cp_token *token;
14955
 
14956
          /* Peek at the next token.  */
14957
          token = cp_lexer_peek_nth_token (parser->lexer, 2);
14958
          /* If it's an ellipsis, then the list is complete.  */
14959
          if (token->type == CPP_ELLIPSIS)
14960
            break;
14961
          /* Otherwise, there must be more parameters.  Consume the
14962
             `,'.  */
14963
          cp_lexer_consume_token (parser->lexer);
14964
          /* When parsing something like:
14965
 
14966
                int i(float f, double d)
14967
 
14968
             we can tell after seeing the declaration for "f" that we
14969
             are not looking at an initialization of a variable "i",
14970
             but rather at the declaration of a function "i".
14971
 
14972
             Due to the fact that the parsing of template arguments
14973
             (as specified to a template-id) requires backtracking we
14974
             cannot use this technique when inside a template argument
14975
             list.  */
14976
          if (!parser->in_template_argument_list_p
14977
              && !parser->in_type_id_in_expr_p
14978
              && cp_parser_uncommitted_to_tentative_parse_p (parser)
14979
              /* However, a parameter-declaration of the form
14980
                 "foat(f)" (which is a valid declaration of a
14981
                 parameter "f") can also be interpreted as an
14982
                 expression (the conversion of "f" to "float").  */
14983
              && !parenthesized_p)
14984
            cp_parser_commit_to_tentative_parse (parser);
14985
        }
14986
      else
14987
        {
14988
          cp_parser_error (parser, "expected %<,%> or %<...%>");
14989
          if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14990
            cp_parser_skip_to_closing_parenthesis (parser,
14991
                                                   /*recovering=*/true,
14992
                                                   /*or_comma=*/false,
14993
                                                   /*consume_paren=*/false);
14994
          break;
14995
        }
14996
    }
14997
 
14998
  parser->in_unbraced_linkage_specification_p
14999
    = saved_in_unbraced_linkage_specification_p;
15000
 
15001
  return parameters;
15002
}
15003
 
15004
/* Parse a parameter declaration.
15005
 
15006
   parameter-declaration:
15007
     decl-specifier-seq ... [opt] declarator
15008
     decl-specifier-seq declarator = assignment-expression
15009
     decl-specifier-seq ... [opt] abstract-declarator [opt]
15010
     decl-specifier-seq abstract-declarator [opt] = assignment-expression
15011
 
15012
   If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15013
   declares a template parameter.  (In that case, a non-nested `>'
15014
   token encountered during the parsing of the assignment-expression
15015
   is not interpreted as a greater-than operator.)
15016
 
15017
   Returns a representation of the parameter, or NULL if an error
15018
   occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15019
   true iff the declarator is of the form "(p)".  */
15020
 
15021
static cp_parameter_declarator *
15022
cp_parser_parameter_declaration (cp_parser *parser,
15023
                                 bool template_parm_p,
15024
                                 bool *parenthesized_p)
15025
{
15026
  int declares_class_or_enum;
15027
  cp_decl_specifier_seq decl_specifiers;
15028
  cp_declarator *declarator;
15029
  tree default_argument;
15030
  cp_token *token = NULL, *declarator_token_start = NULL;
15031
  const char *saved_message;
15032
 
15033
  /* In a template parameter, `>' is not an operator.
15034
 
15035
     [temp.param]
15036
 
15037
     When parsing a default template-argument for a non-type
15038
     template-parameter, the first non-nested `>' is taken as the end
15039
     of the template parameter-list rather than a greater-than
15040
     operator.  */
15041
 
15042
  /* Type definitions may not appear in parameter types.  */
15043
  saved_message = parser->type_definition_forbidden_message;
15044
  parser->type_definition_forbidden_message
15045
    = G_("types may not be defined in parameter types");
15046
 
15047
  /* Parse the declaration-specifiers.  */
15048
  cp_parser_decl_specifier_seq (parser,
15049
                                CP_PARSER_FLAGS_NONE,
15050
                                &decl_specifiers,
15051
                                &declares_class_or_enum);
15052
 
15053
  /* Complain about missing 'typename' or other invalid type names.  */
15054
  if (!decl_specifiers.any_type_specifiers_p)
15055
    cp_parser_parse_and_diagnose_invalid_type_name (parser);
15056
 
15057
  /* If an error occurred, there's no reason to attempt to parse the
15058
     rest of the declaration.  */
15059
  if (cp_parser_error_occurred (parser))
15060
    {
15061
      parser->type_definition_forbidden_message = saved_message;
15062
      return NULL;
15063
    }
15064
 
15065
  /* Peek at the next token.  */
15066
  token = cp_lexer_peek_token (parser->lexer);
15067
 
15068
  /* If the next token is a `)', `,', `=', `>', or `...', then there
15069
     is no declarator. However, when variadic templates are enabled,
15070
     there may be a declarator following `...'.  */
15071
  if (token->type == CPP_CLOSE_PAREN
15072
      || token->type == CPP_COMMA
15073
      || token->type == CPP_EQ
15074
      || token->type == CPP_GREATER)
15075
    {
15076
      declarator = NULL;
15077
      if (parenthesized_p)
15078
        *parenthesized_p = false;
15079
    }
15080
  /* Otherwise, there should be a declarator.  */
15081
  else
15082
    {
15083
      bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15084
      parser->default_arg_ok_p = false;
15085
 
15086
      /* After seeing a decl-specifier-seq, if the next token is not a
15087
         "(", there is no possibility that the code is a valid
15088
         expression.  Therefore, if parsing tentatively, we commit at
15089
         this point.  */
15090
      if (!parser->in_template_argument_list_p
15091
          /* In an expression context, having seen:
15092
 
15093
               (int((char ...
15094
 
15095
             we cannot be sure whether we are looking at a
15096
             function-type (taking a "char" as a parameter) or a cast
15097
             of some object of type "char" to "int".  */
15098
          && !parser->in_type_id_in_expr_p
15099
          && cp_parser_uncommitted_to_tentative_parse_p (parser)
15100
          && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15101
        cp_parser_commit_to_tentative_parse (parser);
15102
      /* Parse the declarator.  */
15103
      declarator_token_start = token;
15104
      declarator = cp_parser_declarator (parser,
15105
                                         CP_PARSER_DECLARATOR_EITHER,
15106
                                         /*ctor_dtor_or_conv_p=*/NULL,
15107
                                         parenthesized_p,
15108
                                         /*member_p=*/false);
15109
      parser->default_arg_ok_p = saved_default_arg_ok_p;
15110
      /* After the declarator, allow more attributes.  */
15111
      decl_specifiers.attributes
15112
        = chainon (decl_specifiers.attributes,
15113
                   cp_parser_attributes_opt (parser));
15114
    }
15115
 
15116
  /* If the next token is an ellipsis, and we have not seen a
15117
     declarator name, and the type of the declarator contains parameter
15118
     packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15119
     a parameter pack expansion expression. Otherwise, leave the
15120
     ellipsis for a C-style variadic function. */
15121
  token = cp_lexer_peek_token (parser->lexer);
15122
  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15123
    {
15124
      tree type = decl_specifiers.type;
15125
 
15126
      if (type && DECL_P (type))
15127
        type = TREE_TYPE (type);
15128
 
15129
      if (type
15130
          && TREE_CODE (type) != TYPE_PACK_EXPANSION
15131
          && declarator_can_be_parameter_pack (declarator)
15132
          && (!declarator || !declarator->parameter_pack_p)
15133
          && uses_parameter_packs (type))
15134
        {
15135
          /* Consume the `...'. */
15136
          cp_lexer_consume_token (parser->lexer);
15137
          maybe_warn_variadic_templates ();
15138
 
15139
          /* Build a pack expansion type */
15140
          if (declarator)
15141
            declarator->parameter_pack_p = true;
15142
          else
15143
            decl_specifiers.type = make_pack_expansion (type);
15144
        }
15145
    }
15146
 
15147
  /* The restriction on defining new types applies only to the type
15148
     of the parameter, not to the default argument.  */
15149
  parser->type_definition_forbidden_message = saved_message;
15150
 
15151
  /* If the next token is `=', then process a default argument.  */
15152
  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15153
    {
15154
      /* Consume the `='.  */
15155
      cp_lexer_consume_token (parser->lexer);
15156
 
15157
      /* If we are defining a class, then the tokens that make up the
15158
         default argument must be saved and processed later.  */
15159
      if (!template_parm_p && at_class_scope_p ()
15160
          && TYPE_BEING_DEFINED (current_class_type)
15161
          && !LAMBDA_TYPE_P (current_class_type))
15162
        {
15163
          unsigned depth = 0;
15164
          int maybe_template_id = 0;
15165
          cp_token *first_token;
15166
          cp_token *token;
15167
 
15168
          /* Add tokens until we have processed the entire default
15169
             argument.  We add the range [first_token, token).  */
15170
          first_token = cp_lexer_peek_token (parser->lexer);
15171
          while (true)
15172
            {
15173
              bool done = false;
15174
 
15175
              /* Peek at the next token.  */
15176
              token = cp_lexer_peek_token (parser->lexer);
15177
              /* What we do depends on what token we have.  */
15178
              switch (token->type)
15179
                {
15180
                  /* In valid code, a default argument must be
15181
                     immediately followed by a `,' `)', or `...'.  */
15182
                case CPP_COMMA:
15183
                  if (depth == 0 && maybe_template_id)
15184
                    {
15185
                      /* If we've seen a '<', we might be in a
15186
                         template-argument-list.  Until Core issue 325 is
15187
                         resolved, we don't know how this situation ought
15188
                         to be handled, so try to DTRT.  We check whether
15189
                         what comes after the comma is a valid parameter
15190
                         declaration list.  If it is, then the comma ends
15191
                         the default argument; otherwise the default
15192
                         argument continues.  */
15193
                      bool error = false;
15194
 
15195
                      /* Set ITALP so cp_parser_parameter_declaration_list
15196
                         doesn't decide to commit to this parse.  */
15197
                      bool saved_italp = parser->in_template_argument_list_p;
15198
                      parser->in_template_argument_list_p = true;
15199
 
15200
                      cp_parser_parse_tentatively (parser);
15201
                      cp_lexer_consume_token (parser->lexer);
15202
                      cp_parser_parameter_declaration_list (parser, &error);
15203
                      if (!cp_parser_error_occurred (parser) && !error)
15204
                        done = true;
15205
                      cp_parser_abort_tentative_parse (parser);
15206
 
15207
                      parser->in_template_argument_list_p = saved_italp;
15208
                      break;
15209
                    }
15210
                case CPP_CLOSE_PAREN:
15211
                case CPP_ELLIPSIS:
15212
                  /* If we run into a non-nested `;', `}', or `]',
15213
                     then the code is invalid -- but the default
15214
                     argument is certainly over.  */
15215
                case CPP_SEMICOLON:
15216
                case CPP_CLOSE_BRACE:
15217
                case CPP_CLOSE_SQUARE:
15218
                  if (depth == 0)
15219
                    done = true;
15220
                  /* Update DEPTH, if necessary.  */
15221
                  else if (token->type == CPP_CLOSE_PAREN
15222
                           || token->type == CPP_CLOSE_BRACE
15223
                           || token->type == CPP_CLOSE_SQUARE)
15224
                    --depth;
15225
                  break;
15226
 
15227
                case CPP_OPEN_PAREN:
15228
                case CPP_OPEN_SQUARE:
15229
                case CPP_OPEN_BRACE:
15230
                  ++depth;
15231
                  break;
15232
 
15233
                case CPP_LESS:
15234
                  if (depth == 0)
15235
                    /* This might be the comparison operator, or it might
15236
                       start a template argument list.  */
15237
                    ++maybe_template_id;
15238
                  break;
15239
 
15240
                case CPP_RSHIFT:
15241
                  if (cxx_dialect == cxx98)
15242
                    break;
15243
                  /* Fall through for C++0x, which treats the `>>'
15244
                     operator like two `>' tokens in certain
15245
                     cases.  */
15246
 
15247
                case CPP_GREATER:
15248
                  if (depth == 0)
15249
                    {
15250
                      /* This might be an operator, or it might close a
15251
                         template argument list.  But if a previous '<'
15252
                         started a template argument list, this will have
15253
                         closed it, so we can't be in one anymore.  */
15254
                      maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15255
                      if (maybe_template_id < 0)
15256
                        maybe_template_id = 0;
15257
                    }
15258
                  break;
15259
 
15260
                  /* If we run out of tokens, issue an error message.  */
15261
                case CPP_EOF:
15262
                case CPP_PRAGMA_EOL:
15263
                  error_at (token->location, "file ends in default argument");
15264
                  done = true;
15265
                  break;
15266
 
15267
                case CPP_NAME:
15268
                case CPP_SCOPE:
15269
                  /* In these cases, we should look for template-ids.
15270
                     For example, if the default argument is
15271
                     `X<int, double>()', we need to do name lookup to
15272
                     figure out whether or not `X' is a template; if
15273
                     so, the `,' does not end the default argument.
15274
 
15275
                     That is not yet done.  */
15276
                  break;
15277
 
15278
                default:
15279
                  break;
15280
                }
15281
 
15282
              /* If we've reached the end, stop.  */
15283
              if (done)
15284
                break;
15285
 
15286
              /* Add the token to the token block.  */
15287
              token = cp_lexer_consume_token (parser->lexer);
15288
            }
15289
 
15290
          /* Create a DEFAULT_ARG to represent the unparsed default
15291
             argument.  */
15292
          default_argument = make_node (DEFAULT_ARG);
15293
          DEFARG_TOKENS (default_argument)
15294
            = cp_token_cache_new (first_token, token);
15295
          DEFARG_INSTANTIATIONS (default_argument) = NULL;
15296
        }
15297
      /* Outside of a class definition, we can just parse the
15298
         assignment-expression.  */
15299
      else
15300
        {
15301
          token = cp_lexer_peek_token (parser->lexer);
15302
          default_argument
15303
            = cp_parser_default_argument (parser, template_parm_p);
15304
        }
15305
 
15306
      if (!parser->default_arg_ok_p)
15307
        {
15308
          if (flag_permissive)
15309
            warning (0, "deprecated use of default argument for parameter of non-function");
15310
          else
15311
            {
15312
              error_at (token->location,
15313
                        "default arguments are only "
15314
                        "permitted for function parameters");
15315
              default_argument = NULL_TREE;
15316
            }
15317
        }
15318
      else if ((declarator && declarator->parameter_pack_p)
15319
               || (decl_specifiers.type
15320
                   && PACK_EXPANSION_P (decl_specifiers.type)))
15321
        {
15322
          /* Find the name of the parameter pack.  */
15323
          cp_declarator *id_declarator = declarator;
15324
          while (id_declarator && id_declarator->kind != cdk_id)
15325
            id_declarator = id_declarator->declarator;
15326
 
15327
          if (id_declarator && id_declarator->kind == cdk_id)
15328
            error_at (declarator_token_start->location,
15329
                      template_parm_p
15330
                      ? "template parameter pack %qD"
15331
                      " cannot have a default argument"
15332
                      : "parameter pack %qD cannot have a default argument",
15333
                      id_declarator->u.id.unqualified_name);
15334
          else
15335
            error_at (declarator_token_start->location,
15336
                      template_parm_p
15337
                      ? "template parameter pack cannot have a default argument"
15338
                      : "parameter pack cannot have a default argument");
15339
 
15340
          default_argument = NULL_TREE;
15341
        }
15342
    }
15343
  else
15344
    default_argument = NULL_TREE;
15345
 
15346
  return make_parameter_declarator (&decl_specifiers,
15347
                                    declarator,
15348
                                    default_argument);
15349
}
15350
 
15351
/* Parse a default argument and return it.
15352
 
15353
   TEMPLATE_PARM_P is true if this is a default argument for a
15354
   non-type template parameter.  */
15355
static tree
15356
cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15357
{
15358
  tree default_argument = NULL_TREE;
15359
  bool saved_greater_than_is_operator_p;
15360
  bool saved_local_variables_forbidden_p;
15361
 
15362
  /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15363
     set correctly.  */
15364
  saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15365
  parser->greater_than_is_operator_p = !template_parm_p;
15366
  /* Local variable names (and the `this' keyword) may not
15367
     appear in a default argument.  */
15368
  saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15369
  parser->local_variables_forbidden_p = true;
15370
  /* Parse the assignment-expression.  */
15371
  if (template_parm_p)
15372
    push_deferring_access_checks (dk_no_deferred);
15373
  default_argument
15374
    = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15375
  if (template_parm_p)
15376
    pop_deferring_access_checks ();
15377
  parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15378
  parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15379
 
15380
  return default_argument;
15381
}
15382
 
15383
/* Parse a function-body.
15384
 
15385
   function-body:
15386
     compound_statement  */
15387
 
15388
static void
15389
cp_parser_function_body (cp_parser *parser)
15390
{
15391
  cp_parser_compound_statement (parser, NULL, false);
15392
}
15393
 
15394
/* Parse a ctor-initializer-opt followed by a function-body.  Return
15395
   true if a ctor-initializer was present.  */
15396
 
15397
static bool
15398
cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15399
{
15400
  tree body;
15401
  bool ctor_initializer_p;
15402
 
15403
  /* Begin the function body.  */
15404
  body = begin_function_body ();
15405
  /* Parse the optional ctor-initializer.  */
15406
  ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15407
  /* Parse the function-body.  */
15408
  cp_parser_function_body (parser);
15409
  /* Finish the function body.  */
15410
  finish_function_body (body);
15411
 
15412
  return ctor_initializer_p;
15413
}
15414
 
15415
/* Parse an initializer.
15416
 
15417
   initializer:
15418
     = initializer-clause
15419
     ( expression-list )
15420
 
15421
   Returns an expression representing the initializer.  If no
15422
   initializer is present, NULL_TREE is returned.
15423
 
15424
   *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15425
   production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
15426
   set to TRUE if there is no initializer present.  If there is an
15427
   initializer, and it is not a constant-expression, *NON_CONSTANT_P
15428
   is set to true; otherwise it is set to false.  */
15429
 
15430
static tree
15431
cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15432
                       bool* non_constant_p)
15433
{
15434
  cp_token *token;
15435
  tree init;
15436
 
15437
  /* Peek at the next token.  */
15438
  token = cp_lexer_peek_token (parser->lexer);
15439
 
15440
  /* Let our caller know whether or not this initializer was
15441
     parenthesized.  */
15442
  *is_direct_init = (token->type != CPP_EQ);
15443
  /* Assume that the initializer is constant.  */
15444
  *non_constant_p = false;
15445
 
15446
  if (token->type == CPP_EQ)
15447
    {
15448
      /* Consume the `='.  */
15449
      cp_lexer_consume_token (parser->lexer);
15450
      /* Parse the initializer-clause.  */
15451
      init = cp_parser_initializer_clause (parser, non_constant_p);
15452
    }
15453
  else if (token->type == CPP_OPEN_PAREN)
15454
    {
15455
      VEC(tree,gc) *vec;
15456
      vec = cp_parser_parenthesized_expression_list (parser, false,
15457
                                                     /*cast_p=*/false,
15458
                                                     /*allow_expansion_p=*/true,
15459
                                                     non_constant_p);
15460
      if (vec == NULL)
15461
        return error_mark_node;
15462
      init = build_tree_list_vec (vec);
15463
      release_tree_vector (vec);
15464
    }
15465
  else if (token->type == CPP_OPEN_BRACE)
15466
    {
15467
      maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15468
      init = cp_parser_braced_list (parser, non_constant_p);
15469
      CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15470
    }
15471
  else
15472
    {
15473
      /* Anything else is an error.  */
15474
      cp_parser_error (parser, "expected initializer");
15475
      init = error_mark_node;
15476
    }
15477
 
15478
  return init;
15479
}
15480
 
15481
/* Parse an initializer-clause.
15482
 
15483
   initializer-clause:
15484
     assignment-expression
15485
     braced-init-list
15486
 
15487
   Returns an expression representing the initializer.
15488
 
15489
   If the `assignment-expression' production is used the value
15490
   returned is simply a representation for the expression.
15491
 
15492
   Otherwise, calls cp_parser_braced_list.  */
15493
 
15494
static tree
15495
cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15496
{
15497
  tree initializer;
15498
 
15499
  /* Assume the expression is constant.  */
15500
  *non_constant_p = false;
15501
 
15502
  /* If it is not a `{', then we are looking at an
15503
     assignment-expression.  */
15504
  if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15505
    {
15506
      initializer
15507
        = cp_parser_constant_expression (parser,
15508
                                        /*allow_non_constant_p=*/true,
15509
                                        non_constant_p);
15510
      if (!*non_constant_p)
15511
        initializer = fold_non_dependent_expr (initializer);
15512
    }
15513
  else
15514
    initializer = cp_parser_braced_list (parser, non_constant_p);
15515
 
15516
  return initializer;
15517
}
15518
 
15519
/* Parse a brace-enclosed initializer list.
15520
 
15521
   braced-init-list:
15522
     { initializer-list , [opt] }
15523
     { }
15524
 
15525
   Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
15526
   the elements of the initializer-list (or NULL, if the last
15527
   production is used).  The TREE_TYPE for the CONSTRUCTOR will be
15528
   NULL_TREE.  There is no way to detect whether or not the optional
15529
   trailing `,' was provided.  NON_CONSTANT_P is as for
15530
   cp_parser_initializer.  */
15531
 
15532
static tree
15533
cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15534
{
15535
  tree initializer;
15536
 
15537
  /* Consume the `{' token.  */
15538
  cp_lexer_consume_token (parser->lexer);
15539
  /* Create a CONSTRUCTOR to represent the braced-initializer.  */
15540
  initializer = make_node (CONSTRUCTOR);
15541
  /* If it's not a `}', then there is a non-trivial initializer.  */
15542
  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15543
    {
15544
      /* Parse the initializer list.  */
15545
      CONSTRUCTOR_ELTS (initializer)
15546
        = cp_parser_initializer_list (parser, non_constant_p);
15547
      /* A trailing `,' token is allowed.  */
15548
      if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15549
        cp_lexer_consume_token (parser->lexer);
15550
    }
15551
  /* Now, there should be a trailing `}'.  */
15552
  cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15553
  TREE_TYPE (initializer) = init_list_type_node;
15554
  return initializer;
15555
}
15556
 
15557
/* Parse an initializer-list.
15558
 
15559
   initializer-list:
15560
     initializer-clause ... [opt]
15561
     initializer-list , initializer-clause ... [opt]
15562
 
15563
   GNU Extension:
15564
 
15565
   initializer-list:
15566
     identifier : initializer-clause
15567
     initializer-list, identifier : initializer-clause
15568
 
15569
   Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
15570
   for the initializer.  If the INDEX of the elt is non-NULL, it is the
15571
   IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
15572
   as for cp_parser_initializer.  */
15573
 
15574
static VEC(constructor_elt,gc) *
15575
cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15576
{
15577
  VEC(constructor_elt,gc) *v = NULL;
15578
 
15579
  /* Assume all of the expressions are constant.  */
15580
  *non_constant_p = false;
15581
 
15582
  /* Parse the rest of the list.  */
15583
  while (true)
15584
    {
15585
      cp_token *token;
15586
      tree identifier;
15587
      tree initializer;
15588
      bool clause_non_constant_p;
15589
 
15590
      /* If the next token is an identifier and the following one is a
15591
         colon, we are looking at the GNU designated-initializer
15592
         syntax.  */
15593
      if (cp_parser_allow_gnu_extensions_p (parser)
15594
          && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15595
          && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15596
        {
15597
          /* Warn the user that they are using an extension.  */
15598
          pedwarn (input_location, OPT_pedantic,
15599
                   "ISO C++ does not allow designated initializers");
15600
          /* Consume the identifier.  */
15601
          identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15602
          /* Consume the `:'.  */
15603
          cp_lexer_consume_token (parser->lexer);
15604
        }
15605
      else
15606
        identifier = NULL_TREE;
15607
 
15608
      /* Parse the initializer.  */
15609
      initializer = cp_parser_initializer_clause (parser,
15610
                                                  &clause_non_constant_p);
15611
      /* If any clause is non-constant, so is the entire initializer.  */
15612
      if (clause_non_constant_p)
15613
        *non_constant_p = true;
15614
 
15615
      /* If we have an ellipsis, this is an initializer pack
15616
         expansion.  */
15617
      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15618
        {
15619
          /* Consume the `...'.  */
15620
          cp_lexer_consume_token (parser->lexer);
15621
 
15622
          /* Turn the initializer into an initializer expansion.  */
15623
          initializer = make_pack_expansion (initializer);
15624
        }
15625
 
15626
      /* Add it to the vector.  */
15627
      CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15628
 
15629
      /* If the next token is not a comma, we have reached the end of
15630
         the list.  */
15631
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15632
        break;
15633
 
15634
      /* Peek at the next token.  */
15635
      token = cp_lexer_peek_nth_token (parser->lexer, 2);
15636
      /* If the next token is a `}', then we're still done.  An
15637
         initializer-clause can have a trailing `,' after the
15638
         initializer-list and before the closing `}'.  */
15639
      if (token->type == CPP_CLOSE_BRACE)
15640
        break;
15641
 
15642
      /* Consume the `,' token.  */
15643
      cp_lexer_consume_token (parser->lexer);
15644
    }
15645
 
15646
  return v;
15647
}
15648
 
15649
/* Classes [gram.class] */
15650
 
15651
/* Parse a class-name.
15652
 
15653
   class-name:
15654
     identifier
15655
     template-id
15656
 
15657
   TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15658
   to indicate that names looked up in dependent types should be
15659
   assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
15660
   keyword has been used to indicate that the name that appears next
15661
   is a template.  TAG_TYPE indicates the explicit tag given before
15662
   the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
15663
   looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
15664
   is the class being defined in a class-head.
15665
 
15666
   Returns the TYPE_DECL representing the class.  */
15667
 
15668
static tree
15669
cp_parser_class_name (cp_parser *parser,
15670
                      bool typename_keyword_p,
15671
                      bool template_keyword_p,
15672
                      enum tag_types tag_type,
15673
                      bool check_dependency_p,
15674
                      bool class_head_p,
15675
                      bool is_declaration)
15676
{
15677
  tree decl;
15678
  tree scope;
15679
  bool typename_p;
15680
  cp_token *token;
15681
  tree identifier = NULL_TREE;
15682
 
15683
  /* All class-names start with an identifier.  */
15684
  token = cp_lexer_peek_token (parser->lexer);
15685
  if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15686
    {
15687
      cp_parser_error (parser, "expected class-name");
15688
      return error_mark_node;
15689
    }
15690
 
15691
  /* PARSER->SCOPE can be cleared when parsing the template-arguments
15692
     to a template-id, so we save it here.  */
15693
  scope = parser->scope;
15694
  if (scope == error_mark_node)
15695
    return error_mark_node;
15696
 
15697
  /* Any name names a type if we're following the `typename' keyword
15698
     in a qualified name where the enclosing scope is type-dependent.  */
15699
  typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15700
                && dependent_type_p (scope));
15701
  /* Handle the common case (an identifier, but not a template-id)
15702
     efficiently.  */
15703
  if (token->type == CPP_NAME
15704
      && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15705
    {
15706
      cp_token *identifier_token;
15707
      bool ambiguous_p;
15708
 
15709
      /* Look for the identifier.  */
15710
      identifier_token = cp_lexer_peek_token (parser->lexer);
15711
      ambiguous_p = identifier_token->ambiguous_p;
15712
      identifier = cp_parser_identifier (parser);
15713
      /* If the next token isn't an identifier, we are certainly not
15714
         looking at a class-name.  */
15715
      if (identifier == error_mark_node)
15716
        decl = error_mark_node;
15717
      /* If we know this is a type-name, there's no need to look it
15718
         up.  */
15719
      else if (typename_p)
15720
        decl = identifier;
15721
      else
15722
        {
15723
          tree ambiguous_decls;
15724
          /* If we already know that this lookup is ambiguous, then
15725
             we've already issued an error message; there's no reason
15726
             to check again.  */
15727
          if (ambiguous_p)
15728
            {
15729
              cp_parser_simulate_error (parser);
15730
              return error_mark_node;
15731
            }
15732
          /* If the next token is a `::', then the name must be a type
15733
             name.
15734
 
15735
             [basic.lookup.qual]
15736
 
15737
             During the lookup for a name preceding the :: scope
15738
             resolution operator, object, function, and enumerator
15739
             names are ignored.  */
15740
          if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15741
            tag_type = typename_type;
15742
          /* Look up the name.  */
15743
          decl = cp_parser_lookup_name (parser, identifier,
15744
                                        tag_type,
15745
                                        /*is_template=*/false,
15746
                                        /*is_namespace=*/false,
15747
                                        check_dependency_p,
15748
                                        &ambiguous_decls,
15749
                                        identifier_token->location);
15750
          if (ambiguous_decls)
15751
            {
15752
              if (cp_parser_parsing_tentatively (parser))
15753
                cp_parser_simulate_error (parser);
15754
              return error_mark_node;
15755
            }
15756
        }
15757
    }
15758
  else
15759
    {
15760
      /* Try a template-id.  */
15761
      decl = cp_parser_template_id (parser, template_keyword_p,
15762
                                    check_dependency_p,
15763
                                    is_declaration);
15764
      if (decl == error_mark_node)
15765
        return error_mark_node;
15766
    }
15767
 
15768
  decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15769
 
15770
  /* If this is a typename, create a TYPENAME_TYPE.  */
15771
  if (typename_p && decl != error_mark_node)
15772
    {
15773
      decl = make_typename_type (scope, decl, typename_type,
15774
                                 /*complain=*/tf_error);
15775
      if (decl != error_mark_node)
15776
        decl = TYPE_NAME (decl);
15777
    }
15778
 
15779
  /* Check to see that it is really the name of a class.  */
15780
  if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15781
      && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15782
      && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15783
    /* Situations like this:
15784
 
15785
         template <typename T> struct A {
15786
           typename T::template X<int>::I i;
15787
         };
15788
 
15789
       are problematic.  Is `T::template X<int>' a class-name?  The
15790
       standard does not seem to be definitive, but there is no other
15791
       valid interpretation of the following `::'.  Therefore, those
15792
       names are considered class-names.  */
15793
    {
15794
      decl = make_typename_type (scope, decl, tag_type, tf_error);
15795
      if (decl != error_mark_node)
15796
        decl = TYPE_NAME (decl);
15797
    }
15798
  else if (TREE_CODE (decl) != TYPE_DECL
15799
           || TREE_TYPE (decl) == error_mark_node
15800
           || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15801
    decl = error_mark_node;
15802
 
15803
  if (decl == error_mark_node)
15804
    cp_parser_error (parser, "expected class-name");
15805
  else if (identifier && !parser->scope)
15806
    maybe_note_name_used_in_class (identifier, decl);
15807
 
15808
  return decl;
15809
}
15810
 
15811
/* Parse a class-specifier.
15812
 
15813
   class-specifier:
15814
     class-head { member-specification [opt] }
15815
 
15816
   Returns the TREE_TYPE representing the class.  */
15817
 
15818
static tree
15819
cp_parser_class_specifier (cp_parser* parser)
15820
{
15821
  tree type;
15822
  tree attributes = NULL_TREE;
15823
  bool nested_name_specifier_p;
15824
  unsigned saved_num_template_parameter_lists;
15825
  bool saved_in_function_body;
15826
  bool saved_in_unbraced_linkage_specification_p;
15827
  tree old_scope = NULL_TREE;
15828
  tree scope = NULL_TREE;
15829
  tree bases;
15830
 
15831
  push_deferring_access_checks (dk_no_deferred);
15832
 
15833
  /* Parse the class-head.  */
15834
  type = cp_parser_class_head (parser,
15835
                               &nested_name_specifier_p,
15836
                               &attributes,
15837
                               &bases);
15838
  /* If the class-head was a semantic disaster, skip the entire body
15839
     of the class.  */
15840
  if (!type)
15841
    {
15842
      cp_parser_skip_to_end_of_block_or_statement (parser);
15843
      pop_deferring_access_checks ();
15844
      return error_mark_node;
15845
    }
15846
 
15847
  /* Look for the `{'.  */
15848
  if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15849
    {
15850
      pop_deferring_access_checks ();
15851
      return error_mark_node;
15852
    }
15853
 
15854
  /* Process the base classes. If they're invalid, skip the
15855
     entire class body.  */
15856
  if (!xref_basetypes (type, bases))
15857
    {
15858
      /* Consuming the closing brace yields better error messages
15859
         later on.  */
15860
      if (cp_parser_skip_to_closing_brace (parser))
15861
        cp_lexer_consume_token (parser->lexer);
15862
      pop_deferring_access_checks ();
15863
      return error_mark_node;
15864
    }
15865
 
15866
  /* Issue an error message if type-definitions are forbidden here.  */
15867
  cp_parser_check_type_definition (parser);
15868
  /* Remember that we are defining one more class.  */
15869
  ++parser->num_classes_being_defined;
15870
  /* Inside the class, surrounding template-parameter-lists do not
15871
     apply.  */
15872
  saved_num_template_parameter_lists
15873
    = parser->num_template_parameter_lists;
15874
  parser->num_template_parameter_lists = 0;
15875
  /* We are not in a function body.  */
15876
  saved_in_function_body = parser->in_function_body;
15877
  parser->in_function_body = false;
15878
  /* We are not immediately inside an extern "lang" block.  */
15879
  saved_in_unbraced_linkage_specification_p
15880
    = parser->in_unbraced_linkage_specification_p;
15881
  parser->in_unbraced_linkage_specification_p = false;
15882
 
15883
  /* Start the class.  */
15884
  if (nested_name_specifier_p)
15885
    {
15886
      scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15887
      old_scope = push_inner_scope (scope);
15888
    }
15889
  type = begin_class_definition (type, attributes);
15890
 
15891
  if (type == error_mark_node)
15892
    /* If the type is erroneous, skip the entire body of the class.  */
15893
    cp_parser_skip_to_closing_brace (parser);
15894
  else
15895
    /* Parse the member-specification.  */
15896
    cp_parser_member_specification_opt (parser);
15897
 
15898
  /* Look for the trailing `}'.  */
15899
  cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15900
  /* Look for trailing attributes to apply to this class.  */
15901
  if (cp_parser_allow_gnu_extensions_p (parser))
15902
    attributes = cp_parser_attributes_opt (parser);
15903
  if (type != error_mark_node)
15904
    type = finish_struct (type, attributes);
15905
  if (nested_name_specifier_p)
15906
    pop_inner_scope (old_scope, scope);
15907
  /* If this class is not itself within the scope of another class,
15908
     then we need to parse the bodies of all of the queued function
15909
     definitions.  Note that the queued functions defined in a class
15910
     are not always processed immediately following the
15911
     class-specifier for that class.  Consider:
15912
 
15913
       struct A {
15914
         struct B { void f() { sizeof (A); } };
15915
       };
15916
 
15917
     If `f' were processed before the processing of `A' were
15918
     completed, there would be no way to compute the size of `A'.
15919
     Note that the nesting we are interested in here is lexical --
15920
     not the semantic nesting given by TYPE_CONTEXT.  In particular,
15921
     for:
15922
 
15923
       struct A { struct B; };
15924
       struct A::B { void f() { } };
15925
 
15926
     there is no need to delay the parsing of `A::B::f'.  */
15927
  if (--parser->num_classes_being_defined == 0)
15928
    {
15929
      tree queue_entry;
15930
      tree fn;
15931
      tree class_type = NULL_TREE;
15932
      tree pushed_scope = NULL_TREE;
15933
 
15934
      /* In a first pass, parse default arguments to the functions.
15935
         Then, in a second pass, parse the bodies of the functions.
15936
         This two-phased approach handles cases like:
15937
 
15938
            struct S {
15939
              void f() { g(); }
15940
              void g(int i = 3);
15941
            };
15942
 
15943
         */
15944
      for (TREE_PURPOSE (parser->unparsed_functions_queues)
15945
             = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15946
           (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15947
           TREE_PURPOSE (parser->unparsed_functions_queues)
15948
             = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15949
        {
15950
          fn = TREE_VALUE (queue_entry);
15951
          /* If there are default arguments that have not yet been processed,
15952
             take care of them now.  */
15953
          if (class_type != TREE_PURPOSE (queue_entry))
15954
            {
15955
              if (pushed_scope)
15956
                pop_scope (pushed_scope);
15957
              class_type = TREE_PURPOSE (queue_entry);
15958
              pushed_scope = push_scope (class_type);
15959
            }
15960
          /* Make sure that any template parameters are in scope.  */
15961
          maybe_begin_member_template_processing (fn);
15962
          /* Parse the default argument expressions.  */
15963
          cp_parser_late_parsing_default_args (parser, fn);
15964
          /* Remove any template parameters from the symbol table.  */
15965
          maybe_end_member_template_processing ();
15966
        }
15967
      if (pushed_scope)
15968
        pop_scope (pushed_scope);
15969
      /* Now parse the body of the functions.  */
15970
      for (TREE_VALUE (parser->unparsed_functions_queues)
15971
             = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15972
           (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15973
           TREE_VALUE (parser->unparsed_functions_queues)
15974
             = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15975
        {
15976
          /* Figure out which function we need to process.  */
15977
          fn = TREE_VALUE (queue_entry);
15978
          /* Parse the function.  */
15979
          cp_parser_late_parsing_for_member (parser, fn);
15980
        }
15981
    }
15982
 
15983
  /* Put back any saved access checks.  */
15984
  pop_deferring_access_checks ();
15985
 
15986
  /* Restore saved state.  */
15987
  parser->in_function_body = saved_in_function_body;
15988
  parser->num_template_parameter_lists
15989
    = saved_num_template_parameter_lists;
15990
  parser->in_unbraced_linkage_specification_p
15991
    = saved_in_unbraced_linkage_specification_p;
15992
 
15993
  return type;
15994
}
15995
 
15996
/* Parse a class-head.
15997
 
15998
   class-head:
15999
     class-key identifier [opt] base-clause [opt]
16000
     class-key nested-name-specifier identifier base-clause [opt]
16001
     class-key nested-name-specifier [opt] template-id
16002
       base-clause [opt]
16003
 
16004
   GNU Extensions:
16005
     class-key attributes identifier [opt] base-clause [opt]
16006
     class-key attributes nested-name-specifier identifier base-clause [opt]
16007
     class-key attributes nested-name-specifier [opt] template-id
16008
       base-clause [opt]
16009
 
16010
   Upon return BASES is initialized to the list of base classes (or
16011
   NULL, if there are none) in the same form returned by
16012
   cp_parser_base_clause.
16013
 
16014
   Returns the TYPE of the indicated class.  Sets
16015
   *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16016
   involving a nested-name-specifier was used, and FALSE otherwise.
16017
 
16018
   Returns error_mark_node if this is not a class-head.
16019
 
16020
   Returns NULL_TREE if the class-head is syntactically valid, but
16021
   semantically invalid in a way that means we should skip the entire
16022
   body of the class.  */
16023
 
16024
static tree
16025
cp_parser_class_head (cp_parser* parser,
16026
                      bool* nested_name_specifier_p,
16027
                      tree *attributes_p,
16028
                      tree *bases)
16029
{
16030
  tree nested_name_specifier;
16031
  enum tag_types class_key;
16032
  tree id = NULL_TREE;
16033
  tree type = NULL_TREE;
16034
  tree attributes;
16035
  bool template_id_p = false;
16036
  bool qualified_p = false;
16037
  bool invalid_nested_name_p = false;
16038
  bool invalid_explicit_specialization_p = false;
16039
  tree pushed_scope = NULL_TREE;
16040
  unsigned num_templates;
16041
  cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16042
  /* Assume no nested-name-specifier will be present.  */
16043
  *nested_name_specifier_p = false;
16044
  /* Assume no template parameter lists will be used in defining the
16045
     type.  */
16046
  num_templates = 0;
16047
 
16048
  *bases = NULL_TREE;
16049
 
16050
  /* Look for the class-key.  */
16051
  class_key = cp_parser_class_key (parser);
16052
  if (class_key == none_type)
16053
    return error_mark_node;
16054
 
16055
  /* Parse the attributes.  */
16056
  attributes = cp_parser_attributes_opt (parser);
16057
 
16058
  /* If the next token is `::', that is invalid -- but sometimes
16059
     people do try to write:
16060
 
16061
       struct ::S {};
16062
 
16063
     Handle this gracefully by accepting the extra qualifier, and then
16064
     issuing an error about it later if this really is a
16065
     class-head.  If it turns out just to be an elaborated type
16066
     specifier, remain silent.  */
16067
  if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16068
    qualified_p = true;
16069
 
16070
  push_deferring_access_checks (dk_no_check);
16071
 
16072
  /* Determine the name of the class.  Begin by looking for an
16073
     optional nested-name-specifier.  */
16074
  nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16075
  nested_name_specifier
16076
    = cp_parser_nested_name_specifier_opt (parser,
16077
                                           /*typename_keyword_p=*/false,
16078
                                           /*check_dependency_p=*/false,
16079
                                           /*type_p=*/false,
16080
                                           /*is_declaration=*/false);
16081
  /* If there was a nested-name-specifier, then there *must* be an
16082
     identifier.  */
16083
  if (nested_name_specifier)
16084
    {
16085
      type_start_token = cp_lexer_peek_token (parser->lexer);
16086
      /* Although the grammar says `identifier', it really means
16087
         `class-name' or `template-name'.  You are only allowed to
16088
         define a class that has already been declared with this
16089
         syntax.
16090
 
16091
         The proposed resolution for Core Issue 180 says that wherever
16092
         you see `class T::X' you should treat `X' as a type-name.
16093
 
16094
         It is OK to define an inaccessible class; for example:
16095
 
16096
           class A { class B; };
16097
           class A::B {};
16098
 
16099
         We do not know if we will see a class-name, or a
16100
         template-name.  We look for a class-name first, in case the
16101
         class-name is a template-id; if we looked for the
16102
         template-name first we would stop after the template-name.  */
16103
      cp_parser_parse_tentatively (parser);
16104
      type = cp_parser_class_name (parser,
16105
                                   /*typename_keyword_p=*/false,
16106
                                   /*template_keyword_p=*/false,
16107
                                   class_type,
16108
                                   /*check_dependency_p=*/false,
16109
                                   /*class_head_p=*/true,
16110
                                   /*is_declaration=*/false);
16111
      /* If that didn't work, ignore the nested-name-specifier.  */
16112
      if (!cp_parser_parse_definitely (parser))
16113
        {
16114
          invalid_nested_name_p = true;
16115
          type_start_token = cp_lexer_peek_token (parser->lexer);
16116
          id = cp_parser_identifier (parser);
16117
          if (id == error_mark_node)
16118
            id = NULL_TREE;
16119
        }
16120
      /* If we could not find a corresponding TYPE, treat this
16121
         declaration like an unqualified declaration.  */
16122
      if (type == error_mark_node)
16123
        nested_name_specifier = NULL_TREE;
16124
      /* Otherwise, count the number of templates used in TYPE and its
16125
         containing scopes.  */
16126
      else
16127
        {
16128
          tree scope;
16129
 
16130
          for (scope = TREE_TYPE (type);
16131
               scope && TREE_CODE (scope) != NAMESPACE_DECL;
16132
               scope = (TYPE_P (scope)
16133
                        ? TYPE_CONTEXT (scope)
16134
                        : DECL_CONTEXT (scope)))
16135
            if (TYPE_P (scope)
16136
                && CLASS_TYPE_P (scope)
16137
                && CLASSTYPE_TEMPLATE_INFO (scope)
16138
                && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16139
                && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16140
              ++num_templates;
16141
        }
16142
    }
16143
  /* Otherwise, the identifier is optional.  */
16144
  else
16145
    {
16146
      /* We don't know whether what comes next is a template-id,
16147
         an identifier, or nothing at all.  */
16148
      cp_parser_parse_tentatively (parser);
16149
      /* Check for a template-id.  */
16150
      type_start_token = cp_lexer_peek_token (parser->lexer);
16151
      id = cp_parser_template_id (parser,
16152
                                  /*template_keyword_p=*/false,
16153
                                  /*check_dependency_p=*/true,
16154
                                  /*is_declaration=*/true);
16155
      /* If that didn't work, it could still be an identifier.  */
16156
      if (!cp_parser_parse_definitely (parser))
16157
        {
16158
          if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16159
            {
16160
              type_start_token = cp_lexer_peek_token (parser->lexer);
16161
              id = cp_parser_identifier (parser);
16162
            }
16163
          else
16164
            id = NULL_TREE;
16165
        }
16166
      else
16167
        {
16168
          template_id_p = true;
16169
          ++num_templates;
16170
        }
16171
    }
16172
 
16173
  pop_deferring_access_checks ();
16174
 
16175
  if (id)
16176
    cp_parser_check_for_invalid_template_id (parser, id,
16177
                                             type_start_token->location);
16178
 
16179
  /* If it's not a `:' or a `{' then we can't really be looking at a
16180
     class-head, since a class-head only appears as part of a
16181
     class-specifier.  We have to detect this situation before calling
16182
     xref_tag, since that has irreversible side-effects.  */
16183
  if (!cp_parser_next_token_starts_class_definition_p (parser))
16184
    {
16185
      cp_parser_error (parser, "expected %<{%> or %<:%>");
16186
      return error_mark_node;
16187
    }
16188
 
16189
  /* At this point, we're going ahead with the class-specifier, even
16190
     if some other problem occurs.  */
16191
  cp_parser_commit_to_tentative_parse (parser);
16192
  /* Issue the error about the overly-qualified name now.  */
16193
  if (qualified_p)
16194
    {
16195
      cp_parser_error (parser,
16196
                       "global qualification of class name is invalid");
16197
      return error_mark_node;
16198
    }
16199
  else if (invalid_nested_name_p)
16200
    {
16201
      cp_parser_error (parser,
16202
                       "qualified name does not name a class");
16203
      return error_mark_node;
16204
    }
16205
  else if (nested_name_specifier)
16206
    {
16207
      tree scope;
16208
 
16209
      /* Reject typedef-names in class heads.  */
16210
      if (!DECL_IMPLICIT_TYPEDEF_P (type))
16211
        {
16212
          error_at (type_start_token->location,
16213
                    "invalid class name in declaration of %qD",
16214
                    type);
16215
          type = NULL_TREE;
16216
          goto done;
16217
        }
16218
 
16219
      /* Figure out in what scope the declaration is being placed.  */
16220
      scope = current_scope ();
16221
      /* If that scope does not contain the scope in which the
16222
         class was originally declared, the program is invalid.  */
16223
      if (scope && !is_ancestor (scope, nested_name_specifier))
16224
        {
16225
          if (at_namespace_scope_p ())
16226
            error_at (type_start_token->location,
16227
                      "declaration of %qD in namespace %qD which does not "
16228
                      "enclose %qD",
16229
                      type, scope, nested_name_specifier);
16230
          else
16231
            error_at (type_start_token->location,
16232
                      "declaration of %qD in %qD which does not enclose %qD",
16233
                      type, scope, nested_name_specifier);
16234
          type = NULL_TREE;
16235
          goto done;
16236
        }
16237
      /* [dcl.meaning]
16238
 
16239
         A declarator-id shall not be qualified except for the
16240
         definition of a ... nested class outside of its class
16241
         ... [or] the definition or explicit instantiation of a
16242
         class member of a namespace outside of its namespace.  */
16243
      if (scope == nested_name_specifier)
16244
        {
16245
          permerror (nested_name_specifier_token_start->location,
16246
                     "extra qualification not allowed");
16247
          nested_name_specifier = NULL_TREE;
16248
          num_templates = 0;
16249
        }
16250
    }
16251
  /* An explicit-specialization must be preceded by "template <>".  If
16252
     it is not, try to recover gracefully.  */
16253
  if (at_namespace_scope_p ()
16254
      && parser->num_template_parameter_lists == 0
16255
      && template_id_p)
16256
    {
16257
      error_at (type_start_token->location,
16258
                "an explicit specialization must be preceded by %<template <>%>");
16259
      invalid_explicit_specialization_p = true;
16260
      /* Take the same action that would have been taken by
16261
         cp_parser_explicit_specialization.  */
16262
      ++parser->num_template_parameter_lists;
16263
      begin_specialization ();
16264
    }
16265
  /* There must be no "return" statements between this point and the
16266
     end of this function; set "type "to the correct return value and
16267
     use "goto done;" to return.  */
16268
  /* Make sure that the right number of template parameters were
16269
     present.  */
16270
  if (!cp_parser_check_template_parameters (parser, num_templates,
16271
                                            type_start_token->location,
16272
                                            /*declarator=*/NULL))
16273
    {
16274
      /* If something went wrong, there is no point in even trying to
16275
         process the class-definition.  */
16276
      type = NULL_TREE;
16277
      goto done;
16278
    }
16279
 
16280
  /* Look up the type.  */
16281
  if (template_id_p)
16282
    {
16283
      if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16284
          && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16285
              || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16286
        {
16287
          error_at (type_start_token->location,
16288
                    "function template %qD redeclared as a class template", id);
16289
          type = error_mark_node;
16290
        }
16291
      else
16292
        {
16293
          type = TREE_TYPE (id);
16294
          type = maybe_process_partial_specialization (type);
16295
        }
16296
      if (nested_name_specifier)
16297
        pushed_scope = push_scope (nested_name_specifier);
16298
    }
16299
  else if (nested_name_specifier)
16300
    {
16301
      tree class_type;
16302
 
16303
      /* Given:
16304
 
16305
            template <typename T> struct S { struct T };
16306
            template <typename T> struct S<T>::T { };
16307
 
16308
         we will get a TYPENAME_TYPE when processing the definition of
16309
         `S::T'.  We need to resolve it to the actual type before we
16310
         try to define it.  */
16311
      if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16312
        {
16313
          class_type = resolve_typename_type (TREE_TYPE (type),
16314
                                              /*only_current_p=*/false);
16315
          if (TREE_CODE (class_type) != TYPENAME_TYPE)
16316
            type = TYPE_NAME (class_type);
16317
          else
16318
            {
16319
              cp_parser_error (parser, "could not resolve typename type");
16320
              type = error_mark_node;
16321
            }
16322
        }
16323
 
16324
      if (maybe_process_partial_specialization (TREE_TYPE (type))
16325
          == error_mark_node)
16326
        {
16327
          type = NULL_TREE;
16328
          goto done;
16329
        }
16330
 
16331
      class_type = current_class_type;
16332
      /* Enter the scope indicated by the nested-name-specifier.  */
16333
      pushed_scope = push_scope (nested_name_specifier);
16334
      /* Get the canonical version of this type.  */
16335
      type = TYPE_MAIN_DECL (TREE_TYPE (type));
16336
      if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16337
          && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16338
        {
16339
          type = push_template_decl (type);
16340
          if (type == error_mark_node)
16341
            {
16342
              type = NULL_TREE;
16343
              goto done;
16344
            }
16345
        }
16346
 
16347
      type = TREE_TYPE (type);
16348
      *nested_name_specifier_p = true;
16349
    }
16350
  else      /* The name is not a nested name.  */
16351
    {
16352
      /* If the class was unnamed, create a dummy name.  */
16353
      if (!id)
16354
        id = make_anon_name ();
16355
      type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16356
                       parser->num_template_parameter_lists);
16357
    }
16358
 
16359
  /* Indicate whether this class was declared as a `class' or as a
16360
     `struct'.  */
16361
  if (TREE_CODE (type) == RECORD_TYPE)
16362
    CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16363
  cp_parser_check_class_key (class_key, type);
16364
 
16365
  /* If this type was already complete, and we see another definition,
16366
     that's an error.  */
16367
  if (type != error_mark_node && COMPLETE_TYPE_P (type))
16368
    {
16369
      error_at (type_start_token->location, "redefinition of %q#T",
16370
                type);
16371
      error_at (type_start_token->location, "previous definition of %q+#T",
16372
                type);
16373
      type = NULL_TREE;
16374
      goto done;
16375
    }
16376
  else if (type == error_mark_node)
16377
    type = NULL_TREE;
16378
 
16379
  /* We will have entered the scope containing the class; the names of
16380
     base classes should be looked up in that context.  For example:
16381
 
16382
       struct A { struct B {}; struct C; };
16383
       struct A::C : B {};
16384
 
16385
     is valid.  */
16386
 
16387
  /* Get the list of base-classes, if there is one.  */
16388
  if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16389
    *bases = cp_parser_base_clause (parser);
16390
 
16391
 done:
16392
  /* Leave the scope given by the nested-name-specifier.  We will
16393
     enter the class scope itself while processing the members.  */
16394
  if (pushed_scope)
16395
    pop_scope (pushed_scope);
16396
 
16397
  if (invalid_explicit_specialization_p)
16398
    {
16399
      end_specialization ();
16400
      --parser->num_template_parameter_lists;
16401
    }
16402
  *attributes_p = attributes;
16403
  return type;
16404
}
16405
 
16406
/* Parse a class-key.
16407
 
16408
   class-key:
16409
     class
16410
     struct
16411
     union
16412
 
16413
   Returns the kind of class-key specified, or none_type to indicate
16414
   error.  */
16415
 
16416
static enum tag_types
16417
cp_parser_class_key (cp_parser* parser)
16418
{
16419
  cp_token *token;
16420
  enum tag_types tag_type;
16421
 
16422
  /* Look for the class-key.  */
16423
  token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16424
  if (!token)
16425
    return none_type;
16426
 
16427
  /* Check to see if the TOKEN is a class-key.  */
16428
  tag_type = cp_parser_token_is_class_key (token);
16429
  if (!tag_type)
16430
    cp_parser_error (parser, "expected class-key");
16431
  return tag_type;
16432
}
16433
 
16434
/* Parse an (optional) member-specification.
16435
 
16436
   member-specification:
16437
     member-declaration member-specification [opt]
16438
     access-specifier : member-specification [opt]  */
16439
 
16440
static void
16441
cp_parser_member_specification_opt (cp_parser* parser)
16442
{
16443
  while (true)
16444
    {
16445
      cp_token *token;
16446
      enum rid keyword;
16447
 
16448
      /* Peek at the next token.  */
16449
      token = cp_lexer_peek_token (parser->lexer);
16450
      /* If it's a `}', or EOF then we've seen all the members.  */
16451
      if (token->type == CPP_CLOSE_BRACE
16452
          || token->type == CPP_EOF
16453
          || token->type == CPP_PRAGMA_EOL)
16454
        break;
16455
 
16456
      /* See if this token is a keyword.  */
16457
      keyword = token->keyword;
16458
      switch (keyword)
16459
        {
16460
        case RID_PUBLIC:
16461
        case RID_PROTECTED:
16462
        case RID_PRIVATE:
16463
          /* Consume the access-specifier.  */
16464
          cp_lexer_consume_token (parser->lexer);
16465
          /* Remember which access-specifier is active.  */
16466
          current_access_specifier = token->u.value;
16467
          /* Look for the `:'.  */
16468
          cp_parser_require (parser, CPP_COLON, "%<:%>");
16469
          break;
16470
 
16471
        default:
16472
          /* Accept #pragmas at class scope.  */
16473
          if (token->type == CPP_PRAGMA)
16474
            {
16475
              cp_parser_pragma (parser, pragma_external);
16476
              break;
16477
            }
16478
 
16479
          /* Otherwise, the next construction must be a
16480
             member-declaration.  */
16481
          cp_parser_member_declaration (parser);
16482
        }
16483
    }
16484
}
16485
 
16486
/* Parse a member-declaration.
16487
 
16488
   member-declaration:
16489
     decl-specifier-seq [opt] member-declarator-list [opt] ;
16490
     function-definition ; [opt]
16491
     :: [opt] nested-name-specifier template [opt] unqualified-id ;
16492
     using-declaration
16493
     template-declaration
16494
 
16495
   member-declarator-list:
16496
     member-declarator
16497
     member-declarator-list , member-declarator
16498
 
16499
   member-declarator:
16500
     declarator pure-specifier [opt]
16501
     declarator constant-initializer [opt]
16502
     identifier [opt] : constant-expression
16503
 
16504
   GNU Extensions:
16505
 
16506
   member-declaration:
16507
     __extension__ member-declaration
16508
 
16509
   member-declarator:
16510
     declarator attributes [opt] pure-specifier [opt]
16511
     declarator attributes [opt] constant-initializer [opt]
16512
     identifier [opt] attributes [opt] : constant-expression
16513
 
16514
   C++0x Extensions:
16515
 
16516
   member-declaration:
16517
     static_assert-declaration  */
16518
 
16519
static void
16520
cp_parser_member_declaration (cp_parser* parser)
16521
{
16522
  cp_decl_specifier_seq decl_specifiers;
16523
  tree prefix_attributes;
16524
  tree decl;
16525
  int declares_class_or_enum;
16526
  bool friend_p;
16527
  cp_token *token = NULL;
16528
  cp_token *decl_spec_token_start = NULL;
16529
  cp_token *initializer_token_start = NULL;
16530
  int saved_pedantic;
16531
 
16532
  /* Check for the `__extension__' keyword.  */
16533
  if (cp_parser_extension_opt (parser, &saved_pedantic))
16534
    {
16535
      /* Recurse.  */
16536
      cp_parser_member_declaration (parser);
16537
      /* Restore the old value of the PEDANTIC flag.  */
16538
      pedantic = saved_pedantic;
16539
 
16540
      return;
16541
    }
16542
 
16543
  /* Check for a template-declaration.  */
16544
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16545
    {
16546
      /* An explicit specialization here is an error condition, and we
16547
         expect the specialization handler to detect and report this.  */
16548
      if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16549
          && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16550
        cp_parser_explicit_specialization (parser);
16551
      else
16552
        cp_parser_template_declaration (parser, /*member_p=*/true);
16553
 
16554
      return;
16555
    }
16556
 
16557
  /* Check for a using-declaration.  */
16558
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16559
    {
16560
      /* Parse the using-declaration.  */
16561
      cp_parser_using_declaration (parser,
16562
                                   /*access_declaration_p=*/false);
16563
      return;
16564
    }
16565
 
16566
  /* Check for @defs.  */
16567
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16568
    {
16569
      tree ivar, member;
16570
      tree ivar_chains = cp_parser_objc_defs_expression (parser);
16571
      ivar = ivar_chains;
16572
      while (ivar)
16573
        {
16574
          member = ivar;
16575
          ivar = TREE_CHAIN (member);
16576
          TREE_CHAIN (member) = NULL_TREE;
16577
          finish_member_declaration (member);
16578
        }
16579
      return;
16580
    }
16581
 
16582
  /* If the next token is `static_assert' we have a static assertion.  */
16583
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16584
    {
16585
      cp_parser_static_assert (parser, /*member_p=*/true);
16586
      return;
16587
    }
16588
 
16589
  if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16590
    return;
16591
 
16592
  /* Parse the decl-specifier-seq.  */
16593
  decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16594
  cp_parser_decl_specifier_seq (parser,
16595
                                CP_PARSER_FLAGS_OPTIONAL,
16596
                                &decl_specifiers,
16597
                                &declares_class_or_enum);
16598
  prefix_attributes = decl_specifiers.attributes;
16599
  decl_specifiers.attributes = NULL_TREE;
16600
  /* Check for an invalid type-name.  */
16601
  if (!decl_specifiers.any_type_specifiers_p
16602
      && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16603
    return;
16604
  /* If there is no declarator, then the decl-specifier-seq should
16605
     specify a type.  */
16606
  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16607
    {
16608
      /* If there was no decl-specifier-seq, and the next token is a
16609
         `;', then we have something like:
16610
 
16611
           struct S { ; };
16612
 
16613
         [class.mem]
16614
 
16615
         Each member-declaration shall declare at least one member
16616
         name of the class.  */
16617
      if (!decl_specifiers.any_specifiers_p)
16618
        {
16619
          cp_token *token = cp_lexer_peek_token (parser->lexer);
16620
          if (!in_system_header_at (token->location))
16621
            pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16622
        }
16623
      else
16624
        {
16625
          tree type;
16626
 
16627
          /* See if this declaration is a friend.  */
16628
          friend_p = cp_parser_friend_p (&decl_specifiers);
16629
          /* If there were decl-specifiers, check to see if there was
16630
             a class-declaration.  */
16631
          type = check_tag_decl (&decl_specifiers);
16632
          /* Nested classes have already been added to the class, but
16633
             a `friend' needs to be explicitly registered.  */
16634
          if (friend_p)
16635
            {
16636
              /* If the `friend' keyword was present, the friend must
16637
                 be introduced with a class-key.  */
16638
               if (!declares_class_or_enum)
16639
                 error_at (decl_spec_token_start->location,
16640
                           "a class-key must be used when declaring a friend");
16641
               /* In this case:
16642
 
16643
                    template <typename T> struct A {
16644
                      friend struct A<T>::B;
16645
                    };
16646
 
16647
                  A<T>::B will be represented by a TYPENAME_TYPE, and
16648
                  therefore not recognized by check_tag_decl.  */
16649
               if (!type
16650
                   && decl_specifiers.type
16651
                   && TYPE_P (decl_specifiers.type))
16652
                 type = decl_specifiers.type;
16653
               if (!type || !TYPE_P (type))
16654
                 error_at (decl_spec_token_start->location,
16655
                           "friend declaration does not name a class or "
16656
                           "function");
16657
               else
16658
                 make_friend_class (current_class_type, type,
16659
                                    /*complain=*/true);
16660
            }
16661
          /* If there is no TYPE, an error message will already have
16662
             been issued.  */
16663
          else if (!type || type == error_mark_node)
16664
            ;
16665
          /* An anonymous aggregate has to be handled specially; such
16666
             a declaration really declares a data member (with a
16667
             particular type), as opposed to a nested class.  */
16668
          else if (ANON_AGGR_TYPE_P (type))
16669
            {
16670
              /* Remove constructors and such from TYPE, now that we
16671
                 know it is an anonymous aggregate.  */
16672
              fixup_anonymous_aggr (type);
16673
              /* And make the corresponding data member.  */
16674
              decl = build_decl (decl_spec_token_start->location,
16675
                                 FIELD_DECL, NULL_TREE, type);
16676
              /* Add it to the class.  */
16677
              finish_member_declaration (decl);
16678
            }
16679
          else
16680
            cp_parser_check_access_in_redeclaration
16681
                                              (TYPE_NAME (type),
16682
                                               decl_spec_token_start->location);
16683
        }
16684
    }
16685
  else
16686
    {
16687
      /* See if these declarations will be friends.  */
16688
      friend_p = cp_parser_friend_p (&decl_specifiers);
16689
 
16690
      /* Keep going until we hit the `;' at the end of the
16691
         declaration.  */
16692
      while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16693
        {
16694
          tree attributes = NULL_TREE;
16695
          tree first_attribute;
16696
 
16697
          /* Peek at the next token.  */
16698
          token = cp_lexer_peek_token (parser->lexer);
16699
 
16700
          /* Check for a bitfield declaration.  */
16701
          if (token->type == CPP_COLON
16702
              || (token->type == CPP_NAME
16703
                  && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16704
                  == CPP_COLON))
16705
            {
16706
              tree identifier;
16707
              tree width;
16708
 
16709
              /* Get the name of the bitfield.  Note that we cannot just
16710
                 check TOKEN here because it may have been invalidated by
16711
                 the call to cp_lexer_peek_nth_token above.  */
16712
              if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16713
                identifier = cp_parser_identifier (parser);
16714
              else
16715
                identifier = NULL_TREE;
16716
 
16717
              /* Consume the `:' token.  */
16718
              cp_lexer_consume_token (parser->lexer);
16719
              /* Get the width of the bitfield.  */
16720
              width
16721
                = cp_parser_constant_expression (parser,
16722
                                                 /*allow_non_constant=*/false,
16723
                                                 NULL);
16724
 
16725
              /* Look for attributes that apply to the bitfield.  */
16726
              attributes = cp_parser_attributes_opt (parser);
16727
              /* Remember which attributes are prefix attributes and
16728
                 which are not.  */
16729
              first_attribute = attributes;
16730
              /* Combine the attributes.  */
16731
              attributes = chainon (prefix_attributes, attributes);
16732
 
16733
              /* Create the bitfield declaration.  */
16734
              decl = grokbitfield (identifier
16735
                                   ? make_id_declarator (NULL_TREE,
16736
                                                         identifier,
16737
                                                         sfk_none)
16738
                                   : NULL,
16739
                                   &decl_specifiers,
16740
                                   width,
16741
                                   attributes);
16742
            }
16743
          else
16744
            {
16745
              cp_declarator *declarator;
16746
              tree initializer;
16747
              tree asm_specification;
16748
              int ctor_dtor_or_conv_p;
16749
 
16750
              /* Parse the declarator.  */
16751
              declarator
16752
                = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16753
                                        &ctor_dtor_or_conv_p,
16754
                                        /*parenthesized_p=*/NULL,
16755
                                        /*member_p=*/true);
16756
 
16757
              /* If something went wrong parsing the declarator, make sure
16758
                 that we at least consume some tokens.  */
16759
              if (declarator == cp_error_declarator)
16760
                {
16761
                  /* Skip to the end of the statement.  */
16762
                  cp_parser_skip_to_end_of_statement (parser);
16763
                  /* If the next token is not a semicolon, that is
16764
                     probably because we just skipped over the body of
16765
                     a function.  So, we consume a semicolon if
16766
                     present, but do not issue an error message if it
16767
                     is not present.  */
16768
                  if (cp_lexer_next_token_is (parser->lexer,
16769
                                              CPP_SEMICOLON))
16770
                    cp_lexer_consume_token (parser->lexer);
16771
                  return;
16772
                }
16773
 
16774
              if (declares_class_or_enum & 2)
16775
                cp_parser_check_for_definition_in_return_type
16776
                                            (declarator, decl_specifiers.type,
16777
                                             decl_specifiers.type_location);
16778
 
16779
              /* Look for an asm-specification.  */
16780
              asm_specification = cp_parser_asm_specification_opt (parser);
16781
              /* Look for attributes that apply to the declaration.  */
16782
              attributes = cp_parser_attributes_opt (parser);
16783
              /* Remember which attributes are prefix attributes and
16784
                 which are not.  */
16785
              first_attribute = attributes;
16786
              /* Combine the attributes.  */
16787
              attributes = chainon (prefix_attributes, attributes);
16788
 
16789
              /* If it's an `=', then we have a constant-initializer or a
16790
                 pure-specifier.  It is not correct to parse the
16791
                 initializer before registering the member declaration
16792
                 since the member declaration should be in scope while
16793
                 its initializer is processed.  However, the rest of the
16794
                 front end does not yet provide an interface that allows
16795
                 us to handle this correctly.  */
16796
              if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16797
                {
16798
                  /* In [class.mem]:
16799
 
16800
                     A pure-specifier shall be used only in the declaration of
16801
                     a virtual function.
16802
 
16803
                     A member-declarator can contain a constant-initializer
16804
                     only if it declares a static member of integral or
16805
                     enumeration type.
16806
 
16807
                     Therefore, if the DECLARATOR is for a function, we look
16808
                     for a pure-specifier; otherwise, we look for a
16809
                     constant-initializer.  When we call `grokfield', it will
16810
                     perform more stringent semantics checks.  */
16811
                  initializer_token_start = cp_lexer_peek_token (parser->lexer);
16812
                  if (function_declarator_p (declarator))
16813
                    initializer = cp_parser_pure_specifier (parser);
16814
                  else
16815
                    /* Parse the initializer.  */
16816
                    initializer = cp_parser_constant_initializer (parser);
16817
                }
16818
              /* Otherwise, there is no initializer.  */
16819
              else
16820
                initializer = NULL_TREE;
16821
 
16822
              /* See if we are probably looking at a function
16823
                 definition.  We are certainly not looking at a
16824
                 member-declarator.  Calling `grokfield' has
16825
                 side-effects, so we must not do it unless we are sure
16826
                 that we are looking at a member-declarator.  */
16827
              if (cp_parser_token_starts_function_definition_p
16828
                  (cp_lexer_peek_token (parser->lexer)))
16829
                {
16830
                  /* The grammar does not allow a pure-specifier to be
16831
                     used when a member function is defined.  (It is
16832
                     possible that this fact is an oversight in the
16833
                     standard, since a pure function may be defined
16834
                     outside of the class-specifier.  */
16835
                  if (initializer)
16836
                    error_at (initializer_token_start->location,
16837
                              "pure-specifier on function-definition");
16838
                  decl = cp_parser_save_member_function_body (parser,
16839
                                                              &decl_specifiers,
16840
                                                              declarator,
16841
                                                              attributes);
16842
                  /* If the member was not a friend, declare it here.  */
16843
                  if (!friend_p)
16844
                    finish_member_declaration (decl);
16845
                  /* Peek at the next token.  */
16846
                  token = cp_lexer_peek_token (parser->lexer);
16847
                  /* If the next token is a semicolon, consume it.  */
16848
                  if (token->type == CPP_SEMICOLON)
16849
                    cp_lexer_consume_token (parser->lexer);
16850
                  return;
16851
                }
16852
              else
16853
                if (declarator->kind == cdk_function)
16854
                  declarator->id_loc = token->location;
16855
                /* Create the declaration.  */
16856
                decl = grokfield (declarator, &decl_specifiers,
16857
                                  initializer, /*init_const_expr_p=*/true,
16858
                                  asm_specification,
16859
                                  attributes);
16860
            }
16861
 
16862
          /* Reset PREFIX_ATTRIBUTES.  */
16863
          while (attributes && TREE_CHAIN (attributes) != first_attribute)
16864
            attributes = TREE_CHAIN (attributes);
16865
          if (attributes)
16866
            TREE_CHAIN (attributes) = NULL_TREE;
16867
 
16868
          /* If there is any qualification still in effect, clear it
16869
             now; we will be starting fresh with the next declarator.  */
16870
          parser->scope = NULL_TREE;
16871
          parser->qualifying_scope = NULL_TREE;
16872
          parser->object_scope = NULL_TREE;
16873
          /* If it's a `,', then there are more declarators.  */
16874
          if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16875
            cp_lexer_consume_token (parser->lexer);
16876
          /* If the next token isn't a `;', then we have a parse error.  */
16877
          else if (cp_lexer_next_token_is_not (parser->lexer,
16878
                                               CPP_SEMICOLON))
16879
            {
16880
              cp_parser_error (parser, "expected %<;%>");
16881
              /* Skip tokens until we find a `;'.  */
16882
              cp_parser_skip_to_end_of_statement (parser);
16883
 
16884
              break;
16885
            }
16886
 
16887
          if (decl)
16888
            {
16889
              /* Add DECL to the list of members.  */
16890
              if (!friend_p)
16891
                finish_member_declaration (decl);
16892
 
16893
              if (TREE_CODE (decl) == FUNCTION_DECL)
16894
                cp_parser_save_default_args (parser, decl);
16895
            }
16896
        }
16897
    }
16898
 
16899
  cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16900
}
16901
 
16902
/* Parse a pure-specifier.
16903
 
16904
   pure-specifier:
16905
     = 0
16906
 
16907
   Returns INTEGER_ZERO_NODE if a pure specifier is found.
16908
   Otherwise, ERROR_MARK_NODE is returned.  */
16909
 
16910
static tree
16911
cp_parser_pure_specifier (cp_parser* parser)
16912
{
16913
  cp_token *token;
16914
 
16915
  /* Look for the `=' token.  */
16916
  if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16917
    return error_mark_node;
16918
  /* Look for the `0' token.  */
16919
  token = cp_lexer_peek_token (parser->lexer);
16920
 
16921
  if (token->type == CPP_EOF
16922
      || token->type == CPP_PRAGMA_EOL)
16923
    return error_mark_node;
16924
 
16925
  cp_lexer_consume_token (parser->lexer);
16926
 
16927
  /* Accept = default or = delete in c++0x mode.  */
16928
  if (token->keyword == RID_DEFAULT
16929
      || token->keyword == RID_DELETE)
16930
    {
16931
      maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
16932
      return token->u.value;
16933
    }
16934
 
16935
  /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16936
  if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16937
    {
16938
      cp_parser_error (parser,
16939
                       "invalid pure specifier (only %<= 0%> is allowed)");
16940
      cp_parser_skip_to_end_of_statement (parser);
16941
      return error_mark_node;
16942
    }
16943
  if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16944
    {
16945
      error_at (token->location, "templates may not be %<virtual%>");
16946
      return error_mark_node;
16947
    }
16948
 
16949
  return integer_zero_node;
16950
}
16951
 
16952
/* Parse a constant-initializer.
16953
 
16954
   constant-initializer:
16955
     = constant-expression
16956
 
16957
   Returns a representation of the constant-expression.  */
16958
 
16959
static tree
16960
cp_parser_constant_initializer (cp_parser* parser)
16961
{
16962
  /* Look for the `=' token.  */
16963
  if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16964
    return error_mark_node;
16965
 
16966
  /* It is invalid to write:
16967
 
16968
       struct S { static const int i = { 7 }; };
16969
 
16970
     */
16971
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16972
    {
16973
      cp_parser_error (parser,
16974
                       "a brace-enclosed initializer is not allowed here");
16975
      /* Consume the opening brace.  */
16976
      cp_lexer_consume_token (parser->lexer);
16977
      /* Skip the initializer.  */
16978
      cp_parser_skip_to_closing_brace (parser);
16979
      /* Look for the trailing `}'.  */
16980
      cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16981
 
16982
      return error_mark_node;
16983
    }
16984
 
16985
  return cp_parser_constant_expression (parser,
16986
                                        /*allow_non_constant=*/false,
16987
                                        NULL);
16988
}
16989
 
16990
/* Derived classes [gram.class.derived] */
16991
 
16992
/* Parse a base-clause.
16993
 
16994
   base-clause:
16995
     : base-specifier-list
16996
 
16997
   base-specifier-list:
16998
     base-specifier ... [opt]
16999
     base-specifier-list , base-specifier ... [opt]
17000
 
17001
   Returns a TREE_LIST representing the base-classes, in the order in
17002
   which they were declared.  The representation of each node is as
17003
   described by cp_parser_base_specifier.
17004
 
17005
   In the case that no bases are specified, this function will return
17006
   NULL_TREE, not ERROR_MARK_NODE.  */
17007
 
17008
static tree
17009
cp_parser_base_clause (cp_parser* parser)
17010
{
17011
  tree bases = NULL_TREE;
17012
 
17013
  /* Look for the `:' that begins the list.  */
17014
  cp_parser_require (parser, CPP_COLON, "%<:%>");
17015
 
17016
  /* Scan the base-specifier-list.  */
17017
  while (true)
17018
    {
17019
      cp_token *token;
17020
      tree base;
17021
      bool pack_expansion_p = false;
17022
 
17023
      /* Look for the base-specifier.  */
17024
      base = cp_parser_base_specifier (parser);
17025
      /* Look for the (optional) ellipsis. */
17026
      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17027
        {
17028
          /* Consume the `...'. */
17029
          cp_lexer_consume_token (parser->lexer);
17030
 
17031
          pack_expansion_p = true;
17032
        }
17033
 
17034
      /* Add BASE to the front of the list.  */
17035
      if (base != error_mark_node)
17036
        {
17037
          if (pack_expansion_p)
17038
            /* Make this a pack expansion type. */
17039
            TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17040
 
17041
 
17042
          if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17043
            {
17044
              TREE_CHAIN (base) = bases;
17045
              bases = base;
17046
            }
17047
        }
17048
      /* Peek at the next token.  */
17049
      token = cp_lexer_peek_token (parser->lexer);
17050
      /* If it's not a comma, then the list is complete.  */
17051
      if (token->type != CPP_COMMA)
17052
        break;
17053
      /* Consume the `,'.  */
17054
      cp_lexer_consume_token (parser->lexer);
17055
    }
17056
 
17057
  /* PARSER->SCOPE may still be non-NULL at this point, if the last
17058
     base class had a qualified name.  However, the next name that
17059
     appears is certainly not qualified.  */
17060
  parser->scope = NULL_TREE;
17061
  parser->qualifying_scope = NULL_TREE;
17062
  parser->object_scope = NULL_TREE;
17063
 
17064
  return nreverse (bases);
17065
}
17066
 
17067
/* Parse a base-specifier.
17068
 
17069
   base-specifier:
17070
     :: [opt] nested-name-specifier [opt] class-name
17071
     virtual access-specifier [opt] :: [opt] nested-name-specifier
17072
       [opt] class-name
17073
     access-specifier virtual [opt] :: [opt] nested-name-specifier
17074
       [opt] class-name
17075
 
17076
   Returns a TREE_LIST.  The TREE_PURPOSE will be one of
17077
   ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17078
   indicate the specifiers provided.  The TREE_VALUE will be a TYPE
17079
   (or the ERROR_MARK_NODE) indicating the type that was specified.  */
17080
 
17081
static tree
17082
cp_parser_base_specifier (cp_parser* parser)
17083
{
17084
  cp_token *token;
17085
  bool done = false;
17086
  bool virtual_p = false;
17087
  bool duplicate_virtual_error_issued_p = false;
17088
  bool duplicate_access_error_issued_p = false;
17089
  bool class_scope_p, template_p;
17090
  tree access = access_default_node;
17091
  tree type;
17092
 
17093
  /* Process the optional `virtual' and `access-specifier'.  */
17094
  while (!done)
17095
    {
17096
      /* Peek at the next token.  */
17097
      token = cp_lexer_peek_token (parser->lexer);
17098
      /* Process `virtual'.  */
17099
      switch (token->keyword)
17100
        {
17101
        case RID_VIRTUAL:
17102
          /* If `virtual' appears more than once, issue an error.  */
17103
          if (virtual_p && !duplicate_virtual_error_issued_p)
17104
            {
17105
              cp_parser_error (parser,
17106
                               "%<virtual%> specified more than once in base-specified");
17107
              duplicate_virtual_error_issued_p = true;
17108
            }
17109
 
17110
          virtual_p = true;
17111
 
17112
          /* Consume the `virtual' token.  */
17113
          cp_lexer_consume_token (parser->lexer);
17114
 
17115
          break;
17116
 
17117
        case RID_PUBLIC:
17118
        case RID_PROTECTED:
17119
        case RID_PRIVATE:
17120
          /* If more than one access specifier appears, issue an
17121
             error.  */
17122
          if (access != access_default_node
17123
              && !duplicate_access_error_issued_p)
17124
            {
17125
              cp_parser_error (parser,
17126
                               "more than one access specifier in base-specified");
17127
              duplicate_access_error_issued_p = true;
17128
            }
17129
 
17130
          access = ridpointers[(int) token->keyword];
17131
 
17132
          /* Consume the access-specifier.  */
17133
          cp_lexer_consume_token (parser->lexer);
17134
 
17135
          break;
17136
 
17137
        default:
17138
          done = true;
17139
          break;
17140
        }
17141
    }
17142
  /* It is not uncommon to see programs mechanically, erroneously, use
17143
     the 'typename' keyword to denote (dependent) qualified types
17144
     as base classes.  */
17145
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17146
    {
17147
      token = cp_lexer_peek_token (parser->lexer);
17148
      if (!processing_template_decl)
17149
        error_at (token->location,
17150
                  "keyword %<typename%> not allowed outside of templates");
17151
      else
17152
        error_at (token->location,
17153
                  "keyword %<typename%> not allowed in this context "
17154
                  "(the base class is implicitly a type)");
17155
      cp_lexer_consume_token (parser->lexer);
17156
    }
17157
 
17158
  /* Look for the optional `::' operator.  */
17159
  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17160
  /* Look for the nested-name-specifier.  The simplest way to
17161
     implement:
17162
 
17163
       [temp.res]
17164
 
17165
       The keyword `typename' is not permitted in a base-specifier or
17166
       mem-initializer; in these contexts a qualified name that
17167
       depends on a template-parameter is implicitly assumed to be a
17168
       type name.
17169
 
17170
     is to pretend that we have seen the `typename' keyword at this
17171
     point.  */
17172
  cp_parser_nested_name_specifier_opt (parser,
17173
                                       /*typename_keyword_p=*/true,
17174
                                       /*check_dependency_p=*/true,
17175
                                       typename_type,
17176
                                       /*is_declaration=*/true);
17177
  /* If the base class is given by a qualified name, assume that names
17178
     we see are type names or templates, as appropriate.  */
17179
  class_scope_p = (parser->scope && TYPE_P (parser->scope));
17180
  template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17181
 
17182
  /* Finally, look for the class-name.  */
17183
  type = cp_parser_class_name (parser,
17184
                               class_scope_p,
17185
                               template_p,
17186
                               typename_type,
17187
                               /*check_dependency_p=*/true,
17188
                               /*class_head_p=*/false,
17189
                               /*is_declaration=*/true);
17190
 
17191
  if (type == error_mark_node)
17192
    return error_mark_node;
17193
 
17194
  return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17195
}
17196
 
17197
/* Exception handling [gram.exception] */
17198
 
17199
/* Parse an (optional) exception-specification.
17200
 
17201
   exception-specification:
17202
     throw ( type-id-list [opt] )
17203
 
17204
   Returns a TREE_LIST representing the exception-specification.  The
17205
   TREE_VALUE of each node is a type.  */
17206
 
17207
static tree
17208
cp_parser_exception_specification_opt (cp_parser* parser)
17209
{
17210
  cp_token *token;
17211
  tree type_id_list;
17212
 
17213
  /* Peek at the next token.  */
17214
  token = cp_lexer_peek_token (parser->lexer);
17215
  /* If it's not `throw', then there's no exception-specification.  */
17216
  if (!cp_parser_is_keyword (token, RID_THROW))
17217
    return NULL_TREE;
17218
 
17219
  /* Consume the `throw'.  */
17220
  cp_lexer_consume_token (parser->lexer);
17221
 
17222
  /* Look for the `('.  */
17223
  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17224
 
17225
  /* Peek at the next token.  */
17226
  token = cp_lexer_peek_token (parser->lexer);
17227
  /* If it's not a `)', then there is a type-id-list.  */
17228
  if (token->type != CPP_CLOSE_PAREN)
17229
    {
17230
      const char *saved_message;
17231
 
17232
      /* Types may not be defined in an exception-specification.  */
17233
      saved_message = parser->type_definition_forbidden_message;
17234
      parser->type_definition_forbidden_message
17235
        = G_("types may not be defined in an exception-specification");
17236
      /* Parse the type-id-list.  */
17237
      type_id_list = cp_parser_type_id_list (parser);
17238
      /* Restore the saved message.  */
17239
      parser->type_definition_forbidden_message = saved_message;
17240
    }
17241
  else
17242
    type_id_list = empty_except_spec;
17243
 
17244
  /* Look for the `)'.  */
17245
  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17246
 
17247
  return type_id_list;
17248
}
17249
 
17250
/* Parse an (optional) type-id-list.
17251
 
17252
   type-id-list:
17253
     type-id ... [opt]
17254
     type-id-list , type-id ... [opt]
17255
 
17256
   Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
17257
   in the order that the types were presented.  */
17258
 
17259
static tree
17260
cp_parser_type_id_list (cp_parser* parser)
17261
{
17262
  tree types = NULL_TREE;
17263
 
17264
  while (true)
17265
    {
17266
      cp_token *token;
17267
      tree type;
17268
 
17269
      /* Get the next type-id.  */
17270
      type = cp_parser_type_id (parser);
17271
      /* Parse the optional ellipsis. */
17272
      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17273
        {
17274
          /* Consume the `...'. */
17275
          cp_lexer_consume_token (parser->lexer);
17276
 
17277
          /* Turn the type into a pack expansion expression. */
17278
          type = make_pack_expansion (type);
17279
        }
17280
      /* Add it to the list.  */
17281
      types = add_exception_specifier (types, type, /*complain=*/1);
17282
      /* Peek at the next token.  */
17283
      token = cp_lexer_peek_token (parser->lexer);
17284
      /* If it is not a `,', we are done.  */
17285
      if (token->type != CPP_COMMA)
17286
        break;
17287
      /* Consume the `,'.  */
17288
      cp_lexer_consume_token (parser->lexer);
17289
    }
17290
 
17291
  return nreverse (types);
17292
}
17293
 
17294
/* Parse a try-block.
17295
 
17296
   try-block:
17297
     try compound-statement handler-seq  */
17298
 
17299
static tree
17300
cp_parser_try_block (cp_parser* parser)
17301
{
17302
  tree try_block;
17303
 
17304
  cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17305
  try_block = begin_try_block ();
17306
  cp_parser_compound_statement (parser, NULL, true);
17307
  finish_try_block (try_block);
17308
  cp_parser_handler_seq (parser);
17309
  finish_handler_sequence (try_block);
17310
 
17311
  return try_block;
17312
}
17313
 
17314
/* Parse a function-try-block.
17315
 
17316
   function-try-block:
17317
     try ctor-initializer [opt] function-body handler-seq  */
17318
 
17319
static bool
17320
cp_parser_function_try_block (cp_parser* parser)
17321
{
17322
  tree compound_stmt;
17323
  tree try_block;
17324
  bool ctor_initializer_p;
17325
 
17326
  /* Look for the `try' keyword.  */
17327
  if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17328
    return false;
17329
  /* Let the rest of the front end know where we are.  */
17330
  try_block = begin_function_try_block (&compound_stmt);
17331
  /* Parse the function-body.  */
17332
  ctor_initializer_p
17333
    = cp_parser_ctor_initializer_opt_and_function_body (parser);
17334
  /* We're done with the `try' part.  */
17335
  finish_function_try_block (try_block);
17336
  /* Parse the handlers.  */
17337
  cp_parser_handler_seq (parser);
17338
  /* We're done with the handlers.  */
17339
  finish_function_handler_sequence (try_block, compound_stmt);
17340
 
17341
  return ctor_initializer_p;
17342
}
17343
 
17344
/* Parse a handler-seq.
17345
 
17346
   handler-seq:
17347
     handler handler-seq [opt]  */
17348
 
17349
static void
17350
cp_parser_handler_seq (cp_parser* parser)
17351
{
17352
  while (true)
17353
    {
17354
      cp_token *token;
17355
 
17356
      /* Parse the handler.  */
17357
      cp_parser_handler (parser);
17358
      /* Peek at the next token.  */
17359
      token = cp_lexer_peek_token (parser->lexer);
17360
      /* If it's not `catch' then there are no more handlers.  */
17361
      if (!cp_parser_is_keyword (token, RID_CATCH))
17362
        break;
17363
    }
17364
}
17365
 
17366
/* Parse a handler.
17367
 
17368
   handler:
17369
     catch ( exception-declaration ) compound-statement  */
17370
 
17371
static void
17372
cp_parser_handler (cp_parser* parser)
17373
{
17374
  tree handler;
17375
  tree declaration;
17376
 
17377
  cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17378
  handler = begin_handler ();
17379
  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17380
  declaration = cp_parser_exception_declaration (parser);
17381
  finish_handler_parms (declaration, handler);
17382
  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17383
  cp_parser_compound_statement (parser, NULL, false);
17384
  finish_handler (handler);
17385
}
17386
 
17387
/* Parse an exception-declaration.
17388
 
17389
   exception-declaration:
17390
     type-specifier-seq declarator
17391
     type-specifier-seq abstract-declarator
17392
     type-specifier-seq
17393
     ...
17394
 
17395
   Returns a VAR_DECL for the declaration, or NULL_TREE if the
17396
   ellipsis variant is used.  */
17397
 
17398
static tree
17399
cp_parser_exception_declaration (cp_parser* parser)
17400
{
17401
  cp_decl_specifier_seq type_specifiers;
17402
  cp_declarator *declarator;
17403
  const char *saved_message;
17404
 
17405
  /* If it's an ellipsis, it's easy to handle.  */
17406
  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17407
    {
17408
      /* Consume the `...' token.  */
17409
      cp_lexer_consume_token (parser->lexer);
17410
      return NULL_TREE;
17411
    }
17412
 
17413
  /* Types may not be defined in exception-declarations.  */
17414
  saved_message = parser->type_definition_forbidden_message;
17415
  parser->type_definition_forbidden_message
17416
    = G_("types may not be defined in exception-declarations");
17417
 
17418
  /* Parse the type-specifier-seq.  */
17419
  cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17420
                                /*is_trailing_return=*/false,
17421
                                &type_specifiers);
17422
  /* If it's a `)', then there is no declarator.  */
17423
  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17424
    declarator = NULL;
17425
  else
17426
    declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17427
                                       /*ctor_dtor_or_conv_p=*/NULL,
17428
                                       /*parenthesized_p=*/NULL,
17429
                                       /*member_p=*/false);
17430
 
17431
  /* Restore the saved message.  */
17432
  parser->type_definition_forbidden_message = saved_message;
17433
 
17434
  if (!type_specifiers.any_specifiers_p)
17435
    return error_mark_node;
17436
 
17437
  return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17438
}
17439
 
17440
/* Parse a throw-expression.
17441
 
17442
   throw-expression:
17443
     throw assignment-expression [opt]
17444
 
17445
   Returns a THROW_EXPR representing the throw-expression.  */
17446
 
17447
static tree
17448
cp_parser_throw_expression (cp_parser* parser)
17449
{
17450
  tree expression;
17451
  cp_token* token;
17452
 
17453
  cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17454
  token = cp_lexer_peek_token (parser->lexer);
17455
  /* Figure out whether or not there is an assignment-expression
17456
     following the "throw" keyword.  */
17457
  if (token->type == CPP_COMMA
17458
      || token->type == CPP_SEMICOLON
17459
      || token->type == CPP_CLOSE_PAREN
17460
      || token->type == CPP_CLOSE_SQUARE
17461
      || token->type == CPP_CLOSE_BRACE
17462
      || token->type == CPP_COLON)
17463
    expression = NULL_TREE;
17464
  else
17465
    expression = cp_parser_assignment_expression (parser,
17466
                                                  /*cast_p=*/false, NULL);
17467
 
17468
  return build_throw (expression);
17469
}
17470
 
17471
/* GNU Extensions */
17472
 
17473
/* Parse an (optional) asm-specification.
17474
 
17475
   asm-specification:
17476
     asm ( string-literal )
17477
 
17478
   If the asm-specification is present, returns a STRING_CST
17479
   corresponding to the string-literal.  Otherwise, returns
17480
   NULL_TREE.  */
17481
 
17482
static tree
17483
cp_parser_asm_specification_opt (cp_parser* parser)
17484
{
17485
  cp_token *token;
17486
  tree asm_specification;
17487
 
17488
  /* Peek at the next token.  */
17489
  token = cp_lexer_peek_token (parser->lexer);
17490
  /* If the next token isn't the `asm' keyword, then there's no
17491
     asm-specification.  */
17492
  if (!cp_parser_is_keyword (token, RID_ASM))
17493
    return NULL_TREE;
17494
 
17495
  /* Consume the `asm' token.  */
17496
  cp_lexer_consume_token (parser->lexer);
17497
  /* Look for the `('.  */
17498
  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17499
 
17500
  /* Look for the string-literal.  */
17501
  asm_specification = cp_parser_string_literal (parser, false, false);
17502
 
17503
  /* Look for the `)'.  */
17504
  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17505
 
17506
  return asm_specification;
17507
}
17508
 
17509
/* Parse an asm-operand-list.
17510
 
17511
   asm-operand-list:
17512
     asm-operand
17513
     asm-operand-list , asm-operand
17514
 
17515
   asm-operand:
17516
     string-literal ( expression )
17517
     [ string-literal ] string-literal ( expression )
17518
 
17519
   Returns a TREE_LIST representing the operands.  The TREE_VALUE of
17520
   each node is the expression.  The TREE_PURPOSE is itself a
17521
   TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17522
   string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17523
   is a STRING_CST for the string literal before the parenthesis. Returns
17524
   ERROR_MARK_NODE if any of the operands are invalid.  */
17525
 
17526
static tree
17527
cp_parser_asm_operand_list (cp_parser* parser)
17528
{
17529
  tree asm_operands = NULL_TREE;
17530
  bool invalid_operands = false;
17531
 
17532
  while (true)
17533
    {
17534
      tree string_literal;
17535
      tree expression;
17536
      tree name;
17537
 
17538
      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17539
        {
17540
          /* Consume the `[' token.  */
17541
          cp_lexer_consume_token (parser->lexer);
17542
          /* Read the operand name.  */
17543
          name = cp_parser_identifier (parser);
17544
          if (name != error_mark_node)
17545
            name = build_string (IDENTIFIER_LENGTH (name),
17546
                                 IDENTIFIER_POINTER (name));
17547
          /* Look for the closing `]'.  */
17548
          cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17549
        }
17550
      else
17551
        name = NULL_TREE;
17552
      /* Look for the string-literal.  */
17553
      string_literal = cp_parser_string_literal (parser, false, false);
17554
 
17555
      /* Look for the `('.  */
17556
      cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17557
      /* Parse the expression.  */
17558
      expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17559
      /* Look for the `)'.  */
17560
      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17561
 
17562
      if (name == error_mark_node
17563
          || string_literal == error_mark_node
17564
          || expression == error_mark_node)
17565
        invalid_operands = true;
17566
 
17567
      /* Add this operand to the list.  */
17568
      asm_operands = tree_cons (build_tree_list (name, string_literal),
17569
                                expression,
17570
                                asm_operands);
17571
      /* If the next token is not a `,', there are no more
17572
         operands.  */
17573
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17574
        break;
17575
      /* Consume the `,'.  */
17576
      cp_lexer_consume_token (parser->lexer);
17577
    }
17578
 
17579
  return invalid_operands ? error_mark_node : nreverse (asm_operands);
17580
}
17581
 
17582
/* Parse an asm-clobber-list.
17583
 
17584
   asm-clobber-list:
17585
     string-literal
17586
     asm-clobber-list , string-literal
17587
 
17588
   Returns a TREE_LIST, indicating the clobbers in the order that they
17589
   appeared.  The TREE_VALUE of each node is a STRING_CST.  */
17590
 
17591
static tree
17592
cp_parser_asm_clobber_list (cp_parser* parser)
17593
{
17594
  tree clobbers = NULL_TREE;
17595
 
17596
  while (true)
17597
    {
17598
      tree string_literal;
17599
 
17600
      /* Look for the string literal.  */
17601
      string_literal = cp_parser_string_literal (parser, false, false);
17602
      /* Add it to the list.  */
17603
      clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17604
      /* If the next token is not a `,', then the list is
17605
         complete.  */
17606
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17607
        break;
17608
      /* Consume the `,' token.  */
17609
      cp_lexer_consume_token (parser->lexer);
17610
    }
17611
 
17612
  return clobbers;
17613
}
17614
 
17615
/* Parse an asm-label-list.
17616
 
17617
   asm-label-list:
17618
     identifier
17619
     asm-label-list , identifier
17620
 
17621
   Returns a TREE_LIST, indicating the labels in the order that they
17622
   appeared.  The TREE_VALUE of each node is a label.  */
17623
 
17624
static tree
17625
cp_parser_asm_label_list (cp_parser* parser)
17626
{
17627
  tree labels = NULL_TREE;
17628
 
17629
  while (true)
17630
    {
17631
      tree identifier, label, name;
17632
 
17633
      /* Look for the identifier.  */
17634
      identifier = cp_parser_identifier (parser);
17635
      if (!error_operand_p (identifier))
17636
        {
17637
          label = lookup_label (identifier);
17638
          if (TREE_CODE (label) == LABEL_DECL)
17639
            {
17640
              TREE_USED (label) = 1;
17641
              check_goto (label);
17642
              name = build_string (IDENTIFIER_LENGTH (identifier),
17643
                                   IDENTIFIER_POINTER (identifier));
17644
              labels = tree_cons (name, label, labels);
17645
            }
17646
        }
17647
      /* If the next token is not a `,', then the list is
17648
         complete.  */
17649
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17650
        break;
17651
      /* Consume the `,' token.  */
17652
      cp_lexer_consume_token (parser->lexer);
17653
    }
17654
 
17655
  return nreverse (labels);
17656
}
17657
 
17658
/* Parse an (optional) series of attributes.
17659
 
17660
   attributes:
17661
     attributes attribute
17662
 
17663
   attribute:
17664
     __attribute__ (( attribute-list [opt] ))
17665
 
17666
   The return value is as for cp_parser_attribute_list.  */
17667
 
17668
static tree
17669
cp_parser_attributes_opt (cp_parser* parser)
17670
{
17671
  tree attributes = NULL_TREE;
17672
 
17673
  while (true)
17674
    {
17675
      cp_token *token;
17676
      tree attribute_list;
17677
 
17678
      /* Peek at the next token.  */
17679
      token = cp_lexer_peek_token (parser->lexer);
17680
      /* If it's not `__attribute__', then we're done.  */
17681
      if (token->keyword != RID_ATTRIBUTE)
17682
        break;
17683
 
17684
      /* Consume the `__attribute__' keyword.  */
17685
      cp_lexer_consume_token (parser->lexer);
17686
      /* Look for the two `(' tokens.  */
17687
      cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17688
      cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17689
 
17690
      /* Peek at the next token.  */
17691
      token = cp_lexer_peek_token (parser->lexer);
17692
      if (token->type != CPP_CLOSE_PAREN)
17693
        /* Parse the attribute-list.  */
17694
        attribute_list = cp_parser_attribute_list (parser);
17695
      else
17696
        /* If the next token is a `)', then there is no attribute
17697
           list.  */
17698
        attribute_list = NULL;
17699
 
17700
      /* Look for the two `)' tokens.  */
17701
      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17702
      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17703
 
17704
      /* Add these new attributes to the list.  */
17705
      attributes = chainon (attributes, attribute_list);
17706
    }
17707
 
17708
  return attributes;
17709
}
17710
 
17711
/* Parse an attribute-list.
17712
 
17713
   attribute-list:
17714
     attribute
17715
     attribute-list , attribute
17716
 
17717
   attribute:
17718
     identifier
17719
     identifier ( identifier )
17720
     identifier ( identifier , expression-list )
17721
     identifier ( expression-list )
17722
 
17723
   Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
17724
   to an attribute.  The TREE_PURPOSE of each node is the identifier
17725
   indicating which attribute is in use.  The TREE_VALUE represents
17726
   the arguments, if any.  */
17727
 
17728
static tree
17729
cp_parser_attribute_list (cp_parser* parser)
17730
{
17731
  tree attribute_list = NULL_TREE;
17732
  bool save_translate_strings_p = parser->translate_strings_p;
17733
 
17734
  parser->translate_strings_p = false;
17735
  while (true)
17736
    {
17737
      cp_token *token;
17738
      tree identifier;
17739
      tree attribute;
17740
 
17741
      /* Look for the identifier.  We also allow keywords here; for
17742
         example `__attribute__ ((const))' is legal.  */
17743
      token = cp_lexer_peek_token (parser->lexer);
17744
      if (token->type == CPP_NAME
17745
          || token->type == CPP_KEYWORD)
17746
        {
17747
          tree arguments = NULL_TREE;
17748
 
17749
          /* Consume the token.  */
17750
          token = cp_lexer_consume_token (parser->lexer);
17751
 
17752
          /* Save away the identifier that indicates which attribute
17753
             this is.  */
17754
          identifier = (token->type == CPP_KEYWORD)
17755
            /* For keywords, use the canonical spelling, not the
17756
               parsed identifier.  */
17757
            ? ridpointers[(int) token->keyword]
17758
            : token->u.value;
17759
 
17760
          attribute = build_tree_list (identifier, NULL_TREE);
17761
 
17762
          /* Peek at the next token.  */
17763
          token = cp_lexer_peek_token (parser->lexer);
17764
          /* If it's an `(', then parse the attribute arguments.  */
17765
          if (token->type == CPP_OPEN_PAREN)
17766
            {
17767
              VEC(tree,gc) *vec;
17768
              vec = cp_parser_parenthesized_expression_list
17769
                    (parser, true, /*cast_p=*/false,
17770
                     /*allow_expansion_p=*/false,
17771
                     /*non_constant_p=*/NULL);
17772
              if (vec == NULL)
17773
                arguments = error_mark_node;
17774
              else
17775
                {
17776
                  arguments = build_tree_list_vec (vec);
17777
                  release_tree_vector (vec);
17778
                }
17779
              /* Save the arguments away.  */
17780
              TREE_VALUE (attribute) = arguments;
17781
            }
17782
 
17783
          if (arguments != error_mark_node)
17784
            {
17785
              /* Add this attribute to the list.  */
17786
              TREE_CHAIN (attribute) = attribute_list;
17787
              attribute_list = attribute;
17788
            }
17789
 
17790
          token = cp_lexer_peek_token (parser->lexer);
17791
        }
17792
      /* Now, look for more attributes.  If the next token isn't a
17793
         `,', we're done.  */
17794
      if (token->type != CPP_COMMA)
17795
        break;
17796
 
17797
      /* Consume the comma and keep going.  */
17798
      cp_lexer_consume_token (parser->lexer);
17799
    }
17800
  parser->translate_strings_p = save_translate_strings_p;
17801
 
17802
  /* We built up the list in reverse order.  */
17803
  return nreverse (attribute_list);
17804
}
17805
 
17806
/* Parse an optional `__extension__' keyword.  Returns TRUE if it is
17807
   present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
17808
   current value of the PEDANTIC flag, regardless of whether or not
17809
   the `__extension__' keyword is present.  The caller is responsible
17810
   for restoring the value of the PEDANTIC flag.  */
17811
 
17812
static bool
17813
cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17814
{
17815
  /* Save the old value of the PEDANTIC flag.  */
17816
  *saved_pedantic = pedantic;
17817
 
17818
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17819
    {
17820
      /* Consume the `__extension__' token.  */
17821
      cp_lexer_consume_token (parser->lexer);
17822
      /* We're not being pedantic while the `__extension__' keyword is
17823
         in effect.  */
17824
      pedantic = 0;
17825
 
17826
      return true;
17827
    }
17828
 
17829
  return false;
17830
}
17831
 
17832
/* Parse a label declaration.
17833
 
17834
   label-declaration:
17835
     __label__ label-declarator-seq ;
17836
 
17837
   label-declarator-seq:
17838
     identifier , label-declarator-seq
17839
     identifier  */
17840
 
17841
static void
17842
cp_parser_label_declaration (cp_parser* parser)
17843
{
17844
  /* Look for the `__label__' keyword.  */
17845
  cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17846
 
17847
  while (true)
17848
    {
17849
      tree identifier;
17850
 
17851
      /* Look for an identifier.  */
17852
      identifier = cp_parser_identifier (parser);
17853
      /* If we failed, stop.  */
17854
      if (identifier == error_mark_node)
17855
        break;
17856
      /* Declare it as a label.  */
17857
      finish_label_decl (identifier);
17858
      /* If the next token is a `;', stop.  */
17859
      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17860
        break;
17861
      /* Look for the `,' separating the label declarations.  */
17862
      cp_parser_require (parser, CPP_COMMA, "%<,%>");
17863
    }
17864
 
17865
  /* Look for the final `;'.  */
17866
  cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17867
}
17868
 
17869
/* Support Functions */
17870
 
17871
/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17872
   NAME should have one of the representations used for an
17873
   id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17874
   is returned.  If PARSER->SCOPE is a dependent type, then a
17875
   SCOPE_REF is returned.
17876
 
17877
   If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17878
   returned; the name was already resolved when the TEMPLATE_ID_EXPR
17879
   was formed.  Abstractly, such entities should not be passed to this
17880
   function, because they do not need to be looked up, but it is
17881
   simpler to check for this special case here, rather than at the
17882
   call-sites.
17883
 
17884
   In cases not explicitly covered above, this function returns a
17885
   DECL, OVERLOAD, or baselink representing the result of the lookup.
17886
   If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17887
   is returned.
17888
 
17889
   If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17890
   (e.g., "struct") that was used.  In that case bindings that do not
17891
   refer to types are ignored.
17892
 
17893
   If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17894
   ignored.
17895
 
17896
   If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17897
   are ignored.
17898
 
17899
   If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17900
   types.
17901
 
17902
   If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17903
   TREE_LIST of candidates if name-lookup results in an ambiguity, and
17904
   NULL_TREE otherwise.  */
17905
 
17906
static tree
17907
cp_parser_lookup_name (cp_parser *parser, tree name,
17908
                       enum tag_types tag_type,
17909
                       bool is_template,
17910
                       bool is_namespace,
17911
                       bool check_dependency,
17912
                       tree *ambiguous_decls,
17913
                       location_t name_location)
17914
{
17915
  int flags = 0;
17916
  tree decl;
17917
  tree object_type = parser->context->object_type;
17918
 
17919
  if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17920
    flags |= LOOKUP_COMPLAIN;
17921
 
17922
  /* Assume that the lookup will be unambiguous.  */
17923
  if (ambiguous_decls)
17924
    *ambiguous_decls = NULL_TREE;
17925
 
17926
  /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17927
     no longer valid.  Note that if we are parsing tentatively, and
17928
     the parse fails, OBJECT_TYPE will be automatically restored.  */
17929
  parser->context->object_type = NULL_TREE;
17930
 
17931
  if (name == error_mark_node)
17932
    return error_mark_node;
17933
 
17934
  /* A template-id has already been resolved; there is no lookup to
17935
     do.  */
17936
  if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17937
    return name;
17938
  if (BASELINK_P (name))
17939
    {
17940
      gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17941
                  == TEMPLATE_ID_EXPR);
17942
      return name;
17943
    }
17944
 
17945
  /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17946
     it should already have been checked to make sure that the name
17947
     used matches the type being destroyed.  */
17948
  if (TREE_CODE (name) == BIT_NOT_EXPR)
17949
    {
17950
      tree type;
17951
 
17952
      /* Figure out to which type this destructor applies.  */
17953
      if (parser->scope)
17954
        type = parser->scope;
17955
      else if (object_type)
17956
        type = object_type;
17957
      else
17958
        type = current_class_type;
17959
      /* If that's not a class type, there is no destructor.  */
17960
      if (!type || !CLASS_TYPE_P (type))
17961
        return error_mark_node;
17962
      if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17963
        lazily_declare_fn (sfk_destructor, type);
17964
      if (!CLASSTYPE_DESTRUCTORS (type))
17965
          return error_mark_node;
17966
      /* If it was a class type, return the destructor.  */
17967
      return CLASSTYPE_DESTRUCTORS (type);
17968
    }
17969
 
17970
  /* By this point, the NAME should be an ordinary identifier.  If
17971
     the id-expression was a qualified name, the qualifying scope is
17972
     stored in PARSER->SCOPE at this point.  */
17973
  gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17974
 
17975
  /* Perform the lookup.  */
17976
  if (parser->scope)
17977
    {
17978
      bool dependent_p;
17979
 
17980
      if (parser->scope == error_mark_node)
17981
        return error_mark_node;
17982
 
17983
      /* If the SCOPE is dependent, the lookup must be deferred until
17984
         the template is instantiated -- unless we are explicitly
17985
         looking up names in uninstantiated templates.  Even then, we
17986
         cannot look up the name if the scope is not a class type; it
17987
         might, for example, be a template type parameter.  */
17988
      dependent_p = (TYPE_P (parser->scope)
17989
                     && dependent_scope_p (parser->scope));
17990
      if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17991
          && dependent_p)
17992
        /* Defer lookup.  */
17993
        decl = error_mark_node;
17994
      else
17995
        {
17996
          tree pushed_scope = NULL_TREE;
17997
 
17998
          /* If PARSER->SCOPE is a dependent type, then it must be a
17999
             class type, and we must not be checking dependencies;
18000
             otherwise, we would have processed this lookup above.  So
18001
             that PARSER->SCOPE is not considered a dependent base by
18002
             lookup_member, we must enter the scope here.  */
18003
          if (dependent_p)
18004
            pushed_scope = push_scope (parser->scope);
18005
 
18006
          /* If the PARSER->SCOPE is a template specialization, it
18007
             may be instantiated during name lookup.  In that case,
18008
             errors may be issued.  Even if we rollback the current
18009
             tentative parse, those errors are valid.  */
18010
          decl = lookup_qualified_name (parser->scope, name,
18011
                                        tag_type != none_type,
18012
                                        /*complain=*/true);
18013
 
18014
          /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18015
             lookup result and the nested-name-specifier nominates a class C:
18016
               * if the name specified after the nested-name-specifier, when
18017
               looked up in C, is the injected-class-name of C (Clause 9), or
18018
               * if the name specified after the nested-name-specifier is the
18019
               same as the identifier or the simple-template-id's template-
18020
               name in the last component of the nested-name-specifier,
18021
             the name is instead considered to name the constructor of
18022
             class C. [ Note: for example, the constructor is not an
18023
             acceptable lookup result in an elaborated-type-specifier so
18024
             the constructor would not be used in place of the
18025
             injected-class-name. --end note ] Such a constructor name
18026
             shall be used only in the declarator-id of a declaration that
18027
             names a constructor or in a using-declaration.  */
18028
          if (tag_type == none_type
18029
              && DECL_SELF_REFERENCE_P (decl)
18030
              && same_type_p (DECL_CONTEXT (decl), parser->scope))
18031
            decl = lookup_qualified_name (parser->scope, ctor_identifier,
18032
                                          tag_type != none_type,
18033
                                          /*complain=*/true);
18034
 
18035
          /* If we have a single function from a using decl, pull it out.  */
18036
          if (TREE_CODE (decl) == OVERLOAD
18037
              && !really_overloaded_fn (decl))
18038
            decl = OVL_FUNCTION (decl);
18039
 
18040
          if (pushed_scope)
18041
            pop_scope (pushed_scope);
18042
        }
18043
 
18044
      /* If the scope is a dependent type and either we deferred lookup or
18045
         we did lookup but didn't find the name, rememeber the name.  */
18046
      if (decl == error_mark_node && TYPE_P (parser->scope)
18047
          && dependent_type_p (parser->scope))
18048
        {
18049
          if (tag_type)
18050
            {
18051
              tree type;
18052
 
18053
              /* The resolution to Core Issue 180 says that `struct
18054
                 A::B' should be considered a type-name, even if `A'
18055
                 is dependent.  */
18056
              type = make_typename_type (parser->scope, name, tag_type,
18057
                                         /*complain=*/tf_error);
18058
              decl = TYPE_NAME (type);
18059
            }
18060
          else if (is_template
18061
                   && (cp_parser_next_token_ends_template_argument_p (parser)
18062
                       || cp_lexer_next_token_is (parser->lexer,
18063
                                                  CPP_CLOSE_PAREN)))
18064
            decl = make_unbound_class_template (parser->scope,
18065
                                                name, NULL_TREE,
18066
                                                /*complain=*/tf_error);
18067
          else
18068
            decl = build_qualified_name (/*type=*/NULL_TREE,
18069
                                         parser->scope, name,
18070
                                         is_template);
18071
        }
18072
      parser->qualifying_scope = parser->scope;
18073
      parser->object_scope = NULL_TREE;
18074
    }
18075
  else if (object_type)
18076
    {
18077
      tree object_decl = NULL_TREE;
18078
      /* Look up the name in the scope of the OBJECT_TYPE, unless the
18079
         OBJECT_TYPE is not a class.  */
18080
      if (CLASS_TYPE_P (object_type))
18081
        /* If the OBJECT_TYPE is a template specialization, it may
18082
           be instantiated during name lookup.  In that case, errors
18083
           may be issued.  Even if we rollback the current tentative
18084
           parse, those errors are valid.  */
18085
        object_decl = lookup_member (object_type,
18086
                                     name,
18087
                                     /*protect=*/0,
18088
                                     tag_type != none_type);
18089
      /* Look it up in the enclosing context, too.  */
18090
      decl = lookup_name_real (name, tag_type != none_type,
18091
                               /*nonclass=*/0,
18092
                               /*block_p=*/true, is_namespace, flags);
18093
      parser->object_scope = object_type;
18094
      parser->qualifying_scope = NULL_TREE;
18095
      if (object_decl)
18096
        decl = object_decl;
18097
    }
18098
  else
18099
    {
18100
      decl = lookup_name_real (name, tag_type != none_type,
18101
                               /*nonclass=*/0,
18102
                               /*block_p=*/true, is_namespace, flags);
18103
      parser->qualifying_scope = NULL_TREE;
18104
      parser->object_scope = NULL_TREE;
18105
    }
18106
 
18107
  /* If the lookup failed, let our caller know.  */
18108
  if (!decl || decl == error_mark_node)
18109
    return error_mark_node;
18110
 
18111
  /* Pull out the template from an injected-class-name (or multiple).  */
18112
  if (is_template)
18113
    decl = maybe_get_template_decl_from_type_decl (decl);
18114
 
18115
  /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
18116
  if (TREE_CODE (decl) == TREE_LIST)
18117
    {
18118
      if (ambiguous_decls)
18119
        *ambiguous_decls = decl;
18120
      /* The error message we have to print is too complicated for
18121
         cp_parser_error, so we incorporate its actions directly.  */
18122
      if (!cp_parser_simulate_error (parser))
18123
        {
18124
          error_at (name_location, "reference to %qD is ambiguous",
18125
                    name);
18126
          print_candidates (decl);
18127
        }
18128
      return error_mark_node;
18129
    }
18130
 
18131
  gcc_assert (DECL_P (decl)
18132
              || TREE_CODE (decl) == OVERLOAD
18133
              || TREE_CODE (decl) == SCOPE_REF
18134
              || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18135
              || BASELINK_P (decl));
18136
 
18137
  /* If we have resolved the name of a member declaration, check to
18138
     see if the declaration is accessible.  When the name resolves to
18139
     set of overloaded functions, accessibility is checked when
18140
     overload resolution is done.
18141
 
18142
     During an explicit instantiation, access is not checked at all,
18143
     as per [temp.explicit].  */
18144
  if (DECL_P (decl))
18145
    check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18146
 
18147
  return decl;
18148
}
18149
 
18150
/* Like cp_parser_lookup_name, but for use in the typical case where
18151
   CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18152
   IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
18153
 
18154
static tree
18155
cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18156
{
18157
  return cp_parser_lookup_name (parser, name,
18158
                                none_type,
18159
                                /*is_template=*/false,
18160
                                /*is_namespace=*/false,
18161
                                /*check_dependency=*/true,
18162
                                /*ambiguous_decls=*/NULL,
18163
                                location);
18164
}
18165
 
18166
/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18167
   the current context, return the TYPE_DECL.  If TAG_NAME_P is
18168
   true, the DECL indicates the class being defined in a class-head,
18169
   or declared in an elaborated-type-specifier.
18170
 
18171
   Otherwise, return DECL.  */
18172
 
18173
static tree
18174
cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18175
{
18176
  /* If the TEMPLATE_DECL is being declared as part of a class-head,
18177
     the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18178
 
18179
       struct A {
18180
         template <typename T> struct B;
18181
       };
18182
 
18183
       template <typename T> struct A::B {};
18184
 
18185
     Similarly, in an elaborated-type-specifier:
18186
 
18187
       namespace N { struct X{}; }
18188
 
18189
       struct A {
18190
         template <typename T> friend struct N::X;
18191
       };
18192
 
18193
     However, if the DECL refers to a class type, and we are in
18194
     the scope of the class, then the name lookup automatically
18195
     finds the TYPE_DECL created by build_self_reference rather
18196
     than a TEMPLATE_DECL.  For example, in:
18197
 
18198
       template <class T> struct S {
18199
         S s;
18200
       };
18201
 
18202
     there is no need to handle such case.  */
18203
 
18204
  if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18205
    return DECL_TEMPLATE_RESULT (decl);
18206
 
18207
  return decl;
18208
}
18209
 
18210
/* If too many, or too few, template-parameter lists apply to the
18211
   declarator, issue an error message.  Returns TRUE if all went well,
18212
   and FALSE otherwise.  */
18213
 
18214
static bool
18215
cp_parser_check_declarator_template_parameters (cp_parser* parser,
18216
                                                cp_declarator *declarator,
18217
                                                location_t declarator_location)
18218
{
18219
  unsigned num_templates;
18220
 
18221
  /* We haven't seen any classes that involve template parameters yet.  */
18222
  num_templates = 0;
18223
 
18224
  switch (declarator->kind)
18225
    {
18226
    case cdk_id:
18227
      if (declarator->u.id.qualifying_scope)
18228
        {
18229
          tree scope;
18230
 
18231
          scope = declarator->u.id.qualifying_scope;
18232
 
18233
          while (scope && CLASS_TYPE_P (scope))
18234
            {
18235
              /* You're supposed to have one `template <...>'
18236
                 for every template class, but you don't need one
18237
                 for a full specialization.  For example:
18238
 
18239
                 template <class T> struct S{};
18240
                 template <> struct S<int> { void f(); };
18241
                 void S<int>::f () {}
18242
 
18243
                 is correct; there shouldn't be a `template <>' for
18244
                 the definition of `S<int>::f'.  */
18245
              if (!CLASSTYPE_TEMPLATE_INFO (scope))
18246
                /* If SCOPE does not have template information of any
18247
                   kind, then it is not a template, nor is it nested
18248
                   within a template.  */
18249
                break;
18250
              if (explicit_class_specialization_p (scope))
18251
                break;
18252
              if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18253
                ++num_templates;
18254
 
18255
              scope = TYPE_CONTEXT (scope);
18256
            }
18257
        }
18258
      else if (TREE_CODE (declarator->u.id.unqualified_name)
18259
               == TEMPLATE_ID_EXPR)
18260
        /* If the DECLARATOR has the form `X<y>' then it uses one
18261
           additional level of template parameters.  */
18262
        ++num_templates;
18263
 
18264
      return cp_parser_check_template_parameters
18265
        (parser, num_templates, declarator_location, declarator);
18266
 
18267
 
18268
    case cdk_function:
18269
    case cdk_array:
18270
    case cdk_pointer:
18271
    case cdk_reference:
18272
    case cdk_ptrmem:
18273
      return (cp_parser_check_declarator_template_parameters
18274
              (parser, declarator->declarator, declarator_location));
18275
 
18276
    case cdk_error:
18277
      return true;
18278
 
18279
    default:
18280
      gcc_unreachable ();
18281
    }
18282
  return false;
18283
}
18284
 
18285
/* NUM_TEMPLATES were used in the current declaration.  If that is
18286
   invalid, return FALSE and issue an error messages.  Otherwise,
18287
   return TRUE.  If DECLARATOR is non-NULL, then we are checking a
18288
   declarator and we can print more accurate diagnostics.  */
18289
 
18290
static bool
18291
cp_parser_check_template_parameters (cp_parser* parser,
18292
                                     unsigned num_templates,
18293
                                     location_t location,
18294
                                     cp_declarator *declarator)
18295
{
18296
  /* If there are the same number of template classes and parameter
18297
     lists, that's OK.  */
18298
  if (parser->num_template_parameter_lists == num_templates)
18299
    return true;
18300
  /* If there are more, but only one more, then we are referring to a
18301
     member template.  That's OK too.  */
18302
  if (parser->num_template_parameter_lists == num_templates + 1)
18303
    return true;
18304
  /* If there are more template classes than parameter lists, we have
18305
     something like:
18306
 
18307
       template <class T> void S<T>::R<T>::f ();  */
18308
  if (parser->num_template_parameter_lists < num_templates)
18309
    {
18310
      if (declarator && !current_function_decl)
18311
        error_at (location, "specializing member %<%T::%E%> "
18312
                  "requires %<template<>%> syntax",
18313
                  declarator->u.id.qualifying_scope,
18314
                  declarator->u.id.unqualified_name);
18315
      else if (declarator)
18316
        error_at (location, "invalid declaration of %<%T::%E%>",
18317
                  declarator->u.id.qualifying_scope,
18318
                  declarator->u.id.unqualified_name);
18319
      else
18320
        error_at (location, "too few template-parameter-lists");
18321
      return false;
18322
    }
18323
  /* Otherwise, there are too many template parameter lists.  We have
18324
     something like:
18325
 
18326
     template <class T> template <class U> void S::f();  */
18327
  error_at (location, "too many template-parameter-lists");
18328
  return false;
18329
}
18330
 
18331
/* Parse an optional `::' token indicating that the following name is
18332
   from the global namespace.  If so, PARSER->SCOPE is set to the
18333
   GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18334
   unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18335
   Returns the new value of PARSER->SCOPE, if the `::' token is
18336
   present, and NULL_TREE otherwise.  */
18337
 
18338
static tree
18339
cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18340
{
18341
  cp_token *token;
18342
 
18343
  /* Peek at the next token.  */
18344
  token = cp_lexer_peek_token (parser->lexer);
18345
  /* If we're looking at a `::' token then we're starting from the
18346
     global namespace, not our current location.  */
18347
  if (token->type == CPP_SCOPE)
18348
    {
18349
      /* Consume the `::' token.  */
18350
      cp_lexer_consume_token (parser->lexer);
18351
      /* Set the SCOPE so that we know where to start the lookup.  */
18352
      parser->scope = global_namespace;
18353
      parser->qualifying_scope = global_namespace;
18354
      parser->object_scope = NULL_TREE;
18355
 
18356
      return parser->scope;
18357
    }
18358
  else if (!current_scope_valid_p)
18359
    {
18360
      parser->scope = NULL_TREE;
18361
      parser->qualifying_scope = NULL_TREE;
18362
      parser->object_scope = NULL_TREE;
18363
    }
18364
 
18365
  return NULL_TREE;
18366
}
18367
 
18368
/* Returns TRUE if the upcoming token sequence is the start of a
18369
   constructor declarator.  If FRIEND_P is true, the declarator is
18370
   preceded by the `friend' specifier.  */
18371
 
18372
static bool
18373
cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18374
{
18375
  bool constructor_p;
18376
  tree nested_name_specifier;
18377
  cp_token *next_token;
18378
 
18379
  /* The common case is that this is not a constructor declarator, so
18380
     try to avoid doing lots of work if at all possible.  It's not
18381
     valid declare a constructor at function scope.  */
18382
  if (parser->in_function_body)
18383
    return false;
18384
  /* And only certain tokens can begin a constructor declarator.  */
18385
  next_token = cp_lexer_peek_token (parser->lexer);
18386
  if (next_token->type != CPP_NAME
18387
      && next_token->type != CPP_SCOPE
18388
      && next_token->type != CPP_NESTED_NAME_SPECIFIER
18389
      && next_token->type != CPP_TEMPLATE_ID)
18390
    return false;
18391
 
18392
  /* Parse tentatively; we are going to roll back all of the tokens
18393
     consumed here.  */
18394
  cp_parser_parse_tentatively (parser);
18395
  /* Assume that we are looking at a constructor declarator.  */
18396
  constructor_p = true;
18397
 
18398
  /* Look for the optional `::' operator.  */
18399
  cp_parser_global_scope_opt (parser,
18400
                              /*current_scope_valid_p=*/false);
18401
  /* Look for the nested-name-specifier.  */
18402
  nested_name_specifier
18403
    = (cp_parser_nested_name_specifier_opt (parser,
18404
                                            /*typename_keyword_p=*/false,
18405
                                            /*check_dependency_p=*/false,
18406
                                            /*type_p=*/false,
18407
                                            /*is_declaration=*/false));
18408
  /* Outside of a class-specifier, there must be a
18409
     nested-name-specifier.  */
18410
  if (!nested_name_specifier &&
18411
      (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18412
       || friend_p))
18413
    constructor_p = false;
18414
  else if (nested_name_specifier == error_mark_node)
18415
    constructor_p = false;
18416
 
18417
  /* If we have a class scope, this is easy; DR 147 says that S::S always
18418
     names the constructor, and no other qualified name could.  */
18419
  if (constructor_p && nested_name_specifier
18420
      && TYPE_P (nested_name_specifier))
18421
    {
18422
      tree id = cp_parser_unqualified_id (parser,
18423
                                          /*template_keyword_p=*/false,
18424
                                          /*check_dependency_p=*/false,
18425
                                          /*declarator_p=*/true,
18426
                                          /*optional_p=*/false);
18427
      if (is_overloaded_fn (id))
18428
        id = DECL_NAME (get_first_fn (id));
18429
      if (!constructor_name_p (id, nested_name_specifier))
18430
        constructor_p = false;
18431
    }
18432
  /* If we still think that this might be a constructor-declarator,
18433
     look for a class-name.  */
18434
  else if (constructor_p)
18435
    {
18436
      /* If we have:
18437
 
18438
           template <typename T> struct S {
18439
             S();
18440
           };
18441
 
18442
         we must recognize that the nested `S' names a class.  */
18443
      tree type_decl;
18444
      type_decl = cp_parser_class_name (parser,
18445
                                        /*typename_keyword_p=*/false,
18446
                                        /*template_keyword_p=*/false,
18447
                                        none_type,
18448
                                        /*check_dependency_p=*/false,
18449
                                        /*class_head_p=*/false,
18450
                                        /*is_declaration=*/false);
18451
      /* If there was no class-name, then this is not a constructor.  */
18452
      constructor_p = !cp_parser_error_occurred (parser);
18453
 
18454
      /* If we're still considering a constructor, we have to see a `(',
18455
         to begin the parameter-declaration-clause, followed by either a
18456
         `)', an `...', or a decl-specifier.  We need to check for a
18457
         type-specifier to avoid being fooled into thinking that:
18458
 
18459
           S (f) (int);
18460
 
18461
         is a constructor.  (It is actually a function named `f' that
18462
         takes one parameter (of type `int') and returns a value of type
18463
         `S'.  */
18464
      if (constructor_p
18465
          && !cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18466
        constructor_p = false;
18467
 
18468
      if (constructor_p
18469
          && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18470
          && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18471
          /* A parameter declaration begins with a decl-specifier,
18472
             which is either the "attribute" keyword, a storage class
18473
             specifier, or (usually) a type-specifier.  */
18474
          && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18475
        {
18476
          tree type;
18477
          tree pushed_scope = NULL_TREE;
18478
          unsigned saved_num_template_parameter_lists;
18479
 
18480
          /* Names appearing in the type-specifier should be looked up
18481
             in the scope of the class.  */
18482
          if (current_class_type)
18483
            type = NULL_TREE;
18484
          else
18485
            {
18486
              type = TREE_TYPE (type_decl);
18487
              if (TREE_CODE (type) == TYPENAME_TYPE)
18488
                {
18489
                  type = resolve_typename_type (type,
18490
                                                /*only_current_p=*/false);
18491
                  if (TREE_CODE (type) == TYPENAME_TYPE)
18492
                    {
18493
                      cp_parser_abort_tentative_parse (parser);
18494
                      return false;
18495
                    }
18496
                }
18497
              pushed_scope = push_scope (type);
18498
            }
18499
 
18500
          /* Inside the constructor parameter list, surrounding
18501
             template-parameter-lists do not apply.  */
18502
          saved_num_template_parameter_lists
18503
            = parser->num_template_parameter_lists;
18504
          parser->num_template_parameter_lists = 0;
18505
 
18506
          /* Look for the type-specifier.  */
18507
          cp_parser_type_specifier (parser,
18508
                                    CP_PARSER_FLAGS_NONE,
18509
                                    /*decl_specs=*/NULL,
18510
                                    /*is_declarator=*/true,
18511
                                    /*declares_class_or_enum=*/NULL,
18512
                                    /*is_cv_qualifier=*/NULL);
18513
 
18514
          parser->num_template_parameter_lists
18515
            = saved_num_template_parameter_lists;
18516
 
18517
          /* Leave the scope of the class.  */
18518
          if (pushed_scope)
18519
            pop_scope (pushed_scope);
18520
 
18521
          constructor_p = !cp_parser_error_occurred (parser);
18522
        }
18523
    }
18524
 
18525
  /* We did not really want to consume any tokens.  */
18526
  cp_parser_abort_tentative_parse (parser);
18527
 
18528
  return constructor_p;
18529
}
18530
 
18531
/* Parse the definition of the function given by the DECL_SPECIFIERS,
18532
   ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
18533
   they must be performed once we are in the scope of the function.
18534
 
18535
   Returns the function defined.  */
18536
 
18537
static tree
18538
cp_parser_function_definition_from_specifiers_and_declarator
18539
  (cp_parser* parser,
18540
   cp_decl_specifier_seq *decl_specifiers,
18541
   tree attributes,
18542
   const cp_declarator *declarator)
18543
{
18544
  tree fn;
18545
  bool success_p;
18546
 
18547
  /* Begin the function-definition.  */
18548
  success_p = start_function (decl_specifiers, declarator, attributes);
18549
 
18550
  /* The things we're about to see are not directly qualified by any
18551
     template headers we've seen thus far.  */
18552
  reset_specialization ();
18553
 
18554
  /* If there were names looked up in the decl-specifier-seq that we
18555
     did not check, check them now.  We must wait until we are in the
18556
     scope of the function to perform the checks, since the function
18557
     might be a friend.  */
18558
  perform_deferred_access_checks ();
18559
 
18560
  if (!success_p)
18561
    {
18562
      /* Skip the entire function.  */
18563
      cp_parser_skip_to_end_of_block_or_statement (parser);
18564
      fn = error_mark_node;
18565
    }
18566
  else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18567
    {
18568
      /* Seen already, skip it.  An error message has already been output.  */
18569
      cp_parser_skip_to_end_of_block_or_statement (parser);
18570
      fn = current_function_decl;
18571
      current_function_decl = NULL_TREE;
18572
      /* If this is a function from a class, pop the nested class.  */
18573
      if (current_class_name)
18574
        pop_nested_class ();
18575
    }
18576
  else
18577
    fn = cp_parser_function_definition_after_declarator (parser,
18578
                                                         /*inline_p=*/false);
18579
 
18580
  return fn;
18581
}
18582
 
18583
/* Parse the part of a function-definition that follows the
18584
   declarator.  INLINE_P is TRUE iff this function is an inline
18585
   function defined within a class-specifier.
18586
 
18587
   Returns the function defined.  */
18588
 
18589
static tree
18590
cp_parser_function_definition_after_declarator (cp_parser* parser,
18591
                                                bool inline_p)
18592
{
18593
  tree fn;
18594
  bool ctor_initializer_p = false;
18595
  bool saved_in_unbraced_linkage_specification_p;
18596
  bool saved_in_function_body;
18597
  unsigned saved_num_template_parameter_lists;
18598
  cp_token *token;
18599
 
18600
  saved_in_function_body = parser->in_function_body;
18601
  parser->in_function_body = true;
18602
  /* If the next token is `return', then the code may be trying to
18603
     make use of the "named return value" extension that G++ used to
18604
     support.  */
18605
  token = cp_lexer_peek_token (parser->lexer);
18606
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18607
    {
18608
      /* Consume the `return' keyword.  */
18609
      cp_lexer_consume_token (parser->lexer);
18610
      /* Look for the identifier that indicates what value is to be
18611
         returned.  */
18612
      cp_parser_identifier (parser);
18613
      /* Issue an error message.  */
18614
      error_at (token->location,
18615
                "named return values are no longer supported");
18616
      /* Skip tokens until we reach the start of the function body.  */
18617
      while (true)
18618
        {
18619
          cp_token *token = cp_lexer_peek_token (parser->lexer);
18620
          if (token->type == CPP_OPEN_BRACE
18621
              || token->type == CPP_EOF
18622
              || token->type == CPP_PRAGMA_EOL)
18623
            break;
18624
          cp_lexer_consume_token (parser->lexer);
18625
        }
18626
    }
18627
  /* The `extern' in `extern "C" void f () { ... }' does not apply to
18628
     anything declared inside `f'.  */
18629
  saved_in_unbraced_linkage_specification_p
18630
    = parser->in_unbraced_linkage_specification_p;
18631
  parser->in_unbraced_linkage_specification_p = false;
18632
  /* Inside the function, surrounding template-parameter-lists do not
18633
     apply.  */
18634
  saved_num_template_parameter_lists
18635
    = parser->num_template_parameter_lists;
18636
  parser->num_template_parameter_lists = 0;
18637
 
18638
  start_lambda_scope (current_function_decl);
18639
 
18640
  /* If the next token is `try', then we are looking at a
18641
     function-try-block.  */
18642
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18643
    ctor_initializer_p = cp_parser_function_try_block (parser);
18644
  /* A function-try-block includes the function-body, so we only do
18645
     this next part if we're not processing a function-try-block.  */
18646
  else
18647
    ctor_initializer_p
18648
      = cp_parser_ctor_initializer_opt_and_function_body (parser);
18649
 
18650
  finish_lambda_scope ();
18651
 
18652
  /* Finish the function.  */
18653
  fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18654
                        (inline_p ? 2 : 0));
18655
  /* Generate code for it, if necessary.  */
18656
  expand_or_defer_fn (fn);
18657
  /* Restore the saved values.  */
18658
  parser->in_unbraced_linkage_specification_p
18659
    = saved_in_unbraced_linkage_specification_p;
18660
  parser->num_template_parameter_lists
18661
    = saved_num_template_parameter_lists;
18662
  parser->in_function_body = saved_in_function_body;
18663
 
18664
  return fn;
18665
}
18666
 
18667
/* Parse a template-declaration, assuming that the `export' (and
18668
   `extern') keywords, if present, has already been scanned.  MEMBER_P
18669
   is as for cp_parser_template_declaration.  */
18670
 
18671
static void
18672
cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18673
{
18674
  tree decl = NULL_TREE;
18675
  VEC (deferred_access_check,gc) *checks;
18676
  tree parameter_list;
18677
  bool friend_p = false;
18678
  bool need_lang_pop;
18679
  cp_token *token;
18680
 
18681
  /* Look for the `template' keyword.  */
18682
  token = cp_lexer_peek_token (parser->lexer);
18683
  if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18684
    return;
18685
 
18686
  /* And the `<'.  */
18687
  if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18688
    return;
18689
  if (at_class_scope_p () && current_function_decl)
18690
    {
18691
      /* 14.5.2.2 [temp.mem]
18692
 
18693
         A local class shall not have member templates.  */
18694
      error_at (token->location,
18695
                "invalid declaration of member template in local class");
18696
      cp_parser_skip_to_end_of_block_or_statement (parser);
18697
      return;
18698
    }
18699
  /* [temp]
18700
 
18701
     A template ... shall not have C linkage.  */
18702
  if (current_lang_name == lang_name_c)
18703
    {
18704
      error_at (token->location, "template with C linkage");
18705
      /* Give it C++ linkage to avoid confusing other parts of the
18706
         front end.  */
18707
      push_lang_context (lang_name_cplusplus);
18708
      need_lang_pop = true;
18709
    }
18710
  else
18711
    need_lang_pop = false;
18712
 
18713
  /* We cannot perform access checks on the template parameter
18714
     declarations until we know what is being declared, just as we
18715
     cannot check the decl-specifier list.  */
18716
  push_deferring_access_checks (dk_deferred);
18717
 
18718
  /* If the next token is `>', then we have an invalid
18719
     specialization.  Rather than complain about an invalid template
18720
     parameter, issue an error message here.  */
18721
  if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18722
    {
18723
      cp_parser_error (parser, "invalid explicit specialization");
18724
      begin_specialization ();
18725
      parameter_list = NULL_TREE;
18726
    }
18727
  else
18728
    /* Parse the template parameters.  */
18729
    parameter_list = cp_parser_template_parameter_list (parser);
18730
 
18731
  /* Get the deferred access checks from the parameter list.  These
18732
     will be checked once we know what is being declared, as for a
18733
     member template the checks must be performed in the scope of the
18734
     class containing the member.  */
18735
  checks = get_deferred_access_checks ();
18736
 
18737
  /* Look for the `>'.  */
18738
  cp_parser_skip_to_end_of_template_parameter_list (parser);
18739
  /* We just processed one more parameter list.  */
18740
  ++parser->num_template_parameter_lists;
18741
  /* If the next token is `template', there are more template
18742
     parameters.  */
18743
  if (cp_lexer_next_token_is_keyword (parser->lexer,
18744
                                      RID_TEMPLATE))
18745
    cp_parser_template_declaration_after_export (parser, member_p);
18746
  else
18747
    {
18748
      /* There are no access checks when parsing a template, as we do not
18749
         know if a specialization will be a friend.  */
18750
      push_deferring_access_checks (dk_no_check);
18751
      token = cp_lexer_peek_token (parser->lexer);
18752
      decl = cp_parser_single_declaration (parser,
18753
                                           checks,
18754
                                           member_p,
18755
                                           /*explicit_specialization_p=*/false,
18756
                                           &friend_p);
18757
      pop_deferring_access_checks ();
18758
 
18759
      /* If this is a member template declaration, let the front
18760
         end know.  */
18761
      if (member_p && !friend_p && decl)
18762
        {
18763
          if (TREE_CODE (decl) == TYPE_DECL)
18764
            cp_parser_check_access_in_redeclaration (decl, token->location);
18765
 
18766
          decl = finish_member_template_decl (decl);
18767
        }
18768
      else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18769
        make_friend_class (current_class_type, TREE_TYPE (decl),
18770
                           /*complain=*/true);
18771
    }
18772
  /* We are done with the current parameter list.  */
18773
  --parser->num_template_parameter_lists;
18774
 
18775
  pop_deferring_access_checks ();
18776
 
18777
  /* Finish up.  */
18778
  finish_template_decl (parameter_list);
18779
 
18780
  /* Register member declarations.  */
18781
  if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18782
    finish_member_declaration (decl);
18783
  /* For the erroneous case of a template with C linkage, we pushed an
18784
     implicit C++ linkage scope; exit that scope now.  */
18785
  if (need_lang_pop)
18786
    pop_lang_context ();
18787
  /* If DECL is a function template, we must return to parse it later.
18788
     (Even though there is no definition, there might be default
18789
     arguments that need handling.)  */
18790
  if (member_p && decl
18791
      && (TREE_CODE (decl) == FUNCTION_DECL
18792
          || DECL_FUNCTION_TEMPLATE_P (decl)))
18793
    TREE_VALUE (parser->unparsed_functions_queues)
18794
      = tree_cons (NULL_TREE, decl,
18795
                   TREE_VALUE (parser->unparsed_functions_queues));
18796
}
18797
 
18798
/* Perform the deferred access checks from a template-parameter-list.
18799
   CHECKS is a TREE_LIST of access checks, as returned by
18800
   get_deferred_access_checks.  */
18801
 
18802
static void
18803
cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18804
{
18805
  ++processing_template_parmlist;
18806
  perform_access_checks (checks);
18807
  --processing_template_parmlist;
18808
}
18809
 
18810
/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18811
   `function-definition' sequence.  MEMBER_P is true, this declaration
18812
   appears in a class scope.
18813
 
18814
   Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
18815
   *FRIEND_P is set to TRUE iff the declaration is a friend.  */
18816
 
18817
static tree
18818
cp_parser_single_declaration (cp_parser* parser,
18819
                              VEC (deferred_access_check,gc)* checks,
18820
                              bool member_p,
18821
                              bool explicit_specialization_p,
18822
                              bool* friend_p)
18823
{
18824
  int declares_class_or_enum;
18825
  tree decl = NULL_TREE;
18826
  cp_decl_specifier_seq decl_specifiers;
18827
  bool function_definition_p = false;
18828
  cp_token *decl_spec_token_start;
18829
 
18830
  /* This function is only used when processing a template
18831
     declaration.  */
18832
  gcc_assert (innermost_scope_kind () == sk_template_parms
18833
              || innermost_scope_kind () == sk_template_spec);
18834
 
18835
  /* Defer access checks until we know what is being declared.  */
18836
  push_deferring_access_checks (dk_deferred);
18837
 
18838
  /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18839
     alternative.  */
18840
  decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18841
  cp_parser_decl_specifier_seq (parser,
18842
                                CP_PARSER_FLAGS_OPTIONAL,
18843
                                &decl_specifiers,
18844
                                &declares_class_or_enum);
18845
  if (friend_p)
18846
    *friend_p = cp_parser_friend_p (&decl_specifiers);
18847
 
18848
  /* There are no template typedefs.  */
18849
  if (decl_specifiers.specs[(int) ds_typedef])
18850
    {
18851
      error_at (decl_spec_token_start->location,
18852
                "template declaration of %<typedef%>");
18853
      decl = error_mark_node;
18854
    }
18855
 
18856
  /* Gather up the access checks that occurred the
18857
     decl-specifier-seq.  */
18858
  stop_deferring_access_checks ();
18859
 
18860
  /* Check for the declaration of a template class.  */
18861
  if (declares_class_or_enum)
18862
    {
18863
      if (cp_parser_declares_only_class_p (parser))
18864
        {
18865
          decl = shadow_tag (&decl_specifiers);
18866
 
18867
          /* In this case:
18868
 
18869
               struct C {
18870
                 friend template <typename T> struct A<T>::B;
18871
               };
18872
 
18873
             A<T>::B will be represented by a TYPENAME_TYPE, and
18874
             therefore not recognized by shadow_tag.  */
18875
          if (friend_p && *friend_p
18876
              && !decl
18877
              && decl_specifiers.type
18878
              && TYPE_P (decl_specifiers.type))
18879
            decl = decl_specifiers.type;
18880
 
18881
          if (decl && decl != error_mark_node)
18882
            decl = TYPE_NAME (decl);
18883
          else
18884
            decl = error_mark_node;
18885
 
18886
          /* Perform access checks for template parameters.  */
18887
          cp_parser_perform_template_parameter_access_checks (checks);
18888
        }
18889
    }
18890
 
18891
  /* Complain about missing 'typename' or other invalid type names.  */
18892
  if (!decl_specifiers.any_type_specifiers_p)
18893
    cp_parser_parse_and_diagnose_invalid_type_name (parser);
18894
 
18895
  /* If it's not a template class, try for a template function.  If
18896
     the next token is a `;', then this declaration does not declare
18897
     anything.  But, if there were errors in the decl-specifiers, then
18898
     the error might well have come from an attempted class-specifier.
18899
     In that case, there's no need to warn about a missing declarator.  */
18900
  if (!decl
18901
      && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18902
          || decl_specifiers.type != error_mark_node))
18903
    {
18904
      decl = cp_parser_init_declarator (parser,
18905
                                        &decl_specifiers,
18906
                                        checks,
18907
                                        /*function_definition_allowed_p=*/true,
18908
                                        member_p,
18909
                                        declares_class_or_enum,
18910
                                        &function_definition_p);
18911
 
18912
    /* 7.1.1-1 [dcl.stc]
18913
 
18914
       A storage-class-specifier shall not be specified in an explicit
18915
       specialization...  */
18916
    if (decl
18917
        && explicit_specialization_p
18918
        && decl_specifiers.storage_class != sc_none)
18919
      {
18920
        error_at (decl_spec_token_start->location,
18921
                  "explicit template specialization cannot have a storage class");
18922
        decl = error_mark_node;
18923
      }
18924
    }
18925
 
18926
  pop_deferring_access_checks ();
18927
 
18928
  /* Clear any current qualification; whatever comes next is the start
18929
     of something new.  */
18930
  parser->scope = NULL_TREE;
18931
  parser->qualifying_scope = NULL_TREE;
18932
  parser->object_scope = NULL_TREE;
18933
  /* Look for a trailing `;' after the declaration.  */
18934
  if (!function_definition_p
18935
      && (decl == error_mark_node
18936
          || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18937
    cp_parser_skip_to_end_of_block_or_statement (parser);
18938
 
18939
  return decl;
18940
}
18941
 
18942
/* Parse a cast-expression that is not the operand of a unary "&".  */
18943
 
18944
static tree
18945
cp_parser_simple_cast_expression (cp_parser *parser)
18946
{
18947
  return cp_parser_cast_expression (parser, /*address_p=*/false,
18948
                                    /*cast_p=*/false, NULL);
18949
}
18950
 
18951
/* Parse a functional cast to TYPE.  Returns an expression
18952
   representing the cast.  */
18953
 
18954
static tree
18955
cp_parser_functional_cast (cp_parser* parser, tree type)
18956
{
18957
  VEC(tree,gc) *vec;
18958
  tree expression_list;
18959
  tree cast;
18960
  bool nonconst_p;
18961
 
18962
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18963
    {
18964
      maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18965
      expression_list = cp_parser_braced_list (parser, &nonconst_p);
18966
      CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18967
      if (TREE_CODE (type) == TYPE_DECL)
18968
        type = TREE_TYPE (type);
18969
      return finish_compound_literal (type, expression_list);
18970
    }
18971
 
18972
 
18973
  vec = cp_parser_parenthesized_expression_list (parser, false,
18974
                                                 /*cast_p=*/true,
18975
                                                 /*allow_expansion_p=*/true,
18976
                                                 /*non_constant_p=*/NULL);
18977
  if (vec == NULL)
18978
    expression_list = error_mark_node;
18979
  else
18980
    {
18981
      expression_list = build_tree_list_vec (vec);
18982
      release_tree_vector (vec);
18983
    }
18984
 
18985
  cast = build_functional_cast (type, expression_list,
18986
                                tf_warning_or_error);
18987
  /* [expr.const]/1: In an integral constant expression "only type
18988
     conversions to integral or enumeration type can be used".  */
18989
  if (TREE_CODE (type) == TYPE_DECL)
18990
    type = TREE_TYPE (type);
18991
  if (cast != error_mark_node
18992
      && !cast_valid_in_integral_constant_expression_p (type)
18993
      && (cp_parser_non_integral_constant_expression
18994
          (parser, "a call to a constructor")))
18995
    return error_mark_node;
18996
  return cast;
18997
}
18998
 
18999
/* Save the tokens that make up the body of a member function defined
19000
   in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
19001
   already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
19002
   specifiers applied to the declaration.  Returns the FUNCTION_DECL
19003
   for the member function.  */
19004
 
19005
static tree
19006
cp_parser_save_member_function_body (cp_parser* parser,
19007
                                     cp_decl_specifier_seq *decl_specifiers,
19008
                                     cp_declarator *declarator,
19009
                                     tree attributes)
19010
{
19011
  cp_token *first;
19012
  cp_token *last;
19013
  tree fn;
19014
 
19015
  /* Create the FUNCTION_DECL.  */
19016
  fn = grokmethod (decl_specifiers, declarator, attributes);
19017
  /* If something went badly wrong, bail out now.  */
19018
  if (fn == error_mark_node)
19019
    {
19020
      /* If there's a function-body, skip it.  */
19021
      if (cp_parser_token_starts_function_definition_p
19022
          (cp_lexer_peek_token (parser->lexer)))
19023
        cp_parser_skip_to_end_of_block_or_statement (parser);
19024
      return error_mark_node;
19025
    }
19026
 
19027
  /* Remember it, if there default args to post process.  */
19028
  cp_parser_save_default_args (parser, fn);
19029
 
19030
  /* Save away the tokens that make up the body of the
19031
     function.  */
19032
  first = parser->lexer->next_token;
19033
  /* We can have braced-init-list mem-initializers before the fn body.  */
19034
  if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19035
    {
19036
      cp_lexer_consume_token (parser->lexer);
19037
      while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19038
             && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19039
        {
19040
          /* cache_group will stop after an un-nested { } pair, too.  */
19041
          if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19042
            break;
19043
 
19044
          /* variadic mem-inits have ... after the ')'.  */
19045
          if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19046
            cp_lexer_consume_token (parser->lexer);
19047
        }
19048
    }
19049
  cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19050
  /* Handle function try blocks.  */
19051
  while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19052
    cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19053
  last = parser->lexer->next_token;
19054
 
19055
  /* Save away the inline definition; we will process it when the
19056
     class is complete.  */
19057
  DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19058
  DECL_PENDING_INLINE_P (fn) = 1;
19059
 
19060
  /* We need to know that this was defined in the class, so that
19061
     friend templates are handled correctly.  */
19062
  DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19063
 
19064
  /* Add FN to the queue of functions to be parsed later.  */
19065
  TREE_VALUE (parser->unparsed_functions_queues)
19066
    = tree_cons (NULL_TREE, fn,
19067
                 TREE_VALUE (parser->unparsed_functions_queues));
19068
 
19069
  return fn;
19070
}
19071
 
19072
/* Parse a template-argument-list, as well as the trailing ">" (but
19073
   not the opening ">").  See cp_parser_template_argument_list for the
19074
   return value.  */
19075
 
19076
static tree
19077
cp_parser_enclosed_template_argument_list (cp_parser* parser)
19078
{
19079
  tree arguments;
19080
  tree saved_scope;
19081
  tree saved_qualifying_scope;
19082
  tree saved_object_scope;
19083
  bool saved_greater_than_is_operator_p;
19084
  int saved_unevaluated_operand;
19085
  int saved_inhibit_evaluation_warnings;
19086
 
19087
  /* [temp.names]
19088
 
19089
     When parsing a template-id, the first non-nested `>' is taken as
19090
     the end of the template-argument-list rather than a greater-than
19091
     operator.  */
19092
  saved_greater_than_is_operator_p
19093
    = parser->greater_than_is_operator_p;
19094
  parser->greater_than_is_operator_p = false;
19095
  /* Parsing the argument list may modify SCOPE, so we save it
19096
     here.  */
19097
  saved_scope = parser->scope;
19098
  saved_qualifying_scope = parser->qualifying_scope;
19099
  saved_object_scope = parser->object_scope;
19100
  /* We need to evaluate the template arguments, even though this
19101
     template-id may be nested within a "sizeof".  */
19102
  saved_unevaluated_operand = cp_unevaluated_operand;
19103
  cp_unevaluated_operand = 0;
19104
  saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19105
  c_inhibit_evaluation_warnings = 0;
19106
  /* Parse the template-argument-list itself.  */
19107
  if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19108
      || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19109
    arguments = NULL_TREE;
19110
  else
19111
    arguments = cp_parser_template_argument_list (parser);
19112
  /* Look for the `>' that ends the template-argument-list. If we find
19113
     a '>>' instead, it's probably just a typo.  */
19114
  if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19115
    {
19116
      if (cxx_dialect != cxx98)
19117
        {
19118
          /* In C++0x, a `>>' in a template argument list or cast
19119
             expression is considered to be two separate `>'
19120
             tokens. So, change the current token to a `>', but don't
19121
             consume it: it will be consumed later when the outer
19122
             template argument list (or cast expression) is parsed.
19123
             Note that this replacement of `>' for `>>' is necessary
19124
             even if we are parsing tentatively: in the tentative
19125
             case, after calling
19126
             cp_parser_enclosed_template_argument_list we will always
19127
             throw away all of the template arguments and the first
19128
             closing `>', either because the template argument list
19129
             was erroneous or because we are replacing those tokens
19130
             with a CPP_TEMPLATE_ID token.  The second `>' (which will
19131
             not have been thrown away) is needed either to close an
19132
             outer template argument list or to complete a new-style
19133
             cast.  */
19134
          cp_token *token = cp_lexer_peek_token (parser->lexer);
19135
          token->type = CPP_GREATER;
19136
        }
19137
      else if (!saved_greater_than_is_operator_p)
19138
        {
19139
          /* If we're in a nested template argument list, the '>>' has
19140
            to be a typo for '> >'. We emit the error message, but we
19141
            continue parsing and we push a '>' as next token, so that
19142
            the argument list will be parsed correctly.  Note that the
19143
            global source location is still on the token before the
19144
            '>>', so we need to say explicitly where we want it.  */
19145
          cp_token *token = cp_lexer_peek_token (parser->lexer);
19146
          error_at (token->location, "%<>>%> should be %<> >%> "
19147
                    "within a nested template argument list");
19148
 
19149
          token->type = CPP_GREATER;
19150
        }
19151
      else
19152
        {
19153
          /* If this is not a nested template argument list, the '>>'
19154
            is a typo for '>'. Emit an error message and continue.
19155
            Same deal about the token location, but here we can get it
19156
            right by consuming the '>>' before issuing the diagnostic.  */
19157
          cp_token *token = cp_lexer_consume_token (parser->lexer);
19158
          error_at (token->location,
19159
                    "spurious %<>>%>, use %<>%> to terminate "
19160
                    "a template argument list");
19161
        }
19162
    }
19163
  else
19164
    cp_parser_skip_to_end_of_template_parameter_list (parser);
19165
  /* The `>' token might be a greater-than operator again now.  */
19166
  parser->greater_than_is_operator_p
19167
    = saved_greater_than_is_operator_p;
19168
  /* Restore the SAVED_SCOPE.  */
19169
  parser->scope = saved_scope;
19170
  parser->qualifying_scope = saved_qualifying_scope;
19171
  parser->object_scope = saved_object_scope;
19172
  cp_unevaluated_operand = saved_unevaluated_operand;
19173
  c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19174
 
19175
  return arguments;
19176
}
19177
 
19178
/* MEMBER_FUNCTION is a member function, or a friend.  If default
19179
   arguments, or the body of the function have not yet been parsed,
19180
   parse them now.  */
19181
 
19182
static void
19183
cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19184
{
19185
  /* If this member is a template, get the underlying
19186
     FUNCTION_DECL.  */
19187
  if (DECL_FUNCTION_TEMPLATE_P (member_function))
19188
    member_function = DECL_TEMPLATE_RESULT (member_function);
19189
 
19190
  /* There should not be any class definitions in progress at this
19191
     point; the bodies of members are only parsed outside of all class
19192
     definitions.  */
19193
  gcc_assert (parser->num_classes_being_defined == 0);
19194
  /* While we're parsing the member functions we might encounter more
19195
     classes.  We want to handle them right away, but we don't want
19196
     them getting mixed up with functions that are currently in the
19197
     queue.  */
19198
  parser->unparsed_functions_queues
19199
    = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19200
 
19201
  /* Make sure that any template parameters are in scope.  */
19202
  maybe_begin_member_template_processing (member_function);
19203
 
19204
  /* If the body of the function has not yet been parsed, parse it
19205
     now.  */
19206
  if (DECL_PENDING_INLINE_P (member_function))
19207
    {
19208
      tree function_scope;
19209
      cp_token_cache *tokens;
19210
 
19211
      /* The function is no longer pending; we are processing it.  */
19212
      tokens = DECL_PENDING_INLINE_INFO (member_function);
19213
      DECL_PENDING_INLINE_INFO (member_function) = NULL;
19214
      DECL_PENDING_INLINE_P (member_function) = 0;
19215
 
19216
      /* If this is a local class, enter the scope of the containing
19217
         function.  */
19218
      function_scope = current_function_decl;
19219
      if (function_scope)
19220
        push_function_context ();
19221
 
19222
      /* Push the body of the function onto the lexer stack.  */
19223
      cp_parser_push_lexer_for_tokens (parser, tokens);
19224
 
19225
      /* Let the front end know that we going to be defining this
19226
         function.  */
19227
      start_preparsed_function (member_function, NULL_TREE,
19228
                                SF_PRE_PARSED | SF_INCLASS_INLINE);
19229
 
19230
      /* Don't do access checking if it is a templated function.  */
19231
      if (processing_template_decl)
19232
        push_deferring_access_checks (dk_no_check);
19233
 
19234
      /* Now, parse the body of the function.  */
19235
      cp_parser_function_definition_after_declarator (parser,
19236
                                                      /*inline_p=*/true);
19237
 
19238
      if (processing_template_decl)
19239
        pop_deferring_access_checks ();
19240
 
19241
      /* Leave the scope of the containing function.  */
19242
      if (function_scope)
19243
        pop_function_context ();
19244
      cp_parser_pop_lexer (parser);
19245
    }
19246
 
19247
  /* Remove any template parameters from the symbol table.  */
19248
  maybe_end_member_template_processing ();
19249
 
19250
  /* Restore the queue.  */
19251
  parser->unparsed_functions_queues
19252
    = TREE_CHAIN (parser->unparsed_functions_queues);
19253
}
19254
 
19255
/* If DECL contains any default args, remember it on the unparsed
19256
   functions queue.  */
19257
 
19258
static void
19259
cp_parser_save_default_args (cp_parser* parser, tree decl)
19260
{
19261
  tree probe;
19262
 
19263
  for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19264
       probe;
19265
       probe = TREE_CHAIN (probe))
19266
    if (TREE_PURPOSE (probe))
19267
      {
19268
        TREE_PURPOSE (parser->unparsed_functions_queues)
19269
          = tree_cons (current_class_type, decl,
19270
                       TREE_PURPOSE (parser->unparsed_functions_queues));
19271
        break;
19272
      }
19273
}
19274
 
19275
/* FN is a FUNCTION_DECL which may contains a parameter with an
19276
   unparsed DEFAULT_ARG.  Parse the default args now.  This function
19277
   assumes that the current scope is the scope in which the default
19278
   argument should be processed.  */
19279
 
19280
static void
19281
cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19282
{
19283
  bool saved_local_variables_forbidden_p;
19284
  tree parm, parmdecl;
19285
 
19286
  /* While we're parsing the default args, we might (due to the
19287
     statement expression extension) encounter more classes.  We want
19288
     to handle them right away, but we don't want them getting mixed
19289
     up with default args that are currently in the queue.  */
19290
  parser->unparsed_functions_queues
19291
    = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19292
 
19293
  /* Local variable names (and the `this' keyword) may not appear
19294
     in a default argument.  */
19295
  saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19296
  parser->local_variables_forbidden_p = true;
19297
 
19298
  for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19299
         parmdecl = DECL_ARGUMENTS (fn);
19300
       parm && parm != void_list_node;
19301
       parm = TREE_CHAIN (parm),
19302
         parmdecl = TREE_CHAIN (parmdecl))
19303
    {
19304
      cp_token_cache *tokens;
19305
      tree default_arg = TREE_PURPOSE (parm);
19306
      tree parsed_arg;
19307
      VEC(tree,gc) *insts;
19308
      tree copy;
19309
      unsigned ix;
19310
 
19311
      if (!default_arg)
19312
        continue;
19313
 
19314
      if (TREE_CODE (default_arg) != DEFAULT_ARG)
19315
        /* This can happen for a friend declaration for a function
19316
           already declared with default arguments.  */
19317
        continue;
19318
 
19319
       /* Push the saved tokens for the default argument onto the parser's
19320
          lexer stack.  */
19321
      tokens = DEFARG_TOKENS (default_arg);
19322
      cp_parser_push_lexer_for_tokens (parser, tokens);
19323
 
19324
      start_lambda_scope (parmdecl);
19325
 
19326
      /* Parse the assignment-expression.  */
19327
      parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19328
      if (parsed_arg == error_mark_node)
19329
        {
19330
          cp_parser_pop_lexer (parser);
19331
          continue;
19332
        }
19333
 
19334
      if (!processing_template_decl)
19335
        parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19336
 
19337
      TREE_PURPOSE (parm) = parsed_arg;
19338
 
19339
      /* Update any instantiations we've already created.  */
19340
      for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19341
           VEC_iterate (tree, insts, ix, copy); ix++)
19342
        TREE_PURPOSE (copy) = parsed_arg;
19343
 
19344
      finish_lambda_scope ();
19345
 
19346
      /* If the token stream has not been completely used up, then
19347
         there was extra junk after the end of the default
19348
         argument.  */
19349
      if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19350
        cp_parser_error (parser, "expected %<,%>");
19351
 
19352
      /* Revert to the main lexer.  */
19353
      cp_parser_pop_lexer (parser);
19354
    }
19355
 
19356
  /* Make sure no default arg is missing.  */
19357
  check_default_args (fn);
19358
 
19359
  /* Restore the state of local_variables_forbidden_p.  */
19360
  parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19361
 
19362
  /* Restore the queue.  */
19363
  parser->unparsed_functions_queues
19364
    = TREE_CHAIN (parser->unparsed_functions_queues);
19365
}
19366
 
19367
/* Parse the operand of `sizeof' (or a similar operator).  Returns
19368
   either a TYPE or an expression, depending on the form of the
19369
   input.  The KEYWORD indicates which kind of expression we have
19370
   encountered.  */
19371
 
19372
static tree
19373
cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19374
{
19375
  tree expr = NULL_TREE;
19376
  const char *saved_message;
19377
  char *tmp;
19378
  bool saved_integral_constant_expression_p;
19379
  bool saved_non_integral_constant_expression_p;
19380
  bool pack_expansion_p = false;
19381
 
19382
  /* Types cannot be defined in a `sizeof' expression.  Save away the
19383
     old message.  */
19384
  saved_message = parser->type_definition_forbidden_message;
19385
  /* And create the new one.  */
19386
  tmp = concat ("types may not be defined in %<",
19387
                IDENTIFIER_POINTER (ridpointers[keyword]),
19388
                "%> expressions", NULL);
19389
  parser->type_definition_forbidden_message = tmp;
19390
 
19391
  /* The restrictions on constant-expressions do not apply inside
19392
     sizeof expressions.  */
19393
  saved_integral_constant_expression_p
19394
    = parser->integral_constant_expression_p;
19395
  saved_non_integral_constant_expression_p
19396
    = parser->non_integral_constant_expression_p;
19397
  parser->integral_constant_expression_p = false;
19398
 
19399
  /* If it's a `...', then we are computing the length of a parameter
19400
     pack.  */
19401
  if (keyword == RID_SIZEOF
19402
      && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19403
    {
19404
      /* Consume the `...'.  */
19405
      cp_lexer_consume_token (parser->lexer);
19406
      maybe_warn_variadic_templates ();
19407
 
19408
      /* Note that this is an expansion.  */
19409
      pack_expansion_p = true;
19410
    }
19411
 
19412
  /* Do not actually evaluate the expression.  */
19413
  ++cp_unevaluated_operand;
19414
  ++c_inhibit_evaluation_warnings;
19415
  /* If it's a `(', then we might be looking at the type-id
19416
     construction.  */
19417
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19418
    {
19419
      tree type;
19420
      bool saved_in_type_id_in_expr_p;
19421
 
19422
      /* We can't be sure yet whether we're looking at a type-id or an
19423
         expression.  */
19424
      cp_parser_parse_tentatively (parser);
19425
      /* Consume the `('.  */
19426
      cp_lexer_consume_token (parser->lexer);
19427
      /* Parse the type-id.  */
19428
      saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19429
      parser->in_type_id_in_expr_p = true;
19430
      type = cp_parser_type_id (parser);
19431
      parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19432
      /* Now, look for the trailing `)'.  */
19433
      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19434
      /* If all went well, then we're done.  */
19435
      if (cp_parser_parse_definitely (parser))
19436
        {
19437
          cp_decl_specifier_seq decl_specs;
19438
 
19439
          /* Build a trivial decl-specifier-seq.  */
19440
          clear_decl_specs (&decl_specs);
19441
          decl_specs.type = type;
19442
 
19443
          /* Call grokdeclarator to figure out what type this is.  */
19444
          expr = grokdeclarator (NULL,
19445
                                 &decl_specs,
19446
                                 TYPENAME,
19447
                                 /*initialized=*/0,
19448
                                 /*attrlist=*/NULL);
19449
        }
19450
    }
19451
 
19452
  /* If the type-id production did not work out, then we must be
19453
     looking at the unary-expression production.  */
19454
  if (!expr)
19455
    expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19456
                                       /*cast_p=*/false, NULL);
19457
 
19458
  if (pack_expansion_p)
19459
    /* Build a pack expansion. */
19460
    expr = make_pack_expansion (expr);
19461
 
19462
  /* Go back to evaluating expressions.  */
19463
  --cp_unevaluated_operand;
19464
  --c_inhibit_evaluation_warnings;
19465
 
19466
  /* Free the message we created.  */
19467
  free (tmp);
19468
  /* And restore the old one.  */
19469
  parser->type_definition_forbidden_message = saved_message;
19470
  parser->integral_constant_expression_p
19471
    = saved_integral_constant_expression_p;
19472
  parser->non_integral_constant_expression_p
19473
    = saved_non_integral_constant_expression_p;
19474
 
19475
  return expr;
19476
}
19477
 
19478
/* If the current declaration has no declarator, return true.  */
19479
 
19480
static bool
19481
cp_parser_declares_only_class_p (cp_parser *parser)
19482
{
19483
  /* If the next token is a `;' or a `,' then there is no
19484
     declarator.  */
19485
  return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19486
          || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19487
}
19488
 
19489
/* Update the DECL_SPECS to reflect the storage class indicated by
19490
   KEYWORD.  */
19491
 
19492
static void
19493
cp_parser_set_storage_class (cp_parser *parser,
19494
                             cp_decl_specifier_seq *decl_specs,
19495
                             enum rid keyword,
19496
                             location_t location)
19497
{
19498
  cp_storage_class storage_class;
19499
 
19500
  if (parser->in_unbraced_linkage_specification_p)
19501
    {
19502
      error_at (location, "invalid use of %qD in linkage specification",
19503
                ridpointers[keyword]);
19504
      return;
19505
    }
19506
  else if (decl_specs->storage_class != sc_none)
19507
    {
19508
      decl_specs->conflicting_specifiers_p = true;
19509
      return;
19510
    }
19511
 
19512
  if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19513
      && decl_specs->specs[(int) ds_thread])
19514
    {
19515
      error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19516
      decl_specs->specs[(int) ds_thread] = 0;
19517
    }
19518
 
19519
  switch (keyword)
19520
    {
19521
    case RID_AUTO:
19522
      storage_class = sc_auto;
19523
      break;
19524
    case RID_REGISTER:
19525
      storage_class = sc_register;
19526
      break;
19527
    case RID_STATIC:
19528
      storage_class = sc_static;
19529
      break;
19530
    case RID_EXTERN:
19531
      storage_class = sc_extern;
19532
      break;
19533
    case RID_MUTABLE:
19534
      storage_class = sc_mutable;
19535
      break;
19536
    default:
19537
      gcc_unreachable ();
19538
    }
19539
  decl_specs->storage_class = storage_class;
19540
 
19541
  /* A storage class specifier cannot be applied alongside a typedef
19542
     specifier. If there is a typedef specifier present then set
19543
     conflicting_specifiers_p which will trigger an error later
19544
     on in grokdeclarator. */
19545
  if (decl_specs->specs[(int)ds_typedef])
19546
    decl_specs->conflicting_specifiers_p = true;
19547
}
19548
 
19549
/* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
19550
   is true, the type is a user-defined type; otherwise it is a
19551
   built-in type specified by a keyword.  */
19552
 
19553
static void
19554
cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19555
                              tree type_spec,
19556
                              location_t location,
19557
                              bool user_defined_p)
19558
{
19559
  decl_specs->any_specifiers_p = true;
19560
 
19561
  /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19562
     (with, for example, in "typedef int wchar_t;") we remember that
19563
     this is what happened.  In system headers, we ignore these
19564
     declarations so that G++ can work with system headers that are not
19565
     C++-safe.  */
19566
  if (decl_specs->specs[(int) ds_typedef]
19567
      && !user_defined_p
19568
      && (type_spec == boolean_type_node
19569
          || type_spec == char16_type_node
19570
          || type_spec == char32_type_node
19571
          || type_spec == wchar_type_node)
19572
      && (decl_specs->type
19573
          || decl_specs->specs[(int) ds_long]
19574
          || decl_specs->specs[(int) ds_short]
19575
          || decl_specs->specs[(int) ds_unsigned]
19576
          || decl_specs->specs[(int) ds_signed]))
19577
    {
19578
      decl_specs->redefined_builtin_type = type_spec;
19579
      if (!decl_specs->type)
19580
        {
19581
          decl_specs->type = type_spec;
19582
          decl_specs->user_defined_type_p = false;
19583
          decl_specs->type_location = location;
19584
        }
19585
    }
19586
  else if (decl_specs->type)
19587
    decl_specs->multiple_types_p = true;
19588
  else
19589
    {
19590
      decl_specs->type = type_spec;
19591
      decl_specs->user_defined_type_p = user_defined_p;
19592
      decl_specs->redefined_builtin_type = NULL_TREE;
19593
      decl_specs->type_location = location;
19594
    }
19595
}
19596
 
19597
/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19598
   Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
19599
 
19600
static bool
19601
cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19602
{
19603
  return decl_specifiers->specs[(int) ds_friend] != 0;
19604
}
19605
 
19606
/* If the next token is of the indicated TYPE, consume it.  Otherwise,
19607
   issue an error message indicating that TOKEN_DESC was expected.
19608
 
19609
   Returns the token consumed, if the token had the appropriate type.
19610
   Otherwise, returns NULL.  */
19611
 
19612
static cp_token *
19613
cp_parser_require (cp_parser* parser,
19614
                   enum cpp_ttype type,
19615
                   const char* token_desc)
19616
{
19617
  if (cp_lexer_next_token_is (parser->lexer, type))
19618
    return cp_lexer_consume_token (parser->lexer);
19619
  else
19620
    {
19621
      /* Output the MESSAGE -- unless we're parsing tentatively.  */
19622
      if (!cp_parser_simulate_error (parser))
19623
        {
19624
          char *message = concat ("expected ", token_desc, NULL);
19625
          cp_parser_error (parser, message);
19626
          free (message);
19627
        }
19628
      return NULL;
19629
    }
19630
}
19631
 
19632
/* An error message is produced if the next token is not '>'.
19633
   All further tokens are skipped until the desired token is
19634
   found or '{', '}', ';' or an unbalanced ')' or ']'.  */
19635
 
19636
static void
19637
cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19638
{
19639
  /* Current level of '< ... >'.  */
19640
  unsigned level = 0;
19641
  /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
19642
  unsigned nesting_depth = 0;
19643
 
19644
  /* Are we ready, yet?  If not, issue error message.  */
19645
  if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19646
    return;
19647
 
19648
  /* Skip tokens until the desired token is found.  */
19649
  while (true)
19650
    {
19651
      /* Peek at the next token.  */
19652
      switch (cp_lexer_peek_token (parser->lexer)->type)
19653
        {
19654
        case CPP_LESS:
19655
          if (!nesting_depth)
19656
            ++level;
19657
          break;
19658
 
19659
        case CPP_RSHIFT:
19660
          if (cxx_dialect == cxx98)
19661
            /* C++0x views the `>>' operator as two `>' tokens, but
19662
               C++98 does not. */
19663
            break;
19664
          else if (!nesting_depth && level-- == 0)
19665
            {
19666
              /* We've hit a `>>' where the first `>' closes the
19667
                 template argument list, and the second `>' is
19668
                 spurious.  Just consume the `>>' and stop; we've
19669
                 already produced at least one error.  */
19670
              cp_lexer_consume_token (parser->lexer);
19671
              return;
19672
            }
19673
          /* Fall through for C++0x, so we handle the second `>' in
19674
             the `>>'.  */
19675
 
19676
        case CPP_GREATER:
19677
          if (!nesting_depth && level-- == 0)
19678
            {
19679
              /* We've reached the token we want, consume it and stop.  */
19680
              cp_lexer_consume_token (parser->lexer);
19681
              return;
19682
            }
19683
          break;
19684
 
19685
        case CPP_OPEN_PAREN:
19686
        case CPP_OPEN_SQUARE:
19687
          ++nesting_depth;
19688
          break;
19689
 
19690
        case CPP_CLOSE_PAREN:
19691
        case CPP_CLOSE_SQUARE:
19692
          if (nesting_depth-- == 0)
19693
            return;
19694
          break;
19695
 
19696
        case CPP_EOF:
19697
        case CPP_PRAGMA_EOL:
19698
        case CPP_SEMICOLON:
19699
        case CPP_OPEN_BRACE:
19700
        case CPP_CLOSE_BRACE:
19701
          /* The '>' was probably forgotten, don't look further.  */
19702
          return;
19703
 
19704
        default:
19705
          break;
19706
        }
19707
 
19708
      /* Consume this token.  */
19709
      cp_lexer_consume_token (parser->lexer);
19710
    }
19711
}
19712
 
19713
/* If the next token is the indicated keyword, consume it.  Otherwise,
19714
   issue an error message indicating that TOKEN_DESC was expected.
19715
 
19716
   Returns the token consumed, if the token had the appropriate type.
19717
   Otherwise, returns NULL.  */
19718
 
19719
static cp_token *
19720
cp_parser_require_keyword (cp_parser* parser,
19721
                           enum rid keyword,
19722
                           const char* token_desc)
19723
{
19724
  cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19725
 
19726
  if (token && token->keyword != keyword)
19727
    {
19728
      dyn_string_t error_msg;
19729
 
19730
      /* Format the error message.  */
19731
      error_msg = dyn_string_new (0);
19732
      dyn_string_append_cstr (error_msg, "expected ");
19733
      dyn_string_append_cstr (error_msg, token_desc);
19734
      cp_parser_error (parser, error_msg->s);
19735
      dyn_string_delete (error_msg);
19736
      return NULL;
19737
    }
19738
 
19739
  return token;
19740
}
19741
 
19742
/* Returns TRUE iff TOKEN is a token that can begin the body of a
19743
   function-definition.  */
19744
 
19745
static bool
19746
cp_parser_token_starts_function_definition_p (cp_token* token)
19747
{
19748
  return (/* An ordinary function-body begins with an `{'.  */
19749
          token->type == CPP_OPEN_BRACE
19750
          /* A ctor-initializer begins with a `:'.  */
19751
          || token->type == CPP_COLON
19752
          /* A function-try-block begins with `try'.  */
19753
          || token->keyword == RID_TRY
19754
          /* The named return value extension begins with `return'.  */
19755
          || token->keyword == RID_RETURN);
19756
}
19757
 
19758
/* Returns TRUE iff the next token is the ":" or "{" beginning a class
19759
   definition.  */
19760
 
19761
static bool
19762
cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19763
{
19764
  cp_token *token;
19765
 
19766
  token = cp_lexer_peek_token (parser->lexer);
19767
  return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19768
}
19769
 
19770
/* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19771
   C++0x) ending a template-argument.  */
19772
 
19773
static bool
19774
cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19775
{
19776
  cp_token *token;
19777
 
19778
  token = cp_lexer_peek_token (parser->lexer);
19779
  return (token->type == CPP_COMMA
19780
          || token->type == CPP_GREATER
19781
          || token->type == CPP_ELLIPSIS
19782
          || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19783
}
19784
 
19785
/* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19786
   (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
19787
 
19788
static bool
19789
cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19790
                                                     size_t n)
19791
{
19792
  cp_token *token;
19793
 
19794
  token = cp_lexer_peek_nth_token (parser->lexer, n);
19795
  if (token->type == CPP_LESS)
19796
    return true;
19797
  /* Check for the sequence `<::' in the original code. It would be lexed as
19798
     `[:', where `[' is a digraph, and there is no whitespace before
19799
     `:'.  */
19800
  if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19801
    {
19802
      cp_token *token2;
19803
      token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19804
      if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19805
        return true;
19806
    }
19807
  return false;
19808
}
19809
 
19810
/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19811
   or none_type otherwise.  */
19812
 
19813
static enum tag_types
19814
cp_parser_token_is_class_key (cp_token* token)
19815
{
19816
  switch (token->keyword)
19817
    {
19818
    case RID_CLASS:
19819
      return class_type;
19820
    case RID_STRUCT:
19821
      return record_type;
19822
    case RID_UNION:
19823
      return union_type;
19824
 
19825
    default:
19826
      return none_type;
19827
    }
19828
}
19829
 
19830
/* Issue an error message if the CLASS_KEY does not match the TYPE.  */
19831
 
19832
static void
19833
cp_parser_check_class_key (enum tag_types class_key, tree type)
19834
{
19835
  if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19836
    permerror (input_location, "%qs tag used in naming %q#T",
19837
            class_key == union_type ? "union"
19838
             : class_key == record_type ? "struct" : "class",
19839
             type);
19840
}
19841
 
19842
/* Issue an error message if DECL is redeclared with different
19843
   access than its original declaration [class.access.spec/3].
19844
   This applies to nested classes and nested class templates.
19845
   [class.mem/1].  */
19846
 
19847
static void
19848
cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19849
{
19850
  if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19851
    return;
19852
 
19853
  if ((TREE_PRIVATE (decl)
19854
       != (current_access_specifier == access_private_node))
19855
      || (TREE_PROTECTED (decl)
19856
          != (current_access_specifier == access_protected_node)))
19857
    error_at (location, "%qD redeclared with different access", decl);
19858
}
19859
 
19860
/* Look for the `template' keyword, as a syntactic disambiguator.
19861
   Return TRUE iff it is present, in which case it will be
19862
   consumed.  */
19863
 
19864
static bool
19865
cp_parser_optional_template_keyword (cp_parser *parser)
19866
{
19867
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19868
    {
19869
      /* The `template' keyword can only be used within templates;
19870
         outside templates the parser can always figure out what is a
19871
         template and what is not.  */
19872
      if (!processing_template_decl)
19873
        {
19874
          cp_token *token = cp_lexer_peek_token (parser->lexer);
19875
          error_at (token->location,
19876
                    "%<template%> (as a disambiguator) is only allowed "
19877
                    "within templates");
19878
          /* If this part of the token stream is rescanned, the same
19879
             error message would be generated.  So, we purge the token
19880
             from the stream.  */
19881
          cp_lexer_purge_token (parser->lexer);
19882
          return false;
19883
        }
19884
      else
19885
        {
19886
          /* Consume the `template' keyword.  */
19887
          cp_lexer_consume_token (parser->lexer);
19888
          return true;
19889
        }
19890
    }
19891
 
19892
  return false;
19893
}
19894
 
19895
/* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
19896
   set PARSER->SCOPE, and perform other related actions.  */
19897
 
19898
static void
19899
cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19900
{
19901
  int i;
19902
  struct tree_check *check_value;
19903
  deferred_access_check *chk;
19904
  VEC (deferred_access_check,gc) *checks;
19905
 
19906
  /* Get the stored value.  */
19907
  check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19908
  /* Perform any access checks that were deferred.  */
19909
  checks = check_value->checks;
19910
  if (checks)
19911
    {
19912
      for (i = 0 ;
19913
           VEC_iterate (deferred_access_check, checks, i, chk) ;
19914
           ++i)
19915
        {
19916
          perform_or_defer_access_check (chk->binfo,
19917
                                         chk->decl,
19918
                                         chk->diag_decl);
19919
        }
19920
    }
19921
  /* Set the scope from the stored value.  */
19922
  parser->scope = check_value->value;
19923
  parser->qualifying_scope = check_value->qualifying_scope;
19924
  parser->object_scope = NULL_TREE;
19925
}
19926
 
19927
/* Consume tokens up through a non-nested END token.  Returns TRUE if we
19928
   encounter the end of a block before what we were looking for.  */
19929
 
19930
static bool
19931
cp_parser_cache_group (cp_parser *parser,
19932
                       enum cpp_ttype end,
19933
                       unsigned depth)
19934
{
19935
  while (true)
19936
    {
19937
      cp_token *token = cp_lexer_peek_token (parser->lexer);
19938
 
19939
      /* Abort a parenthesized expression if we encounter a semicolon.  */
19940
      if ((end == CPP_CLOSE_PAREN || depth == 0)
19941
          && token->type == CPP_SEMICOLON)
19942
        return true;
19943
      /* If we've reached the end of the file, stop.  */
19944
      if (token->type == CPP_EOF
19945
          || (end != CPP_PRAGMA_EOL
19946
              && token->type == CPP_PRAGMA_EOL))
19947
        return true;
19948
      if (token->type == CPP_CLOSE_BRACE && depth == 0)
19949
        /* We've hit the end of an enclosing block, so there's been some
19950
           kind of syntax error.  */
19951
        return true;
19952
 
19953
      /* Consume the token.  */
19954
      cp_lexer_consume_token (parser->lexer);
19955
      /* See if it starts a new group.  */
19956
      if (token->type == CPP_OPEN_BRACE)
19957
        {
19958
          cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19959
          /* In theory this should probably check end == '}', but
19960
             cp_parser_save_member_function_body needs it to exit
19961
             after either '}' or ')' when called with ')'.  */
19962
          if (depth == 0)
19963
            return false;
19964
        }
19965
      else if (token->type == CPP_OPEN_PAREN)
19966
        {
19967
          cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19968
          if (depth == 0 && end == CPP_CLOSE_PAREN)
19969
            return false;
19970
        }
19971
      else if (token->type == CPP_PRAGMA)
19972
        cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19973
      else if (token->type == end)
19974
        return false;
19975
    }
19976
}
19977
 
19978
/* Begin parsing tentatively.  We always save tokens while parsing
19979
   tentatively so that if the tentative parsing fails we can restore the
19980
   tokens.  */
19981
 
19982
static void
19983
cp_parser_parse_tentatively (cp_parser* parser)
19984
{
19985
  /* Enter a new parsing context.  */
19986
  parser->context = cp_parser_context_new (parser->context);
19987
  /* Begin saving tokens.  */
19988
  cp_lexer_save_tokens (parser->lexer);
19989
  /* In order to avoid repetitive access control error messages,
19990
     access checks are queued up until we are no longer parsing
19991
     tentatively.  */
19992
  push_deferring_access_checks (dk_deferred);
19993
}
19994
 
19995
/* Commit to the currently active tentative parse.  */
19996
 
19997
static void
19998
cp_parser_commit_to_tentative_parse (cp_parser* parser)
19999
{
20000
  cp_parser_context *context;
20001
  cp_lexer *lexer;
20002
 
20003
  /* Mark all of the levels as committed.  */
20004
  lexer = parser->lexer;
20005
  for (context = parser->context; context->next; context = context->next)
20006
    {
20007
      if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
20008
        break;
20009
      context->status = CP_PARSER_STATUS_KIND_COMMITTED;
20010
      while (!cp_lexer_saving_tokens (lexer))
20011
        lexer = lexer->next;
20012
      cp_lexer_commit_tokens (lexer);
20013
    }
20014
}
20015
 
20016
/* Abort the currently active tentative parse.  All consumed tokens
20017
   will be rolled back, and no diagnostics will be issued.  */
20018
 
20019
static void
20020
cp_parser_abort_tentative_parse (cp_parser* parser)
20021
{
20022
  cp_parser_simulate_error (parser);
20023
  /* Now, pretend that we want to see if the construct was
20024
     successfully parsed.  */
20025
  cp_parser_parse_definitely (parser);
20026
}
20027
 
20028
/* Stop parsing tentatively.  If a parse error has occurred, restore the
20029
   token stream.  Otherwise, commit to the tokens we have consumed.
20030
   Returns true if no error occurred; false otherwise.  */
20031
 
20032
static bool
20033
cp_parser_parse_definitely (cp_parser* parser)
20034
{
20035
  bool error_occurred;
20036
  cp_parser_context *context;
20037
 
20038
  /* Remember whether or not an error occurred, since we are about to
20039
     destroy that information.  */
20040
  error_occurred = cp_parser_error_occurred (parser);
20041
  /* Remove the topmost context from the stack.  */
20042
  context = parser->context;
20043
  parser->context = context->next;
20044
  /* If no parse errors occurred, commit to the tentative parse.  */
20045
  if (!error_occurred)
20046
    {
20047
      /* Commit to the tokens read tentatively, unless that was
20048
         already done.  */
20049
      if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20050
        cp_lexer_commit_tokens (parser->lexer);
20051
 
20052
      pop_to_parent_deferring_access_checks ();
20053
    }
20054
  /* Otherwise, if errors occurred, roll back our state so that things
20055
     are just as they were before we began the tentative parse.  */
20056
  else
20057
    {
20058
      cp_lexer_rollback_tokens (parser->lexer);
20059
      pop_deferring_access_checks ();
20060
    }
20061
  /* Add the context to the front of the free list.  */
20062
  context->next = cp_parser_context_free_list;
20063
  cp_parser_context_free_list = context;
20064
 
20065
  return !error_occurred;
20066
}
20067
 
20068
/* Returns true if we are parsing tentatively and are not committed to
20069
   this tentative parse.  */
20070
 
20071
static bool
20072
cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20073
{
20074
  return (cp_parser_parsing_tentatively (parser)
20075
          && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20076
}
20077
 
20078
/* Returns nonzero iff an error has occurred during the most recent
20079
   tentative parse.  */
20080
 
20081
static bool
20082
cp_parser_error_occurred (cp_parser* parser)
20083
{
20084
  return (cp_parser_parsing_tentatively (parser)
20085
          && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20086
}
20087
 
20088
/* Returns nonzero if GNU extensions are allowed.  */
20089
 
20090
static bool
20091
cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20092
{
20093
  return parser->allow_gnu_extensions_p;
20094
}
20095
 
20096
/* Objective-C++ Productions */
20097
 
20098
 
20099
/* Parse an Objective-C expression, which feeds into a primary-expression
20100
   above.
20101
 
20102
   objc-expression:
20103
     objc-message-expression
20104
     objc-string-literal
20105
     objc-encode-expression
20106
     objc-protocol-expression
20107
     objc-selector-expression
20108
 
20109
  Returns a tree representation of the expression.  */
20110
 
20111
static tree
20112
cp_parser_objc_expression (cp_parser* parser)
20113
{
20114
  /* Try to figure out what kind of declaration is present.  */
20115
  cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20116
 
20117
  switch (kwd->type)
20118
    {
20119
    case CPP_OPEN_SQUARE:
20120
      return cp_parser_objc_message_expression (parser);
20121
 
20122
    case CPP_OBJC_STRING:
20123
      kwd = cp_lexer_consume_token (parser->lexer);
20124
      return objc_build_string_object (kwd->u.value);
20125
 
20126
    case CPP_KEYWORD:
20127
      switch (kwd->keyword)
20128
        {
20129
        case RID_AT_ENCODE:
20130
          return cp_parser_objc_encode_expression (parser);
20131
 
20132
        case RID_AT_PROTOCOL:
20133
          return cp_parser_objc_protocol_expression (parser);
20134
 
20135
        case RID_AT_SELECTOR:
20136
          return cp_parser_objc_selector_expression (parser);
20137
 
20138
        default:
20139
          break;
20140
        }
20141
    default:
20142
      error_at (kwd->location,
20143
                "misplaced %<@%D%> Objective-C++ construct",
20144
                kwd->u.value);
20145
      cp_parser_skip_to_end_of_block_or_statement (parser);
20146
    }
20147
 
20148
  return error_mark_node;
20149
}
20150
 
20151
/* Parse an Objective-C message expression.
20152
 
20153
   objc-message-expression:
20154
     [ objc-message-receiver objc-message-args ]
20155
 
20156
   Returns a representation of an Objective-C message.  */
20157
 
20158
static tree
20159
cp_parser_objc_message_expression (cp_parser* parser)
20160
{
20161
  tree receiver, messageargs;
20162
 
20163
  cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
20164
  receiver = cp_parser_objc_message_receiver (parser);
20165
  messageargs = cp_parser_objc_message_args (parser);
20166
  cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20167
 
20168
  return objc_build_message_expr (build_tree_list (receiver, messageargs));
20169
}
20170
 
20171
/* Parse an objc-message-receiver.
20172
 
20173
   objc-message-receiver:
20174
     expression
20175
     simple-type-specifier
20176
 
20177
  Returns a representation of the type or expression.  */
20178
 
20179
static tree
20180
cp_parser_objc_message_receiver (cp_parser* parser)
20181
{
20182
  tree rcv;
20183
 
20184
  /* An Objective-C message receiver may be either (1) a type
20185
     or (2) an expression.  */
20186
  cp_parser_parse_tentatively (parser);
20187
  rcv = cp_parser_expression (parser, false, NULL);
20188
 
20189
  if (cp_parser_parse_definitely (parser))
20190
    return rcv;
20191
 
20192
  rcv = cp_parser_simple_type_specifier (parser,
20193
                                         /*decl_specs=*/NULL,
20194
                                         CP_PARSER_FLAGS_NONE);
20195
 
20196
  return objc_get_class_reference (rcv);
20197
}
20198
 
20199
/* Parse the arguments and selectors comprising an Objective-C message.
20200
 
20201
   objc-message-args:
20202
     objc-selector
20203
     objc-selector-args
20204
     objc-selector-args , objc-comma-args
20205
 
20206
   objc-selector-args:
20207
     objc-selector [opt] : assignment-expression
20208
     objc-selector-args objc-selector [opt] : assignment-expression
20209
 
20210
   objc-comma-args:
20211
     assignment-expression
20212
     objc-comma-args , assignment-expression
20213
 
20214
   Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20215
   selector arguments and TREE_VALUE containing a list of comma
20216
   arguments.  */
20217
 
20218
static tree
20219
cp_parser_objc_message_args (cp_parser* parser)
20220
{
20221
  tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20222
  bool maybe_unary_selector_p = true;
20223
  cp_token *token = cp_lexer_peek_token (parser->lexer);
20224
 
20225
  while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20226
    {
20227
      tree selector = NULL_TREE, arg;
20228
 
20229
      if (token->type != CPP_COLON)
20230
        selector = cp_parser_objc_selector (parser);
20231
 
20232
      /* Detect if we have a unary selector.  */
20233
      if (maybe_unary_selector_p
20234
          && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20235
        return build_tree_list (selector, NULL_TREE);
20236
 
20237
      maybe_unary_selector_p = false;
20238
      cp_parser_require (parser, CPP_COLON, "%<:%>");
20239
      arg = cp_parser_assignment_expression (parser, false, NULL);
20240
 
20241
      sel_args
20242
        = chainon (sel_args,
20243
                   build_tree_list (selector, arg));
20244
 
20245
      token = cp_lexer_peek_token (parser->lexer);
20246
    }
20247
 
20248
  /* Handle non-selector arguments, if any. */
20249
  while (token->type == CPP_COMMA)
20250
    {
20251
      tree arg;
20252
 
20253
      cp_lexer_consume_token (parser->lexer);
20254
      arg = cp_parser_assignment_expression (parser, false, NULL);
20255
 
20256
      addl_args
20257
        = chainon (addl_args,
20258
                   build_tree_list (NULL_TREE, arg));
20259
 
20260
      token = cp_lexer_peek_token (parser->lexer);
20261
    }
20262
 
20263
  return build_tree_list (sel_args, addl_args);
20264
}
20265
 
20266
/* Parse an Objective-C encode expression.
20267
 
20268
   objc-encode-expression:
20269
     @encode objc-typename
20270
 
20271
   Returns an encoded representation of the type argument.  */
20272
 
20273
static tree
20274
cp_parser_objc_encode_expression (cp_parser* parser)
20275
{
20276
  tree type;
20277
  cp_token *token;
20278
 
20279
  cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
20280
  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20281
  token = cp_lexer_peek_token (parser->lexer);
20282
  type = complete_type (cp_parser_type_id (parser));
20283
  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20284
 
20285
  if (!type)
20286
    {
20287
      error_at (token->location,
20288
                "%<@encode%> must specify a type as an argument");
20289
      return error_mark_node;
20290
    }
20291
 
20292
  return objc_build_encode_expr (type);
20293
}
20294
 
20295
/* Parse an Objective-C @defs expression.  */
20296
 
20297
static tree
20298
cp_parser_objc_defs_expression (cp_parser *parser)
20299
{
20300
  tree name;
20301
 
20302
  cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
20303
  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20304
  name = cp_parser_identifier (parser);
20305
  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20306
 
20307
  return objc_get_class_ivars (name);
20308
}
20309
 
20310
/* Parse an Objective-C protocol expression.
20311
 
20312
  objc-protocol-expression:
20313
    @protocol ( identifier )
20314
 
20315
  Returns a representation of the protocol expression.  */
20316
 
20317
static tree
20318
cp_parser_objc_protocol_expression (cp_parser* parser)
20319
{
20320
  tree proto;
20321
 
20322
  cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20323
  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20324
  proto = cp_parser_identifier (parser);
20325
  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20326
 
20327
  return objc_build_protocol_expr (proto);
20328
}
20329
 
20330
/* Parse an Objective-C selector expression.
20331
 
20332
   objc-selector-expression:
20333
     @selector ( objc-method-signature )
20334
 
20335
   objc-method-signature:
20336
     objc-selector
20337
     objc-selector-seq
20338
 
20339
   objc-selector-seq:
20340
     objc-selector :
20341
     objc-selector-seq objc-selector :
20342
 
20343
  Returns a representation of the method selector.  */
20344
 
20345
static tree
20346
cp_parser_objc_selector_expression (cp_parser* parser)
20347
{
20348
  tree sel_seq = NULL_TREE;
20349
  bool maybe_unary_selector_p = true;
20350
  cp_token *token;
20351
  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20352
 
20353
  cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
20354
  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20355
  token = cp_lexer_peek_token (parser->lexer);
20356
 
20357
  while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20358
         || token->type == CPP_SCOPE)
20359
    {
20360
      tree selector = NULL_TREE;
20361
 
20362
      if (token->type != CPP_COLON
20363
          || token->type == CPP_SCOPE)
20364
        selector = cp_parser_objc_selector (parser);
20365
 
20366
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20367
          && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20368
        {
20369
          /* Detect if we have a unary selector.  */
20370
          if (maybe_unary_selector_p)
20371
            {
20372
              sel_seq = selector;
20373
              goto finish_selector;
20374
            }
20375
          else
20376
            {
20377
              cp_parser_error (parser, "expected %<:%>");
20378
            }
20379
        }
20380
      maybe_unary_selector_p = false;
20381
      token = cp_lexer_consume_token (parser->lexer);
20382
 
20383
      if (token->type == CPP_SCOPE)
20384
        {
20385
          sel_seq
20386
            = chainon (sel_seq,
20387
                       build_tree_list (selector, NULL_TREE));
20388
          sel_seq
20389
            = chainon (sel_seq,
20390
                       build_tree_list (NULL_TREE, NULL_TREE));
20391
        }
20392
      else
20393
        sel_seq
20394
          = chainon (sel_seq,
20395
                     build_tree_list (selector, NULL_TREE));
20396
 
20397
      token = cp_lexer_peek_token (parser->lexer);
20398
    }
20399
 
20400
 finish_selector:
20401
  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20402
 
20403
  return objc_build_selector_expr (loc, sel_seq);
20404
}
20405
 
20406
/* Parse a list of identifiers.
20407
 
20408
   objc-identifier-list:
20409
     identifier
20410
     objc-identifier-list , identifier
20411
 
20412
   Returns a TREE_LIST of identifier nodes.  */
20413
 
20414
static tree
20415
cp_parser_objc_identifier_list (cp_parser* parser)
20416
{
20417
  tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20418
  cp_token *sep = cp_lexer_peek_token (parser->lexer);
20419
 
20420
  while (sep->type == CPP_COMMA)
20421
    {
20422
      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20423
      list = chainon (list,
20424
                      build_tree_list (NULL_TREE,
20425
                                       cp_parser_identifier (parser)));
20426
      sep = cp_lexer_peek_token (parser->lexer);
20427
    }
20428
 
20429
  return list;
20430
}
20431
 
20432
/* Parse an Objective-C alias declaration.
20433
 
20434
   objc-alias-declaration:
20435
     @compatibility_alias identifier identifier ;
20436
 
20437
   This function registers the alias mapping with the Objective-C front end.
20438
   It returns nothing.  */
20439
 
20440
static void
20441
cp_parser_objc_alias_declaration (cp_parser* parser)
20442
{
20443
  tree alias, orig;
20444
 
20445
  cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
20446
  alias = cp_parser_identifier (parser);
20447
  orig = cp_parser_identifier (parser);
20448
  objc_declare_alias (alias, orig);
20449
  cp_parser_consume_semicolon_at_end_of_statement (parser);
20450
}
20451
 
20452
/* Parse an Objective-C class forward-declaration.
20453
 
20454
   objc-class-declaration:
20455
     @class objc-identifier-list ;
20456
 
20457
   The function registers the forward declarations with the Objective-C
20458
   front end.  It returns nothing.  */
20459
 
20460
static void
20461
cp_parser_objc_class_declaration (cp_parser* parser)
20462
{
20463
  cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
20464
  objc_declare_class (cp_parser_objc_identifier_list (parser));
20465
  cp_parser_consume_semicolon_at_end_of_statement (parser);
20466
}
20467
 
20468
/* Parse a list of Objective-C protocol references.
20469
 
20470
   objc-protocol-refs-opt:
20471
     objc-protocol-refs [opt]
20472
 
20473
   objc-protocol-refs:
20474
     < objc-identifier-list >
20475
 
20476
   Returns a TREE_LIST of identifiers, if any.  */
20477
 
20478
static tree
20479
cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20480
{
20481
  tree protorefs = NULL_TREE;
20482
 
20483
  if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20484
    {
20485
      cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
20486
      protorefs = cp_parser_objc_identifier_list (parser);
20487
      cp_parser_require (parser, CPP_GREATER, "%<>%>");
20488
    }
20489
 
20490
  return protorefs;
20491
}
20492
 
20493
/* Parse a Objective-C visibility specification.  */
20494
 
20495
static void
20496
cp_parser_objc_visibility_spec (cp_parser* parser)
20497
{
20498
  cp_token *vis = cp_lexer_peek_token (parser->lexer);
20499
 
20500
  switch (vis->keyword)
20501
    {
20502
    case RID_AT_PRIVATE:
20503
      objc_set_visibility (2);
20504
      break;
20505
    case RID_AT_PROTECTED:
20506
      objc_set_visibility (0);
20507
      break;
20508
    case RID_AT_PUBLIC:
20509
      objc_set_visibility (1);
20510
      break;
20511
    default:
20512
      return;
20513
    }
20514
 
20515
  /* Eat '@private'/'@protected'/'@public'.  */
20516
  cp_lexer_consume_token (parser->lexer);
20517
}
20518
 
20519
/* Parse an Objective-C method type.  */
20520
 
20521
static void
20522
cp_parser_objc_method_type (cp_parser* parser)
20523
{
20524
  objc_set_method_type
20525
   (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20526
    ? PLUS_EXPR
20527
    : MINUS_EXPR);
20528
}
20529
 
20530
/* Parse an Objective-C protocol qualifier.  */
20531
 
20532
static tree
20533
cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20534
{
20535
  tree quals = NULL_TREE, node;
20536
  cp_token *token = cp_lexer_peek_token (parser->lexer);
20537
 
20538
  node = token->u.value;
20539
 
20540
  while (node && TREE_CODE (node) == IDENTIFIER_NODE
20541
         && (node == ridpointers [(int) RID_IN]
20542
             || node == ridpointers [(int) RID_OUT]
20543
             || node == ridpointers [(int) RID_INOUT]
20544
             || node == ridpointers [(int) RID_BYCOPY]
20545
             || node == ridpointers [(int) RID_BYREF]
20546
             || node == ridpointers [(int) RID_ONEWAY]))
20547
    {
20548
      quals = tree_cons (NULL_TREE, node, quals);
20549
      cp_lexer_consume_token (parser->lexer);
20550
      token = cp_lexer_peek_token (parser->lexer);
20551
      node = token->u.value;
20552
    }
20553
 
20554
  return quals;
20555
}
20556
 
20557
/* Parse an Objective-C typename.  */
20558
 
20559
static tree
20560
cp_parser_objc_typename (cp_parser* parser)
20561
{
20562
  tree type_name = NULL_TREE;
20563
 
20564
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20565
    {
20566
      tree proto_quals, cp_type = NULL_TREE;
20567
 
20568
      cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20569
      proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20570
 
20571
      /* An ObjC type name may consist of just protocol qualifiers, in which
20572
         case the type shall default to 'id'.  */
20573
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20574
        cp_type = cp_parser_type_id (parser);
20575
 
20576
      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20577
      type_name = build_tree_list (proto_quals, cp_type);
20578
    }
20579
 
20580
  return type_name;
20581
}
20582
 
20583
/* Check to see if TYPE refers to an Objective-C selector name.  */
20584
 
20585
static bool
20586
cp_parser_objc_selector_p (enum cpp_ttype type)
20587
{
20588
  return (type == CPP_NAME || type == CPP_KEYWORD
20589
          || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20590
          || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20591
          || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20592
          || type == CPP_XOR || type == CPP_XOR_EQ);
20593
}
20594
 
20595
/* Parse an Objective-C selector.  */
20596
 
20597
static tree
20598
cp_parser_objc_selector (cp_parser* parser)
20599
{
20600
  cp_token *token = cp_lexer_consume_token (parser->lexer);
20601
 
20602
  if (!cp_parser_objc_selector_p (token->type))
20603
    {
20604
      error_at (token->location, "invalid Objective-C++ selector name");
20605
      return error_mark_node;
20606
    }
20607
 
20608
  /* C++ operator names are allowed to appear in ObjC selectors.  */
20609
  switch (token->type)
20610
    {
20611
    case CPP_AND_AND: return get_identifier ("and");
20612
    case CPP_AND_EQ: return get_identifier ("and_eq");
20613
    case CPP_AND: return get_identifier ("bitand");
20614
    case CPP_OR: return get_identifier ("bitor");
20615
    case CPP_COMPL: return get_identifier ("compl");
20616
    case CPP_NOT: return get_identifier ("not");
20617
    case CPP_NOT_EQ: return get_identifier ("not_eq");
20618
    case CPP_OR_OR: return get_identifier ("or");
20619
    case CPP_OR_EQ: return get_identifier ("or_eq");
20620
    case CPP_XOR: return get_identifier ("xor");
20621
    case CPP_XOR_EQ: return get_identifier ("xor_eq");
20622
    default: return token->u.value;
20623
    }
20624
}
20625
 
20626
/* Parse an Objective-C params list.  */
20627
 
20628
static tree
20629
cp_parser_objc_method_keyword_params (cp_parser* parser)
20630
{
20631
  tree params = NULL_TREE;
20632
  bool maybe_unary_selector_p = true;
20633
  cp_token *token = cp_lexer_peek_token (parser->lexer);
20634
 
20635
  while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20636
    {
20637
      tree selector = NULL_TREE, type_name, identifier;
20638
 
20639
      if (token->type != CPP_COLON)
20640
        selector = cp_parser_objc_selector (parser);
20641
 
20642
      /* Detect if we have a unary selector.  */
20643
      if (maybe_unary_selector_p
20644
          && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20645
        return selector;
20646
 
20647
      maybe_unary_selector_p = false;
20648
      cp_parser_require (parser, CPP_COLON, "%<:%>");
20649
      type_name = cp_parser_objc_typename (parser);
20650
      identifier = cp_parser_identifier (parser);
20651
 
20652
      params
20653
        = chainon (params,
20654
                   objc_build_keyword_decl (selector,
20655
                                            type_name,
20656
                                            identifier));
20657
 
20658
      token = cp_lexer_peek_token (parser->lexer);
20659
    }
20660
 
20661
  return params;
20662
}
20663
 
20664
/* Parse the non-keyword Objective-C params.  */
20665
 
20666
static tree
20667
cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20668
{
20669
  tree params = make_node (TREE_LIST);
20670
  cp_token *token = cp_lexer_peek_token (parser->lexer);
20671
  *ellipsisp = false;  /* Initially, assume no ellipsis.  */
20672
 
20673
  while (token->type == CPP_COMMA)
20674
    {
20675
      cp_parameter_declarator *parmdecl;
20676
      tree parm;
20677
 
20678
      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20679
      token = cp_lexer_peek_token (parser->lexer);
20680
 
20681
      if (token->type == CPP_ELLIPSIS)
20682
        {
20683
          cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
20684
          *ellipsisp = true;
20685
          break;
20686
        }
20687
 
20688
      parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20689
      parm = grokdeclarator (parmdecl->declarator,
20690
                             &parmdecl->decl_specifiers,
20691
                             PARM, /*initialized=*/0,
20692
                             /*attrlist=*/NULL);
20693
 
20694
      chainon (params, build_tree_list (NULL_TREE, parm));
20695
      token = cp_lexer_peek_token (parser->lexer);
20696
    }
20697
 
20698
  return params;
20699
}
20700
 
20701
/* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
20702
 
20703
static void
20704
cp_parser_objc_interstitial_code (cp_parser* parser)
20705
{
20706
  cp_token *token = cp_lexer_peek_token (parser->lexer);
20707
 
20708
  /* If the next token is `extern' and the following token is a string
20709
     literal, then we have a linkage specification.  */
20710
  if (token->keyword == RID_EXTERN
20711
      && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20712
    cp_parser_linkage_specification (parser);
20713
  /* Handle #pragma, if any.  */
20714
  else if (token->type == CPP_PRAGMA)
20715
    cp_parser_pragma (parser, pragma_external);
20716
  /* Allow stray semicolons.  */
20717
  else if (token->type == CPP_SEMICOLON)
20718
    cp_lexer_consume_token (parser->lexer);
20719
  /* Finally, try to parse a block-declaration, or a function-definition.  */
20720
  else
20721
    cp_parser_block_declaration (parser, /*statement_p=*/false);
20722
}
20723
 
20724
/* Parse a method signature.  */
20725
 
20726
static tree
20727
cp_parser_objc_method_signature (cp_parser* parser)
20728
{
20729
  tree rettype, kwdparms, optparms;
20730
  bool ellipsis = false;
20731
 
20732
  cp_parser_objc_method_type (parser);
20733
  rettype = cp_parser_objc_typename (parser);
20734
  kwdparms = cp_parser_objc_method_keyword_params (parser);
20735
  optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20736
 
20737
  return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20738
}
20739
 
20740
/* Pars an Objective-C method prototype list.  */
20741
 
20742
static void
20743
cp_parser_objc_method_prototype_list (cp_parser* parser)
20744
{
20745
  cp_token *token = cp_lexer_peek_token (parser->lexer);
20746
 
20747
  while (token->keyword != RID_AT_END)
20748
    {
20749
      if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20750
        {
20751
          objc_add_method_declaration
20752
           (cp_parser_objc_method_signature (parser));
20753
          cp_parser_consume_semicolon_at_end_of_statement (parser);
20754
        }
20755
      else
20756
        /* Allow for interspersed non-ObjC++ code.  */
20757
        cp_parser_objc_interstitial_code (parser);
20758
 
20759
      token = cp_lexer_peek_token (parser->lexer);
20760
    }
20761
 
20762
  cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20763
  objc_finish_interface ();
20764
}
20765
 
20766
/* Parse an Objective-C method definition list.  */
20767
 
20768
static void
20769
cp_parser_objc_method_definition_list (cp_parser* parser)
20770
{
20771
  cp_token *token = cp_lexer_peek_token (parser->lexer);
20772
 
20773
  while (token->keyword != RID_AT_END)
20774
    {
20775
      tree meth;
20776
 
20777
      if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20778
        {
20779
          push_deferring_access_checks (dk_deferred);
20780
          objc_start_method_definition
20781
           (cp_parser_objc_method_signature (parser));
20782
 
20783
          /* For historical reasons, we accept an optional semicolon.  */
20784
          if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20785
            cp_lexer_consume_token (parser->lexer);
20786
 
20787
          perform_deferred_access_checks ();
20788
          stop_deferring_access_checks ();
20789
          meth = cp_parser_function_definition_after_declarator (parser,
20790
                                                                 false);
20791
          pop_deferring_access_checks ();
20792
          objc_finish_method_definition (meth);
20793
        }
20794
      else
20795
        /* Allow for interspersed non-ObjC++ code.  */
20796
        cp_parser_objc_interstitial_code (parser);
20797
 
20798
      token = cp_lexer_peek_token (parser->lexer);
20799
    }
20800
 
20801
  cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20802
  objc_finish_implementation ();
20803
}
20804
 
20805
/* Parse Objective-C ivars.  */
20806
 
20807
static void
20808
cp_parser_objc_class_ivars (cp_parser* parser)
20809
{
20810
  cp_token *token = cp_lexer_peek_token (parser->lexer);
20811
 
20812
  if (token->type != CPP_OPEN_BRACE)
20813
    return;     /* No ivars specified.  */
20814
 
20815
  cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
20816
  token = cp_lexer_peek_token (parser->lexer);
20817
 
20818
  while (token->type != CPP_CLOSE_BRACE)
20819
    {
20820
      cp_decl_specifier_seq declspecs;
20821
      int decl_class_or_enum_p;
20822
      tree prefix_attributes;
20823
 
20824
      cp_parser_objc_visibility_spec (parser);
20825
 
20826
      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20827
        break;
20828
 
20829
      cp_parser_decl_specifier_seq (parser,
20830
                                    CP_PARSER_FLAGS_OPTIONAL,
20831
                                    &declspecs,
20832
                                    &decl_class_or_enum_p);
20833
      prefix_attributes = declspecs.attributes;
20834
      declspecs.attributes = NULL_TREE;
20835
 
20836
      /* Keep going until we hit the `;' at the end of the
20837
         declaration.  */
20838
      while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20839
        {
20840
          tree width = NULL_TREE, attributes, first_attribute, decl;
20841
          cp_declarator *declarator = NULL;
20842
          int ctor_dtor_or_conv_p;
20843
 
20844
          /* Check for a (possibly unnamed) bitfield declaration.  */
20845
          token = cp_lexer_peek_token (parser->lexer);
20846
          if (token->type == CPP_COLON)
20847
            goto eat_colon;
20848
 
20849
          if (token->type == CPP_NAME
20850
              && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20851
                  == CPP_COLON))
20852
            {
20853
              /* Get the name of the bitfield.  */
20854
              declarator = make_id_declarator (NULL_TREE,
20855
                                               cp_parser_identifier (parser),
20856
                                               sfk_none);
20857
 
20858
             eat_colon:
20859
              cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20860
              /* Get the width of the bitfield.  */
20861
              width
20862
                = cp_parser_constant_expression (parser,
20863
                                                 /*allow_non_constant=*/false,
20864
                                                 NULL);
20865
            }
20866
          else
20867
            {
20868
              /* Parse the declarator.  */
20869
              declarator
20870
                = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20871
                                        &ctor_dtor_or_conv_p,
20872
                                        /*parenthesized_p=*/NULL,
20873
                                        /*member_p=*/false);
20874
            }
20875
 
20876
          /* Look for attributes that apply to the ivar.  */
20877
          attributes = cp_parser_attributes_opt (parser);
20878
          /* Remember which attributes are prefix attributes and
20879
             which are not.  */
20880
          first_attribute = attributes;
20881
          /* Combine the attributes.  */
20882
          attributes = chainon (prefix_attributes, attributes);
20883
 
20884
          if (width)
20885
              /* Create the bitfield declaration.  */
20886
              decl = grokbitfield (declarator, &declspecs,
20887
                                   width,
20888
                                   attributes);
20889
          else
20890
            decl = grokfield (declarator, &declspecs,
20891
                              NULL_TREE, /*init_const_expr_p=*/false,
20892
                              NULL_TREE, attributes);
20893
 
20894
          /* Add the instance variable.  */
20895
          objc_add_instance_variable (decl);
20896
 
20897
          /* Reset PREFIX_ATTRIBUTES.  */
20898
          while (attributes && TREE_CHAIN (attributes) != first_attribute)
20899
            attributes = TREE_CHAIN (attributes);
20900
          if (attributes)
20901
            TREE_CHAIN (attributes) = NULL_TREE;
20902
 
20903
          token = cp_lexer_peek_token (parser->lexer);
20904
 
20905
          if (token->type == CPP_COMMA)
20906
            {
20907
              cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20908
              continue;
20909
            }
20910
          break;
20911
        }
20912
 
20913
      cp_parser_consume_semicolon_at_end_of_statement (parser);
20914
      token = cp_lexer_peek_token (parser->lexer);
20915
    }
20916
 
20917
  cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
20918
  /* For historical reasons, we accept an optional semicolon.  */
20919
  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20920
    cp_lexer_consume_token (parser->lexer);
20921
}
20922
 
20923
/* Parse an Objective-C protocol declaration.  */
20924
 
20925
static void
20926
cp_parser_objc_protocol_declaration (cp_parser* parser)
20927
{
20928
  tree proto, protorefs;
20929
  cp_token *tok;
20930
 
20931
  cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20932
  if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20933
    {
20934
      tok = cp_lexer_peek_token (parser->lexer);
20935
      error_at (tok->location, "identifier expected after %<@protocol%>");
20936
      goto finish;
20937
    }
20938
 
20939
  /* See if we have a forward declaration or a definition.  */
20940
  tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20941
 
20942
  /* Try a forward declaration first.  */
20943
  if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20944
    {
20945
      objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20946
     finish:
20947
      cp_parser_consume_semicolon_at_end_of_statement (parser);
20948
    }
20949
 
20950
  /* Ok, we got a full-fledged definition (or at least should).  */
20951
  else
20952
    {
20953
      proto = cp_parser_identifier (parser);
20954
      protorefs = cp_parser_objc_protocol_refs_opt (parser);
20955
      objc_start_protocol (proto, protorefs);
20956
      cp_parser_objc_method_prototype_list (parser);
20957
    }
20958
}
20959
 
20960
/* Parse an Objective-C superclass or category.  */
20961
 
20962
static void
20963
cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20964
                                                          tree *categ)
20965
{
20966
  cp_token *next = cp_lexer_peek_token (parser->lexer);
20967
 
20968
  *super = *categ = NULL_TREE;
20969
  if (next->type == CPP_COLON)
20970
    {
20971
      cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20972
      *super = cp_parser_identifier (parser);
20973
    }
20974
  else if (next->type == CPP_OPEN_PAREN)
20975
    {
20976
      cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20977
      *categ = cp_parser_identifier (parser);
20978
      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20979
    }
20980
}
20981
 
20982
/* Parse an Objective-C class interface.  */
20983
 
20984
static void
20985
cp_parser_objc_class_interface (cp_parser* parser)
20986
{
20987
  tree name, super, categ, protos;
20988
 
20989
  cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20990
  name = cp_parser_identifier (parser);
20991
  cp_parser_objc_superclass_or_category (parser, &super, &categ);
20992
  protos = cp_parser_objc_protocol_refs_opt (parser);
20993
 
20994
  /* We have either a class or a category on our hands.  */
20995
  if (categ)
20996
    objc_start_category_interface (name, categ, protos);
20997
  else
20998
    {
20999
      objc_start_class_interface (name, super, protos);
21000
      /* Handle instance variable declarations, if any.  */
21001
      cp_parser_objc_class_ivars (parser);
21002
      objc_continue_interface ();
21003
    }
21004
 
21005
  cp_parser_objc_method_prototype_list (parser);
21006
}
21007
 
21008
/* Parse an Objective-C class implementation.  */
21009
 
21010
static void
21011
cp_parser_objc_class_implementation (cp_parser* parser)
21012
{
21013
  tree name, super, categ;
21014
 
21015
  cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
21016
  name = cp_parser_identifier (parser);
21017
  cp_parser_objc_superclass_or_category (parser, &super, &categ);
21018
 
21019
  /* We have either a class or a category on our hands.  */
21020
  if (categ)
21021
    objc_start_category_implementation (name, categ);
21022
  else
21023
    {
21024
      objc_start_class_implementation (name, super);
21025
      /* Handle instance variable declarations, if any.  */
21026
      cp_parser_objc_class_ivars (parser);
21027
      objc_continue_implementation ();
21028
    }
21029
 
21030
  cp_parser_objc_method_definition_list (parser);
21031
}
21032
 
21033
/* Consume the @end token and finish off the implementation.  */
21034
 
21035
static void
21036
cp_parser_objc_end_implementation (cp_parser* parser)
21037
{
21038
  cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
21039
  objc_finish_implementation ();
21040
}
21041
 
21042
/* Parse an Objective-C declaration.  */
21043
 
21044
static void
21045
cp_parser_objc_declaration (cp_parser* parser)
21046
{
21047
  /* Try to figure out what kind of declaration is present.  */
21048
  cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21049
 
21050
  switch (kwd->keyword)
21051
    {
21052
    case RID_AT_ALIAS:
21053
      cp_parser_objc_alias_declaration (parser);
21054
      break;
21055
    case RID_AT_CLASS:
21056
      cp_parser_objc_class_declaration (parser);
21057
      break;
21058
    case RID_AT_PROTOCOL:
21059
      cp_parser_objc_protocol_declaration (parser);
21060
      break;
21061
    case RID_AT_INTERFACE:
21062
      cp_parser_objc_class_interface (parser);
21063
      break;
21064
    case RID_AT_IMPLEMENTATION:
21065
      cp_parser_objc_class_implementation (parser);
21066
      break;
21067
    case RID_AT_END:
21068
      cp_parser_objc_end_implementation (parser);
21069
      break;
21070
    default:
21071
      error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21072
                kwd->u.value);
21073
      cp_parser_skip_to_end_of_block_or_statement (parser);
21074
    }
21075
}
21076
 
21077
/* Parse an Objective-C try-catch-finally statement.
21078
 
21079
   objc-try-catch-finally-stmt:
21080
     @try compound-statement objc-catch-clause-seq [opt]
21081
       objc-finally-clause [opt]
21082
 
21083
   objc-catch-clause-seq:
21084
     objc-catch-clause objc-catch-clause-seq [opt]
21085
 
21086
   objc-catch-clause:
21087
     @catch ( exception-declaration ) compound-statement
21088
 
21089
   objc-finally-clause
21090
     @finally compound-statement
21091
 
21092
   Returns NULL_TREE.  */
21093
 
21094
static tree
21095
cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21096
  location_t location;
21097
  tree stmt;
21098
 
21099
  cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
21100
  location = cp_lexer_peek_token (parser->lexer)->location;
21101
  /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21102
     node, lest it get absorbed into the surrounding block.  */
21103
  stmt = push_stmt_list ();
21104
  cp_parser_compound_statement (parser, NULL, false);
21105
  objc_begin_try_stmt (location, pop_stmt_list (stmt));
21106
 
21107
  while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21108
    {
21109
      cp_parameter_declarator *parmdecl;
21110
      tree parm;
21111
 
21112
      cp_lexer_consume_token (parser->lexer);
21113
      cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21114
      parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21115
      parm = grokdeclarator (parmdecl->declarator,
21116
                             &parmdecl->decl_specifiers,
21117
                             PARM, /*initialized=*/0,
21118
                             /*attrlist=*/NULL);
21119
      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21120
      objc_begin_catch_clause (parm);
21121
      cp_parser_compound_statement (parser, NULL, false);
21122
      objc_finish_catch_clause ();
21123
    }
21124
 
21125
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21126
    {
21127
      cp_lexer_consume_token (parser->lexer);
21128
      location = cp_lexer_peek_token (parser->lexer)->location;
21129
      /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21130
         node, lest it get absorbed into the surrounding block.  */
21131
      stmt = push_stmt_list ();
21132
      cp_parser_compound_statement (parser, NULL, false);
21133
      objc_build_finally_clause (location, pop_stmt_list (stmt));
21134
    }
21135
 
21136
  return objc_finish_try_stmt ();
21137
}
21138
 
21139
/* Parse an Objective-C synchronized statement.
21140
 
21141
   objc-synchronized-stmt:
21142
     @synchronized ( expression ) compound-statement
21143
 
21144
   Returns NULL_TREE.  */
21145
 
21146
static tree
21147
cp_parser_objc_synchronized_statement (cp_parser *parser) {
21148
  location_t location;
21149
  tree lock, stmt;
21150
 
21151
  cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21152
 
21153
  location = cp_lexer_peek_token (parser->lexer)->location;
21154
  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21155
  lock = cp_parser_expression (parser, false, NULL);
21156
  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21157
 
21158
  /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21159
     node, lest it get absorbed into the surrounding block.  */
21160
  stmt = push_stmt_list ();
21161
  cp_parser_compound_statement (parser, NULL, false);
21162
 
21163
  return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21164
}
21165
 
21166
/* Parse an Objective-C throw statement.
21167
 
21168
   objc-throw-stmt:
21169
     @throw assignment-expression [opt] ;
21170
 
21171
   Returns a constructed '@throw' statement.  */
21172
 
21173
static tree
21174
cp_parser_objc_throw_statement (cp_parser *parser) {
21175
  tree expr = NULL_TREE;
21176
  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21177
 
21178
  cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21179
 
21180
  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21181
    expr = cp_parser_assignment_expression (parser, false, NULL);
21182
 
21183
  cp_parser_consume_semicolon_at_end_of_statement (parser);
21184
 
21185
  return objc_build_throw_stmt (loc, expr);
21186
}
21187
 
21188
/* Parse an Objective-C statement.  */
21189
 
21190
static tree
21191
cp_parser_objc_statement (cp_parser * parser) {
21192
  /* Try to figure out what kind of declaration is present.  */
21193
  cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21194
 
21195
  switch (kwd->keyword)
21196
    {
21197
    case RID_AT_TRY:
21198
      return cp_parser_objc_try_catch_finally_statement (parser);
21199
    case RID_AT_SYNCHRONIZED:
21200
      return cp_parser_objc_synchronized_statement (parser);
21201
    case RID_AT_THROW:
21202
      return cp_parser_objc_throw_statement (parser);
21203
    default:
21204
      error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21205
               kwd->u.value);
21206
      cp_parser_skip_to_end_of_block_or_statement (parser);
21207
    }
21208
 
21209
  return error_mark_node;
21210
}
21211
 
21212
/* OpenMP 2.5 parsing routines.  */
21213
 
21214
/* Returns name of the next clause.
21215
   If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21216
   the token is not consumed.  Otherwise appropriate pragma_omp_clause is
21217
   returned and the token is consumed.  */
21218
 
21219
static pragma_omp_clause
21220
cp_parser_omp_clause_name (cp_parser *parser)
21221
{
21222
  pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21223
 
21224
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21225
    result = PRAGMA_OMP_CLAUSE_IF;
21226
  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21227
    result = PRAGMA_OMP_CLAUSE_DEFAULT;
21228
  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21229
    result = PRAGMA_OMP_CLAUSE_PRIVATE;
21230
  else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21231
    {
21232
      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21233
      const char *p = IDENTIFIER_POINTER (id);
21234
 
21235
      switch (p[0])
21236
        {
21237
        case 'c':
21238
          if (!strcmp ("collapse", p))
21239
            result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21240
          else if (!strcmp ("copyin", p))
21241
            result = PRAGMA_OMP_CLAUSE_COPYIN;
21242
          else if (!strcmp ("copyprivate", p))
21243
            result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21244
          break;
21245
        case 'f':
21246
          if (!strcmp ("firstprivate", p))
21247
            result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21248
          break;
21249
        case 'l':
21250
          if (!strcmp ("lastprivate", p))
21251
            result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21252
          break;
21253
        case 'n':
21254
          if (!strcmp ("nowait", p))
21255
            result = PRAGMA_OMP_CLAUSE_NOWAIT;
21256
          else if (!strcmp ("num_threads", p))
21257
            result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21258
          break;
21259
        case 'o':
21260
          if (!strcmp ("ordered", p))
21261
            result = PRAGMA_OMP_CLAUSE_ORDERED;
21262
          break;
21263
        case 'r':
21264
          if (!strcmp ("reduction", p))
21265
            result = PRAGMA_OMP_CLAUSE_REDUCTION;
21266
          break;
21267
        case 's':
21268
          if (!strcmp ("schedule", p))
21269
            result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21270
          else if (!strcmp ("shared", p))
21271
            result = PRAGMA_OMP_CLAUSE_SHARED;
21272
          break;
21273
        case 'u':
21274
          if (!strcmp ("untied", p))
21275
            result = PRAGMA_OMP_CLAUSE_UNTIED;
21276
          break;
21277
        }
21278
    }
21279
 
21280
  if (result != PRAGMA_OMP_CLAUSE_NONE)
21281
    cp_lexer_consume_token (parser->lexer);
21282
 
21283
  return result;
21284
}
21285
 
21286
/* Validate that a clause of the given type does not already exist.  */
21287
 
21288
static void
21289
check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21290
                           const char *name, location_t location)
21291
{
21292
  tree c;
21293
 
21294
  for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21295
    if (OMP_CLAUSE_CODE (c) == code)
21296
      {
21297
        error_at (location, "too many %qs clauses", name);
21298
        break;
21299
      }
21300
}
21301
 
21302
/* OpenMP 2.5:
21303
   variable-list:
21304
     identifier
21305
     variable-list , identifier
21306
 
21307
   In addition, we match a closing parenthesis.  An opening parenthesis
21308
   will have been consumed by the caller.
21309
 
21310
   If KIND is nonzero, create the appropriate node and install the decl
21311
   in OMP_CLAUSE_DECL and add the node to the head of the list.
21312
 
21313
   If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21314
   return the list created.  */
21315
 
21316
static tree
21317
cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21318
                                tree list)
21319
{
21320
  cp_token *token;
21321
  while (1)
21322
    {
21323
      tree name, decl;
21324
 
21325
      token = cp_lexer_peek_token (parser->lexer);
21326
      name = cp_parser_id_expression (parser, /*template_p=*/false,
21327
                                      /*check_dependency_p=*/true,
21328
                                      /*template_p=*/NULL,
21329
                                      /*declarator_p=*/false,
21330
                                      /*optional_p=*/false);
21331
      if (name == error_mark_node)
21332
        goto skip_comma;
21333
 
21334
      decl = cp_parser_lookup_name_simple (parser, name, token->location);
21335
      if (decl == error_mark_node)
21336
        cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21337
      else if (kind != 0)
21338
        {
21339
          tree u = build_omp_clause (token->location, kind);
21340
          OMP_CLAUSE_DECL (u) = decl;
21341
          OMP_CLAUSE_CHAIN (u) = list;
21342
          list = u;
21343
        }
21344
      else
21345
        list = tree_cons (decl, NULL_TREE, list);
21346
 
21347
    get_comma:
21348
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21349
        break;
21350
      cp_lexer_consume_token (parser->lexer);
21351
    }
21352
 
21353
  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21354
    {
21355
      int ending;
21356
 
21357
      /* Try to resync to an unnested comma.  Copied from
21358
         cp_parser_parenthesized_expression_list.  */
21359
    skip_comma:
21360
      ending = cp_parser_skip_to_closing_parenthesis (parser,
21361
                                                      /*recovering=*/true,
21362
                                                      /*or_comma=*/true,
21363
                                                      /*consume_paren=*/true);
21364
      if (ending < 0)
21365
        goto get_comma;
21366
    }
21367
 
21368
  return list;
21369
}
21370
 
21371
/* Similarly, but expect leading and trailing parenthesis.  This is a very
21372
   common case for omp clauses.  */
21373
 
21374
static tree
21375
cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21376
{
21377
  if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21378
    return cp_parser_omp_var_list_no_open (parser, kind, list);
21379
  return list;
21380
}
21381
 
21382
/* OpenMP 3.0:
21383
   collapse ( constant-expression ) */
21384
 
21385
static tree
21386
cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21387
{
21388
  tree c, num;
21389
  location_t loc;
21390
  HOST_WIDE_INT n;
21391
 
21392
  loc = cp_lexer_peek_token (parser->lexer)->location;
21393
  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21394
    return list;
21395
 
21396
  num = cp_parser_constant_expression (parser, false, NULL);
21397
 
21398
  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21399
    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21400
                                           /*or_comma=*/false,
21401
                                           /*consume_paren=*/true);
21402
 
21403
  if (num == error_mark_node)
21404
    return list;
21405
  num = fold_non_dependent_expr (num);
21406
  if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21407
      || !host_integerp (num, 0)
21408
      || (n = tree_low_cst (num, 0)) <= 0
21409
      || (int) n != n)
21410
    {
21411
      error_at (loc, "collapse argument needs positive constant integer expression");
21412
      return list;
21413
    }
21414
 
21415
  check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21416
  c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21417
  OMP_CLAUSE_CHAIN (c) = list;
21418
  OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21419
 
21420
  return c;
21421
}
21422
 
21423
/* OpenMP 2.5:
21424
   default ( shared | none ) */
21425
 
21426
static tree
21427
cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21428
{
21429
  enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21430
  tree c;
21431
 
21432
  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21433
    return list;
21434
  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21435
    {
21436
      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21437
      const char *p = IDENTIFIER_POINTER (id);
21438
 
21439
      switch (p[0])
21440
        {
21441
        case 'n':
21442
          if (strcmp ("none", p) != 0)
21443
            goto invalid_kind;
21444
          kind = OMP_CLAUSE_DEFAULT_NONE;
21445
          break;
21446
 
21447
        case 's':
21448
          if (strcmp ("shared", p) != 0)
21449
            goto invalid_kind;
21450
          kind = OMP_CLAUSE_DEFAULT_SHARED;
21451
          break;
21452
 
21453
        default:
21454
          goto invalid_kind;
21455
        }
21456
 
21457
      cp_lexer_consume_token (parser->lexer);
21458
    }
21459
  else
21460
    {
21461
    invalid_kind:
21462
      cp_parser_error (parser, "expected %<none%> or %<shared%>");
21463
    }
21464
 
21465
  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21466
    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21467
                                           /*or_comma=*/false,
21468
                                           /*consume_paren=*/true);
21469
 
21470
  if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21471
    return list;
21472
 
21473
  check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21474
  c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21475
  OMP_CLAUSE_CHAIN (c) = list;
21476
  OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21477
 
21478
  return c;
21479
}
21480
 
21481
/* OpenMP 2.5:
21482
   if ( expression ) */
21483
 
21484
static tree
21485
cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21486
{
21487
  tree t, c;
21488
 
21489
  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21490
    return list;
21491
 
21492
  t = cp_parser_condition (parser);
21493
 
21494
  if (t == error_mark_node
21495
      || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21496
    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21497
                                           /*or_comma=*/false,
21498
                                           /*consume_paren=*/true);
21499
 
21500
  check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21501
 
21502
  c = build_omp_clause (location, OMP_CLAUSE_IF);
21503
  OMP_CLAUSE_IF_EXPR (c) = t;
21504
  OMP_CLAUSE_CHAIN (c) = list;
21505
 
21506
  return c;
21507
}
21508
 
21509
/* OpenMP 2.5:
21510
   nowait */
21511
 
21512
static tree
21513
cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21514
                             tree list, location_t location)
21515
{
21516
  tree c;
21517
 
21518
  check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21519
 
21520
  c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21521
  OMP_CLAUSE_CHAIN (c) = list;
21522
  return c;
21523
}
21524
 
21525
/* OpenMP 2.5:
21526
   num_threads ( expression ) */
21527
 
21528
static tree
21529
cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21530
                                  location_t location)
21531
{
21532
  tree t, c;
21533
 
21534
  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21535
    return list;
21536
 
21537
  t = cp_parser_expression (parser, false, NULL);
21538
 
21539
  if (t == error_mark_node
21540
      || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21541
    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21542
                                           /*or_comma=*/false,
21543
                                           /*consume_paren=*/true);
21544
 
21545
  check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21546
                             "num_threads", location);
21547
 
21548
  c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21549
  OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21550
  OMP_CLAUSE_CHAIN (c) = list;
21551
 
21552
  return c;
21553
}
21554
 
21555
/* OpenMP 2.5:
21556
   ordered */
21557
 
21558
static tree
21559
cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21560
                              tree list, location_t location)
21561
{
21562
  tree c;
21563
 
21564
  check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21565
                             "ordered", location);
21566
 
21567
  c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21568
  OMP_CLAUSE_CHAIN (c) = list;
21569
  return c;
21570
}
21571
 
21572
/* OpenMP 2.5:
21573
   reduction ( reduction-operator : variable-list )
21574
 
21575
   reduction-operator:
21576
     One of: + * - & ^ | && || */
21577
 
21578
static tree
21579
cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21580
{
21581
  enum tree_code code;
21582
  tree nlist, c;
21583
 
21584
  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21585
    return list;
21586
 
21587
  switch (cp_lexer_peek_token (parser->lexer)->type)
21588
    {
21589
    case CPP_PLUS:
21590
      code = PLUS_EXPR;
21591
      break;
21592
    case CPP_MULT:
21593
      code = MULT_EXPR;
21594
      break;
21595
    case CPP_MINUS:
21596
      code = MINUS_EXPR;
21597
      break;
21598
    case CPP_AND:
21599
      code = BIT_AND_EXPR;
21600
      break;
21601
    case CPP_XOR:
21602
      code = BIT_XOR_EXPR;
21603
      break;
21604
    case CPP_OR:
21605
      code = BIT_IOR_EXPR;
21606
      break;
21607
    case CPP_AND_AND:
21608
      code = TRUTH_ANDIF_EXPR;
21609
      break;
21610
    case CPP_OR_OR:
21611
      code = TRUTH_ORIF_EXPR;
21612
      break;
21613
    default:
21614
      cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21615
                               "%<|%>, %<&&%>, or %<||%>");
21616
    resync_fail:
21617
      cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21618
                                             /*or_comma=*/false,
21619
                                             /*consume_paren=*/true);
21620
      return list;
21621
    }
21622
  cp_lexer_consume_token (parser->lexer);
21623
 
21624
  if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21625
    goto resync_fail;
21626
 
21627
  nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21628
  for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21629
    OMP_CLAUSE_REDUCTION_CODE (c) = code;
21630
 
21631
  return nlist;
21632
}
21633
 
21634
/* OpenMP 2.5:
21635
   schedule ( schedule-kind )
21636
   schedule ( schedule-kind , expression )
21637
 
21638
   schedule-kind:
21639
     static | dynamic | guided | runtime | auto  */
21640
 
21641
static tree
21642
cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21643
{
21644
  tree c, t;
21645
 
21646
  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21647
    return list;
21648
 
21649
  c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21650
 
21651
  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21652
    {
21653
      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21654
      const char *p = IDENTIFIER_POINTER (id);
21655
 
21656
      switch (p[0])
21657
        {
21658
        case 'd':
21659
          if (strcmp ("dynamic", p) != 0)
21660
            goto invalid_kind;
21661
          OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21662
          break;
21663
 
21664
        case 'g':
21665
          if (strcmp ("guided", p) != 0)
21666
            goto invalid_kind;
21667
          OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21668
          break;
21669
 
21670
        case 'r':
21671
          if (strcmp ("runtime", p) != 0)
21672
            goto invalid_kind;
21673
          OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21674
          break;
21675
 
21676
        default:
21677
          goto invalid_kind;
21678
        }
21679
    }
21680
  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21681
    OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21682
  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21683
    OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21684
  else
21685
    goto invalid_kind;
21686
  cp_lexer_consume_token (parser->lexer);
21687
 
21688
  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21689
    {
21690
      cp_token *token;
21691
      cp_lexer_consume_token (parser->lexer);
21692
 
21693
      token = cp_lexer_peek_token (parser->lexer);
21694
      t = cp_parser_assignment_expression (parser, false, NULL);
21695
 
21696
      if (t == error_mark_node)
21697
        goto resync_fail;
21698
      else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21699
        error_at (token->location, "schedule %<runtime%> does not take "
21700
                  "a %<chunk_size%> parameter");
21701
      else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21702
        error_at (token->location, "schedule %<auto%> does not take "
21703
                  "a %<chunk_size%> parameter");
21704
      else
21705
        OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21706
 
21707
      if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21708
        goto resync_fail;
21709
    }
21710
  else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21711
    goto resync_fail;
21712
 
21713
  check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21714
  OMP_CLAUSE_CHAIN (c) = list;
21715
  return c;
21716
 
21717
 invalid_kind:
21718
  cp_parser_error (parser, "invalid schedule kind");
21719
 resync_fail:
21720
  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21721
                                         /*or_comma=*/false,
21722
                                         /*consume_paren=*/true);
21723
  return list;
21724
}
21725
 
21726
/* OpenMP 3.0:
21727
   untied */
21728
 
21729
static tree
21730
cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21731
                             tree list, location_t location)
21732
{
21733
  tree c;
21734
 
21735
  check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21736
 
21737
  c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21738
  OMP_CLAUSE_CHAIN (c) = list;
21739
  return c;
21740
}
21741
 
21742
/* Parse all OpenMP clauses.  The set clauses allowed by the directive
21743
   is a bitmask in MASK.  Return the list of clauses found; the result
21744
   of clause default goes in *pdefault.  */
21745
 
21746
static tree
21747
cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21748
                           const char *where, cp_token *pragma_tok)
21749
{
21750
  tree clauses = NULL;
21751
  bool first = true;
21752
  cp_token *token = NULL;
21753
 
21754
  while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21755
    {
21756
      pragma_omp_clause c_kind;
21757
      const char *c_name;
21758
      tree prev = clauses;
21759
 
21760
      if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21761
        cp_lexer_consume_token (parser->lexer);
21762
 
21763
      token = cp_lexer_peek_token (parser->lexer);
21764
      c_kind = cp_parser_omp_clause_name (parser);
21765
      first = false;
21766
 
21767
      switch (c_kind)
21768
        {
21769
        case PRAGMA_OMP_CLAUSE_COLLAPSE:
21770
          clauses = cp_parser_omp_clause_collapse (parser, clauses,
21771
                                                   token->location);
21772
          c_name = "collapse";
21773
          break;
21774
        case PRAGMA_OMP_CLAUSE_COPYIN:
21775
          clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21776
          c_name = "copyin";
21777
          break;
21778
        case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21779
          clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21780
                                            clauses);
21781
          c_name = "copyprivate";
21782
          break;
21783
        case PRAGMA_OMP_CLAUSE_DEFAULT:
21784
          clauses = cp_parser_omp_clause_default (parser, clauses,
21785
                                                  token->location);
21786
          c_name = "default";
21787
          break;
21788
        case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21789
          clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21790
                                            clauses);
21791
          c_name = "firstprivate";
21792
          break;
21793
        case PRAGMA_OMP_CLAUSE_IF:
21794
          clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21795
          c_name = "if";
21796
          break;
21797
        case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21798
          clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21799
                                            clauses);
21800
          c_name = "lastprivate";
21801
          break;
21802
        case PRAGMA_OMP_CLAUSE_NOWAIT:
21803
          clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21804
          c_name = "nowait";
21805
          break;
21806
        case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21807
          clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21808
                                                      token->location);
21809
          c_name = "num_threads";
21810
          break;
21811
        case PRAGMA_OMP_CLAUSE_ORDERED:
21812
          clauses = cp_parser_omp_clause_ordered (parser, clauses,
21813
                                                  token->location);
21814
          c_name = "ordered";
21815
          break;
21816
        case PRAGMA_OMP_CLAUSE_PRIVATE:
21817
          clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21818
                                            clauses);
21819
          c_name = "private";
21820
          break;
21821
        case PRAGMA_OMP_CLAUSE_REDUCTION:
21822
          clauses = cp_parser_omp_clause_reduction (parser, clauses);
21823
          c_name = "reduction";
21824
          break;
21825
        case PRAGMA_OMP_CLAUSE_SCHEDULE:
21826
          clauses = cp_parser_omp_clause_schedule (parser, clauses,
21827
                                                   token->location);
21828
          c_name = "schedule";
21829
          break;
21830
        case PRAGMA_OMP_CLAUSE_SHARED:
21831
          clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21832
                                            clauses);
21833
          c_name = "shared";
21834
          break;
21835
        case PRAGMA_OMP_CLAUSE_UNTIED:
21836
          clauses = cp_parser_omp_clause_untied (parser, clauses,
21837
                                                 token->location);
21838
          c_name = "nowait";
21839
          break;
21840
        default:
21841
          cp_parser_error (parser, "expected %<#pragma omp%> clause");
21842
          goto saw_error;
21843
        }
21844
 
21845
      if (((mask >> c_kind) & 1) == 0)
21846
        {
21847
          /* Remove the invalid clause(s) from the list to avoid
21848
             confusing the rest of the compiler.  */
21849
          clauses = prev;
21850
          error_at (token->location, "%qs is not valid for %qs", c_name, where);
21851
        }
21852
    }
21853
 saw_error:
21854
  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21855
  return finish_omp_clauses (clauses);
21856
}
21857
 
21858
/* OpenMP 2.5:
21859
   structured-block:
21860
     statement
21861
 
21862
   In practice, we're also interested in adding the statement to an
21863
   outer node.  So it is convenient if we work around the fact that
21864
   cp_parser_statement calls add_stmt.  */
21865
 
21866
static unsigned
21867
cp_parser_begin_omp_structured_block (cp_parser *parser)
21868
{
21869
  unsigned save = parser->in_statement;
21870
 
21871
  /* Only move the values to IN_OMP_BLOCK if they weren't false.
21872
     This preserves the "not within loop or switch" style error messages
21873
     for nonsense cases like
21874
        void foo() {
21875
        #pragma omp single
21876
          break;
21877
        }
21878
  */
21879
  if (parser->in_statement)
21880
    parser->in_statement = IN_OMP_BLOCK;
21881
 
21882
  return save;
21883
}
21884
 
21885
static void
21886
cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21887
{
21888
  parser->in_statement = save;
21889
}
21890
 
21891
static tree
21892
cp_parser_omp_structured_block (cp_parser *parser)
21893
{
21894
  tree stmt = begin_omp_structured_block ();
21895
  unsigned int save = cp_parser_begin_omp_structured_block (parser);
21896
 
21897
  cp_parser_statement (parser, NULL_TREE, false, NULL);
21898
 
21899
  cp_parser_end_omp_structured_block (parser, save);
21900
  return finish_omp_structured_block (stmt);
21901
}
21902
 
21903
/* OpenMP 2.5:
21904
   # pragma omp atomic new-line
21905
     expression-stmt
21906
 
21907
   expression-stmt:
21908
     x binop= expr | x++ | ++x | x-- | --x
21909
   binop:
21910
     +, *, -, /, &, ^, |, <<, >>
21911
 
21912
  where x is an lvalue expression with scalar type.  */
21913
 
21914
static void
21915
cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21916
{
21917
  tree lhs, rhs;
21918
  enum tree_code code;
21919
 
21920
  cp_parser_require_pragma_eol (parser, pragma_tok);
21921
 
21922
  lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21923
                                    /*cast_p=*/false, NULL);
21924
  switch (TREE_CODE (lhs))
21925
    {
21926
    case ERROR_MARK:
21927
      goto saw_error;
21928
 
21929
    case PREINCREMENT_EXPR:
21930
    case POSTINCREMENT_EXPR:
21931
      lhs = TREE_OPERAND (lhs, 0);
21932
      code = PLUS_EXPR;
21933
      rhs = integer_one_node;
21934
      break;
21935
 
21936
    case PREDECREMENT_EXPR:
21937
    case POSTDECREMENT_EXPR:
21938
      lhs = TREE_OPERAND (lhs, 0);
21939
      code = MINUS_EXPR;
21940
      rhs = integer_one_node;
21941
      break;
21942
 
21943
    default:
21944
      switch (cp_lexer_peek_token (parser->lexer)->type)
21945
        {
21946
        case CPP_MULT_EQ:
21947
          code = MULT_EXPR;
21948
          break;
21949
        case CPP_DIV_EQ:
21950
          code = TRUNC_DIV_EXPR;
21951
          break;
21952
        case CPP_PLUS_EQ:
21953
          code = PLUS_EXPR;
21954
          break;
21955
        case CPP_MINUS_EQ:
21956
          code = MINUS_EXPR;
21957
          break;
21958
        case CPP_LSHIFT_EQ:
21959
          code = LSHIFT_EXPR;
21960
          break;
21961
        case CPP_RSHIFT_EQ:
21962
          code = RSHIFT_EXPR;
21963
          break;
21964
        case CPP_AND_EQ:
21965
          code = BIT_AND_EXPR;
21966
          break;
21967
        case CPP_OR_EQ:
21968
          code = BIT_IOR_EXPR;
21969
          break;
21970
        case CPP_XOR_EQ:
21971
          code = BIT_XOR_EXPR;
21972
          break;
21973
        default:
21974
          cp_parser_error (parser,
21975
                           "invalid operator for %<#pragma omp atomic%>");
21976
          goto saw_error;
21977
        }
21978
      cp_lexer_consume_token (parser->lexer);
21979
 
21980
      rhs = cp_parser_expression (parser, false, NULL);
21981
      if (rhs == error_mark_node)
21982
        goto saw_error;
21983
      break;
21984
    }
21985
  finish_omp_atomic (code, lhs, rhs);
21986
  cp_parser_consume_semicolon_at_end_of_statement (parser);
21987
  return;
21988
 
21989
 saw_error:
21990
  cp_parser_skip_to_end_of_block_or_statement (parser);
21991
}
21992
 
21993
 
21994
/* OpenMP 2.5:
21995
   # pragma omp barrier new-line  */
21996
 
21997
static void
21998
cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21999
{
22000
  cp_parser_require_pragma_eol (parser, pragma_tok);
22001
  finish_omp_barrier ();
22002
}
22003
 
22004
/* OpenMP 2.5:
22005
   # pragma omp critical [(name)] new-line
22006
     structured-block  */
22007
 
22008
static tree
22009
cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
22010
{
22011
  tree stmt, name = NULL;
22012
 
22013
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22014
    {
22015
      cp_lexer_consume_token (parser->lexer);
22016
 
22017
      name = cp_parser_identifier (parser);
22018
 
22019
      if (name == error_mark_node
22020
          || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22021
        cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22022
                                               /*or_comma=*/false,
22023
                                               /*consume_paren=*/true);
22024
      if (name == error_mark_node)
22025
        name = NULL;
22026
    }
22027
  cp_parser_require_pragma_eol (parser, pragma_tok);
22028
 
22029
  stmt = cp_parser_omp_structured_block (parser);
22030
  return c_finish_omp_critical (input_location, stmt, name);
22031
}
22032
 
22033
/* OpenMP 2.5:
22034
   # pragma omp flush flush-vars[opt] new-line
22035
 
22036
   flush-vars:
22037
     ( variable-list ) */
22038
 
22039
static void
22040
cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22041
{
22042
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22043
    (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22044
  cp_parser_require_pragma_eol (parser, pragma_tok);
22045
 
22046
  finish_omp_flush ();
22047
}
22048
 
22049
/* Helper function, to parse omp for increment expression.  */
22050
 
22051
static tree
22052
cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22053
{
22054
  tree cond = cp_parser_binary_expression (parser, false, true,
22055
                                           PREC_NOT_OPERATOR, NULL);
22056
  bool overloaded_p;
22057
 
22058
  if (cond == error_mark_node
22059
      || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22060
    {
22061
      cp_parser_skip_to_end_of_statement (parser);
22062
      return error_mark_node;
22063
    }
22064
 
22065
  switch (TREE_CODE (cond))
22066
    {
22067
    case GT_EXPR:
22068
    case GE_EXPR:
22069
    case LT_EXPR:
22070
    case LE_EXPR:
22071
      break;
22072
    default:
22073
      return error_mark_node;
22074
    }
22075
 
22076
  /* If decl is an iterator, preserve LHS and RHS of the relational
22077
     expr until finish_omp_for.  */
22078
  if (decl
22079
      && (type_dependent_expression_p (decl)
22080
          || CLASS_TYPE_P (TREE_TYPE (decl))))
22081
    return cond;
22082
 
22083
  return build_x_binary_op (TREE_CODE (cond),
22084
                            TREE_OPERAND (cond, 0), ERROR_MARK,
22085
                            TREE_OPERAND (cond, 1), ERROR_MARK,
22086
                            &overloaded_p, tf_warning_or_error);
22087
}
22088
 
22089
/* Helper function, to parse omp for increment expression.  */
22090
 
22091
static tree
22092
cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22093
{
22094
  cp_token *token = cp_lexer_peek_token (parser->lexer);
22095
  enum tree_code op;
22096
  tree lhs, rhs;
22097
  cp_id_kind idk;
22098
  bool decl_first;
22099
 
22100
  if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22101
    {
22102
      op = (token->type == CPP_PLUS_PLUS
22103
            ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22104
      cp_lexer_consume_token (parser->lexer);
22105
      lhs = cp_parser_cast_expression (parser, false, false, NULL);
22106
      if (lhs != decl)
22107
        return error_mark_node;
22108
      return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22109
    }
22110
 
22111
  lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22112
  if (lhs != decl)
22113
    return error_mark_node;
22114
 
22115
  token = cp_lexer_peek_token (parser->lexer);
22116
  if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22117
    {
22118
      op = (token->type == CPP_PLUS_PLUS
22119
            ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22120
      cp_lexer_consume_token (parser->lexer);
22121
      return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22122
    }
22123
 
22124
  op = cp_parser_assignment_operator_opt (parser);
22125
  if (op == ERROR_MARK)
22126
    return error_mark_node;
22127
 
22128
  if (op != NOP_EXPR)
22129
    {
22130
      rhs = cp_parser_assignment_expression (parser, false, NULL);
22131
      rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22132
      return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22133
    }
22134
 
22135
  lhs = cp_parser_binary_expression (parser, false, false,
22136
                                     PREC_ADDITIVE_EXPRESSION, NULL);
22137
  token = cp_lexer_peek_token (parser->lexer);
22138
  decl_first = lhs == decl;
22139
  if (decl_first)
22140
    lhs = NULL_TREE;
22141
  if (token->type != CPP_PLUS
22142
      && token->type != CPP_MINUS)
22143
    return error_mark_node;
22144
 
22145
  do
22146
    {
22147
      op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22148
      cp_lexer_consume_token (parser->lexer);
22149
      rhs = cp_parser_binary_expression (parser, false, false,
22150
                                         PREC_ADDITIVE_EXPRESSION, NULL);
22151
      token = cp_lexer_peek_token (parser->lexer);
22152
      if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22153
        {
22154
          if (lhs == NULL_TREE)
22155
            {
22156
              if (op == PLUS_EXPR)
22157
                lhs = rhs;
22158
              else
22159
                lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22160
            }
22161
          else
22162
            lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22163
                                     NULL, tf_warning_or_error);
22164
        }
22165
    }
22166
  while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22167
 
22168
  if (!decl_first)
22169
    {
22170
      if (rhs != decl || op == MINUS_EXPR)
22171
        return error_mark_node;
22172
      rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22173
    }
22174
  else
22175
    rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22176
 
22177
  return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22178
}
22179
 
22180
/* Parse the restricted form of the for statement allowed by OpenMP.  */
22181
 
22182
static tree
22183
cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22184
{
22185
  tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22186
  tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22187
  tree this_pre_body, cl;
22188
  location_t loc_first;
22189
  bool collapse_err = false;
22190
  int i, collapse = 1, nbraces = 0;
22191
 
22192
  for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22193
    if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22194
      collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22195
 
22196
  gcc_assert (collapse >= 1);
22197
 
22198
  declv = make_tree_vec (collapse);
22199
  initv = make_tree_vec (collapse);
22200
  condv = make_tree_vec (collapse);
22201
  incrv = make_tree_vec (collapse);
22202
 
22203
  loc_first = cp_lexer_peek_token (parser->lexer)->location;
22204
 
22205
  for (i = 0; i < collapse; i++)
22206
    {
22207
      int bracecount = 0;
22208
      bool add_private_clause = false;
22209
      location_t loc;
22210
 
22211
      if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22212
        {
22213
          cp_parser_error (parser, "for statement expected");
22214
          return NULL;
22215
        }
22216
      loc = cp_lexer_consume_token (parser->lexer)->location;
22217
 
22218
      if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22219
        return NULL;
22220
 
22221
      init = decl = real_decl = NULL;
22222
      this_pre_body = push_stmt_list ();
22223
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22224
        {
22225
          /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22226
 
22227
             init-expr:
22228
                       var = lb
22229
                       integer-type var = lb
22230
                       random-access-iterator-type var = lb
22231
                       pointer-type var = lb
22232
          */
22233
          cp_decl_specifier_seq type_specifiers;
22234
 
22235
          /* First, try to parse as an initialized declaration.  See
22236
             cp_parser_condition, from whence the bulk of this is copied.  */
22237
 
22238
          cp_parser_parse_tentatively (parser);
22239
          cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22240
                                        /*is_trailing_return=*/false,
22241
                                        &type_specifiers);
22242
          if (cp_parser_parse_definitely (parser))
22243
            {
22244
              /* If parsing a type specifier seq succeeded, then this
22245
                 MUST be a initialized declaration.  */
22246
              tree asm_specification, attributes;
22247
              cp_declarator *declarator;
22248
 
22249
              declarator = cp_parser_declarator (parser,
22250
                                                 CP_PARSER_DECLARATOR_NAMED,
22251
                                                 /*ctor_dtor_or_conv_p=*/NULL,
22252
                                                 /*parenthesized_p=*/NULL,
22253
                                                 /*member_p=*/false);
22254
              attributes = cp_parser_attributes_opt (parser);
22255
              asm_specification = cp_parser_asm_specification_opt (parser);
22256
 
22257
              if (declarator == cp_error_declarator)
22258
                cp_parser_skip_to_end_of_statement (parser);
22259
 
22260
              else
22261
                {
22262
                  tree pushed_scope, auto_node;
22263
 
22264
                  decl = start_decl (declarator, &type_specifiers,
22265
                                     SD_INITIALIZED, attributes,
22266
                                     /*prefix_attributes=*/NULL_TREE,
22267
                                     &pushed_scope);
22268
 
22269
                  auto_node = type_uses_auto (TREE_TYPE (decl));
22270
                  if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22271
                    {
22272
                      if (cp_lexer_next_token_is (parser->lexer,
22273
                                                  CPP_OPEN_PAREN))
22274
                        error ("parenthesized initialization is not allowed in "
22275
                               "OpenMP %<for%> loop");
22276
                      else
22277
                        /* Trigger an error.  */
22278
                        cp_parser_require (parser, CPP_EQ, "%<=%>");
22279
 
22280
                      init = error_mark_node;
22281
                      cp_parser_skip_to_end_of_statement (parser);
22282
                    }
22283
                  else if (CLASS_TYPE_P (TREE_TYPE (decl))
22284
                           || type_dependent_expression_p (decl)
22285
                           || auto_node)
22286
                    {
22287
                      bool is_direct_init, is_non_constant_init;
22288
 
22289
                      init = cp_parser_initializer (parser,
22290
                                                    &is_direct_init,
22291
                                                    &is_non_constant_init);
22292
 
22293
                      if (auto_node && describable_type (init))
22294
                        {
22295
                          TREE_TYPE (decl)
22296
                            = do_auto_deduction (TREE_TYPE (decl), init,
22297
                                                 auto_node);
22298
 
22299
                          if (!CLASS_TYPE_P (TREE_TYPE (decl))
22300
                              && !type_dependent_expression_p (decl))
22301
                            goto non_class;
22302
                        }
22303
 
22304
                      cp_finish_decl (decl, init, !is_non_constant_init,
22305
                                      asm_specification,
22306
                                      LOOKUP_ONLYCONVERTING);
22307
                      if (CLASS_TYPE_P (TREE_TYPE (decl)))
22308
                        {
22309
                          for_block
22310
                            = tree_cons (NULL, this_pre_body, for_block);
22311
                          init = NULL_TREE;
22312
                        }
22313
                      else
22314
                        init = pop_stmt_list (this_pre_body);
22315
                      this_pre_body = NULL_TREE;
22316
                    }
22317
                  else
22318
                    {
22319
                      /* Consume '='.  */
22320
                      cp_lexer_consume_token (parser->lexer);
22321
                      init = cp_parser_assignment_expression (parser, false, NULL);
22322
 
22323
                    non_class:
22324
                      if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22325
                        init = error_mark_node;
22326
                      else
22327
                        cp_finish_decl (decl, NULL_TREE,
22328
                                        /*init_const_expr_p=*/false,
22329
                                        asm_specification,
22330
                                        LOOKUP_ONLYCONVERTING);
22331
                    }
22332
 
22333
                  if (pushed_scope)
22334
                    pop_scope (pushed_scope);
22335
                }
22336
            }
22337
          else
22338
            {
22339
              cp_id_kind idk;
22340
              /* If parsing a type specifier sequence failed, then
22341
                 this MUST be a simple expression.  */
22342
              cp_parser_parse_tentatively (parser);
22343
              decl = cp_parser_primary_expression (parser, false, false,
22344
                                                   false, &idk);
22345
              if (!cp_parser_error_occurred (parser)
22346
                  && decl
22347
                  && DECL_P (decl)
22348
                  && CLASS_TYPE_P (TREE_TYPE (decl)))
22349
                {
22350
                  tree rhs;
22351
 
22352
                  cp_parser_parse_definitely (parser);
22353
                  cp_parser_require (parser, CPP_EQ, "%<=%>");
22354
                  rhs = cp_parser_assignment_expression (parser, false, NULL);
22355
                  finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22356
                                                         rhs,
22357
                                                         tf_warning_or_error));
22358
                  add_private_clause = true;
22359
                }
22360
              else
22361
                {
22362
                  decl = NULL;
22363
                  cp_parser_abort_tentative_parse (parser);
22364
                  init = cp_parser_expression (parser, false, NULL);
22365
                  if (init)
22366
                    {
22367
                      if (TREE_CODE (init) == MODIFY_EXPR
22368
                          || TREE_CODE (init) == MODOP_EXPR)
22369
                        real_decl = TREE_OPERAND (init, 0);
22370
                    }
22371
                }
22372
            }
22373
        }
22374
      cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22375
      if (this_pre_body)
22376
        {
22377
          this_pre_body = pop_stmt_list (this_pre_body);
22378
          if (pre_body)
22379
            {
22380
              tree t = pre_body;
22381
              pre_body = push_stmt_list ();
22382
              add_stmt (t);
22383
              add_stmt (this_pre_body);
22384
              pre_body = pop_stmt_list (pre_body);
22385
            }
22386
          else
22387
            pre_body = this_pre_body;
22388
        }
22389
 
22390
      if (decl)
22391
        real_decl = decl;
22392
      if (par_clauses != NULL && real_decl != NULL_TREE)
22393
        {
22394
          tree *c;
22395
          for (c = par_clauses; *c ; )
22396
            if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22397
                && OMP_CLAUSE_DECL (*c) == real_decl)
22398
              {
22399
                error_at (loc, "iteration variable %qD"
22400
                          " should not be firstprivate", real_decl);
22401
                *c = OMP_CLAUSE_CHAIN (*c);
22402
              }
22403
            else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22404
                     && OMP_CLAUSE_DECL (*c) == real_decl)
22405
              {
22406
                /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22407
                   change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
22408
                tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22409
                OMP_CLAUSE_DECL (l) = real_decl;
22410
                OMP_CLAUSE_CHAIN (l) = clauses;
22411
                CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22412
                clauses = l;
22413
                OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22414
                CP_OMP_CLAUSE_INFO (*c) = NULL;
22415
                add_private_clause = false;
22416
              }
22417
            else
22418
              {
22419
                if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22420
                    && OMP_CLAUSE_DECL (*c) == real_decl)
22421
                  add_private_clause = false;
22422
                c = &OMP_CLAUSE_CHAIN (*c);
22423
              }
22424
        }
22425
 
22426
      if (add_private_clause)
22427
        {
22428
          tree c;
22429
          for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22430
            {
22431
              if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22432
                   || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22433
                  && OMP_CLAUSE_DECL (c) == decl)
22434
                break;
22435
              else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22436
                       && OMP_CLAUSE_DECL (c) == decl)
22437
                error_at (loc, "iteration variable %qD "
22438
                          "should not be firstprivate",
22439
                          decl);
22440
              else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22441
                       && OMP_CLAUSE_DECL (c) == decl)
22442
                error_at (loc, "iteration variable %qD should not be reduction",
22443
                          decl);
22444
            }
22445
          if (c == NULL)
22446
            {
22447
              c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22448
              OMP_CLAUSE_DECL (c) = decl;
22449
              c = finish_omp_clauses (c);
22450
              if (c)
22451
                {
22452
                  OMP_CLAUSE_CHAIN (c) = clauses;
22453
                  clauses = c;
22454
                }
22455
            }
22456
        }
22457
 
22458
      cond = NULL;
22459
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22460
        cond = cp_parser_omp_for_cond (parser, decl);
22461
      cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22462
 
22463
      incr = NULL;
22464
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22465
        {
22466
          /* If decl is an iterator, preserve the operator on decl
22467
             until finish_omp_for.  */
22468
          if (decl
22469
              && (type_dependent_expression_p (decl)
22470
                  || CLASS_TYPE_P (TREE_TYPE (decl))))
22471
            incr = cp_parser_omp_for_incr (parser, decl);
22472
          else
22473
            incr = cp_parser_expression (parser, false, NULL);
22474
        }
22475
 
22476
      if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22477
        cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22478
                                               /*or_comma=*/false,
22479
                                               /*consume_paren=*/true);
22480
 
22481
      TREE_VEC_ELT (declv, i) = decl;
22482
      TREE_VEC_ELT (initv, i) = init;
22483
      TREE_VEC_ELT (condv, i) = cond;
22484
      TREE_VEC_ELT (incrv, i) = incr;
22485
 
22486
      if (i == collapse - 1)
22487
        break;
22488
 
22489
      /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22490
         in between the collapsed for loops to be still considered perfectly
22491
         nested.  Hopefully the final version clarifies this.
22492
         For now handle (multiple) {'s and empty statements.  */
22493
      cp_parser_parse_tentatively (parser);
22494
      do
22495
        {
22496
          if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22497
            break;
22498
          else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22499
            {
22500
              cp_lexer_consume_token (parser->lexer);
22501
              bracecount++;
22502
            }
22503
          else if (bracecount
22504
                   && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22505
            cp_lexer_consume_token (parser->lexer);
22506
          else
22507
            {
22508
              loc = cp_lexer_peek_token (parser->lexer)->location;
22509
              error_at (loc, "not enough collapsed for loops");
22510
              collapse_err = true;
22511
              cp_parser_abort_tentative_parse (parser);
22512
              declv = NULL_TREE;
22513
              break;
22514
            }
22515
        }
22516
      while (1);
22517
 
22518
      if (declv)
22519
        {
22520
          cp_parser_parse_definitely (parser);
22521
          nbraces += bracecount;
22522
        }
22523
    }
22524
 
22525
  /* Note that we saved the original contents of this flag when we entered
22526
     the structured block, and so we don't need to re-save it here.  */
22527
  parser->in_statement = IN_OMP_FOR;
22528
 
22529
  /* Note that the grammar doesn't call for a structured block here,
22530
     though the loop as a whole is a structured block.  */
22531
  body = push_stmt_list ();
22532
  cp_parser_statement (parser, NULL_TREE, false, NULL);
22533
  body = pop_stmt_list (body);
22534
 
22535
  if (declv == NULL_TREE)
22536
    ret = NULL_TREE;
22537
  else
22538
    ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22539
                          pre_body, clauses);
22540
 
22541
  while (nbraces)
22542
    {
22543
      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22544
        {
22545
          cp_lexer_consume_token (parser->lexer);
22546
          nbraces--;
22547
        }
22548
      else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22549
        cp_lexer_consume_token (parser->lexer);
22550
      else
22551
        {
22552
          if (!collapse_err)
22553
            {
22554
              error_at (cp_lexer_peek_token (parser->lexer)->location,
22555
                        "collapsed loops not perfectly nested");
22556
            }
22557
          collapse_err = true;
22558
          cp_parser_statement_seq_opt (parser, NULL);
22559
          if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22560
            break;
22561
        }
22562
    }
22563
 
22564
  while (for_block)
22565
    {
22566
      add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22567
      for_block = TREE_CHAIN (for_block);
22568
    }
22569
 
22570
  return ret;
22571
}
22572
 
22573
/* OpenMP 2.5:
22574
   #pragma omp for for-clause[optseq] new-line
22575
     for-loop  */
22576
 
22577
#define OMP_FOR_CLAUSE_MASK                             \
22578
        ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22579
        | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22580
        | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22581
        | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22582
        | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
22583
        | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
22584
        | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
22585
        | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22586
 
22587
static tree
22588
cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22589
{
22590
  tree clauses, sb, ret;
22591
  unsigned int save;
22592
 
22593
  clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22594
                                       "#pragma omp for", pragma_tok);
22595
 
22596
  sb = begin_omp_structured_block ();
22597
  save = cp_parser_begin_omp_structured_block (parser);
22598
 
22599
  ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22600
 
22601
  cp_parser_end_omp_structured_block (parser, save);
22602
  add_stmt (finish_omp_structured_block (sb));
22603
 
22604
  return ret;
22605
}
22606
 
22607
/* OpenMP 2.5:
22608
   # pragma omp master new-line
22609
     structured-block  */
22610
 
22611
static tree
22612
cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22613
{
22614
  cp_parser_require_pragma_eol (parser, pragma_tok);
22615
  return c_finish_omp_master (input_location,
22616
                              cp_parser_omp_structured_block (parser));
22617
}
22618
 
22619
/* OpenMP 2.5:
22620
   # pragma omp ordered new-line
22621
     structured-block  */
22622
 
22623
static tree
22624
cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22625
{
22626
  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22627
  cp_parser_require_pragma_eol (parser, pragma_tok);
22628
  return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22629
}
22630
 
22631
/* OpenMP 2.5:
22632
 
22633
   section-scope:
22634
     { section-sequence }
22635
 
22636
   section-sequence:
22637
     section-directive[opt] structured-block
22638
     section-sequence section-directive structured-block  */
22639
 
22640
static tree
22641
cp_parser_omp_sections_scope (cp_parser *parser)
22642
{
22643
  tree stmt, substmt;
22644
  bool error_suppress = false;
22645
  cp_token *tok;
22646
 
22647
  if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22648
    return NULL_TREE;
22649
 
22650
  stmt = push_stmt_list ();
22651
 
22652
  if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22653
    {
22654
      unsigned save;
22655
 
22656
      substmt = begin_omp_structured_block ();
22657
      save = cp_parser_begin_omp_structured_block (parser);
22658
 
22659
      while (1)
22660
        {
22661
          cp_parser_statement (parser, NULL_TREE, false, NULL);
22662
 
22663
          tok = cp_lexer_peek_token (parser->lexer);
22664
          if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22665
            break;
22666
          if (tok->type == CPP_CLOSE_BRACE)
22667
            break;
22668
          if (tok->type == CPP_EOF)
22669
            break;
22670
        }
22671
 
22672
      cp_parser_end_omp_structured_block (parser, save);
22673
      substmt = finish_omp_structured_block (substmt);
22674
      substmt = build1 (OMP_SECTION, void_type_node, substmt);
22675
      add_stmt (substmt);
22676
    }
22677
 
22678
  while (1)
22679
    {
22680
      tok = cp_lexer_peek_token (parser->lexer);
22681
      if (tok->type == CPP_CLOSE_BRACE)
22682
        break;
22683
      if (tok->type == CPP_EOF)
22684
        break;
22685
 
22686
      if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22687
        {
22688
          cp_lexer_consume_token (parser->lexer);
22689
          cp_parser_require_pragma_eol (parser, tok);
22690
          error_suppress = false;
22691
        }
22692
      else if (!error_suppress)
22693
        {
22694
          cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22695
          error_suppress = true;
22696
        }
22697
 
22698
      substmt = cp_parser_omp_structured_block (parser);
22699
      substmt = build1 (OMP_SECTION, void_type_node, substmt);
22700
      add_stmt (substmt);
22701
    }
22702
  cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22703
 
22704
  substmt = pop_stmt_list (stmt);
22705
 
22706
  stmt = make_node (OMP_SECTIONS);
22707
  TREE_TYPE (stmt) = void_type_node;
22708
  OMP_SECTIONS_BODY (stmt) = substmt;
22709
 
22710
  add_stmt (stmt);
22711
  return stmt;
22712
}
22713
 
22714
/* OpenMP 2.5:
22715
   # pragma omp sections sections-clause[optseq] newline
22716
     sections-scope  */
22717
 
22718
#define OMP_SECTIONS_CLAUSE_MASK                        \
22719
        ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22720
        | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22721
        | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22722
        | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22723
        | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22724
 
22725
static tree
22726
cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22727
{
22728
  tree clauses, ret;
22729
 
22730
  clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22731
                                       "#pragma omp sections", pragma_tok);
22732
 
22733
  ret = cp_parser_omp_sections_scope (parser);
22734
  if (ret)
22735
    OMP_SECTIONS_CLAUSES (ret) = clauses;
22736
 
22737
  return ret;
22738
}
22739
 
22740
/* OpenMP 2.5:
22741
   # pragma parallel parallel-clause new-line
22742
   # pragma parallel for parallel-for-clause new-line
22743
   # pragma parallel sections parallel-sections-clause new-line  */
22744
 
22745
#define OMP_PARALLEL_CLAUSE_MASK                        \
22746
        ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22747
        | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22748
        | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22749
        | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22750
        | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
22751
        | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
22752
        | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22753
        | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22754
 
22755
static tree
22756
cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22757
{
22758
  enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22759
  const char *p_name = "#pragma omp parallel";
22760
  tree stmt, clauses, par_clause, ws_clause, block;
22761
  unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22762
  unsigned int save;
22763
  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22764
 
22765
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22766
    {
22767
      cp_lexer_consume_token (parser->lexer);
22768
      p_kind = PRAGMA_OMP_PARALLEL_FOR;
22769
      p_name = "#pragma omp parallel for";
22770
      mask |= OMP_FOR_CLAUSE_MASK;
22771
      mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22772
    }
22773
  else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22774
    {
22775
      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22776
      const char *p = IDENTIFIER_POINTER (id);
22777
      if (strcmp (p, "sections") == 0)
22778
        {
22779
          cp_lexer_consume_token (parser->lexer);
22780
          p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22781
          p_name = "#pragma omp parallel sections";
22782
          mask |= OMP_SECTIONS_CLAUSE_MASK;
22783
          mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22784
        }
22785
    }
22786
 
22787
  clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22788
  block = begin_omp_parallel ();
22789
  save = cp_parser_begin_omp_structured_block (parser);
22790
 
22791
  switch (p_kind)
22792
    {
22793
    case PRAGMA_OMP_PARALLEL:
22794
      cp_parser_statement (parser, NULL_TREE, false, NULL);
22795
      par_clause = clauses;
22796
      break;
22797
 
22798
    case PRAGMA_OMP_PARALLEL_FOR:
22799
      c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22800
      cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22801
      break;
22802
 
22803
    case PRAGMA_OMP_PARALLEL_SECTIONS:
22804
      c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22805
      stmt = cp_parser_omp_sections_scope (parser);
22806
      if (stmt)
22807
        OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22808
      break;
22809
 
22810
    default:
22811
      gcc_unreachable ();
22812
    }
22813
 
22814
  cp_parser_end_omp_structured_block (parser, save);
22815
  stmt = finish_omp_parallel (par_clause, block);
22816
  if (p_kind != PRAGMA_OMP_PARALLEL)
22817
    OMP_PARALLEL_COMBINED (stmt) = 1;
22818
  return stmt;
22819
}
22820
 
22821
/* OpenMP 2.5:
22822
   # pragma omp single single-clause[optseq] new-line
22823
     structured-block  */
22824
 
22825
#define OMP_SINGLE_CLAUSE_MASK                          \
22826
        ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22827
        | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22828
        | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
22829
        | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22830
 
22831
static tree
22832
cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22833
{
22834
  tree stmt = make_node (OMP_SINGLE);
22835
  TREE_TYPE (stmt) = void_type_node;
22836
 
22837
  OMP_SINGLE_CLAUSES (stmt)
22838
    = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22839
                                 "#pragma omp single", pragma_tok);
22840
  OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22841
 
22842
  return add_stmt (stmt);
22843
}
22844
 
22845
/* OpenMP 3.0:
22846
   # pragma omp task task-clause[optseq] new-line
22847
     structured-block  */
22848
 
22849
#define OMP_TASK_CLAUSE_MASK                            \
22850
        ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22851
        | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
22852
        | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22853
        | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22854
        | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22855
        | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22856
 
22857
static tree
22858
cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22859
{
22860
  tree clauses, block;
22861
  unsigned int save;
22862
 
22863
  clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22864
                                       "#pragma omp task", pragma_tok);
22865
  block = begin_omp_task ();
22866
  save = cp_parser_begin_omp_structured_block (parser);
22867
  cp_parser_statement (parser, NULL_TREE, false, NULL);
22868
  cp_parser_end_omp_structured_block (parser, save);
22869
  return finish_omp_task (clauses, block);
22870
}
22871
 
22872
/* OpenMP 3.0:
22873
   # pragma omp taskwait new-line  */
22874
 
22875
static void
22876
cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22877
{
22878
  cp_parser_require_pragma_eol (parser, pragma_tok);
22879
  finish_omp_taskwait ();
22880
}
22881
 
22882
/* OpenMP 2.5:
22883
   # pragma omp threadprivate (variable-list) */
22884
 
22885
static void
22886
cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22887
{
22888
  tree vars;
22889
 
22890
  vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22891
  cp_parser_require_pragma_eol (parser, pragma_tok);
22892
 
22893
  finish_omp_threadprivate (vars);
22894
}
22895
 
22896
/* Main entry point to OpenMP statement pragmas.  */
22897
 
22898
static void
22899
cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22900
{
22901
  tree stmt;
22902
 
22903
  switch (pragma_tok->pragma_kind)
22904
    {
22905
    case PRAGMA_OMP_ATOMIC:
22906
      cp_parser_omp_atomic (parser, pragma_tok);
22907
      return;
22908
    case PRAGMA_OMP_CRITICAL:
22909
      stmt = cp_parser_omp_critical (parser, pragma_tok);
22910
      break;
22911
    case PRAGMA_OMP_FOR:
22912
      stmt = cp_parser_omp_for (parser, pragma_tok);
22913
      break;
22914
    case PRAGMA_OMP_MASTER:
22915
      stmt = cp_parser_omp_master (parser, pragma_tok);
22916
      break;
22917
    case PRAGMA_OMP_ORDERED:
22918
      stmt = cp_parser_omp_ordered (parser, pragma_tok);
22919
      break;
22920
    case PRAGMA_OMP_PARALLEL:
22921
      stmt = cp_parser_omp_parallel (parser, pragma_tok);
22922
      break;
22923
    case PRAGMA_OMP_SECTIONS:
22924
      stmt = cp_parser_omp_sections (parser, pragma_tok);
22925
      break;
22926
    case PRAGMA_OMP_SINGLE:
22927
      stmt = cp_parser_omp_single (parser, pragma_tok);
22928
      break;
22929
    case PRAGMA_OMP_TASK:
22930
      stmt = cp_parser_omp_task (parser, pragma_tok);
22931
      break;
22932
    default:
22933
      gcc_unreachable ();
22934
    }
22935
 
22936
  if (stmt)
22937
    SET_EXPR_LOCATION (stmt, pragma_tok->location);
22938
}
22939
 
22940
/* The parser.  */
22941
 
22942
static GTY (()) cp_parser *the_parser;
22943
 
22944
 
22945
/* Special handling for the first token or line in the file.  The first
22946
   thing in the file might be #pragma GCC pch_preprocess, which loads a
22947
   PCH file, which is a GC collection point.  So we need to handle this
22948
   first pragma without benefit of an existing lexer structure.
22949
 
22950
   Always returns one token to the caller in *FIRST_TOKEN.  This is
22951
   either the true first token of the file, or the first token after
22952
   the initial pragma.  */
22953
 
22954
static void
22955
cp_parser_initial_pragma (cp_token *first_token)
22956
{
22957
  tree name = NULL;
22958
 
22959
  cp_lexer_get_preprocessor_token (NULL, first_token);
22960
  if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22961
    return;
22962
 
22963
  cp_lexer_get_preprocessor_token (NULL, first_token);
22964
  if (first_token->type == CPP_STRING)
22965
    {
22966
      name = first_token->u.value;
22967
 
22968
      cp_lexer_get_preprocessor_token (NULL, first_token);
22969
      if (first_token->type != CPP_PRAGMA_EOL)
22970
        error_at (first_token->location,
22971
                  "junk at end of %<#pragma GCC pch_preprocess%>");
22972
    }
22973
  else
22974
    error_at (first_token->location, "expected string literal");
22975
 
22976
  /* Skip to the end of the pragma.  */
22977
  while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22978
    cp_lexer_get_preprocessor_token (NULL, first_token);
22979
 
22980
  /* Now actually load the PCH file.  */
22981
  if (name)
22982
    c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22983
 
22984
  /* Read one more token to return to our caller.  We have to do this
22985
     after reading the PCH file in, since its pointers have to be
22986
     live.  */
22987
  cp_lexer_get_preprocessor_token (NULL, first_token);
22988
}
22989
 
22990
/* Normal parsing of a pragma token.  Here we can (and must) use the
22991
   regular lexer.  */
22992
 
22993
static bool
22994
cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22995
{
22996
  cp_token *pragma_tok;
22997
  unsigned int id;
22998
 
22999
  pragma_tok = cp_lexer_consume_token (parser->lexer);
23000
  gcc_assert (pragma_tok->type == CPP_PRAGMA);
23001
  parser->lexer->in_pragma = true;
23002
 
23003
  id = pragma_tok->pragma_kind;
23004
  switch (id)
23005
    {
23006
    case PRAGMA_GCC_PCH_PREPROCESS:
23007
      error_at (pragma_tok->location,
23008
                "%<#pragma GCC pch_preprocess%> must be first");
23009
      break;
23010
 
23011
    case PRAGMA_OMP_BARRIER:
23012
      switch (context)
23013
        {
23014
        case pragma_compound:
23015
          cp_parser_omp_barrier (parser, pragma_tok);
23016
          return false;
23017
        case pragma_stmt:
23018
          error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
23019
                    "used in compound statements");
23020
          break;
23021
        default:
23022
          goto bad_stmt;
23023
        }
23024
      break;
23025
 
23026
    case PRAGMA_OMP_FLUSH:
23027
      switch (context)
23028
        {
23029
        case pragma_compound:
23030
          cp_parser_omp_flush (parser, pragma_tok);
23031
          return false;
23032
        case pragma_stmt:
23033
          error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23034
                    "used in compound statements");
23035
          break;
23036
        default:
23037
          goto bad_stmt;
23038
        }
23039
      break;
23040
 
23041
    case PRAGMA_OMP_TASKWAIT:
23042
      switch (context)
23043
        {
23044
        case pragma_compound:
23045
          cp_parser_omp_taskwait (parser, pragma_tok);
23046
          return false;
23047
        case pragma_stmt:
23048
          error_at (pragma_tok->location,
23049
                    "%<#pragma omp taskwait%> may only be "
23050
                    "used in compound statements");
23051
          break;
23052
        default:
23053
          goto bad_stmt;
23054
        }
23055
      break;
23056
 
23057
    case PRAGMA_OMP_THREADPRIVATE:
23058
      cp_parser_omp_threadprivate (parser, pragma_tok);
23059
      return false;
23060
 
23061
    case PRAGMA_OMP_ATOMIC:
23062
    case PRAGMA_OMP_CRITICAL:
23063
    case PRAGMA_OMP_FOR:
23064
    case PRAGMA_OMP_MASTER:
23065
    case PRAGMA_OMP_ORDERED:
23066
    case PRAGMA_OMP_PARALLEL:
23067
    case PRAGMA_OMP_SECTIONS:
23068
    case PRAGMA_OMP_SINGLE:
23069
    case PRAGMA_OMP_TASK:
23070
      if (context == pragma_external)
23071
        goto bad_stmt;
23072
      cp_parser_omp_construct (parser, pragma_tok);
23073
      return true;
23074
 
23075
    case PRAGMA_OMP_SECTION:
23076
      error_at (pragma_tok->location,
23077
                "%<#pragma omp section%> may only be used in "
23078
                "%<#pragma omp sections%> construct");
23079
      break;
23080
 
23081
    default:
23082
      gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23083
      c_invoke_pragma_handler (id);
23084
      break;
23085
 
23086
    bad_stmt:
23087
      cp_parser_error (parser, "expected declaration specifiers");
23088
      break;
23089
    }
23090
 
23091
  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23092
  return false;
23093
}
23094
 
23095
/* The interface the pragma parsers have to the lexer.  */
23096
 
23097
enum cpp_ttype
23098
pragma_lex (tree *value)
23099
{
23100
  cp_token *tok;
23101
  enum cpp_ttype ret;
23102
 
23103
  tok = cp_lexer_peek_token (the_parser->lexer);
23104
 
23105
  ret = tok->type;
23106
  *value = tok->u.value;
23107
 
23108
  if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23109
    ret = CPP_EOF;
23110
  else if (ret == CPP_STRING)
23111
    *value = cp_parser_string_literal (the_parser, false, false);
23112
  else
23113
    {
23114
      cp_lexer_consume_token (the_parser->lexer);
23115
      if (ret == CPP_KEYWORD)
23116
        ret = CPP_NAME;
23117
    }
23118
 
23119
  return ret;
23120
}
23121
 
23122
 
23123
/* External interface.  */
23124
 
23125
/* Parse one entire translation unit.  */
23126
 
23127
void
23128
c_parse_file (void)
23129
{
23130
  static bool already_called = false;
23131
 
23132
  if (already_called)
23133
    {
23134
      sorry ("inter-module optimizations not implemented for C++");
23135
      return;
23136
    }
23137
  already_called = true;
23138
 
23139
  the_parser = cp_parser_new ();
23140
  push_deferring_access_checks (flag_access_control
23141
                                ? dk_no_deferred : dk_no_check);
23142
  cp_parser_translation_unit (the_parser);
23143
  the_parser = NULL;
23144
}
23145
 
23146
#include "gt-cp-parser.h"

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.