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

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [gcc/] [cp/] [parser.c] - Blame information for rev 12

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 12 jlechner
/* C++ Parser.
2
   Copyright (C) 2000, 2001, 2002, 2003, 2004,
3
   2005  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 2, 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 COPYING.  If not, write to the Free
20
   Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21
   02110-1301, USA.  */
22
 
23
#include "config.h"
24
#include "system.h"
25
#include "coretypes.h"
26
#include "tm.h"
27
#include "dyn-string.h"
28
#include "varray.h"
29
#include "cpplib.h"
30
#include "tree.h"
31
#include "cp-tree.h"
32
#include "c-pragma.h"
33
#include "decl.h"
34
#include "flags.h"
35
#include "diagnostic.h"
36
#include "toplev.h"
37
#include "output.h"
38
#include "target.h"
39
#include "c-common.h"
40
 
41
 
42
/* The lexer.  */
43
 
44
/* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45
   and c-lex.c) and the C++ parser.  */
46
 
47
/* A C++ token.  */
48
 
49
typedef struct cp_token GTY (())
50
{
51
  /* The kind of token.  */
52
  ENUM_BITFIELD (cpp_ttype) type : 8;
53
  /* If this token is a keyword, this value indicates which keyword.
54
     Otherwise, this value is RID_MAX.  */
55
  ENUM_BITFIELD (rid) keyword : 8;
56
  /* Token flags.  */
57
  unsigned char flags;
58
  /* True if this token is from a system header.  */
59
  BOOL_BITFIELD in_system_header : 1;
60
  /* True if this token is from a context where it is implicitly extern "C" */
61
  BOOL_BITFIELD implicit_extern_c : 1;
62
  /* True for a CPP_NAME token that is not a keyword (i.e., for which
63
     KEYWORD is RID_MAX) iff this name was looked up and found to be
64
     ambiguous.  An error has already been reported.  */
65
  BOOL_BITFIELD ambiguous_p : 1;
66
  /* The value associated with this token, if any.  */
67
  tree value;
68
  /* The location at which this token was found.  */
69
  location_t location;
70
} cp_token;
71
 
72
/* We use a stack of token pointer for saving token sets.  */
73
typedef struct cp_token *cp_token_position;
74
DEF_VEC_P (cp_token_position);
75
DEF_VEC_ALLOC_P (cp_token_position,heap);
76
 
77
static const cp_token eof_token =
78
{
79
  CPP_EOF, RID_MAX, 0, 0, 0, false, NULL_TREE,
80
#if USE_MAPPED_LOCATION
81
 
82
#else
83
  {0, 0}
84
#endif
85
};
86
 
87
/* The cp_lexer structure represents the C++ lexer.  It is responsible
88
   for managing the token stream from the preprocessor and supplying
89
   it to the parser.  Tokens are never added to the cp_lexer after
90
   it is created.  */
91
 
92
typedef struct cp_lexer GTY (())
93
{
94
  /* The memory allocated for the buffer.  NULL if this lexer does not
95
     own the token buffer.  */
96
  cp_token * GTY ((length ("%h.buffer_length"))) buffer;
97
  /* If the lexer owns the buffer, this is the number of tokens in the
98
     buffer.  */
99
  size_t buffer_length;
100
 
101
  /* A pointer just past the last available token.  The tokens
102
     in this lexer are [buffer, last_token).  */
103
  cp_token_position GTY ((skip)) last_token;
104
 
105
  /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
106
     no more available tokens.  */
107
  cp_token_position GTY ((skip)) next_token;
108
 
109
  /* A stack indicating positions at which cp_lexer_save_tokens was
110
     called.  The top entry is the most recent position at which we
111
     began saving tokens.  If the stack is non-empty, we are saving
112
     tokens.  */
113
  VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
114
 
115
  /* True if we should output debugging information.  */
116
  bool debugging_p;
117
 
118
  /* The next lexer in a linked list of lexers.  */
119
  struct cp_lexer *next;
120
} cp_lexer;
121
 
122
/* cp_token_cache is a range of tokens.  There is no need to represent
123
   allocate heap memory for it, since tokens are never removed from the
124
   lexer's array.  There is also no need for the GC to walk through
125
   a cp_token_cache, since everything in here is referenced through
126
   a lexer.  */
127
 
128
typedef struct cp_token_cache GTY(())
129
{
130
  /* The beginning of the token range.  */
131
  cp_token * GTY((skip)) first;
132
 
133
  /* Points immediately after the last token in the range.  */
134
  cp_token * GTY ((skip)) last;
135
} cp_token_cache;
136
 
137
/* Prototypes.  */
138
 
139
static cp_lexer *cp_lexer_new_main
140
  (void);
141
static cp_lexer *cp_lexer_new_from_tokens
142
  (cp_token_cache *tokens);
143
static void cp_lexer_destroy
144
  (cp_lexer *);
145
static int cp_lexer_saving_tokens
146
  (const cp_lexer *);
147
static cp_token_position cp_lexer_token_position
148
  (cp_lexer *, bool);
149
static cp_token *cp_lexer_token_at
150
  (cp_lexer *, cp_token_position);
151
static void cp_lexer_get_preprocessor_token
152
  (cp_lexer *, cp_token *);
153
static inline cp_token *cp_lexer_peek_token
154
  (cp_lexer *);
155
static cp_token *cp_lexer_peek_nth_token
156
  (cp_lexer *, size_t);
157
static inline bool cp_lexer_next_token_is
158
  (cp_lexer *, enum cpp_ttype);
159
static bool cp_lexer_next_token_is_not
160
  (cp_lexer *, enum cpp_ttype);
161
static bool cp_lexer_next_token_is_keyword
162
  (cp_lexer *, enum rid);
163
static cp_token *cp_lexer_consume_token
164
  (cp_lexer *);
165
static void cp_lexer_purge_token
166
  (cp_lexer *);
167
static void cp_lexer_purge_tokens_after
168
  (cp_lexer *, cp_token_position);
169
static void cp_lexer_handle_pragma
170
  (cp_lexer *);
171
static void cp_lexer_save_tokens
172
  (cp_lexer *);
173
static void cp_lexer_commit_tokens
174
  (cp_lexer *);
175
static void cp_lexer_rollback_tokens
176
  (cp_lexer *);
177
#ifdef ENABLE_CHECKING
178
static void cp_lexer_print_token
179
  (FILE *, cp_token *);
180
static inline bool cp_lexer_debugging_p
181
  (cp_lexer *);
182
static void cp_lexer_start_debugging
183
  (cp_lexer *) ATTRIBUTE_UNUSED;
184
static void cp_lexer_stop_debugging
185
  (cp_lexer *) ATTRIBUTE_UNUSED;
186
#else
187
/* If we define cp_lexer_debug_stream to NULL it will provoke warnings
188
   about passing NULL to functions that require non-NULL arguments
189
   (fputs, fprintf).  It will never be used, so all we need is a value
190
   of the right type that's guaranteed not to be NULL.  */
191
#define cp_lexer_debug_stream stdout
192
#define cp_lexer_print_token(str, tok) (void) 0
193
#define cp_lexer_debugging_p(lexer) 0
194
#endif /* ENABLE_CHECKING */
195
 
196
static cp_token_cache *cp_token_cache_new
197
  (cp_token *, cp_token *);
198
 
199
/* Manifest constants.  */
200
#define CP_LEXER_BUFFER_SIZE 10000
201
#define CP_SAVED_TOKEN_STACK 5
202
 
203
/* A token type for keywords, as opposed to ordinary identifiers.  */
204
#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
205
 
206
/* A token type for template-ids.  If a template-id is processed while
207
   parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
208
   the value of the CPP_TEMPLATE_ID is whatever was returned by
209
   cp_parser_template_id.  */
210
#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
211
 
212
/* A token type for nested-name-specifiers.  If a
213
   nested-name-specifier is processed while parsing tentatively, it is
214
   replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
215
   CPP_NESTED_NAME_SPECIFIER is whatever was returned by
216
   cp_parser_nested_name_specifier_opt.  */
217
#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
218
 
219
/* A token type for tokens that are not tokens at all; these are used
220
   to represent slots in the array where there used to be a token
221
   that has now been deleted.  */
222
#define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
223
 
224
/* The number of token types, including C++-specific ones.  */
225
#define N_CP_TTYPES ((int) (CPP_PURGED + 1))
226
 
227
/* Variables.  */
228
 
229
#ifdef ENABLE_CHECKING
230
/* The stream to which debugging output should be written.  */
231
static FILE *cp_lexer_debug_stream;
232
#endif /* ENABLE_CHECKING */
233
 
234
/* Create a new main C++ lexer, the lexer that gets tokens from the
235
   preprocessor.  */
236
 
237
static cp_lexer *
238
cp_lexer_new_main (void)
239
{
240
  cp_token first_token;
241
  cp_lexer *lexer;
242
  cp_token *pos;
243
  size_t alloc;
244
  size_t space;
245
  cp_token *buffer;
246
 
247
  /* It's possible that lexing the first token will load a PCH file,
248
     which is a GC collection point.  So we have to grab the first
249
     token before allocating any memory.  Pragmas must not be deferred
250
     as -fpch-preprocess can generate a pragma to load the PCH file in
251
     the preprocessed output used by -save-temps.  */
252
  cp_lexer_get_preprocessor_token (NULL, &first_token);
253
 
254
  /* Tell cpplib we want CPP_PRAGMA tokens.  */
255
  cpp_get_options (parse_in)->defer_pragmas = true;
256
 
257
  /* Tell c_lex not to merge string constants.  */
258
  c_lex_return_raw_strings = true;
259
 
260
  c_common_no_more_pch ();
261
 
262
  /* Allocate the memory.  */
263
  lexer = GGC_CNEW (cp_lexer);
264
 
265
#ifdef ENABLE_CHECKING
266
  /* Initially we are not debugging.  */
267
  lexer->debugging_p = false;
268
#endif /* ENABLE_CHECKING */
269
  lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
270
                                   CP_SAVED_TOKEN_STACK);
271
 
272
  /* Create the buffer.  */
273
  alloc = CP_LEXER_BUFFER_SIZE;
274
  buffer = ggc_alloc (alloc * sizeof (cp_token));
275
 
276
  /* Put the first token in the buffer.  */
277
  space = alloc;
278
  pos = buffer;
279
  *pos = first_token;
280
 
281
  /* Get the remaining tokens from the preprocessor.  */
282
  while (pos->type != CPP_EOF)
283
    {
284
      pos++;
285
      if (!--space)
286
        {
287
          space = alloc;
288
          alloc *= 2;
289
          buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
290
          pos = buffer + space;
291
        }
292
      cp_lexer_get_preprocessor_token (lexer, pos);
293
    }
294
  lexer->buffer = buffer;
295
  lexer->buffer_length = alloc - space;
296
  lexer->last_token = pos;
297
  lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
298
 
299
  /* Pragma processing (via cpp_handle_deferred_pragma) may result in
300
     direct calls to c_lex.  Those callers all expect c_lex to do
301
     string constant concatenation.  */
302
  c_lex_return_raw_strings = false;
303
 
304
  /* Subsequent preprocessor diagnostics should use compiler
305
     diagnostic functions to get the compiler source location.  */
306
  cpp_get_options (parse_in)->client_diagnostic = true;
307
  cpp_get_callbacks (parse_in)->error = cp_cpp_error;
308
 
309
  gcc_assert (lexer->next_token->type != CPP_PURGED);
310
  return lexer;
311
}
312
 
313
/* Create a new lexer whose token stream is primed with the tokens in
314
   CACHE.  When these tokens are exhausted, no new tokens will be read.  */
315
 
316
static cp_lexer *
317
cp_lexer_new_from_tokens (cp_token_cache *cache)
318
{
319
  cp_token *first = cache->first;
320
  cp_token *last = cache->last;
321
  cp_lexer *lexer = GGC_CNEW (cp_lexer);
322
 
323
  /* We do not own the buffer.  */
324
  lexer->buffer = NULL;
325
  lexer->buffer_length = 0;
326
  lexer->next_token = first == last ? (cp_token *)&eof_token : first;
327
  lexer->last_token = last;
328
 
329
  lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
330
                                   CP_SAVED_TOKEN_STACK);
331
 
332
#ifdef ENABLE_CHECKING
333
  /* Initially we are not debugging.  */
334
  lexer->debugging_p = false;
335
#endif
336
 
337
  gcc_assert (lexer->next_token->type != CPP_PURGED);
338
  return lexer;
339
}
340
 
341
/* Frees all resources associated with LEXER.  */
342
 
343
static void
344
cp_lexer_destroy (cp_lexer *lexer)
345
{
346
  if (lexer->buffer)
347
    ggc_free (lexer->buffer);
348
  VEC_free (cp_token_position, heap, lexer->saved_tokens);
349
  ggc_free (lexer);
350
}
351
 
352
/* Returns nonzero if debugging information should be output.  */
353
 
354
#ifdef ENABLE_CHECKING
355
 
356
static inline bool
357
cp_lexer_debugging_p (cp_lexer *lexer)
358
{
359
  return lexer->debugging_p;
360
}
361
 
362
#endif /* ENABLE_CHECKING */
363
 
364
static inline cp_token_position
365
cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
366
{
367
  gcc_assert (!previous_p || lexer->next_token != &eof_token);
368
 
369
  return lexer->next_token - previous_p;
370
}
371
 
372
static inline cp_token *
373
cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
374
{
375
  return pos;
376
}
377
 
378
/* nonzero if we are presently saving tokens.  */
379
 
380
static inline int
381
cp_lexer_saving_tokens (const cp_lexer* lexer)
382
{
383
  return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
384
}
385
 
386
/* Store the next token from the preprocessor in *TOKEN.  Return true
387
   if we reach EOF.  */
388
 
389
static void
390
cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
391
                                 cp_token *token)
392
{
393
  static int is_extern_c = 0;
394
 
395
   /* Get a new token from the preprocessor.  */
396
  token->type
397
    = c_lex_with_flags (&token->value, &token->location, &token->flags);
398
  token->in_system_header = in_system_header;
399
 
400
  /* On some systems, some header files are surrounded by an
401
     implicit extern "C" block.  Set a flag in the token if it
402
     comes from such a header.  */
403
  is_extern_c += pending_lang_change;
404
  pending_lang_change = 0;
405
  token->implicit_extern_c = is_extern_c > 0;
406
 
407
  /* Check to see if this token is a keyword.  */
408
  if (token->type == CPP_NAME)
409
    {
410
      if (C_IS_RESERVED_WORD (token->value))
411
        {
412
          /* Mark this token as a keyword.  */
413
          token->type = CPP_KEYWORD;
414
          /* Record which keyword.  */
415
          token->keyword = C_RID_CODE (token->value);
416
          /* Update the value.  Some keywords are mapped to particular
417
             entities, rather than simply having the value of the
418
             corresponding IDENTIFIER_NODE.  For example, `__const' is
419
             mapped to `const'.  */
420
          token->value = ridpointers[token->keyword];
421
        }
422
      else
423
        {
424
          token->ambiguous_p = false;
425
          token->keyword = RID_MAX;
426
        }
427
    }
428
  /* Handle Objective-C++ keywords.  */
429
  else if (token->type == CPP_AT_NAME)
430
    {
431
      token->type = CPP_KEYWORD;
432
      switch (C_RID_CODE (token->value))
433
        {
434
        /* Map 'class' to '@class', 'private' to '@private', etc.  */
435
        case RID_CLASS: token->keyword = RID_AT_CLASS; break;
436
        case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
437
        case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
438
        case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
439
        case RID_THROW: token->keyword = RID_AT_THROW; break;
440
        case RID_TRY: token->keyword = RID_AT_TRY; break;
441
        case RID_CATCH: token->keyword = RID_AT_CATCH; break;
442
        default: token->keyword = C_RID_CODE (token->value);
443
        }
444
    }
445
  else
446
    token->keyword = RID_MAX;
447
}
448
 
449
/* Update the globals input_location and in_system_header from TOKEN.  */
450
static inline void
451
cp_lexer_set_source_position_from_token (cp_token *token)
452
{
453
  if (token->type != CPP_EOF)
454
    {
455
      input_location = token->location;
456
      in_system_header = token->in_system_header;
457
    }
458
}
459
 
460
/* Return a pointer to the next token in the token stream, but do not
461
   consume it.  */
462
 
463
static inline cp_token *
464
cp_lexer_peek_token (cp_lexer *lexer)
465
{
466
  if (cp_lexer_debugging_p (lexer))
467
    {
468
      fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
469
      cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
470
      putc ('\n', cp_lexer_debug_stream);
471
    }
472
  return lexer->next_token;
473
}
474
 
475
/* Return true if the next token has the indicated TYPE.  */
476
 
477
static inline bool
478
cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
479
{
480
  return cp_lexer_peek_token (lexer)->type == type;
481
}
482
 
483
/* Return true if the next token does not have the indicated TYPE.  */
484
 
485
static inline bool
486
cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
487
{
488
  return !cp_lexer_next_token_is (lexer, type);
489
}
490
 
491
/* Return true if the next token is the indicated KEYWORD.  */
492
 
493
static inline bool
494
cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
495
{
496
  cp_token *token;
497
 
498
  /* Peek at the next token.  */
499
  token = cp_lexer_peek_token (lexer);
500
  /* Check to see if it is the indicated keyword.  */
501
  return token->keyword == keyword;
502
}
503
 
504
/* Return a pointer to the Nth token in the token stream.  If N is 1,
505
   then this is precisely equivalent to cp_lexer_peek_token (except
506
   that it is not inline).  One would like to disallow that case, but
507
   there is one case (cp_parser_nth_token_starts_template_id) where
508
   the caller passes a variable for N and it might be 1.  */
509
 
510
static cp_token *
511
cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
512
{
513
  cp_token *token;
514
 
515
  /* N is 1-based, not zero-based.  */
516
  gcc_assert (n > 0);
517
 
518
  if (cp_lexer_debugging_p (lexer))
519
    fprintf (cp_lexer_debug_stream,
520
             "cp_lexer: peeking ahead %ld at token: ", (long)n);
521
 
522
  --n;
523
  token = lexer->next_token;
524
  gcc_assert (!n || token != &eof_token);
525
  while (n != 0)
526
    {
527
      ++token;
528
      if (token == lexer->last_token)
529
        {
530
          token = (cp_token *)&eof_token;
531
          break;
532
        }
533
 
534
      if (token->type != CPP_PURGED)
535
        --n;
536
    }
537
 
538
  if (cp_lexer_debugging_p (lexer))
539
    {
540
      cp_lexer_print_token (cp_lexer_debug_stream, token);
541
      putc ('\n', cp_lexer_debug_stream);
542
    }
543
 
544
  return token;
545
}
546
 
547
/* Return the next token, and advance the lexer's next_token pointer
548
   to point to the next non-purged token.  */
549
 
550
static cp_token *
551
cp_lexer_consume_token (cp_lexer* lexer)
552
{
553
  cp_token *token = lexer->next_token;
554
 
555
  gcc_assert (token != &eof_token);
556
 
557
  do
558
    {
559
      lexer->next_token++;
560
      if (lexer->next_token == lexer->last_token)
561
        {
562
          lexer->next_token = (cp_token *)&eof_token;
563
          break;
564
        }
565
 
566
    }
567
  while (lexer->next_token->type == CPP_PURGED);
568
 
569
  cp_lexer_set_source_position_from_token (token);
570
 
571
  /* Provide debugging output.  */
572
  if (cp_lexer_debugging_p (lexer))
573
    {
574
      fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
575
      cp_lexer_print_token (cp_lexer_debug_stream, token);
576
      putc ('\n', cp_lexer_debug_stream);
577
    }
578
 
579
  return token;
580
}
581
 
582
/* Permanently remove the next token from the token stream, and
583
   advance the next_token pointer to refer to the next non-purged
584
   token.  */
585
 
586
static void
587
cp_lexer_purge_token (cp_lexer *lexer)
588
{
589
  cp_token *tok = lexer->next_token;
590
 
591
  gcc_assert (tok != &eof_token);
592
  tok->type = CPP_PURGED;
593
  tok->location = UNKNOWN_LOCATION;
594
  tok->value = NULL_TREE;
595
  tok->keyword = RID_MAX;
596
 
597
  do
598
    {
599
      tok++;
600
      if (tok == lexer->last_token)
601
        {
602
          tok = (cp_token *)&eof_token;
603
          break;
604
        }
605
    }
606
  while (tok->type == CPP_PURGED);
607
  lexer->next_token = tok;
608
}
609
 
610
/* Permanently remove all tokens after TOK, up to, but not
611
   including, the token that will be returned next by
612
   cp_lexer_peek_token.  */
613
 
614
static void
615
cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
616
{
617
  cp_token *peek = lexer->next_token;
618
 
619
  if (peek == &eof_token)
620
    peek = lexer->last_token;
621
 
622
  gcc_assert (tok < peek);
623
 
624
  for ( tok += 1; tok != peek; tok += 1)
625
    {
626
      tok->type = CPP_PURGED;
627
      tok->location = UNKNOWN_LOCATION;
628
      tok->value = NULL_TREE;
629
      tok->keyword = RID_MAX;
630
    }
631
}
632
 
633
/* Consume and handle a pragma token.  */
634
static void
635
cp_lexer_handle_pragma (cp_lexer *lexer)
636
{
637
  cpp_string s;
638
  cp_token *token = cp_lexer_consume_token (lexer);
639
  gcc_assert (token->type == CPP_PRAGMA);
640
  gcc_assert (token->value);
641
 
642
  s.len = TREE_STRING_LENGTH (token->value);
643
  s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
644
 
645
  cpp_handle_deferred_pragma (parse_in, &s);
646
 
647
  /* Clearing token->value here means that we will get an ICE if we
648
     try to process this #pragma again (which should be impossible).  */
649
  token->value = NULL;
650
}
651
 
652
/* Begin saving tokens.  All tokens consumed after this point will be
653
   preserved.  */
654
 
655
static void
656
cp_lexer_save_tokens (cp_lexer* lexer)
657
{
658
  /* Provide debugging output.  */
659
  if (cp_lexer_debugging_p (lexer))
660
    fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
661
 
662
  VEC_safe_push (cp_token_position, heap,
663
                 lexer->saved_tokens, lexer->next_token);
664
}
665
 
666
/* Commit to the portion of the token stream most recently saved.  */
667
 
668
static void
669
cp_lexer_commit_tokens (cp_lexer* lexer)
670
{
671
  /* Provide debugging output.  */
672
  if (cp_lexer_debugging_p (lexer))
673
    fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
674
 
675
  VEC_pop (cp_token_position, lexer->saved_tokens);
676
}
677
 
678
/* Return all tokens saved since the last call to cp_lexer_save_tokens
679
   to the token stream.  Stop saving tokens.  */
680
 
681
static void
682
cp_lexer_rollback_tokens (cp_lexer* lexer)
683
{
684
  /* Provide debugging output.  */
685
  if (cp_lexer_debugging_p (lexer))
686
    fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
687
 
688
  lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
689
}
690
 
691
/* Print a representation of the TOKEN on the STREAM.  */
692
 
693
#ifdef ENABLE_CHECKING
694
 
695
static void
696
cp_lexer_print_token (FILE * stream, cp_token *token)
697
{
698
  /* We don't use cpp_type2name here because the parser defines
699
     a few tokens of its own.  */
700
  static const char *const token_names[] = {
701
    /* cpplib-defined token types */
702
#define OP(e, s) #e,
703
#define TK(e, s) #e,
704
    TTYPE_TABLE
705
#undef OP
706
#undef TK
707
    /* C++ parser token types - see "Manifest constants", above.  */
708
    "KEYWORD",
709
    "TEMPLATE_ID",
710
    "NESTED_NAME_SPECIFIER",
711
    "PURGED"
712
  };
713
 
714
  /* If we have a name for the token, print it out.  Otherwise, we
715
     simply give the numeric code.  */
716
  gcc_assert (token->type < ARRAY_SIZE(token_names));
717
  fputs (token_names[token->type], stream);
718
 
719
  /* For some tokens, print the associated data.  */
720
  switch (token->type)
721
    {
722
    case CPP_KEYWORD:
723
      /* Some keywords have a value that is not an IDENTIFIER_NODE.
724
         For example, `struct' is mapped to an INTEGER_CST.  */
725
      if (TREE_CODE (token->value) != IDENTIFIER_NODE)
726
        break;
727
      /* else fall through */
728
    case CPP_NAME:
729
      fputs (IDENTIFIER_POINTER (token->value), stream);
730
      break;
731
 
732
    case CPP_STRING:
733
    case CPP_WSTRING:
734
    case CPP_PRAGMA:
735
      fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
736
      break;
737
 
738
    default:
739
      break;
740
    }
741
}
742
 
743
/* Start emitting debugging information.  */
744
 
745
static void
746
cp_lexer_start_debugging (cp_lexer* lexer)
747
{
748
  lexer->debugging_p = true;
749
}
750
 
751
/* Stop emitting debugging information.  */
752
 
753
static void
754
cp_lexer_stop_debugging (cp_lexer* lexer)
755
{
756
  lexer->debugging_p = false;
757
}
758
 
759
#endif /* ENABLE_CHECKING */
760
 
761
/* Create a new cp_token_cache, representing a range of tokens.  */
762
 
763
static cp_token_cache *
764
cp_token_cache_new (cp_token *first, cp_token *last)
765
{
766
  cp_token_cache *cache = GGC_NEW (cp_token_cache);
767
  cache->first = first;
768
  cache->last = last;
769
  return cache;
770
}
771
 
772
 
773
/* Decl-specifiers.  */
774
 
775
static void clear_decl_specs
776
  (cp_decl_specifier_seq *);
777
 
778
/* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
779
 
780
static void
781
clear_decl_specs (cp_decl_specifier_seq *decl_specs)
782
{
783
  memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
784
}
785
 
786
/* Declarators.  */
787
 
788
/* Nothing other than the parser should be creating declarators;
789
   declarators are a semi-syntactic representation of C++ entities.
790
   Other parts of the front end that need to create entities (like
791
   VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
792
 
793
static cp_declarator *make_call_declarator
794
  (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
795
static cp_declarator *make_array_declarator
796
  (cp_declarator *, tree);
797
static cp_declarator *make_pointer_declarator
798
  (cp_cv_quals, cp_declarator *);
799
static cp_declarator *make_reference_declarator
800
  (cp_cv_quals, cp_declarator *);
801
static cp_parameter_declarator *make_parameter_declarator
802
  (cp_decl_specifier_seq *, cp_declarator *, tree);
803
static cp_declarator *make_ptrmem_declarator
804
  (cp_cv_quals, tree, cp_declarator *);
805
 
806
cp_declarator *cp_error_declarator;
807
 
808
/* The obstack on which declarators and related data structures are
809
   allocated.  */
810
static struct obstack declarator_obstack;
811
 
812
/* Alloc BYTES from the declarator memory pool.  */
813
 
814
static inline void *
815
alloc_declarator (size_t bytes)
816
{
817
  return obstack_alloc (&declarator_obstack, bytes);
818
}
819
 
820
/* Allocate a declarator of the indicated KIND.  Clear fields that are
821
   common to all declarators.  */
822
 
823
static cp_declarator *
824
make_declarator (cp_declarator_kind kind)
825
{
826
  cp_declarator *declarator;
827
 
828
  declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
829
  declarator->kind = kind;
830
  declarator->attributes = NULL_TREE;
831
  declarator->declarator = NULL;
832
 
833
  return declarator;
834
}
835
 
836
/* Make a declarator for a generalized identifier.  If
837
   QUALIFYING_SCOPE is non-NULL, the identifier is
838
   QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
839
   UNQUALIFIED_NAME.  SFK indicates the kind of special function this
840
   is, if any.   */
841
 
842
static cp_declarator *
843
make_id_declarator (tree qualifying_scope, tree unqualified_name,
844
                    special_function_kind sfk)
845
{
846
  cp_declarator *declarator;
847
 
848
  /* It is valid to write:
849
 
850
       class C { void f(); };
851
       typedef C D;
852
       void D::f();
853
 
854
     The standard is not clear about whether `typedef const C D' is
855
     legal; as of 2002-09-15 the committee is considering that
856
     question.  EDG 3.0 allows that syntax.  Therefore, we do as
857
     well.  */
858
  if (qualifying_scope && TYPE_P (qualifying_scope))
859
    qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
860
 
861
  gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
862
              || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
863
              || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
864
 
865
  declarator = make_declarator (cdk_id);
866
  declarator->u.id.qualifying_scope = qualifying_scope;
867
  declarator->u.id.unqualified_name = unqualified_name;
868
  declarator->u.id.sfk = sfk;
869
 
870
  return declarator;
871
}
872
 
873
/* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
874
   of modifiers such as const or volatile to apply to the pointer
875
   type, represented as identifiers.  */
876
 
877
cp_declarator *
878
make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
879
{
880
  cp_declarator *declarator;
881
 
882
  declarator = make_declarator (cdk_pointer);
883
  declarator->declarator = target;
884
  declarator->u.pointer.qualifiers = cv_qualifiers;
885
  declarator->u.pointer.class_type = NULL_TREE;
886
 
887
  return declarator;
888
}
889
 
890
/* Like make_pointer_declarator -- but for references.  */
891
 
892
cp_declarator *
893
make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
894
{
895
  cp_declarator *declarator;
896
 
897
  declarator = make_declarator (cdk_reference);
898
  declarator->declarator = target;
899
  declarator->u.pointer.qualifiers = cv_qualifiers;
900
  declarator->u.pointer.class_type = NULL_TREE;
901
 
902
  return declarator;
903
}
904
 
905
/* Like make_pointer_declarator -- but for a pointer to a non-static
906
   member of CLASS_TYPE.  */
907
 
908
cp_declarator *
909
make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
910
                        cp_declarator *pointee)
911
{
912
  cp_declarator *declarator;
913
 
914
  declarator = make_declarator (cdk_ptrmem);
915
  declarator->declarator = pointee;
916
  declarator->u.pointer.qualifiers = cv_qualifiers;
917
  declarator->u.pointer.class_type = class_type;
918
 
919
  return declarator;
920
}
921
 
922
/* Make a declarator for the function given by TARGET, with the
923
   indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
924
   "const"-qualified member function.  The EXCEPTION_SPECIFICATION
925
   indicates what exceptions can be thrown.  */
926
 
927
cp_declarator *
928
make_call_declarator (cp_declarator *target,
929
                      cp_parameter_declarator *parms,
930
                      cp_cv_quals cv_qualifiers,
931
                      tree exception_specification)
932
{
933
  cp_declarator *declarator;
934
 
935
  declarator = make_declarator (cdk_function);
936
  declarator->declarator = target;
937
  declarator->u.function.parameters = parms;
938
  declarator->u.function.qualifiers = cv_qualifiers;
939
  declarator->u.function.exception_specification = exception_specification;
940
 
941
  return declarator;
942
}
943
 
944
/* Make a declarator for an array of BOUNDS elements, each of which is
945
   defined by ELEMENT.  */
946
 
947
cp_declarator *
948
make_array_declarator (cp_declarator *element, tree bounds)
949
{
950
  cp_declarator *declarator;
951
 
952
  declarator = make_declarator (cdk_array);
953
  declarator->declarator = element;
954
  declarator->u.array.bounds = bounds;
955
 
956
  return declarator;
957
}
958
 
959
cp_parameter_declarator *no_parameters;
960
 
961
/* Create a parameter declarator with the indicated DECL_SPECIFIERS,
962
   DECLARATOR and DEFAULT_ARGUMENT.  */
963
 
964
cp_parameter_declarator *
965
make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
966
                           cp_declarator *declarator,
967
                           tree default_argument)
968
{
969
  cp_parameter_declarator *parameter;
970
 
971
  parameter = ((cp_parameter_declarator *)
972
               alloc_declarator (sizeof (cp_parameter_declarator)));
973
  parameter->next = NULL;
974
  if (decl_specifiers)
975
    parameter->decl_specifiers = *decl_specifiers;
976
  else
977
    clear_decl_specs (&parameter->decl_specifiers);
978
  parameter->declarator = declarator;
979
  parameter->default_argument = default_argument;
980
  parameter->ellipsis_p = false;
981
 
982
  return parameter;
983
}
984
 
985
/* The parser.  */
986
 
987
/* Overview
988
   --------
989
 
990
   A cp_parser parses the token stream as specified by the C++
991
   grammar.  Its job is purely parsing, not semantic analysis.  For
992
   example, the parser breaks the token stream into declarators,
993
   expressions, statements, and other similar syntactic constructs.
994
   It does not check that the types of the expressions on either side
995
   of an assignment-statement are compatible, or that a function is
996
   not declared with a parameter of type `void'.
997
 
998
   The parser invokes routines elsewhere in the compiler to perform
999
   semantic analysis and to build up the abstract syntax tree for the
1000
   code processed.
1001
 
1002
   The parser (and the template instantiation code, which is, in a
1003
   way, a close relative of parsing) are the only parts of the
1004
   compiler that should be calling push_scope and pop_scope, or
1005
   related functions.  The parser (and template instantiation code)
1006
   keeps track of what scope is presently active; everything else
1007
   should simply honor that.  (The code that generates static
1008
   initializers may also need to set the scope, in order to check
1009
   access control correctly when emitting the initializers.)
1010
 
1011
   Methodology
1012
   -----------
1013
 
1014
   The parser is of the standard recursive-descent variety.  Upcoming
1015
   tokens in the token stream are examined in order to determine which
1016
   production to use when parsing a non-terminal.  Some C++ constructs
1017
   require arbitrary look ahead to disambiguate.  For example, it is
1018
   impossible, in the general case, to tell whether a statement is an
1019
   expression or declaration without scanning the entire statement.
1020
   Therefore, the parser is capable of "parsing tentatively."  When the
1021
   parser is not sure what construct comes next, it enters this mode.
1022
   Then, while we attempt to parse the construct, the parser queues up
1023
   error messages, rather than issuing them immediately, and saves the
1024
   tokens it consumes.  If the construct is parsed successfully, the
1025
   parser "commits", i.e., it issues any queued error messages and
1026
   the tokens that were being preserved are permanently discarded.
1027
   If, however, the construct is not parsed successfully, the parser
1028
   rolls back its state completely so that it can resume parsing using
1029
   a different alternative.
1030
 
1031
   Future Improvements
1032
   -------------------
1033
 
1034
   The performance of the parser could probably be improved substantially.
1035
   We could often eliminate the need to parse tentatively by looking ahead
1036
   a little bit.  In some places, this approach might not entirely eliminate
1037
   the need to parse tentatively, but it might still speed up the average
1038
   case.  */
1039
 
1040
/* Flags that are passed to some parsing functions.  These values can
1041
   be bitwise-ored together.  */
1042
 
1043
typedef enum cp_parser_flags
1044
{
1045
  /* No flags.  */
1046
  CP_PARSER_FLAGS_NONE = 0x0,
1047
  /* The construct is optional.  If it is not present, then no error
1048
     should be issued.  */
1049
  CP_PARSER_FLAGS_OPTIONAL = 0x1,
1050
  /* When parsing a type-specifier, do not allow user-defined types.  */
1051
  CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1052
} cp_parser_flags;
1053
 
1054
/* The different kinds of declarators we want to parse.  */
1055
 
1056
typedef enum cp_parser_declarator_kind
1057
{
1058
  /* We want an abstract declarator.  */
1059
  CP_PARSER_DECLARATOR_ABSTRACT,
1060
  /* We want a named declarator.  */
1061
  CP_PARSER_DECLARATOR_NAMED,
1062
  /* We don't mind, but the name must be an unqualified-id.  */
1063
  CP_PARSER_DECLARATOR_EITHER
1064
} cp_parser_declarator_kind;
1065
 
1066
/* The precedence values used to parse binary expressions.  The minimum value
1067
   of PREC must be 1, because zero is reserved to quickly discriminate
1068
   binary operators from other tokens.  */
1069
 
1070
enum cp_parser_prec
1071
{
1072
  PREC_NOT_OPERATOR,
1073
  PREC_LOGICAL_OR_EXPRESSION,
1074
  PREC_LOGICAL_AND_EXPRESSION,
1075
  PREC_INCLUSIVE_OR_EXPRESSION,
1076
  PREC_EXCLUSIVE_OR_EXPRESSION,
1077
  PREC_AND_EXPRESSION,
1078
  PREC_EQUALITY_EXPRESSION,
1079
  PREC_RELATIONAL_EXPRESSION,
1080
  PREC_SHIFT_EXPRESSION,
1081
  PREC_ADDITIVE_EXPRESSION,
1082
  PREC_MULTIPLICATIVE_EXPRESSION,
1083
  PREC_PM_EXPRESSION,
1084
  NUM_PREC_VALUES = PREC_PM_EXPRESSION
1085
};
1086
 
1087
/* A mapping from a token type to a corresponding tree node type, with a
1088
   precedence value.  */
1089
 
1090
typedef struct cp_parser_binary_operations_map_node
1091
{
1092
  /* The token type.  */
1093
  enum cpp_ttype token_type;
1094
  /* The corresponding tree code.  */
1095
  enum tree_code tree_type;
1096
  /* The precedence of this operator.  */
1097
  enum cp_parser_prec prec;
1098
} cp_parser_binary_operations_map_node;
1099
 
1100
/* The status of a tentative parse.  */
1101
 
1102
typedef enum cp_parser_status_kind
1103
{
1104
  /* No errors have occurred.  */
1105
  CP_PARSER_STATUS_KIND_NO_ERROR,
1106
  /* An error has occurred.  */
1107
  CP_PARSER_STATUS_KIND_ERROR,
1108
  /* We are committed to this tentative parse, whether or not an error
1109
     has occurred.  */
1110
  CP_PARSER_STATUS_KIND_COMMITTED
1111
} cp_parser_status_kind;
1112
 
1113
typedef struct cp_parser_expression_stack_entry
1114
{
1115
  tree lhs;
1116
  enum tree_code tree_type;
1117
  int prec;
1118
} cp_parser_expression_stack_entry;
1119
 
1120
/* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1121
   entries because precedence levels on the stack are monotonically
1122
   increasing.  */
1123
typedef struct cp_parser_expression_stack_entry
1124
  cp_parser_expression_stack[NUM_PREC_VALUES];
1125
 
1126
/* Context that is saved and restored when parsing tentatively.  */
1127
typedef struct cp_parser_context GTY (())
1128
{
1129
  /* If this is a tentative parsing context, the status of the
1130
     tentative parse.  */
1131
  enum cp_parser_status_kind status;
1132
  /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1133
     that are looked up in this context must be looked up both in the
1134
     scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1135
     the context of the containing expression.  */
1136
  tree object_type;
1137
 
1138
  /* The next parsing context in the stack.  */
1139
  struct cp_parser_context *next;
1140
} cp_parser_context;
1141
 
1142
/* Prototypes.  */
1143
 
1144
/* Constructors and destructors.  */
1145
 
1146
static cp_parser_context *cp_parser_context_new
1147
  (cp_parser_context *);
1148
 
1149
/* Class variables.  */
1150
 
1151
static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1152
 
1153
/* The operator-precedence table used by cp_parser_binary_expression.
1154
   Transformed into an associative array (binops_by_token) by
1155
   cp_parser_new.  */
1156
 
1157
static const cp_parser_binary_operations_map_node binops[] = {
1158
  { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1159
  { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1160
 
1161
  { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1162
  { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1163
  { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1164
 
1165
  { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1166
  { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1167
 
1168
  { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1169
  { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1170
 
1171
  { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1172
  { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1173
  { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1174
  { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1175
  { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1176
  { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1177
 
1178
  { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1179
  { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1180
 
1181
  { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1182
 
1183
  { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1184
 
1185
  { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1186
 
1187
  { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1188
 
1189
  { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1190
};
1191
 
1192
/* The same as binops, but initialized by cp_parser_new so that
1193
   binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1194
   for speed.  */
1195
static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1196
 
1197
/* Constructors and destructors.  */
1198
 
1199
/* Construct a new context.  The context below this one on the stack
1200
   is given by NEXT.  */
1201
 
1202
static cp_parser_context *
1203
cp_parser_context_new (cp_parser_context* next)
1204
{
1205
  cp_parser_context *context;
1206
 
1207
  /* Allocate the storage.  */
1208
  if (cp_parser_context_free_list != NULL)
1209
    {
1210
      /* Pull the first entry from the free list.  */
1211
      context = cp_parser_context_free_list;
1212
      cp_parser_context_free_list = context->next;
1213
      memset (context, 0, sizeof (*context));
1214
    }
1215
  else
1216
    context = GGC_CNEW (cp_parser_context);
1217
 
1218
  /* No errors have occurred yet in this context.  */
1219
  context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1220
  /* If this is not the bottomost context, copy information that we
1221
     need from the previous context.  */
1222
  if (next)
1223
    {
1224
      /* If, in the NEXT context, we are parsing an `x->' or `x.'
1225
         expression, then we are parsing one in this context, too.  */
1226
      context->object_type = next->object_type;
1227
      /* Thread the stack.  */
1228
      context->next = next;
1229
    }
1230
 
1231
  return context;
1232
}
1233
 
1234
/* The cp_parser structure represents the C++ parser.  */
1235
 
1236
typedef struct cp_parser GTY(())
1237
{
1238
  /* The lexer from which we are obtaining tokens.  */
1239
  cp_lexer *lexer;
1240
 
1241
  /* The scope in which names should be looked up.  If NULL_TREE, then
1242
     we look up names in the scope that is currently open in the
1243
     source program.  If non-NULL, this is either a TYPE or
1244
     NAMESPACE_DECL for the scope in which we should look.  It can
1245
     also be ERROR_MARK, when we've parsed a bogus scope.
1246
 
1247
     This value is not cleared automatically after a name is looked
1248
     up, so we must be careful to clear it before starting a new look
1249
     up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1250
     will look up `Z' in the scope of `X', rather than the current
1251
     scope.)  Unfortunately, it is difficult to tell when name lookup
1252
     is complete, because we sometimes peek at a token, look it up,
1253
     and then decide not to consume it.   */
1254
  tree scope;
1255
 
1256
  /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1257
     last lookup took place.  OBJECT_SCOPE is used if an expression
1258
     like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1259
     respectively.  QUALIFYING_SCOPE is used for an expression of the
1260
     form "X::Y"; it refers to X.  */
1261
  tree object_scope;
1262
  tree qualifying_scope;
1263
 
1264
  /* A stack of parsing contexts.  All but the bottom entry on the
1265
     stack will be tentative contexts.
1266
 
1267
     We parse tentatively in order to determine which construct is in
1268
     use in some situations.  For example, in order to determine
1269
     whether a statement is an expression-statement or a
1270
     declaration-statement we parse it tentatively as a
1271
     declaration-statement.  If that fails, we then reparse the same
1272
     token stream as an expression-statement.  */
1273
  cp_parser_context *context;
1274
 
1275
  /* True if we are parsing GNU C++.  If this flag is not set, then
1276
     GNU extensions are not recognized.  */
1277
  bool allow_gnu_extensions_p;
1278
 
1279
  /* TRUE if the `>' token should be interpreted as the greater-than
1280
     operator.  FALSE if it is the end of a template-id or
1281
     template-parameter-list.  */
1282
  bool greater_than_is_operator_p;
1283
 
1284
  /* TRUE if default arguments are allowed within a parameter list
1285
     that starts at this point. FALSE if only a gnu extension makes
1286
     them permissible.  */
1287
  bool default_arg_ok_p;
1288
 
1289
  /* TRUE if we are parsing an integral constant-expression.  See
1290
     [expr.const] for a precise definition.  */
1291
  bool integral_constant_expression_p;
1292
 
1293
  /* TRUE if we are parsing an integral constant-expression -- but a
1294
     non-constant expression should be permitted as well.  This flag
1295
     is used when parsing an array bound so that GNU variable-length
1296
     arrays are tolerated.  */
1297
  bool allow_non_integral_constant_expression_p;
1298
 
1299
  /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1300
     been seen that makes the expression non-constant.  */
1301
  bool non_integral_constant_expression_p;
1302
 
1303
  /* TRUE if local variable names and `this' are forbidden in the
1304
     current context.  */
1305
  bool local_variables_forbidden_p;
1306
 
1307
  /* TRUE if the declaration we are parsing is part of a
1308
     linkage-specification of the form `extern string-literal
1309
     declaration'.  */
1310
  bool in_unbraced_linkage_specification_p;
1311
 
1312
  /* TRUE if we are presently parsing a declarator, after the
1313
     direct-declarator.  */
1314
  bool in_declarator_p;
1315
 
1316
  /* TRUE if we are presently parsing a template-argument-list.  */
1317
  bool in_template_argument_list_p;
1318
 
1319
  /* TRUE if we are presently parsing the body of an
1320
     iteration-statement.  */
1321
  bool in_iteration_statement_p;
1322
 
1323
  /* TRUE if we are presently parsing the body of a switch
1324
     statement.  */
1325
  bool in_switch_statement_p;
1326
 
1327
  /* TRUE if we are parsing a type-id in an expression context.  In
1328
     such a situation, both "type (expr)" and "type (type)" are valid
1329
     alternatives.  */
1330
  bool in_type_id_in_expr_p;
1331
 
1332
  /* TRUE if we are currently in a header file where declarations are
1333
     implicitly extern "C".  */
1334
  bool implicit_extern_c;
1335
 
1336
  /* TRUE if strings in expressions should be translated to the execution
1337
     character set.  */
1338
  bool translate_strings_p;
1339
 
1340
  /* If non-NULL, then we are parsing a construct where new type
1341
     definitions are not permitted.  The string stored here will be
1342
     issued as an error message if a type is defined.  */
1343
  const char *type_definition_forbidden_message;
1344
 
1345
  /* A list of lists. The outer list is a stack, used for member
1346
     functions of local classes. At each level there are two sub-list,
1347
     one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1348
     sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1349
     TREE_VALUE's. The functions are chained in reverse declaration
1350
     order.
1351
 
1352
     The TREE_PURPOSE sublist contains those functions with default
1353
     arguments that need post processing, and the TREE_VALUE sublist
1354
     contains those functions with definitions that need post
1355
     processing.
1356
 
1357
     These lists can only be processed once the outermost class being
1358
     defined is complete.  */
1359
  tree unparsed_functions_queues;
1360
 
1361
  /* The number of classes whose definitions are currently in
1362
     progress.  */
1363
  unsigned num_classes_being_defined;
1364
 
1365
  /* The number of template parameter lists that apply directly to the
1366
     current declaration.  */
1367
  unsigned num_template_parameter_lists;
1368
} cp_parser;
1369
 
1370
/* The type of a function that parses some kind of expression.  */
1371
typedef tree (*cp_parser_expression_fn) (cp_parser *);
1372
 
1373
/* Prototypes.  */
1374
 
1375
/* Constructors and destructors.  */
1376
 
1377
static cp_parser *cp_parser_new
1378
  (void);
1379
 
1380
/* Routines to parse various constructs.
1381
 
1382
   Those that return `tree' will return the error_mark_node (rather
1383
   than NULL_TREE) if a parse error occurs, unless otherwise noted.
1384
   Sometimes, they will return an ordinary node if error-recovery was
1385
   attempted, even though a parse error occurred.  So, to check
1386
   whether or not a parse error occurred, you should always use
1387
   cp_parser_error_occurred.  If the construct is optional (indicated
1388
   either by an `_opt' in the name of the function that does the
1389
   parsing or via a FLAGS parameter), then NULL_TREE is returned if
1390
   the construct is not present.  */
1391
 
1392
/* Lexical conventions [gram.lex]  */
1393
 
1394
static tree cp_parser_identifier
1395
  (cp_parser *);
1396
static tree cp_parser_string_literal
1397
  (cp_parser *, bool, bool);
1398
 
1399
/* Basic concepts [gram.basic]  */
1400
 
1401
static bool cp_parser_translation_unit
1402
  (cp_parser *);
1403
 
1404
/* Expressions [gram.expr]  */
1405
 
1406
static tree cp_parser_primary_expression
1407
  (cp_parser *, bool, bool, bool, cp_id_kind *);
1408
static tree cp_parser_id_expression
1409
  (cp_parser *, bool, bool, bool *, bool);
1410
static tree cp_parser_unqualified_id
1411
  (cp_parser *, bool, bool, bool);
1412
static tree cp_parser_nested_name_specifier_opt
1413
  (cp_parser *, bool, bool, bool, bool);
1414
static tree cp_parser_nested_name_specifier
1415
  (cp_parser *, bool, bool, bool, bool);
1416
static tree cp_parser_class_or_namespace_name
1417
  (cp_parser *, bool, bool, bool, bool, bool);
1418
static tree cp_parser_postfix_expression
1419
  (cp_parser *, bool, bool);
1420
static tree cp_parser_postfix_open_square_expression
1421
  (cp_parser *, tree, bool);
1422
static tree cp_parser_postfix_dot_deref_expression
1423
  (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1424
static tree cp_parser_parenthesized_expression_list
1425
  (cp_parser *, bool, bool, bool *);
1426
static void cp_parser_pseudo_destructor_name
1427
  (cp_parser *, tree *, tree *);
1428
static tree cp_parser_unary_expression
1429
  (cp_parser *, bool, bool);
1430
static enum tree_code cp_parser_unary_operator
1431
  (cp_token *);
1432
static tree cp_parser_new_expression
1433
  (cp_parser *);
1434
static tree cp_parser_new_placement
1435
  (cp_parser *);
1436
static tree cp_parser_new_type_id
1437
  (cp_parser *, tree *);
1438
static cp_declarator *cp_parser_new_declarator_opt
1439
  (cp_parser *);
1440
static cp_declarator *cp_parser_direct_new_declarator
1441
  (cp_parser *);
1442
static tree cp_parser_new_initializer
1443
  (cp_parser *);
1444
static tree cp_parser_delete_expression
1445
  (cp_parser *);
1446
static tree cp_parser_cast_expression
1447
  (cp_parser *, bool, bool);
1448
static tree cp_parser_binary_expression
1449
  (cp_parser *, bool);
1450
static tree cp_parser_question_colon_clause
1451
  (cp_parser *, tree);
1452
static tree cp_parser_assignment_expression
1453
  (cp_parser *, bool);
1454
static enum tree_code cp_parser_assignment_operator_opt
1455
  (cp_parser *);
1456
static tree cp_parser_expression
1457
  (cp_parser *, bool);
1458
static tree cp_parser_constant_expression
1459
  (cp_parser *, bool, bool *);
1460
static tree cp_parser_builtin_offsetof
1461
  (cp_parser *);
1462
 
1463
/* Statements [gram.stmt.stmt]  */
1464
 
1465
static void cp_parser_statement
1466
  (cp_parser *, tree);
1467
static tree cp_parser_labeled_statement
1468
  (cp_parser *, tree);
1469
static tree cp_parser_expression_statement
1470
  (cp_parser *, tree);
1471
static tree cp_parser_compound_statement
1472
  (cp_parser *, tree, bool);
1473
static void cp_parser_statement_seq_opt
1474
  (cp_parser *, tree);
1475
static tree cp_parser_selection_statement
1476
  (cp_parser *);
1477
static tree cp_parser_condition
1478
  (cp_parser *);
1479
static tree cp_parser_iteration_statement
1480
  (cp_parser *);
1481
static void cp_parser_for_init_statement
1482
  (cp_parser *);
1483
static tree cp_parser_jump_statement
1484
  (cp_parser *);
1485
static void cp_parser_declaration_statement
1486
  (cp_parser *);
1487
 
1488
static tree cp_parser_implicitly_scoped_statement
1489
  (cp_parser *);
1490
static void cp_parser_already_scoped_statement
1491
  (cp_parser *);
1492
 
1493
/* Declarations [gram.dcl.dcl] */
1494
 
1495
static void cp_parser_declaration_seq_opt
1496
  (cp_parser *);
1497
static void cp_parser_declaration
1498
  (cp_parser *);
1499
static void cp_parser_block_declaration
1500
  (cp_parser *, bool);
1501
static void cp_parser_simple_declaration
1502
  (cp_parser *, bool);
1503
static void cp_parser_decl_specifier_seq
1504
  (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1505
static tree cp_parser_storage_class_specifier_opt
1506
  (cp_parser *);
1507
static tree cp_parser_function_specifier_opt
1508
  (cp_parser *, cp_decl_specifier_seq *);
1509
static tree cp_parser_type_specifier
1510
  (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1511
   int *, bool *);
1512
static tree cp_parser_simple_type_specifier
1513
  (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1514
static tree cp_parser_type_name
1515
  (cp_parser *);
1516
static tree cp_parser_elaborated_type_specifier
1517
  (cp_parser *, bool, bool);
1518
static tree cp_parser_enum_specifier
1519
  (cp_parser *);
1520
static void cp_parser_enumerator_list
1521
  (cp_parser *, tree);
1522
static void cp_parser_enumerator_definition
1523
  (cp_parser *, tree);
1524
static tree cp_parser_namespace_name
1525
  (cp_parser *);
1526
static void cp_parser_namespace_definition
1527
  (cp_parser *);
1528
static void cp_parser_namespace_body
1529
  (cp_parser *);
1530
static tree cp_parser_qualified_namespace_specifier
1531
  (cp_parser *);
1532
static void cp_parser_namespace_alias_definition
1533
  (cp_parser *);
1534
static void cp_parser_using_declaration
1535
  (cp_parser *);
1536
static void cp_parser_using_directive
1537
  (cp_parser *);
1538
static void cp_parser_asm_definition
1539
  (cp_parser *);
1540
static void cp_parser_linkage_specification
1541
  (cp_parser *);
1542
 
1543
/* Declarators [gram.dcl.decl] */
1544
 
1545
static tree cp_parser_init_declarator
1546
  (cp_parser *, cp_decl_specifier_seq *, tree, bool, bool, int, bool *);
1547
static cp_declarator *cp_parser_declarator
1548
  (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1549
static cp_declarator *cp_parser_direct_declarator
1550
  (cp_parser *, cp_parser_declarator_kind, int *, bool);
1551
static enum tree_code cp_parser_ptr_operator
1552
  (cp_parser *, tree *, cp_cv_quals *);
1553
static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1554
  (cp_parser *);
1555
static tree cp_parser_declarator_id
1556
  (cp_parser *);
1557
static tree cp_parser_type_id
1558
  (cp_parser *);
1559
static void cp_parser_type_specifier_seq
1560
  (cp_parser *, bool, cp_decl_specifier_seq *);
1561
static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1562
  (cp_parser *);
1563
static cp_parameter_declarator *cp_parser_parameter_declaration_list
1564
  (cp_parser *, bool *);
1565
static cp_parameter_declarator *cp_parser_parameter_declaration
1566
  (cp_parser *, bool, bool *);
1567
static void cp_parser_function_body
1568
  (cp_parser *);
1569
static tree cp_parser_initializer
1570
  (cp_parser *, bool *, bool *);
1571
static tree cp_parser_initializer_clause
1572
  (cp_parser *, bool *);
1573
static VEC(constructor_elt,gc) *cp_parser_initializer_list
1574
  (cp_parser *, bool *);
1575
 
1576
static bool cp_parser_ctor_initializer_opt_and_function_body
1577
  (cp_parser *);
1578
 
1579
/* Classes [gram.class] */
1580
 
1581
static tree cp_parser_class_name
1582
  (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1583
static tree cp_parser_class_specifier
1584
  (cp_parser *);
1585
static tree cp_parser_class_head
1586
  (cp_parser *, bool *, tree *);
1587
static enum tag_types cp_parser_class_key
1588
  (cp_parser *);
1589
static void cp_parser_member_specification_opt
1590
  (cp_parser *);
1591
static void cp_parser_member_declaration
1592
  (cp_parser *);
1593
static tree cp_parser_pure_specifier
1594
  (cp_parser *);
1595
static tree cp_parser_constant_initializer
1596
  (cp_parser *);
1597
 
1598
/* Derived classes [gram.class.derived] */
1599
 
1600
static tree cp_parser_base_clause
1601
  (cp_parser *);
1602
static tree cp_parser_base_specifier
1603
  (cp_parser *);
1604
 
1605
/* Special member functions [gram.special] */
1606
 
1607
static tree cp_parser_conversion_function_id
1608
  (cp_parser *);
1609
static tree cp_parser_conversion_type_id
1610
  (cp_parser *);
1611
static cp_declarator *cp_parser_conversion_declarator_opt
1612
  (cp_parser *);
1613
static bool cp_parser_ctor_initializer_opt
1614
  (cp_parser *);
1615
static void cp_parser_mem_initializer_list
1616
  (cp_parser *);
1617
static tree cp_parser_mem_initializer
1618
  (cp_parser *);
1619
static tree cp_parser_mem_initializer_id
1620
  (cp_parser *);
1621
 
1622
/* Overloading [gram.over] */
1623
 
1624
static tree cp_parser_operator_function_id
1625
  (cp_parser *);
1626
static tree cp_parser_operator
1627
  (cp_parser *);
1628
 
1629
/* Templates [gram.temp] */
1630
 
1631
static void cp_parser_template_declaration
1632
  (cp_parser *, bool);
1633
static tree cp_parser_template_parameter_list
1634
  (cp_parser *);
1635
static tree cp_parser_template_parameter
1636
  (cp_parser *, bool *);
1637
static tree cp_parser_type_parameter
1638
  (cp_parser *);
1639
static tree cp_parser_template_id
1640
  (cp_parser *, bool, bool, bool);
1641
static tree cp_parser_template_name
1642
  (cp_parser *, bool, bool, bool, bool *);
1643
static tree cp_parser_template_argument_list
1644
  (cp_parser *);
1645
static tree cp_parser_template_argument
1646
  (cp_parser *);
1647
static void cp_parser_explicit_instantiation
1648
  (cp_parser *);
1649
static void cp_parser_explicit_specialization
1650
  (cp_parser *);
1651
 
1652
/* Exception handling [gram.exception] */
1653
 
1654
static tree cp_parser_try_block
1655
  (cp_parser *);
1656
static bool cp_parser_function_try_block
1657
  (cp_parser *);
1658
static void cp_parser_handler_seq
1659
  (cp_parser *);
1660
static void cp_parser_handler
1661
  (cp_parser *);
1662
static tree cp_parser_exception_declaration
1663
  (cp_parser *);
1664
static tree cp_parser_throw_expression
1665
  (cp_parser *);
1666
static tree cp_parser_exception_specification_opt
1667
  (cp_parser *);
1668
static tree cp_parser_type_id_list
1669
  (cp_parser *);
1670
 
1671
/* GNU Extensions */
1672
 
1673
static tree cp_parser_asm_specification_opt
1674
  (cp_parser *);
1675
static tree cp_parser_asm_operand_list
1676
  (cp_parser *);
1677
static tree cp_parser_asm_clobber_list
1678
  (cp_parser *);
1679
static tree cp_parser_attributes_opt
1680
  (cp_parser *);
1681
static tree cp_parser_attribute_list
1682
  (cp_parser *);
1683
static bool cp_parser_extension_opt
1684
  (cp_parser *, int *);
1685
static void cp_parser_label_declaration
1686
  (cp_parser *);
1687
 
1688
/* Objective-C++ Productions */
1689
 
1690
static tree cp_parser_objc_message_receiver
1691
  (cp_parser *);
1692
static tree cp_parser_objc_message_args
1693
  (cp_parser *);
1694
static tree cp_parser_objc_message_expression
1695
  (cp_parser *);
1696
static tree cp_parser_objc_encode_expression
1697
  (cp_parser *);
1698
static tree cp_parser_objc_defs_expression
1699
  (cp_parser *);
1700
static tree cp_parser_objc_protocol_expression
1701
  (cp_parser *);
1702
static tree cp_parser_objc_selector_expression
1703
  (cp_parser *);
1704
static tree cp_parser_objc_expression
1705
  (cp_parser *);
1706
static bool cp_parser_objc_selector_p
1707
  (enum cpp_ttype);
1708
static tree cp_parser_objc_selector
1709
  (cp_parser *);
1710
static tree cp_parser_objc_protocol_refs_opt
1711
  (cp_parser *);
1712
static void cp_parser_objc_declaration
1713
  (cp_parser *);
1714
static tree cp_parser_objc_statement
1715
  (cp_parser *);
1716
 
1717
/* Utility Routines */
1718
 
1719
static tree cp_parser_lookup_name
1720
  (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1721
static tree cp_parser_lookup_name_simple
1722
  (cp_parser *, tree);
1723
static tree cp_parser_maybe_treat_template_as_class
1724
  (tree, bool);
1725
static bool cp_parser_check_declarator_template_parameters
1726
  (cp_parser *, cp_declarator *);
1727
static bool cp_parser_check_template_parameters
1728
  (cp_parser *, unsigned);
1729
static tree cp_parser_simple_cast_expression
1730
  (cp_parser *);
1731
static tree cp_parser_global_scope_opt
1732
  (cp_parser *, bool);
1733
static bool cp_parser_constructor_declarator_p
1734
  (cp_parser *, bool);
1735
static tree cp_parser_function_definition_from_specifiers_and_declarator
1736
  (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1737
static tree cp_parser_function_definition_after_declarator
1738
  (cp_parser *, bool);
1739
static void cp_parser_template_declaration_after_export
1740
  (cp_parser *, bool);
1741
static void cp_parser_perform_template_parameter_access_checks
1742
  (tree);
1743
static tree cp_parser_single_declaration
1744
  (cp_parser *, tree, bool, bool *);
1745
static tree cp_parser_functional_cast
1746
  (cp_parser *, tree);
1747
static tree cp_parser_save_member_function_body
1748
  (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1749
static tree cp_parser_enclosed_template_argument_list
1750
  (cp_parser *);
1751
static void cp_parser_save_default_args
1752
  (cp_parser *, tree);
1753
static void cp_parser_late_parsing_for_member
1754
  (cp_parser *, tree);
1755
static void cp_parser_late_parsing_default_args
1756
  (cp_parser *, tree);
1757
static tree cp_parser_sizeof_operand
1758
  (cp_parser *, enum rid);
1759
static bool cp_parser_declares_only_class_p
1760
  (cp_parser *);
1761
static void cp_parser_set_storage_class
1762
  (cp_decl_specifier_seq *, cp_storage_class);
1763
static void cp_parser_set_decl_spec_type
1764
  (cp_decl_specifier_seq *, tree, bool);
1765
static bool cp_parser_friend_p
1766
  (const cp_decl_specifier_seq *);
1767
static cp_token *cp_parser_require
1768
  (cp_parser *, enum cpp_ttype, const char *);
1769
static cp_token *cp_parser_require_keyword
1770
  (cp_parser *, enum rid, const char *);
1771
static bool cp_parser_token_starts_function_definition_p
1772
  (cp_token *);
1773
static bool cp_parser_next_token_starts_class_definition_p
1774
  (cp_parser *);
1775
static bool cp_parser_next_token_ends_template_argument_p
1776
  (cp_parser *);
1777
static bool cp_parser_nth_token_starts_template_argument_list_p
1778
  (cp_parser *, size_t);
1779
static enum tag_types cp_parser_token_is_class_key
1780
  (cp_token *);
1781
static void cp_parser_check_class_key
1782
  (enum tag_types, tree type);
1783
static void cp_parser_check_access_in_redeclaration
1784
  (tree type);
1785
static bool cp_parser_optional_template_keyword
1786
  (cp_parser *);
1787
static void cp_parser_pre_parsed_nested_name_specifier
1788
  (cp_parser *);
1789
static void cp_parser_cache_group
1790
  (cp_parser *, enum cpp_ttype, unsigned);
1791
static void cp_parser_parse_tentatively
1792
  (cp_parser *);
1793
static void cp_parser_commit_to_tentative_parse
1794
  (cp_parser *);
1795
static void cp_parser_abort_tentative_parse
1796
  (cp_parser *);
1797
static bool cp_parser_parse_definitely
1798
  (cp_parser *);
1799
static inline bool cp_parser_parsing_tentatively
1800
  (cp_parser *);
1801
static bool cp_parser_uncommitted_to_tentative_parse_p
1802
  (cp_parser *);
1803
static void cp_parser_error
1804
  (cp_parser *, const char *);
1805
static void cp_parser_name_lookup_error
1806
  (cp_parser *, tree, tree, const char *);
1807
static bool cp_parser_simulate_error
1808
  (cp_parser *);
1809
static void cp_parser_check_type_definition
1810
  (cp_parser *);
1811
static void cp_parser_check_for_definition_in_return_type
1812
  (cp_declarator *, tree);
1813
static void cp_parser_check_for_invalid_template_id
1814
  (cp_parser *, tree);
1815
static bool cp_parser_non_integral_constant_expression
1816
  (cp_parser *, const char *);
1817
static void cp_parser_diagnose_invalid_type_name
1818
  (cp_parser *, tree, tree);
1819
static bool cp_parser_parse_and_diagnose_invalid_type_name
1820
  (cp_parser *);
1821
static int cp_parser_skip_to_closing_parenthesis
1822
  (cp_parser *, bool, bool, bool);
1823
static void cp_parser_skip_to_end_of_statement
1824
  (cp_parser *);
1825
static void cp_parser_consume_semicolon_at_end_of_statement
1826
  (cp_parser *);
1827
static void cp_parser_skip_to_end_of_block_or_statement
1828
  (cp_parser *);
1829
static void cp_parser_skip_to_closing_brace
1830
  (cp_parser *);
1831
static void cp_parser_skip_until_found
1832
  (cp_parser *, enum cpp_ttype, const char *);
1833
static bool cp_parser_error_occurred
1834
  (cp_parser *);
1835
static bool cp_parser_allow_gnu_extensions_p
1836
  (cp_parser *);
1837
static bool cp_parser_is_string_literal
1838
  (cp_token *);
1839
static bool cp_parser_is_keyword
1840
  (cp_token *, enum rid);
1841
static tree cp_parser_make_typename_type
1842
  (cp_parser *, tree, tree);
1843
 
1844
/* Returns nonzero if we are parsing tentatively.  */
1845
 
1846
static inline bool
1847
cp_parser_parsing_tentatively (cp_parser* parser)
1848
{
1849
  return parser->context->next != NULL;
1850
}
1851
 
1852
/* Returns nonzero if TOKEN is a string literal.  */
1853
 
1854
static bool
1855
cp_parser_is_string_literal (cp_token* token)
1856
{
1857
  return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1858
}
1859
 
1860
/* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1861
 
1862
static bool
1863
cp_parser_is_keyword (cp_token* token, enum rid keyword)
1864
{
1865
  return token->keyword == keyword;
1866
}
1867
 
1868
/* A minimum or maximum operator has been seen.  As these are
1869
   deprecated, issue a warning.  */
1870
 
1871
static inline void
1872
cp_parser_warn_min_max (void)
1873
{
1874
  if (warn_deprecated && !in_system_header)
1875
    warning (0, "minimum/maximum operators are deprecated");
1876
}
1877
 
1878
/* If not parsing tentatively, issue a diagnostic of the form
1879
      FILE:LINE: MESSAGE before TOKEN
1880
   where TOKEN is the next token in the input stream.  MESSAGE
1881
   (specified by the caller) is usually of the form "expected
1882
   OTHER-TOKEN".  */
1883
 
1884
static void
1885
cp_parser_error (cp_parser* parser, const char* message)
1886
{
1887
  if (!cp_parser_simulate_error (parser))
1888
    {
1889
      cp_token *token = cp_lexer_peek_token (parser->lexer);
1890
      /* This diagnostic makes more sense if it is tagged to the line
1891
         of the token we just peeked at.  */
1892
      cp_lexer_set_source_position_from_token (token);
1893
      if (token->type == CPP_PRAGMA)
1894
        {
1895
          error ("%<#pragma%> is not allowed here");
1896
          cp_lexer_purge_token (parser->lexer);
1897
          return;
1898
        }
1899
      c_parse_error (message,
1900
                     /* Because c_parser_error does not understand
1901
                        CPP_KEYWORD, keywords are treated like
1902
                        identifiers.  */
1903
                     (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1904
                     token->value);
1905
    }
1906
}
1907
 
1908
/* Issue an error about name-lookup failing.  NAME is the
1909
   IDENTIFIER_NODE DECL is the result of
1910
   the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1911
   the thing that we hoped to find.  */
1912
 
1913
static void
1914
cp_parser_name_lookup_error (cp_parser* parser,
1915
                             tree name,
1916
                             tree decl,
1917
                             const char* desired)
1918
{
1919
  /* If name lookup completely failed, tell the user that NAME was not
1920
     declared.  */
1921
  if (decl == error_mark_node)
1922
    {
1923
      if (parser->scope && parser->scope != global_namespace)
1924
        error ("%<%D::%D%> has not been declared",
1925
               parser->scope, name);
1926
      else if (parser->scope == global_namespace)
1927
        error ("%<::%D%> has not been declared", name);
1928
      else if (parser->object_scope
1929
               && !CLASS_TYPE_P (parser->object_scope))
1930
        error ("request for member %qD in non-class type %qT",
1931
               name, parser->object_scope);
1932
      else if (parser->object_scope)
1933
        error ("%<%T::%D%> has not been declared",
1934
               parser->object_scope, name);
1935
      else
1936
        error ("%qD has not been declared", name);
1937
    }
1938
  else if (parser->scope && parser->scope != global_namespace)
1939
    error ("%<%D::%D%> %s", parser->scope, name, desired);
1940
  else if (parser->scope == global_namespace)
1941
    error ("%<::%D%> %s", name, desired);
1942
  else
1943
    error ("%qD %s", name, desired);
1944
}
1945
 
1946
/* If we are parsing tentatively, remember that an error has occurred
1947
   during this tentative parse.  Returns true if the error was
1948
   simulated; false if a message should be issued by the caller.  */
1949
 
1950
static bool
1951
cp_parser_simulate_error (cp_parser* parser)
1952
{
1953
  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1954
    {
1955
      parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1956
      return true;
1957
    }
1958
  return false;
1959
}
1960
 
1961
/* This function is called when a type is defined.  If type
1962
   definitions are forbidden at this point, an error message is
1963
   issued.  */
1964
 
1965
static void
1966
cp_parser_check_type_definition (cp_parser* parser)
1967
{
1968
  /* If types are forbidden here, issue a message.  */
1969
  if (parser->type_definition_forbidden_message)
1970
    /* Use `%s' to print the string in case there are any escape
1971
       characters in the message.  */
1972
    error ("%s", parser->type_definition_forbidden_message);
1973
}
1974
 
1975
/* This function is called when the DECLARATOR is processed.  The TYPE
1976
   was a type defined in the decl-specifiers.  If it is invalid to
1977
   define a type in the decl-specifiers for DECLARATOR, an error is
1978
   issued.  */
1979
 
1980
static void
1981
cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1982
                                               tree type)
1983
{
1984
  /* [dcl.fct] forbids type definitions in return types.
1985
     Unfortunately, it's not easy to know whether or not we are
1986
     processing a return type until after the fact.  */
1987
  while (declarator
1988
         && (declarator->kind == cdk_pointer
1989
             || declarator->kind == cdk_reference
1990
             || declarator->kind == cdk_ptrmem))
1991
    declarator = declarator->declarator;
1992
  if (declarator
1993
      && declarator->kind == cdk_function)
1994
    {
1995
      error ("new types may not be defined in a return type");
1996
      inform ("(perhaps a semicolon is missing after the definition of %qT)",
1997
              type);
1998
    }
1999
}
2000
 
2001
/* A type-specifier (TYPE) has been parsed which cannot be followed by
2002
   "<" in any valid C++ program.  If the next token is indeed "<",
2003
   issue a message warning the user about what appears to be an
2004
   invalid attempt to form a template-id.  */
2005
 
2006
static void
2007
cp_parser_check_for_invalid_template_id (cp_parser* parser,
2008
                                         tree type)
2009
{
2010
  cp_token_position start = 0;
2011
 
2012
  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2013
    {
2014
      if (TYPE_P (type))
2015
        error ("%qT is not a template", type);
2016
      else if (TREE_CODE (type) == IDENTIFIER_NODE)
2017
        error ("%qE is not a template", type);
2018
      else
2019
        error ("invalid template-id");
2020
      /* Remember the location of the invalid "<".  */
2021
      if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2022
        start = cp_lexer_token_position (parser->lexer, true);
2023
      /* Consume the "<".  */
2024
      cp_lexer_consume_token (parser->lexer);
2025
      /* Parse the template arguments.  */
2026
      cp_parser_enclosed_template_argument_list (parser);
2027
      /* Permanently remove the invalid template arguments so that
2028
         this error message is not issued again.  */
2029
      if (start)
2030
        cp_lexer_purge_tokens_after (parser->lexer, start);
2031
    }
2032
}
2033
 
2034
/* If parsing an integral constant-expression, issue an error message
2035
   about the fact that THING appeared and return true.  Otherwise,
2036
   return false.  In either case, set
2037
   PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2038
 
2039
static bool
2040
cp_parser_non_integral_constant_expression (cp_parser  *parser,
2041
                                            const char *thing)
2042
{
2043
  parser->non_integral_constant_expression_p = true;
2044
  if (parser->integral_constant_expression_p)
2045
    {
2046
      if (!parser->allow_non_integral_constant_expression_p)
2047
        {
2048
          error ("%s cannot appear in a constant-expression", thing);
2049
          return true;
2050
        }
2051
    }
2052
  return false;
2053
}
2054
 
2055
/* Emit a diagnostic for an invalid type name.  SCOPE is the
2056
   qualifying scope (or NULL, if none) for ID.  This function commits
2057
   to the current active tentative parse, if any.  (Otherwise, the
2058
   problematic construct might be encountered again later, resulting
2059
   in duplicate error messages.)  */
2060
 
2061
static void
2062
cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2063
{
2064
  tree decl, old_scope;
2065
  /* Try to lookup the identifier.  */
2066
  old_scope = parser->scope;
2067
  parser->scope = scope;
2068
  decl = cp_parser_lookup_name_simple (parser, id);
2069
  parser->scope = old_scope;
2070
  /* If the lookup found a template-name, it means that the user forgot
2071
  to specify an argument list. Emit a useful error message.  */
2072
  if (TREE_CODE (decl) == TEMPLATE_DECL)
2073
    error ("invalid use of template-name %qE without an argument list",
2074
      decl);
2075
  else if (!parser->scope)
2076
    {
2077
      /* Issue an error message.  */
2078
      error ("%qE does not name a type", id);
2079
      /* If we're in a template class, it's possible that the user was
2080
         referring to a type from a base class.  For example:
2081
 
2082
           template <typename T> struct A { typedef T X; };
2083
           template <typename T> struct B : public A<T> { X x; };
2084
 
2085
         The user should have said "typename A<T>::X".  */
2086
      if (processing_template_decl && current_class_type
2087
          && TYPE_BINFO (current_class_type))
2088
        {
2089
          tree b;
2090
 
2091
          for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2092
               b;
2093
               b = TREE_CHAIN (b))
2094
            {
2095
              tree base_type = BINFO_TYPE (b);
2096
              if (CLASS_TYPE_P (base_type)
2097
                  && dependent_type_p (base_type))
2098
                {
2099
                  tree field;
2100
                  /* Go from a particular instantiation of the
2101
                     template (which will have an empty TYPE_FIELDs),
2102
                     to the main version.  */
2103
                  base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2104
                  for (field = TYPE_FIELDS (base_type);
2105
                       field;
2106
                       field = TREE_CHAIN (field))
2107
                    if (TREE_CODE (field) == TYPE_DECL
2108
                        && DECL_NAME (field) == id)
2109
                      {
2110
                        inform ("(perhaps %<typename %T::%E%> was intended)",
2111
                                BINFO_TYPE (b), id);
2112
                        break;
2113
                      }
2114
                  if (field)
2115
                    break;
2116
                }
2117
            }
2118
        }
2119
    }
2120
  /* Here we diagnose qualified-ids where the scope is actually correct,
2121
     but the identifier does not resolve to a valid type name.  */
2122
  else if (parser->scope != error_mark_node)
2123
    {
2124
      if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2125
        error ("%qE in namespace %qE does not name a type",
2126
               id, parser->scope);
2127
      else if (TYPE_P (parser->scope))
2128
        error ("%qE in class %qT does not name a type", id, parser->scope);
2129
      else
2130
        gcc_unreachable ();
2131
    }
2132
  cp_parser_commit_to_tentative_parse (parser);
2133
}
2134
 
2135
/* Check for a common situation where a type-name should be present,
2136
   but is not, and issue a sensible error message.  Returns true if an
2137
   invalid type-name was detected.
2138
 
2139
   The situation handled by this function are variable declarations of the
2140
   form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2141
   Usually, `ID' should name a type, but if we got here it means that it
2142
   does not. We try to emit the best possible error message depending on
2143
   how exactly the id-expression looks like.
2144
*/
2145
 
2146
static bool
2147
cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2148
{
2149
  tree id;
2150
 
2151
  cp_parser_parse_tentatively (parser);
2152
  id = cp_parser_id_expression (parser,
2153
                                /*template_keyword_p=*/false,
2154
                                /*check_dependency_p=*/true,
2155
                                /*template_p=*/NULL,
2156
                                /*declarator_p=*/true);
2157
  /* After the id-expression, there should be a plain identifier,
2158
     otherwise this is not a simple variable declaration. Also, if
2159
     the scope is dependent, we cannot do much.  */
2160
  if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2161
      || (parser->scope && TYPE_P (parser->scope)
2162
          && dependent_type_p (parser->scope)))
2163
    {
2164
      cp_parser_abort_tentative_parse (parser);
2165
      return false;
2166
    }
2167
  if (!cp_parser_parse_definitely (parser)
2168
      || TREE_CODE (id) != IDENTIFIER_NODE)
2169
    return false;
2170
 
2171
  /* Emit a diagnostic for the invalid type.  */
2172
  cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2173
  /* Skip to the end of the declaration; there's no point in
2174
     trying to process it.  */
2175
  cp_parser_skip_to_end_of_block_or_statement (parser);
2176
  return true;
2177
}
2178
 
2179
/* Consume tokens up to, and including, the next non-nested closing `)'.
2180
   Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2181
   are doing error recovery. Returns -1 if OR_COMMA is true and we
2182
   found an unnested comma.  */
2183
 
2184
static int
2185
cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2186
                                       bool recovering,
2187
                                       bool or_comma,
2188
                                       bool consume_paren)
2189
{
2190
  unsigned paren_depth = 0;
2191
  unsigned brace_depth = 0;
2192
  int result;
2193
 
2194
  if (recovering && !or_comma
2195
      && cp_parser_uncommitted_to_tentative_parse_p (parser))
2196
    return 0;
2197
 
2198
  while (true)
2199
    {
2200
      cp_token *token;
2201
 
2202
      /* If we've run out of tokens, then there is no closing `)'.  */
2203
      if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2204
        {
2205
          result = 0;
2206
          break;
2207
        }
2208
 
2209
      token = cp_lexer_peek_token (parser->lexer);
2210
 
2211
      /* This matches the processing in skip_to_end_of_statement.  */
2212
      if (token->type == CPP_SEMICOLON && !brace_depth)
2213
        {
2214
          result = 0;
2215
          break;
2216
        }
2217
      if (token->type == CPP_OPEN_BRACE)
2218
        ++brace_depth;
2219
      if (token->type == CPP_CLOSE_BRACE)
2220
        {
2221
          if (!brace_depth--)
2222
            {
2223
              result = 0;
2224
              break;
2225
            }
2226
        }
2227
      if (recovering && or_comma && token->type == CPP_COMMA
2228
          && !brace_depth && !paren_depth)
2229
        {
2230
          result = -1;
2231
          break;
2232
        }
2233
 
2234
      if (!brace_depth)
2235
        {
2236
          /* If it is an `(', we have entered another level of nesting.  */
2237
          if (token->type == CPP_OPEN_PAREN)
2238
            ++paren_depth;
2239
          /* If it is a `)', then we might be done.  */
2240
          else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2241
            {
2242
              if (consume_paren)
2243
                cp_lexer_consume_token (parser->lexer);
2244
              {
2245
                result = 1;
2246
                break;
2247
              }
2248
            }
2249
        }
2250
 
2251
      /* Consume the token.  */
2252
      cp_lexer_consume_token (parser->lexer);
2253
    }
2254
 
2255
  return result;
2256
}
2257
 
2258
/* Consume tokens until we reach the end of the current statement.
2259
   Normally, that will be just before consuming a `;'.  However, if a
2260
   non-nested `}' comes first, then we stop before consuming that.  */
2261
 
2262
static void
2263
cp_parser_skip_to_end_of_statement (cp_parser* parser)
2264
{
2265
  unsigned nesting_depth = 0;
2266
 
2267
  while (true)
2268
    {
2269
      cp_token *token;
2270
 
2271
      /* Peek at the next token.  */
2272
      token = cp_lexer_peek_token (parser->lexer);
2273
      /* If we've run out of tokens, stop.  */
2274
      if (token->type == CPP_EOF)
2275
        break;
2276
      /* If the next token is a `;', we have reached the end of the
2277
         statement.  */
2278
      if (token->type == CPP_SEMICOLON && !nesting_depth)
2279
        break;
2280
      /* If the next token is a non-nested `}', then we have reached
2281
         the end of the current block.  */
2282
      if (token->type == CPP_CLOSE_BRACE)
2283
        {
2284
          /* If this is a non-nested `}', stop before consuming it.
2285
             That way, when confronted with something like:
2286
 
2287
               { 3 + }
2288
 
2289
             we stop before consuming the closing `}', even though we
2290
             have not yet reached a `;'.  */
2291
          if (nesting_depth == 0)
2292
            break;
2293
          /* If it is the closing `}' for a block that we have
2294
             scanned, stop -- but only after consuming the token.
2295
             That way given:
2296
 
2297
                void f g () { ... }
2298
                typedef int I;
2299
 
2300
             we will stop after the body of the erroneously declared
2301
             function, but before consuming the following `typedef'
2302
             declaration.  */
2303
          if (--nesting_depth == 0)
2304
            {
2305
              cp_lexer_consume_token (parser->lexer);
2306
              break;
2307
            }
2308
        }
2309
      /* If it the next token is a `{', then we are entering a new
2310
         block.  Consume the entire block.  */
2311
      else if (token->type == CPP_OPEN_BRACE)
2312
        ++nesting_depth;
2313
      /* Consume the token.  */
2314
      cp_lexer_consume_token (parser->lexer);
2315
    }
2316
}
2317
 
2318
/* This function is called at the end of a statement or declaration.
2319
   If the next token is a semicolon, it is consumed; otherwise, error
2320
   recovery is attempted.  */
2321
 
2322
static void
2323
cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2324
{
2325
  /* Look for the trailing `;'.  */
2326
  if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2327
    {
2328
      /* If there is additional (erroneous) input, skip to the end of
2329
         the statement.  */
2330
      cp_parser_skip_to_end_of_statement (parser);
2331
      /* If the next token is now a `;', consume it.  */
2332
      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2333
        cp_lexer_consume_token (parser->lexer);
2334
    }
2335
}
2336
 
2337
/* Skip tokens until we have consumed an entire block, or until we
2338
   have consumed a non-nested `;'.  */
2339
 
2340
static void
2341
cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2342
{
2343
  int nesting_depth = 0;
2344
 
2345
  while (nesting_depth >= 0)
2346
    {
2347
      cp_token *token = cp_lexer_peek_token (parser->lexer);
2348
 
2349
      if (token->type == CPP_EOF)
2350
        break;
2351
 
2352
      switch (token->type)
2353
        {
2354
        case CPP_EOF:
2355
          /* If we've run out of tokens, stop.  */
2356
          nesting_depth = -1;
2357
          continue;
2358
 
2359
        case CPP_SEMICOLON:
2360
          /* Stop if this is an unnested ';'. */
2361
          if (!nesting_depth)
2362
            nesting_depth = -1;
2363
          break;
2364
 
2365
        case CPP_CLOSE_BRACE:
2366
          /* Stop if this is an unnested '}', or closes the outermost
2367
             nesting level.  */
2368
          nesting_depth--;
2369
          if (!nesting_depth)
2370
            nesting_depth = -1;
2371
          break;
2372
 
2373
        case CPP_OPEN_BRACE:
2374
          /* Nest. */
2375
          nesting_depth++;
2376
          break;
2377
 
2378
        default:
2379
          break;
2380
        }
2381
 
2382
      /* Consume the token.  */
2383
      cp_lexer_consume_token (parser->lexer);
2384
 
2385
    }
2386
}
2387
 
2388
/* Skip tokens until a non-nested closing curly brace is the next
2389
   token.  */
2390
 
2391
static void
2392
cp_parser_skip_to_closing_brace (cp_parser *parser)
2393
{
2394
  unsigned nesting_depth = 0;
2395
 
2396
  while (true)
2397
    {
2398
      cp_token *token;
2399
 
2400
      /* Peek at the next token.  */
2401
      token = cp_lexer_peek_token (parser->lexer);
2402
      /* If we've run out of tokens, stop.  */
2403
      if (token->type == CPP_EOF)
2404
        break;
2405
      /* If the next token is a non-nested `}', then we have reached
2406
         the end of the current block.  */
2407
      if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2408
        break;
2409
      /* If it the next token is a `{', then we are entering a new
2410
         block.  Consume the entire block.  */
2411
      else if (token->type == CPP_OPEN_BRACE)
2412
        ++nesting_depth;
2413
      /* Consume the token.  */
2414
      cp_lexer_consume_token (parser->lexer);
2415
    }
2416
}
2417
 
2418
/* This is a simple wrapper around make_typename_type. When the id is
2419
   an unresolved identifier node, we can provide a superior diagnostic
2420
   using cp_parser_diagnose_invalid_type_name.  */
2421
 
2422
static tree
2423
cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2424
{
2425
  tree result;
2426
  if (TREE_CODE (id) == IDENTIFIER_NODE)
2427
    {
2428
      result = make_typename_type (scope, id, typename_type,
2429
                                   /*complain=*/0);
2430
      if (result == error_mark_node)
2431
        cp_parser_diagnose_invalid_type_name (parser, scope, id);
2432
      return result;
2433
    }
2434
  return make_typename_type (scope, id, typename_type, tf_error);
2435
}
2436
 
2437
 
2438
/* Create a new C++ parser.  */
2439
 
2440
static cp_parser *
2441
cp_parser_new (void)
2442
{
2443
  cp_parser *parser;
2444
  cp_lexer *lexer;
2445
  unsigned i;
2446
 
2447
  /* cp_lexer_new_main is called before calling ggc_alloc because
2448
     cp_lexer_new_main might load a PCH file.  */
2449
  lexer = cp_lexer_new_main ();
2450
 
2451
  /* Initialize the binops_by_token so that we can get the tree
2452
     directly from the token.  */
2453
  for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2454
    binops_by_token[binops[i].token_type] = binops[i];
2455
 
2456
  parser = GGC_CNEW (cp_parser);
2457
  parser->lexer = lexer;
2458
  parser->context = cp_parser_context_new (NULL);
2459
 
2460
  /* For now, we always accept GNU extensions.  */
2461
  parser->allow_gnu_extensions_p = 1;
2462
 
2463
  /* The `>' token is a greater-than operator, not the end of a
2464
     template-id.  */
2465
  parser->greater_than_is_operator_p = true;
2466
 
2467
  parser->default_arg_ok_p = true;
2468
 
2469
  /* We are not parsing a constant-expression.  */
2470
  parser->integral_constant_expression_p = false;
2471
  parser->allow_non_integral_constant_expression_p = false;
2472
  parser->non_integral_constant_expression_p = false;
2473
 
2474
  /* Local variable names are not forbidden.  */
2475
  parser->local_variables_forbidden_p = false;
2476
 
2477
  /* We are not processing an `extern "C"' declaration.  */
2478
  parser->in_unbraced_linkage_specification_p = false;
2479
 
2480
  /* We are not processing a declarator.  */
2481
  parser->in_declarator_p = false;
2482
 
2483
  /* We are not processing a template-argument-list.  */
2484
  parser->in_template_argument_list_p = false;
2485
 
2486
  /* We are not in an iteration statement.  */
2487
  parser->in_iteration_statement_p = false;
2488
 
2489
  /* We are not in a switch statement.  */
2490
  parser->in_switch_statement_p = false;
2491
 
2492
  /* We are not parsing a type-id inside an expression.  */
2493
  parser->in_type_id_in_expr_p = false;
2494
 
2495
  /* Declarations aren't implicitly extern "C".  */
2496
  parser->implicit_extern_c = false;
2497
 
2498
  /* String literals should be translated to the execution character set.  */
2499
  parser->translate_strings_p = true;
2500
 
2501
  /* The unparsed function queue is empty.  */
2502
  parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2503
 
2504
  /* There are no classes being defined.  */
2505
  parser->num_classes_being_defined = 0;
2506
 
2507
  /* No template parameters apply.  */
2508
  parser->num_template_parameter_lists = 0;
2509
 
2510
  return parser;
2511
}
2512
 
2513
/* Create a cp_lexer structure which will emit the tokens in CACHE
2514
   and push it onto the parser's lexer stack.  This is used for delayed
2515
   parsing of in-class method bodies and default arguments, and should
2516
   not be confused with tentative parsing.  */
2517
static void
2518
cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2519
{
2520
  cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2521
  lexer->next = parser->lexer;
2522
  parser->lexer = lexer;
2523
 
2524
  /* Move the current source position to that of the first token in the
2525
     new lexer.  */
2526
  cp_lexer_set_source_position_from_token (lexer->next_token);
2527
}
2528
 
2529
/* Pop the top lexer off the parser stack.  This is never used for the
2530
   "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2531
static void
2532
cp_parser_pop_lexer (cp_parser *parser)
2533
{
2534
  cp_lexer *lexer = parser->lexer;
2535
  parser->lexer = lexer->next;
2536
  cp_lexer_destroy (lexer);
2537
 
2538
  /* Put the current source position back where it was before this
2539
     lexer was pushed.  */
2540
  cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2541
}
2542
 
2543
/* Lexical conventions [gram.lex]  */
2544
 
2545
/* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2546
   identifier.  */
2547
 
2548
static tree
2549
cp_parser_identifier (cp_parser* parser)
2550
{
2551
  cp_token *token;
2552
 
2553
  /* Look for the identifier.  */
2554
  token = cp_parser_require (parser, CPP_NAME, "identifier");
2555
  /* Return the value.  */
2556
  return token ? token->value : error_mark_node;
2557
}
2558
 
2559
/* Parse a sequence of adjacent string constants.  Returns a
2560
   TREE_STRING representing the combined, nul-terminated string
2561
   constant.  If TRANSLATE is true, translate the string to the
2562
   execution character set.  If WIDE_OK is true, a wide string is
2563
   invalid here.
2564
 
2565
   C++98 [lex.string] says that if a narrow string literal token is
2566
   adjacent to a wide string literal token, the behavior is undefined.
2567
   However, C99 6.4.5p4 says that this results in a wide string literal.
2568
   We follow C99 here, for consistency with the C front end.
2569
 
2570
   This code is largely lifted from lex_string() in c-lex.c.
2571
 
2572
   FUTURE: ObjC++ will need to handle @-strings here.  */
2573
static tree
2574
cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2575
{
2576
  tree value;
2577
  bool wide = false;
2578
  size_t count;
2579
  struct obstack str_ob;
2580
  cpp_string str, istr, *strs;
2581
  cp_token *tok;
2582
 
2583
  tok = cp_lexer_peek_token (parser->lexer);
2584
  if (!cp_parser_is_string_literal (tok))
2585
    {
2586
      cp_parser_error (parser, "expected string-literal");
2587
      return error_mark_node;
2588
    }
2589
 
2590
  /* Try to avoid the overhead of creating and destroying an obstack
2591
     for the common case of just one string.  */
2592
  if (!cp_parser_is_string_literal
2593
      (cp_lexer_peek_nth_token (parser->lexer, 2)))
2594
    {
2595
      cp_lexer_consume_token (parser->lexer);
2596
 
2597
      str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2598
      str.len = TREE_STRING_LENGTH (tok->value);
2599
      count = 1;
2600
      if (tok->type == CPP_WSTRING)
2601
        wide = true;
2602
 
2603
      strs = &str;
2604
    }
2605
  else
2606
    {
2607
      gcc_obstack_init (&str_ob);
2608
      count = 0;
2609
 
2610
      do
2611
        {
2612
          cp_lexer_consume_token (parser->lexer);
2613
          count++;
2614
          str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2615
          str.len = TREE_STRING_LENGTH (tok->value);
2616
          if (tok->type == CPP_WSTRING)
2617
            wide = true;
2618
 
2619
          obstack_grow (&str_ob, &str, sizeof (cpp_string));
2620
 
2621
          tok = cp_lexer_peek_token (parser->lexer);
2622
        }
2623
      while (cp_parser_is_string_literal (tok));
2624
 
2625
      strs = (cpp_string *) obstack_finish (&str_ob);
2626
    }
2627
 
2628
  if (wide && !wide_ok)
2629
    {
2630
      cp_parser_error (parser, "a wide string is invalid in this context");
2631
      wide = false;
2632
    }
2633
 
2634
  if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2635
      (parse_in, strs, count, &istr, wide))
2636
    {
2637
      value = build_string (istr.len, (char *)istr.text);
2638
      free ((void *)istr.text);
2639
 
2640
      TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2641
      value = fix_string_type (value);
2642
    }
2643
  else
2644
    /* cpp_interpret_string has issued an error.  */
2645
    value = error_mark_node;
2646
 
2647
  if (count > 1)
2648
    obstack_free (&str_ob, 0);
2649
 
2650
  return value;
2651
}
2652
 
2653
 
2654
/* Basic concepts [gram.basic]  */
2655
 
2656
/* Parse a translation-unit.
2657
 
2658
   translation-unit:
2659
     declaration-seq [opt]
2660
 
2661
   Returns TRUE if all went well.  */
2662
 
2663
static bool
2664
cp_parser_translation_unit (cp_parser* parser)
2665
{
2666
  /* The address of the first non-permanent object on the declarator
2667
     obstack.  */
2668
  static void *declarator_obstack_base;
2669
 
2670
  bool success;
2671
 
2672
  /* Create the declarator obstack, if necessary.  */
2673
  if (!cp_error_declarator)
2674
    {
2675
      gcc_obstack_init (&declarator_obstack);
2676
      /* Create the error declarator.  */
2677
      cp_error_declarator = make_declarator (cdk_error);
2678
      /* Create the empty parameter list.  */
2679
      no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2680
      /* Remember where the base of the declarator obstack lies.  */
2681
      declarator_obstack_base = obstack_next_free (&declarator_obstack);
2682
    }
2683
 
2684
  cp_parser_declaration_seq_opt (parser);
2685
 
2686
  /* If there are no tokens left then all went well.  */
2687
  if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2688
    {
2689
      /* Get rid of the token array; we don't need it any more.  */
2690
      cp_lexer_destroy (parser->lexer);
2691
      parser->lexer = NULL;
2692
 
2693
      /* This file might have been a context that's implicitly extern
2694
         "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2695
      if (parser->implicit_extern_c)
2696
        {
2697
          pop_lang_context ();
2698
          parser->implicit_extern_c = false;
2699
        }
2700
 
2701
      /* Finish up.  */
2702
      finish_translation_unit ();
2703
 
2704
      success = true;
2705
    }
2706
  else
2707
    {
2708
      cp_parser_error (parser, "expected declaration");
2709
      success = false;
2710
    }
2711
 
2712
  /* Make sure the declarator obstack was fully cleaned up.  */
2713
  gcc_assert (obstack_next_free (&declarator_obstack)
2714
              == declarator_obstack_base);
2715
 
2716
  /* All went well.  */
2717
  return success;
2718
}
2719
 
2720
/* Expressions [gram.expr] */
2721
 
2722
/* Parse a primary-expression.
2723
 
2724
   primary-expression:
2725
     literal
2726
     this
2727
     ( expression )
2728
     id-expression
2729
 
2730
   GNU Extensions:
2731
 
2732
   primary-expression:
2733
     ( compound-statement )
2734
     __builtin_va_arg ( assignment-expression , type-id )
2735
 
2736
   Objective-C++ Extension:
2737
 
2738
   primary-expression:
2739
     objc-expression
2740
 
2741
   literal:
2742
     __null
2743
 
2744
   ADDRESS_P is true iff this expression was immediately preceded by
2745
   "&" and therefore might denote a pointer-to-member.  CAST_P is true
2746
   iff this expression is the target of a cast.  TEMPLATE_ARG_P is
2747
   true iff this expression is a tempalte argument.
2748
 
2749
   Returns a representation of the expression.  Upon return, *IDK
2750
   indicates what kind of id-expression (if any) was present.  */
2751
 
2752
static tree
2753
cp_parser_primary_expression (cp_parser *parser,
2754
                              bool address_p,
2755
                              bool cast_p,
2756
                              bool template_arg_p,
2757
                              cp_id_kind *idk)
2758
{
2759
  cp_token *token;
2760
 
2761
  /* Assume the primary expression is not an id-expression.  */
2762
  *idk = CP_ID_KIND_NONE;
2763
 
2764
  /* Peek at the next token.  */
2765
  token = cp_lexer_peek_token (parser->lexer);
2766
  switch (token->type)
2767
    {
2768
      /* literal:
2769
           integer-literal
2770
           character-literal
2771
           floating-literal
2772
           string-literal
2773
           boolean-literal  */
2774
    case CPP_CHAR:
2775
    case CPP_WCHAR:
2776
    case CPP_NUMBER:
2777
      token = cp_lexer_consume_token (parser->lexer);
2778
      /* Floating-point literals are only allowed in an integral
2779
         constant expression if they are cast to an integral or
2780
         enumeration type.  */
2781
      if (TREE_CODE (token->value) == REAL_CST
2782
          && parser->integral_constant_expression_p
2783
          && pedantic)
2784
        {
2785
          /* CAST_P will be set even in invalid code like "int(2.7 +
2786
             ...)".   Therefore, we have to check that the next token
2787
             is sure to end the cast.  */
2788
          if (cast_p)
2789
            {
2790
              cp_token *next_token;
2791
 
2792
              next_token = cp_lexer_peek_token (parser->lexer);
2793
              if (/* The comma at the end of an
2794
                     enumerator-definition.  */
2795
                  next_token->type != CPP_COMMA
2796
                  /* The curly brace at the end of an enum-specifier.  */
2797
                  && next_token->type != CPP_CLOSE_BRACE
2798
                  /* The end of a statement.  */
2799
                  && next_token->type != CPP_SEMICOLON
2800
                  /* The end of the cast-expression.  */
2801
                  && next_token->type != CPP_CLOSE_PAREN
2802
                  /* The end of an array bound.  */
2803
                  && next_token->type != CPP_CLOSE_SQUARE
2804
                  /* The closing ">" in a template-argument-list.  */
2805
                  && (next_token->type != CPP_GREATER
2806
                      || parser->greater_than_is_operator_p))
2807
                cast_p = false;
2808
            }
2809
 
2810
          /* If we are within a cast, then the constraint that the
2811
             cast is to an integral or enumeration type will be
2812
             checked at that point.  If we are not within a cast, then
2813
             this code is invalid.  */
2814
          if (!cast_p)
2815
            cp_parser_non_integral_constant_expression
2816
              (parser, "floating-point literal");
2817
        }
2818
      return token->value;
2819
 
2820
    case CPP_STRING:
2821
    case CPP_WSTRING:
2822
      /* ??? Should wide strings be allowed when parser->translate_strings_p
2823
         is false (i.e. in attributes)?  If not, we can kill the third
2824
         argument to cp_parser_string_literal.  */
2825
      return cp_parser_string_literal (parser,
2826
                                       parser->translate_strings_p,
2827
                                       true);
2828
 
2829
    case CPP_OPEN_PAREN:
2830
      {
2831
        tree expr;
2832
        bool saved_greater_than_is_operator_p;
2833
 
2834
        /* Consume the `('.  */
2835
        cp_lexer_consume_token (parser->lexer);
2836
        /* Within a parenthesized expression, a `>' token is always
2837
           the greater-than operator.  */
2838
        saved_greater_than_is_operator_p
2839
          = parser->greater_than_is_operator_p;
2840
        parser->greater_than_is_operator_p = true;
2841
        /* If we see `( { ' then we are looking at the beginning of
2842
           a GNU statement-expression.  */
2843
        if (cp_parser_allow_gnu_extensions_p (parser)
2844
            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2845
          {
2846
            /* Statement-expressions are not allowed by the standard.  */
2847
            if (pedantic)
2848
              pedwarn ("ISO C++ forbids braced-groups within expressions");
2849
 
2850
            /* And they're not allowed outside of a function-body; you
2851
               cannot, for example, write:
2852
 
2853
                 int i = ({ int j = 3; j + 1; });
2854
 
2855
               at class or namespace scope.  */
2856
            if (!at_function_scope_p ())
2857
              error ("statement-expressions are allowed only inside functions");
2858
            /* Start the statement-expression.  */
2859
            expr = begin_stmt_expr ();
2860
            /* Parse the compound-statement.  */
2861
            cp_parser_compound_statement (parser, expr, false);
2862
            /* Finish up.  */
2863
            expr = finish_stmt_expr (expr, false);
2864
          }
2865
        else
2866
          {
2867
            /* Parse the parenthesized expression.  */
2868
            expr = cp_parser_expression (parser, cast_p);
2869
            /* Let the front end know that this expression was
2870
               enclosed in parentheses. This matters in case, for
2871
               example, the expression is of the form `A::B', since
2872
               `&A::B' might be a pointer-to-member, but `&(A::B)' is
2873
               not.  */
2874
            finish_parenthesized_expr (expr);
2875
          }
2876
        /* The `>' token might be the end of a template-id or
2877
           template-parameter-list now.  */
2878
        parser->greater_than_is_operator_p
2879
          = saved_greater_than_is_operator_p;
2880
        /* Consume the `)'.  */
2881
        if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2882
          cp_parser_skip_to_end_of_statement (parser);
2883
 
2884
        return expr;
2885
      }
2886
 
2887
    case CPP_KEYWORD:
2888
      switch (token->keyword)
2889
        {
2890
          /* These two are the boolean literals.  */
2891
        case RID_TRUE:
2892
          cp_lexer_consume_token (parser->lexer);
2893
          return boolean_true_node;
2894
        case RID_FALSE:
2895
          cp_lexer_consume_token (parser->lexer);
2896
          return boolean_false_node;
2897
 
2898
          /* The `__null' literal.  */
2899
        case RID_NULL:
2900
          cp_lexer_consume_token (parser->lexer);
2901
          return null_node;
2902
 
2903
          /* Recognize the `this' keyword.  */
2904
        case RID_THIS:
2905
          cp_lexer_consume_token (parser->lexer);
2906
          if (parser->local_variables_forbidden_p)
2907
            {
2908
              error ("%<this%> may not be used in this context");
2909
              return error_mark_node;
2910
            }
2911
          /* Pointers cannot appear in constant-expressions.  */
2912
          if (cp_parser_non_integral_constant_expression (parser,
2913
                                                          "`this'"))
2914
            return error_mark_node;
2915
          return finish_this_expr ();
2916
 
2917
          /* The `operator' keyword can be the beginning of an
2918
             id-expression.  */
2919
        case RID_OPERATOR:
2920
          goto id_expression;
2921
 
2922
        case RID_FUNCTION_NAME:
2923
        case RID_PRETTY_FUNCTION_NAME:
2924
        case RID_C99_FUNCTION_NAME:
2925
          /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2926
             __func__ are the names of variables -- but they are
2927
             treated specially.  Therefore, they are handled here,
2928
             rather than relying on the generic id-expression logic
2929
             below.  Grammatically, these names are id-expressions.
2930
 
2931
             Consume the token.  */
2932
          token = cp_lexer_consume_token (parser->lexer);
2933
          /* Look up the name.  */
2934
          return finish_fname (token->value);
2935
 
2936
        case RID_VA_ARG:
2937
          {
2938
            tree expression;
2939
            tree type;
2940
 
2941
            /* The `__builtin_va_arg' construct is used to handle
2942
               `va_arg'.  Consume the `__builtin_va_arg' token.  */
2943
            cp_lexer_consume_token (parser->lexer);
2944
            /* Look for the opening `('.  */
2945
            cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2946
            /* Now, parse the assignment-expression.  */
2947
            expression = cp_parser_assignment_expression (parser,
2948
                                                          /*cast_p=*/false);
2949
            /* Look for the `,'.  */
2950
            cp_parser_require (parser, CPP_COMMA, "`,'");
2951
            /* Parse the type-id.  */
2952
            type = cp_parser_type_id (parser);
2953
            /* Look for the closing `)'.  */
2954
            cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2955
            /* Using `va_arg' in a constant-expression is not
2956
               allowed.  */
2957
            if (cp_parser_non_integral_constant_expression (parser,
2958
                                                            "`va_arg'"))
2959
              return error_mark_node;
2960
            return build_x_va_arg (expression, type);
2961
          }
2962
 
2963
        case RID_OFFSETOF:
2964
          return cp_parser_builtin_offsetof (parser);
2965
 
2966
          /* Objective-C++ expressions.  */
2967
        case RID_AT_ENCODE:
2968
        case RID_AT_PROTOCOL:
2969
        case RID_AT_SELECTOR:
2970
          return cp_parser_objc_expression (parser);
2971
 
2972
        default:
2973
          cp_parser_error (parser, "expected primary-expression");
2974
          return error_mark_node;
2975
        }
2976
 
2977
      /* An id-expression can start with either an identifier, a
2978
         `::' as the beginning of a qualified-id, or the "operator"
2979
         keyword.  */
2980
    case CPP_NAME:
2981
    case CPP_SCOPE:
2982
    case CPP_TEMPLATE_ID:
2983
    case CPP_NESTED_NAME_SPECIFIER:
2984
      {
2985
        tree id_expression;
2986
        tree decl;
2987
        const char *error_msg;
2988
        bool template_p;
2989
        bool done;
2990
 
2991
      id_expression:
2992
        /* Parse the id-expression.  */
2993
        id_expression
2994
          = cp_parser_id_expression (parser,
2995
                                     /*template_keyword_p=*/false,
2996
                                     /*check_dependency_p=*/true,
2997
                                     &template_p,
2998
                                     /*declarator_p=*/false);
2999
        if (id_expression == error_mark_node)
3000
          return error_mark_node;
3001
        token = cp_lexer_peek_token (parser->lexer);
3002
        done = (token->type != CPP_OPEN_SQUARE
3003
                && token->type != CPP_OPEN_PAREN
3004
                && token->type != CPP_DOT
3005
                && token->type != CPP_DEREF
3006
                && token->type != CPP_PLUS_PLUS
3007
                && token->type != CPP_MINUS_MINUS);
3008
        /* If we have a template-id, then no further lookup is
3009
           required.  If the template-id was for a template-class, we
3010
           will sometimes have a TYPE_DECL at this point.  */
3011
        if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3012
                 || TREE_CODE (id_expression) == TYPE_DECL)
3013
          decl = id_expression;
3014
        /* Look up the name.  */
3015
        else
3016
          {
3017
            tree ambiguous_decls;
3018
 
3019
            decl = cp_parser_lookup_name (parser, id_expression,
3020
                                          none_type,
3021
                                          template_p,
3022
                                          /*is_namespace=*/false,
3023
                                          /*check_dependency=*/true,
3024
                                          &ambiguous_decls);
3025
            /* If the lookup was ambiguous, an error will already have
3026
               been issued.  */
3027
            if (ambiguous_decls)
3028
              return error_mark_node;
3029
 
3030
            /* In Objective-C++, an instance variable (ivar) may be preferred
3031
               to whatever cp_parser_lookup_name() found.  */
3032
            decl = objc_lookup_ivar (decl, id_expression);
3033
 
3034
            /* If name lookup gives us a SCOPE_REF, then the
3035
               qualifying scope was dependent.  */
3036
            if (TREE_CODE (decl) == SCOPE_REF)
3037
              return decl;
3038
            /* Check to see if DECL is a local variable in a context
3039
               where that is forbidden.  */
3040
            if (parser->local_variables_forbidden_p
3041
                && local_variable_p (decl))
3042
              {
3043
                /* It might be that we only found DECL because we are
3044
                   trying to be generous with pre-ISO scoping rules.
3045
                   For example, consider:
3046
 
3047
                     int i;
3048
                     void g() {
3049
                       for (int i = 0; i < 10; ++i) {}
3050
                       extern void f(int j = i);
3051
                     }
3052
 
3053
                   Here, name look up will originally find the out
3054
                   of scope `i'.  We need to issue a warning message,
3055
                   but then use the global `i'.  */
3056
                decl = check_for_out_of_scope_variable (decl);
3057
                if (local_variable_p (decl))
3058
                  {
3059
                    error ("local variable %qD may not appear in this context",
3060
                           decl);
3061
                    return error_mark_node;
3062
                  }
3063
              }
3064
          }
3065
 
3066
        decl = (finish_id_expression
3067
                (id_expression, decl, parser->scope,
3068
                 idk,
3069
                 parser->integral_constant_expression_p,
3070
                 parser->allow_non_integral_constant_expression_p,
3071
                 &parser->non_integral_constant_expression_p,
3072
                 template_p, done, address_p,
3073
                 template_arg_p,
3074
                 &error_msg));
3075
        if (error_msg)
3076
          cp_parser_error (parser, error_msg);
3077
        return decl;
3078
      }
3079
 
3080
      /* Anything else is an error.  */
3081
    default:
3082
      /* ...unless we have an Objective-C++ message or string literal, that is.  */
3083
      if (c_dialect_objc ()
3084
          && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3085
        return cp_parser_objc_expression (parser);
3086
 
3087
      cp_parser_error (parser, "expected primary-expression");
3088
      return error_mark_node;
3089
    }
3090
}
3091
 
3092
/* Parse an id-expression.
3093
 
3094
   id-expression:
3095
     unqualified-id
3096
     qualified-id
3097
 
3098
   qualified-id:
3099
     :: [opt] nested-name-specifier template [opt] unqualified-id
3100
     :: identifier
3101
     :: operator-function-id
3102
     :: template-id
3103
 
3104
   Return a representation of the unqualified portion of the
3105
   identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3106
   a `::' or nested-name-specifier.
3107
 
3108
   Often, if the id-expression was a qualified-id, the caller will
3109
   want to make a SCOPE_REF to represent the qualified-id.  This
3110
   function does not do this in order to avoid wastefully creating
3111
   SCOPE_REFs when they are not required.
3112
 
3113
   If TEMPLATE_KEYWORD_P is true, then we have just seen the
3114
   `template' keyword.
3115
 
3116
   If CHECK_DEPENDENCY_P is false, then names are looked up inside
3117
   uninstantiated templates.
3118
 
3119
   If *TEMPLATE_P is non-NULL, it is set to true iff the
3120
   `template' keyword is used to explicitly indicate that the entity
3121
   named is a template.
3122
 
3123
   If DECLARATOR_P is true, the id-expression is appearing as part of
3124
   a declarator, rather than as part of an expression.  */
3125
 
3126
static tree
3127
cp_parser_id_expression (cp_parser *parser,
3128
                         bool template_keyword_p,
3129
                         bool check_dependency_p,
3130
                         bool *template_p,
3131
                         bool declarator_p)
3132
{
3133
  bool global_scope_p;
3134
  bool nested_name_specifier_p;
3135
 
3136
  /* Assume the `template' keyword was not used.  */
3137
  if (template_p)
3138
    *template_p = template_keyword_p;
3139
 
3140
  /* Look for the optional `::' operator.  */
3141
  global_scope_p
3142
    = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3143
       != NULL_TREE);
3144
  /* Look for the optional nested-name-specifier.  */
3145
  nested_name_specifier_p
3146
    = (cp_parser_nested_name_specifier_opt (parser,
3147
                                            /*typename_keyword_p=*/false,
3148
                                            check_dependency_p,
3149
                                            /*type_p=*/false,
3150
                                            declarator_p)
3151
       != NULL_TREE);
3152
  /* If there is a nested-name-specifier, then we are looking at
3153
     the first qualified-id production.  */
3154
  if (nested_name_specifier_p)
3155
    {
3156
      tree saved_scope;
3157
      tree saved_object_scope;
3158
      tree saved_qualifying_scope;
3159
      tree unqualified_id;
3160
      bool is_template;
3161
 
3162
      /* See if the next token is the `template' keyword.  */
3163
      if (!template_p)
3164
        template_p = &is_template;
3165
      *template_p = cp_parser_optional_template_keyword (parser);
3166
      /* Name lookup we do during the processing of the
3167
         unqualified-id might obliterate SCOPE.  */
3168
      saved_scope = parser->scope;
3169
      saved_object_scope = parser->object_scope;
3170
      saved_qualifying_scope = parser->qualifying_scope;
3171
      /* Process the final unqualified-id.  */
3172
      unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3173
                                                 check_dependency_p,
3174
                                                 declarator_p);
3175
      /* Restore the SAVED_SCOPE for our caller.  */
3176
      parser->scope = saved_scope;
3177
      parser->object_scope = saved_object_scope;
3178
      parser->qualifying_scope = saved_qualifying_scope;
3179
 
3180
      return unqualified_id;
3181
    }
3182
  /* Otherwise, if we are in global scope, then we are looking at one
3183
     of the other qualified-id productions.  */
3184
  else if (global_scope_p)
3185
    {
3186
      cp_token *token;
3187
      tree id;
3188
 
3189
      /* Peek at the next token.  */
3190
      token = cp_lexer_peek_token (parser->lexer);
3191
 
3192
      /* If it's an identifier, and the next token is not a "<", then
3193
         we can avoid the template-id case.  This is an optimization
3194
         for this common case.  */
3195
      if (token->type == CPP_NAME
3196
          && !cp_parser_nth_token_starts_template_argument_list_p
3197
               (parser, 2))
3198
        return cp_parser_identifier (parser);
3199
 
3200
      cp_parser_parse_tentatively (parser);
3201
      /* Try a template-id.  */
3202
      id = cp_parser_template_id (parser,
3203
                                  /*template_keyword_p=*/false,
3204
                                  /*check_dependency_p=*/true,
3205
                                  declarator_p);
3206
      /* If that worked, we're done.  */
3207
      if (cp_parser_parse_definitely (parser))
3208
        return id;
3209
 
3210
      /* Peek at the next token.  (Changes in the token buffer may
3211
         have invalidated the pointer obtained above.)  */
3212
      token = cp_lexer_peek_token (parser->lexer);
3213
 
3214
      switch (token->type)
3215
        {
3216
        case CPP_NAME:
3217
          return cp_parser_identifier (parser);
3218
 
3219
        case CPP_KEYWORD:
3220
          if (token->keyword == RID_OPERATOR)
3221
            return cp_parser_operator_function_id (parser);
3222
          /* Fall through.  */
3223
 
3224
        default:
3225
          cp_parser_error (parser, "expected id-expression");
3226
          return error_mark_node;
3227
        }
3228
    }
3229
  else
3230
    return cp_parser_unqualified_id (parser, template_keyword_p,
3231
                                     /*check_dependency_p=*/true,
3232
                                     declarator_p);
3233
}
3234
 
3235
/* Parse an unqualified-id.
3236
 
3237
   unqualified-id:
3238
     identifier
3239
     operator-function-id
3240
     conversion-function-id
3241
     ~ class-name
3242
     template-id
3243
 
3244
   If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3245
   keyword, in a construct like `A::template ...'.
3246
 
3247
   Returns a representation of unqualified-id.  For the `identifier'
3248
   production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3249
   production a BIT_NOT_EXPR is returned; the operand of the
3250
   BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3251
   other productions, see the documentation accompanying the
3252
   corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3253
   names are looked up in uninstantiated templates.  If DECLARATOR_P
3254
   is true, the unqualified-id is appearing as part of a declarator,
3255
   rather than as part of an expression.  */
3256
 
3257
static tree
3258
cp_parser_unqualified_id (cp_parser* parser,
3259
                          bool template_keyword_p,
3260
                          bool check_dependency_p,
3261
                          bool declarator_p)
3262
{
3263
  cp_token *token;
3264
 
3265
  /* Peek at the next token.  */
3266
  token = cp_lexer_peek_token (parser->lexer);
3267
 
3268
  switch (token->type)
3269
    {
3270
    case CPP_NAME:
3271
      {
3272
        tree id;
3273
 
3274
        /* We don't know yet whether or not this will be a
3275
           template-id.  */
3276
        cp_parser_parse_tentatively (parser);
3277
        /* Try a template-id.  */
3278
        id = cp_parser_template_id (parser, template_keyword_p,
3279
                                    check_dependency_p,
3280
                                    declarator_p);
3281
        /* If it worked, we're done.  */
3282
        if (cp_parser_parse_definitely (parser))
3283
          return id;
3284
        /* Otherwise, it's an ordinary identifier.  */
3285
        return cp_parser_identifier (parser);
3286
      }
3287
 
3288
    case CPP_TEMPLATE_ID:
3289
      return cp_parser_template_id (parser, template_keyword_p,
3290
                                    check_dependency_p,
3291
                                    declarator_p);
3292
 
3293
    case CPP_COMPL:
3294
      {
3295
        tree type_decl;
3296
        tree qualifying_scope;
3297
        tree object_scope;
3298
        tree scope;
3299
        bool done;
3300
 
3301
        /* Consume the `~' token.  */
3302
        cp_lexer_consume_token (parser->lexer);
3303
        /* Parse the class-name.  The standard, as written, seems to
3304
           say that:
3305
 
3306
             template <typename T> struct S { ~S (); };
3307
             template <typename T> S<T>::~S() {}
3308
 
3309
           is invalid, since `~' must be followed by a class-name, but
3310
           `S<T>' is dependent, and so not known to be a class.
3311
           That's not right; we need to look in uninstantiated
3312
           templates.  A further complication arises from:
3313
 
3314
             template <typename T> void f(T t) {
3315
               t.T::~T();
3316
             }
3317
 
3318
           Here, it is not possible to look up `T' in the scope of `T'
3319
           itself.  We must look in both the current scope, and the
3320
           scope of the containing complete expression.
3321
 
3322
           Yet another issue is:
3323
 
3324
             struct S {
3325
               int S;
3326
               ~S();
3327
             };
3328
 
3329
             S::~S() {}
3330
 
3331
           The standard does not seem to say that the `S' in `~S'
3332
           should refer to the type `S' and not the data member
3333
           `S::S'.  */
3334
 
3335
        /* DR 244 says that we look up the name after the "~" in the
3336
           same scope as we looked up the qualifying name.  That idea
3337
           isn't fully worked out; it's more complicated than that.  */
3338
        scope = parser->scope;
3339
        object_scope = parser->object_scope;
3340
        qualifying_scope = parser->qualifying_scope;
3341
 
3342
        /* If the name is of the form "X::~X" it's OK.  */
3343
        if (scope && TYPE_P (scope)
3344
            && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3345
            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3346
                == CPP_OPEN_PAREN)
3347
            && (cp_lexer_peek_token (parser->lexer)->value
3348
                == TYPE_IDENTIFIER (scope)))
3349
          {
3350
            cp_lexer_consume_token (parser->lexer);
3351
            return build_nt (BIT_NOT_EXPR, scope);
3352
          }
3353
 
3354
        /* If there was an explicit qualification (S::~T), first look
3355
           in the scope given by the qualification (i.e., S).  */
3356
        done = false;
3357
        type_decl = NULL_TREE;
3358
        if (scope)
3359
          {
3360
            cp_parser_parse_tentatively (parser);
3361
            type_decl = cp_parser_class_name (parser,
3362
                                              /*typename_keyword_p=*/false,
3363
                                              /*template_keyword_p=*/false,
3364
                                              none_type,
3365
                                              /*check_dependency=*/false,
3366
                                              /*class_head_p=*/false,
3367
                                              declarator_p);
3368
            if (cp_parser_parse_definitely (parser))
3369
              done = true;
3370
          }
3371
        /* In "N::S::~S", look in "N" as well.  */
3372
        if (!done && scope && qualifying_scope)
3373
          {
3374
            cp_parser_parse_tentatively (parser);
3375
            parser->scope = qualifying_scope;
3376
            parser->object_scope = NULL_TREE;
3377
            parser->qualifying_scope = NULL_TREE;
3378
            type_decl
3379
              = cp_parser_class_name (parser,
3380
                                      /*typename_keyword_p=*/false,
3381
                                      /*template_keyword_p=*/false,
3382
                                      none_type,
3383
                                      /*check_dependency=*/false,
3384
                                      /*class_head_p=*/false,
3385
                                      declarator_p);
3386
            if (cp_parser_parse_definitely (parser))
3387
              done = true;
3388
          }
3389
        /* In "p->S::~T", look in the scope given by "*p" as well.  */
3390
        else if (!done && object_scope)
3391
          {
3392
            cp_parser_parse_tentatively (parser);
3393
            parser->scope = object_scope;
3394
            parser->object_scope = NULL_TREE;
3395
            parser->qualifying_scope = NULL_TREE;
3396
            type_decl
3397
              = cp_parser_class_name (parser,
3398
                                      /*typename_keyword_p=*/false,
3399
                                      /*template_keyword_p=*/false,
3400
                                      none_type,
3401
                                      /*check_dependency=*/false,
3402
                                      /*class_head_p=*/false,
3403
                                      declarator_p);
3404
            if (cp_parser_parse_definitely (parser))
3405
              done = true;
3406
          }
3407
        /* Look in the surrounding context.  */
3408
        if (!done)
3409
          {
3410
            parser->scope = NULL_TREE;
3411
            parser->object_scope = NULL_TREE;
3412
            parser->qualifying_scope = NULL_TREE;
3413
            type_decl
3414
              = cp_parser_class_name (parser,
3415
                                      /*typename_keyword_p=*/false,
3416
                                      /*template_keyword_p=*/false,
3417
                                      none_type,
3418
                                      /*check_dependency=*/false,
3419
                                      /*class_head_p=*/false,
3420
                                      declarator_p);
3421
          }
3422
        /* If an error occurred, assume that the name of the
3423
           destructor is the same as the name of the qualifying
3424
           class.  That allows us to keep parsing after running
3425
           into ill-formed destructor names.  */
3426
        if (type_decl == error_mark_node && scope && TYPE_P (scope))
3427
          return build_nt (BIT_NOT_EXPR, scope);
3428
        else if (type_decl == error_mark_node)
3429
          return error_mark_node;
3430
 
3431
        /* Check that destructor name and scope match.  */
3432
        if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3433
          {
3434
            if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3435
              error ("declaration of %<~%T%> as member of %qT",
3436
                     type_decl, scope);
3437
            return error_mark_node;
3438
          }
3439
 
3440
        /* [class.dtor]
3441
 
3442
           A typedef-name that names a class shall not be used as the
3443
           identifier in the declarator for a destructor declaration.  */
3444
        if (declarator_p
3445
            && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3446
            && !DECL_SELF_REFERENCE_P (type_decl)
3447
            && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3448
          error ("typedef-name %qD used as destructor declarator",
3449
                 type_decl);
3450
 
3451
        return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3452
      }
3453
 
3454
    case CPP_KEYWORD:
3455
      if (token->keyword == RID_OPERATOR)
3456
        {
3457
          tree id;
3458
 
3459
          /* This could be a template-id, so we try that first.  */
3460
          cp_parser_parse_tentatively (parser);
3461
          /* Try a template-id.  */
3462
          id = cp_parser_template_id (parser, template_keyword_p,
3463
                                      /*check_dependency_p=*/true,
3464
                                      declarator_p);
3465
          /* If that worked, we're done.  */
3466
          if (cp_parser_parse_definitely (parser))
3467
            return id;
3468
          /* We still don't know whether we're looking at an
3469
             operator-function-id or a conversion-function-id.  */
3470
          cp_parser_parse_tentatively (parser);
3471
          /* Try an operator-function-id.  */
3472
          id = cp_parser_operator_function_id (parser);
3473
          /* If that didn't work, try a conversion-function-id.  */
3474
          if (!cp_parser_parse_definitely (parser))
3475
            id = cp_parser_conversion_function_id (parser);
3476
 
3477
          return id;
3478
        }
3479
      /* Fall through.  */
3480
 
3481
    default:
3482
      cp_parser_error (parser, "expected unqualified-id");
3483
      return error_mark_node;
3484
    }
3485
}
3486
 
3487
/* Parse an (optional) nested-name-specifier.
3488
 
3489
   nested-name-specifier:
3490
     class-or-namespace-name :: nested-name-specifier [opt]
3491
     class-or-namespace-name :: template nested-name-specifier [opt]
3492
 
3493
   PARSER->SCOPE should be set appropriately before this function is
3494
   called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3495
   effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3496
   in name lookups.
3497
 
3498
   Sets PARSER->SCOPE to the class (TYPE) or namespace
3499
   (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3500
   it unchanged if there is no nested-name-specifier.  Returns the new
3501
   scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3502
 
3503
   If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3504
   part of a declaration and/or decl-specifier.  */
3505
 
3506
static tree
3507
cp_parser_nested_name_specifier_opt (cp_parser *parser,
3508
                                     bool typename_keyword_p,
3509
                                     bool check_dependency_p,
3510
                                     bool type_p,
3511
                                     bool is_declaration)
3512
{
3513
  bool success = false;
3514
  cp_token_position start = 0;
3515
  cp_token *token;
3516
 
3517
  /* If the next token corresponds to a nested name specifier, there
3518
     is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3519
     false, it may have been true before, in which case something
3520
     like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3521
     of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3522
     CHECK_DEPENDENCY_P is false, we have to fall through into the
3523
     main loop.  */
3524
  if (check_dependency_p
3525
      && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3526
    {
3527
      cp_parser_pre_parsed_nested_name_specifier (parser);
3528
      return parser->scope;
3529
    }
3530
 
3531
  /* Remember where the nested-name-specifier starts.  */
3532
  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3533
    {
3534
      start = cp_lexer_token_position (parser->lexer, false);
3535
      push_deferring_access_checks (dk_deferred);
3536
    }
3537
 
3538
  while (true)
3539
    {
3540
      tree new_scope;
3541
      tree old_scope;
3542
      tree saved_qualifying_scope;
3543
      bool template_keyword_p;
3544
 
3545
      /* Spot cases that cannot be the beginning of a
3546
         nested-name-specifier.  */
3547
      token = cp_lexer_peek_token (parser->lexer);
3548
 
3549
      /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3550
         the already parsed nested-name-specifier.  */
3551
      if (token->type == CPP_NESTED_NAME_SPECIFIER)
3552
        {
3553
          /* Grab the nested-name-specifier and continue the loop.  */
3554
          cp_parser_pre_parsed_nested_name_specifier (parser);
3555
          success = true;
3556
          continue;
3557
        }
3558
 
3559
      /* Spot cases that cannot be the beginning of a
3560
         nested-name-specifier.  On the second and subsequent times
3561
         through the loop, we look for the `template' keyword.  */
3562
      if (success && token->keyword == RID_TEMPLATE)
3563
        ;
3564
      /* A template-id can start a nested-name-specifier.  */
3565
      else if (token->type == CPP_TEMPLATE_ID)
3566
        ;
3567
      else
3568
        {
3569
          /* If the next token is not an identifier, then it is
3570
             definitely not a class-or-namespace-name.  */
3571
          if (token->type != CPP_NAME)
3572
            break;
3573
          /* If the following token is neither a `<' (to begin a
3574
             template-id), nor a `::', then we are not looking at a
3575
             nested-name-specifier.  */
3576
          token = cp_lexer_peek_nth_token (parser->lexer, 2);
3577
          if (token->type != CPP_SCOPE
3578
              && !cp_parser_nth_token_starts_template_argument_list_p
3579
                  (parser, 2))
3580
            break;
3581
        }
3582
 
3583
      /* The nested-name-specifier is optional, so we parse
3584
         tentatively.  */
3585
      cp_parser_parse_tentatively (parser);
3586
 
3587
      /* Look for the optional `template' keyword, if this isn't the
3588
         first time through the loop.  */
3589
      if (success)
3590
        template_keyword_p = cp_parser_optional_template_keyword (parser);
3591
      else
3592
        template_keyword_p = false;
3593
 
3594
      /* Save the old scope since the name lookup we are about to do
3595
         might destroy it.  */
3596
      old_scope = parser->scope;
3597
      saved_qualifying_scope = parser->qualifying_scope;
3598
      /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3599
         look up names in "X<T>::I" in order to determine that "Y" is
3600
         a template.  So, if we have a typename at this point, we make
3601
         an effort to look through it.  */
3602
      if (is_declaration
3603
          && !typename_keyword_p
3604
          && parser->scope
3605
          && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3606
        parser->scope = resolve_typename_type (parser->scope,
3607
                                               /*only_current_p=*/false);
3608
      /* Parse the qualifying entity.  */
3609
      new_scope
3610
        = cp_parser_class_or_namespace_name (parser,
3611
                                             typename_keyword_p,
3612
                                             template_keyword_p,
3613
                                             check_dependency_p,
3614
                                             type_p,
3615
                                             is_declaration);
3616
      /* Look for the `::' token.  */
3617
      cp_parser_require (parser, CPP_SCOPE, "`::'");
3618
 
3619
      /* If we found what we wanted, we keep going; otherwise, we're
3620
         done.  */
3621
      if (!cp_parser_parse_definitely (parser))
3622
        {
3623
          bool error_p = false;
3624
 
3625
          /* Restore the OLD_SCOPE since it was valid before the
3626
             failed attempt at finding the last
3627
             class-or-namespace-name.  */
3628
          parser->scope = old_scope;
3629
          parser->qualifying_scope = saved_qualifying_scope;
3630
          /* If the next token is an identifier, and the one after
3631
             that is a `::', then any valid interpretation would have
3632
             found a class-or-namespace-name.  */
3633
          while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3634
                 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3635
                     == CPP_SCOPE)
3636
                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3637
                     != CPP_COMPL))
3638
            {
3639
              token = cp_lexer_consume_token (parser->lexer);
3640
              if (!error_p)
3641
                {
3642
                  if (!token->ambiguous_p)
3643
                    {
3644
                      tree decl;
3645
                      tree ambiguous_decls;
3646
 
3647
                      decl = cp_parser_lookup_name (parser, token->value,
3648
                                                    none_type,
3649
                                                    /*is_template=*/false,
3650
                                                    /*is_namespace=*/false,
3651
                                                    /*check_dependency=*/true,
3652
                                                    &ambiguous_decls);
3653
                      if (TREE_CODE (decl) == TEMPLATE_DECL)
3654
                        error ("%qD used without template parameters", decl);
3655
                      else if (ambiguous_decls)
3656
                        {
3657
                          error ("reference to %qD is ambiguous",
3658
                                 token->value);
3659
                          print_candidates (ambiguous_decls);
3660
                          decl = error_mark_node;
3661
                        }
3662
                      else
3663
                        cp_parser_name_lookup_error
3664
                          (parser, token->value, decl,
3665
                           "is not a class or namespace");
3666
                    }
3667
                  parser->scope = error_mark_node;
3668
                  error_p = true;
3669
                  /* Treat this as a successful nested-name-specifier
3670
                     due to:
3671
 
3672
                     [basic.lookup.qual]
3673
 
3674
                     If the name found is not a class-name (clause
3675
                     _class_) or namespace-name (_namespace.def_), the
3676
                     program is ill-formed.  */
3677
                  success = true;
3678
                }
3679
              cp_lexer_consume_token (parser->lexer);
3680
            }
3681
          break;
3682
        }
3683
      /* We've found one valid nested-name-specifier.  */
3684
      success = true;
3685
      /* Name lookup always gives us a DECL.  */
3686
      if (TREE_CODE (new_scope) == TYPE_DECL)
3687
        new_scope = TREE_TYPE (new_scope);
3688
      /* Uses of "template" must be followed by actual templates.  */
3689
      if (template_keyword_p
3690
          && !(CLASS_TYPE_P (new_scope)
3691
               && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3692
                    && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3693
                   || CLASSTYPE_IS_TEMPLATE (new_scope)))
3694
          && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3695
               && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3696
                   == TEMPLATE_ID_EXPR)))
3697
        pedwarn (TYPE_P (new_scope)
3698
                 ? "%qT is not a template"
3699
                 : "%qD is not a template",
3700
                 new_scope);
3701
      /* If it is a class scope, try to complete it; we are about to
3702
         be looking up names inside the class.  */
3703
      if (TYPE_P (new_scope)
3704
          /* Since checking types for dependency can be expensive,
3705
             avoid doing it if the type is already complete.  */
3706
          && !COMPLETE_TYPE_P (new_scope)
3707
          /* Do not try to complete dependent types.  */
3708
          && !dependent_type_p (new_scope))
3709
        new_scope = complete_type (new_scope);
3710
      /* Make sure we look in the right scope the next time through
3711
         the loop.  */
3712
      parser->scope = new_scope;
3713
    }
3714
 
3715
  /* If parsing tentatively, replace the sequence of tokens that makes
3716
     up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3717
     token.  That way, should we re-parse the token stream, we will
3718
     not have to repeat the effort required to do the parse, nor will
3719
     we issue duplicate error messages.  */
3720
  if (success && start)
3721
    {
3722
      cp_token *token;
3723
      tree access_checks;
3724
 
3725
      token = cp_lexer_token_at (parser->lexer, start);
3726
      /* Reset the contents of the START token.  */
3727
      token->type = CPP_NESTED_NAME_SPECIFIER;
3728
      /* Retrieve any deferred checks.  Do not pop this access checks yet
3729
         so the memory will not be reclaimed during token replacing below.  */
3730
      access_checks = get_deferred_access_checks ();
3731
      token->value = build_tree_list (copy_list (access_checks),
3732
                                      parser->scope);
3733
      TREE_TYPE (token->value) = parser->qualifying_scope;
3734
      token->keyword = RID_MAX;
3735
 
3736
      /* Purge all subsequent tokens.  */
3737
      cp_lexer_purge_tokens_after (parser->lexer, start);
3738
    }
3739
 
3740
  if (start)
3741
    pop_to_parent_deferring_access_checks ();
3742
 
3743
  return success ? parser->scope : NULL_TREE;
3744
}
3745
 
3746
/* Parse a nested-name-specifier.  See
3747
   cp_parser_nested_name_specifier_opt for details.  This function
3748
   behaves identically, except that it will an issue an error if no
3749
   nested-name-specifier is present.  */
3750
 
3751
static tree
3752
cp_parser_nested_name_specifier (cp_parser *parser,
3753
                                 bool typename_keyword_p,
3754
                                 bool check_dependency_p,
3755
                                 bool type_p,
3756
                                 bool is_declaration)
3757
{
3758
  tree scope;
3759
 
3760
  /* Look for the nested-name-specifier.  */
3761
  scope = cp_parser_nested_name_specifier_opt (parser,
3762
                                               typename_keyword_p,
3763
                                               check_dependency_p,
3764
                                               type_p,
3765
                                               is_declaration);
3766
  /* If it was not present, issue an error message.  */
3767
  if (!scope)
3768
    {
3769
      cp_parser_error (parser, "expected nested-name-specifier");
3770
      parser->scope = NULL_TREE;
3771
    }
3772
 
3773
  return scope;
3774
}
3775
 
3776
/* Parse a class-or-namespace-name.
3777
 
3778
   class-or-namespace-name:
3779
     class-name
3780
     namespace-name
3781
 
3782
   TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3783
   TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3784
   CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3785
   TYPE_P is TRUE iff the next name should be taken as a class-name,
3786
   even the same name is declared to be another entity in the same
3787
   scope.
3788
 
3789
   Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3790
   specified by the class-or-namespace-name.  If neither is found the
3791
   ERROR_MARK_NODE is returned.  */
3792
 
3793
static tree
3794
cp_parser_class_or_namespace_name (cp_parser *parser,
3795
                                   bool typename_keyword_p,
3796
                                   bool template_keyword_p,
3797
                                   bool check_dependency_p,
3798
                                   bool type_p,
3799
                                   bool is_declaration)
3800
{
3801
  tree saved_scope;
3802
  tree saved_qualifying_scope;
3803
  tree saved_object_scope;
3804
  tree scope;
3805
  bool only_class_p;
3806
 
3807
  /* Before we try to parse the class-name, we must save away the
3808
     current PARSER->SCOPE since cp_parser_class_name will destroy
3809
     it.  */
3810
  saved_scope = parser->scope;
3811
  saved_qualifying_scope = parser->qualifying_scope;
3812
  saved_object_scope = parser->object_scope;
3813
  /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3814
     there is no need to look for a namespace-name.  */
3815
  only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3816
  if (!only_class_p)
3817
    cp_parser_parse_tentatively (parser);
3818
  scope = cp_parser_class_name (parser,
3819
                                typename_keyword_p,
3820
                                template_keyword_p,
3821
                                type_p ? class_type : none_type,
3822
                                check_dependency_p,
3823
                                /*class_head_p=*/false,
3824
                                is_declaration);
3825
  /* If that didn't work, try for a namespace-name.  */
3826
  if (!only_class_p && !cp_parser_parse_definitely (parser))
3827
    {
3828
      /* Restore the saved scope.  */
3829
      parser->scope = saved_scope;
3830
      parser->qualifying_scope = saved_qualifying_scope;
3831
      parser->object_scope = saved_object_scope;
3832
      /* If we are not looking at an identifier followed by the scope
3833
         resolution operator, then this is not part of a
3834
         nested-name-specifier.  (Note that this function is only used
3835
         to parse the components of a nested-name-specifier.)  */
3836
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3837
          || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3838
        return error_mark_node;
3839
      scope = cp_parser_namespace_name (parser);
3840
    }
3841
 
3842
  return scope;
3843
}
3844
 
3845
/* Parse a postfix-expression.
3846
 
3847
   postfix-expression:
3848
     primary-expression
3849
     postfix-expression [ expression ]
3850
     postfix-expression ( expression-list [opt] )
3851
     simple-type-specifier ( expression-list [opt] )
3852
     typename :: [opt] nested-name-specifier identifier
3853
       ( expression-list [opt] )
3854
     typename :: [opt] nested-name-specifier template [opt] template-id
3855
       ( expression-list [opt] )
3856
     postfix-expression . template [opt] id-expression
3857
     postfix-expression -> template [opt] id-expression
3858
     postfix-expression . pseudo-destructor-name
3859
     postfix-expression -> pseudo-destructor-name
3860
     postfix-expression ++
3861
     postfix-expression --
3862
     dynamic_cast < type-id > ( expression )
3863
     static_cast < type-id > ( expression )
3864
     reinterpret_cast < type-id > ( expression )
3865
     const_cast < type-id > ( expression )
3866
     typeid ( expression )
3867
     typeid ( type-id )
3868
 
3869
   GNU Extension:
3870
 
3871
   postfix-expression:
3872
     ( type-id ) { initializer-list , [opt] }
3873
 
3874
   This extension is a GNU version of the C99 compound-literal
3875
   construct.  (The C99 grammar uses `type-name' instead of `type-id',
3876
   but they are essentially the same concept.)
3877
 
3878
   If ADDRESS_P is true, the postfix expression is the operand of the
3879
   `&' operator.  CAST_P is true if this expression is the target of a
3880
   cast.
3881
 
3882
   Returns a representation of the expression.  */
3883
 
3884
static tree
3885
cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3886
{
3887
  cp_token *token;
3888
  enum rid keyword;
3889
  cp_id_kind idk = CP_ID_KIND_NONE;
3890
  tree postfix_expression = NULL_TREE;
3891
 
3892
  /* Peek at the next token.  */
3893
  token = cp_lexer_peek_token (parser->lexer);
3894
  /* Some of the productions are determined by keywords.  */
3895
  keyword = token->keyword;
3896
  switch (keyword)
3897
    {
3898
    case RID_DYNCAST:
3899
    case RID_STATCAST:
3900
    case RID_REINTCAST:
3901
    case RID_CONSTCAST:
3902
      {
3903
        tree type;
3904
        tree expression;
3905
        const char *saved_message;
3906
 
3907
        /* All of these can be handled in the same way from the point
3908
           of view of parsing.  Begin by consuming the token
3909
           identifying the cast.  */
3910
        cp_lexer_consume_token (parser->lexer);
3911
 
3912
        /* New types cannot be defined in the cast.  */
3913
        saved_message = parser->type_definition_forbidden_message;
3914
        parser->type_definition_forbidden_message
3915
          = "types may not be defined in casts";
3916
 
3917
        /* Look for the opening `<'.  */
3918
        cp_parser_require (parser, CPP_LESS, "`<'");
3919
        /* Parse the type to which we are casting.  */
3920
        type = cp_parser_type_id (parser);
3921
        /* Look for the closing `>'.  */
3922
        cp_parser_require (parser, CPP_GREATER, "`>'");
3923
        /* Restore the old message.  */
3924
        parser->type_definition_forbidden_message = saved_message;
3925
 
3926
        /* And the expression which is being cast.  */
3927
        cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3928
        expression = cp_parser_expression (parser, /*cast_p=*/true);
3929
        cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3930
 
3931
        /* Only type conversions to integral or enumeration types
3932
           can be used in constant-expressions.  */
3933
        if (parser->integral_constant_expression_p
3934
            && !dependent_type_p (type)
3935
            && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3936
            && (cp_parser_non_integral_constant_expression
3937
                (parser,
3938
                 "a cast to a type other than an integral or "
3939
                 "enumeration type")))
3940
          return error_mark_node;
3941
 
3942
        switch (keyword)
3943
          {
3944
          case RID_DYNCAST:
3945
            postfix_expression
3946
              = build_dynamic_cast (type, expression);
3947
            break;
3948
          case RID_STATCAST:
3949
            postfix_expression
3950
              = build_static_cast (type, expression);
3951
            break;
3952
          case RID_REINTCAST:
3953
            postfix_expression
3954
              = build_reinterpret_cast (type, expression);
3955
            break;
3956
          case RID_CONSTCAST:
3957
            postfix_expression
3958
              = build_const_cast (type, expression);
3959
            break;
3960
          default:
3961
            gcc_unreachable ();
3962
          }
3963
      }
3964
      break;
3965
 
3966
    case RID_TYPEID:
3967
      {
3968
        tree type;
3969
        const char *saved_message;
3970
        bool saved_in_type_id_in_expr_p;
3971
 
3972
        /* Consume the `typeid' token.  */
3973
        cp_lexer_consume_token (parser->lexer);
3974
        /* Look for the `(' token.  */
3975
        cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3976
        /* Types cannot be defined in a `typeid' expression.  */
3977
        saved_message = parser->type_definition_forbidden_message;
3978
        parser->type_definition_forbidden_message
3979
          = "types may not be defined in a `typeid\' expression";
3980
        /* We can't be sure yet whether we're looking at a type-id or an
3981
           expression.  */
3982
        cp_parser_parse_tentatively (parser);
3983
        /* Try a type-id first.  */
3984
        saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3985
        parser->in_type_id_in_expr_p = true;
3986
        type = cp_parser_type_id (parser);
3987
        parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3988
        /* Look for the `)' token.  Otherwise, we can't be sure that
3989
           we're not looking at an expression: consider `typeid (int
3990
           (3))', for example.  */
3991
        cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3992
        /* If all went well, simply lookup the type-id.  */
3993
        if (cp_parser_parse_definitely (parser))
3994
          postfix_expression = get_typeid (type);
3995
        /* Otherwise, fall back to the expression variant.  */
3996
        else
3997
          {
3998
            tree expression;
3999
 
4000
            /* Look for an expression.  */
4001
            expression = cp_parser_expression (parser, /*cast_p=*/false);
4002
            /* Compute its typeid.  */
4003
            postfix_expression = build_typeid (expression);
4004
            /* Look for the `)' token.  */
4005
            cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4006
          }
4007
        /* `typeid' may not appear in an integral constant expression.  */
4008
        if (cp_parser_non_integral_constant_expression(parser,
4009
                                                       "`typeid' operator"))
4010
          return error_mark_node;
4011
        /* Restore the saved message.  */
4012
        parser->type_definition_forbidden_message = saved_message;
4013
      }
4014
      break;
4015
 
4016
    case RID_TYPENAME:
4017
      {
4018
        tree type;
4019
        /* The syntax permitted here is the same permitted for an
4020
           elaborated-type-specifier.  */
4021
        type = cp_parser_elaborated_type_specifier (parser,
4022
                                                    /*is_friend=*/false,
4023
                                                    /*is_declaration=*/false);
4024
        postfix_expression = cp_parser_functional_cast (parser, type);
4025
      }
4026
      break;
4027
 
4028
    default:
4029
      {
4030
        tree type;
4031
 
4032
        /* If the next thing is a simple-type-specifier, we may be
4033
           looking at a functional cast.  We could also be looking at
4034
           an id-expression.  So, we try the functional cast, and if
4035
           that doesn't work we fall back to the primary-expression.  */
4036
        cp_parser_parse_tentatively (parser);
4037
        /* Look for the simple-type-specifier.  */
4038
        type = cp_parser_simple_type_specifier (parser,
4039
                                                /*decl_specs=*/NULL,
4040
                                                CP_PARSER_FLAGS_NONE);
4041
        /* Parse the cast itself.  */
4042
        if (!cp_parser_error_occurred (parser))
4043
          postfix_expression
4044
            = cp_parser_functional_cast (parser, type);
4045
        /* If that worked, we're done.  */
4046
        if (cp_parser_parse_definitely (parser))
4047
          break;
4048
 
4049
        /* If the functional-cast didn't work out, try a
4050
           compound-literal.  */
4051
        if (cp_parser_allow_gnu_extensions_p (parser)
4052
            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4053
          {
4054
            VEC(constructor_elt,gc) *initializer_list = NULL;
4055
            bool saved_in_type_id_in_expr_p;
4056
 
4057
            cp_parser_parse_tentatively (parser);
4058
            /* Consume the `('.  */
4059
            cp_lexer_consume_token (parser->lexer);
4060
            /* Parse the type.  */
4061
            saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4062
            parser->in_type_id_in_expr_p = true;
4063
            type = cp_parser_type_id (parser);
4064
            parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4065
            /* Look for the `)'.  */
4066
            cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4067
            /* Look for the `{'.  */
4068
            cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4069
            /* If things aren't going well, there's no need to
4070
               keep going.  */
4071
            if (!cp_parser_error_occurred (parser))
4072
              {
4073
                bool non_constant_p;
4074
                /* Parse the initializer-list.  */
4075
                initializer_list
4076
                  = cp_parser_initializer_list (parser, &non_constant_p);
4077
                /* Allow a trailing `,'.  */
4078
                if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4079
                  cp_lexer_consume_token (parser->lexer);
4080
                /* Look for the final `}'.  */
4081
                cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4082
              }
4083
            /* If that worked, we're definitely looking at a
4084
               compound-literal expression.  */
4085
            if (cp_parser_parse_definitely (parser))
4086
              {
4087
                /* Warn the user that a compound literal is not
4088
                   allowed in standard C++.  */
4089
                if (pedantic)
4090
                  pedwarn ("ISO C++ forbids compound-literals");
4091
                /* Form the representation of the compound-literal.  */
4092
                postfix_expression
4093
                  = finish_compound_literal (type, initializer_list);
4094
                break;
4095
              }
4096
          }
4097
 
4098
        /* It must be a primary-expression.  */
4099
        postfix_expression
4100
          = cp_parser_primary_expression (parser, address_p, cast_p,
4101
                                          /*template_arg_p=*/false,
4102
                                          &idk);
4103
      }
4104
      break;
4105
    }
4106
 
4107
  /* Keep looping until the postfix-expression is complete.  */
4108
  while (true)
4109
    {
4110
      if (idk == CP_ID_KIND_UNQUALIFIED
4111
          && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4112
          && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4113
        /* It is not a Koenig lookup function call.  */
4114
        postfix_expression
4115
          = unqualified_name_lookup_error (postfix_expression);
4116
 
4117
      /* Peek at the next token.  */
4118
      token = cp_lexer_peek_token (parser->lexer);
4119
 
4120
      switch (token->type)
4121
        {
4122
        case CPP_OPEN_SQUARE:
4123
          postfix_expression
4124
            = cp_parser_postfix_open_square_expression (parser,
4125
                                                        postfix_expression,
4126
                                                        false);
4127
          idk = CP_ID_KIND_NONE;
4128
          break;
4129
 
4130
        case CPP_OPEN_PAREN:
4131
          /* postfix-expression ( expression-list [opt] ) */
4132
          {
4133
            bool koenig_p;
4134
            bool is_builtin_constant_p;
4135
            bool saved_integral_constant_expression_p = false;
4136
            bool saved_non_integral_constant_expression_p = false;
4137
            tree args;
4138
 
4139
            is_builtin_constant_p
4140
              = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4141
            if (is_builtin_constant_p)
4142
              {
4143
                /* The whole point of __builtin_constant_p is to allow
4144
                   non-constant expressions to appear as arguments.  */
4145
                saved_integral_constant_expression_p
4146
                  = parser->integral_constant_expression_p;
4147
                saved_non_integral_constant_expression_p
4148
                  = parser->non_integral_constant_expression_p;
4149
                parser->integral_constant_expression_p = false;
4150
              }
4151
            args = (cp_parser_parenthesized_expression_list
4152
                    (parser, /*is_attribute_list=*/false,
4153
                     /*cast_p=*/false,
4154
                     /*non_constant_p=*/NULL));
4155
            if (is_builtin_constant_p)
4156
              {
4157
                parser->integral_constant_expression_p
4158
                  = saved_integral_constant_expression_p;
4159
                parser->non_integral_constant_expression_p
4160
                  = saved_non_integral_constant_expression_p;
4161
              }
4162
 
4163
            if (args == error_mark_node)
4164
              {
4165
                postfix_expression = error_mark_node;
4166
                break;
4167
              }
4168
 
4169
            /* Function calls are not permitted in
4170
               constant-expressions.  */
4171
            if (! builtin_valid_in_constant_expr_p (postfix_expression)
4172
                && cp_parser_non_integral_constant_expression (parser,
4173
                                                               "a function call"))
4174
              {
4175
                postfix_expression = error_mark_node;
4176
                break;
4177
              }
4178
 
4179
            koenig_p = false;
4180
            if (idk == CP_ID_KIND_UNQUALIFIED)
4181
              {
4182
                if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4183
                  {
4184
                    if (args)
4185
                      {
4186
                        koenig_p = true;
4187
                        postfix_expression
4188
                          = perform_koenig_lookup (postfix_expression, args);
4189
                      }
4190
                    else
4191
                      postfix_expression
4192
                        = unqualified_fn_lookup_error (postfix_expression);
4193
                  }
4194
                /* We do not perform argument-dependent lookup if
4195
                   normal lookup finds a non-function, in accordance
4196
                   with the expected resolution of DR 218.  */
4197
                else if (args && is_overloaded_fn (postfix_expression))
4198
                  {
4199
                    tree fn = get_first_fn (postfix_expression);
4200
 
4201
                    if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4202
                      fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4203
 
4204
                    /* Only do argument dependent lookup if regular
4205
                       lookup does not find a set of member functions.
4206
                       [basic.lookup.koenig]/2a  */
4207
                    if (!DECL_FUNCTION_MEMBER_P (fn))
4208
                      {
4209
                        koenig_p = true;
4210
                        postfix_expression
4211
                          = perform_koenig_lookup (postfix_expression, args);
4212
                      }
4213
                  }
4214
              }
4215
 
4216
            if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4217
              {
4218
                tree instance = TREE_OPERAND (postfix_expression, 0);
4219
                tree fn = TREE_OPERAND (postfix_expression, 1);
4220
 
4221
                if (processing_template_decl
4222
                    && (type_dependent_expression_p (instance)
4223
                        || (!BASELINK_P (fn)
4224
                            && TREE_CODE (fn) != FIELD_DECL)
4225
                        || type_dependent_expression_p (fn)
4226
                        || any_type_dependent_arguments_p (args)))
4227
                  {
4228
                    postfix_expression
4229
                      = build_min_nt (CALL_EXPR, postfix_expression,
4230
                                      args, NULL_TREE);
4231
                    break;
4232
                  }
4233
 
4234
                if (BASELINK_P (fn))
4235
                  postfix_expression
4236
                    = (build_new_method_call
4237
                       (instance, fn, args, NULL_TREE,
4238
                        (idk == CP_ID_KIND_QUALIFIED
4239
                         ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4240
                else
4241
                  postfix_expression
4242
                    = finish_call_expr (postfix_expression, args,
4243
                                        /*disallow_virtual=*/false,
4244
                                        /*koenig_p=*/false);
4245
              }
4246
            else if (TREE_CODE (postfix_expression) == OFFSET_REF
4247
                     || TREE_CODE (postfix_expression) == MEMBER_REF
4248
                     || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4249
              postfix_expression = (build_offset_ref_call_from_tree
4250
                                    (postfix_expression, args));
4251
            else if (idk == CP_ID_KIND_QUALIFIED)
4252
              /* A call to a static class member, or a namespace-scope
4253
                 function.  */
4254
              postfix_expression
4255
                = finish_call_expr (postfix_expression, args,
4256
                                    /*disallow_virtual=*/true,
4257
                                    koenig_p);
4258
            else
4259
              /* All other function calls.  */
4260
              postfix_expression
4261
                = finish_call_expr (postfix_expression, args,
4262
                                    /*disallow_virtual=*/false,
4263
                                    koenig_p);
4264
 
4265
            /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4266
            idk = CP_ID_KIND_NONE;
4267
          }
4268
          break;
4269
 
4270
        case CPP_DOT:
4271
        case CPP_DEREF:
4272
          /* postfix-expression . template [opt] id-expression
4273
             postfix-expression . pseudo-destructor-name
4274
             postfix-expression -> template [opt] id-expression
4275
             postfix-expression -> pseudo-destructor-name */
4276
 
4277
          /* Consume the `.' or `->' operator.  */
4278
          cp_lexer_consume_token (parser->lexer);
4279
 
4280
          postfix_expression
4281
            = cp_parser_postfix_dot_deref_expression (parser, token->type,
4282
                                                      postfix_expression,
4283
                                                      false, &idk);
4284
          break;
4285
 
4286
        case CPP_PLUS_PLUS:
4287
          /* postfix-expression ++  */
4288
          /* Consume the `++' token.  */
4289
          cp_lexer_consume_token (parser->lexer);
4290
          /* Generate a representation for the complete expression.  */
4291
          postfix_expression
4292
            = finish_increment_expr (postfix_expression,
4293
                                     POSTINCREMENT_EXPR);
4294
          /* Increments may not appear in constant-expressions.  */
4295
          if (cp_parser_non_integral_constant_expression (parser,
4296
                                                          "an increment"))
4297
            postfix_expression = error_mark_node;
4298
          idk = CP_ID_KIND_NONE;
4299
          break;
4300
 
4301
        case CPP_MINUS_MINUS:
4302
          /* postfix-expression -- */
4303
          /* Consume the `--' token.  */
4304
          cp_lexer_consume_token (parser->lexer);
4305
          /* Generate a representation for the complete expression.  */
4306
          postfix_expression
4307
            = finish_increment_expr (postfix_expression,
4308
                                     POSTDECREMENT_EXPR);
4309
          /* Decrements may not appear in constant-expressions.  */
4310
          if (cp_parser_non_integral_constant_expression (parser,
4311
                                                          "a decrement"))
4312
            postfix_expression = error_mark_node;
4313
          idk = CP_ID_KIND_NONE;
4314
          break;
4315
 
4316
        default:
4317
          return postfix_expression;
4318
        }
4319
    }
4320
 
4321
  /* We should never get here.  */
4322
  gcc_unreachable ();
4323
  return error_mark_node;
4324
}
4325
 
4326
/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4327
   by cp_parser_builtin_offsetof.  We're looking for
4328
 
4329
     postfix-expression [ expression ]
4330
 
4331
   FOR_OFFSETOF is set if we're being called in that context, which
4332
   changes how we deal with integer constant expressions.  */
4333
 
4334
static tree
4335
cp_parser_postfix_open_square_expression (cp_parser *parser,
4336
                                          tree postfix_expression,
4337
                                          bool for_offsetof)
4338
{
4339
  tree index;
4340
 
4341
  /* Consume the `[' token.  */
4342
  cp_lexer_consume_token (parser->lexer);
4343
 
4344
  /* Parse the index expression.  */
4345
  /* ??? For offsetof, there is a question of what to allow here.  If
4346
     offsetof is not being used in an integral constant expression context,
4347
     then we *could* get the right answer by computing the value at runtime.
4348
     If we are in an integral constant expression context, then we might
4349
     could accept any constant expression; hard to say without analysis.
4350
     Rather than open the barn door too wide right away, allow only integer
4351
     constant expressions here.  */
4352
  if (for_offsetof)
4353
    index = cp_parser_constant_expression (parser, false, NULL);
4354
  else
4355
    index = cp_parser_expression (parser, /*cast_p=*/false);
4356
 
4357
  /* Look for the closing `]'.  */
4358
  cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4359
 
4360
  /* Build the ARRAY_REF.  */
4361
  postfix_expression = grok_array_decl (postfix_expression, index);
4362
 
4363
  /* When not doing offsetof, array references are not permitted in
4364
     constant-expressions.  */
4365
  if (!for_offsetof
4366
      && (cp_parser_non_integral_constant_expression
4367
          (parser, "an array reference")))
4368
    postfix_expression = error_mark_node;
4369
 
4370
  return postfix_expression;
4371
}
4372
 
4373
/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4374
   by cp_parser_builtin_offsetof.  We're looking for
4375
 
4376
     postfix-expression . template [opt] id-expression
4377
     postfix-expression . pseudo-destructor-name
4378
     postfix-expression -> template [opt] id-expression
4379
     postfix-expression -> pseudo-destructor-name
4380
 
4381
   FOR_OFFSETOF is set if we're being called in that context.  That sorta
4382
   limits what of the above we'll actually accept, but nevermind.
4383
   TOKEN_TYPE is the "." or "->" token, which will already have been
4384
   removed from the stream.  */
4385
 
4386
static tree
4387
cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4388
                                        enum cpp_ttype token_type,
4389
                                        tree postfix_expression,
4390
                                        bool for_offsetof, cp_id_kind *idk)
4391
{
4392
  tree name;
4393
  bool dependent_p;
4394
  bool pseudo_destructor_p;
4395
  tree scope = NULL_TREE;
4396
 
4397
  /* If this is a `->' operator, dereference the pointer.  */
4398
  if (token_type == CPP_DEREF)
4399
    postfix_expression = build_x_arrow (postfix_expression);
4400
  /* Check to see whether or not the expression is type-dependent.  */
4401
  dependent_p = type_dependent_expression_p (postfix_expression);
4402
  /* The identifier following the `->' or `.' is not qualified.  */
4403
  parser->scope = NULL_TREE;
4404
  parser->qualifying_scope = NULL_TREE;
4405
  parser->object_scope = NULL_TREE;
4406
  *idk = CP_ID_KIND_NONE;
4407
  /* Enter the scope corresponding to the type of the object
4408
     given by the POSTFIX_EXPRESSION.  */
4409
  if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4410
    {
4411
      scope = TREE_TYPE (postfix_expression);
4412
      /* According to the standard, no expression should ever have
4413
         reference type.  Unfortunately, we do not currently match
4414
         the standard in this respect in that our internal representation
4415
         of an expression may have reference type even when the standard
4416
         says it does not.  Therefore, we have to manually obtain the
4417
         underlying type here.  */
4418
      scope = non_reference (scope);
4419
      /* The type of the POSTFIX_EXPRESSION must be complete.  */
4420
      if (scope == unknown_type_node)
4421
        {
4422
          error ("%qE does not have class type", postfix_expression);
4423
          scope = NULL_TREE;
4424
        }
4425
      else
4426
        scope = complete_type_or_else (scope, NULL_TREE);
4427
      /* Let the name lookup machinery know that we are processing a
4428
         class member access expression.  */
4429
      parser->context->object_type = scope;
4430
      /* If something went wrong, we want to be able to discern that case,
4431
         as opposed to the case where there was no SCOPE due to the type
4432
         of expression being dependent.  */
4433
      if (!scope)
4434
        scope = error_mark_node;
4435
      /* If the SCOPE was erroneous, make the various semantic analysis
4436
         functions exit quickly -- and without issuing additional error
4437
         messages.  */
4438
      if (scope == error_mark_node)
4439
        postfix_expression = error_mark_node;
4440
    }
4441
 
4442
  /* Assume this expression is not a pseudo-destructor access.  */
4443
  pseudo_destructor_p = false;
4444
 
4445
  /* If the SCOPE is a scalar type, then, if this is a valid program,
4446
     we must be looking at a pseudo-destructor-name.  */
4447
  if (scope && SCALAR_TYPE_P (scope))
4448
    {
4449
      tree s;
4450
      tree type;
4451
 
4452
      cp_parser_parse_tentatively (parser);
4453
      /* Parse the pseudo-destructor-name.  */
4454
      s = NULL_TREE;
4455
      cp_parser_pseudo_destructor_name (parser, &s, &type);
4456
      if (cp_parser_parse_definitely (parser))
4457
        {
4458
          pseudo_destructor_p = true;
4459
          postfix_expression
4460
            = finish_pseudo_destructor_expr (postfix_expression,
4461
                                             s, TREE_TYPE (type));
4462
        }
4463
    }
4464
 
4465
  if (!pseudo_destructor_p)
4466
    {
4467
      /* If the SCOPE is not a scalar type, we are looking at an
4468
         ordinary class member access expression, rather than a
4469
         pseudo-destructor-name.  */
4470
      bool template_p;
4471
      /* Parse the id-expression.  */
4472
      name = (cp_parser_id_expression
4473
              (parser,
4474
               cp_parser_optional_template_keyword (parser),
4475
               /*check_dependency_p=*/true,
4476
               &template_p,
4477
               /*declarator_p=*/false));
4478
      /* In general, build a SCOPE_REF if the member name is qualified.
4479
         However, if the name was not dependent and has already been
4480
         resolved; there is no need to build the SCOPE_REF.  For example;
4481
 
4482
             struct X { void f(); };
4483
             template <typename T> void f(T* t) { t->X::f(); }
4484
 
4485
         Even though "t" is dependent, "X::f" is not and has been resolved
4486
         to a BASELINK; there is no need to include scope information.  */
4487
 
4488
      /* But we do need to remember that there was an explicit scope for
4489
         virtual function calls.  */
4490
      if (parser->scope)
4491
        *idk = CP_ID_KIND_QUALIFIED;
4492
 
4493
      /* If the name is a template-id that names a type, we will get a
4494
         TYPE_DECL here.  That is invalid code.  */
4495
      if (TREE_CODE (name) == TYPE_DECL)
4496
        {
4497
          error ("invalid use of %qD", name);
4498
          postfix_expression = error_mark_node;
4499
        }
4500
      else
4501
        {
4502
          if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4503
            {
4504
              name = build_qualified_name (/*type=*/NULL_TREE,
4505
                                           parser->scope,
4506
                                           name,
4507
                                           template_p);
4508
              parser->scope = NULL_TREE;
4509
              parser->qualifying_scope = NULL_TREE;
4510
              parser->object_scope = NULL_TREE;
4511
            }
4512
          if (scope && name && BASELINK_P (name))
4513
            adjust_result_of_qualified_name_lookup
4514
              (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4515
          postfix_expression
4516
            = finish_class_member_access_expr (postfix_expression, name,
4517
                                               template_p);
4518
        }
4519
    }
4520
 
4521
  /* We no longer need to look up names in the scope of the object on
4522
     the left-hand side of the `.' or `->' operator.  */
4523
  parser->context->object_type = NULL_TREE;
4524
 
4525
  /* Outside of offsetof, these operators may not appear in
4526
     constant-expressions.  */
4527
  if (!for_offsetof
4528
      && (cp_parser_non_integral_constant_expression
4529
          (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4530
    postfix_expression = error_mark_node;
4531
 
4532
  return postfix_expression;
4533
}
4534
 
4535
/* Parse a parenthesized expression-list.
4536
 
4537
   expression-list:
4538
     assignment-expression
4539
     expression-list, assignment-expression
4540
 
4541
   attribute-list:
4542
     expression-list
4543
     identifier
4544
     identifier, expression-list
4545
 
4546
   CAST_P is true if this expression is the target of a cast.
4547
 
4548
   Returns a TREE_LIST.  The TREE_VALUE of each node is a
4549
   representation of an assignment-expression.  Note that a TREE_LIST
4550
   is returned even if there is only a single expression in the list.
4551
   error_mark_node is returned if the ( and or ) are
4552
   missing. NULL_TREE is returned on no expressions. The parentheses
4553
   are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4554
   list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4555
   indicates whether or not all of the expressions in the list were
4556
   constant.  */
4557
 
4558
static tree
4559
cp_parser_parenthesized_expression_list (cp_parser* parser,
4560
                                         bool is_attribute_list,
4561
                                         bool cast_p,
4562
                                         bool *non_constant_p)
4563
{
4564
  tree expression_list = NULL_TREE;
4565
  bool fold_expr_p = is_attribute_list;
4566
  tree identifier = NULL_TREE;
4567
 
4568
  /* Assume all the expressions will be constant.  */
4569
  if (non_constant_p)
4570
    *non_constant_p = false;
4571
 
4572
  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4573
    return error_mark_node;
4574
 
4575
  /* Consume expressions until there are no more.  */
4576
  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4577
    while (true)
4578
      {
4579
        tree expr;
4580
 
4581
        /* At the beginning of attribute lists, check to see if the
4582
           next token is an identifier.  */
4583
        if (is_attribute_list
4584
            && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4585
          {
4586
            cp_token *token;
4587
 
4588
            /* Consume the identifier.  */
4589
            token = cp_lexer_consume_token (parser->lexer);
4590
            /* Save the identifier.  */
4591
            identifier = token->value;
4592
          }
4593
        else
4594
          {
4595
            /* Parse the next assignment-expression.  */
4596
            if (non_constant_p)
4597
              {
4598
                bool expr_non_constant_p;
4599
                expr = (cp_parser_constant_expression
4600
                        (parser, /*allow_non_constant_p=*/true,
4601
                         &expr_non_constant_p));
4602
                if (expr_non_constant_p)
4603
                  *non_constant_p = true;
4604
              }
4605
            else
4606
              expr = cp_parser_assignment_expression (parser, cast_p);
4607
 
4608
            if (fold_expr_p)
4609
              expr = fold_non_dependent_expr (expr);
4610
 
4611
             /* Add it to the list.  We add error_mark_node
4612
                expressions to the list, so that we can still tell if
4613
                the correct form for a parenthesized expression-list
4614
                is found. That gives better errors.  */
4615
            expression_list = tree_cons (NULL_TREE, expr, expression_list);
4616
 
4617
            if (expr == error_mark_node)
4618
              goto skip_comma;
4619
          }
4620
 
4621
        /* After the first item, attribute lists look the same as
4622
           expression lists.  */
4623
        is_attribute_list = false;
4624
 
4625
      get_comma:;
4626
        /* If the next token isn't a `,', then we are done.  */
4627
        if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4628
          break;
4629
 
4630
        /* Otherwise, consume the `,' and keep going.  */
4631
        cp_lexer_consume_token (parser->lexer);
4632
      }
4633
 
4634
  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4635
    {
4636
      int ending;
4637
 
4638
    skip_comma:;
4639
      /* We try and resync to an unnested comma, as that will give the
4640
         user better diagnostics.  */
4641
      ending = cp_parser_skip_to_closing_parenthesis (parser,
4642
                                                      /*recovering=*/true,
4643
                                                      /*or_comma=*/true,
4644
                                                      /*consume_paren=*/true);
4645
      if (ending < 0)
4646
        goto get_comma;
4647
      if (!ending)
4648
        return error_mark_node;
4649
    }
4650
 
4651
  /* We built up the list in reverse order so we must reverse it now.  */
4652
  expression_list = nreverse (expression_list);
4653
  if (identifier)
4654
    expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4655
 
4656
  return expression_list;
4657
}
4658
 
4659
/* Parse a pseudo-destructor-name.
4660
 
4661
   pseudo-destructor-name:
4662
     :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4663
     :: [opt] nested-name-specifier template template-id :: ~ type-name
4664
     :: [opt] nested-name-specifier [opt] ~ type-name
4665
 
4666
   If either of the first two productions is used, sets *SCOPE to the
4667
   TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4668
   NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4669
   or ERROR_MARK_NODE if the parse fails.  */
4670
 
4671
static void
4672
cp_parser_pseudo_destructor_name (cp_parser* parser,
4673
                                  tree* scope,
4674
                                  tree* type)
4675
{
4676
  bool nested_name_specifier_p;
4677
 
4678
  /* Assume that things will not work out.  */
4679
  *type = error_mark_node;
4680
 
4681
  /* Look for the optional `::' operator.  */
4682
  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4683
  /* Look for the optional nested-name-specifier.  */
4684
  nested_name_specifier_p
4685
    = (cp_parser_nested_name_specifier_opt (parser,
4686
                                            /*typename_keyword_p=*/false,
4687
                                            /*check_dependency_p=*/true,
4688
                                            /*type_p=*/false,
4689
                                            /*is_declaration=*/true)
4690
       != NULL_TREE);
4691
  /* Now, if we saw a nested-name-specifier, we might be doing the
4692
     second production.  */
4693
  if (nested_name_specifier_p
4694
      && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4695
    {
4696
      /* Consume the `template' keyword.  */
4697
      cp_lexer_consume_token (parser->lexer);
4698
      /* Parse the template-id.  */
4699
      cp_parser_template_id (parser,
4700
                             /*template_keyword_p=*/true,
4701
                             /*check_dependency_p=*/false,
4702
                             /*is_declaration=*/true);
4703
      /* Look for the `::' token.  */
4704
      cp_parser_require (parser, CPP_SCOPE, "`::'");
4705
    }
4706
  /* If the next token is not a `~', then there might be some
4707
     additional qualification.  */
4708
  else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4709
    {
4710
      /* Look for the type-name.  */
4711
      *scope = TREE_TYPE (cp_parser_type_name (parser));
4712
 
4713
      if (*scope == error_mark_node)
4714
        return;
4715
 
4716
      /* If we don't have ::~, then something has gone wrong.  Since
4717
         the only caller of this function is looking for something
4718
         after `.' or `->' after a scalar type, most likely the
4719
         program is trying to get a member of a non-aggregate
4720
         type.  */
4721
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4722
          || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4723
        {
4724
          cp_parser_error (parser, "request for member of non-aggregate type");
4725
          return;
4726
        }
4727
 
4728
      /* Look for the `::' token.  */
4729
      cp_parser_require (parser, CPP_SCOPE, "`::'");
4730
    }
4731
  else
4732
    *scope = NULL_TREE;
4733
 
4734
  /* Look for the `~'.  */
4735
  cp_parser_require (parser, CPP_COMPL, "`~'");
4736
  /* Look for the type-name again.  We are not responsible for
4737
     checking that it matches the first type-name.  */
4738
  *type = cp_parser_type_name (parser);
4739
}
4740
 
4741
/* Parse a unary-expression.
4742
 
4743
   unary-expression:
4744
     postfix-expression
4745
     ++ cast-expression
4746
     -- cast-expression
4747
     unary-operator cast-expression
4748
     sizeof unary-expression
4749
     sizeof ( type-id )
4750
     new-expression
4751
     delete-expression
4752
 
4753
   GNU Extensions:
4754
 
4755
   unary-expression:
4756
     __extension__ cast-expression
4757
     __alignof__ unary-expression
4758
     __alignof__ ( type-id )
4759
     __real__ cast-expression
4760
     __imag__ cast-expression
4761
     && identifier
4762
 
4763
   ADDRESS_P is true iff the unary-expression is appearing as the
4764
   operand of the `&' operator.   CAST_P is true if this expression is
4765
   the target of a cast.
4766
 
4767
   Returns a representation of the expression.  */
4768
 
4769
static tree
4770
cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4771
{
4772
  cp_token *token;
4773
  enum tree_code unary_operator;
4774
 
4775
  /* Peek at the next token.  */
4776
  token = cp_lexer_peek_token (parser->lexer);
4777
  /* Some keywords give away the kind of expression.  */
4778
  if (token->type == CPP_KEYWORD)
4779
    {
4780
      enum rid keyword = token->keyword;
4781
 
4782
      switch (keyword)
4783
        {
4784
        case RID_ALIGNOF:
4785
        case RID_SIZEOF:
4786
          {
4787
            tree operand;
4788
            enum tree_code op;
4789
 
4790
            op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4791
            /* Consume the token.  */
4792
            cp_lexer_consume_token (parser->lexer);
4793
            /* Parse the operand.  */
4794
            operand = cp_parser_sizeof_operand (parser, keyword);
4795
 
4796
            if (TYPE_P (operand))
4797
              return cxx_sizeof_or_alignof_type (operand, op, true);
4798
            else
4799
              return cxx_sizeof_or_alignof_expr (operand, op);
4800
          }
4801
 
4802
        case RID_NEW:
4803
          return cp_parser_new_expression (parser);
4804
 
4805
        case RID_DELETE:
4806
          return cp_parser_delete_expression (parser);
4807
 
4808
        case RID_EXTENSION:
4809
          {
4810
            /* The saved value of the PEDANTIC flag.  */
4811
            int saved_pedantic;
4812
            tree expr;
4813
 
4814
            /* Save away the PEDANTIC flag.  */
4815
            cp_parser_extension_opt (parser, &saved_pedantic);
4816
            /* Parse the cast-expression.  */
4817
            expr = cp_parser_simple_cast_expression (parser);
4818
            /* Restore the PEDANTIC flag.  */
4819
            pedantic = saved_pedantic;
4820
 
4821
            return expr;
4822
          }
4823
 
4824
        case RID_REALPART:
4825
        case RID_IMAGPART:
4826
          {
4827
            tree expression;
4828
 
4829
            /* Consume the `__real__' or `__imag__' token.  */
4830
            cp_lexer_consume_token (parser->lexer);
4831
            /* Parse the cast-expression.  */
4832
            expression = cp_parser_simple_cast_expression (parser);
4833
            /* Create the complete representation.  */
4834
            return build_x_unary_op ((keyword == RID_REALPART
4835
                                      ? REALPART_EXPR : IMAGPART_EXPR),
4836
                                     expression);
4837
          }
4838
          break;
4839
 
4840
        default:
4841
          break;
4842
        }
4843
    }
4844
 
4845
  /* Look for the `:: new' and `:: delete', which also signal the
4846
     beginning of a new-expression, or delete-expression,
4847
     respectively.  If the next token is `::', then it might be one of
4848
     these.  */
4849
  if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4850
    {
4851
      enum rid keyword;
4852
 
4853
      /* See if the token after the `::' is one of the keywords in
4854
         which we're interested.  */
4855
      keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4856
      /* If it's `new', we have a new-expression.  */
4857
      if (keyword == RID_NEW)
4858
        return cp_parser_new_expression (parser);
4859
      /* Similarly, for `delete'.  */
4860
      else if (keyword == RID_DELETE)
4861
        return cp_parser_delete_expression (parser);
4862
    }
4863
 
4864
  /* Look for a unary operator.  */
4865
  unary_operator = cp_parser_unary_operator (token);
4866
  /* The `++' and `--' operators can be handled similarly, even though
4867
     they are not technically unary-operators in the grammar.  */
4868
  if (unary_operator == ERROR_MARK)
4869
    {
4870
      if (token->type == CPP_PLUS_PLUS)
4871
        unary_operator = PREINCREMENT_EXPR;
4872
      else if (token->type == CPP_MINUS_MINUS)
4873
        unary_operator = PREDECREMENT_EXPR;
4874
      /* Handle the GNU address-of-label extension.  */
4875
      else if (cp_parser_allow_gnu_extensions_p (parser)
4876
               && token->type == CPP_AND_AND)
4877
        {
4878
          tree identifier;
4879
 
4880
          /* Consume the '&&' token.  */
4881
          cp_lexer_consume_token (parser->lexer);
4882
          /* Look for the identifier.  */
4883
          identifier = cp_parser_identifier (parser);
4884
          /* Create an expression representing the address.  */
4885
          return finish_label_address_expr (identifier);
4886
        }
4887
    }
4888
  if (unary_operator != ERROR_MARK)
4889
    {
4890
      tree cast_expression;
4891
      tree expression = error_mark_node;
4892
      const char *non_constant_p = NULL;
4893
 
4894
      /* Consume the operator token.  */
4895
      token = cp_lexer_consume_token (parser->lexer);
4896
      /* Parse the cast-expression.  */
4897
      cast_expression
4898
        = cp_parser_cast_expression (parser,
4899
                                     unary_operator == ADDR_EXPR,
4900
                                     /*cast_p=*/false);
4901
      /* Now, build an appropriate representation.  */
4902
      switch (unary_operator)
4903
        {
4904
        case INDIRECT_REF:
4905
          non_constant_p = "`*'";
4906
          expression = build_x_indirect_ref (cast_expression, "unary *");
4907
          break;
4908
 
4909
        case ADDR_EXPR:
4910
          non_constant_p = "`&'";
4911
          /* Fall through.  */
4912
        case BIT_NOT_EXPR:
4913
          expression = build_x_unary_op (unary_operator, cast_expression);
4914
          break;
4915
 
4916
        case PREINCREMENT_EXPR:
4917
        case PREDECREMENT_EXPR:
4918
          non_constant_p = (unary_operator == PREINCREMENT_EXPR
4919
                            ? "`++'" : "`--'");
4920
          /* Fall through.  */
4921
        case UNARY_PLUS_EXPR:
4922
        case NEGATE_EXPR:
4923
        case TRUTH_NOT_EXPR:
4924
          expression = finish_unary_op_expr (unary_operator, cast_expression);
4925
          break;
4926
 
4927
        default:
4928
          gcc_unreachable ();
4929
        }
4930
 
4931
      if (non_constant_p
4932
          && cp_parser_non_integral_constant_expression (parser,
4933
                                                         non_constant_p))
4934
        expression = error_mark_node;
4935
 
4936
      return expression;
4937
    }
4938
 
4939
  return cp_parser_postfix_expression (parser, address_p, cast_p);
4940
}
4941
 
4942
/* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4943
   unary-operator, the corresponding tree code is returned.  */
4944
 
4945
static enum tree_code
4946
cp_parser_unary_operator (cp_token* token)
4947
{
4948
  switch (token->type)
4949
    {
4950
    case CPP_MULT:
4951
      return INDIRECT_REF;
4952
 
4953
    case CPP_AND:
4954
      return ADDR_EXPR;
4955
 
4956
    case CPP_PLUS:
4957
      return UNARY_PLUS_EXPR;
4958
 
4959
    case CPP_MINUS:
4960
      return NEGATE_EXPR;
4961
 
4962
    case CPP_NOT:
4963
      return TRUTH_NOT_EXPR;
4964
 
4965
    case CPP_COMPL:
4966
      return BIT_NOT_EXPR;
4967
 
4968
    default:
4969
      return ERROR_MARK;
4970
    }
4971
}
4972
 
4973
/* Parse a new-expression.
4974
 
4975
   new-expression:
4976
     :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4977
     :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4978
 
4979
   Returns a representation of the expression.  */
4980
 
4981
static tree
4982
cp_parser_new_expression (cp_parser* parser)
4983
{
4984
  bool global_scope_p;
4985
  tree placement;
4986
  tree type;
4987
  tree initializer;
4988
  tree nelts;
4989
 
4990
  /* Look for the optional `::' operator.  */
4991
  global_scope_p
4992
    = (cp_parser_global_scope_opt (parser,
4993
                                   /*current_scope_valid_p=*/false)
4994
       != NULL_TREE);
4995
  /* Look for the `new' operator.  */
4996
  cp_parser_require_keyword (parser, RID_NEW, "`new'");
4997
  /* There's no easy way to tell a new-placement from the
4998
     `( type-id )' construct.  */
4999
  cp_parser_parse_tentatively (parser);
5000
  /* Look for a new-placement.  */
5001
  placement = cp_parser_new_placement (parser);
5002
  /* If that didn't work out, there's no new-placement.  */
5003
  if (!cp_parser_parse_definitely (parser))
5004
    placement = NULL_TREE;
5005
 
5006
  /* If the next token is a `(', then we have a parenthesized
5007
     type-id.  */
5008
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5009
    {
5010
      /* Consume the `('.  */
5011
      cp_lexer_consume_token (parser->lexer);
5012
      /* Parse the type-id.  */
5013
      type = cp_parser_type_id (parser);
5014
      /* Look for the closing `)'.  */
5015
      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5016
      /* There should not be a direct-new-declarator in this production,
5017
         but GCC used to allowed this, so we check and emit a sensible error
5018
         message for this case.  */
5019
      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5020
        {
5021
          error ("array bound forbidden after parenthesized type-id");
5022
          inform ("try removing the parentheses around the type-id");
5023
          cp_parser_direct_new_declarator (parser);
5024
        }
5025
      nelts = NULL_TREE;
5026
    }
5027
  /* Otherwise, there must be a new-type-id.  */
5028
  else
5029
    type = cp_parser_new_type_id (parser, &nelts);
5030
 
5031
  /* If the next token is a `(', then we have a new-initializer.  */
5032
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5033
    initializer = cp_parser_new_initializer (parser);
5034
  else
5035
    initializer = NULL_TREE;
5036
 
5037
  /* A new-expression may not appear in an integral constant
5038
     expression.  */
5039
  if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5040
    return error_mark_node;
5041
 
5042
  /* Create a representation of the new-expression.  */
5043
  return build_new (placement, type, nelts, initializer, global_scope_p);
5044
}
5045
 
5046
/* Parse a new-placement.
5047
 
5048
   new-placement:
5049
     ( expression-list )
5050
 
5051
   Returns the same representation as for an expression-list.  */
5052
 
5053
static tree
5054
cp_parser_new_placement (cp_parser* parser)
5055
{
5056
  tree expression_list;
5057
 
5058
  /* Parse the expression-list.  */
5059
  expression_list = (cp_parser_parenthesized_expression_list
5060
                     (parser, false, /*cast_p=*/false,
5061
                      /*non_constant_p=*/NULL));
5062
 
5063
  return expression_list;
5064
}
5065
 
5066
/* Parse a new-type-id.
5067
 
5068
   new-type-id:
5069
     type-specifier-seq new-declarator [opt]
5070
 
5071
   Returns the TYPE allocated.  If the new-type-id indicates an array
5072
   type, *NELTS is set to the number of elements in the last array
5073
   bound; the TYPE will not include the last array bound.  */
5074
 
5075
static tree
5076
cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5077
{
5078
  cp_decl_specifier_seq type_specifier_seq;
5079
  cp_declarator *new_declarator;
5080
  cp_declarator *declarator;
5081
  cp_declarator *outer_declarator;
5082
  const char *saved_message;
5083
  tree type;
5084
 
5085
  /* The type-specifier sequence must not contain type definitions.
5086
     (It cannot contain declarations of new types either, but if they
5087
     are not definitions we will catch that because they are not
5088
     complete.)  */
5089
  saved_message = parser->type_definition_forbidden_message;
5090
  parser->type_definition_forbidden_message
5091
    = "types may not be defined in a new-type-id";
5092
  /* Parse the type-specifier-seq.  */
5093
  cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5094
                                &type_specifier_seq);
5095
  /* Restore the old message.  */
5096
  parser->type_definition_forbidden_message = saved_message;
5097
  /* Parse the new-declarator.  */
5098
  new_declarator = cp_parser_new_declarator_opt (parser);
5099
 
5100
  /* Determine the number of elements in the last array dimension, if
5101
     any.  */
5102
  *nelts = NULL_TREE;
5103
  /* Skip down to the last array dimension.  */
5104
  declarator = new_declarator;
5105
  outer_declarator = NULL;
5106
  while (declarator && (declarator->kind == cdk_pointer
5107
                        || declarator->kind == cdk_ptrmem))
5108
    {
5109
      outer_declarator = declarator;
5110
      declarator = declarator->declarator;
5111
    }
5112
  while (declarator
5113
         && declarator->kind == cdk_array
5114
         && declarator->declarator
5115
         && declarator->declarator->kind == cdk_array)
5116
    {
5117
      outer_declarator = declarator;
5118
      declarator = declarator->declarator;
5119
    }
5120
 
5121
  if (declarator && declarator->kind == cdk_array)
5122
    {
5123
      *nelts = declarator->u.array.bounds;
5124
      if (*nelts == error_mark_node)
5125
        *nelts = integer_one_node;
5126
 
5127
      if (outer_declarator)
5128
        outer_declarator->declarator = declarator->declarator;
5129
      else
5130
        new_declarator = NULL;
5131
    }
5132
 
5133
  type = groktypename (&type_specifier_seq, new_declarator);
5134
  if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5135
    {
5136
      *nelts = array_type_nelts_top (type);
5137
      type = TREE_TYPE (type);
5138
    }
5139
  return type;
5140
}
5141
 
5142
/* Parse an (optional) new-declarator.
5143
 
5144
   new-declarator:
5145
     ptr-operator new-declarator [opt]
5146
     direct-new-declarator
5147
 
5148
   Returns the declarator.  */
5149
 
5150
static cp_declarator *
5151
cp_parser_new_declarator_opt (cp_parser* parser)
5152
{
5153
  enum tree_code code;
5154
  tree type;
5155
  cp_cv_quals cv_quals;
5156
 
5157
  /* We don't know if there's a ptr-operator next, or not.  */
5158
  cp_parser_parse_tentatively (parser);
5159
  /* Look for a ptr-operator.  */
5160
  code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5161
  /* If that worked, look for more new-declarators.  */
5162
  if (cp_parser_parse_definitely (parser))
5163
    {
5164
      cp_declarator *declarator;
5165
 
5166
      /* Parse another optional declarator.  */
5167
      declarator = cp_parser_new_declarator_opt (parser);
5168
 
5169
      /* Create the representation of the declarator.  */
5170
      if (type)
5171
        declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5172
      else if (code == INDIRECT_REF)
5173
        declarator = make_pointer_declarator (cv_quals, declarator);
5174
      else
5175
        declarator = make_reference_declarator (cv_quals, declarator);
5176
 
5177
      return declarator;
5178
    }
5179
 
5180
  /* If the next token is a `[', there is a direct-new-declarator.  */
5181
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5182
    return cp_parser_direct_new_declarator (parser);
5183
 
5184
  return NULL;
5185
}
5186
 
5187
/* Parse a direct-new-declarator.
5188
 
5189
   direct-new-declarator:
5190
     [ expression ]
5191
     direct-new-declarator [constant-expression]
5192
 
5193
   */
5194
 
5195
static cp_declarator *
5196
cp_parser_direct_new_declarator (cp_parser* parser)
5197
{
5198
  cp_declarator *declarator = NULL;
5199
 
5200
  while (true)
5201
    {
5202
      tree expression;
5203
 
5204
      /* Look for the opening `['.  */
5205
      cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5206
      /* The first expression is not required to be constant.  */
5207
      if (!declarator)
5208
        {
5209
          expression = cp_parser_expression (parser, /*cast_p=*/false);
5210
          /* The standard requires that the expression have integral
5211
             type.  DR 74 adds enumeration types.  We believe that the
5212
             real intent is that these expressions be handled like the
5213
             expression in a `switch' condition, which also allows
5214
             classes with a single conversion to integral or
5215
             enumeration type.  */
5216
          if (!processing_template_decl)
5217
            {
5218
              expression
5219
                = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5220
                                              expression,
5221
                                              /*complain=*/true);
5222
              if (!expression)
5223
                {
5224
                  error ("expression in new-declarator must have integral "
5225
                         "or enumeration type");
5226
                  expression = error_mark_node;
5227
                }
5228
            }
5229
        }
5230
      /* But all the other expressions must be.  */
5231
      else
5232
        expression
5233
          = cp_parser_constant_expression (parser,
5234
                                           /*allow_non_constant=*/false,
5235
                                           NULL);
5236
      /* Look for the closing `]'.  */
5237
      cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5238
 
5239
      /* Add this bound to the declarator.  */
5240
      declarator = make_array_declarator (declarator, expression);
5241
 
5242
      /* If the next token is not a `[', then there are no more
5243
         bounds.  */
5244
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5245
        break;
5246
    }
5247
 
5248
  return declarator;
5249
}
5250
 
5251
/* Parse a new-initializer.
5252
 
5253
   new-initializer:
5254
     ( expression-list [opt] )
5255
 
5256
   Returns a representation of the expression-list.  If there is no
5257
   expression-list, VOID_ZERO_NODE is returned.  */
5258
 
5259
static tree
5260
cp_parser_new_initializer (cp_parser* parser)
5261
{
5262
  tree expression_list;
5263
 
5264
  expression_list = (cp_parser_parenthesized_expression_list
5265
                     (parser, false, /*cast_p=*/false,
5266
                      /*non_constant_p=*/NULL));
5267
  if (!expression_list)
5268
    expression_list = void_zero_node;
5269
 
5270
  return expression_list;
5271
}
5272
 
5273
/* Parse a delete-expression.
5274
 
5275
   delete-expression:
5276
     :: [opt] delete cast-expression
5277
     :: [opt] delete [ ] cast-expression
5278
 
5279
   Returns a representation of the expression.  */
5280
 
5281
static tree
5282
cp_parser_delete_expression (cp_parser* parser)
5283
{
5284
  bool global_scope_p;
5285
  bool array_p;
5286
  tree expression;
5287
 
5288
  /* Look for the optional `::' operator.  */
5289
  global_scope_p
5290
    = (cp_parser_global_scope_opt (parser,
5291
                                   /*current_scope_valid_p=*/false)
5292
       != NULL_TREE);
5293
  /* Look for the `delete' keyword.  */
5294
  cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5295
  /* See if the array syntax is in use.  */
5296
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5297
    {
5298
      /* Consume the `[' token.  */
5299
      cp_lexer_consume_token (parser->lexer);
5300
      /* Look for the `]' token.  */
5301
      cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5302
      /* Remember that this is the `[]' construct.  */
5303
      array_p = true;
5304
    }
5305
  else
5306
    array_p = false;
5307
 
5308
  /* Parse the cast-expression.  */
5309
  expression = cp_parser_simple_cast_expression (parser);
5310
 
5311
  /* A delete-expression may not appear in an integral constant
5312
     expression.  */
5313
  if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5314
    return error_mark_node;
5315
 
5316
  return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5317
}
5318
 
5319
/* Parse a cast-expression.
5320
 
5321
   cast-expression:
5322
     unary-expression
5323
     ( type-id ) cast-expression
5324
 
5325
   ADDRESS_P is true iff the unary-expression is appearing as the
5326
   operand of the `&' operator.   CAST_P is true if this expression is
5327
   the target of a cast.
5328
 
5329
   Returns a representation of the expression.  */
5330
 
5331
static tree
5332
cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5333
{
5334
  /* If it's a `(', then we might be looking at a cast.  */
5335
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5336
    {
5337
      tree type = NULL_TREE;
5338
      tree expr = NULL_TREE;
5339
      bool compound_literal_p;
5340
      const char *saved_message;
5341
 
5342
      /* There's no way to know yet whether or not this is a cast.
5343
         For example, `(int (3))' is a unary-expression, while `(int)
5344
         3' is a cast.  So, we resort to parsing tentatively.  */
5345
      cp_parser_parse_tentatively (parser);
5346
      /* Types may not be defined in a cast.  */
5347
      saved_message = parser->type_definition_forbidden_message;
5348
      parser->type_definition_forbidden_message
5349
        = "types may not be defined in casts";
5350
      /* Consume the `('.  */
5351
      cp_lexer_consume_token (parser->lexer);
5352
      /* A very tricky bit is that `(struct S) { 3 }' is a
5353
         compound-literal (which we permit in C++ as an extension).
5354
         But, that construct is not a cast-expression -- it is a
5355
         postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5356
         is legal; if the compound-literal were a cast-expression,
5357
         you'd need an extra set of parentheses.)  But, if we parse
5358
         the type-id, and it happens to be a class-specifier, then we
5359
         will commit to the parse at that point, because we cannot
5360
         undo the action that is done when creating a new class.  So,
5361
         then we cannot back up and do a postfix-expression.
5362
 
5363
         Therefore, we scan ahead to the closing `)', and check to see
5364
         if the token after the `)' is a `{'.  If so, we are not
5365
         looking at a cast-expression.
5366
 
5367
         Save tokens so that we can put them back.  */
5368
      cp_lexer_save_tokens (parser->lexer);
5369
      /* Skip tokens until the next token is a closing parenthesis.
5370
         If we find the closing `)', and the next token is a `{', then
5371
         we are looking at a compound-literal.  */
5372
      compound_literal_p
5373
        = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5374
                                                  /*consume_paren=*/true)
5375
           && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5376
      /* Roll back the tokens we skipped.  */
5377
      cp_lexer_rollback_tokens (parser->lexer);
5378
      /* If we were looking at a compound-literal, simulate an error
5379
         so that the call to cp_parser_parse_definitely below will
5380
         fail.  */
5381
      if (compound_literal_p)
5382
        cp_parser_simulate_error (parser);
5383
      else
5384
        {
5385
          bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5386
          parser->in_type_id_in_expr_p = true;
5387
          /* Look for the type-id.  */
5388
          type = cp_parser_type_id (parser);
5389
          /* Look for the closing `)'.  */
5390
          cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5391
          parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5392
        }
5393
 
5394
      /* Restore the saved message.  */
5395
      parser->type_definition_forbidden_message = saved_message;
5396
 
5397
      /* If ok so far, parse the dependent expression. We cannot be
5398
         sure it is a cast. Consider `(T ())'.  It is a parenthesized
5399
         ctor of T, but looks like a cast to function returning T
5400
         without a dependent expression.  */
5401
      if (!cp_parser_error_occurred (parser))
5402
        expr = cp_parser_cast_expression (parser,
5403
                                          /*address_p=*/false,
5404
                                          /*cast_p=*/true);
5405
 
5406
      if (cp_parser_parse_definitely (parser))
5407
        {
5408
          /* Warn about old-style casts, if so requested.  */
5409
          if (warn_old_style_cast
5410
              && !in_system_header
5411
              && !VOID_TYPE_P (type)
5412
              && current_lang_name != lang_name_c)
5413
            warning (0, "use of old-style cast");
5414
 
5415
          /* Only type conversions to integral or enumeration types
5416
             can be used in constant-expressions.  */
5417
          if (parser->integral_constant_expression_p
5418
              && !dependent_type_p (type)
5419
              && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5420
              && (cp_parser_non_integral_constant_expression
5421
                  (parser,
5422
                   "a cast to a type other than an integral or "
5423
                   "enumeration type")))
5424
            return error_mark_node;
5425
 
5426
          /* Perform the cast.  */
5427
          expr = build_c_cast (type, expr);
5428
          return expr;
5429
        }
5430
    }
5431
 
5432
  /* If we get here, then it's not a cast, so it must be a
5433
     unary-expression.  */
5434
  return cp_parser_unary_expression (parser, address_p, cast_p);
5435
}
5436
 
5437
/* Parse a binary expression of the general form:
5438
 
5439
   pm-expression:
5440
     cast-expression
5441
     pm-expression .* cast-expression
5442
     pm-expression ->* cast-expression
5443
 
5444
   multiplicative-expression:
5445
     pm-expression
5446
     multiplicative-expression * pm-expression
5447
     multiplicative-expression / pm-expression
5448
     multiplicative-expression % pm-expression
5449
 
5450
   additive-expression:
5451
     multiplicative-expression
5452
     additive-expression + multiplicative-expression
5453
     additive-expression - multiplicative-expression
5454
 
5455
   shift-expression:
5456
     additive-expression
5457
     shift-expression << additive-expression
5458
     shift-expression >> additive-expression
5459
 
5460
   relational-expression:
5461
     shift-expression
5462
     relational-expression < shift-expression
5463
     relational-expression > shift-expression
5464
     relational-expression <= shift-expression
5465
     relational-expression >= shift-expression
5466
 
5467
  GNU Extension:
5468
 
5469
   relational-expression:
5470
     relational-expression <? shift-expression
5471
     relational-expression >? shift-expression
5472
 
5473
   equality-expression:
5474
     relational-expression
5475
     equality-expression == relational-expression
5476
     equality-expression != relational-expression
5477
 
5478
   and-expression:
5479
     equality-expression
5480
     and-expression & equality-expression
5481
 
5482
   exclusive-or-expression:
5483
     and-expression
5484
     exclusive-or-expression ^ and-expression
5485
 
5486
   inclusive-or-expression:
5487
     exclusive-or-expression
5488
     inclusive-or-expression | exclusive-or-expression
5489
 
5490
   logical-and-expression:
5491
     inclusive-or-expression
5492
     logical-and-expression && inclusive-or-expression
5493
 
5494
   logical-or-expression:
5495
     logical-and-expression
5496
     logical-or-expression || logical-and-expression
5497
 
5498
   All these are implemented with a single function like:
5499
 
5500
   binary-expression:
5501
     simple-cast-expression
5502
     binary-expression <token> binary-expression
5503
 
5504
   CAST_P is true if this expression is the target of a cast.
5505
 
5506
   The binops_by_token map is used to get the tree codes for each <token> type.
5507
   binary-expressions are associated according to a precedence table.  */
5508
 
5509
#define TOKEN_PRECEDENCE(token) \
5510
  ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5511
   ? PREC_NOT_OPERATOR \
5512
   : binops_by_token[token->type].prec)
5513
 
5514
static tree
5515
cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5516
{
5517
  cp_parser_expression_stack stack;
5518
  cp_parser_expression_stack_entry *sp = &stack[0];
5519
  tree lhs, rhs;
5520
  cp_token *token;
5521
  enum tree_code tree_type;
5522
  enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5523
  bool overloaded_p;
5524
 
5525
  /* Parse the first expression.  */
5526
  lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5527
 
5528
  for (;;)
5529
    {
5530
      /* Get an operator token.  */
5531
      token = cp_lexer_peek_token (parser->lexer);
5532
      if (token->type == CPP_MIN || token->type == CPP_MAX)
5533
        cp_parser_warn_min_max ();
5534
 
5535
      new_prec = TOKEN_PRECEDENCE (token);
5536
 
5537
      /* Popping an entry off the stack means we completed a subexpression:
5538
         - either we found a token which is not an operator (`>' where it is not
5539
           an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5540
           will happen repeatedly;
5541
         - or, we found an operator which has lower priority.  This is the case
5542
           where the recursive descent *ascends*, as in `3 * 4 + 5' after
5543
           parsing `3 * 4'.  */
5544
      if (new_prec <= prec)
5545
        {
5546
          if (sp == stack)
5547
            break;
5548
          else
5549
            goto pop;
5550
        }
5551
 
5552
     get_rhs:
5553
      tree_type = binops_by_token[token->type].tree_type;
5554
 
5555
      /* We used the operator token.  */
5556
      cp_lexer_consume_token (parser->lexer);
5557
 
5558
      /* Extract another operand.  It may be the RHS of this expression
5559
         or the LHS of a new, higher priority expression.  */
5560
      rhs = cp_parser_simple_cast_expression (parser);
5561
 
5562
      /* Get another operator token.  Look up its precedence to avoid
5563
         building a useless (immediately popped) stack entry for common
5564
         cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5565
      token = cp_lexer_peek_token (parser->lexer);
5566
      lookahead_prec = TOKEN_PRECEDENCE (token);
5567
      if (lookahead_prec > new_prec)
5568
        {
5569
          /* ... and prepare to parse the RHS of the new, higher priority
5570
             expression.  Since precedence levels on the stack are
5571
             monotonically increasing, we do not have to care about
5572
             stack overflows.  */
5573
          sp->prec = prec;
5574
          sp->tree_type = tree_type;
5575
          sp->lhs = lhs;
5576
          sp++;
5577
          lhs = rhs;
5578
          prec = new_prec;
5579
          new_prec = lookahead_prec;
5580
          goto get_rhs;
5581
 
5582
         pop:
5583
          /* If the stack is not empty, we have parsed into LHS the right side
5584
             (`4' in the example above) of an expression we had suspended.
5585
             We can use the information on the stack to recover the LHS (`3')
5586
             from the stack together with the tree code (`MULT_EXPR'), and
5587
             the precedence of the higher level subexpression
5588
             (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5589
             which will be used to actually build the additive expression.  */
5590
          --sp;
5591
          prec = sp->prec;
5592
          tree_type = sp->tree_type;
5593
          rhs = lhs;
5594
          lhs = sp->lhs;
5595
        }
5596
 
5597
      overloaded_p = false;
5598
      lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5599
 
5600
      /* If the binary operator required the use of an overloaded operator,
5601
         then this expression cannot be an integral constant-expression.
5602
         An overloaded operator can be used even if both operands are
5603
         otherwise permissible in an integral constant-expression if at
5604
         least one of the operands is of enumeration type.  */
5605
 
5606
      if (overloaded_p
5607
          && (cp_parser_non_integral_constant_expression
5608
              (parser, "calls to overloaded operators")))
5609
        return error_mark_node;
5610
    }
5611
 
5612
  return lhs;
5613
}
5614
 
5615
 
5616
/* Parse the `? expression : assignment-expression' part of a
5617
   conditional-expression.  The LOGICAL_OR_EXPR is the
5618
   logical-or-expression that started the conditional-expression.
5619
   Returns a representation of the entire conditional-expression.
5620
 
5621
   This routine is used by cp_parser_assignment_expression.
5622
 
5623
     ? expression : assignment-expression
5624
 
5625
   GNU Extensions:
5626
 
5627
     ? : assignment-expression */
5628
 
5629
static tree
5630
cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5631
{
5632
  tree expr;
5633
  tree assignment_expr;
5634
 
5635
  /* Consume the `?' token.  */
5636
  cp_lexer_consume_token (parser->lexer);
5637
  if (cp_parser_allow_gnu_extensions_p (parser)
5638
      && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5639
    /* Implicit true clause.  */
5640
    expr = NULL_TREE;
5641
  else
5642
    /* Parse the expression.  */
5643
    expr = cp_parser_expression (parser, /*cast_p=*/false);
5644
 
5645
  /* The next token should be a `:'.  */
5646
  cp_parser_require (parser, CPP_COLON, "`:'");
5647
  /* Parse the assignment-expression.  */
5648
  assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5649
 
5650
  /* Build the conditional-expression.  */
5651
  return build_x_conditional_expr (logical_or_expr,
5652
                                   expr,
5653
                                   assignment_expr);
5654
}
5655
 
5656
/* Parse an assignment-expression.
5657
 
5658
   assignment-expression:
5659
     conditional-expression
5660
     logical-or-expression assignment-operator assignment_expression
5661
     throw-expression
5662
 
5663
   CAST_P is true if this expression is the target of a cast.
5664
 
5665
   Returns a representation for the expression.  */
5666
 
5667
static tree
5668
cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5669
{
5670
  tree expr;
5671
 
5672
  /* If the next token is the `throw' keyword, then we're looking at
5673
     a throw-expression.  */
5674
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5675
    expr = cp_parser_throw_expression (parser);
5676
  /* Otherwise, it must be that we are looking at a
5677
     logical-or-expression.  */
5678
  else
5679
    {
5680
      /* Parse the binary expressions (logical-or-expression).  */
5681
      expr = cp_parser_binary_expression (parser, cast_p);
5682
      /* If the next token is a `?' then we're actually looking at a
5683
         conditional-expression.  */
5684
      if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5685
        return cp_parser_question_colon_clause (parser, expr);
5686
      else
5687
        {
5688
          enum tree_code assignment_operator;
5689
 
5690
          /* If it's an assignment-operator, we're using the second
5691
             production.  */
5692
          assignment_operator
5693
            = cp_parser_assignment_operator_opt (parser);
5694
          if (assignment_operator != ERROR_MARK)
5695
            {
5696
              tree rhs;
5697
 
5698
              /* Parse the right-hand side of the assignment.  */
5699
              rhs = cp_parser_assignment_expression (parser, cast_p);
5700
              /* An assignment may not appear in a
5701
                 constant-expression.  */
5702
              if (cp_parser_non_integral_constant_expression (parser,
5703
                                                              "an assignment"))
5704
                return error_mark_node;
5705
              /* Build the assignment expression.  */
5706
              expr = build_x_modify_expr (expr,
5707
                                          assignment_operator,
5708
                                          rhs);
5709
            }
5710
        }
5711
    }
5712
 
5713
  return expr;
5714
}
5715
 
5716
/* Parse an (optional) assignment-operator.
5717
 
5718
   assignment-operator: one of
5719
     = *= /= %= += -= >>= <<= &= ^= |=
5720
 
5721
   GNU Extension:
5722
 
5723
   assignment-operator: one of
5724
     <?= >?=
5725
 
5726
   If the next token is an assignment operator, the corresponding tree
5727
   code is returned, and the token is consumed.  For example, for
5728
   `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5729
   NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5730
   TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5731
   operator, ERROR_MARK is returned.  */
5732
 
5733
static enum tree_code
5734
cp_parser_assignment_operator_opt (cp_parser* parser)
5735
{
5736
  enum tree_code op;
5737
  cp_token *token;
5738
 
5739
  /* Peek at the next toen.  */
5740
  token = cp_lexer_peek_token (parser->lexer);
5741
 
5742
  switch (token->type)
5743
    {
5744
    case CPP_EQ:
5745
      op = NOP_EXPR;
5746
      break;
5747
 
5748
    case CPP_MULT_EQ:
5749
      op = MULT_EXPR;
5750
      break;
5751
 
5752
    case CPP_DIV_EQ:
5753
      op = TRUNC_DIV_EXPR;
5754
      break;
5755
 
5756
    case CPP_MOD_EQ:
5757
      op = TRUNC_MOD_EXPR;
5758
      break;
5759
 
5760
    case CPP_PLUS_EQ:
5761
      op = PLUS_EXPR;
5762
      break;
5763
 
5764
    case CPP_MINUS_EQ:
5765
      op = MINUS_EXPR;
5766
      break;
5767
 
5768
    case CPP_RSHIFT_EQ:
5769
      op = RSHIFT_EXPR;
5770
      break;
5771
 
5772
    case CPP_LSHIFT_EQ:
5773
      op = LSHIFT_EXPR;
5774
      break;
5775
 
5776
    case CPP_AND_EQ:
5777
      op = BIT_AND_EXPR;
5778
      break;
5779
 
5780
    case CPP_XOR_EQ:
5781
      op = BIT_XOR_EXPR;
5782
      break;
5783
 
5784
    case CPP_OR_EQ:
5785
      op = BIT_IOR_EXPR;
5786
      break;
5787
 
5788
    case CPP_MIN_EQ:
5789
      op = MIN_EXPR;
5790
      cp_parser_warn_min_max ();
5791
      break;
5792
 
5793
    case CPP_MAX_EQ:
5794
      op = MAX_EXPR;
5795
      cp_parser_warn_min_max ();
5796
      break;
5797
 
5798
    default:
5799
      /* Nothing else is an assignment operator.  */
5800
      op = ERROR_MARK;
5801
    }
5802
 
5803
  /* If it was an assignment operator, consume it.  */
5804
  if (op != ERROR_MARK)
5805
    cp_lexer_consume_token (parser->lexer);
5806
 
5807
  return op;
5808
}
5809
 
5810
/* Parse an expression.
5811
 
5812
   expression:
5813
     assignment-expression
5814
     expression , assignment-expression
5815
 
5816
   CAST_P is true if this expression is the target of a cast.
5817
 
5818
   Returns a representation of the expression.  */
5819
 
5820
static tree
5821
cp_parser_expression (cp_parser* parser, bool cast_p)
5822
{
5823
  tree expression = NULL_TREE;
5824
 
5825
  while (true)
5826
    {
5827
      tree assignment_expression;
5828
 
5829
      /* Parse the next assignment-expression.  */
5830
      assignment_expression
5831
        = cp_parser_assignment_expression (parser, cast_p);
5832
      /* If this is the first assignment-expression, we can just
5833
         save it away.  */
5834
      if (!expression)
5835
        expression = assignment_expression;
5836
      else
5837
        expression = build_x_compound_expr (expression,
5838
                                            assignment_expression);
5839
      /* If the next token is not a comma, then we are done with the
5840
         expression.  */
5841
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5842
        break;
5843
      /* Consume the `,'.  */
5844
      cp_lexer_consume_token (parser->lexer);
5845
      /* A comma operator cannot appear in a constant-expression.  */
5846
      if (cp_parser_non_integral_constant_expression (parser,
5847
                                                      "a comma operator"))
5848
        expression = error_mark_node;
5849
    }
5850
 
5851
  return expression;
5852
}
5853
 
5854
/* Parse a constant-expression.
5855
 
5856
   constant-expression:
5857
     conditional-expression
5858
 
5859
  If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5860
  accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5861
  constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5862
  is false, NON_CONSTANT_P should be NULL.  */
5863
 
5864
static tree
5865
cp_parser_constant_expression (cp_parser* parser,
5866
                               bool allow_non_constant_p,
5867
                               bool *non_constant_p)
5868
{
5869
  bool saved_integral_constant_expression_p;
5870
  bool saved_allow_non_integral_constant_expression_p;
5871
  bool saved_non_integral_constant_expression_p;
5872
  tree expression;
5873
 
5874
  /* It might seem that we could simply parse the
5875
     conditional-expression, and then check to see if it were
5876
     TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5877
     one that the compiler can figure out is constant, possibly after
5878
     doing some simplifications or optimizations.  The standard has a
5879
     precise definition of constant-expression, and we must honor
5880
     that, even though it is somewhat more restrictive.
5881
 
5882
     For example:
5883
 
5884
       int i[(2, 3)];
5885
 
5886
     is not a legal declaration, because `(2, 3)' is not a
5887
     constant-expression.  The `,' operator is forbidden in a
5888
     constant-expression.  However, GCC's constant-folding machinery
5889
     will fold this operation to an INTEGER_CST for `3'.  */
5890
 
5891
  /* Save the old settings.  */
5892
  saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5893
  saved_allow_non_integral_constant_expression_p
5894
    = parser->allow_non_integral_constant_expression_p;
5895
  saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5896
  /* We are now parsing a constant-expression.  */
5897
  parser->integral_constant_expression_p = true;
5898
  parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5899
  parser->non_integral_constant_expression_p = false;
5900
  /* Although the grammar says "conditional-expression", we parse an
5901
     "assignment-expression", which also permits "throw-expression"
5902
     and the use of assignment operators.  In the case that
5903
     ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5904
     otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5905
     actually essential that we look for an assignment-expression.
5906
     For example, cp_parser_initializer_clauses uses this function to
5907
     determine whether a particular assignment-expression is in fact
5908
     constant.  */
5909
  expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5910
  /* Restore the old settings.  */
5911
  parser->integral_constant_expression_p
5912
    = saved_integral_constant_expression_p;
5913
  parser->allow_non_integral_constant_expression_p
5914
    = saved_allow_non_integral_constant_expression_p;
5915
  if (allow_non_constant_p)
5916
    *non_constant_p = parser->non_integral_constant_expression_p;
5917
  else if (parser->non_integral_constant_expression_p)
5918
    expression = error_mark_node;
5919
  parser->non_integral_constant_expression_p
5920
    = saved_non_integral_constant_expression_p;
5921
 
5922
  return expression;
5923
}
5924
 
5925
/* Parse __builtin_offsetof.
5926
 
5927
   offsetof-expression:
5928
     "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5929
 
5930
   offsetof-member-designator:
5931
     id-expression
5932
     | offsetof-member-designator "." id-expression
5933
     | offsetof-member-designator "[" expression "]"
5934
*/
5935
 
5936
static tree
5937
cp_parser_builtin_offsetof (cp_parser *parser)
5938
{
5939
  int save_ice_p, save_non_ice_p;
5940
  tree type, expr;
5941
  cp_id_kind dummy;
5942
 
5943
  /* We're about to accept non-integral-constant things, but will
5944
     definitely yield an integral constant expression.  Save and
5945
     restore these values around our local parsing.  */
5946
  save_ice_p = parser->integral_constant_expression_p;
5947
  save_non_ice_p = parser->non_integral_constant_expression_p;
5948
 
5949
  /* Consume the "__builtin_offsetof" token.  */
5950
  cp_lexer_consume_token (parser->lexer);
5951
  /* Consume the opening `('.  */
5952
  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5953
  /* Parse the type-id.  */
5954
  type = cp_parser_type_id (parser);
5955
  /* Look for the `,'.  */
5956
  cp_parser_require (parser, CPP_COMMA, "`,'");
5957
 
5958
  /* Build the (type *)null that begins the traditional offsetof macro.  */
5959
  expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5960
 
5961
  /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5962
  expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5963
                                                 true, &dummy);
5964
  while (true)
5965
    {
5966
      cp_token *token = cp_lexer_peek_token (parser->lexer);
5967
      switch (token->type)
5968
        {
5969
        case CPP_OPEN_SQUARE:
5970
          /* offsetof-member-designator "[" expression "]" */
5971
          expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5972
          break;
5973
 
5974
        case CPP_DOT:
5975
          /* offsetof-member-designator "." identifier */
5976
          cp_lexer_consume_token (parser->lexer);
5977
          expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5978
                                                         true, &dummy);
5979
          break;
5980
 
5981
        case CPP_CLOSE_PAREN:
5982
          /* Consume the ")" token.  */
5983
          cp_lexer_consume_token (parser->lexer);
5984
          goto success;
5985
 
5986
        default:
5987
          /* Error.  We know the following require will fail, but
5988
             that gives the proper error message.  */
5989
          cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5990
          cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5991
          expr = error_mark_node;
5992
          goto failure;
5993
        }
5994
    }
5995
 
5996
 success:
5997
  /* If we're processing a template, we can't finish the semantics yet.
5998
     Otherwise we can fold the entire expression now.  */
5999
  if (processing_template_decl)
6000
    expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6001
  else
6002
    expr = fold_offsetof (expr);
6003
 
6004
 failure:
6005
  parser->integral_constant_expression_p = save_ice_p;
6006
  parser->non_integral_constant_expression_p = save_non_ice_p;
6007
 
6008
  return expr;
6009
}
6010
 
6011
/* Statements [gram.stmt.stmt]  */
6012
 
6013
/* Parse a statement.
6014
 
6015
   statement:
6016
     labeled-statement
6017
     expression-statement
6018
     compound-statement
6019
     selection-statement
6020
     iteration-statement
6021
     jump-statement
6022
     declaration-statement
6023
     try-block  */
6024
 
6025
static void
6026
cp_parser_statement (cp_parser* parser, tree in_statement_expr)
6027
{
6028
  tree statement;
6029
  cp_token *token;
6030
  location_t statement_location;
6031
 
6032
  /* There is no statement yet.  */
6033
  statement = NULL_TREE;
6034
  /* Peek at the next token.  */
6035
  token = cp_lexer_peek_token (parser->lexer);
6036
  /* Remember the location of the first token in the statement.  */
6037
  statement_location = token->location;
6038
  /* If this is a keyword, then that will often determine what kind of
6039
     statement we have.  */
6040
  if (token->type == CPP_KEYWORD)
6041
    {
6042
      enum rid keyword = token->keyword;
6043
 
6044
      switch (keyword)
6045
        {
6046
        case RID_CASE:
6047
        case RID_DEFAULT:
6048
          statement = cp_parser_labeled_statement (parser,
6049
                                                   in_statement_expr);
6050
          break;
6051
 
6052
        case RID_IF:
6053
        case RID_SWITCH:
6054
          statement = cp_parser_selection_statement (parser);
6055
          break;
6056
 
6057
        case RID_WHILE:
6058
        case RID_DO:
6059
        case RID_FOR:
6060
          statement = cp_parser_iteration_statement (parser);
6061
          break;
6062
 
6063
        case RID_BREAK:
6064
        case RID_CONTINUE:
6065
        case RID_RETURN:
6066
        case RID_GOTO:
6067
          statement = cp_parser_jump_statement (parser);
6068
          break;
6069
 
6070
          /* Objective-C++ exception-handling constructs.  */
6071
        case RID_AT_TRY:
6072
        case RID_AT_CATCH:
6073
        case RID_AT_FINALLY:
6074
        case RID_AT_SYNCHRONIZED:
6075
        case RID_AT_THROW:
6076
          statement = cp_parser_objc_statement (parser);
6077
          break;
6078
 
6079
        case RID_TRY:
6080
          statement = cp_parser_try_block (parser);
6081
          break;
6082
 
6083
        default:
6084
          /* It might be a keyword like `int' that can start a
6085
             declaration-statement.  */
6086
          break;
6087
        }
6088
    }
6089
  else if (token->type == CPP_NAME)
6090
    {
6091
      /* If the next token is a `:', then we are looking at a
6092
         labeled-statement.  */
6093
      token = cp_lexer_peek_nth_token (parser->lexer, 2);
6094
      if (token->type == CPP_COLON)
6095
        statement = cp_parser_labeled_statement (parser, in_statement_expr);
6096
    }
6097
  /* Anything that starts with a `{' must be a compound-statement.  */
6098
  else if (token->type == CPP_OPEN_BRACE)
6099
    statement = cp_parser_compound_statement (parser, NULL, false);
6100
  /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6101
     a statement all its own.  */
6102
  else if (token->type == CPP_PRAGMA)
6103
    {
6104
      cp_lexer_handle_pragma (parser->lexer);
6105
      return;
6106
    }
6107
  else if (token->type == CPP_EOF)
6108
    {
6109
      cp_parser_error (parser, "expected statement");
6110
      return;
6111
    }
6112
 
6113
  /* Everything else must be a declaration-statement or an
6114
     expression-statement.  Try for the declaration-statement
6115
     first, unless we are looking at a `;', in which case we know that
6116
     we have an expression-statement.  */
6117
  if (!statement)
6118
    {
6119
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6120
        {
6121
          cp_parser_parse_tentatively (parser);
6122
          /* Try to parse the declaration-statement.  */
6123
          cp_parser_declaration_statement (parser);
6124
          /* If that worked, we're done.  */
6125
          if (cp_parser_parse_definitely (parser))
6126
            return;
6127
        }
6128
      /* Look for an expression-statement instead.  */
6129
      statement = cp_parser_expression_statement (parser, in_statement_expr);
6130
    }
6131
 
6132
  /* Set the line number for the statement.  */
6133
  if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6134
    SET_EXPR_LOCATION (statement, statement_location);
6135
}
6136
 
6137
/* Parse a labeled-statement.
6138
 
6139
   labeled-statement:
6140
     identifier : statement
6141
     case constant-expression : statement
6142
     default : statement
6143
 
6144
   GNU Extension:
6145
 
6146
   labeled-statement:
6147
     case constant-expression ... constant-expression : statement
6148
 
6149
   Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6150
   For an ordinary label, returns a LABEL_EXPR.  */
6151
 
6152
static tree
6153
cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6154
{
6155
  cp_token *token;
6156
  tree statement = error_mark_node;
6157
 
6158
  /* The next token should be an identifier.  */
6159
  token = cp_lexer_peek_token (parser->lexer);
6160
  if (token->type != CPP_NAME
6161
      && token->type != CPP_KEYWORD)
6162
    {
6163
      cp_parser_error (parser, "expected labeled-statement");
6164
      return error_mark_node;
6165
    }
6166
 
6167
  switch (token->keyword)
6168
    {
6169
    case RID_CASE:
6170
      {
6171
        tree expr, expr_hi;
6172
        cp_token *ellipsis;
6173
 
6174
        /* Consume the `case' token.  */
6175
        cp_lexer_consume_token (parser->lexer);
6176
        /* Parse the constant-expression.  */
6177
        expr = cp_parser_constant_expression (parser,
6178
                                              /*allow_non_constant_p=*/false,
6179
                                              NULL);
6180
 
6181
        ellipsis = cp_lexer_peek_token (parser->lexer);
6182
        if (ellipsis->type == CPP_ELLIPSIS)
6183
          {
6184
            /* Consume the `...' token.  */
6185
            cp_lexer_consume_token (parser->lexer);
6186
            expr_hi =
6187
              cp_parser_constant_expression (parser,
6188
                                             /*allow_non_constant_p=*/false,
6189
                                             NULL);
6190
            /* We don't need to emit warnings here, as the common code
6191
               will do this for us.  */
6192
          }
6193
        else
6194
          expr_hi = NULL_TREE;
6195
 
6196
        if (!parser->in_switch_statement_p)
6197
          error ("case label %qE not within a switch statement", expr);
6198
        else
6199
          statement = finish_case_label (expr, expr_hi);
6200
      }
6201
      break;
6202
 
6203
    case RID_DEFAULT:
6204
      /* Consume the `default' token.  */
6205
      cp_lexer_consume_token (parser->lexer);
6206
      if (!parser->in_switch_statement_p)
6207
        error ("case label not within a switch statement");
6208
      else
6209
        statement = finish_case_label (NULL_TREE, NULL_TREE);
6210
      break;
6211
 
6212
    default:
6213
      /* Anything else must be an ordinary label.  */
6214
      statement = finish_label_stmt (cp_parser_identifier (parser));
6215
      break;
6216
    }
6217
 
6218
  /* Require the `:' token.  */
6219
  cp_parser_require (parser, CPP_COLON, "`:'");
6220
  /* Parse the labeled statement.  */
6221
  cp_parser_statement (parser, in_statement_expr);
6222
 
6223
  /* Return the label, in the case of a `case' or `default' label.  */
6224
  return statement;
6225
}
6226
 
6227
/* Parse an expression-statement.
6228
 
6229
   expression-statement:
6230
     expression [opt] ;
6231
 
6232
   Returns the new EXPR_STMT -- or NULL_TREE if the expression
6233
   statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6234
   indicates whether this expression-statement is part of an
6235
   expression statement.  */
6236
 
6237
static tree
6238
cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6239
{
6240
  tree statement = NULL_TREE;
6241
 
6242
  /* If the next token is a ';', then there is no expression
6243
     statement.  */
6244
  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6245
    statement = cp_parser_expression (parser, /*cast_p=*/false);
6246
 
6247
  /* Consume the final `;'.  */
6248
  cp_parser_consume_semicolon_at_end_of_statement (parser);
6249
 
6250
  if (in_statement_expr
6251
      && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6252
    /* This is the final expression statement of a statement
6253
       expression.  */
6254
    statement = finish_stmt_expr_expr (statement, in_statement_expr);
6255
  else if (statement)
6256
    statement = finish_expr_stmt (statement);
6257
  else
6258
    finish_stmt ();
6259
 
6260
  return statement;
6261
}
6262
 
6263
/* Parse a compound-statement.
6264
 
6265
   compound-statement:
6266
     { statement-seq [opt] }
6267
 
6268
   Returns a tree representing the statement.  */
6269
 
6270
static tree
6271
cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6272
                              bool in_try)
6273
{
6274
  tree compound_stmt;
6275
 
6276
  /* Consume the `{'.  */
6277
  if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6278
    return error_mark_node;
6279
  /* Begin the compound-statement.  */
6280
  compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6281
  /* Parse an (optional) statement-seq.  */
6282
  cp_parser_statement_seq_opt (parser, in_statement_expr);
6283
  /* Finish the compound-statement.  */
6284
  finish_compound_stmt (compound_stmt);
6285
  /* Consume the `}'.  */
6286
  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6287
 
6288
  return compound_stmt;
6289
}
6290
 
6291
/* Parse an (optional) statement-seq.
6292
 
6293
   statement-seq:
6294
     statement
6295
     statement-seq [opt] statement  */
6296
 
6297
static void
6298
cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6299
{
6300
  /* Scan statements until there aren't any more.  */
6301
  while (true)
6302
    {
6303
      /* If we're looking at a `}', then we've run out of statements.  */
6304
      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6305
          || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6306
        break;
6307
 
6308
      /* Parse the statement.  */
6309
      cp_parser_statement (parser, in_statement_expr);
6310
    }
6311
}
6312
 
6313
/* Parse a selection-statement.
6314
 
6315
   selection-statement:
6316
     if ( condition ) statement
6317
     if ( condition ) statement else statement
6318
     switch ( condition ) statement
6319
 
6320
   Returns the new IF_STMT or SWITCH_STMT.  */
6321
 
6322
static tree
6323
cp_parser_selection_statement (cp_parser* parser)
6324
{
6325
  cp_token *token;
6326
  enum rid keyword;
6327
 
6328
  /* Peek at the next token.  */
6329
  token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6330
 
6331
  /* See what kind of keyword it is.  */
6332
  keyword = token->keyword;
6333
  switch (keyword)
6334
    {
6335
    case RID_IF:
6336
    case RID_SWITCH:
6337
      {
6338
        tree statement;
6339
        tree condition;
6340
 
6341
        /* Look for the `('.  */
6342
        if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6343
          {
6344
            cp_parser_skip_to_end_of_statement (parser);
6345
            return error_mark_node;
6346
          }
6347
 
6348
        /* Begin the selection-statement.  */
6349
        if (keyword == RID_IF)
6350
          statement = begin_if_stmt ();
6351
        else
6352
          statement = begin_switch_stmt ();
6353
 
6354
        /* Parse the condition.  */
6355
        condition = cp_parser_condition (parser);
6356
        /* Look for the `)'.  */
6357
        if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6358
          cp_parser_skip_to_closing_parenthesis (parser, true, false,
6359
                                                 /*consume_paren=*/true);
6360
 
6361
        if (keyword == RID_IF)
6362
          {
6363
            /* Add the condition.  */
6364
            finish_if_stmt_cond (condition, statement);
6365
 
6366
            /* Parse the then-clause.  */
6367
            cp_parser_implicitly_scoped_statement (parser);
6368
            finish_then_clause (statement);
6369
 
6370
            /* If the next token is `else', parse the else-clause.  */
6371
            if (cp_lexer_next_token_is_keyword (parser->lexer,
6372
                                                RID_ELSE))
6373
              {
6374
                /* Consume the `else' keyword.  */
6375
                cp_lexer_consume_token (parser->lexer);
6376
                begin_else_clause (statement);
6377
                /* Parse the else-clause.  */
6378
                cp_parser_implicitly_scoped_statement (parser);
6379
                finish_else_clause (statement);
6380
              }
6381
 
6382
            /* Now we're all done with the if-statement.  */
6383
            finish_if_stmt (statement);
6384
          }
6385
        else
6386
          {
6387
            bool in_switch_statement_p;
6388
 
6389
            /* Add the condition.  */
6390
            finish_switch_cond (condition, statement);
6391
 
6392
            /* Parse the body of the switch-statement.  */
6393
            in_switch_statement_p = parser->in_switch_statement_p;
6394
            parser->in_switch_statement_p = true;
6395
            cp_parser_implicitly_scoped_statement (parser);
6396
            parser->in_switch_statement_p = in_switch_statement_p;
6397
 
6398
            /* Now we're all done with the switch-statement.  */
6399
            finish_switch_stmt (statement);
6400
          }
6401
 
6402
        return statement;
6403
      }
6404
      break;
6405
 
6406
    default:
6407
      cp_parser_error (parser, "expected selection-statement");
6408
      return error_mark_node;
6409
    }
6410
}
6411
 
6412
/* Parse a condition.
6413
 
6414
   condition:
6415
     expression
6416
     type-specifier-seq declarator = assignment-expression
6417
 
6418
   GNU Extension:
6419
 
6420
   condition:
6421
     type-specifier-seq declarator asm-specification [opt]
6422
       attributes [opt] = assignment-expression
6423
 
6424
   Returns the expression that should be tested.  */
6425
 
6426
static tree
6427
cp_parser_condition (cp_parser* parser)
6428
{
6429
  cp_decl_specifier_seq type_specifiers;
6430
  const char *saved_message;
6431
 
6432
  /* Try the declaration first.  */
6433
  cp_parser_parse_tentatively (parser);
6434
  /* New types are not allowed in the type-specifier-seq for a
6435
     condition.  */
6436
  saved_message = parser->type_definition_forbidden_message;
6437
  parser->type_definition_forbidden_message
6438
    = "types may not be defined in conditions";
6439
  /* Parse the type-specifier-seq.  */
6440
  cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6441
                                &type_specifiers);
6442
  /* Restore the saved message.  */
6443
  parser->type_definition_forbidden_message = saved_message;
6444
  /* If all is well, we might be looking at a declaration.  */
6445
  if (!cp_parser_error_occurred (parser))
6446
    {
6447
      tree decl;
6448
      tree asm_specification;
6449
      tree attributes;
6450
      cp_declarator *declarator;
6451
      tree initializer = NULL_TREE;
6452
 
6453
      /* Parse the declarator.  */
6454
      declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6455
                                         /*ctor_dtor_or_conv_p=*/NULL,
6456
                                         /*parenthesized_p=*/NULL,
6457
                                         /*member_p=*/false);
6458
      /* Parse the attributes.  */
6459
      attributes = cp_parser_attributes_opt (parser);
6460
      /* Parse the asm-specification.  */
6461
      asm_specification = cp_parser_asm_specification_opt (parser);
6462
      /* If the next token is not an `=', then we might still be
6463
         looking at an expression.  For example:
6464
 
6465
           if (A(a).x)
6466
 
6467
         looks like a decl-specifier-seq and a declarator -- but then
6468
         there is no `=', so this is an expression.  */
6469
      cp_parser_require (parser, CPP_EQ, "`='");
6470
      /* If we did see an `=', then we are looking at a declaration
6471
         for sure.  */
6472
      if (cp_parser_parse_definitely (parser))
6473
        {
6474
          tree pushed_scope;
6475
          bool non_constant_p;
6476
 
6477
          /* Create the declaration.  */
6478
          decl = start_decl (declarator, &type_specifiers,
6479
                             /*initialized_p=*/true,
6480
                             attributes, /*prefix_attributes=*/NULL_TREE,
6481
                             &pushed_scope);
6482
          /* Parse the assignment-expression.  */
6483
          initializer
6484
            = cp_parser_constant_expression (parser,
6485
                                             /*allow_non_constant_p=*/true,
6486
                                             &non_constant_p);
6487
          if (!non_constant_p)
6488
            initializer = fold_non_dependent_expr (initializer);
6489
 
6490
          /* Process the initializer.  */
6491
          cp_finish_decl (decl,
6492
                          initializer, !non_constant_p,
6493
                          asm_specification,
6494
                          LOOKUP_ONLYCONVERTING);
6495
 
6496
          if (pushed_scope)
6497
            pop_scope (pushed_scope);
6498
 
6499
          return convert_from_reference (decl);
6500
        }
6501
    }
6502
  /* If we didn't even get past the declarator successfully, we are
6503
     definitely not looking at a declaration.  */
6504
  else
6505
    cp_parser_abort_tentative_parse (parser);
6506
 
6507
  /* Otherwise, we are looking at an expression.  */
6508
  return cp_parser_expression (parser, /*cast_p=*/false);
6509
}
6510
 
6511
/* Parse an iteration-statement.
6512
 
6513
   iteration-statement:
6514
     while ( condition ) statement
6515
     do statement while ( expression ) ;
6516
     for ( for-init-statement condition [opt] ; expression [opt] )
6517
       statement
6518
 
6519
   Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6520
 
6521
static tree
6522
cp_parser_iteration_statement (cp_parser* parser)
6523
{
6524
  cp_token *token;
6525
  enum rid keyword;
6526
  tree statement;
6527
  bool in_iteration_statement_p;
6528
 
6529
 
6530
  /* Peek at the next token.  */
6531
  token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6532
  if (!token)
6533
    return error_mark_node;
6534
 
6535
  /* Remember whether or not we are already within an iteration
6536
     statement.  */
6537
  in_iteration_statement_p = parser->in_iteration_statement_p;
6538
 
6539
  /* See what kind of keyword it is.  */
6540
  keyword = token->keyword;
6541
  switch (keyword)
6542
    {
6543
    case RID_WHILE:
6544
      {
6545
        tree condition;
6546
 
6547
        /* Begin the while-statement.  */
6548
        statement = begin_while_stmt ();
6549
        /* Look for the `('.  */
6550
        cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6551
        /* Parse the condition.  */
6552
        condition = cp_parser_condition (parser);
6553
        finish_while_stmt_cond (condition, statement);
6554
        /* Look for the `)'.  */
6555
        cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6556
        /* Parse the dependent statement.  */
6557
        parser->in_iteration_statement_p = true;
6558
        cp_parser_already_scoped_statement (parser);
6559
        parser->in_iteration_statement_p = in_iteration_statement_p;
6560
        /* We're done with the while-statement.  */
6561
        finish_while_stmt (statement);
6562
      }
6563
      break;
6564
 
6565
    case RID_DO:
6566
      {
6567
        tree expression;
6568
 
6569
        /* Begin the do-statement.  */
6570
        statement = begin_do_stmt ();
6571
        /* Parse the body of the do-statement.  */
6572
        parser->in_iteration_statement_p = true;
6573
        cp_parser_implicitly_scoped_statement (parser);
6574
        parser->in_iteration_statement_p = in_iteration_statement_p;
6575
        finish_do_body (statement);
6576
        /* Look for the `while' keyword.  */
6577
        cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6578
        /* Look for the `('.  */
6579
        cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6580
        /* Parse the expression.  */
6581
        expression = cp_parser_expression (parser, /*cast_p=*/false);
6582
        /* We're done with the do-statement.  */
6583
        finish_do_stmt (expression, statement);
6584
        /* Look for the `)'.  */
6585
        cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6586
        /* Look for the `;'.  */
6587
        cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6588
      }
6589
      break;
6590
 
6591
    case RID_FOR:
6592
      {
6593
        tree condition = NULL_TREE;
6594
        tree expression = NULL_TREE;
6595
 
6596
        /* Begin the for-statement.  */
6597
        statement = begin_for_stmt ();
6598
        /* Look for the `('.  */
6599
        cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6600
        /* Parse the initialization.  */
6601
        cp_parser_for_init_statement (parser);
6602
        finish_for_init_stmt (statement);
6603
 
6604
        /* If there's a condition, process it.  */
6605
        if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6606
          condition = cp_parser_condition (parser);
6607
        finish_for_cond (condition, statement);
6608
        /* Look for the `;'.  */
6609
        cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6610
 
6611
        /* If there's an expression, process it.  */
6612
        if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6613
          expression = cp_parser_expression (parser, /*cast_p=*/false);
6614
        finish_for_expr (expression, statement);
6615
        /* Look for the `)'.  */
6616
        cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6617
 
6618
        /* Parse the body of the for-statement.  */
6619
        parser->in_iteration_statement_p = true;
6620
        cp_parser_already_scoped_statement (parser);
6621
        parser->in_iteration_statement_p = in_iteration_statement_p;
6622
 
6623
        /* We're done with the for-statement.  */
6624
        finish_for_stmt (statement);
6625
      }
6626
      break;
6627
 
6628
    default:
6629
      cp_parser_error (parser, "expected iteration-statement");
6630
      statement = error_mark_node;
6631
      break;
6632
    }
6633
 
6634
  return statement;
6635
}
6636
 
6637
/* Parse a for-init-statement.
6638
 
6639
   for-init-statement:
6640
     expression-statement
6641
     simple-declaration  */
6642
 
6643
static void
6644
cp_parser_for_init_statement (cp_parser* parser)
6645
{
6646
  /* If the next token is a `;', then we have an empty
6647
     expression-statement.  Grammatically, this is also a
6648
     simple-declaration, but an invalid one, because it does not
6649
     declare anything.  Therefore, if we did not handle this case
6650
     specially, we would issue an error message about an invalid
6651
     declaration.  */
6652
  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6653
    {
6654
      /* We're going to speculatively look for a declaration, falling back
6655
         to an expression, if necessary.  */
6656
      cp_parser_parse_tentatively (parser);
6657
      /* Parse the declaration.  */
6658
      cp_parser_simple_declaration (parser,
6659
                                    /*function_definition_allowed_p=*/false);
6660
      /* If the tentative parse failed, then we shall need to look for an
6661
         expression-statement.  */
6662
      if (cp_parser_parse_definitely (parser))
6663
        return;
6664
    }
6665
 
6666
  cp_parser_expression_statement (parser, false);
6667
}
6668
 
6669
/* Parse a jump-statement.
6670
 
6671
   jump-statement:
6672
     break ;
6673
     continue ;
6674
     return expression [opt] ;
6675
     goto identifier ;
6676
 
6677
   GNU extension:
6678
 
6679
   jump-statement:
6680
     goto * expression ;
6681
 
6682
   Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6683
 
6684
static tree
6685
cp_parser_jump_statement (cp_parser* parser)
6686
{
6687
  tree statement = error_mark_node;
6688
  cp_token *token;
6689
  enum rid keyword;
6690
 
6691
  /* Peek at the next token.  */
6692
  token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6693
  if (!token)
6694
    return error_mark_node;
6695
 
6696
  /* See what kind of keyword it is.  */
6697
  keyword = token->keyword;
6698
  switch (keyword)
6699
    {
6700
    case RID_BREAK:
6701
      if (!parser->in_switch_statement_p
6702
          && !parser->in_iteration_statement_p)
6703
        {
6704
          error ("break statement not within loop or switch");
6705
          statement = error_mark_node;
6706
        }
6707
      else
6708
        statement = finish_break_stmt ();
6709
      cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6710
      break;
6711
 
6712
    case RID_CONTINUE:
6713
      if (!parser->in_iteration_statement_p)
6714
        {
6715
          error ("continue statement not within a loop");
6716
          statement = error_mark_node;
6717
        }
6718
      else
6719
        statement = finish_continue_stmt ();
6720
      cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6721
      break;
6722
 
6723
    case RID_RETURN:
6724
      {
6725
        tree expr;
6726
 
6727
        /* If the next token is a `;', then there is no
6728
           expression.  */
6729
        if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6730
          expr = cp_parser_expression (parser, /*cast_p=*/false);
6731
        else
6732
          expr = NULL_TREE;
6733
        /* Build the return-statement.  */
6734
        statement = finish_return_stmt (expr);
6735
        /* Look for the final `;'.  */
6736
        cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6737
      }
6738
      break;
6739
 
6740
    case RID_GOTO:
6741
      /* Create the goto-statement.  */
6742
      if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6743
        {
6744
          /* Issue a warning about this use of a GNU extension.  */
6745
          if (pedantic)
6746
            pedwarn ("ISO C++ forbids computed gotos");
6747
          /* Consume the '*' token.  */
6748
          cp_lexer_consume_token (parser->lexer);
6749
          /* Parse the dependent expression.  */
6750
          finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6751
        }
6752
      else
6753
        finish_goto_stmt (cp_parser_identifier (parser));
6754
      /* Look for the final `;'.  */
6755
      cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6756
      break;
6757
 
6758
    default:
6759
      cp_parser_error (parser, "expected jump-statement");
6760
      break;
6761
    }
6762
 
6763
  return statement;
6764
}
6765
 
6766
/* Parse a declaration-statement.
6767
 
6768
   declaration-statement:
6769
     block-declaration  */
6770
 
6771
static void
6772
cp_parser_declaration_statement (cp_parser* parser)
6773
{
6774
  void *p;
6775
 
6776
  /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6777
  p = obstack_alloc (&declarator_obstack, 0);
6778
 
6779
 /* Parse the block-declaration.  */
6780
  cp_parser_block_declaration (parser, /*statement_p=*/true);
6781
 
6782
  /* Free any declarators allocated.  */
6783
  obstack_free (&declarator_obstack, p);
6784
 
6785
  /* Finish off the statement.  */
6786
  finish_stmt ();
6787
}
6788
 
6789
/* Some dependent statements (like `if (cond) statement'), are
6790
   implicitly in their own scope.  In other words, if the statement is
6791
   a single statement (as opposed to a compound-statement), it is
6792
   none-the-less treated as if it were enclosed in braces.  Any
6793
   declarations appearing in the dependent statement are out of scope
6794
   after control passes that point.  This function parses a statement,
6795
   but ensures that is in its own scope, even if it is not a
6796
   compound-statement.
6797
 
6798
   Returns the new statement.  */
6799
 
6800
static tree
6801
cp_parser_implicitly_scoped_statement (cp_parser* parser)
6802
{
6803
  tree statement;
6804
 
6805
  /* If the token is not a `{', then we must take special action.  */
6806
  if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6807
    {
6808
      /* Create a compound-statement.  */
6809
      statement = begin_compound_stmt (0);
6810
      /* Parse the dependent-statement.  */
6811
      cp_parser_statement (parser, false);
6812
      /* Finish the dummy compound-statement.  */
6813
      finish_compound_stmt (statement);
6814
    }
6815
  /* Otherwise, we simply parse the statement directly.  */
6816
  else
6817
    statement = cp_parser_compound_statement (parser, NULL, false);
6818
 
6819
  /* Return the statement.  */
6820
  return statement;
6821
}
6822
 
6823
/* For some dependent statements (like `while (cond) statement'), we
6824
   have already created a scope.  Therefore, even if the dependent
6825
   statement is a compound-statement, we do not want to create another
6826
   scope.  */
6827
 
6828
static void
6829
cp_parser_already_scoped_statement (cp_parser* parser)
6830
{
6831
  /* If the token is a `{', then we must take special action.  */
6832
  if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6833
    cp_parser_statement (parser, false);
6834
  else
6835
    {
6836
      /* Avoid calling cp_parser_compound_statement, so that we
6837
         don't create a new scope.  Do everything else by hand.  */
6838
      cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6839
      cp_parser_statement_seq_opt (parser, false);
6840
      cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6841
    }
6842
}
6843
 
6844
/* Declarations [gram.dcl.dcl] */
6845
 
6846
/* Parse an optional declaration-sequence.
6847
 
6848
   declaration-seq:
6849
     declaration
6850
     declaration-seq declaration  */
6851
 
6852
static void
6853
cp_parser_declaration_seq_opt (cp_parser* parser)
6854
{
6855
  while (true)
6856
    {
6857
      cp_token *token;
6858
 
6859
      token = cp_lexer_peek_token (parser->lexer);
6860
 
6861
      if (token->type == CPP_CLOSE_BRACE
6862
          || token->type == CPP_EOF)
6863
        break;
6864
 
6865
      if (token->type == CPP_SEMICOLON)
6866
        {
6867
          /* A declaration consisting of a single semicolon is
6868
             invalid.  Allow it unless we're being pedantic.  */
6869
          cp_lexer_consume_token (parser->lexer);
6870
          if (pedantic && !in_system_header)
6871
            pedwarn ("extra %<;%>");
6872
          continue;
6873
        }
6874
 
6875
      /* If we're entering or exiting a region that's implicitly
6876
         extern "C", modify the lang context appropriately.  */
6877
      if (!parser->implicit_extern_c && token->implicit_extern_c)
6878
        {
6879
          push_lang_context (lang_name_c);
6880
          parser->implicit_extern_c = true;
6881
        }
6882
      else if (parser->implicit_extern_c && !token->implicit_extern_c)
6883
        {
6884
          pop_lang_context ();
6885
          parser->implicit_extern_c = false;
6886
        }
6887
 
6888
      if (token->type == CPP_PRAGMA)
6889
        {
6890
          /* A top-level declaration can consist solely of a #pragma.
6891
             A nested declaration cannot, so this is done here and not
6892
             in cp_parser_declaration.  (A #pragma at block scope is
6893
             handled in cp_parser_statement.)  */
6894
          cp_lexer_handle_pragma (parser->lexer);
6895
          continue;
6896
        }
6897
 
6898
      /* Parse the declaration itself.  */
6899
      cp_parser_declaration (parser);
6900
    }
6901
}
6902
 
6903
/* Parse a declaration.
6904
 
6905
   declaration:
6906
     block-declaration
6907
     function-definition
6908
     template-declaration
6909
     explicit-instantiation
6910
     explicit-specialization
6911
     linkage-specification
6912
     namespace-definition
6913
 
6914
   GNU extension:
6915
 
6916
   declaration:
6917
      __extension__ declaration */
6918
 
6919
static void
6920
cp_parser_declaration (cp_parser* parser)
6921
{
6922
  cp_token token1;
6923
  cp_token token2;
6924
  int saved_pedantic;
6925
  void *p;
6926
 
6927
  /* Check for the `__extension__' keyword.  */
6928
  if (cp_parser_extension_opt (parser, &saved_pedantic))
6929
    {
6930
      /* Parse the qualified declaration.  */
6931
      cp_parser_declaration (parser);
6932
      /* Restore the PEDANTIC flag.  */
6933
      pedantic = saved_pedantic;
6934
 
6935
      return;
6936
    }
6937
 
6938
  /* Try to figure out what kind of declaration is present.  */
6939
  token1 = *cp_lexer_peek_token (parser->lexer);
6940
 
6941
  if (token1.type != CPP_EOF)
6942
    token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6943
  else
6944
    token2.type = token2.keyword = RID_MAX;
6945
 
6946
  /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6947
  p = obstack_alloc (&declarator_obstack, 0);
6948
 
6949
  /* If the next token is `extern' and the following token is a string
6950
     literal, then we have a linkage specification.  */
6951
  if (token1.keyword == RID_EXTERN
6952
      && cp_parser_is_string_literal (&token2))
6953
    cp_parser_linkage_specification (parser);
6954
  /* If the next token is `template', then we have either a template
6955
     declaration, an explicit instantiation, or an explicit
6956
     specialization.  */
6957
  else if (token1.keyword == RID_TEMPLATE)
6958
    {
6959
      /* `template <>' indicates a template specialization.  */
6960
      if (token2.type == CPP_LESS
6961
          && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6962
        cp_parser_explicit_specialization (parser);
6963
      /* `template <' indicates a template declaration.  */
6964
      else if (token2.type == CPP_LESS)
6965
        cp_parser_template_declaration (parser, /*member_p=*/false);
6966
      /* Anything else must be an explicit instantiation.  */
6967
      else
6968
        cp_parser_explicit_instantiation (parser);
6969
    }
6970
  /* If the next token is `export', then we have a template
6971
     declaration.  */
6972
  else if (token1.keyword == RID_EXPORT)
6973
    cp_parser_template_declaration (parser, /*member_p=*/false);
6974
  /* If the next token is `extern', 'static' or 'inline' and the one
6975
     after that is `template', we have a GNU extended explicit
6976
     instantiation directive.  */
6977
  else if (cp_parser_allow_gnu_extensions_p (parser)
6978
           && (token1.keyword == RID_EXTERN
6979
               || token1.keyword == RID_STATIC
6980
               || token1.keyword == RID_INLINE)
6981
           && token2.keyword == RID_TEMPLATE)
6982
    cp_parser_explicit_instantiation (parser);
6983
  /* If the next token is `namespace', check for a named or unnamed
6984
     namespace definition.  */
6985
  else if (token1.keyword == RID_NAMESPACE
6986
           && (/* A named namespace definition.  */
6987
               (token2.type == CPP_NAME
6988
                && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6989
                    == CPP_OPEN_BRACE))
6990
               /* An unnamed namespace definition.  */
6991
               || token2.type == CPP_OPEN_BRACE))
6992
    cp_parser_namespace_definition (parser);
6993
  /* Objective-C++ declaration/definition.  */
6994
  else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
6995
    cp_parser_objc_declaration (parser);
6996
  /* We must have either a block declaration or a function
6997
     definition.  */
6998
  else
6999
    /* Try to parse a block-declaration, or a function-definition.  */
7000
    cp_parser_block_declaration (parser, /*statement_p=*/false);
7001
 
7002
  /* Free any declarators allocated.  */
7003
  obstack_free (&declarator_obstack, p);
7004
}
7005
 
7006
/* Parse a block-declaration.
7007
 
7008
   block-declaration:
7009
     simple-declaration
7010
     asm-definition
7011
     namespace-alias-definition
7012
     using-declaration
7013
     using-directive
7014
 
7015
   GNU Extension:
7016
 
7017
   block-declaration:
7018
     __extension__ block-declaration
7019
     label-declaration
7020
 
7021
   If STATEMENT_P is TRUE, then this block-declaration is occurring as
7022
   part of a declaration-statement.  */
7023
 
7024
static void
7025
cp_parser_block_declaration (cp_parser *parser,
7026
                             bool      statement_p)
7027
{
7028
  cp_token *token1;
7029
  int saved_pedantic;
7030
 
7031
  /* Check for the `__extension__' keyword.  */
7032
  if (cp_parser_extension_opt (parser, &saved_pedantic))
7033
    {
7034
      /* Parse the qualified declaration.  */
7035
      cp_parser_block_declaration (parser, statement_p);
7036
      /* Restore the PEDANTIC flag.  */
7037
      pedantic = saved_pedantic;
7038
 
7039
      return;
7040
    }
7041
 
7042
  /* Peek at the next token to figure out which kind of declaration is
7043
     present.  */
7044
  token1 = cp_lexer_peek_token (parser->lexer);
7045
 
7046
  /* If the next keyword is `asm', we have an asm-definition.  */
7047
  if (token1->keyword == RID_ASM)
7048
    {
7049
      if (statement_p)
7050
        cp_parser_commit_to_tentative_parse (parser);
7051
      cp_parser_asm_definition (parser);
7052
    }
7053
  /* If the next keyword is `namespace', we have a
7054
     namespace-alias-definition.  */
7055
  else if (token1->keyword == RID_NAMESPACE)
7056
    cp_parser_namespace_alias_definition (parser);
7057
  /* If the next keyword is `using', we have either a
7058
     using-declaration or a using-directive.  */
7059
  else if (token1->keyword == RID_USING)
7060
    {
7061
      cp_token *token2;
7062
 
7063
      if (statement_p)
7064
        cp_parser_commit_to_tentative_parse (parser);
7065
      /* If the token after `using' is `namespace', then we have a
7066
         using-directive.  */
7067
      token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7068
      if (token2->keyword == RID_NAMESPACE)
7069
        cp_parser_using_directive (parser);
7070
      /* Otherwise, it's a using-declaration.  */
7071
      else
7072
        cp_parser_using_declaration (parser);
7073
    }
7074
  /* If the next keyword is `__label__' we have a label declaration.  */
7075
  else if (token1->keyword == RID_LABEL)
7076
    {
7077
      if (statement_p)
7078
        cp_parser_commit_to_tentative_parse (parser);
7079
      cp_parser_label_declaration (parser);
7080
    }
7081
  /* Anything else must be a simple-declaration.  */
7082
  else
7083
    cp_parser_simple_declaration (parser, !statement_p);
7084
}
7085
 
7086
/* Parse a simple-declaration.
7087
 
7088
   simple-declaration:
7089
     decl-specifier-seq [opt] init-declarator-list [opt] ;
7090
 
7091
   init-declarator-list:
7092
     init-declarator
7093
     init-declarator-list , init-declarator
7094
 
7095
   If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7096
   function-definition as a simple-declaration.  */
7097
 
7098
static void
7099
cp_parser_simple_declaration (cp_parser* parser,
7100
                              bool function_definition_allowed_p)
7101
{
7102
  cp_decl_specifier_seq decl_specifiers;
7103
  int declares_class_or_enum;
7104
  bool saw_declarator;
7105
 
7106
  /* Defer access checks until we know what is being declared; the
7107
     checks for names appearing in the decl-specifier-seq should be
7108
     done as if we were in the scope of the thing being declared.  */
7109
  push_deferring_access_checks (dk_deferred);
7110
 
7111
  /* Parse the decl-specifier-seq.  We have to keep track of whether
7112
     or not the decl-specifier-seq declares a named class or
7113
     enumeration type, since that is the only case in which the
7114
     init-declarator-list is allowed to be empty.
7115
 
7116
     [dcl.dcl]
7117
 
7118
     In a simple-declaration, the optional init-declarator-list can be
7119
     omitted only when declaring a class or enumeration, that is when
7120
     the decl-specifier-seq contains either a class-specifier, an
7121
     elaborated-type-specifier, or an enum-specifier.  */
7122
  cp_parser_decl_specifier_seq (parser,
7123
                                CP_PARSER_FLAGS_OPTIONAL,
7124
                                &decl_specifiers,
7125
                                &declares_class_or_enum);
7126
  /* We no longer need to defer access checks.  */
7127
  stop_deferring_access_checks ();
7128
 
7129
  /* In a block scope, a valid declaration must always have a
7130
     decl-specifier-seq.  By not trying to parse declarators, we can
7131
     resolve the declaration/expression ambiguity more quickly.  */
7132
  if (!function_definition_allowed_p
7133
      && !decl_specifiers.any_specifiers_p)
7134
    {
7135
      cp_parser_error (parser, "expected declaration");
7136
      goto done;
7137
    }
7138
 
7139
  /* If the next two tokens are both identifiers, the code is
7140
     erroneous. The usual cause of this situation is code like:
7141
 
7142
       T t;
7143
 
7144
     where "T" should name a type -- but does not.  */
7145
  if (!decl_specifiers.type
7146
      && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7147
    {
7148
      /* If parsing tentatively, we should commit; we really are
7149
         looking at a declaration.  */
7150
      cp_parser_commit_to_tentative_parse (parser);
7151
      /* Give up.  */
7152
      goto done;
7153
    }
7154
 
7155
  /* If we have seen at least one decl-specifier, and the next token
7156
     is not a parenthesis, then we must be looking at a declaration.
7157
     (After "int (" we might be looking at a functional cast.)  */
7158
  if (decl_specifiers.any_specifiers_p
7159
      && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7160
    cp_parser_commit_to_tentative_parse (parser);
7161
 
7162
  /* Keep going until we hit the `;' at the end of the simple
7163
     declaration.  */
7164
  saw_declarator = false;
7165
  while (cp_lexer_next_token_is_not (parser->lexer,
7166
                                     CPP_SEMICOLON))
7167
    {
7168
      cp_token *token;
7169
      bool function_definition_p;
7170
      tree decl;
7171
 
7172
      if (saw_declarator)
7173
        {
7174
          /* If we are processing next declarator, coma is expected */
7175
          token = cp_lexer_peek_token (parser->lexer);
7176
          gcc_assert (token->type == CPP_COMMA);
7177
          cp_lexer_consume_token (parser->lexer);
7178
        }
7179
      else
7180
        saw_declarator = true;
7181
 
7182
      /* Parse the init-declarator.  */
7183
      decl = cp_parser_init_declarator (parser, &decl_specifiers,
7184
                                        /*checks=*/NULL_TREE,
7185
                                        function_definition_allowed_p,
7186
                                        /*member_p=*/false,
7187
                                        declares_class_or_enum,
7188
                                        &function_definition_p);
7189
      /* If an error occurred while parsing tentatively, exit quickly.
7190
         (That usually happens when in the body of a function; each
7191
         statement is treated as a declaration-statement until proven
7192
         otherwise.)  */
7193
      if (cp_parser_error_occurred (parser))
7194
        goto done;
7195
      /* Handle function definitions specially.  */
7196
      if (function_definition_p)
7197
        {
7198
          /* If the next token is a `,', then we are probably
7199
             processing something like:
7200
 
7201
               void f() {}, *p;
7202
 
7203
             which is erroneous.  */
7204
          if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7205
            error ("mixing declarations and function-definitions is forbidden");
7206
          /* Otherwise, we're done with the list of declarators.  */
7207
          else
7208
            {
7209
              pop_deferring_access_checks ();
7210
              return;
7211
            }
7212
        }
7213
      /* The next token should be either a `,' or a `;'.  */
7214
      token = cp_lexer_peek_token (parser->lexer);
7215
      /* If it's a `,', there are more declarators to come.  */
7216
      if (token->type == CPP_COMMA)
7217
        /* will be consumed next time around */;
7218
      /* If it's a `;', we are done.  */
7219
      else if (token->type == CPP_SEMICOLON)
7220
        break;
7221
      /* Anything else is an error.  */
7222
      else
7223
        {
7224
          /* If we have already issued an error message we don't need
7225
             to issue another one.  */
7226
          if (decl != error_mark_node
7227
              || cp_parser_uncommitted_to_tentative_parse_p (parser))
7228
            cp_parser_error (parser, "expected %<,%> or %<;%>");
7229
          /* Skip tokens until we reach the end of the statement.  */
7230
          cp_parser_skip_to_end_of_statement (parser);
7231
          /* If the next token is now a `;', consume it.  */
7232
          if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7233
            cp_lexer_consume_token (parser->lexer);
7234
          goto done;
7235
        }
7236
      /* After the first time around, a function-definition is not
7237
         allowed -- even if it was OK at first.  For example:
7238
 
7239
           int i, f() {}
7240
 
7241
         is not valid.  */
7242
      function_definition_allowed_p = false;
7243
    }
7244
 
7245
  /* Issue an error message if no declarators are present, and the
7246
     decl-specifier-seq does not itself declare a class or
7247
     enumeration.  */
7248
  if (!saw_declarator)
7249
    {
7250
      if (cp_parser_declares_only_class_p (parser))
7251
        shadow_tag (&decl_specifiers);
7252
      /* Perform any deferred access checks.  */
7253
      perform_deferred_access_checks ();
7254
    }
7255
 
7256
  /* Consume the `;'.  */
7257
  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7258
 
7259
 done:
7260
  pop_deferring_access_checks ();
7261
}
7262
 
7263
/* Parse a decl-specifier-seq.
7264
 
7265
   decl-specifier-seq:
7266
     decl-specifier-seq [opt] decl-specifier
7267
 
7268
   decl-specifier:
7269
     storage-class-specifier
7270
     type-specifier
7271
     function-specifier
7272
     friend
7273
     typedef
7274
 
7275
   GNU Extension:
7276
 
7277
   decl-specifier:
7278
     attributes
7279
 
7280
   Set *DECL_SPECS to a representation of the decl-specifier-seq.
7281
 
7282
   The parser flags FLAGS is used to control type-specifier parsing.
7283
 
7284
   *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7285
   flags:
7286
 
7287
     1: one of the decl-specifiers is an elaborated-type-specifier
7288
        (i.e., a type declaration)
7289
     2: one of the decl-specifiers is an enum-specifier or a
7290
        class-specifier (i.e., a type definition)
7291
 
7292
   */
7293
 
7294
static void
7295
cp_parser_decl_specifier_seq (cp_parser* parser,
7296
                              cp_parser_flags flags,
7297
                              cp_decl_specifier_seq *decl_specs,
7298
                              int* declares_class_or_enum)
7299
{
7300
  bool constructor_possible_p = !parser->in_declarator_p;
7301
  cp_decl_spec ds;
7302
 
7303
  /* Clear DECL_SPECS.  */
7304
  clear_decl_specs (decl_specs);
7305
 
7306
  /* Assume no class or enumeration type is declared.  */
7307
  *declares_class_or_enum = 0;
7308
 
7309
  /* Keep reading specifiers until there are no more to read.  */
7310
  while (true)
7311
    {
7312
      bool constructor_p;
7313
      bool found_decl_spec;
7314
      cp_token *token;
7315
 
7316
      /* Peek at the next token.  */
7317
      token = cp_lexer_peek_token (parser->lexer);
7318
      /* Handle attributes.  */
7319
      if (token->keyword == RID_ATTRIBUTE)
7320
        {
7321
          /* Parse the attributes.  */
7322
          decl_specs->attributes
7323
            = chainon (decl_specs->attributes,
7324
                       cp_parser_attributes_opt (parser));
7325
          continue;
7326
        }
7327
      /* Assume we will find a decl-specifier keyword.  */
7328
      found_decl_spec = true;
7329
      /* If the next token is an appropriate keyword, we can simply
7330
         add it to the list.  */
7331
      switch (token->keyword)
7332
        {
7333
          /* decl-specifier:
7334
               friend  */
7335
        case RID_FRIEND:
7336
          ++decl_specs->specs[(int) ds_friend];
7337
          /* Consume the token.  */
7338
          cp_lexer_consume_token (parser->lexer);
7339
          break;
7340
 
7341
          /* function-specifier:
7342
               inline
7343
               virtual
7344
               explicit  */
7345
        case RID_INLINE:
7346
        case RID_VIRTUAL:
7347
        case RID_EXPLICIT:
7348
          cp_parser_function_specifier_opt (parser, decl_specs);
7349
          break;
7350
 
7351
          /* decl-specifier:
7352
               typedef  */
7353
        case RID_TYPEDEF:
7354
          ++decl_specs->specs[(int) ds_typedef];
7355
          /* Consume the token.  */
7356
          cp_lexer_consume_token (parser->lexer);
7357
          /* A constructor declarator cannot appear in a typedef.  */
7358
          constructor_possible_p = false;
7359
          /* The "typedef" keyword can only occur in a declaration; we
7360
             may as well commit at this point.  */
7361
          cp_parser_commit_to_tentative_parse (parser);
7362
          break;
7363
 
7364
          /* storage-class-specifier:
7365
               auto
7366
               register
7367
               static
7368
               extern
7369
               mutable
7370
 
7371
             GNU Extension:
7372
               thread  */
7373
        case RID_AUTO:
7374
          /* Consume the token.  */
7375
          cp_lexer_consume_token (parser->lexer);
7376
          cp_parser_set_storage_class (decl_specs, sc_auto);
7377
          break;
7378
        case RID_REGISTER:
7379
          /* Consume the token.  */
7380
          cp_lexer_consume_token (parser->lexer);
7381
          cp_parser_set_storage_class (decl_specs, sc_register);
7382
          break;
7383
        case RID_STATIC:
7384
          /* Consume the token.  */
7385
          cp_lexer_consume_token (parser->lexer);
7386
          if (decl_specs->specs[(int) ds_thread])
7387
            {
7388
              error ("%<__thread%> before %<static%>");
7389
              decl_specs->specs[(int) ds_thread] = 0;
7390
            }
7391
          cp_parser_set_storage_class (decl_specs, sc_static);
7392
          break;
7393
        case RID_EXTERN:
7394
          /* Consume the token.  */
7395
          cp_lexer_consume_token (parser->lexer);
7396
          if (decl_specs->specs[(int) ds_thread])
7397
            {
7398
              error ("%<__thread%> before %<extern%>");
7399
              decl_specs->specs[(int) ds_thread] = 0;
7400
            }
7401
          cp_parser_set_storage_class (decl_specs, sc_extern);
7402
          break;
7403
        case RID_MUTABLE:
7404
          /* Consume the token.  */
7405
          cp_lexer_consume_token (parser->lexer);
7406
          cp_parser_set_storage_class (decl_specs, sc_mutable);
7407
          break;
7408
        case RID_THREAD:
7409
          /* Consume the token.  */
7410
          cp_lexer_consume_token (parser->lexer);
7411
          ++decl_specs->specs[(int) ds_thread];
7412
          break;
7413
 
7414
        default:
7415
          /* We did not yet find a decl-specifier yet.  */
7416
          found_decl_spec = false;
7417
          break;
7418
        }
7419
 
7420
      /* Constructors are a special case.  The `S' in `S()' is not a
7421
         decl-specifier; it is the beginning of the declarator.  */
7422
      constructor_p
7423
        = (!found_decl_spec
7424
           && constructor_possible_p
7425
           && (cp_parser_constructor_declarator_p
7426
               (parser, decl_specs->specs[(int) ds_friend] != 0)));
7427
 
7428
      /* If we don't have a DECL_SPEC yet, then we must be looking at
7429
         a type-specifier.  */
7430
      if (!found_decl_spec && !constructor_p)
7431
        {
7432
          int decl_spec_declares_class_or_enum;
7433
          bool is_cv_qualifier;
7434
          tree type_spec;
7435
 
7436
          type_spec
7437
            = cp_parser_type_specifier (parser, flags,
7438
                                        decl_specs,
7439
                                        /*is_declaration=*/true,
7440
                                        &decl_spec_declares_class_or_enum,
7441
                                        &is_cv_qualifier);
7442
 
7443
          *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7444
 
7445
          /* If this type-specifier referenced a user-defined type
7446
             (a typedef, class-name, etc.), then we can't allow any
7447
             more such type-specifiers henceforth.
7448
 
7449
             [dcl.spec]
7450
 
7451
             The longest sequence of decl-specifiers that could
7452
             possibly be a type name is taken as the
7453
             decl-specifier-seq of a declaration.  The sequence shall
7454
             be self-consistent as described below.
7455
 
7456
             [dcl.type]
7457
 
7458
             As a general rule, at most one type-specifier is allowed
7459
             in the complete decl-specifier-seq of a declaration.  The
7460
             only exceptions are the following:
7461
 
7462
             -- const or volatile can be combined with any other
7463
                type-specifier.
7464
 
7465
             -- signed or unsigned can be combined with char, long,
7466
                short, or int.
7467
 
7468
             -- ..
7469
 
7470
             Example:
7471
 
7472
               typedef char* Pc;
7473
               void g (const int Pc);
7474
 
7475
             Here, Pc is *not* part of the decl-specifier seq; it's
7476
             the declarator.  Therefore, once we see a type-specifier
7477
             (other than a cv-qualifier), we forbid any additional
7478
             user-defined types.  We *do* still allow things like `int
7479
             int' to be considered a decl-specifier-seq, and issue the
7480
             error message later.  */
7481
          if (type_spec && !is_cv_qualifier)
7482
            flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7483
          /* A constructor declarator cannot follow a type-specifier.  */
7484
          if (type_spec)
7485
            {
7486
              constructor_possible_p = false;
7487
              found_decl_spec = true;
7488
            }
7489
        }
7490
 
7491
      /* If we still do not have a DECL_SPEC, then there are no more
7492
         decl-specifiers.  */
7493
      if (!found_decl_spec)
7494
        break;
7495
 
7496
      decl_specs->any_specifiers_p = true;
7497
      /* After we see one decl-specifier, further decl-specifiers are
7498
         always optional.  */
7499
      flags |= CP_PARSER_FLAGS_OPTIONAL;
7500
    }
7501
 
7502
  /* Check for repeated decl-specifiers.  */
7503
  for (ds = ds_first; ds != ds_last; ++ds)
7504
    {
7505
      unsigned count = decl_specs->specs[(int)ds];
7506
      if (count < 2)
7507
        continue;
7508
      /* The "long" specifier is a special case because of "long long".  */
7509
      if (ds == ds_long)
7510
        {
7511
          if (count > 2)
7512
            error ("%<long long long%> is too long for GCC");
7513
          else if (pedantic && !in_system_header && warn_long_long)
7514
            pedwarn ("ISO C++ does not support %<long long%>");
7515
        }
7516
      else if (count > 1)
7517
        {
7518
          static const char *const decl_spec_names[] = {
7519
            "signed",
7520
            "unsigned",
7521
            "short",
7522
            "long",
7523
            "const",
7524
            "volatile",
7525
            "restrict",
7526
            "inline",
7527
            "virtual",
7528
            "explicit",
7529
            "friend",
7530
            "typedef",
7531
            "__complex",
7532
            "__thread"
7533
          };
7534
          error ("duplicate %qs", decl_spec_names[(int)ds]);
7535
        }
7536
    }
7537
 
7538
  /* Don't allow a friend specifier with a class definition.  */
7539
  if (decl_specs->specs[(int) ds_friend] != 0
7540
      && (*declares_class_or_enum & 2))
7541
    error ("class definition may not be declared a friend");
7542
}
7543
 
7544
/* Parse an (optional) storage-class-specifier.
7545
 
7546
   storage-class-specifier:
7547
     auto
7548
     register
7549
     static
7550
     extern
7551
     mutable
7552
 
7553
   GNU Extension:
7554
 
7555
   storage-class-specifier:
7556
     thread
7557
 
7558
   Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7559
 
7560
static tree
7561
cp_parser_storage_class_specifier_opt (cp_parser* parser)
7562
{
7563
  switch (cp_lexer_peek_token (parser->lexer)->keyword)
7564
    {
7565
    case RID_AUTO:
7566
    case RID_REGISTER:
7567
    case RID_STATIC:
7568
    case RID_EXTERN:
7569
    case RID_MUTABLE:
7570
    case RID_THREAD:
7571
      /* Consume the token.  */
7572
      return cp_lexer_consume_token (parser->lexer)->value;
7573
 
7574
    default:
7575
      return NULL_TREE;
7576
    }
7577
}
7578
 
7579
/* Parse an (optional) function-specifier.
7580
 
7581
   function-specifier:
7582
     inline
7583
     virtual
7584
     explicit
7585
 
7586
   Returns an IDENTIFIER_NODE corresponding to the keyword used.
7587
   Updates DECL_SPECS, if it is non-NULL.  */
7588
 
7589
static tree
7590
cp_parser_function_specifier_opt (cp_parser* parser,
7591
                                  cp_decl_specifier_seq *decl_specs)
7592
{
7593
  switch (cp_lexer_peek_token (parser->lexer)->keyword)
7594
    {
7595
    case RID_INLINE:
7596
      if (decl_specs)
7597
        ++decl_specs->specs[(int) ds_inline];
7598
      break;
7599
 
7600
    case RID_VIRTUAL:
7601
      if (decl_specs)
7602
        ++decl_specs->specs[(int) ds_virtual];
7603
      break;
7604
 
7605
    case RID_EXPLICIT:
7606
      if (decl_specs)
7607
        ++decl_specs->specs[(int) ds_explicit];
7608
      break;
7609
 
7610
    default:
7611
      return NULL_TREE;
7612
    }
7613
 
7614
  /* Consume the token.  */
7615
  return cp_lexer_consume_token (parser->lexer)->value;
7616
}
7617
 
7618
/* Parse a linkage-specification.
7619
 
7620
   linkage-specification:
7621
     extern string-literal { declaration-seq [opt] }
7622
     extern string-literal declaration  */
7623
 
7624
static void
7625
cp_parser_linkage_specification (cp_parser* parser)
7626
{
7627
  tree linkage;
7628
 
7629
  /* Look for the `extern' keyword.  */
7630
  cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7631
 
7632
  /* Look for the string-literal.  */
7633
  linkage = cp_parser_string_literal (parser, false, false);
7634
 
7635
  /* Transform the literal into an identifier.  If the literal is a
7636
     wide-character string, or contains embedded NULs, then we can't
7637
     handle it as the user wants.  */
7638
  if (strlen (TREE_STRING_POINTER (linkage))
7639
      != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7640
    {
7641
      cp_parser_error (parser, "invalid linkage-specification");
7642
      /* Assume C++ linkage.  */
7643
      linkage = lang_name_cplusplus;
7644
    }
7645
  else
7646
    linkage = get_identifier (TREE_STRING_POINTER (linkage));
7647
 
7648
  /* We're now using the new linkage.  */
7649
  push_lang_context (linkage);
7650
 
7651
  /* If the next token is a `{', then we're using the first
7652
     production.  */
7653
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7654
    {
7655
      /* Consume the `{' token.  */
7656
      cp_lexer_consume_token (parser->lexer);
7657
      /* Parse the declarations.  */
7658
      cp_parser_declaration_seq_opt (parser);
7659
      /* Look for the closing `}'.  */
7660
      cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7661
    }
7662
  /* Otherwise, there's just one declaration.  */
7663
  else
7664
    {
7665
      bool saved_in_unbraced_linkage_specification_p;
7666
 
7667
      saved_in_unbraced_linkage_specification_p
7668
        = parser->in_unbraced_linkage_specification_p;
7669
      parser->in_unbraced_linkage_specification_p = true;
7670
      have_extern_spec = true;
7671
      cp_parser_declaration (parser);
7672
      have_extern_spec = false;
7673
      parser->in_unbraced_linkage_specification_p
7674
        = saved_in_unbraced_linkage_specification_p;
7675
    }
7676
 
7677
  /* We're done with the linkage-specification.  */
7678
  pop_lang_context ();
7679
}
7680
 
7681
/* Special member functions [gram.special] */
7682
 
7683
/* Parse a conversion-function-id.
7684
 
7685
   conversion-function-id:
7686
     operator conversion-type-id
7687
 
7688
   Returns an IDENTIFIER_NODE representing the operator.  */
7689
 
7690
static tree
7691
cp_parser_conversion_function_id (cp_parser* parser)
7692
{
7693
  tree type;
7694
  tree saved_scope;
7695
  tree saved_qualifying_scope;
7696
  tree saved_object_scope;
7697
  tree pushed_scope = NULL_TREE;
7698
 
7699
  /* Look for the `operator' token.  */
7700
  if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7701
    return error_mark_node;
7702
  /* When we parse the conversion-type-id, the current scope will be
7703
     reset.  However, we need that information in able to look up the
7704
     conversion function later, so we save it here.  */
7705
  saved_scope = parser->scope;
7706
  saved_qualifying_scope = parser->qualifying_scope;
7707
  saved_object_scope = parser->object_scope;
7708
  /* We must enter the scope of the class so that the names of
7709
     entities declared within the class are available in the
7710
     conversion-type-id.  For example, consider:
7711
 
7712
       struct S {
7713
         typedef int I;
7714
         operator I();
7715
       };
7716
 
7717
       S::operator I() { ... }
7718
 
7719
     In order to see that `I' is a type-name in the definition, we
7720
     must be in the scope of `S'.  */
7721
  if (saved_scope)
7722
    pushed_scope = push_scope (saved_scope);
7723
  /* Parse the conversion-type-id.  */
7724
  type = cp_parser_conversion_type_id (parser);
7725
  /* Leave the scope of the class, if any.  */
7726
  if (pushed_scope)
7727
    pop_scope (pushed_scope);
7728
  /* Restore the saved scope.  */
7729
  parser->scope = saved_scope;
7730
  parser->qualifying_scope = saved_qualifying_scope;
7731
  parser->object_scope = saved_object_scope;
7732
  /* If the TYPE is invalid, indicate failure.  */
7733
  if (type == error_mark_node)
7734
    return error_mark_node;
7735
  return mangle_conv_op_name_for_type (type);
7736
}
7737
 
7738
/* Parse a conversion-type-id:
7739
 
7740
   conversion-type-id:
7741
     type-specifier-seq conversion-declarator [opt]
7742
 
7743
   Returns the TYPE specified.  */
7744
 
7745
static tree
7746
cp_parser_conversion_type_id (cp_parser* parser)
7747
{
7748
  tree attributes;
7749
  cp_decl_specifier_seq type_specifiers;
7750
  cp_declarator *declarator;
7751
  tree type_specified;
7752
 
7753
  /* Parse the attributes.  */
7754
  attributes = cp_parser_attributes_opt (parser);
7755
  /* Parse the type-specifiers.  */
7756
  cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7757
                                &type_specifiers);
7758
  /* If that didn't work, stop.  */
7759
  if (type_specifiers.type == error_mark_node)
7760
    return error_mark_node;
7761
  /* Parse the conversion-declarator.  */
7762
  declarator = cp_parser_conversion_declarator_opt (parser);
7763
 
7764
  type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7765
                                    /*initialized=*/0, &attributes);
7766
  if (attributes)
7767
    cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7768
  return type_specified;
7769
}
7770
 
7771
/* Parse an (optional) conversion-declarator.
7772
 
7773
   conversion-declarator:
7774
     ptr-operator conversion-declarator [opt]
7775
 
7776
   */
7777
 
7778
static cp_declarator *
7779
cp_parser_conversion_declarator_opt (cp_parser* parser)
7780
{
7781
  enum tree_code code;
7782
  tree class_type;
7783
  cp_cv_quals cv_quals;
7784
 
7785
  /* We don't know if there's a ptr-operator next, or not.  */
7786
  cp_parser_parse_tentatively (parser);
7787
  /* Try the ptr-operator.  */
7788
  code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7789
  /* If it worked, look for more conversion-declarators.  */
7790
  if (cp_parser_parse_definitely (parser))
7791
    {
7792
      cp_declarator *declarator;
7793
 
7794
      /* Parse another optional declarator.  */
7795
      declarator = cp_parser_conversion_declarator_opt (parser);
7796
 
7797
      /* Create the representation of the declarator.  */
7798
      if (class_type)
7799
        declarator = make_ptrmem_declarator (cv_quals, class_type,
7800
                                             declarator);
7801
      else if (code == INDIRECT_REF)
7802
        declarator = make_pointer_declarator (cv_quals, declarator);
7803
      else
7804
        declarator = make_reference_declarator (cv_quals, declarator);
7805
 
7806
      return declarator;
7807
   }
7808
 
7809
  return NULL;
7810
}
7811
 
7812
/* Parse an (optional) ctor-initializer.
7813
 
7814
   ctor-initializer:
7815
     : mem-initializer-list
7816
 
7817
   Returns TRUE iff the ctor-initializer was actually present.  */
7818
 
7819
static bool
7820
cp_parser_ctor_initializer_opt (cp_parser* parser)
7821
{
7822
  /* If the next token is not a `:', then there is no
7823
     ctor-initializer.  */
7824
  if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7825
    {
7826
      /* Do default initialization of any bases and members.  */
7827
      if (DECL_CONSTRUCTOR_P (current_function_decl))
7828
        finish_mem_initializers (NULL_TREE);
7829
 
7830
      return false;
7831
    }
7832
 
7833
  /* Consume the `:' token.  */
7834
  cp_lexer_consume_token (parser->lexer);
7835
  /* And the mem-initializer-list.  */
7836
  cp_parser_mem_initializer_list (parser);
7837
 
7838
  return true;
7839
}
7840
 
7841
/* Parse a mem-initializer-list.
7842
 
7843
   mem-initializer-list:
7844
     mem-initializer
7845
     mem-initializer , mem-initializer-list  */
7846
 
7847
static void
7848
cp_parser_mem_initializer_list (cp_parser* parser)
7849
{
7850
  tree mem_initializer_list = NULL_TREE;
7851
 
7852
  /* Let the semantic analysis code know that we are starting the
7853
     mem-initializer-list.  */
7854
  if (!DECL_CONSTRUCTOR_P (current_function_decl))
7855
    error ("only constructors take base initializers");
7856
 
7857
  /* Loop through the list.  */
7858
  while (true)
7859
    {
7860
      tree mem_initializer;
7861
 
7862
      /* Parse the mem-initializer.  */
7863
      mem_initializer = cp_parser_mem_initializer (parser);
7864
      /* Add it to the list, unless it was erroneous.  */
7865
      if (mem_initializer != error_mark_node)
7866
        {
7867
          TREE_CHAIN (mem_initializer) = mem_initializer_list;
7868
          mem_initializer_list = mem_initializer;
7869
        }
7870
      /* If the next token is not a `,', we're done.  */
7871
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7872
        break;
7873
      /* Consume the `,' token.  */
7874
      cp_lexer_consume_token (parser->lexer);
7875
    }
7876
 
7877
  /* Perform semantic analysis.  */
7878
  if (DECL_CONSTRUCTOR_P (current_function_decl))
7879
    finish_mem_initializers (mem_initializer_list);
7880
}
7881
 
7882
/* Parse a mem-initializer.
7883
 
7884
   mem-initializer:
7885
     mem-initializer-id ( expression-list [opt] )
7886
 
7887
   GNU extension:
7888
 
7889
   mem-initializer:
7890
     ( expression-list [opt] )
7891
 
7892
   Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7893
   class) or FIELD_DECL (for a non-static data member) to initialize;
7894
   the TREE_VALUE is the expression-list.  An empty initialization
7895
   list is represented by void_list_node.  */
7896
 
7897
static tree
7898
cp_parser_mem_initializer (cp_parser* parser)
7899
{
7900
  tree mem_initializer_id;
7901
  tree expression_list;
7902
  tree member;
7903
 
7904
  /* Find out what is being initialized.  */
7905
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7906
    {
7907
      pedwarn ("anachronistic old-style base class initializer");
7908
      mem_initializer_id = NULL_TREE;
7909
    }
7910
  else
7911
    mem_initializer_id = cp_parser_mem_initializer_id (parser);
7912
  member = expand_member_init (mem_initializer_id);
7913
  if (member && !DECL_P (member))
7914
    in_base_initializer = 1;
7915
 
7916
  expression_list
7917
    = cp_parser_parenthesized_expression_list (parser, false,
7918
                                               /*cast_p=*/false,
7919
                                               /*non_constant_p=*/NULL);
7920
  if (expression_list == error_mark_node)
7921
    return error_mark_node;
7922
  if (!expression_list)
7923
    expression_list = void_type_node;
7924
 
7925
  in_base_initializer = 0;
7926
 
7927
  return member ? build_tree_list (member, expression_list) : error_mark_node;
7928
}
7929
 
7930
/* Parse a mem-initializer-id.
7931
 
7932
   mem-initializer-id:
7933
     :: [opt] nested-name-specifier [opt] class-name
7934
     identifier
7935
 
7936
   Returns a TYPE indicating the class to be initializer for the first
7937
   production.  Returns an IDENTIFIER_NODE indicating the data member
7938
   to be initialized for the second production.  */
7939
 
7940
static tree
7941
cp_parser_mem_initializer_id (cp_parser* parser)
7942
{
7943
  bool global_scope_p;
7944
  bool nested_name_specifier_p;
7945
  bool template_p = false;
7946
  tree id;
7947
 
7948
  /* `typename' is not allowed in this context ([temp.res]).  */
7949
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7950
    {
7951
      error ("keyword %<typename%> not allowed in this context (a qualified "
7952
             "member initializer is implicitly a type)");
7953
      cp_lexer_consume_token (parser->lexer);
7954
    }
7955
  /* Look for the optional `::' operator.  */
7956
  global_scope_p
7957
    = (cp_parser_global_scope_opt (parser,
7958
                                   /*current_scope_valid_p=*/false)
7959
       != NULL_TREE);
7960
  /* Look for the optional nested-name-specifier.  The simplest way to
7961
     implement:
7962
 
7963
       [temp.res]
7964
 
7965
       The keyword `typename' is not permitted in a base-specifier or
7966
       mem-initializer; in these contexts a qualified name that
7967
       depends on a template-parameter is implicitly assumed to be a
7968
       type name.
7969
 
7970
     is to assume that we have seen the `typename' keyword at this
7971
     point.  */
7972
  nested_name_specifier_p
7973
    = (cp_parser_nested_name_specifier_opt (parser,
7974
                                            /*typename_keyword_p=*/true,
7975
                                            /*check_dependency_p=*/true,
7976
                                            /*type_p=*/true,
7977
                                            /*is_declaration=*/true)
7978
       != NULL_TREE);
7979
  if (nested_name_specifier_p)
7980
    template_p = cp_parser_optional_template_keyword (parser);
7981
  /* If there is a `::' operator or a nested-name-specifier, then we
7982
     are definitely looking for a class-name.  */
7983
  if (global_scope_p || nested_name_specifier_p)
7984
    return cp_parser_class_name (parser,
7985
                                 /*typename_keyword_p=*/true,
7986
                                 /*template_keyword_p=*/template_p,
7987
                                 none_type,
7988
                                 /*check_dependency_p=*/true,
7989
                                 /*class_head_p=*/false,
7990
                                 /*is_declaration=*/true);
7991
  /* Otherwise, we could also be looking for an ordinary identifier.  */
7992
  cp_parser_parse_tentatively (parser);
7993
  /* Try a class-name.  */
7994
  id = cp_parser_class_name (parser,
7995
                             /*typename_keyword_p=*/true,
7996
                             /*template_keyword_p=*/false,
7997
                             none_type,
7998
                             /*check_dependency_p=*/true,
7999
                             /*class_head_p=*/false,
8000
                             /*is_declaration=*/true);
8001
  /* If we found one, we're done.  */
8002
  if (cp_parser_parse_definitely (parser))
8003
    return id;
8004
  /* Otherwise, look for an ordinary identifier.  */
8005
  return cp_parser_identifier (parser);
8006
}
8007
 
8008
/* Overloading [gram.over] */
8009
 
8010
/* Parse an operator-function-id.
8011
 
8012
   operator-function-id:
8013
     operator operator
8014
 
8015
   Returns an IDENTIFIER_NODE for the operator which is a
8016
   human-readable spelling of the identifier, e.g., `operator +'.  */
8017
 
8018
static tree
8019
cp_parser_operator_function_id (cp_parser* parser)
8020
{
8021
  /* Look for the `operator' keyword.  */
8022
  if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8023
    return error_mark_node;
8024
  /* And then the name of the operator itself.  */
8025
  return cp_parser_operator (parser);
8026
}
8027
 
8028
/* Parse an operator.
8029
 
8030
   operator:
8031
     new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8032
     += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8033
     || ++ -- , ->* -> () []
8034
 
8035
   GNU Extensions:
8036
 
8037
   operator:
8038
     <? >? <?= >?=
8039
 
8040
   Returns an IDENTIFIER_NODE for the operator which is a
8041
   human-readable spelling of the identifier, e.g., `operator +'.  */
8042
 
8043
static tree
8044
cp_parser_operator (cp_parser* parser)
8045
{
8046
  tree id = NULL_TREE;
8047
  cp_token *token;
8048
 
8049
  /* Peek at the next token.  */
8050
  token = cp_lexer_peek_token (parser->lexer);
8051
  /* Figure out which operator we have.  */
8052
  switch (token->type)
8053
    {
8054
    case CPP_KEYWORD:
8055
      {
8056
        enum tree_code op;
8057
 
8058
        /* The keyword should be either `new' or `delete'.  */
8059
        if (token->keyword == RID_NEW)
8060
          op = NEW_EXPR;
8061
        else if (token->keyword == RID_DELETE)
8062
          op = DELETE_EXPR;
8063
        else
8064
          break;
8065
 
8066
        /* Consume the `new' or `delete' token.  */
8067
        cp_lexer_consume_token (parser->lexer);
8068
 
8069
        /* Peek at the next token.  */
8070
        token = cp_lexer_peek_token (parser->lexer);
8071
        /* If it's a `[' token then this is the array variant of the
8072
           operator.  */
8073
        if (token->type == CPP_OPEN_SQUARE)
8074
          {
8075
            /* Consume the `[' token.  */
8076
            cp_lexer_consume_token (parser->lexer);
8077
            /* Look for the `]' token.  */
8078
            cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8079
            id = ansi_opname (op == NEW_EXPR
8080
                              ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8081
          }
8082
        /* Otherwise, we have the non-array variant.  */
8083
        else
8084
          id = ansi_opname (op);
8085
 
8086
        return id;
8087
      }
8088
 
8089
    case CPP_PLUS:
8090
      id = ansi_opname (PLUS_EXPR);
8091
      break;
8092
 
8093
    case CPP_MINUS:
8094
      id = ansi_opname (MINUS_EXPR);
8095
      break;
8096
 
8097
    case CPP_MULT:
8098
      id = ansi_opname (MULT_EXPR);
8099
      break;
8100
 
8101
    case CPP_DIV:
8102
      id = ansi_opname (TRUNC_DIV_EXPR);
8103
      break;
8104
 
8105
    case CPP_MOD:
8106
      id = ansi_opname (TRUNC_MOD_EXPR);
8107
      break;
8108
 
8109
    case CPP_XOR:
8110
      id = ansi_opname (BIT_XOR_EXPR);
8111
      break;
8112
 
8113
    case CPP_AND:
8114
      id = ansi_opname (BIT_AND_EXPR);
8115
      break;
8116
 
8117
    case CPP_OR:
8118
      id = ansi_opname (BIT_IOR_EXPR);
8119
      break;
8120
 
8121
    case CPP_COMPL:
8122
      id = ansi_opname (BIT_NOT_EXPR);
8123
      break;
8124
 
8125
    case CPP_NOT:
8126
      id = ansi_opname (TRUTH_NOT_EXPR);
8127
      break;
8128
 
8129
    case CPP_EQ:
8130
      id = ansi_assopname (NOP_EXPR);
8131
      break;
8132
 
8133
    case CPP_LESS:
8134
      id = ansi_opname (LT_EXPR);
8135
      break;
8136
 
8137
    case CPP_GREATER:
8138
      id = ansi_opname (GT_EXPR);
8139
      break;
8140
 
8141
    case CPP_PLUS_EQ:
8142
      id = ansi_assopname (PLUS_EXPR);
8143
      break;
8144
 
8145
    case CPP_MINUS_EQ:
8146
      id = ansi_assopname (MINUS_EXPR);
8147
      break;
8148
 
8149
    case CPP_MULT_EQ:
8150
      id = ansi_assopname (MULT_EXPR);
8151
      break;
8152
 
8153
    case CPP_DIV_EQ:
8154
      id = ansi_assopname (TRUNC_DIV_EXPR);
8155
      break;
8156
 
8157
    case CPP_MOD_EQ:
8158
      id = ansi_assopname (TRUNC_MOD_EXPR);
8159
      break;
8160
 
8161
    case CPP_XOR_EQ:
8162
      id = ansi_assopname (BIT_XOR_EXPR);
8163
      break;
8164
 
8165
    case CPP_AND_EQ:
8166
      id = ansi_assopname (BIT_AND_EXPR);
8167
      break;
8168
 
8169
    case CPP_OR_EQ:
8170
      id = ansi_assopname (BIT_IOR_EXPR);
8171
      break;
8172
 
8173
    case CPP_LSHIFT:
8174
      id = ansi_opname (LSHIFT_EXPR);
8175
      break;
8176
 
8177
    case CPP_RSHIFT:
8178
      id = ansi_opname (RSHIFT_EXPR);
8179
      break;
8180
 
8181
    case CPP_LSHIFT_EQ:
8182
      id = ansi_assopname (LSHIFT_EXPR);
8183
      break;
8184
 
8185
    case CPP_RSHIFT_EQ:
8186
      id = ansi_assopname (RSHIFT_EXPR);
8187
      break;
8188
 
8189
    case CPP_EQ_EQ:
8190
      id = ansi_opname (EQ_EXPR);
8191
      break;
8192
 
8193
    case CPP_NOT_EQ:
8194
      id = ansi_opname (NE_EXPR);
8195
      break;
8196
 
8197
    case CPP_LESS_EQ:
8198
      id = ansi_opname (LE_EXPR);
8199
      break;
8200
 
8201
    case CPP_GREATER_EQ:
8202
      id = ansi_opname (GE_EXPR);
8203
      break;
8204
 
8205
    case CPP_AND_AND:
8206
      id = ansi_opname (TRUTH_ANDIF_EXPR);
8207
      break;
8208
 
8209
    case CPP_OR_OR:
8210
      id = ansi_opname (TRUTH_ORIF_EXPR);
8211
      break;
8212
 
8213
    case CPP_PLUS_PLUS:
8214
      id = ansi_opname (POSTINCREMENT_EXPR);
8215
      break;
8216
 
8217
    case CPP_MINUS_MINUS:
8218
      id = ansi_opname (PREDECREMENT_EXPR);
8219
      break;
8220
 
8221
    case CPP_COMMA:
8222
      id = ansi_opname (COMPOUND_EXPR);
8223
      break;
8224
 
8225
    case CPP_DEREF_STAR:
8226
      id = ansi_opname (MEMBER_REF);
8227
      break;
8228
 
8229
    case CPP_DEREF:
8230
      id = ansi_opname (COMPONENT_REF);
8231
      break;
8232
 
8233
    case CPP_OPEN_PAREN:
8234
      /* Consume the `('.  */
8235
      cp_lexer_consume_token (parser->lexer);
8236
      /* Look for the matching `)'.  */
8237
      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8238
      return ansi_opname (CALL_EXPR);
8239
 
8240
    case CPP_OPEN_SQUARE:
8241
      /* Consume the `['.  */
8242
      cp_lexer_consume_token (parser->lexer);
8243
      /* Look for the matching `]'.  */
8244
      cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8245
      return ansi_opname (ARRAY_REF);
8246
 
8247
      /* Extensions.  */
8248
    case CPP_MIN:
8249
      id = ansi_opname (MIN_EXPR);
8250
      cp_parser_warn_min_max ();
8251
      break;
8252
 
8253
    case CPP_MAX:
8254
      id = ansi_opname (MAX_EXPR);
8255
      cp_parser_warn_min_max ();
8256
      break;
8257
 
8258
    case CPP_MIN_EQ:
8259
      id = ansi_assopname (MIN_EXPR);
8260
      cp_parser_warn_min_max ();
8261
      break;
8262
 
8263
    case CPP_MAX_EQ:
8264
      id = ansi_assopname (MAX_EXPR);
8265
      cp_parser_warn_min_max ();
8266
      break;
8267
 
8268
    default:
8269
      /* Anything else is an error.  */
8270
      break;
8271
    }
8272
 
8273
  /* If we have selected an identifier, we need to consume the
8274
     operator token.  */
8275
  if (id)
8276
    cp_lexer_consume_token (parser->lexer);
8277
  /* Otherwise, no valid operator name was present.  */
8278
  else
8279
    {
8280
      cp_parser_error (parser, "expected operator");
8281
      id = error_mark_node;
8282
    }
8283
 
8284
  return id;
8285
}
8286
 
8287
/* Parse a template-declaration.
8288
 
8289
   template-declaration:
8290
     export [opt] template < template-parameter-list > declaration
8291
 
8292
   If MEMBER_P is TRUE, this template-declaration occurs within a
8293
   class-specifier.
8294
 
8295
   The grammar rule given by the standard isn't correct.  What
8296
   is really meant is:
8297
 
8298
   template-declaration:
8299
     export [opt] template-parameter-list-seq
8300
       decl-specifier-seq [opt] init-declarator [opt] ;
8301
     export [opt] template-parameter-list-seq
8302
       function-definition
8303
 
8304
   template-parameter-list-seq:
8305
     template-parameter-list-seq [opt]
8306
     template < template-parameter-list >  */
8307
 
8308
static void
8309
cp_parser_template_declaration (cp_parser* parser, bool member_p)
8310
{
8311
  /* Check for `export'.  */
8312
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8313
    {
8314
      /* Consume the `export' token.  */
8315
      cp_lexer_consume_token (parser->lexer);
8316
      /* Warn that we do not support `export'.  */
8317
      warning (0, "keyword %<export%> not implemented, and will be ignored");
8318
    }
8319
 
8320
  cp_parser_template_declaration_after_export (parser, member_p);
8321
}
8322
 
8323
/* Parse a template-parameter-list.
8324
 
8325
   template-parameter-list:
8326
     template-parameter
8327
     template-parameter-list , template-parameter
8328
 
8329
   Returns a TREE_LIST.  Each node represents a template parameter.
8330
   The nodes are connected via their TREE_CHAINs.  */
8331
 
8332
static tree
8333
cp_parser_template_parameter_list (cp_parser* parser)
8334
{
8335
  tree parameter_list = NULL_TREE;
8336
 
8337
  while (true)
8338
    {
8339
      tree parameter;
8340
      cp_token *token;
8341
      bool is_non_type;
8342
 
8343
      /* Parse the template-parameter.  */
8344
      parameter = cp_parser_template_parameter (parser, &is_non_type);
8345
      /* Add it to the list.  */
8346
      if (parameter != error_mark_node)
8347
        parameter_list = process_template_parm (parameter_list,
8348
                                                parameter,
8349
                                                is_non_type);
8350
      /* Peek at the next token.  */
8351
      token = cp_lexer_peek_token (parser->lexer);
8352
      /* If it's not a `,', we're done.  */
8353
      if (token->type != CPP_COMMA)
8354
        break;
8355
      /* Otherwise, consume the `,' token.  */
8356
      cp_lexer_consume_token (parser->lexer);
8357
    }
8358
 
8359
  return parameter_list;
8360
}
8361
 
8362
/* Parse a template-parameter.
8363
 
8364
   template-parameter:
8365
     type-parameter
8366
     parameter-declaration
8367
 
8368
   If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8369
   the parameter.  The TREE_PURPOSE is the default value, if any.
8370
   Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8371
   iff this parameter is a non-type parameter.  */
8372
 
8373
static tree
8374
cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8375
{
8376
  cp_token *token;
8377
  cp_parameter_declarator *parameter_declarator;
8378
  tree parm;
8379
 
8380
  /* Assume it is a type parameter or a template parameter.  */
8381
  *is_non_type = false;
8382
  /* Peek at the next token.  */
8383
  token = cp_lexer_peek_token (parser->lexer);
8384
  /* If it is `class' or `template', we have a type-parameter.  */
8385
  if (token->keyword == RID_TEMPLATE)
8386
    return cp_parser_type_parameter (parser);
8387
  /* If it is `class' or `typename' we do not know yet whether it is a
8388
     type parameter or a non-type parameter.  Consider:
8389
 
8390
       template <typename T, typename T::X X> ...
8391
 
8392
     or:
8393
 
8394
       template <class C, class D*> ...
8395
 
8396
     Here, the first parameter is a type parameter, and the second is
8397
     a non-type parameter.  We can tell by looking at the token after
8398
     the identifier -- if it is a `,', `=', or `>' then we have a type
8399
     parameter.  */
8400
  if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8401
    {
8402
      /* Peek at the token after `class' or `typename'.  */
8403
      token = cp_lexer_peek_nth_token (parser->lexer, 2);
8404
      /* If it's an identifier, skip it.  */
8405
      if (token->type == CPP_NAME)
8406
        token = cp_lexer_peek_nth_token (parser->lexer, 3);
8407
      /* Now, see if the token looks like the end of a template
8408
         parameter.  */
8409
      if (token->type == CPP_COMMA
8410
          || token->type == CPP_EQ
8411
          || token->type == CPP_GREATER)
8412
        return cp_parser_type_parameter (parser);
8413
    }
8414
 
8415
  /* Otherwise, it is a non-type parameter.
8416
 
8417
     [temp.param]
8418
 
8419
     When parsing a default template-argument for a non-type
8420
     template-parameter, the first non-nested `>' is taken as the end
8421
     of the template parameter-list rather than a greater-than
8422
     operator.  */
8423
  *is_non_type = true;
8424
  parameter_declarator
8425
     = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8426
                                        /*parenthesized_p=*/NULL);
8427
  parm = grokdeclarator (parameter_declarator->declarator,
8428
                         &parameter_declarator->decl_specifiers,
8429
                         PARM, /*initialized=*/0,
8430
                         /*attrlist=*/NULL);
8431
  if (parm == error_mark_node)
8432
    return error_mark_node;
8433
  return build_tree_list (parameter_declarator->default_argument, parm);
8434
}
8435
 
8436
/* Parse a type-parameter.
8437
 
8438
   type-parameter:
8439
     class identifier [opt]
8440
     class identifier [opt] = type-id
8441
     typename identifier [opt]
8442
     typename identifier [opt] = type-id
8443
     template < template-parameter-list > class identifier [opt]
8444
     template < template-parameter-list > class identifier [opt]
8445
       = id-expression
8446
 
8447
   Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8448
   TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8449
   the declaration of the parameter.  */
8450
 
8451
static tree
8452
cp_parser_type_parameter (cp_parser* parser)
8453
{
8454
  cp_token *token;
8455
  tree parameter;
8456
 
8457
  /* Look for a keyword to tell us what kind of parameter this is.  */
8458
  token = cp_parser_require (parser, CPP_KEYWORD,
8459
                             "`class', `typename', or `template'");
8460
  if (!token)
8461
    return error_mark_node;
8462
 
8463
  switch (token->keyword)
8464
    {
8465
    case RID_CLASS:
8466
    case RID_TYPENAME:
8467
      {
8468
        tree identifier;
8469
        tree default_argument;
8470
 
8471
        /* If the next token is an identifier, then it names the
8472
           parameter.  */
8473
        if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8474
          identifier = cp_parser_identifier (parser);
8475
        else
8476
          identifier = NULL_TREE;
8477
 
8478
        /* Create the parameter.  */
8479
        parameter = finish_template_type_parm (class_type_node, identifier);
8480
 
8481
        /* If the next token is an `=', we have a default argument.  */
8482
        if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8483
          {
8484
            /* Consume the `=' token.  */
8485
            cp_lexer_consume_token (parser->lexer);
8486
            /* Parse the default-argument.  */
8487
            push_deferring_access_checks (dk_no_deferred);
8488
            default_argument = cp_parser_type_id (parser);
8489
            pop_deferring_access_checks ();
8490
          }
8491
        else
8492
          default_argument = NULL_TREE;
8493
 
8494
        /* Create the combined representation of the parameter and the
8495
           default argument.  */
8496
        parameter = build_tree_list (default_argument, parameter);
8497
      }
8498
      break;
8499
 
8500
    case RID_TEMPLATE:
8501
      {
8502
        tree parameter_list;
8503
        tree identifier;
8504
        tree default_argument;
8505
 
8506
        /* Look for the `<'.  */
8507
        cp_parser_require (parser, CPP_LESS, "`<'");
8508
        /* Parse the template-parameter-list.  */
8509
        begin_template_parm_list ();
8510
        parameter_list
8511
          = cp_parser_template_parameter_list (parser);
8512
        parameter_list = end_template_parm_list (parameter_list);
8513
        /* Look for the `>'.  */
8514
        cp_parser_require (parser, CPP_GREATER, "`>'");
8515
        /* Look for the `class' keyword.  */
8516
        cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8517
        /* If the next token is an `=', then there is a
8518
           default-argument.  If the next token is a `>', we are at
8519
           the end of the parameter-list.  If the next token is a `,',
8520
           then we are at the end of this parameter.  */
8521
        if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8522
            && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8523
            && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8524
          {
8525
            identifier = cp_parser_identifier (parser);
8526
            /* Treat invalid names as if the parameter were nameless.  */
8527
            if (identifier == error_mark_node)
8528
              identifier = NULL_TREE;
8529
          }
8530
        else
8531
          identifier = NULL_TREE;
8532
 
8533
        /* Create the template parameter.  */
8534
        parameter = finish_template_template_parm (class_type_node,
8535
                                                   identifier);
8536
 
8537
        /* If the next token is an `=', then there is a
8538
           default-argument.  */
8539
        if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8540
          {
8541
            bool is_template;
8542
 
8543
            /* Consume the `='.  */
8544
            cp_lexer_consume_token (parser->lexer);
8545
            /* Parse the id-expression.  */
8546
            push_deferring_access_checks (dk_no_deferred);
8547
            default_argument
8548
              = cp_parser_id_expression (parser,
8549
                                         /*template_keyword_p=*/false,
8550
                                         /*check_dependency_p=*/true,
8551
                                         /*template_p=*/&is_template,
8552
                                         /*declarator_p=*/false);
8553
            if (TREE_CODE (default_argument) == TYPE_DECL)
8554
              /* If the id-expression was a template-id that refers to
8555
                 a template-class, we already have the declaration here,
8556
                 so no further lookup is needed.  */
8557
                 ;
8558
            else
8559
              /* Look up the name.  */
8560
              default_argument
8561
                = cp_parser_lookup_name (parser, default_argument,
8562
                                         none_type,
8563
                                         /*is_template=*/is_template,
8564
                                         /*is_namespace=*/false,
8565
                                         /*check_dependency=*/true,
8566
                                         /*ambiguous_decls=*/NULL);
8567
            /* See if the default argument is valid.  */
8568
            default_argument
8569
              = check_template_template_default_arg (default_argument);
8570
            pop_deferring_access_checks ();
8571
          }
8572
        else
8573
          default_argument = NULL_TREE;
8574
 
8575
        /* Create the combined representation of the parameter and the
8576
           default argument.  */
8577
        parameter = build_tree_list (default_argument, parameter);
8578
      }
8579
      break;
8580
 
8581
    default:
8582
      gcc_unreachable ();
8583
      break;
8584
    }
8585
 
8586
  return parameter;
8587
}
8588
 
8589
/* Parse a template-id.
8590
 
8591
   template-id:
8592
     template-name < template-argument-list [opt] >
8593
 
8594
   If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8595
   `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8596
   returned.  Otherwise, if the template-name names a function, or set
8597
   of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8598
   names a class, returns a TYPE_DECL for the specialization.
8599
 
8600
   If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8601
   uninstantiated templates.  */
8602
 
8603
static tree
8604
cp_parser_template_id (cp_parser *parser,
8605
                       bool template_keyword_p,
8606
                       bool check_dependency_p,
8607
                       bool is_declaration)
8608
{
8609
  tree template;
8610
  tree arguments;
8611
  tree template_id;
8612
  cp_token_position start_of_id = 0;
8613
  tree access_check = NULL_TREE;
8614
  cp_token *next_token, *next_token_2;
8615
  bool is_identifier;
8616
 
8617
  /* If the next token corresponds to a template-id, there is no need
8618
     to reparse it.  */
8619
  next_token = cp_lexer_peek_token (parser->lexer);
8620
  if (next_token->type == CPP_TEMPLATE_ID)
8621
    {
8622
      tree value;
8623
      tree check;
8624
 
8625
      /* Get the stored value.  */
8626
      value = cp_lexer_consume_token (parser->lexer)->value;
8627
      /* Perform any access checks that were deferred.  */
8628
      for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8629
        perform_or_defer_access_check (TREE_PURPOSE (check),
8630
                                       TREE_VALUE (check));
8631
      /* Return the stored value.  */
8632
      return TREE_VALUE (value);
8633
    }
8634
 
8635
  /* Avoid performing name lookup if there is no possibility of
8636
     finding a template-id.  */
8637
  if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8638
      || (next_token->type == CPP_NAME
8639
          && !cp_parser_nth_token_starts_template_argument_list_p
8640
               (parser, 2)))
8641
    {
8642
      cp_parser_error (parser, "expected template-id");
8643
      return error_mark_node;
8644
    }
8645
 
8646
  /* Remember where the template-id starts.  */
8647
  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8648
    start_of_id = cp_lexer_token_position (parser->lexer, false);
8649
 
8650
  push_deferring_access_checks (dk_deferred);
8651
 
8652
  /* Parse the template-name.  */
8653
  is_identifier = false;
8654
  template = cp_parser_template_name (parser, template_keyword_p,
8655
                                      check_dependency_p,
8656
                                      is_declaration,
8657
                                      &is_identifier);
8658
  if (template == error_mark_node || is_identifier)
8659
    {
8660
      pop_deferring_access_checks ();
8661
      return template;
8662
    }
8663
 
8664
  /* If we find the sequence `[:' after a template-name, it's probably
8665
     a digraph-typo for `< ::'. Substitute the tokens and check if we can
8666
     parse correctly the argument list.  */
8667
  next_token = cp_lexer_peek_token (parser->lexer);
8668
  next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8669
  if (next_token->type == CPP_OPEN_SQUARE
8670
      && next_token->flags & DIGRAPH
8671
      && next_token_2->type == CPP_COLON
8672
      && !(next_token_2->flags & PREV_WHITE))
8673
    {
8674
      cp_parser_parse_tentatively (parser);
8675
      /* Change `:' into `::'.  */
8676
      next_token_2->type = CPP_SCOPE;
8677
      /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8678
         CPP_LESS.  */
8679
      cp_lexer_consume_token (parser->lexer);
8680
      /* Parse the arguments.  */
8681
      arguments = cp_parser_enclosed_template_argument_list (parser);
8682
      if (!cp_parser_parse_definitely (parser))
8683
        {
8684
          /* If we couldn't parse an argument list, then we revert our changes
8685
             and return simply an error. Maybe this is not a template-id
8686
             after all.  */
8687
          next_token_2->type = CPP_COLON;
8688
          cp_parser_error (parser, "expected %<<%>");
8689
          pop_deferring_access_checks ();
8690
          return error_mark_node;
8691
        }
8692
      /* Otherwise, emit an error about the invalid digraph, but continue
8693
         parsing because we got our argument list.  */
8694
      pedwarn ("%<<::%> cannot begin a template-argument list");
8695
      inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8696
              "between %<<%> and %<::%>");
8697
      if (!flag_permissive)
8698
        {
8699
          static bool hint;
8700
          if (!hint)
8701
            {
8702
              inform ("(if you use -fpermissive G++ will accept your code)");
8703
              hint = true;
8704
            }
8705
        }
8706
    }
8707
  else
8708
    {
8709
      /* Look for the `<' that starts the template-argument-list.  */
8710
      if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8711
        {
8712
          pop_deferring_access_checks ();
8713
          return error_mark_node;
8714
        }
8715
      /* Parse the arguments.  */
8716
      arguments = cp_parser_enclosed_template_argument_list (parser);
8717
    }
8718
 
8719
  /* Build a representation of the specialization.  */
8720
  if (TREE_CODE (template) == IDENTIFIER_NODE)
8721
    template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8722
  else if (DECL_CLASS_TEMPLATE_P (template)
8723
           || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8724
    template_id
8725
      = finish_template_type (template, arguments,
8726
                              cp_lexer_next_token_is (parser->lexer,
8727
                                                      CPP_SCOPE));
8728
  else
8729
    {
8730
      /* If it's not a class-template or a template-template, it should be
8731
         a function-template.  */
8732
      gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8733
                   || TREE_CODE (template) == OVERLOAD
8734
                   || BASELINK_P (template)));
8735
 
8736
      template_id = lookup_template_function (template, arguments);
8737
    }
8738
 
8739
  /* Retrieve any deferred checks.  Do not pop this access checks yet
8740
     so the memory will not be reclaimed during token replacing below.  */
8741
  access_check = get_deferred_access_checks ();
8742
 
8743
  /* If parsing tentatively, replace the sequence of tokens that makes
8744
     up the template-id with a CPP_TEMPLATE_ID token.  That way,
8745
     should we re-parse the token stream, we will not have to repeat
8746
     the effort required to do the parse, nor will we issue duplicate
8747
     error messages about problems during instantiation of the
8748
     template.  */
8749
  if (start_of_id)
8750
    {
8751
      cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8752
 
8753
      /* Reset the contents of the START_OF_ID token.  */
8754
      token->type = CPP_TEMPLATE_ID;
8755
      token->value = build_tree_list (access_check, template_id);
8756
      token->keyword = RID_MAX;
8757
 
8758
      /* Purge all subsequent tokens.  */
8759
      cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8760
 
8761
      /* ??? Can we actually assume that, if template_id ==
8762
         error_mark_node, we will have issued a diagnostic to the
8763
         user, as opposed to simply marking the tentative parse as
8764
         failed?  */
8765
      if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8766
        error ("parse error in template argument list");
8767
    }
8768
 
8769
  pop_deferring_access_checks ();
8770
  return template_id;
8771
}
8772
 
8773
/* Parse a template-name.
8774
 
8775
   template-name:
8776
     identifier
8777
 
8778
   The standard should actually say:
8779
 
8780
   template-name:
8781
     identifier
8782
     operator-function-id
8783
 
8784
   A defect report has been filed about this issue.
8785
 
8786
   A conversion-function-id cannot be a template name because they cannot
8787
   be part of a template-id. In fact, looking at this code:
8788
 
8789
   a.operator K<int>()
8790
 
8791
   the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8792
   It is impossible to call a templated conversion-function-id with an
8793
   explicit argument list, since the only allowed template parameter is
8794
   the type to which it is converting.
8795
 
8796
   If TEMPLATE_KEYWORD_P is true, then we have just seen the
8797
   `template' keyword, in a construction like:
8798
 
8799
     T::template f<3>()
8800
 
8801
   In that case `f' is taken to be a template-name, even though there
8802
   is no way of knowing for sure.
8803
 
8804
   Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8805
   name refers to a set of overloaded functions, at least one of which
8806
   is a template, or an IDENTIFIER_NODE with the name of the template,
8807
   if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8808
   names are looked up inside uninstantiated templates.  */
8809
 
8810
static tree
8811
cp_parser_template_name (cp_parser* parser,
8812
                         bool template_keyword_p,
8813
                         bool check_dependency_p,
8814
                         bool is_declaration,
8815
                         bool *is_identifier)
8816
{
8817
  tree identifier;
8818
  tree decl;
8819
  tree fns;
8820
 
8821
  /* If the next token is `operator', then we have either an
8822
     operator-function-id or a conversion-function-id.  */
8823
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8824
    {
8825
      /* We don't know whether we're looking at an
8826
         operator-function-id or a conversion-function-id.  */
8827
      cp_parser_parse_tentatively (parser);
8828
      /* Try an operator-function-id.  */
8829
      identifier = cp_parser_operator_function_id (parser);
8830
      /* If that didn't work, try a conversion-function-id.  */
8831
      if (!cp_parser_parse_definitely (parser))
8832
        {
8833
          cp_parser_error (parser, "expected template-name");
8834
          return error_mark_node;
8835
        }
8836
    }
8837
  /* Look for the identifier.  */
8838
  else
8839
    identifier = cp_parser_identifier (parser);
8840
 
8841
  /* If we didn't find an identifier, we don't have a template-id.  */
8842
  if (identifier == error_mark_node)
8843
    return error_mark_node;
8844
 
8845
  /* If the name immediately followed the `template' keyword, then it
8846
     is a template-name.  However, if the next token is not `<', then
8847
     we do not treat it as a template-name, since it is not being used
8848
     as part of a template-id.  This enables us to handle constructs
8849
     like:
8850
 
8851
       template <typename T> struct S { S(); };
8852
       template <typename T> S<T>::S();
8853
 
8854
     correctly.  We would treat `S' as a template -- if it were `S<T>'
8855
     -- but we do not if there is no `<'.  */
8856
 
8857
  if (processing_template_decl
8858
      && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8859
    {
8860
      /* In a declaration, in a dependent context, we pretend that the
8861
         "template" keyword was present in order to improve error
8862
         recovery.  For example, given:
8863
 
8864
           template <typename T> void f(T::X<int>);
8865
 
8866
         we want to treat "X<int>" as a template-id.  */
8867
      if (is_declaration
8868
          && !template_keyword_p
8869
          && parser->scope && TYPE_P (parser->scope)
8870
          && check_dependency_p
8871
          && dependent_type_p (parser->scope)
8872
          /* Do not do this for dtors (or ctors), since they never
8873
             need the template keyword before their name.  */
8874
          && !constructor_name_p (identifier, parser->scope))
8875
        {
8876
          cp_token_position start = 0;
8877
 
8878
          /* Explain what went wrong.  */
8879
          error ("non-template %qD used as template", identifier);
8880
          inform ("use %<%T::template %D%> to indicate that it is a template",
8881
                  parser->scope, identifier);
8882
          /* If parsing tentatively, find the location of the "<" token.  */
8883
          if (cp_parser_simulate_error (parser))
8884
            start = cp_lexer_token_position (parser->lexer, true);
8885
          /* Parse the template arguments so that we can issue error
8886
             messages about them.  */
8887
          cp_lexer_consume_token (parser->lexer);
8888
          cp_parser_enclosed_template_argument_list (parser);
8889
          /* Skip tokens until we find a good place from which to
8890
             continue parsing.  */
8891
          cp_parser_skip_to_closing_parenthesis (parser,
8892
                                                 /*recovering=*/true,
8893
                                                 /*or_comma=*/true,
8894
                                                 /*consume_paren=*/false);
8895
          /* If parsing tentatively, permanently remove the
8896
             template argument list.  That will prevent duplicate
8897
             error messages from being issued about the missing
8898
             "template" keyword.  */
8899
          if (start)
8900
            cp_lexer_purge_tokens_after (parser->lexer, start);
8901
          if (is_identifier)
8902
            *is_identifier = true;
8903
          return identifier;
8904
        }
8905
 
8906
      /* If the "template" keyword is present, then there is generally
8907
         no point in doing name-lookup, so we just return IDENTIFIER.
8908
         But, if the qualifying scope is non-dependent then we can
8909
         (and must) do name-lookup normally.  */
8910
      if (template_keyword_p
8911
          && (!parser->scope
8912
              || (TYPE_P (parser->scope)
8913
                  && dependent_type_p (parser->scope))))
8914
        return identifier;
8915
    }
8916
 
8917
  /* Look up the name.  */
8918
  decl = cp_parser_lookup_name (parser, identifier,
8919
                                none_type,
8920
                                /*is_template=*/false,
8921
                                /*is_namespace=*/false,
8922
                                check_dependency_p,
8923
                                /*ambiguous_decls=*/NULL);
8924
  decl = maybe_get_template_decl_from_type_decl (decl);
8925
 
8926
  /* If DECL is a template, then the name was a template-name.  */
8927
  if (TREE_CODE (decl) == TEMPLATE_DECL)
8928
    ;
8929
  else
8930
    {
8931
      tree fn = NULL_TREE;
8932
 
8933
      /* The standard does not explicitly indicate whether a name that
8934
         names a set of overloaded declarations, some of which are
8935
         templates, is a template-name.  However, such a name should
8936
         be a template-name; otherwise, there is no way to form a
8937
         template-id for the overloaded templates.  */
8938
      fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8939
      if (TREE_CODE (fns) == OVERLOAD)
8940
        for (fn = fns; fn; fn = OVL_NEXT (fn))
8941
          if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8942
            break;
8943
 
8944
      if (!fn)
8945
        {
8946
          /* The name does not name a template.  */
8947
          cp_parser_error (parser, "expected template-name");
8948
          return error_mark_node;
8949
        }
8950
    }
8951
 
8952
  /* If DECL is dependent, and refers to a function, then just return
8953
     its name; we will look it up again during template instantiation.  */
8954
  if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8955
    {
8956
      tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8957
      if (TYPE_P (scope) && dependent_type_p (scope))
8958
        return identifier;
8959
    }
8960
 
8961
  return decl;
8962
}
8963
 
8964
/* Parse a template-argument-list.
8965
 
8966
   template-argument-list:
8967
     template-argument
8968
     template-argument-list , template-argument
8969
 
8970
   Returns a TREE_VEC containing the arguments.  */
8971
 
8972
static tree
8973
cp_parser_template_argument_list (cp_parser* parser)
8974
{
8975
  tree fixed_args[10];
8976
  unsigned n_args = 0;
8977
  unsigned alloced = 10;
8978
  tree *arg_ary = fixed_args;
8979
  tree vec;
8980
  bool saved_in_template_argument_list_p;
8981
  bool saved_ice_p;
8982
  bool saved_non_ice_p;
8983
 
8984
  saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8985
  parser->in_template_argument_list_p = true;
8986
  /* Even if the template-id appears in an integral
8987
     constant-expression, the contents of the argument list do
8988
     not.  */
8989
  saved_ice_p = parser->integral_constant_expression_p;
8990
  parser->integral_constant_expression_p = false;
8991
  saved_non_ice_p = parser->non_integral_constant_expression_p;
8992
  parser->non_integral_constant_expression_p = false;
8993
  /* Parse the arguments.  */
8994
  do
8995
    {
8996
      tree argument;
8997
 
8998
      if (n_args)
8999
        /* Consume the comma.  */
9000
        cp_lexer_consume_token (parser->lexer);
9001
 
9002
      /* Parse the template-argument.  */
9003
      argument = cp_parser_template_argument (parser);
9004
      if (n_args == alloced)
9005
        {
9006
          alloced *= 2;
9007
 
9008
          if (arg_ary == fixed_args)
9009
            {
9010
              arg_ary = xmalloc (sizeof (tree) * alloced);
9011
              memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9012
            }
9013
          else
9014
            arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
9015
        }
9016
      arg_ary[n_args++] = argument;
9017
    }
9018
  while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9019
 
9020
  vec = make_tree_vec (n_args);
9021
 
9022
  while (n_args--)
9023
    TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9024
 
9025
  if (arg_ary != fixed_args)
9026
    free (arg_ary);
9027
  parser->non_integral_constant_expression_p = saved_non_ice_p;
9028
  parser->integral_constant_expression_p = saved_ice_p;
9029
  parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9030
  return vec;
9031
}
9032
 
9033
/* Parse a template-argument.
9034
 
9035
   template-argument:
9036
     assignment-expression
9037
     type-id
9038
     id-expression
9039
 
9040
   The representation is that of an assignment-expression, type-id, or
9041
   id-expression -- except that the qualified id-expression is
9042
   evaluated, so that the value returned is either a DECL or an
9043
   OVERLOAD.
9044
 
9045
   Although the standard says "assignment-expression", it forbids
9046
   throw-expressions or assignments in the template argument.
9047
   Therefore, we use "conditional-expression" instead.  */
9048
 
9049
static tree
9050
cp_parser_template_argument (cp_parser* parser)
9051
{
9052
  tree argument;
9053
  bool template_p;
9054
  bool address_p;
9055
  bool maybe_type_id = false;
9056
  cp_token *token;
9057
  cp_id_kind idk;
9058
 
9059
  /* There's really no way to know what we're looking at, so we just
9060
     try each alternative in order.
9061
 
9062
       [temp.arg]
9063
 
9064
       In a template-argument, an ambiguity between a type-id and an
9065
       expression is resolved to a type-id, regardless of the form of
9066
       the corresponding template-parameter.
9067
 
9068
     Therefore, we try a type-id first.  */
9069
  cp_parser_parse_tentatively (parser);
9070
  argument = cp_parser_type_id (parser);
9071
  /* If there was no error parsing the type-id but the next token is a '>>',
9072
     we probably found a typo for '> >'. But there are type-id which are
9073
     also valid expressions. For instance:
9074
 
9075
     struct X { int operator >> (int); };
9076
     template <int V> struct Foo {};
9077
     Foo<X () >> 5> r;
9078
 
9079
     Here 'X()' is a valid type-id of a function type, but the user just
9080
     wanted to write the expression "X() >> 5". Thus, we remember that we
9081
     found a valid type-id, but we still try to parse the argument as an
9082
     expression to see what happens.  */
9083
  if (!cp_parser_error_occurred (parser)
9084
      && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9085
    {
9086
      maybe_type_id = true;
9087
      cp_parser_abort_tentative_parse (parser);
9088
    }
9089
  else
9090
    {
9091
      /* If the next token isn't a `,' or a `>', then this argument wasn't
9092
      really finished. This means that the argument is not a valid
9093
      type-id.  */
9094
      if (!cp_parser_next_token_ends_template_argument_p (parser))
9095
        cp_parser_error (parser, "expected template-argument");
9096
      /* If that worked, we're done.  */
9097
      if (cp_parser_parse_definitely (parser))
9098
        return argument;
9099
    }
9100
  /* We're still not sure what the argument will be.  */
9101
  cp_parser_parse_tentatively (parser);
9102
  /* Try a template.  */
9103
  argument = cp_parser_id_expression (parser,
9104
                                      /*template_keyword_p=*/false,
9105
                                      /*check_dependency_p=*/true,
9106
                                      &template_p,
9107
                                      /*declarator_p=*/false);
9108
  /* If the next token isn't a `,' or a `>', then this argument wasn't
9109
     really finished.  */
9110
  if (!cp_parser_next_token_ends_template_argument_p (parser))
9111
    cp_parser_error (parser, "expected template-argument");
9112
  if (!cp_parser_error_occurred (parser))
9113
    {
9114
      /* Figure out what is being referred to.  If the id-expression
9115
         was for a class template specialization, then we will have a
9116
         TYPE_DECL at this point.  There is no need to do name lookup
9117
         at this point in that case.  */
9118
      if (TREE_CODE (argument) != TYPE_DECL)
9119
        argument = cp_parser_lookup_name (parser, argument,
9120
                                          none_type,
9121
                                          /*is_template=*/template_p,
9122
                                          /*is_namespace=*/false,
9123
                                          /*check_dependency=*/true,
9124
                                          /*ambiguous_decls=*/NULL);
9125
      if (TREE_CODE (argument) != TEMPLATE_DECL
9126
          && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9127
        cp_parser_error (parser, "expected template-name");
9128
    }
9129
  if (cp_parser_parse_definitely (parser))
9130
    return argument;
9131
  /* It must be a non-type argument.  There permitted cases are given
9132
     in [temp.arg.nontype]:
9133
 
9134
     -- an integral constant-expression of integral or enumeration
9135
        type; or
9136
 
9137
     -- the name of a non-type template-parameter; or
9138
 
9139
     -- the name of an object or function with external linkage...
9140
 
9141
     -- the address of an object or function with external linkage...
9142
 
9143
     -- a pointer to member...  */
9144
  /* Look for a non-type template parameter.  */
9145
  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9146
    {
9147
      cp_parser_parse_tentatively (parser);
9148
      argument = cp_parser_primary_expression (parser,
9149
                                               /*adress_p=*/false,
9150
                                               /*cast_p=*/false,
9151
                                               /*template_arg_p=*/true,
9152
                                               &idk);
9153
      if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9154
          || !cp_parser_next_token_ends_template_argument_p (parser))
9155
        cp_parser_simulate_error (parser);
9156
      if (cp_parser_parse_definitely (parser))
9157
        return argument;
9158
    }
9159
 
9160
  /* If the next token is "&", the argument must be the address of an
9161
     object or function with external linkage.  */
9162
  address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9163
  if (address_p)
9164
    cp_lexer_consume_token (parser->lexer);
9165
  /* See if we might have an id-expression.  */
9166
  token = cp_lexer_peek_token (parser->lexer);
9167
  if (token->type == CPP_NAME
9168
      || token->keyword == RID_OPERATOR
9169
      || token->type == CPP_SCOPE
9170
      || token->type == CPP_TEMPLATE_ID
9171
      || token->type == CPP_NESTED_NAME_SPECIFIER)
9172
    {
9173
      cp_parser_parse_tentatively (parser);
9174
      argument = cp_parser_primary_expression (parser,
9175
                                               address_p,
9176
                                               /*cast_p=*/false,
9177
                                               /*template_arg_p=*/true,
9178
                                               &idk);
9179
      if (cp_parser_error_occurred (parser)
9180
          || !cp_parser_next_token_ends_template_argument_p (parser))
9181
        cp_parser_abort_tentative_parse (parser);
9182
      else
9183
        {
9184
          if (TREE_CODE (argument) == INDIRECT_REF)
9185
            {
9186
              gcc_assert (REFERENCE_REF_P (argument));
9187
              argument = TREE_OPERAND (argument, 0);
9188
            }
9189
 
9190
          if (TREE_CODE (argument) == BASELINK)
9191
            /* We don't need the information about what class was used
9192
               to name the overloaded functions.  */
9193
            argument = BASELINK_FUNCTIONS (argument);
9194
 
9195
          if (TREE_CODE (argument) == VAR_DECL)
9196
            {
9197
              /* A variable without external linkage might still be a
9198
                 valid constant-expression, so no error is issued here
9199
                 if the external-linkage check fails.  */
9200
              if (!DECL_EXTERNAL_LINKAGE_P (argument))
9201
                cp_parser_simulate_error (parser);
9202
            }
9203
          else if (is_overloaded_fn (argument))
9204
            /* All overloaded functions are allowed; if the external
9205
               linkage test does not pass, an error will be issued
9206
               later.  */
9207
            ;
9208
          else if (address_p
9209
                   && (TREE_CODE (argument) == OFFSET_REF
9210
                       || TREE_CODE (argument) == SCOPE_REF))
9211
            /* A pointer-to-member.  */
9212
            ;
9213
          else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9214
            ;
9215
          else
9216
            cp_parser_simulate_error (parser);
9217
 
9218
          if (cp_parser_parse_definitely (parser))
9219
            {
9220
              if (address_p)
9221
                argument = build_x_unary_op (ADDR_EXPR, argument);
9222
              return argument;
9223
            }
9224
        }
9225
    }
9226
  /* If the argument started with "&", there are no other valid
9227
     alternatives at this point.  */
9228
  if (address_p)
9229
    {
9230
      cp_parser_error (parser, "invalid non-type template argument");
9231
      return error_mark_node;
9232
    }
9233
 
9234
  /* If the argument wasn't successfully parsed as a type-id followed
9235
     by '>>', the argument can only be a constant expression now.
9236
     Otherwise, we try parsing the constant-expression tentatively,
9237
     because the argument could really be a type-id.  */
9238
  if (maybe_type_id)
9239
    cp_parser_parse_tentatively (parser);
9240
  argument = cp_parser_constant_expression (parser,
9241
                                            /*allow_non_constant_p=*/false,
9242
                                            /*non_constant_p=*/NULL);
9243
  argument = fold_non_dependent_expr (argument);
9244
  if (!maybe_type_id)
9245
    return argument;
9246
  if (!cp_parser_next_token_ends_template_argument_p (parser))
9247
    cp_parser_error (parser, "expected template-argument");
9248
  if (cp_parser_parse_definitely (parser))
9249
    return argument;
9250
  /* We did our best to parse the argument as a non type-id, but that
9251
     was the only alternative that matched (albeit with a '>' after
9252
     it). We can assume it's just a typo from the user, and a
9253
     diagnostic will then be issued.  */
9254
  return cp_parser_type_id (parser);
9255
}
9256
 
9257
/* Parse an explicit-instantiation.
9258
 
9259
   explicit-instantiation:
9260
     template declaration
9261
 
9262
   Although the standard says `declaration', what it really means is:
9263
 
9264
   explicit-instantiation:
9265
     template decl-specifier-seq [opt] declarator [opt] ;
9266
 
9267
   Things like `template int S<int>::i = 5, int S<double>::j;' are not
9268
   supposed to be allowed.  A defect report has been filed about this
9269
   issue.
9270
 
9271
   GNU Extension:
9272
 
9273
   explicit-instantiation:
9274
     storage-class-specifier template
9275
       decl-specifier-seq [opt] declarator [opt] ;
9276
     function-specifier template
9277
       decl-specifier-seq [opt] declarator [opt] ;  */
9278
 
9279
static void
9280
cp_parser_explicit_instantiation (cp_parser* parser)
9281
{
9282
  int declares_class_or_enum;
9283
  cp_decl_specifier_seq decl_specifiers;
9284
  tree extension_specifier = NULL_TREE;
9285
 
9286
  /* Look for an (optional) storage-class-specifier or
9287
     function-specifier.  */
9288
  if (cp_parser_allow_gnu_extensions_p (parser))
9289
    {
9290
      extension_specifier
9291
        = cp_parser_storage_class_specifier_opt (parser);
9292
      if (!extension_specifier)
9293
        extension_specifier
9294
          = cp_parser_function_specifier_opt (parser,
9295
                                              /*decl_specs=*/NULL);
9296
    }
9297
 
9298
  /* Look for the `template' keyword.  */
9299
  cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9300
  /* Let the front end know that we are processing an explicit
9301
     instantiation.  */
9302
  begin_explicit_instantiation ();
9303
  /* [temp.explicit] says that we are supposed to ignore access
9304
     control while processing explicit instantiation directives.  */
9305
  push_deferring_access_checks (dk_no_check);
9306
  /* Parse a decl-specifier-seq.  */
9307
  cp_parser_decl_specifier_seq (parser,
9308
                                CP_PARSER_FLAGS_OPTIONAL,
9309
                                &decl_specifiers,
9310
                                &declares_class_or_enum);
9311
  /* If there was exactly one decl-specifier, and it declared a class,
9312
     and there's no declarator, then we have an explicit type
9313
     instantiation.  */
9314
  if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9315
    {
9316
      tree type;
9317
 
9318
      type = check_tag_decl (&decl_specifiers);
9319
      /* Turn access control back on for names used during
9320
         template instantiation.  */
9321
      pop_deferring_access_checks ();
9322
      if (type)
9323
        do_type_instantiation (type, extension_specifier, /*complain=*/1);
9324
    }
9325
  else
9326
    {
9327
      cp_declarator *declarator;
9328
      tree decl;
9329
 
9330
      /* Parse the declarator.  */
9331
      declarator
9332
        = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9333
                                /*ctor_dtor_or_conv_p=*/NULL,
9334
                                /*parenthesized_p=*/NULL,
9335
                                /*member_p=*/false);
9336
      if (declares_class_or_enum & 2)
9337
        cp_parser_check_for_definition_in_return_type (declarator,
9338
                                                       decl_specifiers.type);
9339
      if (declarator != cp_error_declarator)
9340
        {
9341
          decl = grokdeclarator (declarator, &decl_specifiers,
9342
                                 NORMAL, 0, NULL);
9343
          /* Turn access control back on for names used during
9344
             template instantiation.  */
9345
          pop_deferring_access_checks ();
9346
          /* Do the explicit instantiation.  */
9347
          do_decl_instantiation (decl, extension_specifier);
9348
        }
9349
      else
9350
        {
9351
          pop_deferring_access_checks ();
9352
          /* Skip the body of the explicit instantiation.  */
9353
          cp_parser_skip_to_end_of_statement (parser);
9354
        }
9355
    }
9356
  /* We're done with the instantiation.  */
9357
  end_explicit_instantiation ();
9358
 
9359
  cp_parser_consume_semicolon_at_end_of_statement (parser);
9360
}
9361
 
9362
/* Parse an explicit-specialization.
9363
 
9364
   explicit-specialization:
9365
     template < > declaration
9366
 
9367
   Although the standard says `declaration', what it really means is:
9368
 
9369
   explicit-specialization:
9370
     template <> decl-specifier [opt] init-declarator [opt] ;
9371
     template <> function-definition
9372
     template <> explicit-specialization
9373
     template <> template-declaration  */
9374
 
9375
static void
9376
cp_parser_explicit_specialization (cp_parser* parser)
9377
{
9378
  bool need_lang_pop;
9379
  /* Look for the `template' keyword.  */
9380
  cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9381
  /* Look for the `<'.  */
9382
  cp_parser_require (parser, CPP_LESS, "`<'");
9383
  /* Look for the `>'.  */
9384
  cp_parser_require (parser, CPP_GREATER, "`>'");
9385
  /* We have processed another parameter list.  */
9386
  ++parser->num_template_parameter_lists;
9387
  /* [temp]
9388
 
9389
     A template ... explicit specialization ... shall not have C
9390
     linkage.  */
9391
  if (current_lang_name == lang_name_c)
9392
    {
9393
      error ("template specialization with C linkage");
9394
      /* Give it C++ linkage to avoid confusing other parts of the
9395
         front end.  */
9396
      push_lang_context (lang_name_cplusplus);
9397
      need_lang_pop = true;
9398
    }
9399
  else
9400
    need_lang_pop = false;
9401
  /* Let the front end know that we are beginning a specialization.  */
9402
  begin_specialization ();
9403
  /* If the next keyword is `template', we need to figure out whether
9404
     or not we're looking a template-declaration.  */
9405
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9406
    {
9407
      if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9408
          && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9409
        cp_parser_template_declaration_after_export (parser,
9410
                                                     /*member_p=*/false);
9411
      else
9412
        cp_parser_explicit_specialization (parser);
9413
    }
9414
  else
9415
    /* Parse the dependent declaration.  */
9416
    cp_parser_single_declaration (parser,
9417
                                  /*checks=*/NULL_TREE,
9418
                                  /*member_p=*/false,
9419
                                  /*friend_p=*/NULL);
9420
  /* We're done with the specialization.  */
9421
  end_specialization ();
9422
  /* For the erroneous case of a template with C linkage, we pushed an
9423
     implicit C++ linkage scope; exit that scope now.  */
9424
  if (need_lang_pop)
9425
    pop_lang_context ();
9426
  /* We're done with this parameter list.  */
9427
  --parser->num_template_parameter_lists;
9428
}
9429
 
9430
/* Parse a type-specifier.
9431
 
9432
   type-specifier:
9433
     simple-type-specifier
9434
     class-specifier
9435
     enum-specifier
9436
     elaborated-type-specifier
9437
     cv-qualifier
9438
 
9439
   GNU Extension:
9440
 
9441
   type-specifier:
9442
     __complex__
9443
 
9444
   Returns a representation of the type-specifier.  For a
9445
   class-specifier, enum-specifier, or elaborated-type-specifier, a
9446
   TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9447
 
9448
   The parser flags FLAGS is used to control type-specifier parsing.
9449
 
9450
   If IS_DECLARATION is TRUE, then this type-specifier is appearing
9451
   in a decl-specifier-seq.
9452
 
9453
   If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9454
   class-specifier, enum-specifier, or elaborated-type-specifier, then
9455
   *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9456
   if a type is declared; 2 if it is defined.  Otherwise, it is set to
9457
   zero.
9458
 
9459
   If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9460
   cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9461
   is set to FALSE.  */
9462
 
9463
static tree
9464
cp_parser_type_specifier (cp_parser* parser,
9465
                          cp_parser_flags flags,
9466
                          cp_decl_specifier_seq *decl_specs,
9467
                          bool is_declaration,
9468
                          int* declares_class_or_enum,
9469
                          bool* is_cv_qualifier)
9470
{
9471
  tree type_spec = NULL_TREE;
9472
  cp_token *token;
9473
  enum rid keyword;
9474
  cp_decl_spec ds = ds_last;
9475
 
9476
  /* Assume this type-specifier does not declare a new type.  */
9477
  if (declares_class_or_enum)
9478
    *declares_class_or_enum = 0;
9479
  /* And that it does not specify a cv-qualifier.  */
9480
  if (is_cv_qualifier)
9481
    *is_cv_qualifier = false;
9482
  /* Peek at the next token.  */
9483
  token = cp_lexer_peek_token (parser->lexer);
9484
 
9485
  /* If we're looking at a keyword, we can use that to guide the
9486
     production we choose.  */
9487
  keyword = token->keyword;
9488
  switch (keyword)
9489
    {
9490
    case RID_ENUM:
9491
      /* 'enum' [identifier] '{' introduces an enum-specifier;
9492
         'enum' <anything else> introduces an elaborated-type-specifier.  */
9493
      if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9494
          || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9495
              && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9496
                 == CPP_OPEN_BRACE))
9497
        {
9498
          if (parser->num_template_parameter_lists)
9499
            {
9500
              error ("template declaration of %qs", "enum");
9501
              cp_parser_skip_to_end_of_block_or_statement (parser);
9502
              type_spec = error_mark_node;
9503
            }
9504
          else
9505
            type_spec = cp_parser_enum_specifier (parser);
9506
 
9507
          if (declares_class_or_enum)
9508
            *declares_class_or_enum = 2;
9509
          if (decl_specs)
9510
            cp_parser_set_decl_spec_type (decl_specs,
9511
                                          type_spec,
9512
                                          /*user_defined_p=*/true);
9513
          return type_spec;
9514
        }
9515
      else
9516
        goto elaborated_type_specifier;
9517
 
9518
      /* Any of these indicate either a class-specifier, or an
9519
         elaborated-type-specifier.  */
9520
    case RID_CLASS:
9521
    case RID_STRUCT:
9522
    case RID_UNION:
9523
      /* Parse tentatively so that we can back up if we don't find a
9524
         class-specifier.  */
9525
      cp_parser_parse_tentatively (parser);
9526
      /* Look for the class-specifier.  */
9527
      type_spec = cp_parser_class_specifier (parser);
9528
      /* If that worked, we're done.  */
9529
      if (cp_parser_parse_definitely (parser))
9530
        {
9531
          if (declares_class_or_enum)
9532
            *declares_class_or_enum = 2;
9533
          if (decl_specs)
9534
            cp_parser_set_decl_spec_type (decl_specs,
9535
                                          type_spec,
9536
                                          /*user_defined_p=*/true);
9537
          return type_spec;
9538
        }
9539
 
9540
      /* Fall through.  */
9541
    elaborated_type_specifier:
9542
      /* We're declaring (not defining) a class or enum.  */
9543
      if (declares_class_or_enum)
9544
        *declares_class_or_enum = 1;
9545
 
9546
      /* Fall through.  */
9547
    case RID_TYPENAME:
9548
      /* Look for an elaborated-type-specifier.  */
9549
      type_spec
9550
        = (cp_parser_elaborated_type_specifier
9551
           (parser,
9552
            decl_specs && decl_specs->specs[(int) ds_friend],
9553
            is_declaration));
9554
      if (decl_specs)
9555
        cp_parser_set_decl_spec_type (decl_specs,
9556
                                      type_spec,
9557
                                      /*user_defined_p=*/true);
9558
      return type_spec;
9559
 
9560
    case RID_CONST:
9561
      ds = ds_const;
9562
      if (is_cv_qualifier)
9563
        *is_cv_qualifier = true;
9564
      break;
9565
 
9566
    case RID_VOLATILE:
9567
      ds = ds_volatile;
9568
      if (is_cv_qualifier)
9569
        *is_cv_qualifier = true;
9570
      break;
9571
 
9572
    case RID_RESTRICT:
9573
      ds = ds_restrict;
9574
      if (is_cv_qualifier)
9575
        *is_cv_qualifier = true;
9576
      break;
9577
 
9578
    case RID_COMPLEX:
9579
      /* The `__complex__' keyword is a GNU extension.  */
9580
      ds = ds_complex;
9581
      break;
9582
 
9583
    default:
9584
      break;
9585
    }
9586
 
9587
  /* Handle simple keywords.  */
9588
  if (ds != ds_last)
9589
    {
9590
      if (decl_specs)
9591
        {
9592
          ++decl_specs->specs[(int)ds];
9593
          decl_specs->any_specifiers_p = true;
9594
        }
9595
      return cp_lexer_consume_token (parser->lexer)->value;
9596
    }
9597
 
9598
  /* If we do not already have a type-specifier, assume we are looking
9599
     at a simple-type-specifier.  */
9600
  type_spec = cp_parser_simple_type_specifier (parser,
9601
                                               decl_specs,
9602
                                               flags);
9603
 
9604
  /* If we didn't find a type-specifier, and a type-specifier was not
9605
     optional in this context, issue an error message.  */
9606
  if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9607
    {
9608
      cp_parser_error (parser, "expected type specifier");
9609
      return error_mark_node;
9610
    }
9611
 
9612
  return type_spec;
9613
}
9614
 
9615
/* Parse a simple-type-specifier.
9616
 
9617
   simple-type-specifier:
9618
     :: [opt] nested-name-specifier [opt] type-name
9619
     :: [opt] nested-name-specifier template template-id
9620
     char
9621
     wchar_t
9622
     bool
9623
     short
9624
     int
9625
     long
9626
     signed
9627
     unsigned
9628
     float
9629
     double
9630
     void
9631
 
9632
   GNU Extension:
9633
 
9634
   simple-type-specifier:
9635
     __typeof__ unary-expression
9636
     __typeof__ ( type-id )
9637
 
9638
   Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9639
   appropriately updated.  */
9640
 
9641
static tree
9642
cp_parser_simple_type_specifier (cp_parser* parser,
9643
                                 cp_decl_specifier_seq *decl_specs,
9644
                                 cp_parser_flags flags)
9645
{
9646
  tree type = NULL_TREE;
9647
  cp_token *token;
9648
 
9649
  /* Peek at the next token.  */
9650
  token = cp_lexer_peek_token (parser->lexer);
9651
 
9652
  /* If we're looking at a keyword, things are easy.  */
9653
  switch (token->keyword)
9654
    {
9655
    case RID_CHAR:
9656
      if (decl_specs)
9657
        decl_specs->explicit_char_p = true;
9658
      type = char_type_node;
9659
      break;
9660
    case RID_WCHAR:
9661
      type = wchar_type_node;
9662
      break;
9663
    case RID_BOOL:
9664
      type = boolean_type_node;
9665
      break;
9666
    case RID_SHORT:
9667
      if (decl_specs)
9668
        ++decl_specs->specs[(int) ds_short];
9669
      type = short_integer_type_node;
9670
      break;
9671
    case RID_INT:
9672
      if (decl_specs)
9673
        decl_specs->explicit_int_p = true;
9674
      type = integer_type_node;
9675
      break;
9676
    case RID_LONG:
9677
      if (decl_specs)
9678
        ++decl_specs->specs[(int) ds_long];
9679
      type = long_integer_type_node;
9680
      break;
9681
    case RID_SIGNED:
9682
      if (decl_specs)
9683
        ++decl_specs->specs[(int) ds_signed];
9684
      type = integer_type_node;
9685
      break;
9686
    case RID_UNSIGNED:
9687
      if (decl_specs)
9688
        ++decl_specs->specs[(int) ds_unsigned];
9689
      type = unsigned_type_node;
9690
      break;
9691
    case RID_FLOAT:
9692
      type = float_type_node;
9693
      break;
9694
    case RID_DOUBLE:
9695
      type = double_type_node;
9696
      break;
9697
    case RID_VOID:
9698
      type = void_type_node;
9699
      break;
9700
 
9701
    case RID_TYPEOF:
9702
      /* Consume the `typeof' token.  */
9703
      cp_lexer_consume_token (parser->lexer);
9704
      /* Parse the operand to `typeof'.  */
9705
      type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9706
      /* If it is not already a TYPE, take its type.  */
9707
      if (!TYPE_P (type))
9708
        type = finish_typeof (type);
9709
 
9710
      if (decl_specs)
9711
        cp_parser_set_decl_spec_type (decl_specs, type,
9712
                                      /*user_defined_p=*/true);
9713
 
9714
      return type;
9715
 
9716
    default:
9717
      break;
9718
    }
9719
 
9720
  /* If the type-specifier was for a built-in type, we're done.  */
9721
  if (type)
9722
    {
9723
      tree id;
9724
 
9725
      /* Record the type.  */
9726
      if (decl_specs
9727
          && (token->keyword != RID_SIGNED
9728
              && token->keyword != RID_UNSIGNED
9729
              && token->keyword != RID_SHORT
9730
              && token->keyword != RID_LONG))
9731
        cp_parser_set_decl_spec_type (decl_specs,
9732
                                      type,
9733
                                      /*user_defined=*/false);
9734
      if (decl_specs)
9735
        decl_specs->any_specifiers_p = true;
9736
 
9737
      /* Consume the token.  */
9738
      id = cp_lexer_consume_token (parser->lexer)->value;
9739
 
9740
      /* There is no valid C++ program where a non-template type is
9741
         followed by a "<".  That usually indicates that the user thought
9742
         that the type was a template.  */
9743
      cp_parser_check_for_invalid_template_id (parser, type);
9744
 
9745
      return TYPE_NAME (type);
9746
    }
9747
 
9748
  /* The type-specifier must be a user-defined type.  */
9749
  if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9750
    {
9751
      bool qualified_p;
9752
      bool global_p;
9753
 
9754
      /* Don't gobble tokens or issue error messages if this is an
9755
         optional type-specifier.  */
9756
      if (flags & CP_PARSER_FLAGS_OPTIONAL)
9757
        cp_parser_parse_tentatively (parser);
9758
 
9759
      /* Look for the optional `::' operator.  */
9760
      global_p
9761
        = (cp_parser_global_scope_opt (parser,
9762
                                       /*current_scope_valid_p=*/false)
9763
           != NULL_TREE);
9764
      /* Look for the nested-name specifier.  */
9765
      qualified_p
9766
        = (cp_parser_nested_name_specifier_opt (parser,
9767
                                                /*typename_keyword_p=*/false,
9768
                                                /*check_dependency_p=*/true,
9769
                                                /*type_p=*/false,
9770
                                                /*is_declaration=*/false)
9771
           != NULL_TREE);
9772
      /* If we have seen a nested-name-specifier, and the next token
9773
         is `template', then we are using the template-id production.  */
9774
      if (parser->scope
9775
          && cp_parser_optional_template_keyword (parser))
9776
        {
9777
          /* Look for the template-id.  */
9778
          type = cp_parser_template_id (parser,
9779
                                        /*template_keyword_p=*/true,
9780
                                        /*check_dependency_p=*/true,
9781
                                        /*is_declaration=*/false);
9782
          /* If the template-id did not name a type, we are out of
9783
             luck.  */
9784
          if (TREE_CODE (type) != TYPE_DECL)
9785
            {
9786
              cp_parser_error (parser, "expected template-id for type");
9787
              type = NULL_TREE;
9788
            }
9789
        }
9790
      /* Otherwise, look for a type-name.  */
9791
      else
9792
        type = cp_parser_type_name (parser);
9793
      /* Keep track of all name-lookups performed in class scopes.  */
9794
      if (type
9795
          && !global_p
9796
          && !qualified_p
9797
          && TREE_CODE (type) == TYPE_DECL
9798
          && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9799
        maybe_note_name_used_in_class (DECL_NAME (type), type);
9800
      /* If it didn't work out, we don't have a TYPE.  */
9801
      if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9802
          && !cp_parser_parse_definitely (parser))
9803
        type = NULL_TREE;
9804
      if (type && decl_specs)
9805
        cp_parser_set_decl_spec_type (decl_specs, type,
9806
                                      /*user_defined=*/true);
9807
    }
9808
 
9809
  /* If we didn't get a type-name, issue an error message.  */
9810
  if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9811
    {
9812
      cp_parser_error (parser, "expected type-name");
9813
      return error_mark_node;
9814
    }
9815
 
9816
  /* There is no valid C++ program where a non-template type is
9817
     followed by a "<".  That usually indicates that the user thought
9818
     that the type was a template.  */
9819
  if (type && type != error_mark_node)
9820
    {
9821
      /* As a last-ditch effort, see if TYPE is an Objective-C type.
9822
         If it is, then the '<'...'>' enclose protocol names rather than
9823
         template arguments, and so everything is fine.  */
9824
      if (c_dialect_objc ()
9825
          && (objc_is_id (type) || objc_is_class_name (type)))
9826
        {
9827
          tree protos = cp_parser_objc_protocol_refs_opt (parser);
9828
          tree qual_type = objc_get_protocol_qualified_type (type, protos);
9829
 
9830
          /* Clobber the "unqualified" type previously entered into
9831
             DECL_SPECS with the new, improved protocol-qualified version.  */
9832
          if (decl_specs)
9833
            decl_specs->type = qual_type;
9834
 
9835
          return qual_type;
9836
        }
9837
 
9838
      cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9839
    }
9840
 
9841
  return type;
9842
}
9843
 
9844
/* Parse a type-name.
9845
 
9846
   type-name:
9847
     class-name
9848
     enum-name
9849
     typedef-name
9850
 
9851
   enum-name:
9852
     identifier
9853
 
9854
   typedef-name:
9855
     identifier
9856
 
9857
   Returns a TYPE_DECL for the type.  */
9858
 
9859
static tree
9860
cp_parser_type_name (cp_parser* parser)
9861
{
9862
  tree type_decl;
9863
  tree identifier;
9864
 
9865
  /* We can't know yet whether it is a class-name or not.  */
9866
  cp_parser_parse_tentatively (parser);
9867
  /* Try a class-name.  */
9868
  type_decl = cp_parser_class_name (parser,
9869
                                    /*typename_keyword_p=*/false,
9870
                                    /*template_keyword_p=*/false,
9871
                                    none_type,
9872
                                    /*check_dependency_p=*/true,
9873
                                    /*class_head_p=*/false,
9874
                                    /*is_declaration=*/false);
9875
  /* If it's not a class-name, keep looking.  */
9876
  if (!cp_parser_parse_definitely (parser))
9877
    {
9878
      /* It must be a typedef-name or an enum-name.  */
9879
      identifier = cp_parser_identifier (parser);
9880
      if (identifier == error_mark_node)
9881
        return error_mark_node;
9882
 
9883
      /* Look up the type-name.  */
9884
      type_decl = cp_parser_lookup_name_simple (parser, identifier);
9885
 
9886
      if (TREE_CODE (type_decl) != TYPE_DECL
9887
          && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9888
        {
9889
          /* See if this is an Objective-C type.  */
9890
          tree protos = cp_parser_objc_protocol_refs_opt (parser);
9891
          tree type = objc_get_protocol_qualified_type (identifier, protos);
9892
          if (type)
9893
            type_decl = TYPE_NAME (type);
9894
        }
9895
 
9896
      /* Issue an error if we did not find a type-name.  */
9897
      if (TREE_CODE (type_decl) != TYPE_DECL)
9898
        {
9899
          if (!cp_parser_simulate_error (parser))
9900
            cp_parser_name_lookup_error (parser, identifier, type_decl,
9901
                                         "is not a type");
9902
          type_decl = error_mark_node;
9903
        }
9904
      /* Remember that the name was used in the definition of the
9905
         current class so that we can check later to see if the
9906
         meaning would have been different after the class was
9907
         entirely defined.  */
9908
      else if (type_decl != error_mark_node
9909
               && !parser->scope)
9910
        maybe_note_name_used_in_class (identifier, type_decl);
9911
    }
9912
 
9913
  return type_decl;
9914
}
9915
 
9916
 
9917
/* Parse an elaborated-type-specifier.  Note that the grammar given
9918
   here incorporates the resolution to DR68.
9919
 
9920
   elaborated-type-specifier:
9921
     class-key :: [opt] nested-name-specifier [opt] identifier
9922
     class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9923
     enum :: [opt] nested-name-specifier [opt] identifier
9924
     typename :: [opt] nested-name-specifier identifier
9925
     typename :: [opt] nested-name-specifier template [opt]
9926
       template-id
9927
 
9928
   GNU extension:
9929
 
9930
   elaborated-type-specifier:
9931
     class-key attributes :: [opt] nested-name-specifier [opt] identifier
9932
     class-key attributes :: [opt] nested-name-specifier [opt]
9933
               template [opt] template-id
9934
     enum attributes :: [opt] nested-name-specifier [opt] identifier
9935
 
9936
   If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9937
   declared `friend'.  If IS_DECLARATION is TRUE, then this
9938
   elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9939
   something is being declared.
9940
 
9941
   Returns the TYPE specified.  */
9942
 
9943
static tree
9944
cp_parser_elaborated_type_specifier (cp_parser* parser,
9945
                                     bool is_friend,
9946
                                     bool is_declaration)
9947
{
9948
  enum tag_types tag_type;
9949
  tree identifier;
9950
  tree type = NULL_TREE;
9951
  tree attributes = NULL_TREE;
9952
 
9953
  /* See if we're looking at the `enum' keyword.  */
9954
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9955
    {
9956
      /* Consume the `enum' token.  */
9957
      cp_lexer_consume_token (parser->lexer);
9958
      /* Remember that it's an enumeration type.  */
9959
      tag_type = enum_type;
9960
      /* Parse the attributes.  */
9961
      attributes = cp_parser_attributes_opt (parser);
9962
    }
9963
  /* Or, it might be `typename'.  */
9964
  else if (cp_lexer_next_token_is_keyword (parser->lexer,
9965
                                           RID_TYPENAME))
9966
    {
9967
      /* Consume the `typename' token.  */
9968
      cp_lexer_consume_token (parser->lexer);
9969
      /* Remember that it's a `typename' type.  */
9970
      tag_type = typename_type;
9971
      /* The `typename' keyword is only allowed in templates.  */
9972
      if (!processing_template_decl)
9973
        pedwarn ("using %<typename%> outside of template");
9974
    }
9975
  /* Otherwise it must be a class-key.  */
9976
  else
9977
    {
9978
      tag_type = cp_parser_class_key (parser);
9979
      if (tag_type == none_type)
9980
        return error_mark_node;
9981
      /* Parse the attributes.  */
9982
      attributes = cp_parser_attributes_opt (parser);
9983
    }
9984
 
9985
  /* Look for the `::' operator.  */
9986
  cp_parser_global_scope_opt (parser,
9987
                              /*current_scope_valid_p=*/false);
9988
  /* Look for the nested-name-specifier.  */
9989
  if (tag_type == typename_type)
9990
    {
9991
      if (!cp_parser_nested_name_specifier (parser,
9992
                                           /*typename_keyword_p=*/true,
9993
                                           /*check_dependency_p=*/true,
9994
                                           /*type_p=*/true,
9995
                                            is_declaration))
9996
        return error_mark_node;
9997
    }
9998
  else
9999
    /* Even though `typename' is not present, the proposed resolution
10000
       to Core Issue 180 says that in `class A<T>::B', `B' should be
10001
       considered a type-name, even if `A<T>' is dependent.  */
10002
    cp_parser_nested_name_specifier_opt (parser,
10003
                                         /*typename_keyword_p=*/true,
10004
                                         /*check_dependency_p=*/true,
10005
                                         /*type_p=*/true,
10006
                                         is_declaration);
10007
  /* For everything but enumeration types, consider a template-id.  */
10008
  if (tag_type != enum_type)
10009
    {
10010
      bool template_p = false;
10011
      tree decl;
10012
 
10013
      /* Allow the `template' keyword.  */
10014
      template_p = cp_parser_optional_template_keyword (parser);
10015
      /* If we didn't see `template', we don't know if there's a
10016
         template-id or not.  */
10017
      if (!template_p)
10018
        cp_parser_parse_tentatively (parser);
10019
      /* Parse the template-id.  */
10020
      decl = cp_parser_template_id (parser, template_p,
10021
                                    /*check_dependency_p=*/true,
10022
                                    is_declaration);
10023
      /* If we didn't find a template-id, look for an ordinary
10024
         identifier.  */
10025
      if (!template_p && !cp_parser_parse_definitely (parser))
10026
        ;
10027
      /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10028
         in effect, then we must assume that, upon instantiation, the
10029
         template will correspond to a class.  */
10030
      else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10031
               && tag_type == typename_type)
10032
        type = make_typename_type (parser->scope, decl,
10033
                                   typename_type,
10034
                                   /*complain=*/1);
10035
      else
10036
        type = TREE_TYPE (decl);
10037
    }
10038
 
10039
  /* For an enumeration type, consider only a plain identifier.  */
10040
  if (!type)
10041
    {
10042
      identifier = cp_parser_identifier (parser);
10043
 
10044
      if (identifier == error_mark_node)
10045
        {
10046
          parser->scope = NULL_TREE;
10047
          return error_mark_node;
10048
        }
10049
 
10050
      /* For a `typename', we needn't call xref_tag.  */
10051
      if (tag_type == typename_type
10052
          && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10053
        return cp_parser_make_typename_type (parser, parser->scope,
10054
                                             identifier);
10055
      /* Look up a qualified name in the usual way.  */
10056
      if (parser->scope)
10057
        {
10058
          tree decl;
10059
 
10060
          decl = cp_parser_lookup_name (parser, identifier,
10061
                                        tag_type,
10062
                                        /*is_template=*/false,
10063
                                        /*is_namespace=*/false,
10064
                                        /*check_dependency=*/true,
10065
                                        /*ambiguous_decls=*/NULL);
10066
 
10067
          /* If we are parsing friend declaration, DECL may be a
10068
             TEMPLATE_DECL tree node here.  However, we need to check
10069
             whether this TEMPLATE_DECL results in valid code.  Consider
10070
             the following example:
10071
 
10072
               namespace N {
10073
                 template <class T> class C {};
10074
               }
10075
               class X {
10076
                 template <class T> friend class N::C; // #1, valid code
10077
               };
10078
               template <class T> class Y {
10079
                 friend class N::C;                    // #2, invalid code
10080
               };
10081
 
10082
             For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10083
             name lookup of `N::C'.  We see that friend declaration must
10084
             be template for the code to be valid.  Note that
10085
             processing_template_decl does not work here since it is
10086
             always 1 for the above two cases.  */
10087
 
10088
          decl = (cp_parser_maybe_treat_template_as_class
10089
                  (decl, /*tag_name_p=*/is_friend
10090
                         && parser->num_template_parameter_lists));
10091
 
10092
          if (TREE_CODE (decl) != TYPE_DECL)
10093
            {
10094
              cp_parser_diagnose_invalid_type_name (parser,
10095
                                                    parser->scope,
10096
                                                    identifier);
10097
              return error_mark_node;
10098
            }
10099
 
10100
          if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10101
            check_elaborated_type_specifier
10102
              (tag_type, decl,
10103
               (parser->num_template_parameter_lists
10104
                || DECL_SELF_REFERENCE_P (decl)));
10105
 
10106
          type = TREE_TYPE (decl);
10107
        }
10108
      else
10109
        {
10110
          /* An elaborated-type-specifier sometimes introduces a new type and
10111
             sometimes names an existing type.  Normally, the rule is that it
10112
             introduces a new type only if there is not an existing type of
10113
             the same name already in scope.  For example, given:
10114
 
10115
               struct S {};
10116
               void f() { struct S s; }
10117
 
10118
             the `struct S' in the body of `f' is the same `struct S' as in
10119
             the global scope; the existing definition is used.  However, if
10120
             there were no global declaration, this would introduce a new
10121
             local class named `S'.
10122
 
10123
             An exception to this rule applies to the following code:
10124
 
10125
               namespace N { struct S; }
10126
 
10127
             Here, the elaborated-type-specifier names a new type
10128
             unconditionally; even if there is already an `S' in the
10129
             containing scope this declaration names a new type.
10130
             This exception only applies if the elaborated-type-specifier
10131
             forms the complete declaration:
10132
 
10133
               [class.name]
10134
 
10135
               A declaration consisting solely of `class-key identifier ;' is
10136
               either a redeclaration of the name in the current scope or a
10137
               forward declaration of the identifier as a class name.  It
10138
               introduces the name into the current scope.
10139
 
10140
             We are in this situation precisely when the next token is a `;'.
10141
 
10142
             An exception to the exception is that a `friend' declaration does
10143
             *not* name a new type; i.e., given:
10144
 
10145
               struct S { friend struct T; };
10146
 
10147
             `T' is not a new type in the scope of `S'.
10148
 
10149
             Also, `new struct S' or `sizeof (struct S)' never results in the
10150
             definition of a new type; a new type can only be declared in a
10151
             declaration context.  */
10152
 
10153
          tag_scope ts;
10154
          bool template_p;
10155
 
10156
          if (is_friend)
10157
            /* Friends have special name lookup rules.  */
10158
            ts = ts_within_enclosing_non_class;
10159
          else if (is_declaration
10160
                   && cp_lexer_next_token_is (parser->lexer,
10161
                                              CPP_SEMICOLON))
10162
            /* This is a `class-key identifier ;' */
10163
            ts = ts_current;
10164
          else
10165
            ts = ts_global;
10166
 
10167
          /* Warn about attributes. They are ignored.  */
10168
          if (attributes)
10169
            warning (OPT_Wattributes,
10170
                     "type attributes are honored only at type definition");
10171
 
10172
          template_p =
10173
            (parser->num_template_parameter_lists
10174
             && (cp_parser_next_token_starts_class_definition_p (parser)
10175
                 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10176
          /* An unqualified name was used to reference this type, so
10177
             there were no qualifying templates.  */
10178
          if (!cp_parser_check_template_parameters (parser,
10179
                                                    /*num_templates=*/0))
10180
            return error_mark_node;
10181
          type = xref_tag (tag_type, identifier, ts, template_p);
10182
        }
10183
    }
10184
  if (tag_type != enum_type)
10185
    cp_parser_check_class_key (tag_type, type);
10186
 
10187
  /* A "<" cannot follow an elaborated type specifier.  If that
10188
     happens, the user was probably trying to form a template-id.  */
10189
  cp_parser_check_for_invalid_template_id (parser, type);
10190
 
10191
  return type;
10192
}
10193
 
10194
/* Parse an enum-specifier.
10195
 
10196
   enum-specifier:
10197
     enum identifier [opt] { enumerator-list [opt] }
10198
 
10199
   GNU Extensions:
10200
     enum identifier [opt] { enumerator-list [opt] } attributes
10201
 
10202
   Returns an ENUM_TYPE representing the enumeration.  */
10203
 
10204
static tree
10205
cp_parser_enum_specifier (cp_parser* parser)
10206
{
10207
  tree identifier;
10208
  tree type;
10209
 
10210
  /* Caller guarantees that the current token is 'enum', an identifier
10211
     possibly follows, and the token after that is an opening brace.
10212
     If we don't have an identifier, fabricate an anonymous name for
10213
     the enumeration being defined.  */
10214
  cp_lexer_consume_token (parser->lexer);
10215
 
10216
  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10217
    identifier = cp_parser_identifier (parser);
10218
  else
10219
    identifier = make_anon_name ();
10220
 
10221
  /* Issue an error message if type-definitions are forbidden here.  */
10222
  cp_parser_check_type_definition (parser);
10223
 
10224
  /* Create the new type.  We do this before consuming the opening brace
10225
     so the enum will be recorded as being on the line of its tag (or the
10226
     'enum' keyword, if there is no tag).  */
10227
  type = start_enum (identifier);
10228
 
10229
  /* Consume the opening brace.  */
10230
  cp_lexer_consume_token (parser->lexer);
10231
 
10232
  /* If the next token is not '}', then there are some enumerators.  */
10233
  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10234
    cp_parser_enumerator_list (parser, type);
10235
 
10236
  /* Consume the final '}'.  */
10237
  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10238
 
10239
  /* Look for trailing attributes to apply to this enumeration, and
10240
     apply them if appropriate.  */
10241
  if (cp_parser_allow_gnu_extensions_p (parser))
10242
    {
10243
      tree trailing_attr = cp_parser_attributes_opt (parser);
10244
      cplus_decl_attributes (&type,
10245
                             trailing_attr,
10246
                             (int) ATTR_FLAG_TYPE_IN_PLACE);
10247
    }
10248
 
10249
  /* Finish up the enumeration.  */
10250
  finish_enum (type);
10251
 
10252
  return type;
10253
}
10254
 
10255
/* Parse an enumerator-list.  The enumerators all have the indicated
10256
   TYPE.
10257
 
10258
   enumerator-list:
10259
     enumerator-definition
10260
     enumerator-list , enumerator-definition  */
10261
 
10262
static void
10263
cp_parser_enumerator_list (cp_parser* parser, tree type)
10264
{
10265
  while (true)
10266
    {
10267
      /* Parse an enumerator-definition.  */
10268
      cp_parser_enumerator_definition (parser, type);
10269
 
10270
      /* If the next token is not a ',', we've reached the end of
10271
         the list.  */
10272
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10273
        break;
10274
      /* Otherwise, consume the `,' and keep going.  */
10275
      cp_lexer_consume_token (parser->lexer);
10276
      /* If the next token is a `}', there is a trailing comma.  */
10277
      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10278
        {
10279
          if (pedantic && !in_system_header)
10280
            pedwarn ("comma at end of enumerator list");
10281
          break;
10282
        }
10283
    }
10284
}
10285
 
10286
/* Parse an enumerator-definition.  The enumerator has the indicated
10287
   TYPE.
10288
 
10289
   enumerator-definition:
10290
     enumerator
10291
     enumerator = constant-expression
10292
 
10293
   enumerator:
10294
     identifier  */
10295
 
10296
static void
10297
cp_parser_enumerator_definition (cp_parser* parser, tree type)
10298
{
10299
  tree identifier;
10300
  tree value;
10301
 
10302
  /* Look for the identifier.  */
10303
  identifier = cp_parser_identifier (parser);
10304
  if (identifier == error_mark_node)
10305
    return;
10306
 
10307
  /* If the next token is an '=', then there is an explicit value.  */
10308
  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10309
    {
10310
      /* Consume the `=' token.  */
10311
      cp_lexer_consume_token (parser->lexer);
10312
      /* Parse the value.  */
10313
      value = cp_parser_constant_expression (parser,
10314
                                             /*allow_non_constant_p=*/false,
10315
                                             NULL);
10316
    }
10317
  else
10318
    value = NULL_TREE;
10319
 
10320
  /* Create the enumerator.  */
10321
  build_enumerator (identifier, value, type);
10322
}
10323
 
10324
/* Parse a namespace-name.
10325
 
10326
   namespace-name:
10327
     original-namespace-name
10328
     namespace-alias
10329
 
10330
   Returns the NAMESPACE_DECL for the namespace.  */
10331
 
10332
static tree
10333
cp_parser_namespace_name (cp_parser* parser)
10334
{
10335
  tree identifier;
10336
  tree namespace_decl;
10337
 
10338
  /* Get the name of the namespace.  */
10339
  identifier = cp_parser_identifier (parser);
10340
  if (identifier == error_mark_node)
10341
    return error_mark_node;
10342
 
10343
  /* Look up the identifier in the currently active scope.  Look only
10344
     for namespaces, due to:
10345
 
10346
       [basic.lookup.udir]
10347
 
10348
       When looking up a namespace-name in a using-directive or alias
10349
       definition, only namespace names are considered.
10350
 
10351
     And:
10352
 
10353
       [basic.lookup.qual]
10354
 
10355
       During the lookup of a name preceding the :: scope resolution
10356
       operator, object, function, and enumerator names are ignored.
10357
 
10358
     (Note that cp_parser_class_or_namespace_name only calls this
10359
     function if the token after the name is the scope resolution
10360
     operator.)  */
10361
  namespace_decl = cp_parser_lookup_name (parser, identifier,
10362
                                          none_type,
10363
                                          /*is_template=*/false,
10364
                                          /*is_namespace=*/true,
10365
                                          /*check_dependency=*/true,
10366
                                          /*ambiguous_decls=*/NULL);
10367
  /* If it's not a namespace, issue an error.  */
10368
  if (namespace_decl == error_mark_node
10369
      || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10370
    {
10371
      if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10372
        error ("%qD is not a namespace-name", identifier);
10373
      cp_parser_error (parser, "expected namespace-name");
10374
      namespace_decl = error_mark_node;
10375
    }
10376
 
10377
  return namespace_decl;
10378
}
10379
 
10380
/* Parse a namespace-definition.
10381
 
10382
   namespace-definition:
10383
     named-namespace-definition
10384
     unnamed-namespace-definition
10385
 
10386
   named-namespace-definition:
10387
     original-namespace-definition
10388
     extension-namespace-definition
10389
 
10390
   original-namespace-definition:
10391
     namespace identifier { namespace-body }
10392
 
10393
   extension-namespace-definition:
10394
     namespace original-namespace-name { namespace-body }
10395
 
10396
   unnamed-namespace-definition:
10397
     namespace { namespace-body } */
10398
 
10399
static void
10400
cp_parser_namespace_definition (cp_parser* parser)
10401
{
10402
  tree identifier;
10403
 
10404
  /* Look for the `namespace' keyword.  */
10405
  cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10406
 
10407
  /* Get the name of the namespace.  We do not attempt to distinguish
10408
     between an original-namespace-definition and an
10409
     extension-namespace-definition at this point.  The semantic
10410
     analysis routines are responsible for that.  */
10411
  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10412
    identifier = cp_parser_identifier (parser);
10413
  else
10414
    identifier = NULL_TREE;
10415
 
10416
  /* Look for the `{' to start the namespace.  */
10417
  cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10418
  /* Start the namespace.  */
10419
  push_namespace (identifier);
10420
  /* Parse the body of the namespace.  */
10421
  cp_parser_namespace_body (parser);
10422
  /* Finish the namespace.  */
10423
  pop_namespace ();
10424
  /* Look for the final `}'.  */
10425
  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10426
}
10427
 
10428
/* Parse a namespace-body.
10429
 
10430
   namespace-body:
10431
     declaration-seq [opt]  */
10432
 
10433
static void
10434
cp_parser_namespace_body (cp_parser* parser)
10435
{
10436
  cp_parser_declaration_seq_opt (parser);
10437
}
10438
 
10439
/* Parse a namespace-alias-definition.
10440
 
10441
   namespace-alias-definition:
10442
     namespace identifier = qualified-namespace-specifier ;  */
10443
 
10444
static void
10445
cp_parser_namespace_alias_definition (cp_parser* parser)
10446
{
10447
  tree identifier;
10448
  tree namespace_specifier;
10449
 
10450
  /* Look for the `namespace' keyword.  */
10451
  cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10452
  /* Look for the identifier.  */
10453
  identifier = cp_parser_identifier (parser);
10454
  if (identifier == error_mark_node)
10455
    return;
10456
  /* Look for the `=' token.  */
10457
  cp_parser_require (parser, CPP_EQ, "`='");
10458
  /* Look for the qualified-namespace-specifier.  */
10459
  namespace_specifier
10460
    = cp_parser_qualified_namespace_specifier (parser);
10461
  /* Look for the `;' token.  */
10462
  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10463
 
10464
  /* Register the alias in the symbol table.  */
10465
  do_namespace_alias (identifier, namespace_specifier);
10466
}
10467
 
10468
/* Parse a qualified-namespace-specifier.
10469
 
10470
   qualified-namespace-specifier:
10471
     :: [opt] nested-name-specifier [opt] namespace-name
10472
 
10473
   Returns a NAMESPACE_DECL corresponding to the specified
10474
   namespace.  */
10475
 
10476
static tree
10477
cp_parser_qualified_namespace_specifier (cp_parser* parser)
10478
{
10479
  /* Look for the optional `::'.  */
10480
  cp_parser_global_scope_opt (parser,
10481
                              /*current_scope_valid_p=*/false);
10482
 
10483
  /* Look for the optional nested-name-specifier.  */
10484
  cp_parser_nested_name_specifier_opt (parser,
10485
                                       /*typename_keyword_p=*/false,
10486
                                       /*check_dependency_p=*/true,
10487
                                       /*type_p=*/false,
10488
                                       /*is_declaration=*/true);
10489
 
10490
  return cp_parser_namespace_name (parser);
10491
}
10492
 
10493
/* Parse a using-declaration.
10494
 
10495
   using-declaration:
10496
     using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10497
     using :: unqualified-id ;  */
10498
 
10499
static void
10500
cp_parser_using_declaration (cp_parser* parser)
10501
{
10502
  cp_token *token;
10503
  bool typename_p = false;
10504
  bool global_scope_p;
10505
  tree decl;
10506
  tree identifier;
10507
  tree qscope;
10508
 
10509
  /* Look for the `using' keyword.  */
10510
  cp_parser_require_keyword (parser, RID_USING, "`using'");
10511
 
10512
  /* Peek at the next token.  */
10513
  token = cp_lexer_peek_token (parser->lexer);
10514
  /* See if it's `typename'.  */
10515
  if (token->keyword == RID_TYPENAME)
10516
    {
10517
      /* Remember that we've seen it.  */
10518
      typename_p = true;
10519
      /* Consume the `typename' token.  */
10520
      cp_lexer_consume_token (parser->lexer);
10521
    }
10522
 
10523
  /* Look for the optional global scope qualification.  */
10524
  global_scope_p
10525
    = (cp_parser_global_scope_opt (parser,
10526
                                   /*current_scope_valid_p=*/false)
10527
       != NULL_TREE);
10528
 
10529
  /* If we saw `typename', or didn't see `::', then there must be a
10530
     nested-name-specifier present.  */
10531
  if (typename_p || !global_scope_p)
10532
    qscope = cp_parser_nested_name_specifier (parser, typename_p,
10533
                                              /*check_dependency_p=*/true,
10534
                                              /*type_p=*/false,
10535
                                              /*is_declaration=*/true);
10536
  /* Otherwise, we could be in either of the two productions.  In that
10537
     case, treat the nested-name-specifier as optional.  */
10538
  else
10539
    qscope = cp_parser_nested_name_specifier_opt (parser,
10540
                                                  /*typename_keyword_p=*/false,
10541
                                                  /*check_dependency_p=*/true,
10542
                                                  /*type_p=*/false,
10543
                                                  /*is_declaration=*/true);
10544
  if (!qscope)
10545
    qscope = global_namespace;
10546
 
10547
  /* Parse the unqualified-id.  */
10548
  identifier = cp_parser_unqualified_id (parser,
10549
                                         /*template_keyword_p=*/false,
10550
                                         /*check_dependency_p=*/true,
10551
                                         /*declarator_p=*/true);
10552
 
10553
  /* The function we call to handle a using-declaration is different
10554
     depending on what scope we are in.  */
10555
  if (qscope == error_mark_node || identifier == error_mark_node)
10556
    ;
10557
  else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10558
           && TREE_CODE (identifier) != BIT_NOT_EXPR)
10559
    /* [namespace.udecl]
10560
 
10561
       A using declaration shall not name a template-id.  */
10562
    error ("a template-id may not appear in a using-declaration");
10563
  else
10564
    {
10565
      if (at_class_scope_p ())
10566
        {
10567
          /* Create the USING_DECL.  */
10568
          decl = do_class_using_decl (parser->scope, identifier);
10569
          /* Add it to the list of members in this class.  */
10570
          finish_member_declaration (decl);
10571
        }
10572
      else
10573
        {
10574
          decl = cp_parser_lookup_name_simple (parser, identifier);
10575
          if (decl == error_mark_node)
10576
            cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10577
          else if (!at_namespace_scope_p ())
10578
            do_local_using_decl (decl, qscope, identifier);
10579
          else
10580
            do_toplevel_using_decl (decl, qscope, identifier);
10581
        }
10582
    }
10583
 
10584
  /* Look for the final `;'.  */
10585
  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10586
}
10587
 
10588
/* Parse a using-directive.
10589
 
10590
   using-directive:
10591
     using namespace :: [opt] nested-name-specifier [opt]
10592
       namespace-name ;  */
10593
 
10594
static void
10595
cp_parser_using_directive (cp_parser* parser)
10596
{
10597
  tree namespace_decl;
10598
  tree attribs;
10599
 
10600
  /* Look for the `using' keyword.  */
10601
  cp_parser_require_keyword (parser, RID_USING, "`using'");
10602
  /* And the `namespace' keyword.  */
10603
  cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10604
  /* Look for the optional `::' operator.  */
10605
  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10606
  /* And the optional nested-name-specifier.  */
10607
  cp_parser_nested_name_specifier_opt (parser,
10608
                                       /*typename_keyword_p=*/false,
10609
                                       /*check_dependency_p=*/true,
10610
                                       /*type_p=*/false,
10611
                                       /*is_declaration=*/true);
10612
  /* Get the namespace being used.  */
10613
  namespace_decl = cp_parser_namespace_name (parser);
10614
  /* And any specified attributes.  */
10615
  attribs = cp_parser_attributes_opt (parser);
10616
  /* Update the symbol table.  */
10617
  parse_using_directive (namespace_decl, attribs);
10618
  /* Look for the final `;'.  */
10619
  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10620
}
10621
 
10622
/* Parse an asm-definition.
10623
 
10624
   asm-definition:
10625
     asm ( string-literal ) ;
10626
 
10627
   GNU Extension:
10628
 
10629
   asm-definition:
10630
     asm volatile [opt] ( string-literal ) ;
10631
     asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10632
     asm volatile [opt] ( string-literal : asm-operand-list [opt]
10633
                          : asm-operand-list [opt] ) ;
10634
     asm volatile [opt] ( string-literal : asm-operand-list [opt]
10635
                          : asm-operand-list [opt]
10636
                          : asm-operand-list [opt] ) ;  */
10637
 
10638
static void
10639
cp_parser_asm_definition (cp_parser* parser)
10640
{
10641
  tree string;
10642
  tree outputs = NULL_TREE;
10643
  tree inputs = NULL_TREE;
10644
  tree clobbers = NULL_TREE;
10645
  tree asm_stmt;
10646
  bool volatile_p = false;
10647
  bool extended_p = false;
10648
 
10649
  /* Look for the `asm' keyword.  */
10650
  cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10651
  /* See if the next token is `volatile'.  */
10652
  if (cp_parser_allow_gnu_extensions_p (parser)
10653
      && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10654
    {
10655
      /* Remember that we saw the `volatile' keyword.  */
10656
      volatile_p = true;
10657
      /* Consume the token.  */
10658
      cp_lexer_consume_token (parser->lexer);
10659
    }
10660
  /* Look for the opening `('.  */
10661
  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10662
    return;
10663
  /* Look for the string.  */
10664
  string = cp_parser_string_literal (parser, false, false);
10665
  if (string == error_mark_node)
10666
    {
10667
      cp_parser_skip_to_closing_parenthesis (parser, true, false,
10668
                                             /*consume_paren=*/true);
10669
      return;
10670
    }
10671
 
10672
  /* If we're allowing GNU extensions, check for the extended assembly
10673
     syntax.  Unfortunately, the `:' tokens need not be separated by
10674
     a space in C, and so, for compatibility, we tolerate that here
10675
     too.  Doing that means that we have to treat the `::' operator as
10676
     two `:' tokens.  */
10677
  if (cp_parser_allow_gnu_extensions_p (parser)
10678
      && at_function_scope_p ()
10679
      && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10680
          || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10681
    {
10682
      bool inputs_p = false;
10683
      bool clobbers_p = false;
10684
 
10685
      /* The extended syntax was used.  */
10686
      extended_p = true;
10687
 
10688
      /* Look for outputs.  */
10689
      if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10690
        {
10691
          /* Consume the `:'.  */
10692
          cp_lexer_consume_token (parser->lexer);
10693
          /* Parse the output-operands.  */
10694
          if (cp_lexer_next_token_is_not (parser->lexer,
10695
                                          CPP_COLON)
10696
              && cp_lexer_next_token_is_not (parser->lexer,
10697
                                             CPP_SCOPE)
10698
              && cp_lexer_next_token_is_not (parser->lexer,
10699
                                             CPP_CLOSE_PAREN))
10700
            outputs = cp_parser_asm_operand_list (parser);
10701
        }
10702
      /* If the next token is `::', there are no outputs, and the
10703
         next token is the beginning of the inputs.  */
10704
      else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10705
        /* The inputs are coming next.  */
10706
        inputs_p = true;
10707
 
10708
      /* Look for inputs.  */
10709
      if (inputs_p
10710
          || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10711
        {
10712
          /* Consume the `:' or `::'.  */
10713
          cp_lexer_consume_token (parser->lexer);
10714
          /* Parse the output-operands.  */
10715
          if (cp_lexer_next_token_is_not (parser->lexer,
10716
                                          CPP_COLON)
10717
              && cp_lexer_next_token_is_not (parser->lexer,
10718
                                             CPP_CLOSE_PAREN))
10719
            inputs = cp_parser_asm_operand_list (parser);
10720
        }
10721
      else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10722
        /* The clobbers are coming next.  */
10723
        clobbers_p = true;
10724
 
10725
      /* Look for clobbers.  */
10726
      if (clobbers_p
10727
          || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10728
        {
10729
          /* Consume the `:' or `::'.  */
10730
          cp_lexer_consume_token (parser->lexer);
10731
          /* Parse the clobbers.  */
10732
          if (cp_lexer_next_token_is_not (parser->lexer,
10733
                                          CPP_CLOSE_PAREN))
10734
            clobbers = cp_parser_asm_clobber_list (parser);
10735
        }
10736
    }
10737
  /* Look for the closing `)'.  */
10738
  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10739
    cp_parser_skip_to_closing_parenthesis (parser, true, false,
10740
                                           /*consume_paren=*/true);
10741
  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10742
 
10743
  /* Create the ASM_EXPR.  */
10744
  if (at_function_scope_p ())
10745
    {
10746
      asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10747
                                  inputs, clobbers);
10748
      /* If the extended syntax was not used, mark the ASM_EXPR.  */
10749
      if (!extended_p)
10750
        {
10751
          tree temp = asm_stmt;
10752
          if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10753
            temp = TREE_OPERAND (temp, 0);
10754
 
10755
          ASM_INPUT_P (temp) = 1;
10756
        }
10757
    }
10758
  else
10759
    assemble_asm (string);
10760
}
10761
 
10762
/* Declarators [gram.dcl.decl] */
10763
 
10764
/* Parse an init-declarator.
10765
 
10766
   init-declarator:
10767
     declarator initializer [opt]
10768
 
10769
   GNU Extension:
10770
 
10771
   init-declarator:
10772
     declarator asm-specification [opt] attributes [opt] initializer [opt]
10773
 
10774
   function-definition:
10775
     decl-specifier-seq [opt] declarator ctor-initializer [opt]
10776
       function-body
10777
     decl-specifier-seq [opt] declarator function-try-block
10778
 
10779
   GNU Extension:
10780
 
10781
   function-definition:
10782
     __extension__ function-definition
10783
 
10784
   The DECL_SPECIFIERS apply to this declarator.  Returns a
10785
   representation of the entity declared.  If MEMBER_P is TRUE, then
10786
   this declarator appears in a class scope.  The new DECL created by
10787
   this declarator is returned.
10788
 
10789
   The CHECKS are access checks that should be performed once we know
10790
   what entity is being declared (and, therefore, what classes have
10791
   befriended it).
10792
 
10793
   If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10794
   for a function-definition here as well.  If the declarator is a
10795
   declarator for a function-definition, *FUNCTION_DEFINITION_P will
10796
   be TRUE upon return.  By that point, the function-definition will
10797
   have been completely parsed.
10798
 
10799
   FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10800
   is FALSE.  */
10801
 
10802
static tree
10803
cp_parser_init_declarator (cp_parser* parser,
10804
                           cp_decl_specifier_seq *decl_specifiers,
10805
                           tree checks,
10806
                           bool function_definition_allowed_p,
10807
                           bool member_p,
10808
                           int declares_class_or_enum,
10809
                           bool* function_definition_p)
10810
{
10811
  cp_token *token;
10812
  cp_declarator *declarator;
10813
  tree prefix_attributes;
10814
  tree attributes;
10815
  tree asm_specification;
10816
  tree initializer;
10817
  tree decl = NULL_TREE;
10818
  tree scope;
10819
  bool is_initialized;
10820
  /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
10821
     initialized with "= ..", CPP_OPEN_PAREN if initialized with
10822
     "(...)".  */
10823
  enum cpp_ttype initialization_kind;
10824
  bool is_parenthesized_init;
10825
  bool is_non_constant_init;
10826
  int ctor_dtor_or_conv_p;
10827
  bool friend_p;
10828
  tree pushed_scope = NULL;
10829
 
10830
  /* Gather the attributes that were provided with the
10831
     decl-specifiers.  */
10832
  prefix_attributes = decl_specifiers->attributes;
10833
 
10834
  /* Assume that this is not the declarator for a function
10835
     definition.  */
10836
  if (function_definition_p)
10837
    *function_definition_p = false;
10838
 
10839
  /* Defer access checks while parsing the declarator; we cannot know
10840
     what names are accessible until we know what is being
10841
     declared.  */
10842
  resume_deferring_access_checks ();
10843
 
10844
  /* Parse the declarator.  */
10845
  declarator
10846
    = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10847
                            &ctor_dtor_or_conv_p,
10848
                            /*parenthesized_p=*/NULL,
10849
                            /*member_p=*/false);
10850
  /* Gather up the deferred checks.  */
10851
  stop_deferring_access_checks ();
10852
 
10853
  /* If the DECLARATOR was erroneous, there's no need to go
10854
     further.  */
10855
  if (declarator == cp_error_declarator)
10856
    return error_mark_node;
10857
 
10858
  if (declares_class_or_enum & 2)
10859
    cp_parser_check_for_definition_in_return_type (declarator,
10860
                                                   decl_specifiers->type);
10861
 
10862
  /* Figure out what scope the entity declared by the DECLARATOR is
10863
     located in.  `grokdeclarator' sometimes changes the scope, so
10864
     we compute it now.  */
10865
  scope = get_scope_of_declarator (declarator);
10866
 
10867
  /* If we're allowing GNU extensions, look for an asm-specification
10868
     and attributes.  */
10869
  if (cp_parser_allow_gnu_extensions_p (parser))
10870
    {
10871
      /* Look for an asm-specification.  */
10872
      asm_specification = cp_parser_asm_specification_opt (parser);
10873
      /* And attributes.  */
10874
      attributes = cp_parser_attributes_opt (parser);
10875
    }
10876
  else
10877
    {
10878
      asm_specification = NULL_TREE;
10879
      attributes = NULL_TREE;
10880
    }
10881
 
10882
  /* Peek at the next token.  */
10883
  token = cp_lexer_peek_token (parser->lexer);
10884
  /* Check to see if the token indicates the start of a
10885
     function-definition.  */
10886
  if (cp_parser_token_starts_function_definition_p (token))
10887
    {
10888
      if (!function_definition_allowed_p)
10889
        {
10890
          /* If a function-definition should not appear here, issue an
10891
             error message.  */
10892
          cp_parser_error (parser,
10893
                           "a function-definition is not allowed here");
10894
          return error_mark_node;
10895
        }
10896
      else
10897
        {
10898
          /* Neither attributes nor an asm-specification are allowed
10899
             on a function-definition.  */
10900
          if (asm_specification)
10901
            error ("an asm-specification is not allowed on a function-definition");
10902
          if (attributes)
10903
            error ("attributes are not allowed on a function-definition");
10904
          /* This is a function-definition.  */
10905
          *function_definition_p = true;
10906
 
10907
          /* Parse the function definition.  */
10908
          if (member_p)
10909
            decl = cp_parser_save_member_function_body (parser,
10910
                                                        decl_specifiers,
10911
                                                        declarator,
10912
                                                        prefix_attributes);
10913
          else
10914
            decl
10915
              = (cp_parser_function_definition_from_specifiers_and_declarator
10916
                 (parser, decl_specifiers, prefix_attributes, declarator));
10917
 
10918
          return decl;
10919
        }
10920
    }
10921
 
10922
  /* [dcl.dcl]
10923
 
10924
     Only in function declarations for constructors, destructors, and
10925
     type conversions can the decl-specifier-seq be omitted.
10926
 
10927
     We explicitly postpone this check past the point where we handle
10928
     function-definitions because we tolerate function-definitions
10929
     that are missing their return types in some modes.  */
10930
  if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10931
    {
10932
      cp_parser_error (parser,
10933
                       "expected constructor, destructor, or type conversion");
10934
      return error_mark_node;
10935
    }
10936
 
10937
  /* An `=' or an `(' indicates an initializer.  */
10938
  if (token->type == CPP_EQ
10939
      || token->type == CPP_OPEN_PAREN)
10940
    {
10941
      is_initialized = true;
10942
      initialization_kind = token->type;
10943
    }
10944
  else
10945
    {
10946
      /* If the init-declarator isn't initialized and isn't followed by a
10947
         `,' or `;', it's not a valid init-declarator.  */
10948
      if (token->type != CPP_COMMA
10949
          && token->type != CPP_SEMICOLON)
10950
        {
10951
          cp_parser_error (parser, "expected initializer");
10952
          return error_mark_node;
10953
        }
10954
      is_initialized = false;
10955
      initialization_kind = CPP_EOF;
10956
    }
10957
 
10958
  /* Because start_decl has side-effects, we should only call it if we
10959
     know we're going ahead.  By this point, we know that we cannot
10960
     possibly be looking at any other construct.  */
10961
  cp_parser_commit_to_tentative_parse (parser);
10962
 
10963
  /* If the decl specifiers were bad, issue an error now that we're
10964
     sure this was intended to be a declarator.  Then continue
10965
     declaring the variable(s), as int, to try to cut down on further
10966
     errors.  */
10967
  if (decl_specifiers->any_specifiers_p
10968
      && decl_specifiers->type == error_mark_node)
10969
    {
10970
      cp_parser_error (parser, "invalid type in declaration");
10971
      decl_specifiers->type = integer_type_node;
10972
    }
10973
 
10974
  /* Check to see whether or not this declaration is a friend.  */
10975
  friend_p = cp_parser_friend_p (decl_specifiers);
10976
 
10977
  /* Check that the number of template-parameter-lists is OK.  */
10978
  if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10979
    return error_mark_node;
10980
 
10981
  /* Enter the newly declared entry in the symbol table.  If we're
10982
     processing a declaration in a class-specifier, we wait until
10983
     after processing the initializer.  */
10984
  if (!member_p)
10985
    {
10986
      if (parser->in_unbraced_linkage_specification_p)
10987
        {
10988
          decl_specifiers->storage_class = sc_extern;
10989
          have_extern_spec = false;
10990
        }
10991
      decl = start_decl (declarator, decl_specifiers,
10992
                         is_initialized, attributes, prefix_attributes,
10993
                         &pushed_scope);
10994
    }
10995
  else if (scope)
10996
    /* Enter the SCOPE.  That way unqualified names appearing in the
10997
       initializer will be looked up in SCOPE.  */
10998
    pushed_scope = push_scope (scope);
10999
 
11000
  /* Perform deferred access control checks, now that we know in which
11001
     SCOPE the declared entity resides.  */
11002
  if (!member_p && decl)
11003
    {
11004
      tree saved_current_function_decl = NULL_TREE;
11005
 
11006
      /* If the entity being declared is a function, pretend that we
11007
         are in its scope.  If it is a `friend', it may have access to
11008
         things that would not otherwise be accessible.  */
11009
      if (TREE_CODE (decl) == FUNCTION_DECL)
11010
        {
11011
          saved_current_function_decl = current_function_decl;
11012
          current_function_decl = decl;
11013
        }
11014
 
11015
      /* Perform access checks for template parameters.  */
11016
      cp_parser_perform_template_parameter_access_checks (checks);
11017
 
11018
      /* Perform the access control checks for the declarator and the
11019
         the decl-specifiers.  */
11020
      perform_deferred_access_checks ();
11021
 
11022
      /* Restore the saved value.  */
11023
      if (TREE_CODE (decl) == FUNCTION_DECL)
11024
        current_function_decl = saved_current_function_decl;
11025
    }
11026
 
11027
  /* Parse the initializer.  */
11028
  initializer = NULL_TREE;
11029
  is_parenthesized_init = false;
11030
  is_non_constant_init = true;
11031
  if (is_initialized)
11032
    {
11033
      if (declarator->kind == cdk_function
11034
          && declarator->declarator->kind == cdk_id
11035
          && initialization_kind == CPP_EQ)
11036
        initializer = cp_parser_pure_specifier (parser);
11037
      else
11038
        initializer = cp_parser_initializer (parser,
11039
                                             &is_parenthesized_init,
11040
                                             &is_non_constant_init);
11041
    }
11042
 
11043
  /* The old parser allows attributes to appear after a parenthesized
11044
     initializer.  Mark Mitchell proposed removing this functionality
11045
     on the GCC mailing lists on 2002-08-13.  This parser accepts the
11046
     attributes -- but ignores them.  */
11047
  if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11048
    if (cp_parser_attributes_opt (parser))
11049
      warning (OPT_Wattributes,
11050
               "attributes after parenthesized initializer ignored");
11051
 
11052
  /* For an in-class declaration, use `grokfield' to create the
11053
     declaration.  */
11054
  if (member_p)
11055
    {
11056
      if (pushed_scope)
11057
        {
11058
          pop_scope (pushed_scope);
11059
          pushed_scope = false;
11060
        }
11061
      decl = grokfield (declarator, decl_specifiers,
11062
                        initializer, !is_non_constant_init,
11063
                        /*asmspec=*/NULL_TREE,
11064
                        prefix_attributes);
11065
      if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11066
        cp_parser_save_default_args (parser, decl);
11067
    }
11068
 
11069
  /* Finish processing the declaration.  But, skip friend
11070
     declarations.  */
11071
  if (!friend_p && decl && decl != error_mark_node)
11072
    {
11073
      cp_finish_decl (decl,
11074
                      initializer, !is_non_constant_init,
11075
                      asm_specification,
11076
                      /* If the initializer is in parentheses, then this is
11077
                         a direct-initialization, which means that an
11078
                         `explicit' constructor is OK.  Otherwise, an
11079
                         `explicit' constructor cannot be used.  */
11080
                      ((is_parenthesized_init || !is_initialized)
11081
                     ? 0 : LOOKUP_ONLYCONVERTING));
11082
    }
11083
  if (!friend_p && pushed_scope)
11084
    pop_scope (pushed_scope);
11085
 
11086
  return decl;
11087
}
11088
 
11089
/* Parse a declarator.
11090
 
11091
   declarator:
11092
     direct-declarator
11093
     ptr-operator declarator
11094
 
11095
   abstract-declarator:
11096
     ptr-operator abstract-declarator [opt]
11097
     direct-abstract-declarator
11098
 
11099
   GNU Extensions:
11100
 
11101
   declarator:
11102
     attributes [opt] direct-declarator
11103
     attributes [opt] ptr-operator declarator
11104
 
11105
   abstract-declarator:
11106
     attributes [opt] ptr-operator abstract-declarator [opt]
11107
     attributes [opt] direct-abstract-declarator
11108
 
11109
   If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11110
   detect constructor, destructor or conversion operators. It is set
11111
   to -1 if the declarator is a name, and +1 if it is a
11112
   function. Otherwise it is set to zero. Usually you just want to
11113
   test for >0, but internally the negative value is used.
11114
 
11115
   (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11116
   a decl-specifier-seq unless it declares a constructor, destructor,
11117
   or conversion.  It might seem that we could check this condition in
11118
   semantic analysis, rather than parsing, but that makes it difficult
11119
   to handle something like `f()'.  We want to notice that there are
11120
   no decl-specifiers, and therefore realize that this is an
11121
   expression, not a declaration.)
11122
 
11123
   If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11124
   the declarator is a direct-declarator of the form "(...)".
11125
 
11126
   MEMBER_P is true iff this declarator is a member-declarator.  */
11127
 
11128
static cp_declarator *
11129
cp_parser_declarator (cp_parser* parser,
11130
                      cp_parser_declarator_kind dcl_kind,
11131
                      int* ctor_dtor_or_conv_p,
11132
                      bool* parenthesized_p,
11133
                      bool member_p)
11134
{
11135
  cp_token *token;
11136
  cp_declarator *declarator;
11137
  enum tree_code code;
11138
  cp_cv_quals cv_quals;
11139
  tree class_type;
11140
  tree attributes = NULL_TREE;
11141
 
11142
  /* Assume this is not a constructor, destructor, or type-conversion
11143
     operator.  */
11144
  if (ctor_dtor_or_conv_p)
11145
    *ctor_dtor_or_conv_p = 0;
11146
 
11147
  if (cp_parser_allow_gnu_extensions_p (parser))
11148
    attributes = cp_parser_attributes_opt (parser);
11149
 
11150
  /* Peek at the next token.  */
11151
  token = cp_lexer_peek_token (parser->lexer);
11152
 
11153
  /* Check for the ptr-operator production.  */
11154
  cp_parser_parse_tentatively (parser);
11155
  /* Parse the ptr-operator.  */
11156
  code = cp_parser_ptr_operator (parser,
11157
                                 &class_type,
11158
                                 &cv_quals);
11159
  /* If that worked, then we have a ptr-operator.  */
11160
  if (cp_parser_parse_definitely (parser))
11161
    {
11162
      /* If a ptr-operator was found, then this declarator was not
11163
         parenthesized.  */
11164
      if (parenthesized_p)
11165
        *parenthesized_p = true;
11166
      /* The dependent declarator is optional if we are parsing an
11167
         abstract-declarator.  */
11168
      if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11169
        cp_parser_parse_tentatively (parser);
11170
 
11171
      /* Parse the dependent declarator.  */
11172
      declarator = cp_parser_declarator (parser, dcl_kind,
11173
                                         /*ctor_dtor_or_conv_p=*/NULL,
11174
                                         /*parenthesized_p=*/NULL,
11175
                                         /*member_p=*/false);
11176
 
11177
      /* If we are parsing an abstract-declarator, we must handle the
11178
         case where the dependent declarator is absent.  */
11179
      if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11180
          && !cp_parser_parse_definitely (parser))
11181
        declarator = NULL;
11182
 
11183
      /* Build the representation of the ptr-operator.  */
11184
      if (class_type)
11185
        declarator = make_ptrmem_declarator (cv_quals,
11186
                                             class_type,
11187
                                             declarator);
11188
      else if (code == INDIRECT_REF)
11189
        declarator = make_pointer_declarator (cv_quals, declarator);
11190
      else
11191
        declarator = make_reference_declarator (cv_quals, declarator);
11192
    }
11193
  /* Everything else is a direct-declarator.  */
11194
  else
11195
    {
11196
      if (parenthesized_p)
11197
        *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11198
                                                   CPP_OPEN_PAREN);
11199
      declarator = cp_parser_direct_declarator (parser, dcl_kind,
11200
                                                ctor_dtor_or_conv_p,
11201
                                                member_p);
11202
    }
11203
 
11204
  if (attributes && declarator != cp_error_declarator)
11205
    declarator->attributes = attributes;
11206
 
11207
  return declarator;
11208
}
11209
 
11210
/* Parse a direct-declarator or direct-abstract-declarator.
11211
 
11212
   direct-declarator:
11213
     declarator-id
11214
     direct-declarator ( parameter-declaration-clause )
11215
       cv-qualifier-seq [opt]
11216
       exception-specification [opt]
11217
     direct-declarator [ constant-expression [opt] ]
11218
     ( declarator )
11219
 
11220
   direct-abstract-declarator:
11221
     direct-abstract-declarator [opt]
11222
       ( parameter-declaration-clause )
11223
       cv-qualifier-seq [opt]
11224
       exception-specification [opt]
11225
     direct-abstract-declarator [opt] [ constant-expression [opt] ]
11226
     ( abstract-declarator )
11227
 
11228
   Returns a representation of the declarator.  DCL_KIND is
11229
   CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11230
   direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11231
   we are parsing a direct-declarator.  It is
11232
   CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11233
   of ambiguity we prefer an abstract declarator, as per
11234
   [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11235
   cp_parser_declarator.  */
11236
 
11237
static cp_declarator *
11238
cp_parser_direct_declarator (cp_parser* parser,
11239
                             cp_parser_declarator_kind dcl_kind,
11240
                             int* ctor_dtor_or_conv_p,
11241
                             bool member_p)
11242
{
11243
  cp_token *token;
11244
  cp_declarator *declarator = NULL;
11245
  tree scope = NULL_TREE;
11246
  bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11247
  bool saved_in_declarator_p = parser->in_declarator_p;
11248
  bool first = true;
11249
  tree pushed_scope = NULL_TREE;
11250
 
11251
  while (true)
11252
    {
11253
      /* Peek at the next token.  */
11254
      token = cp_lexer_peek_token (parser->lexer);
11255
      if (token->type == CPP_OPEN_PAREN)
11256
        {
11257
          /* This is either a parameter-declaration-clause, or a
11258
             parenthesized declarator. When we know we are parsing a
11259
             named declarator, it must be a parenthesized declarator
11260
             if FIRST is true. For instance, `(int)' is a
11261
             parameter-declaration-clause, with an omitted
11262
             direct-abstract-declarator. But `((*))', is a
11263
             parenthesized abstract declarator. Finally, when T is a
11264
             template parameter `(T)' is a
11265
             parameter-declaration-clause, and not a parenthesized
11266
             named declarator.
11267
 
11268
             We first try and parse a parameter-declaration-clause,
11269
             and then try a nested declarator (if FIRST is true).
11270
 
11271
             It is not an error for it not to be a
11272
             parameter-declaration-clause, even when FIRST is
11273
             false. Consider,
11274
 
11275
               int i (int);
11276
               int i (3);
11277
 
11278
             The first is the declaration of a function while the
11279
             second is a the definition of a variable, including its
11280
             initializer.
11281
 
11282
             Having seen only the parenthesis, we cannot know which of
11283
             these two alternatives should be selected.  Even more
11284
             complex are examples like:
11285
 
11286
               int i (int (a));
11287
               int i (int (3));
11288
 
11289
             The former is a function-declaration; the latter is a
11290
             variable initialization.
11291
 
11292
             Thus again, we try a parameter-declaration-clause, and if
11293
             that fails, we back out and return.  */
11294
 
11295
          if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11296
            {
11297
              cp_parameter_declarator *params;
11298
              unsigned saved_num_template_parameter_lists;
11299
 
11300
              /* In a member-declarator, the only valid interpretation
11301
                 of a parenthesis is the start of a
11302
                 parameter-declaration-clause.  (It is invalid to
11303
                 initialize a static data member with a parenthesized
11304
                 initializer; only the "=" form of initialization is
11305
                 permitted.)  */
11306
              if (!member_p)
11307
                cp_parser_parse_tentatively (parser);
11308
 
11309
              /* Consume the `('.  */
11310
              cp_lexer_consume_token (parser->lexer);
11311
              if (first)
11312
                {
11313
                  /* If this is going to be an abstract declarator, we're
11314
                     in a declarator and we can't have default args.  */
11315
                  parser->default_arg_ok_p = false;
11316
                  parser->in_declarator_p = true;
11317
                }
11318
 
11319
              /* Inside the function parameter list, surrounding
11320
                 template-parameter-lists do not apply.  */
11321
              saved_num_template_parameter_lists
11322
                = parser->num_template_parameter_lists;
11323
              parser->num_template_parameter_lists = 0;
11324
 
11325
              /* Parse the parameter-declaration-clause.  */
11326
              params = cp_parser_parameter_declaration_clause (parser);
11327
 
11328
              parser->num_template_parameter_lists
11329
                = saved_num_template_parameter_lists;
11330
 
11331
              /* If all went well, parse the cv-qualifier-seq and the
11332
                 exception-specification.  */
11333
              if (member_p || cp_parser_parse_definitely (parser))
11334
                {
11335
                  cp_cv_quals cv_quals;
11336
                  tree exception_specification;
11337
 
11338
                  if (ctor_dtor_or_conv_p)
11339
                    *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11340
                  first = false;
11341
                  /* Consume the `)'.  */
11342
                  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11343
 
11344
                  /* Parse the cv-qualifier-seq.  */
11345
                  cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11346
                  /* And the exception-specification.  */
11347
                  exception_specification
11348
                    = cp_parser_exception_specification_opt (parser);
11349
 
11350
                  /* Create the function-declarator.  */
11351
                  declarator = make_call_declarator (declarator,
11352
                                                     params,
11353
                                                     cv_quals,
11354
                                                     exception_specification);
11355
                  /* Any subsequent parameter lists are to do with
11356
                     return type, so are not those of the declared
11357
                     function.  */
11358
                  parser->default_arg_ok_p = false;
11359
 
11360
                  /* Repeat the main loop.  */
11361
                  continue;
11362
                }
11363
            }
11364
 
11365
          /* If this is the first, we can try a parenthesized
11366
             declarator.  */
11367
          if (first)
11368
            {
11369
              bool saved_in_type_id_in_expr_p;
11370
 
11371
              parser->default_arg_ok_p = saved_default_arg_ok_p;
11372
              parser->in_declarator_p = saved_in_declarator_p;
11373
 
11374
              /* Consume the `('.  */
11375
              cp_lexer_consume_token (parser->lexer);
11376
              /* Parse the nested declarator.  */
11377
              saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11378
              parser->in_type_id_in_expr_p = true;
11379
              declarator
11380
                = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11381
                                        /*parenthesized_p=*/NULL,
11382
                                        member_p);
11383
              parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11384
              first = false;
11385
              /* Expect a `)'.  */
11386
              if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11387
                declarator = cp_error_declarator;
11388
              if (declarator == cp_error_declarator)
11389
                break;
11390
 
11391
              goto handle_declarator;
11392
            }
11393
          /* Otherwise, we must be done.  */
11394
          else
11395
            break;
11396
        }
11397
      else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11398
               && token->type == CPP_OPEN_SQUARE)
11399
        {
11400
          /* Parse an array-declarator.  */
11401
          tree bounds;
11402
 
11403
          if (ctor_dtor_or_conv_p)
11404
            *ctor_dtor_or_conv_p = 0;
11405
 
11406
          first = false;
11407
          parser->default_arg_ok_p = false;
11408
          parser->in_declarator_p = true;
11409
          /* Consume the `['.  */
11410
          cp_lexer_consume_token (parser->lexer);
11411
          /* Peek at the next token.  */
11412
          token = cp_lexer_peek_token (parser->lexer);
11413
          /* If the next token is `]', then there is no
11414
             constant-expression.  */
11415
          if (token->type != CPP_CLOSE_SQUARE)
11416
            {
11417
              bool non_constant_p;
11418
 
11419
              bounds
11420
                = cp_parser_constant_expression (parser,
11421
                                                 /*allow_non_constant=*/true,
11422
                                                 &non_constant_p);
11423
              if (!non_constant_p)
11424
                bounds = fold_non_dependent_expr (bounds);
11425
              /* Normally, the array bound must be an integral constant
11426
                 expression.  However, as an extension, we allow VLAs
11427
                 in function scopes.  */
11428
              else if (!at_function_scope_p ())
11429
                {
11430
                  error ("array bound is not an integer constant");
11431
                  bounds = error_mark_node;
11432
                }
11433
            }
11434
          else
11435
            bounds = NULL_TREE;
11436
          /* Look for the closing `]'.  */
11437
          if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11438
            {
11439
              declarator = cp_error_declarator;
11440
              break;
11441
            }
11442
 
11443
          declarator = make_array_declarator (declarator, bounds);
11444
        }
11445
      else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11446
        {
11447
          tree qualifying_scope;
11448
          tree unqualified_name;
11449
          special_function_kind sfk;
11450
 
11451
          /* Parse a declarator-id */
11452
          if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11453
            cp_parser_parse_tentatively (parser);
11454
          unqualified_name = cp_parser_declarator_id (parser);
11455
          qualifying_scope = parser->scope;
11456
          if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11457
            {
11458
              if (!cp_parser_parse_definitely (parser))
11459
                unqualified_name = error_mark_node;
11460
              else if (qualifying_scope
11461
                       || (TREE_CODE (unqualified_name)
11462
                           != IDENTIFIER_NODE))
11463
                {
11464
                  cp_parser_error (parser, "expected unqualified-id");
11465
                  unqualified_name = error_mark_node;
11466
                }
11467
            }
11468
 
11469
          if (unqualified_name == error_mark_node)
11470
            {
11471
              declarator = cp_error_declarator;
11472
              break;
11473
            }
11474
 
11475
          if (qualifying_scope && at_namespace_scope_p ()
11476
              && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11477
            {
11478
              /* In the declaration of a member of a template class
11479
                 outside of the class itself, the SCOPE will sometimes
11480
                 be a TYPENAME_TYPE.  For example, given:
11481
 
11482
                 template <typename T>
11483
                 int S<T>::R::i = 3;
11484
 
11485
                 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11486
                 this context, we must resolve S<T>::R to an ordinary
11487
                 type, rather than a typename type.
11488
 
11489
                 The reason we normally avoid resolving TYPENAME_TYPEs
11490
                 is that a specialization of `S' might render
11491
                 `S<T>::R' not a type.  However, if `S' is
11492
                 specialized, then this `i' will not be used, so there
11493
                 is no harm in resolving the types here.  */
11494
              tree type;
11495
 
11496
              /* Resolve the TYPENAME_TYPE.  */
11497
              type = resolve_typename_type (qualifying_scope,
11498
                                            /*only_current_p=*/false);
11499
              /* If that failed, the declarator is invalid.  */
11500
              if (type == error_mark_node)
11501
                error ("%<%T::%D%> is not a type",
11502
                       TYPE_CONTEXT (qualifying_scope),
11503
                       TYPE_IDENTIFIER (qualifying_scope));
11504
              qualifying_scope = type;
11505
            }
11506
 
11507
          sfk = sfk_none;
11508
          if (unqualified_name)
11509
            {
11510
              tree class_type;
11511
 
11512
              if (qualifying_scope
11513
                  && CLASS_TYPE_P (qualifying_scope))
11514
                class_type = qualifying_scope;
11515
              else
11516
                class_type = current_class_type;
11517
 
11518
              if (TREE_CODE (unqualified_name) == TYPE_DECL)
11519
                {
11520
                  tree name_type = TREE_TYPE (unqualified_name);
11521
                  if (class_type && same_type_p (name_type, class_type))
11522
                    {
11523
                      if (qualifying_scope
11524
                          && CLASSTYPE_USE_TEMPLATE (name_type))
11525
                        {
11526
                          error ("invalid use of constructor as a template");
11527
                          inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11528
                                  "name the constructor in a qualified name",
11529
                                  class_type,
11530
                                  DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11531
                                  class_type, name_type);
11532
                          declarator = cp_error_declarator;
11533
                          break;
11534
                        }
11535
                      else
11536
                        unqualified_name = constructor_name (class_type);
11537
                    }
11538
                  else
11539
                    {
11540
                      /* We do not attempt to print the declarator
11541
                         here because we do not have enough
11542
                         information about its original syntactic
11543
                         form.  */
11544
                      cp_parser_error (parser, "invalid declarator");
11545
                      declarator = cp_error_declarator;
11546
                      break;
11547
                    }
11548
                }
11549
 
11550
              if (class_type)
11551
                {
11552
                  if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11553
                    sfk = sfk_destructor;
11554
                  else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11555
                    sfk = sfk_conversion;
11556
                  else if (/* There's no way to declare a constructor
11557
                              for an anonymous type, even if the type
11558
                              got a name for linkage purposes.  */
11559
                           !TYPE_WAS_ANONYMOUS (class_type)
11560
                           && constructor_name_p (unqualified_name,
11561
                                                  class_type))
11562
                    {
11563
                      unqualified_name = constructor_name (class_type);
11564
                      sfk = sfk_constructor;
11565
                    }
11566
 
11567
                  if (ctor_dtor_or_conv_p && sfk != sfk_none)
11568
                    *ctor_dtor_or_conv_p = -1;
11569
                }
11570
            }
11571
          declarator = make_id_declarator (qualifying_scope,
11572
                                           unqualified_name,
11573
                                           sfk);
11574
          declarator->id_loc = token->location;
11575
 
11576
        handle_declarator:;
11577
          scope = get_scope_of_declarator (declarator);
11578
          if (scope)
11579
            /* Any names that appear after the declarator-id for a
11580
               member are looked up in the containing scope.  */
11581
            pushed_scope = push_scope (scope);
11582
          parser->in_declarator_p = true;
11583
          if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11584
              || (declarator && declarator->kind == cdk_id))
11585
            /* Default args are only allowed on function
11586
               declarations.  */
11587
            parser->default_arg_ok_p = saved_default_arg_ok_p;
11588
          else
11589
            parser->default_arg_ok_p = false;
11590
 
11591
          first = false;
11592
        }
11593
      /* We're done.  */
11594
      else
11595
        break;
11596
    }
11597
 
11598
  /* For an abstract declarator, we might wind up with nothing at this
11599
     point.  That's an error; the declarator is not optional.  */
11600
  if (!declarator)
11601
    cp_parser_error (parser, "expected declarator");
11602
 
11603
  /* If we entered a scope, we must exit it now.  */
11604
  if (pushed_scope)
11605
    pop_scope (pushed_scope);
11606
 
11607
  parser->default_arg_ok_p = saved_default_arg_ok_p;
11608
  parser->in_declarator_p = saved_in_declarator_p;
11609
 
11610
  return declarator;
11611
}
11612
 
11613
/* Parse a ptr-operator.
11614
 
11615
   ptr-operator:
11616
     * cv-qualifier-seq [opt]
11617
     &
11618
     :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11619
 
11620
   GNU Extension:
11621
 
11622
   ptr-operator:
11623
     & cv-qualifier-seq [opt]
11624
 
11625
   Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11626
   Returns ADDR_EXPR if a reference was used.  In the case of a
11627
   pointer-to-member, *TYPE is filled in with the TYPE containing the
11628
   member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11629
   TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11630
   ERROR_MARK if an error occurred.  */
11631
 
11632
static enum tree_code
11633
cp_parser_ptr_operator (cp_parser* parser,
11634
                        tree* type,
11635
                        cp_cv_quals *cv_quals)
11636
{
11637
  enum tree_code code = ERROR_MARK;
11638
  cp_token *token;
11639
 
11640
  /* Assume that it's not a pointer-to-member.  */
11641
  *type = NULL_TREE;
11642
  /* And that there are no cv-qualifiers.  */
11643
  *cv_quals = TYPE_UNQUALIFIED;
11644
 
11645
  /* Peek at the next token.  */
11646
  token = cp_lexer_peek_token (parser->lexer);
11647
  /* If it's a `*' or `&' we have a pointer or reference.  */
11648
  if (token->type == CPP_MULT || token->type == CPP_AND)
11649
    {
11650
      /* Remember which ptr-operator we were processing.  */
11651
      code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11652
 
11653
      /* Consume the `*' or `&'.  */
11654
      cp_lexer_consume_token (parser->lexer);
11655
 
11656
      /* A `*' can be followed by a cv-qualifier-seq, and so can a
11657
         `&', if we are allowing GNU extensions.  (The only qualifier
11658
         that can legally appear after `&' is `restrict', but that is
11659
         enforced during semantic analysis.  */
11660
      if (code == INDIRECT_REF
11661
          || cp_parser_allow_gnu_extensions_p (parser))
11662
        *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11663
    }
11664
  else
11665
    {
11666
      /* Try the pointer-to-member case.  */
11667
      cp_parser_parse_tentatively (parser);
11668
      /* Look for the optional `::' operator.  */
11669
      cp_parser_global_scope_opt (parser,
11670
                                  /*current_scope_valid_p=*/false);
11671
      /* Look for the nested-name specifier.  */
11672
      cp_parser_nested_name_specifier (parser,
11673
                                       /*typename_keyword_p=*/false,
11674
                                       /*check_dependency_p=*/true,
11675
                                       /*type_p=*/false,
11676
                                       /*is_declaration=*/false);
11677
      /* If we found it, and the next token is a `*', then we are
11678
         indeed looking at a pointer-to-member operator.  */
11679
      if (!cp_parser_error_occurred (parser)
11680
          && cp_parser_require (parser, CPP_MULT, "`*'"))
11681
        {
11682
          /* Indicate that the `*' operator was used.  */
11683
          code = INDIRECT_REF;
11684
 
11685
          if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11686
            error ("%qD is a namespace", parser->scope);
11687
          else
11688
            {
11689
              /* The type of which the member is a member is given by the
11690
                 current SCOPE.  */
11691
              *type = parser->scope;
11692
              /* The next name will not be qualified.  */
11693
              parser->scope = NULL_TREE;
11694
              parser->qualifying_scope = NULL_TREE;
11695
              parser->object_scope = NULL_TREE;
11696
              /* Look for the optional cv-qualifier-seq.  */
11697
              *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11698
            }
11699
        }
11700
      /* If that didn't work we don't have a ptr-operator.  */
11701
      if (!cp_parser_parse_definitely (parser))
11702
        cp_parser_error (parser, "expected ptr-operator");
11703
    }
11704
 
11705
  return code;
11706
}
11707
 
11708
/* Parse an (optional) cv-qualifier-seq.
11709
 
11710
   cv-qualifier-seq:
11711
     cv-qualifier cv-qualifier-seq [opt]
11712
 
11713
   cv-qualifier:
11714
     const
11715
     volatile
11716
 
11717
   GNU Extension:
11718
 
11719
   cv-qualifier:
11720
     __restrict__
11721
 
11722
   Returns a bitmask representing the cv-qualifiers.  */
11723
 
11724
static cp_cv_quals
11725
cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11726
{
11727
  cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11728
 
11729
  while (true)
11730
    {
11731
      cp_token *token;
11732
      cp_cv_quals cv_qualifier;
11733
 
11734
      /* Peek at the next token.  */
11735
      token = cp_lexer_peek_token (parser->lexer);
11736
      /* See if it's a cv-qualifier.  */
11737
      switch (token->keyword)
11738
        {
11739
        case RID_CONST:
11740
          cv_qualifier = TYPE_QUAL_CONST;
11741
          break;
11742
 
11743
        case RID_VOLATILE:
11744
          cv_qualifier = TYPE_QUAL_VOLATILE;
11745
          break;
11746
 
11747
        case RID_RESTRICT:
11748
          cv_qualifier = TYPE_QUAL_RESTRICT;
11749
          break;
11750
 
11751
        default:
11752
          cv_qualifier = TYPE_UNQUALIFIED;
11753
          break;
11754
        }
11755
 
11756
      if (!cv_qualifier)
11757
        break;
11758
 
11759
      if (cv_quals & cv_qualifier)
11760
        {
11761
          error ("duplicate cv-qualifier");
11762
          cp_lexer_purge_token (parser->lexer);
11763
        }
11764
      else
11765
        {
11766
          cp_lexer_consume_token (parser->lexer);
11767
          cv_quals |= cv_qualifier;
11768
        }
11769
    }
11770
 
11771
  return cv_quals;
11772
}
11773
 
11774
/* Parse a declarator-id.
11775
 
11776
   declarator-id:
11777
     id-expression
11778
     :: [opt] nested-name-specifier [opt] type-name
11779
 
11780
   In the `id-expression' case, the value returned is as for
11781
   cp_parser_id_expression if the id-expression was an unqualified-id.
11782
   If the id-expression was a qualified-id, then a SCOPE_REF is
11783
   returned.  The first operand is the scope (either a NAMESPACE_DECL
11784
   or TREE_TYPE), but the second is still just a representation of an
11785
   unqualified-id.  */
11786
 
11787
static tree
11788
cp_parser_declarator_id (cp_parser* parser)
11789
{
11790
  tree id;
11791
  /* The expression must be an id-expression.  Assume that qualified
11792
     names are the names of types so that:
11793
 
11794
       template <class T>
11795
       int S<T>::R::i = 3;
11796
 
11797
     will work; we must treat `S<T>::R' as the name of a type.
11798
     Similarly, assume that qualified names are templates, where
11799
     required, so that:
11800
 
11801
       template <class T>
11802
       int S<T>::R<T>::i = 3;
11803
 
11804
     will work, too.  */
11805
  id = cp_parser_id_expression (parser,
11806
                                /*template_keyword_p=*/false,
11807
                                /*check_dependency_p=*/false,
11808
                                /*template_p=*/NULL,
11809
                                /*declarator_p=*/true);
11810
  if (BASELINK_P (id))
11811
    id = BASELINK_FUNCTIONS (id);
11812
  return id;
11813
}
11814
 
11815
/* Parse a type-id.
11816
 
11817
   type-id:
11818
     type-specifier-seq abstract-declarator [opt]
11819
 
11820
   Returns the TYPE specified.  */
11821
 
11822
static tree
11823
cp_parser_type_id (cp_parser* parser)
11824
{
11825
  cp_decl_specifier_seq type_specifier_seq;
11826
  cp_declarator *abstract_declarator;
11827
 
11828
  /* Parse the type-specifier-seq.  */
11829
  cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11830
                                &type_specifier_seq);
11831
  if (type_specifier_seq.type == error_mark_node)
11832
    return error_mark_node;
11833
 
11834
  /* There might or might not be an abstract declarator.  */
11835
  cp_parser_parse_tentatively (parser);
11836
  /* Look for the declarator.  */
11837
  abstract_declarator
11838
    = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11839
                            /*parenthesized_p=*/NULL,
11840
                            /*member_p=*/false);
11841
  /* Check to see if there really was a declarator.  */
11842
  if (!cp_parser_parse_definitely (parser))
11843
    abstract_declarator = NULL;
11844
 
11845
  return groktypename (&type_specifier_seq, abstract_declarator);
11846
}
11847
 
11848
/* Parse a type-specifier-seq.
11849
 
11850
   type-specifier-seq:
11851
     type-specifier type-specifier-seq [opt]
11852
 
11853
   GNU extension:
11854
 
11855
   type-specifier-seq:
11856
     attributes type-specifier-seq [opt]
11857
 
11858
   If IS_CONDITION is true, we are at the start of a "condition",
11859
   e.g., we've just seen "if (".
11860
 
11861
   Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11862
 
11863
static void
11864
cp_parser_type_specifier_seq (cp_parser* parser,
11865
                              bool is_condition,
11866
                              cp_decl_specifier_seq *type_specifier_seq)
11867
{
11868
  bool seen_type_specifier = false;
11869
  cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11870
 
11871
  /* Clear the TYPE_SPECIFIER_SEQ.  */
11872
  clear_decl_specs (type_specifier_seq);
11873
 
11874
  /* Parse the type-specifiers and attributes.  */
11875
  while (true)
11876
    {
11877
      tree type_specifier;
11878
      bool is_cv_qualifier;
11879
 
11880
      /* Check for attributes first.  */
11881
      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11882
        {
11883
          type_specifier_seq->attributes =
11884
            chainon (type_specifier_seq->attributes,
11885
                     cp_parser_attributes_opt (parser));
11886
          continue;
11887
        }
11888
 
11889
      /* Look for the type-specifier.  */
11890
      type_specifier = cp_parser_type_specifier (parser,
11891
                                                 flags,
11892
                                                 type_specifier_seq,
11893
                                                 /*is_declaration=*/false,
11894
                                                 NULL,
11895
                                                 &is_cv_qualifier);
11896
      if (!type_specifier)
11897
        {
11898
          /* If the first type-specifier could not be found, this is not a
11899
             type-specifier-seq at all.  */
11900
          if (!seen_type_specifier)
11901
            {
11902
              cp_parser_error (parser, "expected type-specifier");
11903
              type_specifier_seq->type = error_mark_node;
11904
              return;
11905
            }
11906
          /* If subsequent type-specifiers could not be found, the
11907
             type-specifier-seq is complete.  */
11908
          break;
11909
        }
11910
 
11911
      seen_type_specifier = true;
11912
      /* The standard says that a condition can be:
11913
 
11914
            type-specifier-seq declarator = assignment-expression
11915
 
11916
         However, given:
11917
 
11918
           struct S {};
11919
           if (int S = ...)
11920
 
11921
         we should treat the "S" as a declarator, not as a
11922
         type-specifier.  The standard doesn't say that explicitly for
11923
         type-specifier-seq, but it does say that for
11924
         decl-specifier-seq in an ordinary declaration.  Perhaps it
11925
         would be clearer just to allow a decl-specifier-seq here, and
11926
         then add a semantic restriction that if any decl-specifiers
11927
         that are not type-specifiers appear, the program is invalid.  */
11928
      if (is_condition && !is_cv_qualifier)
11929
        flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11930
    }
11931
 
11932
  return;
11933
}
11934
 
11935
/* Parse a parameter-declaration-clause.
11936
 
11937
   parameter-declaration-clause:
11938
     parameter-declaration-list [opt] ... [opt]
11939
     parameter-declaration-list , ...
11940
 
11941
   Returns a representation for the parameter declarations.  A return
11942
   value of NULL indicates a parameter-declaration-clause consisting
11943
   only of an ellipsis.  */
11944
 
11945
static cp_parameter_declarator *
11946
cp_parser_parameter_declaration_clause (cp_parser* parser)
11947
{
11948
  cp_parameter_declarator *parameters;
11949
  cp_token *token;
11950
  bool ellipsis_p;
11951
  bool is_error;
11952
 
11953
  /* Peek at the next token.  */
11954
  token = cp_lexer_peek_token (parser->lexer);
11955
  /* Check for trivial parameter-declaration-clauses.  */
11956
  if (token->type == CPP_ELLIPSIS)
11957
    {
11958
      /* Consume the `...' token.  */
11959
      cp_lexer_consume_token (parser->lexer);
11960
      return NULL;
11961
    }
11962
  else if (token->type == CPP_CLOSE_PAREN)
11963
    /* There are no parameters.  */
11964
    {
11965
#ifndef NO_IMPLICIT_EXTERN_C
11966
      if (in_system_header && current_class_type == NULL
11967
          && current_lang_name == lang_name_c)
11968
        return NULL;
11969
      else
11970
#endif
11971
        return no_parameters;
11972
    }
11973
  /* Check for `(void)', too, which is a special case.  */
11974
  else if (token->keyword == RID_VOID
11975
           && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11976
               == CPP_CLOSE_PAREN))
11977
    {
11978
      /* Consume the `void' token.  */
11979
      cp_lexer_consume_token (parser->lexer);
11980
      /* There are no parameters.  */
11981
      return no_parameters;
11982
    }
11983
 
11984
  /* Parse the parameter-declaration-list.  */
11985
  parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11986
  /* If a parse error occurred while parsing the
11987
     parameter-declaration-list, then the entire
11988
     parameter-declaration-clause is erroneous.  */
11989
  if (is_error)
11990
    return NULL;
11991
 
11992
  /* Peek at the next token.  */
11993
  token = cp_lexer_peek_token (parser->lexer);
11994
  /* If it's a `,', the clause should terminate with an ellipsis.  */
11995
  if (token->type == CPP_COMMA)
11996
    {
11997
      /* Consume the `,'.  */
11998
      cp_lexer_consume_token (parser->lexer);
11999
      /* Expect an ellipsis.  */
12000
      ellipsis_p
12001
        = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12002
    }
12003
  /* It might also be `...' if the optional trailing `,' was
12004
     omitted.  */
12005
  else if (token->type == CPP_ELLIPSIS)
12006
    {
12007
      /* Consume the `...' token.  */
12008
      cp_lexer_consume_token (parser->lexer);
12009
      /* And remember that we saw it.  */
12010
      ellipsis_p = true;
12011
    }
12012
  else
12013
    ellipsis_p = false;
12014
 
12015
  /* Finish the parameter list.  */
12016
  if (parameters && ellipsis_p)
12017
    parameters->ellipsis_p = true;
12018
 
12019
  return parameters;
12020
}
12021
 
12022
/* Parse a parameter-declaration-list.
12023
 
12024
   parameter-declaration-list:
12025
     parameter-declaration
12026
     parameter-declaration-list , parameter-declaration
12027
 
12028
   Returns a representation of the parameter-declaration-list, as for
12029
   cp_parser_parameter_declaration_clause.  However, the
12030
   `void_list_node' is never appended to the list.  Upon return,
12031
   *IS_ERROR will be true iff an error occurred.  */
12032
 
12033
static cp_parameter_declarator *
12034
cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12035
{
12036
  cp_parameter_declarator *parameters = NULL;
12037
  cp_parameter_declarator **tail = &parameters;
12038
 
12039
  /* Assume all will go well.  */
12040
  *is_error = false;
12041
 
12042
  /* Look for more parameters.  */
12043
  while (true)
12044
    {
12045
      cp_parameter_declarator *parameter;
12046
      bool parenthesized_p;
12047
      /* Parse the parameter.  */
12048
      parameter
12049
        = cp_parser_parameter_declaration (parser,
12050
                                           /*template_parm_p=*/false,
12051
                                           &parenthesized_p);
12052
 
12053
      /* If a parse error occurred parsing the parameter declaration,
12054
         then the entire parameter-declaration-list is erroneous.  */
12055
      if (!parameter)
12056
        {
12057
          *is_error = true;
12058
          parameters = NULL;
12059
          break;
12060
        }
12061
      /* Add the new parameter to the list.  */
12062
      *tail = parameter;
12063
      tail = &parameter->next;
12064
 
12065
      /* Peek at the next token.  */
12066
      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12067
          || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12068
          /* These are for Objective-C++ */
12069
          || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12070
          || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12071
        /* The parameter-declaration-list is complete.  */
12072
        break;
12073
      else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12074
        {
12075
          cp_token *token;
12076
 
12077
          /* Peek at the next token.  */
12078
          token = cp_lexer_peek_nth_token (parser->lexer, 2);
12079
          /* If it's an ellipsis, then the list is complete.  */
12080
          if (token->type == CPP_ELLIPSIS)
12081
            break;
12082
          /* Otherwise, there must be more parameters.  Consume the
12083
             `,'.  */
12084
          cp_lexer_consume_token (parser->lexer);
12085
          /* When parsing something like:
12086
 
12087
                int i(float f, double d)
12088
 
12089
             we can tell after seeing the declaration for "f" that we
12090
             are not looking at an initialization of a variable "i",
12091
             but rather at the declaration of a function "i".
12092
 
12093
             Due to the fact that the parsing of template arguments
12094
             (as specified to a template-id) requires backtracking we
12095
             cannot use this technique when inside a template argument
12096
             list.  */
12097
          if (!parser->in_template_argument_list_p
12098
              && !parser->in_type_id_in_expr_p
12099
              && cp_parser_uncommitted_to_tentative_parse_p (parser)
12100
              /* However, a parameter-declaration of the form
12101
                 "foat(f)" (which is a valid declaration of a
12102
                 parameter "f") can also be interpreted as an
12103
                 expression (the conversion of "f" to "float").  */
12104
              && !parenthesized_p)
12105
            cp_parser_commit_to_tentative_parse (parser);
12106
        }
12107
      else
12108
        {
12109
          cp_parser_error (parser, "expected %<,%> or %<...%>");
12110
          if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12111
            cp_parser_skip_to_closing_parenthesis (parser,
12112
                                                   /*recovering=*/true,
12113
                                                   /*or_comma=*/false,
12114
                                                   /*consume_paren=*/false);
12115
          break;
12116
        }
12117
    }
12118
 
12119
  return parameters;
12120
}
12121
 
12122
/* Parse a parameter declaration.
12123
 
12124
   parameter-declaration:
12125
     decl-specifier-seq declarator
12126
     decl-specifier-seq declarator = assignment-expression
12127
     decl-specifier-seq abstract-declarator [opt]
12128
     decl-specifier-seq abstract-declarator [opt] = assignment-expression
12129
 
12130
   If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12131
   declares a template parameter.  (In that case, a non-nested `>'
12132
   token encountered during the parsing of the assignment-expression
12133
   is not interpreted as a greater-than operator.)
12134
 
12135
   Returns a representation of the parameter, or NULL if an error
12136
   occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12137
   true iff the declarator is of the form "(p)".  */
12138
 
12139
static cp_parameter_declarator *
12140
cp_parser_parameter_declaration (cp_parser *parser,
12141
                                 bool template_parm_p,
12142
                                 bool *parenthesized_p)
12143
{
12144
  int declares_class_or_enum;
12145
  bool greater_than_is_operator_p;
12146
  cp_decl_specifier_seq decl_specifiers;
12147
  cp_declarator *declarator;
12148
  tree default_argument;
12149
  cp_token *token;
12150
  const char *saved_message;
12151
 
12152
  /* In a template parameter, `>' is not an operator.
12153
 
12154
     [temp.param]
12155
 
12156
     When parsing a default template-argument for a non-type
12157
     template-parameter, the first non-nested `>' is taken as the end
12158
     of the template parameter-list rather than a greater-than
12159
     operator.  */
12160
  greater_than_is_operator_p = !template_parm_p;
12161
 
12162
  /* Type definitions may not appear in parameter types.  */
12163
  saved_message = parser->type_definition_forbidden_message;
12164
  parser->type_definition_forbidden_message
12165
    = "types may not be defined in parameter types";
12166
 
12167
  /* Parse the declaration-specifiers.  */
12168
  cp_parser_decl_specifier_seq (parser,
12169
                                CP_PARSER_FLAGS_NONE,
12170
                                &decl_specifiers,
12171
                                &declares_class_or_enum);
12172
  /* If an error occurred, there's no reason to attempt to parse the
12173
     rest of the declaration.  */
12174
  if (cp_parser_error_occurred (parser))
12175
    {
12176
      parser->type_definition_forbidden_message = saved_message;
12177
      return NULL;
12178
    }
12179
 
12180
  /* Peek at the next token.  */
12181
  token = cp_lexer_peek_token (parser->lexer);
12182
  /* If the next token is a `)', `,', `=', `>', or `...', then there
12183
     is no declarator.  */
12184
  if (token->type == CPP_CLOSE_PAREN
12185
      || token->type == CPP_COMMA
12186
      || token->type == CPP_EQ
12187
      || token->type == CPP_ELLIPSIS
12188
      || token->type == CPP_GREATER)
12189
    {
12190
      declarator = NULL;
12191
      if (parenthesized_p)
12192
        *parenthesized_p = false;
12193
    }
12194
  /* Otherwise, there should be a declarator.  */
12195
  else
12196
    {
12197
      bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12198
      parser->default_arg_ok_p = false;
12199
 
12200
      /* After seeing a decl-specifier-seq, if the next token is not a
12201
         "(", there is no possibility that the code is a valid
12202
         expression.  Therefore, if parsing tentatively, we commit at
12203
         this point.  */
12204
      if (!parser->in_template_argument_list_p
12205
          /* In an expression context, having seen:
12206
 
12207
               (int((char ...
12208
 
12209
             we cannot be sure whether we are looking at a
12210
             function-type (taking a "char" as a parameter) or a cast
12211
             of some object of type "char" to "int".  */
12212
          && !parser->in_type_id_in_expr_p
12213
          && cp_parser_uncommitted_to_tentative_parse_p (parser)
12214
          && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12215
        cp_parser_commit_to_tentative_parse (parser);
12216
      /* Parse the declarator.  */
12217
      declarator = cp_parser_declarator (parser,
12218
                                         CP_PARSER_DECLARATOR_EITHER,
12219
                                         /*ctor_dtor_or_conv_p=*/NULL,
12220
                                         parenthesized_p,
12221
                                         /*member_p=*/false);
12222
      parser->default_arg_ok_p = saved_default_arg_ok_p;
12223
      /* After the declarator, allow more attributes.  */
12224
      decl_specifiers.attributes
12225
        = chainon (decl_specifiers.attributes,
12226
                   cp_parser_attributes_opt (parser));
12227
    }
12228
 
12229
  /* The restriction on defining new types applies only to the type
12230
     of the parameter, not to the default argument.  */
12231
  parser->type_definition_forbidden_message = saved_message;
12232
 
12233
  /* If the next token is `=', then process a default argument.  */
12234
  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12235
    {
12236
      bool saved_greater_than_is_operator_p;
12237
      /* Consume the `='.  */
12238
      cp_lexer_consume_token (parser->lexer);
12239
 
12240
      /* If we are defining a class, then the tokens that make up the
12241
         default argument must be saved and processed later.  */
12242
      if (!template_parm_p && at_class_scope_p ()
12243
          && TYPE_BEING_DEFINED (current_class_type))
12244
        {
12245
          unsigned depth = 0;
12246
          cp_token *first_token;
12247
          cp_token *token;
12248
 
12249
          /* Add tokens until we have processed the entire default
12250
             argument.  We add the range [first_token, token).  */
12251
          first_token = cp_lexer_peek_token (parser->lexer);
12252
          while (true)
12253
            {
12254
              bool done = false;
12255
 
12256
              /* Peek at the next token.  */
12257
              token = cp_lexer_peek_token (parser->lexer);
12258
              /* What we do depends on what token we have.  */
12259
              switch (token->type)
12260
                {
12261
                  /* In valid code, a default argument must be
12262
                     immediately followed by a `,' `)', or `...'.  */
12263
                case CPP_COMMA:
12264
                case CPP_CLOSE_PAREN:
12265
                case CPP_ELLIPSIS:
12266
                  /* If we run into a non-nested `;', `}', or `]',
12267
                     then the code is invalid -- but the default
12268
                     argument is certainly over.  */
12269
                case CPP_SEMICOLON:
12270
                case CPP_CLOSE_BRACE:
12271
                case CPP_CLOSE_SQUARE:
12272
                  if (depth == 0)
12273
                    done = true;
12274
                  /* Update DEPTH, if necessary.  */
12275
                  else if (token->type == CPP_CLOSE_PAREN
12276
                           || token->type == CPP_CLOSE_BRACE
12277
                           || token->type == CPP_CLOSE_SQUARE)
12278
                    --depth;
12279
                  break;
12280
 
12281
                case CPP_OPEN_PAREN:
12282
                case CPP_OPEN_SQUARE:
12283
                case CPP_OPEN_BRACE:
12284
                  ++depth;
12285
                  break;
12286
 
12287
                case CPP_GREATER:
12288
                  /* If we see a non-nested `>', and `>' is not an
12289
                     operator, then it marks the end of the default
12290
                     argument.  */
12291
                  if (!depth && !greater_than_is_operator_p)
12292
                    done = true;
12293
                  break;
12294
 
12295
                  /* If we run out of tokens, issue an error message.  */
12296
                case CPP_EOF:
12297
                  error ("file ends in default argument");
12298
                  done = true;
12299
                  break;
12300
 
12301
                case CPP_NAME:
12302
                case CPP_SCOPE:
12303
                  /* In these cases, we should look for template-ids.
12304
                     For example, if the default argument is
12305
                     `X<int, double>()', we need to do name lookup to
12306
                     figure out whether or not `X' is a template; if
12307
                     so, the `,' does not end the default argument.
12308
 
12309
                     That is not yet done.  */
12310
                  break;
12311
 
12312
                default:
12313
                  break;
12314
                }
12315
 
12316
              /* If we've reached the end, stop.  */
12317
              if (done)
12318
                break;
12319
 
12320
              /* Add the token to the token block.  */
12321
              token = cp_lexer_consume_token (parser->lexer);
12322
            }
12323
 
12324
          /* Create a DEFAULT_ARG to represented the unparsed default
12325
             argument.  */
12326
          default_argument = make_node (DEFAULT_ARG);
12327
          DEFARG_TOKENS (default_argument)
12328
            = cp_token_cache_new (first_token, token);
12329
          DEFARG_INSTANTIATIONS (default_argument) = NULL;
12330
        }
12331
      /* Outside of a class definition, we can just parse the
12332
         assignment-expression.  */
12333
      else
12334
        {
12335
          bool saved_local_variables_forbidden_p;
12336
 
12337
          /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12338
             set correctly.  */
12339
          saved_greater_than_is_operator_p
12340
            = parser->greater_than_is_operator_p;
12341
          parser->greater_than_is_operator_p = greater_than_is_operator_p;
12342
          /* Local variable names (and the `this' keyword) may not
12343
             appear in a default argument.  */
12344
          saved_local_variables_forbidden_p
12345
            = parser->local_variables_forbidden_p;
12346
          parser->local_variables_forbidden_p = true;
12347
          /* The default argument expression may cause implicitly
12348
             defined member functions to be synthesized, which will
12349
             result in garbage collection.  We must treat this
12350
             situation as if we were within the body of function so as
12351
             to avoid collecting live data on the stack.  */
12352
          ++function_depth;
12353
          /* Parse the assignment-expression.  */
12354
          if (template_parm_p)
12355
            push_deferring_access_checks (dk_no_deferred);
12356
          default_argument
12357
            = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12358
          if (template_parm_p)
12359
            pop_deferring_access_checks ();
12360
          /* Restore saved state.  */
12361
          --function_depth;
12362
          parser->greater_than_is_operator_p
12363
            = saved_greater_than_is_operator_p;
12364
          parser->local_variables_forbidden_p
12365
            = saved_local_variables_forbidden_p;
12366
        }
12367
      if (!parser->default_arg_ok_p)
12368
        {
12369
          if (!flag_pedantic_errors)
12370
            warning (0, "deprecated use of default argument for parameter of non-function");
12371
          else
12372
            {
12373
              error ("default arguments are only permitted for function parameters");
12374
              default_argument = NULL_TREE;
12375
            }
12376
        }
12377
    }
12378
  else
12379
    default_argument = NULL_TREE;
12380
 
12381
  return make_parameter_declarator (&decl_specifiers,
12382
                                    declarator,
12383
                                    default_argument);
12384
}
12385
 
12386
/* Parse a function-body.
12387
 
12388
   function-body:
12389
     compound_statement  */
12390
 
12391
static void
12392
cp_parser_function_body (cp_parser *parser)
12393
{
12394
  cp_parser_compound_statement (parser, NULL, false);
12395
}
12396
 
12397
/* Parse a ctor-initializer-opt followed by a function-body.  Return
12398
   true if a ctor-initializer was present.  */
12399
 
12400
static bool
12401
cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12402
{
12403
  tree body;
12404
  bool ctor_initializer_p;
12405
 
12406
  /* Begin the function body.  */
12407
  body = begin_function_body ();
12408
  /* Parse the optional ctor-initializer.  */
12409
  ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12410
  /* Parse the function-body.  */
12411
  cp_parser_function_body (parser);
12412
  /* Finish the function body.  */
12413
  finish_function_body (body);
12414
 
12415
  return ctor_initializer_p;
12416
}
12417
 
12418
/* Parse an initializer.
12419
 
12420
   initializer:
12421
     = initializer-clause
12422
     ( expression-list )
12423
 
12424
   Returns an expression representing the initializer.  If no
12425
   initializer is present, NULL_TREE is returned.
12426
 
12427
   *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12428
   production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12429
   set to FALSE if there is no initializer present.  If there is an
12430
   initializer, and it is not a constant-expression, *NON_CONSTANT_P
12431
   is set to true; otherwise it is set to false.  */
12432
 
12433
static tree
12434
cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12435
                       bool* non_constant_p)
12436
{
12437
  cp_token *token;
12438
  tree init;
12439
 
12440
  /* Peek at the next token.  */
12441
  token = cp_lexer_peek_token (parser->lexer);
12442
 
12443
  /* Let our caller know whether or not this initializer was
12444
     parenthesized.  */
12445
  *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12446
  /* Assume that the initializer is constant.  */
12447
  *non_constant_p = false;
12448
 
12449
  if (token->type == CPP_EQ)
12450
    {
12451
      /* Consume the `='.  */
12452
      cp_lexer_consume_token (parser->lexer);
12453
      /* Parse the initializer-clause.  */
12454
      init = cp_parser_initializer_clause (parser, non_constant_p);
12455
    }
12456
  else if (token->type == CPP_OPEN_PAREN)
12457
    init = cp_parser_parenthesized_expression_list (parser, false,
12458
                                                    /*cast_p=*/false,
12459
                                                    non_constant_p);
12460
  else
12461
    {
12462
      /* Anything else is an error.  */
12463
      cp_parser_error (parser, "expected initializer");
12464
      init = error_mark_node;
12465
    }
12466
 
12467
  return init;
12468
}
12469
 
12470
/* Parse an initializer-clause.
12471
 
12472
   initializer-clause:
12473
     assignment-expression
12474
     { initializer-list , [opt] }
12475
     { }
12476
 
12477
   Returns an expression representing the initializer.
12478
 
12479
   If the `assignment-expression' production is used the value
12480
   returned is simply a representation for the expression.
12481
 
12482
   Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12483
   the elements of the initializer-list (or NULL, if the last
12484
   production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12485
   NULL_TREE.  There is no way to detect whether or not the optional
12486
   trailing `,' was provided.  NON_CONSTANT_P is as for
12487
   cp_parser_initializer.  */
12488
 
12489
static tree
12490
cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12491
{
12492
  tree initializer;
12493
 
12494
  /* Assume the expression is constant.  */
12495
  *non_constant_p = false;
12496
 
12497
  /* If it is not a `{', then we are looking at an
12498
     assignment-expression.  */
12499
  if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12500
    {
12501
      initializer
12502
        = cp_parser_constant_expression (parser,
12503
                                        /*allow_non_constant_p=*/true,
12504
                                        non_constant_p);
12505
      if (!*non_constant_p)
12506
        initializer = fold_non_dependent_expr (initializer);
12507
    }
12508
  else
12509
    {
12510
      /* Consume the `{' token.  */
12511
      cp_lexer_consume_token (parser->lexer);
12512
      /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12513
      initializer = make_node (CONSTRUCTOR);
12514
      /* If it's not a `}', then there is a non-trivial initializer.  */
12515
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12516
        {
12517
          /* Parse the initializer list.  */
12518
          CONSTRUCTOR_ELTS (initializer)
12519
            = cp_parser_initializer_list (parser, non_constant_p);
12520
          /* A trailing `,' token is allowed.  */
12521
          if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12522
            cp_lexer_consume_token (parser->lexer);
12523
        }
12524
      /* Now, there should be a trailing `}'.  */
12525
      cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12526
    }
12527
 
12528
  return initializer;
12529
}
12530
 
12531
/* Parse an initializer-list.
12532
 
12533
   initializer-list:
12534
     initializer-clause
12535
     initializer-list , initializer-clause
12536
 
12537
   GNU Extension:
12538
 
12539
   initializer-list:
12540
     identifier : initializer-clause
12541
     initializer-list, identifier : initializer-clause
12542
 
12543
   Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
12544
   for the initializer.  If the INDEX of the elt is non-NULL, it is the
12545
   IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12546
   as for cp_parser_initializer.  */
12547
 
12548
static VEC(constructor_elt,gc) *
12549
cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12550
{
12551
  VEC(constructor_elt,gc) *v = NULL;
12552
 
12553
  /* Assume all of the expressions are constant.  */
12554
  *non_constant_p = false;
12555
 
12556
  /* Parse the rest of the list.  */
12557
  while (true)
12558
    {
12559
      cp_token *token;
12560
      tree identifier;
12561
      tree initializer;
12562
      bool clause_non_constant_p;
12563
 
12564
      /* If the next token is an identifier and the following one is a
12565
         colon, we are looking at the GNU designated-initializer
12566
         syntax.  */
12567
      if (cp_parser_allow_gnu_extensions_p (parser)
12568
          && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12569
          && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12570
        {
12571
          /* Consume the identifier.  */
12572
          identifier = cp_lexer_consume_token (parser->lexer)->value;
12573
          /* Consume the `:'.  */
12574
          cp_lexer_consume_token (parser->lexer);
12575
        }
12576
      else
12577
        identifier = NULL_TREE;
12578
 
12579
      /* Parse the initializer.  */
12580
      initializer = cp_parser_initializer_clause (parser,
12581
                                                  &clause_non_constant_p);
12582
      /* If any clause is non-constant, so is the entire initializer.  */
12583
      if (clause_non_constant_p)
12584
        *non_constant_p = true;
12585
 
12586
      /* Add it to the vector.  */
12587
      CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12588
 
12589
      /* If the next token is not a comma, we have reached the end of
12590
         the list.  */
12591
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12592
        break;
12593
 
12594
      /* Peek at the next token.  */
12595
      token = cp_lexer_peek_nth_token (parser->lexer, 2);
12596
      /* If the next token is a `}', then we're still done.  An
12597
         initializer-clause can have a trailing `,' after the
12598
         initializer-list and before the closing `}'.  */
12599
      if (token->type == CPP_CLOSE_BRACE)
12600
        break;
12601
 
12602
      /* Consume the `,' token.  */
12603
      cp_lexer_consume_token (parser->lexer);
12604
    }
12605
 
12606
  return v;
12607
}
12608
 
12609
/* Classes [gram.class] */
12610
 
12611
/* Parse a class-name.
12612
 
12613
   class-name:
12614
     identifier
12615
     template-id
12616
 
12617
   TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12618
   to indicate that names looked up in dependent types should be
12619
   assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12620
   keyword has been used to indicate that the name that appears next
12621
   is a template.  TAG_TYPE indicates the explicit tag given before
12622
   the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12623
   looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12624
   is the class being defined in a class-head.
12625
 
12626
   Returns the TYPE_DECL representing the class.  */
12627
 
12628
static tree
12629
cp_parser_class_name (cp_parser *parser,
12630
                      bool typename_keyword_p,
12631
                      bool template_keyword_p,
12632
                      enum tag_types tag_type,
12633
                      bool check_dependency_p,
12634
                      bool class_head_p,
12635
                      bool is_declaration)
12636
{
12637
  tree decl;
12638
  tree scope;
12639
  bool typename_p;
12640
  cp_token *token;
12641
 
12642
  /* All class-names start with an identifier.  */
12643
  token = cp_lexer_peek_token (parser->lexer);
12644
  if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12645
    {
12646
      cp_parser_error (parser, "expected class-name");
12647
      return error_mark_node;
12648
    }
12649
 
12650
  /* PARSER->SCOPE can be cleared when parsing the template-arguments
12651
     to a template-id, so we save it here.  */
12652
  scope = parser->scope;
12653
  if (scope == error_mark_node)
12654
    return error_mark_node;
12655
 
12656
  /* Any name names a type if we're following the `typename' keyword
12657
     in a qualified name where the enclosing scope is type-dependent.  */
12658
  typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12659
                && dependent_type_p (scope));
12660
  /* Handle the common case (an identifier, but not a template-id)
12661
     efficiently.  */
12662
  if (token->type == CPP_NAME
12663
      && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12664
    {
12665
      cp_token *identifier_token;
12666
      tree identifier;
12667
      bool ambiguous_p;
12668
 
12669
      /* Look for the identifier.  */
12670
      identifier_token = cp_lexer_peek_token (parser->lexer);
12671
      ambiguous_p = identifier_token->ambiguous_p;
12672
      identifier = cp_parser_identifier (parser);
12673
      /* If the next token isn't an identifier, we are certainly not
12674
         looking at a class-name.  */
12675
      if (identifier == error_mark_node)
12676
        decl = error_mark_node;
12677
      /* If we know this is a type-name, there's no need to look it
12678
         up.  */
12679
      else if (typename_p)
12680
        decl = identifier;
12681
      else
12682
        {
12683
          tree ambiguous_decls;
12684
          /* If we already know that this lookup is ambiguous, then
12685
             we've already issued an error message; there's no reason
12686
             to check again.  */
12687
          if (ambiguous_p)
12688
            {
12689
              cp_parser_simulate_error (parser);
12690
              return error_mark_node;
12691
            }
12692
          /* If the next token is a `::', then the name must be a type
12693
             name.
12694
 
12695
             [basic.lookup.qual]
12696
 
12697
             During the lookup for a name preceding the :: scope
12698
             resolution operator, object, function, and enumerator
12699
             names are ignored.  */
12700
          if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12701
            tag_type = typename_type;
12702
          /* Look up the name.  */
12703
          decl = cp_parser_lookup_name (parser, identifier,
12704
                                        tag_type,
12705
                                        /*is_template=*/false,
12706
                                        /*is_namespace=*/false,
12707
                                        check_dependency_p,
12708
                                        &ambiguous_decls);
12709
          if (ambiguous_decls)
12710
            {
12711
              error ("reference to %qD is ambiguous", identifier);
12712
              print_candidates (ambiguous_decls);
12713
              if (cp_parser_parsing_tentatively (parser))
12714
                {
12715
                  identifier_token->ambiguous_p = true;
12716
                  cp_parser_simulate_error (parser);
12717
                }
12718
              return error_mark_node;
12719
            }
12720
        }
12721
    }
12722
  else
12723
    {
12724
      /* Try a template-id.  */
12725
      decl = cp_parser_template_id (parser, template_keyword_p,
12726
                                    check_dependency_p,
12727
                                    is_declaration);
12728
      if (decl == error_mark_node)
12729
        return error_mark_node;
12730
    }
12731
 
12732
  decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12733
 
12734
  /* If this is a typename, create a TYPENAME_TYPE.  */
12735
  if (typename_p && decl != error_mark_node)
12736
    {
12737
      decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12738
      if (decl != error_mark_node)
12739
        decl = TYPE_NAME (decl);
12740
    }
12741
 
12742
  /* Check to see that it is really the name of a class.  */
12743
  if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12744
      && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12745
      && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12746
    /* Situations like this:
12747
 
12748
         template <typename T> struct A {
12749
           typename T::template X<int>::I i;
12750
         };
12751
 
12752
       are problematic.  Is `T::template X<int>' a class-name?  The
12753
       standard does not seem to be definitive, but there is no other
12754
       valid interpretation of the following `::'.  Therefore, those
12755
       names are considered class-names.  */
12756
    {
12757
      decl = make_typename_type (scope, decl, tag_type, tf_error);
12758
      if (decl != error_mark_node)
12759
        decl = TYPE_NAME (decl);
12760
    }
12761
  else if (TREE_CODE (decl) != TYPE_DECL
12762
           || TREE_TYPE (decl) == error_mark_node
12763
           || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12764
    decl = error_mark_node;
12765
 
12766
  if (decl == error_mark_node)
12767
    cp_parser_error (parser, "expected class-name");
12768
 
12769
  return decl;
12770
}
12771
 
12772
/* Parse a class-specifier.
12773
 
12774
   class-specifier:
12775
     class-head { member-specification [opt] }
12776
 
12777
   Returns the TREE_TYPE representing the class.  */
12778
 
12779
static tree
12780
cp_parser_class_specifier (cp_parser* parser)
12781
{
12782
  cp_token *token;
12783
  tree type;
12784
  tree attributes = NULL_TREE;
12785
  int has_trailing_semicolon;
12786
  bool nested_name_specifier_p;
12787
  unsigned saved_num_template_parameter_lists;
12788
  tree old_scope = NULL_TREE;
12789
  tree scope = NULL_TREE;
12790
 
12791
  push_deferring_access_checks (dk_no_deferred);
12792
 
12793
  /* Parse the class-head.  */
12794
  type = cp_parser_class_head (parser,
12795
                               &nested_name_specifier_p,
12796
                               &attributes);
12797
  /* If the class-head was a semantic disaster, skip the entire body
12798
     of the class.  */
12799
  if (!type)
12800
    {
12801
      cp_parser_skip_to_end_of_block_or_statement (parser);
12802
      pop_deferring_access_checks ();
12803
      return error_mark_node;
12804
    }
12805
 
12806
  /* Look for the `{'.  */
12807
  if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12808
    {
12809
      pop_deferring_access_checks ();
12810
      return error_mark_node;
12811
    }
12812
 
12813
  /* Issue an error message if type-definitions are forbidden here.  */
12814
  cp_parser_check_type_definition (parser);
12815
  /* Remember that we are defining one more class.  */
12816
  ++parser->num_classes_being_defined;
12817
  /* Inside the class, surrounding template-parameter-lists do not
12818
     apply.  */
12819
  saved_num_template_parameter_lists
12820
    = parser->num_template_parameter_lists;
12821
  parser->num_template_parameter_lists = 0;
12822
 
12823
  /* Start the class.  */
12824
  if (nested_name_specifier_p)
12825
    {
12826
      scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12827
      old_scope = push_inner_scope (scope);
12828
    }
12829
  type = begin_class_definition (type);
12830
 
12831
  if (type == error_mark_node)
12832
    /* If the type is erroneous, skip the entire body of the class.  */
12833
    cp_parser_skip_to_closing_brace (parser);
12834
  else
12835
    /* Parse the member-specification.  */
12836
    cp_parser_member_specification_opt (parser);
12837
 
12838
  /* Look for the trailing `}'.  */
12839
  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12840
  /* We get better error messages by noticing a common problem: a
12841
     missing trailing `;'.  */
12842
  token = cp_lexer_peek_token (parser->lexer);
12843
  has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12844
  /* Look for trailing attributes to apply to this class.  */
12845
  if (cp_parser_allow_gnu_extensions_p (parser))
12846
    {
12847
      tree sub_attr = cp_parser_attributes_opt (parser);
12848
      attributes = chainon (attributes, sub_attr);
12849
    }
12850
  if (type != error_mark_node)
12851
    type = finish_struct (type, attributes);
12852
  if (nested_name_specifier_p)
12853
    pop_inner_scope (old_scope, scope);
12854
  /* If this class is not itself within the scope of another class,
12855
     then we need to parse the bodies of all of the queued function
12856
     definitions.  Note that the queued functions defined in a class
12857
     are not always processed immediately following the
12858
     class-specifier for that class.  Consider:
12859
 
12860
       struct A {
12861
         struct B { void f() { sizeof (A); } };
12862
       };
12863
 
12864
     If `f' were processed before the processing of `A' were
12865
     completed, there would be no way to compute the size of `A'.
12866
     Note that the nesting we are interested in here is lexical --
12867
     not the semantic nesting given by TYPE_CONTEXT.  In particular,
12868
     for:
12869
 
12870
       struct A { struct B; };
12871
       struct A::B { void f() { } };
12872
 
12873
     there is no need to delay the parsing of `A::B::f'.  */
12874
  if (--parser->num_classes_being_defined == 0)
12875
    {
12876
      tree queue_entry;
12877
      tree fn;
12878
      tree class_type = NULL_TREE;
12879
      tree pushed_scope = NULL_TREE;
12880
 
12881
      /* In a first pass, parse default arguments to the functions.
12882
         Then, in a second pass, parse the bodies of the functions.
12883
         This two-phased approach handles cases like:
12884
 
12885
            struct S {
12886
              void f() { g(); }
12887
              void g(int i = 3);
12888
            };
12889
 
12890
         */
12891
      for (TREE_PURPOSE (parser->unparsed_functions_queues)
12892
             = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12893
           (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12894
           TREE_PURPOSE (parser->unparsed_functions_queues)
12895
             = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12896
        {
12897
          fn = TREE_VALUE (queue_entry);
12898
          /* If there are default arguments that have not yet been processed,
12899
             take care of them now.  */
12900
          if (class_type != TREE_PURPOSE (queue_entry))
12901
            {
12902
              if (pushed_scope)
12903
                pop_scope (pushed_scope);
12904
              class_type = TREE_PURPOSE (queue_entry);
12905
              pushed_scope = push_scope (class_type);
12906
            }
12907
          /* Make sure that any template parameters are in scope.  */
12908
          maybe_begin_member_template_processing (fn);
12909
          /* Parse the default argument expressions.  */
12910
          cp_parser_late_parsing_default_args (parser, fn);
12911
          /* Remove any template parameters from the symbol table.  */
12912
          maybe_end_member_template_processing ();
12913
        }
12914
      if (pushed_scope)
12915
        pop_scope (pushed_scope);
12916
      /* Now parse the body of the functions.  */
12917
      for (TREE_VALUE (parser->unparsed_functions_queues)
12918
             = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12919
           (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12920
           TREE_VALUE (parser->unparsed_functions_queues)
12921
             = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12922
        {
12923
          /* Figure out which function we need to process.  */
12924
          fn = TREE_VALUE (queue_entry);
12925
          /* Parse the function.  */
12926
          cp_parser_late_parsing_for_member (parser, fn);
12927
        }
12928
    }
12929
 
12930
  /* Put back any saved access checks.  */
12931
  pop_deferring_access_checks ();
12932
 
12933
  /* Restore the count of active template-parameter-lists.  */
12934
  parser->num_template_parameter_lists
12935
    = saved_num_template_parameter_lists;
12936
 
12937
  return type;
12938
}
12939
 
12940
/* Parse a class-head.
12941
 
12942
   class-head:
12943
     class-key identifier [opt] base-clause [opt]
12944
     class-key nested-name-specifier identifier base-clause [opt]
12945
     class-key nested-name-specifier [opt] template-id
12946
       base-clause [opt]
12947
 
12948
   GNU Extensions:
12949
     class-key attributes identifier [opt] base-clause [opt]
12950
     class-key attributes nested-name-specifier identifier base-clause [opt]
12951
     class-key attributes nested-name-specifier [opt] template-id
12952
       base-clause [opt]
12953
 
12954
   Returns the TYPE of the indicated class.  Sets
12955
   *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12956
   involving a nested-name-specifier was used, and FALSE otherwise.
12957
 
12958
   Returns error_mark_node if this is not a class-head.
12959
 
12960
   Returns NULL_TREE if the class-head is syntactically valid, but
12961
   semantically invalid in a way that means we should skip the entire
12962
   body of the class.  */
12963
 
12964
static tree
12965
cp_parser_class_head (cp_parser* parser,
12966
                      bool* nested_name_specifier_p,
12967
                      tree *attributes_p)
12968
{
12969
  tree nested_name_specifier;
12970
  enum tag_types class_key;
12971
  tree id = NULL_TREE;
12972
  tree type = NULL_TREE;
12973
  tree attributes;
12974
  bool template_id_p = false;
12975
  bool qualified_p = false;
12976
  bool invalid_nested_name_p = false;
12977
  bool invalid_explicit_specialization_p = false;
12978
  tree pushed_scope = NULL_TREE;
12979
  unsigned num_templates;
12980
  tree bases;
12981
 
12982
  /* Assume no nested-name-specifier will be present.  */
12983
  *nested_name_specifier_p = false;
12984
  /* Assume no template parameter lists will be used in defining the
12985
     type.  */
12986
  num_templates = 0;
12987
 
12988
  /* Look for the class-key.  */
12989
  class_key = cp_parser_class_key (parser);
12990
  if (class_key == none_type)
12991
    return error_mark_node;
12992
 
12993
  /* Parse the attributes.  */
12994
  attributes = cp_parser_attributes_opt (parser);
12995
 
12996
  /* If the next token is `::', that is invalid -- but sometimes
12997
     people do try to write:
12998
 
12999
       struct ::S {};
13000
 
13001
     Handle this gracefully by accepting the extra qualifier, and then
13002
     issuing an error about it later if this really is a
13003
     class-head.  If it turns out just to be an elaborated type
13004
     specifier, remain silent.  */
13005
  if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13006
    qualified_p = true;
13007
 
13008
  push_deferring_access_checks (dk_no_check);
13009
 
13010
  /* Determine the name of the class.  Begin by looking for an
13011
     optional nested-name-specifier.  */
13012
  nested_name_specifier
13013
    = cp_parser_nested_name_specifier_opt (parser,
13014
                                           /*typename_keyword_p=*/false,
13015
                                           /*check_dependency_p=*/false,
13016
                                           /*type_p=*/false,
13017
                                           /*is_declaration=*/false);
13018
  /* If there was a nested-name-specifier, then there *must* be an
13019
     identifier.  */
13020
  if (nested_name_specifier)
13021
    {
13022
      /* Although the grammar says `identifier', it really means
13023
         `class-name' or `template-name'.  You are only allowed to
13024
         define a class that has already been declared with this
13025
         syntax.
13026
 
13027
         The proposed resolution for Core Issue 180 says that whever
13028
         you see `class T::X' you should treat `X' as a type-name.
13029
 
13030
         It is OK to define an inaccessible class; for example:
13031
 
13032
           class A { class B; };
13033
           class A::B {};
13034
 
13035
         We do not know if we will see a class-name, or a
13036
         template-name.  We look for a class-name first, in case the
13037
         class-name is a template-id; if we looked for the
13038
         template-name first we would stop after the template-name.  */
13039
      cp_parser_parse_tentatively (parser);
13040
      type = cp_parser_class_name (parser,
13041
                                   /*typename_keyword_p=*/false,
13042
                                   /*template_keyword_p=*/false,
13043
                                   class_type,
13044
                                   /*check_dependency_p=*/false,
13045
                                   /*class_head_p=*/true,
13046
                                   /*is_declaration=*/false);
13047
      /* If that didn't work, ignore the nested-name-specifier.  */
13048
      if (!cp_parser_parse_definitely (parser))
13049
        {
13050
          invalid_nested_name_p = true;
13051
          id = cp_parser_identifier (parser);
13052
          if (id == error_mark_node)
13053
            id = NULL_TREE;
13054
        }
13055
      /* If we could not find a corresponding TYPE, treat this
13056
         declaration like an unqualified declaration.  */
13057
      if (type == error_mark_node)
13058
        nested_name_specifier = NULL_TREE;
13059
      /* Otherwise, count the number of templates used in TYPE and its
13060
         containing scopes.  */
13061
      else
13062
        {
13063
          tree scope;
13064
 
13065
          for (scope = TREE_TYPE (type);
13066
               scope && TREE_CODE (scope) != NAMESPACE_DECL;
13067
               scope = (TYPE_P (scope)
13068
                        ? TYPE_CONTEXT (scope)
13069
                        : DECL_CONTEXT (scope)))
13070
            if (TYPE_P (scope)
13071
                && CLASS_TYPE_P (scope)
13072
                && CLASSTYPE_TEMPLATE_INFO (scope)
13073
                && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13074
                && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13075
              ++num_templates;
13076
        }
13077
    }
13078
  /* Otherwise, the identifier is optional.  */
13079
  else
13080
    {
13081
      /* We don't know whether what comes next is a template-id,
13082
         an identifier, or nothing at all.  */
13083
      cp_parser_parse_tentatively (parser);
13084
      /* Check for a template-id.  */
13085
      id = cp_parser_template_id (parser,
13086
                                  /*template_keyword_p=*/false,
13087
                                  /*check_dependency_p=*/true,
13088
                                  /*is_declaration=*/true);
13089
      /* If that didn't work, it could still be an identifier.  */
13090
      if (!cp_parser_parse_definitely (parser))
13091
        {
13092
          if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13093
            id = cp_parser_identifier (parser);
13094
          else
13095
            id = NULL_TREE;
13096
        }
13097
      else
13098
        {
13099
          template_id_p = true;
13100
          ++num_templates;
13101
        }
13102
    }
13103
 
13104
  pop_deferring_access_checks ();
13105
 
13106
  if (id)
13107
    cp_parser_check_for_invalid_template_id (parser, id);
13108
 
13109
  /* If it's not a `:' or a `{' then we can't really be looking at a
13110
     class-head, since a class-head only appears as part of a
13111
     class-specifier.  We have to detect this situation before calling
13112
     xref_tag, since that has irreversible side-effects.  */
13113
  if (!cp_parser_next_token_starts_class_definition_p (parser))
13114
    {
13115
      cp_parser_error (parser, "expected %<{%> or %<:%>");
13116
      return error_mark_node;
13117
    }
13118
 
13119
  /* At this point, we're going ahead with the class-specifier, even
13120
     if some other problem occurs.  */
13121
  cp_parser_commit_to_tentative_parse (parser);
13122
  /* Issue the error about the overly-qualified name now.  */
13123
  if (qualified_p)
13124
    cp_parser_error (parser,
13125
                     "global qualification of class name is invalid");
13126
  else if (invalid_nested_name_p)
13127
    cp_parser_error (parser,
13128
                     "qualified name does not name a class");
13129
  else if (nested_name_specifier)
13130
    {
13131
      tree scope;
13132
 
13133
      /* Reject typedef-names in class heads.  */
13134
      if (!DECL_IMPLICIT_TYPEDEF_P (type))
13135
        {
13136
          error ("invalid class name in declaration of %qD", type);
13137
          type = NULL_TREE;
13138
          goto done;
13139
        }
13140
 
13141
      /* Figure out in what scope the declaration is being placed.  */
13142
      scope = current_scope ();
13143
      /* If that scope does not contain the scope in which the
13144
         class was originally declared, the program is invalid.  */
13145
      if (scope && !is_ancestor (scope, nested_name_specifier))
13146
        {
13147
          error ("declaration of %qD in %qD which does not enclose %qD",
13148
                 type, scope, nested_name_specifier);
13149
          type = NULL_TREE;
13150
          goto done;
13151
        }
13152
      /* [dcl.meaning]
13153
 
13154
         A declarator-id shall not be qualified exception of the
13155
         definition of a ... nested class outside of its class
13156
         ... [or] a the definition or explicit instantiation of a
13157
         class member of a namespace outside of its namespace.  */
13158
      if (scope == nested_name_specifier)
13159
        {
13160
          pedwarn ("extra qualification ignored");
13161
          nested_name_specifier = NULL_TREE;
13162
          num_templates = 0;
13163
        }
13164
    }
13165
  /* An explicit-specialization must be preceded by "template <>".  If
13166
     it is not, try to recover gracefully.  */
13167
  if (at_namespace_scope_p ()
13168
      && parser->num_template_parameter_lists == 0
13169
      && template_id_p)
13170
    {
13171
      error ("an explicit specialization must be preceded by %<template <>%>");
13172
      invalid_explicit_specialization_p = true;
13173
      /* Take the same action that would have been taken by
13174
         cp_parser_explicit_specialization.  */
13175
      ++parser->num_template_parameter_lists;
13176
      begin_specialization ();
13177
    }
13178
  /* There must be no "return" statements between this point and the
13179
     end of this function; set "type "to the correct return value and
13180
     use "goto done;" to return.  */
13181
  /* Make sure that the right number of template parameters were
13182
     present.  */
13183
  if (!cp_parser_check_template_parameters (parser, num_templates))
13184
    {
13185
      /* If something went wrong, there is no point in even trying to
13186
         process the class-definition.  */
13187
      type = NULL_TREE;
13188
      goto done;
13189
    }
13190
 
13191
  /* Look up the type.  */
13192
  if (template_id_p)
13193
    {
13194
      type = TREE_TYPE (id);
13195
      maybe_process_partial_specialization (type);
13196
      if (nested_name_specifier)
13197
        pushed_scope = push_scope (nested_name_specifier);
13198
    }
13199
  else if (nested_name_specifier)
13200
    {
13201
      tree class_type;
13202
 
13203
      /* Given:
13204
 
13205
            template <typename T> struct S { struct T };
13206
            template <typename T> struct S<T>::T { };
13207
 
13208
         we will get a TYPENAME_TYPE when processing the definition of
13209
         `S::T'.  We need to resolve it to the actual type before we
13210
         try to define it.  */
13211
      if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13212
        {
13213
          class_type = resolve_typename_type (TREE_TYPE (type),
13214
                                              /*only_current_p=*/false);
13215
          if (class_type != error_mark_node)
13216
            type = TYPE_NAME (class_type);
13217
          else
13218
            {
13219
              cp_parser_error (parser, "could not resolve typename type");
13220
              type = error_mark_node;
13221
            }
13222
        }
13223
 
13224
      maybe_process_partial_specialization (TREE_TYPE (type));
13225
      class_type = current_class_type;
13226
      /* Enter the scope indicated by the nested-name-specifier.  */
13227
      pushed_scope = push_scope (nested_name_specifier);
13228
      /* Get the canonical version of this type.  */
13229
      type = TYPE_MAIN_DECL (TREE_TYPE (type));
13230
      if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13231
          && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13232
        {
13233
          type = push_template_decl (type);
13234
          if (type == error_mark_node)
13235
            {
13236
              type = NULL_TREE;
13237
              goto done;
13238
            }
13239
        }
13240
 
13241
      type = TREE_TYPE (type);
13242
      *nested_name_specifier_p = true;
13243
    }
13244
  else      /* The name is not a nested name.  */
13245
    {
13246
      /* If the class was unnamed, create a dummy name.  */
13247
      if (!id)
13248
        id = make_anon_name ();
13249
      type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13250
                       parser->num_template_parameter_lists);
13251
    }
13252
 
13253
  /* Indicate whether this class was declared as a `class' or as a
13254
     `struct'.  */
13255
  if (TREE_CODE (type) == RECORD_TYPE)
13256
    CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13257
  cp_parser_check_class_key (class_key, type);
13258
 
13259
  /* If this type was already complete, and we see another definition,
13260
     that's an error.  */
13261
  if (type != error_mark_node && COMPLETE_TYPE_P (type))
13262
    {
13263
      error ("redefinition of %q#T", type);
13264
      error ("previous definition of %q+#T", type);
13265
      type = NULL_TREE;
13266
      goto done;
13267
    }
13268
 
13269
  /* We will have entered the scope containing the class; the names of
13270
     base classes should be looked up in that context.  For example:
13271
 
13272
       struct A { struct B {}; struct C; };
13273
       struct A::C : B {};
13274
 
13275
     is valid.  */
13276
  bases = NULL_TREE;
13277
 
13278
  /* Get the list of base-classes, if there is one.  */
13279
  if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13280
    bases = cp_parser_base_clause (parser);
13281
 
13282
  /* Process the base classes.  */
13283
  xref_basetypes (type, bases);
13284
 
13285
 done:
13286
  /* Leave the scope given by the nested-name-specifier.  We will
13287
     enter the class scope itself while processing the members.  */
13288
  if (pushed_scope)
13289
    pop_scope (pushed_scope);
13290
 
13291
  if (invalid_explicit_specialization_p)
13292
    {
13293
      end_specialization ();
13294
      --parser->num_template_parameter_lists;
13295
    }
13296
  *attributes_p = attributes;
13297
  return type;
13298
}
13299
 
13300
/* Parse a class-key.
13301
 
13302
   class-key:
13303
     class
13304
     struct
13305
     union
13306
 
13307
   Returns the kind of class-key specified, or none_type to indicate
13308
   error.  */
13309
 
13310
static enum tag_types
13311
cp_parser_class_key (cp_parser* parser)
13312
{
13313
  cp_token *token;
13314
  enum tag_types tag_type;
13315
 
13316
  /* Look for the class-key.  */
13317
  token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13318
  if (!token)
13319
    return none_type;
13320
 
13321
  /* Check to see if the TOKEN is a class-key.  */
13322
  tag_type = cp_parser_token_is_class_key (token);
13323
  if (!tag_type)
13324
    cp_parser_error (parser, "expected class-key");
13325
  return tag_type;
13326
}
13327
 
13328
/* Parse an (optional) member-specification.
13329
 
13330
   member-specification:
13331
     member-declaration member-specification [opt]
13332
     access-specifier : member-specification [opt]  */
13333
 
13334
static void
13335
cp_parser_member_specification_opt (cp_parser* parser)
13336
{
13337
  while (true)
13338
    {
13339
      cp_token *token;
13340
      enum rid keyword;
13341
 
13342
      /* Peek at the next token.  */
13343
      token = cp_lexer_peek_token (parser->lexer);
13344
      /* If it's a `}', or EOF then we've seen all the members.  */
13345
      if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
13346
        break;
13347
 
13348
      /* See if this token is a keyword.  */
13349
      keyword = token->keyword;
13350
      switch (keyword)
13351
        {
13352
        case RID_PUBLIC:
13353
        case RID_PROTECTED:
13354
        case RID_PRIVATE:
13355
          /* Consume the access-specifier.  */
13356
          cp_lexer_consume_token (parser->lexer);
13357
          /* Remember which access-specifier is active.  */
13358
          current_access_specifier = token->value;
13359
          /* Look for the `:'.  */
13360
          cp_parser_require (parser, CPP_COLON, "`:'");
13361
          break;
13362
 
13363
        default:
13364
          /* Accept #pragmas at class scope.  */
13365
          if (token->type == CPP_PRAGMA)
13366
            {
13367
              cp_lexer_handle_pragma (parser->lexer);
13368
              break;
13369
            }
13370
 
13371
          /* Otherwise, the next construction must be a
13372
             member-declaration.  */
13373
          cp_parser_member_declaration (parser);
13374
        }
13375
    }
13376
}
13377
 
13378
/* Parse a member-declaration.
13379
 
13380
   member-declaration:
13381
     decl-specifier-seq [opt] member-declarator-list [opt] ;
13382
     function-definition ; [opt]
13383
     :: [opt] nested-name-specifier template [opt] unqualified-id ;
13384
     using-declaration
13385
     template-declaration
13386
 
13387
   member-declarator-list:
13388
     member-declarator
13389
     member-declarator-list , member-declarator
13390
 
13391
   member-declarator:
13392
     declarator pure-specifier [opt]
13393
     declarator constant-initializer [opt]
13394
     identifier [opt] : constant-expression
13395
 
13396
   GNU Extensions:
13397
 
13398
   member-declaration:
13399
     __extension__ member-declaration
13400
 
13401
   member-declarator:
13402
     declarator attributes [opt] pure-specifier [opt]
13403
     declarator attributes [opt] constant-initializer [opt]
13404
     identifier [opt] attributes [opt] : constant-expression  */
13405
 
13406
static void
13407
cp_parser_member_declaration (cp_parser* parser)
13408
{
13409
  cp_decl_specifier_seq decl_specifiers;
13410
  tree prefix_attributes;
13411
  tree decl;
13412
  int declares_class_or_enum;
13413
  bool friend_p;
13414
  cp_token *token;
13415
  int saved_pedantic;
13416
 
13417
  /* Check for the `__extension__' keyword.  */
13418
  if (cp_parser_extension_opt (parser, &saved_pedantic))
13419
    {
13420
      /* Recurse.  */
13421
      cp_parser_member_declaration (parser);
13422
      /* Restore the old value of the PEDANTIC flag.  */
13423
      pedantic = saved_pedantic;
13424
 
13425
      return;
13426
    }
13427
 
13428
  /* Check for a template-declaration.  */
13429
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13430
    {
13431
      /* An explicit specialization here is an error condition, and we
13432
         expect the specialization handler to detect and report this.  */
13433
      if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13434
          && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13435
        cp_parser_explicit_specialization (parser);
13436
      else
13437
        cp_parser_template_declaration (parser, /*member_p=*/true);
13438
 
13439
      return;
13440
    }
13441
 
13442
  /* Check for a using-declaration.  */
13443
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13444
    {
13445
      /* Parse the using-declaration.  */
13446
      cp_parser_using_declaration (parser);
13447
 
13448
      return;
13449
    }
13450
 
13451
  /* Check for @defs.  */
13452
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13453
    {
13454
      tree ivar, member;
13455
      tree ivar_chains = cp_parser_objc_defs_expression (parser);
13456
      ivar = ivar_chains;
13457
      while (ivar)
13458
        {
13459
          member = ivar;
13460
          ivar = TREE_CHAIN (member);
13461
          TREE_CHAIN (member) = NULL_TREE;
13462
          finish_member_declaration (member);
13463
        }
13464
      return;
13465
    }
13466
 
13467
  /* Parse the decl-specifier-seq.  */
13468
  cp_parser_decl_specifier_seq (parser,
13469
                                CP_PARSER_FLAGS_OPTIONAL,
13470
                                &decl_specifiers,
13471
                                &declares_class_or_enum);
13472
  prefix_attributes = decl_specifiers.attributes;
13473
  decl_specifiers.attributes = NULL_TREE;
13474
  /* Check for an invalid type-name.  */
13475
  if (!decl_specifiers.type
13476
      && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13477
    return;
13478
  /* If there is no declarator, then the decl-specifier-seq should
13479
     specify a type.  */
13480
  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13481
    {
13482
      /* If there was no decl-specifier-seq, and the next token is a
13483
         `;', then we have something like:
13484
 
13485
           struct S { ; };
13486
 
13487
         [class.mem]
13488
 
13489
         Each member-declaration shall declare at least one member
13490
         name of the class.  */
13491
      if (!decl_specifiers.any_specifiers_p)
13492
        {
13493
          cp_token *token = cp_lexer_peek_token (parser->lexer);
13494
          if (pedantic && !token->in_system_header)
13495
            pedwarn ("%Hextra %<;%>", &token->location);
13496
        }
13497
      else
13498
        {
13499
          tree type;
13500
 
13501
          /* See if this declaration is a friend.  */
13502
          friend_p = cp_parser_friend_p (&decl_specifiers);
13503
          /* If there were decl-specifiers, check to see if there was
13504
             a class-declaration.  */
13505
          type = check_tag_decl (&decl_specifiers);
13506
          /* Nested classes have already been added to the class, but
13507
             a `friend' needs to be explicitly registered.  */
13508
          if (friend_p)
13509
            {
13510
              /* If the `friend' keyword was present, the friend must
13511
                 be introduced with a class-key.  */
13512
               if (!declares_class_or_enum)
13513
                 error ("a class-key must be used when declaring a friend");
13514
               /* In this case:
13515
 
13516
                    template <typename T> struct A {
13517
                      friend struct A<T>::B;
13518
                    };
13519
 
13520
                  A<T>::B will be represented by a TYPENAME_TYPE, and
13521
                  therefore not recognized by check_tag_decl.  */
13522
               if (!type
13523
                   && decl_specifiers.type
13524
                   && TYPE_P (decl_specifiers.type))
13525
                 type = decl_specifiers.type;
13526
               if (!type || !TYPE_P (type))
13527
                 error ("friend declaration does not name a class or "
13528
                        "function");
13529
               else
13530
                 make_friend_class (current_class_type, type,
13531
                                    /*complain=*/true);
13532
            }
13533
          /* If there is no TYPE, an error message will already have
13534
             been issued.  */
13535
          else if (!type || type == error_mark_node)
13536
            ;
13537
          /* An anonymous aggregate has to be handled specially; such
13538
             a declaration really declares a data member (with a
13539
             particular type), as opposed to a nested class.  */
13540
          else if (ANON_AGGR_TYPE_P (type))
13541
            {
13542
              /* Remove constructors and such from TYPE, now that we
13543
                 know it is an anonymous aggregate.  */
13544
              fixup_anonymous_aggr (type);
13545
              /* And make the corresponding data member.  */
13546
              decl = build_decl (FIELD_DECL, NULL_TREE, type);
13547
              /* Add it to the class.  */
13548
              finish_member_declaration (decl);
13549
            }
13550
          else
13551
            cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13552
        }
13553
    }
13554
  else
13555
    {
13556
      /* See if these declarations will be friends.  */
13557
      friend_p = cp_parser_friend_p (&decl_specifiers);
13558
 
13559
      /* Keep going until we hit the `;' at the end of the
13560
         declaration.  */
13561
      while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13562
        {
13563
          tree attributes = NULL_TREE;
13564
          tree first_attribute;
13565
 
13566
          /* Peek at the next token.  */
13567
          token = cp_lexer_peek_token (parser->lexer);
13568
 
13569
          /* Check for a bitfield declaration.  */
13570
          if (token->type == CPP_COLON
13571
              || (token->type == CPP_NAME
13572
                  && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13573
                  == CPP_COLON))
13574
            {
13575
              tree identifier;
13576
              tree width;
13577
 
13578
              /* Get the name of the bitfield.  Note that we cannot just
13579
                 check TOKEN here because it may have been invalidated by
13580
                 the call to cp_lexer_peek_nth_token above.  */
13581
              if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13582
                identifier = cp_parser_identifier (parser);
13583
              else
13584
                identifier = NULL_TREE;
13585
 
13586
              /* Consume the `:' token.  */
13587
              cp_lexer_consume_token (parser->lexer);
13588
              /* Get the width of the bitfield.  */
13589
              width
13590
                = cp_parser_constant_expression (parser,
13591
                                                 /*allow_non_constant=*/false,
13592
                                                 NULL);
13593
 
13594
              /* Look for attributes that apply to the bitfield.  */
13595
              attributes = cp_parser_attributes_opt (parser);
13596
              /* Remember which attributes are prefix attributes and
13597
                 which are not.  */
13598
              first_attribute = attributes;
13599
              /* Combine the attributes.  */
13600
              attributes = chainon (prefix_attributes, attributes);
13601
 
13602
              /* Create the bitfield declaration.  */
13603
              decl = grokbitfield (identifier
13604
                                   ? make_id_declarator (NULL_TREE,
13605
                                                         identifier,
13606
                                                         sfk_none)
13607
                                   : NULL,
13608
                                   &decl_specifiers,
13609
                                   width);
13610
              /* Apply the attributes.  */
13611
              cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13612
            }
13613
          else
13614
            {
13615
              cp_declarator *declarator;
13616
              tree initializer;
13617
              tree asm_specification;
13618
              int ctor_dtor_or_conv_p;
13619
 
13620
              /* Parse the declarator.  */
13621
              declarator
13622
                = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13623
                                        &ctor_dtor_or_conv_p,
13624
                                        /*parenthesized_p=*/NULL,
13625
                                        /*member_p=*/true);
13626
 
13627
              /* If something went wrong parsing the declarator, make sure
13628
                 that we at least consume some tokens.  */
13629
              if (declarator == cp_error_declarator)
13630
                {
13631
                  /* Skip to the end of the statement.  */
13632
                  cp_parser_skip_to_end_of_statement (parser);
13633
                  /* If the next token is not a semicolon, that is
13634
                     probably because we just skipped over the body of
13635
                     a function.  So, we consume a semicolon if
13636
                     present, but do not issue an error message if it
13637
                     is not present.  */
13638
                  if (cp_lexer_next_token_is (parser->lexer,
13639
                                              CPP_SEMICOLON))
13640
                    cp_lexer_consume_token (parser->lexer);
13641
                  return;
13642
                }
13643
 
13644
              if (declares_class_or_enum & 2)
13645
                cp_parser_check_for_definition_in_return_type
13646
                  (declarator, decl_specifiers.type);
13647
 
13648
              /* Look for an asm-specification.  */
13649
              asm_specification = cp_parser_asm_specification_opt (parser);
13650
              /* Look for attributes that apply to the declaration.  */
13651
              attributes = cp_parser_attributes_opt (parser);
13652
              /* Remember which attributes are prefix attributes and
13653
                 which are not.  */
13654
              first_attribute = attributes;
13655
              /* Combine the attributes.  */
13656
              attributes = chainon (prefix_attributes, attributes);
13657
 
13658
              /* If it's an `=', then we have a constant-initializer or a
13659
                 pure-specifier.  It is not correct to parse the
13660
                 initializer before registering the member declaration
13661
                 since the member declaration should be in scope while
13662
                 its initializer is processed.  However, the rest of the
13663
                 front end does not yet provide an interface that allows
13664
                 us to handle this correctly.  */
13665
              if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13666
                {
13667
                  /* In [class.mem]:
13668
 
13669
                     A pure-specifier shall be used only in the declaration of
13670
                     a virtual function.
13671
 
13672
                     A member-declarator can contain a constant-initializer
13673
                     only if it declares a static member of integral or
13674
                     enumeration type.
13675
 
13676
                     Therefore, if the DECLARATOR is for a function, we look
13677
                     for a pure-specifier; otherwise, we look for a
13678
                     constant-initializer.  When we call `grokfield', it will
13679
                     perform more stringent semantics checks.  */
13680
                  if (declarator->kind == cdk_function
13681
                      && declarator->declarator->kind == cdk_id)
13682
                    initializer = cp_parser_pure_specifier (parser);
13683
                  else
13684
                    /* Parse the initializer.  */
13685
                    initializer = cp_parser_constant_initializer (parser);
13686
                }
13687
              /* Otherwise, there is no initializer.  */
13688
              else
13689
                initializer = NULL_TREE;
13690
 
13691
              /* See if we are probably looking at a function
13692
                 definition.  We are certainly not looking at a
13693
                 member-declarator.  Calling `grokfield' has
13694
                 side-effects, so we must not do it unless we are sure
13695
                 that we are looking at a member-declarator.  */
13696
              if (cp_parser_token_starts_function_definition_p
13697
                  (cp_lexer_peek_token (parser->lexer)))
13698
                {
13699
                  /* The grammar does not allow a pure-specifier to be
13700
                     used when a member function is defined.  (It is
13701
                     possible that this fact is an oversight in the
13702
                     standard, since a pure function may be defined
13703
                     outside of the class-specifier.  */
13704
                  if (initializer)
13705
                    error ("pure-specifier on function-definition");
13706
                  decl = cp_parser_save_member_function_body (parser,
13707
                                                              &decl_specifiers,
13708
                                                              declarator,
13709
                                                              attributes);
13710
                  /* If the member was not a friend, declare it here.  */
13711
                  if (!friend_p)
13712
                    finish_member_declaration (decl);
13713
                  /* Peek at the next token.  */
13714
                  token = cp_lexer_peek_token (parser->lexer);
13715
                  /* If the next token is a semicolon, consume it.  */
13716
                  if (token->type == CPP_SEMICOLON)
13717
                    cp_lexer_consume_token (parser->lexer);
13718
                  return;
13719
                }
13720
              else
13721
                /* Create the declaration.  */
13722
                decl = grokfield (declarator, &decl_specifiers,
13723
                                  initializer, /*init_const_expr_p=*/true,
13724
                                  asm_specification,
13725
                                  attributes);
13726
            }
13727
 
13728
          /* Reset PREFIX_ATTRIBUTES.  */
13729
          while (attributes && TREE_CHAIN (attributes) != first_attribute)
13730
            attributes = TREE_CHAIN (attributes);
13731
          if (attributes)
13732
            TREE_CHAIN (attributes) = NULL_TREE;
13733
 
13734
          /* If there is any qualification still in effect, clear it
13735
             now; we will be starting fresh with the next declarator.  */
13736
          parser->scope = NULL_TREE;
13737
          parser->qualifying_scope = NULL_TREE;
13738
          parser->object_scope = NULL_TREE;
13739
          /* If it's a `,', then there are more declarators.  */
13740
          if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13741
            cp_lexer_consume_token (parser->lexer);
13742
          /* If the next token isn't a `;', then we have a parse error.  */
13743
          else if (cp_lexer_next_token_is_not (parser->lexer,
13744
                                               CPP_SEMICOLON))
13745
            {
13746
              cp_parser_error (parser, "expected %<;%>");
13747
              /* Skip tokens until we find a `;'.  */
13748
              cp_parser_skip_to_end_of_statement (parser);
13749
 
13750
              break;
13751
            }
13752
 
13753
          if (decl)
13754
            {
13755
              /* Add DECL to the list of members.  */
13756
              if (!friend_p)
13757
                finish_member_declaration (decl);
13758
 
13759
              if (TREE_CODE (decl) == FUNCTION_DECL)
13760
                cp_parser_save_default_args (parser, decl);
13761
            }
13762
        }
13763
    }
13764
 
13765
  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13766
}
13767
 
13768
/* Parse a pure-specifier.
13769
 
13770
   pure-specifier:
13771
     = 0
13772
 
13773
   Returns INTEGER_ZERO_NODE if a pure specifier is found.
13774
   Otherwise, ERROR_MARK_NODE is returned.  */
13775
 
13776
static tree
13777
cp_parser_pure_specifier (cp_parser* parser)
13778
{
13779
  cp_token *token;
13780
 
13781
  /* Look for the `=' token.  */
13782
  if (!cp_parser_require (parser, CPP_EQ, "`='"))
13783
    return error_mark_node;
13784
  /* Look for the `0' token.  */
13785
  token = cp_lexer_consume_token (parser->lexer);
13786
  if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13787
    {
13788
      cp_parser_error (parser,
13789
                       "invalid pure specifier (only `= 0' is allowed)");
13790
      cp_parser_skip_to_end_of_statement (parser);
13791
      return error_mark_node;
13792
    }
13793
 
13794
  /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13795
     We need to get information from the lexer about how the number
13796
     was spelled in order to fix this problem.  */
13797
  return integer_zero_node;
13798
}
13799
 
13800
/* Parse a constant-initializer.
13801
 
13802
   constant-initializer:
13803
     = constant-expression
13804
 
13805
   Returns a representation of the constant-expression.  */
13806
 
13807
static tree
13808
cp_parser_constant_initializer (cp_parser* parser)
13809
{
13810
  /* Look for the `=' token.  */
13811
  if (!cp_parser_require (parser, CPP_EQ, "`='"))
13812
    return error_mark_node;
13813
 
13814
  /* It is invalid to write:
13815
 
13816
       struct S { static const int i = { 7 }; };
13817
 
13818
     */
13819
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13820
    {
13821
      cp_parser_error (parser,
13822
                       "a brace-enclosed initializer is not allowed here");
13823
      /* Consume the opening brace.  */
13824
      cp_lexer_consume_token (parser->lexer);
13825
      /* Skip the initializer.  */
13826
      cp_parser_skip_to_closing_brace (parser);
13827
      /* Look for the trailing `}'.  */
13828
      cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13829
 
13830
      return error_mark_node;
13831
    }
13832
 
13833
  return cp_parser_constant_expression (parser,
13834
                                        /*allow_non_constant=*/false,
13835
                                        NULL);
13836
}
13837
 
13838
/* Derived classes [gram.class.derived] */
13839
 
13840
/* Parse a base-clause.
13841
 
13842
   base-clause:
13843
     : base-specifier-list
13844
 
13845
   base-specifier-list:
13846
     base-specifier
13847
     base-specifier-list , base-specifier
13848
 
13849
   Returns a TREE_LIST representing the base-classes, in the order in
13850
   which they were declared.  The representation of each node is as
13851
   described by cp_parser_base_specifier.
13852
 
13853
   In the case that no bases are specified, this function will return
13854
   NULL_TREE, not ERROR_MARK_NODE.  */
13855
 
13856
static tree
13857
cp_parser_base_clause (cp_parser* parser)
13858
{
13859
  tree bases = NULL_TREE;
13860
 
13861
  /* Look for the `:' that begins the list.  */
13862
  cp_parser_require (parser, CPP_COLON, "`:'");
13863
 
13864
  /* Scan the base-specifier-list.  */
13865
  while (true)
13866
    {
13867
      cp_token *token;
13868
      tree base;
13869
 
13870
      /* Look for the base-specifier.  */
13871
      base = cp_parser_base_specifier (parser);
13872
      /* Add BASE to the front of the list.  */
13873
      if (base != error_mark_node)
13874
        {
13875
          TREE_CHAIN (base) = bases;
13876
          bases = base;
13877
        }
13878
      /* Peek at the next token.  */
13879
      token = cp_lexer_peek_token (parser->lexer);
13880
      /* If it's not a comma, then the list is complete.  */
13881
      if (token->type != CPP_COMMA)
13882
        break;
13883
      /* Consume the `,'.  */
13884
      cp_lexer_consume_token (parser->lexer);
13885
    }
13886
 
13887
  /* PARSER->SCOPE may still be non-NULL at this point, if the last
13888
     base class had a qualified name.  However, the next name that
13889
     appears is certainly not qualified.  */
13890
  parser->scope = NULL_TREE;
13891
  parser->qualifying_scope = NULL_TREE;
13892
  parser->object_scope = NULL_TREE;
13893
 
13894
  return nreverse (bases);
13895
}
13896
 
13897
/* Parse a base-specifier.
13898
 
13899
   base-specifier:
13900
     :: [opt] nested-name-specifier [opt] class-name
13901
     virtual access-specifier [opt] :: [opt] nested-name-specifier
13902
       [opt] class-name
13903
     access-specifier virtual [opt] :: [opt] nested-name-specifier
13904
       [opt] class-name
13905
 
13906
   Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13907
   ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13908
   indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13909
   (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13910
 
13911
static tree
13912
cp_parser_base_specifier (cp_parser* parser)
13913
{
13914
  cp_token *token;
13915
  bool done = false;
13916
  bool virtual_p = false;
13917
  bool duplicate_virtual_error_issued_p = false;
13918
  bool duplicate_access_error_issued_p = false;
13919
  bool class_scope_p, template_p;
13920
  tree access = access_default_node;
13921
  tree type;
13922
 
13923
  /* Process the optional `virtual' and `access-specifier'.  */
13924
  while (!done)
13925
    {
13926
      /* Peek at the next token.  */
13927
      token = cp_lexer_peek_token (parser->lexer);
13928
      /* Process `virtual'.  */
13929
      switch (token->keyword)
13930
        {
13931
        case RID_VIRTUAL:
13932
          /* If `virtual' appears more than once, issue an error.  */
13933
          if (virtual_p && !duplicate_virtual_error_issued_p)
13934
            {
13935
              cp_parser_error (parser,
13936
                               "%<virtual%> specified more than once in base-specified");
13937
              duplicate_virtual_error_issued_p = true;
13938
            }
13939
 
13940
          virtual_p = true;
13941
 
13942
          /* Consume the `virtual' token.  */
13943
          cp_lexer_consume_token (parser->lexer);
13944
 
13945
          break;
13946
 
13947
        case RID_PUBLIC:
13948
        case RID_PROTECTED:
13949
        case RID_PRIVATE:
13950
          /* If more than one access specifier appears, issue an
13951
             error.  */
13952
          if (access != access_default_node
13953
              && !duplicate_access_error_issued_p)
13954
            {
13955
              cp_parser_error (parser,
13956
                               "more than one access specifier in base-specified");
13957
              duplicate_access_error_issued_p = true;
13958
            }
13959
 
13960
          access = ridpointers[(int) token->keyword];
13961
 
13962
          /* Consume the access-specifier.  */
13963
          cp_lexer_consume_token (parser->lexer);
13964
 
13965
          break;
13966
 
13967
        default:
13968
          done = true;
13969
          break;
13970
        }
13971
    }
13972
  /* It is not uncommon to see programs mechanically, erroneously, use
13973
     the 'typename' keyword to denote (dependent) qualified types
13974
     as base classes.  */
13975
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13976
    {
13977
      if (!processing_template_decl)
13978
        error ("keyword %<typename%> not allowed outside of templates");
13979
      else
13980
        error ("keyword %<typename%> not allowed in this context "
13981
               "(the base class is implicitly a type)");
13982
      cp_lexer_consume_token (parser->lexer);
13983
    }
13984
 
13985
  /* Look for the optional `::' operator.  */
13986
  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13987
  /* Look for the nested-name-specifier.  The simplest way to
13988
     implement:
13989
 
13990
       [temp.res]
13991
 
13992
       The keyword `typename' is not permitted in a base-specifier or
13993
       mem-initializer; in these contexts a qualified name that
13994
       depends on a template-parameter is implicitly assumed to be a
13995
       type name.
13996
 
13997
     is to pretend that we have seen the `typename' keyword at this
13998
     point.  */
13999
  cp_parser_nested_name_specifier_opt (parser,
14000
                                       /*typename_keyword_p=*/true,
14001
                                       /*check_dependency_p=*/true,
14002
                                       typename_type,
14003
                                       /*is_declaration=*/true);
14004
  /* If the base class is given by a qualified name, assume that names
14005
     we see are type names or templates, as appropriate.  */
14006
  class_scope_p = (parser->scope && TYPE_P (parser->scope));
14007
  template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14008
 
14009
  /* Finally, look for the class-name.  */
14010
  type = cp_parser_class_name (parser,
14011
                               class_scope_p,
14012
                               template_p,
14013
                               typename_type,
14014
                               /*check_dependency_p=*/true,
14015
                               /*class_head_p=*/false,
14016
                               /*is_declaration=*/true);
14017
 
14018
  if (type == error_mark_node)
14019
    return error_mark_node;
14020
 
14021
  return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14022
}
14023
 
14024
/* Exception handling [gram.exception] */
14025
 
14026
/* Parse an (optional) exception-specification.
14027
 
14028
   exception-specification:
14029
     throw ( type-id-list [opt] )
14030
 
14031
   Returns a TREE_LIST representing the exception-specification.  The
14032
   TREE_VALUE of each node is a type.  */
14033
 
14034
static tree
14035
cp_parser_exception_specification_opt (cp_parser* parser)
14036
{
14037
  cp_token *token;
14038
  tree type_id_list;
14039
 
14040
  /* Peek at the next token.  */
14041
  token = cp_lexer_peek_token (parser->lexer);
14042
  /* If it's not `throw', then there's no exception-specification.  */
14043
  if (!cp_parser_is_keyword (token, RID_THROW))
14044
    return NULL_TREE;
14045
 
14046
  /* Consume the `throw'.  */
14047
  cp_lexer_consume_token (parser->lexer);
14048
 
14049
  /* Look for the `('.  */
14050
  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14051
 
14052
  /* Peek at the next token.  */
14053
  token = cp_lexer_peek_token (parser->lexer);
14054
  /* If it's not a `)', then there is a type-id-list.  */
14055
  if (token->type != CPP_CLOSE_PAREN)
14056
    {
14057
      const char *saved_message;
14058
 
14059
      /* Types may not be defined in an exception-specification.  */
14060
      saved_message = parser->type_definition_forbidden_message;
14061
      parser->type_definition_forbidden_message
14062
        = "types may not be defined in an exception-specification";
14063
      /* Parse the type-id-list.  */
14064
      type_id_list = cp_parser_type_id_list (parser);
14065
      /* Restore the saved message.  */
14066
      parser->type_definition_forbidden_message = saved_message;
14067
    }
14068
  else
14069
    type_id_list = empty_except_spec;
14070
 
14071
  /* Look for the `)'.  */
14072
  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14073
 
14074
  return type_id_list;
14075
}
14076
 
14077
/* Parse an (optional) type-id-list.
14078
 
14079
   type-id-list:
14080
     type-id
14081
     type-id-list , type-id
14082
 
14083
   Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
14084
   in the order that the types were presented.  */
14085
 
14086
static tree
14087
cp_parser_type_id_list (cp_parser* parser)
14088
{
14089
  tree types = NULL_TREE;
14090
 
14091
  while (true)
14092
    {
14093
      cp_token *token;
14094
      tree type;
14095
 
14096
      /* Get the next type-id.  */
14097
      type = cp_parser_type_id (parser);
14098
      /* Add it to the list.  */
14099
      types = add_exception_specifier (types, type, /*complain=*/1);
14100
      /* Peek at the next token.  */
14101
      token = cp_lexer_peek_token (parser->lexer);
14102
      /* If it is not a `,', we are done.  */
14103
      if (token->type != CPP_COMMA)
14104
        break;
14105
      /* Consume the `,'.  */
14106
      cp_lexer_consume_token (parser->lexer);
14107
    }
14108
 
14109
  return nreverse (types);
14110
}
14111
 
14112
/* Parse a try-block.
14113
 
14114
   try-block:
14115
     try compound-statement handler-seq  */
14116
 
14117
static tree
14118
cp_parser_try_block (cp_parser* parser)
14119
{
14120
  tree try_block;
14121
 
14122
  cp_parser_require_keyword (parser, RID_TRY, "`try'");
14123
  try_block = begin_try_block ();
14124
  cp_parser_compound_statement (parser, NULL, true);
14125
  finish_try_block (try_block);
14126
  cp_parser_handler_seq (parser);
14127
  finish_handler_sequence (try_block);
14128
 
14129
  return try_block;
14130
}
14131
 
14132
/* Parse a function-try-block.
14133
 
14134
   function-try-block:
14135
     try ctor-initializer [opt] function-body handler-seq  */
14136
 
14137
static bool
14138
cp_parser_function_try_block (cp_parser* parser)
14139
{
14140
  tree try_block;
14141
  bool ctor_initializer_p;
14142
 
14143
  /* Look for the `try' keyword.  */
14144
  if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14145
    return false;
14146
  /* Let the rest of the front-end know where we are.  */
14147
  try_block = begin_function_try_block ();
14148
  /* Parse the function-body.  */
14149
  ctor_initializer_p
14150
    = cp_parser_ctor_initializer_opt_and_function_body (parser);
14151
  /* We're done with the `try' part.  */
14152
  finish_function_try_block (try_block);
14153
  /* Parse the handlers.  */
14154
  cp_parser_handler_seq (parser);
14155
  /* We're done with the handlers.  */
14156
  finish_function_handler_sequence (try_block);
14157
 
14158
  return ctor_initializer_p;
14159
}
14160
 
14161
/* Parse a handler-seq.
14162
 
14163
   handler-seq:
14164
     handler handler-seq [opt]  */
14165
 
14166
static void
14167
cp_parser_handler_seq (cp_parser* parser)
14168
{
14169
  while (true)
14170
    {
14171
      cp_token *token;
14172
 
14173
      /* Parse the handler.  */
14174
      cp_parser_handler (parser);
14175
      /* Peek at the next token.  */
14176
      token = cp_lexer_peek_token (parser->lexer);
14177
      /* If it's not `catch' then there are no more handlers.  */
14178
      if (!cp_parser_is_keyword (token, RID_CATCH))
14179
        break;
14180
    }
14181
}
14182
 
14183
/* Parse a handler.
14184
 
14185
   handler:
14186
     catch ( exception-declaration ) compound-statement  */
14187
 
14188
static void
14189
cp_parser_handler (cp_parser* parser)
14190
{
14191
  tree handler;
14192
  tree declaration;
14193
 
14194
  cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14195
  handler = begin_handler ();
14196
  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14197
  declaration = cp_parser_exception_declaration (parser);
14198
  finish_handler_parms (declaration, handler);
14199
  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14200
  cp_parser_compound_statement (parser, NULL, false);
14201
  finish_handler (handler);
14202
}
14203
 
14204
/* Parse an exception-declaration.
14205
 
14206
   exception-declaration:
14207
     type-specifier-seq declarator
14208
     type-specifier-seq abstract-declarator
14209
     type-specifier-seq
14210
     ...
14211
 
14212
   Returns a VAR_DECL for the declaration, or NULL_TREE if the
14213
   ellipsis variant is used.  */
14214
 
14215
static tree
14216
cp_parser_exception_declaration (cp_parser* parser)
14217
{
14218
  tree decl;
14219
  cp_decl_specifier_seq type_specifiers;
14220
  cp_declarator *declarator;
14221
  const char *saved_message;
14222
 
14223
  /* If it's an ellipsis, it's easy to handle.  */
14224
  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14225
    {
14226
      /* Consume the `...' token.  */
14227
      cp_lexer_consume_token (parser->lexer);
14228
      return NULL_TREE;
14229
    }
14230
 
14231
  /* Types may not be defined in exception-declarations.  */
14232
  saved_message = parser->type_definition_forbidden_message;
14233
  parser->type_definition_forbidden_message
14234
    = "types may not be defined in exception-declarations";
14235
 
14236
  /* Parse the type-specifier-seq.  */
14237
  cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14238
                                &type_specifiers);
14239
  /* If it's a `)', then there is no declarator.  */
14240
  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14241
    declarator = NULL;
14242
  else
14243
    declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14244
                                       /*ctor_dtor_or_conv_p=*/NULL,
14245
                                       /*parenthesized_p=*/NULL,
14246
                                       /*member_p=*/false);
14247
 
14248
  /* Restore the saved message.  */
14249
  parser->type_definition_forbidden_message = saved_message;
14250
 
14251
  if (type_specifiers.any_specifiers_p)
14252
    {
14253
      decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14254
      if (decl == NULL_TREE)
14255
        error ("invalid catch parameter");
14256
    }
14257
  else
14258
    decl = NULL_TREE;
14259
 
14260
  return decl;
14261
}
14262
 
14263
/* Parse a throw-expression.
14264
 
14265
   throw-expression:
14266
     throw assignment-expression [opt]
14267
 
14268
   Returns a THROW_EXPR representing the throw-expression.  */
14269
 
14270
static tree
14271
cp_parser_throw_expression (cp_parser* parser)
14272
{
14273
  tree expression;
14274
  cp_token* token;
14275
 
14276
  cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14277
  token = cp_lexer_peek_token (parser->lexer);
14278
  /* Figure out whether or not there is an assignment-expression
14279
     following the "throw" keyword.  */
14280
  if (token->type == CPP_COMMA
14281
      || token->type == CPP_SEMICOLON
14282
      || token->type == CPP_CLOSE_PAREN
14283
      || token->type == CPP_CLOSE_SQUARE
14284
      || token->type == CPP_CLOSE_BRACE
14285
      || token->type == CPP_COLON)
14286
    expression = NULL_TREE;
14287
  else
14288
    expression = cp_parser_assignment_expression (parser,
14289
                                                  /*cast_p=*/false);
14290
 
14291
  return build_throw (expression);
14292
}
14293
 
14294
/* GNU Extensions */
14295
 
14296
/* Parse an (optional) asm-specification.
14297
 
14298
   asm-specification:
14299
     asm ( string-literal )
14300
 
14301
   If the asm-specification is present, returns a STRING_CST
14302
   corresponding to the string-literal.  Otherwise, returns
14303
   NULL_TREE.  */
14304
 
14305
static tree
14306
cp_parser_asm_specification_opt (cp_parser* parser)
14307
{
14308
  cp_token *token;
14309
  tree asm_specification;
14310
 
14311
  /* Peek at the next token.  */
14312
  token = cp_lexer_peek_token (parser->lexer);
14313
  /* If the next token isn't the `asm' keyword, then there's no
14314
     asm-specification.  */
14315
  if (!cp_parser_is_keyword (token, RID_ASM))
14316
    return NULL_TREE;
14317
 
14318
  /* Consume the `asm' token.  */
14319
  cp_lexer_consume_token (parser->lexer);
14320
  /* Look for the `('.  */
14321
  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14322
 
14323
  /* Look for the string-literal.  */
14324
  asm_specification = cp_parser_string_literal (parser, false, false);
14325
 
14326
  /* Look for the `)'.  */
14327
  cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14328
 
14329
  return asm_specification;
14330
}
14331
 
14332
/* Parse an asm-operand-list.
14333
 
14334
   asm-operand-list:
14335
     asm-operand
14336
     asm-operand-list , asm-operand
14337
 
14338
   asm-operand:
14339
     string-literal ( expression )
14340
     [ string-literal ] string-literal ( expression )
14341
 
14342
   Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14343
   each node is the expression.  The TREE_PURPOSE is itself a
14344
   TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14345
   string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14346
   is a STRING_CST for the string literal before the parenthesis.  */
14347
 
14348
static tree
14349
cp_parser_asm_operand_list (cp_parser* parser)
14350
{
14351
  tree asm_operands = NULL_TREE;
14352
 
14353
  while (true)
14354
    {
14355
      tree string_literal;
14356
      tree expression;
14357
      tree name;
14358
 
14359
      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14360
        {
14361
          /* Consume the `[' token.  */
14362
          cp_lexer_consume_token (parser->lexer);
14363
          /* Read the operand name.  */
14364
          name = cp_parser_identifier (parser);
14365
          if (name != error_mark_node)
14366
            name = build_string (IDENTIFIER_LENGTH (name),
14367
                                 IDENTIFIER_POINTER (name));
14368
          /* Look for the closing `]'.  */
14369
          cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14370
        }
14371
      else
14372
        name = NULL_TREE;
14373
      /* Look for the string-literal.  */
14374
      string_literal = cp_parser_string_literal (parser, false, false);
14375
 
14376
      /* Look for the `('.  */
14377
      cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14378
      /* Parse the expression.  */
14379
      expression = cp_parser_expression (parser, /*cast_p=*/false);
14380
      /* Look for the `)'.  */
14381
      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14382
 
14383
      /* Add this operand to the list.  */
14384
      asm_operands = tree_cons (build_tree_list (name, string_literal),
14385
                                expression,
14386
                                asm_operands);
14387
      /* If the next token is not a `,', there are no more
14388
         operands.  */
14389
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14390
        break;
14391
      /* Consume the `,'.  */
14392
      cp_lexer_consume_token (parser->lexer);
14393
    }
14394
 
14395
  return nreverse (asm_operands);
14396
}
14397
 
14398
/* Parse an asm-clobber-list.
14399
 
14400
   asm-clobber-list:
14401
     string-literal
14402
     asm-clobber-list , string-literal
14403
 
14404
   Returns a TREE_LIST, indicating the clobbers in the order that they
14405
   appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14406
 
14407
static tree
14408
cp_parser_asm_clobber_list (cp_parser* parser)
14409
{
14410
  tree clobbers = NULL_TREE;
14411
 
14412
  while (true)
14413
    {
14414
      tree string_literal;
14415
 
14416
      /* Look for the string literal.  */
14417
      string_literal = cp_parser_string_literal (parser, false, false);
14418
      /* Add it to the list.  */
14419
      clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14420
      /* If the next token is not a `,', then the list is
14421
         complete.  */
14422
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14423
        break;
14424
      /* Consume the `,' token.  */
14425
      cp_lexer_consume_token (parser->lexer);
14426
    }
14427
 
14428
  return clobbers;
14429
}
14430
 
14431
/* Parse an (optional) series of attributes.
14432
 
14433
   attributes:
14434
     attributes attribute
14435
 
14436
   attribute:
14437
     __attribute__ (( attribute-list [opt] ))
14438
 
14439
   The return value is as for cp_parser_attribute_list.  */
14440
 
14441
static tree
14442
cp_parser_attributes_opt (cp_parser* parser)
14443
{
14444
  tree attributes = NULL_TREE;
14445
 
14446
  while (true)
14447
    {
14448
      cp_token *token;
14449
      tree attribute_list;
14450
 
14451
      /* Peek at the next token.  */
14452
      token = cp_lexer_peek_token (parser->lexer);
14453
      /* If it's not `__attribute__', then we're done.  */
14454
      if (token->keyword != RID_ATTRIBUTE)
14455
        break;
14456
 
14457
      /* Consume the `__attribute__' keyword.  */
14458
      cp_lexer_consume_token (parser->lexer);
14459
      /* Look for the two `(' tokens.  */
14460
      cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14461
      cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14462
 
14463
      /* Peek at the next token.  */
14464
      token = cp_lexer_peek_token (parser->lexer);
14465
      if (token->type != CPP_CLOSE_PAREN)
14466
        /* Parse the attribute-list.  */
14467
        attribute_list = cp_parser_attribute_list (parser);
14468
      else
14469
        /* If the next token is a `)', then there is no attribute
14470
           list.  */
14471
        attribute_list = NULL;
14472
 
14473
      /* Look for the two `)' tokens.  */
14474
      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14475
      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14476
 
14477
      /* Add these new attributes to the list.  */
14478
      attributes = chainon (attributes, attribute_list);
14479
    }
14480
 
14481
  return attributes;
14482
}
14483
 
14484
/* Parse an attribute-list.
14485
 
14486
   attribute-list:
14487
     attribute
14488
     attribute-list , attribute
14489
 
14490
   attribute:
14491
     identifier
14492
     identifier ( identifier )
14493
     identifier ( identifier , expression-list )
14494
     identifier ( expression-list )
14495
 
14496
   Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14497
   to an attribute.  The TREE_PURPOSE of each node is the identifier
14498
   indicating which attribute is in use.  The TREE_VALUE represents
14499
   the arguments, if any.  */
14500
 
14501
static tree
14502
cp_parser_attribute_list (cp_parser* parser)
14503
{
14504
  tree attribute_list = NULL_TREE;
14505
  bool save_translate_strings_p = parser->translate_strings_p;
14506
 
14507
  parser->translate_strings_p = false;
14508
  while (true)
14509
    {
14510
      cp_token *token;
14511
      tree identifier;
14512
      tree attribute;
14513
 
14514
      /* Look for the identifier.  We also allow keywords here; for
14515
         example `__attribute__ ((const))' is legal.  */
14516
      token = cp_lexer_peek_token (parser->lexer);
14517
      if (token->type == CPP_NAME
14518
          || token->type == CPP_KEYWORD)
14519
        {
14520
          /* Consume the token.  */
14521
          token = cp_lexer_consume_token (parser->lexer);
14522
 
14523
          /* Save away the identifier that indicates which attribute
14524
             this is.  */
14525
          identifier = token->value;
14526
          attribute = build_tree_list (identifier, NULL_TREE);
14527
 
14528
          /* Peek at the next token.  */
14529
          token = cp_lexer_peek_token (parser->lexer);
14530
          /* If it's an `(', then parse the attribute arguments.  */
14531
          if (token->type == CPP_OPEN_PAREN)
14532
            {
14533
              tree arguments;
14534
 
14535
              arguments = (cp_parser_parenthesized_expression_list
14536
                           (parser, true, /*cast_p=*/false,
14537
                            /*non_constant_p=*/NULL));
14538
              /* Save the identifier and arguments away.  */
14539
              TREE_VALUE (attribute) = arguments;
14540
            }
14541
 
14542
          /* Add this attribute to the list.  */
14543
          TREE_CHAIN (attribute) = attribute_list;
14544
          attribute_list = attribute;
14545
 
14546
          token = cp_lexer_peek_token (parser->lexer);
14547
        }
14548
      /* Now, look for more attributes.  If the next token isn't a
14549
         `,', we're done.  */
14550
      if (token->type != CPP_COMMA)
14551
        break;
14552
 
14553
      /* Consume the comma and keep going.  */
14554
      cp_lexer_consume_token (parser->lexer);
14555
    }
14556
  parser->translate_strings_p = save_translate_strings_p;
14557
 
14558
  /* We built up the list in reverse order.  */
14559
  return nreverse (attribute_list);
14560
}
14561
 
14562
/* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14563
   present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14564
   current value of the PEDANTIC flag, regardless of whether or not
14565
   the `__extension__' keyword is present.  The caller is responsible
14566
   for restoring the value of the PEDANTIC flag.  */
14567
 
14568
static bool
14569
cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14570
{
14571
  /* Save the old value of the PEDANTIC flag.  */
14572
  *saved_pedantic = pedantic;
14573
 
14574
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14575
    {
14576
      /* Consume the `__extension__' token.  */
14577
      cp_lexer_consume_token (parser->lexer);
14578
      /* We're not being pedantic while the `__extension__' keyword is
14579
         in effect.  */
14580
      pedantic = 0;
14581
 
14582
      return true;
14583
    }
14584
 
14585
  return false;
14586
}
14587
 
14588
/* Parse a label declaration.
14589
 
14590
   label-declaration:
14591
     __label__ label-declarator-seq ;
14592
 
14593
   label-declarator-seq:
14594
     identifier , label-declarator-seq
14595
     identifier  */
14596
 
14597
static void
14598
cp_parser_label_declaration (cp_parser* parser)
14599
{
14600
  /* Look for the `__label__' keyword.  */
14601
  cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14602
 
14603
  while (true)
14604
    {
14605
      tree identifier;
14606
 
14607
      /* Look for an identifier.  */
14608
      identifier = cp_parser_identifier (parser);
14609
      /* If we failed, stop.  */
14610
      if (identifier == error_mark_node)
14611
        break;
14612
      /* Declare it as a label.  */
14613
      finish_label_decl (identifier);
14614
      /* If the next token is a `;', stop.  */
14615
      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14616
        break;
14617
      /* Look for the `,' separating the label declarations.  */
14618
      cp_parser_require (parser, CPP_COMMA, "`,'");
14619
    }
14620
 
14621
  /* Look for the final `;'.  */
14622
  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14623
}
14624
 
14625
/* Support Functions */
14626
 
14627
/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14628
   NAME should have one of the representations used for an
14629
   id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14630
   is returned.  If PARSER->SCOPE is a dependent type, then a
14631
   SCOPE_REF is returned.
14632
 
14633
   If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14634
   returned; the name was already resolved when the TEMPLATE_ID_EXPR
14635
   was formed.  Abstractly, such entities should not be passed to this
14636
   function, because they do not need to be looked up, but it is
14637
   simpler to check for this special case here, rather than at the
14638
   call-sites.
14639
 
14640
   In cases not explicitly covered above, this function returns a
14641
   DECL, OVERLOAD, or baselink representing the result of the lookup.
14642
   If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14643
   is returned.
14644
 
14645
   If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14646
   (e.g., "struct") that was used.  In that case bindings that do not
14647
   refer to types are ignored.
14648
 
14649
   If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14650
   ignored.
14651
 
14652
   If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14653
   are ignored.
14654
 
14655
   If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14656
   types.
14657
 
14658
   If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14659
   TREE_LIST of candiates if name-lookup results in an ambiguity, and
14660
   NULL_TREE otherwise.  */
14661
 
14662
static tree
14663
cp_parser_lookup_name (cp_parser *parser, tree name,
14664
                       enum tag_types tag_type,
14665
                       bool is_template,
14666
                       bool is_namespace,
14667
                       bool check_dependency,
14668
                       tree *ambiguous_decls)
14669
{
14670
  int flags = 0;
14671
  tree decl;
14672
  tree object_type = parser->context->object_type;
14673
 
14674
  if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14675
    flags |= LOOKUP_COMPLAIN;
14676
 
14677
  /* Assume that the lookup will be unambiguous.  */
14678
  if (ambiguous_decls)
14679
    *ambiguous_decls = NULL_TREE;
14680
 
14681
  /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14682
     no longer valid.  Note that if we are parsing tentatively, and
14683
     the parse fails, OBJECT_TYPE will be automatically restored.  */
14684
  parser->context->object_type = NULL_TREE;
14685
 
14686
  if (name == error_mark_node)
14687
    return error_mark_node;
14688
 
14689
  /* A template-id has already been resolved; there is no lookup to
14690
     do.  */
14691
  if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14692
    return name;
14693
  if (BASELINK_P (name))
14694
    {
14695
      gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14696
                  == TEMPLATE_ID_EXPR);
14697
      return name;
14698
    }
14699
 
14700
  /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14701
     it should already have been checked to make sure that the name
14702
     used matches the type being destroyed.  */
14703
  if (TREE_CODE (name) == BIT_NOT_EXPR)
14704
    {
14705
      tree type;
14706
 
14707
      /* Figure out to which type this destructor applies.  */
14708
      if (parser->scope)
14709
        type = parser->scope;
14710
      else if (object_type)
14711
        type = object_type;
14712
      else
14713
        type = current_class_type;
14714
      /* If that's not a class type, there is no destructor.  */
14715
      if (!type || !CLASS_TYPE_P (type))
14716
        return error_mark_node;
14717
      if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14718
        lazily_declare_fn (sfk_destructor, type);
14719
      if (!CLASSTYPE_DESTRUCTORS (type))
14720
          return error_mark_node;
14721
      /* If it was a class type, return the destructor.  */
14722
      return CLASSTYPE_DESTRUCTORS (type);
14723
    }
14724
 
14725
  /* By this point, the NAME should be an ordinary identifier.  If
14726
     the id-expression was a qualified name, the qualifying scope is
14727
     stored in PARSER->SCOPE at this point.  */
14728
  gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14729
 
14730
  /* Perform the lookup.  */
14731
  if (parser->scope)
14732
    {
14733
      bool dependent_p;
14734
 
14735
      if (parser->scope == error_mark_node)
14736
        return error_mark_node;
14737
 
14738
      /* If the SCOPE is dependent, the lookup must be deferred until
14739
         the template is instantiated -- unless we are explicitly
14740
         looking up names in uninstantiated templates.  Even then, we
14741
         cannot look up the name if the scope is not a class type; it
14742
         might, for example, be a template type parameter.  */
14743
      dependent_p = (TYPE_P (parser->scope)
14744
                     && !(parser->in_declarator_p
14745
                          && currently_open_class (parser->scope))
14746
                     && dependent_type_p (parser->scope));
14747
      if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14748
           && dependent_p)
14749
        {
14750
          if (tag_type)
14751
            {
14752
              tree type;
14753
 
14754
              /* The resolution to Core Issue 180 says that `struct
14755
                 A::B' should be considered a type-name, even if `A'
14756
                 is dependent.  */
14757
              type = make_typename_type (parser->scope, name, tag_type,
14758
                                         /*complain=*/1);
14759
              decl = TYPE_NAME (type);
14760
            }
14761
          else if (is_template
14762
                   && (cp_parser_next_token_ends_template_argument_p (parser)
14763
                       || cp_lexer_next_token_is (parser->lexer,
14764
                                                  CPP_CLOSE_PAREN)))
14765
            decl = make_unbound_class_template (parser->scope,
14766
                                                name, NULL_TREE,
14767
                                                /*complain=*/1);
14768
          else
14769
            decl = build_qualified_name (/*type=*/NULL_TREE,
14770
                                         parser->scope, name,
14771
                                         is_template);
14772
        }
14773
      else
14774
        {
14775
          tree pushed_scope = NULL_TREE;
14776
 
14777
          /* If PARSER->SCOPE is a dependent type, then it must be a
14778
             class type, and we must not be checking dependencies;
14779
             otherwise, we would have processed this lookup above.  So
14780
             that PARSER->SCOPE is not considered a dependent base by
14781
             lookup_member, we must enter the scope here.  */
14782
          if (dependent_p)
14783
            pushed_scope = push_scope (parser->scope);
14784
          /* If the PARSER->SCOPE is a template specialization, it
14785
             may be instantiated during name lookup.  In that case,
14786
             errors may be issued.  Even if we rollback the current
14787
             tentative parse, those errors are valid.  */
14788
          decl = lookup_qualified_name (parser->scope, name,
14789
                                        tag_type != none_type,
14790
                                        /*complain=*/true);
14791
          if (pushed_scope)
14792
            pop_scope (pushed_scope);
14793
        }
14794
      parser->qualifying_scope = parser->scope;
14795
      parser->object_scope = NULL_TREE;
14796
    }
14797
  else if (object_type)
14798
    {
14799
      tree object_decl = NULL_TREE;
14800
      /* Look up the name in the scope of the OBJECT_TYPE, unless the
14801
         OBJECT_TYPE is not a class.  */
14802
      if (CLASS_TYPE_P (object_type))
14803
        /* If the OBJECT_TYPE is a template specialization, it may
14804
           be instantiated during name lookup.  In that case, errors
14805
           may be issued.  Even if we rollback the current tentative
14806
           parse, those errors are valid.  */
14807
        object_decl = lookup_member (object_type,
14808
                                     name,
14809
                                     /*protect=*/0,
14810
                                     tag_type != none_type);
14811
      /* Look it up in the enclosing context, too.  */
14812
      decl = lookup_name_real (name, tag_type != none_type,
14813
                               /*nonclass=*/0,
14814
                               /*block_p=*/true, is_namespace, flags);
14815
      parser->object_scope = object_type;
14816
      parser->qualifying_scope = NULL_TREE;
14817
      if (object_decl)
14818
        decl = object_decl;
14819
    }
14820
  else
14821
    {
14822
      decl = lookup_name_real (name, tag_type != none_type,
14823
                               /*nonclass=*/0,
14824
                               /*block_p=*/true, is_namespace, flags);
14825
      parser->qualifying_scope = NULL_TREE;
14826
      parser->object_scope = NULL_TREE;
14827
    }
14828
 
14829
  /* If the lookup failed, let our caller know.  */
14830
  if (!decl || decl == error_mark_node)
14831
    return error_mark_node;
14832
 
14833
  /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14834
  if (TREE_CODE (decl) == TREE_LIST)
14835
    {
14836
      if (ambiguous_decls)
14837
        *ambiguous_decls = decl;
14838
      /* The error message we have to print is too complicated for
14839
         cp_parser_error, so we incorporate its actions directly.  */
14840
      if (!cp_parser_simulate_error (parser))
14841
        {
14842
          error ("reference to %qD is ambiguous", name);
14843
          print_candidates (decl);
14844
        }
14845
      return error_mark_node;
14846
    }
14847
 
14848
  gcc_assert (DECL_P (decl)
14849
              || TREE_CODE (decl) == OVERLOAD
14850
              || TREE_CODE (decl) == SCOPE_REF
14851
              || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14852
              || BASELINK_P (decl));
14853
 
14854
  /* If we have resolved the name of a member declaration, check to
14855
     see if the declaration is accessible.  When the name resolves to
14856
     set of overloaded functions, accessibility is checked when
14857
     overload resolution is done.
14858
 
14859
     During an explicit instantiation, access is not checked at all,
14860
     as per [temp.explicit].  */
14861
  if (DECL_P (decl))
14862
    check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14863
 
14864
  return decl;
14865
}
14866
 
14867
/* Like cp_parser_lookup_name, but for use in the typical case where
14868
   CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14869
   IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14870
 
14871
static tree
14872
cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14873
{
14874
  return cp_parser_lookup_name (parser, name,
14875
                                none_type,
14876
                                /*is_template=*/false,
14877
                                /*is_namespace=*/false,
14878
                                /*check_dependency=*/true,
14879
                                /*ambiguous_decls=*/NULL);
14880
}
14881
 
14882
/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14883
   the current context, return the TYPE_DECL.  If TAG_NAME_P is
14884
   true, the DECL indicates the class being defined in a class-head,
14885
   or declared in an elaborated-type-specifier.
14886
 
14887
   Otherwise, return DECL.  */
14888
 
14889
static tree
14890
cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14891
{
14892
  /* If the TEMPLATE_DECL is being declared as part of a class-head,
14893
     the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14894
 
14895
       struct A {
14896
         template <typename T> struct B;
14897
       };
14898
 
14899
       template <typename T> struct A::B {};
14900
 
14901
     Similarly, in an elaborated-type-specifier:
14902
 
14903
       namespace N { struct X{}; }
14904
 
14905
       struct A {
14906
         template <typename T> friend struct N::X;
14907
       };
14908
 
14909
     However, if the DECL refers to a class type, and we are in
14910
     the scope of the class, then the name lookup automatically
14911
     finds the TYPE_DECL created by build_self_reference rather
14912
     than a TEMPLATE_DECL.  For example, in:
14913
 
14914
       template <class T> struct S {
14915
         S s;
14916
       };
14917
 
14918
     there is no need to handle such case.  */
14919
 
14920
  if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14921
    return DECL_TEMPLATE_RESULT (decl);
14922
 
14923
  return decl;
14924
}
14925
 
14926
/* If too many, or too few, template-parameter lists apply to the
14927
   declarator, issue an error message.  Returns TRUE if all went well,
14928
   and FALSE otherwise.  */
14929
 
14930
static bool
14931
cp_parser_check_declarator_template_parameters (cp_parser* parser,
14932
                                                cp_declarator *declarator)
14933
{
14934
  unsigned num_templates;
14935
 
14936
  /* We haven't seen any classes that involve template parameters yet.  */
14937
  num_templates = 0;
14938
 
14939
  switch (declarator->kind)
14940
    {
14941
    case cdk_id:
14942
      if (declarator->u.id.qualifying_scope)
14943
        {
14944
          tree scope;
14945
          tree member;
14946
 
14947
          scope = declarator->u.id.qualifying_scope;
14948
          member = declarator->u.id.unqualified_name;
14949
 
14950
          while (scope && CLASS_TYPE_P (scope))
14951
            {
14952
              /* You're supposed to have one `template <...>'
14953
                 for every template class, but you don't need one
14954
                 for a full specialization.  For example:
14955
 
14956
                 template <class T> struct S{};
14957
                 template <> struct S<int> { void f(); };
14958
                 void S<int>::f () {}
14959
 
14960
                 is correct; there shouldn't be a `template <>' for
14961
                 the definition of `S<int>::f'.  */
14962
              if (CLASSTYPE_TEMPLATE_INFO (scope)
14963
                  && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14964
                      || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14965
                  && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14966
                ++num_templates;
14967
 
14968
              scope = TYPE_CONTEXT (scope);
14969
            }
14970
        }
14971
      else if (TREE_CODE (declarator->u.id.unqualified_name)
14972
               == TEMPLATE_ID_EXPR)
14973
        /* If the DECLARATOR has the form `X<y>' then it uses one
14974
           additional level of template parameters.  */
14975
        ++num_templates;
14976
 
14977
      return cp_parser_check_template_parameters (parser,
14978
                                                  num_templates);
14979
 
14980
    case cdk_function:
14981
    case cdk_array:
14982
    case cdk_pointer:
14983
    case cdk_reference:
14984
    case cdk_ptrmem:
14985
      return (cp_parser_check_declarator_template_parameters
14986
              (parser, declarator->declarator));
14987
 
14988
    case cdk_error:
14989
      return true;
14990
 
14991
    default:
14992
      gcc_unreachable ();
14993
    }
14994
  return false;
14995
}
14996
 
14997
/* NUM_TEMPLATES were used in the current declaration.  If that is
14998
   invalid, return FALSE and issue an error messages.  Otherwise,
14999
   return TRUE.  */
15000
 
15001
static bool
15002
cp_parser_check_template_parameters (cp_parser* parser,
15003
                                     unsigned num_templates)
15004
{
15005
  /* If there are more template classes than parameter lists, we have
15006
     something like:
15007
 
15008
       template <class T> void S<T>::R<T>::f ();  */
15009
  if (parser->num_template_parameter_lists < num_templates)
15010
    {
15011
      error ("too few template-parameter-lists");
15012
      return false;
15013
    }
15014
  /* If there are the same number of template classes and parameter
15015
     lists, that's OK.  */
15016
  if (parser->num_template_parameter_lists == num_templates)
15017
    return true;
15018
  /* If there are more, but only one more, then we are referring to a
15019
     member template.  That's OK too.  */
15020
  if (parser->num_template_parameter_lists == num_templates + 1)
15021
      return true;
15022
  /* Otherwise, there are too many template parameter lists.  We have
15023
     something like:
15024
 
15025
     template <class T> template <class U> void S::f();  */
15026
  error ("too many template-parameter-lists");
15027
  return false;
15028
}
15029
 
15030
/* Parse an optional `::' token indicating that the following name is
15031
   from the global namespace.  If so, PARSER->SCOPE is set to the
15032
   GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15033
   unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15034
   Returns the new value of PARSER->SCOPE, if the `::' token is
15035
   present, and NULL_TREE otherwise.  */
15036
 
15037
static tree
15038
cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15039
{
15040
  cp_token *token;
15041
 
15042
  /* Peek at the next token.  */
15043
  token = cp_lexer_peek_token (parser->lexer);
15044
  /* If we're looking at a `::' token then we're starting from the
15045
     global namespace, not our current location.  */
15046
  if (token->type == CPP_SCOPE)
15047
    {
15048
      /* Consume the `::' token.  */
15049
      cp_lexer_consume_token (parser->lexer);
15050
      /* Set the SCOPE so that we know where to start the lookup.  */
15051
      parser->scope = global_namespace;
15052
      parser->qualifying_scope = global_namespace;
15053
      parser->object_scope = NULL_TREE;
15054
 
15055
      return parser->scope;
15056
    }
15057
  else if (!current_scope_valid_p)
15058
    {
15059
      parser->scope = NULL_TREE;
15060
      parser->qualifying_scope = NULL_TREE;
15061
      parser->object_scope = NULL_TREE;
15062
    }
15063
 
15064
  return NULL_TREE;
15065
}
15066
 
15067
/* Returns TRUE if the upcoming token sequence is the start of a
15068
   constructor declarator.  If FRIEND_P is true, the declarator is
15069
   preceded by the `friend' specifier.  */
15070
 
15071
static bool
15072
cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15073
{
15074
  bool constructor_p;
15075
  tree type_decl = NULL_TREE;
15076
  bool nested_name_p;
15077
  cp_token *next_token;
15078
 
15079
  /* The common case is that this is not a constructor declarator, so
15080
     try to avoid doing lots of work if at all possible.  It's not
15081
     valid declare a constructor at function scope.  */
15082
  if (at_function_scope_p ())
15083
    return false;
15084
  /* And only certain tokens can begin a constructor declarator.  */
15085
  next_token = cp_lexer_peek_token (parser->lexer);
15086
  if (next_token->type != CPP_NAME
15087
      && next_token->type != CPP_SCOPE
15088
      && next_token->type != CPP_NESTED_NAME_SPECIFIER
15089
      && next_token->type != CPP_TEMPLATE_ID)
15090
    return false;
15091
 
15092
  /* Parse tentatively; we are going to roll back all of the tokens
15093
     consumed here.  */
15094
  cp_parser_parse_tentatively (parser);
15095
  /* Assume that we are looking at a constructor declarator.  */
15096
  constructor_p = true;
15097
 
15098
  /* Look for the optional `::' operator.  */
15099
  cp_parser_global_scope_opt (parser,
15100
                              /*current_scope_valid_p=*/false);
15101
  /* Look for the nested-name-specifier.  */
15102
  nested_name_p
15103
    = (cp_parser_nested_name_specifier_opt (parser,
15104
                                            /*typename_keyword_p=*/false,
15105
                                            /*check_dependency_p=*/false,
15106
                                            /*type_p=*/false,
15107
                                            /*is_declaration=*/false)
15108
       != NULL_TREE);
15109
  /* Outside of a class-specifier, there must be a
15110
     nested-name-specifier.  */
15111
  if (!nested_name_p &&
15112
      (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15113
       || friend_p))
15114
    constructor_p = false;
15115
  /* If we still think that this might be a constructor-declarator,
15116
     look for a class-name.  */
15117
  if (constructor_p)
15118
    {
15119
      /* If we have:
15120
 
15121
           template <typename T> struct S { S(); };
15122
           template <typename T> S<T>::S ();
15123
 
15124
         we must recognize that the nested `S' names a class.
15125
         Similarly, for:
15126
 
15127
           template <typename T> S<T>::S<T> ();
15128
 
15129
         we must recognize that the nested `S' names a template.  */
15130
      type_decl = cp_parser_class_name (parser,
15131
                                        /*typename_keyword_p=*/false,
15132
                                        /*template_keyword_p=*/false,
15133
                                        none_type,
15134
                                        /*check_dependency_p=*/false,
15135
                                        /*class_head_p=*/false,
15136
                                        /*is_declaration=*/false);
15137
      /* If there was no class-name, then this is not a constructor.  */
15138
      constructor_p = !cp_parser_error_occurred (parser);
15139
    }
15140
 
15141
  /* If we're still considering a constructor, we have to see a `(',
15142
     to begin the parameter-declaration-clause, followed by either a
15143
     `)', an `...', or a decl-specifier.  We need to check for a
15144
     type-specifier to avoid being fooled into thinking that:
15145
 
15146
       S::S (f) (int);
15147
 
15148
     is a constructor.  (It is actually a function named `f' that
15149
     takes one parameter (of type `int') and returns a value of type
15150
     `S::S'.  */
15151
  if (constructor_p
15152
      && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15153
    {
15154
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15155
          && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15156
          /* A parameter declaration begins with a decl-specifier,
15157
             which is either the "attribute" keyword, a storage class
15158
             specifier, or (usually) a type-specifier.  */
15159
          && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15160
          && !cp_parser_storage_class_specifier_opt (parser))
15161
        {
15162
          tree type;
15163
          tree pushed_scope = NULL_TREE;
15164
          unsigned saved_num_template_parameter_lists;
15165
 
15166
          /* Names appearing in the type-specifier should be looked up
15167
             in the scope of the class.  */
15168
          if (current_class_type)
15169
            type = NULL_TREE;
15170
          else
15171
            {
15172
              type = TREE_TYPE (type_decl);
15173
              if (TREE_CODE (type) == TYPENAME_TYPE)
15174
                {
15175
                  type = resolve_typename_type (type,
15176
                                                /*only_current_p=*/false);
15177
                  if (type == error_mark_node)
15178
                    {
15179
                      cp_parser_abort_tentative_parse (parser);
15180
                      return false;
15181
                    }
15182
                }
15183
              pushed_scope = push_scope (type);
15184
            }
15185
 
15186
          /* Inside the constructor parameter list, surrounding
15187
             template-parameter-lists do not apply.  */
15188
          saved_num_template_parameter_lists
15189
            = parser->num_template_parameter_lists;
15190
          parser->num_template_parameter_lists = 0;
15191
 
15192
          /* Look for the type-specifier.  */
15193
          cp_parser_type_specifier (parser,
15194
                                    CP_PARSER_FLAGS_NONE,
15195
                                    /*decl_specs=*/NULL,
15196
                                    /*is_declarator=*/true,
15197
                                    /*declares_class_or_enum=*/NULL,
15198
                                    /*is_cv_qualifier=*/NULL);
15199
 
15200
          parser->num_template_parameter_lists
15201
            = saved_num_template_parameter_lists;
15202
 
15203
          /* Leave the scope of the class.  */
15204
          if (pushed_scope)
15205
            pop_scope (pushed_scope);
15206
 
15207
          constructor_p = !cp_parser_error_occurred (parser);
15208
        }
15209
    }
15210
  else
15211
    constructor_p = false;
15212
  /* We did not really want to consume any tokens.  */
15213
  cp_parser_abort_tentative_parse (parser);
15214
 
15215
  return constructor_p;
15216
}
15217
 
15218
/* Parse the definition of the function given by the DECL_SPECIFIERS,
15219
   ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15220
   they must be performed once we are in the scope of the function.
15221
 
15222
   Returns the function defined.  */
15223
 
15224
static tree
15225
cp_parser_function_definition_from_specifiers_and_declarator
15226
  (cp_parser* parser,
15227
   cp_decl_specifier_seq *decl_specifiers,
15228
   tree attributes,
15229
   const cp_declarator *declarator)
15230
{
15231
  tree fn;
15232
  bool success_p;
15233
 
15234
  /* Begin the function-definition.  */
15235
  success_p = start_function (decl_specifiers, declarator, attributes);
15236
 
15237
  /* The things we're about to see are not directly qualified by any
15238
     template headers we've seen thus far.  */
15239
  reset_specialization ();
15240
 
15241
  /* If there were names looked up in the decl-specifier-seq that we
15242
     did not check, check them now.  We must wait until we are in the
15243
     scope of the function to perform the checks, since the function
15244
     might be a friend.  */
15245
  perform_deferred_access_checks ();
15246
 
15247
  if (!success_p)
15248
    {
15249
      /* Skip the entire function.  */
15250
      error ("invalid function declaration");
15251
      cp_parser_skip_to_end_of_block_or_statement (parser);
15252
      fn = error_mark_node;
15253
    }
15254
  else
15255
    fn = cp_parser_function_definition_after_declarator (parser,
15256
                                                         /*inline_p=*/false);
15257
 
15258
  return fn;
15259
}
15260
 
15261
/* Parse the part of a function-definition that follows the
15262
   declarator.  INLINE_P is TRUE iff this function is an inline
15263
   function defined with a class-specifier.
15264
 
15265
   Returns the function defined.  */
15266
 
15267
static tree
15268
cp_parser_function_definition_after_declarator (cp_parser* parser,
15269
                                                bool inline_p)
15270
{
15271
  tree fn;
15272
  bool ctor_initializer_p = false;
15273
  bool saved_in_unbraced_linkage_specification_p;
15274
  unsigned saved_num_template_parameter_lists;
15275
 
15276
  /* If the next token is `return', then the code may be trying to
15277
     make use of the "named return value" extension that G++ used to
15278
     support.  */
15279
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15280
    {
15281
      /* Consume the `return' keyword.  */
15282
      cp_lexer_consume_token (parser->lexer);
15283
      /* Look for the identifier that indicates what value is to be
15284
         returned.  */
15285
      cp_parser_identifier (parser);
15286
      /* Issue an error message.  */
15287
      error ("named return values are no longer supported");
15288
      /* Skip tokens until we reach the start of the function body.  */
15289
      while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
15290
             && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
15291
        cp_lexer_consume_token (parser->lexer);
15292
    }
15293
  /* The `extern' in `extern "C" void f () { ... }' does not apply to
15294
     anything declared inside `f'.  */
15295
  saved_in_unbraced_linkage_specification_p
15296
    = parser->in_unbraced_linkage_specification_p;
15297
  parser->in_unbraced_linkage_specification_p = false;
15298
  /* Inside the function, surrounding template-parameter-lists do not
15299
     apply.  */
15300
  saved_num_template_parameter_lists
15301
    = parser->num_template_parameter_lists;
15302
  parser->num_template_parameter_lists = 0;
15303
  /* If the next token is `try', then we are looking at a
15304
     function-try-block.  */
15305
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15306
    ctor_initializer_p = cp_parser_function_try_block (parser);
15307
  /* A function-try-block includes the function-body, so we only do
15308
     this next part if we're not processing a function-try-block.  */
15309
  else
15310
    ctor_initializer_p
15311
      = cp_parser_ctor_initializer_opt_and_function_body (parser);
15312
 
15313
  /* Finish the function.  */
15314
  fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15315
                        (inline_p ? 2 : 0));
15316
  /* Generate code for it, if necessary.  */
15317
  expand_or_defer_fn (fn);
15318
  /* Restore the saved values.  */
15319
  parser->in_unbraced_linkage_specification_p
15320
    = saved_in_unbraced_linkage_specification_p;
15321
  parser->num_template_parameter_lists
15322
    = saved_num_template_parameter_lists;
15323
 
15324
  return fn;
15325
}
15326
 
15327
/* Parse a template-declaration, assuming that the `export' (and
15328
   `extern') keywords, if present, has already been scanned.  MEMBER_P
15329
   is as for cp_parser_template_declaration.  */
15330
 
15331
static void
15332
cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15333
{
15334
  tree decl = NULL_TREE;
15335
  tree checks;
15336
  tree parameter_list;
15337
  bool friend_p = false;
15338
  bool need_lang_pop;
15339
 
15340
  /* Look for the `template' keyword.  */
15341
  if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15342
    return;
15343
 
15344
  /* And the `<'.  */
15345
  if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15346
    return;
15347
  /* [temp]
15348
 
15349
     A template ... shall not have C linkage.  */
15350
  if (current_lang_name == lang_name_c)
15351
    {
15352
      error ("template with C linkage");
15353
      /* Give it C++ linkage to avoid confusing other parts of the
15354
         front end.  */
15355
      push_lang_context (lang_name_cplusplus);
15356
      need_lang_pop = true;
15357
    }
15358
  else
15359
    need_lang_pop = false;
15360
 
15361
  /* We cannot perform access checks on the template parameter
15362
     declarations until we know what is being declared, just as we
15363
     cannot check the decl-specifier list.  */
15364
  push_deferring_access_checks (dk_deferred);
15365
 
15366
  /* If the next token is `>', then we have an invalid
15367
     specialization.  Rather than complain about an invalid template
15368
     parameter, issue an error message here.  */
15369
  if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15370
    {
15371
      cp_parser_error (parser, "invalid explicit specialization");
15372
      begin_specialization ();
15373
      parameter_list = NULL_TREE;
15374
    }
15375
  else
15376
    {
15377
      /* Parse the template parameters.  */
15378
      begin_template_parm_list ();
15379
      parameter_list = cp_parser_template_parameter_list (parser);
15380
      parameter_list = end_template_parm_list (parameter_list);
15381
    }
15382
 
15383
  /* Get the deferred access checks from the parameter list.  These
15384
     will be checked once we know what is being declared, as for a
15385
     member template the checks must be performed in the scope of the
15386
     class containing the member.  */
15387
  checks = get_deferred_access_checks ();
15388
 
15389
  /* Look for the `>'.  */
15390
  cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15391
  /* We just processed one more parameter list.  */
15392
  ++parser->num_template_parameter_lists;
15393
  /* If the next token is `template', there are more template
15394
     parameters.  */
15395
  if (cp_lexer_next_token_is_keyword (parser->lexer,
15396
                                      RID_TEMPLATE))
15397
    cp_parser_template_declaration_after_export (parser, member_p);
15398
  else
15399
    {
15400
      /* There are no access checks when parsing a template, as we do not
15401
         know if a specialization will be a friend.  */
15402
      push_deferring_access_checks (dk_no_check);
15403
      decl = cp_parser_single_declaration (parser,
15404
                                           checks,
15405
                                           member_p,
15406
                                           &friend_p);
15407
      pop_deferring_access_checks ();
15408
 
15409
      /* If this is a member template declaration, let the front
15410
         end know.  */
15411
      if (member_p && !friend_p && decl)
15412
        {
15413
          if (TREE_CODE (decl) == TYPE_DECL)
15414
            cp_parser_check_access_in_redeclaration (decl);
15415
 
15416
          decl = finish_member_template_decl (decl);
15417
        }
15418
      else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15419
        make_friend_class (current_class_type, TREE_TYPE (decl),
15420
                           /*complain=*/true);
15421
    }
15422
  /* We are done with the current parameter list.  */
15423
  --parser->num_template_parameter_lists;
15424
 
15425
  pop_deferring_access_checks ();
15426
 
15427
  /* Finish up.  */
15428
  finish_template_decl (parameter_list);
15429
 
15430
  /* Register member declarations.  */
15431
  if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15432
    finish_member_declaration (decl);
15433
  /* For the erroneous case of a template with C linkage, we pushed an
15434
     implicit C++ linkage scope; exit that scope now.  */
15435
  if (need_lang_pop)
15436
    pop_lang_context ();
15437
  /* If DECL is a function template, we must return to parse it later.
15438
     (Even though there is no definition, there might be default
15439
     arguments that need handling.)  */
15440
  if (member_p && decl
15441
      && (TREE_CODE (decl) == FUNCTION_DECL
15442
          || DECL_FUNCTION_TEMPLATE_P (decl)))
15443
    TREE_VALUE (parser->unparsed_functions_queues)
15444
      = tree_cons (NULL_TREE, decl,
15445
                   TREE_VALUE (parser->unparsed_functions_queues));
15446
}
15447
 
15448
/* Perform the deferred access checks from a template-parameter-list.
15449
   CHECKS is a TREE_LIST of access checks, as returned by
15450
   get_deferred_access_checks.  */
15451
 
15452
static void
15453
cp_parser_perform_template_parameter_access_checks (tree checks)
15454
{
15455
  ++processing_template_parmlist;
15456
  perform_access_checks (checks);
15457
  --processing_template_parmlist;
15458
}
15459
 
15460
/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15461
   `function-definition' sequence.  MEMBER_P is true, this declaration
15462
   appears in a class scope.
15463
 
15464
   Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15465
   *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15466
 
15467
static tree
15468
cp_parser_single_declaration (cp_parser* parser,
15469
                              tree checks,
15470
                              bool member_p,
15471
                              bool* friend_p)
15472
{
15473
  int declares_class_or_enum;
15474
  tree decl = NULL_TREE;
15475
  cp_decl_specifier_seq decl_specifiers;
15476
  bool function_definition_p = false;
15477
 
15478
  /* This function is only used when processing a template
15479
     declaration.  */
15480
  gcc_assert (innermost_scope_kind () == sk_template_parms
15481
              || innermost_scope_kind () == sk_template_spec);
15482
 
15483
  /* Defer access checks until we know what is being declared.  */
15484
  push_deferring_access_checks (dk_deferred);
15485
 
15486
  /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15487
     alternative.  */
15488
  cp_parser_decl_specifier_seq (parser,
15489
                                CP_PARSER_FLAGS_OPTIONAL,
15490
                                &decl_specifiers,
15491
                                &declares_class_or_enum);
15492
  if (friend_p)
15493
    *friend_p = cp_parser_friend_p (&decl_specifiers);
15494
 
15495
  /* There are no template typedefs.  */
15496
  if (decl_specifiers.specs[(int) ds_typedef])
15497
    {
15498
      error ("template declaration of %qs", "typedef");
15499
      decl = error_mark_node;
15500
    }
15501
 
15502
  /* Gather up the access checks that occurred the
15503
     decl-specifier-seq.  */
15504
  stop_deferring_access_checks ();
15505
 
15506
  /* Check for the declaration of a template class.  */
15507
  if (declares_class_or_enum)
15508
    {
15509
      if (cp_parser_declares_only_class_p (parser))
15510
        {
15511
          decl = shadow_tag (&decl_specifiers);
15512
 
15513
          /* In this case:
15514
 
15515
               struct C {
15516
                 friend template <typename T> struct A<T>::B;
15517
               };
15518
 
15519
             A<T>::B will be represented by a TYPENAME_TYPE, and
15520
             therefore not recognized by shadow_tag.  */
15521
          if (friend_p && *friend_p
15522
              && !decl
15523
              && decl_specifiers.type
15524
              && TYPE_P (decl_specifiers.type))
15525
            decl = decl_specifiers.type;
15526
 
15527
          if (decl && decl != error_mark_node)
15528
            decl = TYPE_NAME (decl);
15529
          else
15530
            decl = error_mark_node;
15531
 
15532
          /* Perform access checks for template parameters.  */
15533
          cp_parser_perform_template_parameter_access_checks (checks);
15534
        }
15535
    }
15536
  /* If it's not a template class, try for a template function.  If
15537
     the next token is a `;', then this declaration does not declare
15538
     anything.  But, if there were errors in the decl-specifiers, then
15539
     the error might well have come from an attempted class-specifier.
15540
     In that case, there's no need to warn about a missing declarator.  */
15541
  if (!decl
15542
      && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15543
          || decl_specifiers.type != error_mark_node))
15544
    decl = cp_parser_init_declarator (parser,
15545
                                      &decl_specifiers,
15546
                                      checks,
15547
                                      /*function_definition_allowed_p=*/true,
15548
                                      member_p,
15549
                                      declares_class_or_enum,
15550
                                      &function_definition_p);
15551
 
15552
  pop_deferring_access_checks ();
15553
 
15554
  /* Clear any current qualification; whatever comes next is the start
15555
     of something new.  */
15556
  parser->scope = NULL_TREE;
15557
  parser->qualifying_scope = NULL_TREE;
15558
  parser->object_scope = NULL_TREE;
15559
  /* Look for a trailing `;' after the declaration.  */
15560
  if (!function_definition_p
15561
      && (decl == error_mark_node
15562
          || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15563
    cp_parser_skip_to_end_of_block_or_statement (parser);
15564
 
15565
  return decl;
15566
}
15567
 
15568
/* Parse a cast-expression that is not the operand of a unary "&".  */
15569
 
15570
static tree
15571
cp_parser_simple_cast_expression (cp_parser *parser)
15572
{
15573
  return cp_parser_cast_expression (parser, /*address_p=*/false,
15574
                                    /*cast_p=*/false);
15575
}
15576
 
15577
/* Parse a functional cast to TYPE.  Returns an expression
15578
   representing the cast.  */
15579
 
15580
static tree
15581
cp_parser_functional_cast (cp_parser* parser, tree type)
15582
{
15583
  tree expression_list;
15584
  tree cast;
15585
 
15586
  expression_list
15587
    = cp_parser_parenthesized_expression_list (parser, false,
15588
                                               /*cast_p=*/true,
15589
                                               /*non_constant_p=*/NULL);
15590
 
15591
  cast = build_functional_cast (type, expression_list);
15592
  /* [expr.const]/1: In an integral constant expression "only type
15593
     conversions to integral or enumeration type can be used".  */
15594
  if (TREE_CODE (type) == TYPE_DECL)
15595
    type = TREE_TYPE (type);
15596
  if (cast != error_mark_node && !dependent_type_p (type)
15597
      && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15598
    {
15599
      if (cp_parser_non_integral_constant_expression
15600
          (parser, "a call to a constructor"))
15601
        return error_mark_node;
15602
    }
15603
  return cast;
15604
}
15605
 
15606
/* Save the tokens that make up the body of a member function defined
15607
   in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15608
   already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15609
   specifiers applied to the declaration.  Returns the FUNCTION_DECL
15610
   for the member function.  */
15611
 
15612
static tree
15613
cp_parser_save_member_function_body (cp_parser* parser,
15614
                                     cp_decl_specifier_seq *decl_specifiers,
15615
                                     cp_declarator *declarator,
15616
                                     tree attributes)
15617
{
15618
  cp_token *first;
15619
  cp_token *last;
15620
  tree fn;
15621
 
15622
  /* Create the function-declaration.  */
15623
  fn = start_method (decl_specifiers, declarator, attributes);
15624
  /* If something went badly wrong, bail out now.  */
15625
  if (fn == error_mark_node)
15626
    {
15627
      /* If there's a function-body, skip it.  */
15628
      if (cp_parser_token_starts_function_definition_p
15629
          (cp_lexer_peek_token (parser->lexer)))
15630
        cp_parser_skip_to_end_of_block_or_statement (parser);
15631
      return error_mark_node;
15632
    }
15633
 
15634
  /* Remember it, if there default args to post process.  */
15635
  cp_parser_save_default_args (parser, fn);
15636
 
15637
  /* Save away the tokens that make up the body of the
15638
     function.  */
15639
  first = parser->lexer->next_token;
15640
  cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15641
  /* Handle function try blocks.  */
15642
  while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15643
    cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15644
  last = parser->lexer->next_token;
15645
 
15646
  /* Save away the inline definition; we will process it when the
15647
     class is complete.  */
15648
  DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15649
  DECL_PENDING_INLINE_P (fn) = 1;
15650
 
15651
  /* We need to know that this was defined in the class, so that
15652
     friend templates are handled correctly.  */
15653
  DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15654
 
15655
  /* We're done with the inline definition.  */
15656
  finish_method (fn);
15657
 
15658
  /* Add FN to the queue of functions to be parsed later.  */
15659
  TREE_VALUE (parser->unparsed_functions_queues)
15660
    = tree_cons (NULL_TREE, fn,
15661
                 TREE_VALUE (parser->unparsed_functions_queues));
15662
 
15663
  return fn;
15664
}
15665
 
15666
/* Parse a template-argument-list, as well as the trailing ">" (but
15667
   not the opening ">").  See cp_parser_template_argument_list for the
15668
   return value.  */
15669
 
15670
static tree
15671
cp_parser_enclosed_template_argument_list (cp_parser* parser)
15672
{
15673
  tree arguments;
15674
  tree saved_scope;
15675
  tree saved_qualifying_scope;
15676
  tree saved_object_scope;
15677
  bool saved_greater_than_is_operator_p;
15678
  bool saved_skip_evaluation;
15679
 
15680
  /* [temp.names]
15681
 
15682
     When parsing a template-id, the first non-nested `>' is taken as
15683
     the end of the template-argument-list rather than a greater-than
15684
     operator.  */
15685
  saved_greater_than_is_operator_p
15686
    = parser->greater_than_is_operator_p;
15687
  parser->greater_than_is_operator_p = false;
15688
  /* Parsing the argument list may modify SCOPE, so we save it
15689
     here.  */
15690
  saved_scope = parser->scope;
15691
  saved_qualifying_scope = parser->qualifying_scope;
15692
  saved_object_scope = parser->object_scope;
15693
  /* We need to evaluate the template arguments, even though this
15694
     template-id may be nested within a "sizeof".  */
15695
  saved_skip_evaluation = skip_evaluation;
15696
  skip_evaluation = false;
15697
  /* Parse the template-argument-list itself.  */
15698
  if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15699
    arguments = NULL_TREE;
15700
  else
15701
    arguments = cp_parser_template_argument_list (parser);
15702
  /* Look for the `>' that ends the template-argument-list. If we find
15703
     a '>>' instead, it's probably just a typo.  */
15704
  if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15705
    {
15706
      if (!saved_greater_than_is_operator_p)
15707
        {
15708
          /* If we're in a nested template argument list, the '>>' has
15709
            to be a typo for '> >'. We emit the error message, but we
15710
            continue parsing and we push a '>' as next token, so that
15711
            the argument list will be parsed correctly.  Note that the
15712
            global source location is still on the token before the
15713
            '>>', so we need to say explicitly where we want it.  */
15714
          cp_token *token = cp_lexer_peek_token (parser->lexer);
15715
          error ("%H%<>>%> should be %<> >%> "
15716
                 "within a nested template argument list",
15717
                 &token->location);
15718
 
15719
          /* ??? Proper recovery should terminate two levels of
15720
             template argument list here.  */
15721
          token->type = CPP_GREATER;
15722
        }
15723
      else
15724
        {
15725
          /* If this is not a nested template argument list, the '>>'
15726
            is a typo for '>'. Emit an error message and continue.
15727
            Same deal about the token location, but here we can get it
15728
            right by consuming the '>>' before issuing the diagnostic.  */
15729
          cp_lexer_consume_token (parser->lexer);
15730
          error ("spurious %<>>%>, use %<>%> to terminate "
15731
                 "a template argument list");
15732
        }
15733
    }
15734
  else
15735
    cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15736
  /* The `>' token might be a greater-than operator again now.  */
15737
  parser->greater_than_is_operator_p
15738
    = saved_greater_than_is_operator_p;
15739
  /* Restore the SAVED_SCOPE.  */
15740
  parser->scope = saved_scope;
15741
  parser->qualifying_scope = saved_qualifying_scope;
15742
  parser->object_scope = saved_object_scope;
15743
  skip_evaluation = saved_skip_evaluation;
15744
 
15745
  return arguments;
15746
}
15747
 
15748
/* MEMBER_FUNCTION is a member function, or a friend.  If default
15749
   arguments, or the body of the function have not yet been parsed,
15750
   parse them now.  */
15751
 
15752
static void
15753
cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15754
{
15755
  /* If this member is a template, get the underlying
15756
     FUNCTION_DECL.  */
15757
  if (DECL_FUNCTION_TEMPLATE_P (member_function))
15758
    member_function = DECL_TEMPLATE_RESULT (member_function);
15759
 
15760
  /* There should not be any class definitions in progress at this
15761
     point; the bodies of members are only parsed outside of all class
15762
     definitions.  */
15763
  gcc_assert (parser->num_classes_being_defined == 0);
15764
  /* While we're parsing the member functions we might encounter more
15765
     classes.  We want to handle them right away, but we don't want
15766
     them getting mixed up with functions that are currently in the
15767
     queue.  */
15768
  parser->unparsed_functions_queues
15769
    = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15770
 
15771
  /* Make sure that any template parameters are in scope.  */
15772
  maybe_begin_member_template_processing (member_function);
15773
 
15774
  /* If the body of the function has not yet been parsed, parse it
15775
     now.  */
15776
  if (DECL_PENDING_INLINE_P (member_function))
15777
    {
15778
      tree function_scope;
15779
      cp_token_cache *tokens;
15780
 
15781
      /* The function is no longer pending; we are processing it.  */
15782
      tokens = DECL_PENDING_INLINE_INFO (member_function);
15783
      DECL_PENDING_INLINE_INFO (member_function) = NULL;
15784
      DECL_PENDING_INLINE_P (member_function) = 0;
15785
 
15786
      /* If this is a local class, enter the scope of the containing
15787
         function.  */
15788
      function_scope = current_function_decl;
15789
      if (function_scope)
15790
        push_function_context_to (function_scope);
15791
 
15792
 
15793
      /* Push the body of the function onto the lexer stack.  */
15794
      cp_parser_push_lexer_for_tokens (parser, tokens);
15795
 
15796
      /* Let the front end know that we going to be defining this
15797
         function.  */
15798
      start_preparsed_function (member_function, NULL_TREE,
15799
                                SF_PRE_PARSED | SF_INCLASS_INLINE);
15800
 
15801
      /* Don't do access checking if it is a templated function.  */
15802
      if (processing_template_decl)
15803
        push_deferring_access_checks (dk_no_check);
15804
 
15805
      /* Now, parse the body of the function.  */
15806
      cp_parser_function_definition_after_declarator (parser,
15807
                                                      /*inline_p=*/true);
15808
 
15809
      if (processing_template_decl)
15810
        pop_deferring_access_checks ();
15811
 
15812
      /* Leave the scope of the containing function.  */
15813
      if (function_scope)
15814
        pop_function_context_from (function_scope);
15815
      cp_parser_pop_lexer (parser);
15816
    }
15817
 
15818
  /* Remove any template parameters from the symbol table.  */
15819
  maybe_end_member_template_processing ();
15820
 
15821
  /* Restore the queue.  */
15822
  parser->unparsed_functions_queues
15823
    = TREE_CHAIN (parser->unparsed_functions_queues);
15824
}
15825
 
15826
/* If DECL contains any default args, remember it on the unparsed
15827
   functions queue.  */
15828
 
15829
static void
15830
cp_parser_save_default_args (cp_parser* parser, tree decl)
15831
{
15832
  tree probe;
15833
 
15834
  for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15835
       probe;
15836
       probe = TREE_CHAIN (probe))
15837
    if (TREE_PURPOSE (probe))
15838
      {
15839
        TREE_PURPOSE (parser->unparsed_functions_queues)
15840
          = tree_cons (current_class_type, decl,
15841
                       TREE_PURPOSE (parser->unparsed_functions_queues));
15842
        break;
15843
      }
15844
  return;
15845
}
15846
 
15847
/* FN is a FUNCTION_DECL which may contains a parameter with an
15848
   unparsed DEFAULT_ARG.  Parse the default args now.  This function
15849
   assumes that the current scope is the scope in which the default
15850
   argument should be processed.  */
15851
 
15852
static void
15853
cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15854
{
15855
  bool saved_local_variables_forbidden_p;
15856
  tree parm;
15857
 
15858
  /* While we're parsing the default args, we might (due to the
15859
     statement expression extension) encounter more classes.  We want
15860
     to handle them right away, but we don't want them getting mixed
15861
     up with default args that are currently in the queue.  */
15862
  parser->unparsed_functions_queues
15863
    = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15864
 
15865
  /* Local variable names (and the `this' keyword) may not appear
15866
     in a default argument.  */
15867
  saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15868
  parser->local_variables_forbidden_p = true;
15869
 
15870
  for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15871
       parm;
15872
       parm = TREE_CHAIN (parm))
15873
    {
15874
      cp_token_cache *tokens;
15875
      tree default_arg = TREE_PURPOSE (parm);
15876
      tree parsed_arg;
15877
      VEC(tree,gc) *insts;
15878
      tree copy;
15879
      unsigned ix;
15880
 
15881
      if (!default_arg)
15882
        continue;
15883
 
15884
      if (TREE_CODE (default_arg) != DEFAULT_ARG)
15885
        /* This can happen for a friend declaration for a function
15886
           already declared with default arguments.  */
15887
        continue;
15888
 
15889
       /* Push the saved tokens for the default argument onto the parser's
15890
          lexer stack.  */
15891
      tokens = DEFARG_TOKENS (default_arg);
15892
      cp_parser_push_lexer_for_tokens (parser, tokens);
15893
 
15894
      /* Parse the assignment-expression.  */
15895
      parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15896
 
15897
      if (!processing_template_decl)
15898
        parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
15899
 
15900
      TREE_PURPOSE (parm) = parsed_arg;
15901
 
15902
      /* Update any instantiations we've already created.  */
15903
      for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15904
           VEC_iterate (tree, insts, ix, copy); ix++)
15905
        TREE_PURPOSE (copy) = parsed_arg;
15906
 
15907
      /* If the token stream has not been completely used up, then
15908
         there was extra junk after the end of the default
15909
         argument.  */
15910
      if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15911
        cp_parser_error (parser, "expected %<,%>");
15912
 
15913
      /* Revert to the main lexer.  */
15914
      cp_parser_pop_lexer (parser);
15915
    }
15916
 
15917
  /* Make sure no default arg is missing.  */
15918
  check_default_args (fn);
15919
 
15920
  /* Restore the state of local_variables_forbidden_p.  */
15921
  parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15922
 
15923
  /* Restore the queue.  */
15924
  parser->unparsed_functions_queues
15925
    = TREE_CHAIN (parser->unparsed_functions_queues);
15926
}
15927
 
15928
/* Parse the operand of `sizeof' (or a similar operator).  Returns
15929
   either a TYPE or an expression, depending on the form of the
15930
   input.  The KEYWORD indicates which kind of expression we have
15931
   encountered.  */
15932
 
15933
static tree
15934
cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15935
{
15936
  static const char *format;
15937
  tree expr = NULL_TREE;
15938
  const char *saved_message;
15939
  bool saved_integral_constant_expression_p;
15940
  bool saved_non_integral_constant_expression_p;
15941
 
15942
  /* Initialize FORMAT the first time we get here.  */
15943
  if (!format)
15944
    format = "types may not be defined in '%s' expressions";
15945
 
15946
  /* Types cannot be defined in a `sizeof' expression.  Save away the
15947
     old message.  */
15948
  saved_message = parser->type_definition_forbidden_message;
15949
  /* And create the new one.  */
15950
  parser->type_definition_forbidden_message
15951
    = xmalloc (strlen (format)
15952
               + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15953
               + 1 /* `\0' */);
15954
  sprintf ((char *) parser->type_definition_forbidden_message,
15955
           format, IDENTIFIER_POINTER (ridpointers[keyword]));
15956
 
15957
  /* The restrictions on constant-expressions do not apply inside
15958
     sizeof expressions.  */
15959
  saved_integral_constant_expression_p
15960
    = parser->integral_constant_expression_p;
15961
  saved_non_integral_constant_expression_p
15962
    = parser->non_integral_constant_expression_p;
15963
  parser->integral_constant_expression_p = false;
15964
 
15965
  /* Do not actually evaluate the expression.  */
15966
  ++skip_evaluation;
15967
  /* If it's a `(', then we might be looking at the type-id
15968
     construction.  */
15969
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15970
    {
15971
      tree type;
15972
      bool saved_in_type_id_in_expr_p;
15973
 
15974
      /* We can't be sure yet whether we're looking at a type-id or an
15975
         expression.  */
15976
      cp_parser_parse_tentatively (parser);
15977
      /* Consume the `('.  */
15978
      cp_lexer_consume_token (parser->lexer);
15979
      /* Parse the type-id.  */
15980
      saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15981
      parser->in_type_id_in_expr_p = true;
15982
      type = cp_parser_type_id (parser);
15983
      parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15984
      /* Now, look for the trailing `)'.  */
15985
      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15986
      /* If all went well, then we're done.  */
15987
      if (cp_parser_parse_definitely (parser))
15988
        {
15989
          cp_decl_specifier_seq decl_specs;
15990
 
15991
          /* Build a trivial decl-specifier-seq.  */
15992
          clear_decl_specs (&decl_specs);
15993
          decl_specs.type = type;
15994
 
15995
          /* Call grokdeclarator to figure out what type this is.  */
15996
          expr = grokdeclarator (NULL,
15997
                                 &decl_specs,
15998
                                 TYPENAME,
15999
                                 /*initialized=*/0,
16000
                                 /*attrlist=*/NULL);
16001
        }
16002
    }
16003
 
16004
  /* If the type-id production did not work out, then we must be
16005
     looking at the unary-expression production.  */
16006
  if (!expr)
16007
    expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16008
                                       /*cast_p=*/false);
16009
  /* Go back to evaluating expressions.  */
16010
  --skip_evaluation;
16011
 
16012
  /* Free the message we created.  */
16013
  free ((char *) parser->type_definition_forbidden_message);
16014
  /* And restore the old one.  */
16015
  parser->type_definition_forbidden_message = saved_message;
16016
  parser->integral_constant_expression_p
16017
    = saved_integral_constant_expression_p;
16018
  parser->non_integral_constant_expression_p
16019
    = saved_non_integral_constant_expression_p;
16020
 
16021
  return expr;
16022
}
16023
 
16024
/* If the current declaration has no declarator, return true.  */
16025
 
16026
static bool
16027
cp_parser_declares_only_class_p (cp_parser *parser)
16028
{
16029
  /* If the next token is a `;' or a `,' then there is no
16030
     declarator.  */
16031
  return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16032
          || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16033
}
16034
 
16035
/* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
16036
 
16037
static void
16038
cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
16039
                             cp_storage_class storage_class)
16040
{
16041
  if (decl_specs->storage_class != sc_none)
16042
    decl_specs->multiple_storage_classes_p = true;
16043
  else
16044
    decl_specs->storage_class = storage_class;
16045
}
16046
 
16047
/* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
16048
   is true, the type is a user-defined type; otherwise it is a
16049
   built-in type specified by a keyword.  */
16050
 
16051
static void
16052
cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16053
                              tree type_spec,
16054
                              bool user_defined_p)
16055
{
16056
  decl_specs->any_specifiers_p = true;
16057
 
16058
  /* If the user tries to redeclare bool or wchar_t (with, for
16059
     example, in "typedef int wchar_t;") we remember that this is what
16060
     happened.  In system headers, we ignore these declarations so
16061
     that G++ can work with system headers that are not C++-safe.  */
16062
  if (decl_specs->specs[(int) ds_typedef]
16063
      && !user_defined_p
16064
      && (type_spec == boolean_type_node
16065
          || type_spec == wchar_type_node)
16066
      && (decl_specs->type
16067
          || decl_specs->specs[(int) ds_long]
16068
          || decl_specs->specs[(int) ds_short]
16069
          || decl_specs->specs[(int) ds_unsigned]
16070
          || decl_specs->specs[(int) ds_signed]))
16071
    {
16072
      decl_specs->redefined_builtin_type = type_spec;
16073
      if (!decl_specs->type)
16074
        {
16075
          decl_specs->type = type_spec;
16076
          decl_specs->user_defined_type_p = false;
16077
        }
16078
    }
16079
  else if (decl_specs->type)
16080
    decl_specs->multiple_types_p = true;
16081
  else
16082
    {
16083
      decl_specs->type = type_spec;
16084
      decl_specs->user_defined_type_p = user_defined_p;
16085
      decl_specs->redefined_builtin_type = NULL_TREE;
16086
    }
16087
}
16088
 
16089
/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16090
   Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
16091
 
16092
static bool
16093
cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16094
{
16095
  return decl_specifiers->specs[(int) ds_friend] != 0;
16096
}
16097
 
16098
/* If the next token is of the indicated TYPE, consume it.  Otherwise,
16099
   issue an error message indicating that TOKEN_DESC was expected.
16100
 
16101
   Returns the token consumed, if the token had the appropriate type.
16102
   Otherwise, returns NULL.  */
16103
 
16104
static cp_token *
16105
cp_parser_require (cp_parser* parser,
16106
                   enum cpp_ttype type,
16107
                   const char* token_desc)
16108
{
16109
  if (cp_lexer_next_token_is (parser->lexer, type))
16110
    return cp_lexer_consume_token (parser->lexer);
16111
  else
16112
    {
16113
      /* Output the MESSAGE -- unless we're parsing tentatively.  */
16114
      if (!cp_parser_simulate_error (parser))
16115
        {
16116
          char *message = concat ("expected ", token_desc, NULL);
16117
          cp_parser_error (parser, message);
16118
          free (message);
16119
        }
16120
      return NULL;
16121
    }
16122
}
16123
 
16124
/* Like cp_parser_require, except that tokens will be skipped until
16125
   the desired token is found.  An error message is still produced if
16126
   the next token is not as expected.  */
16127
 
16128
static void
16129
cp_parser_skip_until_found (cp_parser* parser,
16130
                            enum cpp_ttype type,
16131
                            const char* token_desc)
16132
{
16133
  cp_token *token;
16134
  unsigned nesting_depth = 0;
16135
 
16136
  if (cp_parser_require (parser, type, token_desc))
16137
    return;
16138
 
16139
  /* Skip tokens until the desired token is found.  */
16140
  while (true)
16141
    {
16142
      /* Peek at the next token.  */
16143
      token = cp_lexer_peek_token (parser->lexer);
16144
      /* If we've reached the token we want, consume it and
16145
         stop.  */
16146
      if (token->type == type && !nesting_depth)
16147
        {
16148
          cp_lexer_consume_token (parser->lexer);
16149
          return;
16150
        }
16151
      /* If we've run out of tokens, stop.  */
16152
      if (token->type == CPP_EOF)
16153
        return;
16154
      if (token->type == CPP_OPEN_BRACE
16155
          || token->type == CPP_OPEN_PAREN
16156
          || token->type == CPP_OPEN_SQUARE)
16157
        ++nesting_depth;
16158
      else if (token->type == CPP_CLOSE_BRACE
16159
               || token->type == CPP_CLOSE_PAREN
16160
               || token->type == CPP_CLOSE_SQUARE)
16161
        {
16162
          if (nesting_depth-- == 0)
16163
            return;
16164
        }
16165
      /* Consume this token.  */
16166
      cp_lexer_consume_token (parser->lexer);
16167
    }
16168
}
16169
 
16170
/* If the next token is the indicated keyword, consume it.  Otherwise,
16171
   issue an error message indicating that TOKEN_DESC was expected.
16172
 
16173
   Returns the token consumed, if the token had the appropriate type.
16174
   Otherwise, returns NULL.  */
16175
 
16176
static cp_token *
16177
cp_parser_require_keyword (cp_parser* parser,
16178
                           enum rid keyword,
16179
                           const char* token_desc)
16180
{
16181
  cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16182
 
16183
  if (token && token->keyword != keyword)
16184
    {
16185
      dyn_string_t error_msg;
16186
 
16187
      /* Format the error message.  */
16188
      error_msg = dyn_string_new (0);
16189
      dyn_string_append_cstr (error_msg, "expected ");
16190
      dyn_string_append_cstr (error_msg, token_desc);
16191
      cp_parser_error (parser, error_msg->s);
16192
      dyn_string_delete (error_msg);
16193
      return NULL;
16194
    }
16195
 
16196
  return token;
16197
}
16198
 
16199
/* Returns TRUE iff TOKEN is a token that can begin the body of a
16200
   function-definition.  */
16201
 
16202
static bool
16203
cp_parser_token_starts_function_definition_p (cp_token* token)
16204
{
16205
  return (/* An ordinary function-body begins with an `{'.  */
16206
          token->type == CPP_OPEN_BRACE
16207
          /* A ctor-initializer begins with a `:'.  */
16208
          || token->type == CPP_COLON
16209
          /* A function-try-block begins with `try'.  */
16210
          || token->keyword == RID_TRY
16211
          /* The named return value extension begins with `return'.  */
16212
          || token->keyword == RID_RETURN);
16213
}
16214
 
16215
/* Returns TRUE iff the next token is the ":" or "{" beginning a class
16216
   definition.  */
16217
 
16218
static bool
16219
cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16220
{
16221
  cp_token *token;
16222
 
16223
  token = cp_lexer_peek_token (parser->lexer);
16224
  return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16225
}
16226
 
16227
/* Returns TRUE iff the next token is the "," or ">" ending a
16228
   template-argument.  */
16229
 
16230
static bool
16231
cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16232
{
16233
  cp_token *token;
16234
 
16235
  token = cp_lexer_peek_token (parser->lexer);
16236
  return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16237
}
16238
 
16239
/* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16240
   (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
16241
 
16242
static bool
16243
cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16244
                                                     size_t n)
16245
{
16246
  cp_token *token;
16247
 
16248
  token = cp_lexer_peek_nth_token (parser->lexer, n);
16249
  if (token->type == CPP_LESS)
16250
    return true;
16251
  /* Check for the sequence `<::' in the original code. It would be lexed as
16252
     `[:', where `[' is a digraph, and there is no whitespace before
16253
     `:'.  */
16254
  if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16255
    {
16256
      cp_token *token2;
16257
      token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16258
      if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16259
        return true;
16260
    }
16261
  return false;
16262
}
16263
 
16264
/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16265
   or none_type otherwise.  */
16266
 
16267
static enum tag_types
16268
cp_parser_token_is_class_key (cp_token* token)
16269
{
16270
  switch (token->keyword)
16271
    {
16272
    case RID_CLASS:
16273
      return class_type;
16274
    case RID_STRUCT:
16275
      return record_type;
16276
    case RID_UNION:
16277
      return union_type;
16278
 
16279
    default:
16280
      return none_type;
16281
    }
16282
}
16283
 
16284
/* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16285
 
16286
static void
16287
cp_parser_check_class_key (enum tag_types class_key, tree type)
16288
{
16289
  if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16290
    pedwarn ("%qs tag used in naming %q#T",
16291
            class_key == union_type ? "union"
16292
             : class_key == record_type ? "struct" : "class",
16293
             type);
16294
}
16295
 
16296
/* Issue an error message if DECL is redeclared with different
16297
   access than its original declaration [class.access.spec/3].
16298
   This applies to nested classes and nested class templates.
16299
   [class.mem/1].  */
16300
 
16301
static void
16302
cp_parser_check_access_in_redeclaration (tree decl)
16303
{
16304
  if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16305
    return;
16306
 
16307
  if ((TREE_PRIVATE (decl)
16308
       != (current_access_specifier == access_private_node))
16309
      || (TREE_PROTECTED (decl)
16310
          != (current_access_specifier == access_protected_node)))
16311
    error ("%qD redeclared with different access", decl);
16312
}
16313
 
16314
/* Look for the `template' keyword, as a syntactic disambiguator.
16315
   Return TRUE iff it is present, in which case it will be
16316
   consumed.  */
16317
 
16318
static bool
16319
cp_parser_optional_template_keyword (cp_parser *parser)
16320
{
16321
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16322
    {
16323
      /* The `template' keyword can only be used within templates;
16324
         outside templates the parser can always figure out what is a
16325
         template and what is not.  */
16326
      if (!processing_template_decl)
16327
        {
16328
          error ("%<template%> (as a disambiguator) is only allowed "
16329
                 "within templates");
16330
          /* If this part of the token stream is rescanned, the same
16331
             error message would be generated.  So, we purge the token
16332
             from the stream.  */
16333
          cp_lexer_purge_token (parser->lexer);
16334
          return false;
16335
        }
16336
      else
16337
        {
16338
          /* Consume the `template' keyword.  */
16339
          cp_lexer_consume_token (parser->lexer);
16340
          return true;
16341
        }
16342
    }
16343
 
16344
  return false;
16345
}
16346
 
16347
/* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16348
   set PARSER->SCOPE, and perform other related actions.  */
16349
 
16350
static void
16351
cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16352
{
16353
  tree value;
16354
  tree check;
16355
 
16356
  /* Get the stored value.  */
16357
  value = cp_lexer_consume_token (parser->lexer)->value;
16358
  /* Perform any access checks that were deferred.  */
16359
  for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16360
    perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16361
  /* Set the scope from the stored value.  */
16362
  parser->scope = TREE_VALUE (value);
16363
  parser->qualifying_scope = TREE_TYPE (value);
16364
  parser->object_scope = NULL_TREE;
16365
}
16366
 
16367
/* Consume tokens up through a non-nested END token.  */
16368
 
16369
static void
16370
cp_parser_cache_group (cp_parser *parser,
16371
                       enum cpp_ttype end,
16372
                       unsigned depth)
16373
{
16374
  while (true)
16375
    {
16376
      cp_token *token;
16377
 
16378
      /* Abort a parenthesized expression if we encounter a brace.  */
16379
      if ((end == CPP_CLOSE_PAREN || depth == 0)
16380
          && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16381
        return;
16382
      /* If we've reached the end of the file, stop.  */
16383
      if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16384
        return;
16385
      /* Consume the next token.  */
16386
      token = cp_lexer_consume_token (parser->lexer);
16387
      /* See if it starts a new group.  */
16388
      if (token->type == CPP_OPEN_BRACE)
16389
        {
16390
          cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16391
          if (depth == 0)
16392
            return;
16393
        }
16394
      else if (token->type == CPP_OPEN_PAREN)
16395
        cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16396
      else if (token->type == end)
16397
        return;
16398
    }
16399
}
16400
 
16401
/* Begin parsing tentatively.  We always save tokens while parsing
16402
   tentatively so that if the tentative parsing fails we can restore the
16403
   tokens.  */
16404
 
16405
static void
16406
cp_parser_parse_tentatively (cp_parser* parser)
16407
{
16408
  /* Enter a new parsing context.  */
16409
  parser->context = cp_parser_context_new (parser->context);
16410
  /* Begin saving tokens.  */
16411
  cp_lexer_save_tokens (parser->lexer);
16412
  /* In order to avoid repetitive access control error messages,
16413
     access checks are queued up until we are no longer parsing
16414
     tentatively.  */
16415
  push_deferring_access_checks (dk_deferred);
16416
}
16417
 
16418
/* Commit to the currently active tentative parse.  */
16419
 
16420
static void
16421
cp_parser_commit_to_tentative_parse (cp_parser* parser)
16422
{
16423
  cp_parser_context *context;
16424
  cp_lexer *lexer;
16425
 
16426
  /* Mark all of the levels as committed.  */
16427
  lexer = parser->lexer;
16428
  for (context = parser->context; context->next; context = context->next)
16429
    {
16430
      if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16431
        break;
16432
      context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16433
      while (!cp_lexer_saving_tokens (lexer))
16434
        lexer = lexer->next;
16435
      cp_lexer_commit_tokens (lexer);
16436
    }
16437
}
16438
 
16439
/* Abort the currently active tentative parse.  All consumed tokens
16440
   will be rolled back, and no diagnostics will be issued.  */
16441
 
16442
static void
16443
cp_parser_abort_tentative_parse (cp_parser* parser)
16444
{
16445
  cp_parser_simulate_error (parser);
16446
  /* Now, pretend that we want to see if the construct was
16447
     successfully parsed.  */
16448
  cp_parser_parse_definitely (parser);
16449
}
16450
 
16451
/* Stop parsing tentatively.  If a parse error has occurred, restore the
16452
   token stream.  Otherwise, commit to the tokens we have consumed.
16453
   Returns true if no error occurred; false otherwise.  */
16454
 
16455
static bool
16456
cp_parser_parse_definitely (cp_parser* parser)
16457
{
16458
  bool error_occurred;
16459
  cp_parser_context *context;
16460
 
16461
  /* Remember whether or not an error occurred, since we are about to
16462
     destroy that information.  */
16463
  error_occurred = cp_parser_error_occurred (parser);
16464
  /* Remove the topmost context from the stack.  */
16465
  context = parser->context;
16466
  parser->context = context->next;
16467
  /* If no parse errors occurred, commit to the tentative parse.  */
16468
  if (!error_occurred)
16469
    {
16470
      /* Commit to the tokens read tentatively, unless that was
16471
         already done.  */
16472
      if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16473
        cp_lexer_commit_tokens (parser->lexer);
16474
 
16475
      pop_to_parent_deferring_access_checks ();
16476
    }
16477
  /* Otherwise, if errors occurred, roll back our state so that things
16478
     are just as they were before we began the tentative parse.  */
16479
  else
16480
    {
16481
      cp_lexer_rollback_tokens (parser->lexer);
16482
      pop_deferring_access_checks ();
16483
    }
16484
  /* Add the context to the front of the free list.  */
16485
  context->next = cp_parser_context_free_list;
16486
  cp_parser_context_free_list = context;
16487
 
16488
  return !error_occurred;
16489
}
16490
 
16491
/* Returns true if we are parsing tentatively and are not committed to
16492
   this tentative parse.  */
16493
 
16494
static bool
16495
cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16496
{
16497
  return (cp_parser_parsing_tentatively (parser)
16498
          && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16499
}
16500
 
16501
/* Returns nonzero iff an error has occurred during the most recent
16502
   tentative parse.  */
16503
 
16504
static bool
16505
cp_parser_error_occurred (cp_parser* parser)
16506
{
16507
  return (cp_parser_parsing_tentatively (parser)
16508
          && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16509
}
16510
 
16511
/* Returns nonzero if GNU extensions are allowed.  */
16512
 
16513
static bool
16514
cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16515
{
16516
  return parser->allow_gnu_extensions_p;
16517
}
16518
 
16519
/* Objective-C++ Productions */
16520
 
16521
 
16522
/* Parse an Objective-C expression, which feeds into a primary-expression
16523
   above.
16524
 
16525
   objc-expression:
16526
     objc-message-expression
16527
     objc-string-literal
16528
     objc-encode-expression
16529
     objc-protocol-expression
16530
     objc-selector-expression
16531
 
16532
  Returns a tree representation of the expression.  */
16533
 
16534
static tree
16535
cp_parser_objc_expression (cp_parser* parser)
16536
{
16537
  /* Try to figure out what kind of declaration is present.  */
16538
  cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16539
 
16540
  switch (kwd->type)
16541
    {
16542
    case CPP_OPEN_SQUARE:
16543
      return cp_parser_objc_message_expression (parser);
16544
 
16545
    case CPP_OBJC_STRING:
16546
      kwd = cp_lexer_consume_token (parser->lexer);
16547
      return objc_build_string_object (kwd->value);
16548
 
16549
    case CPP_KEYWORD:
16550
      switch (kwd->keyword)
16551
        {
16552
        case RID_AT_ENCODE:
16553
          return cp_parser_objc_encode_expression (parser);
16554
 
16555
        case RID_AT_PROTOCOL:
16556
          return cp_parser_objc_protocol_expression (parser);
16557
 
16558
        case RID_AT_SELECTOR:
16559
          return cp_parser_objc_selector_expression (parser);
16560
 
16561
        default:
16562
          break;
16563
        }
16564
    default:
16565
      error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16566
      cp_parser_skip_to_end_of_block_or_statement (parser);
16567
    }
16568
 
16569
  return error_mark_node;
16570
}
16571
 
16572
/* Parse an Objective-C message expression.
16573
 
16574
   objc-message-expression:
16575
     [ objc-message-receiver objc-message-args ]
16576
 
16577
   Returns a representation of an Objective-C message.  */
16578
 
16579
static tree
16580
cp_parser_objc_message_expression (cp_parser* parser)
16581
{
16582
  tree receiver, messageargs;
16583
 
16584
  cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
16585
  receiver = cp_parser_objc_message_receiver (parser);
16586
  messageargs = cp_parser_objc_message_args (parser);
16587
  cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16588
 
16589
  return objc_build_message_expr (build_tree_list (receiver, messageargs));
16590
}
16591
 
16592
/* Parse an objc-message-receiver.
16593
 
16594
   objc-message-receiver:
16595
     expression
16596
     simple-type-specifier
16597
 
16598
  Returns a representation of the type or expression.  */
16599
 
16600
static tree
16601
cp_parser_objc_message_receiver (cp_parser* parser)
16602
{
16603
  tree rcv;
16604
 
16605
  /* An Objective-C message receiver may be either (1) a type
16606
     or (2) an expression.  */
16607
  cp_parser_parse_tentatively (parser);
16608
  rcv = cp_parser_expression (parser, false);
16609
 
16610
  if (cp_parser_parse_definitely (parser))
16611
    return rcv;
16612
 
16613
  rcv = cp_parser_simple_type_specifier (parser,
16614
                                         /*decl_specs=*/NULL,
16615
                                         CP_PARSER_FLAGS_NONE);
16616
 
16617
  return objc_get_class_reference (rcv);
16618
}
16619
 
16620
/* Parse the arguments and selectors comprising an Objective-C message.
16621
 
16622
   objc-message-args:
16623
     objc-selector
16624
     objc-selector-args
16625
     objc-selector-args , objc-comma-args
16626
 
16627
   objc-selector-args:
16628
     objc-selector [opt] : assignment-expression
16629
     objc-selector-args objc-selector [opt] : assignment-expression
16630
 
16631
   objc-comma-args:
16632
     assignment-expression
16633
     objc-comma-args , assignment-expression
16634
 
16635
   Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16636
   selector arguments and TREE_VALUE containing a list of comma
16637
   arguments.  */
16638
 
16639
static tree
16640
cp_parser_objc_message_args (cp_parser* parser)
16641
{
16642
  tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16643
  bool maybe_unary_selector_p = true;
16644
  cp_token *token = cp_lexer_peek_token (parser->lexer);
16645
 
16646
  while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16647
    {
16648
      tree selector = NULL_TREE, arg;
16649
 
16650
      if (token->type != CPP_COLON)
16651
        selector = cp_parser_objc_selector (parser);
16652
 
16653
      /* Detect if we have a unary selector.  */
16654
      if (maybe_unary_selector_p
16655
          && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16656
        return build_tree_list (selector, NULL_TREE);
16657
 
16658
      maybe_unary_selector_p = false;
16659
      cp_parser_require (parser, CPP_COLON, "`:'");
16660
      arg = cp_parser_assignment_expression (parser, false);
16661
 
16662
      sel_args
16663
        = chainon (sel_args,
16664
                   build_tree_list (selector, arg));
16665
 
16666
      token = cp_lexer_peek_token (parser->lexer);
16667
    }
16668
 
16669
  /* Handle non-selector arguments, if any. */
16670
  while (token->type == CPP_COMMA)
16671
    {
16672
      tree arg;
16673
 
16674
      cp_lexer_consume_token (parser->lexer);
16675
      arg = cp_parser_assignment_expression (parser, false);
16676
 
16677
      addl_args
16678
        = chainon (addl_args,
16679
                   build_tree_list (NULL_TREE, arg));
16680
 
16681
      token = cp_lexer_peek_token (parser->lexer);
16682
    }
16683
 
16684
  return build_tree_list (sel_args, addl_args);
16685
}
16686
 
16687
/* Parse an Objective-C encode expression.
16688
 
16689
   objc-encode-expression:
16690
     @encode objc-typename
16691
 
16692
   Returns an encoded representation of the type argument.  */
16693
 
16694
static tree
16695
cp_parser_objc_encode_expression (cp_parser* parser)
16696
{
16697
  tree type;
16698
 
16699
  cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
16700
  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16701
  type = complete_type (cp_parser_type_id (parser));
16702
  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16703
 
16704
  if (!type)
16705
    {
16706
      error ("%<@encode%> must specify a type as an argument");
16707
      return error_mark_node;
16708
    }
16709
 
16710
  return objc_build_encode_expr (type);
16711
}
16712
 
16713
/* Parse an Objective-C @defs expression.  */
16714
 
16715
static tree
16716
cp_parser_objc_defs_expression (cp_parser *parser)
16717
{
16718
  tree name;
16719
 
16720
  cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
16721
  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16722
  name = cp_parser_identifier (parser);
16723
  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16724
 
16725
  return objc_get_class_ivars (name);
16726
}
16727
 
16728
/* Parse an Objective-C protocol expression.
16729
 
16730
  objc-protocol-expression:
16731
    @protocol ( identifier )
16732
 
16733
  Returns a representation of the protocol expression.  */
16734
 
16735
static tree
16736
cp_parser_objc_protocol_expression (cp_parser* parser)
16737
{
16738
  tree proto;
16739
 
16740
  cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
16741
  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16742
  proto = cp_parser_identifier (parser);
16743
  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16744
 
16745
  return objc_build_protocol_expr (proto);
16746
}
16747
 
16748
/* Parse an Objective-C selector expression.
16749
 
16750
   objc-selector-expression:
16751
     @selector ( objc-method-signature )
16752
 
16753
   objc-method-signature:
16754
     objc-selector
16755
     objc-selector-seq
16756
 
16757
   objc-selector-seq:
16758
     objc-selector :
16759
     objc-selector-seq objc-selector :
16760
 
16761
  Returns a representation of the method selector.  */
16762
 
16763
static tree
16764
cp_parser_objc_selector_expression (cp_parser* parser)
16765
{
16766
  tree sel_seq = NULL_TREE;
16767
  bool maybe_unary_selector_p = true;
16768
  cp_token *token;
16769
 
16770
  cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
16771
  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16772
  token = cp_lexer_peek_token (parser->lexer);
16773
 
16774
  while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16775
         || token->type == CPP_SCOPE)
16776
    {
16777
      tree selector = NULL_TREE;
16778
 
16779
      if (token->type != CPP_COLON
16780
          || token->type == CPP_SCOPE)
16781
        selector = cp_parser_objc_selector (parser);
16782
 
16783
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16784
          && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16785
        {
16786
          /* Detect if we have a unary selector.  */
16787
          if (maybe_unary_selector_p)
16788
            {
16789
              sel_seq = selector;
16790
              goto finish_selector;
16791
            }
16792
          else
16793
            {
16794
              cp_parser_error (parser, "expected %<:%>");
16795
            }
16796
        }
16797
      maybe_unary_selector_p = false;
16798
      token = cp_lexer_consume_token (parser->lexer);
16799
 
16800
      if (token->type == CPP_SCOPE)
16801
        {
16802
          sel_seq
16803
            = chainon (sel_seq,
16804
                       build_tree_list (selector, NULL_TREE));
16805
          sel_seq
16806
            = chainon (sel_seq,
16807
                       build_tree_list (NULL_TREE, NULL_TREE));
16808
        }
16809
      else
16810
        sel_seq
16811
          = chainon (sel_seq,
16812
                     build_tree_list (selector, NULL_TREE));
16813
 
16814
      token = cp_lexer_peek_token (parser->lexer);
16815
    }
16816
 
16817
 finish_selector:
16818
  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16819
 
16820
  return objc_build_selector_expr (sel_seq);
16821
}
16822
 
16823
/* Parse a list of identifiers.
16824
 
16825
   objc-identifier-list:
16826
     identifier
16827
     objc-identifier-list , identifier
16828
 
16829
   Returns a TREE_LIST of identifier nodes.  */
16830
 
16831
static tree
16832
cp_parser_objc_identifier_list (cp_parser* parser)
16833
{
16834
  tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16835
  cp_token *sep = cp_lexer_peek_token (parser->lexer);
16836
 
16837
  while (sep->type == CPP_COMMA)
16838
    {
16839
      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
16840
      list = chainon (list,
16841
                      build_tree_list (NULL_TREE,
16842
                                       cp_parser_identifier (parser)));
16843
      sep = cp_lexer_peek_token (parser->lexer);
16844
    }
16845
 
16846
  return list;
16847
}
16848
 
16849
/* Parse an Objective-C alias declaration.
16850
 
16851
   objc-alias-declaration:
16852
     @compatibility_alias identifier identifier ;
16853
 
16854
   This function registers the alias mapping with the Objective-C front-end.
16855
   It returns nothing.  */
16856
 
16857
static void
16858
cp_parser_objc_alias_declaration (cp_parser* parser)
16859
{
16860
  tree alias, orig;
16861
 
16862
  cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
16863
  alias = cp_parser_identifier (parser);
16864
  orig = cp_parser_identifier (parser);
16865
  objc_declare_alias (alias, orig);
16866
  cp_parser_consume_semicolon_at_end_of_statement (parser);
16867
}
16868
 
16869
/* Parse an Objective-C class forward-declaration.
16870
 
16871
   objc-class-declaration:
16872
     @class objc-identifier-list ;
16873
 
16874
   The function registers the forward declarations with the Objective-C
16875
   front-end.  It returns nothing.  */
16876
 
16877
static void
16878
cp_parser_objc_class_declaration (cp_parser* parser)
16879
{
16880
  cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
16881
  objc_declare_class (cp_parser_objc_identifier_list (parser));
16882
  cp_parser_consume_semicolon_at_end_of_statement (parser);
16883
}
16884
 
16885
/* Parse a list of Objective-C protocol references.
16886
 
16887
   objc-protocol-refs-opt:
16888
     objc-protocol-refs [opt]
16889
 
16890
   objc-protocol-refs:
16891
     < objc-identifier-list >
16892
 
16893
   Returns a TREE_LIST of identifiers, if any.  */
16894
 
16895
static tree
16896
cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16897
{
16898
  tree protorefs = NULL_TREE;
16899
 
16900
  if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16901
    {
16902
      cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
16903
      protorefs = cp_parser_objc_identifier_list (parser);
16904
      cp_parser_require (parser, CPP_GREATER, "`>'");
16905
    }
16906
 
16907
  return protorefs;
16908
}
16909
 
16910
/* Parse a Objective-C visibility specification.  */
16911
 
16912
static void
16913
cp_parser_objc_visibility_spec (cp_parser* parser)
16914
{
16915
  cp_token *vis = cp_lexer_peek_token (parser->lexer);
16916
 
16917
  switch (vis->keyword)
16918
    {
16919
    case RID_AT_PRIVATE:
16920
      objc_set_visibility (2);
16921
      break;
16922
    case RID_AT_PROTECTED:
16923
      objc_set_visibility (0);
16924
      break;
16925
    case RID_AT_PUBLIC:
16926
      objc_set_visibility (1);
16927
      break;
16928
    default:
16929
      return;
16930
    }
16931
 
16932
  /* Eat '@private'/'@protected'/'@public'.  */
16933
  cp_lexer_consume_token (parser->lexer);
16934
}
16935
 
16936
/* Parse an Objective-C method type.  */
16937
 
16938
static void
16939
cp_parser_objc_method_type (cp_parser* parser)
16940
{
16941
  objc_set_method_type
16942
   (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16943
    ? PLUS_EXPR
16944
    : MINUS_EXPR);
16945
}
16946
 
16947
/* Parse an Objective-C protocol qualifier.  */
16948
 
16949
static tree
16950
cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16951
{
16952
  tree quals = NULL_TREE, node;
16953
  cp_token *token = cp_lexer_peek_token (parser->lexer);
16954
 
16955
  node = token->value;
16956
 
16957
  while (node && TREE_CODE (node) == IDENTIFIER_NODE
16958
         && (node == ridpointers [(int) RID_IN]
16959
             || node == ridpointers [(int) RID_OUT]
16960
             || node == ridpointers [(int) RID_INOUT]
16961
             || node == ridpointers [(int) RID_BYCOPY]
16962
             || node == ridpointers [(int) RID_BYREF]
16963
             || node == ridpointers [(int) RID_ONEWAY]))
16964
    {
16965
      quals = tree_cons (NULL_TREE, node, quals);
16966
      cp_lexer_consume_token (parser->lexer);
16967
      token = cp_lexer_peek_token (parser->lexer);
16968
      node = token->value;
16969
    }
16970
 
16971
  return quals;
16972
}
16973
 
16974
/* Parse an Objective-C typename.  */
16975
 
16976
static tree
16977
cp_parser_objc_typename (cp_parser* parser)
16978
{
16979
  tree typename = NULL_TREE;
16980
 
16981
  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16982
    {
16983
      tree proto_quals, cp_type = NULL_TREE;
16984
 
16985
      cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
16986
      proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16987
 
16988
      /* An ObjC type name may consist of just protocol qualifiers, in which
16989
         case the type shall default to 'id'.  */
16990
      if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16991
        cp_type = cp_parser_type_id (parser);
16992
 
16993
      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16994
      typename = build_tree_list (proto_quals, cp_type);
16995
    }
16996
 
16997
  return typename;
16998
}
16999
 
17000
/* Check to see if TYPE refers to an Objective-C selector name.  */
17001
 
17002
static bool
17003
cp_parser_objc_selector_p (enum cpp_ttype type)
17004
{
17005
  return (type == CPP_NAME || type == CPP_KEYWORD
17006
          || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17007
          || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17008
          || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17009
          || type == CPP_XOR || type == CPP_XOR_EQ);
17010
}
17011
 
17012
/* Parse an Objective-C selector.  */
17013
 
17014
static tree
17015
cp_parser_objc_selector (cp_parser* parser)
17016
{
17017
  cp_token *token = cp_lexer_consume_token (parser->lexer);
17018
 
17019
  if (!cp_parser_objc_selector_p (token->type))
17020
    {
17021
      error ("invalid Objective-C++ selector name");
17022
      return error_mark_node;
17023
    }
17024
 
17025
  /* C++ operator names are allowed to appear in ObjC selectors.  */
17026
  switch (token->type)
17027
    {
17028
    case CPP_AND_AND: return get_identifier ("and");
17029
    case CPP_AND_EQ: return get_identifier ("and_eq");
17030
    case CPP_AND: return get_identifier ("bitand");
17031
    case CPP_OR: return get_identifier ("bitor");
17032
    case CPP_COMPL: return get_identifier ("compl");
17033
    case CPP_NOT: return get_identifier ("not");
17034
    case CPP_NOT_EQ: return get_identifier ("not_eq");
17035
    case CPP_OR_OR: return get_identifier ("or");
17036
    case CPP_OR_EQ: return get_identifier ("or_eq");
17037
    case CPP_XOR: return get_identifier ("xor");
17038
    case CPP_XOR_EQ: return get_identifier ("xor_eq");
17039
    default: return token->value;
17040
    }
17041
}
17042
 
17043
/* Parse an Objective-C params list.  */
17044
 
17045
static tree
17046
cp_parser_objc_method_keyword_params (cp_parser* parser)
17047
{
17048
  tree params = NULL_TREE;
17049
  bool maybe_unary_selector_p = true;
17050
  cp_token *token = cp_lexer_peek_token (parser->lexer);
17051
 
17052
  while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17053
    {
17054
      tree selector = NULL_TREE, typename, identifier;
17055
 
17056
      if (token->type != CPP_COLON)
17057
        selector = cp_parser_objc_selector (parser);
17058
 
17059
      /* Detect if we have a unary selector.  */
17060
      if (maybe_unary_selector_p
17061
          && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17062
        return selector;
17063
 
17064
      maybe_unary_selector_p = false;
17065
      cp_parser_require (parser, CPP_COLON, "`:'");
17066
      typename = cp_parser_objc_typename (parser);
17067
      identifier = cp_parser_identifier (parser);
17068
 
17069
      params
17070
        = chainon (params,
17071
                   objc_build_keyword_decl (selector,
17072
                                            typename,
17073
                                            identifier));
17074
 
17075
      token = cp_lexer_peek_token (parser->lexer);
17076
    }
17077
 
17078
  return params;
17079
}
17080
 
17081
/* Parse the non-keyword Objective-C params.  */
17082
 
17083
static tree
17084
cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17085
{
17086
  tree params = make_node (TREE_LIST);
17087
  cp_token *token = cp_lexer_peek_token (parser->lexer);
17088
  *ellipsisp = false;  /* Initially, assume no ellipsis.  */
17089
 
17090
  while (token->type == CPP_COMMA)
17091
    {
17092
      cp_parameter_declarator *parmdecl;
17093
      tree parm;
17094
 
17095
      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17096
      token = cp_lexer_peek_token (parser->lexer);
17097
 
17098
      if (token->type == CPP_ELLIPSIS)
17099
        {
17100
          cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
17101
          *ellipsisp = true;
17102
          break;
17103
        }
17104
 
17105
      parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17106
      parm = grokdeclarator (parmdecl->declarator,
17107
                             &parmdecl->decl_specifiers,
17108
                             PARM, /*initialized=*/0,
17109
                             /*attrlist=*/NULL);
17110
 
17111
      chainon (params, build_tree_list (NULL_TREE, parm));
17112
      token = cp_lexer_peek_token (parser->lexer);
17113
    }
17114
 
17115
  return params;
17116
}
17117
 
17118
/* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
17119
 
17120
static void
17121
cp_parser_objc_interstitial_code (cp_parser* parser)
17122
{
17123
  cp_token *token = cp_lexer_peek_token (parser->lexer);
17124
 
17125
  /* If the next token is `extern' and the following token is a string
17126
     literal, then we have a linkage specification.  */
17127
  if (token->keyword == RID_EXTERN
17128
      && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17129
    cp_parser_linkage_specification (parser);
17130
  /* Handle #pragma, if any.  */
17131
  else if (token->type == CPP_PRAGMA)
17132
    cp_lexer_handle_pragma (parser->lexer);
17133
  /* Allow stray semicolons.  */
17134
  else if (token->type == CPP_SEMICOLON)
17135
    cp_lexer_consume_token (parser->lexer);
17136
  /* Finally, try to parse a block-declaration, or a function-definition.  */
17137
  else
17138
    cp_parser_block_declaration (parser, /*statement_p=*/false);
17139
}
17140
 
17141
/* Parse a method signature.  */
17142
 
17143
static tree
17144
cp_parser_objc_method_signature (cp_parser* parser)
17145
{
17146
  tree rettype, kwdparms, optparms;
17147
  bool ellipsis = false;
17148
 
17149
  cp_parser_objc_method_type (parser);
17150
  rettype = cp_parser_objc_typename (parser);
17151
  kwdparms = cp_parser_objc_method_keyword_params (parser);
17152
  optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17153
 
17154
  return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17155
}
17156
 
17157
/* Pars an Objective-C method prototype list.  */
17158
 
17159
static void
17160
cp_parser_objc_method_prototype_list (cp_parser* parser)
17161
{
17162
  cp_token *token = cp_lexer_peek_token (parser->lexer);
17163
 
17164
  while (token->keyword != RID_AT_END)
17165
    {
17166
      if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17167
        {
17168
          objc_add_method_declaration
17169
           (cp_parser_objc_method_signature (parser));
17170
          cp_parser_consume_semicolon_at_end_of_statement (parser);
17171
        }
17172
      else
17173
        /* Allow for interspersed non-ObjC++ code.  */
17174
        cp_parser_objc_interstitial_code (parser);
17175
 
17176
      token = cp_lexer_peek_token (parser->lexer);
17177
    }
17178
 
17179
  cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17180
  objc_finish_interface ();
17181
}
17182
 
17183
/* Parse an Objective-C method definition list.  */
17184
 
17185
static void
17186
cp_parser_objc_method_definition_list (cp_parser* parser)
17187
{
17188
  cp_token *token = cp_lexer_peek_token (parser->lexer);
17189
 
17190
  while (token->keyword != RID_AT_END)
17191
    {
17192
      tree meth;
17193
 
17194
      if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17195
        {
17196
          push_deferring_access_checks (dk_deferred);
17197
          objc_start_method_definition
17198
           (cp_parser_objc_method_signature (parser));
17199
 
17200
          /* For historical reasons, we accept an optional semicolon.  */
17201
          if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17202
            cp_lexer_consume_token (parser->lexer);
17203
 
17204
          perform_deferred_access_checks ();
17205
          stop_deferring_access_checks ();
17206
          meth = cp_parser_function_definition_after_declarator (parser,
17207
                                                                 false);
17208
          pop_deferring_access_checks ();
17209
          objc_finish_method_definition (meth);
17210
        }
17211
      else
17212
        /* Allow for interspersed non-ObjC++ code.  */
17213
        cp_parser_objc_interstitial_code (parser);
17214
 
17215
      token = cp_lexer_peek_token (parser->lexer);
17216
    }
17217
 
17218
  cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17219
  objc_finish_implementation ();
17220
}
17221
 
17222
/* Parse Objective-C ivars.  */
17223
 
17224
static void
17225
cp_parser_objc_class_ivars (cp_parser* parser)
17226
{
17227
  cp_token *token = cp_lexer_peek_token (parser->lexer);
17228
 
17229
  if (token->type != CPP_OPEN_BRACE)
17230
    return;     /* No ivars specified.  */
17231
 
17232
  cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
17233
  token = cp_lexer_peek_token (parser->lexer);
17234
 
17235
  while (token->type != CPP_CLOSE_BRACE)
17236
    {
17237
      cp_decl_specifier_seq declspecs;
17238
      int decl_class_or_enum_p;
17239
      tree prefix_attributes;
17240
 
17241
      cp_parser_objc_visibility_spec (parser);
17242
 
17243
      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17244
        break;
17245
 
17246
      cp_parser_decl_specifier_seq (parser,
17247
                                    CP_PARSER_FLAGS_OPTIONAL,
17248
                                    &declspecs,
17249
                                    &decl_class_or_enum_p);
17250
      prefix_attributes = declspecs.attributes;
17251
      declspecs.attributes = NULL_TREE;
17252
 
17253
      /* Keep going until we hit the `;' at the end of the
17254
         declaration.  */
17255
      while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17256
        {
17257
          tree width = NULL_TREE, attributes, first_attribute, decl;
17258
          cp_declarator *declarator = NULL;
17259
          int ctor_dtor_or_conv_p;
17260
 
17261
          /* Check for a (possibly unnamed) bitfield declaration.  */
17262
          token = cp_lexer_peek_token (parser->lexer);
17263
          if (token->type == CPP_COLON)
17264
            goto eat_colon;
17265
 
17266
          if (token->type == CPP_NAME
17267
              && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17268
                  == CPP_COLON))
17269
            {
17270
              /* Get the name of the bitfield.  */
17271
              declarator = make_id_declarator (NULL_TREE,
17272
                                               cp_parser_identifier (parser),
17273
                                               sfk_none);
17274
 
17275
             eat_colon:
17276
              cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17277
              /* Get the width of the bitfield.  */
17278
              width
17279
                = cp_parser_constant_expression (parser,
17280
                                                 /*allow_non_constant=*/false,
17281
                                                 NULL);
17282
            }
17283
          else
17284
            {
17285
              /* Parse the declarator.  */
17286
              declarator
17287
                = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17288
                                        &ctor_dtor_or_conv_p,
17289
                                        /*parenthesized_p=*/NULL,
17290
                                        /*member_p=*/false);
17291
            }
17292
 
17293
          /* Look for attributes that apply to the ivar.  */
17294
          attributes = cp_parser_attributes_opt (parser);
17295
          /* Remember which attributes are prefix attributes and
17296
             which are not.  */
17297
          first_attribute = attributes;
17298
          /* Combine the attributes.  */
17299
          attributes = chainon (prefix_attributes, attributes);
17300
 
17301
          if (width)
17302
            {
17303
              /* Create the bitfield declaration.  */
17304
              decl = grokbitfield (declarator, &declspecs, width);
17305
              cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17306
            }
17307
          else
17308
            decl = grokfield (declarator, &declspecs,
17309
                              NULL_TREE, /*init_const_expr_p=*/false,
17310
                              NULL_TREE, attributes);
17311
 
17312
          /* Add the instance variable.  */
17313
          objc_add_instance_variable (decl);
17314
 
17315
          /* Reset PREFIX_ATTRIBUTES.  */
17316
          while (attributes && TREE_CHAIN (attributes) != first_attribute)
17317
            attributes = TREE_CHAIN (attributes);
17318
          if (attributes)
17319
            TREE_CHAIN (attributes) = NULL_TREE;
17320
 
17321
          token = cp_lexer_peek_token (parser->lexer);
17322
 
17323
          if (token->type == CPP_COMMA)
17324
            {
17325
              cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17326
              continue;
17327
            }
17328
          break;
17329
        }
17330
 
17331
      cp_parser_consume_semicolon_at_end_of_statement (parser);
17332
      token = cp_lexer_peek_token (parser->lexer);
17333
    }
17334
 
17335
  cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17336
  /* For historical reasons, we accept an optional semicolon.  */
17337
  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17338
    cp_lexer_consume_token (parser->lexer);
17339
}
17340
 
17341
/* Parse an Objective-C protocol declaration.  */
17342
 
17343
static void
17344
cp_parser_objc_protocol_declaration (cp_parser* parser)
17345
{
17346
  tree proto, protorefs;
17347
  cp_token *tok;
17348
 
17349
  cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17350
  if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17351
    {
17352
      error ("identifier expected after %<@protocol%>");
17353
      goto finish;
17354
    }
17355
 
17356
  /* See if we have a forward declaration or a definition.  */
17357
  tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17358
 
17359
  /* Try a forward declaration first.  */
17360
  if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17361
    {
17362
      objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17363
     finish:
17364
      cp_parser_consume_semicolon_at_end_of_statement (parser);
17365
    }
17366
 
17367
  /* Ok, we got a full-fledged definition (or at least should).  */
17368
  else
17369
    {
17370
      proto = cp_parser_identifier (parser);
17371
      protorefs = cp_parser_objc_protocol_refs_opt (parser);
17372
      objc_start_protocol (proto, protorefs);
17373
      cp_parser_objc_method_prototype_list (parser);
17374
    }
17375
}
17376
 
17377
/* Parse an Objective-C superclass or category.  */
17378
 
17379
static void
17380
cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17381
                                                          tree *categ)
17382
{
17383
  cp_token *next = cp_lexer_peek_token (parser->lexer);
17384
 
17385
  *super = *categ = NULL_TREE;
17386
  if (next->type == CPP_COLON)
17387
    {
17388
      cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17389
      *super = cp_parser_identifier (parser);
17390
    }
17391
  else if (next->type == CPP_OPEN_PAREN)
17392
    {
17393
      cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17394
      *categ = cp_parser_identifier (parser);
17395
      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17396
    }
17397
}
17398
 
17399
/* Parse an Objective-C class interface.  */
17400
 
17401
static void
17402
cp_parser_objc_class_interface (cp_parser* parser)
17403
{
17404
  tree name, super, categ, protos;
17405
 
17406
  cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17407
  name = cp_parser_identifier (parser);
17408
  cp_parser_objc_superclass_or_category (parser, &super, &categ);
17409
  protos = cp_parser_objc_protocol_refs_opt (parser);
17410
 
17411
  /* We have either a class or a category on our hands.  */
17412
  if (categ)
17413
    objc_start_category_interface (name, categ, protos);
17414
  else
17415
    {
17416
      objc_start_class_interface (name, super, protos);
17417
      /* Handle instance variable declarations, if any.  */
17418
      cp_parser_objc_class_ivars (parser);
17419
      objc_continue_interface ();
17420
    }
17421
 
17422
  cp_parser_objc_method_prototype_list (parser);
17423
}
17424
 
17425
/* Parse an Objective-C class implementation.  */
17426
 
17427
static void
17428
cp_parser_objc_class_implementation (cp_parser* parser)
17429
{
17430
  tree name, super, categ;
17431
 
17432
  cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17433
  name = cp_parser_identifier (parser);
17434
  cp_parser_objc_superclass_or_category (parser, &super, &categ);
17435
 
17436
  /* We have either a class or a category on our hands.  */
17437
  if (categ)
17438
    objc_start_category_implementation (name, categ);
17439
  else
17440
    {
17441
      objc_start_class_implementation (name, super);
17442
      /* Handle instance variable declarations, if any.  */
17443
      cp_parser_objc_class_ivars (parser);
17444
      objc_continue_implementation ();
17445
    }
17446
 
17447
  cp_parser_objc_method_definition_list (parser);
17448
}
17449
 
17450
/* Consume the @end token and finish off the implementation.  */
17451
 
17452
static void
17453
cp_parser_objc_end_implementation (cp_parser* parser)
17454
{
17455
  cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17456
  objc_finish_implementation ();
17457
}
17458
 
17459
/* Parse an Objective-C declaration.  */
17460
 
17461
static void
17462
cp_parser_objc_declaration (cp_parser* parser)
17463
{
17464
  /* Try to figure out what kind of declaration is present.  */
17465
  cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17466
 
17467
  switch (kwd->keyword)
17468
    {
17469
    case RID_AT_ALIAS:
17470
      cp_parser_objc_alias_declaration (parser);
17471
      break;
17472
    case RID_AT_CLASS:
17473
      cp_parser_objc_class_declaration (parser);
17474
      break;
17475
    case RID_AT_PROTOCOL:
17476
      cp_parser_objc_protocol_declaration (parser);
17477
      break;
17478
    case RID_AT_INTERFACE:
17479
      cp_parser_objc_class_interface (parser);
17480
      break;
17481
    case RID_AT_IMPLEMENTATION:
17482
      cp_parser_objc_class_implementation (parser);
17483
      break;
17484
    case RID_AT_END:
17485
      cp_parser_objc_end_implementation (parser);
17486
      break;
17487
    default:
17488
      error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17489
      cp_parser_skip_to_end_of_block_or_statement (parser);
17490
    }
17491
}
17492
 
17493
/* Parse an Objective-C try-catch-finally statement.
17494
 
17495
   objc-try-catch-finally-stmt:
17496
     @try compound-statement objc-catch-clause-seq [opt]
17497
       objc-finally-clause [opt]
17498
 
17499
   objc-catch-clause-seq:
17500
     objc-catch-clause objc-catch-clause-seq [opt]
17501
 
17502
   objc-catch-clause:
17503
     @catch ( exception-declaration ) compound-statement
17504
 
17505
   objc-finally-clause
17506
     @finally compound-statement
17507
 
17508
   Returns NULL_TREE.  */
17509
 
17510
static tree
17511
cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17512
  location_t location;
17513
  tree stmt;
17514
 
17515
  cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17516
  location = cp_lexer_peek_token (parser->lexer)->location;
17517
  /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17518
     node, lest it get absorbed into the surrounding block.  */
17519
  stmt = push_stmt_list ();
17520
  cp_parser_compound_statement (parser, NULL, false);
17521
  objc_begin_try_stmt (location, pop_stmt_list (stmt));
17522
 
17523
  while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17524
    {
17525
      cp_parameter_declarator *parmdecl;
17526
      tree parm;
17527
 
17528
      cp_lexer_consume_token (parser->lexer);
17529
      cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17530
      parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17531
      parm = grokdeclarator (parmdecl->declarator,
17532
                             &parmdecl->decl_specifiers,
17533
                             PARM, /*initialized=*/0,
17534
                             /*attrlist=*/NULL);
17535
      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17536
      objc_begin_catch_clause (parm);
17537
      cp_parser_compound_statement (parser, NULL, false);
17538
      objc_finish_catch_clause ();
17539
    }
17540
 
17541
  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17542
    {
17543
      cp_lexer_consume_token (parser->lexer);
17544
      location = cp_lexer_peek_token (parser->lexer)->location;
17545
      /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17546
         node, lest it get absorbed into the surrounding block.  */
17547
      stmt = push_stmt_list ();
17548
      cp_parser_compound_statement (parser, NULL, false);
17549
      objc_build_finally_clause (location, pop_stmt_list (stmt));
17550
    }
17551
 
17552
  return objc_finish_try_stmt ();
17553
}
17554
 
17555
/* Parse an Objective-C synchronized statement.
17556
 
17557
   objc-synchronized-stmt:
17558
     @synchronized ( expression ) compound-statement
17559
 
17560
   Returns NULL_TREE.  */
17561
 
17562
static tree
17563
cp_parser_objc_synchronized_statement (cp_parser *parser) {
17564
  location_t location;
17565
  tree lock, stmt;
17566
 
17567
  cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17568
 
17569
  location = cp_lexer_peek_token (parser->lexer)->location;
17570
  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17571
  lock = cp_parser_expression (parser, false);
17572
  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17573
 
17574
  /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17575
     node, lest it get absorbed into the surrounding block.  */
17576
  stmt = push_stmt_list ();
17577
  cp_parser_compound_statement (parser, NULL, false);
17578
 
17579
  return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17580
}
17581
 
17582
/* Parse an Objective-C throw statement.
17583
 
17584
   objc-throw-stmt:
17585
     @throw assignment-expression [opt] ;
17586
 
17587
   Returns a constructed '@throw' statement.  */
17588
 
17589
static tree
17590
cp_parser_objc_throw_statement (cp_parser *parser) {
17591
  tree expr = NULL_TREE;
17592
 
17593
  cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17594
 
17595
  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17596
    expr = cp_parser_assignment_expression (parser, false);
17597
 
17598
  cp_parser_consume_semicolon_at_end_of_statement (parser);
17599
 
17600
  return objc_build_throw_stmt (expr);
17601
}
17602
 
17603
/* Parse an Objective-C statement.  */
17604
 
17605
static tree
17606
cp_parser_objc_statement (cp_parser * parser) {
17607
  /* Try to figure out what kind of declaration is present.  */
17608
  cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17609
 
17610
  switch (kwd->keyword)
17611
    {
17612
    case RID_AT_TRY:
17613
      return cp_parser_objc_try_catch_finally_statement (parser);
17614
    case RID_AT_SYNCHRONIZED:
17615
      return cp_parser_objc_synchronized_statement (parser);
17616
    case RID_AT_THROW:
17617
      return cp_parser_objc_throw_statement (parser);
17618
    default:
17619
      error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17620
      cp_parser_skip_to_end_of_block_or_statement (parser);
17621
    }
17622
 
17623
  return error_mark_node;
17624
}
17625
 
17626
/* The parser.  */
17627
 
17628
static GTY (()) cp_parser *the_parser;
17629
 
17630
/* External interface.  */
17631
 
17632
/* Parse one entire translation unit.  */
17633
 
17634
void
17635
c_parse_file (void)
17636
{
17637
  bool error_occurred;
17638
  static bool already_called = false;
17639
 
17640
  if (already_called)
17641
    {
17642
      sorry ("inter-module optimizations not implemented for C++");
17643
      return;
17644
    }
17645
  already_called = true;
17646
 
17647
  the_parser = cp_parser_new ();
17648
  push_deferring_access_checks (flag_access_control
17649
                                ? dk_no_deferred : dk_no_check);
17650
  error_occurred = cp_parser_translation_unit (the_parser);
17651
  the_parser = NULL;
17652
}
17653
 
17654
/* This variable must be provided by every front end.  */
17655
 
17656
int yydebug;
17657
 
17658
#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.